#!/usr/bin/perl
use lib 'lib',
'lib/arch';
use GD;
######################################################################
####################### Constant defines #############################
########## This all that will need to be modified by the user ########
######################################################################
$width=24; # the width of an individual image
$height=24; # the height of an individual image
# reload_time is the number of minutes to wait before allowing
# the same visitor to increment the counter again
$reload_time=10;
# the following variable is the array of ip addresses to be excluded
@excludeIP = ();
# in the future I will exclude myself, but for testing, I will
# let myself be included
######################################################################
######################### initialization #############################
######################################################################
# load all the images into the array images.
# images[0] will hold the image which represents 0.
# This loop simply loads the images named "0.gif" .. "9.gif"
# These images should be in the same directory as this program
for ($i=0; $i<=9; $i++)
{&load_image($i . ".gif",$i);
}
# the first argument is the name of the image,
# the second argument is the number of the image
sub load_image
{@args=@_;
if (! open (GIF,$args[0]) )
{print "Content-type: text/html\n\n";
print "Sorry, the image, $args[0], could not be opened.\n
";
print "The error reported was: $!";
exit 0;
}
$images[$args[1]] = newFromGif GD::Image(GIF);
close GIF;
}
# read in the ip address
$userIP = $ENV{'REMOTE_ADDR'};
# check that against the ip array. Set the incr variable
# to true if it's in there and false if it's not
# the counter will not be incremented if this is true
$incr=1; #1 if true
for ($i=0; $i<=$#excludeIP; $i++)
{
if ($excludeIP[$i] eq $userIP)
{$incr=0; #0 is false
last; #this is a break statement
}
}
# get the counter value now
if (! open (COUNT,"counter.txt") )
{print "Content-type: text/html\n\n";
print "Sorry, counter.txt could not be opened for reading.\n$!";
exit 0;
}
@temp=;
chomp(@temp);
$counter_value=$temp[0];
$reloads_included=$temp[1];
close COUNT;
# now check if this was a reload
# first open the file of ips and dates
if (! open (RECENT,"recent_visits.txt") )
{print "Content-type: text/html\n\n";
print "Sorry, recent_visits.txt could not be opened for reading.\n$!";
exit 0;
}
@lines=;
chop(@lines);
close RECENT;
# get the current time/date
$cur_time=time;
$found=0; # this is to keep track of whether or not the IP was found
# in the list of IP with the correct time. If it wasn't then
# this is a new IP which needs to be added to the list
# now loop through each item and see if it matches the current IP
# also copy over only the ones which are still recent into the
# keep array which will be written back to the file
foreach $pair (@lines)
{($theIP,$thedate)=split(/,/,$pair);
if ( ($cur_time-$thedate) <= $reload_time * 60 )
{if ($theIP eq $userIP)
{push(@keep,($theIP.",".$cur_time."\n") ); # add this IP back into list with updated time
$incr=0; # 0 is also false
$found=1; # 1 is true
}
else
{push(@keep,($theIP.",".$thedate."\n") );
}
}
}
if ($found==0) # if it wasn't in the list, add it to the list
{push(@keep,($userIP.",".$cur_time."\n") );
}
# now open recent visits to print out updated IP/Time list
if (! open (RECENT,">recent_visits.txt") )
{print "Content-type: text/html\n\n";
print "Sorry, recent_visits.txt could not be opened for writing.\n$!";
exit 0;
}
# print out array
print RECENT @keep;
close RECENT;
############# now increment counter and output to file ################
if ($incr==1) #if this isn't an excluded user, increment the counter
{$counter_value++;
}
#always increment the reloads_included counter:
$reloads_included++;
if (! open (COUNT,">counter.txt") )
{print "Content-type: text/html\n\n";
print "Sorry, counter.txt could not be opened for writing.\n$!";
exit 0;
}
print COUNT "$counter_value\n";
print COUNT "$reloads_included\n";
close COUNT;
######################################################################
######################## formatting/output ###########################
######################################################################
# get the number of images that I will need
# $len will contain a number >= 1 which will represent how many
# base ten digits are needed to represent the counter
$i=$counter_value;
$len=1;
for (;$i>=10;$len++,$i=$i/10)
{#empty for loop
}
# done with error checking, so initialize the output
# for preparing image output
print "Content-type: image/gif\n\n";
# create new image to be outputted
$overallwidth=$len*$width; #width of resulting outputted image
$myImage = new GD::Image($overallwidth,$height);
$white = $myImage->colorAllocate(255,255,255);
for ($i=$len-1;$i>=0;$i--)
{# grab leading character from the counter_value
$num=int $counter_value/(10**$i);
$counter_value=$counter_value%(10**$i);
$myImage->copy($images[$num],($len-$i-1)*$width,0,0,0,$width,$height);
}
$gif_data = $myImage->gif;
print $gif_data;