Esempio n. 1
0
 public static function Test($scheme, $server, $port, $userName, $userPassword, $mailbox, $arPFolders = array(), $bDebug = false)
 {
     $arAvailableFolders = array("calendar", "contacts", "tasks");
     if (!is_array($arPFolders)) {
         $arPFolders = array($arPFolders);
     }
     $arFolders = array();
     foreach ($arPFolders as $value) {
         if (in_array($value, $arAvailableFolders)) {
             $arFolders[] = $value;
         }
     }
     if (count($arFolders) <= 0) {
         $arFolders = $arAvailableFolders;
     }
     $arMessages = array();
     if (in_array("calendar", $arFolders)) {
         $e = new CDavExchangeCalendar($scheme, $server, $port, $userName, $userPassword);
         if ($bDebug) {
             $e->Debug();
         }
         $calendarId = null;
         $r = $e->AddCalendar(array("NAME" => "TestCalendarFolderName4", "MAILBOX" => $mailbox));
         $arMessages = array_merge($arMessages, $e->GetErrors());
         foreach ($r as $v) {
             $calendarId = $v;
         }
         if (is_null($calendarId)) {
             $arMessages[] = array("ERROR", "Calendar folder creation error.");
         } else {
             $arMessages[] = array("SUCCESS", "Calendar folder was successfully created (Id = " . $calendarId["XML_ID"] . ", ChangeKey = " . $calendarId["MODIFICATION_LABEL"] . ").");
             $r = $e->UpdateCalendar($calendarId, array("NAME" => "New TestCalendarFolderName4"));
             $arMessages = array_merge($arMessages, $e->GetErrors());
             $calendarId = null;
             foreach ($r as $v) {
                 $calendarId = $v;
             }
             if (is_null($calendarId)) {
                 $arMessages[] = array("ERROR", "Calendar folder modification error.");
             } else {
                 $arMessages[] = array("SUCCESS", "Calendar folder was successfully updated (Id = " . $calendarId["XML_ID"] . ", ChangeKey = " . $calendarId["MODIFICATION_LABEL"] . ").");
                 $bFound = false;
                 $r = $e->GetCalendarsList(array("MAILBOX" => $mailbox));
                 $arMessages = array_merge($arMessages, $e->GetErrors());
                 foreach ($r as $v) {
                     if ($v["NAME"] == "New TestCalendarFolderName4" && $v["XML_ID"] == $calendarId["XML_ID"]) {
                         $bFound = true;
                     }
                 }
                 if (!$bFound) {
                     $arMessages[] = array("ERROR", "Calendar folder is not found by list.");
                 } else {
                     $arMessages[] = array("SUCCESS", "Calendar folder was successfully found by list.");
                     $bFound = false;
                     $r = $e->GetCalendarById($calendarId["XML_ID"]);
                     $arMessages = array_merge($arMessages, $e->GetErrors());
                     foreach ($r as $v) {
                         if ($v["NAME"] == "New TestCalendarFolderName4" && $v["XML_ID"] == $calendarId["XML_ID"]) {
                             $bFound = true;
                         }
                     }
                     if (!$bFound) {
                         $arMessages[] = array("ERROR", "Calendar folder is not found by id.");
                     } else {
                         $arMessages[] = array("SUCCESS", "Calendar folder was successfully found by id.");
                         $itemId = null;
                         $arFields = array("MAILBOX" => $mailbox, "CALENDAR_ID" => $calendarId["XML_ID"], "NAME" => "TestCalendarItem Name", "DETAIL_TEXT" => "TestCalendarItem detail text", "DETAIL_TEXT_TYPE" => "html", "PROPERTY_IMPORTANCE" => "normal", "PROPERTY_REMIND_SETTINGS" => "20_min", "ACTIVE_FROM" => "16.10.2011 09:00:00", "ACTIVE_TO" => "16.10.2011 09:30:00", "PROPERTY_LOCATION" => "Some location");
                         $r = $e->Add($arFields);
                         $arMessages = array_merge($arMessages, $e->GetErrors());
                         foreach ($r as $v) {
                             $itemId = $v;
                         }
                         if (is_null($itemId)) {
                             $arMessages[] = array("ERROR", "Calendar item creation error.");
                         } else {
                             $arMessages[] = array("SUCCESS", "Calendar item was successfully created (Id = " . $itemId["XML_ID"] . ", ChangeKey = " . $itemId["MODIFICATION_LABEL"] . ").");
                             $arFields = array("NAME" => "New TestCalendarItem Name", "DETAIL_TEXT" => "TestCalendarItem detail text", "DETAIL_TEXT_TYPE" => "text", "ACTIVE_FROM" => "17.10.2011 09:00:00", "ACTIVE_TO" => "17.10.2011 09:30:00", "PROPERTY_LOCATION" => "Some new location");
                             $r = $e->Update($itemId, $arFields);
                             $itemId = null;
                             $arMessages = array_merge($arMessages, $e->GetErrors());
                             foreach ($r as $v) {
                                 $itemId = $v;
                             }
                             if (is_null($itemId)) {
                                 $arMessages[] = array("ERROR", "Calendar item modification error.");
                             } else {
                                 $arMessages[] = array("SUCCESS", "Calendar item was successfully updated (Id = " . $itemId["XML_ID"] . ", ChangeKey = " . $itemId["MODIFICATION_LABEL"] . ").");
                                 $bFound = false;
                                 $r = $e->GetList(array("Mailbox" => $mailbox, "CalendarId" => $calendarId["XML_ID"]), array("ItemShape" => "AllProperties"));
                                 $arMessages = array_merge($arMessages, $e->GetErrors());
                                 foreach ($r as $v) {
                                     if ($v["NAME"] == "New TestCalendarItem Name" && $v["XML_ID"] == $itemId["XML_ID"]) {
                                         $bFound = true;
                                     }
                                 }
                                 if (!$bFound) {
                                     $arMessages[] = array("ERROR", "Calendar item is not found by list.");
                                 } else {
                                     $arMessages[] = array("SUCCESS", "Calendar item was successfully found by list.");
                                     $bFound = false;
                                     $r = $e->GetById($itemId["XML_ID"]);
                                     $arMessages = array_merge($arMessages, $e->GetErrors());
                                     foreach ($r as $v) {
                                         if ($v["NAME"] == "New TestCalendarItem Name" && $v["XML_ID"] == $itemId["XML_ID"]) {
                                             $bFound = true;
                                         }
                                     }
                                     if (!$bFound) {
                                         $arMessages[] = array("ERROR", "Calendar item is not found by id.");
                                     } else {
                                         $arMessages[] = array("SUCCESS", "Calendar item was successfully found by id.");
                                         $r = $e->Delete($itemId["XML_ID"]);
                                         if (!$r) {
                                             $arMessages[] = array("ERROR", "Calendar item deletion error.");
                                         } else {
                                             $arMessages[] = array("SUCCESS", "Calendar item was successfully deleted.");
                                             $bFound = false;
                                             $r = $e->GetList(array("Mailbox" => $mailbox, "CalendarId" => $calendarId["XML_ID"]), array("ItemShape" => "IdOnly"));
                                             $arMessages = array_merge($arMessages, $e->GetErrors());
                                             foreach ($r as $v) {
                                                 $bFound = true;
                                             }
                                             if ($bFound) {
                                                 $arMessages[] = array("ERROR", "Calendar folder should be empty.");
                                             } else {
                                                 $arMessages[] = array("SUCCESS", "Calendar folder is empty.");
                                                 $r = $e->DeleteCalendar($calendarId["XML_ID"]);
                                                 $arMessages = array_merge($arMessages, $e->GetErrors());
                                                 if ($r) {
                                                     $arMessages[] = array("SUCCESS", "Calendar folder was successfully deleted.");
                                                 } else {
                                                     $arMessages[] = array("ERROR", "Calendar folder deletion error.");
                                                 }
                                             }
                                         }
                                     }
                                 }
                             }
                         }
                     }
                 }
             }
         }
     }
     if (in_array("contacts", $arFolders)) {
         $e = new CDavExchangeContacts($scheme, $server, $port, $userName, $userPassword);
         if ($bDebug) {
             $e->Debug();
         }
         $addressbookId = null;
         $r = $e->AddAddressbook(array("NAME" => "TestAddressbookFolderName4", "MAILBOX" => $mailbox));
         $arMessages = array_merge($arMessages, $e->GetErrors());
         foreach ($r as $v) {
             $addressbookId = $v;
         }
         if (is_null($addressbookId)) {
             $arMessages[] = array("ERROR", "Contacts folder creation error.");
         } else {
             $arMessages[] = array("SUCCESS", "Contacts folder was successfully created (Id = " . $addressbookId["XML_ID"] . ", ChangeKey = " . $addressbookId["MODIFICATION_LABEL"] . ").");
             $r = $e->UpdateAddressbook($addressbookId, array("NAME" => "New TestAddressbookFolderName4"));
             $arMessages = array_merge($arMessages, $e->GetErrors());
             $addressbookId = null;
             foreach ($r as $v) {
                 $addressbookId = $v;
             }
             if (is_null($addressbookId)) {
                 $arMessages[] = array("ERROR", "Contacts folder modification error.");
             } else {
                 $arMessages[] = array("SUCCESS", "Contacts folder was successfully updated (Id = " . $addressbookId["XML_ID"] . ", ChangeKey = " . $addressbookId["MODIFICATION_LABEL"] . ").");
                 $bFound = false;
                 $r = $e->GetAddressbooksList(array("MAILBOX" => $mailbox));
                 $arMessages = array_merge($arMessages, $e->GetErrors());
                 foreach ($r as $v) {
                     if ($v["NAME"] == "New TestAddressbookFolderName4" && $v["XML_ID"] == $addressbookId["XML_ID"]) {
                         $bFound = true;
                     }
                 }
                 if (!$bFound) {
                     $arMessages[] = array("ERROR", "Contacts folder is not found by list.");
                 } else {
                     $arMessages[] = array("SUCCESS", "Contacts folder was successfully found by list.");
                     $bFound = false;
                     $r = $e->GetAddressbookById($addressbookId["XML_ID"]);
                     $arMessages = array_merge($arMessages, $e->GetErrors());
                     foreach ($r as $v) {
                         if ($v["NAME"] == "New TestAddressbookFolderName4" && $v["XML_ID"] == $addressbookId["XML_ID"]) {
                             $bFound = true;
                         }
                     }
                     if (!$bFound) {
                         $arMessages[] = array("ERROR", "Contacts folder is not found by id.");
                     } else {
                         $arMessages[] = array("SUCCESS", "Contacts folder was successfully found by id.");
                         $itemId = null;
                         $arFields = array("MAILBOX" => $mailbox, "ADDRESSBOOK_ID" => $addressbookId["XML_ID"], "NAME" => "TestAddressbookItem Name", "LAST_NAME" => "MyLastName", "SECOND_NAME" => "MySecondName", "EMAIL" => "*****@*****.**", "WORK_POSITION" => "Programmer", "WORK_ZIP" => "236001", "WORK_CITY" => "Kaliningrad", "WORK_STREET" => "Kirov str., 261", "PERSONAL_PHONE" => "6547646546", "PERSONAL_MOBILE" => "55435656", "WORK_PHONE" => "876467343", "WORK_FAX" => "345737365", "WORK_COMPANY" => "Bitrix", "WORK_WWW" => "http://www.1c-bitrix.com", "PERSONAL_ICQ" => "535435353", "WORK_COUNTRY" => 23);
                         $r = $e->Add($arFields);
                         $arMessages = array_merge($arMessages, $e->GetErrors());
                         foreach ($r as $v) {
                             $itemId = $v;
                         }
                         if (is_null($itemId)) {
                             $arMessages[] = array("ERROR", "Contacts item creation error.");
                         } else {
                             $arMessages[] = array("SUCCESS", "Contacts item was successfully created (Id = " . $itemId["XML_ID"] . ", ChangeKey = " . $itemId["MODIFICATION_LABEL"] . ").");
                             $arFields = array("NAME" => "New TestAddressbookItem Name", "LAST_NAME" => "My new LastName", "SECOND_NAME" => "MySecondName", "EMAIL" => "*****@*****.**", "WORK_POSITION" => "Programmer", "WORK_ZIP" => "236001", "WORK_CITY" => "Kaliningrad", "WORK_STREET" => "Kirov str., 261", "PERSONAL_PHONE" => "6547646546", "PERSONAL_MOBILE" => "55435656", "WORK_PHONE" => "876467343", "WORK_FAX" => "345737365", "WORK_COMPANY" => "Bitrix", "WORK_WWW" => "http://www.1c-bitrix.com", "PERSONAL_ICQ" => "535435353", "WORK_COUNTRY" => 23);
                             $r = $e->Update($itemId, $arFields);
                             $itemId = null;
                             $arMessages = array_merge($arMessages, $e->GetErrors());
                             foreach ($r as $v) {
                                 $itemId = $v;
                             }
                             if (is_null($itemId)) {
                                 $arMessages[] = array("ERROR", "Contacts item modification error.");
                             } else {
                                 $arMessages[] = array("SUCCESS", "Contacts item was successfully updated (Id = " . $itemId["XML_ID"] . ", ChangeKey = " . $itemId["MODIFICATION_LABEL"] . ").");
                                 $bFound = false;
                                 $r = $e->GetList(array("Mailbox" => $mailbox, "AddressbookId" => $addressbookId["XML_ID"]), array("ItemShape" => "AllProperties"));
                                 $arMessages = array_merge($arMessages, $e->GetErrors());
                                 foreach ($r as $v) {
                                     if ($v["NAME"] == "New TestAddressbookItem Name" && $v["XML_ID"] == $itemId["XML_ID"]) {
                                         $bFound = true;
                                     }
                                 }
                                 if (!$bFound) {
                                     $arMessages[] = array("ERROR", "Contacts item is not found by list.");
                                 } else {
                                     $arMessages[] = array("SUCCESS", "Contacts item was successfully found by list.");
                                     $bFound = false;
                                     $r = $e->GetById($itemId["XML_ID"]);
                                     $arMessages = array_merge($arMessages, $e->GetErrors());
                                     foreach ($r as $v) {
                                         if ($v["NAME"] == "New TestAddressbookItem Name" && $v["XML_ID"] == $itemId["XML_ID"]) {
                                             $bFound = true;
                                         }
                                     }
                                     if (!$bFound) {
                                         $arMessages[] = array("ERROR", "Contacts item is not found by id.");
                                     } else {
                                         $arMessages[] = array("SUCCESS", "Contacts item was successfully found by id.");
                                         $r = $e->Delete($itemId["XML_ID"]);
                                         if (!$r) {
                                             $arMessages[] = array("ERROR", "Contacts item deletion error.");
                                         } else {
                                             $arMessages[] = array("SUCCESS", "Contacts item was successfully deleted.");
                                             $bFound = false;
                                             $r = $e->GetList(array("Mailbox" => $mailbox, "AddressbookId" => $addressbookId["XML_ID"]), array("ItemShape" => "IdOnly"));
                                             $arMessages = array_merge($arMessages, $e->GetErrors());
                                             foreach ($r as $v) {
                                                 $bFound = true;
                                             }
                                             if ($bFound) {
                                                 $arMessages[] = array("ERROR", "Contacts folder should be empty.");
                                             } else {
                                                 $arMessages[] = array("SUCCESS", "Contacts folder is empty.");
                                                 $r = $e->DeleteAddressbook($addressbookId["XML_ID"]);
                                                 $arMessages = array_merge($arMessages, $e->GetErrors());
                                                 if ($r) {
                                                     $arMessages[] = array("SUCCESS", "Contacts folder was successfully deleted.");
                                                 } else {
                                                     $arMessages[] = array("ERROR", "Contacts folder deletion error.");
                                                 }
                                             }
                                         }
                                     }
                                 }
                             }
                         }
                     }
                 }
             }
         }
     }
     if (in_array("tasks", $arFolders)) {
         $e = new CDavExchangeTasks($scheme, $server, $port, $userName, $userPassword);
         if ($bDebug) {
             $e->Debug();
         }
         $folderId = null;
         $r = $e->AddFolder(array("NAME" => "TestFolderFolderName6", "MAILBOX" => $mailbox));
         $arMessages = array_merge($arMessages, $e->GetErrors());
         foreach ($r as $v) {
             $folderId = $v;
         }
         if (is_null($folderId)) {
             $arMessages[] = array("ERROR", "Tasks folder creation error.");
         } else {
             $arMessages[] = array("SUCCESS", "Tasks folder was successfully created (Id = " . $folderId["XML_ID"] . ", ChangeKey = " . $folderId["MODIFICATION_LABEL"] . ").");
             $r = $e->UpdateFolder($folderId, array("NAME" => "New TestFolderFolderName6"));
             $arMessages = array_merge($arMessages, $e->GetErrors());
             $folderId = null;
             foreach ($r as $v) {
                 $folderId = $v;
             }
             if (is_null($folderId)) {
                 $arMessages[] = array("ERROR", "Tasks folder modification error.");
             } else {
                 $arMessages[] = array("SUCCESS", "Tasks folder was successfully updated (Id = " . $folderId["XML_ID"] . ", ChangeKey = " . $folderId["MODIFICATION_LABEL"] . ").");
                 $bFound = false;
                 $r = $e->GetFoldersList(array("MAILBOX" => $mailbox));
                 $arMessages = array_merge($arMessages, $e->GetErrors());
                 foreach ($r as $v) {
                     if ($v["NAME"] == "New TestFolderFolderName6" && $v["XML_ID"] == $folderId["XML_ID"]) {
                         $bFound = true;
                     }
                 }
                 if (!$bFound) {
                     $arMessages[] = array("ERROR", "Tasks folder is not found by list.");
                 } else {
                     $arMessages[] = array("SUCCESS", "Tasks folder was successfully found by list.");
                     $bFound = false;
                     $r = $e->GetFolderById($folderId["XML_ID"]);
                     $arMessages = array_merge($arMessages, $e->GetErrors());
                     foreach ($r as $v) {
                         if ($v["NAME"] == "New TestFolderFolderName6" && $v["XML_ID"] == $folderId["XML_ID"]) {
                             $bFound = true;
                         }
                     }
                     if (!$bFound) {
                         $arMessages[] = array("ERROR", "Tasks folder is not found by id.");
                     } else {
                         $arMessages[] = array("SUCCESS", "Tasks folder was successfully found by id.");
                         $itemId = null;
                         $arFields = array("MAILBOX" => $mailbox, "FOLDER_ID" => $folderId["XML_ID"], "SUBJECT" => "TestFolderItem Name", "BODY" => "Should be done!", "BODY_TYPE" => "text", "IMPORTANCE" => "High", "START_DATE" => "20.10.2011", "DUE_DATE" => "25.10.2011", "PERCENT_COMPLETE" => "0", "STATUS" => "NotStarted", "TOTAL_WORK" => "123", "REMINDER_MINUTES_BEFORE_START" => 365);
                         $r = $e->Add($arFields);
                         $arMessages = array_merge($arMessages, $e->GetErrors());
                         foreach ($r as $v) {
                             $itemId = $v;
                         }
                         if (is_null($itemId)) {
                             $arMessages[] = array("ERROR", "Tasks item creation error.");
                         } else {
                             $arMessages[] = array("SUCCESS", "Tasks item was successfully created (Id = " . $itemId["XML_ID"] . ", ChangeKey = " . $itemId["MODIFICATION_LABEL"] . ").");
                             $arFields = array("SUBJECT" => "New TestFolderItem Name", "BODY" => "Should be done!!!", "BODY_TYPE" => "text", "IMPORTANCE" => "Low", "START_DATE" => "20.10.2011", "DUE_DATE" => "24.10.2011", "PERCENT_COMPLETE" => "0", "STATUS" => "NotStarted", "TOTAL_WORK" => "23", "REMINDER_MINUTES_BEFORE_START" => 365);
                             $r = $e->Update($itemId, $arFields);
                             $itemId = null;
                             $arMessages = array_merge($arMessages, $e->GetErrors());
                             foreach ($r as $v) {
                                 $itemId = $v;
                             }
                             if (is_null($itemId)) {
                                 $arMessages[] = array("ERROR", "Tasks item modification error.");
                             } else {
                                 $arMessages[] = array("SUCCESS", "Tasks item was successfully updated (Id = " . $itemId["XML_ID"] . ", ChangeKey = " . $itemId["MODIFICATION_LABEL"] . ").");
                                 $bFound = false;
                                 $r = $e->GetList(array("Mailbox" => $mailbox, "FolderId" => $folderId["XML_ID"]), array("ItemShape" => "AllProperties"));
                                 $arMessages = array_merge($arMessages, $e->GetErrors());
                                 foreach ($r as $v) {
                                     if ($v["SUBJECT"] == "New TestFolderItem Name" && $v["XML_ID"] == $itemId["XML_ID"]) {
                                         $bFound = true;
                                     }
                                 }
                                 if (!$bFound) {
                                     $arMessages[] = array("ERROR", "Tasks item is not found by list.");
                                 } else {
                                     $arMessages[] = array("SUCCESS", "Tasks item was successfully found by list.");
                                     $bFound = false;
                                     $r = $e->GetById($itemId["XML_ID"]);
                                     $arMessages = array_merge($arMessages, $e->GetErrors());
                                     foreach ($r as $v) {
                                         if ($v["SUBJECT"] == "New TestFolderItem Name" && $v["XML_ID"] == $itemId["XML_ID"]) {
                                             $bFound = true;
                                         }
                                     }
                                     if (!$bFound) {
                                         $arMessages[] = array("ERROR", "Tasks item is not found by id.");
                                     } else {
                                         $arMessages[] = array("SUCCESS", "Tasks item was successfully found by id.");
                                         $r = $e->Delete($itemId["XML_ID"]);
                                         if (!$r) {
                                             $arMessages[] = array("ERROR", "Tasks item deletion error.");
                                         } else {
                                             $arMessages[] = array("SUCCESS", "Tasks item was successfully deleted.");
                                             $bFound = false;
                                             $r = $e->GetList(array("Mailbox" => $mailbox, "FolderId" => $folderId["XML_ID"]), array("ItemShape" => "IdOnly"));
                                             $arMessages = array_merge($arMessages, $e->GetErrors());
                                             foreach ($r as $v) {
                                                 $bFound = true;
                                             }
                                             if ($bFound) {
                                                 $arMessages[] = array("ERROR", "Tasks folder should be empty.");
                                             } else {
                                                 $arMessages[] = array("SUCCESS", "Tasks folder is empty.");
                                                 $r = $e->DeleteFolder($folderId["XML_ID"]);
                                                 $arMessages = array_merge($arMessages, $e->GetErrors());
                                                 if ($r) {
                                                     $arMessages[] = array("SUCCESS", "Tasks folder was successfully deleted.");
                                                 } else {
                                                     $arMessages[] = array("ERROR", "Tasks folder deletion error.");
                                                 }
                                             }
                                         }
                                     }
                                 }
                             }
                         }
                     }
                 }
             }
         }
     }
     return $arMessages;
 }
Esempio n. 2
0
 public 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)) {
         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($by = "UF_BXDAVEX_CNTSYNC", $order = "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($by = "ID", $order = "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();";
 }