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
|
<?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\UserCountry\Columns;
use Piwik\Common;
use Piwik\Network\IPUtils;
use Piwik\Plugin\Dimension\VisitDimension;
use Piwik\Plugins\UserCountry\LocationProvider\GeoIp;
use Piwik\Plugins\UserCountry\LocationProvider;
use Piwik\Plugins\PrivacyManager\Config as PrivacyManagerConfig;
use Piwik\Plugins\UserCountry\LocationProvider\DefaultProvider;
use Piwik\Tracker\Visitor;
use Piwik\Tracker\Visit;
use Piwik\Tracker\Request;
abstract class Base extends VisitDimension
{
private static $cachedLocations = array();
protected function getUrlOverrideValueIfAllowed($urlParamToOverride, Request $request)
{
if (!$request->isAuthenticated()) {
return false;
}
$value = Common::getRequestVar($urlParamToOverride, false, 'string', $request->getParams());
if (!empty($value)) {
return $value;
}
return false;
}
public function getRequiredVisitFields()
{
return array('location_ip', 'location_browser_lang');
}
protected function getLocationDetail($userInfo, $locationKey)
{
$location = $this->getCachedLocation($userInfo);
if (!empty($location[$locationKey])) {
return $location[$locationKey];
}
return false;
}
protected function getUserInfo(Request $request, Visitor $visitor)
{
$ipAddress = $this->getIpAddress($visitor->getVisitorColumn('location_ip'), $request);
$language = $visitor->getVisitorColumn('location_browser_lang');
$userInfo = array('lang' => $language, 'ip' => $ipAddress);
return $userInfo;
}
protected function getCachedLocation($userInfo)
{
require_once PIWIK_INCLUDE_PATH . "/plugins/UserCountry/LocationProvider.php";
$key = md5(implode(',', $userInfo));
if (array_key_exists($key, self::$cachedLocations) && empty($GLOBALS['PIWIK_TRACKER_LOCAL_TRACKING'])) {
return self::$cachedLocations[$key];
}
$provider = $this->getProvider();
$location = $this->getLocation($provider, $userInfo);
if (empty($location)) {
$providerId = $provider->getId();
Common::printDebug("GEO: couldn't find a location with Geo Module '$providerId'");
if (!$this->isDefaultProvider($provider)) {
Common::printDebug("Using default provider as fallback...");
$provider = $this->getDefaultProvider();
$location = $this->getLocation($provider, $userInfo);
}
}
if (empty($location)) {
$location = array();
}
if (empty($location['country_code'])) { // sanity check
$location['country_code'] = Visit::UNKNOWN_CODE;
}
self::$cachedLocations[$key] = $location;
return $location;
}
private function getIpAddress($anonymizedIp, \Piwik\Tracker\Request $request)
{
$privacyConfig = new PrivacyManagerConfig();
$ip = $request->getIp();
if ($privacyConfig->useAnonymizedIpForVisitEnrichment) {
$ip = $anonymizedIp;
}
$ipAddress = IPUtils::binaryToStringIP($ip);
return $ipAddress;
}
/**
* @param \Piwik\Plugins\UserCountry\LocationProvider $provider
* @param array $userInfo
* @return array|null
*/
private function getLocation($provider, $userInfo)
{
$location = $provider->getLocation($userInfo);
$providerId = $provider->getId();
$ipAddress = $userInfo['ip'];
if ($location === false) {
return false;
}
Common::printDebug("GEO: Found IP $ipAddress location (provider '" . $providerId . "'): " . var_export($location, true));
return $location;
}
private function getDefaultProvider()
{
$id = DefaultProvider::ID;
$provider = LocationProvider::getProviderById($id);
return $provider;
}
private function isDefaultProvider($provider)
{
return !empty($provider) && DefaultProvider::ID == $provider->getId();
}
private function getProvider()
{
$id = Common::getCurrentLocationProviderId();
$provider = LocationProvider::getProviderById($id);
if ($provider === false) {
$provider = $this->getDefaultProvider();
Common::printDebug("GEO: no current location provider sent, falling back to default '$id' one.");
}
return $provider;
}
}
|