3 use Friendica\Core\L10n;
5 class Sabre_CardDAV_Backend_Std extends Sabre_CardDAV_Backend_Common
9 * @var null|Sabre_CardDAV_Backend_Std
11 private static $instance = null;
15 * @return Sabre_CardDAV_Backend_Std
17 public static function getInstance() {
18 if (self::$instance == null) {
19 self::$instance = new Sabre_CardDAV_Backend_Std();
21 return self::$instance;
28 public function __construct()
37 public function getNamespace()
39 return CARDDAV_NAMESPACE_PRIVATE;
46 public static function getBackendTypeName()
48 return L10n::t("Private Addressbooks");
52 * Returns the list of addressbooks for a specific user.
54 * @param string $principalUri
57 public function getAddressBooksForUser($principalUri)
59 $n = dav_compat_principal2namespace($principalUri);
60 if ($n["namespace"] != $this->getNamespace()) return array();
62 $addressBooks = array();
64 $books = q("SELECT * FROM %s%saddressbooks WHERE `namespace` = %d AND `namespace_id` = %d", CALDAV_SQL_DB, CALDAV_SQL_PREFIX, IntVal($n["namespace"]), IntVal($n["namespace_id"]));
65 foreach ($books as $row) {
66 if (in_array($row["uri"], $GLOBALS["CARDDAV_PRIVATE_SYSTEM_ADDRESSBOOKS"])) continue;
68 $addressBooks[] = array(
71 'principaluri' => $principalUri,
72 '{DAV:}displayname' => $row['displayname'],
73 '{' . Sabre_CardDAV_Plugin::NS_CARDDAV . '}addressbook-description' => $row['description'],
74 '{http://calendarserver.org/ns/}getctag' => $row['ctag'],
75 '{' . Sabre_CardDAV_Plugin::NS_CARDDAV . '}supported-address-data' =>
76 new Sabre_CardDAV_Property_SupportedAddressData(),
86 * Creates a new address book
88 * @param string $principalUri
89 * @param string $url Just the 'basename' of the url.
90 * @param array $properties
91 * @throws Sabre_DAV_Exception_BadRequest
94 public function createAddressBook($principalUri, $url, array $properties)
96 $uid = dav_compat_principal2uid($principalUri);
99 'displayname' => null,
100 'description' => null,
101 'principaluri' => $principalUri,
105 foreach ($properties as $property=> $newValue) {
108 case '{DAV:}displayname' :
109 $values['displayname'] = $newValue;
111 case '{' . Sabre_CardDAV_Plugin::NS_CARDDAV . '}addressbook-description' :
112 $values['description'] = $newValue;
115 throw new Sabre_DAV_Exception_BadRequest('Unknown property: ' . $property);
120 q("INSERT INTO %s%saddressbooks (`uri`, `displayname`, `description`, `namespace`, `namespace_id`, `ctag`) VALUES ('%s', '%s', '%s', %d, %d, 1)",
121 CALDAV_SQL_DB, CALDAV_SQL_PREFIX, dbesc($values["uri"]), dbesc($values["displayname"]), dbesc($values["description"]), CARDDAV_NAMESPACE_PRIVATE, IntVal($uid)
126 * Deletes an entire addressbook and all its contents
128 * @param int $addressBookId
129 * @throws Sabre_DAV_Exception_Forbidden
132 public function deleteAddressBook($addressBookId)
134 q("DELETE FROM %s%saddressbookobjects WHERE `id` = %d", CALDAV_SQL_DB, CALDAV_SQL_PREFIX, IntVal($addressBookId));
135 q("DELETE FROM %s%saddressbooks WHERE `addressbook_id` = %d", CALDAV_SQL_DB, CALDAV_SQL_PREFIX, IntVal($addressBookId));
139 * Returns all cards for a specific addressbook id.
141 * This method should return the following properties for each card:
142 * * carddata - raw vcard data
143 * * uri - Some unique url
144 * * lastmodified - A unix timestamp
146 * It's recommended to also return the following properties:
147 * * etag - A unique etag. This must change every time the card changes.
148 * * size - The size of the card in bytes.
150 * If these last two properties are provided, less time will be spent
151 * calculating them. If they are specified, you can also ommit carddata.
152 * This may speed up certain requests, especially with large cards.
154 * @param string $addressbookId
157 public function getCards($addressbookId)
159 $r = q('SELECT `id`, `carddata`, `uri`, `lastmodified`, `etag`, `size`, `contact` FROM %s%saddressbookobjects WHERE `addressbook_id` = %d AND `manually_deleted` = 0',
160 CALDAV_SQL_DB, CALDAV_SQL_PREFIX, IntVal($addressbookId)
167 * Returns a specfic card.
169 * The same set of properties must be returned as with getCards. The only
170 * exception is that 'carddata' is absolutely required.
172 * @param mixed $addressBookId
173 * @param string $cardUri
174 * @throws Sabre_DAV_Exception_NotFound
177 public function getCard($addressBookId, $cardUri)
179 $x = q("SELECT `id`, `carddata`, `uri`, `lastmodified`, `etag`, `size` FROM %s%saddressbookobjects WHERE `addressbook_id` = %d AND `uri` = '%s'",
180 CALDAV_SQL_DB, CALDAV_SQL_PREFIX, IntVal($addressBookId), dbesc($cardUri));
181 if (count($x) == 0) throw new Sabre_DAV_Exception_NotFound();
186 * Creates a new card.
188 * The addressbook id will be passed as the first argument. This is the
189 * same id as it is returned from the getAddressbooksForUser method.
191 * The cardUri is a base uri, and doesn't include the full path. The
192 * cardData argument is the vcard body, and is passed as a string.
194 * It is possible to return an ETag from this method. This ETag is for the
195 * newly created resource, and must be enclosed with double quotes (that
196 * is, the string itself must contain the double quotes).
198 * You should only return the ETag if you store the carddata as-is. If a
199 * subsequent GET request on the same card does not have the same body,
200 * byte-by-byte and you did return an ETag here, clients tend to get
203 * If you don't return an ETag, you can just return null.
205 * @param string $addressBookId
206 * @param string $cardUri
207 * @param string $cardData
208 * @throws Sabre_DAV_Exception_Forbidden
211 public function createCard($addressBookId, $cardUri, $cardData)
213 $etag = md5($cardData);
214 q("INSERT INTO %s%saddressbookobjects (`carddata`, `uri`, `lastmodified`, `addressbook_id`, `etag`, `size`) VALUES ('%s', '%s', NOW(), %d, '%s', %d)",
215 CALDAV_SQL_DB, CALDAV_SQL_PREFIX, dbesc($cardData), dbesc($cardUri), IntVal($addressBookId), dbesc($etag), strlen($cardData)
218 q('UPDATE %s%saddressbooks SET `ctag` = `ctag` + 1 WHERE `id` = %d', CALDAV_SQL_DB, CALDAV_SQL_PREFIX, IntVal($addressBookId));
220 return '"' . $etag . '"';
227 * The addressbook id will be passed as the first argument. This is the
228 * same id as it is returned from the getAddressbooksForUser method.
230 * The cardUri is a base uri, and doesn't include the full path. The
231 * cardData argument is the vcard body, and is passed as a string.
233 * It is possible to return an ETag from this method. This ETag should
234 * match that of the updated resource, and must be enclosed with double
235 * quotes (that is: the string itself must contain the actual quotes).
237 * You should only return the ETag if you store the carddata as-is. If a
238 * subsequent GET request on the same card does not have the same body,
239 * byte-by-byte and you did return an ETag here, clients tend to get
242 * If you don't return an ETag, you can just return null.
244 * @param string $addressBookId
245 * @param string $cardUri
246 * @param string $cardData
247 * @throws Sabre_DAV_Exception_Forbidden
248 * @return string|null
250 public function updateCard($addressBookId, $cardUri, $cardData)
252 $etag = md5($cardData);
253 q("UPDATE %s%saddressbookobjects SET `carddata` = '%s', `lastmodified` = NOW(), `etag` = '%s', `size` = %d, `manually_edited` = 1 WHERE `uri` = '%s' AND `addressbook_id` = %d",
254 CALDAV_SQL_DB, CALDAV_SQL_PREFIX, dbesc($cardData), dbesc($etag), strlen($cardData), dbesc($cardUri), IntVal($addressBookId)
257 q('UPDATE %s%saddressbooks SET `ctag` = `ctag` + 1 WHERE `id` = %d', CALDAV_SQL_DB, CALDAV_SQL_PREFIX, IntVal($addressBookId));
259 return '"' . $etag . '"';
265 * @param string $addressBookId
266 * @param string $cardUri
267 * @throws Sabre_DAV_Exception_Forbidden
270 public function deleteCard($addressBookId, $cardUri)
272 q("DELETE FROM %s%saddressbookobjects WHERE `addressbook_id` = %d AND `uri` = '%s'", CALDAV_SQL_DB, CALDAV_SQL_PREFIX, IntVal($addressBookId), dbesc($cardUri));
273 q('UPDATE %s%saddressbooks SET `ctag` = `ctag` + 1 WHERE `id` = %d', CALDAV_SQL_DB, CALDAV_SQL_PREFIX, IntVal($addressBookId));