. ** ** debug.class - Debug Functionality for ITools */ /** * Debug functions */ class it_debug { var $level; static $force_html; /** * Constructor * @param $level Debug level. All debug() calls with a level <= $level are shown. */ function __construct($level=0) { $this->level = isset($GLOBALS['debug_level']) ? $GLOBALS['debug_level'] : $level; } /** * Output a message if the global debug level is higher or the same as $level * @param $text Message to display * @param $level Debug level */ function debug($text, $level = 0) { if ($this->level >= $level) { echo (isset($_SERVER['REMOTE_ADDR']) ? nl2br("$level $text\n") : "$level $text")."\n"; flush(); } } /** * Get source line of grandparent calling this function * @param $stacksoffs go up an extra $stacksoffs levels */ static function srcline($stackoffs = 0) { $stack = debug_backtrace(@constant('DEBUG_BACKTRACE_IGNORE_ARGS')); $line = $stack[1 + $stackoffs]['line']; $file = $stack[1 + $stackoffs]['file']; if (!isset($GLOBALS['it_debug::dump source'][$file])) $GLOBALS['it_debug::dump source'][$file] = @it::file($file); return $GLOBALS['it_debug::dump source'][$file][$line-1]; } /** * Prepare for debug output being echoed */ static function setup() { $GLOBALS['debug_noredir'] = 1; if (ob_get_level() == 0 && $_SERVER['REMOTE_ADDR']) ob_start(); # prevent later 'headers already sent' error if ($_SERVER['REMOTE_ADDR'] && !EDC('edplain') && !it::match('curl|wget', $_SERVER['HTTP_USER_AGENT'])) self::$force_html = true; } /** * Backend for functions D(), ED() and EDX() in functions.php * @param Origargs Array containing original arguments do ED() etc * @param $p['color'] Allow color [true] * @param $p['html'] Allow html [default based on REMOTE_ADDR] * @param $p['short'] Omit variable names * @return String representation of dump */ static function dump($args, $p = []) { $p += ['html' => !EDC('edplain') && (self::$force_html || $_SERVER['REMOTE_ADDR']), 'color' => true]; $htmlspecialchars = array("&" => "&", '<' => "<", '>' => ">"); if ((preg_match('/csv|txt|gif|jpg/', $_SERVER['PHP_SELF']) || preg_grep('#text/(calendar|css|javascript|json|plain|rfc822|xml)|application/#', headers_list()) || $GLOBALS['debug_edplain']) && !$GLOBALS['debug_edhtml']) ; else if ($p['html']) list($blue, $noblue) = $p['color'] ? ["", ""] : []; else if (defined('STDOUT') && posix_isatty(STDOUT) && $p['color']) list($blue, $noblue, $red, $nored, $ansiok) = getenv('IT_ED_BRIGHT') ? array("\033[34m", "\033[m", "\033[33m", "\033[m", 1) : array("\033[34m", "\033[m", "\033[31m", "\033[m", 1); $src = it_debug::srcline(1); list($dummy, $function, $paramlist) = preg_match('#\b(D|ED|EDC|EDX)\s*\((.*)#i', $src, $parts) ? $parts : array(); $paramtokens = token_get_all("\s*([^\n]+),#", 'var \$$1 = $2;', $values); $item = $head . "class $classname { $values }$tail"; } $item = preg_replace_callback('/\b(1[2-9]\d\d\d\d\d\d\d\d)\b(.*)/', function($m) { return $m[1] . $m[2] . " # " . date('Y-m-d H:i:s', $m[1]); }, $item); $item = preg_replace("#(=>?)\s*\n\s*(array|class)#", '$1 $2', $item); # array( and class on same line as key $item = preg_replace_callback('#array \(\s+([^(){};]{1,100}),\s+\)#', function($m) { return "array (" . preg_replace("#\n\s+#", " ", $m[1]) . ")"; }, $item); # short arrays on 1 line $item = preg_replace('#class (\S+) \{\s+([^({,;]+;)?\s+\}#', 'class $1 { $2 }', $item); # 1-element objects on 1 line #$item = preg_replace('#\{\s*var \$attr#', '{ var $attr', $item); # move $attr on same line $item = preg_replace("#\\(\s*\n\s*\\)#", "()", $item); # empty arrays on 1 line $item = preg_replace("# =>\s*\n\s+\(object\) array\(\n#", " => (object) array(\n", $item); #$item = preg_replace('#\s+var \$_(.|\n)*?;\s*\n#', "", $item); $item = "$red$item$nored"; if ($p['html']) $item = preg_replace("#(https?:)?//[^\s'\"]+#", "\$0", strtr($item, $htmlspecialchars)); # htmlspecialchars() does not like bad utf8 if (preg_match('/^[\'"]/', $var)) $r .= "$item "; else { $var = $p['short'] ? "" : "$blue$var=$noblue"; $r .= $previtem && preg_match("/\n/", $item . $previtem) ? "\n$var$item " : "$var$item "; $previtem = $item; } } if ($GLOBALS['debug_indent']) $r = str_repeat(" ", count(debug_backtrace(@constant('DEBUG_BACKTRACE_IGNORE_ARGS')))-3) . $r; # not it_html::Q(); we dont wanna load it_html in ultraclassloader debugging $title = htmlspecialchars(it_debug::backtrace(array('skiplevels' => 2)), ENT_COMPAT, "ISO-8859-1"); # skip this func as well as ED()/EDX() if ($p['html']) return "
$r
\n"; else return $ansiok || $p['short'] ? "$r\n" : "\xe2\x96\x88\xe2\x96\x8c" . preg_replace("#\n#", "\n\xe2\x96\x88 ", "$r") . "\n"; } /** * Return short stackdump * @param $p['levels'] number of stack levels to return (default: 0 = all) * @param $p['skiplevels'] number of stack levels to omit * @param $p['skipfiles'] regular expression of filenames to omit * @param $p['trace'] Stack trace to compact */ static function backtrace($p = array()) { if (!is_array($p)) $p = array('skiplevels' => $p); $p += array('levels' => 0, 'skiplevels'=> 0, 'skipfiles' => "###"); foreach ($p['trace'] ?: array_slice(debug_backtrace(@constant('DEBUG_BACKTRACE_IGNORE_ARGS')), $p['skiplevels']) as $call) { if (($fn = $call['file']) && !it::match($p['skipfiles'], $call['file'])) { $fn = (it::match('auto_prepend', $fn) ? basename(dirname(dirname($fn))) . "/" : (it::match('/(cgi|bin)/', $fn) ? basename(dirname($fn)) . "/" : "")) . basename($fn); $result[] = $fn . ":" . $call['line']; if (--$p['levels'] == 0) break; } } return implode(" ", (array)$result); } static function set($debug_string) { foreach (preg_split('/[.,]/', $debug_string) as $ultrad) { $ultravar = preg_split('/[-=:]/', $ultrad, 2); $GLOBALS["debug_$ultravar[0]"] = isset($ultravar[1]) ? $ultravar[1] : 1; } # keep debug parameters in subrequests $_SERVER['ULTRAHOSTNAME'] = it::replace(['^\.+' => '', '([^.])\.*$' => '\1.'], $debug_string) . $_SERVER['ULTRAHOSTNAME']; } } ?>