#!/usr/bin/perl
 
########################################################################
# COPYRIGHT NOTICE:
#
# Copyright 2002 FocalMedia.Net All Rights Reserved.
#
# Selling the code for this program without prior written consent 
# from FocalMedia.Net is expressly forbidden. You may not 
# redistribute this program in any shape or form.
# 
# This program is distributed "as is" and without warranty of any
# kind, either express or implied. In no event shall the liability 
# of FocalMedia.Net for any damages, losses and/or causes of action 
# exceed the total amount paid by the user for this software.
#
########################################################################

use FindBin;
use lib $FindBin::Bin;
use CGI;
use CGI::Carp qw(fatalsToBrowser);
use clickt;
use DBI;

&get_setup;
$q = CGI->new;

######################################################################

$id = $q->param('id');

$dsn = "DBI:mysql:$dbname";
$dbh = DBI->connect("$dsn", "$dbusern", "$dbpasswd");
if ( !defined $dbh ) {die "Cannot connect to MySQL server: $DBI::errstr\n"; }

$sth = $dbh->prepare("SELECT * FROM ctracker WHERE urlid = '$id'");
if ( !defined $dbh ) {die "Cannot connect to MySQL server: $DBI::errstr\n"; }
$sth->execute;

while ( @row = $sth->fetchrow() )
	{
	$link = $row[1];
	$clicks = $row[2];
	}

$clicks++;

$sth = $dbh->prepare("UPDATE ctracker SET clicks = '$clicks' WHERE urlid = '$id'");
if ( !defined $dbh ) {die "Cannot connect to MySQL server: $DBI::errstr\n"; }
$sth->execute;

#################

$timep = time();

$sth = $dbh->prepare("INSERT INTO ctracker2 SET
								urlid = '$id',
								cdate = '$timep'");

if ( !defined $dbh ) {die "Cannot connect to MySQL server: $DBI::errstr\n"; }
$sth->execute;

#################

$sth->finish;
$dbh->disconnect;

#print "Content-type: text/html\n\n";
print "Location: $link\n\n";


######################################################################

sub get_setup
{

$exists = (-e "config.cgi");
if ($exists > 0)
	{
	
	open (STP, "config.cgi");
		while (defined($line=<STP>))
			{
			if ($line =~ m/#/g)
				{
				$r = pos($line);
				$line = substr($line, 0, $r - 1);
				}
				
				$line =~ s/\n//g;
	

if ($line =~ /^WEBURL/){$weburl = &get_setup_line($line, WEBURL);}
if ($line =~ /^SCRIPT_URL/){$script_url = &get_setup_line($line, SCRIPT_URL);}

if ($line =~ /^USERNAME/){$username = &get_setup_line($line, USERNAME);}
if ($line =~ /^PASSWORD/){$password = &get_setup_line($line, PASSWORD);}

if ($line =~ /^DBNAME/){$dbname = &get_setup_line($line, DBNAME);}
if ($line =~ /^DBUSERN/){$dbusern = &get_setup_line($line, DBUSERN);}
if ($line =~ /^DBPASSWD/){$dbpasswd = &get_setup_line($line, DBPASSWD);}

			}
	close (STP);
	
	}
}




sub get_setup_line
{
my ($setup_line, $setup_var) = @_;
$crit = "\"";
$setup_line =~ m/$crit/g;
$r1 = pos($setup_line);
$setup_line =~ m/$crit/g;
$r2 = pos($setup_line);
$setup_line = substr($setup_line, $r1, ($r2 - $r1 - 1));
$return_val = $setup_line;
return ($return_val);
}

#### END CONFIGURATION ########################################################




