‪TYPO3CMS  10.4
ShortcutAndMountPointRedirect.php
Go to the documentation of this file.
1 <?php
2 
3 declare(strict_types=1);
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 
19 
20 use Psr\Http\Message\ResponseInterface;
21 use Psr\Http\Message\ServerRequestInterface;
22 use Psr\Http\Server\MiddlewareInterface;
23 use Psr\Http\Server\RequestHandlerInterface;
28 
35 class ‪ShortcutAndMountPointRedirect implements MiddlewareInterface
36 {
40  private ‪$controller;
41 
43  {
44  $this->controller = ‪$controller;
45  }
46 
47  public function ‪process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
48  {
49  // Check for shortcut page and mount point redirect
50  $redirectToUri = $this->‪getRedirectUri($request);
51  if ($redirectToUri !== null && $redirectToUri !== (string)$request->getUri()) {
53  return new ‪RedirectResponse($redirectToUri, 307);
54  }
55 
56  // See if the current page is of doktype "External URL", if so, do a redirect as well.
57  if (empty($this->controller->config['config']['disablePageExternalUrl'] ?? null)
58  && ‪PageRepository::DOKTYPE_LINK === (int)$this->controller->page['doktype']) {
59  $externalUrl = $this->‪prefixExternalPageUrl(
60  $this->controller->page['url'],
61  $request->getAttribute('normalizedParams')->getSiteUrl()
62  );
63  if (!empty($externalUrl)) {
65  return new ‪RedirectResponse($externalUrl, 303);
66  }
67  }
68 
69  return $handler->handle($request);
70  }
71 
72  protected function ‪getRedirectUri(ServerRequestInterface $request): ?string
73  {
74  $redirectToUri = $this->controller->getRedirectUriForShortcut($request);
75  if ($redirectToUri !== null) {
76  return $redirectToUri;
77  }
78  return $this->controller->getRedirectUriForMountPoint($request);
79  }
80 
88  protected function ‪prefixExternalPageUrl(string $redirectTo, string $sitePrefix): string
89  {
90  $uI = parse_url($redirectTo);
91  // If relative path, prefix Site URL
92  // If it's a valid email without protocol, add "mailto:"
93  if (!($uI['scheme'] ?? false)) {
94  if (GeneralUtility::validEmail($redirectTo)) {
95  $redirectTo = 'mailto:' . $redirectTo;
96  } elseif ($redirectTo[0] !== '/') {
97  $redirectTo = $sitePrefix . $redirectTo;
98  }
99  }
100  return $redirectTo;
101  }
102 
110  protected function ‪releaseTypoScriptFrontendControllerLocks(): void
111  {
112  $this->controller->releaseLocks();
113  }
114 }
‪TYPO3\CMS\Frontend\Middleware\ShortcutAndMountPointRedirect\prefixExternalPageUrl
‪string prefixExternalPageUrl(string $redirectTo, string $sitePrefix)
Definition: ShortcutAndMountPointRedirect.php:87
‪TYPO3\CMS\Frontend\Middleware\ShortcutAndMountPointRedirect\getRedirectUri
‪getRedirectUri(ServerRequestInterface $request)
Definition: ShortcutAndMountPointRedirect.php:71
‪TYPO3\CMS\Frontend\Middleware\ShortcutAndMountPointRedirect\__construct
‪__construct(TypoScriptFrontendController $controller)
Definition: ShortcutAndMountPointRedirect.php:41
‪TYPO3\CMS\Core\Domain\Repository\PageRepository\DOKTYPE_LINK
‪const DOKTYPE_LINK
Definition: PageRepository.php:104
‪TYPO3\CMS\Frontend\Middleware
Definition: BackendUserAuthenticator.php:18
‪TYPO3\CMS\Frontend\Middleware\ShortcutAndMountPointRedirect\process
‪process(ServerRequestInterface $request, RequestHandlerInterface $handler)
Definition: ShortcutAndMountPointRedirect.php:46
‪TYPO3\CMS\Frontend\Middleware\ShortcutAndMountPointRedirect\releaseTypoScriptFrontendControllerLocks
‪releaseTypoScriptFrontendControllerLocks()
Definition: ShortcutAndMountPointRedirect.php:109
‪TYPO3\CMS\Frontend\Middleware\ShortcutAndMountPointRedirect\$controller
‪TypoScriptFrontendController $controller
Definition: ShortcutAndMountPointRedirect.php:39
‪TYPO3\CMS\Core\Http\RedirectResponse
Definition: RedirectResponse.php:28
‪TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController
Definition: TypoScriptFrontendController.php:98
‪TYPO3\CMS\Core\Domain\Repository\PageRepository
Definition: PageRepository.php:52
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:46
‪TYPO3\CMS\Frontend\Middleware\ShortcutAndMountPointRedirect
Definition: ShortcutAndMountPointRedirect.php:36