<?php
/////////////////////////////////////////////////////////////////////////////
//
// SpeedPHP - 蹇€熺殑涓枃PHP妗嗘灦
//
// Copyright (c) 2008 - 2009 SpeedPHP.com All rights reserved.
//
// 璁稿彲鍗忚璇锋煡鐪?http://www.speedphp.com/
//
/////////////////////////////////////////////////////////////////////////////

/**
 * spView 鍩虹瑙嗗浘绫?
 */
class spView {
	/**
	 * smarty瀹炰緥
	 */
	private $smarty = null;
	/**
	 * 妯℃澘鏄惁宸茶緭鍑?
	 */
	private $displayed = FALSE;

	/**
	 * 鏋勯€犲嚱鏁帮紝杩涜Smarty绫荤殑瀹炰緥鍖栨搷浣?
	 */
	public function __construct()
	{
		if(FALSE == $GLOBALS['G_SP']['view']['enabled'])return FALSE;
		if(FALSE != $GLOBALS['G_SP']['view']['auto_ob_start'])ob_start();
		$this->smarty = spClass($GLOBALS['G_SP']['view']['engine_name'],null,$GLOBALS['G_SP']['view']['engine_path']);
		$configs = $GLOBALS['G_SP']['view']['config'];
		if( is_array($configs) ){
			foreach( $configs as $key => $value ){
				if( isset($this->smarty->{$key}) )$this->smarty->{$key} = $value;
			}
		}
		spAddViewFunction('T', array( $this, '__smarty_T'));
		spAddViewFunction('spUrl', array( $this, '__smarty_spUrl'));
	}

	/**
	 * 杈揿嚭椤甸溃
	 * @param tplname 妯℃澘鏂囦欢璺缎
	 */
	public function display($tplname)
	{
		$this->addfuncs();
		$this->displayed = TRUE;
		if($GLOBALS['G_SP']['view']['debugging'] && SP_DEBUG)$this->smarty->debugging = TRUE;
		$this->smarty->display($tplname);
	}

	/**
	 * 銮峰彇Smarty镄勫疄渚?
	 */	
	public function getView()
	{
		$this->addfuncs();
		return $this->smarty;
	}
	/**
	 * 镊姩杈揿嚭椤甸溃
	 * @param tplname 妯℃澘鏂囦欢璺缎
	 */
	public function auto_display($tplname)
	{
		if( TRUE != $this->displayed && 
			FALSE != $GLOBALS['G_SP']['view']['auto_display'] &&
			TRUE == $this->smarty->template_exists($tplname)){
			$this->display($tplname);
		}
	}
	
	/**
	 * 娉ㄥ唽宸叉寕闱犵殑瑙嗗浘鍑芥暟
	 */
	public function addfuncs()
	{
		if( is_array($GLOBALS['G_SP']["view_registered_functions"]) ){
			foreach( $GLOBALS['G_SP']["view_registered_functions"] as $alias => $func )
			{
				$this->smarty->register_function($alias, $func);
			}
		}
	}
	/**
	 * 杈呭侄spUrl镄勫嚱鏁帮紝璁﹕pUrl鍙湪妯℃澘涓娇鐢ㄣ€?
	 * @param params 浼犲叆镄勫弬鏁?
	 */
	public function __smarty_spUrl($params)
	{
		$controller = $GLOBALS['G_SP']["default_controller"];
		$action = $GLOBALS['G_SP']["default_action"];
		$args = array();
		$anchor = null;
		foreach($params as $key => $param){
			if( $key == $GLOBALS['G_SP']["url_controller"] ){
				$controller = $param;
			}elseif( $key == $GLOBALS['G_SP']["url_action"] ){
				$action = $param;
			}elseif( $key == 'anchor' ){
				$anchor = $param;
			}else{
				$args[$key] = $param;
			}
		}
		return spUrl($controller, $action, $args, $anchor);
	}
	/**
	 * 杈呭侄T镄勫嚱鏁帮紝璁㏕鍙湪妯℃澘涓娇鐢ㄣ€?
	 * @param params 浼犲叆镄勫弬鏁?
	 */
	public function __smarty_T($params)
	{
		return T($params['w']);
	}
}

/**
 * spHtml
 * 闱欐€丠TML鐢熸垚绫?
 */
