1C-Bitrix 25.700.0
Загрузка...
Поиск...
Не найдено
provider.php
См. документацию.
1<?php
2
3namespace Bitrix\Rest\Preset;
4
5use Bitrix\Main\SystemException;
6use Bitrix\Main\UserTable;
7use Bitrix\Main\Loader;
8use Bitrix\Main\Type\DateTime;
9use Bitrix\Main\Localization\Loc;
10use Bitrix\Main\Security\Random;
11use Bitrix\Main\Entity\ReferenceField;
12use Bitrix\Rest\APAuth\PermissionTable;
13use Bitrix\Rest\Engine\Access\HoldEntity;
14use Bitrix\Rest\Enum\Integration\ElementCodeType;
15use Bitrix\Rest\Lang;
16use Bitrix\Rest\PlacementLangTable;
17use Bitrix\Rest\Preset\Data\Element;
18use Bitrix\Rest\Preset\Data\Rest;
19use Bitrix\Rest\EventTable;
20use Bitrix\Rest\AppTable;
21use Bitrix\Rest\APAuth\PasswordTable;
22use Bitrix\Rest\PlacementTable;
23use Bitrix\Rest\AppLangTable;
24use Bitrix\Rest\Event\Sender;
25use Bitrix\Rest\OAuthService;
26use Bitrix\Rest\Analytic;
27use Bitrix\Im\Model\BotTable;
28use Bitrix\Im\Bot;
29
34class Provider
35{
37 public const URI_METHOD_INFO = '';
39 public const URI_EXAMPLE_DOWNLOAD = '';
40 public const APP_MODE_SERVER = 'SERVER';
41 public const APP_MODE_ZIP = 'ZIP';
42
52 public static function deleteIntegration($id)
53 {
54 $result = [
55 'result' => 'success'
56 ];
57 $errorList = [];
58
59 $res = IntegrationTable::getList(
60 [
61 'filter' => [
62 '=ID' => $id
63 ],
64 'select' => [
65 'ID',
66 'APP_ID',
67 'BOT_ID',
68 'PASSWORD_ID',
69 'USER_ID',
70 'ELEMENT_CODE',
71 'PASS' => 'PASSWORD.PASSWORD'
72 ],
73 'limit' => 1
74 ]
75 );
76 if ($integration = $res->fetch())
77 {
78 global $USER;
79 if ($integration['USER_ID'] === $USER->GetID() || \CRestUtil::isAdmin())
80 {
81 $filterEvent = [
82 '=INTEGRATION_ID' => $integration['ID']
83 ];
84 if ($integration['BOT_ID'] > 0 && Loader::includeModule('im'))
85 {
86 $res = BotTable::getList(
87 [
88 'filter' => [
89 '=BOT_ID' => $integration['BOT_ID'],
90 ]
91 ]
92 );
93 if ($bot = $res->fetch())
94 {
95 $filterEvent = [
96 'LOGIC' => 'OR',
97 $filterEvent,
98 [
99 '=APP_ID' => '',
100 '=APPLICATION_TOKEN' => $bot['APP_ID'],
101 ]
102 ];
103 Bot::unRegister(
104 [
105 'BOT_ID' => $integration['BOT_ID'],
106 'MODULE_ID' => 'rest'
107 ]
108 );
109 }
110 }
111 if (($integration['PASSWORD_ID'] > 0) && !static::deleteWebHook($integration['PASSWORD_ID']))
112 {
113 $errorList[] = Loc::getMessage('INTEGRATION_PRESET_PROVIDER_ERROR_DELETE_WEBHOOK');
114 }
115 $resEvent = EventTable::getList(
116 [
117 'filter' => $filterEvent,
118 'select' => [
119 'ID'
120 ]
121 ]
122 );
123 while ($event = $resEvent->fetch())
124 {
125 $res = EventTable::delete($event['ID']);
126 if (!$res->isSuccess())
127 {
128 $errorList[] = $res->getErrorMessages();
129 }
130 }
131 $res = AppTable::getList(
132 [
133 'filter' => [
134 '=ID' => $integration['APP_ID']
135 ],
136 'select' => [
137 'ID'
138 ]
139 ]
140 );
141 if ($app = $res->fetch())
142 {
143 $resPlacement = PlacementTable::getList(
144 [
145 'filter' => [
146 '=APP_ID' => $app['ID']
147 ],
148 'select' => [
149 'ID'
150 ]
151 ]
152 );
153 while ($placement = $resPlacement->fetch())
154 {
155 $res = PlacementTable::delete($placement['ID']);
156 if (!$res->isSuccess())
157 {
158 $errorList[] = $res->getErrorMessages();
159 }
160 }
161 $res = AppTable::delete($app['ID']);
162 if (!$res->isSuccess())
163 {
164 $errorList[] = $res->getErrorMessages();
165 }
166 }
167
168 if (empty($errorList))
169 {
170 $res = IntegrationTable::delete($integration['ID']);
171 if (!$res->isSuccess())
172 {
173 $errorList[] = $res->getErrorMessages();
174 }
175 else if (ElementCodeType::IN_WEBHOOK->value === $integration['ELEMENT_CODE'])
176 {
177 if (HoldEntity::is(HoldEntity::TYPE_WEBHOOK, $integration['PASS']))
178 {
179 HoldEntity::delete(HoldEntity::TYPE_WEBHOOK, $integration['PASS']);
180 HoldEntity::checkBlockCode(HoldEntity::TYPE_WEBHOOK);
181 }
182 }
183 }
184 }
185 else
186 {
187 $errorList[] = Loc::getMessage('INTEGRATION_PRESET_PROVIDER_ERROR_ACCESS_DENIED');
188 }
189 }
190
191 if (!empty($errorList))
192 {
193 $result['result'] = 'error';
194 $result['errors'] = $errorList;
195 }
196
197 \Bitrix\Rest\Engine\Access::getActiveEntity(true);
198
199 return $result;
200 }
201
211 public static function getIntegration($id)
212 {
213 $id = intVal($id);
214 $return = [];
215 $runtime = [
216 new ReferenceField(
217 'APPLICATION_DATA',
218 AppTable::class,
219 [
220 '=ref.ID' => 'this.APP_ID'
221 ],
222 [
223 'join_type' => 'LEFT'
224 ]
225 ),
226 new ReferenceField(
227 'PASSWORD_DATA',
228 PasswordTable::class,
229 [
230 '=ref.ID' => 'this.PASSWORD_ID'
231 ],
232 [
233 'join_type' => 'LEFT'
234 ]
235 ),
236 ];
237 $select = [
238 '*',
239 'APPLICATION_DATA_' => 'APPLICATION_DATA',
240 'PASSWORD_DATA_' => 'PASSWORD_DATA'
241 ];
242 if (Loader::includeModule('im'))
243 {
244 $runtime[] = new ReferenceField(
245 'BOT_DATA',
246 BotTable::class,
247 [
248 '=ref.BOT_ID' => 'this.BOT_ID'
249 ],
250 [
251 'join_type' => 'LEFT'
252 ]
253 );
254 $select['BOT_DATA_'] = 'BOT_DATA';
255
256 $runtime[] = new ReferenceField(
257 'BOT_ACCOUNT',
258 UserTable::class,
259 [
260 '=ref.ID' => 'this.BOT_ID'
261 ],
262 [
263 'join_type' => 'LEFT'
264 ]
265 );
266 $select['BOT_ACCOUNT_NAME'] = 'BOT_ACCOUNT.NAME';
267 }
268
269 $res = IntegrationTable::getList(
270 [
271 'filter' => [
272 '=ID' => $id
273 ],
274 'limit' => 1,
275 'select' => $select,
276 'runtime' => $runtime
277 ]
278 );
279 if ($item = $res->fetch())
280 {
281 if ($item['PASSWORD_DATA_ID'] > 0)
282 {
283 $item['PASSWORD_DATA_URL'] = \CRestUtil::getWebhookEndpoint(
284 $item['PASSWORD_DATA_PASSWORD'],
285 $item['PASSWORD_DATA_USER_ID']
286 );
287 }
288
289 if (!empty($item['BOT_DATA_APP_ID']) && mb_strpos($item['BOT_DATA_APP_ID'], 'custom') === 0)
290 {
291 //clear 'custom' prefix only for webhook bot
292 $item['BOT_DATA_APP_ID'] = mb_substr($item['BOT_DATA_APP_ID'], 6);
293 }
294
295 if ($item['APP_ID'] > 0 && $item['APPLICATION_ONLY_API'] == 'N')
296 {
297 $resLang = AppLangTable::getList(
298 [
299 'filter' => [
300 '=APP_ID' => $item['APP_ID']
301 ]
302 ]
303 );
304 while ($lang = $resLang->fetch())
305 {
306 $item['APPLICATION_LANG_DATA'][$lang['LANGUAGE_ID']] = $lang['MENU_NAME'];
307 }
308 }
309
310 if ($item['APP_ID'] > 0 && !empty($item['WIDGET_LIST']))
311 {
312 $resLang = PlacementTable::getList(
313 [
314 'filter' => [
315 '=APP_ID' => $item['APP_ID'],
316 ],
317 'select' => [
318 'ID',
319 'LANG_ALL',
320 ],
321 ]
322 );
323 foreach ($resLang->fetchCollection() as $placement)
324 {
325 if (!is_null($placement->getLangAll()))
326 {
327 foreach ($placement->getLangAll() as $lang)
328 {
329 $item['WIDGET_LANG_LIST'][$lang->getLanguageId()] = [
330 'TITLE' => $lang->getTitle(),
331 'DESCRIPTION' => $lang->getDescription(),
332 'GROUP_NAME' => $lang->getGroupName(),
333 ];
334 }
335 }
336 }
337 }
338
339 $return = $item;
340 }
341
342 return $return;
343 }
344
356 public static function saveIntegration($requestData, $elementCode = '', $id = 0)
357 {
358 global $USER;
359 $result = [
360 'status' => true,
361 ];
362 $itemsEvent = [];
363 $errorList = [];
364 $id = (isset($requestData['ID']) && intVal($requestData['ID']) > 0) ? intVal($requestData['ID']) : $id;
365 $userId = $GLOBALS['USER']->getID();
366 $isAdmin = \CRestUtil::isAdmin();
367
368 $presetData = Element::get($elementCode);
369
370 if (
371 !$isAdmin
372 &&
373 (
374 $presetData['ADMIN_ONLY'] === 'Y'
375 || $presetData['OPTIONS']['WIDGET_NEEDED'] !== 'D'
376 || $presetData['OPTIONS']['APPLICATION_NEEDED'] !== 'D'
377 )
378 )
379 {
380 $result['status'] = false;
381 $result['errors'][] = Loc::getMessage('INTEGRATION_PRESET_PROVIDER_ERROR_ACCESS_DENIED');
382 return $result;
383 }
384
385 if (!OAuthService::getEngine()->isRegistered())
386 {
387 try
388 {
390 OAuthService::getEngine()->getClient()->getApplicationList();
391 }
392 catch (SystemException $e)
393 {
394 $result['status'] = false;
395 $result['errors'][] = $e->getCode() . ': ' . $e->getMessage();
396 return $result;
397 }
398 }
399
400 $presetData = $presetData['OPTIONS'];
401
402 $saveData = [
403 'ELEMENT_CODE' => $elementCode,
404 'USER_ID' => $USER->GetID(),
405 'TITLE' => $requestData['TITLE'],
406 'SCOPE' => is_array($requestData['SCOPE']) ? $requestData['SCOPE'] : [],
407 'QUERY' => $requestData['QUERY'],
408 'OUTGOING_HANDLER_URL' => trim($requestData['OUTGOING_HANDLER_URL'] ?? null),
409 'OUTGOING_EVENTS' => isset($requestData['OUTGOING_EVENTS']) && is_array($requestData['OUTGOING_EVENTS']) ? $requestData['OUTGOING_EVENTS'] : [],
410 'APPLICATION_ONLY_API' => (isset($requestData['APPLICATION_ONLY_API']) && $requestData['APPLICATION_ONLY_API'] === 'Y') ? 'Y' : 'N',
411 'APPLICATION_NEEDED' => (isset($requestData['APPLICATION_NEEDED']) && $requestData['APPLICATION_NEEDED'] === 'Y') ? 'Y' : 'N',
412 'APPLICATION_EVENTS' => (isset($requestData['APPLICATION_EVENTS']) && is_array($requestData['APPLICATION_EVENTS'])) ? $requestData['APPLICATION_EVENTS'] : [],
413 'OUTGOING_NEEDED' => (isset($requestData['OUTGOING_NEEDED']) && $requestData['OUTGOING_NEEDED'] === 'Y') ? 'Y' : 'N',
414 'WIDGET_NEEDED' => (isset($requestData['WIDGET_NEEDED']) && $requestData['WIDGET_NEEDED'] === 'Y') ? 'Y' : 'N',
415 'WIDGET_HANDLER_URL' => trim($requestData['WIDGET_HANDLER_URL'] ?? null),
416 'WIDGET_LIST' => $requestData['WIDGET_LIST'] ?? null,
417 'WIDGET_LANG_LIST' => (isset($requestData['WIDGET_LANG_LIST']) && is_array($requestData['WIDGET_LANG_LIST'])) ? $requestData['WIDGET_LANG_LIST'] : [],
418 'BOT_HANDLER_URL' => trim($requestData['BOT_HANDLER_URL'] ?? null)
419 ];
420
421 if ($id > 0)
422 {
423 $itemsEvent = EventTable::getList(
424 [
425 'filter' => [
426 '=INTEGRATION_ID' => $id
427 ]
428 ]
429 )->fetchAll();
430 }
431
432 if (!empty($itemsEvent['0']['APPLICATION_TOKEN']))
433 {
434 $saveData['APPLICATION_TOKEN'] = $itemsEvent['0']['APPLICATION_TOKEN'];
435 }
436 else
437 {
438 $saveData['APPLICATION_TOKEN'] = Random::getString(32);
439 }
440
441 $isAdd = false;
442 if ($id == 0)
443 {
444 $saveResult = IntegrationTable::add($saveData);
445 $isAdd = true;
446 $id = $saveResult->getId();
447 if (!$result['status'] = $saveResult->isSuccess())
448 {
449 $errorList = $saveResult->getErrorMessages();
450 }
451 }
452
453 if ($id > 0 || $result['status'])
454 {
456 if (!empty($saveData['TITLE']))
457 {
458 $title = $saveData['TITLE'];
459 }
460 else
461 {
462 $title = Loc::getMessage('INTEGRATION_PRESET_PROVIDER_TITLE_PREFIX', ['#ID#' => $id]);
463 }
464
465 if (!$isAdd)
466 {
467 $resIntegration = IntegrationTable::getList(
468 [
469 'filter' => [
470 'ID' => $id
471 ]
472 ]
473 );
474 if ($integrationData = $resIntegration->fetch())
475 {
476 if (!$isAdmin && $integrationData['USER_ID'] != $userId)
477 {
478 $result['status'] = false;
479 $result['errors'][] = Loc::getMessage('INTEGRATION_PRESET_PROVIDER_ERROR_ACCESS_DENIED');
480 return $result;
481 }
482 if ($integrationData['PASSWORD_ID'] > 0 && ($requestData['MODE'] === 'GEN_SAVE' || $integrationData['USER_ID'] != $userId))
483 {
485 'integrationRegen',
486 'integration' . $integrationData['ID'],
487 $integrationData['ELEMENT_CODE'],
488 'code'
489 );
490 if (static::deleteWebHook($integrationData['PASSWORD_ID']))
491 {
492 $integrationData['PASSWORD_ID'] = 0;
493 }
494 else
495 {
496 $result['status'] = false;
497 $errorList[] = Loc::getMessage('INTEGRATION_PRESET_PROVIDER_ERROR_DELETE_WEBHOOK');
498 return $result;
499 }
500 }
501
502 $saveData = array_merge($integrationData, $saveData);
503 }
504 //bot on webhooks
505 if ($presetData['BOT_NEEDED'] !== 'D' && Loader::includeModule('im'))
506 {
507 if (!empty($requestData['BOT_NAME']) && !empty($requestData['BOT_HANDLER_URL']))
508 {
509 $botId = 0;
510 $allEvents = [
511 'onImBotMessageAdd',
512 'onImBotJoinChat',
513 'onImBotDelete',
514 'onImBotMessageUpdate',
515 'onImBotMessageDelete'
516 ];
517 $params = [
518 'TYPE' => $requestData['BOT_TYPE'],
519 'EVENT_MESSAGE_ADD' => $saveData['BOT_HANDLER_URL'],
520 'EVENT_WELCOME_MESSAGE' => $saveData['BOT_HANDLER_URL'],
521 'EVENT_BOT_DELETE' => $saveData['BOT_HANDLER_URL'],
522 'MODULE_ID' => 'rest',
523 'PROPERTIES' => [
524 'NAME' => $requestData['BOT_NAME']
525 ]
526 ];
527
528 if ($integrationData['BOT_ID'] > 0)
529 {
530 $res = BotTable::getList(
531 [
532 'filter' => [
533 '=BOT_ID' => $integrationData['BOT_ID'],
534 ]
535 ]
536 );
537 if ($bot = $res->fetch())
538 {
539 $params = array_merge($bot, $params);
540 }
541 }
542 $clientId = $params['APP_ID'];
543 if (!$params['APP_ID'])
544 {
545 $clientId = Random::getString(32);
546 $params['APP_ID'] = 'custom' . $clientId;
547 $params['CODE'] = Random::getString(16);
548 }
549 elseif (mb_stripos($clientId, 'custom') === 0)
550 {
551 $clientId = mb_substr($clientId, 6);
552 }
553
554 $uriWithClientId = $saveData['BOT_HANDLER_URL']
555 . (mb_strpos($saveData['BOT_HANDLER_URL'], '?') === false ? '?' : '&')
556 . 'CLIENT_ID=' . $clientId;
557 $events = [
558 'onImBotMessageAdd' => $uriWithClientId,
559 'onImBotJoinChat' => $uriWithClientId,
560 'onImBotDelete' => $uriWithClientId,
561 ];
562 if (in_array($requestData['BOT_TYPE'], ['S', 'O']))
563 {
564 $events['onImBotMessageUpdate'] = $uriWithClientId;
565 $events['onImBotMessageDelete'] = $uriWithClientId;
566 $params['EVENT_MESSAGE_UPDATE'] = $uriWithClientId;
567 $params['EVENT_MESSAGE_DELETE'] = $uriWithClientId;
568 }
569
570 if ($integrationData['BOT_ID'] > 0)
571 {
572 if (Bot::update(['BOT_ID' => $integrationData['BOT_ID']], $params) === true)
573 {
574 $botId = $integrationData['BOT_ID'];
575 }
576 }
577 else
578 {
579 $botId = Bot::register($params);
580 }
581
582 if ($botId > 0)
583 {
584 $allEvents = array_change_key_case(array_combine($allEvents, $allEvents), CASE_UPPER);
585 $saveData['BOT_ID'] = $botId;
586 if ($integrationData['BOT_ID'] > 0)
587 {
588 $res = EventTable::getList(
589 [
590 'filter' => [
591 '=APP_ID' => '',
592 '=EVENT_NAME' => array_keys($allEvents),
593 '=APPLICATION_TOKEN' => [
594 $clientId,
595 $params['APP_ID'],
596 ],
597 ],
598 'select' => [
599 'ID',
600 'EVENT_NAME',
601 'EVENT_HANDLER'
602 ]
603 ]
604 );
605 while ($event = $res->fetch())
606 {
607 if (isset($events[$allEvents[$event['EVENT_NAME']]]) &&
608 $event['EVENT_HANDLER'] === $events[$allEvents[$event['EVENT_NAME']]])
609 {
610 unset($events[$allEvents[$event['EVENT_NAME']]]);
611 }
612 else
613 {
614 EventTable::delete($event['ID']);
615 }
616 }
617 }
618 if ($params['APP_ID'])
619 {
620 foreach ($events as $event => $eventHandler)
621 {
622 $res = EventTable::add(
623 [
624 'APP_ID' => '',
625 'EVENT_NAME' => mb_strtoupper($event),
626 'EVENT_HANDLER' => $eventHandler,
627 'APPLICATION_TOKEN' => $clientId,
628 'USER_ID' => 0,
629 ]
630 );
631 if ($result['status'] = $res->isSuccess())
632 {
633 Sender::bind('im', $event);
634 }
635 else
636 {
637 $errors = $res->getErrorMessages();
638 if (is_array($errors))
639 {
640 $errorList = array_merge($errorList, $errors);
641 }
642 break;
643 }
644 }
645 }
646 }
647 else
648 {
649 $errorList[] = Loc::getMessage('INTEGRATION_PRESET_PROVIDER_ERROR_BOT_CREATE');
650 }
651 }
652 else
653 {
654 $errorList[] = Loc::getMessage(
655 'INTEGRATION_PRESET_PROVIDER_ERROR_BOT_REQUIRED_FIELD_EMPTY'
656 );
657 }
658 }
659 }
660
661 if (!isset($presetData['QUERY_NEEDED']) || $presetData['QUERY_NEEDED'] !== 'D')
662 {
663 $webhook = static::getWebHook($saveData['SCOPE'], $saveData['PASSWORD_ID'] ?? null, $title);
664 $saveData['PASSWORD_ID'] = $webhook['ID'];
665 }
666
667 if (
668 $presetData['OUTGOING_NEEDED'] !== 'D'
669 && !empty($saveData['OUTGOING_HANDLER_URL'])
670 && !empty($saveData['OUTGOING_EVENTS'])
671 && $saveData['OUTGOING_NEEDED'] == 'Y'
672 )
673 {
674 $eventAddList = [];
675 $eventUpdateList = [];
676 if (!empty($itemsEvent))
677 {
678 $itemsEvent = array_column($itemsEvent, null, 'EVENT_NAME');
679 }
680
681 foreach ($saveData['OUTGOING_EVENTS'] as $event)
682 {
683 if ($itemsEvent[$event])
684 {
685 $eventUpdateList[] = $itemsEvent[$event]['ID'];
686 unset($itemsEvent[$event]);
687 }
688 else
689 {
690 $eventAddList[] = [
691 'TITLE' => $title,
692 'EVENT_NAME' => $event,
693 'EVENT_HANDLER' => $saveData['OUTGOING_HANDLER_URL'],
694 'USER_ID' => $userId,
695 'DATE_CREATE' => new DateTime(),
696 'APPLICATION_TOKEN' => $saveData['APPLICATION_TOKEN'],
697 'INTEGRATION_ID' => $saveData['ID']
698 ];
699 }
700 }
701
702 foreach ($itemsEvent as $event)
703 {
704 EventTable::delete($event['ID']);
705 }
706
707 foreach ($eventAddList as $item)
708 {
709 $res = EventTable::add($item);
710 if (!$result['status'] = $res->isSuccess())
711 {
712 $errors = $res->getErrorMessages();
713 if (is_array($errors))
714 {
715 $errorList = array_merge($errorList, $errors);
716 }
717 break;
718 }
719 }
720
721 if ($result['status'] && !empty($eventUpdateList))
722 {
723 $res = EventTable::updateMulti(
724 $eventUpdateList,
725 [
726 'TITLE' => $title,
727 'EVENT_HANDLER' => $saveData['OUTGOING_HANDLER_URL'],
728 'USER_ID' => $userId,
729 'APPLICATION_TOKEN' => $saveData['APPLICATION_TOKEN'],
730 ]
731 );
732 if (!$result['status'] = $res->isSuccess())
733 {
734 $errors = $res->getErrorMessages();
735 if (is_array($errors))
736 {
737 $errorList = array_merge($errorList, $errors);
738 }
739 }
740 }
741 }
742 else
743 {
744 foreach ($itemsEvent as $event)
745 {
746 EventTable::delete($event['ID']);
747 }
748 }
749
750 if (
751 $presetData['WIDGET_NEEDED'] !== 'D'
752 && $saveData['WIDGET_NEEDED'] == 'Y'
753 && !empty($saveData['WIDGET_HANDLER_URL'])
754 && is_array($saveData['WIDGET_LIST'])
755 )
756 {
757 $app = static::saveApp(
758 [
759 'ID' => $saveData['APP_ID'],
760 'FIELDS' => [
761 'URL' => $saveData['WIDGET_HANDLER_URL'],
762 'URL_INSTALL' => $saveData['WIDGET_HANDLER_URL'],
763 'SCOPE' => $saveData['SCOPE'],
764 'ONLY_API' => 'Y',
765 'MOBILE' => 'N',
766 'APP_NAME' => $saveData['TITLE'],
767 ],
768 'PLACEMENTS' => $saveData['WIDGET_LIST'],
769 'PLACEMENTS_LANG_LIST' => $saveData['WIDGET_LANG_LIST'],
770 'PLACEMENT_HANDLER_URL' => $saveData['WIDGET_HANDLER_URL'],
771 'INTEGRATION_CODE' => $saveData['ELEMENT_CODE'],
772 'INTEGRATION_ID' => $saveData['ID']
773 ]
774 );
775 if ($app['ID'] > 0)
776 {
777 $saveData['APP_ID'] = $app['ID'];
778 }
779
780 if (!empty($app['errors']))
781 {
782 $errorList = array_merge($errorList, $app['errors']);
783 }
784 }
785 elseif (
786 $presetData['APPLICATION_NEEDED'] !== 'D'
787 && $saveData['APPLICATION_NEEDED'] == 'Y'
788 )
789 {
790 $app = static::saveApp(
791 [
792 'ID' => $saveData['APP_ID'],
793 'FIELDS' => [
794 'URL' => trim($requestData['APPLICATION_URL_HANDLER']),
795 'URL_INSTALL' => trim($requestData['APPLICATION_URL_INSTALL']),
796 'SCOPE' => $saveData['SCOPE'],
797 'ONLY_API' => ($saveData['APPLICATION_ONLY_API'] == 'Y') ? 'Y' : 'N',
798 'MOBILE' => ($saveData['APPLICATION_ONLY_API'] != 'Y'
799 && $requestData['APPLICATION_MOBILE'] === 'Y') ? 'Y' : 'N',
800 'APP_NAME' => $saveData['TITLE'],
801 ],
802 'LANG_NAME' => ($saveData['APPLICATION_ONLY_API'] != 'Y' && is_array($requestData['APPLICATION_LANG_NAME'])) ?
803 $requestData['APPLICATION_LANG_NAME']
804 :
805 [],
806 'INTEGRATION_CODE' => $saveData['ELEMENT_CODE'],
807 'INTEGRATION_ID' => $saveData['ID']
808 ]
809 );
810
811 if ($app['ID'] > 0)
812 {
813 $saveData['APP_ID'] = $app['ID'];
814 }
815
816 if (!empty($app['errors']))
817 {
818 $errorList = array_merge($errorList, $app['errors']);
819 }
820 }
821 elseif (isset($saveData['APP_ID']) && $saveData['APP_ID'] > 0)
822 {
823 $res = AppTable::getList(
824 [
825 'filter' => [
826 '=ID' => $saveData['APP_ID']
827 ]
828 ]
829 );
830 if ($app = $res->fetch())
831 {
832 AppTable::delete($app['ID']);
833 }
834 $placementList = PlacementTable::getList(
835 [
836 'filter' => [
837 '=APP_ID' => $app['ID']
838 ]
839 ]
840 )->fetchAll();
841 if (is_array($placementList))
842 {
843 foreach ($placementList as $placement)
844 {
845 PlacementTable::delete($placement['ID']);
846 }
847 }
848 }
849
850 $resultSave = IntegrationTable::update($id, $saveData);
851 if (!$resultSave->isSuccess())
852 {
853 $errors = $resultSave->getErrorMessages();
854 if (is_array($errors))
855 {
856 $errorList = array_merge($errorList, $errors);
857 }
858 }
859
861 }
862
863 $result['ID'] = $id;
864 if (!empty($errorList))
865 {
866 $result['status'] = false;
867 $result['errors'] = $errorList;
868 }
869 \Bitrix\Rest\Engine\Access::getActiveEntity(true);
870
871 return $result;
872 }
873
874 private static function saveApp($data)
875 {
876 $app = [];
877 $return = [];
878 $errorList = [];
879 if ($data['ID'] > 0)
880 {
881 $dbApp = AppTable::getList(
882 [
883 'filter' => [
884 '=ID' => $data['ID'],
885 ],
886 'limit' => 1
887 ]
888 );
889 if ($app = $dbApp->fetch())
890 {
891 $data['FIELDS'] = array_merge($app, $data['FIELDS']);
892 }
893 }
894
895 if (count($data['FIELDS']['SCOPE']) <= 0)
896 {
897 $errorList[] = Loc::getMessage('INTEGRATION_PRESET_PROVIDER_ERROR_EMPTY_SCOPE');
898 }
899
900 if (empty($errorList))
901 {
902 foreach (GetModuleEvents('rest', 'OnRestLocalAppSave', true) as $eventHandler)
903 {
904 $eventResult = ExecuteModuleEventEx($eventHandler, [$app, &$data['FIELDS']]);
905 if ($eventResult !== null)
906 {
907 $errorList[] = strip_tags($eventResult);
908 }
909 }
910 }
911
912 if (empty($errorList) && empty($data['FIELDS']['URL']))
913 {
914 $errorList[] = Loc::getMessage('INTEGRATION_PRESET_PROVIDER_ERROR_INCORRECT_URL');
915 }
916
917 if (empty($errorList))
918 {
919 try
920 {
921 $appFields = [
922 'URL' => $data['FIELDS']['URL'],
923 'URL_INSTALL' => $data['FIELDS']['URL_INSTALL'],
924 'CLIENT_ID' => $data['FIELDS']['CLIENT_ID'],
925 'CODE' => $data['FIELDS']['CLIENT_ID'],
926 'SCOPE' => implode(',', $data['FIELDS']['SCOPE']),
927 'STATUS' => AppTable::STATUS_LOCAL,
928 'APP_NAME' => $data['FIELDS']['APP_NAME'],
929 'MOBILE' => $data['FIELDS']['MOBILE'],
930 ];
931 if ($app['ID'] > 0)
932 {
933 $result = AppTable::update($app['ID'], $appFields);
934 }
935 else
936 {
937 $appFields['INSTALLED'] = (!empty($data['FIELDS']['URL_INSTALL'])
938 && $data['FIELDS']['ONLY_API'] !== 'Y') ? AppTable::NOT_INSTALLED : AppTable::INSTALLED;
939 $result = AppTable::add($appFields);
940 if ($result->isSuccess())
941 {
942 Analytic::logToFile(
943 'integrationAppCreated',
944 'integration' . $data['INTEGRATION_ID'],
945 $data['INTEGRATION_CODE'],
946 'code'
947 );
948 }
949 }
950
951 if ($result->isSuccess())
952 {
953 $return['ID'] = $result->getId();
954
955 AppLangTable::deleteByApp($return['ID']);
956 if ($data['FIELDS']['ONLY_API'] === 'N')
957 {
958 foreach ($data['LANG_NAME'] as $lang => $name)
959 {
960 AppLangTable::add(
961 [
962 'APP_ID' => $return['ID'],
963 'LANGUAGE_ID' => $lang,
964 'MENU_NAME' => $name
965 ]
966 );
967 }
968 }
969 else
970 {
971 if (
972 $data['FIELDS']['ONLY_API'] === 'Y'
973 && !empty($app['URL_INSTALL'])
974 && $data['FIELDS']['URL_INSTALL'] != $app['URL_INSTALL']
975 )
976 {
977 $event = EventTable::getList(
978 [
979 'filter' => [
980 '=APP_ID' => $return['ID'],
981 '=EVENT_NAME' => 'ONAPPINSTALL',
982 ],
983 'limit' => 1
984 ]
985 );
986 if ($eventInstall = $event->fetch())
987 {
988 // checkCallback is already called inside checkFields
989 $result = EventTable::update(
990 $eventInstall['ID'],
991 [
992 'APP_ID' => $return['ID'],
993 'EVENT_NAME' => 'ONAPPINSTALL',
994 'EVENT_HANDLER' => $data['FIELDS']['URL_INSTALL'],
995 ]
996 );
997 }
998 }
999
1000 if (!empty($data['FIELDS']['URL_INSTALL']) && empty($app['URL_INSTALL']))
1001 {
1002 // checkCallback is already called inside checkFields
1003 $result = EventTable::add(
1004 [
1005 'APP_ID' => $return['ID'],
1006 'EVENT_NAME' => 'ONAPPINSTALL',
1007 'EVENT_HANDLER' => $data['FIELDS']['URL_INSTALL'],
1008 ]
1009 );
1010 if ($result->isSuccess())
1011 {
1012 Sender::bind('rest', 'OnRestAppInstall');
1013 }
1014 }
1015
1016 if ($app['ID'] <= 0)
1017 {
1018 AppTable::install($return['ID']);
1019 }
1020 }
1021
1022 if (defined('BX_COMP_MANAGED_CACHE'))
1023 {
1024 global $CACHE_MANAGER;
1025 $CACHE_MANAGER->ClearByTag('sonet_group');
1026 }
1027 }
1028 else
1029 {
1030 $errorList = $result->getErrorMessages();
1031 }
1032 }
1033 catch (\Bitrix\Rest\OAuthException $e)
1034 {
1035 $errorList[] = $e->getMessage();
1036 }
1037
1038 if (empty($errorList) && $data['PLACEMENTS'])
1039 {
1040 $title = '';
1041 $placementListOld = [];
1042 $updateIDList = [];
1043 $addList = [];
1044 $data['PLACEMENTS'] = is_array($data['PLACEMENTS']) ? $data['PLACEMENTS'] : [];
1045
1046 $placementLangList = [];
1047 foreach ($data['PLACEMENTS_LANG_LIST'] as $lang => $fields)
1048 {
1049 if (!empty($fields['TITLE']))
1050 {
1051 $placementLangList[$lang] = [
1052 'LANGUAGE_ID' => $lang,
1053 'TITLE' => $fields['TITLE'],
1054 ];
1055 }
1056 }
1057
1058 $langList = Lang::listLanguage();
1059 $defaultLang = $langList[0];
1060 if (!empty($placementLangList))
1061 {
1062 foreach ($langList as $lang)
1063 {
1064 if (isset($placementLangList[$lang]))
1065 {
1066 $title = $placementLangList[$lang]['TITLE'];
1067 break;
1068 }
1069 }
1070 }
1071
1072 if ($title === '')
1073 {
1074 $title = $data['APP_NAME'];
1075 $placementLangList[$defaultLang] = [
1076 'LANGUAGE_ID' => $defaultLang,
1077 'TITLE' => $title,
1078 ];
1079 }
1080
1081 $accessPlacement = Rest::getAccessPlacement($data['FIELDS']['SCOPE']);
1082 $placementRes = PlacementTable::getList(
1083 [
1084 'filter' => [
1085 '=APP_ID' => $return['ID']
1086 ]
1087 ]
1088 );
1089 if ($placementList = $placementRes->fetchAll())
1090 {
1091 $placementListOld = array_column($placementList, 'PLACEMENT', 'ID');
1092 }
1093
1094 foreach ($data['PLACEMENTS'] as $placement)
1095 {
1096 if (!in_array($placement, $accessPlacement))
1097 {
1098 $errorList[] = Loc::getMessage(
1099 "INTEGRATION_PRESET_PROVIDER_ERROR_ACCESS_PLACEMENT",
1100 [
1101 '#PLACEMENT_CODE#' => $placement
1102 ]
1103 ) ;
1104 continue;
1105 }
1106
1107 $key = array_search($placement, $placementListOld);
1108 if ($key !== false)
1109 {
1110 $updateIDList[] = $key;
1111 unset($placementListOld[$key]);
1112 }
1113 else
1114 {
1115 $addList[] = [
1116 'APP_ID' => $return['ID'],
1117 'PLACEMENT' => $placement,
1118 'PLACEMENT_HANDLER' => $data['PLACEMENT_HANDLER_URL'],
1119 'TITLE' => $title,
1120 ];
1121 }
1122 }
1123 if (!empty($placementListOld))
1124 {
1125 foreach (array_keys($placementListOld) as $place)
1126 {
1127 PlacementTable::delete($place);
1128 }
1129 }
1130
1131 if (!empty($updateIDList))
1132 {
1133 $resultPlacementBind = PlacementTable::updateMulti(
1134 $updateIDList,
1135 [
1136 'PLACEMENT_HANDLER' => $data['PLACEMENT_HANDLER_URL'],
1137 'TITLE' => $title,
1138 ]
1139 );
1140 if ($resultPlacementBind->isSuccess())
1141 {
1142 foreach ($updateIDList as $id)
1143 {
1144 PlacementLangTable::deleteByPlacement((int) $id);
1145 foreach ($placementLangList as $fields)
1146 {
1147 $fields['PLACEMENT_ID'] = $id;
1148 $resultPlacementLang = PlacementLangTable::add($fields);
1149 if (!$resultPlacementLang->isSuccess())
1150 {
1151 $errors = $resultPlacementLang->getErrorMessages();
1152 if (is_array($errors))
1153 {
1154 $errorList = array_merge($errorList, $errors);
1155 }
1156 }
1157 }
1158 }
1159 }
1160 else
1161 {
1162 $errors = $resultPlacementBind->getErrorMessages();
1163 if (is_array($errors))
1164 {
1165 $errorList = array_merge($errorList, $errors);
1166 }
1167 }
1168 }
1169
1170 if (count($addList) > 0)
1171 {
1172 Analytic::logToFile(
1173 'integrationPlacementCreated',
1174 'integration' . $data['INTEGRATION_ID'],
1175 $data['INTEGRATION_CODE'],
1176 'integrationCode'
1177 );
1178 foreach ($addList as $item)
1179 {
1180 $resultPlacementBind = PlacementTable::add($item);
1181 if ($resultPlacementBind->isSuccess())
1182 {
1183 $id = (int) $resultPlacementBind->getId();
1184 foreach ($placementLangList as $fields)
1185 {
1186 $fields['PLACEMENT_ID'] = $id;
1187 $resultPlacementLang = PlacementLangTable::add($fields);
1188 if (!$resultPlacementLang->isSuccess())
1189 {
1190 $errors = $resultPlacementLang->getErrorMessages();
1191 if (is_array($errors))
1192 {
1193 $errorList = array_merge($errorList, $errors);
1194 }
1195 }
1196 }
1197 Analytic::logToFile(
1198 'integrationPlacementCreated',
1199 'integration' . $data['INTEGRATION_ID'],
1200 $item['PLACEMENT'],
1201 'placementCode'
1202 );
1203 }
1204 else
1205 {
1206 $errors = $resultPlacementBind->getErrorMessages();
1207 if (is_array($errors))
1208 {
1209 $errorList = array_merge($errorList, $errors);
1210 }
1211 }
1212 }
1213 }
1214 }
1215 }
1216
1217 if (!empty($errorList))
1218 {
1219 $return['errors'] = $errorList;
1220 }
1221
1222 return $return;
1223 }
1224
1225 private static function getWebHook($scopeList = [], $id = 0, $title = '')
1226 {
1227 $password = [];
1228 $id = intVal($id);
1229 $scopeList = is_array($scopeList) ? $scopeList : [];
1230 if ($id !== 0)
1231 {
1232 $passData = PasswordTable::getList(
1233 [
1234 'filter' => [
1235 '=ID' => $id,
1236 ],
1237 'select' => [
1238 'PASSWORD',
1239 'USER_ID',
1240 'TITLE',
1241 'ID'
1242 ],
1243 'limit' => 1
1244 ]
1245 );
1246 if ($passwordData = $passData->fetch())
1247 {
1248 $scopeListOld = [];
1249 $scopeIdList = [];
1250 $permData = PermissionTable::getList(
1251 [
1252 'filter' => [
1253 '=PASSWORD_ID' => $passwordData['ID']
1254 ]
1255 ]
1256 );
1257 while ($scopeItem = $permData->fetch())
1258 {
1259 $scopeIdList[$scopeItem['PERM']] = $scopeItem['ID'];
1260 $scopeListOld[] = $scopeItem['PERM'];
1261 }
1262 $resultList = array_diff($scopeList, $scopeListOld);
1263 foreach ($resultList as $scope)
1264 {
1265 PermissionTable::add(
1266 [
1267 'PASSWORD_ID' => $passwordData['ID'],
1268 'PERM' => $scope,
1269 ]
1270 );
1271 }
1272 $resultList = array_diff($scopeListOld, $scopeList);
1273 foreach ($resultList as $scope)
1274 {
1275 if (isset($scopeIdList[$scope]))
1276 {
1277 PermissionTable::delete($scopeIdList[$scope]);
1278 }
1279 }
1280
1281 if ($title !== '' && $passwordData['TITLE'] !== $title)
1282 {
1283 PasswordTable::update(
1284 $passwordData['ID'],
1285 [
1286 'TITLE' => $title
1287 ]
1288 );
1289 }
1290
1291 $password = $passwordData;
1292 }
1293 }
1294
1295 if (empty($password))
1296 {
1297 $userId = $GLOBALS['USER']->GetID();
1298 $passwordCreat = PasswordTable::createPassword(
1299 $userId,
1300 $scopeList,
1301 $title,
1302 true
1303 );
1304 if ($passwordCreat !== false)
1305 {
1306 $password = $passwordCreat;
1307 }
1308 }
1309 if (!empty($password['PASSWORD']))
1310 {
1311 $password['URL'] = \CRestUtil::getWebhookEndpoint($password['PASSWORD'], $password['USER_ID']);
1312 }
1313
1314 return $password;
1315 }
1316
1317 private static function deleteWebHook($id)
1318 {
1319 $result = false;
1320 $passData = PasswordTable::getList(
1321 [
1322 'filter' => [
1323 '=ID' => $id,
1324 ],
1325 'select' => [
1326 'PASSWORD',
1327 'USER_ID',
1328 'ID'
1329 ],
1330 'limit' => 1
1331 ]
1332 );
1333 if ($passwordData = $passData->fetch())
1334 {
1335 $permData = PermissionTable::getList(
1336 [
1337 'filter' => [
1338 '=PASSWORD_ID' => $passwordData['ID']
1339 ],
1340 'select' => [
1341 'ID'
1342 ]
1343 ]
1344 );
1345 while ($scopeItem = $permData->fetch())
1346 {
1347 PermissionTable::delete($scopeItem['ID']);
1348 }
1349 $deleteResult = PasswordTable::delete($passwordData['ID']);
1350 if ($deleteResult->isSuccess())
1351 {
1352 $result = true;
1353 }
1354 }
1355 return $result;
1356 }
1357}
if(!is_object($USER)||! $USER->IsAuthorized()) $userId
Определения check_mail.php:18
static includeModule($moduleName)
Определения loader.php:67
static logToFile($action, $tag='', $label='', $actionType='')
Определения analytic.php:20
static getEngine()
Определения oauthservice.php:49
static register()
Определения oauthservice.php:59
const URI_METHOD_INFO
Определения provider.php:37
static getIntegration($id)
Определения provider.php:211
const URI_EXAMPLE_DOWNLOAD
Определения provider.php:39
static deleteIntegration($id)
Определения provider.php:52
const APP_MODE_ZIP
Определения provider.php:41
const APP_MODE_SERVER
Определения provider.php:40
static saveIntegration($requestData, $elementCode='', $id=0)
Определения provider.php:356
global $CACHE_MANAGER
Определения clear_component_cache.php:7
$data['IS_AVAILABLE']
Определения .description.php:13
$res
Определения filter_act.php:7
$result
Определения get_property_values.php:14
$errors
Определения iblock_catalog_edit.php:74
$select
Определения iblock_catalog_list.php:194
$app
Определения proxy.php:8
global $USER
Определения csv_new_run.php:40
if(!defined('SITE_ID')) $lang
Определения include.php:91
ExecuteModuleEventEx($arEvent, $arParams=[])
Определения tools.php:5214
GetModuleEvents($MODULE_ID, $MESSAGE_ID, $bReturnArray=false)
Определения tools.php:5177
$name
Определения menu_edit.php:35
$password
Определения mysql_to_pgsql.php:34
$GLOBALS['____1690880296']
Определения license.php:1
$event
Определения prolog_after.php:141
if( $daysToExpire >=0 &&$daysToExpire< 60 elseif)( $daysToExpire< 0)
Определения prolog_main_admin.php:393
if(empty($signedUserToken)) $key
Определения quickway.php:257
</p ></td >< td valign=top style='border-top:none;border-left:none;border-bottom:solid windowtext 1.0pt;border-right:solid windowtext 1.0pt;padding:0cm 2.0pt 0cm 2.0pt;height:9.0pt'>< p class=Normal align=center style='margin:0cm;margin-bottom:.0001pt;text-align:center;line-height:normal'>< a name=ТекстовоеПоле54 ></a ><?=($taxRate > count( $arTaxList) > 0) ? $taxRate."%"
Определения waybill.php:936
if($inWords) echo htmlspecialcharsbx(Number2Word_Rus(roundEx($totalVatSum $params['CURRENCY']
Определения template.php:799
$title
Определения pdf.php:123
$clientId
Определения seo_client.php:18
$fields
Определения yandex_run.php:501