/** * The actual ChangesSink. * For max. the $timeout value this method should block and if no changes * are available return an empty array. * If changes are available a list of folderids is expected. * * @param int $timeout max. amount of seconds to block * * @access public * @return array */ public function ChangesSink($timeout = 30) { $notifications = array(); $stopat = time() + $timeout - 1; //We can get here and the ChangesSink not be initialized yet if (!$this->changessinkinit) { ZLog::Write(LOGLEVEL_DEBUG, sprintf("BackendCalDAV->ChangesSink - Not initialized ChangesSink, sleep and exit")); // We sleep and do nothing else sleep($timeout); return $notifications; } // only check once to reduce pressure in the DAV server foreach ($this->sinkdata as $k => $v) { $changed = false; $url = $this->_caldav_path . substr($k, 1) . "/"; $response = $this->_caldav->GetSync($url, false, CALDAV_SUPPORTS_SYNC); if (CALDAV_SUPPORTS_SYNC) { if (count($response) > 0) { $changed = true; ZLog::Write(LOGLEVEL_DEBUG, sprintf("BackendCalDAV->ChangesSink - Changes detected")); } } else { // If the numbers of events are different, we know for sure, there are changes if (count($response) != count($v)) { $changed = true; ZLog::Write(LOGLEVEL_DEBUG, sprintf("BackendCalDAV->ChangesSink - Changes detected")); } else { // If the numbers of events are equals, we compare the biggest date // FIXME: we are comparing strings no dates if (!isset($this->sinkmax[$k])) { $this->sinkmax[$k] = ''; for ($i = 0; $i < count($v); $i++) { if ($v[$i]['getlastmodified'] > $this->sinkmax[$k]) { $this->sinkmax[$k] = $v[$i]['getlastmodified']; } } } for ($i = 0; $i < count($response); $i++) { if ($response[$i]['getlastmodified'] > $this->sinkmax[$k]) { $changed = true; } } if ($changed) { ZLog::Write(LOGLEVEL_DEBUG, sprintf("BackendCalDAV->ChangesSink - Changes detected")); } } } if ($changed) { $notifications[] = $k; } } // Wait to timeout if (empty($notifications)) { while ($stopat > time()) { sleep(1); } } return $notifications; }
define('LOGLEVEL', LOGLEVEL_DEBUG); define('LOGUSERLEVEL', LOGLEVEL_DEVICEID); // $username = "******"; // $password = "******"; // // define('CALDAV_SERVER', 'http://sogo-demo.inverse.ca'); // define('CALDAV_PORT', '80'); // define('CALDAV_PATH', '/SOGo/dav/%u/Calendar/'); // define('CALDAV_PERSONAL', 'personal'); // define('CALDAV_SUPPORTS_SYNC', true); $caldav_path = str_replace('%u', $username, CALDAV_PATH); $caldav = new CalDAVClient(CALDAV_SERVER . ":" . CALDAV_PORT . $caldav_path, $username, $password); printf("Connected %d\n", $caldav->CheckConnection()); // Show options supported by server $options = $caldav->DoOptionsRequest(); print_r($options); $calendars = $caldav->FindCalendars(); print_r($calendars); $path = $caldav_path . CALDAV_PERSONAL . "/"; $val = $caldav->GetCalendarDetails($path); print_r($val); $begin = gmdate("Ymd\\THis\\Z", time() - 24 * 7 * 60 * 60); $finish = gmdate("Ymd\\THis\\Z", 2147483647); $msgs = $caldav->GetEvents($begin, $finish, $path); print_r($msgs); // Initial sync $results = $caldav->GetSync($path, true, CALDAV_SUPPORTS_SYNC); print_r($results); sleep(60); $results = $caldav->GetSync($path, false, CALDAV_SUPPORTS_SYNC); print_r($results);