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