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_Std extends Sabre_CardDAV_Backend_Abstract
20 public function __construct()
26 * Returns the list of addressbooks for a specific user.
28 * @param string $principalUri
31 public function getAddressBooksForUser($principalUri)
33 $uid = dav_compat_principal2uid($principalUri);
35 $addressBooks = array();
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'
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));
44 foreach ($books as $row) {
45 $addressBooks[] = array(
46 'id' => CARDDAV_NAMESPACE_PHONECONTACTS . "-" . $row['id'],
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(),
63 * Updates an addressbook's properties
65 * See Sabre_DAV_IProperties for a description of the mutations array, as
66 * well as the return value.
68 * @param mixed $addressBookId
69 * @param array $mutations
70 * @throws Sabre_DAV_Exception_Forbidden
71 * @see Sabre_DAV_IProperties::updateProperties
74 public function updateAddressBook($addressBookId, array $mutations)
76 $x = explode("-", $addressBookId);
80 foreach ($mutations as $property=> $newValue) {
83 case '{DAV:}displayname' :
84 $updates['displayname'] = $newValue;
86 case '{' . Sabre_CardDAV_Plugin::NS_CARDDAV . '}addressbook-description' :
87 $updates['description'] = $newValue;
90 // If any unsupported values were being updated, we must
91 // let the entire request fail.
97 // No values are being updated?
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) . ' ';
106 $query .= ' WHERE id = ' . IntVal($x[1]);
114 * Creates a new address book
116 * @param string $principalUri
117 * @param string $url Just the 'basename' of the url.
118 * @param array $properties
119 * @throws Sabre_DAV_Exception_BadRequest
122 public function createAddressBook($principalUri, $url, array $properties)
125 'displayname' => null,
126 'description' => null,
127 'principaluri' => $principalUri,
131 foreach ($properties as $property=> $newValue) {
134 case '{DAV:}displayname' :
135 $values['displayname'] = $newValue;
137 case '{' . Sabre_CardDAV_Plugin::NS_CARDDAV . '}addressbook-description' :
138 $values['description'] = $newValue;
141 throw new Sabre_DAV_Exception_BadRequest('Unknown property: ' . $property);
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"])
152 * Deletes an entire addressbook and all its contents
154 * @param int $addressBookId
155 * @throws Sabre_DAV_Exception_Forbidden
158 public function deleteAddressBook($addressBookId)
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]));
166 * Returns all cards for a specific addressbook id.
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
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.
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.
181 * @param string $addressbookId
184 public function getCards($addressbookId)
186 $x = explode("-", $addressbookId);
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])
196 * Returns a specfic card.
198 * The same set of properties must be returned as with getCards. The only
199 * exception is that 'carddata' is absolutely required.
201 * @param mixed $addressBookId
202 * @param string $cardUri
203 * @throws Sabre_DAV_Exception_NotFound
206 public function getCard($addressBookId, $cardUri)
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();
216 * Creates a new card.
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.
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.
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).
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
233 * If you don't return an ETag, you can just return null.
235 * @param string $addressBookId
236 * @param string $cardUri
237 * @param string $cardData
238 * @throws Sabre_DAV_Exception_Forbidden
241 public function createCard($addressBookId, $cardUri, $cardData)
243 $x = explode("-", $addressBookId);
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)
250 q('UPDATE %s%saddressbooks_phone SET ctag = ctag + 1 WHERE id = %d', CALDAV_SQL_DB, CALDAV_SQL_PREFIX, IntVal($x[1]));
252 return '"' . $etag . '"';
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.
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.
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).
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
274 * If you don't return an ETag, you can just return null.
276 * @param string $addressBookId
277 * @param string $cardUri
278 * @param string $cardData
279 * @throws Sabre_DAV_Exception_Forbidden
280 * @return string|null
282 public function updateCard($addressBookId, $cardUri, $cardData)
284 $x = explode("-", $addressBookId);
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])
291 q('UPDATE %s%saddressbooks_phone SET ctag = ctag + 1 WHERE id = %d', CALDAV_SQL_DB, CALDAV_SQL_PREFIX, IntVal($x[1]));
293 return '"' . $etag . '"';
299 * @param string $addressBookId
300 * @param string $cardUri
301 * @throws Sabre_DAV_Exception_Forbidden
304 public function deleteCard($addressBookId, $cardUri)
306 $x = explode("-", $addressBookId);
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]));