Ejemplo n.º 1
0
             if ($error !== false) {
                 $arResult['ERRORS'][] = $error;
             }
         }
         if (empty($arResult['ERRORS'])) {
             $arFields = array('LID' => SITE_ID, 'ACTIVE' => 'Y', 'SERVICE_ID' => $arResult['SERVICE'], 'NAME' => $arResult['SETTINGS']['name'], 'SERVER' => $arResult['SETTINGS']['server'], 'PORT' => $arResult['SETTINGS']['port'], 'LINK' => $arResult['SETTINGS']['link'], 'LOGIN' => $arResult['SETTINGS']['login'], 'PASSWORD' => $arResult['SETTINGS']['password'], 'USE_TLS' => $arResult['SETTINGS']['encryption'] == 'Y' ? 'Y' : 'N', 'SERVER_TYPE' => 'imap', 'USER_ID' => $USER->GetID());
             if (!empty($arResult['ID'])) {
                 $res = CMailbox::Update($arResult['ID'], $arFields);
             } else {
                 $arResult['ID'] = CMailbox::Add($arFields);
                 $res = $arResult['ID'] > 0;
             }
             if (!$res) {
                 $arResult['ERRORS'][] = GetMessage('INTR_MAIL_SAVE_ERROR');
             } else {
                 CUserCounter::Set($USER->GetID(), 'mail_unseen', $unseen, SITE_ID);
                 CUserOptions::SetOption('global', 'last_mail_check_' . SITE_ID, time());
                 CUserOptions::SetOption('global', 'last_mail_check_success_' . SITE_ID, $unseen >= 0);
                 LocalRedirect($APPLICATION->GetCurPage() . '?STEP=confirm&ACT=setup');
             }
         }
     }
     break;
 case 'remove':
     if (check_bitrix_sessid()) {
         CMailbox::Delete($arResult['ID']);
         CUserCounter::Clear($USER->GetID(), 'mail_unseen', SITE_ID);
         CUserOptions::DeleteOption('global', 'last_mail_check_' . SITE_ID);
         CUserOptions::DeleteOption('global', 'last_mail_check_success_' . SITE_ID);
         LocalRedirect($APPLICATION->GetCurPage() . '?STEP=confirm&ACT=remove');
     } else {
Ejemplo n.º 2
0
 public static function SetUnreadCounter($userId)
 {
     return false;
     $userId = intval($userId);
     if ($userId <= 0) {
         return false;
     }
     global $DB;
     $sqlCounter = "SELECT COUNT(M.ID) as CNT\n\t\t\t\t\t\tFROM b_im_message M\n\t\t\t\t\t\tINNER JOIN b_im_relation R1 ON M.ID > R1.LAST_ID AND M.CHAT_ID = R1.CHAT_ID AND R1.MESSAGE_TYPE IN ('" . IM_MESSAGE_OPEN . "','" . IM_MESSAGE_CHAT . "') AND R1.STATUS < " . IM_STATUS_READ . "\n\t\t\t\t\t\tWHERE R1.USER_ID = " . $userId;
     $dbRes = $DB->Query($sqlCounter, false, "File: " . __FILE__ . "<br>Line: " . __LINE__);
     if ($row = $dbRes->Fetch()) {
         CUserCounter::Set($userId, 'im_chat_v2', $row['CNT'], '**', false);
     } else {
         CUserCounter::Set($userId, 'im_chat_v2', 0, '**', false);
     }
     return true;
 }
Ejemplo n.º 3
0
        case 'crdomain':
            list($acc['login'], $acc['domain']) = explode('@', $acc['LOGIN'], 2);
            $crCheckMailbox = CControllerClient::ExecuteEvent('OnMailControllerCheckMemberMailbox', array('DOMAIN' => $acc['domain'], 'NAME' => $acc['login']));
            if (isset($crCheckMailbox['result'])) {
                $unseen = intval($crCheckMailbox['result']);
            } else {
                $unseen = -1;
                $error = empty($crCheckMailbox['error']) ? GetMessage('INTR_MAIL_CONTROLLER_INVALID') : CMail::getErrorMessage($crCheckMailbox['error']);
            }
            break;
        case 'domain':
            $service = \Bitrix\Mail\MailServicesTable::getRowById($acc['SERVICE_ID']);
            list($acc['login'], $acc['domain']) = explode('@', $acc['LOGIN'], 2);
            $result = CMailDomain2::getUnreadMessagesCount($service['TOKEN'], $acc['domain'], $acc['login'], $error);
            if (is_null($result)) {
                $unseen = -1;
                $error = CMail::getErrorMessage($error);
            } else {
                $unseen = intval($result);
            }
            break;
    }
    CUserCounter::Set($userId, 'mail_unseen', $unseen, $siteId);
    CUserOptions::SetOption('global', 'last_mail_check_' . $siteId, time(), false, $userId);
    CUserOptions::SetOption('global', 'last_mail_check_success_' . $siteId, $unseen >= 0, false, $userId);
} else {
    $unseen = 0;
    CUserOptions::SetOption('global', 'last_mail_check_' . $siteId, -1, false, $userId);
}
header('Content-Type: application/x-javascript; charset=' . LANG_CHARSET);
echo json_encode(array('result' => $error === false ? 'ok' : 'error', 'unseen' => $unseen, 'last_check' => CUserOptions::GetOption('global', 'last_mail_check_' . $siteId, false, $userId), 'error' => $error));
Ejemplo n.º 4
0
 public function Synchronize()
 {
     $currentDay = time() + CTimeZone::GetOffset();
     $currentDayEnd = ConvertTimeStamp(mktime(23, 59, 59, date('n', $currentDay), date('j', $currentDay), date('Y', $currentDay)), 'FULL', SITE_ID);
     $count = 0;
     if ($this->typeID === self::CurrentActivies) {
         //Count of open user activities (start time: before tomorrow)
         $filter = array('RESPONSIBLE_ID' => $this->userID, 'COMPLETED' => 'N', '<=START_TIME' => $currentDayEnd);
         $count = CCrmActivity::GetCount($filter);
     } elseif ($this->typeID === self::CurrentCompanyActivies) {
         $count = CCrmActivity::GetCurrentQuantity($this->userID, CCrmOwnerType::Company);
     } elseif ($this->typeID === self::CurrentContactActivies) {
         $count = CCrmActivity::GetCurrentQuantity($this->userID, CCrmOwnerType::Contact);
     } elseif ($this->typeID === self::CurrentLeadActivies) {
         $count = CCrmActivity::GetCurrentQuantity($this->userID, CCrmOwnerType::Lead);
         if (CCrmUserCounterSettings::GetValue(CCrmUserCounterSettings::ReckonActivitylessItems, true)) {
             $leadTable = CCrmLead::TABLE_NAME;
             $activityTable = CCrmActivity::USER_ACTIVITY_TABLE_NAME;
             $statusStr = "'CONVERTED'";
             $statusCount = 1;
             $statuses = self::GetStatusList('STATUS');
             $isFound = false;
             foreach ($statuses as &$status) {
                 if (!$isFound) {
                     $isFound = $status['STATUS_ID'] === 'CONVERTED';
                 } else {
                     $statusStr .= ",'{$status['STATUS_ID']}'";
                     $statusCount++;
                     // Foolproof
                     if ($statusCount === 10) {
                         break;
                     }
                 }
             }
             unset($status);
             global $DBType;
             $sqlData = array('FROM' => '', 'WHERE' => "l.ASSIGNED_BY_ID = {$this->userID} AND l.STATUS_ID NOT IN({$statusStr}) AND l.ID NOT IN(SELECT a.OWNER_ID FROM {$activityTable} a WHERE a.USER_ID = 0 AND a.OWNER_TYPE_ID = 1)", 'GROUPBY' => '');
             $count += CSqlUtil::GetRowCount($sqlData, $leadTable, 'l', $DBType);
         }
     } elseif ($this->typeID === self::CurrentDealActivies) {
         $count = CCrmActivity::GetCurrentQuantity($this->userID, CCrmOwnerType::Deal);
         if (CCrmUserCounterSettings::GetValue(CCrmUserCounterSettings::ReckonActivitylessItems, true)) {
             $dealTable = CCrmDeal::TABLE_NAME;
             $activityTable = CCrmActivity::USER_ACTIVITY_TABLE_NAME;
             $stageStr = "'WON'";
             $stageCount = 1;
             $stages = self::GetStatusList('DEAL_STAGE');
             $isFound = false;
             foreach ($stages as &$stage) {
                 if (!$isFound) {
                     $isFound = $stage['STATUS_ID'] === 'WON';
                 } else {
                     $stageStr .= ",'{$stage['STATUS_ID']}'";
                     $stageCount++;
                     // Foolproof
                     if ($stageCount === 10) {
                         break;
                     }
                 }
             }
             unset($stage);
             global $DB;
             $dbResult = $DB->Query("SELECT COUNT(d.ID) AS CNT FROM {$dealTable} d WHERE d.ASSIGNED_BY_ID = {$this->userID} AND d.STAGE_ID NOT IN({$stageStr}) AND d.ID NOT IN(SELECT a.OWNER_ID FROM {$activityTable} a WHERE a.USER_ID = 0 AND a.OWNER_TYPE_ID = 2)", false, 'File: ' . __FILE__ . '<br/>Line: ' . __LINE__);
             $result = $dbResult->Fetch();
             $count += is_array($result) ? intval($result['CNT']) : 0;
         }
     } elseif ($this->typeID === self::CurrentQuoteActivies) {
         $count = 0;
         if (CCrmUserCounterSettings::GetValue(CCrmUserCounterSettings::ReckonActivitylessItems, true)) {
             $quoteTable = CCrmQuote::TABLE_NAME;
             $statusStr = "'APPROVED'";
             $statusCount = 1;
             $statuses = self::GetStatusList('QUOTE_STATUS');
             $isFound = false;
             foreach ($statuses as &$status) {
                 if (!$isFound) {
                     $isFound = $status['STATUS_ID'] === 'APPROVED';
                 } else {
                     $statusStr .= ",'{$status['STATUS_ID']}'";
                     $statusCount++;
                     // Foolproof
                     if ($statusCount === 10) {
                         break;
                     }
                 }
             }
             unset($status);
             global $DB;
             $currentDay = time() + CTimeZone::GetOffset();
             $currentDayEnd = ConvertTimeStamp(mktime(23, 59, 59, date('n', $currentDay), date('j', $currentDay), date('Y', $currentDay)), 'FULL', SITE_ID);
             $currentDayEnd = $DB->CharToDateFunction($DB->ForSql($currentDayEnd), 'FULL');
             $dbResult = $DB->Query("SELECT COUNT(q.ID) AS CNT FROM {$quoteTable} q WHERE q.ASSIGNED_BY_ID = {$this->userID} AND q.CLOSEDATE IS NOT NULL AND q.CLOSEDATE <= {$currentDayEnd} AND q.STATUS_ID NOT IN ({$statusStr})", false, 'File: ' . __FILE__ . '<br/>Line: ' . __LINE__);
             $result = $dbResult->Fetch();
             $count += is_array($result) ? intval($result['CNT']) : 0;
         }
     }
     if ($this->curValue !== $count) {
         $this->curValue = $count;
         if ($this->code !== '') {
             CUserCounter::Set($this->userID, $this->code, $this->curValue, SITE_ID, '', false);
         }
     }
     $this->RefreshLastCalculatedTime();
     return $this->curValue;
 }
