From d5b5deaae52e4aaa136261f966bf580e4b487ded Mon Sep 17 00:00:00 2001 From: wwccss Date: Wed, 17 Jul 2013 20:11:58 +0800 Subject: [PATCH] * finish task#1492. --- framework/helper.class.php | 45 +++------- framework/router.class.php | 173 +++++++++++++++++++++++++------------ 2 files changed, 131 insertions(+), 87 deletions(-) diff --git a/framework/helper.class.php b/framework/helper.class.php index 4117b34618..8460546730 100644 --- a/framework/helper.class.php +++ b/framework/helper.class.php @@ -424,6 +424,19 @@ class helper chdir($cwd); } } + + /** + * Remove UTF8 Bom + * + * @param string $string + * @access public + * @return string + */ + public static function removeUTF8Bom($string) + { + if(substr($string, 0, 3) == pack('CCC', 239, 187, 191)) return substr($string, 3); + return $string; + } } /** @@ -466,25 +479,6 @@ function getTime() return ((float)$usec + (float)$sec); } -/** - * Save the sql. - * - * @access protected - * @return void - */ -function saveSQL() -{ - if(!class_exists('dao')) return; - global $app; - $sqlLog = $app->getLogRoot() . 'sql.' . date('Ymd') . '.log'; - $fh = @fopen($sqlLog, 'a'); - if(!$fh) return false; - fwrite($fh, date('Ymd H:i:s') . ": " . $app->getURI() . "\n"); - foreach(dao::$querys as $query) fwrite($fh, " $query\n"); - fwrite($fh, "\n"); - fclose($fh); -} - /** * dump a var. * @@ -561,16 +555,3 @@ function isonlybody() { return (isset($_GET['onlybody']) and $_GET['onlybody'] == 'yes'); } - -/** - * Remove UTF8 Bom - * - * @param string $string - * @access public - * @return string - */ -function removeUTF8Bom($string) -{ - if(substr($string, 0, 3) == pack('CCC', 239, 187, 191)) return substr($string, 3); - return $string; -} diff --git a/framework/router.class.php b/framework/router.class.php index 850d9f8383..90c646e801 100755 --- a/framework/router.class.php +++ b/framework/router.class.php @@ -305,6 +305,7 @@ class router $this->loadConfig('common'); $this->setDebug(); + $this->setErrorHandler(); $this->connectDB(); @@ -407,7 +408,7 @@ class router { $this->appRoot = realpath($appRoot) . $this->pathFix; } - if(!is_dir($this->appRoot)) $this->error("The app you call not noud in {$this->appRoot}", __FILE__, __LINE__, $exit = true); + if(!is_dir($this->appRoot)) $this->triggerError("The app you call not noud in {$this->appRoot}", __FILE__, __LINE__, $exit = true); } /** @@ -511,11 +512,19 @@ class router */ public function setDebug() { - if(isset($this->config->debug) and $this->config->debug) - { - error_reporting(E_ALL & ~ E_STRICT); - register_shutdown_function('saveSQL'); - } + if(!empty($this->config->debug)) error_reporting(E_ALL & ~ E_STRICT); + } + + /** + * Set the error handler. + * + * @access public + * @return void + */ + public function setErrorHandler() + { + set_error_handler(array($this, 'saveError')); + register_shutdown_function(array($this, 'shutdown')); } /** @@ -801,7 +810,7 @@ class router } else { - $this->error("The request type {$this->config->requestType} not supported", __FILE__, __LINE__, $exit = true); + $this->triggerError("The request type {$this->config->requestType} not supported", __FILE__, __LINE__, $exit = true); } } @@ -959,7 +968,7 @@ class router $this->controlFile = $this->moduleRoot . $this->moduleName . $this->pathFix . 'control.php'; if(!is_file($this->controlFile)) { - $this->error("the control file $this->controlFile not found.", __FILE__, __LINE__, $exitIfNone); + $this->triggerError("the control file $this->controlFile not found.", __FILE__, __LINE__, $exitIfNone); return false; } return true; @@ -1094,11 +1103,11 @@ class router /* Set the class name of the control. */ $className = class_exists("my$moduleName") ? "my$moduleName" : $moduleName; - if(!class_exists($className)) $this->error("the control $className not found", __FILE__, __LINE__, $exit = true); + if(!class_exists($className)) $this->triggerError("the control $className not found", __FILE__, __LINE__, $exit = true); /* Create a instance of the control. */ $module = new $className(); - if(!method_exists($module, $methodName)) $this->error("the module $moduleName has no $methodName method", __FILE__, __LINE__, $exit = true); + if(!method_exists($module, $methodName)) $this->triggerError("the module $moduleName has no $methodName method", __FILE__, __LINE__, $exit = true); $this->control = $module; /* include default value for module*/ @@ -1219,7 +1228,7 @@ class router } else { - if($defaultValue === '_NOT_SET') $this->error("The param '$key' should pass value. ", __FILE__, __LINE__, $exit = true); + if($defaultValue === '_NOT_SET') $this->triggerError("The param '$key' should pass value. ", __FILE__, __LINE__, $exit = true); } $i ++; } @@ -1235,7 +1244,7 @@ class router } else { - if($defaultValue === '_NOT_SET') $this->error("The param '$key' should pass value. ", __FILE__, __LINE__, $exit = true); + if($defaultValue === '_NOT_SET') $this->triggerError("The param '$key' should pass value. ", __FILE__, __LINE__, $exit = true); } } } @@ -1288,47 +1297,6 @@ class router //-------------------- Tool methods.------------------// - /** - * The error handler. - * - * @param string $message error message - * @param string $file the file error occers - * @param int $line the line error occers - * @param bool $exit exit the program or not - * @access public - * @return void - */ - public function error($message, $file, $line, $exit = false) - { - /* Log the error info. */ - $log = "ERROR: $message in $file on line $line"; - if(isset($_SERVER['SCRIPT_URI'])) $log .= ", request: $_SERVER[SCRIPT_URI]";; - $trace = debug_backtrace(); - extract($trace[0]); - extract($trace[1]); - $log .= ", last called by $file on line $line through function $function.\n"; - error_log($log); - - /* If exit, output the error. */ - if($exit) - { - if($this->config->debug) - { - if(PHP_SAPI != 'cli') - { - $htmlError = ""; - $htmlError .= "$log"; - die($htmlError); - } - else - { - die($log); - } - } - die(); - } - } - /** * Load a class file. * @@ -1354,7 +1322,7 @@ class router $classFile = $this->coreLibRoot . $className; if(is_dir($classFile)) $classFile .= $this->pathFix . $className; $classFile .= '.class.php'; - if(!helper::import($classFile)) $this->error("class file $classFile not found", __FILE__, __LINE__, $exit = true); + if(!helper::import($classFile)) $this->triggerError("class file $classFile not found", __FILE__, __LINE__, $exit = true); } /* If staitc, return. */ @@ -1362,7 +1330,7 @@ class router /* Instance it. */ global $$className; - if(!class_exists($className)) $this->error("the class $className not found in $classFile", __FILE__, __LINE__, $exit = true); + if(!class_exists($className)) $this->triggerError("the class $className not found in $classFile", __FILE__, __LINE__, $exit = true); if(!is_object($$className)) $$className = new $className(); return $$className; } @@ -1551,6 +1519,101 @@ class router self::error($exception->getMessage(), __FILE__, __LINE__, $exit = true); } } + + //-------------------- Error methods.------------------// + + /** + * The shutdown handler. + * + * @access public + * @return void + */ + public function shutdown() + { + /* If debug on, save sql lines. */ + if(!empty($this->config->debug)) $this->saveSQL(); + + /* If any error occers, save it. */ + $error = error_get_last(); + if($error) $this->saveError($error['type'], $error['message'], $error['file'], $error['line']); + } + + /** + * Trriger an error. + * + * @param string $message error message + * @param string $file the file error occers + * @param int $line the line error occers + * @param bool $exit exit the program or not + * @access public + * @return void + */ + public function triggerError($message, $file, $line, $exit = false) + { + /* Set the error info. */ + $log = "ERROR: $message in $file on line $line"; + if(isset($_SERVER['SCRIPT_URI'])) $log .= ", request: $_SERVER[SCRIPT_URI]";; + $trace = debug_backtrace(); + extract($trace[0]); + extract($trace[1]); + $log .= ", last called by $file on line $line through function $function.\n"; + + /* Trigger it. */ + trigger_error($log, $exit ? E_USER_ERROR : E_USER_WARNING); + } + + /** + * Save error info. + * + * @param int $level + * @param string $message + * @param string $file + * @param int $line + * @access public + * @return void + */ + public function saveError($level, $message, $file, $line) + { + /* Set the error info. */ + $errorLog = "\n" . date('H:i:s') . " $message in $file on line $line "; + $errorLog .= "when visiting " . $this->getURI() . "\n"; + + /* Save to log file. */ + $errorFile = $this->getLogRoot() . 'php.' . date('Ymd') . '.log'; + $fh = @fopen($errorFile, 'a'); + if($fh) fwrite($fh, strip_tags($errorLog)) && fclose($fh); + + /* If error level is serious, die. */ + if($level == E_ERROR or $level == E_PARSE or $level == E_CORE_ERROR or $level == E_COMPILE_ERROR or $level == E_USER_ERROR) + { + echo $level; + if(empty($this->config->debug)) die(); + if(PHP_SAPI == 'cli') die($errorLog); + + $htmlError = ""; + $htmlError .= "" . nl2br($errorLog) . ""; + die($htmlError); + } + } + + /** + * Save the sql. + * + * @access protected + * @return void + */ + public function saveSQL() + { + if(!class_exists('dao')) return; + + $sqlLog = $this->getLogRoot() . 'sql.' . date('Ymd') . '.log'; + $fh = @fopen($sqlLog, 'a'); + if(!$fh) return false; + fwrite($fh, date('Ymd H:i:s') . ": " . $this->getURI() . "\n"); + foreach(dao::$querys as $query) fwrite($fh, " $query\n"); + fwrite($fh, "\n"); + fclose($fh); + } } /**