6 * This CardDAV backend uses PDO to store addressbooks
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
14 class Sabre_CardDAV_Backend_PDO extends Sabre_CardDAV_Backend_Abstract {
24 * The PDO table name used to store addressbooks
26 protected $addressBooksTableName;
29 * The PDO table name used to store cards
31 protected $cardsTableName;
37 * @param string $addressBooksTableName
38 * @param string $cardsTableName
40 public function __construct(PDO $pdo, $addressBooksTableName = 'addressbooks', $cardsTableName = 'cards') {
43 $this->addressBooksTableName = $addressBooksTableName;
44 $this->cardsTableName = $cardsTableName;
49 * Returns the list of addressbooks for a specific user.
51 * @param string $principalUri
54 public function getAddressBooksForUser($principalUri) {
56 $stmt = $this->pdo->prepare('SELECT id, uri, displayname, principaluri, description, ctag FROM '.$this->addressBooksTableName.' WHERE principaluri = ?');
57 $stmt->execute(array($principalUri));
59 $addressBooks = array();
61 foreach($stmt->fetchAll() as $row) {
63 $addressBooks[] = array(
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(),
82 * Updates an addressbook's properties
84 * See Sabre_DAV_IProperties for a description of the mutations array, as
85 * well as the return value.
87 * @param mixed $addressBookId
88 * @param array $mutations
89 * @see Sabre_DAV_IProperties::updateProperties
92 public function updateAddressBook($addressBookId, array $mutations) {
96 foreach($mutations as $property=>$newValue) {
99 case '{DAV:}displayname' :
100 $updates['displayname'] = $newValue;
102 case '{' . Sabre_CardDAV_Plugin::NS_CARDDAV . '}addressbook-description' :
103 $updates['description'] = $newValue;
106 // If any unsupported values were being updated, we must
107 // let the entire request fail.
113 // No values are being updated?
118 $query = 'UPDATE ' . $this->addressBooksTableName . ' SET ctag = ctag + 1 ';
119 foreach($updates as $key=>$value) {
120 $query.=', `' . $key . '` = :' . $key . ' ';
122 $query.=' WHERE id = :addressbookid';
124 $stmt = $this->pdo->prepare($query);
125 $updates['addressbookid'] = $addressBookId;
127 $stmt->execute($updates);
134 * Creates a new address book
136 * @param string $principalUri
137 * @param string $url Just the 'basename' of the url.
138 * @param array $properties
141 public function createAddressBook($principalUri, $url, array $properties) {
144 'displayname' => null,
145 'description' => null,
146 'principaluri' => $principalUri,
150 foreach($properties as $property=>$newValue) {
153 case '{DAV:}displayname' :
154 $values['displayname'] = $newValue;
156 case '{' . Sabre_CardDAV_Plugin::NS_CARDDAV . '}addressbook-description' :
157 $values['description'] = $newValue;
160 throw new Sabre_DAV_Exception_BadRequest('Unknown property: ' . $property);
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);
172 * Deletes an entire addressbook and all its contents
174 * @param int $addressBookId
177 public function deleteAddressBook($addressBookId) {
179 $stmt = $this->pdo->prepare('DELETE FROM ' . $this->cardsTableName . ' WHERE addressbookid = ?');
180 $stmt->execute(array($addressBookId));
182 $stmt = $this->pdo->prepare('DELETE FROM ' . $this->addressBooksTableName . ' WHERE id = ?');
183 $stmt->execute(array($addressBookId));
188 * Returns all cards for a specific addressbook id.
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
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.
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.
203 * @param mixed $addressbookId
206 public function getCards($addressbookId) {
208 $stmt = $this->pdo->prepare('SELECT id, carddata, uri, lastmodified FROM ' . $this->cardsTableName . ' WHERE addressbookid = ?');
209 $stmt->execute(array($addressbookId));
211 return $stmt->fetchAll(PDO::FETCH_ASSOC);
217 * Returns a specfic card.
219 * The same set of properties must be returned as with getCards. The only
220 * exception is that 'carddata' is absolutely required.
222 * @param mixed $addressBookId
223 * @param string $cardUri
226 public function getCard($addressBookId, $cardUri) {
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));
231 $result = $stmt->fetchAll(PDO::FETCH_ASSOC);
233 return (count($result)>0?$result[0]:false);
238 * Creates a new card.
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.
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.
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).
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
255 * If you don't return an ETag, you can just return null.
257 * @param mixed $addressBookId
258 * @param string $cardUri
259 * @param string $cardData
260 * @return string|null
262 public function createCard($addressBookId, $cardUri, $cardData) {
264 $stmt = $this->pdo->prepare('INSERT INTO ' . $this->cardsTableName . ' (carddata, uri, lastmodified, addressbookid) VALUES (?, ?, ?, ?)');
266 $result = $stmt->execute(array($cardData, $cardUri, time(), $addressBookId));
268 $stmt2 = $this->pdo->prepare('UPDATE ' . $this->addressBooksTableName . ' SET ctag = ctag + 1 WHERE id = ?');
269 $stmt2->execute(array($addressBookId));
271 return '"' . md5($cardData) . '"';
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.
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.
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).
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
293 * If you don't return an ETag, you can just return null.
295 * @param mixed $addressBookId
296 * @param string $cardUri
297 * @param string $cardData
298 * @return string|null
300 public function updateCard($addressBookId, $cardUri, $cardData) {
302 $stmt = $this->pdo->prepare('UPDATE ' . $this->cardsTableName . ' SET carddata = ?, lastmodified = ? WHERE uri = ? AND addressbookid =?');
303 $stmt->execute(array($cardData, time(), $cardUri, $addressBookId));
305 $stmt2 = $this->pdo->prepare('UPDATE ' . $this->addressBooksTableName . ' SET ctag = ctag + 1 WHERE id = ?');
306 $stmt2->execute(array($addressBookId));
308 return '"' . md5($cardData) . '"';
315 * @param mixed $addressBookId
316 * @param string $cardUri
319 public function deleteCard($addressBookId, $cardUri) {
321 $stmt = $this->pdo->prepare('DELETE FROM ' . $this->cardsTableName . ' WHERE addressbookid = ? AND uri = ?');
322 $stmt->execute(array($addressBookId, $cardUri));
324 $stmt2 = $this->pdo->prepare('UPDATE ' . $this->addressBooksTableName . ' SET ctag = ctag + 1 WHERE id = ?');
325 $stmt2->execute(array($addressBookId));
327 return $stmt->rowCount()===1;