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