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