[BUGFIX] Require doctrine/lexer to get removed lexer classes
[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\Core\Utility\GeneralUtility;
24 use TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController;
25
26 /**
27 * Checks mount points, shortcuts and redirects to the target.
28 * Alternatively, checks if the current page is an redirect to an external page
29 */
30 class ShortcutAndMountPointRedirect implements MiddlewareInterface
31 {
32 /**
33 * @var TypoScriptFrontendController
34 */
35 private $controller;
36
37 public function __construct(TypoScriptFrontendController $controller = null)
38 {
39 $this->controller = $controller ?: $GLOBALS['TSFE'];
40 }
41
42 public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
43 {
44 // Check for shortcut page and mount point redirect
45 $redirectToUri = $this->getRedirectUri();
46 if ($redirectToUri !== null && $redirectToUri !== (string)$request->getUri()) {
47 return new RedirectResponse($redirectToUri, 307);
48 }
49
50 // See if the current page is of doktype "External URL", if so, do a redirect as well.
51 if (empty($this->controller->config['config']['disablePageExternalUrl'] ?? null)
52 && $this->controller->sys_page::DOKTYPE_LINK === (int)$this->controller->page['doktype']) {
53 $externalUrl = $this->prefixExternalPageUrl(
54 $this->controller->page['url'],
55 $request->getAttribute('normalizedParams')->getSiteUrl()
56 );
57 if (!empty($externalUrl)) {
58 return new RedirectResponse($externalUrl, 303);
59 }
60 }
61
62 return $handler->handle($request);
63 }
64
65 protected function getRedirectUri(): ?string
66 {
67 $redirectToUri = $this->controller->getRedirectUriForShortcut();
68 if ($redirectToUri !== null) {
69 return $redirectToUri;
70 }
71 return $this->controller->getRedirectUriForMountPoint();
72 }
73
74 /**
75 * Returns the redirect URL for the input page row IF the doktype is set to 3.
76 *
77 * @param string $redirectTo The page row to return URL type for
78 * @param string $sitePrefix if no protocol or relative path given, the site prefix is added
79 * @return string The URL from based on the external page URL given with a prefix.
80 */
81 protected function prefixExternalPageUrl(string $redirectTo, string $sitePrefix): string
82 {
83 $uI = parse_url($redirectTo);
84 // If relative path, prefix Site URL
85 // If it's a valid email without protocol, add "mailto:"
86 if (!($uI['scheme'] ?? false)) {
87 if (GeneralUtility::validEmail($redirectTo)) {
88 $redirectTo = 'mailto:' . $redirectTo;
89 } elseif ($redirectTo[0] !== '/') {
90 $redirectTo = $sitePrefix . $redirectTo;
91 }
92 }
93 return $redirectTo;
94 }
95 }