b90a0136f6e340871fce778db36c6aa118938f1d
[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
17 /**
18 * A quite exception handler which catches but ignores any exception.
19 *
20 * This file is a backport from FLOW3
21 */
22 class ProductionExceptionHandler extends AbstractExceptionHandler
23 {
24 /**
25 * Default title for error messages
26 *
27 * @var string
28 */
29 protected $defaultTitle = 'Oops, an error occurred!';
30
31 /**
32 * Default message for error messages
33 *
34 * @var string
35 */
36 protected $defaultMessage = '';
37
38 /**
39 * Constructs this exception handler - registers itself as the default exception handler.
40 */
41 public function __construct()
42 {
43 set_exception_handler(array($this, 'handleException'));
44 }
45
46 /**
47 * Echoes an exception for the web.
48 *
49 * @param \Throwable $exception The throwable object.
50 * @return void
51 */
52 public function echoExceptionWeb(\Throwable $exception)
53 {
54 $this->sendStatusHeaders($exception);
55 $this->writeLogEntries($exception, self::CONTEXT_WEB);
56 $messageObj = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance(
57 \TYPO3\CMS\Core\Messaging\ErrorpageMessage::class,
58 $this->getMessage($exception),
59 $this->getTitle($exception)
60 );
61 $messageObj->output();
62 }
63
64 /**
65 * Echoes an exception for the command line.
66 *
67 * @param \Throwable $exception The throwable object.
68 * @return void
69 */
70 public function echoExceptionCLI(\Throwable $exception)
71 {
72 $filePathAndName = $exception->getFile();
73 $exceptionCodeNumber = $exception->getCode() > 0 ? '#' . $exception->getCode() . ': ' : '';
74 $this->writeLogEntries($exception, self::CONTEXT_CLI);
75 echo LF . 'Uncaught TYPO3 Exception ' . $exceptionCodeNumber . $exception->getMessage() . LF;
76 echo 'thrown in file ' . $filePathAndName . LF;
77 echo 'in line ' . $exception->getLine() . LF . LF;
78 die(1);
79 }
80
81 /**
82 * Determines, whether Exception details should be outputted
83 *
84 * @param \Throwable $exception The throwable object.
85 * @return bool
86 */
87 protected function discloseExceptionInformation(\Throwable $exception)
88 {
89 // Allow message to be shown in production mode if the exception is about
90 // trusted host configuration. By doing so we do not disclose
91 // any valuable information to an attacker but avoid confusions among TYPO3 admins
92 // in production context.
93 if ($exception->getCode() === 1396795884) {
94 return true;
95 }
96 // Show client error messages 40x in every case
97 if ($exception instanceof Http\AbstractClientErrorException) {
98 return true;
99 }
100 // Only show errors in FE, if a BE user is authenticated
101 if (TYPO3_MODE === 'FE') {
102 return $GLOBALS['TSFE']->beUserLogin;
103 }
104 return true;
105 }
106
107 /**
108 * Returns the title for the error message
109 *
110 * @param \Throwable $exception The throwable object.
111 * @return string
112 */
113 protected function getTitle(\Throwable $exception)
114 {
115 if ($this->discloseExceptionInformation($exception) && method_exists($exception, 'getTitle') && $exception->getTitle() !== '') {
116 return htmlspecialchars($exception->getTitle());
117 } else {
118 return $this->defaultTitle;
119 }
120 }
121
122 /**
123 * Returns the message for the error message
124 *
125 * @param \Throwable $exception The throwable object.
126 * @return string
127 */
128 protected function getMessage(\Throwable $exception)
129 {
130 if ($this->discloseExceptionInformation($exception)) {
131 // Exception has an error code given
132 if ($exception->getCode() > 0) {
133 $moreInformationLink = '<p>More information regarding this error might be available <a href="'
134 . TYPO3_URL_EXCEPTION . $exception->getCode() . '" target="_blank">online</a>.</p>';
135 } else {
136 $moreInformationLink = '';
137 }
138 return htmlspecialchars($exception->getMessage()) . $moreInformationLink;
139 } else {
140 return $this->defaultMessage;
141 }
142 }
143 }