• Модуль: dav
  • Путь к файлу: ~/bitrix/modules/dav/classes/general/exchangecontacts.php
  • Класс: CDavExchangeContacts
  • Вызов: CDavExchangeContacts::DataSync
static function DataSync($paramUserId = 0)
{
	if (DAV_EXCH_DEBUG)
		CDav::WriteToLog("Starting EXCHANGE contacts sync...", "SC");

	$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)/* || (COption::GetOptionString("dav", "agent_contacts", "N") != "Y")*/)
	{
		CAgent::RemoveAgent("CDavExchangeContacts::DataSync();", "dav");
		COption::SetOptionString("dav", "agent_contacts", "N");
		return "";
	}

	$exchange = new CDavExchangeContacts($exchangeScheme, $exchangeServer, $exchangePort, $exchangeUsername, $exchangePassword);

	if (GW_DEBUG)
		$exchange->Debug();

	$exchangeMailbox = COption::GetOptionString("dav", "exchange_mailbox", "");
	$exchangeUseLogin = COption::GetOptionString("dav", "exchange_use_login", "Y");

	self::InitUserEntity();

	$maxNumber = 5;
	$index = 0;
	$rootStructureName = null;

	$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("UF_BXDAVEX_CNTSYNC", "asc", $arUserFilter, array("SELECT" => array("ID", "LOGIN", "UF_BXDAVEX_MAILBOX", "UF_BXDAVEX_CNTSYNC")));
	while ($arUser = $dbUserList->Fetch())
	{
		$index++;
		if ($index > $maxNumber)
			break;

		if (DAV_EXCH_DEBUG)
			CDav::WriteToLog("Processing user [".$arUser["ID"]."] ".$arUser["LOGIN"], "SC");

		$lastSyncDate = $arUser["UF_BXDAVEX_CNTSYNC"];
		if (empty($lastSyncDate))
			$lastSyncDate = ConvertTimeStamp(mktime(0, 0, 0, 1, 1, 2000), "FULL");

		$GLOBALS["USER_FIELD_MANAGER"]->Update("USER", $arUser["ID"], array("UF_BXDAVEX_CNTSYNC" => ConvertTimeStamp(time(), "FULL")));

		$mailbox = (($exchangeUseLogin === "Y") ? $arUser["LOGIN"].$exchangeMailbox : $arUser["UF_BXDAVEX_MAILBOX"]);
		if (empty($mailbox))
			continue;

		$tmpNumItems = 0;
		$arAddressbookCache = null;

		$dbUserListTmp = CUser::GetList(
			"ID",
			"asc",
			array("TIMESTAMP_X_1" => $lastSyncDate, "ACTIVE" => "Y", "!UF_DEPARTMENT" => false)
		);
		if ($arUserTmp = $dbUserListTmp->Fetch())
		{
			if (is_null($rootStructureName))
			{
				$iblockStructure = COption::GetOptionInt("intranet", 'iblock_structure', 0);
				$db = CIBlockSection::GetList(array("LEFT_MARGIN" => "ASC"), array("IBLOCK_ID" => $iblockStructure));
				if ($ar = $db->Fetch())
					$rootStructureName = $ar["NAME"];
			}
			if (is_null($rootStructureName))
			{
				if (DAV_EXCH_DEBUG)
					CDav::WriteToLog("ERROR: Root structure is not found", "SC");
				break;
			}

			$arAddressbooksList = $exchange->GetAddressbooksList(array("mailbox" => $mailbox));

			$arErrorsTmp = $exchange->GetErrors();
			if (count($arErrorsTmp) > 0)
			{
				$txt = '';
				foreach ($arErrorsTmp as $v)
				{
					if (!empty($txt))
						$txt .= ", ";
					$txt .= "[".$v[0]."] ".$v[1];
				}
				if (DAV_EXCH_DEBUG)
					CDav::WriteToLog("ERROR: ".$txt, "SC");

				continue;
			}

			$arAddressbook = null;
			foreach ($arAddressbooksList as $value)
			{
				if ($value["NAME"] == $rootStructureName)
				{
					$arAddressbook = $value;
					break;
				}
			}

			if (is_null($arAddressbook))
				$arAddressbook = $exchange->AddAddressbook(array("mailbox" => $mailbox, "NAME" => $rootStructureName));

			if (!is_array($arAddressbook) || !isset($arAddressbook["XML_ID"]))
			{
				if (DAV_EXCH_DEBUG)
					CDav::WriteToLog("ERROR: Addressbook '".$rootStructureName."' for mailbox '".$mailbox."' is not found", "SC");
				continue;
			}

			if (is_null($arAddressbookCache))
			{
				$arAddressbookCache = array();

				$arPrs = $exchange->GetList(
					array("Mailbox" => $mailbox, "AddressbookId" => $arAddressbook["XML_ID"]),
					array("ItemShape" => "IdOnly", "AdditionalProperties" => array("contacts:GivenName", "contacts:MiddleName", "contacts:Surname"))
				);
				foreach ($arPrs as $prs)
				{
					$s = $prs["NAME"]."/".$prs["SECOND_NAME"]."/".$prs["LAST_NAME"];
					$arAddressbookCache[$s] = array(
						"XML_ID" => $prs["XML_ID"],
						"MODIFICATION_LABEL" => $prs["MODIFICATION_LABEL"]
					);
				}
			}

			do
			{
				$s = $arUserTmp["NAME"]."/".$arUserTmp["SECOND_NAME"]."/".$arUserTmp["LAST_NAME"];
				if (array_key_exists($s, $arAddressbookCache))
				{
					$exchange->Update(
						$arAddressbookCache[$s],
						array_merge($arUserTmp, array("Mailbox" => $mailbox, "AddressbookId" => $arAddressbook["XML_ID"]))
					);
				}
				else
				{
					$exchange->Add(array_merge($arUserTmp, array("Mailbox" => $mailbox, "AddressbookId" => $arAddressbook["XML_ID"])));
				}

				$arErrorsTmp = $exchange->GetErrors();
				if (count($arErrorsTmp) > 0)
				{
					$txt = '';
					foreach ($arErrorsTmp as $v)
					{
						if (!empty($txt))
							$txt .= ", ";
						$txt .= "[".$v[0]."] ".$v[1];
					}
					if (DAV_EXCH_DEBUG)
						CDav::WriteToLog("ERROR: ".$txt, "SC");
				}

				$tmpNumItems++;
			}
			while ($arUserTmp = $dbUserListTmp->Fetch());
		}

		if (DAV_EXCH_DEBUG)
			CDav::WriteToLog("Sync ".intval($tmpNumItems)." items", "SC");
	}

	if (DAV_EXCH_DEBUG)
		CDav::WriteToLog("EXCHANGE contacts sync finished", "SC");

	return "CDavExchangeContacts::DataSync();";
}