‪TYPO3CMS  11.5
ProductionExceptionHandler.php
Go to the documentation of this file.
1 <?php
2 
3 /*
4  * This file is part of the TYPO3 CMS project.
5  *
6  * It is free software; you can redistribute it and/or modify it under
7  * the terms of the GNU General Public License, either version 2
8  * of the License, or any later version.
9  *
10  * For the full copyright and license information, please read the
11  * LICENSE.txt file that was distributed with this source code.
12  *
13  * The TYPO3 project - inspiring people to share!
14  */
15 
16 namespace ‪TYPO3\CMS\Core\Error;
17 
23 
30 {
36  protected ‪$defaultTitle = 'Oops, an error occurred!';
37 
43  protected ‪$defaultMessage = '';
44 
48  public function ‪__construct()
49  {
50  $callable = [$this, 'handleException'];
51  if (is_callable($callable)) {
52  set_exception_handler($callable);
53  }
54  }
55 
61  public function ‪echoExceptionWeb(\Throwable $exception)
62  {
63  $this->‪sendStatusHeaders($exception);
64  $this->‪writeLogEntries($exception, self::CONTEXT_WEB);
65  echo GeneralUtility::makeInstance(ErrorPageController::class)->errorAction(
66  $this->‪getTitle($exception),
67  $this->‪getMessage($exception),
69  $this->‪discloseExceptionInformation($exception) ? $exception->getCode() : 0,
70  503
71  );
72  }
73 
79  public function ‪echoExceptionCLI(\Throwable $exception)
80  {
81  $filePathAndName = $exception->getFile();
82  $exceptionCodeNumber = $exception->getCode() > 0 ? '#' . $exception->getCode() . ': ' : '';
83  $this->‪writeLogEntries($exception, self::CONTEXT_CLI);
84  echo LF . 'Uncaught TYPO3 Exception ' . $exceptionCodeNumber . $exception->getMessage() . LF;
85  echo 'thrown in file ' . $filePathAndName . LF;
86  echo 'in line ' . $exception->getLine() . LF . LF;
87  die(1);
88  }
89 
96  protected function ‪discloseExceptionInformation(\Throwable $exception)
97  {
98  // Allow message to be shown in production mode if the exception is about
99  // trusted host configuration. By doing so we do not disclose
100  // any valuable information to an attacker but avoid confusions among TYPO3 admins
101  // in production context.
102  if ($exception->getCode() === 1396795884) {
103  return true;
104  }
105  // Show client error messages 40x in every case
106  if ($exception instanceof ‪AbstractClientErrorException) {
107  return true;
108  }
109  // Only show errors if a BE user is authenticated
110  $backendUser = $this->‪getBackendUser();
111  if ($backendUser instanceof ‪BackendUserAuthentication) {
112  return ($backendUser->user['uid'] ?? 0) > 0;
113  }
114  return false;
115  }
116 
123  protected function ‪getTitle(\Throwable $exception)
124  {
125  if ($this->‪discloseExceptionInformation($exception) && method_exists($exception, 'getTitle') && $exception->getTitle() !== '') {
126  return $exception->getTitle();
127  }
128  return ‪$this->defaultTitle;
129  }
130 
137  protected function ‪getMessage(\Throwable $exception)
138  {
139  if ($this->‪discloseExceptionInformation($exception)) {
140  return $exception->getMessage();
141  }
143  }
144 }
‪TYPO3\CMS\Core\Error\ProductionExceptionHandler\$defaultMessage
‪string $defaultMessage
Definition: ProductionExceptionHandler.php:41
‪TYPO3\CMS\Core\Error\ProductionExceptionHandler\__construct
‪__construct()
Definition: ProductionExceptionHandler.php:46
‪TYPO3\CMS\Core\Error\AbstractExceptionHandler\getBackendUser
‪BackendUserAuthentication null getBackendUser()
Definition: AbstractExceptionHandler.php:205
‪TYPO3\CMS\Core\Messaging\AbstractMessage
Definition: AbstractMessage.php:26
‪TYPO3\CMS\Core\Controller\ErrorPageController
Definition: ErrorPageController.php:30
‪TYPO3\CMS\Core\Error\ProductionExceptionHandler
Definition: ProductionExceptionHandler.php:30
‪TYPO3\CMS\Core\Error\ProductionExceptionHandler\echoExceptionWeb
‪echoExceptionWeb(\Throwable $exception)
Definition: ProductionExceptionHandler.php:59
‪TYPO3\CMS\Core\Error\AbstractExceptionHandler\writeLogEntries
‪writeLogEntries(\Throwable $exception, string $mode)
Definition: AbstractExceptionHandler.php:83
‪TYPO3\CMS\Core\Error\Http\AbstractClientErrorException
Definition: AbstractClientErrorException.php:21
‪TYPO3\CMS\Core\Error\AbstractExceptionHandler
Definition: AbstractExceptionHandler.php:37
‪TYPO3\CMS\Core\Error\ProductionExceptionHandler\discloseExceptionInformation
‪bool discloseExceptionInformation(\Throwable $exception)
Definition: ProductionExceptionHandler.php:94
‪TYPO3\CMS\Core\Error\ProductionExceptionHandler\getMessage
‪string getMessage(\Throwable $exception)
Definition: ProductionExceptionHandler.php:135
‪TYPO3\CMS\Core\Error\ProductionExceptionHandler\$defaultTitle
‪string $defaultTitle
Definition: ProductionExceptionHandler.php:35
‪TYPO3\CMS\Core\Error\AbstractExceptionHandler\sendStatusHeaders
‪sendStatusHeaders(\Throwable $exception)
Definition: AbstractExceptionHandler.php:188
‪TYPO3\CMS\Core\Error\ProductionExceptionHandler\getTitle
‪string getTitle(\Throwable $exception)
Definition: ProductionExceptionHandler.php:121
‪TYPO3\CMS\Core\Authentication\BackendUserAuthentication
Definition: BackendUserAuthentication.php:62
‪TYPO3\CMS\Core\Error
Definition: AbstractExceptionHandler.php:16
‪TYPO3\CMS\Core\Error\ProductionExceptionHandler\echoExceptionCLI
‪echoExceptionCLI(\Throwable $exception)
Definition: ProductionExceptionHandler.php:77
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:50
‪TYPO3\CMS\Core\Messaging\AbstractMessage\ERROR
‪const ERROR
Definition: AbstractMessage.php:31