-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTrackSiteByUrl.php
104 lines (86 loc) · 2.64 KB
/
TrackSiteByUrl.php
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
<?php
/**
* Piwik - free/libre analytics platform
*
* @link http://piwik.org
* @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
*/
namespace Piwik\Plugins\TrackSiteByUrl;
use Piwik\Container\StaticContainer;
use Psr\Log\LoggerInterface;
use Piwik\Common;
use Piwik\Piwik;
use Piwik\UrlHelper;
use Piwik\Plugins\SitesManager\API as APISitesManager;
class TrackSiteByUrl extends \Piwik\Plugin
{
/*
private $logger;
public function __construct($pluginName=false)
{
parent::__construct($pluginName);
$this->logger = StaticContainer::getContainer()->get('Psr\Log\LoggerInterface');
}
*/
public function getUrlRecursive($url)
{
$recPath = array();
$count = 0;
$parts = explode('/', $url);
for ($i=count($parts); $i>=0; $i--) {
if ($parts) {
$recPath[$count++] = implode('/', $parts);
array_pop($parts);
}
}
return $recPath;
}
public function getListHooksRegistered()
{
//$this->logger->debug("getListHooksRegistered");
return array(
"Tracker.Request.getIdSite" => 'getRequestIdSite'
);
}
public function getRequestIdSite(&$idSite, $params)
{
$url = Common::getRequestVar('url', '', 'string', $_REQUEST);
$res = self::getSiteIdForUrl($url);
if($res)
{
$idSite = $res;
}
}
public function getSiteIdForUrl($url)
{
$id = null;
$urlParse = @parse_url(Common::unsanitizeInputValue($url));
if(isset($urlParse['host']))
{
$isSuperUser = Piwik::hasUserSuperUserAccess();
Piwik::setUserHasSuperUserAccess();
$protocol = '';
if(isset($urlParse['scheme']))
{
$protocol = $urlParse['scheme'];
if(UrlHelper::isLookLikeUrl($url))
{
$protocol .= '://';
}
}
$ret = $this->getUrlRecursive($urlParse["path"]);
$hostUrl = $protocol . $urlParse['host'];
for ($i=0; $i<sizeof($ret); $i++) {
$hostUrl = $protocol. $urlParse['host'] . $ret[$i];
$sites = APISitesManager::getInstance()->getSitesIdFromSiteUrl($hostUrl);
if(count($sites))
{
$id = $sites[0]['idsite'];
break;
}
}
Piwik::setUserHasSuperUserAccess($isSuperUser);
}
return $id;
}
}