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