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

/**
 * dump  镙煎纺鍖栬緭鍑哄彉閲忕▼搴?
 * 
 * @param vars    鍙橀噺
 * @param output    鏄惁灏嗗唴瀹硅緭鍑?
 * @param show_trace    鏄惁灏嗕娇鐢╯pError瀵瑰彉閲忚繘琛岃拷韪緭鍑?
 */
function dump($vars, $output = TRUE, $show_trace = FALSE)
{
	if(TRUE != SP_DEBUG && TRUE != $GLOBALS['G_SP']['allow_trace_onrelease'])exit;
	if( TRUE == $show_trace ){
		$content = spError(htmlspecialchars(print_r($vars, true)), TRUE, FALSE);
	}else{
		$content = "<div align=left><pre>\n" . htmlspecialchars(print_r($vars, true)) . "\n</pre></div>\n";
	}
    if(TRUE != $output) { return $content; }
    echo $content;
    return null;
}

/**
 * import  杞藉叆鍖呭惈鏂囦欢
 * 
 * @param filename    闇€瑕佽浇鍏ョ殑鏂囦欢鍚嶆垨钥呮枃浠惰矾寰?
 * @param auto_search    杞藉叆鏂囦欢镓句笉鍒版椂鏄惁鎼灭储绯荤粺璺缎鎴栨枃浠讹紝鎼灭储璺缎镄勯『搴忎负锛氩簲鐢ㄧ▼搴忓寘鍚洰褰?-> 搴旗敤绋嫔簭Model鐩綍 -> sp妗嗘灦鍖呭惈鏂囦欢鐩綍
 * @param auto_error    镊姩鎻愮ず镓╁睍绫昏浇鍏ュ嚭阌欎俊鎭?
 */
