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