Ejemplo n.º 1
0
 /**
  * Method will be invoked after new database record inserted.
  *
  * @param array $newRecord All fields of inserted record.
  *
  * @return void
  */
 public function afterInsertTrigger(array $newRecord)
 {
     $arParams = array();
     $chatId = $newRecord['CHAT_ID'];
     $arRel = \CIMChat::GetRelationById($chatId);
     $arFields['MESSAGE_TYPE'] = '';
     foreach ($arRel as $rel) {
         $arFields['MESSAGE_TYPE'] = $rel["MESSAGE_TYPE"];
         break;
     }
     $arFields['PARAMS'] = array();
     $arFields['FILES'] = array();
     //CUserCounter::Increment($arFields['TO_USER_ID'], 'im_message_v2', '**', false);
     if ($arFields['MESSAGE_TYPE'] == IM_MESSAGE_PRIVATE) {
         foreach ($arRel as $rel) {
             if ($rel['USER_ID'] == $newRecord['AUTHOR_ID']) {
                 $arFields['FROM_USER_ID'] = $rel['USER_ID'];
             } else {
                 $arFields['TO_USER_ID'] = $rel['USER_ID'];
             }
         }
         \CIMContactList::SetRecent(array('ENTITY_ID' => $arFields['TO_USER_ID'], 'MESSAGE_ID' => $newRecord['ID'], 'CHAT_TYPE' => IM_MESSAGE_PRIVATE, 'USER_ID' => $arFields['FROM_USER_ID']));
         \CIMContactList::SetRecent(array('ENTITY_ID' => $arFields['FROM_USER_ID'], 'MESSAGE_ID' => $newRecord['ID'], 'CHAT_TYPE' => IM_MESSAGE_PRIVATE, 'USER_ID' => $arFields['TO_USER_ID']));
         if (\CModule::IncludeModule('pull')) {
             $pullMessage = array('module_id' => 'im', 'command' => 'message', 'params' => \CIMMessage::GetFormatMessage(array('ID' => $newRecord['ID'], 'CHAT_ID' => $chatId, 'TO_USER_ID' => $arFields['TO_USER_ID'], 'FROM_USER_ID' => $arFields['FROM_USER_ID'], 'SYSTEM' => $newRecord['NOTIFY_EVENT'] == 'private_system' ? 'Y' : 'N', 'MESSAGE' => $newRecord['MESSAGE'], 'DATE_CREATE' => time(), 'PARAMS' => $arFields['PARAMS'], 'FILES' => $arFields['FILES'])));
             $pullMessageTo = $pullMessage;
             if (\CPullOptions::GetPushStatus()) {
                 if (\CIMSettings::GetNotifyAccess($arFields["TO_USER_ID"], 'im', 'message', \CIMSettings::CLIENT_PUSH)) {
                     $pushParams = \CIMMessenger::PreparePushForPrivate(array('FROM_USER_ID' => $arFields['FROM_USER_ID'], 'MESSAGE' => $newRecord['MESSAGE'], 'SYSTEM' => $arFields['SYSTEM'], 'FILES' => $arFields['FILES']));
                     $pullMessageTo = array_merge($pullMessage, $pushParams);
                 }
             }
             \CPullStack::AddByUser($arFields['TO_USER_ID'], $pullMessageTo);
             \CPullStack::AddByUser($arFields['FROM_USER_ID'], $pullMessage);
             \CPushManager::DeleteFromQueueBySubTag($arFields['FROM_USER_ID'], 'IM_MESS');
             //self::SendBadges($arFields['TO_USER_ID']);
         }
     } else {
         if ($arFields['MESSAGE_TYPE'] == IM_MESSAGE_CHAT || $arFields['MESSAGE_TYPE'] == IM_MESSAGE_OPEN) {
             foreach ($arRel as $relation) {
                 \CIMContactList::SetRecent(array('ENTITY_ID' => $relation['CHAT_ID'], 'MESSAGE_ID' => $newRecord['ID'], 'CHAT_TYPE' => $relation['MESSAGE_TYPE'], 'USER_ID' => $relation['USER_ID']));
             }
             if (\CModule::IncludeModule('pull')) {
                 $pullMessage = array('module_id' => 'im', 'command' => 'messageChat', 'params' => \CIMMessage::GetFormatMessage(array('ID' => $newRecord['ID'], 'CHAT_ID' => $chatId, 'TO_CHAT_ID' => $chatId, 'FROM_USER_ID' => $newRecord['AUTHOR_ID'], 'MESSAGE' => $newRecord['MESSAGE'], 'SYSTEM' => $newRecord['AUTHOR_ID'] > 0 ? 'N' : 'Y', 'DATE_CREATE' => time(), 'PARAMS' => $arFields['PARAMS'], 'FILES' => $arFields['FILES'])));
                 $chat = \Bitrix\Im\ChatTable::getById($chatId);
                 $chatData = $chat->fetch();
                 if ($chatData && \CPullOptions::GetPushStatus()) {
                     $pushParams = \CIMMessenger::PreparePushForChat(array('CHAT_ID' => $chatId, 'CHAT_TITLE' => $chatData['TITLE'], 'FROM_USER_ID' => $newRecord['AUTHOR_ID'], 'MESSAGE' => $newRecord['MESSAGE'], 'SYSTEM' => $newRecord['AUTHOR_ID'] > 0 ? 'N' : 'Y', 'FILES' => $arFields['FILES']));
                     $pullMessage = array_merge($pullMessage, $pushParams);
                 }
                 $pullUsers = array();
                 $pullUsersSkip = array();
                 foreach ($arRel as $rel) {
                     $pullUsers[] = $rel['USER_ID'];
                     if ($rel['USER_ID'] == $newRecord['AUTHOR_ID']) {
                         $pullUsersSkip[] = $rel['USER_ID'];
                         \CPushManager::DeleteFromQueueBySubTag($newRecord['AUTHOR_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 ($arFields['MESSAGE_TYPE'] == IM_MESSAGE_OPEN) {
                     \CPullWatch::AddToStack('IM_PUBLIC_' . $chatId, $pullMessage);
                 }
                 /* TODO uncomment after fix mantis:66363
                 			\CIMMessenger::SendMention(Array(
                 				'CHAT_ID' => $chatId,
                 				'CHAT_TITLE' => $chatData['TITLE'],
                 				'CHAT_RELATION' => $arRel,
                 				'CHAT_TYPE' => $chatData['TYPE'],
                 				'MESSAGE' => $newRecord['MESSAGE'],
                 				'FILES' => $arFields['FILES'],
                 				'FROM_USER_ID' => $newRecord['AUTHOR_ID'],
                 			));
                 			*/
                 //\CIMMessenger::SendBadges($usersForBadges);
                 foreach (\GetModuleEvents("im", "OnAfterMessagesAdd", true) as $arEvent) {
                     \ExecuteModuleEventEx($arEvent, array($newRecord['ID'], $newRecord));
                 }
             }
         }
     }
 }
Ejemplo n.º 2
0
 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;
 }
Ejemplo n.º 3
0
 public static function UploadFileFromDisk($chatId, $files)
 {
     if (intval($chatId) <= 0) {
         return false;
     }
     $chatRelation = CIMChat::GetRelationById($chatId);
     if (!$chatRelation[self::GetUserId()]) {
         return false;
     }
     $result['FILES'] = array();
     $messageFileId = array();
     foreach ($files as $fileId) {
         $newFile = self::SaveFromLocalDisk($chatId, substr($fileId, 4));
         if ($newFile) {
             $result['FILES'][$fileId] = self::GetFileParams($chatId, $newFile);
             $messageFileId[] = $newFile->getId();
             foreach ($chatRelation as $relation) {
                 if ($relation['MESSAGE_TYPE'] != IM_MESSAGE_PRIVATE) {
                     break;
                 }
                 if (self::GetUserId() == $relation['USER_ID']) {
                     continue;
                 }
                 \Bitrix\Disk\Driver::getInstance()->getRecentlyUsedManager()->push($relation['USER_ID'], $newFile->getId());
             }
         } else {
             $result['FILES'][$fileId]['id'] = 0;
         }
     }
     if (empty($messageFileId)) {
         return false;
     }
     $result['MESSAGE_ID'] = 0;
     $arChat = CIMChat::GetChatData(array('ID' => $chatId));
     $ar = array("TO_CHAT_ID" => $chatId, "FROM_USER_ID" => self::GetUserId(), "MESSAGE_TYPE" => $arChat['chat'][$chatId]['messageType'], "PARAMS" => array('FILE_ID' => $messageFileId));
     $messageId = CIMMessage::Add($ar);
     if ($messageId) {
         $result['MESSAGE_ID'] = $messageId;
     }
     return $result;
 }
Ejemplo n.º 4
0
 public static function UndeliveredMessageAgent()
 {
     if (!CModule::IncludeModule("socialnetwork")) {
         return false;
     }
     $activateNewAgent = false;
     $dbMessage = CSocNetMessages::GetList(array("ID" => "ASC"), array("DATE_VIEW" => "", "TO_DELETED" => "N", "MESSAGE_TYPE" => "P"), false, array("nTopCount" => '500'), array("ID", "FROM_USER_ID", "TO_USER_ID", "MESSAGE", "DATE_CREATE"));
     while ($arMessage = $dbMessage->Fetch()) {
         $ar = array("FROM_USER_ID" => intval($arMessage["FROM_USER_ID"]), "TO_USER_ID" => intval($arMessage["TO_USER_ID"]), "MESSAGE" => $arMessage["MESSAGE"], "MESSAGE_DATE" => $arMessage["DATE_CREATE"]);
         CIMMessage::Add($ar);
         CSocNetMessages::Update($arMessage["ID"], array("TO_DELETED" => "Y", "FROM_DELETED" => "Y"));
         $activateNewAgent = true;
     }
     return $activateNewAgent ? "CIMConvert::UndeliveredMessageAgent();" : "";
 }
Ejemplo n.º 5
0
			if (isset($arMessage['message']))
			{
				foreach ($arMessage['message'] as $id => $ar)
					$arMessage['message'][$id]['recipientId'] = 'chat'.$ar['recipientId'];

				$arMessage['usersMessage']['chat'.$chatId] = $arMessage['usersMessage'][$chatId];
				unset($arMessage['usersMessage'][$chatId]);
			}
			$dialogId = 'chat'.$chatId;
		}
		else
		{
			$dialogId = intval($_POST['USER_ID']);
			if (CIMContactList::AllowToSend(Array('TO_USER_ID' => $dialogId)))
			{
				$CIMMessage = new CIMMessage();
				$arMessage = $CIMMessage->GetLastMessage($dialogId, false, ($_POST['USER_LOAD'] == 'Y'? true: false), false, false);

				$chatId = $arMessage['chatId'];
				if ($chatId <= 0)
				{
					$chatId = CIMMessage::GetChatId($USER->GetId(), $dialogId);
				}
			}
		}
		echo CUtil::PhpToJsObject(Array(
			'CHAT_ID' => $chatId,
			'USER_ID' => $dialogId,
			'MESSAGE' => isset($arMessage['message'])? $arMessage['message']: Array(),
			'USERS_MESSAGE' => isset($arMessage['message'])? $arMessage['usersMessage']: Array(),
			'USERS' => isset($arMessage['users'])? $arMessage['users']: Array(),
Ejemplo n.º 6
0
	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;
	}
Ejemplo n.º 7
0
 public function GetUnreadMessage($arParams = array())
 {
     global $DB;
     $bSpeedCheck = isset($arParams['SPEED_CHECK']) && $arParams['SPEED_CHECK'] == 'N' ? false : true;
     $lastId = !isset($arParams['LAST_ID']) || $arParams['LAST_ID'] == null ? null : IntVal($arParams['LAST_ID']);
     $order = isset($arParams['ORDER']) && $arParams['ORDER'] == 'ASC' ? 'ASC' : 'DESC';
     $loadDepartment = isset($arParams['LOAD_DEPARTMENT']) && $arParams['LOAD_DEPARTMENT'] == 'N' ? false : true;
     $bTimeZone = isset($arParams['USE_TIME_ZONE']) && $arParams['USE_TIME_ZONE'] == 'N' ? false : true;
     $bGroupByChat = isset($arParams['GROUP_BY_CHAT']) && $arParams['GROUP_BY_CHAT'] == 'Y' ? true : false;
     $bUserLoad = isset($arParams['USER_LOAD']) && $arParams['USER_LOAD'] == 'N' ? false : true;
     $bFileLoad = isset($arParams['FILE_LOAD']) && $arParams['FILE_LOAD'] == 'N' ? false : true;
     $arExistUserData = isset($arParams['EXIST_USER_DATA']) && is_array($arParams['EXIST_USER_DATA']) ? $arParams['EXIST_USER_DATA'] : array();
     $messageType = isset($arParams['MESSAGE_TYPE']) && in_array($arParams['MESSAGE_TYPE'], array(IM_MESSAGE_OPEN, IM_MESSAGE_CHAT)) ? $arParams['MESSAGE_TYPE'] : 'ALL';
     $arMessages = array();
     $arUnreadMessage = array();
     $arUsersMessage = array();
     $arResult = array('message' => array(), 'unreadMessage' => array(), 'usersMessage' => array(), 'users' => array(), 'userInGroup' => array(), 'woUserInGroup' => array(), 'files' => array(), 'countMessage' => 0, 'chat' => array(), 'userChatBlockStatus' => array(), 'userInChat' => array(), 'result' => false);
     $bLoadMessage = $bSpeedCheck ? CIMMessenger::SpeedFileExists($this->user_id, IM_SPEED_GROUP) : false;
     $count = CIMMessenger::SpeedFileGet($this->user_id, IM_SPEED_GROUP);
     if (!$bLoadMessage || $bLoadMessage && intval($count) > 0) {
         $ssqlLastId = "R1.LAST_ID";
         $ssqlStatus = " AND R1.STATUS < " . IM_STATUS_READ;
         if (!is_null($lastId) && intval($lastId) > 0 && !CIMMessenger::CheckXmppStatusOnline()) {
             $ssqlLastId = intval($lastId);
             $ssqlStatus = "";
         }
         $arRelations = array();
         if (strlen($ssqlStatus) > 0) {
             $strSql = "\n\t\t\t\t\tSELECT\n\t\t\t\t\t\tR1.USER_ID,\n\t\t\t\t\t\tR1.CHAT_ID,\n\t\t\t\t\t\tR1.LAST_ID\n\t\t\t\t\tFROM\n\t\t\t\t\t\tb_im_relation R1\n\t\t\t\t\tWHERE\n\t\t\t\t\t\tR1.USER_ID = " . $this->user_id . "\n\t\t\t\t\t\t" . ($messageType == 'ALL' ? "AND R1.MESSAGE_TYPE IN ('" . IM_MESSAGE_OPEN . "','" . IM_MESSAGE_CHAT . "')" : "AND R1.MESSAGE_TYPE = '" . $messageType . "'") . "\n\t\t\t\t\t\t" . $ssqlStatus . "\n\t\t\t\t";
             $dbSubRes = $DB->Query($strSql, false, "File: " . __FILE__ . "<br>Line: " . __LINE__);
             while ($arRes = $dbSubRes->Fetch()) {
                 $arRelations[] = $arRes;
             }
         }
         $arMessageId = array();
         $arMessageChatId = array();
         $arLastMessage = array();
         $arMark = array();
         $arChat = array();
         $arPrepareResult = array();
         $arFilteredResult = array();
         if (!empty($arRelations)) {
             if (!$bTimeZone) {
                 CTimeZone::Disable();
             }
             $strSql = "\n\t\t\t\t\tSELECT\n\t\t\t\t\t\tM.ID,\n\t\t\t\t\t\tM.CHAT_ID,\n\t\t\t\t\t\tM.MESSAGE,\n\t\t\t\t\t\t" . $DB->DatetimeToTimestampFunction('M.DATE_CREATE') . " DATE_CREATE,\n\t\t\t\t\t\tM.AUTHOR_ID,\n\t\t\t\t\t\tR1.STATUS R1_STATUS,\n\t\t\t\t\t\tR1.MESSAGE_TYPE MESSAGE_TYPE\n\t\t\t\t\tFROM b_im_message M\n\t\t\t\t\tINNER JOIN b_im_relation R1 ON M.ID > " . $ssqlLastId . " AND M.CHAT_ID = R1.CHAT_ID AND R1.USER_ID != M.AUTHOR_ID\n\t\t\t\t\tWHERE\n\t\t\t\t\t\tR1.USER_ID = " . $this->user_id . "\n\t\t\t\t\t\t" . ($messageType == 'ALL' ? "AND R1.MESSAGE_TYPE IN ('" . IM_MESSAGE_OPEN . "','" . IM_MESSAGE_CHAT . "')" : "AND R1.MESSAGE_TYPE = '" . $messageType . "'") . "\n\t\t\t\t\t\t" . $ssqlStatus . "\n\t\t\t\t";
             if (!$bTimeZone) {
                 CTimeZone::Enable();
             }
             $dbRes = $DB->Query($strSql, false, "File: " . __FILE__ . "<br>Line: " . __LINE__);
             $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()) {
                 $arPrepareResult[$arRes['CHAT_ID']][$arRes['ID']] = $arRes;
             }
             foreach ($arPrepareResult as $chatId => $arRes) {
                 if (count($arPrepareResult[$chatId]) > 100) {
                     $arPrepareResult[$chatId] = array_slice($arRes, -100, 100);
                 }
                 $arFilteredResult = array_merge($arFilteredResult, $arPrepareResult[$chatId]);
             }
             unset($arPrepareResult);
         }
         foreach ($arFilteredResult as $arRes) {
             $arUsers[] = $arRes['AUTHOR_ID'];
             $arMessages[$arRes['ID']] = array('id' => $arRes['ID'], 'chatId' => $arRes['CHAT_ID'], 'senderId' => $arRes['AUTHOR_ID'], 'recipientId' => $arRes['CHAT_ID'], 'date' => $arRes['DATE_CREATE'], 'text' => $arRes['MESSAGE'], 'messageType' => $arRes['MESSAGE_TYPE']);
             if ($bGroupByChat) {
                 $arMessages[$arRes['ID']]['conversation'] = $arRes['CHAT_ID'];
                 $arMessages[$arRes['ID']]['unread'] = $this->user_id != $arRes['AUTHOR_ID'] ? 'Y' : 'N';
             } else {
                 $arUsersMessage[$arRes['CHAT_ID']][] = $arRes['ID'];
                 if ($this->user_id != $arRes['AUTHOR_ID']) {
                     $arUnreadMessage[$arRes['CHAT_ID']][] = $arRes['ID'];
                 }
             }
             if ($arRes['R1_STATUS'] == IM_STATUS_UNREAD && (!isset($arMark[$arRes["CHAT_ID"]]) || $arMark[$arRes["CHAT_ID"]] < $arRes["ID"])) {
                 $arMark[$arRes["CHAT_ID"]] = $arRes["ID"];
             }
             if (!isset($arLastMessage[$arRes["CHAT_ID"]]) || $arLastMessage[$arRes["CHAT_ID"]] < $arRes["ID"]) {
                 $arLastMessage[$arRes["CHAT_ID"]] = $arRes["ID"];
             }
             $arChat[$arRes["CHAT_ID"]] = $arRes["CHAT_ID"];
             $arMessageId[] = $arRes['ID'];
             $arMessageChatId[$arRes['CHAT_ID']][$arRes["ID"]] = $arRes["ID"];
         }
         $params = CIMMessageParam::Get($arMessageId);
         if ($bFileLoad) {
             foreach ($arMessageChatId as $chatId => $messages) {
                 $files = array();
                 foreach ($messages as $messageId) {
                     $arMessages[$messageId]['params'] = $params[$messageId];
                     if (isset($params[$messageId]['FILE_ID'])) {
                         foreach ($params[$messageId]['FILE_ID'] as $fileId) {
                             $files[$fileId] = $fileId;
                         }
                     }
                 }
                 $arMessageFiles = CIMDisk::GetFiles($chatId, $files);
                 foreach ($arMessageFiles as $key => $value) {
                     $arResult['files'][$chatId][$key] = $value;
                 }
             }
         } else {
             foreach ($params as $messageId => $param) {
                 $arMessages[$messageId]['params'] = $param;
             }
         }
         if (!empty($arMessages)) {
             foreach ($arMark as $chatId => $lastSendId) {
                 CIMMessage::SetLastSendId($chatId, $this->user_id, $lastSendId);
             }
         } else {
             foreach ($arRelations as $relation) {
                 CIMMessage::SetLastId($relation['CHAT_ID'], $relation['USER_ID']);
             }
         }
         if ($bGroupByChat) {
             foreach ($arMessages as $key => $value) {
                 $arMessages[$arLastMessage[$value['conversation']]]['counter']++;
                 if ($arLastMessage[$value['conversation']] != $value['id']) {
                     unset($arMessages[$key]);
                 } else {
                     $arMessages[$key]['text'] = $CCTP->convertText(htmlspecialcharsbx($value['text']));
                     $arMessages[$key]['text_mobile'] = strip_tags(preg_replace("/<img.*?data-code=\"([^\"]*)\".*?>/i", "\$1", $CCTP->convertText(htmlspecialcharsbx(preg_replace("/\\[s\\].*?\\[\\/s\\]/i", "", $value['text'])))), '<br>');
                     $arUsersMessage[$value['conversation']][] = $value['id'];
                     if ($value['unread'] == 'Y') {
                         $arUnreadMessage[$value['conversation']][] = $value['id'];
                     }
                     unset($arMessages[$key]['conversation']);
                     unset($arMessages[$key]['unread']);
                 }
             }
         } else {
             foreach ($arMessages as $key => $value) {
                 $arMessages[$key]['text'] = $CCTP->convertText(htmlspecialcharsbx($value['text']));
                 $arMessages[$key]['text_mobile'] = strip_tags(preg_replace("/<img.*?data-code=\"([^\"]*)\".*?>/i", "\$1", $CCTP->convertText(htmlspecialcharsbx(preg_replace("/\\[s\\].*?\\[\\/s\\]/i", "", $value['text'])))), '<br>');
             }
         }
         $arResult['message'] = $arMessages;
         $arResult['unreadMessage'] = $arUnreadMessage;
         $arResult['usersMessage'] = $arUsersMessage;
         $arChat = self::GetChatData(array('ID' => $arChat, 'USE_CACHE' => 'N'));
         if (!empty($arChat)) {
             $arResult['chat'] = $arChat['chat'];
             $arResult['userChatBlockStatus'] = $arChat['userChatBlockStatus'];
             $arResult['userInChat'] = $arChat['userInChat'];
             foreach ($arChat['userInChat'] as $value) {
                 $arUsers[] = $value;
             }
         }
         if ($bUserLoad && !empty($arUsers)) {
             $arUserData = CIMContactList::GetUserData(array('ID' => array_diff(array_unique($arUsers), $arExistUserData), 'DEPARTMENT' => $loadDepartment ? 'Y' : 'N'));
             $arResult['users'] = $arUserData['users'];
             $arResult['userInGroup'] = $arUserData['userInGroup'];
             $arResult['woUserInGroup'] = $arUserData['woUserInGroup'];
         } else {
             $arResult['users'] = array();
             $arResult['userInGroup'] = array();
             $arResult['userInGroup'] = array();
         }
         $arResult['countMessage'] = CIMMessenger::GetMessageCounter($this->user_id, $arResult);
         if (!$bGroupByChat) {
             CIMMessenger::SpeedFileCreate($this->user_id, $arResult['countMessage'], IM_SPEED_GROUP);
         }
         $arResult['result'] = true;
     } else {
         $arResult['countMessage'] = CIMMessenger::GetMessageCounter($this->user_id, $arResult);
     }
     return $arResult;
 }
Ejemplo n.º 8
0
 public static function OnAfterUserUpdate($arParams)
 {
     if ($arParams['ACTIVE'] == 'N') {
         CIMMessage::SetReadMessageAll($arParams['ID']);
     }
 }
Ejemplo n.º 9
0
 public function ReceiveMessage($senderJId, array $arMessage, CXMPPClient $senderClient)
 {
     if (!$senderClient->IsAuthenticated()) {
         return false;
     }
     if (strlen($senderJId) <= 0) {
         return false;
     }
     if (!array_key_exists("message", $arMessage) || !array_key_exists(".", $arMessage["message"])) {
         return false;
     }
     $type = "normal";
     if (array_key_exists("type", $arMessage["message"]["."])) {
         $type = $arMessage["message"]["."]["type"];
     }
     if ($type == "error") {
         return false;
     }
     $mid = "";
     if (array_key_exists("id", $arMessage["message"]["."])) {
         $mid = $arMessage["message"]["."]["id"];
     }
     if (!in_array($type, array("chat", "groupchat", "headline", "normal"))) {
         return CXMPPUtility::GetErrorArray($senderJId, "message", "modify", "bad-request", "", $mid, "", $senderClient->GetClientDomain());
     }
     $recipientJId = "";
     if (array_key_exists("to", $arMessage["message"]["."])) {
         $recipientJId = $arMessage["message"]["."]["to"];
     }
     if (strlen($recipientJId) <= 0) {
         return CXMPPUtility::GetErrorArray($senderJId, "message", "modify", "bad-request", "", $mid, "", $senderClient->GetClientDomain());
     }
     $body = "";
     if (array_key_exists("body", $arMessage["message"])) {
         $body = $arMessage["message"]["body"]["#"];
     }
     if (strlen($body) <= 0) {
         return true;
     }
     //$body = CXMPPReceiveMessage::htmlspecialcharsback($body);
     //$body = html_entity_decode($body);
     $body = html_entity_decode($body, ENT_COMPAT, SITE_CHARSET);
     $body = str_replace("&apos;", "'", $body);
     $arSender = CXMPPUtility::GetUserByJId($senderJId);
     if (!$arSender) {
         return CXMPPUtility::GetErrorArray($senderJId, "message", "auth", "forbidden", $recipientJId, $mid, "", $senderClient->GetClientDomain());
     }
     $arRecipient = CXMPPUtility::GetUserByJId($recipientJId);
     if (!$arRecipient) {
         return CXMPPUtility::GetErrorArray($senderJId, "message", "cancel", "item-not-found", $recipientJId, $mid, "", $senderClient->GetClientDomain());
     }
     if (!CSocNetUserPerms::CanPerformOperation($arSender["ID"], $arRecipient["ID"], "message", false)) {
         return CXMPPUtility::GetErrorArray($senderJId, "message", "auth", "forbidden", $recipientJId, $mid, "", $senderClient->GetClientDomain());
     }
     if (IsModuleInstalled("im") && CModule::IncludeModule("im")) {
         $arMessageFields = array("FROM_USER_ID" => $arSender["ID"], "TO_USER_ID" => $arRecipient["ID"], "MESSAGE" => $body);
         CIMMessage::Add($arMessageFields);
     } else {
         $arMessageFields = array("=DATE_CREATE" => $GLOBALS["DB"]->CurrentTimeFunction(), "MESSAGE_TYPE" => SONET_MESSAGE_PRIVATE, "FROM_USER_ID" => $arSender["ID"], "TO_USER_ID" => $arRecipient["ID"], "MESSAGE" => $body);
         CSocNetMessages::Add($arMessageFields);
     }
     return true;
 }
Ejemplo n.º 10
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'];
     }
     $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;
 }