Ejemplo n.º 5
0
	public static function SetUnreadCounter($userId)
	{
		return false;

		global $DB;

		$userId = intval($userId);
		if ($userId <= 0)
			return false;

		$sqlCounter = "SELECT COUNT(M.ID) as CNT
			FROM b_im_message M
			INNER JOIN b_im_relation R1 ON M.ID > R1.LAST_ID AND M.CHAT_ID = R1.CHAT_ID AND R1.USER_ID != M.AUTHOR_ID
			WHERE R1.USER_ID = ".$userId." AND R1.MESSAGE_TYPE = '".IM_MESSAGE_PRIVATE."' AND R1.STATUS < ".IM_STATUS_READ;
		$dbRes = $DB->Query($sqlCounter, false, "File: ".__FILE__."<br>Line: ".__LINE__);
		if ($row = $dbRes->Fetch())
			CUserCounter::Set($userId, 'im_message_v2', $row['CNT'], '**', false);
		else
			CUserCounter::Set($userId, 'im_message_v2', 0, '**', false);

		return true;
	}
Ejemplo n.º 6
0
 public static function SetUnreadCounter($userId)
 {
     return false;
     global $DB;
     $userId = intval($userId);
     if ($userId <= 0) {
         return false;
     }
     $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.ID > R1.LAST_ID AND M.CHAT_ID = R1.CHAT_ID\n\t\t\tWHERE R1.USER_ID = " . $userId . " AND R1.MESSAGE_TYPE = '" . IM_MESSAGE_SYSTEM . "' AND R1.STATUS < " . IM_STATUS_READ;
     $dbRes = $DB->Query($strSql, false, "File: " . __FILE__ . "<br>Line: " . __LINE__);
     if ($row = $dbRes->Fetch()) {
         CUserCounter::Set($userId, 'im_notify_v2', $row['CNT'], '**', false);
     } else {
         CUserCounter::Set($userId, 'im_notify_v2', 0, '**', false);
     }
     return true;
 }
