[TASK] Remove obsolete require statements from ErrorHandler
[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 throw new Exception($message, 1);
96 } else {
97 switch ($errorLevel) {
98 case E_USER_ERROR:
99 case E_RECOVERABLE_ERROR:
100 $severity = 2;
101 break;
102 case E_USER_WARNING:
103 case E_WARNING:
104 $severity = 1;
105 break;
106 default:
107 $severity = 0;
108 }
109 $logTitle = 'Core: Error handler (' . TYPO3_MODE . ')';
110 // Write error message to the configured syslogs,
111 // see: $TYPO3_CONF_VARS['SYS']['systemLog']
112 if ($errorLevel & $GLOBALS['TYPO3_CONF_VARS']['SYS']['syslogErrorReporting']) {
113 GeneralUtility::sysLog($message, $logTitle, $severity);
114 }
115 // Write error message to devlog extension(s),
116 // see: $TYPO3_CONF_VARS['SYS']['enable_errorDLOG']
117 if (TYPO3_ERROR_DLOG) {
118 GeneralUtility::devLog($message, $logTitle, $severity + 1);
119 }
120 // Write error message to TSlog (admin panel)
121 if (is_object($GLOBALS['TT'])) {
122 $GLOBALS['TT']->setTSlogMessage($logTitle . ': ' . $message, $severity + 1);
123 }
124 // Write error message to sys_log table (ext: belog, Tools->Log)
125 if ($errorLevel & $GLOBALS['TYPO3_CONF_VARS']['SYS']['belogErrorReporting']) {
126 // Silently catch in case an error occurs before a database connection exists,
127 // but DatabaseConnection fails to connect.
128 try {
129 $this->writeLog($logTitle . ': ' . $message, $severity);
130 } catch (\Exception $e) {
131 }
132 }
133 if ($severity === 2) {
134 // Let the internal handler continue. This will stop the script
135 return FALSE;
136 } else {
137 // Add error message to the flashmessageQueue
138 if (defined('TYPO3_ERRORHANDLER_MODE') && TYPO3_ERRORHANDLER_MODE == 'debug') {
139 /** @var $flashMessage \TYPO3\CMS\Core\Messaging\FlashMessage */
140 $flashMessage = GeneralUtility::makeInstance(
141 \TYPO3\CMS\Core\Messaging\FlashMessage::class,
142 $message,
143 'PHP ' . $errorLevels[$errorLevel],
144 $severity
145 );
146 /** @var $flashMessageService \TYPO3\CMS\Core\Messaging\FlashMessageService */
147 $flashMessageService = GeneralUtility::makeInstance(\TYPO3\CMS\Core\Messaging\FlashMessageService::class);
148 /** @var $defaultFlashMessageQueue \TYPO3\CMS\Core\Messaging\FlashMessageQueue */
149 $defaultFlashMessageQueue = $flashMessageService->getMessageQueueByIdentifier();
150 $defaultFlashMessageQueue->enqueue($flashMessage);
151 }
152 // Don't execute PHP internal error handler
153 return TRUE;
154 }
155 }
156 }
157
158 /**
159 * Writes an error in the sys_log table
160 *
161 * @param string $logMessage Default text that follows the message (in english!).
162 * @param int $severity The error level of the message (0 = OK, 1 = warning, 2 = error)
163 * @return void
164 */
165 protected function writeLog($logMessage, $severity) {
166 if (is_object($GLOBALS['TYPO3_DB']) && $GLOBALS['TYPO3_DB']->isConnected()) {
167 $userId = 0;
168 $workspace = 0;
169 if (is_object($GLOBALS['BE_USER'])) {
170 if (isset($GLOBALS['BE_USER']->user['uid'])) {
171 $userId = $GLOBALS['BE_USER']->user['uid'];
172 }
173 if (isset($GLOBALS['BE_USER']->workspace)) {
174 $workspace = $GLOBALS['BE_USER']->workspace;
175 }
176 }
177 $fields_values = array(
178 'userid' => $userId,
179 'type' => 5,
180 'action' => 0,
181 'error' => $severity,
182 'details_nr' => 0,
183 'details' => $logMessage,
184 'IP' => (string)GeneralUtility::getIndpEnv('REMOTE_ADDR'),
185 'tstamp' => $GLOBALS['EXEC_TIME'],
186 'workspace' => $workspace
187 );
188 $GLOBALS['TYPO3_DB']->exec_INSERTquery('sys_log', $fields_values);
189 }
190 }
191
192 }