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); $siteId = CDav::GetIntranetSite(); $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; } $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 = CEventCalendar::SyncCalendars("exchange", $arUserCalendars, "user", $arUser["ID"], $siteId); $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 = CEventCalendar::SyncCalendarItems("exchange", $userCalendar["CALENDAR_ID"], $arUserCalendarItems); $tmpNumItems += count($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"], "DETAIL_TEXT" => $arModifiedCalendarItem["DETAIL_TEXT"], "DETAIL_TEXT_TYPE" => $arModifiedCalendarItem["DETAIL_TEXT_TYPE"], "XML_ID" => $arModifiedCalendarItem["XML_ID"], "PROPERTY_LOCATION" => $arModifiedCalendarItem["PROPERTY_LOCATION"], "ACTIVE_FROM" => $arModifiedCalendarItem["ACTIVE_FROM"], "ACTIVE_TO" => $arModifiedCalendarItem["ACTIVE_TO"], "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"]); if ($arModifiedCalendarItem["IS_RECURRING"]) { if ($arModifiedCalendarItem["RECURRING_TYPE"] == "MONTHLY_ABSOLUTE" || $arModifiedCalendarItem["RECURRING_TYPE"] == "MONTHLY_RELATIVE") { $arModifyEventArray["PROPERTY_PERIOD_TYPE"] = "MONTHLY"; } elseif ($arModifiedCalendarItem["RECURRING_TYPE"] == "YEARLY_ABSOLUTE" || $arModifiedCalendarItem["RECURRING_TYPE"] == "YEARLY_RELATIVE") { $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 (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"); } } CEventCalendar::ModifyEvent($userCalendar["CALENDAR_ID"], $arModifyEventArray); $bShouldClearCache = true; } } } if (DAV_EXCH_DEBUG) { CDav::WriteToLog("Sync " . intval($tmpNumCals) . " calendars, " . intval($tmpNumItems) . " items", "SYNCE"); } } if ($bShouldClearCache) { CEventCalendar::SyncClearCache($siteId); } 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(); $siteId = CDav::GetIntranetSite(); $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 = CEventCalendar::SyncCalendars("caldav", $arUserCalendars, $arConnection["ENTITY_TYPE"], $arConnection["ENTITY_ID"], $siteId, $arConnection["ID"]); foreach ($arUserCalendars as $userCalendar) { $bShouldClearCache = true; $arCalendarItemsList = $client->GetCalendarItemsList($userCalendar["XML_ID"]); $arUserCalendarItems = array(); 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 = CEventCalendar::SyncCalendarItems("caldav", $userCalendar["CALENDAR_ID"], $arUserCalendarItems); $arHrefs = array(); $arIdMap = array(); foreach ($arUserCalendarItems as $value) { $h = $userCalendar["XML_ID"] . $value["XML_ID"] . ".ics"; $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"], "ACTIVE_FROM" => $value["calendar-data"]["ACTIVE_FROM"], "ACTIVE_TO" => $value["calendar-data"]["ACTIVE_TO"], "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"]); 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 = CEventCalendar::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) { CEventCalendar::SyncClearCache($siteId); } if (DAV_CALDAV_DEBUG) { CDav::WriteToLog("CalDAV sync finished", "SYNCC"); } return "CDavGroupdavClientCalendar::DataSync();"; }
public function Put($id, $siteId, $account, $arPath) { $calendarId = $this->GetCalendarId($siteId, $account, $arPath); if ($calendarId == null) { return '404 Not Found'; } CDav::Report("CDavCalendarHandler::Put", "calendarId", $calendarId); $request = $this->groupdav->GetRequest(); $oldEvent = $this->GetEntry('PUT', $id, $calendarId); if (!is_null($oldEvent) && !is_array($oldEvent)) { return $oldEvent; } $charset = "utf-8"; $arContentParameters = $request->GetContentParameters(); //CDav::Report("CDavCalendarHandler::Put", "arContentParameters", $arContentParameters); if (!empty($arContentParameters['CONTENT_TYPE'])) { $arContentType = explode(';', $arContentParameters['CONTENT_TYPE']); if (count($arContentType) > 1) { array_shift($arContentType); foreach ($arContentType as $attribute) { $attribute = trim($attribute); list($key, $value) = explode('=', $attribute); if (strtolower($key) == 'charset') { $charset = strtolower($value); } } } } $content = $request->GetRequestBody(); $content = htmlspecialcharsback($content); //CDav::Report("CDavCalendarHandler::Put", "content", $content); if (is_array($oldEvent)) { $eventId = $oldEvent['ID']; } else { // Search the same event without ID $eventId = 0; } $cs = CDav::GetCharset($siteId); if (is_null($cs) || empty($cs)) { $cs = "utf-8"; } $content = $GLOBALS["APPLICATION"]->ConvertCharset($content, $charset, $cs); CDav::Report("CDavCalendarHandler::Put", "content (converted " . $charset . " -> " . $cs . ")", $content); $cal = new CDavICalendar($content, $siteId); $arEvents = $cal->GetComponents('VTIMEZONE', false); if (count($arEvents) <= 0) { return '404 Not Found'; } $arFields = $this->ConvertICalToArray($arEvents[0], $cal); if ($eventId > 0) { $arFields['ID'] = $eventId; } else { $arFields['CREATED_BY'] = $arFields['MODIFIED_BY']; } CDav::Report("CDavCalendarHandler::Put", "arFields", $arFields); $eventId = CEventCalendar::ModifyEvent($calendarId, $arFields); if (!is_int($eventId)) { return false; } //header('ETag: '.$this->GetETag($calendarId, $xmlId)); //$path = preg_replace('|(.*)/[^/]*|', '\1/', $request->GetPath()); //header('Location: '.$request->GetBaseUri().$path.$this->GetPath($xmlId)); return "201 Created"; }