Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
sypexgeo.php
1<?php
2
4
10
16final class SypexGeo extends Base
17{
21 public function getTitle()
22 {
23 return Loc::getMessage('MAIN_SRV_GEOIP_SG_TITLE');
24 }
25
29 public function getDescription()
30 {
31 return Loc::getMessage('MAIN_SRV_GEOIP_SG_DESCRIPTION');
32 }
33
39 protected function sendRequest($ip, $key)
40 {
41 $result = new Main\Result();
42 $httpClient = $this->getHttpClient();
43 $url = 'http://api.sypexgeo.net/';
44
45 if($key <> '')
46 $url .= $key.'/';
47
48 $url .= "json/".$ip;
49
50 $httpRes = $httpClient->get($url);
51 $errors = $httpClient->getError();
52
53 if (!$httpRes && !empty($errors))
54 {
55 $strError = "";
56
57 foreach($errors as $errorCode => $errMes)
58 $strError .= $errorCode.": ".$errMes;
59
60 $result->addError(new Error($strError));
61 }
62 else
63 {
64 $status = $httpClient->getStatus();
65
66 if ($status != 200)
67 {
68 $result->addError(new Error('Sypexgeo.net http status: '.$status));
69 }
70 else
71 {
72 $arRes = json_decode($httpRes, true);
73
74 if(is_array($arRes))
75 {
76 if(mb_strtolower(SITE_CHARSET) != 'utf-8')
77 $arRes = Encoding::convertEncoding($arRes, 'UTF-8', SITE_CHARSET);
78
79 $result->setData($arRes);
80 }
81 else
82 {
83 $result->addError(new Error('Can\'t decode json result'));
84 }
85 }
86 }
87
88 return $result;
89 }
90
94 protected static function getHttpClient()
95 {
96 return new HttpClient(array(
97 "version" => "1.1",
98 "socketTimeout" => 5,
99 "streamTimeout" => 5,
100 "redirect" => true,
101 "redirectMax" => 5,
102 ));
103 }
104
109 public function getSupportedLanguages()
110 {
111 return array('en', 'ru');
112 }
113
119 public function getDataResult($ip, $lang = '')
120 {
121 $dataResult = new Result;
122 $geoData = new Data();
123
124 $geoData->lang = $lang = $lang <> '' ? $lang : 'en';
125
126 $key = !empty($this->config['KEY']) ? $this->config['KEY'] : '';
127 $res = $this->sendRequest($ip, $key);
128
129 if($res->isSuccess())
130 {
131 $data = $res->getData();
132
133 $geoData->countryName = $data['country']['name_'.$lang];
134 $geoData->countryCode = $data['country']['iso'];
135 $geoData->regionName = $data['region']['name_'.$lang];
136 $geoData->regionGeonameId = $data['region']['id'];
137 $geoData->regionCode = $data['region']['iso'];
138 $geoData->cityName = $data['city']['name_'.$lang];
139 $geoData->cityGeonameId = $data['city']['id'];
140 $geoData->latitude = $data['city']['lat'];
141 $geoData->longitude = $data['city']['lon'];
142 $geoData->timezone = $data['region']['timezone'];
143
144 if ($geoData->regionGeonameId && $geoData->regionName)
145 {
146 $geoData->geonames[$geoData->regionGeonameId][$lang] = $geoData->regionName;
147 }
148
149 if ($geoData->cityGeonameId && $geoData->cityName)
150 {
151 $geoData->geonames[$geoData->cityGeonameId][$lang] = $geoData->cityName;
152 }
153 }
154 else
155 {
156 $dataResult->addErrors($res->getErrors());
157 }
158
159 $dataResult->setGeoData($geoData);
160 return $dataResult;
161 }
162
167 public function createConfigField(array $postFields)
168 {
169 return array(
170 'KEY' => $postFields['KEY'] ?? ''
171 );
172 }
173
177 public function getConfigForAdmin()
178 {
179 return array(
180 array(
181 'NAME' => 'KEY',
182 'TITLE' => Loc::getMessage('MAIN_SRV_GEOIP_SG_KEY'),
183 'TYPE' => 'TEXT',
184 'VALUE' => htmlspecialcharsbx($this->config['KEY'])
185 )
186 );
187 }
188
192 public function getProvidingData()
193 {
194 $result = new ProvidingData();
195 $result->countryName = true;
196 $result->countryCode = true;
197 $result->regionName = true;
198 $result->regionGeonameId = true;
199 $result->regionCode = true;
200 $result->cityName = true;
201 $result->cityGeonameId = true;
202 $result->latitude = true;
203 $result->longitude = true;
204 $result->timezone = true;
205 return $result;
206 }
207}
static getMessage($code, $replace=null, $language=null)
Definition loc.php:29
createConfigField(array $postFields)
Definition sypexgeo.php:167