class spHtml
{
	/**
	 * 鐢熸垚鍗曚釜闱欐€侀〉闱?
	 * 
	 * @param spurl spUrl镄勫弬鏁?
	 * @param alias_url 鐢熸垚HTML鏂囦欢镄勫悕绉帮紝濡傛灉涓嶈缃产lias_url锛屽皢浣跨敤骞存湀镞ョ敓鎴愮洰褰曞强闅忔満鏁颁负鏂囦欢鍚岖殑褰㈠纺鐢熸垚HTML鏂囦欢銆?
	 * @param update_mode    镟存柊妯″纺锛岄粯璁?涓哄悓镞舵洿鏂板垪琛ㄥ强鏂囦欢
	 * 0鏄粎镟存柊鍒楄〃
	 * 1鏄粎镟存柊鏂囦欢
	 */
	public function make($spurl, $alias_url = null, $update_mode = 2)
	{
		$spurl = array_pad($spurl, 4, null);$spurl[] = TRUE;
		if( '*' == $spurl[1] or '*' == $spurl[2] )return FALSE;
		if( $url_item = call_user_func_array($GLOBALS['G_SP']['html']['url_getter'],$spurl) ){
			list($baseuri, $realfile) = $url_item;
		}else{
			if( null == $alias_url ){
				$filedir = $GLOBALS['G_SP']['html']['file_root_name'].'/'.date('Y/n/d').'/';
				$filename = substr(time(),3,10).substr(mt_rand(100000, substr(time(),3,10)),4).".html";
			}else{
				$filedir = $GLOBALS['G_SP']['html']['file_root_name'].'/'.dirname($alias_url) . '/';
				$filename = basename($alias_url);
			}
			$baseuri = rtrim(dirname($GLOBALS['G_SP']['url']["url_path_base"]), '/\\')."/".$filedir.$filename;
			$realfile = dirname($_SERVER['SCRIPT_FILENAME'])."/".$filedir.$filename;
		}
		if( 0 == $update_mode or 2 == $update_mode )
			call_user_func_array($GLOBALS['G_SP']['html']['url_setter'], array($spurl, $baseuri, $realfile));
		if( 1 == $update_mode or 2 == $update_mode ){
			__mkdirs(dirname($realfile));
			$cachedata = @file_get_contents('http://'.$_SERVER["SERVER_NAME"].call_user_func_array("spUrl",$spurl));
			@file_put_contents($realfile, $cachedata);
		}
	}
	
	/**
	 * 镓归噺鐢熸垚闱欐€侀〉闱?
	 * @param spurls 鏁扮粍褰㈠纺锛屾疮椤规槸涓€涓猰ake()镄勫叏閮ㄥ弬鏁?
	 */
	public function makeAll($spurls)
	{
		foreach( $spurls as $single ){
			list($spurl, $alias_url) = $single;
			$this->make($spurl, $alias_url, 0);
		}
		foreach( $spurls as $single ){
			list($spurl, $alias_url) = $single;
			$this->make($spurl, $alias_url, 1);
		}
	}	

	/**
	 * 銮峰彇url镄勫垪琛ㄧ▼搴忥紝鍙互鎸夐厤缃紑鍚槸鍚︽镆ユ枃浠跺瓨鍦?
	 * @param controller    鎺у埗鍣ㄥ悕绉帮紝榛樿涓洪厤缃?default_controller'
	 * @param action    锷ㄤ綔鍚岖О锛岄粯璁や负閰岖疆'default_action' 
	 * @param args    浼犻€掔殑鍙傛暟锛屾暟缁勫舰寮?
	 * @param anchor    璺宠浆阌氱偣
	 * @param force_no_check    鏄惁妫€镆ョ墿鐞嗘枃浠舵槸鍚﹀瓨鍦?
	 * 
	 */
	public function getUrl($controller = null, $action = null, $args = null, $anchor = null, $force_no_check = FALSE)
	{
		if( $url_list = spAccess('r', 'sp_url_list') ){
			$url_list = explode("\n",$url_list);
			$args = (is_array($args) && !empty($args) ) ? serialize($args) : null;
			$url_input = "{$controller}|{$action}|{$args}|$anchor|";
			foreach( $url_list as $url ){
				if( substr($url,0,strlen($url_input)) == $url_input )
				{
					$url_item = explode("|",substr($url,strlen($url_input)));
					if( TRUE == $GLOBALS['G_SP']['html']['safe_check_file_exists'] && TRUE != $force_no_check ){
						if( !is_readable($url_item[1]) )return FALSE;
					}
					return $url_item;
				}
			}
		}
		return FALSE;
	}
	
