1C-Bitrix
25.700.0
Загрузка...
Поиск...
Не найдено
sypexgeo.php
См. документацию.
1
<?php
2
3
namespace
Bitrix\Main\Service\GeoIp;
4
5
use Bitrix\Main;
6
use Bitrix\Main\Error;
7
use Bitrix\Main\Web\HttpClient;
8
use Bitrix\Main\Localization\Loc;
9
15
final
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
}
Bitrix\Main\DB\Result
Определения
result.php:20
Bitrix\Main\Error
Определения
error.php:15
Bitrix\Main\ORM\Data\Result
Определения
result.php:16
Bitrix\Main\Service\GeoIp\Base
Определения
base.php:12
Bitrix\Main\Service\GeoIp\ProvidingData
Определения
providingdata.php:12
Bitrix\Main\Service\GeoIp\SypexGeo
Определения
sypexgeo.php:16
Bitrix\Main\Service\GeoIp\SypexGeo\sendRequest
sendRequest($ip, $key)
Определения
sypexgeo.php:38
Bitrix\Main\Service\GeoIp\SypexGeo\getConfigForAdmin
getConfigForAdmin()
Определения
sypexgeo.php:173
Bitrix\Main\Service\GeoIp\SypexGeo\getProvidingData
getProvidingData()
Определения
sypexgeo.php:188
Bitrix\Main\Service\GeoIp\SypexGeo\getDescription
getDescription()
Определения
sypexgeo.php:28
Bitrix\Main\Service\GeoIp\SypexGeo\getSupportedLanguages
getSupportedLanguages()
Определения
sypexgeo.php:105
Bitrix\Main\Service\GeoIp\SypexGeo\getHttpClient
static getHttpClient()
Определения
sypexgeo.php:90
Bitrix\Main\Service\GeoIp\SypexGeo\getTitle
getTitle()
Определения
sypexgeo.php:20
Bitrix\Main\Service\GeoIp\SypexGeo\createConfigField
createConfigField(array $postFields)
Определения
sypexgeo.php:163
Bitrix\Main\Service\GeoIp\SypexGeo\getDataResult
getDataResult($ip, $lang='')
Определения
sypexgeo.php:115
Bitrix\Main\Web\HttpClient
Определения
httpclient.php:24
$data
$data['IS_AVAILABLE']
Определения
.description.php:13
array
</td ></tr ></table ></td ></tr >< tr >< td class="bx-popup-label bx-width30"><?=GetMessage("PAGE_NEW_TAGS")?> array( $site)
Определения
file_new.php:804
$res
$res
Определения
filter_act.php:7
$result
$result
Определения
get_property_values.php:14
$errors
$errors
Определения
iblock_catalog_edit.php:74
$strError
$strError
Определения
options_user_settings.php:4
$lang
if(!defined('SITE_ID')) $lang
Определения
include.php:91
$status
$status
Определения
session.php:10
htmlspecialcharsbx
htmlspecialcharsbx($string, $flags=ENT_COMPAT, $doubleEncode=true)
Определения
tools.php:2701
Bitrix\Main\Data
Определения
aliases.php:105
$key
if(empty($signedUserToken)) $key
Определения
quickway.php:257
$arRes
$arRes
Определения
options.php:104
$url
$url
Определения
iframe.php:7
bitrix
modules
main
lib
service
geoip
sypexgeo.php
Создано системой
1.14.0