Ejemplo n.º 7
0
    $arResult["H_NAV_STRING"] = $dbRecordsList->GetPageNavStringEx($navComponentObject, GetMessage("INTS_TASKS_NAV"), "", false);
    $arResult["H_NAV_CACHED_DATA"] = $navComponentObject->GetTemplateCachedData();
    $arResult["H_NAV_RESULT"] = $dbRecordsList;
}
if (strlen($arResult["FatalErrorMessage"]) <= 0) {
    if (!$arParams['COUNTERS_ONLY']) {
        if ($arParams["SET_TITLE"] == "Y") {
            $APPLICATION->SetTitle(GetMessage("BPABS_TITLE"));
        }
        if ($arParams["SET_NAV_CHAIN"] == "Y") {
            $APPLICATION->AddChainItem(GetMessage("BPABS_TITLE"));
        }
    }
    $arResult['COUNTERS'] = CBPTaskService::getCounters($targetUserId);
    if ($arParams['COUNTERS_ONLY']) {
        $arResult['COUNTERS_RUNNING'] = CBPStateService::getRunningCounters($targetUserId);
    }
    //counter autofixer
    $currentCounter = (int) CUserCounter::GetValue($targetUserId, 'bp_tasks', '**');
    if (isset($arResult['COUNTERS']['*']) && $currentCounter != $arResult['COUNTERS']['*']) {
        CUserCounter::Set($targetUserId, 'bp_tasks', $arResult['COUNTERS']['*'], '**');
    }
} elseif (!$arParams['COUNTERS_ONLY']) {
    if ($arParams["SET_TITLE"] == "Y") {
        $APPLICATION->SetTitle(GetMessage("BPWC_WLC_ERROR"));
    }
    if ($arParams["SET_NAV_CHAIN"] == "Y") {
        $APPLICATION->AddChainItem(GetMessage("BPWC_WLC_ERROR"));
    }
}
$this->IncludeComponentTemplate();
Ejemplo n.º 8
0
 public static function Set($code, $value, $site_id = SITE_ID, $tag = '', $sendPull = true)
 {
     return CUserCounter::Set(CUserCounter::SYSTEM_USER_ID, $code, $value, $site_id, $tag, $sendPull);
 }
 private static function recountCounters_ORIGINATORS_WITHOUT_DEADLINES($userId)
 {
     global $DB;
     $DB->startTransaction();
     // Count tasks without DEADLINES
     // not in states: CTasks::STATE_SUPPOSEDLY_COMPLETED, CTasks::STATE_COMPLETED, CTasks::STATE_DECLINED
     // and where CREATED_BY != RESPONSIBLE_ID.
     // Count tasks for originators.
     $strSql = "SELECT COUNT(T.ID) AS T_WO_DEADLINES, \n\t\t\t\tT.CREATED_BY AS CREATED_BY\n\t\t\tFROM b_tasks T\n\t\t\tWHERE \n\t\t\t\tT.ZOMBIE = 'N'\n\t\t\t\tAND T.CREATED_BY != T.RESPONSIBLE_ID\n\t\t\t\tAND T.CREATED_BY != 0\n\t\t\t\tAND T.RESPONSIBLE_ID != 0\n\t\t\t\tAND T.DEADLINE IS NULL\n\t\t\t\tAND T.STATUS != " . CTasks::STATE_DECLINED . "\n\t\t\t\tAND T.STATUS != " . CTasks::STATE_SUPPOSEDLY_COMPLETED . "\n\t\t\t\tAND T.STATUS != " . CTasks::STATE_COMPLETED . "\n\t\t\t";
     if ($userId !== '*') {
         // All users or not?
         $strSql .= " AND T.CREATED_BY = " . (int) $userId;
     }
     $strSql .= " GROUP BY T.CREATED_BY";
     $rc = $DB->query($strSql);
     $i = 0;
     while ($ar = $rc->fetch()) {
         $arValues = CUserCounter::GetValues($ar['CREATED_BY'], $site_id = '**');
         $total = 0;
         if (isset($arValues[CTaskCountersProcessor::COUNTER_TASKS_TOTAL])) {
             $total = (int) $arValues[CTaskCountersProcessor::COUNTER_TASKS_TOTAL];
         }
         $subtotal = 0;
         if (isset($arValues[CTaskCountersProcessor::COUNTER_TASKS_ORIGINATOR])) {
             $subtotal = (int) $arValues[CTaskCountersProcessor::COUNTER_TASKS_ORIGINATOR];
         }
         CUserCounter::Set((int) $ar['CREATED_BY'], CTaskCountersProcessor::COUNTER_TASKS_ORIGINATOR_WO_DEADLINE, (int) $ar['T_WO_DEADLINES'], '**', $tag = '', false);
         CUserCounter::Set((int) $ar['CREATED_BY'], CTaskCountersProcessor::COUNTER_TASKS_ORIGINATOR, $subtotal + (int) $ar['T_WO_DEADLINES'], '**', $tag = '', false);
         CUserCounter::Set((int) $ar['CREATED_BY'], CTaskCountersProcessor::COUNTER_TASKS_TOTAL, $total + (int) $ar['T_WO_DEADLINES'], '**', $tag = '', false);
         // commit on after every 100 users have been processed
         if (!(++$i % 100)) {
             soundex('commit every 100 users');
             $DB->commit();
             $DB->startTransaction();
         }
     }
     $DB->commit();
 }
