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