#!/usr/bin/perl

###########################################
## RushHour 1.0                          ##
## Written by Janis E. Kenderdine        ##
## http://www.fifedrum.org/fifer         ##
## February 06, 2005                      ##
###########################################

#############CUSTOMIZE HERE ###############

## Uncomment this if your home directory is
## http://www.yourdomain.com/ (the site root)
## and give the home directory a displayname
$site_root = "http\:\/\/www\.fifedrum\.org\/fifer";

@web_crawlers = ("Yahoo\!\ Slurp", "bot", "crawler", "archiver");

################### STOP ##################
##Nothing below needs to be touched

use CGI ':standard';
use CGI::Carp 'fatalsToBrowser';
print header;
$thisdir = `pwd`;
chomp $thisdir;
$datadir = "$thisdir/../data";

@params = param();

$filename = param("filename");
$date = param("date");
$report_type = param("type");
&Main;
if (keys(%visitor))
 {
 &Graph;
 &Display("visitor", "Unique Visitors");
 }
if (keys(%bots))
 {
&Display("bots", "Web Crawlers/Bots");
 }
print '<p style="font-size:80%" align="right"><a href="#top">Back to Top</a></p>
</body>
</html>';

#main hits
sub Main
{
print '
<html>
<head><title>Traffic Report for ' . $date . '</title></head>
<body>
<a name="top"></a>
<table width="100%" rules="all" cellpadding="0" cellspacing="0" style="border: #000000 solid thin">
<tr>
  <th colspan="4" style="background-color:#cccccc; padding: 5px; font-weight: bold;">' . $date . '</th>
</tr>' . "\n";

if ($report_type eq "monthly") 
{ 
 @filenames = `ls $datadir/$filename`; 
 foreach (@filenames) { $_ = "$datadir/$filename/$_";} 
}
else { @filenames = ("$datadir/$filename"); }

foreach (@filenames)
{
open (DATA, "$_") || print "$_";
while ($line = <DATA>)
 {
  my @splitline = split("\t", $line);
  $ip_address = $splitline[0];
  $ip_docs = "docs_$ip_address";
  $ip_time = "time_$ip_address";
  $ip_keywords = "kw_$ip_address";
  push (@$ip_docs, $splitline[3]);
  push (@$ip_time, $splitline[1]);    
  %$ip_address = (
        user_agent => $splitline[4],
  );
  if (($splitline[2] =~ /^$site_root/gi) && ($splitline[2] ne "")) {}
  else { $$ip_address{'referrer'} = $splitline[2]; }
  if ($line =~ /search/gi)
   {
   if ($splitline[2] =~ "q=") { $start = index("$splitline[2]", "q="); }
   else { $start = index("$splitline[2]", "p="); }
   if ($start < "0") {} else { $start = $start+2;}
   my $sub = (substr($splitline[2], $start));
   if ($sub =~ "&") { $end = index($sub, "&"); } else {$end = length($sub); }
   $$ip_address{'keywords'} = substr($splitline[2], $start, $end);
   }
  
  $toggle = "visitor";
  foreach (@web_crawlers)
  {
   if ($line =~ /$_/) { $toggle="bots" };
  }
  $$toggle{$ip_address} = 1;
  $total_visits{$ip_address} = 1;
 } 
 close DATA;
}
 print '</table>
 <div align="center" style="padding:10px"><a href="index.cgi">All Reports Available</a> | <a href="#visitor">Unique Visitors</a> | <a href="#bots">Web Crawlers/Bots</a></div>';
}

