debug( $pLevel ); } } /** * Output in a pseudo standard output format * * LogFormat "%V %h %l %{USERID}e %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-agent}i\" \"%{Cookie}n\"" combinedcookie * **/ function bit_print_log( $pLogParams, $pLogMessages ) { global $gBitUser; $virtualHost = BitBase::getParameter( $pLogParams, 'hostname', $_SERVER['HTTP_HOST'] ); $remoteAddr = BitBase::getParameter( $pLogParams, 'remote_addr', $_SERVER['REMOTE_ADDR'] ); $userAgent = BitBase::getParameter( $pLogParams, 'user_agent', $_SERVER['HTTP_USER_AGENT'] ); $statusCode = BitBase::getParameter( $pLogParams, 'status_code', 200 ); $scriptFilename = BitBase::getParameter( $pLogParams, 'script_uri', $_SERVER['SCRIPT_FILENAME'] ); $ident = BitBase::getParameter( $pLogParams, 'ident', '-' ); $userName = BitBase::getParameter( $pLogParams, 'username', $gBitUser->getField('username') ); $executionTime = BitBase::getParameter( $pLogParams, 'exectime', '-' ); $logTimestamp = BitBase::getParameter( $pLogParams, 'timestamp', date( '[d/M/Y:H:i:s O]' ) ); for( $i = 1; $i < func_num_args(); $i++ ) { if( $pLogMessage = func_get_arg( $i ) ) { $errlines = explode( "\n", \is_array( $pLogMessage ) || \is_object( $pLogMessage ) ? vc( $pLogMessage, false ) : $pLogMessage); foreach ($errlines as $txt) { print "$virtualHost $remoteAddr $ident $userName $logTimestamp \"$scriptFilename\" $statusCode $executionTime \"$userAgent\" $pLogMessage\n"; } } else { print "$virtualHost $remoteAddr $ident $userName $logTimestamp \"$scriptFilename\" $statusCode $executionTime \"$userAgent\"\n"; } } } function bit_error_log() { for( $i = 0; $i < func_num_args(); $i++ ) { if( $pLogMessage = func_get_arg( $i ) ) { $errlines = explode( "\n", \is_array( $pLogMessage ) || \is_object( $pLogMessage ) ? vc( $pLogMessage, false ) : $pLogMessage); foreach ($errlines as $txt) { error_log($txt); } } } error_log( 'SCRIPT_URI: '.BitBase::getParameter( $_SERVER, 'SCRIPT_URI', 'OUTPUT' )."\n".bit_stack( 5 ) ); } function emergency_break( ) { global $gBitSystem; if( \is_object( $gBitSystem ) ) { $gBitSystem->setHttpStatus( HttpStatusCodes::HTTP_BAD_REQUEST ); } vd( 'EMERGENCY BREAK' ); foreach (\func_get_args () as $arg){ vd( $arg ); } bt(); die; } if( !function_exists( 'eb' ) ) { function eb() { emergency_break( \func_get_args() ); } } function bit_error_email ( $pSubject, $pMessage, $pGlobalVars=[] ) { // You can prevent sending of error emails by adding define('ERROR_EMAIL', ''); in your config/kernel/config_inc.php $errorEmail = \defined( 'ERROR_EMAIL' ) ? ERROR_EMAIL : (!empty( $_SERVER['SERVER_ADMIN'] ) ? $_SERVER['SERVER_ADMIN'] : null); $separator = "\n"; $indent = " "; $parameters = ''; if( empty( $pGlobalVars ) ) { $pGlobalVars = [ '$_POST' => $_POST, '$_GET' => $_GET, '$_FILES' => $_FILES, '$_COOKIE' => $_COOKIE, ]; } foreach( $pGlobalVars as $global => $hash ) { if( !empty( $hash )) { $parameters .= "$separator$global: $separator".var_export( $hash, true ).$separator; } } $parameters = preg_replace( "/\n/", $separator.$indent, $parameters ); mail( $errorEmail, $pSubject, $pMessage.$parameters.$separator.$separator.'$_SERVER: '.var_export( $_SERVER, true ) ); } function bit_error_handler ( $errno, $errstr, $errfile, $errline, $errcontext=null ) { // error_reporting() === 0 if code was prepended with @ $reportingLevel = error_reporting(); if( $reportingLevel !== 0 && !strpos( $errfile, 'templates_c' ) ) { $errType = 'Error'; $isReported = true; switch ($errno) { case E_ERROR: $errType = 'FATAL ERROR'; break; case E_WARNING: $isReported = $reportingLevel & E_WARNING; $errType = 'WARNING'; break; case E_PARSE: $isReported = $reportingLevel & E_PARSE; $errType = 'PARSE'; break; case E_NOTICE: $isReported = $reportingLevel & E_NOTICE; $errType = 'NOTICE'; break; case E_CORE_ERROR: $isReported = $reportingLevel & E_CORE_ERROR; $errType = 'CORE_ERROR'; break; case E_CORE_WARNING: $isReported = $reportingLevel & E_CORE_WARNING; $errType = 'CORE_WARNING'; break; case E_COMPILE_ERROR: $isReported = $reportingLevel & E_COMPILE_ERROR; $errType = 'COMPILE_ERROR'; break; case E_COMPILE_WARNING: $isReported = $reportingLevel & E_COMPILE_WARNING; $errType = 'COMPILE_WARNING'; break; case E_USER_ERROR: $isReported = $reportingLevel & E_USER_ERROR; $errType = 'USER_ERROR'; break; case E_USER_WARNING: $isReported = $reportingLevel & E_USER_WARNING; $errType = 'USER_WARNING'; break; case E_USER_NOTICE: $isReported = $reportingLevel & E_USER_NOTICE; $errType = 'USER_NOTICE'; break; case E_RECOVERABLE_ERROR: $isReported = $reportingLevel & E_RECOVERABLE_ERROR; $errType = 'RECOVERABLE_ERROR'; break; case E_DEPRECATED: $isReported = $reportingLevel & E_DEPRECATED; $errType = 'DEPRECATED'; break; case E_USER_DEPRECATED: $isReported = $reportingLevel & E_USER_DEPRECATED; $errType = 'USER_DEPRECATED'; break; default: $errType = 'Error '.$errno; break; } if( $isReported ) { //eb( $isReported, $errType, $errno, $reportingLevel, $errfile ); $errorSubject = 'PHP '.$errType.' on '.php_uname( 'n' ).': '.$errstr; $errorString = $errType." [#$errno]: $errstr \n in $errfile on line $errline\n\n".bit_error_string( [ 'errno'=>$errno, 'db_msg'=>$errType ] ); if( \defined( 'IS_LIVE' ) && IS_LIVE ) { if( \defined( 'ERROR_EMAIL' ) ) { // Send an e-mail to the administrator bit_error_email( $errorSubject, $errorString ); } else { error_log( $errorString ); } } else { if( $errType == E_ERROR ) { eb( $errorSubject, $errorString ); } else { bt( $errorSubject ); } } } } // Execute PHP's internal error handler return false; } function bit_shutdown_handler() { $isError = false; $error = error_get_last(); if( $error && $error['type'] & (E_ERROR | E_PARSE | E_CORE_ERROR | E_COMPILE_ERROR | E_USER_ERROR | E_USER_ERROR) ){ header( "HTTP/1.0 500 Internal Server Error" ); print "Internal Server Error"; bit_error_handler( $error['type'], $error['message'], $error['file'], $error['line'] ); } } register_shutdown_function('Bitweaver\bit_shutdown_handler'); function bit_display_error( $pLogMessage, $pSubject, $pFatal = true ) { global $gBitSystem; if( $pFatal ) { header( $_SERVER["SERVER_PROTOCOL"].' '.HttpStatusCodes::getMessageForCode( HttpStatusCodes::HTTP_INTERNAL_SERVER_ERROR ) ); } error_log( $pLogMessage ); if( ( !\defined( 'IS_LIVE' ) || !IS_LIVE ) ) { print '
".$pLogMessage.""; print "
\t".date( "Y-m-d H:i:s" )."\n".bit_stack()."\n"; } } // End if function_exists('bt') function bit_stack( $pDepth = 999 ) { $s = ''; if (PHPVERSION() >= 4.3) { $MAXSTRLEN = 128; $traceArr = debug_backtrace(); array_shift($traceArr); $tabs = \sizeof($traceArr)-1; $indent = ''; $sClass = ''; $levels = $pDepth; foreach ($traceArr as $arr) { $levels -= 1; if( $levels < 0 ) { break; } $args = []; for ($i=0; $i <= $tabs; $i++) { $indent .= '}'; } $tabs -= 1; if ( isset($arr['class']) ) { $sClass .= $arr['class'].'::'; } if ( isset($arr['args']) ) { foreach( $arr['args'] as $v ) { if (\is_null($v) ) { $args[] = 'null'; } elseif (\is_array($v)) { $args[] = 'Array['.\sizeof($v).']'; } elseif (\is_object($v)) { $args[] = 'Object:'.$v::class; } elseif (\is_bool($v)) { $args[] = $v ? 'true' : 'false'; } else { $v = (string) @$v; $str = htmlspecialchars(substr($v,0,$MAXSTRLEN)); if (\strlen($v) > $MAXSTRLEN) $str .= '...'; $args[] = $str; } } } if( !preg_match( "*include*", $arr['function'] ) && !preg_match( "*silentlog*", strtolower( $arr['function'] ) ) ) { $s .= "\n ".$indent.' -> '; $s .= $sClass.$arr['function'].'('.implode(', ',$args).')'; } $s .= "\n $indent"; $s .= @\sprintf(" LINE: %4d, %s", !empty($arr['line']) ? $arr['line'] : 'none', !empty($arr['file']) ? $arr['file'] : 'none'); $indent = ''; } $s .= "\n"; } return $s; } // variable argument var dump function vvd() { for( $i = 0; $i < \func_num_args(); $i++ ) { vd( func_get_arg( $i ) ); } } // var dump variable in something nicely readable in web browser function vd( $pVar, $pGlobals=false, $pDelay=false ) { global $gBitSystem; ob_start(); if( $pGlobals ) { print '
';
if( \is_object( $iVar ) ) {
var_dump( $iVar );
} elseif( is_string( $iVar ) && !empty( $_SERVER['HTTP_USER_AGENT'] ) && $_SERVER['HTTP_USER_AGENT'] != 'cron' ) {
var_dump( htmlentities( $iVar ) );
} else {
var_dump( $iVar );
}
print "\n";
}
$ret = ob_get_contents();
ob_end_clean();
return "$ret\n";
}
function va( $iVar ) {
$dbg = var_export($iVar, 1);
$dbg = highlight_string( "\n$dbg\n?>", 1);
echo "$pMessage"; } }