[TASK] Deprecate TSFE->makeCacheHash()
[Packages/TYPO3.CMS.git] / typo3 / sysext / frontend / Classes / Middleware / PageResolver.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 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\RouteResult;
28 use TYPO3\CMS\Core\Site\Entity\Site;
29 use TYPO3\CMS\Core\Site\Entity\SiteInterface;
30 use TYPO3\CMS\Core\Site\Entity\SiteLanguage;
31 use TYPO3\CMS\Core\Type\Bitmask\Permission;
32 use TYPO3\CMS\Core\Utility\GeneralUtility;
33 use TYPO3\CMS\Frontend\Controller\ErrorController;
34 use TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController;
35 use TYPO3\CMS\Frontend\Page\PageAccessFailureReasons;
36
37 /**
38 * Process the ID, type and other parameters.
39 * After this point we have an array, TSFE->page, which is the page-record of the current page, $TSFE->id.
40 *
41 * Now, if there is a backend user logged in and he has NO access to this page,
42 * then re-evaluate the id shown!
43 */
44 class PageResolver implements MiddlewareInterface
45 {
46 /**
47 * @var TypoScriptFrontendController
48 */
49 protected $controller;
50
51 public function __construct(TypoScriptFrontendController $controller = null)
52 {
53 $this->controller = $controller ?? $GLOBALS['TSFE'];
54 }
55
56 /**
57 * Resolve the page ID
58 *
59 * @param ServerRequestInterface $request
60 * @param RequestHandlerInterface $handler
61 * @return ResponseInterface
62 */
63 public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
64 {
65 // First, resolve the root page of the site, the Page ID of the current domain
66 if (($site = $request->getAttribute('site', null)) instanceof SiteInterface) {
67 $this->controller->domainStartPage = $site->getRootPageId();
68 }
69 $language = $request->getAttribute('language', null);
70
71 $hasSiteConfiguration = $language instanceof SiteLanguage && $site instanceof Site;
72
73 // Resolve the page ID based on TYPO3's native routing functionality
74 if ($hasSiteConfiguration) {
75 /** @var RouteResult $previousResult */
76 $previousResult = $request->getAttribute('routing', null);
77 if ($previousResult && $previousResult->getTail()) {
78 // Check for the route
79 $routeResult = $site->getRouter()->matchRequest($request, $previousResult);
80 if ($routeResult === null) {
81 return GeneralUtility::makeInstance(ErrorController::class)->pageNotFoundAction(
82 $request,
83 'The requested page does not exist',
84 ['code' => PageAccessFailureReasons::PAGE_NOT_FOUND]
85 );
86 }
87 $request = $request->withAttribute('routing', $routeResult);
88 if (is_array($routeResult['page'])) {
89 $page = $routeResult['page'];
90 $this->controller->id = (int)($page['l10n_parent'] > 0 ? $page['l10n_parent'] : $page['uid']);
91 $tail = $routeResult->getTail();
92 $requestedUri = $request->getUri();
93 // the request was called with "/my-page" but it's actually called "/my-page/", let's do a redirect
94 if ($tail === '' && substr($requestedUri->getPath(), -1) !== substr($page['slug'], -1)) {
95 $uri = $requestedUri->withPath($requestedUri->getPath() . '/');
96 return new RedirectResponse($uri, 307);
97 }
98 if ($tail === '/') {
99 $uri = $requestedUri->withPath(rtrim($requestedUri->getPath(), '/'));
100 return new RedirectResponse($uri, 307);
101 }
102 if (!empty($tail)) {
103 // @todo: kick in the resolvers for the RouteEnhancers at this point
104 return GeneralUtility::makeInstance(ErrorController::class)->pageNotFoundAction(
105 $request,
106 'The requested page does not exist',
107 ['code' => PageAccessFailureReasons::PAGE_NOT_FOUND]
108 );
109 }
110 } else {
111 return GeneralUtility::makeInstance(ErrorController::class)->pageNotFoundAction(
112 $request,
113 'The requested page does not exist',
114 ['code' => PageAccessFailureReasons::PAGE_NOT_FOUND]
115 );
116 }
117 // At this point, we later get further route modifiers
118 // for bw-compat we update $GLOBALS[TYPO3_REQUEST] to be used later in TSFE.
119 $GLOBALS['TYPO3_REQUEST'] = $request;
120 }
121 } else {
122 // old-school page resolving for realurl, cooluri etc.
123 $this->controller->siteScript = $request->getAttribute('normalizedParams')->getSiteScript();
124 if (!empty($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['tslib/class.tslib_fe.php']['checkAlternativeIdMethods-PostProc'])) {
125 trigger_error('The "checkAlternativeIdMethods-PostProc" hook will be removed in TYPO3 v10.0 in favor of PSR-15. Use a middleware instead.', E_USER_DEPRECATED);
126 $this->checkAlternativeIdMethods($this->controller);
127 }
128 }
129
130 $this->controller->determineId();
131
132 // No access? Then remove user & Re-evaluate the page-id
133 if ($this->controller->isBackendUserLoggedIn() && !$GLOBALS['BE_USER']->doesUserHaveAccess($this->controller->page, Permission::PAGE_SHOW)) {
134 unset($GLOBALS['BE_USER']);
135 // Register an empty backend user as aspect
136 $this->setBackendUserAspect(GeneralUtility::makeInstance(Context::class), null);
137 if (!$hasSiteConfiguration) {
138 $this->checkAlternativeIdMethods($this->controller);
139 }
140 $this->controller->determineId();
141 }
142
143 return $handler->handle($request);
144 }
145
146 /**
147 * Provides ways to bypass the '?id=[xxx]&type=[xx]' format, using either PATH_INFO or Server Rewrites
148 *
149 * Two options:
150 * 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)
151 * 2) Using hook which enables features like those provided from "realurl" extension (AKA "Speaking URLs")
152 *
153 * @param TypoScriptFrontendController $tsfe
154 */
155 protected function checkAlternativeIdMethods(TypoScriptFrontendController $tsfe)
156 {
157 // Call post processing function for custom URL methods.
158 $_params = ['pObj' => &$tsfe];
159 foreach ($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['tslib/class.tslib_fe.php']['checkAlternativeIdMethods-PostProc'] ?? [] as $_funcRef) {
160 GeneralUtility::callUserFunction($_funcRef, $_params, $tsfe);
161 }
162 }
163
164 /**
165 * Register the backend user as aspect
166 *
167 * @param Context $context
168 * @param BackendUserAuthentication $user
169 */
170 protected function setBackendUserAspect(Context $context, BackendUserAuthentication $user = null)
171 {
172 $context->setAspect('backend.user', GeneralUtility::makeInstance(UserAspect::class, $user));
173 $context->setAspect('workspace', GeneralUtility::makeInstance(WorkspaceAspect::class, $user ? $user->workspace : 0));
174 }
175 }