123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883 |
- <?php
- if (!defined('IS_INITPHP')) exit('Access Denied!');
- function C($key,$value=null){
- static $_config = array();
- $args = func_num_args();
- if($args == 1){
- if(is_string($key)){
- return isset($_config[$key])?$_config[$key]:null;
- }
- if(is_array($key)){
- if(array_keys($key) !== range(0, count($key) - 1)){
- $_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 I($name,$default='',$filter=null) {
- if(strpos($name,'.')) {
-
- list($method,$name) = explode('.',$name,2);
-
- }else{
-
- $method = 'param';
- }
- switch(strtolower($method)) {
-
- case 'get' : $input =& $_GET;break;
-
- case 'post' : $input =& $_POST;break;
-
- case 'put' : parse_str(file_get_contents('php://input'), $input);break;
-
- case 'param' :
-
- switch($_SERVER['REQUEST_METHOD']) {
-
- case 'POST':
- $input = $_POST;
- break;
-
- case 'PUT':
- parse_str(file_get_contents('php://input'), $input);
- break;
-
- default:
- $input = $_GET;
- }
- break;
-
- case 'request' : $input =& $_REQUEST; break;
-
- case 'session' : $input =& $_SESSION; break;
-
- case 'cookie' : $input =& $_COOKIE; break;
-
- case 'server' : $input =& $_SERVER; break;
-
- case 'globals' : $input =& $GLOBALS; break;
-
- default:
- return NULL;
- }
-
-
- if(empty($name)) {
-
- $data = $input;
-
-
-
- array_walk_recursive($data,'filter_exp');
-
- $filters = isset($filter)?$filter:C('DEFAULT_FILTER');
- if($filters) {
- $filters = explode(',',$filters);
-
- foreach($filters as $filter){
-
- $data = array_map_recursive($filter,$data);
- }
- }
- }elseif(isset($input[$name])) {
- $data = $input[$name];
- is_array($data) && array_walk_recursive($data,'filter_exp');
- $filters = isset($filter)?$filter:C('DEFAULT_FILTER');
- if($filters) {
- $filters = explode(',',$filters);
- foreach($filters as $filter){
- if(function_exists($filter)) {
- $data = is_array($data)?array_map_recursive($filter,$data):$filter($data);
- }else{
- $data = filter_var($data,is_int($filter)?$filter:filter_id($filter));
- if(false === $data) {
- return isset($default)?$default:NULL;
- }
- }
- }
- }
- }else{
- $data = isset($default)?$default:NULL;
- }
-
-
- return $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 "<pre>";
- debug_print_backtrace();
- echo "</pre>";
- }
- 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').'/');
- $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';
- }
- }
-
- $controllerClass = $this->c.'Controller';
- $controller = new $controllerClass();
-
-
-
-
- if(!class_exists($this->c.'Controller')){
-
-
- halt('jsonp1({"error":"非法访问"})');
- }
-
- if($this->c == "index" || $this->c == "Index" || $this->c == "reqhis" || $this->c == "ReqHis" || $this->c == "other" || $this->c == "Other" ){
- call_user_func(array($controller,'BaseAction'),$this->a);
- }else{
- 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 executeinsert($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_insert_id($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')){
- $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){
- $msg = iconv("utf-8","gb2312",$msg);
- self::write($msg, 'FATAL', true);
- }
-
- public static function warn($msg){
- $msg = iconv("utf-8","gb2312",$msg);
- self::write($msg, 'WARN', true);
- }
-
- public static function notice($msg){
- $msg = iconv("utf-8","gb2312",$msg);
- self::write($msg, 'NOTICE');
- }
-
-
- public static function gethis($msg){
- $msg = iconv("utf-8","gb2312",$msg);
- self::write($msg, 'GETHIS');
- }
-
-
-
- public static function posthis($msg){
- $msg = iconv("utf-8","gb2312",$msg);
- self::write($msg, 'POSTHIS');
- }
-
-
-
- public static function xmlerr($msg){
- self::write($msg, 'XMLERR');
- }
-
- public static function xmltc($msg){
- self::write($msg, 'XMLTC');
- }
-
-
-
- public static function syncdberr($msg){
- self::write($msg, 'SYNCDBERR');
- }
-
-
-
- public static function cur_err($msg){
- self::write($msg, 'CURERR');
- }
-
-
- public static function json($msg){
- $msg = iconv("utf-8","gb2312",$msg);
- self::write($msg, 'JSON');
- }
-
-
- public static function debug($msg){
- $msg = iconv("utf-8","gb2312",$msg);
- self::write($msg, 'DEBUG');
- }
-
- public static function sql($msg){
- $msg = iconv("utf-8","gb2312",$msg);
- self::write($msg, 'SQL');
- }
-
-
-
- public static function getpost($msg){
- self::write($msg, 'GETPOST');
- }
-
- }
- 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;
- }
- }
|