]> git.mxchange.org Git - friendica-addons.git/blob - dav/common/dav_carddav_backend_virtual.inc.php
Merge pull request #334 from annando/1601-converse-update
[friendica-addons.git] / dav / common / dav_carddav_backend_virtual.inc.php
1 <?php
2
3 abstract class Sabre_CardDAV_Backend_Virtual extends Sabre_CardDAV_Backend_Common
4 {
5
6         /**
7          * @static
8          * @abstract
9          * @param int $addressbookId
10          * @param string $uri
11          * @return array
12          */
13         /*
14         abstract public function getItemsByUri($addressbookId, $uri);
15     */
16
17         /**
18          * @static
19          * @param int $addressbookId
20          */
21         static public function invalidateCache($addressbookId) {
22                 q("UPDATE %s%saddressbooks SET `needs_rebuild` = 1 WHERE `id` = %d", CALDAV_SQL_DB, CALDAV_SQL_PREFIX, IntVal($addressbookId));
23         }
24
25         /**
26          * @static
27          * @abstract
28          * @param int $addressbookId
29          * @param bool $force
30          */
31         static abstract protected function createCache_internal($addressbookId, $force = false);
32
33         /**
34          * @param int $addressbookId
35          * @param null|array $addressbook
36          * @param bool $force
37          */
38         public function createCache($addressbookId, $addressbook = null, $force = false) {
39                 $addressbookId = IntVal($addressbookId);
40
41                 if (!$addressbook) {
42                         $add = q("SELECT `needs_rebuild`, `uri` FROM %s%saddressbooks WHERE `id` = %d", CALDAV_SQL_DB, CALDAV_SQL_PREFIX, $addressbookId);
43                         $addressbook = $add[0];
44                 }
45                 if ($addressbook["needs_rebuild"] == 1 || $force) {
46                         static::createCache_internal($addressbookId, $force);
47                         q("UPDATE %s%saddressbooks SET `needs_rebuild` = 0, `ctag` = `ctag` + 1 WHERE `id` = %d", CALDAV_SQL_DB, CALDAV_SQL_PREFIX, $addressbookId);
48                 }
49         }
50
51         /**
52          * @static
53          * @abstract
54          * @param int $addressbookId
55          * @param int $contactId
56          * @param bool $force
57          */
58         static abstract protected function createCardCache($addressbookId, $contactId, $force = false);
59
60         /**
61          * @param int $addressbookId
62          * @return array
63          */
64         public function getCards($addressbookId)
65         {
66                 $addressbookId = IntVal($addressbookId);
67                 $add = q("SELECT * FROM %s%saddressbooks WHERE `id` = %d", CALDAV_SQL_DB, CALDAV_SQL_PREFIX, $addressbookId);
68                 if ($add[0]["needs_rebuild"]) {
69                         static::createCache_internal($addressbookId);
70                         q("UPDATE %s%saddressbooks SET `needs_rebuild` = 0, `ctag` = `ctag` + 1 WHERE `id` = %d", CALDAV_SQL_DB, CALDAV_SQL_PREFIX, $addressbookId);
71                         $add[0]["needs_rebuild"] = 0;
72                         $add[0]["ctag"]++;
73                 }
74
75                 $ret = array();
76                 $x = q("SELECT * FROM %s%saddressbookobjects WHERE `addressbook_id` = %d AND `manually_deleted` = 0", CALDAV_SQL_DB, CALDAV_SQL_PREFIX, $addressbookId);
77                 foreach ($x as $y) $ret[] = self::getCard($addressbookId, $add[0]["uri"], $add[0], $y);
78
79                 return $ret;
80         }
81
82
83         /**
84          * Replaces the x-prop_name value. Replaces the prop_name value IF the old value is the same as the old value of x-prop_name (meaning: the user has not manually changed it)
85          *
86          * @param Sabre\VObject\Component $component
87          * @param string $prop_name
88          * @param string $prop_value
89          * @param array $parameters
90          * @return void
91          */
92         static public function card_set_automatic_value(&$component, $prop_name, $prop_value, $parameters = array()) {
93                 $automatic = $component->select("X-" . $prop_name);
94                 $curr = $component->select($prop_name);
95
96                 if (count($automatic) == 0) {
97                         $prop = new Sabre\VObject\Property('X-' . $prop_name, $prop_value);
98                         foreach ($parameters as $key=>$val) $prop->add($key, $val);
99                         $component->children[] = $prop;
100
101                         if (count($curr) == 0) {
102                                 $prop = new Sabre\VObject\Property($prop_name, $prop_value);
103                                 foreach ($parameters as $key=>$val) $prop->add($key, $val);
104                                 $component->children[] = $prop;
105                         }
106
107                 } else foreach ($automatic as $auto_prop) {
108                         /** @var Sabre\VObject\Property $auto_prop */
109                         /** @var Sabre\VObject\Property $actual_prop */
110                         foreach ($curr as $actual_prop) {
111                                 if ($auto_prop->value == $actual_prop->value) $actual_prop->setValue($prop_value);
112                         }
113                         $auto_prop->setValue($prop_value);
114                 }
115         }
116
117
118
119         /**
120          * Deletes the x-prop_name value. Deletes the prop_name value IF the old value is the same as the old value of x-prop_name (meaning: the user has not manually changed it)
121          *
122          * @param Sabre\VObject\Component $component
123          * @param string $prop_name
124          * @param array $parameters
125          */
126         static public function card_del_automatic_value(&$component, $prop_name, $parameters = array()) {
127                 // @TODO
128         }
129
130         /**
131          * @param int $addressbookId
132          * @param string $objectUri
133          * @param array $book
134          * @param array $obj
135          * @throws Sabre_DAV_Exception_NotFound
136          * @return array
137          */
138         public function getCard($addressbookId, $objectUri, $book = null, $obj = null)
139         {
140                 $addressbookId = IntVal($addressbookId);
141
142                 if ($book == null) {
143                         $add = q("SELECT `needs_rebuild`, `uri` FROM %s%saddressbooks WHERE `id` = %d", CALDAV_SQL_DB, CALDAV_SQL_PREFIX, $addressbookId);
144                         $book = $add[0];
145                 }
146                 if ($book["needs_rebuild"] == 1) {
147                         static::createCache_internal($addressbookId);
148                         q("UPDATE %s%saddressbooks SET `needs_rebuild` = 0, `ctag` = `ctag` + 1 WHERE `id` = %d", CALDAV_SQL_DB, CALDAV_SQL_PREFIX, $addressbookId);
149                         $add[0]["needs_rebuild"] = 0;
150                 }
151
152                 if ($obj == null) {
153                         $r = q("SELECT * FROM %s%saddressbookobjects WHERE `uri` = '%s' AND `addressbook_id` = %d AND `manually_deleted` = 0",
154                                 CALDAV_SQL_DB, CALDAV_SQL_PREFIX, dbesc($objectUri), IntVal($addressbookId));
155                         if (count($r) == 0) throw new Sabre_DAV_Exception_NotFound();
156                         $obj = $r[0];
157                         if ($obj["needs_rebuild"] == 1) $obj = static::createCardCache($addressbookId, $obj["contact"]);
158                 }
159
160                 $ret = array(
161                         "id" => IntVal($obj["uri"]),
162                         "carddata" => $obj["carddata"],
163                         "uri" => $obj["uri"],
164                         "lastmodified" => $obj["lastmodified"],
165                         "addressbookid" => $addressbookId,
166                         "etag" => $obj["etag"],
167                         "size" => IntVal($obj["size"]),
168                 );
169                 return $ret;
170         }
171
172
173
174         /**
175          * @param string $principalUri
176          * @param string $addressbookUri
177          * @param array $properties
178          * @throws Sabre_DAV_Exception_Forbidden
179          * @return void
180          */
181         public function createAddressBook($principalUri, $addressbookUri, array $properties)
182         {
183                 throw new Sabre_DAV_Exception_Forbidden();
184         }
185
186         /**
187          * @param string $addressbookId
188          * @throws Sabre_DAV_Exception_Forbidden
189          * @return void
190          */
191         public function deleteAddressBook($addressbookId)
192         {
193                 throw new Sabre_DAV_Exception_Forbidden();
194         }
195
196
197         /**
198          * @param string $addressbookId
199          * @param string $objectUri
200          * @param string $cardData
201          * @throws Sabre_DAV_Exception_Forbidden
202          * @return null|string|void
203          */
204         function createCard($addressbookId, $objectUri, $cardData)
205         {
206                 throw new Sabre_DAV_Exception_Forbidden();
207         }
208
209         /**
210          * Updates a card.
211          *
212          * The addressbook id will be passed as the first argument. This is the
213          * same id as it is returned from the getAddressbooksForUser method.
214          *
215          * The cardUri is a base uri, and doesn't include the full path. The
216          * cardData argument is the vcard body, and is passed as a string.
217          *
218          * It is possible to return an ETag from this method. This ETag should
219          * match that of the updated resource, and must be enclosed with double
220          * quotes (that is: the string itself must contain the actual quotes).
221          *
222          * You should only return the ETag if you store the carddata as-is. If a
223          * subsequent GET request on the same card does not have the same body,
224          * byte-by-byte and you did return an ETag here, clients tend to get
225          * confused.
226          *
227          * If you don't return an ETag, you can just return null.
228          *
229          * @param string $addressBookId
230          * @param string $cardUri
231          * @param string $cardData
232          * @throws Sabre_DAV_Exception_Forbidden
233          * @return string|null
234          */
235         public function updateCard($addressBookId, $cardUri, $cardData)
236         {
237                 echo "Die!"; die(); // @TODO
238                 $x = explode("-", $addressBookId);
239
240                 $etag = md5($cardData);
241                 q("UPDATE %s%scards SET carddata = '%s', lastmodified = %d, etag = '%s', size = %d, manually_edited = 1 WHERE uri = '%s' AND namespace = %d AND namespace_id =%d",
242                         CALDAV_SQL_DB, CALDAV_SQL_PREFIX, dbesc($cardData), time(), $etag, strlen($cardData), dbesc($cardUri), IntVal($x[10]), IntVal($x[1])
243                 );
244                 q('UPDATE %s%saddressbooks_community SET ctag = ctag + 1 WHERE uid = %d', CALDAV_SQL_DB, CALDAV_SQL_PREFIX, IntVal($x[1]));
245
246                 return '"' . $etag . '"';
247         }
248
249
250
251
252         /**
253          * Deletes a card
254          *
255          * @param string $addressBookId
256          * @param string $cardUri
257          * @throws Sabre_DAV_Exception_Forbidden
258          * @return bool
259          */
260         public function deleteCard($addressBookId, $cardUri)
261         {
262                 q("UPDATE %s%scards SET `manually_deleted` = 1 WHERE `addressbook_id` = %d AND `uri` = '%s'", CALDAV_SQL_DB, CALDAV_SQL_PREFIX, IntVal($addressBookId), dbesc($cardUri));
263                 q('UPDATE %s%saddressbooks SET ctag = ctag + 1 WHERE `id` = %d', CALDAV_SQL_DB, CALDAV_SQL_PREFIX, IntVal($addressBookId));
264
265                 return true;
266         }
267
268
269 }