<?php 
@session_start();
include(dirname(__FILE__) . "/../../dbcon.php");
$decimal=2;
$memberid="";

if(isset($_REQUEST)) {
	$member_id = isset($_REQUEST['member_id']) ? intval($_REQUEST['member_id']) : 0;
	$domain_id = isset($_REQUEST['domain_id']) ? intval($_REQUEST['domain_id']) : 0;
	$selection = isset($_REQUEST['selection']) ? trim($_REQUEST['selection']) : '';
	$r1 = isset($_REQUEST['r1']) ? trim($_REQUEST['r1']) : 'default';
	$from_day = isset($_REQUEST['from_day']) ? trim($_REQUEST['from_day']) : '';
	$from_month = isset($_REQUEST['from_month']) ? trim($_REQUEST['from_month']) : '';
	$from_year = isset($_REQUEST['from_year']) ? trim($_REQUEST['from_year']) : '';
	$to_day = isset($_REQUEST['to_day']) ? trim($_REQUEST['to_day']) : '';
	$to_month = isset($_REQUEST['to_month']) ? trim($_REQUEST['to_month']) : '';
	$to_year = isset($_REQUEST['to_year']) ? trim($_REQUEST['to_year']) : '';
	$type = isset($_REQUEST['type']) ? trim($_REQUEST['type']) : 'adv';
}


if(isset($_REQUEST['debugstats'])) {
	echo '<pre>', var_export($_GET, true), '</pre>';
}

$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"];
		$memberid=$_SESSION["member_id"];
		$querymembers = "SELECT * FROM members WHERE member_id='$memberid'";
		$resultmembers = mysql_query ($querymembers);
		$linemembers=mysql_fetch_array($resultmembers);
	}else{
		echo "Please define either a member or an advertisment or a banner";
	}
}

?>
<div class="box box-default box-solid">
    <div class="box-header with-border">
        <strong><?php echo $linemembers["companyname"]; ?></strong>
    </div>
    <div class="box-body">
        IP Addresses in <b class="text-info">
            <?php
            // Set the default selection parameters
            $fromdate="$from_year-$from_month-$from_day";
            $todate="$to_year-$to_month-$to_day";
            $wherequery=" AND added_on>='$fromdate 00:00:00' AND added_on<='$todate 23:59:59'";
            //extract the domainname from the id
            $linedomain = mysql_fetch_array(mysql_query("SELECT domainname FROM domaincategories WHERE catdom_id='$domain_id'"));
            echo $linedomain["domainname"];
            ?>
        </b>
    </div>
</div>


<?php
// Get All catdoms
$catdoms = array(0, $domain_id);
$q = "SELECT * FROM domaincategories WHERE parent_id='$domain_id';";
$r = @mysql_query($q);
if($r && mysql_num_rows($r) > 0) {
    while($row = mysql_fetch_assoc($r)) {
        $catdoms [] = intval($row['catdom_id']);
    }
}

$catdoms_str = implode(", ", $catdoms);


//Build up the query for advertisments
//adverts_stats_clicks.ipfrom, banner_stats_clicks.ipfrom

$NoOfAdvs = 0;
$noofips_adv = 0;
if($type == "adv") {
    //$query_ips_adv="SELECT ipfrom FROM adverts_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 click_on LIKE 'www%' AND click_from!='' AND click_from not like 'http%'";
    $query_ips_adv="SELECT ipfrom, count(ipfrom) as num_ipfrom FROM adverts_stats_clicks WHERE domain_id='$domain_id' AND catdom_id IN ($catdoms_str) AND ipfrom REGEXP '^[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}$' AND click_on LIKE 'www%' AND click_from IN ('s', 'top', 'sel', 'adv', 'e') ";


    //Search for the advertisments which are bound with the member
    $queryadv = "SELECT * FROM advertisment WHERE member_id='$memberid'";
    //print "<br/>".$queryadv;
    $resultadv = mysql_query($queryadv);
    $NoOfAdvs = mysql_num_rows($resultadv);
    if($NoOfAdvs>0){
        //$query_ips_adv .= " AND (";
        $i=1;
        $adv_ids = array();
        while($lineadv=mysql_fetch_array($resultadv)){
            //$query_ips_adv .= " adverts_stats_clicks.advertisment_id = '$lineadv[advertisment_id]'";
            $adv_ids[] = $lineadv['advertisment_id'];
            //if($i<$NoOfAdvs){ $query_ips_adv .= " OR ";}
            //$i++;
        }
        //$query_ips_adv .= ")";
        $adv_str = implode(",", $adv_ids);
        $query_ips_adv .= " AND adverts_stats_clicks.advertisment_id IN ($adv_str)";
        $query_ips_adv .=$wherequery;
        $query_ips_adv .=" GROUP BY ipfrom";

        //echo "<br>$query_ips_adv";

        //print "<br/>".$query_ips_adv;
        //die();

        $result_ips_adv=mysql_query($query_ips_adv) or die(mysql_error());
        $noofips_adv=mysql_num_rows($result_ips_adv);
    }
}
?>
<?php

