package SPYCOUNT; # By Meryll Larkin written January 2004 # Last update February 2006 # A custom module to be used with spy.cgi and stats.cgi # To set update and set a Vistor Count on website use strict; my $writeFile = "count"; my $writePath = "/var/www/append/"; my $image_url = "http://www.alwanza.com/images/counter/"; sub new { my $class = shift; my $self = {}; bless $self, $class; return $self; } sub set_count { my ($self, $visitCount) = @_; chdir($writePath) || $self->error ("Can't change directory to $writePath"); open (COUNT, ">$writeFile"); flock(COUNT,8); print COUNT "$visitCount"; close COUNT; return; } sub get_count { my $self = shift; chdir($writePath) || $self->error ("Can't change directory to $writePath"); open (COUNT, "<$writeFile"); flock(COUNT,8); my $visitCount = ; close COUNT; $visitCount =~ s/\D//g; # strip off anything that isn't a digit return $visitCount; } sub get_display { my ($self, $visitCount) = @_; my $displayCount; my %images = ( "0" => $image_url . "0DEFAULT.gif", "1" => $image_url . "1DEFAULT.gif", "2" => $image_url . "2DEFAULT.gif", "3" => $image_url . "3DEFAULT.gif", "4" => $image_url . "4DEFAULT.gif", "5" => $image_url . "5DEFAULT.gif", "6" => $image_url . "6DEFAULT.gif", "7" => $image_url . "7DEFAULT.gif", "8" => $image_url . "8DEFAULT.gif", "9" => $image_url . "9DEFAULT.gif", ); my $five_digit_visit = &_normalizeDigits($self, $visitCount); $visitCount = $five_digit_visit; my $digits = length($visitCount); my @visitCount; my $i; for ($i = 0; $i<$digits; $i++) { $_ = substr($visitCount, $i, 1); $displayCount .= qq||; } return $displayCount; } sub _normalizeDigits { my ($self, $numberstring) = @_; my $normal; $normal = "0000" . $numberstring if ($numberstring =~ /^\d$/); $normal = "000" . $numberstring if ($numberstring =~ /^\d\d$/); $normal = "00" . $numberstring if ($numberstring =~ /^\d\d\d$/); $normal = "0" . $numberstring if ($numberstring =~ /^\d\d\d\d$/); $normal = $numberstring if ($numberstring =~ /^\d{5}$/); $numberstring = $normal; return $numberstring; } sub _error { my $self = shift; my $errorstring = shift; $self->{_errorstring} = $errorstring; return $self->{_errorstring}; } 1