68103a9fda6010db3e4610e1d70b048ef82001cb
[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 /**
64 * TODO: 25.09.2009
65 * either remove this line or let the link point to site that offers error information for TYPO3
66 */
67
68 // $moreInformationLink = ($exceptionCodeNumber != '') ? '(<a href="' . TYPO3_URL_EXCEPTION . $exception->getCode() . '">More information</a>)' : '';
69 $backtraceCode = $this->getBacktraceCode($exception->getTrace());
70
71 $this->writeLogEntries($exception, self::CONTEXT_WEB);
72
73 // Set the XML prologue
74 $xmlPrologue = '<?xml version="1.0" encoding="utf-8"?>';
75
76 // Set the doctype declaration
77 $docType = '<!DOCTYPE html
78 PUBLIC "-//W3C//DTD XHTML 1.1//EN"
79 "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">';
80
81 // Get the browser info
82 $browserInfo = t3lib_utility_Client::getBrowserInfo(t3lib_div::getIndpEnv('HTTP_USER_AGENT'));
83
84 // Put the XML prologue before or after the doctype declaration according to browser
85 if ($browserInfo['browser'] === 'msie' && $browserInfo['version'] < 7) {
86 $headerStart = $docType . LF . $xmlPrologue;
87 } else {
88 $headerStart = $xmlPrologue . LF . $docType;
89 }
90
91 echo $headerStart . '
92 <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
93 <head>
94 <title>TYPO3 Exception</title>
95 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
96 <style type="text/css">
97 .ExceptionProperty {
98 color: #101010;
99 }
100 pre {
101 margin: 0;
102 font-size: 11px;
103 color: #515151;
104 background-color: #D0D0D0;
105 padding-left: 30px;
106 }
107 </style>
108 </head>
109 <body>
110 <div style="
111 position: absolute;
112 left: 10px;
113 background-color: #B9B9B9;
114 outline: 1px solid #515151;
115 color: #515151;
116 font-family: Arial, Helvetica, sans-serif;
117 font-size: 12px;
118 margin: 10px;
119 padding: 0;
120 ">
121 <div style="width: 100%; background-color: #515151; color: white; padding: 2px; margin: 0 0 6px 0;">Uncaught TYPO3 Exception</div>
122 <div style="width: 100%; padding: 2px; margin: 0 0 6px 0;">
123 <strong style="color: #BE0027;">' . $exceptionCodeNumber . $exception->getMessage() . '</strong> ' . /* $moreInformationLink .*/
124 '<br />
125 <br />
126 <span class="ExceptionProperty">' . get_class($exception) . '</span> thrown in file<br />
127 <span class="ExceptionProperty">' . $filePathAndName . '</span> in line
128 <span class="ExceptionProperty">' . $exception->getLine() . '</span>.<br />
129 <br />
130 ' . $backtraceCode . '
131 </div>
132 </div>
133 </body>
134 </html>
135 ';
136 }
137
138 /**
139 * Formats and echoes the exception for the command line
140 *
141 * @param Exception $exception The exception object
142 * @return void
143 * @author Robert Lemke <robert@typo3.org>
144 */
145 public function echoExceptionCLI(Exception $exception) {
146 $filePathAndName = $exception->getFile();
147 $exceptionCodeNumber = ($exception->getCode() > 0) ? '#' . $exception->getCode() . ': ' : '';
148 $this->writeLogEntries($exception, self::CONTEXT_CLI);
149
150 echo "\nUncaught TYPO3 Exception " . $exceptionCodeNumber . $exception->getMessage() . LF;
151 echo "thrown in file " . $filePathAndName . LF;
152 echo "in line " . $exception->getLine() . "\n\n";
153 }
154
155 /**
156 * Renders some backtrace
157 *
158 * @param array $trace The trace
159 * @return string Backtrace information
160 * @author Robert Lemke <robert@typo3.org>
161 */
162 protected function getBacktraceCode(array $trace) {
163 $backtraceCode = '';
164 if (count($trace)) {
165 foreach ($trace as $index => $step) {
166 $class = isset($step['class']) ? $step['class'] . '<span style="color:white;">::</span>' : '';
167
168 $arguments = '';
169 if (isset($step['args']) && is_array($step['args'])) {
170 foreach ($step['args'] as $argument) {
171 $arguments .= (strlen($arguments) === 0) ? '' : '<span style="color:white;">,</span> ';
172 if (is_object($argument)) {
173 $arguments .= '<span style="color:#FF8700;"><em>' . get_class($argument) . '</em></span>';
174 } elseif (is_string($argument)) {
175 $preparedArgument = (strlen($argument) < 100) ? $argument : substr($argument, 0, 50) . '#tripleDot#' . substr($argument, -50);
176 $preparedArgument = htmlspecialchars($preparedArgument);
177 $preparedArgument = str_replace('#tripleDot#', '<span style="color:white;">&hellip;</span>', $preparedArgument);
178 $preparedArgument = str_replace(LF, '<span style="color:white;">&crarr;</span>', $preparedArgument);
179 $arguments .= '"<span style="color:#FF8700;" title="' . htmlspecialchars($argument) . '">' . $preparedArgument . '</span>"';
180 } elseif (is_numeric($argument)) {
181 $arguments .= '<span style="color:#FF8700;">' . (string) $argument . '</span>';
182 } else {
183 $arguments .= '<span style="color:#FF8700;"><em>' . gettype($argument) . '</em></span>';
184 }
185 }
186 }
187
188 $backtraceCode .= '<pre style="color:#69A550; background-color: #414141; padding: 4px 2px 4px 2px;">';
189 $backtraceCode .= '<span style="color:white;">' . (count($trace) - $index) . '</span> ' . $class . $step['function'] . '<span style="color:white;">(' . $arguments . ')</span>';
190 $backtraceCode .= '</pre>';
191
192 if (isset($step['file'])) {
193 $backtraceCode .= $this->getCodeSnippet($step['file'], $step['line']) . '<br />';
194 }
195 }
196 }
197
198 return $backtraceCode;
199 }
200
201 /**
202 * Returns a code snippet from the specified file.
203 *
204 * @param string $filePathAndName Absolute path and file name of the PHP file
205 * @param integer $lineNumber Line number defining the center of the code snippet
206 * @return string The code snippet
207 * @author Robert Lemke <robert@typo3.org>
208 */
209 protected function getCodeSnippet($filePathAndName, $lineNumber) {
210 $codeSnippet = '<br />';
211 if (@file_exists($filePathAndName)) {
212 $phpFile = @file($filePathAndName);
213 if (is_array($phpFile)) {
214 $startLine = ($lineNumber > 2) ? ($lineNumber - 2) : 1;
215 $endLine = ($lineNumber < (count($phpFile) - 2)) ? ($lineNumber + 3) : count($phpFile) + 1;
216 if ($endLine > $startLine) {
217 $codeSnippet = '<br /><span style="font-size:10px;">' . $filePathAndName . ':</span><br /><pre>';
218 for ($line = $startLine; $line < $endLine; $line++) {
219 $codeLine = str_replace(TAB, ' ', $phpFile[$line - 1]);
220
221 if ($line === $lineNumber) {
222 $codeSnippet .= '</pre><pre style="background-color: #F1F1F1; color: black;">';
223 }
224 $codeSnippet .= sprintf('%05d', $line) . ': ' . $codeLine;
225 if ($line === $lineNumber) {
226 $codeSnippet .= '</pre><pre>';
227 }
228 }
229 $codeSnippet .= '</pre>';
230 }
231 }
232 }
233 return $codeSnippet;
234 }
235 }
236
237
238 if (defined('TYPO3_MODE') && isset($GLOBALS['TYPO3_CONF_VARS'][TYPO3_MODE]['XCLASS']['t3lib/error/class.t3lib_error_debugexceptionhandler.php'])) {
239 include_once($GLOBALS['TYPO3_CONF_VARS'][TYPO3_MODE]['XCLASS']['t3lib/error/class.t3lib_error_debugexceptionhandler.php']);
240 }
241
242 ?>