Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
command.php
1<?php
9namespace Bitrix\Im;
10
11use Bitrix\Main,
14Loc::loadMessages(__FILE__);
15
17{
18 const CACHE_TTL = 31536000;
19 const CACHE_PATH = '/bx/im/command/';
20
21 public static function register(array $fields)
22 {
23 $moduleId = $fields['MODULE_ID'];
24 $command = mb_substr($fields['COMMAND'], 0, 1) == '/'? mb_substr($fields['COMMAND'], 1) : $fields['COMMAND'];
25
26 $botId = isset($fields['BOT_ID'])? intval($fields['BOT_ID']): 0;
27 if ($botId > 0 && (!\Bitrix\Im\User::getInstance($botId)->isExists() || !\Bitrix\Im\User::getInstance($botId)->isBot()))
28 {
29 $botId = 0;
30 }
31
32 $common = isset($fields['COMMON']) && $fields['COMMON'] == 'Y'? 'Y': 'N';
33 if ($botId <= 0)
34 {
35 $common = 'Y';
36 }
37
38 $hidden = isset($fields['HIDDEN']) && $fields['HIDDEN'] == 'Y'? 'Y': 'N';
39 $sonetSupport = isset($fields['SONET_SUPPORT']) && $fields['SONET_SUPPORT'] == 'Y'? 'Y': 'N';
40 $extranetSupport = isset($fields['EXTRANET_SUPPORT']) && $fields['EXTRANET_SUPPORT'] == 'Y'? 'Y': 'N';
41
42 /* vars for module install */
43 $class = isset($fields['CLASS'])? $fields['CLASS']: '';
44 $methodCommandAdd = isset($fields['METHOD_COMMAND_ADD'])? $fields['METHOD_COMMAND_ADD']: '';
45 $methodLangGet = isset($fields['METHOD_LANG_GET'])? $fields['METHOD_LANG_GET']: '';
46
47 /* vars for rest install */
48 $appId = isset($fields['APP_ID'])? $fields['APP_ID']: '';
49 $langSet = isset($fields['LANG'])? $fields['LANG']: Array();
50
51 if ($moduleId == '')
52 {
53 return false;
54 }
55 if ($moduleId == 'rest')
56 {
57 if (empty($appId) || empty($langSet) && $hidden == 'N')
58 {
59 return false;
60 }
61 }
62 else
63 {
64 if (empty($class) || empty($methodCommandAdd))
65 {
66 return false;
67 }
68 if (empty($methodLangGet))
69 {
70 $hidden = 'Y';
71 }
72 }
73
74 $commands = self::getListCache();
75 foreach ($commands as $cmd)
76 {
77 if ($botId)
78 {
79 if ($botId == $cmd['BOT_ID'] && $command == $cmd['COMMAND'])
80 {
81 return $cmd['ID'];
82 }
83 }
84 else if ($appId)
85 {
86 if ($appId == $cmd['APP_ID'] && $command == $cmd['COMMAND'])
87 {
88 return $cmd['ID'];
89 }
90 }
91 else if ($moduleId == $cmd['MODULE_ID'] && $command == $cmd['COMMAND'])
92 {
93 return $cmd['ID'];
94 }
95 }
96
97 $result = \Bitrix\Im\Model\CommandTable::add(Array(
98 'BOT_ID' => $botId,
99 'MODULE_ID' => $moduleId,
100 'COMMAND' => $command,
101 'COMMON' => $common,
102 'HIDDEN' => $hidden,
103 'SONET_SUPPORT' => $sonetSupport,
104 'EXTRANET_SUPPORT' => $extranetSupport,
105 'CLASS' => $class,
106 'METHOD_COMMAND_ADD' => $methodCommandAdd,
107 'METHOD_LANG_GET' => $methodLangGet,
108 'APP_ID' => $appId
109 ));
110
111 if (!$result->isSuccess())
112 return false;
113
114 $cache = \Bitrix\Main\Data\Cache::createInstance();
115 $cache->cleanDir(self::CACHE_PATH);
116
117 $commandId = $result->getId();
118
119 if ($moduleId == 'rest')
120 {
121 foreach ($langSet as $lang)
122 {
123 if (!isset($lang['LANGUAGE_ID']) || empty($lang['LANGUAGE_ID']))
124 continue;
125
126 if (!isset($lang['TITLE']) && empty($lang['TITLE']))
127 continue;
128
129 try
130 {
131 \Bitrix\Im\Model\CommandLangTable::add(array(
132 'COMMAND_ID' => $commandId,
133 'LANGUAGE_ID' => mb_strtolower($lang['LANGUAGE_ID']),
134 'TITLE' => $lang['TITLE'],
135 'PARAMS' => isset($lang['PARAMS'])? $lang['PARAMS']: ''
136 ));
137 }
138 catch(Exception $e)
139 {
140 }
141 }
142 }
143
144 return $commandId;
145 }
146
147 public static function unRegister(array $command)
148 {
149 $commandId = intval($command['COMMAND_ID']);
150 $moduleId = isset($command['MODULE_ID'])? $command['MODULE_ID']: '';
151 $appId = isset($command['APP_ID'])? $command['APP_ID']: '';
152
153 if (intval($commandId) <= 0)
154 return false;
155
156 if (!isset($command['FORCE']) || $command['FORCE'] == 'N')
157 {
158 $commands = self::getListCache();
159 if (!isset($commands[$commandId]))
160 return false;
161
162 if ($moduleId <> '' && $commands[$commandId]['MODULE_ID'] != $moduleId)
163 return false;
164
165 if ($appId <> '' && $commands[$commandId]['APP_ID'] != $appId)
166 return false;
167 }
168
169 \Bitrix\Im\Model\CommandTable::delete($commandId);
170
171 $orm = \Bitrix\Im\Model\CommandLangTable::getList(Array(
172 'filter' => Array('=COMMAND_ID' => $commandId)
173 ));
174 while ($row = $orm->fetch())
175 {
176 \Bitrix\Im\Model\CommandLangTable::delete($row['ID']);
177 }
178
179 $cache = \Bitrix\Main\Data\Cache::createInstance();
180 $cache->cleanDir(self::CACHE_PATH);
181
182 if (\Bitrix\Main\Loader::includeModule('pull'))
183 {
184 \CPullStack::AddShared(Array(
185 'module_id' => 'im',
186 'command' => 'commandDelete',
187 'params' => Array(
188 'commandId' => $commandId
189 ),
190 'extra' => \Bitrix\Im\Common::getPullExtra()
191 ));
192 }
193
194 return true;
195 }
196
197 public static function update(array $command, array $updateFields)
198 {
199 $commandId = $command['COMMAND_ID'];
200 $moduleId = isset($command['MODULE_ID'])? $command['MODULE_ID']: '';
201 $appId = isset($command['APP_ID'])? $command['APP_ID']: '';
202
203 if (intval($commandId) <= 0)
204 return false;
205
206 $commands = self::getListCache();
207 if (!isset($commands[$commandId]))
208 return false;
209
210 if ($moduleId <> '' && $commands[$commandId]['MODULE_ID'] != $moduleId)
211 return false;
212
213 if ($appId <> '' && $commands[$commandId]['APP_ID'] != $appId)
214 return false;
215
216 if (isset($updateFields['LANG']) && $commands[$commandId]['MODULE_ID'] == 'rest')
217 {
218 $orm = \Bitrix\Im\Model\CommandLangTable::getList(Array(
219 'filter' => Array('=COMMAND_ID' => $commandId)
220 ));
221 while ($row = $orm->fetch())
222 {
223 \Bitrix\Im\Model\CommandLangTable::delete($row['ID']);
224 }
225 foreach ($updateFields['LANG'] as $lang)
226 {
227 if (!isset($lang['LANGUAGE_ID']) || empty($lang['LANGUAGE_ID']))
228 continue;
229
230 if (!isset($lang['TITLE']) && empty($lang['TITLE']))
231 continue;
232
233 try
234 {
235 \Bitrix\Im\Model\CommandLangTable::add(array(
236 'COMMAND_ID' => $commandId,
237 'LANGUAGE_ID' => mb_strtolower($lang['LANGUAGE_ID']),
238 'TITLE' => $lang['TITLE'],
239 'PARAMS' => isset($lang['PARAMS'])? $lang['PARAMS']: ''
240 ));
241 }
242 catch(Exception $e)
243 {
244 }
245 }
246 }
247
248 $update = Array();
249 if (isset($updateFields['CLASS']) && !empty($updateFields['CLASS']))
250 {
251 $update['CLASS'] = $updateFields['CLASS'];
252 }
253 if (isset($updateFields['METHOD_COMMAND_ADD']))
254 {
255 $update['METHOD_COMMAND_ADD'] = $updateFields['METHOD_COMMAND_ADD'];
256 }
257 if (isset($updateFields['METHOD_LANG_GET']))
258 {
259 $update['METHOD_LANG_GET'] = $updateFields['METHOD_LANG_GET'];
260 }
261 if (isset($updateFields['COMMON']))
262 {
263 if ($commands[$commandId]['BOT_ID'] <= 0)
264 {
265 $update['COMMON'] = 'Y';
266 }
267 else
268 {
269 $update['COMMON'] = $updateFields['COMMON'] == 'Y'? 'Y': 'N';
270 }
271 }
272 if (isset($updateFields['HIDDEN']))
273 {
274 $update['HIDDEN'] = $updateFields['HIDDEN'] == 'Y'? 'Y': 'N';
275 }
276 if (isset($updateFields['EXTRANET_SUPPORT']))
277 {
278 $update['EXTRANET_SUPPORT'] = $updateFields['EXTRANET_SUPPORT'] == 'Y'? 'Y': 'N';
279 }
280 if (isset($updateFields['SONET_SUPPORT']))
281 {
282 $update['SONET_SUPPORT'] = $updateFields['SONET_SUPPORT'] == 'Y'? 'Y': 'N';
283 }
284 if (!empty($update))
285 {
286 \Bitrix\Im\Model\CommandTable::update($commandId, $update);
287
288 $cache = \Bitrix\Main\Data\Cache::createInstance();
289 $cache->cleanDir(self::CACHE_PATH);
290 }
291
292 return true;
293 }
294
295 public static function onCommandAdd($messageId, $messageFields)
296 {
297 $skipCommand = $messageFields['SKIP_COMMAND'] ?? null;
298 $isSystem = $messageFields['SYSTEM'] ?? null;
299
300 if ($skipCommand === 'Y' || $isSystem === 'Y')
301 {
302 return true;
303 }
304
305 $commands = self::getListCache();
306 if (empty($commands))
307 {
308 return false;
309 }
310
311 $commandList = Array();
312 $message = $messageFields['MESSAGE'] ?? null;
313 if (preg_match_all("/^\\/(?P<COMMAND>[^\\040\\n]*)(\\040?)(?P<PARAMS>.*)$/m", $message, $matches))
314 {
315 foreach($matches['COMMAND'] as $idx => $cmd)
316 {
317 $commandData = self::findCommands(Array('COMMAND' => $cmd, 'EXEC_PARAMS' => $matches['PARAMS'][$idx], 'MESSAGE_FIELDS' => $messageFields));
318 if (!$commandData)
319 {
320 continue;
321 }
322
323 $commandList = array_merge($commandList, $commandData);
324 }
325 }
326 if (empty($commandList))
327 {
328 return false;
329 }
330
331 $messageFields['DIALOG_ID'] = self::getDialogId($messageFields);
332 unset(
333 $messageFields['MESSAGE_OUT'],
334 $messageFields['NOTIFY_EVENT'],
335 $messageFields['NOTIFY_MODULE'],
336 $messageFields['URL_PREVIEW']
337 );
338
339 $count = 0;
340 $executed = Array();
341 foreach ($commandList as $params)
342 {
343 if (!$params['MODULE_ID'] || !\Bitrix\Main\Loader::includeModule($params['MODULE_ID']))
344 {
345 continue;
346 }
347 $hash = md5($params['EXEC_PARAMS'].$params['COMMAND_ID']);
348 if (isset($executed[$hash]) && $executed[$hash])
349 {
350 continue;
351 }
352 $executed[$hash] = true;
353
354 if ($count >= 10)
355 {
356 break;
357 }
358 $count++;
359
360 if ($params['BOT_ID'] > 0)
361 {
362 Bot\Token::add($params['BOT_ID'], $messageFields['DIALOG_ID']);
363 if ($messageFields['MESSAGE_TYPE'] == IM_MESSAGE_PRIVATE)
364 {
365 Bot\Token::add($params['BOT_ID'], $messageFields['TO_USER_ID']);
366 }
367 }
368
369 $messageFields['COMMAND'] = $params['COMMAND'];
370 $messageFields['COMMAND_ID'] = $params['COMMAND_ID'];
371 $messageFields['COMMAND_PARAMS'] = $params['EXEC_PARAMS'];
372
373 if ($params["METHOD_COMMAND_ADD"] && class_exists($params["CLASS"]) && method_exists($params["CLASS"], $params["METHOD_COMMAND_ADD"]))
374 {
375 if ($params['BOT_ID'] > 0)
376 {
377 \Bitrix\Im\Model\BotTable::update($params['BOT_ID'], array(
378 "COUNT_COMMAND" => new \Bitrix\Main\DB\SqlExpression("?# + 1", "COUNT_COMMAND")
379 ));
380 }
381
382 call_user_func_array(array($params["CLASS"], $params["METHOD_COMMAND_ADD"]), Array($messageId, $messageFields));
383 }
384 }
385 unset(
386 $messageFields['COMMAND'],
387 $messageFields['COMMAND_ID'],
388 $messageFields['COMMAND_PARAMS'],
389 $messageFields['COMMAND_CONTEXT']
390 );
391
392 foreach(\Bitrix\Main\EventManager::getInstance()->findEventHandlers("im", "onImCommandAdd") as $event)
393 {
394 ExecuteModuleEventEx($event, Array($commandList, $messageId, $messageFields));
395 }
396
397 return true;
398 }
399
400 public static function addMessage(array $access, array $messageFields)
401 {
402 $messageId = intval($access['MESSAGE_ID']);
403 $commandId = intval($access['COMMAND_ID']);
404 $moduleId = isset($access['MODULE_ID'])? $access['MODULE_ID']: '';
405 $appId = isset($access['APP_ID'])? $access['APP_ID']: '';
406
407 if ($messageId <= 0 || $commandId <= 0)
408 return false;
409
410 $commands = self::getListCache();
411 if (!isset($commands[$commandId]))
412 return false;
413
414 if ($moduleId <> '' && $commands[$commandId]['MODULE_ID'] != $moduleId)
415 return false;
416
417 if ($appId <> '' && $commands[$commandId]['APP_ID'] != $appId)
418 return false;
419
420 $botId = intval($commands[$commandId]['BOT_ID']);
421
422 $orm = \Bitrix\Im\Model\MessageTable::getById($messageId);
423 if (!($message = $orm->fetch()))
424 return false;
425
426 $orm = \Bitrix\Im\Model\ChatTable::getById($message['CHAT_ID']);
427 if (!($chat = $orm->fetch()))
428 return false;
429
430 $relations = \CIMChat::GetRelationById($message['CHAT_ID'], false, true, false);
431
432 $chatWithBot = false;
433 foreach ($relations as $userId => $relation)
434 {
435 if ($relation['MESSAGE_TYPE'] != IM_MESSAGE_PRIVATE)
436 {
437 $messageFields['DIALOG_ID'] = 'chat'.$message['CHAT_ID'];
438 break;
439 }
440 if ($message['AUTHOR_ID'] == $botId)
441 {
442 $chatWithBot = true;
443 if ($botId != $relation['USER_ID'])
444 {
445 $messageFields['DIALOG_ID'] = $relation['USER_ID'];
446 break;
447 }
448 }
449 else if ($message['AUTHOR_ID'] != $relation['USER_ID'])
450 {
451 $messageFields['DIALOG_ID'] = $relation['USER_ID'];
452 break;
453 }
454 }
455
456 if ($botId > 0)
457 {
458 $grantAccess = false;
459 if (Bot\Token::isActive($botId, $messageFields['DIALOG_ID']))
460 {
461 $grantAccess = true;
462 }
463 }
464 else
465 {
466 $grantAccess = false;
467 if (preg_match_all("/^\\/(?P<COMMAND>[^\\040\\n]*)(\\040?)(?P<PARAMS>.*)$/m", $message['MESSAGE'], $matches))
468 {
469 foreach($matches['COMMAND'] as $idx => $cmd)
470 {
471 if ($commands[$commandId]['COMMAND'] == $cmd)
472 {
473 $grantAccess = true;
474 break;
475 }
476 }
477 }
478 }
479 if (!$grantAccess)
480 return true;
481
482 $messageFields['ATTACH'] = $messageFields['ATTACH']? $messageFields['ATTACH']: null;
483 $messageFields['KEYBOARD'] = $messageFields['KEYBOARD']? $messageFields['KEYBOARD']: null;
484 $messageFields['MENU'] = $messageFields['MENU']? $messageFields['MENU']: null;
485
486 if (Common::isChatId($messageFields['DIALOG_ID']))
487 {
488 $chatId = \Bitrix\Im\Dialog::getChatId($messageFields['DIALOG_ID']);
489 if ($chatId <= 0)
490 return false;
491
492 if (isset($relations[$botId]) && $messageFields['SYSTEM'] != 'Y')
493 {
494 $ar = Array(
495 "FROM_USER_ID" => $botId,
496 "TO_CHAT_ID" => $chatId,
497 "ATTACH" => $messageFields['ATTACH'],
498 "KEYBOARD" => $messageFields['KEYBOARD'],
499 "MENU" => $messageFields['MENU'],
500 );
501 if (isset($messageFields['MESSAGE']))
502 {
503 $ar['MESSAGE'] = $messageFields['MESSAGE'];
504 }
505 }
506 else
507 {
508 $ar = Array(
509 "FROM_USER_ID" => $botId,
510 "TO_CHAT_ID" => $chatId,
511 "ATTACH" => $messageFields['ATTACH'],
512 "KEYBOARD" => $messageFields['KEYBOARD'],
513 "MENU" => $messageFields['MENU'],
514 "SYSTEM" => 'Y',
515 );
516 if (isset($messageFields['MESSAGE']))
517 {
518 $ar['MESSAGE'] = $messageFields['MESSAGE'];
519 }
520 if ($botId > 0)
521 {
522 $ar['MESSAGE'] = Loc::getMessage("COMMAND_BOT_ANSWER", Array("#BOT_NAME#" => "[USER=".$botId."][/USER]\n ")).$ar['MESSAGE'];
523 }
524 else
525 {
526 $ar['MESSAGE'] = "[B]".Loc::getMessage("COMMAND_SYSTEM_ANSWER", Array("#COMMAND#" => "/".$commands[$commandId]['COMMAND']))."[/B]\n ".$ar['MESSAGE'];
527 }
528 }
529
530 if (isset($messageFields['URL_PREVIEW']) && $messageFields['URL_PREVIEW'] == 'N')
531 {
532 $ar['URL_PREVIEW'] = 'N';
533 }
534
535 $ar['SKIP_USER_CHECK'] = 'Y';
536 $ar['SKIP_COMMAND'] = 'Y';
537
538 $id = \CIMChat::AddMessage($ar);
539 }
540 else
541 {
542 if ($chatWithBot)
543 {
544 $message['AUTHOR_ID'] = intval($messageFields['DIALOG_ID']);
545 $userId = $botId;
546 }
547 else
548 {
549 $userId = intval($messageFields['DIALOG_ID']);
550 }
551 \CModule::IncludeModule('imbot');
552 if ($botId == $userId && $messageFields['SYSTEM'] != 'Y')
553 {
554 $ar = Array(
555 "FROM_USER_ID" => $userId,
556 "TO_USER_ID" => $message['AUTHOR_ID'],
557 "ATTACH" => $messageFields['ATTACH'],
558 "KEYBOARD" => $messageFields['KEYBOARD'],
559 );
560 if (isset($messageFields['MESSAGE']))
561 {
562 $ar['MESSAGE'] = $messageFields['MESSAGE'];
563 }
564 }
565 else
566 {
567 $ar = Array(
568 "FROM_USER_ID" => $message['AUTHOR_ID'],
569 "TO_USER_ID" => $userId,
570 "ATTACH" => $messageFields['ATTACH'],
571 "KEYBOARD" => $messageFields['KEYBOARD'],
572 "SYSTEM" => "Y",
573 );
574 if (isset($messageFields['MESSAGE']))
575 {
576 $ar['MESSAGE'] = $messageFields['MESSAGE'];
577 }
578 if ($botId > 0)
579 {
580 $ar['MESSAGE'] = Loc::getMessage("COMMAND_BOT_ANSWER", Array("#BOT_NAME#" => "[USER=".$botId."][/USER]\n ")).$ar['MESSAGE'];
581 }
582 else
583 {
584 $ar['MESSAGE'] = "[B]".Loc::getMessage("COMMAND_SYSTEM_ANSWER", Array("#COMMAND#" => "/".$commands[$commandId]['COMMAND']))."[/B]\n ".$ar['MESSAGE'];
585 }
586 }
587
588 if (isset($messageFields['URL_PREVIEW']) && $messageFields['URL_PREVIEW'] == 'N')
589 {
590 $ar['URL_PREVIEW'] = 'N';
591 }
592
593 $ar['SKIP_COMMAND'] = 'Y';
594 $id = \CIMMessage::Add($ar);
595 }
596
597 return $id;
598 }
599
600 public static function getDialogId($messageFields)
601 {
602 if ($messageFields['MESSAGE_TYPE'] == IM_MESSAGE_PRIVATE)
603 {
604 $dialogId = $messageFields['FROM_USER_ID'];
605 }
606 else
607 {
608 $dialogId = 'chat'.$messageFields['TO_CHAT_ID'];
609 }
610
611 return $dialogId;
612 }
613
614 private static function findCommands($fields)
615 {
616 $command = mb_substr($fields['COMMAND'], 0, 1) == '/'? mb_substr($fields['COMMAND'], 1) : $fields['COMMAND'];
617 $execParams = isset($fields['EXEC_PARAMS'])? $fields['EXEC_PARAMS']: '';
618 $messageFields = isset($fields['MESSAGE_FIELDS'])? $fields['MESSAGE_FIELDS']: Array();
619
620 if ($messageFields['MESSAGE_TYPE'] != IM_MESSAGE_PRIVATE)
621 {
622 $relations = \CIMChat::GetRelationById($messageFields['TO_CHAT_ID'], false, true, false);
623 }
624
625 $result = Array();
626 if ($command == '')
627 return $result;
628
629 $isExtranet = \Bitrix\Im\User::getInstance($messageFields['FROM_USER_ID'])->isExtranet();
630
631 $commands = self::getListCache();
632 $bots = Bot::getListCache();
633 foreach ($commands as $value)
634 {
635 $chatEntityType = $messageFields['CHAT_ENTITY_TYPE'] ?? null;
636 if (
637 $chatEntityType === 'LIVECHAT'
638 || ($chatEntityType === 'LINES' && $bots[$value['BOT_ID']]['OPENLINE'] != 'Y')
639 )
640 {
641 continue;
642 }
643 if ($value['COMMAND'] == $command)
644 {
645 if ($value['EXTRANET_SUPPORT'] == 'N' && $isExtranet)
646 {
647 continue;
648 }
649 if ($value['COMMON'] == 'N')
650 {
651 if ($messageFields['MESSAGE_TYPE'] == IM_MESSAGE_PRIVATE)
652 {
653 if ($messageFields['TO_USER_ID'] != $value['BOT_ID'])
654 {
655 continue;
656 }
657 }
658 else
659 {
660 if (!isset($relations[$value['BOT_ID']]))
661 {
662 continue;
663 }
664 }
665 }
666
667 $result[$value['COMMAND_ID']] = $value;
668 $result[$value['COMMAND_ID']]['CONTEXT'] = $messageFields['COMMAND_CONTEXT'];
669 $result[$value['COMMAND_ID']]['EXEC_PARAMS'] = $execParams;
670 }
671 }
672
673 return $result;
674 }
675
676 private static function mergeWithDefaultCommands($commands)
677 {
678 $defaultCommands = Array(
679 Array('COMMAND' => 'me', 'TITLE' => Loc::getMessage("COMMAND_DEF_ME_TITLE"), 'PARAMS' => Loc::getMessage("COMMAND_DEF_ME_PARAMS"), 'HIDDEN' => 'N', 'EXTRANET_SUPPORT' => 'Y'),
680 Array('COMMAND' => 'loud', 'TITLE' => Loc::getMessage("COMMAND_DEF_LOUD_TITLE"), 'PARAMS' => Loc::getMessage("COMMAND_DEF_LOUD_PARAMS"), 'HIDDEN' => 'N', 'EXTRANET_SUPPORT' => 'Y'),
681 Array('COMMAND' => '>>', 'TITLE' => Loc::getMessage("COMMAND_DEF_QUOTE_TITLE"), 'PARAMS' => Loc::getMessage("COMMAND_DEF_QUOTE_PARAMS"), 'HIDDEN' => 'N', 'EXTRANET_SUPPORT' => 'Y'),
682 Array('COMMAND' => 'rename', 'TITLE' => Loc::getMessage("COMMAND_DEF_RENAME_TITLE"), 'PARAMS' => Loc::getMessage("COMMAND_DEF_RENAME_PARAMS"), 'HIDDEN' => 'N', 'EXTRANET_SUPPORT' => 'Y', 'CATEGORY' => Loc::getMessage("COMMAND_DEF_CATEGORY_CHAT"), 'CONTEXT' => 'chat'),
683 Array('COMMAND' => 'getDialogId', 'TITLE' => Loc::getMessage("COMMAND_DEF_DIALOGID_TITLE"), 'HIDDEN' => 'N', 'EXTRANET_SUPPORT' => 'N', 'CATEGORY' => Loc::getMessage("COMMAND_DEF_CATEGORY_CHAT")),
684 Array('COMMAND' => 'webrtcDebug', 'TITLE' => Loc::getMessage("COMMAND_DEF_WD_TITLE"), 'HIDDEN' => 'N', 'EXTRANET_SUPPORT' => 'Y', 'CATEGORY' => Loc::getMessage("COMMAND_DEF_CATEGORY_DEBUG"), 'CONTEXT' => 'call'),
685 );
686
687 $imCommands = Array();
688 foreach ($defaultCommands as $i => $command)
689 {
690 $newCommand['ID'] = 'def'.$i;
691 $newCommand['BOT_ID'] = 0;
692 $newCommand['APP_ID'] = '';
693 $newCommand['COMMAND'] = $command['COMMAND'];
694 $newCommand['HIDDEN'] = isset($command['HIDDEN'])? $command['HIDDEN']: 'N';
695 $newCommand['COMMON'] = 'Y';
696 $newCommand['EXTRANET_SUPPORT'] = isset($command['EXTRANET_SUPPORT'])? $command['EXTRANET_SUPPORT']: 'N';
697 $newCommand['SONET_SUPPORT'] = isset($command['SONET_SUPPORT'])? $command['SONET_SUPPORT']: 'N';
698 $newCommand['CLASS'] = '';
699 $newCommand['METHOD_COMMAND_ADD'] = '';
700 $newCommand['METHOD_LANG_GET'] = '';
701 if (!$command['TITLE'])
702 {
703 $newCommand['HIDDEN'] = 'Y';
704 }
705 $newCommand['MODULE_ID'] = 'im';
706 $newCommand['COMMAND_ID'] = $newCommand['ID'];
707 $newCommand['CATEGORY'] = isset($command['CATEGORY'])? $command['CATEGORY']: Loc::getMessage('COMMAND_IM_CATEGORY');
708 $newCommand['CONTEXT'] = isset($command['CONTEXT'])? $command['CONTEXT']: '';
709 $newCommand['TITLE'] = isset($command['TITLE'])? $command['TITLE']: '';
710 $newCommand['PARAMS'] = isset($command['PARAMS'])? $command['PARAMS']: '';
711
712 $imCommands[$newCommand['COMMAND_ID']] = $newCommand;
713 }
714
715 $result = $imCommands;
716 if (is_array($commands))
717 {
718 foreach ($commands as $i => $v)
719 {
720 $result[$i] = $v;
721 }
722 }
723
724 return $result;
725 }
726
727 public static function getListCache($lang = LANGUAGE_ID)
728 {
729 $cache = \Bitrix\Main\Data\Cache::createInstance();
730 if($cache->initCache(self::CACHE_TTL, 'list_v5_'.$lang, self::CACHE_PATH))
731 {
732 $result = $cache->getVars();
733 }
734 else
735 {
736 $loadRestLang = false;
737 $result = Array();
738 $orm = \Bitrix\Im\Model\CommandTable::getList();
739 while ($row = $orm->fetch())
740 {
741 $row['COMMAND_ID'] = $row['ID'];
742 $row['CONTEXT'] = '';
743
744 if ($row['BOT_ID'] > 0)
745 {
746 $row['CATEGORY'] = \Bitrix\Im\User::getInstance($row['BOT_ID'])->getFullName();
747 }
748 else if ($row['MODULE_ID'] == 'im')
749 {
750 $row['CATEGORY'] = Loc::getMessage('COMMAND_IM_CATEGORY');
751 }
752 else
753 {
754 $moduleClass = new \CModule();
755 $module = $moduleClass->createModuleObject($row['MODULE_ID']);
756 $row['CATEGORY'] = $module->MODULE_NAME;
757 }
758
759 if (!empty($row['CLASS']) && !empty($row['METHOD_LANG_GET']))
760 {
761 if (\Bitrix\Main\Loader::includeModule($row['MODULE_ID']) && class_exists($row["CLASS"]) && method_exists($row["CLASS"], $row["METHOD_LANG_GET"]))
762 {
763 $localize = call_user_func_array(array($row["CLASS"], $row["METHOD_LANG_GET"]), Array($row['COMMAND'], $lang));
764 if ($localize)
765 {
766 $row['TITLE'] = $localize['TITLE'];
767 $row['PARAMS'] = $localize['PARAMS'];
768 }
769 else
770 {
771 $row['HIDDEN'] = 'Y';
772 $row['METHOD_LANG_GET'] = '';
773 }
774 }
775 else
776 {
777 $row['HIDDEN'] = 'Y';
778 $row['METHOD_LANG_GET'] = '';
779 }
780 }
781 else
782 {
783 $row['TITLE'] = '';
784 $row['PARAMS'] = '';
785 if ($row['MODULE_ID'] == 'rest')
786 {
787 $loadRestLang = true;
788 if ($row['BOT_ID'] <= 0 && $row['APP_ID'])
789 {
790 $res = \Bitrix\Rest\AppTable::getList([
791 'filter' => array('=CLIENT_ID' => $row['APP_ID']),
792 ]);
793 if ($app = $res->fetch())
794 {
795 $row['CATEGORY'] = !empty($app['APP_NAME'])
796 ? $app['APP_NAME']
797 : (!empty($app['APP_NAME_DEFAULT'])
798 ? $app['APP_NAME_DEFAULT']
799 : $app['CODE']
800 )
801 ;
802 }
803 }
804 }
805 }
806 $result[$row['COMMAND_ID']] = $row;
807 }
808
809 if ($loadRestLang)
810 {
811 $langSet = Array();
812 $orm = \Bitrix\Im\Model\CommandLangTable::getList();
813 while ($row = $orm->fetch())
814 {
815 if (!isset($result[$row['COMMAND_ID']]))
816 continue;
817
818 $langSet[$row['COMMAND_ID']][$row['LANGUAGE_ID']]['TITLE'] = $row['TITLE'];
819 $langSet[$row['COMMAND_ID']][$row['LANGUAGE_ID']]['PARAMS'] = $row['PARAMS'];
820 }
821
822 $langAlter = \Bitrix\Im\Bot::getDefaultLanguage();
823 foreach ($result as $commandId => $commandData)
824 {
825 if (isset($langSet[$commandId][$lang]))
826 {
827 $result[$commandId]['TITLE'] = $langSet[$commandId][$lang]['TITLE'];
828 $result[$commandId]['PARAMS'] = $langSet[$commandId][$lang]['PARAMS'];
829 }
830 else if (isset($langSet[$commandId][$langAlter]))
831 {
832 $result[$commandId]['TITLE'] = $langSet[$commandId][$langAlter]['TITLE'];
833 $result[$commandId]['PARAMS'] = $langSet[$commandId][$langAlter]['PARAMS'];
834 }
835 else if (isset($langSet[$commandId]))
836 {
837 $langSetCommand = array_values($langSet[$commandId]);
838 $result[$commandId]['TITLE'] = $langSetCommand[0]['TITLE'];
839 $result[$commandId]['PARAMS'] = $langSetCommand[0]['PARAMS'];
840 }
841 }
842
843 foreach ($result as $key => $value)
844 {
845 if (empty($value['TITLE']))
846 {
847 $result[$key]['HIDDEN'] = 'Y';
848 $row['METHOD_LANG_GET'] = '';
849 }
850 }
851 }
852
853 if (!empty($result))
854 {
855 \Bitrix\Main\Type\Collection::sortByColumn(
856 $result,
857 Array('MODULE_ID' => SORT_ASC),
858 '',
859 null,
860 true
861 );
862 }
863
864 $result = self::mergeWithDefaultCommands($result);
865
866 $cache->startDataCache();
867 $cache->endDataCache($result);
868 }
869
870
871 return $result;
872 }
873
874 public static function getListForJs($lang = LANGUAGE_ID)
875 {
876 $commands = self::getListCache($lang);
877
878 $result = Array();
879 foreach ($commands as $command)
880 {
881 if ($command['HIDDEN'] == 'Y')
882 continue;
883
884 $result[] = Array(
885 'id' => $command['COMMAND_ID'],
886 'bot_id' => $command['BOT_ID'],
887 'command' => '/'.$command['COMMAND'],
888 'category' => $command['CATEGORY'],
889 'common' => $command['COMMON'] == 'Y',
890 'context' => $command['CONTEXT'],
891 'title' => $command['TITLE'],
892 'params' => $command['PARAMS'],
893 'extranet' => $command['EXTRANET_SUPPORT'] == 'Y',
894 );
895 }
896
897 return $result;
898 }
899
900 public static function getListSonetForJs($lang = LANGUAGE_ID)
901 {
902 $commands = self::getListCache($lang);
903
904 $result = Array();
905 foreach ($commands as $command)
906 {
907 if ($command['HIDDEN'] == 'Y')
908 continue;
909
910 if ($command['SONET_SUPPORT'] != 'Y')
911 continue;
912
913 $result[] = Array(
914 'id' => $command['COMMAND_ID'],
915 'command' => '/'.$command['COMMAND'],
916 'title' => $command['TITLE'],
917 'params' => $command['PARAMS'],
918 'extranet' => $command['EXTRANET_SUPPORT'] == 'Y',
919 );
920 }
921
922 return $result;
923 }
924
925 public static function clearCache()
926 {
927 $cache = \Bitrix\Main\Data\Cache::createInstance();
928 $cache->cleanDir(self::CACHE_PATH);
929
930 return true;
931 }
932}
static addMessage(array $access, array $messageFields)
Definition command.php:400
static update(array $command, array $updateFields)
Definition command.php:197
static getListSonetForJs($lang=LANGUAGE_ID)
Definition command.php:900
static getListCache($lang=LANGUAGE_ID)
Definition command.php:727
static getListForJs($lang=LANGUAGE_ID)
Definition command.php:874
static getDialogId($messageFields)
Definition command.php:600
static onCommandAdd($messageId, $messageFields)
Definition command.php:295
static unRegister(array $command)
Definition command.php:147
static clearCache()
Definition command.php:925
static isChatId($id)
Definition common.php:64
static getPullExtra()
Definition common.php:128
static getInstance($userId=null)
Definition user.php:44
static loadMessages($file)
Definition loc.php:64
static getMessage($code, $replace=null, $language=null)
Definition loc.php:29