1C-Bitrix 25.700.0
Загрузка...
Поиск...
Не найдено
geoip2.php
См. документацию.
1<?php
2
3namespace Bitrix\Main\Service\GeoIp;
4
5use Bitrix\Main\Error;
6use Bitrix\Main\Localization\Loc;
7use GeoIp2\Exception;
8use GeoIp2\Database;
9use MaxMind\Db\Reader;
10
16class GeoIP2 extends Base
17{
19 protected static $reader;
20
24 public function getTitle()
25 {
26 return 'GeoIP2';
27 }
28
32 public function getDescription()
33 {
34 return Loc::getMessage("geoip_geoip2_desc");
35 }
36
41 public function getSupportedLanguages()
42 {
43 return ['de', 'en', 'es', 'fr', 'ja', 'pt-BR', 'ru', 'zh-CN'];
44 }
45
51 public function getDataResult($ip, $lang = '')
52 {
53 $dataResult = $this->initReader();
54 if (!$dataResult->isSuccess())
55 {
56 return $dataResult;
57 }
58
59 $geoData = new Data();
60 $geoData->lang = ($lang != '' ? $lang : 'en');
61
62 try
63 {
64 $type = $this->config['TYPE'] ?? 'city';
65
66 if ($type == 'city')
67 {
68 $record = static::$reader->city($ip);
69 }
70 else
71 {
72 $record = static::$reader->country($ip);
73 }
74
75 $geoData->ipNetwork = $record->traits->network;
76
77 $geoData->continentCode = $record->continent->code;
78 $geoData->continentName = ($record->continent->names[$geoData->lang] ?? $record->continent->name);
79
80 $geoData->countryCode = $record->country->isoCode;
81 $geoData->countryName = ($record->country->names[$geoData->lang] ?? $record->country->name);
82
83 if ($record instanceof \GeoIp2\Model\City)
84 {
85 if (isset($record->subdivisions[0]))
86 {
88 $subdivision = $record->subdivisions[0];
89 $geoData->regionCode = $subdivision->isoCode;
90 $geoData->regionName = ($subdivision->names[$geoData->lang] ?? $subdivision->name);
91 $geoData->regionGeonameId = $subdivision->geonameId;
92
93 if ($subdivision->geonameId)
94 {
95 $geoData->geonames[$subdivision->geonameId] = $subdivision->names;
96 }
97 }
98
99 if (isset($record->subdivisions[1]))
100 {
102 $subdivision = $record->subdivisions[1];
103 $geoData->subRegionCode = $subdivision->isoCode;
104 $geoData->subRegionName = ($subdivision->names[$geoData->lang] ?? $subdivision->name);
105 $geoData->subRegionGeonameId = $subdivision->geonameId;
106
107 if ($subdivision->geonameId)
108 {
109 $geoData->geonames[$subdivision->geonameId] = $subdivision->names;
110 }
111 }
112
113 $geoData->cityName = ($record->city->names[$geoData->lang] ?? $record->city->name);
114 $geoData->cityGeonameId = $record->city->geonameId;
115
116 if ($record->city->geonameId)
117 {
118 $geoData->geonames[$record->city->geonameId] = $record->city->names;
119 }
120
121 $geoData->latitude = $record->location->latitude;
122 $geoData->longitude = $record->location->longitude;
123 $geoData->timezone = $record->location->timeZone;
124
125 $geoData->zipCode = $record->postal->code;
126
127 $geoData->ispName = $record->traits->isp;
128 $geoData->organizationName = $record->traits->organization;
129 $geoData->asn = $record->traits->autonomousSystemNumber;
130 $geoData->asnOrganizationName = $record->traits->autonomousSystemOrganization;
131 }
132
133 $dataResult->setGeoData($geoData);
134 }
135 catch(Exception\AddressNotFoundException)
136 {
137 // is it an error?
138 }
139 catch(Reader\InvalidDatabaseException)
140 {
141 $dataResult->addError(new Error(Loc::getMessage("geoip_geoip2_err_reading")));
142 }
143
144 return $dataResult;
145 }
146
151 public function isInstalled()
152 {
153 return (isset($this->config['FILE']) && $this->config['FILE'] !== '' && file_exists($this->config['FILE']));
154 }
155
160 public function createConfigField(array $postFields)
161 {
162 return [
163 'TYPE' => $postFields['TYPE'] ?? 'city',
164 'FILE' => $postFields['FILE'] ?? '',
165 ];
166 }
167
171 public function getConfigForAdmin()
172 {
173 return [
174 [
175 'NAME' => 'TYPE',
176 'TITLE' => Loc::getMessage("geoip_geoip2_type"),
177 'TYPE' => 'LIST',
178 'VALUE' => ($this->config['TYPE'] ?? 'city'),
179 'OPTIONS' => [
180 'city' => 'GeoIP2/GeoLite2 City',
181 'country' => 'GeoIP2/GeoLite2 Country',
182 ],
183 'REQUIRED' => true,
184 ],
185 [
186 'NAME' => 'FILE',
187 'TITLE' => Loc::getMessage("geoip_geoip2_file"),
188 'TYPE' => 'TEXT',
189 'VALUE' => htmlspecialcharsbx($this->config['FILE'] ?? ''),
190 'REQUIRED' => true,
191 ],
192 ];
193 }
194
198 public function getProvidingData()
199 {
200 $type = $this->config['TYPE'] ?? 'city';
201
202 if ($type == 'city')
203 {
205 }
207 }
208
212 protected function initReader(): Result
213 {
214 $dataResult = new Result();
215
216 if (static::$reader === null)
217 {
218 if ($this->config['FILE'] == '')
219 {
220 $dataResult->addError(new Error(Loc::getMessage("geoip_geoip2_no_file")));
221 return $dataResult;
222 }
223
224 if (!file_exists($this->config['FILE']))
225 {
226 $dataResult->addError(new Error(Loc::getMessage("geoip_geoip2_file_not_found")));
227 return $dataResult;
228 }
229
230 try
231 {
232 static::$reader = new Database\Reader($this->config['FILE']);
233 }
234 catch(Reader\InvalidDatabaseException)
235 {
236 $dataResult->addError(new Error(Loc::getMessage("geoip_geoip2_err_reading")));
237 }
238 }
239
240 return $dataResult;
241 }
242}
$type
Определения options.php:106
Определения result.php:20
Определения error.php:15
static $reader
Определения geoip2.php:19
getSupportedLanguages()
Определения geoip2.php:41
createConfigField(array $postFields)
Определения geoip2.php:160
</td ></tr ></table ></td ></tr >< tr >< td class="bx-popup-label bx-width30"><?=GetMessage("PAGE_NEW_TAGS")?> array( $site)
Определения file_new.php:804
if(!defined('SITE_ID')) $lang
Определения include.php:91
htmlspecialcharsbx($string, $flags=ENT_COMPAT, $doubleEncode=true)
Определения tools.php:2701
Определения aliases.php:105
trait Error
Определения error.php:11