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
18 * @var null|Sabre_CardDAV_Backend_Std
20 private static $instance = null;
24 * @return Sabre_CardDAV_Backend_Std
26 public static function getInstance() {
27 if (self::$instance == null) {
28 self::$instance = new Sabre_CardDAV_Backend_Std();
30 return self::$instance;
37 public function __construct()
43 * Returns the list of addressbooks for a specific user.
45 * @param string $principalUri
48 public function getAddressBooksForUser($principalUri)
50 $uid = dav_compat_principal2uid($principalUri);
52 $addressBooks = array();
54 $books = q("SELECT id, uri, displayname, principaluri, description, ctag FROM %s%saddressbooks_phone WHERE principaluri = '%s'", CALDAV_SQL_DB, CALDAV_SQL_PREFIX, dbesc($principalUri));
55 if (count($books) == 0) {
56 q("INSERT INTO %s%saddressbooks_phone (uid, principaluri, displayname, uri, description, ctag) VALUES (%d, '%s', '%s', '%s', '%s', 1)",
57 CALDAV_SQL_DB, CALDAV_SQL_PREFIX, $uid, dbesc($principalUri), 'Other', 'phone', 'Manually added contacts'
59 $books = q("SELECT id, uri, displayname, principaluri, description, ctag FROM %s%saddressbooks_phone WHERE principaluri = '%s'", CALDAV_SQL_DB, CALDAV_SQL_PREFIX, dbesc($principalUri));
61 foreach ($books as $row) {
62 $addressBooks[] = array(
63 'id' => CARDDAV_NAMESPACE_PHONECONTACTS . "-" . $row['id'],
65 'principaluri' => $row['principaluri'],
66 '{DAV:}displayname' => $row['displayname'],
67 '{' . Sabre_CardDAV_Plugin::NS_CARDDAV . '}addressbook-description' => $row['description'],
68 '{http://calendarserver.org/ns/}getctag' => $row['ctag'],
69 '{' . Sabre_CardDAV_Plugin::NS_CARDDAV . '}supported-address-data' =>
70 new Sabre_CardDAV_Property_SupportedAddressData(),
80 * Updates an addressbook's properties
82 * See Sabre_DAV_IProperties for a description of the mutations array, as
83 * well as the return value.
85 * @param mixed $addressBookId
86 * @param array $mutations
87 * @throws Sabre_DAV_Exception_Forbidden
88 * @see Sabre_DAV_IProperties::updateProperties
91 public function updateAddressBook($addressBookId, array $mutations)
93 $x = explode("-", $addressBookId);
97 foreach ($mutations as $property=> $newValue) {
100 case '{DAV:}displayname' :
101 $updates['displayname'] = $newValue;
103 case '{' . Sabre_CardDAV_Plugin::NS_CARDDAV . '}addressbook-description' :
104 $updates['description'] = $newValue;
107 // If any unsupported values were being updated, we must
108 // let the entire request fail.
114 // No values are being updated?
119 $query = 'UPDATE ' . CALDAV_SQL_DB . CALDAV_SQL_PREFIX . 'addressbooks_phone SET ctag = ctag + 1 ';
120 foreach ($updates as $key=> $value) {
121 $query .= ', `' . dbesc($key) . '` = ' . dbesc($key) . ' ';
123 $query .= ' WHERE id = ' . IntVal($x[1]);
131 * Creates a new address book
133 * @param string $principalUri
134 * @param string $url Just the 'basename' of the url.
135 * @param array $properties
136 * @throws Sabre_DAV_Exception_BadRequest
139 public function createAddressBook($principalUri, $url, array $properties)
142 'displayname' => null,
143 'description' => null,
144 'principaluri' => $principalUri,
148 foreach ($properties as $property=> $newValue) {
151 case '{DAV:}displayname' :
152 $values['displayname'] = $newValue;
154 case '{' . Sabre_CardDAV_Plugin::NS_CARDDAV . '}addressbook-description' :
155 $values['description'] = $newValue;
158 throw new Sabre_DAV_Exception_BadRequest('Unknown property: ' . $property);
163 q("INSERT INTO %s%saddressbooks_phone (uri, displayname, description, principaluri, ctag) VALUES ('%s', '%s', '%s', '%s', 1)",
164 CALDAV_SQL_DB, CALDAV_SQL_PREFIX, dbesc($values["uri"]), dbesc($values["displayname"]), dbesc($values["description"]), dbesc($values["principaluri"])
169 * Deletes an entire addressbook and all its contents
171 * @param int $addressBookId
172 * @throws Sabre_DAV_Exception_Forbidden
175 public function deleteAddressBook($addressBookId)
177 $x = explode("-", $addressBookId);
178 q("DELETE FROM %s%scards WHERE namespace = %d AND namespace_id = %d", CALDAV_SQL_DB, CALDAV_SQL_PREFIX, IntVal($x[0]), IntVal($x[1]));
179 q("DELETE FROM %s%saddressbooks_phone WHERE id = %d", CALDAV_SQL_DB, CALDAV_SQL_PREFIX, IntVal($x[1]));
183 * Returns all cards for a specific addressbook id.
185 * This method should return the following properties for each card:
186 * * carddata - raw vcard data
187 * * uri - Some unique url
188 * * lastmodified - A unix timestamp
190 * It's recommended to also return the following properties:
191 * * etag - A unique etag. This must change every time the card changes.
192 * * size - The size of the card in bytes.
194 * If these last two properties are provided, less time will be spent
195 * calculating them. If they are specified, you can also ommit carddata.
196 * This may speed up certain requests, especially with large cards.
198 * @param string $addressbookId
201 public function getCards($addressbookId)
203 $x = explode("-", $addressbookId);
205 $r = q('SELECT id, carddata, uri, lastmodified, etag, size, contact FROM %s%scards WHERE namespace = %d AND namespace_id = %d AND manually_deleted = 0',
206 CALDAV_SQL_DB, CALDAV_SQL_PREFIX, IntVal($x[0]), IntVal($x[1])
213 * Returns a specfic card.
215 * The same set of properties must be returned as with getCards. The only
216 * exception is that 'carddata' is absolutely required.
218 * @param mixed $addressBookId
219 * @param string $cardUri
220 * @throws Sabre_DAV_Exception_NotFound
223 public function getCard($addressBookId, $cardUri)
225 $x = explode("-", $addressBookId);
226 $x = q("SELECT id, carddata, uri, lastmodified, etag, size FROM %s%scards WHERE namespace = %d AND namespace_id = %d AND uri = '%s'",
227 CALDAV_SQL_DB, CALDAV_SQL_PREFIX, IntVal($x[0]), IntVal($x[1]), dbesc($cardUri));
228 if (count($x) == 0) throw new Sabre_DAV_Exception_NotFound();
233 * Creates a new card.
235 * The addressbook id will be passed as the first argument. This is the
236 * same id as it is returned from the getAddressbooksForUser method.
238 * The cardUri is a base uri, and doesn't include the full path. The
239 * cardData argument is the vcard body, and is passed as a string.
241 * It is possible to return an ETag from this method. This ETag is for the
242 * newly created resource, and must be enclosed with double quotes (that
243 * is, the string itself must contain the double quotes).
245 * You should only return the ETag if you store the carddata as-is. If a
246 * subsequent GET request on the same card does not have the same body,
247 * byte-by-byte and you did return an ETag here, clients tend to get
250 * If you don't return an ETag, you can just return null.
252 * @param string $addressBookId
253 * @param string $cardUri
254 * @param string $cardData
255 * @throws Sabre_DAV_Exception_Forbidden
258 public function createCard($addressBookId, $cardUri, $cardData)
260 $x = explode("-", $addressBookId);
262 $etag = md5($cardData);
263 q("INSERT INTO %s%scards (carddata, uri, lastmodified, namespace, namespace_id, etag, size) VALUES ('%s', '%s', %d, %d, '%s', %d)",
264 CALDAV_SQL_DB, CALDAV_SQL_PREFIX, dbesc($cardData), dbesc($cardUri), time(), IntVal($x[0]), IntVal($x[1]), $etag, strlen($cardData)
267 q('UPDATE %s%saddressbooks_phone SET ctag = ctag + 1 WHERE id = %d', CALDAV_SQL_DB, CALDAV_SQL_PREFIX, IntVal($x[1]));
269 return '"' . $etag . '"';
276 * The addressbook id will be passed as the first argument. This is the
277 * same id as it is returned from the getAddressbooksForUser method.
279 * The cardUri is a base uri, and doesn't include the full path. The
280 * cardData argument is the vcard body, and is passed as a string.
282 * It is possible to return an ETag from this method. This ETag should
283 * match that of the updated resource, and must be enclosed with double
284 * quotes (that is: the string itself must contain the actual quotes).
286 * You should only return the ETag if you store the carddata as-is. If a
287 * subsequent GET request on the same card does not have the same body,
288 * byte-by-byte and you did return an ETag here, clients tend to get
291 * If you don't return an ETag, you can just return null.
293 * @param string $addressBookId
294 * @param string $cardUri
295 * @param string $cardData
296 * @throws Sabre_DAV_Exception_Forbidden
297 * @return string|null
299 public function updateCard($addressBookId, $cardUri, $cardData)
301 $x = explode("-", $addressBookId);
303 $etag = md5($cardData);
304 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",
305 CALDAV_SQL_DB, CALDAV_SQL_PREFIX, dbesc($cardData), time(), $etag, strlen($cardData), dbesc($cardUri), IntVal($x[10]), IntVal($x[1])
308 q('UPDATE %s%saddressbooks_phone SET ctag = ctag + 1 WHERE id = %d', CALDAV_SQL_DB, CALDAV_SQL_PREFIX, IntVal($x[1]));
310 return '"' . $etag . '"';
316 * @param string $addressBookId
317 * @param string $cardUri
318 * @throws Sabre_DAV_Exception_Forbidden
321 public function deleteCard($addressBookId, $cardUri)
323 $x = explode("-", $addressBookId);
325 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));
326 q('UPDATE %s%saddressbooks_phone SET ctag = ctag + 1 WHERE id = %d', CALDAV_SQL_DB, CALDAV_SQL_PREFIX, IntVal($x[1]));