TYPO3  7.6
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(array($this, 'handleException'));
32  }
33 
40  public function echoExceptionWeb(\Exception $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
57  $browserInfo = \TYPO3\CMS\Core\Utility\ClientUtility::getBrowserInfo(
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(\Exception $exception)
119  {
120  $filePathAndName = $exception->getFile();
121  $exceptionCodeNumber = $exception->getCode() > 0 ? '#' . $exception->getCode() . ': ' : '';
122  $this->writeLogEntries($exception, self::CONTEXT_CLI);
123  echo '
124 Uncaught TYPO3 Exception ' . $exceptionCodeNumber . $exception->getMessage() . LF;
125  echo 'thrown in file ' . $filePathAndName . LF;
126  echo 'in line ' . $exception->getLine() . '
127 
128 ';
129  die(1);
130  }
131 
138  protected function getBacktraceCode(array $trace)
139  {
140  $backtraceCode = '';
141  if (!empty($trace)) {
142  foreach ($trace as $index => $step) {
143  $class = isset($step['class']) ? $step['class'] . '<span style="color:white;">::</span>' : '';
144  $arguments = '';
145  if (isset($step['args']) && is_array($step['args'])) {
146  foreach ($step['args'] as $argument) {
147  $arguments .= (string)$arguments === '' ? '' : '<span style="color:white;">,</span> ';
148  if (is_object($argument)) {
149  $arguments .= '<span style="color:#FF8700;"><em>' . get_class($argument) . '</em></span>';
150  } elseif (is_string($argument)) {
151  $preparedArgument = strlen($argument) < 100
152  ? $argument
153  : substr($argument, 0, 50) . '#tripleDot#' . substr($argument, -50);
154  $preparedArgument = str_replace(
155  array(
156  '#tripleDot#',
157  LF),
158  array(
159  '<span style="color:white;">&hellip;</span>',
160  '<span style="color:white;">&crarr;</span>'
161  ),
162  htmlspecialchars($preparedArgument)
163  );
164  $arguments .= '"<span style="color:#FF8700;" title="' . htmlspecialchars($argument) . '">'
165  . $preparedArgument . '</span>"';
166  } elseif (is_numeric($argument)) {
167  $arguments .= '<span style="color:#FF8700;">' . (string)$argument . '</span>';
168  } else {
169  $arguments .= '<span style="color:#FF8700;"><em>' . gettype($argument) . '</em></span>';
170  }
171  }
172  }
173  $backtraceCode .= '<pre style="color:#69A550; background-color: #414141; padding: 4px 2px 4px 2px;">';
174  $backtraceCode .= '<span style="color:white;">' . (count($trace) - $index) . '</span> ' . $class
175  . $step['function'] . '<span style="color:white;">(' . $arguments . ')</span>';
176  $backtraceCode .= '</pre>';
177  if (isset($step['file'])) {
178  $backtraceCode .= $this->getCodeSnippet($step['file'], $step['line']) . '<br />';
179  }
180  }
181  }
182  return $backtraceCode;
183  }
184 
192  protected function getCodeSnippet($filePathAndName, $lineNumber)
193  {
194  $codeSnippet = '<br />';
195  if (@file_exists($filePathAndName)) {
196  $phpFile = @file($filePathAndName);
197  if (is_array($phpFile)) {
198  $startLine = $lineNumber > 2 ? $lineNumber - 2 : 1;
199  $phpFileCount = count($phpFile);
200  $endLine = $lineNumber < $phpFileCount - 2 ? $lineNumber + 3 : $phpFileCount + 1;
201  if ($endLine > $startLine) {
202  $codeSnippet = '<br /><span style="font-size:10px;">' . $filePathAndName . ':</span><br /><pre>';
203  for ($line = $startLine; $line < $endLine; $line++) {
204  $codeLine = str_replace(TAB, ' ', $phpFile[$line - 1]);
205  if ($line === $lineNumber) {
206  $codeSnippet .= '</pre><pre style="background-color: #F1F1F1; color: black;">';
207  }
208  $codeSnippet .= sprintf('%05d', $line) . ': ' . $codeLine;
209  if ($line === $lineNumber) {
210  $codeSnippet .= '</pre><pre>';
211  }
212  }
213  $codeSnippet .= '</pre>';
214  }
215  }
216  }
217  return $codeSnippet;
218  }
219 }