• Модуль: main
  • Путь к файлу: ~/bitrix/modules/main/lib/service/geoip/manager.php
  • Класс: BitrixMainServiceGeoIpManager
  • Вызов: Manager::getDataResult
static function getDataResult($ip = '', $lang = '', array $required = [])
{
	$result = null;

	if ($ip == '')
	{
		$ip = self::getRealIp();
	}

	// cache on the hit
	if (isset(self::$data[$ip][$lang]))
	{
		$data = self::$data[$ip][$lang];
		if (empty($required) || self::hasDataAllRequiredFields($required, $data))
		{
			$result = ((new Result())->setGeoData($data));
		}
	}

	if (!$result)
	{
		if(self::$handlers === null)
		{
			self::initHandlers();
		}

		foreach (self::$handlers as $class => $handler)
		{
			if (!$handler->isInstalled() || !$handler->isActive())
			{
				continue;
			}

			if ($lang != '' && !in_array($lang, $handler->getSupportedLanguages()))
			{
				continue;
			}

			if (!empty($required) && !self::hasDataAllRequiredFields($required, $handler->getProvidingData()))
			{
				continue;
			}

			$ipAddress = new WebIpAddress($ip);

			// get from cache
			$records = self::getFromStore($ipAddress, $class);
			$data = static::findForIp($ipAddress, $records, $lang);

			if ($data)
			{
				if (empty($required) || self::hasDataAllRequiredFields($required, $data))
				{
					$data->ip = $ip;
					self::$data[$ip][$lang] = $data;

					$result = ((new Result())->setGeoData($data));
					break;
				}
			}

			$dataResult = $handler->getDataResult($ip, $lang);

			if (!$dataResult)
			{
				continue;
			}

			if (!$dataResult->isSuccess())
			{
				if (self::$logErrors && ($logger = static::getLogger()))
				{
					$logger->error(
						"{date} - {host}nIP: {ip}, handler: {handler}, lang: {lang}n{errors}n{trace}{delimiter}n",
						[
							'ip' => $ip,
							'lang' => $lang,
							'handler' => $handler->getId(),
							'errors' => $dataResult->getErrorMessages(),
							'trace' => DiagHelper::getBackTrace(6, DEBUG_BACKTRACE_IGNORE_ARGS, 3),
						]
					);
				}

				continue;
			}

			$data = $dataResult->getGeoData();
			$data->handlerClass = $class;
			$data->ip = $ip;

			// write to cache
			self::$data[$ip][$lang] = $data;
			self::saveToStore($ipAddress, $records, $data, $lang);

			// save geonames
			if (Option::get('main', 'collect_geonames', 'N') == 'Y')
			{
				if (!empty($data->geonames))
				{
					InternalGeonameTable::save($data->geonames);
				}
			}

			$result = $dataResult;
			break;
		}
	}

	if ($result)
	{
		$event = new Event('main', 'onGeoIpGetResult', [
			'originalData' => clone $result->getGeoData(),
			'data' => $result->getGeoData(),
		]);
		$event->send();
	}

	return $result;
}