function C($key,$value=null){
static $_config = array();
$args = func_num_args();
if($args == 1){
if(is_string($key)){ //如果傳入的key是字元串
return isset($_config[$key])?$_config[$key]:null;
}
if(is_array($key)){
if(array_keys($key) !== range(0, count($key) - 1)){ //如果傳入的key是關聯數組
$_config = array_merge($_config, $key);
}else{
$ret = array();
foreach ($key as $k) {
$ret[$k] = isset($_config[$k])?$_config[$k]:null;
}
return $ret;
}
}
}else{
if(is_string($key)){
$_config[$key] = $value;
}else{
halt('傳入參數不正确');
}
}
return null;
}
function W($name, $data = array()){
$fullName = $name.'Widget';
if(!class_exists($fullName)){
halt('Widget '.$name.'不存在');
}
$widget = new $fullName();
$widget->invoke($data);
}
function halt($str, $display=false){
Log::fatal($str.' debug_backtrace:'.var_export(debug_backtrace(), true));
header("Content-Type:text/html; charset=utf-8");
if($display){
echo "";
debug_print_backtrace();
echo "";
}
echo $str;
exit;
}
function M(){
$dbConf = C(array('DB_HOST','DB_PORT','DB_USER','DB_PWD','DB_NAME','DB_CHARSET'));
return DB::getInstance($dbConf);
}
function includeIfExist($path){
if(file_exists($path)){
include $path;
}
}
class SinglePHP {
private $c;
private $a;
private static $_instance;
private function __construct($conf){
C($conf);
}
private function __clone(){}
public static function getInstance($conf){
if(!(self::$_instance instanceof self)){
self::$_instance = new self($conf);
}
return self::$_instance;
}
public function run(){
if(C('USE_SESSION') == true){
session_start();
}
C('APP_FULL_PATH', getcwd().'/'.C('APP_PATH').'/');
includeIfExist( C('APP_FULL_PATH').'/common.php');
$pathMod = C('PATH_MOD');
$pathMod = empty($pathMod)?'NORMAL':$pathMod;
spl_autoload_register(array('SinglePHP', 'autoload'));
if(strcmp(strtoupper($pathMod),'NORMAL') === 0 || !isset($_SERVER['PATH_INFO'])){
$this->c = isset($_GET['c'])?$_GET['c']:'Index';
$this->a = isset($_GET['a'])?$_GET['a']:'Index';
}else{
$pathInfo = isset($_SERVER['PATH_INFO'])?$_SERVER['PATH_INFO']:'';
$pathInfoArr = explode('/',trim($pathInfo,'/'));
if(isset($pathInfoArr[0]) && $pathInfoArr[0] !== ''){
$this->c = $pathInfoArr[0];
}else{
$this->c = 'Index';
}
if(isset($pathInfoArr[1])){
$this->a = $pathInfoArr[1];
}else{
$this->a = 'Index';
}
}
if(!class_exists($this->c.'Controller')){
halt('控制器'.$this->c.'不存在');
}
$controllerClass = $this->c.'Controller';
$controller = new $controllerClass();
if(!method_exists($controller, $this->a.'Action')){
halt('方法'.$this->a.'不存在');
}
call_user_func(array($controller,$this->a.'Action'));
}
public static function autoload($class){
if(substr($class,-10)=='Controller'){
includeIfExist(C('APP_FULL_PATH').'/Controller/'.$class.'.class.php');
}elseif(substr($class,-6)=='Widget'){
includeIfExist(C('APP_FULL_PATH').'/Widget/'.$class.'.class.php');
}else{
includeIfExist(C('APP_FULL_PATH').'/Lib/'.$class.'.class.php');
}
}
}
class Controller {
private $_view;
public function __construct(){
$this->_view = new View();
$this->_init();
}
protected function _init(){}
protected function display($tpl=''){
if($tpl === ''){
$trace = debug_backtrace();
$controller = substr($trace[1]['class'], 0, -10);
$action = substr($trace[1]['function'], 0 , -6);
$tpl = $controller . '/' . $action;
}elseif(strpos($tpl, '/') === false){
$trace = debug_backtrace();
$controller = substr($trace[1]['class'], 0, -10);
$tpl = $controller . '/' . $tpl;
}
$this->_view->display($tpl);
}
protected function assign($name,$value){
$this->_view->assign($name,$value);
}
protected function ajaxReturn($data){
echo json_encode($data);
exit;
}
protected function redirect($url){
header("Location: $url");
exit;
}
}
class View {
private $_tplDir;
private $_viewPath;
private $_data = array();
private static $tmpData;
public function __construct($tplDir=''){
if($tplDir == ''){
$this->_tplDir = './'.C('APP_PATH').'/View/';
}else{
$this->_tplDir = $tplDir;
}
}
public function assign($key, $value) {
$this->_data[$key] = $value;
}
public function display($tplFile) {
$this->_viewPath = $this->_tplDir . $tplFile . '.php';
unset($tplFile);
extract($this->_data);
include $this->_viewPath;
}
public static function tplInclude($path, $data=array()){
self::$tmpData = array(
'path' => C('APP_FULL_PATH') . '/View/' . $path . '.php',
'data' => $data,
);
unset($path);
unset($data);
extract(self::$tmpData['data']);
include self::$tmpData['path'];
}
}
class Widget {
protected $_view;
protected $_widgetName;
public function __construct(){
$this->_widgetName = get_class($this);
$dir = C('APP_FULL_PATH') . '/Widget/Tpl/';
$this->_view = new View($dir);
}
public function invoke($data){}
protected function display($tpl=''){
if($tpl == ''){
$tpl = $this->_widgetName;
}
$this->_view->display($tpl);
}
protected function assign($name,$value){
$this->_view->assign($name,$value);
}
}
class DB {
private $_db;
private $_lastSql;
private $_rows;
private $_error;
private static $_instance = array();
private function __construct($dbConf){
if(!isset($dbConf['DB_CHARSET'])){
$dbConf['DB_CHARSET'] = 'utf8';
}
$this->_db = mysql_connect($dbConf['DB_HOST'].':'.$dbConf['DB_PORT'],$dbConf['DB_USER'],$dbConf['DB_PWD']);
if($this->_db === false){
halt(mysql_error());
}
$selectDb = mysql_select_db($dbConf['DB_NAME'],$this->_db);
if($selectDb === false){
halt(mysql_error());
}
mysql_set_charset($dbConf['DB_CHARSET']);
}
private function __clone(){}
static public function getInstance($dbConf){
if(!isset($dbConf['DB_PORT'])){
$dbConf['DB_PORT'] = '3306';
}
$key = $dbConf['DB_HOST'].':'.$dbConf['DB_PORT'];
if(!isset(self::$_instance[$key]) || !(self::$_instance[$key] instanceof self)){
self::$_instance[$key] = new self($dbConf);
}
return self::$_instance[$key];
}
public function escape($str){
return mysql_real_escape_string($str, $this->_db);
}
public function query($sql){
$this->_rows = 0;
$this->_error = '';
$this->_lastSql = $sql;
$this->logSql();
$res = mysql_query($sql,$this->_db);
if($res === false){
$this->_error = mysql_error($this->_db);
$this->logError();
return false;
}else{
$this->_rows = mysql_num_rows($res);
$result = array();
if($this->_rows >0) {
while($row = mysql_fetch_array($res, MYSQL_ASSOC)){
$result[] = $row;
}
mysql_data_seek($res,0);
}
return $result;
}
}
public function execute($sql) {
$this->_rows = 0;
$this->_error = '';
$this->_lastSql = $sql;
$this->logSql();
$result = mysql_query($sql, $this->_db) ;
if ( false === $result) {
$this->_error = mysql_error($this->_db);
$this->logError();
return false;
} else {
$this->_rows = mysql_affected_rows($this->_db);
return $this->_rows;
}
}
public function getRows(){
return $this->_rows;
}
public function getInsertId() {
return mysql_insert_id($this->_db);
}
public function getLastSql(){
return $this->_lastSql;
}
public function getError(){
return $this->_error;
}
private function logSql(){
Log::sql($this->_lastSql);
}
private function logError(){
$str = '[SQL ERR]'.$this->_error.' SQL:'.$this->_lastSql;
Log::warn($str);
}
}
class Log{
public static function write($msg, $level='DEBUG', $wf=false){
if(function_exists('sae_debug')){ //如果是SAE,則使用sae_debug函數打日志
$msg = "[{$level}]".$msg;
sae_set_display_errors(false);
sae_debug(trim($msg));
sae_set_display_errors(true);
}else{
$msg = date('[ Y-m-d H:i:s ]')."[{$level}]".$msg."\r\n";
$logPath = C('APP_FULL_PATH').'/Log/'.date('Ymd').'.log';
if($wf){
$logPath .= '.wf';
}
file_put_contents($logPath, $msg, FILE_APPEND);
}
}
public static function fatal($msg){
self::write($msg, 'FATAL', true);
}
public static function warn($msg){
self::write($msg, 'WARN', true);
}
public static function notice($msg){
self::write($msg, 'NOTICE');
}
public static function debug($msg){
self::write($msg, 'DEBUG');
}
public static function sql($msg){
self::write($msg, 'SQL');
}
}
class ExtException extends Exception{
protected $extra;
public function __construct($message = "", $extra = array(), $code = 0, $previous = null){
$this->extra = $extra;
parent::__construct($message, $code, $previous);
}
public function getExtra(){
return $this->extra;
}
}