#!/usr/bin/perl

# log.cgi -- count and log web page hits

# the first line of this script may have to be changed
# if your system's Perl interpreter is not located in /usr/bin/perl

# Written by P. Lutus Ashland, Oregon lutusp@arachnoid.com 2/21/96
# Modified 3/2/1998 Keith Bramich for Cadenza
# Modified 14/2/1998 Keith Bramich to make the counter image invisible (1 pixel).

# This is a cute, small, serviceable hits counter program. It uses the x-bitmap format
# which is monochrome. It won't look very good if the textcolor and background are the same color
# and the browser is made by Netscape. Every other place and browser, OK.
#
# Call the counter like this: 
# 	<IMG src="http://www.cadenza.org/utils/ilog.cgi?name" width=1 height=1>

# Ownername can have particular web page designators attached to it like this: 
#       Ownername_mypagename
# This allows you to count pages independently.
# The directory in which ilog.cgi is stored must have world read, write and execute access.
# The file ilog.cgi must have world execute access.

$directory = "/home/cadenza/pubstats";
$myaddress = "158.152.58.21";

# $min_width is the minimum number of digits to print --
# the program will always print more as the number grows larger

$min_width = 3;

$countname = "counter_dat"; # a default name for the counter file

@envname = (
'REMOTE_ADDR',
'HTTP_REFERER',
'HTTP_USER_AGENT',
'HTTP_ACCEPT',
);

if ($ENV{COMSPEC} ne '') { # most definitely MSDOS, so use those old funky names
  @envname = keys(%ENV);
}

if ($ENV{'QUERY_STRING'} ne '') {
  $countname = $ENV{'QUERY_STRING'}; # get the file name from counter.cgi?ownername construct
}

chdir($directory);
unless (-e $countname) { # unless this file already exists
  open (COUNT,">$countname"); # directory has to have world write permission for this to work
  print COUNT "0\n";
  close COUNT;
}

$count = 0; # now get and increment the counter
open (COUNT,$countname) || die "Content-type:text/plain\n\nCan't open $countname!\n";
$count = <COUNT>;
close COUNT;
$count++;
open (COUNT,">$countname"); # I don't lock access so this might break or not count right in busy sites
print COUNT "$count\n"; # but locking has problems of its own ...
close COUNT;

  # Log to a file specific to the owner, ignoring anything after an underscore
  # so that the owner can have separate counters for separate pages.
  $logname = $countname;
  $logname =~ s/\_.*//;
  $logname = $logname . ".log";
  
  unless (-e $logname) 
  { # unless this file already exists
    open (LOG,">$logname"); # directory has to have world write permission for this to work
    print LOG "Count\tDateTime";
    print LOG "\tUsername";
    foreach $envlbl (@envname) 
    {
      print LOG "\t$envlbl";
    }
    print LOG "\n";
    close LOG;
  } # done initializing stats file
  
  open (LOG,">>$logname"); # now enter this stat record
  $tim = &readtime;
  print LOG "$count\t\"$tim\"\t$countname";
  foreach $envlbl (@envname) {
    print LOG "\t\"$ENV{$envlbl}\"";
  }
  print LOG "\n";
  close LOG;

  print "Content-type:image/x-xbitmap\n\n";
  print "#define counter_width 1\n";
  print "#define counter_height 1\n";
  print "static unsigned char counter_bits[] = {0x00};\n";

exit;

sub readtime {
  local ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time);
  sprintf ("%2.0f/%02.0f/%04.0f %2.0f:%02.0f:%02.0f",$mon+1,$mday,($year > 50)?$year+1900:year+2000,$hour,$min,$sec); # to the stack
}

