* Copyright (c) 2001-2008 Platon Group, http://platon.sk/
* All rights reserved.
*
* See README file for more information about this software.
* See COPYING file for license information.
*
* Download the latest version from
* http://platon.sk/projects/Metafox/
*/
/* $Platon: Metafox/error.inc.php,v 1.15 2014-04-23 18:00:56 igor Exp $ */
require_once 'cfgldr.inc.php';
require_once 'functions.inc.php';
require_once 'HTML/Template/Platon.php';
function ezin_php_error_handler($errno, $errstr, $errfile, $errline) /* {{{ */
{
global $ezin_cgi;
if (! ($errno & error_reporting())) {
return;
}
if ($ezin_cgi['output'] == 'admin') {
echo "
ERROR: $errno:$errstr:$errfile:$errline
\n";
return;
}
$errortype = array (
1 => 'error',
2 => 'warning',
4 => 'parsing error',
8 => 'notice',
16 => 'core error',
32 => 'core warning',
64 => 'compile error',
128 => 'compile warning',
256 => 'user error',
512 => 'user warning',
1024 => 'user notice'
);
$err_ar = array(
'error_reporting' => error_reporting(),
'error_file' => trim($errfile),
'error_line' => trim($errline),
'error_type' => $errortype[$errno],
'error_code' => trim($errno),
'error_message' => trim($errstr)
);
@ob_end_clean();
global $ezin_sys;
global $ezin_cfg;
global $ezin_cgi;
// template function file loading
if (file_exists($ezin_cfg['theme']['dir'].'/'.$ezin_sys['tmpl']['fnc_file'])) {
include_once $ezin_cfg['theme']['dir'].'/'.$ezin_sys['tmpl']['fnc_file'];
}
$tmpl = ezin_load_template($ezin_sys['tmpl']['error'], 1);
if ($tmpl != false) {
$tmpl = new HTML_Template_Platon($ezin_sys['tmpl']['error']);
}
if ($tmpl == false || $tmpl->isVar('content')) {
$content = '';
$content .= '';
$content .= '| ';
$content .= 'An error occured';
$content .= ' |
';
$content .= '| |
'."\n";
foreach ($err_ar as $key => $val) {
$content .= '| ';
$content .= '' . Platon::htmlspecialchars($key) . '';
$content .= ' | ';
$content .= Platon::htmlspecialchars($val);
$content .= ' |
'."\n";
}
$content .= '| |
';
$content .= '| ';
$content .= 'Powered by';
$content .= ' ';
$content .= $ezin_sys['software']['name'].'';
$content .= ' |
';
$content .= '
';
if ($tmpl != false) {
$tmpl->set('content', $content);
}
}
if ($tmpl != false) {
if (preg_match('|^.*index.php\(\d+\) : eval\(\)\'d code$|', $errfile)) {
$err_ar['error_source'] = ezin_get_main_template_source($errline);
} else {
$err_ar['error_source'] = 'not available';
}
$tmpl->set($err_ar);
ezin_substitute_basic_properties($tmpl);
//echo(Platon::htmlspecialchars($tmpl->getCode()));
@$tmpl->Output();
} else {
echo $content;
}
ezin_error_send_notify_email($err_ar);
flush();
exit;
} /* }}} */
function ezin_pear_error_handler($err_obj) /* {{{ */
{
$error_string = $err_obj->getMessage();
// SENSITIVE!!! . ' | ' . $err_obj->getDebugInfo();
trigger_error($error_string, E_USER_ERROR);
} /* }}} */
function ezin_get_main_template_source($line_around) /* {{{ */
{
global $ezin_tmpl_main;
$index = $line_around - 10;
$content = '';
$code_ar = @$ezin_tmpl_main->getCode();
$code_ar = preg_split('|[\n]|', $code_ar);
$code_ar = array_slice($code_ar, $index - 1, 25);
foreach ($code_ar as $val) {
if (strlen($val) > 60) {
$val = trim(str_replace(';', ";\n ", $val));
$val = trim(str_replace('.\'', "\n .'", $val));
}
$val = sprintf('%04d: %s', $index, Platon::htmlspecialchars($val));
if ($index == $line_around) {
$val = "$val";
}
$content .= $val."\n";
$index++;
}
return $content;
} /* }}} */
function ezin_error_send_notify_email($err_ar) /* {{{ */
{
global $ezin_sys, $ezin_cfg;
$err_ar['error_source'] = Platon::undo_htmlspecialchars(@$err_ar['error_source']);
$err_ar['error_source'] = preg_replace('|(.*)|', '--> \\1 <--',
$err_ar['error_source']);
$err_msg = '';
foreach ($err_ar as $key => $val) {
$err_msg .= sprintf("* %s:%s%s\n", $key, $key == 'error_source' ? "\n\n" : ' ', $val);
}
/*
* Checking existing error log
*/
// TODO: fix the /tmp/ to use something more standard compliant
$site_key = @$ezin_cfg['abbreviation'].'.'.md5(@$ezin_cfg['name']);
$error_log_file = '/tmp/Metafox-error.'.$site_key.'.txt';
$error_log_mtime = @filemtime($error_log_file);
$error_log_msg = @file_get_contents($error_log_file);
if (! strcasecmp(trim($err_msg), trim($error_log_msg)) && time() - $error_log_mtime < 1800) {
return 1;
}
/*
* Error logging
*/
if (($f_log = @fopen($error_log_file, 'w')) != false) {
fwrite($f_log, $err_msg);
@fclose($f_log);
}
/*
* E-mail composing
*/
$from = '';
$to = '';
$subject = '';
$encoding = '';
$message = '';
$tmpl = ezin_load_template($ezin_sys['tmpl']['e-mail']['error'], 1);
if ($tmpl != false) {
$tmpl = new HTML_Template_Platon($ezin_sys['tmpl']['e-mail']['error']);
}
if ($tmpl == false || $tmpl->isVar('content')) {
$message .= 'Dear administrator of ';
$message .= $ezin_sys['software']['name']." on $ezin_cfg[name]\n\n";
$message .= 'An error has occured on your portal at server time ';
$message .= date($ezin_cfg['format_datetime']) . '.';
$message .= "\nFull error report follows on the next lines.\n\n";
$message .= $err_msg;
$message .= "\nI wish you a fast and effective solution of this problem.";
$message .= "\n\nYours,\n\n";
$message .= $ezin_sys['software']['name']." on $ezin_cfg[name]\n\n";
if ($tmpl != false) {
$tmpl->set('content', $content);
}
}
if ($tmpl != false) {
$tmpl->set($err_ar);
ezin_substitute_basic_properties($tmpl);
ob_start();
@$tmpl->Output();
$message = ob_get_contents();
ob_end_clean();
$message = ezin_email_template_parse($message, $from, $to, $subject, $encoding);
}
empty($subject) && $subject = sprintf('%s: error report', $ezin_cfg['name']);
if (strlen($to) <= 0) {
$to = $ezin_cfg['admin_email'];
}
/*
* E-mail sending
*/
return ezin_email_send($to, $from, $subject, $encoding, $message);
} /* }}} */
error_reporting(!empty($ezin_sys['error_reporting']) ? $ezin_sys['error_reporting'] : E_ALL);
set_error_handler('ezin_php_error_handler');
require_once 'PEAR.php';
PEAR::setErrorHandling(PEAR_ERROR_CALLBACK, 'ezin_pear_error_handler');
/* Modeline for ViM {{{
* vim: set ts=4:
* vim600: fdm=marker fdl=0 fdc=0:
* }}} */
?>