<?php
ob_start();
// only allow numeric acip's

$acip = $_GET['acip'];

// Create blank #pci and save it with either a pci from the get var or the cookie
$pci = '';

if (isset($_GET['pci'])){
	$pci = $_GET['pci'];
}elseif(null !== (getCookie202('tracking202pci'))) {
		$pci =getCookie202('tracking202pci');
}	


	

if (! is_numeric($acip))
    die();

include_once(str_repeat("../", 2).'202-config/connect2.php');
include_once(str_repeat("../", 2).'202-config/class-dataengine-slim.php');

//do our own url var getting because $_GET changes . to _
$urlvarslist = getUrlVars202();


if(isset($_GET['click_id'])) { //if there's a passed use it
    $click_id = $db->real_escape_string($_GET['click_id']);

}elseif(null !== (getCookie202('tracking202subid'))) { //if there's a cookie use it
		$click_id =getCookie202('tracking202subid');
}else { //if not find the list clicks id of the ip within a 30 day range
    $mysql['user_id'] = 1;
    $mysql['ip_address'] = $db->real_escape_string($_SERVER['REMOTE_ADDR']);
    $daysago = time() - 86400; // 24 hours
    $click_sql1 = "	SELECT 	202_clicks.click_id,ppc_account_id,click_id_public 
					FROM 		202_clicks
					LEFT JOIN	202_clicks_advance USING (click_id)
					LEFT JOIN 	202_ips USING (ip_id)
                    LEFT JOIN	202_clicks_record USING (click_id)
					WHERE 	202_ips.ip_address='".$mysql['ip_address']."'
					AND		202_clicks.user_id='".$mysql['user_id']."'
					AND		202_clicks.click_time >= '".$daysago."'
					ORDER BY 	202_clicks.click_id DESC
					LIMIT 		1";

    $click_result1 = $db->query($click_sql1) or record_mysql_error($click_sql1);
    $click_row1 = $click_result1->fetch_assoc();
    $click_id = $db->real_escape_string($click_row1['click_id']);     
    $mysql['ppc_account_id'] = $db->real_escape_string($click_row1['ppc_account_id']);
    $mysql['click_id_public'] = $db->real_escape_string($click_row1['click_id_public']);
    $pci=$mysql['click_id_public'];
	
}

$mysql['click_id'] = $click_id;

$usedCachedRedirect = false;
if (! $db)
    $usedCachedRedirect = true;
    
    // the mysql server is down, use the txt cached redirect
if ($usedCachedRedirect == true) {
    
    $acip = $_GET['acip'];
    
    // if a cached key is found for this acip, redirect to that url
    if ($memcacheWorking) {
        $getUrl = $memcache->get(md5('ac_' . $acip . systemHash()));
        if ($getUrl) {
            
            $new_url = str_replace("[[subid]]", "p202", $getUrl);
            
            // c1 sring replace for cached redirect
            if (isset($_GET['c1']) && $_GET['c1'] != '') {
                $new_url = str_replace("[[c1]]", $_GET['c1'], $new_url);
            } else {
                $new_url = str_replace("[[c1]]", "p202c1", $new_url);
            }
            
            // c2 sring replace for cached redirect
            if (isset($_GET['c2']) && $_GET['c2'] != '') {
                $new_url = str_replace("[[c2]]", $_GET['c2'], $new_url);
            } else {
                $new_url = str_replace("[[c2]]", "p202c2", $new_url);
            }
            
            // c3 sring replace for cached redirect
            if (isset($_GET['c3']) && $_GET['c3'] != '') {
                $new_url = str_replace("[[c3]]", $_GET['c3'], $new_url);
            } else {
                $new_url = str_replace("[[c3]]", "p202c3", $new_url);
            }
            
            // c4 sring replace for cached redirect
            if (isset($_GET['c4']) && $_GET['c4'] != '') {
                $new_url = str_replace("[[c4]]", $_GET['c4'], $new_url);
            } else {
                $new_url = str_replace("[[c4]]", "p202c4", $new_url);
            }
            
            $urlvars = getPrePopVars($urlvarslist);
            
            $new_url = setPrePopVars($urlvars, $redirect_site_url, false);
       
            header('location: ' . $new_url);
            die();
        }
    }
    
    die("<h2>Error establishing a database connection - please contact the webhost</h2>");
}

