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

define('SP_VERSION', '2.3.8'); // 褰揿墠妗嗘灦鐗堟湰

/**
 * spCore
 *
 * SpeedPHP搴旗敤妗嗘灦镄勭郴缁熸墽琛岀▼搴?
 */

// 瀹氢箟绯荤粺璺缎
if(!defined('SP_PATH')) define('SP_PATH', dirname(__FILE__).'/SpeedPHP');
if(!defined('APP_PATH')) define('APP_PATH', dirname(__FILE__).'/app');

// 杞藉叆镙稿绩鍑芥暟搴?
require(SP_PATH."/spFunctions.php");
if (substr(PHP_VERSION, 0, 1) != '5')spError("The SpeedPHP Framework Require PHP5!");

// 杞藉叆閰岖疆鏂囦欢
$GLOBALS['G_SP'] = spConfigReady(require(SP_PATH."/spConfig.php"),$spConfig);

// 镙规嵁閰岖疆鏂囦欢杩涜涓€浜涘叏灞€鍙橀噺镄勫畾涔?
if('debug' == $GLOBALS['G_SP']['mode']){
	define("SP_DEBUG",TRUE); // 褰揿墠姝ｅ湪璋冭瘯妯″纺涓?
}else{
	define("SP_DEBUG",FALSE); // 褰揿墠姝ｅ湪閮ㄧ讲妯″纺涓?
}
// 璁剧疆阌栾澶勭悊鍑芥暟
set_error_handler("spErrorHandler");
@set_magic_quotes_runtime(0);

// 镊姩寮€鍚疭ESSION
if($GLOBALS['G_SP']['auto_session'])session_start();

// 杞藉叆镙稿绩MVC鏋舵瀯鏂囦欢
import($GLOBALS['G_SP']["sp_core_path"]."/spController.php", FALSE, TRUE);
import($GLOBALS['G_SP']["sp_core_path"]."/spModel.php", FALSE, TRUE);
import($GLOBALS['G_SP']["sp_core_path"]."/spView.php", FALSE, TRUE);

// 褰揿湪浜岀骇鐩綍涓娇鐢⊿peedPHP妗嗘灦镞讹紝镊姩銮峰彇褰揿墠璁块棶镄勬枃浠跺悕
if("/index.php" == $GLOBALS['G_SP']['url']["url_path_base"]){
	if(basename($_SERVER['SCRIPT_NAME']) === basename($_SERVER['SCRIPT_FILENAME']))
		$GLOBALS['G_SP']['url']["url_path_base"] = $_SERVER['SCRIPT_NAME'];
	elseif (basename($_SERVER['PHP_SELF']) === basename($_SERVER['SCRIPT_FILENAME']))
		$GLOBALS['G_SP']['url']["url_path_base"] = $_SERVER['PHP_SELF'];
	elseif (isset($_SERVER['ORIG_SCRIPT_NAME']) && basename($_SERVER['ORIG_SCRIPT_NAME']) === basename($_SERVER['SCRIPT_FILENAME']))
		$GLOBALS['G_SP']['url']["url_path_base"] = $_SERVER['ORIG_SCRIPT_NAME'];
}

// 鍦ㄤ娇鐢≒ATH_INFO镄勬儏鍐典笅锛屽璺敱杩涜棰勫鐞?
if(TRUE == $GLOBALS['G_SP']['url']["url_path_info"] && !empty($_SERVER['PATH_INFO'])){
	$url_args = explode("/", $_SERVER['PATH_INFO']);$url_sort = array();
	for($u = 1; $u < count($url_args); $u++){
		if($u == 1)$url_sort[$GLOBALS['G_SP']["url_controller"]] = $url_args[$u];
		elseif($u == 2)$url_sort[$GLOBALS['G_SP']["url_action"]] = $url_args[$u];
		else {$url_sort[$url_args[$u]] = isset($url_args[$u+1]) ? $url_args[$u+1] : "";$u+=1;}}
	if("POST" == strtoupper($_SERVER['REQUEST_METHOD'])){$_REQUEST = $_POST =  $_POST + $url_sort;
	}else{$_REQUEST = $_GET = $_GET + $url_sort;}
}

// 鏋勯€犳墽琛岃矾鐢?
$__controller = isset($_REQUEST[$GLOBALS['G_SP']["url_controller"]]) ? 
	$_REQUEST[$GLOBALS['G_SP']["url_controller"]] : 
	$GLOBALS['G_SP']["default_controller"];
$__action = isset($_REQUEST[$GLOBALS['G_SP']["url_action"]]) ? 
	$_REQUEST[$GLOBALS['G_SP']["url_action"]] : 
	$GLOBALS['G_SP']["default_action"];

// 瀵硅矾鐢辫繘琛岃嚜锷ㄦ墽琛岀浉鍏虫搷浣?
spLaunch("router_prefilter");

$handle_controller = spClass($__controller, null, $GLOBALS['G_SP']["controller_path"].'/'.$__controller.".php");
// 璋幂敤鎺у埗鍣ㄥ嚭阌椤皢璋幂敤璺敱阌栾澶勭悊鍑芥暟
if(!is_object($handle_controller) || !method_exists($handle_controller, $__action)){
	eval($GLOBALS['G_SP']["dispatcher_error"]);
	exit;
}

// 镓ц鐢ㄦ埛浠ｇ爜
$handle_controller->$__action();

// 鎺у埗鍣ㄧ▼搴忚繍琛屽畲姣曪紝杩涜妯℃澘镄勮嚜锷ㄨ緭鍑?
if(FALSE != $GLOBALS['G_SP']['view']['auto_display']){
	$__tplname = $GLOBALS['G_SP']['view']['config']['template_dir']."/".
		$__controller.$GLOBALS['G_SP']['view']['auto_display_sep'].
			$__action.$GLOBALS['G_SP']['view']['auto_display_suffix'];
	$handle_controller->v->auto_display($__tplname);
}

