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