<?php
@session_start();
include(dirname(__FILE__) . "/../../dbcon.php");

/* Get Max & Min Dates **********************************/
$max_years_deep = 1;
$current_year = intval(date("Y"));
$current_month = intval(date("m"));
$current_day = intval(date("d"));


$prev_year = $current_year - $max_years_deep;
$min_date_timestamp = strtotime("$prev_year-01-01");
$min_date = date("Y-m-d", $min_date_timestamp);

$prev_month = 1;
$prev_day = 1;

/*********************************************************/
function getTimestamp($date, $mode="short") {
	global $prev_year;
	if(!empty($date)) {
		$parts = explode("/", $date);
		//if(intval($parts[2]) < $prev_year) $parts[2] = $prev_year;
		$timestamp = mktime(12, 0, 0 , intval($parts[1]), intval($parts[0]), intval($parts[2]));
		return $timestamp;
	}
	if($mode == "short") return date("Y-m-d");
	else return date("Y-m-d H:i:s");
}

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 = "";
$type = isset($_REQUEST['type']) ? trim(mysql_real_escape_string($_REQUEST['type'])) : 'adv';
if(isset($_GET['member_id']) && intval($_GET['member_id'])>0) {
	$memberid = $_GET['member_id'];
	$querymembers = "SELECT * FROM members WHERE member_id='$memberid'";
	$resultmembers = mysql_query ($querymembers);
	$linemembers=mysql_fetch_array($resultmembers);
}
else {
	if(isset($_SESSION["member_id"])){
		$memberid=$_SESSION["member_id"];
		$querymembers = "SELECT * FROM members WHERE member_id='$memberid'";
		$resultmembers = mysql_query ($querymembers);
		$linemembers=mysql_fetch_array($resultmembers);
	}
	elseif(isset($advertisment_id) && $advertisment_id>0){
		$queryadvertisment = "SELECT * FROM advertisment WHERE advertisment_id='$advertisment_id'";
		$resultadvertisment = mysql_query ($queryadvertisment);
		$lineadvertisment=mysql_fetch_array($resultadvertisment);
		$memberid=$lineadvertisment["member_id"];
	}
	elseif(isset($banner_id) && $banner_id>0){
		$querybanner = "SELECT * FROM banner WHERE module_id='$banner_id'";
		$resultbanner = mysql_query ($querybanner);
		$linebanner=mysql_fetch_array($resultbanner);
		$memberid=$linebanner["member_id"];
	}
	else{
		echo "Please define either a member or an advertisment or a banner";
	}
}
?>
<!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><?php 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 width="32%" align="left" scope="row"><strong class="tdheadername"><?php echo $linemembers["companyname"]; ?></strong></th>
              </tr>
              <tr>
                <th class="tdheaderoverview" scope="row">
                <?php 
				// Set the default selection parameters
				if(!isset($_REQUEST['r1']) || $_REQUEST['r1']!="FDTD") $r1 = "default";
				if(!isset($_REQUEST['selection'])){
					$_REQUEST['selection'] = "Y";
					$selection = $_REQUEST['selection'];
				}
				else {
					$selection = $_REQUEST['selection'];
				}

				if(isset($_REQUEST['r1']) && $_REQUEST['r1']!="") $r1 = $_REQUEST['r1'];
				
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=="CY"){
		$fromdate 	= date("Y-m-d", getTimestamp("01/01/$current_year"));
		$todate		= date("Y-m-d");
		$wherequery=" AND added_on >= '$fromdate' AND added_on <= '$todate' ";
	}
	elseif($selection=="LY"){
		$fromdate 	= date("Y-m-d", getTimestamp("01/01/$prev_year"));
		$todate		= date("Y-m-d", getTimestamp("01/01/$current_year"));
		$wherequery=" AND added_on >= '$fromdate' AND added_on < '$todate' ";
	}
	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="";
	}
}

if(!isset($wherequery) || $wherequery == "") {
	$today=date("Y-m-d");
	$wherequery .=" AND added_on between '$current_year-01-01' and '$today'";
}
 //extract the domainname from the id
$linedomain = mysql_fetch_array(mysql_query("SELECT domainname FROM domaincategories WHERE catdom_id='$domain_id'"));echo $linedomain["domainname"];?>
                  Country Statistics</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" >
