#!/usr/bin/perl

#-------------------------Welcome-----------------------------
#
# Click Count
# Ver. 1.2.2
# By Luke and Mark Pfeifer
# http://www.staff.net/cgi-scripts/		
# Release 3-22-97
# Updated 12-30-97
#
#--------------- Variable Defention --------------------------
#
# $filename - the name of the log file. 
#
# $main_dir - the location of the log file.  You must
#	have the full path.  If you are unsure what it is
#	contact your server admin.
#
# $autoadd - decides wether or not to automatically
#	add links that are not in the database.
#	1 = on      2 = off
#
# $view_log - The password you wish to use to view the
#	log file online so clickcount.pl?view=whatever you 
#	chose for this string.  In this script it is set-up
#	to use test so you would use clickcount.pl?view=test
#
#-------------------------------------------------------------
#
#------- Start User Configuration ----------------------------
$filename = 'click-count.log';
$main_dir = '/home/hot-world.de/www/cgi-bin/klickcount';
$autoadd=1;  
$view_log='susanne';
#---End User Configuration -----------------------------------

#**************** DO NOT EDIT PAST THIS LINE *****************#

&FormInput(*input);

################################
# Some Default Set Variables
################################
$addnew=0;

$lock = "$main_dir/clickcount_lock.lock";

####################
# Set Lock File
####################
if ($input{'view'} ne $view_log) 
  {
	&SetLock;
  }

####################
# Read in Data File
####################
open(DATA,"$main_dir/$filename");
   @lines = <DATA>;
close(DATA);


#####################
# View Log
#####################
if ($input{'view'} eq $view_log) 
  {
	$spacing = "&nbsp;&nbsp;&nbsp;&nbsp;";

	print "HTTP/1.0 200 OK\n" if $ENV{PERLXS} eq "PerlIS";
	print "Content-type: text/html\n\n";

	print "<html>\n";
	print "<title>Hot-World.de - Klick-Counter</title>\n";
	print "<body bgcolor=FFFFFF>\n";
	print "<center>\n";
	print "<h1>Klick-Count Log Viewer</h1>\n";
	print "<table border=1>\n";
	print "<tr><td colspan=1 bgcolor=\"CCFFFF\">$spacing<strong><u>Seite</strong></u>$spacing</td>\n";
	print "<td colspan=1 bgcolor=\"FF9999\">$spacing<strong><u>Klicks</strong></u> $spacing</td></tr>\n";

	foreach $line (@lines)
  	  {
		($link_url1, $link_count1) = split(/\|/,$line);
  		print "<tr><td bgcolor=\"CCFFFF\" align=left>$link_url1 $spacing</td>\n";
		print "<td bgcolor=\"FF9999\" align=right>$spacing $link_count1</td></tr>\n";
  	  }

	print "</table>\n";
	print "</center>\n";
	print "</body>\n";
	print "</html>\n";
  }  

	
#####################
# Count Incrementing
#####################

else
  {

	open(DATA,">$main_dir/$filename");
	  foreach $line (@lines)
	  {
	  	($link_url1, $link_count1) = split(/\|/,$line);
	  	if ($input{'url'} eq $link_url1) 
		  {
			$link_count1++;
		 	print DATA ("$link_url1|$link_count1\n");
		 	$addnew=1;
	  	  }
	      else 
	  	  {
			print DATA $line;
	  	  }

	  }

	#####################
	# Auto Add entry
	#####################

	if ($addnew == 0 && $autoadd == 1)
	  {
		print DATA ("$input{'url'}|1\n");
	  }
	 &EndLock;

	#####################
	# Close Log File
	#####################
	close(DATA);



	#####################
	# Go to URL
	#####################

	if ($input{'url'} !~ m?://?)
	{
		$input{'url'} = "http://" . $input{'url'};
	}
	print "HTTP/1.0 302 Temporary Redirection\r\n" if $ENV{PERLXS} eq "PerlIS";
	print "Content-type: text/html\n";

	print "Location: $input{'url'}\n\n";

		
  } # Closes Else for View Log

	exit;


#-------------------------------------------------------------
# function: FormInput
#-------------------------------------------------------------
sub FormInput
{
local (*qs) = @_ if @_;

if ($ENV{'REQUEST_METHOD'} eq "GET")
        {
        $qs = $ENV{'QUERY_STRING'};
        }
elsif ($ENV{'REQUEST_METHOD'} eq "POST")
        {
        read(STDIN,$qs,$ENV{'CONTENT_LENGTH'});
        }

@qs = split(/&/,$qs);

foreach $i (0 .. $#qs)
        {
        $qs[$i] =~ s/\+/ /g;
        $qs[$i] =~ s/%(..)/pack("c",hex($1))/ge;

        ($name,$value) = split(/=/,$qs[$i],2);

        if($qs{$name} ne "")
                {
                $qs{$name} = "$qs{$name}:$value";
                }
        else
                {
                $qs{$name} = $value;
                }
        }

return 1;
}

#-------------------------------------------------------------
# SetLock: Subroutine
#-------------------------------------------------------------
sub SetLock {

        $timecheck = 0;
	while(-e $lock)
	    {
            sleep(5);
	    $timecheck = $timecheck + 1;
	    if ($timecheck >= 5)
		{
		unlink("$lock");
		}
	    }
        open(LOCKFILE,">$lock");
        close(LOCKFILE);
        return;
}

#-------------------------------------------------------------
# EndLock: Subroutine
#-------------------------------------------------------------
sub EndLock {

	unlink("$lock");
        return;
}
