2014-06-02 18:58:49 +04:00
< ? php namespace Majestic\Exception ;
2010-03-07 17:26:44 +00:00
/**
* @ copyright NetMonsters < team @ netmonsters . ru >
* @ link http :// netmonsters . ru
* @ package Majestic
* @ subpackage exception
* @ since 2010 - 02 - 26
*/
class ErrorHandler
{
2011-10-13 14:55:06 +04:00
2010-03-07 17:26:44 +00:00
static public function init ()
{
2014-07-03 19:49:43 +04:00
//set_error_handler(array('Majestic\Exception\ErrorHandler', 'error_handler'));
2010-03-07 17:26:44 +00:00
}
2011-10-13 14:55:06 +04:00
static public function error_handler ( $errno , $errstr , $errfile , $errline )
2010-03-07 17:26:44 +00:00
{
2012-06-04 16:10:34 +04:00
$ob_handlers = ob_get_status ();
if ( ! empty ( $ob_handlers )) {
2012-07-09 12:52:52 +04:00
ob_end_clean ();
2012-06-04 16:10:34 +04:00
}
2012-10-05 19:38:24 +04:00
if ( error_reporting () !== 0 ) {
2014-06-02 18:58:49 +04:00
throw new \ErrorException ( $errstr , 0 , $errno , $errfile , $errline );
2012-10-05 19:38:24 +04:00
}
return true ;
2010-03-07 17:26:44 +00:00
}
2011-10-13 14:55:06 +04:00
2012-12-11 15:03:48 +04:00
static public function logError ( $exception )
{
$error = 0 ;
2012-12-11 19:05:39 +04:00
$exception_name = '' ;
2014-06-02 18:58:49 +04:00
if ( $exception instanceof \ErrorException ) {
2012-12-11 15:03:48 +04:00
$error = $exception -> getSeverity ();
2012-12-11 19:05:39 +04:00
} else {
$exception_name = get_class ( $exception ) . ': ' ;
2012-12-11 15:03:48 +04:00
}
switch ( $error ) {
case E_NOTICE :
$error = 'Notice' ;
break ;
case E_WARNING :
$error = 'Warning' ;
break ;
case E_ERROR :
$error = 'Fatal Error' ;
break ;
default :
$error = 'Unknown Error' ;
break ;
}
2012-12-11 19:05:39 +04:00
$message = 'PHP ' . $error . ': ' . $exception_name . $exception -> getMessage () . ' in ' . $exception -> getFile () . ':' . $exception -> getLine () . ' \nStack trace:\n' . $exception -> getTraceAsString () . self :: getHTTPErrorConditions ();
2012-12-11 15:03:48 +04:00
// PHP Fatal error: Uncaught exception 'LogicException' in /www/test.tfs/face/htdocs/index.php:11\nStack trace:\n#0 {main}\n thrown in /www/test.tfs/face/htdocs/index.php on line 11, referer: http://test.tfs.manekeno.netmonsters.ru/news/create
error_log ( $message );
}
2012-12-11 16:28:04 +04:00
static public function getHTTPErrorConditions ()
2012-12-11 15:03:48 +04:00
{
$text = null ;
2014-06-02 18:58:49 +04:00
if ( ! is_null ( \Majestic\Env :: Server ( 'REQUEST_METHOD' )) && ! is_null ( \Majestic\Env :: Server ( 'REQUEST_URI' ))) {
$text = ', URL: ' . \Majestic\Env :: Server ( 'REQUEST_METHOD' ) . ' ' . \Majestic\Env :: Server ( 'REQUEST_URI' );
$text .= ', referrer: ' . \Majestic\Env :: Server ( 'HTTP_REFERER' );
2012-12-11 15:03:48 +04:00
}
return $text ;
}
2010-03-07 17:26:44 +00:00
static protected function getSource ( $file , $hiline )
{
$code = array ();
$i = 0 ;
2011-10-13 14:55:06 +04:00
foreach ( file ( $file ) as $line ) {
2010-03-07 17:26:44 +00:00
$i ++ ;
2011-10-13 14:55:06 +04:00
if ( $i >= $hiline - 10 && $i <= $hiline + 10 ) {
2010-03-07 17:26:44 +00:00
if ( $i == $hiline ) {
2010-03-07 19:54:09 +00:00
$code [] = '<tr class="error"><th>' . $i . '</th><td><span class="specific">' . htmlentities ( $line , ENT_QUOTES , 'UTF-8' ) . '</span></td></tr>' ;
2011-10-13 14:55:06 +04:00
} else {
2010-03-07 19:54:09 +00:00
$code [] = '<tr><th>' . $i . '</th><td>' . htmlentities ( $line , ENT_QUOTES , 'UTF-8' ) . '</td></tr>' ;
2010-03-07 17:26:44 +00:00
}
}
2011-10-13 14:55:06 +04:00
if ( $i > $hiline + 10 ) {
2010-03-07 17:26:44 +00:00
break ;
}
}
return implode ( '' , $code );
}
2011-10-13 14:55:06 +04:00
2010-03-07 17:26:44 +00:00
static protected function wrapArray ( $array , $name )
{
if ( ! $array ) {
return '<p>No ' . $name . ' data</p>' ;
}
$text = '<table class="req"><thead><tr><th>Variable</th><th>Value</th></tr></thead><tbody>' ;
foreach ( $array as $key => $value ) {
2010-04-27 17:28:43 +00:00
if ( is_array ( $value ) || is_object ( $value )) {
2010-03-13 23:33:46 +00:00
$value = print_r ( $value , true );
}
2010-03-07 19:54:09 +00:00
$value = ( $value ) ? htmlentities ( $value , ENT_QUOTES , 'UTF-8' ) : ' ' ;
2010-03-07 17:26:44 +00:00
$text .= '<tr><td>' . $key . '</td><td class="code"><div>' . $value . '</div></td></tr>' ;
}
$text .= '</tbody></table>' ;
return $text ;
}
2011-10-13 14:55:06 +04:00
2010-03-07 17:26:44 +00:00
static protected function wrapTrace ( $trace )
{
return '<code>' . nl2br ( $trace ) . '</code>' ;
}
2011-10-13 14:55:06 +04:00
2010-03-07 17:26:44 +00:00
/**
2014-06-02 18:58:49 +04:00
* @ param \Exception $exception
2012-11-19 19:20:15 +04:00
* @ return string
2010-03-07 17:26:44 +00:00
*/
static public function showDebug ( $exception )
{
2012-06-04 16:10:34 +04:00
$ob_handlers = ob_get_status ();
if ( ! empty ( $ob_handlers )) {
ob_end_clean ();
}
2010-03-07 17:26:44 +00:00
$class = get_class ( $exception );
2011-10-13 14:55:06 +04:00
2014-06-02 18:58:49 +04:00
$method = \Majestic\Env :: Server ( 'REQUEST_METHOD' , '' );
$uri = \Majestic\Env :: getRequestUri ();
2010-03-07 17:26:44 +00:00
$source = self :: getSource ( $exception -> getFile (), $exception -> getLine ());
2014-06-02 18:58:49 +04:00
$time = date ( 'r' , \Majestic\Env :: Server ( 'REQUEST_TIME' , time ()));
2011-10-13 14:55:06 +04:00
2010-04-27 17:28:43 +00:00
$trace = nl2br ( $exception -> getTraceAsString ());
2011-10-13 14:55:06 +04:00
2014-06-02 18:58:49 +04:00
$get = self :: wrapArray ( \Majestic\Env :: Get (), 'GET' );
$post = self :: wrapArray ( \Majestic\Env :: Post (), 'POST' );
$session = self :: wrapArray ( \Majestic\Session\Session :: get (), 'SESSION' );
$files = self :: wrapArray ( \Majestic\Env :: Files (), 'FILES' );
$cookies = self :: wrapArray ( \Majestic\Env :: Cookie (), 'COOKIE' );
$server = self :: wrapArray ( \Majestic\Env :: Server (), 'SERVER' );
2011-10-13 14:55:06 +04:00
$message = <<< EOD
2010-03-07 17:26:44 +00:00
<! DOCTYPE html PUBLIC " -//W3C//DTD XHTML 1.0 Strict//EN " " http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd " >
< html xmlns = " http://www.w3.org/1999/xhtml " >
< head >
< meta http - equiv = " Content-Type " content = " text/html; charset=utf-8 " />
< title > { $class } at { $uri } </ title >
< style type = " text/css " >
html * { padding : 0 ; margin : 0 ; }
body * { padding : 10 px 20 px ; }
body * * { padding : 0 ; }
body { font : small sans - serif ; }
body > div { border - bottom : 1 px solid #ddd; }
h1 { font - weight : normal ; }
h2 { margin - bottom :. 8 em ; }
h2 span { font - size : 80 % ; color : #666; font-weight:normal; }
h3 { margin : 1 em 0 . 5 em 0 ; }
h4 { margin : 0 0 . 5 em 0 ; font - weight : normal ; }
table { border : 1 px solid #ccc; border-collapse: collapse; width:100%; background:white; }
tbody td , tbody th { vertical - align : top ; padding : 2 px 3 px ; }
thead th { padding : 1 px 6 px 1 px 3 px ; background : #fefefe; text-align:left; font-weight:normal; font-size:11px; border:1px solid #ddd; }
tbody th { width : 12 em ; text - align : right ; color : #666; padding-right:.5em; }
table . vars { margin : 5 px 0 2 px 40 px ; }
table . vars td , table . req td { font - family : monospace ; }
table td . code { width : 100 % ; }
table td . code div { overflow : hidden ; }
table . source th { color : #666; }
table . source td { font - family : monospace ; white - space : pre ; border - bottom : 1 px solid #eee; }
ul . traceback { list - style - type : none ; }
ul . traceback li . frame { margin - bottom : 1 em ; }
div . context { margin : 10 px 0 ; }
div . context ol { padding - left : 30 px ; margin : 0 10 px ; list - style - position : inside ; }
div . context ol li { font - family : monospace ; white - space : pre ; color : #666; cursor:pointer; }
div . context ol . context - line li { color : black ; background - color : #ccc; }
div . context ol . context - line li span { float : right ; }
div . commands { margin - left : 40 px ; }
div . commands a { color : black ; text - decoration : none ; }
#summary { background: #ffc; }
#summary h2 { font-weight: normal; color: #666; }
#explanation { background:#eee; }
#source{ background:#f6f6f6; }
#traceback { background:#eee; }
#requestinfo { background:#f6f6f6; padding-left:120px; }
#summary table { border:none; background:transparent; }
#requestinfo h2, #requestinfo h3 { position:relative; margin-left:-100px; }
#requestinfo h3 { margin-bottom:-1em; }
. error { background : #ffc; }
. specific { color : #cc3300; font-weight:bold; }
h2 span . commands { font - size :. 7 em ;}
span . commands a : link { color : #5E5694;}
pre . exception_value { font - family : sans - serif ; color : #666; font-size: 1.5em; margin: 10px 0 10px 0; }
</ style >
</ head >
< body >
< div id = " summary " >
< h1 > { $class } at { $uri } </ h1 >
< pre class = " exception_value " > { $exception -> getMessage ()} </ pre >
< table class = " meta " >
< tbody >
< tr >< th > Request Method :</ th >< td > { $method } </ td ></ tr >
< tr >< th > Exception Type :</ th >< td > { $class } </ td ></ tr >
< tr >< th > Exception Message :</ th >< td >< pre > { $exception -> getMessage ()} </ pre ></ td ></ tr >
< tr >< th > Exception Location :</ th >< td > { $exception -> getFile ()}, line { $exception -> getLine ()} </ td ></ tr >
< tr >< th > Server time :</ th >< td > { $time } </ td ></ tr >
</ tbody >
</ table >
</ div >
< div id = " source " >
< h2 > Context </ h2 >
< p > In file < code > { $exception -> getFile ()} </ code > , error at line < strong > { $exception -> getLine ()} </ strong ></ p >
< h3 > { $exception -> getMessage ()} </ h3 >
< table class = " source cut-top " >
< tbody > { $source } </ tbody >
</ table >
</ div >
< div id = " traceback " >
< h2 > Traceback </ h2 >
2010-04-27 17:28:43 +00:00
< div id = " browserTraceback " >< code > { $trace } </ code ></ div >
2010-03-07 17:26:44 +00:00
</ div >
< div id = " requestinfo " >
< h2 > Request information </ h2 >
< h3 id = " get-info " > GET </ h3 >
{ $get }
< h3 id = " post-info " > POST </ h3 >
{ $post }
2010-04-26 19:56:01 +00:00
< h3 id = " session-info " > SESSION </ h3 >
{ $session }
2010-03-07 17:26:44 +00:00
< h3 id = " files-info " > FILES </ h3 >
{ $files }
< h3 id = " cookie-info " > COOKIES </ h3 >
{ $cookies }
< h3 id = " meta-info " > SERVER </ h3 >
{ $server }
< h3 id = " settings-info " > Settings </ h3 >
< p > todo </ p >
</ div >
< div id = " explanation " >
< p > You ' re seeing this error because you have defined constant < code > DEBUG = True </ code > in your config file . Change that to < code > False </ code > , and it will display a standard 500 page .</ p >
</ div >
</ body >
</ html >
EOD ;
return $message ;
}
}