[FEATURE] Create an Exception hierarchy for HTTP Status Exceptions
[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 $backtrace = $exception->getTrace();
70
71 // write error message to the configured syslogs
72 t3lib_div::sysLog($logMessage, $logTitle, 4);
73
74 // When database credentials are wrong, the exception is probably
75 // caused by this. Therefor we cannot do any database operation,
76 // otherwise this will lead into recurring exceptions.
77 try {
78 // In case an error occurs before a database connection exists, try
79 // to connect to the DB to be able to write the devlog/sys_log entry
80 if (isset($GLOBALS['TYPO3_DB']) && is_object($GLOBALS['TYPO3_DB']) && empty($GLOBALS['TYPO3_DB']->link)) {
81 $GLOBALS['TYPO3_DB']->connectDB();
82 }
83
84 // write error message to devlog
85 // see: $TYPO3_CONF_VARS['SYS']['enable_exceptionDLOG']
86 if (TYPO3_EXCEPTION_DLOG) {
87 t3lib_div::devLog(
88 $logMessage,
89 $logTitle,
90 3,
91 array(
92 'TYPO3_MODE' => TYPO3_MODE,
93 'backtrace' => $backtrace
94 )
95 );
96 }
97
98 // write error message to sys_log table
99 $this->writeLog($logTitle . ': ' . $logMessage);
100 } catch (Exception $exception) {
101 // Nothing happens here. It seems the database credentials are wrong
102 }
103 }
104
105 /**
106 * Writes an exception in the sys_log table
107 *
108 * @param string Default text that follows the message.
109 * @return void
110 */
111 protected function writeLog($logMessage) {
112 if (is_object($GLOBALS['TYPO3_DB']) && !empty($GLOBALS['TYPO3_DB']->link)) {
113 $userId = 0;
114 $workspace = 0;
115 if (is_object($GLOBALS['BE_USER'])) {
116 if (isset($GLOBALS['BE_USER']->user['uid'])) {
117 $userId = $GLOBALS['BE_USER']->user['uid'];
118 }
119 if (isset($GLOBALS['BE_USER']->workspace)) {
120 $workspace = $GLOBALS['BE_USER']->workspace;
121 }
122 }
123
124 $fields_values = Array(
125 'userid' => $userId,
126 'type' => 5,
127 'action' => 0,
128 'error' => 2,
129 'details_nr' => 0,
130 'details' => $logMessage,
131 'IP' => t3lib_div::getIndpEnv('REMOTE_ADDR'),
132 'tstamp' => $GLOBALS['EXEC_TIME'],
133 'workspace' => $workspace
134 );
135
136 $GLOBALS['TYPO3_DB']->exec_INSERTquery('sys_log', $fields_values);
137 }
138 }
139
140 /**
141 * Sends the HTTP Status 500 code, if $exception is *not* a t3lib_error_http_StatusException
142 * and headers are not sent, yet.
143 *
144 * @param Exception $exception
145 * @return void
146 */
147 protected function sendStatusHeaders(Exception $exception) {
148 if (method_exists($exception, 'getStatusHeaders')) {
149 $headers = $exception->getStatusHeaders();
150 } else {
151 $headers = array(t3lib_utility_Http::HTTP_STATUS_500);
152 }
153 if (!headers_sent()) {
154 foreach($headers as $header) {
155 header($header);
156 }
157 }
158 }
159 }
160
161 if (defined('TYPO3_MODE') && isset($GLOBALS['TYPO3_CONF_VARS'][TYPO3_MODE]['XCLASS']['t3lib/error/class.t3lib_error_abstractexceptionhandler.php'])) {
162 include_once($GLOBALS['TYPO3_CONF_VARS'][TYPO3_MODE]['XCLASS']['t3lib/error/class.t3lib_error_abstractexceptionhandler.php']);
163 }
164
165 ?>