24c0821ae6f1466dfa6e96a2d405fb5ab824fbb7
[Packages/TYPO3.CMS.git] / typo3 / sysext / core / Classes / Error / DebugExceptionHandler.php
1 <?php
2 namespace TYPO3\CMS\Core\Error;
3
4 /***************************************************************
5 * Copyright notice
6 *
7 * (c) 2009-2013 Ingo Renner <ingo@typo3.org>
8 * All rights reserved
9 *
10 * This script is part of the TYPO3 project. The TYPO3 project is
11 * free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * The GNU General Public License can be found at
17 * http://www.gnu.org/copyleft/gpl.html.
18 *
19 * This script is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
23 *
24 * This copyright notice MUST APPEAR in all copies of the script!
25 ***************************************************************/
26 /**
27 * A basic but solid exception handler which catches everything which
28 * falls through the other exception handlers and provides useful debugging
29 * information.
30 *
31 * This file is a backport from FLOW3
32 *
33 * @author Ingo Renner <ingo@typo3.org>
34 */
35 class DebugExceptionHandler extends \TYPO3\CMS\Core\Error\AbstractExceptionHandler {
36
37 /**
38 * Constructs this exception handler - registers itself as the default exception handler.
39 *
40 * @author Robert Lemke <robert@typo3.org>
41 */
42 public function __construct() {
43 set_exception_handler(array($this, 'handleException'));
44 }
45
46 /**
47 * Formats and echoes the exception as XHTML.
48 *
49 * @param Exception $exception The exception object
50 * @return void
51 * @author Robert Lemke <robert@typo3.org>
52 */
53 public function echoExceptionWeb(\Exception $exception) {
54 $this->sendStatusHeaders($exception);
55 $filePathAndName = $exception->getFile();
56 $exceptionCodeNumber = $exception->getCode() > 0 ? '#' . $exception->getCode() . ': ' : '';
57 $moreInformationLink = $exceptionCodeNumber != '' ? '(<a href="' . TYPO3_URL_EXCEPTION . 'debug/' . $exception->getCode() . '" target="_blank">More information</a>)' : '';
58 $backtraceCode = $this->getBacktraceCode($exception->getTrace());
59 $this->writeLogEntries($exception, self::CONTEXT_WEB);
60 // Set the XML prologue
61 $xmlPrologue = '<?xml version="1.0" encoding="utf-8"?>';
62 // Set the doctype declaration
63 $docType = '<!DOCTYPE html
64 PUBLIC "-//W3C//DTD XHTML 1.1//EN"
65 "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">';
66 // Get the browser info
67 $browserInfo = \TYPO3\CMS\Core\Utility\ClientUtility::getBrowserInfo(\TYPO3\CMS\Core\Utility\GeneralUtility::getIndpEnv('HTTP_USER_AGENT'));
68 // Put the XML prologue before or after the doctype declaration according to browser
69 if ($browserInfo['browser'] === 'msie' && $browserInfo['version'] < 7) {
70 $headerStart = $docType . LF . $xmlPrologue;
71 } else {
72 $headerStart = $xmlPrologue . LF . $docType;
73 }
74 echo $headerStart . '
75 <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
76 <head>
77 <title>TYPO3 Exception</title>
78 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
79 <style type="text/css">
80 .ExceptionProperty {
81 color: #101010;
82 }
83 pre {
84 margin: 0;
85 font-size: 11px;
86 color: #515151;
87 background-color: #D0D0D0;
88 padding-left: 30px;
89 }
90 </style>
91 </head>
92 <body>
93 <div style="
94 position: absolute;
95 left: 10px;
96 background-color: #B9B9B9;
97 outline: 1px solid #515151;
98 color: #515151;
99 font-family: Arial, Helvetica, sans-serif;
100 font-size: 12px;
101 margin: 10px;
102 padding: 0;
103 ">
104 <div style="width: 100%; background-color: #515151; color: white; padding: 2px; margin: 0 0 6px 0;">Uncaught TYPO3 Exception</div>
105 <div style="width: 100%; padding: 2px; margin: 0 0 6px 0;">
106 <strong style="color: #BE0027;">' . $exceptionCodeNumber . htmlspecialchars($exception->getMessage()) . '</strong> ' . $moreInformationLink . '<br />
107 <br />
108 <span class="ExceptionProperty">' . get_class($exception) . '</span> thrown in file<br />
109 <span class="ExceptionProperty">' . htmlspecialchars($filePathAndName) . '</span> in line
110 <span class="ExceptionProperty">' . $exception->getLine() . '</span>.<br />
111 <br />
112 ' . $backtraceCode . '
113 </div>
114 </div>
115 </body>
116 </html>
117 ';
118 }
119
120 /**
121 * Formats and echoes the exception for the command line
122 *
123 * @param Exception $exception The exception object
124 * @return void
125 * @author Robert Lemke <robert@typo3.org>
126 */
127 public function echoExceptionCLI(\Exception $exception) {
128 $filePathAndName = $exception->getFile();
129 $exceptionCodeNumber = $exception->getCode() > 0 ? '#' . $exception->getCode() . ': ' : '';
130 $this->writeLogEntries($exception, self::CONTEXT_CLI);
131 echo '
132 Uncaught TYPO3 Exception ' . $exceptionCodeNumber . $exception->getMessage() . LF;
133 echo 'thrown in file ' . $filePathAndName . LF;
134 echo 'in line ' . $exception->getLine() . '
135
136 ';
137 die(1);
138 }
139
140 /**
141 * Renders some backtrace
142 *
143 * @param array $trace The trace
144 * @return string Backtrace information
145 * @author Robert Lemke <robert@typo3.org>
146 */
147 protected function getBacktraceCode(array $trace) {
148 $backtraceCode = '';
149 if (count($trace)) {
150 foreach ($trace as $index => $step) {
151 $class = isset($step['class']) ? $step['class'] . '<span style="color:white;">::</span>' : '';
152 $arguments = '';
153 if (isset($step['args']) && is_array($step['args'])) {
154 foreach ($step['args'] as $argument) {
155 $arguments .= strlen($arguments) === 0 ? '' : '<span style="color:white;">,</span> ';
156 if (is_object($argument)) {
157 $arguments .= '<span style="color:#FF8700;"><em>' . get_class($argument) . '</em></span>';
158 } elseif (is_string($argument)) {
159 $preparedArgument = strlen($argument) < 100 ? $argument : substr($argument, 0, 50) . '#tripleDot#' . substr($argument, -50);
160 $preparedArgument = htmlspecialchars($preparedArgument);
161 $preparedArgument = str_replace('#tripleDot#', '<span style="color:white;">&hellip;</span>', $preparedArgument);
162 $preparedArgument = str_replace(LF, '<span style="color:white;">&crarr;</span>', $preparedArgument);
163 $arguments .= '"<span style="color:#FF8700;" title="' . htmlspecialchars($argument) . '">' . $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 . $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
182 /**
183 * Returns a code snippet from the specified file.
184 *
185 * @param string $filePathAndName Absolute path and file name of the PHP file
186 * @param integer $lineNumber Line number defining the center of the code snippet
187 * @return string The code snippet
188 * @author Robert Lemke <robert@typo3.org>
189 */
190 protected function getCodeSnippet($filePathAndName, $lineNumber) {
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 $endLine = $lineNumber < count($phpFile) - 2 ? $lineNumber + 3 : count($phpFile) + 1;
197 if ($endLine > $startLine) {
198 $codeSnippet = '<br /><span style="font-size:10px;">' . $filePathAndName . ':</span><br /><pre>';
199 for ($line = $startLine; $line < $endLine; $line++) {
200 $codeLine = str_replace(TAB, ' ', $phpFile[$line - 1]);
201 if ($line === $lineNumber) {
202 $codeSnippet .= '</pre><pre style="background-color: #F1F1F1; color: black;">';
203 }
204 $codeSnippet .= sprintf('%05d', $line) . ': ' . $codeLine;
205 if ($line === $lineNumber) {
206 $codeSnippet .= '</pre><pre>';
207 }
208 }
209 $codeSnippet .= '</pre>';
210 }
211 }
212 }
213 return $codeSnippet;
214 }
215
216 }