ee543f37a636b1157b0b299fc01134a63ba3adee
[Packages/TYPO3.CMS.git] / typo3 / sysext / core / Classes / Error / ProductionExceptionHandler.php
1 <?php
2 namespace TYPO3\CMS\Core\Error;
3
4 /***************************************************************
5 * Copyright notice
6 *
7 * (c) 2009-2013 Ingo Renner <ingo@typo3.org>
8 * All rights reserved
9 *
10 * This script is part of the TYPO3 project. The TYPO3 project is
11 * free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * The GNU General Public License can be found at
17 * http://www.gnu.org/copyleft/gpl.html.
18 *
19 * This script is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
23 *
24 * This copyright notice MUST APPEAR in all copies of the script!
25 ***************************************************************/
26 /**
27 * A quite exception handler which catches but ignores any exception.
28 *
29 * This file is a backport from FLOW3
30 *
31 * @author Ingo Renner <ingo@typo3.org>
32 */
33 class ProductionExceptionHandler extends \TYPO3\CMS\Core\Error\AbstractExceptionHandler {
34
35 /**
36 * Default title for error messages
37 *
38 * @var string
39 */
40 protected $defaultTitle = 'Oops, an error occurred!';
41
42 /**
43 * Default message for error messages
44 *
45 * @var string
46 */
47 protected $defaultMessage = '';
48
49 /**
50 * Constructs this exception handler - registers itself as the default exception handler.
51 *
52 * @author Robert Lemke <robert@typo3.org>
53 */
54 public function __construct() {
55 set_exception_handler(array($this, 'handleException'));
56 }
57
58 /**
59 * Echoes an exception for the web.
60 *
61 * @param Exception $exception The exception
62 * @return void
63 */
64 public function echoExceptionWeb(\Exception $exception) {
65 $this->sendStatusHeaders($exception);
66 $this->writeLogEntries($exception, self::CONTEXT_WEB);
67 $messageObj = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Core\\Messaging\\ErrorpageMessage', $this->getMessage($exception), $this->getTitle($exception));
68 $messageObj->output();
69 }
70
71 /**
72 * Echoes an exception for the command line.
73 *
74 * @param Exception $exception The exception
75 * @return void
76 */
77 public function echoExceptionCLI(\Exception $exception) {
78 $this->writeLogEntries($exception, self::CONTEXT_CLI);
79 die(1);
80 }
81
82 /**
83 * Determines, whether Exception details should be outputted
84 *
85 * @param Exception $exception The exception
86 * @return boolean
87 */
88 protected function discloseExceptionInformation(\Exception $exception) {
89 // Show client error messages 40x in every case
90 if ($exception instanceof \TYPO3\CMS\Core\Error\Http\AbstractClientErrorException) {
91 return TRUE;
92 }
93 // Only show errors in FE, if a BE user is authenticated
94 if (TYPO3_MODE === 'FE') {
95 return $GLOBALS['TSFE']->beUserLogin;
96 }
97 return TRUE;
98 }
99
100 /**
101 * Returns the title for the error message
102 *
103 * @param Exception $exception Exception causing the error
104 * @return string
105 */
106 protected function getTitle(\Exception $exception) {
107 if ($this->discloseExceptionInformation($exception) && method_exists($exception, 'getTitle') && strlen($exception->getTitle()) > 0) {
108 return htmlspecialchars($exception->getTitle());
109 } else {
110 return $this->defaultTitle;
111 }
112 }
113
114 /**
115 * Returns the message for the error message
116 *
117 * @param Exception $exception Exception causing the error
118 * @return string
119 */
120 protected function getMessage(\Exception $exception) {
121 if ($this->discloseExceptionInformation($exception)) {
122 // Exception has an error code given
123 if ($exception->getCode() > 0) {
124 $moreInformationLink = '<p>More information regarding this error might be available <a href="' . TYPO3_URL_EXCEPTION . $exception->getCode() . '" target="_blank">online</a>.</p>';
125 } else {
126 $moreInformationLink = '';
127 }
128 return htmlspecialchars($exception->getMessage()) . $moreInformationLink;
129 } else {
130 return $this->defaultMessage;
131 }
132 }
133
134 }
135
136
137 ?>