function fetch_external($bind_id, $min_age = '') { if (!function_exists("curl_init")) { dbg_error_log("external", "external resource cannot be fetched without curl, please install curl"); $request->DoResponse(503, translate('PHP5 curl support is required for external binds')); return; } $sql = 'SELECT collection.*, collection.dav_name AS path, dav_binding.external_url AS external_url FROM dav_binding LEFT JOIN collection ON (collection.collection_id=bound_source_id) WHERE bind_id = :bind_id'; $params = array(':bind_id' => $bind_id); if (strlen($min_age) > 2) { $sql .= ' AND collection.modified + interval :interval < NOW()'; $params[':interval'] = $min_age; } $sql .= ' ORDER BY modified DESC LIMIT 1'; $qry = new AwlQuery($sql, $params); if ($qry->Exec('DAVResource') && $qry->rows() > 0 && ($row = $qry->Fetch())) { $local_ts = new DateTime($row->modified); $curl = curl_init($row->external_url); curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); curl_setopt($curl, CURLOPT_HEADER, true); curl_setopt($curl, CURLOPT_FILETIME, true); curl_setopt($curl, CURLOPT_NOBODY, true); curl_setopt($curl, CURLOPT_TIMEVALUE, $local_ts->format("U")); curl_setopt($curl, CURLOPT_TIMECONDITION, CURL_TIMECOND_IFMODSINCE); dbg_error_log("external", "checking external resource for remote changes " . $row->external_url); $ics = curl_exec($curl); $info = curl_getinfo($curl); if ($info['http_code'] === 304 || isset($info['filetime']) && new DateTime("@" . $info['filetime']) <= $local_ts && $info['filetime'] != '-1') { dbg_error_log("external", "external resource unchanged " . $info['filetime'] . ' < ' . $local_ts->getTimestamp()); curl_close($curl); // BUGlet: should track server-time instead of local-time $qry = new AwlQuery('UPDATE collection SET modified=NOW() WHERE collection_id = :cid', array(':cid' => $row->collection_id)); $qry->Exec('DAVResource'); return true; } dbg_error_log("external", "external resource changed, re importing" . $info['filetime']); curl_setopt($curl, CURLOPT_NOBODY, false); curl_setopt($curl, CURLOPT_HEADER, false); $ics = curl_exec($curl); curl_close($curl); if (is_string($ics) && strlen($ics) > 20) { // BUGlet: should track server-time instead of local-time $qry = new AwlQuery('UPDATE collection SET modified=NOW(), dav_etag=:etag WHERE collection_id = :cid', array(':cid' => $row->collection_id, ':etag' => md5($ics))); $qry->Exec('DAVResource'); require_once 'caldav-PUT-functions.php'; import_collection($ics, $row->user_no, $row->path, 'External Fetch', false); return true; } } else { dbg_error_log("external", "external resource up to date or not found id(%s)", $bind_id); } return false; }
} include_once 'caldav-PUT-functions.php'; controlRequestContainer($dav_resource->GetProperty('username'), $dav_resource->GetProperty('user_no'), $dav_resource->bound_from(), true); $lock_opener = $request->FailIfLocked(); if ($dav_resource->IsCollection()) { if ($dav_resource->IsPrincipal() || $dav_resource->IsBinding() || !isset($c->readonly_webdav_collections) || $c->readonly_webdav_collections == true) { $request->DoResponse(405); // Method not allowed return; } $appending = isset($_GET['mode']) && $_GET['mode'] == 'append'; /** * CalDAV does not define the result of a PUT on a collection. We treat that * as an import. The code is in caldav-PUT-functions.php */ import_collection($request->raw_post, $request->user_no, $request->path, true, $appending); $request->DoResponse(200); return; } $etag = md5($request->raw_post); $ic = new iCalComponent($request->raw_post); if (!$dav_resource->Exists() && (isset($request->etag_if_match) && $request->etag_if_match != '')) { /** * RFC2068, 14.25: * If none of the entity tags match, or if "*" is given and no current * entity exists, the server MUST NOT perform the requested method, and * MUST return a 412 (Precondition Failed) response. */ $request->PreconditionFailed(412, 'if-match'); } if ($dav_resource->Exists()) {
} $this->username = $principal->username(); $this->principal_id = $principal->principal_id(); $this->email = $principal->email(); $this->dav_name = $principal->dav_name(); $this->principal = $principal; $this->logged_in = true; } function AllowedTo($do_something) { return $this->logged_in; } } $session = new FakeSession(); $dest = new DAVResource($target); $session = new FakeSession($dest->user_no()); if ($mode == 'append' && !$dest->Exists()) { printf("The target '%s' does not exist.\n", $target); exit(1); } if (!$dest->IsCollection()) { printf("The target '%s' is not a collection.\n", $target); exit(1); } $user_no = $dest->user_no(); $username = $session->username; param_to_global('mode'); include_once 'caldav-PUT-functions.php'; controlRequestContainer($session->username, $dest->user_no(), $target, false, $dest->IsPublic() ? true : false); import_collection($ics, $dest->user_no(), $target, $session->user_no, $mode == 'append'); printf(translate("Calendar '%s' was loaded from file.\n"), $target);
static function importFromDirectory() { global $c; if (empty($_POST["calendar_path"])) { dbg_error_log("importFromDirectory", "calendar path not given"); return; } $path_ics = $_POST["calendar_path"]; if (substr($path_ics, -1, 1) != '/') { $path_ics .= '/'; } // ensure that we target a collection if (substr($path_ics, 0, 1) != '/') { $path_ics = '/' . $path_ics; } // ensure that we target a collection if (empty($_POST["directory_path"])) { dbg_error_log("importFromDirectory", "directory path not given"); return; } $dir = $_POST["directory_path"]; if (!is_readable($dir)) { $c->messages[] = sprintf(i18n('directory %s is not readable'), htmlspecialchars($dir)); dbg_error_log("importFromDirectory", "directory is not readable"); return; } if ($handle = opendir($dir)) { $c->readonly_webdav_collections = false; // Override this setting so we can create collections/events on import. while (false !== ($file = readdir($handle))) { if ($file == "." || $file == ".." || substr($file, -4) != '.ics') { continue; } if (!is_readable($dir . '/' . $file)) { dbg_error_log("importFromDirectory", "ics file '%s' is not readable", $dir . '/' . $file); continue; } $ics = file_get_contents($dir . '/' . $file); $ics = trim($ics); if ($ics != '') { if (!check_string($ics)) { $c->messages[] = sprintf(translate('The file "%s" is not UTF-8 encoded, please check error for more details'), $dir . '/' . $file); continue; } $username = substr($file, 0, -4); $principal = new Principal('username', $username); if (!$principal->Exists()) { $c->messages[] = sprintf(translate('The principal "%s" does not exist'), $username); continue; } $path = "/" . $username . $path_ics; $user_no = $principal->user_no(); if (controlRequestContainer($username, $user_no, $path, false) === -1) { continue; } dbg_error_log("importFromDirectory", "importing to {$path}"); import_collection($ics, $user_no, $path, 1); $c->messages[] = sprintf(translate('All events of user "%s" were deleted and replaced by those from file %s'), substr($file, 0, -4), $dir . '/' . $file); } } closedir($handle); } }
* as if it were a "PUT" request against a collection. This is something * of a hack. It works though :-) */ $ics = trim(file_get_contents($_FILES['ics_file']['tmp_name'])); dbg_error_log('collection-edit', ':Write: Loaded %d bytes from %s', strlen($ics), $_FILES['ics_file']['tmp_name']); include_once 'check_UTF8.php'; if (!check_string($ics)) { $ics = force_utf8($ics); } if (check_string($ics)) { $path = $editor->Value('dav_name'); $user_no = $editor->Value('user_no'); $username = $editor->Value('username'); include_once 'caldav-PUT-functions.php'; controlRequestContainer($username, $user_no, $path, false, $publicly_readable == 'on' ? true : false); import_collection($ics, $user_no, $path, $session->user_no); $c->messages[] = sprintf(translate('Calendar "%s" was loaded from file.'), $path); } else { $c->messages[] = i18n('The file is not UTF-8 encoded, please check the error for more details.'); } } } } else { if ($id > 0) { $editor->GetRecord(); } if ($editor->IsSubmit()) { $c->messages[] = i18n('You do not have permission to modify this record.'); } } if ($editor->Available()) {
* of a hack. It works though :-) */ $ics = trim(file_get_contents($_FILES['ics_file']['tmp_name'])); dbg_error_log('collection-edit', ':Write: Loaded %d bytes from %s', strlen($ics), $_FILES['ics_file']['tmp_name']); include_once 'check_UTF8.php'; if (!check_string($ics)) { $ics = force_utf8($ics); } if (check_string($ics)) { $path = $editor->Value('dav_name'); $user_no = $editor->Value('user_no'); $username = $editor->Value('username'); param_to_global('mode'); include_once 'caldav-PUT-functions.php'; controlRequestContainer($username, $user_no, $path, false, $publicly_readable == 'on' ? true : false); import_collection($ics, $user_no, $path, $session->user_no, $mode == 'on'); $c->messages[] = sprintf(translate('Calendar "%s" was loaded from file.'), $path); } else { $c->messages[] = i18n('The file is not UTF-8 encoded, please check the error for more details.'); } } } // Uncache anything to do with the collection $cache = getCacheInstance(); $cache->delete('collection-' . $editor->Value('dav_name'), null); } else { if ($id > 0) { $editor->GetRecord(); } if ($editor->IsSubmit()) { $c->messages[] = i18n('You do not have permission to modify this record.');