‪TYPO3CMS  9.5
ShortcutAndMountPointRedirect.php
Go to the documentation of this file.
1 <?php
2 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 
18 use Psr\Http\Message\ResponseInterface;
19 use Psr\Http\Message\ServerRequestInterface;
20 use Psr\Http\Server\MiddlewareInterface;
21 use Psr\Http\Server\RequestHandlerInterface;
26 
33 class ‪ShortcutAndMountPointRedirect implements MiddlewareInterface
34 {
38  private ‪$controller;
39 
41  {
42  $this->controller = ‪$controller ?: ‪$GLOBALS['TSFE'];
43  }
44 
45  public function ‪process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
46  {
47  // Check for shortcut page and mount point redirect
48  $redirectToUri = $this->‪getRedirectUri($request);
49  if ($redirectToUri !== null && $redirectToUri !== (string)$request->getUri()) {
50  return new ‪RedirectResponse($redirectToUri, 307);
51  }
52 
53  // See if the current page is of doktype "External URL", if so, do a redirect as well.
54  if (empty($this->controller->config['config']['disablePageExternalUrl'] ?? null)
55  && ‪PageRepository::DOKTYPE_LINK === (int)$this->controller->page['doktype']) {
56  $externalUrl = $this->‪prefixExternalPageUrl(
57  $this->controller->page['url'],
58  $request->getAttribute('normalizedParams')->getSiteUrl()
59  );
60  if (!empty($externalUrl)) {
61  return new ‪RedirectResponse($externalUrl, 303);
62  }
63  }
64 
65  return $handler->handle($request);
66  }
67 
68  protected function ‪getRedirectUri(ServerRequestInterface $request): ?string
69  {
70  $redirectToUri = $this->controller->getRedirectUriForShortcut($request);
71  if ($redirectToUri !== null) {
72  return $redirectToUri;
73  }
74  return $this->controller->getRedirectUriForMountPoint($request);
75  }
76 
84  protected function ‪prefixExternalPageUrl(string $redirectTo, string $sitePrefix): string
85  {
86  $uI = parse_url($redirectTo);
87  // If relative path, prefix Site URL
88  // If it's a valid email without protocol, add "mailto:"
89  if (!($uI['scheme'] ?? false)) {
90  if (GeneralUtility::validEmail($redirectTo)) {
91  $redirectTo = 'mailto:' . $redirectTo;
92  } elseif ($redirectTo[0] !== '/') {
93  $redirectTo = $sitePrefix . $redirectTo;
94  }
95  }
96  return $redirectTo;
97  }
98 }
‪TYPO3\CMS\Frontend\Middleware\ShortcutAndMountPointRedirect\prefixExternalPageUrl
‪string prefixExternalPageUrl(string $redirectTo, string $sitePrefix)
Definition: ShortcutAndMountPointRedirect.php:83
‪TYPO3\CMS\Frontend\Middleware\ShortcutAndMountPointRedirect\getRedirectUri
‪getRedirectUri(ServerRequestInterface $request)
Definition: ShortcutAndMountPointRedirect.php:67
‪TYPO3\CMS\Frontend\Page\PageRepository\DOKTYPE_LINK
‪const DOKTYPE_LINK
Definition: PageRepository.php:169
‪TYPO3\CMS\Frontend\Middleware
Definition: BackendUserAuthenticator.php:4
‪TYPO3\CMS\Frontend\Page\PageRepository
Definition: PageRepository.php:53
‪TYPO3\CMS\Frontend\Middleware\ShortcutAndMountPointRedirect\process
‪process(ServerRequestInterface $request, RequestHandlerInterface $handler)
Definition: ShortcutAndMountPointRedirect.php:44
‪TYPO3\CMS\Frontend\Middleware\ShortcutAndMountPointRedirect\$controller
‪TypoScriptFrontendController $controller
Definition: ShortcutAndMountPointRedirect.php:37
‪TYPO3\CMS\Core\Http\RedirectResponse
Definition: RedirectResponse.php:27
‪TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController
Definition: TypoScriptFrontendController.php:97
‪$GLOBALS
‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['adminpanel']['modules']
Definition: ext_localconf.php:5
‪TYPO3\CMS\Frontend\Middleware\ShortcutAndMountPointRedirect\__construct
‪__construct(TypoScriptFrontendController $controller=null)
Definition: ShortcutAndMountPointRedirect.php:39
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:45
‪TYPO3\CMS\Frontend\Middleware\ShortcutAndMountPointRedirect
Definition: ShortcutAndMountPointRedirect.php:34