Fixed bug #14312: Notice undefined index systemLog and undefined constant TYPO3_ERROR...
[Packages/TYPO3.CMS.git] / t3lib / error / class.t3lib_error_abstractexceptionhandler.php
1 <?php
2 /***************************************************************
3 * Copyright notice
4 *
5 * (c) 2009 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 /**
28 * An abstract exception handler
29 *
30 * This file is a backport from FLOW3
31 *
32 * @package TYPO3
33 * @subpackage t3lib_error
34 * @version $Id$
35 */
36 abstract class t3lib_error_AbstractExceptionHandler implements t3lib_error_ExceptionHandlerInterface, t3lib_Singleton {
37 const CONTEXT_WEB = 'WEB';
38 const CONTEXT_CLI = 'CLI';
39
40 /**
41 * Displays the given exception
42 *
43 * @param Exception $exception The exception object
44 * @return void
45 */
46 public function handleException(Exception $exception) {
47 switch (PHP_SAPI) {
48 case 'cli' :
49 $this->echoExceptionCLI($exception);
50 break;
51 default :
52 $this->echoExceptionWeb($exception);
53 }
54 }
55
56
57 /**
58 * Writes exception to different logs
59 *
60 * @param Exception $exception The exception
61 * @param string the context where the exception was thrown, WEB or CLI
62 * @return void
63 * @see t3lib_div::sysLog(), t3lib_div::devLog()
64 */
65 protected function writeLogEntries(Exception $exception, $context) {
66 $filePathAndName = $exception->getFile();
67 $exceptionCodeNumber = ($exception->getCode() > 0) ? '#' . $exception->getCode() . ': ' : '';
68 $logTitle = 'Core: Exception handler (' . $context . ')';
69 $logMessage = 'Uncaught TYPO3 Exception: ' . $exceptionCodeNumber . $exception->getMessage() . ' | ' .
70 get_class($exception) . ' thrown in file ' . $filePathAndName . ' in line ' . $exception->getLine();
71 $backtrace = $exception->getTrace();
72
73 // write error message to the configured syslogs
74 t3lib_div::sysLog($logMessage, $logTitle, 4);
75
76 // When database credentials are wrong, the exception is probably
77 // caused by this. Therefor we cannot do any database operation,
78 // otherwise this will lead into recurring exceptions.
79 try {
80 // In case an error occurs before a database connection exists, try
81 // to connect to the DB to be able to write the devlog/sys_log entry
82 if (isset($GLOBALS['TYPO3_DB']) && is_object($GLOBALS['TYPO3_DB']) && empty($GLOBALS['TYPO3_DB']->link)) {
83 $GLOBALS['TYPO3_DB']->connectDB();
84 }
85
86 // write error message to devlog
87 // see: $TYPO3_CONF_VARS['SYS']['enable_exceptionDLOG']
88 if (TYPO3_EXCEPTION_DLOG) {
89 t3lib_div::devLog($logMessage, $logTitle, 3, array(
90 'TYPO3_MODE' => TYPO3_MODE,
91 'backtrace' => $backtrace
92 ));
93 }
94
95 // write error message to sys_log table
96 $this->writeLog($logTitle . ': ' . $logMessage);
97 } catch (Exception $exception) {
98 // Nothing happens here. It seems the database credentials are wrong
99 }
100 }
101
102 /**
103 * Writes an exception in the sys_log table
104 *
105 * @param string Default text that follows the message.
106 * @return void
107 */
108 protected function writeLog($logMessage) {
109 if (is_object($GLOBALS['TYPO3_DB']) && !empty($GLOBALS['TYPO3_DB']->link)) {
110 $userId = 0;
111 $workspace = 0;
112 if (is_object($GLOBALS['BE_USER'])) {
113 if (isset($GLOBALS['BE_USER']->user['uid'])) {
114 $userId = $GLOBALS['BE_USER']->user['uid'];
115 }
116 if (isset($GLOBALS['BE_USER']->workspace)) {
117 $workspace = $GLOBALS['BE_USER']->workspace;
118 }
119 }
120
121 $fields_values = Array (
122 'userid' => $userId,
123 'type' => 5,
124 'action' => 0,
125 'error' => 2,
126 'details_nr' => 0,
127 'details' => $logMessage,
128 'IP' => t3lib_div::getIndpEnv('REMOTE_ADDR'),
129 'tstamp' => $GLOBALS['EXEC_TIME'],
130 'workspace' => $workspace
131 );
132
133 $GLOBALS['TYPO3_DB']->exec_INSERTquery('sys_log', $fields_values);
134 }
135 }
136
137
138 }
139
140 if (defined('TYPO3_MODE') && $TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['t3lib/error/class.t3lib_error_abstractexceptionhandler.php']) {
141 include_once($TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['t3lib/error/class.t3lib_error_abstractexceptionhandler.php']);
142 }
143
144 ?>