• Модуль: imbot
  • Путь к файлу: ~/bitrix/modules/imbot/lib/bot/network.php
  • Класс: BitrixImBotBotNetwork
  • Вызов: Network::checkPublicUrl
static function checkPublicUrl($publicUrl = null)
{
	$publicUrl = $publicUrl ?? ImBotHttp::getServerAddress();
	if (empty($publicUrl))
	{
		$message = Loc::getMessage('IMBOT_NETWORK_ERROR_PUBLIC_URL_EMPTY');
		if (empty($message))
		{
			$message = 'Cannot detect a value of the portal public url.';
		}

		self::$lastError = new ImBotError(
			__METHOD__,
			'PUBLIC_URL_EMPTY',
			$message
		);

		return false;
	}

	if (
		!($parsedUrl = parse_url($publicUrl))
		|| empty($parsedUrl['host'])
		|| strpos($parsedUrl['host'], '.') === false
		|| !in_array($parsedUrl['scheme'], ['http', 'https'])
	)
	{
		$message = Loc::getMessage('IMBOT_NETWORK_ERROR_PUBLIC_URL_MALFORMED');
		if (empty($message))
		{
			$message = 'Portal public url is malformed.';
		}
		self::$lastError = new ImBotError(
			__METHOD__,
			'PUBLIC_URL_MALFORMED',
			$message
		);

		return false;
	}

	// check for local address
	$host = $parsedUrl['host'];
	if (
		strtolower($host) == 'localhost'
		|| $host == '0.0.0.0'
		||
		(
			preg_match('#^d{1,3}.d{1,3}.d{1,3}.d{1,3}$#', $host)
			&& preg_match('#^(127|10|172.16|192.168).#', $host)
		)
	)
	{
		$message = Loc::getMessage('IMBOT_NETWORK_ERROR_PUBLIC_URL_LOCALHOST', ['#HOST#' => $host]);
		if (empty($message))
		{
			$message = 'Portal public url points to localhost: '.$host;
		}

		self::$lastError = new ImBotError(
			__METHOD__,
			'PUBLIC_URL_LOCALHOST',
			$message
		);

		return false;
	}

	$error = (new BitrixMainWebUri($publicUrl))->convertToPunycode();
	if ($error instanceof BitrixMainError)
	{
		$message = Loc::getMessage('IMBOT_NETWORK_ERROR_CONVERTING_PUNYCODE', ['#HOST#' => $host, '#ERROR#' => $error->getMessage()]);
		if (empty($message))
		{
			$message = 'Error converting hostname '.$host.' to punycode: '.$error->getMessage();
		}

		self::$lastError = new ImBotError(
			__METHOD__,
			'PUBLIC_URL_MALFORMED',
			$message
		);

		return false;
	}

	$documentRoot = '';
	$siteList = CSite::getList('', '', ['DOMAIN' => $host, 'ACTIVE' => 'Y']);
	if ($site = $siteList->fetch())
	{
		$documentRoot = $site['ABS_DOC_ROOT'];
	}
	else
	{
		$siteList = CSite::getList('', '', ['DEFAULT' => 'Y', 'ACTIVE' => 'Y']);
		if ($site = $siteList->fetch())
		{
			$documentRoot = $site['ABS_DOC_ROOT'];
		}
	}
	if ($documentRoot)
	{
		$documentRoot = MainIOPath::normalize($documentRoot);
		$publicHandler = new MainIOFile($documentRoot. self::PORTAL_PATH);
		if (!$publicHandler->isExists())
		{
			$message = Loc::getMessage('IMBOT_NETWORK_ERROR_PUBLIC_URL_HANDLER_PATH', ['#PATH#' => self::PORTAL_PATH]);
			if (empty($message))
			{
				$message = 'The file handler has not been found within the site document root. Expected: '.self::PORTAL_PATH;
			}

			self::$lastError = new ImBotError(
				__METHOD__,
				'PUBLIC_URL_HANDLER_PATH',
				$message
			);

			return false;
		}
	}

	$port = '';
	if (
		isset($parsedUrl['port'])
		&& (int)$parsedUrl['port'] > 0
	)
	{
		$port = ':'.(int)$parsedUrl['port'];
	}

	$http = self::instanceHttpClient();

	$http->setPortalDomain($parsedUrl['scheme'].'://'.$parsedUrl['host']. $port);

	$result = $http->query(self::COMMAND_CHECK_PUBLIC_URL, [], true);

	if (isset($result['error']))
	{
		$message = Loc::getMessage('IMBOT_NETWORK_ERROR_'. mb_strtoupper($result['error']['code']));
		if (empty($message))
		{
			if ($result['error']['msg'] !== '')
			{
				$message = $result['error']['msg'];
			}
			else
			{
				$message = Loc::getMessage('IMBOT_NETWORK_ERROR_PUBLIC_URL_FAIL', ['#ERROR#' => $result['error']['code']]);
			}
		}
		self::$lastError = new ImBotError(
			__METHOD__,
			$result['error']['code'],
			$message,
			[
				($result['error']['errorStack'] ?? ''),
				($result['error']['errorResult'] ?? '')
			]
		);

		return false;
	}

	return isset($result['result']) && ($result['result'] === 'OK');
}