<?php

/*
*	Copyright VeryIDE,2007-2010
*	http://www.veryide.com/
*
*	$Id: index.php,v2 10:10 2009-7-28 Leilei $
*/

/**********/

//载入全局配置和函数包
require("../../../include/global.php");

require(VI_ROOT."/function/client.php");

require(VI_ROOT."/module/adsview/function.php");
require(VI_ROOT."/module/adsview/config.php");

require(VI_ROOT."/module/analytic/function.php");

//动作
$action = GetQuery("action");

//未知操作时跳转
if( !$action && $Licence['type'] != 'free' ) header("location:service.php?ref=index");

//当前IP
//$ip=getIP();

//跳转的URL
$url = GetQuery("url");

//是否开启日志
$log = GetQuery("log");

//广告ID
$aid = GetNumber("id",0);

//广告位Key
$key = GetQuery("key");

//输出编码
$charset = GetQuery("charset");

//过期秒数
$second=10;


switch($action){
	
	//广告网关
	case "proxy":
	
		//禁止缓存
		header("Cache-Control: max-age=0, must-revalidate, max-age=0");   

		$script = "./cache/" . strtolower($key).'.js';
		
		if( file_exists($script)	){
			$text = file_get_contents($script);
			
			if( in_array(strtolower($charset), array('gbk', 'big5', 'utf-8')) && $text && strtolower($charset) != $Project["charset"] ){
				header('Content-Type:text/html; charset='.$charset);
				$text = iconv($Project["charset"],$charset,$text);
			}
		}
		
		echo $text;
		
		//$config["MAX-VIEWS"]
			
		//ping
		analytic_ping( $appid, $aid, 'view' );		

	break;
	
	//单文件引用
	case "single":
	
		header("Cache-Control: max-age=0, must-revalidate, max-age=0");   

		$script = './adsview.js';
		
		$text = file_get_contents($script);
		
		echo $text;
		
		/****************/		

		$script = "./cache/" . strtolower($key).'.js';
		
		if( file_exists($script)	){
			$text = file_get_contents($script);
			
			if( in_array(strtolower($charset), array('gbk', 'big5', 'utf-8')) && $text && strtolower($charset) != $Project["charset"] ){
				header('Content-Type:text/html; charset='.$charset);
				$text = iconv($Project["charset"],$charset,$text);
			}
		}
		
		echo $text;
		
		//$config["MAX-VIEWS"]
			
		//ping
		analytic_ping($appid,$aid,'view');		

	break;
	
	//链出统计
	case "click":
	
		if ($url && $aid){
			
			//$config["MAX-CLICKS"]
			
			//ping
			analytic_ping($appid,$aid,'click');
			
			//redirect
			header("Location: ".$url);
			
		}
		
	break;
	
	//搜索广告
	case "search":

		$tag = GetQuery("tag");
	
		//mysql open
		reader_mysql();
		
		if( !in_array(strtolower($charset), array('gbk', 'big5', 'utf-8')) ){
			$charset='';
		}
		
		$ads = ad_create("SELECT * FROM ".VI_DBMODPRE."adsarea where `key` ='".$key."'" ,$mysql,$tag,false,$charset);
		
		//exit("SELECT * FROM ".VI_DBMODPRE."adsarea where key ='".$key."'");
		
		echo $ads;
	
		//mysql close
		reader_mysql(true);
	
	break;
	
	//展示统计
	//DELETE
	case "view":
	
		if ($aid){
			
			//$config["MAX-VIEWS"]
			
			loader_cache($appid,"adsview.".$aid);
			
			//更新状态为在播
			if( $VICACHE["mod.adsview"]["status"] == 1 ){
				
				//mysql open
				reader_mysql();
	
				//展示广告并更新广告状态为在播
				$sql="update ".VI_DBMODPRE."adsview set status=2 where status>0 and id=".$aid;
				mysql_query($sql,$mysql);
				
				$Project["queries"]++;

				//更新缓存
				cache_rows($appid,$mysql,VI_DBMODPRE."adsview","select * from ".VI_DBMODPRE."adsview where id=".$aid);
	  
				//mysql close
				reader_mysql(true);
				
				echo 'OK';
				
			}else{
				
				//ping
				analytic_ping($appid,$aid,'view');
				
			}
			
		}
		
	break;
	
	//过期处理
	case "expire":
	
		if ( $aid ){
			
			loader_cache($appid,"adsview.".$aid);
			
			if( $VICACHE["mod.adsview"]["expire"] < time() && $VICACHE["mod.adsview"]["status"] > 0 ){
	
				//mysql open
				reader_mysql();
			
				//停止广告并更新广告状态为停止
				$sql="update ".VI_DBMODPRE."adsview set `end` = ".time().", status=0 where id=".$aid;
				mysql_query($sql,$mysql);
				
				$Project["queries"]++;
				
				//更新缓存
				cache_rows($appid,$mysql,VI_DBMODPRE."adsview","select * from ".VI_DBMODPRE."adsview where id=".$aid);

				//更新脚本缓存
				ad_create("SELECT * FROM ".VI_DBMODPRE."adsarea where id =".$VICACHE["mod.adsview"]["type"] ,$mysql);
		
				//mysql close
				reader_mysql(true);
				
				echo 'OK';
				
			}
			
			/*
			
			//减到一天的时间
			$sql="select status,t_id,a_start,expire from ".VI_DBMODPRE."adsview where expire<".time()." and status>0 and a_id=".$aid;
			
			$result=mysql_query($sql,$mysql);
			
			$Project["queries"]++;
			
			#	!
			if(mysql_num_rows($result)==1){
				$row=mysql_fetch_assoc($result);
			
				$status=$row["status"];
				$t_id=$row["t_id"];
				
				$a_start=$row["a_start"];
				$expire=$row["expire"];
				
			
				//停止广告并更新广告状态为停止
				$sql="update ".VI_DBMODPRE."adsview end=".time().",status=0 where a_id=".$a_id;
				mysql_query($sql,$mysql);
				
				$Project["queries"]++;

				echo 'OK';
				ad_create("SELECT * FROM ".VI_DBMODPRE."adsarea where t_id =".$t_id ,$mysql);
				
			}
			
			*/
		}
		
	break;
}

?>