TYPO3 API  SVNRelease
class.t3lib_error_errorhandler.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  * Global error handler for TYPO3
00028  *
00029  * This file is a backport from FLOW3
00030  *
00031  * @package TYPO3
00032  * @subpackage t3lib_error
00033  * @author Rupert Germann <rupi@gmx.li>
00034  * @version $Id: class.t3lib_error_errorhandler.php 10121 2011-01-18 20:15:30Z ohader $
00035  */
00036 class t3lib_error_ErrorHandler implements t3lib_error_ErrorHandlerInterface {
00037 
00038     /**
00039      * Error levels which should result in an exception thrown.
00040      *
00041      * @var integer
00042      */
00043     protected $exceptionalErrors = array();
00044 
00045     /**
00046      * Registers this class as default error handler
00047      *
00048      * @param integer    The integer representing the E_* error level which should be
00049      *                   handled by the registered error handler.
00050      * @return void
00051      */
00052     public function __construct($errorHandlerErrors) {
00053         set_error_handler(array($this, 'handleError'), $errorHandlerErrors);
00054     }
00055 
00056 
00057     /**
00058      * Defines which error levels should result in an exception thrown.
00059      *
00060      * @param integer    The integer representing the E_* error level to handle as exceptions
00061      * @return void
00062      */
00063     public function setExceptionalErrors($exceptionalErrors) {
00064         $this->exceptionalErrors = (int) $exceptionalErrors;
00065 
00066     }
00067 
00068     /**
00069      * Handles an error.
00070      * If the error is registered as exceptionalError it will by converted into an exception, to be handled
00071      * by the configured exceptionhandler. Additionall the error message is written to the configured logs.
00072      * If TYPO3_MODE is 'BE' the error message is also added to the flashMessageQueue, in FE the error message
00073      * is displayed in the admin panel (as TsLog message)
00074      *
00075      * @param integer    The error level - one of the E_* constants
00076      * @param string     The error message
00077      * @param string     Name of the file the error occurred in
00078      * @param integer    Line number where the error occurred
00079      * @return void
00080      * @throws t3lib_error_Exception with the data passed to this method if the error is registered as exceptionalError
00081      */
00082     public function handleError($errorLevel, $errorMessage, $errorFile, $errorLine) {
00083             // don't do anything if error_reporting is disabled by an @ sign
00084         if (error_reporting() == 0) {
00085             return TRUE;
00086         }
00087 
00088         $errorLevels = array(
00089             E_WARNING => 'Warning',
00090             E_NOTICE => 'Notice',
00091             E_USER_ERROR => 'User Error',
00092             E_USER_WARNING => 'User Warning',
00093             E_USER_NOTICE => 'User Notice',
00094             E_STRICT => 'Runtime Notice',
00095             E_RECOVERABLE_ERROR => 'Catchable Fatal Error'
00096         );
00097 
00098         $message = 'PHP ' . $errorLevels[$errorLevel] . ': ' . $errorMessage . ' in ' . $errorFile . ' line ' . $errorLine;
00099 
00100         if ($errorLevel & $this->exceptionalErrors) {
00101             if (!class_exists('t3lib_error_Exception', FALSE)) {
00102                 require_once(PATH_t3lib . 'class.t3lib_exception.php');
00103                 require_once(PATH_t3lib . 'error/class.t3lib_error_exception.php');
00104             }
00105 
00106             throw new t3lib_error_Exception($message, 1);
00107         } else {
00108 
00109             switch ($errorLevel) {
00110                 case E_USER_ERROR:
00111                 case E_RECOVERABLE_ERROR:
00112                     $severity = 2;
00113                     break;
00114                 case E_USER_WARNING:
00115                 case E_WARNING:
00116                     $severity = 1;
00117                     break;
00118                 default:
00119                     $severity = 0;
00120                     break;
00121             }
00122 
00123             $logTitle = 'Core: Error handler (' . TYPO3_MODE . ')';
00124 
00125                 // Write error message to the configured syslogs,
00126                 // see: $TYPO3_CONF_VARS['SYS']['systemLog']
00127             if ($errorLevel & $GLOBALS['TYPO3_CONF_VARS']['SYS']['syslogErrorReporting']) {
00128                 t3lib_div::sysLog($message, $logTitle, $severity);
00129             }
00130 
00131                 // In case an error occurs before a database connection exists, try
00132                 // to connect to the DB to be able to write an entry to devlog/sys_log
00133             if (is_object($GLOBALS['TYPO3_DB']) && empty($GLOBALS['TYPO3_DB']->link)) {
00134                 try {
00135                     $GLOBALS['TYPO3_DB']->connectDB();
00136                 }
00137                 catch (Exception $e) {
00138                     // There's nothing more we can do at this point if the
00139                     // database failed. It is up to the various log writers
00140                     // to check for themselves whether the have a DB connection
00141                     // available or not.
00142                 }
00143             }
00144 
00145                 // Write error message to devlog extension(s),
00146                 // see: $TYPO3_CONF_VARS['SYS']['enable_errorDLOG']
00147             if (TYPO3_ERROR_DLOG) {
00148                 t3lib_div::devLog($message, $logTitle, $severity + 1);
00149             }
00150                 // Write error message to TSlog (admin panel)
00151             if (is_object($GLOBALS['TT'])) {
00152                 $GLOBALS['TT']->setTSlogMessage($logTitle . ': ' . $message, $severity + 1);
00153             }
00154                 // Write error message to sys_log table (ext: belog, Tools->Log)
00155             if ($errorLevel & $GLOBALS['TYPO3_CONF_VARS']['SYS']['belogErrorReporting']) {
00156                 $this->writeLog($logTitle . ': ' . $message, $severity);
00157             }
00158 
00159                 // Add error message to the flashmessageQueue
00160             if (defined('TYPO3_ERRORHANDLER_MODE') && TYPO3_ERRORHANDLER_MODE == 'debug') {
00161                 $flashMessage = t3lib_div::makeInstance(
00162                     't3lib_FlashMessage',
00163                     $message,
00164                     'PHP ' . $errorLevels[$errorLevel],
00165                     $severity
00166                 );
00167                 t3lib_FlashMessageQueue::addMessage($flashMessage);
00168             }
00169         }
00170 
00171             // Don't execute PHP internal error handler
00172         return TRUE;
00173     }
00174 
00175     /**
00176      * Writes an error in the sys_log table
00177      *
00178      * @param   string      Default text that follows the message (in english!).
00179      * @param   integer     The eror level of the message (0 = OK, 1 = warning, 2 = error)
00180      * @return  void
00181      */
00182     protected function writeLog($logMessage, $severity) {
00183         if (is_object($GLOBALS['TYPO3_DB']) && !empty($GLOBALS['TYPO3_DB']->link)) {
00184             $userId = 0;
00185             $workspace = 0;
00186             if (is_object($GLOBALS['BE_USER'])) {
00187                 if (isset($GLOBALS['BE_USER']->user['uid'])) {
00188                     $userId = $GLOBALS['BE_USER']->user['uid'];
00189                 }
00190                 if (isset($GLOBALS['BE_USER']->workspace)) {
00191                     $workspace = $GLOBALS['BE_USER']->workspace;
00192                 }
00193             }
00194 
00195             $fields_values = Array(
00196                 'userid' => $userId,
00197                 'type' => 5,
00198                 'action' => 0,
00199                 'error' => $severity,
00200                 'details_nr' => 0,
00201                 'details' => $logMessage,
00202                 'IP' => t3lib_div::getIndpEnv('REMOTE_ADDR'),
00203                 'tstamp' => $GLOBALS['EXEC_TIME'],
00204                 'workspace' => $workspace
00205             );
00206             $GLOBALS['TYPO3_DB']->exec_INSERTquery('sys_log', $fields_values);
00207         }
00208     }
00209 
00210 }
00211 
00212 
00213 if (defined('TYPO3_MODE') && isset($GLOBALS['TYPO3_CONF_VARS'][TYPO3_MODE]['XCLASS']['t3lib/error/class.t3lib_error_errorhandler.php'])) {
00214     include_once($GLOBALS['TYPO3_CONF_VARS'][TYPO3_MODE]['XCLASS']['t3lib/error/class.t3lib_error_errorhandler.php']);
00215 }
00216 
00217 ?>