File: //web/data/www.tbbprovision.com/lib/Core.php
<?php
/**
* fastphp框架核心
*/
class Core
{
private static $model = 'common';
// 运行程序
public function run()
{
spl_autoload_register(array($this, 'loadClass'));
$this->setReporting();
$this->removeMagicQuotes();
$this->unregisterGlobals();
$this->query_data_filter();
$this->route();
}
// 路由处理
public function route()
{
$path_info = $_SERVER['PATH_INFO'];
$path_info && $path_info = @explode('/',trim($path_info,'/'));
$parame = $_SERVER['QUERY_STRING'];
$query_string = (array)str::strtoparames($parame);
// 获取模块。分为前端和后台
if($path_info[0]==WEB_PATH || $path_info[0]==ADMIN_PATH){
self::$model = $path_info[0];
array_shift($path_info);
}
if($path_info){
$controllerName = ucfirst($path_info[0]);
array_shift($path_info);
}elseif($query_string['mod']){
$controllerName = ucfirst($query_string['mod']);
}else{
$controllerName = 'Index';
}
$controller = ucfirst($controllerName) . 'Controller';
if(!class_exists($controller)){
View::page_404();
}
if($path_info){
$action = str_replace('.html','',$path_info[0]);//将act的后缀去掉
array_shift($path_info);
}elseif($query_string['act']){
$action = $query_string['act'];
}else{
$action = 'index';
}
$action .= '_action';
$controller_main = new $controller($controllerName,$action,self::$model);
if(!method_exists($controller_main,$action)){
View::page_404();
}
// 实例化控制器
//$dispatch = new $controller($controllerName, $action,self::$model);
// 如果控制器存和动作存在,这调用并传入URL参数
if ((int)method_exists($controller, $action)) {
call_user_func_array(array($controller_main, $action),array(($path_info[0]?str_replace('.html','',$path_info[0]):$parame)));
} else {
View::page_404();
//exit($controller. '::'.$action. "控制器不存在");
}
}
// 检测开发环境
public function setReporting()
{
header('Content-Type: text/html; charset=utf-8');
date_default_timezone_set('PRC');
if (APP_DEBUG === true) {
error_reporting(E_ALL);
ini_set('display_errors','On');
/*@error_reporting(E_ALL ^ E_NOTICE ^ E_DEPRECATED);
//@ini_set('session.cookie_domain','www.bspect.com');
phpversion()<'5.3.0' && set_magic_quotes_runtime(0);
//5.1.0*/
} else {
error_reporting(E_ALL);
ini_set('display_errors','Off');
// ini_set('log_errors', 'On');
// ini_set('error_log', RUNTIME_PATH. 'logs/error.log');
}
//开户session
//@ini_set('session.cookie_domain','tbbcompany.com');
session_start();
isset($_GET['session_id']) && @session_id($_GET['session_id']);
}
// 删除敏感字符
public function stripSlashesDeep($value)
{
$value = is_array($value) ? array_map(array($this, 'stripSlashesDeep'), $value) : stripslashes($value);
return $value;
}
// 检测敏感字符并删除
public function removeMagicQuotes()
{
if (get_magic_quotes_gpc()) {
$_GET = isset($_GET) ? $this->stripSlashesDeep($_GET ) : '';
$_POST = isset($_POST) ? $this->stripSlashesDeep($_POST ) : '';
$_COOKIE = isset($_COOKIE) ? $this->stripSlashesDeep($_COOKIE) : '';
$_SESSION = isset($_SESSION) ? $this->stripSlashesDeep($_SESSION) : '';
}
}
// 检测自定义全局变量(register globals)并移除
public function unregisterGlobals()
{
if (ini_get('register_globals')) {
$array = array('_SESSION', '_POST', '_GET', '_COOKIE', '_REQUEST', '_SERVER', '_ENV', '_FILES');
foreach ($array as $value) {
foreach ($GLOBALS[$value] as $key => $var) {
if ($var === $GLOBALS[$key]) {
unset($GLOBALS[$key]);
}
}
}
}
}
// 自动加载控制器和模型类
public static function loadClass($class)
{
$url = isset($_GET['url']) ? $_GET['url'] : false;
$frameworks = FRAME_PATH . $class . '.class.php';
$status_class = FRAME_PATH . '/class/' . $class . '.class.php';
$controllers = APP_PATH . 'application/'.(self::$model).'/controllers/' . $class . '.class.php';
$models = APP_PATH . 'application/'.(self::$model).'/models/' . $class . '.class.php';
if (file_exists($frameworks)) {
// 加载框架核心类
include $frameworks;
}elseif(file_exists($status_class)){
//常用用类
include $status_class;
} elseif (file_exists($controllers)) {
// 加载应用控制器类
include $controllers;
} elseif (file_exists($models)) {
//加载应用模型类
include $models;
} else {
// 错误代码
}
}
/***
* 数据过滤
*/
private function query_data_filter(){
$this->slashes_gpcf($_GET);
$this->slashes_gpcf($_POST);
$this->slashes_gpcf($_COOKIE);
$this->slashes_gpcf($_FILES);
$this->slashes_gpcf($_REQUEST);
}
/**
* 请求数据处理
*/
private function slashes_gpcf(&$ary){
foreach($ary as $k=>$v){
if(is_array($v)){
self::slashes_gpcf($ary[$k]);
}else{
$ary[$k]=trim($ary[$k]);
!get_magic_quotes_gpc() && $ary[$k]=addslashes($ary[$k]);
}
}
}
}