<?php

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

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

//载入配置文件
Module :: loader( 'member' );

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

	//获取 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=&`state`=-1&size=10&max=100&charset=&key=");

	//转换成 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   	= getgpc('data');
	
	//数据源
	$source 	= getgpc('source');
	
	//编码
	$charset 	= getgpc('charset');
	
	//状态
	$state   	= getnum('state',-1);
	
	//每页大小
	$page_size	= getnum('size',10);
	
	//数量总量
	$max   		= getnum('max',0);
	
	//关键字
	$field 		= getgpc('field');	
	$key 		= getgpc('key');	
	
	//回调
	$callback	= getgpc('callback');
	
	//预览
	$preview	= getgpc('preview');
	
	if(	!in_array($charset,array("gbk","utf-8")) ){
		$charset = $_G['product']['charset'];
	}
	
	//解密钥出错
	$keys = $_GET["keys"];			
	$hash = unserialize(authcrypt( $keys, VI_START, 'decode' ));
	
	$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 = "mod:member";

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

		break;
		
		//数据表
		default:
		case "value":
		
			$DB = "mod:value";
			
			if( $hash['appid'] ){
				$CO.=" and appid ='".$hash['appid']."'";
			}
		
			if( $hash['fid'] ){
				$CO.=" and fid =".$hash['fid'];
			}
		
			if( $key ){
				$CO.=" and `$field` like '%".$key."%'";
			}
			
			//日期筛选
			$start = getgpc('start');
			$end = getgpc('end');
		
			//时间
			if ($start && $end){
				$CO.=" and  dateline >=".strtotime($start)." and dateline <= ".strtotime($end.' 23:59:59');
			}

		break;
	}

	if( $state>-1 ){
		$CO.=" and `state` = ".$state;
	}
	
	$sql="SELECT * FROM `$DB` WHERE 1=1 $CO ORDER BY id desc";

	//连接数据库
	System :: connect();

	//查询数据库_总记录数
	$row_count = System :: $db -> getCount( $sql );
	
	//分页参数
	$page=getpage("page");
	$page_start=$page_size*($page-1);
	$sql=$sql." limit $page_start,".$page_size;	

	//分页数量
	$page_count=ceil($row_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="'.$row_count.'" pagecount="'.$row_count.'" appid="'.$hash['appid'].'" fid="'.$hash['fid'].'" name="'.$hash['name'].'" start="'.$start.'" end="'.$end.'" page="'.$page.'" key="'.$key.'">';
		
			$result = System :: $db -> getAll( $sql );

        		foreach( $result as $row ){
				
				$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":
		
			!$preview && header('Content-Type:text/javascript; charset='.$charset.'');
		
			$content='{"pagesize":'.$page_size.', "rowscount":'.$row_count.', "pagecount":'.$page_count.', "appid":"'.$hash['appid'].'", "fid":'.$hash['fid'].', "name":"'.$hash['name'].'", "page":'.$page.', "key":"'.$key.'", "items" : [';
		
			$result = System :: $db -> getAll( $sql );
			
			$num_rows = count($result);
			
			$x = 1;
			foreach( $result as $row ){
			
				$n = 1;
				$content .= '{';
				
				foreach ($row as $key => $value) {
	
					//替换换行
					$value = preg_replace("/([\r\n])/","<br />",$value);
					
					if( $key == 'config' ){
						$content .= '"'.$key.'" : '.($value);
					}else{
						$content .= '"'.$key.'" : '.'"'.$value.'"';
					}
					$content .= ($n!=count($row)?',':'');
					
					$n++;
				}
				$content .= '}'.($x!=$num_rows?',':'');
				
				$x++;
			}
			
			$content.=']}';
		
		break;
	
	}

	//关闭数据库
	System :: connect();
		
	//转编码
	if( $charset != $_G['product']['charset'] ){
		$content = iconv( $_G['product']['charset'], $charset, $content );
	}
	
	echo $callback ? $callback.'( '.$content.' );' : $content;

?>