//print $query_ips_ban;

$NoOfBans = 0;
$noofips_ban = 0;
if($type == "ban") {
    //Build up the query for banners
    //$query_ips_ban="SELECT ipfrom 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  ipfrom, count(ipfrom) as num_ipfrom 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 in ('left', 'right', 'middle', 'popup') ";

    //Search for the banners 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;
        $ban_ids = array();
        while($lineadv=mysql_fetch_assoc($resultadv)){
            $ban_ids[] = $lineadv['module_id'];
            //$query_ips_ban .= " banner_stats_clicks.banner_id = '$lineadv[module_id]'";
            //$advert_ids[]=$lineadv[advertisment_id];
            //if($i<$NoOfBans){ $query_ips_ban .= " OR ";}
            //$i++;
        }
        $bans_str = implode(",",$ban_ids);
        $query_ips_ban .= " banner_id IN ($bans_str)";
        $query_ips_ban .= $wherequery;
        $query_ips_ban .=" GROUP BY ipfrom";

        //echo '<br>' . $query_ips_ban . '<br>';

        $result_ips_ban=mysql_query($query_ips_ban) or die(mysql_error());
        $noofips_ban= mysql_num_rows($result_ips_ban);
    }
}

?>
<table class="table table-bordered">
    <thead>
    <tr>
        <th colspan="2">Total Number Of Unique IPs: <?php echo $noofips_adv+$noofips_ban;?></th>
    </tr>
    <tr>
        <th colspan="2">IP Addresses & Clicks</th>
    </tr>
    </thead>
    <tbody>
        <?php
        if($NoOfAdvs>0){
            ?>
            <tr>
                <td colspan="2" class="bg-gray"><b>Clicks On Adverts</b></td>
            </tr>
            <tr>
                <td><b>IP Address</b></td>
                <td class="text-right"><b>Clicks</b></td>
            </tr>
            <?php
            $total_clicks = 0;
            while($line_ips_adv=mysql_fetch_assoc($result_ips_adv)){
                $total_clicks += intval($line_ips_adv["num_ipfrom"]);
                ?>
                <tr>
                    <td class="text-left"><?php echo $line_ips_adv["ipfrom"];?></td>
                    <td class="text-right"><?php echo $line_ips_adv["num_ipfrom"];?></td>
                </tr>
                <?php
            }
            ?>
            <tr>
                <td class="text-right"><b>Total Clicks</b></td>
                <td class="text-right"><b><?=$total_clicks;?></b></td>
            </tr>
            <?php
        }
        ?>
        <?php
        if($NoOfBans>0){
            ?>
            <tr>
                <td colspan="2" class="bg-gray"><b>Clicks On Banners</b></td>
            </tr>
            <tr>
                <td><b>IP Address</b></td>
                <td class="text-right"><b>Clicks</b></td>
            </tr>
            <?php
            $total_clicks = 0;
            while($line_ips_ban=mysql_fetch_assoc($result_ips_ban)){
                $total_clicks += intval($line_ips_ban["num_ipfrom"]);
                ?>
                <tr>
                    <td class="text-left"><?php echo $line_ips_ban["ipfrom"];?></td>
                    <td class="text-right"><?php echo $line_ips_ban["num_ipfrom"];?></td>
                </tr>
                <?php
            }
            ?>
            <tr>
                <td class="text-right"><b>Total Clicks</b></td>
                <td class="text-right"><b><?=$total_clicks;?></b></td>
            </tr>
            <?php
        }
        ?>
    </tbody>
</table>
