[TASK] Inject PSR-7 data in TSFE methods
[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\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;
37
38 /**
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.
41 *
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!
44 */
45 class PageResolver implements MiddlewareInterface
46 {
47 /**
48 * @var TypoScriptFrontendController
49 */
50 protected $controller;
51
52 public function __construct(TypoScriptFrontendController $controller = null)
53 {
54 $this->controller = $controller ?? $GLOBALS['TSFE'];
55 }
56
57 /**
58 * Resolve the page ID
59 *
60 * @param ServerRequestInterface $request
61 * @param RequestHandlerInterface $handler
62 * @return ResponseInterface
63 */
64 public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
65 {
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();
69 }
70 $language = $request->getAttribute('language', null);
71
72 $hasSiteConfiguration = $language instanceof SiteLanguage && $site instanceof Site;
73
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);
91 }
92 if ($tail === '/') {
93 $uri = $requestedUri->withPath(rtrim($requestedUri->getPath(), '/'));
94 return new RedirectResponse($uri, 307);
95 }
96 if (!empty($tail)) {
97 // @todo: kick in the resolvers for the RouteEnhancers at this point
98 return GeneralUtility::makeInstance(ErrorController::class)->pageNotFoundAction(
99 $request,
100 'The requested page does not exist',
101 ['code' => PageAccessFailureReasons::PAGE_NOT_FOUND]
102 );
103 }
104 } else {
105 return GeneralUtility::makeInstance(ErrorController::class)->pageNotFoundAction(
106 $request,
107 'The requested page does not exist',
108 ['code' => PageAccessFailureReasons::PAGE_NOT_FOUND]
109 );
110 }
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;
114 }
115 } else {
116 // old-school page resolving for realurl, cooluri etc.
117 $this->controller->siteScript = $request->getAttribute('normalizedParams')->getSiteScript();
118 $this->checkAlternativeIdMethods($this->controller);
119 }
120
121 $this->controller->determineId();
122
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);
130 }
131 $this->controller->determineId();
132 }
133
134 // Evaluate the cache hash parameter
135 $this->controller->makeCacheHash($request);
136
137 return $handler->handle($request);
138 }
139
140 /**
141 * @return PageRouter
142 */
143 protected function getPageRouter(): PageRouter
144 {
145 return GeneralUtility::makeInstance(PageRouter::class);
146 }
147
148 /**
149 * Provides ways to bypass the '?id=[xxx]&type=[xx]' format, using either PATH_INFO or Server Rewrites
150 *
151 * Two options:
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")
154 *
155 * @param TypoScriptFrontendController $tsfe
156 */
157 protected function checkAlternativeIdMethods(TypoScriptFrontendController $tsfe)
158 {
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);
163 }
164 }
165
166 /**
167 * Register the backend user as aspect
168 *
169 * @param Context $context
170 * @param BackendUserAuthentication $user
171 */
172 protected function setBackendUserAspect(Context $context, BackendUserAuthentication $user = null)
173 {
174 $context->setAspect('backend.user', GeneralUtility::makeInstance(UserAspect::class, $user));
175 $context->setAspect('workspace', GeneralUtility::makeInstance(WorkspaceAspect::class, $user ? $user->workspace : 0));
176 }
177 }