-
Notifications
You must be signed in to change notification settings - Fork 0
/
DaData.php
169 lines (146 loc) · 4.75 KB
/
DaData.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
<?php
use Bitrix\Main\{Error, Result};
use Bitrix\Main\Text\Encoding;
use Bitrix\Main\Web\HttpClient;
use Bitrix\Main\Service\GeoIp\{Base, Data, ProvidingData};
final class DaData extends Base
{
/**
* @return string Title of handler.
*/
public function getTitle()
{
return "DaData";
}
/**
* @return string Handler description.
*/
public function getDescription()
{
return "Определяет город по IP-адресу в России";
}
/**
* @param string $ip Ip address
* @param string $key license key.
* @return Result
*/
protected function sendRequest(string $ip, string $key)
{
$result = new Result();
$httpClient = $this->getHttpClient();
$httpClient->setHeader("Accept", "application/json");
$httpClient->setHeader("Authorization", "Token $key");
$url = "https://suggestions.dadata.ru/suggestions/api/4_1/rs/iplocate/address?ip=$ip";
$httpRes = $httpClient->get($url);
$errors = $httpClient->getError();
if (!$httpRes && !empty($errors)) {
$strError = "";
foreach ($errors as $errorCode => $errMes) {
$strError .= "$errorCode: $errMes";
}
$result->addError(new Error($strError));
} else {
$status = $httpClient->getStatus();
if ($status != 200) {
$result->addError(new Error("Dadata http status: $status"));
} else {
$arRes = json_decode($httpRes, true);
if (is_array($arRes)) {
if (mb_strtolower(SITE_CHARSET) != "utf-8") {
$arRes = Encoding::convertEncoding($arRes, "UTF-8", SITE_CHARSET);
}
$result->setData($arRes);
} else {
$result->addError(new Error("Can't decode json result"));
}
}
}
return $result;
}
/**
* @return HttpClient
*/
protected static function getHttpClient()
{
return new HttpClient([
"version" => "1.1",
"socketTimeout" => 5,
"streamTimeout" => 5,
"redirect" => true,
"redirectMax" => 5,
]);
}
/**
* Languages supported by handler ISO 639-1
* @return array
*/
public function getSupportedLanguages()
{
return ["ru"];
}
/**
* @param string $ip Ip address
* @param string $lang Language identifier
* @return \Bitrix\Main\Service\GeoIp\Result | null
*/
public function getDataResult($ip, $lang = "")
{
$dataResult = new \Bitrix\Main\Service\GeoIp\Result;
$geoData = new Data();
$geoData->ip = $ip;
$geoData->lang = $lang = $lang != "" ? $lang : "en";
$key = !empty($this->config["TOKEN"]) ? $this->config["TOKEN"] : "";
$res = $this->sendRequest($ip, $key);
if ($res->isSuccess()) {
$data = $res->getData();
$geoData->countryName = $data["location"]["data"]["country"];
$geoData->countryCode = $data["location"]["data"]["country_iso_code"];
$geoData->regionName = $data["location"]["data"]["region"];
$geoData->regionCode = $data["location"]["data"]["region_iso_code"];
$geoData->cityName = $data["location"]["data"]["city"];
$geoData->latitude = $data["location"]["data"]["geo_lat"];
$geoData->longitude = $data["location"]["data"]["geo_lon"];
} else {
$dataResult->addErrors($res->getErrors());
}
$dataResult->setGeoData($geoData);
return $dataResult;
}
/**
* @param array $postFields $_POST
* @return array Field CONFIG for saving to DB in admin edit form.
*/
public function createConfigField($postFields)
{
return ["TOKEN" => $postFields["TOKEN"] ?? ""];
}
/**
* @return array Set of fields description for administration purposes.
*/
public function getConfigForAdmin()
{
return [
[
"NAME" => "TOKEN",
"TITLE" => "Токен",
"TYPE" => "TEXT",
"VALUE" => htmlspecialcharsbx($this->config["TOKEN"])
]
];
}
/**
* @return ProvidingData Geolocation information witch handler can return.
*/
public function getProvidingData()
{
$result = new ProvidingData();
$result->countryName = true;
$result->countryCode = true;
$result->regionName = true;
$result->regionCode = true;
$result->cityName = true;
$result->latitude = true;
$result->longitude = true;
return $result;
}
}