[TASK] Use fully qualified name resolution in PHP 5.5
[Packages/TYPO3.CMS.git] / typo3 / sysext / core / Classes / Error / ErrorHandler.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 use TYPO3\CMS\Core\Utility\GeneralUtility;
18
19 /**
20 * Global error handler for TYPO3
21 *
22 * This file is a backport from TYPO3 Flow
23 *
24 * @author Rupert Germann <rupi@gmx.li>
25 */
26 class ErrorHandler implements ErrorHandlerInterface {
27
28 /**
29 * Error levels which should result in an exception thrown.
30 *
31 * @var array
32 */
33 protected $exceptionalErrors = array();
34
35 /**
36 * Registers this class as default error handler
37 *
38 * @param int $errorHandlerErrors The integer representing the E_* error level which should be
39 */
40 public function __construct($errorHandlerErrors) {
41 $excludedErrors = E_COMPILE_WARNING | E_COMPILE_ERROR | E_CORE_WARNING | E_CORE_ERROR | E_PARSE | E_ERROR;
42 // reduces error types to those a custom error handler can process
43 $errorHandlerErrors = $errorHandlerErrors & ~$excludedErrors;
44 set_error_handler(array($this, 'handleError'), $errorHandlerErrors);
45 }
46
47 /**
48 * Defines which error levels should result in an exception thrown.
49 *
50 * @param int $exceptionalErrors The integer representing the E_* error level to handle as exceptions
51 * @return void
52 */
53 public function setExceptionalErrors($exceptionalErrors) {
54 $this->exceptionalErrors = (int)$exceptionalErrors;
55 }
56
57 /**
58 * Handles an error.
59 * If the error is registered as exceptionalError it will by converted into an exception, to be handled
60 * by the configured exceptionhandler. Additionally the error message is written to the configured logs.
61 * If TYPO3_MODE is 'BE' the error message is also added to the flashMessageQueue, in FE the error message
62 * is displayed in the admin panel (as TsLog message)
63 *
64 * @param int $errorLevel The error level - one of the E_* constants
65 * @param string $errorMessage The error message
66 * @param string $errorFile Name of the file the error occurred in
67 * @param int $errorLine Line number where the error occurred
68 * @return bool
69 * @throws Exception with the data passed to this method if the error is registered as exceptionalError
70 */
71 public function handleError($errorLevel, $errorMessage, $errorFile, $errorLine) {
72 // Don't do anything if error_reporting is disabled by an @ sign
73 if (error_reporting() === 0) {
74 return TRUE;
75 }
76 $errorLevels = array(
77 E_WARNING => 'Warning',
78 E_NOTICE => 'Notice',
79 E_USER_ERROR => 'User Error',
80 E_USER_WARNING => 'User Warning',
81 E_USER_NOTICE => 'User Notice',
82 E_STRICT => 'Runtime Notice',
83 E_RECOVERABLE_ERROR => 'Catchable Fatal Error',
84 E_DEPRECATED => 'Runtime Deprecation Notice'
85 );
86 $message = 'PHP ' . $errorLevels[$errorLevel] . ': ' . $errorMessage . ' in ' . $errorFile . ' line ' . $errorLine;
87 if ($errorLevel & $this->exceptionalErrors) {
88 // handle error raised at early parse time
89 // autoloader not available & built-in classes not resolvable
90 if (!class_exists('stdClass', FALSE)) {
91 $message = 'PHP ' . $errorLevels[$errorLevel] . ': ' . $errorMessage . ' in ' . basename($errorFile) .
92 'line ' . $errorLine;
93 die($message);
94 }
95 // We need to manually require the exception classes in case
96 // the autoloader is not available at this point yet.
97 // @see http://forge.typo3.org/issues/23444
98 if (!class_exists('TYPO3\\CMS\\Core\\Error\\Exception', FALSE)) {
99 require_once PATH_site . 'typo3/sysext/core/Classes/Exception.php';
100 require_once PATH_site . 'typo3/sysext/core/Classes/Error/Exception.php';
101 }
102 throw new Exception($message, 1);
103 } else {
104 switch ($errorLevel) {
105 case E_USER_ERROR:
106 case E_RECOVERABLE_ERROR:
107 $severity = 2;
108 break;
109 case E_USER_WARNING:
110 case E_WARNING:
111 $severity = 1;
112 break;
113 default:
114 $severity = 0;
115 }
116 $logTitle = 'Core: Error handler (' . TYPO3_MODE . ')';
117 // Write error message to the configured syslogs,
118 // see: $TYPO3_CONF_VARS['SYS']['systemLog']
119 if ($errorLevel & $GLOBALS['TYPO3_CONF_VARS']['SYS']['syslogErrorReporting']) {
120 GeneralUtility::sysLog($message, $logTitle, $severity);
121 }
122 // Write error message to devlog extension(s),
123 // see: $TYPO3_CONF_VARS['SYS']['enable_errorDLOG']
124 if (TYPO3_ERROR_DLOG) {
125 GeneralUtility::devLog($message, $logTitle, $severity + 1);
126 }
127 // Write error message to TSlog (admin panel)
128 if (is_object($GLOBALS['TT'])) {
129 $GLOBALS['TT']->setTSlogMessage($logTitle . ': ' . $message, $severity + 1);
130 }
131 // Write error message to sys_log table (ext: belog, Tools->Log)
132 if ($errorLevel & $GLOBALS['TYPO3_CONF_VARS']['SYS']['belogErrorReporting']) {
133 // Silently catch in case an error occurs before a database connection exists,
134 // but DatabaseConnection fails to connect.
135 try {
136 $this->writeLog($logTitle . ': ' . $message, $severity);
137 } catch (\Exception $e) {
138 }
139 }
140 if ($severity === 2) {
141 // Let the internal handler continue. This will stop the script
142 return FALSE;
143 } else {
144 // Add error message to the flashmessageQueue
145 if (defined('TYPO3_ERRORHANDLER_MODE') && TYPO3_ERRORHANDLER_MODE == 'debug') {
146 /** @var $flashMessage \TYPO3\CMS\Core\Messaging\FlashMessage */
147 $flashMessage = GeneralUtility::makeInstance(
148 'TYPO3\\CMS\\Core\\Messaging\\FlashMessage',
149 $message,
150 'PHP ' . $errorLevels[$errorLevel],
151 $severity
152 );
153 /** @var $flashMessageService \TYPO3\CMS\Core\Messaging\FlashMessageService */
154 $flashMessageService = GeneralUtility::makeInstance(\TYPO3\CMS\Core\Messaging\FlashMessageService::class);
155 /** @var $defaultFlashMessageQueue \TYPO3\CMS\Core\Messaging\FlashMessageQueue */
156 $defaultFlashMessageQueue = $flashMessageService->getMessageQueueByIdentifier();
157 $defaultFlashMessageQueue->enqueue($flashMessage);
158 }
159 // Don't execute PHP internal error handler
160 return TRUE;
161 }
162 }
163 }
164
165 /**
166 * Writes an error in the sys_log table
167 *
168 * @param string $logMessage Default text that follows the message (in english!).
169 * @param int $severity The error level of the message (0 = OK, 1 = warning, 2 = error)
170 * @return void
171 */
172 protected function writeLog($logMessage, $severity) {
173 if (is_object($GLOBALS['TYPO3_DB']) && $GLOBALS['TYPO3_DB']->isConnected()) {
174 $userId = 0;
175 $workspace = 0;
176 if (is_object($GLOBALS['BE_USER'])) {
177 if (isset($GLOBALS['BE_USER']->user['uid'])) {
178 $userId = $GLOBALS['BE_USER']->user['uid'];
179 }
180 if (isset($GLOBALS['BE_USER']->workspace)) {
181 $workspace = $GLOBALS['BE_USER']->workspace;
182 }
183 }
184 $fields_values = array(
185 'userid' => $userId,
186 'type' => 5,
187 'action' => 0,
188 'error' => $severity,
189 'details_nr' => 0,
190 'details' => $logMessage,
191 'IP' => (string)GeneralUtility::getIndpEnv('REMOTE_ADDR'),
192 'tstamp' => $GLOBALS['EXEC_TIME'],
193 'workspace' => $workspace
194 );
195 $GLOBALS['TYPO3_DB']->exec_INSERTquery('sys_log', $fields_values);
196 }
197 }
198
199 }