<?php

/*
	Name:	index.php
	Date:		17:51 2008-10-25
*/



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

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

/*
	应用范例：
	
	/////////////////////////////////////////////////////

	//获取 API 数据
	$data = file_get_contents("http://gg.hualongxiang.com/member/api.php?keys=JMAUCbw8kZ9gvOxDCyqHUsozti6C3mUuyIXvBR68vR%2Bk%2BTgSr0C04kB4j2qWlnb59V8D3RDHQ85iUO80U40rIouBFGG4%2FcwgTbWncKJZkOu8Xt%2FBGBAcBXiLM88jMDYQSipS2rUL2QOkSKnYMUGFyYMMGlkAgniqhTP2MFFl92%2FitCe81phgOw%3D%3D&data=json&source=value&start=&end=&status=-1&size=10&max=100&charset=&key=");

	//去掉 JS 标识符
	$data = str_replace("var JSON = ", "", $data);

	//转换成 UTF-8
	$array = json_decode(iconv('GBK', 'UTF-8', $data),true);

	//处理返回的数据
	if( is_array($array) ){		
		$array = parseArray($array);
	}

	//处理数组
	function parseArray($array){
		foreach ( $array as $key => $val ){
			if( is_string($val) && $key == "config" ){
				 $val = json_decode($val,true);
			}
			if( is_array($val) ){
				$array[$key] = parseArray($val);
			}else{
				$array[$key] = iconv('UTF-8', 'GBK', $val);
			}			
		}
		return $array;
	}


	//调试
	//var_dump($array);
	//print_r($array);

*/
	
	//编码
	$charset 	= GetQuery("charset");

	//频道
	$show	= GetNumber("parent",0);

	//广告位
	$type	= GetNumber("type",0);
	
	//形式
	$show	= GetNumber("show",1);

	//数量
	$number	= GetNumber("number",10);
	
	//宽度
	$width	= GetNumber("width",0);
	
	//高度
	$height	= GetNumber("height",0);
	
	//关键字
	$key 	= GetQuery("key");
	
	//配置
	$config = stripcslashes($_GET["config"]);			
	
	//虚拟广告位
	$area = array( "id"=>time(), "show"=>$show, "number"=>$number, "width"=>$width, "height"=>$height , "config"=>$config );
	
	if(	!in_array($charset,array("gbk","utf-8")) ){
		$charset = $Project["charset"];
	}
	
	//解密钥出错
	/*
	$keys = $_GET["keys"];			
	$hash = unserialize(do_mdecrypt($keys,VI_START));
	
	$va = !is_array($hash);
	$vb = !$hash["site"];
	
	/*
	if( $_SESSION["Manager"]["id"] ){
		$vc = false;
	}else{
		$vc = stripos($_SERVER['HTTP_REFERER'],$hash["site"]) === false;
	}
	*/

	
	//查询
	$DB = VI_DBMODPRE."adsview";			

	//查询条件
	$CO = "";

	if( $key ){
		$CO.=" and name like '%".$key."%' or tag like '%".$key."%'";
	}
	
	if( $parent ){
		$CO.=" and parent = '".$parent."'";
	}
	
	if( $type ){
		$CO.=" and type = '".$type."'";
	}

	//日期筛选
	$start = GetQuery("start");
	$end = GetQuery("end");

	//时间
	if ($start && $end){

		//开始时间
		$e_array=explode("-",$start);
		$time1=mktime(0, 0, 0, $e_array[1], $e_array[2], $e_array[0]);

		//到期时间
		$e_array=explode("-",$end);
		$time2=mktime(23, 59, 59, $e_array[1], $e_array[2], $e_array[0]);

		$CO.=" and  dateline >=".$time1." and dateline <= ".$time2;
	}
	
	//形式
	switch( $source ){

		//文字广告
		case "text":

		break;

		//图片广告
		case "image":

			$CO.=" and file <> '' ";

		break;
		
		//富媒体
		case "rich":

			$CO.=" and file <> '' ";

		break;
	
	}
	
	$sql="select * from $DB where status>0 $CO order by id desc";

	//mysql open
	reader_mysql();
    
    //查询数据库_总记录数
    $result=mysql_query(str_replace("*","count(id) as count",$sql),$mysql);
    $row=mysql_fetch_assoc($result);
    $rows_count=$row["count"];
    
    $Project["queries"]++;

    //分页参数
    $sql .=" limit 0,".$number;
		
	header('Content-Type:text/html; charset='.$charset.'');	

	//VeryIDE 绝对地址
	$HOT = GetHost();

	//广告请求目录 URL
	$API = readpath($HOT.VI_BASE.$Modules["adsview"]["path"]);

	$content = ad_area( $API, $area ) ;

	$result=mysql_query($sql,$mysql);

	$x = 1;
	while($ad=mysql_fetch_assoc($result)){
		
		$config = fix_json($ad["config"]);
		
		$content .= $temp = ad_script($API,$area,$ad,$config).chr(13);

		$x++;
	}
	
	if( $charset != $Project["charset"] ){
		echo iconv($Project["charset"],$charset,$content);
	}else{
		echo $content;	
	}	
	
	//插入广告
	echo '	_ad'.$area["id"].'.adInsert();';

	//mysql close
	reader_mysql(true);	

?>
