‪TYPO3CMS  ‪main
SiteBaseRedirectResolver.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;
32 
41 class ‪SiteBaseRedirectResolver implements MiddlewareInterface
42 {
46  public function ‪process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
47  {
48  $site = $request->getAttribute('site', null);
49  $language = $request->getAttribute('language', null);
50  $routeResult = $request->getAttribute('routing', null);
51 
52  // Usually called when "https://www.example.com" was entered, but all sites have "https://www.example.com/lang-key/"
53  // So a redirect to the first possible language is done.
54  if ($site instanceof ‪Site && !($language instanceof ‪SiteLanguage)) {
55  if ($routeResult instanceof ‪SiteRouteResult && $routeResult->‪getTail() === '') {
56  $language = $site->getDefaultLanguage();
57  if ($language->isEnabled()) {
58  return new ‪RedirectResponse($language->getBase(), 307);
59  }
60  // Default language is disabled, check for the first (enabled) language in list to redirect to that
61  foreach ($site->getLanguages() as $language) {
62  return new ‪RedirectResponse($language->getBase(), 307);
63  }
64  }
65  return GeneralUtility::makeInstance(ErrorController::class)->pageNotFoundAction(
66  $request,
67  'The requested page does not exist',
69  );
70  }
71 
72  // Language is found, and hidden but also not visible to the BE user, this needs to fail
73  if ($language instanceof ‪SiteLanguage && !$this->‪isLanguageEnabled($language, ‪$GLOBALS['BE_USER'] ?? null)) {
74  return GeneralUtility::makeInstance(ErrorController::class)->pageNotFoundAction(
75  $request,
76  'Page is not available in the requested language.',
78  );
79  }
80 
81  if ($language instanceof ‪SiteLanguage && $routeResult instanceof ‪SiteRouteResult) {
82  $requestedUri = $request->getUri();
83  $tail = $routeResult->getTail();
84  // a URL was called via "/fr-FR/" but the page is actually called "/fr-FR", let's do a redirect
85  if ($tail === '/') {
86  $uri = $requestedUri->withPath(rtrim($requestedUri->getPath(), '/'));
87  return new ‪RedirectResponse($uri, 307);
88  }
89  }
90  return $handler->handle($request);
91  }
92 
96  protected function ‪isLanguageEnabled(‪SiteLanguage $language, ?‪BackendUserAuthentication $user = null): bool
97  {
98  // language is hidden, check if a possible backend user is allowed to access the language
99  if ($language->‪enabled() || $user?->checkLanguageAccess($language)) {
100  return true;
101  }
102  return false;
103  }
104 }
‪TYPO3\CMS\Frontend\Middleware\SiteBaseRedirectResolver
Definition: SiteBaseRedirectResolver.php:42
‪TYPO3\CMS\Frontend\Middleware\SiteBaseRedirectResolver\process
‪process(ServerRequestInterface $request, RequestHandlerInterface $handler)
Definition: SiteBaseRedirectResolver.php:46
‪TYPO3\CMS\Core\Site\Entity\SiteLanguage\enabled
‪enabled()
Definition: SiteLanguage.php:249
‪TYPO3\CMS\Frontend\Middleware\SiteBaseRedirectResolver\isLanguageEnabled
‪isLanguageEnabled(SiteLanguage $language, ?BackendUserAuthentication $user=null)
Definition: SiteBaseRedirectResolver.php:96
‪TYPO3\CMS\Frontend\Controller\ErrorController
Definition: ErrorController.php:38
‪TYPO3\CMS\Core\Site\Entity\Site
Definition: Site.php:42
‪TYPO3\CMS\Core\Site\Entity\SiteLanguage
Definition: SiteLanguage.php:27
‪TYPO3\CMS\Frontend\Middleware
Definition: BackendUserAuthenticator.php:18
‪TYPO3\CMS\Core\Routing\SiteRouteResult
Definition: SiteRouteResult.php:30
‪TYPO3\CMS\Core\Routing\SiteRouteResult\getTail
‪getTail()
Definition: SiteRouteResult.php:82
‪TYPO3\CMS\Frontend\Page\PageAccessFailureReasons\LANGUAGE_NOT_AVAILABLE
‪const LANGUAGE_NOT_AVAILABLE
Definition: PageAccessFailureReasons.php:42
‪TYPO3\CMS\Frontend\Page\PageAccessFailureReasons\PAGE_NOT_FOUND
‪const PAGE_NOT_FOUND
Definition: PageAccessFailureReasons.php:28
‪TYPO3\CMS\Core\Authentication\BackendUserAuthentication
Definition: BackendUserAuthentication.php:62
‪TYPO3\CMS\Core\Http\RedirectResponse
Definition: RedirectResponse.php:30
‪$GLOBALS
‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['adminpanel']['modules']
Definition: ext_localconf.php:25
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:52
‪TYPO3\CMS\Frontend\Page\PageAccessFailureReasons
Definition: PageAccessFailureReasons.php:25