[TASK] Replace Space Indent into Tab indent (CGL Cleanup)
[Packages/TYPO3.CMS.git] / t3lib / error / class.t3lib_error_abstractexceptionhandler.php
1 <?php
2 /***************************************************************
3 * Copyright notice
4 *
5 * (c) 2009-2011 Ingo Renner <ingo@typo3.org>
6 * All rights reserved
7 *
8 * This script is part of the TYPO3 project. The TYPO3 project is
9 * free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * The GNU General Public License can be found at
15 * http://www.gnu.org/copyleft/gpl.html.
16 *
17 * This script is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
21 *
22 * This copyright notice MUST APPEAR in all copies of the script!
23 ***************************************************************/
24
25
26 /**
27 * An abstract exception handler
28 *
29 * This file is a backport from FLOW3
30 *
31 * @package TYPO3
32 * @subpackage t3lib_error
33 */
34 abstract class t3lib_error_AbstractExceptionHandler implements t3lib_error_ExceptionHandlerInterface, t3lib_Singleton {
35 const CONTEXT_WEB = 'WEB';
36 const CONTEXT_CLI = 'CLI';
37
38 /**
39 * Displays the given exception
40 *
41 * @param Exception $exception The exception object
42 * @return void
43 */
44 public function handleException(Exception $exception) {
45 switch (PHP_SAPI) {
46 case 'cli' :
47 $this->echoExceptionCLI($exception);
48 break;
49 default :
50 $this->echoExceptionWeb($exception);
51 }
52 }
53
54
55 /**
56 * Writes exception to different logs
57 *
58 * @param Exception $exception The exception
59 * @param string the context where the exception was thrown, WEB or CLI
60 * @return void
61 * @see t3lib_div::sysLog(), t3lib_div::devLog()
62 */
63 protected function writeLogEntries(Exception $exception, $context) {
64 $filePathAndName = $exception->getFile();
65 $exceptionCodeNumber = ($exception->getCode() > 0) ? '#' . $exception->getCode() . ': ' : '';
66 $logTitle = 'Core: Exception handler (' . $context . ')';
67 $logMessage = 'Uncaught TYPO3 Exception: ' . $exceptionCodeNumber . $exception->getMessage() . ' | ' .
68 get_class($exception) . ' thrown in file ' . $filePathAndName . ' in line ' . $exception->getLine();
69 if ($context === 'WEB') {
70 $logMessage .= '. Requested URL: ' . t3lib_div::getIndpEnv('TYPO3_REQUEST_URL');
71 }
72
73 $backtrace = $exception->getTrace();
74
75 // write error message to the configured syslogs
76 t3lib_div::sysLog($logMessage, $logTitle, 4);
77
78 // When database credentials are wrong, the exception is probably
79 // caused by this. Therefor we cannot do any database operation,
80 // otherwise this will lead into recurring exceptions.
81 try {
82 // In case an error occurs before a database connection exists, try
83 // to connect to the DB to be able to write the devlog/sys_log entry
84 if (isset($GLOBALS['TYPO3_DB']) && is_object($GLOBALS['TYPO3_DB']) && empty($GLOBALS['TYPO3_DB']->link)) {
85 $GLOBALS['TYPO3_DB']->connectDB();
86 }
87
88 // write error message to devlog
89 // see: $TYPO3_CONF_VARS['SYS']['enable_exceptionDLOG']
90 if (TYPO3_EXCEPTION_DLOG) {
91 t3lib_div::devLog(
92 $logMessage,
93 $logTitle,
94 3,
95 array(
96 'TYPO3_MODE' => TYPO3_MODE,
97 'backtrace' => $backtrace
98 )
99 );
100 }
101
102 // write error message to sys_log table
103 $this->writeLog($logTitle . ': ' . $logMessage);
104 } catch (Exception $exception) {
105 // Nothing happens here. It seems the database credentials are wrong
106 }
107 }
108
109 /**
110 * Writes an exception in the sys_log table
111 *
112 * @param string Default text that follows the message.
113 * @return void
114 */
115 protected function writeLog($logMessage) {
116 if (is_object($GLOBALS['TYPO3_DB']) && !empty($GLOBALS['TYPO3_DB']->link)) {
117 $userId = 0;
118 $workspace = 0;
119 if (is_object($GLOBALS['BE_USER'])) {
120 if (isset($GLOBALS['BE_USER']->user['uid'])) {
121 $userId = $GLOBALS['BE_USER']->user['uid'];
122 }
123 if (isset($GLOBALS['BE_USER']->workspace)) {
124 $workspace = $GLOBALS['BE_USER']->workspace;
125 }
126 }
127
128 $fields_values = Array(
129 'userid' => $userId,
130 'type' => 5,
131 'action' => 0,
132 'error' => 2,
133 'details_nr' => 0,
134 'details' => $logMessage,
135 'IP' => t3lib_div::getIndpEnv('REMOTE_ADDR'),
136 'tstamp' => $GLOBALS['EXEC_TIME'],
137 'workspace' => $workspace
138 );
139
140 $GLOBALS['TYPO3_DB']->exec_INSERTquery('sys_log', $fields_values);
141 }
142 }
143
144 /**
145 * Sends the HTTP Status 500 code, if $exception is *not* a t3lib_error_http_StatusException
146 * and headers are not sent, yet.
147 *
148 * @param Exception $exception
149 * @return void
150 */
151 protected function sendStatusHeaders(Exception $exception) {
152 if (method_exists($exception, 'getStatusHeaders')) {
153 $headers = $exception->getStatusHeaders();
154 } else {
155 $headers = array(t3lib_utility_Http::HTTP_STATUS_500);
156 }
157 if (!headers_sent()) {
158 foreach($headers as $header) {
159 header($header);
160 }
161 }
162 }
163 }
164
165 ?>