lynx: A Text Browser (Cont.)


The following interface demonstrates how to use lynx in PHP scripts. This interface searches the URL content for the lines containing the keyword.

☂You may need to create the empty file result.txt initially, and open it via
     shell⯈ chmod 777 result.txt 
Otherwise, the file result.txt may not be written.


Displaying the Lines in the URL Content Containing the Keyword

  URL:
 

  A keyword:
 





     
  Password:                    
https://undcemcs01.und.edu/~wen.chen.hu/course/515/2/Lynx.php
<?php

# Remove leading and trailing spacing.
$URL     = trim( $_POST["URL"] );
$keyword = trim( $_POST["keyword"] );

# For security, remove some Unix metacharacters.
$meta    = array( ";", ">", ">>", ";", "*", "?", "&", "|" );
$URL     = str_replace( $meta, "", $URL );
$keyword = str_replace( $meta, "", $keyword );

if ( $_POST['act'] == "Search the URL content" ) {
  header( "Content-type: text/plain" );
  $cmd = "lynx -dump -source '" . $URL . "' > result.txt";
  echo  ( $cmd . "\n\n" );
  system( "chmod 777 result.txt ../2/" );
  system( $cmd );
  system( "chmod 755 result.txt ../2/" );
  $cmd = "grep  $keyword result.txt";
  echo  ( $cmd . "\n\n\n" );
  system( $cmd );
}

elseif ( $_POST["act"] == "Display the URL content" ) {
  header( "Content-type: text/plain" );
  $cmd = "lynx -dump -source '" . $URL . "' > result.txt";
  echo  ( $cmd . "\n\n\n" );
  system( "chmod 777 result.txt ../2/" );
  system( $cmd );
  system( "chmod 755 result.txt ../2/" );
  system( "cat result.txt" );
}

elseif ( $_POST["act"] == "Help" ) {
  header( "Content-type: text/html" );
  $file = fopen( "Help.html", "r" ) or
    exit( "Unable to open file!" );
  while ( !feof( $file ) )
    echo  fgets( $file );
  fclose( $file );
}

else {
  header( "Content-type: text/html" );
  echo( "<html><body>" );
  echo( "<h3>No such option: " . $_POST["act"] . "" );
  echo( "</body></html>" );
}

?>

The following PHP and Unix functions are used:


      “We must live together as brothers or perish together as fools.”    
      ― Martin Luther King Jr.