fae88943e65d9726a94ad14f3406c869aa9e9666
[Packages/TYPO3.CMS.git] / typo3 / sysext / frontend / Classes / Middleware / ShortcutAndMountPointRedirect.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\Http\RedirectResponse;
23 use TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController;
24
25 /**
26 * Checks mount points or shortcuts and redirects to the target
27 */
28 class ShortcutAndMountPointRedirect implements MiddlewareInterface
29 {
30 /**
31 * @var TypoScriptFrontendController
32 */
33 private $controller;
34
35 public function __construct(TypoScriptFrontendController $controller = null)
36 {
37 $this->controller = $controller ?: $GLOBALS['TSFE'];
38 }
39
40 public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
41 {
42 // Check for shortcut page and mount point redirect
43 $redirectToUri = $this->getRedirectUri();
44 if ($redirectToUri !== null && $redirectToUri !== (string)$request->getUri()) {
45 return new RedirectResponse($redirectToUri, 307);
46 }
47
48 return $handler->handle($request);
49 }
50
51 protected function getRedirectUri(): ?string
52 {
53 $redirectToUri = $this->controller->getRedirectUriForShortcut();
54 if ($redirectToUri !== null) {
55 return $redirectToUri;
56 }
57 return $this->controller->getRedirectUriForMountPoint();
58 }
59 }