#!/usr/bin/perl
#^^^^^^^^^^^^^^^----Change this to the path to perl on your box

######################################################################
# counter.pl - A Graphical Counter
# Version 2.2
#
# Written by Ben Short, Shortboy Productions
# (c) 1999-2000 Shortboy Productions
#
# Disclaimer: The author of this script, nor Shortboy Productions take
#             ANY responsibilty for any damage this script may cause.
#	      This script has been tested, and deemed "safe" at he time
#	      of production. Any damage caused would be due to the
#	      End Users lack of knowledge of setting up the script, and
#	      no-one can take responsibilty for someones stupidity :P
######################################################################

#File where counter values are kept
$countname="counts.txt";

#Log IPs? (0=no, 1=yes)
$logip=0;

#Location of the date command
$datepath="/bin/date";

#File where IP logging is to be recorded
#$ipname="/home/users/amm/public_html/cgi-bin/gcounter/ipdata.txt";

#URL Location of Digits
$url="http://itr.pl/gcounter/";

#Name of Server counter is to be working on
@serverlist=("www.itr.pl");

#Digit Properties, if using digits that came with this install,
#Dont touch ;)
$digext=".gif";
$digtype="a";
$width = "15";
$height = "20";

#######################DO NOT EDIT BELOW THIS LINE#########################
print "Content-type: text/html\n\n";

open(FILE, $countname);
$counter = <FILE>;
close(FILE);

sub counter {
	($count, $lastip)=split(":", $counter);
	if ($ENV{'REMOTE_ADDR'} ne $alastip) { 
		$count=$count+1; 
		}
	else { 
		$count=$count+0; 
		}


	@nums = split(//, $count);
	foreach $CountNr (@nums) {
		$printcount = "<img src=\"$url$CountNr$digtype$digext\" width=\"$width\" height=\"$height\" alt=\"$CountNr\">";
		print $printcount;
		}	
	}


sub write_count {
	open(FILE, ">$countname");
	print FILE "$count:$ENV{'REMOTE_ADDR'}";
	close(FILE);
	}

sub write_ip {
	$date=`$datepath`;
	chomp($date);
	open(FILE, ">>$ipname");
        print FILE "$date                $ENV{'REMOTE_ADDR'}\n";
	close(FILE);
	}

sub main {
	foreach $server (@serverlist) {
#		if (($ENV{'HTTP_HOST'} eq "$server") && ($done != 1)) {
		if ($done != 1) {
			&counter;
			&write_count;
			if ($logip eq "1") {
				&write_ip;
				}
			$done = 1;
			$valid =1
			}
#		}
	if ($valid =0) {
		print "Either this Counter has not been configured properly or <BR>";
		print "You are attempting to use someone elses counter.<br>";
		print "Please check your configuration and try again.";
		}
	}
}
&main;

