- Модуль: imconnector
- Путь к файлу: ~/bitrix/modules/imconnector/lib/connector.php
- Класс: BitrixImConnectorConnector
- Вызов: Connector::checkPublicUrl
static function checkPublicUrl(string $publicUrl, bool $checkHandlerPath = true): Result
{
$result = new Result();
if (empty($publicUrl))
{
$message = Loc::getMessage('IMCONNECTOR_ERROR_PUBLIC_URL_EMPTY');
if (empty($message))
{
$message = 'Cannot detect a value of the portal public url.';
}
return $result->addError(new Error($message, Library::ERROR_IMCONNECTOR_PUBLIC_URL_EMPTY));
}
if (
!($parsedUrl = parse_url($publicUrl))
|| empty($parsedUrl['host'])
|| strpos($parsedUrl['host'], '.') === false
|| !in_array($parsedUrl['scheme'], ['http', 'https'])
)
{
$message = Loc::getMessage('IMCONNECTOR_ERROR_PUBLIC_URL_MALFORMED');
if (empty($message))
{
$message = 'Portal public url is malformed.';
}
return $result->addError(new Error($message, Library::ERROR_IMCONNECTOR_PUBLIC_URL_MALFORMED));
}
// 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('IMCONNECTOR_ERROR_PUBLIC_URL_LOCALHOST', ['#HOST#' => $host]);
if (empty($message))
{
$message = 'Portal public url points to localhost: '.$host;
}
return $result->addError(new Error($message, Library::ERROR_IMCONNECTOR_PUBLIC_URL_LOCALHOST));
}
$error = (new BitrixMainWebUri($publicUrl))->convertToPunycode();
if ($error instanceof BitrixMainError)
{
$message = Loc::getMessage('IMCONNECTOR_ERROR_CONVERTING_PUNYCODE', ['#HOST#' => $host, '#ERROR#' => $error->getMessage()]);
if (empty($message))
{
$message = 'Error converting hostname '.$host.' to punycode: '.$error->getMessage();
}
return $result->addError(new Error($message, Library::ERROR_IMCONNECTOR_PUBLIC_URL_MALFORMED));
}
if ($checkHandlerPath)
{
$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 = BitrixMainIOPath::normalize($documentRoot);
$publicHandler = new BitrixMainIOFile($documentRoot. Library::PORTAL_PATH);
if (!$publicHandler->isExists())
{
$message = Loc::getMessage('IMCONNECTOR_ERROR_PUBLIC_URL_HANDLER_PATH', ['#PATH#' => Library::PORTAL_PATH]);
if (empty($message))
{
$message = 'The file handler has not been found within the site document root. Expected: '. Library::PORTAL_PATH;
}
return $result->addError(new Error($message, Library::ERROR_IMCONNECTOR_PUBLIC_URL_HANDLER_PATH));
}
}
}
return $result;
}