Exemplo n.º 1
0
 function GetLastMessage($toChatId, $fromUserId = false, $loadExtraData = false, $bTimeZone = true, $limit = true)
 {
     global $DB;
     $fromUserId = IntVal($fromUserId);
     if ($fromUserId <= 0) {
         $fromUserId = $this->user_id;
     }
     $toChatId = IntVal($toChatId);
     if ($toChatId <= 0) {
         $GLOBALS["APPLICATION"]->ThrowException(GetMessage("IM_ERROR_EMPTY_CHAT_ID"), "ERROR_TO_CHAT_ID");
         return false;
     }
     $orm = IM\ChatTable::getById($toChatId);
     if (!($chatData = $orm->fetch())) {
         $GLOBALS["APPLICATION"]->ThrowException(GetMessage("IM_ERROR_CHAT_NOT_EXISTS"), "ERROR_CHAT_NOT_EXISTS");
         return false;
     }
     if ($chatData['TYPE'] == IM_MESSAGE_OPEN && !CIMMessenger::CheckEnableOpenChat()) {
         $GLOBALS["APPLICATION"]->ThrowException(GetMessage("IM_ERROR_CHAT_NOT_EXISTS"), "ERROR_CHAT_NOT_EXISTS");
         return false;
     }
     if ($limit) {
         $dbType = strtolower($DB->type);
         if ($dbType == "mysql") {
             $sqlLimit = " AND M.DATE_CREATE > DATE_SUB(NOW(), INTERVAL 30 DAY)";
         } else {
             if ($dbType == "mssql") {
                 $sqlLimit = " AND M.DATE_CREATE > dateadd(day, -30, getdate())";
             } else {
                 if ($dbType == "oracle") {
                     $sqlLimit = " AND M.DATE_CREATE > SYSDATE-30";
                 }
             }
         }
     }
     $limitById = '';
     $ar = \CIMChat::GetRelationById($toChatId, $fromUserId);
     if ($ar && $ar['START_ID'] > 0) {
         $limitById = 'AND M.ID >= ' . intval($ar['START_ID']);
     }
     if (!$bTimeZone) {
         CTimeZone::Disable();
     }
     if ($chatData['TYPE'] == IM_MESSAGE_OPEN) {
         $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\t\tC.TYPE CHAT_TYPE,\n\t\t\t\t\tR.USER_ID RID\n\t\t\t\tFROM b_im_message M\n\t\t\t\tINNER JOIN b_im_chat C ON C.ID = M.CHAT_ID AND C.TYPE = '" . IM_MESSAGE_OPEN . "'\n\t\t\t\tLEFT JOIN b_im_relation R ON R.CHAT_ID = M.CHAT_ID AND R.USER_ID = " . $fromUserId . "\n\t\t\t\tWHERE\n\t\t\t\t\tM.CHAT_ID = " . $toChatId . "\n\t\t\t\t\t" . $limitById . "\n\t\t\t\t\t#LIMIT#\n\t\t\t\tORDER BY M.DATE_CREATE DESC\n\t\t\t";
     } else {
         $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\t\tC.TYPE CHAT_TYPE,\n\t\t\t\t\tR1.USER_ID RID\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\tINNER JOIN b_im_chat C ON C.ID = M.CHAT_ID AND C.TYPE = '" . IM_MESSAGE_CHAT . "'\n\t\t\t\tWHERE\n\t\t\t\t\tR1.CHAT_ID = " . $toChatId . " AND R1.USER_ID = " . $fromUserId . "\n\t\t\t\t\t" . $limitById . "\n\t\t\t\t\t#LIMIT#\n\t\t\t\tORDER BY M.DATE_CREATE DESC\n\t\t\t";
     }
     $strSql = $DB->TopSql($strSql, 20);
     if (!$bTimeZone) {
         CTimeZone::Enable();
     }
     //LEFT JOIN b_im_message_param MP on MP.MESSAGE_ID = M.ID and MP.PARAM_NAME = 'FOR_USER_ID'
     //and (MP.PARAM_VALUE is null or MP.PARAM_VALUE = '".$fromUserId."')
     if ($limit) {
         $dbRes = $DB->Query(str_replace("#LIMIT#", $sqlLimit, $strSql), false, "File: " . __FILE__ . "<br>Line: " . __LINE__);
         if (!$dbRes->SelectedRowsCount()) {
             $dbRes = $DB->Query(str_replace("#LIMIT#", "", $strSql), false, "File: " . __FILE__ . "<br>Line: " . __LINE__);
         }
     } else {
         $dbRes = $DB->Query(str_replace("#LIMIT#", "", $strSql), false, "File: " . __FILE__ . "<br>Line: " . __LINE__);
     }
     CIMStatus::Set($fromUserId, array('IDLE' => null));
     $chatType = $chatData['TYPE'];
     $chatRelationUserId = 0;
     $arMessages = array();
     $arMessageId = array();
     $arUsersMessage = array();
     $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()) {
         $arRes['CHAT_TYPE'] = trim($arRes['CHAT_TYPE']);
         $chatType = $arRes['CHAT_TYPE'];
         $chatRelationUserId = intval($arRes['RID']);
         $arMessages[$arRes['ID']] = array('id' => $arRes['ID'], 'chatId' => $arRes['CHAT_ID'], 'senderId' => $arRes['AUTHOR_ID'], 'recipientId' => $arRes['CHAT_ID'], 'date' => $arRes['DATE_CREATE'], 'text' => $CCTP->convertText(htmlspecialcharsbx($arRes['MESSAGE'])));
         $arMessageId[] = $arRes['ID'];
         $arUsersMessage[$arRes['CHAT_ID']][] = $arRes['ID'];
     }
     if ($chatType == IM_MESSAGE_OPEN && $chatRelationUserId <= 0) {
         if (IM\User::getInstance($fromUserId)->isExtranet()) {
             $arMessages = array();
             $arMessageId = array();
             $arUsersMessage = array();
             $loadExtraData = false;
         } else {
             if (CModule::IncludeModule('pull')) {
                 CPullWatch::Add($fromUserId, 'IM_PUBLIC_' . $toChatId, true);
             }
         }
     }
     $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;
             }
         }
         unset($arMessages[$messageId]['params']['URL_ID']);
     }
     $arChatFiles = CIMDisk::GetFiles($toChatId, $arFiles);
     $arMessages = CIMMessageLink::prepareShow($arMessages, $params);
     $arResult = array('chatId' => $toChatId, 'message' => $arMessages, 'usersMessage' => $arUsersMessage, 'users' => array(), 'userInGroup' => array(), 'woUserInGroup' => array(), 'files' => $arChatFiles);
     if (is_array($loadExtraData) || is_bool($loadExtraData) && $loadExtraData == true) {
         $bDepartment = true;
         if (is_array($loadExtraData) && $loadExtraData['DEPARTMENT'] == 'N') {
             $bDepartment = false;
         }
         $arChat = self::GetChatData(array('ID' => $toChatId, 'USE_CACHE' => 'N'));
         if ($arChat['chat'][$toChatId]['messageType'] == IM_MESSAGE_OPEN || in_array($fromUserId, $arChat['userInChat'][$toChatId])) {
             $arResult['userInChat'] = $arChat['userInChat'];
             $arResult['userChatBlockStatus'] = $arChat['userChatBlockStatus'];
             $ar = CIMContactList::GetUserData(array('ID' => $arChat['userInChat'][$toChatId], 'DEPARTMENT' => $bDepartment ? 'Y' : 'N', 'USE_CACHE' => 'N'));
             $arResult['users'] = $ar['users'];
             $arResult['userInGroup'] = $ar['userInGroup'];
             $arResult['woUserInGroup'] = $ar['woUserInGroup'];
             if ($arChat['chat'][$toChatId]['extranet'] === "") {
                 $isExtranet = false;
                 foreach ($ar['users'] as $userData) {
                     if ($userData['extranet']) {
                         $isExtranet = true;
                         break;
                     }
                 }
                 IM\ChatTable::update($toChatId, array('EXTRANET' => $isExtranet ? "Y" : "N"));
                 $arChat['chat'][$toChatId]['extranet'] = $isExtranet;
             }
             $arResult['chat'] = $arChat['chat'];
         }
     }
     return $arResult;
 }
