3 declare(strict_types
=1);
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 namespace TYPO3\CMS\Frontend\Middleware
;
20 use Psr\Http\Message\ResponseInterface
;
21 use Psr\Http\Message\ServerRequestInterface
;
22 use Psr\Http\Server\MiddlewareInterface
;
23 use Psr\Http\Server\RequestHandlerInterface
;
24 use TYPO3\CMS\Core\Context\Context
;
25 use TYPO3\CMS\Core\Routing\PageArguments
;
26 use TYPO3\CMS\Core\Site\Entity\Site
;
27 use TYPO3\CMS\Core\Utility\GeneralUtility
;
28 use TYPO3\CMS\Frontend\Authentication\FrontendUserAuthentication
;
29 use TYPO3\CMS\Frontend\Controller\ErrorController
;
30 use TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController
;
31 use TYPO3\CMS\Frontend\Page\PageAccessFailureReasons
;
34 * Creates an instance of TypoScriptFrontendController and makes this globally available
35 * via $GLOBALS['TSFE'].
37 * In addition, determineId builds up the rootline based on a valid frontend-user authentication and
38 * Backend permissions if previewing.
40 * @internal this middleware might get removed in TYPO3 v11.0.
42 class TypoScriptFrontendInitialization
implements MiddlewareInterface
49 public function __construct(Context
$context)
51 $this->context
= $context;
55 * Creates an instance of TSFE and sets it as a global variable.
57 * @param ServerRequestInterface $request
58 * @param RequestHandlerInterface $handler
59 * @return ResponseInterface
61 public function process(ServerRequestInterface
$request, RequestHandlerInterface
$handler): ResponseInterface
63 $GLOBALS['TYPO3_REQUEST'] = $request;
64 /** @var Site $site */
65 $site = $request->getAttribute('site', null);
66 $pageArguments = $request->getAttribute('routing', null);
67 if (!$pageArguments instanceof PageArguments
) {
68 // Page Arguments must be set in order to validate. This middleware only works if PageArguments
69 // is available, and is usually combined with the Page Resolver middleware
70 return GeneralUtility
::makeInstance(ErrorController
::class)->pageNotFoundAction(
72 'Page Arguments could not be resolved',
73 ['code' => PageAccessFailureReasons
::INVALID_PAGE_ARGUMENTS
]
76 $frontendUser = $request->getAttribute('frontend.user');
77 if (!$frontendUser instanceof FrontendUserAuthentication
) {
78 throw new \
RuntimeException('The PSR-7 Request attribute "frontend.user" needs to be available as FrontendUserAuthentication object (as created by the FrontendUserAuthenticator middleware).', 1590740612);
81 $controller = GeneralUtility
::makeInstance(
82 TypoScriptFrontendController
::class,
85 $request->getAttribute('language', $site->getDefaultLanguage()),
89 if ($pageArguments->getArguments()['no_cache'] ??
$request->getParsedBody()['no_cache'] ??
false) {
90 $controller->set_no_cache('&no_cache=1 has been supplied, so caching is disabled! URL: "' . (string)$request->getUri() . '"');
92 // Usually only set by the PageArgumentValidator
93 if ($request->getAttribute('noCache', false)) {
94 $controller->no_cache
= 1;
97 $controller->determineId($request);
99 // Make TSFE globally available
100 $GLOBALS['TSFE'] = $controller;
101 return $handler->handle($request);