source: pmb4.1/trunk/fuentes/pmb/admin/connecteurs/out/webdav/lib/Sabre/CardDAV/Backend/PDO.php @ 478

Last change on this file since 478 was 478, checked in by mabarracus, 4 years ago

copy trusty code 4.1

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