'.$message.'
PHP:'.$logtrace;
dzz_error::write_error_log($messagesave);
}
if($show) {
if(!defined('IN_MOBILE')) {
dzz_error::show_error('system', "
$message",DZZ_CORE_DEBUG?$showtrace:array(), 0);
} else {
dzz_error::mobile_show_error('system', "$message", DZZ_CORE_DEBUG?$showtrace:array(), 0);
}
}
if($halt) {
exit();
} else {
return $message;
}
}
public static function template_error($message, $tplname) {
$message = lang($message);
$tplname = str_replace(DZZ_ROOT, '', $tplname);
$message = $message.': '.$tplname;
dzz_error::system_error($message);
}
public static function debug_backtrace() {
$skipfunc[] = 'error->debug_backtrace';
$skipfunc[] = 'error->db_error';
$skipfunc[] = 'error->template_error';
$skipfunc[] = 'error->system_error';
$skipfunc[] = 'db_mysql->halt';
$skipfunc[] = 'db_mysql->query';
$skipfunc[] = 'DB::_execute';
$show = $log = '';
$debug_backtrace = debug_backtrace();
krsort($debug_backtrace);
foreach ($debug_backtrace as $k => $error) {
$file = str_replace(DZZ_ROOT, '', $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 .= "[Line: $error[line]]".$file."($func)";
$log .= !empty($log) ? ' -> ' : '';$file.':'.$error['line'];
$log .= $file.':'.$error['line'];
}
return array($show, $log);
}
public static function db_error($message, $sql) {
global $_G;
list($showtrace, $logtrace) = dzz_error::debug_backtrace();
$title = lang('db_'.$message);
$title_msg = lang('db_error_message');
$title_sql = lang('db_query_sql');
$title_backtrace = lang('backtrace');
$title_help = lang('db_help_link');
$db = &DB::object();
$dberrno = $db->errno();
$dberror = str_replace($db->tablepre, '', $db->error());
$sql = dhtmlspecialchars(str_replace($db->tablepre, '', $sql));
$msg = '[Type] '.$title.'';
$msg .= $dberrno ? '['.$dberrno.'] '.$dberror.'' : '';
$msg .= $sql ? '[Query] '.$sql.'' : '';
dzz_error::show_error('db', $msg, $showtrace, false);
unset($msg, $phperror);
$errormsg = ''.$title.'';
$errormsg .= "[$dberrno]
ERR: $dberror
";
if($sql) {
$errormsg .= 'SQL: '.$sql;
}
$errormsg .= "
";
$errormsg .= 'PHP: '.$logtrace;
dzz_error::write_error_log($errormsg);
exit();
}
public static function exception_error($exception) {
if($exception instanceof DbException) {
$type = 'db';
} else {
$type = 'system';
}
if($type == 'db') {
$errormsg = '('.$exception->getCode().') ';
$errormsg .= self::sql_clear($exception->getMessage());
if($exception->getSql()) {
$errormsg .= '';
$errormsg .= self::sql_clear($exception->getSql());
$errormsg .= '
';
}
} 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('DZZ_DEBUG') && DZZ_DEBUG) ? $arg : '%d';
} elseif(is_float($arg)) {
$fun .= (defined('DZZ_DEBUG') && DZZ_DEBUG) ? $arg : '%f';
} else {
$fun .= (defined('DZZ_DEBUG') && DZZ_DEBUG) ? '\''.dhtmlspecialchars(substr(self::clear($arg), 0, 10)).(strlen($arg) > 10 ? ' ...' : '').'\'' : '%s';
}
$mark = ', ';
}
}
$fun .= ')';
$error['function'] = $fun;
}
$phpmsg[] = array(
'file' => str_replace(array(DZZ_ROOT, '\\'), array('', '/'), $error['file']),
'line' => $error['line'],
'function' => $error['function'],
);
}
self::show_error($type, $errormsg, $phpmsg);
exit();
}
public static function show_error($type, $errormsg, $phpmsg = '', $typemsg = '') {
global $_G;
ob_end_clean();
$gzip = getglobal('gzipcompress');
ob_start($gzip ? 'ob_gzhandler' : null);
$host = $_SERVER['HTTP_HOST'];
$title = $type == 'db' ? 'Database' : 'System';
echo <<
$host - $title Error
Dzz! $title Error
$errormsg
EOT;
if(!empty($phpmsg)) {
echo '
';
echo '
PHP Debug
';
echo '
';
echo '| No. | File | Line | Code |
';
if(is_array($phpmsg)) {
foreach($phpmsg as $k => $msg) {
$k++;
echo '';
echo '| '.$k.' | ';
echo ''.$msg['file'].' | ';
echo ''.$msg['line'].' | ';
echo ''.$msg['function'].' | ';
echo '
';
}
} else {
echo ' |
';
}
echo '
';
}
$helplink = '';
$endmsg = lang('error_end_message', array('host'=>$host));
echo <<
$endmsg. $helplink
EOT;
$exit && exit();
}
public static function mobile_show_error($type, $errormsg, $phpmsg) {
global $_G;
ob_end_clean();
ob_start();
$host = $_SERVER['HTTP_HOST'];
$phpmsg = trim($phpmsg);
$title = 'Mobile '.($type == 'db' ? 'Database' : 'System');
echo <<
$host - $title Error
| Dzz! $title Error |
EOT;
echo <<
|
| Error messages: |
|
|
EOT;
if(!empty($phpmsg) && $type == 'db') {
echo << |
| Program messages: |
|
|
EOT;
}
$endmsg = lang('mobile_error_end_message', array('host'=>$host));
echo <<
$endmsg |
EOT;
$exit && exit();
}
public static function clear($message) {
if(is_object($message)){
$message = json_decode($message,true);
}
return str_replace(array("\t", "\r", "\n"), " ", $message);
}
public static function sql_clear($message) {
$message = self::clear($message);
$message = str_replace(DB::object()->tablepre, '', $message);
$message = dhtmlspecialchars($message);
return $message;
}
public static function write_error_log($message) {
$loginfo=array("mark"=>"errorlog","content"=>$message);
Hook::listen('systemlog',$loginfo);
return;
$message = dzz_error::clear($message);
$time = time();
$file = DZZ_ROOT.'./data/log/'.date("Ym").'_errorlog.php';
$hash = md5($message);
$uid = getglobal('uid');
$ip = getglobal('clientip');
$user = 'User: uid='.intval($uid).'; IP='.$ip.'; RIP:'.$_SERVER['REMOTE_ADDR'];
$uri = 'Request: '.dhtmlspecialchars(dzz_error::clear($_SERVER['REQUEST_URI']));
$message = "\t{$time}\t$message\t$hash\t$user $uri\n";
if($fp = @fopen($file, 'rb')) {
$lastlen = 50000;
$maxtime = 60 * 10;
$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] != '') continue;
if($row[3] == $hash && ($row[1] > $time - $maxtime)) {
return;
}
}
}
}
error_log($message, 3, $file);
}
}