Ejemplo n.º 11
0
 public function GetUnreadMessage($arParams = array())
 {
     global $DB;
     $bSpeedCheck = isset($arParams['SPEED_CHECK']) && $arParams['SPEED_CHECK'] == 'N' ? false : true;
     $lastId = !isset($arParams['LAST_ID']) || $arParams['LAST_ID'] == null ? null : IntVal($arParams['LAST_ID']);
     $order = isset($arParams['ORDER']) && $arParams['ORDER'] == 'ASC' ? 'ASC' : 'DESC';
     $loadDepartment = isset($arParams['LOAD_DEPARTMENT']) && $arParams['LOAD_DEPARTMENT'] == 'N' ? false : true;
     $bTimeZone = isset($arParams['USE_TIME_ZONE']) && $arParams['USE_TIME_ZONE'] == 'N' ? false : true;
     $bGroupByChat = isset($arParams['GROUP_BY_CHAT']) && $arParams['GROUP_BY_CHAT'] == 'Y' ? true : false;
     $bUserLoad = isset($arParams['USER_LOAD']) && $arParams['USER_LOAD'] == 'N' ? false : true;
     $arExistUserData = isset($arParams['EXIST_USER_DATA']) && is_array($arParams['EXIST_USER_DATA']) ? $arParams['EXIST_USER_DATA'] : array();
     $arMessages = array();
     $arUnreadMessage = array();
     $arUsersMessage = array();
     $arResult = array('message' => array(), 'unreadMessage' => array(), 'usersMessage' => array(), 'users' => array(), 'userInGroup' => array(), 'woUserInGroup' => array(), 'countMessage' => 0, 'chat' => array(), 'userInChat' => array(), 'result' => false);
     $bLoadMessage = $bSpeedCheck ? CIMMessenger::SpeedFileExists($this->user_id, IM_SPEED_GROUP) : false;
     $count = CIMMessenger::SpeedFileGet($this->user_id, IM_SPEED_GROUP);
     if (!$bLoadMessage || $bLoadMessage && $count > 0) {
         $ssqlLastId = "R1.LAST_ID";
         $ssqlStatus = " AND R1.STATUS < " . IM_STATUS_READ;
         if (!is_null($lastId) && intval($lastId) > 0 && !CIMMessenger::CheckXmppStatusOnline()) {
             $ssqlLastId = intval($lastId);
             $ssqlStatus = "";
         }
         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->DateToCharFunction('M.DATE_CREATE') . " DATE_CREATE,\n\t\t\t\t\tM.AUTHOR_ID,\n\t\t\t\t\tR1.STATUS R1_STATUS\n\t\t\t\tFROM b_im_message M\n\t\t\t\tINNER JOIN b_im_relation R1 ON M.ID > " . $ssqlLastId . " AND M.CHAT_ID = R1.CHAT_ID AND R1.USER_ID != M.AUTHOR_ID\n\t\t\t\tWHERE R1.USER_ID = " . $this->user_id . " AND R1.MESSAGE_TYPE = '" . IM_MESSAGE_GROUP . "' " . $ssqlStatus . "\n\t\t\t\tORDER BY ID " . ($order == "DESC" ? "DESC" : "ASC") . "\n\t\t\t";
         if (!$bTimeZone) {
             CTimeZone::Enable();
         }
         $dbRes = $DB->Query($strSql, false, "File: " . __FILE__ . "<br>Line: " . __LINE__);
         $arLastMessage = array();
         $arMark = array();
         $arChat = array();
         $CCTP = new CTextParser();
         $CCTP->MaxStringLen = 200;
         $CCTP->allow = array("HTML" => "N", "ANCHOR" => $this->bHideLink ? "N" : "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");
         if (!$this->bHideLink) {
             $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");
         }
         while ($arRes = $dbRes->Fetch()) {
             $arUsers[] = $arRes['AUTHOR_ID'];
             $arMessages[$arRes['ID']] = array('id' => $arRes['ID'], 'senderId' => $arRes['AUTHOR_ID'], 'recipientId' => $arRes['CHAT_ID'], 'date' => MakeTimeStamp($arRes['DATE_CREATE']), 'text' => $arRes['MESSAGE']);
             if ($bGroupByChat) {
                 $arMessages[$arRes['ID']]['conversation'] = $arRes['CHAT_ID'];
                 $arMessages[$arRes['ID']]['unread'] = $this->user_id != $arRes['AUTHOR_ID'] ? 'Y' : 'N';
             } else {
                 $arUsersMessage[$arRes['CHAT_ID']][] = $arRes['ID'];
                 if ($this->user_id != $arRes['AUTHOR_ID']) {
                     $arUnreadMessage[$arRes['CHAT_ID']][] = $arRes['ID'];
                 }
             }
             if ($arRes['R1_STATUS'] == IM_STATUS_UNREAD && (!isset($arMark[$arRes["CHAT_ID"]]) || $arMark[$arRes["CHAT_ID"]] < $arRes["ID"])) {
                 $arMark[$arRes["CHAT_ID"]] = $arRes["ID"];
             }
             if (!isset($arLastMessage[$arRes["CHAT_ID"]]) || $arLastMessage[$arRes["CHAT_ID"]] < $arRes["ID"]) {
                 $arLastMessage[$arRes["CHAT_ID"]] = $arRes["ID"];
             }
             $arChat[$arRes["CHAT_ID"]] = $arRes["CHAT_ID"];
         }
         if ($bGroupByChat) {
             foreach ($arMessages as $key => $value) {
                 $arMessages[$arLastMessage[$value['conversation']]]['counter']++;
                 if ($arLastMessage[$value['conversation']] != $value['id']) {
                     unset($arMessages[$key]);
                 } else {
                     $arMessages[$key]['text'] = $CCTP->convertText(htmlspecialcharsbx($value['text']));
                     if ($this->bHideLink) {
                         $arMessages[$key]['text_mobile'] = $arMessages[$key]['text'];
                     } else {
                         $arMessages[$key]['text_mobile'] = $CCTPM->convertText(htmlspecialcharsbx($value['text']));
                     }
                     $arUsersMessage[$value['conversation']][] = $value['id'];
                     if ($value['unread'] == 'Y') {
                         $arUnreadMessage[$value['conversation']][] = $value['id'];
                     }
                     unset($arMessages[$key]['conversation']);
                     unset($arMessages[$key]['unread']);
                 }
             }
         } else {
             foreach ($arMessages as $key => $value) {
                 $arMessages[$key]['text'] = $CCTP->convertText(htmlspecialcharsbx($value['text']));
                 if ($this->bHideLink) {
                     $arMessages[$key]['text_mobile'] = $arMessages[$key]['text'];
                 } else {
                     $arMessages[$key]['text_mobile'] = $CCTPM->convertText(htmlspecialcharsbx($value['text']));
                 }
             }
         }
         foreach ($arMark as $chatId => $lastSendId) {
             CIMMessage::SetLastSendId($chatId, $this->user_id, $lastSendId);
         }
         $arResult['message'] = $arMessages;
         $arResult['unreadMessage'] = $arUnreadMessage;
         $arResult['usersMessage'] = $arUsersMessage;
         $arChat = self::GetChatData(array('ID' => $arChat, 'USE_CACHE' => 'N'));
         if (!empty($arChat)) {
             $arResult['chat'] = $arChat['chat'];
             $arResult['userInChat'] = $arChat['userInChat'];
             foreach ($arChat['userInChat'] as $value) {
                 $arUsers[] = $value;
             }
         }
         if ($bUserLoad && !empty($arUsers)) {
             $arUserData = CIMContactList::GetUserData(array('ID' => array_diff(array_unique($arUsers), $arExistUserData), 'DEPARTMENT' => $loadDepartment ? 'Y' : 'N'));
             $arResult['users'] = $arUserData['users'];
             $arResult['userInGroup'] = $arUserData['userInGroup'];
             $arResult['woUserInGroup'] = $arUserData['woUserInGroup'];
         } else {
             $arResult['users'] = array();
             $arResult['userInGroup'] = array();
             $arResult['userInGroup'] = array();
         }
         $arResult['countMessage'] = CIMMessenger::GetMessageCounter($this->user_id, $arResult);
         if (!$bGroupByChat) {
             CIMMessenger::SpeedFileCreate($this->user_id, $arResult['countMessage'], IM_SPEED_GROUP);
         }
         $arResult['result'] = true;
     } else {
         $arResult['countMessage'] = CIMMessenger::GetMessageCounter($this->user_id, $arResult);
     }
     return $arResult;
 }
