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