Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
client.php
1<?php
3
12use Bitrix\Market\Subscription;
15use Bitrix\Bitrix24\Feature;
16
17if(!defined('REST_MP_CATEGORIES_CACHE_TTL'))
18{
19 define('REST_MP_CATEGORIES_CACHE_TTL', 86400);
20}
21
22class Client
23{
24 const CATEGORIES_CACHE_TTL = REST_MP_CATEGORIES_CACHE_TTL;
25 private const SUBSCRIPTION_REGION = [
26 'ru',
27 'ua',
28 'by',
29 ];
30 private const SUBSCRIPTION_DEFAULT_START_TIME = [
31 'ua' => 1625090400,
32 'by' => 1660514400,
33 ];
34
35 protected static $buyLinkList = array(
36 'bitrix24' => '/settings/order/make.php?limit=#NUM#&module=#CODE#',
37 'ru' => 'https://marketplace.1c-bitrix.ru/tobasket.php?ID=#CODE#&limit=#NUM#&b24=y',
38 'en' => 'https://store.bitrix24.com/tobasket.php?ID=#CODE#&limit=#NUM#&b24=y',
39 'de' => 'https://store.bitrix24.de/tobasket.php?ID=#CODE#&limit=#NUM#&b24=y',
40 'ua' => 'https://marketplace.1c-bitrix.ua/tobasket.php?ID=#CODE#&limit=#NUM#&b24=y',
41 );
42
43 private static $appTop = null;
44 private static $isPayApplicationAvailable;
45
46 public static function getTop($action, $fields = array())
47 {
48 $allowedActions = array(
53 );
54
55 if(in_array($action, $allowedActions))
56 {
57 if(!is_array(self::$appTop))
58 {
59 $batch = array();
60 foreach($allowedActions as $method)
61 {
62 $batch[$method] = array($method, $fields);
63 }
64
65 self::$appTop = Transport::instance()->batch($batch);
66 }
67
68 return self::$appTop[$action];
69 }
70 else
71 {
72 return Transport::instance()->call($action);
73 }
74 }
75
76 public static function getBuy($codeList)
77 {
78 return Transport::instance()->call(
80 array(
81 "code" => implode(",", $codeList)
82 )
83 );
84 }
85
86 public static function getImmuneApp()
87 {
88 return Transport::instance()->call(
90 );
91 }
92
93 public static function getUpdates($codeList)
94 {
95 $updatesList = Transport::instance()->call(
97 array(
98 "code" => serialize($codeList)
99 )
100 );
101
102 return $updatesList;
103 }
104
105 public static function setAvailableUpdate($updateList = array())
106 {
107 if(!is_array($updateList) || count($updateList) <= 0)
108 {
109 $cnt = 0;
110 $optionValue = "";
111 }
112 else
113 {
114 $cnt = count($updateList);
115 $optionValue = array();
116
117 foreach($updateList as $update)
118 {
119 if(is_array($update['VERSIONS']) && count($update['VERSIONS']) > 0)
120 {
121 $optionValue[$update["CODE"]] = max(array_keys($update["VERSIONS"]));
122 }
123 }
124
125 $optionValue = serialize($optionValue);
126 }
127
128 Option::set("rest", "mp_num_updates", $cnt);
129 Option::set("rest", "mp_updates", $optionValue);
130 }
131
132 public static function getAvailableUpdate($code = false)
133 {
134 $updates = Option::get("rest", "mp_updates", "");
135 $updates = $updates == "" ? array() : unserialize($updates, ['allowed_classes' => false]);
136
137 if($code !== false)
138 {
139 return array_key_exists($code, $updates) ? $updates[$code] : false;
140 }
141 else
142 {
143 return $updates;
144 }
145 }
146
147 public static function getAvailableUpdateNum()
148 {
149 return intval(Option::get("rest", "mp_num_updates", 0));
150 }
151
158 public static function getCategoriesFull($forceReload = false)
159 {
160 $managedCache = Application::getInstance()->getManagedCache();
161
162 $cacheId = 'rest|marketplace|categories|full|'.LANGUAGE_ID;
163
164 $requestNeeded = true;
165
166 if (
167 $forceReload === false
168 && static::CATEGORIES_CACHE_TTL > 0
169 && $managedCache->read(static::CATEGORIES_CACHE_TTL, $cacheId)
170 )
171 {
172 $result = $managedCache->get($cacheId);
173 if (is_array($result))
174 {
175 $requestNeeded = false;
176 }
177 elseif (intval($result) > time())
178 {
179 $requestNeeded = false;
180 $result = [];
181 }
182 }
183
184 if ($requestNeeded)
185 {
187 if (!is_array($result))
188 {
189 $result = time() + 300;
190 }
191
192 if (static::CATEGORIES_CACHE_TTL > 0)
193 {
194 $managedCache->set($cacheId, $result);
195 }
196 }
197
198 return (is_array($result) ? $result : []);
199 }
200
207 public static function getCategories($forceReload = false)
208 {
209 $categories = static::getCategoriesFull($forceReload);
210 return (is_array($categories['ITEMS']) ? $categories['ITEMS'] : []);
211 }
212
213 public static function getCategory($code, $page = false, $pageSize = false)
214 {
215 $queryFields = Array(
216 "code" => $code
217 );
218 $page = intval($page);
219 $pageSize = intval($pageSize);
220 if($page > 0)
221 {
222 $queryFields["page"] = $page;
223 }
224 if($pageSize > 0)
225 {
226 $queryFields["onPageSize"] = $pageSize;
227 }
228
229 return Transport::instance()->call(
231 $queryFields
232 );
233 }
234
235 public static function getByTag($tag, $page = false, $pageSize = false)
236 {
237 $queryFields = Array(
238 "tag" => $tag
239 );
240 $page = intval($page);
241 if($page > 0)
242 {
243 $queryFields["page"] = $page;
244 }
245
246 if($pageSize > 0)
247 {
248 $queryFields["onPageSize"] = $pageSize;
249 }
250
251 return Transport::instance()->call(
253 $queryFields
254 );
255 }
256
257 public static function getLastByTag($tag, $page = false, $pageSize = false)
258 {
259 $queryFields = Array(
260 "tag" => $tag,
261 "sort" => "date_public"
262 );
263
264 $page = intval($page);
265 if($page > 0)
266 {
267 $queryFields["page"] = $page;
268 }
269
270 if($pageSize > 0)
271 {
272 $queryFields["onPageSize"] = $pageSize;
273 }
274
275 return Transport::instance()->call(Transport::METHOD_GET_TAG, $queryFields);
276 }
277
278 public static function getApp($code, $version = false, $checkHash = false, $installHash = false)
279 {
280 $queryFields = Array(
281 "code" => $code
282 );
283
284 $version = intval($version);
285 if($version > 0)
286 {
287 $queryFields["ver"] = $version;
288 }
289
290 if($checkHash !== false)
291 {
292 $queryFields["check_hash"] = $checkHash;
293 $queryFields["install_hash"] = $installHash;
294 }
295
296 return Transport::instance()->call(
298 $queryFields
299 );
300 }
301
308 public static function getSite($id)
309 {
310 $query = [
311 'site_id' => $id
312 ];
313
314 return Transport::instance()->call(
316 $query
317 );
318 }
319
327 public static function getSiteList(array $query = [])
328 {
329 $query['onPageSize'] = (int)($query['pageSize'] ?? 50);
330 $query['page'] = (int)($query['page'] ?? 1);
331
332 return Transport::instance()->call(
334 $query
335 );
336 }
337
338 public static function getAppPublic($code, $version = false, $checkHash = false, $installHash = false)
339 {
340 $queryFields = [
341 "code" => $code
342 ];
343
344 $version = intval($version);
345 if($version > 0)
346 {
347 $queryFields["ver"] = $version;
348 }
349
350 if($checkHash !== false)
351 {
352 $queryFields["check_hash"] = $checkHash;
353 $queryFields["install_hash"] = $installHash;
354 }
355
356 return Transport::instance()->call(
358 $queryFields
359 );
360 }
361
362 public static function filterApp($fields, $page = false)
363 {
364 if (!is_array($fields))
365 $fields = array($fields);
366
367 $queryFields = $fields;
368
369 $page = intval($page);
370 if($page > 0)
371 {
372 $queryFields["page"] = $page;
373 }
374
375 return Transport::instance()->call(
377 $queryFields
378 );
379 }
380
381 public static function searchApp($q, $page = false)
382 {
383 $q = trim($q);
384
385 $queryFields = Array(
386 "q" => $q
387 );
388
389 $page = intval($page);
390 if($page > 0)
391 {
392 $queryFields["page"] = $page;
393 }
394
395 return Transport::instance()->call(
397 $queryFields
398 );
399 }
400
401 public static function getInstall($code, $version = false, $checkHash = false, $installHash = false)
402 {
403 $queryFields = Array(
404 "code" => $code,
405 "encode_key" => "Y",
406 "member_id" => \CRestUtil::getMemberId(),
407 );
408
409 $version = intval($version);
410 if($version > 0)
411 {
412 $queryFields["ver"] = $version;
413 }
414
415 if($checkHash !== false)
416 {
417 $queryFields["check_hash"] = $checkHash;
418 $queryFields["install_hash"] = $installHash;
419 }
420
421 return Transport::instance()->call(Transport::METHOD_GET_INSTALL, $queryFields);
422 }
423
424 public static function getBuyLink($num, $appCode)
425 {
426 $linkTpl = static::$buyLinkList['en'];
427
428 if(\Bitrix\Main\ModuleManager::isModuleInstalled('bitrix24'))
429 {
430 $linkTpl = static::$buyLinkList['bitrix24'];
431 }
432 else
433 {
434 if(array_key_exists(LANGUAGE_ID, static::$buyLinkList))
435 {
436 $linkTpl = static::$buyLinkList[LANGUAGE_ID];
437 }
438 elseif(array_key_exists(Loc::getDefaultLang(LANGUAGE_ID), static::$buyLinkList))
439 {
440 $linkTpl = static::$buyLinkList[Loc::getDefaultLang(LANGUAGE_ID)];
441 }
442 }
443
444 return str_replace(
445 array('#NUM#', '#CODE#'),
446 array(intval($num), urlencode($appCode)),
447 $linkTpl
448 );
449 }
450
451 public static function getNumUpdates()
452 {
453 $appCodes = array();
454 $dbApps = AppTable::getList(array(
455 'filter' => array(
456 "=ACTIVE" => AppTable::ACTIVE,
457 "!=STATUS" => AppTable::STATUS_LOCAL,
458 ),
459 'select' => array('CODE', 'VERSION')
460 ));
461 while($app = $dbApps->fetch())
462 {
463 $appCodes[$app["CODE"]] = $app["VERSION"];
464 }
465
466 if(!empty($appCodes))
467 {
468 $updateList = static::getUpdates($appCodes);
469
470 if (is_array($updateList) && isset($updateList['ITEMS']))
471 {
472 self::setAvailableUpdate($updateList['ITEMS']);
473 }
474 else
475 {
477 }
478 }
479
480 return __CLASS__."::getNumUpdates();";
481 }
482
483 public static function getTagByPlacement($placement)
484 {
485 $tag = array();
486 if($placement <> '')
487 {
488 if(mb_substr($placement, 0, 4) === 'CRM_' || $placement === \Bitrix\Rest\Api\UserFieldType::PLACEMENT_UF_TYPE)
489 {
490 if($placement !== 'CRM_ROBOT_TRIGGERS')
491 {
492 $tag[] = 'placement';
493 }
494 else
495 {
496 $tag[] = 'automation';
497 }
498
499 $tag[] = 'crm';
500 }
501 elseif(mb_substr($placement, 0, 5) === 'CALL_')
502 {
503 $tag[] = 'placement';
504 $tag[] = 'telephony';
505 }
506 }
507
508 $tag[] = $placement;
509
510 return $tag;
511 }
512
516 public static function isSubscriptionAvailable()
517 {
518 if (ModuleManager::isModuleInstalled('bitrix24'))
519 {
520 $status = Option::get('bitrix24', '~mp24_paid', 'N');
521 }
522 else
523 {
524 $status = Option::get('main', '~mp24_paid', 'N');
525 if ($status === 'T' && Option::get('main', '~mp24_used_trial', 'N') !== 'Y')
526 {
527 Option::set('main', '~mp24_used_trial', 'Y');
528 }
529 }
530
531 $result = ($status === 'Y' || $status === 'T');
532
533 if (
534 $status === 'Y'
536 && Loader::includeModule('bitrix24')
537 && \CBitrix24::getLicenseFamily() === 'project'
538 && Option::get('rest', 'can_use_subscription_project', 'N') === 'N'
539 )
540 {
541 $result = false;
542 }
543 elseif($result)
544 {
545 $date = static::getSubscriptionFinalDate();
546 if ($date)
547 {
548 $now = new \Bitrix\Main\Type\Date();
549 if ($date < $now)
550 {
551 $result = false;
552 }
553 }
554 }
555
556 return $result;
557 }
558
559 public static function isStartDemoSubscription(): bool
560 {
561 if (ModuleManager::isModuleInstalled('bitrix24'))
562 {
563 return Option::get('bitrix24', '~mp24_paid', 'N') === 'T'
564 && Option::get('bitrix24', '~mp24_used_trial', 'N') === 'Y';
565 }
566
567 return false;
568 }
569
570 public static function getSubscriptionFinalDate(): ?Date
571 {
572 $result = null;
573
574 if (ModuleManager::isModuleInstalled('bitrix24'))
575 {
576 $timestamp = (int)Option::get('bitrix24', '~mp24_paid_date');
577 }
578 else
579 {
580 $timestamp = (int)Option::get('main', '~mp24_paid_date');
581 }
582
583 if ($timestamp > 0)
584 {
585 $result = Date::createFromTimestamp($timestamp);
586 }
587
588 return $result;
589 }
590
596 public static function isSubscriptionDemo(): bool
597 {
598 if (ModuleManager::isModuleInstalled('bitrix24'))
599 {
600 $status = Option::get('bitrix24', '~mp24_paid', 'N');
601 }
602 else
603 {
604 $status = Option::get('main', '~mp24_paid', 'N');
605 }
606
607 return $status === 'T';
608 }
609
610 private static function checkSubscriptionAccessStart($region): bool
611 {
612 $canStart = true;
613 if (!empty(static::SUBSCRIPTION_DEFAULT_START_TIME[$region]))
614 {
615 $time = Option::get(
616 'rest',
617 'subscription_region_start_time_' . $region,
618 static::SUBSCRIPTION_DEFAULT_START_TIME[$region]
619 );
620 $canStart = $time < time();
621 }
622
623 return $canStart && in_array($region, static::SUBSCRIPTION_REGION, true);
624 }
625
626 public static function isSubscriptionAccess()
627 {
628 if (ModuleManager::isModuleInstalled('bitrix24') && Loader::includeModule('bitrix24'))
629 {
630 $result = static::checkSubscriptionAccessStart(\CBitrix24::getLicensePrefix());
631 }
632 else
633 {
634 $result = Option::get(Access::MODULE_ID, Access::OPTION_SUBSCRIPTION_AVAILABLE, 'N') === 'Y';
635 }
636
637 return $result;
638 }
639
640 public static function canBuySubscription()
641 {
642 $result = false;
643 if (
644 static::isSubscriptionAccess()
646 && !(
648 && Loader::includeModule('bitrix24')
649 && !Feature::isFeatureEnabled('rest_can_buy_subscription')
650 )
651 )
652 {
653 $result = true;
654 }
655
656 return $result;
657 }
658
659 public static function isSubscriptionDemoAvailable()
660 {
661 if (ModuleManager::isModuleInstalled('bitrix24'))
662 {
663 $used = Option::get('bitrix24', '~mp24_used_trial', 'N') === 'Y';
664 }
665 else
666 {
667 $used = Option::get('main', '~mp24_used_trial', 'N') === 'Y';
668 }
669
670 return !$used && static::isSubscriptionAccess();
671 }
672
677 public static function isPayApplicationAvailable() : bool
678 {
679 if (is_null(static::$isPayApplicationAvailable))
680 {
681 static::$isPayApplicationAvailable = true;
682 $time = (int) Option::get('rest', 'time_pay_application_off', 1621029600);
683 if (time() > $time)
684 {
685 if (Loader::includeModule('bitrix24'))
686 {
687 $region = \CBitrix24::getLicensePrefix();
688 }
689 else
690 {
691 $region = Option::get('main', '~PARAM_CLIENT_LANG', '');
692 }
693
694 if ($region === 'ru')
695 {
696 static::$isPayApplicationAvailable = false;
697 }
698 }
699 }
700
701 return static::$isPayApplicationAvailable;
702 }
703
707 public static function onChangeSubscriptionDate(Event $event): void
708 {
709 if (static::isSubscriptionAvailable())
710 {
711 if (self::isStartDemoSubscription())
712 {
713 $eventDemo = new Event(
714 'rest',
715 'onSubscriptionIsDemo',
716 );
717
718 $eventDemo->send();
719 }
720
721 $event = new Event(
722 'rest',
723 'onSubscriptionRenew',
724 );
725
726 EventManager::getInstance()->send($event);
727 }
728 }
729}
static getDefaultLang($lang)
Definition loc.php:460
static isModuleInstalled($moduleName)
const STATUS_LOCAL
Definition app.php:74
const OPTION_SUBSCRIPTION_AVAILABLE
Definition access.php:34
static searchApp($q, $page=false)
Definition client.php:381
static getTop($action, $fields=array())
Definition client.php:46
static getAvailableUpdate($code=false)
Definition client.php:132
static setAvailableUpdate($updateList=array())
Definition client.php:105
static getBuyLink($num, $appCode)
Definition client.php:424
static getCategories($forceReload=false)
Definition client.php:207
static getUpdates($codeList)
Definition client.php:93
static getCategory($code, $page=false, $pageSize=false)
Definition client.php:213
static getBuy($codeList)
Definition client.php:76
static getApp($code, $version=false, $checkHash=false, $installHash=false)
Definition client.php:278
static getByTag($tag, $page=false, $pageSize=false)
Definition client.php:235
static getAppPublic($code, $version=false, $checkHash=false, $installHash=false)
Definition client.php:338
static filterApp($fields, $page=false)
Definition client.php:362
static getTagByPlacement($placement)
Definition client.php:483
static onChangeSubscriptionDate(Event $event)
Definition client.php:707
static getSiteList(array $query=[])
Definition client.php:327
static getInstall($code, $version=false, $checkHash=false, $installHash=false)
Definition client.php:401
static getLastByTag($tag, $page=false, $pageSize=false)
Definition client.php:257
static getCategoriesFull($forceReload=false)
Definition client.php:158