00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00062 require_once(dirname(__FILE__) . '/KjwCompat.php');
00063 require_once(dirname(__FILE__) . '/KjwObject.php');
00064
00077 $kjw_suppressed_error = null;
00078
00093 function kjw_error_handler($errno, $errstr, $errfile, $errline, $errcontext) {
00094
00095 if (($error_reporting = error_reporting()) != 0) {
00096
00097 if ($error_reporting & $errno) {
00099
00100
00101
00102
00103
00104
00105 kjw_croak('(global)', 'Internal Server Error', "$errstr.", 1);
00106 }
00107 } else {
00108 global $kjw_suppressed_error;
00109 $kjw_suppressed_error = $errstr;
00110 }
00111 }
00112
00124 function kjw_trace($msg, $level = 'DEBUG', $namespace = '(global)') {
00125 if (defined('KJW_STDLOG'))
00126 fwrite(KJW_STDLOG, strftime('%y-%m-%d %H:%M:%S %z: ') . "$level: $namespace: $msg\n");
00127 }
00128
00147 function kjw_croak($namespace, $shortMsg, $detailedMsg, $backtraceShift = 0) {
00148
00149
00150 if (error_reporting() == 0) {
00151 global $kjw_suppressed_error;
00152 $kjw_suppressed_error = $detailedMsg;
00153 return;
00154 }
00155
00156
00157 $print_html = php_sapi_name() != 'cli';
00158
00159
00160 $shortTextMsg = "** $shortMsg **\n\n"
00161 . "An error has occurred. We apologise for the inconvenience and ask you to try again later.\n"
00162 . "Most likely, the webmaster is working on fixing this error at this very moment.\n";
00163 $shortHtmlMsg = "<div style=\"display:block;color:black;font-family:sans-serif;font-size:18px;margin:0;padding:0 0 5px 0;"
00164 . "font-weight:bold;background:none;\">"
00165 . htmlentities($shortMsg) . "</div>\n"
00166 . "An error has occurred. We apologise for the inconvenience and ask you to try again later.<br/>\n"
00167 . "Most likely, the webmaster is working on fixing this error at this very moment.<br/><br/>\n"
00168 . "<a href=\"/\">Go back to home to if refreshing this page does not help.</a>\n";
00169
00170
00171 $details = array();
00172 $details[] = "Date: " . strftime('%Y-%m-%d %H:%M:%S.') . sprintf('%06d', (int)substr(microtime(), 2, 6));
00173 $details[] = "Detailed message:\n " . htmlentities($detailedMsg);
00174 $details[] = "Croak namespace/object: [" . htmlentities($namespace) . "]";
00175 if (isset($_SERVER['REMOTE_ADDR']))
00176 $details[] = "Caller IP: [" . htmlentities($_SERVER['REMOTE_ADDR']) . "]";
00177 if (isset($_SERVER['HTTP_HOST']) && isset($_SERVER['REQUEST_URI']))
00178 $details[] = "Viewed URL: " . htmlentities("http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]") . " ($_SERVER[REQUEST_METHOD])";
00179 if (isset($_SERVER['HTTP_REFERER']))
00180 $details[] = "Referrer URL: " . htmlentities($_SERVER['HTTP_REFERER']);
00181 if (isset($_SERVER['HTTP_USER_AGENT']))
00182 $details[] = "User-Agent: " . htmlentities($_SERVER['HTTP_USER_AGENT']);
00183
00184
00185 $backtrace = '';
00186 $back = debug_backtrace();
00187 while ($backtraceShift--)
00188 array_shift($back);
00189 foreach($back as $k => $v) {
00190 $backtrace .= sprintf(" %-20s %s()\n", htmlentities(basename(@$v['file'])) . ':'
00191 . htmlentities(@$v['line']), htmlentities(@$v['function']));
00192 }
00193
00194
00195 $cookie = serialize($_COOKIE);
00196 $post = serialize($_POST);
00197
00198 $session = serialize(@$_SESSION);
00199 $server = serialize($_SERVER);
00200
00201
00202 if (KJW_EMAIL_ERRORS_TO != '') {
00203 require_once(dirname(__FILE__) . '/KjwMail.php');
00204 kjwmail_sendplain(
00205 KJW_EMAIL_ERRORS_TO,
00206 'KjwLib::kjw_croak: ' . substr($shortMsg, 0, 40),
00207 $shortTextMsg . "\n[DETAILS]\n * " . implode("\n * ", $details) . "\n\n[BACKTRACE]\n$backtrace\n"
00208 . "[POST]\n $post\n\n[COOKIE]\n $cookie\n\n[SESSION]\n $session\n\n[SERVER]\n $server\n\n"
00209 );
00210 }
00211
00212
00213 kjw_trace($shortMsg, 'ERROR', $namespace);
00214 kjw_trace("DETAILS\n * " . implode("\n * ", $details), 'DEBUG', $namespace);
00215 kjw_trace("BACKTRACE\n$backtrace", 'DEBUG', $namespace);
00216 kjw_trace("POST = $post", 'DEBUG', $namespace);
00217 kjw_trace("COOKIE = $cookie", 'DEBUG', $namespace);
00218 kjw_trace("SESSION = $session", 'DEBUG', $namespace);
00219 kjw_trace("SERVER = $server", 'DEBUG', $namespace);
00220
00221
00222 if (!$print_html) {
00223 echo $shortTextMsg;
00224 if (KJW_EMAIL_ERRORS_TO == '')
00225 echo "\n[DETAILS]\n * ", implode("\n * ", $details), "\n\n[BACKTRACE]\n", $backtrace;
00226 } else {
00227
00228 echo "<!-- '\"/> --></a></iframe></select></script></textarea><br style=\"clear:both;\"/>\n";
00229
00230 $totalMsg = "<div style=\"display:block;font-weight:normal;font-family:sans-serif;font-size:12px;color:black;"
00231 . "background-color:white;padding:12px;margin:0;\">$shortHtmlMsg<br/>\n";
00232 if (KJW_EMAIL_ERRORS_TO == '')
00233 $totalMsg .= "<pre style=\"color: #903000;\">[DETAILS]\n * " . implode("\n * ", $details)
00234 . "\n\n[BACKTRACE]\n$backtrace</pre>\n";
00235 $totalMsg .= "</div>";
00236
00237
00238 echo $totalMsg;
00239
00240 echo "<style>html, body {display:block;position:static;text-align:left;margin:0;padding:0;background:none;}</style>";
00241 echo "<script type=\"text/javascript\">document.body.innerHTML = '"
00242 . str_replace("</", "<\\/", addcslashes($totalMsg, "\x01..\x1f'\\")) . "';</script>";
00243 }
00244
00245
00246 die(-1);
00247 }
00248
00249
00250
00254 if (!defined('KJW_DEBUG'))
00255 define('KJW_DEBUG', false);
00256 if (!defined('KJW_STDLOG'))
00257 if (defined('STDERR'))
00258 define('KJW_STDLOG', STDERR);
00259 if (!defined('KJW_DIE_ON_TRANSIENT_ERROR'))
00260 define('KJW_DIE_ON_TRANSIENT_ERROR', false);
00261 if (!defined('KJW_EMAIL_ERRORS_TO'))
00262 define('KJW_EMAIL_ERRORS_TO', '');
00263
00267 set_error_handler('kjw_error_handler');
00268
00269
00270 ?>