Ejemplo n.º 10
0
 private function redirect()
 {
     global $APPLICATION, $USER;
     $services = CIntranetMailSetupHelper::getMailServices();
     $mailbox = CIntranetMailSetupHelper::getUserMailbox($USER->GetID());
     $serviceId = $mailbox['SERVICE_ID'];
     $error = false;
     switch ($services[$serviceId]['type']) {
         case 'imap':
             if (!empty($services[$serviceId]['link'])) {
                 LocalRedirect($services[$serviceId]['link'], true);
             } else {
                 if (!empty($mailbox['LINK'])) {
                     LocalRedirect($mailbox['LINK'], true);
                 } else {
                     LocalRedirect($APPLICATION->GetCurPage() . '?page=home');
                 }
             }
             break;
         case 'controller':
             list($login, $domain) = explode('@', $mailbox['LOGIN'], 2);
             $crRedirect = CControllerClient::ExecuteEvent('OnMailControllerRedirect', array('LOCALE' => LANGUAGE_ID, 'DOMAIN' => $domain, 'NAME' => $login));
             if (isset($crRedirect['result'])) {
                 LocalRedirect($crRedirect['result'], true);
             } else {
                 $error = empty($crRedirect['error']) ? GetMessage('INTR_MAIL_CONTROLLER_INVALID') : CMail::getErrorMessage($crRedirect['error']);
             }
             break;
         case 'domain':
             list($login, $domain) = explode('@', $mailbox['LOGIN'], 2);
             $result = CMailDomain2::getRedirectUrl(LANGUAGE_ID, $services[$serviceId]['token'], $domain, $login, $errorUrl = '', $error);
             if (is_null($result)) {
                 $error = CMail::getErrorMessage($error);
             } else {
                 LocalRedirect($result, true);
             }
             break;
         case 'crdomain':
             list($login, $domain) = explode('@', $mailbox['LOGIN'], 2);
             $crRedirect = CControllerClient::ExecuteEvent('OnMailControllerMemberRedirect', array('LOCALE' => LANGUAGE_ID, 'DOMAIN' => $domain, 'NAME' => $login));
             if (isset($crRedirect['result'])) {
                 LocalRedirect($crRedirect['result'], true);
             } else {
                 $error = empty($crRedirect['error']) ? GetMessage('INTR_MAIL_CONTROLLER_INVALID') : CMail::getErrorMessage($crRedirect['error']);
             }
             break;
         default:
             LocalRedirect($APPLICATION->GetCurPage() . '?page=home');
     }
     if ($error !== false) {
         CUserCounter::Set($USER->GetID(), 'mail_unseen', -1, SITE_ID);
         CUserOptions::SetOption('global', 'last_mail_check_' . SITE_ID, time());
         CUserOptions::SetOption('global', 'last_mail_check_success_' . SITE_ID, false);
         LocalRedirect($APPLICATION->GetCurPage() . '?page=home');
     }
 }
