764699f2207f2f5d5c532c91b7013dae849e610f
[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 }
138
139 /**
140 * Renders some backtrace
141 *
142 * @param array $trace The trace
143 * @return string Backtrace information
144 * @author Robert Lemke <robert@typo3.org>
145 */
146 protected function getBacktraceCode(array $trace) {
147 $backtraceCode = '';
148 if (count($trace)) {
149 foreach ($trace as $index => $step) {
150 $class = isset($step['class']) ? $step['class'] . '<span style="color:white;">::</span>' : '';
151 $arguments = '';
152 if (isset($step['args']) && is_array($step['args'])) {
153 foreach ($step['args'] as $argument) {
154 $arguments .= strlen($arguments) === 0 ? '' : '<span style="color:white;">,</span> ';
155 if (is_object($argument)) {
156 $arguments .= '<span style="color:#FF8700;"><em>' . get_class($argument) . '</em></span>';
157 } elseif (is_string($argument)) {
158 $preparedArgument = strlen($argument) < 100 ? $argument : substr($argument, 0, 50) . '#tripleDot#' . substr($argument, -50);
159 $preparedArgument = htmlspecialchars($preparedArgument);
160 $preparedArgument = str_replace('#tripleDot#', '<span style="color:white;">&hellip;</span>', $preparedArgument);
161 $preparedArgument = str_replace(LF, '<span style="color:white;">&crarr;</span>', $preparedArgument);
162 $arguments .= '"<span style="color:#FF8700;" title="' . htmlspecialchars($argument) . '">' . $preparedArgument . '</span>"';
163 } elseif (is_numeric($argument)) {
164 $arguments .= '<span style="color:#FF8700;">' . (string) $argument . '</span>';
165 } else {
166 $arguments .= '<span style="color:#FF8700;"><em>' . gettype($argument) . '</em></span>';
167 }
168 }
169 }
170 $backtraceCode .= '<pre style="color:#69A550; background-color: #414141; padding: 4px 2px 4px 2px;">';
171 $backtraceCode .= '<span style="color:white;">' . (count($trace) - $index) . '</span> ' . $class . $step['function'] . '<span style="color:white;">(' . $arguments . ')</span>';
172 $backtraceCode .= '</pre>';
173 if (isset($step['file'])) {
174 $backtraceCode .= $this->getCodeSnippet($step['file'], $step['line']) . '<br />';
175 }
176 }
177 }
178 return $backtraceCode;
179 }
180
181 /**
182 * Returns a code snippet from the specified file.
183 *
184 * @param string $filePathAndName Absolute path and file name of the PHP file
185 * @param integer $lineNumber Line number defining the center of the code snippet
186 * @return string The code snippet
187 * @author Robert Lemke <robert@typo3.org>
188 */
189 protected function getCodeSnippet($filePathAndName, $lineNumber) {
190 $codeSnippet = '<br />';
191 if (@file_exists($filePathAndName)) {
192 $phpFile = @file($filePathAndName);
193 if (is_array($phpFile)) {
194 $startLine = $lineNumber > 2 ? $lineNumber - 2 : 1;
195 $endLine = $lineNumber < count($phpFile) - 2 ? $lineNumber + 3 : count($phpFile) + 1;
196 if ($endLine > $startLine) {
197 $codeSnippet = '<br /><span style="font-size:10px;">' . $filePathAndName . ':</span><br /><pre>';
198 for ($line = $startLine; $line < $endLine; $line++) {
199 $codeLine = str_replace(TAB, ' ', $phpFile[$line - 1]);
200 if ($line === $lineNumber) {
201 $codeSnippet .= '</pre><pre style="background-color: #F1F1F1; color: black;">';
202 }
203 $codeSnippet .= sprintf('%05d', $line) . ': ' . $codeLine;
204 if ($line === $lineNumber) {
205 $codeSnippet .= '</pre><pre>';
206 }
207 }
208 $codeSnippet .= '</pre>';
209 }
210 }
211 }
212 return $codeSnippet;
213 }
214
215 }
216
217
218 ?>