[BUGFIX] BELog module error: "vsprintf(): Too few arguments"
[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-2011 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 // In case an error occurs before a database connection exists, try
77 // to connect to the DB to be able to write the devlog/sys_log entry
78 if (isset($GLOBALS['TYPO3_DB']) && is_object($GLOBALS['TYPO3_DB']) && empty($GLOBALS['TYPO3_DB']->link)) {
79 $GLOBALS['TYPO3_DB']->connectDB();
80 }
81 // Write error message to devlog
82 // see: $TYPO3_CONF_VARS['SYS']['enable_exceptionDLOG']
83 if (TYPO3_EXCEPTION_DLOG) {
84 \TYPO3\CMS\Core\Utility\GeneralUtility::devLog($logMessage, $logTitle, 3, array(
85 'TYPO3_MODE' => TYPO3_MODE,
86 'backtrace' => $backtrace
87 ));
88 }
89 // Write error message to sys_log table
90 $this->writeLog($logTitle . ': ' . $logMessage);
91 } catch (\Exception $exception) {
92
93 }
94 }
95
96 /**
97 * Writes an exception in the sys_log table
98 *
99 * @param string $logMessage Default text that follows the message.
100 * @return void
101 */
102 protected function writeLog($logMessage) {
103 if (is_object($GLOBALS['TYPO3_DB']) && !empty($GLOBALS['TYPO3_DB']->link)) {
104 $userId = 0;
105 $workspace = 0;
106 if (is_object($GLOBALS['BE_USER'])) {
107 if (isset($GLOBALS['BE_USER']->user['uid'])) {
108 $userId = $GLOBALS['BE_USER']->user['uid'];
109 }
110 if (isset($GLOBALS['BE_USER']->workspace)) {
111 $workspace = $GLOBALS['BE_USER']->workspace;
112 }
113 }
114 $fields_values = array(
115 'userid' => $userId,
116 'type' => 5,
117 'action' => 0,
118 'error' => 2,
119 'details_nr' => 0,
120 'details' => str_replace('%', '%%', $logMessage),
121 'IP' => \TYPO3\CMS\Core\Utility\GeneralUtility::getIndpEnv('REMOTE_ADDR'),
122 'tstamp' => $GLOBALS['EXEC_TIME'],
123 'workspace' => $workspace
124 );
125 $GLOBALS['TYPO3_DB']->exec_INSERTquery('sys_log', $fields_values);
126 }
127 }
128
129 /**
130 * Sends the HTTP Status 500 code, if $exception is *not* a t3lib_error_http_StatusException
131 * and headers are not sent, yet.
132 *
133 * @param Exception $exception
134 * @return void
135 */
136 protected function sendStatusHeaders(\Exception $exception) {
137 if (method_exists($exception, 'getStatusHeaders')) {
138 $headers = $exception->getStatusHeaders();
139 } else {
140 $headers = array(\TYPO3\CMS\Core\Utility\HttpUtility::HTTP_STATUS_500);
141 }
142 if (!headers_sent()) {
143 foreach ($headers as $header) {
144 header($header);
145 }
146 }
147 }
148
149 }
150
151
152 ?>