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