15 private const ON_BUILD_VERIFICATION_MANAGER_LIST =
"onBuildVerificationManagerList";
38 $res = Main\SiteTable::getList([
39 "select" => [
"ID",
"NAME",
"SERVER_NAME"],
44 while ($row = $res->fetch())
46 if (empty($row[
"SERVER_NAME"]))
48 $row[
"SERVER_NAME"] = Main\Application::getInstance()->getContext()->getServer()->getServerName();
57 $siteList = array_merge($siteList, $landingSiteList);
69 $landingSiteList = [];
71 if (Main\Loader::includeModule(
'landing'))
73 $res = Landing\Site::getList([
75 'ID',
'NAME' =>
'TITLE',
'SERVER_NAME' =>
'DOMAIN.DOMAIN'
82 while ($row = $res->fetch())
84 $landingSiteList[] = $row;
88 return $landingSiteList;
100 $result = array_filter($landingSiteList,
static function($site) use ($domain) {
101 return $site[
'SERVER_NAME'] === $domain;
104 return $result ? true :
false;
116 return Internals\DomainVerificationTable::getList($parameters);
130 public static function save($data, $file)
132 if (empty($file[
"tmp_name"]) || empty($file[
"name"]))
134 return (
new Main\
Result())->addError(
new Main\
Error(
"File not found"));
137 $data[
"CONTENT"] = self::readFile($file[
"tmp_name"]);
139 $data = self::prepareData($data);
140 $checkDataResult = self::checkData($data);
141 if (!$checkDataResult->isSuccess())
143 return $checkDataResult;
146 $res = Internals\DomainVerificationTable::getList([
149 "=PATH" => $data[
"PATH"],
150 "=DOMAIN" => $data[
"DOMAIN"],
155 return Internals\DomainVerificationTable::update($res[
"ID"], [
156 "CONTENT" => $data[
"CONTENT"],
157 "ENTITY" => $data[
"ENTITY"],
161 $addResult = Internals\DomainVerificationTable::add($data);
162 if ($addResult->isSuccess())
164 if (self::canUseUrlRewrite($data[
"DOMAIN"]))
166 self::addUrlRewrite($data[
"DOMAIN"], $data[
"PATH"]);
168 elseif (self::isLandingSite($data[
"DOMAIN"]))
170 self::registerLandingEventHandler();
174 self::registerB24EventHandler();
185 private static function prepareData(array $data) : array
189 $data[
"PATH"] = static::getPathPrefix().$data[
"PATH"];
192 $data[
"DOMAIN"] = self::prepareDomain($data[
"DOMAIN"]);
201 private static function checkData($data) : Main\
Result
205 if (!isset($data[
"PATH"]))
207 $result->addError(
new Main\
Error(
"Path not found"));
209 elseif (!isset($data[
"DOMAIN"]))
211 $result->addError(
new Main\Error(
"Domain not found"));
213 elseif (!isset($data[
"CONTENT"]))
215 $result->addError(
new Main\Error(
"Content not found"));
217 elseif (!isset($data[
"ENTITY"]))
219 $result->addError(
new Main\
Error(
"Entity not found"));
232 $domainVerificationData = Internals\DomainVerificationTable::getById($id)->fetch();
234 $deleteResult = Internals\DomainVerificationTable::delete($id);
235 if ($deleteResult->isSuccess())
237 if (self::canUseUrlRewrite($domainVerificationData[
"DOMAIN"]))
239 self::deleteUrlRewrite($domainVerificationData[
"DOMAIN"], $domainVerificationData[
"PATH"]);
243 self::unRegisterEventHandlers();
247 return $deleteResult;
255 private static function readFile($path)
258 if ($file->isExists())
260 return $file->getContents();
270 private static function prepareDomain($domain)
272 $domain = filter_var($domain, FILTER_SANITIZE_URL);
273 $domain = trim($domain,
" \t\n\r\0\x0B/\\");
274 $components = parse_url($domain);
276 if (isset($components[
"host"]) && !empty($components[
"host"]))
278 return $components[
"host"];
281 if (isset($components[
"path"]) && !empty($components[
"path"]))
283 return $components[
"path"];
296 $handlerList = static::getEntityList();
297 return in_array($entityName, $handlerList);
316 private static function addUrlRewrite(
string $domain,
string $path): bool
319 "CONDITION" =>
"#^{$path}#",
322 "PATH" => static::getUrlRewritePath(),
325 $siteId = self::getSiteIdByDomain($domain);
331 Main\UrlRewriter::add($siteId, $fields);
345 private static function deleteUrlRewrite(
string $domain,
string $path): bool
348 "CONDITION" =>
"#^{$path}#",
349 "PATH" => static::getUrlRewritePath(),
352 $siteId = self::getSiteIdByDomain($domain);
358 Main\UrlRewriter::delete($siteId, $fields);
371 private static function getSiteIdByDomain($domain)
373 $site = array_filter(static::getSiteList(),
function($site) use ($domain) {
374 return $domain === $site[
"SERVER_NAME"];
382 $site = current($site);
404 "=PATH" => $requestUri,
405 "=DOMAIN" => $serverName,
414 private static function isB24(): bool
416 return Main\ModuleManager::isModuleInstalled(
'bitrix24');
423 public static function getManagerList(): array
426 '\Bitrix\Sale\PaySystem\Domain\Verification\Manager' =>
'/bitrix/modules/sale/lib/paysystem/domain/verification/manager.php',
429 $event =
new Event(
'sale', self::ON_BUILD_VERIFICATION_MANAGER_LIST);
432 $resultList = $event->getResults();
433 if (is_array($resultList) && !empty($resultList))
435 $customHandlerList = [];
436 foreach ($resultList as $eventResult)
439 if ($eventResult->getType() === Main\EventResult::SUCCESS)
441 $params = $eventResult->getParameters();
442 if (!empty($params) && is_array($params))
444 $customHandlerList[] = $params;
449 $handlerList = array_merge($handlerList, ...$customHandlerList);
452 Main\Loader::registerAutoLoadClasses(
null, $handlerList);
462 private static function canUseUrlRewrite($domain): bool
464 return (!self::isB24() && !self::isLandingSite($domain));
467 private static function registerLandingEventHandler(): void
469 $eventManager = Main\EventManager::getInstance();
470 $handlers = $eventManager->findEventHandlers(
'landing',
'onPubHttpStatus');
471 $onPubHttStatus = array_filter($handlers,
static function($handler) {
472 return $handler[
'TO_METHOD'] ===
'landingDomainVerificationHandler';
474 if (!$onPubHttStatus)
476 $eventManager->registerEventHandler(
480 '\Bitrix\Sale\Domain\Verification\Service',
481 'landingDomainVerificationHandler'
486 private static function registerB24EventHandler(): void
488 $eventManager = Main\EventManager::getInstance();
489 $handlers = $eventManager->findEventHandlers(
'main',
'OnEpilog');
490 $onEpilog = array_filter($handlers,
static function($handler) {
491 return $handler[
'TO_METHOD'] ===
'b24DomainVerificationHandler';
495 $eventManager->registerEventHandler(
499 '\Bitrix\Sale\Domain\Verification\Service',
500 'b24DomainVerificationHandler');
504 private static function unRegisterEventHandlers(): void
506 $domainVerificationList = Internals\DomainVerificationTable::getList()->fetchAll();
508 $needUnRegisterLandingHandler =
true;
509 $needUnRegisterB24Handler =
true;
511 foreach ($domainVerificationList as $domainVerification)
513 if (self::isLandingSite($domainVerification[
'DOMAIN']))
515 $needUnRegisterLandingHandler =
false;
519 $needUnRegisterB24Handler =
false;
523 if ($needUnRegisterLandingHandler)
525 self::unRegisterLandingEventHandler();
528 if ($needUnRegisterB24Handler)
530 self::unRegisterB24EventHandler();
534 private static function unRegisterLandingEventHandler(): void
536 $eventManager = Main\EventManager::getInstance();
537 $eventManager->unRegisterEventHandler(
541 '\Bitrix\Sale\Domain\Verification\Service',
542 'landingDomainVerificationHandler'
546 private static function unRegisterB24EventHandler(): void
548 $eventManager = Main\EventManager::getInstance();
549 $eventManager->unRegisterEventHandler(
553 '\Bitrix\Sale\Domain\Verification\Service',
554 'b24DomainVerificationHandler'