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