public static function SetRecent($entityId, $messageId, $isChat = false, $userId = false) { $entityId = intval($entityId); $messageId = intval($messageId); if ($entityId <= 0 || $messageId <= 0) { return false; } $userId = intval($userId); if ($userId <= 0) { $userId = $GLOBALS['USER']->GetID(); } if (!$isChat && $userId == $entityId) { return false; } global $DB; $strSQL = "\n\t\t\tINSERT INTO b_im_recent (USER_ID, ITEM_TYPE, ITEM_ID, ITEM_MID)\n\t\t\tVALUES (" . $userId . ", '" . ($isChat ? IM_MESSAGE_GROUP : IM_MESSAGE_PRIVATE) . "', " . $entityId . ", " . $messageId . ")\n\t\t\tON DUPLICATE KEY UPDATE ITEM_MID = " . $messageId; $DB->Query($strSQL, false, "FILE: " . __FILE__ . "<br> LINE: " . __LINE__); $obCache = new CPHPCache(); $obCache->CleanDir('/bx/im/rec' . CIMMessenger::GetCachePath($userId)); if ($isChat) { CIMMessenger::SpeedFileDelete($userId, IM_SPEED_GROUP); } else { CIMMessenger::SpeedFileDelete($userId, IM_SPEED_MESSAGE); } return true; }
function Add($arFields) { global $DB; if (IsModuleInstalled("im") && CModule::IncludeModule("im")) { if ($arFields["MESSAGE_TYPE"] == SONET_MESSAGE_SYSTEM) { if (!(isset($arFields["EMAIL_TEMPLATE"]) && strlen($arFields["EMAIL_TEMPLATE"]) > 0)) { $arFields["EMAIL_TEMPLATE"] = "SONET_NEW_MESSAGE"; } $ID = CIMNotify::Add($arFields); return $ID; } else { CIMMessenger::SpeedFileDelete($arFields['TO_USER_ID'], IM_SPEED_MESSAGE); } } if (defined("INTASK_SKIP_SOCNET_MESSAGES1") && INTASK_SKIP_SOCNET_MESSAGES1) { $arFields["=DATE_VIEW"] = $DB->CurrentTimeFunction(); } $arFields1 = array(); foreach ($arFields as $key => $value) { if (substr($key, 0, 1) == "=") { $arFields1[substr($key, 1)] = $value; unset($arFields[$key]); } } if (!CSocNetMessages::CheckFields("ADD", $arFields)) { return false; } $db_events = GetModuleEvents("socialnetwork", "OnBeforeSocNetMessagesAdd"); while ($arEvent = $db_events->Fetch()) { if (ExecuteModuleEventEx($arEvent, array(&$arFields)) === false) { return false; } } $arInsert = $DB->PrepareInsert("b_sonet_messages", $arFields); foreach ($arFields1 as $key => $value) { if (strlen($arInsert[0]) > 0) { $arInsert[0] .= ", "; } $arInsert[0] .= $key; if (strlen($arInsert[1]) > 0) { $arInsert[1] .= ", "; } $arInsert[1] .= $value; } $ID = false; if (strlen($arInsert[0]) > 0) { $strSql = "INSERT INTO b_sonet_messages(" . $arInsert[0] . ") " . "VALUES(" . $arInsert[1] . ")"; $DB->Query($strSql, False, "File: " . __FILE__ . "<br>Line: " . __LINE__); $ID = IntVal($DB->LastID()); $events = GetModuleEvents("socialnetwork", "OnSocNetMessagesAdd"); while ($arEvent = $events->Fetch()) { ExecuteModuleEventEx($arEvent, array($ID, $arFields)); } //CSocNetMessages::SendEvent($ID, "SONET_NEW_MESSAGE"); CSocNetMessages::__SpeedFileCreate($arFields["TO_USER_ID"]); } return $ID; }
public static function SetRecent($arParams) { $itemId = intval($arParams['ENTITY_ID']); $messageId = intval($arParams['MESSAGE_ID']); if ($itemId <= 0) { return false; } $userId = intval($arParams['USER_ID']); if ($userId <= 0) { $userId = (int) $GLOBALS['USER']->GetID(); } $chatType = IM_MESSAGE_PRIVATE; if (isset($arParams['CHAT_TYPE']) && in_array($arParams['CHAT_TYPE'], array(IM_MESSAGE_OPEN, IM_MESSAGE_CHAT))) { $chatType = $arParams['CHAT_TYPE']; } else { if (isset($arParams['CHAT_ID'])) { $orm = IM\ChatTable::getById($arParams['CHAT_ID']); if ($chatData = $orm->fetch()) { $chatType = $chatData['TYPE']; } } } $isChat = in_array($chatType, array(IM_MESSAGE_OPEN, IM_MESSAGE_CHAT)); if (!$isChat && $userId == $itemId) { return false; } global $DB; $strSQL = "\n\t\t\tINSERT INTO b_im_recent (USER_ID, ITEM_TYPE, ITEM_ID, ITEM_MID)\n\t\t\tVALUES (" . $userId . ", '" . $chatType . "', " . $itemId . ", " . $messageId . ")\n\t\t\tON DUPLICATE KEY UPDATE ITEM_MID = " . $messageId . "\n\t\t"; $DB->Query($strSQL, false, "FILE: " . __FILE__ . "<br> LINE: " . __LINE__); $obCache = new CPHPCache(); $obCache->CleanDir('/bx/imc/recent' . CIMMessenger::GetCachePath($userId)); if ($isChat) { CIMMessenger::SpeedFileDelete($userId, IM_SPEED_GROUP); } else { CIMMessenger::SpeedFileDelete($userId, IM_SPEED_MESSAGE); } return true; }
public function SetReadMessage($chatId, $lastId = null) { global $DB; $chatId = intval($chatId); if ($chatId <= 0) { return false; } $sqlLastId = ''; if (intval($lastId) > 0) { $sqlLastId = "AND M.ID <= " . intval($lastId); } $strSql = "\n\t\t\tSELECT COUNT(M.ID) CNT, MAX(M.ID) ID, M.CHAT_ID\n\t\t\tFROM b_im_message M\n\t\t\tINNER JOIN b_im_relation R1 ON M.ID > R1.LAST_ID " . $sqlLastId . " AND M.CHAT_ID = R1.CHAT_ID\n\t\t\tWHERE R1.CHAT_ID = " . $chatId . " AND R1.USER_ID = " . $this->user_id . "\n\t\t\tGROUP BY M.CHAT_ID\n\t\t"; $dbRes = $DB->Query($strSql, false, "File: " . __FILE__ . "<br>Line: " . __LINE__); if ($arRes = $dbRes->Fetch()) { $bReadMessage = CIMMessage::SetLastId($chatId, $this->user_id, $arRes['ID']); if ($bReadMessage) { //CUserCounter::Decrement($this->user_id, 'im_chat_v2', '**', false, $arRes['CNT']); CIMMessenger::SpeedFileDelete($this->user_id, IM_SPEED_GROUP); if (CModule::IncludeModule("pull")) { CPushManager::DeleteFromQueueBySubTag($this->user_id, 'IM_MESS'); CPullStack::AddByUser($this->user_id, array('module_id' => 'im', 'command' => 'readMessageChat', 'params' => array('chatId' => $chatId, 'lastId' => $arRes['ID'], 'count' => $arRes['CNT']))); CIMMessenger::SendBadges($this->user_id); } return true; } } return false; }
public static function UpdateRecent($entityId, $messageId, $isChat = false, $userId = false) { $entityId = intval($entityId); $messageId = intval($messageId); if ($entityId <= 0 || $messageId <= 0) return false; $sqlUserId = ""; if (!$isChat) { if ($userId == $entityId) return false; $userId = $userId<=0? intval($GLOBALS['USER']->GetID()): intval($userId); $sqlUserId = 'USER_ID = '.($userId).' AND'; } global $DB; $strSQL = " UPDATE b_im_recent SET ITEM_MID = ".$messageId." WHERE ".$sqlUserId." ITEM_TYPE = '".($isChat? IM_MESSAGE_GROUP: IM_MESSAGE_PRIVATE)."' AND ITEM_ID = ".$entityId; $DB->Query($strSQL, false, "FILE: ".__FILE__."<br> LINE: ".__LINE__); if (!$isChat) { $obCache = new CPHPCache(); $obCache->CleanDir('/bx/imc/rec'.CIMMessenger::GetCachePath($userId)); CIMMessenger::SpeedFileDelete($userId, IM_SPEED_MESSAGE); } else { $obCache = new CPHPCache(); $arRel = CIMChat::GetRelationById($entityId); foreach ($arRel as $rel) { $obCache->CleanDir('/bx/imc/rec'.CIMMessenger::GetCachePath($rel['USER_ID'])); CIMMessenger::SpeedFileDelete($rel['USER_ID'], IM_SPEED_GROUP); } } return true; }
public static function Add($arFields) { global $DB; if (isset($arFields['TITLE']) && !isset($arFields['NOTIFY_TITLE'])) $arFields['NOTIFY_TITLE'] = $arFields['TITLE']; if (isset($arFields['NOTIFY_MESSAGE']) && !isset($arFields['MESSAGE'])) $arFields['MESSAGE'] = $arFields['NOTIFY_MESSAGE']; if (isset($arFields['NOTIFY_MESSAGE_OUT']) && !isset($arFields['MESSAGE_OUT'])) $arFields['MESSAGE_OUT'] = $arFields['NOTIFY_MESSAGE_OUT']; $bConvert = false; if (isset($arFields['CONVERT']) && $arFields['CONVERT'] == 'Y') $bConvert = true; if (!isset($arFields['MESSAGE_OUT'])) $arFields['MESSAGE_OUT'] = ""; if (!isset($arFields['MESSAGE_TYPE'])) $arFields['MESSAGE_TYPE'] = ""; if (!isset($arFields['NOTIFY_MODULE'])) $arFields['NOTIFY_MODULE'] = 'im'; if (!isset($arFields['NOTIFY_EVENT'])) $arFields['NOTIFY_EVENT'] = 'default'; if (!isset($arFields['PARAMS'])) $arFields['PARAMS'] = Array(); if ($arFields['MESSAGE_TYPE'] == IM_MESSAGE_SYSTEM) { if (!isset($arFields['NOTIFY_TYPE']) && intval($arFields['FROM_USER_ID']) > 0) $arFields['NOTIFY_TYPE'] = IM_NOTIFY_FROM; else if (!isset($arFields['NOTIFY_TYPE'])) $arFields['NOTIFY_TYPE'] = IM_NOTIFY_SYSTEM; } if (isset($arFields['NOTIFY_EMAIL_TEMPLATE']) && !isset($arFields['EMAIL_TEMPLATE'])) $arFields['EMAIL_TEMPLATE'] = $arFields['NOTIFY_EMAIL_TEMPLATE']; if (isset($arFields['EMAIL_TEMPLATE']) && strlen(trim($arFields['EMAIL_TEMPLATE']))>0) $arParams['EMAIL_TEMPLATE'] = trim($arFields['EMAIL_TEMPLATE']); if (!isset($arFields['AUTHOR_ID'])) $arFields['AUTHOR_ID'] = intval($arFields['FROM_USER_ID']); foreach(GetModuleEvents("im", "OnBeforeMessageNotifyAdd", true) as $arEvent) { $result = ExecuteModuleEventEx($arEvent, array(&$arFields)); if($result===false || isset($result['result']) && $result['result'] === false) { if (isset($result['reason'])) { $CBXSanitizer = new CBXSanitizer; $CBXSanitizer->AddTags(array( 'a' => array('href','style', 'target'), 'b' => array(), 'u' => array(), 'i' => array(), 'br' => array(), 'span' => array('style'), )); $reason = $CBXSanitizer->SanitizeHtml($result['reason']); } else { $reason = $arFields['MESSAGE_TYPE'] == IM_MESSAGE_PRIVATE? GetMessage("IM_ERROR_MESSAGE_CANCELED"): ($arFields['MESSAGE_TYPE'] == IM_MESSAGE_GROUP? GetMessage("IM_ERROR_GROUP_CANCELED"): GetMessage("IM_ERROR_NOTIFY_CANCELED")); } $GLOBALS["APPLICATION"]->ThrowException($reason, "ERROR_FROM_OTHER_MODULE"); return false; } } if (!self::CheckFields($arFields)) return false; if ($arFields['MESSAGE_TYPE'] == IM_MESSAGE_PRIVATE) { if (isset($arFields['TO_CHAT_ID'])) { $chatId = $arFields['TO_CHAT_ID']; $arRel = CIMChat::GetRelationById($chatId); foreach ($arRel as $rel) { if ($rel['USER_ID'] == $arFields['FROM_USER_ID']) continue; $arFields['TO_USER_ID'] = $rel['USER_ID']; } if (!IsModuleInstalled('intranet')) { if (CIMSettings::GetPrivacy(CIMSettings::PRIVACY_MESSAGE) == CIMSettings::PRIVACY_RESULT_CONTACT && CModule::IncludeModule('socialnetwork') && CSocNetUser::IsFriendsAllowed() && !CSocNetUserRelations::IsFriends($arFields['FROM_USER_ID'], $arFields['TO_USER_ID'])) { $GLOBALS["APPLICATION"]->ThrowException(GetMessage('IM_ERROR_MESSAGE_PRIVACY_SELF'), "ERROR_FROM_PRIVACY_SELF"); return false; } else if (CIMSettings::GetPrivacy(CIMSettings::PRIVACY_MESSAGE, $arFields['TO_USER_ID']) == CIMSettings::PRIVACY_RESULT_CONTACT && CModule::IncludeModule('socialnetwork') && CSocNetUser::IsFriendsAllowed() && !CSocNetUserRelations::IsFriends($arFields['FROM_USER_ID'], $arFields['TO_USER_ID'])) { $GLOBALS["APPLICATION"]->ThrowException(GetMessage('IM_ERROR_MESSAGE_PRIVACY'), "ERROR_FROM_PRIVACY"); return false; } } } else { $arFields['FROM_USER_ID'] = intval($arFields['FROM_USER_ID']); $arFields['TO_USER_ID'] = intval($arFields['TO_USER_ID']); if (!IsModuleInstalled('intranet')) { if (CIMSettings::GetPrivacy(CIMSettings::PRIVACY_MESSAGE) == CIMSettings::PRIVACY_RESULT_CONTACT && CModule::IncludeModule('socialnetwork') && CSocNetUser::IsFriendsAllowed() && !CSocNetUserRelations::IsFriends($arFields['FROM_USER_ID'], $arFields['TO_USER_ID'])) { $GLOBALS["APPLICATION"]->ThrowException(GetMessage('IM_ERROR_MESSAGE_PRIVACY_SELF'), "ERROR_FROM_PRIVACY_SELF"); return false; } else if (CIMSettings::GetPrivacy(CIMSettings::PRIVACY_MESSAGE, $arFields['TO_USER_ID']) == CIMSettings::PRIVACY_RESULT_CONTACT && CModule::IncludeModule('socialnetwork') && CSocNetUser::IsFriendsAllowed() && !CSocNetUserRelations::IsFriends($arFields['FROM_USER_ID'], $arFields['TO_USER_ID'])) { $GLOBALS["APPLICATION"]->ThrowException(GetMessage('IM_ERROR_MESSAGE_PRIVACY'), "ERROR_FROM_PRIVACY"); return false; } } $chatId = CIMMessage::GetChatId($arFields['FROM_USER_ID'], $arFields['TO_USER_ID']); } if ($chatId > 0) { $arParams = Array(); $arParams['CHAT_ID'] = $chatId; $arParams['AUTHOR_ID'] = intval($arFields['AUTHOR_ID']); $arParams['MESSAGE'] = trim($arFields['MESSAGE']); $arParams['MESSAGE_OUT'] = trim($arFields['MESSAGE_OUT']); $arParams['NOTIFY_MODULE'] = $arFields['NOTIFY_MODULE']; $arParams['NOTIFY_EVENT'] = $arFields['SYSTEM'] == 'Y'? 'private_system': 'private'; if (isset($arFields['IMPORT_ID'])) $arParams['IMPORT_ID'] = intval($arFields['IMPORT_ID']); if (isset($arFields['MESSAGE_DATE'])) $arParams['DATE_CREATE'] = $arFields['MESSAGE_DATE']; else $arParams['~DATE_CREATE'] = $DB->CurrentTimeFunction(); $arFiles = Array(); $arFields['FILES'] = Array(); if (isset($arFields['PARAMS']['FILE_ID'])) { foreach ($arFields['PARAMS']['FILE_ID'] as $fileId) { $arFiles[$fileId] = $fileId; } } $arFields['FILES'] = CIMDisk::GetFiles($chatId, $arFiles); $messageFiles = self::GetFormatFilesMessageOut($arFields['FILES']); if (strlen($messageFiles) > 0) { $arParams['MESSAGE_OUT'] = strlen($arParams['MESSAGE_OUT'])>0? $arParams['MESSAGE_OUT']."\n".$messageFiles: $messageFiles; $arFields['MESSAGE_OUT'] = $arParams['MESSAGE_OUT']; } $messageID = IntVal($DB->Add("b_im_message", $arParams, Array('MESSAGE','MESSAGE_OUT'))); if ($messageID <= 0) return false; if (!empty($arFields['PARAMS'])) CIMMessageParam::Set($messageID, $arFields['PARAMS']); //CUserCounter::Increment($arFields['TO_USER_ID'], 'im_message_v2', '**', false); CIMContactList::SetRecent($arFields['TO_USER_ID'], $messageID, false, $arFields['FROM_USER_ID']); CIMContactList::SetRecent($arFields['FROM_USER_ID'], $messageID, false, $arFields['TO_USER_ID']); CIMStatus::SetIdle($arFields['FROM_USER_ID'], false); if (!$bConvert) { $strSql = " UPDATE b_im_relation SET STATUS = (case when USER_ID = ".$arFields['TO_USER_ID']." then '".IM_STATUS_UNREAD."' else '".IM_STATUS_READ."' end), LAST_ID = (case when USER_ID = ".$arFields['TO_USER_ID']." then LAST_ID else ".$messageID." end), LAST_SEND_ID = (case when USER_ID = ".$arFields['TO_USER_ID']." then LAST_SEND_ID else ".$messageID." end), LAST_READ = (case when USER_ID = ".$arFields['TO_USER_ID']." then LAST_READ else ".$DB->CurrentTimeFunction()." end) WHERE CHAT_ID = ".$chatId; $DB->Query($strSql, false, "File: ".__FILE__."<br>Line: ".__LINE__); if (CModule::IncludeModule("pull")) { $arParams['FROM_USER_ID'] = $arFields['FROM_USER_ID']; $arParams['TO_USER_ID'] = $arFields['TO_USER_ID']; $pushText = ''; if (CPullOptions::GetPushStatus() && (!isset($arFields['PUSH']) || $arFields['PUSH'] == 'Y')) { $arSelect = Array("ID", "LAST_NAME", "NAME", "LOGIN", "SECOND_NAME"); $dbUsers = CUser::GetList(($sort_by = false), ($dummy=''), array('ID' => $arParams['FROM_USER_ID']), array('FIELDS' => $arSelect)); if ($arUser = $dbUsers->GetNext(true, false)) { $sName = CUser::FormatName(CSite::GetNameFormat(false), $arUser, true, false); $pushText = $sName.': '.$arParams['MESSAGE']; if (count($arFields['FILES']) > 0 && strlen($pushText) < 200) { foreach ($arFields['FILES'] as $file) { $file = " [".GetMessage('IM_MESSAGE_FILE').": ".$file['name']."]"; if (strlen($pushText.$file) > 200) break; $pushText = trim($pushText).$file; } } } } $arPullTo = Array( 'module_id' => 'im', 'command' => 'message', 'params' => CIMMessage::GetFormatMessage(Array( 'ID' => $messageID, 'CHAT_ID' => $chatId, 'TO_USER_ID' => $arParams['TO_USER_ID'], 'FROM_USER_ID' => $arParams['FROM_USER_ID'], 'SYSTEM' => $arFields['SYSTEM'] == 'Y'? 'Y': 'N', 'MESSAGE' => $arParams['MESSAGE'], 'DATE_CREATE' => time(), 'PARAMS' => $arFields['PARAMS'], 'FILES' => $arFields['FILES'], )), ); $arPullFrom = $arPullTo; $arPullTo['push_params'] = 'IM_MESS_'.$arParams['FROM_USER_ID']; $arPullTo['push_tag'] = 'IM_MESS_'.$arParams['FROM_USER_ID']; $arPullTo['push_sub_tag'] = 'IM_MESS'; $arPullTo['push_app_id'] = 'Bitrix24'; $arPullTo['push_text'] = preg_replace("/\[s\].*?\[\/s\]/i", "", $pushText); $arPullTo['push_text'] = preg_replace("/\[[bui]\](.*?)\[\/[bui]\]/i", "$1", $arPullTo['push_text']); $arPullTo['push_text'] = preg_replace("/------------------------------------------------------(.*)------------------------------------------------------/mi", " [".GetMessage('IM_QUOTE')."] ", str_replace(array("#BR#"), Array(" "), $arPullTo['push_text'])); CPullStack::AddByUser($arParams['TO_USER_ID'], $arPullTo); CPullStack::AddByUser($arParams['FROM_USER_ID'], $arPullFrom); CPushManager::DeleteFromQueueBySubTag($arParams['FROM_USER_ID'], 'IM_MESS'); self::SendBadges($arParams['TO_USER_ID']); } foreach(GetModuleEvents("im", "OnAfterMessagesAdd", true) as $arEvent) ExecuteModuleEventEx($arEvent, array(intval($messageID), $arFields)); } return $messageID; } else { $GLOBALS["APPLICATION"]->ThrowException(GetMessage("IM_ERROR_MESSAGE_CREATE"), "CHAT_ID"); return false; } } else if ($arFields['MESSAGE_TYPE'] == IM_MESSAGE_GROUP) { $arFields['FROM_USER_ID'] = intval($arFields['FROM_USER_ID']); $chatId = 0; $systemMessage = false; if (isset($arFields['SYSTEM']) && $arFields['SYSTEM'] == 'Y') { $strSql = " SELECT C.ID CHAT_ID, C.TITLE CHAT_TITLE, C.AUTHOR_ID CHAT_AUTHOR_ID FROM b_im_chat C WHERE C.ID = ".intval($arFields['TO_CHAT_ID'])." "; $systemMessage = true; } else { $strSql = " SELECT R.CHAT_ID, C.TITLE CHAT_TITLE, C.AUTHOR_ID CHAT_AUTHOR_ID FROM b_im_relation R LEFT JOIN b_im_chat C ON R.CHAT_ID = C.ID WHERE R.USER_ID = ".$arFields['FROM_USER_ID']." AND R.CHAT_ID = ".intval($arFields['TO_CHAT_ID'])." "; } $dbRes = $DB->Query($strSql, false, "File: ".__FILE__."<br>Line: ".__LINE__); if ($arRes = $dbRes->Fetch()) { $chatId = intval($arRes['CHAT_ID']); $chatTitle = htmlspecialcharsbx($arRes['CHAT_TITLE']); $chatAuthorId = intval($arRes['CHAT_AUTHOR_ID']); } else { $GLOBALS["APPLICATION"]->ThrowException(GetMessage("IM_ERROR_GROUP_CANCELED"), "CANCELED"); return false; } if ($chatId > 0) { $arParams = Array(); $arParams['CHAT_ID'] = $chatId; $arParams['AUTHOR_ID'] = $systemMessage? 0: intval($arFields['AUTHOR_ID']); $arParams['MESSAGE'] = trim($arFields['MESSAGE']); $arParams['MESSAGE_OUT'] = trim($arFields['MESSAGE_OUT']); $arParams['NOTIFY_MODULE'] = 'im'; $arParams['NOTIFY_EVENT'] = 'group'; if (isset($arFields['MESSAGE_DATE'])) $arParams['DATE_CREATE'] = $arFields['MESSAGE_DATE']; else $arParams['~DATE_CREATE'] = $DB->CurrentTimeFunction(); $arFiles = Array(); $arFields['FILES'] = Array(); if (isset($arFields['PARAMS']['FILE_ID'])) { foreach ($arFields['PARAMS']['FILE_ID'] as $fileId) { $arFiles[$fileId] = $fileId; } } $arFields['FILES'] = CIMDisk::GetFiles($chatId, $arFiles); $messageFiles = self::GetFormatFilesMessageOut($arFields['FILES']); if (strlen($messageFiles) > 0) { $arParams['MESSAGE_OUT'] = strlen($arParams['MESSAGE_OUT'])>0? $arParams['MESSAGE_OUT']."\n".$messageFiles: $messageFiles; $arFields['MESSAGE_OUT'] = $arParams['MESSAGE_OUT']; } $messageID = IntVal($DB->Add("b_im_message", $arParams, Array('MESSAGE','MESSAGE_OUT'))); if ($messageID <= 0) return false; if (!empty($arFields['PARAMS'])) CIMMessageParam::Set($messageID, $arFields['PARAMS']); $sqlCounter = "SELECT USER_ID as ID, 1 as CNT, '**' as SITE_ID, 'im_chat_v2' as CODE, 1 as SENT FROM b_im_relation R1 WHERE CHAT_ID = ".$chatId." AND USER_ID <> ".$arFields['FROM_USER_ID']; //CUserCounter::IncrementWithSelect($sqlCounter, false); $arRel = CIMChat::GetRelationById($chatId); foreach ($arRel as $rel) CIMContactList::SetRecent($chatId, $messageID, true, $rel['USER_ID']); $strSql = " UPDATE b_im_relation SET STATUS = (case when USER_ID = ".$arFields['FROM_USER_ID']." then '".IM_STATUS_READ."' else '".IM_STATUS_UNREAD."' end), LAST_ID = (case when USER_ID = ".$arFields['FROM_USER_ID']." then ".$messageID." else LAST_ID end), LAST_SEND_ID = (case when USER_ID = ".$arFields['FROM_USER_ID']." then ".$messageID." else LAST_SEND_ID end), LAST_READ = (case when USER_ID = ".$arFields['FROM_USER_ID']." then ".$DB->CurrentTimeFunction()." else LAST_READ end) WHERE CHAT_ID = ".$chatId; $DB->Query($strSql, false, "File: ".__FILE__."<br>Line: ".__LINE__); CIMStatus::SetIdle($arFields['FROM_USER_ID'], false); if (CModule::IncludeModule("pull")) { $arParams['FROM_USER_ID'] = $arFields['FROM_USER_ID']; $arParams['TO_CHAT_ID'] = $arFields['TO_CHAT_ID']; $arPullTo = Array( 'module_id' => 'im', 'command' => 'messageChat', 'params' => CIMMessage::GetFormatMessage(Array( 'ID' => $messageID, 'CHAT_ID' => $chatId, 'TO_CHAT_ID' => $arParams['TO_CHAT_ID'], 'FROM_USER_ID' => $arParams['FROM_USER_ID'], 'MESSAGE' => $arParams['MESSAGE'], 'SYSTEM' => $arFields['SYSTEM'] == 'Y'? 'Y': 'N', 'DATE_CREATE' => time(), 'PARAMS' => $arFields['PARAMS'], 'FILES' => $arFields['FILES'], )), ); $arPullFrom = $arPullTo; unset($arPullFrom['push_text']); foreach ($arRel as $rel) { if ($rel['USER_ID'] == $arParams['FROM_USER_ID']) { CPullStack::AddByUser($arParams['FROM_USER_ID'], $arPullFrom); CPushManager::DeleteFromQueueBySubTag($arParams['FROM_USER_ID'], 'IM_MESS'); } } $usersForBadges = Array(); foreach ($arRel as $rel) { if ($rel['USER_ID'] != $arParams['FROM_USER_ID']) { CPullStack::AddByUser($rel['USER_ID'], $arPullTo); $usersForBadges[] = $rel['USER_ID']; } } self::SendBadges($usersForBadges); } foreach(GetModuleEvents("im", "OnAfterMessagesAdd", true) as $arEvent) ExecuteModuleEventEx($arEvent, array(intval($messageID), $arFields)); return $messageID; } else { $GLOBALS["APPLICATION"]->ThrowException(GetMessage("IM_ERROR_MESSAGE_CREATE"), "CHAT_ID"); return false; } } else if ($arFields['MESSAGE_TYPE'] == IM_MESSAGE_SYSTEM) { $arFields['TO_USER_ID'] = intval($arFields['TO_USER_ID']); $chatId = 0; $strSql = " SELECT CHAT_ID FROM b_im_relation WHERE USER_ID = ".$arFields['TO_USER_ID']." AND MESSAGE_TYPE = '".IM_MESSAGE_SYSTEM."'"; $dbRes = $DB->Query($strSql, false, "File: ".__FILE__."<br>Line: ".__LINE__); if ($arRes = $dbRes->Fetch()) $chatId = intval($arRes['CHAT_ID']); else { $chatId = IntVal($DB->Add("b_im_chat", Array('AUTHOR_ID' => $arFields['TO_USER_ID']), Array())); if ($chatId <= 0) { $GLOBALS["APPLICATION"]->ThrowException(GetMessage("IM_ERROR_MESSAGE_CREATE"), "CHAT_ID"); return false; } $strSql = "INSERT INTO b_im_relation (CHAT_ID, MESSAGE_TYPE, USER_ID, STATUS) VALUES (".$chatId.",'".IM_MESSAGE_SYSTEM."',".intval($arFields['TO_USER_ID']).", ".($bConvert? 2: 0).")"; $DB->Query($strSql, false, "File: ".__FILE__."<br>Line: ".__LINE__); } if ($chatId > 0) { $arParams = Array(); $arParams['CHAT_ID'] = $chatId; $arParams['AUTHOR_ID'] = intval($arFields['AUTHOR_ID']); $arParams['MESSAGE'] = trim($arFields['MESSAGE']); $arParams['MESSAGE_OUT'] = trim($arFields['MESSAGE_OUT']); $arParams['NOTIFY_TYPE'] = intval($arFields['NOTIFY_TYPE']); $arParams['NOTIFY_MODULE'] = $arFields['NOTIFY_MODULE']; $arParams['NOTIFY_EVENT'] = $arFields['NOTIFY_EVENT']; //if (strlen($arParams['MESSAGE']) <= 0 && strlen($arParams['MESSAGE_OUT']) <= 0) // return false; $sendToSite = true; if ($arParams['NOTIFY_TYPE'] != IM_NOTIFY_CONFIRM) $sendToSite = CIMSettings::GetNotifyAccess($arFields["TO_USER_ID"], $arFields["NOTIFY_MODULE"], $arFields["NOTIFY_EVENT"], CIMSettings::CLIENT_SITE); if (!$sendToSite) $arParams['NOTIFY_READ'] = 'Y'; if (isset($arFields['IMPORT_ID'])) $arParams['IMPORT_ID'] = intval($arFields['IMPORT_ID']); if (isset($arFields['MESSAGE_DATE'])) $arParams['DATE_CREATE'] = $arFields['MESSAGE_DATE']; else $arParams['~DATE_CREATE'] = $DB->CurrentTimeFunction(); if (isset($arFields['EMAIL_TEMPLATE']) && strlen(trim($arFields['EMAIL_TEMPLATE']))>0) $arParams['EMAIL_TEMPLATE'] = trim($arFields['EMAIL_TEMPLATE']); if (isset($arFields['NOTIFY_TAG'])) $arParams['NOTIFY_TAG'] = $arFields['NOTIFY_TAG']; if (isset($arFields['NOTIFY_SUB_TAG'])) $arParams['NOTIFY_SUB_TAG'] = $arFields['NOTIFY_SUB_TAG']; if (isset($arFields['NOTIFY_TITLE']) && strlen(trim($arFields['NOTIFY_TITLE']))>0) $arParams['NOTIFY_TITLE'] = trim($arFields['NOTIFY_TITLE']); if ($arParams['NOTIFY_TYPE'] == IM_NOTIFY_CONFIRM) { if (isset($arFields['NOTIFY_BUTTONS'])) { foreach ($arFields['NOTIFY_BUTTONS'] as $key => $arButtons) { if (is_array($arButtons)) { if (isset($arButtons['TITLE']) && strlen($arButtons['TITLE']) > 0 && isset($arButtons['VALUE']) && strlen($arButtons['VALUE']) > 0 && isset($arButtons['TYPE']) && strlen($arButtons['TYPE']) > 0) { $arButtons['TITLE'] = htmlspecialcharsbx($arButtons['TITLE']); $arButtons['VALUE'] = htmlspecialcharsbx($arButtons['VALUE']); $arButtons['TYPE'] = htmlspecialcharsbx($arButtons['TYPE']); $arFields['NOTIFY_BUTTONS'][$key] = $arButtons; } else unset($arFields['NOTIFY_BUTTONS'][$key]); } else unset($arFields['NOTIFY_BUTTONS'][$key]); } } else { $arFields['NOTIFY_BUTTONS'] = Array( Array('TITLE' => GetMessage('IM_ERROR_BUTTON_ACCEPT'), 'VALUE' => 'Y', 'TYPE' => 'accept'), Array('TITLE' => GetMessage('IM_ERROR_BUTTON_CANCEL'), 'VALUE' => 'N', 'TYPE' => 'cancel'), ); } $arParams['NOTIFY_BUTTONS'] = serialize($arFields["NOTIFY_BUTTONS"]); if (isset($arParams['NOTIFY_TAG']) && strlen($arParams['NOTIFY_TAG'])>0) CIMNotify::DeleteByTag($arParams['NOTIFY_TAG']); } $messageID = IntVal($DB->Add("b_im_message", $arParams, Array('MESSAGE', 'MESSAGE_OUT', 'NOTIFY_BUTTONS'))); if ($messageID <= 0) return false; if ($sendToSite) CIMMessenger::SpeedFileDelete($arFields['TO_USER_ID'], IM_SPEED_NOTIFY); if (!$bConvert) { //CUserCounter::Increment($arFields['TO_USER_ID'], 'im_notify_v2', '**', false); $strSql = "UPDATE b_im_relation SET STATUS = '".IM_STATUS_UNREAD."' WHERE USER_ID = ".intval($arFields['TO_USER_ID'])." AND MESSAGE_TYPE = '".IM_MESSAGE_SYSTEM."' AND CHAT_ID = ".$chatId; $DB->Query($strSql, false, "File: ".__FILE__."<br>Line: ".__LINE__); if (CModule::IncludeModule("pull")) { CPullStack::AddByUser(intval($arFields['TO_USER_ID']), Array( 'module_id' => 'im', 'command' => 'notify', 'params' => CIMNotify::GetFormatNotify(Array( 'ID' => $messageID, 'DATE_CREATE' => time(), 'FROM_USER_ID' => intval($arFields['FROM_USER_ID']), 'MESSAGE' => $arParams['MESSAGE'], 'NOTIFY_MODULE' => $arParams['NOTIFY_MODULE'], 'NOTIFY_EVENT' => $arParams['NOTIFY_EVENT'], 'NOTIFY_TAG' => $arParams['NOTIFY_TAG'], 'NOTIFY_TYPE' => $arParams['NOTIFY_TYPE'], 'NOTIFY_BUTTONS' => isset($arParams['NOTIFY_BUTTONS'])? $arParams['NOTIFY_BUTTONS']: serialize(Array()), 'NOTIFY_TITLE' => isset($arParams['NOTIFY_TITLE'])? $arParams['NOTIFY_TITLE']: '', 'NOTIFY_SILENT' => $sendToSite? false: true, )), )); self::SendBadges($arFields['TO_USER_ID']); } foreach(GetModuleEvents("im", "OnAfterNotifyAdd", true) as $arEvent) ExecuteModuleEventEx($arEvent, array(intval($messageID), $arFields)); } return $messageID; } else { $GLOBALS["APPLICATION"]->ThrowException(GetMessage("IM_ERROR_MESSAGE_CREATE"), "CHAT_ID"); return false; } } else { $GLOBALS["APPLICATION"]->ThrowException(GetMessage("IM_ERROR_MESSAGE_TYPE"), "MESSAGE_TYPE"); return false; } return false; }
public static function SetReadMessageAll($fromUserId) { global $DB; $fromUserId = intval($fromUserId); if ($fromUserId <= 0) return false; $strSql = " SELECT RT.ID, RT.USER_ID, RT.CHAT_ID FROM b_im_relation RF INNER JOIN b_im_relation RT on RF.CHAT_ID = RT.CHAT_ID AND RT.ID != RF.ID WHERE RF.USER_ID = ".$fromUserId." AND RT.MESSAGE_TYPE = '".IM_MESSAGE_PRIVATE."' AND RT.STATUS < ".IM_STATUS_READ; $dbRes = $DB->Query($strSql, false, "File: ".__FILE__."<br>Line: ".__LINE__); if ($arRes = $dbRes->Fetch()) { self::SetLastId(intval($arRes['CHAT_ID']), $arRes['USER_ID']); //CUserCounter::Clear($arRes['USER_ID'], 'im_message_v2', '**', false); CIMMessenger::SpeedFileDelete($arRes['USER_ID'], IM_SPEED_MESSAGE); } return true; }
public static function DeleteBySubTag($notifySubTag, $authorId = false) { global $DB; if (strlen($notifySubTag) <= 0) return false; $sqlUser = ""; $sqlUser2 = ""; if ($authorId !== false) { $sqlUser = "******".intval($authorId); $sqlUser2 = " AND AUTHOR_ID = ".intval($authorId); } $dbRes = $DB->Query("SELECT R.USER_ID, R.STATUS FROM b_im_relation R, b_im_message M WHERE M.CHAT_ID = R.CHAT_ID AND M.NOTIFY_SUB_TAG = '".$DB->ForSQL($notifySubTag)."'".$sqlUser, false, "File: ".__FILE__."<br>Line: ".__LINE__); $arUsers = Array(); while ($row = $dbRes->Fetch()) { $count = $row['STATUS'] < IM_STATUS_READ? 1: 0; if (isset($arUsers[$row['USER_ID']])) $arUsers[$row['USER_ID']] += $count; else $arUsers[$row['USER_ID']] = $count; } $arUsersSend = Array(); foreach ($arUsers as $userId => $count) { CIMMessenger::SpeedFileDelete($userId, IM_SPEED_NOTIFY); //if ($count > 0) //{ // $arUsersSend[] = $userId; // CUserCounter::Decrement($userId, 'im_notify', '**', false); //} } $strSql = "DELETE FROM b_im_message WHERE NOTIFY_SUB_TAG = '".$DB->ForSQL($notifySubTag)."'".$sqlUser2; $DB->Query($strSql, false, "File: ".__FILE__."<br>Line: ".__LINE__); //CIMMessenger::SendBadges($arUsersSend); return true; }
public static function DeleteBySubTag($notifySubTag, $authorId = false) { global $DB; if (strlen($notifySubTag) <= 0) { return false; } $sqlUser = ""; $sqlUser2 = ""; if ($authorId !== false) { $sqlUser = "******" . intval($authorId); $sqlUser2 = " AND AUTHOR_ID = " . intval($authorId); } $dbRes = $DB->Query("SELECT DISTINCT USER_ID FROM b_im_relation R, b_im_message M WHERE M.CHAT_ID = R.CHAT_ID AND M.NOTIFY_SUB_TAG = '" . $DB->ForSQL($notifySubTag) . "'" . $sqlUser, false, "File: " . __FILE__ . "<br>Line: " . __LINE__); while ($arRes = $dbRes->Fetch()) { CIMMessenger::SpeedFileDelete($arRes['USER_ID'], IM_SPEED_NOTIFY); } $strSql = "DELETE FROM b_im_message WHERE NOTIFY_SUB_TAG = '" . $DB->ForSQL($notifySubTag) . "'" . $sqlUser2; $DB->Query($strSql, false, "File: " . __FILE__ . "<br>Line: " . __LINE__); return true; }
public function SetReadMessage($chatId, $lastId = null) { global $DB; $chatId = intval($chatId); if ($chatId <= 0) { return false; } $bReadMessage = false; if ($lastId == null) { $strSql = "\n\t\t\t\tSELECT MAX(M.ID) ID, M.CHAT_ID\n\t\t\t\tFROM b_im_message M\n\t\t\t\tINNER JOIN b_im_relation R1 ON M.ID >= R1.LAST_ID AND M.CHAT_ID = R1.CHAT_ID\n\t\t\t\tWHERE R1.CHAT_ID = " . $chatId . " AND R1.USER_ID = " . $this->user_id . "\n\t\t\t\tGROUP BY M.CHAT_ID\n\t\t\t"; $dbRes = $DB->Query($strSql, false, "File: " . __FILE__ . "<br>Line: " . __LINE__); if ($arRes = $dbRes->Fetch()) { $bReadMessage = CIMMessage::SetLastId($chatId, $this->user_id, $arRes['ID']); } $lastId = $arRes['ID']; } else { $bReadMessage = CIMMessage::SetLastId($chatId, $this->user_id, intval($lastId)); } if ($bReadMessage) { CIMMessenger::SpeedFileDelete($this->user_id, IM_SPEED_GROUP); if (CModule::IncludeModule("pull")) { CPushManager::DeleteFromQueue($this->user_id, 'IM_GROUP_' . $chatId); CPullStack::AddByUser($this->user_id, array('module_id' => 'im', 'command' => 'readMessageChat', 'params' => array('chatId' => $chatId, 'lastId' => $lastId))); } return true; } return false; }
public static function Add($arFields) { global $DB; if (isset($arFields['TITLE']) && !isset($arFields['NOTIFY_TITLE'])) { $arFields['NOTIFY_TITLE'] = $arFields['TITLE']; } if (isset($arFields['NOTIFY_MESSAGE']) && !isset($arFields['MESSAGE'])) { $arFields['MESSAGE'] = $arFields['NOTIFY_MESSAGE']; } if (isset($arFields['NOTIFY_MESSAGE_OUT']) && !isset($arFields['MESSAGE_OUT'])) { $arFields['MESSAGE_OUT'] = $arFields['NOTIFY_MESSAGE_OUT']; } $bConvert = false; if (isset($arFields['CONVERT']) && $arFields['CONVERT'] == 'Y') { $bConvert = true; } if (!isset($arFields['MESSAGE_OUT'])) { $arFields['MESSAGE_OUT'] = ""; } if (!isset($arFields['MESSAGE_TYPE'])) { $arFields['MESSAGE_TYPE'] = ""; } if (!isset($arFields['NOTIFY_MODULE'])) { $arFields['NOTIFY_MODULE'] = 'im'; } if (!isset($arFields['NOTIFY_EVENT'])) { $arFields['NOTIFY_EVENT'] = 'default'; } if ($arFields['MESSAGE_TYPE'] == IM_MESSAGE_SYSTEM) { if (!isset($arFields['NOTIFY_TYPE']) && intval($arFields['FROM_USER_ID']) > 0) { $arFields['NOTIFY_TYPE'] = IM_NOTIFY_FROM; } else { if (!isset($arFields['NOTIFY_TYPE'])) { $arFields['NOTIFY_TYPE'] = IM_NOTIFY_SYSTEM; } } } if (isset($arFields['NOTIFY_EMAIL_TEMPLATE']) && !isset($arFields['EMAIL_TEMPLATE'])) { $arFields['EMAIL_TEMPLATE'] = $arFields['NOTIFY_EMAIL_TEMPLATE']; } if (isset($arFields['EMAIL_TEMPLATE']) && strlen(trim($arFields['EMAIL_TEMPLATE'])) > 0) { $arParams['EMAIL_TEMPLATE'] = trim($arFields['EMAIL_TEMPLATE']); } if (!isset($arFields['AUTHOR_ID'])) { $arFields['AUTHOR_ID'] = intval($arFields['FROM_USER_ID']); } foreach (GetModuleEvents("im", "OnBeforeMessageNotifyAdd", true) as $arEvent) { $result = ExecuteModuleEventEx($arEvent, array(&$arFields)); if ($result === false || isset($result['result']) && $result['result'] === false) { if (isset($result['reason'])) { $CBXSanitizer = new CBXSanitizer(); $CBXSanitizer->AddTags(array('a' => array('href', 'style', 'target'), 'b' => array(), 'u' => array(), 'i' => array(), 'br' => array(), 'span' => array('style'))); $reason = $CBXSanitizer->SanitizeHtml($result['reason']); } else { $reason = $arFields['MESSAGE_TYPE'] == IM_MESSAGE_PRIVATE ? GetMessage("IM_ERROR_MESSAGE_CANCELED") : ($arFields['MESSAGE_TYPE'] == IM_MESSAGE_GROUP ? GetMessage("IM_ERROR_GROUP_CANCELED") : GetMessage("IM_ERROR_NOTIFY_CANCELED")); } $GLOBALS["APPLICATION"]->ThrowException($reason, "ERROR_FROM_OTHER_MODULE"); return false; } } if (!self::CheckFields($arFields)) { return false; } if ($arFields['MESSAGE_TYPE'] == IM_MESSAGE_PRIVATE) { $arFields['FROM_USER_ID'] = intval($arFields['FROM_USER_ID']); $arFields['TO_USER_ID'] = intval($arFields['TO_USER_ID']); $chatId = 0; $strSql = "\n\t\t\t\tSELECT RF.CHAT_ID\n\t\t\t\tFROM\n\t\t\t\t\tb_im_relation RF\n\t\t\t\t\tINNER JOIN b_im_relation RT on RF.CHAT_ID = RT.CHAT_ID\n\t\t\t\tWHERE\n\t\t\t\t\tRF.USER_ID = " . $arFields['FROM_USER_ID'] . "\n\t\t\t\tand RT.USER_ID = " . $arFields['TO_USER_ID'] . "\n\t\t\t\tand RF.MESSAGE_TYPE = '" . IM_MESSAGE_PRIVATE . "'\n\t\t\t"; $dbRes = $DB->Query($strSql, false, "File: " . __FILE__ . "<br>Line: " . __LINE__); if ($arRes = $dbRes->Fetch()) { $chatId = intval($arRes['CHAT_ID']); } else { $chatId = IntVal($DB->Add("b_im_chat", array('AUTHOR_ID' => $arFields['AUTHOR_ID']), array())); if ($chatId <= 0) { $GLOBALS["APPLICATION"]->ThrowException(GetMessage("IM_ERROR_MESSAGE_CREATE"), "CHAT_ID"); return false; } $strSql = "INSERT INTO b_im_relation (CHAT_ID, MESSAGE_TYPE, USER_ID, STATUS) VALUES (" . $chatId . ",'" . IM_MESSAGE_PRIVATE . "'," . $arFields['FROM_USER_ID'] . ", " . ($bConvert ? 2 : 0) . ")"; $DB->Query($strSql, false, "File: " . __FILE__ . "<br>Line: " . __LINE__); $strSql = "INSERT INTO b_im_relation (CHAT_ID, MESSAGE_TYPE, USER_ID, STATUS) VALUES (" . $chatId . ",'" . IM_MESSAGE_PRIVATE . "'," . $arFields['TO_USER_ID'] . ", " . ($bConvert ? 2 : 0) . ")"; $DB->Query($strSql, false, "File: " . __FILE__ . "<br>Line: " . __LINE__); } if ($chatId > 0) { $arParams = array(); $arParams['CHAT_ID'] = $chatId; $arParams['AUTHOR_ID'] = intval($arFields['AUTHOR_ID']); $arParams['MESSAGE'] = trim($arFields['MESSAGE']); $arParams['MESSAGE_OUT'] = trim($arFields['MESSAGE_OUT']); $arParams['NOTIFY_MODULE'] = $arFields['NOTIFY_MODULE']; $arParams['NOTIFY_EVENT'] = 'private'; if (isset($arFields['IMPORT_ID'])) { $arParams['IMPORT_ID'] = intval($arFields['IMPORT_ID']); } if (isset($arFields['MESSAGE_DATE'])) { $arParams['DATE_CREATE'] = $arFields['MESSAGE_DATE']; } else { $arParams['~DATE_CREATE'] = $DB->CurrentTimeFunction(); } $messageID = IntVal($DB->Add("b_im_message", $arParams, array('MESSAGE', 'MESSAGE_OUT'))); CIMContactList::SetRecent($arFields['TO_USER_ID'], $messageID, false, $arFields['FROM_USER_ID']); CIMContactList::UpdateRecent($arFields['FROM_USER_ID'], $messageID, false, $arFields['TO_USER_ID']); if (!$bConvert) { if (CModule::IncludeModule("pull")) { $arParams['FROM_USER_ID'] = $arFields['FROM_USER_ID']; $arParams['TO_USER_ID'] = $arFields['TO_USER_ID']; $pushText = ''; if (CPullOptions::GetPushStatus()) { $arSelect = array("ID", "LAST_NAME", "NAME", "LOGIN", "SECOND_NAME"); $dbUsers = CUser::GetList($sort_by = false, $dummy = '', array('ID' => $arParams['FROM_USER_ID']), array('FIELDS' => $arSelect)); if ($arUser = $dbUsers->GetNext(true, false)) { $sName = CUser::FormatName(CSite::GetNameFormat(false), $arUser, true, false); $pushText = $sName . ': ' . $arParams['MESSAGE']; } } $arPullTo = array('module_id' => 'im', 'command' => 'message', 'params' => CIMMessage::GetFormatMessage(array('ID' => $messageID, 'TO_USER_ID' => $arParams['TO_USER_ID'], 'FROM_USER_ID' => $arParams['FROM_USER_ID'], 'MESSAGE' => $arParams['MESSAGE'], 'DATE_CREATE' => time())), 'push_params' => 'IM_MESS_' . $arParams['FROM_USER_ID'], 'push_tag' => 'IM_MESS_' . $arParams['FROM_USER_ID'], 'push_text' => $pushText); $arPullFrom = $arPullTo; unset($arPullFrom['push_text']); CPullStack::AddByUser($arParams['TO_USER_ID'], $arPullTo); CPullStack::AddByUser($arParams['FROM_USER_ID'], $arPullFrom); } $strSql = "\n\t\t\t\t\t\tUPDATE b_im_relation SET STATUS = '" . IM_STATUS_UNREAD . "'\n\t\t\t\t\t\tWHERE USER_ID = " . $arFields['TO_USER_ID'] . " AND MESSAGE_TYPE = '" . IM_MESSAGE_PRIVATE . "' AND CHAT_ID = " . $chatId; $dbRes = $DB->Query($strSql, false, "File: " . __FILE__ . "<br>Line: " . __LINE__); foreach (GetModuleEvents("im", "OnAfterMessagesAdd", true) as $arEvent) { ExecuteModuleEventEx($arEvent, array(intval($messageID), $arFields)); } } return $messageID; } else { $GLOBALS["APPLICATION"]->ThrowException(GetMessage("IM_ERROR_MESSAGE_CREATE"), "CHAT_ID"); return false; } } else { if ($arFields['MESSAGE_TYPE'] == IM_MESSAGE_GROUP) { $chatId = 0; $systemMessage = false; if (isset($arFields['SYSTEM']) && $arFields['SYSTEM'] == 'Y' && intval($arFields['FROM_USER_ID']) <= 0) { $strSql = "\n\t\t\t\t\tSELECT C.ID CHAT_ID, C.TITLE CHAT_TITLE, C.AUTHOR_ID CHAT_AUTHOR_ID\n\t\t\t\t\tFROM b_im_chat C\n\t\t\t\t\tWHERE C.ID = " . intval($arFields['TO_CHAT_ID']) . "\n\t\t\t\t"; $systemMessage = true; } else { $strSql = "\n\t\t\t\t\tSELECT R.CHAT_ID, C.TITLE CHAT_TITLE, C.AUTHOR_ID CHAT_AUTHOR_ID\n\t\t\t\t\tFROM b_im_relation R LEFT JOIN b_im_chat C ON R.CHAT_ID = C.ID\n\t\t\t\t\tWHERE R.USER_ID = " . intval($arFields['FROM_USER_ID']) . " AND R.MESSAGE_TYPE = '" . IM_MESSAGE_GROUP . "' AND R.CHAT_ID = " . intval($arFields['TO_CHAT_ID']) . "\n\t\t\t\t"; } $dbRes = $DB->Query($strSql, false, "File: " . __FILE__ . "<br>Line: " . __LINE__); if ($arRes = $dbRes->Fetch()) { $chatId = intval($arRes['CHAT_ID']); $chatTitle = htmlspecialcharsbx($arRes['CHAT_TITLE']); $chatAuthorId = intval($arRes['CHAT_AUTHOR_ID']); } else { $GLOBALS["APPLICATION"]->ThrowException(GetMessage("IM_ERROR_GROUP_CANCELED"), "CANCELED"); return false; } if ($chatId > 0) { $arParams = array(); $arParams['CHAT_ID'] = $chatId; $arParams['AUTHOR_ID'] = intval($arFields['AUTHOR_ID']); $arParams['MESSAGE'] = trim($arFields['MESSAGE']); $arParams['MESSAGE_OUT'] = trim($arFields['MESSAGE_OUT']); $arParams['NOTIFY_MODULE'] = 'im'; $arParams['NOTIFY_EVENT'] = 'group'; if (isset($arFields['MESSAGE_DATE'])) { $arParams['DATE_CREATE'] = $arFields['MESSAGE_DATE']; } else { $arParams['~DATE_CREATE'] = $DB->CurrentTimeFunction(); } $messageID = IntVal($DB->Add("b_im_message", $arParams, array('MESSAGE', 'MESSAGE_OUT'))); //if(!$systemMessage) CIMContactList::SetRecent($chatId, $messageID, true, $arFields['FROM_USER_ID']); CIMContactList::UpdateRecent($chatId, $messageID, true); if (CModule::IncludeModule("pull")) { $arParams['FROM_USER_ID'] = $arFields['FROM_USER_ID']; $arParams['TO_CHAT_ID'] = $arFields['TO_CHAT_ID']; $arPullTo = array('module_id' => 'im', 'command' => 'messageChat', 'params' => CIMMessage::GetFormatMessage(array('ID' => $messageID, 'TO_CHAT_ID' => $arParams['TO_CHAT_ID'], 'FROM_USER_ID' => $arParams['FROM_USER_ID'], 'MESSAGE' => $arParams['MESSAGE'], 'DATE_CREATE' => time()))); $arPullFrom = $arPullTo; unset($arPullFrom['push_text']); CPullStack::AddByUser($arParams['FROM_USER_ID'], $arPullFrom); $arRel = CIMChat::GetRelationById($chatId); foreach ($arRel as $rel) { if ($rel['USER_ID'] != $arParams['FROM_USER_ID']) { CPullStack::AddByUser($rel['USER_ID'], $arPullTo); } } } if (!$systemMessage) { $strSql = "\n\t\t\t\t\t\tUPDATE b_im_relation SET STATUS = '" . IM_STATUS_UNREAD . "'\n\t\t\t\t\t\tWHERE USER_ID <> " . $arFields['FROM_USER_ID'] . " AND MESSAGE_TYPE = '" . IM_MESSAGE_GROUP . "' AND CHAT_ID = " . $chatId; $dbRes = $DB->Query($strSql, false, "File: " . __FILE__ . "<br>Line: " . __LINE__); } foreach (GetModuleEvents("im", "OnAfterMessagesAdd", true) as $arEvent) { ExecuteModuleEventEx($arEvent, array(intval($messageID), $arFields)); } return $messageID; } else { $GLOBALS["APPLICATION"]->ThrowException(GetMessage("IM_ERROR_MESSAGE_CREATE"), "CHAT_ID"); return false; } } else { if ($arFields['MESSAGE_TYPE'] == IM_MESSAGE_SYSTEM) { $chatId = 0; $strSql = "\n\t\t\t\tSELECT CHAT_ID\n\t\t\t\tFROM b_im_relation\n\t\t\t\tWHERE USER_ID = " . intval($arFields['TO_USER_ID']) . " AND MESSAGE_TYPE = '" . IM_MESSAGE_SYSTEM . "'"; $dbRes = $DB->Query($strSql, false, "File: " . __FILE__ . "<br>Line: " . __LINE__); if ($arRes = $dbRes->Fetch()) { $chatId = intval($arRes['CHAT_ID']); } else { $chatId = IntVal($DB->Add("b_im_chat", array('AUTHOR_ID' => intval($arFields['TO_USER_ID'])), array())); if ($chatId <= 0) { $GLOBALS["APPLICATION"]->ThrowException(GetMessage("IM_ERROR_MESSAGE_CREATE"), "CHAT_ID"); return false; } $strSql = "INSERT INTO b_im_relation (CHAT_ID, MESSAGE_TYPE, USER_ID, STATUS) VALUES (" . $chatId . ",'" . IM_MESSAGE_SYSTEM . "'," . intval($arFields['TO_USER_ID']) . ", " . ($bConvert ? 2 : 0) . ")"; $DB->Query($strSql, false, "File: " . __FILE__ . "<br>Line: " . __LINE__); } if ($chatId > 0) { $arParams = array(); $arParams['CHAT_ID'] = $chatId; $arParams['AUTHOR_ID'] = intval($arFields['AUTHOR_ID']); $arParams['MESSAGE'] = trim($arFields['MESSAGE']); $arParams['MESSAGE_OUT'] = trim($arFields['MESSAGE_OUT']); $arParams['NOTIFY_TYPE'] = intval($arFields['NOTIFY_TYPE']); $arParams['NOTIFY_MODULE'] = $arFields['NOTIFY_MODULE']; $arParams['NOTIFY_EVENT'] = $arFields['NOTIFY_EVENT']; if (isset($arFields['IMPORT_ID'])) { $arParams['IMPORT_ID'] = intval($arFields['IMPORT_ID']); } if (isset($arFields['MESSAGE_DATE'])) { $arParams['DATE_CREATE'] = $arFields['MESSAGE_DATE']; } else { $arParams['~DATE_CREATE'] = $DB->CurrentTimeFunction(); } if (isset($arFields['EMAIL_TEMPLATE']) && strlen(trim($arFields['EMAIL_TEMPLATE'])) > 0) { $arParams['EMAIL_TEMPLATE'] = trim($arFields['EMAIL_TEMPLATE']); } if (isset($arFields['NOTIFY_TAG'])) { $arParams['NOTIFY_TAG'] = $arFields['NOTIFY_TAG']; } if (isset($arFields['NOTIFY_SUB_TAG'])) { $arParams['NOTIFY_SUB_TAG'] = $arFields['NOTIFY_SUB_TAG']; } if (isset($arFields['NOTIFY_TITLE']) && strlen(trim($arFields['NOTIFY_TITLE'])) > 0) { $arParams['NOTIFY_TITLE'] = trim($arFields['NOTIFY_TITLE']); } if ($arParams['NOTIFY_TYPE'] == IM_NOTIFY_CONFIRM) { if (isset($arFields['NOTIFY_BUTTONS'])) { foreach ($arFields['NOTIFY_BUTTONS'] as $key => $arButtons) { if (is_array($arButtons)) { if (isset($arButtons['TITLE']) && strlen($arButtons['TITLE']) > 0 && isset($arButtons['VALUE']) && strlen($arButtons['VALUE']) > 0 && isset($arButtons['TYPE']) && strlen($arButtons['TYPE']) > 0) { $arButtons['TITLE'] = htmlspecialcharsbx($arButtons['TITLE']); $arButtons['VALUE'] = htmlspecialcharsbx($arButtons['VALUE']); $arButtons['TYPE'] = htmlspecialcharsbx($arButtons['TYPE']); $arFields['NOTIFY_BUTTONS'][$key] = $arButtons; } else { unset($arFields['NOTIFY_BUTTONS'][$key]); } } else { unset($arFields['NOTIFY_BUTTONS'][$key]); } } } else { $arFields['NOTIFY_BUTTONS'] = array(array('TITLE' => GetMessage('IM_ERROR_BUTTON_ACCEPT'), 'VALUE' => 'Y', 'TYPE' => 'accept'), array('TITLE' => GetMessage('IM_ERROR_BUTTON_CANCEL'), 'VALUE' => 'N', 'TYPE' => 'cancel')); } $arParams['NOTIFY_BUTTONS'] = serialize($arFields["NOTIFY_BUTTONS"]); if (isset($arParams['NOTIFY_TAG']) && strlen($arParams['NOTIFY_TAG']) > 0) { CIMNotify::DeleteByTag($arParams['NOTIFY_TAG']); } } $messageID = IntVal($DB->Add("b_im_message", $arParams, array('MESSAGE', 'MESSAGE_OUT', 'NOTIFY_BUTTONS'))); CIMMessenger::SpeedFileDelete(intval($arFields['TO_USER_ID']), IM_SPEED_NOTIFY); if (!$bConvert) { if (CModule::IncludeModule("pull")) { CPullStack::AddByUser(intval($arFields['TO_USER_ID']), array('module_id' => 'im', 'command' => 'notify', 'params' => CIMNotify::GetFormatNotify(array('ID' => $messageID, 'TIMESTAMP' => time(), 'FROM_USER_ID' => intval($arFields['FROM_USER_ID']), 'MESSAGE' => $arParams['MESSAGE'], 'NOTIFY_TAG' => $arParams['NOTIFY_TAG'], 'NOTIFY_TYPE' => $arParams['NOTIFY_TYPE'], 'NOTIFY_BUTTONS' => isset($arParams['NOTIFY_BUTTONS']) ? $arParams['NOTIFY_BUTTONS'] : serialize(array()), 'NOTIFY_TITLE' => isset($arParams['NOTIFY_TITLE']) ? $arParams['NOTIFY_TITLE'] : '')))); } $strSql = "\n\t\t\t\t\t\tUPDATE b_im_relation SET STATUS = '" . IM_STATUS_UNREAD . "'\n\t\t\t\t\t\tWHERE USER_ID = " . intval($arFields['TO_USER_ID']) . " AND MESSAGE_TYPE = '" . IM_MESSAGE_SYSTEM . "' AND CHAT_ID = " . $chatId; $dbRes = $DB->Query($strSql, false, "File: " . __FILE__ . "<br>Line: " . __LINE__); foreach (GetModuleEvents("im", "OnAfterNotifyAdd", true) as $arEvent) { ExecuteModuleEventEx($arEvent, array(intval($messageID), $arFields)); } } return $messageID; } else { $GLOBALS["APPLICATION"]->ThrowException(GetMessage("IM_ERROR_MESSAGE_CREATE"), "CHAT_ID"); return false; } } else { $GLOBALS["APPLICATION"]->ThrowException(GetMessage("IM_ERROR_MESSAGE_TYPE"), "MESSAGE_TYPE"); return false; } } } return false; }
public static function Add($arFields) { global $DB; if (isset($arFields['TITLE']) && !isset($arFields['NOTIFY_TITLE'])) { $arFields['NOTIFY_TITLE'] = $arFields['TITLE']; } if (isset($arFields['NOTIFY_MESSAGE']) && !isset($arFields['MESSAGE'])) { $arFields['MESSAGE'] = $arFields['NOTIFY_MESSAGE']; } if (isset($arFields['NOTIFY_MESSAGE_OUT']) && !isset($arFields['MESSAGE_OUT'])) { $arFields['MESSAGE_OUT'] = $arFields['NOTIFY_MESSAGE_OUT']; } $bConvert = false; if (isset($arFields['CONVERT']) && $arFields['CONVERT'] == 'Y') { $bConvert = true; } if (!isset($arFields['MESSAGE_OUT'])) { $arFields['MESSAGE_OUT'] = ""; } if (!isset($arFields['MESSAGE_TYPE'])) { $arFields['MESSAGE_TYPE'] = ""; } if (!isset($arFields['NOTIFY_MODULE'])) { $arFields['NOTIFY_MODULE'] = 'im'; } if (!isset($arFields['NOTIFY_EVENT'])) { $arFields['NOTIFY_EVENT'] = 'default'; } if (!isset($arFields['PARAMS'])) { $arFields['PARAMS'] = array(); } if ($arFields['MESSAGE_TYPE'] == IM_MESSAGE_SYSTEM) { if (!isset($arFields['NOTIFY_TYPE']) && intval($arFields['FROM_USER_ID']) > 0) { $arFields['NOTIFY_TYPE'] = IM_NOTIFY_FROM; } else { if (!isset($arFields['NOTIFY_TYPE'])) { $arFields['NOTIFY_TYPE'] = IM_NOTIFY_SYSTEM; } } } if (isset($arFields['NOTIFY_EMAIL_TEMPLATE']) && !isset($arFields['EMAIL_TEMPLATE'])) { $arFields['EMAIL_TEMPLATE'] = $arFields['NOTIFY_EMAIL_TEMPLATE']; } if (isset($arFields['EMAIL_TEMPLATE']) && strlen(trim($arFields['EMAIL_TEMPLATE'])) > 0) { $arParams['EMAIL_TEMPLATE'] = trim($arFields['EMAIL_TEMPLATE']); } if (!isset($arFields['AUTHOR_ID'])) { $arFields['AUTHOR_ID'] = intval($arFields['FROM_USER_ID']); } foreach (GetModuleEvents("im", "OnBeforeMessageNotifyAdd", true) as $arEvent) { $result = ExecuteModuleEventEx($arEvent, array(&$arFields)); if ($result === false || isset($result['result']) && $result['result'] === false) { if (isset($result['reason'])) { $CBXSanitizer = new CBXSanitizer(); $CBXSanitizer->AddTags(array('a' => array('href', 'style', 'target'), 'b' => array(), 'u' => array(), 'i' => array(), 'br' => array(), 'span' => array('style'))); $reason = $CBXSanitizer->SanitizeHtml($result['reason']); } else { if ($arFields['MESSAGE_TYPE'] == IM_MESSAGE_PRIVATE) { $reason = GetMessage("IM_ERROR_MESSAGE_CANCELED"); } else { if ($arFields['MESSAGE_TYPE'] == IM_MESSAGE_SYSTEM) { $reason = GetMessage("IM_ERROR_NOTIFY_CANCELED"); } else { $reason = GetMessage("IM_ERROR_GROUP_CANCELED"); } } } $GLOBALS["APPLICATION"]->ThrowException($reason, "ERROR_FROM_OTHER_MODULE"); return false; } } if (!self::CheckFields($arFields)) { return false; } if ($arFields['MESSAGE_TYPE'] == IM_MESSAGE_PRIVATE) { if (isset($arFields['TO_CHAT_ID'])) { $chatId = $arFields['TO_CHAT_ID']; $arRel = CIMChat::GetRelationById($chatId); foreach ($arRel as $rel) { if ($rel['USER_ID'] == $arFields['FROM_USER_ID']) { continue; } $arFields['TO_USER_ID'] = $rel['USER_ID']; } if (!IsModuleInstalled('intranet')) { if (CIMSettings::GetPrivacy(CIMSettings::PRIVACY_MESSAGE) == CIMSettings::PRIVACY_RESULT_CONTACT && CModule::IncludeModule('socialnetwork') && CSocNetUser::IsFriendsAllowed() && !CSocNetUserRelations::IsFriends($arFields['FROM_USER_ID'], $arFields['TO_USER_ID'])) { $GLOBALS["APPLICATION"]->ThrowException(GetMessage('IM_ERROR_MESSAGE_PRIVACY_SELF'), "ERROR_FROM_PRIVACY_SELF"); return false; } else { if (CIMSettings::GetPrivacy(CIMSettings::PRIVACY_MESSAGE, $arFields['TO_USER_ID']) == CIMSettings::PRIVACY_RESULT_CONTACT && CModule::IncludeModule('socialnetwork') && CSocNetUser::IsFriendsAllowed() && !CSocNetUserRelations::IsFriends($arFields['FROM_USER_ID'], $arFields['TO_USER_ID'])) { $GLOBALS["APPLICATION"]->ThrowException(GetMessage('IM_ERROR_MESSAGE_PRIVACY'), "ERROR_FROM_PRIVACY"); return false; } } } } else { $arFields['FROM_USER_ID'] = intval($arFields['FROM_USER_ID']); $arFields['TO_USER_ID'] = intval($arFields['TO_USER_ID']); if (!IsModuleInstalled('intranet')) { if (CIMSettings::GetPrivacy(CIMSettings::PRIVACY_MESSAGE) == CIMSettings::PRIVACY_RESULT_CONTACT && CModule::IncludeModule('socialnetwork') && CSocNetUser::IsFriendsAllowed() && !CSocNetUserRelations::IsFriends($arFields['FROM_USER_ID'], $arFields['TO_USER_ID'])) { $GLOBALS["APPLICATION"]->ThrowException(GetMessage('IM_ERROR_MESSAGE_PRIVACY_SELF'), "ERROR_FROM_PRIVACY_SELF"); return false; } else { if (CIMSettings::GetPrivacy(CIMSettings::PRIVACY_MESSAGE, $arFields['TO_USER_ID']) == CIMSettings::PRIVACY_RESULT_CONTACT && CModule::IncludeModule('socialnetwork') && CSocNetUser::IsFriendsAllowed() && !CSocNetUserRelations::IsFriends($arFields['FROM_USER_ID'], $arFields['TO_USER_ID'])) { $GLOBALS["APPLICATION"]->ThrowException(GetMessage('IM_ERROR_MESSAGE_PRIVACY'), "ERROR_FROM_PRIVACY"); return false; } } } $chatId = CIMMessage::GetChatId($arFields['FROM_USER_ID'], $arFields['TO_USER_ID']); } if ($chatId > 0) { $arParams = array(); $arParams['CHAT_ID'] = $chatId; $arParams['AUTHOR_ID'] = intval($arFields['AUTHOR_ID']); $arParams['MESSAGE'] = trim($arFields['MESSAGE']); $arParams['MESSAGE_OUT'] = trim($arFields['MESSAGE_OUT']); $arParams['NOTIFY_MODULE'] = $arFields['NOTIFY_MODULE']; $arParams['NOTIFY_EVENT'] = $arFields['SYSTEM'] == 'Y' ? 'private_system' : 'private'; if (isset($arFields['IMPORT_ID'])) { $arParams['IMPORT_ID'] = intval($arFields['IMPORT_ID']); } if (isset($arFields['MESSAGE_DATE'])) { $arParams['DATE_CREATE'] = $arFields['MESSAGE_DATE']; } $arFiles = array(); $arFields['FILES'] = array(); if (isset($arFields['PARAMS']['FILE_ID'])) { foreach ($arFields['PARAMS']['FILE_ID'] as $fileId) { $arFiles[$fileId] = $fileId; } } $arFields['FILES'] = CIMDisk::GetFiles($chatId, $arFiles); $messageFiles = self::GetFormatFilesMessageOut($arFields['FILES']); if (strlen($messageFiles) > 0) { $arParams['MESSAGE_OUT'] = strlen($arParams['MESSAGE_OUT']) > 0 ? $arParams['MESSAGE_OUT'] . "\n" . $messageFiles : $messageFiles; $arFields['MESSAGE_OUT'] = $arParams['MESSAGE_OUT']; } $result = IM\MessageTable::add($arParams); $messageID = IntVal($result->getId()); if ($messageID <= 0) { return false; } IM\ChatTable::update($chatId, array('LAST_MESSAGE_ID' => $messageID)); if (!empty($arFields['PARAMS'])) { CIMMessageParam::Set($messageID, $arFields['PARAMS']); } //CUserCounter::Increment($arFields['TO_USER_ID'], 'im_message_v2', '**', false); CIMContactList::SetRecent(array('ENTITY_ID' => $arFields['TO_USER_ID'], 'MESSAGE_ID' => $messageID, 'CHAT_TYPE' => IM_MESSAGE_PRIVATE, 'USER_ID' => $arFields['FROM_USER_ID'])); CIMContactList::SetRecent(array('ENTITY_ID' => $arFields['FROM_USER_ID'], 'MESSAGE_ID' => $messageID, 'CHAT_TYPE' => IM_MESSAGE_PRIVATE, 'USER_ID' => $arFields['TO_USER_ID'])); CIMStatus::SetIdle($arFields['FROM_USER_ID'], false); if (!$bConvert) { $arRel = CIMChat::GetRelationById($chatId); foreach ($arRel as $relation) { if ($relation["USER_ID"] == $arFields["TO_USER_ID"]) { if ($relation['STATUS'] != IM_STATUS_UNREAD) { IM\RelationTable::update($relation["ID"], array("STATUS" => IM_STATUS_UNREAD)); } } else { IM\RelationTable::update($relation["ID"], array("STATUS" => IM_STATUS_READ, "LAST_ID" => $messageID, "LAST_SEND_ID" => $messageID, "LAST_READ" => new Bitrix\Main\Type\DateTime())); } } if (CModule::IncludeModule("pull")) { $arParams['FROM_USER_ID'] = $arFields['FROM_USER_ID']; $arParams['TO_USER_ID'] = $arFields['TO_USER_ID']; $arPullTo = array('module_id' => 'im', 'command' => 'message', 'params' => CIMMessage::GetFormatMessage(array('ID' => $messageID, 'CHAT_ID' => $chatId, 'TO_USER_ID' => $arParams['TO_USER_ID'], 'FROM_USER_ID' => $arParams['FROM_USER_ID'], 'SYSTEM' => $arFields['SYSTEM'] == 'Y' ? 'Y' : 'N', 'MESSAGE' => $arParams['MESSAGE'], 'DATE_CREATE' => time(), 'PARAMS' => $arFields['PARAMS'], 'FILES' => $arFields['FILES']))); $arPullFrom = $arPullTo; $pushText = ''; if (CPullOptions::GetPushStatus() && (!isset($arFields['PUSH']) || $arFields['PUSH'] == 'Y')) { $arSelect = array("ID", "LAST_NAME", "NAME", "LOGIN", "SECOND_NAME"); $dbUsers = CUser::GetList($sort_by = false, $dummy = '', array('ID' => $arParams['FROM_USER_ID']), array('FIELDS' => $arSelect)); if ($arUser = $dbUsers->GetNext(true, false)) { $sName = CUser::FormatName(CSite::GetNameFormat(false), $arUser, true, false); $pushText = $sName . ': ' . $arParams['MESSAGE']; if (count($arFields['FILES']) > 0 && strlen($pushText) < 200) { foreach ($arFields['FILES'] as $file) { $file = " [" . GetMessage('IM_MESSAGE_FILE') . ": " . $file['name'] . "]"; if (strlen($pushText . $file) > 200) { break; } $pushText = trim($pushText) . $file; } } } if (CIMSettings::GetNotifyAccess($arParams["TO_USER_ID"], 'im', 'message', CIMSettings::CLIENT_PUSH)) { $arPullTo['push_params'] = array('TAG' => 'IM_MESS_' . $arParams['FROM_USER_ID'], 'CATEGORY' => 'ANSWER', 'URL' => SITE_DIR . 'mobile/ajax.php?mobile_action=im_answer', 'PARAMS' => array('RECIPIENT_ID' => $arParams['FROM_USER_ID'])); $arPullTo['push_tag'] = 'IM_MESS_' . $arParams['FROM_USER_ID']; $arPullTo['push_sub_tag'] = 'IM_MESS'; $arPullTo['push_app_id'] = 'Bitrix24'; $arPullTo['push_text'] = preg_replace("/\\[s\\].*?\\[\\/s\\]/i", "", $pushText); $arPullTo['push_text'] = preg_replace("/\\[[bui]\\](.*?)\\[\\/[bui]\\]/i", "\$1", $arPullTo['push_text']); $arPullTo['push_text'] = preg_replace("/\\[PCH=([0-9]{1,})\\](.*?)\\[\\/PCH\\]/i", "\$2", $arPullTo['push_text']); $arPullTo['push_text'] = preg_replace("/\\[USER=([0-9]{1,})\\](.*?)\\[\\/USER\\]/i", "\$2", $arPullTo['push_text']); $arPullTo['push_text'] = preg_replace('#\\-{54}.+?\\-{54}#s', " [" . GetMessage('IM_QUOTE') . "] ", str_replace(array("#BR#"), array(" "), $arPullTo['push_text'])); } } CPullStack::AddByUser($arParams['TO_USER_ID'], $arPullTo); CPullStack::AddByUser($arParams['FROM_USER_ID'], $arPullFrom); CPushManager::DeleteFromQueueBySubTag($arParams['FROM_USER_ID'], 'IM_MESS'); self::SendBadges($arParams['TO_USER_ID']); } foreach (GetModuleEvents("im", "OnAfterMessagesAdd", true) as $arEvent) { ExecuteModuleEventEx($arEvent, array(intval($messageID), $arFields)); } } return $messageID; } else { $GLOBALS["APPLICATION"]->ThrowException(GetMessage("IM_ERROR_MESSAGE_CREATE"), "CHAT_ID"); return false; } } else { if ($arFields['MESSAGE_TYPE'] == IM_MESSAGE_CHAT || $arFields['MESSAGE_TYPE'] == IM_MESSAGE_OPEN) { $arFields['FROM_USER_ID'] = intval($arFields['FROM_USER_ID']); $chatId = 0; $systemMessage = false; if (isset($arFields['SYSTEM']) && $arFields['SYSTEM'] == 'Y') { $strSql = "\n\t\t\t\t\tSELECT C.ID CHAT_ID, C.TITLE CHAT_TITLE, C.AUTHOR_ID CHAT_AUTHOR_ID, C.TYPE CHAT_TYPE, '1' RID\n\t\t\t\t\tFROM b_im_chat C\n\t\t\t\t\tWHERE C.ID = " . intval($arFields['TO_CHAT_ID']) . "\n\t\t\t\t"; $systemMessage = true; } else { $strSql = "\n\t\t\t\t\tSELECT C.ID CHAT_ID, C.TITLE CHAT_TITLE, C.AUTHOR_ID CHAT_AUTHOR_ID, C.TYPE CHAT_TYPE, R.USER_ID RID\n\t\t\t\t\tFROM b_im_chat C\n\t\t\t\t\tLEFT JOIN b_im_relation R ON R.CHAT_ID = C.ID AND R.USER_ID = " . $arFields['FROM_USER_ID'] . "\n\t\t\t\t\tWHERE C.ID = " . intval($arFields['TO_CHAT_ID']) . "\n\t\t\t\t"; } $dbRes = $DB->Query($strSql, false, "File: " . __FILE__ . "<br>Line: " . __LINE__); if ($arRes = $dbRes->Fetch()) { $chatId = intval($arRes['CHAT_ID']); $chatTitle = htmlspecialcharsbx($arRes['CHAT_TITLE']); $chatAuthorId = intval($arRes['CHAT_AUTHOR_ID']); $arFields['MESSAGE_TYPE'] = $arRes['CHAT_TYPE']; if ($arRes['CHAT_TYPE'] == IM_MESSAGE_OPEN) { if (intval($arRes['RID']) <= 0) { if (IM\User::getInstance($arFields['FROM_USER_ID'])->isExtranet()) { $GLOBALS["APPLICATION"]->ThrowException(GetMessage("IM_ERROR_GROUP_CANCELED"), "CANCELED"); return false; } else { $chat = new CIMChat(0); $chat->AddUser($chatId, $arFields['FROM_USER_ID']); } } } else { if (intval($arRes['RID']) <= 0) { $GLOBALS["APPLICATION"]->ThrowException(GetMessage("IM_ERROR_GROUP_CANCELED"), "CANCELED"); return false; } } } else { $GLOBALS["APPLICATION"]->ThrowException(GetMessage("IM_ERROR_GROUP_CANCELED"), "CANCELED"); return false; } if ($chatId > 0) { $arParams = array(); $arParams['CHAT_ID'] = $chatId; $arParams['AUTHOR_ID'] = $systemMessage ? 0 : intval($arFields['AUTHOR_ID']); $arParams['MESSAGE'] = trim($arFields['MESSAGE']); $arParams['MESSAGE_OUT'] = trim($arFields['MESSAGE_OUT']); $arParams['NOTIFY_MODULE'] = 'im'; $arParams['NOTIFY_EVENT'] = 'group'; if (isset($arFields['MESSAGE_DATE'])) { $arParams['DATE_CREATE'] = $arFields['MESSAGE_DATE']; } $arFiles = array(); $arFields['FILES'] = array(); if (isset($arFields['PARAMS']['FILE_ID'])) { foreach ($arFields['PARAMS']['FILE_ID'] as $fileId) { $arFiles[$fileId] = $fileId; } } $arFields['FILES'] = CIMDisk::GetFiles($chatId, $arFiles); $messageFiles = self::GetFormatFilesMessageOut($arFields['FILES']); if (strlen($messageFiles) > 0) { $arParams['MESSAGE_OUT'] = strlen($arParams['MESSAGE_OUT']) > 0 ? $arParams['MESSAGE_OUT'] . "\n" . $messageFiles : $messageFiles; $arFields['MESSAGE_OUT'] = $arParams['MESSAGE_OUT']; } $result = IM\MessageTable::add($arParams); $messageID = IntVal($result->getId()); if ($messageID <= 0) { return false; } IM\ChatTable::update($chatId, array('LAST_MESSAGE_ID' => $messageID)); if (!empty($arFields['PARAMS'])) { CIMMessageParam::Set($messageID, $arFields['PARAMS']); } //$sqlCounter = "SELECT USER_ID as ID, 1 as CNT, '**' as SITE_ID, 'im_chat_v2' as CODE, 1 as SENT // FROM b_im_relation R1 // WHERE CHAT_ID = ".$chatId." AND USER_ID <> ".$arFields['FROM_USER_ID']; //CUserCounter::IncrementWithSelect($sqlCounter, false); $arRel = CIMChat::GetRelationById($chatId); foreach ($arRel as $relation) { CIMContactList::SetRecent(array('ENTITY_ID' => $chatId, 'MESSAGE_ID' => $messageID, 'CHAT_TYPE' => $arFields['MESSAGE_TYPE'], 'USER_ID' => $relation['USER_ID'])); if ($relation["USER_ID"] == $arFields["FROM_USER_ID"]) { IM\RelationTable::update($relation["ID"], array("STATUS" => IM_STATUS_READ, "LAST_ID" => $messageID, "LAST_SEND_ID" => $messageID, "LAST_READ" => new Bitrix\Main\Type\DateTime())); } else { if ($relation['STATUS'] != IM_STATUS_UNREAD) { IM\RelationTable::update($relation["ID"], array("STATUS" => IM_STATUS_UNREAD)); } } } CIMStatus::SetIdle($arFields['FROM_USER_ID'], false); if (CModule::IncludeModule("pull")) { $arParams['FROM_USER_ID'] = $arFields['FROM_USER_ID']; $arParams['TO_CHAT_ID'] = $arFields['TO_CHAT_ID']; $arPullTo = array('module_id' => 'im', 'command' => 'messageChat', 'params' => CIMMessage::GetFormatMessage(array('ID' => $messageID, 'CHAT_ID' => $chatId, 'TO_CHAT_ID' => $arParams['TO_CHAT_ID'], 'FROM_USER_ID' => $arParams['FROM_USER_ID'], 'MESSAGE' => $arParams['MESSAGE'], 'SYSTEM' => $arFields['SYSTEM'] == 'Y' ? 'Y' : 'N', 'DATE_CREATE' => time(), 'PARAMS' => $arFields['PARAMS'], 'FILES' => $arFields['FILES']))); $pushText = ''; if ($arFields['MESSAGE_TYPE'] == IM_MESSAGE_CHAT && CPullOptions::GetPushStatus() && (!isset($arFields['PUSH']) || $arFields['PUSH'] == 'Y')) { if ($arFields['SYSTEM'] == 'Y') { $pushText = substr(htmlspecialcharsback($chatTitle), 0, 32) . ': ' . $arParams['MESSAGE']; } else { $arSelect = array("ID", "LAST_NAME", "NAME", "LOGIN", "SECOND_NAME"); $dbUsers = CUser::GetList($sort_by = false, $dummy = '', array('ID' => $arParams['FROM_USER_ID']), array('FIELDS' => $arSelect)); if ($arUser = $dbUsers->GetNext(true, false)) { $sName = CUser::FormatName(CSite::GetNameFormat(false), $arUser, true, false); $pushText = GetMessage('IM_PUSH_GROUP_TITLE', array('#USER#' => $sName, '#GROUP#' => substr(htmlspecialcharsback($chatTitle), 0, 32))) . ': ' . $arParams['MESSAGE']; } } if (count($arFields['FILES']) > 0 && strlen($pushText) < 200 && strlen($pushText) > 0) { foreach ($arFields['FILES'] as $file) { $file = " [" . GetMessage('IM_MESSAGE_FILE') . ": " . $file['name'] . "]"; if (strlen($pushText . $file) > 200) { break; } $pushText = trim($pushText) . $file; } } $arPullTo['push_params'] = array('TAG' => 'IM_CHAT_' . $chatId, 'CATEGORY' => 'ANSWER', 'URL' => SITE_DIR . 'mobile/ajax.php?mobile_action=im_answer', 'PARAMS' => array('RECIPIENT_ID' => 'chat' . $chatId)); $arPullTo['push_tag'] = 'IM_CHAT_' . $chatId; $arPullTo['push_sub_tag'] = 'IM_MESS'; $arPullTo['push_app_id'] = 'Bitrix24'; $arPullTo['push_text'] = preg_replace("/\\[s\\].*?\\[\\/s\\]/i", "-", $pushText); $arPullTo['push_text'] = preg_replace("/\\[[bui]\\](.*?)\\[\\/[bui]\\]/i", "\$1", $arPullTo['push_text']); $arPullTo['push_text'] = preg_replace("/\\[USER=([0-9]{1,})\\](.*?)\\[\\/USER\\]/i", "\$2", $arPullTo['push_text']); $arPullTo['push_text'] = preg_replace("/\\[PCH=([0-9]{1,})\\](.*?)\\[\\/PCH\\]/i", "\$2", $arPullTo['push_text']); $arPullTo['push_text'] = preg_replace('#\\-{54}.+?\\-{54}#s', " [" . GetMessage('IM_QUOTE') . "] ", str_replace(array("#BR#"), array(" "), $arPullTo['push_text'])); } $arPullFrom = $arPullTo; unset($arPullFrom['push_text']); foreach ($arRel as $rel) { if ($rel['USER_ID'] == $arParams['FROM_USER_ID']) { CPullStack::AddByUser($arParams['FROM_USER_ID'], $arPullFrom); CPushManager::DeleteFromQueueBySubTag($arParams['FROM_USER_ID'], 'IM_MESS'); } else { $arPullUser = $arPullTo; if ($rel['NOTIFY_BLOCK'] == 'Y' || !CIMSettings::GetNotifyAccess($rel['USER_ID'], 'im', 'chat', CIMSettings::CLIENT_PUSH)) { unset($arPullUser['push_text']); } CPullStack::AddByUser($rel['USER_ID'], $arPullUser); //$usersForBadges[] = $rel['USER_ID']; } } if ($arRes['CHAT_TYPE'] == IM_MESSAGE_OPEN) { CPullWatch::AddToStack('IM_PUBLIC_' . $chatId, $arPullFrom); $forUsers = array(); if (preg_match_all("/\\[USER=([0-9]{1,})\\](.*?)\\[\\/USER\\]/i", $arParams['MESSAGE'], $matches)) { foreach ($matches[1] as $userId) { if (!isset($arRel[$userId])) { $forUsers[$userId] = $userId; } } } if (count($forUsers)) { $arSelect = array("ID", "LAST_NAME", "NAME", "LOGIN", "SECOND_NAME"); $dbUsers = CUser::GetList($sort_by = false, $dummy = '', array('ID' => $arParams['FROM_USER_ID']), array('FIELDS' => $arSelect)); if ($arUser = $dbUsers->GetNext(true, false)) { $sName = CUser::FormatName(CSite::GetNameFormat(false), $arUser, true, false); $notifyMail = GetMessage('IM_MESSAGE_MENTION', array('#TITLE#' => substr(htmlspecialcharsback($chatTitle), 0, 32))); $notifyText = GetMessage('IM_MESSAGE_MENTION', array('#TITLE#' => '[CHAT=' . $chatId . ']' . substr(htmlspecialcharsback($chatTitle), 0, 32) . '[/CHAT]')); $pushText = GetMessage('IM_MESSAGE_MENTION_PUSH', array('#USER#' => $sName, '#TITLE#' => substr(htmlspecialcharsback($chatTitle), 0, 32))); } if (strlen($notifyText) > 0) { $arMessageFields = array("TO_USER_ID" => $userId, "FROM_USER_ID" => $arParams['FROM_USER_ID'], "NOTIFY_TYPE" => IM_NOTIFY_FROM, "NOTIFY_MODULE" => "im", "NOTIFY_EVENT" => "mention", "NOTIFY_TAG" => 'IM|MENTION|' . $chatId, "NOTIFY_SUB_TAG" => "IM_MESS", "NOTIFY_MESSAGE" => $notifyText, "NOTIFY_MESSAGE_OUT" => $notifyMail, "PUSH_MESSAGE" => $pushText, "PUSH_PARAMS" => array('TAG' => 'IM_CHAT_' . $chatId, 'CATEGORY' => 'ANSWER', 'URL' => SITE_DIR . 'mobile/ajax.php?mobile_action=im_answer', 'PARAMS' => array('RECIPIENT_ID' => 'chat' . $chatId)), "PUSH_APP_ID" => 'Bitirx24'); CIMNotify::Add($arMessageFields); } } } //self::SendBadges($usersForBadges); } foreach (GetModuleEvents("im", "OnAfterMessagesAdd", true) as $arEvent) { ExecuteModuleEventEx($arEvent, array(intval($messageID), $arFields)); } return $messageID; } else { $GLOBALS["APPLICATION"]->ThrowException(GetMessage("IM_ERROR_MESSAGE_CREATE"), "CHAT_ID"); return false; } } else { if ($arFields['MESSAGE_TYPE'] == IM_MESSAGE_SYSTEM) { $arFields['TO_USER_ID'] = intval($arFields['TO_USER_ID']); $chatId = 0; $strSql = "\n\t\t\t\tSELECT CHAT_ID\n\t\t\t\tFROM b_im_relation\n\t\t\t\tWHERE USER_ID = " . $arFields['TO_USER_ID'] . " AND MESSAGE_TYPE = '" . IM_MESSAGE_SYSTEM . "'"; $dbRes = $DB->Query($strSql, false, "File: " . __FILE__ . "<br>Line: " . __LINE__); if ($arRes = $dbRes->Fetch()) { $chatId = intval($arRes['CHAT_ID']); } else { $result = IM\ChatTable::add(array('TYPE' => IM_MESSAGE_SYSTEM, 'AUTHOR_ID' => $arFields['TO_USER_ID'])); $chatId = $result->getId(); if ($chatId <= 0) { $GLOBALS["APPLICATION"]->ThrowException(GetMessage("IM_ERROR_MESSAGE_CREATE"), "CHAT_ID"); return false; } IM\RelationTable::add(array("CHAT_ID" => $chatId, "MESSAGE_TYPE" => IM_MESSAGE_SYSTEM, "USER_ID" => intval($arFields['TO_USER_ID']), "STATUS" => $bConvert ? 2 : 0)); } if ($chatId > 0) { $arParams = array(); $arParams['CHAT_ID'] = $chatId; $arParams['AUTHOR_ID'] = intval($arFields['AUTHOR_ID']); $arParams['MESSAGE'] = trim($arFields['MESSAGE']); $arParams['MESSAGE_OUT'] = trim($arFields['MESSAGE_OUT']); $arParams['NOTIFY_TYPE'] = intval($arFields['NOTIFY_TYPE']); $arParams['NOTIFY_MODULE'] = $arFields['NOTIFY_MODULE']; $arParams['NOTIFY_EVENT'] = $arFields['NOTIFY_EVENT']; //if (strlen($arParams['MESSAGE']) <= 0 && strlen($arParams['MESSAGE_OUT']) <= 0) // return false; $sendToSite = true; if ($arParams['NOTIFY_TYPE'] != IM_NOTIFY_CONFIRM) { $sendToSite = CIMSettings::GetNotifyAccess($arFields["TO_USER_ID"], $arFields["NOTIFY_MODULE"], $arFields["NOTIFY_EVENT"], CIMSettings::CLIENT_SITE); } if (!$sendToSite) { $arParams['NOTIFY_READ'] = 'Y'; } if (isset($arFields['IMPORT_ID'])) { $arParams['IMPORT_ID'] = intval($arFields['IMPORT_ID']); } if (isset($arFields['MESSAGE_DATE'])) { $arParams['DATE_CREATE'] = $arFields['MESSAGE_DATE']; } if (isset($arFields['EMAIL_TEMPLATE']) && strlen(trim($arFields['EMAIL_TEMPLATE'])) > 0) { $arParams['EMAIL_TEMPLATE'] = trim($arFields['EMAIL_TEMPLATE']); } $arParams['NOTIFY_TAG'] = isset($arFields['NOTIFY_TAG']) ? $arFields['NOTIFY_TAG'] : ''; $arParams['NOTIFY_SUB_TAG'] = isset($arFields['NOTIFY_SUB_TAG']) ? $arFields['NOTIFY_SUB_TAG'] : ''; if (isset($arFields['NOTIFY_TITLE']) && strlen(trim($arFields['NOTIFY_TITLE'])) > 0) { $arParams['NOTIFY_TITLE'] = trim($arFields['NOTIFY_TITLE']); } if ($arParams['NOTIFY_TYPE'] == IM_NOTIFY_CONFIRM) { if (isset($arFields['NOTIFY_BUTTONS'])) { foreach ($arFields['NOTIFY_BUTTONS'] as $key => $arButtons) { if (is_array($arButtons)) { if (isset($arButtons['TITLE']) && strlen($arButtons['TITLE']) > 0 && isset($arButtons['VALUE']) && strlen($arButtons['VALUE']) > 0 && isset($arButtons['TYPE']) && strlen($arButtons['TYPE']) > 0) { $arButtons['TITLE'] = htmlspecialcharsbx($arButtons['TITLE']); $arButtons['VALUE'] = htmlspecialcharsbx($arButtons['VALUE']); $arButtons['TYPE'] = htmlspecialcharsbx($arButtons['TYPE']); $arFields['NOTIFY_BUTTONS'][$key] = $arButtons; } else { unset($arFields['NOTIFY_BUTTONS'][$key]); } } else { unset($arFields['NOTIFY_BUTTONS'][$key]); } } } else { $arFields['NOTIFY_BUTTONS'] = array(array('TITLE' => GetMessage('IM_ERROR_BUTTON_ACCEPT'), 'VALUE' => 'Y', 'TYPE' => 'accept'), array('TITLE' => GetMessage('IM_ERROR_BUTTON_CANCEL'), 'VALUE' => 'N', 'TYPE' => 'cancel')); } $arParams['NOTIFY_BUTTONS'] = serialize($arFields["NOTIFY_BUTTONS"]); if (isset($arParams['NOTIFY_TAG']) && strlen($arParams['NOTIFY_TAG']) > 0) { CIMNotify::DeleteByTag($arParams['NOTIFY_TAG']); } } if (!$bConvert) { if (isset($arFields['PUSH_MESSAGE']) && CIMSettings::GetNotifyAccess($arFields["TO_USER_ID"], $arFields['NOTIFY_MODULE'], $arFields['NOTIFY_EVENT'], CIMSettings::CLIENT_PUSH) && CModule::IncludeModule('pull')) { $CPushManager = new CPushManager(); $CPushManager->AddQueue(array('USER_ID' => $arFields['TO_USER_ID'], 'MESSAGE' => str_replace("\n", " ", trim($arFields['PUSH_MESSAGE'])), 'PARAMS' => isset($arFields['PUSH_PARAMS']) ? $arFields['PUSH_PARAMS'] : '', 'TAG' => $arParams['NOTIFY_TAG'], 'SUB_TAG' => $arParams['NOTIFY_SUB_TAG'], 'APP_ID' => isset($arParams['PUSH_APP_ID']) ? $arParams['PUSH_APP_ID'] : '')); } foreach (GetModuleEvents("im", "OnAfterNotifyAdd", true) as $arEvent) { ExecuteModuleEventEx($arEvent, array(intval($messageID), $arFields)); } } if (!$sendToSite) { return 0; } $result = IM\MessageTable::add($arParams); $messageID = IntVal($result->getId()); if ($messageID <= 0) { return false; } IM\ChatTable::update($chatId, array('LAST_MESSAGE_ID' => $messageID)); CIMMessenger::SpeedFileDelete($arFields['TO_USER_ID'], IM_SPEED_NOTIFY); if (!$bConvert) { //CUserCounter::Increment($arFields['TO_USER_ID'], 'im_notify_v2', '**', false); $strSql = "UPDATE b_im_relation SET STATUS = '" . IM_STATUS_UNREAD . "' WHERE USER_ID = " . intval($arFields['TO_USER_ID']) . " AND MESSAGE_TYPE = '" . IM_MESSAGE_SYSTEM . "' AND CHAT_ID = " . $chatId; $DB->Query($strSql, false, "File: " . __FILE__ . "<br>Line: " . __LINE__); if (CModule::IncludeModule("pull")) { CPullStack::AddByUser(intval($arFields['TO_USER_ID']), array('module_id' => 'im', 'command' => 'notify', 'params' => CIMNotify::GetFormatNotify(array('ID' => $messageID, 'DATE_CREATE' => time(), 'FROM_USER_ID' => intval($arFields['FROM_USER_ID']), 'MESSAGE' => $arParams['MESSAGE'], 'NOTIFY_MODULE' => $arParams['NOTIFY_MODULE'], 'NOTIFY_EVENT' => $arParams['NOTIFY_EVENT'], 'NOTIFY_TAG' => $arParams['NOTIFY_TAG'], 'NOTIFY_TYPE' => $arParams['NOTIFY_TYPE'], 'NOTIFY_BUTTONS' => isset($arParams['NOTIFY_BUTTONS']) ? $arParams['NOTIFY_BUTTONS'] : serialize(array()), 'NOTIFY_TITLE' => isset($arParams['NOTIFY_TITLE']) ? $arParams['NOTIFY_TITLE'] : '')))); self::SendBadges($arFields['TO_USER_ID']); } } return $messageID; } else { $GLOBALS["APPLICATION"]->ThrowException(GetMessage("IM_ERROR_MESSAGE_CREATE"), "CHAT_ID"); return false; } } else { $GLOBALS["APPLICATION"]->ThrowException(GetMessage("IM_ERROR_MESSAGE_TYPE"), "MESSAGE_TYPE"); return false; } } } }
public static function DeleteBySubTag($notifySubTag, $authorId = false) { global $DB; if (strlen($notifySubTag) <= 0) { return false; } $sqlUser = ""; $sqlUser2 = ""; if ($authorId !== false) { $sqlUser = "******" . intval($authorId); $sqlUser2 = " AND AUTHOR_ID = " . intval($authorId); } $dbRes = $DB->Query("SELECT M.ID, M.NOTIFY_TYPE, R.USER_ID, R.STATUS FROM b_im_relation R, b_im_message M WHERE M.CHAT_ID = R.CHAT_ID AND M.NOTIFY_SUB_TAG = '" . $DB->ForSQL($notifySubTag) . "'" . $sqlUser, false, "File: " . __FILE__ . "<br>Line: " . __LINE__); $arUsers = array(); $messages = array(); while ($row = $dbRes->Fetch()) { $messages[$row['ID']] = $row['NOTIFY_TYPE']; $count = $row['STATUS'] < IM_STATUS_READ ? 1 : 0; if (isset($arUsers[$row['USER_ID']])) { $arUsers[$row['USER_ID']] += $count; } else { $arUsers[$row['USER_ID']] = $count; } } $pullActive = false; if (CModule::IncludeModule("pull")) { $pullActive = true; } $arUsersSend = array(); foreach ($arUsers as $userId => $count) { CIMMessenger::SpeedFileDelete($userId, IM_SPEED_NOTIFY); if ($count > 0) { $arUsersSend[] = $userId; //CUserCounter::Decrement($userId, 'im_notify_v2', '**', false); } if ($pullActive) { CPushManager::DeleteFromQueueBySubTag($userId, $notifySubTag); } } if ($pullActive) { CPullStack::AddByUsers(array_keys($arUsers), array('module_id' => 'im', 'command' => 'massDeleteMessage', 'params' => array('MESSAGE' => $messages))); } if (count($messages) > 0) { $messageParameters = IM\MessageParamTable::getList(array('select' => array('ID'), 'filter' => array('=MESSAGE_ID' => array_keys($messages)))); while ($ar = $messageParameters->fetch()) { IM\MessageParamTable::delete($ar['ID']); } $strSql = "DELETE FROM b_im_message WHERE NOTIFY_SUB_TAG = '" . $DB->ForSQL($notifySubTag) . "'" . $sqlUser2; $DB->Query($strSql, false, "File: " . __FILE__ . "<br>Line: " . __LINE__); } CIMMessenger::SendBadges($arUsersSend); return true; }
public static function SetRecent($arParams) { global $USER; $itemId = intval($arParams['ENTITY_ID']); $messageId = intval($arParams['MESSAGE_ID']); if ($itemId <= 0) { return false; } $userId = intval($arParams['USER_ID']); if ($userId <= 0) { $userId = (int) $USER->GetID(); } $chatType = IM_MESSAGE_PRIVATE; if (isset($arParams['CHAT_TYPE']) && in_array($arParams['CHAT_TYPE'], array(IM_MESSAGE_OPEN, IM_MESSAGE_CHAT))) { $chatType = $arParams['CHAT_TYPE']; } else { if (isset($arParams['CHAT_ID'])) { $orm = IM\ChatTable::getById($arParams['CHAT_ID']); if ($chatData = $orm->fetch()) { $chatType = $chatData['TYPE']; } } } $isChat = in_array($chatType, array(IM_MESSAGE_OPEN, IM_MESSAGE_CHAT)); if (!$isChat && $userId == $itemId) { return false; } $connection = \Bitrix\Main\Application::getInstance()->getConnection(); $isUserAlreadyInRecent = $connection->queryScalar("SELECT 1 FROM b_im_recent WHERE USER_ID = " . $userId); $merge = $connection->getSqlHelper()->prepareMerge("b_im_recent", array('USER_ID', 'ITEM_TYPE', 'ITEM_ID'), array('USER_ID' => $userId, 'ITEM_TYPE' => $chatType, 'ITEM_ID' => $itemId, 'ITEM_MID' => $messageId), array('ITEM_MID' => $messageId)); if ($merge && $merge[0] != "") { $connection->query($merge[0]); } $obCache = new CPHPCache(); $obCache->CleanDir('/bx/imc/recent' . CIMMessenger::GetCachePath($userId)); if ($isChat) { CIMMessenger::SpeedFileDelete($userId, IM_SPEED_GROUP); } else { CIMMessenger::SpeedFileDelete($userId, IM_SPEED_MESSAGE); } if (!$isUserAlreadyInRecent) { $event = new \Bitrix\Main\Event("im", "OnAfterRecentAdd", array("user_id" => $userId)); $event->send(); } return true; }
public static function Add($arFields) { global $DB; if (isset($arFields['TITLE']) && !isset($arFields['NOTIFY_TITLE'])) { $arFields['NOTIFY_TITLE'] = $arFields['TITLE']; } if (isset($arFields['NOTIFY_MESSAGE']) && !isset($arFields['MESSAGE'])) { $arFields['MESSAGE'] = $arFields['NOTIFY_MESSAGE']; } if (isset($arFields['NOTIFY_MESSAGE_OUT']) && !isset($arFields['MESSAGE_OUT'])) { $arFields['MESSAGE_OUT'] = $arFields['NOTIFY_MESSAGE_OUT']; } if (isset($arFields['MESSAGE'])) { $arFields['MESSAGE'] = trim($arFields['MESSAGE']); } $arFields['MESSAGE_OUT'] = isset($arFields['MESSAGE_OUT']) ? trim($arFields['MESSAGE_OUT']) : ""; $bConvert = false; if (isset($arFields['CONVERT']) && $arFields['CONVERT'] == 'Y') { $bConvert = true; } if (!isset($arFields['MESSAGE_TYPE'])) { $arFields['MESSAGE_TYPE'] = ""; } if (!isset($arFields['NOTIFY_MODULE'])) { $arFields['NOTIFY_MODULE'] = 'im'; } if (!isset($arFields['NOTIFY_EVENT'])) { $arFields['NOTIFY_EVENT'] = 'default'; } if (!isset($arFields['PARAMS'])) { $arFields['PARAMS'] = array(); } if (isset($arFields['ATTACH']) || isset($arFields['PARAMS']['ATTACH'])) { $attach = isset($arFields['ATTACH']) ? $arFields['ATTACH'] : $arFields['PARAMS']['ATTACH']; if (is_object($attach)) { $arFields['PARAMS']['ATTACH'] = array($attach); } else { if (is_array($attach)) { $arFields['PARAMS']['ATTACH'] = $attach; } else { $arFields['PARAMS']['ATTACH'] = array(); } } } if (isset($arFields['FOR_USER_ID'])) { $arFields['PARAMS']['FOR_USER_ID'] = $arFields['FOR_USER_ID']; } $arFields['URL_ATTACH'] = array(); if ($arFields['MESSAGE_TYPE'] == IM_MESSAGE_SYSTEM) { if (!isset($arFields['NOTIFY_TYPE']) && intval($arFields['FROM_USER_ID']) > 0) { $arFields['NOTIFY_TYPE'] = IM_NOTIFY_FROM; } else { if (!isset($arFields['NOTIFY_TYPE'])) { $arFields['NOTIFY_TYPE'] = IM_NOTIFY_SYSTEM; } } if (isset($arFields['NOTIFY_ANSWER']) && $arFields['NOTIFY_ANSWER'] == 'Y') { $arFields['PARAMS']['CAN_ANSWER'] = 'Y'; } /* $urlPrepare = self::PrepareUrl($arFields['MESSAGE']); if ($urlPrepare['RESULT']) { if (empty($arFields['MESSAGE_OUT'])) { $arFields['MESSAGE_OUT'] = $arFields['MESSAGE']; } $arFields['MESSAGE'] = $urlPrepare['MESSAGE']; $arFields['PARAMS']['ATTACH'] = array_merge($arFields['PARAMS']['ATTACH'], $urlPrepare['ATTACH']); } */ } else { $link = new CIMMessageLink(); $urlPrepare = $link->prepareInsert($arFields['MESSAGE']); if ($urlPrepare['RESULT']) { if (empty($arFields['MESSAGE_OUT'])) { $arFields['MESSAGE_OUT'] = $arFields['MESSAGE']; } $arFields['MESSAGE'] = $urlPrepare['MESSAGE']; if (isset($arFields['PARAMS']['URL_ID'])) { $arFields['PARAMS']['URL_ID'] = array_merge($arFields['PARAMS']['URL_ID'], $urlPrepare['URL_ID']); } else { $arFields['PARAMS']['URL_ID'] = $urlPrepare['URL_ID']; } $arFields['URL_ATTACH'] = $urlPrepare['ATTACH']; } } if (isset($arFields['NOTIFY_EMAIL_TEMPLATE']) && !isset($arFields['EMAIL_TEMPLATE'])) { $arFields['EMAIL_TEMPLATE'] = $arFields['NOTIFY_EMAIL_TEMPLATE']; } if (!isset($arFields['AUTHOR_ID'])) { $arFields['AUTHOR_ID'] = intval($arFields['FROM_USER_ID']); } foreach (GetModuleEvents("im", "OnBeforeMessageNotifyAdd", true) as $arEvent) { $result = ExecuteModuleEventEx($arEvent, array(&$arFields)); if ($result === false || isset($result['result']) && $result['result'] === false) { if (isset($result['reason'])) { $CBXSanitizer = new CBXSanitizer(); $CBXSanitizer->AddTags(array('a' => array('href', 'style', 'target'), 'b' => array(), 'u' => array(), 'i' => array(), 'br' => array(), 'span' => array('style'))); $reason = $CBXSanitizer->SanitizeHtml($result['reason']); } else { if ($arFields['MESSAGE_TYPE'] == IM_MESSAGE_PRIVATE) { $reason = GetMessage("IM_ERROR_MESSAGE_CANCELED"); } else { if ($arFields['MESSAGE_TYPE'] == IM_MESSAGE_SYSTEM) { $reason = GetMessage("IM_ERROR_NOTIFY_CANCELED"); } else { $reason = GetMessage("IM_ERROR_GROUP_CANCELED"); } } } $GLOBALS["APPLICATION"]->ThrowException($reason, "ERROR_FROM_OTHER_MODULE"); return false; } } if (!self::CheckFields($arFields)) { return false; } if ($arFields['MESSAGE_TYPE'] == IM_MESSAGE_PRIVATE) { if (isset($arFields['TO_CHAT_ID'])) { $chatId = $arFields['TO_CHAT_ID']; $arRel = CIMChat::GetRelationById($chatId); foreach ($arRel as $rel) { if ($rel['USER_ID'] == $arFields['FROM_USER_ID']) { continue; } $arFields['TO_USER_ID'] = $rel['USER_ID']; } if (!IsModuleInstalled('intranet')) { if (CIMSettings::GetPrivacy(CIMSettings::PRIVACY_MESSAGE) == CIMSettings::PRIVACY_RESULT_CONTACT && CModule::IncludeModule('socialnetwork') && CSocNetUser::IsFriendsAllowed() && !CSocNetUserRelations::IsFriends($arFields['FROM_USER_ID'], $arFields['TO_USER_ID'])) { $GLOBALS["APPLICATION"]->ThrowException(GetMessage('IM_ERROR_MESSAGE_PRIVACY_SELF'), "ERROR_FROM_PRIVACY_SELF"); return false; } else { if (CIMSettings::GetPrivacy(CIMSettings::PRIVACY_MESSAGE, $arFields['TO_USER_ID']) == CIMSettings::PRIVACY_RESULT_CONTACT && CModule::IncludeModule('socialnetwork') && CSocNetUser::IsFriendsAllowed() && !CSocNetUserRelations::IsFriends($arFields['FROM_USER_ID'], $arFields['TO_USER_ID'])) { $GLOBALS["APPLICATION"]->ThrowException(GetMessage('IM_ERROR_MESSAGE_PRIVACY'), "ERROR_FROM_PRIVACY"); return false; } } } } else { $arFields['FROM_USER_ID'] = intval($arFields['FROM_USER_ID']); $arFields['TO_USER_ID'] = intval($arFields['TO_USER_ID']); if (!IsModuleInstalled('intranet')) { if (CIMSettings::GetPrivacy(CIMSettings::PRIVACY_MESSAGE) == CIMSettings::PRIVACY_RESULT_CONTACT && CModule::IncludeModule('socialnetwork') && CSocNetUser::IsFriendsAllowed() && !CSocNetUserRelations::IsFriends($arFields['FROM_USER_ID'], $arFields['TO_USER_ID'])) { $GLOBALS["APPLICATION"]->ThrowException(GetMessage('IM_ERROR_MESSAGE_PRIVACY_SELF'), "ERROR_FROM_PRIVACY_SELF"); return false; } else { if (CIMSettings::GetPrivacy(CIMSettings::PRIVACY_MESSAGE, $arFields['TO_USER_ID']) == CIMSettings::PRIVACY_RESULT_CONTACT && CModule::IncludeModule('socialnetwork') && CSocNetUser::IsFriendsAllowed() && !CSocNetUserRelations::IsFriends($arFields['FROM_USER_ID'], $arFields['TO_USER_ID'])) { $GLOBALS["APPLICATION"]->ThrowException(GetMessage('IM_ERROR_MESSAGE_PRIVACY'), "ERROR_FROM_PRIVACY"); return false; } } } $chatId = CIMMessage::GetChatId($arFields['FROM_USER_ID'], $arFields['TO_USER_ID']); } if ($chatId > 0) { $arParams = array(); $arParams['CHAT_ID'] = $chatId; $arParams['AUTHOR_ID'] = intval($arFields['AUTHOR_ID']); $arParams['MESSAGE'] = $arFields['MESSAGE']; $arParams['MESSAGE_OUT'] = $arFields['MESSAGE_OUT']; $arParams['NOTIFY_MODULE'] = $arFields['NOTIFY_MODULE']; $arParams['NOTIFY_EVENT'] = $arFields['SYSTEM'] == 'Y' ? 'private_system' : 'private'; if (isset($arFields['IMPORT_ID'])) { $arParams['IMPORT_ID'] = intval($arFields['IMPORT_ID']); } if (isset($arFields['MESSAGE_DATE'])) { $arParams['DATE_CREATE'] = $arFields['MESSAGE_DATE']; } $arFiles = array(); $arFields['FILES'] = array(); if (isset($arFields['PARAMS']['FILE_ID'])) { foreach ($arFields['PARAMS']['FILE_ID'] as $fileId) { $arFiles[$fileId] = $fileId; } } $arFields['FILES'] = CIMDisk::GetFiles($chatId, $arFiles); $messageFiles = self::GetFormatFilesMessageOut($arFields['FILES']); if (strlen($messageFiles) > 0) { $arParams['MESSAGE_OUT'] = strlen($arParams['MESSAGE_OUT']) > 0 ? $arParams['MESSAGE_OUT'] . "\n" . $messageFiles : $messageFiles; $arFields['MESSAGE_OUT'] = $arParams['MESSAGE_OUT']; } $result = IM\MessageTable::add($arParams); $messageID = IntVal($result->getId()); if ($messageID <= 0) { return false; } IM\ChatTable::update($chatId, array('LAST_MESSAGE_ID' => $messageID)); if (!empty($arFields['PARAMS'])) { CIMMessageParam::Set($messageID, $arFields['PARAMS']); } if (!empty($arFields['URL_ATTACH'])) { if (isset($arFields['PARAMS']['ATTACH'])) { $arFields['PARAMS']['ATTACH'] = array_merge($arFields['PARAMS']['ATTACH'], $arFields['URL_ATTACH']); } else { $arFields['PARAMS']['ATTACH'] = $arFields['URL_ATTACH']; } } //CUserCounter::Increment($arFields['TO_USER_ID'], 'im_message_v2', '**', false); CIMContactList::SetRecent(array('ENTITY_ID' => $arFields['TO_USER_ID'], 'MESSAGE_ID' => $messageID, 'CHAT_TYPE' => IM_MESSAGE_PRIVATE, 'USER_ID' => $arFields['FROM_USER_ID'])); CIMContactList::SetRecent(array('ENTITY_ID' => $arFields['FROM_USER_ID'], 'MESSAGE_ID' => $messageID, 'CHAT_TYPE' => IM_MESSAGE_PRIVATE, 'USER_ID' => $arFields['TO_USER_ID'])); CIMStatus::SetIdle($arFields['FROM_USER_ID'], false); if (!$bConvert) { $arRel = CIMChat::GetRelationById($chatId); foreach ($arRel as $relation) { if ($relation["USER_ID"] == $arFields["TO_USER_ID"]) { if ($relation['STATUS'] != IM_STATUS_UNREAD) { IM\RelationTable::update($relation["ID"], array("STATUS" => IM_STATUS_UNREAD)); } } else { IM\RelationTable::update($relation["ID"], array("STATUS" => IM_STATUS_READ, "LAST_ID" => $messageID, "LAST_SEND_ID" => $messageID, "LAST_READ" => new Bitrix\Main\Type\DateTime())); } } if (CModule::IncludeModule("pull")) { $arParams['FROM_USER_ID'] = $arFields['FROM_USER_ID']; $arParams['TO_USER_ID'] = $arFields['TO_USER_ID']; $pullMessage = array('module_id' => 'im', 'command' => 'message', 'params' => CIMMessage::GetFormatMessage(array('ID' => $messageID, 'CHAT_ID' => $chatId, 'TO_USER_ID' => $arParams['TO_USER_ID'], 'FROM_USER_ID' => $arParams['FROM_USER_ID'], 'SYSTEM' => $arFields['SYSTEM'] == 'Y' ? 'Y' : 'N', 'MESSAGE' => $arParams['MESSAGE'], 'DATE_CREATE' => time(), 'PARAMS' => self::PrepareParamsForPull($arFields['PARAMS']), 'FILES' => $arFields['FILES']))); $pullMessageTo = $pullMessage; if (CPullOptions::GetPushStatus() && (!isset($arFields['PUSH']) || $arFields['PUSH'] == 'Y')) { if (CIMSettings::GetNotifyAccess($arParams["TO_USER_ID"], 'im', 'message', CIMSettings::CLIENT_PUSH)) { $pushParams = self::PreparePushForPrivate(array('FROM_USER_ID' => $arParams['FROM_USER_ID'], 'MESSAGE' => $arParams['MESSAGE'], 'SYSTEM' => $arFields['SYSTEM'], 'FILES' => $arFields['FILES'], 'ATTACH' => isset($arFields['PARAMS']['ATTACH']) ? true : false)); $pullMessageTo = array_merge($pullMessage, $pushParams); } } CPullStack::AddByUser($arParams['TO_USER_ID'], $pullMessageTo); CPullStack::AddByUser($arParams['FROM_USER_ID'], $pullMessage); CPushManager::DeleteFromQueueBySubTag($arParams['FROM_USER_ID'], 'IM_MESS'); //self::SendBadges($arParams['TO_USER_ID']); } foreach (GetModuleEvents("im", "OnAfterMessagesAdd", true) as $arEvent) { ExecuteModuleEventEx($arEvent, array(intval($messageID), $arFields)); } } return $messageID; } else { $GLOBALS["APPLICATION"]->ThrowException(GetMessage("IM_ERROR_MESSAGE_CREATE"), "CHAT_ID"); return false; } } else { if ($arFields['MESSAGE_TYPE'] == IM_MESSAGE_CHAT || $arFields['MESSAGE_TYPE'] == IM_MESSAGE_OPEN) { $arFields['FROM_USER_ID'] = intval($arFields['FROM_USER_ID']); $chatId = 0; $systemMessage = false; if (isset($arFields['SYSTEM']) && $arFields['SYSTEM'] == 'Y') { $strSql = "\n\t\t\t\t\tSELECT C.ID CHAT_ID, C.TITLE CHAT_TITLE, C.AUTHOR_ID CHAT_AUTHOR_ID, C.TYPE CHAT_TYPE, '1' RID\n\t\t\t\t\tFROM b_im_chat C\n\t\t\t\t\tWHERE C.ID = " . intval($arFields['TO_CHAT_ID']) . "\n\t\t\t\t"; $systemMessage = true; } else { $strSql = "\n\t\t\t\t\tSELECT C.ID CHAT_ID, C.TITLE CHAT_TITLE, C.AUTHOR_ID CHAT_AUTHOR_ID, C.TYPE CHAT_TYPE, R.USER_ID RID\n\t\t\t\t\tFROM b_im_chat C\n\t\t\t\t\tLEFT JOIN b_im_relation R ON R.CHAT_ID = C.ID AND R.USER_ID = " . $arFields['FROM_USER_ID'] . "\n\t\t\t\t\tWHERE C.ID = " . intval($arFields['TO_CHAT_ID']) . "\n\t\t\t\t"; } $dbRes = $DB->Query($strSql, false, "File: " . __FILE__ . "<br>Line: " . __LINE__); if ($arRes = $dbRes->Fetch()) { $chatId = intval($arRes['CHAT_ID']); $chatTitle = htmlspecialcharsbx($arRes['CHAT_TITLE']); $chatAuthorId = intval($arRes['CHAT_AUTHOR_ID']); $arRes['CHAT_TYPE'] = trim($arRes['CHAT_TYPE']); $arFields['MESSAGE_TYPE'] = $arRes['CHAT_TYPE']; if ($arRes['CHAT_TYPE'] == IM_MESSAGE_OPEN) { if (!CIMMessenger::CheckEnableOpenChat()) { $GLOBALS["APPLICATION"]->ThrowException(GetMessage("IM_ERROR_GROUP_CANCELED"), "CANCELED"); return false; } else { if (intval($arRes['RID']) <= 0) { if (IM\User::getInstance($arFields['FROM_USER_ID'])->isExtranet()) { $GLOBALS["APPLICATION"]->ThrowException(GetMessage("IM_ERROR_GROUP_CANCELED"), "CANCELED"); return false; } else { $chat = new CIMChat(0); $chat->AddUser($chatId, $arFields['FROM_USER_ID']); } } } } else { if (intval($arRes['RID']) <= 0) { $GLOBALS["APPLICATION"]->ThrowException(GetMessage("IM_ERROR_GROUP_CANCELED"), "CANCELED"); return false; } } } else { $GLOBALS["APPLICATION"]->ThrowException(GetMessage("IM_ERROR_GROUP_CANCELED"), "CANCELED"); return false; } if ($chatId > 0) { $arParams = array(); $arParams['CHAT_ID'] = $chatId; $arParams['AUTHOR_ID'] = $systemMessage ? 0 : intval($arFields['AUTHOR_ID']); $arParams['MESSAGE'] = $arFields['MESSAGE']; $arParams['MESSAGE_OUT'] = $arFields['MESSAGE_OUT']; $arParams['NOTIFY_MODULE'] = 'im'; $arParams['NOTIFY_EVENT'] = 'group'; if (isset($arFields['MESSAGE_DATE'])) { $arParams['DATE_CREATE'] = $arFields['MESSAGE_DATE']; } $arFiles = array(); $arFields['FILES'] = array(); if (isset($arFields['PARAMS']['FILE_ID'])) { foreach ($arFields['PARAMS']['FILE_ID'] as $fileId) { $arFiles[$fileId] = $fileId; } } $arFields['FILES'] = CIMDisk::GetFiles($chatId, $arFiles); $messageFiles = self::GetFormatFilesMessageOut($arFields['FILES']); if (strlen($messageFiles) > 0) { $arParams['MESSAGE_OUT'] = strlen($arParams['MESSAGE_OUT']) > 0 ? $arParams['MESSAGE_OUT'] . "\n" . $messageFiles : $messageFiles; $arFields['MESSAGE_OUT'] = $arParams['MESSAGE_OUT']; } $result = IM\MessageTable::add($arParams); $messageID = IntVal($result->getId()); if ($messageID <= 0) { return false; } IM\ChatTable::update($chatId, array('LAST_MESSAGE_ID' => $messageID)); if (!empty($arFields['PARAMS'])) { CIMMessageParam::Set($messageID, $arFields['PARAMS']); } if (!empty($arFields['URL_ATTACH'])) { if (isset($arFields['PARAMS']['ATTACH'])) { $arFields['PARAMS']['ATTACH'] = array_merge($arFields['PARAMS']['ATTACH'], $arFields['URL_ATTACH']); } else { $arFields['PARAMS']['ATTACH'] = $arFields['URL_ATTACH']; } } //$sqlCounter = "SELECT USER_ID as ID, 1 as CNT, '**' as SITE_ID, 'im_chat_v2' as CODE, 1 as SENT // FROM b_im_relation R1 // WHERE CHAT_ID = ".$chatId." AND USER_ID <> ".$arFields['FROM_USER_ID']; //CUserCounter::IncrementWithSelect($sqlCounter, false); $arRel = CIMChat::GetRelationById($chatId); foreach ($arRel as $relation) { CIMContactList::SetRecent(array('ENTITY_ID' => $chatId, 'MESSAGE_ID' => $messageID, 'CHAT_TYPE' => $arFields['MESSAGE_TYPE'], 'USER_ID' => $relation['USER_ID'])); if ($relation["USER_ID"] == $arFields["FROM_USER_ID"]) { IM\RelationTable::update($relation["ID"], array("STATUS" => IM_STATUS_READ, "LAST_ID" => $messageID, "LAST_SEND_ID" => $messageID, "LAST_READ" => new Bitrix\Main\Type\DateTime())); } else { if ($relation['STATUS'] != IM_STATUS_UNREAD) { IM\RelationTable::update($relation["ID"], array("STATUS" => IM_STATUS_UNREAD)); } } } CIMStatus::SetIdle($arFields['FROM_USER_ID'], false); if (CModule::IncludeModule("pull")) { $arParams['FROM_USER_ID'] = $arFields['FROM_USER_ID']; $arParams['TO_CHAT_ID'] = $arFields['TO_CHAT_ID']; $pullMessage = array('module_id' => 'im', 'command' => 'messageChat', 'params' => CIMMessage::GetFormatMessage(array('ID' => $messageID, 'CHAT_ID' => $chatId, 'TO_CHAT_ID' => $arParams['TO_CHAT_ID'], 'FROM_USER_ID' => $arParams['FROM_USER_ID'], 'MESSAGE' => $arParams['MESSAGE'], 'SYSTEM' => $arFields['SYSTEM'] == 'Y' ? 'Y' : 'N', 'DATE_CREATE' => time(), 'PARAMS' => self::PrepareParamsForPull($arFields['PARAMS']), 'FILES' => $arFields['FILES']))); if (CPullOptions::GetPushStatus() && (!isset($arFields['PUSH']) || $arFields['PUSH'] == 'Y')) { $pushParams = self::PreparePushForChat(array('CHAT_ID' => $chatId, 'CHAT_TITLE' => $chatTitle, 'FROM_USER_ID' => $arParams['FROM_USER_ID'], 'MESSAGE' => $arParams['MESSAGE'], 'SYSTEM' => $arFields['SYSTEM'], 'FILES' => $arFields['FILES'], 'ATTACH' => isset($arFields['PARAMS']['ATTACH']) ? true : false)); $pullMessage = array_merge($pullMessage, $pushParams); } $pullUsers = array(); $pullUsersSkip = array(); foreach ($arRel as $rel) { $pullUsers[] = $rel['USER_ID']; if ($rel['USER_ID'] == $arParams['FROM_USER_ID']) { $pullUsersSkip[] = $rel['USER_ID']; CPushManager::DeleteFromQueueBySubTag($arParams['FROM_USER_ID'], 'IM_MESS'); } else { if ($rel['NOTIFY_BLOCK'] == 'Y' || !CIMSettings::GetNotifyAccess($rel['USER_ID'], 'im', $arFields['MESSAGE_TYPE'] == IM_MESSAGE_OPEN ? 'openChat' : 'chat', CIMSettings::CLIENT_PUSH)) { $pullUsersSkip[] = $rel['USER_ID']; } } } $pullMessage['push']['skip_users'] = $pullUsersSkip; CPullStack::AddByUsers($pullUsers, $pullMessage); if ($arRes['CHAT_TYPE'] == IM_MESSAGE_OPEN) { $pullMessageToWatch = $pullMessage; unset($pullMessageToWatch['push']); CPullWatch::AddToStack('IM_PUBLIC_' . $chatId, $pullMessageToWatch); } self::SendMention(array('CHAT_ID' => $chatId, 'CHAT_TITLE' => $chatTitle, 'CHAT_RELATION' => $arRel, 'CHAT_TYPE' => $arFields['MESSAGE_TYPE'], 'MESSAGE' => $arParams['MESSAGE'], 'FILES' => $arFields['FILES'], 'FROM_USER_ID' => $arParams['FROM_USER_ID'])); //self::SendBadges($usersForBadges); } foreach (GetModuleEvents("im", "OnAfterMessagesAdd", true) as $arEvent) { ExecuteModuleEventEx($arEvent, array(intval($messageID), $arFields)); } return $messageID; } else { $GLOBALS["APPLICATION"]->ThrowException(GetMessage("IM_ERROR_MESSAGE_CREATE"), "CHAT_ID"); return false; } } else { if ($arFields['MESSAGE_TYPE'] == IM_MESSAGE_SYSTEM) { $arFields['TO_USER_ID'] = intval($arFields['TO_USER_ID']); $orm = \Bitrix\Main\UserTable::getById($arFields['TO_USER_ID']); $userData = $orm->fetch(); if (!$userData || $userData['ACTIVE'] == 'N' || $userData['EXTERNAL_AUTH_ID'] == 'email') { $GLOBALS["APPLICATION"]->ThrowException(GetMessage("IM_ERROR_MESSAGE_CREATE"), "TO_USER_ID"); return false; } $chatId = 0; $strSql = "\n\t\t\t\tSELECT C.ID CHAT_ID\n\t\t\t\tFROM b_im_chat C\n\t\t\t\tWHERE C.AUTHOR_ID = " . $arFields['TO_USER_ID'] . " AND C.TYPE = '" . IM_MESSAGE_SYSTEM . "'\n\t\t\t"; $dbRes = $DB->Query($strSql, false, "File: " . __FILE__ . "<br>Line: " . __LINE__); if ($arRes = $dbRes->Fetch()) { $chatId = intval($arRes['CHAT_ID']); } else { $result = IM\ChatTable::add(array('TYPE' => IM_MESSAGE_SYSTEM, 'AUTHOR_ID' => $arFields['TO_USER_ID'])); $chatId = $result->getId(); if ($chatId <= 0) { $GLOBALS["APPLICATION"]->ThrowException(GetMessage("IM_ERROR_MESSAGE_CREATE"), "CHAT_ID"); return false; } IM\RelationTable::add(array("CHAT_ID" => $chatId, "MESSAGE_TYPE" => IM_MESSAGE_SYSTEM, "USER_ID" => intval($arFields['TO_USER_ID']), "STATUS" => $bConvert ? 2 : 0)); } if ($chatId > 0) { $arParams = array(); $arParams['CHAT_ID'] = $chatId; $arParams['AUTHOR_ID'] = intval($arFields['AUTHOR_ID']); $arParams['MESSAGE'] = $arFields['MESSAGE']; $arParams['MESSAGE_OUT'] = $arFields['MESSAGE_OUT']; $arParams['NOTIFY_TYPE'] = intval($arFields['NOTIFY_TYPE']); $arParams['NOTIFY_MODULE'] = $arFields['NOTIFY_MODULE']; $arParams['NOTIFY_EVENT'] = $arFields['NOTIFY_EVENT']; //if (strlen($arParams['MESSAGE']) <= 0 && strlen($arParams['MESSAGE_OUT']) <= 0) // return false; $sendToSite = true; if ($arParams['NOTIFY_TYPE'] != IM_NOTIFY_CONFIRM) { $sendToSite = CIMSettings::GetNotifyAccess($arFields["TO_USER_ID"], $arFields["NOTIFY_MODULE"], $arFields["NOTIFY_EVENT"], CIMSettings::CLIENT_SITE); } if (!$sendToSite) { $arParams['NOTIFY_READ'] = 'Y'; } if (isset($arFields['IMPORT_ID'])) { $arParams['IMPORT_ID'] = intval($arFields['IMPORT_ID']); } if (isset($arFields['MESSAGE_DATE'])) { $arParams['DATE_CREATE'] = $arFields['MESSAGE_DATE']; } if (isset($arFields['EMAIL_TEMPLATE']) && strlen(trim($arFields['EMAIL_TEMPLATE'])) > 0) { $arParams['EMAIL_TEMPLATE'] = trim($arFields['EMAIL_TEMPLATE']); } $arParams['NOTIFY_TAG'] = isset($arFields['NOTIFY_TAG']) ? $arFields['NOTIFY_TAG'] : ''; $arParams['NOTIFY_SUB_TAG'] = isset($arFields['NOTIFY_SUB_TAG']) ? $arFields['NOTIFY_SUB_TAG'] : ''; if (isset($arFields['NOTIFY_TITLE']) && strlen(trim($arFields['NOTIFY_TITLE'])) > 0) { $arParams['NOTIFY_TITLE'] = trim($arFields['NOTIFY_TITLE']); } if ($arParams['NOTIFY_TYPE'] == IM_NOTIFY_CONFIRM) { if (isset($arFields['NOTIFY_BUTTONS'])) { foreach ($arFields['NOTIFY_BUTTONS'] as $key => $arButtons) { if (is_array($arButtons)) { if (isset($arButtons['TITLE']) && strlen($arButtons['TITLE']) > 0 && isset($arButtons['VALUE']) && strlen($arButtons['VALUE']) > 0 && isset($arButtons['TYPE']) && strlen($arButtons['TYPE']) > 0) { $arButtons['TITLE'] = htmlspecialcharsbx($arButtons['TITLE']); $arButtons['VALUE'] = htmlspecialcharsbx($arButtons['VALUE']); $arButtons['TYPE'] = htmlspecialcharsbx($arButtons['TYPE']); $arFields['NOTIFY_BUTTONS'][$key] = $arButtons; } else { unset($arFields['NOTIFY_BUTTONS'][$key]); } } else { unset($arFields['NOTIFY_BUTTONS'][$key]); } } } else { $arFields['NOTIFY_BUTTONS'] = array(array('TITLE' => GetMessage('IM_ERROR_BUTTON_ACCEPT'), 'VALUE' => 'Y', 'TYPE' => 'accept'), array('TITLE' => GetMessage('IM_ERROR_BUTTON_CANCEL'), 'VALUE' => 'N', 'TYPE' => 'cancel')); } $arParams['NOTIFY_BUTTONS'] = serialize($arFields["NOTIFY_BUTTONS"]); if (isset($arParams['NOTIFY_TAG']) && strlen($arParams['NOTIFY_TAG']) > 0) { CIMNotify::DeleteByTag($arParams['NOTIFY_TAG']); } } if ($sendToSite) { $result = IM\MessageTable::add($arParams); $messageID = IntVal($result->getId()); if ($messageID <= 0) { return false; } } else { $messageID = time(); } if (!$bConvert) { if (CModule::IncludeModule('pull')) { $CPushManager = new CPushManager(); if (isset($arFields['PUSH_MESSAGE']) && CIMSettings::GetNotifyAccess($arFields["TO_USER_ID"], $arFields['NOTIFY_MODULE'], $arFields['NOTIFY_EVENT'], CIMSettings::CLIENT_PUSH) && CModule::IncludeModule('pull')) { $CPushManager->AddQueue(array('USER_ID' => $arFields['TO_USER_ID'], 'MESSAGE' => str_replace("\n", " ", trim($arFields['PUSH_MESSAGE'])), 'PARAMS' => isset($arFields['PUSH_PARAMS']) ? $arFields['PUSH_PARAMS'] : '', 'TAG' => $arParams['NOTIFY_TAG'], 'SUB_TAG' => $arParams['NOTIFY_SUB_TAG'], 'APP_ID' => isset($arParams['PUSH_APP_ID']) ? $arParams['PUSH_APP_ID'] : '')); } else { $CPushManager->AddQueue(array('USER_ID' => $arFields['TO_USER_ID'], 'APP_ID' => isset($arParams['PUSH_APP_ID']) ? $arParams['PUSH_APP_ID'] : '')); } } foreach (GetModuleEvents("im", "OnAfterNotifyAdd", true) as $arEvent) { ExecuteModuleEventEx($arEvent, array(intval($messageID), $arFields)); } } if (!$sendToSite) { return false; } if (!empty($arFields['PARAMS'])) { CIMMessageParam::Set($messageID, $arFields['PARAMS']); } IM\ChatTable::update($chatId, array('LAST_MESSAGE_ID' => $messageID)); CIMMessenger::SpeedFileDelete($arFields['TO_USER_ID'], IM_SPEED_NOTIFY); if (!$bConvert) { //CUserCounter::Increment($arFields['TO_USER_ID'], 'im_notify_v2', '**', false); $strSql = "UPDATE b_im_relation SET STATUS = '" . IM_STATUS_UNREAD . "' WHERE USER_ID = " . intval($arFields['TO_USER_ID']) . " AND MESSAGE_TYPE = '" . IM_MESSAGE_SYSTEM . "' AND CHAT_ID = " . $chatId; $DB->Query($strSql, false, "File: " . __FILE__ . "<br>Line: " . __LINE__); if (CModule::IncludeModule("pull")) { CPullStack::AddByUser(intval($arFields['TO_USER_ID']), array('module_id' => 'im', 'command' => 'notify', 'params' => CIMNotify::GetFormatNotify(array('ID' => $messageID, 'DATE_CREATE' => time(), 'FROM_USER_ID' => intval($arFields['FROM_USER_ID']), 'MESSAGE' => $arParams['MESSAGE'], 'PARAMS' => self::PrepareParamsForPull($arFields['PARAMS']), 'NOTIFY_MODULE' => $arParams['NOTIFY_MODULE'], 'NOTIFY_EVENT' => $arParams['NOTIFY_EVENT'], 'NOTIFY_TAG' => $arParams['NOTIFY_TAG'], 'NOTIFY_TYPE' => $arParams['NOTIFY_TYPE'], 'NOTIFY_BUTTONS' => isset($arParams['NOTIFY_BUTTONS']) ? $arParams['NOTIFY_BUTTONS'] : serialize(array()), 'NOTIFY_TITLE' => isset($arParams['NOTIFY_TITLE']) ? $arParams['NOTIFY_TITLE'] : '')))); self::SendBadges($arFields['TO_USER_ID']); } } return $messageID; } else { $GLOBALS["APPLICATION"]->ThrowException(GetMessage("IM_ERROR_MESSAGE_CREATE"), "CHAT_ID"); return false; } } else { $GLOBALS["APPLICATION"]->ThrowException(GetMessage("IM_ERROR_MESSAGE_TYPE"), "MESSAGE_TYPE"); return false; } } } }