<?php 
    header('Content-type: application/javascript');
    header('Access-Control-Allow-Origin: *');
	header('Content-type: text/html');
	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);
    if(!$country || $country=="Anonymous Proxy" || $country=="Satellite Provider")
    {
        echo 0;
        exit();
    }
	$url = $_SERVER['HTTP_REFERER'];
	$url = str_replace("http://","",$url);
	$url = str_replace("https://","",$url);
	if(!$url)
	$url = 'Invalid Adress';
	
	$url_check = parse_url($_SERVER['HTTP_REFERER']);
	
	// Check if tag exist
	$sql = mysql_query("SELECT `valid`,`ad_type`,`unique_user`,`website`,`unique` FROM `app_tags` WHERE `unique`='" . $_GET[tag] . "'");
    $tag = mysql_fetch_array($sql);
	
	if(!$tag || $tag[valid] != 1)
	{
		echo "0";
		exit();
	}
	$ad_type = $tag[ad_type];
	
	$split = explode("x",$ad_type);


	
	
	$sql = mysql_query("SELECT `unique`,`ad_content`,`html_content`,`unique_user` FROM `app_campaigns` WHERE 
	(`unique_tags` LIKE '%".$tag[unique]."%' )
	AND (`unique_countries` LIKE '%".$country."%' OR `unique_countries` LIKE 'all' ) 
	AND (`unique_agents` LIKE '%".$_GET[browser]."%' OR `unique_agents` LIKE 'all' ) 
	AND `valid` = '1' 
	ORDER BY RAND() LIMIT 1");
    $ad = mysql_fetch_array($sql);
	if(!$ad)
	{
		echo 0;
		exit();
	}
	$sql = mysql_query("SELECT `ip` FROM `app_count_imp` WHERE 
	`unique_tag`='" . $_GET[tag] . "' AND `unique_ad` = '".$ad[unique]."' 
	AND (`verify_hash` = '".$_GET[secure]."' OR `ip` = '".$ip."') AND `date` = '".date('Y-m-d')."'  LIMIT 1 ");
    $verif = mysql_fetch_array($sql);
	

	if(!$verif)
	{
		  $sql = mysql_query("INSERT INTO `app_count_imp` (`verify_hash`,`unique_tag`,`unique_publisher`,`unique_ad`,`unique_advertiser`,`ip`,`country`,`browser`,`url`,`date`,`time`) VALUES ('" . $_GET[secure] . "','" . $_GET[tag] . "','".$tag[unique_user]."','".$ad[unique]."','".$ad[unique_user]."','".$ip."', '".$country."','".$_GET[browser]."',
		  '".$url."','".date("Y-m-d")."','".date("H:i")."')");

	}
	
?>



<div style="width:<?= $split[0]?>px; height:<?= $split[1]?>px; max-width:<?= $split[0]?>px; max-height:<?= $split[1]?>px; ">
<div id="ad<?=$_GET[tag] ?>" style="position:absolute; width:<?= $split[0]?>px; height:<?= $split[1]?>px; max-width:<?= $split[0]?>px; max-height:<?= $split[1]?>px; ">
<?php if($ad[html_content]) {?>
<?= htmlspecialchars_decode(html_entity_decode($ad[ad_content]),ENT_QUOTES) ?>

<?php }else{ ?>
<a href="<?= APP_WEBSITE ?>/ad.php?tag=<?=$_GET[tag]?>&ad=<?= $ad[unique]?>&secure=<?= $_GET[secure] ?>&browser=<?= $_GET[browser] ?>&theurl=<?= urlencode($url)?>" target="_blank">
<img src="<?= APP_PATH ?>/ads_data/<?= $ad[ad_content] ?>" width="<?= $split[0]?>" height="<?= $split[1]?>" />
</a>
<?php } ?>

<?php if($config->get_value('watermark')){ ?>
<div style='padding:3px; width:90px; position:absolute; bottom:0; right:0; font-size:10px; text-align:right; background:#<?= $config->get_value('watermark_color')?>; color:#<?= $config->get_value('watermark_text_color') ?>; font-family:arial;  -moz-border-radius: 3px 0 0 0;-webkit-border-radius: 3px 0 0 0;-khtml-border-radius: 3px 0 0 0; border-radius: 3px 0 0 0;'><a href='<?= $config->get_value('watermark_url') ?>' target='_blank' style='color:#<?= $config->get_value('watermark_text_color') ?>; text-decoration:none;'><?= $config->get_value('watermark_title') ?></a></div>
<?php } ?>

</div>
</div>


<?php
mysql_close($conn);
?>