[TASK] Move domainStartPage setting to PageResolver
[Packages/TYPO3.CMS.git] / typo3 / sysext / frontend / Classes / Middleware / SiteResolver.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\Routing\SiteMatcher;
24 use TYPO3\CMS\Core\Site\Entity\PseudoSite;
25 use TYPO3\CMS\Core\Site\Entity\Site;
26 use TYPO3\CMS\Core\Site\Entity\SiteLanguage;
27 use TYPO3\CMS\Core\Site\SiteFinder;
28 use TYPO3\CMS\Core\Utility\GeneralUtility;
29 use TYPO3\CMS\Frontend\Controller\ErrorController;
30 use TYPO3\CMS\Frontend\Page\PageAccessFailureReasons;
31
32 /**
33 * Identifies if a site is configured for the request, based on "id" and "L" GET/POST parameters, or the requested
34 * string.
35 *
36 * If a site is found, the request is populated with the found language+site objects. If none is found, the main magic
37 * is handled by the PageResolver middleware.
38 *
39 * In addition to that, TSFE gets the $domainStartPage information resolved and added.
40 */
41 class SiteResolver implements MiddlewareInterface
42 {
43 /**
44 * @var SiteMatcher
45 */
46 protected $matcher;
47
48 public function __construct(SiteMatcher $matcher = null)
49 {
50 $this->matcher = $matcher ?? GeneralUtility::makeInstance(
51 SiteMatcher::class,
52 GeneralUtility::makeInstance(SiteFinder::class)
53 );
54 }
55
56 /**
57 * Resolve the site/language information by checking the page ID or the URL.
58 *
59 * @param ServerRequestInterface $request
60 * @param RequestHandlerInterface $handler
61 * @return ResponseInterface
62 */
63 public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
64 {
65 $routeResult = $this->matcher->matchRequest($request);
66 $site = $routeResult['site'] ?? null;
67 $language = $routeResult['language'] ?? null;
68
69 // language is found, and hidden but also not visible to the BE user, this needs to fail
70 if ($language instanceof SiteLanguage && !$this->isLanguageEnabled($language, $GLOBALS['BE_USER'] ?? null)) {
71 $request = $request->withAttribute('site', $site);
72 return GeneralUtility::makeInstance(ErrorController::class)->pageNotFoundAction(
73 $request,
74 'Page is not available in the requested language.',
75 ['code' => PageAccessFailureReasons::LANGUAGE_NOT_AVAILABLE]
76 );
77 }
78
79 // Add language+site information to the PSR-7 request object.
80 if ($language instanceof SiteLanguage && $site instanceof Site) {
81 $request = $request->withAttribute('site', $site);
82 $request = $request->withAttribute('language', $language);
83 $queryParams = $request->getQueryParams();
84 // necessary to calculate the proper hash base
85 $queryParams['L'] = $language->getLanguageId();
86 $request = $request->withQueryParams($queryParams);
87 $_GET['L'] = $queryParams['L'];
88 // At this point, we later get further route modifiers
89 // for bw-compat we update $GLOBALS[TYPO3_REQUEST] to be used later in TSFE.
90 $GLOBALS['TYPO3_REQUEST'] = $request;
91 } elseif ($site instanceof PseudoSite) {
92 $request = $request->withAttribute('site', $site);
93 // At this point, we later get further route modifiers
94 // for bw-compat we update $GLOBALS[TYPO3_REQUEST] to be used later in TSFE.
95 $GLOBALS['TYPO3_REQUEST'] = $request;
96 }
97
98 return $handler->handle($request);
99 }
100
101 /**
102 * Checks if the language is allowed in Frontend, if not, check if there is valid BE user
103 *
104 * @param SiteLanguage|null $language
105 * @param BackendUserAuthentication|null $user
106 * @return bool
107 */
108 protected function isLanguageEnabled(SiteLanguage $language, BackendUserAuthentication $user = null): bool
109 {
110 // language is hidden, check if a possible backend user is allowed to access the language
111 if ($language->enabled() || ($user instanceof BackendUserAuthentication && $user->checkLanguageAccess($language->getLanguageId()))) {
112 return true;
113 }
114 return false;
115 }
116 }