/* OK FIRST IF THERE IS NO PUBLIC CLICK_ID, JUST REDIRECT TO THE NORMAL CAMPAIGN */
if (!isset($vars[1]) && $pci == '') {
	
    $mysql['aff_campaign_id_public'] = $db->real_escape_string($acip);
 $aff_campaign_sql = "SELECT   aff_campaign_rotate, 
									aff_campaign_url, 
									aff_campaign_url_2, 
									aff_campaign_url_3, 
									aff_campaign_url_4, 
									aff_campaign_url_5, 
									aff_campaign_name, 
									aff_campaign_cloaking 
						    FROM 	202_aff_campaigns 
						    WHERE 	aff_campaign_id_public='" . $mysql['aff_campaign_id_public'] . "'
						    AND 202_aff_campaigns.user_id=1";
    $aff_campaign_row = memcache_mysql_fetch_assoc($db, $aff_campaign_sql);
    if (empty($aff_campaign_row['aff_campaign_url'])) {
        die();
    } // if there is no aff_url to redirect to DIE!
    
    $redirect_site_url = rotateTrackerUrl($db, $aff_campaign_row);

    $redirect_site_url = replaceTrackerPlaceholders($db, $redirect_site_url, $click_id, $mysql);
	$urlvars = getPrePopVars($urlvarslist);
	
	if (isset($urlvars)) {
        $redirect_site_url = setPrePopVars($urlvars, $redirect_site_url, false);
    }
    // ok if there is a url that exists, if redirect php style, or if its cloaked, redirect meta refresh style.
    if ($aff_campaign_row['aff_campaign_cloaking'] == 0) {
        
        // cloaking OFF, so do a php header redirect

        header('location: ' . $redirect_site_url);
        die();
    } else {
        
        // cloaking ON, so do a meta REFRESH
        $html['aff_campaign_name'] = $aff_campaign_row['aff_campaign_name'];
        ?>

<html>
<head>
<title><?php echo $html['aff_campaign_name']; ?></title>
<meta name="robots" content="noindex">
<meta http-equiv="refresh"
	content="1; url=<?php echo $redirect_site_url; ?>">
</head>
<body>

	<form name="form1" id="form1" method="get"
		action="<?php echo dirname($_SERVER['PHP_SELF']);?>/cl2.php">
		<input type="hidden" name="q"
			value="<?php echo $redirect_site_url; ?>" />
	</form>
	<script type="text/javascript">
					document.form1.submit();
				</script>

	<div style="padding: 30px; text-align: center;">
					You are being automatically redirected to <?php echo $html['aff_campaign_name']; ?>.<br />
		<br /> Page Stuck? <a href="<?php echo $redirect_site_url; ?>">Click
			Here</a>.
	</div>
</body>
</html>

<?php
    
}
    
    // terminate this script, this is the end, if there was no public_click_id
    die();
}

/* ------------------------------------------------------- */
/* ------------------------------------------------------- */
/* ------------------------------------------------------- */
//
//
//
// ANYTHING BELOW THIS ASSUMES THERE IS A PUBLIC CLICK ID
//
//
/* ------------------------------------------------------- */
/* ------------------------------------------------------- */
/* ------------------------------------------------------- */

$mysql['aff_campaign_id_public'] = $db->real_escape_string($_GET['acip']);
$mysql['click_id_public'] = $db->real_escape_string($pci);
$info_sql = "
	SELECT
		2c.click_id,
		2c.user_id,
		2c.ppc_account_id,
		click_filtered,
		landing_page_id,
		click_cloaking,
		click_cloaking_site_url_id,
		click_redirect_site_url_id,
		2ac.aff_campaign_id,
		aff_campaign_rotate, 
		aff_campaign_url, 
		aff_campaign_url_2, 
		aff_campaign_url_3, 
		aff_campaign_url_4, 
		aff_campaign_url_5, 
		aff_campaign_name, 
		aff_campaign_cloaking,
		aff_campaign_payout
	FROM
		202_aff_campaigns AS 2ac,
		202_clicks_record AS 2cr
		LEFT JOIN 202_clicks AS 2c ON (2c.click_id = 2cr.click_id)
		LEFT JOIN 202_clicks_site AS 2cs ON (2cs.click_id = 2cr.click_id)
	WHERE
		2ac.aff_campaign_id_public='" . $mysql['aff_campaign_id_public'] . "'
		AND 2cr.click_id_public='" . $mysql['click_id_public'] . "'
";
$info_row = memcache_mysql_fetch_assoc($db, $info_sql);

// cache the url for later use if db is down
if ($memcacheWorking) {
    
    $url = $info_row['aff_campaign_url'] . "&subid=p202";
    $tid = $acip;
    
    $getKey = $memcache->get(md5('ac_' . $tid . systemHash()));
    if ($getKey === false) {
        $setUrl = setCache(md5('ac_' . $tid . systemHash()), $url, 0);
    }
}

$click_id = $info_row['click_id'];

$mysql['click_id'] = $db->real_escape_string($click_id);

$mysql['aff_campaign_id'] = $db->real_escape_string($info_row['aff_campaign_id']);
$mysql['click_payout'] = $db->real_escape_string($info_row['aff_campaign_payout']);
$mysql['ppc_account_id'] = $db->real_escape_string($info_row['ppc_account_id']);

