2009-08-19 05:55:23 +02:00
|
|
|
<?php
|
|
|
|
/**
|
2012-04-12 08:02:46 +02:00
|
|
|
* @package framework
|
2009-08-19 05:55:23 +02:00
|
|
|
* @subpackage dev
|
|
|
|
*/
|
API CHANGE: Renamed conflicting classes to have an "SS_" namespace, and renamed existing "SS" namespace to "SS_". The affected classes are: HTTPRequest, HTTPResponse, Query, Database, SSBacktrace, SSCli, SSDatetime, SSDatetimeTest, SSLog, SSLogTest, SSLogEmailWriter, SSLogErrorEmailFormatter, SSLogErrorFileFormatter, SSLogFileWriter and SSZendLog.
MINOR: Replaced usage of renamed classes with the new namespaced name.
From: Andrew Short <andrewjshort@gmail.com>
git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/trunk@90075 467b73ca-7a2a-4603-9d3b-597d59a354a9
2009-10-26 04:06:31 +01:00
|
|
|
class SS_Backtrace {
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2011-05-26 12:04:01 +02:00
|
|
|
/**
|
|
|
|
* @var array Replaces all arguments with a '<filtered>' string,
|
|
|
|
* mostly for security reasons. Use string values for global functions,
|
|
|
|
* and array notation for class methods.
|
|
|
|
* PHP's debug_backtrace() doesn't allow to inspect the argument names,
|
|
|
|
* so all arguments of the provided functions will be filtered out.
|
|
|
|
*/
|
2013-03-21 19:48:54 +01:00
|
|
|
private static $ignore_function_args = array(
|
2011-05-26 12:04:01 +02:00
|
|
|
'mysql_connect',
|
|
|
|
'mssql_connect',
|
|
|
|
'pg_connect',
|
2016-03-30 00:35:15 +02:00
|
|
|
array('PDO', '__construct'),
|
2013-10-30 11:36:07 +01:00
|
|
|
array('mysqli', 'mysqli'),
|
|
|
|
array('mysqli', 'select_db'),
|
2018-07-30 01:49:08 +02:00
|
|
|
array('mysqli', 'real_connect'),
|
2011-05-26 12:04:01 +02:00
|
|
|
array('DB', 'connect'),
|
|
|
|
array('Security', 'check_default_admin'),
|
|
|
|
array('Security', 'encrypt_password'),
|
|
|
|
array('Security', 'setDefaultAdmin'),
|
|
|
|
array('DB', 'createDatabase'),
|
|
|
|
array('Member', 'checkPassword'),
|
|
|
|
array('Member', 'changePassword'),
|
|
|
|
array('MemberPassword', 'checkPassword'),
|
|
|
|
array('PasswordValidator', 'validate'),
|
|
|
|
array('PasswordEncryptor_PHPHash', 'encrypt'),
|
|
|
|
array('PasswordEncryptor_PHPHash', 'salt'),
|
|
|
|
array('PasswordEncryptor_LegacyPHPHash', 'encrypt'),
|
|
|
|
array('PasswordEncryptor_LegacyPHPHash', 'salt'),
|
|
|
|
array('PasswordEncryptor_MySQLPassword', 'encrypt'),
|
|
|
|
array('PasswordEncryptor_MySQLPassword', 'salt'),
|
|
|
|
array('PasswordEncryptor_MySQLOldPassword', 'encrypt'),
|
|
|
|
array('PasswordEncryptor_MySQLOldPassword', 'salt'),
|
2012-05-02 03:51:29 +02:00
|
|
|
array('PasswordEncryptor_Blowfish', 'encrypt'),
|
|
|
|
array('PasswordEncryptor_Blowfish', 'salt'),
|
2011-05-26 12:04:01 +02:00
|
|
|
);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2009-08-19 05:55:23 +02:00
|
|
|
/**
|
|
|
|
* Return debug_backtrace() results with functions filtered
|
|
|
|
* specific to the debugging system, and not the trace.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2009-08-19 05:55:23 +02:00
|
|
|
* @param null|array $ignoredFunctions If an array, filter these functions out of the trace
|
|
|
|
* @return array
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public static function filtered_backtrace($ignoredFunctions = null) {
|
2009-08-19 05:55:23 +02:00
|
|
|
return self::filter_backtrace(debug_backtrace(), $ignoredFunctions);
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2009-08-19 05:55:23 +02:00
|
|
|
/**
|
|
|
|
* Filter a backtrace so that it doesn't show the calls to the
|
|
|
|
* debugging system, which is useless information.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2009-08-19 05:55:23 +02:00
|
|
|
* @param array $bt Backtrace to filter
|
|
|
|
* @param null|array $ignoredFunctions List of extra functions to filter out
|
|
|
|
* @return array
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public static function filter_backtrace($bt, $ignoredFunctions = null) {
|
2009-08-19 05:55:23 +02:00
|
|
|
$defaultIgnoredFunctions = array(
|
API CHANGE: Renamed conflicting classes to have an "SS_" namespace, and renamed existing "SS" namespace to "SS_". The affected classes are: HTTPRequest, HTTPResponse, Query, Database, SSBacktrace, SSCli, SSDatetime, SSDatetimeTest, SSLog, SSLogTest, SSLogEmailWriter, SSLogErrorEmailFormatter, SSLogErrorFileFormatter, SSLogFileWriter and SSZendLog.
MINOR: Replaced usage of renamed classes with the new namespaced name.
From: Andrew Short <andrewjshort@gmail.com>
git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/trunk@90075 467b73ca-7a2a-4603-9d3b-597d59a354a9
2009-10-26 04:06:31 +01:00
|
|
|
'SS_Log::log',
|
|
|
|
'SS_Backtrace::backtrace',
|
|
|
|
'SS_Backtrace::filtered_backtrace',
|
2009-08-19 05:55:23 +02:00
|
|
|
'Zend_Log_Writer_Abstract->write',
|
|
|
|
'Zend_Log->log',
|
|
|
|
'Zend_Log->__call',
|
|
|
|
'Zend_Log->err',
|
|
|
|
'DebugView->writeTrace',
|
|
|
|
'CliDebugView->writeTrace',
|
|
|
|
'Debug::emailError',
|
|
|
|
'Debug::warningHandler',
|
2009-08-19 08:03:57 +02:00
|
|
|
'Debug::noticeHandler',
|
2009-08-19 05:55:23 +02:00
|
|
|
'Debug::fatalHandler',
|
|
|
|
'errorHandler',
|
|
|
|
'Debug::showError',
|
|
|
|
'Debug::backtrace',
|
|
|
|
'exceptionHandler'
|
|
|
|
);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2009-08-19 05:55:23 +02:00
|
|
|
if($ignoredFunctions) foreach($ignoredFunctions as $ignoredFunction) {
|
|
|
|
$defaultIgnoredFunctions[] = $ignoredFunction;
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2009-08-19 05:55:23 +02:00
|
|
|
while($bt && in_array(self::full_func_name($bt[0]), $defaultIgnoredFunctions)) {
|
|
|
|
array_shift($bt);
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-03-21 19:48:54 +01:00
|
|
|
$ignoredArgs = Config::inst()->get('SS_Backtrace', 'ignore_function_args');
|
|
|
|
|
2011-05-26 12:04:01 +02:00
|
|
|
// Filter out arguments
|
|
|
|
foreach($bt as $i => $frame) {
|
|
|
|
$match = false;
|
2013-10-18 03:30:10 +02:00
|
|
|
if(!empty($bt[$i]['class'])) {
|
2013-03-21 19:48:54 +01:00
|
|
|
foreach($ignoredArgs as $fnSpec) {
|
2012-09-26 23:34:00 +02:00
|
|
|
if(is_array($fnSpec) && $bt[$i]['class'] == $fnSpec[0] && $bt[$i]['function'] == $fnSpec[1]) {
|
|
|
|
$match = true;
|
|
|
|
}
|
2011-05-26 12:04:01 +02:00
|
|
|
}
|
|
|
|
} else {
|
2013-03-21 19:48:54 +01:00
|
|
|
if(in_array($bt[$i]['function'], $ignoredArgs)) $match = true;
|
2011-05-26 12:04:01 +02:00
|
|
|
}
|
|
|
|
if($match) {
|
|
|
|
foreach($bt[$i]['args'] as $j => $arg) $bt[$i]['args'][$j] = '<filtered>';
|
|
|
|
}
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
|
|
|
return $bt;
|
2009-08-19 05:55:23 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2009-08-19 05:55:23 +02:00
|
|
|
/**
|
|
|
|
* Render or return a backtrace from the given scope.
|
|
|
|
*
|
|
|
|
* @param unknown_type $returnVal
|
|
|
|
* @param unknown_type $ignoreAjax
|
|
|
|
* @return unknown
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public static function backtrace($returnVal = false, $ignoreAjax = false, $ignoredFunctions = null) {
|
2009-08-19 05:55:23 +02:00
|
|
|
$plainText = Director::is_cli() || (Director::is_ajax() && !$ignoreAjax);
|
|
|
|
$result = self::get_rendered_backtrace(debug_backtrace(), $plainText, $ignoredFunctions);
|
|
|
|
if($returnVal) {
|
|
|
|
return $result;
|
|
|
|
} else {
|
|
|
|
echo $result;
|
|
|
|
}
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2009-08-19 05:55:23 +02:00
|
|
|
/**
|
2012-09-26 23:34:00 +02:00
|
|
|
* Return the full function name. If showArgs is set to true, a string representation of the arguments will be
|
|
|
|
* shown
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2017-11-30 18:49:46 +01:00
|
|
|
* @param SS_Object $item
|
2010-11-24 07:23:50 +01:00
|
|
|
* @param boolean $showArg
|
|
|
|
* @param Int $argCharLimit
|
|
|
|
* @return String
|
2009-08-19 05:55:23 +02:00
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public static function full_func_name($item, $showArgs = false, $argCharLimit = 10000) {
|
2009-08-19 05:55:23 +02:00
|
|
|
$funcName = '';
|
|
|
|
if(isset($item['class'])) $funcName .= $item['class'];
|
|
|
|
if(isset($item['type'])) $funcName .= $item['type'];
|
|
|
|
if(isset($item['function'])) $funcName .= $item['function'];
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2009-08-19 05:55:23 +02:00
|
|
|
if($showArgs && isset($item['args'])) {
|
|
|
|
$args = array();
|
|
|
|
foreach($item['args'] as $arg) {
|
|
|
|
if(!is_object($arg) || method_exists($arg, '__toString')) {
|
2011-10-28 23:49:17 +02:00
|
|
|
$sarg = is_array($arg) ? 'Array' : strval($arg);
|
|
|
|
$args[] = (strlen($sarg) > $argCharLimit) ? substr($sarg, 0, $argCharLimit) . '...' : $sarg;
|
2009-08-19 05:55:23 +02:00
|
|
|
} else {
|
|
|
|
$args[] = get_class($arg);
|
|
|
|
}
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2009-08-19 05:55:23 +02:00
|
|
|
$funcName .= "(" . implode(",", $args) .")";
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2009-08-19 05:55:23 +02:00
|
|
|
return $funcName;
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2009-08-19 05:55:23 +02:00
|
|
|
/**
|
|
|
|
* Render a backtrace array into an appropriate plain-text or HTML string.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2009-08-19 05:55:23 +02:00
|
|
|
* @param string $bt The trace array, as returned by debug_backtrace() or Exception::getTrace()
|
|
|
|
* @param boolean $plainText Set to false for HTML output, or true for plain-text output
|
|
|
|
* @param array List of functions that should be ignored. If not set, a default is provided
|
|
|
|
* @return string The rendered backtrace
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public static function get_rendered_backtrace($bt, $plainText = false, $ignoredFunctions = null) {
|
2009-08-19 05:55:23 +02:00
|
|
|
$bt = self::filter_backtrace($bt, $ignoredFunctions);
|
2012-04-14 14:05:49 +02:00
|
|
|
$result = ($plainText) ? '' : '<ul>';
|
2009-08-19 05:55:23 +02:00
|
|
|
foreach($bt as $item) {
|
|
|
|
if($plainText) {
|
|
|
|
$result .= self::full_func_name($item,true) . "\n";
|
2012-02-06 23:04:42 +01:00
|
|
|
if(isset($item['line']) && isset($item['file'])) $result .= basename($item['file']) . ":$item[line]\n";
|
2009-08-19 05:55:23 +02:00
|
|
|
$result .= "\n";
|
|
|
|
} else {
|
|
|
|
if ($item['function'] == 'user_error') {
|
|
|
|
$name = $item['args'][0];
|
|
|
|
} else {
|
|
|
|
$name = self::full_func_name($item,true);
|
|
|
|
}
|
2010-12-08 00:53:42 +01:00
|
|
|
$result .= "<li><b>" . htmlentities($name, ENT_COMPAT, 'UTF-8') . "</b>\n<br />\n";
|
2014-08-15 08:53:05 +02:00
|
|
|
$result .= isset($item['file']) ? htmlentities(basename($item['file']), ENT_COMPAT, 'UTF-8') : '';
|
2012-02-06 23:04:42 +01:00
|
|
|
$result .= isset($item['line']) ? ":$item[line]" : '';
|
2009-08-19 05:55:23 +02:00
|
|
|
$result .= "</li>\n";
|
|
|
|
}
|
|
|
|
}
|
2012-04-14 14:05:49 +02:00
|
|
|
if(!$plainText) $result .= '</ul>';
|
2009-08-19 05:55:23 +02:00
|
|
|
return $result;
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-03-24 04:04:52 +01:00
|
|
|
}
|