#!/usr/bin/perl

use strict;
use CGI;

# Your password to access the
# program over the web goes
# inside these brackets -----+
#                            |
my($accesskey) = q[foo];# <--+


$|             = 1;
my($cgi)       = CGI->new();
my($limit)     = $cgi->param('limit') || 300;
my(@log)       = ();
my($logfile)   = '/home/www/atrixnet/cgi-bin/logs/access.log';
my($pswd)      = $cgi->param('pw') || '';
my($pattern)   = $cgi->param('pattern');
my($filter)    = $cgi->param('filter');



if ($pswd ne $accesskey) {

   &authenticate();
}
elsif ($pswd eq $accesskey) {

   &admit();
}

exit;



sub authenticate {

   print( $cgi->header(), qq[
   <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
   <html>
   <head>
   <title>Access Log</title>
   <link rel="stylesheet" type="text/css" href="/css/style.css">
   </head>
   <body>
   <br>
   <h2>Access Log for $ENV{'SERVER_NAME'}</h2><br>
   <blockquote><form action="access_log.cgi" method="post">
   <input type="text" name="pw" size="30"> <strong>Password</strong><br><br>
   <input type="text" name="pattern" size="30"> <strong>match pattern</strong><br><br>
   <input type="text" name="filter" size="30"> <strong>filter pattern</strong><br><br>
   <select name="limit">
      <option value="100">100</option>
      <option value="200">200</option>
      <option value="300">300</option>
      <option value="400">400</option>
      <option value="500">500</option>
      <option value="600">600</option>
      <option value="700">700</option>
      <option value="800">800</option>
      <option value="900">900</option>
      <option value="1000">1000</option>
      <option value="0">ALL</option>
    </select> Maximum Lines displayed<br><br>
   <input type="submit" value="view log">
   </form></blockquote>
   <p>&nbsp;</p>
   </body></html>] );
}

sub admit {
   print( $cgi->header(), qq[
   <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
   <html>
   <head>
   <title>Access Log</title>
   <link rel="stylesheet" type="text/css" href="/css/style.css">
   </head>
   <body><a name="top"></a><br>
   <h2>Access Log for $ENV{'SERVER_NAME'}</h2><br><br>
   <div align="center"><a href="#eof">skip to bottom</a></div>
   <br><br><pre>] );

   open(LOG,"<$logfile") or print "can't open $logfile for reading: $!";
   @log = <LOG>;
   close LOG;

   my $i = ($#log + 1);
   my $saved = $i;

   LOGSCAN: foreach my $line (reverse @log) {
      my $re        = (length($pattern) > 0) ? qr/$pattern/o : 0;
      my($negre)    = (length($filter) > 0) ? qr/$filter/o : 0;
      my $criterion = ($limit) ? ($saved - $limit) : 0;
      my($match)    = 0;
      if (
          (length($line) > 2) and
          ($i > $criterion )
          ) {

         if ($re or $negre) {

            if ($re) {

               if ($line !~ /$re/) {

                  next;
               }
               else {

                  $match++;
               }
            }

            if ($negre) {

               if ($line =~ /$negre/) {

                  next;
               }
               else {

                  $match++;
               }
            }

            if ($match) {

               print qq|<strong>$i\.) </strong>$line<br><br>| and --$i;
            }
            else {

               next;
            }
         }
         else {

            print qq|<strong>$i\.) </strong>$line<br><br>| and --$i;
         }
      }
   }

   print( qq[
   </pre><div align="center"><a href="#top">top</a></div>
   <a name="eof"></a><p>&nbsp;</p></body></html>] );
}

__END__