Ejemplo n.º 12
0
 public static function MailMessageAgent()
 {
     global $DB;
     $defSiteID = CSite::GetDefSite();
     $arMark = array();
     $arUnsendMessage = CIMMessage::GetUnsendMessage();
     $arToUser = array();
     $arFromUser = array();
     $arDialog = array();
     foreach ($arUnsendMessage as $id => $arMessage) {
         if (!isset($arMark[$arMessage["TO_USER_ID"]][$arMessage["CHAT_ID"]]) || $arMark[$arMessage["TO_USER_ID"]][$arMessage["CHAT_ID"]] < $arMessage["ID"]) {
             $arMark[$arMessage["TO_USER_ID"]][$arMessage["CHAT_ID"]] = $arMessage["ID"];
         }
         if (strlen($arMessage["MESSAGE_OUT"]) <= 0) {
             $arMessage["MESSAGE_OUT"] = $arMessage["MESSAGE"];
         }
         if (!isset($arToUser[$arMessage["TO_USER_ID"]])) {
             $siteID = $arMessage["TO_USER_LID"];
             if ($siteID == false || StrLen($siteID) <= 0) {
                 $siteID = $defSiteID;
                 if ($siteID == false || StrLen($siteID) <= 0) {
                     continue;
                 }
             }
             $arMessage["USER"] = htmlspecialcharsback(CUser::FormatName(CSite::GetNameFormat(false), array("NAME" => $arMessage["TO_USER_NAME"], "LAST_NAME" => $arMessage["TO_USER_LAST_NAME"], "SECOND_NAME" => $arMessage["TO_USER_SECOND_NAME"], "LOGIN" => $arMessage["TO_USER_LOGIN"]), true));
             $arToUser[$arMessage["TO_USER_ID"]] = array("USER" => $arMessage["USER"], "USER_ID" => $arMessage["TO_USER_ID"], "USER_LOGIN" => $arMessage["TO_USER_LOGIN"], "USER_NAME" => $arMessage["TO_USER_NAME"], "USER_LAST_NAME" => $arMessage["TO_USER_LAST_NAME"], "USER_SECOND_NAME" => $arMessage["TO_USER_SECOND_NAME"], "TO_USER_LID" => $siteID, "EMAIL_TO" => $arMessage["TO_USER_EMAIL"]);
         }
         if (!isset($arFromUser[$arMessage["FROM_USER_ID"]])) {
             $arMessage["FROM_USER"] = htmlspecialcharsback(CUser::FormatName(CSite::GetNameFormat(false), array("NAME" => $arMessage["FROM_USER_NAME"], "LAST_NAME" => $arMessage["FROM_USER_LAST_NAME"], "SECOND_NAME" => $arMessage["FROM_USER_SECOND_NAME"], "LOGIN" => $arMessage["FROM_USER_LOGIN"]), true));
             $arFromUser[$arMessage["FROM_USER_ID"]] = array("FROM_USER" => $arMessage["FROM_USER"], "FROM_USER_ID" => $arMessage["FROM_USER_ID"], "FROM_USER_LOGIN" => $arMessage["FROM_USER_LOGIN"], "FROM_USER_NAME" => $arMessage["FROM_USER_NAME"], "FROM_USER_LAST_NAME" => $arMessage["FROM_USER_LAST_NAME"], "FROM_USER_SECOND_NAME" => $arMessage["FROM_USER_SECOND_NAME"]);
         }
         $arDialog[$arMessage["TO_USER_ID"]][$arMessage["FROM_USER_ID"]][] = array('DATE_CREATE' => $arMessage["DATE_CREATE"], 'MESSAGE' => CTextParser::convert4mail(str_replace("#BR#", "\n", strip_tags($arMessage["MESSAGE_OUT"]))));
     }
     foreach ($arMark as $userId => $ar) {
         foreach ($ar as $chatId => $lastSendId) {
             CIMMessage::SetLastSendId($chatId, $userId, $lastSendId);
         }
     }
     foreach ($arToUser as $toID => $arToInfo) {
         $message = "";
         $bHeader = false;
         $arNames = array();
         $bFirstMessage = true;
         foreach ($arDialog[$toID] as $fromID => $arMessages) {
             if ($bFirstMessage) {
                 $bFirstMessage = false;
             } else {
                 $message .= "\n";
             }
             if (count($arDialog[$toID]) > 1) {
                 $message .= GetMessage('IM_MAIL_TEMPLATE_NEW_MESSAGE_HEADER', array('#FROM_USER#' => $arFromUser[$fromID]['FROM_USER'])) . "\n";
                 $bHeader = true;
             }
             $arNames[] = $arFromUser[$fromID]['FROM_USER'];
             foreach ($arMessages as $arMessage) {
                 $message .= "\n" . GetMessage('IM_MAIL_TEMPLATE_NEW_MESSAGE_TEXT', array('#DATE_CREATE#' => $arMessage['DATE_CREATE'], '#MESSAGE#' => $arMessage['MESSAGE'])) . "\n";
             }
         }
         if ($bHeader) {
             $message .= "\n" . GetMessage('IM_MAIL_TEMPLATE_NEW_MESSAGE_FOOTER');
         }
         $arFields = array("USER" => $arToInfo["USER_ID"], "USER_ID" => $arToInfo["USER_ID"], "USER_LOGIN" => $arToInfo["USER_LOGIN"], "USER_NAME" => $arToInfo["USER_NAME"], "USER_LAST_NAME" => $arToInfo["USER_LAST_NAME"], "USER_SECOND_NAME" => $arToInfo["USER_SECOND_NAME"], "EMAIL_TO" => $arToInfo["EMAIL_TO"], "TITLE" => $arToInfo["TITLE"], "MESSAGES" => $message);
         if (count($arNames) > 1) {
             $mailTemplate = "IM_NEW_MESSAGE_GROUP";
             $arFields['FROM_USERS'] = implode(', ', $arNames);
         } else {
             $mailTemplate = "IM_NEW_MESSAGE";
             $arFields['FROM_USER'] = implode(', ', $arNames);
         }
         $event = new CEvent();
         $event->Send($mailTemplate, $arToInfo['TO_USER_LID'], $arFields, "N");
     }
     return "CIMMail::MailMessageAgent();";
 }
