<?php

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

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

require("../config.php");
require("../function.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);

*/


	//数据类型
	$data   	= GetQuery("data");
	
	//数据源
	$source 	= GetQuery("source");
	
	//编码
	$charset 	= GetQuery("charset");
	
	//状态
	$status   	= GetNumber("status",-1);
	
	//每页大小
	$page_size	= GetNumber("size",10);
	
	//数量总量
	$max   		= GetNumber("max",0);
	
	//关键字
	$key 		= GetQuery("key");
	
	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;
	}
	
	//验证授权信息
	if( $va || $vb || $vc ){
		
		if( $va ){
			$error = 'hash';	
		}elseif( $vb ){
			$error = 'site';	
		}elseif( $vc ){
			$error = 'noallow';	
		}

		switch( $data ){
			case "xml":
				$content='<?xml version="1.0" encoding="'.$charset.'" ?>';				
				$content.='<error content="'.$error.'" />';			
			break;
		
			case "json":			
				$content='var JSON = {"error" : "'.$error.'"}';
			break;
		}
	
		echo $content;
		exit();
	}
	*/

	//查询条件
	$CO = "";
	
	//数据源
	switch( $source ){

		//用户表
		case "member":

			$DB = VI_DBMODPRE."members";

			if( $key ){
				$CO=" and account like '%".$key."%'";
			}

		break;
		
		//数据表
		default:
		case "value":
		
			$DB = VI_DBMODPRE."values";
			
			if( $hash["appid"] ){
				$CO.=" and appid ='".$hash["appid"]."'";
			}
		
			if( $hash["fid"] ){
				$CO.=" and fid =".$hash["fid"];
			}
		
			if( $key ){
				$CO.=" and account like '%".$key."%'";
			}
			
			//日期筛选
			$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;
			}

		break;
	}

	if( $status>-1 ){
		$CO.=" and status = ".$status;
	}
	
	$sql="select * from $DB where 1=1 $CO order by id desc";

	//mysql open
	reader_mysql();
    
    //查询数据库_总记录数
	$rows_count = $db -> getValue(str_replace("*","count(id)",$sql));
    
    $Project["queries"]++;

    //分页参数
    $page=GetPage("page");
    $page_start=$page_size*($page-1);
    $sql=$sql." limit $page_start,".$page_size;	
	
	//分页数量
	$page_count=ceil($rows_count/$page_size);
	
	switch( $data ){
		case "xml":
			
			# UTF-8
			header("Content-Type: text/xml; charset=".$charset."");
	
			$content='<?xml version="1.0" encoding="'.$charset.'" ?>';
			$content.='<data pagesize="'.$page_size.'" rowscount="'.$rows_count.'" pagecount="'.$rows_count.'" appid="'.$hash["appid"].'" fid="'.$hash["fid"].'" name="'.$hash["name"].'" start="'.$start.'" end="'.$end.'" page="'.$page.'" key="'.$key.'">';
		
			$result=mysql_query($sql,$mysql);
			
			while($row=mysql_fetch_assoc($result)){
				
				$cdata = array("tag_book","tag_music","tag_movie","tag_sport","tag_person","tag_other","tag_brand","tag_color","tag_yearn");
			
				$n = 1;
				$content .= '<item ';
				foreach ($row as $key => $value) {
					
					if( !in_array($key,$cdata) ){
						$content .= ''.$key.'='.'"'.str_replace('$','\$',addslashes($value)).'" ';
					}
					
					$n++;
				}
				$content .= '>';
				
				foreach ($row as $key => $value) {
					
					if( in_array($key,$cdata) ){
						$content .= '<'.$key.'><![CDATA['.$value.']]></'.$key.'>';
					}
					
					$n++;
				}
				
				$content .= '</item>';
			
			}
			
			$content.='</data>';
		
		break;
	
		case "json":
		
			header('Content-Type:text/html; charset='.$charset.'');
		
			$content='var JSON = {"pagesize":'.$page_size.', "rowscount":'.$rows_count.', "pagecount":'.$page_count.', "appid":"'.$hash["appid"].'", "fid":'.$hash["fid"].', "name":"'.$hash["name"].'", "page":'.$page.', "key":"'.$key.'", "items" : [';
		
			$result=mysql_query($sql,$mysql);
			
			$num_rows = mysql_num_rows($result);
			
			$x = 1;
			while($row=mysql_fetch_assoc($result)){
			
				$n = 1;
				$content .= '{';
				
				foreach ($row as $key => $value) {
	
					//替换换行
					$value = preg_replace("/([\r\n])/","<br />",$value);				
				
					$content .= '"'.$key.'" : '.'"'.addslashes($value).'"'.($n!=count($row)?',':'');
					$n++;
				}
				$content .= '}'.($x!=$num_rows?',':'');
				
				//echo $x.'!='.$num_rows;
				//echo '<br />';
				$x++;
			}
			
			$content.=']}';
		
		break;
	
	}
	
	if( $charset != $Project["charset"] ){
		echo iconv($Project["charset"],$charset,$content);
	}else{
		echo $content;	
	}	

	//mysql close
	reader_mysql(true);	

?>
