Fixed bug #14312: Notice undefined index systemLog and undefined constant TYPO3_ERROR...
[Packages/TYPO3.CMS.git] / t3lib / error / class.t3lib_error_errorhandler.php
1 <?php
2 /***************************************************************
3 * Copyright notice
4 *
5 * (c) 2009 Ingo Renner <ingo@typo3.org>
6 * All rights reserved
7 *
8 * This script is part of the TYPO3 project. The TYPO3 project is
9 * free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * The GNU General Public License can be found at
15 * http://www.gnu.org/copyleft/gpl.html.
16 *
17 * This script is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
21 *
22 * This copyright notice MUST APPEAR in all copies of the script!
23 ***************************************************************/
24
25
26
27 /**
28 * Global error handler for TYPO3
29 *
30 * This file is a backport from FLOW3
31 *
32 * @package TYPO3
33 * @subpackage t3lib_error
34 * @author Rupert Germann <rupi@gmx.li>
35 * @version $Id$
36 */
37 class t3lib_error_ErrorHandler implements t3lib_error_ErrorHandlerInterface {
38
39 /**
40 * Error levels which should result in an exception thrown.
41 *
42 * @var integer
43 */
44 protected $exceptionalErrors = array();
45
46 /**
47 * Registers this class as default error handler
48 *
49 * @param integer The integer representing the E_* error level which should be
50 * handled by the registered error handler.
51 * @return void
52 */
53 public function __construct($errorHandlerErrors) {
54 set_error_handler(array($this, 'handleError'), $errorHandlerErrors);
55 }
56
57
58
59 /**
60 * Defines which error levels should result in an exception thrown.
61 *
62 * @param integer The integer representing the E_* error level to handle as exceptions
63 * @return void
64 */
65 public function setExceptionalErrors($exceptionalErrors) {
66 $this->exceptionalErrors = (int)$exceptionalErrors;
67
68 }
69
70 /**
71 * Handles an error.
72 * If the error is registered as exceptionalError it will by converted into an exception, to be handled
73 * by the configured exceptionhandler. Additionall the error message is written to the configured logs.
74 * If TYPO3_MODE is 'BE' the error message is also added to the flashMessageQueue, in FE the error message
75 * is displayed in the admin panel (as TsLog message)
76 *
77 * @param integer The error level - one of the E_* constants
78 * @param string The error message
79 * @param string Name of the file the error occurred in
80 * @param integer Line number where the error occurred
81 * @return void
82 * @throws t3lib_error_Exception with the data passed to this method if the error is registered as exceptionalError
83 */
84 public function handleError($errorLevel, $errorMessage, $errorFile, $errorLine) {
85 // don't do anything if error_reporting is disabled by an @ sign
86 if (error_reporting()==0) { 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 );
97
98 $message = 'PHP ' .$errorLevels[$errorLevel] . ': ' . $errorMessage . ' in ' . $errorFile . ' line ' . $errorLine;
99
100 if ($errorLevel & $this->exceptionalErrors) {
101 throw new t3lib_error_Exception($message, 1);
102 } else {
103
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 break;
116 }
117
118 $logTitle = 'Core: Error handler (' . TYPO3_MODE . ')';
119
120 // Write error message to the configured syslogs,
121 // see: $TYPO3_CONF_VARS['SYS']['systemLog']
122 if ($errorLevel & $GLOBALS['TYPO3_CONF_VARS']['SYS']['syslogErrorReporting']) {
123 t3lib_div::sysLog($message, $logTitle, $severity);
124 }
125
126 // In case an error occurs before a database connection exists, try
127 // to connect to the DB to be able to write an entry to devlog/sys_log
128 if (is_object($GLOBALS['TYPO3_DB']) && empty($GLOBALS['TYPO3_DB']->link)) {
129 try {
130 $GLOBALS['TYPO3_DB']->connectDB();
131 }
132 catch (Exception $e) {
133 // There's nothing more we can do at this point if the
134 // database failed. It is up to the various log writers
135 // to check for themselves whether the have a DB connection
136 // available or not.
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 t3lib_div::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
154 // Add error message to the flashmessageQueue
155 if (defined('TYPO3_ERRORHANDLER_MODE') && TYPO3_ERRORHANDLER_MODE == 'debug') {
156 $flashMessage = t3lib_div::makeInstance(
157 't3lib_FlashMessage',
158 $message,
159 'PHP ' . $errorLevels[$errorLevel],
160 $severity
161 );
162 t3lib_FlashMessageQueue::addMessage($flashMessage);
163 }
164 }
165
166 // Don't execute PHP internal error handler
167 return TRUE;
168 }
169
170 /**
171 * Writes an error in the sys_log table
172 *
173 * @param string Default text that follows the message (in english!).
174 * @param integer The eror 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']) && !empty($GLOBALS['TYPO3_DB']->link)) {
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
190 $fields_values = Array (
191 'userid' => $userId,
192 'type' => 5,
193 'action' => 0,
194 'error' => $severity,
195 'details_nr' => 0,
196 'details' => $logMessage,
197 'IP' => t3lib_div::getIndpEnv('REMOTE_ADDR'),
198 'tstamp' => $GLOBALS['EXEC_TIME'],
199 'workspace' => $workspace
200 );
201 $GLOBALS['TYPO3_DB']->exec_INSERTquery('sys_log', $fields_values);
202 }
203 }
204
205 }
206
207
208 if (defined('TYPO3_MODE') && $TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['t3lib/error/class.t3lib_error_errorhandler.php']) {
209 include_once($TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['t3lib/error/class.t3lib_error_errorhandler.php']);
210 }
211
212 ?>