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
|
|
|
|
18
|
|
|
namespace ApacheSolrForTypo3\Solr\Domain\Site; |
19
|
|
|
|
20
|
|
|
use ApacheSolrForTypo3\Solr\System\Util\SiteUtility; |
21
|
|
|
use Doctrine\DBAL\Driver\Exception as DBALDriverException; |
22
|
|
|
use Throwable; |
23
|
|
|
use TYPO3\CMS\Core\Exception\SiteNotFoundException; |
24
|
|
|
use TYPO3\CMS\Core\Site\SiteFinder; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* SiteHashService |
28
|
|
|
* |
29
|
|
|
* Responsible to provide sitehash related service methods. |
30
|
|
|
* |
31
|
|
|
* @author Timo Hund <[email protected]> |
32
|
|
|
*/ |
33
|
|
|
class SiteHashService |
34
|
|
|
{ |
35
|
|
|
/** |
36
|
|
|
* SiteFinder |
37
|
|
|
*/ |
38
|
|
|
protected SiteFinder $siteFinder; |
39
|
|
|
|
40
|
213 |
|
public function __construct(SiteFinder $siteFinder) |
41
|
|
|
{ |
42
|
213 |
|
$this->siteFinder = $siteFinder; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Resolves magic keywords in allowed sites configuration. |
47
|
|
|
* Supported keywords: |
48
|
|
|
* __solr_current_site - The domain of the site the query has been started from |
49
|
|
|
* __current_site - Same as __solr_current_site |
50
|
|
|
* __all - Adds all domains as allowed sites |
51
|
|
|
* * - Means all sites are allowed, same as no siteHash |
52
|
|
|
* |
53
|
|
|
* @param int $pageId A page ID that is then resolved to the site it belongs to |
54
|
|
|
* @param string|null $allowedSitesConfiguration TypoScript setting for allowed sites |
55
|
|
|
* @return string List of allowed sites/domains, magic keywords resolved |
56
|
|
|
* @throws DBALDriverException |
57
|
|
|
* @throws Throwable |
58
|
|
|
*/ |
59
|
43 |
|
public function getAllowedSitesForPageIdAndAllowedSitesConfiguration( |
60
|
|
|
int $pageId, |
61
|
|
|
?string $allowedSitesConfiguration = '' |
62
|
|
|
): string { |
63
|
43 |
|
if ($allowedSitesConfiguration === '__all') { |
64
|
2 |
|
return $this->getDomainListOfAllSites(); |
65
|
|
|
} |
66
|
41 |
|
if ($allowedSitesConfiguration === '*') { |
67
|
2 |
|
return '*'; |
68
|
|
|
} |
69
|
|
|
// we thread empty allowed site configurations as __solr_current_site since this is the default behaviour |
70
|
39 |
|
$allowedSitesConfiguration = empty($allowedSitesConfiguration) ? '__solr_current_site' : $allowedSitesConfiguration; |
71
|
39 |
|
return $this->getDomainByPageIdAndReplaceMarkers($pageId, $allowedSitesConfiguration); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Gets the site hash for a domain |
76
|
|
|
* |
77
|
|
|
* @param string $domain Domain to calculate the site hash for. |
78
|
|
|
* @return string site hash for $domain |
79
|
|
|
*/ |
80
|
198 |
|
public function getSiteHashForDomain(string $domain): string |
81
|
|
|
{ |
82
|
198 |
|
static $siteHashes = []; |
83
|
198 |
|
if (isset($siteHashes[$domain])) { |
84
|
114 |
|
return $siteHashes[$domain]; |
85
|
|
|
} |
86
|
|
|
|
87
|
198 |
|
$siteHashes[$domain] = sha1($domain . $GLOBALS['TYPO3_CONF_VARS']['SYS']['encryptionKey'] . 'tx_solr'); |
88
|
198 |
|
return $siteHashes[$domain]; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Returns a comma separated list of all domains from all sites. |
93
|
|
|
* |
94
|
|
|
* @return string |
95
|
|
|
* @throws DBALDriverException |
96
|
|
|
* @throws Throwable |
97
|
|
|
*/ |
98
|
2 |
|
protected function getDomainListOfAllSites(): string |
99
|
|
|
{ |
100
|
2 |
|
$sites = $this->siteFinder->getAllSites(); |
101
|
2 |
|
$domains = []; |
102
|
2 |
|
foreach ($sites as $typo3Site) { |
103
|
2 |
|
$connections = SiteUtility::getAllSolrConnectionConfigurations($typo3Site); |
104
|
2 |
|
if (!empty($connections)) { |
105
|
2 |
|
$domains[] = $typo3Site->getBase()->getHost(); |
106
|
|
|
} |
107
|
|
|
} |
108
|
|
|
|
109
|
2 |
|
return implode(',', $domains); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* Retrieves the domain of the site that belongs to the passed pageId and replaces their markers __solr_current_site |
114
|
|
|
* and __current_site. |
115
|
|
|
* |
116
|
|
|
* @param int $pageId |
117
|
|
|
* @param string $allowedSitesConfiguration |
118
|
|
|
* @return string |
119
|
|
|
*/ |
120
|
39 |
|
protected function getDomainByPageIdAndReplaceMarkers(int $pageId, string $allowedSitesConfiguration): string |
121
|
|
|
{ |
122
|
|
|
try { |
123
|
39 |
|
$typo3Site = $this->siteFinder->getSiteByPageId($pageId); |
124
|
39 |
|
$domainOfPage = $typo3Site->getBase()->getHost(); |
125
|
|
|
} catch (SiteNotFoundException $e) { |
126
|
|
|
return ''; |
127
|
|
|
} |
128
|
|
|
|
129
|
39 |
|
$allowedSites = str_replace(['__solr_current_site', '__current_site'], $domainOfPage, $allowedSitesConfiguration); |
130
|
39 |
|
return (string)$allowedSites; |
131
|
|
|
} |
132
|
|
|
} |
133
|
|
|
|