Ejemplo n.º 13
0
	public static function UploadFileFromDisk($chatId, $files)
	{
		if (intval($chatId) <= 0)
			return false;

		$chatRelation = CIMChat::GetRelationById($chatId);
		if (!$chatRelation[self::GetUserId()])
			return false;

		$result['FILES'] = Array();
		$messageFileId = Array();
		foreach ($files as $fileId)
		{
			$newFile = self::SaveFromLocalDisk($chatId, substr($fileId, 4));
			if ($newFile)
			{
				$result['FILES'][$fileId] = self::GetFileParams($chatId, $newFile);
				$messageFileId[] = $newFile->getId();
			}
			else
			{
				$result['FILES'][$fileId]['id'] = 0;
			}
		}
		if (empty($messageFileId))
		{
			return false;
		}

		$result['MESSAGE_ID'] = 0;
		$arChat = CIMChat::GetChatData(Array('ID' => $chatId));
		$ar = Array(
			"TO_CHAT_ID" => $chatId,
			"FROM_USER_ID" => self::GetUserId(),
			"MESSAGE_TYPE" => $arChat['chat'][$chatId]['type'] == IM_MESSAGE_PRIVATE? IM_MESSAGE_PRIVATE: IM_MESSAGE_GROUP,
			"PARAMS" => Array(
				'FILE_ID' => $messageFileId
			)
		);
		$messageId = CIMMessage::Add($ar);
		if ($messageId)
		{
			$result['MESSAGE_ID'] = $messageId;
		}

		return $result;
	}
