TYPO3 API  SVNRelease
class.t3lib_error_debugexceptionhandler.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  * A basic but solid exception handler which catches everything which
00028  * falls through the other exception handlers and provides useful debugging
00029  * information.
00030  *
00031  * This file is a backport from FLOW3
00032  *
00033  * @package TYPO3
00034  * @subpackage t3lib_error
00035  * @version $Id: class.t3lib_error_debugexceptionhandler.php 10317 2011-01-26 00:56:49Z baschny $
00036  */
00037 class t3lib_error_DebugExceptionHandler extends t3lib_error_AbstractExceptionHandler {
00038 
00039     /**
00040      * Constructs this exception handler - registers itself as the default exception handler.
00041      *
00042      * @author Robert Lemke <robert@typo3.org>
00043      */
00044     public function __construct() {
00045         set_exception_handler(array($this, 'handleException'));
00046     }
00047 
00048     /**
00049      * Formats and echoes the exception as XHTML.
00050      *
00051      * @param  Exception $exception The exception object
00052      * @return void
00053      * @author Robert Lemke <robert@typo3.org>
00054      */
00055     public function echoExceptionWeb(Exception $exception) {
00056         if (!headers_sent()) {
00057             header("HTTP/1.1 500 Internal Server Error");
00058         }
00059 
00060         $filePathAndName = $exception->getFile();
00061 
00062         $exceptionCodeNumber = ($exception->getCode() > 0) ? '#' . $exception->getCode() . ': ' : '';
00063 
00064         /**
00065          * TODO: 25.09.2009
00066          * either remove this line or let the link point to site that offers error information for TYPO3
00067          */
00068 
00069             //      $moreInformationLink = ($exceptionCodeNumber != '') ? '(<a href="' . TYPO3_URL_EXCEPTION . $exception->getCode() . '">More information</a>)' : '';
00070         $backtraceCode = $this->getBacktraceCode($exception->getTrace());
00071 
00072         $this->writeLogEntries($exception, self::CONTEXT_WEB);
00073 
00074             // Set the XML prologue
00075         $xmlPrologue = '<?xml version="1.0" encoding="utf-8"?>';
00076 
00077             // Set the doctype declaration
00078         $docType = '<!DOCTYPE html
00079      PUBLIC "-//W3C//DTD XHTML 1.1//EN"
00080      "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">';
00081 
00082             // Get the browser info
00083         $browserInfo = t3lib_utility_Client::getBrowserInfo(t3lib_div::getIndpEnv('HTTP_USER_AGENT'));
00084 
00085             // Put the XML prologue before or after the doctype declaration according to browser
00086         if ($browserInfo['browser'] === 'msie' && $browserInfo['version'] < 7) {
00087             $headerStart = $docType . LF . $xmlPrologue;
00088         } else {
00089             $headerStart = $xmlPrologue . LF . $docType;
00090         }
00091 
00092         echo $headerStart . '
00093             <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
00094                 <head>
00095                     <title>TYPO3 Exception</title>
00096                     <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
00097                     <style type="text/css">
00098                         .ExceptionProperty {
00099                             color: #101010;
00100                         }
00101                         pre {
00102                             margin: 0;
00103                             font-size: 11px;
00104                             color: #515151;
00105                             background-color: #D0D0D0;
00106                             padding-left: 30px;
00107                         }
00108                     </style>
00109                 </head>
00110                 <body>
00111                     <div style="
00112                             position: absolute;
00113                             left: 10px;
00114                             background-color: #B9B9B9;
00115                             outline: 1px solid #515151;
00116                             color: #515151;
00117                             font-family: Arial, Helvetica, sans-serif;
00118                             font-size: 12px;
00119                             margin: 10px;
00120                             padding: 0;
00121                         ">
00122                         <div style="width: 100%; background-color: #515151; color: white; padding: 2px; margin: 0 0 6px 0;">Uncaught TYPO3 Exception</div>
00123                         <div style="width: 100%; padding: 2px; margin: 0 0 6px 0;">
00124                             <strong style="color: #BE0027;">' . $exceptionCodeNumber . $exception->getMessage() . '</strong> ' . /* $moreInformationLink .*/
00125              '<br />
00126                             <br />
00127                             <span class="ExceptionProperty">' . get_class($exception) . '</span> thrown in file<br />
00128                             <span class="ExceptionProperty">' . $filePathAndName . '</span> in line
00129                             <span class="ExceptionProperty">' . $exception->getLine() . '</span>.<br />
00130                             <br />
00131                             ' . $backtraceCode . '
00132                         </div>
00133                     </div>
00134                 </body>
00135             </html>
00136         ';
00137     }
00138 
00139     /**
00140      * Formats and echoes the exception for the command line
00141      *
00142      * @param Exception $exception The exception object
00143      * @return void
00144      * @author Robert Lemke <robert@typo3.org>
00145      */
00146     public function echoExceptionCLI(Exception $exception) {
00147         $filePathAndName = $exception->getFile();
00148         $exceptionCodeNumber = ($exception->getCode() > 0) ? '#' . $exception->getCode() . ': ' : '';
00149         $this->writeLogEntries($exception, self::CONTEXT_CLI);
00150 
00151         echo "\nUncaught TYPO3 Exception " . $exceptionCodeNumber . $exception->getMessage() . LF;
00152         echo "thrown in file " . $filePathAndName . LF;
00153         echo "in line " . $exception->getLine() . "\n\n";
00154     }
00155 
00156     /**
00157      * Renders some backtrace
00158      *
00159      * @param array $trace The trace
00160      * @return string Backtrace information
00161      * @author Robert Lemke <robert@typo3.org>
00162      */
00163     protected function getBacktraceCode(array $trace) {
00164         $backtraceCode = '';
00165         if (count($trace)) {
00166             foreach ($trace as $index => $step) {
00167                 $class = isset($step['class']) ? $step['class'] . '<span style="color:white;">::</span>' : '';
00168 
00169                 $arguments = '';
00170                 if (isset($step['args']) && is_array($step['args'])) {
00171                     foreach ($step['args'] as $argument) {
00172                         $arguments .= (strlen($arguments) === 0) ? '' : '<span style="color:white;">,</span> ';
00173                         if (is_object($argument)) {
00174                             $arguments .= '<span style="color:#FF8700;"><em>' . get_class($argument) . '</em></span>';
00175                         } elseif (is_string($argument)) {
00176                             $preparedArgument = (strlen($argument) < 100) ? $argument : substr($argument, 0, 50) . '#tripleDot#' . substr($argument, -50);
00177                             $preparedArgument = htmlspecialchars($preparedArgument);
00178                             $preparedArgument = str_replace('#tripleDot#', '<span style="color:white;">&hellip;</span>', $preparedArgument);
00179                             $preparedArgument = str_replace(LF, '<span style="color:white;">&crarr;</span>', $preparedArgument);
00180                             $arguments .= '"<span style="color:#FF8700;" title="' . htmlspecialchars($argument) . '">' . $preparedArgument . '</span>"';
00181                         } elseif (is_numeric($argument)) {
00182                             $arguments .= '<span style="color:#FF8700;">' . (string) $argument . '</span>';
00183                         } else {
00184                             $arguments .= '<span style="color:#FF8700;"><em>' . gettype($argument) . '</em></span>';
00185                         }
00186                     }
00187                 }
00188 
00189                 $backtraceCode .= '<pre style="color:#69A550; background-color: #414141; padding: 4px 2px 4px 2px;">';
00190                 $backtraceCode .= '<span style="color:white;">' . (count($trace) - $index) . '</span> ' . $class . $step['function'] . '<span style="color:white;">(' . $arguments . ')</span>';
00191                 $backtraceCode .= '</pre>';
00192 
00193                 if (isset($step['file'])) {
00194                     $backtraceCode .= $this->getCodeSnippet($step['file'], $step['line']) . '<br />';
00195                 }
00196             }
00197         }
00198 
00199         return $backtraceCode;
00200     }
00201 
00202     /**
00203      * Returns a code snippet from the specified file.
00204      *
00205      * @param string $filePathAndName Absolute path and file name of the PHP file
00206      * @param integer $lineNumber Line number defining the center of the code snippet
00207      * @return string The code snippet
00208      * @author Robert Lemke <robert@typo3.org>
00209      */
00210     protected function getCodeSnippet($filePathAndName, $lineNumber) {
00211         $codeSnippet = '<br />';
00212         if (@file_exists($filePathAndName)) {
00213             $phpFile = @file($filePathAndName);
00214             if (is_array($phpFile)) {
00215                 $startLine = ($lineNumber > 2) ? ($lineNumber - 2) : 1;
00216                 $endLine = ($lineNumber < (count($phpFile) - 2)) ? ($lineNumber + 3) : count($phpFile) + 1;
00217                 if ($endLine > $startLine) {
00218                     $codeSnippet = '<br /><span style="font-size:10px;">' . $filePathAndName . ':</span><br /><pre>';
00219                     for ($line = $startLine; $line < $endLine; $line++) {
00220                         $codeLine = str_replace(TAB, ' ', $phpFile[$line - 1]);
00221 
00222                         if ($line === $lineNumber) {
00223                             $codeSnippet .= '</pre><pre style="background-color: #F1F1F1; color: black;">';
00224                         }
00225                         $codeSnippet .= sprintf('%05d', $line) . ': ' . $codeLine;
00226                         if ($line === $lineNumber) {
00227                             $codeSnippet .= '</pre><pre>';
00228                         }
00229                     }
00230                     $codeSnippet .= '</pre>';
00231                 }
00232             }
00233         }
00234         return $codeSnippet;
00235     }
00236 }
00237 
00238 
00239 if (defined('TYPO3_MODE') && isset($GLOBALS['TYPO3_CONF_VARS'][TYPO3_MODE]['XCLASS']['t3lib/error/class.t3lib_error_debugexceptionhandler.php'])) {
00240     include_once($GLOBALS['TYPO3_CONF_VARS'][TYPO3_MODE]['XCLASS']['t3lib/error/class.t3lib_error_debugexceptionhandler.php']);
00241 }
00242 
00243 ?>