/usr/share/php/Nette/Tracy/BlueScreen.php is in php-nette 2.4-20160731-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 | <?php
/**
* This file is part of the Tracy (https://tracy.nette.org)
* Copyright (c) 2004 David Grudl (https://davidgrudl.com)
*/
namespace Tracy;
/**
* Red BlueScreen.
*/
class BlueScreen
{
/** @var string[] */
public $info = [];
/** @var callable[] */
private $panels = [];
/** @var string[] paths to be collapsed in stack trace (e.g. core libraries) */
public $collapsePaths = [];
/** @var int */
public $maxDepth = 3;
/** @var int */
public $maxLength = 150;
public function __construct()
{
$this->collapsePaths[] = preg_match('#(.+/vendor)/tracy/tracy/src/Tracy$#', strtr(__DIR__, '\\', '/'), $m)
? $m[1]
: __DIR__;
}
/**
* Add custom panel.
* @param callable
* @return self
*/
public function addPanel($panel)
{
if (!in_array($panel, $this->panels, TRUE)) {
$this->panels[] = $panel;
}
return $this;
}
/**
* Renders blue screen.
* @param \Exception|\Throwable
* @return void
*/
public function render($exception)
{
if (Helpers::isAjax() && session_status() === PHP_SESSION_ACTIVE) {
ob_start(function () {});
$this->renderTemplate($exception, __DIR__ . '/assets/BlueScreen/content.phtml');
$contentId = $_SERVER['HTTP_X_TRACY_AJAX'];
$queue = & $_SESSION['_tracy']['bluescreen'];
$queue = array_slice(array_filter((array) $queue), -5, NULL, TRUE);
$queue[$contentId] = ['content' => ob_get_clean(), 'dumps' => Dumper::fetchLiveData()];
} else {
$this->renderTemplate($exception, __DIR__ . '/assets/BlueScreen/page.phtml');
}
}
/**
* Renders blue screen to file (if file exists, it will not be overwritten).
* @param \Exception|\Throwable
* @param string file path
* @return void
*/
public function renderToFile($exception, $file)
{
if ($handle = @fopen($file, 'x')) {
ob_start(); // double buffer prevents sending HTTP headers in some PHP
ob_start(function ($buffer) use ($handle) { fwrite($handle, $buffer); }, 4096);
$this->renderTemplate($exception, __DIR__ . '/assets/BlueScreen/page.phtml');
ob_end_flush();
ob_end_clean();
fclose($handle);
}
}
private function renderTemplate($exception, $template)
{
$info = array_filter($this->info);
$source = Helpers::getSource();
$sourceIsUrl = preg_match('#^https?://#', $source);
$title = $exception instanceof \ErrorException
? Helpers::errorTypeToString($exception->getSeverity())
: Helpers::getClass($exception);
$skipError = $sourceIsUrl && $exception instanceof \ErrorException && !empty($exception->skippable)
? $source . (strpos($source, '?') ? '&' : '?') . '_tracy_skip_error'
: NULL;
$lastError = $exception instanceof \ErrorException || $exception instanceof \Error ? NULL : error_get_last();
$dump = function($v) {
return Dumper::toHtml($v, [
Dumper::DEPTH => $this->maxDepth,
Dumper::TRUNCATE => $this->maxLength,
Dumper::LIVE => TRUE,
Dumper::LOCATION => Dumper::LOCATION_CLASS,
]);
};
require $template;
}
/**
* @return \stdClass[]
*/
private function renderPanels($ex)
{
$obLevel = ob_get_level();
$res = [];
foreach ($this->panels as $callback) {
try {
$panel = call_user_func($callback, $ex);
if (empty($panel['tab']) || empty($panel['panel'])) {
continue;
}
$res[] = (object) $panel;
continue;
} catch (\Throwable $e) {
} catch (\Exception $e) {
}
while (ob_get_level() > $obLevel) { // restore ob-level if broken
ob_end_clean();
}
is_callable($callback, TRUE, $name);
$res[] = (object) [
'tab' => "Error in panel $name",
'panel' => nl2br(Helpers::escapeHtml($e)),
];
}
return $res;
}
/**
* Returns syntax highlighted source code.
* @param string
* @param int
* @param int
* @return string|NULL
*/
public static function highlightFile($file, $line, $lines = 15, array $vars = NULL)
{
$source = @file_get_contents($file); // @ file may not exist
if ($source) {
$source = static::highlightPhp($source, $line, $lines, $vars);
if ($editor = Helpers::editorUri($file, $line)) {
$source = substr_replace($source, ' data-tracy-href="' . Helpers::escapeHtml($editor) . '"', 4, 0);
}
return $source;
}
}
/**
* Returns syntax highlighted source code.
* @param string
* @param int
* @param int
* @return string
*/
public static function highlightPhp($source, $line, $lines = 15, array $vars = NULL)
{
if (function_exists('ini_set')) {
ini_set('highlight.comment', '#998; font-style: italic');
ini_set('highlight.default', '#000');
ini_set('highlight.html', '#06B');
ini_set('highlight.keyword', '#D24; font-weight: bold');
ini_set('highlight.string', '#080');
}
$source = str_replace(["\r\n", "\r"], "\n", $source);
$source = explode("\n", highlight_string($source, TRUE));
$out = $source[0]; // <code><span color=highlight.html>
$source = str_replace('<br />', "\n", $source[1]);
$out .= static::highlightLine($source, $line, $lines);
if ($vars) {
$out = preg_replace_callback('#">\$(\w+)( )?</span>#', function ($m) use ($vars) {
return array_key_exists($m[1], $vars)
? '" title="'
. str_replace('"', '"', trim(strip_tags(Dumper::toHtml($vars[$m[1]], [Dumper::DEPTH => 1]))))
. $m[0]
: $m[0];
}, $out);
}
$out = str_replace(' ', ' ', $out);
return "<pre class='code'><div>$out</div></pre>";
}
/**
* Returns highlighted line in HTML code.
* @return string
*/
public static function highlightLine($html, $line, $lines = 15)
{
$source = explode("\n", "\n" . str_replace("\r\n", "\n", $html));
$out = '';
$spans = 1;
$start = $i = max(1, min($line, count($source) - 1) - floor($lines * 2 / 3));
while (--$i >= 1) { // find last highlighted block
if (preg_match('#.*(</?span[^>]*>)#', $source[$i], $m)) {
if ($m[1] !== '</span>') {
$spans++;
$out .= $m[1];
}
break;
}
}
$source = array_slice($source, $start, $lines, TRUE);
end($source);
$numWidth = strlen((string) key($source));
foreach ($source as $n => $s) {
$spans += substr_count($s, '<span') - substr_count($s, '</span');
$s = str_replace(["\r", "\n"], ['', ''], $s);
preg_match_all('#<[^>]+>#', $s, $tags);
if ($n == $line) {
$out .= sprintf(
"<span class='highlight'>%{$numWidth}s: %s\n</span>%s",
$n,
strip_tags($s),
implode('', $tags[0])
);
} else {
$out .= sprintf("<span class='line'>%{$numWidth}s:</span> %s\n", $n, $s);
}
}
$out .= str_repeat('</span>', $spans) . '</code>';
return $out;
}
/**
* Should a file be collapsed in stack trace?
* @param string
* @return bool
*/
public function isCollapsed($file)
{
$file = strtr($file, '\\', '/') . '/';
foreach ($this->collapsePaths as $path) {
$path = strtr($path, '\\', '/') . '/';
if (strncmp($file, $path, strlen($path)) === 0) {
return TRUE;
}
}
return FALSE;
}
}
|