21 public static function register(array $fields)
23 $moduleId = $fields[
'MODULE_ID'];
33 $jscommand = isset($fields[
'JS'])? $fields[
'JS']:
'';
35 if (isset($fields[
'IFRAME']) && $fields[
'IFRAME'])
37 $check = parse_url($fields[
'IFRAME']);
38 if (!isset($check[
'scheme']) && !isset($check[
'host']))
40 if (mb_strpos($fields[
'IFRAME'],
'/desktop_app/iframe/') !== 0)
45 else if (!in_array($check[
'scheme'], Array(
'http',
'https')) || empty($check[
'host']))
49 $iframe = $fields[
'IFRAME'].(isset($check[
'query'])?
'&':
'?');
50 if (isset($fields[
'IFRAME_WIDTH']))
52 $iframeWidth = intval($fields[
'IFRAME_WIDTH']) > 250? $fields[
'IFRAME_WIDTH']: 250;
54 if (isset($fields[
'IFRAME_HEIGHT']))
56 $iframeHeight = intval($fields[
'IFRAME_HEIGHT']) > 50? $fields[
'IFRAME_HEIGHT']: 50;
58 $iframePopup = isset($fields[
'IFRAME_POPUP']) && $fields[
'IFRAME_POPUP'] ==
'Y'?
'Y': $iframePopup;
65 $code = $fields[
'CODE'];
71 $iconFileId = intval($fields[
'ICON_ID']);
73 $botId = isset($fields[
'BOT_ID'])? intval($fields[
'BOT_ID']): 0;
74 $hash = isset($fields[
'HASH']) && !empty($fields[
'HASH'])? mb_substr($fields[
'HASH'], 0, 32) : md5($botId.$fields[
'CODE'].\CMain::GetServerUniqID());
75 $context = isset($fields[
'CONTEXT'])? $fields[
'CONTEXT']:
'ALL';
76 $registered = isset($fields[
'REGISTERED']) && $fields[
'REGISTERED'] ==
'N'?
'N':
'Y';
77 $hidden = isset($fields[
'HIDDEN']) && $fields[
'HIDDEN'] ==
'Y'?
'Y':
'N';
83 $extranetSupport = isset($fields[
'EXTRANET_SUPPORT']) && $fields[
'EXTRANET_SUPPORT'] ==
'Y'?
'Y':
'N';
84 $livechatSupport = isset($fields[
'LIVECHAT_SUPPORT']) && $fields[
'LIVECHAT_SUPPORT'] ==
'Y'?
'Y':
'N';
87 $class = isset($fields[
'CLASS'])? $fields[
'CLASS']:
'';
88 $methodLangGet = isset($fields[
'METHOD_LANG_GET'])? $fields[
'METHOD_LANG_GET']:
'';
91 $restAppId = isset($fields[
'APP_ID'])? $fields[
'APP_ID']:
'';
92 $langSet = isset($fields[
'LANG'])? $fields[
'LANG']: Array();
94 if ($moduleId ==
'rest')
103 if (empty($class) || empty($methodLangGet))
110 foreach ($apps as $cmd)
114 if ($botId == $cmd[
'BOT_ID'] && $code == $cmd[
'CODE'])
121 if ($restAppId == $cmd[
'APP_ID'] && $code == $cmd[
'CODE'])
126 else if ($moduleId == $cmd[
'MODULE_ID'] && $code == $cmd[
'CODE'])
132 $result = \Bitrix\Im\Model\AppTable::add(Array(
135 'MODULE_ID' => $moduleId,
137 'ICON_FILE_ID' => $iconFileId,
138 'CONTEXT' => ToLower($context),
140 'REGISTERED' => $registered,
142 'IFRAME_WIDTH' => $iframeWidth,
143 'IFRAME_HEIGHT' => $iframeHeight,
144 'IFRAME_POPUP' => $iframePopup,
146 'EXTRANET_SUPPORT' => $extranetSupport,
147 'LIVECHAT_SUPPORT' => $livechatSupport,
149 'METHOD_LANG_GET' => $methodLangGet,
150 'APP_ID' => $restAppId
153 if (!$result->isSuccess())
156 $cache = \Bitrix\Main\Data\Cache::createInstance();
157 $cache->cleanDir(self::CACHE_PATH);
159 $appId = $result->getId();
161 if ($moduleId ==
'rest')
163 foreach ($langSet as $lang)
165 if (!isset($lang[
'LANGUAGE_ID']) || empty($lang[
'LANGUAGE_ID']))
168 if (!isset($lang[
'TITLE']) && empty($lang[
'TITLE']))
173 \Bitrix\Im\Model\AppLangTable::add(array(
175 'LANGUAGE_ID' => mb_strtolower($lang[
'LANGUAGE_ID']),
176 'TITLE' => $lang[
'TITLE'],
177 'DESCRIPTION' => $lang[
'DESCRIPTION'],
178 'COPYRIGHT' => $lang[
'COPYRIGHT']
192 $appId = intval($app[
'ID']);
193 $moduleId = isset($app[
'MODULE_ID'])? $app[
'MODULE_ID']:
'';
194 $restAppId = isset($app[
'APP_ID'])? $app[
'APP_ID']:
'';
196 if (intval($appId) <= 0)
199 if (!isset($app[
'FORCE']) || $app[
'FORCE'] ==
'N')
202 if (!isset($icons[$appId]))
205 if ($moduleId <>
'' && $icons[$appId][
'MODULE_ID'] != $moduleId)
208 if ($restAppId <>
'' && $icons[$appId][
'APP_ID'] != $restAppId)
212 \Bitrix\Im\Model\AppTable::delete($appId);
214 $orm = \Bitrix\Im\Model\AppLangTable::getList(Array(
215 'filter' => Array(
'=APP_ID' => $appId)
217 while ($row = $orm->fetch())
219 \Bitrix\Im\Model\AppLangTable::delete($row[
'ID']);
222 $cache = \Bitrix\Main\Data\Cache::createInstance();
223 $cache->cleanDir(self::CACHE_PATH);
225 if (\
Bitrix\Main\Loader::includeModule(
'pull'))
227 \CPullStack::AddShared(Array(
229 'command' =>
'appDeleteIcon',
240 public static function update(array $app, array $updateFields)
242 $appId = intval($app[
'ID']);
243 $userId = intval($app[
'USER_ID']);
244 $moduleId = isset($app[
'MODULE_ID'])? $app[
'MODULE_ID']:
'';
245 $restAppId = isset($app[
'APP_ID'])? $app[
'APP_ID']:
'';
247 if (intval($appId) <= 0)
251 if (!isset($apps[$appId]))
254 if ($moduleId <>
'' && $apps[$appId][
'MODULE_ID'] != $moduleId)
257 if ($restAppId <>
'' && $apps[$appId][
'APP_ID'] != $restAppId)
260 if (isset($updateFields[
'LANG']) && $apps[$appId][
'MODULE_ID'] ==
'rest')
262 $orm = \Bitrix\Im\Model\AppLangTable::getList(Array(
263 'filter' => Array(
'=APP_ID' => $appId)
265 while ($row = $orm->fetch())
267 \Bitrix\Im\Model\AppLangTable::delete($row[
'ID']);
269 foreach ($updateFields[
'LANG'] as $lang)
271 if (!isset($lang[
'LANGUAGE_ID']) || empty($lang[
'LANGUAGE_ID']))
274 if (!isset($lang[
'TITLE']) && empty($lang[
'TITLE']))
279 \Bitrix\Im\Model\AppLangTable::add(array(
281 'LANGUAGE_ID' => mb_strtolower($lang[
'LANGUAGE_ID']),
282 'TITLE' => $lang[
'TITLE'],
283 'DESCRIPTION' => $lang[
'DESCRIPTION'],
284 'COPYRIGHT' => $lang[
'COPYRIGHT']
294 if (isset($updateFields[
'CONTEXT']) && !empty($updateFields[
'CONTEXT']))
296 $update[
'CONTEXT'] = ToLower($updateFields[
'CONTEXT']);
298 if (isset($updateFields[
'HASH']) && !empty($updateFields[
'HASH']))
300 $update[
'HASH'] = $updateFields[
'HASH'];
302 if (isset($updateFields[
'HIDDEN']))
304 $update[
'HIDDEN'] = $updateFields[
'HIDDEN'] ==
'Y'?
'Y':
'N';
306 if (isset($updateFields[
'REGISTERED']))
308 $update[
'REGISTERED'] = $updateFields[
'REGISTERED'] ==
'N'?
'N':
'Y';
310 if (isset($updateFields[
'IFRAME']) && !empty($updateFields[
'IFRAME']))
312 $check = parse_url($updateFields[
'IFRAME']);
313 if (!isset($check[
'scheme']) && !isset($check[
'host']))
315 if (mb_strpos($updateFields[
'IFRAME'],
'/desktop_app/iframe/') !== 0)
320 else if (!in_array($check[
'scheme'], Array(
'http',
'https')) || empty($check[
'host']))
324 $update[
'IFRAME'] = $updateFields[
'IFRAME'].(isset($check[
'query'])?
'&':
'?');
326 else if (isset($updateFields[
'JS']) && !empty($updateFields[
'JS']))
328 $update[
'JS'] = $updateFields[
'JS'];
330 if (isset($updateFields[
'IFRAME_WIDTH']))
332 $update[
'IFRAME_WIDTH'] = intval($updateFields[
'IFRAME_WIDTH']) > 250? intval($updateFields[
'IFRAME_WIDTH']): 250;
334 if (isset($updateFields[
'IFRAME_HEIGHT']))
336 $update[
'IFRAME_HEIGHT'] = intval($updateFields[
'IFRAME_HEIGHT']) > 50? intval($updateFields[
'IFRAME_HEIGHT']): 50;
338 if (isset($updateFields[
'IFRAME_POPUP']))
340 $update[
'IFRAME_POPUP'] = $updateFields[
'IFRAME_POPUP'] ==
'Y'?
'Y':
'N';
342 if (isset($updateFields[
'ICON_ID']) && $updateFields[
'ICON_ID'])
344 $update[
'ICON_FILE_ID'] = intval($updateFields[
'ICON_ID']);
346 if (isset($updateFields[
'CLASS']) && !empty($updateFields[
'CLASS']))
348 $update[
'CLASS'] = $updateFields[
'CLASS'];
350 if (isset($updateFields[
'METHOD_LANG_GET']))
352 $update[
'METHOD_LANG_GET'] = $updateFields[
'METHOD_LANG_GET'];
354 if (isset($updateFields[
'EXTRANET_SUPPORT']))
356 $update[
'EXTRANET_SUPPORT'] = $updateFields[
'EXTRANET_SUPPORT'] ==
'Y'?
'Y':
'N';
358 if (isset($updateFields[
'LIVECHAT_SUPPORT']))
360 $update[
'LIVECHAT_SUPPORT'] = $updateFields[
'LIVECHAT_SUPPORT'] ==
'Y'?
'Y':
'N';
365 \Bitrix\Im\Model\AppTable::update($appId, $update);
367 $cache = \Bitrix\Main\Data\Cache::createInstance();
368 $cache->cleanDir(self::CACHE_PATH);
371 if (\
Bitrix\Main\Loader::includeModule(
'pull'))
374 $update[
'REGISTERED']
375 || $update[
'DOMAIN_HASH']
376 || $update[
'CONTEXT']
379 || $update[
'IFRAME_WIDTH']
380 || $update[
'IFRAME_HEIGHT']
381 || $update[
'IFRAME_POPUP']
384 \CPullStack::AddShared(Array(
386 'command' =>
'appUpdateIcon',
390 'domainHash' => $update[
'HASH'],
391 'context' => $update[
'CONTEXT'],
392 'js' => $update[
'JS'],
393 'iframe' => $update[
'IFRAME'],
394 'iframeWidth' => $update[
'IFRAME_WIDTH'],
395 'iframeHeight' => $update[
'IFRAME_HEIGHT'],
396 'iframePopup' => $update[
'IFRAME_POPUP'],
401 else if ($update[
'ICON_ID'])
406 \CPullStack::AddShared(Array(
408 'command' =>
'appDeleteIcon',
422 $botId = intval($params[
'BOT_ID']);
423 $userId = intval($params[
'USER_ID']);
425 if (!$botId && !$userId || empty($params[
'DIALOG_ID']))
430 Bot\Token::add($botId, $params[
'DIALOG_ID']);
431 Bot\Token::add($userId, $params[
'DIALOG_ID']);
435 Bot\Token::add($botId, $params[
'USER_ID']);
441 public static function addMessage(array $app, array $messageFields)
443 $appId = intval($app[
'ID']);
444 $moduleId = isset($app[
'MODULE_ID'])? $app[
'MODULE_ID']:
'';
445 $restAppId = isset($app[
'APP_ID'])? $app[
'APP_ID']:
'';
451 if (!isset($apps[$appId]))
454 if ($moduleId <>
'' && $apps[$appId][
'MODULE_ID'] != $moduleId)
457 if ($restAppId <>
'' && $apps[$appId][
'APP_ID'] != $restAppId)
460 $botId = intval($apps[$appId][
'BOT_ID']);
464 $chatId = \Bitrix\Im\Dialog::getChatId($messageFields[
'DIALOG_ID']);
465 $relations = \CIMChat::GetRelationById($chatId,
false,
true,
false);
469 $userId = intval($messageFields[
'DIALOG_ID']);
470 if (!$userId || $botId == $userId)
474 $relations = \CIMChat::GetPrivateRelation($botId, $userId);
477 if ($botId && !
Bot\Token::isActive($botId, $messageFields[
'DIALOG_ID']))
482 $messageFields[
'ATTACH'] = $messageFields[
'ATTACH']? $messageFields[
'ATTACH']:
null;
483 $messageFields[
'KEYBOARD'] = $messageFields[
'KEYBOARD']? $messageFields[
'KEYBOARD']:
null;
485 $fromUserId = isset($messageFields[
'FROM_USER_ID'])? $messageFields[
'FROM_USER_ID']: $botId;
489 $chatId = \Bitrix\Im\Dialog::getChatId($messageFields[
'DIALOG_ID']);
493 if (isset($relations[$fromUserId]) && $messageFields[
'SYSTEM'] !=
'Y')
496 "FROM_USER_ID" => $fromUserId,
497 "TO_CHAT_ID" => $chatId,
498 "ATTACH" => $messageFields[
'ATTACH'],
499 "KEYBOARD" => $messageFields[
'KEYBOARD'],
501 if (isset($messageFields[
'MESSAGE']))
503 $ar[
'MESSAGE'] = $messageFields[
'MESSAGE'];
509 "FROM_USER_ID" => isset($relations[$fromUserId])? $fromUserId: 0,
510 "TO_CHAT_ID" => $chatId,
511 "ATTACH" => $messageFields[
'ATTACH'],
512 "KEYBOARD" => $messageFields[
'KEYBOARD'],
515 if (isset($messageFields[
'MESSAGE']))
517 $ar[
'MESSAGE'] = $messageFields[
'MESSAGE'];
520 $ar[
'MESSAGE'] =
"[B]".$apps[$appId][
'TITLE'].
"[/B]\n".$ar[
'MESSAGE'];
523 if (isset($messageFields[
'URL_PREVIEW']) && $messageFields[
'URL_PREVIEW'] ==
'N')
525 $ar[
'URL_PREVIEW'] =
'N';
528 $ar[
'SKIP_USER_CHECK'] =
'Y';
529 $ar[
'SKIP_COMMAND'] =
'Y';
531 $id = \CIMChat::AddMessage($ar);
535 $userId = intval($messageFields[
'DIALOG_ID']);
536 \CModule::IncludeModule(
'imbot');
538 if (isset($relations[$fromUserId]) && $messageFields[
'SYSTEM'] !=
'Y')
541 "FROM_USER_ID" => $fromUserId,
542 "TO_USER_ID" => $userId,
543 "ATTACH" => $messageFields[
'ATTACH'],
544 "KEYBOARD" => $messageFields[
'KEYBOARD'],
546 if (isset($messageFields[
'MESSAGE']))
548 $ar[
'MESSAGE'] = $messageFields[
'MESSAGE'];
554 "FROM_USER_ID" => isset($relations[$fromUserId])? $fromUserId: 0,
555 "TO_USER_ID" => $userId,
556 "ATTACH" => $messageFields[
'ATTACH'],
557 "KEYBOARD" => $messageFields[
'KEYBOARD'],
560 if (isset($messageFields[
'MESSAGE']))
562 $ar[
'MESSAGE'] = $messageFields[
'MESSAGE'];
565 $ar[
'MESSAGE'] =
"[B]".$apps[$appId][
'TITLE'].
"[/B]\n".$ar[
'MESSAGE'];
568 if (isset($messageFields[
'URL_PREVIEW']) && $messageFields[
'URL_PREVIEW'] ==
'N')
570 $ar[
'URL_PREVIEW'] =
'N';
573 $ar[
'SKIP_COMMAND'] =
'Y';
574 $id = \CIMMessage::Add($ar);
582 $cache = \Bitrix\Main\Data\Cache::createInstance();
583 if($cache->initCache(self::CACHE_TTL,
'list_v3_'.$lang, self::CACHE_PATH))
585 $result = $cache->getVars();
589 $loadRestLang =
false;
591 $orm = \Bitrix\Im\Model\AppTable::getList();
592 while ($row = $orm->fetch())
594 if ($row[
'ICON_FILE_ID'])
596 $image = \CFile::ResizeImageGet(
597 $row[
'ICON_FILE_ID'],
598 array(
'width' => 108,
'height' => 66),
599 BX_RESIZE_IMAGE_PROPORTIONAL,
604 $row[
'ICON_URL'] = empty($image[
'src'])?
'': $image[
'src'];
608 $row[
'ICON_URL'] =
'';
611 if (!empty($row[
'CLASS']) && !empty($row[
'METHOD_LANG_GET']))
613 if (\
Bitrix\Main\Loader::includeModule($row[
'MODULE_ID']) && class_exists($row[
"CLASS"]) && method_exists($row[
"CLASS"], $row[
"METHOD_LANG_GET"]))
615 $localize = call_user_func_array(array($row[
"CLASS"], $row[
"METHOD_LANG_GET"]), Array($row[
'CODE'], $lang));
618 $row[
'TITLE'] = $localize[
'TITLE'];
619 $row[
'DESCRIPTION'] = $localize[
'DESCRIPTION'];
620 $row[
'COPYRIGHT'] = $localize[
'COPYRIGHT'];
624 $row[
'METHOD_LANG_GET'] =
'';
629 $row[
'METHOD_LANG_GET'] =
'';
635 $row[
'DESCRIPTION'] =
'';
636 $row[
'COPYRIGHT'] =
'';
638 if ($row[
'MODULE_ID'] ==
'rest')
640 $loadRestLang =
true;
643 $result[$row[
'ID']] = $row;
649 $orm = \Bitrix\Im\Model\AppLangTable::getList();
650 while ($row = $orm->fetch())
652 if (!isset($result[$row[
'APP_ID']]))
655 $langSet[$row[
'APP_ID']][$row[
'LANGUAGE_ID']][
'TITLE'] = $row[
'TITLE'];
656 $langSet[$row[
'APP_ID']][$row[
'LANGUAGE_ID']][
'DESCRIPTION'] = $row[
'DESCRIPTION'];
657 $langSet[$row[
'APP_ID']][$row[
'LANGUAGE_ID']][
'COPYRIGHT'] = $row[
'COPYRIGHT'];
660 $langAlter = \Bitrix\Im\Bot::getDefaultLanguage();
661 foreach ($result as $appId => $commandData)
663 if (isset($langSet[$appId][$lang]))
665 $result[$appId][
'TITLE'] = $langSet[$appId][$lang][
'TITLE'];
666 $result[$appId][
'DESCRIPTION'] = $langSet[$appId][$lang][
'DESCRIPTION'];
667 $result[$appId][
'COPYRIGHT'] = $langSet[$appId][$lang][
'COPYRIGHT'];
669 else if (isset($langSet[$appId][$langAlter]))
671 $result[$appId][
'TITLE'] = $langSet[$appId][$langAlter][
'TITLE'];
672 $result[$appId][
'DESCRIPTION'] = $langSet[$appId][$langAlter][
'DESCRIPTION'];
673 $result[$appId][
'COPYRIGHT'] = $langSet[$appId][$langAlter][
'COPYRIGHT'];
675 else if (isset($langSet[$appId]))
677 $langSetCommand = array_values($langSet[$appId]);
678 $result[$appId][
'TITLE'] = $langSetCommand[0][
'TITLE'];
679 $result[$appId][
'DESCRIPTION'] = $langSetCommand[0][
'DESCRIPTION'];
680 $result[$appId][
'COPYRIGHT'] = $langSetCommand[0][
'COPYRIGHT'];
684 foreach ($result as $key => $value)
686 if (empty($value[
'TITLE']))
688 $row[
'METHOD_LANG_GET'] =
'';
693 $cache->startDataCache();
694 $cache->endDataCache($result);
705 $isExtranet = $userId && \Bitrix\Im\User::getInstance($userId)->isExtranet();
706 $isConnector = $userId && \Bitrix\Im\User::getInstance($userId)->isConnector();
709 foreach ($apps as $app)
711 if ($isConnector && $app[
'LIVECHAT_SUPPORT'] !=
'Y')
713 else if ($isExtranet && $app[
'EXTRANET_SUPPORT'] !=
'Y')
716 $botData = \Bitrix\Im\Bot::getCache($app[
'BOT_ID']);
719 'botId' => $app[
'BOT_ID'],
720 'botCode' => htmlspecialcharsbx($botData[
'CODE'] ??
''),
721 'domainHash' => self::getDomainHash($app[
'HASH']),
722 'userHash' => self::getUserHash($userId, $app[
'HASH']),
723 'code' => htmlspecialcharsbx($app[
'CODE']),
724 'url' => $app[
'ICON_URL'],
725 'iframe' => $app[
'IFRAME'],
726 'iframeWidth' => $app[
'IFRAME_WIDTH'],
727 'iframeHeight' => $app[
'IFRAME_HEIGHT'],
728 'iframePopup' => $app[
'IFRAME_POPUP'] ==
'Y',
730 'context' => ToLower($app[
'CONTEXT']),
731 'hidden' => $app[
'HIDDEN'] ==
'Y',
732 'title' => $app[
'TITLE'],
733 'description' => $app[
'DESCRIPTION'],
734 'copyright' => $app[
'COPYRIGHT'],