#!/usr/bin/perl -w # ############################################### # count.pl - A Perl vistor counter script # # # # This script is copyright IE Internet.com Ltd# # all rights reserved. # # # # You MAY NOT use, modify, copy, distibute or # # re-distribute this script without the prior # # permission of IE Internet. # # # ############################################### use GD; use CGI; $minLen = 4; $counterdir = "../../count/"; $lockWait = 5; &ReadParse; &initialize; &loadbitmaps; &incrementCount; &generateBitmap; &writeBitmap; exit(0); sub writeBitmap { #Fix because IE on XP with SP2 installed is now incapable of displaying x-xbitmap format. if ($ENV{'HTTP_USER_AGENT'} =~ m#msie (\d+)|msie/(\d+)|internet explorer/(\d+)#oi){ $count_width=$len*8; $count_height=16; $tmpLen = length($totalReads); if ($tmpLen < $minLen){ for($i = ($tmpLen); $i < ($minLen); $i++) { $totalReads= "0" . "$totalReads"; } } $cgi = new CGI; $im = new GD::Image($count_width,$count_height); $white = $im->colorAllocate(255,255,255); $black = $im->colorAllocate(0,0,0); $im->transparent($white); $im->interlaced('true'); $im->fill(50,50,$white); $im->rectangle(0,0,$count_width-1,$count_height-1,$white); $im->string(gdMediumBoldFont,$len,0,$totalReads,$black); binmode STDOUT; print $cgi->header( "image/png" ); # Convert the image to PNG and print it on standard output print $im->png; } elsif ($ENV{'HTTP_USER_AGENT'} =~ m#msie|microsoft#oi){ $count_width=$len*8; $count_height=16; $tmpLen = length($totalReads); if ($tmpLen < $minLen){ for($i = ($tmpLen); $i < ($minLen); $i++) { $totalReads= "0" . "$totalReads"; } } $cgi = new CGI; $im = new GD::Image($count_width,$count_height); $white = $im->colorAllocate(255,255,255); $black = $im->colorAllocate(0,0,0); $im->transparent($white); $im->interlaced('true'); $im->fill(50,50,$white); $im->rectangle(0,0,$count_width-1,$count_height-1,$white); $im->string(gdMediumBoldFont,$len,0,$totalReads,$black); binmode STDOUT; print $cgi->header( "image/png" ); # Convert the image to PNG and print it on standard output print $im->png; } else{ print "Content-type: image/x-xbitmap\n\n"; print "#define count_width ",$len*8,"\n#define count_height 16\n"; print "static char count_bits[] = {\n"; for($i = 0; $i < ($#bytes + 1); $i++) { print "0x$bytes[$i]"; if ($i != $#bytes) { print ","; if (($i+1) % 7 == 0) { print "\n" ; } } } print "};\n"; } } sub generateBitmap { $count = $totalReads; @bytes = (); $len = length($count) > $minLen ? length($count) : $minLen; $outtext = sprintf("%0${len}d",$count); for ($i = 0; $i < $len*3; $i++ ) { push(@bytes,"00"); } for ($y=0; $y < 10; $y++) { for ($x=0; $x < $len; $x++) { push(@bytes,substr($chars{substr($outtext,$x,1)},$y*3,2)); } } for ($i = 0; $i < $len*3; $i++ ) { push(@bytes,"00"); } } sub initialize { $countername = "count.txt"; $counterFile = $counterdir . $countername; if (!( -f $counterFile )) { $start = $in{'COUNT'} -1; open(CNTRFILE,">$counterFile"); print CNTRFILE "$start\n"; close(CNTRFILE); } } sub incrementCount { if (&lockFile == 1) { $count = "0"; return; } &incrementTotalReads; &unlockFile; } sub unlockFile { unlink("$counterFile.lock"); } sub lockFile { $lockCount = 0; while (-f "$counterFile.lock") { if ($lockCount > $lockWait) { $count = 0; return 1; } sleep 1; $lockCount++; } open(LOCK,">$counterFile.lock") || die("Can't open $counterFile.lock: $!\n"); return 0; } sub incrementTotalReads { if (-e $counterFile) { open(COUNT,"$counterFile") || die("Can't open $counterFile: $!\n"); } $totalReads = ; chop $totalReads; close(COUNT); $totalReads++; open(COUNT,">$counterFile") || die "$0: can\'t open $counterFile: $!\n"; print (COUNT "$totalReads\n"); close(COUNT); } sub loadbitmaps { ###Images in white with black background # $chars{'0'} = "c3 99 99 99 99 99 99 99 99 c3"; # $chars{'1'} = "cf c7 cf cf cf cf cf cf cf c7"; # $chars{'2'} = "c3 99 9f 9f cf e7 f3 f9 f9 81"; # $chars{'3'} = "c3 99 9f 9f c7 9f 9f 9f 99 c3"; # $chars{'4'} = "cf cf c7 c7 cb cb cd 81 cf 87"; # $chars{'5'} = "81 f9 f9 f9 c1 9f 9f 9f 99 c3"; # $chars{'6'} = "c7 f3 f9 f9 c1 99 99 99 99 c3"; # $chars{'7'} = "81 99 9f 9f cf cf e7 e7 f3 f3"; # $chars{'8'} = "c3 99 99 99 c3 99 99 99 99 c3"; # $chars{'9'} = "c3 99 99 99 99 83 9f 9f cf e3"; ###Images in black with white background $chars{'0'} = "3c 66 66 66 66 66 66 66 66 3c"; $chars{'1'} = "30 38 30 30 30 30 30 30 30 30"; $chars{'2'} = "3c 66 60 60 30 18 0c 06 06 7e"; $chars{'3'} = "3c 66 60 60 38 60 60 60 66 3c"; $chars{'4'} = "30 30 38 38 34 34 32 7e 30 78"; $chars{'5'} = "7e 06 06 06 3e 60 60 60 66 3c"; $chars{'6'} = "38 0c 06 06 3e 66 66 66 66 3c"; $chars{'7'} = "7e 66 60 60 30 30 18 18 0c 0c"; $chars{'8'} = "3c 66 66 66 3c 66 66 66 66 3c"; $chars{'9'} = "3c 66 66 66 66 7c 60 60 30 1c"; } sub ReadParse { if (@_) { local (*in) = @_; } local ($i, $loc, $key, $val); # Read in text if ($ENV{'REQUEST_METHOD'} eq "GET") { $in = $ENV{'QUERY_STRING'}; } elsif ($ENV{'REQUEST_METHOD'} eq "POST") { for ($i = 0; $i < $ENV{'CONTENT_LENGTH'}; $i++) { $in .= getc; } } @in = split(/&/,$in); foreach $i (0 .. $#in) { # Convert plus's to spaces $in[$i] =~ s/\+/ /g; # Convert %XX from hex numbers to alphanumeric $in[$i] =~ s/%(..)/pack("c",hex($1))/ge; # Split into key and value. $loc = index($in[$i],"="); $key = substr($in[$i],0,$loc); $val = substr($in[$i],$loc+1); $in{$key} .= '\0' if (defined($in{$key})); # \0 is the multiple separator $in{$key} .= $val; } return 1; }