Ejemplo n.º 14
0
 /**
  * Method will be invoked after new database record inserted.
  *
  * @param array $newRecord All fields of inserted record.
  *
  * @return void
  */
 public function afterInsertTrigger(array $newRecord)
 {
     $arParams = array();
     $chatId = $newRecord['CHAT_ID'];
     $arRel = \CIMChat::GetRelationById($chatId);
     //AddMessage2Log($newRecord);
     //AddMessage2Log($arRel);
     $arFields['MESSAGE_TYPE'] = '';
     foreach ($arRel as $rel) {
         $arFields['MESSAGE_TYPE'] = $rel["MESSAGE_TYPE"];
         break;
     }
     //AddMessage2Log($arParams);
     //CUserCounter::Increment($arFields['TO_USER_ID'], 'im_message_v2', '**', false);
     if ($arFields['MESSAGE_TYPE'] == IM_MESSAGE_PRIVATE) {
         foreach ($arRel as $rel) {
             if ($rel['USER_ID'] == $newRecord['AUTHOR_ID']) {
                 $arFields['FROM_USER_ID'] = $rel['USER_ID'];
             } else {
                 $arFields['TO_USER_ID'] = $rel['USER_ID'];
             }
         }
         \CIMContactList::SetRecent(array('ENTITY_ID' => $arFields['TO_USER_ID'], 'MESSAGE_ID' => $newRecord['ID'], 'CHAT_TYPE' => IM_MESSAGE_PRIVATE, 'USER_ID' => $arFields['FROM_USER_ID']));
         \CIMContactList::SetRecent(array('ENTITY_ID' => $arFields['FROM_USER_ID'], 'MESSAGE_ID' => $newRecord['ID'], 'CHAT_TYPE' => IM_MESSAGE_PRIVATE, 'USER_ID' => $arFields['TO_USER_ID']));
         if (\CModule::IncludeModule('pull')) {
             $arPullTo = array('module_id' => 'im', 'command' => 'message', 'params' => \CIMMessage::GetFormatMessage(array('ID' => $newRecord['ID'], 'CHAT_ID' => $chatId, 'TO_USER_ID' => $arFields['TO_USER_ID'], 'FROM_USER_ID' => $arFields['FROM_USER_ID'], 'SYSTEM' => $newRecord['NOTIFY_EVENT'] == 'private_system' ? 'Y' : 'N', 'MESSAGE' => $newRecord['MESSAGE'], 'DATE_CREATE' => time())));
             $arPullFrom = $arPullTo;
             \CPullStack::AddByUser($arFields['TO_USER_ID'], $arPullTo);
             \CPullStack::AddByUser($arFields['FROM_USER_ID'], $arPullFrom);
             \CPushManager::DeleteFromQueueBySubTag($arParams['FROM_USER_ID'], 'IM_MESS');
             //self::SendBadges($arParams['TO_USER_ID']);
         }
     } else {
         if ($arFields['MESSAGE_TYPE'] == IM_MESSAGE_CHAT || $arFields['MESSAGE_TYPE'] == IM_MESSAGE_OPEN) {
             foreach ($arRel as $relation) {
                 \CIMContactList::SetRecent(array('ENTITY_ID' => $relation['CHAT_ID'], 'MESSAGE_ID' => $newRecord['ID'], 'CHAT_TYPE' => $relation['MESSAGE_TYPE'], 'USER_ID' => $relation['USER_ID']));
             }
             if (\CModule::IncludeModule('pull')) {
                 $arPullTo = array('module_id' => 'im', 'command' => 'messageChat', 'params' => \CIMMessage::GetFormatMessage(array('ID' => $newRecord['ID'], 'CHAT_ID' => $chatId, 'TO_CHAT_ID' => $chatId, 'FROM_USER_ID' => $newRecord['AUTHOR_ID'], 'MESSAGE' => $newRecord['MESSAGE'], 'SYSTEM' => $newRecord['AUTHOR_ID'] > 0 ? 'N' : 'Y', 'DATE_CREATE' => time())));
                 $arPullFrom = $arPullTo;
                 foreach ($arRel as $rel) {
                     if ($rel['USER_ID'] == $arParams['FROM_USER_ID']) {
                         \CPullStack::AddByUser($arParams['FROM_USER_ID'], $arPullFrom);
                         \CPushManager::DeleteFromQueueBySubTag($arParams['FROM_USER_ID'], 'IM_MESS');
                     } else {
                         \CPullStack::AddByUser($rel['USER_ID'], $arPullTo);
                         //$usersForBadges[] = $rel['USER_ID'];
                     }
                 }
             }
         }
     }
 }
