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