Ejemplo n.º 11
0
 private static function executeCheck(&$error)
 {
     global $USER;
     $error = false;
     $unseen = -1;
     if (!check_bitrix_sessid()) {
         $error = GetMessage('INTR_MAIL_CSRF');
     }
     if ($error === false) {
         $mailbox = CIntranetMailSetupHelper::getUserMailbox($USER->GetID());
         if (empty($mailbox)) {
             $error = GetMessage('INTR_MAIL_AJAX_ERROR');
         }
     }
     if ($error === false) {
         switch ($mailbox['SERVER_TYPE']) {
             case 'imap':
                 $unseen = CMailUtil::CheckImapMailbox($mailbox['SERVER'], $mailbox['PORT'], $mailbox['USE_TLS'], $mailbox['LOGIN'], $mailbox['PASSWORD'], $error, 30);
                 break;
             case 'controller':
                 list($login, $domain) = explode('@', $mailbox['LOGIN'], 2);
                 $crCheckMailbox = CControllerClient::ExecuteEvent('OnMailControllerCheckMailbox', array('DOMAIN' => $domain, 'NAME' => $login));
                 if (isset($crCheckMailbox['result'])) {
                     $unseen = intval($crCheckMailbox['result']);
                 } else {
                     $error = empty($crCheckMailbox['error']) ? GetMessage('INTR_MAIL_CONTROLLER_INVALID') : CMail::getErrorMessage($crCheckMailbox['error']);
                 }
                 break;
             case 'crdomain':
                 list($login, $domain) = explode('@', $mailbox['LOGIN'], 2);
                 $crCheckMailbox = CControllerClient::ExecuteEvent('OnMailControllerCheckMemberMailbox', array('DOMAIN' => $domain, 'NAME' => $login));
                 if (isset($crCheckMailbox['result'])) {
                     $unseen = intval($crCheckMailbox['result']);
                 } else {
                     $error = empty($crCheckMailbox['error']) ? GetMessage('INTR_MAIL_CONTROLLER_INVALID') : CMail::getErrorMessage($crCheckMailbox['error']);
                 }
                 break;
             case 'domain':
                 $serviceId = $mailbox['SERVICE_ID'];
                 $services = CIntranetMailSetupHelper::getMailServices();
                 list($login, $domain) = explode('@', $mailbox['LOGIN'], 2);
                 $result = CMailDomain2::getUnreadMessagesCount($services[$serviceId]['token'], $domain, $login, $error);
                 if (is_null($result)) {
                     $error = CMail::getErrorMessage($error);
                 } else {
                     $unseen = intval($result);
                 }
                 break;
         }
         CUserCounter::Set($USER->GetID(), 'mail_unseen', $unseen, SITE_ID);
         CUserOptions::SetOption('global', 'last_mail_check_' . SITE_ID, time());
         CUserOptions::SetOption('global', 'last_mail_check_success_' . SITE_ID, $unseen >= 0);
     }
     return array('unseen' => $unseen);
 }