<?php 

//Build up the query for advertisments

$noofips_adv = 0;
$NoOfAdvs = 0;
if($type == "adv") {
	//$query_ips_adv="SELECT * FROM adverts_stats_clicks WHERE domain_id=$domain_id AND click_on LIKE 'www%' AND click_from!='' and click_from not like 'http%' and ipfrom REGEXP '[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}'";
	$query_ips_adv="SELECT * FROM adverts_stats_clicks WHERE domain_id=$domain_id AND click_on LIKE 'www%' AND click_from!='' ";
	echo "<br/>$query_ips_adv";
	//Search for the advertisments which are bound with the member
	$queryadv="SELECT * FROM advertisment WHERE member_id='$memberid'";
	echo "$queryadv<br/>";
	$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;
		
		echo $query_ips_adv;
		
		$result_ips_adv=mysql_query($query_ips_adv) or die(mysql_error());
		$noofips_adv=mysql_num_rows($result_ips_adv);
	
	}
}

$noofips_ban = 0;
$NoOfBans = 0;
if($type == "ban") {
	//Build up the query for banners
	//$query_ips_ban="SELECT * FROM banner_stats_clicks WHERE domain_id=$domain_id AND ipfrom REGEXP '[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}' AND location!='' ";
	$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_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"><?php $country=array(); $other=0;?>Country</th>
              </tr>
<?php 	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"><?php echo $line_ips_adv["ipfrom"];?> 
                <?php 
				$ip_long=ip2long_($line_ips_adv["ipfrom"]);?></th>
                <th class="tdsort1stPortal" scope="row">
                <?php 
				$query_country="SELECT Country FROM ip2country1 WHERE $ip_long between IPfrom AND IPto";
				$result_country=@mysql_query($query_country);
				if($result_country && 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";
					if($other>0){
						$other++;
					}else{
						$other=1;
					}
				}?></th>
</tr>
<?php }}?>
<?php 	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">
                <?php 
				echo $line_ips_ban["ipfrom"];
				?>
				<?php 
				$ip_long=ip2long_($line_ips_ban["ipfrom"]);?>
				</th>
                <th class="tdsort1stPortal" scope="row">
                <?php 
				$query_country="SELECT Country FROM ip2country1 WHERE IPfrom<=$ip_long AND IPto>=$ip_long";
				$result_country = @mysql_query($query_country);
				if($result_country && 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";
					if($other>0){
						$other++;
					}else{
						$other=1;
					}
				}?></th>
</tr>
<?php }}?>      </table><?php //print_r($country);?><br />
<?php //print_r($country_count);?>-->
 <?php
 if(is_array($country_count)){
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" ><?php 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">Percentage</td>
</tr>
<?php if(is_array($country_count)){
foreach ($country_count as $key => $val) {?>
<tr onmouseover="this.className='classover'" onmouseout="this.className=''">
<th class="tdsort1stPortal" scope="row"><?php   echo "$country[$key] ($val)";?></th>
<td class="tdsortstats"><?php echo round($val/$total_noofips*100,2);?>%</td>
</tr>
<?php }}?> 
<tr onmouseover="this.className='classover'" onmouseout="this.className=''">
<th class="tdsort1stPortal" scope="row">Other (<?php echo $other;?>)</th>
<td class="tdsortstats"><?php echo round($other/$total_noofips*100,2);?>%</td>
</tr>
<tr>
<th class="tdsort1stfooter" scope="row">Total</th>
<td class="tdsortfooter">100%</td>
</tr>
</table> 
<!--       
<p>
	<a href="pop-country-statistic2.php?member_id=<?php echo $memberid;?>&domain_id=<?php echo $domain_id;?>&selection=<?php echo $selection;?>&r1=<?php echo $r1;?>&from_day=<?php echo $from_day;?>&from_month=<?php echo $from_month;?>&from_year=<?php echo $from_year;?>&to_day=<?php echo $to_day;?>&to_month=<?php echo $to_month;?>&to_year=<?php echo $to_year;?>&type=<?php echo $type;?>">IP Analysis</a>
</p>
-->
</body>
</html>
