[TASK] Move domainStartPage setting to PageResolver
[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\Site\Entity\SiteInterface;
27 use TYPO3\CMS\Core\Type\Bitmask\Permission;
28 use TYPO3\CMS\Core\Utility\GeneralUtility;
29 use TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController;
30
31 /**
32 * Process the ID, type and other parameters.
33 * After this point we have an array, TSFE->page, which is the page-record of the current page, $TSFE->id.
34 *
35 * Now, if there is a backend user logged in and he has NO access to this page,
36 * then re-evaluate the id shown!
37 */
38 class PageResolver implements MiddlewareInterface
39 {
40 /**
41 * Resolve the page ID
42 *
43 * @param ServerRequestInterface $request
44 * @param RequestHandlerInterface $handler
45 * @return ResponseInterface
46 */
47 public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
48 {
49 // First, resolve the root page of the site, the Page ID of the current domain
50 if (($site = $request->getAttribute('site', null)) instanceof SiteInterface) {
51 $GLOBALS['TSFE']->domainStartPage = $site->getRootPageId();
52 }
53
54 $GLOBALS['TSFE']->siteScript = $request->getAttribute('normalizedParams')->getSiteScript();
55 $this->checkAlternativeIdMethods($GLOBALS['TSFE']);
56 $GLOBALS['TSFE']->determineId();
57
58 // No access? Then remove user & Re-evaluate the page-id
59 if ($GLOBALS['TSFE']->isBackendUserLoggedIn() && !$GLOBALS['BE_USER']->doesUserHaveAccess($GLOBALS['TSFE']->page, Permission::PAGE_SHOW)) {
60 unset($GLOBALS['BE_USER']);
61 // Register an empty backend user as aspect
62 $this->setBackendUserAspect(GeneralUtility::makeInstance(Context::class), null);
63 $this->checkAlternativeIdMethods($GLOBALS['TSFE']);
64 $GLOBALS['TSFE']->determineId();
65 }
66
67 // Evaluate the cache hash parameter
68 $GLOBALS['TSFE']->makeCacheHash();
69
70 return $handler->handle($request);
71 }
72
73 /**
74 * Provides ways to bypass the '?id=[xxx]&type=[xx]' format, using either PATH_INFO or Server Rewrites
75 *
76 * Two options:
77 * 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)
78 * 2) Using hook which enables features like those provided from "realurl" extension (AKA "Speaking URLs")
79 *
80 * @param TypoScriptFrontendController $tsfe
81 */
82 protected function checkAlternativeIdMethods(TypoScriptFrontendController $tsfe)
83 {
84 // Call post processing function for custom URL methods.
85 $_params = ['pObj' => &$tsfe];
86 foreach ($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['tslib/class.tslib_fe.php']['checkAlternativeIdMethods-PostProc'] ?? [] as $_funcRef) {
87 GeneralUtility::callUserFunction($_funcRef, $_params, $tsfe);
88 }
89 }
90
91 /**
92 * Register the backend user as aspect
93 *
94 * @param Context $context
95 * @param BackendUserAuthentication $user
96 */
97 protected function setBackendUserAspect(Context $context, BackendUserAuthentication $user = null)
98 {
99 $context->setAspect('backend.user', GeneralUtility::makeInstance(UserAspect::class, $user));
100 $context->setAspect('workspace', GeneralUtility::makeInstance(WorkspaceAspect::class, $user ? $user->workspace : 0));
101 }
102 }