f28021cb48c57ddaf43c9730c71f016162cac84a
[Packages/TYPO3.CMS.git] / typo3 / sysext / backend / Classes / Middleware / SiteResolver.php
1 <?php
2 declare(strict_types = 1);
3 namespace TYPO3\CMS\Backend\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\Exception\SiteNotFoundException;
23 use TYPO3\CMS\Core\Site\SiteFinder;
24 use TYPO3\CMS\Core\Utility\GeneralUtility;
25
26 /**
27 * Usually called after the route object is resolved, however, this is not possible yet as this happens
28 * within the RequestHandler/RouteDispatcher right now and should go away.
29 *
30 * This middleware checks for a "id" parameter. If present, it adds a site information to this page ID.
31 *
32 * Very useful for all "Web" related modules to resolve all available languages for a site.
33 */
34 class SiteResolver implements MiddlewareInterface
35 {
36 /**
37 * Resolve the site information by checking the page ID ("id" parameter) which is typically used in BE modules
38 * of type "web".
39 *
40 * @param ServerRequestInterface $request
41 * @param RequestHandlerInterface $handler
42 * @return ResponseInterface
43 */
44 public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
45 {
46 $finder = GeneralUtility::makeInstance(SiteFinder::class);
47 $site = null;
48 $pageId = (int)($request->getQueryParams()['id'] ?? $request->getParsedBody()['id'] ?? 0);
49
50 // Check if we have a _GET/_POST parameter for "id", then a site information can be resolved based.
51 if ($pageId > 0) {
52 try {
53 $site = $finder->getSiteByPageId($pageId);
54 $request = $request->withAttribute('site', $site);
55 $GLOBALS['TYPO3_REQUEST'] = $request;
56 } catch (SiteNotFoundException $e) {
57 }
58 }
59 return $handler->handle($request);
60 }
61 }