Ejemplo n.º 12
0
 public static function DataSync($paramUserId = 0)
 {
     $exchangeScheme = COption::GetOptionString("dav", "exchange_scheme", "http");
     $exchangeServer = COption::GetOptionString("dav", "exchange_server", "");
     $exchangePort = COption::GetOptionString("dav", "exchange_port", "80");
     $exchangeUsername = COption::GetOptionString("dav", "exchange_username", "");
     $exchangePassword = COption::GetOptionString("dav", "exchange_password", "");
     if (empty($exchangeServer)) {
         CAgent::RemoveAgent("CDavExchangeMail::DataSync();", "dav");
         COption::SetOptionString("dav", "agent_mail", "N");
         return "";
     }
     $exchange = new CDavExchangeMail($exchangeScheme, $exchangeServer, $exchangePort, $exchangeUsername, $exchangePassword);
     //$exchange->Debug();
     $exchangeMailbox = COption::GetOptionString("dav", "exchange_mailbox", "");
     $exchangeUseLogin = COption::GetOptionString("dav", "exchange_use_login", "Y");
     self::InitUserEntity();
     $maxNumber = 5;
     $index = 0;
     $paramUserId = intval($paramUserId);
     $arUserFilter = array("ACTIVE" => "Y", "!UF_DEPARTMENT" => false);
     if ($paramUserId > 0) {
         $arUserFilter["ID_EQUAL_EXACT"] = $paramUserId;
     }
     if ($exchangeUseLogin == "N") {
         $arUserFilter["!UF_BXDAVEX_MAILBOX"] = false;
     }
     $dbUserList = CUser::GetList($by = "UF_BXDAVEX_MLSYNC", $order = "asc", $arUserFilter, array('SELECT' => array('UF_BXDAVEX_MAILBOX', 'UF_BXDAVEX_MLSYNC'), 'FIELDS' => array('ID', 'LOGIN')));
     while ($arUser = $dbUserList->Fetch()) {
         $index++;
         if ($index > $maxNumber) {
             break;
         }
         $GLOBALS["USER_FIELD_MANAGER"]->Update("USER", $arUser["ID"], array("UF_BXDAVEX_MLSYNC" => ConvertTimeStamp(time(), FULL)));
         $mailbox = $exchangeUseLogin == "Y" ? $arUser["LOGIN"] . $exchangeMailbox : $arUser["UF_BXDAVEX_MAILBOX"];
         if (empty($mailbox)) {
             continue;
         }
         $numberOfUnread = 0;
         $arInbox = $exchange->GetFolderById(array("XML_ID" => "inbox", "Mailbox" => $mailbox));
         if (is_array($arInbox)) {
             foreach ($arInbox as $inbox) {
                 if (isset($inbox["UNREAD_COUNT"])) {
                     $numberOfUnread += intval($inbox["UNREAD_COUNT"]);
                 }
                 $arInbox1 = $exchange->GetFoldersList(array("XML_ID" => $inbox["XML_ID"], "Mailbox" => $mailbox));
                 if (is_array($arInbox1)) {
                     foreach ($arInbox1 as $inbox1) {
                         if (isset($inbox1["UNREAD_COUNT"])) {
                             $numberOfUnread += intval($inbox1["UNREAD_COUNT"]);
                         }
                     }
                 }
             }
         }
         $GLOBALS["USER_FIELD_MANAGER"]->Update("USER", $arUser["ID"], array("UF_UNREAD_MAIL_COUNT" => $numberOfUnread));
         CUserCounter::Set($arUser["ID"], 'dav_unread_mail', $numberOfUnread, '**');
     }
     return "CDavExchangeMail::DataSync();";
 }