023ca889430f69c71938b9ace1739e7fe26f430c
[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\Controller\ErrorPageController;
17 use TYPO3\CMS\Core\Messaging\AbstractMessage;
18 use TYPO3\CMS\Core\Utility\GeneralUtility;
19
20 /**
21 * A quite exception handler which catches but ignores any exception.
22 *
23 * This file is a backport from FLOW3
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 public function __construct()
45 {
46 set_exception_handler([$this, 'handleException']);
47 }
48
49 /**
50 * Echoes an exception for the web.
51 *
52 * @param \Throwable $exception The throwable object.
53 */
54 public function echoExceptionWeb(\Throwable $exception)
55 {
56 $this->sendStatusHeaders($exception);
57 $this->writeLogEntries($exception, self::CONTEXT_WEB);
58 echo GeneralUtility::makeInstance(ErrorPageController::class)->errorAction(
59 $this->getTitle($exception),
60 $this->getMessage($exception),
61 AbstractMessage::ERROR,
62 $this->discloseExceptionInformation($exception) ? $exception->getCode() : 0
63 );
64 }
65
66 /**
67 * Echoes an exception for the command line.
68 *
69 * @param \Throwable $exception The throwable object.
70 */
71 public function echoExceptionCLI(\Throwable $exception)
72 {
73 $filePathAndName = $exception->getFile();
74 $exceptionCodeNumber = $exception->getCode() > 0 ? '#' . $exception->getCode() . ': ' : '';
75 $this->writeLogEntries($exception, self::CONTEXT_CLI);
76 echo LF . 'Uncaught TYPO3 Exception ' . $exceptionCodeNumber . $exception->getMessage() . LF;
77 echo 'thrown in file ' . $filePathAndName . LF;
78 echo 'in line ' . $exception->getLine() . LF . LF;
79 die(1);
80 }
81
82 /**
83 * Determines, whether Exception details should be outputted
84 *
85 * @param \Throwable $exception The throwable object.
86 * @return bool
87 */
88 protected function discloseExceptionInformation(\Throwable $exception)
89 {
90 // Allow message to be shown in production mode if the exception is about
91 // trusted host configuration. By doing so we do not disclose
92 // any valuable information to an attacker but avoid confusions among TYPO3 admins
93 // in production context.
94 if ($exception->getCode() === 1396795884) {
95 return true;
96 }
97 // Show client error messages 40x in every case
98 if ($exception instanceof Http\AbstractClientErrorException) {
99 return true;
100 }
101 // Only show errors in FE, if a BE user is authenticated
102 if (TYPO3_MODE === 'FE') {
103 return $GLOBALS['TSFE']->beUserLogin;
104 }
105 return true;
106 }
107
108 /**
109 * Returns the title for the error message
110 *
111 * @param \Throwable $exception The throwable object.
112 * @return string
113 */
114 protected function getTitle(\Throwable $exception)
115 {
116 if ($this->discloseExceptionInformation($exception) && method_exists($exception, 'getTitle') && $exception->getTitle() !== '') {
117 return $exception->getTitle();
118 } else {
119 return $this->defaultTitle;
120 }
121 }
122
123 /**
124 * Returns the message for the error message
125 *
126 * @param \Throwable $exception The throwable object.
127 * @return string
128 */
129 protected function getMessage(\Throwable $exception)
130 {
131 if ($this->discloseExceptionInformation($exception)) {
132 return $exception->getMessage();
133 } else {
134 return $this->defaultMessage;
135 }
136 }
137 }