#!/usr/bin/perl
#use CGI::Carp qw(fatalsToBrowser);

#############################################################
# Set program parameters here                               #
#############################################################

# Time (in minutes) to show the user as online after the last request
$keep_online_time = 10;

# Time (in minutes) to save the user's IP after the last request. The counter is only increased for unknown IPs.
$keep_in_log_time = 20;

# Set to 0 if your server's operating system doesn't implement flock() (for example Windows 95/98/ME)
$use_flock = 1;

#############################################################
# Program starts here                                       #
# GTCounter 1.1 by Wladimir Palant (http://www.gtchat.de/)  #
#############################################################

$LOCK_EX = 2;
$LOCK_UN = 8;
$current_time = time;

open(LOG, "/var/www/vhosts/windsurfinghargen.com/httpdocs/cgi/log.txt");
flock(LOG,$LOCK_EX) if ($use_flock);
seek(LOG,0,0);

@entries = <LOG>;

seek(LOG,0,0);
truncate(LOG,0);
flock(LOG,$LOCK_UN) if ($use_flock);
close(LOG);


open (WRITELOG,">/var/www/vhosts/windsurfinghargen.com/httpdocs/cgi/log.txt");
$add=1;
$online=1;

print WRITELOG "$ENV{REMOTE_ADDR}|$current_time\n";
foreach $curentry (@entries)
{
	$curentry =~ s/[\n\r]//g;
	($ip, $time) = split(/\|/, $curentry);
	if ($current_time - $time <= $keep_in_log_time*60 && $ip ne $ENV{REMOTE_ADDR})
	{
		print WRITELOG "$curentry\n";
	}
	if ($current_time - $time <= $keep_online_time*60 && $ip ne $ENV{REMOTE_ADDR})
	{
		$online++;
	}
	if ($current_time - $time <= $keep_in_log_time*60 && $ip eq $ENV{REMOTE_ADDR})
	{
		$add=0;
	}
}
close(WRITELOG);

open(COUNT, "/var/www/vhosts/windsurfinghargen.com/httpdocs/cgi/count.txt");
flock(COUNT,$LOCK_EX) if ($use_flock);
seek(COUNT,0,0);
$count = <COUNT>;
flock(COUNT,$LOCK_UN) if ($use_flock);
close(COUNT);

if ($add != 0)
{
	$count++;
	open (WRITE,">/var/www/vhosts/windsurfinghargen.com/httpdocs/cgi/count.txt");
	print WRITE "$count";
	close(WRITE);
}

print "Content-type: text/javascript\n\n";
print "document.write('Bezoekersteller : $count<br>Nu online : $online<br>')";

