]> git.mxchange.org Git - friendica-addons.git/blob - dav/common/dav_caldav_backend_common.inc.php
Merge remote branch 'upstream/master'
[friendica-addons.git] / dav / common / dav_caldav_backend_common.inc.php
1 <?php
2
3 abstract class Sabre_CalDAV_Backend_Common extends Sabre_CalDAV_Backend_Abstract {
4         /**
5          * List of CalDAV properties, and how they map to database fieldnames
6          *
7          * Add your own properties by simply adding on to this array
8          *
9          * @var array
10          */
11         public $propertyMap = array(
12                 '{DAV:}displayname'                          => 'displayname',
13                 '{urn:ietf:params:xml:ns:caldav}calendar-description' => 'description',
14                 '{urn:ietf:params:xml:ns:caldav}calendar-timezone'    => 'timezone',
15                 '{http://apple.com/ns/ical/}calendar-order'  => 'calendarorder',
16                 '{http://apple.com/ns/ical/}calendar-color'  => 'calendarcolor',
17         );
18
19
20         abstract public function getNamespace();
21         abstract public function getCalUrlPrefix();
22
23         /**
24          * @param int $namespace
25          * @param int $namespace_id
26          */
27         protected function increaseCalendarCtag($namespace, $namespace_id) {
28                 $namespace = IntVal($namespace);
29                 $namespace_id = IntVal($namespace_id);
30
31                 q("UPDATE %s%scalendars SET `ctag` = `ctag` + 1 WHERE `namespace` = %d AND `namespace_id` = %d", CALDAV_SQL_DB, CALDAV_SQL_PREFIX, $namespace, $namespace_id);
32         }
33
34
35
36         /**
37          * Returns a list of calendars for a principal.
38          *
39          * Every project is an array with the following keys:
40          *  * id, a unique id that will be used by other functions to modify the
41          *    calendar. This can be the same as the uri or a database key.
42          *  * uri, which the basename of the uri with which the calendar is
43          *    accessed.
44          *  * principaluri. The owner of the calendar. Almost always the same as
45          *    principalUri passed to this method.
46          *
47          * Furthermore it can contain webdav properties in clark notation. A very
48          * common one is '{DAV:}displayname'.
49          *
50          * @param string $principalUri
51          * @return array
52          */
53         public function getCalendarsForUser($principalUri)
54         {
55                 list(,$name) = Sabre_DAV_URLUtil::splitPath($principalUri);
56                 $user_id = dav_compat_username2id($name);
57
58                 $cals = q("SELECT * FROM %s%scalendars WHERE `uid`=%d AND `namespace` = %d", CALDAV_SQL_DB, CALDAV_SQL_PREFIX, $user_id, $this->getNamespace());
59                 $ret = array();
60                 foreach ($cals as $cal) {
61                         $dat = array(
62                                 "id" => $cal["namespace"] . "-" . $cal["namespace_id"],
63                                 "uri" => $this->getCalUrlPrefix() . "-" . $cal["namespace_id"],
64                                 "principaluri" => $principalUri,
65                                 '{' . Sabre_CalDAV_Plugin::NS_CALENDARSERVER . '}getctag' => $cal['ctag']?$cal['ctag']:'0',
66                                 "calendar_class" => "Sabre_CalDAV_Calendar",
67                         );
68                         foreach ($this->propertyMap as $key=>$field) $dat[$key] = $cal[$field];
69
70                         $ret[] = $dat;
71                 }
72
73                 return $ret;
74         }
75
76         /**
77          * Updates properties for a calendar.
78          *
79          * The mutations array uses the propertyName in clark-notation as key,
80          * and the array value for the property value. In the case a property
81          * should be deleted, the property value will be null.
82          *
83          * This method must be atomic. If one property cannot be changed, the
84          * entire operation must fail.
85          *
86          * If the operation was successful, true can be returned.
87          * If the operation failed, false can be returned.
88          *
89          * Deletion of a non-existent property is always successful.
90          *
91          * Lastly, it is optional to return detailed information about any
92          * failures. In this case an array should be returned with the following
93          * structure:
94          *
95          * array(
96          *   403 => array(
97          *      '{DAV:}displayname' => null,
98          *   ),
99          *   424 => array(
100          *      '{DAV:}owner' => null,
101          *   )
102          * )
103          *
104          * In this example it was forbidden to update {DAV:}displayname.
105          * (403 Forbidden), which in turn also caused {DAV:}owner to fail
106          * (424 Failed Dependency) because the request needs to be atomic.
107          *
108          * @param mixed $calendarId
109          * @param array $mutations
110          * @return bool|array
111          */
112         public function updateCalendar($calendarId, array $mutations) {
113
114                 $newValues = array();
115                 $result = array(
116                         200 => array(), // Ok
117                         403 => array(), // Forbidden
118                         424 => array(), // Failed Dependency
119                 );
120
121                 $hasError = false;
122
123                 foreach($mutations as $propertyName=>$propertyValue) {
124
125                         // We don't know about this property.
126                         if (!isset($this->propertyMap[$propertyName])) {
127                                 $hasError = true;
128                                 $result[403][$propertyName] = null;
129                                 unset($mutations[$propertyName]);
130                                 continue;
131                         }
132
133                         $fieldName = $this->propertyMap[$propertyName];
134                         $newValues[$fieldName] = $propertyValue;
135
136                 }
137
138                 // If there were any errors we need to fail the request
139                 if ($hasError) {
140                         // Properties has the remaining properties
141                         foreach($mutations as $propertyName=>$propertyValue) {
142                                 $result[424][$propertyName] = null;
143                         }
144
145                         // Removing unused statuscodes for cleanliness
146                         foreach($result as $status=>$properties) {
147                                 if (is_array($properties) && count($properties)===0) unset($result[$status]);
148                         }
149
150                         return $result;
151
152                 }
153
154                 $x = explode("-", $calendarId);
155
156                 $this->increaseCalendarCtag($x[0], $x[1]);
157
158                 $valuesSql = array();
159                 foreach($newValues as $fieldName=>$value) $valuesSql[] = "`" . $fieldName . "` = '" . dbesc($value) . "'";
160                 if (count($valuesSql) > 0) {
161                         q("UPDATE %s%scalendars SET " . implode(", ", $valuesSql) . " WHERE `namespace` = %d AND `namespace_id` = %d",
162                                 CALDAV_SQL_DB, CALDAV_SQL_PREFIX, IntVal($x[0]), IntVal($x[1])
163                         );
164                 }
165
166                 return true;
167
168         }
169
170 }