TYPO3CMS  8
 All Classes Namespaces Files Functions Variables Pages
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 
40  public function echoExceptionWeb(\Throwable $exception)
41  {
42  $this->sendStatusHeaders($exception);
43  $filePathAndName = $exception->getFile();
44  $exceptionCodeNumber = $exception->getCode() > 0 ? '#' . $exception->getCode() . ': ' : '';
45  $moreInformationLink = $exceptionCodeNumber !== ''
46  ? '(<a href="' . TYPO3_URL_EXCEPTION . 'debug/' . $exception->getCode() . '" target="_blank">More information</a>)'
47  : '';
48  $backtraceCode = $this->getBacktraceCode($exception->getTrace());
49  $this->writeLogEntries($exception, self::CONTEXT_WEB);
50  // Set the XML prologue
51  $xmlPrologue = '<?xml version="1.0" encoding="utf-8"?>';
52  // Set the doctype declaration
53  $docType = '<!DOCTYPE html
54  PUBLIC "-//W3C//DTD XHTML 1.1//EN"
55  "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">';
56  // Get the browser info
58  \TYPO3\CMS\Core\Utility\GeneralUtility::getIndpEnv('HTTP_USER_AGENT')
59  );
60  // Put the XML prologue before or after the doctype declaration according to browser
61  if ($browserInfo['browser'] === 'msie' && $browserInfo['version'] < 7) {
62  $headerStart = $docType . LF . $xmlPrologue;
63  } else {
64  $headerStart = $xmlPrologue . LF . $docType;
65  }
66  echo $headerStart . '
67  <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
68  <head>
69  <title>TYPO3 Exception</title>
70  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
71  <style type="text/css">
72  .ExceptionProperty {
73  color: #101010;
74  }
75  pre {
76  margin: 0;
77  font-size: 11px;
78  color: #515151;
79  background-color: #D0D0D0;
80  padding-left: 30px;
81  }
82  </style>
83  </head>
84  <body>
85  <div style="
86  position: absolute;
87  left: 10px;
88  background-color: #B9B9B9;
89  outline: 1px solid #515151;
90  color: #515151;
91  font-family: Arial, Helvetica, sans-serif;
92  font-size: 12px;
93  margin: 10px;
94  padding: 0;
95  ">
96  <div style="width: 100%; background-color: #515151; color: white; padding: 2px; margin: 0 0 6px 0;">Uncaught TYPO3 Exception</div>
97  <div style="width: 100%; padding: 2px; margin: 0 0 6px 0;">
98  <strong style="color: #BE0027;">' . $exceptionCodeNumber . htmlspecialchars($exception->getMessage()) . '</strong> ' . $moreInformationLink . '<br />
99  <br />
100  <span class="ExceptionProperty">' . get_class($exception) . '</span> thrown in file<br />
101  <span class="ExceptionProperty">' . htmlspecialchars($filePathAndName) . '</span> in line
102  <span class="ExceptionProperty">' . $exception->getLine() . '</span>.<br />
103  <br />
104  ' . $backtraceCode . '
105  </div>
106  </div>
107  </body>
108  </html>
109  ';
110  }
111 
118  public function echoExceptionCLI(\Throwable $exception)
119  {
120  $filePathAndName = $exception->getFile();
121  $exceptionCodeNumber = $exception->getCode() > 0 ? '#' . $exception->getCode() . ': ' : '';
122  $this->writeLogEntries($exception, self::CONTEXT_CLI);
123  echo LF . 'Uncaught TYPO3 Exception ' . $exceptionCodeNumber . $exception->getMessage() . LF;
124  echo 'thrown in file ' . $filePathAndName . LF;
125  echo 'in line ' . $exception->getLine() . LF . LF;
126  die(1);
127  }
128 
135  protected function getBacktraceCode(array $trace)
136  {
137  $backtraceCode = '';
138  if (!empty($trace)) {
139  foreach ($trace as $index => $step) {
140  $class = isset($step['class']) ? htmlspecialchars($step['class']) . '<span style="color:white;">::</span>' : '';
141  $arguments = '';
142  if (isset($step['args']) && is_array($step['args'])) {
143  foreach ($step['args'] as $argument) {
144  $arguments .= (string)$arguments === '' ? '' : '<span style="color:white;">,</span> ';
145  if (is_object($argument)) {
146  $arguments .= '<span style="color:#FF8700;"><em>' . htmlspecialchars(get_class($argument)) . '</em></span>';
147  } elseif (is_string($argument)) {
148  $preparedArgument = strlen($argument) < 100
149  ? $argument
150  : substr($argument, 0, 50) . '#tripleDot#' . substr($argument, -50);
151  $preparedArgument = str_replace(
152  [
153  '#tripleDot#',
154  LF],
155  [
156  '<span style="color:white;">&hellip;</span>',
157  '<span style="color:white;">&crarr;</span>'
158  ],
159  htmlspecialchars($preparedArgument)
160  );
161  $arguments .= '"<span style="color:#FF8700;" title="' . htmlspecialchars($argument) . '">'
162  . $preparedArgument . '</span>"';
163  } elseif (is_numeric($argument)) {
164  $arguments .= '<span style="color:#FF8700;">' . (string)$argument . '</span>';
165  } else {
166  $arguments .= '<span style="color:#FF8700;"><em>' . gettype($argument) . '</em></span>';
167  }
168  }
169  }
170  $backtraceCode .= '<pre style="color:#69A550; background-color: #414141; padding: 4px 2px 4px 2px;">';
171  $backtraceCode .= '<span style="color:white;">' . (count($trace) - $index) . '</span> ' . $class
172  . $step['function'] . '<span style="color:white;">(' . $arguments . ')</span>';
173  $backtraceCode .= '</pre>';
174  if (isset($step['file'])) {
175  $backtraceCode .= $this->getCodeSnippet($step['file'], $step['line']) . '<br />';
176  }
177  }
178  }
179  return $backtraceCode;
180  }
181 
189  protected function getCodeSnippet($filePathAndName, $lineNumber)
190  {
191  $codeSnippet = '<br />';
192  if (@file_exists($filePathAndName)) {
193  $phpFile = @file($filePathAndName);
194  if (is_array($phpFile)) {
195  $startLine = $lineNumber > 2 ? $lineNumber - 2 : 1;
196  $phpFileCount = count($phpFile);
197  $endLine = $lineNumber < $phpFileCount - 2 ? $lineNumber + 3 : $phpFileCount + 1;
198  if ($endLine > $startLine) {
199  $codeSnippet = '<br /><span style="font-size:10px;">' . htmlspecialchars($filePathAndName) . ':</span><br /><pre>';
200  for ($line = $startLine; $line < $endLine; $line++) {
201  $codeLine = str_replace(TAB, ' ', $phpFile[$line - 1]);
202  if ($line === $lineNumber) {
203  $codeSnippet .= '</pre><pre style="background-color: #F1F1F1; color: black;">';
204  }
205  $codeSnippet .= sprintf('%05d', $line) . ': ' . htmlspecialchars($codeLine);
206  if ($line === $lineNumber) {
207  $codeSnippet .= '</pre><pre>';
208  }
209  }
210  $codeSnippet .= '</pre>';
211  }
212  }
213  }
214  return $codeSnippet;
215  }
216 }