Completed
Push — master ( 9517e4...034cdd )
by Timo
44s
created

SiteHashService::getAvailableSites()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
namespace ApacheSolrForTypo3\Solr\Domain\Site;
4
5
/***************************************************************
6
 *  Copyright notice
7
 *
8
 *  (c) 2017- Timo Hund <[email protected]>
9
 *  All rights reserved
10
 *
11
 *  This script is part of the TYPO3 project. The TYPO3 project is
12
 *  free software; you can redistribute it and/or modify
13
 *  it under the terms of the GNU General Public License as published by
14
 *  the Free Software Foundation; either version 2 of the License, or
15
 *  (at your option) any later version.
16
 *
17
 *  The GNU General Public License can be found at
18
 *  http://www.gnu.org/copyleft/gpl.html.
19
 *
20
 *  This script is distributed in the hope that it will be useful,
21
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
22
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
23
 *  GNU General Public License for more details.
24
 *
25
 *  This copyright notice MUST APPEAR in all copies of the script!
26
 ***************************************************************/
27
28
use ApacheSolrForTypo3\Solr\Site;
29
30
/**
31
 * SiteHashService
32
 *
33
 * Responsible to provide sitehash related service methods.
34
 *
35
 * @author Timo Hund <[email protected]>
36
 */
37
class SiteHashService
38
{
39
40
    /**
41
     * Resolves magic keywords in allowed sites configuration.
42
     * Supported keywords:
43
     *   __solr_current_site - The domain of the site the query has been started from
44
     *   __current_site - Same as __solr_current_site
45
     *   __all - Adds all domains as allowed sites
46
     *   * - Means all sites are allowed, same as no siteHash
47
     *
48
     * @param integer $pageId A page ID that is then resolved to the site it belongs to
49
     * @param string $allowedSitesConfiguration TypoScript setting for allowed sites
50
     * @return string List of allowed sites/domains, magic keywords resolved
51
     */
52
    public function getAllowedSitesForPageIdAndAllowedSitesConfiguration($pageId, $allowedSitesConfiguration)
53
    {
54
        if ($allowedSitesConfiguration === '__all') {
55
            return  $this->getDomainListOfAllSites();
56
        } elseif ($allowedSitesConfiguration === '*') {
57
            return '*';
58
        } else {
59
            return $this->getDomainByPageIdAndReplaceMarkers($pageId, $allowedSitesConfiguration);
60
        }
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
     */
69
    public function getSiteHashForDomain($domain)
70
    {
71
        static $siteHashes = [];
72
        if (isset($siteHashes[$domain])) {
73
            return $siteHashes[$domain];
74
        }
75
76
        $siteHashes[$domain] = sha1($domain . $GLOBALS['TYPO3_CONF_VARS']['SYS']['encryptionKey'] . 'tx_solr');
77
        return $siteHashes[$domain];
78
    }
79
80
81
    /**
82
     * Returns a comma separated list of all domains from all sites.
83
     *
84
     * @return string
85
     */
86
    protected function getDomainListOfAllSites()
87
    {
88
        $sites = $this->getAvailableSites();
89
        $domains = [];
90
        foreach ($sites as $site) {
91
            $domains[] = $site->getDomain();
92
        }
93
94
        $allowedSites = implode(',', $domains);
95
        return $allowedSites;
96
    }
97
98
    /**
99
     * Retrieves the domain of the site that belongs to the passed pageId and replaces their markers __solr_current_site
100
     * and __current_site.
101
     *
102
     * @param integer $pageId
103
     * @param string $allowedSitesConfiguration
104
     * @return string
105
     */
106
    protected function getDomainByPageIdAndReplaceMarkers($pageId, $allowedSitesConfiguration)
107
    {
108
        $domainOfPage = $this->getSiteByPageId($pageId)->getDomain();
109
        $allowedSites = str_replace(['__solr_current_site', '__current_site'], $domainOfPage, $allowedSitesConfiguration);
110
        return (string)$allowedSites;
111
    }
112
113
    /**
114
     * @return Site[]
115
     */
116
    protected function getAvailableSites()
117
    {
118
        return Site::getAvailableSites();
119
    }
120
121
    /**
122
     * @param $pageId
123
     * @return Site
124
     */
125
    protected function getSiteByPageId($pageId)
126
    {
127
        return Site::getSiteByPageId($pageId);
128
    }
129
}
130