#!/usr/bin/perl -w use strict; use DB_File; $|++; use vars qw(%db %result %names @keywords @found @match @pairs $isfound $original_Query $parse_over); my $MAXHITS = 10; my $query ="query"; my $start = 0; # only a key name for %result my $keyCount = "Just_Silly_Name"; my $script_name = &query_env('SCRIPT_NAME'); dbmopen(%db,"search_index.db",0) or die "dbmopen: $!"; &parse_form; ## store keywords and count of each matching file foreach my $keyword (@keywords) { if (defined (my $filelist = $db{lc $keyword})) { $isfound =1; # accumulate the number of keywords matched & record the keyword foreach my $fileno ($filelist=~ /(-\d+)/g) { $result{$fileno}{$keyCount}++; $result{$fileno}{$keyword} = $keyword; } } } print "Content-Type: text/html\n\n"; use HTML::Template; $ENV{HTML_TEMPLATE_ROOT} = '/where/to/store/template/file/'; my $template = HTML::Template->new(filename=>'show_result.tmpl'); $template->param('keywords' =>join ' ',@keywords); ## print the result and matched keywords if found if ($isfound) { $template->param('FOUND' => 1); # store fileno with the most matched to the least and corresponding matched keywords into array foreach my $key (sort hashValueDesc keys(%result) ) { push @found,$db{$key}; # collect the matching keywords push @match,[grep { $_ ne $keyCount } keys %{ $result{$key} }]; } my $low = $start; $low = 0 if ($low < 0 or $low > $#found); my $high = $low + $MAXHITS -1; $high = $#found if $high > $#found; #$template->param('keywords' =>join ' ',@keywords); $template->param('found' => $#found); $template->param('low' => $low+1,'high' => $high+1); my @loop; foreach ($low..$high) { my ($url,$title,$last_mod) = split /<>/,$found[$_]; my %result = ( 'title' => $title, 'match' => (join ' ',@{$match[$_]}), 'url' => $url, 'last_mod' => $last_mod, ); push (@loop,\%result); } $template->param('match_url' => \@loop); if ($low > 0) { $start = $low - $MAXHITS; $start =0 if ($start < 0); $template->param('previous_link' => "$script_name?$query=$original_Query.&start=$start"); } if ($high < $#found) { $template->param('next_link' => "$script_name?$query=$original_Query&start=".($low+ $MAXHITS)); } } else { #$template->param('NOT_FOUND' => 1); } print $template->output; sub hashValueDesc { $result{$b}{$keyCount} <=> $result{$a}{$keyCount}; } sub parse_form { if ("\U$ENV{'REQUEST_METHOD'}\E" eq 'GET') { @pairs = split(/&/, $ENV{'QUERY_STRING'}); } elsif ("\U$ENV{'REQUEST_METHOD'}\E" eq 'POST') { read(STDIN,my $buffer, $ENV{'CONTENT_LENGTH'}); @pairs = split(/&/, $buffer); } else { &error('whoops! unknown request method'); } ## End of if foreach my $pair (@pairs) { my ($name, $value) = split(/=/,$pair); $original_Query =$value if (!(defined $original_Query)); $name =~ tr/+/ /; $value =~ tr/+/ /; $name =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg; $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg; # Remove meaningless symbols $value =~ s/\W+/ /g; if (!(exists $names{$query})) { # condition order is important! if ($name ne $query or $value =~ /^\s*$/) { print "Location: http://www.easyya.com\n\n"; #exit; } else { $names{$query} = ""; @keywords = split /\s+/,$value; } } else { # if doesn't match, we still get start=0 if ($value =~ /^\d+$/) { $start = $value; $parse_over = 1; } } ## exit after two pairs done.no wasting time if user fake out something! last if $parse_over; } ## End of foreach } sub error { my ($msg) = @_; print "

$msg

\n"; exit; } sub query_env { my ($name,$default) = @_; if (($ENV{$name}) and ($ENV{$name} =~ /^(.*)$/s)) { return $1; } elsif (defined($default)) { return $default; } else { return ''; } } 1; __END__