Skip to content
This repository
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 190 lines (175 sloc) 6.176 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189
<?php
/**
* @package framework
* @subpackage dev
*/
class SS_Backtrace {

/**
* @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.
*/
static $ignore_function_args = array(
'mysql_connect',
'mssql_connect',
'pg_connect',
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'),
array('PasswordEncryptor_Blowfish', 'encrypt'),
array('PasswordEncryptor_Blowfish', 'salt'),
);

/**
* Return debug_backtrace() results with functions filtered
* specific to the debugging system, and not the trace.
*
* @param null|array $ignoredFunctions If an array, filter these functions out of the trace
* @return array
*/
public static function filtered_backtrace($ignoredFunctions = null) {
return self::filter_backtrace(debug_backtrace(), $ignoredFunctions);
}

/**
* Filter a backtrace so that it doesn't show the calls to the
* debugging system, which is useless information.
*
* @param array $bt Backtrace to filter
* @param null|array $ignoredFunctions List of extra functions to filter out
* @return array
*/
public static function filter_backtrace($bt, $ignoredFunctions = null) {
$defaultIgnoredFunctions = array(
'SS_Log::log',
'SS_Backtrace::backtrace',
'SS_Backtrace::filtered_backtrace',
'Zend_Log_Writer_Abstract->write',
'Zend_Log->log',
'Zend_Log->__call',
'Zend_Log->err',
'DebugView->writeTrace',
'CliDebugView->writeTrace',
'Debug::emailError',
'Debug::warningHandler',
'Debug::noticeHandler',
'Debug::fatalHandler',
'errorHandler',
'Debug::showError',
'Debug::backtrace',
'exceptionHandler'
);

if($ignoredFunctions) foreach($ignoredFunctions as $ignoredFunction) {
$defaultIgnoredFunctions[] = $ignoredFunction;
}

while($bt && in_array(self::full_func_name($bt[0]), $defaultIgnoredFunctions)) {
array_shift($bt);
}

// Filter out arguments
foreach($bt as $i => $frame) {
$match = false;
if(@$bt[$i]['class']) {
foreach(self::$ignore_function_args as $fnSpec) {
if(is_array($fnSpec) && $bt[$i]['class'] == $fnSpec[0] && $bt[$i]['function'] == $fnSpec[1]) {
$match = true;
}
}
} else {
if(in_array($bt[$i]['function'], self::$ignore_function_args)) $match = true;
}
if($match) {
foreach($bt[$i]['args'] as $j => $arg) $bt[$i]['args'][$j] = '<filtered>';
}
}

return $bt;
}

/**
* Render or return a backtrace from the given scope.
*
* @param unknown_type $returnVal
* @param unknown_type $ignoreAjax
* @return unknown
*/
public static function backtrace($returnVal = false, $ignoreAjax = false, $ignoredFunctions = null) {
$plainText = Director::is_cli() || (Director::is_ajax() && !$ignoreAjax);
$result = self::get_rendered_backtrace(debug_backtrace(), $plainText, $ignoredFunctions);
if($returnVal) {
return $result;
} else {
echo $result;
}
}

/**
* Return the full function name. If showArgs is set to true, a string representation of the arguments will be
* shown
*
* @param Object $item
* @param boolean $showArg
* @param Int $argCharLimit
* @return String
*/
public static function full_func_name($item, $showArgs = false, $argCharLimit = 10000) {
$funcName = '';
if(isset($item['class'])) $funcName .= $item['class'];
if(isset($item['type'])) $funcName .= $item['type'];
if(isset($item['function'])) $funcName .= $item['function'];

if($showArgs && isset($item['args'])) {
$args = array();
foreach($item['args'] as $arg) {
if(!is_object($arg) || method_exists($arg, '__toString')) {
$sarg = is_array($arg) ? 'Array' : strval($arg);
$args[] = (strlen($sarg) > $argCharLimit) ? substr($sarg, 0, $argCharLimit) . '...' : $sarg;
} else {
$args[] = get_class($arg);
}
}

$funcName .= "(" . implode(",", $args) .")";
}

return $funcName;
}

/**
* Render a backtrace array into an appropriate plain-text or HTML string.
*
* @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
*/
public static function get_rendered_backtrace($bt, $plainText = false, $ignoredFunctions = null) {
$bt = self::filter_backtrace($bt, $ignoredFunctions);
$result = ($plainText) ? '' : '<ul>';
foreach($bt as $item) {
if($plainText) {
$result .= self::full_func_name($item,true) . "\n";
if(isset($item['line']) && isset($item['file'])) $result .= basename($item['file']) . ":$item[line]\n";
$result .= "\n";
} else {
if ($item['function'] == 'user_error') {
$name = $item['args'][0];
} else {
$name = self::full_func_name($item,true);
}
$result .= "<li><b>" . htmlentities($name, ENT_COMPAT, 'UTF-8') . "</b>\n<br />\n";
$result .= isset($item['file']) ? htmlentities(basename($item['file']), ENT_COMPAT, 'UTF-8') : '';
$result .= isset($item['line']) ? ":$item[line]" : '';
$result .= "</li>\n";
}
}
if(!$plainText) $result .= '</ul>';
return $result;
}

}
Something went wrong with that request. Please try again.