i think this is line129
elsif ($ENV{'REQUEST_METHOD'} eq 'POST') {
i do not think you can figure it out until you look at the whole code:
#!/usr/bin/perl
use LWP::Simple;
%in = &parse_form;
&altavista;
&print_template;
sub print_template {
# --------------------------------------------------------
#
#
if (!$in{'query'}) {$error="Please enter one or more keywords:";}
if ($error) {$results = $error;}
# Bold Search Terms
@search_terms = split (/\s/, $in{'query'});
foreach $term (@search_terms) {
$results =~ s,(<[^>]+>)|(\Q$term\E),defined($1) ? $1 : "<STRONG>$2</STRONG>",gie; # Bold Search Terms
}
# Get the template
open (READIT, "results.html");
read (READIT, $template, 20000,0);
close (READIT);
$template =~ s#<%results%>#$results#igs;
$template =~ s#<%query%>#$query#igs;
$template =~ s#<%next%>#$next#igs;
$template =~ s#<%hits%>#$hits#igs;
# Print the results
print "Content-type: text/html\n\n";
print $template;
exit;
}
sub altavista {
# --------------------------------------------------------
# Search Altavista
#
$query = $in{'query'};
$query_q = &urlencode($query);
$font = "<font face=verdana size=2>";
$font2 = "<font color=#808080 face=verdana size=1>";
$page = ($in{'page'} * 10) || 0;
$pageXX = $page - 10;
$url = "http://www.altavista.com/cgi-bin/query?q=$query_q&text=yes&stq=$pageXX";
$av_results = get ($url);
$av_results or $error = "No Results Available!" and &print_template;
# If we see this, then we couldn't get a match.
$none = quotemeta ("AltaVista found no document matching your query.");
if ($av_results =~ m/$none/) {$error = "No matching sites for: <b>$query</b>" and &print_template;}
# Get the results.
else {
if ($av_results =~ /([^>\s]+) pages found./) { $hits = $1; }
else {$error = "Can't Parse Results!" and &print_template;}
while ($av_results =~ m#<b>#sog) {
$av_results =~ s/<b>//gie;
$av_results =~ s/<\/b>//gie;
}
while ($av_results =~ m#<EM>#sog) {
$av_results =~ s/<EM>//gie;
$av_results =~ s/<\/EM>//gie;
}
while ($av_results =~ m#(\d+)\.\s*\n<a href="([^"]+)">\n([^<]+)</a></dt>\n<dd>([^<]+)</dd>\n
<font color="\#808080">URL:([^\n]+)\n#sog) {
($count, $url, $title, $description, $url2) = ($1, $2, $3, $4, $5);
$url2 =~ s/\s//gie;
$url2 = "http://" . $url2;
$results .= qq~<p>$font$count. <a href="$url">$title</a>
\n$description</font>
\n$font2 $url2</font>\n~;
}
# If less than 10 don't build search bar
while ($hits =~ m#,#sog) {$hits =~ s/,//;}
$next = &next_hits($page, $hits, 10);
if ($hits <= 10) {$next="";}
}
&print_template;
}
sub next_hits {
# --------------------------------------------------------
# Creates a next hits toolbar.
#
my ($page, $numhits, $maxhits) = @_;
my ($left, $right, $upper, $lower, $next_hit, $prev_hit, $i);
my $nh = int ($page / $maxhits) || 1;
my $output = '';
$numhits =~ s/,//g;
$numhits = int ($numhits);
$next_url = $ENV{'QUERY_STRING'};
$next_url =~ s/\&page=\d+//;
$next_hit = $nh + 1;
$prev_hit = $nh - 1;
# First, set how many pages we have on the left and the right.
$left = $nh;
$right = int($numhits/$maxhits) - $nh;
# Then work out what page number we can go above and below.
($left > 7) ? ($lower = $left - 7) : ($lower = 1);
($right > 7) ? ($upper = $nh + 7) : ($upper = int($numhits/$maxhits) + 1);
# Finally, adjust those page numbers if we are near an endpoint.
(7 - $nh >= 0) and ($upper = $upper + (8 - $nh));
($nh > ($numhits/$maxhits - 7)) and ($lower = $lower - ($nh - int($numhits/$maxhits - 7) - 1));
# Then let's go through the pages and build the HTML.
($nh > 1) and ($output .= qq~<a href="$ENV{'SCRIPT_NAME'}?$next_url&page=$prev_hit">[<<]</a> ~);
for ($i = 1; $i <= int($numhits/$maxhits) + 1; $i++) {
if ($i < $lower) { $output .= " ... "; $i = ($lower-1); next; }
if ($i > $upper) { $output .= " ... "; last; }
($i == $nh) ?
($output .= qq~$i ~) :
($output .= qq~<a href="$ENV{'SCRIPT_NAME'}?$next_url&page=$i">$i</a> ~);
(($i * $maxhits) >= $numhits) and print "Exiting3" and last; # Special case if we hit exact.
}
$output .= qq~<a href="$ENV{'SCRIPT_NAME'}?$next_url&page=$next_hit">[>>]</a> ~ unless ($nh == $i);
return $output;
}
sub parse_form {
# --------------------------------------------------------
# Parses the form input and returns a hash with all the name
# value pairs. Removes any field with "---" as a value
# (as this denotes an empty SELECT field.
#
my (@pairs, %in);
my ($buffer, $pair, $name, $value);
if ($ENV{'REQUEST_METHOD'} eq 'GET') {
@pairs = split(/&/, $ENV{'QUERY_STRING'});
}
elsif ($ENV{'REQUEST_METHOD'} eq 'POST') {
read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});
@pairs = split(/&/, $buffer);
}
else {
&cgierr('You cant run this script from telnet/shell.');
}
PAIR: foreach $pair (@pairs) {
($name, $value) = split(/=/, $pair);
$name =~ tr/+/ /;
$name =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
$value =~ tr/+/ /;
$value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
($value eq "---") and next PAIR;
exists $in{$name} ? ($in{$name} .= "~~$value") : ($in{$name} = $value);
}
return %in;
}
sub urlencode {
# --------------------------------------------------------
# Escapes a string to make it suitable for printing as a URL.
#
my($toencode) = shift;
$toencode =~ s/([^a-zA-Z0-9_\-.])/uc sprintf("%%x",ord($1))/eg;
$toencode =~ s/\//\//g;
return $toencode;
}
1;