Welcome to mirror list, hosted at ThFree Co, Russian Federation.

SiteUrls.php « SitesManager « plugins - github.com/matomo-org/matomo.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 8880af0972b5cf6984b21f42262cea33f77f46da (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
<?php

/**
 * Matomo - free/libre analytics platform
 *
 * @link https://matomo.org
 * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
 *
 */

namespace Piwik\Plugins\SitesManager;

use Piwik\Cache;
use Piwik\Common;
use Piwik\Tracker\Request;

class SiteUrls
{
    private static $cacheId = 'allSiteUrlsPerSite';

    public static function clearSitesCache()
    {
        self::getCache()->delete(self::$cacheId);
    }

    /**
     * Groups all URLs by host, path and idsite.
     *
     * @param array $urls  An array containing URLs by idsite,
     *                     eg array(array($idSite = 1 => array('apache.piwik', 'apache2.piwik'), 2 => array(), ...))
     *                     as returned by {@link getAllCachedSiteUrls()} and {@link getAllSiteUrls}
     * @return array All urls grouped by host => path => idSites. Path having the most '/' will be listed first
     * array(
         'apache.piwik' => array(
             '/test/two' => $idsite = array(3),
             '/test' => $idsite = array(1),
             '/' => $idsite = array(2),
         ),
         'test.apache.piwik' => array(
             '/test/two' => $idsite = array(3),
             '/test' => $idsite = array(1),
             '/' => $idsite = array(2, 3),
         ),
       );
     */
    public function groupUrlsByHost($siteUrls)
    {
        if (empty($siteUrls)) {
            return [];
        }

        $allUrls = [];

        foreach ($siteUrls as $idSite => $urls) {
            $idSite = (int) $idSite;
            foreach ($urls as $url) {
                $this->addUrlByHost($allUrls, $idSite, $url);
            }
        }

        $this->sortUrlsByHost($allUrls);

        return $allUrls;
    }

    public function addUrlByHost(&$allUrls, $idSite, $url, $addPath = true)
    {
        $urlParsed = @parse_url($url);

        if ($urlParsed === false || !isset($urlParsed['host'])) {
            return;
        }

        $host = $this->toCanonicalHost($urlParsed['host']);
        $path = $this->getCanonicalPathFromParsedUrl($urlParsed);

        if (!isset($allUrls[$host])) {
            $allUrls[$host] = [];
        }

        if (!$addPath) {
            $path = '/';
        }

        if (!isset($allUrls[$host][$path])) {
            $allUrls[$host][$path] = [];
        }

        if (!in_array($idSite, $allUrls[$host][$path])) {
            $allUrls[$host][$path][] = $idSite;
        }
    }

    private function sortUrlsByHost(&$allUrls)
    {
        foreach ($allUrls as $host => $paths) {
            uksort($paths, [$this, 'sortByPathDepth']);
            $allUrls[$host] = $paths;
        }
    }

    public function getIdSitesMatchingUrl($parsedUrl, $urlsGroupedByHost)
    {
        if (empty($parsedUrl['host'])) {
            return null;
        }

        $urlHost = $this->toCanonicalHost($parsedUrl['host']);
        $urlPath = $this->getCanonicalPathFromParsedUrl($parsedUrl);

        // As wildcard subdomain might be allowed, for e.g. my.sub.example.org we need to check e.g.
        // - my.sub.example.org
        // - .my.sub.example.org
        // - .sub.example.org
        // - .example.org
        $hostsToCheck = [
            $urlHost,
            '.' . $urlHost,
        ];

        while (substr_count($urlHost, '.') >= 2) {
            $urlHost = substr($urlHost, strpos($urlHost, '.') + 1);
            $hostsToCheck[] = '.' . $urlHost;
        }

        foreach ($hostsToCheck as $host) {
            if (isset($urlsGroupedByHost[$host])) {
                $paths = $urlsGroupedByHost[$host];

                foreach ($paths as $path => $idSites) {
                    if (0 === strpos($urlPath, $path)) {
                        return $idSites;
                    }
                }

                if (isset($paths['/'])) {
                    return $paths['/'];
                }
            }
        }

        return null;
    }

    public function getPathMatchingUrl($parsedUrl, $urlsGroupedByHost)
    {
        if (empty($parsedUrl['host'])) {
            return null;
        }

        $urlHost = $this->toCanonicalHost($parsedUrl['host']);
        $urlPath = $this->getCanonicalPathFromParsedUrl($parsedUrl);

        if (isset($urlsGroupedByHost[$urlHost])) {
            $paths = $urlsGroupedByHost[$urlHost];

            foreach ($paths as $path => $idSites) {
                if (0 === strpos($urlPath, $path)) {
                    return $path;
                }
            }
        }
    }

    public function getAllCachedSiteUrls()
    {
        $cache    = $this->getCache();
        $siteUrls = $cache->fetch(self::$cacheId);

        if (empty($siteUrls)) {
            $siteUrls = $this->getAllSiteUrls();
            $cache->save(self::$cacheId, $siteUrls, 1800);
        }

        return $siteUrls;
    }

    public function getAllSiteUrls()
    {
        $model    = new Model();
        $siteUrls = $model->getAllKnownUrlsForAllSites();

        if (empty($siteUrls)) {
            return [];
        }

        $urls = [];
        foreach ($siteUrls as $siteUrl) {
            $siteId = (int) $siteUrl['idsite'];

            if (!isset($urls[$siteId])) {
                $urls[$siteId] = [];
            }

            $urls[$siteId][] = $siteUrl['url'];
        }

        return $urls;
    }

    private static function getCache()
    {
        return Cache::getLazyCache();
    }

    public function addRequestUrlToSiteUrls(&$allUrls, Request $request)
    {
        $idSite = $request->getIdSite();
        $url = $request->getParam('url');

        $this->addUrlByHost($allUrls, $idSite, $url, $addPath = false);
    }

    private function sortByPathDepth($pathA, $pathB)
    {
        // list first the paths with most '/' , and list path = '/' last
        $numSlashA = substr_count($pathA, '/');
        $numSlashB = substr_count($pathB, '/');

        if ($numSlashA === $numSlashB) {
            return -1 * strcmp($pathA, $pathB);
        }

        return $numSlashA > $numSlashB ? -1 : 1;
    }

    private function toCanonicalHost($host)
    {
        $host = mb_strtolower($host);
        if (strpos($host, 'www.') === 0) {
            $host = substr($host, 4);
        }

        return $host;
    }

    private function getCanonicalPathFromParsedUrl($urlParsed)
    {
        $path = '/';

        if (isset($urlParsed['path'])) {
            $path = mb_strtolower($urlParsed['path']);
            if (!Common::stringEndsWith($path, '/')) {
                $path .= '/';
            }
        }

        return $path;
    }
}