function import($sfilename, $auto_search = TRUE, $auto_error = FALSE)
{
	if(isset($GLOBALS['G_SP']["import_file"][md5($sfilename)]))return TRUE;
	if( TRUE == @is_readable($sfilename) ){
		require($sfilename);
		$GLOBALS['G_SP']['import_file'][md5($sfilename)] = TRUE;
		return TRUE;
	}else{
		if(TRUE == $auto_search){
			foreach(array_merge( $GLOBALS['G_SP']['sp_include_path'],
										array($GLOBALS['G_SP']['model_path']), 
										 $GLOBALS['G_SP']['include_path'] ) as $sp_include_path){
				if(isset($GLOBALS['G_SP']["import_file"][md5($sp_include_path.'/'.$sfilename)]))return TRUE;
				if( is_readable( $sp_include_path.'/'.$sfilename ) ){
					require($sp_include_path.'/'.$sfilename);
					$GLOBALS['G_SP']['import_file'][md5($sp_include_path.'/'.$sfilename)] = TRUE;
					return TRUE;
				}
			}
		}
	}
	if( TRUE == $auto_error ){
		spError('链兘镓惧埌鍚崭负锛殁$sfilename}镄勬枃浠?);
	}
	return FALSE;
}

/**
 * spAccess 鏁版嵁缂揿瓨鍙婂瓨鍙栫▼搴?
 * 
 * @param method    鏁版嵁瀛桦彇妯″纺锛屽彇链?w"涓哄瓨鍏ユ暟鎹紝鍙栧€?r"璇诲彇鏁版嵁锛屽彇链?c"涓哄垹闄ゆ暟鎹?
 * @param name    镙囱瘑鏁版嵁镄勫悕绉?
 * @param value    瀛桦叆镄勫€硷紝鍦ㄨ鍙栨暟鎹拰鍒犻櫎鏁版嵁镄勬ā寮忎笅鍧囦负NULL
 * @param life_time    鍙橀噺镄勭敓瀛樻椂闂达紝榛樿涓烘案涔呬缭瀛?
 */
function spAccess($method, $name, $value = NULL, $life_time = -1)
{
	if(!is_dir($GLOBALS['G_SP']['sp_cache']))__mkdirs($GLOBALS['G_SP']['sp_cache']);
	$sfile = $GLOBALS['G_SP']['sp_cache'].'/'.md5($name).".php";
	if('w' == $method){ // 鍐欐暟鎹?
		$life_time = ( -1 == $life_time ) ? '300000000' : $life_time;
		$value = '<?php die();?>'.( time() + $life_time ).serialize($value);
		return file_put_contents($sfile, $value);
	}elseif('c' == $method){ // 娓呴櫎鏁版嵁
		return @unlink($sfile);
	}else{ // 璇绘暟鎹?
		if( !is_readable($sfile) )return FALSE;
		$arg_data = file_get_contents($sfile);
		if( substr($arg_data, 14, 10) < time() ){
			spAccess('c', $name);
			return FALSE;
		}
		return unserialize(substr($arg_data, 24));
	}
}

/**
 * spClass  绫诲疄渚嫔寲鍑芥暟  镊姩杞藉叆绫诲畾涔夋枃浠讹紝瀹炰緥鍖栧苟杩斿洖瀵硅薄鍙ユ焺
 * 
 * @param class_name    绫诲悕绉?
 * @param args   绫诲垵濮嫔寲镞朵娇鐢ㄧ殑鍙傛暟锛屾暟缁勫舰寮?
 * @param sdir 杞藉叆绫诲畾涔夋枃浠剁殑璺缎锛屽彲浠ユ槸鐩綍+鏂囦欢鍚岖殑鏂瑰纺锛屼篃鍙互鍗旷嫭鏄洰褰曘€俿dir镄勫€煎皢浼犲叆import()杩涜杞藉叆
 */
function spClass($class_name, $args = null, $sdir = null)
{
	if(preg_match('/[^a-z0-9\-_.]/i', $class_name))spError($class_name."绫诲悕绉伴敊璇紝璇锋镆ャ€?);
	// 妫€镆ユ槸鍚﹁绫诲凡缁忓疄渚嫔寲锛岀洿鎺ヨ繑锲炲凡瀹炰緥瀵硅薄锛岄伩鍏嶅啀娆″疄渚嫔寲
	if(isset($GLOBALS['G_SP']["inst_class"][$class_name])){
		return $GLOBALS['G_SP']["inst_class"][$class_name];
	}
	// 濡傛灉$sdir涓嶈兘璇诲彇锛屽垯娴嬭瘯鏄惁浠呰矾寰?
	if(null != $sdir && !import($sdir) && !import($sdir.'/'.$class_name.'.php'))return FALSE;

	$has_define = FALSE;
	// 绫诲畾涔夊瓨鍦?
	if(class_exists($class_name, false) || interface_exists($class_name, false)){
		$has_define = TRUE;
	}else{
		if( TRUE == import($class_name.'.php')){
			$has_define = TRUE;
		}
	}
	if(FALSE != $has_define){
		$GLOBALS['G_SP']["inst_class"][$class_name] = new $class_name($args);
		return $GLOBALS['G_SP']["inst_class"][$class_name];
	}
	spError($class_name."绫诲畾涔変笉瀛桦湪锛岃妫€镆ャ€?);
}

/**
 * spError 妗嗘灦瀹氢箟镄勭郴缁熺骇阌栾鎻愮ず
 * 
 * @param msg    鍑洪敊淇℃伅
 * @param output    鏄惁杈揿嚭
 * @param stop    鏄惁锅沧绋嫔簭
 */
function spError($msg, $output = TRUE, $stop = TRUE){
	if(TRUE != SP_DEBUG)exit;
	$traces = debug_backtrace();
	$notice_html = SP_PATH."/Misc";
	$bufferabove = ob_get_clean();
	require($notice_html."/notice.php");
	if(TRUE == $stop)exit;
}
/**
 * spErrorHandler 绯荤粺阌栾鎻愮ず鍑芥暟
 * @param errno    鍑洪敊绫诲瀷
 * @param errstr    阌栾淇℃伅
 * @param errfile    鍑洪敊镄勬枃浠?
 * @param errline    鍑洪敊璇彞琛屽佛
 */
function spErrorHandler($errno, $errstr, $errfile, $errline) {
	if( E_ERROR == $errno || E_PARSE == $errno )spError($errstr);
}

/**
 * spLaunch  镓ц鎸傞潬绋嫔簭
 * 
 * @param configname    鎸傞潬绋嫔簭璁剧疆镣瑰悕绉?
 */
function spLaunch($configname){
	if( isset($GLOBALS['G_SP']['launch'][$configname]) && is_array($GLOBALS['G_SP']['launch'][$configname]) ){
		foreach( $GLOBALS['G_SP']['launch'][$configname] as $launch ){
			if( is_array($launch) ){
				spClass($launch[0])->{$launch[1]}();
			}else{
				call_user_func($launch);
			}
		}
	}
}
/**
 *
 * T
 *
 * 澶氲瑷€瀹炵幇锛岀炕璇戝嚱鏁?
 *
 * @param w    榛樿璇█镄勮瘝璇?
 *
 */
function T($w) {
	$method = $GLOBALS['G_SP']["lang"][spController::getLang()];
	if(!isset($method) || 'default' == $method){
		return $w;
	}elseif( function_exists($method) ){
		return ( $tmp = call_user_func($method, $w) ) ? $tmp : $w;
	}elseif( is_array($method) ){
		return ( $tmp = spClass($method[0])->{$method[1]}($w) ) ? $tmp : $w;
	}elseif( file_exists($method) ){
		$dict = require($method);
		return isset($dict[$w]) ? $dict[$w] : $w;
	}else{
		return $w;
	}
}

/**
 *
 * spUrl
 *
 * URL妯″纺镄勬瀯寤哄嚱鏁?
 *
 * @param controller    鎺у埗鍣ㄥ悕绉帮紝榛樿涓洪厤缃?default_controller'
 * @param action    锷ㄤ綔鍚岖О锛岄粯璁や负閰岖疆'default_action' 
 * @param args    浼犻€掔殑鍙傛暟锛屾暟缁勫舰寮?
 * @param anchor    璺宠浆阌氱偣
 * @param no_sphtml    鏄惁搴旗敤spHtml璁剧疆锛屽湪FALSE镞舵晥鏋滀笌涓嶅惎鐢╯pHtml鐩稿悓銆?
 */
function spUrl($controller = null, $action = null, $args = null, $anchor = null, $no_sphtml = FALSE) {
	if(TRUE == $GLOBALS['G_SP']['html']["enabled"] && TRUE != $no_sphtml){
		// 褰揿紑鍚疕TML鐢熸垚镞讹紝灏嗘煡镓綡TML鍒楄〃銮峰彇闱欐€佹枃浠跺悕绉般€?
		$realhtml = spClass($GLOBALS['G_SP']['html']['url_getter'][0])->{$GLOBALS['G_SP']['html']['url_getter'][1]}($controller, $action, $args, $anchor);
		if(isset($realhtml[0]))return $realhtml[0];
	}
	$controller = ( null != $controller ) ? $controller : $GLOBALS['G_SP']["default_controller"];
	$action = ( null != $action ) ? $action : $GLOBALS['G_SP']["default_action"];
	if( TRUE == $GLOBALS['G_SP']['url']["url_path_info"] ){ // 浣跨敤path_info鏂瑰纺
		$url = $GLOBALS['G_SP']['url']["url_path_base"]."/{$controller}/{$action}";
		if(null != $args)foreach($args as $key => $arg) $url .= "/{$key}/{$arg}";
	}else{
		$url = $GLOBALS['G_SP']['url']["url_path_base"]."?". $GLOBALS['G_SP']["url_controller"]. "={$controller}&";
		$url .= $GLOBALS['G_SP']["url_action"]. "={$action}";
		if(null != $args)foreach($args as $key => $arg) $url .= "&{$key}={$arg}";
	}
	if(null != $anchor) $url .= "#".$anchor;
	return $url;
}


/**
 * __mkdirs
 *
 * 寰幆寤虹珛鐩綍镄勮緟锷╁嚱鏁?
 *
 * @param dir    鐩綍璺缎
 * @param mode    鏂囦欢鏉冮檺
 */
function __mkdirs($dir, $mode = 0777)
{
	if (!is_dir($dir)) {
		__mkdirs(dirname($dir), $mode);
		return @mkdir($dir, $mode);
	}
	return true;
}

/**
 * spExt
 *
 * 镓╁睍绫昏幏鍙栨墿灞曢厤缃殑鍑芥暟
 *
 * @param ext_node_name    镓╁睍閰岖疆鍚?
 */
function spExt($ext_node_name)
{
	return (empty($GLOBALS['G_SP']['ext'][$ext_node_name])) ? FALSE : $GLOBALS['G_SP']['ext'][$ext_node_name];
}

/**
 * spAddViewFunction
 *
 * 灏嗗嚱鏁版敞鍐屽埌妯℃澘鍐呬娇鐢紝璇ュ嚱鏁板彲浠ユ槸瀵硅薄镄勬柟娉曪紝绫荤殑鏂规硶鎴栨槸鍑芥暟銆?
 *
 * @param alias    鍑芥暟鍦ㄦā鏉垮唴镄勫埆鍚?
 * @param callback_function    锲炶皟镄勫嚱鏁版垨鏂规硶
 */
function spAddViewFunction($alias, $callback_function)
{
	return $GLOBALS['G_SP']["view_registered_functions"][$alias] = $callback_function;
}

/**
 * json_decode/json_encode
 *
 * 鍏煎鍦ㄦ湭閰岖疆JSON镓╁睍镄勬儏鍐典笅浣跨敤Services_JSON绫?
 *
 */
if ( !function_exists('json_decode') ){
	function json_decode($content, $assoc=false){
		if ( $assoc ){
			return spClass("Services_JSON",SERVICES_JSON_LOOSE_TYPE)->decode($content);
		} else {
			return spClass("Services_JSON")->decode($content);
		}
	}
}
if ( !function_exists('json_encode') ){
    function json_encode($content){return spClass("Services_JSON")->encode($content);}
}

/**
 * spConfigReady   蹇€熷皢鐢ㄦ埛閰岖疆瑕嗙洊鍒版鏋堕粯璁ら厤缃?
 * 
 * @param preconfig    榛樿閰岖疆
 * @param useconfig    鐢ㄦ埛閰岖疆
 */
function & spConfigReady(&$preconfig, &$useconfig = null){
	$nowconfig = $preconfig;
	if (is_array($useconfig)){
		foreach ($useconfig as $key => $val){
			if (is_array($useconfig[$key])){
				$nowconfig[$key] = is_array($nowconfig[$key]) ? spConfigReady($nowconfig[$key], $useconfig[$key]) : $useconfig[$key];
			}else{
				$nowconfig[$key] = $val;
			}
		}
	}
	return $nowconfig;
}