$update_sql = "
	UPDATE
		202_clicks AS 2c
	SET
		2c.aff_campaign_id='" . $mysql['aff_campaign_id'] . "',
		2c.click_payout='" . $mysql['click_payout'] . "'
	WHERE
		2c.click_id='" . $mysql['click_id'] . "'
";
// this function delays the sql, because UPDATING is very very slow
//delay_sql($db, $update_sql);
$click_result = $db->query($update_sql) or record_mysql_error($db, $update_sql);

$mysql['click_out'] = 1;

if (($info_row['click_cloaking'] == 1) or // if tracker has overrided cloaking on
(($info_row['click_cloaking'] == - 1) and ($info_row['aff_campaign_cloaking'] == 1)) or ((! isset($info_row['click_cloaking'])) and ($info_row['aff_campaign_cloaking'] == 1))) // if no tracker but but by default campaign has cloaking on
{
    $cloaking_on = true;
    $mysql['click_cloaking'] = 1;
    // if cloaking is on, add in a click_id_public, because we will be forwarding them to a cloaked /cl/xxxx link
} else {
    $cloaking_on = false;
    $mysql['click_cloaking'] = 0;
}

$update_sql = "
	UPDATE
		202_clicks_record
	SET
		click_out='" . $mysql['click_out'] . "',
		click_cloaking='" . $mysql['click_cloaking'] . "'
	WHERE
		click_id='" . $mysql['click_id'] . "'
";
$click_result = $db->query($update_sql) or record_mysql_error($db, $update_sql);

$outbound_site_url = 'http://' . $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI'];
$click_outbound_site_url_id = INDEXES::get_site_url_id($db, $outbound_site_url);
$mysql['click_outbound_site_url_id'] = $db->real_escape_string($click_outbound_site_url_id);

if ($cloaking_on == true) {
    $cloaking_site_url = getScheme().'://' . $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI'];
}

$redirect_site_url = rotateTrackerUrl($db, $info_row);

$redirect_site_url = replaceTrackerPlaceholders($db, $redirect_site_url, $click_id, $mysql);

$click_redirect_site_url_id = INDEXES::get_site_url_id($db, $redirect_site_url);
$mysql['click_redirect_site_url_id'] = $db->real_escape_string($click_redirect_site_url_id);

$update_sql = "
	UPDATE
		202_clicks_site
	SET
		click_outbound_site_url_id='" . $mysql['click_outbound_site_url_id'] . "',
		click_redirect_site_url_id='" . $mysql['click_redirect_site_url_id'] . "'
	WHERE
		click_id='" . $mysql['click_id'] . "'
";
$click_result = $db->query($update_sql) or record_mysql_error($db, $update_sql);

// alright now the updates,
// WE WANT TO DELAY THESES UPDATES, in a MYSQL DATABASE? Or else the UPDATES lag the server.

// ADD TO CLICK SUMMARY TABLE?

// update the click summary table if this is a 'real click'
// if ($info_row['click_filtered'] == 0) {

$mysql['landing_page_id'] = $db->real_escape_string($info_row['landing_page_id']);
$mysql['user_id'] = $db->real_escape_string($info_row['user_id']);

// set timezone correctly
$user_sql = "SELECT user_timezone FROM 202_users WHERE user_id='" . $mysql['user_id'] . "'";
$user_row = memcache_mysql_fetch_assoc($db, $user_sql);
date_default_timezone_set($user_row['user_timezone']);


// set the cookie
setClickIdCookie($mysql['click_id'], $mysql['aff_campaign_id']);
// NOW LETS REDIRECT
$urlvars = getPrePopVars($urlvarslist);

$redirect_site_url = setPrePopVars($urlvars, $redirect_site_url, false);


//set dirty hour
$de = new DataEngine();
$data=($de->setDirtyHour($mysql['click_id']));

if ($cloaking_on == true) {
    
    // if cloaking is turned on, meta refresh out
    
    ?>
<html>
<head>
<title><?php echo $html['aff_campaign_name']; ?></title>
<meta name="robots" content="noindex">
<meta http-equiv="refresh"
	content="1; url=<?php echo $redirect_site_url; ?>">
</head>
<body>

	<form name="form1" id="form1" method="get"
		action="<?php echo dirname($_SERVER['PHP_SELF']);?>/cl2.php">
		<input type="hidden" name="q"
			value="<?php echo $redirect_site_url; ?>" />
	</form>
	<script type="text/javascript">
			document.form1.submit();
		</script>

	<div style="padding: 30px; text-align: center;">
			You are being automatically redirected to <?php echo $html['aff_campaign_name']; ?>.<br />
		<br /> Page Stuck? <a href="<?php echo $redirect_site_url; ?>">Click
			Here</a>.
	</div>
</body>
</html>
<?php

} else {
    
    // if cloaking is turned off, php header redirect out
       
    header('location: ' . $redirect_site_url);

}

