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 Psr\Http\Message\ResponseInterface
;
19 use Psr\Http\Message\ServerRequestInterface
;
20 use Psr\Http\Server\MiddlewareInterface
;
21 use Psr\Http\Server\RequestHandlerInterface
;
22 use TYPO3\CMS\Core\Authentication\BackendUserAuthentication
;
23 use TYPO3\CMS\Core\Context\Context
;
24 use TYPO3\CMS\Core\Context\UserAspect
;
25 use TYPO3\CMS\Core\Context\WorkspaceAspect
;
26 use TYPO3\CMS\Core\Http\RedirectResponse
;
27 use TYPO3\CMS\Core\Routing\PageRouter
;
28 use TYPO3\CMS\Core\Routing\RouteResult
;
29 use TYPO3\CMS\Core\Site\Entity\Site
;
30 use TYPO3\CMS\Core\Site\Entity\SiteInterface
;
31 use TYPO3\CMS\Core\Site\Entity\SiteLanguage
;
32 use TYPO3\CMS\Core\Type\Bitmask\Permission
;
33 use TYPO3\CMS\Core\Utility\GeneralUtility
;
34 use TYPO3\CMS\Frontend\Controller\ErrorController
;
35 use TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController
;
36 use TYPO3\CMS\Frontend\Page\PageAccessFailureReasons
;
39 * Process the ID, type and other parameters.
40 * After this point we have an array, TSFE->page, which is the page-record of the current page, $TSFE->id.
42 * Now, if there is a backend user logged in and he has NO access to this page,
43 * then re-evaluate the id shown!
45 class PageResolver
implements MiddlewareInterface
48 * @var TypoScriptFrontendController
50 protected $controller;
52 public function __construct(TypoScriptFrontendController
$controller = null)
54 $this->controller
= $controller ??
$GLOBALS['TSFE'];
60 * @param ServerRequestInterface $request
61 * @param RequestHandlerInterface $handler
62 * @return ResponseInterface
64 public function process(ServerRequestInterface
$request, RequestHandlerInterface
$handler): ResponseInterface
66 // First, resolve the root page of the site, the Page ID of the current domain
67 if (($site = $request->getAttribute('site', null)) instanceof SiteInterface
) {
68 $this->controller
->domainStartPage
= $site->getRootPageId();
70 $language = $request->getAttribute('language', null);
72 $hasSiteConfiguration = $language instanceof SiteLanguage
&& $site instanceof Site
;
74 // Resolve the page ID based on TYPO3's native routing functionality
75 if ($hasSiteConfiguration) {
76 /** @var RouteResult $previousResult */
77 $previousResult = $request->getAttribute('routing', new RouteResult($request->getUri(), $site, $language));
78 if (!empty($previousResult->getTail())) {
79 // Check for the route
80 $routeResult = $this->getPageRouter()->matchRoute($request, $previousResult->getTail(), $site, $language);
81 $request = $request->withAttribute('routing', $routeResult);
82 if (is_array($routeResult['page'])) {
83 $page = $routeResult['page'];
84 $this->controller
->id
= (int)($page['l10n_parent'] > 0 ?
$page['l10n_parent'] : $page['uid']);
85 $tail = $routeResult->getTail();
86 $requestedUri = $request->getUri();
87 // the request was called with "/my-page" but it's actually called "/my-page/", let's do a redirect
88 if ($tail === '' && substr($requestedUri->getPath(), -1) !== substr($page['slug'], -1)) {
89 $uri = $requestedUri->withPath($requestedUri->getPath() . '/');
90 return new RedirectResponse($uri, 307);
93 $uri = $requestedUri->withPath(rtrim($requestedUri->getPath(), '/'));
94 return new RedirectResponse($uri, 307);
97 // @todo: kick in the resolvers for the RouteEnhancers at this point
98 return GeneralUtility
::makeInstance(ErrorController
::class)->pageNotFoundAction(
100 'The requested page does not exist',
101 ['code' => PageAccessFailureReasons
::PAGE_NOT_FOUND
]
105 return GeneralUtility
::makeInstance(ErrorController
::class)->pageNotFoundAction(
107 'The requested page does not exist',
108 ['code' => PageAccessFailureReasons
::PAGE_NOT_FOUND
]
111 // At this point, we later get further route modifiers
112 // for bw-compat we update $GLOBALS[TYPO3_REQUEST] to be used later in TSFE.
113 $GLOBALS['TYPO3_REQUEST'] = $request;
116 // old-school page resolving for realurl, cooluri etc.
117 $this->controller
->siteScript
= $request->getAttribute('normalizedParams')->getSiteScript();
118 $this->checkAlternativeIdMethods($this->controller
);
121 $this->controller
->determineId();
123 // No access? Then remove user & Re-evaluate the page-id
124 if ($this->controller
->isBackendUserLoggedIn() && !$GLOBALS['BE_USER']->doesUserHaveAccess($this->controller
->page
, Permission
::PAGE_SHOW
)) {
125 unset($GLOBALS['BE_USER']);
126 // Register an empty backend user as aspect
127 $this->setBackendUserAspect(GeneralUtility
::makeInstance(Context
::class), null);
128 if (!$hasSiteConfiguration) {
129 $this->checkAlternativeIdMethods($this->controller
);
131 $this->controller
->determineId();
134 // Evaluate the cache hash parameter
135 $this->controller
->makeCacheHash();
137 return $handler->handle($request);
143 protected function getPageRouter(): PageRouter
145 return GeneralUtility
::makeInstance(PageRouter
::class);
149 * Provides ways to bypass the '?id=[xxx]&type=[xx]' format, using either PATH_INFO or Server Rewrites
152 * 1) Use PATH_INFO (also Apache) to extract id and type from that var. Does not require any special modules compiled with apache. (less typical)
153 * 2) Using hook which enables features like those provided from "realurl" extension (AKA "Speaking URLs")
155 * @param TypoScriptFrontendController $tsfe
157 protected function checkAlternativeIdMethods(TypoScriptFrontendController
$tsfe)
159 // Call post processing function for custom URL methods.
160 $_params = ['pObj' => &$tsfe];
161 foreach ($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['tslib/class.tslib_fe.php']['checkAlternativeIdMethods-PostProc'] ??
[] as $_funcRef) {
162 GeneralUtility
::callUserFunction($_funcRef, $_params, $tsfe);
167 * Register the backend user as aspect
169 * @param Context $context
170 * @param BackendUserAuthentication $user
172 protected function setBackendUserAspect(Context
$context, BackendUserAuthentication
$user = null)
174 $context->setAspect('backend.user', GeneralUtility
::makeInstance(UserAspect
::class, $user));
175 $context->setAspect('workspace', GeneralUtility
::makeInstance(WorkspaceAspect
::class, $user ?
$user->workspace
: 0));