Пример #1
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;
 }
Пример #2
0
	function GetLastMessage($toUserId, $fromUserId = false, $loadUserData = false, $bTimeZone = true, $limit = true)
	{
		global $DB;

		$fromUserId = IntVal($fromUserId);
		if ($fromUserId <= 0)
			$fromUserId = $this->user_id;

		$toUserId = IntVal($toUserId);
		if ($toUserId <= 0)
		{
			$GLOBALS["APPLICATION"]->ThrowException(GetMessage("IM_ERROR_EMPTY_USER_ID"), "ERROR_TO_USER_ID");
			return false;
		}

		$chatId = 0;
		$startId = 0;
		$arMessages = Array();
		$arUsersMessage = Array();
		$arMessageId = Array();

		if (!$bTimeZone)
			CTimeZone::Disable();
		$strSql ="
			SELECT R1.CHAT_ID, R1.START_ID, R2.LAST_ID, ".$DB->DatetimeToTimestampFunction('R2.LAST_READ')." LAST_READ
			FROM b_im_relation R1
			INNER JOIN b_im_relation R2 on R2.CHAT_ID = R1.CHAT_ID
			WHERE
				R1.USER_ID = ".$fromUserId."
				AND R1.MESSAGE_TYPE = '".IM_MESSAGE_PRIVATE."'
				AND R2.USER_ID = ".$toUserId."
		";
		if (!$bTimeZone)
			CTimeZone::Enable();
		$dbRes = $DB->Query($strSql, false, "File: ".__FILE__."<br>Line: ".__LINE__);
		if ($arRes = $dbRes->Fetch())
		{
			$chatId = intval($arRes['CHAT_ID']);
			$startId = intval($arRes['START_ID']);
			$lastId = intval($arRes['LAST_ID']);
			$lastRead = intval($arRes['LAST_READ']);
		}

		if ($chatId > 0)
		{
			if ($limit)
			{
				$dbType = strtolower($DB->type);
				if ($dbType== "mysql")
					$sqlLimit = " AND M.DATE_CREATE > DATE_SUB(NOW(), INTERVAL 30 DAY)";
				else if ($dbType == "mssql")
					$sqlLimit = " AND M.DATE_CREATE > dateadd(day, -30, getdate())";
				else if ($dbType == "oracle")
					$sqlLimit = " AND M.DATE_CREATE > SYSDATE-30";
			}

			if (!$bTimeZone)
				CTimeZone::Disable();
			$strSql ="
				SELECT
					M.ID,
					M.CHAT_ID,
					M.MESSAGE,
					".$DB->DatetimeToTimestampFunction('M.DATE_CREATE')." DATE_CREATE,
					M.AUTHOR_ID,
					M.NOTIFY_EVENT
				FROM b_im_message M
				WHERE M.CHAT_ID = ".$chatId." #LIMIT#
				ORDER BY DATE_CREATE DESC, ID DESC
			";
			$strSql = $DB->TopSql($strSql, 20);
			if (!$bTimeZone)
				CTimeZone::Enable();

			if ($limit)
			{
				$dbRes = $DB->Query(str_replace("#LIMIT#", $sqlLimit, $strSql), false, "File: ".__FILE__."<br>Line: ".__LINE__);
			}
			else
			{
				$dbRes = $DB->Query(str_replace("#LIMIT#", "", $strSql), false, "File: ".__FILE__."<br>Line: ".__LINE__);
			}

			CIMStatus::Set($fromUserId, Array('IDLE' => null));

			$CCTP = new CTextParser();
			$CCTP->MaxStringLen = 200;
			$CCTP->allow = array("HTML" => "N", "ANCHOR" => $this->bHideLink? "N": "Y", "BIU" => "Y", "IMG" => "N", "QUOTE" => "N", "CODE" => "N", "FONT" => "N", "LIST" => "N", "SMILES" => $this->bHideLink? "N": "Y", "NL2BR" => "Y", "VIDEO" => "N", "TABLE" => "N", "CUT_ANCHOR" => "N", "ALIGN" => "N");
			while ($arRes = $dbRes->Fetch())
			{
				if ($arRes['ID'] < $startId)
					continue;

				if ($fromUserId == $arRes['AUTHOR_ID'])
				{
					$arRes['TO_USER_ID'] = $toUserId;
					$arRes['FROM_USER_ID'] = $fromUserId;
					$convId = $arRes['TO_USER_ID'];
				}
				else
				{
					$arRes['TO_USER_ID'] = $fromUserId;
					$arRes['FROM_USER_ID'] = $toUserId;
					$convId = $arRes['FROM_USER_ID'];
				}

				$arMessages[$arRes['ID']] = Array(
					'id' => $arRes['ID'],
					'chatId' => $arRes['CHAT_ID'],
					'senderId' => $arRes['FROM_USER_ID'],
					'recipientId' => $arRes['TO_USER_ID'],
					'system' => $arRes['NOTIFY_EVENT'] == 'private'? 'N': 'Y',
					'date' => $arRes['DATE_CREATE'],
					'text' => $CCTP->convertText(htmlspecialcharsbx($arRes['MESSAGE']))
				);

				$arMessageId[] = $arRes['ID'];
				$arUsersMessage[$convId][] = $arRes['ID'];
			}
		}
		$params = CIMMessageParam::Get($arMessageId);

		$arFiles = Array();
		foreach ($params as $messageId => $param)
		{
			$arMessages[$messageId]['params'] = $param;
			if (isset($param['FILE_ID']))
			{
				foreach ($param['FILE_ID'] as $fileId)
				{
					$arFiles[$fileId] = $fileId;
				}
			}
		}
		$arChatFiles = CIMDisk::GetFiles($chatId, $arFiles);

		$arResult = Array(
			'chatId' => $chatId,
			'message' => $arMessages,
			'usersMessage' => $arUsersMessage,
			'users' => Array(),
			'userInGroup' => Array(),
			'woUserInGroup' => Array(),
			'files' => $arChatFiles
		);

		if ($lastRead > 0)
		{
			$arResult['readedList'][$toUserId] = Array(
				'messageId' => $lastId,
				'date' => $lastRead,
			);
		}

		if (is_array($loadUserData) || is_bool($loadUserData) && $loadUserData == true)
		{
			$bDepartment = true;
			if (is_array($loadUserData) && $loadUserData['DEPARTMENT'] == 'N')
				$bDepartment = false;

			$ar = CIMContactList::GetUserData(array(
					'ID' => Array($fromUserId, $toUserId),
					'DEPARTMENT' => ($bDepartment? 'Y': 'N'),
					'USE_CACHE' => 'N',
					'PHONES' => IsModuleInstalled('voximplant')? 'Y': 'N'
				)
			);
			$arResult['users'] = $ar['users'];
			$arResult['userInGroup']  = $ar['userInGroup'];
			$arResult['woUserInGroup']  = $ar['woUserInGroup'];
			$arResult['phones']  = $ar['phones'];
		}

		return $arResult;
	}
