<?php
require_once(dirname(__FILE__).'/../config.php');

$GET_IP			=	$prepare->get_client_ip();
$geolocation	=	ip2c_geolocation($GET_IP);
$ip['ADDRESS']	=	$GET_IP;

//*** GEOLOCATION ***//
$ip['CONTINENT_CODE']		=	$geolocation['continentCode'];
$ip['CONTINENT_NAME']		=	$geolocation['continentName'];
$ip['COUNTRY_CODE_2']		=	$geolocation['countryCode2'];
$ip['COUNTRY_CODE_3']		=	$geolocation['countryCode3'];
$ip['COUNTRY_NAME']			=	$geolocation['countryName'];
$ip['COUNTRY_LAT']			=	$geolocation['countryLatitude'];
$ip['COUNTRY_LONG']			=	$geolocation['countryLongitude'];

$ip['REGION_NAME']			=	$geolocation['regionName'];
$ip['CITY_NAME']			=	$geolocation['cityName'];
$ip['CITY_LAT']				=	$geolocation['cityLatitude'];
$ip['CITY_LONG']			=	$geolocation['cityLongitude'];




//*** GET ALL VARIABLES ***//
$FULL_URL	=	$prepare->sanitize( $_GET['c'], 'input' );
$URL		=	base64_decode( $FULL_URL );
$DECODE		=	explode( '&', $URL );

$USER_ID	=	str_replace( 'user=', '', $DECODE['0'] );//*** ADVERTISER USER ID ***//
$AD_ID		=	str_replace( 'ad_id=', '', $DECODE['1'] );//*** ADVERTISER AD ID ***//
$KEYWORD	=	str_replace( 'keyword=', '', $DECODE['2'] );//*** ADVERTISER AD KEYWORD ***//
$PRICE		=	str_replace( 'keyword_price=', '', $DECODE['3'] );//*** ADVERTISER AD KEYWORD PRICE ***//
$TARGET		=	str_replace( 'url_target=', '', $DECODE['4'] );//*** ADVERTISER AD KEYWORD PRICE ***//
$SLOT		=	str_replace( 'slot=', '', $DECODE['5'] );//*** PUBLISHER AD SLOT ***//
$CHANNEL_ID	=	str_replace( 'channel_id=', '', $DECODE['6'] );//*** PUBLISHER CHANNEL ID ***//
$PUB_ID		=	str_replace( array('client=','ca-pub-'), array('',''), $DECODE['7'] );//*** PUBLISHER CHANNEL ID ***//

$REFERER	=	str_replace( 'referer=', '', $DECODE['8'] );//*** REFERER DOMAIN ***//
$DOMAIN		=	str_replace( 'domain=', '', $DECODE['9'] );//*** WEBSITE DOMAIN ***//
$CAMPAIGN	=	str_replace( 'campaign=', '', $DECODE['10'] );//*** ADV CAMPAIGN ***//




$CLICK_FRAUD	=	$db->Query("SELECT `lt_date` FROM login_tracking where lt_user=? and lt_ip=? limit 1","is",$PUB_ID,$GET_IP);


