Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
basemanager.php
1<?php
3
8
13abstract class BaseManager
14{
15 private const ON_BUILD_VERIFICATION_MANAGER_LIST = "onBuildVerificationManagerList";
16
20 abstract public static function getPathPrefix(): string;
21
25 abstract protected static function getUrlRewritePath(): string;
26
34 public static function getSiteList() : array
35 {
36 $siteList = [];
37
38 $res = Main\SiteTable::getList([
39 "select" => ["ID", "NAME", "SERVER_NAME"],
40 "filter" => [
41 "ACTIVE" => "Y"
42 ]
43 ]);
44 while ($row = $res->fetch())
45 {
46 if (empty($row["SERVER_NAME"]))
47 {
48 $row["SERVER_NAME"] = Main\Application::getInstance()->getContext()->getServer()->getServerName();
49 }
50
51 $siteList[] = $row;
52 }
53
54 $landingSiteList = self::getLandingSiteList();
55 if ($landingSiteList)
56 {
57 $siteList = array_merge($siteList, $landingSiteList);
58 }
59
60 return $siteList;
61 }
62
67 public static function getLandingSiteList() : array
68 {
69 $landingSiteList = [];
70
71 if (Main\Loader::includeModule('landing'))
72 {
73 $res = Landing\Site::getList([
74 'select' => [
75 'ID', 'NAME' => 'TITLE', 'SERVER_NAME' => 'DOMAIN.DOMAIN'
76 ],
77 'filter' => [
78 'ACTIVE' => 'Y',
79 'TYPE' => 'STORE'
80 ]
81 ]);
82 while ($row = $res->fetch())
83 {
84 $landingSiteList[] = $row;
85 }
86 }
87
88 return $landingSiteList;
89 }
90
96 public static function isLandingSite($domain) : bool
97 {
98 $landingSiteList = self::getLandingSiteList();
99
100 $result = array_filter($landingSiteList, static function($site) use ($domain) {
101 return $site['SERVER_NAME'] === $domain;
102 });
103
104 return $result ? true : false;
105 }
106
114 public static function getList(array $parameters = []) : Main\ORM\Query\Result
115 {
116 return Internals\DomainVerificationTable::getList($parameters);
117 }
118
130 public static function save($data, $file)
131 {
132 if (empty($file["tmp_name"]) || empty($file["name"]))
133 {
134 return (new Main\Result())->addError(new Main\Error("File not found"));
135 }
136
137 $data["CONTENT"] = self::readFile($file["tmp_name"]);
138
139 $data = self::prepareData($data);
140 $checkDataResult = self::checkData($data);
141 if (!$checkDataResult->isSuccess())
142 {
143 return $checkDataResult;
144 }
145
146 $res = Internals\DomainVerificationTable::getList([
147 "select" => ["ID"],
148 "filter" => [
149 "=PATH" => $data["PATH"],
150 "=DOMAIN" => $data["DOMAIN"],
151 ]
152 ])->fetch();
153 if ($res)
154 {
155 return Internals\DomainVerificationTable::update($res["ID"], [
156 "CONTENT" => $data["CONTENT"],
157 "ENTITY" => $data["ENTITY"],
158 ]);
159 }
160
161 $addResult = Internals\DomainVerificationTable::add($data);
162 if ($addResult->isSuccess())
163 {
164 if (self::canUseUrlRewrite($data["DOMAIN"]))
165 {
166 self::addUrlRewrite($data["DOMAIN"], $data["PATH"]);
167 }
168 elseif (self::isLandingSite($data["DOMAIN"]))
169 {
170 self::registerLandingEventHandler();
171 }
172 else
173 {
174 self::registerB24EventHandler();
175 }
176 }
177
178 return $addResult;
179 }
180
185 private static function prepareData(array $data) : array
186 {
187 if ($data["PATH"])
188 {
189 $data["PATH"] = static::getPathPrefix().$data["PATH"];
190 }
191
192 $data["DOMAIN"] = self::prepareDomain($data["DOMAIN"]);
193
194 return $data;
195 }
196
201 private static function checkData($data) : Main\Result
202 {
203 $result = new Main\Result();
204
205 if (!isset($data["PATH"]))
206 {
207 $result->addError(new Main\Error("Path not found"));
208 }
209 elseif (!isset($data["DOMAIN"]))
210 {
211 $result->addError(new Main\Error("Domain not found"));
212 }
213 elseif (!isset($data["CONTENT"]))
214 {
215 $result->addError(new Main\Error("Content not found"));
216 }
217 elseif (!isset($data["ENTITY"]))
218 {
219 $result->addError(new Main\Error("Entity not found"));
220 }
221
222 return $result;
223 }
224
230 public static function delete($id): Main\ORM\Data\DeleteResult
231 {
232 $domainVerificationData = Internals\DomainVerificationTable::getById($id)->fetch();
233
234 $deleteResult = Internals\DomainVerificationTable::delete($id);
235 if ($deleteResult->isSuccess())
236 {
237 if (self::canUseUrlRewrite($domainVerificationData["DOMAIN"]))
238 {
239 self::deleteUrlRewrite($domainVerificationData["DOMAIN"], $domainVerificationData["PATH"]);
240 }
241 else
242 {
243 self::unRegisterEventHandlers();
244 }
245 }
246
247 return $deleteResult;
248 }
249
255 private static function readFile($path)
256 {
257 $file = new Main\IO\File($path);
258 if ($file->isExists())
259 {
260 return $file->getContents();
261 }
262
263 return null;
264 }
265
270 private static function prepareDomain($domain)
271 {
272 $domain = filter_var($domain, FILTER_SANITIZE_URL);
273 $domain = trim($domain, " \t\n\r\0\x0B/\\");
274 $components = parse_url($domain);
275
276 if (isset($components["host"]) && !empty($components["host"]))
277 {
278 return $components["host"];
279 }
280
281 if (isset($components["path"]) && !empty($components["path"]))
282 {
283 return $components["path"];
284 }
285
286 return $domain;
287 }
288
294 public static function needVerification($entityName) : bool
295 {
296 $handlerList = static::getEntityList();
297 return in_array($entityName, $handlerList);
298 }
299
304 abstract protected static function getEntityList() : array;
305
316 private static function addUrlRewrite(string $domain, string $path): bool
317 {
318 $fields = [
319 "CONDITION" => "#^{$path}#",
320 "RULE" => "",
321 "ID" => "",
322 "PATH" => static::getUrlRewritePath(),
323 ];
324
325 $siteId = self::getSiteIdByDomain($domain);
326 if (!$siteId)
327 {
328 return false;
329 }
330
331 Main\UrlRewriter::add($siteId, $fields);
332 return true;
333 }
334
345 private static function deleteUrlRewrite(string $domain, string $path): bool
346 {
347 $fields = [
348 "CONDITION" => "#^{$path}#",
349 "PATH" => static::getUrlRewritePath(),
350 ];
351
352 $siteId = self::getSiteIdByDomain($domain);
353 if (!$siteId)
354 {
355 return false;
356 }
357
358 Main\UrlRewriter::delete($siteId, $fields);
359
360 return true;
361 }
362
371 private static function getSiteIdByDomain($domain)
372 {
373 $site = array_filter(static::getSiteList(), function($site) use ($domain) {
374 return $domain === $site["SERVER_NAME"];
375 });
376
377 if (!$site)
378 {
379 return null;
380 }
381
382 $site = current($site);
383 if ($site["ID"])
384 {
385 return $site["ID"];
386 }
387
388 return null;
389 }
390
399 public static function searchByRequest($serverName, $requestUri)
400 {
401 return self::getList([
402 "select" => ["*"],
403 "filter" => [
404 "=PATH" => $requestUri,
405 "=DOMAIN" => $serverName,
406 ],
407 "limit" => 1
408 ])->fetch();
409 }
410
414 private static function isB24(): bool
415 {
416 return Main\ModuleManager::isModuleInstalled('bitrix24');
417 }
418
423 public static function getManagerList(): array
424 {
425 $handlerList = [
426 '\Bitrix\Sale\PaySystem\Domain\Verification\Manager' => '/bitrix/modules/sale/lib/paysystem/domain/verification/manager.php',
427 ];
428
429 $event = new Event('sale', self::ON_BUILD_VERIFICATION_MANAGER_LIST);
430 $event->send();
431
432 $resultList = $event->getResults();
433 if (is_array($resultList) && !empty($resultList))
434 {
435 $customHandlerList = [];
436 foreach ($resultList as $eventResult)
437 {
439 if ($eventResult->getType() === Main\EventResult::SUCCESS)
440 {
441 $params = $eventResult->getParameters();
442 if (!empty($params) && is_array($params))
443 {
444 $customHandlerList[] = $params;
445 }
446 }
447 }
448
449 $handlerList = array_merge($handlerList, ...$customHandlerList);
450 }
451
452 Main\Loader::registerAutoLoadClasses(null, $handlerList);
453
454 return $handlerList;
455 }
456
462 private static function canUseUrlRewrite($domain): bool
463 {
464 return (!self::isB24() && !self::isLandingSite($domain));
465 }
466
467 private static function registerLandingEventHandler(): void
468 {
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';
473 });
474 if (!$onPubHttStatus)
475 {
476 $eventManager->registerEventHandler(
477 'landing',
478 'onPubHttpStatus',
479 'sale',
480 '\Bitrix\Sale\Domain\Verification\Service',
481 'landingDomainVerificationHandler'
482 );
483 }
484 }
485
486 private static function registerB24EventHandler(): void
487 {
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';
492 });
493 if (!$onEpilog)
494 {
495 $eventManager->registerEventHandler(
496 'main',
497 'OnEpilog',
498 'sale',
499 '\Bitrix\Sale\Domain\Verification\Service',
500 'b24DomainVerificationHandler');
501 }
502 }
503
504 private static function unRegisterEventHandlers(): void
505 {
506 $domainVerificationList = Internals\DomainVerificationTable::getList()->fetchAll();
507
508 $needUnRegisterLandingHandler = true;
509 $needUnRegisterB24Handler = true;
510
511 foreach ($domainVerificationList as $domainVerification)
512 {
513 if (self::isLandingSite($domainVerification['DOMAIN']))
514 {
515 $needUnRegisterLandingHandler = false;
516 }
517 else
518 {
519 $needUnRegisterB24Handler = false;
520 }
521 }
522
523 if ($needUnRegisterLandingHandler)
524 {
525 self::unRegisterLandingEventHandler();
526 }
527
528 if ($needUnRegisterB24Handler)
529 {
530 self::unRegisterB24EventHandler();
531 }
532 }
533
534 private static function unRegisterLandingEventHandler(): void
535 {
536 $eventManager = Main\EventManager::getInstance();
537 $eventManager->unRegisterEventHandler(
538 'landing',
539 'onPubHttpStatus',
540 'sale',
541 '\Bitrix\Sale\Domain\Verification\Service',
542 'landingDomainVerificationHandler'
543 );
544 }
545
546 private static function unRegisterB24EventHandler(): void
547 {
548 $eventManager = Main\EventManager::getInstance();
549 $eventManager->unRegisterEventHandler(
550 'main',
551 'OnEpilog',
552 'sale',
553 '\Bitrix\Sale\Domain\Verification\Service',
554 'b24DomainVerificationHandler'
555 );
556 }
557}
static searchByRequest($serverName, $requestUri)
static getList(array $parameters=[])