#Display("visitor" or "bots", Visitors/Bots)
sub Display
{
 $type = $_[0];
 $display_type = $_[1];
 $num = keys(%$type);
 print '<p style="font-size:80%" align="right"><a name="' . $type . '"></a><a href="#top">Back to Top</a></p>
 <div style="border: #000000 solid thin">
 <table width="100%" cellpadding="0" cellspacing="0">
  <tr>
    <th colspan="4" style="background-color:#ccccff; padding: 5px; font-weight: bold;">' . $display_type . ' (' . $num . ')</th>
  </tr>' . "\n";
 $x=0;
 foreach $_ (keys(%$type))
  {
   $ip_address = $_;
   $ip_docs = "docs_$ip_address";
   $ip_time = "time_$ip_address";
   $ip_keywords = "kw_$ip_address";
   $user_agent = $$ip_address{'user_agent'};
   $referrer = $$ip_address{'referrer'};
   $referrer_display = substr($referrer, 0, 100);
   if (substr($referrer, 100, 1) ne "") { $dotdot = "..."; } else { $dotdot = ""; }
   if ($x%2) { $grey = "; background-color:#eeeeff"; } else { $grey = ""; }
   print '
   <tr>
    <td style="padding: 2px 5px 2px 5px; border-top: solid #000000 thin' . $grey . '" valign="top" nowrap><b>IP Address:</b></td>
    <td style="padding: 2px 5px 2px 5px; border-top: solid #000000 thin' . $grey . '" valign="top" nowrap>' . $ip_address . '</td>
    <td style="padding: 2px 5px 2px 5px; border-top: solid #000000 thin' . $grey . '" valign="top" nowrap><b>User Agent:</b></td>
    <td style="padding: 2px 5px 2px 5px; border-top: solid #000000 thin' . $grey . '" valign="top">' . $user_agent . '</td>
   </tr>';
   if (($type eq "visitor") && ($$ip_address{'referrer'} ne "")) {
   print '
   <tr>
    <td style="padding: 2px 5px 2px 5px; border-top: solid #dddddd thin' . $grey . '" valign="top" colspan="4"><b>Referrer:</b> <a href="' . $referrer . '" target="blank">' . $referrer_display . '</a>' . $dotdot . '</td>
   </tr>'; }
   if ($$ip_address{'keywords'} ne "") {
   my $keywords = $$ip_address{'keywords'};
   $keywords =~ s/\+/\ /gi;
   $keywords =~ s/%22//gi;
   print '
   <tr>
    <td style="padding: 2px 5px 2px 5px' . $grey . '" valign="top" colspan="4"><b>Keywords:</b> ' . $keywords . '</td>
   </tr>'; }
   print '
   <tr>
    <td style="padding: 2px 5px 2px 5px; border-top: solid #dddddd thin' . $grey . '" valign="top" nowrap><b>Time:</b></td>
    <td style="padding: 2px 5px 2px 5px; border-top: solid #dddddd thin' . $grey . '" valign="top" nowrap>';
   foreach (@$ip_time) { print "$_<br>"; }
    print '</td>
    <td style="padding: 2px 5px 2px 5px; border-top: solid #dddddd thin' . $grey . '" valign="top" nowrap><b>Docs Visited:</b></td>
    <td style="padding: 2px 5px 2px 5px; border-top: solid #dddddd thin' . $grey . '" valign="top" width="100%">';
   foreach (@$ip_docs) { print "$_<br>"; }
	print '</td>
   </tr>
';
   $x++;
  }
 print '</table>
 </div>
';
}

sub Graph
{
 print '
 <div style="border: #000000 solid thin">
 <table width="100%" cellpadding="0" cellspacing="0">
  <tr>
    <th colspan="4" style="background-color:#ccccff; padding: 5px; font-weight: bold;">Page Popularity &#8224;</th>
  </tr>
 </table>
 <br>' . "\n";

 $rows = 0;
 $cols = 0;
 foreach $ip_address (keys(%visitor))
  {   
  $ip_docs = "docs_$ip_address";
  foreach (@$ip_docs) { $$_{$ip_address} = 1; $total_docs{$_} = 1; }
  }

 $cols = (keys(%total_docs));
 foreach (sort keys(%total_docs))
  {
  my @keys = keys(%$_);
  my $num_hits = @keys;
  $height{$_} = $num_hits;
  if ($num_hits > $rows) { $rows = $num_hits };
  }
  
 %colors = (1, "#cccccc",
 			2, "#ffcccc",
			3, "#ff9999",
			4, "#ffcc99",
			5, "#ffff99",
			6, "#99ff99",
			7, "#99ffff",
			8, "#9999ff",
			9, "#ff99ff",
			0, "#ffccff");
  print '
 <table cellpadding="0" cellspacing="0" align="center">';
 $color_division = 999999/$cols;
 $color_division =~ s/\.//gi;
 $color_division = substr($color_division, 0, 6);
 print "<tr><td>&nbsp;</td></tr>";
 $rowheight = 250/$rows;
 my $decimal = index($rowheight, ".");
 $rowheight = substr ($rowheight, 0, $decimal);
 $colwidth = 100/$cols;
 my $decimal = index($colwidth, ".");
 $colwidth = substr ($colwidth, 0, $decimal);
 $colwidth .= "%";
 $colwidth = "20";
 $num = $rows;
 for (my $x=0; $x<$rows; $x++)
 {
 $colornum = 1;
 my $row_num = $rows-$x;
 if ($row_num%2 eq "0") { $num="$row_num"; } else { $num = ""; }
 print "<tr>\n";
 print "<td width=\"10\" height=\"$rowheight\" style=\"font-size:$rowheightpx; padding-right:2px\" align=\"right\" nowrap\">$num</td>";
 print "<td width=\"10\" height=\"$rowheight\" nowrap\" style=\"border-top:#ccccff solid thin; border-left:#ccccff solid thin;\">&nbsp;</td>";
  foreach(sort keys(%total_docs))
   {
     $colornum = $colornum%10;
	 $color = $colors{$colornum};
	 if ( ($rows - $height{$_}) <= $x) { print "  <td width=\"$colwidth\" height=\"$rowheight\" align=\"right\" style=\"margin: 0px 1px 0px 1px; background-color:$color;\" nowrap>&nbsp;</td>"; }
	 else { print "<td width=\"$colwidth\" height=\"$rowheight\" style=\"border-top:#ccccff solid thin\">&nbsp;</td>"; }
	 $colornum++;
   }
 print "<td width=\"10\" height=\"$rowheight\" nowrap style=\"border-top:#ccccff solid thin; border-right:#ccccff solid thin;\">&nbsp;</td>";
 print "</tr>\n";
 }
 $keynum = 1;
 print "<tr>\n";
 print "<td width=\"10\" height=\"$rowheight\" nowrap style=\"border-top:#ccccff solid thin; font-size:$rowheightpx\" align=\"right\">0</td>";
 print "<td width=\"10\" height=\"$rowheight\" nowrap style=\"border-top:#ccccff solid thin; border-left:#ccccff solid thin\">&nbsp;</td>";
  foreach(sort keys(%total_docs))
   {
	 print "  <td width=\"$colwidth\" height=\"$rowheight\" align=\"center\" style=\"border-top:#ccccff solid thin; padding: 0px 1px 0px 1px\" nowrap><b>$keynum.</b></td>";
	 $keynum++;
   }
 print "<td width=\"10\" height=\"$rowheight\" nowrap style=\"border-top:#ccccff solid thin;\">&nbsp;</td>";
 print '</tr>
<tr>
<td colspan="2"></td>
<td height="10" valign="bottom" colspan="' . $cols . '" style="font-size:80%">&#8224; Does not include crawlers/bots</td>
<td></td>
</tr>
</table>
<br>';
 $colornum = 1;
 $keynum = 1;
  foreach (sort keys(%total_docs))
  {
  $colornum = $colornum%10;
  $color = $colors{$colornum};
  my @keys = keys(%$_);
  my $num_hits = @keys;
  $height{$_} = $num_hits;
  print "<div style=\"background-color:$color; padding:0px 5px 0px 5px; margin:2px 0px 2px 0px\">$keynum. <a href=\"$_\">$_</a> : $num_hits</div>\n";
  $colornum++;
  $keynum++;
  }
  print '</div>';
}


exit;
