#!/usr/local/bin/perl $curpath = substr($0, 0, rindex($0, "/")+1); require "${curpath}config.cgi"; #=====================================================# # This file's name: makelog.pl # # For each page that is accessed, this perl script # # writes information about the access to a log file # # # # LogScribe - by Usama Wazeer (usamaw@cs.utexas.edu) # # http://www.cs.utexas.edu/users/usamaw # # - and Daniel Cedras (cedras@texas.net) # # http://www.texas.net/~cedras/html # #=====================================================# #======================================# # Initialize and get command line info # #======================================# $usebackup = 0; $date = time(); $numofsecs = 60 * 60 * 24 * $numofdays; $ENV{'QUERY_STRING'} =~ s/%2C/,/g; @from_info = split(/,/, $ENV{'QUERY_STRING'}); ($pagename,$pageurl) = @from_info; #$hostname = $ENV{'REMOTE_HOST'}; #$hostname = substr($ENV{'HTTP_VHOSTING_AGENT'},0,index($ENV{'HTTP_VHOSTING_AGENT'},' ')); #$hostname = $ENV{'$HTTP_VHOSTING_AGENT'}; $hostname = $ENV{'HTTP_X_FORWARDED_FOR'}; #=========================================================# # We must return to the browser the image we had promised # # Let's send out a single transparent pixel-gif # #=========================================================# # Speed up output: $! = 1; $| = 1; #if ($ARGV[0] =~ /Location/i) # {print "$ARGV[0]";} # else {print "Content-type: image/gif\n\nGIF89a\1\0\1\0\200\0\0\0\0\0\0\0\0!\371\4\1\0\0\0\0,\0\0\0\0\1\0\1\0\0\2\2D\1\0\n";} print "Content-type: image/gif\n\nGIF89a\1\0\1\0\200\0\0\0\0\0\0\0\0!\371\4\1\0\0\0\0,\0\0\0\0\1\0\1\0\0\2\2D\1\0\n"; #=========================================================# # Get the guest's host name - if we get an IP, convert it # #=========================================================# &address_to_name ($hostname); sub address_to_name { local ($address) = shift(@_); local (@octets); local ($name, $aliases, $type, $len, $addr); local ($ip_number); @octets = split ('\.', $address); $ip_number = pack ("CCCC", @octets[0..3]); ($name, $aliases, $type, $len, $addr) = gethostbyaddr ($ip_number, 2); if ($name) {$hostname = $name;} else {$hostname = $ENV{'REMOTE_ADDR'};} } #==================================================# # Strip the operating system from the browser type # #==================================================# @brow = split ('\(', $ENV{'HTTP_USER_AGENT'}); $browser = $brow[0]; #==================================================# # Is the refering page allowed to add to the log ? # #==================================================# &check_url; sub check_url { if ($ENV{'HTTP_REFERER'}) { foreach $referer (@referers) { if ($ENV{'HTTP_REFERER'} =~ /$referer/i) { $check_referer = 1; last; } } } else {$check_referer = 1;} if ($check_referer != 1) {exit;} } #========================================# # Should the connecting user be logged ? # #========================================# &check_ignoresites; sub check_ignoresites { if ($hostname) { foreach $ignoresite (@ignoresites) {if ($hostname =~ /$ignoresite/i) {exit;}} } } #==========================================================# # Let's make sure the logfile isn't in use by checking for # # the presence of a lock. If the lock is there for over an # # hour then remove the lock and then go on. if the lock # # hasn't been there an hour, check for the lock every # # second for a minute - after a minute give up. The # # presence of a lock for over an hour indicates that a # # previous instance of makefile screwed up and did not # # remove it. # #==========================================================# if (-f $lockfile) { if (-M $lockfile > (1/24)) { unlink($lockfile); $usebackup = 1; } else { for ($i = 60; $i > 0; --$i) { sleep(1); last unless -f $lockfile; } if (-f $lockfile) {exit;} } } #=========================================================# # If the lockfile isn't there, create one as we are about # # to process the logfile - we dont want another makelog # # to screw it up # #=========================================================# open(LOCKFILE,">$lockfile"); close(LOCKFILE); #==================================# # Logfile exist ? If not create it # #==================================# if (! -e $logfile) { open (LOG,">$logfile") || die "Can't create $logfile: $!\n"; print LOG "LOG-BEGIN\n"; close(LOG); } #===================================================# # If the lock is still there after an hour, the # # logfile must have gotten corrupted, let's read in # # the backup instead if such is the case # #===================================================# if (!$usebackup) {open (FILE,"$logfile") || die "Can't open $logfile: $!\n";} else {open (FILE,"$backup") || die "Can't open $backup: $!\n";} @LINE=; close(FILE); $SIZE=@LINE; #===============# # Create Backup # #===============# rename($logfile,$backup) || die "Can't create $backup: $!\n"; #===========================# # Re-create file for output # #===========================# if ($oldlogfile) {open (OLDLOG, ">>$oldlogfile") || die "Can't create $oldlogfile: $!\n";} open (LOG,">$logfile") || die "Can't create $logfile: $!\n"; # Write entry at very top and move subsequent lines down # and delete all lines starting from and below time limit $delete = 0; for ($i=0;$i<=$SIZE;$i++) { $_=$LINE[$i]; if ($delete==0) { if (/LOG-BEGIN/) { print LOG "LOG-BEGIN\n"; print LOG "$date $hostname "; if ($pagename) {print LOG "$pagename ";} else {print LOG "HTML_ERROR:Missing_Pagename ";} if ($pageurl) {print LOG "$pageurl ";} else {print LOG "HTML_ERROR:Missing_URL ";} if ($browser) {print LOG "$browser\n";} else {print LOG "Unknown\n";} print "$pagename $pageurl $browser"; } else { ($timeinlog,$junk,$junk,$junk)=$_; $timediff = $date - $timeinlog; if ($timediff > $numofsecs) { $delete = 1; if ($oldlogfile) {print OLDLOG $_;} } else {print LOG $_;} } } elsif ($oldlogfile) {print OLDLOG $_;} } close (LOG); if ($oldlogfile) {close (OLDLOG);} #==========================================# # We are through with writing the log, now # # let's remove the lock so that the next # # makelog can use the logfile # #==========================================# unlink($lockfile); exit;