<?php 
header("Content-type: text/html; charset=UTF-8");
require_once('libs/mg_db.class.php');
require_once('libs/mg_agent.func.php');
$config = require('config.php');

$c['u_key']    = intval($_GET['u_key']);
$c['u_usid']   = intval( $_GET['u_usid']);

$data = get_gdcache($c['u_key'], $config);

$ad = get_adcache($data, $config);

$http_user_agent = $_SERVER['HTTP_USER_AGENT'];

$c['ip']       = mg_ip();
$c['time']     = time();
$c['uid']      = intval($data['uid']);
$c['url']      = $_SERVER['HTTP_REFERER'];
$c['hash']     = $_GET['hash'];
$c['browser']  = mg_browser($http_user_agent);
$c['platform'] = mg_platform($http_user_agent);

if ($c['browser'] == 'Unknown' || $c['platform'] == 'Unknown')
{
	error_log('Unknown : '.$c['ip'].' : '.$http_user_agent." \n", 3, $config['path']['error_log']);
}

if (in_array($data['adtypeid'], array(153,197,329,386,394,395)))
{
	$c['hash'] = hash('ripemd160', $c['u_key'].$c['time'].$c['ip'].$c['url'].rand(1000, 9999));
	$randad = random_ads($data, $ad);
	$c['u_adid'] = $randad['id'];
}
else 
{
	$c['hash']   = $_GET['hash'];
	$c['u_adid'] = intval($_GET['adid']);
}

$c['uid']   = $data['uid'];
$c['bizid'] = $ad[$c['u_adid']]['bizid'];
$c['adid']  = $c['u_adid'];

counter($c, 3);

if ($data['state'] == 1)
{
	//$tmphash = $data['config']['adtypeid'] == 197 ? '' : "&hash={$c['hash']}";
	$jumpurl = "{$ad[$c['u_adid']]['middle']}";
}
else
{
	exit('金山联盟提示您：<br /><br />　　当前广告位未经审核，一般5个工作日内审核，您可以登录到联盟(<a href="http://union.ijinshan.com/?c=user&a=message">http://union.ijinshan.com/?c=user&a=message</a>)进行咨询。');
	//exit('金山联盟提示您：<br /><br />　　当前广告位未经审核，一般1个工作日内审核，或者您联系客服：QQ 800001444 咨询。');
}

if ( is_array($data['resid']) && array_key_exists($ad[$c['u_adid']]['bizid'], $data['resid']) )
{
	$jumpurl = $data['resid'][$ad[$c['u_adid']]['bizid']];
}

$jumpurl = str_replace('$u_key', $c['u_key'], $jumpurl);
$jumpurl = str_replace('$u_adid', $c['u_adid'], $jumpurl);
$jumpurl = str_replace('$u_hash', $c['hash'], $jumpurl);
$jumpurl = str_replace('$u_usid', $c['u_usid'], $jumpurl);

header('Location: '.$jumpurl);
?>