TYPO3 CMS  TYPO3_6-2
DebugExceptionHandler.php
Go to the documentation of this file.
1 <?php
2 namespace TYPO3\CMS\Core\Error;
3 
26 
32  public function __construct() {
33  set_exception_handler(array($this, 'handleException'));
34  }
35 
43  public function echoExceptionWeb(\Exception $exception) {
44  $this->sendStatusHeaders($exception);
45  $filePathAndName = $exception->getFile();
46  $exceptionCodeNumber = $exception->getCode() > 0 ? '#' . $exception->getCode() . ': ' : '';
47  $moreInformationLink = $exceptionCodeNumber !== ''
48  ? '(<a href="' . TYPO3_URL_EXCEPTION . 'debug/' . $exception->getCode() . '" target="_blank">More information</a>)'
49  : '';
50  $backtraceCode = $this->getBacktraceCode($exception->getTrace());
51  $this->writeLogEntries($exception, self::CONTEXT_WEB);
52  // Set the XML prologue
53  $xmlPrologue = '<?xml version="1.0" encoding="utf-8"?>';
54  // Set the doctype declaration
55  $docType = '<!DOCTYPE html
56  PUBLIC "-//W3C//DTD XHTML 1.1//EN"
57  "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">';
58  // Get the browser info
60  \TYPO3\CMS\Core\Utility\GeneralUtility::getIndpEnv('HTTP_USER_AGENT')
61  );
62  // Put the XML prologue before or after the doctype declaration according to browser
63  if ($browserInfo['browser'] === 'msie' && $browserInfo['version'] < 7) {
64  $headerStart = $docType . LF . $xmlPrologue;
65  } else {
66  $headerStart = $xmlPrologue . LF . $docType;
67  }
68  echo $headerStart . '
69  <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
70  <head>
71  <title>TYPO3 Exception</title>
72  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
73  <style type="text/css">
74  .ExceptionProperty {
75  color: #101010;
76  }
77  pre {
78  margin: 0;
79  font-size: 11px;
80  color: #515151;
81  background-color: #D0D0D0;
82  padding-left: 30px;
83  }
84  </style>
85  </head>
86  <body>
87  <div style="
88  position: absolute;
89  left: 10px;
90  background-color: #B9B9B9;
91  outline: 1px solid #515151;
92  color: #515151;
93  font-family: Arial, Helvetica, sans-serif;
94  font-size: 12px;
95  margin: 10px;
96  padding: 0;
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 
121  public function echoExceptionCLI(\Exception $exception) {
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 
141  protected function getBacktraceCode(array $trace) {
142  $backtraceCode = '';
143  if (count($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 .= strlen($arguments) === 0 ? '' : '<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  array(
158  '#tripleDot#',
159  LF),
160  array(
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 
195  protected function getCodeSnippet($filePathAndName, $lineNumber) {
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  $endLine = $lineNumber < count($phpFile) - 2 ? $lineNumber + 3 : count($phpFile) + 1;
202  if ($endLine > $startLine) {
203  $codeSnippet = '<br /><span style="font-size:10px;">' . htmlspecialchars($filePathAndName) . ':</span><br /><pre>';
204  for ($line = $startLine; $line < $endLine; $line++) {
205  $codeLine = str_replace(TAB, ' ', $phpFile[$line - 1]);
206  if ($line === $lineNumber) {
207  $codeSnippet .= '</pre><pre style="background-color: #F1F1F1; color: black;">';
208  }
209  $codeSnippet .= sprintf('%05d', $line) . ': ' . htmlspecialchars($codeLine);
210  if ($line === $lineNumber) {
211  $codeSnippet .= '</pre><pre>';
212  }
213  }
214  $codeSnippet .= '</pre>';
215  }
216  }
217  }
218  return $codeSnippet;
219  }
220 
221 }
die
Definition: index.php:6