<?php
@session_start();
include("../../dbcon.php");
function ip2long_($ip)
{
 $ip = explode(".",$ip);
 if (!is_numeric(join(NULL,$ip)) or count($ip) != 4) {return false;}
 else {return $ip[3]+256*$ip[2]+256*256*$ip[1]+256*256*256*$ip[0];}
}
$decimal=2;

$memberid="";
if(isset($_SESSION["member_id"])){
	$memberid = $_SESSION['member_id'];
}
else {
	if(isset($_REQUEST['member_id'])) {
		$memberid = $_REQUEST['member_id'];
	}
}

if($memberid != ""){
$memberid=$_SESSION["member_id"];
$querymembers = "SELECT * FROM members WHERE member_id='$memberid'";
$resultmembers = mysql_query ($querymembers);
$linemembers=mysql_fetch_array($resultmembers);
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title><? echo $websitename;?> Administration Panel - Statistics </title>
<link href="stylestats.css" rel="stylesheet" type="text/css" />
</head>

<body>
<table width="100%" border="0" cellspacing="0" cellpadding="0">
        <tr> 
        <td background="imgs/logobg.gif"> <table width="400" border="0" cellspacing="0" cellpadding="3">
              <tr> 
                <td width="23%"><a href="http://www.cyprusnet.com" target="_blank"><img src="imgs/logo.gif" alt="CyprusNet" width="211" height="48" border="0" /></a></td>
              </tr>
            </table>
            <br />
            <table width="400" border="0" cellspacing="0" cellpadding="0">
              <tr>
<th align="left" scope="row"><a href="javascript:history.back(1)">&lt;&lt; Back</a> </th>
</tr>
<tr>
  <th width="32%" align="left" scope="row"><strong class="tdheadername"><?=$linemembers["companyname"]; ?></strong></th>
              </tr>
              <tr>
                <th class="tdheaderoverview" scope="row"><? if(!isset($r1) || $r1==""){
			$r1=="default";
}
if($r1=="FDTD"){
	$fromdate="$from_year-$from_month-$from_day 00:00:00";
	$todate="$to_year-$to_month-$to_day 23:59:59";
	$wherequery=" AND added_on>='$fromdate' AND added_on<='$todate'";
}else{
 if($selection=="CM"){
//	$wherequery=" AND MONTH(added_on)=MONTH(NOW())";
	$from_year=date("Y");
	$from_month=date("m");
	$from_day="01";
	$to_year=date("Y");
	$to_month=date("m");
	$to_day=date("t");
	$fromdate="$from_year-$from_month-$from_day 00:00:00";
	$todate="$to_year-$to_month-$to_day 23:59:59";
	$wherequery=" AND added_on>='$fromdate' AND added_on<='$todate'";
//	}elseif($selection=="FDTD"){
	}elseif($selection=="LM"){
 $LM  = mktime(0, 0, 0, date("m")-1  , date("d"), date("Y"));
	$from_year=date("Y", $LM);
	$from_month=date("m", $LM);
	$from_day="01";
	$to_year=date("Y", $LM);
	$to_month=date("m", $LM);
	$to_day=date("t", $LM);
	$fromdate="$from_year-$from_month-$from_day 00:00:00";
	$todate="$to_year-$to_month-$to_day 23:59:59";
	$wherequery=" AND added_on>='$fromdate' AND added_on<='$todate'";
//	$wherequery=" AND MONTH(added_on)=MONTH(NOW() - INTERVAL 1 MONTH)";
	}elseif($selection=="Y"){
 $Y  = mktime(0, 0, 0, date("m")  , date("d")-1, date("Y"));
	$from_year=date("Y", $Y);
	$from_month=date("m", $Y);
	$from_day=date("d", $Y);
	$to_year=date("Y", $Y);
	$to_month=date("m", $Y);
	$to_day=date("d", $Y);
	$fromdate="$from_year-$from_month-$from_day 00:00:00";
	$todate="$to_year-$to_month-$to_day 23:59:59";
	$wherequery=" AND added_on>='$fromdate' AND added_on<='$todate'";
//	$wherequery=" AND DATE_SUB('$today', INTERVAL 1 DAY) LIKE added_on";
	}else{
	$from_year="";
	$from_month="";
	$from_day="";
	$to_year="";
	$to_month="";
	$to_day="";
	$wherequery="";
	}
}
	$today=date("Y-m-d");
$wherequery .=" AND added_on NOT LIKE '$today%' AND added_on > '2005-10-16%'";
// $tomorrow  = mktime(0, 0, 0, date("m")  , date("d")+1, date("Y"));
?><? //extract the domainname from the id
$linedomain=mysql_fetch_array(mysql_query("SELECT domainname FROM domaincategories WHERE catdom_id='$domain_id'"));echo $linedomain["domainname"];?>
                  IP Addresses</th>
              </tr>
            </table>
            <br />
           <table width="400" border="1" cellpadding="3" cellspacing="0" bordercolor="#BEBEBE" class="tableresults">
              <tr>
              <th colspan="2" align="left"class="tdshowall" scope="row" >
<!--22/11/2005 - 31/12/2005 ???<br /> --><? //Build up the query for advertisments
 //adverts_stats_clicks.ipfrom, banner_stats_clicks.ipfrom
 $query_ips_adv="SELECT * FROM adverts_stats_clicks WHERE domain_id=$domain_id AND click_on LIKE 'www%' AND click_from!=''";
//Search for the advertisments which are bound with the member
	$queryadv="SELECT * FROM advertisment WHERE member_id='$memberid'";
 $resultadv=mysql_query($queryadv);
	$NoOfAdvs=mysql_num_rows($resultadv);
	if($NoOfAdvs>0){
$query_ips_adv .= " AND (";	
$i=1;
while($lineadv=mysql_fetch_assoc($resultadv)){
$query_ips_adv .= " adverts_stats_clicks.advertisment_id = '$lineadv[advertisment_id]'";
//$advert_ids[]=$lineadv[advertisment_id];
if($i<$NoOfAdvs){ $query_ips_adv .= " OR ";}
$i++;
}
$query_ips_adv .= ")";
 			 $query_ips_adv .=$wherequery;
$result_ips_adv=mysql_query($query_ips_adv) or die(mysql_error());
$noofips_adv=mysql_num_rows($result_ips_adv);
}
?><? //Build up the query for banners
 $query_ips_ban="SELECT * FROM banner_stats_clicks WHERE domain_id=$domain_id AND location!='' ";
//Search for the advertisments which are bound with the member
	$queryadv="SELECT * FROM banner WHERE member_id='$memberid'";
 $resultadv=mysql_query($queryadv);
	$NoOfBans=mysql_num_rows($resultadv);
	if($NoOfBans>0){
$query_ips_ban .= " AND (";	
$i=1;
while($lineadv=mysql_fetch_assoc($resultadv)){
$query_ips_ban .= " banner_stats_clicks.banner_id = '$lineadv[module_id]'";
//$advert_ids[]=$lineadv[advertisment_id];
if($i<$NoOfBans){ $query_ips_ban .= " OR ";}
$i++;
}
$query_ips_ban .= ")";
 			$query_ips_ban .=$wherequery;
$result_ips_ban=mysql_query($query_ips_ban) or die(mysql_error());
 $noofips_ban= mysql_num_rows($result_ips_ban);
}
?><? echo $noofips_adv+$noofips_ban;?> Results </th>
              </tr>
              <tr>
                <th align="left" nowrap="nowrap" class="tdsort1st" scope="row">IP
                    Address</th>
                <th align="left" nowrap="nowrap" class="tdsort1st" scope="row"><? $country=array(); $other=0;?>Country</th>
              </tr>
<? 	if($NoOfAdvs>0){while($line_ips_adv=mysql_fetch_assoc($result_ips_adv)){?>              <tr onmouseover="this.className='classover'" onmouseout="this.className=''">
                <th class="tdsort1stPortal" scope="row"><?=$line_ips_adv["ipfrom"];?> <? $ip_long=ip2long_($line_ips_adv["ipfrom"]);?></th>
                <th class="tdsort1stPortal" scope="row"><? $query_country="SELECT Country FROM ip2country1 WHERE IPfrom<=$ip_long AND IPto>=$ip_long";
				$result_country=mysql_query($query_country) or die(mysql_error());
				if(mysql_num_rows($result_country)>0){
				$line_country=mysql_fetch_assoc($result_country);
				echo $line_country["Country"];
				$key=array_search($line_country["Country"],$country);
				if($key>-1){
				$country_count[$key]++;
				}else{
				$country[]=$line_country["Country"];
				$country_count[]=1;
				}
				}else{
				echo "Other";
				$key=array_search("Other",$country);
				if($key>-1){
				$country_count[$key]++;
				}else{
				$country[]="Other";
				$country_count[]=1;
				}
//				if($other>0){
//				$other++;
//				}else{
//				$other=1;
//				}
				}?></th>
</tr>
<? }}?>
<? 	if($NoOfBans>0){
while($line_ips_ban=mysql_fetch_assoc($result_ips_ban)){?>              <tr onmouseover="this.className='classover'" onmouseout="this.className=''">
                <th class="tdsort1stPortal" scope="row"><?=$line_ips_ban["ipfrom"];?><? $ip_long=ip2long_($line_ips_ban["ipfrom"]);?></th>
                <th class="tdsort1stPortal" scope="row"><? $query_country="SELECT Country FROM ip2country1 WHERE IPfrom<=$ip_long AND IPto>=$ip_long";
				$result_country=mysql_query($query_country) or die(mysql_error());
				if(mysql_num_rows($result_country)>0){
				$line_country=mysql_fetch_assoc($result_country);
				echo $line_country["Country"];
				$key=array_search($line_country["Country"],$country);
				if($key>-1){
				$country_count[$key]++;
				}else{
				$country[]=$line_country["Country"];
				$country_count[]=1;
				}
				}else{
				echo "Other";
				$key=array_search("Other",$country);
				if($key>-1){
				$country_count[$key]++;
				}else{
				$country[]="Other";
				$country_count[]=1;
				}
//				if($other>0){
//				$other++;
//				}else{
//				$other=1;
//				}
				}?></th>
</tr>
<? }}?>      </table><? //print_r($country);?><br />
<? //print_r($country_count);?>
 <?php
arsort($country_count);?>

        <table width="400" border="1" cellpadding="3" cellspacing="0" bordercolor="#BEBEBE" class="tableresults">
<tr>
<th colspan="2" align="left" scope="row"class="tdshowall" ><? echo $total_noofips=$noofips_adv+$noofips_ban;?> Results</th>
</tr>
<tr>
<th align="left" nowrap="nowrap" class="tdsort1st" scope="row">Country</th>
<td nowrap="nowrap" class="tdsort">Precentage</td>
</tr>
<? foreach ($country_count as $key => $val) {?>
<tr onmouseover="this.className='classover'" onmouseout="this.className=''">
<th class="tdsort1stPortal" scope="row"><?   echo "$country[$key] ($val)";?></th>
<td class="tdsortstats"><?=round($val/$total_noofips*100,2);?>%</td>
</tr>
<? }?> 
<tr>
<th class="tdsort1stfooter" scope="row">Total</th>
<td class="tdsortfooter">100%</td>
</tr>
</table>
<? }else{
echo "Please define either a member or an advertisment or a banner";
}?>
</body>
</html>
