#!/usr/local/bin/perl
#completely stolen from jsd by jjb
#modified to look at access log instead of error log

print "Content-type: text/html\n\n<title>Access Log Search</title>";

# If a user goes directly to this page, CONTENT_LENGTH will
# be zero, so we pull in the form for them to fill out
# (which redirects them right back to this script, but with
# some CGI stuff this time)

if ($ENV{'CONTENT_LENGTH'} < 1) {
  print <<ENDOFTEXT;
<h1>Access Log Search</h1>
Enter a string in the box. I will then search for it
in the accesss log and print what I find.
<p>
<form method="POST">
String (actually it can be a regexp): <input type="text" name="rexp" size=40
maxlength=120><p>
<input type = "submit" value ="Start Searching">
</form>
ENDOFTEXT

    exit 0;
}

# They submitted some content, so we parse it.

############## PARSE 'POST' SUBMISSIONS ##################
read(STDIN,$in,$ENV{'CONTENT_LENGTH'});
@in = split(/&/,$in);

foreach $i (0 .. $#in) {
    $in[$i] =~ s/\+/ /g;
    $in[$i] =~ tr/A-Z/a-z/;
    $in[$i] =~ s/%(..)/pack("c",hex($1))/ge;
    ($key, $val) = split(/=/,$in[$i],2);
    $in{$key} .= '\0' if (defined($in{$key}));
    $in{$key} .= $val;
}
############## PARSE 'POST' SUBMISSIONS ##################

print "<h1>Search Results</h1>\n";

if ($in{'rexp'} eq "") { print "Fill in the string, you dolt!\n"; exit 0; }

print "Looking for $in{'rexp'}...<p><pre>";
print("<code>grep \"$in{'rexp'}\" /var/www.logs/Rollover/access_log</code>\n");
print "<hr><br>\n";
$x=`/usr/bin/grep \"$in{'rexp'}\" /var/www.logs/Rollover/access_log`;

print "$x</pre><hr>End of grep.\n";

exit 0;
