50850103be18f4f842b6bdcef6c7eb439a2d58f1
[Packages/TYPO3.CMS.git] / typo3 / sysext / frontend / Classes / Middleware / TypoScriptFrontendInitialization.php
1 <?php
2 declare(strict_types = 1);
3 namespace TYPO3\CMS\Frontend\Middleware;
4
5 /*
6 * This file is part of the TYPO3 CMS project.
7 *
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.
11 *
12 * For the full copyright and license information, please read the
13 * LICENSE.txt file that was distributed with this source code.
14 *
15 * The TYPO3 project - inspiring people to share!
16 */
17
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;
30
31 /**
32 * Creates an instance of TypoScriptFrontendController and makes this globally available
33 * via $GLOBALS['TSFE'].
34 *
35 * @internal this middleware might get removed in TYPO3 v10.0.
36 */
37 class TypoScriptFrontendInitialization implements MiddlewareInterface, LoggerAwareInterface
38 {
39 use LoggerAwareTrait;
40
41 /**
42 * Creates an instance of TSFE and sets it as a global variable,
43 * also pings the database in order ensure a valid database connection.
44 *
45 * @param ServerRequestInterface $request
46 * @param RequestHandlerInterface $handler
47 * @return ResponseInterface
48 */
49 public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
50 {
51 $GLOBALS['TSFE'] = GeneralUtility::makeInstance(
52 TypoScriptFrontendController::class,
53 null,
54 $request->getParsedBody()['id'] ?? $request->getQueryParams()['id'] ?? 0,
55 $request->getParsedBody()['type'] ?? $request->getQueryParams()['type'] ?? 0,
56 null,
57 $request->getParsedBody()['cHash'] ?? $request->getQueryParams()['cHash'] ?? '',
58 null,
59 $request->getParsedBody()['MP'] ?? $request->getQueryParams()['MP'] ?? ''
60 );
61 if ($request->getParsedBody()['no_cache'] ?? $request->getQueryParams()['no_cache'] ?? false) {
62 $GLOBALS['TSFE']->set_no_cache('&no_cache=1 has been supplied, so caching is disabled! URL: "' . (string)$request->getUri() . '"');
63 }
64
65 // Set up the database connection and see if the connection can be established
66 try {
67 $connection = GeneralUtility::makeInstance(ConnectionPool::class)->getConnectionForTable('pages');
68 $connection->connect();
69 } catch (ConnectionException | \RuntimeException $exception) {
70 $message = 'Cannot connect to the configured database';
71 $this->logger->emergency($message, ['exception' => $exception]);
72 try {
73 return GeneralUtility::makeInstance(ErrorController::class)->unavailableAction($request, $message);
74 } catch (ServiceUnavailableException $e) {
75 throw new ServiceUnavailableException($message, 1526013723);
76 }
77 }
78 return $handler->handle($request);
79 }
80 }