#!/usr/bin/perl # Shortened for URL length with switch to GET method # maxresults mr # previews pr # query q # searchkind sk #Keyword from html form for any kind of pet $anykind = "any"; # My directory for the log file $logdir = "."; #Path to the index $index = "petindex"; #Path to the configuration file $config = "search.conf"; #- Main Program -------------------------------------------------------------# #Parse the form data &parse_form_data (*FORM); #Construct a simple query $query = $FORM{'q'}; $results = $FORM{'mr'}; $temp_previews = $FORM{'pr'}; $sk = $FORM{'sk'}; if ($temp_previews eq "ON") { $show_previews = 1; } else { $show_previews = 0; } if ($query || ($sk ne $anykind) ) { &search_parse; } else { &search_error("Please enter at least one word in the \"Search for\" box."); } #- Subroutines ---------------------------------------------------------------# #Subroutine for checking and parsing the incoming form data. sub parse_form_data { local (*FORM_DATA) = @_; local ($request_method, $query_string, @key_value_pairs, $key_value, $key, $value); $request_method = $ENV{'REQUEST_METHOD'}; if ($request_method eq "GET") { $query_string = $ENV{'QUERY_STRING'}; } elsif ($request_method eq "POST") { read (STDIN, $query_string, $ENV{'CONTENT_LENGTH'}); } else { &search_error ("Forms must use either GET or POST."); } @key_value_pairs = split (/&/, $query_string); foreach $key_value (@key_value_pairs) { ($key, $value) = split (/=/, $key_value); $value =~ tr/+/ /; $value =~ s/%([\dA-Fa-f][\dA-Fa-f])/pack ("C", hex ($1))/eg; if (defined ($FORM_DATA{$key})) { $FORM_DATA{$key} = join ("\0", $FORM_DATA{$key}, $value); } else { $FORM_DATA{$key} = $value; } } } #Subroutine for constructing the search request and formating the results. sub search_parse { #Create your query command if ($results < 1 || $results > 500) {$results = 10;}; $count=0; if ($sk eq "Dogs") { $restriction = "kind=dog "; } elsif ($sk eq "Alpacas") { $restriction = "kind=Alpaca "; } elsif ($sk eq "Bats") { $restriction = "kind=bat "; } elsif ($sk eq "Birds") { $restriction = "kind=bird "; } elsif ($sk eq "Bisons") { $restriction = "kind=bison "; } elsif ($sk eq "Burros") { $restriction = "kind=burro "; } elsif ($sk eq "Camels") { $restriction = "kind=camel "; } elsif ($sk eq "Cats") { $restriction = "kind=cat "; } elsif ($sk eq "Cattle") { $restriction = "kind=cattle "; } elsif ($sk eq "Chickens") { $restriction = "kind=chicken "; } elsif ($sk eq "Chinchillas") { $restriction = "kind=chinchilla "; } elsif ($sk eq "Cows") { $restriction = "kind=cow "; } elsif ($sk eq "Deer") { $restriction = "kind=deer "; } elsif ($sk eq "Degus") { $restriction = "kind=degu "; } elsif ($sk eq "Dogs") { $restriction = "kind=dog "; } elsif ($sk eq "Donkeys") { $restriction = "kind=donkey "; } elsif ($sk eq "Ducks") { $restriction = "kind=duck "; } elsif ($sk eq "Duprasis") { $restriction = "kind=duprasi "; } elsif ($sk eq "Ferrets") { $restriction = "kind=ferret "; } elsif ($sk eq "Fish") { $restriction = "kind=fish "; } elsif ($sk eq "Frogs") { $restriction = "kind=frog "; } elsif ($sk eq "Gerbils") { $restriction = "kind=gerbil "; } elsif ($sk eq "Goats") { $restriction = "kind=goat "; } elsif ($sk eq "Guinea Pigs") { $restriction = "kind=guinea pig "; } elsif ($sk eq "Hamsters") { $restriction = "kind=hamster "; } elsif ($sk eq "Hedgehogs") { $restriction = "kind=hedgehog "; } elsif ($sk eq "Horses") { $restriction = "kind=horse "; } elsif ($sk eq "Iguanas") { $restriction = "kind=iguana "; } elsif ($sk eq "Lizards") { $restriction = "kind=lizard "; } elsif ($sk eq "Llamas") { $restriction = "kind=llama "; } elsif ($sk eq "Monkeys") { $restriction = "kind=monkey "; } elsif ($sk eq "Mice") { $restriction = "kind=mice "; } elsif ($sk eq "Mules") { $restriction = "kind=mule "; } elsif ($sk eq "Opossum") { $restriction = "kind=opossum "; } elsif ($sk eq "Parrots") { $restriction = "kind=parrot "; } elsif ($sk eq "Pigs") { $restriction = "kind=pig "; } elsif ($sk eq "Porcupines") { $restriction = "kind=porcupine "; } elsif ($sk eq "Prairie Dogs") { $restriction = "kind=prairiedog "; } elsif ($sk eq "Rabbits") { $restriction = "kind=rabbit "; } elsif ($sk eq "Raccoons") { $restriction = "kind=raccoon "; } elsif ($sk eq "Rats") { $restriction = "kind=rat "; } elsif ($sk eq "Sheep") { $restriction = "kind=sheep "; } elsif ($sk eq "Skunks") { $restriction = "kind=skunk "; } elsif ($sk eq "Snakes") { $restriction = "kind=snake "; } elsif ($sk eq "Spiders") { $restriction = "kind=spider "; } elsif ($sk eq "Squirrels") { $restriction = "kind=squirrel "; } elsif ($sk eq "Sugar Gliders") { $restriction = "kind=sugar glider "; } elsif ($sk eq "Toads") { $restriction = "kind=toad "; } elsif ($sk eq "Tortoises") { $restriction = "kind=tortoise "; } elsif ($sk eq "Turtles") { $restriction = "kind=turtle "; } elsif ($sk eq "Wallabys") { $restriction = "kind=wallaby "; } elsif ($sk eq "Whales") { $restriction = "kind=whale "; } else { $restriction = ""; } $restricted_query = $restriction . $query; # # # logging open(SEARCHLOG_FH,">> searchlog"); chdir $logdir; # print SEARCHLOG_FH (localtime(time))," ",$ENV{'REMOTE_ADDR'}," ",$restricted_query,"\n"; ($mysec,$mymin,$myhour,$mymday,$mymon,$myyear,$mywday,$myyday,$myisdst) = localtime(time); $mymon += 1; print SEARCHLOG_FH $myhour, ":", $mymin,":",$mysec," ",$mymon,"/",$mymday," ",$ENV{'REMOTE_ADDR'}," ",$restricted_query,"\n"; close(SEARCHLOG_FH); # # # open(SEARCH_P, "./search -w $restricted_query -m $results -f $index|"); #Check for errors $foundIt = 0; while () { chop; if ($foundIt == 0 && m/Number of hits/) { ($trash, $hitcount) = split /: /, $_, 2; $foundIt = 1; } if ($_ eq "err: no results") {&search_error("There are no items that matched your search request.");} if ($_ eq "err: could not open index file") {&search_error("Something went wrong.");} if ($_ eq "err: no search words specified") {&search_error("Please enter at least one word in the \"Search for\" box.");} if ($_ eq "err: a word is too common") {&search_error("One of your search terms is too common, please try again.");} next if /^\D/; $count++; push(@results, $_); } # # # if ($query eq "") { $query = "all ". $sk; } #Print the results page &html_header("Search Results"); print < Your search for $query returned $count items out of $hitcount items.


