TYPO3 API  SVNRelease
class.t3lib_error_abstractexceptionhandler.php
Go to the documentation of this file.
00001 <?php
00002 /***************************************************************
00003  *  Copyright notice
00004  *
00005  *  (c) 2009-2011 Ingo Renner <ingo@typo3.org>
00006  *  All rights reserved
00007  *
00008  *  This script is part of the TYPO3 project. The TYPO3 project is
00009  *  free software; you can redistribute it and/or modify
00010  *  it under the terms of the GNU General Public License as published by
00011  *  the Free Software Foundation; either version 2 of the License, or
00012  *  (at your option) any later version.
00013  *
00014  *  The GNU General Public License can be found at
00015  *  http://www.gnu.org/copyleft/gpl.html.
00016  *
00017  *  This script is distributed in the hope that it will be useful,
00018  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00019  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00020  *  GNU General Public License for more details.
00021  *
00022  *  This copyright notice MUST APPEAR in all copies of the script!
00023  ***************************************************************/
00024 
00025 
00026 /**
00027  * An abstract exception handler
00028  *
00029  * This file is a backport from FLOW3
00030  *
00031  * @package TYPO3
00032  * @subpackage t3lib_error
00033  * @version $Id: class.t3lib_error_abstractexceptionhandler.php 10121 2011-01-18 20:15:30Z ohader $
00034  */
00035 abstract class t3lib_error_AbstractExceptionHandler implements t3lib_error_ExceptionHandlerInterface, t3lib_Singleton {
00036     const CONTEXT_WEB = 'WEB';
00037     const CONTEXT_CLI = 'CLI';
00038 
00039     /**
00040      * Displays the given exception
00041      *
00042      * @param Exception $exception The exception object
00043      * @return void
00044      */
00045     public function handleException(Exception $exception) {
00046         switch (PHP_SAPI) {
00047             case 'cli' :
00048                 $this->echoExceptionCLI($exception);
00049                 break;
00050             default :
00051                 $this->echoExceptionWeb($exception);
00052         }
00053     }
00054 
00055 
00056     /**
00057      * Writes exception to different logs
00058      *
00059      * @param Exception $exception The exception
00060      * @param string     the context where the exception was thrown, WEB or CLI
00061      * @return void
00062      * @see t3lib_div::sysLog(), t3lib_div::devLog()
00063      */
00064     protected function writeLogEntries(Exception $exception, $context) {
00065         $filePathAndName = $exception->getFile();
00066         $exceptionCodeNumber = ($exception->getCode() > 0) ? '#' . $exception->getCode() . ': ' : '';
00067         $logTitle = 'Core: Exception handler (' . $context . ')';
00068         $logMessage = 'Uncaught TYPO3 Exception: ' . $exceptionCodeNumber . $exception->getMessage() . ' | ' .
00069                       get_class($exception) . ' thrown in file ' . $filePathAndName . ' in line ' . $exception->getLine();
00070         $backtrace = $exception->getTrace();
00071 
00072             // write error message to the configured syslogs
00073         t3lib_div::sysLog($logMessage, $logTitle, 4);
00074 
00075             // When database credentials are wrong, the exception is probably
00076             // caused by this. Therefor we cannot do any database operation,
00077             // otherwise this will lead into recurring exceptions.
00078         try {
00079                 // In case an error occurs before a database connection exists, try
00080                 // to connect to the DB to be able to write the devlog/sys_log entry
00081             if (isset($GLOBALS['TYPO3_DB']) && is_object($GLOBALS['TYPO3_DB']) && empty($GLOBALS['TYPO3_DB']->link)) {
00082                 $GLOBALS['TYPO3_DB']->connectDB();
00083             }
00084 
00085                 // write error message to devlog
00086                 // see: $TYPO3_CONF_VARS['SYS']['enable_exceptionDLOG']
00087             if (TYPO3_EXCEPTION_DLOG) {
00088                 t3lib_div::devLog(
00089                     $logMessage,
00090                     $logTitle,
00091                     3,
00092                     array(
00093                         'TYPO3_MODE' => TYPO3_MODE,
00094                         'backtrace' => $backtrace
00095                     )
00096                 );
00097             }
00098 
00099                 // write error message to sys_log table
00100             $this->writeLog($logTitle . ': ' . $logMessage);
00101         } catch (Exception $exception) {
00102             // Nothing happens here. It seems the database credentials are wrong
00103         }
00104     }
00105 
00106     /**
00107      * Writes an exception in the sys_log table
00108      *
00109      * @param   string      Default text that follows the message.
00110      * @return  void
00111      */
00112     protected function writeLog($logMessage) {
00113         if (is_object($GLOBALS['TYPO3_DB']) && !empty($GLOBALS['TYPO3_DB']->link)) {
00114             $userId = 0;
00115             $workspace = 0;
00116             if (is_object($GLOBALS['BE_USER'])) {
00117                 if (isset($GLOBALS['BE_USER']->user['uid'])) {
00118                     $userId = $GLOBALS['BE_USER']->user['uid'];
00119                 }
00120                 if (isset($GLOBALS['BE_USER']->workspace)) {
00121                     $workspace = $GLOBALS['BE_USER']->workspace;
00122                 }
00123             }
00124 
00125             $fields_values = Array(
00126                 'userid' => $userId,
00127                 'type' => 5,
00128                 'action' => 0,
00129                 'error' => 2,
00130                 'details_nr' => 0,
00131                 'details' => $logMessage,
00132                 'IP' => t3lib_div::getIndpEnv('REMOTE_ADDR'),
00133                 'tstamp' => $GLOBALS['EXEC_TIME'],
00134                 'workspace' => $workspace
00135             );
00136 
00137             $GLOBALS['TYPO3_DB']->exec_INSERTquery('sys_log', $fields_values);
00138         }
00139     }
00140 
00141 
00142 }
00143 
00144 if (defined('TYPO3_MODE') && isset($GLOBALS['TYPO3_CONF_VARS'][TYPO3_MODE]['XCLASS']['t3lib/error/class.t3lib_error_abstractexceptionhandler.php'])) {
00145     include_once($GLOBALS['TYPO3_CONF_VARS'][TYPO3_MODE]['XCLASS']['t3lib/error/class.t3lib_error_abstractexceptionhandler.php']);
00146 }
00147 
00148 ?>