Пример #3
0
	public static function GetById($ID, $params = Array())
	{
		global $DB;

		$ID = intval($ID);

		$strSql = "
			SELECT
				DISTINCT M.*,
				".$DB->DatetimeToTimestampFunction('M.DATE_CREATE')." DATE_CREATE,
				R.MESSAGE_TYPE
			FROM b_im_message M
			LEFT JOIN b_im_relation R ON M.CHAT_ID = R.CHAT_ID
			WHERE M.ID = ".$ID;
		$dbRes = $DB->Query($strSql, false, "File: ".__FILE__."<br>Line: ".__LINE__);
		if ($arRes = $dbRes->Fetch())
		{
			$param = CIMMessageParam::Get($arRes['ID']);
			$arRes['PARAMS'] = $param? $param: Array();
		}
		if ($arRes && $params['WITH_FILES'] == 'Y')
		{
			$arFiles = Array();
			foreach ($arRes['PARAMS']['FILE_ID'] as $fileId)
			{
				$arFiles[$fileId] = $fileId;
			}
			$arRes['FILES'] = CIMDisk::GetFiles($arRes['CHAT_ID'], $arFiles);
		}

		return $arRes;
	}
Пример #4
0
 function GetMoreChatMessage($pageId, $chatId, $bTimeZone = true)
 {
     global $DB;
     $iNumPage = 1;
     if (intval($pageId) > 0) {
         $iNumPage = intval($pageId);
     }
     $chatId = IntVal($chatId);
     $limitById = '';
     $ar = \CIMChat::GetRelationById($chatId, $this->user_id);
     if ($ar && $ar['START_ID'] > 0) {
         $limitById = 'AND M.ID >= ' . intval($ar['START_ID']);
     }
     $strSql = "\n\t\t\tSELECT COUNT(M.ID) as CNT\n\t\t\tFROM b_im_message M\n\t\t\tINNER JOIN b_im_relation R1 ON M.CHAT_ID = R1.CHAT_ID\n\t\t\tWHERE R1.CHAT_ID = " . $chatId . " AND R1.USER_ID = " . $this->user_id . " " . $limitById . "\n\t\t";
     $res_cnt = $DB->Query($strSql);
     $res_cnt = $res_cnt->Fetch();
     $cnt = $res_cnt["CNT"];
     $arMessages = array();
     $arMessageFiles = array();
     $arMessageId = array();
     $usersMessage = array();
     if ($cnt > 0 && ceil($cnt / 20) >= $iNumPage) {
         if (!$bTimeZone) {
             CTimeZone::Disable();
         }
         $strSql = "\n\t\t\t\tSELECT\n\t\t\t\t\tM.ID,\n\t\t\t\t\tM.CHAT_ID,\n\t\t\t\t\tM.MESSAGE,\n\t\t\t\t\t" . $DB->DatetimeToTimestampFunction('M.DATE_CREATE') . " DATE_CREATE,\n\t\t\t\t\tM.AUTHOR_ID\n\t\t\t\tFROM b_im_message M\n\t\t\t\tINNER JOIN b_im_relation R1 ON M.CHAT_ID = R1.CHAT_ID\n\t\t\t\tWHERE R1.CHAT_ID = " . $chatId . " AND R1.USER_ID = " . $this->user_id . " " . $limitById . "\n\t\t\t\tORDER BY M.DATE_CREATE DESC, M.ID DESC\n\t\t\t";
         if (!$bTimeZone) {
             CTimeZone::Enable();
         }
         $dbRes = new CDBResult();
         $dbRes->NavQuery($strSql, $cnt, array('iNumPage' => $iNumPage, 'nPageSize' => 20));
         $CCTP = new CTextParser();
         $CCTP->MaxStringLen = 200;
         $CCTP->allow = array("HTML" => "N", "ANCHOR" => $this->bHideLink ? "N" : "Y", "BIU" => "Y", "IMG" => "N", "QUOTE" => "N", "CODE" => "N", "FONT" => "N", "LIST" => "N", "SMILES" => $this->bHideLink ? "N" : "Y", "NL2BR" => "Y", "VIDEO" => "N", "TABLE" => "N", "CUT_ANCHOR" => "N", "ALIGN" => "N");
         while ($arRes = $dbRes->Fetch()) {
             $arMessages[$arRes['ID']] = array('id' => $arRes['ID'], 'chatId' => $arRes['CHAT_ID'], 'senderId' => $arRes['AUTHOR_ID'], 'recipientId' => $arRes['CHAT_ID'], 'date' => $arRes['DATE_CREATE'], 'system' => $arRes['AUTHOR_ID'] > 0 ? 'N' : 'Y', 'text' => $CCTP->convertText(htmlspecialcharsbx($arRes['MESSAGE'])));
             $usersMessage[$arRes['CHAT_ID']][] = $arRes['ID'];
             $arMessageId[] = $arRes['ID'];
         }
         $params = CIMMessageParam::Get($arMessageId);
         $arFiles = array();
         foreach ($params as $messageId => $param) {
             $arMessages[$messageId]['params'] = $param;
             if (isset($param['FILE_ID'])) {
                 foreach ($param['FILE_ID'] as $fileId) {
                     $arFiles[$fileId] = $fileId;
                 }
             }
         }
         $arMessageFiles = CIMDisk::GetFiles($chatId, $arFiles);
     }
     return array('chatId' => $chatId, 'message' => $arMessages, 'usersMessage' => $usersMessage, 'files' => $arMessageFiles);
 }
