10namespace Bitrix\Main\Service\GeoIp;
12use Bitrix\Main\Application;
13use Bitrix\Main\Context;
15use Bitrix\Main\IO\File;
16use Bitrix\Main\Loader;
17use Bitrix\Main\EventResult;
18use Bitrix\Main\Config\Configuration;
19use Bitrix\Main\Config\Option;
20use Bitrix\Main\Web\IpAddress;
52 return $resultData !==
null ? $resultData->getGeoData()->countryCode :
'';
64 return $resultData !==
null ? $resultData->getGeoData()->countryName :
'';
76 return $resultData !==
null ? $resultData->getGeoData()->cityName :
'';
88 return $resultData !==
null ? $resultData->getGeoData()->zipCode :
'';
103 &&
$data->getGeoData()->latitude !=
null
104 &&
$data->getGeoData()->longitude !=
null
108 'latitude' =>
$data->getGeoData()->latitude,
109 'longitude' =>
$data->getGeoData()->longitude,
129 return $resultData !==
null ? $resultData->getGeoData()->latitude :
'';
141 return $resultData !==
null ? $resultData->getGeoData()->longitude :
'';
153 return $resultData !==
null ? $resultData->getGeoData()->organizationName :
'';
165 return $resultData !==
null ? $resultData->getGeoData()->ispName :
'';
177 return $resultData !==
null ? $resultData->getGeoData()->timezone :
'';
198 if (isset(self::$data[$ip][
$lang]))
201 if (empty($required) || self::hasDataAllRequiredFields($required,
$data))
209 if (self::$handlers ===
null)
211 self::initHandlers();
214 foreach (self::$handlers as $class => $handler)
216 if (!$handler->isInstalled() || !$handler->isActive())
221 if (
$lang !=
'' && !in_array(
$lang, $handler->getSupportedLanguages()))
226 if (!empty($required) && !self::hasDataAllRequiredFields($required, $handler->getProvidingData()))
235 $data = static::findForIp($ipAddress, $records,
$lang);
239 if (empty($required) || self::hasDataAllRequiredFields($required,
$data))
249 $dataResult = $handler->getDataResult($ip,
$lang);
256 if (!$dataResult->isSuccess())
258 if (self::$logErrors && (
$logger = static::getLogger()))
261 "{date} - {host}\nIP: {ip}, handler: {handler}, lang: {lang}\n{errors}\n{trace}{delimiter}\n",
265 'handler' => $handler->getId(),
266 'errors' => $dataResult->getErrorMessages(),
267 'trace' =>
Diag\Helper::getBackTrace(6, DEBUG_BACKTRACE_IGNORE_ARGS, 3),
275 $data = $dataResult->getGeoData();
276 $data->handlerClass = $class;
284 if (Option::get(
'main',
'collect_geonames',
'N') ==
'Y')
286 if (!empty(
$data->geonames))
300 'originalData' => clone
$result->getGeoData(),
301 'data' =>
$result->getGeoData(),
311 return $ipAddress->
toRange(24) .
':v1:' . $handler;
316 if (!$ipAddress->
isIPv4())
321 $cacheTtl = static::getCacheTtl();
326 $cacheId = static::getCacheId($ipAddress, $handler);
328 if ($cache->read($cacheTtl, $cacheId, self::CACHE_DIR))
330 $records = $cache->get($cacheId);
332 if (is_array($records))
344 foreach ($records as $range =>
$data)
355 if (property_exists(
$result, $attr))
369 if (!$ipAddress->
isIPv4())
374 $cacheTtl = static::getCacheTtl();
378 $storedData = array_filter(get_object_vars($geoData),
function ($value) {
379 return $value !==
null;
382 $network = $geoData->ipNetwork ?? $ipAddress->
toRange(32);
389 $cacheId = static::getCacheId($ipAddress, $geoData->handlerClass);
391 $cache->clean($cacheId, self::CACHE_DIR);
392 $cache->read($cacheTtl, $cacheId, self::CACHE_DIR);
393 $cache->set($cacheId, $records);
402 private static function hasDataAllRequiredFields(
array $required, $geoData)
404 if (empty($required))
409 $vars = get_object_vars($geoData);
411 foreach ($required as $field)
413 if ($vars[$field] ===
null)
422 private static function initHandlers()
424 if (self::$handlers !==
null)
429 self::$handlers = [];
432 '\Bitrix\Main\Service\GeoIp\GeoIP2' =>
'lib/service/geoip/geoip2.php',
433 '\Bitrix\Main\Service\GeoIp\MaxMind' =>
'lib/service/geoip/maxmind.php',
434 '\Bitrix\Main\Service\GeoIp\Extension' =>
'lib/service/geoip/extension.php',
435 '\Bitrix\Main\Service\GeoIp\SypexGeo' =>
'lib/service/geoip/sypexgeo.php',
440 $handlersFields = [];
443 while ($row =
$res->fetch())
445 $handlersFields[$row[
'CLASS_NAME']] = $row;
448 foreach ($buildInHandlers as $class => $file)
450 if (self::isHandlerClassValid($class))
452 $fields = $handlersFields[$class] ?? [];
453 $handlersList[$class] =
new $class(
$fields);
454 $handlersSort[$class] = $handlersList[$class]->getSort();
458 $event =
new Event(
'main',
'onMainGeoIpHandlersBuildList');
460 $resultList =
$event->getResults();
462 if (is_array($resultList) && !empty($resultList))
466 foreach ($resultList as $eventResult)
468 if ($eventResult->getType() != EventResult::SUCCESS)
473 $params = $eventResult->getParameters();
477 $customClasses = array_merge($customClasses,
$params);
481 if (!empty($customClasses))
485 foreach ($customClasses as $class => $file)
492 if (self::isHandlerClassValid($class))
494 $fields = $handlersFields[$class] ?? [];
495 $handlersList[$class] =
new $class(
$fields);
496 $handlersSort[$class] = $handlersList[$class]->getSort();
502 asort($handlersSort, SORT_NUMERIC);
504 foreach ($handlersSort as $class => $sort)
506 self::$handlers[$class] = $handlersList[$class];
514 private static function isHandlerClassValid($className)
516 if (!class_exists($className))
521 if (!is_subclass_of($className,
'\Bitrix\Main\Service\GeoIp\Base'))
535 $xForwarded =
$context->getServer()->get(
'HTTP_X_FORWARDED_FOR');
537 if (!empty($xForwarded))
539 $ips = explode(
", ", $xForwarded);
541 foreach ($ips as $forwarded)
543 $ipAddress =
new IPAddress($forwarded);
544 if ($ipAddress->isIPv4() && !$ipAddress->isPrivate())
546 return (
string)$ipAddress;
551 return trim(
$context->getRequest()->getRemoteAddress());
560 if (self::$handlers ===
null)
562 self::initHandlers();
565 return self::$handlers;
574 if (self::$handlers ===
null)
576 self::initHandlers();
579 return self::$handlers[$className] ??
null;
588 self::$logErrors = $isLog;
600 foreach ($adminFields as $field)
602 if ($field[
'TYPE'] ==
'COLSPAN2')
604 $heading = isset($field[
'HEADING']) && $field[
'HEADING'] ?
' class="heading"' :
'';
605 $result .=
'<tr' . $heading .
'><td colspan="2">' . $field[
'TITLE'];
607 elseif ($field[
'TYPE'] ==
'TEXT' || $field[
'TYPE'] ==
'CHECKBOX' || $field[
'TYPE'] ==
'LIST')
609 $required = isset($field[
'REQUIRED']) && $field[
'REQUIRED'] ?
' class="adm-detail-required-field"' :
'';
610 $disabled = isset($field[
'DISABLED']) && $field[
'DISABLED'] ?
' disabled' :
'';
611 $value = isset($field[
'VALUE']) ?
' value="' . $field[
'VALUE'] .
'"' :
'';
612 $name = isset($field[
'NAME']) ?
' name="' . $field[
'NAME'] .
'"' :
'';
613 $title = isset($field[
'TITLE']) ?
' title="' . $field[
'TITLE'] .
'"' :
'';
615 $result .=
'<tr' . $required .
'><td width="40%">' . $field[
'TITLE'] .
':</td><td width="60%">';
617 if ($field[
'TYPE'] ==
'TEXT')
619 $result .=
'<input type="text" size="45" maxlength="255"' .
$name . $value . $disabled .
$title .
'>';
621 elseif ($field[
'TYPE'] ==
'CHECKBOX')
623 $checked = isset($field[
'CHECKED']) && $field[
'CHECKED'] ?
' checked' :
'';
624 $result .=
'<input type="checkbox"' .
$name . $value . $checked . $disabled .
$title .
'>';
629 if (is_array($field[
'OPTIONS']))
631 foreach ($field[
'OPTIONS'] as
$key =>
$val)
633 $result .=
'<option value="' .
$key .
'"' . (
$key == $field[
'VALUE'] ?
' selected' :
'') .
'>' .
$val .
'</option>';
648 $cacheFlags = Configuration::getValue(
'cache_flags');
649 return $cacheFlags[
'geoip_manager'] ?? 604800;
655 $cache->cleanDir(static::CACHE_DIR);
660 if (static::$logger ===
null)
670 return static::$logger;
static create(string $id, $params=[])
static registerAutoLoadClasses($moduleName, array $classes)
static getList(array $parameters=array())
static getHandlerAdminConfigHtml(Base $handler)
static getOrganizationName($ip='', $lang='')
static saveToStore(IpAddress $ipAddress, array $records, Data $geoData, string $lang)
static getCacheId(IpAddress $ipAddress, string $handler)
static getTimezoneName($ip='', $lang='')
static getCountryCode($ip='', $lang='')
static getDataResult($ip='', $lang='', array $required=[])
static getFromStore(IpAddress $ipAddress, string $handler)
static getCountryName($ip='', $lang='')
static getGeoPosition($ip='', $lang='')
static getCityName($ip='', $lang='')
static getGeoPositionLatitude($ip='', $lang='')
static getIspName($ip='', $lang='')
static getGeoPositionLongitude($ip='', $lang='')
static getCityPostCode($ip='', $lang='')
static findForIp(IpAddress $ipAddress, array $records, string $lang)
static getHandlerByClassName($className)
static setLogErrors($isLog)
</td ></tr ></table ></td ></tr >< tr >< td class="bx-popup-label bx-width30"><?=GetMessage("PAGE_NEW_TAGS")?> array( $site)
if(!defined('SITE_ID')) $lang
if( $daysToExpire >=0 &&$daysToExpire< 60 elseif)( $daysToExpire< 0)
if(empty($signedUserToken)) $key
if($inWords) echo htmlspecialcharsbx(Number2Word_Rus(roundEx($totalVatSum $params['CURRENCY']