-
Notifications
You must be signed in to change notification settings - Fork 10
/
IpToCountryRepository.php
175 lines (152 loc) · 5.3 KB
/
IpToCountryRepository.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
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
<?php
/**
* Copyright © Magefan (support@magefan.com). All rights reserved.
* See LICENSE.txt for license details (http://opensource.org/licenses/osl-3.0.php).
*/
declare(strict_types=1);
namespace Magefan\GeoIp\Model;
use Magento\Store\Model\ScopeInterface;
use Magento\Framework\Filesystem\DirectoryList;
use Magento\Framework\Module\Dir as ModuleDir;
use Magento\Framework\App\RequestInterface;
use Magento\Framework\App\Config\ScopeConfigInterface;
use Magento\Framework\HTTP\PhpEnvironment\RemoteAddress;
use Magefan\GeoIp\Api\IpToCountryRepositoryInterface;
class IpToCountryRepository implements IpToCountryRepositoryInterface
{
/**
* Default path in system.xml
*/
const XML_PATH_CLOUDFLARE_ENABLED = 'mfgeoip/cloudflare/cloudflare_ip_enable';
/**
* Allow IPs path in system.xml
*/
const XML_PATH_ALLOW_IPS = 'mfgeoip/developer/allow_ips';
/**
* Simulate country path in system.xml
*/
const XML_PATH_SIMULATE_COUNTRY = 'mfgeoip/developer/simulate_country';
/**
* @var RemoteAddress
*/
protected $remoteAddress;
/**
* @var array
*/
protected $ipToCountry = [];
/**
* @var ScopeConfigInterface
*/
protected $config;
/**
* @var RequestInterface
*/
protected $request;
/**
* @var DirectoryList
*/
private $directoryList;
/**
* @var ModuleDir
*/
private $moduleDir;
/**
* @param RemoteAddress $remoteAddress
* @param DirectoryList $directoryList
* @param ModuleDir $moduleDir
* @param ScopeConfigInterface $config
* @param RequestInterface $httpRequest
*/
public function __construct(
RemoteAddress $remoteAddress,
DirectoryList $directoryList,
ModuleDir $moduleDir,
ScopeConfigInterface $config,
RequestInterface $httpRequest
) {
$this->remoteAddress = $remoteAddress;
$this->directoryList = $directoryList;
$this->moduleDir = $moduleDir;
$this->config = $config;
$this->request = $httpRequest;
}
/**
* Get Country Code by IP
* @param string $ip
* @return mixed
*/
public function getCountryCode($ip)
{
$ip = (string)$ip;
if (!$ip) {
return false;
}
if (!isset($this->ipToCountry[$ip])) {
$this->ipToCountry[$ip] = false;
$simulateCountry = $this->config->getValue(self::XML_PATH_SIMULATE_COUNTRY, ScopeInterface::SCOPE_STORE) ?: '';
if ($simulateCountry) {
$allowedIPs = explode(',', $this->config->getValue(self::XML_PATH_ALLOW_IPS, ScopeInterface::SCOPE_STORE) ?: '');
foreach ($allowedIPs as $allowedIp) {
$allowedIp = trim($allowedIp);
if ($allowedIp && $allowedIp == $ip) {
$this->ipToCountry[$ip] = $simulateCountry;
return $this->ipToCountry[$ip];
}
}
}
$cloudflareEnable = $this->config->getValue(self::XML_PATH_CLOUDFLARE_ENABLED, ScopeInterface::SCOPE_STORE);
if ($cloudflareEnable) {
$countryCode = $this->request->getServer('HTTP_CF_IPCOUNTRY');
if ($countryCode) {
$this->ipToCountry[$ip] = $countryCode;
}
}
if (!$this->ipToCountry[$ip]) {
if (function_exists('geoip_country_code_by_name')) {
$rf = new \ReflectionFunction('geoip_country_code_by_name');
$params = $rf->getParameters();
if (!$params || !is_array($params) || count($params) < 2) { /* Fix for custom geoip php libraries, so 0 or 1 params */
try {
$this->ipToCountry[$ip] = geoip_country_code_by_name($ip);
} catch (\Exception $e) {
//do nothing
}
}
}
}
if (!$this->ipToCountry[$ip]) {
try {
$filename = $this->directoryList->getPath('var') . DIRECTORY_SEPARATOR . 'magefan/geoip/GeoLite2-Country.mmdb';
if (file_exists($filename)) {
$datFile = $filename;
} else {
$datFile = $this->moduleDir->getDir('Magefan_GeoIp') . '/data/GeoLite2-Country.mmdb';
//throw new \Exception('No .mmdb file');
}
$reader = new \GeoIp2\Database\Reader($datFile);
$record = $reader->country($ip);
if ($record && $record->country && $record->country->isoCode) {
$this->ipToCountry[$ip] = $record->country->isoCode;
}
} catch (\Exception $e) {}
}
}
return $this->ipToCountry[$ip];
}
/**
* Retrieve current visitor country code by IP
* @return string | false
*/
public function getVisitorCountryCode()
{
return $this->getCountryCode($this->getRemoteAddress());
}
/**
* Retrieve current IP
* @return string
*/
public function getRemoteAddress()
{
return $this->remoteAddress->getRemoteAddress();
}
}