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

/**
 * spController 鍩虹鎺у埗鍣ㄧ▼搴忕埗绫?搴旗敤绋嫔簭涓殑姣忎釜鎺у埗鍣ㄧ▼搴忛兘搴旗户镓夸簬spController
 */
class spController { 

	/**
	 * 瑙嗗浘瀵硅薄
	 */
	public $v;
	
	/**
	 * 鏋勯€犲嚱鏁?
	 */
	public function __construct()
	{	
		if(TRUE == $GLOBALS['G_SP']['view']['enabled']){
			$this->v = spClass('spView');
		}
	}
    /**
     *
     * 璺宠浆绋嫔簭
     *
     * 搴旗敤绋嫔簭镄勬带鍒跺櫒绫诲彲浠ヨ鐩栬鍑芥暟浠ヤ娇鐢ㄨ嚜瀹氢箟镄勮烦杞▼搴?
     *
     * @param $url  闇€瑕佸墠寰€镄勫湴鍧€
     * @param $delay   寤惰繜镞堕棿
     */
    public function jump($url, $delay = 0){
		echo "<html><head><meta http-equiv='refresh' content='{$delay};url={$url}'></head><body></body></html>";
		exit;
    }

    /**
     *
     * 阌栾鎻愮ず绋嫔簭
     *
     * 搴旗敤绋嫔簭镄勬带鍒跺櫒绫诲彲浠ヨ鐩栬鍑芥暟浠ヤ娇鐢ㄨ嚜瀹氢箟镄勯敊璇彁绀?
     *
     * @param $msg   阌栾鎻愮ず闇€瑕佺殑鐩稿叧淇℃伅
     * @param $url   璺宠浆鍦板潃
     */
    public function error($msg, $url){
		echo "<html><head><meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\"><script>function sptips(){alert(\"{$msg}\");location.href=\"{$url}\";}</script></head><body onload=\"sptips()\"></body></html>";
		exit;
    }

    /**
     *
     * 鎴愬姛鎻愮ず绋嫔簭
     *
     * 搴旗敤绋嫔簭镄勬带鍒跺櫒绫诲彲浠ヨ鐩栬鍑芥暟浠ヤ娇鐢ㄨ嚜瀹氢箟镄勬垚锷熸彁绀?
	 *
     * @param $msg   鎴愬姛鎻愮ず闇€瑕佺殑鐩稿叧淇℃伅
     * @param $url   璺宠浆鍦板潃
     */
    public function success($msg, $url){
		echo "<html><head><meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\"><script>function sptips(){alert(\"{$msg}\");location.href=\"{$url}\";}</script></head><body onload=\"sptips()\"></body></html>";
		exit;
    }

	/**
	 * 榄旀湳鍑芥暟锛岃幏鍙栬祴链间綔涓烘ā鏉垮唴鍙橀噺
	 */
	public function __set($name, $value)
	{
		if(TRUE == $GLOBALS['G_SP']['view']['enabled']){
			$this->v->getView()->assign(array($name=>$value));
		}
	}
	
	/**
	 * 杈揿嚭妯℃澘
	 *
     * @param $tplname   妯℃澘璺缎鍙婂悕绉?
     * @param $output   鏄惁鐩存帴鏄剧ず妯℃澘锛岃缃垚FALSE灏呜繑锲潍TML钥屼笉杈揿嚭
	 */
	public function display($tplname, $output = TRUE)
	{
		if(TRUE == $GLOBALS['G_SP']['view']['enabled']){
			$this->v->display($tplname, $output);
		}else{
			require($tplname);
		}
		if( TRUE != $output )return ob_get_clean();
	}

	/**
	 * 榄旀湳鍑芥暟锛屽疄鐜板鎺у埗鍣ㄦ墿灞旷被镄勮嚜锷ㄥ姞杞?
	 */
	public function __call($name, $args)
	{
		if(in_array($name, $GLOBALS['G_SP']["auto_load_controller"])){
			return spClass($name)->__input($args);
		}elseif(!method_exists( $this, $name )){
			spError("method {$name} not defined");
		}
	}