Exemplo n.º 2
0
             $ar = array("FROM_USER_ID" => intval($USER->GetID()), "TO_USER_ID" => intval($_POST['RECIPIENT_ID']), "MESSAGE" => $_POST['MESSAGE']);
             $insertID = CIMMessage::Add($ar);
         } else {
             $errorMessage = GetMessage('IM_ACCESS_ERROR');
         }
     }
     if (!$insertID && !$errorMessage) {
         if ($e = $GLOBALS["APPLICATION"]->GetException()) {
             $errorMessage = $e->GetString();
         }
         if (StrLen($errorMessage) == 0) {
             $errorMessage = GetMessage('IM_UNKNOWN_ERROR');
         }
     }
     $params = CIMMessageParam::Get(array($insertID));
     $arMessages = CIMMessageLink::prepareShow(array(), $params);
     $CCTP = new CTextParser();
     $CCTP->MaxStringLen = 200;
     $CCTP->allow = array("HTML" => "N", "ANCHOR" => isset($_POST['MOBILE']) ? "N" : "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");
     $userTzOffset = isset($_POST['USER_TZ_OFFSET']) ? intval($_POST['USER_TZ_OFFSET']) : CTimeZone::GetOffset();
     $arResult = array('TMP_ID' => $tmpID, 'ID' => $insertID, 'SEND_DATE' => time() + $userTzOffset, 'SEND_MESSAGE' => $CCTP->convertText(htmlspecialcharsbx($ar['MESSAGE'])), 'SEND_MESSAGE_PARAMS' => $arMessages[$insertID]['params'], 'SENDER_ID' => intval($USER->GetID()), 'RECIPIENT_ID' => $_POST['CHAT'] == 'Y' ? htmlspecialcharsbx($_POST['RECIPIENT_ID']) : intval($_POST['RECIPIENT_ID']), 'ERROR' => $errorMessage);
     if (isset($_POST['MOBILE'])) {
         $arFormat = array("today" => "today, " . GetMessage('IM_MESSAGE_FORMAT_TIME'), "" => GetMessage('IM_MESSAGE_FORMAT_DATE'));
         $arResult['SEND_DATE_FORMAT'] = FormatDate($arFormat, time() + $userTzOffset);
     }
     echo CUtil::PhpToJsObject($arResult);
     CIMContactList::SetOnline();
     CIMMessenger::SetCurrentTab($_POST['TAB']);
 } else {
     if ($_POST['IM_EDIT_MESSAGE'] == 'Y') {
         CUtil::decodeURIComponent($_POST);
Exemplo n.º 3
0
 public static function Add($arFields)
 {
     global $DB;
     if (isset($arFields['TITLE']) && !isset($arFields['NOTIFY_TITLE'])) {
         $arFields['NOTIFY_TITLE'] = $arFields['TITLE'];
     }
     if (isset($arFields['NOTIFY_MESSAGE']) && !isset($arFields['MESSAGE'])) {
         $arFields['MESSAGE'] = $arFields['NOTIFY_MESSAGE'];
     }
     if (isset($arFields['NOTIFY_MESSAGE_OUT']) && !isset($arFields['MESSAGE_OUT'])) {
         $arFields['MESSAGE_OUT'] = $arFields['NOTIFY_MESSAGE_OUT'];
     }
     if (isset($arFields['MESSAGE'])) {
         $arFields['MESSAGE'] = trim($arFields['MESSAGE']);
     }
     $arFields['MESSAGE_OUT'] = isset($arFields['MESSAGE_OUT']) ? trim($arFields['MESSAGE_OUT']) : "";
     $bConvert = false;
     if (isset($arFields['CONVERT']) && $arFields['CONVERT'] == 'Y') {
         $bConvert = true;
     }
     if (!isset($arFields['MESSAGE_TYPE'])) {
         $arFields['MESSAGE_TYPE'] = "";
     }
     if (!isset($arFields['NOTIFY_MODULE'])) {
         $arFields['NOTIFY_MODULE'] = 'im';
     }
     if (!isset($arFields['NOTIFY_EVENT'])) {
         $arFields['NOTIFY_EVENT'] = 'default';
     }
     if (!isset($arFields['PARAMS'])) {
         $arFields['PARAMS'] = array();
     }
     if (isset($arFields['ATTACH']) || isset($arFields['PARAMS']['ATTACH'])) {
         $attach = isset($arFields['ATTACH']) ? $arFields['ATTACH'] : $arFields['PARAMS']['ATTACH'];
         if (is_object($attach)) {
             $arFields['PARAMS']['ATTACH'] = array($attach);
         } else {
             if (is_array($attach)) {
                 $arFields['PARAMS']['ATTACH'] = $attach;
             } else {
                 $arFields['PARAMS']['ATTACH'] = array();
             }
         }
     }
     if (isset($arFields['FOR_USER_ID'])) {
         $arFields['PARAMS']['FOR_USER_ID'] = $arFields['FOR_USER_ID'];
     }
     $arFields['URL_ATTACH'] = array();
     if ($arFields['MESSAGE_TYPE'] == IM_MESSAGE_SYSTEM) {
         if (!isset($arFields['NOTIFY_TYPE']) && intval($arFields['FROM_USER_ID']) > 0) {
             $arFields['NOTIFY_TYPE'] = IM_NOTIFY_FROM;
         } else {
             if (!isset($arFields['NOTIFY_TYPE'])) {
                 $arFields['NOTIFY_TYPE'] = IM_NOTIFY_SYSTEM;
             }
         }
         if (isset($arFields['NOTIFY_ANSWER']) && $arFields['NOTIFY_ANSWER'] == 'Y') {
             $arFields['PARAMS']['CAN_ANSWER'] = 'Y';
         }
         /*
         $urlPrepare = self::PrepareUrl($arFields['MESSAGE']);
         if ($urlPrepare['RESULT'])
         {
         	if (empty($arFields['MESSAGE_OUT']))
         	{
         		$arFields['MESSAGE_OUT'] = $arFields['MESSAGE'];
         	}
         	$arFields['MESSAGE'] = $urlPrepare['MESSAGE'];
         	$arFields['PARAMS']['ATTACH'] = array_merge($arFields['PARAMS']['ATTACH'], $urlPrepare['ATTACH']);
         }
         */
     } else {
         $link = new CIMMessageLink();
         $urlPrepare = $link->prepareInsert($arFields['MESSAGE']);
         if ($urlPrepare['RESULT']) {
             if (empty($arFields['MESSAGE_OUT'])) {
                 $arFields['MESSAGE_OUT'] = $arFields['MESSAGE'];
             }
             $arFields['MESSAGE'] = $urlPrepare['MESSAGE'];
             if (isset($arFields['PARAMS']['URL_ID'])) {
                 $arFields['PARAMS']['URL_ID'] = array_merge($arFields['PARAMS']['URL_ID'], $urlPrepare['URL_ID']);
             } else {
                 $arFields['PARAMS']['URL_ID'] = $urlPrepare['URL_ID'];
             }
             $arFields['URL_ATTACH'] = $urlPrepare['ATTACH'];
         }
     }
     if (isset($arFields['NOTIFY_EMAIL_TEMPLATE']) && !isset($arFields['EMAIL_TEMPLATE'])) {
         $arFields['EMAIL_TEMPLATE'] = $arFields['NOTIFY_EMAIL_TEMPLATE'];
     }
     if (!isset($arFields['AUTHOR_ID'])) {
         $arFields['AUTHOR_ID'] = intval($arFields['FROM_USER_ID']);
     }
     foreach (GetModuleEvents("im", "OnBeforeMessageNotifyAdd", true) as $arEvent) {
         $result = ExecuteModuleEventEx($arEvent, array(&$arFields));
         if ($result === false || isset($result['result']) && $result['result'] === false) {
             if (isset($result['reason'])) {
                 $CBXSanitizer = new CBXSanitizer();
                 $CBXSanitizer->AddTags(array('a' => array('href', 'style', 'target'), 'b' => array(), 'u' => array(), 'i' => array(), 'br' => array(), 'span' => array('style')));
                 $reason = $CBXSanitizer->SanitizeHtml($result['reason']);
             } else {
                 if ($arFields['MESSAGE_TYPE'] == IM_MESSAGE_PRIVATE) {
                     $reason = GetMessage("IM_ERROR_MESSAGE_CANCELED");
                 } else {
                     if ($arFields['MESSAGE_TYPE'] == IM_MESSAGE_SYSTEM) {
                         $reason = GetMessage("IM_ERROR_NOTIFY_CANCELED");
                     } else {
                         $reason = GetMessage("IM_ERROR_GROUP_CANCELED");
                     }
                 }
             }
             $GLOBALS["APPLICATION"]->ThrowException($reason, "ERROR_FROM_OTHER_MODULE");
             return false;
         }
     }
     if (!self::CheckFields($arFields)) {
         return false;
     }
     if ($arFields['MESSAGE_TYPE'] == IM_MESSAGE_PRIVATE) {
         if (isset($arFields['TO_CHAT_ID'])) {
             $chatId = $arFields['TO_CHAT_ID'];
             $arRel = CIMChat::GetRelationById($chatId);
             foreach ($arRel as $rel) {
                 if ($rel['USER_ID'] == $arFields['FROM_USER_ID']) {
                     continue;
                 }
                 $arFields['TO_USER_ID'] = $rel['USER_ID'];
             }
             if (!IsModuleInstalled('intranet')) {
                 if (CIMSettings::GetPrivacy(CIMSettings::PRIVACY_MESSAGE) == CIMSettings::PRIVACY_RESULT_CONTACT && CModule::IncludeModule('socialnetwork') && CSocNetUser::IsFriendsAllowed() && !CSocNetUserRelations::IsFriends($arFields['FROM_USER_ID'], $arFields['TO_USER_ID'])) {
                     $GLOBALS["APPLICATION"]->ThrowException(GetMessage('IM_ERROR_MESSAGE_PRIVACY_SELF'), "ERROR_FROM_PRIVACY_SELF");
                     return false;
                 } else {
                     if (CIMSettings::GetPrivacy(CIMSettings::PRIVACY_MESSAGE, $arFields['TO_USER_ID']) == CIMSettings::PRIVACY_RESULT_CONTACT && CModule::IncludeModule('socialnetwork') && CSocNetUser::IsFriendsAllowed() && !CSocNetUserRelations::IsFriends($arFields['FROM_USER_ID'], $arFields['TO_USER_ID'])) {
                         $GLOBALS["APPLICATION"]->ThrowException(GetMessage('IM_ERROR_MESSAGE_PRIVACY'), "ERROR_FROM_PRIVACY");
                         return false;
                     }
                 }
             }
         } else {
             $arFields['FROM_USER_ID'] = intval($arFields['FROM_USER_ID']);
             $arFields['TO_USER_ID'] = intval($arFields['TO_USER_ID']);
             if (!IsModuleInstalled('intranet')) {
                 if (CIMSettings::GetPrivacy(CIMSettings::PRIVACY_MESSAGE) == CIMSettings::PRIVACY_RESULT_CONTACT && CModule::IncludeModule('socialnetwork') && CSocNetUser::IsFriendsAllowed() && !CSocNetUserRelations::IsFriends($arFields['FROM_USER_ID'], $arFields['TO_USER_ID'])) {
                     $GLOBALS["APPLICATION"]->ThrowException(GetMessage('IM_ERROR_MESSAGE_PRIVACY_SELF'), "ERROR_FROM_PRIVACY_SELF");
                     return false;
                 } else {
                     if (CIMSettings::GetPrivacy(CIMSettings::PRIVACY_MESSAGE, $arFields['TO_USER_ID']) == CIMSettings::PRIVACY_RESULT_CONTACT && CModule::IncludeModule('socialnetwork') && CSocNetUser::IsFriendsAllowed() && !CSocNetUserRelations::IsFriends($arFields['FROM_USER_ID'], $arFields['TO_USER_ID'])) {
                         $GLOBALS["APPLICATION"]->ThrowException(GetMessage('IM_ERROR_MESSAGE_PRIVACY'), "ERROR_FROM_PRIVACY");
                         return false;
                     }
                 }
             }
             $chatId = CIMMessage::GetChatId($arFields['FROM_USER_ID'], $arFields['TO_USER_ID']);
         }
         if ($chatId > 0) {
             $arParams = array();
             $arParams['CHAT_ID'] = $chatId;
             $arParams['AUTHOR_ID'] = intval($arFields['AUTHOR_ID']);
             $arParams['MESSAGE'] = $arFields['MESSAGE'];
             $arParams['MESSAGE_OUT'] = $arFields['MESSAGE_OUT'];
             $arParams['NOTIFY_MODULE'] = $arFields['NOTIFY_MODULE'];
             $arParams['NOTIFY_EVENT'] = $arFields['SYSTEM'] == 'Y' ? 'private_system' : 'private';
             if (isset($arFields['IMPORT_ID'])) {
                 $arParams['IMPORT_ID'] = intval($arFields['IMPORT_ID']);
             }
             if (isset($arFields['MESSAGE_DATE'])) {
                 $arParams['DATE_CREATE'] = $arFields['MESSAGE_DATE'];
             }
             $arFiles = array();
             $arFields['FILES'] = array();
             if (isset($arFields['PARAMS']['FILE_ID'])) {
                 foreach ($arFields['PARAMS']['FILE_ID'] as $fileId) {
                     $arFiles[$fileId] = $fileId;
                 }
             }
             $arFields['FILES'] = CIMDisk::GetFiles($chatId, $arFiles);
             $messageFiles = self::GetFormatFilesMessageOut($arFields['FILES']);
             if (strlen($messageFiles) > 0) {
                 $arParams['MESSAGE_OUT'] = strlen($arParams['MESSAGE_OUT']) > 0 ? $arParams['MESSAGE_OUT'] . "\n" . $messageFiles : $messageFiles;
                 $arFields['MESSAGE_OUT'] = $arParams['MESSAGE_OUT'];
             }
             $result = IM\MessageTable::add($arParams);
             $messageID = IntVal($result->getId());
             if ($messageID <= 0) {
                 return false;
             }
             IM\ChatTable::update($chatId, array('LAST_MESSAGE_ID' => $messageID));
             if (!empty($arFields['PARAMS'])) {
                 CIMMessageParam::Set($messageID, $arFields['PARAMS']);
             }
             if (!empty($arFields['URL_ATTACH'])) {
                 if (isset($arFields['PARAMS']['ATTACH'])) {
                     $arFields['PARAMS']['ATTACH'] = array_merge($arFields['PARAMS']['ATTACH'], $arFields['URL_ATTACH']);
                 } else {
                     $arFields['PARAMS']['ATTACH'] = $arFields['URL_ATTACH'];
                 }
             }
             //CUserCounter::Increment($arFields['TO_USER_ID'], 'im_message_v2', '**', false);
             CIMContactList::SetRecent(array('ENTITY_ID' => $arFields['TO_USER_ID'], 'MESSAGE_ID' => $messageID, 'CHAT_TYPE' => IM_MESSAGE_PRIVATE, 'USER_ID' => $arFields['FROM_USER_ID']));
             CIMContactList::SetRecent(array('ENTITY_ID' => $arFields['FROM_USER_ID'], 'MESSAGE_ID' => $messageID, 'CHAT_TYPE' => IM_MESSAGE_PRIVATE, 'USER_ID' => $arFields['TO_USER_ID']));
             CIMStatus::SetIdle($arFields['FROM_USER_ID'], false);
             if (!$bConvert) {
                 $arRel = CIMChat::GetRelationById($chatId);
                 foreach ($arRel as $relation) {
                     if ($relation["USER_ID"] == $arFields["TO_USER_ID"]) {
                         if ($relation['STATUS'] != IM_STATUS_UNREAD) {
                             IM\RelationTable::update($relation["ID"], array("STATUS" => IM_STATUS_UNREAD));
                         }
                     } else {
                         IM\RelationTable::update($relation["ID"], array("STATUS" => IM_STATUS_READ, "LAST_ID" => $messageID, "LAST_SEND_ID" => $messageID, "LAST_READ" => new Bitrix\Main\Type\DateTime()));
                     }
                 }
                 if (CModule::IncludeModule("pull")) {
                     $arParams['FROM_USER_ID'] = $arFields['FROM_USER_ID'];
                     $arParams['TO_USER_ID'] = $arFields['TO_USER_ID'];
                     $pullMessage = array('module_id' => 'im', 'command' => 'message', 'params' => CIMMessage::GetFormatMessage(array('ID' => $messageID, 'CHAT_ID' => $chatId, 'TO_USER_ID' => $arParams['TO_USER_ID'], 'FROM_USER_ID' => $arParams['FROM_USER_ID'], 'SYSTEM' => $arFields['SYSTEM'] == 'Y' ? 'Y' : 'N', 'MESSAGE' => $arParams['MESSAGE'], 'DATE_CREATE' => time(), 'PARAMS' => self::PrepareParamsForPull($arFields['PARAMS']), 'FILES' => $arFields['FILES'])));
                     $pullMessageTo = $pullMessage;
                     if (CPullOptions::GetPushStatus() && (!isset($arFields['PUSH']) || $arFields['PUSH'] == 'Y')) {
                         if (CIMSettings::GetNotifyAccess($arParams["TO_USER_ID"], 'im', 'message', CIMSettings::CLIENT_PUSH)) {
                             $pushParams = self::PreparePushForPrivate(array('FROM_USER_ID' => $arParams['FROM_USER_ID'], 'MESSAGE' => $arParams['MESSAGE'], 'SYSTEM' => $arFields['SYSTEM'], 'FILES' => $arFields['FILES'], 'ATTACH' => isset($arFields['PARAMS']['ATTACH']) ? true : false));
                             $pullMessageTo = array_merge($pullMessage, $pushParams);
                         }
                     }
                     CPullStack::AddByUser($arParams['TO_USER_ID'], $pullMessageTo);
                     CPullStack::AddByUser($arParams['FROM_USER_ID'], $pullMessage);
                     CPushManager::DeleteFromQueueBySubTag($arParams['FROM_USER_ID'], 'IM_MESS');
                     //self::SendBadges($arParams['TO_USER_ID']);
                 }
                 foreach (GetModuleEvents("im", "OnAfterMessagesAdd", true) as $arEvent) {
                     ExecuteModuleEventEx($arEvent, array(intval($messageID), $arFields));
                 }
             }
             return $messageID;
         } else {
             $GLOBALS["APPLICATION"]->ThrowException(GetMessage("IM_ERROR_MESSAGE_CREATE"), "CHAT_ID");
             return false;
         }
     } else {
         if ($arFields['MESSAGE_TYPE'] == IM_MESSAGE_CHAT || $arFields['MESSAGE_TYPE'] == IM_MESSAGE_OPEN) {
             $arFields['FROM_USER_ID'] = intval($arFields['FROM_USER_ID']);
             $chatId = 0;
             $systemMessage = false;
             if (isset($arFields['SYSTEM']) && $arFields['SYSTEM'] == 'Y') {
                 $strSql = "\n\t\t\t\t\tSELECT C.ID CHAT_ID, C.TITLE CHAT_TITLE, C.AUTHOR_ID CHAT_AUTHOR_ID, C.TYPE CHAT_TYPE, '1' RID\n\t\t\t\t\tFROM b_im_chat C\n\t\t\t\t\tWHERE C.ID = " . intval($arFields['TO_CHAT_ID']) . "\n\t\t\t\t";
                 $systemMessage = true;
             } else {
                 $strSql = "\n\t\t\t\t\tSELECT C.ID CHAT_ID, C.TITLE CHAT_TITLE, C.AUTHOR_ID CHAT_AUTHOR_ID, C.TYPE CHAT_TYPE, R.USER_ID RID\n\t\t\t\t\tFROM b_im_chat C\n\t\t\t\t\tLEFT JOIN b_im_relation R ON R.CHAT_ID = C.ID AND R.USER_ID = " . $arFields['FROM_USER_ID'] . "\n\t\t\t\t\tWHERE C.ID = " . intval($arFields['TO_CHAT_ID']) . "\n\t\t\t\t";
             }
             $dbRes = $DB->Query($strSql, false, "File: " . __FILE__ . "<br>Line: " . __LINE__);
             if ($arRes = $dbRes->Fetch()) {
                 $chatId = intval($arRes['CHAT_ID']);
                 $chatTitle = htmlspecialcharsbx($arRes['CHAT_TITLE']);
                 $chatAuthorId = intval($arRes['CHAT_AUTHOR_ID']);
                 $arRes['CHAT_TYPE'] = trim($arRes['CHAT_TYPE']);
                 $arFields['MESSAGE_TYPE'] = $arRes['CHAT_TYPE'];
                 if ($arRes['CHAT_TYPE'] == IM_MESSAGE_OPEN) {
                     if (!CIMMessenger::CheckEnableOpenChat()) {
                         $GLOBALS["APPLICATION"]->ThrowException(GetMessage("IM_ERROR_GROUP_CANCELED"), "CANCELED");
                         return false;
                     } else {
                         if (intval($arRes['RID']) <= 0) {
                             if (IM\User::getInstance($arFields['FROM_USER_ID'])->isExtranet()) {
                                 $GLOBALS["APPLICATION"]->ThrowException(GetMessage("IM_ERROR_GROUP_CANCELED"), "CANCELED");
                                 return false;
                             } else {
                                 $chat = new CIMChat(0);
                                 $chat->AddUser($chatId, $arFields['FROM_USER_ID']);
                             }
                         }
                     }
                 } else {
                     if (intval($arRes['RID']) <= 0) {
                         $GLOBALS["APPLICATION"]->ThrowException(GetMessage("IM_ERROR_GROUP_CANCELED"), "CANCELED");
                         return false;
                     }
                 }
             } else {
                 $GLOBALS["APPLICATION"]->ThrowException(GetMessage("IM_ERROR_GROUP_CANCELED"), "CANCELED");
                 return false;
             }
             if ($chatId > 0) {
                 $arParams = array();
                 $arParams['CHAT_ID'] = $chatId;
                 $arParams['AUTHOR_ID'] = $systemMessage ? 0 : intval($arFields['AUTHOR_ID']);
                 $arParams['MESSAGE'] = $arFields['MESSAGE'];
                 $arParams['MESSAGE_OUT'] = $arFields['MESSAGE_OUT'];
                 $arParams['NOTIFY_MODULE'] = 'im';
                 $arParams['NOTIFY_EVENT'] = 'group';
                 if (isset($arFields['MESSAGE_DATE'])) {
                     $arParams['DATE_CREATE'] = $arFields['MESSAGE_DATE'];
                 }
                 $arFiles = array();
                 $arFields['FILES'] = array();
                 if (isset($arFields['PARAMS']['FILE_ID'])) {
                     foreach ($arFields['PARAMS']['FILE_ID'] as $fileId) {
                         $arFiles[$fileId] = $fileId;
                     }
                 }
                 $arFields['FILES'] = CIMDisk::GetFiles($chatId, $arFiles);
                 $messageFiles = self::GetFormatFilesMessageOut($arFields['FILES']);
                 if (strlen($messageFiles) > 0) {
                     $arParams['MESSAGE_OUT'] = strlen($arParams['MESSAGE_OUT']) > 0 ? $arParams['MESSAGE_OUT'] . "\n" . $messageFiles : $messageFiles;
                     $arFields['MESSAGE_OUT'] = $arParams['MESSAGE_OUT'];
                 }
                 $result = IM\MessageTable::add($arParams);
                 $messageID = IntVal($result->getId());
                 if ($messageID <= 0) {
                     return false;
                 }
                 IM\ChatTable::update($chatId, array('LAST_MESSAGE_ID' => $messageID));
                 if (!empty($arFields['PARAMS'])) {
                     CIMMessageParam::Set($messageID, $arFields['PARAMS']);
                 }
                 if (!empty($arFields['URL_ATTACH'])) {
                     if (isset($arFields['PARAMS']['ATTACH'])) {
                         $arFields['PARAMS']['ATTACH'] = array_merge($arFields['PARAMS']['ATTACH'], $arFields['URL_ATTACH']);
                     } else {
                         $arFields['PARAMS']['ATTACH'] = $arFields['URL_ATTACH'];
                     }
                 }
                 //$sqlCounter = "SELECT USER_ID as ID, 1 as CNT, '**' as SITE_ID, 'im_chat_v2' as CODE, 1 as SENT
                 //				FROM b_im_relation R1
                 //				WHERE CHAT_ID = ".$chatId." AND USER_ID <> ".$arFields['FROM_USER_ID'];
                 //CUserCounter::IncrementWithSelect($sqlCounter, false);
                 $arRel = CIMChat::GetRelationById($chatId);
                 foreach ($arRel as $relation) {
                     CIMContactList::SetRecent(array('ENTITY_ID' => $chatId, 'MESSAGE_ID' => $messageID, 'CHAT_TYPE' => $arFields['MESSAGE_TYPE'], 'USER_ID' => $relation['USER_ID']));
                     if ($relation["USER_ID"] == $arFields["FROM_USER_ID"]) {
                         IM\RelationTable::update($relation["ID"], array("STATUS" => IM_STATUS_READ, "LAST_ID" => $messageID, "LAST_SEND_ID" => $messageID, "LAST_READ" => new Bitrix\Main\Type\DateTime()));
                     } else {
                         if ($relation['STATUS'] != IM_STATUS_UNREAD) {
                             IM\RelationTable::update($relation["ID"], array("STATUS" => IM_STATUS_UNREAD));
                         }
                     }
                 }
                 CIMStatus::SetIdle($arFields['FROM_USER_ID'], false);
                 if (CModule::IncludeModule("pull")) {
                     $arParams['FROM_USER_ID'] = $arFields['FROM_USER_ID'];
                     $arParams['TO_CHAT_ID'] = $arFields['TO_CHAT_ID'];
                     $pullMessage = array('module_id' => 'im', 'command' => 'messageChat', 'params' => CIMMessage::GetFormatMessage(array('ID' => $messageID, 'CHAT_ID' => $chatId, 'TO_CHAT_ID' => $arParams['TO_CHAT_ID'], 'FROM_USER_ID' => $arParams['FROM_USER_ID'], 'MESSAGE' => $arParams['MESSAGE'], 'SYSTEM' => $arFields['SYSTEM'] == 'Y' ? 'Y' : 'N', 'DATE_CREATE' => time(), 'PARAMS' => self::PrepareParamsForPull($arFields['PARAMS']), 'FILES' => $arFields['FILES'])));
                     if (CPullOptions::GetPushStatus() && (!isset($arFields['PUSH']) || $arFields['PUSH'] == 'Y')) {
                         $pushParams = self::PreparePushForChat(array('CHAT_ID' => $chatId, 'CHAT_TITLE' => $chatTitle, 'FROM_USER_ID' => $arParams['FROM_USER_ID'], 'MESSAGE' => $arParams['MESSAGE'], 'SYSTEM' => $arFields['SYSTEM'], 'FILES' => $arFields['FILES'], 'ATTACH' => isset($arFields['PARAMS']['ATTACH']) ? true : false));
                         $pullMessage = array_merge($pullMessage, $pushParams);
                     }
                     $pullUsers = array();
                     $pullUsersSkip = array();
                     foreach ($arRel as $rel) {
                         $pullUsers[] = $rel['USER_ID'];
                         if ($rel['USER_ID'] == $arParams['FROM_USER_ID']) {
                             $pullUsersSkip[] = $rel['USER_ID'];
                             CPushManager::DeleteFromQueueBySubTag($arParams['FROM_USER_ID'], 'IM_MESS');
                         } else {
                             if ($rel['NOTIFY_BLOCK'] == 'Y' || !CIMSettings::GetNotifyAccess($rel['USER_ID'], 'im', $arFields['MESSAGE_TYPE'] == IM_MESSAGE_OPEN ? 'openChat' : 'chat', CIMSettings::CLIENT_PUSH)) {
                                 $pullUsersSkip[] = $rel['USER_ID'];
                             }
                         }
                     }
                     $pullMessage['push']['skip_users'] = $pullUsersSkip;
                     CPullStack::AddByUsers($pullUsers, $pullMessage);
                     if ($arRes['CHAT_TYPE'] == IM_MESSAGE_OPEN) {
                         $pullMessageToWatch = $pullMessage;
                         unset($pullMessageToWatch['push']);
                         CPullWatch::AddToStack('IM_PUBLIC_' . $chatId, $pullMessageToWatch);
                     }
                     self::SendMention(array('CHAT_ID' => $chatId, 'CHAT_TITLE' => $chatTitle, 'CHAT_RELATION' => $arRel, 'CHAT_TYPE' => $arFields['MESSAGE_TYPE'], 'MESSAGE' => $arParams['MESSAGE'], 'FILES' => $arFields['FILES'], 'FROM_USER_ID' => $arParams['FROM_USER_ID']));
                     //self::SendBadges($usersForBadges);
                 }
                 foreach (GetModuleEvents("im", "OnAfterMessagesAdd", true) as $arEvent) {
                     ExecuteModuleEventEx($arEvent, array(intval($messageID), $arFields));
                 }
                 return $messageID;
             } else {
                 $GLOBALS["APPLICATION"]->ThrowException(GetMessage("IM_ERROR_MESSAGE_CREATE"), "CHAT_ID");
                 return false;
             }
         } else {
             if ($arFields['MESSAGE_TYPE'] == IM_MESSAGE_SYSTEM) {
                 $arFields['TO_USER_ID'] = intval($arFields['TO_USER_ID']);
                 $orm = \Bitrix\Main\UserTable::getById($arFields['TO_USER_ID']);
                 $userData = $orm->fetch();
                 if (!$userData || $userData['ACTIVE'] == 'N' || $userData['EXTERNAL_AUTH_ID'] == 'email') {
                     $GLOBALS["APPLICATION"]->ThrowException(GetMessage("IM_ERROR_MESSAGE_CREATE"), "TO_USER_ID");
                     return false;
                 }
                 $chatId = 0;
                 $strSql = "\n\t\t\t\tSELECT C.ID CHAT_ID\n\t\t\t\tFROM b_im_chat C\n\t\t\t\tWHERE C.AUTHOR_ID = " . $arFields['TO_USER_ID'] . " AND C.TYPE = '" . IM_MESSAGE_SYSTEM . "'\n\t\t\t";
                 $dbRes = $DB->Query($strSql, false, "File: " . __FILE__ . "<br>Line: " . __LINE__);
                 if ($arRes = $dbRes->Fetch()) {
                     $chatId = intval($arRes['CHAT_ID']);
                 } else {
                     $result = IM\ChatTable::add(array('TYPE' => IM_MESSAGE_SYSTEM, 'AUTHOR_ID' => $arFields['TO_USER_ID']));
                     $chatId = $result->getId();
                     if ($chatId <= 0) {
                         $GLOBALS["APPLICATION"]->ThrowException(GetMessage("IM_ERROR_MESSAGE_CREATE"), "CHAT_ID");
                         return false;
                     }
                     IM\RelationTable::add(array("CHAT_ID" => $chatId, "MESSAGE_TYPE" => IM_MESSAGE_SYSTEM, "USER_ID" => intval($arFields['TO_USER_ID']), "STATUS" => $bConvert ? 2 : 0));
                 }
                 if ($chatId > 0) {
                     $arParams = array();
                     $arParams['CHAT_ID'] = $chatId;
                     $arParams['AUTHOR_ID'] = intval($arFields['AUTHOR_ID']);
                     $arParams['MESSAGE'] = $arFields['MESSAGE'];
                     $arParams['MESSAGE_OUT'] = $arFields['MESSAGE_OUT'];
                     $arParams['NOTIFY_TYPE'] = intval($arFields['NOTIFY_TYPE']);
                     $arParams['NOTIFY_MODULE'] = $arFields['NOTIFY_MODULE'];
                     $arParams['NOTIFY_EVENT'] = $arFields['NOTIFY_EVENT'];
                     //if (strlen($arParams['MESSAGE']) <= 0 && strlen($arParams['MESSAGE_OUT']) <= 0)
                     //	return false;
                     $sendToSite = true;
                     if ($arParams['NOTIFY_TYPE'] != IM_NOTIFY_CONFIRM) {
                         $sendToSite = CIMSettings::GetNotifyAccess($arFields["TO_USER_ID"], $arFields["NOTIFY_MODULE"], $arFields["NOTIFY_EVENT"], CIMSettings::CLIENT_SITE);
                     }
                     if (!$sendToSite) {
                         $arParams['NOTIFY_READ'] = 'Y';
                     }
                     if (isset($arFields['IMPORT_ID'])) {
                         $arParams['IMPORT_ID'] = intval($arFields['IMPORT_ID']);
                     }
                     if (isset($arFields['MESSAGE_DATE'])) {
                         $arParams['DATE_CREATE'] = $arFields['MESSAGE_DATE'];
                     }
                     if (isset($arFields['EMAIL_TEMPLATE']) && strlen(trim($arFields['EMAIL_TEMPLATE'])) > 0) {
                         $arParams['EMAIL_TEMPLATE'] = trim($arFields['EMAIL_TEMPLATE']);
                     }
                     $arParams['NOTIFY_TAG'] = isset($arFields['NOTIFY_TAG']) ? $arFields['NOTIFY_TAG'] : '';
                     $arParams['NOTIFY_SUB_TAG'] = isset($arFields['NOTIFY_SUB_TAG']) ? $arFields['NOTIFY_SUB_TAG'] : '';
                     if (isset($arFields['NOTIFY_TITLE']) && strlen(trim($arFields['NOTIFY_TITLE'])) > 0) {
                         $arParams['NOTIFY_TITLE'] = trim($arFields['NOTIFY_TITLE']);
                     }
                     if ($arParams['NOTIFY_TYPE'] == IM_NOTIFY_CONFIRM) {
                         if (isset($arFields['NOTIFY_BUTTONS'])) {
                             foreach ($arFields['NOTIFY_BUTTONS'] as $key => $arButtons) {
                                 if (is_array($arButtons)) {
                                     if (isset($arButtons['TITLE']) && strlen($arButtons['TITLE']) > 0 && isset($arButtons['VALUE']) && strlen($arButtons['VALUE']) > 0 && isset($arButtons['TYPE']) && strlen($arButtons['TYPE']) > 0) {
                                         $arButtons['TITLE'] = htmlspecialcharsbx($arButtons['TITLE']);
                                         $arButtons['VALUE'] = htmlspecialcharsbx($arButtons['VALUE']);
                                         $arButtons['TYPE'] = htmlspecialcharsbx($arButtons['TYPE']);
                                         $arFields['NOTIFY_BUTTONS'][$key] = $arButtons;
                                     } else {
                                         unset($arFields['NOTIFY_BUTTONS'][$key]);
                                     }
                                 } else {
                                     unset($arFields['NOTIFY_BUTTONS'][$key]);
                                 }
                             }
                         } else {
                             $arFields['NOTIFY_BUTTONS'] = array(array('TITLE' => GetMessage('IM_ERROR_BUTTON_ACCEPT'), 'VALUE' => 'Y', 'TYPE' => 'accept'), array('TITLE' => GetMessage('IM_ERROR_BUTTON_CANCEL'), 'VALUE' => 'N', 'TYPE' => 'cancel'));
                         }
                         $arParams['NOTIFY_BUTTONS'] = serialize($arFields["NOTIFY_BUTTONS"]);
                         if (isset($arParams['NOTIFY_TAG']) && strlen($arParams['NOTIFY_TAG']) > 0) {
                             CIMNotify::DeleteByTag($arParams['NOTIFY_TAG']);
                         }
                     }
                     if ($sendToSite) {
                         $result = IM\MessageTable::add($arParams);
                         $messageID = IntVal($result->getId());
                         if ($messageID <= 0) {
                             return false;
                         }
                     } else {
                         $messageID = time();
                     }
                     if (!$bConvert) {
                         if (CModule::IncludeModule('pull')) {
                             $CPushManager = new CPushManager();
                             if (isset($arFields['PUSH_MESSAGE']) && CIMSettings::GetNotifyAccess($arFields["TO_USER_ID"], $arFields['NOTIFY_MODULE'], $arFields['NOTIFY_EVENT'], CIMSettings::CLIENT_PUSH) && CModule::IncludeModule('pull')) {
                                 $CPushManager->AddQueue(array('USER_ID' => $arFields['TO_USER_ID'], 'MESSAGE' => str_replace("\n", " ", trim($arFields['PUSH_MESSAGE'])), 'PARAMS' => isset($arFields['PUSH_PARAMS']) ? $arFields['PUSH_PARAMS'] : '', 'TAG' => $arParams['NOTIFY_TAG'], 'SUB_TAG' => $arParams['NOTIFY_SUB_TAG'], 'APP_ID' => isset($arParams['PUSH_APP_ID']) ? $arParams['PUSH_APP_ID'] : ''));
                             } else {
                                 $CPushManager->AddQueue(array('USER_ID' => $arFields['TO_USER_ID'], 'APP_ID' => isset($arParams['PUSH_APP_ID']) ? $arParams['PUSH_APP_ID'] : ''));
                             }
                         }
                         foreach (GetModuleEvents("im", "OnAfterNotifyAdd", true) as $arEvent) {
                             ExecuteModuleEventEx($arEvent, array(intval($messageID), $arFields));
                         }
                     }
                     if (!$sendToSite) {
                         return false;
                     }
                     if (!empty($arFields['PARAMS'])) {
                         CIMMessageParam::Set($messageID, $arFields['PARAMS']);
                     }
                     IM\ChatTable::update($chatId, array('LAST_MESSAGE_ID' => $messageID));
                     CIMMessenger::SpeedFileDelete($arFields['TO_USER_ID'], IM_SPEED_NOTIFY);
                     if (!$bConvert) {
                         //CUserCounter::Increment($arFields['TO_USER_ID'], 'im_notify_v2', '**', false);
                         $strSql = "UPDATE b_im_relation SET STATUS = '" . IM_STATUS_UNREAD . "' WHERE USER_ID = " . intval($arFields['TO_USER_ID']) . " AND MESSAGE_TYPE = '" . IM_MESSAGE_SYSTEM . "' AND CHAT_ID = " . $chatId;
                         $DB->Query($strSql, false, "File: " . __FILE__ . "<br>Line: " . __LINE__);
                         if (CModule::IncludeModule("pull")) {
                             CPullStack::AddByUser(intval($arFields['TO_USER_ID']), array('module_id' => 'im', 'command' => 'notify', 'params' => CIMNotify::GetFormatNotify(array('ID' => $messageID, 'DATE_CREATE' => time(), 'FROM_USER_ID' => intval($arFields['FROM_USER_ID']), 'MESSAGE' => $arParams['MESSAGE'], 'PARAMS' => self::PrepareParamsForPull($arFields['PARAMS']), 'NOTIFY_MODULE' => $arParams['NOTIFY_MODULE'], 'NOTIFY_EVENT' => $arParams['NOTIFY_EVENT'], 'NOTIFY_TAG' => $arParams['NOTIFY_TAG'], 'NOTIFY_TYPE' => $arParams['NOTIFY_TYPE'], 'NOTIFY_BUTTONS' => isset($arParams['NOTIFY_BUTTONS']) ? $arParams['NOTIFY_BUTTONS'] : serialize(array()), 'NOTIFY_TITLE' => isset($arParams['NOTIFY_TITLE']) ? $arParams['NOTIFY_TITLE'] : ''))));
                             self::SendBadges($arFields['TO_USER_ID']);
                         }
                     }
                     return $messageID;
                 } else {
                     $GLOBALS["APPLICATION"]->ThrowException(GetMessage("IM_ERROR_MESSAGE_CREATE"), "CHAT_ID");
                     return false;
                 }
             } else {
                 $GLOBALS["APPLICATION"]->ThrowException(GetMessage("IM_ERROR_MESSAGE_TYPE"), "MESSAGE_TYPE");
                 return false;
             }
         }
     }
 }
Exemplo n.º 4
0
 function GetLastMessage($toUserId, $fromUserId = false, $loadUserData = false, $bTimeZone = true, $limit = true)
 {
     global $DB;
     $fromUserId = IntVal($fromUserId);
     if ($fromUserId <= 0) {
         $fromUserId = $this->user_id;
     }
     $toUserId = IntVal($toUserId);
     if ($toUserId <= 0) {
         $GLOBALS["APPLICATION"]->ThrowException(GetMessage("IM_ERROR_EMPTY_USER_ID"), "ERROR_TO_USER_ID");
         return false;
     }
     $chatId = 0;
     $startId = 0;
     $arMessages = array();
     $arUsersMessage = array();
     $arMessageId = array();
     if (!$bTimeZone) {
         CTimeZone::Disable();
     }
     $strSql = "\n\t\t\tSELECT R1.CHAT_ID, R1.START_ID, R2.LAST_ID, " . $DB->DatetimeToTimestampFunction('R2.LAST_READ') . " LAST_READ, R1.NOTIFY_BLOCK\n\t\t\tFROM b_im_relation R1\n\t\t\tINNER JOIN b_im_relation R2 on R2.CHAT_ID = R1.CHAT_ID\n\t\t\tWHERE\n\t\t\t\tR1.USER_ID = " . $fromUserId . "\n\t\t\t\tAND R1.MESSAGE_TYPE = '" . IM_MESSAGE_PRIVATE . "'\n\t\t\t\tAND R2.USER_ID = " . $toUserId . "\n\t\t\t\tAND R2.MESSAGE_TYPE = '" . IM_MESSAGE_PRIVATE . "'\n\t\t";
     if (!$bTimeZone) {
         CTimeZone::Enable();
     }
     $dbRes = $DB->Query($strSql, false, "File: " . __FILE__ . "<br>Line: " . __LINE__);
     if ($arRes = $dbRes->Fetch()) {
         $chatId = intval($arRes['CHAT_ID']);
         $startId = intval($arRes['START_ID']);
         $lastId = intval($arRes['LAST_ID']);
         $lastRead = intval($arRes['LAST_READ']);
         $blockNotify = $arRes['NOTIFY_BLOCK'] != 'N';
     }
     if ($chatId > 0) {
         if ($limit) {
             $dbType = strtolower($DB->type);
             if ($dbType == "mysql") {
                 $sqlLimit = " AND M.DATE_CREATE > DATE_SUB(NOW(), INTERVAL 30 DAY)";
             } else {
                 if ($dbType == "mssql") {
                     $sqlLimit = " AND M.DATE_CREATE > dateadd(day, -30, getdate())";
                 } else {
                     if ($dbType == "oracle") {
                         $sqlLimit = " AND M.DATE_CREATE > SYSDATE-30";
                     }
                 }
             }
         }
         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\t\tM.NOTIFY_EVENT\n\t\t\t\tFROM b_im_message M\n\t\t\t\tWHERE M.CHAT_ID = " . $chatId . " #LIMIT#\n\t\t\t\tORDER BY M.DATE_CREATE DESC, M.ID DESC\n\t\t\t";
         $strSql = $DB->TopSql($strSql, 20);
         if (!$bTimeZone) {
             CTimeZone::Enable();
         }
         if ($limit) {
             $dbRes = $DB->Query(str_replace("#LIMIT#", $sqlLimit, $strSql), false, "File: " . __FILE__ . "<br>Line: " . __LINE__);
         } else {
             $dbRes = $DB->Query(str_replace("#LIMIT#", "", $strSql), false, "File: " . __FILE__ . "<br>Line: " . __LINE__);
         }
         CIMStatus::Set($fromUserId, array('IDLE' => null));
         $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()) {
             if ($arRes['ID'] < $startId) {
                 continue;
             }
             if ($fromUserId == $arRes['AUTHOR_ID']) {
                 $arRes['TO_USER_ID'] = $toUserId;
                 $arRes['FROM_USER_ID'] = $fromUserId;
                 $convId = $arRes['TO_USER_ID'];
             } else {
                 $arRes['TO_USER_ID'] = $fromUserId;
                 $arRes['FROM_USER_ID'] = $toUserId;
                 $convId = $arRes['FROM_USER_ID'];
             }
             $arMessages[$arRes['ID']] = array('id' => $arRes['ID'], 'chatId' => $arRes['CHAT_ID'], 'senderId' => $arRes['FROM_USER_ID'], 'recipientId' => $arRes['TO_USER_ID'], 'system' => $arRes['NOTIFY_EVENT'] == 'private' ? 'N' : 'Y', 'date' => $arRes['DATE_CREATE'], 'text' => $CCTP->convertText(htmlspecialcharsbx($arRes['MESSAGE'])));
             $arMessageId[] = $arRes['ID'];
             $arUsersMessage[$convId][] = $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;
             }
         }
         unset($arMessages[$messageId]['params']['URL_ID']);
     }
     $arChatFiles = CIMDisk::GetFiles($chatId, $arFiles);
     $arMessages = CIMMessageLink::prepareShow($arMessages, $params);
     $arUserChatBlockStatus = array();
     if ($blockNotify) {
         $arUserChatBlockStatus[$chatId][$fromUserId] = 'Y';
     }
     $arResult = array('chatId' => $chatId, 'message' => $arMessages, 'usersMessage' => $arUsersMessage, 'users' => array(), 'userInGroup' => array(), 'woUserInGroup' => array(), 'files' => $arChatFiles, 'userChatBlockStatus' => $arUserChatBlockStatus);
     if ($lastRead > 0) {
         $arResult['readedList'][$toUserId] = array('messageId' => $lastId, 'date' => $lastRead);
     }
     if (is_array($loadUserData) || is_bool($loadUserData) && $loadUserData == true) {
         $bDepartment = true;
         if (is_array($loadUserData) && $loadUserData['DEPARTMENT'] == 'N') {
             $bDepartment = false;
         }
         $ar = CIMContactList::GetUserData(array('ID' => array($fromUserId, $toUserId), 'DEPARTMENT' => $bDepartment ? 'Y' : 'N', 'USE_CACHE' => 'N', 'SHOW_ONLINE' => 'Y', 'PHONES' => IsModuleInstalled('voximplant') ? 'Y' : 'N'));
         $arResult['users'] = $ar['users'];
         $arResult['userInGroup'] = $ar['userInGroup'];
         $arResult['woUserInGroup'] = $ar['woUserInGroup'];
         $arResult['phones'] = $ar['phones'];
     }
     return $arResult;
 }
Exemplo n.º 5
0
 /**
  * @param $pageId
  * @param $chatId
  * @param bool $bTimeZone
  * @return array
  */
 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;
                 }
             }
             unset($arMessages[$messageId]['params']['URL_ID']);
         }
         $arMessageFiles = CIMDisk::GetFiles($chatId, $arFiles);
         $arMessages = CIMMessageLink::prepareShow($arMessages, $params);
     }
     return array('chatId' => $chatId, 'message' => $arMessages, 'usersMessage' => $usersMessage, 'files' => $arMessageFiles);
 }