function onExecuteStartWriting(\Bitrix\Main\Event $event) { $parameters = $event->getParameters(); $userId = $parameters[0]; $dialogId = $parameters[1] . $parameters[2]; if ($userId > 0) { if (!\Bitrix\Main\Loader::includeModule('pull')) { return; } \CPushManager::DeleteFromQueueBySubTag($userId, 'IM_MESS'); if (intval($dialogId) > 0) { \CPullStack::AddByUser($dialogId, array('module_id' => 'im', 'command' => 'startWriting', 'expiry' => 60, 'params' => array('senderId' => $userId, 'dialogId' => $dialogId))); } elseif (substr($dialogId, 0, 4) == 'chat') { $chatId = substr($dialogId, 4); $arRelation = \CIMChat::GetRelationById($chatId); unset($arRelation[$userId]); $pullMessage = array('module_id' => 'im', 'command' => 'startWriting', 'expiry' => 60, 'params' => array('senderId' => $userId, 'dialogId' => $dialogId)); \CPullStack::AddByUsers(array_keys($arRelation), $pullMessage); $orm = \Bitrix\Im\ChatTable::getById($chatId); $chat = $orm->fetch(); if ($chat['TYPE'] == IM_MESSAGE_OPEN) { \CPullWatch::AddToStack('IM_PUBLIC_' . $chatId, $pullMessage); } } } }
/** * Method will be invoked after an database record updated. * * @param array $oldRecord All fields before update. * @param array $newRecord All fields after update. * * @return void */ public static function afterUpdateTrigger(array $oldRecord, array $newRecord) { if ($oldRecord['TITLE'] !== $newRecord['TITLE']) { if (\CModule::IncludeModule("pull")) { $ar = \CIMChat::GetRelationById($newRecord['CHAT_ID']); foreach ($ar as $rel) { \CIMContactList::CleanChatCache($rel['USER_ID']); \CPullStack::AddByUser($rel['USER_ID'], array('module_id' => 'im', 'command' => 'chatRename', 'params' => array('chatId' => $newRecord['CHAT_ID'], 'chatTitle' => htmlspecialcharsbx($newRecord['TITLE'])))); } } } if ($oldRecord['AVATAR'] !== $newRecord['AVATAR']) { if (\CModule::IncludeModule('pull')) { $avatarImage = \CIMChat::GetAvatarImage($newRecord['AVATAR']); $ar = \CIMChat::GetRelationById($newRecord['CHAT_ID']); foreach ($ar as $relation) { \CIMContactList::CleanChatCache($relation['USER_ID']); \CPullStack::AddByUser($relation['USER_ID'], array('module_id' => 'im', 'command' => 'chatAvatar', 'params' => array('chatId' => $newRecord['CHAT_ID'], 'chatAvatar' => $avatarImage))); } } } if ($oldRecord['COLOR'] !== $newRecord['COLOR']) { if (\CModule::IncludeModule('pull')) { $ar = \CIMChat::GetRelationById($newRecord['CHAT_ID']); foreach ($ar as $relation) { \CIMContactList::CleanChatCache($relation['USER_ID']); \CPullStack::AddByUser($relation['USER_ID'], array('module_id' => 'im', 'command' => 'chatChangeColor', 'params' => array('chatId' => $newRecord['CHAT_ID'], 'chatColor' => \Bitrix\Im\Color::getColor($newRecord['COLOR'])))); } } } }
public static function OnFileDelete($params) { $result = IM\ChatTable::getList(array('select' => array('ID', 'AUTHOR_ID'), 'filter' => array('=AVATAR' => $params['ID']))); while ($row = $result->fetch()) { IM\ChatTable::update($row['ID'], array('AVATAR' => '')); $obCache = new CPHPCache(); $arRel = CIMChat::GetRelationById($row['ID']); foreach ($arRel as $rel) { $obCache->CleanDir('/bx/imc/recent' . CIMMessenger::GetCachePath($rel['USER_ID'])); } } }
function onExecuteStartWriting(\Bitrix\Main\Event $event) { $parameters = $event->getParameters(); $userId = $parameters[0]; $dialogId = $parameters[1] . $parameters[2]; if ($userId > 0) { if (!\Bitrix\Main\Loader::includeModule('pull')) { return; } \CPushManager::DeleteFromQueueBySubTag($userId, 'IM_MESS'); if (intval($dialogId) > 0) { \CPullStack::AddByUser($dialogId, array('module_id' => 'im', 'command' => 'startWriting', 'expiry' => 60, 'params' => array('senderId' => $userId, 'dialogId' => $dialogId))); } elseif (substr($dialogId, 0, 4) == 'chat') { $arRelation = \CIMChat::GetRelationById(substr($dialogId, 4)); foreach ($arRelation as $rel) { if ($rel['USER_ID'] == $userId) { continue; } \CPullStack::AddByUser($rel['USER_ID'], array('module_id' => 'im', 'command' => 'startWriting', 'expiry' => 60, 'params' => array('senderId' => $userId, 'dialogId' => $dialogId))); } } } }
public static function StartWriting($dialogId) { global $USER; if ($USER->GetID() > 0 && strlen($dialogId) > 0 && CModule::IncludeModule("pull")) { CPushManager::DeleteFromQueueBySubTag($USER->GetID(), 'IM_MESS'); if (intval($dialogId) > 0) { CPullStack::AddByUser($dialogId, Array( 'module_id' => 'im', 'command' => 'startWriting', 'params' => Array( 'senderId' => $USER->GetID(), 'dialogId' => $dialogId ), )); } elseif (substr($dialogId, 0, 4) == 'chat') { $arRelation = CIMChat::GetRelationById(substr($dialogId, 4)); foreach ($arRelation as $rel) { if ($rel['USER_ID'] == $USER->GetID()) continue; CPullStack::AddByUser($rel['USER_ID'], Array( 'module_id' => 'im', 'command' => 'startWriting', 'params' => Array( 'senderId' => $USER->GetID(), 'dialogId' => $dialogId ), )); } } return true; } return false; }
function GetMoreChatMessage($pageId, $chatId, $bTimeZone = true) { global $DB; $iNumPage = 1; if (intval($pageId) > 0) { $iNumPage = intval($pageId); } $chatId = IntVal($chatId); $limitById = ''; $ar = \CIMChat::GetRelationById($chatId, $this->user_id); if ($ar && $ar['START_ID'] > 0) { $limitById = 'AND M.ID >= ' . intval($ar['START_ID']); } $strSql = "\n\t\t\tSELECT COUNT(M.ID) as CNT\n\t\t\tFROM b_im_message M\n\t\t\tINNER JOIN b_im_relation R1 ON M.CHAT_ID = R1.CHAT_ID\n\t\t\tWHERE R1.CHAT_ID = " . $chatId . " AND R1.USER_ID = " . $this->user_id . " " . $limitById . "\n\t\t"; $res_cnt = $DB->Query($strSql); $res_cnt = $res_cnt->Fetch(); $cnt = $res_cnt["CNT"]; $arMessages = array(); $arMessageFiles = array(); $arMessageId = array(); $usersMessage = array(); if ($cnt > 0 && ceil($cnt / 20) >= $iNumPage) { if (!$bTimeZone) { CTimeZone::Disable(); } $strSql = "\n\t\t\t\tSELECT\n\t\t\t\t\tM.ID,\n\t\t\t\t\tM.CHAT_ID,\n\t\t\t\t\tM.MESSAGE,\n\t\t\t\t\t" . $DB->DatetimeToTimestampFunction('M.DATE_CREATE') . " DATE_CREATE,\n\t\t\t\t\tM.AUTHOR_ID\n\t\t\t\tFROM b_im_message M\n\t\t\t\tINNER JOIN b_im_relation R1 ON M.CHAT_ID = R1.CHAT_ID\n\t\t\t\tWHERE R1.CHAT_ID = " . $chatId . " AND R1.USER_ID = " . $this->user_id . " " . $limitById . "\n\t\t\t\tORDER BY M.DATE_CREATE DESC, M.ID DESC\n\t\t\t"; if (!$bTimeZone) { CTimeZone::Enable(); } $dbRes = new CDBResult(); $dbRes->NavQuery($strSql, $cnt, array('iNumPage' => $iNumPage, 'nPageSize' => 20)); $CCTP = new CTextParser(); $CCTP->MaxStringLen = 200; $CCTP->allow = array("HTML" => "N", "ANCHOR" => $this->bHideLink ? "N" : "Y", "BIU" => "Y", "IMG" => "N", "QUOTE" => "N", "CODE" => "N", "FONT" => "N", "LIST" => "N", "SMILES" => $this->bHideLink ? "N" : "Y", "NL2BR" => "Y", "VIDEO" => "N", "TABLE" => "N", "CUT_ANCHOR" => "N", "ALIGN" => "N"); while ($arRes = $dbRes->Fetch()) { $arMessages[$arRes['ID']] = array('id' => $arRes['ID'], 'chatId' => $arRes['CHAT_ID'], 'senderId' => $arRes['AUTHOR_ID'], 'recipientId' => $arRes['CHAT_ID'], 'date' => $arRes['DATE_CREATE'], 'system' => $arRes['AUTHOR_ID'] > 0 ? 'N' : 'Y', 'text' => $CCTP->convertText(htmlspecialcharsbx($arRes['MESSAGE']))); $usersMessage[$arRes['CHAT_ID']][] = $arRes['ID']; $arMessageId[] = $arRes['ID']; } $params = CIMMessageParam::Get($arMessageId); $arFiles = array(); foreach ($params as $messageId => $param) { $arMessages[$messageId]['params'] = $param; if (isset($param['FILE_ID'])) { foreach ($param['FILE_ID'] as $fileId) { $arFiles[$fileId] = $fileId; } } } $arMessageFiles = CIMDisk::GetFiles($chatId, $arFiles); } return array('chatId' => $chatId, 'message' => $arMessages, 'usersMessage' => $usersMessage, 'files' => $arMessageFiles); }
public function DeleteUser($chatId, $userId, $checkPermission = true) { global $DB; $chatId = intval($chatId); $userId = intval($userId); if ($chatId <= 0 || $userId <= 0) { $GLOBALS["APPLICATION"]->ThrowException(GetMessage("IM_ERROR_EMPTY_USER_OR_CHAT"), "EMPTY_USER_OR_CHAT"); return false; } $strSql = "\n\t\t\tSELECT R.CHAT_ID, C.TITLE CHAT_TITLE, C.AUTHOR_ID CHAT_AUTHOR_ID\n\t\t\tFROM b_im_relation R LEFT JOIN b_im_chat C ON R.CHAT_ID = C.ID\n\t\t\tWHERE R.USER_ID = " . $userId . " AND R.MESSAGE_TYPE = '" . IM_MESSAGE_GROUP . "' AND R.CHAT_ID = " . $chatId; $dbRes = $DB->Query($strSql, false, "File: " . __FILE__ . "<br>Line: " . __LINE__); if ($arRes = $dbRes->Fetch()) { $chatTitle = $arRes['CHAT_TITLE']; $chatAuthorId = intval($arRes['CHAT_AUTHOR_ID']); if ($chatAuthorId == $userId) { $strSql = "\n\t\t\t\t\tSELECT R.USER_ID\n\t\t\t\t\tFROM b_im_relation R\n\t\t\t\t\tWHERE R.CHAT_ID = " . $chatId . " AND R.USER_ID <> " . $chatAuthorId; $strSql = $DB->TopSql($strSql, 1); $dbRes = $DB->Query($strSql, false, "File: " . __FILE__ . "<br>Line: " . __LINE__); if ($arRes = $dbRes->Fetch()) { $strSql = "UPDATE b_im_chat SET AUTHOR_ID = " . $arRes['USER_ID'] . " WHERE ID = " . $chatId; $DB->Query($strSql, false, "File: " . __FILE__ . "<br>Line: " . __LINE__); } } $bSelf = true; $arUsers = array($userId); if (is_object($GLOBALS["USER"]) && $GLOBALS["USER"]->GetId() != $userId) { if ($checkPermission && $chatAuthorId != $GLOBALS["USER"]->GetId()) { $GLOBALS["APPLICATION"]->ThrowException(GetMessage("IM_ERROR_KICK"), "IM_ERROR_KICK"); return false; } $bSelf = false; $arUsers[] = $GLOBALS["USER"]->GetId(); } $arSelect = array("ID", "LAST_NAME", "NAME", "LOGIN", "SECOND_NAME", "PERSONAL_GENDER"); $dbUsers = CUser::GetList($sort_by = false, $dummy = '', array('ID' => implode('|', $arUsers)), array('FIELDS' => $arSelect)); $arUsers = array(); while ($arUser = $dbUsers->Fetch()) { $arUsers[$arUser['ID']]['NAME'] = CUser::FormatName(CSite::GetNameFormat(false), $arUser, true, false); $arUsers[$arUser['ID']]['GENDER'] = $arUser["PERSONAL_GENDER"] == 'F' ? 'F' : 'M'; } if ($bSelf) { $message = GetMessage("IM_CHAT_LEAVE_" . $arUsers[$userId]['GENDER'], array('#USER_NAME#' => $arUsers[$userId]['NAME'])); } else { $message = GetMessage("IM_CHAT_KICK_" . $arUsers[$GLOBALS["USER"]->GetId()]['GENDER'], array('#USER_1_NAME#' => $arUsers[$GLOBALS["USER"]->GetId()]['NAME'], '#USER_2_NAME#' => $arUsers[$userId]['NAME'])); } $arOldRelation = array(); if (CModule::IncludeModule("pull")) { $arOldRelation = CIMChat::GetRelationById($chatId); } $CIMChat = new CIMChat($userId); $CIMChat->SetReadMessage($chatId); CIMContactList::CleanChatCache($userId); $strSql = "DELETE FROM b_im_relation WHERE CHAT_ID = " . $chatId . " AND USER_ID = " . $userId; $DB->Query($strSql, false, "File: " . __FILE__ . "<br>Line: " . __LINE__); CIMDisk::ChangeFolderMembers($chatId, $userId, false); self::AddMessage(array("TO_CHAT_ID" => $chatId, "MESSAGE" => $message, "FROM_USER_ID" => $this->user_id, "SYSTEM" => 'Y')); foreach ($arOldRelation as $rel) { CPullStack::AddByUser($rel['USER_ID'], array('module_id' => 'im', 'command' => 'chatUserLeave', 'params' => array('chatId' => $chatId, 'chatTitle' => $chatTitle, 'userId' => $userId, 'message' => $bSelf ? '' : htmlspecialcharsbx($message)))); } CIMContactList::DeleteRecent($chatId, true, $userId); return true; } $GLOBALS["APPLICATION"]->ThrowException(GetMessage("IM_ERROR_USER_NOT_FOUND"), "USER_NOT_FOUND"); return false; }
/** * Method will be invoked after an database record updated. * * @param array $oldRecord All fields before update. * @param array $newRecord All fields after update. * * @return void */ public function afterUpdateTrigger(array $oldRecord, array $newRecord) { if (!\Bitrix\Main\Loader::includeModule('pull')) { return; } $arFields = \CIMMessenger::GetById($newRecord['ID'], array('WITH_FILES' => 'Y')); if (!$arFields) { return; } $arFields['DATE_MODIFY'] = time() + \CTimeZone::GetOffset(); $CCTP = new \CTextParser(); $CCTP->MaxStringLen = 200; $CCTP->allow = array("HTML" => "N", "ANCHOR" => "Y", "BIU" => "Y", "IMG" => "N", "QUOTE" => "N", "CODE" => "N", "FONT" => "N", "LIST" => "N", "SMILES" => "Y", "NL2BR" => "Y", "VIDEO" => "N", "TABLE" => "N", "CUT_ANCHOR" => "N", "ALIGN" => "N"); $pullMessage = $CCTP->convertText(htmlspecialcharsbx($arFields['MESSAGE'])); $relations = \CIMChat::GetRelationById($arFields['CHAT_ID']); $arPullMessage = array('id' => $arFields['ID'], 'type' => $arFields['MESSAGE_TYPE'] == IM_MESSAGE_PRIVATE ? 'private' : 'chat', 'text' => $pullMessage, 'date' => $arFields['DATE_MODIFY']); if ($arFields['MESSAGE_TYPE'] == IM_MESSAGE_PRIVATE) { $arFields['FROM_USER_ID'] = $arFields['AUTHOR_ID']; foreach ($relations as $rel) { if ($rel['USER_ID'] != $arFields['AUTHOR_ID']) { $arFields['TO_USER_ID'] = $rel['USER_ID']; } } $arPullMessage['fromUserId'] = $arFields['FROM_USER_ID']; $arPullMessage['toUserId'] = $arFields['TO_USER_ID']; } else { $arPullMessage['chatId'] = $arFields['CHAT_ID']; $arPullMessage['senderId'] = $arFields['AUTHOR_ID']; } \CPullStack::AddByUsers(array_keys($relations), $p = array('module_id' => 'im', 'command' => $arFields['PARAMS']['IS_DELETED'] === 'Y' ? 'messageDelete' : 'messageUpdate', 'params' => $arPullMessage)); foreach ($relations as $rel) { $obCache = new \CPHPCache(); $obCache->CleanDir('/bx/imc/recent' . \CIMMessenger::GetCachePath($rel['USER_ID'])); } if ($newRecord['MESSAGE_TYPE'] == IM_MESSAGE_OPEN) { \CPullWatch::AddToStack('IM_PUBLIC_' . $arFields['CHAT_ID'], array('module_id' => 'im', 'command' => $arFields['PARAMS']['IS_DELETED'] === 'Y' ? 'messageDelete' : 'messageUpdate', 'params' => $arPullMessage)); } foreach (\GetModuleEvents("im", "OnAfterMessagesUpdate", true) as $arEvent) { \ExecuteModuleEventEx($arEvent, array(intval($newRecord['ID']), $arFields)); } }
public static function GetFolderModel($chatId) { if (!self::Enabled()) { return false; } $folderModel = false; $result = IM\ChatTable::getById($chatId); if (!($chat = $result->fetch())) { return false; } $folderId = intval($chat['DISK_FOLDER_ID']); $chatType = $chat['TYPE']; if ($folderId > 0) { $folderModel = \Bitrix\Disk\Folder::getById($folderId); if (!$folderModel || $folderModel->getStorageId() != self::GetStorageId()) { $folderId = 0; } } if (!$folderId) { $driver = \Bitrix\Disk\Driver::getInstance(); $storageModel = self::GetStorage(); if (!$storageModel) { return false; } $rightsManager = $driver->getRightsManager(); $fullAccessTaskId = $rightsManager->getTaskIdByName($rightsManager::TASK_FULL); $accessCodes = array(); $accessCodes[] = array('ACCESS_CODE' => 'AU', 'TASK_ID' => $fullAccessTaskId, 'NEGATIVE' => 1); $chatRelation = CIMChat::GetRelationById($chatId); if ($chatType == IM_MESSAGE_OPEN) { $departmentCode = self::GetTopDepartmentCode(); if ($departmentCode) { $accessCodes[] = array('ACCESS_CODE' => $departmentCode, 'TASK_ID' => $fullAccessTaskId); } $users = CIMContactList::GetUserData(array('ID' => array_keys($chatRelation), 'DEPARTMENT' => 'N', 'SHOW_ONLINE' => 'N')); foreach ($users['users'] as $userData) { if ($userData['extranet']) { $accessCodes[] = array('ACCESS_CODE' => 'U' . $userData['id'], 'TASK_ID' => $fullAccessTaskId); } } } else { foreach ($chatRelation as $relation) { $accessCodes[] = array('ACCESS_CODE' => 'U' . $relation['USER_ID'], 'TASK_ID' => $fullAccessTaskId); } } $folderModel = $storageModel->addFolder(array('NAME' => 'chat' . $chatId, 'CREATED_BY' => self::GetUserId()), $accessCodes); if ($folderModel) { IM\ChatTable::update($chatId, array('DISK_FOLDER_ID' => $folderModel->getId())); } } return $folderModel; }
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']); if (!IsModuleInstalled('intranet')) { if (CIMSettings::GetPrivacy(CIMSettings::PRIVACY_MESSAGE) == CIMSettings::PRIVACY_RESULT_CONTACT) { $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(); $messageID = IntVal($DB->Add("b_im_message", $arParams, Array('MESSAGE','MESSAGE_OUT'))); if ($messageID <= 0) return false; //CUserCounter::Increment($arFields['TO_USER_ID'], 'im_message', '**', 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']); 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']; } } $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'], 'SYSTEM' => $arFields['SYSTEM'] == 'Y'? 'Y': 'N', 'MESSAGE' => $arParams['MESSAGE'], 'DATE_CREATE' => time(), )), ); $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(); $messageID = IntVal($DB->Add("b_im_message", $arParams, Array('MESSAGE','MESSAGE_OUT'))); if ($messageID <= 0) return false; /* $sqlCounter = "SELECT USER_ID as ID, 1 as CNT, '**' as SITE_ID, 'im_chat' 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__); 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'], 'SYSTEM' => $arFields['SYSTEM'] == 'Y'? 'Y': 'N', 'DATE_CREATE' => time(), )), ); $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']; $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', '**', 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; }
$CIMNotify->DeleteWithCheck($_POST['NOTIFY_ID']); echo CUtil::PhpToJsObject(array('NOTIFY_ID' => intval($_POST['NOTIFY_ID']), 'ERROR' => $errorMessage)); } else { if ($_POST['IM_NOTIFY_GROUP_REMOVE'] == 'Y') { $errorMessage = ""; $CIMNotify = new CIMNotify(); if ($arNotify = $CIMNotify->GetNotify($_POST['NOTIFY_ID'])) { CIMNotify::DeleteByTag($arNotify['NOTIFY_TAG']); } echo CUtil::PhpToJsObject(array('NOTIFY_ID' => intval($_POST['NOTIFY_ID']), 'ERROR' => $errorMessage)); } else { if ($_POST['IM_RECENT_HIDE'] == 'Y') { if (substr($_POST['USER_ID'], 0, 4) == 'chat') { $userId = $USER->GetId(); $chatId = substr($_POST['USER_ID'], 4); $relation = CIMChat::GetRelationById($chatId); if (!isset($relation[$userId]) && CModule::IncludeModule('pull')) { CPullWatch::Delete($userId, 'IM_PUBLIC_' . $chatId); } CIMContactList::DeleteRecent($chatId, true); } else { CIMContactList::DeleteRecent($_POST['USER_ID']); } echo CUtil::PhpToJsObject(array('USER_ID' => htmlspecialcharsbx($_POST['USER_ID']), 'ERROR' => '')); } else { if ($_POST['IM_CHAT_ADD'] == 'Y') { $_POST['USERS'] = CUtil::JsObjectToPhp($_POST['USERS']); $errorMessage = ""; $chatId = 0; if ($_POST['USERS'] != 'open' && !is_array($_POST['USERS'])) { $errorMessage = GetMessage('IM_UNKNOWN_ERROR');
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 GetFolderModel($chatId) { if (!self::Enabled()) return false; $folderModel = false; $result = Im\ChatTable::getById($chatId); if (!$chat = $result->fetch()) return false; $folderId = intval($chat['DISK_FOLDER_ID']); if ($folderId > 0) { $folderModel = \Bitrix\Disk\Folder::getById($folderId); if (!$folderModel || $folderModel->getStorageId() != self::GetStorageId()) { $folderId = 0; } } if (!$folderId) { $driver = \Bitrix\Disk\Driver::getInstance(); $storageModel = self::GetStorage(); if (!$storageModel) { return false; } $rightsManager = $driver->getRightsManager(); $fullAccessTaskId = $rightsManager->getTaskIdByName($rightsManager::TASK_FULL); $accessCodes = array(); $accessCodes[] = Array( 'ACCESS_CODE' => 'AU', 'TASK_ID' => $fullAccessTaskId, 'NEGATIVE' => 1 ); $chatRelation = CIMChat::GetRelationById($chatId); foreach ($chatRelation as $relation) { $accessCodes[] = Array( 'ACCESS_CODE' => 'U'.$relation['USER_ID'], 'TASK_ID' => $fullAccessTaskId ); } $folderModel = $storageModel->addFolder(array('NAME' => 'chat'.$chatId, 'CREATED_BY' => self::GetUserId()), $accessCodes); if ($folderModel) Im\ChatTable::update($chatId, Array('DISK_FOLDER_ID' => $folderModel->getId())); } return $folderModel; }
public static function StartWriting($dialogId) { global $USER; if (substr($dialogId, 0, 4) == 'chat') { $arRelation = CIMChat::GetRelationById(substr($dialogId, 4)); if (!isset($arRelation[$USER->GetID()])) { return false; } } foreach (\Bitrix\Main\EventManager::getInstance()->findEventHandlers("im", "OnStartWriting") as $event) { $result = ExecuteModuleEventEx($event, array($USER->GetID(), $dialogId)); } if ($USER->GetID() > 0 && strlen($dialogId) > 0 && CModule::IncludeModule("pull")) { CPushManager::DeleteFromQueueBySubTag($USER->GetID(), 'IM_MESS'); if (intval($dialogId) > 0) { CPullStack::AddByUser($dialogId, array('module_id' => 'im', 'command' => 'startWriting', 'expiry' => 60, 'params' => array('senderId' => $USER->GetID(), 'dialogId' => $dialogId))); } elseif (substr($dialogId, 0, 4) == 'chat') { foreach ($arRelation as $rel) { if ($rel['USER_ID'] == $USER->GetID()) { continue; } CPullStack::AddByUser($rel['USER_ID'], array('module_id' => 'im', 'command' => 'startWriting', 'expiry' => 60, 'params' => array('senderId' => $USER->GetID(), 'dialogId' => $dialogId))); } } 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 OnAfterUserUpdate($arParams) { $commonChatId = CIMChat::GetGeneralChatId(); if ($commonChatId > 0 && (isset($arParams['ACTIVE']) || isset($arParams['UF_DEPARTMENT']))) { if ($arParams['ACTIVE'] == 'N') { CIMMessage::SetReadMessageAll($arParams['ID']); if ($commonChatId && CIMChat::GetRelationById($commonChatId, $arParams["ID"])) { $CIMChat = new CIMChat($arParams["ID"]); $CIMChat->DeleteUser($commonChatId, $arParams["ID"]); } } else { $commonChatId = CIMChat::GetGeneralChatId(); if ($commonChatId) { $userInChat = CIMChat::GetRelationById($commonChatId, $arParams["ID"]); $userCanJoin = CIMChat::CanJoinGeneralChatId($arParams["ID"]); if ($userInChat && !$userCanJoin) { $CIMChat = new CIMChat($arParams["ID"]); $CIMChat->DeleteUser($commonChatId, $arParams["ID"]); } else { if (!$userInChat && $userCanJoin) { $CIMChat = new CIMChat(0); $CIMChat->AddUser($commonChatId, array($arParams["ID"])); } } } } } }
public function DeleteUser($chatId, $userId, $checkPermission = true) { global $DB; $chatId = intval($chatId); $userId = intval($userId); if ($chatId <= 0 || $userId <= 0) { $GLOBALS["APPLICATION"]->ThrowException(GetMessage("IM_ERROR_EMPTY_USER_OR_CHAT"), "EMPTY_USER_OR_CHAT"); return false; } $strSql = "\n\t\t\tSELECT R.CHAT_ID, C.TITLE CHAT_TITLE, C.AUTHOR_ID CHAT_AUTHOR_ID, C.EXTRANET CHAT_EXTRANET, C.TYPE CHAT_TYPE\n\t\t\tFROM b_im_relation R LEFT JOIN b_im_chat C ON R.CHAT_ID = C.ID\n\t\t\tWHERE R.USER_ID = " . $userId . " AND R.MESSAGE_TYPE IN ('" . IM_MESSAGE_OPEN . "','" . IM_MESSAGE_CHAT . "') AND R.CHAT_ID = " . $chatId; $dbRes = $DB->Query($strSql, false, "File: " . __FILE__ . "<br>Line: " . __LINE__); if ($arRes = $dbRes->Fetch()) { $extranetFlag = $arRes["CHAT_EXTRANET"] == "" ? "" : ($arRes["CHAT_EXTRANET"] == "Y" ? true : false); $chatTitle = $arRes['CHAT_TITLE']; $chatType = $arRes['CHAT_TYPE']; $chatAuthorId = intval($arRes['CHAT_AUTHOR_ID']); if ($chatAuthorId == $userId) { $strSql = "\n\t\t\t\t\tSELECT R.USER_ID\n\t\t\t\t\tFROM b_im_relation R\n\t\t\t\t\tWHERE R.CHAT_ID = " . $chatId . " AND R.USER_ID <> " . $chatAuthorId; $strSql = $DB->TopSql($strSql, 1); $dbRes = $DB->Query($strSql, false, "File: " . __FILE__ . "<br>Line: " . __LINE__); if ($arRes = $dbRes->Fetch()) { $strSql = "UPDATE b_im_chat SET AUTHOR_ID = " . $arRes['USER_ID'] . " WHERE ID = " . $chatId; $DB->Query($strSql, false, "File: " . __FILE__ . "<br>Line: " . __LINE__); } } $bSelf = true; $arUsers = array($userId); if (is_object($GLOBALS["USER"]) && $GLOBALS["USER"]->GetId() != $userId) { if ($checkPermission && $chatAuthorId != $GLOBALS["USER"]->GetId()) { $GLOBALS["APPLICATION"]->ThrowException(GetMessage("IM_ERROR_KICK"), "IM_ERROR_KICK"); return false; } $bSelf = false; $arUsers[] = $GLOBALS["USER"]->GetId(); } $arOldRelation = CIMChat::GetRelationById($chatId); $arUsers = CIMContactList::GetUserData(array('ID' => array_keys($arOldRelation), 'DEPARTMENT' => 'N', 'USE_CACHE' => 'N')); $arUsers = $arUsers['users']; if ($bSelf) { $message = GetMessage("IM_CHAT_LEAVE_" . $arUsers[$userId]['gender'], array('#USER_NAME#' => htmlspecialcharsback($arUsers[$userId]['name']))); } else { $message = GetMessage("IM_CHAT_KICK_" . $arUsers[$GLOBALS["USER"]->GetId()]['gender'], array('#USER_1_NAME#' => htmlspecialcharsback($arUsers[$GLOBALS["USER"]->GetId()]['name']), '#USER_2_NAME#' => htmlspecialcharsback($arUsers[$userId]['name']))); } $CIMChat = new CIMChat($userId); $CIMChat->SetReadMessage($chatId); CIMContactList::CleanChatCache($userId); $publicPullWatch = false; if ($chatType == IM_MESSAGE_OPEN && CModule::IncludeModule("pull")) { $publicPullWatch = true; } $relationList = IM\RelationTable::getList(array("select" => array("ID", "USER_ID"), "filter" => array("=CHAT_ID" => $chatId, "=USER_ID" => $userId))); while ($relation = $relationList->fetch()) { if ($publicPullWatch && !$arUsers[$relation["USER_ID"]]['extranet']) { CPullWatch::Add($relation["USER_ID"], 'IM_PUBLIC_' . $chatId, true); } Im\RelationTable::delete($relation["ID"]); CIMContactList::DeleteRecent($chatId, true, $relation["USER_ID"]); if ($extranetFlag !== false) { $isExtranet = false; foreach ($arUsers as $userData) { if ($userData['id'] == $userId) { continue; } if ($userData['extranet']) { $isExtranet = true; break; } } if (!$isExtranet || $extranetFlag === "") { IM\ChatTable::update($chatId, array('EXTRANET' => $isExtranet ? "Y" : "N")); } $extranetFlag = $isExtranet; } } CIMDisk::ChangeFolderMembers($chatId, $userId, false); self::AddMessage(array("TO_CHAT_ID" => $chatId, "MESSAGE" => $message, "FROM_USER_ID" => $this->user_id, "SYSTEM" => 'Y')); foreach ($arOldRelation as $rel) { CPullStack::AddByUser($rel['USER_ID'], array('module_id' => 'im', 'command' => 'chatUserLeave', 'params' => array('chatId' => $chatId, 'chatTitle' => $chatTitle, 'userId' => $userId, 'message' => $bSelf ? '' : htmlspecialcharsbx($message)))); } return true; } $GLOBALS["APPLICATION"]->ThrowException(GetMessage("IM_ERROR_USER_NOT_FOUND"), "USER_NOT_FOUND"); return false; }
public static function SendMention($params) { $params['CHAT_ID'] = intval($params['CHAT_ID']); if (!isset($params['MESSAGE']) || $params['CHAT_ID'] <= 0) { return false; } if (!isset($params['CHAT_TITLE']) || !isset($params['CHAT_TYPE'])) { $orm = \Bitrix\Im\ChatTable::getById($params['CHAT_ID']); $chat = $orm->fetch(); if (!$chat) { return false; } $params['CHAT_TITLE'] = $chat['TITLE']; $params['CHAT_TYPE'] = trim($chat['TYPE']); } if (!in_array($params['CHAT_TYPE'], array(IM_MESSAGE_OPEN, IM_MESSAGE_CHAT))) { return false; } if (!isset($params['CHAT_RELATION'])) { $params['CHAT_RELATION'] = CIMChat::GetRelationById($params['CHAT_ID']); } $forUsers = array(); if (preg_match_all("/\\[USER=([0-9]{1,})\\](.*?)\\[\\/USER\\]/i", $params['MESSAGE'], $matches)) { if ($params['CHAT_TYPE'] == IM_MESSAGE_OPEN) { foreach ($matches[1] as $userId) { if (!isset($params['CHAT_RELATION'][$userId])) { $forUsers[$userId] = $userId; } else { if ($params['CHAT_RELATION'][$userId]['NOTIFY_BLOCK'] == 'Y' || !CIMSettings::GetNotifyAccess($params['CHAT_RELATION'][$userId]['USER_ID'], 'im', 'openChat', CIMSettings::CLIENT_PUSH)) { $forUsers[$userId] = $userId; } } } } else { foreach ($matches[1] as $userId) { if (isset($params['CHAT_RELATION'][$userId]) && ($params['CHAT_RELATION'][$userId]['NOTIFY_BLOCK'] == 'Y' || !CIMSettings::GetNotifyAccess($params['CHAT_RELATION'][$userId]['USER_ID'], 'im', 'chat', CIMSettings::CLIENT_PUSH))) { $forUsers[$userId] = $userId; } } } } $userName = \Bitrix\Im\User::getInstance($params['FROM_USER_ID'])->getName(); $userGender = \Bitrix\Im\User::getInstance($params['FROM_USER_ID'])->getGender(); if ($userName) { $chatTitle = substr(htmlspecialcharsback($params['CHAT_TITLE']), 0, 32); $notifyMail = GetMessage('IM_MESSAGE_MENTION_' . ($userGender == 'F' ? 'F' : 'M'), array('#TITLE#' => $chatTitle)); $notifyText = GetMessage('IM_MESSAGE_MENTION_' . ($userGender == 'F' ? 'F' : 'M'), array('#TITLE#' => '[CHAT=' . $params['CHAT_ID'] . ']' . $chatTitle . '[/CHAT]')); $pushText = GetMessage('IM_MESSAGE_MENTION_PUSH_' . ($userGender == 'F' ? 'F' : 'M'), array('#USER#' => $userName, '#TITLE#' => $chatTitle)) . ': ' . self::PrepareMessageForPush(array('MESSAGE' => $params['MESSAGE'], 'FILES' => $params['FILES'])); } if (strlen($notifyText) > 0) { foreach ($forUsers as $userId) { $arMessageFields = array("TO_USER_ID" => $userId, "FROM_USER_ID" => $params['FROM_USER_ID'], "NOTIFY_TYPE" => IM_NOTIFY_FROM, "NOTIFY_MODULE" => "im", "NOTIFY_EVENT" => "mention", "NOTIFY_TAG" => 'IM|MENTION|' . $params['CHAT_ID'], "NOTIFY_SUB_TAG" => "IM_MESS", "NOTIFY_MESSAGE" => $notifyText, "NOTIFY_MESSAGE_OUT" => $notifyMail, "PUSH_MESSAGE" => $pushText, "PUSH_PARAMS" => array('TAG' => 'IM_CHAT_' . $params['CHAT_ID'], 'CATEGORY' => 'ANSWER', 'URL' => SITE_DIR . 'mobile/ajax.php?mobile_action=im_answer', 'PARAMS' => array('RECIPIENT_ID' => 'chat' . $params['CHAT_ID'])), "PUSH_APP_ID" => 'Bitrix24'); CIMNotify::Add($arMessageFields); } } return true; }