Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
app.php
1<?php
2namespace Bitrix\Rest;
3
15
16Loc::loadMessages(__FILE__);
17
18
58class AppTable extends Main\Entity\DataManager
59{
60 const ACTIVE = 'Y';
61 const INACTIVE = 'N';
62 const INSTALLED = 'Y';
63 const NOT_INSTALLED = 'N';
64 const TRIALED = 'Y';
65 const NOT_TRIALED = 'N';
66
67 const TYPE_STANDARD = 'N';
68 const TYPE_ONLY_API = 'A';
69 const TYPE_CONFIGURATION = 'C';
70 const TYPE_SMART_ROBOTS = 'R';
71
72 const MODE_SITE = 'S';
73
74 const STATUS_LOCAL = 'L';
75 const STATUS_FREE = 'F';
76 const STATUS_PAID = 'P';
77 const STATUS_DEMO = 'D';
78 const STATUS_TRIAL = 'T';
80
82 const PAID_GRACE_PERIOD = -14;
83
84 const CACHE_TTL = 86400;
85 const CACHE_PATH = '/rest/app/';
86
87 private static $skipRemoteUpdate = false;
88
89 protected static $licenseLang = null;
90
91 protected static $applicationCache = array();
92
93 protected static $localAppDeniedScope = array(
94 'landing_cloud', 'rating',
95 );
96
102 public static function getTableName()
103 {
104 return 'b_rest_app';
105 }
106
112 public static function getMap()
113 {
114 return array(
115 'ID' => array(
116 'data_type' => 'integer',
117 'primary' => true,
118 'autocomplete' => true,
119 ),
120 'CLIENT_ID' => array(
121 'data_type' => 'string',
122 'required' => true,
123 'validation' => array(__CLASS__, 'validateClientId'),
124 ),
125 'CODE' => array(
126 'data_type' => 'string',
127 'required' => true,
128 'validation' => array(__CLASS__, 'validateCode'),
129 ),
130 'ACTIVE' => array(
131 'data_type' => 'boolean',
132 'values' => array(static::INACTIVE, static::ACTIVE),
133 ),
134 'INSTALLED' => array(
135 'data_type' => 'boolean',
136 'values' => array(static::NOT_INSTALLED, static::INSTALLED),
137 ),
138 'URL' => array(
139 'data_type' => 'string',
140 'validation' => array(__CLASS__, 'validateUrl'),
141 ),
142 'URL_DEMO' => array(
143 'data_type' => 'string',
144 'validation' => array(__CLASS__, 'validateUrlDemo'),
145 ),
146 'URL_INSTALL' => array(
147 'data_type' => 'string',
148 'validation' => array(__CLASS__, 'validateUrlInstall'),
149 ),
150 'VERSION' => array(
151 'data_type' => 'string',
152 'validation' => array(__CLASS__, 'validateVersion'),
153 ),
154 'SCOPE' => array(
155 'data_type' => 'string',
156 'required' => true,
157 'validation' => array(__CLASS__, 'validateScope'),
158 ),
159 'STATUS' => array(
160 'data_type' => 'enum',
161 'required' => true,
162 'values' => array(
163 static::STATUS_LOCAL,
164 static::STATUS_FREE,
165 static::STATUS_PAID,
166 static::STATUS_DEMO,
167 static::STATUS_TRIAL,
168 static::STATUS_SUBSCRIPTION,
169 ),
170 ),
171 'DATE_FINISH' => array(
172 'data_type' => 'date',
173 ),
174 'IS_TRIALED' => array(
175 'data_type' => 'boolean',
176 'values' => array(static::NOT_TRIALED, static::TRIALED),
177 ),
178 'SHARED_KEY' => array(
179 'data_type' => 'string',
180 'validation' => array(__CLASS__, 'validateSharedKey'),
181 ),
182 'CLIENT_SECRET' => array(
183 'data_type' => 'string',
184 'validation' => array(__CLASS__, 'validateClientSecret'),
185 ),
186 'APP_NAME' => array(
187 'data_type' => 'string',
188 'validation' => array(__CLASS__, 'validateAppName'),
189 ),
190 'ACCESS' => array(
191 'data_type' => 'string',
192 'validation' => array(__CLASS__, 'validateAccess'),
193 ),
194 'APPLICATION_TOKEN' => array(
195 'data_type' => 'string',
196 ),
197 'MOBILE' => array(
198 'data_type' => 'boolean',
199 'values' => array(static::INACTIVE, static::ACTIVE),
200 ),
201 'USER_INSTALL' => array(
202 'data_type' => 'boolean',
203 'values' => array(static::INACTIVE, static::ACTIVE),
204 ),
205 'LANG' => array(
206 'data_type' => 'Bitrix\Rest\AppLangTable',
207 'reference' => array(
208 '=this.ID' => 'ref.APP_ID',
209 '=ref.LANGUAGE_ID' => new Main\DB\SqlExpression('?s', LANGUAGE_ID),
210 ),
211 ),
212 'LANG_DEFAULT' => array(
213 'data_type' => 'Bitrix\Rest\AppLangTable',
214 'reference' => array(
215 '=this.ID' => 'ref.APP_ID',
216 '=ref.LANGUAGE_ID' => new Main\DB\SqlExpression('?s', Loc::getDefaultLang(LANGUAGE_ID)),
217 ),
218 ),
219 'LANG_LICENSE' => array(
220 'data_type' => 'Bitrix\Rest\AppLangTable',
221 'reference' => array(
222 '=this.ID' => 'ref.APP_ID',
223 '=ref.LANGUAGE_ID' => new Main\DB\SqlExpression('?s', static::getLicenseLanguage()),
224 ),
225 ),
226 (new OneToMany('LANG_ALL', AppLangTable::class, 'APP'))
227 ->configureJoinType('left')
228 );
229 }
230
236 public static function setSkipRemoteUpdate($v)
237 {
238 static::$skipRemoteUpdate = $v;
239 }
240
247 public static function onBeforeAdd(Main\Entity\Event $event)
248 {
249 $result = new Main\Entity\EventResult();
250 $data = $event->getParameters();
251
252 if($data['fields']['STATUS'] == static::STATUS_LOCAL && !$data['fields']['CLIENT_ID'])
253 {
254 $rnd = Main\Security\Random::getString(8);
255 $dummyClientId = 'no_client_id_'.$rnd;
256 $dummyClientSecret = 'no_client_secret_'.$rnd;
257
258 $result->modifyFields(array(
259 "CLIENT_ID" => $dummyClientId,
260 "CLIENT_SECRET" => $dummyClientSecret,
261 "CODE" => $dummyClientId,
262 ));
263 }
264
265 return $result;
266 }
267
275 public static function onAfterAdd(Main\Entity\Event $event)
276 {
277 EventController::onAddApp($event);
278 $data = $event->getParameters();
279 if(!static::$skipRemoteUpdate)
280 {
281 if(
282 $data['fields']['STATUS'] === static::STATUS_LOCAL
283 && OAuthService::getEngine()->isRegistered()
284 )
285 {
286 try
287 {
288 $appFields = array(
289 'TITLE' => $data['fields']['APP_NAME'],
290 'REDIRECT_URI' => $data['fields']['URL'],
291 'SCOPE' => $data['fields']['SCOPE'],
292 );
293
294 $clientData = OAuthService::getEngine()
295 ->getClient()
296 ->addApplication($appFields);
297 }
298 catch(Main\SystemException $e)
299 {
300 $clientData = array(
301 "error" => $e->getCode(),
302 "error_description" => $e->getMessage(),
303 );
304 }
305
306 if(is_array($clientData))
307 {
308 if($clientData['result'])
309 {
310 static::$skipRemoteUpdate = true;
311
312 static::clearClientCache($clientData['result']['client_id']);
313
314 $updateResult = static::update($data['id'], array(
315 'CLIENT_ID' => $clientData['result']['client_id'],
316 'CLIENT_SECRET' => $clientData['result']['client_secret'],
317 'STATUS' => static::STATUS_LOCAL,
318 'SHARED_KEY' => md5(\CRestUtil::getMemberId().$clientData['result']['client_secret']),
319 'CODE' => $clientData['result']['client_id'],
320 ));
321 static::$skipRemoteUpdate = false;
322
323 if($updateResult->isSuccess())
324 {
325 return true;
326 }
327 else
328 {
329 $clientData = array('error' => $updateResult->getErrorMessages());
330 }
331 }
332 }
333 else
334 {
335 $clientData = array('error' => 'Unknown error');
336 }
337
338 static::$skipRemoteUpdate = true;
339 static::delete($data['id']);
340 static::$skipRemoteUpdate = false;
341
342 throw new OAuthException($clientData);
343 }
344 }
345
346 if($data['fields']['STATUS'] !== static::STATUS_LOCAL)
347 {
348 \Bitrix\Rest\Engine\Access::getActiveEntity(true);
349 }
350
351 return true;
352 }
353
361 public static function onAfterUpdate(Main\Entity\Event $event)
362 {
363 $data = $event->getParameters();
364 static::clearClientCache($data['primary']['ID']);
365
366 if(!static::$skipRemoteUpdate)
367 {
368 if(
369 isset($data['fields']['STATUS'])
370 && $data['fields']['STATUS'] === static::STATUS_LOCAL
371 && OAuthService::getEngine()->isRegistered()
372 )
373 {
374 $app = static::getByClientId($data['primary']['ID']);
375
376 try
377 {
378 $updateResult = OAuthService::getEngine()
379 ->getClient()
380 ->updateApplication(array(
381 "CLIENT_ID" => $app['CLIENT_ID'],
382 'TITLE' => $data['fields']['APP_NAME'],
383 'REDIRECT_URI' => $data['fields']['URL'],
384 'SCOPE' => $data['fields']['SCOPE'],
385 ));
386
387 if($updateResult['result'])
388 {
389 return true;
390 }
391 }
392 catch(Main\SystemException $e)
393 {
394 $updateResult = array(
395 "error" => $e->getCode(),
396 "error_description" => $e->getMessage(),
397 );
398 }
399
400 throw new OAuthException($updateResult);
401 }
402 }
403
404 if(isset($data['fields']['STATUS']) && $data['fields']['STATUS'] !== static::STATUS_LOCAL)
405 {
406 \Bitrix\Rest\Engine\Access::getActiveEntity(true);
407 }
408
409 return true;
410 }
411
417 public static function onDelete(Main\Entity\Event $event)
418 {
419 if(!static::$skipRemoteUpdate)
420 {
421 $data = $event->getParameters();
422 $app = static::getByClientId($data['primary']['ID']);
423
424 if($app['STATUS'] == AppTable::STATUS_LOCAL)
425 {
426 if(OAuthService::getEngine()->isRegistered())
427 {
428 \CRestUtil::cleanApp($app["ID"], true);
429
430 try
431 {
433 ->getClient()
434 ->deleteApplication(array(
435 'CLIENT_ID' => $app['CLIENT_ID'],
436 ));
437 }
438 catch(\Bitrix\Main\SystemException $e)
439 {
440 }
441 }
442 }
443 }
444 }
445
451 public static function onAfterDelete(Main\Entity\Event $event)
452 {
453 $data = $event->getParameters();
454
455 static::clearClientCache($data['primary']['ID']);
456 AppLangTable::deleteByApp($data['primary']['ID']);
457 }
458
459 public static function install($appId)
460 {
461 $appInfo = static::getByClientId($appId);
462 if($appInfo)
463 {
464 $eventFields = array(
465 'APP_ID' => $appId,
466 'VERSION' => $appInfo['VERSION'],
467 'ACTIVE' => $appInfo['ACTIVE'],
468 'INSTALLED' => $appInfo['INSTALLED'],
469 );
470
471 if ($appInfo['ACTIVE'] === self::ACTIVE && $appInfo['INSTALLED'] === self::INSTALLED)
472 {
473 $res = PlacementTable::getList(
474 [
475 'filter' => [
476 '=APP_ID' => $appInfo['ID'],
477 ],
478 'select' => [
479 'ID',
480 'PLACEMENT',
481 'USER_ID',
482 ],
483 ]
484 );
485 while ($item = $res->fetch())
486 {
487 $event = new Event(
488 'rest',
490 [
491 'ID' => $item['ID'],
492 'PLACEMENT' => $item['PLACEMENT'],
493 'USER_ID' => $item['USER_ID'],
494 ]
495 );
496 EventManager::getInstance()->send($event);
497 }
498 }
499
500 foreach(GetModuleEvents("rest", "OnRestAppInstall", true) as $eventHandler)
501 {
502 ExecuteModuleEventEx($eventHandler, array($eventFields));
503 }
504 }
505 }
506
513 public static function uninstall($appId, $clean = 0)
514 {
515 $appInfo = static::getByClientId($appId);
516 if($appInfo)
517 {
518 \CRestUtil::cleanApp($appId, $clean);
519
520 if($appInfo['STATUS'] !== static::STATUS_LOCAL)
521 {
522 OAuthService::getEngine()->getClient()->unInstallApplication(array(
523 'CLIENT_ID' => $appInfo['CLIENT_ID']
524 ));
525 }
526 }
527 }
528
536 public static function checkUninstallAvailability($appId, $clean = 0)
537 {
538 $event = new Main\Event('rest', 'onBeforeApplicationUninstall', [
539 'ID' => $appId,
540 'CLEAN' => $clean
541 ]);
542 $event->send();
543
544 $result = new Main\ErrorCollection();
545 if ($event->getResults())
546 {
548 foreach ($event->getResults() as $eventResult)
549 {
550 if($eventResult->getType() === EventResult::ERROR)
551 {
552 $eventResultData = $eventResult->getParameters();
553 if ($eventResultData instanceof Main\Error)
554 {
555 $result->add([$eventResultData]);
556 }
557 }
558 }
559 }
560
561 return $result;
562 }
563
571 public static function updateAppStatusInfo()
572 {
573 $appList = OAuthService::getEngine()->getClient()->getApplicationList();
574
575 if(is_array($appList) && is_array($appList['result']))
576 {
577 $dbApps = static::getList(array(
578 'filter' => array(
579 '!=STATUS' => static::STATUS_LOCAL,
580 ),
581 'select' => array(
582 'ID', 'CLIENT_ID', 'STATUS', 'DATE_FINISH',
583 )
584 ));
585
586 $localApps = array();
587 while($app = $dbApps->fetch())
588 {
589 $localApps[$app['CLIENT_ID']] = $app;
590 }
591
592 foreach($appList['result'] as $app)
593 {
594 if(array_key_exists($app['client_id'], $localApps))
595 {
596 $dateFinishLocal = $localApps[$app['client_id']]['DATE_FINISH']
597 ? $localApps[$app['client_id']]['DATE_FINISH']->getTimestamp()
598 : '';
599 $dateFinishRemote = ($app['date_finish'] ?? null) ? Main\Type\Date::createFromTimestamp($app['date_finish'])->getTimestamp() : '';
600
601 if(
602 $localApps[$app['client_id']]['STATUS'] !== $app['status']
603 || $dateFinishRemote != $dateFinishLocal
604 )
605 {
606 $appFields = array(
607 'STATUS' => $app['status'],
608 'DATE_FINISH' => $app['date_finish']
609 ? Main\Type\Date::createFromTimestamp($app['date_finish'])
610 : '',
611 );
612
613 static::setSkipRemoteUpdate(true);
614 $result = static::update($localApps[$app['client_id']]['ID'], $appFields);
615 static::setSkipRemoteUpdate(false);
616
617 if(
618 $result->isSuccess()
619 && $appFields['STATUS'] === static::STATUS_PAID
620 )
621 {
622 static::callAppPaymentEvent($localApps[$app['client_id']]['ID']);
623 }
624 }
625 }
626 else
627 {
628 $appFields = array(
629 'CLIENT_ID' => $app['client_id'],
630 'CODE' => $app['code'],
631 'ACTIVE' => $app['active'] ? static::ACTIVE : static::INACTIVE,
632 'INSTALLED' => static::NOT_INSTALLED,
633 'VERSION' => $app['version'],
634 'STATUS' => $app['status'],
635 'SCOPE' => $app['scope'],
636 );
637
638 if(!empty($app['date_finish']))
639 {
640 $appFields['DATE_FINISH'] = Main\Type\Date::createFromTimestamp($app['date_finish']);
641 }
642
643 $result = static::add($appFields);
644
645 if($result->isSuccess() && $appFields['STATUS'] === static::STATUS_PAID)
646 {
647 static::callAppPaymentEvent($result->getId());
648 }
649 }
650 }
651 }
652 }
653
659 public static function callAppPaymentEvent($appId)
660 {
661 // for compatibility purpose module_id is bitrix24 here
662 foreach(GetModuleEvents('bitrix24', 'OnAfterAppPaid', true) as $event)
663 {
664 ExecuteModuleEventEx($event, array($appId));
665 }
666 }
667
675 public static function getAppStatusInfo($app, $detailUrl)
676 {
677 $res = array();
678
679 if (
680 !empty($app)
681 && (
682 is_string($app)
683 || is_integer($app)
684 )
685 )
686 {
687 $appInfo = $app = static::getByClientId($app);
688 }
689 elseif (isset($app['CODE']))
690 {
691 $appInfo = static::getByClientId($app['CODE']);
692 }
693
694 if(is_array($app))
695 {
696 $res['STATUS'] = $app['STATUS'];
697 $res['PAYMENT_NOTIFY'] = 'N';
698 $res['PAYMENT_EXPIRED'] = 'N';
699 $res['PAYMENT_ALLOW'] = 'Y';
700
701 if ($app['STATUS'] === self::STATUS_SUBSCRIPTION)
702 {
703 if (!\Bitrix\Rest\Marketplace\Client::isSubscriptionAvailable())
704 {
705 $res['MESSAGE_REPLACE'] = array(
706 '#DETAIL_URL#' => $detailUrl,
707 '#DAYS#' => 0,
708 '#CODE#' => urlencode($app['CODE'])
709 );
710 $res['PAYMENT_NOTIFY'] = 'Y';
711 $res['PAYMENT_EXPIRED'] = 'Y';
712 $res['PAYMENT_ALLOW'] = 'N';
713 }
714 else
715 {
716 $dateFinish = \Bitrix\Rest\Marketplace\Client::getSubscriptionFinalDate();
717 if (!is_null($dateFinish))
718 {
719 $res['DAYS_LEFT'] = floor(($dateFinish->getTimestamp() - \CTimeZone::getOffset() - time()) / 86400);
720 if($res['DAYS_LEFT'] < 0)
721 {
722 $res['MESSAGE_REPLACE'] = array(
723 '#DETAIL_URL#' => $detailUrl,
724 '#DAYS#' => $res['DAYS_LEFT'],
725 '#CODE#' => urlencode($app['CODE'])
726 );
727 $res['PAYMENT_NOTIFY'] = 'Y';
728 $res['PAYMENT_EXPIRED'] = 'Y';
729 $res['PAYMENT_ALLOW'] = 'N';
730 }
731 elseif ($res['DAYS_LEFT'] < static::PAID_NOTIFY_DAYS)
732 {
733 $res['MESSAGE_REPLACE'] = array(
734 '#DETAIL_URL#' => $detailUrl,
735 '#DAYS#' => $res['DAYS_LEFT'],
736 '#CODE#' => urlencode($app['CODE'])
737 );
738 $res['PAYMENT_NOTIFY'] = 'Y';
739 }
740 }
741 }
742 }
743 elseif($app['DATE_FINISH'] <> '' && $app['STATUS'] != self::STATUS_FREE)
744 {
745 $res['DAYS_LEFT'] = floor(
746 (MakeTimeStamp($app['DATE_FINISH']) - \CTimeZone::getOffset() - time()) / 86400
747 );
748
749 if(
750 $res['DAYS_LEFT'] < static::PAID_NOTIFY_DAYS
751 || $app['STATUS'] == static::STATUS_TRIAL)
752 {
753 $res['PAYMENT_NOTIFY'] = 'Y';
754
755 if($res['DAYS_LEFT'] < 0)
756 {
757 $res['PAYMENT_EXPIRED'] = 'Y';
758
759 if($app['STATUS'] == static::STATUS_TRIAL)
760 {
761 $res['PAYMENT_ALLOW'] = 'N';
762 }
763 elseif(
764 $app['STATUS'] == static::STATUS_PAID
765 && $res['DAYS_LEFT'] < static::PAID_GRACE_PERIOD
766 )
767 {
768 if($app['IS_TRIALED'] == 'N' && $app['URL_DEMO'] <> '')
769 {
770 $res['STATUS'] = static::STATUS_DEMO;
771 }
772 else
773 {
774 $res['PAYMENT_ALLOW'] = 'N';
775 }
776 }
777 }
778 }
779
780 $res['MESSAGE_REPLACE'] = array(
781 "#DETAIL_URL#" => $detailUrl,
782 "#DAYS#" => $res['DAYS_LEFT'],
783 "#CODE#" => urlencode($app['CODE']),
784 );
785 }
786 elseif($app['STATUS'] == self::STATUS_DEMO)
787 {
788 $res['PAYMENT_NOTIFY'] = 'Y';
789 $res['MESSAGE_REPLACE'] = array(
790 "#DETAIL_URL#" => $detailUrl,
791 "#CODE#" => urlencode($app['CODE'])
792 );
793 }
794 else
795 {
796 $res['MESSAGE_REPLACE'] = array(
797 "#DETAIL_URL#" => $detailUrl,
798 "#CODE#" => urlencode($app['CODE'])
799 );
800 }
801
802 $res['MESSAGE_SUFFIX'] = '_'.$res['STATUS'].'_'.$res['PAYMENT_EXPIRED'].'_'.$res['PAYMENT_ALLOW'];
803
804 }
805
806 if (!empty($appInfo['CLIENT_ID']))
807 {
808 $isHold = \Bitrix\Rest\Engine\Access\HoldEntity::is(
809 \Bitrix\Rest\Engine\Access\HoldEntity::TYPE_APP,
810 $appInfo['CLIENT_ID']
811 );
812 if ($isHold)
813 {
814 $res['MESSAGE_SUFFIX'] = '_HOLD_OVERLOAD';
815 $res['PAYMENT_NOTIFY'] = 'Y';
816 }
817 }
818
819 return $res;
820 }
821
832 public static function getStatusMessage($suffix, $replace = null, $checkAdmin = true, $language = null)
833 {
834 if ($checkAdmin && \CRestUtil::isAdmin())
835 {
836 $suffix .= '_A';
837 }
838
839 if (
840 is_array($replace)
841 && array_key_exists('#DAYS#', $replace)
842 && (
843 is_int($replace['#DAYS#'])
844 || preg_match('/^(-|)\d+$/', $replace['#DAYS#'])
845 )
846 )
847 {
848 $replace['#DAYS#'] = FormatDate('ddiff', time(), time() + 24 * 60 * 60 * $replace['#DAYS#']);
849 }
850
851 return Loc::getMessage('PAYMENT_MESSAGE' . $suffix, $replace, $language);
852 }
853
858 public static function getAccess($appId)
859 {
860 $appInfo = static::getByClientId($appId);
861 if($appInfo)
862 {
863 if($appInfo['ACCESS'] <> '')
864 {
865 $rightsList = explode(",", $appInfo["ACCESS"]);
866
867 $access = new \CAccess();
868 $accessNames = $access->getNames($rightsList);
869
870 $result = array();
871 foreach($rightsList as $right)
872 {
873 $result[$right] = array(
874 "id" => $right,
875 "provider" => $accessNames[$right]["provider_id"],
876 "name" => $accessNames[$right]["name"]
877 );
878 }
879 return $result;
880 }
881 }
882
883 return false;
884 }
885
891 public static function setAccess($appId, $newRights = array())
892 {
893 $appInfo = static::getByClientId($appId);
894 if($appInfo)
895 {
896 $rights = '';
897 if(is_array($newRights) && !empty($newRights))
898 {
899 $rightIdList = array();
900 foreach($newRights as $key => $rightsList)
901 {
902 foreach($rightsList as $rightId => $ar)
903 {
904 $rightIdList[] = $rightId;
905 }
906 }
907 $rights = implode(",", $rightIdList);
908 }
909
910 static::update($appId, array(
911 'ACCESS' => $rights,
912 ));
913 }
914
915 if(defined("BX_COMP_MANAGED_CACHE"))
916 {
917 global $CACHE_MANAGER;
918 $CACHE_MANAGER->ClearByTag('bitrix24_left_menu');
919 }
920 }
921
929 public static function getByClientId($clientId)
930 {
931 if(!array_key_exists($clientId, static::$applicationCache))
932 {
933 if(strval(intval($clientId)) == $clientId)
934 {
935 $filter = array('=ID' => $clientId);
936 }
937 else
938 {
939 $filter = array(
940 array(
941 'LOGIC' => 'OR',
942 '=CODE' => $clientId,
943 '=CLIENT_ID' => $clientId,
944 ),
945 );
946 }
947
948 $dbRes = static::getList(
949 [
950 'filter' => $filter,
951 'select' => [
952 '*',
953 'MENU_NAME' => 'LANG.MENU_NAME',
954 'MENU_NAME_DEFAULT' => 'LANG_DEFAULT.MENU_NAME',
955 'MENU_NAME_LICENSE' => 'LANG_LICENSE.MENU_NAME',
956 ],
957 'limit' => 1,
958 ]
959 );
960
961 foreach ($dbRes->fetchCollection() as $app)
962 {
963 $appInfo = [
964 'ID' => $app->getId(),
965 'MENU_NAME' => !is_null($app->getLang()) ? $app->getLang()->getMenuName() : '',
966 'MENU_NAME_DEFAULT' => !is_null($app->getLangDefault()) ? $app->getLangDefault()->getMenuName() : '',
967 'MENU_NAME_LICENSE' => !is_null($app->getLangLicense()) ? $app->getLangLicense()->getMenuName() : '',
968 ];
969 foreach ($app->sysGetEntity()->getScalarFields() as $field)
970 {
971 $fieldName = $field->getName();
972 if ($field instanceof BooleanField)
973 {
974 $appInfo[$fieldName] = $app->get($fieldName) ? 'Y' : 'N';
975 }
976 else
977 {
978 $appInfo[$fieldName] = $app->get($fieldName);
979 }
980 }
981 $app->fillLangAll();
982 if (!is_null($app->getLangAll()))
983 {
984 foreach ($app->getLangAll() as $lang)
985 {
986 $appInfo['LANG_ALL'][$lang->getLanguageId()] = [
987 'MENU_NAME' => $lang->getMenuName(),
988 ];
989 }
990 }
991 if ($appInfo['MENU_NAME'] === '')
992 {
993 $appInfo = Lang::mergeFromLangAll($appInfo);
994 }
995 }
996
997 if (isset($appInfo) && is_array($appInfo))
998 {
999 static::$applicationCache[$appInfo['ID']] = $appInfo;
1000 static::$applicationCache[$appInfo['CLIENT_ID']] = $appInfo;
1001 static::$applicationCache[$appInfo['CODE']] = $appInfo;
1002 }
1003 }
1004
1005 return static::$applicationCache[$clientId];
1006 }
1007
1008 protected static function clearClientCache($clientId)
1009 {
1010 if(array_key_exists($clientId, static::$applicationCache))
1011 {
1012 $app = static::$applicationCache[$clientId];
1013 if(is_array($app))
1014 {
1015 unset(static::$applicationCache[$app['ID']]);
1016 unset(static::$applicationCache[$app['CLIENT_ID']]);
1017 }
1018 else
1019 {
1020 unset(static::$applicationCache[$clientId]);
1021 }
1022 }
1023 }
1024
1025 protected static function getLicenseLanguage()
1026 {
1027 if(static::$licenseLang === null)
1028 {
1029 if(Main\Loader::includeModule('bitrix24'))
1030 {
1031 static::$licenseLang = \CBitrix24::getLicensePrefix();
1032 }
1033 else
1034 {
1035 $dbSites = \CSite::getList('sort', 'asc', array('DEFAULT' => 'Y', 'ACTIVE' => 'Y'));
1036 $site = $dbSites->fetch();
1037
1038 static::$licenseLang = is_array($site) && isset($site['LANGUAGE_ID']) ? $site['LANGUAGE_ID'] : LANGUAGE_ID;
1039 }
1040
1041 if(static::$licenseLang === null)
1042 {
1043 static::$licenseLang = 'en';
1044 }
1045 }
1046
1047 return static::$licenseLang;
1048 }
1049
1050
1056 public static function validateClientId()
1057 {
1058 return array(
1059 new Main\Entity\Validator\Length(null, 128),
1060 new Main\Entity\Validator\Unique(),
1061 );
1062 }
1063
1069 public static function validateCode()
1070 {
1071 return array(
1072 new Main\Entity\Validator\Length(null, 128),
1073 new Main\Entity\Validator\Unique(),
1074 );
1075 }
1076
1082 public static function validateUrl()
1083 {
1084 return array(
1085 new Main\Entity\Validator\Length(null, 1000),
1086 );
1087 }
1088
1094 public static function validateUrlDemo()
1095 {
1096 return array(
1097 new Main\Entity\Validator\Length(null, 1000),
1098 );
1099 }
1100
1106 public static function validateUrlInstall()
1107 {
1108 return array(
1109 new Main\Entity\Validator\Length(null, 1000),
1110 function ($value, $primary, array $row, Main\Entity\Field $field)
1111 {
1112 $checkResult = true;
1113
1114 if($value)
1115 {
1116 try
1117 {
1118 if(!HandlerHelper::checkCallback($value, $row, false))
1119 {
1120 $checkResult = false;
1121 }
1122 }
1123 catch(RestException $e)
1124 {
1125 $checkResult = false;
1126 }
1127
1128 if(!$checkResult)
1129 {
1130 return Loc::getMessage("MP_ERROR_INCORRECT_URL_INSTALL");
1131 }
1132 }
1133
1134 return true;
1135 }
1136 );
1137 }
1138
1144 public static function validateVersion()
1145 {
1146 return array(
1147 new Main\Entity\Validator\Length(null, 4),
1148 );
1149 }
1150
1156 public static function validateScope()
1157 {
1158 return array(
1159 new Main\Entity\Validator\Length(null, 2000),
1160 );
1161 }
1162
1168 public static function validateSharedKey()
1169 {
1170 return array(
1171 new Main\Entity\Validator\Length(null, 32),
1172 );
1173 }
1174
1180 public static function validateClientSecret()
1181 {
1182 return array(
1183 new Main\Entity\Validator\Length(null, 100),
1184 );
1185 }
1186
1192 public static function validateAppName()
1193 {
1194 return array(
1195 new Main\Entity\Validator\Length(null, 1000),
1196 );
1197 }
1198
1204 public static function validateAccess()
1205 {
1206 return array(
1207 new Main\Entity\Validator\Length(null, 2000),
1208 );
1209 }
1210
1215 public static function cleanLocalPermissionList(array $permissionList)
1216 {
1217 foreach($permissionList as $key => $perm)
1218 {
1219 if(in_array($perm, static::$localAppDeniedScope))
1220 {
1221 unset($permissionList[$key]);
1222 }
1223 }
1224
1225 return array_values($permissionList);
1226 }
1227
1233 public static function canUninstallByType($code, $version = false)
1234 {
1235 $result = true;
1236
1237 $type = static::getAppType($code, $version);
1238 if($type == static::TYPE_CONFIGURATION)
1239 {
1240 $appList = \Bitrix\Rest\Configuration\Helper::getInstance()->getBasicAppList();
1241 if(in_array($code, $appList))
1242 {
1243 $result = false;
1244 }
1245 }
1246
1247 return $result;
1248 }
1249
1255 public static function getAppType($code, $version = false)
1256 {
1257 $result = false;
1258 $cache = Cache::createInstance();
1259 if ($cache->initCache(static::CACHE_TTL, 'appType'.md5($code.$version), static::CACHE_PATH))
1260 {
1261 $result = $cache->getVars();
1262 }
1263 elseif ($cache->startDataCache())
1264 {
1265 $appDetailInfo = Client::getInstall($code, $version);
1266 $result = ($appDetailInfo['ITEMS']['TYPE'])?:false;
1267 $cache->endDataCache($result);
1268 }
1269
1270 return $result;
1271 }
1272}
static loadMessages($file)
Definition loc.php:64
static getDefaultLang($lang)
Definition loc.php:460
static getMessage($code, $replace=null, $language=null)
Definition loc.php:29
static deleteByApp($appId)
Definition applang.php:77
static validateClientId()
Definition app.php:1056
static getStatusMessage($suffix, $replace=null, $checkAdmin=true, $language=null)
Definition app.php:832
static validateUrl()
Definition app.php:1082
const TYPE_SMART_ROBOTS
Definition app.php:70
const STATUS_PAID
Definition app.php:76
const NOT_TRIALED
Definition app.php:65
static getMap()
Definition app.php:112
static canUninstallByType($code, $version=false)
Definition app.php:1233
static validateScope()
Definition app.php:1156
static getAppType($code, $version=false)
Definition app.php:1255
static getAppStatusInfo($app, $detailUrl)
Definition app.php:675
static onBeforeAdd(Main\Entity\Event $event)
Definition app.php:247
static clearClientCache($clientId)
Definition app.php:1008
static validateAccess()
Definition app.php:1204
static onAfterDelete(Main\Entity\Event $event)
Definition app.php:451
static install($appId)
Definition app.php:459
static validateUrlInstall()
Definition app.php:1106
static getByClientId($clientId)
Definition app.php:929
const TYPE_CONFIGURATION
Definition app.php:69
static validateAppName()
Definition app.php:1192
const PAID_GRACE_PERIOD
Definition app.php:82
static $localAppDeniedScope
Definition app.php:93
const STATUS_TRIAL
Definition app.php:78
const TYPE_STANDARD
Definition app.php:67
static $licenseLang
Definition app.php:89
const STATUS_LOCAL
Definition app.php:74
static uninstall($appId, $clean=0)
Definition app.php:513
static onDelete(Main\Entity\Event $event)
Definition app.php:417
static validateUrlDemo()
Definition app.php:1094
const PAID_NOTIFY_DAYS
Definition app.php:81
static getLicenseLanguage()
Definition app.php:1025
const STATUS_DEMO
Definition app.php:77
static getAccess($appId)
Definition app.php:858
static onAfterUpdate(Main\Entity\Event $event)
Definition app.php:361
const NOT_INSTALLED
Definition app.php:63
static $applicationCache
Definition app.php:91
static cleanLocalPermissionList(array $permissionList)
Definition app.php:1215
static validateSharedKey()
Definition app.php:1168
const STATUS_FREE
Definition app.php:75
static setAccess($appId, $newRights=array())
Definition app.php:891
const STATUS_SUBSCRIPTION
Definition app.php:79
static onAfterAdd(Main\Entity\Event $event)
Definition app.php:275
static setSkipRemoteUpdate($v)
Definition app.php:236
const TYPE_ONLY_API
Definition app.php:68
static updateAppStatusInfo()
Definition app.php:571
static callAppPaymentEvent($appId)
Definition app.php:659
static validateVersion()
Definition app.php:1144
static validateClientSecret()
Definition app.php:1180
static getTableName()
Definition app.php:102
static validateCode()
Definition app.php:1069
static checkCallback($handlerUrl, $appInfo=array(), $checkInstallUrl=true)
static mergeFromLangAll($data)
Definition lang.php:59