<?php
if(!defined('APP_PATH')||!defined('I_PATH')){exit('Access Denied');}
class controllerBase { 
	public $v;
	private $__template_vals = array();
	public function __construct()
	{	
		if( TRUE == func_get_iglobal(array('view','enabled'))){
			$this->v = syClass('viewBase');
			//$this->ldnavhover = !isset($_GET['tid'])?0:$_GET['tid'];
		}
	}
    public function jump($url, $delay = 0){
		echo "<html><head><meta http-equiv='refresh' content='{$delay};url={$url}'></head><body></body></html>";
		exit;
    }
	
    public function getmytid($tid)
    {
    	$arr = syDB('classtype')->find(array('tid'=>$tid),null,'pid');
    	if($arr['pid'] != '0')
    	{ 
    		return $this->getmytid($arr['pid']);
    	}
    	return $tid;
    }    
    
    public function getnavid()
    {
    	if(isset($_REQUEST['tid']))
    	{
    		return self::getmytid($_REQUEST['tid']);
    	}
    	$mytype = isset($_REQUEST['c'])?($_REQUEST['c']=='article' || $_REQUEST['c']=='product')?$_REQUEST['c']:'0':'0';
    	if($mytype!='0')
    	{
    		if(!isset($_REQUEST['id'])){return '0';}
    		$mytid = syDB($mytype)->find(array('id'=>$_REQUEST['id']),null,'tid');
    		return $mytid['tid']!='0'?self::getmytid($mytid['tid']):'0';
    	}
    	return $mytype;
    }
    
    
    /**
     * 
     * 进入QQ登陆
     * @param int $type  0 前台登陆，1 后台登陆QQ
     */
    public  function go_qqlogin($type=0,$referer_url=NULL)
    {
    		$_SESSION['referre']=$_SERVER['HTTP_REFERER'];//$_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
    		if(isset($referer_url))
    		{
    			$_SESSION['referre']=$referer_url;
    		}
    		$_SESSION['referre_type']=$type;
    		header("location:".$GLOBALS['iConfig']['ext']['http_path']."/qq/index.php");
    }
    
    
    /**
     * 添加一个QQ分享
     * Enter description here ...
     * @param array $content 结构
		$content=array();
		$content['title']="东莞蓝鼎网智能建站系统ICMS盛世推出";
		$content['url']="http://www.5545.cn/icms/";
		$content['comment']="好东西分享一下";
		$content['summary']="超过1W企业选择我们;相信大家没有错的；第一个社区营销类建站系统";
		$content['images']="http://www.5545.cn/icms/template/other/images//logo.gif";
		$content['nswb']="1";
     */
    public  function share_qq($content=array())
    {
    	//添加成功 同步数据到 QQ
    	
		if ($GLOBALS ['iConfig'] ['ext'] ['qq_sync'] == 1) //设置成同步QQ
		{
			//print_r($GLOBALS);
			//print_r($_SESSION);
			if (isset ( $_SESSION ['openid'] )) {
		    	$action="add_share";
		    	require_once APP_PATH."/qq/index.php";
		    	add_share_content($content);
			}
		}
    }
    
    
	public function geticonfig()
	{
		$url = $_SERVER['HTTP_HOST']?$_SERVER['HTTP_HOST']:$_SERVER['SERVER_NAME'];
		$url = 'http://'.$url.str_replace('index.php','',$_SERVER['SCRIPT_NAME']);	
		$statcode = syDB('sysconfig')->find(array('code'=>'stat_code'));
		return array(
					'stat_code'=>htmlspecialchars_decode($statcode['value']),
					'file_config'=>$GLOBALS['LD_I'],
					'url' =>$url,
					'tid'=>$this->getnavid(),
					'ldurl'=>$GLOBALS['TURL'],//TURL
					'www'=>$GLOBALS['WWW'],
					'logo'=>$GLOBALS['iConfig']['ext']['site_logo'],	//网站logo
					'online'=>$GLOBALS['iConfig']['online'],	//网站客服
					'http_path'=>$GLOBALS['iConfig']['ext']['http_path'],	//网站logo
		);
	}

	public function __set($name, $value)
	{
		if(TRUE == func_get_iglobal(array('view','enabled')) && false !== $value){
			$this->v->engine->assign(array($name=>$value));
		}
		$this->__template_vals[$name] = $value;
	}

	public function __get($name)
	{
		return $this->__template_vals[$name];
	}

	public function display($tplname, $output = TRUE)
	{
		@ob_start();
		if(TRUE == func_get_iglobal(array('view','enabled'))){
			$this->v->display($tplname);
		}else{
			extract($this->__template_vals);
			require($tplname);
		}
		if( TRUE != $output )return ob_get_clean();
	}

	public function auto_display($tplname)
	{
		if( TRUE != $this->v->displayed && FALSE != func_get_iglobal(array('view','auto_display'))){
			if( method_exists($this->v->engine, 'templateExists') && TRUE == $this->v->engine->templateExists($tplname))$this->display($tplname);
		}
	}

	public function __call($name, $args)
	{
		if(in_array($name, func_get_iglobal(array('auto_load_controller')))){
			return syClass($name)->__input($args);
		}elseif(!method_exists( $this, $name )){
			syError("方法 {$name}未定义！<br />请检查是否控制器类(".get_class($this).")与数据模型类重名？");
		}
	}

	public function getView()
	{
		$this->v->addfuncs();
		return $this->v->engine;
	}
	
}

class sysParams {
	private $args = null;
	public function __construct(){
		$this->args = $_REQUEST;
		$this->mescape=syClass('modelBase');
	}

	public function get($name = null, $ftype = 0, $default = FALSE, $method = null)
	{
		if(null != $name){
			if( $this->has($name) ){
				if( null != $method ){
					switch (strtolower($method)) {
						case 'get':
							return $this->filters($_GET[$name]);
						case 'post':
							return $this->filters($_POST[$name]);
						case 'cookie':
							return $this->filters($_COOKIE[$name]);
					}
				}
			return $this->filters($ftype,$this->args[$name]);
			}else{
				return (FALSE === $default) ? FALSE : $default;
			}
		}else{
			return $this->args;
		}
	}

	public function set($name, $value)
	{
		$this->args[$name] = $value;
	}

	public function filters($ftype,$value)
	{
		switch ($ftype) {
				case 0://整数
					return (int)$value;
				case 1://字符串
					$value=htmlspecialchars(trim($value));
					if(!get_magic_quotes_gpc())$value = addslashes($value);
					return $value;
				case 2://数组
					if($value=='')return '';
					$a=array();
					foreach($value as $k=>$v){
						$v=trim($v);
						$v=htmlspecialchars($v);
						if(!get_magic_quotes_gpc())$v = addslashes($v);
						$a[$k]=$v;
					}
					return $a;
				case 3://浮点
					return (float)$value;
				case 4:
					return trim($value);
		}
	}

	public function has($name)
	{
		return isset($this->args[$name]);
	}

	public function __input($args = -1)
	{
		if( -1 == $args )return $this;
		@list( $name, $default, $method ) = $args;
		return $this->get($name, $default, $method);
	}

	public function request(){
		return $_SERVER["QUERY_STRING"];
	}
}

