3 class Sabre_CalDAV_Backend_Private extends Sabre_CalDAV_Backend_Common
8 * @var null|Sabre_CalDAV_Backend_Private
10 private static $instance = null;
14 * @return Sabre_CalDAV_Backend_Private
16 public static function getInstance()
18 if (self::$instance == null) {
19 self::$instance = new Sabre_CalDAV_Backend_Private();
21 return self::$instance;
28 public function getNamespace()
30 return CALDAV_NAMESPACE_PRIVATE;
37 public static function getBackendTypeName() {
38 return t("Private Events");
43 * @param array $calendar
47 public function getPermissionsCalendar($calendar, $user)
49 if ($calendar["namespace"] == CALDAV_NAMESPACE_PRIVATE && $user == $calendar["namespace_id"]) return array("read"=> true, "write"=> true);
50 return array("read"=> false, "write"=> false);
55 * @param array $calendar
57 * @param string $calendarobject_id
58 * @param null|array $item_arr
61 public function getPermissionsItem($calendar, $user, $calendarobject_id, $item_arr = null)
63 return $this->getPermissionsCalendar($calendar, $user);
69 * @param array $calendar
70 * @param string $base_path
73 private function jqcal2wdcal($row, $calendar, $base_path)
75 $not = q("SELECT COUNT(*) num FROM %s%snotifications WHERE `calendar_id` = %d AND `calendarobject_id` = %d",
76 CALDAV_SQL_DB, CALDAV_SQL_PREFIX, IntVal($row["calendar_id"]), IntVal($row["calendarobject_id"])
78 $editable = $this->getPermissionsItem($calendar["namespace_id"], $row["calendarobject_id"], $row);
80 $end = wdcal_mySql2PhpTime($row["EndTime"]);
81 if ($row["IsAllDayEvent"]) $end -= 1;
84 "jq_id" => $row["id"],
85 "ev_id" => $row["calendarobject_id"],
86 "summary" => escape_tags($row["Summary"]),
87 "start" => wdcal_mySql2PhpTime($row["StartTime"]),
89 "is_allday" => $row["IsAllDayEvent"],
91 "is_recurring" => $row["IsRecurring"],
92 "color" => (is_null($row["Color"]) || $row["Color"] == "" ? $calendar["calendarcolor"] : $row["Color"]),
93 "is_editable" => ($editable ? 1 : 0),
94 "is_editable_quick" => ($editable && !$row["IsRecurring"] ? 1 : 0),
97 "has_notification" => ($not[0]["num"] > 0 ? 1 : 0),
98 "url_detail" => $base_path . $row["calendarobject_id"] . "/",
99 "url_edit" => $base_path . $row["calendarobject_id"] . "/edit/",
100 "special_type" => "",
105 * @param int $calendarId
108 * @param string $base_path
111 public function listItemsByRange($calendarId, $sd, $ed, $base_path)
113 $calendar = Sabre_CalDAV_Backend_Common::loadCalendarById($calendarId);
114 $von = wdcal_php2MySqlTime($sd);
115 $bis = wdcal_php2MySqlTime($ed);
117 // @TODO Events, die früher angefangen haben, aber noch andauern
118 $evs = q("SELECT * FROM %s%sjqcalendar WHERE `calendar_id` = %d AND `starttime` between '%s' and '%s'",
119 CALDAV_SQL_DB, CALDAV_SQL_PREFIX,
120 IntVal($calendarId), dbesc($von), dbesc($bis));
123 foreach ($evs as $row) $events[] = $this->jqcal2wdcal($row, $calendar, $base_path . $row["calendar_id"] . "/");
130 * @param int $calendar_id
131 * @param int $calendarobject_id
134 public function getItemDetailRedirect($calendar_id, $calendarobject_id)
136 return "/dav/wdcal/$calendar_id/$calendarobject_id/edit/";
140 * Returns a list of calendars for a principal.
142 * Every project is an array with the following keys:
143 * * id, a unique id that will be used by other functions to modify the
144 * calendar. This can be the same as the uri or a database key.
145 * * uri, which the basename of the uri with which the calendar is
147 * * principaluri. The owner of the calendar. Almost always the same as
148 * principalUri passed to this method.
150 * Furthermore it can contain webdav properties in clark notation. A very
151 * common one is '{DAV:}displayname'.
153 * @param string $principalUri
156 public function getCalendarsForUser($principalUri)
158 $n = dav_compat_principal2namespace($principalUri);
159 if ($n["namespace"] != $this->getNamespace()) return array();
161 $cals = q("SELECT * FROM %s%scalendars WHERE `namespace` = %d AND `namespace_id` = %d", CALDAV_SQL_DB, CALDAV_SQL_PREFIX, $this->getNamespace(), IntVal($n["namespace_id"]));
163 foreach ($cals as $cal) {
164 if (in_array($cal["uri"], $GLOBALS["CALDAV_PRIVATE_SYSTEM_CALENDARS"])) continue;
168 "uri" => $cal["uri"],
169 "principaluri" => $principalUri,
170 '{' . Sabre_CalDAV_Plugin::NS_CALENDARSERVER . '}getctag' => $cal['ctag'] ? $cal['ctag'] : '0',
171 "calendar_class" => "Sabre_CalDAV_Calendar",
173 foreach ($this->propertyMap as $key=> $field) $dat[$key] = $cal[$field];
183 * Creates a new calendar for a principal.
185 * If the creation was a success, an id must be returned that can be used to reference
186 * this calendar in other methods, such as updateCalendar.
188 * @param string $principalUri
189 * @param string $calendarUri
190 * @param array $properties
191 * @throws Sabre_DAV_Exception
192 * @return string|void
194 public function createCalendar($principalUri, $calendarUri, array $properties)
197 $uid = dav_compat_principal2uid($principalUri);
199 $r = q("SELECT * FROM %s%scalendars WHERE `namespace` = %d AND `namespace_id` = %d AND `uri` = '%s'", CALDAV_SQL_DB, CALDAV_SQL_PREFIX, CALDAV_NAMESPACE_PRIVATE, $uid, dbesc($calendarUri));
200 if (count($r) > 0) throw new Sabre_DAV_Exception("A calendar with this URI already exists");
202 $keys = array("`namespace`", "`namespace_id`", "`ctag`", "`uri`");
203 $vals = array(CALDAV_NAMESPACE_PRIVATE, IntVal($uid), 1, "'" . dbesc($calendarUri) . "'");
206 $sccs = '{urn:ietf:params:xml:ns:caldav}supported-calendar-component-set';
207 $has_vevent = $has_vtodo = 1;
208 if (isset($properties[$sccs])) {
209 if (!($properties[$sccs] instanceof Sabre_CalDAV_Property_SupportedCalendarComponentSet)) {
210 throw new Sabre_DAV_Exception('The ' . $sccs . ' property must be of type: Sabre_CalDAV_Property_SupportedCalendarComponentSet');
212 $v = $properties[$sccs]->getValue();
213 $has_vevent = $has_vtodo = 0;
215 if (mb_strtolower($w) == "vevent") $has_vevent = 1;
216 if (mb_strtolower($w) == "vtodo") $has_vtodo = 1;
219 $keys[] = "`has_vevent`";
220 $keys[] = "`has_vtodo`";
221 $vals[] = $has_vevent;
222 $vals[] = $has_vtodo;
224 foreach ($this->propertyMap as $xmlName=> $dbName) {
225 if (isset($properties[$xmlName])) {
226 $keys[] = "`$dbName`";
227 $vals[] = "'" . dbesc($properties[$xmlName]) . "'";
231 $sql = sprintf("INSERT INTO %s%scalendars (" . implode(', ', $keys) . ") VALUES (" . implode(', ', $vals) . ")", CALDAV_SQL_DB, CALDAV_SQL_PREFIX);
235 $x = q("SELECT id FROM %s%scalendars WHERE `namespace` = %d AND `namespace_id` = %d AND `uri` = '%s'",
236 CALDAV_SQL_DB, CALDAV_SQL_PREFIX, CALDAV_NAMESPACE_PRIVATE, $uid, $calendarUri
243 * Updates properties for a calendar.
245 * The mutations array uses the propertyName in clark-notation as key,
246 * and the array value for the property value. In the case a property
247 * should be deleted, the property value will be null.
249 * This method must be atomic. If one property cannot be changed, the
250 * entire operation must fail.
252 * If the operation was successful, true can be returned.
253 * If the operation failed, false can be returned.
255 * Deletion of a non-existent property is always successful.
257 * Lastly, it is optional to return detailed information about any
258 * failures. In this case an array should be returned with the following
263 * '{DAV:}displayname' => null,
266 * '{DAV:}owner' => null,
270 * In this example it was forbidden to update {DAV:}displayname.
271 * (403 Forbidden), which in turn also caused {DAV:}owner to fail
272 * (424 Failed Dependency) because the request needs to be atomic.
274 * @param string $calendarId
275 * @param array $mutations
278 public function updateCalendar($calendarId, array $mutations)
281 $newValues = array();
283 200 => array(), // Ok
284 403 => array(), // Forbidden
285 424 => array(), // Failed Dependency
290 foreach ($mutations as $propertyName=> $propertyValue) {
292 // We don't know about this property.
293 if (!isset($this->propertyMap[$propertyName])) {
295 $result[403][$propertyName] = null;
296 unset($mutations[$propertyName]);
300 $fieldName = $this->propertyMap[$propertyName];
301 $newValues[$fieldName] = $propertyValue;
305 // If there were any errors we need to fail the request
307 // Properties has the remaining properties
308 foreach ($mutations as $propertyName=> $propertyValue) {
309 $result[424][$propertyName] = null;
312 // Removing unused statuscodes for cleanliness
313 foreach ($result as $status=> $properties) {
314 if (is_array($properties) && count($properties) === 0) unset($result[$status]);
321 $sql = "`ctag` = `ctag` + 1";
322 foreach ($newValues as $key=> $val) $sql .= ", `" . $key . "` = '" . dbesc($val) . "'";
324 $sql = sprintf("UPDATE %s%scalendars SET $sql WHERE `id` = %d", CALDAV_SQL_DB, CALDAV_SQL_PREFIX, IntVal($calendarId));
334 * Delete a calendar and all it's objects
336 * @param string $calendarId
339 public function deleteCalendar($calendarId)
341 q("DELETE FROM %s%scalendarobjects WHERE `calendar_id` = %d", CALDAV_SQL_DB, CALDAV_SQL_PREFIX, IntVal($calendarId));
342 q("DELETE FROM %s%scalendars WHERE `id` = %d", CALDAV_SQL_DB, CALDAV_SQL_PREFIX, IntVal($calendarId));
348 * Returns all calendar objects within a calendar.
350 * Every item contains an array with the following keys:
351 * * id - unique identifier which will be used for subsequent updates
352 * * calendardata - The iCalendar-compatible calendar data
353 * * uri - a unique key which will be used to construct the uri. This can be any arbitrary string.
354 * * lastmodified - a timestamp of the last modification time
355 * * etag - An arbitrary string, surrounded by double-quotes. (e.g.:
357 * * calendarid - The calendarid as it was passed to this function.
358 * * size - The size of the calendar objects, in bytes.
360 * Note that the etag is optional, but it's highly encouraged to return for
363 * The calendardata is also optional. If it's not returned
364 * 'getCalendarObject' will be called later, which *is* expected to return
367 * If neither etag or size are specified, the calendardata will be
368 * used/fetched to determine these numbers. If both are specified the
369 * amount of times this is needed is reduced by a great degree.
371 * @param mixed $calendarId
374 function getCalendarObjects($calendarId)
376 $objs = q("SELECT * FROM %s%scalendarobjects WHERE `calendar_id` = %d", CALDAV_SQL_DB, CALDAV_SQL_PREFIX, IntVal($calendarId));
378 foreach ($objs as $obj) {
380 "id" => IntVal($obj["id"]),
381 "calendardata" => $obj["calendardata"],
382 "uri" => $obj["uri"],
383 "lastmodified" => $obj["lastmodified"],
384 "calendarid" => $calendarId,
385 "etag" => $obj["etag"],
386 "size" => IntVal($obj["size"]),
393 * Returns information from a single calendar object, based on it's object
396 * The returned array must have the same keys as getCalendarObjects. The
397 * 'calendardata' object is required here though, while it's not required
398 * for getCalendarObjects.
400 * @param string $calendarId
401 * @param string $objectUri
402 * @throws Sabre_DAV_Exception_NotFound
405 function getCalendarObject($calendarId, $objectUri)
407 $o = q("SELECT * FROM %s%scalendarobjects WHERE `calendar_id` = %d AND `uri` = '%s'",
408 CALDAV_SQL_DB, CALDAV_SQL_PREFIX, IntVal($calendarId), dbesc($objectUri));
410 $o[0]["calendarid"] = $calendarId;
411 $o[0]["calendardata"] = str_ireplace("Europe/Belgrade", "Europe/Berlin", $o[0]["calendardata"]);
413 } else throw new Sabre_DAV_Exception_NotFound($calendarId . " / " . $objectUri);
417 * Creates a new calendar object.
419 * It is possible return an etag from this function, which will be used in
420 * the response to this PUT request. Note that the ETag must be surrounded
423 * However, you should only really return this ETag if you don't mangle the
424 * calendar-data. If the result of a subsequent GET to this object is not
425 * the exact same as this request body, you should omit the ETag.
427 * @param mixed $calendarId
428 * @param string $objectUri
429 * @param string $calendarData
430 * @return string|null
432 function createCalendarObject($calendarId, $objectUri, $calendarData)
434 $calendarData = icalendar_sanitize_string($calendarData);
436 $extraData = $this->getDenormalizedData($calendarData);
438 q("INSERT INTO %s%scalendarobjects (`calendar_id`, `uri`, `calendardata`, `lastmodified`, `componentType`, `firstOccurence`, `lastOccurence`, `etag`, `size`)
439 VALUES (%d, '%s', '%s', NOW(), '%s', '%s', '%s', '%s', %d)",
440 CALDAV_SQL_DB, CALDAV_SQL_PREFIX, IntVal($calendarId), dbesc($objectUri), addslashes($calendarData), dbesc($extraData['componentType']),
441 dbesc(wdcal_php2MySqlTime($extraData['firstOccurence'])), dbesc(wdcal_php2MySqlTime($extraData['lastOccurence'])), dbesc($extraData["etag"]), IntVal($extraData["size"])
444 $this->increaseCalendarCtag($calendarId);
445 renderCalDavEntry_uri($objectUri);
447 return '"' . $extraData['etag'] . '"';
451 * Updates an existing calendarobject, based on it's uri.
453 * It is possible return an etag from this function, which will be used in
454 * the response to this PUT request. Note that the ETag must be surrounded
457 * However, you should only really return this ETag if you don't mangle the
458 * calendar-data. If the result of a subsequent GET to this object is not
459 * the exact same as this request body, you should omit the ETag.
461 * @param mixed $calendarId
462 * @param string $objectUri
463 * @param string $calendarData
464 * @return string|null
466 function updateCalendarObject($calendarId, $objectUri, $calendarData)
468 $calendarData = icalendar_sanitize_string($calendarData);
470 $extraData = $this->getDenormalizedData($calendarData);
472 q("UPDATE %s%scalendarobjects SET `calendardata` = '%s', `lastmodified` = NOW(), `etag` = '%s', `size` = %d, `componentType` = '%s', `firstOccurence` = '%s', `lastOccurence` = '%s'
473 WHERE `calendar_id` = %d AND `uri` = '%s'",
474 CALDAV_SQL_DB, CALDAV_SQL_PREFIX, dbesc($calendarData), dbesc($extraData["etag"]), IntVal($extraData["size"]), dbesc($extraData["componentType"]),
475 dbesc(wdcal_php2MySqlTime($extraData["firstOccurence"])), dbesc(wdcal_php2MySqlTime($extraData["lastOccurence"])), IntVal($calendarId), dbesc($objectUri));
477 $this->increaseCalendarCtag($calendarId);
478 renderCalDavEntry_uri($objectUri);
480 return '"' . $extraData['etag'] . '"';
484 * Deletes an existing calendar object.
486 * @param string $calendarId
487 * @param string $objectUri
488 * @throws Sabre_DAV_Exception_NotFound
491 function deleteCalendarObject($calendarId, $objectUri)
493 $r = q("SELECT `id` FROM %s%scalendarobjects WHERE `calendar_id` = %d AND `uri` = '%s'", CALDAV_SQL_DB, CALDAV_SQL_PREFIX, IntVal($calendarId), dbesc($objectUri));
494 if (count($r) == 0) throw new Sabre_DAV_Exception_NotFound();
496 q("DELETE FROM %s%scalendarobjects WHERE `calendar_id` = %d AND `uri` = '%s'", CALDAV_SQL_DB, CALDAV_SQL_PREFIX, IntVal($calendarId), dbesc($objectUri));
498 $this->increaseCalendarCtag($calendarId);
499 renderCalDavEntry_calobj_id($r[0]["id"]);