55 return $resultData !==
null ? $resultData->getGeoData()->countryCode :
'';
67 return $resultData !==
null ? $resultData->getGeoData()->countryName :
'';
79 return $resultData !==
null ? $resultData->getGeoData()->cityName :
'';
91 return $resultData !==
null ? $resultData->getGeoData()->zipCode :
'';
106 &&
$data->getGeoData()->latitude !=
null
107 &&
$data->getGeoData()->longitude !=
null
111 'latitude' =>
$data->getGeoData()->latitude,
112 'longitude' =>
$data->getGeoData()->longitude,
132 return $resultData !==
null ? $resultData->getGeoData()->latitude :
'';
144 return $resultData !==
null ? $resultData->getGeoData()->longitude :
'';
156 return $resultData !==
null ? $resultData->getGeoData()->organizationName :
'';
168 return $resultData !==
null ? $resultData->getGeoData()->ispName :
'';
180 return $resultData !==
null ? $resultData->getGeoData()->timezone :
'';
191 public static function getDataResult($ip =
'', $lang =
'', array $required = [])
201 if (isset(self::$data[$ip][$lang]))
203 $data = self::$data[$ip][$lang];
204 if (empty($required) || self::hasDataAllRequiredFields($required,
$data))
212 if(self::$handlers ===
null)
214 self::initHandlers();
217 foreach (self::$handlers as $class => $handler)
219 if (!$handler->isInstalled() || !$handler->isActive())
224 if ($lang !=
'' && !in_array($lang, $handler->getSupportedLanguages()))
229 if (!empty($required) && !self::hasDataAllRequiredFields($required, $handler->getProvidingData()))
238 $data = static::findForIp($ipAddress, $records, $lang);
242 if (empty($required) || self::hasDataAllRequiredFields($required,
$data))
245 self::$data[$ip][$lang] =
$data;
252 $dataResult = $handler->getDataResult($ip, $lang);
259 if (!$dataResult->isSuccess())
261 if (self::$logErrors && (
$logger = static::getLogger()))
264 "{date} - {host}\nIP: {ip}, handler: {handler}, lang: {lang}\n{errors}\n{trace}{delimiter}\n",
268 'handler' => $handler->getId(),
269 'errors' => $dataResult->getErrorMessages(),
270 'trace' => Diag\Helper::getBackTrace(6, DEBUG_BACKTRACE_IGNORE_ARGS, 3),
278 $data = $dataResult->getGeoData();
279 $data->handlerClass = $class;
283 self::$data[$ip][$lang] =
$data;
287 if (Option::get(
'main',
'collect_geonames',
'N') ==
'Y')
289 if (!empty(
$data->geonames))
291 Internal\GeonameTable::save(
$data->geonames);
295 $result = $dataResult;
302 $event =
new Event(
'main',
'onGeoIpGetResult', [
303 'originalData' => clone $result->getGeoData(),
304 'data' => $result->getGeoData(),
314 return $ipAddress->toRange(24) .
':v1:' . $handler;
319 $cacheTtl = static::getCacheTtl();
324 $cacheId = static::getCacheId($ipAddress, $handler);
326 if ($cache->read($cacheTtl, $cacheId, self::CACHE_DIR))
328 $records = $cache->get($cacheId);
330 if (is_array($records))
342 foreach ($records as $range =>
$data)
344 if (isset(
$data[$lang]))
347 if ($ipAddress->matchRange($range))
349 $result =
new Data();
351 foreach (
$data[$lang] as $attr => $value)
353 if (property_exists($result, $attr))
355 $result->$attr = $value;
367 $cacheTtl = static::getCacheTtl();
372 foreach (get_object_vars($geoData) as $attr => $value)
376 $storedData[$attr] = $value;
380 $network = $geoData->ipNetwork ?? $ipAddress->toRange(32);
381 $records[$network][$lang] = $storedData;
387 $cacheId = static::getCacheId($ipAddress, $geoData->handlerClass);
389 $cache->clean($cacheId, self::CACHE_DIR);
390 $cache->read($cacheTtl, $cacheId, self::CACHE_DIR);
391 $cache->set($cacheId, $records);
400 private static function hasDataAllRequiredFields(array $required, $geoData)
407 $vars = get_object_vars($geoData);
409 foreach($required as $field)
411 if($vars[$field] ===
null)
420 private static function initHandlers()
422 if(self::$handlers !==
null)
425 self::$handlers = array();
426 $handlersList = array();
427 $buildInHandlers = array(
428 '\Bitrix\Main\Service\GeoIp\GeoIP2' =>
'lib/service/geoip/geoip2.php',
429 '\Bitrix\Main\Service\GeoIp\MaxMind' =>
'lib/service/geoip/maxmind.php',
430 '\Bitrix\Main\Service\GeoIp\Extension' =>
'lib/service/geoip/extension.php',
431 '\Bitrix\Main\Service\GeoIp\SypexGeo' =>
'lib/service/geoip/sypexgeo.php'
436 $handlersFields = array();
437 $res = HandlerTable::getList([
'cache' => [
'ttl' => static::getCacheTtl()]]);
439 while($row = $res->fetch())
440 $handlersFields[$row[
'CLASS_NAME']] = $row;
442 foreach($buildInHandlers as $class => $file)
444 if(self::isHandlerClassValid($class))
446 $fields = $handlersFields[$class] ?? [];
447 $handlersList[$class] =
new $class($fields);
448 $handlersSort[$class] = $handlersList[$class]->getSort();
452 $event =
new Event(
'main',
'onMainGeoIpHandlersBuildList');
454 $resultList = $event->getResults();
456 if (is_array($resultList) && !empty($resultList))
458 $customClasses = array();
460 foreach ($resultList as $eventResult)
462 if ($eventResult->getType() != EventResult::SUCCESS)
465 $params = $eventResult->getParameters();
467 if(!empty($params) && is_array($params))
468 $customClasses = array_merge($customClasses, $params);
471 if(!empty($customClasses))
475 foreach($customClasses as $class => $file)
482 if(self::isHandlerClassValid($class))
484 $fields = $handlersFields[$class] ?? [];
485 $handlersList[$class] =
new $class($fields);
486 $handlersSort[$class] = $handlersList[$class]->getSort();
492 asort($handlersSort, SORT_NUMERIC);
494 foreach($handlersSort as $class => $sort)
495 self::$handlers[$class] = $handlersList[$class];
502 private static function isHandlerClassValid($className)
504 if(!class_exists($className))
507 if(!is_subclass_of($className,
'\Bitrix\Main\Service\GeoIp\Base'))
521 if (!empty($xForwarded))
523 $ips = explode (
", ", $xForwarded);
524 $fCount = count($ips);
526 for ($i = 0; $i < $fCount; $i++)
528 if (!preg_match(
"/^(10|172\\.16|192\\.168)\\./", $ips[$i]))
550 if(self::$handlers ===
null)
551 self::initHandlers();
562 if(self::$handlers ===
null)
563 self::initHandlers();
565 return self::$handlers[$className] ??
null;
574 self::$logErrors = $isLog;
586 foreach ($adminFields as $field)
588 if ($field[
'TYPE'] ==
'COLSPAN2')
590 $heading = isset($field[
'HEADING']) && $field[
'HEADING'] ?
' class="heading"' :
'';
591 $result .=
'<tr'.$heading.
'><td colspan="2">'.$field[
'TITLE'];
593 elseif ($field[
'TYPE'] ==
'TEXT' || $field[
'TYPE'] ==
'CHECKBOX' || $field[
'TYPE'] ==
'LIST')
595 $required = isset($field[
'REQUIRED']) && $field[
'REQUIRED'] ?
' class="adm-detail-required-field"' :
'';
596 $disabled = isset($field[
'DISABLED']) && $field[
'DISABLED'] ?
' disabled' :
'';
597 $value = isset($field[
'VALUE']) ?
' value="'.$field[
'VALUE'].
'"' :
'';
598 $name = isset($field[
'NAME']) ?
' name="'.$field[
'NAME'].
'"' :
'';
599 $title = isset($field[
'TITLE']) ?
' title="'.$field[
'TITLE'].
'"' :
'';
601 $result .=
'<tr'.$required.
'><td width="40%">'.$field[
'TITLE'].
':</td><td width="60%">';
603 if ($field[
'TYPE'] ==
'TEXT')
605 $result .=
'<input type="text" size="45" maxlength="255"'.$name.$value.$disabled.$title.
'>';
607 elseif ($field[
'TYPE'] ==
'CHECKBOX')
609 $checked = isset($field[
'CHECKED']) && $field[
'CHECKED'] ?
' checked' :
'';
610 $result .=
'<input type="checkbox"'.$name.$value.$checked.$disabled.$title.
'>';
614 $result .=
'<select' . $name . $disabled . $title .
'>';
615 if (is_array($field[
'OPTIONS']))
617 foreach ($field[
'OPTIONS'] as $key => $val)
619 $result .=
'<option value="' . $key .
'"' . ($key == $field[
'VALUE'] ?
' selected' :
'') .
'>' . $val .
'</option>';
622 $result .=
'</select>';
626 $result .=
'</td></tr>';
634 static $cacheTtl =
null;
636 if($cacheTtl ===
null)
638 $cacheFlags = Configuration::getValue(
'cache_flags');
639 $cacheTtl = $cacheFlags[
'geoip_manager'] ?? 604800;
647 $cache->cleanDir(static::CACHE_DIR);
652 if (static::$logger ===
null)
654 $logger = Diag\Logger::create(
'main.GeoIpManager');
662 return static::$logger;
static registerAutoLoadClasses($moduleName, array $classes)
static findForIp(Web\IpAddress $ipAddress, array $records, string $lang)
static getHandlerAdminConfigHtml(Base $handler)
static getOrganizationName($ip='', $lang='')
static getTimezoneName($ip='', $lang='')
static getCountryCode($ip='', $lang='')
static getDataResult($ip='', $lang='', array $required=[])
static getCountryName($ip='', $lang='')
static getGeoPosition($ip='', $lang='')
static getCityName($ip='', $lang='')
static getGeoPositionLatitude($ip='', $lang='')
static saveToStore(Web\IpAddress $ipAddress, array $records, Data $geoData, string $lang)
static getIspName($ip='', $lang='')
static getCacheId(Web\IpAddress $ipAddress, string $handler)
static getGeoPositionLongitude($ip='', $lang='')
static getCityPostCode($ip='', $lang='')
static getHandlerByClassName($className)
static setLogErrors($isLog)
static getFromStore(Web\IpAddress $ipAddress, string $handler)