#!/usr/bin/perl
############################################
##                                        ##
##       Developed by Kaviraj Kodai       ##
##          www.UltimateTools.net         ##
##       (scripts@UltimateTools.net)      ##
##                                        ##
##           Ultimate Ad Tracker          ##
##              version:  1.0             ##
##       last modified: August/2003       ##
##           copyright (c) 2003           ##
##                                        ##
##                                        ##
##    For additional information visit    ##
##     http://www.UltimateTools.net       ##
##                                        ##
################################################################
#   COPYRIGHT (C) 2003 KAVIRAJ KODAI. ALL RIGHTS RESERVED      #
################################################################
# Copyright Notice                                             #
# ----------------                                             #
# This program is a commercial product and must NOT be copied, #
# modified, distributed, or installed without a user license   #
# from http://www.UltimateTools.net. Any modification of the   #
# script without the written consent of Kaviraj Kodai is       #
# strictly prohibited.  You may not remove any of these header #
# notices. By using this code you agree to indemnify the author#
# from any liability that might arise from it's use.           #
################################################################

require "config.cgi";
&configure;

################################################################
#                  DO NOT EDIT ANYTHING BELOW                  #
################################################################

use CGI qw (:standard);
use CGI::Carp qw(fatalsToBrowser);

$camp = $ENV{'QUERY_STRING'};
chomp($camp);

$cursor=$dbh->prepare("select acctid from campaigns where campid='$camp'");
$cursor->execute;
if ($cursor->err != 0)
{
        &error("Invalid id detected");
}
($accid) = $cursor->fetchrow_array;
chomp($accid);
$cursor->finish;

if ($accid eq ""){&error("Invalid id detected");}

$tablename = "$accid"."acct";
$cursor=$dbh->prepare("select url from $tablename where campid='$camp'");
$cursor->execute;
($redurl) = $cursor->fetchrow_array;
$cursor->finish;

$refer = $ENV{'HTTP_REFERER'};
$refer =~s/'/''/g;
$ip = $ENV{'REMOTE_ADDR'};
$ip =~s/'/''/g;
$useragent = $ENV{'HTTP_USER_AGENT'};
$useragent =~s/'/''/g;

my $tstamp = time();
my ($y, $m, $d, $ss, $mm, $hh) = (localtime($tstamp))[5,4,3,0,1,2];
$y += 1900;
$y = sprintf("%02d", $y % 100); 
$m += 1;
if (length($ss) < 2) {$ss = "0" . $ss}
if (length($mm) < 2) {$mm = "0" . $mm}
if (length($hh) < 2) {$hh = "0" . $hh}
if (length($m) < 2) {$m = "0" . $m}
if (length($d) < 2) {$d = "0" . $d}
$date = "$m/$d/$y - $hh\:$mm\:$ss";

$time = time;

$query = new CGI;
$theCookie = $query->cookie("ULTTRACK$accid");
chomp($theCookie);

if ($theCookie ne "$camp"){

$cookie = $query->cookie(-name=>"ULTTRACK$accid",
			 -value=>$camp,
			 -expires=>'+1d',
			 -path=>'/');
print "Set-cookie: $cookie";

$tablename2 = "$camp"."track";
$cursor1=$dbh->prepare("INSERT INTO $tablename2 (acctid,refer,ip,useragent,date,time) VALUES ('$accid','$refer','$ip','$useragent','$date','$time')");
$cursor1->execute;
$cursor1->finish;

$tablename3 = "$accid"."acct";
$cursor=$dbh->prepare("SELECT rawclick,uniqueclick from $tablename3 where campid='$camp'");
$cursor->execute;
($rawclick,$unique) = $cursor->fetchrow_array;

$newraw = $rawclick+1;
$newunique = $unique+1;

$tablename4 = "$accid"."acct";
$cursor=$dbh->prepare("update $tablename4 set rawclick='$newraw', uniqueclick='$newunique' where campid='$camp'");
$cursor->execute;
if ($cursor->err != 0)
{
        &error("Cannot update counts!");
}

$cursor->finish;
 $dbh->disconnect; 

print $query->redirect(-url=>"$redurl");
exit;
}


$tablename5 = "$accid"."acct";
$cursor=$dbh->prepare("SELECT rawclick from $tablename5 where campid='$camp'");
$cursor->execute;

($rawclick) = $cursor->fetchrow_array;
$newraw = $rawclick+1;

$tablename6 = "$accid"."acct";
$cursor=$dbh->prepare("update $tablename6 set rawclick='$newraw' where campid='$camp'");
$cursor->execute;
if ($cursor->err != 0)
{
        &error("Cannot update counts!");
}

$cursor->finish;
 $dbh->disconnect; 

print $query->redirect(-url=>"$redurl");
exit;

# end of script