if ( empty ( $CLICK_FRAUD ) ) { //*** IF NO CLICK FRAUD ***//


//** DON'T TRACK THE LATEST 60 SECONDS CLICKED ADS **//
$NOW		=	strtotime("+2 seconds");
$MAX_TIME	=	strtotime("-60 seconds");
$PREVENT_MULTIPLE_CLIKS	=	$db->Query("SELECT `aac_id` FROM advertisers_ad_clicks where aac_ip=? and aac_date<? and aac_date>? and aac_ad_id=? limit 1","siii",$GET_IP,$NOW,$MAX_TIME,$AD_ID);


if ( empty ( $PREVENT_MULTIPLE_CLIKS ) ) {

//*** TRACK CLICK FOR ADVERTISER PART ***//
$aac_ad_id		=	$AD_ID;
$aac_user_id	=	$USER_ID;
$aac_word		=	$KEYWORD;
$aac_value		=	$PRICE;
$aac_domain		=	$DOMAIN;
$aac_referer	=	$REFERER;
$aac_ip			=	$GET_IP;
$aac_country	=	$geolocation['countryName'];
$aac_county		=	$geolocation['regionName'];
$aac_city		=	$geolocation['cityName'];
$aac_date		=	strtotime( "now" );

	$db->NoQuery("INSERT INTO advertisers_ad_clicks VALUES(NULL,?,?,?,?,?,?,?,?,?,?,?)","sssssssssss",$aac_ad_id,$aac_user_id,$aac_word,$aac_value,$aac_domain,$aac_referer,$aac_ip,$aac_country,$aac_county,$aac_city,$aac_date);

	//*** advertisers_ad_units_statistics ***//
	$db->NoQuery("update advertisers_ad_units_statistics set aaus_clicks=aaus_clicks+1, aaus_money_spent=aaus_money_spent+" . $PRICE . " where aaus_ad_id=?","i",$AD_ID);

	//*** advertisers_campaigns_statistics ***//
	$db->NoQuery("update advertisers_campaigns_statistics set acs_clicks=acs_clicks+1, acs_money_spent=acs_money_spent+" . $PRICE . " where acs_campaign_id=?","i",$CAMPAIGN);

	//*** advertisers_profiles ***//
	$db->NoQuery("update advertisers_profiles set adv_profile_deposits=adv_profile_deposits-" . $PRICE . ", adv_profile_spent=adv_profile_spent+" . $PRICE . " where adv_profile_user_id=?","i",$USER_ID);


	//*** advertisers_campaigns ***//
	$db->NoQuery("update advertisers_campaigns set adv_campaign_daily_spent=adv_campaign_daily_spent+" . $PRICE . " where adv_campaign_id=?","i",$CAMPAIGN);




//*** TRACK CLICK FOR PUBLISHER PART ***//
$pauc_ad_id		=	$SLOT;
$pauc_user_id	=	$PUB_ID;
$pauc_word		=	$KEYWORD;
$pauc_value		=	( GIVEN_PERCENT*$PRICE ) / 100;
$pauc_domain	=	$DOMAIN;
$pauc_referer	=	$REFERER;
$pauc_ip		=	$GET_IP;
$pauc_country	=	$geolocation['countryName'];
$pauc_county	=	$geolocation['countryName'];
$pauc_city		=	$geolocation['cityName'];
$pauc_date		=	strtotime( "now" );

	$db->NoQuery("INSERT INTO publisher_ad_units_clicks VALUES(NULL,?,?,?,?,?,?,?,?,?,?,?)","sssssssssss",$pauc_ad_id,$pauc_user_id,$pauc_word,$pauc_value,$pauc_domain,$pauc_referer,$pauc_ip,$pauc_country,$pauc_county,$pauc_city,$pauc_date);


		//*** publishers_ad_units_statistics ***//
		$db->NoQuery("update publishers_ad_units_statistics set paus_clicks=paus_clicks+1, paus_money=paus_money+" . $pauc_value . " where paus_ad_id=?","i",$SLOT);

		//*** publisher_channels_statistics ***//
		$db->NoQuery("update publisher_channels_statistics set pcs_clicks=pcs_clicks+1, pcs_money=pcs_money+" . $pauc_value . " where pcs_channel_id=?","i",$CHANNEL_ID);

		//*** publishers_profiles ***//
		$db->NoQuery("update publishers_profiles set pub_profile_money=pub_profile_money+" . $pauc_value . " where pub_profile_user_id=?","i",$pauc_user_id);

}


} else { //*** IF CLICK FRAUD DETECTED ***//

$cf_user_id	=	$PUB_ID;
$cf_ad_id	=	$AD_ID;
$cf_keyword	=	$KEYWORD;
$cf_value	=	$PRICE;
$cf_date	=	strtotime( "now" );
	$db->NoQuery("INSERT INTO click_fraud VALUES(NULL,?,?,?,?,?)","sssss",$cf_user_id,$cf_ad_id,$cf_keyword,$cf_value,$cf_date);
}

header ( 'Location: '.$TARGET );
?>