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