<?php
Custom Exception functions
Set_exception_handler (' handle_exception ');
Custom error functions
Set_error_handler (' Handle_error ');
/**
* Exception Handling
*
* @param mixed $exception Exception object
* @author blog.snsgou.com
*/
function Handle_exception ($exception) {
Error::exceptionerror ($exception);
}
/**
* Error Handling
*
* @param string $errNo error code
* @param string $ERRSTR error message
* @param string $errFile error file
* @param string $errLine error line
* @author blog.snsgou.com
*/
function Handle_error ($errNo, $errStr, $errFile, $errLine) {
if ($errNo) {
Error::systemerror ($errStr, False, True, false);
}
}
/**
* System error Handling
*
* @author blog.snsgou.com
*/
Class Error {
public static function Systemerror ($message, $show = True, $save = True, $halt = True) {
List ($showTrace, $logTrace) = self::d ebugbacktrace ();
if ($save) {
$messageSave = ' <b> '. $message. ' </b><br/><b>php:</b> '. $logTrace;
Self::writeerrorlog ($messageSave);
}
if ($show) {
Self::showerror (' System ', "<li> $message </li>", $showTrace, 0);
}
if ($halt) {
Exit ();
} else {
return $message;
}
}
/**
* Code Execution Procedure backtracking information
*
* @static
* @access Public
*/
public static function Debugbacktrace () {
$skipFunc [] = ' error->debugbacktrace ';
$show = $log = ';
$debugBacktrace = Debug_backtrace ();
ksort ($debugBacktrace);
foreach ($debugBacktrace as $k => $error) {
if (!isset ($error [' file '])] {
& NBSP;&NBSP;&NBSP;&NBSP;//uses the reflection API to get the file and line number of the method/function
try {
if (Isset ($error [' class ']) {
$reflection = new Reflectionmethod ($ Error[' class '], $error [' function ']);
} else {
$reflection = new Reflectionfunction ($error [' function ']);
}
$error [' file '] = $reflection->getfilename ();
$error [' line '] = $reflection->getstartline ();
} catch (Exception $e) {
continue;
&NBSP;&NBSP;&NBSP;&NBSP}
}
$file = Str_replace (Site_path, ', $error [' file ']);
$func = Isset ($error [' class '])? $error [' class ']: ';
$func. = Isset ($error [' type '])? $error [' type ']: ';
$func. = Isset ($error [' function '])? $error [' function ']: ';
if (In_array ($func, $skipFunc)) {
Break
}
$error [' line '] = sprintf ('%04d ', $error [' line ']);
$show. = ' <li>[line: '. $error [' line ']. ']' . $file. ' ('. $func. ') </li> ';
$log. =!empty ($log)? '-> ': ';
$log. = $file. ':' . $error [' line '];
}
Return Array ($show, $log);
}
/**
* Exception Handling
*
* @static
* @access Public
* @param mixed $exception
*/
public static function Exceptionerror ($exception) {
if ($exception instanceof dbexception) {
$type = ' db ';
} else {
$type = ' System ';
}
if ($type = = ' db ') {
$ERRORMSG = ' ('. $exception->getcode (). ') ';
$errorMsg. = Self::sqlclear ($exception->getmessage (), $exception->getdbconfig ());
if ($exception->getsql ()) {
$errorMsg. = ' <div class= ' sql > ';
$errorMsg. = Self::sqlclear ($exception->getsql (), $exception->getdbconfig ());
$errorMsg. = ' </div> ';
}
} else {
$ERRORMSG = $exception->getmessage ();
}
$trace = $exception->gettrace ();
Krsort ($trace);
$trace [] = Array (' file ' => $exception->getfile (), ' line ' => $exception->getline (), ' function ' => ' break ') ;
$PHPMSG = Array ();
foreach ($trace as $error) {
if (!empty ($error [' function '])) {
$fun = ';
if (!empty ($error [' class '])) {
$fun. = $error [' class ']. $error [' type '];
}
$fun. = $error [' function ']. '(';
if (!empty ($error [' args '])) {
$mark = ';
foreach ($error [' args '] as $arg) {
$fun. = $mark;
if (Is_array ($arg)) {
$fun. = ' Array ';
} elseif (Is_bool ($arg)) {
$fun. = $arg? ' True ': ' false ';
} elseif (Is_int ($arg)) {
$fun. = (defined (' Site_debug ') && site_debug)? $arg: '%d ';
} elseif (Is_float ($arg)) {
$fun. = (defined (' Site_debug ') && site_debug)? $arg: '%f ';
} else {
$fun. = (defined (' Site_debug ') && site_debug)? '\'' . Htmlspecialchars (substr (Self::clear ($arg), 0, 10)). (strlen ($arg) > 10? ' ...' : '') . ' \ ': '%s ';
}
$mark = ', ';
}
}
$fun. = ') ';
$error [' function '] = $fun;
}
if (!isset ($error [' line '])) {
Continue
}
$PHPMSG [] = Array (' file ' => str_replace (Array (site_path, ' \ '), Array (', '/'), $error [' file ']), ' line ' => $error [' Line '], ' function ' => $error [' function ']);
}
Self::showerror ($type, $ERRORMSG, $PHPMSG);
Exit ();
}
/**
* Logging error log
*
* @static
* @access Public
* @param string $message
*/
public static function Writeerrorlog ($message) {
return false; Do not write temporarily
$message = Self::clear ($message);
$time = time ();
$file = Log_path. '/' . Date (' Y.M.D '). ' _errorlog.php ';
$hash = MD5 ($message);
$userId = 0;
$ip = Get_client_ip ();
$user = ' <b>User:</b> userid= '. Intval ($userId). '; Ip= '. $ip. '; RIP: '. $_server[' REMOTE_ADDR '];
$uri = ' Request: '. Htmlspecialchars (Self::clear ($_server[' Request_uri '));
$message = "<?php exit;? >\t{$time}\t$message\t$hash\t$user $uri \ n ";
Determine if the $message has been recorded in the interval $maxtime, and there is no need to record
if (Is_file ($file)) {
$fp = @fopen ($file, ' RB ');
$lastlen = 50000; Read the last $lastlen length byte content
$maxtime = 60 * 10; Time interval: 10 minutes
$offset = FileSize ($file)-$lastlen;
if ($offset > 0) {
Fseek ($fp, $offset);
}
if ($data = Fread ($fp, $lastlen)) {
$array = explode ("\ n", $data);
if (Is_array ($array))
foreach ($array as $key => $val) {
$row = Explode ("T", $val);
if ($row [0]!= ' <?php exit;? > ') {
Continue
}
if ($row [3] = = $hash && ($row [1] > $time-$maxtime)) {
Return
}
}
}
}
Error_log ($message, 3, $file);
}
/**
* Clear Text part characters
*
* @param string $message
*/
public static function Clear ($message) {
return Str_replace (Array ("\ t", "\ r", "\ n"), "", $message);
}
/**
* SQL statement character cleanup
*
* @static
* @access Public
* @param string $message
* @param string $dbConfig
*/
public static function Sqlclear ($message, $dbConfig) {
$message = Self::clear ($message);
if (!) ( Defined (' Site_debug ') && Site_debug)) {
$message = Str_replace ($dbConfig [' Database '], ' * * * ', $message);
$message = Str_replace ($dbConfig [' prefix '], ' * * * ", $message);
$message = Str_replace (C (' Db_prefix '), ' * * *, $message);
}
$message = Htmlspecialchars ($message);
return $message;
}
/**
* Display Error
*
* @static
* @access Public
* @param string $type error type Db,system
* @param string $ERRORMSG
* @param string $PHPMSG
*/
public static function ShowError ($type, $errorMsg, $PHPMSG = ') {
Global $_g;
$ERRORMSG = Str_replace (Site_path, ', $ERRORMSG);
Ob_end_clean ();
$host = $_server[' http_host '];
$title = $type = = ' db '? ' Database ': ' System ';
Echo <<<eot
<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">
<title> $host-$title error</title>
<meta http-equiv= "Content-type" content= "text/html; charset={$_g[' config ' [' Output '] [' CharSet ']} '/>
<meta name= "ROBOTS" content= "noindex,nofollow,noarchive"/>
<style type= "Text/css" >
<!--
body {background-color:white; color:black; font:9pt/11pt Verdana, Arial, Sans-serif;}
#container {margin:10px;}
#message {width:1024px; color:black;}
. red {color:red;}
a:link {font:9pt/11pt Verdana, Arial, Sans-serif; color:red;}
a:visited {font:9pt/11pt Verdana, arial, sans-serif; color: #4e4e4e;}
h1 {color: #FF0000; font:18pt "Verdana"; margin-bottom:0.5em;}
. bg1 {background-color: #FFFFCC;}
. Bg2 {background-color: #EEEEEE;}
. Table {background: #AAAAAA; font:11pt menlo,consolas, Lucida Console}
. info {
Background:none Repeat scroll 0 0 #F3F3F3;
border:0px solid #aaaaaa;
border-radius:10px 10px 10px 10px;
Color: #000000;
font-size:11pt;
line-height:160%;
Margin-bottom:1em;
Padding:1em;
}
. help {
Background: #F3F3F3;
border-radius:10px 10px 10px 10px;
font:12px Verdana, Arial, Sans-serif;
Text-align:center;
line-height:160%;
Padding:1em;
}
. sql {
Background:none Repeat scroll 0 0 #FFFFCC;
border:1px solid #aaaaaa;
Color: #000000;
Font:arial, Sans-serif;
font-size:9pt;
line-height:160%;
Margin-top:1em;
padding:4px;
}
-->
</style>
<body>
<div id= "Container" >
<div class= ' info ' > $ERRORMSG </div>
EOT;
if (!empty ($PHPMSG)) {
Echo ' <div class= ' info ' > ';
Echo ' <p><strong>php debug</strong></p> ';
Echo ' <table cellpadding= "5" cellspacing= "1" width= "100%" class= "table" ><tbody> ";
if (Is_array ($PHPMSG)) {
Echo ' <tr class= "Bg2" ><td>No.</td><td>File</td><td>Line</td><td> Code</td></tr> ';
foreach ($phpMsg as $k => $msg) {
$k + +;
Echo ' <tr class= ' bg1 ' > ';
Echo ' <td> '. $k. ' </td> ';
Echo ' <td> '. $msg [' file ']. ' </td> ';
Echo ' <td> '. $msg [' line ']. ' </td> ';
Echo ' <td> '. $msg [' function ']. ' </td> ';
Echo ' </tr> ';
}
} else {
Echo ' <tr><td><ul> '. $PHPMSG. ' </ul></td></tr> ';
}
Echo ' </tbody></table></div> ';
}
Echo <<<eot
</div>
</body>
EOT;
Exit ();
}
}
/**
* DB Exception class
*
* @author blog.snsgou.com
*/
Class Dbexception extends Exception {
protected $sql;
protected $dbConfig; Current Database configuration information
Public function __construct ($message, $code = 0, $sql = ', $dbConfig = Array ()) {
$this->sql = $sql;
$this->dbconfig = $dbConfig;
Parent::__construct ($message, $code);
}
Public Function GetSQL () {
return $this->sql;
}
Public Function Getdbconfig () {
return $this->dbconfig;
}
}