Пример #5
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;
 }
Пример #6
0
         if (substr($_POST['RECIPIENT_ID'], 0, 4) != 'chat') {
             $ar = array("FROM_USER_ID" => intval($USER->GetID()), "TO_USER_ID" => intval($_POST['RECIPIENT_ID']), "MESSAGE" => $_POST['MESSAGE']);
             $insertID = CIMMessage::Add($ar);
         } else {
             $errorMessage = GetMessage('IM_ACCESS_ERROR');
         }
     }
     if (!$insertID && !$errorMessage) {
         if ($e = $GLOBALS["APPLICATION"]->GetException()) {
             $errorMessage = $e->GetString();
         }
         if (StrLen($errorMessage) == 0) {
             $errorMessage = GetMessage('IM_UNKNOWN_ERROR');
         }
     }
     $params = CIMMessageParam::Get(array($insertID));
     $arMessages = CIMMessageLink::prepareShow(array(), $params);
     $CCTP = new CTextParser();
     $CCTP->MaxStringLen = 200;
     $CCTP->allow = array("HTML" => "N", "ANCHOR" => isset($_POST['MOBILE']) ? "N" : "Y", "BIU" => "Y", "IMG" => "N", "QUOTE" => "N", "CODE" => "N", "FONT" => "N", "LIST" => "N", "SMILES" => "Y", "NL2BR" => "Y", "VIDEO" => "N", "TABLE" => "N", "CUT_ANCHOR" => "N", "ALIGN" => "N");
     $userTzOffset = isset($_POST['USER_TZ_OFFSET']) ? intval($_POST['USER_TZ_OFFSET']) : CTimeZone::GetOffset();
     $arResult = array('TMP_ID' => $tmpID, 'ID' => $insertID, 'SEND_DATE' => time() + $userTzOffset, 'SEND_MESSAGE' => $CCTP->convertText(htmlspecialcharsbx($ar['MESSAGE'])), 'SEND_MESSAGE_PARAMS' => $arMessages[$insertID]['params'], 'SENDER_ID' => intval($USER->GetID()), 'RECIPIENT_ID' => $_POST['CHAT'] == 'Y' ? htmlspecialcharsbx($_POST['RECIPIENT_ID']) : intval($_POST['RECIPIENT_ID']), 'ERROR' => $errorMessage);
     if (isset($_POST['MOBILE'])) {
         $arFormat = array("today" => "today, " . GetMessage('IM_MESSAGE_FORMAT_TIME'), "" => GetMessage('IM_MESSAGE_FORMAT_DATE'));
         $arResult['SEND_DATE_FORMAT'] = FormatDate($arFormat, time() + $userTzOffset);
     }
     echo CUtil::PhpToJsObject($arResult);
     CIMContactList::SetOnline();
     CIMMessenger::SetCurrentTab($_POST['TAB']);
 } else {
     if ($_POST['IM_EDIT_MESSAGE'] == 'Y') {
Пример #7
0
 public function GetUnreadNotify($arParams = array())
 {
     global $DB;
     $order = isset($arParams['ORDER']) && $arParams['ORDER'] == 'ASC' ? 'ASC' : 'DESC';
     $bSpeedCheck = isset($arParams['SPEED_CHECK']) && $arParams['SPEED_CHECK'] == 'N' ? false : true;
     $bTimeZone = isset($arParams['USE_TIME_ZONE']) && $arParams['USE_TIME_ZONE'] == 'N' ? false : true;
     $bGetOnlyFlash = isset($arParams['GET_ONLY_FLASH']) && $arParams['GET_ONLY_FLASH'] == 'Y' ? true : false;
     $arNotify['result'] = false;
     $arNotify['notify'] = array();
     $arNotify['unreadNotify'] = array();
     $arNotify['loadNotify'] = false;
     $arNotify['countNotify'] = 0;
     $arNotify['maxNotify'] = 0;
     $bLoadNotify = $bSpeedCheck ? !CIMMessenger::SpeedFileExists($this->user_id, IM_SPEED_NOTIFY) : true;
     if ($bLoadNotify) {
         $strSql = "SELECT CHAT_ID, STATUS FROM b_im_relation WHERE USER_ID = " . $this->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']);
             $chatStatus = $arRes['STATUS'];
         } else {
             return $arNotify;
         }
         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\tM.MESSAGE_OUT,\n\t\t\t\t\t" . $DB->DatetimeToTimestampFunction('M.DATE_CREATE') . " DATE_CREATE,\n\t\t\t\t\tM.NOTIFY_TYPE,\n\t\t\t\t\tM.NOTIFY_MODULE,\n\t\t\t\t\tM.NOTIFY_EVENT,\n\t\t\t\t\tM.NOTIFY_TITLE,\n\t\t\t\t\tM.NOTIFY_BUTTONS,\n\t\t\t\t\tM.NOTIFY_TAG,\n\t\t\t\t\tM.NOTIFY_SUB_TAG,\n\t\t\t\t\tM.NOTIFY_READ,\n\t\t\t\t\t{$this->user_id} TO_USER_ID,\n\t\t\t\t\tM.AUTHOR_ID FROM_USER_ID\n\t\t\t\tFROM b_im_message M\n\t\t\t\tWHERE M.CHAT_ID = " . $chatId . " AND M.NOTIFY_READ = 'N'\n\t\t\t";
         if (!$bTimeZone) {
             CTimeZone::Enable();
         }
         $strSql = $DB->TopSql($strSql, 500);
         $dbRes = $DB->Query($strSql, false, "File: " . __FILE__ . "<br>Line: " . __LINE__);
         $arMark = array();
         $arGetUsers = array();
         $arNotifyId = array();
         while ($arRes = $dbRes->Fetch()) {
             if ($this->bHideLink) {
                 $arRes['HIDE_LINK'] = 'Y';
             }
             $arNotifyId[$arRes['ID']] = $arRes['ID'];
             $arNotify['original_notify'][$arRes['ID']] = $arRes;
             $arNotify['notify'][$arRes['ID']] = $arRes;
             $arNotify['unreadNotify'][$arRes['ID']] = $arRes['ID'];
             if ($chatStatus == IM_STATUS_UNREAD && (!isset($arMark[$arRes["CHAT_ID"]]) || $arMark[$arRes["CHAT_ID"]] < $arRes["ID"])) {
                 $arMark[$arRes["CHAT_ID"]] = $arRes["ID"];
             }
             if ($arNotify['maxNotify'] < $arRes['ID']) {
                 $arNotify['maxNotify'] = $arRes['ID'];
             }
             $arGetUsers[] = $arRes['FROM_USER_ID'];
         }
         $params = CIMMessageParam::Get(array_keys($arNotifyId));
         foreach ($params as $notifyId => $param) {
             $arNotify['notify'][$notifyId]['PARAMS'] = $param;
         }
         foreach ($arMark as $chatId => $lastSendId) {
             CIMNotify::SetLastSendId($chatId, $lastSendId);
         }
         $arNotify['countNotify'] = $this->GetNotifyCounter($arNotify);
         CIMMessenger::SpeedFileCreate($this->user_id, $arNotify['countNotify'], IM_SPEED_NOTIFY);
         $arUsers = CIMContactList::GetUserData(array('ID' => $arGetUsers, 'DEPARTMENT' => 'N', 'USE_CACHE' => 'Y', 'CACHE_TTL' => 86400));
         $arGetUsers = $arUsers['users'];
         if ($bGetOnlyFlash) {
             foreach ($arNotify['notify'] as $key => $value) {
                 if (isset($_SESSION['IM_FLASHED_NOTIFY'][$key])) {
                     unset($arNotify['notify'][$key]);
                     unset($arNotify['original_notify'][$key]);
                     $arNotify['loadNotify'] = true;
                 } else {
                     $value['FROM_USER_DATA'] = $arGetUsers;
                     $arNotify['notify'][$key] = self::GetFormatNotify($value);
                 }
             }
         } else {
             foreach ($arNotify['notify'] as $key => $value) {
                 $value['FROM_USER_DATA'] = $arGetUsers;
                 $arNotify['notify'][$key] = self::GetFormatNotify($value);
             }
         }
         $arNotify['result'] = true;
     } else {
         $arNotify['countNotify'] = $this->GetNotifyCounter();
         if ($arNotify['countNotify'] > 0) {
             $arNotify['loadNotify'] = true;
         }
     }
     return $arNotify;
 }
	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 = self::GetUserNameTemplate(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/recent'.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.ID M_ID, M.CHAT_ID M_CHAT_ID, M.MESSAGE M_MESSAGE, ".$DB->DatetimeToTimestampFunction('M.DATE_CREATE')." M_DATE_CREATE,
					C.TITLE C_TITLE, C.AUTHOR_ID C_OWNER_ID, C.ENTITY_TYPE C_ENTITY_TYPE, C.AVATAR C_AVATAR,
					U.LOGIN, U.NAME, U.LAST_NAME, U.PERSONAL_PHOTO, U.SECOND_NAME, U.PERSONAL_BIRTHDAY, U.PERSONAL_GENDER, U.WORK_POSITION
				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();
			$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))
			{
				$arMessageId[] = $arRes['M_ID'];
				$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'],
						'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("/------------------------------------------------------(.*)------------------------------------------------------/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,
						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'],
						'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,
						'profile' => CIMContactList::GetUserPath($arRes["ITEM_ID"])
					);
				}
				else
				{
					$itemId = 'chat'.$itemId;
					$item['CHAT'] = Array(
						'id' => $arRes['ITEM_ID'],
						'name' => $arRes["C_TITLE"],
						'avatar' => CIMChat::GetAvatarImage($arRes["C_AVATAR"]),
						'owner' => $arRes["C_OWNER_ID"],
						'style' => strlen($arRes["C_ENTITY_TYPE"])>0? 'call': 'group',
					);
				}
				$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_GROUP]))
					self::DeleteRecent($toDelete[IM_MESSAGE_GROUP], true);
			}

			if (IsModuleInstalled('extranet') && $bIntranetEnable)
			{
				$arUserDepartment = Array();
				$arFilter['ID'] = $USER->GetID()."|".implode('|', $arUsers);
				$arExtParams = Array('FIELDS' => Array("ID"), 'SELECT' => Array('UF_DEPARTMENT'));

				$dbUsers = CUser::GetList(($sort_by = Array('last_name'=>'asc')), ($dummy=''), $arFilter, $arExtParams);
				while ($arUser = $dbUsers->GetNext(true, false))
				{
					$arUserDepartment[$arUser['ID']] = self::IsExtranet($arUser);
				}

				foreach ($arRecent as $key => $value)
				{
					if (isset($value['USER']))
					{
						$arRecent[$key]['USER']['extranet'] = $arUserDepartment[$value['USER']['id']];
					}
				}
			}

			if($obCache->StartDataCache())
				$obCache->EndDataCache(Array('recent' => $arRecent, 'users' => $arUsers));
		}

		$arOnline = CIMStatus::GetList(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']])? $arOnline['users'][$value['USER']['id']]['status']: 'offline';
			$arRecent[$key]['USER']['idle'] = isset($arOnline['users'][$value['USER']['id']])? $arOnline['users'][$value['USER']['id']]['idle']: 0;
		}

		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',
				'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' => 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' => array(__CLASS__, 'GetRecentListSortCounter'),
					'MESSAGE' => array(__CLASS__, 'GetRecentListSortMessage'),
				),
				null, true
			);
		}

		return $arRecent;
	}