#!/usr/bin/perl

print "Content-type: text/html\n\n";

$long = $ENV{'QUERY_STRING'};
if ($long) {
	@pairs=split(/&/,$long);
	foreach $item(@pairs) {
		($name,$content)=split (/=/,$item,2);
		$content=~tr/+/ /;
		$content=~ s/%(..)/pack("c",hex($1))/ge;
		if ($INPUT{$name}) { $INPUT{$name} = $INPUT{$name}.",".$content; }
		else { $INPUT{$name} = $content; }
	}
}
else {
	read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});
	@pairs = split(/&/, $buffer);
	foreach $pair (@pairs) {
		($name, $value) = split(/=/, $pair);
		$value =~ tr/+/ /;
		$value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
		if ($INPUT{$name}) { $INPUT{$name} = $INPUT{$name}.",".$value; }
		else { $INPUT{$name} = $value; }
	}
}

$key = "7652121111";

unless ($INPUT{key} eq $key) {
print qq~
<html>
<head>
	<title>Login to Statistics Below</title>
</head>
<body>
<h2>Enter Security Key To Continue!</h2><br>
<form method="post" action="view_log.idx">
<input type="text" name="key" class="form">
<input type="submit" class="submit" value="Login">
</form>
</div>
</body>
</html>
~;
exit 0;
}


$d_name = "muncieml_db1";
$d_username = "muncieml_db1";
$d_pass = "\@rockdb1";

use DBI;
my $dsn = "DBI:mysql:database=$d_name;host=localhost";
my $dbh = DBI->connect($dsn, $d_username, $d_pass, {RaiseError => 1}) || die $DBI::errstr;

my $caasth = $dbh->prepare("select COUNT(visitor_num) from starpress_ideal_ad");
$caasth->execute;
while (($allcount) = $caasth->fetchrow_array()) {
$all_count = "$allcount";
}


my $aasth = $dbh->prepare("select visitor_num,DATE_FORMAT(datetime, '%b %D, %Y at %h\:%i%p'),ipadd,referer,broswer from starpress_ideal_ad GROUP BY ipadd ORDER BY datetime ASC");
$aasth->execute;

$start_count = "1";

print qq~
<html>
<head>
<title>Visit Laurel Meadows Grand Opening AD Tracker</title>
<style>
	a:link			{ font-size: 10px; color: #ffff99; font-weight: bold;text-decoration: none; background-color: transparent; }
	a:visited 		{ font-size: 10px; color: #ffff99; font-weight: bold; text-decoration: none; background-color: transparent; }
	a:active		{ font-size: 10px; color: #ffff99; font-weight: bold; text-decoration: none; background-color: transparent; }
	a:hover 		{ font-size: 10px; color: #ffffff; font-weight: bold; text-decoration: underline; background-color: transparent; }

	td				{ font-family: tahoma; font-size: 9px; font-weight: bold; color: #000000; text-decoration: none; }
	table			{ font-family: tahoma; font-size: 9px; font-weight: bold; color: #000000; text-decoration: none; }
	body 			{ font-family: tahoma; font-size: 9px; font-weight: bold; color: #000000; text-decoration: none; }

	.header 		{ font-size: 12px; background: black; color: white; }
	.sheader 		{ font-size: 9px; background: white; color: black; }

</style>
</head>
<body bgcolor="white" leftMargin="10" topMargin="10" marginwidth="10" marginheight="10">
<br>
<font size="4"><u>TheStarPress.com Pop Up Laurel Meadows AD Campaign April 25th, 26th and 27th Detailed Visitor Tracker</u></font><br><br>
<br>
<table border="0" width="100%" bgcolor="black">
<tr>
<td valign="top" width="100%" align="center">
	<table border="0" cellpadding="2" width="100%" cellspacing="1" bgcolor="black">
		<tr>
			<td valign="top" class="header">#</td>
			<td valign="top" class="header">Date & Time</td>
			<td valign="top" class="header">IP Address</td>
			<td valign="top" class="header">Referer</td>
			<td valign="top" class="header">Broswer</td>
		</tr>~;
while (($num,$datetime,$ipadd,$referer,$browser) = $aasth->fetchrow_array()) {

## CHECK AMOUNT OF VISITS PER IP
my $vpip = $dbh->prepare("select COUNT(visitor_num) from starpress_ideal_ad where ipadd = '$ipadd'");
$vpip->execute;
while (($vpipcount) = $vpip->fetchrow_array()) {
$vpip_count = "$vpipcount";
}


## LABEL RYAN AND EAGLE IPS
if ($ipadd eq "68.51.82.210") { $ipadd = "Ryan's Home"; }
if ($ipadd eq "70.89.193.137") { $ipadd = "Eagle Real Estate, Inc."; }

print qq~<tr>
			<td valign="top" class="sheader">$start_count</td>
			<td valign="top" class="sheader">$datetime</td>
			<td valign="top" class="sheader">$ipadd ($vpip_count)</td>
			<td valign="top" class="sheader">$referer</td>
			<td valign="top" class="sheader">$browser</td>
		</tr>~;
$start_count++;
$vpip_count = "0";
}

#WHEN LAST COUNT SET BACK TO CORRECT NUMBER
$start_count = ($start_count-1);

print qq~</table>
</td>
</tr>
</table>
<br>
<font size="4">$start_count Unique Visitors | $all_count Total Impressions (Includes Users Who Clicked More Than Once)</font><br><br>
</body>
</html>
~;