1C-Bitrix 25.700.0
Загрузка...
Поиск...
Не найдено
sypexgeo.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
15final class SypexGeo extends Base
16{
20 public function getTitle()
21 {
22 return Loc::getMessage('MAIN_SRV_GEOIP_SG_TITLE');
23 }
24
28 public function getDescription()
29 {
30 return Loc::getMessage('MAIN_SRV_GEOIP_SG_DESCRIPTION');
31 }
32
38 protected function sendRequest($ip, $key)
39 {
40 $result = new Main\Result();
41 $httpClient = $this->getHttpClient();
42 $url = 'http://api.sypexgeo.net/';
43
44 if($key <> '')
45 $url .= $key.'/';
46
47 $url .= "json/".$ip;
48
49 $httpRes = $httpClient->get($url);
50 $errors = $httpClient->getError();
51
52 if (!$httpRes && !empty($errors))
53 {
54 $strError = "";
55
56 foreach($errors as $errorCode => $errMes)
57 $strError .= $errorCode.": ".$errMes;
58
59 $result->addError(new Error($strError));
60 }
61 else
62 {
63 $status = $httpClient->getStatus();
64
65 if ($status != 200)
66 {
67 $result->addError(new Error('Sypexgeo.net http status: '.$status));
68 }
69 else
70 {
71 $arRes = json_decode($httpRes, true);
72
73 if(is_array($arRes))
74 {
75 $result->setData($arRes);
76 }
77 else
78 {
79 $result->addError(new Error('Can\'t decode json result'));
80 }
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('en', 'ru');
108 }
109
115 public function getDataResult($ip, $lang = '')
116 {
117 $dataResult = new Result;
118 $geoData = new Data();
119
120 $geoData->lang = $lang = $lang <> '' ? $lang : 'en';
121
122 $key = !empty($this->config['KEY']) ? $this->config['KEY'] : '';
123 $res = $this->sendRequest($ip, $key);
124
125 if($res->isSuccess())
126 {
127 $data = $res->getData();
128
129 $geoData->countryName = $data['country']['name_'.$lang];
130 $geoData->countryCode = $data['country']['iso'];
131 $geoData->regionName = $data['region']['name_'.$lang];
132 $geoData->regionGeonameId = $data['region']['id'];
133 $geoData->regionCode = $data['region']['iso'];
134 $geoData->cityName = $data['city']['name_'.$lang];
135 $geoData->cityGeonameId = $data['city']['id'];
136 $geoData->latitude = $data['city']['lat'];
137 $geoData->longitude = $data['city']['lon'];
138 $geoData->timezone = $data['region']['timezone'];
139
140 if ($geoData->regionGeonameId && $geoData->regionName)
141 {
142 $geoData->geonames[$geoData->regionGeonameId][$lang] = $geoData->regionName;
143 }
144
145 if ($geoData->cityGeonameId && $geoData->cityName)
146 {
147 $geoData->geonames[$geoData->cityGeonameId][$lang] = $geoData->cityName;
148 }
149 }
150 else
151 {
152 $dataResult->addErrors($res->getErrors());
153 }
154
155 $dataResult->setGeoData($geoData);
156 return $dataResult;
157 }
158
163 public function createConfigField(array $postFields)
164 {
165 return array(
166 'KEY' => $postFields['KEY'] ?? ''
167 );
168 }
169
173 public function getConfigForAdmin()
174 {
175 return array(
176 array(
177 'NAME' => 'KEY',
178 'TITLE' => Loc::getMessage('MAIN_SRV_GEOIP_SG_KEY'),
179 'TYPE' => 'TEXT',
180 'VALUE' => htmlspecialcharsbx($this->config['KEY'])
181 )
182 );
183 }
184
188 public function getProvidingData()
189 {
190 $result = new ProvidingData();
191 $result->countryName = true;
192 $result->countryCode = true;
193 $result->regionName = true;
194 $result->regionGeonameId = true;
195 $result->regionCode = true;
196 $result->cityName = true;
197 $result->cityGeonameId = true;
198 $result->latitude = true;
199 $result->longitude = true;
200 $result->timezone = true;
201 return $result;
202 }
203}
Определения result.php:20
Определения error.php:15
sendRequest($ip, $key)
Определения sypexgeo.php:38
static getHttpClient()
Определения sypexgeo.php:90
createConfigField(array $postFields)
Определения sypexgeo.php:163
getDataResult($ip, $lang='')
Определения sypexgeo.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
if(empty($signedUserToken)) $key
Определения quickway.php:257
$arRes
Определения options.php:104
$url
Определения iframe.php:7