3 use Friendica\Core\L10n;
5 class Sabre_CalDAV_Backend_Private extends Sabre_CalDAV_Backend_Common
10 * @var null|Sabre_CalDAV_Backend_Private
12 private static $instance = null;
16 * @return Sabre_CalDAV_Backend_Private
18 public static function getInstance()
20 if (self::$instance == null) {
21 self::$instance = new Sabre_CalDAV_Backend_Private();
23 return self::$instance;
30 public function getNamespace()
32 return CALDAV_NAMESPACE_PRIVATE;
39 public static function getBackendTypeName()
41 return L10n::t("Private Events");
46 * @param array $calendar
50 public function getPermissionsCalendar($calendar, $user)
52 if ($calendar["namespace"] == CALDAV_NAMESPACE_PRIVATE && $user == $calendar["namespace_id"]) return array("read"=> true, "write"=> true);
53 return array("read"=> false, "write"=> false);
58 * @param array $calendar
60 * @param string $calendarobject_id
61 * @param null|array $item_arr
64 public function getPermissionsItem($calendar, $user, $calendarobject_id, $item_arr = null)
66 return $this->getPermissionsCalendar($calendar, $user);
72 * @param array $calendar
73 * @param string $base_path
76 private function jqcal2wdcal($row, $calendar, $base_path)
78 $not = q("SELECT COUNT(*) num FROM %s%snotifications WHERE `calendar_id` = %d AND `calendarobject_id` = %d",
79 CALDAV_SQL_DB, CALDAV_SQL_PREFIX, IntVal($row["calendar_id"]), IntVal($row["calendarobject_id"])
81 $editable = $this->getPermissionsItem($calendar["namespace_id"], $row["calendarobject_id"], $row);
83 $end = wdcal_mySql2PhpTime($row["EndTime"]);
84 if ($row["IsAllDayEvent"]) $end -= 1;
87 "jq_id" => $row["id"],
88 "ev_id" => $row["calendarobject_id"],
89 "summary" => escape_tags($row["Summary"]),
90 "start" => wdcal_mySql2PhpTime($row["StartTime"]),
92 "is_allday" => $row["IsAllDayEvent"],
94 "is_recurring" => $row["IsRecurring"],
95 "color" => (is_null($row["Color"]) || $row["Color"] == "" ? $calendar["calendarcolor"] : $row["Color"]),
96 "is_editable" => ($editable ? 1 : 0),
97 "is_editable_quick" => ($editable && !$row["IsRecurring"] ? 1 : 0),
100 "has_notification" => ($not[0]["num"] > 0 ? 1 : 0),
101 "url_detail" => $base_path . $row["calendarobject_id"] . "/",
102 "url_edit" => $base_path . $row["calendarobject_id"] . "/edit/",
103 "special_type" => "",
108 * @param int $calendarId
111 * @param string $base_path
114 public function listItemsByRange($calendarId, $sd, $ed, $base_path)
116 $calendar = Sabre_CalDAV_Backend_Common::loadCalendarById($calendarId);
117 $von = wdcal_php2MySqlTime($sd);
118 $bis = wdcal_php2MySqlTime($ed);
119 $timezoneOffset = date("P");
121 // @TODO Events, die früher angefangen haben, aber noch andauern
122 $evs = q("SELECT *, CONVERT_TZ(`StartTime`, @@session.time_zone, '$timezoneOffset') StartTime, CONVERT_TZ(`EndTime`, @@session.time_zone, '$timezoneOffset') EndTime
123 FROM %s%sjqcalendar WHERE `calendar_id` = %d AND `StartTime` between '%s' and '%s'",
124 CALDAV_SQL_DB, CALDAV_SQL_PREFIX, IntVal($calendarId), dbesc($von), dbesc($bis));
127 foreach ($evs as $row) $events[] = $this->jqcal2wdcal($row, $calendar, $base_path . $row["calendar_id"] . "/");
134 * @param int $calendar_id
135 * @param int $calendarobject_id
138 public function getItemDetailRedirect($calendar_id, $calendarobject_id)
140 return "/dav/wdcal/$calendar_id/$calendarobject_id/edit/";
144 * Returns a list of calendars for a principal.
146 * Every project is an array with the following keys:
147 * * id, a unique id that will be used by other functions to modify the
148 * calendar. This can be the same as the uri or a database key.
149 * * uri, which the basename of the uri with which the calendar is
151 * * principaluri. The owner of the calendar. Almost always the same as
152 * principalUri passed to this method.
154 * Furthermore it can contain webdav properties in clark notation. A very
155 * common one is '{DAV:}displayname'.
157 * @param string $principalUri
158 * @throws DAVVersionMismatchException
161 public function getCalendarsForUser($principalUri)
163 $n = dav_compat_principal2namespace($principalUri);
164 if ($n["namespace"] != $this->getNamespace()) return array();
166 $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"]));
168 foreach ($cals as $cal) {
169 if (!isset($cal["uri"])) throw new DAVVersionMismatchException();
170 if (in_array($cal["uri"], $GLOBALS["CALDAV_PRIVATE_SYSTEM_CALENDARS"])) continue;
172 $components = array();
173 if ($cal["has_vevent"]) $components[] = "VEVENT";
174 if ($cal["has_vtodo"]) $components[] = "VTODO";
178 "uri" => $cal["uri"],
179 "principaluri" => $principalUri,
180 '{' . Sabre_CalDAV_Plugin::NS_CALENDARSERVER . '}getctag' => $cal['ctag'] ? $cal['ctag'] : '0',
181 '{' . Sabre_CalDAV_Plugin::NS_CALDAV . '}supported-calendar-component-set' => new Sabre_CalDAV_Property_SupportedCalendarComponentSet($components),
182 "calendar_class" => "Sabre_CalDAV_Calendar_Private",
184 foreach ($this->propertyMap as $key=> $field) $dat[$key] = $cal[$field];
194 * Creates a new calendar for a principal.
196 * If the creation was a success, an id must be returned that can be used to reference
197 * this calendar in other methods, such as updateCalendar.
199 * @param string $principalUri
200 * @param string $calendarUri
201 * @param array $properties
202 * @throws Sabre_DAV_Exception|Sabre_DAV_Exception_Conflict
203 * @return string|void
205 public function createCalendar($principalUri, $calendarUri, array $properties)
208 $uid = dav_compat_principal2uid($principalUri);
210 $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));
211 if (count($r) > 0) throw new Sabre_DAV_Exception_Conflict("A calendar with this URI already exists");
213 $keys = array("`namespace`", "`namespace_id`", "`ctag`", "`uri`");
214 $vals = array(CALDAV_NAMESPACE_PRIVATE, IntVal($uid), 1, "'" . dbesc($calendarUri) . "'");
217 $sccs = '{urn:ietf:params:xml:ns:caldav}supported-calendar-component-set';
218 $has_vevent = $has_vtodo = 1;
219 if (isset($properties[$sccs])) {
220 if (!($properties[$sccs] instanceof Sabre_CalDAV_Property_SupportedCalendarComponentSet)) {
221 throw new Sabre_DAV_Exception('The ' . $sccs . ' property must be of type: Sabre_CalDAV_Property_SupportedCalendarComponentSet');
223 $v = $properties[$sccs]->getValue();
224 $has_vevent = $has_vtodo = 0;
226 if (mb_strtolower($w) == "vevent") $has_vevent = 1;
227 if (mb_strtolower($w) == "vtodo") $has_vtodo = 1;
230 $keys[] = "`has_vevent`";
231 $keys[] = "`has_vtodo`";
232 $vals[] = $has_vevent;
233 $vals[] = $has_vtodo;
235 foreach ($this->propertyMap as $xmlName=> $dbName) {
236 if (isset($properties[$xmlName])) {
237 $keys[] = "`$dbName`";
238 $vals[] = "'" . dbesc($properties[$xmlName]) . "'";
242 $sql = sprintf("INSERT INTO %s%scalendars (" . implode(', ', $keys) . ") VALUES (" . implode(', ', $vals) . ")", CALDAV_SQL_DB, CALDAV_SQL_PREFIX);
246 $x = q("SELECT id FROM %s%scalendars WHERE `namespace` = %d AND `namespace_id` = %d AND `uri` = '%s'",
247 CALDAV_SQL_DB, CALDAV_SQL_PREFIX, CALDAV_NAMESPACE_PRIVATE, $uid, $calendarUri
254 * Updates properties for a calendar.
256 * The mutations array uses the propertyName in clark-notation as key,
257 * and the array value for the property value. In the case a property
258 * should be deleted, the property value will be null.
260 * This method must be atomic. If one property cannot be changed, the
261 * entire operation must fail.
263 * If the operation was successful, true can be returned.
264 * If the operation failed, false can be returned.
266 * Deletion of a non-existent property is always successful.
268 * Lastly, it is optional to return detailed information about any
269 * failures. In this case an array should be returned with the following
274 * '{DAV:}displayname' => null,
277 * '{DAV:}owner' => null,
281 * In this example it was forbidden to update {DAV:}displayname.
282 * (403 Forbidden), which in turn also caused {DAV:}owner to fail
283 * (424 Failed Dependency) because the request needs to be atomic.
285 * @param string $calendarId
286 * @param array $mutations
289 public function updateCalendar($calendarId, array $mutations)
292 $newValues = array();
294 200 => array(), // Ok
295 403 => array(), // Forbidden
296 424 => array(), // Failed Dependency
301 foreach ($mutations as $propertyName=> $propertyValue) {
303 // We don't know about this property.
304 if (!isset($this->propertyMap[$propertyName])) {
306 $result[403][$propertyName] = null;
307 unset($mutations[$propertyName]);
311 $fieldName = $this->propertyMap[$propertyName];
312 $newValues[$fieldName] = $propertyValue;
316 // If there were any errors we need to fail the request
318 // Properties has the remaining properties
319 foreach ($mutations as $propertyName=> $propertyValue) {
320 $result[424][$propertyName] = null;
323 // Removing unused statuscodes for cleanliness
324 foreach ($result as $status=> $properties) {
325 if (is_array($properties) && count($properties) === 0) unset($result[$status]);
332 $sql = "`ctag` = `ctag` + 1";
333 foreach ($newValues as $key=> $val) $sql .= ", `" . $key . "` = '" . dbesc($val) . "'";
335 $sql = sprintf("UPDATE %s%scalendars SET $sql WHERE `id` = %d", CALDAV_SQL_DB, CALDAV_SQL_PREFIX, IntVal($calendarId));
345 * Delete a calendar and all it's objects
347 * @param string $calendarId
350 public function deleteCalendar($calendarId)
352 q("DELETE FROM %s%scalendarobjects WHERE `calendar_id` = %d", CALDAV_SQL_DB, CALDAV_SQL_PREFIX, IntVal($calendarId));
353 q("DELETE FROM %s%scalendars WHERE `id` = %d", CALDAV_SQL_DB, CALDAV_SQL_PREFIX, IntVal($calendarId));
359 * Returns all calendar objects within a calendar.
361 * Every item contains an array with the following keys:
362 * * id - unique identifier which will be used for subsequent updates
363 * * calendardata - The iCalendar-compatible calendar data
364 * * uri - a unique key which will be used to construct the uri. This can be any arbitrary string.
365 * * lastmodified - a timestamp of the last modification time
366 * * etag - An arbitrary string, surrounded by double-quotes. (e.g.:
368 * * calendarid - The calendarid as it was passed to this function.
369 * * size - The size of the calendar objects, in bytes.
371 * Note that the etag is optional, but it's highly encouraged to return for
374 * The calendardata is also optional. If it's not returned
375 * 'getCalendarObject' will be called later, which *is* expected to return
378 * If neither etag or size are specified, the calendardata will be
379 * used/fetched to determine these numbers. If both are specified the
380 * amount of times this is needed is reduced by a great degree.
382 * @param mixed $calendarId
385 function getCalendarObjects($calendarId)
387 $objs = q("SELECT * FROM %s%scalendarobjects WHERE `calendar_id` = %d", CALDAV_SQL_DB, CALDAV_SQL_PREFIX, IntVal($calendarId));
389 foreach ($objs as $obj) {
391 "id" => IntVal($obj["id"]),
392 "calendardata" => $obj["calendardata"],
393 "uri" => $obj["uri"],
394 "lastmodified" => $obj["lastmodified"],
395 "calendarid" => $calendarId,
396 "etag" => $obj["etag"],
397 "size" => IntVal($obj["size"]),
404 * Returns information from a single calendar object, based on it's object
407 * The returned array must have the same keys as getCalendarObjects. The
408 * 'calendardata' object is required here though, while it's not required
409 * for getCalendarObjects.
411 * @param string $calendarId
412 * @param string $objectUri
413 * @throws Sabre_DAV_Exception_NotFound
416 function getCalendarObject($calendarId, $objectUri)
418 $o = q("SELECT * FROM %s%scalendarobjects WHERE `calendar_id` = %d AND `uri` = '%s'",
419 CALDAV_SQL_DB, CALDAV_SQL_PREFIX, IntVal($calendarId), dbesc($objectUri));
421 $o[0]["calendarid"] = $calendarId;
422 $o[0]["calendardata"] = str_ireplace("Europe/Belgrade", "Europe/Berlin", $o[0]["calendardata"]);
424 } else throw new Sabre_DAV_Exception_NotFound($calendarId . " / " . $objectUri);
428 * Creates a new calendar object.
430 * It is possible return an etag from this function, which will be used in
431 * the response to this PUT request. Note that the ETag must be surrounded
434 * However, you should only really return this ETag if you don't mangle the
435 * calendar-data. If the result of a subsequent GET to this object is not
436 * the exact same as this request body, you should omit the ETag.
438 * @param mixed $calendarId
439 * @param string $objectUri
440 * @param string $calendarData
441 * @return string|null
443 function createCalendarObject($calendarId, $objectUri, $calendarData)
445 $calendarData = icalendar_sanitize_string($calendarData);
447 $extraData = $this->getDenormalizedData($calendarData);
449 q("INSERT INTO %s%scalendarobjects (`calendar_id`, `uri`, `calendardata`, `lastmodified`, `componentType`, `firstOccurence`, `lastOccurence`, `etag`, `size`)
450 VALUES (%d, '%s', '%s', NOW(), '%s', '%s', '%s', '%s', %d)",
451 CALDAV_SQL_DB, CALDAV_SQL_PREFIX, IntVal($calendarId), dbesc($objectUri), addslashes($calendarData), dbesc($extraData['componentType']),
452 dbesc(wdcal_php2MySqlTime($extraData['firstOccurence'])), dbesc(wdcal_php2MySqlTime($extraData['lastOccurence'])), dbesc($extraData["etag"]), IntVal($extraData["size"])
455 $this->increaseCalendarCtag($calendarId);
456 renderCalDavEntry_uri($objectUri);
458 return '"' . $extraData['etag'] . '"';
462 * Updates an existing calendarobject, based on it's uri.
464 * It is possible return an etag from this function, which will be used in
465 * the response to this PUT request. Note that the ETag must be surrounded
468 * However, you should only really return this ETag if you don't mangle the
469 * calendar-data. If the result of a subsequent GET to this object is not
470 * the exact same as this request body, you should omit the ETag.
472 * @param mixed $calendarId
473 * @param string $objectUri
474 * @param string $calendarData
475 * @return string|null
477 function updateCalendarObject($calendarId, $objectUri, $calendarData)
479 $calendarData = icalendar_sanitize_string($calendarData);
481 $extraData = $this->getDenormalizedData($calendarData);
483 q("UPDATE %s%scalendarobjects SET `calendardata` = '%s', `lastmodified` = NOW(), `etag` = '%s', `size` = %d, `componentType` = '%s', `firstOccurence` = '%s', `lastOccurence` = '%s'
484 WHERE `calendar_id` = %d AND `uri` = '%s'",
485 CALDAV_SQL_DB, CALDAV_SQL_PREFIX, dbesc($calendarData), dbesc($extraData["etag"]), IntVal($extraData["size"]), dbesc($extraData["componentType"]),
486 dbesc(wdcal_php2MySqlTime($extraData["firstOccurence"])), dbesc(wdcal_php2MySqlTime($extraData["lastOccurence"])), IntVal($calendarId), dbesc($objectUri));
488 $this->increaseCalendarCtag($calendarId);
489 renderCalDavEntry_uri($objectUri);
491 return '"' . $extraData['etag'] . '"';
495 * Deletes an existing calendar object.
497 * @param string $calendarId
498 * @param string $objectUri
499 * @throws Sabre_DAV_Exception_NotFound
502 function deleteCalendarObject($calendarId, $objectUri)
504 $r = q("SELECT `id` FROM %s%scalendarobjects WHERE `calendar_id` = %d AND `uri` = '%s'", CALDAV_SQL_DB, CALDAV_SQL_PREFIX, IntVal($calendarId), dbesc($objectUri));
505 if (count($r) == 0) throw new Sabre_DAV_Exception_NotFound();
507 q("DELETE FROM %s%scalendarobjects WHERE `calendar_id` = %d AND `uri` = '%s'", CALDAV_SQL_DB, CALDAV_SQL_PREFIX, IntVal($calendarId), dbesc($objectUri));
509 $this->increaseCalendarCtag($calendarId);
510 renderCalDavEntry_calobj_id($r[0]["id"]);