$endrun=false;
define("CONTROLLERS_DIR",dirname(__FILE__).'/controllers/');
define("VIEWS_DIR",dirname(__FILE__).'/views/');
define("MODELS_DIR",dirname(__FILE__).'/models/');
define("CONFIG_DIR",dirname(__FILE__).'/config/');
define("CLASSS_DIR",dirname(__FILE__).'/classs/');
define("CONFIG_FILE",'config.php');
if(file_exists(CONFIG_DIR.CONFIG_FILE))
{
require(CONFIG_DIR.CONFIG_FILE);
}
function inti()
{
set_error_handler('error_handler');
register_shutdown_function('shutdown_func');
if(version_compare(PHP_VERSION,'5.4.0','
{
ini_set('magic_quotes_runtime',0);
}
else
{
define('MAGIC_QUOTES_GPC',false);
}
if(get_config('charset')!='error')
{
ini_set('default_charset',get_config('charset'));
}
if(get_config('appname')!='error')
{
header('X-Powered-By:'.get_config('appname'));
}
if(get_config('timezone')!='error')
{
ini_set('date.timezone',get_config('timezone'));
}
if(get_config('debug')!='error')
{
if(get_config('debug')==false)
{
ini_set("display_errors", "off");
}
if(get_config('debug')==true)
{
ini_set("display_errors", "on");
}
}
}
function get_query()
{
$request_url=$_SERVER['REQUEST_URI'];
$a=explode('/',$request_url);
$length=count($a);
if($length<4)
{
return false;
}
else
{
$s=$a[3];
if(strpos($s,'.html')===false)
{
return $s;
}
else
{
return str_replace('.html','',$s);
}
}
}
function get_config($key)
{
if(isset($GLOBALS['config']))
{
if(is_array($GLOBALS['config']))
{
if(array_key_exists($key,$GLOBALS['config']))
{
return $GLOBALS['config'][$key];
}
}
}
return 'error';
}
function is_method($method)
{
$m=$_SERVER['REQUEST_METHOD'];
if(strtoupper($method)==$m)
{
return true;
}
return false;
}
//页面跳转
function redirect($url)
{
header('Location:'.$url);
exit();
}
//载入model
function model($name)
{
$name.='_model';
if(file_exists(MODELS_DIR.$name.'.php'))
{
require_once(MODELS_DIR.$name.'.php');
if(class_exists($name))
{
$c=new $name;
return $c;
}
}
return false;
}
//载入class
function load_class($name)
{
if(file_exists(CLASSS_DIR.$name.'.php'))
{
include(CLASSS_DIR.$name.'.php');
}
}
function error_handler($errno,$errstr,$errfile,$errline)
{
if (!(error_reporting() & $errno))
{
return;
}
echo 'something is wrong!';
}
function shutdown_func()
{
if(!$GLOBALS['endrun'])
{
echo("500 something is wrong!");
}
}
function map()
{
$request_url=$_SERVER['REQUEST_URI'];
if($request_url=='/')
{
instance('index','index');
}
else
{
$a=explode('/',$request_url);
$length=count($a);
$c=$a[1];
if($length>=3)
{
$m=$a[2];
if($m=="")
{
instance($c,$c);
}
else
{
instance($c,$m);
}
}
else
{
instance($c,$c);
}
}
$GLOBALS['endrun']=true;
}
function instance($c,$m)
{
$c.='_controller';
$m.='_action';
if(file_exists(CONTROLLERS_DIR.$c.'.php'))
{
require_once(CONTROLLERS_DIR.$c.'.php');
if(class_exists($c))
{
if(method_exists($c,$m))
{
$r=new $c;
if($c!=$m)
{
$r->$m();
}
}
else
{
echo '404 not found';
}
}
else
{
echo '404 not found';
}
}
else
{
echo '404 not found';
}
}
function json($arr)
{
header('content-type:application/json; charset=utf-8');
if(is_array($arr))
{
echo json_encode($arr);
return;
}
echo json_decode(array('error'=>'parameter error'));
}
function text($str)
{
header('content-type:text/plain; charset=utf-8');
echo $str;
}
function render($view,$data = null)
{
$path=VIEWS_DIR.$view.'.php';
if(file_exists($path))
{
header('content-type:text/html; charset=utf-8');
ob_start();
if(is_array($data))
{
extract($data);
}
require $path;
echo trim(ob_get_clean());
}
else
{
echo 'template not found';
}
}
function run()
{
inti();
map();
}
run();
?>