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