Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
maxmind.php
1<?php
2
4
10
16class MaxMind extends Base
17{
21 public function getTitle()
22 {
23 return 'MaxMind';
24 }
25
29 public function getDescription()
30 {
31 return Loc::getMessage('MAIN_SRV_GEOIP_MM_DESCRIPTION');
32 }
33
40 protected function sendRequest($ipAddress, $userId, $licenseKey)
41 {
42 $result = new Main\Result();
43 $httpClient = $this->getHttpClient();
44 $httpClient->setAuthorization($userId, $licenseKey);
45
46 $httpRes = $httpClient->get($this->getEndpoint() . $ipAddress . '?pretty');
47
48 $errors = $httpClient->getError();
49
50 if (!$httpRes && !empty($errors))
51 {
52 $strError = "";
53
54 foreach($errors as $errorCode => $errMes)
55 $strError .= $errorCode.": ".$errMes;
56
57 $result->addError(new Error($strError));
58 }
59 else
60 {
61 $status = $httpClient->getStatus();
62
63 if ($status != 200)
64 $result->addError(new Error('Http status: '.$status));
65
66 $arRes = json_decode($httpRes, true);
67
68 if(is_array($arRes))
69 {
70 if ($status == 200)
71 {
72 $result->setData($arRes);
73 }
74 else
75 {
76 $result->addError(new Error('['.$arRes['code'].'] '.$arRes['error']));
77 }
78 }
79 else
80 {
81 $result->addError(new Error('Can\'t decode json result'));
82 }
83 }
84
85 return $result;
86 }
87
91 protected static function getHttpClient()
92 {
93 return new HttpClient(array(
94 "version" => "1.1",
95 "socketTimeout" => 5,
96 "streamTimeout" => 5,
97 "redirect" => true,
98 "redirectMax" => 5,
99 ));
100 }
101
106 public function getSupportedLanguages()
107 {
108 return array('de', 'en', 'es', 'fr', 'ja', 'pt-BR', 'ru', 'zh-CN');
109 }
110
116 public function getDataResult($ip, $lang = '')
117 {
118 $dataResult = new Result();
119 $geoData = new Data();
120
121 $geoData->lang = ($lang <> '' ? $lang : 'en');
122
123 if($this->config['USER_ID'] == '' || $this->config['LICENSE_KEY'] == '')
124 {
125 $dataResult->addError(new Error(Loc::getMessage('MAIN_SRV_GEOIP_MM_SETT_EMPTY')));
126 return $dataResult;
127 }
128
129 $res = $this->sendRequest($ip, $this->config['USER_ID'], $this->config['LICENSE_KEY']);
130
131 if($res->isSuccess())
132 {
133 $lang = $geoData->lang;
134
135 $data = $res->getData();
136
137 if (strtolower(SITE_CHARSET) != 'utf-8')
138 {
139 $data = Encoding::convertEncoding($data, 'UTF-8', SITE_CHARSET);
140 }
141
142 $geoData->ipNetwork = $data['traits']['network'] ?? null;
143
144 $geoData->continentCode = $data['continent']['code'] ?? null;
145 $geoData->continentName = $data['continent']['names'][$lang] ?? $data['continent']['name'] ?? null;
146
147 $geoData->countryCode = $data['country']['iso_code'] ?? null;
148 $geoData->countryName = $data['country']['names'][$lang] ?? $data['country']['name'] ?? null;
149
150 $geoData->regionCode = $data['subdivisions'][0]['iso_code'] ?? null;
151 $geoData->regionGeonameId = $data['subdivisions'][0]['geoname_id'] ?? null;
152 $geoData->regionName = $data['subdivisions'][0]['names'][$lang] ?? $data['subdivisions'][0]['name'] ?? null;
153
154 if ($geoData->regionGeonameId)
155 {
156 $geoData->geonames[$geoData->regionGeonameId] = $res->getData()['subdivisions'][0]['names'] ?? [];
157 }
158
159 $geoData->subRegionCode = $data['subdivisions'][1]['iso_code'] ?? null;
160 $geoData->subRegionGeonameId = $data['subdivisions'][1]['geoname_id'] ?? null;
161 $geoData->subRegionName = $data['subdivisions'][1]['names'][$lang] ?? $data['subdivisions'][1]['name'] ?? null;
162
163 if ($geoData->subRegionGeonameId)
164 {
165 $geoData->geonames[$geoData->subRegionGeonameId] = $res->getData()['subdivisions'][1]['names'] ?? [];
166 }
167
168 $geoData->cityName = $data['city']['names'][$lang] ?? $data['city']['name'] ?? null;
169 $geoData->cityGeonameId = $data['city']['geoname_id'] ?? null;
170
171 if ($geoData->cityGeonameId)
172 {
173 $geoData->geonames[$geoData->cityGeonameId] = $res->getData()['city']['names'] ?? [];
174 }
175
176 $geoData->latitude = $data['location']['latitude'] ?? null;
177 $geoData->longitude = $data['location']['longitude'] ?? null;
178 $geoData->timezone = $data['location']['time_zone'] ?? null;
179
180 $geoData->zipCode = $data['postal']['code'] ?? null;
181
182 $geoData->ispName = $data['traits']['isp'] ?? null;
183 $geoData->organizationName = $data['traits']['organization'] ?? null;
184 $geoData->asn = $data['traits']['autonomous_system_number'] ?? null;
185 $geoData->asnOrganizationName = $data['traits']['autonomous_system_organization'] ?? null;
186 }
187 else
188 {
189 $dataResult->addErrors($res->getErrors());
190 }
191
192 $dataResult->setGeoData($geoData);
193 return $dataResult;
194 }
195
200 public function isInstalled()
201 {
202 return !empty($this->config['USER_ID']) && !empty($this->config['LICENSE_KEY']);
203 }
204
209 public function createConfigField(array $postFields)
210 {
211 return array(
212 'SERVICE' => $postFields['SERVICE'] ?? 'GeoIP2City',
213 'USER_ID' => $postFields['USER_ID'] ?? '',
214 'LICENSE_KEY' => $postFields['LICENSE_KEY'] ?? '',
215 );
216 }
217
221 public function getConfigForAdmin()
222 {
223 return [
224 [
225 'NAME' => 'SERVICE',
226 'TITLE' => Loc::getMessage("main_geoip_mm_service"),
227 'TYPE' => 'LIST',
228 'VALUE' => ($this->config['SERVICE'] ?? 'GeoIP2City'),
229 'OPTIONS' => [
230 'GeoLite2Country' => 'GeoLite2 Country',
231 'GeoLite2City' => 'GeoLite2 City',
232 'GeoIP2Country' => 'GeoIP2 Country',
233 'GeoIP2City' => 'GeoIP2 City',
234 'GeoIP2Insights' => 'GeoIP2 Insights',
235 ],
236 'REQUIRED' => true,
237 ],
238 [
239 'NAME' => 'USER_ID',
240 'TITLE' => Loc::getMessage('MAIN_SRV_GEOIP_MM_F_USER_ID'),
241 'TYPE' => 'TEXT',
242 'VALUE' => htmlspecialcharsbx($this->config['USER_ID']),
243 'REQUIRED' => true,
244 ],
245 [
246 'NAME' => 'LICENSE_KEY',
247 'TITLE' => Loc::getMessage('MAIN_SRV_GEOIP_MM_F_LICENSE_KEY'),
248 'TYPE' => 'TEXT',
249 'VALUE' => htmlspecialcharsbx($this->config['LICENSE_KEY']),
250 'REQUIRED' => true,
251 ]
252 ];
253 }
254
258 public function getProvidingData()
259 {
260 $service = $this->config['SERVICE'] ?? 'GeoIP2City';
261
262 if ($service == 'GeoLite2Country' || $service == 'GeoIP2Country')
263 {
265 }
266
268 }
269
270 protected function getEndpoint(): string
271 {
272 static $endpoints = [
273 'GeoLite2Country' => 'https://geolite.info/geoip/v2.1/country/',
274 'GeoLite2City' => 'https://geolite.info/geoip/v2.1/city/',
275 'GeoIP2Country' => 'https://geoip.maxmind.com/geoip/v2.1/country/',
276 'GeoIP2City' => 'https://geoip.maxmind.com/geoip/v2.1/city/',
277 'GeoIP2Insights' => 'https://geoip.maxmind.com/geoip/v2.1/insights/',
278 ];
279
280 $service = $this->config['SERVICE'] ?? 'GeoIP2City';
281 if (isset($endpoints[$service]))
282 {
283 return $endpoints[$service];
284 }
285 return $endpoints['GeoIP2City'];
286 }
287}
static getMessage($code, $replace=null, $language=null)
Definition loc.php:29
sendRequest($ipAddress, $userId, $licenseKey)
Definition maxmind.php:40
createConfigField(array $postFields)
Definition maxmind.php:209