-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathBlockedGeoIp.php
79 lines (64 loc) · 2.27 KB
/
BlockedGeoIp.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
<?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\TrackingSpamPrevention;
use Piwik\Plugins\UserCountry\LocationProvider;
use Piwik\Plugins\UserCountry\VisitorGeolocator;
class BlockedGeoIp
{
/**
* @var array
*/
private $blockedProviders;
public function __construct($blockedProviders = [])
{
$this->blockedProviders = $blockedProviders;
}
public function detectLocation($ip, $language)
{
// we need to use both IP and language so it will use the same cache key as in core!
$visitorLocator = new VisitorGeolocator();
$info = array('lang' => $language, 'ip' => $ip);
return $visitorLocator->getLocation($info, $useClassCache = true);
}
public function isExcludedCountry($ip, $language, $excludedCountries, $includedCountries)
{
if (empty($excludedCountries) && empty($includedCountries)) {
return false;
}
$result = $this->detectLocation($ip, $language);
if (empty($result[LocationProvider::COUNTRY_CODE_KEY])) {
return false;
}
$countryCode = strtolower($result[LocationProvider::COUNTRY_CODE_KEY]);
if (!empty($includedCountries) && in_array($countryCode, $includedCountries, true)) {
return false;
} elseif (!empty($includedCountries)) {
return true;
}
if (!empty($excludedCountries) && in_array($countryCode, $excludedCountries, true)) {
return true;
}
return false;
}
public function isExcludedProvider($ip, $language)
{
if (empty($this->blockedProviders) || !(\Piwik\Plugin\Manager::getInstance()->isPluginActivated('UserCountry'))) {
return false;
}
$result = $this->detectLocation($ip, $language);
if (!empty($result[LocationProvider::ORG_KEY])) {
$org = $result[LocationProvider::ORG_KEY];
foreach ($this->blockedProviders as $blockedProvider) {
if (!empty($blockedProvider) && stripos($org, $blockedProvider) !== false) {
return true;
}
}
}
return false;
}
}