TYPO3  7.6
core/Classes/Error/ProductionExceptionHandler.php
Go to the documentation of this file.
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 
23 {
29  protected $defaultTitle = 'Oops, an error occurred!';
30 
36  protected $defaultMessage = '';
37 
41  public function __construct()
42  {
43  set_exception_handler(array($this, 'handleException'));
44  }
45 
52  public function echoExceptionWeb(\Exception $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 
70  public function echoExceptionCLI(\Exception $exception)
71  {
72  $this->writeLogEntries($exception, self::CONTEXT_CLI);
73  die(1);
74  }
75 
82  protected function discloseExceptionInformation(\Exception $exception)
83  {
84  // Allow message to be shown in production mode if the exception is about
85  // trusted host configuration. By doing so we do not disclose
86  // any valuable information to an attacker but avoid confusions among TYPO3 admins
87  // in production context.
88  if ($exception->getCode() === 1396795884) {
89  return true;
90  }
91  // Show client error messages 40x in every case
92  if ($exception instanceof Http\AbstractClientErrorException) {
93  return true;
94  }
95  // Only show errors in FE, if a BE user is authenticated
96  if (TYPO3_MODE === 'FE') {
97  return $GLOBALS['TSFE']->beUserLogin;
98  }
99  return true;
100  }
101 
108  protected function getTitle(\Exception $exception)
109  {
110  if ($this->discloseExceptionInformation($exception) && method_exists($exception, 'getTitle') && $exception->getTitle() !== '') {
111  return htmlspecialchars($exception->getTitle());
112  } else {
113  return $this->defaultTitle;
114  }
115  }
116 
123  protected function getMessage(\Exception $exception)
124  {
125  if ($this->discloseExceptionInformation($exception)) {
126  // Exception has an error code given
127  if ($exception->getCode() > 0) {
128  $moreInformationLink = '<p>More information regarding this error might be available <a href="'
129  . TYPO3_URL_EXCEPTION . $exception->getCode() . '" target="_blank">online</a>.</p>';
130  } else {
131  $moreInformationLink = '';
132  }
133  return htmlspecialchars($exception->getMessage()) . $moreInformationLink;
134  } else {
135  return $this->defaultMessage;
136  }
137  }
138 }