<?php

/******************************************************
 * CjOverkill version 3.0.1
 * © Kaloyan Olegov Georgiev
 * http://www.icefire.org/
 * spam@icefire.org
 * 
 * Please read the lisence before you start editing this script.
 * 
********************************************************/

include ("../cj-conf.inc.php");
include ("../cj-functions.inc.php");
cjoverkill_connect();

include ("security.inc.php");

$stime=localtime();
$thishour=$stime[2];
$bgthishour="#CCCCCC";

if (!isset($_GET["id"]) || $_GET["id"]=="") {
    cjoverkill_print_error("You must select a trade for this");
}
$tid=$_GET["id"];
$sql=@mysql_query("SELECT domain FROM cjoverkill_trades WHERE trade_id='$tid'") OR
  cjoverkill_db_error(mysql_error());
$tmp=@mysql_fetch_array($sql);
extract($tmp);
$sql=@mysql_query("SELECT
		    raw_in0+raw_in1+raw_in2+raw_in3+raw_in4+raw_in5+raw_in6+raw_in7+raw_in8+raw_in9+raw_in10+raw_in11+raw_in12+
		    raw_in13+raw_in14+raw_in15+raw_in16+raw_in17+raw_in18+raw_in19+raw_in20+raw_in21+raw_in22+raw_in23 AS raw_day,
		    uniq_in0+uniq_in1+uniq_in2+uniq_in3+uniq_in4+uniq_in5+uniq_in6+uniq_in7+uniq_in8+uniq_in9+uniq_in10+uniq_in11+uniq_in12+
		    uniq_in13+uniq_in14+uniq_in15+uniq_in16+uniq_in17+uniq_in18+uniq_in19+uniq_in20+uniq_in21+uniq_in22+uniq_in23 AS uniq_day,
		    clicks0+clicks1+clicks2+clicks3+clicks4+clicks5+clicks6+clicks7+clicks8+clicks9+clicks10+clicks11+clicks12+
		    clicks13+clicks14+clicks15+clicks16+clicks17+clicks18+clicks19+clicks20+clicks21+clicks22+clicks23 AS clicks_day,
		    out0+out1+out2+out3+out4+out5+out6+out7+out8+out9+out10+out11+out12+
		    out13+out14+out15+out16+out17+out18+out19+out20+out21+out22+out23 AS out_day
		    FROM cjoverkill_stats WHERE trade_id='$tid'") OR
  cjoverkill_db_error(mysql_error());
$tmp=@mysql_fetch_array($sql);
extract($tmp);
$sql=@mysql_query("SELECT raw_tot, uniq_tot, clicks_tot, out_tot, signed, last_visit FROM cjoverkill_trades
		    WHERE trade_id='$tid'") OR
  cjoverkill_db_error(mysql_error());
$tmp=@mysql_fetch_array($sql);
extract($tmp);

$tms="Detailed Stats for $domain";

// Totals
if ($out_tot!=0){
    $vale_tot="".round(($clicks_tot/$out_tot)*100,0)."%";
}
else {
    $vale_tot="--";
}
if ($uniq_tot!=0){
    $prod_tot="".round($clicks_tot/$uniq_tot*100, 1)."%";
    $return_tot="".round($out_tot/$uniq_tot*100, 1)."%";
}
else {
    $prod_tot="0%";
    $return_tot="0%";
}

// Day

if ($out_day!=0){
    $vale_day="".round(($clicks_day/$out_day)*100,0)."%";
}
else {
    $vale_day="--";
}
if ($uniq_day!=0){
    $prod_day="".round($clicks_day/$uniq_day*100, 1)."%";
    $return_day="".round($out_day/$uniq_day*100, 1)."%";
}
else {
    $prod_day="0%";
    $return_day="0%";
}

$bgcolor="#EFEFEF";

echo ("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">
	<html>
	<head>
	<title>$tms</title>
	<meta http-equiv=\"Content-Type\" content=\"text/html; charset=iso-8859-1\">
	<link href=\"../cj-style.css\" rel=\"stylesheet\" type=\"text/css\">
	</head>
	<body bgcolor=\"#FFFFFF\" text=\"#000000\" link=\"#000000\" vlink=\"#000000\" alink=\"#000000\">
	<CENTER>&nbsp;<BR>
	<strong><font size=\"4\">$tms</font></strong><BR>
	&nbsp;<BR></CENTER>
	<table width=\"440\" border=\"1\" cellspacing=\"0\" cellpadding=\"1\" align=\"center\">
	<tr class=\"toprows\">
	<td align=\"center\" colspan=\"8\">Totals</td>
	</tr>
	<tr class=\"toprows\">
	<td width=\"40\">Hour</td>
	<td width=\"40\">Q</td>
	<td width=\"60\">Raw</td>
	<td width=\"60\">Uniq</td>
	<td width=\"60\">Out</td>
	<td width=\"60\">Clicks</td>
	<td width=\"60\">Prod</td>
	<td width=\"60\">Return</td>
	</tr>
	<tr bgcolor=\"$bgcolor\" class=\"traderow\">
	<td width=\"40\">&nbsp;</td>
	<td>$vale_tot</td>
	<td>$raw_tot</td>
	<td>$uniq_tot</td>
	<td>$out_tot</td>
	<td>$clicks_tot</td>
	<td>$prod_tot</td>
	<td>$return_tot</td>
	</tr>
	<tr class=\"toprows\">
	<td align=\"center\" colspan=\"8\">Last Day</td>
	</tr>
	<tr bgcolor=\"$bgcolor\" class=\"traderow\">
	<td width=\"40\">&nbsp;</td>
	<td>$vale_day</td>
	<td>$raw_day</td>
	<td>$uniq_day</td>
	<td>$out_day</td>
	<td>$clicks_day</td>
	<td>$prod_day</td>
	<td>$return_day</td>
	</tr>
	<tr class=\"toprows\">
	<td align=\"center\" colspan=\"8\">Last 24 Hours</td>
	</tr>
	");
// Hourly
for ($i=0; $i<=23; $i++){
    $sql=@mysql_query("SELECT raw_in$i AS raw_hour, uniq_in$i AS uniq_hour, clicks$i AS clicks_hour, out$i AS out_hour
		    FROM cjoverkill_stats WHERE trade_id='$tid'") OR
      cjoverkill_db_error(mysql_error());
    $tmp=@mysql_fetch_array($sql);
    extract($tmp);
    if ($out_hour!=0){
	$vale_hour="".round(($clicks_hour/$out_hour)*100,0)."%";
    }
    else {
	$vale_hour="--";
    }
    if ($uniq_hour!=0){
	$prod_hour="".round($clicks_hour/$uniq_hour*100, 1)."%";
	$return_hour="".round($out_hour/$uniq_hour*100, 1)."%";
    }
    else {
	$prod_hour="0%";
	$return_hour="0%";
    }
    $hour=$i;
    $hour2=$i+1;
    if ($i==$thishour){
	$bgcolor=$bgthishour;
    }
    else {
	$bgcolor="#EFEFEF";
    }
    echo ("<tr bgcolor=\"$bgcolor\" class=\"traderow\">
	    <td width=\"40\">$hour-$hour2</td>
	    <td>$vale_hour</td>
	    <td>$raw_hour</td>
	    <td>$uniq_hour</td>
	    <td>$out_hour</td>
	    <td>$clicks_hour</td>
	    <td>$prod_hour</td>
	    <td>$return_hour</td>
	    </tr>
	    ");
}

cjoverkill_disconnect();
echo("</table>
       <br>&nbsp;
	<center><font size=\"2\"><a href=\"javascript:window.close()\">Close Window</a></font></center>
	<br>&nbsp;
	</body>
	</html>
	");

?>
