TYPO3 CMS  TYPO3_6-2
AbstractExceptionHandler.php
Go to the documentation of this file.
1 <?php
3 
19 
28 
29  const CONTEXT_WEB = 'WEB';
30  const CONTEXT_CLI = 'CLI';
31 
38  public function handleException(\Exception $exception) {
39  switch (PHP_SAPI) {
40  case 'cli':
41  $this->echoExceptionCLI($exception);
42  break;
43  default:
44  $this->echoExceptionWeb($exception);
45  }
46  }
47 
56  protected function writeLogEntries(\Exception $exception, $context) {
57  // Do not write any logs for this message to avoid filling up tables or files with illegal requests
58  if ($exception->getCode() === 1396795884) {
59  return;
60  }
61  $filePathAndName = $exception->getFile();
62  $exceptionCodeNumber = $exception->getCode() > 0 ? '#' . $exception->getCode() . ': ' : '';
63  $logTitle = 'Core: Exception handler (' . $context . ')';
64  $logMessage = 'Uncaught TYPO3 Exception: ' . $exceptionCodeNumber . $exception->getMessage() . ' | '
65  . get_class($exception) . ' thrown in file ' . $filePathAndName . ' in line ' . $exception->getLine();
66  if ($context === 'WEB') {
67  $logMessage .= '. Requested URL: ' . GeneralUtility::getIndpEnv('TYPO3_REQUEST_URL');
68  }
69  $backtrace = $exception->getTrace();
70  // Write error message to the configured syslogs
71  GeneralUtility::sysLog($logMessage, $logTitle, GeneralUtility::SYSLOG_SEVERITY_FATAL);
72  // When database credentials are wrong, the exception is probably
73  // caused by this. Therefor we cannot do any database operation,
74  // otherwise this will lead into recurring exceptions.
75  try {
76  // Write error message to devlog
77  // see: $TYPO3_CONF_VARS['SYS']['enable_exceptionDLOG']
78  if (TYPO3_EXCEPTION_DLOG) {
79  GeneralUtility::devLog($logMessage, $logTitle, 3, array(
80  'TYPO3_MODE' => TYPO3_MODE,
81  'backtrace' => $backtrace
82  ));
83  }
84  // Write error message to sys_log table
85  $this->writeLog($logTitle . ': ' . $logMessage);
86  } catch (\Exception $exception) {
87 
88  }
89  }
90 
97  protected function writeLog($logMessage) {
98  $databaseConnection = $this->getDatabaseConnection();
99  if (!is_object($databaseConnection) || !$databaseConnection->isConnected()) {
100  return;
101  }
102  $userId = 0;
103  $workspace = 0;
104  if (is_object($GLOBALS['BE_USER'])) {
105  if (isset($GLOBALS['BE_USER']->user['uid'])) {
106  $userId = $GLOBALS['BE_USER']->user['uid'];
107  }
108  if (isset($GLOBALS['BE_USER']->workspace)) {
109  $workspace = $GLOBALS['BE_USER']->workspace;
110  }
111  }
112  $fields_values = array(
113  'userid' => $userId,
114  'type' => 5,
115  'action' => 0,
116  'error' => 2,
117  'details_nr' => 0,
118  'details' => str_replace('%', '%%', $logMessage),
119  'IP' => (string)GeneralUtility::getIndpEnv('REMOTE_ADDR'),
120  'tstamp' => $GLOBALS['EXEC_TIME'],
121  'workspace' => $workspace
122  );
123  $databaseConnection->exec_INSERTquery('sys_log', $fields_values);
124  }
125 
133  protected function sendStatusHeaders(\Exception $exception) {
134  if (method_exists($exception, 'getStatusHeaders')) {
135  $headers = $exception->getStatusHeaders();
136  } else {
137  $headers = array(\TYPO3\CMS\Core\Utility\HttpUtility::HTTP_STATUS_500);
138  }
139  if (!headers_sent()) {
140  foreach ($headers as $header) {
141  header($header);
142  }
143  }
144  }
145 
150  protected function getDatabaseConnection() {
151  return $GLOBALS['TYPO3_DB'];
152  }
153 
154 }
static devLog($msg, $extKey, $severity=0, $dataVar=FALSE)
const TYPO3_MODE
Definition: init.php:40
if(!defined('TYPO3_MODE')) $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_userauth.php']['logoff_pre_processing'][]