01505f4cb1e1c921b979ac1ea7f39820639f2705
[Packages/TYPO3.CMS.git] / typo3 / sysext / core / Classes / Error / AbstractExceptionHandler.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 * An abstract exception handler
28 *
29 * This file is a backport from FLOW3
30 *
31 * @author Ingo Renner <ingo@typo3.org>
32 */
33 abstract class AbstractExceptionHandler implements \TYPO3\CMS\Core\Error\ExceptionHandlerInterface, \TYPO3\CMS\Core\SingletonInterface {
34
35 const CONTEXT_WEB = 'WEB';
36 const CONTEXT_CLI = 'CLI';
37 /**
38 * Displays the given exception
39 *
40 * @param Exception $exception The exception object
41 * @return void
42 */
43 public function handleException(\Exception $exception) {
44 switch (PHP_SAPI) {
45 case 'cli':
46 $this->echoExceptionCLI($exception);
47 break;
48 default:
49 $this->echoExceptionWeb($exception);
50 }
51 }
52
53 /**
54 * Writes exception to different logs
55 *
56 * @param Exception $exception The exception
57 * @param string $context The context where the exception was thrown, WEB or CLI
58 * @return void
59 * @see \TYPO3\CMS\Core\Utility\GeneralUtility::sysLog(), \TYPO3\CMS\Core\Utility\GeneralUtility::devLog()
60 */
61 protected function writeLogEntries(\Exception $exception, $context) {
62 $filePathAndName = $exception->getFile();
63 $exceptionCodeNumber = $exception->getCode() > 0 ? '#' . $exception->getCode() . ': ' : '';
64 $logTitle = 'Core: Exception handler (' . $context . ')';
65 $logMessage = 'Uncaught TYPO3 Exception: ' . $exceptionCodeNumber . $exception->getMessage() . ' | ' . get_class($exception) . ' thrown in file ' . $filePathAndName . ' in line ' . $exception->getLine();
66 if ($context === 'WEB') {
67 $logMessage .= '. Requested URL: ' . \TYPO3\CMS\Core\Utility\GeneralUtility::getIndpEnv('TYPO3_REQUEST_URL');
68 }
69 $backtrace = $exception->getTrace();
70 // Write error message to the configured syslogs
71 \TYPO3\CMS\Core\Utility\GeneralUtility::sysLog($logMessage, $logTitle, \TYPO3\CMS\Core\Utility\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 \TYPO3\CMS\Core\Utility\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
91 /**
92 * Writes an exception in the sys_log table
93 *
94 * @param string $logMessage Default text that follows the message.
95 * @return void
96 */
97 protected function writeLog($logMessage) {
98 if (is_object($GLOBALS['TYPO3_DB']) && !empty($GLOBALS['TYPO3_DB']->link)) {
99 $userId = 0;
100 $workspace = 0;
101 if (is_object($GLOBALS['BE_USER'])) {
102 if (isset($GLOBALS['BE_USER']->user['uid'])) {
103 $userId = $GLOBALS['BE_USER']->user['uid'];
104 }
105 if (isset($GLOBALS['BE_USER']->workspace)) {
106 $workspace = $GLOBALS['BE_USER']->workspace;
107 }
108 }
109 $fields_values = array(
110 'userid' => $userId,
111 'type' => 5,
112 'action' => 0,
113 'error' => 2,
114 'details_nr' => 0,
115 'details' => str_replace('%', '%%', $logMessage),
116 'IP' => \TYPO3\CMS\Core\Utility\GeneralUtility::getIndpEnv('REMOTE_ADDR'),
117 'tstamp' => $GLOBALS['EXEC_TIME'],
118 'workspace' => $workspace
119 );
120 $GLOBALS['TYPO3_DB']->exec_INSERTquery('sys_log', $fields_values);
121 }
122 }
123
124 /**
125 * Sends the HTTP Status 500 code, if $exception is *not* a
126 * TYPO3\\CMS\\Core\\Error\\Http\\StatusException and headers are not sent, yet.
127 *
128 * @param Exception $exception
129 * @return void
130 */
131 protected function sendStatusHeaders(\Exception $exception) {
132 if (method_exists($exception, 'getStatusHeaders')) {
133 $headers = $exception->getStatusHeaders();
134 } else {
135 $headers = array(\TYPO3\CMS\Core\Utility\HttpUtility::HTTP_STATUS_500);
136 }
137 if (!headers_sent()) {
138 foreach ($headers as $header) {
139 header($header);
140 }
141 }
142 }
143
144 }