TYPO3 CMS  TYPO3_6-2
ErrorHandler.php
Go to the documentation of this file.
1 <?php
2 namespace TYPO3\CMS\Core\Error;
3 
17 
26 
32  protected $exceptionalErrors = array();
33 
39  public function __construct($errorHandlerErrors) {
40  $excludedErrors = E_COMPILE_WARNING | E_COMPILE_ERROR | E_CORE_WARNING | E_CORE_ERROR | E_PARSE | E_ERROR;
41  // reduces error types to those a custom error handler can process
42  $errorHandlerErrors = $errorHandlerErrors & ~$excludedErrors;
43  set_error_handler(array($this, 'handleError'), $errorHandlerErrors);
44  }
45 
53  $this->exceptionalErrors = (int)$exceptionalErrors;
54  }
55 
70  public function handleError($errorLevel, $errorMessage, $errorFile, $errorLine) {
71  // Don't do anything if error_reporting is disabled by an @ sign
72  if (error_reporting() === 0) {
73  return TRUE;
74  }
75  $errorLevels = array(
76  E_WARNING => 'Warning',
77  E_NOTICE => 'Notice',
78  E_USER_ERROR => 'User Error',
79  E_USER_WARNING => 'User Warning',
80  E_USER_NOTICE => 'User Notice',
81  E_STRICT => 'Runtime Notice',
82  E_RECOVERABLE_ERROR => 'Catchable Fatal Error',
83  E_DEPRECATED => 'Runtime Deprecation Notice'
84  );
85  $message = 'PHP ' . $errorLevels[$errorLevel] . ': ' . $errorMessage . ' in ' . $errorFile . ' line ' . $errorLine;
86  if ($errorLevel & $this->exceptionalErrors) {
87  // handle error raised at early parse time
88  // autoloader not available & built-in classes not resolvable
89  if (!class_exists('stdClass', FALSE)) {
90  $message = 'PHP ' . $errorLevels[$errorLevel] . ': ' . $errorMessage . ' in ' . basename($errorFile) .
91  'line ' . $errorLine;
92  die($message);
93  }
94  // We need to manually require the exception classes in case
95  // the autoloader is not available at this point yet.
96  // @see http://forge.typo3.org/issues/23444
97  if (!class_exists('TYPO3\\CMS\\Core\\Error\\Exception', FALSE)) {
98  require_once PATH_site . 'typo3/sysext/core/Classes/Exception.php';
99  require_once PATH_site . 'typo3/sysext/core/Classes/Error/Exception.php';
100  }
101  throw new Exception($message, 1);
102  } else {
103  switch ($errorLevel) {
104  case E_USER_ERROR:
105 
106  case E_RECOVERABLE_ERROR:
107  $severity = 2;
108  break;
109  case E_USER_WARNING:
110 
111  case E_WARNING:
112  $severity = 1;
113  break;
114  default:
115  $severity = 0;
116  }
117  $logTitle = 'Core: Error handler (' . TYPO3_MODE . ')';
118  // Write error message to the configured syslogs,
119  // see: $TYPO3_CONF_VARS['SYS']['systemLog']
120  if ($errorLevel & $GLOBALS['TYPO3_CONF_VARS']['SYS']['syslogErrorReporting']) {
121  GeneralUtility::sysLog($message, $logTitle, $severity);
122  }
123  // Write error message to devlog extension(s),
124  // see: $TYPO3_CONF_VARS['SYS']['enable_errorDLOG']
125  if (TYPO3_ERROR_DLOG) {
126  GeneralUtility::devLog($message, $logTitle, $severity + 1);
127  }
128  // Write error message to TSlog (admin panel)
129  if (is_object($GLOBALS['TT'])) {
130  $GLOBALS['TT']->setTSlogMessage($logTitle . ': ' . $message, $severity + 1);
131  }
132  // Write error message to sys_log table (ext: belog, Tools->Log)
133  if ($errorLevel & $GLOBALS['TYPO3_CONF_VARS']['SYS']['belogErrorReporting']) {
134  // Silently catch in case an error occurs before a database connection exists,
135  // but DatabaseConnection fails to connect.
136  try {
137  $this->writeLog($logTitle . ': ' . $message, $severity);
138  } catch (\Exception $e) {
139  }
140  }
141  if ($severity === 2) {
142  // Let the internal handler continue. This will stop the script
143  return FALSE;
144  } else {
145  // Add error message to the flashmessageQueue
146  if (defined('TYPO3_ERRORHANDLER_MODE') && TYPO3_ERRORHANDLER_MODE == 'debug') {
148  $flashMessage = GeneralUtility::makeInstance(
149  'TYPO3\\CMS\\Core\\Messaging\\FlashMessage',
150  $message,
151  'PHP ' . $errorLevels[$errorLevel],
152  $severity
153  );
155  $flashMessageService = GeneralUtility::makeInstance('TYPO3\\CMS\\Core\\Messaging\\FlashMessageService');
157  $defaultFlashMessageQueue = $flashMessageService->getMessageQueueByIdentifier();
158  $defaultFlashMessageQueue->enqueue($flashMessage);
159  }
160  // Don't execute PHP internal error handler
161  return TRUE;
162  }
163  }
164  }
165 
173  protected function writeLog($logMessage, $severity) {
174  if (is_object($GLOBALS['TYPO3_DB']) && $GLOBALS['TYPO3_DB']->isConnected()) {
175  $userId = 0;
176  $workspace = 0;
177  if (is_object($GLOBALS['BE_USER'])) {
178  if (isset($GLOBALS['BE_USER']->user['uid'])) {
179  $userId = $GLOBALS['BE_USER']->user['uid'];
180  }
181  if (isset($GLOBALS['BE_USER']->workspace)) {
182  $workspace = $GLOBALS['BE_USER']->workspace;
183  }
184  }
185  $fields_values = array(
186  'userid' => $userId,
187  'type' => 5,
188  'action' => 0,
189  'error' => $severity,
190  'details_nr' => 0,
191  'details' => $logMessage,
192  'IP' => (string)GeneralUtility::getIndpEnv('REMOTE_ADDR'),
193  'tstamp' => $GLOBALS['EXEC_TIME'],
194  'workspace' => $workspace
195  );
196  $GLOBALS['TYPO3_DB']->exec_INSERTquery('sys_log', $fields_values);
197  }
198  }
199 
200 }
handleError($errorLevel, $errorMessage, $errorFile, $errorLine)
writeLog($logMessage, $severity)
if(!defined('ADODB_ERROR_HANDLER_TYPE')) define('ADODB_ERROR_HANDLER_TYPE' E_USER_ERROR
static devLog($msg, $extKey, $severity=0, $dataVar=FALSE)
const TYPO3_MODE
Definition: init.php:40
setExceptionalErrors($exceptionalErrors)
__construct($errorHandlerErrors)
die
Definition: index.php:6
if(!defined('TYPO3_MODE')) $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_userauth.php']['logoff_pre_processing'][]