TYPO3  7.6
AbstractExceptionHandler.php
Go to the documentation of this file.
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 
19 
26 {
27  const CONTEXT_WEB = 'WEB';
28  const CONTEXT_CLI = 'CLI';
29 
36  public function handleException(\Exception $exception)
37  {
38  switch (PHP_SAPI) {
39  case 'cli':
40  $this->echoExceptionCLI($exception);
41  break;
42  default:
43  $this->echoExceptionWeb($exception);
44  }
45  }
46 
55  protected function writeLogEntries(\Exception $exception, $context)
56  {
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 
96  protected function writeLog($logMessage)
97  {
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  {
135  if (method_exists($exception, 'getStatusHeaders')) {
136  $headers = $exception->getStatusHeaders();
137  } else {
138  $headers = array(\TYPO3\CMS\Core\Utility\HttpUtility::HTTP_STATUS_500);
139  }
140  if (!headers_sent()) {
141  foreach ($headers as $header) {
142  header($header);
143  }
144  }
145  }
146 
151  protected function getDatabaseConnection()
152  {
153  return $GLOBALS['TYPO3_DB'];
154  }
155 }