PageRenderTime 24ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

/web/vendor/tracy/tracy/src/Tracy/BlueScreen.php

https://gitlab.com/adam.kvita/MI-VMM-SIFT
PHP | 270 lines | 204 code | 28 blank | 38 comment | 17 complexity | 489d331b5b99ecca8cc977d26e609492 MD5 | raw file
  1. <?php
  2. /**
  3. * This file is part of the Tracy (https://tracy.nette.org)
  4. * Copyright (c) 2004 David Grudl (https://davidgrudl.com)
  5. */
  6. namespace Tracy;
  7. /**
  8. * Red BlueScreen.
  9. */
  10. class BlueScreen
  11. {
  12. /** @var string[] */
  13. public $info = [];
  14. /** @var callable[] */
  15. private $panels = [];
  16. /** @var string[] paths to be collapsed in stack trace (e.g. core libraries) */
  17. public $collapsePaths = [];
  18. /** @var int */
  19. public $maxDepth = 3;
  20. /** @var int */
  21. public $maxLength = 150;
  22. public function __construct()
  23. {
  24. $this->collapsePaths[] = preg_match('#(.+/vendor)/tracy/tracy/src/Tracy$#', strtr(__DIR__, '\\', '/'), $m)
  25. ? $m[1]
  26. : __DIR__;
  27. }
  28. /**
  29. * Add custom panel.
  30. * @param callable
  31. * @return self
  32. */
  33. public function addPanel($panel)
  34. {
  35. if (!in_array($panel, $this->panels, TRUE)) {
  36. $this->panels[] = $panel;
  37. }
  38. return $this;
  39. }
  40. /**
  41. * Renders blue screen.
  42. * @param \Exception|\Throwable
  43. * @return void
  44. */
  45. public function render($exception)
  46. {
  47. if (Helpers::isAjax() && session_status() === PHP_SESSION_ACTIVE) {
  48. ob_start(function () {});
  49. $this->renderTemplate($exception, __DIR__ . '/assets/BlueScreen/content.phtml');
  50. $contentId = $_SERVER['HTTP_X_TRACY_AJAX'];
  51. $queue = & $_SESSION['_tracy']['bluescreen'];
  52. $queue = array_slice(array_filter((array) $queue), -5, NULL, TRUE);
  53. $queue[$contentId] = ['content' => ob_get_clean(), 'dumps' => Dumper::fetchLiveData()];
  54. } else {
  55. $this->renderTemplate($exception, __DIR__ . '/assets/BlueScreen/page.phtml');
  56. }
  57. }
  58. /**
  59. * Renders blue screen to file (if file exists, it will not be overwritten).
  60. * @param \Exception|\Throwable
  61. * @param string file path
  62. * @return void
  63. */
  64. public function renderToFile($exception, $file)
  65. {
  66. if ($handle = @fopen($file, 'x')) {
  67. ob_start(); // double buffer prevents sending HTTP headers in some PHP
  68. ob_start(function ($buffer) use ($handle) { fwrite($handle, $buffer); }, 4096);
  69. $this->renderTemplate($exception, __DIR__ . '/assets/BlueScreen/page.phtml');
  70. ob_end_flush();
  71. ob_end_clean();
  72. fclose($handle);
  73. }
  74. }
  75. private function renderTemplate($exception, $template)
  76. {
  77. $info = array_filter($this->info);
  78. $source = Helpers::getSource();
  79. $sourceIsUrl = preg_match('#^https?://#', $source);
  80. $title = $exception instanceof \ErrorException
  81. ? Helpers::errorTypeToString($exception->getSeverity())
  82. : Helpers::getClass($exception);
  83. $skipError = $sourceIsUrl && $exception instanceof \ErrorException && !empty($exception->skippable)
  84. ? $source . (strpos($source, '?') ? '&' : '?') . '_tracy_skip_error'
  85. : NULL;
  86. $lastError = $exception instanceof \ErrorException || $exception instanceof \Error ? NULL : error_get_last();
  87. $dump = function($v) {
  88. return Dumper::toHtml($v, [
  89. Dumper::DEPTH => $this->maxDepth,
  90. Dumper::TRUNCATE => $this->maxLength,
  91. Dumper::LIVE => TRUE,
  92. Dumper::LOCATION => Dumper::LOCATION_CLASS,
  93. ]);
  94. };
  95. require $template;
  96. }
  97. /**
  98. * @return \stdClass[]
  99. */
  100. private function renderPanels($ex)
  101. {
  102. $obLevel = ob_get_level();
  103. $res = [];
  104. foreach ($this->panels as $callback) {
  105. try {
  106. $panel = call_user_func($callback, $ex);
  107. if (empty($panel['tab']) || empty($panel['panel'])) {
  108. continue;
  109. }
  110. $res[] = (object) $panel;
  111. continue;
  112. } catch (\Throwable $e) {
  113. } catch (\Exception $e) {
  114. }
  115. while (ob_get_level() > $obLevel) { // restore ob-level if broken
  116. ob_end_clean();
  117. }
  118. is_callable($callback, TRUE, $name);
  119. $res[] = (object) [
  120. 'tab' => "Error in panel $name",
  121. 'panel' => nl2br(Helpers::escapeHtml($e)),
  122. ];
  123. }
  124. return $res;
  125. }
  126. /**
  127. * Returns syntax highlighted source code.
  128. * @param string
  129. * @param int
  130. * @param int
  131. * @return string|NULL
  132. */
  133. public static function highlightFile($file, $line, $lines = 15, array $vars = NULL)
  134. {
  135. $source = @file_get_contents($file); // @ file may not exist
  136. if ($source) {
  137. $source = static::highlightPhp($source, $line, $lines, $vars);
  138. if ($editor = Helpers::editorUri($file, $line)) {
  139. $source = substr_replace($source, ' data-tracy-href="' . Helpers::escapeHtml($editor) . '"', 4, 0);
  140. }
  141. return $source;
  142. }
  143. }
  144. /**
  145. * Returns syntax highlighted source code.
  146. * @param string
  147. * @param int
  148. * @param int
  149. * @return string
  150. */
  151. public static function highlightPhp($source, $line, $lines = 15, array $vars = NULL)
  152. {
  153. if (function_exists('ini_set')) {
  154. ini_set('highlight.comment', '#998; font-style: italic');
  155. ini_set('highlight.default', '#000');
  156. ini_set('highlight.html', '#06B');
  157. ini_set('highlight.keyword', '#D24; font-weight: bold');
  158. ini_set('highlight.string', '#080');
  159. }
  160. $source = str_replace(["\r\n", "\r"], "\n", $source);
  161. $source = explode("\n", highlight_string($source, TRUE));
  162. $out = $source[0]; // <code><span color=highlight.html>
  163. $source = str_replace('<br />', "\n", $source[1]);
  164. $out .= static::highlightLine($source, $line, $lines);
  165. if ($vars) {
  166. $out = preg_replace_callback('#">\$(\w+)(&nbsp;)?</span>#', function ($m) use ($vars) {
  167. return array_key_exists($m[1], $vars)
  168. ? '" title="'
  169. . str_replace('"', '&quot;', trim(strip_tags(Dumper::toHtml($vars[$m[1]], [Dumper::DEPTH => 1]))))
  170. . $m[0]
  171. : $m[0];
  172. }, $out);
  173. }
  174. $out = str_replace('&nbsp;', ' ', $out);
  175. return "<pre class='code'><div>$out</div></pre>";
  176. }
  177. /**
  178. * Returns highlighted line in HTML code.
  179. * @return string
  180. */
  181. public static function highlightLine($html, $line, $lines = 15)
  182. {
  183. $source = explode("\n", "\n" . str_replace("\r\n", "\n", $html));
  184. $out = '';
  185. $spans = 1;
  186. $start = $i = max(1, min($line, count($source) - 1) - floor($lines * 2 / 3));
  187. while (--$i >= 1) { // find last highlighted block
  188. if (preg_match('#.*(</?span[^>]*>)#', $source[$i], $m)) {
  189. if ($m[1] !== '</span>') {
  190. $spans++;
  191. $out .= $m[1];
  192. }
  193. break;
  194. }
  195. }
  196. $source = array_slice($source, $start, $lines, TRUE);
  197. end($source);
  198. $numWidth = strlen((string) key($source));
  199. foreach ($source as $n => $s) {
  200. $spans += substr_count($s, '<span') - substr_count($s, '</span');
  201. $s = str_replace(["\r", "\n"], ['', ''], $s);
  202. preg_match_all('#<[^>]+>#', $s, $tags);
  203. if ($n == $line) {
  204. $out .= sprintf(
  205. "<span class='highlight'>%{$numWidth}s: %s\n</span>%s",
  206. $n,
  207. strip_tags($s),
  208. implode('', $tags[0])
  209. );
  210. } else {
  211. $out .= sprintf("<span class='line'>%{$numWidth}s:</span> %s\n", $n, $s);
  212. }
  213. }
  214. $out .= str_repeat('</span>', $spans) . '</code>';
  215. return $out;
  216. }
  217. /**
  218. * Should a file be collapsed in stack trace?
  219. * @param string
  220. * @return bool
  221. */
  222. public function isCollapsed($file)
  223. {
  224. $file = strtr($file, '\\', '/') . '/';
  225. foreach ($this->collapsePaths as $path) {
  226. $path = strtr($path, '\\', '/') . '/';
  227. if (strncmp($file, $path, strlen($path)) === 0) {
  228. return TRUE;
  229. }
  230. }
  231. return FALSE;
  232. }
  233. }