ed1a740a14ff4eb5a06147445f04bc823b009559
[Packages/TYPO3.CMS.git] / typo3 / sysext / core / Classes / Error / DebugExceptionHandler.php
1 <?php
2 declare(strict_types = 1);
3 namespace TYPO3\CMS\Core\Error;
4
5 /*
6 * This file is part of the TYPO3 CMS project.
7 *
8 * It is free software; you can redistribute it and/or modify it under
9 * the terms of the GNU General Public License, either version 2
10 * of the License, or any later version.
11 *
12 * For the full copyright and license information, please read the
13 * LICENSE.txt file that was distributed with this source code.
14 *
15 * The TYPO3 project - inspiring people to share!
16 */
17
18 /**
19 * A basic but solid exception handler which catches everything which
20 * falls through the other exception handlers and provides useful debugging
21 * information.
22 */
23 class DebugExceptionHandler extends AbstractExceptionHandler
24 {
25 /**
26 * Constructs this exception handler - registers itself as the default exception handler.
27 */
28 public function __construct()
29 {
30 set_exception_handler([$this, 'handleException']);
31 }
32
33 /**
34 * Formats and echoes the exception as XHTML.
35 *
36 * @param \Throwable $exception The throwable object.
37 */
38 public function echoExceptionWeb(\Throwable $exception)
39 {
40 $this->sendStatusHeaders($exception);
41 $this->writeLogEntries($exception, self::CONTEXT_WEB);
42
43 $content = $this->getContent($exception);
44 $css = $this->getStylesheet();
45
46 echo <<<HTML
47 <!DOCTYPE html>
48 <html>
49 <head>
50 <meta charset="UTF-8" />
51 <title>TYPO3 Exception</title>
52 <meta name="robots" content="noindex,nofollow" />
53 <style>$css</style>
54 </head>
55 <body>
56 $content
57 </body>
58 </html>
59 HTML;
60 }
61
62 /**
63 * Formats and echoes the exception for the command line
64 *
65 * @param \Throwable $exception The throwable object.
66 */
67 public function echoExceptionCLI(\Throwable $exception)
68 {
69 $filePathAndName = $exception->getFile();
70 $exceptionCodeNumber = $exception->getCode() > 0 ? '#' . $exception->getCode() . ': ' : '';
71 $this->writeLogEntries($exception, self::CONTEXT_CLI);
72 echo LF . 'Uncaught TYPO3 Exception ' . $exceptionCodeNumber . $exception->getMessage() . LF;
73 echo 'thrown in file ' . $filePathAndName . LF;
74 echo 'in line ' . $exception->getLine() . LF . LF;
75 die(1);
76 }
77
78 /**
79 * Generates the HTML for the error output.
80 *
81 * @param \Throwable $throwable
82 * @return string
83 */
84 protected function getContent(\Throwable $throwable): string
85 {
86 $content = '';
87
88 // exceptions can be chained
89 // for easier debugging, all exceptions are displayed to the developer
90 $throwables = $this->getAllThrowables($throwable);
91 $count = count($throwables);
92 foreach ($throwables as $position => $e) {
93 $content .= $this->getSingleThrowableContent($e, $position + 1, $count);
94 }
95
96 $exceptionInfo = '';
97 if ($throwable->getCode() > 0) {
98 $wikiLink = TYPO3_URL_EXCEPTION . 'debug/' . $throwable->getCode();
99 $exceptionInfo = <<<INFO
100 <div class="container">
101 <div class="callout">
102 <h4 class="callout-title">Get help in the TYPO3 Wiki</h4>
103 <div class="callout-body">
104 <p>
105 If you need help solving this exception, you can have a look at the TYPO3 Wiki.
106 There you can find solutions provided by the TYPO3 community.
107 Once you have found a solution to the problem, help others by contributing to the wiki page.
108 </p>
109 <p>
110 <a href="$wikiLink" target="_blank">Find a solution for this exception in the TYPO3 wiki.</a>
111 </p>
112 </div>
113 </div>
114 </div>
115 INFO;
116 }
117
118 $typo3Logo = $this->getTypo3LogoAsSvg();
119
120 return <<<HTML
121 <div class="exception-page">
122 <div class="exception-summary">
123 <div class="container">
124 <div class="exception-message-wrapper">
125 <div class="exception-illustration hidden-xs-down">$typo3Logo</div>
126 <h1 class="exception-message break-long-words">Whoops, looks like something went wrong.</h1>
127 </div>
128 </div>
129 </div>
130
131 $exceptionInfo
132
133 <div class="container">
134 $content
135 </div>
136 </div>
137 HTML;
138 }
139
140 /**
141 * Renders the HTML for a single throwable.
142 *
143 * @param \Throwable $throwable
144 * @param int $index
145 * @param int $total
146 * @return string
147 */
148 protected function getSingleThrowableContent(\Throwable $throwable, int $index, int $total): string
149 {
150 $exceptionTitle = $this->formatClass(get_class($throwable));
151 $exceptionMessage = $this->escapeHtml($throwable->getMessage());
152
153 // The trace does not contain the step where the exception is thrown.
154 // To display it as well it is added manually to the trace.
155 $trace = $throwable->getTrace();
156 array_unshift($trace, [
157 'file' => $throwable->getFile(),
158 'line' => $throwable->getLine(),
159 'args' => [],
160 ]);
161
162 $backtraceCode = $this->getBacktraceCode($trace);
163
164 return <<<HTML
165 <div class="trace">
166 <div class="trace-head">
167 <h3 class="trace-class">
168 <span class="text-muted">({$index}/{$total})</span>
169 <span class="exception-title">{$exceptionTitle}</span>
170 </h3>
171 <p class="trace-message break-long-words">{$exceptionMessage}</p>
172 </div>
173 <div class="trace-body">
174 {$backtraceCode}
175 </div>
176 </div>
177 HTML;
178 }
179
180 /**
181 * Generates the stylesheet needed to display the error page.
182 *
183 * @return string
184 */
185 protected function getStylesheet(): string
186 {
187 return <<<STYLESHEET
188 html {
189 font-family: sans-serif;
190 line-height: 1.15;
191 -webkit-text-size-adjust: 100%;
192 -ms-text-size-adjust: 100%;
193 -ms-overflow-style: scrollbar;
194 -webkit-tap-highlight-color: transparent;
195 }
196 body {
197 margin: 0;
198 font-family: -apple-system,BlinkMacSystemFont,"Segoe UI",Roboto,"Helvetica Neue",Arial,sans-serif,"Apple Color Emoji","Segoe UI Emoji","Segoe UI Symbol","Noto Color Emoji";
199 font-size: 1rem;
200 font-weight: 400;
201 line-height: 1.5;
202 color: #212121;
203 text-align: left;
204 background-color: #eaeaea;
205 }
206
207 a {
208 color: #ff8700;
209 text-decoration: underline;
210 }
211
212 a:hover {
213 text-decoration: none;
214 }
215
216 abbr[title] {
217 border-bottom: none;
218 cursor: help;
219 text-decoration: none;
220 }
221
222 code,
223 kbd,
224 pre,
225 samp {
226 font-family: SFMono-Regular,Menlo,Monaco,Consolas,"Liberation Mono","Courier New",monospace;
227 font-size: 1em;
228 }
229
230 pre {
231 background-color: #ffffff;
232 overflow-x: auto;
233 border: 1px solid rgba(0,0,0,0.125);
234 }
235
236 pre span {
237 display: block;
238 line-height: 1.3em;
239 }
240
241 pre span:before {
242 display: inline-block;
243 content: attr(data-line);
244 border-right: 1px solid #b9b9b9;
245 margin-right: 0.5em;
246 padding-right: 0.5em;
247 background-color: #f4f4f4;
248 width: 4em;
249 text-align: right;
250 color: #515151;
251 }
252
253 pre span.highlight {
254 background-color: #cce5ff;
255 }
256
257 .break-long-words {
258 -ms-word-break: break-all;
259 word-break: break-all;
260 word-break: break-word;
261 -webkit-hyphens: auto;
262 -moz-hyphens: auto;
263 hyphens: auto;
264 }
265
266 .callout {
267 padding: 1.5rem;
268 background-color: #fff;
269 margin-bottom: 2em;
270 box-shadow: 0 2px 1px rgba(0,0,0,.15);
271 border-left: 3px solid #8c8c8c;
272 }
273
274 .callout-title {
275 margin: 0;
276 }
277
278 .callout-body p:last-child {
279 margin-bottom: 0;
280 }
281
282 .container {
283 max-width: 1140px;
284 margin: 0 auto;
285 padding: 0 30px;
286 }
287
288 .exception-page {
289 position: relative;
290 height: 100vh;
291 overflow-x: hidden;
292 overflow-y: scroll;
293 }
294
295 .exception-illustration {
296 width: 3em;
297 height: 3em;
298 float: left;
299 margin-right: 1rem;
300 }
301
302 .exception-illustration svg {
303 width: 100%;
304 }
305
306 .exception-illustration svg path {
307 fill: #ff8700;
308 }
309
310 .exception-summary {
311 background: #000000;
312 color: #fff;
313 padding: 1.5rem 0;
314 margin-bottom: 2rem;
315 }
316
317 .exception-summary h1 {
318 margin: 0;
319 }
320
321 .text-muted {
322 opacity: 0.5;
323 }
324
325 .trace {
326 background-color: #fff;
327 margin-bottom: 2rem;
328 box-shadow: 0 2px 1px rgba(0,0,0,.15);
329 }
330
331 .trace-arguments {
332 color: #8c8c8c;
333 }
334
335 .trace-body {
336 }
337
338 .trace-call {
339 margin-bottom: 1rem;
340 }
341
342 .trace-class {
343 margin: 0;
344 }
345
346 .trace-file pre {
347 margin-top: 1.5rem;
348 margin-bottom: 0;
349 }
350
351 .trace-head {
352 color: #721c24;
353 background-color: #f8d7da;
354 padding: 1.5rem;
355 }
356
357 .trace-message {
358 margin-bottom: 0;
359 }
360
361 .trace-step {
362 padding: 1.5rem;
363 border-bottom: 1px solid #b9b9b9;
364 }
365
366 .trace-step > *:first-child {
367 margin-top: 0;
368 }
369
370 .trace-step > *:last-child {
371 margin-bottom: 0;
372 }
373
374 .trace-step:nth-child(even)
375 {
376 background-color: #fafafa;
377 }
378
379 .trace-step:last-child {
380 border-bottom: none;
381 }
382 STYLESHEET;
383 }
384
385 /**
386 * Renders the backtrace as HTML.
387 *
388 * @param array $trace
389 * @return string
390 */
391 protected function getBacktraceCode(array $trace): string
392 {
393 $content = '';
394
395 foreach ($trace as $index => $step) {
396 $content .= '<div class="trace-step">';
397 $args = $this->flattenArgs($step['args']);
398
399 if (isset($step['function'])) {
400 $content .= '<div class="trace-call">' . sprintf(
401 'at <span class="trace-class">%s</span><span class="trace-type">%s</span><span class="trace-method">%s</span>(<span class="trace-arguments">%s</span>)',
402 $this->formatClass($step['class'] ?? ''),
403 $step['type'],
404 $step['function'],
405 $this->formatArgs($args)
406 ) . '</div>';
407 }
408
409 if (isset($step['file']) && isset($step['line'])) {
410 $content .= $this->getCodeSnippet($step['file'], $step['line']);
411 }
412
413 $content .= '</div>';
414 }
415
416 return $content;
417 }
418
419 /**
420 * Returns a code snippet from the specified file.
421 *
422 * @param string $filePathAndName Absolute path and file name of the PHP file
423 * @param int $lineNumber Line number defining the center of the code snippet
424 * @return string The code snippet
425 */
426 protected function getCodeSnippet(string $filePathAndName, int $lineNumber): string
427 {
428 $showLinesAround = 4;
429
430 $content = '<div class="trace-file">';
431 $content .= '<div class="trace-file-head">' . $this->formatPath($filePathAndName, $lineNumber) . '</div>';
432
433 if (@file_exists($filePathAndName)) {
434 $phpFile = @file($filePathAndName);
435 if (is_array($phpFile)) {
436 $startLine = $lineNumber > $showLinesAround ? $lineNumber - $showLinesAround : 1;
437 $phpFileCount = count($phpFile);
438 $endLine = $lineNumber < $phpFileCount - $showLinesAround ? $lineNumber + $showLinesAround + 1 : $phpFileCount + 1;
439 if ($endLine > $startLine) {
440 $content .= '<div class="trace-file-content">';
441 $content .= '<pre>';
442
443 for ($line = $startLine; $line < $endLine; $line++) {
444 $codeLine = str_replace(TAB, ' ', $phpFile[$line - 1]);
445 $spanClass = '';
446 if ($line === $lineNumber) {
447 $spanClass = 'highlight';
448 }
449
450 $content .= '<span class="' . $spanClass . '" data-line="' . $line . '">' . $this->escapeHtml($codeLine) . '</span>';
451 }
452
453 $content .= '</pre>';
454 $content .= '</div>';
455 }
456 }
457 }
458
459 $content .= '</div>';
460
461 return $content;
462 }
463
464 /**
465 * Formats a path adding a line number.
466 *
467 * @param string $path The full path of the file.
468 * @param int $line The line number.
469 * @return string
470 */
471 protected function formatPath(string $path, int $line): string
472 {
473 $file = $this->escapeHtml(preg_match('#[^/\\\\]*+$#', $path, $file) ? $file[0] : $path);
474
475 return sprintf(
476 '<span class="block trace-file-path">in <abbr title="%s%3$s"><strong>%s</strong>%s</abbr></span>',
477 $this->escapeHtml($path),
478 $file,
479 0 < $line ? ' line ' . $line : ''
480 );
481 }
482
483 /**
484 * Formats the arguments of a method call.
485 *
486 * @param array $args The flattened args of method/function call
487 * @return string
488 */
489 protected function formatArgs(array $args): string
490 {
491 $result = [];
492 foreach ($args as $key => $item) {
493 if ('object' === $item[0]) {
494 $formattedValue = sprintf('<em>object</em>(%s)', $this->formatClass($item[1]));
495 } elseif ('array' === $item[0]) {
496 $formattedValue = sprintf('<em>array</em>(%s)', is_array($item[1]) ? $this->formatArgs($item[1]) : $item[1]);
497 } elseif ('null' === $item[0]) {
498 $formattedValue = '<em>null</em>';
499 } elseif ('boolean' === $item[0]) {
500 $formattedValue = '<em>' . strtolower(var_export($item[1], true)) . '</em>';
501 } elseif ('resource' === $item[0]) {
502 $formattedValue = '<em>resource</em>';
503 } else {
504 $formattedValue = str_replace("\n", '', $this->escapeHtml(var_export($item[1], true)));
505 }
506
507 $result[] = \is_int($key) ? $formattedValue : sprintf("'%s' => %s", $this->escapeHtml($key), $formattedValue);
508 }
509
510 return implode(', ', $result);
511 }
512
513 protected function flattenArgs(array $args, int $level = 0, int &$count = 0): array
514 {
515 $result = [];
516 foreach ($args as $key => $value) {
517 if (++$count > 1e4) {
518 return ['array', '*SKIPPED over 10000 entries*'];
519 }
520 if ($value instanceof \__PHP_Incomplete_Class) {
521 // is_object() returns false on PHP<=7.1
522 $result[$key] = ['incomplete-object', $this->getClassNameFromIncomplete($value)];
523 } elseif (is_object($value)) {
524 $result[$key] = ['object', get_class($value)];
525 } elseif (is_array($value)) {
526 if ($level > 10) {
527 $result[$key] = ['array', '*DEEP NESTED ARRAY*'];
528 } else {
529 $result[$key] = ['array', $this->flattenArgs($value, $level + 1, $count)];
530 }
531 } elseif (null === $value) {
532 $result[$key] = ['null', null];
533 } elseif (is_bool($value)) {
534 $result[$key] = ['boolean', $value];
535 } elseif (is_int($value)) {
536 $result[$key] = ['integer', $value];
537 } elseif (is_float($value)) {
538 $result[$key] = ['float', $value];
539 } elseif (is_resource($value)) {
540 $result[$key] = ['resource', get_resource_type($value)];
541 } else {
542 $result[$key] = ['string', (string)$value];
543 }
544 }
545
546 return $result;
547 }
548
549 protected function getClassNameFromIncomplete(\__PHP_Incomplete_Class $value): string
550 {
551 $array = new \ArrayObject($value);
552
553 return $array['__PHP_Incomplete_Class_Name'];
554 }
555
556 protected function escapeHtml(string $str): string
557 {
558 return htmlspecialchars($str, ENT_COMPAT | ENT_SUBSTITUTE);
559 }
560
561 protected function formatClass(string $class): string
562 {
563 $parts = explode('\\', $class);
564 $shortClassName = array_pop($parts);
565
566 if (strpos($class, 'class@anonymous') === 0) {
567 $shortClassName = 'class@anonymous';
568 }
569
570 return sprintf('<abbr title="%s">%s</abbr>', $class, $shortClassName);
571 }
572
573 protected function getTypo3LogoAsSvg(): string
574 {
575 return <<<SVG
576 <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 16 16"><path d="M11.1 10.3c-.2 0-.3.1-.5.1C9 10.4 6.8 5 6.8 3.2c0-.7.2-.9.4-1.1-2 .2-4.2.9-4.9 1.8-.2.2-.3.6-.3 1 0 2.8 3 9.2 5.1 9.2 1 0 2.6-1.6 4-3.8m-1-8.4c1.9 0 3.9.3 3.9 1.4 0 2.2-1.4 4.9-2.1 4.9C10.6 8.3 9 4.7 9 2.9c0-.8.3-1 1.1-1"></path></svg>
577 SVG;
578 }
579
580 protected function getAllThrowables(\Throwable $throwable): array
581 {
582 $all = [$throwable];
583
584 while ($throwable = $throwable->getPrevious()) {
585 $all[] = $throwable;
586 }
587
588 return $all;
589 }
590 }