2 declare(strict_types
= 1);
3 namespace TYPO3\CMS\Frontend\Middleware
;
6 * This file is part of the TYPO3 CMS project.
8 * It is free software; you can redistribute it and/or modify it under
9 * the terms of the GNU General Public License, either version 2
10 * of the License, or any later version.
12 * For the full copyright and license information, please read the
13 * LICENSE.txt file that was distributed with this source code.
15 * The TYPO3 project - inspiring people to share!
18 use Doctrine\DBAL\Exception\ConnectionException
;
19 use Psr\Http\Message\ResponseInterface
;
20 use Psr\Http\Message\ServerRequestInterface
;
21 use Psr\Http\Server\MiddlewareInterface
;
22 use Psr\Http\Server\RequestHandlerInterface
;
23 use Psr\Log\LoggerAwareInterface
;
24 use Psr\Log\LoggerAwareTrait
;
25 use TYPO3\CMS\Core\Database\ConnectionPool
;
26 use TYPO3\CMS\Core\Error\Http\ServiceUnavailableException
;
27 use TYPO3\CMS\Core\Utility\GeneralUtility
;
28 use TYPO3\CMS\Frontend\Controller\ErrorController
;
29 use TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController
;
32 * Creates an instance of TypoScriptFrontendController and makes this globally available
33 * via $GLOBALS['TSFE'].
35 * For now, GeneralUtility::_GP() is used in favor of $request->getQueryParams() due to
36 * hooks who could have $_GET/$_POST modified before.
40 class TypoScriptFrontendInitialization
implements MiddlewareInterface
, LoggerAwareInterface
45 * Creates an instance of TSFE and sets it as a global variable,
46 * also pings the database in order ensure a valid database connection.
48 * @param ServerRequestInterface $request
49 * @param RequestHandlerInterface $handler
50 * @return ResponseInterface
52 public function process(ServerRequestInterface
$request, RequestHandlerInterface
$handler): ResponseInterface
54 $GLOBALS['TSFE'] = GeneralUtility
::makeInstance(
55 TypoScriptFrontendController
::class,
57 GeneralUtility
::_GP('id'),
58 GeneralUtility
::_GP('type'),
60 GeneralUtility
::_GP('cHash'),
62 GeneralUtility
::_GP('MP')
64 if (GeneralUtility
::_GP('no_cache')) {
65 $GLOBALS['TSFE']->set_no_cache('&no_cache=1 has been supplied, so caching is disabled! URL: "' . (string)$request->getUri() . '"');
68 // Set up the database connection and see if the connection can be established
70 $connection = GeneralUtility
::makeInstance(ConnectionPool
::class)->getConnectionForTable('pages');
71 $connection->connect();
72 } catch (ConnectionException
$exception) {
73 // Cannot connect to current database
74 $message = 'Cannot connect to the configured database "' . $connection->getDatabase() . '"';
75 $this->logger
->emergency($message, ['exception' => $exception]);
77 return GeneralUtility
::makeInstance(ErrorController
::class)->unavailableAction($request, $message);
78 } catch (ServiceUnavailableException
$e) {
79 throw new ServiceUnavailableException($message, 1526013723);
82 // Call post processing function for DB connection:
83 $_params = ['pObj' => &$GLOBALS['TSFE']];
84 foreach ($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['tslib/class.tslib_fe.php']['connectToDB'] ??
[] as $_funcRef) {
85 GeneralUtility
::callUserFunction($_funcRef, $_params, $GLOBALS['TSFE']);
88 return $handler->handle($request);