@@ -48,1036 +48,1036 @@ |
||
48 | 48 | |
49 | 49 | class CardDavBackend implements BackendInterface, SyncSupport { |
50 | 50 | |
51 | - const PERSONAL_ADDRESSBOOK_URI = 'contacts'; |
|
52 | - const PERSONAL_ADDRESSBOOK_NAME = 'Contacts'; |
|
53 | - |
|
54 | - /** @var Principal */ |
|
55 | - private $principalBackend; |
|
56 | - |
|
57 | - /** @var string */ |
|
58 | - private $dbCardsTable = 'cards'; |
|
59 | - |
|
60 | - /** @var string */ |
|
61 | - private $dbCardsPropertiesTable = 'cards_properties'; |
|
62 | - |
|
63 | - /** @var IDBConnection */ |
|
64 | - private $db; |
|
65 | - |
|
66 | - /** @var Backend */ |
|
67 | - private $sharingBackend; |
|
68 | - |
|
69 | - /** @var array properties to index */ |
|
70 | - public static $indexProperties = array( |
|
71 | - 'BDAY', 'UID', 'N', 'FN', 'TITLE', 'ROLE', 'NOTE', 'NICKNAME', |
|
72 | - 'ORG', 'CATEGORIES', 'EMAIL', 'TEL', 'IMPP', 'ADR', 'URL', 'GEO', 'CLOUD'); |
|
73 | - |
|
74 | - /** |
|
75 | - * @var string[] Map of uid => display name |
|
76 | - */ |
|
77 | - protected $userDisplayNames; |
|
78 | - |
|
79 | - /** @var IUserManager */ |
|
80 | - private $userManager; |
|
81 | - |
|
82 | - /** @var EventDispatcherInterface */ |
|
83 | - private $dispatcher; |
|
84 | - |
|
85 | - /** |
|
86 | - * CardDavBackend constructor. |
|
87 | - * |
|
88 | - * @param IDBConnection $db |
|
89 | - * @param Principal $principalBackend |
|
90 | - * @param IUserManager $userManager |
|
91 | - * @param EventDispatcherInterface $dispatcher |
|
92 | - */ |
|
93 | - public function __construct(IDBConnection $db, |
|
94 | - Principal $principalBackend, |
|
95 | - IUserManager $userManager, |
|
96 | - EventDispatcherInterface $dispatcher = null) { |
|
97 | - $this->db = $db; |
|
98 | - $this->principalBackend = $principalBackend; |
|
99 | - $this->userManager = $userManager; |
|
100 | - $this->dispatcher = $dispatcher; |
|
101 | - $this->sharingBackend = new Backend($this->db, $principalBackend, 'addressbook'); |
|
102 | - } |
|
103 | - |
|
104 | - /** |
|
105 | - * Return the number of address books for a principal |
|
106 | - * |
|
107 | - * @param $principalUri |
|
108 | - * @return int |
|
109 | - */ |
|
110 | - public function getAddressBooksForUserCount($principalUri) { |
|
111 | - $principalUri = $this->convertPrincipal($principalUri, true); |
|
112 | - $query = $this->db->getQueryBuilder(); |
|
113 | - $query->select($query->createFunction('COUNT(*)')) |
|
114 | - ->from('addressbooks') |
|
115 | - ->where($query->expr()->eq('principaluri', $query->createNamedParameter($principalUri))); |
|
116 | - |
|
117 | - return (int)$query->execute()->fetchColumn(); |
|
118 | - } |
|
119 | - |
|
120 | - /** |
|
121 | - * Returns the list of address books for a specific user. |
|
122 | - * |
|
123 | - * Every addressbook should have the following properties: |
|
124 | - * id - an arbitrary unique id |
|
125 | - * uri - the 'basename' part of the url |
|
126 | - * principaluri - Same as the passed parameter |
|
127 | - * |
|
128 | - * Any additional clark-notation property may be passed besides this. Some |
|
129 | - * common ones are : |
|
130 | - * {DAV:}displayname |
|
131 | - * {urn:ietf:params:xml:ns:carddav}addressbook-description |
|
132 | - * {http://calendarserver.org/ns/}getctag |
|
133 | - * |
|
134 | - * @param string $principalUri |
|
135 | - * @return array |
|
136 | - */ |
|
137 | - function getAddressBooksForUser($principalUri) { |
|
138 | - $principalUriOriginal = $principalUri; |
|
139 | - $principalUri = $this->convertPrincipal($principalUri, true); |
|
140 | - $query = $this->db->getQueryBuilder(); |
|
141 | - $query->select(['id', 'uri', 'displayname', 'principaluri', 'description', 'synctoken']) |
|
142 | - ->from('addressbooks') |
|
143 | - ->where($query->expr()->eq('principaluri', $query->createNamedParameter($principalUri))); |
|
144 | - |
|
145 | - $addressBooks = []; |
|
146 | - |
|
147 | - $result = $query->execute(); |
|
148 | - while($row = $result->fetch()) { |
|
149 | - $addressBooks[$row['id']] = [ |
|
150 | - 'id' => $row['id'], |
|
151 | - 'uri' => $row['uri'], |
|
152 | - 'principaluri' => $this->convertPrincipal($row['principaluri'], false), |
|
153 | - '{DAV:}displayname' => $row['displayname'], |
|
154 | - '{' . Plugin::NS_CARDDAV . '}addressbook-description' => $row['description'], |
|
155 | - '{http://calendarserver.org/ns/}getctag' => $row['synctoken'], |
|
156 | - '{http://sabredav.org/ns}sync-token' => $row['synctoken']?$row['synctoken']:'0', |
|
157 | - ]; |
|
158 | - } |
|
159 | - $result->closeCursor(); |
|
160 | - |
|
161 | - // query for shared calendars |
|
162 | - $principals = $this->principalBackend->getGroupMembership($principalUriOriginal, true); |
|
163 | - $principals[]= $principalUri; |
|
164 | - |
|
165 | - $query = $this->db->getQueryBuilder(); |
|
166 | - $result = $query->select(['a.id', 'a.uri', 'a.displayname', 'a.principaluri', 'a.description', 'a.synctoken', 's.access']) |
|
167 | - ->from('dav_shares', 's') |
|
168 | - ->join('s', 'addressbooks', 'a', $query->expr()->eq('s.resourceid', 'a.id')) |
|
169 | - ->where($query->expr()->in('s.principaluri', $query->createParameter('principaluri'))) |
|
170 | - ->andWhere($query->expr()->eq('s.type', $query->createParameter('type'))) |
|
171 | - ->setParameter('type', 'addressbook') |
|
172 | - ->setParameter('principaluri', $principals, IQueryBuilder::PARAM_STR_ARRAY) |
|
173 | - ->execute(); |
|
174 | - |
|
175 | - $readOnlyPropertyName = '{' . \OCA\DAV\DAV\Sharing\Plugin::NS_OWNCLOUD . '}read-only'; |
|
176 | - while($row = $result->fetch()) { |
|
177 | - $readOnly = (int) $row['access'] === Backend::ACCESS_READ; |
|
178 | - if (isset($addressBooks[$row['id']])) { |
|
179 | - if ($readOnly) { |
|
180 | - // New share can not have more permissions then the old one. |
|
181 | - continue; |
|
182 | - } |
|
183 | - if (isset($addressBooks[$row['id']][$readOnlyPropertyName]) && |
|
184 | - $addressBooks[$row['id']][$readOnlyPropertyName] === 0) { |
|
185 | - // Old share is already read-write, no more permissions can be gained |
|
186 | - continue; |
|
187 | - } |
|
188 | - } |
|
189 | - |
|
190 | - list(, $name) = URLUtil::splitPath($row['principaluri']); |
|
191 | - $uri = $row['uri'] . '_shared_by_' . $name; |
|
192 | - $displayName = $row['displayname'] . ' (' . $this->getUserDisplayName($name) . ')'; |
|
193 | - |
|
194 | - $addressBooks[$row['id']] = [ |
|
195 | - 'id' => $row['id'], |
|
196 | - 'uri' => $uri, |
|
197 | - 'principaluri' => $principalUriOriginal, |
|
198 | - '{DAV:}displayname' => $displayName, |
|
199 | - '{' . Plugin::NS_CARDDAV . '}addressbook-description' => $row['description'], |
|
200 | - '{http://calendarserver.org/ns/}getctag' => $row['synctoken'], |
|
201 | - '{http://sabredav.org/ns}sync-token' => $row['synctoken']?$row['synctoken']:'0', |
|
202 | - '{' . \OCA\DAV\DAV\Sharing\Plugin::NS_OWNCLOUD . '}owner-principal' => $row['principaluri'], |
|
203 | - $readOnlyPropertyName => $readOnly, |
|
204 | - ]; |
|
205 | - } |
|
206 | - $result->closeCursor(); |
|
207 | - |
|
208 | - return array_values($addressBooks); |
|
209 | - } |
|
210 | - |
|
211 | - public function getUsersOwnAddressBooks($principalUri) { |
|
212 | - $principalUriOriginal = $principalUri; |
|
213 | - $principalUri = $this->convertPrincipal($principalUri, true); |
|
214 | - $query = $this->db->getQueryBuilder(); |
|
215 | - $query->select(['id', 'uri', 'displayname', 'principaluri', 'description', 'synctoken']) |
|
216 | - ->from('addressbooks') |
|
217 | - ->where($query->expr()->eq('principaluri', $query->createNamedParameter($principalUri))); |
|
218 | - |
|
219 | - $addressBooks = []; |
|
220 | - |
|
221 | - $result = $query->execute(); |
|
222 | - while($row = $result->fetch()) { |
|
223 | - $addressBooks[$row['id']] = [ |
|
224 | - 'id' => $row['id'], |
|
225 | - 'uri' => $row['uri'], |
|
226 | - 'principaluri' => $this->convertPrincipal($row['principaluri'], false), |
|
227 | - '{DAV:}displayname' => $row['displayname'], |
|
228 | - '{' . Plugin::NS_CARDDAV . '}addressbook-description' => $row['description'], |
|
229 | - '{http://calendarserver.org/ns/}getctag' => $row['synctoken'], |
|
230 | - '{http://sabredav.org/ns}sync-token' => $row['synctoken']?$row['synctoken']:'0', |
|
231 | - ]; |
|
232 | - } |
|
233 | - $result->closeCursor(); |
|
234 | - |
|
235 | - return array_values($addressBooks); |
|
236 | - } |
|
237 | - |
|
238 | - private function getUserDisplayName($uid) { |
|
239 | - if (!isset($this->userDisplayNames[$uid])) { |
|
240 | - $user = $this->userManager->get($uid); |
|
241 | - |
|
242 | - if ($user instanceof IUser) { |
|
243 | - $this->userDisplayNames[$uid] = $user->getDisplayName(); |
|
244 | - } else { |
|
245 | - $this->userDisplayNames[$uid] = $uid; |
|
246 | - } |
|
247 | - } |
|
248 | - |
|
249 | - return $this->userDisplayNames[$uid]; |
|
250 | - } |
|
251 | - |
|
252 | - /** |
|
253 | - * @param int $addressBookId |
|
254 | - */ |
|
255 | - public function getAddressBookById($addressBookId) { |
|
256 | - $query = $this->db->getQueryBuilder(); |
|
257 | - $result = $query->select(['id', 'uri', 'displayname', 'principaluri', 'description', 'synctoken']) |
|
258 | - ->from('addressbooks') |
|
259 | - ->where($query->expr()->eq('id', $query->createNamedParameter($addressBookId))) |
|
260 | - ->execute(); |
|
261 | - |
|
262 | - $row = $result->fetch(); |
|
263 | - $result->closeCursor(); |
|
264 | - if ($row === false) { |
|
265 | - return null; |
|
266 | - } |
|
267 | - |
|
268 | - return [ |
|
269 | - 'id' => $row['id'], |
|
270 | - 'uri' => $row['uri'], |
|
271 | - 'principaluri' => $row['principaluri'], |
|
272 | - '{DAV:}displayname' => $row['displayname'], |
|
273 | - '{' . Plugin::NS_CARDDAV . '}addressbook-description' => $row['description'], |
|
274 | - '{http://calendarserver.org/ns/}getctag' => $row['synctoken'], |
|
275 | - '{http://sabredav.org/ns}sync-token' => $row['synctoken']?$row['synctoken']:'0', |
|
276 | - ]; |
|
277 | - } |
|
278 | - |
|
279 | - /** |
|
280 | - * @param $addressBookUri |
|
281 | - * @return array|null |
|
282 | - */ |
|
283 | - public function getAddressBooksByUri($principal, $addressBookUri) { |
|
284 | - $query = $this->db->getQueryBuilder(); |
|
285 | - $result = $query->select(['id', 'uri', 'displayname', 'principaluri', 'description', 'synctoken']) |
|
286 | - ->from('addressbooks') |
|
287 | - ->where($query->expr()->eq('uri', $query->createNamedParameter($addressBookUri))) |
|
288 | - ->andWhere($query->expr()->eq('principaluri', $query->createNamedParameter($principal))) |
|
289 | - ->setMaxResults(1) |
|
290 | - ->execute(); |
|
291 | - |
|
292 | - $row = $result->fetch(); |
|
293 | - $result->closeCursor(); |
|
294 | - if ($row === false) { |
|
295 | - return null; |
|
296 | - } |
|
297 | - |
|
298 | - return [ |
|
299 | - 'id' => $row['id'], |
|
300 | - 'uri' => $row['uri'], |
|
301 | - 'principaluri' => $row['principaluri'], |
|
302 | - '{DAV:}displayname' => $row['displayname'], |
|
303 | - '{' . Plugin::NS_CARDDAV . '}addressbook-description' => $row['description'], |
|
304 | - '{http://calendarserver.org/ns/}getctag' => $row['synctoken'], |
|
305 | - '{http://sabredav.org/ns}sync-token' => $row['synctoken']?$row['synctoken']:'0', |
|
306 | - ]; |
|
307 | - } |
|
308 | - |
|
309 | - /** |
|
310 | - * Updates properties for an address book. |
|
311 | - * |
|
312 | - * The list of mutations is stored in a Sabre\DAV\PropPatch object. |
|
313 | - * To do the actual updates, you must tell this object which properties |
|
314 | - * you're going to process with the handle() method. |
|
315 | - * |
|
316 | - * Calling the handle method is like telling the PropPatch object "I |
|
317 | - * promise I can handle updating this property". |
|
318 | - * |
|
319 | - * Read the PropPatch documentation for more info and examples. |
|
320 | - * |
|
321 | - * @param string $addressBookId |
|
322 | - * @param \Sabre\DAV\PropPatch $propPatch |
|
323 | - * @return void |
|
324 | - */ |
|
325 | - function updateAddressBook($addressBookId, \Sabre\DAV\PropPatch $propPatch) { |
|
326 | - $supportedProperties = [ |
|
327 | - '{DAV:}displayname', |
|
328 | - '{' . Plugin::NS_CARDDAV . '}addressbook-description', |
|
329 | - ]; |
|
330 | - |
|
331 | - $propPatch->handle($supportedProperties, function($mutations) use ($addressBookId) { |
|
332 | - |
|
333 | - $updates = []; |
|
334 | - foreach($mutations as $property=>$newValue) { |
|
335 | - |
|
336 | - switch($property) { |
|
337 | - case '{DAV:}displayname' : |
|
338 | - $updates['displayname'] = $newValue; |
|
339 | - break; |
|
340 | - case '{' . Plugin::NS_CARDDAV . '}addressbook-description' : |
|
341 | - $updates['description'] = $newValue; |
|
342 | - break; |
|
343 | - } |
|
344 | - } |
|
345 | - $query = $this->db->getQueryBuilder(); |
|
346 | - $query->update('addressbooks'); |
|
347 | - |
|
348 | - foreach($updates as $key=>$value) { |
|
349 | - $query->set($key, $query->createNamedParameter($value)); |
|
350 | - } |
|
351 | - $query->where($query->expr()->eq('id', $query->createNamedParameter($addressBookId))) |
|
352 | - ->execute(); |
|
353 | - |
|
354 | - $this->addChange($addressBookId, "", 2); |
|
355 | - |
|
356 | - return true; |
|
357 | - |
|
358 | - }); |
|
359 | - } |
|
360 | - |
|
361 | - /** |
|
362 | - * Creates a new address book |
|
363 | - * |
|
364 | - * @param string $principalUri |
|
365 | - * @param string $url Just the 'basename' of the url. |
|
366 | - * @param array $properties |
|
367 | - * @return int |
|
368 | - * @throws BadRequest |
|
369 | - */ |
|
370 | - function createAddressBook($principalUri, $url, array $properties) { |
|
371 | - $values = [ |
|
372 | - 'displayname' => null, |
|
373 | - 'description' => null, |
|
374 | - 'principaluri' => $principalUri, |
|
375 | - 'uri' => $url, |
|
376 | - 'synctoken' => 1 |
|
377 | - ]; |
|
378 | - |
|
379 | - foreach($properties as $property=>$newValue) { |
|
380 | - |
|
381 | - switch($property) { |
|
382 | - case '{DAV:}displayname' : |
|
383 | - $values['displayname'] = $newValue; |
|
384 | - break; |
|
385 | - case '{' . Plugin::NS_CARDDAV . '}addressbook-description' : |
|
386 | - $values['description'] = $newValue; |
|
387 | - break; |
|
388 | - default : |
|
389 | - throw new BadRequest('Unknown property: ' . $property); |
|
390 | - } |
|
391 | - |
|
392 | - } |
|
393 | - |
|
394 | - // Fallback to make sure the displayname is set. Some clients may refuse |
|
395 | - // to work with addressbooks not having a displayname. |
|
396 | - if(is_null($values['displayname'])) { |
|
397 | - $values['displayname'] = $url; |
|
398 | - } |
|
399 | - |
|
400 | - $query = $this->db->getQueryBuilder(); |
|
401 | - $query->insert('addressbooks') |
|
402 | - ->values([ |
|
403 | - 'uri' => $query->createParameter('uri'), |
|
404 | - 'displayname' => $query->createParameter('displayname'), |
|
405 | - 'description' => $query->createParameter('description'), |
|
406 | - 'principaluri' => $query->createParameter('principaluri'), |
|
407 | - 'synctoken' => $query->createParameter('synctoken'), |
|
408 | - ]) |
|
409 | - ->setParameters($values) |
|
410 | - ->execute(); |
|
411 | - |
|
412 | - return $query->getLastInsertId(); |
|
413 | - } |
|
414 | - |
|
415 | - /** |
|
416 | - * Deletes an entire addressbook and all its contents |
|
417 | - * |
|
418 | - * @param mixed $addressBookId |
|
419 | - * @return void |
|
420 | - */ |
|
421 | - function deleteAddressBook($addressBookId) { |
|
422 | - $query = $this->db->getQueryBuilder(); |
|
423 | - $query->delete('cards') |
|
424 | - ->where($query->expr()->eq('addressbookid', $query->createParameter('addressbookid'))) |
|
425 | - ->setParameter('addressbookid', $addressBookId) |
|
426 | - ->execute(); |
|
427 | - |
|
428 | - $query->delete('addressbookchanges') |
|
429 | - ->where($query->expr()->eq('addressbookid', $query->createParameter('addressbookid'))) |
|
430 | - ->setParameter('addressbookid', $addressBookId) |
|
431 | - ->execute(); |
|
432 | - |
|
433 | - $query->delete('addressbooks') |
|
434 | - ->where($query->expr()->eq('id', $query->createParameter('id'))) |
|
435 | - ->setParameter('id', $addressBookId) |
|
436 | - ->execute(); |
|
437 | - |
|
438 | - $this->sharingBackend->deleteAllShares($addressBookId); |
|
439 | - |
|
440 | - $query->delete($this->dbCardsPropertiesTable) |
|
441 | - ->where($query->expr()->eq('addressbookid', $query->createNamedParameter($addressBookId))) |
|
442 | - ->execute(); |
|
443 | - |
|
444 | - } |
|
445 | - |
|
446 | - /** |
|
447 | - * Returns all cards for a specific addressbook id. |
|
448 | - * |
|
449 | - * This method should return the following properties for each card: |
|
450 | - * * carddata - raw vcard data |
|
451 | - * * uri - Some unique url |
|
452 | - * * lastmodified - A unix timestamp |
|
453 | - * |
|
454 | - * It's recommended to also return the following properties: |
|
455 | - * * etag - A unique etag. This must change every time the card changes. |
|
456 | - * * size - The size of the card in bytes. |
|
457 | - * |
|
458 | - * If these last two properties are provided, less time will be spent |
|
459 | - * calculating them. If they are specified, you can also ommit carddata. |
|
460 | - * This may speed up certain requests, especially with large cards. |
|
461 | - * |
|
462 | - * @param mixed $addressBookId |
|
463 | - * @return array |
|
464 | - */ |
|
465 | - function getCards($addressBookId) { |
|
466 | - $query = $this->db->getQueryBuilder(); |
|
467 | - $query->select(['id', 'uri', 'lastmodified', 'etag', 'size', 'carddata']) |
|
468 | - ->from('cards') |
|
469 | - ->where($query->expr()->eq('addressbookid', $query->createNamedParameter($addressBookId))); |
|
470 | - |
|
471 | - $cards = []; |
|
472 | - |
|
473 | - $result = $query->execute(); |
|
474 | - while($row = $result->fetch()) { |
|
475 | - $row['etag'] = '"' . $row['etag'] . '"'; |
|
476 | - $row['carddata'] = $this->readBlob($row['carddata']); |
|
477 | - $cards[] = $row; |
|
478 | - } |
|
479 | - $result->closeCursor(); |
|
480 | - |
|
481 | - return $cards; |
|
482 | - } |
|
483 | - |
|
484 | - /** |
|
485 | - * Returns a specific card. |
|
486 | - * |
|
487 | - * The same set of properties must be returned as with getCards. The only |
|
488 | - * exception is that 'carddata' is absolutely required. |
|
489 | - * |
|
490 | - * If the card does not exist, you must return false. |
|
491 | - * |
|
492 | - * @param mixed $addressBookId |
|
493 | - * @param string $cardUri |
|
494 | - * @return array |
|
495 | - */ |
|
496 | - function getCard($addressBookId, $cardUri) { |
|
497 | - $query = $this->db->getQueryBuilder(); |
|
498 | - $query->select(['id', 'uri', 'lastmodified', 'etag', 'size', 'carddata']) |
|
499 | - ->from('cards') |
|
500 | - ->where($query->expr()->eq('addressbookid', $query->createNamedParameter($addressBookId))) |
|
501 | - ->andWhere($query->expr()->eq('uri', $query->createNamedParameter($cardUri))) |
|
502 | - ->setMaxResults(1); |
|
503 | - |
|
504 | - $result = $query->execute(); |
|
505 | - $row = $result->fetch(); |
|
506 | - if (!$row) { |
|
507 | - return false; |
|
508 | - } |
|
509 | - $row['etag'] = '"' . $row['etag'] . '"'; |
|
510 | - $row['carddata'] = $this->readBlob($row['carddata']); |
|
511 | - |
|
512 | - return $row; |
|
513 | - } |
|
514 | - |
|
515 | - /** |
|
516 | - * Returns a list of cards. |
|
517 | - * |
|
518 | - * This method should work identical to getCard, but instead return all the |
|
519 | - * cards in the list as an array. |
|
520 | - * |
|
521 | - * If the backend supports this, it may allow for some speed-ups. |
|
522 | - * |
|
523 | - * @param mixed $addressBookId |
|
524 | - * @param string[] $uris |
|
525 | - * @return array |
|
526 | - */ |
|
527 | - function getMultipleCards($addressBookId, array $uris) { |
|
528 | - if (empty($uris)) { |
|
529 | - return []; |
|
530 | - } |
|
531 | - |
|
532 | - $chunks = array_chunk($uris, 100); |
|
533 | - $cards = []; |
|
534 | - |
|
535 | - $query = $this->db->getQueryBuilder(); |
|
536 | - $query->select(['id', 'uri', 'lastmodified', 'etag', 'size', 'carddata']) |
|
537 | - ->from('cards') |
|
538 | - ->where($query->expr()->eq('addressbookid', $query->createNamedParameter($addressBookId))) |
|
539 | - ->andWhere($query->expr()->in('uri', $query->createParameter('uri'))); |
|
540 | - |
|
541 | - foreach ($chunks as $uris) { |
|
542 | - $query->setParameter('uri', $uris, IQueryBuilder::PARAM_STR_ARRAY); |
|
543 | - $result = $query->execute(); |
|
544 | - |
|
545 | - while ($row = $result->fetch()) { |
|
546 | - $row['etag'] = '"' . $row['etag'] . '"'; |
|
547 | - $row['carddata'] = $this->readBlob($row['carddata']); |
|
548 | - $cards[] = $row; |
|
549 | - } |
|
550 | - $result->closeCursor(); |
|
551 | - } |
|
552 | - return $cards; |
|
553 | - } |
|
554 | - |
|
555 | - /** |
|
556 | - * Creates a new card. |
|
557 | - * |
|
558 | - * The addressbook id will be passed as the first argument. This is the |
|
559 | - * same id as it is returned from the getAddressBooksForUser method. |
|
560 | - * |
|
561 | - * The cardUri is a base uri, and doesn't include the full path. The |
|
562 | - * cardData argument is the vcard body, and is passed as a string. |
|
563 | - * |
|
564 | - * It is possible to return an ETag from this method. This ETag is for the |
|
565 | - * newly created resource, and must be enclosed with double quotes (that |
|
566 | - * is, the string itself must contain the double quotes). |
|
567 | - * |
|
568 | - * You should only return the ETag if you store the carddata as-is. If a |
|
569 | - * subsequent GET request on the same card does not have the same body, |
|
570 | - * byte-by-byte and you did return an ETag here, clients tend to get |
|
571 | - * confused. |
|
572 | - * |
|
573 | - * If you don't return an ETag, you can just return null. |
|
574 | - * |
|
575 | - * @param mixed $addressBookId |
|
576 | - * @param string $cardUri |
|
577 | - * @param string $cardData |
|
578 | - * @return string |
|
579 | - */ |
|
580 | - function createCard($addressBookId, $cardUri, $cardData) { |
|
581 | - $etag = md5($cardData); |
|
582 | - |
|
583 | - $query = $this->db->getQueryBuilder(); |
|
584 | - $query->insert('cards') |
|
585 | - ->values([ |
|
586 | - 'carddata' => $query->createNamedParameter($cardData, IQueryBuilder::PARAM_LOB), |
|
587 | - 'uri' => $query->createNamedParameter($cardUri), |
|
588 | - 'lastmodified' => $query->createNamedParameter(time()), |
|
589 | - 'addressbookid' => $query->createNamedParameter($addressBookId), |
|
590 | - 'size' => $query->createNamedParameter(strlen($cardData)), |
|
591 | - 'etag' => $query->createNamedParameter($etag), |
|
592 | - ]) |
|
593 | - ->execute(); |
|
594 | - |
|
595 | - $this->addChange($addressBookId, $cardUri, 1); |
|
596 | - $this->updateProperties($addressBookId, $cardUri, $cardData); |
|
597 | - |
|
598 | - if (!is_null($this->dispatcher)) { |
|
599 | - $this->dispatcher->dispatch('\OCA\DAV\CardDAV\CardDavBackend::createCard', |
|
600 | - new GenericEvent(null, [ |
|
601 | - 'addressBookId' => $addressBookId, |
|
602 | - 'cardUri' => $cardUri, |
|
603 | - 'cardData' => $cardData])); |
|
604 | - } |
|
605 | - |
|
606 | - return '"' . $etag . '"'; |
|
607 | - } |
|
608 | - |
|
609 | - /** |
|
610 | - * Updates a card. |
|
611 | - * |
|
612 | - * The addressbook id will be passed as the first argument. This is the |
|
613 | - * same id as it is returned from the getAddressBooksForUser method. |
|
614 | - * |
|
615 | - * The cardUri is a base uri, and doesn't include the full path. The |
|
616 | - * cardData argument is the vcard body, and is passed as a string. |
|
617 | - * |
|
618 | - * It is possible to return an ETag from this method. This ETag should |
|
619 | - * match that of the updated resource, and must be enclosed with double |
|
620 | - * quotes (that is: the string itself must contain the actual quotes). |
|
621 | - * |
|
622 | - * You should only return the ETag if you store the carddata as-is. If a |
|
623 | - * subsequent GET request on the same card does not have the same body, |
|
624 | - * byte-by-byte and you did return an ETag here, clients tend to get |
|
625 | - * confused. |
|
626 | - * |
|
627 | - * If you don't return an ETag, you can just return null. |
|
628 | - * |
|
629 | - * @param mixed $addressBookId |
|
630 | - * @param string $cardUri |
|
631 | - * @param string $cardData |
|
632 | - * @return string |
|
633 | - */ |
|
634 | - function updateCard($addressBookId, $cardUri, $cardData) { |
|
635 | - |
|
636 | - $etag = md5($cardData); |
|
637 | - $query = $this->db->getQueryBuilder(); |
|
638 | - $query->update('cards') |
|
639 | - ->set('carddata', $query->createNamedParameter($cardData, IQueryBuilder::PARAM_LOB)) |
|
640 | - ->set('lastmodified', $query->createNamedParameter(time())) |
|
641 | - ->set('size', $query->createNamedParameter(strlen($cardData))) |
|
642 | - ->set('etag', $query->createNamedParameter($etag)) |
|
643 | - ->where($query->expr()->eq('uri', $query->createNamedParameter($cardUri))) |
|
644 | - ->andWhere($query->expr()->eq('addressbookid', $query->createNamedParameter($addressBookId))) |
|
645 | - ->execute(); |
|
646 | - |
|
647 | - $this->addChange($addressBookId, $cardUri, 2); |
|
648 | - $this->updateProperties($addressBookId, $cardUri, $cardData); |
|
649 | - |
|
650 | - if (!is_null($this->dispatcher)) { |
|
651 | - $this->dispatcher->dispatch('\OCA\DAV\CardDAV\CardDavBackend::updateCard', |
|
652 | - new GenericEvent(null, [ |
|
653 | - 'addressBookId' => $addressBookId, |
|
654 | - 'cardUri' => $cardUri, |
|
655 | - 'cardData' => $cardData])); |
|
656 | - } |
|
657 | - |
|
658 | - return '"' . $etag . '"'; |
|
659 | - } |
|
660 | - |
|
661 | - /** |
|
662 | - * Deletes a card |
|
663 | - * |
|
664 | - * @param mixed $addressBookId |
|
665 | - * @param string $cardUri |
|
666 | - * @return bool |
|
667 | - */ |
|
668 | - function deleteCard($addressBookId, $cardUri) { |
|
669 | - try { |
|
670 | - $cardId = $this->getCardId($addressBookId, $cardUri); |
|
671 | - } catch (\InvalidArgumentException $e) { |
|
672 | - $cardId = null; |
|
673 | - } |
|
674 | - $query = $this->db->getQueryBuilder(); |
|
675 | - $ret = $query->delete('cards') |
|
676 | - ->where($query->expr()->eq('addressbookid', $query->createNamedParameter($addressBookId))) |
|
677 | - ->andWhere($query->expr()->eq('uri', $query->createNamedParameter($cardUri))) |
|
678 | - ->execute(); |
|
679 | - |
|
680 | - $this->addChange($addressBookId, $cardUri, 3); |
|
681 | - |
|
682 | - if (!is_null($this->dispatcher)) { |
|
683 | - $this->dispatcher->dispatch('\OCA\DAV\CardDAV\CardDavBackend::deleteCard', |
|
684 | - new GenericEvent(null, [ |
|
685 | - 'addressBookId' => $addressBookId, |
|
686 | - 'cardUri' => $cardUri])); |
|
687 | - } |
|
688 | - |
|
689 | - if ($ret === 1) { |
|
690 | - if ($cardId !== null) { |
|
691 | - $this->purgeProperties($addressBookId, $cardId); |
|
692 | - } |
|
693 | - return true; |
|
694 | - } |
|
695 | - |
|
696 | - return false; |
|
697 | - } |
|
698 | - |
|
699 | - /** |
|
700 | - * The getChanges method returns all the changes that have happened, since |
|
701 | - * the specified syncToken in the specified address book. |
|
702 | - * |
|
703 | - * This function should return an array, such as the following: |
|
704 | - * |
|
705 | - * [ |
|
706 | - * 'syncToken' => 'The current synctoken', |
|
707 | - * 'added' => [ |
|
708 | - * 'new.txt', |
|
709 | - * ], |
|
710 | - * 'modified' => [ |
|
711 | - * 'modified.txt', |
|
712 | - * ], |
|
713 | - * 'deleted' => [ |
|
714 | - * 'foo.php.bak', |
|
715 | - * 'old.txt' |
|
716 | - * ] |
|
717 | - * ]; |
|
718 | - * |
|
719 | - * The returned syncToken property should reflect the *current* syncToken |
|
720 | - * of the calendar, as reported in the {http://sabredav.org/ns}sync-token |
|
721 | - * property. This is needed here too, to ensure the operation is atomic. |
|
722 | - * |
|
723 | - * If the $syncToken argument is specified as null, this is an initial |
|
724 | - * sync, and all members should be reported. |
|
725 | - * |
|
726 | - * The modified property is an array of nodenames that have changed since |
|
727 | - * the last token. |
|
728 | - * |
|
729 | - * The deleted property is an array with nodenames, that have been deleted |
|
730 | - * from collection. |
|
731 | - * |
|
732 | - * The $syncLevel argument is basically the 'depth' of the report. If it's |
|
733 | - * 1, you only have to report changes that happened only directly in |
|
734 | - * immediate descendants. If it's 2, it should also include changes from |
|
735 | - * the nodes below the child collections. (grandchildren) |
|
736 | - * |
|
737 | - * The $limit argument allows a client to specify how many results should |
|
738 | - * be returned at most. If the limit is not specified, it should be treated |
|
739 | - * as infinite. |
|
740 | - * |
|
741 | - * If the limit (infinite or not) is higher than you're willing to return, |
|
742 | - * you should throw a Sabre\DAV\Exception\TooMuchMatches() exception. |
|
743 | - * |
|
744 | - * If the syncToken is expired (due to data cleanup) or unknown, you must |
|
745 | - * return null. |
|
746 | - * |
|
747 | - * The limit is 'suggestive'. You are free to ignore it. |
|
748 | - * |
|
749 | - * @param string $addressBookId |
|
750 | - * @param string $syncToken |
|
751 | - * @param int $syncLevel |
|
752 | - * @param int $limit |
|
753 | - * @return array |
|
754 | - */ |
|
755 | - function getChangesForAddressBook($addressBookId, $syncToken, $syncLevel, $limit = null) { |
|
756 | - // Current synctoken |
|
757 | - $stmt = $this->db->prepare('SELECT `synctoken` FROM `*PREFIX*addressbooks` WHERE `id` = ?'); |
|
758 | - $stmt->execute([ $addressBookId ]); |
|
759 | - $currentToken = $stmt->fetchColumn(0); |
|
760 | - |
|
761 | - if (is_null($currentToken)) return null; |
|
762 | - |
|
763 | - $result = [ |
|
764 | - 'syncToken' => $currentToken, |
|
765 | - 'added' => [], |
|
766 | - 'modified' => [], |
|
767 | - 'deleted' => [], |
|
768 | - ]; |
|
769 | - |
|
770 | - if ($syncToken) { |
|
771 | - |
|
772 | - $query = "SELECT `uri`, `operation` FROM `*PREFIX*addressbookchanges` WHERE `synctoken` >= ? AND `synctoken` < ? AND `addressbookid` = ? ORDER BY `synctoken`"; |
|
773 | - if ($limit>0) { |
|
774 | - $query .= " `LIMIT` " . (int)$limit; |
|
775 | - } |
|
776 | - |
|
777 | - // Fetching all changes |
|
778 | - $stmt = $this->db->prepare($query); |
|
779 | - $stmt->execute([$syncToken, $currentToken, $addressBookId]); |
|
780 | - |
|
781 | - $changes = []; |
|
782 | - |
|
783 | - // This loop ensures that any duplicates are overwritten, only the |
|
784 | - // last change on a node is relevant. |
|
785 | - while($row = $stmt->fetch(\PDO::FETCH_ASSOC)) { |
|
786 | - |
|
787 | - $changes[$row['uri']] = $row['operation']; |
|
788 | - |
|
789 | - } |
|
790 | - |
|
791 | - foreach($changes as $uri => $operation) { |
|
792 | - |
|
793 | - switch($operation) { |
|
794 | - case 1: |
|
795 | - $result['added'][] = $uri; |
|
796 | - break; |
|
797 | - case 2: |
|
798 | - $result['modified'][] = $uri; |
|
799 | - break; |
|
800 | - case 3: |
|
801 | - $result['deleted'][] = $uri; |
|
802 | - break; |
|
803 | - } |
|
804 | - |
|
805 | - } |
|
806 | - } else { |
|
807 | - // No synctoken supplied, this is the initial sync. |
|
808 | - $query = "SELECT `uri` FROM `*PREFIX*cards` WHERE `addressbookid` = ?"; |
|
809 | - $stmt = $this->db->prepare($query); |
|
810 | - $stmt->execute([$addressBookId]); |
|
811 | - |
|
812 | - $result['added'] = $stmt->fetchAll(\PDO::FETCH_COLUMN); |
|
813 | - } |
|
814 | - return $result; |
|
815 | - } |
|
816 | - |
|
817 | - /** |
|
818 | - * Adds a change record to the addressbookchanges table. |
|
819 | - * |
|
820 | - * @param mixed $addressBookId |
|
821 | - * @param string $objectUri |
|
822 | - * @param int $operation 1 = add, 2 = modify, 3 = delete |
|
823 | - * @return void |
|
824 | - */ |
|
825 | - protected function addChange($addressBookId, $objectUri, $operation) { |
|
826 | - $sql = 'INSERT INTO `*PREFIX*addressbookchanges`(`uri`, `synctoken`, `addressbookid`, `operation`) SELECT ?, `synctoken`, ?, ? FROM `*PREFIX*addressbooks` WHERE `id` = ?'; |
|
827 | - $stmt = $this->db->prepare($sql); |
|
828 | - $stmt->execute([ |
|
829 | - $objectUri, |
|
830 | - $addressBookId, |
|
831 | - $operation, |
|
832 | - $addressBookId |
|
833 | - ]); |
|
834 | - $stmt = $this->db->prepare('UPDATE `*PREFIX*addressbooks` SET `synctoken` = `synctoken` + 1 WHERE `id` = ?'); |
|
835 | - $stmt->execute([ |
|
836 | - $addressBookId |
|
837 | - ]); |
|
838 | - } |
|
839 | - |
|
840 | - private function readBlob($cardData) { |
|
841 | - if (is_resource($cardData)) { |
|
842 | - return stream_get_contents($cardData); |
|
843 | - } |
|
844 | - |
|
845 | - return $cardData; |
|
846 | - } |
|
847 | - |
|
848 | - /** |
|
849 | - * @param IShareable $shareable |
|
850 | - * @param string[] $add |
|
851 | - * @param string[] $remove |
|
852 | - */ |
|
853 | - public function updateShares(IShareable $shareable, $add, $remove) { |
|
854 | - $this->sharingBackend->updateShares($shareable, $add, $remove); |
|
855 | - } |
|
856 | - |
|
857 | - /** |
|
858 | - * search contact |
|
859 | - * |
|
860 | - * @param int $addressBookId |
|
861 | - * @param string $pattern which should match within the $searchProperties |
|
862 | - * @param array $searchProperties defines the properties within the query pattern should match |
|
863 | - * @return array an array of contacts which are arrays of key-value-pairs |
|
864 | - */ |
|
865 | - public function search($addressBookId, $pattern, $searchProperties) { |
|
866 | - $query = $this->db->getQueryBuilder(); |
|
867 | - $query2 = $this->db->getQueryBuilder(); |
|
868 | - |
|
869 | - $query2->selectDistinct('cp.cardid')->from($this->dbCardsPropertiesTable, 'cp'); |
|
870 | - $query2->andWhere($query2->expr()->eq('cp.addressbookid', $query->createNamedParameter($addressBookId))); |
|
871 | - foreach ($searchProperties as $property) { |
|
872 | - $query2->expr()->orX($query2->expr()->eq('cp.name', $query->createNamedParameter($property))); |
|
873 | - } |
|
874 | - $query2->andWhere($query2->expr()->ilike('cp.value', $query->createNamedParameter('%' . $this->db->escapeLikeParameter($pattern) . '%'))); |
|
875 | - |
|
876 | - $query->select('c.carddata', 'c.uri')->from($this->dbCardsTable, 'c') |
|
877 | - ->where($query->expr()->in('c.id', $query->createFunction($query2->getSQL()))); |
|
878 | - |
|
879 | - $result = $query->execute(); |
|
880 | - $cards = $result->fetchAll(); |
|
881 | - |
|
882 | - $result->closeCursor(); |
|
883 | - |
|
884 | - return array_map(function($array) { |
|
885 | - $array['carddata'] = $this->readBlob($array['carddata']); |
|
886 | - return $array; |
|
887 | - }, $cards); |
|
888 | - } |
|
889 | - |
|
890 | - /** |
|
891 | - * @param int $bookId |
|
892 | - * @param string $name |
|
893 | - * @return array |
|
894 | - */ |
|
895 | - public function collectCardProperties($bookId, $name) { |
|
896 | - $query = $this->db->getQueryBuilder(); |
|
897 | - $result = $query->selectDistinct('value') |
|
898 | - ->from($this->dbCardsPropertiesTable) |
|
899 | - ->where($query->expr()->eq('name', $query->createNamedParameter($name))) |
|
900 | - ->andWhere($query->expr()->eq('addressbookid', $query->createNamedParameter($bookId))) |
|
901 | - ->execute(); |
|
902 | - |
|
903 | - $all = $result->fetchAll(PDO::FETCH_COLUMN); |
|
904 | - $result->closeCursor(); |
|
905 | - |
|
906 | - return $all; |
|
907 | - } |
|
908 | - |
|
909 | - /** |
|
910 | - * get URI from a given contact |
|
911 | - * |
|
912 | - * @param int $id |
|
913 | - * @return string |
|
914 | - */ |
|
915 | - public function getCardUri($id) { |
|
916 | - $query = $this->db->getQueryBuilder(); |
|
917 | - $query->select('uri')->from($this->dbCardsTable) |
|
918 | - ->where($query->expr()->eq('id', $query->createParameter('id'))) |
|
919 | - ->setParameter('id', $id); |
|
920 | - |
|
921 | - $result = $query->execute(); |
|
922 | - $uri = $result->fetch(); |
|
923 | - $result->closeCursor(); |
|
924 | - |
|
925 | - if (!isset($uri['uri'])) { |
|
926 | - throw new \InvalidArgumentException('Card does not exists: ' . $id); |
|
927 | - } |
|
928 | - |
|
929 | - return $uri['uri']; |
|
930 | - } |
|
931 | - |
|
932 | - /** |
|
933 | - * return contact with the given URI |
|
934 | - * |
|
935 | - * @param int $addressBookId |
|
936 | - * @param string $uri |
|
937 | - * @returns array |
|
938 | - */ |
|
939 | - public function getContact($addressBookId, $uri) { |
|
940 | - $result = []; |
|
941 | - $query = $this->db->getQueryBuilder(); |
|
942 | - $query->select('*')->from($this->dbCardsTable) |
|
943 | - ->where($query->expr()->eq('uri', $query->createNamedParameter($uri))) |
|
944 | - ->andWhere($query->expr()->eq('addressbookid', $query->createNamedParameter($addressBookId))); |
|
945 | - $queryResult = $query->execute(); |
|
946 | - $contact = $queryResult->fetch(); |
|
947 | - $queryResult->closeCursor(); |
|
948 | - |
|
949 | - if (is_array($contact)) { |
|
950 | - $result = $contact; |
|
951 | - } |
|
952 | - |
|
953 | - return $result; |
|
954 | - } |
|
955 | - |
|
956 | - /** |
|
957 | - * Returns the list of people whom this address book is shared with. |
|
958 | - * |
|
959 | - * Every element in this array should have the following properties: |
|
960 | - * * href - Often a mailto: address |
|
961 | - * * commonName - Optional, for example a first + last name |
|
962 | - * * status - See the Sabre\CalDAV\SharingPlugin::STATUS_ constants. |
|
963 | - * * readOnly - boolean |
|
964 | - * * summary - Optional, a description for the share |
|
965 | - * |
|
966 | - * @return array |
|
967 | - */ |
|
968 | - public function getShares($addressBookId) { |
|
969 | - return $this->sharingBackend->getShares($addressBookId); |
|
970 | - } |
|
971 | - |
|
972 | - /** |
|
973 | - * update properties table |
|
974 | - * |
|
975 | - * @param int $addressBookId |
|
976 | - * @param string $cardUri |
|
977 | - * @param string $vCardSerialized |
|
978 | - */ |
|
979 | - protected function updateProperties($addressBookId, $cardUri, $vCardSerialized) { |
|
980 | - $cardId = $this->getCardId($addressBookId, $cardUri); |
|
981 | - $vCard = $this->readCard($vCardSerialized); |
|
982 | - |
|
983 | - $this->purgeProperties($addressBookId, $cardId); |
|
984 | - |
|
985 | - $query = $this->db->getQueryBuilder(); |
|
986 | - $query->insert($this->dbCardsPropertiesTable) |
|
987 | - ->values( |
|
988 | - [ |
|
989 | - 'addressbookid' => $query->createNamedParameter($addressBookId), |
|
990 | - 'cardid' => $query->createNamedParameter($cardId), |
|
991 | - 'name' => $query->createParameter('name'), |
|
992 | - 'value' => $query->createParameter('value'), |
|
993 | - 'preferred' => $query->createParameter('preferred') |
|
994 | - ] |
|
995 | - ); |
|
996 | - |
|
997 | - foreach ($vCard->children() as $property) { |
|
998 | - if(!in_array($property->name, self::$indexProperties)) { |
|
999 | - continue; |
|
1000 | - } |
|
1001 | - $preferred = 0; |
|
1002 | - foreach($property->parameters as $parameter) { |
|
1003 | - if ($parameter->name == 'TYPE' && strtoupper($parameter->getValue()) == 'PREF') { |
|
1004 | - $preferred = 1; |
|
1005 | - break; |
|
1006 | - } |
|
1007 | - } |
|
1008 | - $query->setParameter('name', $property->name); |
|
1009 | - $query->setParameter('value', substr($property->getValue(), 0, 254)); |
|
1010 | - $query->setParameter('preferred', $preferred); |
|
1011 | - $query->execute(); |
|
1012 | - } |
|
1013 | - } |
|
1014 | - |
|
1015 | - /** |
|
1016 | - * read vCard data into a vCard object |
|
1017 | - * |
|
1018 | - * @param string $cardData |
|
1019 | - * @return VCard |
|
1020 | - */ |
|
1021 | - protected function readCard($cardData) { |
|
1022 | - return Reader::read($cardData); |
|
1023 | - } |
|
1024 | - |
|
1025 | - /** |
|
1026 | - * delete all properties from a given card |
|
1027 | - * |
|
1028 | - * @param int $addressBookId |
|
1029 | - * @param int $cardId |
|
1030 | - */ |
|
1031 | - protected function purgeProperties($addressBookId, $cardId) { |
|
1032 | - $query = $this->db->getQueryBuilder(); |
|
1033 | - $query->delete($this->dbCardsPropertiesTable) |
|
1034 | - ->where($query->expr()->eq('cardid', $query->createNamedParameter($cardId))) |
|
1035 | - ->andWhere($query->expr()->eq('addressbookid', $query->createNamedParameter($addressBookId))); |
|
1036 | - $query->execute(); |
|
1037 | - } |
|
1038 | - |
|
1039 | - /** |
|
1040 | - * get ID from a given contact |
|
1041 | - * |
|
1042 | - * @param int $addressBookId |
|
1043 | - * @param string $uri |
|
1044 | - * @return int |
|
1045 | - */ |
|
1046 | - protected function getCardId($addressBookId, $uri) { |
|
1047 | - $query = $this->db->getQueryBuilder(); |
|
1048 | - $query->select('id')->from($this->dbCardsTable) |
|
1049 | - ->where($query->expr()->eq('uri', $query->createNamedParameter($uri))) |
|
1050 | - ->andWhere($query->expr()->eq('addressbookid', $query->createNamedParameter($addressBookId))); |
|
1051 | - |
|
1052 | - $result = $query->execute(); |
|
1053 | - $cardIds = $result->fetch(); |
|
1054 | - $result->closeCursor(); |
|
1055 | - |
|
1056 | - if (!isset($cardIds['id'])) { |
|
1057 | - throw new \InvalidArgumentException('Card does not exists: ' . $uri); |
|
1058 | - } |
|
1059 | - |
|
1060 | - return (int)$cardIds['id']; |
|
1061 | - } |
|
1062 | - |
|
1063 | - /** |
|
1064 | - * For shared address books the sharee is set in the ACL of the address book |
|
1065 | - * @param $addressBookId |
|
1066 | - * @param $acl |
|
1067 | - * @return array |
|
1068 | - */ |
|
1069 | - public function applyShareAcl($addressBookId, $acl) { |
|
1070 | - return $this->sharingBackend->applyShareAcl($addressBookId, $acl); |
|
1071 | - } |
|
1072 | - |
|
1073 | - private function convertPrincipal($principalUri, $toV2) { |
|
1074 | - if ($this->principalBackend->getPrincipalPrefix() === 'principals') { |
|
1075 | - list(, $name) = URLUtil::splitPath($principalUri); |
|
1076 | - if ($toV2 === true) { |
|
1077 | - return "principals/users/$name"; |
|
1078 | - } |
|
1079 | - return "principals/$name"; |
|
1080 | - } |
|
1081 | - return $principalUri; |
|
1082 | - } |
|
51 | + const PERSONAL_ADDRESSBOOK_URI = 'contacts'; |
|
52 | + const PERSONAL_ADDRESSBOOK_NAME = 'Contacts'; |
|
53 | + |
|
54 | + /** @var Principal */ |
|
55 | + private $principalBackend; |
|
56 | + |
|
57 | + /** @var string */ |
|
58 | + private $dbCardsTable = 'cards'; |
|
59 | + |
|
60 | + /** @var string */ |
|
61 | + private $dbCardsPropertiesTable = 'cards_properties'; |
|
62 | + |
|
63 | + /** @var IDBConnection */ |
|
64 | + private $db; |
|
65 | + |
|
66 | + /** @var Backend */ |
|
67 | + private $sharingBackend; |
|
68 | + |
|
69 | + /** @var array properties to index */ |
|
70 | + public static $indexProperties = array( |
|
71 | + 'BDAY', 'UID', 'N', 'FN', 'TITLE', 'ROLE', 'NOTE', 'NICKNAME', |
|
72 | + 'ORG', 'CATEGORIES', 'EMAIL', 'TEL', 'IMPP', 'ADR', 'URL', 'GEO', 'CLOUD'); |
|
73 | + |
|
74 | + /** |
|
75 | + * @var string[] Map of uid => display name |
|
76 | + */ |
|
77 | + protected $userDisplayNames; |
|
78 | + |
|
79 | + /** @var IUserManager */ |
|
80 | + private $userManager; |
|
81 | + |
|
82 | + /** @var EventDispatcherInterface */ |
|
83 | + private $dispatcher; |
|
84 | + |
|
85 | + /** |
|
86 | + * CardDavBackend constructor. |
|
87 | + * |
|
88 | + * @param IDBConnection $db |
|
89 | + * @param Principal $principalBackend |
|
90 | + * @param IUserManager $userManager |
|
91 | + * @param EventDispatcherInterface $dispatcher |
|
92 | + */ |
|
93 | + public function __construct(IDBConnection $db, |
|
94 | + Principal $principalBackend, |
|
95 | + IUserManager $userManager, |
|
96 | + EventDispatcherInterface $dispatcher = null) { |
|
97 | + $this->db = $db; |
|
98 | + $this->principalBackend = $principalBackend; |
|
99 | + $this->userManager = $userManager; |
|
100 | + $this->dispatcher = $dispatcher; |
|
101 | + $this->sharingBackend = new Backend($this->db, $principalBackend, 'addressbook'); |
|
102 | + } |
|
103 | + |
|
104 | + /** |
|
105 | + * Return the number of address books for a principal |
|
106 | + * |
|
107 | + * @param $principalUri |
|
108 | + * @return int |
|
109 | + */ |
|
110 | + public function getAddressBooksForUserCount($principalUri) { |
|
111 | + $principalUri = $this->convertPrincipal($principalUri, true); |
|
112 | + $query = $this->db->getQueryBuilder(); |
|
113 | + $query->select($query->createFunction('COUNT(*)')) |
|
114 | + ->from('addressbooks') |
|
115 | + ->where($query->expr()->eq('principaluri', $query->createNamedParameter($principalUri))); |
|
116 | + |
|
117 | + return (int)$query->execute()->fetchColumn(); |
|
118 | + } |
|
119 | + |
|
120 | + /** |
|
121 | + * Returns the list of address books for a specific user. |
|
122 | + * |
|
123 | + * Every addressbook should have the following properties: |
|
124 | + * id - an arbitrary unique id |
|
125 | + * uri - the 'basename' part of the url |
|
126 | + * principaluri - Same as the passed parameter |
|
127 | + * |
|
128 | + * Any additional clark-notation property may be passed besides this. Some |
|
129 | + * common ones are : |
|
130 | + * {DAV:}displayname |
|
131 | + * {urn:ietf:params:xml:ns:carddav}addressbook-description |
|
132 | + * {http://calendarserver.org/ns/}getctag |
|
133 | + * |
|
134 | + * @param string $principalUri |
|
135 | + * @return array |
|
136 | + */ |
|
137 | + function getAddressBooksForUser($principalUri) { |
|
138 | + $principalUriOriginal = $principalUri; |
|
139 | + $principalUri = $this->convertPrincipal($principalUri, true); |
|
140 | + $query = $this->db->getQueryBuilder(); |
|
141 | + $query->select(['id', 'uri', 'displayname', 'principaluri', 'description', 'synctoken']) |
|
142 | + ->from('addressbooks') |
|
143 | + ->where($query->expr()->eq('principaluri', $query->createNamedParameter($principalUri))); |
|
144 | + |
|
145 | + $addressBooks = []; |
|
146 | + |
|
147 | + $result = $query->execute(); |
|
148 | + while($row = $result->fetch()) { |
|
149 | + $addressBooks[$row['id']] = [ |
|
150 | + 'id' => $row['id'], |
|
151 | + 'uri' => $row['uri'], |
|
152 | + 'principaluri' => $this->convertPrincipal($row['principaluri'], false), |
|
153 | + '{DAV:}displayname' => $row['displayname'], |
|
154 | + '{' . Plugin::NS_CARDDAV . '}addressbook-description' => $row['description'], |
|
155 | + '{http://calendarserver.org/ns/}getctag' => $row['synctoken'], |
|
156 | + '{http://sabredav.org/ns}sync-token' => $row['synctoken']?$row['synctoken']:'0', |
|
157 | + ]; |
|
158 | + } |
|
159 | + $result->closeCursor(); |
|
160 | + |
|
161 | + // query for shared calendars |
|
162 | + $principals = $this->principalBackend->getGroupMembership($principalUriOriginal, true); |
|
163 | + $principals[]= $principalUri; |
|
164 | + |
|
165 | + $query = $this->db->getQueryBuilder(); |
|
166 | + $result = $query->select(['a.id', 'a.uri', 'a.displayname', 'a.principaluri', 'a.description', 'a.synctoken', 's.access']) |
|
167 | + ->from('dav_shares', 's') |
|
168 | + ->join('s', 'addressbooks', 'a', $query->expr()->eq('s.resourceid', 'a.id')) |
|
169 | + ->where($query->expr()->in('s.principaluri', $query->createParameter('principaluri'))) |
|
170 | + ->andWhere($query->expr()->eq('s.type', $query->createParameter('type'))) |
|
171 | + ->setParameter('type', 'addressbook') |
|
172 | + ->setParameter('principaluri', $principals, IQueryBuilder::PARAM_STR_ARRAY) |
|
173 | + ->execute(); |
|
174 | + |
|
175 | + $readOnlyPropertyName = '{' . \OCA\DAV\DAV\Sharing\Plugin::NS_OWNCLOUD . '}read-only'; |
|
176 | + while($row = $result->fetch()) { |
|
177 | + $readOnly = (int) $row['access'] === Backend::ACCESS_READ; |
|
178 | + if (isset($addressBooks[$row['id']])) { |
|
179 | + if ($readOnly) { |
|
180 | + // New share can not have more permissions then the old one. |
|
181 | + continue; |
|
182 | + } |
|
183 | + if (isset($addressBooks[$row['id']][$readOnlyPropertyName]) && |
|
184 | + $addressBooks[$row['id']][$readOnlyPropertyName] === 0) { |
|
185 | + // Old share is already read-write, no more permissions can be gained |
|
186 | + continue; |
|
187 | + } |
|
188 | + } |
|
189 | + |
|
190 | + list(, $name) = URLUtil::splitPath($row['principaluri']); |
|
191 | + $uri = $row['uri'] . '_shared_by_' . $name; |
|
192 | + $displayName = $row['displayname'] . ' (' . $this->getUserDisplayName($name) . ')'; |
|
193 | + |
|
194 | + $addressBooks[$row['id']] = [ |
|
195 | + 'id' => $row['id'], |
|
196 | + 'uri' => $uri, |
|
197 | + 'principaluri' => $principalUriOriginal, |
|
198 | + '{DAV:}displayname' => $displayName, |
|
199 | + '{' . Plugin::NS_CARDDAV . '}addressbook-description' => $row['description'], |
|
200 | + '{http://calendarserver.org/ns/}getctag' => $row['synctoken'], |
|
201 | + '{http://sabredav.org/ns}sync-token' => $row['synctoken']?$row['synctoken']:'0', |
|
202 | + '{' . \OCA\DAV\DAV\Sharing\Plugin::NS_OWNCLOUD . '}owner-principal' => $row['principaluri'], |
|
203 | + $readOnlyPropertyName => $readOnly, |
|
204 | + ]; |
|
205 | + } |
|
206 | + $result->closeCursor(); |
|
207 | + |
|
208 | + return array_values($addressBooks); |
|
209 | + } |
|
210 | + |
|
211 | + public function getUsersOwnAddressBooks($principalUri) { |
|
212 | + $principalUriOriginal = $principalUri; |
|
213 | + $principalUri = $this->convertPrincipal($principalUri, true); |
|
214 | + $query = $this->db->getQueryBuilder(); |
|
215 | + $query->select(['id', 'uri', 'displayname', 'principaluri', 'description', 'synctoken']) |
|
216 | + ->from('addressbooks') |
|
217 | + ->where($query->expr()->eq('principaluri', $query->createNamedParameter($principalUri))); |
|
218 | + |
|
219 | + $addressBooks = []; |
|
220 | + |
|
221 | + $result = $query->execute(); |
|
222 | + while($row = $result->fetch()) { |
|
223 | + $addressBooks[$row['id']] = [ |
|
224 | + 'id' => $row['id'], |
|
225 | + 'uri' => $row['uri'], |
|
226 | + 'principaluri' => $this->convertPrincipal($row['principaluri'], false), |
|
227 | + '{DAV:}displayname' => $row['displayname'], |
|
228 | + '{' . Plugin::NS_CARDDAV . '}addressbook-description' => $row['description'], |
|
229 | + '{http://calendarserver.org/ns/}getctag' => $row['synctoken'], |
|
230 | + '{http://sabredav.org/ns}sync-token' => $row['synctoken']?$row['synctoken']:'0', |
|
231 | + ]; |
|
232 | + } |
|
233 | + $result->closeCursor(); |
|
234 | + |
|
235 | + return array_values($addressBooks); |
|
236 | + } |
|
237 | + |
|
238 | + private function getUserDisplayName($uid) { |
|
239 | + if (!isset($this->userDisplayNames[$uid])) { |
|
240 | + $user = $this->userManager->get($uid); |
|
241 | + |
|
242 | + if ($user instanceof IUser) { |
|
243 | + $this->userDisplayNames[$uid] = $user->getDisplayName(); |
|
244 | + } else { |
|
245 | + $this->userDisplayNames[$uid] = $uid; |
|
246 | + } |
|
247 | + } |
|
248 | + |
|
249 | + return $this->userDisplayNames[$uid]; |
|
250 | + } |
|
251 | + |
|
252 | + /** |
|
253 | + * @param int $addressBookId |
|
254 | + */ |
|
255 | + public function getAddressBookById($addressBookId) { |
|
256 | + $query = $this->db->getQueryBuilder(); |
|
257 | + $result = $query->select(['id', 'uri', 'displayname', 'principaluri', 'description', 'synctoken']) |
|
258 | + ->from('addressbooks') |
|
259 | + ->where($query->expr()->eq('id', $query->createNamedParameter($addressBookId))) |
|
260 | + ->execute(); |
|
261 | + |
|
262 | + $row = $result->fetch(); |
|
263 | + $result->closeCursor(); |
|
264 | + if ($row === false) { |
|
265 | + return null; |
|
266 | + } |
|
267 | + |
|
268 | + return [ |
|
269 | + 'id' => $row['id'], |
|
270 | + 'uri' => $row['uri'], |
|
271 | + 'principaluri' => $row['principaluri'], |
|
272 | + '{DAV:}displayname' => $row['displayname'], |
|
273 | + '{' . Plugin::NS_CARDDAV . '}addressbook-description' => $row['description'], |
|
274 | + '{http://calendarserver.org/ns/}getctag' => $row['synctoken'], |
|
275 | + '{http://sabredav.org/ns}sync-token' => $row['synctoken']?$row['synctoken']:'0', |
|
276 | + ]; |
|
277 | + } |
|
278 | + |
|
279 | + /** |
|
280 | + * @param $addressBookUri |
|
281 | + * @return array|null |
|
282 | + */ |
|
283 | + public function getAddressBooksByUri($principal, $addressBookUri) { |
|
284 | + $query = $this->db->getQueryBuilder(); |
|
285 | + $result = $query->select(['id', 'uri', 'displayname', 'principaluri', 'description', 'synctoken']) |
|
286 | + ->from('addressbooks') |
|
287 | + ->where($query->expr()->eq('uri', $query->createNamedParameter($addressBookUri))) |
|
288 | + ->andWhere($query->expr()->eq('principaluri', $query->createNamedParameter($principal))) |
|
289 | + ->setMaxResults(1) |
|
290 | + ->execute(); |
|
291 | + |
|
292 | + $row = $result->fetch(); |
|
293 | + $result->closeCursor(); |
|
294 | + if ($row === false) { |
|
295 | + return null; |
|
296 | + } |
|
297 | + |
|
298 | + return [ |
|
299 | + 'id' => $row['id'], |
|
300 | + 'uri' => $row['uri'], |
|
301 | + 'principaluri' => $row['principaluri'], |
|
302 | + '{DAV:}displayname' => $row['displayname'], |
|
303 | + '{' . Plugin::NS_CARDDAV . '}addressbook-description' => $row['description'], |
|
304 | + '{http://calendarserver.org/ns/}getctag' => $row['synctoken'], |
|
305 | + '{http://sabredav.org/ns}sync-token' => $row['synctoken']?$row['synctoken']:'0', |
|
306 | + ]; |
|
307 | + } |
|
308 | + |
|
309 | + /** |
|
310 | + * Updates properties for an address book. |
|
311 | + * |
|
312 | + * The list of mutations is stored in a Sabre\DAV\PropPatch object. |
|
313 | + * To do the actual updates, you must tell this object which properties |
|
314 | + * you're going to process with the handle() method. |
|
315 | + * |
|
316 | + * Calling the handle method is like telling the PropPatch object "I |
|
317 | + * promise I can handle updating this property". |
|
318 | + * |
|
319 | + * Read the PropPatch documentation for more info and examples. |
|
320 | + * |
|
321 | + * @param string $addressBookId |
|
322 | + * @param \Sabre\DAV\PropPatch $propPatch |
|
323 | + * @return void |
|
324 | + */ |
|
325 | + function updateAddressBook($addressBookId, \Sabre\DAV\PropPatch $propPatch) { |
|
326 | + $supportedProperties = [ |
|
327 | + '{DAV:}displayname', |
|
328 | + '{' . Plugin::NS_CARDDAV . '}addressbook-description', |
|
329 | + ]; |
|
330 | + |
|
331 | + $propPatch->handle($supportedProperties, function($mutations) use ($addressBookId) { |
|
332 | + |
|
333 | + $updates = []; |
|
334 | + foreach($mutations as $property=>$newValue) { |
|
335 | + |
|
336 | + switch($property) { |
|
337 | + case '{DAV:}displayname' : |
|
338 | + $updates['displayname'] = $newValue; |
|
339 | + break; |
|
340 | + case '{' . Plugin::NS_CARDDAV . '}addressbook-description' : |
|
341 | + $updates['description'] = $newValue; |
|
342 | + break; |
|
343 | + } |
|
344 | + } |
|
345 | + $query = $this->db->getQueryBuilder(); |
|
346 | + $query->update('addressbooks'); |
|
347 | + |
|
348 | + foreach($updates as $key=>$value) { |
|
349 | + $query->set($key, $query->createNamedParameter($value)); |
|
350 | + } |
|
351 | + $query->where($query->expr()->eq('id', $query->createNamedParameter($addressBookId))) |
|
352 | + ->execute(); |
|
353 | + |
|
354 | + $this->addChange($addressBookId, "", 2); |
|
355 | + |
|
356 | + return true; |
|
357 | + |
|
358 | + }); |
|
359 | + } |
|
360 | + |
|
361 | + /** |
|
362 | + * Creates a new address book |
|
363 | + * |
|
364 | + * @param string $principalUri |
|
365 | + * @param string $url Just the 'basename' of the url. |
|
366 | + * @param array $properties |
|
367 | + * @return int |
|
368 | + * @throws BadRequest |
|
369 | + */ |
|
370 | + function createAddressBook($principalUri, $url, array $properties) { |
|
371 | + $values = [ |
|
372 | + 'displayname' => null, |
|
373 | + 'description' => null, |
|
374 | + 'principaluri' => $principalUri, |
|
375 | + 'uri' => $url, |
|
376 | + 'synctoken' => 1 |
|
377 | + ]; |
|
378 | + |
|
379 | + foreach($properties as $property=>$newValue) { |
|
380 | + |
|
381 | + switch($property) { |
|
382 | + case '{DAV:}displayname' : |
|
383 | + $values['displayname'] = $newValue; |
|
384 | + break; |
|
385 | + case '{' . Plugin::NS_CARDDAV . '}addressbook-description' : |
|
386 | + $values['description'] = $newValue; |
|
387 | + break; |
|
388 | + default : |
|
389 | + throw new BadRequest('Unknown property: ' . $property); |
|
390 | + } |
|
391 | + |
|
392 | + } |
|
393 | + |
|
394 | + // Fallback to make sure the displayname is set. Some clients may refuse |
|
395 | + // to work with addressbooks not having a displayname. |
|
396 | + if(is_null($values['displayname'])) { |
|
397 | + $values['displayname'] = $url; |
|
398 | + } |
|
399 | + |
|
400 | + $query = $this->db->getQueryBuilder(); |
|
401 | + $query->insert('addressbooks') |
|
402 | + ->values([ |
|
403 | + 'uri' => $query->createParameter('uri'), |
|
404 | + 'displayname' => $query->createParameter('displayname'), |
|
405 | + 'description' => $query->createParameter('description'), |
|
406 | + 'principaluri' => $query->createParameter('principaluri'), |
|
407 | + 'synctoken' => $query->createParameter('synctoken'), |
|
408 | + ]) |
|
409 | + ->setParameters($values) |
|
410 | + ->execute(); |
|
411 | + |
|
412 | + return $query->getLastInsertId(); |
|
413 | + } |
|
414 | + |
|
415 | + /** |
|
416 | + * Deletes an entire addressbook and all its contents |
|
417 | + * |
|
418 | + * @param mixed $addressBookId |
|
419 | + * @return void |
|
420 | + */ |
|
421 | + function deleteAddressBook($addressBookId) { |
|
422 | + $query = $this->db->getQueryBuilder(); |
|
423 | + $query->delete('cards') |
|
424 | + ->where($query->expr()->eq('addressbookid', $query->createParameter('addressbookid'))) |
|
425 | + ->setParameter('addressbookid', $addressBookId) |
|
426 | + ->execute(); |
|
427 | + |
|
428 | + $query->delete('addressbookchanges') |
|
429 | + ->where($query->expr()->eq('addressbookid', $query->createParameter('addressbookid'))) |
|
430 | + ->setParameter('addressbookid', $addressBookId) |
|
431 | + ->execute(); |
|
432 | + |
|
433 | + $query->delete('addressbooks') |
|
434 | + ->where($query->expr()->eq('id', $query->createParameter('id'))) |
|
435 | + ->setParameter('id', $addressBookId) |
|
436 | + ->execute(); |
|
437 | + |
|
438 | + $this->sharingBackend->deleteAllShares($addressBookId); |
|
439 | + |
|
440 | + $query->delete($this->dbCardsPropertiesTable) |
|
441 | + ->where($query->expr()->eq('addressbookid', $query->createNamedParameter($addressBookId))) |
|
442 | + ->execute(); |
|
443 | + |
|
444 | + } |
|
445 | + |
|
446 | + /** |
|
447 | + * Returns all cards for a specific addressbook id. |
|
448 | + * |
|
449 | + * This method should return the following properties for each card: |
|
450 | + * * carddata - raw vcard data |
|
451 | + * * uri - Some unique url |
|
452 | + * * lastmodified - A unix timestamp |
|
453 | + * |
|
454 | + * It's recommended to also return the following properties: |
|
455 | + * * etag - A unique etag. This must change every time the card changes. |
|
456 | + * * size - The size of the card in bytes. |
|
457 | + * |
|
458 | + * If these last two properties are provided, less time will be spent |
|
459 | + * calculating them. If they are specified, you can also ommit carddata. |
|
460 | + * This may speed up certain requests, especially with large cards. |
|
461 | + * |
|
462 | + * @param mixed $addressBookId |
|
463 | + * @return array |
|
464 | + */ |
|
465 | + function getCards($addressBookId) { |
|
466 | + $query = $this->db->getQueryBuilder(); |
|
467 | + $query->select(['id', 'uri', 'lastmodified', 'etag', 'size', 'carddata']) |
|
468 | + ->from('cards') |
|
469 | + ->where($query->expr()->eq('addressbookid', $query->createNamedParameter($addressBookId))); |
|
470 | + |
|
471 | + $cards = []; |
|
472 | + |
|
473 | + $result = $query->execute(); |
|
474 | + while($row = $result->fetch()) { |
|
475 | + $row['etag'] = '"' . $row['etag'] . '"'; |
|
476 | + $row['carddata'] = $this->readBlob($row['carddata']); |
|
477 | + $cards[] = $row; |
|
478 | + } |
|
479 | + $result->closeCursor(); |
|
480 | + |
|
481 | + return $cards; |
|
482 | + } |
|
483 | + |
|
484 | + /** |
|
485 | + * Returns a specific card. |
|
486 | + * |
|
487 | + * The same set of properties must be returned as with getCards. The only |
|
488 | + * exception is that 'carddata' is absolutely required. |
|
489 | + * |
|
490 | + * If the card does not exist, you must return false. |
|
491 | + * |
|
492 | + * @param mixed $addressBookId |
|
493 | + * @param string $cardUri |
|
494 | + * @return array |
|
495 | + */ |
|
496 | + function getCard($addressBookId, $cardUri) { |
|
497 | + $query = $this->db->getQueryBuilder(); |
|
498 | + $query->select(['id', 'uri', 'lastmodified', 'etag', 'size', 'carddata']) |
|
499 | + ->from('cards') |
|
500 | + ->where($query->expr()->eq('addressbookid', $query->createNamedParameter($addressBookId))) |
|
501 | + ->andWhere($query->expr()->eq('uri', $query->createNamedParameter($cardUri))) |
|
502 | + ->setMaxResults(1); |
|
503 | + |
|
504 | + $result = $query->execute(); |
|
505 | + $row = $result->fetch(); |
|
506 | + if (!$row) { |
|
507 | + return false; |
|
508 | + } |
|
509 | + $row['etag'] = '"' . $row['etag'] . '"'; |
|
510 | + $row['carddata'] = $this->readBlob($row['carddata']); |
|
511 | + |
|
512 | + return $row; |
|
513 | + } |
|
514 | + |
|
515 | + /** |
|
516 | + * Returns a list of cards. |
|
517 | + * |
|
518 | + * This method should work identical to getCard, but instead return all the |
|
519 | + * cards in the list as an array. |
|
520 | + * |
|
521 | + * If the backend supports this, it may allow for some speed-ups. |
|
522 | + * |
|
523 | + * @param mixed $addressBookId |
|
524 | + * @param string[] $uris |
|
525 | + * @return array |
|
526 | + */ |
|
527 | + function getMultipleCards($addressBookId, array $uris) { |
|
528 | + if (empty($uris)) { |
|
529 | + return []; |
|
530 | + } |
|
531 | + |
|
532 | + $chunks = array_chunk($uris, 100); |
|
533 | + $cards = []; |
|
534 | + |
|
535 | + $query = $this->db->getQueryBuilder(); |
|
536 | + $query->select(['id', 'uri', 'lastmodified', 'etag', 'size', 'carddata']) |
|
537 | + ->from('cards') |
|
538 | + ->where($query->expr()->eq('addressbookid', $query->createNamedParameter($addressBookId))) |
|
539 | + ->andWhere($query->expr()->in('uri', $query->createParameter('uri'))); |
|
540 | + |
|
541 | + foreach ($chunks as $uris) { |
|
542 | + $query->setParameter('uri', $uris, IQueryBuilder::PARAM_STR_ARRAY); |
|
543 | + $result = $query->execute(); |
|
544 | + |
|
545 | + while ($row = $result->fetch()) { |
|
546 | + $row['etag'] = '"' . $row['etag'] . '"'; |
|
547 | + $row['carddata'] = $this->readBlob($row['carddata']); |
|
548 | + $cards[] = $row; |
|
549 | + } |
|
550 | + $result->closeCursor(); |
|
551 | + } |
|
552 | + return $cards; |
|
553 | + } |
|
554 | + |
|
555 | + /** |
|
556 | + * Creates a new card. |
|
557 | + * |
|
558 | + * The addressbook id will be passed as the first argument. This is the |
|
559 | + * same id as it is returned from the getAddressBooksForUser method. |
|
560 | + * |
|
561 | + * The cardUri is a base uri, and doesn't include the full path. The |
|
562 | + * cardData argument is the vcard body, and is passed as a string. |
|
563 | + * |
|
564 | + * It is possible to return an ETag from this method. This ETag is for the |
|
565 | + * newly created resource, and must be enclosed with double quotes (that |
|
566 | + * is, the string itself must contain the double quotes). |
|
567 | + * |
|
568 | + * You should only return the ETag if you store the carddata as-is. If a |
|
569 | + * subsequent GET request on the same card does not have the same body, |
|
570 | + * byte-by-byte and you did return an ETag here, clients tend to get |
|
571 | + * confused. |
|
572 | + * |
|
573 | + * If you don't return an ETag, you can just return null. |
|
574 | + * |
|
575 | + * @param mixed $addressBookId |
|
576 | + * @param string $cardUri |
|
577 | + * @param string $cardData |
|
578 | + * @return string |
|
579 | + */ |
|
580 | + function createCard($addressBookId, $cardUri, $cardData) { |
|
581 | + $etag = md5($cardData); |
|
582 | + |
|
583 | + $query = $this->db->getQueryBuilder(); |
|
584 | + $query->insert('cards') |
|
585 | + ->values([ |
|
586 | + 'carddata' => $query->createNamedParameter($cardData, IQueryBuilder::PARAM_LOB), |
|
587 | + 'uri' => $query->createNamedParameter($cardUri), |
|
588 | + 'lastmodified' => $query->createNamedParameter(time()), |
|
589 | + 'addressbookid' => $query->createNamedParameter($addressBookId), |
|
590 | + 'size' => $query->createNamedParameter(strlen($cardData)), |
|
591 | + 'etag' => $query->createNamedParameter($etag), |
|
592 | + ]) |
|
593 | + ->execute(); |
|
594 | + |
|
595 | + $this->addChange($addressBookId, $cardUri, 1); |
|
596 | + $this->updateProperties($addressBookId, $cardUri, $cardData); |
|
597 | + |
|
598 | + if (!is_null($this->dispatcher)) { |
|
599 | + $this->dispatcher->dispatch('\OCA\DAV\CardDAV\CardDavBackend::createCard', |
|
600 | + new GenericEvent(null, [ |
|
601 | + 'addressBookId' => $addressBookId, |
|
602 | + 'cardUri' => $cardUri, |
|
603 | + 'cardData' => $cardData])); |
|
604 | + } |
|
605 | + |
|
606 | + return '"' . $etag . '"'; |
|
607 | + } |
|
608 | + |
|
609 | + /** |
|
610 | + * Updates a card. |
|
611 | + * |
|
612 | + * The addressbook id will be passed as the first argument. This is the |
|
613 | + * same id as it is returned from the getAddressBooksForUser method. |
|
614 | + * |
|
615 | + * The cardUri is a base uri, and doesn't include the full path. The |
|
616 | + * cardData argument is the vcard body, and is passed as a string. |
|
617 | + * |
|
618 | + * It is possible to return an ETag from this method. This ETag should |
|
619 | + * match that of the updated resource, and must be enclosed with double |
|
620 | + * quotes (that is: the string itself must contain the actual quotes). |
|
621 | + * |
|
622 | + * You should only return the ETag if you store the carddata as-is. If a |
|
623 | + * subsequent GET request on the same card does not have the same body, |
|
624 | + * byte-by-byte and you did return an ETag here, clients tend to get |
|
625 | + * confused. |
|
626 | + * |
|
627 | + * If you don't return an ETag, you can just return null. |
|
628 | + * |
|
629 | + * @param mixed $addressBookId |
|
630 | + * @param string $cardUri |
|
631 | + * @param string $cardData |
|
632 | + * @return string |
|
633 | + */ |
|
634 | + function updateCard($addressBookId, $cardUri, $cardData) { |
|
635 | + |
|
636 | + $etag = md5($cardData); |
|
637 | + $query = $this->db->getQueryBuilder(); |
|
638 | + $query->update('cards') |
|
639 | + ->set('carddata', $query->createNamedParameter($cardData, IQueryBuilder::PARAM_LOB)) |
|
640 | + ->set('lastmodified', $query->createNamedParameter(time())) |
|
641 | + ->set('size', $query->createNamedParameter(strlen($cardData))) |
|
642 | + ->set('etag', $query->createNamedParameter($etag)) |
|
643 | + ->where($query->expr()->eq('uri', $query->createNamedParameter($cardUri))) |
|
644 | + ->andWhere($query->expr()->eq('addressbookid', $query->createNamedParameter($addressBookId))) |
|
645 | + ->execute(); |
|
646 | + |
|
647 | + $this->addChange($addressBookId, $cardUri, 2); |
|
648 | + $this->updateProperties($addressBookId, $cardUri, $cardData); |
|
649 | + |
|
650 | + if (!is_null($this->dispatcher)) { |
|
651 | + $this->dispatcher->dispatch('\OCA\DAV\CardDAV\CardDavBackend::updateCard', |
|
652 | + new GenericEvent(null, [ |
|
653 | + 'addressBookId' => $addressBookId, |
|
654 | + 'cardUri' => $cardUri, |
|
655 | + 'cardData' => $cardData])); |
|
656 | + } |
|
657 | + |
|
658 | + return '"' . $etag . '"'; |
|
659 | + } |
|
660 | + |
|
661 | + /** |
|
662 | + * Deletes a card |
|
663 | + * |
|
664 | + * @param mixed $addressBookId |
|
665 | + * @param string $cardUri |
|
666 | + * @return bool |
|
667 | + */ |
|
668 | + function deleteCard($addressBookId, $cardUri) { |
|
669 | + try { |
|
670 | + $cardId = $this->getCardId($addressBookId, $cardUri); |
|
671 | + } catch (\InvalidArgumentException $e) { |
|
672 | + $cardId = null; |
|
673 | + } |
|
674 | + $query = $this->db->getQueryBuilder(); |
|
675 | + $ret = $query->delete('cards') |
|
676 | + ->where($query->expr()->eq('addressbookid', $query->createNamedParameter($addressBookId))) |
|
677 | + ->andWhere($query->expr()->eq('uri', $query->createNamedParameter($cardUri))) |
|
678 | + ->execute(); |
|
679 | + |
|
680 | + $this->addChange($addressBookId, $cardUri, 3); |
|
681 | + |
|
682 | + if (!is_null($this->dispatcher)) { |
|
683 | + $this->dispatcher->dispatch('\OCA\DAV\CardDAV\CardDavBackend::deleteCard', |
|
684 | + new GenericEvent(null, [ |
|
685 | + 'addressBookId' => $addressBookId, |
|
686 | + 'cardUri' => $cardUri])); |
|
687 | + } |
|
688 | + |
|
689 | + if ($ret === 1) { |
|
690 | + if ($cardId !== null) { |
|
691 | + $this->purgeProperties($addressBookId, $cardId); |
|
692 | + } |
|
693 | + return true; |
|
694 | + } |
|
695 | + |
|
696 | + return false; |
|
697 | + } |
|
698 | + |
|
699 | + /** |
|
700 | + * The getChanges method returns all the changes that have happened, since |
|
701 | + * the specified syncToken in the specified address book. |
|
702 | + * |
|
703 | + * This function should return an array, such as the following: |
|
704 | + * |
|
705 | + * [ |
|
706 | + * 'syncToken' => 'The current synctoken', |
|
707 | + * 'added' => [ |
|
708 | + * 'new.txt', |
|
709 | + * ], |
|
710 | + * 'modified' => [ |
|
711 | + * 'modified.txt', |
|
712 | + * ], |
|
713 | + * 'deleted' => [ |
|
714 | + * 'foo.php.bak', |
|
715 | + * 'old.txt' |
|
716 | + * ] |
|
717 | + * ]; |
|
718 | + * |
|
719 | + * The returned syncToken property should reflect the *current* syncToken |
|
720 | + * of the calendar, as reported in the {http://sabredav.org/ns}sync-token |
|
721 | + * property. This is needed here too, to ensure the operation is atomic. |
|
722 | + * |
|
723 | + * If the $syncToken argument is specified as null, this is an initial |
|
724 | + * sync, and all members should be reported. |
|
725 | + * |
|
726 | + * The modified property is an array of nodenames that have changed since |
|
727 | + * the last token. |
|
728 | + * |
|
729 | + * The deleted property is an array with nodenames, that have been deleted |
|
730 | + * from collection. |
|
731 | + * |
|
732 | + * The $syncLevel argument is basically the 'depth' of the report. If it's |
|
733 | + * 1, you only have to report changes that happened only directly in |
|
734 | + * immediate descendants. If it's 2, it should also include changes from |
|
735 | + * the nodes below the child collections. (grandchildren) |
|
736 | + * |
|
737 | + * The $limit argument allows a client to specify how many results should |
|
738 | + * be returned at most. If the limit is not specified, it should be treated |
|
739 | + * as infinite. |
|
740 | + * |
|
741 | + * If the limit (infinite or not) is higher than you're willing to return, |
|
742 | + * you should throw a Sabre\DAV\Exception\TooMuchMatches() exception. |
|
743 | + * |
|
744 | + * If the syncToken is expired (due to data cleanup) or unknown, you must |
|
745 | + * return null. |
|
746 | + * |
|
747 | + * The limit is 'suggestive'. You are free to ignore it. |
|
748 | + * |
|
749 | + * @param string $addressBookId |
|
750 | + * @param string $syncToken |
|
751 | + * @param int $syncLevel |
|
752 | + * @param int $limit |
|
753 | + * @return array |
|
754 | + */ |
|
755 | + function getChangesForAddressBook($addressBookId, $syncToken, $syncLevel, $limit = null) { |
|
756 | + // Current synctoken |
|
757 | + $stmt = $this->db->prepare('SELECT `synctoken` FROM `*PREFIX*addressbooks` WHERE `id` = ?'); |
|
758 | + $stmt->execute([ $addressBookId ]); |
|
759 | + $currentToken = $stmt->fetchColumn(0); |
|
760 | + |
|
761 | + if (is_null($currentToken)) return null; |
|
762 | + |
|
763 | + $result = [ |
|
764 | + 'syncToken' => $currentToken, |
|
765 | + 'added' => [], |
|
766 | + 'modified' => [], |
|
767 | + 'deleted' => [], |
|
768 | + ]; |
|
769 | + |
|
770 | + if ($syncToken) { |
|
771 | + |
|
772 | + $query = "SELECT `uri`, `operation` FROM `*PREFIX*addressbookchanges` WHERE `synctoken` >= ? AND `synctoken` < ? AND `addressbookid` = ? ORDER BY `synctoken`"; |
|
773 | + if ($limit>0) { |
|
774 | + $query .= " `LIMIT` " . (int)$limit; |
|
775 | + } |
|
776 | + |
|
777 | + // Fetching all changes |
|
778 | + $stmt = $this->db->prepare($query); |
|
779 | + $stmt->execute([$syncToken, $currentToken, $addressBookId]); |
|
780 | + |
|
781 | + $changes = []; |
|
782 | + |
|
783 | + // This loop ensures that any duplicates are overwritten, only the |
|
784 | + // last change on a node is relevant. |
|
785 | + while($row = $stmt->fetch(\PDO::FETCH_ASSOC)) { |
|
786 | + |
|
787 | + $changes[$row['uri']] = $row['operation']; |
|
788 | + |
|
789 | + } |
|
790 | + |
|
791 | + foreach($changes as $uri => $operation) { |
|
792 | + |
|
793 | + switch($operation) { |
|
794 | + case 1: |
|
795 | + $result['added'][] = $uri; |
|
796 | + break; |
|
797 | + case 2: |
|
798 | + $result['modified'][] = $uri; |
|
799 | + break; |
|
800 | + case 3: |
|
801 | + $result['deleted'][] = $uri; |
|
802 | + break; |
|
803 | + } |
|
804 | + |
|
805 | + } |
|
806 | + } else { |
|
807 | + // No synctoken supplied, this is the initial sync. |
|
808 | + $query = "SELECT `uri` FROM `*PREFIX*cards` WHERE `addressbookid` = ?"; |
|
809 | + $stmt = $this->db->prepare($query); |
|
810 | + $stmt->execute([$addressBookId]); |
|
811 | + |
|
812 | + $result['added'] = $stmt->fetchAll(\PDO::FETCH_COLUMN); |
|
813 | + } |
|
814 | + return $result; |
|
815 | + } |
|
816 | + |
|
817 | + /** |
|
818 | + * Adds a change record to the addressbookchanges table. |
|
819 | + * |
|
820 | + * @param mixed $addressBookId |
|
821 | + * @param string $objectUri |
|
822 | + * @param int $operation 1 = add, 2 = modify, 3 = delete |
|
823 | + * @return void |
|
824 | + */ |
|
825 | + protected function addChange($addressBookId, $objectUri, $operation) { |
|
826 | + $sql = 'INSERT INTO `*PREFIX*addressbookchanges`(`uri`, `synctoken`, `addressbookid`, `operation`) SELECT ?, `synctoken`, ?, ? FROM `*PREFIX*addressbooks` WHERE `id` = ?'; |
|
827 | + $stmt = $this->db->prepare($sql); |
|
828 | + $stmt->execute([ |
|
829 | + $objectUri, |
|
830 | + $addressBookId, |
|
831 | + $operation, |
|
832 | + $addressBookId |
|
833 | + ]); |
|
834 | + $stmt = $this->db->prepare('UPDATE `*PREFIX*addressbooks` SET `synctoken` = `synctoken` + 1 WHERE `id` = ?'); |
|
835 | + $stmt->execute([ |
|
836 | + $addressBookId |
|
837 | + ]); |
|
838 | + } |
|
839 | + |
|
840 | + private function readBlob($cardData) { |
|
841 | + if (is_resource($cardData)) { |
|
842 | + return stream_get_contents($cardData); |
|
843 | + } |
|
844 | + |
|
845 | + return $cardData; |
|
846 | + } |
|
847 | + |
|
848 | + /** |
|
849 | + * @param IShareable $shareable |
|
850 | + * @param string[] $add |
|
851 | + * @param string[] $remove |
|
852 | + */ |
|
853 | + public function updateShares(IShareable $shareable, $add, $remove) { |
|
854 | + $this->sharingBackend->updateShares($shareable, $add, $remove); |
|
855 | + } |
|
856 | + |
|
857 | + /** |
|
858 | + * search contact |
|
859 | + * |
|
860 | + * @param int $addressBookId |
|
861 | + * @param string $pattern which should match within the $searchProperties |
|
862 | + * @param array $searchProperties defines the properties within the query pattern should match |
|
863 | + * @return array an array of contacts which are arrays of key-value-pairs |
|
864 | + */ |
|
865 | + public function search($addressBookId, $pattern, $searchProperties) { |
|
866 | + $query = $this->db->getQueryBuilder(); |
|
867 | + $query2 = $this->db->getQueryBuilder(); |
|
868 | + |
|
869 | + $query2->selectDistinct('cp.cardid')->from($this->dbCardsPropertiesTable, 'cp'); |
|
870 | + $query2->andWhere($query2->expr()->eq('cp.addressbookid', $query->createNamedParameter($addressBookId))); |
|
871 | + foreach ($searchProperties as $property) { |
|
872 | + $query2->expr()->orX($query2->expr()->eq('cp.name', $query->createNamedParameter($property))); |
|
873 | + } |
|
874 | + $query2->andWhere($query2->expr()->ilike('cp.value', $query->createNamedParameter('%' . $this->db->escapeLikeParameter($pattern) . '%'))); |
|
875 | + |
|
876 | + $query->select('c.carddata', 'c.uri')->from($this->dbCardsTable, 'c') |
|
877 | + ->where($query->expr()->in('c.id', $query->createFunction($query2->getSQL()))); |
|
878 | + |
|
879 | + $result = $query->execute(); |
|
880 | + $cards = $result->fetchAll(); |
|
881 | + |
|
882 | + $result->closeCursor(); |
|
883 | + |
|
884 | + return array_map(function($array) { |
|
885 | + $array['carddata'] = $this->readBlob($array['carddata']); |
|
886 | + return $array; |
|
887 | + }, $cards); |
|
888 | + } |
|
889 | + |
|
890 | + /** |
|
891 | + * @param int $bookId |
|
892 | + * @param string $name |
|
893 | + * @return array |
|
894 | + */ |
|
895 | + public function collectCardProperties($bookId, $name) { |
|
896 | + $query = $this->db->getQueryBuilder(); |
|
897 | + $result = $query->selectDistinct('value') |
|
898 | + ->from($this->dbCardsPropertiesTable) |
|
899 | + ->where($query->expr()->eq('name', $query->createNamedParameter($name))) |
|
900 | + ->andWhere($query->expr()->eq('addressbookid', $query->createNamedParameter($bookId))) |
|
901 | + ->execute(); |
|
902 | + |
|
903 | + $all = $result->fetchAll(PDO::FETCH_COLUMN); |
|
904 | + $result->closeCursor(); |
|
905 | + |
|
906 | + return $all; |
|
907 | + } |
|
908 | + |
|
909 | + /** |
|
910 | + * get URI from a given contact |
|
911 | + * |
|
912 | + * @param int $id |
|
913 | + * @return string |
|
914 | + */ |
|
915 | + public function getCardUri($id) { |
|
916 | + $query = $this->db->getQueryBuilder(); |
|
917 | + $query->select('uri')->from($this->dbCardsTable) |
|
918 | + ->where($query->expr()->eq('id', $query->createParameter('id'))) |
|
919 | + ->setParameter('id', $id); |
|
920 | + |
|
921 | + $result = $query->execute(); |
|
922 | + $uri = $result->fetch(); |
|
923 | + $result->closeCursor(); |
|
924 | + |
|
925 | + if (!isset($uri['uri'])) { |
|
926 | + throw new \InvalidArgumentException('Card does not exists: ' . $id); |
|
927 | + } |
|
928 | + |
|
929 | + return $uri['uri']; |
|
930 | + } |
|
931 | + |
|
932 | + /** |
|
933 | + * return contact with the given URI |
|
934 | + * |
|
935 | + * @param int $addressBookId |
|
936 | + * @param string $uri |
|
937 | + * @returns array |
|
938 | + */ |
|
939 | + public function getContact($addressBookId, $uri) { |
|
940 | + $result = []; |
|
941 | + $query = $this->db->getQueryBuilder(); |
|
942 | + $query->select('*')->from($this->dbCardsTable) |
|
943 | + ->where($query->expr()->eq('uri', $query->createNamedParameter($uri))) |
|
944 | + ->andWhere($query->expr()->eq('addressbookid', $query->createNamedParameter($addressBookId))); |
|
945 | + $queryResult = $query->execute(); |
|
946 | + $contact = $queryResult->fetch(); |
|
947 | + $queryResult->closeCursor(); |
|
948 | + |
|
949 | + if (is_array($contact)) { |
|
950 | + $result = $contact; |
|
951 | + } |
|
952 | + |
|
953 | + return $result; |
|
954 | + } |
|
955 | + |
|
956 | + /** |
|
957 | + * Returns the list of people whom this address book is shared with. |
|
958 | + * |
|
959 | + * Every element in this array should have the following properties: |
|
960 | + * * href - Often a mailto: address |
|
961 | + * * commonName - Optional, for example a first + last name |
|
962 | + * * status - See the Sabre\CalDAV\SharingPlugin::STATUS_ constants. |
|
963 | + * * readOnly - boolean |
|
964 | + * * summary - Optional, a description for the share |
|
965 | + * |
|
966 | + * @return array |
|
967 | + */ |
|
968 | + public function getShares($addressBookId) { |
|
969 | + return $this->sharingBackend->getShares($addressBookId); |
|
970 | + } |
|
971 | + |
|
972 | + /** |
|
973 | + * update properties table |
|
974 | + * |
|
975 | + * @param int $addressBookId |
|
976 | + * @param string $cardUri |
|
977 | + * @param string $vCardSerialized |
|
978 | + */ |
|
979 | + protected function updateProperties($addressBookId, $cardUri, $vCardSerialized) { |
|
980 | + $cardId = $this->getCardId($addressBookId, $cardUri); |
|
981 | + $vCard = $this->readCard($vCardSerialized); |
|
982 | + |
|
983 | + $this->purgeProperties($addressBookId, $cardId); |
|
984 | + |
|
985 | + $query = $this->db->getQueryBuilder(); |
|
986 | + $query->insert($this->dbCardsPropertiesTable) |
|
987 | + ->values( |
|
988 | + [ |
|
989 | + 'addressbookid' => $query->createNamedParameter($addressBookId), |
|
990 | + 'cardid' => $query->createNamedParameter($cardId), |
|
991 | + 'name' => $query->createParameter('name'), |
|
992 | + 'value' => $query->createParameter('value'), |
|
993 | + 'preferred' => $query->createParameter('preferred') |
|
994 | + ] |
|
995 | + ); |
|
996 | + |
|
997 | + foreach ($vCard->children() as $property) { |
|
998 | + if(!in_array($property->name, self::$indexProperties)) { |
|
999 | + continue; |
|
1000 | + } |
|
1001 | + $preferred = 0; |
|
1002 | + foreach($property->parameters as $parameter) { |
|
1003 | + if ($parameter->name == 'TYPE' && strtoupper($parameter->getValue()) == 'PREF') { |
|
1004 | + $preferred = 1; |
|
1005 | + break; |
|
1006 | + } |
|
1007 | + } |
|
1008 | + $query->setParameter('name', $property->name); |
|
1009 | + $query->setParameter('value', substr($property->getValue(), 0, 254)); |
|
1010 | + $query->setParameter('preferred', $preferred); |
|
1011 | + $query->execute(); |
|
1012 | + } |
|
1013 | + } |
|
1014 | + |
|
1015 | + /** |
|
1016 | + * read vCard data into a vCard object |
|
1017 | + * |
|
1018 | + * @param string $cardData |
|
1019 | + * @return VCard |
|
1020 | + */ |
|
1021 | + protected function readCard($cardData) { |
|
1022 | + return Reader::read($cardData); |
|
1023 | + } |
|
1024 | + |
|
1025 | + /** |
|
1026 | + * delete all properties from a given card |
|
1027 | + * |
|
1028 | + * @param int $addressBookId |
|
1029 | + * @param int $cardId |
|
1030 | + */ |
|
1031 | + protected function purgeProperties($addressBookId, $cardId) { |
|
1032 | + $query = $this->db->getQueryBuilder(); |
|
1033 | + $query->delete($this->dbCardsPropertiesTable) |
|
1034 | + ->where($query->expr()->eq('cardid', $query->createNamedParameter($cardId))) |
|
1035 | + ->andWhere($query->expr()->eq('addressbookid', $query->createNamedParameter($addressBookId))); |
|
1036 | + $query->execute(); |
|
1037 | + } |
|
1038 | + |
|
1039 | + /** |
|
1040 | + * get ID from a given contact |
|
1041 | + * |
|
1042 | + * @param int $addressBookId |
|
1043 | + * @param string $uri |
|
1044 | + * @return int |
|
1045 | + */ |
|
1046 | + protected function getCardId($addressBookId, $uri) { |
|
1047 | + $query = $this->db->getQueryBuilder(); |
|
1048 | + $query->select('id')->from($this->dbCardsTable) |
|
1049 | + ->where($query->expr()->eq('uri', $query->createNamedParameter($uri))) |
|
1050 | + ->andWhere($query->expr()->eq('addressbookid', $query->createNamedParameter($addressBookId))); |
|
1051 | + |
|
1052 | + $result = $query->execute(); |
|
1053 | + $cardIds = $result->fetch(); |
|
1054 | + $result->closeCursor(); |
|
1055 | + |
|
1056 | + if (!isset($cardIds['id'])) { |
|
1057 | + throw new \InvalidArgumentException('Card does not exists: ' . $uri); |
|
1058 | + } |
|
1059 | + |
|
1060 | + return (int)$cardIds['id']; |
|
1061 | + } |
|
1062 | + |
|
1063 | + /** |
|
1064 | + * For shared address books the sharee is set in the ACL of the address book |
|
1065 | + * @param $addressBookId |
|
1066 | + * @param $acl |
|
1067 | + * @return array |
|
1068 | + */ |
|
1069 | + public function applyShareAcl($addressBookId, $acl) { |
|
1070 | + return $this->sharingBackend->applyShareAcl($addressBookId, $acl); |
|
1071 | + } |
|
1072 | + |
|
1073 | + private function convertPrincipal($principalUri, $toV2) { |
|
1074 | + if ($this->principalBackend->getPrincipalPrefix() === 'principals') { |
|
1075 | + list(, $name) = URLUtil::splitPath($principalUri); |
|
1076 | + if ($toV2 === true) { |
|
1077 | + return "principals/users/$name"; |
|
1078 | + } |
|
1079 | + return "principals/$name"; |
|
1080 | + } |
|
1081 | + return $principalUri; |
|
1082 | + } |
|
1083 | 1083 | } |
@@ -114,7 +114,7 @@ discard block |
||
114 | 114 | ->from('addressbooks') |
115 | 115 | ->where($query->expr()->eq('principaluri', $query->createNamedParameter($principalUri))); |
116 | 116 | |
117 | - return (int)$query->execute()->fetchColumn(); |
|
117 | + return (int) $query->execute()->fetchColumn(); |
|
118 | 118 | } |
119 | 119 | |
120 | 120 | /** |
@@ -145,22 +145,22 @@ discard block |
||
145 | 145 | $addressBooks = []; |
146 | 146 | |
147 | 147 | $result = $query->execute(); |
148 | - while($row = $result->fetch()) { |
|
148 | + while ($row = $result->fetch()) { |
|
149 | 149 | $addressBooks[$row['id']] = [ |
150 | 150 | 'id' => $row['id'], |
151 | 151 | 'uri' => $row['uri'], |
152 | 152 | 'principaluri' => $this->convertPrincipal($row['principaluri'], false), |
153 | 153 | '{DAV:}displayname' => $row['displayname'], |
154 | - '{' . Plugin::NS_CARDDAV . '}addressbook-description' => $row['description'], |
|
154 | + '{'.Plugin::NS_CARDDAV.'}addressbook-description' => $row['description'], |
|
155 | 155 | '{http://calendarserver.org/ns/}getctag' => $row['synctoken'], |
156 | - '{http://sabredav.org/ns}sync-token' => $row['synctoken']?$row['synctoken']:'0', |
|
156 | + '{http://sabredav.org/ns}sync-token' => $row['synctoken'] ? $row['synctoken'] : '0', |
|
157 | 157 | ]; |
158 | 158 | } |
159 | 159 | $result->closeCursor(); |
160 | 160 | |
161 | 161 | // query for shared calendars |
162 | 162 | $principals = $this->principalBackend->getGroupMembership($principalUriOriginal, true); |
163 | - $principals[]= $principalUri; |
|
163 | + $principals[] = $principalUri; |
|
164 | 164 | |
165 | 165 | $query = $this->db->getQueryBuilder(); |
166 | 166 | $result = $query->select(['a.id', 'a.uri', 'a.displayname', 'a.principaluri', 'a.description', 'a.synctoken', 's.access']) |
@@ -172,8 +172,8 @@ discard block |
||
172 | 172 | ->setParameter('principaluri', $principals, IQueryBuilder::PARAM_STR_ARRAY) |
173 | 173 | ->execute(); |
174 | 174 | |
175 | - $readOnlyPropertyName = '{' . \OCA\DAV\DAV\Sharing\Plugin::NS_OWNCLOUD . '}read-only'; |
|
176 | - while($row = $result->fetch()) { |
|
175 | + $readOnlyPropertyName = '{'.\OCA\DAV\DAV\Sharing\Plugin::NS_OWNCLOUD.'}read-only'; |
|
176 | + while ($row = $result->fetch()) { |
|
177 | 177 | $readOnly = (int) $row['access'] === Backend::ACCESS_READ; |
178 | 178 | if (isset($addressBooks[$row['id']])) { |
179 | 179 | if ($readOnly) { |
@@ -188,18 +188,18 @@ discard block |
||
188 | 188 | } |
189 | 189 | |
190 | 190 | list(, $name) = URLUtil::splitPath($row['principaluri']); |
191 | - $uri = $row['uri'] . '_shared_by_' . $name; |
|
192 | - $displayName = $row['displayname'] . ' (' . $this->getUserDisplayName($name) . ')'; |
|
191 | + $uri = $row['uri'].'_shared_by_'.$name; |
|
192 | + $displayName = $row['displayname'].' ('.$this->getUserDisplayName($name).')'; |
|
193 | 193 | |
194 | 194 | $addressBooks[$row['id']] = [ |
195 | 195 | 'id' => $row['id'], |
196 | 196 | 'uri' => $uri, |
197 | 197 | 'principaluri' => $principalUriOriginal, |
198 | 198 | '{DAV:}displayname' => $displayName, |
199 | - '{' . Plugin::NS_CARDDAV . '}addressbook-description' => $row['description'], |
|
199 | + '{'.Plugin::NS_CARDDAV.'}addressbook-description' => $row['description'], |
|
200 | 200 | '{http://calendarserver.org/ns/}getctag' => $row['synctoken'], |
201 | - '{http://sabredav.org/ns}sync-token' => $row['synctoken']?$row['synctoken']:'0', |
|
202 | - '{' . \OCA\DAV\DAV\Sharing\Plugin::NS_OWNCLOUD . '}owner-principal' => $row['principaluri'], |
|
201 | + '{http://sabredav.org/ns}sync-token' => $row['synctoken'] ? $row['synctoken'] : '0', |
|
202 | + '{'.\OCA\DAV\DAV\Sharing\Plugin::NS_OWNCLOUD.'}owner-principal' => $row['principaluri'], |
|
203 | 203 | $readOnlyPropertyName => $readOnly, |
204 | 204 | ]; |
205 | 205 | } |
@@ -219,15 +219,15 @@ discard block |
||
219 | 219 | $addressBooks = []; |
220 | 220 | |
221 | 221 | $result = $query->execute(); |
222 | - while($row = $result->fetch()) { |
|
222 | + while ($row = $result->fetch()) { |
|
223 | 223 | $addressBooks[$row['id']] = [ |
224 | 224 | 'id' => $row['id'], |
225 | 225 | 'uri' => $row['uri'], |
226 | 226 | 'principaluri' => $this->convertPrincipal($row['principaluri'], false), |
227 | 227 | '{DAV:}displayname' => $row['displayname'], |
228 | - '{' . Plugin::NS_CARDDAV . '}addressbook-description' => $row['description'], |
|
228 | + '{'.Plugin::NS_CARDDAV.'}addressbook-description' => $row['description'], |
|
229 | 229 | '{http://calendarserver.org/ns/}getctag' => $row['synctoken'], |
230 | - '{http://sabredav.org/ns}sync-token' => $row['synctoken']?$row['synctoken']:'0', |
|
230 | + '{http://sabredav.org/ns}sync-token' => $row['synctoken'] ? $row['synctoken'] : '0', |
|
231 | 231 | ]; |
232 | 232 | } |
233 | 233 | $result->closeCursor(); |
@@ -270,9 +270,9 @@ discard block |
||
270 | 270 | 'uri' => $row['uri'], |
271 | 271 | 'principaluri' => $row['principaluri'], |
272 | 272 | '{DAV:}displayname' => $row['displayname'], |
273 | - '{' . Plugin::NS_CARDDAV . '}addressbook-description' => $row['description'], |
|
273 | + '{'.Plugin::NS_CARDDAV.'}addressbook-description' => $row['description'], |
|
274 | 274 | '{http://calendarserver.org/ns/}getctag' => $row['synctoken'], |
275 | - '{http://sabredav.org/ns}sync-token' => $row['synctoken']?$row['synctoken']:'0', |
|
275 | + '{http://sabredav.org/ns}sync-token' => $row['synctoken'] ? $row['synctoken'] : '0', |
|
276 | 276 | ]; |
277 | 277 | } |
278 | 278 | |
@@ -300,9 +300,9 @@ discard block |
||
300 | 300 | 'uri' => $row['uri'], |
301 | 301 | 'principaluri' => $row['principaluri'], |
302 | 302 | '{DAV:}displayname' => $row['displayname'], |
303 | - '{' . Plugin::NS_CARDDAV . '}addressbook-description' => $row['description'], |
|
303 | + '{'.Plugin::NS_CARDDAV.'}addressbook-description' => $row['description'], |
|
304 | 304 | '{http://calendarserver.org/ns/}getctag' => $row['synctoken'], |
305 | - '{http://sabredav.org/ns}sync-token' => $row['synctoken']?$row['synctoken']:'0', |
|
305 | + '{http://sabredav.org/ns}sync-token' => $row['synctoken'] ? $row['synctoken'] : '0', |
|
306 | 306 | ]; |
307 | 307 | } |
308 | 308 | |
@@ -325,19 +325,19 @@ discard block |
||
325 | 325 | function updateAddressBook($addressBookId, \Sabre\DAV\PropPatch $propPatch) { |
326 | 326 | $supportedProperties = [ |
327 | 327 | '{DAV:}displayname', |
328 | - '{' . Plugin::NS_CARDDAV . '}addressbook-description', |
|
328 | + '{'.Plugin::NS_CARDDAV.'}addressbook-description', |
|
329 | 329 | ]; |
330 | 330 | |
331 | 331 | $propPatch->handle($supportedProperties, function($mutations) use ($addressBookId) { |
332 | 332 | |
333 | 333 | $updates = []; |
334 | - foreach($mutations as $property=>$newValue) { |
|
334 | + foreach ($mutations as $property=>$newValue) { |
|
335 | 335 | |
336 | - switch($property) { |
|
336 | + switch ($property) { |
|
337 | 337 | case '{DAV:}displayname' : |
338 | 338 | $updates['displayname'] = $newValue; |
339 | 339 | break; |
340 | - case '{' . Plugin::NS_CARDDAV . '}addressbook-description' : |
|
340 | + case '{'.Plugin::NS_CARDDAV.'}addressbook-description' : |
|
341 | 341 | $updates['description'] = $newValue; |
342 | 342 | break; |
343 | 343 | } |
@@ -345,7 +345,7 @@ discard block |
||
345 | 345 | $query = $this->db->getQueryBuilder(); |
346 | 346 | $query->update('addressbooks'); |
347 | 347 | |
348 | - foreach($updates as $key=>$value) { |
|
348 | + foreach ($updates as $key=>$value) { |
|
349 | 349 | $query->set($key, $query->createNamedParameter($value)); |
350 | 350 | } |
351 | 351 | $query->where($query->expr()->eq('id', $query->createNamedParameter($addressBookId))) |
@@ -376,24 +376,24 @@ discard block |
||
376 | 376 | 'synctoken' => 1 |
377 | 377 | ]; |
378 | 378 | |
379 | - foreach($properties as $property=>$newValue) { |
|
379 | + foreach ($properties as $property=>$newValue) { |
|
380 | 380 | |
381 | - switch($property) { |
|
381 | + switch ($property) { |
|
382 | 382 | case '{DAV:}displayname' : |
383 | 383 | $values['displayname'] = $newValue; |
384 | 384 | break; |
385 | - case '{' . Plugin::NS_CARDDAV . '}addressbook-description' : |
|
385 | + case '{'.Plugin::NS_CARDDAV.'}addressbook-description' : |
|
386 | 386 | $values['description'] = $newValue; |
387 | 387 | break; |
388 | 388 | default : |
389 | - throw new BadRequest('Unknown property: ' . $property); |
|
389 | + throw new BadRequest('Unknown property: '.$property); |
|
390 | 390 | } |
391 | 391 | |
392 | 392 | } |
393 | 393 | |
394 | 394 | // Fallback to make sure the displayname is set. Some clients may refuse |
395 | 395 | // to work with addressbooks not having a displayname. |
396 | - if(is_null($values['displayname'])) { |
|
396 | + if (is_null($values['displayname'])) { |
|
397 | 397 | $values['displayname'] = $url; |
398 | 398 | } |
399 | 399 | |
@@ -471,8 +471,8 @@ discard block |
||
471 | 471 | $cards = []; |
472 | 472 | |
473 | 473 | $result = $query->execute(); |
474 | - while($row = $result->fetch()) { |
|
475 | - $row['etag'] = '"' . $row['etag'] . '"'; |
|
474 | + while ($row = $result->fetch()) { |
|
475 | + $row['etag'] = '"'.$row['etag'].'"'; |
|
476 | 476 | $row['carddata'] = $this->readBlob($row['carddata']); |
477 | 477 | $cards[] = $row; |
478 | 478 | } |
@@ -506,7 +506,7 @@ discard block |
||
506 | 506 | if (!$row) { |
507 | 507 | return false; |
508 | 508 | } |
509 | - $row['etag'] = '"' . $row['etag'] . '"'; |
|
509 | + $row['etag'] = '"'.$row['etag'].'"'; |
|
510 | 510 | $row['carddata'] = $this->readBlob($row['carddata']); |
511 | 511 | |
512 | 512 | return $row; |
@@ -543,7 +543,7 @@ discard block |
||
543 | 543 | $result = $query->execute(); |
544 | 544 | |
545 | 545 | while ($row = $result->fetch()) { |
546 | - $row['etag'] = '"' . $row['etag'] . '"'; |
|
546 | + $row['etag'] = '"'.$row['etag'].'"'; |
|
547 | 547 | $row['carddata'] = $this->readBlob($row['carddata']); |
548 | 548 | $cards[] = $row; |
549 | 549 | } |
@@ -603,7 +603,7 @@ discard block |
||
603 | 603 | 'cardData' => $cardData])); |
604 | 604 | } |
605 | 605 | |
606 | - return '"' . $etag . '"'; |
|
606 | + return '"'.$etag.'"'; |
|
607 | 607 | } |
608 | 608 | |
609 | 609 | /** |
@@ -655,7 +655,7 @@ discard block |
||
655 | 655 | 'cardData' => $cardData])); |
656 | 656 | } |
657 | 657 | |
658 | - return '"' . $etag . '"'; |
|
658 | + return '"'.$etag.'"'; |
|
659 | 659 | } |
660 | 660 | |
661 | 661 | /** |
@@ -755,7 +755,7 @@ discard block |
||
755 | 755 | function getChangesForAddressBook($addressBookId, $syncToken, $syncLevel, $limit = null) { |
756 | 756 | // Current synctoken |
757 | 757 | $stmt = $this->db->prepare('SELECT `synctoken` FROM `*PREFIX*addressbooks` WHERE `id` = ?'); |
758 | - $stmt->execute([ $addressBookId ]); |
|
758 | + $stmt->execute([$addressBookId]); |
|
759 | 759 | $currentToken = $stmt->fetchColumn(0); |
760 | 760 | |
761 | 761 | if (is_null($currentToken)) return null; |
@@ -770,8 +770,8 @@ discard block |
||
770 | 770 | if ($syncToken) { |
771 | 771 | |
772 | 772 | $query = "SELECT `uri`, `operation` FROM `*PREFIX*addressbookchanges` WHERE `synctoken` >= ? AND `synctoken` < ? AND `addressbookid` = ? ORDER BY `synctoken`"; |
773 | - if ($limit>0) { |
|
774 | - $query .= " `LIMIT` " . (int)$limit; |
|
773 | + if ($limit > 0) { |
|
774 | + $query .= " `LIMIT` ".(int) $limit; |
|
775 | 775 | } |
776 | 776 | |
777 | 777 | // Fetching all changes |
@@ -782,15 +782,15 @@ discard block |
||
782 | 782 | |
783 | 783 | // This loop ensures that any duplicates are overwritten, only the |
784 | 784 | // last change on a node is relevant. |
785 | - while($row = $stmt->fetch(\PDO::FETCH_ASSOC)) { |
|
785 | + while ($row = $stmt->fetch(\PDO::FETCH_ASSOC)) { |
|
786 | 786 | |
787 | 787 | $changes[$row['uri']] = $row['operation']; |
788 | 788 | |
789 | 789 | } |
790 | 790 | |
791 | - foreach($changes as $uri => $operation) { |
|
791 | + foreach ($changes as $uri => $operation) { |
|
792 | 792 | |
793 | - switch($operation) { |
|
793 | + switch ($operation) { |
|
794 | 794 | case 1: |
795 | 795 | $result['added'][] = $uri; |
796 | 796 | break; |
@@ -871,7 +871,7 @@ discard block |
||
871 | 871 | foreach ($searchProperties as $property) { |
872 | 872 | $query2->expr()->orX($query2->expr()->eq('cp.name', $query->createNamedParameter($property))); |
873 | 873 | } |
874 | - $query2->andWhere($query2->expr()->ilike('cp.value', $query->createNamedParameter('%' . $this->db->escapeLikeParameter($pattern) . '%'))); |
|
874 | + $query2->andWhere($query2->expr()->ilike('cp.value', $query->createNamedParameter('%'.$this->db->escapeLikeParameter($pattern).'%'))); |
|
875 | 875 | |
876 | 876 | $query->select('c.carddata', 'c.uri')->from($this->dbCardsTable, 'c') |
877 | 877 | ->where($query->expr()->in('c.id', $query->createFunction($query2->getSQL()))); |
@@ -923,7 +923,7 @@ discard block |
||
923 | 923 | $result->closeCursor(); |
924 | 924 | |
925 | 925 | if (!isset($uri['uri'])) { |
926 | - throw new \InvalidArgumentException('Card does not exists: ' . $id); |
|
926 | + throw new \InvalidArgumentException('Card does not exists: '.$id); |
|
927 | 927 | } |
928 | 928 | |
929 | 929 | return $uri['uri']; |
@@ -995,11 +995,11 @@ discard block |
||
995 | 995 | ); |
996 | 996 | |
997 | 997 | foreach ($vCard->children() as $property) { |
998 | - if(!in_array($property->name, self::$indexProperties)) { |
|
998 | + if (!in_array($property->name, self::$indexProperties)) { |
|
999 | 999 | continue; |
1000 | 1000 | } |
1001 | 1001 | $preferred = 0; |
1002 | - foreach($property->parameters as $parameter) { |
|
1002 | + foreach ($property->parameters as $parameter) { |
|
1003 | 1003 | if ($parameter->name == 'TYPE' && strtoupper($parameter->getValue()) == 'PREF') { |
1004 | 1004 | $preferred = 1; |
1005 | 1005 | break; |
@@ -1054,10 +1054,10 @@ discard block |
||
1054 | 1054 | $result->closeCursor(); |
1055 | 1055 | |
1056 | 1056 | if (!isset($cardIds['id'])) { |
1057 | - throw new \InvalidArgumentException('Card does not exists: ' . $uri); |
|
1057 | + throw new \InvalidArgumentException('Card does not exists: '.$uri); |
|
1058 | 1058 | } |
1059 | 1059 | |
1060 | - return (int)$cardIds['id']; |
|
1060 | + return (int) $cardIds['id']; |
|
1061 | 1061 | } |
1062 | 1062 | |
1063 | 1063 | /** |