TYPO3 CMS  TYPO3_8-7
DebugExceptionHandler.php
Go to the documentation of this file.
1 <?php
2 namespace TYPO3\CMS\Core\Error;
3 
4 /*
5  * This file is part of the TYPO3 CMS project.
6  *
7  * It is free software; you can redistribute it and/or modify it under
8  * the terms of the GNU General Public License, either version 2
9  * of the License, or any later version.
10  *
11  * For the full copyright and license information, please read the
12  * LICENSE.txt file that was distributed with this source code.
13  *
14  * The TYPO3 project - inspiring people to share!
15  */
16 
25 {
29  public function __construct()
30  {
31  set_exception_handler([$this, 'handleException']);
32  }
33 
39  public function echoExceptionWeb(\Throwable $exception)
40  {
41  $this->sendStatusHeaders($exception);
42  $filePathAndName = $exception->getFile();
43  $exceptionCodeNumber = $exception->getCode() > 0 ? '#' . $exception->getCode() . ': ' : '';
44  $moreInformationLink = $exceptionCodeNumber !== ''
45  ? '(<a href="' . TYPO3_URL_EXCEPTION . 'debug/' . $exception->getCode() . '" target="_blank" rel="noopener noreferrer">More information</a>)'
46  : '';
47  $backtraceCode = $this->getBacktraceCode($exception->getTrace());
48  $this->writeLogEntries($exception, self::CONTEXT_WEB);
49  // Set the XML prologue
50  $xmlPrologue = '<?xml version="1.0" encoding="utf-8"?>';
51  // Set the doctype declaration
52  $docType = '<!DOCTYPE html
53  PUBLIC "-//W3C//DTD XHTML 1.1//EN"
54  "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">';
55  // Get the browser info
57  \TYPO3\CMS\Core\Utility\GeneralUtility::getIndpEnv('HTTP_USER_AGENT')
58  );
59  // Put the XML prologue before or after the doctype declaration according to browser
60  if ($browserInfo['browser'] === 'msie' && $browserInfo['version'] < 7) {
61  $headerStart = $docType . LF . $xmlPrologue;
62  } else {
63  $headerStart = $xmlPrologue . LF . $docType;
64  }
65  echo $headerStart . '
66  <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
67  <head>
68  <title>TYPO3 Exception</title>
69  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
70  <style type="text/css">
71  .ExceptionProperty {
72  color: #101010;
73  }
74  pre {
75  margin: 0;
76  font-size: 11px;
77  color: #515151;
78  background-color: #D0D0D0;
79  padding-left: 30px;
80  }
81  </style>
82  </head>
83  <body>
84  <div style="
85  position: absolute;
86  left: 10px;
87  background-color: #B9B9B9;
88  outline: 1px solid #515151;
89  color: #515151;
90  font-family: Arial, Helvetica, sans-serif;
91  font-size: 12px;
92  margin: 10px;
93  padding: 0;
94  right: 10px;
95  overflow: scroll;
96  max-height: calc(100% - 24px);
97  ">
98  <div style="width: 100%; background-color: #515151; color: white; padding: 2px; margin: 0 0 6px 0;">Uncaught TYPO3 Exception</div>
99  <div style="width: 100%; padding: 2px; margin: 0 0 6px 0;">
100  <strong style="color: #BE0027;">' . $exceptionCodeNumber . htmlspecialchars($exception->getMessage()) . '</strong> ' . $moreInformationLink . '<br />
101  <br />
102  <span class="ExceptionProperty">' . get_class($exception) . '</span> thrown in file<br />
103  <span class="ExceptionProperty">' . htmlspecialchars($filePathAndName) . '</span> in line
104  <span class="ExceptionProperty">' . $exception->getLine() . '</span>.<br />
105  <br />
106  ' . $backtraceCode . '
107  </div>
108  </div>
109  </body>
110  </html>
111  ';
112  }
113 
119  public function echoExceptionCLI(\Throwable $exception)
120  {
121  $filePathAndName = $exception->getFile();
122  $exceptionCodeNumber = $exception->getCode() > 0 ? '#' . $exception->getCode() . ': ' : '';
123  $this->writeLogEntries($exception, self::CONTEXT_CLI);
124  echo LF . 'Uncaught TYPO3 Exception ' . $exceptionCodeNumber . $exception->getMessage() . LF;
125  echo 'thrown in file ' . $filePathAndName . LF;
126  echo 'in line ' . $exception->getLine() . LF . LF;
127  die(1);
128  }
129 
136  protected function getBacktraceCode(array $trace)
137  {
138  $backtraceCode = '';
139  if (!empty($trace)) {
140  foreach ($trace as $index => $step) {
141  $class = isset($step['class']) ? htmlspecialchars($step['class']) . '<span style="color:white;">::</span>' : '';
142  $arguments = '';
143  if (isset($step['args']) && is_array($step['args'])) {
144  foreach ($step['args'] as $argument) {
145  $arguments .= (string)$arguments === '' ? '' : '<span style="color:white;">,</span> ';
146  if (is_object($argument)) {
147  $arguments .= '<span style="color:#FF8700;"><em>' . htmlspecialchars(get_class($argument)) . '</em></span>';
148  } elseif (is_string($argument)) {
149  $preparedArgument = strlen($argument) < 100
150  ? $argument
151  : substr($argument, 0, 50) . '#tripleDot#' . substr($argument, -50);
152  $preparedArgument = str_replace(
153  [
154  '#tripleDot#',
155  LF],
156  [
157  '<span style="color:white;">&hellip;</span>',
158  '<span style="color:white;">&crarr;</span>'
159  ],
160  htmlspecialchars($preparedArgument)
161  );
162  $arguments .= '"<span style="color:#FF8700;" title="' . htmlspecialchars($argument) . '">'
163  . $preparedArgument . '</span>"';
164  } elseif (is_numeric($argument)) {
165  $arguments .= '<span style="color:#FF8700;">' . (string)$argument . '</span>';
166  } else {
167  $arguments .= '<span style="color:#FF8700;"><em>' . gettype($argument) . '</em></span>';
168  }
169  }
170  }
171  $backtraceCode .= '<pre style="color:#69A550; background-color: #414141; padding: 4px 2px 4px 2px;">';
172  $backtraceCode .= '<span style="color:white;">' . (count($trace) - $index) . '</span> ' . $class
173  . $step['function'] . '<span style="color:white;">(' . $arguments . ')</span>';
174  $backtraceCode .= '</pre>';
175  if (isset($step['file'])) {
176  $backtraceCode .= $this->getCodeSnippet($step['file'], $step['line']) . '<br />';
177  }
178  }
179  }
180  return $backtraceCode;
181  }
182 
190  protected function getCodeSnippet($filePathAndName, $lineNumber)
191  {
192  $codeSnippet = '<br />';
193  if (@file_exists($filePathAndName)) {
194  $phpFile = @file($filePathAndName);
195  if (is_array($phpFile)) {
196  $startLine = $lineNumber > 2 ? $lineNumber - 2 : 1;
197  $phpFileCount = count($phpFile);
198  $endLine = $lineNumber < $phpFileCount - 2 ? $lineNumber + 3 : $phpFileCount + 1;
199  if ($endLine > $startLine) {
200  $codeSnippet = '<br /><span style="font-size:10px;">' . htmlspecialchars($filePathAndName) . ':</span><br /><pre>';
201  for ($line = $startLine; $line < $endLine; $line++) {
202  $codeLine = str_replace(TAB, ' ', $phpFile[$line - 1]);
203  if ($line === $lineNumber) {
204  $codeSnippet .= '</pre><pre style="background-color: #F1F1F1; color: black;">';
205  }
206  $codeSnippet .= sprintf('%05d', $line) . ': ' . htmlspecialchars($codeLine);
207  if ($line === $lineNumber) {
208  $codeSnippet .= '</pre><pre>';
209  }
210  }
211  $codeSnippet .= '</pre>';
212  }
213  }
214  }
215  return $codeSnippet;
216  }
217 }