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