Ejemplo n.º 15
0
     $arMessage = array();
     $chatId = 0;
     if (substr($_POST['USER_ID'], 0, 4) == 'chat') {
         $chatId = intval(substr($_POST['USER_ID'], 4));
         $CIMChat = new CIMChat();
         $arMessage = $CIMChat->GetLastMessage($chatId, false, $_POST['USER_LOAD'] == 'Y' ? true : false, false);
         if (isset($arMessage['message'])) {
             foreach ($arMessage['message'] as $id => $ar) {
                 $arMessage['message'][$id]['recipientId'] = 'chat' . $ar['recipientId'];
             }
             $arMessage['usersMessage']['chat' . $chatId] = $arMessage['usersMessage'][$chatId];
             unset($arMessage['usersMessage'][$chatId]);
         }
     } else {
         if (CIMContactList::AllowToSend(array('TO_USER_ID' => $_POST['USER_ID']))) {
             $CIMMessage = new CIMMessage();
             $arMessage = $CIMMessage->GetLastMessage(intval($_POST['USER_ID']), false, $_POST['USER_LOAD'] == 'Y' ? true : false, false);
         }
     }
     echo CUtil::PhpToJsObject(array('USER_ID' => $chatId > 0 ? htmlspecialcharsbx($_POST['USER_ID']) : intval($_POST['USER_ID']), 'MESSAGE' => isset($arMessage['message']) ? $arMessage['message'] : array(), 'USERS_MESSAGE' => isset($arMessage['message']) ? $arMessage['usersMessage'] : array(), 'USERS' => isset($arMessage['users']) ? $arMessage['users'] : array(), 'USER_IN_GROUP' => isset($arMessage['userInGroup']) ? $arMessage['userInGroup'] : array(), 'WO_USER_IN_GROUP' => isset($arMessage['woUserInGroup']) ? $arMessage['woUserInGroup'] : array(), 'CHAT' => isset($arMessage['chat']) ? $arMessage['chat'] : array(), 'USER_IN_CHAT' => isset($arMessage['userInChat']) ? $arMessage['userInChat'] : array(), 'ERROR' => ''));
 } else {
     if ($_POST['IM_HISTORY_LOAD_MORE'] == 'Y') {
         $arMessage = array();
         if (CIMContactList::AllowToSend(array('TO_USER_ID' => $_POST['USER_ID']))) {
             $CIMHistory = new CIMHistory(false, array('hide_link' => isset($_POST['MOBILE']) ? true : false));
             if (substr($_POST['USER_ID'], 0, 4) == 'chat') {
                 $chatId = substr($_POST['USER_ID'], 4);
                 $arMessage = $CIMHistory->GetMoreChatMessage(intval($_POST['PAGE_ID']), $chatId, false);
                 if (!empty($arMessage['message'])) {
                     foreach ($arMessage['message'] as $id => $ar) {
                         $arMessage['message'][$id]['recipientId'] = 'chat' . $ar['recipientId'];
Ejemplo n.º 16
0
 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"]));
                     }
                 }
             }
         }
     }
 }
