Index: branches/5.2.x/core/kernel/utility/logger.php =================================================================== --- branches/5.2.x/core/kernel/utility/logger.php (revision 16543) +++ branches/5.2.x/core/kernel/utility/logger.php (revision 16544) @@ -1,1413 +1,1477 @@ 'logger.php', 'kErrorHandlerStack' => 'logger.php', 'kExceptionHandlerStack' => 'logger.php', 'kDBConnection' => 'db_connection.php', 'kDBConnectionDebug' => 'db_connection.php', 'kDBLoadBalancer' => 'db_load_balancer.php', ); /** * Create event log * * @param Array $methods_to_call List of invokable kLogger class method with their parameters (if any) * @access public */ public function __construct($methods_to_call = Array ()) { parent::__construct(); $system_config = kUtil::getSystemConfig(); $this->_debugMode = $this->Application->isDebugMode(); $this->setState($system_config->get('EnableSystemLog', self::STATE_DISABLED)); $this->_maxLogLevel = $system_config->get('SystemLogMaxLevel', self::LL_NOTICE); foreach ($methods_to_call as $method_to_call) { call_user_func_array(Array ($this, $method_to_call[0]), $method_to_call[1]); } if ( !kUtil::constOn('DBG_ZEND_PRESENT') && !$this->Application->isDebugMode() ) { // don't report error on screen if debug mode is turned off error_reporting(0); ini_set('display_errors', 0); } register_shutdown_function(Array ($this, 'catchLastError')); } /** * Sets state of the logged (enabled/user-only/disabled) * * @param $new_state * @return void * @access public */ public function setState($new_state = null) { if ( isset($new_state) ) { $this->_state = (int)$new_state; } if ( $this->_state === self::STATE_ENABLED ) { $this->_enableErrorHandling(); } elseif ( $this->_state === self::STATE_DISABLED ) { $this->_disableErrorHandling(); } } /** * Enable error/exception handling capabilities * * @return void * @access protected */ protected function _enableErrorHandling() { $this->_disableErrorHandling(); $this->_handlers[self::LL_ERROR] = new kErrorHandlerStack($this); $this->_handlers[self::LL_CRITICAL] = new kExceptionHandlerStack($this); } /** * Disables error/exception handling capabilities * * @return void * @access protected */ protected function _disableErrorHandling() { foreach ($this->_handlers as $index => $handler) { $this->_handlers[$index]->__destruct(); unset($this->_handlers[$index]); } } /** * Initializes new log record. Use "kLogger::write" to save to db/disk * * @param string $message * @param int $code * @return kLogger * @access public */ public function prepare($message = '', $code = null) { $this->_logRecord = Array ( 'LogUniqueId' => kUtil::generateId(), 'LogMessage' => $message, 'LogLevel' => self::LL_INFO, 'LogCode' => $code, 'LogType' => self::LT_OTHER, 'LogHostname' => $_SERVER['HTTP_HOST'], 'LogRequestSource' => php_sapi_name() == 'cli' ? 2 : 1, 'LogRequestURI' => php_sapi_name() == 'cli' ? implode(' ', $GLOBALS['argv']) : $_SERVER['REQUEST_URI'], 'LogUserId' => USER_GUEST, 'IpAddress' => isset($_SERVER['REMOTE_ADDR']) ? $_SERVER['REMOTE_ADDR'] : '', 'LogSessionKey' => 0, 'LogProcessId' => getmypid(), 'LogUserData' => '', 'LogNotificationStatus' => self::LNS_DISABLED, ); if ( $this->Application->isAdmin ) { $this->_logRecord['LogInterface'] = defined('CRON') && CRON ? self::LI_CRON_ADMIN : self::LI_ADMIN; } else { $this->_logRecord['LogInterface'] = defined('CRON') && CRON ? self::LI_CRON_FRONT : self::LI_FRONT; } if ( $this->Application->InitDone ) { $this->_logRecord['LogUserId'] = $this->Application->RecallVar('user_id'); $this->_logRecord['LogSessionKey'] = $this->Application->GetSID(); $this->_logRecord['IpAddress'] = $this->Application->getClientIp(); } return $this; } /** * Sets one or more fields of log record * * @param string|Array $field_name * @param string|null $field_value * @return kLogger * @access public * @throws UnexpectedValueException */ public function setLogField($field_name, $field_value = null) { if ( isset($field_value) ) { $this->_logRecord[$field_name] = $field_value; } elseif ( is_array($field_name) ) { $this->_logRecord = array_merge($this->_logRecord, $field_name); } else { throw new UnexpectedValueException('Invalid arguments'); } return $this; } /** * Sets user data * * @param string $data * @param bool $as_array * @return kLogger * @access public */ public function setUserData($data, $as_array = false) { if ( $as_array ) { $data = serialize((array)$data); } return $this->setLogField('LogUserData', $data); } /** * Add user data * * @param string $data * @param bool $as_array * @return kLogger * @access public */ public function addUserData($data, $as_array = false) { $new_data = $this->_logRecord['LogUserData']; if ( $as_array ) { $new_data = $new_data ? unserialize($new_data) : Array (); $new_data[] = $data; $new_data = serialize($new_data); } else { $new_data .= ($new_data ? PHP_EOL : '') . $data; } return $this->setLogField('LogUserData', $new_data); } /** * Adds event to log record * * @param kEvent $event * @return kLogger * @access public */ public function addEvent(kEvent $event) { $this->_logRecord['LogEventName'] = (string)$event; return $this; } /** * Adds log source file & file to log record * * @param string|Array $file_or_trace file path * @param int $line file line * @return kLogger * @access public */ public function addSource($file_or_trace = '', $line = 0) { if ( is_array($file_or_trace) ) { $trace_info = $file_or_trace[0]; $this->_logRecord['LogSourceFilename'] = $trace_info['file']; $this->_logRecord['LogSourceFileLine'] = $trace_info['line']; } else { $this->_logRecord['LogSourceFilename'] = $file_or_trace; $this->_logRecord['LogSourceFileLine'] = $line; } return $this; } /** * Adds session contents to log record * * @param bool $include_optional Include optional session variables * @return kLogger * @access public */ public function addSessionData($include_optional = false) { if ( $this->Application->InitDone ) { $this->_logRecord['LogSessionData'] = serialize($this->Application->Session->getSessionData($include_optional)); } return $this; } /** * Adds user request information to log record * * @return kLogger * @access public */ public function addRequestData() { $request_data = array( 'Headers' => $this->Application->HttpQuery->getHeaders(), ); $request_variables = Array('_GET' => $_GET, '_POST' => $_POST, '_COOKIE' => $_COOKIE); foreach ( $request_variables as $title => $data ) { if ( !$data ) { continue; } $request_data[$title] = $data; } $this->_logRecord['LogRequestData'] = serialize($request_data); return $this; } /** * Adds trace to log record * * @param Array $trace * @param int $skip_levels * @param Array $skip_files * @return kLogger * @access public */ public function addTrace($trace = null, $skip_levels = 1, $skip_files = null) { $trace = $this->createTrace($trace, $skip_levels, $skip_files); foreach ($trace as $trace_index => $trace_info) { if ( isset($trace_info['args']) ) { $trace[$trace_index]['args'] = $this->_implodeObjects($trace_info['args']); } } $this->_logRecord['LogBacktrace'] = serialize($this->_removeObjectsFromTrace($trace)); return $this; } /** * Remove objects from trace, since before PHP 5.2.5 there wasn't possible to remove them initially * * @param Array $trace * @return Array * @access protected */ protected function _removeObjectsFromTrace($trace) { if ( version_compare(PHP_VERSION, '5.3', '>=') ) { return $trace; } $trace_indexes = array_keys($trace); foreach ($trace_indexes as $trace_index) { unset($trace[$trace_index]['object']); } return $trace; } /** * Implodes object to prevent memory leaks * * @param Array $array * @return Array * @access protected */ protected function _implodeObjects($array) { $ret = Array (); foreach ($array as $key => $value) { if ( is_array($value) ) { $ret[$key] = $this->_implodeObjects($value); } elseif ( is_object($value) ) { if ( $value instanceof kEvent ) { $ret[$key] = 'Event: ' . (string)$value; } elseif ( $value instanceof kBase ) { $ret[$key] = (string)$value; } else { $ret[$key] = 'Class: ' . get_class($value); } } elseif ( strlen($value) > 200 ) { $ret[$key] = substr($value, 0, 50) . ' ...'; } else { $ret[$key] = $value; } } return $ret; } /** * Removes first N levels from trace * * @param Array $trace * @param int $levels * @param Array $files * @return Array * @access public */ public function createTrace($trace = null, $levels = null, $files = null) { if ( !isset($trace) ) { $trace = debug_backtrace(false); } if ( !$trace ) { // no trace information return $trace; } if ( isset($levels) && is_numeric($levels) ) { for ($i = 0; $i < $levels; $i++) { array_shift($trace); } } if ( isset($files) && is_array($files) ) { $classes = array_keys($files); while ( true ) { $trace_info = $trace[0]; $file = isset($trace_info['file']) ? basename($trace_info['file']) : ''; $class = isset($trace_info['class']) ? $trace_info['class'] : ''; if ( ($file && !in_array($file, $files)) || ($class && !in_array($class, $classes)) ) { break; } array_shift($trace); } } return $trace; } /** * Adds PHP error to log record * * @param int $errno * @param string $errstr * @param string $errfile * @param int $errline * @return kLogger * @access public */ public function addError($errno, $errstr, $errfile = null, $errline = null) { $errstr = self::expandMessage($errstr, !$this->_debugMode); $this->_logRecord['LogLevel'] = $this->_getLogLevelByErrorNo($errno); if ( $this->isLogType(self::LT_DATABASE, $errstr) ) { list ($errno, $errstr, $sql) = self::parseDatabaseError($errstr); $this->_logRecord['LogType'] = self::LT_DATABASE; $this->_logRecord['LogUserData'] = $sql; $trace = $this->createTrace(null, 4, $this->_ignoreInTrace); $this->addSource($trace); $this->addTrace($trace, 0); } else { $this->_logRecord['LogType'] = self::LT_PHP; $this->addSource((string)$errfile, $errline); $this->addTrace(null, 4); } $this->_logRecord['LogCode'] = $errno; $this->_logRecord['LogMessage'] = $errstr; return $this; } /** * Adds PHP exception to log record * * @param Exception $exception * @return kLogger * @access public */ public function addException($exception) { $errstr = self::expandMessage($exception->getMessage(), !$this->_debugMode); $this->_logRecord['LogLevel'] = self::LL_CRITICAL; $exception_trace = $exception->getTrace(); array_unshift($exception_trace, array( 'function' => '', 'file' => $exception->getFile() !== null ? $exception->getFile() : 'n/a', 'line' => $exception->getLine() !== null ? $exception->getLine() : 'n/a', 'args' => array(), )); if ( $this->isLogType(self::LT_DATABASE, $errstr) ) { list ($errno, $errstr, $sql) = self::parseDatabaseError($errstr); $this->_logRecord['LogType'] = self::LT_DATABASE; $this->_logRecord['LogUserData'] = $sql; $trace = $this->createTrace($exception_trace, null, $this->_ignoreInTrace); $this->addSource($trace); $this->addTrace($trace, 0); } else { $this->_logRecord['LogType'] = self::LT_PHP; $errno = $exception->getCode(); $this->addSource((string)$exception->getFile(), $exception->getLine()); $this->addTrace($exception_trace, 0); } $this->_logRecord['LogCode'] = $errno; $this->_logRecord['LogMessage'] = $errstr; return $this; } /** * Allows to map PHP error numbers to syslog log level * * @param int $errno * @return int * @access protected */ protected function _getLogLevelByErrorNo($errno) { $error_number_mapping = Array ( self::LL_ERROR => Array (E_RECOVERABLE_ERROR, E_USER_ERROR, E_ERROR, E_CORE_ERROR, E_COMPILE_ERROR, E_PARSE), self::LL_WARNING => Array (E_WARNING, E_USER_WARNING, E_CORE_WARNING, E_COMPILE_WARNING), self::LL_NOTICE => Array (E_NOTICE, E_USER_NOTICE, E_STRICT), ); if ( version_compare(PHP_VERSION, '5.3.0', '>=') ) { $error_number_mapping[self::LL_NOTICE][] = E_DEPRECATED; $error_number_mapping[self::LL_NOTICE][] = E_USER_DEPRECATED; } foreach ($error_number_mapping as $log_level => $error_numbers) { if ( in_array($errno, $error_numbers) ) { return $log_level; } } return self::LL_ERROR; } /** * Changes log level of a log record * * @param int $log_level * @return kLogger * @access public */ public function setLogLevel($log_level) { $this->_logRecord['LogLevel'] = $log_level; return $this; } /** * Writes prepared log to database or disk, when database isn't available * - * @param int $storage_medium - * @return bool|int - * @access public - * @throws InvalidArgumentException + * @param integer $storage_medium Storage medium. + * @param boolean $check_origin Check error origin. + * + * @return integer|boolean + * @throws InvalidArgumentException When unknown storage medium is given. */ - public function write($storage_medium = self::LS_AUTOMATIC) + public function write($storage_medium = self::LS_AUTOMATIC, $check_origin = false) { - if ( !$this->_logRecord || $this->_logRecord['LogLevel'] > $this->_maxLogLevel || $this->_state == self::STATE_DISABLED ) { - // nothing to save OR less detailed logging requested OR disabled + if ( $check_origin && isset($this->_logRecord['LogSourceFilename']) ) { + $origin_allowed = self::isErrorOriginAllowed($this->_logRecord['LogSourceFilename']); + } + else { + $origin_allowed = true; + } + + if ( !$this->_logRecord + || $this->_logRecord['LogLevel'] > $this->_maxLogLevel + || !$origin_allowed + || $this->_state == self::STATE_DISABLED + ) { + // Nothing to save OR less detailed logging requested OR origin not allowed OR disabled. + $this->_logRecord = array(); + return false; } $this->_logRecord['LogMemoryUsed'] = memory_get_usage(); $this->_logRecord['LogTimestamp'] = adodb_mktime(); $this->_logRecord['LogDate'] = adodb_date('Y-m-d H:i:s'); if ( $storage_medium == self::LS_AUTOMATIC ) { $storage_medium = $this->Conn->connectionOpened() ? self::LS_DATABASE : self::LS_DISK; } if ( $storage_medium == self::LS_DATABASE ) { $result = $this->Conn->doInsert($this->_logRecord, TABLE_PREFIX . 'SystemLog'); } elseif ( $storage_medium == self::LS_DISK ) { $result = $this->_saveToFile(RESTRICTED . '/system.log'); } else { throw new InvalidArgumentException('Unknown storage medium "' . $storage_medium . '"'); } $unique_id = $this->_logRecord['LogUniqueId']; if ( $this->_logRecord['LogNotificationStatus'] == self::LNS_SENT ) { $this->_sendNotification($unique_id); } $this->_logRecord = Array (); return $result ? $unique_id : false; } /** * Catches last error happened before script ended * * @return void * @access public */ public function catchLastError() { $this->write(); $last_error = error_get_last(); if ( !is_null($last_error) && isset($this->_handlers[self::LL_ERROR]) ) { /** @var kErrorHandlerStack $handler */ $handler = $this->_handlers[self::LL_ERROR]; $handler->handle($last_error['type'], $last_error['message'], $last_error['file'], $last_error['line']); } } /** * Deletes log with given id from database or disk, when database isn't available * * @param int $unique_id * @param int $storage_medium * @return void * @access public * @throws InvalidArgumentException */ public function delete($unique_id, $storage_medium = self::LS_AUTOMATIC) { if ( $storage_medium == self::LS_AUTOMATIC ) { $storage_medium = $this->Conn->connectionOpened() ? self::LS_DATABASE : self::LS_DISK; } if ( $storage_medium == self::LS_DATABASE ) { $sql = 'DELETE FROM ' . TABLE_PREFIX . 'SystemLog WHERE LogUniqueId = ' . $unique_id; $this->Conn->Query($sql); } elseif ( $storage_medium == self::LS_DISK ) { // TODO: no way to delete a line from a file } else { throw new InvalidArgumentException('Unknown storage medium "' . $storage_medium . '"'); } } /** * Send notification (delayed or instant) about log record to e-mail from configuration * * @param bool $instant * @return kLogger * @access public */ public function notify($instant = false) { $this->_logRecord['LogNotificationStatus'] = $instant ? self::LNS_SENT : self::LNS_PENDING; return $this; } /** * Sends notification e-mail about message with given $unique_id * * @param int $unique_id * @return void * @access protected */ protected function _sendNotification($unique_id) { $notification_email = $this->Application->ConfigValue('SystemLogNotificationEmail'); if ( !$notification_email ) { trigger_error('System Log notification E-mail not specified', E_USER_NOTICE); return; } $send_params = Array ( 'to_name' => $notification_email, 'to_email' => $notification_email, ); // initialize list outside of e-mail event with right settings $this->Application->recallObject('system-log.email', 'system-log_List', Array ('unique_id' => $unique_id)); $this->Application->emailAdmin('SYSTEM.LOG.NOTIFY', null, $send_params); $this->Application->removeObject('system-log.email'); } /** * Adds error/exception handler * * @param string|Array $handler * @param bool $is_exception * @return void * @access public */ public function addErrorHandler($handler, $is_exception = false) { $this->_handlers[$is_exception ? self::LL_CRITICAL : self::LL_ERROR]->add($handler); } /** * SQL Error Handler * * When not debug mode, then fatal database query won't break anything. * * @param int $code * @param string $msg * @param string $sql * @return bool * @access public * @throws RuntimeException */ public function handleSQLError($code, $msg, $sql) { $error_msg = self::shortenMessage(self::DB_ERROR_PREFIX . ' #' . $code . ' - ' . $msg . '. SQL: ' . trim($sql)); if ( isset($this->Application->Debugger) ) { if ( kUtil::constOn('DBG_SQL_FAILURE') && !defined('IS_INSTALL') ) { throw new RuntimeException($error_msg); } else { $this->Application->Debugger->appendTrace(); } } // next line also trigger attached error handlers trigger_error($error_msg, E_USER_WARNING); return true; } /** * Packs information about error into a single line * * @param string $errno * @param bool $strip_tags * @return string * @access public */ public function toString($errno = null, $strip_tags = false) { if ( !isset($errno) ) { $errno = $this->_logRecord['LogCode']; } $errstr = $this->_logRecord['LogMessage']; $errfile = $this->_logRecord['LogSourceFilename']; $errline = $this->_logRecord['LogSourceFileLine']; if ( PHP_SAPI === 'cli' ) { $result = sprintf(' [%s] ' . PHP_EOL . ' %s', $errno, $errstr); if ( $this->_logRecord['LogBacktrace'] ) { $result .= $this->printBacktrace(unserialize($this->_logRecord['LogBacktrace'])); } } else { $result = '' . $errno . ': ' . "{$errstr} in {$errfile} on line {$errline}"; } return $strip_tags ? strip_tags($result) : $result; } /** * Prints backtrace result * * @param array $trace Trace. * * @return string */ protected function printBacktrace(array $trace) { if ( !$trace ) { return ''; } $ret = PHP_EOL . PHP_EOL . PHP_EOL . 'Exception trace:' . PHP_EOL; foreach ( $trace as $trace_info ) { $class = isset($trace_info['class']) ? $trace_info['class'] : ''; $type = isset($trace_info['type']) ? $trace_info['type'] : ''; $function = $trace_info['function']; $args = isset($trace_info['args']) && $trace_info['args'] ? '...' : ''; $file = isset($trace_info['file']) ? $trace_info['file'] : 'n/a'; $line = isset($trace_info['line']) ? $trace_info['line'] : 'n/a'; $ret .= sprintf(' %s%s%s(%s) at %s:%s' . PHP_EOL, $class, $type, $function, $args, $file, $line); } return $ret; } /** * Saves log to file (e.g. when not possible to save into database) * * @param $filename * @return bool * @access protected */ protected function _saveToFile($filename) { $time = adodb_date('Y-m-d H:i:s'); $log_file = new SplFileObject($filename, 'a'); return $log_file->fwrite('[' . $time . '] #' . $this->toString(null, true) . PHP_EOL) > 0; } /** * Checks if log type of current log record matches given one * * @param int $log_type * @param string $log_message * @return bool * @access public */ public function isLogType($log_type, $log_message = null) { if ( $this->_logRecord['LogType'] == $log_type ) { return true; } if ( $log_type == self::LT_DATABASE ) { if ( !isset($log_message) ) { $log_message = $this->_logRecord['LogMessage']; } return strpos($log_message, self::DB_ERROR_PREFIX) !== false; } return false; } /** * Shortens message * * @param string $message * @return string * @access public */ public static function shortenMessage($message) { $max_len = ini_get('log_errors_max_len'); if ( strlen($message) > $max_len ) { $long_key = kUtil::generateId(); self::$_longMessages[$long_key] = $message; return mb_substr($message, 0, $max_len - strlen($long_key) - 2) . ' #' . $long_key; } return $message; } /** * Expands shortened message * * @param string $message * @param bool $clear_cache Allow debugger to expand message after it's been expanded by kLogger * @return string * @access public */ public static function expandMessage($message, $clear_cache = true) { if ( preg_match('/(.*)#([\d]+)$/', $message, $regs) ) { $long_key = $regs[2]; if ( isset(self::$_longMessages[$long_key]) ) { $message = self::$_longMessages[$long_key]; if ( $clear_cache ) { unset(self::$_longMessages[$long_key]); } } } return $message; } /** + * Determines if error should be logged based on it's origin. + * + * @param string $file File. + * + * @return boolean + */ + public static function isErrorOriginAllowed($file) + { + static $error_origin_regexp; + + // Lazy detect error origins, because they're not available at construction time. + if ( !$error_origin_regexp ) { + $error_origins = array(); + $application = kApplication::Instance(); + + foreach ( $application->ModuleInfo as $module_info ) { + $error_origins[] = preg_quote(rtrim($module_info['Path'], '/'), '/'); + } + + $error_origins = array_unique($error_origins); + $error_origin_regexp = '/^' . preg_quote(FULL_PATH, '/') . '\/(' . implode('|', $error_origins) . ')\//'; + } + + // Allow dynamically generated code. + if ( strpos($file, 'eval()\'d code') !== false ) { + return true; + } + + // Allow known modules. + if ( preg_match('/^' . preg_quote(MODULES_PATH, '/') . '\//', $file) ) { + return preg_match($error_origin_regexp, $file) == 1; + } + + // Don't allow Vendors. + if ( preg_match('/^' . preg_quote(FULL_PATH, '/') . '\/vendor\//', $file) ) { + return false; + } + + // Allow everything else within main folder. + return preg_match('/^' . preg_quote(FULL_PATH, '/') . '\//', $file) == 1; + } + + /** * Parses database error message into error number, error message and sql that caused that error * * @static * @param string $message * @return Array * @access public */ public static function parseDatabaseError($message) { $regexp = '/' . preg_quote(self::DB_ERROR_PREFIX) . ' #(.*?) - (.*?)\. SQL: (.*?)$/s'; if ( preg_match($regexp, $message, $regs) ) { // errno, errstr, sql return Array ($regs[1], $regs[2], $regs[3]); } return Array (0, $message, ''); } } /** * Base class for error or exception handling */ abstract class kHandlerStack extends kBase { /** * List of added handlers * * @var Array * @access protected */ protected $_handlers = Array (); /** * Reference to event log, which created this object * * @var kLogger * @access protected */ protected $_logger; /** * Remembers if handler is activated * * @var bool * @access protected */ protected $_enabled = false; public function __construct(kLogger $logger) { parent::__construct(); $this->_logger = $logger; if ( !kUtil::constOn('DBG_ZEND_PRESENT') ) { $this->attach(); $this->_enabled = true; } } /** * Detaches from error handling routines on class destruction * * @return void * @access public */ public function __destruct() { if ( !$this->_enabled ) { return; } $this->detach(); $this->_enabled = false; } /** * Attach to error handling routines * * @abstract * @return void * @access protected */ abstract protected function attach(); /** * Detach from error handling routines * * @abstract * @return void * @access protected */ abstract protected function detach(); /** * Adds new handler to the stack * * @param callable $handler * @return void * @access public */ public function add($handler) { $this->_handlers[] = $handler; } + /** + * Returns `true`, when no other error handlers should process this error. + * + * @param integer $errno Error code. + * + * @return boolean + */ protected function _handleFatalError($errno) { $debug_mode = defined('DEBUG_MODE') && DEBUG_MODE; $skip_reporting = defined('DBG_SKIP_REPORTING') && DBG_SKIP_REPORTING; if ( !$this->_handlers || ($debug_mode && $skip_reporting) ) { // when debugger absent OR it's present, but we actually can't see it's error report (e.g. during ajax request) if ( $this->_isFatalError($errno) ) { $this->_displayFatalError($errno); } if ( !$this->_handlers ) { return true; } } - return null; + return false; } /** * Determines if given error is a fatal * * @abstract * @param Exception|int $errno * @return bool */ abstract protected function _isFatalError($errno); /** * Displays div with given error message * * @param string $errno * @return void * @access protected */ protected function _displayFatalError($errno) { $errno = $this->_getFatalErrorTitle($errno); $margin = $this->Application->isAdmin ? '8px' : 'auto'; $error_msg = $this->_logger->toString($errno, PHP_SAPI === 'cli'); if ( PHP_SAPI === 'cli' ) { echo $error_msg; } else { echo '
' . $error_msg . '
'; } exit; } /** * Returns title to show for a fatal * * @abstract * @param Exception|int $errno * @return string */ abstract protected function _getFatalErrorTitle($errno); } /** * Class, that handles errors */ class kErrorHandlerStack extends kHandlerStack { /** * Attach to error handling routines * * @return void * @access protected */ protected function attach() { // set as error handler $error_handler = set_error_handler(Array ($this, 'handle')); if ( $error_handler ) { // wrap around previous error handler, if any was set $this->_handlers[] = $error_handler; } } /** * Detach from error handling routines * * @return void * @access protected */ protected function detach() { restore_error_handler(); } /** * Determines if given error is a fatal * * @param int $errno * @return bool * @access protected */ protected function _isFatalError($errno) { $fatal_errors = Array (E_USER_ERROR, E_RECOVERABLE_ERROR, E_ERROR, E_CORE_ERROR, E_COMPILE_ERROR, E_PARSE); return in_array($errno, $fatal_errors); } /** * Returns title to show for a fatal * * @param int $errno * @return string * @access protected */ protected function _getFatalErrorTitle($errno) { return 'Fatal Error'; } /** * Default error handler * * @param int $errno * @param string $errstr * @param string $errfile * @param int $errline * @param Array $errcontext * @return bool * @access public */ public function handle($errno, $errstr, $errfile = null, $errline = null, $errcontext = Array ()) { $log = $this->_logger->prepare()->addError($errno, $errstr, $errfile, $errline); if ( $this->_handleFatalError($errno) ) { - $log->write(); + $log->write(kLogger::LS_AUTOMATIC, !$this->_isFatalError($errno)); return true; } - $log->write(); + $log->write(kLogger::LS_AUTOMATIC, !$this->_isFatalError($errno)); $res = false; foreach ($this->_handlers as $handler) { $res = call_user_func($handler, $errno, $errstr, $errfile, $errline, $errcontext); } return $res; } } /** * Class, that handles exceptions */ class kExceptionHandlerStack extends kHandlerStack { /** * Attach to error handling routines * * @return void * @access protected */ protected function attach() { // set as exception handler $exception_handler = set_exception_handler(Array ($this, 'handle')); if ( $exception_handler ) { // wrap around previous exception handler, if any was set $this->_handlers[] = $exception_handler; } } /** * Detach from error handling routines * * @return void * @access protected */ protected function detach() { restore_exception_handler(); } /** * Determines if given error is a fatal * * @param Exception $errno * @return bool */ protected function _isFatalError($errno) { return true; } /** * Returns title to show for a fatal * * @param Exception $errno * @return string */ protected function _getFatalErrorTitle($errno) { return get_class($errno); } /** * Handles exception * * @param Exception $exception * @return bool * @access public */ public function handle($exception) { $log = $this->_logger->prepare()->addException($exception); if ( $exception instanceof kRedirectException ) { /** @var kRedirectException $exception */ $exception->run(); } if ( $this->_handleFatalError($exception) ) { $log->write(); return true; } $log->write(); $res = false; foreach ($this->_handlers as $handler) { $res = call_user_func($handler, $exception); } return $res; } } Index: branches/5.2.x/core/kernel/utility/debugger.php =================================================================== --- branches/5.2.x/core/kernel/utility/debugger.php (revision 16543) +++ branches/5.2.x/core/kernel/utility/debugger.php (revision 16544) @@ -1,2143 +1,2146 @@ = 1099511627776) { $return = round($bytes / 1024 / 1024 / 1024 / 1024, 2); $suffix = "TB"; } elseif ($bytes >= 1073741824) { $return = round($bytes / 1024 / 1024 / 1024, 2); $suffix = "GB"; } elseif ($bytes >= 1048576) { $return = round($bytes / 1024 / 1024, 2); $suffix = "MB"; } elseif ($bytes >= 1024) { $return = round($bytes / 1024, 2); $suffix = "KB"; } else { $return = $bytes; $suffix = "Byte"; } $return .= ' '.$suffix; return $return; } /** * Checks, that user IP address is within allowed range * * @param string $ip_list semi-column (by default) separated ip address list * @param string $separator ip address separator (default ";") * * @return bool */ public static function ipMatch($ip_list, $separator = ';') { if ( php_sapi_name() == 'cli' ) { return false; } $ip_match = false; $ip_addresses = $ip_list ? explode($separator, $ip_list) : Array (); $client_ip = self::getClientIp(); foreach ($ip_addresses as $ip_address) { if ( self::netMatch($ip_address, $client_ip) ) { $ip_match = true; break; } } return $ip_match; } /** * Returns the client IP address. * * @return string The client IP address * @access public */ public static function getClientIp() { if ( self::$trustProxy ) { if ( array_key_exists('HTTP_CLIENT_IP', $_SERVER) ) { return $_SERVER['HTTP_CLIENT_IP']; } if ( array_key_exists('HTTP_X_FORWARDED_FOR', $_SERVER) ) { $client_ip = explode(',', $_SERVER['HTTP_X_FORWARDED_FOR']); foreach ($client_ip as $ip_address) { $clean_ip_address = trim($ip_address); if ( false !== filter_var($clean_ip_address, FILTER_VALIDATE_IP) ) { return $clean_ip_address; } } return ''; } } return $_SERVER['REMOTE_ADDR']; } /** * Checks, that given ip belongs to given subnet * * @param string $network * @param string $ip * @return bool * @access public */ public static function netMatch($network, $ip) { $network = trim($network); $ip = trim($ip); if ( preg_replace('/[\d\.\/-]/', '', $network) != '' ) { $network = gethostbyname($network); } if ($network == $ip) { // comparing two ip addresses directly return true; } $d = strpos($network, '-'); if ($d !== false) { // ip address range specified $from = ip2long(trim(substr($network, 0, $d))); $to = ip2long(trim(substr($network, $d + 1))); $ip = ip2long($ip); return ($ip >= $from && $ip <= $to); } elseif (strpos($network, '/') !== false) { // single subnet specified $ip_arr = explode('/', $network); if (!preg_match("@\d*\.\d*\.\d*\.\d*@", $ip_arr[0], $matches)) { $ip_arr[0] .= '.0'; // Alternate form 194.1.4/24 } $network_long = ip2long($ip_arr[0]); $x = ip2long($ip_arr[1]); $mask = long2ip($x) == $ip_arr[1] ? $x : (0xffffffff << (32 - $ip_arr[1])); $ip_long = ip2long($ip); return ($ip_long & $mask) == ($network_long & $mask); } return false; } } /** * Main debugger class, that can be used with any In-Portal (or not) project */ class Debugger { const ROW_TYPE_ERROR = 'error'; const ROW_TYPE_WARNING = 'warning'; const ROW_TYPE_NOTICE = 'notice'; const ROW_TYPE_SQL = 'sql'; const ROW_TYPE_OTHER = 'other'; /** * Holds reference to global KernelApplication instance * * @var kApplication * @access private */ private $Application = null; /** * Stores last fatal error hash or 0, when no fatal error happened * * @var integer */ private $_fatalErrorHash = 0; private $_filterTypes = Array ('error', 'sql', 'other'); /** * Counts warnings on the page * * @var int * @access public */ public $WarningCount = 0; /** * Allows to track compile errors, like "stack-overflow" * * @var bool * @access private */ private $_compileError = false; /** * Debugger data for building report * * @var Array * @access private */ private $Data = Array (); /** * Holds information about each profiler record (start/end/description) * * @var Array * @access private */ private $ProfilerData = Array (); /** * Holds information about total execution time per profiler key (e.g. total sql time) * * @var Array * @access private */ private $ProfilerTotals = Array (); /** * Counts how much each of total types were called (e.g. total error count) * * @var Array * @access private */ private $ProfilerTotalCount = Array (); /** * Holds information about all profile points registered * * @var Array * @access private */ private $ProfilePoints = Array (); /** * Prevent recursion when processing debug_backtrace() function results * * @var Array * @access private */ private $RecursionStack = Array (); /** * Cross browser debugger report scrollbar width detection * * @var int * @access private */ private $scrollbarWidth = 0; /** * Remembers how much memory & time was spent on including files * * @var Array * @access public * @see kUtil::includeOnce */ public $IncludesData = Array (); /** * Remembers maximal include deep level * * @var int * @access public * @see kUtil::includeOnce */ public $IncludeLevel = 0; /** * Prevents report generation more then once * * @var bool * @access private */ private $_inReportPrinting = false; /** * Transparent spacer image used in case of none spacer image defined via SPACER_URL constant. * Used while drawing progress bars (memory usage, time usage, etc.) * * @var string * @access private */ private $dummyImage = ''; /** * Temporary files created by debugger will be stored here * * @var string * @access private */ private $tempFolder = ''; /** * Debug rows will be separated using this string before writing to debug file * * @var string * @access private */ private $rowSeparator = '@@'; /** * Base URL for debugger includes * * @var string * @access private */ private $baseURL = ''; /** * Sub-folder, where In-Portal is installed * * @var string * @access private */ private $basePath = ''; /** * Holds last recorded timestamp (for appendTimestamp) * * @var int * @access private */ private $LastMoment; /** * Determines, that current request is AJAX request * * @var bool * @access private */ private $_isAjax = false; /** * Data, parsed from the editor url. * * @var array */ protected $editorUrlData = array('url' => '', 'params' => array()); /** * Creates instance of debugger */ public function __construct() { global $start, $dbg_options; // check if user haven't defined DEBUG_MODE contant directly if ( defined('DEBUG_MODE') && DEBUG_MODE ) { die('error: constant DEBUG_MODE defined directly, please use $dbg_options array instead'); } if ( class_exists('kUtil') ) { DebuggerUtil::$trustProxy = kUtil::getSystemConfig()->get('TrustProxy'); } // check IP before enabling debug mode $ip_match = DebuggerUtil::ipMatch(isset($dbg_options['DBG_IP']) ? $dbg_options['DBG_IP'] : ''); if ( !$ip_match || (isset($_COOKIE['debug_off']) && $_COOKIE['debug_off']) ) { define('DEBUG_MODE', 0); return; } // debug is allowed for user, continue initialization $this->InitDebugger(); $this->profileStart('kernel4_startup', 'Startup and Initialization of kernel4', $start); $this->profileStart('script_runtime', 'Script runtime', $start); $this->LastMoment = $start; error_reporting(E_ALL & ~E_STRICT); // show errors on screen in case if not in Zend Studio debugging ini_set('display_errors', DebuggerUtil::constOn('DBG_ZEND_PRESENT') ? 0 : 1); // vertical scrollbar width differs in Firefox and other browsers $this->scrollbarWidth = $this->isGecko() ? 22 : 25; $this->appendRequest(); } /** * Set's default values to constants debugger uses * */ function InitDebugger() { global $dbg_options; unset($dbg_options['DBG_IP']); // Detect fact, that this session being debugged by Zend Studio foreach ($_COOKIE as $cookie_name => $cookie_value) { if (substr($cookie_name, 0, 6) == 'debug_') { DebuggerUtil::safeDefine('DBG_ZEND_PRESENT', 1); break; } } DebuggerUtil::safeDefine('DBG_ZEND_PRESENT', 0); // set this constant value to 0 (zero) to debug debugger using Zend Studio // set default values for debugger constants $dbg_constMap = Array ( 'DBG_USE_HIGHLIGHT' => 1, // highlight output same as php code using "highlight_string" function 'DBG_WINDOW_WIDTH' => 700, // set width of debugger window (in pixels) for better viewing large amount of debug data 'DBG_USE_SHUTDOWN_FUNC' => DBG_ZEND_PRESENT ? 0 : 1, // use shutdown function to include debugger code into output 'DBG_HANDLE_ERRORS' => DBG_ZEND_PRESENT ? 0 : 1, // handle all allowed by php (see php manual) errors instead of default handler 'DBG_DOMVIEWER' => '/temp/domviewer.html', // path to DOMViewer on website 'DOC_ROOT' => str_replace('\\', '/', realpath($_SERVER['DOCUMENT_ROOT']) ), // windows hack 'DBG_LOCAL_BASE_PATH' => 'w:', // replace DOC_ROOT in filenames (in errors) using this path 'DBG_EDITOR_URL' => 'file://%F:%L', 'DBG_SHORTCUT' => 'F12', // Defines debugger activation shortcut (any symbols or Ctrl/Alt/Shift are allowed, e.g. Ctrl+Alt+F12) ); // debugger is initialized before kHTTPQuery, so do jQuery headers check here too if (array_key_exists('HTTP_X_REQUESTED_WITH', $_SERVER) && $_SERVER['HTTP_X_REQUESTED_WITH'] == 'XMLHttpRequest') { $this->_isAjax = true; } elseif (array_key_exists('ajax', $_GET) && $_GET['ajax'] == 'yes') { $this->_isAjax = true; } // user defined options override debugger defaults $dbg_constMap = array_merge($dbg_constMap, $dbg_options); if ($this->_isAjax && array_key_exists('DBG_SKIP_AJAX', $dbg_constMap) && $dbg_constMap['DBG_SKIP_AJAX']) { $dbg_constMap['DBG_SKIP_REPORTING'] = 1; } // allows to validate unit configs via request variable if ( !array_key_exists('DBG_VALIDATE_CONFIGS', $dbg_constMap) ) { $dbg_constMap['DBG_VALIDATE_CONFIGS'] = array_key_exists('validate_configs', $_GET) ? (int)$_GET['validate_configs'] : 0; } // when validation configs, don't show sqls for better validation error displaying if ($dbg_constMap['DBG_VALIDATE_CONFIGS']) { $dbg_constMap['DBG_SQL_PROFILE'] = 0; } // when showing explain make shure, that debugger window is large enough if (array_key_exists('DBG_SQL_EXPLAIN', $dbg_constMap) && $dbg_constMap['DBG_SQL_EXPLAIN']) { $dbg_constMap['DBG_WINDOW_WIDTH'] = 1000; } foreach ($dbg_constMap as $dbg_constName => $dbg_constValue) { DebuggerUtil::safeDefine($dbg_constName, $dbg_constValue); } $this->parseEditorUrl(); } /** * Parses editor url. * * @return void */ protected function parseEditorUrl() { $components = parse_url(DBG_EDITOR_URL); $this->editorUrlData['url'] = $components['scheme'] . '://' . $components['host']; if ( isset($components['path']) ) { $this->editorUrlData['url'] .= $components['path']; } if ( isset($components['query']) ) { parse_str(html_entity_decode($components['query']), $this->editorUrlData['params']); } } /** * Performs debugger initialization * * @return void */ private function InitReport() { if ( !class_exists('kApplication') ) { return; } $application =& kApplication::Instance(); // string used to separate debugger records while in file (used in debugger dump filename too) $this->rowSeparator = '@' . (/*is_object($application->Factory) &&*/ $application->InitDone ? $application->GetSID() : 0) . '@'; // $this->rowSeparator = '@' . rand(0, 100000) . '@'; // include debugger files from this url $reg_exp = '/^' . preg_quote(FULL_PATH, '/') . '/'; $kernel_path = preg_replace($reg_exp, '', KERNEL_PATH, 1); $this->baseURL = PROTOCOL . SERVER_NAME . (defined('PORT') ? ':' . PORT : '') . rtrim(BASE_PATH, '/') . $kernel_path . '/utility/debugger'; // store debugger cookies at this path $this->basePath = rtrim(BASE_PATH, '/'); // save debug output in this folder $this->tempFolder = defined('RESTRICTED') ? RESTRICTED : WRITEABLE . '/cache'; } /** * Appends all passed variable values (without variable names) to debug output * * @return void * @access public */ public function dumpVars() { $dump_mode = 'var_dump'; $dumpVars = func_get_args(); if ( $dumpVars[count($dumpVars) - 1] === 'STRICT' ) { $dump_mode = 'strict_var_dump'; array_pop($dumpVars); } foreach ($dumpVars as $varValue) { $this->Data[] = Array ('value' => $varValue, 'debug_type' => $dump_mode); } } /** * Transforms collected data at given index into human-readable HTML to place in debugger report * * @param int $dataIndex * @return string * @access private */ private function prepareHTML($dataIndex) { static $errors_displayed = 0; $Data =& $this->Data[$dataIndex]; if ( $Data['debug_type'] == 'html' ) { return $Data['html']; } switch ($Data['debug_type']) { case 'error': $errors_displayed++; $fileLink = $this->getFileLink($Data['file'], $Data['line']); $ret = '' . $this->getErrorNameByCode($Data['no']) . ' (#' . $errors_displayed . '): ' . $Data['str']; $ret .= ' in ' . $fileLink . ' on line ' . $Data['line'] . ''; return $ret; break; case 'exception': $fileLink = $this->getFileLink($Data['file'], $Data['line']); $ret = '' . $Data['exception_class'] . ': ' . $Data['str']; $ret .= ' in ' . $fileLink . ' on line ' . $Data['line'] . ''; return $ret; break; case 'var_dump': return $this->highlightString($this->print_r($Data['value'], true)); break; case 'strict_var_dump': return $this->highlightString(var_export($Data['value'], true)); break; case 'trace': ini_set('memory_limit', '500M'); $trace =& $Data['trace']; $i = 0; $traceCount = count($trace); $ret = ''; while ( $i < $traceCount ) { $traceRec =& $trace[$i]; $argsID = 'trace_args_' . $dataIndex . '_' . $i; $has_args = isset($traceRec['args']); if ( isset($traceRec['file']) ) { $func_name = isset($traceRec['class']) ? $traceRec['class'] . $traceRec['type'] . $traceRec['function'] : $traceRec['function']; $args_link = $has_args ? 'Function' : 'Function'; $ret .= $args_link . ': ' . $this->getFileLink($traceRec['file'], $traceRec['line'], $func_name); $ret .= ' in ' . basename($traceRec['file']) . ' on line ' . $traceRec['line'] . '
'; } else { $ret .= 'no file information available'; } if ( $has_args ) { // if parameter value is longer then 200 symbols, then leave only first 50 $args = $this->highlightString($this->print_r($traceRec['args'], true)); $ret .= ''; } $i++; } return $ret; break; case 'profiler': $profileKey = $Data['profile_key']; $Data =& $this->ProfilerData[$profileKey]; $runtime = ($Data['ends'] - $Data['begins']); // in seconds $totals_key = getArrayValue($Data, 'totalsKey'); if ( $totals_key ) { $total_before = $Data['totalsBefore']; $total = $this->ProfilerTotals[$totals_key]; $div_width = Array (); $total_width = ($this->getWindowWidth() - 10); $div_width['before'] = round(($total_before / $total) * $total_width); $div_width['current'] = round(($runtime / $total) * $total_width); $div_width['left'] = round((($total - $total_before - $runtime) / $total) * $total_width); $subtitle = array_key_exists('subtitle', $Data) ? ' (' . $Data['subtitle'] . ')' : ''; $ret = 'Name' . $subtitle . ': ' . $Data['description'] . '
'; $additional = isset($Data['additional']) ? $Data['additional'] : Array (); if ( isset($Data['file']) ) { array_unshift($additional, Array ('name' => 'File', 'value' => $this->getFileLink($Data['file'], $Data['line'], basename($Data['file']) . ':' . $Data['line']))); } array_unshift($additional, Array ('name' => 'Runtime', 'value' => $runtime . 's')); $ret .= '
'; //FF 3.5 needs this! foreach ($additional as $mixed_param) { $ret .= '[' . $mixed_param['name'] . ': ' . $mixed_param['value'] . '] '; } /*if ( isset($Data['file']) ) { $ret .= '[Runtime: ' . $runtime . 's] [File: ' . $this->getFileLink($Data['file'], $Data['line'], basename($Data['file']) . ':' . $Data['line']) . ']
'; } else { $ret .= 'Runtime: ' . $runtime . 's
'; }*/ $ret .= '
'; $ret .= '
'; $ret .= '
'; $ret .= '
'; return $ret; } else { return 'Name: ' . $Data['description'] . '
Runtime: ' . $runtime . 's'; } break; default: return 'incorrect debug data'; break; } } /** * Returns row type for debugger row. * * @param integer $data_index Index of the row. * * @return string */ protected function getRowType($data_index) { $data = $this->Data[$data_index]; switch ($data['debug_type']) { case 'html': if ( strpos($data['html'], 'SQL Total time') !== false ) { return self::ROW_TYPE_SQL; } break; case 'error': $error_map = array( 'Fatal Error' => self::ROW_TYPE_ERROR, 'Warning' => self::ROW_TYPE_WARNING, 'Notice' => self::ROW_TYPE_NOTICE, 'Deprecation Notice' => self::ROW_TYPE_NOTICE, ); return $error_map[$this->getErrorNameByCode($data['no'])]; break; case 'exception': return self::ROW_TYPE_ERROR; break; case 'profiler': if ( preg_match('/^sql_/', $data['profile_key']) ) { return self::ROW_TYPE_SQL; } break; } return self::ROW_TYPE_OTHER; } /** * Returns debugger report window width excluding scrollbar * * @return int * @access private */ private function getWindowWidth() { return DBG_WINDOW_WIDTH - $this->scrollbarWidth - 8; } /** * Tells debugger to skip objects that are heavy in plan of memory usage while printing debug_backtrace results * * @param Object $object * @return bool * @access private */ private function IsBigObject(&$object) { $skip_classes = Array( defined('APPLICATION_CLASS') ? APPLICATION_CLASS : 'kApplication', 'kFactory', 'kUnitConfigReader', 'NParser', ); foreach ($skip_classes as $class_name) { if ( strtolower(get_class($object)) == strtolower($class_name) ) { return true; } } return false; } /** * Advanced version of print_r (for debugger only). Don't print objects recursively. * * @param mixed $p_array Value to be printed. * @param boolean $return_output Return output or print it out. * @param integer $tab_count Offset in tabs. * * @return string */ private function print_r(&$p_array, $return_output = false, $tab_count = -1) { static $first_line = true; // not an array at all if ( !is_array($p_array) ) { switch ( gettype($p_array) ) { case 'NULL': return 'NULL' . "\n"; break; case 'object': return $this->processObject($p_array, $tab_count); break; case 'resource': return (string)$p_array . "\n"; break; default: // number or string if ( strlen($p_array) > 200 ) { $p_array = substr($p_array, 0, 50) . ' ...'; } return $p_array . "\n"; break; } } $output = ''; if ( count($p_array) > 50 ) { $array = array_slice($p_array, 0, 50); $array[] = '...'; } else { $array = $p_array; } $tab_count++; $output .= "Array\n" . str_repeat(' ', $tab_count) . "(\n"; $tab_count++; $tabsign = $tab_count ? str_repeat(' ', $tab_count) : ''; $array_keys = array_keys($array); foreach ($array_keys as $key) { switch ( gettype($array[$key]) ) { case 'array': $output .= $tabsign . '[' . $key . '] = ' . $this->print_r($array[$key], true, $tab_count); break; case 'boolean': $output .= $tabsign . '[' . $key . '] = ' . ($array[$key] ? 'true' : 'false') . "\n"; break; case 'integer': case 'double': case 'string': if ( strlen($array[$key]) > 200 ) { $array[$key] = substr($array[$key], 0, 50) . ' ...'; } $output .= $tabsign . '[' . $key . '] = ' . $array[$key] . "\n"; break; case 'NULL': $output .= $tabsign . '[' . $key . "] = NULL\n"; break; case 'object': $output .= $tabsign . '[' . $key . "] = "; $output .= "Object (" . get_class($array[$key]) . ") = \n" . str_repeat(' ', $tab_count + 1) . "(\n"; $output .= $this->processObject($array[$key], $tab_count + 2); $output .= str_repeat(' ', $tab_count + 1) . ")\n"; break; default: $output .= $tabsign . '[' . $key . '] unknown = ' . gettype($array[$key]) . "\n"; break; } } $tab_count--; $output .= str_repeat(' ', $tab_count) . ")\n"; if ( $first_line ) { $first_line = false; $output .= "\n"; } $tab_count--; if ( $return_output ) { return $output; } else { echo $output; } return true; } /** * Returns string representation of given object (more like print_r, but with recursion prevention check) * * @param Object $object * @param int $tab_count * @return string * @access private */ private function processObject(&$object, $tab_count) { $object_class = get_class($object); if ( !in_array($object_class, $this->RecursionStack) ) { if ( $this->IsBigObject($object) ) { return 'SKIPPED (class: ' . $object_class . ")\n"; } $attribute_names = get_class_vars($object_class); if ( !$attribute_names ) { return "NO_ATTRIBUTES\n"; } else { $output = ''; array_push($this->RecursionStack, $object_class); $tabsign = $tab_count ? str_repeat(' ', $tab_count) : ''; foreach ($attribute_names as $attribute_name => $attribute_value) { if ( is_object($object->$attribute_name) ) { // it is object $output .= $tabsign . '[' . $attribute_name . '] = ' . $this->processObject($object->$attribute_name, $tab_count + 1); } else { $output .= $tabsign . '[' . $attribute_name . '] = ' . $this->print_r($object->$attribute_name, true, $tab_count); } } array_pop($this->RecursionStack); return $output; } } else { // object [in recursion stack] return '*** RECURSION *** (class: ' . $object_class . ")\n"; } } /** * Format SQL Query using predefined formatting * and highlighting techniques * * @param string $sql * @return string * @access public */ public function formatSQL($sql) { $sql = trim(preg_replace('/(\n|\t| )+/is', ' ', $sql)); // whitespace in the beginning of the regex is to avoid splitting inside words, for example "FROM int_ConfigurationValues" into "FROM intConfiguration\n\tValues" $formatted_sql = preg_replace('/\s(CREATE TABLE|DROP TABLE|SELECT|UPDATE|SET|REPLACE|INSERT|DELETE|VALUES|FROM|LEFT JOIN|INNER JOIN|LIMIT|WHERE|HAVING|GROUP BY|ORDER BY)\s/is', "\n\t$1 ", ' ' . $sql); $formatted_sql = $this->highlightString($formatted_sql); if ( defined('DBG_SQL_EXPLAIN') && DBG_SQL_EXPLAIN ) { if ( substr($sql, 0, 6) == 'SELECT' ) { $formatted_sql .= '
' . 'Explain:

'; $explain_result = $this->Application->Conn->Query('EXPLAIN ' . $sql, null, true); $explain_table = ''; foreach ($explain_result as $explain_row) { if ( !$explain_table ) { // first row -> draw header $explain_table .= '' . implode('', array_keys($explain_row)) . ''; } $explain_table .= '' . implode('', $explain_row) . ''; } $formatted_sql .= '' . $explain_table . '
'; } } return $formatted_sql; } /** * Highlights given string using "highlight_string" method * * @param string $string * @return string * @access public */ public function highlightString($string) { if ( !(defined('DBG_USE_HIGHLIGHT') && DBG_USE_HIGHLIGHT) || $this->_compileError ) { return nl2br($string); } $string = str_replace(Array ('\\', '/'), Array ('_no_match_string_', '_n_m_s_'), $string); $this->_compileError = true; // next line is possible cause of compile error $string = highlight_string('', true); $this->_compileError = false; $string = str_replace(Array ('_no_match_string_', '_n_m_s_'), Array ('\\', '/'), $string); if ( strlen($string) >= 65536 ) { // preg_replace will fail, when string is longer, then 65KB return str_replace(Array ('<?php ', '?>'), '', $string); } return preg_replace('/<\?(.*)php (.*)\?>/Us', '\\2', $string); } /** * Determine by php type of browser used to show debugger * * @return bool * @access private */ private function isGecko() { // we need isset because we may run scripts from shell with no user_agent at all return isset($_SERVER['HTTP_USER_AGENT']) && strpos(strtolower($_SERVER['HTTP_USER_AGENT']), 'firefox') !== false; } /** * Returns link for editing php file (from error) in external editor * * @param string $file filename with path from root folder * @param int $line_number line number in file where error is found * @param string $title text to show on file edit link * @return string * @access public */ public function getFileLink($file, $line_number = 1, $title = '') { if ( !$title ) { $title = str_replace('/', '\\', $this->getLocalFile($file)); } $local_file = $this->getLocalFile($file); $url_params = $this->editorUrlData['params']; foreach ( $url_params as $param_name => $param_value ) { $url_params[$param_name] = str_replace( array('%F', '%L'), array($local_file, $line_number), $param_value ); } $url = $this->editorUrlData['url'] . '?' . http_build_query($url_params); return '' . $title . ''; } /** * Converts filepath on server to filepath in mapped DocumentRoot on developer pc * * @param string $remoteFile * @return string * @access private */ private function getLocalFile($remoteFile) { return preg_replace('/^' . preg_quote(DOC_ROOT, '/') . '/', DBG_LOCAL_BASE_PATH, $remoteFile, 1); } /** * Appends call trace till this method call * * @param int $levels_to_shift * @return void * @access public */ public function appendTrace($levels_to_shift = 1) { $levels_shifted = 0; $trace = debug_backtrace(); while ( $levels_shifted < $levels_to_shift ) { array_shift($trace); $levels_shifted++; } $this->Data[] = Array ('trace' => $trace, 'debug_type' => 'trace'); } /** * Appends call trace till this method call * * @param Exception $exception * @return void * @access private */ private function appendExceptionTrace(&$exception) { $trace = $exception->getTrace(); $this->Data[] = Array('trace' => $trace, 'debug_type' => 'trace'); } /** * Adds memory usage statistics * * @param string $msg * @param int $used * @return void * @access public */ public function appendMemoryUsage($msg, $used = null) { if ( !isset($used) ) { $used = round(memory_get_usage() / 1024); } $this->appendHTML('Memory usage ' . $msg . ' ' . $used . 'Kb'); } /** * Appends HTML code without transformations * * @param string $html * @return void * @access public */ public function appendHTML($html) { $this->Data[] = Array ('html' => $html, 'debug_type' => 'html'); } /** * Returns instance of FirePHP class * * @return FirePHP * @link http://www.firephp.org/HQ/Use.htm */ function firePHP() { require_once('FirePHPCore/FirePHP.class.php'); return FirePHP::getInstance(true); } /** * Change debugger info that was already generated before. * Returns true if html was set. * * @param int $index * @param string $html * @param string $type = {'append','prepend','replace'} * @return bool * @access public */ public function setHTMLByIndex($index, $html, $type = 'append') { if ( !isset($this->Data[$index]) || $this->Data[$index]['debug_type'] != 'html' ) { return false; } switch ( $type ) { case 'append': $this->Data[$index]['html'] .= '
' . $html; break; case 'prepend': $this->Data[$index]['html'] = $this->Data[$index]['html'] . '
' . $html; break; case 'replace': $this->Data[$index]['html'] = $html; break; } return true; } /** * Move $debugLineCount lines of input from debug output * end to beginning. * * @param int $debugLineCount * @return void * @access private */ private function moveToBegin($debugLineCount) { $lines = array_splice($this->Data, count($this->Data) - $debugLineCount, $debugLineCount); $this->Data = array_merge($lines, $this->Data); } /** * Moves all debugger report lines after $debugLineCount into $new_row position * * @param int $new_row * @param int $debugLineCount * @return void * @access private */ private function moveAfterRow($new_row, $debugLineCount) { $lines = array_splice($this->Data, count($this->Data) - $debugLineCount, $debugLineCount); $rows_before = array_splice($this->Data, 0, $new_row, $lines); $this->Data = array_merge($rows_before, $this->Data); } /** * Appends HTTP REQUEST information to debugger report * * @return void * @access private */ private function appendRequest() { if ( isset($_SERVER['SCRIPT_FILENAME']) ) { $script = $_SERVER['SCRIPT_FILENAME']; } else { $script = $_SERVER['DOCUMENT_ROOT'] . $_SERVER['PHP_SELF']; } $this->appendHTML('ScriptName: ' . $this->getFileLink($script, 1, basename($script)) . ' (' . dirname($script) . ')'); if ( $this->_isAjax ) { $this->appendHTML('RequestURI: ' . $_SERVER['REQUEST_URI'] . ' (QS Length:' . strlen($_SERVER['QUERY_STRING']) . ')'); } $tools_html = '
' . $this->_getDomViewerHTML() . ' ' . $this->_getToolsHTML() . '
'; $this->appendHTML($tools_html); ob_start(); ?> $_GET, 'PO' => $_POST, 'CO' => $_COOKIE); foreach ($super_globals as $prefix => $data) { foreach ($data as $key => $value) { if ( !is_array($value) && trim($value) == '' ) { $value = 'no value'; } else { $value = htmlspecialchars($this->print_r($value, true), ENT_QUOTES, 'UTF-8'); } echo ''; } } ?>
SrcNameValue
' . $prefix . '' . $key . '' . $value . '
appendHTML(ob_get_contents()); ob_end_clean(); } /** * Appends php session content to debugger output * * @return void * @access private */ private function appendSession() { if ( isset($_SESSION) && $_SESSION ) { $this->appendHTML('PHP Session: [' . ini_get('session.name') . ']'); $this->dumpVars($_SESSION); $this->moveToBegin(2); } } /** * Starts profiling of a given $key * * @param string $key * @param string $description * @param int $timeStamp * @return void * @access public */ public function profileStart($key, $description = null, $timeStamp = null) { if ( !isset($timeStamp) ) { $timeStamp = microtime(true); } $this->ProfilerData[$key] = Array ('begins' => $timeStamp, 'ends' => 5000, 'debuggerRowID' => count($this->Data)); if ( isset($description) ) { $this->ProfilerData[$key]['description'] = $description; } if ( substr($key, 0, 4) == 'sql_' ) { // append place from what was called $trace_results = debug_backtrace(); $trace_count = count($trace_results); $i = 0; while ( $i < $trace_count ) { if ( !isset($trace_results[$i]['file']) ) { $i++; continue; } $trace_file = basename($trace_results[$i]['file']); if ( $trace_file != 'db_connection.php' && $trace_file != 'db_load_balancer.php' && $trace_file != 'adodb.inc.php' ) { break; } $i++; } $this->ProfilerData[$key]['file'] = $trace_results[$i]['file']; $this->ProfilerData[$key]['line'] = $trace_results[$i]['line']; if ( isset($trace_results[$i + 1]['object']) && isset($trace_results[$i + 1]['object']->Prefix) ) { /** @var kBase $object */ $object =& $trace_results[$i + 1]['object']; $prefix_special = rtrim($object->Prefix . '.' . $object->Special, '.'); $this->ProfilerData[$key]['prefix_special'] = $prefix_special; } unset($trace_results); } $this->Data[] = Array ('profile_key' => $key, 'debug_type' => 'profiler'); } /** * Ends profiling for a given $key * * @param string $key * @param string $description * @param int $timeStamp * @return void * @access public */ public function profileFinish($key, $description = null, $timeStamp = null) { if ( !isset($timeStamp) ) { $timeStamp = microtime(true); } $this->ProfilerData[$key]['ends'] = $timeStamp; if ( isset($description) ) { $this->ProfilerData[$key]['description'] = $description; } if ( substr($key, 0, 4) == 'sql_' ) { $func_arguments = func_get_args(); $rows_affected = $func_arguments[3]; $additional = Array (); if ( $rows_affected > 0 ) { $additional[] = Array ('name' => 'Affected Rows', 'value' => $rows_affected); if ( isset($func_arguments[4]) ) { if ( strlen($func_arguments[4]) > 200 ) { $func_arguments[4] = substr($func_arguments[4], 0, 50) . ' ...'; } $additional[] = Array ('name' => 'Result', 'value' => $func_arguments[4]); } } $additional[] = Array ('name' => 'Query Number', 'value' => $func_arguments[5]); if ( $func_arguments[6] ) { $this->profilerAddTotal('cachable_queries', $key); $this->ProfilerData[$key]['subtitle'] = 'cachable'; } if ( (string)$func_arguments[7] !== '' ) { $additional[] = Array ('name' => 'Server #', 'value' => $func_arguments[7]); } if ( array_key_exists('prefix_special', $this->ProfilerData[$key]) ) { $additional[] = Array ('name' => 'PrefixSpecial', 'value' => $this->ProfilerData[$key]['prefix_special']); } $this->ProfilerData[$key]['additional'] =& $additional; } } /** * Collects total execution time from profiler record * * @param string $total_key * @param string $key * @param int $value * @return void * @access public */ public function profilerAddTotal($total_key, $key = null, $value = null) { if ( !isset($this->ProfilerTotals[$total_key]) ) { $this->ProfilerTotals[$total_key] = 0; $this->ProfilerTotalCount[$total_key] = 0; } if ( !isset($value) ) { $value = $this->ProfilerData[$key]['ends'] - $this->ProfilerData[$key]['begins']; } if ( isset($key) ) { $this->ProfilerData[$key]['totalsKey'] = $total_key; $this->ProfilerData[$key]['totalsBefore'] = $this->ProfilerTotals[$total_key]; } $this->ProfilerTotals[$total_key] += $value; $this->ProfilerTotalCount[$total_key]++; } /** * Traces relative code execution speed between this method calls * * @param string $message * @return void * @access public */ public function appendTimestamp($message) { global $start; $time = microtime(true); $from_last = $time - $this->LastMoment; $from_start = $time - $start; $this->appendHTML(sprintf("%s %.5f from last %.5f from start", $message, $from_last, $from_start)); $this->LastMoment = $time; } /** * Returns unique ID for each method call * * @return int * @access public */ public function generateID() { list($usec, $sec) = explode(' ', microtime()); $id_part_1 = substr($usec, 4, 4); $id_part_2 = mt_rand(1, 9); $id_part_3 = substr($sec, 6, 4); $digit_one = substr($id_part_1, 0, 1); if ( $digit_one == 0 ) { $digit_one = mt_rand(1, 9); $id_part_1 = preg_replace('/^0/', '', $id_part_1); $id_part_1 = $digit_one . $id_part_1; } return $id_part_1 . $id_part_2 . $id_part_3; } /** * Returns error name based on it's code * * @param int $error_code * @return string * @access private */ private function getErrorNameByCode($error_code) { $error_map = Array ( 'Fatal Error' => Array (E_RECOVERABLE_ERROR, E_USER_ERROR, E_ERROR, E_CORE_ERROR, E_COMPILE_ERROR, E_PARSE), 'Warning' => Array (E_WARNING, E_USER_WARNING, E_CORE_WARNING, E_COMPILE_WARNING), 'Notice' => Array (E_NOTICE, E_USER_NOTICE, E_STRICT), ); if ( defined('E_DEPRECATED') ) { // Since PHP 5.3. $error_map['Deprecation Notice'] = array(E_DEPRECATED, E_USER_DEPRECATED); } foreach ($error_map as $error_name => $error_codes) { if ( in_array($error_code, $error_codes) ) { return $error_name; } } return ''; } /** * Returns profile total key (check against missing key too) * * @param string $key * @return int * @access private */ private function getProfilerTotal($key) { if ( isset($this->ProfilerTotalCount[$key]) ) { return (int)$this->ProfilerTotalCount[$key]; } return 0; } /** * Counts how much calls were made to a place, where this method is called (basic version of profiler) * * @param string $title * @param int $level * @return void * @access public */ public function ProfilePoint($title, $level = 1) { $trace_results = debug_backtrace(); $level = min($level, count($trace_results) - 1); do { $point = $trace_results[$level]; $location = $point['file'] . ':' . $point['line']; $level++; $has_more = isset($trace_results[$level]); } while ( $has_more && $point['function'] == $trace_results[$level]['function'] ); if ( !isset($this->ProfilePoints[$title]) ) { $this->ProfilePoints[$title] = Array (); } if ( !isset($this->ProfilePoints[$title][$location]) ) { $this->ProfilePoints[$title][$location] = 0; } $this->ProfilePoints[$title][$location]++; } /** * Generates report * * @param boolean $return_result Returns or output report contents. * @param boolean $clean_output_buffer Clean output buffers before displaying anything. * @param boolean $is_shutdown_func Called from shutdown function. * * @return string */ public function printReport($return_result = false, $clean_output_buffer = true, $is_shutdown_func = false) { if ( $this->_inReportPrinting ) { // don't print same report twice (in case if shutdown function used + compression + fatal error) return ''; } $this->_inReportPrinting = true; $last_error = error_get_last(); if ( !is_null($last_error) && $is_shutdown_func ) { $this->saveError( $last_error['type'], $last_error['message'], $last_error['file'], $last_error['line'], null, $is_shutdown_func ); } $this->profileFinish('script_runtime'); $this->_breakOutOfBuffering(!$return_result); $debugger_start = memory_get_usage(); if ( defined('SPACER_URL') ) { $this->dummyImage = SPACER_URL; } $this->InitReport(); // set parameters required by AJAX // defined here, because user can define this constant while script is running, not event before debugger is started DebuggerUtil::safeDefine('DBG_RAISE_ON_WARNINGS', 0); DebuggerUtil::safeDefine('DBG_TOOLBAR_BUTTONS', 1); $this->appendSession(); // show php session if any // ensure, that 1st line of debug output always is this one: $top_line = '
[Reload Frame] [Hide Debugger] [Clear Debugger][Current Time: ' . date('H:i:s') . '] [File Size: #DBG_FILESIZE#]
' . $this->getFilterDropdown() . '
'; $this->appendHTML($top_line); $this->moveToBegin(1); if ( count($this->ProfilePoints) > 0 ) { foreach ($this->ProfilePoints as $point => $locations) { arsort($this->ProfilePoints[$point]); } $this->appendHTML($this->highlightString($this->print_r($this->ProfilePoints, true))); } if ( DebuggerUtil::constOn('DBG_SQL_PROFILE') && isset($this->ProfilerTotals['sql']) ) { // sql query profiling was enabled -> show totals if ( array_key_exists('cachable_queries', $this->ProfilerTotalCount) ) { $append = ' Cachable queries: ' . $this->ProfilerTotalCount['cachable_queries']; } else { $append = ''; } $this->appendHTML('SQL Total time: ' . $this->ProfilerTotals['sql'] . ' Number of queries: ' . $this->ProfilerTotalCount['sql'] . $append); } if ( DebuggerUtil::constOn('DBG_PROFILE_INCLUDES') && isset($this->ProfilerTotals['includes']) ) { // included file profiling was enabled -> show totals $this->appendHTML('Included Files Total time: ' . $this->ProfilerTotals['includes'] . ' Number of includes: ' . $this->ProfilerTotalCount['includes']); } if ( DebuggerUtil::constOn('DBG_PROFILE_MEMORY') ) { // detailed memory usage reporting by objects was enabled -> show totals $this->appendHTML('Memory used by Objects: ' . round($this->ProfilerTotals['objects'] / 1024, 2) . 'Kb'); } if ( DebuggerUtil::constOn('DBG_INCLUDED_FILES') ) { $files = get_included_files(); $this->appendHTML('Included files:'); foreach ($files as $file) { $this->appendHTML($this->getFileLink($this->getLocalFile($file)) . ' (' . round(filesize($file) / 1024, 2) . 'Kb)'); } } if ( DebuggerUtil::constOn('DBG_PROFILE_INCLUDES') ) { $totals = $totals_configs = Array ('mem' => 0, 'time' => 0); $this->appendHTML('Included files statistics:' . (DebuggerUtil::constOn('DBG_SORT_INCLUDES_MEM') ? ' (sorted by memory usage)' : '')); if ( is_array($this->IncludesData['mem']) ) { if ( DebuggerUtil::constOn('DBG_SORT_INCLUDES_MEM') ) { array_multisort($this->IncludesData['mem'], SORT_DESC, $this->IncludesData['file'], $this->IncludesData['time'], $this->IncludesData['level']); } foreach ($this->IncludesData['file'] as $key => $file_name) { $this->appendHTML(str_repeat(' -> ', ($this->IncludesData['level'][$key] >= 0 ? $this->IncludesData['level'][$key] : 0)) . $file_name . ' Mem: ' . sprintf("%.4f Kb", $this->IncludesData['mem'][$key] / 1024) . ' Time: ' . sprintf("%.4f", $this->IncludesData['time'][$key])); if ( $this->IncludesData['level'][$key] == 0 ) { $totals['mem'] += $this->IncludesData['mem'][$key]; $totals['time'] += $this->IncludesData['time'][$key]; } elseif ( $this->IncludesData['level'][$key] == -1 ) { $totals_configs['mem'] += $this->IncludesData['mem'][$key]; $totals_configs['time'] += $this->IncludesData['time'][$key]; } } $this->appendHTML('Sub-Total classes: ' . ' Mem: ' . sprintf("%.4f Kb", $totals['mem'] / 1024) . ' Time: ' . sprintf("%.4f", $totals['time'])); $this->appendHTML('Sub-Total configs: ' . ' Mem: ' . sprintf("%.4f Kb", $totals_configs['mem'] / 1024) . ' Time: ' . sprintf("%.4f", $totals_configs['time'])); $this->appendHTML('Grand Total: ' . ' Mem: ' . sprintf("%.4f Kb", ($totals['mem'] + $totals_configs['mem']) / 1024) . ' Time: ' . sprintf("%.4f", $totals['time'] + $totals_configs['time'])); } } $skip_reporting = DebuggerUtil::constOn('DBG_SKIP_REPORTING') || DebuggerUtil::constOn('DBG_ZEND_PRESENT'); if ( ($this->_isAjax && !DebuggerUtil::constOn('DBG_SKIP_AJAX')) || !$skip_reporting ) { $debug_file = $this->tempFolder . '/debug_' . $this->rowSeparator . '.txt'; if ( file_exists($debug_file) ) { unlink($debug_file); } $i = 0; $fp = fopen($debug_file, 'a'); $lineCount = count($this->Data); while ( $i < $lineCount ) { $html = $this->prepareHTML($i); $row_type = $this->getRowType($i); fwrite($fp, json_encode(Array ('html' => $html, 'row_type' => $row_type)) . $this->rowSeparator); $i++; } fclose($fp); } if ( $skip_reporting ) { // let debugger write report and then don't output anything return ''; } $application =& kApplication::Instance(); $dbg_path = str_replace(FULL_PATH, '', $this->tempFolder); $debugger_params = Array ( 'FilterTypes' => $this->_filterTypes, 'RowSeparator' => $this->rowSeparator, 'ErrorsCount' => (int)$this->getProfilerTotal('error_handling'), 'IsFatalError' => $this->_fatalErrorHappened(), 'SQLCount' => (int)$this->getProfilerTotal('sql'), 'SQLTime' => isset($this->ProfilerTotals['sql']) ? sprintf('%.5f', $this->ProfilerTotals['sql']) : 0, 'ScriptTime' => sprintf('%.5f', $this->ProfilerData['script_runtime']['ends'] - $this->ProfilerData['script_runtime']['begins']), 'ScriptMemory' => DebuggerUtil::formatSize($this->getMemoryUsed($debugger_start)), 'Shortcut' => DBG_SHORTCUT, ); ob_start(); // the getShortReport($this->getMemoryUsed($debugger_start)); return $ret; } else { if ( !DebuggerUtil::constOn('DBG_HIDE_FULL_REPORT') ) { $this->_breakOutOfBuffering(); } elseif ( $clean_output_buffer ) { ob_clean(); } echo $this->getShortReport($this->getMemoryUsed($debugger_start)); } return ''; } function getFilterDropdown() { $filter_options = ''; foreach ( $this->_filterTypes as $filter_type ) { $filter_options .= ''; } return 'Show: '; } function getMemoryUsed($debugger_start) { if ( !isset($this->ProfilerTotals['error_handling']) ) { $memory_used = $debugger_start; $this->ProfilerTotalCount['error_handling'] = 0; } else { $memory_used = $debugger_start - $this->ProfilerTotals['error_handling']; } return $memory_used; } /** * Format's memory usage report by debugger * * @param int $memory_used * @return string * @access private */ private function getShortReport($memory_used) { if ( DebuggerUtil::constOn('DBG_TOOLBAR_BUTTONS') ) { // evenrything is in toolbar - don't duplicate return ''; } else { // toolbar not visible, then show sql & error count too $info = Array ( 'Script Runtime' => 'PROFILE:script_runtime', 'SQL\'s Runtime' => 'PROFILE_T:sql', '-' => 'SEP:-', 'Notice / Warning' => 'PROFILE_TC:error_handling', 'SQLs Count' => 'PROFILE_TC:sql', ); } $ret = ''; // 'Application:' . DebuggerUtil::formatSize($memory_used) . ' (' . $memory_used . ')'; foreach ($info as $title => $value_key) { list ($record_type, $record_data) = explode(':', $value_key, 2); switch ( $record_type ) { case 'PROFILE': // profiler totals value $Data =& $this->ProfilerData[$record_data]; $profile_time = ($Data['ends'] - $Data['begins']); // in seconds $ret .= '' . $title . ':' . sprintf('%.5f', $profile_time) . ' s'; break; case 'PROFILE_TC': // profile totals record count $record_cell = ''; if ( $record_data == 'error_handling' && $this->ProfilerTotalCount[$record_data] > 0 ) { $record_cell = ''; } $ret .= '' . $record_cell . $title . ':' . $record_cell . '' . $this->ProfilerTotalCount[$record_data] . ''; break; case 'PROFILE_T': // profile total $record_cell = ''; $total = array_key_exists($record_data, $this->ProfilerTotals) ? $this->ProfilerTotals[$record_data] : 0; $ret .= '' . $record_cell . $title . ':' . $record_cell . '' . sprintf('%.5f', $total) . ' s'; break; case 'SEP': $ret .= ''; break; } } return '
' . $ret . '
'; } /** * Detects if there was a fatal error at some point * * @return boolean */ private function _fatalErrorHappened() { return $this->_fatalErrorHash !== 0; } /** * Creates error hash * * @param string $errfile File, where error happened. * @param integer $errline Line in file, where error happened. * * @return integer */ private function _getErrorHash($errfile, $errline) { return crc32($errfile . ':' . $errline); } /** * User-defined error handler * * @param integer $errno Error code. * @param string $errstr Error message. * @param string $errfile Error file. * @param integer $errline Error line. * @param array $errcontext Error context. * @param boolean $is_shutdown_func Called from shutdown function. * * @return boolean * @throws Exception When unknown error code given. */ public function saveError( $errno, $errstr, $errfile = null, $errline = null, array $errcontext = null, $is_shutdown_func = false ) { $this->ProfilerData['error_handling']['begins'] = memory_get_usage(); $errorType = $this->getErrorNameByCode($errno); if (!$errorType) { throw new Exception('Unknown error type [' . $errno . ']'); return false; } elseif ( substr($errorType, 0, 5) == 'Fatal' ) { $this->_fatalErrorHash = $this->_getErrorHash($errfile, $errline); $this->appendTrace(4); } + elseif ( !kLogger::isErrorOriginAllowed($errfile) ) { + return; + } $this->expandError($errstr, $errfile, $errline); $this->Data[] = Array ( 'no' => $errno, 'str' => $errstr, 'file' => $errfile, 'line' => $errline, 'context' => $errcontext, 'debug_type' => 'error' ); $this->ProfilerData['error_handling']['ends'] = memory_get_usage(); $this->profilerAddTotal('error_handling', 'error_handling'); if ($errorType == 'Warning') { $this->WarningCount++; } if ( $this->_fatalErrorHappened() && $this->_getErrorHash($errfile, $errline) === $this->_fatalErrorHash ) { // Append debugger report to data in buffer & clean buffer afterwards. echo $this->_breakOutOfBuffering(false) . $this->printReport(true); if ( !$is_shutdown_func ) { exit; } } return true; } /** * Adds exception details into debugger but don't cause fatal error * * @param Exception $exception * @return void * @access public */ public function appendException($exception) { $this->ProfilerData['error_handling']['begins'] = memory_get_usage(); $this->appendExceptionTrace($exception); $errno = $exception->getCode(); $errstr = $exception->getMessage(); $errfile = $exception->getFile(); $errline = $exception->getLine(); $this->expandError($errstr, $errfile, $errline); $this->Data[] = Array ( 'no' => $errno, 'str' => $errstr, 'file' => $errfile, 'line' => $errline, 'exception_class' => get_class($exception), 'debug_type' => 'exception' ); $this->ProfilerData['error_handling']['ends'] = memory_get_usage(); $this->profilerAddTotal('error_handling', 'error_handling'); } /** * User-defined exception handler * * @param Exception $exception * @return void * @access public */ public function saveException($exception) { $this->appendException($exception); $this->_fatalErrorHash = $this->_getErrorHash($exception->getFile(), $exception->getLine()); // Append debugger report to data in buffer & clean buffer afterwards. echo $this->_breakOutOfBuffering(false) . $this->printReport(true); } /** * Transforms short error messages into long ones * * @param string $errstr * @param string $errfile * @param int $errline * @return void * @access private */ private function expandError(&$errstr, &$errfile, &$errline) { $errstr = kLogger::expandMessage($errstr); list ($errno, $errstr, $sql) = kLogger::parseDatabaseError($errstr); if ( $errno != 0 ) { $errstr = '' . $errstr . ' (' . $errno . ')
SQL: ' . $this->formatSQL($sql); } if ( strpos($errfile, 'eval()\'d code') !== false ) { $errstr = '[EVAL, line ' . $errline . ']: ' . $errstr; $tmpStr = $errfile; $pos = strpos($tmpStr, '('); $errfile = substr($tmpStr, 0, $pos); $pos++; $errline = substr($tmpStr, $pos, strpos($tmpStr, ')', $pos) - $pos); } } /** * Break buffering in case if fatal error is happened in the middle * * @param bool $flush * @return string * @access private */ private function _breakOutOfBuffering($flush = true) { $buffer_content = Array (); while ( ob_get_level() ) { $buffer_content[] = ob_get_clean(); } $ret = implode('', array_reverse($buffer_content)); if ( $flush ) { echo $ret; flush(); } return $ret; } /** * Saves given message to "vb_debug.txt" file in DocumentRoot * * @param string $msg * @return void * @access public */ public function saveToFile($msg) { $fp = fopen($_SERVER['DOCUMENT_ROOT'] . '/vb_debug.txt', 'a'); fwrite($fp, $msg . "\n"); fclose($fp); } /** * Prints given constant values in a table * * @param mixed $constants * @return void * @access public */ public function printConstants($constants) { if ( !is_array($constants) ) { $constants = explode(',', $constants); } $constant_tpl = '%s%s'; $ret = ''; foreach ($constants as $constant_name) { $ret .= sprintf($constant_tpl, $constant_name, constant($constant_name)); } $ret .= '
'; $this->appendHTML($ret); } /** * Attaches debugger to Application * * @return void * @access public */ public function AttachToApplication() { if ( !DebuggerUtil::constOn('DBG_HANDLE_ERRORS') ) { return; } if ( class_exists('kApplication') ) { $this->Application =& kApplication::Instance(); $this->Application->Debugger = $this; } // kLogger will auto-detect these automatically // error/exception handlers registered before debugger will be removed! set_error_handler( Array ($this, 'saveError') ); set_exception_handler( Array ($this, 'saveException') ); } /** * Returns HTML for tools section * * @return string * @access private */ private function _getToolsHTML() { $html = '
System Tools:
'; return $html; } /** * Returns HTML for dom viewer section * * @return string * @access private */ private function _getDomViewerHTML() { $html = '
DomViewer:
'; return $html; } } if ( !function_exists('memory_get_usage') ) { // PHP 4.x and compiled without --enable-memory-limit option function memory_get_usage() { return -1; } } if ( !DebuggerUtil::constOn('DBG_ZEND_PRESENT') ) { $debugger = new Debugger(); } if ( DebuggerUtil::constOn('DBG_USE_SHUTDOWN_FUNC') ) { register_shutdown_function(array(&$debugger, 'printReport'), false, true, true); } }