3 abstract class Sabre_CardDAV_Backend_Virtual extends Sabre_CardDAV_Backend_Common
9 * @param int $addressbookId
14 abstract public function getItemsByUri($addressbookId, $uri);
19 * @param int $addressbookId
21 static public function invalidateCache($addressbookId) {
22 q("UPDATE %s%saddressbooks SET `needs_rebuild` = 1 WHERE `id` = %d", CALDAV_SQL_DB, CALDAV_SQL_PREFIX, IntVal($addressbookId));
28 * @param int $addressbookId
31 static abstract protected function createCache_internal($addressbookId, $force = false);
34 * @param int $addressbookId
35 * @param null|array $addressbook
38 public function createCache($addressbookId, $addressbook = null, $force = false) {
39 $addressbookId = IntVal($addressbookId);
42 $add = q("SELECT `needs_rebuild`, `uri` FROM %s%saddressbooks WHERE `id` = %d", CALDAV_SQL_DB, CALDAV_SQL_PREFIX, $addressbookId);
43 $addressbook = $add[0];
45 if ($addressbook["needs_rebuild"] == 1 || $force) {
46 static::createCache_internal($addressbookId, $force);
47 q("UPDATE %s%saddressbooks SET `needs_rebuild` = 0, `ctag` = `ctag` + 1 WHERE `id` = %d", CALDAV_SQL_DB, CALDAV_SQL_PREFIX, $addressbookId);
54 * @param int $addressbookId
55 * @param int $contactId
58 static abstract protected function createCardCache($addressbookId, $contactId, $force = false);
61 * @param int $addressbookId
64 public function getCards($addressbookId)
66 $addressbookId = IntVal($addressbookId);
67 $add = q("SELECT * FROM %s%saddressbooks WHERE `id` = %d", CALDAV_SQL_DB, CALDAV_SQL_PREFIX, $addressbookId);
68 if ($add[0]["needs_rebuild"]) {
69 static::createCache_internal($addressbookId);
70 q("UPDATE %s%saddressbooks SET `needs_rebuild` = 0, `ctag` = `ctag` + 1 WHERE `id` = %d", CALDAV_SQL_DB, CALDAV_SQL_PREFIX, $addressbookId);
71 $add[0]["needs_rebuild"] = 0;
76 $x = q("SELECT * FROM %s%saddressbookobjects WHERE `addressbook_id` = %d AND `manually_deleted` = 0", CALDAV_SQL_DB, CALDAV_SQL_PREFIX, $addressbookId);
77 foreach ($x as $y) $ret[] = self::getCard($addressbookId, $add[0]["uri"], $add[0], $y);
84 * Replaces the x-prop_name value. Replaces the prop_name value IF the old value is the same as the old value of x-prop_name (meaning: the user has not manually changed it)
86 * @param Sabre\VObject\Component $component
87 * @param string $prop_name
88 * @param string $prop_value
89 * @param array $parameters
92 static public function card_set_automatic_value(&$component, $prop_name, $prop_value, $parameters = array()) {
93 $automatic = $component->select("X-" . $prop_name);
94 $curr = $component->select($prop_name);
96 if (count($automatic) == 0) {
97 $prop = new Sabre\VObject\Property('X-' . $prop_name, $prop_value);
98 foreach ($parameters as $key=>$val) $prop->add($key, $val);
99 $component->children[] = $prop;
101 if (count($curr) == 0) {
102 $prop = new Sabre\VObject\Property($prop_name, $prop_value);
103 foreach ($parameters as $key=>$val) $prop->add($key, $val);
104 $component->children[] = $prop;
107 } else foreach ($automatic as $auto_prop) {
108 /** @var Sabre\VObject\Property $auto_prop */
109 /** @var Sabre\VObject\Property $actual_prop */
110 foreach ($curr as $actual_prop) {
111 if ($auto_prop->value == $actual_prop->value) $actual_prop->setValue($prop_value);
113 $auto_prop->setValue($prop_value);
120 * Deletes the x-prop_name value. Deletes the prop_name value IF the old value is the same as the old value of x-prop_name (meaning: the user has not manually changed it)
122 * @param Sabre\VObject\Component $component
123 * @param string $prop_name
124 * @param array $parameters
126 static public function card_del_automatic_value(&$component, $prop_name, $parameters = array()) {
131 * @param int $addressbookId
132 * @param string $objectUri
135 * @throws Sabre_DAV_Exception_NotFound
138 public function getCard($addressbookId, $objectUri, $book = null, $obj = null)
140 $addressbookId = IntVal($addressbookId);
143 $add = q("SELECT `needs_rebuild`, `uri` FROM %s%saddressbooks WHERE `id` = %d", CALDAV_SQL_DB, CALDAV_SQL_PREFIX, $addressbookId);
146 if ($book["needs_rebuild"] == 1) {
147 static::createCache_internal($addressbookId);
148 q("UPDATE %s%saddressbooks SET `needs_rebuild` = 0, `ctag` = `ctag` + 1 WHERE `id` = %d", CALDAV_SQL_DB, CALDAV_SQL_PREFIX, $addressbookId);
149 $add[0]["needs_rebuild"] = 0;
153 $r = q("SELECT * FROM %s%saddressbookobjects WHERE `uri` = '%s' AND `addressbook_id` = %d AND `manually_deleted` = 0",
154 CALDAV_SQL_DB, CALDAV_SQL_PREFIX, dbesc($objectUri), IntVal($addressbookId));
155 if (count($r) == 0) throw new Sabre_DAV_Exception_NotFound();
157 if ($obj["needs_rebuild"] == 1) $obj = static::createCardCache($addressbookId, $obj["contact"]);
161 "id" => IntVal($obj["uri"]),
162 "carddata" => $obj["carddata"],
163 "uri" => $obj["uri"],
164 "lastmodified" => $obj["lastmodified"],
165 "addressbookid" => $addressbookId,
166 "etag" => $obj["etag"],
167 "size" => IntVal($obj["size"]),
175 * @param string $principalUri
176 * @param string $addressbookUri
177 * @param array $properties
178 * @throws Sabre_DAV_Exception_Forbidden
181 public function createAddressBook($principalUri, $addressbookUri, array $properties)
183 throw new Sabre_DAV_Exception_Forbidden();
187 * @param string $addressbookId
188 * @throws Sabre_DAV_Exception_Forbidden
191 public function deleteAddressBook($addressbookId)
193 throw new Sabre_DAV_Exception_Forbidden();
198 * @param string $addressbookId
199 * @param string $objectUri
200 * @param string $cardData
201 * @throws Sabre_DAV_Exception_Forbidden
202 * @return null|string|void
204 function createCard($addressbookId, $objectUri, $cardData)
206 throw new Sabre_DAV_Exception_Forbidden();
212 * The addressbook id will be passed as the first argument. This is the
213 * same id as it is returned from the getAddressbooksForUser method.
215 * The cardUri is a base uri, and doesn't include the full path. The
216 * cardData argument is the vcard body, and is passed as a string.
218 * It is possible to return an ETag from this method. This ETag should
219 * match that of the updated resource, and must be enclosed with double
220 * quotes (that is: the string itself must contain the actual quotes).
222 * You should only return the ETag if you store the carddata as-is. If a
223 * subsequent GET request on the same card does not have the same body,
224 * byte-by-byte and you did return an ETag here, clients tend to get
227 * If you don't return an ETag, you can just return null.
229 * @param string $addressBookId
230 * @param string $cardUri
231 * @param string $cardData
232 * @throws Sabre_DAV_Exception_Forbidden
233 * @return string|null
235 public function updateCard($addressBookId, $cardUri, $cardData)
237 echo "Die!"; die(); // @TODO
238 $x = explode("-", $addressBookId);
240 $etag = md5($cardData);
241 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",
242 CALDAV_SQL_DB, CALDAV_SQL_PREFIX, dbesc($cardData), time(), $etag, strlen($cardData), dbesc($cardUri), IntVal($x[10]), IntVal($x[1])
244 q('UPDATE %s%saddressbooks_community SET ctag = ctag + 1 WHERE uid = %d', CALDAV_SQL_DB, CALDAV_SQL_PREFIX, IntVal($x[1]));
246 return '"' . $etag . '"';
255 * @param string $addressBookId
256 * @param string $cardUri
257 * @throws Sabre_DAV_Exception_Forbidden
260 public function deleteCard($addressBookId, $cardUri)
262 q("UPDATE %s%scards SET `manually_deleted` = 1 WHERE `addressbook_id` = %d AND `uri` = '%s'", CALDAV_SQL_DB, CALDAV_SQL_PREFIX, IntVal($addressBookId), dbesc($cardUri));
263 q('UPDATE %s%saddressbooks SET ctag = ctag + 1 WHERE `id` = %d', CALDAV_SQL_DB, CALDAV_SQL_PREFIX, IntVal($addressBookId));