Ejemplo n.º 17
0
	public static function MessageToPrivate($fromUserId, $toUserId, $messageId, $getUserData = false, $addGenderToMessageId = true)
	{
		$fromUserId = intval($fromUserId);
		$toUserId = intval($toUserId);
		if ($fromUserId <= 0 || $toUserId <= 0)
			return false;

		$message = '';
		if ($fromUserId > 0 && $getUserData)
		{
			$arSelect = Array("ID", "LAST_NAME", "NAME", "LOGIN", "SECOND_NAME", "PERSONAL_GENDER");
			$dbUsers = CUser::GetList(($sort_by = false), ($dummy=''), array('ID' => $fromUserId), 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);
		}

		CIMMessage::Add(Array(
			"FROM_USER_ID" => $fromUserId,
			"TO_USER_ID" =>  $toUserId,
			"MESSAGE" => $message,
			"SYSTEM" => 'Y',
			"PUSH" => 'N',
		));

		return true;
	}
Ejemplo n.º 18
0
if (defined('IM_COMPONENT_INIT')) {
    return;
} 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) {
Ejemplo n.º 19
0
	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;
	}
Ejemplo n.º 20
0
 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;
 }
Ejemplo n.º 21
0
 public function SendUnreadMessages($receiverJId, $domain = "")
 {
     $receiverJId = trim($receiverJId);
     if (strlen($receiverJId) <= 0) {
         return false;
     }
     $receiver = CXMPPUtility::GetUserByJId($receiverJId, $domain);
     if (!$receiver) {
         return false;
     }
     $factory = CXMPPFactory::GetFactory();
     if (IsModuleInstalled("im") && CModule::IncludeModule("im")) {
         $CIMMessage = new CIMMessage($receiverJId, array('hide_link' => true));
         $arMessage = $CIMMessage->GetUnreadMessage(array('SPEED_CHECK' => 'N', 'ORDER' => 'ASC', 'USE_SMILES' => 'N', 'USER_LOAD' => 'N', 'LOAD_DEPARTMENT' => 'N'));
         if ($arMessage['result']) {
             foreach ($arMessage['message'] as $id => $arMessage) {
                 $factory->__SendMessage($arMessage["senderId"], $arMessage["recipientId"], $arMessage["id"], IM_MESSAGE_PRIVATE, htmlspecialcharsbx(CTextParser::convert4mail(str_replace(array("#BR#", "<br />", "<br>", "<br/>"), "\n", $arMessage["text"]))), $domain);
             }
         }
         // Notify
         $CIMNotify = new CIMNotify($receiverJId);
         $arNotify = $CIMNotify->GetUnreadNotify(array('SPEED_CHECK' => 'N', 'ORDER' => 'ASC'));
         if ($arNotify['result']) {
             foreach ($arNotify['original_notify'] as $id => $arNotify) {
                 if (isset($arNotify["NOTIFY_MODULE"]) && isset($arNotify["NOTIFY_EVENT"]) && !CIMSettings::GetNotifyAccess($arNotify["TO_USER_ID"], $arNotify["NOTIFY_MODULE"], $arNotify["NOTIFY_EVENT"], CIMSettings::CLIENT_XMPP)) {
                     continue;
                 }
                 if ($arNotify["MESSAGE_OUT"] == IM_MAIL_SKIP) {
                     $arNotify["MESSAGE_OUT"] = '';
                 }
                 $factory->__SendMessage($arNotify["FROM_USER_ID"], $arNotify["TO_USER_ID"], $arNotify["ID"], IM_MESSAGE_SYSTEM, htmlspecialcharsbx(CTextParser::convert4mail(str_replace(array("#BR#", "<br />", "<br>", "<br/>"), "\n", strlen($arNotify["MESSAGE_OUT"]) > 0 ? $arNotify["MESSAGE_OUT"] : $arNotify["MESSAGE"]))), $domain);
             }
         }
     } else {
         $parser = new CSocNetTextParser();
         $dbMessages = CSocNetMessages::GetList(array("DATE_CREATE" => "ASC"), array("TO_USER_ID" => $receiver["ID"], "DATE_VIEW" => "", "TO_DELETED" => "N", "IS_LOG_ALL" => "Y"), false, false, array("ID", "FROM_USER_ID", "TO_USER_ID", "MESSAGE", "DATE_VIEW", "MESSAGE_TYPE", "FROM_DELETED", "TO_DELETED", "IS_LOG"));
         while ($arMessage = $dbMessages->Fetch()) {
             $factory->__SendMessage($arMessage["IS_LOG"] == "Y" ? -5 : $arMessage["FROM_USER_ID"], $arMessage["TO_USER_ID"], $arMessage["ID"], $arMessage["MESSAGE_TYPE"], htmlspecialcharsbx($parser->convert4mail(str_replace(array("#BR#", "<br />", "<br>", "<br/>"), "\n", $arMessage["MESSAGE"]))), $domain);
         }
     }
     return true;
 }
