kernel
[ class tree: kernel ] [ index: kernel ] [ all elements ]

Source for file bit_error_inc.php

Documentation is available at bit_error_inc.php

  1. <?php
  2. /**
  3.  * Custom ADODB Error Handler. This will be called with the following params
  4.  *
  5.  * @package kernel
  6.  * @subpackage functions
  7.  * @version V3.94  13 Oct 2003  (c) 2000-2003 John Lim (jlim@natsoft.com.my). All rights reserved.
  8.  *  Released under both BSD license and Lesser GPL library license.
  9.  *  Whenever there is any discrepancy between the two licenses,
  10.  *  the BSD license will take precedence.
  11.  *
  12.  *  Set tabs to 4 for best viewing.
  13.  *
  14.  *  Latest version is available at http://php.weblogs.com
  15.  *
  16.  */
  17.  
  18. /**
  19.  * set error handling
  20.  */
  21. if( !defined( 'BIT_INSTALL' ) &&  !defined( 'ADODB_ERROR_HANDLER' )  ) {
  22.     define( 'ADODB_ERROR_HANDLER', 'bitdb_error_handler' );
  23. }
  24.  
  25. /**
  26.  * Switch debug level in database
  27.  *
  28.  **/
  29.  
  30. function bit_db_debug( $pLevel = 99 ) {
  31.     global $gDebug, $gBitDb;
  32.     $gDebug = $pLevel;
  33.     if( is_object( $gBitDb) ) {
  34.         $gBitDb->debug( $pLevel );
  35.     }
  36. }
  37.  
  38. function bit_error_log( $pLogMessage ) {
  39.     if( !empty( $_SERVER['SCRIPT_URI'] )) {
  40.         error_log( "OUTPUT in {$_SERVER['SCRIPT_URI']}" );
  41.     }
  42.  
  43.     $errlines = explode( "\n", (is_array( $pLogMessage ) || is_object( $pLogMessage ) ? vc( $pLogMessage, FALSE ) : $pLogMessage) );
  44.     foreach ($errlines as $txt) { error_log($txt); }
  45. }
  46.  
  47. function bit_error_handler ( $errno, $errstr, $errfile, $errline, $errcontext=NULL ) {
  48.     // error_reporting() === 0 if code was prepended with @
  49.     if( ($errno == 1 || $reportingLevel = error_reporting()) && !strpos( $errfile, 'templates_c' ) ) {
  50.         $errType = FALSE;
  51.         switch ($errno) {
  52.             case E_ERROR: $errType = 'FATAL ERROR'; break;
  53.             case E_WARNING: if( $reportingLevel & E_WARNING ) { $errType = 'WARNING'; } break;
  54.             case E_PARSE: if( $reportingLevel & E_PARSE ) { $errType = 'PARSE'; } break;
  55.             case E_NOTICE: if( $reportingLevel & E_NOTICE ) { $errType = 'NOTICE'; } break;
  56.             case E_CORE_ERROR: if( $reportingLevel & E_CORE_ERROR ) { $errType = 'CORE_ERROR'; } break;
  57.             case E_CORE_WARNING: if( $reportingLevel & E_CORE_WARNING ) { $errType = 'CORE_WARNING'; } break;
  58.             case E_COMPILE_ERROR: if( $reportingLevel & E_COMPILE_ERROR ) { $errType = 'COMPILE_ERROR'; } break;
  59.             case E_COMPILE_WARNING: if( $reportingLevel & E_COMPILE_WARNING ) { $errType = 'COMPILE_WARNING'; } break;
  60.             case E_USER_ERROR: if( $reportingLevel & E_USER_ERROR ) { $errType = 'USER_ERROR'; } break;
  61.             case E_USER_WARNING: if( $reportingLevel & E_USER_WARNING ) { $errType = 'USER_WARNING'; } break;
  62.             case E_USER_NOTICE: if( $reportingLevel & E_USER_NOTICE ) { $errType = 'USER_NOTICE'; } break;
  63.             case E_STRICT: if( $reportingLevel & E_STRICT ) { $errType = 'STRICT'; } break;
  64.             case E_RECOVERABLE_ERROR: if( $reportingLevel & E_RECOVERABLE_ERROR ) { $errType = 'RECOVERABLE_ERROR'; } break;
  65.             case E_DEPRECATED: if( $reportingLevel & E_DEPRECATED ) { $errType = 'DEPRECATED'; } break;
  66.             case E_USER_DEPRECATED: if( $reportingLevel & E_USER_DEPRECATED ) { $errType = 'USER_DEPRECATED'; } break;
  67.             default: $errType = 'Error '.$errno; break;
  68.  
  69.         }
  70.         // Send an e-mail to the administrator
  71.         if( defined( 'IS_LIVE' ) && IS_LIVE && $errType && defined( 'ERROR_EMAIL' ) ) {
  72.             global $gBitDb;
  73.             $messageBody = $errType." [#$errno]: $errstr \n in $errfile on line $errline\n\n".bit_error_string( array( 'errno'=>$errno, 'db_msg'=>$errType ) ).vc( $_SERVER, FALSE );
  74.             mail( ERROR_EMAIL, 'PHP '.$errType.' on '.php_uname( 'n' ).': '.$errstr, $messageBody );
  75.         }
  76.     }
  77.  
  78.     // Execute PHP's internal error handler
  79.     return FALSE;
  80. }
  81.  
  82. function bit_shutdown_handler() {
  83.     $isError = false;
  84.     $error = error_get_last();
  85.  
  86.     if( $error && $error['type'] & (E_ERROR | E_PARSE | E_CORE_ERROR | E_COMPILE_ERROR | E_USER_ERROR | E_USER_ERROR) ){
  87.         header( "HTTP/1.0 500 Internal Server Error" );
  88.         print "Internal Server Error";
  89.         bit_error_handler( $error['type'], $error['message'], $error['file'], $error['line'] );
  90.     }
  91. }
  92.  
  93. register_shutdown_function('bit_shutdown_handler');
  94.  
  95.  
  96. function bit_display_error( $pLogMessage, $pSubject, $pFatal = TRUE ) {
  97.     global $gBitSystem;
  98.  
  99.     if( $pFatal ) {
  100.         header( $_SERVER["SERVER_PROTOCOL"].' '.HttpStatusCodes::getMessageForCode( HttpStatusCodes::HTTP_INTERNAL_SERVER_ERROR ) );
  101.     }
  102.  
  103.     // You can prevent sending of error emails by adding define('ERROR_EMAIL', ''); in your config/kernel/config_inc.php
  104.     $errorEmail = defined( 'ERROR_EMAIL' ) ? ERROR_EMAIL : (!empty( $_SERVER['SERVER_ADMIN'] ) ? $_SERVER['SERVER_ADMIN'] : NULL);
  105.  
  106.     error_log( $pLogMessage );
  107.  
  108.     if( ( !defined( 'IS_LIVE' ) || !IS_LIVE ) ) {
  109.         print '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "DTD/xhtml1-strict.dtd">
  110.             <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
  111.             <head>
  112.                 <title>bitweaver - White Screen of Death</title>
  113.             </head>
  114.             <body style="background:#fff; font-family:monospace;">';
  115. //         print "<h1>Upgrade Beta 1 to Beta 2</h1>If you are getting this error because you just upgraded your bitweaver from Beta 1 to Beta 2, please follow this link to the installer, which will guide you through the upgrade process: <a href='".BIT_ROOT_URL."install/install.php?step=4'>Upgrade Beta 1 to Beta 2</a>";
  116.         print "<h1 style=\"color:#900; font-weight:bold;\">You are running bitweaver in TEST mode</h1>\n";
  117.         print "
  118.             <ul>
  119.                 <li><a href='http://sourceforge.net/tracker/?func=add&amp;group_id=141358&amp;atid=749176'>Click here to log a bug</a>, if this appears to be an error with the application.</li>
  120.                 <li><a href='".BIT_ROOT_URL."install/install.php'>Go here to begin the installation process</a>, if you haven't done so already.</li>
  121.                 <li>To hide this message, please <strong>set the IS_LIVE constant to TRUE</strong> in your config/kernel/config_inc.php file.</li>
  122.             </ul>
  123.             <hr />
  124.         ";
  125.         print "<pre>".$pLogMessage."</pre>";
  126.         print "<hr />";
  127.         print "</body></html>";
  128.     } elseif( $errorEmail ) {
  129.         mail( $errorEmail,  "$pSubject", $pLogMessage );
  130.         if( defined( 'AUTO_BUG_SUBMIT' ) && AUTO_BUG_SUBMIT && !empty( $gBitSystem ) && $gBitSystem->isDatabaseValid() ) {
  131.             mail( 'bugs@bitweaver.org',"$pSubject",$pLogMessage );
  132.         }
  133.     }
  134.  
  135.     if( $pFatal ) {
  136.         die();
  137.     }
  138. }
  139.  
  140. function bit_error_string( $iDBParms ) {
  141.     global $gBitDb;
  142.     global $gBitUser;
  143.     global $_SERVER;
  144.     global $argv;
  145.  
  146.     $separator = "\n";
  147.     $indent = "  ";
  148.  
  149.     $date = date("D M d H:i:s Y"); // [Tue Sep 24 12:19:20 2002] [error]
  150.  
  151.     if( isset( $gBitUser->mInfo ) ) {
  152.         $acctStr = "ID: ".$gBitUser->mInfo['user_id']." - Login: ".$gBitUser->mInfo['login']." - e-mail: ".$gBitUser->mInfo['email'];
  153.     } else {
  154.         $acctStr = "User unknown";
  155.     }
  156.  
  157.     $info  = $indent."[ - ".BIT_MAJOR_VERSION.".".BIT_MINOR_VERSION.".".BIT_SUB_VERSION." ".BIT_LEVEL." - ] [ $date ]".$separator;
  158.     $info .= $indent."-----------------------------------------------------------------------------------------------".$separator;
  159.     $info .= $indent."#### USER AGENT: ".$_SERVER['HTTP_USER_AGENT'].$separator;
  160.     $info .= $indent."#### ACCT: ".$acctStr.$separator;
  161.     $uri = '';
  162.     if( !empty( $_SERVER['SCRIPT_URI'] ) ) {
  163.         $uri = $_SERVER['SCRIPT_URI'].(!empty($_SERVER['QUERY_STRING'])?'?'.$_SERVER['QUERY_STRING']:'').$separator;
  164.     } elseif( !empty( $argv ) ) {
  165.         $uri = implode( ' ', $argv );
  166.     }
  167.     $info .= $indent."#### URL: ".$uri;
  168.     if( isset($_SERVER['HTTP_REFERER'] ) ) {
  169.         $info .= $indent."#### REFERRER: $_SERVER[HTTP_REFERER]".$separator;
  170.     }
  171.     $info .= $indent."#### HOST: $_SERVER[HTTP_HOST]".$separator;
  172.     $info .= $indent."#### IP: $_SERVER[REMOTE_ADDR]".$separator;
  173.     if( !empty( $gBitDb ) ) {
  174.         $info .= $indent."#### DB: ".$gBitDb->mDb->databaseType.'://'.$gBitDb->mDb->user.'@'.$gBitDb->mDb->host.'/'.$gBitDb->mDb->database.$separator;
  175.     }
  176.  
  177.     if( $gBitDb && isset( $php_errormsg ) ) {
  178.         $info .= $indent."#### PHP: ".$php_errormsg.$separator;
  179.     }
  180.  
  181.     if ( !empty( $iDBParms['sql'] ) ) {
  182.         $badSpace = array("\n", "\t");
  183.         $info .= $indent."#### SQL: ".str_replace($badSpace, ' ', $iDBParms['sql']).$separator;
  184.         if( is_array( $iDBParms['p2'] ) ) {
  185.             $info .= $indent.'['.implode( ', ', $iDBParms['p2'] ).']'.$separator;
  186.         }
  187.     }
  188.  
  189.     $errno = (!empty( $iDBParms['errno'] ) ? 'Errno: '.$iDBParms['errno'] : '');
  190.     if( !empty( $iDBParms['db_msg'] ) ) {
  191.         $info .= $indent."#### ERROR CODE: ".$errno."  Message: ".$iDBParms['db_msg'];
  192.     }
  193.  
  194.     $stackTrace = bt( 9999, FALSE );
  195.  
  196.     //multiline expressions matched
  197.     if( preg_match_all( "/.*adodb_error_handler\([^\}]*\)(.+\}.+)/ms", $stackTrace, $match )) {
  198.         $stackTrace = $match[1][0];
  199.     }
  200.  
  201.     $globalVars = array(
  202.         '$_POST'   => $_POST,
  203.         '$_GET'    => $_GET,
  204.         '$_FILES'  => $_FILES,
  205.         '$_COOKIE' => $_COOKIE,
  206.     );
  207.  
  208.     $parameters = '';
  209.     foreach( $globalVars as $global => $hash ) {
  210.         if( !empty( $hash )) {
  211.             $parameters .= $separator.$separator.$global.': '.$separator.var_export( $hash, TRUE );
  212.         }
  213.     }
  214.     $parameters = preg_replace( "/\n/", $separator.$indent, $parameters );
  215.  
  216.     $ret = $info.$separator.$separator.$stackTrace.$parameters;
  217.  
  218.     return $ret;
  219. }
  220.  
  221. if (!function_exists('bt')) {    // Make sure another backtrace function does not exist
  222. function bt( $levels=9999, $iPrint=TRUE ) {
  223.     $s = '';
  224.     if (PHPVERSION() >= 4.3) {
  225.  
  226.         $MAXSTRLEN = 64;
  227.  
  228.         $traceArr = debug_backtrace();
  229.         array_shift($traceArr);
  230.         $tabs = sizeof($traceArr)-1;
  231.         $indent = '';
  232.         $sClass = '';
  233.  
  234.         foreach ($traceArr as $arr) {
  235.             $levels -= 1;
  236.             if ($levels < 0) break;
  237.  
  238.             $args = array();
  239.             for ($i=0; $i <= $tabs; $i++) {
  240.                 $indent .= '}';
  241.             }
  242.             $tabs -= 1;
  243.             if ( isset($arr['class']) ) {
  244.                 $sClass .= $arr['class'].'::';
  245.             }
  246.             if ( isset($arr['args']) ) {
  247.                 foreach( $arr['args'] as $v ) {
  248.                     if (is_null($v) ) {
  249.                         $args[] = 'null';
  250.                     } elseif (is_array($v)) { $args[] = 'Array['.sizeof($v).']';
  251.                     } elseif (is_object($v)) { $args[] = 'Object:'.get_class($v);
  252.                     } elseif (is_bool($v)) { $args[] = $v ? 'true' : 'false';
  253.                     } else {
  254.                         $v = (string) @$v;
  255.                         $str = htmlspecialchars(substr($v,0,$MAXSTRLEN));
  256.                         if (strlen($v) > $MAXSTRLEN) $str .= '...';
  257.                         $args[] = $str;
  258.                     }
  259.                 }
  260.             }
  261.             if( !preg_match( "*include*", $arr['function'] ) && !preg_match( "*silentlog*", strtolower( $arr['function'] ) ) ) {
  262.                 $s .= "\n    ".$indent.'    -> ';
  263.                 $s .= $sClass.$arr['function'].'('.implode(', ',$args).')';
  264.             }
  265.             $s .= "\n    ".$indent;
  266.             $s .= @sprintf(" LINE: %4d, %s", $arr['line'],$arr['file']);
  267.             $indent = '';
  268.         }
  269.         $s .= "\n";
  270.         if( $iPrint ) {
  271.             print '<pre>'.$s."</pre>\n";
  272.         }
  273.     }
  274.     return $s;
  275. }
  276. }    // End if function_exists('bt')
  277.  
  278. // variable argument var dump
  279. function vvd() {
  280.     for( $i = 0; $i < func_num_args(); $i++ ) { 
  281.         vd( func_get_arg( $i ) );
  282.     } 
  283. }
  284.  
  285. // var dump variable in something nicely readable in web browser
  286. function vd( $pVar, $pGlobals=FALSE, $pDelay=FALSE ) {
  287.     global $gBitSystem;
  288.  
  289.     ob_start();
  290.     if( $pGlobals ) {
  291.         print '<h2>$pVar</h2>';
  292.     }
  293.     print vc( $pVar );
  294.     if( $pGlobals ) {
  295.         if( !empty( $_GET )) {
  296.             print '<h2>$_GET</h2>';
  297.             print vc( $_GET );
  298.         }
  299.         if( !empty( $_POST )) {
  300.             print '<h2>$_POST</h2>';
  301.             print vc( $_POST );
  302.         }
  303.         if( !empty( $_FILES )) {
  304.             print '<h2>$_FILES</h2>';
  305.             print vc( $_FILES );
  306.         }
  307.         if( !empty( $_COOKIE )) {
  308.             print '<h2>$_COOKIE</h2>';
  309.             print vc( $_COOKIE );
  310.         }
  311.     }
  312.     if($pDelay) {
  313.         $gBitSystem->mDebugHtml .= ob_get_contents();
  314.         ob_end_clean();
  315.     } else {
  316.         ob_end_flush();
  317.     }
  318.     flush();
  319. }
  320.  
  321. // var capture variable in something nicely readable in web browser
  322. function vc( $iVar, $pHtml=TRUE ) {
  323.     ob_start();
  324.     if( is_object( $iVar ) ) {
  325.         if( isset( $iVar->mDb ) ) {
  326.             unset( $iVar->mDb );
  327.         }
  328.     }
  329.  
  330.     // xdebug rocks!
  331.     if( extension_loaded( 'xdebug' ) ) {
  332.         if( empty( $pHtml ) ) {
  333.             ini_set( 'xdebug.overload_var_dump', FALSE );
  334.         }
  335.         var_dump( $iVar );
  336.     } elseif( $pHtml && !empty( $_SERVER['HTTP_USER_AGENT'] ) && $_SERVER['HTTP_USER_AGENT'] != 'cron' && ((is_object( $iVar ) && !empty( $iVar )) || is_array( $iVar )) ) {
  337.         include_once( UTIL_PKG_PATH.'dBug/dBug.php' );
  338.         new dBug( $iVar, "", FALSE );
  339.     } else {
  340.         print '<pre>';
  341.         if( is_object( $iVar ) ) {
  342.             var_dump( $iVar );
  343.         } elseif( is_string( $iVar ) && !empty( $_SERVER['HTTP_USER_AGENT'] ) && $_SERVER['HTTP_USER_AGENT'] != 'cron' ) {
  344.             var_dump( htmlentities( $iVar ) );
  345.         } else {
  346.             var_dump( $iVar );
  347.         }
  348.         print "</pre>\n";
  349.     }
  350.     $ret = ob_get_contents();
  351.     ob_end_clean();
  352.     return $ret;
  353. }
  354.  
  355.  
  356. function va( $iVar ) {
  357.     $dbg = var_export($iVar, 1);
  358.     $dbg = highlight_string("<?\n". $dbg."\n?>", 1);
  359.     echo "<div><span style='background-color:black;color:white;padding:.5ex;font-weight:bold;'>Var Anatomy</div>";
  360.     echo "<div style='border:1px solid black;padding:2ex;background-color:#efe6d6;'>$dbg</div>";
  361. }
  362.  
  363. /**
  364.  * bitdebug display an debug output when $gDebug is set to TRUE
  365.  *
  366.  * @param array $pMessage Message to display
  367.  * @access public
  368.  * @return TRUE on success, FALSE on failure - mErrors will contain reason for failure
  369.  */
  370. function bitdebug( $pMessage ) {
  371.     global $gDebug;
  372.     if( !empty( $gDebug )) {
  373.         echo "<pre>$pMessage</pre>";
  374.     }
  375. }

Documentation generated on Wed, 29 Jul 2015 13:56:11 +0000 by phpDocumentor 1.5.0-lsces