#!/usr/bin/perl ############################################################################# # Define Variables # $server = "/zeus/standard_unix/www.vernoncoleman.com"; $baseurl = "$server"; # @files - listing of expressions to search for. Default is all htm/html files. # To search all HTML, text, and files containing the word bob, we'd use: # @files = (".htm", ".txt", "bob"); @files = (".htm", ".html"); @directories_to_search = (".." ,"../books"); $search_url = "$ENV{'SCRIPT_NAME'}"; $name_of_org = "Vernon Coleman:"; # Done # ############################################################################# &parse_form; if ($FORM{valid_search} eq "true") { &get_files; $results = 0; &search; &return_html; } else { &output_form; } ############################################################################ sub print_header #7/17/97 12:53PM ############################################################################ { print STDOUT < Vernon Coleman Search Facility END } ##print_header ############################################################################ sub print_footer #7/17/97 12:53PM ############################################################################ { print STDOUT < END } ##print_footer sub output_form { $thispagetitle = "Perform a New Search"; if ( $ENV{'PERLXS'} eq "PerlIS" ) { print "HTTP/1.0 200 OK\n"; } print "Content-type: text/html\n\n"; &print_header; print <

Search For:



Display Results As:
Page Title Only

Page title and paragraph



Your search may take a few moments to display. Please be patient.

ENDHTML &print_footer; } sub parse_form { # Get the input read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'}); # Split the name-value pairs @pairs = split(/&/, $buffer); foreach $pair (@pairs) { ($name, $value) = split(/=/, $pair); $value =~ tr/+/ /; $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg; $FORM{$name} = $value; } } sub get_files { foreach $directory (@directories_to_search ) { foreach $infile (@files) { opendir(DIR, "$directory"); foreach $temp_file (sort grep(/$infile/, readdir(DIR))) { unless ( $directory eq "." ) { $temp_file = "$directory/$temp_file"; } if (-d $temp_file) { push(@directories_to_search, $temp_file); } elsif (-T $temp_file) { push(@FILES,$temp_file); } } closedir(DIR); } } } sub search { @terms = split(/\s+/, $FORM{'terms'}); foreach $FILE (@FILES) { open(FILE,"$FILE"); @LINES = ; close(FILE); $string = join(' ',@LINES); $string =~ s/\n//g; # Added by Tim Woodward 23/04/99 to remove all HTML tags $string =~ s/<[^<>]*>//g; if ($FORM{'boolean'} eq 'AND') { foreach $term (@terms) { if ($FORM{'case'} eq 'Insensitive') { if (!($string =~ /$term/i)) { $include{$FILE} = 'no'; last; } else { $include{$FILE} = 'yes'; } } elsif ($FORM{'case'} eq 'Sensitive') { if (!($string =~ /$term/)) { $include{$FILE} = 'no'; last; } else { $include{$FILE} = 'yes'; } } } } elsif ($FORM{'boolean'} eq 'OR') { foreach $term (@terms) { if ($FORM{'case'} eq 'Insensitive') { if ($string =~ /$term/i) { $include{$FILE} = 'yes'; last; } else { $include{$FILE} = 'no'; } } elsif ($FORM{'case'} eq 'Sensitive') { if ($string =~ /$term/) { $include{$FILE} = 'yes'; last; } else { $include{$FILE} = 'no'; } } } } if ($include{$FILE} eq "yes") { $results++; } open(FOO, "$FILE") || print(STDERR "$FILE not accessible\n"); # This section changes radically. $/ = "\n"; @blah = ; close(FOO); $text = ""; foreach $line ( @blah ) { $text .= $line; } @data = stat($FILE); $mtime = $data[9]; ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $isdst) = localtime($mtime); $mon = $mon + 1; $date{$FILE} = "$mon/$mday/$year"; $temptext = $text; $temptext =~ s/(\n|\cM)//g; $temptext =~ /(.*)<\/title>/i; if ($1 eq "") { $titles{$FILE} = "(no title)"; } else { $titles{$FILE} = "$1"; } $text =~ s/(\n|\cM)/ /g; if ( $text =~ /\$BODYTAG/i ) { $text =~ /\$BODYTAG(.*)/mi; $temp = "$1"; } else { $text =~ /<body (.*?)>(.*)/mi; $temp = "$2"; } $temp =~ s/<(.+?)>/ /g; $temp = &keyword_grab($temp); $abstracts{$FILE} = "$temp"; } } sub return_html { $thispagetitle = "Results Of Search"; if ( $ENV{'PERLXS'} eq "PerlIS" ) { print "HTTP/1.0 200 OK\n"; } print "Content-type: text/html\n\n"; &print_header; print "<CENTER>\n<h1>Results of Search in $name_of_org</h1>\n </center><P>\n"; if ($results == "0") { print "<h2>We're very sorry, but your search did not yield any results.</h2> You may want to broaden the parameters of your search.<BR>"; } else { if ($results != 1) { print("Your search yielded $results results.<BR>\n"); } else { print("Your search yielded a single result.<BR>\n"); } } print "<ul>\n"; foreach $key (keys(%include)) { if ($include{$key} eq 'yes') { print "<CENTER><TABLE WIDTH=400><TR><TD><li><FONT SIZE=+1><a href=\"$key\">$titles{$key}</a></FONT>\n<BR>"; if ($FORM{extended} eq "yes") { print "<STRONG>Abstract:</STRONG><BR> . . . $abstracts{$key} . . . <BR></TD></TR></TABLE></CENTER>\n"; } else { print "</TD></TR></TABLE></CENTER>\n"; } } } print "</ul>\n"; print "<hr>\n"; if ($FORM{extended} ne "yes") { print "<form method=POST name=fs action=\"$search_url\">"; print "<INPUT TYPE=HIDDEN NAME=boolean VALUE=AND>"; print "<INPUT TYPE=HIDDEN NAME=valid_search VALUE=true>"; print "<INPUT TYPE=HIDDEN NAME=case VALUE=Insensitive>"; print "<INPUT TYPE=HIDDEN NAME=extended VALUE=yes>"; print "<INPUT TYPE=HIDDEN NAME=terms VALUE=\"$FORM{'terms'}\"></form>"; print "<center><font size=+2><a href=\"javascript: document.fs.submit();\">Show Abstracts</a> | <a href=\"$search_url\">Search Again</a></font></center>\n"; } else { print "<center><font size=+2><a href=\"$search_url\">Search Again</a></font></center>\n"; } print "</ul>\n"; print "<hr>\n"; print "</body>\n</html>\n"; } ############################################################################ sub keyword_grab #7/22/97 12:14PM ############################################################################ { local($temp) = @_; local($keyword) = $terms[0]; foreach $term ( @terms ) { $temp =~ s/$term/<B>$term<\/B>/gi; } $length = length($temp); $location_of_keyword = index($temp, $keyword); if ( $location_of_keyword == -1 ) { return substr($temp, 0, rindex($temp, " ", 255)); } else { if ( $location_of_keyword >= 240 ) { $offset = rindex($temp, " ", $location_of_keyword - 240); } else { $offset = 0; } if ( ($location_of_keyword + 240) < $length ) { $rindex = rindex($temp, " ", $location_of_keyword + 240); } else { $rindex = $length; } return substr($temp, $offset, $rindex-$offset); } } ##keyword_grab