Ejemplo n.º 22
0
	public static function CallCommand($recipientId, $command, $params = Array())
	{
		global $USER;

		if ($USER->GetID() <= 0 || intval($recipientId) <= 0)
			return false;

		if (!CModule::IncludeModule("pull"))
			return false;

		$arParams = Array(
			'command' => $command,
			'senderId' => $USER->GetID()
		);

		if ($command == 'signaling')
		{
			$arParams['peer'] = $params['peer'];
		}
		else if ($command == 'invite')
		{
			$arParams['video'] = isset($params['video']) && $params['video'] == 'Y'? true: false;

			$arUserData = CIMContactList::GetUserData(Array('ID' => array($USER->GetID(), $recipientId), 'DEPARTMENT' => 'N', 'HR_PHOTO' => 'Y'));
			$arParams['users'] = $arUserData['users'];
			$arParams['hrphoto'] = $arUserData['hrphoto'];
		}
		else if ($command == 'busy')
		{
			$arParams['video'] = isset($params['video']) && $params['video'] == 'Y'? true: false;

			$arSelect = Array("ID", "LAST_NAME", "NAME", "LOGIN", "SECOND_NAME", "PERSONAL_GENDER");
			$dbUsers = CUser::GetList(($sort_by = false), ($dummy=''), array('ID' => $USER->GetID()), array('FIELDS' => $arSelect));
			$arUsers = Array();
			if ($arUser = $dbUsers->Fetch())
			{
				$arUsers['NAME'] = CUser::FormatName(CSite::GetNameFormat(false), $arUser, true, false);
				$arUsers['GENDER'] = $arUser["PERSONAL_GENDER"] == 'F'? 'F': 'M';

				CIMMessage::Add(Array(
					"FROM_USER_ID" => intval($recipientId),
					"TO_USER_ID" => $USER->GetID(),
					"MESSAGE" => GetMessage("IM_CALL_CHAT_BUSY_".$arUsers['GENDER'], Array('#USER_NAME#' => $arUsers['NAME'])),
					"PUSH" => 'N',
					"SYSTEM" => 'Y',
				));
			}
		}
		else if ($command == 'waitTimeout')
		{
			$arSelect = Array("ID", "LAST_NAME", "NAME", "LOGIN", "SECOND_NAME", "PERSONAL_GENDER");
			$dbUsers = CUser::GetList(($sort_by = false), ($dummy=''), array('ID' => $recipientId), array('FIELDS' => $arSelect));
			$arUsers = Array();
			if ($arUser = $dbUsers->Fetch())
			{
				$arUsers['NAME'] = CUser::FormatName(CSite::GetNameFormat(false), $arUser, true, false);

				CIMMessage::Add(Array(
					"FROM_USER_ID" => $USER->GetID(),
					"TO_USER_ID" => intval($recipientId),
					"MESSAGE" => GetMessage("IM_CALL_CHAT_WAIT", Array('#USER_NAME#' => $arUsers['NAME'])),
					"PUSH" => 'N',
					"SYSTEM" => 'Y',
				));
			}
		}
		else if ($command == 'start')
		{
			CIMMessage::Add(Array(
				"FROM_USER_ID" => $USER->GetID(),
				"TO_USER_ID" =>  intval($recipientId),
				"MESSAGE" => GetMessage("IM_CALL_CHAT_START"),
				"SYSTEM" => 'Y',
				"PUSH" => 'N',
				"READED" => 'Y',
			));
		}
		else if ($command == 'errorAccess')
		{
			$arSelect = Array("ID", "LAST_NAME", "NAME", "LOGIN", "SECOND_NAME", "PERSONAL_GENDER");
			$dbUsers = CUser::GetList(($sort_by = false), ($dummy=''), array('ID' => $USER->GetID()), array('FIELDS' => $arSelect));
			$arUsers = Array();
			if ($arUser = $dbUsers->Fetch())
			{
				$arUsers['NAME'] = CUser::FormatName(CSite::GetNameFormat(false), $arUser, true, false);

				CIMMessage::Add(Array(
					"FROM_USER_ID" => $USER->GetID(),
					"TO_USER_ID" => intval($recipientId),
					"MESSAGE" => GetMessage("IM_CALL_CHAT_ERROR", Array('#USER_NAME#' => $arUsers['NAME'])),
					"SYSTEM" => 'Y',
					"PUSH" => 'N',
					"READED" => 'Y',
				));
			}
		}
		else if ($command == 'errorOffline')
		{
			$arSelect = Array("ID", "LAST_NAME", "NAME", "LOGIN", "SECOND_NAME", "PERSONAL_GENDER");
			$dbUsers = CUser::GetList(($sort_by = false), ($dummy=''), array('ID' => intval($recipientId)), array('FIELDS' => $arSelect));
			$arUsers = Array();
			if ($arUser = $dbUsers->Fetch())
			{
				$arUsers['NAME'] = CUser::FormatName(CSite::GetNameFormat(false), $arUser, true, false);

				CIMMessage::Add(Array(
					"FROM_USER_ID" => $USER->GetID(),
					"TO_USER_ID" =>  intval($recipientId),
					"MESSAGE" => GetMessage("IM_CALL_CHAT_OFFLINE", Array('#USER_NAME#' => $arUsers['NAME'])),
					"SYSTEM" => 'Y',
				));
			}
		}
		else if ($command == 'decline')
		{
			if ($params['CONNECTED'] == 'Y')
			{
				CIMMessage::Add(Array(
					"FROM_USER_ID" => $USER->GetID(),
					"TO_USER_ID" =>  intval($recipientId),
					"MESSAGE" => GetMessage("IM_CALL_CHAT_END"),
					"PUSH" => 'N',
					"SYSTEM" => 'Y',
					"READED" => 'Y',
				));
			}
			else
			{
				$arSelect = Array("ID", "LAST_NAME", "NAME", "LOGIN", "SECOND_NAME", "PERSONAL_GENDER");
				$dbUsers = CUser::GetList(($sort_by = false), ($dummy=''), array('ID' => $USER->GetID()), array('FIELDS' => $arSelect));
				$arUsers = Array();
				if ($arUser = $dbUsers->Fetch())
				{
					$arUsers['NAME'] = CUser::FormatName(CSite::GetNameFormat(false), $arUser, true, false);
					$arUsers['GENDER'] = $arUser["PERSONAL_GENDER"] == 'F'? 'F': 'M';

					CIMMessage::Add(Array(
						"FROM_USER_ID" => $USER->GetID(),
						"TO_USER_ID" =>  intval($recipientId),
						"MESSAGE" => GetMessage("IM_CALL_CHAT_DECLINE_".$arUsers['GENDER'], Array('#USER_NAME#' => $arUsers['NAME'])),
						"PUSH" => 'N',
						"SYSTEM" => 'Y',
						"READED" => $params['ACTIVE'] == 'Y'? 'Y': $params['INITIATOR'] == 'Y'? 'N': 'Y',
					));
				}
			}
		}
		else if ($command == 'busy_self')
		{
			CIMMessage::Add(Array(
				"FROM_USER_ID" => $USER->GetID(),
				"TO_USER_ID" =>  intval($recipientId),
				"MESSAGE" => GetMessage("IM_CALL_CHAT_END"),
				"PUSH" => 'N',
				"SYSTEM" => 'Y',
				"READED" => 'Y',
			));
		}
		else if ($command == 'wait' || $command == 'accept' || $command == 'ready' || $command == 'decline_self' || $command == 'accept_self' )
		{
		}
		else
			return false;


		CPullStack::AddByUser(intval($recipientId), Array(
			'module_id' => 'im',
			'command' => 'call',
			'params' => $arParams,
		));

		if ($command == 'accept' || $command == 'decline')
		{
			$arParams['command'] = $command.'_self';
			CPullStack::AddByUser($USER->GetID(), Array(
				'module_id' => 'im',
				'command' => 'call',
				'params' => $arParams,
			));
		}

		return true;
	}