<?php
include("app_needed/config.php");

	include("app_needed/geoip.inc");
	$gi = geoip_open("app_needed/geoip.dat", GEOIP_STANDARD);
	$country = geoip_country_name_by_addr($gi, $ip);
	$url = $_GET[theurl];
	$browser = $_GET[browser];
	
	// Check if tag exist
	if(!$_GET[secure] || !$_GET[tag] || !$_GET[ad])
	header('Location: '.APP_WEBSITE);
	$sql = mysql_query("SELECT `valid`,`ad_type`,`unique_category`,`unique_user` FROM `app_tags` WHERE `unique`='" . $_GET[tag] . "'");
    $tag = mysql_fetch_array($sql);
	if(!$tag)
	header('Location: '.APP_WEBSITE);
	
	$sql = mysql_query("SELECT `ad_destination`,`ad_content`,`html_content`,`unique_user`  FROM `app_campaigns` WHERE `unique`='" . $_GET[ad] . "' LIMIT 1");
    $ad = mysql_fetch_array($sql);
	if(!$ad)
	{
		header('Location: '.APP_WEBSITE);
	}
	
	
	
	$sql = mysql_query("UPDATE `app_count_imp` SET `click`= '1' WHERE (`verify_hash`='" . $_GET[secure] . "' OR `ip` = '".$ip."') AND `unique_ad` = '".$_GET[ad]."' AND `unique_tag` = '".$_GET[tag]."' AND `date` = '".date('Y-m-d')."'");
	echo '<script>window.location = "'.$ad[ad_destination].'";</script>';
?>