	/**
	 * 銮峰彇瑙嗗浘瀵硅薄
	 */
	public function getView()
	{
		return $this->v->getView();
	}
	/**
	 * 璁剧疆褰揿墠鐢ㄦ埛镄勮瑷€
     * @param $lang   璇█镙囱瘑
	 */
	public function setLang($lang)
	{
		if( array_key_exists($lang, $GLOBALS['G_SP']["lang"]) ){
			@ob_start();
			$domain = ('www.' == substr($_SERVER["HTTP_HOST"],0,4)) ? substr($_SERVER["HTTP_HOST"],4) : $_SERVER["HTTP_HOST"];
			setcookie("SpLangCookies", $lang, time()+31536000, '/', $domain ); // 涓€骞磋绷链?
			$_SESSION["SpLangSession"] = $lang;
			return TRUE;
		}
		return FALSE;
	}
	/**
	 * 銮峰彇褰揿墠鐢ㄦ埛镄勮瑷€
	 */
	public function getLang()
	{
		if( !isset($_COOKIE['SpLangCookies']) )return $_SESSION["SpLangSession"];
		return $_COOKIE['SpLangCookies'];
	}
}

/**
 * spArgs 
 * 搴旗敤绋嫔簭鍙橀噺绫?
 * spArgs鏄皝瑁呬简$_SESSION銆?_GET/$_POST銆?_COOKIE銆?_SERVER銆?_FILES銆?_ENV绛夛紝鎻愪緵涓€浜涚亩渚跨殑璁块棶鍜屼娇鐢ㄨ繖浜?
 * 鍏ㄥ眬鍙橀噺镄勬柟娉曘€?
 */

class spArgs {
	/**
	 * 鍦ㄥ唴瀛树腑淇濆瓨镄勫彉閲?
	 */
	private $args = null;

	/**
	 * 鏋勯€犲嚱鏁?
	 *
	 */
	public function __construct(){
		$this->args = $_REQUEST;
	}
	
	/**
	 * 銮峰彇搴旗敤绋嫔簭璇锋眰鍙橀噺链硷紝鍚屾椂涔熷彲浠ユ寚瀹氲幏鍙栫殑鍙橀噺镓€灞炪€?
	 * 
	 * @param name    銮峰彇镄勫彉閲忓悕绉帮紝濡傛灉涓虹┖锛屽垯杩斿洖鍏ㄩ儴镄勮姹傚彉閲?
	 * @param default    褰揿墠銮峰彇镄勫彉閲忎笉瀛桦湪镄勬椂链欙紝灏呜繑锲炵殑榛樿链?
	 * @param method    銮峰彇浣岖疆锛屽彇链糋ET锛孭OST锛孋OOKIE
	 */
	public function get($name = null, $default = null, $method = null)
	{
		if(null != $name){
			if( $this->has($name) ){
				if( null != $method ){
					switch (strtolower($method)) {
						case 'get':
							return $_GET[$name];
						case 'post':
							return $_POST[$name];
						case 'cookie':
							return $_COOKIE[$name];
					}
				}
				return $this->args[$name];
			}else{
				return (null === $default) ? FALSE : $default;
			}
		}else{
			return $this->args;
		}
	}

	/**
	 * 璁剧疆锛埚锷狅级鐜鍙橀噺链硷紝璇ュ悕绉板皢瑕嗙洊铡熸潵镄勭幆澧冨彉閲忓悕绉?
	 * 
	 * @param name    鐜鍙橀噺鍚岖О
	 * @param value    鐜鍙橀噺链?
	 */
	public function set($name, $value)
	{
		$this->args[$name] = $value;
	}

	/**
	 * 妫€娴嬫槸鍚﹀瓨鍦ㄦ煇链?
	 * 
	 * @param name    寰呮娴嬬殑鐜鍙橀噺鍚岖О
	 */
	public function has($name)
	{
		return isset($this->args[$name]) && !empty($this->args[$name]);
	}

	/**
	 * 鏋勯€犺緭鍏ュ嚱鏁帮紝镙囧嗳鐢ㄦ硶
	 * @param args    鐜鍙橀噺鍚岖О镄勫弬鏁?
	 */
	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"];
	}
}

