home *** CD-ROM | disk | FTP | other *** search
/ Child Computer / Child_Computer.iso / turath / Programs / _Files / cgi-bin / search_engine_turath.cgi next >
Encoding:
Text File  |  2001-09-13  |  7.3 KB  |  279 lines

  1. #!/usr/local/bin/perl
  2.  
  3. #print "Content-Type: text/html\n\n";
  4. # Define Server specific variables
  5.  
  6. require "search_engine_turath.pl";
  7.  
  8. $root_web_path = "/users/turath/httpd/htdocs/";
  9. # The following outputs the CGI Header
  10.  
  11. read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});
  12.  
  13. # Process info from Fill in Form
  14.  
  15. @pairs = split(/&/, $buffer);
  16. foreach $pair (@pairs) {
  17.    ($name, $value) = split(/=/, $pair);
  18.    $value =~ tr/+/ /;
  19.    $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
  20.    $value =~ s/<!--(.|\n)*-->//g;
  21.    $FORM{$name} = $value;
  22. }
  23.  
  24. $keywords = $FORM{'keywords'};
  25.  
  26.     #Lower case keyboard
  27.     $keywords=~ tr/`/╨/;
  28.     $keywords=~ tr/qwertyuiop[]/╓╒╦▐▌█┌σ╬═/;
  29.     $keywords=~ tr/asdfghjkl\;\'/╘╙φ╚ß╟╩Σπ▀╪/;
  30.     $keywords=~ tr/,/µ/;
  31.     $keywords=~ s/b/ß╟/g;
  32.     $keywords=~ tr/zxcvnm\.\//╞┴─╤∞╔╥┘/;
  33.  
  34.  
  35.     #Upper case keyboard
  36.     $keywords=~ tr/~/°/;
  37.     $keywords=~ tr/QWER/≤≡⌡±/;
  38.     $keywords=~ s/Y/┼/g;
  39.     $keywords=~ s/T/ß┼/g;
  40.     $keywords=~ s/G/ß├/g;
  41.     $keywords=~ s/H/├/g;
  42.     $keywords=~ tr/ASJ/÷≥▄/;
  43.     $keywords=~ tr/X/·/;
  44.     $keywords=~ s/B/ß┬/g;
  45.     $keywords=~ tr/N/┬/;
  46.  
  47.  
  48. $exact_match = $FORM{'exact_match'};
  49.  
  50. # Take the keywords that were entered and parse them into an array
  51. # of keywords based on word boundary (\s+ splits on whitespace)
  52.  
  53. @keyword_list = split(/\s+/,$keywords);
  54.  
  55. # If the person has not yet entered a search term, output a form that
  56. # will ask them for a search word.
  57.  
  58.  
  59. if ($keywords eq "") {
  60.     &PrintHeaderHTML;
  61.     &PrintNoKeywordHTML;
  62.     &PrintFooterHTML;
  63.     exit;
  64. } # End of if keywords
  65.  
  66. # Begin to send back the dynamic search results page with the header.
  67.  
  68.  &PrintHeaderHTML;
  69.  
  70. #
  71. # We traverse the whole directory structure under $root_web_path
  72. # and in doing so, we also parse the HTML files to see if they have
  73. # the keywords and what their title is.
  74. #
  75. # The following sets up the initial variables
  76. # @dirs is the array of directories as a placeholder for going back up
  77. # the directory tree when we run out of files in a subdirectory.
  78. # $cur_dir is the current directory number and is a reference to the @dirs
  79. # array.
  80. #
  81. # Directory Handles are straight ASCII and consist of "DIR" + $cur_dir
  82.  
  83. $number_of_hits = 0;
  84. $cur_dir = 0;
  85. @dirs = ($root_web_path);
  86. opendir("DIR$cur_dir", $dirs[$cur_dir]);
  87.  
  88. $end_of_all_files = 0;
  89.  
  90. while (!($end_of_all_files)) {
  91.     # The following is used to trace down the next file. If there is no
  92.     # next file (the whole directory tree was traversed), $end_of_all_files
  93.     # is set to positive and then the whole process is ended.
  94.     while (1) {
  95.     $filename = &GetNextEntry("DIR$cur_dir", $dirs[$cur_dir]);
  96.     $fullpath = "$dirs[$cur_dir]/$filename";
  97. #
  98. # CASE 1) File is null and but still can traverse back up the directory
  99. #
  100.     if (!($filename) && $cur_dir > 0) {
  101.         closedir("DIR$cur_dir");
  102.         $cur_dir--;
  103.         next;
  104.     }
  105.  
  106. #
  107. # CASE 2) File is null but nowhere else to go.  So we end the searching.
  108. #
  109.     if (!($filename)) {
  110.         closedir("DIR$cur_dir");
  111.         $end_of_all_files = 1;
  112.             last;
  113.     }
  114.  
  115. #
  116. # CASE 3) File is a directory so traverse down it.
  117. #
  118.     if (-d $fullpath) {
  119.         if (-r $fullpath && -x $fullpath) {
  120.         $cur_dir++;
  121.         $dirs[$cur_dir] = $fullpath;
  122.         opendir("DIR$cur_dir", $dirs[$cur_dir]);
  123.         next;
  124.         } else {
  125.         # Since the dir does not have r or x perms we go on
  126.         next;
  127.         }
  128.     } # End of Case 3 (File is directory
  129.  
  130. #
  131. # CASE 4) The file is an unwanted file
  132. #
  133.     $unwanted_file = 0;
  134.  
  135.     foreach (@unwanted_files) {
  136.         if ($fullpath =~ /$_/) {
  137.         $unwanted_file = 1;
  138.         }
  139.     } # End of foreach unwanted files
  140.  
  141.     if ($unwanted_file) {
  142.         next;
  143.     } # End of Case 4 Unwanted File
  144.  
  145. #
  146. # CASE 5) The file is really something to search
  147. #
  148.     # So we break out of the while loop
  149.     if (-r $fullpath) {
  150.         last;
  151.     } # Make sure the file is readable
  152.  
  153.     } # End of While (1)
  154.  
  155.  
  156.     if (!($end_of_all_files)) {
  157.  
  158. # We set the not_found_words = to the array list and pick out
  159. # words we find so that the not_found_words should not have
  160. # anything in it if all the words were found.
  161. #
  162.     @not_found_words = @keyword_list;
  163.     $are_we_in_head = 0;
  164.     open(SEARCHFILE, $fullpath);
  165.     $headline = "";
  166.     while(<SEARCHFILE>) {
  167.         $line = $_;
  168.         $headline .= $line if ($are_we_in_head == 0);
  169.         $are_we_in_head = 1
  170.         if (($line =~ m!</head>!i) || ($line =~ m!</title>!i));
  171.         &FindKeywords($exact_match, $line, *not_found_words);
  172.     } # End of SEARCHFILE
  173.     close (SEARCHFILE);
  174.  
  175.     if (@not_found_words < 1) {
  176. # Isolate out the <TITLE></TITLE> information
  177.  
  178. $headline =~ s/\n/ /g;
  179. $headline =~ m!<title>(.*)</title>!i;
  180. $title = $1;
  181.  
  182.     if ($title eq "") {
  183.         $title = "╟ß┌Σµ╟Σ █φ╤ π┌╤▌";
  184.     }
  185. $fullpath =~ s!$root_web_path/!!;
  186.  
  187. &PrintBodyHTML($fullpath, $title);
  188. $number_of_hits++;
  189.  
  190. } # If there are no not_found_words
  191.  
  192. } # If Not The End of all Files
  193. } # End of While Not At The End Of All Files
  194.  
  195.  
  196. # Print up the footer
  197.  
  198. if ($number_of_hits == 0) {
  199. &PrintNoHitsBodyHTML;
  200.  
  201. }
  202.  
  203. &PrintFooterHTML;   # Print The Footer HTML Search.....
  204.  
  205. ############################################################
  206. #
  207. # subroutine: FindKeywords
  208. #   Usage:
  209. #     &FindKeywords("on", $line, *not_found_words);
  210. #
  211. #   Parameters:
  212. #     $exact_match = "on" if we are not pattern matching
  213. #     $line = line to search on
  214. #     *not_found_words = array of keywords that have not
  215. #                        matched yet
  216. #
  217. #   Output:
  218. #     *not_found_words will have keywords spliced out of it
  219. #     as they are found.
  220. #
  221. ############################################################
  222.  
  223. sub FindKeywords
  224. {
  225.     local($exact_match, $line, *not_found_words) = @_;
  226.     local($x, $match_word);
  227.  
  228.     if ($exact_match eq "on") {
  229.     for ($x = @not_found_words; $x > 0; $x--) {
  230. # \b matches on word boundary
  231.         $match_word = $not_found_words[$x - 1];
  232.         if ($line =~ /\b$match_word\b/i) {
  233.         splice(@not_found_words,$x - 1, 1);
  234.         } # End of If
  235.     } # End of For Loop
  236.     } else {
  237.     for ($x = @not_found_words; $x > 0; $x--) {
  238.         $match_word = $not_found_words[$x - 1];
  239.         if ($line =~ /$match_word/i) {
  240.         splice(@not_found_words,$x - 1, 1);
  241.         } # End of If
  242.     } # End of For Loop
  243.     } # End of ELSE
  244.  
  245. } # End of FindKeywords
  246.  
  247. ############################################################
  248. #
  249. # subroutine: GetNextEntry
  250. #   Usage:
  251. #     &GetNextEntry(DIRECTORY_HANDLE, "directory_name");
  252. #
  253. #   Parameters:
  254. #     DIRECTORY_HANDLE = handle to currently open directory
  255. #     $directory = full path of directory
  256. #
  257. #   Output:
  258. #     $filename = name of next file, null if no more files
  259. #                 in current directory
  260. #
  261. ############################################################
  262.  
  263. sub GetNextEntry {
  264.     local($dirhandle, $directory) = @_;
  265.  
  266.     while ($filename = readdir($dirhandle)) {
  267.     if (($filename =~ /htm.?/i) ||
  268.         (!($filename =~ /^\.\.?$/) &&
  269.          -d "$directory/$filename")) {
  270.         last;
  271.     } # End of IF Filename is html document or a directory
  272.     } # End of while still stuff to read
  273.  
  274. # Filename will be valid if it is a directory or an HTML file.
  275.     $filename;
  276.  
  277. } # End of GetNextEntry
  278.  
  279.