‪TYPO3CMS  9.5
LegacyDomainResolver.php
Go to the documentation of this file.
1 <?php
2 declare(strict_types = 1);
3 
5 
6 /*
7  * This file is part of the TYPO3 CMS project.
8  *
9  * It is free software; you can redistribute it and/or modify it under
10  * the terms of the GNU General Public License, either version 2
11  * of the License, or any later version.
12  *
13  * For the full copyright and license information, please read the
14  * LICENSE.txt file that was distributed with this source code.
15  *
16  * The TYPO3 project - inspiring people to share!
17  */
18 
19 use Psr\Http\Message\ServerRequestInterface;
28 
37 {
43  protected ‪$domainDataCache = [];
44 
48  protected ‪$cacheIdentifier = 'legacy-domains';
49 
53  protected ‪$cache;
54 
59  protected ‪$groupedDomainsPerPage;
60 
61  public function ‪__construct()
62  {
63  $this->cache = GeneralUtility::makeInstance(CacheManager::class)->getCache('cache_core');
64  $this->‪populate();
65  }
66 
70  protected function ‪populate()
71  {
72  if ($data = $this->cache->get($this->cacheIdentifier)) {
73  // Due to the nature of PhpFrontend, the `<?php` and `#` wraps have to be removed
74  $data = preg_replace('/^<\?php\s*|\s*#$/', '', $data);
75  $this->groupedDomainsPerPage = json_decode($data, true);
76  } else {
77  $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable('sys_domain');
78  $statement = $queryBuilder
79  ->select('*')
80  ->from('sys_domain')
81  ->orderBy('sorting', 'ASC')
82  ->execute();
83 
84  while ($row = $statement->fetch()) {
85  $row['domainName'] = rtrim($row['domainName'], '/');
86  $this->groupedDomainsPerPage[(int)$row['pid']][] = $row;
87  }
88 
89  $this->cache->set($this->cacheIdentifier, json_encode($this->groupedDomainsPerPage));
90  }
91  }
92 
96  public function ‪getGroupedDomainsPerPage(): array
97  {
98  return $this->groupedDomainsPerPage ?? [];
99  }
100 
114  public function ‪matchPageId(int $pageId, ServerRequestInterface $currentRequest = null): ?array
115  {
116  // Using array_key_exists() here, nice $result can be NULL
117  // (happens, if there's no domain records defined)
118  if (array_key_exists($pageId, $this->domainDataCache)) {
119  return $this->domainDataCache[$pageId];
120  }
121  try {
122  $this->domainDataCache[$pageId] = $this->‪resolveDomainEntry(
123  $pageId,
124  $currentRequest
125  );
126  } catch (RootLineException $e) {
127  $this->domainDataCache[$pageId] = null;
128  }
129  return $this->domainDataCache[$pageId];
130  }
131 
139  public function ‪matchRootPageId(int $pageId): ?array
140  {
141  return !empty($this->groupedDomainsPerPage[$pageId]) ? reset($this->groupedDomainsPerPage[$pageId]) : null;
142  }
143 
149  protected function ‪resolveDomainEntry(int $pageId, ?ServerRequestInterface $currentRequest): ?array
150  {
151  $rootLine = GeneralUtility::makeInstance(RootlineUtility::class, $pageId)->get();
152  // walk the rootline downwards from the target page
153  // to the root page, until a domain record is found
154  foreach ($rootLine as $pageInRootline) {
155  $pidInRootline = (int)$pageInRootline['uid'];
156  if (empty($this->groupedDomainsPerPage[$pidInRootline])) {
157  continue;
158  }
159 
160  $domainEntriesOfPage = $this->groupedDomainsPerPage[$pidInRootline];
161  foreach ($domainEntriesOfPage as $domainEntry) {
162  if ($domainEntry['hidden']) {
163  continue;
164  }
165  // When no currentRequest is given, let's take the first non-hidden sys_domain page
166  if ($currentRequest === null) {
167  return $domainEntry;
168  }
169  // Otherwise the check should match against the current domain (and set "isCurrentDomain")
170  // Current domain is "forced", however, otherwise the first one is fine
171  if ($this->‪domainNameMatchesCurrentRequest($domainEntry['domainName'], $currentRequest)) {
172  $result = $domainEntry;
173  $result['isCurrentDomain'] = true;
174  return $result;
175  }
176  }
177  }
178  return null;
179  }
180 
189  protected function ‪domainNameMatchesCurrentRequest($domainName, ServerRequestInterface $request): bool
190  {
192  $normalizedParams = $request->getAttribute('normalizedParams');
193  if (!($normalizedParams instanceof NormalizedParams)) {
194  return false;
195  }
196  $currentDomain = $normalizedParams->getHttpHost();
197  // remove the script filename from the path segment.
198  $currentPathSegment = trim(preg_replace('|/[^/]*$|', '', $normalizedParams->getScriptName()));
199  return $currentDomain === $domainName || $currentDomain . $currentPathSegment === $domainName;
200  }
201 }
‪TYPO3\CMS\Frontend\Compatibility\LegacyDomainResolver\resolveDomainEntry
‪array null resolveDomainEntry(int $pageId, ?ServerRequestInterface $currentRequest)
Definition: LegacyDomainResolver.php:145
‪TYPO3\CMS\Frontend\Compatibility\LegacyDomainResolver\$domainDataCache
‪array $domainDataCache
Definition: LegacyDomainResolver.php:42
‪TYPO3\CMS\Frontend\Compatibility\LegacyDomainResolver\domainNameMatchesCurrentRequest
‪bool domainNameMatchesCurrentRequest($domainName, ServerRequestInterface $request)
Definition: LegacyDomainResolver.php:185
‪TYPO3\CMS\Core\Utility\RootlineUtility
Definition: RootlineUtility.php:36
‪TYPO3\CMS\Frontend\Compatibility\LegacyDomainResolver
Definition: LegacyDomainResolver.php:37
‪TYPO3\CMS\Frontend\Compatibility
Definition: LegacyDomainResolver.php:4
‪TYPO3\CMS\Core\Cache\CacheManager
Definition: CacheManager.php:34
‪TYPO3\CMS\Frontend\Compatibility\LegacyDomainResolver\$groupedDomainsPerPage
‪array $groupedDomainsPerPage
Definition: LegacyDomainResolver.php:55
‪TYPO3\CMS\Frontend\Compatibility\LegacyDomainResolver\getGroupedDomainsPerPage
‪array getGroupedDomainsPerPage()
Definition: LegacyDomainResolver.php:92
‪TYPO3\CMS\Core\Cache\Frontend\FrontendInterface
Definition: FrontendInterface.php:21
‪TYPO3\CMS\Frontend\Compatibility\LegacyDomainResolver\matchRootPageId
‪array null matchRootPageId(int $pageId)
Definition: LegacyDomainResolver.php:135
‪TYPO3\CMS\Frontend\Compatibility\LegacyDomainResolver\$cache
‪FrontendInterface $cache
Definition: LegacyDomainResolver.php:50
‪TYPO3\CMS\Core\SingletonInterface
Definition: SingletonInterface.php:22
‪TYPO3\CMS\Frontend\Compatibility\LegacyDomainResolver\__construct
‪__construct()
Definition: LegacyDomainResolver.php:57
‪TYPO3\CMS\Core\Exception\Page\RootLineException
Definition: RootLineException.php:24
‪TYPO3\CMS\Frontend\Compatibility\LegacyDomainResolver\$cacheIdentifier
‪string $cacheIdentifier
Definition: LegacyDomainResolver.php:46
‪TYPO3\CMS\Core\Database\ConnectionPool
Definition: ConnectionPool.php:44
‪TYPO3\CMS\Frontend\Compatibility\LegacyDomainResolver\populate
‪populate()
Definition: LegacyDomainResolver.php:66
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:45
‪TYPO3\CMS\Core\Http\NormalizedParams
Definition: NormalizedParams.php:32
‪TYPO3\CMS\Frontend\Compatibility\LegacyDomainResolver\matchPageId
‪array null matchPageId(int $pageId, ServerRequestInterface $currentRequest=null)
Definition: LegacyDomainResolver.php:110