Search_Results $InLeftColumn = 1; if ($show_previews) { select(STDOUT); print ""; # print "
"; foreach (@results) { ($stringone, $title, $filesize) = split(/\"/, $_); ($rank, $url) = split(/ /, $stringone); ($PetName, $PetKind, $PetDate) = split(/ - /, $title); if ( $url =~ m#/archive/#) { $preview_image_url = "\"" . $url . "\""; $preview_image_url =~ s/.html/s.jpg/; ($Ua,$Ub,$Uc,$Ud,$Ue,$Uf,$Ug) = split("/",$url); $Uf_Images = $Uf . "Images"; $preview_image_url =~ s/$Uf/$Uf_Images/; if ($InLeftColumn == 1) { print "
"; # Change size below too print ""; print "\n"; $InLeftColumn = 0; } else { print ""; print ""; print "\n"; $InLeftColumn = 1; } } else { # Killed printing of non PET archive pages for now. # print ""; # print ""; # print "\n"; } } if ($InLeftColumn == 0) { print ""; print ""; print "\n"; } # print ""; } else { print ""; print "
    "; foreach (@results) { select(STDOUT); ($stringone, $title, $filesize) = split(/\"/, $_); ($rank, $url) = split(/ /, $stringone); print "
  1. $title
    \n"; } print "
\n"; print "
"; } &html_trailer; } # Subroutine for printing a generic HTML header. sub html_header { # $document_title = $_[0]; print < Search for: $query
Petof the Day

 \"\"$PetName
$PetKind
$PetDate
 \"\"$PetName
$PetKind
$PetDate
  $title
 
Today's Pet
Nominate Your Pet
Previous Pets
Pet Talk
Poll
Search
Why?
About Us


Dog of the Day
Dog of the Day
 
Cat of the Day
Cat of the Day
 
Pet of the Day
Pet of the Day
HTML_Header # if ($show_previews) # { # print ""; # } } # Subroutine for printing the HTML trailer. sub html_trailer { if ($show_previews) { print "
"; } print < Return to the Search page
HTML_Trailer exit; } # Subroutine for printing error messages. sub search_error { &html_header("Search Error"); $error_message = $_[0]; print "

\n$error_message

\n"; &html_trailer; }