<?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");

/////////////////////

	//编码
	$charset 	= getgpc('charset');

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

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

	//数量
	$number	= getnum("number",10);
	
	//宽度
	$width	= getnum("width",0);
	
	//高度
	$height	= getnum("height",0);
	
	//关键字
	$key 	= getgpc('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 = $_G['product']['charset'];
	}

	//查询条件
	$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 = getgpc('start');
	$end = getgpc('end');

	//时间
	if ($start && $end){
		$CO.=" and  start >=".strtotime($start)." and expire <= ".strtotime($end.' 23:59:59');
	}
	
	//形式
	switch( $source ){

		//文字广告
		case "text":

		break;

		//图片广告
		case "image":

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

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

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

		break;
	
	}
	
	$sql="SELECT * FROM `mod:adsview` WHERE `state`>0 $CO ORDER BY id desc";

	//连接数据库
	System :: connect();
    
	//查询数据库_总记录数
	$row_count = System :: $db -> getCount( $sql );

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

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

	//广告请求目录 URL
	$API = Module :: get_index( 'adsview' );

	$content = Adsview :: area( $API, $area ) ;

	$result = System :: $db -> getAll( $sql );

	$x = 1;
	foreach( $result as $ad ){
		
		$config = fix_json($ad["config"]);
		
		$content .= $temp = Adsview :: script($API,$area,$ad,$config).chr(13);

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

	//关闭数据库
	System :: connect();	

?>
