]> git.mxchange.org Git - friendica-addons.git/blob - dav/common/dav_caldav_backend.inc.php
Merge remote branch 'upstream/master'
[friendica-addons.git] / dav / common / dav_caldav_backend.inc.php
1 <?php
2
3 class Sabre_CalDAV_Backend_Std extends Sabre_CalDAV_Backend_Common
4 {
5
6         public function getNamespace()
7         {
8                 return CALDAV_NAMESPACE_PRIVATE;
9         }
10
11         public function getCalUrlPrefix()
12         {
13                 return "private";
14         }
15
16         /**
17          * Creates a new calendar for a principal.
18          *
19          * If the creation was a success, an id must be returned that can be used to reference
20          * this calendar in other methods, such as updateCalendar.
21          *
22          * @param string $principalUri
23          * @param string $calendarUri
24          * @param array $properties
25          * @return void
26          */
27         public function createCalendar($principalUri, $calendarUri, array $properties)
28         {
29                 // TODO: Implement createCalendar() method.
30         }
31
32         /**
33          * Delete a calendar and all it's objects
34          *
35          * @param string $calendarId
36          * @return void
37          */
38         public function deleteCalendar($calendarId)
39         {
40                 // TODO: Implement deleteCalendar() method.
41         }
42
43
44         /**
45          * Returns all calendar objects within a calendar.
46          *
47          * Every item contains an array with the following keys:
48          *   * id - unique identifier which will be used for subsequent updates
49          *   * calendardata - The iCalendar-compatible calendar data
50          *   * uri - a unique key which will be used to construct the uri. This can be any arbitrary string.
51          *   * lastmodified - a timestamp of the last modification time
52          *   * etag - An arbitrary string, surrounded by double-quotes. (e.g.:
53          *   '  "abcdef"')
54          *   * calendarid - The calendarid as it was passed to this function.
55          *   * size - The size of the calendar objects, in bytes.
56          *
57          * Note that the etag is optional, but it's highly encouraged to return for
58          * speed reasons.
59          *
60          * The calendardata is also optional. If it's not returned
61          * 'getCalendarObject' will be called later, which *is* expected to return
62          * calendardata.
63          *
64          * If neither etag or size are specified, the calendardata will be
65          * used/fetched to determine these numbers. If both are specified the
66          * amount of times this is needed is reduced by a great degree.
67          *
68          * @param string $calendarId
69          * @return array
70          */
71         function getCalendarObjects($calendarId)
72         {
73                 $x    = explode("-", $calendarId);
74                 $objs = q("SELECT * FROM %s%scalendarobjects WHERE `namespace` = %d AND `namespace_id` = %d", CALDAV_SQL_DB, CALDAV_SQL_PREFIX, IntVal($x[0]), IntVal($x[1]));
75                 $ret  = array();
76                 foreach ($objs as $obj) {
77                         $ret[] = array(
78                                 "id"           => IntVal($obj["id"]),
79                                 "calendardata" => $obj["calendardata"],
80                                 "uri"          => $obj["uri"],
81                                 "lastmodified" => $obj["lastmodified"],
82                                 "calendarid"   => $calendarId,
83                                 "etag"         => $obj["etag"],
84                                 "size"         => IntVal($obj["size"]),
85                         );
86                 }
87                 return $ret;
88         }
89
90         /**
91          * Returns information from a single calendar object, based on it's object
92          * uri.
93          *
94          * The returned array must have the same keys as getCalendarObjects. The
95          * 'calendardata' object is required here though, while it's not required
96          * for getCalendarObjects.
97          *
98          * @param string $calendarId
99          * @param string $objectUri
100          * @throws Sabre_DAV_Exception_FileNotFound
101          * @return array
102          */
103         function getCalendarObject($calendarId, $objectUri)
104         {
105                 $x = explode("-", $calendarId);
106
107                 $o = q("SELECT * FROM %s%scalendarobjects WHERE `namespace` = %d AND `namespace_id` = %d AND `uri` = '%s'",
108                         CALDAV_SQL_DB, CALDAV_SQL_PREFIX, IntVal($x[0]), IntVal($x[1]), dbesc($objectUri));
109                 if (count($o) > 0) {
110                         $o[0]["calendarid"]   = $calendarId;
111                         $o[0]["calendardata"] = str_ireplace("Europe/Belgrade", "Europe/Berlin", $o[0]["calendardata"]);
112                         return $o[0];
113                 } else throw new Sabre_DAV_Exception_FileNotFound($calendarId . " / " . $objectUri);
114         }
115
116         /**
117          * Creates a new calendar object.
118          *
119          * @param string $calendarId
120          * @param string $objectUri
121          * @param string $calendarData
122          * @return null|string|void
123          */
124         function createCalendarObject($calendarId, $objectUri, $calendarData)
125         {
126                 $x = explode("-", $calendarId);
127
128                 q("INSERT INTO %s%scalendarobjects (`namespace`, `namespace_id`, `uri`, `calendardata`, `lastmodified`, `etag`, `size`) VALUES (%d, %d, '%s', '%s', NOW(), '%s', %d)",
129                         CALDAV_SQL_DB, CALDAV_SQL_PREFIX,
130                         IntVal($x[0]), IntVal($x[1]), dbesc($objectUri), addslashes($calendarData), md5($calendarData), strlen($calendarData)
131                 );
132
133                 $this->increaseCalendarCtag($x[0], $x[1]);
134                 renderCalDavEntry_uri($objectUri);
135         }
136
137         /**
138          * Updates an existing calendarobject, based on it's uri.
139          *
140          * @param string $calendarId
141          * @param string $objectUri
142          * @param string $calendarData
143          * @return null|string|void
144          */
145         function updateCalendarObject($calendarId, $objectUri, $calendarData)
146         {
147                 $x = explode("-", $calendarId);
148
149                 q("UPDATE %s%scalendarobjects SET `calendardata` = '%s', `lastmodified` = NOW(), `etag` = '%s', `size` = %d WHERE `namespace` = %d AND `namespace_id` = %d AND `uri` = '%s'",
150                         CALDAV_SQL_DB, CALDAV_SQL_PREFIX, dbesc($calendarData), md5($calendarData), strlen($calendarData), IntVal($x[0]), IntVal($x[1]), dbesc($objectUri));
151
152                 $this->increaseCalendarCtag($x[0], $x[1]);
153                 renderCalDavEntry_uri($objectUri);
154         }
155
156         /**
157          * Deletes an existing calendar object.
158          *
159          * @param string $calendarId
160          * @param string $objectUri
161          * @return void
162          */
163         function deleteCalendarObject($calendarId, $objectUri)
164         {
165                 $x = explode("-", $calendarId);
166
167                 q("DELETE FROM %s%scalendarobjects WHERE `namespace` = %d AND `namespace_id` = %d AND `uri` = '%s'",
168                         CALDAV_SQL_DB, CALDAV_SQL_PREFIX, IntVal($x[0]), IntVal($x[1]), dbesc($objectUri)
169                 );
170
171                 $this->increaseCalendarCtag($x[0], $x[1]);
172                 renderCalDavEntry_uri($objectUri);
173         }
174 }