[TASK] Re-work/simplify copyright header in PHP files - Part 3
[Packages/TYPO3.CMS.git] / typo3 / sysext / core / Classes / Error / ProductionExceptionHandler.php
1 <?php
2 namespace TYPO3\CMS\Core\Error;
3
4 /**
5 * This file is part of the TYPO3 CMS project.
6 *
7 * It is free software; you can redistribute it and/or modify it under
8 * the terms of the GNU General Public License, either version 2
9 * of the License, or any later version.
10 *
11 * For the full copyright and license information, please read the
12 * LICENSE.txt file that was distributed with this source code.
13 *
14 * The TYPO3 project - inspiring people to share!
15 */
16 use TYPO3\CMS\Core\Messaging\ErrorpageMessage;
17
18 /**
19 * A quite exception handler which catches but ignores any exception.
20 *
21 * This file is a backport from FLOW3
22 *
23 * @author Ingo Renner <ingo@typo3.org>
24 */
25 class ProductionExceptionHandler extends AbstractExceptionHandler {
26
27 /**
28 * Default title for error messages
29 *
30 * @var string
31 */
32 protected $defaultTitle = 'Oops, an error occurred!';
33
34 /**
35 * Default message for error messages
36 *
37 * @var string
38 */
39 protected $defaultMessage = '';
40
41 /**
42 * Constructs this exception handler - registers itself as the default exception handler.
43 *
44 * @author Robert Lemke <robert@typo3.org>
45 */
46 public function __construct() {
47 set_exception_handler(array($this, 'handleException'));
48 }
49
50 /**
51 * Echoes an exception for the web.
52 *
53 * @param \Exception $exception The exception
54 * @return void
55 */
56 public function echoExceptionWeb(\Exception $exception) {
57 $this->sendStatusHeaders($exception);
58 $this->writeLogEntries($exception, self::CONTEXT_WEB);
59 $messageObj = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance(
60 'TYPO3\\CMS\\Core\\Messaging\\ErrorpageMessage',
61 $this->getMessage($exception),
62 $this->getTitle($exception)
63 );
64 $messageObj->output();
65 }
66
67 /**
68 * Echoes an exception for the command line.
69 *
70 * @param \Exception $exception The exception
71 * @return void
72 */
73 public function echoExceptionCLI(\Exception $exception) {
74 $this->writeLogEntries($exception, self::CONTEXT_CLI);
75 die(1);
76 }
77
78 /**
79 * Determines, whether Exception details should be outputted
80 *
81 * @param \Exception $exception The exception
82 * @return boolean
83 */
84 protected function discloseExceptionInformation(\Exception $exception) {
85 // Allow message to be shown in production mode if the exception is about
86 // trusted host configuration. By doing so we do not disclose
87 // any valuable information to an attacker but avoid confusions among TYPO3 admins
88 // in production context.
89 if ($exception->getCode() === 1396795884) {
90 return TRUE;
91 }
92 // Show client error messages 40x in every case
93 if ($exception instanceof Http\AbstractClientErrorException) {
94 return TRUE;
95 }
96 // Only show errors in FE, if a BE user is authenticated
97 if (TYPO3_MODE === 'FE') {
98 return $GLOBALS['TSFE']->beUserLogin;
99 }
100 return TRUE;
101 }
102
103 /**
104 * Returns the title for the error message
105 *
106 * @param \Exception $exception Exception causing the error
107 * @return string
108 */
109 protected function getTitle(\Exception $exception) {
110 if ($this->discloseExceptionInformation($exception) && method_exists($exception, 'getTitle') && $exception->getTitle() !== '') {
111 return htmlspecialchars($exception->getTitle());
112 } else {
113 return $this->defaultTitle;
114 }
115 }
116
117 /**
118 * Returns the message for the error message
119 *
120 * @param \Exception $exception Exception causing the error
121 * @return string
122 */
123 protected function getMessage(\Exception $exception) {
124 if ($this->discloseExceptionInformation($exception)) {
125 // Exception has an error code given
126 if ($exception->getCode() > 0) {
127 $moreInformationLink = '<p>More information regarding this error might be available <a href="'
128 . TYPO3_URL_EXCEPTION . $exception->getCode() . '" target="_blank">online</a>.</p>';
129 } else {
130 $moreInformationLink = '';
131 }
132 return htmlspecialchars($exception->getMessage()) . $moreInformationLink;
133 } else {
134 return $this->defaultMessage;
135 }
136 }
137
138 }