]> git.mxchange.org Git - friendica-addons.git/blob - dav/common/dav_carddav_backend_std.inc.php
Merge remote branch 'upstream/master'
[friendica-addons.git] / dav / common / dav_carddav_backend_std.inc.php
1 <?php
2
3 /**
4  * PDO CardDAV backend
5  *
6  * This CardDAV backend uses PDO to store addressbooks
7  *
8  * @package Sabre
9  * @subpackage CardDAV
10  * @copyright Copyright (C) 2007-2012 Rooftop Solutions. All rights reserved.
11  * @author Evert Pot (http://www.rooftopsolutions.nl/)
12  * @license http://code.google.com/p/sabredav/wiki/License Modified BSD License
13  */
14 class Sabre_CardDAV_Backend_Std extends Sabre_CardDAV_Backend_Abstract
15 {
16
17         /**
18          * @var null|Sabre_CardDAV_Backend_Std
19          */
20         private static $instance = null;
21
22         /**
23          * @static
24          * @return Sabre_CardDAV_Backend_Std
25          */
26         public static function getInstance() {
27                 if (self::$instance == null) {
28                         self::$instance = new Sabre_CardDAV_Backend_Std();
29                 }
30                 return self::$instance;
31         }
32
33
34         /**
35          * Sets up the object
36          */
37         public function __construct()
38         {
39
40         }
41
42         /**
43          * Returns the list of addressbooks for a specific user.
44          *
45          * @param string $principalUri
46          * @return array
47          */
48         public function getAddressBooksForUser($principalUri)
49         {
50                 $uid = dav_compat_principal2uid($principalUri);
51
52                 $addressBooks = array();
53
54                 $books = q("SELECT id, uri, displayname, principaluri, description, ctag FROM %s%saddressbooks_phone WHERE principaluri = '%s'", CALDAV_SQL_DB, CALDAV_SQL_PREFIX, dbesc($principalUri));
55                 if (count($books) == 0) {
56                         q("INSERT INTO %s%saddressbooks_phone (uid, principaluri, displayname, uri, description, ctag) VALUES (%d, '%s', '%s', '%s', '%s', 1)",
57                                 CALDAV_SQL_DB, CALDAV_SQL_PREFIX, $uid, dbesc($principalUri), 'Other', 'phone', 'Manually added contacts'
58                         );
59                         $books = q("SELECT id, uri, displayname, principaluri, description, ctag FROM %s%saddressbooks_phone WHERE principaluri = '%s'", CALDAV_SQL_DB, CALDAV_SQL_PREFIX, dbesc($principalUri));
60                 }
61                 foreach ($books as $row) {
62                         $addressBooks[] = array(
63                                 'id'                                                                => CARDDAV_NAMESPACE_PHONECONTACTS . "-" . $row['id'],
64                                 'uri'                                                               => $row['uri'],
65                                 'principaluri'                                                      => $row['principaluri'],
66                                 '{DAV:}displayname'                                                 => $row['displayname'],
67                                 '{' . Sabre_CardDAV_Plugin::NS_CARDDAV . '}addressbook-description' => $row['description'],
68                                 '{http://calendarserver.org/ns/}getctag'                            => $row['ctag'],
69                                 '{' . Sabre_CardDAV_Plugin::NS_CARDDAV . '}supported-address-data'  =>
70                                 new Sabre_CardDAV_Property_SupportedAddressData(),
71                         );
72                 }
73
74                 return $addressBooks;
75
76         }
77
78
79         /**
80          * Updates an addressbook's properties
81          *
82          * See Sabre_DAV_IProperties for a description of the mutations array, as
83          * well as the return value.
84          *
85          * @param mixed $addressBookId
86          * @param array $mutations
87          * @throws Sabre_DAV_Exception_Forbidden
88          * @see Sabre_DAV_IProperties::updateProperties
89          * @return bool|array
90          */
91         public function updateAddressBook($addressBookId, array $mutations)
92         {
93                 $x = explode("-", $addressBookId);
94
95                 $updates = array();
96
97                 foreach ($mutations as $property=> $newValue) {
98
99                         switch ($property) {
100                                 case '{DAV:}displayname' :
101                                         $updates['displayname'] = $newValue;
102                                         break;
103                                 case '{' . Sabre_CardDAV_Plugin::NS_CARDDAV . '}addressbook-description' :
104                                         $updates['description'] = $newValue;
105                                         break;
106                                 default :
107                                         // If any unsupported values were being updated, we must
108                                         // let the entire request fail.
109                                         return false;
110                         }
111
112                 }
113
114                 // No values are being updated?
115                 if (!$updates) {
116                         return false;
117                 }
118
119                 $query = 'UPDATE ' . CALDAV_SQL_DB . CALDAV_SQL_PREFIX . 'addressbooks_phone SET ctag = ctag + 1 ';
120                 foreach ($updates as $key=> $value) {
121                         $query .= ', `' . dbesc($key) . '` = ' . dbesc($key) . ' ';
122                 }
123                 $query .= ' WHERE id = ' . IntVal($x[1]);
124                 q($query);
125
126                 return true;
127
128         }
129
130         /**
131          * Creates a new address book
132          *
133          * @param string $principalUri
134          * @param string $url Just the 'basename' of the url.
135          * @param array $properties
136          * @throws Sabre_DAV_Exception_BadRequest
137          * @return void
138          */
139         public function createAddressBook($principalUri, $url, array $properties)
140         {
141                 $values = array(
142                         'displayname'  => null,
143                         'description'  => null,
144                         'principaluri' => $principalUri,
145                         'uri'          => $url,
146                 );
147
148                 foreach ($properties as $property=> $newValue) {
149
150                         switch ($property) {
151                                 case '{DAV:}displayname' :
152                                         $values['displayname'] = $newValue;
153                                         break;
154                                 case '{' . Sabre_CardDAV_Plugin::NS_CARDDAV . '}addressbook-description' :
155                                         $values['description'] = $newValue;
156                                         break;
157                                 default :
158                                         throw new Sabre_DAV_Exception_BadRequest('Unknown property: ' . $property);
159                         }
160
161                 }
162
163                 q("INSERT INTO %s%saddressbooks_phone (uri, displayname, description, principaluri, ctag) VALUES ('%s', '%s', '%s', '%s', 1)",
164                         CALDAV_SQL_DB, CALDAV_SQL_PREFIX, dbesc($values["uri"]), dbesc($values["displayname"]), dbesc($values["description"]), dbesc($values["principaluri"])
165                 );
166         }
167
168         /**
169          * Deletes an entire addressbook and all its contents
170          *
171          * @param int $addressBookId
172          * @throws Sabre_DAV_Exception_Forbidden
173          * @return void
174          */
175         public function deleteAddressBook($addressBookId)
176         {
177                 $x = explode("-", $addressBookId);
178                 q("DELETE FROM %s%scards WHERE namespace = %d AND namespace_id = %d", CALDAV_SQL_DB, CALDAV_SQL_PREFIX, IntVal($x[0]), IntVal($x[1]));
179                 q("DELETE FROM %s%saddressbooks_phone WHERE id = %d", CALDAV_SQL_DB, CALDAV_SQL_PREFIX, IntVal($x[1]));
180         }
181
182         /**
183          * Returns all cards for a specific addressbook id.
184          *
185          * This method should return the following properties for each card:
186          *   * carddata - raw vcard data
187          *   * uri - Some unique url
188          *   * lastmodified - A unix timestamp
189          *
190          * It's recommended to also return the following properties:
191          *   * etag - A unique etag. This must change every time the card changes.
192          *   * size - The size of the card in bytes.
193          *
194          * If these last two properties are provided, less time will be spent
195          * calculating them. If they are specified, you can also ommit carddata.
196          * This may speed up certain requests, especially with large cards.
197          *
198          * @param string $addressbookId
199          * @return array
200          */
201         public function getCards($addressbookId)
202         {
203                 $x = explode("-", $addressbookId);
204
205                 $r = q('SELECT id, carddata, uri, lastmodified, etag, size, contact FROM %s%scards WHERE namespace = %d AND namespace_id = %d AND manually_deleted = 0',
206                         CALDAV_SQL_DB, CALDAV_SQL_PREFIX, IntVal($x[0]), IntVal($x[1])
207                 );
208                 if ($r) return $r;
209                 return array();
210         }
211
212         /**
213          * Returns a specfic card.
214          *
215          * The same set of properties must be returned as with getCards. The only
216          * exception is that 'carddata' is absolutely required.
217          *
218          * @param mixed $addressBookId
219          * @param string $cardUri
220          * @throws Sabre_DAV_Exception_NotFound
221          * @return array
222          */
223         public function getCard($addressBookId, $cardUri)
224         {
225                 $x = explode("-", $addressBookId);
226                 $x = q("SELECT id, carddata, uri, lastmodified, etag, size FROM %s%scards WHERE namespace = %d AND namespace_id = %d AND uri = '%s'",
227                         CALDAV_SQL_DB, CALDAV_SQL_PREFIX, IntVal($x[0]), IntVal($x[1]), dbesc($cardUri));
228                 if (count($x) == 0) throw new Sabre_DAV_Exception_NotFound();
229                 return $x[0];
230         }
231
232         /**
233          * Creates a new card.
234          *
235          * The addressbook id will be passed as the first argument. This is the
236          * same id as it is returned from the getAddressbooksForUser method.
237          *
238          * The cardUri is a base uri, and doesn't include the full path. The
239          * cardData argument is the vcard body, and is passed as a string.
240          *
241          * It is possible to return an ETag from this method. This ETag is for the
242          * newly created resource, and must be enclosed with double quotes (that
243          * is, the string itself must contain the double quotes).
244          *
245          * You should only return the ETag if you store the carddata as-is. If a
246          * subsequent GET request on the same card does not have the same body,
247          * byte-by-byte and you did return an ETag here, clients tend to get
248          * confused.
249          *
250          * If you don't return an ETag, you can just return null.
251          *
252          * @param string $addressBookId
253          * @param string $cardUri
254          * @param string $cardData
255          * @throws Sabre_DAV_Exception_Forbidden
256          * @return string
257          */
258         public function createCard($addressBookId, $cardUri, $cardData)
259         {
260                 $x = explode("-", $addressBookId);
261
262                 $etag = md5($cardData);
263                 q("INSERT INTO %s%scards (carddata, uri, lastmodified, namespace, namespace_id, etag, size) VALUES ('%s', '%s', %d, %d, '%s', %d)",
264                         CALDAV_SQL_DB, CALDAV_SQL_PREFIX, dbesc($cardData), dbesc($cardUri), time(), IntVal($x[0]), IntVal($x[1]), $etag, strlen($cardData)
265                 );
266
267                 q('UPDATE %s%saddressbooks_phone SET ctag = ctag + 1 WHERE id = %d', CALDAV_SQL_DB, CALDAV_SQL_PREFIX, IntVal($x[1]));
268
269                 return '"' . $etag . '"';
270
271         }
272
273         /**
274          * Updates a card.
275          *
276          * The addressbook id will be passed as the first argument. This is the
277          * same id as it is returned from the getAddressbooksForUser method.
278          *
279          * The cardUri is a base uri, and doesn't include the full path. The
280          * cardData argument is the vcard body, and is passed as a string.
281          *
282          * It is possible to return an ETag from this method. This ETag should
283          * match that of the updated resource, and must be enclosed with double
284          * quotes (that is: the string itself must contain the actual quotes).
285          *
286          * You should only return the ETag if you store the carddata as-is. If a
287          * subsequent GET request on the same card does not have the same body,
288          * byte-by-byte and you did return an ETag here, clients tend to get
289          * confused.
290          *
291          * If you don't return an ETag, you can just return null.
292          *
293          * @param string $addressBookId
294          * @param string $cardUri
295          * @param string $cardData
296          * @throws Sabre_DAV_Exception_Forbidden
297          * @return string|null
298          */
299         public function updateCard($addressBookId, $cardUri, $cardData)
300         {
301                 $x = explode("-", $addressBookId);
302
303                 $etag = md5($cardData);
304                 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",
305                         CALDAV_SQL_DB, CALDAV_SQL_PREFIX, dbesc($cardData), time(), $etag, strlen($cardData), dbesc($cardUri), IntVal($x[10]), IntVal($x[1])
306                 );
307
308                 q('UPDATE %s%saddressbooks_phone SET ctag = ctag + 1 WHERE id = %d', CALDAV_SQL_DB, CALDAV_SQL_PREFIX, IntVal($x[1]));
309
310                 return '"' . $etag . '"';
311         }
312
313         /**
314          * Deletes a card
315          *
316          * @param string $addressBookId
317          * @param string $cardUri
318          * @throws Sabre_DAV_Exception_Forbidden
319          * @return bool
320          */
321         public function deleteCard($addressBookId, $cardUri)
322         {
323                 $x = explode("-", $addressBookId);
324
325                 q("DELETE FROM %s%scards WHERE namespace = %d AND namespace_id = %d AND uri = '%s'", CALDAV_SQL_DB, CALDAV_SQL_PREFIX, IntVal($x[0]), IntVal($x[1]), dbesc($cardUri));
326                 q('UPDATE %s%saddressbooks_phone SET ctag = ctag + 1 WHERE id = %d', CALDAV_SQL_DB, CALDAV_SQL_PREFIX, IntVal($x[1]));
327
328                 return true;
329         }
330 }