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 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; }
$result = $CIMChat->AddUser($_POST['CHAT_ID'], $_POST['USERS']); if (!$result) { if ($e = $GLOBALS["APPLICATION"]->GetException()) { $errorMessage = $e->GetString(); } } echo CUtil::PhpToJsObject(array('ERROR' => $errorMessage)); } else { if ($_POST['IM_CHAT_LEAVE'] == 'Y') { $CIMChat = new CIMChat(); $result = $CIMChat->DeleteUser($_POST['CHAT_ID'], intval($_POST['USER_ID']) > 0 ? intval($_POST['USER_ID']) : $USER->GetID()); echo CUtil::PhpToJsObject(array('CHAT_ID' => intval($_POST['CHAT_ID']), 'USER_ID' => intval($_POST['USER_ID']), 'ERROR' => $result ? '' : 'AUTHORIZE_ERROR')); } else { if ($_POST['IM_CHAT_RENAME'] == 'Y') { CUtil::decodeURIComponent($_POST); $CIMChat = new CIMChat(); $CIMChat->Rename($_POST['CHAT_ID'], $_POST['CHAT_TITLE']); echo CUtil::PhpToJsObject(array('CHAT_ID' => intval($_POST['CHAT_ID']), 'CHAT_TITLE' => $_POST['CHAT_TITLE'], 'ERROR' => '')); } else { if ($_POST['IM_CALL'] == 'Y') { $errorMessage = ""; if ($_POST['COMMAND'] == 'invite') { CIMMessenger::CallCommand($_POST['RECIPIENT_ID'], $_POST['COMMAND'], array('video' => $_POST['VIDEO'] == 'Y')); } else { if ($_POST['COMMAND'] == 'signaling') { CIMMessenger::CallCommand($_POST['RECIPIENT_ID'], $_POST['COMMAND'], array('peer' => $_POST['PEER'])); } else { CIMMessenger::CallCommand($_POST['RECIPIENT_ID'], $_POST['COMMAND']); } } echo CUtil::PhpToJsObject(array('ERROR' => $errorMessage));
/** * 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 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 InitCounters($userId, $check = true) { return false; if (intval($userId) <= 0) return false; $send = false; if (!$check || !CUserOptions::GetOption('im', 'initCounterNotify2', false, $userId)) { CIMNotify::SetUnreadCounter($userId); CUserOptions::SetOption('im', 'initCounterNotify2', true, $userId); $send = true; } if (!$check || !CUserOptions::GetOption('im', 'initCounterChat2', false, $userId)) { CIMChat::SetUnreadCounter($userId); CUserOptions::SetOption('im', 'initCounterChat2', true, $userId); $send = true; } if (!$check || !CUserOptions::GetOption('im', 'initCounterMessage2', false, $userId)) { CIMMessage::SetUnreadCounter($userId); CUserOptions::SetOption('im', 'initCounterMessage2', true, $userId); $send = true; } if ($send) CIMMessenger::SendBadges($userId); return true; }
public static function GetFormatMessage($arParams) { $arParams['ID'] = intval($arParams['ID']); $arParams['TO_USER_ID'] = isset($arParams['TO_CHAT_ID'])? intval($arParams['TO_CHAT_ID']): intval($arParams['TO_USER_ID']); $arParams['FROM_USER_ID'] = intval($arParams['FROM_USER_ID']); $arParams['MESSAGE'] = trim($arParams['MESSAGE']); $arParams['DATE_CREATE'] = intval($arParams['DATE_CREATE']); $arParams['PARAMS'] = empty($arParams['PARAMS'])? Array(): $arParams['PARAMS']; $arUsers = CIMContactList::GetUserData(Array('ID' => isset($arParams['TO_CHAT_ID'])? $arParams['FROM_USER_ID']: Array($arParams['TO_USER_ID'], $arParams['FROM_USER_ID']))); $arChat = Array(); if (isset($arParams['TO_CHAT_ID'])) { $arChat = CIMChat::GetChatData(array( 'ID' => $arParams['TO_USER_ID'], 'USE_CACHE' => 'N' )); } $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"); $text = $CCTP->convertText(htmlspecialcharsbx($arParams['MESSAGE'])); $text_mobile = strip_tags(preg_replace("/<img.*?data-code=\"([^\"]*)\".*?>/i", "$1", $CCTP->convertText(htmlspecialcharsbx(preg_replace("/\[s\].*?\[\/s\]/i", "", $arParams['MESSAGE'])))) , '<br>'); return Array( 'CHAT_ID' => $arParams['CHAT_ID'], 'CHAT' => isset($arChat['chat'])? $arChat['chat']: Array(), 'USER_IN_CHAT' => isset($arChat['userInChat'])? $arChat['userInChat']: Array(), 'USER_BLOCK_CHAT' => $arChat['userChatBlockStatus'], 'USERS' => $arUsers['users'], 'MESSAGE' => Array( 'id' => $arParams['ID'], 'chatId' => $arParams['CHAT_ID'], 'senderId' => $arParams['FROM_USER_ID'], 'recipientId' => isset($arParams['TO_CHAT_ID'])? 'chat'.$arParams['TO_USER_ID']: $arParams['TO_USER_ID'], 'system' => $arParams['SYSTEM'] == 'Y'? 'Y': 'N', 'date' => $arParams['DATE_CREATE'], 'text' => $text, 'text_mobile' => $text_mobile, 'params' => $arParams['PARAMS'] ), 'FILES' => isset($arParams['FILES'])? $arParams['FILES']: Array(), ); }
} else { define("IM_COMPONENT_INIT", true); } if (intval($USER->GetID()) <= 0) { return; } if (!CModule::IncludeModule('im')) { return; } $GLOBALS["APPLICATION"]->SetPageProperty("BodyClass", "im-page"); $arResult = array(); $arSettings = CIMSettings::Get(); $arResult['SETTINGS'] = $arSettings['settings']; $CIMMessage = new CIMMessage(); $arResult['MESSAGE'] = $CIMMessage->GetUnreadMessage(array('USE_TIME_ZONE' => 'N', 'ORDER' => 'ASC')); $CIMChat = new CIMChat(); $arChatMessage = $CIMChat->GetUnreadMessage(array('USE_TIME_ZONE' => 'N', 'ORDER' => 'ASC')); if ($arChatMessage['result']) { foreach ($arChatMessage['message'] as $id => $ar) { $ar['recipientId'] = 'chat' . $ar['recipientId']; $arResult['MESSAGE']['message'][$id] = $ar; } foreach ($arChatMessage['usersMessage'] as $chatId => $ar) { $arResult['MESSAGE']['usersMessage']['chat' . $chatId] = $ar; } foreach ($arChatMessage['unreadMessage'] as $chatId => $ar) { $arResult['MESSAGE']['unreadMessage']['chat' . $chatId] = $ar; } foreach ($arChatMessage['users'] as $key => $value) { $arResult['MESSAGE']['users'][$key] = $value; }
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 static function GetFormatMessage($arParams) { $arParams['ID'] = intval($arParams['ID']); $arParams['TO_USER_ID'] = isset($arParams['TO_CHAT_ID']) ? intval($arParams['TO_CHAT_ID']) : intval($arParams['TO_USER_ID']); $arParams['FROM_USER_ID'] = intval($arParams['FROM_USER_ID']); $arParams['MESSAGE'] = trim($arParams['MESSAGE']); $arParams['DATE_CREATE'] = intval($arParams['DATE_CREATE']); $arUsers = CIMContactList::GetUserData(array('ID' => isset($arParams['TO_CHAT_ID']) ? $arParams['FROM_USER_ID'] : array($arParams['TO_USER_ID'], $arParams['FROM_USER_ID']))); $arChat = array(); if (isset($arParams['TO_CHAT_ID'])) { $arChat = CIMChat::GetChatData(array('ID' => $arParams['TO_USER_ID'], 'USE_CACHE' => 'N')); } $CCTP = new CTextParser(); $CCTP->MaxStringLen = 200; $CCTP->allow = array("HTML" => "N", "ANCHOR" => "Y", "BIU" => "N", "IMG" => "N", "QUOTE" => "N", "CODE" => "N", "FONT" => "N", "LIST" => "N", "SMILES" => "N", "NL2BR" => "Y", "VIDEO" => "N", "TABLE" => "N", "CUT_ANCHOR" => "N", "ALIGN" => "N"); $CCTPM = new CTextParser(); $CCTPM->MaxStringLen = 200; $CCTPM->allow = array("HTML" => "N", "ANCHOR" => "N", "BIU" => "N", "IMG" => "N", "QUOTE" => "N", "CODE" => "N", "FONT" => "N", "LIST" => "N", "SMILES" => "N", "NL2BR" => "Y", "VIDEO" => "N", "TABLE" => "N", "CUT_ANCHOR" => "N", "ALIGN" => "N"); return array('CHAT' => isset($arChat['chat']) ? $arChat['chat'] : array(), 'USER_IN_CHAT' => isset($arChat['userInChat']) ? $arChat['userInChat'] : array(), 'USERS' => $arUsers['users'], 'MESSAGE' => array('id' => $arParams['ID'], 'senderId' => $arParams['FROM_USER_ID'], 'recipientId' => isset($arParams['TO_CHAT_ID']) ? 'chat' . $arParams['TO_USER_ID'] : $arParams['TO_USER_ID'], 'date' => $arParams['DATE_CREATE'], 'text' => $CCTP->convertText(htmlspecialcharsbx($arParams['MESSAGE'])), 'text_mobile' => $CCTPM->convertText(htmlspecialcharsbx($arParams['MESSAGE'])))); }
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 MessageToChat($chatId, $messageId, $userId = 0, $getUserData = false, $addGenderToMessageId = true) { $chatId = intval($chatId); if ($chatId <= 0 || strlen($messageId) <= 0) return false; $userId = intval($userId); $message = ''; if ($userId > 0 && $getUserData) { $arSelect = Array("ID", "LAST_NAME", "NAME", "LOGIN", "SECOND_NAME", "PERSONAL_GENDER"); $dbUsers = CUser::GetList(($sort_by = false), ($dummy=''), array('ID' => $userId), array('FIELDS' => $arSelect)); if ($arUser = $dbUsers->Fetch()) $message = GetMessage($messageId.($addGenderToMessageId? ($arUser["PERSONAL_GENDER"] == 'F'? 'F': 'M'): ''), Array('#USER_NAME#' => CUser::FormatName(CSite::GetNameFormat(false), $arUser, true, false))); } else { $message = GetMessage($messageId); } CIMChat::AddMessage(Array( "FROM_USER_ID" => $userId, "TO_CHAT_ID" => $chatId, "MESSAGE" => $message, "SYSTEM" => 'Y', )); return true; }
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); }
die; } if (isset($_REQUEST['AJAX_CALL']) && $_REQUEST['AJAX_CALL'] == 'Y') { return; } if (intval($USER->GetID()) <= 0) { return; } if (!CModule::IncludeModule('im')) { return; } $chatId = intval($_GET['chat_id']); if ($chatId <= 0) { return; } $arChat = CIMChat::GetChatData(array('ID' => $chatId, 'USE_CACHE' => 'N')); $arResult['CHAT_ID'] = $chatId; $arResult['CHAT'] = $arChat['chat']; $arResult['USERS'] = array(); if (!empty($arChat['userInChat'][$chatId])) { $ar = CIMContactList::GetUserData(array('ID' => $arChat['userInChat'][$chatId], 'DEPARTMENT' => 'N', 'USE_CACHE' => 'Y')); $arResult['USERS'] = $ar['users']; } if (!$arResult['USERS'][$USER->GetId()]) { return; } $GLOBALS["APPLICATION"]->SetPageProperty("Viewport", "user-scalable=no, initial-scale=1.0, maximum-scale=1.0, width=290"); $GLOBALS["APPLICATION"]->SetPageProperty("BodyClass", "chat-profile-page"); if (!(isset($arParams['TEMPLATE_HIDE']) && $arParams['TEMPLATE_HIDE'] == 'Y')) { $this->IncludeComponentTemplate(); }
public function SendChatMessage($dialogId, $incomingType, $message) { if (strlen($message) <= 0 || strlen($dialogId) <= 0) { return false; } if (CVoxImplantConfig::GetChatAction() == CVoxImplantConfig::INTERFACE_CHAT_NONE) { return false; } if (!CModule::IncludeModule('im')) { return false; } // TODO CHECK NULL USER BEFORE SEND $chatId = 0; if (substr($dialogId, 0, 4) == 'chat') { $chatId = intval(substr($dialogId, 4)); CIMChat::AddMessage(array("FROM_USER_ID" => $incomingType == CVoxImplantMain::CALL_OUTGOING ? $this->userId : 0, "TO_CHAT_ID" => $chatId, "MESSAGE" => $message, "SYSTEM" => 'Y')); } else { if (intval($dialogId) > 0) { CIMMessage::Add(array("FROM_USER_ID" => $incomingType == CVoxImplantMain::CALL_OUTGOING ? $this->userId : intval($dialogId), "TO_USER_ID" => $incomingType == CVoxImplantMain::CALL_OUTGOING ? intval($dialogId) : $this->userId, "MESSAGE" => $message, "SYSTEM" => 'Y')); } } return true; }
public static function GetRecentList($arParams = array()) { global $DB, $USER; $bLoadUnreadMessage = isset($arParams['LOAD_UNREAD_MESSAGE']) && $arParams['LOAD_UNREAD_MESSAGE'] == 'Y' ? true : false; $bTimeZone = isset($arParams['USE_TIME_ZONE']) && $arParams['USE_TIME_ZONE'] == 'N' ? false : true; $bSmiles = isset($arParams['USE_SMILES']) && $arParams['USE_SMILES'] == 'N' ? false : true; $userId = isset($arParams['USER_ID']) ? $arParams['USER_ID'] : $USER->GetId(); $nameTemplate = self::GetUserNameTemplate(SITE_ID); $nameTemplateSite = CSite::GetNameFormat(false); $bIntranetEnable = IsModuleInstalled('intranet') && CModule::IncludeModule('intranet') ? true : false; $arRecent = array(); $arUsers = array(); $bColorEnabled = IM\Color::isEnabled(); $cache_ttl = 2592000; $cache_id = 'im_recent_v8_' . $userId . '_' . $bColorEnabled; $cache_dir = '/bx/imc/recent' . CIMMessenger::GetCachePath($userId); $obCache = new CPHPCache(); if ($obCache->InitCache($cache_ttl, $cache_id, $cache_dir)) { $ar = $obCache->GetVars(); $arRecent = $ar['recent']; $arUsers = $ar['users']; } else { if (!$bTimeZone) { CTimeZone::Disable(); } $strSql = "\n\t\t\t\tSELECT\n\t\t\t\t\tR.ITEM_TYPE, R.ITEM_ID,\n\t\t\t\t\tR.ITEM_MID M_ID, M.AUTHOR_ID M_AUTHOR_ID, M.ID M_ID, M.CHAT_ID M_CHAT_ID, M.MESSAGE M_MESSAGE, " . $DB->DatetimeToTimestampFunction('M.DATE_CREATE') . " M_DATE_CREATE,\n\t\t\t\t\tC.TITLE C_TITLE, C.AUTHOR_ID C_OWNER_ID, C.ENTITY_TYPE C_ENTITY_TYPE, C.AVATAR C_AVATAR, C.CALL_NUMBER C_CALL_NUMBER, C.EXTRANET CHAT_EXTRANET, C.COLOR CHAT_COLOR, C.TYPE CHAT_TYPE,\n\t\t\t\t\tU.LOGIN, U.NAME, U.LAST_NAME, U.PERSONAL_PHOTO, U.SECOND_NAME, U.PERSONAL_BIRTHDAY, U.PERSONAL_GENDER, U.EXTERNAL_AUTH_ID, U.WORK_POSITION,\n\t\t\t\t\tC1.USER_ID RID\n\t\t\t\tFROM\n\t\t\t\tb_im_recent R\n\t\t\t\tLEFT JOIN b_user U ON R.ITEM_TYPE = '" . IM_MESSAGE_PRIVATE . "' AND R.ITEM_ID = U.ID\n\t\t\t\tLEFT JOIN b_im_chat C ON R.ITEM_TYPE != '" . IM_MESSAGE_PRIVATE . "' AND R.ITEM_ID = C.ID\n\t\t\t\tLEFT JOIN b_im_message M ON R.ITEM_MID = M.ID\n\t\t\t\tLEFT JOIN b_im_relation C1 ON C1.CHAT_ID = C.ID AND C1.USER_ID = " . $userId . "\n\t\t\t\tWHERE R.USER_ID = " . $userId; if (!$bTimeZone) { CTimeZone::Enable(); } $toDelete = array(); $arMessageId = array(); $CCTP = new CTextParser(); $CCTP->MaxStringLen = 255; $CCTP->allow = array("HTML" => "N", "ANCHOR" => "Y", "BIU" => "Y", "IMG" => "N", "QUOTE" => "N", "CODE" => "N", "FONT" => "N", "LIST" => "N", "SMILES" => $bSmiles ? "Y" : "N", "NL2BR" => "Y", "VIDEO" => "N", "TABLE" => "N", "CUT_ANCHOR" => "N", "ALIGN" => "N"); $dbRes = $DB->Query($strSql, false, "File: " . __FILE__ . "<br>Line: " . __LINE__); while ($arRes = $dbRes->GetNext(true, false)) { $arRes['ITEM_TYPE'] = trim($arRes['ITEM_TYPE']); if ($arRes['ITEM_TYPE'] == IM_MESSAGE_OPEN) { if (intval($arRes['RID']) <= 0 && IM\User::getInstance($userId)->isExtranet()) { continue; } } else { if ($arRes['ITEM_TYPE'] == IM_MESSAGE_CHAT) { if (intval($arRes['RID']) <= 0) { continue; } } } $arMessageId[] = $arRes['M_ID']; if ($arRes['M_DATE_CREATE'] + 2592000 < time()) { $toDelete[$arRes['ITEM_TYPE']][] = $arRes['ITEM_ID']; continue; } $itemId = $arRes['ITEM_ID']; $item = array('TYPE' => $arRes['ITEM_TYPE'], 'MESSAGE' => array('id' => $arRes['M_ID'], 'chatId' => $arRes['M_CHAT_ID'], 'senderId' => $arRes['M_AUTHOR_ID'], 'date' => $arRes['M_DATE_CREATE'], 'text' => $CCTP->convertText(preg_replace("/\\[s\\].*?\\[\\/s\\]/i", "", $arRes['M_MESSAGE'])))); $item['MESSAGE']['text'] = preg_replace('#\\-{54}.+?\\-{54}#s', " [" . GetMessage('IM_QUOTE') . "] ", strip_tags(str_replace(array("<br>", "<br/>", "<br />", "#BR#"), array(" ", " ", " ", " "), $item['MESSAGE']['text']), "<img>")); if ($arRes['ITEM_TYPE'] == IM_MESSAGE_PRIVATE) { $arUsers[] = $arRes['ITEM_ID']; $arFileTmp = CFile::ResizeImageGet($arRes["PERSONAL_PHOTO"], array('width' => 58, 'height' => 58), BX_RESIZE_IMAGE_EXACT, false, false, true); $item['USER'] = array('id' => $arRes['ITEM_ID'], 'name' => CUser::FormatName($nameTemplateSite, $arRes, true, false), 'nameList' => CUser::FormatName($nameTemplate, $arRes, true, false), 'workPosition' => $arRes['WORK_POSITION'], 'color' => self::GetUserColor($arRes["ID"], $arRes['PERSONAL_GENDER'] == 'M' ? 'M' : 'F'), 'avatar' => empty($arFileTmp['src']) ? '/bitrix/js/im/images/blank.gif' : $arFileTmp['src'], 'status' => 'offline', 'birthday' => $arRes['PERSONAL_BIRTHDAY'], 'gender' => $arRes['PERSONAL_GENDER'] == 'F' ? 'F' : 'M', 'extranet' => false, 'network' => $arRes['EXTERNAL_AUTH_ID'] == self::NETWORK_AUTH_ID, 'phoneDevice' => false, 'profile' => CIMContactList::GetUserPath($arRes["ITEM_ID"])); } else { $chatType = $arRes["ITEM_TYPE"] == IM_MESSAGE_OPEN ? 'open' : 'chat'; if ($arRes["C_ENTITY_TYPE"] == 'CALL') { $chatType = 'call'; } $itemId = 'chat' . $itemId; $item['CHAT'] = array('id' => $arRes['ITEM_ID'], 'name' => $arRes["C_TITLE"], 'color' => $arRes["CHAT_COLOR"] == "" ? IM\Color::getColorByNumber($arRes['ITEM_ID']) : IM\Color::getColor($arRes['CHAT_COLOR']), 'avatar' => CIMChat::GetAvatarImage($arRes["C_AVATAR"]), 'extranet' => $arRes["CHAT_EXTRANET"] == "" ? "" : ($arRes["CHAT_EXTRANET"] == "Y" ? true : false), 'owner' => $arRes["C_OWNER_ID"], 'type' => $chatType, 'messageType' => $arRes['CHAT_TYPE'], 'call_number' => $arRes["C_CALL_NUMBER"]); } $arRecent[$itemId] = $item; } $params = CIMMessageParam::Get($arMessageId); foreach ($arRecent as $key => $value) { if (isset($params[$value['MESSAGE']['id']])) { if (count($params[$value['MESSAGE']['id']]['FILE_ID']) > 0 && strlen(trim($arRecent[$key]['MESSAGE']['text'])) <= 0) { $arRecent[$key]['MESSAGE']['text'] = "[" . GetMessage('IM_FILE') . "]"; } $arRecent[$key]['MESSAGE']['params'] = $params[$value['MESSAGE']['id']]; } } if (!empty($toDelete)) { if (isset($toDelete[IM_MESSAGE_PRIVATE])) { self::DeleteRecent($toDelete[IM_MESSAGE_PRIVATE]); } if (isset($toDelete[IM_MESSAGE_CHAT])) { self::DeleteRecent($toDelete[IM_MESSAGE_CHAT], true); } if (isset($toDelete[IM_MESSAGE_OPEN])) { self::DeleteRecent($toDelete[IM_MESSAGE_OPEN], true); } } $bExtranetEnable = IsModuleInstalled('extranet'); $bVoximplantEnable = IsModuleInstalled('voximplant'); if ($bExtranetEnable || $bVoximplantEnable) { $arUserPhone = array(); $arUserDepartment = array(); $arSelectParams = array(); if ($bExtranetEnable) { $arSelectParams[] = 'UF_DEPARTMENT'; } if ($bVoximplantEnable) { $arSelectParams[] = 'UF_VI_PHONE'; } $dbUsers = CUser::GetList($sort_by = array('last_name' => 'asc'), $dummy = '', array('ID' => $userId . "|" . implode('|', $arUsers)), array('FIELDS' => array("ID"), 'SELECT' => $arSelectParams)); while ($arUser = $dbUsers->GetNext(true, false)) { $arUserPhone[$arUser['ID']] = $arUser['UF_VI_PHONE'] == 'Y'; $arUserDepartment[$arUser['ID']] = self::IsExtranet($arUser); } foreach ($arRecent as $key => $value) { if (isset($value['USER'])) { $arRecent[$key]['USER']['extranet'] = $arUserDepartment[$value['USER']['id']]; $arRecent[$key]['USER']['phoneDevice'] = $arUserPhone[$value['USER']['id']]; } } } if ($obCache->StartDataCache()) { $obCache->EndDataCache(array('recent' => $arRecent, 'users' => $arUsers)); } } $arOnline = CIMStatus::GetList(array('ID' => array_values($arUsers), 'GET_OFFLINE' => 'Y')); foreach ($arRecent as $key => $value) { if ($value['TYPE'] != IM_MESSAGE_PRIVATE) { continue; } $arRecent[$key]['USER']['birthday'] = $bIntranetEnable ? CIntranetUtils::IsToday($value['USER']['birthday']) : false; $arRecent[$key]['USER']['status'] = isset($arOnline['users'][$value['USER']['id']]) ? $arOnline['users'][$value['USER']['id']]['status'] : 'offline'; $arRecent[$key]['USER']['idle'] = isset($arOnline['users'][$value['USER']['id']]) ? $arOnline['users'][$value['USER']['id']]['idle'] : 0; $arRecent[$key]['USER']['mobileLastDate'] = isset($arOnline['users'][$value['USER']['id']]) ? $arOnline['users'][$value['USER']['id']]['mobileLastDate'] : 0; if ($arOnline['users'][$value['USER']['id']]['color']) { $arRecent[$key]['USER']['color'] = $arOnline['users'][$value['USER']['id']]['color']; } } if ($bLoadUnreadMessage) { $CIMMessage = new CIMMessage(false, array('HIDE_LINK' => 'Y')); $ar = $CIMMessage->GetUnreadMessage(array('LOAD_DEPARTMENT' => 'N', 'ORDER' => 'ASC', 'GROUP_BY_CHAT' => 'Y', 'USE_TIME_ZONE' => $bTimeZone ? 'Y' : 'N', 'USE_SMILES' => $bSmiles ? 'Y' : 'N')); foreach ($ar['message'] as $data) { if (!isset($arRecent[$data['senderId']])) { $arRecent[$data['senderId']] = array('TYPE' => IM_MESSAGE_PRIVATE, 'USER' => $ar['users'][$data['senderId']]); } $arRecent[$data['senderId']]['MESSAGE'] = array('id' => $data['id'], 'senderId' => $data['senderId'], 'date' => $data['date'], 'text' => preg_replace('#\\-{54}.+?\\-{54}#s', " [" . GetMessage('IM_QUOTE') . "] ", strip_tags(str_replace(array("<br>", "<br/>", "<br />", "#BR#"), array(" ", " ", " ", " "), $data['text']), "<img>"))); $arRecent[$data['senderId']]['COUNTER'] = $data['counter']; } $CIMChat = new CIMChat(false, array('HIDE_LINK' => 'Y')); $ar = $CIMChat->GetUnreadMessage(array('ORDER' => 'ASC', 'GROUP_BY_CHAT' => 'Y', 'USER_LOAD' => 'N', 'FILE_LOAD' => 'N', 'USE_SMILES' => $bSmiles ? 'Y' : 'N', 'USE_TIME_ZONE' => $bTimeZone ? 'Y' : 'N')); foreach ($ar['message'] as $data) { if (!isset($arRecent['chat' . $data['recipientId']])) { $arRecent['chat' . $data['recipientId']] = array('TYPE' => $ar['messageType'] ? $ar['messageType'] : IM_MESSAGE_CHAT, 'CHAT' => $ar['chat']); } $arRecent['chat' . $data['recipientId']]['MESSAGE'] = array('id' => $data['id'], 'senderId' => $data['senderId'], 'date' => $data['date'], 'text' => $data['text']); $arRecent['chat' . $data['recipientId']]['COUNTER'] = $data['counter']; } } if (!empty($arRecent)) { sortByColumn($arRecent, array('COUNTER' => array(SORT_NUMERIC, SORT_DESC), 'MESSAGE' => array(SORT_NUMERIC, SORT_DESC)), array('COUNTER' => array(__CLASS__, 'GetRecentListSortCounter'), 'MESSAGE' => array(__CLASS__, 'GetRecentListSortMessage')), null, true); } return $arRecent; }
public static function GetRecentList($arParams = Array()) { global $DB, $USER; $bLoadUnreadMessage = isset($arParams['LOAD_UNREAD_MESSAGE']) && $arParams['LOAD_UNREAD_MESSAGE'] == 'Y'? true: false; $bTimeZone = isset($arParams['USE_TIME_ZONE']) && $arParams['USE_TIME_ZONE'] == 'N'? false: true; $bSmiles = isset($arParams['USE_SMILES']) && $arParams['USE_SMILES'] == 'N'? false: true; $nameTemplate = COption::GetOptionString("im", "user_name_template", "#LAST_NAME# #NAME#", SITE_ID); $nameTemplateSite = CSite::GetNameFormat(false); $nameOfSite = CModule::IncludeModule('extranet') && !CExtranet::IsIntranetUser()? "ex": false; $bIntranetEnable = IsModuleInstalled('intranet') && CModule::IncludeModule('intranet')? true: false; $arRecent = Array(); $arUsers = Array(); $cache_ttl = 2592000; $cache_id = $GLOBALS['USER']->GetID(); $cache_dir = '/bx/imc/rec'.CIMMessenger::GetCachePath($cache_id); $obCache = new CPHPCache(); if($obCache->InitCache($cache_ttl, $cache_id, $cache_dir)) { $ar = $obCache->GetVars(); $arRecent = $ar['recent']; $arUsers = $ar['users']; } else { if (!$bTimeZone) CTimeZone::Disable(); $strSql = " SELECT R.ITEM_TYPE, R.ITEM_ID, R.ITEM_MID M_ID, M.AUTHOR_ID M_AUTHOR_ID, M.MESSAGE M_MESSAGE, ".$DB->DatetimeToTimestampFunction('M.DATE_CREATE')." M_DATE_CREATE, C.TITLE C_TITLE, C.AUTHOR_ID C_OWNER_ID, U.LOGIN, U.NAME, U.LAST_NAME, U.PERSONAL_PHOTO, U.SECOND_NAME, U.PERSONAL_BIRTHDAY, U.PERSONAL_GENDER FROM b_im_recent R LEFT JOIN b_user U ON R.ITEM_TYPE = '".IM_MESSAGE_PRIVATE."' AND R.ITEM_ID = U.ID LEFT JOIN b_im_chat C ON R.ITEM_TYPE = '".IM_MESSAGE_GROUP."' AND R.ITEM_ID = C.ID LEFT JOIN b_im_message M ON R.ITEM_MID = M.ID WHERE R.USER_ID = ".$USER->GetId(); if (!$bTimeZone) CTimeZone::Enable(); $toDelete = Array(); $CCTP = new CTextParser(); $CCTP->MaxStringLen = 255; $CCTP->allow = array("HTML" => "N", "ANCHOR" => "Y", "BIU" => "Y", "IMG" => "N", "QUOTE" => "N", "CODE" => "N", "FONT" => "N", "LIST" => "N", "SMILES" => ($bSmiles? "Y": "N"), "NL2BR" => "Y", "VIDEO" => "N", "TABLE" => "N", "CUT_ANCHOR" => "N", "ALIGN" => "N"); $dbRes = $DB->Query($strSql, false, "File: ".__FILE__."<br>Line: ".__LINE__); while ($arRes = $dbRes->GetNext(true, false)) { $arRes['ITEM_TYPE'] = trim($arRes['ITEM_TYPE']); if ($arRes['M_DATE_CREATE']+2592000 < time()) { $toDelete[$arRes['ITEM_TYPE']][] = $arRes['ITEM_ID']; continue; } $itemId = $arRes['ITEM_ID']; $item = Array( 'TYPE' => $arRes['ITEM_TYPE'], 'MESSAGE' => Array( 'id' => $arRes['M_ID'], 'senderId' => $arRes['M_AUTHOR_ID'], 'date' => $arRes['M_DATE_CREATE'], 'text' => $CCTP->convertText(preg_replace("/\[s\].*?\[\/s\]/i", "", $arRes['M_MESSAGE'])) ) ); $item['MESSAGE']['text'] = preg_replace("/------------------------------------------------------(.*)------------------------------------------------------/mi", " [".GetMessage('IM_QUOTE')."] ", strip_tags(str_replace(array("<br>","<br/>","<br />", "#BR#"), Array(" "," ", " ", " "), $item['MESSAGE']['text']), "<img>")); if ($arRes['ITEM_TYPE'] == IM_MESSAGE_PRIVATE) { $arUsers[] = $arRes['ITEM_ID']; $arFileTmp = CFile::ResizeImageGet( $arRes["PERSONAL_PHOTO"], array('width' => 58, 'height' => 58), BX_RESIZE_IMAGE_EXACT, false ); $item['USER'] = Array( 'id' => $arRes['ITEM_ID'], 'name' => CUser::FormatName($nameTemplateSite, $arRes, true, false), 'nameList' => CUser::FormatName($nameTemplate, $arRes, true, false), 'avatar' => empty($arFileTmp['src'])? '/bitrix/js/im/images/blank.gif': $arFileTmp['src'], 'gender' => $arRes['PERSONAL_GENDER'] == 'F'? 'F': 'M', 'status' => 'offline', 'birthday' => $arRes['PERSONAL_BIRTHDAY'], 'profile' => CComponentEngine::MakePathFromTemplate(COption::GetOptionString('im', 'path_to_user_profile', "", $nameOfSite), array("user_id" => $arRes["ITEM_ID"])) ); } else { $itemId = 'chat'.$itemId; $item['CHAT'] = Array( 'id' => $arRes['ITEM_ID'], 'name' => $arRes["C_TITLE"], 'owner' => $arRes["C_OWNER_ID"], ); } $arRecent[$itemId] = $item; } // remove old entity if (!empty($toDelete)) { if (isset($toDelete[IM_MESSAGE_PRIVATE])) self::DeleteRecent($toDelete[IM_MESSAGE_PRIVATE]); if (isset($toDelete[IM_MESSAGE_GROUP])) self::DeleteRecent($toDelete[IM_MESSAGE_GROUP], true); } if($obCache->StartDataCache()) $obCache->EndDataCache(Array('recent' => $arRecent, 'users' => $arUsers)); } $arOnline = self::GetStatus(Array('ID' => array_values($arUsers))); foreach ($arRecent as $key => $value) { if ($value['TYPE'] != IM_MESSAGE_PRIVATE) continue; $arRecent[$key]['USER']['birthday'] = $bIntranetEnable? CIntranetUtils::IsToday($value['USER']['birthday']): false; $arRecent[$key]['USER']['status'] = isset($arOnline['users'][$value['USER']['id']])? 'online': 'offline'; } if ($bLoadUnreadMessage) { $CIMMessage = new CIMMessage(false, Array( 'hide_link' => true )); $ar = $CIMMessage->GetUnreadMessage(Array( 'LOAD_DEPARTMENT' => 'N', 'ORDER' => 'ASC', 'GROUP_BY_CHAT' => 'Y', 'USE_TIME_ZONE' => $bTimeZone? 'Y': 'N', 'USE_SMILES' => $bSmiles? 'Y': 'N' )); foreach ($ar['message'] as $data) { if (!isset($arRecent[$data['senderId']])) { $arRecent[$data['senderId']] = Array( 'TYPE' => IM_MESSAGE_PRIVATE, 'USER' => $ar['users'][$data['senderId']] ); } $arRecent[$data['senderId']]['MESSAGE'] = Array( 'id' => $data['id'], 'senderId' => $data['senderId'], 'date' => $data['date'], 'text' => preg_replace("/------------------------------------------------------(.*)------------------------------------------------------/mi", " [".GetMessage('IM_QUOTE')."] ", strip_tags(str_replace(array("<br>","<br/>","<br />", "#BR#"), Array(" ", " ", " ", " "), $data['text']), "<img>")) ); $arRecent[$data['senderId']]['COUNTER'] = $data['counter']; } $CIMChat = new CIMChat(false, Array( 'hide_link' => true )); $ar = $CIMChat->GetUnreadMessage(Array( 'ORDER' => 'ASC', 'GROUP_BY_CHAT' => 'Y', 'USER_LOAD' => 'N', 'USE_SMILES' => $bSmiles? 'Y': 'N', 'USE_TIME_ZONE' => $bTimeZone? 'Y': 'N' )); foreach ($ar['message'] as $data) { if (!isset($arRecent['chat'.$data['recipientId']])) { $arRecent['chat'.$data['recipientId']] = Array( 'TYPE' => IM_MESSAGE_GROUP, 'CHAT' => $ar['chat'] ); } $arRecent['chat'.$data['recipientId']]['MESSAGE'] = Array( 'id' => $data['id'], 'senderId' => $data['senderId'], 'date' => $data['date'], 'text' => $data['text'] ); $arRecent['chat'.$data['recipientId']]['COUNTER'] = $data['counter']; } } if (!empty($arRecent)) { sortByColumn( $arRecent, array( 'COUNTER' => array(SORT_NUMERIC, SORT_DESC), 'MESSAGE' => array(SORT_NUMERIC, SORT_DESC) ), array( 'COUNTER' => function($counter){ return !is_null($counter); }, 'MESSAGE' => function($recent){ return $recent['date']; } ), null, true ); } return $arRecent; }
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 function OnImFileUpload($arFields) { if (!CModule::IncludeModule("im")) { return false; } $chatId = $arFields['CHAT_ID']; $arChat = CIMChat::GetChatData(array('ID' => $chatId)); if ($arChat['chat'][$chatId]['type'] == IM_MESSAGE_PRIVATE) { $arFields["FROM_USER_ID"] = $arFields['FILE']['authorId']; foreach ($arChat['userInChat'][$chatId] as $userId) { if ($userId != $arFields["FROM_USER_ID"]) { $arFields["TO_USER_ID"] = $userId; } } $domain = CXMPPServer::GetDomain(); if (CXMPPUtility::IsBitrix24Mode()) { $domain = $_SERVER["HTTP_HOST"]; } $factory = CXMPPFactory::GetFactory(); return $factory->__SendMessage($arFields["FROM_USER_ID"], $arFields["TO_USER_ID"], $arFields["MESSAGE_ID"] > 1 ? $arFields["MESSAGE_ID"] : 1, IM_MESSAGE_PRIVATE, htmlspecialcharsbx(CTextParser::convert4mail(str_replace("#BR#", "\n", $arFields["MESSAGE_OUT"]))), $domain); } return true; }
public static function RecentList() { global $DB, $USER; $arRecent = CUserOptions::GetOption('IM', 'recentList2', null); if (is_null($arRecent)) { $arResult = CUserOptions::GetOption('IM', 'recentList', null); if (!is_null($arResult)) { unset($arResult[$GLOBALS['USER']->GetID()]); foreach ($arResult as $key => $value) { $arRecent[IM_MESSAGE_PRIVATE][$key] = $value; } } else { $arRecent = array(); } } if (!empty($arRecent)) { if (isset($arRecent[IM_MESSAGE_PRIVATE]) && !empty($arRecent[IM_MESSAGE_PRIVATE])) { $arUsers = array(); $arInsert = array(); $CIMMessage = new CIMMessage(false, array('HIDE_LINK' => 'Y')); $arMessages = $CIMMessage->GetLastSendMessage(array('TO_USER_ID' => array_keys($arRecent[IM_MESSAGE_PRIVATE]), 'ORDER' => 'ASC', 'LIMIT' => 30, 'USE_TIME_ZONE' => 'N')); foreach ($arMessages as $userId => $arMessage) { $arUsers[] = $userId; $arInsert[$userId] = array('USER_ID' => $USER->GetId(), 'ITEM_TYPE' => IM_MESSAGE_PRIVATE, 'ITEM_ID' => $userId, 'ITEM_MID' => $arMessage['id']); } if (!empty($arUsers)) { $strSql = "SELECT ITEM_ID FROM b_im_recent WHERE USER_ID = " . $USER->GetId() . " AND ITEM_TYPE = '" . IM_MESSAGE_PRIVATE . "' AND ITEM_ID IN (" . implode(',', $arUsers) . ")"; $dbRes = $DB->Query($strSql, false, "File: " . __FILE__ . "<br>Line: " . __LINE__); while ($arRes = $dbRes->Fetch()) { unset($arInsert[$arRes['ITEM_ID']]); } } foreach ($arInsert as $arAdd) { $DB->Add('b_im_recent', $arAdd); } } if (isset($arRecent[IM_MESSAGE_CHAT]) && !empty($arRecent[IM_MESSAGE_CHAT])) { $arChats = array(); $arInsert = array(); $CIMChat = new CIMChat(false, array('HIDE_LINK' => 'Y')); $arMessagesGroup = $CIMChat->GetLastSendMessage(array('ID' => array_keys($arRecent[IM_MESSAGE_CHAT]), 'ORDER' => 'ASC', 'LIMIT' => 30, 'USE_TIME_ZONE' => 'N')); foreach ($arMessagesGroup as $chatId => $arMessage) { $arChats[] = $chatId; $arInsert[$chatId] = array('USER_ID' => $USER->GetId(), 'ITEM_TYPE' => IM_MESSAGE_CHAT, 'ITEM_ID' => $chatId, 'ITEM_MID' => $arMessage['id']); } if (!empty($arChats)) { $strSql = "\n\t\t\t\t\t\tSELECT\n\t\t\t\t\t\t\tITEM_ID\n\t\t\t\t\t\tFROM\n\t\t\t\t\t\t\tb_im_recent\n\t\t\t\t\t\tWHERE\n\t\t\t\t\t\t\tUSER_ID = " . $USER->GetId() . "\n\t\t\t\t\t\t\tAND ITEM_TYPE = '" . IM_MESSAGE_CHAT . "'\n\t\t\t\t\t\t\tAND ITEM_ID IN (" . implode(',', $arChats) . ")\n\t\t\t\t\t"; $dbRes = $DB->Query($strSql, false, "File: " . __FILE__ . "<br>Line: " . __LINE__); while ($arRes = $dbRes->Fetch()) { unset($arInsert[$arRes['ITEM_ID']]); } } foreach ($arInsert as $arAdd) { $DB->Add('b_im_recent', $arAdd); } } CUserOptions::SetOption('IM', 'recentList2', array()); } else { CUserOptions::SetOption('IM', 'recentList2', array()); } return true; }