1C-Bitrix 25.700.0
Загрузка...
Поиск...
Не найдено
maxmind.php
См. документацию.
1<?php
2
3namespace Bitrix\Main\Service\GeoIp;
4
5use Bitrix\Main;
6use Bitrix\Main\Error;
7use Bitrix\Main\Web\HttpClient;
8use Bitrix\Main\Localization\Loc;
9
15class MaxMind extends Base
16{
20 public function getTitle()
21 {
22 return 'MaxMind';
23 }
24
28 public function getDescription()
29 {
30 return Loc::getMessage('MAIN_SRV_GEOIP_MM_DESCRIPTION');
31 }
32
39 protected function sendRequest($ipAddress, $userId, $licenseKey)
40 {
41 $result = new Main\Result();
42 $httpClient = $this->getHttpClient();
43 $httpClient->setAuthorization($userId, $licenseKey);
44
45 $httpRes = $httpClient->get($this->getEndpoint() . $ipAddress . '?pretty');
46
47 $errors = $httpClient->getError();
48
49 if (!$httpRes && !empty($errors))
50 {
51 $strError = "";
52
53 foreach($errors as $errorCode => $errMes)
54 $strError .= $errorCode.": ".$errMes;
55
56 $result->addError(new Error($strError));
57 }
58 else
59 {
60 $status = $httpClient->getStatus();
61
62 if ($status != 200)
63 $result->addError(new Error('Http status: '.$status));
64
65 $arRes = json_decode($httpRes, true);
66
67 if(is_array($arRes))
68 {
69 if ($status == 200)
70 {
71 $result->setData($arRes);
72 }
73 else
74 {
75 $result->addError(new Error('['.$arRes['code'].'] '.$arRes['error']));
76 }
77 }
78 else
79 {
80 $result->addError(new Error('Can\'t decode json result'));
81 }
82 }
83
84 return $result;
85 }
86
90 protected static function getHttpClient()
91 {
92 return new HttpClient(array(
93 "version" => "1.1",
94 "socketTimeout" => 5,
95 "streamTimeout" => 5,
96 "redirect" => true,
97 "redirectMax" => 5,
98 ));
99 }
100
105 public function getSupportedLanguages()
106 {
107 return array('de', 'en', 'es', 'fr', 'ja', 'pt-BR', 'ru', 'zh-CN');
108 }
109
115 public function getDataResult($ip, $lang = '')
116 {
117 $dataResult = new Result();
118 $geoData = new Data();
119
120 $geoData->lang = ($lang <> '' ? $lang : 'en');
121
122 if($this->config['USER_ID'] == '' || $this->config['LICENSE_KEY'] == '')
123 {
124 $dataResult->addError(new Error(Loc::getMessage('MAIN_SRV_GEOIP_MM_SETT_EMPTY')));
125 return $dataResult;
126 }
127
128 $res = $this->sendRequest($ip, $this->config['USER_ID'], $this->config['LICENSE_KEY']);
129
130 if($res->isSuccess())
131 {
132 $lang = $geoData->lang;
133
134 $data = $res->getData();
135
136 $geoData->ipNetwork = $data['traits']['network'] ?? null;
137
138 $geoData->continentCode = $data['continent']['code'] ?? null;
139 $geoData->continentName = $data['continent']['names'][$lang] ?? $data['continent']['name'] ?? null;
140
141 $geoData->countryCode = $data['country']['iso_code'] ?? null;
142 $geoData->countryName = $data['country']['names'][$lang] ?? $data['country']['name'] ?? null;
143
144 $geoData->regionCode = $data['subdivisions'][0]['iso_code'] ?? null;
145 $geoData->regionGeonameId = $data['subdivisions'][0]['geoname_id'] ?? null;
146 $geoData->regionName = $data['subdivisions'][0]['names'][$lang] ?? $data['subdivisions'][0]['name'] ?? null;
147
148 if ($geoData->regionGeonameId)
149 {
150 $geoData->geonames[$geoData->regionGeonameId] = $res->getData()['subdivisions'][0]['names'] ?? [];
151 }
152
153 $geoData->subRegionCode = $data['subdivisions'][1]['iso_code'] ?? null;
154 $geoData->subRegionGeonameId = $data['subdivisions'][1]['geoname_id'] ?? null;
155 $geoData->subRegionName = $data['subdivisions'][1]['names'][$lang] ?? $data['subdivisions'][1]['name'] ?? null;
156
157 if ($geoData->subRegionGeonameId)
158 {
159 $geoData->geonames[$geoData->subRegionGeonameId] = $res->getData()['subdivisions'][1]['names'] ?? [];
160 }
161
162 $geoData->cityName = $data['city']['names'][$lang] ?? $data['city']['name'] ?? null;
163 $geoData->cityGeonameId = $data['city']['geoname_id'] ?? null;
164
165 if ($geoData->cityGeonameId)
166 {
167 $geoData->geonames[$geoData->cityGeonameId] = $res->getData()['city']['names'] ?? [];
168 }
169
170 $geoData->latitude = $data['location']['latitude'] ?? null;
171 $geoData->longitude = $data['location']['longitude'] ?? null;
172 $geoData->timezone = $data['location']['time_zone'] ?? null;
173
174 $geoData->zipCode = $data['postal']['code'] ?? null;
175
176 $geoData->ispName = $data['traits']['isp'] ?? null;
177 $geoData->organizationName = $data['traits']['organization'] ?? null;
178 $geoData->asn = $data['traits']['autonomous_system_number'] ?? null;
179 $geoData->asnOrganizationName = $data['traits']['autonomous_system_organization'] ?? null;
180 }
181 else
182 {
183 $dataResult->addErrors($res->getErrors());
184 }
185
186 $dataResult->setGeoData($geoData);
187 return $dataResult;
188 }
189
194 public function isInstalled()
195 {
196 return !empty($this->config['USER_ID']) && !empty($this->config['LICENSE_KEY']);
197 }
198
203 public function createConfigField(array $postFields)
204 {
205 return array(
206 'SERVICE' => $postFields['SERVICE'] ?? 'GeoIP2City',
207 'USER_ID' => $postFields['USER_ID'] ?? '',
208 'LICENSE_KEY' => $postFields['LICENSE_KEY'] ?? '',
209 );
210 }
211
215 public function getConfigForAdmin()
216 {
217 return [
218 [
219 'NAME' => 'SERVICE',
220 'TITLE' => Loc::getMessage("main_geoip_mm_service"),
221 'TYPE' => 'LIST',
222 'VALUE' => ($this->config['SERVICE'] ?? 'GeoIP2City'),
223 'OPTIONS' => [
224 'GeoLite2Country' => 'GeoLite2 Country',
225 'GeoLite2City' => 'GeoLite2 City',
226 'GeoIP2Country' => 'GeoIP2 Country',
227 'GeoIP2City' => 'GeoIP2 City',
228 'GeoIP2Insights' => 'GeoIP2 Insights',
229 ],
230 'REQUIRED' => true,
231 ],
232 [
233 'NAME' => 'USER_ID',
234 'TITLE' => Loc::getMessage('MAIN_SRV_GEOIP_MM_F_USER_ID'),
235 'TYPE' => 'TEXT',
236 'VALUE' => htmlspecialcharsbx($this->config['USER_ID']),
237 'REQUIRED' => true,
238 ],
239 [
240 'NAME' => 'LICENSE_KEY',
241 'TITLE' => Loc::getMessage('MAIN_SRV_GEOIP_MM_F_LICENSE_KEY'),
242 'TYPE' => 'TEXT',
243 'VALUE' => htmlspecialcharsbx($this->config['LICENSE_KEY']),
244 'REQUIRED' => true,
245 ]
246 ];
247 }
248
252 public function getProvidingData()
253 {
254 $service = $this->config['SERVICE'] ?? 'GeoIP2City';
255
256 if ($service == 'GeoLite2Country' || $service == 'GeoIP2Country')
257 {
259 }
260
262 }
263
264 protected function getEndpoint(): string
265 {
266 static $endpoints = [
267 'GeoLite2Country' => 'https://geolite.info/geoip/v2.1/country/',
268 'GeoLite2City' => 'https://geolite.info/geoip/v2.1/city/',
269 'GeoIP2Country' => 'https://geoip.maxmind.com/geoip/v2.1/country/',
270 'GeoIP2City' => 'https://geoip.maxmind.com/geoip/v2.1/city/',
271 'GeoIP2Insights' => 'https://geoip.maxmind.com/geoip/v2.1/insights/',
272 ];
273
274 $service = $this->config['SERVICE'] ?? 'GeoIP2City';
275 if (isset($endpoints[$service]))
276 {
277 return $endpoints[$service];
278 }
279 return $endpoints['GeoIP2City'];
280 }
281}
if(!is_object($USER)||! $USER->IsAuthorized()) $userId
Определения check_mail.php:18
Определения result.php:20
Определения error.php:15
sendRequest($ipAddress, $userId, $licenseKey)
Определения maxmind.php:39
static getHttpClient()
Определения maxmind.php:90
createConfigField(array $postFields)
Определения maxmind.php:203
getDataResult($ip, $lang='')
Определения maxmind.php:115
$data['IS_AVAILABLE']
Определения .description.php:13
</td ></tr ></table ></td ></tr >< tr >< td class="bx-popup-label bx-width30"><?=GetMessage("PAGE_NEW_TAGS")?> array( $site)
Определения file_new.php:804
$res
Определения filter_act.php:7
$result
Определения get_property_values.php:14
$errors
Определения iblock_catalog_edit.php:74
$strError
Определения options_user_settings.php:4
if(!defined('SITE_ID')) $lang
Определения include.php:91
$status
Определения session.php:10
htmlspecialcharsbx($string, $flags=ENT_COMPAT, $doubleEncode=true)
Определения tools.php:2701
Определения aliases.php:105
$service
Определения payment.php:18
$arRes
Определения options.php:104