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