	/**
	 * 鍐椤叆url镄勫垪琛ㄧ▼搴忥紝鍦╩ake鐢熸垚椤甸溃鍚庯紝灏唖pUrl鍙傛暟鍙婇〉闱㈠湴鍧€鍐椤叆鍒楄〃涓?
	 *
	 * @param spurl spUrl镄勫弬鏁?
	 * @param baseuri URL鍦板潃瀵瑰簲镄勯润镐丠TML鏂囦欢璁块棶鍦板潃
     *
	 */
	public function setUrl($spurl, $baseuri, $realfile)
	{
		@list($controller, $action, $args, $anchor) = $spurl;
		$args = (is_array($args) && !empty($args)) ? serialize($args) : null;
		$url_input = "{$controller}|{$action}|{$args}|$anchor|$baseuri|$realfile";
		$this->clear($controller, $action, $args, $anchor, FALSE);
		if( $url_list = spAccess('r', 'sp_url_list') ){
			spAccess('w', 'sp_url_list', $url_list."\n".$url_input);
		}else{
			spAccess('w', 'sp_url_list', $url_input);
		}
	}

	/**
	 * 娓呴櫎闱欐€佹枃浠?
	 * 
	 * @param controller    闇€瑕佹竻闄TML鏂囦欢镄勬带鍒跺櫒鍚岖О
	 * @param action    闇€瑕佹竻闄TML鏂囦欢镄勫姩浣滃悕绉帮紝榛樿涓烘竻闄よ鎺у埗鍣ㄥ叏閮ㄥ姩浣滀骇鐢熺殑HTML鏂囦欢
	 * 濡傛灉璁剧疆浜哸ction灏嗕粎娓呴櫎璇ction浜х敓镄凥TML鏂囦欢
	 *
	 * @param args    浼犻€掔殑鍙傛暟锛岄粯璁や负绌哄皢娓呴櫎璇ュ姩浣滀换浣曞弬鏁颁骇鐢熺殑HTML鏂囦欢
	 * 濡傛灉璁剧疆浜哸rgs灏嗕粎娓呴櫎璇ュ姩浣沧墽琛屽弬鏁瘾rgs钥屼骇鐢熺殑HTML鏂囦欢
	 *
	 * @param anchor    璺宠浆阌氱偣锛岄粯璁や负绌哄皢娓呴櫎璇ュ姩浣滀换浣曢敋镣逛骇鐢熺殑HTML鏂囦欢
	 * 濡傛灉璁剧疆浜哸nchor灏嗕粎娓呴櫎璇ュ姩浣滆烦杞埌阌氱偣anchor浜х敓镄凥TML鏂囦欢
	 *
	 * @param delete_file    鏄惁鍒犻櫎鐗╃悊鏂囦欢锛孎ALSH灏嗗彧鍒犻櫎鍒楄〃涓闱欐€佹枃浠剁殑鍦板潃锛岃€屼笉鍒犻櫎鐗╃悊鏂囦欢銆?
	 */
	public function clear($controller, $action = null, $args = null, $anchor = null, $delete_file = TRUE)
	{
		if( $url_list = spAccess('r', 'sp_url_list') ){
			$url_list = explode("\n",$url_list);$re_url_list = array();
			if( '*' == $action ){
				$url_input = "{$controller}|";
			}elseif( '*' == $args ){
				$url_input = "{$controller}|{$action}|";
			}else{
				$url_input = "{$controller}|{$action}|{$args}|$anchor|";
			}
			foreach( $url_list as $url ){
				if( substr($url,0,strlen($url_input)) == $url_input )
				{
					$url_tmp = explode("|",$url);$baseuri = $url_tmp[4];
					$realfile = dirname($_SERVER["SCRIPT_FILENAME"]).'/'.$baseuri;
					if( TRUE == $delete_file )@unlink($realfile);
				}else{
					$re_url_list[] = $url;
				}
			}
			spAccess('w', 'sp_url_list', join("\n", $re_url_list));
		}
	}
	

	/**
	 * 娓呴櫎鍏ㄩ儴闱欐€佹枃浠?
	 * 
	 * @param delete_file    鏄惁鍒犻櫎鐗╃悊鏂囦欢锛孎ALSH灏嗗彧鍒犻櫎鍒楄〃涓闱欐€佹枃浠剁殑鍦板潃锛岃€屼笉鍒犻櫎鐗╃悊鏂囦欢銆?
	 */
	public function clearAll($delete_file = FALSE)
	{
		if( TRUE == $delete_file ){
			if( $url_list = spAccess('r', 'sp_url_list') ){
				$url_list = explode("\n",$url_list);
				foreach( $url_list as $url ){
					$url_tmp = explode("|",$url);$baseuri = $url_tmp[4];
					$realfile = dirname($_SERVER["SCRIPT_FILENAME"]).'/'.$baseuri;
					@unlink($realfile);
				}
			}
		}
		spAccess('c', 'sp_url_list');
	}
}