#!/usr/bin/perl
###########################################################################################
#		Network Counter 1.0
#
#     	Digital Schema LLC
#		Author: John Michael Soileau 
# 		Email: johnm@realtimescripts.com
#		Website:  http://realtimescripts.com
#
# This script is Free.  Please do not remove the headers or copyright notices.  Enjoy.
###########################################################################################
# COPYRIGHT NOTICE:
# © Copyright 2000 Digital Schema, LLC. All rights reserved
# Created 10/05/99			Last Modified 01/25/01
#
# Selling the code for this program without prior written consent is expressly forbidden.                                         
# Obtain written permission before redistributing this software over the Internet or 
# in any other medium.  In all cases copyright and header must remain intact.
#
# By using this program you agree to indemnify Digital Schema LLC from any liability.
###########################################################################################
#  Variables to edit
#############################################  
#  The image you want to return?
$image = "http://asia.vtec.net/clear.gif";
###########################################################################################


#################################################
#  MAIN PROGRAM STARTS BELOW.  NO MORE EDITING. #
#################################################
use CGI qw(:standard);

$reset = $cookie_ready = 0;
if ($ENV{'QUERY_STRING'}){
	@pairs = split(/&/, $ENV{'QUERY_STRING'});
	($dump, $site_id) = split(/=/, $pairs[0]);
	$site_id =~ tr/+/ /;
	$site_id =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
	$site_id =~ s/<!--(.|\n)*-->//g;
}
else {&send_image};

$exist_id =  cookie('site_id');
$exist_visits = cookie('visits');

if ($exist_id && $exist_visits) {
	if ($exist_id != $site_id){
		$exist_id = $site_id;
		if ($exist_visits =~ $site_id) {
			&set_cookie($site_id, $exist_visits);	#send back updated cookie
			&send_image;
		}
		$exist_visits = join(" ",$exist_visits,$site_id);
		&set_cookie($site_id, $exist_visits);	#send back updated cookie
		$reset = 1;
		&update_logs;
		&send_image;
	}
&send_image;
}

else {
	&set_cookie($site_id, $site_id);
	&update_logs;
	&send_image;
}
###################################################
sub update_logs {
$uniques = 0;
$multiple = 0;
$found = 0;
&date;
if ( -e "$site_id.log" ) { 
	open (RL, "+<$site_id.log");
		flock(RL, 2);
		@old_info=<RL>;
		foreach $line (@old_info) {
			chomp $line;
			($day,$old_uniques,$old_multiple)= split(/\|/,$line);
			if ($day eq $today){
				$uniques = $old_uniques;
				$multiple = $old_multiple;
				&update;				
				$found = 1;
				$new_line = join("\|",$today,$uniques,$multiple);
				push (@updated_info , $new_line);
			}
			else {
			push (@updated_info , $line);
			}
		}
		unless ($found) {
			&update;
			$new_info = join("\|" ,$today,$uniques,$multiple);
			push (@updated_info , $new_info);
		}
		seek (RL, 0, 0);
		foreach $updated_info (@updated_info) {
			print RL "$updated_info\n";
		}
		flock(RL, 8);
		close (RL);
}
else {
&update;
$new_info = join("\|",$today,$uniques,$multiple);	
open (WL, ">$site_id.log") || print"Can not create $log_file -- $!<BR>";
	print WL "$new_info\n";
	close WL;
$chmod = "chmod 666 $site_id.log";
system $chmod;
}}
########################################
sub set_cookie {
my ($site_id, $visits) = @_;
$id_cookie = cookie(
	-name=>'site_id',
	-value=>$site_id,
	-path=>'/'
);
$visits_cookie = cookie(
	-name=>'visits',
	-value=>$visits,
	-path=>'/'
);
$cookie_ready = 1;
}
###################################################################
sub date {
$date=localtime(time);
($da, $mo, $no, $hr, $yr) = split (/\s+/,$date);
if ($mo =~ /jan/i) {$mo=1}     
	elsif ($mo =~ /feb/i) {$mo=2}  
	elsif ($mo =~ /mar/i) {$mo=3}  
	elsif ($mo =~ /apr/i) {$mo=4}  
	elsif ($mo =~ /may/i) {$mo=5}  
	elsif ($mo =~ /jun/i) {$mo=6}  
	elsif ($mo =~ /jul/i) {$mo=7}  
	elsif ($mo =~ /aug/i) {$mo=8}  
	elsif ($mo =~ /sep/i) {$mo=9}  
	elsif ($mo =~ /oct/i) {$mo=10} 
	elsif ($mo =~ /nov/i) {$mo=11} 
	elsif ($mo =~ /dec/i) {$mo=12} 
$today = join("/", $mo,$no,$yr);
}
#######################################################
sub update {
if ($reset){
	$multiple++;
}
else {$uniques++};
}
########################################################
sub send_image {
print "Location: $image\n";
if ($cookie_ready){
	print header(-cookie=>[$id_cookie,$visits_cookie]);
}else{print "\n\n";}
exit;
}
