‪TYPO3CMS  ‪main
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 
21 
28 {
34  protected ‪$defaultTitle = 'Oops, an error occurred!';
35 
41  protected ‪$defaultMessage = '';
42 
46  public function ‪__construct()
47  {
48  $callable = [$this, 'handleException'];
49  if (is_callable($callable)) {
50  set_exception_handler($callable);
51  }
52  }
53 
59  public function ‪echoExceptionWeb(\Throwable $exception)
60  {
61  $this->‪sendStatusHeaders($exception);
62  $this->‪writeLogEntries($exception, self::CONTEXT_WEB);
63  echo GeneralUtility::makeInstance(ErrorPageController::class)->errorAction(
64  $this->‪getTitle($exception),
65  $this->‪getMessage($exception),
66  $this->‪discloseExceptionInformation($exception) ? $exception->getCode() : 0,
67  503
68  );
69  }
70 
76  public function ‪echoExceptionCLI(\Throwable $exception)
77  {
78  $filePathAndName = $exception->getFile();
79  $exceptionCodeNumber = $exception->getCode() > 0 ? '#' . $exception->getCode() . ': ' : '';
80  $this->‪writeLogEntries($exception, self::CONTEXT_CLI);
81  echo LF . 'Uncaught TYPO3 Exception ' . $exceptionCodeNumber . $exception->getMessage() . LF;
82  echo 'thrown in file ' . $filePathAndName . LF;
83  echo 'in line ' . $exception->getLine() . LF . LF;
84  die(1);
85  }
86 
93  protected function ‪discloseExceptionInformation(\Throwable $exception)
94  {
95  // Allow message to be shown in production mode if the exception is about
96  // trusted host configuration. By doing so we do not disclose
97  // any valuable information to an attacker but avoid confusions among TYPO3 admins
98  // in production context.
99  if ($exception->getCode() === 1396795884) {
100  return true;
101  }
102  // Show client error messages 40x in every case
103  if ($exception instanceof ‪AbstractClientErrorException) {
104  return true;
105  }
106  // Only show errors if a BE user is authenticated
107  $backendUser = $this->‪getBackendUser();
108  if ($backendUser === null) {
109  return false;
110  }
111  return ($backendUser->user['uid'] ?? 0) > 0;
112  }
113 
120  protected function ‪getTitle(\Throwable $exception)
121  {
122  if ($this->‪discloseExceptionInformation($exception) && method_exists($exception, 'getTitle') && $exception->getTitle() !== '') {
123  return $exception->getTitle();
124  }
125  return ‪$this->defaultTitle;
126  }
127 
134  protected function ‪getMessage(\Throwable $exception)
135  {
136  if ($this->‪discloseExceptionInformation($exception)) {
137  return $exception->getMessage();
138  }
140  }
141 }
‪TYPO3\CMS\Core\Error\ProductionExceptionHandler\$defaultMessage
‪string $defaultMessage
Definition: ProductionExceptionHandler.php:39
‪TYPO3\CMS\Core\Error\ProductionExceptionHandler\__construct
‪__construct()
Definition: ProductionExceptionHandler.php:44
‪TYPO3\CMS\Core\Controller\ErrorPageController
Definition: ErrorPageController.php:30
‪TYPO3\CMS\Core\Error\ProductionExceptionHandler
Definition: ProductionExceptionHandler.php:28
‪TYPO3\CMS\Core\Error\ProductionExceptionHandler\echoExceptionWeb
‪echoExceptionWeb(\Throwable $exception)
Definition: ProductionExceptionHandler.php:57
‪TYPO3\CMS\Core\Error\AbstractExceptionHandler\writeLogEntries
‪writeLogEntries(\Throwable $exception, string $mode)
Definition: AbstractExceptionHandler.php:87
‪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:91
‪TYPO3\CMS\Core\Error\ProductionExceptionHandler\getMessage
‪string getMessage(\Throwable $exception)
Definition: ProductionExceptionHandler.php:132
‪TYPO3\CMS\Core\Error\ProductionExceptionHandler\$defaultTitle
‪string $defaultTitle
Definition: ProductionExceptionHandler.php:33
‪TYPO3\CMS\Core\Error\AbstractExceptionHandler\sendStatusHeaders
‪sendStatusHeaders(\Throwable $exception)
Definition: AbstractExceptionHandler.php:192
‪TYPO3\CMS\Core\Error\ProductionExceptionHandler\getTitle
‪string getTitle(\Throwable $exception)
Definition: ProductionExceptionHandler.php:118
‪TYPO3\CMS\Core\Error
Definition: AbstractExceptionHandler.php:16
‪TYPO3\CMS\Core\Error\ProductionExceptionHandler\echoExceptionCLI
‪echoExceptionCLI(\Throwable $exception)
Definition: ProductionExceptionHandler.php:74
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:52
‪TYPO3\CMS\Core\Error\AbstractExceptionHandler\getBackendUser
‪getBackendUser()
Definition: AbstractExceptionHandler.php:206