Пример #1
0
 public static function DoDataSync($paramUserId, &$lastError)
 {
     if (DAV_EXCH_DEBUG) {
         CDav::WriteToLog("Starting EXCHANGE sync...", "SYNCE");
     }
     $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("CDavExchangeCalendar::DataSync();", "dav");
         COption::SetOptionString("dav", "agent_calendar", "N");
         return null;
     }
     static $arWeekDayMap = array("sunday" => 6, "monday" => 0, "tuesday" => 1, "wednesday" => 2, "thursday" => 3, "friday" => 4, "saturday" => 5);
     $exchange = new CDavExchangeCalendar($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 = 15;
     $index = 0;
     $bShouldClearCache = 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_CALSYNC", $order = "asc", $arUserFilter, array("SELECT" => array("ID", "LOGIN", "UF_BXDAVEX_MAILBOX", "UF_BXDAVEX_CALSYNC")));
     while ($arUser = $dbUserList->Fetch()) {
         $index++;
         if ($index > $maxNumber) {
             break;
         }
         if (DAV_EXCH_DEBUG) {
             CDav::WriteToLog("Processing user [" . $arUser["ID"] . "] " . $arUser["LOGIN"], "SYNCE");
         }
         $GLOBALS["USER_FIELD_MANAGER"]->Update("USER", $arUser["ID"], array("UF_BXDAVEX_CALSYNC" => ConvertTimeStamp(time(), FULL)));
         $mailbox = $exchangeUseLogin == "Y" ? $arUser["LOGIN"] . $exchangeMailbox : $arUser["UF_BXDAVEX_MAILBOX"];
         if (empty($mailbox)) {
             $lastError = GetMessage("DAV_EC_EMPTY_MAILBOX");
             continue;
         }
         $arCalendarsList = $exchange->GetCalendarsList(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, "SYNCE");
             }
             $lastError = $txt;
             continue;
         }
         if (!is_array($arCalendarsList)) {
             $lastError = "Incorrect Data from Exchange Server";
             continue;
         }
         $bShouldClearCache = false;
         $arUserCalendars = array(array("XML_ID" => "calendar_" . $arUser["ID"], "NAME" => GetMessage("DAV_EC_CALENDAR"), "MODIFICATION_LABEL" => ""));
         foreach ($arCalendarsList as $value) {
             $arUserCalendars[] = array("XML_ID" => $value["XML_ID"], "NAME" => $value["NAME"], "MODIFICATION_LABEL" => $value["MODIFICATION_LABEL"]);
         }
         $tmpNumCals = count($arUserCalendars);
         $arUserCalendars = CCalendar::SyncCalendars("exchange", $arUserCalendars, "user", $arUser["ID"]);
         $tmpNumItems = 0;
         foreach ($arUserCalendars as $userCalendar) {
             $userCalendarXmlId = $userCalendar["XML_ID"];
             $userCalendarXmlId = $userCalendarXmlId == "calendar_" . $arUser["ID"] ? "calendar" : $userCalendarXmlId;
             $arCalendarItemsList = $exchange->GetList(array("mailbox" => $mailbox, "CalendarId" => $userCalendarXmlId), array("ItemShape" => "IdOnly"));
             $arUserCalendarItems = array();
             foreach ($arCalendarItemsList as $value) {
                 $arUserCalendarItems[] = array("XML_ID" => $value["XML_ID"], "MODIFICATION_LABEL" => $value["MODIFICATION_LABEL"]);
             }
             $arModifiedUserCalendarItems = CCalendar::SyncCalendarItems("exchange", $userCalendar["CALENDAR_ID"], $arUserCalendarItems);
             $tmpNumItems += count($arModifiedUserCalendarItems);
             if (is_array($arModifiedUserCalendarItems)) {
                 foreach ($arModifiedUserCalendarItems as $value) {
                     $arModifiedCalendarItem = $exchange->GetById($value["XML_ID"]);
                     if (is_array($arModifiedCalendarItem) && count($arModifiedCalendarItem) > 0) {
                         $arModifiedCalendarItem = $arModifiedCalendarItem[0];
                         $arModifyEventArray = array("ID" => $value["ID"], "NAME" => $arModifiedCalendarItem["NAME"], "DESCRIPTION" => $arModifiedCalendarItem["DESCRIPTION"], "XML_ID" => $arModifiedCalendarItem["XML_ID"], "PROPERTY_LOCATION" => $arModifiedCalendarItem["PROPERTY_LOCATION"], "DATE_FROM" => $arModifiedCalendarItem["ACTIVE_FROM"], "DATE_TO" => $arModifiedCalendarItem["ACTIVE_TO"], "SKIP_TIME" => $arModifiedCalendarItem["SKIP_TIME"], "PROPERTY_IMPORTANCE" => $arModifiedCalendarItem["PROPERTY_IMPORTANCE"], "PROPERTY_ACCESSIBILITY" => $arModifiedCalendarItem["PROPERTY_FREEBUSY"], "PROPERTY_REMIND_SETTINGS" => $arModifiedCalendarItem["PROPERTY_REMIND_SETTINGS"], "PROPERTY_PERIOD_TYPE" => "NONE", "PROPERTY_BXDAVEX_LABEL" => $arModifiedCalendarItem["MODIFICATION_LABEL"], "PRIVATE_EVENT" => strtolower($arModifiedCalendarItem["PROPERTY_SENSITIVITY"]) == 'private');
                         if ($arModifiedCalendarItem["IS_RECURRING"]) {
                             if ($arModifiedCalendarItem["RECURRING_TYPE"] == "MONTHLY_ABSOLUTE" || $arModifiedCalendarItem["RECURRING_TYPE"] == "MONTHLY_RELATIVE" || $arModifiedCalendarItem["RECURRING_TYPE"] == "MONTHLY") {
                                 $arModifyEventArray["PROPERTY_PERIOD_TYPE"] = "MONTHLY";
                             } elseif ($arModifiedCalendarItem["RECURRING_TYPE"] == "YEARLY_ABSOLUTE" || $arModifiedCalendarItem["RECURRING_TYPE"] == "YEARLY_RELATIVE" || $arModifiedCalendarItem["RECURRING_TYPE"] == "YEARLY") {
                                 $arModifyEventArray["PROPERTY_PERIOD_TYPE"] = "YEARLY";
                             } elseif ($arModifiedCalendarItem["RECURRING_TYPE"] == "WEEKLY") {
                                 $arModifyEventArray["PROPERTY_PERIOD_TYPE"] = "WEEKLY";
                             } elseif ($arModifiedCalendarItem["RECURRING_TYPE"] == "DAILY") {
                                 $arModifyEventArray["PROPERTY_PERIOD_TYPE"] = "DAILY";
                             }
                             if (isset($arModifiedCalendarItem["RECURRING_INTERVAL"])) {
                                 $arModifyEventArray["PROPERTY_PERIOD_COUNT"] = $arModifiedCalendarItem["RECURRING_INTERVAL"];
                             }
                             if ($arModifyEventArray["PROPERTY_PERIOD_TYPE"] == "WEEKLY") {
                                 if (isset($arModifiedCalendarItem["RECURRING_DAYSOFWEEK"])) {
                                     $ar = preg_split("/[;,\\s]/i", $arModifiedCalendarItem["RECURRING_DAYSOFWEEK"]);
                                     $ar1 = array();
                                     foreach ($ar as $v) {
                                         $ar1[] = $arWeekDayMap[strtolower($v)];
                                     }
                                     $arModifyEventArray["PROPERTY_PERIOD_ADDITIONAL"] = implode(",", $ar1);
                                 }
                             }
                             $arModifyEventArray["PROPERTY_EVENT_LENGTH"] = MakeTimeStamp($arModifyEventArray["ACTIVE_TO"]) - MakeTimeStamp($arModifyEventArray["ACTIVE_FROM"]);
                             if ($arModifyEventArray["PROPERTY_EVENT_LENGTH"] <= 0) {
                                 $arModifyEventArray["PROPERTY_EVENT_LENGTH"] = 86400;
                             }
                             if (isset($arModifiedCalendarItem["RECURRING_ENDDATE"])) {
                                 $arModifyEventArray["ACTIVE_TO"] = $arModifiedCalendarItem["RECURRING_ENDDATE"];
                             } elseif (isset($arResultItem["RECURRING_NUMBEROFOCCURRENCES"])) {
                                 $eventTime = self::GetPeriodicEventTime(MakeTimeStamp($arModifyEventArray["ACTIVE_TO"]), array("freq" => $arModifyEventArray["PROPERTY_PERIOD_TYPE"], "interval" => $arModifyEventArray["PROPERTY_PERIOD_COUNT"], "byday" => $arModifyEventArray["PROPERTY_PERIOD_ADDITIONAL"]), $arResultItem["RECURRING_NUMBEROFOCCURRENCES"]);
                                 $arModifyEventArray["ACTIVE_TO"] = date($GLOBALS["DB"]->DateFormatToPHP(FORMAT_DATETIME), $eventTime);
                             } else {
                                 $arModifyEventArray["ACTIVE_TO"] = ConvertTimeStamp(mktime(0, 0, 0, 12, 31, 2025), "FULL");
                             }
                         }
                         CCalendar::ModifyEvent($userCalendar["CALENDAR_ID"], $arModifyEventArray);
                         $bShouldClearCache = true;
                     }
                 }
             }
         }
         if (DAV_EXCH_DEBUG) {
             CDav::WriteToLog("Sync " . intval($tmpNumCals) . " calendars, " . intval($tmpNumItems) . " items", "SYNCE");
         }
     }
     if ($bShouldClearCache) {
         CCalendar::SyncClearCache();
     }
     if (DAV_EXCH_DEBUG) {
         CDav::WriteToLog("EXCHANGE sync finished", "SYNCE");
     }
     return $bShouldClearCache;
 }
 public static function DataSync($paramEntityType = null, $paramEntityId = 0)
 {
     if (DAV_CALDAV_DEBUG) {
         CDav::WriteToLog("Starting CalDAV sync", "SYNCC");
     }
     self::InitUserEntity();
     $maxNumber = 5;
     $index = 0;
     $bShouldClearCache = false;
     $paramEntityId = intval($paramEntityId);
     $arConnectionsFilter = array("ACCOUNT_TYPE" => 'caldav');
     if (!is_null($paramEntityType) && $paramEntityId > 0) {
         $arConnectionsFilter["ENTITY_TYPE"] = $paramEntityType;
         $arConnectionsFilter["ENTITY_ID"] = $paramEntityId;
     }
     $dbConnections = CDavConnection::GetList(array("SYNCHRONIZED" => "ASC"), $arConnectionsFilter, false, false, array("ID", "ENTITY_TYPE", "ENTITY_ID", "SERVER_SCHEME", "SERVER_HOST", "SERVER_PORT", "SERVER_USERNAME", "SERVER_PASSWORD", "SERVER_PATH", "SYNCHRONIZED"));
     while ($arConnection = $dbConnections->Fetch()) {
         $index++;
         if ($index > $maxNumber) {
             break;
         }
         if (DAV_CALDAV_DEBUG) {
             CDav::WriteToLog("Connection [" . $arConnection["ID"] . "] " . $arConnection["ENTITY_TYPE"] . "/" . $arConnection["ENTITY_ID"], "SYNCC");
         }
         CDavConnection::SetLastResult($arConnection["ID"], "[0]");
         $client = new CDavGroupdavClientCalendar($arConnection["SERVER_SCHEME"], $arConnection["SERVER_HOST"], $arConnection["SERVER_PORT"], $arConnection["SERVER_USERNAME"], $arConnection["SERVER_PASSWORD"]);
         if (CDav::UseProxy()) {
             $arProxy = CDav::GetProxySettings();
             $client->SetProxy($arProxy["PROXY_SCHEME"], $arProxy["PROXY_HOST"], $arProxy["PROXY_PORT"], $arProxy["PROXY_USERNAME"], $arProxy["PROXY_PASSWORD"]);
         }
         //$client->Debug();
         if (!$client->CheckWebdavServer($arConnection["SERVER_PATH"])) {
             $t = '';
             $arErrors = $client->GetErrors();
             foreach ($arErrors as $arError) {
                 if (strlen($t) > 0) {
                     $t .= ', ';
                 }
                 $t .= '[' . $arError[0] . '] ' . $arError[1];
             }
             CDavConnection::SetLastResult($arConnection["ID"], strlen($t) > 0 ? $t : "[404] Not Found");
             if (DAV_CALDAV_DEBUG) {
                 CDav::WriteToLog("ERROR: " . $t, "SYNCC");
             }
             continue;
         }
         $arCalendarsList = $client->GetCalendarList($arConnection["SERVER_PATH"]);
         if (count($arCalendarsList) <= 0) {
             CDavConnection::SetLastResult($arConnection["ID"], "[204] No Content");
             continue;
         }
         $arUserCalendars = array();
         foreach ($arCalendarsList as $value) {
             $arUserCalendars[] = array("XML_ID" => $value["href"], "NAME" => $value["displayname"], "DESCRIPTION" => $value["calendar-description"], "COLOR" => $value["calendar-color"], "MODIFICATION_LABEL" => $value["getctag"]);
         }
         $tmpNumCals = count($arUserCalendars);
         $tmpNumItems = 0;
         $arUserCalendars = CCalendar::SyncCalendars("caldav", $arUserCalendars, $arConnection["ENTITY_TYPE"], $arConnection["ENTITY_ID"], $arConnection["ID"]);
         foreach ($arUserCalendars as $userCalendar) {
             $bShouldClearCache = true;
             $arCalendarItemsList = $client->GetCalendarItemsList($userCalendar["XML_ID"]);
             $arUserCalendarItems = array();
             if (is_array($arCalendarItemsList)) {
                 foreach ($arCalendarItemsList as $value) {
                     if (strpos($value["getcontenttype"], "text/calendar") !== false && strpos($value["getcontenttype"], "component=vevent") !== false && isset($value["getetag"])) {
                         $arUserCalendarItems[] = array("XML_ID" => basename($value["href"], ".ics"), "MODIFICATION_LABEL" => $value["getetag"]);
                     }
                 }
             }
             $arUserCalendarItems = CCalendar::SyncCalendarItems("caldav", $userCalendar["CALENDAR_ID"], $arUserCalendarItems);
             $arHrefs = array();
             $arIdMap = array();
             foreach ($arUserCalendarItems as $value) {
                 $h = $client->GetRequestEventPath($userCalendar["XML_ID"], $value["XML_ID"]);
                 $arHrefs[] = $h;
                 $arIdMap[$h] = $value["ID"];
             }
             $arCalendarItemsList = $client->GetCalendarItemsList($userCalendar["XML_ID"], $arHrefs, true);
             $tmpNumItems += count($arCalendarItemsList);
             foreach ($arCalendarItemsList as $value) {
                 if (!array_key_exists($value["href"], $arIdMap)) {
                     continue;
                 }
                 $arModifyEventArray = array("ID" => $arIdMap[$value["href"]], "NAME" => $value["calendar-data"]["NAME"], "DETAIL_TEXT" => $value["calendar-data"]["DETAIL_TEXT"], "DETAIL_TEXT_TYPE" => $value["calendar-data"]["DETAIL_TEXT_TYPE"], "XML_ID" => basename($value["href"], ".ics"), "PROPERTY_LOCATION" => $value["calendar-data"]["PROPERTY_LOCATION"], "DT_FROM_TS" => $value["calendar-data"]["DT_FROM_TS"], "DT_TO_TS" => $value["calendar-data"]["DT_TO_TS"], "DT_LENGTH" => $value["calendar-data"]["DT_LENGTH"], "SKIP_TIME" => $value["calendar-data"]["SKIP_TIME"], "PROPERTY_IMPORTANCE" => $value["calendar-data"]["PROPERTY_IMPORTANCE"], "PROPERTY_ACCESSIBILITY" => $value["calendar-data"]["PROPERTY_ACCESSIBILITY"], "PROPERTY_REMIND_SETTINGS" => $value["calendar-data"]["PROPERTY_REMIND_SETTINGS"], "PROPERTY_PERIOD_TYPE" => "NONE", "PROPERTY_BXDAVCD_LABEL" => $value["getetag"], "VERSION" => $value["calendar-data"]["VERSION"]);
                 if (isset($value["calendar-data"]["PROPERTY_PERIOD_TYPE"]) && $value["calendar-data"]["PROPERTY_PERIOD_TYPE"] != "NONE") {
                     $arModifyEventArray["PROPERTY_PERIOD_TYPE"] = $value["calendar-data"]["PROPERTY_PERIOD_TYPE"];
                     $arModifyEventArray["PROPERTY_PERIOD_COUNT"] = $value["calendar-data"]["PROPERTY_PERIOD_COUNT"];
                     $arModifyEventArray["PROPERTY_PERIOD_ADDITIONAL"] = $value["calendar-data"]["PROPERTY_PERIOD_ADDITIONAL"];
                     $arModifyEventArray["PROPERTY_EVENT_LENGTH"] = $value["calendar-data"]["PROPERTY_EVENT_LENGTH"];
                 }
                 $k = CCalendar::ModifyEvent($userCalendar["CALENDAR_ID"], $arModifyEventArray);
             }
         }
         if (DAV_CALDAV_DEBUG) {
             CDav::WriteToLog("Sync " . intval($tmpNumCals) . " calendars, " . intval($tmpNumItems) . " items", "SYNCC");
         }
         CDavConnection::SetLastResult($arConnection["ID"], "[200] OK");
     }
     if ($bShouldClearCache) {
         CCalendar::SyncClearCache();
     }
     if (DAV_CALDAV_DEBUG) {
         CDav::WriteToLog("CalDAV sync finished", "SYNCC");
     }
     return "CDavGroupdavClientCalendar::DataSync();";
 }