@@ -60,1307 +60,1307 @@ |
||
| 60 | 60 | use Sabre\VObject\Reader; |
| 61 | 61 | |
| 62 | 62 | class CardDavBackend implements BackendInterface, SyncSupport { |
| 63 | - public const PERSONAL_ADDRESSBOOK_URI = 'contacts'; |
|
| 64 | - public const PERSONAL_ADDRESSBOOK_NAME = 'Contacts'; |
|
| 65 | - |
|
| 66 | - private Principal $principalBackend; |
|
| 67 | - private string $dbCardsTable = 'cards'; |
|
| 68 | - private string $dbCardsPropertiesTable = 'cards_properties'; |
|
| 69 | - private IDBConnection $db; |
|
| 70 | - private Backend $sharingBackend; |
|
| 71 | - |
|
| 72 | - /** @var array properties to index */ |
|
| 73 | - public static array $indexProperties = [ |
|
| 74 | - 'BDAY', 'UID', 'N', 'FN', 'TITLE', 'ROLE', 'NOTE', 'NICKNAME', |
|
| 75 | - 'ORG', 'CATEGORIES', 'EMAIL', 'TEL', 'IMPP', 'ADR', 'URL', 'GEO', |
|
| 76 | - 'CLOUD', 'X-SOCIALPROFILE']; |
|
| 77 | - |
|
| 78 | - /** |
|
| 79 | - * @var string[] Map of uid => display name |
|
| 80 | - */ |
|
| 81 | - protected array $userDisplayNames; |
|
| 82 | - private IUserManager $userManager; |
|
| 83 | - private IEventDispatcher $dispatcher; |
|
| 84 | - private array $etagCache = []; |
|
| 85 | - |
|
| 86 | - /** |
|
| 87 | - * CardDavBackend constructor. |
|
| 88 | - * |
|
| 89 | - * @param IDBConnection $db |
|
| 90 | - * @param Principal $principalBackend |
|
| 91 | - * @param IUserManager $userManager |
|
| 92 | - * @param IGroupManager $groupManager |
|
| 93 | - * @param IEventDispatcher $dispatcher |
|
| 94 | - */ |
|
| 95 | - public function __construct(IDBConnection $db, |
|
| 96 | - Principal $principalBackend, |
|
| 97 | - IUserManager $userManager, |
|
| 98 | - IGroupManager $groupManager, |
|
| 99 | - IEventDispatcher $dispatcher) { |
|
| 100 | - $this->db = $db; |
|
| 101 | - $this->principalBackend = $principalBackend; |
|
| 102 | - $this->userManager = $userManager; |
|
| 103 | - $this->dispatcher = $dispatcher; |
|
| 104 | - $this->sharingBackend = new Backend($this->db, $this->userManager, $groupManager, $principalBackend, 'addressbook'); |
|
| 105 | - } |
|
| 106 | - |
|
| 107 | - /** |
|
| 108 | - * Return the number of address books for a principal |
|
| 109 | - * |
|
| 110 | - * @param $principalUri |
|
| 111 | - * @return int |
|
| 112 | - */ |
|
| 113 | - public function getAddressBooksForUserCount($principalUri) { |
|
| 114 | - $principalUri = $this->convertPrincipal($principalUri, true); |
|
| 115 | - $query = $this->db->getQueryBuilder(); |
|
| 116 | - $query->select($query->func()->count('*')) |
|
| 117 | - ->from('addressbooks') |
|
| 118 | - ->where($query->expr()->eq('principaluri', $query->createNamedParameter($principalUri))); |
|
| 119 | - |
|
| 120 | - $result = $query->executeQuery(); |
|
| 121 | - $column = (int) $result->fetchOne(); |
|
| 122 | - $result->closeCursor(); |
|
| 123 | - return $column; |
|
| 124 | - } |
|
| 125 | - |
|
| 126 | - /** |
|
| 127 | - * Returns the list of address books for a specific user. |
|
| 128 | - * |
|
| 129 | - * Every addressbook should have the following properties: |
|
| 130 | - * id - an arbitrary unique id |
|
| 131 | - * uri - the 'basename' part of the url |
|
| 132 | - * principaluri - Same as the passed parameter |
|
| 133 | - * |
|
| 134 | - * Any additional clark-notation property may be passed besides this. Some |
|
| 135 | - * common ones are : |
|
| 136 | - * {DAV:}displayname |
|
| 137 | - * {urn:ietf:params:xml:ns:carddav}addressbook-description |
|
| 138 | - * {http://calendarserver.org/ns/}getctag |
|
| 139 | - * |
|
| 140 | - * @param string $principalUri |
|
| 141 | - * @return array |
|
| 142 | - */ |
|
| 143 | - public function getAddressBooksForUser($principalUri) { |
|
| 144 | - $principalUriOriginal = $principalUri; |
|
| 145 | - $principalUri = $this->convertPrincipal($principalUri, true); |
|
| 146 | - $query = $this->db->getQueryBuilder(); |
|
| 147 | - $query->select(['id', 'uri', 'displayname', 'principaluri', 'description', 'synctoken']) |
|
| 148 | - ->from('addressbooks') |
|
| 149 | - ->where($query->expr()->eq('principaluri', $query->createNamedParameter($principalUri))); |
|
| 150 | - |
|
| 151 | - $addressBooks = []; |
|
| 152 | - |
|
| 153 | - $result = $query->execute(); |
|
| 154 | - while ($row = $result->fetch()) { |
|
| 155 | - $addressBooks[$row['id']] = [ |
|
| 156 | - 'id' => $row['id'], |
|
| 157 | - 'uri' => $row['uri'], |
|
| 158 | - 'principaluri' => $this->convertPrincipal($row['principaluri'], false), |
|
| 159 | - '{DAV:}displayname' => $row['displayname'], |
|
| 160 | - '{' . Plugin::NS_CARDDAV . '}addressbook-description' => $row['description'], |
|
| 161 | - '{http://calendarserver.org/ns/}getctag' => $row['synctoken'], |
|
| 162 | - '{http://sabredav.org/ns}sync-token' => $row['synctoken'] ?: '0', |
|
| 163 | - ]; |
|
| 164 | - |
|
| 165 | - $this->addOwnerPrincipal($addressBooks[$row['id']]); |
|
| 166 | - } |
|
| 167 | - $result->closeCursor(); |
|
| 168 | - |
|
| 169 | - // query for shared addressbooks |
|
| 170 | - $principals = $this->principalBackend->getGroupMembership($principalUriOriginal, true); |
|
| 171 | - $principals = array_merge($principals, $this->principalBackend->getCircleMembership($principalUriOriginal)); |
|
| 172 | - |
|
| 173 | - $principals[] = $principalUri; |
|
| 174 | - |
|
| 175 | - $query = $this->db->getQueryBuilder(); |
|
| 176 | - $result = $query->select(['a.id', 'a.uri', 'a.displayname', 'a.principaluri', 'a.description', 'a.synctoken', 's.access']) |
|
| 177 | - ->from('dav_shares', 's') |
|
| 178 | - ->join('s', 'addressbooks', 'a', $query->expr()->eq('s.resourceid', 'a.id')) |
|
| 179 | - ->where($query->expr()->in('s.principaluri', $query->createParameter('principaluri'))) |
|
| 180 | - ->andWhere($query->expr()->eq('s.type', $query->createParameter('type'))) |
|
| 181 | - ->setParameter('type', 'addressbook') |
|
| 182 | - ->setParameter('principaluri', $principals, IQueryBuilder::PARAM_STR_ARRAY) |
|
| 183 | - ->execute(); |
|
| 184 | - |
|
| 185 | - $readOnlyPropertyName = '{' . \OCA\DAV\DAV\Sharing\Plugin::NS_OWNCLOUD . '}read-only'; |
|
| 186 | - while ($row = $result->fetch()) { |
|
| 187 | - if ($row['principaluri'] === $principalUri) { |
|
| 188 | - continue; |
|
| 189 | - } |
|
| 190 | - |
|
| 191 | - $readOnly = (int)$row['access'] === Backend::ACCESS_READ; |
|
| 192 | - if (isset($addressBooks[$row['id']])) { |
|
| 193 | - if ($readOnly) { |
|
| 194 | - // New share can not have more permissions then the old one. |
|
| 195 | - continue; |
|
| 196 | - } |
|
| 197 | - if (isset($addressBooks[$row['id']][$readOnlyPropertyName]) && |
|
| 198 | - $addressBooks[$row['id']][$readOnlyPropertyName] === 0) { |
|
| 199 | - // Old share is already read-write, no more permissions can be gained |
|
| 200 | - continue; |
|
| 201 | - } |
|
| 202 | - } |
|
| 203 | - |
|
| 204 | - [, $name] = \Sabre\Uri\split($row['principaluri']); |
|
| 205 | - $uri = $row['uri'] . '_shared_by_' . $name; |
|
| 206 | - $displayName = $row['displayname'] . ' (' . $this->getUserDisplayName($name) . ')'; |
|
| 207 | - |
|
| 208 | - $addressBooks[$row['id']] = [ |
|
| 209 | - 'id' => $row['id'], |
|
| 210 | - 'uri' => $uri, |
|
| 211 | - 'principaluri' => $principalUriOriginal, |
|
| 212 | - '{DAV:}displayname' => $displayName, |
|
| 213 | - '{' . Plugin::NS_CARDDAV . '}addressbook-description' => $row['description'], |
|
| 214 | - '{http://calendarserver.org/ns/}getctag' => $row['synctoken'], |
|
| 215 | - '{http://sabredav.org/ns}sync-token' => $row['synctoken'] ?: '0', |
|
| 216 | - '{' . \OCA\DAV\DAV\Sharing\Plugin::NS_OWNCLOUD . '}owner-principal' => $row['principaluri'], |
|
| 217 | - $readOnlyPropertyName => $readOnly, |
|
| 218 | - ]; |
|
| 219 | - |
|
| 220 | - $this->addOwnerPrincipal($addressBooks[$row['id']]); |
|
| 221 | - } |
|
| 222 | - $result->closeCursor(); |
|
| 223 | - |
|
| 224 | - return array_values($addressBooks); |
|
| 225 | - } |
|
| 226 | - |
|
| 227 | - public function getUsersOwnAddressBooks($principalUri) { |
|
| 228 | - $principalUri = $this->convertPrincipal($principalUri, true); |
|
| 229 | - $query = $this->db->getQueryBuilder(); |
|
| 230 | - $query->select(['id', 'uri', 'displayname', 'principaluri', 'description', 'synctoken']) |
|
| 231 | - ->from('addressbooks') |
|
| 232 | - ->where($query->expr()->eq('principaluri', $query->createNamedParameter($principalUri))); |
|
| 233 | - |
|
| 234 | - $addressBooks = []; |
|
| 235 | - |
|
| 236 | - $result = $query->execute(); |
|
| 237 | - while ($row = $result->fetch()) { |
|
| 238 | - $addressBooks[$row['id']] = [ |
|
| 239 | - 'id' => $row['id'], |
|
| 240 | - 'uri' => $row['uri'], |
|
| 241 | - 'principaluri' => $this->convertPrincipal($row['principaluri'], false), |
|
| 242 | - '{DAV:}displayname' => $row['displayname'], |
|
| 243 | - '{' . Plugin::NS_CARDDAV . '}addressbook-description' => $row['description'], |
|
| 244 | - '{http://calendarserver.org/ns/}getctag' => $row['synctoken'], |
|
| 245 | - '{http://sabredav.org/ns}sync-token' => $row['synctoken'] ?: '0', |
|
| 246 | - ]; |
|
| 247 | - |
|
| 248 | - $this->addOwnerPrincipal($addressBooks[$row['id']]); |
|
| 249 | - } |
|
| 250 | - $result->closeCursor(); |
|
| 251 | - |
|
| 252 | - return array_values($addressBooks); |
|
| 253 | - } |
|
| 254 | - |
|
| 255 | - private function getUserDisplayName($uid) { |
|
| 256 | - if (!isset($this->userDisplayNames[$uid])) { |
|
| 257 | - $user = $this->userManager->get($uid); |
|
| 258 | - |
|
| 259 | - if ($user instanceof IUser) { |
|
| 260 | - $this->userDisplayNames[$uid] = $user->getDisplayName(); |
|
| 261 | - } else { |
|
| 262 | - $this->userDisplayNames[$uid] = $uid; |
|
| 263 | - } |
|
| 264 | - } |
|
| 265 | - |
|
| 266 | - return $this->userDisplayNames[$uid]; |
|
| 267 | - } |
|
| 268 | - |
|
| 269 | - /** |
|
| 270 | - * @param int $addressBookId |
|
| 271 | - */ |
|
| 272 | - public function getAddressBookById(int $addressBookId): ?array { |
|
| 273 | - $query = $this->db->getQueryBuilder(); |
|
| 274 | - $result = $query->select(['id', 'uri', 'displayname', 'principaluri', 'description', 'synctoken']) |
|
| 275 | - ->from('addressbooks') |
|
| 276 | - ->where($query->expr()->eq('id', $query->createNamedParameter($addressBookId, IQueryBuilder::PARAM_INT))) |
|
| 277 | - ->executeQuery(); |
|
| 278 | - $row = $result->fetch(); |
|
| 279 | - $result->closeCursor(); |
|
| 280 | - if (!$row) { |
|
| 281 | - return null; |
|
| 282 | - } |
|
| 283 | - |
|
| 284 | - $addressBook = [ |
|
| 285 | - 'id' => $row['id'], |
|
| 286 | - 'uri' => $row['uri'], |
|
| 287 | - 'principaluri' => $row['principaluri'], |
|
| 288 | - '{DAV:}displayname' => $row['displayname'], |
|
| 289 | - '{' . Plugin::NS_CARDDAV . '}addressbook-description' => $row['description'], |
|
| 290 | - '{http://calendarserver.org/ns/}getctag' => $row['synctoken'], |
|
| 291 | - '{http://sabredav.org/ns}sync-token' => $row['synctoken'] ?: '0', |
|
| 292 | - ]; |
|
| 293 | - |
|
| 294 | - $this->addOwnerPrincipal($addressBook); |
|
| 295 | - |
|
| 296 | - return $addressBook; |
|
| 297 | - } |
|
| 298 | - |
|
| 299 | - public function getAddressBooksByUri(string $principal, string $addressBookUri): ?array { |
|
| 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 | - ->executeQuery(); |
|
| 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'] ?: '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 | - public function updateAddressBook($addressBookId, \Sabre\DAV\PropPatch $propPatch) { |
|
| 346 | - $supportedProperties = [ |
|
| 347 | - '{DAV:}displayname', |
|
| 348 | - '{' . Plugin::NS_CARDDAV . '}addressbook-description', |
|
| 349 | - ]; |
|
| 350 | - |
|
| 351 | - $propPatch->handle($supportedProperties, function ($mutations) use ($addressBookId) { |
|
| 352 | - $updates = []; |
|
| 353 | - foreach ($mutations as $property => $newValue) { |
|
| 354 | - switch ($property) { |
|
| 355 | - case '{DAV:}displayname': |
|
| 356 | - $updates['displayname'] = $newValue; |
|
| 357 | - break; |
|
| 358 | - case '{' . Plugin::NS_CARDDAV . '}addressbook-description': |
|
| 359 | - $updates['description'] = $newValue; |
|
| 360 | - break; |
|
| 361 | - } |
|
| 362 | - } |
|
| 363 | - $query = $this->db->getQueryBuilder(); |
|
| 364 | - $query->update('addressbooks'); |
|
| 365 | - |
|
| 366 | - foreach ($updates as $key => $value) { |
|
| 367 | - $query->set($key, $query->createNamedParameter($value)); |
|
| 368 | - } |
|
| 369 | - $query->where($query->expr()->eq('id', $query->createNamedParameter($addressBookId))) |
|
| 370 | - ->executeStatement(); |
|
| 371 | - |
|
| 372 | - $this->addChange($addressBookId, "", 2); |
|
| 373 | - |
|
| 374 | - $addressBookRow = $this->getAddressBookById((int)$addressBookId); |
|
| 375 | - $shares = $this->getShares((int)$addressBookId); |
|
| 376 | - $this->dispatcher->dispatchTyped(new AddressBookUpdatedEvent((int)$addressBookId, $addressBookRow, $shares, $mutations)); |
|
| 377 | - |
|
| 378 | - return true; |
|
| 379 | - }); |
|
| 380 | - } |
|
| 381 | - |
|
| 382 | - /** |
|
| 383 | - * Creates a new address book |
|
| 384 | - * |
|
| 385 | - * @param string $principalUri |
|
| 386 | - * @param string $url Just the 'basename' of the url. |
|
| 387 | - * @param array $properties |
|
| 388 | - * @return int |
|
| 389 | - * @throws BadRequest |
|
| 390 | - */ |
|
| 391 | - public function createAddressBook($principalUri, $url, array $properties) { |
|
| 392 | - if (strlen($url) > 255) { |
|
| 393 | - throw new BadRequest('URI too long. Address book not created'); |
|
| 394 | - } |
|
| 395 | - |
|
| 396 | - $values = [ |
|
| 397 | - 'displayname' => null, |
|
| 398 | - 'description' => null, |
|
| 399 | - 'principaluri' => $principalUri, |
|
| 400 | - 'uri' => $url, |
|
| 401 | - 'synctoken' => 1 |
|
| 402 | - ]; |
|
| 403 | - |
|
| 404 | - foreach ($properties as $property => $newValue) { |
|
| 405 | - switch ($property) { |
|
| 406 | - case '{DAV:}displayname': |
|
| 407 | - $values['displayname'] = $newValue; |
|
| 408 | - break; |
|
| 409 | - case '{' . Plugin::NS_CARDDAV . '}addressbook-description': |
|
| 410 | - $values['description'] = $newValue; |
|
| 411 | - break; |
|
| 412 | - default: |
|
| 413 | - throw new BadRequest('Unknown property: ' . $property); |
|
| 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 | - $addressBookId = $query->getLastInsertId(); |
|
| 436 | - $addressBookRow = $this->getAddressBookById($addressBookId); |
|
| 437 | - $this->dispatcher->dispatchTyped(new AddressBookCreatedEvent($addressBookId, $addressBookRow)); |
|
| 438 | - |
|
| 439 | - return $addressBookId; |
|
| 440 | - } |
|
| 441 | - |
|
| 442 | - /** |
|
| 443 | - * Deletes an entire addressbook and all its contents |
|
| 444 | - * |
|
| 445 | - * @param mixed $addressBookId |
|
| 446 | - * @return void |
|
| 447 | - */ |
|
| 448 | - public function deleteAddressBook($addressBookId) { |
|
| 449 | - $addressBookId = (int)$addressBookId; |
|
| 450 | - $addressBookData = $this->getAddressBookById($addressBookId); |
|
| 451 | - $shares = $this->getShares($addressBookId); |
|
| 452 | - |
|
| 453 | - $query = $this->db->getQueryBuilder(); |
|
| 454 | - $query->delete($this->dbCardsTable) |
|
| 455 | - ->where($query->expr()->eq('addressbookid', $query->createParameter('addressbookid'))) |
|
| 456 | - ->setParameter('addressbookid', $addressBookId, IQueryBuilder::PARAM_INT) |
|
| 457 | - ->executeStatement(); |
|
| 458 | - |
|
| 459 | - $query->delete('addressbookchanges') |
|
| 460 | - ->where($query->expr()->eq('addressbookid', $query->createParameter('addressbookid'))) |
|
| 461 | - ->setParameter('addressbookid', $addressBookId, IQueryBuilder::PARAM_INT) |
|
| 462 | - ->executeStatement(); |
|
| 463 | - |
|
| 464 | - $query->delete('addressbooks') |
|
| 465 | - ->where($query->expr()->eq('id', $query->createParameter('id'))) |
|
| 466 | - ->setParameter('id', $addressBookId, IQueryBuilder::PARAM_INT) |
|
| 467 | - ->executeStatement(); |
|
| 468 | - |
|
| 469 | - $this->sharingBackend->deleteAllShares($addressBookId); |
|
| 470 | - |
|
| 471 | - $query->delete($this->dbCardsPropertiesTable) |
|
| 472 | - ->where($query->expr()->eq('addressbookid', $query->createNamedParameter($addressBookId, IQueryBuilder::PARAM_INT))) |
|
| 473 | - ->executeStatement(); |
|
| 474 | - |
|
| 475 | - if ($addressBookData) { |
|
| 476 | - $this->dispatcher->dispatchTyped(new AddressBookDeletedEvent($addressBookId, $addressBookData, $shares)); |
|
| 477 | - } |
|
| 478 | - } |
|
| 479 | - |
|
| 480 | - /** |
|
| 481 | - * Returns all cards for a specific addressbook id. |
|
| 482 | - * |
|
| 483 | - * This method should return the following properties for each card: |
|
| 484 | - * * carddata - raw vcard data |
|
| 485 | - * * uri - Some unique url |
|
| 486 | - * * lastmodified - A unix timestamp |
|
| 487 | - * |
|
| 488 | - * It's recommended to also return the following properties: |
|
| 489 | - * * etag - A unique etag. This must change every time the card changes. |
|
| 490 | - * * size - The size of the card in bytes. |
|
| 491 | - * |
|
| 492 | - * If these last two properties are provided, less time will be spent |
|
| 493 | - * calculating them. If they are specified, you can also omit carddata. |
|
| 494 | - * This may speed up certain requests, especially with large cards. |
|
| 495 | - * |
|
| 496 | - * @param mixed $addressbookId |
|
| 497 | - * @return array |
|
| 498 | - */ |
|
| 499 | - public function getCards($addressbookId) { |
|
| 500 | - $query = $this->db->getQueryBuilder(); |
|
| 501 | - $query->select(['id', 'uri', 'lastmodified', 'etag', 'size', 'carddata', 'uid']) |
|
| 502 | - ->from($this->dbCardsTable) |
|
| 503 | - ->where($query->expr()->eq('addressbookid', $query->createNamedParameter($addressbookId))); |
|
| 504 | - |
|
| 505 | - $cards = []; |
|
| 506 | - |
|
| 507 | - $result = $query->execute(); |
|
| 508 | - while ($row = $result->fetch()) { |
|
| 509 | - $row['etag'] = '"' . $row['etag'] . '"'; |
|
| 510 | - |
|
| 511 | - $modified = false; |
|
| 512 | - $row['carddata'] = $this->readBlob($row['carddata'], $modified); |
|
| 513 | - if ($modified) { |
|
| 514 | - $row['size'] = strlen($row['carddata']); |
|
| 515 | - } |
|
| 516 | - |
|
| 517 | - $cards[] = $row; |
|
| 518 | - } |
|
| 519 | - $result->closeCursor(); |
|
| 520 | - |
|
| 521 | - return $cards; |
|
| 522 | - } |
|
| 523 | - |
|
| 524 | - /** |
|
| 525 | - * Returns a specific card. |
|
| 526 | - * |
|
| 527 | - * The same set of properties must be returned as with getCards. The only |
|
| 528 | - * exception is that 'carddata' is absolutely required. |
|
| 529 | - * |
|
| 530 | - * If the card does not exist, you must return false. |
|
| 531 | - * |
|
| 532 | - * @param mixed $addressBookId |
|
| 533 | - * @param string $cardUri |
|
| 534 | - * @return array |
|
| 535 | - */ |
|
| 536 | - public function getCard($addressBookId, $cardUri) { |
|
| 537 | - $query = $this->db->getQueryBuilder(); |
|
| 538 | - $query->select(['id', 'uri', 'lastmodified', 'etag', 'size', 'carddata', 'uid']) |
|
| 539 | - ->from($this->dbCardsTable) |
|
| 540 | - ->where($query->expr()->eq('addressbookid', $query->createNamedParameter($addressBookId))) |
|
| 541 | - ->andWhere($query->expr()->eq('uri', $query->createNamedParameter($cardUri))) |
|
| 542 | - ->setMaxResults(1); |
|
| 543 | - |
|
| 544 | - $result = $query->execute(); |
|
| 545 | - $row = $result->fetch(); |
|
| 546 | - if (!$row) { |
|
| 547 | - return false; |
|
| 548 | - } |
|
| 549 | - $row['etag'] = '"' . $row['etag'] . '"'; |
|
| 550 | - |
|
| 551 | - $modified = false; |
|
| 552 | - $row['carddata'] = $this->readBlob($row['carddata'], $modified); |
|
| 553 | - if ($modified) { |
|
| 554 | - $row['size'] = strlen($row['carddata']); |
|
| 555 | - } |
|
| 556 | - |
|
| 557 | - return $row; |
|
| 558 | - } |
|
| 559 | - |
|
| 560 | - /** |
|
| 561 | - * Returns a list of cards. |
|
| 562 | - * |
|
| 563 | - * This method should work identical to getCard, but instead return all the |
|
| 564 | - * cards in the list as an array. |
|
| 565 | - * |
|
| 566 | - * If the backend supports this, it may allow for some speed-ups. |
|
| 567 | - * |
|
| 568 | - * @param mixed $addressBookId |
|
| 569 | - * @param array $uris |
|
| 570 | - * @return array |
|
| 571 | - */ |
|
| 572 | - public function getMultipleCards($addressBookId, array $uris) { |
|
| 573 | - if (empty($uris)) { |
|
| 574 | - return []; |
|
| 575 | - } |
|
| 576 | - |
|
| 577 | - $chunks = array_chunk($uris, 100); |
|
| 578 | - $cards = []; |
|
| 579 | - |
|
| 580 | - $query = $this->db->getQueryBuilder(); |
|
| 581 | - $query->select(['id', 'uri', 'lastmodified', 'etag', 'size', 'carddata', 'uid']) |
|
| 582 | - ->from($this->dbCardsTable) |
|
| 583 | - ->where($query->expr()->eq('addressbookid', $query->createNamedParameter($addressBookId))) |
|
| 584 | - ->andWhere($query->expr()->in('uri', $query->createParameter('uri'))); |
|
| 585 | - |
|
| 586 | - foreach ($chunks as $uris) { |
|
| 587 | - $query->setParameter('uri', $uris, IQueryBuilder::PARAM_STR_ARRAY); |
|
| 588 | - $result = $query->execute(); |
|
| 589 | - |
|
| 590 | - while ($row = $result->fetch()) { |
|
| 591 | - $row['etag'] = '"' . $row['etag'] . '"'; |
|
| 592 | - |
|
| 593 | - $modified = false; |
|
| 594 | - $row['carddata'] = $this->readBlob($row['carddata'], $modified); |
|
| 595 | - if ($modified) { |
|
| 596 | - $row['size'] = strlen($row['carddata']); |
|
| 597 | - } |
|
| 598 | - |
|
| 599 | - $cards[] = $row; |
|
| 600 | - } |
|
| 601 | - $result->closeCursor(); |
|
| 602 | - } |
|
| 603 | - return $cards; |
|
| 604 | - } |
|
| 605 | - |
|
| 606 | - /** |
|
| 607 | - * Creates a new card. |
|
| 608 | - * |
|
| 609 | - * The addressbook id will be passed as the first argument. This is the |
|
| 610 | - * same id as it is returned from the getAddressBooksForUser method. |
|
| 611 | - * |
|
| 612 | - * The cardUri is a base uri, and doesn't include the full path. The |
|
| 613 | - * cardData argument is the vcard body, and is passed as a string. |
|
| 614 | - * |
|
| 615 | - * It is possible to return an ETag from this method. This ETag is for the |
|
| 616 | - * newly created resource, and must be enclosed with double quotes (that |
|
| 617 | - * is, the string itself must contain the double quotes). |
|
| 618 | - * |
|
| 619 | - * You should only return the ETag if you store the carddata as-is. If a |
|
| 620 | - * subsequent GET request on the same card does not have the same body, |
|
| 621 | - * byte-by-byte and you did return an ETag here, clients tend to get |
|
| 622 | - * confused. |
|
| 623 | - * |
|
| 624 | - * If you don't return an ETag, you can just return null. |
|
| 625 | - * |
|
| 626 | - * @param mixed $addressBookId |
|
| 627 | - * @param string $cardUri |
|
| 628 | - * @param string $cardData |
|
| 629 | - * @param bool $checkAlreadyExists |
|
| 630 | - * @return string |
|
| 631 | - */ |
|
| 632 | - public function createCard($addressBookId, $cardUri, $cardData, bool $checkAlreadyExists = true) { |
|
| 633 | - $etag = md5($cardData); |
|
| 634 | - $uid = $this->getUID($cardData); |
|
| 635 | - |
|
| 636 | - if ($checkAlreadyExists) { |
|
| 637 | - $q = $this->db->getQueryBuilder(); |
|
| 638 | - $q->select('uid') |
|
| 639 | - ->from($this->dbCardsTable) |
|
| 640 | - ->where($q->expr()->eq('addressbookid', $q->createNamedParameter($addressBookId))) |
|
| 641 | - ->andWhere($q->expr()->eq('uid', $q->createNamedParameter($uid))) |
|
| 642 | - ->setMaxResults(1); |
|
| 643 | - $result = $q->executeQuery(); |
|
| 644 | - $count = (bool)$result->fetchOne(); |
|
| 645 | - $result->closeCursor(); |
|
| 646 | - if ($count) { |
|
| 647 | - throw new \Sabre\DAV\Exception\BadRequest('VCard object with uid already exists in this addressbook collection.'); |
|
| 648 | - } |
|
| 649 | - } |
|
| 650 | - |
|
| 651 | - $query = $this->db->getQueryBuilder(); |
|
| 652 | - $query->insert('cards') |
|
| 653 | - ->values([ |
|
| 654 | - 'carddata' => $query->createNamedParameter($cardData, IQueryBuilder::PARAM_LOB), |
|
| 655 | - 'uri' => $query->createNamedParameter($cardUri), |
|
| 656 | - 'lastmodified' => $query->createNamedParameter(time()), |
|
| 657 | - 'addressbookid' => $query->createNamedParameter($addressBookId), |
|
| 658 | - 'size' => $query->createNamedParameter(strlen($cardData)), |
|
| 659 | - 'etag' => $query->createNamedParameter($etag), |
|
| 660 | - 'uid' => $query->createNamedParameter($uid), |
|
| 661 | - ]) |
|
| 662 | - ->execute(); |
|
| 663 | - |
|
| 664 | - $etagCacheKey = "$addressBookId#$cardUri"; |
|
| 665 | - $this->etagCache[$etagCacheKey] = $etag; |
|
| 666 | - |
|
| 667 | - $this->addChange($addressBookId, $cardUri, 1); |
|
| 668 | - $this->updateProperties($addressBookId, $cardUri, $cardData); |
|
| 669 | - |
|
| 670 | - $addressBookData = $this->getAddressBookById($addressBookId); |
|
| 671 | - $shares = $this->getShares($addressBookId); |
|
| 672 | - $objectRow = $this->getCard($addressBookId, $cardUri); |
|
| 673 | - $this->dispatcher->dispatchTyped(new CardCreatedEvent($addressBookId, $addressBookData, $shares, $objectRow)); |
|
| 674 | - |
|
| 675 | - return '"' . $etag . '"'; |
|
| 676 | - } |
|
| 677 | - |
|
| 678 | - /** |
|
| 679 | - * Updates a card. |
|
| 680 | - * |
|
| 681 | - * The addressbook id will be passed as the first argument. This is the |
|
| 682 | - * same id as it is returned from the getAddressBooksForUser method. |
|
| 683 | - * |
|
| 684 | - * The cardUri is a base uri, and doesn't include the full path. The |
|
| 685 | - * cardData argument is the vcard body, and is passed as a string. |
|
| 686 | - * |
|
| 687 | - * It is possible to return an ETag from this method. This ETag should |
|
| 688 | - * match that of the updated resource, and must be enclosed with double |
|
| 689 | - * quotes (that is: the string itself must contain the actual quotes). |
|
| 690 | - * |
|
| 691 | - * You should only return the ETag if you store the carddata as-is. If a |
|
| 692 | - * subsequent GET request on the same card does not have the same body, |
|
| 693 | - * byte-by-byte and you did return an ETag here, clients tend to get |
|
| 694 | - * confused. |
|
| 695 | - * |
|
| 696 | - * If you don't return an ETag, you can just return null. |
|
| 697 | - * |
|
| 698 | - * @param mixed $addressBookId |
|
| 699 | - * @param string $cardUri |
|
| 700 | - * @param string $cardData |
|
| 701 | - * @return string |
|
| 702 | - */ |
|
| 703 | - public function updateCard($addressBookId, $cardUri, $cardData) { |
|
| 704 | - $uid = $this->getUID($cardData); |
|
| 705 | - $etag = md5($cardData); |
|
| 706 | - $query = $this->db->getQueryBuilder(); |
|
| 707 | - |
|
| 708 | - // check for recently stored etag and stop if it is the same |
|
| 709 | - $etagCacheKey = "$addressBookId#$cardUri"; |
|
| 710 | - if (isset($this->etagCache[$etagCacheKey]) && $this->etagCache[$etagCacheKey] === $etag) { |
|
| 711 | - return '"' . $etag . '"'; |
|
| 712 | - } |
|
| 713 | - |
|
| 714 | - $query->update($this->dbCardsTable) |
|
| 715 | - ->set('carddata', $query->createNamedParameter($cardData, IQueryBuilder::PARAM_LOB)) |
|
| 716 | - ->set('lastmodified', $query->createNamedParameter(time())) |
|
| 717 | - ->set('size', $query->createNamedParameter(strlen($cardData))) |
|
| 718 | - ->set('etag', $query->createNamedParameter($etag)) |
|
| 719 | - ->set('uid', $query->createNamedParameter($uid)) |
|
| 720 | - ->where($query->expr()->eq('uri', $query->createNamedParameter($cardUri))) |
|
| 721 | - ->andWhere($query->expr()->eq('addressbookid', $query->createNamedParameter($addressBookId))) |
|
| 722 | - ->execute(); |
|
| 723 | - |
|
| 724 | - $this->etagCache[$etagCacheKey] = $etag; |
|
| 725 | - |
|
| 726 | - $this->addChange($addressBookId, $cardUri, 2); |
|
| 727 | - $this->updateProperties($addressBookId, $cardUri, $cardData); |
|
| 728 | - |
|
| 729 | - $addressBookData = $this->getAddressBookById($addressBookId); |
|
| 730 | - $shares = $this->getShares($addressBookId); |
|
| 731 | - $objectRow = $this->getCard($addressBookId, $cardUri); |
|
| 732 | - $this->dispatcher->dispatchTyped(new CardUpdatedEvent($addressBookId, $addressBookData, $shares, $objectRow)); |
|
| 733 | - return '"' . $etag . '"'; |
|
| 734 | - } |
|
| 735 | - |
|
| 736 | - /** |
|
| 737 | - * Deletes a card |
|
| 738 | - * |
|
| 739 | - * @param mixed $addressBookId |
|
| 740 | - * @param string $cardUri |
|
| 741 | - * @return bool |
|
| 742 | - */ |
|
| 743 | - public function deleteCard($addressBookId, $cardUri) { |
|
| 744 | - $addressBookData = $this->getAddressBookById($addressBookId); |
|
| 745 | - $shares = $this->getShares($addressBookId); |
|
| 746 | - $objectRow = $this->getCard($addressBookId, $cardUri); |
|
| 747 | - |
|
| 748 | - try { |
|
| 749 | - $cardId = $this->getCardId($addressBookId, $cardUri); |
|
| 750 | - } catch (\InvalidArgumentException $e) { |
|
| 751 | - $cardId = null; |
|
| 752 | - } |
|
| 753 | - $query = $this->db->getQueryBuilder(); |
|
| 754 | - $ret = $query->delete($this->dbCardsTable) |
|
| 755 | - ->where($query->expr()->eq('addressbookid', $query->createNamedParameter($addressBookId))) |
|
| 756 | - ->andWhere($query->expr()->eq('uri', $query->createNamedParameter($cardUri))) |
|
| 757 | - ->executeStatement(); |
|
| 758 | - |
|
| 759 | - $this->addChange($addressBookId, $cardUri, 3); |
|
| 760 | - |
|
| 761 | - if ($ret === 1) { |
|
| 762 | - if ($cardId !== null) { |
|
| 763 | - $this->dispatcher->dispatchTyped(new CardDeletedEvent($addressBookId, $addressBookData, $shares, $objectRow)); |
|
| 764 | - $this->purgeProperties($addressBookId, $cardId); |
|
| 765 | - } |
|
| 766 | - return true; |
|
| 767 | - } |
|
| 768 | - |
|
| 769 | - return false; |
|
| 770 | - } |
|
| 771 | - |
|
| 772 | - /** |
|
| 773 | - * The getChanges method returns all the changes that have happened, since |
|
| 774 | - * the specified syncToken in the specified address book. |
|
| 775 | - * |
|
| 776 | - * This function should return an array, such as the following: |
|
| 777 | - * |
|
| 778 | - * [ |
|
| 779 | - * 'syncToken' => 'The current synctoken', |
|
| 780 | - * 'added' => [ |
|
| 781 | - * 'new.txt', |
|
| 782 | - * ], |
|
| 783 | - * 'modified' => [ |
|
| 784 | - * 'modified.txt', |
|
| 785 | - * ], |
|
| 786 | - * 'deleted' => [ |
|
| 787 | - * 'foo.php.bak', |
|
| 788 | - * 'old.txt' |
|
| 789 | - * ] |
|
| 790 | - * ]; |
|
| 791 | - * |
|
| 792 | - * The returned syncToken property should reflect the *current* syncToken |
|
| 793 | - * of the calendar, as reported in the {http://sabredav.org/ns}sync-token |
|
| 794 | - * property. This is needed here too, to ensure the operation is atomic. |
|
| 795 | - * |
|
| 796 | - * If the $syncToken argument is specified as null, this is an initial |
|
| 797 | - * sync, and all members should be reported. |
|
| 798 | - * |
|
| 799 | - * The modified property is an array of nodenames that have changed since |
|
| 800 | - * the last token. |
|
| 801 | - * |
|
| 802 | - * The deleted property is an array with nodenames, that have been deleted |
|
| 803 | - * from collection. |
|
| 804 | - * |
|
| 805 | - * The $syncLevel argument is basically the 'depth' of the report. If it's |
|
| 806 | - * 1, you only have to report changes that happened only directly in |
|
| 807 | - * immediate descendants. If it's 2, it should also include changes from |
|
| 808 | - * the nodes below the child collections. (grandchildren) |
|
| 809 | - * |
|
| 810 | - * The $limit argument allows a client to specify how many results should |
|
| 811 | - * be returned at most. If the limit is not specified, it should be treated |
|
| 812 | - * as infinite. |
|
| 813 | - * |
|
| 814 | - * If the limit (infinite or not) is higher than you're willing to return, |
|
| 815 | - * you should throw a Sabre\DAV\Exception\TooMuchMatches() exception. |
|
| 816 | - * |
|
| 817 | - * If the syncToken is expired (due to data cleanup) or unknown, you must |
|
| 818 | - * return null. |
|
| 819 | - * |
|
| 820 | - * The limit is 'suggestive'. You are free to ignore it. |
|
| 821 | - * |
|
| 822 | - * @param string $addressBookId |
|
| 823 | - * @param string $syncToken |
|
| 824 | - * @param int $syncLevel |
|
| 825 | - * @param int|null $limit |
|
| 826 | - * @return array |
|
| 827 | - */ |
|
| 828 | - public function getChangesForAddressBook($addressBookId, $syncToken, $syncLevel, $limit = null) { |
|
| 829 | - // Current synctoken |
|
| 830 | - $qb = $this->db->getQueryBuilder(); |
|
| 831 | - $qb->select('synctoken') |
|
| 832 | - ->from('addressbooks') |
|
| 833 | - ->where( |
|
| 834 | - $qb->expr()->eq('id', $qb->createNamedParameter($addressBookId)) |
|
| 835 | - ); |
|
| 836 | - $stmt = $qb->executeQuery(); |
|
| 837 | - $currentToken = $stmt->fetchOne(); |
|
| 838 | - $stmt->closeCursor(); |
|
| 839 | - |
|
| 840 | - if (is_null($currentToken)) { |
|
| 841 | - return []; |
|
| 842 | - } |
|
| 843 | - |
|
| 844 | - $result = [ |
|
| 845 | - 'syncToken' => $currentToken, |
|
| 846 | - 'added' => [], |
|
| 847 | - 'modified' => [], |
|
| 848 | - 'deleted' => [], |
|
| 849 | - ]; |
|
| 850 | - |
|
| 851 | - if ($syncToken) { |
|
| 852 | - $qb = $this->db->getQueryBuilder(); |
|
| 853 | - $qb->select('uri', 'operation') |
|
| 854 | - ->from('addressbookchanges') |
|
| 855 | - ->where( |
|
| 856 | - $qb->expr()->andX( |
|
| 857 | - $qb->expr()->gte('synctoken', $qb->createNamedParameter($syncToken)), |
|
| 858 | - $qb->expr()->lt('synctoken', $qb->createNamedParameter($currentToken)), |
|
| 859 | - $qb->expr()->eq('addressbookid', $qb->createNamedParameter($addressBookId)) |
|
| 860 | - ) |
|
| 861 | - )->orderBy('synctoken'); |
|
| 862 | - |
|
| 863 | - if (is_int($limit) && $limit > 0) { |
|
| 864 | - $qb->setMaxResults($limit); |
|
| 865 | - } |
|
| 866 | - |
|
| 867 | - // Fetching all changes |
|
| 868 | - $stmt = $qb->executeQuery(); |
|
| 869 | - |
|
| 870 | - $changes = []; |
|
| 871 | - |
|
| 872 | - // This loop ensures that any duplicates are overwritten, only the |
|
| 873 | - // last change on a node is relevant. |
|
| 874 | - while ($row = $stmt->fetch(\PDO::FETCH_ASSOC)) { |
|
| 875 | - $changes[$row['uri']] = $row['operation']; |
|
| 876 | - } |
|
| 877 | - $stmt->closeCursor(); |
|
| 878 | - |
|
| 879 | - foreach ($changes as $uri => $operation) { |
|
| 880 | - switch ($operation) { |
|
| 881 | - case 1: |
|
| 882 | - $result['added'][] = $uri; |
|
| 883 | - break; |
|
| 884 | - case 2: |
|
| 885 | - $result['modified'][] = $uri; |
|
| 886 | - break; |
|
| 887 | - case 3: |
|
| 888 | - $result['deleted'][] = $uri; |
|
| 889 | - break; |
|
| 890 | - } |
|
| 891 | - } |
|
| 892 | - } else { |
|
| 893 | - $qb = $this->db->getQueryBuilder(); |
|
| 894 | - $qb->select('uri') |
|
| 895 | - ->from('cards') |
|
| 896 | - ->where( |
|
| 897 | - $qb->expr()->eq('addressbookid', $qb->createNamedParameter($addressBookId)) |
|
| 898 | - ); |
|
| 899 | - // No synctoken supplied, this is the initial sync. |
|
| 900 | - $stmt = $qb->executeQuery(); |
|
| 901 | - $result['added'] = $stmt->fetchAll(\PDO::FETCH_COLUMN); |
|
| 902 | - $stmt->closeCursor(); |
|
| 903 | - } |
|
| 904 | - return $result; |
|
| 905 | - } |
|
| 906 | - |
|
| 907 | - /** |
|
| 908 | - * Adds a change record to the addressbookchanges table. |
|
| 909 | - * |
|
| 910 | - * @param mixed $addressBookId |
|
| 911 | - * @param string $objectUri |
|
| 912 | - * @param int $operation 1 = add, 2 = modify, 3 = delete |
|
| 913 | - * @return void |
|
| 914 | - */ |
|
| 915 | - protected function addChange($addressBookId, $objectUri, $operation) { |
|
| 916 | - $sql = 'INSERT INTO `*PREFIX*addressbookchanges`(`uri`, `synctoken`, `addressbookid`, `operation`) SELECT ?, `synctoken`, ?, ? FROM `*PREFIX*addressbooks` WHERE `id` = ?'; |
|
| 917 | - $stmt = $this->db->prepare($sql); |
|
| 918 | - $stmt->execute([ |
|
| 919 | - $objectUri, |
|
| 920 | - $addressBookId, |
|
| 921 | - $operation, |
|
| 922 | - $addressBookId |
|
| 923 | - ]); |
|
| 924 | - $stmt = $this->db->prepare('UPDATE `*PREFIX*addressbooks` SET `synctoken` = `synctoken` + 1 WHERE `id` = ?'); |
|
| 925 | - $stmt->execute([ |
|
| 926 | - $addressBookId |
|
| 927 | - ]); |
|
| 928 | - } |
|
| 929 | - |
|
| 930 | - /** |
|
| 931 | - * @param resource|string $cardData |
|
| 932 | - * @param bool $modified |
|
| 933 | - * @return string |
|
| 934 | - */ |
|
| 935 | - private function readBlob($cardData, &$modified = false) { |
|
| 936 | - if (is_resource($cardData)) { |
|
| 937 | - $cardData = stream_get_contents($cardData); |
|
| 938 | - } |
|
| 939 | - |
|
| 940 | - // Micro optimisation |
|
| 941 | - // don't loop through |
|
| 942 | - if (strpos($cardData, 'PHOTO:data:') === 0) { |
|
| 943 | - return $cardData; |
|
| 944 | - } |
|
| 945 | - |
|
| 946 | - $cardDataArray = explode("\r\n", $cardData); |
|
| 947 | - |
|
| 948 | - $cardDataFiltered = []; |
|
| 949 | - $removingPhoto = false; |
|
| 950 | - foreach ($cardDataArray as $line) { |
|
| 951 | - if (strpos($line, 'PHOTO:data:') === 0 |
|
| 952 | - && strpos($line, 'PHOTO:data:image/') !== 0) { |
|
| 953 | - // Filter out PHOTO data of non-images |
|
| 954 | - $removingPhoto = true; |
|
| 955 | - $modified = true; |
|
| 956 | - continue; |
|
| 957 | - } |
|
| 958 | - |
|
| 959 | - if ($removingPhoto) { |
|
| 960 | - if (strpos($line, ' ') === 0) { |
|
| 961 | - continue; |
|
| 962 | - } |
|
| 963 | - // No leading space means this is a new property |
|
| 964 | - $removingPhoto = false; |
|
| 965 | - } |
|
| 966 | - |
|
| 967 | - $cardDataFiltered[] = $line; |
|
| 968 | - } |
|
| 969 | - return implode("\r\n", $cardDataFiltered); |
|
| 970 | - } |
|
| 971 | - |
|
| 972 | - /** |
|
| 973 | - * @param list<array{href: string, commonName: string, readOnly: bool}> $add |
|
| 974 | - * @param list<string> $remove |
|
| 975 | - */ |
|
| 976 | - public function updateShares(IShareable $shareable, array $add, array $remove): void { |
|
| 977 | - $addressBookId = $shareable->getResourceId(); |
|
| 978 | - $addressBookData = $this->getAddressBookById($addressBookId); |
|
| 979 | - $oldShares = $this->getShares($addressBookId); |
|
| 980 | - |
|
| 981 | - $this->sharingBackend->updateShares($shareable, $add, $remove); |
|
| 982 | - |
|
| 983 | - $this->dispatcher->dispatchTyped(new AddressBookShareUpdatedEvent($addressBookId, $addressBookData, $oldShares, $add, $remove)); |
|
| 984 | - } |
|
| 985 | - |
|
| 986 | - /** |
|
| 987 | - * Search contacts in a specific address-book |
|
| 988 | - * |
|
| 989 | - * @param int $addressBookId |
|
| 990 | - * @param string $pattern which should match within the $searchProperties |
|
| 991 | - * @param array $searchProperties defines the properties within the query pattern should match |
|
| 992 | - * @param array $options = array() to define the search behavior |
|
| 993 | - * - 'escape_like_param' - If set to false wildcards _ and % are not escaped, otherwise they are |
|
| 994 | - * - 'limit' - Set a numeric limit for the search results |
|
| 995 | - * - 'offset' - Set the offset for the limited search results |
|
| 996 | - * - 'wildcard' - Whether the search should use wildcards |
|
| 997 | - * @psalm-param array{escape_like_param?: bool, limit?: int, offset?: int, wildcard?: bool} $options |
|
| 998 | - * @return array an array of contacts which are arrays of key-value-pairs |
|
| 999 | - */ |
|
| 1000 | - public function search($addressBookId, $pattern, $searchProperties, $options = []): array { |
|
| 1001 | - return $this->searchByAddressBookIds([$addressBookId], $pattern, $searchProperties, $options); |
|
| 1002 | - } |
|
| 1003 | - |
|
| 1004 | - /** |
|
| 1005 | - * Search contacts in all address-books accessible by a user |
|
| 1006 | - * |
|
| 1007 | - * @param string $principalUri |
|
| 1008 | - * @param string $pattern |
|
| 1009 | - * @param array $searchProperties |
|
| 1010 | - * @param array $options |
|
| 1011 | - * @return array |
|
| 1012 | - */ |
|
| 1013 | - public function searchPrincipalUri(string $principalUri, |
|
| 1014 | - string $pattern, |
|
| 1015 | - array $searchProperties, |
|
| 1016 | - array $options = []): array { |
|
| 1017 | - $addressBookIds = array_map(static function ($row):int { |
|
| 1018 | - return (int) $row['id']; |
|
| 1019 | - }, $this->getAddressBooksForUser($principalUri)); |
|
| 1020 | - |
|
| 1021 | - return $this->searchByAddressBookIds($addressBookIds, $pattern, $searchProperties, $options); |
|
| 1022 | - } |
|
| 1023 | - |
|
| 1024 | - /** |
|
| 1025 | - * @param array $addressBookIds |
|
| 1026 | - * @param string $pattern |
|
| 1027 | - * @param array $searchProperties |
|
| 1028 | - * @param array $options |
|
| 1029 | - * @psalm-param array{types?: bool, escape_like_param?: bool, limit?: int, offset?: int, wildcard?: bool} $options |
|
| 1030 | - * @return array |
|
| 1031 | - */ |
|
| 1032 | - private function searchByAddressBookIds(array $addressBookIds, |
|
| 1033 | - string $pattern, |
|
| 1034 | - array $searchProperties, |
|
| 1035 | - array $options = []): array { |
|
| 1036 | - $escapePattern = !\array_key_exists('escape_like_param', $options) || $options['escape_like_param'] !== false; |
|
| 1037 | - $useWildcards = !\array_key_exists('wildcard', $options) || $options['wildcard'] !== false; |
|
| 1038 | - |
|
| 1039 | - $query2 = $this->db->getQueryBuilder(); |
|
| 1040 | - |
|
| 1041 | - $addressBookOr = $query2->expr()->orX(); |
|
| 1042 | - foreach ($addressBookIds as $addressBookId) { |
|
| 1043 | - $addressBookOr->add($query2->expr()->eq('cp.addressbookid', $query2->createNamedParameter($addressBookId))); |
|
| 1044 | - } |
|
| 1045 | - |
|
| 1046 | - if ($addressBookOr->count() === 0) { |
|
| 1047 | - return []; |
|
| 1048 | - } |
|
| 1049 | - |
|
| 1050 | - $propertyOr = $query2->expr()->orX(); |
|
| 1051 | - foreach ($searchProperties as $property) { |
|
| 1052 | - if ($escapePattern) { |
|
| 1053 | - if ($property === 'EMAIL' && strpos($pattern, ' ') !== false) { |
|
| 1054 | - // There can be no spaces in emails |
|
| 1055 | - continue; |
|
| 1056 | - } |
|
| 1057 | - |
|
| 1058 | - if ($property === 'CLOUD' && preg_match('/[^a-zA-Z0-9 :_.@\/\-\']/', $pattern) === 1) { |
|
| 1059 | - // There can be no chars in cloud ids which are not valid for user ids plus :/ |
|
| 1060 | - // worst case: CA61590A-BBBC-423E-84AF-E6DF01455A53@https://my.nxt/srv/ |
|
| 1061 | - continue; |
|
| 1062 | - } |
|
| 1063 | - } |
|
| 1064 | - |
|
| 1065 | - $propertyOr->add($query2->expr()->eq('cp.name', $query2->createNamedParameter($property))); |
|
| 1066 | - } |
|
| 1067 | - |
|
| 1068 | - if ($propertyOr->count() === 0) { |
|
| 1069 | - return []; |
|
| 1070 | - } |
|
| 1071 | - |
|
| 1072 | - $query2->selectDistinct('cp.cardid') |
|
| 1073 | - ->from($this->dbCardsPropertiesTable, 'cp') |
|
| 1074 | - ->andWhere($addressBookOr) |
|
| 1075 | - ->andWhere($propertyOr); |
|
| 1076 | - |
|
| 1077 | - // No need for like when the pattern is empty |
|
| 1078 | - if ('' !== $pattern) { |
|
| 1079 | - if (!$useWildcards) { |
|
| 1080 | - $query2->andWhere($query2->expr()->eq('cp.value', $query2->createNamedParameter($pattern))); |
|
| 1081 | - } elseif (!$escapePattern) { |
|
| 1082 | - $query2->andWhere($query2->expr()->ilike('cp.value', $query2->createNamedParameter($pattern))); |
|
| 1083 | - } else { |
|
| 1084 | - $query2->andWhere($query2->expr()->ilike('cp.value', $query2->createNamedParameter('%' . $this->db->escapeLikeParameter($pattern) . '%'))); |
|
| 1085 | - } |
|
| 1086 | - } |
|
| 1087 | - |
|
| 1088 | - if (isset($options['limit'])) { |
|
| 1089 | - $query2->setMaxResults($options['limit']); |
|
| 1090 | - } |
|
| 1091 | - if (isset($options['offset'])) { |
|
| 1092 | - $query2->setFirstResult($options['offset']); |
|
| 1093 | - } |
|
| 1094 | - |
|
| 1095 | - $result = $query2->execute(); |
|
| 1096 | - $matches = $result->fetchAll(); |
|
| 1097 | - $result->closeCursor(); |
|
| 1098 | - $matches = array_map(function ($match) { |
|
| 1099 | - return (int)$match['cardid']; |
|
| 1100 | - }, $matches); |
|
| 1101 | - |
|
| 1102 | - $cards = []; |
|
| 1103 | - $query = $this->db->getQueryBuilder(); |
|
| 1104 | - $query->select('c.addressbookid', 'c.carddata', 'c.uri') |
|
| 1105 | - ->from($this->dbCardsTable, 'c') |
|
| 1106 | - ->where($query->expr()->in('c.id', $query->createParameter('matches'))); |
|
| 1107 | - |
|
| 1108 | - foreach (array_chunk($matches, 1000) as $matchesChunk) { |
|
| 1109 | - $query->setParameter('matches', $matchesChunk, IQueryBuilder::PARAM_INT_ARRAY); |
|
| 1110 | - $result = $query->executeQuery(); |
|
| 1111 | - $cards = array_merge($cards, $result->fetchAll()); |
|
| 1112 | - $result->closeCursor(); |
|
| 1113 | - } |
|
| 1114 | - |
|
| 1115 | - return array_map(function ($array) { |
|
| 1116 | - $array['addressbookid'] = (int) $array['addressbookid']; |
|
| 1117 | - $modified = false; |
|
| 1118 | - $array['carddata'] = $this->readBlob($array['carddata'], $modified); |
|
| 1119 | - if ($modified) { |
|
| 1120 | - $array['size'] = strlen($array['carddata']); |
|
| 1121 | - } |
|
| 1122 | - return $array; |
|
| 1123 | - }, $cards); |
|
| 1124 | - } |
|
| 1125 | - |
|
| 1126 | - /** |
|
| 1127 | - * @param int $bookId |
|
| 1128 | - * @param string $name |
|
| 1129 | - * @return array |
|
| 1130 | - */ |
|
| 1131 | - public function collectCardProperties($bookId, $name) { |
|
| 1132 | - $query = $this->db->getQueryBuilder(); |
|
| 1133 | - $result = $query->selectDistinct('value') |
|
| 1134 | - ->from($this->dbCardsPropertiesTable) |
|
| 1135 | - ->where($query->expr()->eq('name', $query->createNamedParameter($name))) |
|
| 1136 | - ->andWhere($query->expr()->eq('addressbookid', $query->createNamedParameter($bookId))) |
|
| 1137 | - ->execute(); |
|
| 1138 | - |
|
| 1139 | - $all = $result->fetchAll(PDO::FETCH_COLUMN); |
|
| 1140 | - $result->closeCursor(); |
|
| 1141 | - |
|
| 1142 | - return $all; |
|
| 1143 | - } |
|
| 1144 | - |
|
| 1145 | - /** |
|
| 1146 | - * get URI from a given contact |
|
| 1147 | - * |
|
| 1148 | - * @param int $id |
|
| 1149 | - * @return string |
|
| 1150 | - */ |
|
| 1151 | - public function getCardUri($id) { |
|
| 1152 | - $query = $this->db->getQueryBuilder(); |
|
| 1153 | - $query->select('uri')->from($this->dbCardsTable) |
|
| 1154 | - ->where($query->expr()->eq('id', $query->createParameter('id'))) |
|
| 1155 | - ->setParameter('id', $id); |
|
| 1156 | - |
|
| 1157 | - $result = $query->execute(); |
|
| 1158 | - $uri = $result->fetch(); |
|
| 1159 | - $result->closeCursor(); |
|
| 1160 | - |
|
| 1161 | - if (!isset($uri['uri'])) { |
|
| 1162 | - throw new \InvalidArgumentException('Card does not exists: ' . $id); |
|
| 1163 | - } |
|
| 1164 | - |
|
| 1165 | - return $uri['uri']; |
|
| 1166 | - } |
|
| 1167 | - |
|
| 1168 | - /** |
|
| 1169 | - * return contact with the given URI |
|
| 1170 | - * |
|
| 1171 | - * @param int $addressBookId |
|
| 1172 | - * @param string $uri |
|
| 1173 | - * @returns array |
|
| 1174 | - */ |
|
| 1175 | - public function getContact($addressBookId, $uri) { |
|
| 1176 | - $result = []; |
|
| 1177 | - $query = $this->db->getQueryBuilder(); |
|
| 1178 | - $query->select('*')->from($this->dbCardsTable) |
|
| 1179 | - ->where($query->expr()->eq('uri', $query->createNamedParameter($uri))) |
|
| 1180 | - ->andWhere($query->expr()->eq('addressbookid', $query->createNamedParameter($addressBookId))); |
|
| 1181 | - $queryResult = $query->execute(); |
|
| 1182 | - $contact = $queryResult->fetch(); |
|
| 1183 | - $queryResult->closeCursor(); |
|
| 1184 | - |
|
| 1185 | - if (is_array($contact)) { |
|
| 1186 | - $modified = false; |
|
| 1187 | - $contact['etag'] = '"' . $contact['etag'] . '"'; |
|
| 1188 | - $contact['carddata'] = $this->readBlob($contact['carddata'], $modified); |
|
| 1189 | - if ($modified) { |
|
| 1190 | - $contact['size'] = strlen($contact['carddata']); |
|
| 1191 | - } |
|
| 1192 | - |
|
| 1193 | - $result = $contact; |
|
| 1194 | - } |
|
| 1195 | - |
|
| 1196 | - return $result; |
|
| 1197 | - } |
|
| 1198 | - |
|
| 1199 | - /** |
|
| 1200 | - * Returns the list of people whom this address book is shared with. |
|
| 1201 | - * |
|
| 1202 | - * Every element in this array should have the following properties: |
|
| 1203 | - * * href - Often a mailto: address |
|
| 1204 | - * * commonName - Optional, for example a first + last name |
|
| 1205 | - * * status - See the Sabre\CalDAV\SharingPlugin::STATUS_ constants. |
|
| 1206 | - * * readOnly - boolean |
|
| 1207 | - * |
|
| 1208 | - * @return list<array{href: string, commonName: string, status: int, readOnly: bool, '{http://owncloud.org/ns}principal': string, '{http://owncloud.org/ns}group-share': bool}> |
|
| 1209 | - */ |
|
| 1210 | - public function getShares(int $addressBookId): array { |
|
| 1211 | - return $this->sharingBackend->getShares($addressBookId); |
|
| 1212 | - } |
|
| 1213 | - |
|
| 1214 | - /** |
|
| 1215 | - * update properties table |
|
| 1216 | - * |
|
| 1217 | - * @param int $addressBookId |
|
| 1218 | - * @param string $cardUri |
|
| 1219 | - * @param string $vCardSerialized |
|
| 1220 | - */ |
|
| 1221 | - protected function updateProperties($addressBookId, $cardUri, $vCardSerialized) { |
|
| 1222 | - $cardId = $this->getCardId($addressBookId, $cardUri); |
|
| 1223 | - $vCard = $this->readCard($vCardSerialized); |
|
| 1224 | - |
|
| 1225 | - $this->purgeProperties($addressBookId, $cardId); |
|
| 1226 | - |
|
| 1227 | - $query = $this->db->getQueryBuilder(); |
|
| 1228 | - $query->insert($this->dbCardsPropertiesTable) |
|
| 1229 | - ->values( |
|
| 1230 | - [ |
|
| 1231 | - 'addressbookid' => $query->createNamedParameter($addressBookId), |
|
| 1232 | - 'cardid' => $query->createNamedParameter($cardId), |
|
| 1233 | - 'name' => $query->createParameter('name'), |
|
| 1234 | - 'value' => $query->createParameter('value'), |
|
| 1235 | - 'preferred' => $query->createParameter('preferred') |
|
| 1236 | - ] |
|
| 1237 | - ); |
|
| 1238 | - |
|
| 1239 | - |
|
| 1240 | - $this->db->beginTransaction(); |
|
| 1241 | - |
|
| 1242 | - try { |
|
| 1243 | - foreach ($vCard->children() as $property) { |
|
| 1244 | - if (!in_array($property->name, self::$indexProperties)) { |
|
| 1245 | - continue; |
|
| 1246 | - } |
|
| 1247 | - $preferred = 0; |
|
| 1248 | - foreach ($property->parameters as $parameter) { |
|
| 1249 | - if ($parameter->name === 'TYPE' && strtoupper($parameter->getValue()) === 'PREF') { |
|
| 1250 | - $preferred = 1; |
|
| 1251 | - break; |
|
| 1252 | - } |
|
| 1253 | - } |
|
| 1254 | - $query->setParameter('name', $property->name); |
|
| 1255 | - $query->setParameter('value', mb_strcut($property->getValue(), 0, 254)); |
|
| 1256 | - $query->setParameter('preferred', $preferred); |
|
| 1257 | - $query->execute(); |
|
| 1258 | - } |
|
| 1259 | - $this->db->commit(); |
|
| 1260 | - } catch (\Exception $e) { |
|
| 1261 | - $this->db->rollBack(); |
|
| 1262 | - } |
|
| 1263 | - } |
|
| 1264 | - |
|
| 1265 | - /** |
|
| 1266 | - * read vCard data into a vCard object |
|
| 1267 | - * |
|
| 1268 | - * @param string $cardData |
|
| 1269 | - * @return VCard |
|
| 1270 | - */ |
|
| 1271 | - protected function readCard($cardData) { |
|
| 1272 | - return Reader::read($cardData); |
|
| 1273 | - } |
|
| 1274 | - |
|
| 1275 | - /** |
|
| 1276 | - * delete all properties from a given card |
|
| 1277 | - * |
|
| 1278 | - * @param int $addressBookId |
|
| 1279 | - * @param int $cardId |
|
| 1280 | - */ |
|
| 1281 | - protected function purgeProperties($addressBookId, $cardId) { |
|
| 1282 | - $query = $this->db->getQueryBuilder(); |
|
| 1283 | - $query->delete($this->dbCardsPropertiesTable) |
|
| 1284 | - ->where($query->expr()->eq('cardid', $query->createNamedParameter($cardId))) |
|
| 1285 | - ->andWhere($query->expr()->eq('addressbookid', $query->createNamedParameter($addressBookId))); |
|
| 1286 | - $query->execute(); |
|
| 1287 | - } |
|
| 1288 | - |
|
| 1289 | - /** |
|
| 1290 | - * Get ID from a given contact |
|
| 1291 | - */ |
|
| 1292 | - protected function getCardId(int $addressBookId, string $uri): int { |
|
| 1293 | - $query = $this->db->getQueryBuilder(); |
|
| 1294 | - $query->select('id')->from($this->dbCardsTable) |
|
| 1295 | - ->where($query->expr()->eq('uri', $query->createNamedParameter($uri))) |
|
| 1296 | - ->andWhere($query->expr()->eq('addressbookid', $query->createNamedParameter($addressBookId))); |
|
| 1297 | - |
|
| 1298 | - $result = $query->execute(); |
|
| 1299 | - $cardIds = $result->fetch(); |
|
| 1300 | - $result->closeCursor(); |
|
| 1301 | - |
|
| 1302 | - if (!isset($cardIds['id'])) { |
|
| 1303 | - throw new \InvalidArgumentException('Card does not exists: ' . $uri); |
|
| 1304 | - } |
|
| 1305 | - |
|
| 1306 | - return (int)$cardIds['id']; |
|
| 1307 | - } |
|
| 1308 | - |
|
| 1309 | - /** |
|
| 1310 | - * For shared address books the sharee is set in the ACL of the address book |
|
| 1311 | - * |
|
| 1312 | - * @param int $addressBookId |
|
| 1313 | - * @param list<array{privilege: string, principal: string, protected: bool}> $acl |
|
| 1314 | - * @return list<array{privilege: string, principal: string, protected: bool}> |
|
| 1315 | - */ |
|
| 1316 | - public function applyShareAcl(int $addressBookId, array $acl): array { |
|
| 1317 | - return $this->sharingBackend->applyShareAcl($addressBookId, $acl); |
|
| 1318 | - } |
|
| 1319 | - |
|
| 1320 | - private function convertPrincipal(string $principalUri, bool $toV2): string { |
|
| 1321 | - if ($this->principalBackend->getPrincipalPrefix() === 'principals') { |
|
| 1322 | - [, $name] = \Sabre\Uri\split($principalUri); |
|
| 1323 | - if ($toV2 === true) { |
|
| 1324 | - return "principals/users/$name"; |
|
| 1325 | - } |
|
| 1326 | - return "principals/$name"; |
|
| 1327 | - } |
|
| 1328 | - return $principalUri; |
|
| 1329 | - } |
|
| 1330 | - |
|
| 1331 | - private function addOwnerPrincipal(array &$addressbookInfo): void { |
|
| 1332 | - $ownerPrincipalKey = '{' . \OCA\DAV\DAV\Sharing\Plugin::NS_OWNCLOUD . '}owner-principal'; |
|
| 1333 | - $displaynameKey = '{' . \OCA\DAV\DAV\Sharing\Plugin::NS_NEXTCLOUD . '}owner-displayname'; |
|
| 1334 | - if (isset($addressbookInfo[$ownerPrincipalKey])) { |
|
| 1335 | - $uri = $addressbookInfo[$ownerPrincipalKey]; |
|
| 1336 | - } else { |
|
| 1337 | - $uri = $addressbookInfo['principaluri']; |
|
| 1338 | - } |
|
| 1339 | - |
|
| 1340 | - $principalInformation = $this->principalBackend->getPrincipalByPath($uri); |
|
| 1341 | - if (isset($principalInformation['{DAV:}displayname'])) { |
|
| 1342 | - $addressbookInfo[$displaynameKey] = $principalInformation['{DAV:}displayname']; |
|
| 1343 | - } |
|
| 1344 | - } |
|
| 1345 | - |
|
| 1346 | - /** |
|
| 1347 | - * Extract UID from vcard |
|
| 1348 | - * |
|
| 1349 | - * @param string $cardData the vcard raw data |
|
| 1350 | - * @return string the uid |
|
| 1351 | - * @throws BadRequest if no UID is available or vcard is empty |
|
| 1352 | - */ |
|
| 1353 | - private function getUID(string $cardData): string { |
|
| 1354 | - if ($cardData !== '') { |
|
| 1355 | - $vCard = Reader::read($cardData); |
|
| 1356 | - if ($vCard->UID) { |
|
| 1357 | - $uid = $vCard->UID->getValue(); |
|
| 1358 | - return $uid; |
|
| 1359 | - } |
|
| 1360 | - // should already be handled, but just in case |
|
| 1361 | - throw new BadRequest('vCards on CardDAV servers MUST have a UID property'); |
|
| 1362 | - } |
|
| 1363 | - // should already be handled, but just in case |
|
| 1364 | - throw new BadRequest('vCard can not be empty'); |
|
| 1365 | - } |
|
| 63 | + public const PERSONAL_ADDRESSBOOK_URI = 'contacts'; |
|
| 64 | + public const PERSONAL_ADDRESSBOOK_NAME = 'Contacts'; |
|
| 65 | + |
|
| 66 | + private Principal $principalBackend; |
|
| 67 | + private string $dbCardsTable = 'cards'; |
|
| 68 | + private string $dbCardsPropertiesTable = 'cards_properties'; |
|
| 69 | + private IDBConnection $db; |
|
| 70 | + private Backend $sharingBackend; |
|
| 71 | + |
|
| 72 | + /** @var array properties to index */ |
|
| 73 | + public static array $indexProperties = [ |
|
| 74 | + 'BDAY', 'UID', 'N', 'FN', 'TITLE', 'ROLE', 'NOTE', 'NICKNAME', |
|
| 75 | + 'ORG', 'CATEGORIES', 'EMAIL', 'TEL', 'IMPP', 'ADR', 'URL', 'GEO', |
|
| 76 | + 'CLOUD', 'X-SOCIALPROFILE']; |
|
| 77 | + |
|
| 78 | + /** |
|
| 79 | + * @var string[] Map of uid => display name |
|
| 80 | + */ |
|
| 81 | + protected array $userDisplayNames; |
|
| 82 | + private IUserManager $userManager; |
|
| 83 | + private IEventDispatcher $dispatcher; |
|
| 84 | + private array $etagCache = []; |
|
| 85 | + |
|
| 86 | + /** |
|
| 87 | + * CardDavBackend constructor. |
|
| 88 | + * |
|
| 89 | + * @param IDBConnection $db |
|
| 90 | + * @param Principal $principalBackend |
|
| 91 | + * @param IUserManager $userManager |
|
| 92 | + * @param IGroupManager $groupManager |
|
| 93 | + * @param IEventDispatcher $dispatcher |
|
| 94 | + */ |
|
| 95 | + public function __construct(IDBConnection $db, |
|
| 96 | + Principal $principalBackend, |
|
| 97 | + IUserManager $userManager, |
|
| 98 | + IGroupManager $groupManager, |
|
| 99 | + IEventDispatcher $dispatcher) { |
|
| 100 | + $this->db = $db; |
|
| 101 | + $this->principalBackend = $principalBackend; |
|
| 102 | + $this->userManager = $userManager; |
|
| 103 | + $this->dispatcher = $dispatcher; |
|
| 104 | + $this->sharingBackend = new Backend($this->db, $this->userManager, $groupManager, $principalBackend, 'addressbook'); |
|
| 105 | + } |
|
| 106 | + |
|
| 107 | + /** |
|
| 108 | + * Return the number of address books for a principal |
|
| 109 | + * |
|
| 110 | + * @param $principalUri |
|
| 111 | + * @return int |
|
| 112 | + */ |
|
| 113 | + public function getAddressBooksForUserCount($principalUri) { |
|
| 114 | + $principalUri = $this->convertPrincipal($principalUri, true); |
|
| 115 | + $query = $this->db->getQueryBuilder(); |
|
| 116 | + $query->select($query->func()->count('*')) |
|
| 117 | + ->from('addressbooks') |
|
| 118 | + ->where($query->expr()->eq('principaluri', $query->createNamedParameter($principalUri))); |
|
| 119 | + |
|
| 120 | + $result = $query->executeQuery(); |
|
| 121 | + $column = (int) $result->fetchOne(); |
|
| 122 | + $result->closeCursor(); |
|
| 123 | + return $column; |
|
| 124 | + } |
|
| 125 | + |
|
| 126 | + /** |
|
| 127 | + * Returns the list of address books for a specific user. |
|
| 128 | + * |
|
| 129 | + * Every addressbook should have the following properties: |
|
| 130 | + * id - an arbitrary unique id |
|
| 131 | + * uri - the 'basename' part of the url |
|
| 132 | + * principaluri - Same as the passed parameter |
|
| 133 | + * |
|
| 134 | + * Any additional clark-notation property may be passed besides this. Some |
|
| 135 | + * common ones are : |
|
| 136 | + * {DAV:}displayname |
|
| 137 | + * {urn:ietf:params:xml:ns:carddav}addressbook-description |
|
| 138 | + * {http://calendarserver.org/ns/}getctag |
|
| 139 | + * |
|
| 140 | + * @param string $principalUri |
|
| 141 | + * @return array |
|
| 142 | + */ |
|
| 143 | + public function getAddressBooksForUser($principalUri) { |
|
| 144 | + $principalUriOriginal = $principalUri; |
|
| 145 | + $principalUri = $this->convertPrincipal($principalUri, true); |
|
| 146 | + $query = $this->db->getQueryBuilder(); |
|
| 147 | + $query->select(['id', 'uri', 'displayname', 'principaluri', 'description', 'synctoken']) |
|
| 148 | + ->from('addressbooks') |
|
| 149 | + ->where($query->expr()->eq('principaluri', $query->createNamedParameter($principalUri))); |
|
| 150 | + |
|
| 151 | + $addressBooks = []; |
|
| 152 | + |
|
| 153 | + $result = $query->execute(); |
|
| 154 | + while ($row = $result->fetch()) { |
|
| 155 | + $addressBooks[$row['id']] = [ |
|
| 156 | + 'id' => $row['id'], |
|
| 157 | + 'uri' => $row['uri'], |
|
| 158 | + 'principaluri' => $this->convertPrincipal($row['principaluri'], false), |
|
| 159 | + '{DAV:}displayname' => $row['displayname'], |
|
| 160 | + '{' . Plugin::NS_CARDDAV . '}addressbook-description' => $row['description'], |
|
| 161 | + '{http://calendarserver.org/ns/}getctag' => $row['synctoken'], |
|
| 162 | + '{http://sabredav.org/ns}sync-token' => $row['synctoken'] ?: '0', |
|
| 163 | + ]; |
|
| 164 | + |
|
| 165 | + $this->addOwnerPrincipal($addressBooks[$row['id']]); |
|
| 166 | + } |
|
| 167 | + $result->closeCursor(); |
|
| 168 | + |
|
| 169 | + // query for shared addressbooks |
|
| 170 | + $principals = $this->principalBackend->getGroupMembership($principalUriOriginal, true); |
|
| 171 | + $principals = array_merge($principals, $this->principalBackend->getCircleMembership($principalUriOriginal)); |
|
| 172 | + |
|
| 173 | + $principals[] = $principalUri; |
|
| 174 | + |
|
| 175 | + $query = $this->db->getQueryBuilder(); |
|
| 176 | + $result = $query->select(['a.id', 'a.uri', 'a.displayname', 'a.principaluri', 'a.description', 'a.synctoken', 's.access']) |
|
| 177 | + ->from('dav_shares', 's') |
|
| 178 | + ->join('s', 'addressbooks', 'a', $query->expr()->eq('s.resourceid', 'a.id')) |
|
| 179 | + ->where($query->expr()->in('s.principaluri', $query->createParameter('principaluri'))) |
|
| 180 | + ->andWhere($query->expr()->eq('s.type', $query->createParameter('type'))) |
|
| 181 | + ->setParameter('type', 'addressbook') |
|
| 182 | + ->setParameter('principaluri', $principals, IQueryBuilder::PARAM_STR_ARRAY) |
|
| 183 | + ->execute(); |
|
| 184 | + |
|
| 185 | + $readOnlyPropertyName = '{' . \OCA\DAV\DAV\Sharing\Plugin::NS_OWNCLOUD . '}read-only'; |
|
| 186 | + while ($row = $result->fetch()) { |
|
| 187 | + if ($row['principaluri'] === $principalUri) { |
|
| 188 | + continue; |
|
| 189 | + } |
|
| 190 | + |
|
| 191 | + $readOnly = (int)$row['access'] === Backend::ACCESS_READ; |
|
| 192 | + if (isset($addressBooks[$row['id']])) { |
|
| 193 | + if ($readOnly) { |
|
| 194 | + // New share can not have more permissions then the old one. |
|
| 195 | + continue; |
|
| 196 | + } |
|
| 197 | + if (isset($addressBooks[$row['id']][$readOnlyPropertyName]) && |
|
| 198 | + $addressBooks[$row['id']][$readOnlyPropertyName] === 0) { |
|
| 199 | + // Old share is already read-write, no more permissions can be gained |
|
| 200 | + continue; |
|
| 201 | + } |
|
| 202 | + } |
|
| 203 | + |
|
| 204 | + [, $name] = \Sabre\Uri\split($row['principaluri']); |
|
| 205 | + $uri = $row['uri'] . '_shared_by_' . $name; |
|
| 206 | + $displayName = $row['displayname'] . ' (' . $this->getUserDisplayName($name) . ')'; |
|
| 207 | + |
|
| 208 | + $addressBooks[$row['id']] = [ |
|
| 209 | + 'id' => $row['id'], |
|
| 210 | + 'uri' => $uri, |
|
| 211 | + 'principaluri' => $principalUriOriginal, |
|
| 212 | + '{DAV:}displayname' => $displayName, |
|
| 213 | + '{' . Plugin::NS_CARDDAV . '}addressbook-description' => $row['description'], |
|
| 214 | + '{http://calendarserver.org/ns/}getctag' => $row['synctoken'], |
|
| 215 | + '{http://sabredav.org/ns}sync-token' => $row['synctoken'] ?: '0', |
|
| 216 | + '{' . \OCA\DAV\DAV\Sharing\Plugin::NS_OWNCLOUD . '}owner-principal' => $row['principaluri'], |
|
| 217 | + $readOnlyPropertyName => $readOnly, |
|
| 218 | + ]; |
|
| 219 | + |
|
| 220 | + $this->addOwnerPrincipal($addressBooks[$row['id']]); |
|
| 221 | + } |
|
| 222 | + $result->closeCursor(); |
|
| 223 | + |
|
| 224 | + return array_values($addressBooks); |
|
| 225 | + } |
|
| 226 | + |
|
| 227 | + public function getUsersOwnAddressBooks($principalUri) { |
|
| 228 | + $principalUri = $this->convertPrincipal($principalUri, true); |
|
| 229 | + $query = $this->db->getQueryBuilder(); |
|
| 230 | + $query->select(['id', 'uri', 'displayname', 'principaluri', 'description', 'synctoken']) |
|
| 231 | + ->from('addressbooks') |
|
| 232 | + ->where($query->expr()->eq('principaluri', $query->createNamedParameter($principalUri))); |
|
| 233 | + |
|
| 234 | + $addressBooks = []; |
|
| 235 | + |
|
| 236 | + $result = $query->execute(); |
|
| 237 | + while ($row = $result->fetch()) { |
|
| 238 | + $addressBooks[$row['id']] = [ |
|
| 239 | + 'id' => $row['id'], |
|
| 240 | + 'uri' => $row['uri'], |
|
| 241 | + 'principaluri' => $this->convertPrincipal($row['principaluri'], false), |
|
| 242 | + '{DAV:}displayname' => $row['displayname'], |
|
| 243 | + '{' . Plugin::NS_CARDDAV . '}addressbook-description' => $row['description'], |
|
| 244 | + '{http://calendarserver.org/ns/}getctag' => $row['synctoken'], |
|
| 245 | + '{http://sabredav.org/ns}sync-token' => $row['synctoken'] ?: '0', |
|
| 246 | + ]; |
|
| 247 | + |
|
| 248 | + $this->addOwnerPrincipal($addressBooks[$row['id']]); |
|
| 249 | + } |
|
| 250 | + $result->closeCursor(); |
|
| 251 | + |
|
| 252 | + return array_values($addressBooks); |
|
| 253 | + } |
|
| 254 | + |
|
| 255 | + private function getUserDisplayName($uid) { |
|
| 256 | + if (!isset($this->userDisplayNames[$uid])) { |
|
| 257 | + $user = $this->userManager->get($uid); |
|
| 258 | + |
|
| 259 | + if ($user instanceof IUser) { |
|
| 260 | + $this->userDisplayNames[$uid] = $user->getDisplayName(); |
|
| 261 | + } else { |
|
| 262 | + $this->userDisplayNames[$uid] = $uid; |
|
| 263 | + } |
|
| 264 | + } |
|
| 265 | + |
|
| 266 | + return $this->userDisplayNames[$uid]; |
|
| 267 | + } |
|
| 268 | + |
|
| 269 | + /** |
|
| 270 | + * @param int $addressBookId |
|
| 271 | + */ |
|
| 272 | + public function getAddressBookById(int $addressBookId): ?array { |
|
| 273 | + $query = $this->db->getQueryBuilder(); |
|
| 274 | + $result = $query->select(['id', 'uri', 'displayname', 'principaluri', 'description', 'synctoken']) |
|
| 275 | + ->from('addressbooks') |
|
| 276 | + ->where($query->expr()->eq('id', $query->createNamedParameter($addressBookId, IQueryBuilder::PARAM_INT))) |
|
| 277 | + ->executeQuery(); |
|
| 278 | + $row = $result->fetch(); |
|
| 279 | + $result->closeCursor(); |
|
| 280 | + if (!$row) { |
|
| 281 | + return null; |
|
| 282 | + } |
|
| 283 | + |
|
| 284 | + $addressBook = [ |
|
| 285 | + 'id' => $row['id'], |
|
| 286 | + 'uri' => $row['uri'], |
|
| 287 | + 'principaluri' => $row['principaluri'], |
|
| 288 | + '{DAV:}displayname' => $row['displayname'], |
|
| 289 | + '{' . Plugin::NS_CARDDAV . '}addressbook-description' => $row['description'], |
|
| 290 | + '{http://calendarserver.org/ns/}getctag' => $row['synctoken'], |
|
| 291 | + '{http://sabredav.org/ns}sync-token' => $row['synctoken'] ?: '0', |
|
| 292 | + ]; |
|
| 293 | + |
|
| 294 | + $this->addOwnerPrincipal($addressBook); |
|
| 295 | + |
|
| 296 | + return $addressBook; |
|
| 297 | + } |
|
| 298 | + |
|
| 299 | + public function getAddressBooksByUri(string $principal, string $addressBookUri): ?array { |
|
| 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 | + ->executeQuery(); |
|
| 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'] ?: '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 | + public function updateAddressBook($addressBookId, \Sabre\DAV\PropPatch $propPatch) { |
|
| 346 | + $supportedProperties = [ |
|
| 347 | + '{DAV:}displayname', |
|
| 348 | + '{' . Plugin::NS_CARDDAV . '}addressbook-description', |
|
| 349 | + ]; |
|
| 350 | + |
|
| 351 | + $propPatch->handle($supportedProperties, function ($mutations) use ($addressBookId) { |
|
| 352 | + $updates = []; |
|
| 353 | + foreach ($mutations as $property => $newValue) { |
|
| 354 | + switch ($property) { |
|
| 355 | + case '{DAV:}displayname': |
|
| 356 | + $updates['displayname'] = $newValue; |
|
| 357 | + break; |
|
| 358 | + case '{' . Plugin::NS_CARDDAV . '}addressbook-description': |
|
| 359 | + $updates['description'] = $newValue; |
|
| 360 | + break; |
|
| 361 | + } |
|
| 362 | + } |
|
| 363 | + $query = $this->db->getQueryBuilder(); |
|
| 364 | + $query->update('addressbooks'); |
|
| 365 | + |
|
| 366 | + foreach ($updates as $key => $value) { |
|
| 367 | + $query->set($key, $query->createNamedParameter($value)); |
|
| 368 | + } |
|
| 369 | + $query->where($query->expr()->eq('id', $query->createNamedParameter($addressBookId))) |
|
| 370 | + ->executeStatement(); |
|
| 371 | + |
|
| 372 | + $this->addChange($addressBookId, "", 2); |
|
| 373 | + |
|
| 374 | + $addressBookRow = $this->getAddressBookById((int)$addressBookId); |
|
| 375 | + $shares = $this->getShares((int)$addressBookId); |
|
| 376 | + $this->dispatcher->dispatchTyped(new AddressBookUpdatedEvent((int)$addressBookId, $addressBookRow, $shares, $mutations)); |
|
| 377 | + |
|
| 378 | + return true; |
|
| 379 | + }); |
|
| 380 | + } |
|
| 381 | + |
|
| 382 | + /** |
|
| 383 | + * Creates a new address book |
|
| 384 | + * |
|
| 385 | + * @param string $principalUri |
|
| 386 | + * @param string $url Just the 'basename' of the url. |
|
| 387 | + * @param array $properties |
|
| 388 | + * @return int |
|
| 389 | + * @throws BadRequest |
|
| 390 | + */ |
|
| 391 | + public function createAddressBook($principalUri, $url, array $properties) { |
|
| 392 | + if (strlen($url) > 255) { |
|
| 393 | + throw new BadRequest('URI too long. Address book not created'); |
|
| 394 | + } |
|
| 395 | + |
|
| 396 | + $values = [ |
|
| 397 | + 'displayname' => null, |
|
| 398 | + 'description' => null, |
|
| 399 | + 'principaluri' => $principalUri, |
|
| 400 | + 'uri' => $url, |
|
| 401 | + 'synctoken' => 1 |
|
| 402 | + ]; |
|
| 403 | + |
|
| 404 | + foreach ($properties as $property => $newValue) { |
|
| 405 | + switch ($property) { |
|
| 406 | + case '{DAV:}displayname': |
|
| 407 | + $values['displayname'] = $newValue; |
|
| 408 | + break; |
|
| 409 | + case '{' . Plugin::NS_CARDDAV . '}addressbook-description': |
|
| 410 | + $values['description'] = $newValue; |
|
| 411 | + break; |
|
| 412 | + default: |
|
| 413 | + throw new BadRequest('Unknown property: ' . $property); |
|
| 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 | + $addressBookId = $query->getLastInsertId(); |
|
| 436 | + $addressBookRow = $this->getAddressBookById($addressBookId); |
|
| 437 | + $this->dispatcher->dispatchTyped(new AddressBookCreatedEvent($addressBookId, $addressBookRow)); |
|
| 438 | + |
|
| 439 | + return $addressBookId; |
|
| 440 | + } |
|
| 441 | + |
|
| 442 | + /** |
|
| 443 | + * Deletes an entire addressbook and all its contents |
|
| 444 | + * |
|
| 445 | + * @param mixed $addressBookId |
|
| 446 | + * @return void |
|
| 447 | + */ |
|
| 448 | + public function deleteAddressBook($addressBookId) { |
|
| 449 | + $addressBookId = (int)$addressBookId; |
|
| 450 | + $addressBookData = $this->getAddressBookById($addressBookId); |
|
| 451 | + $shares = $this->getShares($addressBookId); |
|
| 452 | + |
|
| 453 | + $query = $this->db->getQueryBuilder(); |
|
| 454 | + $query->delete($this->dbCardsTable) |
|
| 455 | + ->where($query->expr()->eq('addressbookid', $query->createParameter('addressbookid'))) |
|
| 456 | + ->setParameter('addressbookid', $addressBookId, IQueryBuilder::PARAM_INT) |
|
| 457 | + ->executeStatement(); |
|
| 458 | + |
|
| 459 | + $query->delete('addressbookchanges') |
|
| 460 | + ->where($query->expr()->eq('addressbookid', $query->createParameter('addressbookid'))) |
|
| 461 | + ->setParameter('addressbookid', $addressBookId, IQueryBuilder::PARAM_INT) |
|
| 462 | + ->executeStatement(); |
|
| 463 | + |
|
| 464 | + $query->delete('addressbooks') |
|
| 465 | + ->where($query->expr()->eq('id', $query->createParameter('id'))) |
|
| 466 | + ->setParameter('id', $addressBookId, IQueryBuilder::PARAM_INT) |
|
| 467 | + ->executeStatement(); |
|
| 468 | + |
|
| 469 | + $this->sharingBackend->deleteAllShares($addressBookId); |
|
| 470 | + |
|
| 471 | + $query->delete($this->dbCardsPropertiesTable) |
|
| 472 | + ->where($query->expr()->eq('addressbookid', $query->createNamedParameter($addressBookId, IQueryBuilder::PARAM_INT))) |
|
| 473 | + ->executeStatement(); |
|
| 474 | + |
|
| 475 | + if ($addressBookData) { |
|
| 476 | + $this->dispatcher->dispatchTyped(new AddressBookDeletedEvent($addressBookId, $addressBookData, $shares)); |
|
| 477 | + } |
|
| 478 | + } |
|
| 479 | + |
|
| 480 | + /** |
|
| 481 | + * Returns all cards for a specific addressbook id. |
|
| 482 | + * |
|
| 483 | + * This method should return the following properties for each card: |
|
| 484 | + * * carddata - raw vcard data |
|
| 485 | + * * uri - Some unique url |
|
| 486 | + * * lastmodified - A unix timestamp |
|
| 487 | + * |
|
| 488 | + * It's recommended to also return the following properties: |
|
| 489 | + * * etag - A unique etag. This must change every time the card changes. |
|
| 490 | + * * size - The size of the card in bytes. |
|
| 491 | + * |
|
| 492 | + * If these last two properties are provided, less time will be spent |
|
| 493 | + * calculating them. If they are specified, you can also omit carddata. |
|
| 494 | + * This may speed up certain requests, especially with large cards. |
|
| 495 | + * |
|
| 496 | + * @param mixed $addressbookId |
|
| 497 | + * @return array |
|
| 498 | + */ |
|
| 499 | + public function getCards($addressbookId) { |
|
| 500 | + $query = $this->db->getQueryBuilder(); |
|
| 501 | + $query->select(['id', 'uri', 'lastmodified', 'etag', 'size', 'carddata', 'uid']) |
|
| 502 | + ->from($this->dbCardsTable) |
|
| 503 | + ->where($query->expr()->eq('addressbookid', $query->createNamedParameter($addressbookId))); |
|
| 504 | + |
|
| 505 | + $cards = []; |
|
| 506 | + |
|
| 507 | + $result = $query->execute(); |
|
| 508 | + while ($row = $result->fetch()) { |
|
| 509 | + $row['etag'] = '"' . $row['etag'] . '"'; |
|
| 510 | + |
|
| 511 | + $modified = false; |
|
| 512 | + $row['carddata'] = $this->readBlob($row['carddata'], $modified); |
|
| 513 | + if ($modified) { |
|
| 514 | + $row['size'] = strlen($row['carddata']); |
|
| 515 | + } |
|
| 516 | + |
|
| 517 | + $cards[] = $row; |
|
| 518 | + } |
|
| 519 | + $result->closeCursor(); |
|
| 520 | + |
|
| 521 | + return $cards; |
|
| 522 | + } |
|
| 523 | + |
|
| 524 | + /** |
|
| 525 | + * Returns a specific card. |
|
| 526 | + * |
|
| 527 | + * The same set of properties must be returned as with getCards. The only |
|
| 528 | + * exception is that 'carddata' is absolutely required. |
|
| 529 | + * |
|
| 530 | + * If the card does not exist, you must return false. |
|
| 531 | + * |
|
| 532 | + * @param mixed $addressBookId |
|
| 533 | + * @param string $cardUri |
|
| 534 | + * @return array |
|
| 535 | + */ |
|
| 536 | + public function getCard($addressBookId, $cardUri) { |
|
| 537 | + $query = $this->db->getQueryBuilder(); |
|
| 538 | + $query->select(['id', 'uri', 'lastmodified', 'etag', 'size', 'carddata', 'uid']) |
|
| 539 | + ->from($this->dbCardsTable) |
|
| 540 | + ->where($query->expr()->eq('addressbookid', $query->createNamedParameter($addressBookId))) |
|
| 541 | + ->andWhere($query->expr()->eq('uri', $query->createNamedParameter($cardUri))) |
|
| 542 | + ->setMaxResults(1); |
|
| 543 | + |
|
| 544 | + $result = $query->execute(); |
|
| 545 | + $row = $result->fetch(); |
|
| 546 | + if (!$row) { |
|
| 547 | + return false; |
|
| 548 | + } |
|
| 549 | + $row['etag'] = '"' . $row['etag'] . '"'; |
|
| 550 | + |
|
| 551 | + $modified = false; |
|
| 552 | + $row['carddata'] = $this->readBlob($row['carddata'], $modified); |
|
| 553 | + if ($modified) { |
|
| 554 | + $row['size'] = strlen($row['carddata']); |
|
| 555 | + } |
|
| 556 | + |
|
| 557 | + return $row; |
|
| 558 | + } |
|
| 559 | + |
|
| 560 | + /** |
|
| 561 | + * Returns a list of cards. |
|
| 562 | + * |
|
| 563 | + * This method should work identical to getCard, but instead return all the |
|
| 564 | + * cards in the list as an array. |
|
| 565 | + * |
|
| 566 | + * If the backend supports this, it may allow for some speed-ups. |
|
| 567 | + * |
|
| 568 | + * @param mixed $addressBookId |
|
| 569 | + * @param array $uris |
|
| 570 | + * @return array |
|
| 571 | + */ |
|
| 572 | + public function getMultipleCards($addressBookId, array $uris) { |
|
| 573 | + if (empty($uris)) { |
|
| 574 | + return []; |
|
| 575 | + } |
|
| 576 | + |
|
| 577 | + $chunks = array_chunk($uris, 100); |
|
| 578 | + $cards = []; |
|
| 579 | + |
|
| 580 | + $query = $this->db->getQueryBuilder(); |
|
| 581 | + $query->select(['id', 'uri', 'lastmodified', 'etag', 'size', 'carddata', 'uid']) |
|
| 582 | + ->from($this->dbCardsTable) |
|
| 583 | + ->where($query->expr()->eq('addressbookid', $query->createNamedParameter($addressBookId))) |
|
| 584 | + ->andWhere($query->expr()->in('uri', $query->createParameter('uri'))); |
|
| 585 | + |
|
| 586 | + foreach ($chunks as $uris) { |
|
| 587 | + $query->setParameter('uri', $uris, IQueryBuilder::PARAM_STR_ARRAY); |
|
| 588 | + $result = $query->execute(); |
|
| 589 | + |
|
| 590 | + while ($row = $result->fetch()) { |
|
| 591 | + $row['etag'] = '"' . $row['etag'] . '"'; |
|
| 592 | + |
|
| 593 | + $modified = false; |
|
| 594 | + $row['carddata'] = $this->readBlob($row['carddata'], $modified); |
|
| 595 | + if ($modified) { |
|
| 596 | + $row['size'] = strlen($row['carddata']); |
|
| 597 | + } |
|
| 598 | + |
|
| 599 | + $cards[] = $row; |
|
| 600 | + } |
|
| 601 | + $result->closeCursor(); |
|
| 602 | + } |
|
| 603 | + return $cards; |
|
| 604 | + } |
|
| 605 | + |
|
| 606 | + /** |
|
| 607 | + * Creates a new card. |
|
| 608 | + * |
|
| 609 | + * The addressbook id will be passed as the first argument. This is the |
|
| 610 | + * same id as it is returned from the getAddressBooksForUser method. |
|
| 611 | + * |
|
| 612 | + * The cardUri is a base uri, and doesn't include the full path. The |
|
| 613 | + * cardData argument is the vcard body, and is passed as a string. |
|
| 614 | + * |
|
| 615 | + * It is possible to return an ETag from this method. This ETag is for the |
|
| 616 | + * newly created resource, and must be enclosed with double quotes (that |
|
| 617 | + * is, the string itself must contain the double quotes). |
|
| 618 | + * |
|
| 619 | + * You should only return the ETag if you store the carddata as-is. If a |
|
| 620 | + * subsequent GET request on the same card does not have the same body, |
|
| 621 | + * byte-by-byte and you did return an ETag here, clients tend to get |
|
| 622 | + * confused. |
|
| 623 | + * |
|
| 624 | + * If you don't return an ETag, you can just return null. |
|
| 625 | + * |
|
| 626 | + * @param mixed $addressBookId |
|
| 627 | + * @param string $cardUri |
|
| 628 | + * @param string $cardData |
|
| 629 | + * @param bool $checkAlreadyExists |
|
| 630 | + * @return string |
|
| 631 | + */ |
|
| 632 | + public function createCard($addressBookId, $cardUri, $cardData, bool $checkAlreadyExists = true) { |
|
| 633 | + $etag = md5($cardData); |
|
| 634 | + $uid = $this->getUID($cardData); |
|
| 635 | + |
|
| 636 | + if ($checkAlreadyExists) { |
|
| 637 | + $q = $this->db->getQueryBuilder(); |
|
| 638 | + $q->select('uid') |
|
| 639 | + ->from($this->dbCardsTable) |
|
| 640 | + ->where($q->expr()->eq('addressbookid', $q->createNamedParameter($addressBookId))) |
|
| 641 | + ->andWhere($q->expr()->eq('uid', $q->createNamedParameter($uid))) |
|
| 642 | + ->setMaxResults(1); |
|
| 643 | + $result = $q->executeQuery(); |
|
| 644 | + $count = (bool)$result->fetchOne(); |
|
| 645 | + $result->closeCursor(); |
|
| 646 | + if ($count) { |
|
| 647 | + throw new \Sabre\DAV\Exception\BadRequest('VCard object with uid already exists in this addressbook collection.'); |
|
| 648 | + } |
|
| 649 | + } |
|
| 650 | + |
|
| 651 | + $query = $this->db->getQueryBuilder(); |
|
| 652 | + $query->insert('cards') |
|
| 653 | + ->values([ |
|
| 654 | + 'carddata' => $query->createNamedParameter($cardData, IQueryBuilder::PARAM_LOB), |
|
| 655 | + 'uri' => $query->createNamedParameter($cardUri), |
|
| 656 | + 'lastmodified' => $query->createNamedParameter(time()), |
|
| 657 | + 'addressbookid' => $query->createNamedParameter($addressBookId), |
|
| 658 | + 'size' => $query->createNamedParameter(strlen($cardData)), |
|
| 659 | + 'etag' => $query->createNamedParameter($etag), |
|
| 660 | + 'uid' => $query->createNamedParameter($uid), |
|
| 661 | + ]) |
|
| 662 | + ->execute(); |
|
| 663 | + |
|
| 664 | + $etagCacheKey = "$addressBookId#$cardUri"; |
|
| 665 | + $this->etagCache[$etagCacheKey] = $etag; |
|
| 666 | + |
|
| 667 | + $this->addChange($addressBookId, $cardUri, 1); |
|
| 668 | + $this->updateProperties($addressBookId, $cardUri, $cardData); |
|
| 669 | + |
|
| 670 | + $addressBookData = $this->getAddressBookById($addressBookId); |
|
| 671 | + $shares = $this->getShares($addressBookId); |
|
| 672 | + $objectRow = $this->getCard($addressBookId, $cardUri); |
|
| 673 | + $this->dispatcher->dispatchTyped(new CardCreatedEvent($addressBookId, $addressBookData, $shares, $objectRow)); |
|
| 674 | + |
|
| 675 | + return '"' . $etag . '"'; |
|
| 676 | + } |
|
| 677 | + |
|
| 678 | + /** |
|
| 679 | + * Updates a card. |
|
| 680 | + * |
|
| 681 | + * The addressbook id will be passed as the first argument. This is the |
|
| 682 | + * same id as it is returned from the getAddressBooksForUser method. |
|
| 683 | + * |
|
| 684 | + * The cardUri is a base uri, and doesn't include the full path. The |
|
| 685 | + * cardData argument is the vcard body, and is passed as a string. |
|
| 686 | + * |
|
| 687 | + * It is possible to return an ETag from this method. This ETag should |
|
| 688 | + * match that of the updated resource, and must be enclosed with double |
|
| 689 | + * quotes (that is: the string itself must contain the actual quotes). |
|
| 690 | + * |
|
| 691 | + * You should only return the ETag if you store the carddata as-is. If a |
|
| 692 | + * subsequent GET request on the same card does not have the same body, |
|
| 693 | + * byte-by-byte and you did return an ETag here, clients tend to get |
|
| 694 | + * confused. |
|
| 695 | + * |
|
| 696 | + * If you don't return an ETag, you can just return null. |
|
| 697 | + * |
|
| 698 | + * @param mixed $addressBookId |
|
| 699 | + * @param string $cardUri |
|
| 700 | + * @param string $cardData |
|
| 701 | + * @return string |
|
| 702 | + */ |
|
| 703 | + public function updateCard($addressBookId, $cardUri, $cardData) { |
|
| 704 | + $uid = $this->getUID($cardData); |
|
| 705 | + $etag = md5($cardData); |
|
| 706 | + $query = $this->db->getQueryBuilder(); |
|
| 707 | + |
|
| 708 | + // check for recently stored etag and stop if it is the same |
|
| 709 | + $etagCacheKey = "$addressBookId#$cardUri"; |
|
| 710 | + if (isset($this->etagCache[$etagCacheKey]) && $this->etagCache[$etagCacheKey] === $etag) { |
|
| 711 | + return '"' . $etag . '"'; |
|
| 712 | + } |
|
| 713 | + |
|
| 714 | + $query->update($this->dbCardsTable) |
|
| 715 | + ->set('carddata', $query->createNamedParameter($cardData, IQueryBuilder::PARAM_LOB)) |
|
| 716 | + ->set('lastmodified', $query->createNamedParameter(time())) |
|
| 717 | + ->set('size', $query->createNamedParameter(strlen($cardData))) |
|
| 718 | + ->set('etag', $query->createNamedParameter($etag)) |
|
| 719 | + ->set('uid', $query->createNamedParameter($uid)) |
|
| 720 | + ->where($query->expr()->eq('uri', $query->createNamedParameter($cardUri))) |
|
| 721 | + ->andWhere($query->expr()->eq('addressbookid', $query->createNamedParameter($addressBookId))) |
|
| 722 | + ->execute(); |
|
| 723 | + |
|
| 724 | + $this->etagCache[$etagCacheKey] = $etag; |
|
| 725 | + |
|
| 726 | + $this->addChange($addressBookId, $cardUri, 2); |
|
| 727 | + $this->updateProperties($addressBookId, $cardUri, $cardData); |
|
| 728 | + |
|
| 729 | + $addressBookData = $this->getAddressBookById($addressBookId); |
|
| 730 | + $shares = $this->getShares($addressBookId); |
|
| 731 | + $objectRow = $this->getCard($addressBookId, $cardUri); |
|
| 732 | + $this->dispatcher->dispatchTyped(new CardUpdatedEvent($addressBookId, $addressBookData, $shares, $objectRow)); |
|
| 733 | + return '"' . $etag . '"'; |
|
| 734 | + } |
|
| 735 | + |
|
| 736 | + /** |
|
| 737 | + * Deletes a card |
|
| 738 | + * |
|
| 739 | + * @param mixed $addressBookId |
|
| 740 | + * @param string $cardUri |
|
| 741 | + * @return bool |
|
| 742 | + */ |
|
| 743 | + public function deleteCard($addressBookId, $cardUri) { |
|
| 744 | + $addressBookData = $this->getAddressBookById($addressBookId); |
|
| 745 | + $shares = $this->getShares($addressBookId); |
|
| 746 | + $objectRow = $this->getCard($addressBookId, $cardUri); |
|
| 747 | + |
|
| 748 | + try { |
|
| 749 | + $cardId = $this->getCardId($addressBookId, $cardUri); |
|
| 750 | + } catch (\InvalidArgumentException $e) { |
|
| 751 | + $cardId = null; |
|
| 752 | + } |
|
| 753 | + $query = $this->db->getQueryBuilder(); |
|
| 754 | + $ret = $query->delete($this->dbCardsTable) |
|
| 755 | + ->where($query->expr()->eq('addressbookid', $query->createNamedParameter($addressBookId))) |
|
| 756 | + ->andWhere($query->expr()->eq('uri', $query->createNamedParameter($cardUri))) |
|
| 757 | + ->executeStatement(); |
|
| 758 | + |
|
| 759 | + $this->addChange($addressBookId, $cardUri, 3); |
|
| 760 | + |
|
| 761 | + if ($ret === 1) { |
|
| 762 | + if ($cardId !== null) { |
|
| 763 | + $this->dispatcher->dispatchTyped(new CardDeletedEvent($addressBookId, $addressBookData, $shares, $objectRow)); |
|
| 764 | + $this->purgeProperties($addressBookId, $cardId); |
|
| 765 | + } |
|
| 766 | + return true; |
|
| 767 | + } |
|
| 768 | + |
|
| 769 | + return false; |
|
| 770 | + } |
|
| 771 | + |
|
| 772 | + /** |
|
| 773 | + * The getChanges method returns all the changes that have happened, since |
|
| 774 | + * the specified syncToken in the specified address book. |
|
| 775 | + * |
|
| 776 | + * This function should return an array, such as the following: |
|
| 777 | + * |
|
| 778 | + * [ |
|
| 779 | + * 'syncToken' => 'The current synctoken', |
|
| 780 | + * 'added' => [ |
|
| 781 | + * 'new.txt', |
|
| 782 | + * ], |
|
| 783 | + * 'modified' => [ |
|
| 784 | + * 'modified.txt', |
|
| 785 | + * ], |
|
| 786 | + * 'deleted' => [ |
|
| 787 | + * 'foo.php.bak', |
|
| 788 | + * 'old.txt' |
|
| 789 | + * ] |
|
| 790 | + * ]; |
|
| 791 | + * |
|
| 792 | + * The returned syncToken property should reflect the *current* syncToken |
|
| 793 | + * of the calendar, as reported in the {http://sabredav.org/ns}sync-token |
|
| 794 | + * property. This is needed here too, to ensure the operation is atomic. |
|
| 795 | + * |
|
| 796 | + * If the $syncToken argument is specified as null, this is an initial |
|
| 797 | + * sync, and all members should be reported. |
|
| 798 | + * |
|
| 799 | + * The modified property is an array of nodenames that have changed since |
|
| 800 | + * the last token. |
|
| 801 | + * |
|
| 802 | + * The deleted property is an array with nodenames, that have been deleted |
|
| 803 | + * from collection. |
|
| 804 | + * |
|
| 805 | + * The $syncLevel argument is basically the 'depth' of the report. If it's |
|
| 806 | + * 1, you only have to report changes that happened only directly in |
|
| 807 | + * immediate descendants. If it's 2, it should also include changes from |
|
| 808 | + * the nodes below the child collections. (grandchildren) |
|
| 809 | + * |
|
| 810 | + * The $limit argument allows a client to specify how many results should |
|
| 811 | + * be returned at most. If the limit is not specified, it should be treated |
|
| 812 | + * as infinite. |
|
| 813 | + * |
|
| 814 | + * If the limit (infinite or not) is higher than you're willing to return, |
|
| 815 | + * you should throw a Sabre\DAV\Exception\TooMuchMatches() exception. |
|
| 816 | + * |
|
| 817 | + * If the syncToken is expired (due to data cleanup) or unknown, you must |
|
| 818 | + * return null. |
|
| 819 | + * |
|
| 820 | + * The limit is 'suggestive'. You are free to ignore it. |
|
| 821 | + * |
|
| 822 | + * @param string $addressBookId |
|
| 823 | + * @param string $syncToken |
|
| 824 | + * @param int $syncLevel |
|
| 825 | + * @param int|null $limit |
|
| 826 | + * @return array |
|
| 827 | + */ |
|
| 828 | + public function getChangesForAddressBook($addressBookId, $syncToken, $syncLevel, $limit = null) { |
|
| 829 | + // Current synctoken |
|
| 830 | + $qb = $this->db->getQueryBuilder(); |
|
| 831 | + $qb->select('synctoken') |
|
| 832 | + ->from('addressbooks') |
|
| 833 | + ->where( |
|
| 834 | + $qb->expr()->eq('id', $qb->createNamedParameter($addressBookId)) |
|
| 835 | + ); |
|
| 836 | + $stmt = $qb->executeQuery(); |
|
| 837 | + $currentToken = $stmt->fetchOne(); |
|
| 838 | + $stmt->closeCursor(); |
|
| 839 | + |
|
| 840 | + if (is_null($currentToken)) { |
|
| 841 | + return []; |
|
| 842 | + } |
|
| 843 | + |
|
| 844 | + $result = [ |
|
| 845 | + 'syncToken' => $currentToken, |
|
| 846 | + 'added' => [], |
|
| 847 | + 'modified' => [], |
|
| 848 | + 'deleted' => [], |
|
| 849 | + ]; |
|
| 850 | + |
|
| 851 | + if ($syncToken) { |
|
| 852 | + $qb = $this->db->getQueryBuilder(); |
|
| 853 | + $qb->select('uri', 'operation') |
|
| 854 | + ->from('addressbookchanges') |
|
| 855 | + ->where( |
|
| 856 | + $qb->expr()->andX( |
|
| 857 | + $qb->expr()->gte('synctoken', $qb->createNamedParameter($syncToken)), |
|
| 858 | + $qb->expr()->lt('synctoken', $qb->createNamedParameter($currentToken)), |
|
| 859 | + $qb->expr()->eq('addressbookid', $qb->createNamedParameter($addressBookId)) |
|
| 860 | + ) |
|
| 861 | + )->orderBy('synctoken'); |
|
| 862 | + |
|
| 863 | + if (is_int($limit) && $limit > 0) { |
|
| 864 | + $qb->setMaxResults($limit); |
|
| 865 | + } |
|
| 866 | + |
|
| 867 | + // Fetching all changes |
|
| 868 | + $stmt = $qb->executeQuery(); |
|
| 869 | + |
|
| 870 | + $changes = []; |
|
| 871 | + |
|
| 872 | + // This loop ensures that any duplicates are overwritten, only the |
|
| 873 | + // last change on a node is relevant. |
|
| 874 | + while ($row = $stmt->fetch(\PDO::FETCH_ASSOC)) { |
|
| 875 | + $changes[$row['uri']] = $row['operation']; |
|
| 876 | + } |
|
| 877 | + $stmt->closeCursor(); |
|
| 878 | + |
|
| 879 | + foreach ($changes as $uri => $operation) { |
|
| 880 | + switch ($operation) { |
|
| 881 | + case 1: |
|
| 882 | + $result['added'][] = $uri; |
|
| 883 | + break; |
|
| 884 | + case 2: |
|
| 885 | + $result['modified'][] = $uri; |
|
| 886 | + break; |
|
| 887 | + case 3: |
|
| 888 | + $result['deleted'][] = $uri; |
|
| 889 | + break; |
|
| 890 | + } |
|
| 891 | + } |
|
| 892 | + } else { |
|
| 893 | + $qb = $this->db->getQueryBuilder(); |
|
| 894 | + $qb->select('uri') |
|
| 895 | + ->from('cards') |
|
| 896 | + ->where( |
|
| 897 | + $qb->expr()->eq('addressbookid', $qb->createNamedParameter($addressBookId)) |
|
| 898 | + ); |
|
| 899 | + // No synctoken supplied, this is the initial sync. |
|
| 900 | + $stmt = $qb->executeQuery(); |
|
| 901 | + $result['added'] = $stmt->fetchAll(\PDO::FETCH_COLUMN); |
|
| 902 | + $stmt->closeCursor(); |
|
| 903 | + } |
|
| 904 | + return $result; |
|
| 905 | + } |
|
| 906 | + |
|
| 907 | + /** |
|
| 908 | + * Adds a change record to the addressbookchanges table. |
|
| 909 | + * |
|
| 910 | + * @param mixed $addressBookId |
|
| 911 | + * @param string $objectUri |
|
| 912 | + * @param int $operation 1 = add, 2 = modify, 3 = delete |
|
| 913 | + * @return void |
|
| 914 | + */ |
|
| 915 | + protected function addChange($addressBookId, $objectUri, $operation) { |
|
| 916 | + $sql = 'INSERT INTO `*PREFIX*addressbookchanges`(`uri`, `synctoken`, `addressbookid`, `operation`) SELECT ?, `synctoken`, ?, ? FROM `*PREFIX*addressbooks` WHERE `id` = ?'; |
|
| 917 | + $stmt = $this->db->prepare($sql); |
|
| 918 | + $stmt->execute([ |
|
| 919 | + $objectUri, |
|
| 920 | + $addressBookId, |
|
| 921 | + $operation, |
|
| 922 | + $addressBookId |
|
| 923 | + ]); |
|
| 924 | + $stmt = $this->db->prepare('UPDATE `*PREFIX*addressbooks` SET `synctoken` = `synctoken` + 1 WHERE `id` = ?'); |
|
| 925 | + $stmt->execute([ |
|
| 926 | + $addressBookId |
|
| 927 | + ]); |
|
| 928 | + } |
|
| 929 | + |
|
| 930 | + /** |
|
| 931 | + * @param resource|string $cardData |
|
| 932 | + * @param bool $modified |
|
| 933 | + * @return string |
|
| 934 | + */ |
|
| 935 | + private function readBlob($cardData, &$modified = false) { |
|
| 936 | + if (is_resource($cardData)) { |
|
| 937 | + $cardData = stream_get_contents($cardData); |
|
| 938 | + } |
|
| 939 | + |
|
| 940 | + // Micro optimisation |
|
| 941 | + // don't loop through |
|
| 942 | + if (strpos($cardData, 'PHOTO:data:') === 0) { |
|
| 943 | + return $cardData; |
|
| 944 | + } |
|
| 945 | + |
|
| 946 | + $cardDataArray = explode("\r\n", $cardData); |
|
| 947 | + |
|
| 948 | + $cardDataFiltered = []; |
|
| 949 | + $removingPhoto = false; |
|
| 950 | + foreach ($cardDataArray as $line) { |
|
| 951 | + if (strpos($line, 'PHOTO:data:') === 0 |
|
| 952 | + && strpos($line, 'PHOTO:data:image/') !== 0) { |
|
| 953 | + // Filter out PHOTO data of non-images |
|
| 954 | + $removingPhoto = true; |
|
| 955 | + $modified = true; |
|
| 956 | + continue; |
|
| 957 | + } |
|
| 958 | + |
|
| 959 | + if ($removingPhoto) { |
|
| 960 | + if (strpos($line, ' ') === 0) { |
|
| 961 | + continue; |
|
| 962 | + } |
|
| 963 | + // No leading space means this is a new property |
|
| 964 | + $removingPhoto = false; |
|
| 965 | + } |
|
| 966 | + |
|
| 967 | + $cardDataFiltered[] = $line; |
|
| 968 | + } |
|
| 969 | + return implode("\r\n", $cardDataFiltered); |
|
| 970 | + } |
|
| 971 | + |
|
| 972 | + /** |
|
| 973 | + * @param list<array{href: string, commonName: string, readOnly: bool}> $add |
|
| 974 | + * @param list<string> $remove |
|
| 975 | + */ |
|
| 976 | + public function updateShares(IShareable $shareable, array $add, array $remove): void { |
|
| 977 | + $addressBookId = $shareable->getResourceId(); |
|
| 978 | + $addressBookData = $this->getAddressBookById($addressBookId); |
|
| 979 | + $oldShares = $this->getShares($addressBookId); |
|
| 980 | + |
|
| 981 | + $this->sharingBackend->updateShares($shareable, $add, $remove); |
|
| 982 | + |
|
| 983 | + $this->dispatcher->dispatchTyped(new AddressBookShareUpdatedEvent($addressBookId, $addressBookData, $oldShares, $add, $remove)); |
|
| 984 | + } |
|
| 985 | + |
|
| 986 | + /** |
|
| 987 | + * Search contacts in a specific address-book |
|
| 988 | + * |
|
| 989 | + * @param int $addressBookId |
|
| 990 | + * @param string $pattern which should match within the $searchProperties |
|
| 991 | + * @param array $searchProperties defines the properties within the query pattern should match |
|
| 992 | + * @param array $options = array() to define the search behavior |
|
| 993 | + * - 'escape_like_param' - If set to false wildcards _ and % are not escaped, otherwise they are |
|
| 994 | + * - 'limit' - Set a numeric limit for the search results |
|
| 995 | + * - 'offset' - Set the offset for the limited search results |
|
| 996 | + * - 'wildcard' - Whether the search should use wildcards |
|
| 997 | + * @psalm-param array{escape_like_param?: bool, limit?: int, offset?: int, wildcard?: bool} $options |
|
| 998 | + * @return array an array of contacts which are arrays of key-value-pairs |
|
| 999 | + */ |
|
| 1000 | + public function search($addressBookId, $pattern, $searchProperties, $options = []): array { |
|
| 1001 | + return $this->searchByAddressBookIds([$addressBookId], $pattern, $searchProperties, $options); |
|
| 1002 | + } |
|
| 1003 | + |
|
| 1004 | + /** |
|
| 1005 | + * Search contacts in all address-books accessible by a user |
|
| 1006 | + * |
|
| 1007 | + * @param string $principalUri |
|
| 1008 | + * @param string $pattern |
|
| 1009 | + * @param array $searchProperties |
|
| 1010 | + * @param array $options |
|
| 1011 | + * @return array |
|
| 1012 | + */ |
|
| 1013 | + public function searchPrincipalUri(string $principalUri, |
|
| 1014 | + string $pattern, |
|
| 1015 | + array $searchProperties, |
|
| 1016 | + array $options = []): array { |
|
| 1017 | + $addressBookIds = array_map(static function ($row):int { |
|
| 1018 | + return (int) $row['id']; |
|
| 1019 | + }, $this->getAddressBooksForUser($principalUri)); |
|
| 1020 | + |
|
| 1021 | + return $this->searchByAddressBookIds($addressBookIds, $pattern, $searchProperties, $options); |
|
| 1022 | + } |
|
| 1023 | + |
|
| 1024 | + /** |
|
| 1025 | + * @param array $addressBookIds |
|
| 1026 | + * @param string $pattern |
|
| 1027 | + * @param array $searchProperties |
|
| 1028 | + * @param array $options |
|
| 1029 | + * @psalm-param array{types?: bool, escape_like_param?: bool, limit?: int, offset?: int, wildcard?: bool} $options |
|
| 1030 | + * @return array |
|
| 1031 | + */ |
|
| 1032 | + private function searchByAddressBookIds(array $addressBookIds, |
|
| 1033 | + string $pattern, |
|
| 1034 | + array $searchProperties, |
|
| 1035 | + array $options = []): array { |
|
| 1036 | + $escapePattern = !\array_key_exists('escape_like_param', $options) || $options['escape_like_param'] !== false; |
|
| 1037 | + $useWildcards = !\array_key_exists('wildcard', $options) || $options['wildcard'] !== false; |
|
| 1038 | + |
|
| 1039 | + $query2 = $this->db->getQueryBuilder(); |
|
| 1040 | + |
|
| 1041 | + $addressBookOr = $query2->expr()->orX(); |
|
| 1042 | + foreach ($addressBookIds as $addressBookId) { |
|
| 1043 | + $addressBookOr->add($query2->expr()->eq('cp.addressbookid', $query2->createNamedParameter($addressBookId))); |
|
| 1044 | + } |
|
| 1045 | + |
|
| 1046 | + if ($addressBookOr->count() === 0) { |
|
| 1047 | + return []; |
|
| 1048 | + } |
|
| 1049 | + |
|
| 1050 | + $propertyOr = $query2->expr()->orX(); |
|
| 1051 | + foreach ($searchProperties as $property) { |
|
| 1052 | + if ($escapePattern) { |
|
| 1053 | + if ($property === 'EMAIL' && strpos($pattern, ' ') !== false) { |
|
| 1054 | + // There can be no spaces in emails |
|
| 1055 | + continue; |
|
| 1056 | + } |
|
| 1057 | + |
|
| 1058 | + if ($property === 'CLOUD' && preg_match('/[^a-zA-Z0-9 :_.@\/\-\']/', $pattern) === 1) { |
|
| 1059 | + // There can be no chars in cloud ids which are not valid for user ids plus :/ |
|
| 1060 | + // worst case: CA61590A-BBBC-423E-84AF-E6DF01455A53@https://my.nxt/srv/ |
|
| 1061 | + continue; |
|
| 1062 | + } |
|
| 1063 | + } |
|
| 1064 | + |
|
| 1065 | + $propertyOr->add($query2->expr()->eq('cp.name', $query2->createNamedParameter($property))); |
|
| 1066 | + } |
|
| 1067 | + |
|
| 1068 | + if ($propertyOr->count() === 0) { |
|
| 1069 | + return []; |
|
| 1070 | + } |
|
| 1071 | + |
|
| 1072 | + $query2->selectDistinct('cp.cardid') |
|
| 1073 | + ->from($this->dbCardsPropertiesTable, 'cp') |
|
| 1074 | + ->andWhere($addressBookOr) |
|
| 1075 | + ->andWhere($propertyOr); |
|
| 1076 | + |
|
| 1077 | + // No need for like when the pattern is empty |
|
| 1078 | + if ('' !== $pattern) { |
|
| 1079 | + if (!$useWildcards) { |
|
| 1080 | + $query2->andWhere($query2->expr()->eq('cp.value', $query2->createNamedParameter($pattern))); |
|
| 1081 | + } elseif (!$escapePattern) { |
|
| 1082 | + $query2->andWhere($query2->expr()->ilike('cp.value', $query2->createNamedParameter($pattern))); |
|
| 1083 | + } else { |
|
| 1084 | + $query2->andWhere($query2->expr()->ilike('cp.value', $query2->createNamedParameter('%' . $this->db->escapeLikeParameter($pattern) . '%'))); |
|
| 1085 | + } |
|
| 1086 | + } |
|
| 1087 | + |
|
| 1088 | + if (isset($options['limit'])) { |
|
| 1089 | + $query2->setMaxResults($options['limit']); |
|
| 1090 | + } |
|
| 1091 | + if (isset($options['offset'])) { |
|
| 1092 | + $query2->setFirstResult($options['offset']); |
|
| 1093 | + } |
|
| 1094 | + |
|
| 1095 | + $result = $query2->execute(); |
|
| 1096 | + $matches = $result->fetchAll(); |
|
| 1097 | + $result->closeCursor(); |
|
| 1098 | + $matches = array_map(function ($match) { |
|
| 1099 | + return (int)$match['cardid']; |
|
| 1100 | + }, $matches); |
|
| 1101 | + |
|
| 1102 | + $cards = []; |
|
| 1103 | + $query = $this->db->getQueryBuilder(); |
|
| 1104 | + $query->select('c.addressbookid', 'c.carddata', 'c.uri') |
|
| 1105 | + ->from($this->dbCardsTable, 'c') |
|
| 1106 | + ->where($query->expr()->in('c.id', $query->createParameter('matches'))); |
|
| 1107 | + |
|
| 1108 | + foreach (array_chunk($matches, 1000) as $matchesChunk) { |
|
| 1109 | + $query->setParameter('matches', $matchesChunk, IQueryBuilder::PARAM_INT_ARRAY); |
|
| 1110 | + $result = $query->executeQuery(); |
|
| 1111 | + $cards = array_merge($cards, $result->fetchAll()); |
|
| 1112 | + $result->closeCursor(); |
|
| 1113 | + } |
|
| 1114 | + |
|
| 1115 | + return array_map(function ($array) { |
|
| 1116 | + $array['addressbookid'] = (int) $array['addressbookid']; |
|
| 1117 | + $modified = false; |
|
| 1118 | + $array['carddata'] = $this->readBlob($array['carddata'], $modified); |
|
| 1119 | + if ($modified) { |
|
| 1120 | + $array['size'] = strlen($array['carddata']); |
|
| 1121 | + } |
|
| 1122 | + return $array; |
|
| 1123 | + }, $cards); |
|
| 1124 | + } |
|
| 1125 | + |
|
| 1126 | + /** |
|
| 1127 | + * @param int $bookId |
|
| 1128 | + * @param string $name |
|
| 1129 | + * @return array |
|
| 1130 | + */ |
|
| 1131 | + public function collectCardProperties($bookId, $name) { |
|
| 1132 | + $query = $this->db->getQueryBuilder(); |
|
| 1133 | + $result = $query->selectDistinct('value') |
|
| 1134 | + ->from($this->dbCardsPropertiesTable) |
|
| 1135 | + ->where($query->expr()->eq('name', $query->createNamedParameter($name))) |
|
| 1136 | + ->andWhere($query->expr()->eq('addressbookid', $query->createNamedParameter($bookId))) |
|
| 1137 | + ->execute(); |
|
| 1138 | + |
|
| 1139 | + $all = $result->fetchAll(PDO::FETCH_COLUMN); |
|
| 1140 | + $result->closeCursor(); |
|
| 1141 | + |
|
| 1142 | + return $all; |
|
| 1143 | + } |
|
| 1144 | + |
|
| 1145 | + /** |
|
| 1146 | + * get URI from a given contact |
|
| 1147 | + * |
|
| 1148 | + * @param int $id |
|
| 1149 | + * @return string |
|
| 1150 | + */ |
|
| 1151 | + public function getCardUri($id) { |
|
| 1152 | + $query = $this->db->getQueryBuilder(); |
|
| 1153 | + $query->select('uri')->from($this->dbCardsTable) |
|
| 1154 | + ->where($query->expr()->eq('id', $query->createParameter('id'))) |
|
| 1155 | + ->setParameter('id', $id); |
|
| 1156 | + |
|
| 1157 | + $result = $query->execute(); |
|
| 1158 | + $uri = $result->fetch(); |
|
| 1159 | + $result->closeCursor(); |
|
| 1160 | + |
|
| 1161 | + if (!isset($uri['uri'])) { |
|
| 1162 | + throw new \InvalidArgumentException('Card does not exists: ' . $id); |
|
| 1163 | + } |
|
| 1164 | + |
|
| 1165 | + return $uri['uri']; |
|
| 1166 | + } |
|
| 1167 | + |
|
| 1168 | + /** |
|
| 1169 | + * return contact with the given URI |
|
| 1170 | + * |
|
| 1171 | + * @param int $addressBookId |
|
| 1172 | + * @param string $uri |
|
| 1173 | + * @returns array |
|
| 1174 | + */ |
|
| 1175 | + public function getContact($addressBookId, $uri) { |
|
| 1176 | + $result = []; |
|
| 1177 | + $query = $this->db->getQueryBuilder(); |
|
| 1178 | + $query->select('*')->from($this->dbCardsTable) |
|
| 1179 | + ->where($query->expr()->eq('uri', $query->createNamedParameter($uri))) |
|
| 1180 | + ->andWhere($query->expr()->eq('addressbookid', $query->createNamedParameter($addressBookId))); |
|
| 1181 | + $queryResult = $query->execute(); |
|
| 1182 | + $contact = $queryResult->fetch(); |
|
| 1183 | + $queryResult->closeCursor(); |
|
| 1184 | + |
|
| 1185 | + if (is_array($contact)) { |
|
| 1186 | + $modified = false; |
|
| 1187 | + $contact['etag'] = '"' . $contact['etag'] . '"'; |
|
| 1188 | + $contact['carddata'] = $this->readBlob($contact['carddata'], $modified); |
|
| 1189 | + if ($modified) { |
|
| 1190 | + $contact['size'] = strlen($contact['carddata']); |
|
| 1191 | + } |
|
| 1192 | + |
|
| 1193 | + $result = $contact; |
|
| 1194 | + } |
|
| 1195 | + |
|
| 1196 | + return $result; |
|
| 1197 | + } |
|
| 1198 | + |
|
| 1199 | + /** |
|
| 1200 | + * Returns the list of people whom this address book is shared with. |
|
| 1201 | + * |
|
| 1202 | + * Every element in this array should have the following properties: |
|
| 1203 | + * * href - Often a mailto: address |
|
| 1204 | + * * commonName - Optional, for example a first + last name |
|
| 1205 | + * * status - See the Sabre\CalDAV\SharingPlugin::STATUS_ constants. |
|
| 1206 | + * * readOnly - boolean |
|
| 1207 | + * |
|
| 1208 | + * @return list<array{href: string, commonName: string, status: int, readOnly: bool, '{http://owncloud.org/ns}principal': string, '{http://owncloud.org/ns}group-share': bool}> |
|
| 1209 | + */ |
|
| 1210 | + public function getShares(int $addressBookId): array { |
|
| 1211 | + return $this->sharingBackend->getShares($addressBookId); |
|
| 1212 | + } |
|
| 1213 | + |
|
| 1214 | + /** |
|
| 1215 | + * update properties table |
|
| 1216 | + * |
|
| 1217 | + * @param int $addressBookId |
|
| 1218 | + * @param string $cardUri |
|
| 1219 | + * @param string $vCardSerialized |
|
| 1220 | + */ |
|
| 1221 | + protected function updateProperties($addressBookId, $cardUri, $vCardSerialized) { |
|
| 1222 | + $cardId = $this->getCardId($addressBookId, $cardUri); |
|
| 1223 | + $vCard = $this->readCard($vCardSerialized); |
|
| 1224 | + |
|
| 1225 | + $this->purgeProperties($addressBookId, $cardId); |
|
| 1226 | + |
|
| 1227 | + $query = $this->db->getQueryBuilder(); |
|
| 1228 | + $query->insert($this->dbCardsPropertiesTable) |
|
| 1229 | + ->values( |
|
| 1230 | + [ |
|
| 1231 | + 'addressbookid' => $query->createNamedParameter($addressBookId), |
|
| 1232 | + 'cardid' => $query->createNamedParameter($cardId), |
|
| 1233 | + 'name' => $query->createParameter('name'), |
|
| 1234 | + 'value' => $query->createParameter('value'), |
|
| 1235 | + 'preferred' => $query->createParameter('preferred') |
|
| 1236 | + ] |
|
| 1237 | + ); |
|
| 1238 | + |
|
| 1239 | + |
|
| 1240 | + $this->db->beginTransaction(); |
|
| 1241 | + |
|
| 1242 | + try { |
|
| 1243 | + foreach ($vCard->children() as $property) { |
|
| 1244 | + if (!in_array($property->name, self::$indexProperties)) { |
|
| 1245 | + continue; |
|
| 1246 | + } |
|
| 1247 | + $preferred = 0; |
|
| 1248 | + foreach ($property->parameters as $parameter) { |
|
| 1249 | + if ($parameter->name === 'TYPE' && strtoupper($parameter->getValue()) === 'PREF') { |
|
| 1250 | + $preferred = 1; |
|
| 1251 | + break; |
|
| 1252 | + } |
|
| 1253 | + } |
|
| 1254 | + $query->setParameter('name', $property->name); |
|
| 1255 | + $query->setParameter('value', mb_strcut($property->getValue(), 0, 254)); |
|
| 1256 | + $query->setParameter('preferred', $preferred); |
|
| 1257 | + $query->execute(); |
|
| 1258 | + } |
|
| 1259 | + $this->db->commit(); |
|
| 1260 | + } catch (\Exception $e) { |
|
| 1261 | + $this->db->rollBack(); |
|
| 1262 | + } |
|
| 1263 | + } |
|
| 1264 | + |
|
| 1265 | + /** |
|
| 1266 | + * read vCard data into a vCard object |
|
| 1267 | + * |
|
| 1268 | + * @param string $cardData |
|
| 1269 | + * @return VCard |
|
| 1270 | + */ |
|
| 1271 | + protected function readCard($cardData) { |
|
| 1272 | + return Reader::read($cardData); |
|
| 1273 | + } |
|
| 1274 | + |
|
| 1275 | + /** |
|
| 1276 | + * delete all properties from a given card |
|
| 1277 | + * |
|
| 1278 | + * @param int $addressBookId |
|
| 1279 | + * @param int $cardId |
|
| 1280 | + */ |
|
| 1281 | + protected function purgeProperties($addressBookId, $cardId) { |
|
| 1282 | + $query = $this->db->getQueryBuilder(); |
|
| 1283 | + $query->delete($this->dbCardsPropertiesTable) |
|
| 1284 | + ->where($query->expr()->eq('cardid', $query->createNamedParameter($cardId))) |
|
| 1285 | + ->andWhere($query->expr()->eq('addressbookid', $query->createNamedParameter($addressBookId))); |
|
| 1286 | + $query->execute(); |
|
| 1287 | + } |
|
| 1288 | + |
|
| 1289 | + /** |
|
| 1290 | + * Get ID from a given contact |
|
| 1291 | + */ |
|
| 1292 | + protected function getCardId(int $addressBookId, string $uri): int { |
|
| 1293 | + $query = $this->db->getQueryBuilder(); |
|
| 1294 | + $query->select('id')->from($this->dbCardsTable) |
|
| 1295 | + ->where($query->expr()->eq('uri', $query->createNamedParameter($uri))) |
|
| 1296 | + ->andWhere($query->expr()->eq('addressbookid', $query->createNamedParameter($addressBookId))); |
|
| 1297 | + |
|
| 1298 | + $result = $query->execute(); |
|
| 1299 | + $cardIds = $result->fetch(); |
|
| 1300 | + $result->closeCursor(); |
|
| 1301 | + |
|
| 1302 | + if (!isset($cardIds['id'])) { |
|
| 1303 | + throw new \InvalidArgumentException('Card does not exists: ' . $uri); |
|
| 1304 | + } |
|
| 1305 | + |
|
| 1306 | + return (int)$cardIds['id']; |
|
| 1307 | + } |
|
| 1308 | + |
|
| 1309 | + /** |
|
| 1310 | + * For shared address books the sharee is set in the ACL of the address book |
|
| 1311 | + * |
|
| 1312 | + * @param int $addressBookId |
|
| 1313 | + * @param list<array{privilege: string, principal: string, protected: bool}> $acl |
|
| 1314 | + * @return list<array{privilege: string, principal: string, protected: bool}> |
|
| 1315 | + */ |
|
| 1316 | + public function applyShareAcl(int $addressBookId, array $acl): array { |
|
| 1317 | + return $this->sharingBackend->applyShareAcl($addressBookId, $acl); |
|
| 1318 | + } |
|
| 1319 | + |
|
| 1320 | + private function convertPrincipal(string $principalUri, bool $toV2): string { |
|
| 1321 | + if ($this->principalBackend->getPrincipalPrefix() === 'principals') { |
|
| 1322 | + [, $name] = \Sabre\Uri\split($principalUri); |
|
| 1323 | + if ($toV2 === true) { |
|
| 1324 | + return "principals/users/$name"; |
|
| 1325 | + } |
|
| 1326 | + return "principals/$name"; |
|
| 1327 | + } |
|
| 1328 | + return $principalUri; |
|
| 1329 | + } |
|
| 1330 | + |
|
| 1331 | + private function addOwnerPrincipal(array &$addressbookInfo): void { |
|
| 1332 | + $ownerPrincipalKey = '{' . \OCA\DAV\DAV\Sharing\Plugin::NS_OWNCLOUD . '}owner-principal'; |
|
| 1333 | + $displaynameKey = '{' . \OCA\DAV\DAV\Sharing\Plugin::NS_NEXTCLOUD . '}owner-displayname'; |
|
| 1334 | + if (isset($addressbookInfo[$ownerPrincipalKey])) { |
|
| 1335 | + $uri = $addressbookInfo[$ownerPrincipalKey]; |
|
| 1336 | + } else { |
|
| 1337 | + $uri = $addressbookInfo['principaluri']; |
|
| 1338 | + } |
|
| 1339 | + |
|
| 1340 | + $principalInformation = $this->principalBackend->getPrincipalByPath($uri); |
|
| 1341 | + if (isset($principalInformation['{DAV:}displayname'])) { |
|
| 1342 | + $addressbookInfo[$displaynameKey] = $principalInformation['{DAV:}displayname']; |
|
| 1343 | + } |
|
| 1344 | + } |
|
| 1345 | + |
|
| 1346 | + /** |
|
| 1347 | + * Extract UID from vcard |
|
| 1348 | + * |
|
| 1349 | + * @param string $cardData the vcard raw data |
|
| 1350 | + * @return string the uid |
|
| 1351 | + * @throws BadRequest if no UID is available or vcard is empty |
|
| 1352 | + */ |
|
| 1353 | + private function getUID(string $cardData): string { |
|
| 1354 | + if ($cardData !== '') { |
|
| 1355 | + $vCard = Reader::read($cardData); |
|
| 1356 | + if ($vCard->UID) { |
|
| 1357 | + $uid = $vCard->UID->getValue(); |
|
| 1358 | + return $uid; |
|
| 1359 | + } |
|
| 1360 | + // should already be handled, but just in case |
|
| 1361 | + throw new BadRequest('vCards on CardDAV servers MUST have a UID property'); |
|
| 1362 | + } |
|
| 1363 | + // should already be handled, but just in case |
|
| 1364 | + throw new BadRequest('vCard can not be empty'); |
|
| 1365 | + } |
|
| 1366 | 1366 | } |
@@ -119,3069 +119,3069 @@ |
||
| 119 | 119 | * @package OCA\DAV\CalDAV |
| 120 | 120 | */ |
| 121 | 121 | class CalDavBackend extends AbstractBackend implements SyncSupport, SubscriptionSupport, SchedulingSupport { |
| 122 | - public const CALENDAR_TYPE_CALENDAR = 0; |
|
| 123 | - public const CALENDAR_TYPE_SUBSCRIPTION = 1; |
|
| 124 | - |
|
| 125 | - public const PERSONAL_CALENDAR_URI = 'personal'; |
|
| 126 | - public const PERSONAL_CALENDAR_NAME = 'Personal'; |
|
| 127 | - |
|
| 128 | - public const RESOURCE_BOOKING_CALENDAR_URI = 'calendar'; |
|
| 129 | - public const RESOURCE_BOOKING_CALENDAR_NAME = 'Calendar'; |
|
| 130 | - |
|
| 131 | - /** |
|
| 132 | - * We need to specify a max date, because we need to stop *somewhere* |
|
| 133 | - * |
|
| 134 | - * On 32 bit system the maximum for a signed integer is 2147483647, so |
|
| 135 | - * MAX_DATE cannot be higher than date('Y-m-d', 2147483647) which results |
|
| 136 | - * in 2038-01-19 to avoid problems when the date is converted |
|
| 137 | - * to a unix timestamp. |
|
| 138 | - */ |
|
| 139 | - public const MAX_DATE = '2038-01-01'; |
|
| 140 | - |
|
| 141 | - public const ACCESS_PUBLIC = 4; |
|
| 142 | - public const CLASSIFICATION_PUBLIC = 0; |
|
| 143 | - public const CLASSIFICATION_PRIVATE = 1; |
|
| 144 | - public const CLASSIFICATION_CONFIDENTIAL = 2; |
|
| 145 | - |
|
| 146 | - /** |
|
| 147 | - * List of CalDAV properties, and how they map to database field names and their type |
|
| 148 | - * Add your own properties by simply adding on to this array. |
|
| 149 | - * |
|
| 150 | - * @var array |
|
| 151 | - * @psalm-var array<string, string[]> |
|
| 152 | - */ |
|
| 153 | - public array $propertyMap = [ |
|
| 154 | - '{DAV:}displayname' => ['displayname', 'string'], |
|
| 155 | - '{urn:ietf:params:xml:ns:caldav}calendar-description' => ['description', 'string'], |
|
| 156 | - '{urn:ietf:params:xml:ns:caldav}calendar-timezone' => ['timezone', 'string'], |
|
| 157 | - '{http://apple.com/ns/ical/}calendar-order' => ['calendarorder', 'int'], |
|
| 158 | - '{http://apple.com/ns/ical/}calendar-color' => ['calendarcolor', 'string'], |
|
| 159 | - '{' . \OCA\DAV\DAV\Sharing\Plugin::NS_NEXTCLOUD . '}deleted-at' => ['deleted_at', 'int'], |
|
| 160 | - ]; |
|
| 161 | - |
|
| 162 | - /** |
|
| 163 | - * List of subscription properties, and how they map to database field names. |
|
| 164 | - * |
|
| 165 | - * @var array |
|
| 166 | - */ |
|
| 167 | - public array $subscriptionPropertyMap = [ |
|
| 168 | - '{DAV:}displayname' => ['displayname', 'string'], |
|
| 169 | - '{http://apple.com/ns/ical/}refreshrate' => ['refreshrate', 'string'], |
|
| 170 | - '{http://apple.com/ns/ical/}calendar-order' => ['calendarorder', 'int'], |
|
| 171 | - '{http://apple.com/ns/ical/}calendar-color' => ['calendarcolor', 'string'], |
|
| 172 | - '{http://calendarserver.org/ns/}subscribed-strip-todos' => ['striptodos', 'bool'], |
|
| 173 | - '{http://calendarserver.org/ns/}subscribed-strip-alarms' => ['stripalarms', 'string'], |
|
| 174 | - '{http://calendarserver.org/ns/}subscribed-strip-attachments' => ['stripattachments', 'string'], |
|
| 175 | - ]; |
|
| 176 | - |
|
| 177 | - /** |
|
| 178 | - * properties to index |
|
| 179 | - * |
|
| 180 | - * This list has to be kept in sync with ICalendarQuery::SEARCH_PROPERTY_* |
|
| 181 | - * |
|
| 182 | - * @see \OCP\Calendar\ICalendarQuery |
|
| 183 | - */ |
|
| 184 | - private const INDEXED_PROPERTIES = [ |
|
| 185 | - 'CATEGORIES', |
|
| 186 | - 'COMMENT', |
|
| 187 | - 'DESCRIPTION', |
|
| 188 | - 'LOCATION', |
|
| 189 | - 'RESOURCES', |
|
| 190 | - 'STATUS', |
|
| 191 | - 'SUMMARY', |
|
| 192 | - 'ATTENDEE', |
|
| 193 | - 'CONTACT', |
|
| 194 | - 'ORGANIZER' |
|
| 195 | - ]; |
|
| 196 | - |
|
| 197 | - /** @var array parameters to index */ |
|
| 198 | - public static array $indexParameters = [ |
|
| 199 | - 'ATTENDEE' => ['CN'], |
|
| 200 | - 'ORGANIZER' => ['CN'], |
|
| 201 | - ]; |
|
| 202 | - |
|
| 203 | - /** |
|
| 204 | - * @var string[] Map of uid => display name |
|
| 205 | - */ |
|
| 206 | - protected array $userDisplayNames; |
|
| 207 | - |
|
| 208 | - private IDBConnection $db; |
|
| 209 | - private Backend $calendarSharingBackend; |
|
| 210 | - private Principal $principalBackend; |
|
| 211 | - private IUserManager $userManager; |
|
| 212 | - private ISecureRandom $random; |
|
| 213 | - private LoggerInterface $logger; |
|
| 214 | - private IEventDispatcher $dispatcher; |
|
| 215 | - private IConfig $config; |
|
| 216 | - private bool $legacyEndpoint; |
|
| 217 | - private string $dbObjectPropertiesTable = 'calendarobjects_props'; |
|
| 218 | - |
|
| 219 | - public function __construct(IDBConnection $db, |
|
| 220 | - Principal $principalBackend, |
|
| 221 | - IUserManager $userManager, |
|
| 222 | - IGroupManager $groupManager, |
|
| 223 | - ISecureRandom $random, |
|
| 224 | - LoggerInterface $logger, |
|
| 225 | - IEventDispatcher $dispatcher, |
|
| 226 | - IConfig $config, |
|
| 227 | - bool $legacyEndpoint = false) { |
|
| 228 | - $this->db = $db; |
|
| 229 | - $this->principalBackend = $principalBackend; |
|
| 230 | - $this->userManager = $userManager; |
|
| 231 | - $this->calendarSharingBackend = new Backend($this->db, $this->userManager, $groupManager, $principalBackend, 'calendar'); |
|
| 232 | - $this->random = $random; |
|
| 233 | - $this->logger = $logger; |
|
| 234 | - $this->dispatcher = $dispatcher; |
|
| 235 | - $this->config = $config; |
|
| 236 | - $this->legacyEndpoint = $legacyEndpoint; |
|
| 237 | - } |
|
| 238 | - |
|
| 239 | - /** |
|
| 240 | - * Return the number of calendars for a principal |
|
| 241 | - * |
|
| 242 | - * By default this excludes the automatically generated birthday calendar |
|
| 243 | - * |
|
| 244 | - * @param $principalUri |
|
| 245 | - * @param bool $excludeBirthday |
|
| 246 | - * @return int |
|
| 247 | - */ |
|
| 248 | - public function getCalendarsForUserCount($principalUri, $excludeBirthday = true) { |
|
| 249 | - $principalUri = $this->convertPrincipal($principalUri, true); |
|
| 250 | - $query = $this->db->getQueryBuilder(); |
|
| 251 | - $query->select($query->func()->count('*')) |
|
| 252 | - ->from('calendars'); |
|
| 253 | - |
|
| 254 | - if ($principalUri === '') { |
|
| 255 | - $query->where($query->expr()->emptyString('principaluri')); |
|
| 256 | - } else { |
|
| 257 | - $query->where($query->expr()->eq('principaluri', $query->createNamedParameter($principalUri))); |
|
| 258 | - } |
|
| 259 | - |
|
| 260 | - if ($excludeBirthday) { |
|
| 261 | - $query->andWhere($query->expr()->neq('uri', $query->createNamedParameter(BirthdayService::BIRTHDAY_CALENDAR_URI))); |
|
| 262 | - } |
|
| 263 | - |
|
| 264 | - $result = $query->executeQuery(); |
|
| 265 | - $column = (int)$result->fetchOne(); |
|
| 266 | - $result->closeCursor(); |
|
| 267 | - return $column; |
|
| 268 | - } |
|
| 269 | - |
|
| 270 | - /** |
|
| 271 | - * @return array{id: int, deleted_at: int}[] |
|
| 272 | - */ |
|
| 273 | - public function getDeletedCalendars(int $deletedBefore): array { |
|
| 274 | - $qb = $this->db->getQueryBuilder(); |
|
| 275 | - $qb->select(['id', 'deleted_at']) |
|
| 276 | - ->from('calendars') |
|
| 277 | - ->where($qb->expr()->isNotNull('deleted_at')) |
|
| 278 | - ->andWhere($qb->expr()->lt('deleted_at', $qb->createNamedParameter($deletedBefore))); |
|
| 279 | - $result = $qb->executeQuery(); |
|
| 280 | - $raw = $result->fetchAll(); |
|
| 281 | - $result->closeCursor(); |
|
| 282 | - return array_map(function ($row) { |
|
| 283 | - return [ |
|
| 284 | - 'id' => (int) $row['id'], |
|
| 285 | - 'deleted_at' => (int) $row['deleted_at'], |
|
| 286 | - ]; |
|
| 287 | - }, $raw); |
|
| 288 | - } |
|
| 289 | - |
|
| 290 | - /** |
|
| 291 | - * Returns a list of calendars for a principal. |
|
| 292 | - * |
|
| 293 | - * Every project is an array with the following keys: |
|
| 294 | - * * id, a unique id that will be used by other functions to modify the |
|
| 295 | - * calendar. This can be the same as the uri or a database key. |
|
| 296 | - * * uri, which the basename of the uri with which the calendar is |
|
| 297 | - * accessed. |
|
| 298 | - * * principaluri. The owner of the calendar. Almost always the same as |
|
| 299 | - * principalUri passed to this method. |
|
| 300 | - * |
|
| 301 | - * Furthermore it can contain webdav properties in clark notation. A very |
|
| 302 | - * common one is '{DAV:}displayname'. |
|
| 303 | - * |
|
| 304 | - * Many clients also require: |
|
| 305 | - * {urn:ietf:params:xml:ns:caldav}supported-calendar-component-set |
|
| 306 | - * For this property, you can just return an instance of |
|
| 307 | - * Sabre\CalDAV\Property\SupportedCalendarComponentSet. |
|
| 308 | - * |
|
| 309 | - * If you return {http://sabredav.org/ns}read-only and set the value to 1, |
|
| 310 | - * ACL will automatically be put in read-only mode. |
|
| 311 | - * |
|
| 312 | - * @param string $principalUri |
|
| 313 | - * @return array |
|
| 314 | - */ |
|
| 315 | - public function getCalendarsForUser($principalUri) { |
|
| 316 | - $principalUriOriginal = $principalUri; |
|
| 317 | - $principalUri = $this->convertPrincipal($principalUri, true); |
|
| 318 | - $fields = array_column($this->propertyMap, 0); |
|
| 319 | - $fields[] = 'id'; |
|
| 320 | - $fields[] = 'uri'; |
|
| 321 | - $fields[] = 'synctoken'; |
|
| 322 | - $fields[] = 'components'; |
|
| 323 | - $fields[] = 'principaluri'; |
|
| 324 | - $fields[] = 'transparent'; |
|
| 325 | - |
|
| 326 | - // Making fields a comma-delimited list |
|
| 327 | - $query = $this->db->getQueryBuilder(); |
|
| 328 | - $query->select($fields) |
|
| 329 | - ->from('calendars') |
|
| 330 | - ->orderBy('calendarorder', 'ASC'); |
|
| 331 | - |
|
| 332 | - if ($principalUri === '') { |
|
| 333 | - $query->where($query->expr()->emptyString('principaluri')); |
|
| 334 | - } else { |
|
| 335 | - $query->where($query->expr()->eq('principaluri', $query->createNamedParameter($principalUri))); |
|
| 336 | - } |
|
| 337 | - |
|
| 338 | - $result = $query->executeQuery(); |
|
| 339 | - |
|
| 340 | - $calendars = []; |
|
| 341 | - while ($row = $result->fetch()) { |
|
| 342 | - $row['principaluri'] = (string) $row['principaluri']; |
|
| 343 | - $components = []; |
|
| 344 | - if ($row['components']) { |
|
| 345 | - $components = explode(',',$row['components']); |
|
| 346 | - } |
|
| 347 | - |
|
| 348 | - $calendar = [ |
|
| 349 | - 'id' => $row['id'], |
|
| 350 | - 'uri' => $row['uri'], |
|
| 351 | - 'principaluri' => $this->convertPrincipal($row['principaluri'], !$this->legacyEndpoint), |
|
| 352 | - '{' . Plugin::NS_CALENDARSERVER . '}getctag' => 'http://sabre.io/ns/sync/' . ($row['synctoken']?$row['synctoken']:'0'), |
|
| 353 | - '{http://sabredav.org/ns}sync-token' => $row['synctoken']?$row['synctoken']:'0', |
|
| 354 | - '{' . Plugin::NS_CALDAV . '}supported-calendar-component-set' => new SupportedCalendarComponentSet($components), |
|
| 355 | - '{' . Plugin::NS_CALDAV . '}schedule-calendar-transp' => new ScheduleCalendarTransp($row['transparent']?'transparent':'opaque'), |
|
| 356 | - '{' . \OCA\DAV\DAV\Sharing\Plugin::NS_OWNCLOUD . '}owner-principal' => $this->convertPrincipal($principalUri, !$this->legacyEndpoint), |
|
| 357 | - ]; |
|
| 358 | - |
|
| 359 | - $calendar = $this->rowToCalendar($row, $calendar); |
|
| 360 | - $calendar = $this->addOwnerPrincipalToCalendar($calendar); |
|
| 361 | - $calendar = $this->addResourceTypeToCalendar($row, $calendar); |
|
| 362 | - |
|
| 363 | - if (!isset($calendars[$calendar['id']])) { |
|
| 364 | - $calendars[$calendar['id']] = $calendar; |
|
| 365 | - } |
|
| 366 | - } |
|
| 367 | - $result->closeCursor(); |
|
| 368 | - |
|
| 369 | - // query for shared calendars |
|
| 370 | - $principals = $this->principalBackend->getGroupMembership($principalUriOriginal, true); |
|
| 371 | - $principals = array_merge($principals, $this->principalBackend->getCircleMembership($principalUriOriginal)); |
|
| 372 | - |
|
| 373 | - $principals[] = $principalUri; |
|
| 374 | - |
|
| 375 | - $fields = array_column($this->propertyMap, 0); |
|
| 376 | - $fields[] = 'a.id'; |
|
| 377 | - $fields[] = 'a.uri'; |
|
| 378 | - $fields[] = 'a.synctoken'; |
|
| 379 | - $fields[] = 'a.components'; |
|
| 380 | - $fields[] = 'a.principaluri'; |
|
| 381 | - $fields[] = 'a.transparent'; |
|
| 382 | - $fields[] = 's.access'; |
|
| 383 | - $query = $this->db->getQueryBuilder(); |
|
| 384 | - $query->select($fields) |
|
| 385 | - ->from('dav_shares', 's') |
|
| 386 | - ->join('s', 'calendars', 'a', $query->expr()->eq('s.resourceid', 'a.id')) |
|
| 387 | - ->where($query->expr()->in('s.principaluri', $query->createParameter('principaluri'))) |
|
| 388 | - ->andWhere($query->expr()->eq('s.type', $query->createParameter('type'))) |
|
| 389 | - ->setParameter('type', 'calendar') |
|
| 390 | - ->setParameter('principaluri', $principals, \Doctrine\DBAL\Connection::PARAM_STR_ARRAY); |
|
| 391 | - |
|
| 392 | - $result = $query->executeQuery(); |
|
| 393 | - |
|
| 394 | - $readOnlyPropertyName = '{' . \OCA\DAV\DAV\Sharing\Plugin::NS_OWNCLOUD . '}read-only'; |
|
| 395 | - while ($row = $result->fetch()) { |
|
| 396 | - $row['principaluri'] = (string) $row['principaluri']; |
|
| 397 | - if ($row['principaluri'] === $principalUri) { |
|
| 398 | - continue; |
|
| 399 | - } |
|
| 400 | - |
|
| 401 | - $readOnly = (int) $row['access'] === Backend::ACCESS_READ; |
|
| 402 | - if (isset($calendars[$row['id']])) { |
|
| 403 | - if ($readOnly) { |
|
| 404 | - // New share can not have more permissions then the old one. |
|
| 405 | - continue; |
|
| 406 | - } |
|
| 407 | - if (isset($calendars[$row['id']][$readOnlyPropertyName]) && |
|
| 408 | - $calendars[$row['id']][$readOnlyPropertyName] === 0) { |
|
| 409 | - // Old share is already read-write, no more permissions can be gained |
|
| 410 | - continue; |
|
| 411 | - } |
|
| 412 | - } |
|
| 413 | - |
|
| 414 | - [, $name] = Uri\split($row['principaluri']); |
|
| 415 | - $uri = $row['uri'] . '_shared_by_' . $name; |
|
| 416 | - $row['displayname'] = $row['displayname'] . ' (' . $this->getUserDisplayName($name) . ')'; |
|
| 417 | - $components = []; |
|
| 418 | - if ($row['components']) { |
|
| 419 | - $components = explode(',',$row['components']); |
|
| 420 | - } |
|
| 421 | - $calendar = [ |
|
| 422 | - 'id' => $row['id'], |
|
| 423 | - 'uri' => $uri, |
|
| 424 | - 'principaluri' => $this->convertPrincipal($principalUri, !$this->legacyEndpoint), |
|
| 425 | - '{' . Plugin::NS_CALENDARSERVER . '}getctag' => 'http://sabre.io/ns/sync/' . ($row['synctoken']?$row['synctoken']:'0'), |
|
| 426 | - '{http://sabredav.org/ns}sync-token' => $row['synctoken']?$row['synctoken']:'0', |
|
| 427 | - '{' . Plugin::NS_CALDAV . '}supported-calendar-component-set' => new SupportedCalendarComponentSet($components), |
|
| 428 | - '{' . Plugin::NS_CALDAV . '}schedule-calendar-transp' => new ScheduleCalendarTransp('transparent'), |
|
| 429 | - '{' . \OCA\DAV\DAV\Sharing\Plugin::NS_OWNCLOUD . '}owner-principal' => $this->convertPrincipal($row['principaluri'], !$this->legacyEndpoint), |
|
| 430 | - $readOnlyPropertyName => $readOnly, |
|
| 431 | - ]; |
|
| 432 | - |
|
| 433 | - $calendar = $this->rowToCalendar($row, $calendar); |
|
| 434 | - $calendar = $this->addOwnerPrincipalToCalendar($calendar); |
|
| 435 | - $calendar = $this->addResourceTypeToCalendar($row, $calendar); |
|
| 436 | - |
|
| 437 | - $calendars[$calendar['id']] = $calendar; |
|
| 438 | - } |
|
| 439 | - $result->closeCursor(); |
|
| 440 | - |
|
| 441 | - return array_values($calendars); |
|
| 442 | - } |
|
| 443 | - |
|
| 444 | - /** |
|
| 445 | - * @param $principalUri |
|
| 446 | - * @return array |
|
| 447 | - */ |
|
| 448 | - public function getUsersOwnCalendars($principalUri) { |
|
| 449 | - $principalUri = $this->convertPrincipal($principalUri, true); |
|
| 450 | - $fields = array_column($this->propertyMap, 0); |
|
| 451 | - $fields[] = 'id'; |
|
| 452 | - $fields[] = 'uri'; |
|
| 453 | - $fields[] = 'synctoken'; |
|
| 454 | - $fields[] = 'components'; |
|
| 455 | - $fields[] = 'principaluri'; |
|
| 456 | - $fields[] = 'transparent'; |
|
| 457 | - // Making fields a comma-delimited list |
|
| 458 | - $query = $this->db->getQueryBuilder(); |
|
| 459 | - $query->select($fields)->from('calendars') |
|
| 460 | - ->where($query->expr()->eq('principaluri', $query->createNamedParameter($principalUri))) |
|
| 461 | - ->orderBy('calendarorder', 'ASC'); |
|
| 462 | - $stmt = $query->executeQuery(); |
|
| 463 | - $calendars = []; |
|
| 464 | - while ($row = $stmt->fetch()) { |
|
| 465 | - $row['principaluri'] = (string) $row['principaluri']; |
|
| 466 | - $components = []; |
|
| 467 | - if ($row['components']) { |
|
| 468 | - $components = explode(',',$row['components']); |
|
| 469 | - } |
|
| 470 | - $calendar = [ |
|
| 471 | - 'id' => $row['id'], |
|
| 472 | - 'uri' => $row['uri'], |
|
| 473 | - 'principaluri' => $this->convertPrincipal($row['principaluri'], !$this->legacyEndpoint), |
|
| 474 | - '{' . Plugin::NS_CALENDARSERVER . '}getctag' => 'http://sabre.io/ns/sync/' . ($row['synctoken']?$row['synctoken']:'0'), |
|
| 475 | - '{http://sabredav.org/ns}sync-token' => $row['synctoken']?$row['synctoken']:'0', |
|
| 476 | - '{' . Plugin::NS_CALDAV . '}supported-calendar-component-set' => new SupportedCalendarComponentSet($components), |
|
| 477 | - '{' . Plugin::NS_CALDAV . '}schedule-calendar-transp' => new ScheduleCalendarTransp($row['transparent']?'transparent':'opaque'), |
|
| 478 | - ]; |
|
| 479 | - |
|
| 480 | - $calendar = $this->rowToCalendar($row, $calendar); |
|
| 481 | - $calendar = $this->addOwnerPrincipalToCalendar($calendar); |
|
| 482 | - $calendar = $this->addResourceTypeToCalendar($row, $calendar); |
|
| 483 | - |
|
| 484 | - if (!isset($calendars[$calendar['id']])) { |
|
| 485 | - $calendars[$calendar['id']] = $calendar; |
|
| 486 | - } |
|
| 487 | - } |
|
| 488 | - $stmt->closeCursor(); |
|
| 489 | - return array_values($calendars); |
|
| 490 | - } |
|
| 491 | - |
|
| 492 | - |
|
| 493 | - /** |
|
| 494 | - * @param $uid |
|
| 495 | - * @return string |
|
| 496 | - */ |
|
| 497 | - private function getUserDisplayName($uid) { |
|
| 498 | - if (!isset($this->userDisplayNames[$uid])) { |
|
| 499 | - $user = $this->userManager->get($uid); |
|
| 500 | - |
|
| 501 | - if ($user instanceof IUser) { |
|
| 502 | - $this->userDisplayNames[$uid] = $user->getDisplayName(); |
|
| 503 | - } else { |
|
| 504 | - $this->userDisplayNames[$uid] = $uid; |
|
| 505 | - } |
|
| 506 | - } |
|
| 507 | - |
|
| 508 | - return $this->userDisplayNames[$uid]; |
|
| 509 | - } |
|
| 510 | - |
|
| 511 | - /** |
|
| 512 | - * @return array |
|
| 513 | - */ |
|
| 514 | - public function getPublicCalendars() { |
|
| 515 | - $fields = array_column($this->propertyMap, 0); |
|
| 516 | - $fields[] = 'a.id'; |
|
| 517 | - $fields[] = 'a.uri'; |
|
| 518 | - $fields[] = 'a.synctoken'; |
|
| 519 | - $fields[] = 'a.components'; |
|
| 520 | - $fields[] = 'a.principaluri'; |
|
| 521 | - $fields[] = 'a.transparent'; |
|
| 522 | - $fields[] = 's.access'; |
|
| 523 | - $fields[] = 's.publicuri'; |
|
| 524 | - $calendars = []; |
|
| 525 | - $query = $this->db->getQueryBuilder(); |
|
| 526 | - $result = $query->select($fields) |
|
| 527 | - ->from('dav_shares', 's') |
|
| 528 | - ->join('s', 'calendars', 'a', $query->expr()->eq('s.resourceid', 'a.id')) |
|
| 529 | - ->where($query->expr()->in('s.access', $query->createNamedParameter(self::ACCESS_PUBLIC))) |
|
| 530 | - ->andWhere($query->expr()->eq('s.type', $query->createNamedParameter('calendar'))) |
|
| 531 | - ->executeQuery(); |
|
| 532 | - |
|
| 533 | - while ($row = $result->fetch()) { |
|
| 534 | - $row['principaluri'] = (string) $row['principaluri']; |
|
| 535 | - [, $name] = Uri\split($row['principaluri']); |
|
| 536 | - $row['displayname'] = $row['displayname'] . "($name)"; |
|
| 537 | - $components = []; |
|
| 538 | - if ($row['components']) { |
|
| 539 | - $components = explode(',',$row['components']); |
|
| 540 | - } |
|
| 541 | - $calendar = [ |
|
| 542 | - 'id' => $row['id'], |
|
| 543 | - 'uri' => $row['publicuri'], |
|
| 544 | - 'principaluri' => $this->convertPrincipal($row['principaluri'], !$this->legacyEndpoint), |
|
| 545 | - '{' . Plugin::NS_CALENDARSERVER . '}getctag' => 'http://sabre.io/ns/sync/' . ($row['synctoken']?$row['synctoken']:'0'), |
|
| 546 | - '{http://sabredav.org/ns}sync-token' => $row['synctoken']?$row['synctoken']:'0', |
|
| 547 | - '{' . Plugin::NS_CALDAV . '}supported-calendar-component-set' => new SupportedCalendarComponentSet($components), |
|
| 548 | - '{' . Plugin::NS_CALDAV . '}schedule-calendar-transp' => new ScheduleCalendarTransp($row['transparent']?'transparent':'opaque'), |
|
| 549 | - '{' . \OCA\DAV\DAV\Sharing\Plugin::NS_OWNCLOUD . '}owner-principal' => $this->convertPrincipal($row['principaluri'], $this->legacyEndpoint), |
|
| 550 | - '{' . \OCA\DAV\DAV\Sharing\Plugin::NS_OWNCLOUD . '}read-only' => (int)$row['access'] === Backend::ACCESS_READ, |
|
| 551 | - '{' . \OCA\DAV\DAV\Sharing\Plugin::NS_OWNCLOUD . '}public' => (int)$row['access'] === self::ACCESS_PUBLIC, |
|
| 552 | - ]; |
|
| 553 | - |
|
| 554 | - $calendar = $this->rowToCalendar($row, $calendar); |
|
| 555 | - $calendar = $this->addOwnerPrincipalToCalendar($calendar); |
|
| 556 | - $calendar = $this->addResourceTypeToCalendar($row, $calendar); |
|
| 557 | - |
|
| 558 | - if (!isset($calendars[$calendar['id']])) { |
|
| 559 | - $calendars[$calendar['id']] = $calendar; |
|
| 560 | - } |
|
| 561 | - } |
|
| 562 | - $result->closeCursor(); |
|
| 563 | - |
|
| 564 | - return array_values($calendars); |
|
| 565 | - } |
|
| 566 | - |
|
| 567 | - /** |
|
| 568 | - * @param string $uri |
|
| 569 | - * @return array |
|
| 570 | - * @throws NotFound |
|
| 571 | - */ |
|
| 572 | - public function getPublicCalendar($uri) { |
|
| 573 | - $fields = array_column($this->propertyMap, 0); |
|
| 574 | - $fields[] = 'a.id'; |
|
| 575 | - $fields[] = 'a.uri'; |
|
| 576 | - $fields[] = 'a.synctoken'; |
|
| 577 | - $fields[] = 'a.components'; |
|
| 578 | - $fields[] = 'a.principaluri'; |
|
| 579 | - $fields[] = 'a.transparent'; |
|
| 580 | - $fields[] = 's.access'; |
|
| 581 | - $fields[] = 's.publicuri'; |
|
| 582 | - $query = $this->db->getQueryBuilder(); |
|
| 583 | - $result = $query->select($fields) |
|
| 584 | - ->from('dav_shares', 's') |
|
| 585 | - ->join('s', 'calendars', 'a', $query->expr()->eq('s.resourceid', 'a.id')) |
|
| 586 | - ->where($query->expr()->in('s.access', $query->createNamedParameter(self::ACCESS_PUBLIC))) |
|
| 587 | - ->andWhere($query->expr()->eq('s.type', $query->createNamedParameter('calendar'))) |
|
| 588 | - ->andWhere($query->expr()->eq('s.publicuri', $query->createNamedParameter($uri))) |
|
| 589 | - ->executeQuery(); |
|
| 590 | - |
|
| 591 | - $row = $result->fetch(); |
|
| 592 | - |
|
| 593 | - $result->closeCursor(); |
|
| 594 | - |
|
| 595 | - if ($row === false) { |
|
| 596 | - throw new NotFound('Node with name \'' . $uri . '\' could not be found'); |
|
| 597 | - } |
|
| 598 | - |
|
| 599 | - $row['principaluri'] = (string) $row['principaluri']; |
|
| 600 | - [, $name] = Uri\split($row['principaluri']); |
|
| 601 | - $row['displayname'] = $row['displayname'] . ' ' . "($name)"; |
|
| 602 | - $components = []; |
|
| 603 | - if ($row['components']) { |
|
| 604 | - $components = explode(',',$row['components']); |
|
| 605 | - } |
|
| 606 | - $calendar = [ |
|
| 607 | - 'id' => $row['id'], |
|
| 608 | - 'uri' => $row['publicuri'], |
|
| 609 | - 'principaluri' => $this->convertPrincipal($row['principaluri'], !$this->legacyEndpoint), |
|
| 610 | - '{' . Plugin::NS_CALENDARSERVER . '}getctag' => 'http://sabre.io/ns/sync/' . ($row['synctoken']?$row['synctoken']:'0'), |
|
| 611 | - '{http://sabredav.org/ns}sync-token' => $row['synctoken']?$row['synctoken']:'0', |
|
| 612 | - '{' . Plugin::NS_CALDAV . '}supported-calendar-component-set' => new SupportedCalendarComponentSet($components), |
|
| 613 | - '{' . Plugin::NS_CALDAV . '}schedule-calendar-transp' => new ScheduleCalendarTransp($row['transparent']?'transparent':'opaque'), |
|
| 614 | - '{' . \OCA\DAV\DAV\Sharing\Plugin::NS_OWNCLOUD . '}owner-principal' => $this->convertPrincipal($row['principaluri'], !$this->legacyEndpoint), |
|
| 615 | - '{' . \OCA\DAV\DAV\Sharing\Plugin::NS_OWNCLOUD . '}read-only' => (int)$row['access'] === Backend::ACCESS_READ, |
|
| 616 | - '{' . \OCA\DAV\DAV\Sharing\Plugin::NS_OWNCLOUD . '}public' => (int)$row['access'] === self::ACCESS_PUBLIC, |
|
| 617 | - ]; |
|
| 618 | - |
|
| 619 | - $calendar = $this->rowToCalendar($row, $calendar); |
|
| 620 | - $calendar = $this->addOwnerPrincipalToCalendar($calendar); |
|
| 621 | - $calendar = $this->addResourceTypeToCalendar($row, $calendar); |
|
| 622 | - |
|
| 623 | - return $calendar; |
|
| 624 | - } |
|
| 625 | - |
|
| 626 | - /** |
|
| 627 | - * @param string $principal |
|
| 628 | - * @param string $uri |
|
| 629 | - * @return array|null |
|
| 630 | - */ |
|
| 631 | - public function getCalendarByUri($principal, $uri) { |
|
| 632 | - $fields = array_column($this->propertyMap, 0); |
|
| 633 | - $fields[] = 'id'; |
|
| 634 | - $fields[] = 'uri'; |
|
| 635 | - $fields[] = 'synctoken'; |
|
| 636 | - $fields[] = 'components'; |
|
| 637 | - $fields[] = 'principaluri'; |
|
| 638 | - $fields[] = 'transparent'; |
|
| 639 | - |
|
| 640 | - // Making fields a comma-delimited list |
|
| 641 | - $query = $this->db->getQueryBuilder(); |
|
| 642 | - $query->select($fields)->from('calendars') |
|
| 643 | - ->where($query->expr()->eq('uri', $query->createNamedParameter($uri))) |
|
| 644 | - ->andWhere($query->expr()->eq('principaluri', $query->createNamedParameter($principal))) |
|
| 645 | - ->setMaxResults(1); |
|
| 646 | - $stmt = $query->executeQuery(); |
|
| 647 | - |
|
| 648 | - $row = $stmt->fetch(); |
|
| 649 | - $stmt->closeCursor(); |
|
| 650 | - if ($row === false) { |
|
| 651 | - return null; |
|
| 652 | - } |
|
| 653 | - |
|
| 654 | - $row['principaluri'] = (string) $row['principaluri']; |
|
| 655 | - $components = []; |
|
| 656 | - if ($row['components']) { |
|
| 657 | - $components = explode(',',$row['components']); |
|
| 658 | - } |
|
| 659 | - |
|
| 660 | - $calendar = [ |
|
| 661 | - 'id' => $row['id'], |
|
| 662 | - 'uri' => $row['uri'], |
|
| 663 | - 'principaluri' => $this->convertPrincipal($row['principaluri'], !$this->legacyEndpoint), |
|
| 664 | - '{' . Plugin::NS_CALENDARSERVER . '}getctag' => 'http://sabre.io/ns/sync/' . ($row['synctoken']?$row['synctoken']:'0'), |
|
| 665 | - '{http://sabredav.org/ns}sync-token' => $row['synctoken']?$row['synctoken']:'0', |
|
| 666 | - '{' . Plugin::NS_CALDAV . '}supported-calendar-component-set' => new SupportedCalendarComponentSet($components), |
|
| 667 | - '{' . Plugin::NS_CALDAV . '}schedule-calendar-transp' => new ScheduleCalendarTransp($row['transparent']?'transparent':'opaque'), |
|
| 668 | - ]; |
|
| 669 | - |
|
| 670 | - $calendar = $this->rowToCalendar($row, $calendar); |
|
| 671 | - $calendar = $this->addOwnerPrincipalToCalendar($calendar); |
|
| 672 | - $calendar = $this->addResourceTypeToCalendar($row, $calendar); |
|
| 673 | - |
|
| 674 | - return $calendar; |
|
| 675 | - } |
|
| 676 | - |
|
| 677 | - /** |
|
| 678 | - * @return array{id: int, uri: string, '{http://calendarserver.org/ns/}getctag': string, '{http://sabredav.org/ns}sync-token': int, '{urn:ietf:params:xml:ns:caldav}supported-calendar-component-set': SupportedCalendarComponentSet, '{urn:ietf:params:xml:ns:caldav}schedule-calendar-transp': ScheduleCalendarTransp }|null |
|
| 679 | - */ |
|
| 680 | - public function getCalendarById(int $calendarId): ?array { |
|
| 681 | - $fields = array_column($this->propertyMap, 0); |
|
| 682 | - $fields[] = 'id'; |
|
| 683 | - $fields[] = 'uri'; |
|
| 684 | - $fields[] = 'synctoken'; |
|
| 685 | - $fields[] = 'components'; |
|
| 686 | - $fields[] = 'principaluri'; |
|
| 687 | - $fields[] = 'transparent'; |
|
| 688 | - |
|
| 689 | - // Making fields a comma-delimited list |
|
| 690 | - $query = $this->db->getQueryBuilder(); |
|
| 691 | - $query->select($fields)->from('calendars') |
|
| 692 | - ->where($query->expr()->eq('id', $query->createNamedParameter($calendarId))) |
|
| 693 | - ->setMaxResults(1); |
|
| 694 | - $stmt = $query->executeQuery(); |
|
| 695 | - |
|
| 696 | - $row = $stmt->fetch(); |
|
| 697 | - $stmt->closeCursor(); |
|
| 698 | - if ($row === false) { |
|
| 699 | - return null; |
|
| 700 | - } |
|
| 701 | - |
|
| 702 | - $row['principaluri'] = (string) $row['principaluri']; |
|
| 703 | - $components = []; |
|
| 704 | - if ($row['components']) { |
|
| 705 | - $components = explode(',',$row['components']); |
|
| 706 | - } |
|
| 707 | - |
|
| 708 | - $calendar = [ |
|
| 709 | - 'id' => $row['id'], |
|
| 710 | - 'uri' => $row['uri'], |
|
| 711 | - 'principaluri' => $this->convertPrincipal($row['principaluri'], !$this->legacyEndpoint), |
|
| 712 | - '{' . Plugin::NS_CALENDARSERVER . '}getctag' => 'http://sabre.io/ns/sync/' . ($row['synctoken']?$row['synctoken']:'0'), |
|
| 713 | - '{http://sabredav.org/ns}sync-token' => $row['synctoken'] ?? 0, |
|
| 714 | - '{' . Plugin::NS_CALDAV . '}supported-calendar-component-set' => new SupportedCalendarComponentSet($components), |
|
| 715 | - '{' . Plugin::NS_CALDAV . '}schedule-calendar-transp' => new ScheduleCalendarTransp($row['transparent']?'transparent':'opaque'), |
|
| 716 | - ]; |
|
| 717 | - |
|
| 718 | - $calendar = $this->rowToCalendar($row, $calendar); |
|
| 719 | - $calendar = $this->addOwnerPrincipalToCalendar($calendar); |
|
| 720 | - $calendar = $this->addResourceTypeToCalendar($row, $calendar); |
|
| 721 | - |
|
| 722 | - return $calendar; |
|
| 723 | - } |
|
| 724 | - |
|
| 725 | - /** |
|
| 726 | - * @param $subscriptionId |
|
| 727 | - */ |
|
| 728 | - public function getSubscriptionById($subscriptionId) { |
|
| 729 | - $fields = array_column($this->subscriptionPropertyMap, 0); |
|
| 730 | - $fields[] = 'id'; |
|
| 731 | - $fields[] = 'uri'; |
|
| 732 | - $fields[] = 'source'; |
|
| 733 | - $fields[] = 'synctoken'; |
|
| 734 | - $fields[] = 'principaluri'; |
|
| 735 | - $fields[] = 'lastmodified'; |
|
| 736 | - |
|
| 737 | - $query = $this->db->getQueryBuilder(); |
|
| 738 | - $query->select($fields) |
|
| 739 | - ->from('calendarsubscriptions') |
|
| 740 | - ->where($query->expr()->eq('id', $query->createNamedParameter($subscriptionId))) |
|
| 741 | - ->orderBy('calendarorder', 'asc'); |
|
| 742 | - $stmt = $query->executeQuery(); |
|
| 743 | - |
|
| 744 | - $row = $stmt->fetch(); |
|
| 745 | - $stmt->closeCursor(); |
|
| 746 | - if ($row === false) { |
|
| 747 | - return null; |
|
| 748 | - } |
|
| 749 | - |
|
| 750 | - $row['principaluri'] = (string) $row['principaluri']; |
|
| 751 | - $subscription = [ |
|
| 752 | - 'id' => $row['id'], |
|
| 753 | - 'uri' => $row['uri'], |
|
| 754 | - 'principaluri' => $row['principaluri'], |
|
| 755 | - 'source' => $row['source'], |
|
| 756 | - 'lastmodified' => $row['lastmodified'], |
|
| 757 | - '{' . Plugin::NS_CALDAV . '}supported-calendar-component-set' => new SupportedCalendarComponentSet(['VTODO', 'VEVENT']), |
|
| 758 | - '{http://sabredav.org/ns}sync-token' => $row['synctoken']?$row['synctoken']:'0', |
|
| 759 | - ]; |
|
| 760 | - |
|
| 761 | - return $this->rowToSubscription($row, $subscription); |
|
| 762 | - } |
|
| 763 | - |
|
| 764 | - /** |
|
| 765 | - * Creates a new calendar for a principal. |
|
| 766 | - * |
|
| 767 | - * If the creation was a success, an id must be returned that can be used to reference |
|
| 768 | - * this calendar in other methods, such as updateCalendar. |
|
| 769 | - * |
|
| 770 | - * @param string $principalUri |
|
| 771 | - * @param string $calendarUri |
|
| 772 | - * @param array $properties |
|
| 773 | - * @return int |
|
| 774 | - * |
|
| 775 | - * @throws CalendarException |
|
| 776 | - */ |
|
| 777 | - public function createCalendar($principalUri, $calendarUri, array $properties) { |
|
| 778 | - if (strlen($calendarUri) > 255) { |
|
| 779 | - throw new CalendarException('URI too long. Calendar not created'); |
|
| 780 | - } |
|
| 781 | - |
|
| 782 | - $values = [ |
|
| 783 | - 'principaluri' => $this->convertPrincipal($principalUri, true), |
|
| 784 | - 'uri' => $calendarUri, |
|
| 785 | - 'synctoken' => 1, |
|
| 786 | - 'transparent' => 0, |
|
| 787 | - 'components' => 'VEVENT,VTODO', |
|
| 788 | - 'displayname' => $calendarUri |
|
| 789 | - ]; |
|
| 790 | - |
|
| 791 | - // Default value |
|
| 792 | - $sccs = '{urn:ietf:params:xml:ns:caldav}supported-calendar-component-set'; |
|
| 793 | - if (isset($properties[$sccs])) { |
|
| 794 | - if (!($properties[$sccs] instanceof SupportedCalendarComponentSet)) { |
|
| 795 | - throw new DAV\Exception('The ' . $sccs . ' property must be of type: \Sabre\CalDAV\Property\SupportedCalendarComponentSet'); |
|
| 796 | - } |
|
| 797 | - $values['components'] = implode(',',$properties[$sccs]->getValue()); |
|
| 798 | - } elseif (isset($properties['components'])) { |
|
| 799 | - // Allow to provide components internally without having |
|
| 800 | - // to create a SupportedCalendarComponentSet object |
|
| 801 | - $values['components'] = $properties['components']; |
|
| 802 | - } |
|
| 803 | - |
|
| 804 | - $transp = '{' . Plugin::NS_CALDAV . '}schedule-calendar-transp'; |
|
| 805 | - if (isset($properties[$transp])) { |
|
| 806 | - $values['transparent'] = (int) ($properties[$transp]->getValue() === 'transparent'); |
|
| 807 | - } |
|
| 808 | - |
|
| 809 | - foreach ($this->propertyMap as $xmlName => [$dbName, $type]) { |
|
| 810 | - if (isset($properties[$xmlName])) { |
|
| 811 | - $values[$dbName] = $properties[$xmlName]; |
|
| 812 | - } |
|
| 813 | - } |
|
| 814 | - |
|
| 815 | - $query = $this->db->getQueryBuilder(); |
|
| 816 | - $query->insert('calendars'); |
|
| 817 | - foreach ($values as $column => $value) { |
|
| 818 | - $query->setValue($column, $query->createNamedParameter($value)); |
|
| 819 | - } |
|
| 820 | - $query->executeStatement(); |
|
| 821 | - $calendarId = $query->getLastInsertId(); |
|
| 822 | - |
|
| 823 | - $calendarData = $this->getCalendarById($calendarId); |
|
| 824 | - $this->dispatcher->dispatchTyped(new CalendarCreatedEvent((int)$calendarId, $calendarData)); |
|
| 825 | - |
|
| 826 | - return $calendarId; |
|
| 827 | - } |
|
| 828 | - |
|
| 829 | - /** |
|
| 830 | - * Updates properties for a calendar. |
|
| 831 | - * |
|
| 832 | - * The list of mutations is stored in a Sabre\DAV\PropPatch object. |
|
| 833 | - * To do the actual updates, you must tell this object which properties |
|
| 834 | - * you're going to process with the handle() method. |
|
| 835 | - * |
|
| 836 | - * Calling the handle method is like telling the PropPatch object "I |
|
| 837 | - * promise I can handle updating this property". |
|
| 838 | - * |
|
| 839 | - * Read the PropPatch documentation for more info and examples. |
|
| 840 | - * |
|
| 841 | - * @param mixed $calendarId |
|
| 842 | - * @param PropPatch $propPatch |
|
| 843 | - * @return void |
|
| 844 | - */ |
|
| 845 | - public function updateCalendar($calendarId, PropPatch $propPatch) { |
|
| 846 | - $supportedProperties = array_keys($this->propertyMap); |
|
| 847 | - $supportedProperties[] = '{' . Plugin::NS_CALDAV . '}schedule-calendar-transp'; |
|
| 848 | - |
|
| 849 | - $propPatch->handle($supportedProperties, function ($mutations) use ($calendarId) { |
|
| 850 | - $newValues = []; |
|
| 851 | - foreach ($mutations as $propertyName => $propertyValue) { |
|
| 852 | - switch ($propertyName) { |
|
| 853 | - case '{' . Plugin::NS_CALDAV . '}schedule-calendar-transp': |
|
| 854 | - $fieldName = 'transparent'; |
|
| 855 | - $newValues[$fieldName] = (int) ($propertyValue->getValue() === 'transparent'); |
|
| 856 | - break; |
|
| 857 | - default: |
|
| 858 | - $fieldName = $this->propertyMap[$propertyName][0]; |
|
| 859 | - $newValues[$fieldName] = $propertyValue; |
|
| 860 | - break; |
|
| 861 | - } |
|
| 862 | - } |
|
| 863 | - $query = $this->db->getQueryBuilder(); |
|
| 864 | - $query->update('calendars'); |
|
| 865 | - foreach ($newValues as $fieldName => $value) { |
|
| 866 | - $query->set($fieldName, $query->createNamedParameter($value)); |
|
| 867 | - } |
|
| 868 | - $query->where($query->expr()->eq('id', $query->createNamedParameter($calendarId))); |
|
| 869 | - $query->executeStatement(); |
|
| 870 | - |
|
| 871 | - $this->addChange($calendarId, "", 2); |
|
| 872 | - |
|
| 873 | - $calendarData = $this->getCalendarById($calendarId); |
|
| 874 | - $shares = $this->getShares($calendarId); |
|
| 875 | - $this->dispatcher->dispatchTyped(new CalendarUpdatedEvent($calendarId, $calendarData, $shares, $mutations)); |
|
| 876 | - |
|
| 877 | - return true; |
|
| 878 | - }); |
|
| 879 | - } |
|
| 880 | - |
|
| 881 | - /** |
|
| 882 | - * Delete a calendar and all it's objects |
|
| 883 | - * |
|
| 884 | - * @param mixed $calendarId |
|
| 885 | - * @return void |
|
| 886 | - */ |
|
| 887 | - public function deleteCalendar($calendarId, bool $forceDeletePermanently = false) { |
|
| 888 | - // The calendar is deleted right away if this is either enforced by the caller |
|
| 889 | - // or the special contacts birthday calendar or when the preference of an empty |
|
| 890 | - // retention (0 seconds) is set, which signals a disabled trashbin. |
|
| 891 | - $calendarData = $this->getCalendarById($calendarId); |
|
| 892 | - $isBirthdayCalendar = isset($calendarData['uri']) && $calendarData['uri'] === BirthdayService::BIRTHDAY_CALENDAR_URI; |
|
| 893 | - $trashbinDisabled = $this->config->getAppValue(Application::APP_ID, RetentionService::RETENTION_CONFIG_KEY) === '0'; |
|
| 894 | - if ($forceDeletePermanently || $isBirthdayCalendar || $trashbinDisabled) { |
|
| 895 | - $calendarData = $this->getCalendarById($calendarId); |
|
| 896 | - $shares = $this->getShares($calendarId); |
|
| 897 | - |
|
| 898 | - $qbDeleteCalendarObjectProps = $this->db->getQueryBuilder(); |
|
| 899 | - $qbDeleteCalendarObjectProps->delete($this->dbObjectPropertiesTable) |
|
| 900 | - ->where($qbDeleteCalendarObjectProps->expr()->eq('calendarid', $qbDeleteCalendarObjectProps->createNamedParameter($calendarId))) |
|
| 901 | - ->andWhere($qbDeleteCalendarObjectProps->expr()->eq('calendartype', $qbDeleteCalendarObjectProps->createNamedParameter(self::CALENDAR_TYPE_CALENDAR))) |
|
| 902 | - ->executeStatement(); |
|
| 903 | - |
|
| 904 | - $qbDeleteCalendarObjects = $this->db->getQueryBuilder(); |
|
| 905 | - $qbDeleteCalendarObjects->delete('calendarobjects') |
|
| 906 | - ->where($qbDeleteCalendarObjects->expr()->eq('calendarid', $qbDeleteCalendarObjects->createNamedParameter($calendarId))) |
|
| 907 | - ->andWhere($qbDeleteCalendarObjects->expr()->eq('calendartype', $qbDeleteCalendarObjects->createNamedParameter(self::CALENDAR_TYPE_CALENDAR))) |
|
| 908 | - ->executeStatement(); |
|
| 909 | - |
|
| 910 | - $qbDeleteCalendarChanges = $this->db->getQueryBuilder(); |
|
| 911 | - $qbDeleteCalendarObjects->delete('calendarchanges') |
|
| 912 | - ->where($qbDeleteCalendarChanges->expr()->eq('calendarid', $qbDeleteCalendarChanges->createNamedParameter($calendarId))) |
|
| 913 | - ->andWhere($qbDeleteCalendarChanges->expr()->eq('calendartype', $qbDeleteCalendarChanges->createNamedParameter(self::CALENDAR_TYPE_CALENDAR))) |
|
| 914 | - ->executeStatement(); |
|
| 915 | - |
|
| 916 | - $this->calendarSharingBackend->deleteAllShares($calendarId); |
|
| 917 | - |
|
| 918 | - $qbDeleteCalendar = $this->db->getQueryBuilder(); |
|
| 919 | - $qbDeleteCalendarObjects->delete('calendars') |
|
| 920 | - ->where($qbDeleteCalendar->expr()->eq('id', $qbDeleteCalendar->createNamedParameter($calendarId))) |
|
| 921 | - ->executeStatement(); |
|
| 922 | - |
|
| 923 | - // Only dispatch if we actually deleted anything |
|
| 924 | - if ($calendarData) { |
|
| 925 | - $this->dispatcher->dispatchTyped(new CalendarDeletedEvent($calendarId, $calendarData, $shares)); |
|
| 926 | - } |
|
| 927 | - } else { |
|
| 928 | - $qbMarkCalendarDeleted = $this->db->getQueryBuilder(); |
|
| 929 | - $qbMarkCalendarDeleted->update('calendars') |
|
| 930 | - ->set('deleted_at', $qbMarkCalendarDeleted->createNamedParameter(time())) |
|
| 931 | - ->where($qbMarkCalendarDeleted->expr()->eq('id', $qbMarkCalendarDeleted->createNamedParameter($calendarId))) |
|
| 932 | - ->executeStatement(); |
|
| 933 | - |
|
| 934 | - $calendarData = $this->getCalendarById($calendarId); |
|
| 935 | - $shares = $this->getShares($calendarId); |
|
| 936 | - if ($calendarData) { |
|
| 937 | - $this->dispatcher->dispatchTyped(new CalendarMovedToTrashEvent( |
|
| 938 | - $calendarId, |
|
| 939 | - $calendarData, |
|
| 940 | - $shares |
|
| 941 | - )); |
|
| 942 | - } |
|
| 943 | - } |
|
| 944 | - } |
|
| 945 | - |
|
| 946 | - public function restoreCalendar(int $id): void { |
|
| 947 | - $qb = $this->db->getQueryBuilder(); |
|
| 948 | - $update = $qb->update('calendars') |
|
| 949 | - ->set('deleted_at', $qb->createNamedParameter(null)) |
|
| 950 | - ->where($qb->expr()->eq('id', $qb->createNamedParameter($id, IQueryBuilder::PARAM_INT), IQueryBuilder::PARAM_INT)); |
|
| 951 | - $update->executeStatement(); |
|
| 952 | - |
|
| 953 | - $calendarData = $this->getCalendarById($id); |
|
| 954 | - $shares = $this->getShares($id); |
|
| 955 | - if ($calendarData === null) { |
|
| 956 | - throw new RuntimeException('Calendar data that was just written can\'t be read back. Check your database configuration.'); |
|
| 957 | - } |
|
| 958 | - $this->dispatcher->dispatchTyped(new CalendarRestoredEvent( |
|
| 959 | - $id, |
|
| 960 | - $calendarData, |
|
| 961 | - $shares |
|
| 962 | - )); |
|
| 963 | - } |
|
| 964 | - |
|
| 965 | - /** |
|
| 966 | - * Delete all of an user's shares |
|
| 967 | - * |
|
| 968 | - * @param string $principaluri |
|
| 969 | - * @return void |
|
| 970 | - */ |
|
| 971 | - public function deleteAllSharesByUser($principaluri) { |
|
| 972 | - $this->calendarSharingBackend->deleteAllSharesByUser($principaluri); |
|
| 973 | - } |
|
| 974 | - |
|
| 975 | - /** |
|
| 976 | - * Returns all calendar objects within a calendar. |
|
| 977 | - * |
|
| 978 | - * Every item contains an array with the following keys: |
|
| 979 | - * * calendardata - The iCalendar-compatible calendar data |
|
| 980 | - * * uri - a unique key which will be used to construct the uri. This can |
|
| 981 | - * be any arbitrary string, but making sure it ends with '.ics' is a |
|
| 982 | - * good idea. This is only the basename, or filename, not the full |
|
| 983 | - * path. |
|
| 984 | - * * lastmodified - a timestamp of the last modification time |
|
| 985 | - * * etag - An arbitrary string, surrounded by double-quotes. (e.g.: |
|
| 986 | - * '"abcdef"') |
|
| 987 | - * * size - The size of the calendar objects, in bytes. |
|
| 988 | - * * component - optional, a string containing the type of object, such |
|
| 989 | - * as 'vevent' or 'vtodo'. If specified, this will be used to populate |
|
| 990 | - * the Content-Type header. |
|
| 991 | - * |
|
| 992 | - * Note that the etag is optional, but it's highly encouraged to return for |
|
| 993 | - * speed reasons. |
|
| 994 | - * |
|
| 995 | - * The calendardata is also optional. If it's not returned |
|
| 996 | - * 'getCalendarObject' will be called later, which *is* expected to return |
|
| 997 | - * calendardata. |
|
| 998 | - * |
|
| 999 | - * If neither etag or size are specified, the calendardata will be |
|
| 1000 | - * used/fetched to determine these numbers. If both are specified the |
|
| 1001 | - * amount of times this is needed is reduced by a great degree. |
|
| 1002 | - * |
|
| 1003 | - * @param mixed $calendarId |
|
| 1004 | - * @param int $calendarType |
|
| 1005 | - * @return array |
|
| 1006 | - */ |
|
| 1007 | - public function getCalendarObjects($calendarId, $calendarType = self::CALENDAR_TYPE_CALENDAR):array { |
|
| 1008 | - $query = $this->db->getQueryBuilder(); |
|
| 1009 | - $query->select(['id', 'uri', 'lastmodified', 'etag', 'calendarid', 'size', 'componenttype', 'classification']) |
|
| 1010 | - ->from('calendarobjects') |
|
| 1011 | - ->where($query->expr()->eq('calendarid', $query->createNamedParameter($calendarId))) |
|
| 1012 | - ->andWhere($query->expr()->eq('calendartype', $query->createNamedParameter($calendarType))) |
|
| 1013 | - ->andWhere($query->expr()->isNull('deleted_at')); |
|
| 1014 | - $stmt = $query->executeQuery(); |
|
| 1015 | - |
|
| 1016 | - $result = []; |
|
| 1017 | - foreach ($stmt->fetchAll() as $row) { |
|
| 1018 | - $result[] = [ |
|
| 1019 | - 'id' => $row['id'], |
|
| 1020 | - 'uri' => $row['uri'], |
|
| 1021 | - 'lastmodified' => $row['lastmodified'], |
|
| 1022 | - 'etag' => '"' . $row['etag'] . '"', |
|
| 1023 | - 'calendarid' => $row['calendarid'], |
|
| 1024 | - 'size' => (int)$row['size'], |
|
| 1025 | - 'component' => strtolower($row['componenttype']), |
|
| 1026 | - 'classification' => (int)$row['classification'] |
|
| 1027 | - ]; |
|
| 1028 | - } |
|
| 1029 | - $stmt->closeCursor(); |
|
| 1030 | - |
|
| 1031 | - return $result; |
|
| 1032 | - } |
|
| 1033 | - |
|
| 1034 | - public function getDeletedCalendarObjects(int $deletedBefore): array { |
|
| 1035 | - $query = $this->db->getQueryBuilder(); |
|
| 1036 | - $query->select(['co.id', 'co.uri', 'co.lastmodified', 'co.etag', 'co.calendarid', 'co.calendartype', 'co.size', 'co.componenttype', 'co.classification', 'co.deleted_at']) |
|
| 1037 | - ->from('calendarobjects', 'co') |
|
| 1038 | - ->join('co', 'calendars', 'c', $query->expr()->eq('c.id', 'co.calendarid', IQueryBuilder::PARAM_INT)) |
|
| 1039 | - ->where($query->expr()->isNotNull('co.deleted_at')) |
|
| 1040 | - ->andWhere($query->expr()->lt('co.deleted_at', $query->createNamedParameter($deletedBefore))); |
|
| 1041 | - $stmt = $query->executeQuery(); |
|
| 1042 | - |
|
| 1043 | - $result = []; |
|
| 1044 | - foreach ($stmt->fetchAll() as $row) { |
|
| 1045 | - $result[] = [ |
|
| 1046 | - 'id' => $row['id'], |
|
| 1047 | - 'uri' => $row['uri'], |
|
| 1048 | - 'lastmodified' => $row['lastmodified'], |
|
| 1049 | - 'etag' => '"' . $row['etag'] . '"', |
|
| 1050 | - 'calendarid' => (int) $row['calendarid'], |
|
| 1051 | - 'calendartype' => (int) $row['calendartype'], |
|
| 1052 | - 'size' => (int) $row['size'], |
|
| 1053 | - 'component' => strtolower($row['componenttype']), |
|
| 1054 | - 'classification' => (int) $row['classification'], |
|
| 1055 | - '{' . \OCA\DAV\DAV\Sharing\Plugin::NS_NEXTCLOUD . '}deleted-at' => $row['deleted_at'] === null ? $row['deleted_at'] : (int) $row['deleted_at'], |
|
| 1056 | - ]; |
|
| 1057 | - } |
|
| 1058 | - $stmt->closeCursor(); |
|
| 1059 | - |
|
| 1060 | - return $result; |
|
| 1061 | - } |
|
| 1062 | - |
|
| 1063 | - /** |
|
| 1064 | - * Return all deleted calendar objects by the given principal that are not |
|
| 1065 | - * in deleted calendars. |
|
| 1066 | - * |
|
| 1067 | - * @param string $principalUri |
|
| 1068 | - * @return array |
|
| 1069 | - * @throws Exception |
|
| 1070 | - */ |
|
| 1071 | - public function getDeletedCalendarObjectsByPrincipal(string $principalUri): array { |
|
| 1072 | - $query = $this->db->getQueryBuilder(); |
|
| 1073 | - $query->select(['co.id', 'co.uri', 'co.lastmodified', 'co.etag', 'co.calendarid', 'co.size', 'co.componenttype', 'co.classification', 'co.deleted_at']) |
|
| 1074 | - ->selectAlias('c.uri', 'calendaruri') |
|
| 1075 | - ->from('calendarobjects', 'co') |
|
| 1076 | - ->join('co', 'calendars', 'c', $query->expr()->eq('c.id', 'co.calendarid', IQueryBuilder::PARAM_INT)) |
|
| 1077 | - ->where($query->expr()->eq('principaluri', $query->createNamedParameter($principalUri))) |
|
| 1078 | - ->andWhere($query->expr()->isNotNull('co.deleted_at')) |
|
| 1079 | - ->andWhere($query->expr()->isNull('c.deleted_at')); |
|
| 1080 | - $stmt = $query->executeQuery(); |
|
| 1081 | - |
|
| 1082 | - $result = []; |
|
| 1083 | - while ($row = $stmt->fetch()) { |
|
| 1084 | - $result[] = [ |
|
| 1085 | - 'id' => $row['id'], |
|
| 1086 | - 'uri' => $row['uri'], |
|
| 1087 | - 'lastmodified' => $row['lastmodified'], |
|
| 1088 | - 'etag' => '"' . $row['etag'] . '"', |
|
| 1089 | - 'calendarid' => $row['calendarid'], |
|
| 1090 | - 'calendaruri' => $row['calendaruri'], |
|
| 1091 | - 'size' => (int)$row['size'], |
|
| 1092 | - 'component' => strtolower($row['componenttype']), |
|
| 1093 | - 'classification' => (int)$row['classification'], |
|
| 1094 | - '{' . \OCA\DAV\DAV\Sharing\Plugin::NS_NEXTCLOUD . '}deleted-at' => $row['deleted_at'] === null ? $row['deleted_at'] : (int) $row['deleted_at'], |
|
| 1095 | - ]; |
|
| 1096 | - } |
|
| 1097 | - $stmt->closeCursor(); |
|
| 1098 | - |
|
| 1099 | - return $result; |
|
| 1100 | - } |
|
| 1101 | - |
|
| 1102 | - /** |
|
| 1103 | - * Returns information from a single calendar object, based on it's object |
|
| 1104 | - * uri. |
|
| 1105 | - * |
|
| 1106 | - * The object uri is only the basename, or filename and not a full path. |
|
| 1107 | - * |
|
| 1108 | - * The returned array must have the same keys as getCalendarObjects. The |
|
| 1109 | - * 'calendardata' object is required here though, while it's not required |
|
| 1110 | - * for getCalendarObjects. |
|
| 1111 | - * |
|
| 1112 | - * This method must return null if the object did not exist. |
|
| 1113 | - * |
|
| 1114 | - * @param mixed $calendarId |
|
| 1115 | - * @param string $objectUri |
|
| 1116 | - * @param int $calendarType |
|
| 1117 | - * @return array|null |
|
| 1118 | - */ |
|
| 1119 | - public function getCalendarObject($calendarId, $objectUri, int $calendarType = self::CALENDAR_TYPE_CALENDAR) { |
|
| 1120 | - $query = $this->db->getQueryBuilder(); |
|
| 1121 | - $query->select(['id', 'uri', 'lastmodified', 'etag', 'calendarid', 'size', 'calendardata', 'componenttype', 'classification', 'deleted_at']) |
|
| 1122 | - ->from('calendarobjects') |
|
| 1123 | - ->where($query->expr()->eq('calendarid', $query->createNamedParameter($calendarId))) |
|
| 1124 | - ->andWhere($query->expr()->eq('uri', $query->createNamedParameter($objectUri))) |
|
| 1125 | - ->andWhere($query->expr()->eq('calendartype', $query->createNamedParameter($calendarType))); |
|
| 1126 | - $stmt = $query->executeQuery(); |
|
| 1127 | - $row = $stmt->fetch(); |
|
| 1128 | - $stmt->closeCursor(); |
|
| 1129 | - |
|
| 1130 | - if (!$row) { |
|
| 1131 | - return null; |
|
| 1132 | - } |
|
| 1133 | - |
|
| 1134 | - return [ |
|
| 1135 | - 'id' => $row['id'], |
|
| 1136 | - 'uri' => $row['uri'], |
|
| 1137 | - 'lastmodified' => $row['lastmodified'], |
|
| 1138 | - 'etag' => '"' . $row['etag'] . '"', |
|
| 1139 | - 'calendarid' => $row['calendarid'], |
|
| 1140 | - 'size' => (int)$row['size'], |
|
| 1141 | - 'calendardata' => $this->readBlob($row['calendardata']), |
|
| 1142 | - 'component' => strtolower($row['componenttype']), |
|
| 1143 | - 'classification' => (int)$row['classification'], |
|
| 1144 | - '{' . \OCA\DAV\DAV\Sharing\Plugin::NS_NEXTCLOUD . '}deleted-at' => $row['deleted_at'] === null ? $row['deleted_at'] : (int) $row['deleted_at'], |
|
| 1145 | - ]; |
|
| 1146 | - } |
|
| 1147 | - |
|
| 1148 | - /** |
|
| 1149 | - * Returns a list of calendar objects. |
|
| 1150 | - * |
|
| 1151 | - * This method should work identical to getCalendarObject, but instead |
|
| 1152 | - * return all the calendar objects in the list as an array. |
|
| 1153 | - * |
|
| 1154 | - * If the backend supports this, it may allow for some speed-ups. |
|
| 1155 | - * |
|
| 1156 | - * @param mixed $calendarId |
|
| 1157 | - * @param string[] $uris |
|
| 1158 | - * @param int $calendarType |
|
| 1159 | - * @return array |
|
| 1160 | - */ |
|
| 1161 | - public function getMultipleCalendarObjects($calendarId, array $uris, $calendarType = self::CALENDAR_TYPE_CALENDAR):array { |
|
| 1162 | - if (empty($uris)) { |
|
| 1163 | - return []; |
|
| 1164 | - } |
|
| 1165 | - |
|
| 1166 | - $chunks = array_chunk($uris, 100); |
|
| 1167 | - $objects = []; |
|
| 1168 | - |
|
| 1169 | - $query = $this->db->getQueryBuilder(); |
|
| 1170 | - $query->select(['id', 'uri', 'lastmodified', 'etag', 'calendarid', 'size', 'calendardata', 'componenttype', 'classification']) |
|
| 1171 | - ->from('calendarobjects') |
|
| 1172 | - ->where($query->expr()->eq('calendarid', $query->createNamedParameter($calendarId))) |
|
| 1173 | - ->andWhere($query->expr()->in('uri', $query->createParameter('uri'))) |
|
| 1174 | - ->andWhere($query->expr()->eq('calendartype', $query->createNamedParameter($calendarType))) |
|
| 1175 | - ->andWhere($query->expr()->isNull('deleted_at')); |
|
| 1176 | - |
|
| 1177 | - foreach ($chunks as $uris) { |
|
| 1178 | - $query->setParameter('uri', $uris, IQueryBuilder::PARAM_STR_ARRAY); |
|
| 1179 | - $result = $query->executeQuery(); |
|
| 1180 | - |
|
| 1181 | - while ($row = $result->fetch()) { |
|
| 1182 | - $objects[] = [ |
|
| 1183 | - 'id' => $row['id'], |
|
| 1184 | - 'uri' => $row['uri'], |
|
| 1185 | - 'lastmodified' => $row['lastmodified'], |
|
| 1186 | - 'etag' => '"' . $row['etag'] . '"', |
|
| 1187 | - 'calendarid' => $row['calendarid'], |
|
| 1188 | - 'size' => (int)$row['size'], |
|
| 1189 | - 'calendardata' => $this->readBlob($row['calendardata']), |
|
| 1190 | - 'component' => strtolower($row['componenttype']), |
|
| 1191 | - 'classification' => (int)$row['classification'] |
|
| 1192 | - ]; |
|
| 1193 | - } |
|
| 1194 | - $result->closeCursor(); |
|
| 1195 | - } |
|
| 1196 | - |
|
| 1197 | - return $objects; |
|
| 1198 | - } |
|
| 1199 | - |
|
| 1200 | - /** |
|
| 1201 | - * Creates a new calendar object. |
|
| 1202 | - * |
|
| 1203 | - * The object uri is only the basename, or filename and not a full path. |
|
| 1204 | - * |
|
| 1205 | - * It is possible return an etag from this function, which will be used in |
|
| 1206 | - * the response to this PUT request. Note that the ETag must be surrounded |
|
| 1207 | - * by double-quotes. |
|
| 1208 | - * |
|
| 1209 | - * However, you should only really return this ETag if you don't mangle the |
|
| 1210 | - * calendar-data. If the result of a subsequent GET to this object is not |
|
| 1211 | - * the exact same as this request body, you should omit the ETag. |
|
| 1212 | - * |
|
| 1213 | - * @param mixed $calendarId |
|
| 1214 | - * @param string $objectUri |
|
| 1215 | - * @param string $calendarData |
|
| 1216 | - * @param int $calendarType |
|
| 1217 | - * @return string |
|
| 1218 | - */ |
|
| 1219 | - public function createCalendarObject($calendarId, $objectUri, $calendarData, $calendarType = self::CALENDAR_TYPE_CALENDAR) { |
|
| 1220 | - $extraData = $this->getDenormalizedData($calendarData); |
|
| 1221 | - |
|
| 1222 | - // Try to detect duplicates |
|
| 1223 | - $qb = $this->db->getQueryBuilder(); |
|
| 1224 | - $qb->select($qb->func()->count('*')) |
|
| 1225 | - ->from('calendarobjects') |
|
| 1226 | - ->where($qb->expr()->eq('calendarid', $qb->createNamedParameter($calendarId))) |
|
| 1227 | - ->andWhere($qb->expr()->eq('uid', $qb->createNamedParameter($extraData['uid']))) |
|
| 1228 | - ->andWhere($qb->expr()->eq('calendartype', $qb->createNamedParameter($calendarType))) |
|
| 1229 | - ->andWhere($qb->expr()->isNull('deleted_at')); |
|
| 1230 | - $result = $qb->executeQuery(); |
|
| 1231 | - $count = (int) $result->fetchOne(); |
|
| 1232 | - $result->closeCursor(); |
|
| 1233 | - |
|
| 1234 | - if ($count !== 0) { |
|
| 1235 | - throw new BadRequest('Calendar object with uid already exists in this calendar collection.'); |
|
| 1236 | - } |
|
| 1237 | - // For a more specific error message we also try to explicitly look up the UID but as a deleted entry |
|
| 1238 | - $qbDel = $this->db->getQueryBuilder(); |
|
| 1239 | - $qbDel->select($qb->func()->count('*')) |
|
| 1240 | - ->from('calendarobjects') |
|
| 1241 | - ->where($qbDel->expr()->eq('calendarid', $qbDel->createNamedParameter($calendarId))) |
|
| 1242 | - ->andWhere($qbDel->expr()->eq('uid', $qbDel->createNamedParameter($extraData['uid']))) |
|
| 1243 | - ->andWhere($qbDel->expr()->eq('calendartype', $qbDel->createNamedParameter($calendarType))) |
|
| 1244 | - ->andWhere($qbDel->expr()->isNotNull('deleted_at')); |
|
| 1245 | - $result = $qbDel->executeQuery(); |
|
| 1246 | - $count = (int) $result->fetchOne(); |
|
| 1247 | - $result->closeCursor(); |
|
| 1248 | - if ($count !== 0) { |
|
| 1249 | - throw new BadRequest('Deleted calendar object with uid already exists in this calendar collection.'); |
|
| 1250 | - } |
|
| 1251 | - |
|
| 1252 | - $query = $this->db->getQueryBuilder(); |
|
| 1253 | - $query->insert('calendarobjects') |
|
| 1254 | - ->values([ |
|
| 1255 | - 'calendarid' => $query->createNamedParameter($calendarId), |
|
| 1256 | - 'uri' => $query->createNamedParameter($objectUri), |
|
| 1257 | - 'calendardata' => $query->createNamedParameter($calendarData, IQueryBuilder::PARAM_LOB), |
|
| 1258 | - 'lastmodified' => $query->createNamedParameter(time()), |
|
| 1259 | - 'etag' => $query->createNamedParameter($extraData['etag']), |
|
| 1260 | - 'size' => $query->createNamedParameter($extraData['size']), |
|
| 1261 | - 'componenttype' => $query->createNamedParameter($extraData['componentType']), |
|
| 1262 | - 'firstoccurence' => $query->createNamedParameter($extraData['firstOccurence']), |
|
| 1263 | - 'lastoccurence' => $query->createNamedParameter($extraData['lastOccurence']), |
|
| 1264 | - 'classification' => $query->createNamedParameter($extraData['classification']), |
|
| 1265 | - 'uid' => $query->createNamedParameter($extraData['uid']), |
|
| 1266 | - 'calendartype' => $query->createNamedParameter($calendarType), |
|
| 1267 | - ]) |
|
| 1268 | - ->executeStatement(); |
|
| 1269 | - |
|
| 1270 | - $this->updateProperties($calendarId, $objectUri, $calendarData, $calendarType); |
|
| 1271 | - $this->addChange($calendarId, $objectUri, 1, $calendarType); |
|
| 1272 | - |
|
| 1273 | - $objectRow = $this->getCalendarObject($calendarId, $objectUri, $calendarType); |
|
| 1274 | - assert($objectRow !== null); |
|
| 1275 | - |
|
| 1276 | - if ($calendarType === self::CALENDAR_TYPE_CALENDAR) { |
|
| 1277 | - $calendarRow = $this->getCalendarById($calendarId); |
|
| 1278 | - $shares = $this->getShares($calendarId); |
|
| 1279 | - |
|
| 1280 | - $this->dispatcher->dispatchTyped(new CalendarObjectCreatedEvent($calendarId, $calendarRow, $shares, $objectRow)); |
|
| 1281 | - } else { |
|
| 1282 | - $subscriptionRow = $this->getSubscriptionById($calendarId); |
|
| 1283 | - |
|
| 1284 | - $this->dispatcher->dispatchTyped(new CachedCalendarObjectCreatedEvent($calendarId, $subscriptionRow, [], $objectRow)); |
|
| 1285 | - } |
|
| 1286 | - |
|
| 1287 | - return '"' . $extraData['etag'] . '"'; |
|
| 1288 | - } |
|
| 1289 | - |
|
| 1290 | - /** |
|
| 1291 | - * Updates an existing calendarobject, based on it's uri. |
|
| 1292 | - * |
|
| 1293 | - * The object uri is only the basename, or filename and not a full path. |
|
| 1294 | - * |
|
| 1295 | - * It is possible return an etag from this function, which will be used in |
|
| 1296 | - * the response to this PUT request. Note that the ETag must be surrounded |
|
| 1297 | - * by double-quotes. |
|
| 1298 | - * |
|
| 1299 | - * However, you should only really return this ETag if you don't mangle the |
|
| 1300 | - * calendar-data. If the result of a subsequent GET to this object is not |
|
| 1301 | - * the exact same as this request body, you should omit the ETag. |
|
| 1302 | - * |
|
| 1303 | - * @param mixed $calendarId |
|
| 1304 | - * @param string $objectUri |
|
| 1305 | - * @param string $calendarData |
|
| 1306 | - * @param int $calendarType |
|
| 1307 | - * @return string |
|
| 1308 | - */ |
|
| 1309 | - public function updateCalendarObject($calendarId, $objectUri, $calendarData, $calendarType = self::CALENDAR_TYPE_CALENDAR) { |
|
| 1310 | - $extraData = $this->getDenormalizedData($calendarData); |
|
| 1311 | - $query = $this->db->getQueryBuilder(); |
|
| 1312 | - $query->update('calendarobjects') |
|
| 1313 | - ->set('calendardata', $query->createNamedParameter($calendarData, IQueryBuilder::PARAM_LOB)) |
|
| 1314 | - ->set('lastmodified', $query->createNamedParameter(time())) |
|
| 1315 | - ->set('etag', $query->createNamedParameter($extraData['etag'])) |
|
| 1316 | - ->set('size', $query->createNamedParameter($extraData['size'])) |
|
| 1317 | - ->set('componenttype', $query->createNamedParameter($extraData['componentType'])) |
|
| 1318 | - ->set('firstoccurence', $query->createNamedParameter($extraData['firstOccurence'])) |
|
| 1319 | - ->set('lastoccurence', $query->createNamedParameter($extraData['lastOccurence'])) |
|
| 1320 | - ->set('classification', $query->createNamedParameter($extraData['classification'])) |
|
| 1321 | - ->set('uid', $query->createNamedParameter($extraData['uid'])) |
|
| 1322 | - ->where($query->expr()->eq('calendarid', $query->createNamedParameter($calendarId))) |
|
| 1323 | - ->andWhere($query->expr()->eq('uri', $query->createNamedParameter($objectUri))) |
|
| 1324 | - ->andWhere($query->expr()->eq('calendartype', $query->createNamedParameter($calendarType))) |
|
| 1325 | - ->executeStatement(); |
|
| 1326 | - |
|
| 1327 | - $this->updateProperties($calendarId, $objectUri, $calendarData, $calendarType); |
|
| 1328 | - $this->addChange($calendarId, $objectUri, 2, $calendarType); |
|
| 1329 | - |
|
| 1330 | - $objectRow = $this->getCalendarObject($calendarId, $objectUri, $calendarType); |
|
| 1331 | - if (is_array($objectRow)) { |
|
| 1332 | - if ($calendarType === self::CALENDAR_TYPE_CALENDAR) { |
|
| 1333 | - $calendarRow = $this->getCalendarById($calendarId); |
|
| 1334 | - $shares = $this->getShares($calendarId); |
|
| 1335 | - |
|
| 1336 | - $this->dispatcher->dispatchTyped(new CalendarObjectUpdatedEvent($calendarId, $calendarRow, $shares, $objectRow)); |
|
| 1337 | - } else { |
|
| 1338 | - $subscriptionRow = $this->getSubscriptionById($calendarId); |
|
| 1339 | - |
|
| 1340 | - $this->dispatcher->dispatchTyped(new CachedCalendarObjectUpdatedEvent($calendarId, $subscriptionRow, [], $objectRow)); |
|
| 1341 | - } |
|
| 1342 | - } |
|
| 1343 | - |
|
| 1344 | - return '"' . $extraData['etag'] . '"'; |
|
| 1345 | - } |
|
| 1346 | - |
|
| 1347 | - /** |
|
| 1348 | - * Moves a calendar object from calendar to calendar. |
|
| 1349 | - * |
|
| 1350 | - * @param int $sourceCalendarId |
|
| 1351 | - * @param int $targetCalendarId |
|
| 1352 | - * @param int $objectId |
|
| 1353 | - * @param string $oldPrincipalUri |
|
| 1354 | - * @param string $newPrincipalUri |
|
| 1355 | - * @param int $calendarType |
|
| 1356 | - * @return bool |
|
| 1357 | - * @throws Exception |
|
| 1358 | - */ |
|
| 1359 | - public function moveCalendarObject(int $sourceCalendarId, int $targetCalendarId, int $objectId, string $oldPrincipalUri, string $newPrincipalUri, int $calendarType = self::CALENDAR_TYPE_CALENDAR): bool { |
|
| 1360 | - $object = $this->getCalendarObjectById($oldPrincipalUri, $objectId); |
|
| 1361 | - if (empty($object)) { |
|
| 1362 | - return false; |
|
| 1363 | - } |
|
| 1364 | - |
|
| 1365 | - $query = $this->db->getQueryBuilder(); |
|
| 1366 | - $query->update('calendarobjects') |
|
| 1367 | - ->set('calendarid', $query->createNamedParameter($targetCalendarId, IQueryBuilder::PARAM_INT)) |
|
| 1368 | - ->where($query->expr()->eq('id', $query->createNamedParameter($objectId, IQueryBuilder::PARAM_INT), IQueryBuilder::PARAM_INT)) |
|
| 1369 | - ->andWhere($query->expr()->eq('calendartype', $query->createNamedParameter($calendarType, IQueryBuilder::PARAM_INT), IQueryBuilder::PARAM_INT)) |
|
| 1370 | - ->executeStatement(); |
|
| 1371 | - |
|
| 1372 | - $this->purgeProperties($sourceCalendarId, $objectId); |
|
| 1373 | - $this->updateProperties($targetCalendarId, $object['uri'], $object['calendardata'], $calendarType); |
|
| 1374 | - |
|
| 1375 | - $this->addChange($sourceCalendarId, $object['uri'], 1, $calendarType); |
|
| 1376 | - $this->addChange($targetCalendarId, $object['uri'], 3, $calendarType); |
|
| 1377 | - |
|
| 1378 | - $object = $this->getCalendarObjectById($newPrincipalUri, $objectId); |
|
| 1379 | - // Calendar Object wasn't found - possibly because it was deleted in the meantime by a different client |
|
| 1380 | - if (empty($object)) { |
|
| 1381 | - return false; |
|
| 1382 | - } |
|
| 1383 | - |
|
| 1384 | - $targetCalendarRow = $this->getCalendarById($targetCalendarId); |
|
| 1385 | - // the calendar this event is being moved to does not exist any longer |
|
| 1386 | - if (empty($targetCalendarRow)) { |
|
| 1387 | - return false; |
|
| 1388 | - } |
|
| 1389 | - |
|
| 1390 | - if ($calendarType === self::CALENDAR_TYPE_CALENDAR) { |
|
| 1391 | - $sourceShares = $this->getShares($sourceCalendarId); |
|
| 1392 | - $targetShares = $this->getShares($targetCalendarId); |
|
| 1393 | - $sourceCalendarRow = $this->getCalendarById($sourceCalendarId); |
|
| 1394 | - $this->dispatcher->dispatchTyped(new CalendarObjectMovedEvent($sourceCalendarId, $sourceCalendarRow, $targetCalendarId, $targetCalendarRow, $sourceShares, $targetShares, $object)); |
|
| 1395 | - } |
|
| 1396 | - return true; |
|
| 1397 | - } |
|
| 1398 | - |
|
| 1399 | - |
|
| 1400 | - /** |
|
| 1401 | - * @param int $calendarObjectId |
|
| 1402 | - * @param int $classification |
|
| 1403 | - */ |
|
| 1404 | - public function setClassification($calendarObjectId, $classification) { |
|
| 1405 | - if (!in_array($classification, [ |
|
| 1406 | - self::CLASSIFICATION_PUBLIC, self::CLASSIFICATION_PRIVATE, self::CLASSIFICATION_CONFIDENTIAL |
|
| 1407 | - ])) { |
|
| 1408 | - throw new \InvalidArgumentException(); |
|
| 1409 | - } |
|
| 1410 | - $query = $this->db->getQueryBuilder(); |
|
| 1411 | - $query->update('calendarobjects') |
|
| 1412 | - ->set('classification', $query->createNamedParameter($classification)) |
|
| 1413 | - ->where($query->expr()->eq('id', $query->createNamedParameter($calendarObjectId))) |
|
| 1414 | - ->executeStatement(); |
|
| 1415 | - } |
|
| 1416 | - |
|
| 1417 | - /** |
|
| 1418 | - * Deletes an existing calendar object. |
|
| 1419 | - * |
|
| 1420 | - * The object uri is only the basename, or filename and not a full path. |
|
| 1421 | - * |
|
| 1422 | - * @param mixed $calendarId |
|
| 1423 | - * @param string $objectUri |
|
| 1424 | - * @param int $calendarType |
|
| 1425 | - * @param bool $forceDeletePermanently |
|
| 1426 | - * @return void |
|
| 1427 | - */ |
|
| 1428 | - public function deleteCalendarObject($calendarId, $objectUri, $calendarType = self::CALENDAR_TYPE_CALENDAR, bool $forceDeletePermanently = false) { |
|
| 1429 | - $data = $this->getCalendarObject($calendarId, $objectUri, $calendarType); |
|
| 1430 | - |
|
| 1431 | - if ($data === null) { |
|
| 1432 | - // Nothing to delete |
|
| 1433 | - return; |
|
| 1434 | - } |
|
| 1435 | - |
|
| 1436 | - if ($forceDeletePermanently || $this->config->getAppValue(Application::APP_ID, RetentionService::RETENTION_CONFIG_KEY) === '0') { |
|
| 1437 | - $stmt = $this->db->prepare('DELETE FROM `*PREFIX*calendarobjects` WHERE `calendarid` = ? AND `uri` = ? AND `calendartype` = ?'); |
|
| 1438 | - $stmt->execute([$calendarId, $objectUri, $calendarType]); |
|
| 1439 | - |
|
| 1440 | - $this->purgeProperties($calendarId, $data['id']); |
|
| 1441 | - |
|
| 1442 | - if ($calendarType === self::CALENDAR_TYPE_CALENDAR) { |
|
| 1443 | - $calendarRow = $this->getCalendarById($calendarId); |
|
| 1444 | - $shares = $this->getShares($calendarId); |
|
| 1445 | - |
|
| 1446 | - $this->dispatcher->dispatchTyped(new CalendarObjectDeletedEvent($calendarId, $calendarRow, $shares, $data)); |
|
| 1447 | - } else { |
|
| 1448 | - $subscriptionRow = $this->getSubscriptionById($calendarId); |
|
| 1449 | - |
|
| 1450 | - $this->dispatcher->dispatchTyped(new CachedCalendarObjectDeletedEvent($calendarId, $subscriptionRow, [], $data)); |
|
| 1451 | - } |
|
| 1452 | - } else { |
|
| 1453 | - $pathInfo = pathinfo($data['uri']); |
|
| 1454 | - if (!empty($pathInfo['extension'])) { |
|
| 1455 | - // Append a suffix to "free" the old URI for recreation |
|
| 1456 | - $newUri = sprintf( |
|
| 1457 | - "%s-deleted.%s", |
|
| 1458 | - $pathInfo['filename'], |
|
| 1459 | - $pathInfo['extension'] |
|
| 1460 | - ); |
|
| 1461 | - } else { |
|
| 1462 | - $newUri = sprintf( |
|
| 1463 | - "%s-deleted", |
|
| 1464 | - $pathInfo['filename'] |
|
| 1465 | - ); |
|
| 1466 | - } |
|
| 1467 | - |
|
| 1468 | - // Try to detect conflicts before the DB does |
|
| 1469 | - // As unlikely as it seems, this can happen when the user imports, then deletes, imports and deletes again |
|
| 1470 | - $newObject = $this->getCalendarObject($calendarId, $newUri, $calendarType); |
|
| 1471 | - if ($newObject !== null) { |
|
| 1472 | - throw new Forbidden("A calendar object with URI $newUri already exists in calendar $calendarId, therefore this object can't be moved into the trashbin"); |
|
| 1473 | - } |
|
| 1474 | - |
|
| 1475 | - $qb = $this->db->getQueryBuilder(); |
|
| 1476 | - $markObjectDeletedQuery = $qb->update('calendarobjects') |
|
| 1477 | - ->set('deleted_at', $qb->createNamedParameter(time(), IQueryBuilder::PARAM_INT)) |
|
| 1478 | - ->set('uri', $qb->createNamedParameter($newUri)) |
|
| 1479 | - ->where( |
|
| 1480 | - $qb->expr()->eq('calendarid', $qb->createNamedParameter($calendarId)), |
|
| 1481 | - $qb->expr()->eq('calendartype', $qb->createNamedParameter($calendarType, IQueryBuilder::PARAM_INT), IQueryBuilder::PARAM_INT), |
|
| 1482 | - $qb->expr()->eq('uri', $qb->createNamedParameter($objectUri)) |
|
| 1483 | - ); |
|
| 1484 | - $markObjectDeletedQuery->executeStatement(); |
|
| 1485 | - |
|
| 1486 | - $calendarData = $this->getCalendarById($calendarId); |
|
| 1487 | - if ($calendarData !== null) { |
|
| 1488 | - $this->dispatcher->dispatchTyped( |
|
| 1489 | - new CalendarObjectMovedToTrashEvent( |
|
| 1490 | - $calendarId, |
|
| 1491 | - $calendarData, |
|
| 1492 | - $this->getShares($calendarId), |
|
| 1493 | - $data |
|
| 1494 | - ) |
|
| 1495 | - ); |
|
| 1496 | - } |
|
| 1497 | - } |
|
| 1498 | - |
|
| 1499 | - $this->addChange($calendarId, $objectUri, 3, $calendarType); |
|
| 1500 | - } |
|
| 1501 | - |
|
| 1502 | - /** |
|
| 1503 | - * @param mixed $objectData |
|
| 1504 | - * |
|
| 1505 | - * @throws Forbidden |
|
| 1506 | - */ |
|
| 1507 | - public function restoreCalendarObject(array $objectData): void { |
|
| 1508 | - $id = (int) $objectData['id']; |
|
| 1509 | - $restoreUri = str_replace("-deleted.ics", ".ics", $objectData['uri']); |
|
| 1510 | - $targetObject = $this->getCalendarObject( |
|
| 1511 | - $objectData['calendarid'], |
|
| 1512 | - $restoreUri |
|
| 1513 | - ); |
|
| 1514 | - if ($targetObject !== null) { |
|
| 1515 | - throw new Forbidden("Can not restore calendar $id because a calendar object with the URI $restoreUri already exists"); |
|
| 1516 | - } |
|
| 1517 | - |
|
| 1518 | - $qb = $this->db->getQueryBuilder(); |
|
| 1519 | - $update = $qb->update('calendarobjects') |
|
| 1520 | - ->set('uri', $qb->createNamedParameter($restoreUri)) |
|
| 1521 | - ->set('deleted_at', $qb->createNamedParameter(null)) |
|
| 1522 | - ->where($qb->expr()->eq('id', $qb->createNamedParameter($id, IQueryBuilder::PARAM_INT), IQueryBuilder::PARAM_INT)); |
|
| 1523 | - $update->executeStatement(); |
|
| 1524 | - |
|
| 1525 | - // Make sure this change is tracked in the changes table |
|
| 1526 | - $qb2 = $this->db->getQueryBuilder(); |
|
| 1527 | - $selectObject = $qb2->select('calendardata', 'uri', 'calendarid', 'calendartype') |
|
| 1528 | - ->selectAlias('componenttype', 'component') |
|
| 1529 | - ->from('calendarobjects') |
|
| 1530 | - ->where($qb2->expr()->eq('id', $qb2->createNamedParameter($id, IQueryBuilder::PARAM_INT), IQueryBuilder::PARAM_INT)); |
|
| 1531 | - $result = $selectObject->executeQuery(); |
|
| 1532 | - $row = $result->fetch(); |
|
| 1533 | - $result->closeCursor(); |
|
| 1534 | - if ($row === false) { |
|
| 1535 | - // Welp, this should possibly not have happened, but let's ignore |
|
| 1536 | - return; |
|
| 1537 | - } |
|
| 1538 | - $this->addChange($row['calendarid'], $row['uri'], 1, (int) $row['calendartype']); |
|
| 1539 | - |
|
| 1540 | - $calendarRow = $this->getCalendarById((int) $row['calendarid']); |
|
| 1541 | - if ($calendarRow === null) { |
|
| 1542 | - throw new RuntimeException('Calendar object data that was just written can\'t be read back. Check your database configuration.'); |
|
| 1543 | - } |
|
| 1544 | - $this->dispatcher->dispatchTyped( |
|
| 1545 | - new CalendarObjectRestoredEvent( |
|
| 1546 | - (int) $objectData['calendarid'], |
|
| 1547 | - $calendarRow, |
|
| 1548 | - $this->getShares((int) $row['calendarid']), |
|
| 1549 | - $row |
|
| 1550 | - ) |
|
| 1551 | - ); |
|
| 1552 | - } |
|
| 1553 | - |
|
| 1554 | - /** |
|
| 1555 | - * Performs a calendar-query on the contents of this calendar. |
|
| 1556 | - * |
|
| 1557 | - * The calendar-query is defined in RFC4791 : CalDAV. Using the |
|
| 1558 | - * calendar-query it is possible for a client to request a specific set of |
|
| 1559 | - * object, based on contents of iCalendar properties, date-ranges and |
|
| 1560 | - * iCalendar component types (VTODO, VEVENT). |
|
| 1561 | - * |
|
| 1562 | - * This method should just return a list of (relative) urls that match this |
|
| 1563 | - * query. |
|
| 1564 | - * |
|
| 1565 | - * The list of filters are specified as an array. The exact array is |
|
| 1566 | - * documented by Sabre\CalDAV\CalendarQueryParser. |
|
| 1567 | - * |
|
| 1568 | - * Note that it is extremely likely that getCalendarObject for every path |
|
| 1569 | - * returned from this method will be called almost immediately after. You |
|
| 1570 | - * may want to anticipate this to speed up these requests. |
|
| 1571 | - * |
|
| 1572 | - * This method provides a default implementation, which parses *all* the |
|
| 1573 | - * iCalendar objects in the specified calendar. |
|
| 1574 | - * |
|
| 1575 | - * This default may well be good enough for personal use, and calendars |
|
| 1576 | - * that aren't very large. But if you anticipate high usage, big calendars |
|
| 1577 | - * or high loads, you are strongly advised to optimize certain paths. |
|
| 1578 | - * |
|
| 1579 | - * The best way to do so is override this method and to optimize |
|
| 1580 | - * specifically for 'common filters'. |
|
| 1581 | - * |
|
| 1582 | - * Requests that are extremely common are: |
|
| 1583 | - * * requests for just VEVENTS |
|
| 1584 | - * * requests for just VTODO |
|
| 1585 | - * * requests with a time-range-filter on either VEVENT or VTODO. |
|
| 1586 | - * |
|
| 1587 | - * ..and combinations of these requests. It may not be worth it to try to |
|
| 1588 | - * handle every possible situation and just rely on the (relatively |
|
| 1589 | - * easy to use) CalendarQueryValidator to handle the rest. |
|
| 1590 | - * |
|
| 1591 | - * Note that especially time-range-filters may be difficult to parse. A |
|
| 1592 | - * time-range filter specified on a VEVENT must for instance also handle |
|
| 1593 | - * recurrence rules correctly. |
|
| 1594 | - * A good example of how to interpret all these filters can also simply |
|
| 1595 | - * be found in Sabre\CalDAV\CalendarQueryFilter. This class is as correct |
|
| 1596 | - * as possible, so it gives you a good idea on what type of stuff you need |
|
| 1597 | - * to think of. |
|
| 1598 | - * |
|
| 1599 | - * @param mixed $calendarId |
|
| 1600 | - * @param array $filters |
|
| 1601 | - * @param int $calendarType |
|
| 1602 | - * @return array |
|
| 1603 | - */ |
|
| 1604 | - public function calendarQuery($calendarId, array $filters, $calendarType = self::CALENDAR_TYPE_CALENDAR):array { |
|
| 1605 | - $componentType = null; |
|
| 1606 | - $requirePostFilter = true; |
|
| 1607 | - $timeRange = null; |
|
| 1608 | - |
|
| 1609 | - // if no filters were specified, we don't need to filter after a query |
|
| 1610 | - if (!$filters['prop-filters'] && !$filters['comp-filters']) { |
|
| 1611 | - $requirePostFilter = false; |
|
| 1612 | - } |
|
| 1613 | - |
|
| 1614 | - // Figuring out if there's a component filter |
|
| 1615 | - if (count($filters['comp-filters']) > 0 && !$filters['comp-filters'][0]['is-not-defined']) { |
|
| 1616 | - $componentType = $filters['comp-filters'][0]['name']; |
|
| 1617 | - |
|
| 1618 | - // Checking if we need post-filters |
|
| 1619 | - if (!$filters['prop-filters'] && !$filters['comp-filters'][0]['comp-filters'] && !$filters['comp-filters'][0]['time-range'] && !$filters['comp-filters'][0]['prop-filters']) { |
|
| 1620 | - $requirePostFilter = false; |
|
| 1621 | - } |
|
| 1622 | - // There was a time-range filter |
|
| 1623 | - if ($componentType === 'VEVENT' && isset($filters['comp-filters'][0]['time-range']) && is_array($filters['comp-filters'][0]['time-range'])) { |
|
| 1624 | - $timeRange = $filters['comp-filters'][0]['time-range']; |
|
| 1625 | - |
|
| 1626 | - // If start time OR the end time is not specified, we can do a |
|
| 1627 | - // 100% accurate mysql query. |
|
| 1628 | - if (!$filters['prop-filters'] && !$filters['comp-filters'][0]['comp-filters'] && !$filters['comp-filters'][0]['prop-filters'] && (!$timeRange['start'] || !$timeRange['end'])) { |
|
| 1629 | - $requirePostFilter = false; |
|
| 1630 | - } |
|
| 1631 | - } |
|
| 1632 | - } |
|
| 1633 | - $columns = ['uri']; |
|
| 1634 | - if ($requirePostFilter) { |
|
| 1635 | - $columns = ['uri', 'calendardata']; |
|
| 1636 | - } |
|
| 1637 | - $query = $this->db->getQueryBuilder(); |
|
| 1638 | - $query->select($columns) |
|
| 1639 | - ->from('calendarobjects') |
|
| 1640 | - ->where($query->expr()->eq('calendarid', $query->createNamedParameter($calendarId))) |
|
| 1641 | - ->andWhere($query->expr()->eq('calendartype', $query->createNamedParameter($calendarType))) |
|
| 1642 | - ->andWhere($query->expr()->isNull('deleted_at')); |
|
| 1643 | - |
|
| 1644 | - if ($componentType) { |
|
| 1645 | - $query->andWhere($query->expr()->eq('componenttype', $query->createNamedParameter($componentType))); |
|
| 1646 | - } |
|
| 1647 | - |
|
| 1648 | - if ($timeRange && $timeRange['start']) { |
|
| 1649 | - $query->andWhere($query->expr()->gt('lastoccurence', $query->createNamedParameter($timeRange['start']->getTimeStamp()))); |
|
| 1650 | - } |
|
| 1651 | - if ($timeRange && $timeRange['end']) { |
|
| 1652 | - $query->andWhere($query->expr()->lt('firstoccurence', $query->createNamedParameter($timeRange['end']->getTimeStamp()))); |
|
| 1653 | - } |
|
| 1654 | - |
|
| 1655 | - $stmt = $query->executeQuery(); |
|
| 1656 | - |
|
| 1657 | - $result = []; |
|
| 1658 | - while ($row = $stmt->fetch()) { |
|
| 1659 | - if ($requirePostFilter) { |
|
| 1660 | - // validateFilterForObject will parse the calendar data |
|
| 1661 | - // catch parsing errors |
|
| 1662 | - try { |
|
| 1663 | - $matches = $this->validateFilterForObject($row, $filters); |
|
| 1664 | - } catch (ParseException $ex) { |
|
| 1665 | - $this->logger->error('Caught parsing exception for calendar data. This usually indicates invalid calendar data. calendar-id:'.$calendarId.' uri:'.$row['uri'], [ |
|
| 1666 | - 'app' => 'dav', |
|
| 1667 | - 'exception' => $ex, |
|
| 1668 | - ]); |
|
| 1669 | - continue; |
|
| 1670 | - } catch (InvalidDataException $ex) { |
|
| 1671 | - $this->logger->error('Caught invalid data exception for calendar data. This usually indicates invalid calendar data. calendar-id:'.$calendarId.' uri:'.$row['uri'], [ |
|
| 1672 | - 'app' => 'dav', |
|
| 1673 | - 'exception' => $ex, |
|
| 1674 | - ]); |
|
| 1675 | - continue; |
|
| 1676 | - } |
|
| 1677 | - |
|
| 1678 | - if (!$matches) { |
|
| 1679 | - continue; |
|
| 1680 | - } |
|
| 1681 | - } |
|
| 1682 | - $result[] = $row['uri']; |
|
| 1683 | - } |
|
| 1684 | - |
|
| 1685 | - return $result; |
|
| 1686 | - } |
|
| 1687 | - |
|
| 1688 | - /** |
|
| 1689 | - * custom Nextcloud search extension for CalDAV |
|
| 1690 | - * |
|
| 1691 | - * TODO - this should optionally cover cached calendar objects as well |
|
| 1692 | - * |
|
| 1693 | - * @param string $principalUri |
|
| 1694 | - * @param array $filters |
|
| 1695 | - * @param integer|null $limit |
|
| 1696 | - * @param integer|null $offset |
|
| 1697 | - * @return array |
|
| 1698 | - */ |
|
| 1699 | - public function calendarSearch($principalUri, array $filters, $limit = null, $offset = null) { |
|
| 1700 | - $calendars = $this->getCalendarsForUser($principalUri); |
|
| 1701 | - $ownCalendars = []; |
|
| 1702 | - $sharedCalendars = []; |
|
| 1703 | - |
|
| 1704 | - $uriMapper = []; |
|
| 1705 | - |
|
| 1706 | - foreach ($calendars as $calendar) { |
|
| 1707 | - if ($calendar['{http://owncloud.org/ns}owner-principal'] === $principalUri) { |
|
| 1708 | - $ownCalendars[] = $calendar['id']; |
|
| 1709 | - } else { |
|
| 1710 | - $sharedCalendars[] = $calendar['id']; |
|
| 1711 | - } |
|
| 1712 | - $uriMapper[$calendar['id']] = $calendar['uri']; |
|
| 1713 | - } |
|
| 1714 | - if (count($ownCalendars) === 0 && count($sharedCalendars) === 0) { |
|
| 1715 | - return []; |
|
| 1716 | - } |
|
| 1717 | - |
|
| 1718 | - $query = $this->db->getQueryBuilder(); |
|
| 1719 | - // Calendar id expressions |
|
| 1720 | - $calendarExpressions = []; |
|
| 1721 | - foreach ($ownCalendars as $id) { |
|
| 1722 | - $calendarExpressions[] = $query->expr()->andX( |
|
| 1723 | - $query->expr()->eq('c.calendarid', |
|
| 1724 | - $query->createNamedParameter($id)), |
|
| 1725 | - $query->expr()->eq('c.calendartype', |
|
| 1726 | - $query->createNamedParameter(self::CALENDAR_TYPE_CALENDAR))); |
|
| 1727 | - } |
|
| 1728 | - foreach ($sharedCalendars as $id) { |
|
| 1729 | - $calendarExpressions[] = $query->expr()->andX( |
|
| 1730 | - $query->expr()->eq('c.calendarid', |
|
| 1731 | - $query->createNamedParameter($id)), |
|
| 1732 | - $query->expr()->eq('c.classification', |
|
| 1733 | - $query->createNamedParameter(self::CLASSIFICATION_PUBLIC)), |
|
| 1734 | - $query->expr()->eq('c.calendartype', |
|
| 1735 | - $query->createNamedParameter(self::CALENDAR_TYPE_CALENDAR))); |
|
| 1736 | - } |
|
| 1737 | - |
|
| 1738 | - if (count($calendarExpressions) === 1) { |
|
| 1739 | - $calExpr = $calendarExpressions[0]; |
|
| 1740 | - } else { |
|
| 1741 | - $calExpr = call_user_func_array([$query->expr(), 'orX'], $calendarExpressions); |
|
| 1742 | - } |
|
| 1743 | - |
|
| 1744 | - // Component expressions |
|
| 1745 | - $compExpressions = []; |
|
| 1746 | - foreach ($filters['comps'] as $comp) { |
|
| 1747 | - $compExpressions[] = $query->expr() |
|
| 1748 | - ->eq('c.componenttype', $query->createNamedParameter($comp)); |
|
| 1749 | - } |
|
| 1750 | - |
|
| 1751 | - if (count($compExpressions) === 1) { |
|
| 1752 | - $compExpr = $compExpressions[0]; |
|
| 1753 | - } else { |
|
| 1754 | - $compExpr = call_user_func_array([$query->expr(), 'orX'], $compExpressions); |
|
| 1755 | - } |
|
| 1756 | - |
|
| 1757 | - if (!isset($filters['props'])) { |
|
| 1758 | - $filters['props'] = []; |
|
| 1759 | - } |
|
| 1760 | - if (!isset($filters['params'])) { |
|
| 1761 | - $filters['params'] = []; |
|
| 1762 | - } |
|
| 1763 | - |
|
| 1764 | - $propParamExpressions = []; |
|
| 1765 | - foreach ($filters['props'] as $prop) { |
|
| 1766 | - $propParamExpressions[] = $query->expr()->andX( |
|
| 1767 | - $query->expr()->eq('i.name', $query->createNamedParameter($prop)), |
|
| 1768 | - $query->expr()->isNull('i.parameter') |
|
| 1769 | - ); |
|
| 1770 | - } |
|
| 1771 | - foreach ($filters['params'] as $param) { |
|
| 1772 | - $propParamExpressions[] = $query->expr()->andX( |
|
| 1773 | - $query->expr()->eq('i.name', $query->createNamedParameter($param['property'])), |
|
| 1774 | - $query->expr()->eq('i.parameter', $query->createNamedParameter($param['parameter'])) |
|
| 1775 | - ); |
|
| 1776 | - } |
|
| 1777 | - |
|
| 1778 | - if (count($propParamExpressions) === 1) { |
|
| 1779 | - $propParamExpr = $propParamExpressions[0]; |
|
| 1780 | - } else { |
|
| 1781 | - $propParamExpr = call_user_func_array([$query->expr(), 'orX'], $propParamExpressions); |
|
| 1782 | - } |
|
| 1783 | - |
|
| 1784 | - $query->select(['c.calendarid', 'c.uri']) |
|
| 1785 | - ->from($this->dbObjectPropertiesTable, 'i') |
|
| 1786 | - ->join('i', 'calendarobjects', 'c', $query->expr()->eq('i.objectid', 'c.id')) |
|
| 1787 | - ->where($calExpr) |
|
| 1788 | - ->andWhere($compExpr) |
|
| 1789 | - ->andWhere($propParamExpr) |
|
| 1790 | - ->andWhere($query->expr()->iLike('i.value', |
|
| 1791 | - $query->createNamedParameter('%'.$this->db->escapeLikeParameter($filters['search-term']).'%'))) |
|
| 1792 | - ->andWhere($query->expr()->isNull('deleted_at')); |
|
| 1793 | - |
|
| 1794 | - if ($offset) { |
|
| 1795 | - $query->setFirstResult($offset); |
|
| 1796 | - } |
|
| 1797 | - if ($limit) { |
|
| 1798 | - $query->setMaxResults($limit); |
|
| 1799 | - } |
|
| 1800 | - |
|
| 1801 | - $stmt = $query->executeQuery(); |
|
| 1802 | - |
|
| 1803 | - $result = []; |
|
| 1804 | - while ($row = $stmt->fetch()) { |
|
| 1805 | - $path = $uriMapper[$row['calendarid']] . '/' . $row['uri']; |
|
| 1806 | - if (!in_array($path, $result)) { |
|
| 1807 | - $result[] = $path; |
|
| 1808 | - } |
|
| 1809 | - } |
|
| 1810 | - |
|
| 1811 | - return $result; |
|
| 1812 | - } |
|
| 1813 | - |
|
| 1814 | - /** |
|
| 1815 | - * used for Nextcloud's calendar API |
|
| 1816 | - * |
|
| 1817 | - * @param array $calendarInfo |
|
| 1818 | - * @param string $pattern |
|
| 1819 | - * @param array $searchProperties |
|
| 1820 | - * @param array $options |
|
| 1821 | - * @param integer|null $limit |
|
| 1822 | - * @param integer|null $offset |
|
| 1823 | - * |
|
| 1824 | - * @return array |
|
| 1825 | - */ |
|
| 1826 | - public function search(array $calendarInfo, $pattern, array $searchProperties, |
|
| 1827 | - array $options, $limit, $offset) { |
|
| 1828 | - $outerQuery = $this->db->getQueryBuilder(); |
|
| 1829 | - $innerQuery = $this->db->getQueryBuilder(); |
|
| 1830 | - |
|
| 1831 | - $innerQuery->selectDistinct('op.objectid') |
|
| 1832 | - ->from($this->dbObjectPropertiesTable, 'op') |
|
| 1833 | - ->andWhere($innerQuery->expr()->eq('op.calendarid', |
|
| 1834 | - $outerQuery->createNamedParameter($calendarInfo['id']))) |
|
| 1835 | - ->andWhere($innerQuery->expr()->eq('op.calendartype', |
|
| 1836 | - $outerQuery->createNamedParameter(self::CALENDAR_TYPE_CALENDAR))); |
|
| 1837 | - |
|
| 1838 | - // only return public items for shared calendars for now |
|
| 1839 | - if (isset($calendarInfo['{http://owncloud.org/ns}owner-principal']) === false || $calendarInfo['principaluri'] !== $calendarInfo['{http://owncloud.org/ns}owner-principal']) { |
|
| 1840 | - $innerQuery->andWhere($innerQuery->expr()->eq('c.classification', |
|
| 1841 | - $outerQuery->createNamedParameter(self::CLASSIFICATION_PUBLIC))); |
|
| 1842 | - } |
|
| 1843 | - |
|
| 1844 | - if (!empty($searchProperties)) { |
|
| 1845 | - $or = $innerQuery->expr()->orX(); |
|
| 1846 | - foreach ($searchProperties as $searchProperty) { |
|
| 1847 | - $or->add($innerQuery->expr()->eq('op.name', |
|
| 1848 | - $outerQuery->createNamedParameter($searchProperty))); |
|
| 1849 | - } |
|
| 1850 | - $innerQuery->andWhere($or); |
|
| 1851 | - } |
|
| 1852 | - |
|
| 1853 | - if ($pattern !== '') { |
|
| 1854 | - $innerQuery->andWhere($innerQuery->expr()->iLike('op.value', |
|
| 1855 | - $outerQuery->createNamedParameter('%' . |
|
| 1856 | - $this->db->escapeLikeParameter($pattern) . '%'))); |
|
| 1857 | - } |
|
| 1858 | - |
|
| 1859 | - $outerQuery->select('c.id', 'c.calendardata', 'c.componenttype', 'c.uid', 'c.uri') |
|
| 1860 | - ->from('calendarobjects', 'c') |
|
| 1861 | - ->where($outerQuery->expr()->isNull('deleted_at')); |
|
| 1862 | - |
|
| 1863 | - if (isset($options['timerange'])) { |
|
| 1864 | - if (isset($options['timerange']['start']) && $options['timerange']['start'] instanceof DateTimeInterface) { |
|
| 1865 | - $outerQuery->andWhere($outerQuery->expr()->gt('lastoccurence', |
|
| 1866 | - $outerQuery->createNamedParameter($options['timerange']['start']->getTimeStamp()))); |
|
| 1867 | - } |
|
| 1868 | - if (isset($options['timerange']['end']) && $options['timerange']['end'] instanceof DateTimeInterface) { |
|
| 1869 | - $outerQuery->andWhere($outerQuery->expr()->lt('firstoccurence', |
|
| 1870 | - $outerQuery->createNamedParameter($options['timerange']['end']->getTimeStamp()))); |
|
| 1871 | - } |
|
| 1872 | - } |
|
| 1873 | - |
|
| 1874 | - if(isset($options['uid'])) { |
|
| 1875 | - $outerQuery->andWhere($outerQuery->expr()->eq('uid', $outerQuery->createNamedParameter($options['uid']))); |
|
| 1876 | - } |
|
| 1877 | - |
|
| 1878 | - if (!empty($options['types'])) { |
|
| 1879 | - $or = $outerQuery->expr()->orX(); |
|
| 1880 | - foreach ($options['types'] as $type) { |
|
| 1881 | - $or->add($outerQuery->expr()->eq('componenttype', |
|
| 1882 | - $outerQuery->createNamedParameter($type))); |
|
| 1883 | - } |
|
| 1884 | - $outerQuery->andWhere($or); |
|
| 1885 | - } |
|
| 1886 | - |
|
| 1887 | - $outerQuery->andWhere($outerQuery->expr()->in('c.id', $outerQuery->createFunction($innerQuery->getSQL()))); |
|
| 1888 | - |
|
| 1889 | - if ($offset) { |
|
| 1890 | - $outerQuery->setFirstResult($offset); |
|
| 1891 | - } |
|
| 1892 | - if ($limit) { |
|
| 1893 | - $outerQuery->setMaxResults($limit); |
|
| 1894 | - } |
|
| 1895 | - |
|
| 1896 | - $result = $outerQuery->executeQuery(); |
|
| 1897 | - $calendarObjects = array_filter($result->fetchAll(), function (array $row) use ($options) { |
|
| 1898 | - $start = $options['timerange']['start'] ?? null; |
|
| 1899 | - $end = $options['timerange']['end'] ?? null; |
|
| 1900 | - |
|
| 1901 | - if ($start === null || !($start instanceof DateTimeInterface) || $end === null || !($end instanceof DateTimeInterface)) { |
|
| 1902 | - // No filter required |
|
| 1903 | - return true; |
|
| 1904 | - } |
|
| 1905 | - |
|
| 1906 | - $isValid = $this->validateFilterForObject($row, [ |
|
| 1907 | - 'name' => 'VCALENDAR', |
|
| 1908 | - 'comp-filters' => [ |
|
| 1909 | - [ |
|
| 1910 | - 'name' => 'VEVENT', |
|
| 1911 | - 'comp-filters' => [], |
|
| 1912 | - 'prop-filters' => [], |
|
| 1913 | - 'is-not-defined' => false, |
|
| 1914 | - 'time-range' => [ |
|
| 1915 | - 'start' => $start, |
|
| 1916 | - 'end' => $end, |
|
| 1917 | - ], |
|
| 1918 | - ], |
|
| 1919 | - ], |
|
| 1920 | - 'prop-filters' => [], |
|
| 1921 | - 'is-not-defined' => false, |
|
| 1922 | - 'time-range' => null, |
|
| 1923 | - ]); |
|
| 1924 | - if (is_resource($row['calendardata'])) { |
|
| 1925 | - // Put the stream back to the beginning so it can be read another time |
|
| 1926 | - rewind($row['calendardata']); |
|
| 1927 | - } |
|
| 1928 | - return $isValid; |
|
| 1929 | - }); |
|
| 1930 | - $result->closeCursor(); |
|
| 1931 | - |
|
| 1932 | - return array_map(function ($o) { |
|
| 1933 | - $calendarData = Reader::read($o['calendardata']); |
|
| 1934 | - $comps = $calendarData->getComponents(); |
|
| 1935 | - $objects = []; |
|
| 1936 | - $timezones = []; |
|
| 1937 | - foreach ($comps as $comp) { |
|
| 1938 | - if ($comp instanceof VTimeZone) { |
|
| 1939 | - $timezones[] = $comp; |
|
| 1940 | - } else { |
|
| 1941 | - $objects[] = $comp; |
|
| 1942 | - } |
|
| 1943 | - } |
|
| 1944 | - |
|
| 1945 | - return [ |
|
| 1946 | - 'id' => $o['id'], |
|
| 1947 | - 'type' => $o['componenttype'], |
|
| 1948 | - 'uid' => $o['uid'], |
|
| 1949 | - 'uri' => $o['uri'], |
|
| 1950 | - 'objects' => array_map(function ($c) { |
|
| 1951 | - return $this->transformSearchData($c); |
|
| 1952 | - }, $objects), |
|
| 1953 | - 'timezones' => array_map(function ($c) { |
|
| 1954 | - return $this->transformSearchData($c); |
|
| 1955 | - }, $timezones), |
|
| 1956 | - ]; |
|
| 1957 | - }, $calendarObjects); |
|
| 1958 | - } |
|
| 1959 | - |
|
| 1960 | - /** |
|
| 1961 | - * @param Component $comp |
|
| 1962 | - * @return array |
|
| 1963 | - */ |
|
| 1964 | - private function transformSearchData(Component $comp) { |
|
| 1965 | - $data = []; |
|
| 1966 | - /** @var Component[] $subComponents */ |
|
| 1967 | - $subComponents = $comp->getComponents(); |
|
| 1968 | - /** @var Property[] $properties */ |
|
| 1969 | - $properties = array_filter($comp->children(), function ($c) { |
|
| 1970 | - return $c instanceof Property; |
|
| 1971 | - }); |
|
| 1972 | - $validationRules = $comp->getValidationRules(); |
|
| 1973 | - |
|
| 1974 | - foreach ($subComponents as $subComponent) { |
|
| 1975 | - $name = $subComponent->name; |
|
| 1976 | - if (!isset($data[$name])) { |
|
| 1977 | - $data[$name] = []; |
|
| 1978 | - } |
|
| 1979 | - $data[$name][] = $this->transformSearchData($subComponent); |
|
| 1980 | - } |
|
| 1981 | - |
|
| 1982 | - foreach ($properties as $property) { |
|
| 1983 | - $name = $property->name; |
|
| 1984 | - if (!isset($validationRules[$name])) { |
|
| 1985 | - $validationRules[$name] = '*'; |
|
| 1986 | - } |
|
| 1987 | - |
|
| 1988 | - $rule = $validationRules[$property->name]; |
|
| 1989 | - if ($rule === '+' || $rule === '*') { // multiple |
|
| 1990 | - if (!isset($data[$name])) { |
|
| 1991 | - $data[$name] = []; |
|
| 1992 | - } |
|
| 1993 | - |
|
| 1994 | - $data[$name][] = $this->transformSearchProperty($property); |
|
| 1995 | - } else { // once |
|
| 1996 | - $data[$name] = $this->transformSearchProperty($property); |
|
| 1997 | - } |
|
| 1998 | - } |
|
| 1999 | - |
|
| 2000 | - return $data; |
|
| 2001 | - } |
|
| 2002 | - |
|
| 2003 | - /** |
|
| 2004 | - * @param Property $prop |
|
| 2005 | - * @return array |
|
| 2006 | - */ |
|
| 2007 | - private function transformSearchProperty(Property $prop) { |
|
| 2008 | - // No need to check Date, as it extends DateTime |
|
| 2009 | - if ($prop instanceof Property\ICalendar\DateTime) { |
|
| 2010 | - $value = $prop->getDateTime(); |
|
| 2011 | - } else { |
|
| 2012 | - $value = $prop->getValue(); |
|
| 2013 | - } |
|
| 2014 | - |
|
| 2015 | - return [ |
|
| 2016 | - $value, |
|
| 2017 | - $prop->parameters() |
|
| 2018 | - ]; |
|
| 2019 | - } |
|
| 2020 | - |
|
| 2021 | - /** |
|
| 2022 | - * @param string $principalUri |
|
| 2023 | - * @param string $pattern |
|
| 2024 | - * @param array $componentTypes |
|
| 2025 | - * @param array $searchProperties |
|
| 2026 | - * @param array $searchParameters |
|
| 2027 | - * @param array $options |
|
| 2028 | - * @return array |
|
| 2029 | - */ |
|
| 2030 | - public function searchPrincipalUri(string $principalUri, |
|
| 2031 | - string $pattern, |
|
| 2032 | - array $componentTypes, |
|
| 2033 | - array $searchProperties, |
|
| 2034 | - array $searchParameters, |
|
| 2035 | - array $options = []): array { |
|
| 2036 | - $escapePattern = !\array_key_exists('escape_like_param', $options) || $options['escape_like_param'] !== false; |
|
| 2037 | - |
|
| 2038 | - $calendarObjectIdQuery = $this->db->getQueryBuilder(); |
|
| 2039 | - $calendarOr = $calendarObjectIdQuery->expr()->orX(); |
|
| 2040 | - $searchOr = $calendarObjectIdQuery->expr()->orX(); |
|
| 2041 | - |
|
| 2042 | - // Fetch calendars and subscription |
|
| 2043 | - $calendars = $this->getCalendarsForUser($principalUri); |
|
| 2044 | - $subscriptions = $this->getSubscriptionsForUser($principalUri); |
|
| 2045 | - foreach ($calendars as $calendar) { |
|
| 2046 | - $calendarAnd = $calendarObjectIdQuery->expr()->andX(); |
|
| 2047 | - $calendarAnd->add($calendarObjectIdQuery->expr()->eq('cob.calendarid', $calendarObjectIdQuery->createNamedParameter((int)$calendar['id']))); |
|
| 2048 | - $calendarAnd->add($calendarObjectIdQuery->expr()->eq('cob.calendartype', $calendarObjectIdQuery->createNamedParameter(self::CALENDAR_TYPE_CALENDAR))); |
|
| 2049 | - |
|
| 2050 | - // If it's shared, limit search to public events |
|
| 2051 | - if (isset($calendar['{http://owncloud.org/ns}owner-principal']) |
|
| 2052 | - && $calendar['principaluri'] !== $calendar['{http://owncloud.org/ns}owner-principal']) { |
|
| 2053 | - $calendarAnd->add($calendarObjectIdQuery->expr()->eq('co.classification', $calendarObjectIdQuery->createNamedParameter(self::CLASSIFICATION_PUBLIC))); |
|
| 2054 | - } |
|
| 2055 | - |
|
| 2056 | - $calendarOr->add($calendarAnd); |
|
| 2057 | - } |
|
| 2058 | - foreach ($subscriptions as $subscription) { |
|
| 2059 | - $subscriptionAnd = $calendarObjectIdQuery->expr()->andX(); |
|
| 2060 | - $subscriptionAnd->add($calendarObjectIdQuery->expr()->eq('cob.calendarid', $calendarObjectIdQuery->createNamedParameter((int)$subscription['id']))); |
|
| 2061 | - $subscriptionAnd->add($calendarObjectIdQuery->expr()->eq('cob.calendartype', $calendarObjectIdQuery->createNamedParameter(self::CALENDAR_TYPE_SUBSCRIPTION))); |
|
| 2062 | - |
|
| 2063 | - // If it's shared, limit search to public events |
|
| 2064 | - if (isset($subscription['{http://owncloud.org/ns}owner-principal']) |
|
| 2065 | - && $subscription['principaluri'] !== $subscription['{http://owncloud.org/ns}owner-principal']) { |
|
| 2066 | - $subscriptionAnd->add($calendarObjectIdQuery->expr()->eq('co.classification', $calendarObjectIdQuery->createNamedParameter(self::CLASSIFICATION_PUBLIC))); |
|
| 2067 | - } |
|
| 2068 | - |
|
| 2069 | - $calendarOr->add($subscriptionAnd); |
|
| 2070 | - } |
|
| 2071 | - |
|
| 2072 | - foreach ($searchProperties as $property) { |
|
| 2073 | - $propertyAnd = $calendarObjectIdQuery->expr()->andX(); |
|
| 2074 | - $propertyAnd->add($calendarObjectIdQuery->expr()->eq('cob.name', $calendarObjectIdQuery->createNamedParameter($property, IQueryBuilder::PARAM_STR))); |
|
| 2075 | - $propertyAnd->add($calendarObjectIdQuery->expr()->isNull('cob.parameter')); |
|
| 2076 | - |
|
| 2077 | - $searchOr->add($propertyAnd); |
|
| 2078 | - } |
|
| 2079 | - foreach ($searchParameters as $property => $parameter) { |
|
| 2080 | - $parameterAnd = $calendarObjectIdQuery->expr()->andX(); |
|
| 2081 | - $parameterAnd->add($calendarObjectIdQuery->expr()->eq('cob.name', $calendarObjectIdQuery->createNamedParameter($property, IQueryBuilder::PARAM_STR))); |
|
| 2082 | - $parameterAnd->add($calendarObjectIdQuery->expr()->eq('cob.parameter', $calendarObjectIdQuery->createNamedParameter($parameter, IQueryBuilder::PARAM_STR_ARRAY))); |
|
| 2083 | - |
|
| 2084 | - $searchOr->add($parameterAnd); |
|
| 2085 | - } |
|
| 2086 | - |
|
| 2087 | - if ($calendarOr->count() === 0) { |
|
| 2088 | - return []; |
|
| 2089 | - } |
|
| 2090 | - if ($searchOr->count() === 0) { |
|
| 2091 | - return []; |
|
| 2092 | - } |
|
| 2093 | - |
|
| 2094 | - $calendarObjectIdQuery->selectDistinct('cob.objectid') |
|
| 2095 | - ->from($this->dbObjectPropertiesTable, 'cob') |
|
| 2096 | - ->leftJoin('cob', 'calendarobjects', 'co', $calendarObjectIdQuery->expr()->eq('co.id', 'cob.objectid')) |
|
| 2097 | - ->andWhere($calendarObjectIdQuery->expr()->in('co.componenttype', $calendarObjectIdQuery->createNamedParameter($componentTypes, IQueryBuilder::PARAM_STR_ARRAY))) |
|
| 2098 | - ->andWhere($calendarOr) |
|
| 2099 | - ->andWhere($searchOr) |
|
| 2100 | - ->andWhere($calendarObjectIdQuery->expr()->isNull('deleted_at')); |
|
| 2101 | - |
|
| 2102 | - if ('' !== $pattern) { |
|
| 2103 | - if (!$escapePattern) { |
|
| 2104 | - $calendarObjectIdQuery->andWhere($calendarObjectIdQuery->expr()->ilike('cob.value', $calendarObjectIdQuery->createNamedParameter($pattern))); |
|
| 2105 | - } else { |
|
| 2106 | - $calendarObjectIdQuery->andWhere($calendarObjectIdQuery->expr()->ilike('cob.value', $calendarObjectIdQuery->createNamedParameter('%' . $this->db->escapeLikeParameter($pattern) . '%'))); |
|
| 2107 | - } |
|
| 2108 | - } |
|
| 2109 | - |
|
| 2110 | - if (isset($options['limit'])) { |
|
| 2111 | - $calendarObjectIdQuery->setMaxResults($options['limit']); |
|
| 2112 | - } |
|
| 2113 | - if (isset($options['offset'])) { |
|
| 2114 | - $calendarObjectIdQuery->setFirstResult($options['offset']); |
|
| 2115 | - } |
|
| 2116 | - |
|
| 2117 | - $result = $calendarObjectIdQuery->executeQuery(); |
|
| 2118 | - $matches = $result->fetchAll(); |
|
| 2119 | - $result->closeCursor(); |
|
| 2120 | - $matches = array_map(static function (array $match):int { |
|
| 2121 | - return (int) $match['objectid']; |
|
| 2122 | - }, $matches); |
|
| 2123 | - |
|
| 2124 | - $query = $this->db->getQueryBuilder(); |
|
| 2125 | - $query->select('calendardata', 'uri', 'calendarid', 'calendartype') |
|
| 2126 | - ->from('calendarobjects') |
|
| 2127 | - ->where($query->expr()->in('id', $query->createNamedParameter($matches, IQueryBuilder::PARAM_INT_ARRAY))); |
|
| 2128 | - |
|
| 2129 | - $result = $query->executeQuery(); |
|
| 2130 | - $calendarObjects = $result->fetchAll(); |
|
| 2131 | - $result->closeCursor(); |
|
| 2132 | - |
|
| 2133 | - return array_map(function (array $array): array { |
|
| 2134 | - $array['calendarid'] = (int)$array['calendarid']; |
|
| 2135 | - $array['calendartype'] = (int)$array['calendartype']; |
|
| 2136 | - $array['calendardata'] = $this->readBlob($array['calendardata']); |
|
| 2137 | - |
|
| 2138 | - return $array; |
|
| 2139 | - }, $calendarObjects); |
|
| 2140 | - } |
|
| 2141 | - |
|
| 2142 | - /** |
|
| 2143 | - * Searches through all of a users calendars and calendar objects to find |
|
| 2144 | - * an object with a specific UID. |
|
| 2145 | - * |
|
| 2146 | - * This method should return the path to this object, relative to the |
|
| 2147 | - * calendar home, so this path usually only contains two parts: |
|
| 2148 | - * |
|
| 2149 | - * calendarpath/objectpath.ics |
|
| 2150 | - * |
|
| 2151 | - * If the uid is not found, return null. |
|
| 2152 | - * |
|
| 2153 | - * This method should only consider * objects that the principal owns, so |
|
| 2154 | - * any calendars owned by other principals that also appear in this |
|
| 2155 | - * collection should be ignored. |
|
| 2156 | - * |
|
| 2157 | - * @param string $principalUri |
|
| 2158 | - * @param string $uid |
|
| 2159 | - * @return string|null |
|
| 2160 | - */ |
|
| 2161 | - public function getCalendarObjectByUID($principalUri, $uid) { |
|
| 2162 | - $query = $this->db->getQueryBuilder(); |
|
| 2163 | - $query->selectAlias('c.uri', 'calendaruri')->selectAlias('co.uri', 'objecturi') |
|
| 2164 | - ->from('calendarobjects', 'co') |
|
| 2165 | - ->leftJoin('co', 'calendars', 'c', $query->expr()->eq('co.calendarid', 'c.id')) |
|
| 2166 | - ->where($query->expr()->eq('c.principaluri', $query->createNamedParameter($principalUri))) |
|
| 2167 | - ->andWhere($query->expr()->eq('co.uid', $query->createNamedParameter($uid))) |
|
| 2168 | - ->andWhere($query->expr()->isNull('co.deleted_at')); |
|
| 2169 | - $stmt = $query->executeQuery(); |
|
| 2170 | - $row = $stmt->fetch(); |
|
| 2171 | - $stmt->closeCursor(); |
|
| 2172 | - if ($row) { |
|
| 2173 | - return $row['calendaruri'] . '/' . $row['objecturi']; |
|
| 2174 | - } |
|
| 2175 | - |
|
| 2176 | - return null; |
|
| 2177 | - } |
|
| 2178 | - |
|
| 2179 | - public function getCalendarObjectById(string $principalUri, int $id): ?array { |
|
| 2180 | - $query = $this->db->getQueryBuilder(); |
|
| 2181 | - $query->select(['co.id', 'co.uri', 'co.lastmodified', 'co.etag', 'co.calendarid', 'co.size', 'co.calendardata', 'co.componenttype', 'co.classification', 'co.deleted_at']) |
|
| 2182 | - ->selectAlias('c.uri', 'calendaruri') |
|
| 2183 | - ->from('calendarobjects', 'co') |
|
| 2184 | - ->join('co', 'calendars', 'c', $query->expr()->eq('c.id', 'co.calendarid', IQueryBuilder::PARAM_INT)) |
|
| 2185 | - ->where($query->expr()->eq('c.principaluri', $query->createNamedParameter($principalUri))) |
|
| 2186 | - ->andWhere($query->expr()->eq('co.id', $query->createNamedParameter($id, IQueryBuilder::PARAM_INT), IQueryBuilder::PARAM_INT)); |
|
| 2187 | - $stmt = $query->executeQuery(); |
|
| 2188 | - $row = $stmt->fetch(); |
|
| 2189 | - $stmt->closeCursor(); |
|
| 2190 | - |
|
| 2191 | - if (!$row) { |
|
| 2192 | - return null; |
|
| 2193 | - } |
|
| 2194 | - |
|
| 2195 | - return [ |
|
| 2196 | - 'id' => $row['id'], |
|
| 2197 | - 'uri' => $row['uri'], |
|
| 2198 | - 'lastmodified' => $row['lastmodified'], |
|
| 2199 | - 'etag' => '"' . $row['etag'] . '"', |
|
| 2200 | - 'calendarid' => $row['calendarid'], |
|
| 2201 | - 'calendaruri' => $row['calendaruri'], |
|
| 2202 | - 'size' => (int)$row['size'], |
|
| 2203 | - 'calendardata' => $this->readBlob($row['calendardata']), |
|
| 2204 | - 'component' => strtolower($row['componenttype']), |
|
| 2205 | - 'classification' => (int)$row['classification'], |
|
| 2206 | - 'deleted_at' => isset($row['deleted_at']) ? ((int) $row['deleted_at']) : null, |
|
| 2207 | - ]; |
|
| 2208 | - } |
|
| 2209 | - |
|
| 2210 | - /** |
|
| 2211 | - * The getChanges method returns all the changes that have happened, since |
|
| 2212 | - * the specified syncToken in the specified calendar. |
|
| 2213 | - * |
|
| 2214 | - * This function should return an array, such as the following: |
|
| 2215 | - * |
|
| 2216 | - * [ |
|
| 2217 | - * 'syncToken' => 'The current synctoken', |
|
| 2218 | - * 'added' => [ |
|
| 2219 | - * 'new.txt', |
|
| 2220 | - * ], |
|
| 2221 | - * 'modified' => [ |
|
| 2222 | - * 'modified.txt', |
|
| 2223 | - * ], |
|
| 2224 | - * 'deleted' => [ |
|
| 2225 | - * 'foo.php.bak', |
|
| 2226 | - * 'old.txt' |
|
| 2227 | - * ] |
|
| 2228 | - * ); |
|
| 2229 | - * |
|
| 2230 | - * The returned syncToken property should reflect the *current* syncToken |
|
| 2231 | - * of the calendar, as reported in the {http://sabredav.org/ns}sync-token |
|
| 2232 | - * property This is * needed here too, to ensure the operation is atomic. |
|
| 2233 | - * |
|
| 2234 | - * If the $syncToken argument is specified as null, this is an initial |
|
| 2235 | - * sync, and all members should be reported. |
|
| 2236 | - * |
|
| 2237 | - * The modified property is an array of nodenames that have changed since |
|
| 2238 | - * the last token. |
|
| 2239 | - * |
|
| 2240 | - * The deleted property is an array with nodenames, that have been deleted |
|
| 2241 | - * from collection. |
|
| 2242 | - * |
|
| 2243 | - * The $syncLevel argument is basically the 'depth' of the report. If it's |
|
| 2244 | - * 1, you only have to report changes that happened only directly in |
|
| 2245 | - * immediate descendants. If it's 2, it should also include changes from |
|
| 2246 | - * the nodes below the child collections. (grandchildren) |
|
| 2247 | - * |
|
| 2248 | - * The $limit argument allows a client to specify how many results should |
|
| 2249 | - * be returned at most. If the limit is not specified, it should be treated |
|
| 2250 | - * as infinite. |
|
| 2251 | - * |
|
| 2252 | - * If the limit (infinite or not) is higher than you're willing to return, |
|
| 2253 | - * you should throw a Sabre\DAV\Exception\TooMuchMatches() exception. |
|
| 2254 | - * |
|
| 2255 | - * If the syncToken is expired (due to data cleanup) or unknown, you must |
|
| 2256 | - * return null. |
|
| 2257 | - * |
|
| 2258 | - * The limit is 'suggestive'. You are free to ignore it. |
|
| 2259 | - * |
|
| 2260 | - * @param string $calendarId |
|
| 2261 | - * @param string $syncToken |
|
| 2262 | - * @param int $syncLevel |
|
| 2263 | - * @param int|null $limit |
|
| 2264 | - * @param int $calendarType |
|
| 2265 | - * @return array |
|
| 2266 | - */ |
|
| 2267 | - public function getChangesForCalendar($calendarId, $syncToken, $syncLevel, $limit = null, $calendarType = self::CALENDAR_TYPE_CALENDAR) { |
|
| 2268 | - // Current synctoken |
|
| 2269 | - $qb = $this->db->getQueryBuilder(); |
|
| 2270 | - $qb->select('synctoken') |
|
| 2271 | - ->from('calendars') |
|
| 2272 | - ->where( |
|
| 2273 | - $qb->expr()->eq('id', $qb->createNamedParameter($calendarId)) |
|
| 2274 | - ); |
|
| 2275 | - $stmt = $qb->executeQuery(); |
|
| 2276 | - $currentToken = $stmt->fetchOne(); |
|
| 2277 | - |
|
| 2278 | - if ($currentToken === false) { |
|
| 2279 | - return null; |
|
| 2280 | - } |
|
| 2281 | - |
|
| 2282 | - $result = [ |
|
| 2283 | - 'syncToken' => $currentToken, |
|
| 2284 | - 'added' => [], |
|
| 2285 | - 'modified' => [], |
|
| 2286 | - 'deleted' => [], |
|
| 2287 | - ]; |
|
| 2288 | - |
|
| 2289 | - if ($syncToken) { |
|
| 2290 | - $qb = $this->db->getQueryBuilder(); |
|
| 2291 | - |
|
| 2292 | - $qb->select('uri', 'operation') |
|
| 2293 | - ->from('calendarchanges') |
|
| 2294 | - ->where( |
|
| 2295 | - $qb->expr()->andX( |
|
| 2296 | - $qb->expr()->gte('synctoken', $qb->createNamedParameter($syncToken)), |
|
| 2297 | - $qb->expr()->lt('synctoken', $qb->createNamedParameter($currentToken)), |
|
| 2298 | - $qb->expr()->eq('calendarid', $qb->createNamedParameter($calendarId)), |
|
| 2299 | - $qb->expr()->eq('calendartype', $qb->createNamedParameter($calendarType)) |
|
| 2300 | - ) |
|
| 2301 | - )->orderBy('synctoken'); |
|
| 2302 | - if (is_int($limit) && $limit > 0) { |
|
| 2303 | - $qb->setMaxResults($limit); |
|
| 2304 | - } |
|
| 2305 | - |
|
| 2306 | - // Fetching all changes |
|
| 2307 | - $stmt = $qb->executeQuery(); |
|
| 2308 | - $changes = []; |
|
| 2309 | - |
|
| 2310 | - // This loop ensures that any duplicates are overwritten, only the |
|
| 2311 | - // last change on a node is relevant. |
|
| 2312 | - while ($row = $stmt->fetch()) { |
|
| 2313 | - $changes[$row['uri']] = $row['operation']; |
|
| 2314 | - } |
|
| 2315 | - $stmt->closeCursor(); |
|
| 2316 | - |
|
| 2317 | - foreach ($changes as $uri => $operation) { |
|
| 2318 | - switch ($operation) { |
|
| 2319 | - case 1: |
|
| 2320 | - $result['added'][] = $uri; |
|
| 2321 | - break; |
|
| 2322 | - case 2: |
|
| 2323 | - $result['modified'][] = $uri; |
|
| 2324 | - break; |
|
| 2325 | - case 3: |
|
| 2326 | - $result['deleted'][] = $uri; |
|
| 2327 | - break; |
|
| 2328 | - } |
|
| 2329 | - } |
|
| 2330 | - } else { |
|
| 2331 | - // No synctoken supplied, this is the initial sync. |
|
| 2332 | - $qb = $this->db->getQueryBuilder(); |
|
| 2333 | - $qb->select('uri') |
|
| 2334 | - ->from('calendarobjects') |
|
| 2335 | - ->where( |
|
| 2336 | - $qb->expr()->andX( |
|
| 2337 | - $qb->expr()->eq('calendarid', $qb->createNamedParameter($calendarId)), |
|
| 2338 | - $qb->expr()->eq('calendartype', $qb->createNamedParameter($calendarType)) |
|
| 2339 | - ) |
|
| 2340 | - ); |
|
| 2341 | - $stmt = $qb->executeQuery(); |
|
| 2342 | - $result['added'] = $stmt->fetchAll(\PDO::FETCH_COLUMN); |
|
| 2343 | - $stmt->closeCursor(); |
|
| 2344 | - } |
|
| 2345 | - return $result; |
|
| 2346 | - } |
|
| 2347 | - |
|
| 2348 | - /** |
|
| 2349 | - * Returns a list of subscriptions for a principal. |
|
| 2350 | - * |
|
| 2351 | - * Every subscription is an array with the following keys: |
|
| 2352 | - * * id, a unique id that will be used by other functions to modify the |
|
| 2353 | - * subscription. This can be the same as the uri or a database key. |
|
| 2354 | - * * uri. This is just the 'base uri' or 'filename' of the subscription. |
|
| 2355 | - * * principaluri. The owner of the subscription. Almost always the same as |
|
| 2356 | - * principalUri passed to this method. |
|
| 2357 | - * |
|
| 2358 | - * Furthermore, all the subscription info must be returned too: |
|
| 2359 | - * |
|
| 2360 | - * 1. {DAV:}displayname |
|
| 2361 | - * 2. {http://apple.com/ns/ical/}refreshrate |
|
| 2362 | - * 3. {http://calendarserver.org/ns/}subscribed-strip-todos (omit if todos |
|
| 2363 | - * should not be stripped). |
|
| 2364 | - * 4. {http://calendarserver.org/ns/}subscribed-strip-alarms (omit if alarms |
|
| 2365 | - * should not be stripped). |
|
| 2366 | - * 5. {http://calendarserver.org/ns/}subscribed-strip-attachments (omit if |
|
| 2367 | - * attachments should not be stripped). |
|
| 2368 | - * 6. {http://calendarserver.org/ns/}source (Must be a |
|
| 2369 | - * Sabre\DAV\Property\Href). |
|
| 2370 | - * 7. {http://apple.com/ns/ical/}calendar-color |
|
| 2371 | - * 8. {http://apple.com/ns/ical/}calendar-order |
|
| 2372 | - * 9. {urn:ietf:params:xml:ns:caldav}supported-calendar-component-set |
|
| 2373 | - * (should just be an instance of |
|
| 2374 | - * Sabre\CalDAV\Property\SupportedCalendarComponentSet, with a bunch of |
|
| 2375 | - * default components). |
|
| 2376 | - * |
|
| 2377 | - * @param string $principalUri |
|
| 2378 | - * @return array |
|
| 2379 | - */ |
|
| 2380 | - public function getSubscriptionsForUser($principalUri) { |
|
| 2381 | - $fields = array_column($this->subscriptionPropertyMap, 0); |
|
| 2382 | - $fields[] = 'id'; |
|
| 2383 | - $fields[] = 'uri'; |
|
| 2384 | - $fields[] = 'source'; |
|
| 2385 | - $fields[] = 'principaluri'; |
|
| 2386 | - $fields[] = 'lastmodified'; |
|
| 2387 | - $fields[] = 'synctoken'; |
|
| 2388 | - |
|
| 2389 | - $query = $this->db->getQueryBuilder(); |
|
| 2390 | - $query->select($fields) |
|
| 2391 | - ->from('calendarsubscriptions') |
|
| 2392 | - ->where($query->expr()->eq('principaluri', $query->createNamedParameter($principalUri))) |
|
| 2393 | - ->orderBy('calendarorder', 'asc'); |
|
| 2394 | - $stmt = $query->executeQuery(); |
|
| 2395 | - |
|
| 2396 | - $subscriptions = []; |
|
| 2397 | - while ($row = $stmt->fetch()) { |
|
| 2398 | - $subscription = [ |
|
| 2399 | - 'id' => $row['id'], |
|
| 2400 | - 'uri' => $row['uri'], |
|
| 2401 | - 'principaluri' => $row['principaluri'], |
|
| 2402 | - 'source' => $row['source'], |
|
| 2403 | - 'lastmodified' => $row['lastmodified'], |
|
| 2404 | - |
|
| 2405 | - '{' . Plugin::NS_CALDAV . '}supported-calendar-component-set' => new SupportedCalendarComponentSet(['VTODO', 'VEVENT']), |
|
| 2406 | - '{http://sabredav.org/ns}sync-token' => $row['synctoken']?$row['synctoken']:'0', |
|
| 2407 | - ]; |
|
| 2408 | - |
|
| 2409 | - $subscriptions[] = $this->rowToSubscription($row, $subscription); |
|
| 2410 | - } |
|
| 2411 | - |
|
| 2412 | - return $subscriptions; |
|
| 2413 | - } |
|
| 2414 | - |
|
| 2415 | - /** |
|
| 2416 | - * Creates a new subscription for a principal. |
|
| 2417 | - * |
|
| 2418 | - * If the creation was a success, an id must be returned that can be used to reference |
|
| 2419 | - * this subscription in other methods, such as updateSubscription. |
|
| 2420 | - * |
|
| 2421 | - * @param string $principalUri |
|
| 2422 | - * @param string $uri |
|
| 2423 | - * @param array $properties |
|
| 2424 | - * @return mixed |
|
| 2425 | - */ |
|
| 2426 | - public function createSubscription($principalUri, $uri, array $properties) { |
|
| 2427 | - if (!isset($properties['{http://calendarserver.org/ns/}source'])) { |
|
| 2428 | - throw new Forbidden('The {http://calendarserver.org/ns/}source property is required when creating subscriptions'); |
|
| 2429 | - } |
|
| 2430 | - |
|
| 2431 | - $values = [ |
|
| 2432 | - 'principaluri' => $principalUri, |
|
| 2433 | - 'uri' => $uri, |
|
| 2434 | - 'source' => $properties['{http://calendarserver.org/ns/}source']->getHref(), |
|
| 2435 | - 'lastmodified' => time(), |
|
| 2436 | - ]; |
|
| 2437 | - |
|
| 2438 | - $propertiesBoolean = ['striptodos', 'stripalarms', 'stripattachments']; |
|
| 2439 | - |
|
| 2440 | - foreach ($this->subscriptionPropertyMap as $xmlName => [$dbName, $type]) { |
|
| 2441 | - if (array_key_exists($xmlName, $properties)) { |
|
| 2442 | - $values[$dbName] = $properties[$xmlName]; |
|
| 2443 | - if (in_array($dbName, $propertiesBoolean)) { |
|
| 2444 | - $values[$dbName] = true; |
|
| 2445 | - } |
|
| 2446 | - } |
|
| 2447 | - } |
|
| 2448 | - |
|
| 2449 | - $valuesToInsert = []; |
|
| 2450 | - |
|
| 2451 | - $query = $this->db->getQueryBuilder(); |
|
| 2452 | - |
|
| 2453 | - foreach (array_keys($values) as $name) { |
|
| 2454 | - $valuesToInsert[$name] = $query->createNamedParameter($values[$name]); |
|
| 2455 | - } |
|
| 2456 | - |
|
| 2457 | - $query->insert('calendarsubscriptions') |
|
| 2458 | - ->values($valuesToInsert) |
|
| 2459 | - ->executeStatement(); |
|
| 2460 | - |
|
| 2461 | - $subscriptionId = $query->getLastInsertId(); |
|
| 2462 | - |
|
| 2463 | - $subscriptionRow = $this->getSubscriptionById($subscriptionId); |
|
| 2464 | - $this->dispatcher->dispatchTyped(new SubscriptionCreatedEvent($subscriptionId, $subscriptionRow)); |
|
| 2465 | - |
|
| 2466 | - return $subscriptionId; |
|
| 2467 | - } |
|
| 2468 | - |
|
| 2469 | - /** |
|
| 2470 | - * Updates a subscription |
|
| 2471 | - * |
|
| 2472 | - * The list of mutations is stored in a Sabre\DAV\PropPatch object. |
|
| 2473 | - * To do the actual updates, you must tell this object which properties |
|
| 2474 | - * you're going to process with the handle() method. |
|
| 2475 | - * |
|
| 2476 | - * Calling the handle method is like telling the PropPatch object "I |
|
| 2477 | - * promise I can handle updating this property". |
|
| 2478 | - * |
|
| 2479 | - * Read the PropPatch documentation for more info and examples. |
|
| 2480 | - * |
|
| 2481 | - * @param mixed $subscriptionId |
|
| 2482 | - * @param PropPatch $propPatch |
|
| 2483 | - * @return void |
|
| 2484 | - */ |
|
| 2485 | - public function updateSubscription($subscriptionId, PropPatch $propPatch) { |
|
| 2486 | - $supportedProperties = array_keys($this->subscriptionPropertyMap); |
|
| 2487 | - $supportedProperties[] = '{http://calendarserver.org/ns/}source'; |
|
| 2488 | - |
|
| 2489 | - $propPatch->handle($supportedProperties, function ($mutations) use ($subscriptionId) { |
|
| 2490 | - $newValues = []; |
|
| 2491 | - |
|
| 2492 | - foreach ($mutations as $propertyName => $propertyValue) { |
|
| 2493 | - if ($propertyName === '{http://calendarserver.org/ns/}source') { |
|
| 2494 | - $newValues['source'] = $propertyValue->getHref(); |
|
| 2495 | - } else { |
|
| 2496 | - $fieldName = $this->subscriptionPropertyMap[$propertyName][0]; |
|
| 2497 | - $newValues[$fieldName] = $propertyValue; |
|
| 2498 | - } |
|
| 2499 | - } |
|
| 2500 | - |
|
| 2501 | - $query = $this->db->getQueryBuilder(); |
|
| 2502 | - $query->update('calendarsubscriptions') |
|
| 2503 | - ->set('lastmodified', $query->createNamedParameter(time())); |
|
| 2504 | - foreach ($newValues as $fieldName => $value) { |
|
| 2505 | - $query->set($fieldName, $query->createNamedParameter($value)); |
|
| 2506 | - } |
|
| 2507 | - $query->where($query->expr()->eq('id', $query->createNamedParameter($subscriptionId))) |
|
| 2508 | - ->executeStatement(); |
|
| 2509 | - |
|
| 2510 | - $subscriptionRow = $this->getSubscriptionById($subscriptionId); |
|
| 2511 | - $this->dispatcher->dispatchTyped(new SubscriptionUpdatedEvent((int)$subscriptionId, $subscriptionRow, [], $mutations)); |
|
| 2512 | - |
|
| 2513 | - return true; |
|
| 2514 | - }); |
|
| 2515 | - } |
|
| 2516 | - |
|
| 2517 | - /** |
|
| 2518 | - * Deletes a subscription. |
|
| 2519 | - * |
|
| 2520 | - * @param mixed $subscriptionId |
|
| 2521 | - * @return void |
|
| 2522 | - */ |
|
| 2523 | - public function deleteSubscription($subscriptionId) { |
|
| 2524 | - $subscriptionRow = $this->getSubscriptionById($subscriptionId); |
|
| 2525 | - |
|
| 2526 | - $query = $this->db->getQueryBuilder(); |
|
| 2527 | - $query->delete('calendarsubscriptions') |
|
| 2528 | - ->where($query->expr()->eq('id', $query->createNamedParameter($subscriptionId))) |
|
| 2529 | - ->executeStatement(); |
|
| 2530 | - |
|
| 2531 | - $query = $this->db->getQueryBuilder(); |
|
| 2532 | - $query->delete('calendarobjects') |
|
| 2533 | - ->where($query->expr()->eq('calendarid', $query->createNamedParameter($subscriptionId))) |
|
| 2534 | - ->andWhere($query->expr()->eq('calendartype', $query->createNamedParameter(self::CALENDAR_TYPE_SUBSCRIPTION))) |
|
| 2535 | - ->executeStatement(); |
|
| 2536 | - |
|
| 2537 | - $query->delete('calendarchanges') |
|
| 2538 | - ->where($query->expr()->eq('calendarid', $query->createNamedParameter($subscriptionId))) |
|
| 2539 | - ->andWhere($query->expr()->eq('calendartype', $query->createNamedParameter(self::CALENDAR_TYPE_SUBSCRIPTION))) |
|
| 2540 | - ->executeStatement(); |
|
| 2541 | - |
|
| 2542 | - $query->delete($this->dbObjectPropertiesTable) |
|
| 2543 | - ->where($query->expr()->eq('calendarid', $query->createNamedParameter($subscriptionId))) |
|
| 2544 | - ->andWhere($query->expr()->eq('calendartype', $query->createNamedParameter(self::CALENDAR_TYPE_SUBSCRIPTION))) |
|
| 2545 | - ->executeStatement(); |
|
| 2546 | - |
|
| 2547 | - if ($subscriptionRow) { |
|
| 2548 | - $this->dispatcher->dispatchTyped(new SubscriptionDeletedEvent((int)$subscriptionId, $subscriptionRow, [])); |
|
| 2549 | - } |
|
| 2550 | - } |
|
| 2551 | - |
|
| 2552 | - /** |
|
| 2553 | - * Returns a single scheduling object for the inbox collection. |
|
| 2554 | - * |
|
| 2555 | - * The returned array should contain the following elements: |
|
| 2556 | - * * uri - A unique basename for the object. This will be used to |
|
| 2557 | - * construct a full uri. |
|
| 2558 | - * * calendardata - The iCalendar object |
|
| 2559 | - * * lastmodified - The last modification date. Can be an int for a unix |
|
| 2560 | - * timestamp, or a PHP DateTime object. |
|
| 2561 | - * * etag - A unique token that must change if the object changed. |
|
| 2562 | - * * size - The size of the object, in bytes. |
|
| 2563 | - * |
|
| 2564 | - * @param string $principalUri |
|
| 2565 | - * @param string $objectUri |
|
| 2566 | - * @return array |
|
| 2567 | - */ |
|
| 2568 | - public function getSchedulingObject($principalUri, $objectUri) { |
|
| 2569 | - $query = $this->db->getQueryBuilder(); |
|
| 2570 | - $stmt = $query->select(['uri', 'calendardata', 'lastmodified', 'etag', 'size']) |
|
| 2571 | - ->from('schedulingobjects') |
|
| 2572 | - ->where($query->expr()->eq('principaluri', $query->createNamedParameter($principalUri))) |
|
| 2573 | - ->andWhere($query->expr()->eq('uri', $query->createNamedParameter($objectUri))) |
|
| 2574 | - ->executeQuery(); |
|
| 2575 | - |
|
| 2576 | - $row = $stmt->fetch(); |
|
| 2577 | - |
|
| 2578 | - if (!$row) { |
|
| 2579 | - return null; |
|
| 2580 | - } |
|
| 2581 | - |
|
| 2582 | - return [ |
|
| 2583 | - 'uri' => $row['uri'], |
|
| 2584 | - 'calendardata' => $row['calendardata'], |
|
| 2585 | - 'lastmodified' => $row['lastmodified'], |
|
| 2586 | - 'etag' => '"' . $row['etag'] . '"', |
|
| 2587 | - 'size' => (int)$row['size'], |
|
| 2588 | - ]; |
|
| 2589 | - } |
|
| 2590 | - |
|
| 2591 | - /** |
|
| 2592 | - * Returns all scheduling objects for the inbox collection. |
|
| 2593 | - * |
|
| 2594 | - * These objects should be returned as an array. Every item in the array |
|
| 2595 | - * should follow the same structure as returned from getSchedulingObject. |
|
| 2596 | - * |
|
| 2597 | - * The main difference is that 'calendardata' is optional. |
|
| 2598 | - * |
|
| 2599 | - * @param string $principalUri |
|
| 2600 | - * @return array |
|
| 2601 | - */ |
|
| 2602 | - public function getSchedulingObjects($principalUri) { |
|
| 2603 | - $query = $this->db->getQueryBuilder(); |
|
| 2604 | - $stmt = $query->select(['uri', 'calendardata', 'lastmodified', 'etag', 'size']) |
|
| 2605 | - ->from('schedulingobjects') |
|
| 2606 | - ->where($query->expr()->eq('principaluri', $query->createNamedParameter($principalUri))) |
|
| 2607 | - ->executeQuery(); |
|
| 2608 | - |
|
| 2609 | - $result = []; |
|
| 2610 | - foreach ($stmt->fetchAll() as $row) { |
|
| 2611 | - $result[] = [ |
|
| 2612 | - 'calendardata' => $row['calendardata'], |
|
| 2613 | - 'uri' => $row['uri'], |
|
| 2614 | - 'lastmodified' => $row['lastmodified'], |
|
| 2615 | - 'etag' => '"' . $row['etag'] . '"', |
|
| 2616 | - 'size' => (int)$row['size'], |
|
| 2617 | - ]; |
|
| 2618 | - } |
|
| 2619 | - $stmt->closeCursor(); |
|
| 2620 | - |
|
| 2621 | - return $result; |
|
| 2622 | - } |
|
| 2623 | - |
|
| 2624 | - /** |
|
| 2625 | - * Deletes a scheduling object from the inbox collection. |
|
| 2626 | - * |
|
| 2627 | - * @param string $principalUri |
|
| 2628 | - * @param string $objectUri |
|
| 2629 | - * @return void |
|
| 2630 | - */ |
|
| 2631 | - public function deleteSchedulingObject($principalUri, $objectUri) { |
|
| 2632 | - $query = $this->db->getQueryBuilder(); |
|
| 2633 | - $query->delete('schedulingobjects') |
|
| 2634 | - ->where($query->expr()->eq('principaluri', $query->createNamedParameter($principalUri))) |
|
| 2635 | - ->andWhere($query->expr()->eq('uri', $query->createNamedParameter($objectUri))) |
|
| 2636 | - ->executeStatement(); |
|
| 2637 | - } |
|
| 2638 | - |
|
| 2639 | - /** |
|
| 2640 | - * Creates a new scheduling object. This should land in a users' inbox. |
|
| 2641 | - * |
|
| 2642 | - * @param string $principalUri |
|
| 2643 | - * @param string $objectUri |
|
| 2644 | - * @param string $objectData |
|
| 2645 | - * @return void |
|
| 2646 | - */ |
|
| 2647 | - public function createSchedulingObject($principalUri, $objectUri, $objectData) { |
|
| 2648 | - $query = $this->db->getQueryBuilder(); |
|
| 2649 | - $query->insert('schedulingobjects') |
|
| 2650 | - ->values([ |
|
| 2651 | - 'principaluri' => $query->createNamedParameter($principalUri), |
|
| 2652 | - 'calendardata' => $query->createNamedParameter($objectData, IQueryBuilder::PARAM_LOB), |
|
| 2653 | - 'uri' => $query->createNamedParameter($objectUri), |
|
| 2654 | - 'lastmodified' => $query->createNamedParameter(time()), |
|
| 2655 | - 'etag' => $query->createNamedParameter(md5($objectData)), |
|
| 2656 | - 'size' => $query->createNamedParameter(strlen($objectData)) |
|
| 2657 | - ]) |
|
| 2658 | - ->executeStatement(); |
|
| 2659 | - } |
|
| 2660 | - |
|
| 2661 | - /** |
|
| 2662 | - * Adds a change record to the calendarchanges table. |
|
| 2663 | - * |
|
| 2664 | - * @param mixed $calendarId |
|
| 2665 | - * @param string $objectUri |
|
| 2666 | - * @param int $operation 1 = add, 2 = modify, 3 = delete. |
|
| 2667 | - * @param int $calendarType |
|
| 2668 | - * @return void |
|
| 2669 | - */ |
|
| 2670 | - protected function addChange($calendarId, $objectUri, $operation, $calendarType = self::CALENDAR_TYPE_CALENDAR) { |
|
| 2671 | - $table = $calendarType === self::CALENDAR_TYPE_CALENDAR ? 'calendars': 'calendarsubscriptions'; |
|
| 2672 | - |
|
| 2673 | - $query = $this->db->getQueryBuilder(); |
|
| 2674 | - $query->select('synctoken') |
|
| 2675 | - ->from($table) |
|
| 2676 | - ->where($query->expr()->eq('id', $query->createNamedParameter($calendarId))); |
|
| 2677 | - $result = $query->executeQuery(); |
|
| 2678 | - $syncToken = (int)$result->fetchOne(); |
|
| 2679 | - $result->closeCursor(); |
|
| 2680 | - |
|
| 2681 | - $query = $this->db->getQueryBuilder(); |
|
| 2682 | - $query->insert('calendarchanges') |
|
| 2683 | - ->values([ |
|
| 2684 | - 'uri' => $query->createNamedParameter($objectUri), |
|
| 2685 | - 'synctoken' => $query->createNamedParameter($syncToken), |
|
| 2686 | - 'calendarid' => $query->createNamedParameter($calendarId), |
|
| 2687 | - 'operation' => $query->createNamedParameter($operation), |
|
| 2688 | - 'calendartype' => $query->createNamedParameter($calendarType), |
|
| 2689 | - ]) |
|
| 2690 | - ->executeStatement(); |
|
| 2691 | - |
|
| 2692 | - $stmt = $this->db->prepare("UPDATE `*PREFIX*$table` SET `synctoken` = `synctoken` + 1 WHERE `id` = ?"); |
|
| 2693 | - $stmt->execute([ |
|
| 2694 | - $calendarId |
|
| 2695 | - ]); |
|
| 2696 | - } |
|
| 2697 | - |
|
| 2698 | - /** |
|
| 2699 | - * Parses some information from calendar objects, used for optimized |
|
| 2700 | - * calendar-queries. |
|
| 2701 | - * |
|
| 2702 | - * Returns an array with the following keys: |
|
| 2703 | - * * etag - An md5 checksum of the object without the quotes. |
|
| 2704 | - * * size - Size of the object in bytes |
|
| 2705 | - * * componentType - VEVENT, VTODO or VJOURNAL |
|
| 2706 | - * * firstOccurence |
|
| 2707 | - * * lastOccurence |
|
| 2708 | - * * uid - value of the UID property |
|
| 2709 | - * |
|
| 2710 | - * @param string $calendarData |
|
| 2711 | - * @return array |
|
| 2712 | - */ |
|
| 2713 | - public function getDenormalizedData($calendarData) { |
|
| 2714 | - $vObject = Reader::read($calendarData); |
|
| 2715 | - $vEvents = []; |
|
| 2716 | - $componentType = null; |
|
| 2717 | - $component = null; |
|
| 2718 | - $firstOccurrence = null; |
|
| 2719 | - $lastOccurrence = null; |
|
| 2720 | - $uid = null; |
|
| 2721 | - $classification = self::CLASSIFICATION_PUBLIC; |
|
| 2722 | - $hasDTSTART = false; |
|
| 2723 | - foreach ($vObject->getComponents() as $component) { |
|
| 2724 | - if ($component->name !== 'VTIMEZONE') { |
|
| 2725 | - // Finding all VEVENTs, and track them |
|
| 2726 | - if ($component->name === 'VEVENT') { |
|
| 2727 | - array_push($vEvents, $component); |
|
| 2728 | - if ($component->DTSTART) { |
|
| 2729 | - $hasDTSTART = true; |
|
| 2730 | - } |
|
| 2731 | - } |
|
| 2732 | - // Track first component type and uid |
|
| 2733 | - if ($uid === null) { |
|
| 2734 | - $componentType = $component->name; |
|
| 2735 | - $uid = (string)$component->UID; |
|
| 2736 | - } |
|
| 2737 | - } |
|
| 2738 | - } |
|
| 2739 | - if (!$componentType) { |
|
| 2740 | - throw new BadRequest('Calendar objects must have a VJOURNAL, VEVENT or VTODO component'); |
|
| 2741 | - } |
|
| 2742 | - |
|
| 2743 | - if ($hasDTSTART) { |
|
| 2744 | - $component = $vEvents[0]; |
|
| 2745 | - |
|
| 2746 | - // Finding the last occurrence is a bit harder |
|
| 2747 | - if (!isset($component->RRULE) && count($vEvents) === 1) { |
|
| 2748 | - $firstOccurrence = $component->DTSTART->getDateTime()->getTimeStamp(); |
|
| 2749 | - if (isset($component->DTEND)) { |
|
| 2750 | - $lastOccurrence = $component->DTEND->getDateTime()->getTimeStamp(); |
|
| 2751 | - } elseif (isset($component->DURATION)) { |
|
| 2752 | - $endDate = clone $component->DTSTART->getDateTime(); |
|
| 2753 | - $endDate->add(DateTimeParser::parse($component->DURATION->getValue())); |
|
| 2754 | - $lastOccurrence = $endDate->getTimeStamp(); |
|
| 2755 | - } elseif (!$component->DTSTART->hasTime()) { |
|
| 2756 | - $endDate = clone $component->DTSTART->getDateTime(); |
|
| 2757 | - $endDate->modify('+1 day'); |
|
| 2758 | - $lastOccurrence = $endDate->getTimeStamp(); |
|
| 2759 | - } else { |
|
| 2760 | - $lastOccurrence = $firstOccurrence; |
|
| 2761 | - } |
|
| 2762 | - } else { |
|
| 2763 | - $it = new EventIterator($vEvents); |
|
| 2764 | - $maxDate = new DateTime(self::MAX_DATE); |
|
| 2765 | - $firstOccurrence = $it->getDtStart()->getTimestamp(); |
|
| 2766 | - if ($it->isInfinite()) { |
|
| 2767 | - $lastOccurrence = $maxDate->getTimestamp(); |
|
| 2768 | - } else { |
|
| 2769 | - $end = $it->getDtEnd(); |
|
| 2770 | - while ($it->valid() && $end < $maxDate) { |
|
| 2771 | - $end = $it->getDtEnd(); |
|
| 2772 | - $it->next(); |
|
| 2773 | - } |
|
| 2774 | - $lastOccurrence = $end->getTimestamp(); |
|
| 2775 | - } |
|
| 2776 | - } |
|
| 2777 | - } |
|
| 2778 | - |
|
| 2779 | - if ($component->CLASS) { |
|
| 2780 | - $classification = CalDavBackend::CLASSIFICATION_PRIVATE; |
|
| 2781 | - switch ($component->CLASS->getValue()) { |
|
| 2782 | - case 'PUBLIC': |
|
| 2783 | - $classification = CalDavBackend::CLASSIFICATION_PUBLIC; |
|
| 2784 | - break; |
|
| 2785 | - case 'CONFIDENTIAL': |
|
| 2786 | - $classification = CalDavBackend::CLASSIFICATION_CONFIDENTIAL; |
|
| 2787 | - break; |
|
| 2788 | - } |
|
| 2789 | - } |
|
| 2790 | - return [ |
|
| 2791 | - 'etag' => md5($calendarData), |
|
| 2792 | - 'size' => strlen($calendarData), |
|
| 2793 | - 'componentType' => $componentType, |
|
| 2794 | - 'firstOccurence' => is_null($firstOccurrence) ? null : max(0, $firstOccurrence), |
|
| 2795 | - 'lastOccurence' => $lastOccurrence, |
|
| 2796 | - 'uid' => $uid, |
|
| 2797 | - 'classification' => $classification |
|
| 2798 | - ]; |
|
| 2799 | - } |
|
| 2800 | - |
|
| 2801 | - /** |
|
| 2802 | - * @param $cardData |
|
| 2803 | - * @return bool|string |
|
| 2804 | - */ |
|
| 2805 | - private function readBlob($cardData) { |
|
| 2806 | - if (is_resource($cardData)) { |
|
| 2807 | - return stream_get_contents($cardData); |
|
| 2808 | - } |
|
| 2809 | - |
|
| 2810 | - return $cardData; |
|
| 2811 | - } |
|
| 2812 | - |
|
| 2813 | - /** |
|
| 2814 | - * @param list<array{href: string, commonName: string, readOnly: bool}> $add |
|
| 2815 | - * @param list<string> $remove |
|
| 2816 | - */ |
|
| 2817 | - public function updateShares(IShareable $shareable, array $add, array $remove): void { |
|
| 2818 | - $calendarId = $shareable->getResourceId(); |
|
| 2819 | - $calendarRow = $this->getCalendarById($calendarId); |
|
| 2820 | - if ($calendarRow === null) { |
|
| 2821 | - throw new \RuntimeException('Trying to update shares for innexistant calendar: ' . $calendarId); |
|
| 2822 | - } |
|
| 2823 | - $oldShares = $this->getShares($calendarId); |
|
| 2824 | - |
|
| 2825 | - $this->calendarSharingBackend->updateShares($shareable, $add, $remove); |
|
| 2826 | - |
|
| 2827 | - $this->dispatcher->dispatchTyped(new CalendarShareUpdatedEvent($calendarId, $calendarRow, $oldShares, $add, $remove)); |
|
| 2828 | - } |
|
| 2829 | - |
|
| 2830 | - /** |
|
| 2831 | - * @return list<array{href: string, commonName: string, status: int, readOnly: bool, '{http://owncloud.org/ns}principal': string, '{http://owncloud.org/ns}group-share': bool}> |
|
| 2832 | - */ |
|
| 2833 | - public function getShares(int $resourceId): array { |
|
| 2834 | - return $this->calendarSharingBackend->getShares($resourceId); |
|
| 2835 | - } |
|
| 2836 | - |
|
| 2837 | - /** |
|
| 2838 | - * @param boolean $value |
|
| 2839 | - * @param \OCA\DAV\CalDAV\Calendar $calendar |
|
| 2840 | - * @return string|null |
|
| 2841 | - */ |
|
| 2842 | - public function setPublishStatus($value, $calendar) { |
|
| 2843 | - $calendarId = $calendar->getResourceId(); |
|
| 2844 | - $calendarData = $this->getCalendarById($calendarId); |
|
| 2845 | - |
|
| 2846 | - $query = $this->db->getQueryBuilder(); |
|
| 2847 | - if ($value) { |
|
| 2848 | - $publicUri = $this->random->generate(16, ISecureRandom::CHAR_HUMAN_READABLE); |
|
| 2849 | - $query->insert('dav_shares') |
|
| 2850 | - ->values([ |
|
| 2851 | - 'principaluri' => $query->createNamedParameter($calendar->getPrincipalURI()), |
|
| 2852 | - 'type' => $query->createNamedParameter('calendar'), |
|
| 2853 | - 'access' => $query->createNamedParameter(self::ACCESS_PUBLIC), |
|
| 2854 | - 'resourceid' => $query->createNamedParameter($calendar->getResourceId()), |
|
| 2855 | - 'publicuri' => $query->createNamedParameter($publicUri) |
|
| 2856 | - ]); |
|
| 2857 | - $query->executeStatement(); |
|
| 2858 | - |
|
| 2859 | - $this->dispatcher->dispatchTyped(new CalendarPublishedEvent($calendarId, $calendarData, $publicUri)); |
|
| 2860 | - return $publicUri; |
|
| 2861 | - } |
|
| 2862 | - $query->delete('dav_shares') |
|
| 2863 | - ->where($query->expr()->eq('resourceid', $query->createNamedParameter($calendar->getResourceId()))) |
|
| 2864 | - ->andWhere($query->expr()->eq('access', $query->createNamedParameter(self::ACCESS_PUBLIC))); |
|
| 2865 | - $query->executeStatement(); |
|
| 2866 | - |
|
| 2867 | - $this->dispatcher->dispatchTyped(new CalendarUnpublishedEvent($calendarId, $calendarData)); |
|
| 2868 | - return null; |
|
| 2869 | - } |
|
| 2870 | - |
|
| 2871 | - /** |
|
| 2872 | - * @param \OCA\DAV\CalDAV\Calendar $calendar |
|
| 2873 | - * @return mixed |
|
| 2874 | - */ |
|
| 2875 | - public function getPublishStatus($calendar) { |
|
| 2876 | - $query = $this->db->getQueryBuilder(); |
|
| 2877 | - $result = $query->select('publicuri') |
|
| 2878 | - ->from('dav_shares') |
|
| 2879 | - ->where($query->expr()->eq('resourceid', $query->createNamedParameter($calendar->getResourceId()))) |
|
| 2880 | - ->andWhere($query->expr()->eq('access', $query->createNamedParameter(self::ACCESS_PUBLIC))) |
|
| 2881 | - ->executeQuery(); |
|
| 2882 | - |
|
| 2883 | - $row = $result->fetch(); |
|
| 2884 | - $result->closeCursor(); |
|
| 2885 | - return $row ? reset($row) : false; |
|
| 2886 | - } |
|
| 2887 | - |
|
| 2888 | - /** |
|
| 2889 | - * @param int $resourceId |
|
| 2890 | - * @param list<array{privilege: string, principal: string, protected: bool}> $acl |
|
| 2891 | - * @return list<array{privilege: string, principal: string, protected: bool}> |
|
| 2892 | - */ |
|
| 2893 | - public function applyShareAcl(int $resourceId, array $acl): array { |
|
| 2894 | - return $this->calendarSharingBackend->applyShareAcl($resourceId, $acl); |
|
| 2895 | - } |
|
| 2896 | - |
|
| 2897 | - /** |
|
| 2898 | - * update properties table |
|
| 2899 | - * |
|
| 2900 | - * @param int $calendarId |
|
| 2901 | - * @param string $objectUri |
|
| 2902 | - * @param string $calendarData |
|
| 2903 | - * @param int $calendarType |
|
| 2904 | - */ |
|
| 2905 | - public function updateProperties($calendarId, $objectUri, $calendarData, $calendarType = self::CALENDAR_TYPE_CALENDAR) { |
|
| 2906 | - $objectId = $this->getCalendarObjectId($calendarId, $objectUri, $calendarType); |
|
| 2907 | - |
|
| 2908 | - try { |
|
| 2909 | - $vCalendar = $this->readCalendarData($calendarData); |
|
| 2910 | - } catch (\Exception $ex) { |
|
| 2911 | - return; |
|
| 2912 | - } |
|
| 2913 | - |
|
| 2914 | - $this->purgeProperties($calendarId, $objectId); |
|
| 2915 | - |
|
| 2916 | - $query = $this->db->getQueryBuilder(); |
|
| 2917 | - $query->insert($this->dbObjectPropertiesTable) |
|
| 2918 | - ->values( |
|
| 2919 | - [ |
|
| 2920 | - 'calendarid' => $query->createNamedParameter($calendarId), |
|
| 2921 | - 'calendartype' => $query->createNamedParameter($calendarType), |
|
| 2922 | - 'objectid' => $query->createNamedParameter($objectId), |
|
| 2923 | - 'name' => $query->createParameter('name'), |
|
| 2924 | - 'parameter' => $query->createParameter('parameter'), |
|
| 2925 | - 'value' => $query->createParameter('value'), |
|
| 2926 | - ] |
|
| 2927 | - ); |
|
| 2928 | - |
|
| 2929 | - $indexComponents = ['VEVENT', 'VJOURNAL', 'VTODO']; |
|
| 2930 | - foreach ($vCalendar->getComponents() as $component) { |
|
| 2931 | - if (!in_array($component->name, $indexComponents)) { |
|
| 2932 | - continue; |
|
| 2933 | - } |
|
| 2934 | - |
|
| 2935 | - foreach ($component->children() as $property) { |
|
| 2936 | - if (in_array($property->name, self::INDEXED_PROPERTIES, true)) { |
|
| 2937 | - $value = $property->getValue(); |
|
| 2938 | - // is this a shitty db? |
|
| 2939 | - if (!$this->db->supports4ByteText()) { |
|
| 2940 | - $value = preg_replace('/[\x{10000}-\x{10FFFF}]/u', "\xEF\xBF\xBD", $value); |
|
| 2941 | - } |
|
| 2942 | - $value = mb_strcut($value, 0, 254); |
|
| 2943 | - |
|
| 2944 | - $query->setParameter('name', $property->name); |
|
| 2945 | - $query->setParameter('parameter', null); |
|
| 2946 | - $query->setParameter('value', $value); |
|
| 2947 | - $query->executeStatement(); |
|
| 2948 | - } |
|
| 2949 | - |
|
| 2950 | - if (array_key_exists($property->name, self::$indexParameters)) { |
|
| 2951 | - $parameters = $property->parameters(); |
|
| 2952 | - $indexedParametersForProperty = self::$indexParameters[$property->name]; |
|
| 2953 | - |
|
| 2954 | - foreach ($parameters as $key => $value) { |
|
| 2955 | - if (in_array($key, $indexedParametersForProperty)) { |
|
| 2956 | - // is this a shitty db? |
|
| 2957 | - if ($this->db->supports4ByteText()) { |
|
| 2958 | - $value = preg_replace('/[\x{10000}-\x{10FFFF}]/u', "\xEF\xBF\xBD", $value); |
|
| 2959 | - } |
|
| 2960 | - |
|
| 2961 | - $query->setParameter('name', $property->name); |
|
| 2962 | - $query->setParameter('parameter', mb_strcut($key, 0, 254)); |
|
| 2963 | - $query->setParameter('value', mb_strcut($value, 0, 254)); |
|
| 2964 | - $query->executeStatement(); |
|
| 2965 | - } |
|
| 2966 | - } |
|
| 2967 | - } |
|
| 2968 | - } |
|
| 2969 | - } |
|
| 2970 | - } |
|
| 2971 | - |
|
| 2972 | - /** |
|
| 2973 | - * deletes all birthday calendars |
|
| 2974 | - */ |
|
| 2975 | - public function deleteAllBirthdayCalendars() { |
|
| 2976 | - $query = $this->db->getQueryBuilder(); |
|
| 2977 | - $result = $query->select(['id'])->from('calendars') |
|
| 2978 | - ->where($query->expr()->eq('uri', $query->createNamedParameter(BirthdayService::BIRTHDAY_CALENDAR_URI))) |
|
| 2979 | - ->executeQuery(); |
|
| 2980 | - |
|
| 2981 | - $ids = $result->fetchAll(); |
|
| 2982 | - $result->closeCursor(); |
|
| 2983 | - foreach ($ids as $id) { |
|
| 2984 | - $this->deleteCalendar( |
|
| 2985 | - $id['id'], |
|
| 2986 | - true // No data to keep in the trashbin, if the user re-enables then we regenerate |
|
| 2987 | - ); |
|
| 2988 | - } |
|
| 2989 | - } |
|
| 2990 | - |
|
| 2991 | - /** |
|
| 2992 | - * @param $subscriptionId |
|
| 2993 | - */ |
|
| 2994 | - public function purgeAllCachedEventsForSubscription($subscriptionId) { |
|
| 2995 | - $query = $this->db->getQueryBuilder(); |
|
| 2996 | - $query->select('uri') |
|
| 2997 | - ->from('calendarobjects') |
|
| 2998 | - ->where($query->expr()->eq('calendarid', $query->createNamedParameter($subscriptionId))) |
|
| 2999 | - ->andWhere($query->expr()->eq('calendartype', $query->createNamedParameter(self::CALENDAR_TYPE_SUBSCRIPTION))); |
|
| 3000 | - $stmt = $query->executeQuery(); |
|
| 3001 | - |
|
| 3002 | - $uris = []; |
|
| 3003 | - foreach ($stmt->fetchAll() as $row) { |
|
| 3004 | - $uris[] = $row['uri']; |
|
| 3005 | - } |
|
| 3006 | - $stmt->closeCursor(); |
|
| 3007 | - |
|
| 3008 | - $query = $this->db->getQueryBuilder(); |
|
| 3009 | - $query->delete('calendarobjects') |
|
| 3010 | - ->where($query->expr()->eq('calendarid', $query->createNamedParameter($subscriptionId))) |
|
| 3011 | - ->andWhere($query->expr()->eq('calendartype', $query->createNamedParameter(self::CALENDAR_TYPE_SUBSCRIPTION))) |
|
| 3012 | - ->executeStatement(); |
|
| 3013 | - |
|
| 3014 | - $query->delete('calendarchanges') |
|
| 3015 | - ->where($query->expr()->eq('calendarid', $query->createNamedParameter($subscriptionId))) |
|
| 3016 | - ->andWhere($query->expr()->eq('calendartype', $query->createNamedParameter(self::CALENDAR_TYPE_SUBSCRIPTION))) |
|
| 3017 | - ->executeStatement(); |
|
| 3018 | - |
|
| 3019 | - $query->delete($this->dbObjectPropertiesTable) |
|
| 3020 | - ->where($query->expr()->eq('calendarid', $query->createNamedParameter($subscriptionId))) |
|
| 3021 | - ->andWhere($query->expr()->eq('calendartype', $query->createNamedParameter(self::CALENDAR_TYPE_SUBSCRIPTION))) |
|
| 3022 | - ->executeStatement(); |
|
| 3023 | - |
|
| 3024 | - foreach ($uris as $uri) { |
|
| 3025 | - $this->addChange($subscriptionId, $uri, 3, self::CALENDAR_TYPE_SUBSCRIPTION); |
|
| 3026 | - } |
|
| 3027 | - } |
|
| 3028 | - |
|
| 3029 | - /** |
|
| 3030 | - * Move a calendar from one user to another |
|
| 3031 | - * |
|
| 3032 | - * @param string $uriName |
|
| 3033 | - * @param string $uriOrigin |
|
| 3034 | - * @param string $uriDestination |
|
| 3035 | - * @param string $newUriName (optional) the new uriName |
|
| 3036 | - */ |
|
| 3037 | - public function moveCalendar($uriName, $uriOrigin, $uriDestination, $newUriName = null) { |
|
| 3038 | - $query = $this->db->getQueryBuilder(); |
|
| 3039 | - $query->update('calendars') |
|
| 3040 | - ->set('principaluri', $query->createNamedParameter($uriDestination)) |
|
| 3041 | - ->set('uri', $query->createNamedParameter($newUriName ?: $uriName)) |
|
| 3042 | - ->where($query->expr()->eq('principaluri', $query->createNamedParameter($uriOrigin))) |
|
| 3043 | - ->andWhere($query->expr()->eq('uri', $query->createNamedParameter($uriName))) |
|
| 3044 | - ->executeStatement(); |
|
| 3045 | - } |
|
| 3046 | - |
|
| 3047 | - /** |
|
| 3048 | - * read VCalendar data into a VCalendar object |
|
| 3049 | - * |
|
| 3050 | - * @param string $objectData |
|
| 3051 | - * @return VCalendar |
|
| 3052 | - */ |
|
| 3053 | - protected function readCalendarData($objectData) { |
|
| 3054 | - return Reader::read($objectData); |
|
| 3055 | - } |
|
| 3056 | - |
|
| 3057 | - /** |
|
| 3058 | - * delete all properties from a given calendar object |
|
| 3059 | - * |
|
| 3060 | - * @param int $calendarId |
|
| 3061 | - * @param int $objectId |
|
| 3062 | - */ |
|
| 3063 | - protected function purgeProperties($calendarId, $objectId) { |
|
| 3064 | - $query = $this->db->getQueryBuilder(); |
|
| 3065 | - $query->delete($this->dbObjectPropertiesTable) |
|
| 3066 | - ->where($query->expr()->eq('objectid', $query->createNamedParameter($objectId))) |
|
| 3067 | - ->andWhere($query->expr()->eq('calendarid', $query->createNamedParameter($calendarId))); |
|
| 3068 | - $query->executeStatement(); |
|
| 3069 | - } |
|
| 3070 | - |
|
| 3071 | - /** |
|
| 3072 | - * get ID from a given calendar object |
|
| 3073 | - * |
|
| 3074 | - * @param int $calendarId |
|
| 3075 | - * @param string $uri |
|
| 3076 | - * @param int $calendarType |
|
| 3077 | - * @return int |
|
| 3078 | - */ |
|
| 3079 | - protected function getCalendarObjectId($calendarId, $uri, $calendarType):int { |
|
| 3080 | - $query = $this->db->getQueryBuilder(); |
|
| 3081 | - $query->select('id') |
|
| 3082 | - ->from('calendarobjects') |
|
| 3083 | - ->where($query->expr()->eq('uri', $query->createNamedParameter($uri))) |
|
| 3084 | - ->andWhere($query->expr()->eq('calendarid', $query->createNamedParameter($calendarId))) |
|
| 3085 | - ->andWhere($query->expr()->eq('calendartype', $query->createNamedParameter($calendarType))); |
|
| 3086 | - |
|
| 3087 | - $result = $query->executeQuery(); |
|
| 3088 | - $objectIds = $result->fetch(); |
|
| 3089 | - $result->closeCursor(); |
|
| 3090 | - |
|
| 3091 | - if (!isset($objectIds['id'])) { |
|
| 3092 | - throw new \InvalidArgumentException('Calendarobject does not exists: ' . $uri); |
|
| 3093 | - } |
|
| 3094 | - |
|
| 3095 | - return (int)$objectIds['id']; |
|
| 3096 | - } |
|
| 3097 | - |
|
| 3098 | - /** |
|
| 3099 | - * return legacy endpoint principal name to new principal name |
|
| 3100 | - * |
|
| 3101 | - * @param $principalUri |
|
| 3102 | - * @param $toV2 |
|
| 3103 | - * @return string |
|
| 3104 | - */ |
|
| 3105 | - private function convertPrincipal($principalUri, $toV2) { |
|
| 3106 | - if ($this->principalBackend->getPrincipalPrefix() === 'principals') { |
|
| 3107 | - [, $name] = Uri\split($principalUri); |
|
| 3108 | - if ($toV2 === true) { |
|
| 3109 | - return "principals/users/$name"; |
|
| 3110 | - } |
|
| 3111 | - return "principals/$name"; |
|
| 3112 | - } |
|
| 3113 | - return $principalUri; |
|
| 3114 | - } |
|
| 3115 | - |
|
| 3116 | - /** |
|
| 3117 | - * adds information about an owner to the calendar data |
|
| 3118 | - * |
|
| 3119 | - */ |
|
| 3120 | - private function addOwnerPrincipalToCalendar(array $calendarInfo): array { |
|
| 3121 | - $ownerPrincipalKey = '{' . \OCA\DAV\DAV\Sharing\Plugin::NS_OWNCLOUD . '}owner-principal'; |
|
| 3122 | - $displaynameKey = '{' . \OCA\DAV\DAV\Sharing\Plugin::NS_NEXTCLOUD . '}owner-displayname'; |
|
| 3123 | - if (isset($calendarInfo[$ownerPrincipalKey])) { |
|
| 3124 | - $uri = $calendarInfo[$ownerPrincipalKey]; |
|
| 3125 | - } else { |
|
| 3126 | - $uri = $calendarInfo['principaluri']; |
|
| 3127 | - } |
|
| 3128 | - |
|
| 3129 | - $principalInformation = $this->principalBackend->getPrincipalByPath($uri); |
|
| 3130 | - if (isset($principalInformation['{DAV:}displayname'])) { |
|
| 3131 | - $calendarInfo[$displaynameKey] = $principalInformation['{DAV:}displayname']; |
|
| 3132 | - } |
|
| 3133 | - return $calendarInfo; |
|
| 3134 | - } |
|
| 3135 | - |
|
| 3136 | - private function addResourceTypeToCalendar(array $row, array $calendar): array { |
|
| 3137 | - if (isset($row['deleted_at'])) { |
|
| 3138 | - // Columns is set and not null -> this is a deleted calendar |
|
| 3139 | - // we send a custom resourcetype to hide the deleted calendar |
|
| 3140 | - // from ordinary DAV clients, but the Calendar app will know |
|
| 3141 | - // how to handle this special resource. |
|
| 3142 | - $calendar['{DAV:}resourcetype'] = new DAV\Xml\Property\ResourceType([ |
|
| 3143 | - '{DAV:}collection', |
|
| 3144 | - sprintf('{%s}deleted-calendar', \OCA\DAV\DAV\Sharing\Plugin::NS_NEXTCLOUD), |
|
| 3145 | - ]); |
|
| 3146 | - } |
|
| 3147 | - return $calendar; |
|
| 3148 | - } |
|
| 3149 | - |
|
| 3150 | - /** |
|
| 3151 | - * Amend the calendar info with database row data |
|
| 3152 | - * |
|
| 3153 | - * @param array $row |
|
| 3154 | - * @param array $calendar |
|
| 3155 | - * |
|
| 3156 | - * @return array |
|
| 3157 | - */ |
|
| 3158 | - private function rowToCalendar($row, array $calendar): array { |
|
| 3159 | - foreach ($this->propertyMap as $xmlName => [$dbName, $type]) { |
|
| 3160 | - $value = $row[$dbName]; |
|
| 3161 | - if ($value !== null) { |
|
| 3162 | - settype($value, $type); |
|
| 3163 | - } |
|
| 3164 | - $calendar[$xmlName] = $value; |
|
| 3165 | - } |
|
| 3166 | - return $calendar; |
|
| 3167 | - } |
|
| 3168 | - |
|
| 3169 | - /** |
|
| 3170 | - * Amend the subscription info with database row data |
|
| 3171 | - * |
|
| 3172 | - * @param array $row |
|
| 3173 | - * @param array $subscription |
|
| 3174 | - * |
|
| 3175 | - * @return array |
|
| 3176 | - */ |
|
| 3177 | - private function rowToSubscription($row, array $subscription): array { |
|
| 3178 | - foreach ($this->subscriptionPropertyMap as $xmlName => [$dbName, $type]) { |
|
| 3179 | - $value = $row[$dbName]; |
|
| 3180 | - if ($value !== null) { |
|
| 3181 | - settype($value, $type); |
|
| 3182 | - } |
|
| 3183 | - $subscription[$xmlName] = $value; |
|
| 3184 | - } |
|
| 3185 | - return $subscription; |
|
| 3186 | - } |
|
| 122 | + public const CALENDAR_TYPE_CALENDAR = 0; |
|
| 123 | + public const CALENDAR_TYPE_SUBSCRIPTION = 1; |
|
| 124 | + |
|
| 125 | + public const PERSONAL_CALENDAR_URI = 'personal'; |
|
| 126 | + public const PERSONAL_CALENDAR_NAME = 'Personal'; |
|
| 127 | + |
|
| 128 | + public const RESOURCE_BOOKING_CALENDAR_URI = 'calendar'; |
|
| 129 | + public const RESOURCE_BOOKING_CALENDAR_NAME = 'Calendar'; |
|
| 130 | + |
|
| 131 | + /** |
|
| 132 | + * We need to specify a max date, because we need to stop *somewhere* |
|
| 133 | + * |
|
| 134 | + * On 32 bit system the maximum for a signed integer is 2147483647, so |
|
| 135 | + * MAX_DATE cannot be higher than date('Y-m-d', 2147483647) which results |
|
| 136 | + * in 2038-01-19 to avoid problems when the date is converted |
|
| 137 | + * to a unix timestamp. |
|
| 138 | + */ |
|
| 139 | + public const MAX_DATE = '2038-01-01'; |
|
| 140 | + |
|
| 141 | + public const ACCESS_PUBLIC = 4; |
|
| 142 | + public const CLASSIFICATION_PUBLIC = 0; |
|
| 143 | + public const CLASSIFICATION_PRIVATE = 1; |
|
| 144 | + public const CLASSIFICATION_CONFIDENTIAL = 2; |
|
| 145 | + |
|
| 146 | + /** |
|
| 147 | + * List of CalDAV properties, and how they map to database field names and their type |
|
| 148 | + * Add your own properties by simply adding on to this array. |
|
| 149 | + * |
|
| 150 | + * @var array |
|
| 151 | + * @psalm-var array<string, string[]> |
|
| 152 | + */ |
|
| 153 | + public array $propertyMap = [ |
|
| 154 | + '{DAV:}displayname' => ['displayname', 'string'], |
|
| 155 | + '{urn:ietf:params:xml:ns:caldav}calendar-description' => ['description', 'string'], |
|
| 156 | + '{urn:ietf:params:xml:ns:caldav}calendar-timezone' => ['timezone', 'string'], |
|
| 157 | + '{http://apple.com/ns/ical/}calendar-order' => ['calendarorder', 'int'], |
|
| 158 | + '{http://apple.com/ns/ical/}calendar-color' => ['calendarcolor', 'string'], |
|
| 159 | + '{' . \OCA\DAV\DAV\Sharing\Plugin::NS_NEXTCLOUD . '}deleted-at' => ['deleted_at', 'int'], |
|
| 160 | + ]; |
|
| 161 | + |
|
| 162 | + /** |
|
| 163 | + * List of subscription properties, and how they map to database field names. |
|
| 164 | + * |
|
| 165 | + * @var array |
|
| 166 | + */ |
|
| 167 | + public array $subscriptionPropertyMap = [ |
|
| 168 | + '{DAV:}displayname' => ['displayname', 'string'], |
|
| 169 | + '{http://apple.com/ns/ical/}refreshrate' => ['refreshrate', 'string'], |
|
| 170 | + '{http://apple.com/ns/ical/}calendar-order' => ['calendarorder', 'int'], |
|
| 171 | + '{http://apple.com/ns/ical/}calendar-color' => ['calendarcolor', 'string'], |
|
| 172 | + '{http://calendarserver.org/ns/}subscribed-strip-todos' => ['striptodos', 'bool'], |
|
| 173 | + '{http://calendarserver.org/ns/}subscribed-strip-alarms' => ['stripalarms', 'string'], |
|
| 174 | + '{http://calendarserver.org/ns/}subscribed-strip-attachments' => ['stripattachments', 'string'], |
|
| 175 | + ]; |
|
| 176 | + |
|
| 177 | + /** |
|
| 178 | + * properties to index |
|
| 179 | + * |
|
| 180 | + * This list has to be kept in sync with ICalendarQuery::SEARCH_PROPERTY_* |
|
| 181 | + * |
|
| 182 | + * @see \OCP\Calendar\ICalendarQuery |
|
| 183 | + */ |
|
| 184 | + private const INDEXED_PROPERTIES = [ |
|
| 185 | + 'CATEGORIES', |
|
| 186 | + 'COMMENT', |
|
| 187 | + 'DESCRIPTION', |
|
| 188 | + 'LOCATION', |
|
| 189 | + 'RESOURCES', |
|
| 190 | + 'STATUS', |
|
| 191 | + 'SUMMARY', |
|
| 192 | + 'ATTENDEE', |
|
| 193 | + 'CONTACT', |
|
| 194 | + 'ORGANIZER' |
|
| 195 | + ]; |
|
| 196 | + |
|
| 197 | + /** @var array parameters to index */ |
|
| 198 | + public static array $indexParameters = [ |
|
| 199 | + 'ATTENDEE' => ['CN'], |
|
| 200 | + 'ORGANIZER' => ['CN'], |
|
| 201 | + ]; |
|
| 202 | + |
|
| 203 | + /** |
|
| 204 | + * @var string[] Map of uid => display name |
|
| 205 | + */ |
|
| 206 | + protected array $userDisplayNames; |
|
| 207 | + |
|
| 208 | + private IDBConnection $db; |
|
| 209 | + private Backend $calendarSharingBackend; |
|
| 210 | + private Principal $principalBackend; |
|
| 211 | + private IUserManager $userManager; |
|
| 212 | + private ISecureRandom $random; |
|
| 213 | + private LoggerInterface $logger; |
|
| 214 | + private IEventDispatcher $dispatcher; |
|
| 215 | + private IConfig $config; |
|
| 216 | + private bool $legacyEndpoint; |
|
| 217 | + private string $dbObjectPropertiesTable = 'calendarobjects_props'; |
|
| 218 | + |
|
| 219 | + public function __construct(IDBConnection $db, |
|
| 220 | + Principal $principalBackend, |
|
| 221 | + IUserManager $userManager, |
|
| 222 | + IGroupManager $groupManager, |
|
| 223 | + ISecureRandom $random, |
|
| 224 | + LoggerInterface $logger, |
|
| 225 | + IEventDispatcher $dispatcher, |
|
| 226 | + IConfig $config, |
|
| 227 | + bool $legacyEndpoint = false) { |
|
| 228 | + $this->db = $db; |
|
| 229 | + $this->principalBackend = $principalBackend; |
|
| 230 | + $this->userManager = $userManager; |
|
| 231 | + $this->calendarSharingBackend = new Backend($this->db, $this->userManager, $groupManager, $principalBackend, 'calendar'); |
|
| 232 | + $this->random = $random; |
|
| 233 | + $this->logger = $logger; |
|
| 234 | + $this->dispatcher = $dispatcher; |
|
| 235 | + $this->config = $config; |
|
| 236 | + $this->legacyEndpoint = $legacyEndpoint; |
|
| 237 | + } |
|
| 238 | + |
|
| 239 | + /** |
|
| 240 | + * Return the number of calendars for a principal |
|
| 241 | + * |
|
| 242 | + * By default this excludes the automatically generated birthday calendar |
|
| 243 | + * |
|
| 244 | + * @param $principalUri |
|
| 245 | + * @param bool $excludeBirthday |
|
| 246 | + * @return int |
|
| 247 | + */ |
|
| 248 | + public function getCalendarsForUserCount($principalUri, $excludeBirthday = true) { |
|
| 249 | + $principalUri = $this->convertPrincipal($principalUri, true); |
|
| 250 | + $query = $this->db->getQueryBuilder(); |
|
| 251 | + $query->select($query->func()->count('*')) |
|
| 252 | + ->from('calendars'); |
|
| 253 | + |
|
| 254 | + if ($principalUri === '') { |
|
| 255 | + $query->where($query->expr()->emptyString('principaluri')); |
|
| 256 | + } else { |
|
| 257 | + $query->where($query->expr()->eq('principaluri', $query->createNamedParameter($principalUri))); |
|
| 258 | + } |
|
| 259 | + |
|
| 260 | + if ($excludeBirthday) { |
|
| 261 | + $query->andWhere($query->expr()->neq('uri', $query->createNamedParameter(BirthdayService::BIRTHDAY_CALENDAR_URI))); |
|
| 262 | + } |
|
| 263 | + |
|
| 264 | + $result = $query->executeQuery(); |
|
| 265 | + $column = (int)$result->fetchOne(); |
|
| 266 | + $result->closeCursor(); |
|
| 267 | + return $column; |
|
| 268 | + } |
|
| 269 | + |
|
| 270 | + /** |
|
| 271 | + * @return array{id: int, deleted_at: int}[] |
|
| 272 | + */ |
|
| 273 | + public function getDeletedCalendars(int $deletedBefore): array { |
|
| 274 | + $qb = $this->db->getQueryBuilder(); |
|
| 275 | + $qb->select(['id', 'deleted_at']) |
|
| 276 | + ->from('calendars') |
|
| 277 | + ->where($qb->expr()->isNotNull('deleted_at')) |
|
| 278 | + ->andWhere($qb->expr()->lt('deleted_at', $qb->createNamedParameter($deletedBefore))); |
|
| 279 | + $result = $qb->executeQuery(); |
|
| 280 | + $raw = $result->fetchAll(); |
|
| 281 | + $result->closeCursor(); |
|
| 282 | + return array_map(function ($row) { |
|
| 283 | + return [ |
|
| 284 | + 'id' => (int) $row['id'], |
|
| 285 | + 'deleted_at' => (int) $row['deleted_at'], |
|
| 286 | + ]; |
|
| 287 | + }, $raw); |
|
| 288 | + } |
|
| 289 | + |
|
| 290 | + /** |
|
| 291 | + * Returns a list of calendars for a principal. |
|
| 292 | + * |
|
| 293 | + * Every project is an array with the following keys: |
|
| 294 | + * * id, a unique id that will be used by other functions to modify the |
|
| 295 | + * calendar. This can be the same as the uri or a database key. |
|
| 296 | + * * uri, which the basename of the uri with which the calendar is |
|
| 297 | + * accessed. |
|
| 298 | + * * principaluri. The owner of the calendar. Almost always the same as |
|
| 299 | + * principalUri passed to this method. |
|
| 300 | + * |
|
| 301 | + * Furthermore it can contain webdav properties in clark notation. A very |
|
| 302 | + * common one is '{DAV:}displayname'. |
|
| 303 | + * |
|
| 304 | + * Many clients also require: |
|
| 305 | + * {urn:ietf:params:xml:ns:caldav}supported-calendar-component-set |
|
| 306 | + * For this property, you can just return an instance of |
|
| 307 | + * Sabre\CalDAV\Property\SupportedCalendarComponentSet. |
|
| 308 | + * |
|
| 309 | + * If you return {http://sabredav.org/ns}read-only and set the value to 1, |
|
| 310 | + * ACL will automatically be put in read-only mode. |
|
| 311 | + * |
|
| 312 | + * @param string $principalUri |
|
| 313 | + * @return array |
|
| 314 | + */ |
|
| 315 | + public function getCalendarsForUser($principalUri) { |
|
| 316 | + $principalUriOriginal = $principalUri; |
|
| 317 | + $principalUri = $this->convertPrincipal($principalUri, true); |
|
| 318 | + $fields = array_column($this->propertyMap, 0); |
|
| 319 | + $fields[] = 'id'; |
|
| 320 | + $fields[] = 'uri'; |
|
| 321 | + $fields[] = 'synctoken'; |
|
| 322 | + $fields[] = 'components'; |
|
| 323 | + $fields[] = 'principaluri'; |
|
| 324 | + $fields[] = 'transparent'; |
|
| 325 | + |
|
| 326 | + // Making fields a comma-delimited list |
|
| 327 | + $query = $this->db->getQueryBuilder(); |
|
| 328 | + $query->select($fields) |
|
| 329 | + ->from('calendars') |
|
| 330 | + ->orderBy('calendarorder', 'ASC'); |
|
| 331 | + |
|
| 332 | + if ($principalUri === '') { |
|
| 333 | + $query->where($query->expr()->emptyString('principaluri')); |
|
| 334 | + } else { |
|
| 335 | + $query->where($query->expr()->eq('principaluri', $query->createNamedParameter($principalUri))); |
|
| 336 | + } |
|
| 337 | + |
|
| 338 | + $result = $query->executeQuery(); |
|
| 339 | + |
|
| 340 | + $calendars = []; |
|
| 341 | + while ($row = $result->fetch()) { |
|
| 342 | + $row['principaluri'] = (string) $row['principaluri']; |
|
| 343 | + $components = []; |
|
| 344 | + if ($row['components']) { |
|
| 345 | + $components = explode(',',$row['components']); |
|
| 346 | + } |
|
| 347 | + |
|
| 348 | + $calendar = [ |
|
| 349 | + 'id' => $row['id'], |
|
| 350 | + 'uri' => $row['uri'], |
|
| 351 | + 'principaluri' => $this->convertPrincipal($row['principaluri'], !$this->legacyEndpoint), |
|
| 352 | + '{' . Plugin::NS_CALENDARSERVER . '}getctag' => 'http://sabre.io/ns/sync/' . ($row['synctoken']?$row['synctoken']:'0'), |
|
| 353 | + '{http://sabredav.org/ns}sync-token' => $row['synctoken']?$row['synctoken']:'0', |
|
| 354 | + '{' . Plugin::NS_CALDAV . '}supported-calendar-component-set' => new SupportedCalendarComponentSet($components), |
|
| 355 | + '{' . Plugin::NS_CALDAV . '}schedule-calendar-transp' => new ScheduleCalendarTransp($row['transparent']?'transparent':'opaque'), |
|
| 356 | + '{' . \OCA\DAV\DAV\Sharing\Plugin::NS_OWNCLOUD . '}owner-principal' => $this->convertPrincipal($principalUri, !$this->legacyEndpoint), |
|
| 357 | + ]; |
|
| 358 | + |
|
| 359 | + $calendar = $this->rowToCalendar($row, $calendar); |
|
| 360 | + $calendar = $this->addOwnerPrincipalToCalendar($calendar); |
|
| 361 | + $calendar = $this->addResourceTypeToCalendar($row, $calendar); |
|
| 362 | + |
|
| 363 | + if (!isset($calendars[$calendar['id']])) { |
|
| 364 | + $calendars[$calendar['id']] = $calendar; |
|
| 365 | + } |
|
| 366 | + } |
|
| 367 | + $result->closeCursor(); |
|
| 368 | + |
|
| 369 | + // query for shared calendars |
|
| 370 | + $principals = $this->principalBackend->getGroupMembership($principalUriOriginal, true); |
|
| 371 | + $principals = array_merge($principals, $this->principalBackend->getCircleMembership($principalUriOriginal)); |
|
| 372 | + |
|
| 373 | + $principals[] = $principalUri; |
|
| 374 | + |
|
| 375 | + $fields = array_column($this->propertyMap, 0); |
|
| 376 | + $fields[] = 'a.id'; |
|
| 377 | + $fields[] = 'a.uri'; |
|
| 378 | + $fields[] = 'a.synctoken'; |
|
| 379 | + $fields[] = 'a.components'; |
|
| 380 | + $fields[] = 'a.principaluri'; |
|
| 381 | + $fields[] = 'a.transparent'; |
|
| 382 | + $fields[] = 's.access'; |
|
| 383 | + $query = $this->db->getQueryBuilder(); |
|
| 384 | + $query->select($fields) |
|
| 385 | + ->from('dav_shares', 's') |
|
| 386 | + ->join('s', 'calendars', 'a', $query->expr()->eq('s.resourceid', 'a.id')) |
|
| 387 | + ->where($query->expr()->in('s.principaluri', $query->createParameter('principaluri'))) |
|
| 388 | + ->andWhere($query->expr()->eq('s.type', $query->createParameter('type'))) |
|
| 389 | + ->setParameter('type', 'calendar') |
|
| 390 | + ->setParameter('principaluri', $principals, \Doctrine\DBAL\Connection::PARAM_STR_ARRAY); |
|
| 391 | + |
|
| 392 | + $result = $query->executeQuery(); |
|
| 393 | + |
|
| 394 | + $readOnlyPropertyName = '{' . \OCA\DAV\DAV\Sharing\Plugin::NS_OWNCLOUD . '}read-only'; |
|
| 395 | + while ($row = $result->fetch()) { |
|
| 396 | + $row['principaluri'] = (string) $row['principaluri']; |
|
| 397 | + if ($row['principaluri'] === $principalUri) { |
|
| 398 | + continue; |
|
| 399 | + } |
|
| 400 | + |
|
| 401 | + $readOnly = (int) $row['access'] === Backend::ACCESS_READ; |
|
| 402 | + if (isset($calendars[$row['id']])) { |
|
| 403 | + if ($readOnly) { |
|
| 404 | + // New share can not have more permissions then the old one. |
|
| 405 | + continue; |
|
| 406 | + } |
|
| 407 | + if (isset($calendars[$row['id']][$readOnlyPropertyName]) && |
|
| 408 | + $calendars[$row['id']][$readOnlyPropertyName] === 0) { |
|
| 409 | + // Old share is already read-write, no more permissions can be gained |
|
| 410 | + continue; |
|
| 411 | + } |
|
| 412 | + } |
|
| 413 | + |
|
| 414 | + [, $name] = Uri\split($row['principaluri']); |
|
| 415 | + $uri = $row['uri'] . '_shared_by_' . $name; |
|
| 416 | + $row['displayname'] = $row['displayname'] . ' (' . $this->getUserDisplayName($name) . ')'; |
|
| 417 | + $components = []; |
|
| 418 | + if ($row['components']) { |
|
| 419 | + $components = explode(',',$row['components']); |
|
| 420 | + } |
|
| 421 | + $calendar = [ |
|
| 422 | + 'id' => $row['id'], |
|
| 423 | + 'uri' => $uri, |
|
| 424 | + 'principaluri' => $this->convertPrincipal($principalUri, !$this->legacyEndpoint), |
|
| 425 | + '{' . Plugin::NS_CALENDARSERVER . '}getctag' => 'http://sabre.io/ns/sync/' . ($row['synctoken']?$row['synctoken']:'0'), |
|
| 426 | + '{http://sabredav.org/ns}sync-token' => $row['synctoken']?$row['synctoken']:'0', |
|
| 427 | + '{' . Plugin::NS_CALDAV . '}supported-calendar-component-set' => new SupportedCalendarComponentSet($components), |
|
| 428 | + '{' . Plugin::NS_CALDAV . '}schedule-calendar-transp' => new ScheduleCalendarTransp('transparent'), |
|
| 429 | + '{' . \OCA\DAV\DAV\Sharing\Plugin::NS_OWNCLOUD . '}owner-principal' => $this->convertPrincipal($row['principaluri'], !$this->legacyEndpoint), |
|
| 430 | + $readOnlyPropertyName => $readOnly, |
|
| 431 | + ]; |
|
| 432 | + |
|
| 433 | + $calendar = $this->rowToCalendar($row, $calendar); |
|
| 434 | + $calendar = $this->addOwnerPrincipalToCalendar($calendar); |
|
| 435 | + $calendar = $this->addResourceTypeToCalendar($row, $calendar); |
|
| 436 | + |
|
| 437 | + $calendars[$calendar['id']] = $calendar; |
|
| 438 | + } |
|
| 439 | + $result->closeCursor(); |
|
| 440 | + |
|
| 441 | + return array_values($calendars); |
|
| 442 | + } |
|
| 443 | + |
|
| 444 | + /** |
|
| 445 | + * @param $principalUri |
|
| 446 | + * @return array |
|
| 447 | + */ |
|
| 448 | + public function getUsersOwnCalendars($principalUri) { |
|
| 449 | + $principalUri = $this->convertPrincipal($principalUri, true); |
|
| 450 | + $fields = array_column($this->propertyMap, 0); |
|
| 451 | + $fields[] = 'id'; |
|
| 452 | + $fields[] = 'uri'; |
|
| 453 | + $fields[] = 'synctoken'; |
|
| 454 | + $fields[] = 'components'; |
|
| 455 | + $fields[] = 'principaluri'; |
|
| 456 | + $fields[] = 'transparent'; |
|
| 457 | + // Making fields a comma-delimited list |
|
| 458 | + $query = $this->db->getQueryBuilder(); |
|
| 459 | + $query->select($fields)->from('calendars') |
|
| 460 | + ->where($query->expr()->eq('principaluri', $query->createNamedParameter($principalUri))) |
|
| 461 | + ->orderBy('calendarorder', 'ASC'); |
|
| 462 | + $stmt = $query->executeQuery(); |
|
| 463 | + $calendars = []; |
|
| 464 | + while ($row = $stmt->fetch()) { |
|
| 465 | + $row['principaluri'] = (string) $row['principaluri']; |
|
| 466 | + $components = []; |
|
| 467 | + if ($row['components']) { |
|
| 468 | + $components = explode(',',$row['components']); |
|
| 469 | + } |
|
| 470 | + $calendar = [ |
|
| 471 | + 'id' => $row['id'], |
|
| 472 | + 'uri' => $row['uri'], |
|
| 473 | + 'principaluri' => $this->convertPrincipal($row['principaluri'], !$this->legacyEndpoint), |
|
| 474 | + '{' . Plugin::NS_CALENDARSERVER . '}getctag' => 'http://sabre.io/ns/sync/' . ($row['synctoken']?$row['synctoken']:'0'), |
|
| 475 | + '{http://sabredav.org/ns}sync-token' => $row['synctoken']?$row['synctoken']:'0', |
|
| 476 | + '{' . Plugin::NS_CALDAV . '}supported-calendar-component-set' => new SupportedCalendarComponentSet($components), |
|
| 477 | + '{' . Plugin::NS_CALDAV . '}schedule-calendar-transp' => new ScheduleCalendarTransp($row['transparent']?'transparent':'opaque'), |
|
| 478 | + ]; |
|
| 479 | + |
|
| 480 | + $calendar = $this->rowToCalendar($row, $calendar); |
|
| 481 | + $calendar = $this->addOwnerPrincipalToCalendar($calendar); |
|
| 482 | + $calendar = $this->addResourceTypeToCalendar($row, $calendar); |
|
| 483 | + |
|
| 484 | + if (!isset($calendars[$calendar['id']])) { |
|
| 485 | + $calendars[$calendar['id']] = $calendar; |
|
| 486 | + } |
|
| 487 | + } |
|
| 488 | + $stmt->closeCursor(); |
|
| 489 | + return array_values($calendars); |
|
| 490 | + } |
|
| 491 | + |
|
| 492 | + |
|
| 493 | + /** |
|
| 494 | + * @param $uid |
|
| 495 | + * @return string |
|
| 496 | + */ |
|
| 497 | + private function getUserDisplayName($uid) { |
|
| 498 | + if (!isset($this->userDisplayNames[$uid])) { |
|
| 499 | + $user = $this->userManager->get($uid); |
|
| 500 | + |
|
| 501 | + if ($user instanceof IUser) { |
|
| 502 | + $this->userDisplayNames[$uid] = $user->getDisplayName(); |
|
| 503 | + } else { |
|
| 504 | + $this->userDisplayNames[$uid] = $uid; |
|
| 505 | + } |
|
| 506 | + } |
|
| 507 | + |
|
| 508 | + return $this->userDisplayNames[$uid]; |
|
| 509 | + } |
|
| 510 | + |
|
| 511 | + /** |
|
| 512 | + * @return array |
|
| 513 | + */ |
|
| 514 | + public function getPublicCalendars() { |
|
| 515 | + $fields = array_column($this->propertyMap, 0); |
|
| 516 | + $fields[] = 'a.id'; |
|
| 517 | + $fields[] = 'a.uri'; |
|
| 518 | + $fields[] = 'a.synctoken'; |
|
| 519 | + $fields[] = 'a.components'; |
|
| 520 | + $fields[] = 'a.principaluri'; |
|
| 521 | + $fields[] = 'a.transparent'; |
|
| 522 | + $fields[] = 's.access'; |
|
| 523 | + $fields[] = 's.publicuri'; |
|
| 524 | + $calendars = []; |
|
| 525 | + $query = $this->db->getQueryBuilder(); |
|
| 526 | + $result = $query->select($fields) |
|
| 527 | + ->from('dav_shares', 's') |
|
| 528 | + ->join('s', 'calendars', 'a', $query->expr()->eq('s.resourceid', 'a.id')) |
|
| 529 | + ->where($query->expr()->in('s.access', $query->createNamedParameter(self::ACCESS_PUBLIC))) |
|
| 530 | + ->andWhere($query->expr()->eq('s.type', $query->createNamedParameter('calendar'))) |
|
| 531 | + ->executeQuery(); |
|
| 532 | + |
|
| 533 | + while ($row = $result->fetch()) { |
|
| 534 | + $row['principaluri'] = (string) $row['principaluri']; |
|
| 535 | + [, $name] = Uri\split($row['principaluri']); |
|
| 536 | + $row['displayname'] = $row['displayname'] . "($name)"; |
|
| 537 | + $components = []; |
|
| 538 | + if ($row['components']) { |
|
| 539 | + $components = explode(',',$row['components']); |
|
| 540 | + } |
|
| 541 | + $calendar = [ |
|
| 542 | + 'id' => $row['id'], |
|
| 543 | + 'uri' => $row['publicuri'], |
|
| 544 | + 'principaluri' => $this->convertPrincipal($row['principaluri'], !$this->legacyEndpoint), |
|
| 545 | + '{' . Plugin::NS_CALENDARSERVER . '}getctag' => 'http://sabre.io/ns/sync/' . ($row['synctoken']?$row['synctoken']:'0'), |
|
| 546 | + '{http://sabredav.org/ns}sync-token' => $row['synctoken']?$row['synctoken']:'0', |
|
| 547 | + '{' . Plugin::NS_CALDAV . '}supported-calendar-component-set' => new SupportedCalendarComponentSet($components), |
|
| 548 | + '{' . Plugin::NS_CALDAV . '}schedule-calendar-transp' => new ScheduleCalendarTransp($row['transparent']?'transparent':'opaque'), |
|
| 549 | + '{' . \OCA\DAV\DAV\Sharing\Plugin::NS_OWNCLOUD . '}owner-principal' => $this->convertPrincipal($row['principaluri'], $this->legacyEndpoint), |
|
| 550 | + '{' . \OCA\DAV\DAV\Sharing\Plugin::NS_OWNCLOUD . '}read-only' => (int)$row['access'] === Backend::ACCESS_READ, |
|
| 551 | + '{' . \OCA\DAV\DAV\Sharing\Plugin::NS_OWNCLOUD . '}public' => (int)$row['access'] === self::ACCESS_PUBLIC, |
|
| 552 | + ]; |
|
| 553 | + |
|
| 554 | + $calendar = $this->rowToCalendar($row, $calendar); |
|
| 555 | + $calendar = $this->addOwnerPrincipalToCalendar($calendar); |
|
| 556 | + $calendar = $this->addResourceTypeToCalendar($row, $calendar); |
|
| 557 | + |
|
| 558 | + if (!isset($calendars[$calendar['id']])) { |
|
| 559 | + $calendars[$calendar['id']] = $calendar; |
|
| 560 | + } |
|
| 561 | + } |
|
| 562 | + $result->closeCursor(); |
|
| 563 | + |
|
| 564 | + return array_values($calendars); |
|
| 565 | + } |
|
| 566 | + |
|
| 567 | + /** |
|
| 568 | + * @param string $uri |
|
| 569 | + * @return array |
|
| 570 | + * @throws NotFound |
|
| 571 | + */ |
|
| 572 | + public function getPublicCalendar($uri) { |
|
| 573 | + $fields = array_column($this->propertyMap, 0); |
|
| 574 | + $fields[] = 'a.id'; |
|
| 575 | + $fields[] = 'a.uri'; |
|
| 576 | + $fields[] = 'a.synctoken'; |
|
| 577 | + $fields[] = 'a.components'; |
|
| 578 | + $fields[] = 'a.principaluri'; |
|
| 579 | + $fields[] = 'a.transparent'; |
|
| 580 | + $fields[] = 's.access'; |
|
| 581 | + $fields[] = 's.publicuri'; |
|
| 582 | + $query = $this->db->getQueryBuilder(); |
|
| 583 | + $result = $query->select($fields) |
|
| 584 | + ->from('dav_shares', 's') |
|
| 585 | + ->join('s', 'calendars', 'a', $query->expr()->eq('s.resourceid', 'a.id')) |
|
| 586 | + ->where($query->expr()->in('s.access', $query->createNamedParameter(self::ACCESS_PUBLIC))) |
|
| 587 | + ->andWhere($query->expr()->eq('s.type', $query->createNamedParameter('calendar'))) |
|
| 588 | + ->andWhere($query->expr()->eq('s.publicuri', $query->createNamedParameter($uri))) |
|
| 589 | + ->executeQuery(); |
|
| 590 | + |
|
| 591 | + $row = $result->fetch(); |
|
| 592 | + |
|
| 593 | + $result->closeCursor(); |
|
| 594 | + |
|
| 595 | + if ($row === false) { |
|
| 596 | + throw new NotFound('Node with name \'' . $uri . '\' could not be found'); |
|
| 597 | + } |
|
| 598 | + |
|
| 599 | + $row['principaluri'] = (string) $row['principaluri']; |
|
| 600 | + [, $name] = Uri\split($row['principaluri']); |
|
| 601 | + $row['displayname'] = $row['displayname'] . ' ' . "($name)"; |
|
| 602 | + $components = []; |
|
| 603 | + if ($row['components']) { |
|
| 604 | + $components = explode(',',$row['components']); |
|
| 605 | + } |
|
| 606 | + $calendar = [ |
|
| 607 | + 'id' => $row['id'], |
|
| 608 | + 'uri' => $row['publicuri'], |
|
| 609 | + 'principaluri' => $this->convertPrincipal($row['principaluri'], !$this->legacyEndpoint), |
|
| 610 | + '{' . Plugin::NS_CALENDARSERVER . '}getctag' => 'http://sabre.io/ns/sync/' . ($row['synctoken']?$row['synctoken']:'0'), |
|
| 611 | + '{http://sabredav.org/ns}sync-token' => $row['synctoken']?$row['synctoken']:'0', |
|
| 612 | + '{' . Plugin::NS_CALDAV . '}supported-calendar-component-set' => new SupportedCalendarComponentSet($components), |
|
| 613 | + '{' . Plugin::NS_CALDAV . '}schedule-calendar-transp' => new ScheduleCalendarTransp($row['transparent']?'transparent':'opaque'), |
|
| 614 | + '{' . \OCA\DAV\DAV\Sharing\Plugin::NS_OWNCLOUD . '}owner-principal' => $this->convertPrincipal($row['principaluri'], !$this->legacyEndpoint), |
|
| 615 | + '{' . \OCA\DAV\DAV\Sharing\Plugin::NS_OWNCLOUD . '}read-only' => (int)$row['access'] === Backend::ACCESS_READ, |
|
| 616 | + '{' . \OCA\DAV\DAV\Sharing\Plugin::NS_OWNCLOUD . '}public' => (int)$row['access'] === self::ACCESS_PUBLIC, |
|
| 617 | + ]; |
|
| 618 | + |
|
| 619 | + $calendar = $this->rowToCalendar($row, $calendar); |
|
| 620 | + $calendar = $this->addOwnerPrincipalToCalendar($calendar); |
|
| 621 | + $calendar = $this->addResourceTypeToCalendar($row, $calendar); |
|
| 622 | + |
|
| 623 | + return $calendar; |
|
| 624 | + } |
|
| 625 | + |
|
| 626 | + /** |
|
| 627 | + * @param string $principal |
|
| 628 | + * @param string $uri |
|
| 629 | + * @return array|null |
|
| 630 | + */ |
|
| 631 | + public function getCalendarByUri($principal, $uri) { |
|
| 632 | + $fields = array_column($this->propertyMap, 0); |
|
| 633 | + $fields[] = 'id'; |
|
| 634 | + $fields[] = 'uri'; |
|
| 635 | + $fields[] = 'synctoken'; |
|
| 636 | + $fields[] = 'components'; |
|
| 637 | + $fields[] = 'principaluri'; |
|
| 638 | + $fields[] = 'transparent'; |
|
| 639 | + |
|
| 640 | + // Making fields a comma-delimited list |
|
| 641 | + $query = $this->db->getQueryBuilder(); |
|
| 642 | + $query->select($fields)->from('calendars') |
|
| 643 | + ->where($query->expr()->eq('uri', $query->createNamedParameter($uri))) |
|
| 644 | + ->andWhere($query->expr()->eq('principaluri', $query->createNamedParameter($principal))) |
|
| 645 | + ->setMaxResults(1); |
|
| 646 | + $stmt = $query->executeQuery(); |
|
| 647 | + |
|
| 648 | + $row = $stmt->fetch(); |
|
| 649 | + $stmt->closeCursor(); |
|
| 650 | + if ($row === false) { |
|
| 651 | + return null; |
|
| 652 | + } |
|
| 653 | + |
|
| 654 | + $row['principaluri'] = (string) $row['principaluri']; |
|
| 655 | + $components = []; |
|
| 656 | + if ($row['components']) { |
|
| 657 | + $components = explode(',',$row['components']); |
|
| 658 | + } |
|
| 659 | + |
|
| 660 | + $calendar = [ |
|
| 661 | + 'id' => $row['id'], |
|
| 662 | + 'uri' => $row['uri'], |
|
| 663 | + 'principaluri' => $this->convertPrincipal($row['principaluri'], !$this->legacyEndpoint), |
|
| 664 | + '{' . Plugin::NS_CALENDARSERVER . '}getctag' => 'http://sabre.io/ns/sync/' . ($row['synctoken']?$row['synctoken']:'0'), |
|
| 665 | + '{http://sabredav.org/ns}sync-token' => $row['synctoken']?$row['synctoken']:'0', |
|
| 666 | + '{' . Plugin::NS_CALDAV . '}supported-calendar-component-set' => new SupportedCalendarComponentSet($components), |
|
| 667 | + '{' . Plugin::NS_CALDAV . '}schedule-calendar-transp' => new ScheduleCalendarTransp($row['transparent']?'transparent':'opaque'), |
|
| 668 | + ]; |
|
| 669 | + |
|
| 670 | + $calendar = $this->rowToCalendar($row, $calendar); |
|
| 671 | + $calendar = $this->addOwnerPrincipalToCalendar($calendar); |
|
| 672 | + $calendar = $this->addResourceTypeToCalendar($row, $calendar); |
|
| 673 | + |
|
| 674 | + return $calendar; |
|
| 675 | + } |
|
| 676 | + |
|
| 677 | + /** |
|
| 678 | + * @return array{id: int, uri: string, '{http://calendarserver.org/ns/}getctag': string, '{http://sabredav.org/ns}sync-token': int, '{urn:ietf:params:xml:ns:caldav}supported-calendar-component-set': SupportedCalendarComponentSet, '{urn:ietf:params:xml:ns:caldav}schedule-calendar-transp': ScheduleCalendarTransp }|null |
|
| 679 | + */ |
|
| 680 | + public function getCalendarById(int $calendarId): ?array { |
|
| 681 | + $fields = array_column($this->propertyMap, 0); |
|
| 682 | + $fields[] = 'id'; |
|
| 683 | + $fields[] = 'uri'; |
|
| 684 | + $fields[] = 'synctoken'; |
|
| 685 | + $fields[] = 'components'; |
|
| 686 | + $fields[] = 'principaluri'; |
|
| 687 | + $fields[] = 'transparent'; |
|
| 688 | + |
|
| 689 | + // Making fields a comma-delimited list |
|
| 690 | + $query = $this->db->getQueryBuilder(); |
|
| 691 | + $query->select($fields)->from('calendars') |
|
| 692 | + ->where($query->expr()->eq('id', $query->createNamedParameter($calendarId))) |
|
| 693 | + ->setMaxResults(1); |
|
| 694 | + $stmt = $query->executeQuery(); |
|
| 695 | + |
|
| 696 | + $row = $stmt->fetch(); |
|
| 697 | + $stmt->closeCursor(); |
|
| 698 | + if ($row === false) { |
|
| 699 | + return null; |
|
| 700 | + } |
|
| 701 | + |
|
| 702 | + $row['principaluri'] = (string) $row['principaluri']; |
|
| 703 | + $components = []; |
|
| 704 | + if ($row['components']) { |
|
| 705 | + $components = explode(',',$row['components']); |
|
| 706 | + } |
|
| 707 | + |
|
| 708 | + $calendar = [ |
|
| 709 | + 'id' => $row['id'], |
|
| 710 | + 'uri' => $row['uri'], |
|
| 711 | + 'principaluri' => $this->convertPrincipal($row['principaluri'], !$this->legacyEndpoint), |
|
| 712 | + '{' . Plugin::NS_CALENDARSERVER . '}getctag' => 'http://sabre.io/ns/sync/' . ($row['synctoken']?$row['synctoken']:'0'), |
|
| 713 | + '{http://sabredav.org/ns}sync-token' => $row['synctoken'] ?? 0, |
|
| 714 | + '{' . Plugin::NS_CALDAV . '}supported-calendar-component-set' => new SupportedCalendarComponentSet($components), |
|
| 715 | + '{' . Plugin::NS_CALDAV . '}schedule-calendar-transp' => new ScheduleCalendarTransp($row['transparent']?'transparent':'opaque'), |
|
| 716 | + ]; |
|
| 717 | + |
|
| 718 | + $calendar = $this->rowToCalendar($row, $calendar); |
|
| 719 | + $calendar = $this->addOwnerPrincipalToCalendar($calendar); |
|
| 720 | + $calendar = $this->addResourceTypeToCalendar($row, $calendar); |
|
| 721 | + |
|
| 722 | + return $calendar; |
|
| 723 | + } |
|
| 724 | + |
|
| 725 | + /** |
|
| 726 | + * @param $subscriptionId |
|
| 727 | + */ |
|
| 728 | + public function getSubscriptionById($subscriptionId) { |
|
| 729 | + $fields = array_column($this->subscriptionPropertyMap, 0); |
|
| 730 | + $fields[] = 'id'; |
|
| 731 | + $fields[] = 'uri'; |
|
| 732 | + $fields[] = 'source'; |
|
| 733 | + $fields[] = 'synctoken'; |
|
| 734 | + $fields[] = 'principaluri'; |
|
| 735 | + $fields[] = 'lastmodified'; |
|
| 736 | + |
|
| 737 | + $query = $this->db->getQueryBuilder(); |
|
| 738 | + $query->select($fields) |
|
| 739 | + ->from('calendarsubscriptions') |
|
| 740 | + ->where($query->expr()->eq('id', $query->createNamedParameter($subscriptionId))) |
|
| 741 | + ->orderBy('calendarorder', 'asc'); |
|
| 742 | + $stmt = $query->executeQuery(); |
|
| 743 | + |
|
| 744 | + $row = $stmt->fetch(); |
|
| 745 | + $stmt->closeCursor(); |
|
| 746 | + if ($row === false) { |
|
| 747 | + return null; |
|
| 748 | + } |
|
| 749 | + |
|
| 750 | + $row['principaluri'] = (string) $row['principaluri']; |
|
| 751 | + $subscription = [ |
|
| 752 | + 'id' => $row['id'], |
|
| 753 | + 'uri' => $row['uri'], |
|
| 754 | + 'principaluri' => $row['principaluri'], |
|
| 755 | + 'source' => $row['source'], |
|
| 756 | + 'lastmodified' => $row['lastmodified'], |
|
| 757 | + '{' . Plugin::NS_CALDAV . '}supported-calendar-component-set' => new SupportedCalendarComponentSet(['VTODO', 'VEVENT']), |
|
| 758 | + '{http://sabredav.org/ns}sync-token' => $row['synctoken']?$row['synctoken']:'0', |
|
| 759 | + ]; |
|
| 760 | + |
|
| 761 | + return $this->rowToSubscription($row, $subscription); |
|
| 762 | + } |
|
| 763 | + |
|
| 764 | + /** |
|
| 765 | + * Creates a new calendar for a principal. |
|
| 766 | + * |
|
| 767 | + * If the creation was a success, an id must be returned that can be used to reference |
|
| 768 | + * this calendar in other methods, such as updateCalendar. |
|
| 769 | + * |
|
| 770 | + * @param string $principalUri |
|
| 771 | + * @param string $calendarUri |
|
| 772 | + * @param array $properties |
|
| 773 | + * @return int |
|
| 774 | + * |
|
| 775 | + * @throws CalendarException |
|
| 776 | + */ |
|
| 777 | + public function createCalendar($principalUri, $calendarUri, array $properties) { |
|
| 778 | + if (strlen($calendarUri) > 255) { |
|
| 779 | + throw new CalendarException('URI too long. Calendar not created'); |
|
| 780 | + } |
|
| 781 | + |
|
| 782 | + $values = [ |
|
| 783 | + 'principaluri' => $this->convertPrincipal($principalUri, true), |
|
| 784 | + 'uri' => $calendarUri, |
|
| 785 | + 'synctoken' => 1, |
|
| 786 | + 'transparent' => 0, |
|
| 787 | + 'components' => 'VEVENT,VTODO', |
|
| 788 | + 'displayname' => $calendarUri |
|
| 789 | + ]; |
|
| 790 | + |
|
| 791 | + // Default value |
|
| 792 | + $sccs = '{urn:ietf:params:xml:ns:caldav}supported-calendar-component-set'; |
|
| 793 | + if (isset($properties[$sccs])) { |
|
| 794 | + if (!($properties[$sccs] instanceof SupportedCalendarComponentSet)) { |
|
| 795 | + throw new DAV\Exception('The ' . $sccs . ' property must be of type: \Sabre\CalDAV\Property\SupportedCalendarComponentSet'); |
|
| 796 | + } |
|
| 797 | + $values['components'] = implode(',',$properties[$sccs]->getValue()); |
|
| 798 | + } elseif (isset($properties['components'])) { |
|
| 799 | + // Allow to provide components internally without having |
|
| 800 | + // to create a SupportedCalendarComponentSet object |
|
| 801 | + $values['components'] = $properties['components']; |
|
| 802 | + } |
|
| 803 | + |
|
| 804 | + $transp = '{' . Plugin::NS_CALDAV . '}schedule-calendar-transp'; |
|
| 805 | + if (isset($properties[$transp])) { |
|
| 806 | + $values['transparent'] = (int) ($properties[$transp]->getValue() === 'transparent'); |
|
| 807 | + } |
|
| 808 | + |
|
| 809 | + foreach ($this->propertyMap as $xmlName => [$dbName, $type]) { |
|
| 810 | + if (isset($properties[$xmlName])) { |
|
| 811 | + $values[$dbName] = $properties[$xmlName]; |
|
| 812 | + } |
|
| 813 | + } |
|
| 814 | + |
|
| 815 | + $query = $this->db->getQueryBuilder(); |
|
| 816 | + $query->insert('calendars'); |
|
| 817 | + foreach ($values as $column => $value) { |
|
| 818 | + $query->setValue($column, $query->createNamedParameter($value)); |
|
| 819 | + } |
|
| 820 | + $query->executeStatement(); |
|
| 821 | + $calendarId = $query->getLastInsertId(); |
|
| 822 | + |
|
| 823 | + $calendarData = $this->getCalendarById($calendarId); |
|
| 824 | + $this->dispatcher->dispatchTyped(new CalendarCreatedEvent((int)$calendarId, $calendarData)); |
|
| 825 | + |
|
| 826 | + return $calendarId; |
|
| 827 | + } |
|
| 828 | + |
|
| 829 | + /** |
|
| 830 | + * Updates properties for a calendar. |
|
| 831 | + * |
|
| 832 | + * The list of mutations is stored in a Sabre\DAV\PropPatch object. |
|
| 833 | + * To do the actual updates, you must tell this object which properties |
|
| 834 | + * you're going to process with the handle() method. |
|
| 835 | + * |
|
| 836 | + * Calling the handle method is like telling the PropPatch object "I |
|
| 837 | + * promise I can handle updating this property". |
|
| 838 | + * |
|
| 839 | + * Read the PropPatch documentation for more info and examples. |
|
| 840 | + * |
|
| 841 | + * @param mixed $calendarId |
|
| 842 | + * @param PropPatch $propPatch |
|
| 843 | + * @return void |
|
| 844 | + */ |
|
| 845 | + public function updateCalendar($calendarId, PropPatch $propPatch) { |
|
| 846 | + $supportedProperties = array_keys($this->propertyMap); |
|
| 847 | + $supportedProperties[] = '{' . Plugin::NS_CALDAV . '}schedule-calendar-transp'; |
|
| 848 | + |
|
| 849 | + $propPatch->handle($supportedProperties, function ($mutations) use ($calendarId) { |
|
| 850 | + $newValues = []; |
|
| 851 | + foreach ($mutations as $propertyName => $propertyValue) { |
|
| 852 | + switch ($propertyName) { |
|
| 853 | + case '{' . Plugin::NS_CALDAV . '}schedule-calendar-transp': |
|
| 854 | + $fieldName = 'transparent'; |
|
| 855 | + $newValues[$fieldName] = (int) ($propertyValue->getValue() === 'transparent'); |
|
| 856 | + break; |
|
| 857 | + default: |
|
| 858 | + $fieldName = $this->propertyMap[$propertyName][0]; |
|
| 859 | + $newValues[$fieldName] = $propertyValue; |
|
| 860 | + break; |
|
| 861 | + } |
|
| 862 | + } |
|
| 863 | + $query = $this->db->getQueryBuilder(); |
|
| 864 | + $query->update('calendars'); |
|
| 865 | + foreach ($newValues as $fieldName => $value) { |
|
| 866 | + $query->set($fieldName, $query->createNamedParameter($value)); |
|
| 867 | + } |
|
| 868 | + $query->where($query->expr()->eq('id', $query->createNamedParameter($calendarId))); |
|
| 869 | + $query->executeStatement(); |
|
| 870 | + |
|
| 871 | + $this->addChange($calendarId, "", 2); |
|
| 872 | + |
|
| 873 | + $calendarData = $this->getCalendarById($calendarId); |
|
| 874 | + $shares = $this->getShares($calendarId); |
|
| 875 | + $this->dispatcher->dispatchTyped(new CalendarUpdatedEvent($calendarId, $calendarData, $shares, $mutations)); |
|
| 876 | + |
|
| 877 | + return true; |
|
| 878 | + }); |
|
| 879 | + } |
|
| 880 | + |
|
| 881 | + /** |
|
| 882 | + * Delete a calendar and all it's objects |
|
| 883 | + * |
|
| 884 | + * @param mixed $calendarId |
|
| 885 | + * @return void |
|
| 886 | + */ |
|
| 887 | + public function deleteCalendar($calendarId, bool $forceDeletePermanently = false) { |
|
| 888 | + // The calendar is deleted right away if this is either enforced by the caller |
|
| 889 | + // or the special contacts birthday calendar or when the preference of an empty |
|
| 890 | + // retention (0 seconds) is set, which signals a disabled trashbin. |
|
| 891 | + $calendarData = $this->getCalendarById($calendarId); |
|
| 892 | + $isBirthdayCalendar = isset($calendarData['uri']) && $calendarData['uri'] === BirthdayService::BIRTHDAY_CALENDAR_URI; |
|
| 893 | + $trashbinDisabled = $this->config->getAppValue(Application::APP_ID, RetentionService::RETENTION_CONFIG_KEY) === '0'; |
|
| 894 | + if ($forceDeletePermanently || $isBirthdayCalendar || $trashbinDisabled) { |
|
| 895 | + $calendarData = $this->getCalendarById($calendarId); |
|
| 896 | + $shares = $this->getShares($calendarId); |
|
| 897 | + |
|
| 898 | + $qbDeleteCalendarObjectProps = $this->db->getQueryBuilder(); |
|
| 899 | + $qbDeleteCalendarObjectProps->delete($this->dbObjectPropertiesTable) |
|
| 900 | + ->where($qbDeleteCalendarObjectProps->expr()->eq('calendarid', $qbDeleteCalendarObjectProps->createNamedParameter($calendarId))) |
|
| 901 | + ->andWhere($qbDeleteCalendarObjectProps->expr()->eq('calendartype', $qbDeleteCalendarObjectProps->createNamedParameter(self::CALENDAR_TYPE_CALENDAR))) |
|
| 902 | + ->executeStatement(); |
|
| 903 | + |
|
| 904 | + $qbDeleteCalendarObjects = $this->db->getQueryBuilder(); |
|
| 905 | + $qbDeleteCalendarObjects->delete('calendarobjects') |
|
| 906 | + ->where($qbDeleteCalendarObjects->expr()->eq('calendarid', $qbDeleteCalendarObjects->createNamedParameter($calendarId))) |
|
| 907 | + ->andWhere($qbDeleteCalendarObjects->expr()->eq('calendartype', $qbDeleteCalendarObjects->createNamedParameter(self::CALENDAR_TYPE_CALENDAR))) |
|
| 908 | + ->executeStatement(); |
|
| 909 | + |
|
| 910 | + $qbDeleteCalendarChanges = $this->db->getQueryBuilder(); |
|
| 911 | + $qbDeleteCalendarObjects->delete('calendarchanges') |
|
| 912 | + ->where($qbDeleteCalendarChanges->expr()->eq('calendarid', $qbDeleteCalendarChanges->createNamedParameter($calendarId))) |
|
| 913 | + ->andWhere($qbDeleteCalendarChanges->expr()->eq('calendartype', $qbDeleteCalendarChanges->createNamedParameter(self::CALENDAR_TYPE_CALENDAR))) |
|
| 914 | + ->executeStatement(); |
|
| 915 | + |
|
| 916 | + $this->calendarSharingBackend->deleteAllShares($calendarId); |
|
| 917 | + |
|
| 918 | + $qbDeleteCalendar = $this->db->getQueryBuilder(); |
|
| 919 | + $qbDeleteCalendarObjects->delete('calendars') |
|
| 920 | + ->where($qbDeleteCalendar->expr()->eq('id', $qbDeleteCalendar->createNamedParameter($calendarId))) |
|
| 921 | + ->executeStatement(); |
|
| 922 | + |
|
| 923 | + // Only dispatch if we actually deleted anything |
|
| 924 | + if ($calendarData) { |
|
| 925 | + $this->dispatcher->dispatchTyped(new CalendarDeletedEvent($calendarId, $calendarData, $shares)); |
|
| 926 | + } |
|
| 927 | + } else { |
|
| 928 | + $qbMarkCalendarDeleted = $this->db->getQueryBuilder(); |
|
| 929 | + $qbMarkCalendarDeleted->update('calendars') |
|
| 930 | + ->set('deleted_at', $qbMarkCalendarDeleted->createNamedParameter(time())) |
|
| 931 | + ->where($qbMarkCalendarDeleted->expr()->eq('id', $qbMarkCalendarDeleted->createNamedParameter($calendarId))) |
|
| 932 | + ->executeStatement(); |
|
| 933 | + |
|
| 934 | + $calendarData = $this->getCalendarById($calendarId); |
|
| 935 | + $shares = $this->getShares($calendarId); |
|
| 936 | + if ($calendarData) { |
|
| 937 | + $this->dispatcher->dispatchTyped(new CalendarMovedToTrashEvent( |
|
| 938 | + $calendarId, |
|
| 939 | + $calendarData, |
|
| 940 | + $shares |
|
| 941 | + )); |
|
| 942 | + } |
|
| 943 | + } |
|
| 944 | + } |
|
| 945 | + |
|
| 946 | + public function restoreCalendar(int $id): void { |
|
| 947 | + $qb = $this->db->getQueryBuilder(); |
|
| 948 | + $update = $qb->update('calendars') |
|
| 949 | + ->set('deleted_at', $qb->createNamedParameter(null)) |
|
| 950 | + ->where($qb->expr()->eq('id', $qb->createNamedParameter($id, IQueryBuilder::PARAM_INT), IQueryBuilder::PARAM_INT)); |
|
| 951 | + $update->executeStatement(); |
|
| 952 | + |
|
| 953 | + $calendarData = $this->getCalendarById($id); |
|
| 954 | + $shares = $this->getShares($id); |
|
| 955 | + if ($calendarData === null) { |
|
| 956 | + throw new RuntimeException('Calendar data that was just written can\'t be read back. Check your database configuration.'); |
|
| 957 | + } |
|
| 958 | + $this->dispatcher->dispatchTyped(new CalendarRestoredEvent( |
|
| 959 | + $id, |
|
| 960 | + $calendarData, |
|
| 961 | + $shares |
|
| 962 | + )); |
|
| 963 | + } |
|
| 964 | + |
|
| 965 | + /** |
|
| 966 | + * Delete all of an user's shares |
|
| 967 | + * |
|
| 968 | + * @param string $principaluri |
|
| 969 | + * @return void |
|
| 970 | + */ |
|
| 971 | + public function deleteAllSharesByUser($principaluri) { |
|
| 972 | + $this->calendarSharingBackend->deleteAllSharesByUser($principaluri); |
|
| 973 | + } |
|
| 974 | + |
|
| 975 | + /** |
|
| 976 | + * Returns all calendar objects within a calendar. |
|
| 977 | + * |
|
| 978 | + * Every item contains an array with the following keys: |
|
| 979 | + * * calendardata - The iCalendar-compatible calendar data |
|
| 980 | + * * uri - a unique key which will be used to construct the uri. This can |
|
| 981 | + * be any arbitrary string, but making sure it ends with '.ics' is a |
|
| 982 | + * good idea. This is only the basename, or filename, not the full |
|
| 983 | + * path. |
|
| 984 | + * * lastmodified - a timestamp of the last modification time |
|
| 985 | + * * etag - An arbitrary string, surrounded by double-quotes. (e.g.: |
|
| 986 | + * '"abcdef"') |
|
| 987 | + * * size - The size of the calendar objects, in bytes. |
|
| 988 | + * * component - optional, a string containing the type of object, such |
|
| 989 | + * as 'vevent' or 'vtodo'. If specified, this will be used to populate |
|
| 990 | + * the Content-Type header. |
|
| 991 | + * |
|
| 992 | + * Note that the etag is optional, but it's highly encouraged to return for |
|
| 993 | + * speed reasons. |
|
| 994 | + * |
|
| 995 | + * The calendardata is also optional. If it's not returned |
|
| 996 | + * 'getCalendarObject' will be called later, which *is* expected to return |
|
| 997 | + * calendardata. |
|
| 998 | + * |
|
| 999 | + * If neither etag or size are specified, the calendardata will be |
|
| 1000 | + * used/fetched to determine these numbers. If both are specified the |
|
| 1001 | + * amount of times this is needed is reduced by a great degree. |
|
| 1002 | + * |
|
| 1003 | + * @param mixed $calendarId |
|
| 1004 | + * @param int $calendarType |
|
| 1005 | + * @return array |
|
| 1006 | + */ |
|
| 1007 | + public function getCalendarObjects($calendarId, $calendarType = self::CALENDAR_TYPE_CALENDAR):array { |
|
| 1008 | + $query = $this->db->getQueryBuilder(); |
|
| 1009 | + $query->select(['id', 'uri', 'lastmodified', 'etag', 'calendarid', 'size', 'componenttype', 'classification']) |
|
| 1010 | + ->from('calendarobjects') |
|
| 1011 | + ->where($query->expr()->eq('calendarid', $query->createNamedParameter($calendarId))) |
|
| 1012 | + ->andWhere($query->expr()->eq('calendartype', $query->createNamedParameter($calendarType))) |
|
| 1013 | + ->andWhere($query->expr()->isNull('deleted_at')); |
|
| 1014 | + $stmt = $query->executeQuery(); |
|
| 1015 | + |
|
| 1016 | + $result = []; |
|
| 1017 | + foreach ($stmt->fetchAll() as $row) { |
|
| 1018 | + $result[] = [ |
|
| 1019 | + 'id' => $row['id'], |
|
| 1020 | + 'uri' => $row['uri'], |
|
| 1021 | + 'lastmodified' => $row['lastmodified'], |
|
| 1022 | + 'etag' => '"' . $row['etag'] . '"', |
|
| 1023 | + 'calendarid' => $row['calendarid'], |
|
| 1024 | + 'size' => (int)$row['size'], |
|
| 1025 | + 'component' => strtolower($row['componenttype']), |
|
| 1026 | + 'classification' => (int)$row['classification'] |
|
| 1027 | + ]; |
|
| 1028 | + } |
|
| 1029 | + $stmt->closeCursor(); |
|
| 1030 | + |
|
| 1031 | + return $result; |
|
| 1032 | + } |
|
| 1033 | + |
|
| 1034 | + public function getDeletedCalendarObjects(int $deletedBefore): array { |
|
| 1035 | + $query = $this->db->getQueryBuilder(); |
|
| 1036 | + $query->select(['co.id', 'co.uri', 'co.lastmodified', 'co.etag', 'co.calendarid', 'co.calendartype', 'co.size', 'co.componenttype', 'co.classification', 'co.deleted_at']) |
|
| 1037 | + ->from('calendarobjects', 'co') |
|
| 1038 | + ->join('co', 'calendars', 'c', $query->expr()->eq('c.id', 'co.calendarid', IQueryBuilder::PARAM_INT)) |
|
| 1039 | + ->where($query->expr()->isNotNull('co.deleted_at')) |
|
| 1040 | + ->andWhere($query->expr()->lt('co.deleted_at', $query->createNamedParameter($deletedBefore))); |
|
| 1041 | + $stmt = $query->executeQuery(); |
|
| 1042 | + |
|
| 1043 | + $result = []; |
|
| 1044 | + foreach ($stmt->fetchAll() as $row) { |
|
| 1045 | + $result[] = [ |
|
| 1046 | + 'id' => $row['id'], |
|
| 1047 | + 'uri' => $row['uri'], |
|
| 1048 | + 'lastmodified' => $row['lastmodified'], |
|
| 1049 | + 'etag' => '"' . $row['etag'] . '"', |
|
| 1050 | + 'calendarid' => (int) $row['calendarid'], |
|
| 1051 | + 'calendartype' => (int) $row['calendartype'], |
|
| 1052 | + 'size' => (int) $row['size'], |
|
| 1053 | + 'component' => strtolower($row['componenttype']), |
|
| 1054 | + 'classification' => (int) $row['classification'], |
|
| 1055 | + '{' . \OCA\DAV\DAV\Sharing\Plugin::NS_NEXTCLOUD . '}deleted-at' => $row['deleted_at'] === null ? $row['deleted_at'] : (int) $row['deleted_at'], |
|
| 1056 | + ]; |
|
| 1057 | + } |
|
| 1058 | + $stmt->closeCursor(); |
|
| 1059 | + |
|
| 1060 | + return $result; |
|
| 1061 | + } |
|
| 1062 | + |
|
| 1063 | + /** |
|
| 1064 | + * Return all deleted calendar objects by the given principal that are not |
|
| 1065 | + * in deleted calendars. |
|
| 1066 | + * |
|
| 1067 | + * @param string $principalUri |
|
| 1068 | + * @return array |
|
| 1069 | + * @throws Exception |
|
| 1070 | + */ |
|
| 1071 | + public function getDeletedCalendarObjectsByPrincipal(string $principalUri): array { |
|
| 1072 | + $query = $this->db->getQueryBuilder(); |
|
| 1073 | + $query->select(['co.id', 'co.uri', 'co.lastmodified', 'co.etag', 'co.calendarid', 'co.size', 'co.componenttype', 'co.classification', 'co.deleted_at']) |
|
| 1074 | + ->selectAlias('c.uri', 'calendaruri') |
|
| 1075 | + ->from('calendarobjects', 'co') |
|
| 1076 | + ->join('co', 'calendars', 'c', $query->expr()->eq('c.id', 'co.calendarid', IQueryBuilder::PARAM_INT)) |
|
| 1077 | + ->where($query->expr()->eq('principaluri', $query->createNamedParameter($principalUri))) |
|
| 1078 | + ->andWhere($query->expr()->isNotNull('co.deleted_at')) |
|
| 1079 | + ->andWhere($query->expr()->isNull('c.deleted_at')); |
|
| 1080 | + $stmt = $query->executeQuery(); |
|
| 1081 | + |
|
| 1082 | + $result = []; |
|
| 1083 | + while ($row = $stmt->fetch()) { |
|
| 1084 | + $result[] = [ |
|
| 1085 | + 'id' => $row['id'], |
|
| 1086 | + 'uri' => $row['uri'], |
|
| 1087 | + 'lastmodified' => $row['lastmodified'], |
|
| 1088 | + 'etag' => '"' . $row['etag'] . '"', |
|
| 1089 | + 'calendarid' => $row['calendarid'], |
|
| 1090 | + 'calendaruri' => $row['calendaruri'], |
|
| 1091 | + 'size' => (int)$row['size'], |
|
| 1092 | + 'component' => strtolower($row['componenttype']), |
|
| 1093 | + 'classification' => (int)$row['classification'], |
|
| 1094 | + '{' . \OCA\DAV\DAV\Sharing\Plugin::NS_NEXTCLOUD . '}deleted-at' => $row['deleted_at'] === null ? $row['deleted_at'] : (int) $row['deleted_at'], |
|
| 1095 | + ]; |
|
| 1096 | + } |
|
| 1097 | + $stmt->closeCursor(); |
|
| 1098 | + |
|
| 1099 | + return $result; |
|
| 1100 | + } |
|
| 1101 | + |
|
| 1102 | + /** |
|
| 1103 | + * Returns information from a single calendar object, based on it's object |
|
| 1104 | + * uri. |
|
| 1105 | + * |
|
| 1106 | + * The object uri is only the basename, or filename and not a full path. |
|
| 1107 | + * |
|
| 1108 | + * The returned array must have the same keys as getCalendarObjects. The |
|
| 1109 | + * 'calendardata' object is required here though, while it's not required |
|
| 1110 | + * for getCalendarObjects. |
|
| 1111 | + * |
|
| 1112 | + * This method must return null if the object did not exist. |
|
| 1113 | + * |
|
| 1114 | + * @param mixed $calendarId |
|
| 1115 | + * @param string $objectUri |
|
| 1116 | + * @param int $calendarType |
|
| 1117 | + * @return array|null |
|
| 1118 | + */ |
|
| 1119 | + public function getCalendarObject($calendarId, $objectUri, int $calendarType = self::CALENDAR_TYPE_CALENDAR) { |
|
| 1120 | + $query = $this->db->getQueryBuilder(); |
|
| 1121 | + $query->select(['id', 'uri', 'lastmodified', 'etag', 'calendarid', 'size', 'calendardata', 'componenttype', 'classification', 'deleted_at']) |
|
| 1122 | + ->from('calendarobjects') |
|
| 1123 | + ->where($query->expr()->eq('calendarid', $query->createNamedParameter($calendarId))) |
|
| 1124 | + ->andWhere($query->expr()->eq('uri', $query->createNamedParameter($objectUri))) |
|
| 1125 | + ->andWhere($query->expr()->eq('calendartype', $query->createNamedParameter($calendarType))); |
|
| 1126 | + $stmt = $query->executeQuery(); |
|
| 1127 | + $row = $stmt->fetch(); |
|
| 1128 | + $stmt->closeCursor(); |
|
| 1129 | + |
|
| 1130 | + if (!$row) { |
|
| 1131 | + return null; |
|
| 1132 | + } |
|
| 1133 | + |
|
| 1134 | + return [ |
|
| 1135 | + 'id' => $row['id'], |
|
| 1136 | + 'uri' => $row['uri'], |
|
| 1137 | + 'lastmodified' => $row['lastmodified'], |
|
| 1138 | + 'etag' => '"' . $row['etag'] . '"', |
|
| 1139 | + 'calendarid' => $row['calendarid'], |
|
| 1140 | + 'size' => (int)$row['size'], |
|
| 1141 | + 'calendardata' => $this->readBlob($row['calendardata']), |
|
| 1142 | + 'component' => strtolower($row['componenttype']), |
|
| 1143 | + 'classification' => (int)$row['classification'], |
|
| 1144 | + '{' . \OCA\DAV\DAV\Sharing\Plugin::NS_NEXTCLOUD . '}deleted-at' => $row['deleted_at'] === null ? $row['deleted_at'] : (int) $row['deleted_at'], |
|
| 1145 | + ]; |
|
| 1146 | + } |
|
| 1147 | + |
|
| 1148 | + /** |
|
| 1149 | + * Returns a list of calendar objects. |
|
| 1150 | + * |
|
| 1151 | + * This method should work identical to getCalendarObject, but instead |
|
| 1152 | + * return all the calendar objects in the list as an array. |
|
| 1153 | + * |
|
| 1154 | + * If the backend supports this, it may allow for some speed-ups. |
|
| 1155 | + * |
|
| 1156 | + * @param mixed $calendarId |
|
| 1157 | + * @param string[] $uris |
|
| 1158 | + * @param int $calendarType |
|
| 1159 | + * @return array |
|
| 1160 | + */ |
|
| 1161 | + public function getMultipleCalendarObjects($calendarId, array $uris, $calendarType = self::CALENDAR_TYPE_CALENDAR):array { |
|
| 1162 | + if (empty($uris)) { |
|
| 1163 | + return []; |
|
| 1164 | + } |
|
| 1165 | + |
|
| 1166 | + $chunks = array_chunk($uris, 100); |
|
| 1167 | + $objects = []; |
|
| 1168 | + |
|
| 1169 | + $query = $this->db->getQueryBuilder(); |
|
| 1170 | + $query->select(['id', 'uri', 'lastmodified', 'etag', 'calendarid', 'size', 'calendardata', 'componenttype', 'classification']) |
|
| 1171 | + ->from('calendarobjects') |
|
| 1172 | + ->where($query->expr()->eq('calendarid', $query->createNamedParameter($calendarId))) |
|
| 1173 | + ->andWhere($query->expr()->in('uri', $query->createParameter('uri'))) |
|
| 1174 | + ->andWhere($query->expr()->eq('calendartype', $query->createNamedParameter($calendarType))) |
|
| 1175 | + ->andWhere($query->expr()->isNull('deleted_at')); |
|
| 1176 | + |
|
| 1177 | + foreach ($chunks as $uris) { |
|
| 1178 | + $query->setParameter('uri', $uris, IQueryBuilder::PARAM_STR_ARRAY); |
|
| 1179 | + $result = $query->executeQuery(); |
|
| 1180 | + |
|
| 1181 | + while ($row = $result->fetch()) { |
|
| 1182 | + $objects[] = [ |
|
| 1183 | + 'id' => $row['id'], |
|
| 1184 | + 'uri' => $row['uri'], |
|
| 1185 | + 'lastmodified' => $row['lastmodified'], |
|
| 1186 | + 'etag' => '"' . $row['etag'] . '"', |
|
| 1187 | + 'calendarid' => $row['calendarid'], |
|
| 1188 | + 'size' => (int)$row['size'], |
|
| 1189 | + 'calendardata' => $this->readBlob($row['calendardata']), |
|
| 1190 | + 'component' => strtolower($row['componenttype']), |
|
| 1191 | + 'classification' => (int)$row['classification'] |
|
| 1192 | + ]; |
|
| 1193 | + } |
|
| 1194 | + $result->closeCursor(); |
|
| 1195 | + } |
|
| 1196 | + |
|
| 1197 | + return $objects; |
|
| 1198 | + } |
|
| 1199 | + |
|
| 1200 | + /** |
|
| 1201 | + * Creates a new calendar object. |
|
| 1202 | + * |
|
| 1203 | + * The object uri is only the basename, or filename and not a full path. |
|
| 1204 | + * |
|
| 1205 | + * It is possible return an etag from this function, which will be used in |
|
| 1206 | + * the response to this PUT request. Note that the ETag must be surrounded |
|
| 1207 | + * by double-quotes. |
|
| 1208 | + * |
|
| 1209 | + * However, you should only really return this ETag if you don't mangle the |
|
| 1210 | + * calendar-data. If the result of a subsequent GET to this object is not |
|
| 1211 | + * the exact same as this request body, you should omit the ETag. |
|
| 1212 | + * |
|
| 1213 | + * @param mixed $calendarId |
|
| 1214 | + * @param string $objectUri |
|
| 1215 | + * @param string $calendarData |
|
| 1216 | + * @param int $calendarType |
|
| 1217 | + * @return string |
|
| 1218 | + */ |
|
| 1219 | + public function createCalendarObject($calendarId, $objectUri, $calendarData, $calendarType = self::CALENDAR_TYPE_CALENDAR) { |
|
| 1220 | + $extraData = $this->getDenormalizedData($calendarData); |
|
| 1221 | + |
|
| 1222 | + // Try to detect duplicates |
|
| 1223 | + $qb = $this->db->getQueryBuilder(); |
|
| 1224 | + $qb->select($qb->func()->count('*')) |
|
| 1225 | + ->from('calendarobjects') |
|
| 1226 | + ->where($qb->expr()->eq('calendarid', $qb->createNamedParameter($calendarId))) |
|
| 1227 | + ->andWhere($qb->expr()->eq('uid', $qb->createNamedParameter($extraData['uid']))) |
|
| 1228 | + ->andWhere($qb->expr()->eq('calendartype', $qb->createNamedParameter($calendarType))) |
|
| 1229 | + ->andWhere($qb->expr()->isNull('deleted_at')); |
|
| 1230 | + $result = $qb->executeQuery(); |
|
| 1231 | + $count = (int) $result->fetchOne(); |
|
| 1232 | + $result->closeCursor(); |
|
| 1233 | + |
|
| 1234 | + if ($count !== 0) { |
|
| 1235 | + throw new BadRequest('Calendar object with uid already exists in this calendar collection.'); |
|
| 1236 | + } |
|
| 1237 | + // For a more specific error message we also try to explicitly look up the UID but as a deleted entry |
|
| 1238 | + $qbDel = $this->db->getQueryBuilder(); |
|
| 1239 | + $qbDel->select($qb->func()->count('*')) |
|
| 1240 | + ->from('calendarobjects') |
|
| 1241 | + ->where($qbDel->expr()->eq('calendarid', $qbDel->createNamedParameter($calendarId))) |
|
| 1242 | + ->andWhere($qbDel->expr()->eq('uid', $qbDel->createNamedParameter($extraData['uid']))) |
|
| 1243 | + ->andWhere($qbDel->expr()->eq('calendartype', $qbDel->createNamedParameter($calendarType))) |
|
| 1244 | + ->andWhere($qbDel->expr()->isNotNull('deleted_at')); |
|
| 1245 | + $result = $qbDel->executeQuery(); |
|
| 1246 | + $count = (int) $result->fetchOne(); |
|
| 1247 | + $result->closeCursor(); |
|
| 1248 | + if ($count !== 0) { |
|
| 1249 | + throw new BadRequest('Deleted calendar object with uid already exists in this calendar collection.'); |
|
| 1250 | + } |
|
| 1251 | + |
|
| 1252 | + $query = $this->db->getQueryBuilder(); |
|
| 1253 | + $query->insert('calendarobjects') |
|
| 1254 | + ->values([ |
|
| 1255 | + 'calendarid' => $query->createNamedParameter($calendarId), |
|
| 1256 | + 'uri' => $query->createNamedParameter($objectUri), |
|
| 1257 | + 'calendardata' => $query->createNamedParameter($calendarData, IQueryBuilder::PARAM_LOB), |
|
| 1258 | + 'lastmodified' => $query->createNamedParameter(time()), |
|
| 1259 | + 'etag' => $query->createNamedParameter($extraData['etag']), |
|
| 1260 | + 'size' => $query->createNamedParameter($extraData['size']), |
|
| 1261 | + 'componenttype' => $query->createNamedParameter($extraData['componentType']), |
|
| 1262 | + 'firstoccurence' => $query->createNamedParameter($extraData['firstOccurence']), |
|
| 1263 | + 'lastoccurence' => $query->createNamedParameter($extraData['lastOccurence']), |
|
| 1264 | + 'classification' => $query->createNamedParameter($extraData['classification']), |
|
| 1265 | + 'uid' => $query->createNamedParameter($extraData['uid']), |
|
| 1266 | + 'calendartype' => $query->createNamedParameter($calendarType), |
|
| 1267 | + ]) |
|
| 1268 | + ->executeStatement(); |
|
| 1269 | + |
|
| 1270 | + $this->updateProperties($calendarId, $objectUri, $calendarData, $calendarType); |
|
| 1271 | + $this->addChange($calendarId, $objectUri, 1, $calendarType); |
|
| 1272 | + |
|
| 1273 | + $objectRow = $this->getCalendarObject($calendarId, $objectUri, $calendarType); |
|
| 1274 | + assert($objectRow !== null); |
|
| 1275 | + |
|
| 1276 | + if ($calendarType === self::CALENDAR_TYPE_CALENDAR) { |
|
| 1277 | + $calendarRow = $this->getCalendarById($calendarId); |
|
| 1278 | + $shares = $this->getShares($calendarId); |
|
| 1279 | + |
|
| 1280 | + $this->dispatcher->dispatchTyped(new CalendarObjectCreatedEvent($calendarId, $calendarRow, $shares, $objectRow)); |
|
| 1281 | + } else { |
|
| 1282 | + $subscriptionRow = $this->getSubscriptionById($calendarId); |
|
| 1283 | + |
|
| 1284 | + $this->dispatcher->dispatchTyped(new CachedCalendarObjectCreatedEvent($calendarId, $subscriptionRow, [], $objectRow)); |
|
| 1285 | + } |
|
| 1286 | + |
|
| 1287 | + return '"' . $extraData['etag'] . '"'; |
|
| 1288 | + } |
|
| 1289 | + |
|
| 1290 | + /** |
|
| 1291 | + * Updates an existing calendarobject, based on it's uri. |
|
| 1292 | + * |
|
| 1293 | + * The object uri is only the basename, or filename and not a full path. |
|
| 1294 | + * |
|
| 1295 | + * It is possible return an etag from this function, which will be used in |
|
| 1296 | + * the response to this PUT request. Note that the ETag must be surrounded |
|
| 1297 | + * by double-quotes. |
|
| 1298 | + * |
|
| 1299 | + * However, you should only really return this ETag if you don't mangle the |
|
| 1300 | + * calendar-data. If the result of a subsequent GET to this object is not |
|
| 1301 | + * the exact same as this request body, you should omit the ETag. |
|
| 1302 | + * |
|
| 1303 | + * @param mixed $calendarId |
|
| 1304 | + * @param string $objectUri |
|
| 1305 | + * @param string $calendarData |
|
| 1306 | + * @param int $calendarType |
|
| 1307 | + * @return string |
|
| 1308 | + */ |
|
| 1309 | + public function updateCalendarObject($calendarId, $objectUri, $calendarData, $calendarType = self::CALENDAR_TYPE_CALENDAR) { |
|
| 1310 | + $extraData = $this->getDenormalizedData($calendarData); |
|
| 1311 | + $query = $this->db->getQueryBuilder(); |
|
| 1312 | + $query->update('calendarobjects') |
|
| 1313 | + ->set('calendardata', $query->createNamedParameter($calendarData, IQueryBuilder::PARAM_LOB)) |
|
| 1314 | + ->set('lastmodified', $query->createNamedParameter(time())) |
|
| 1315 | + ->set('etag', $query->createNamedParameter($extraData['etag'])) |
|
| 1316 | + ->set('size', $query->createNamedParameter($extraData['size'])) |
|
| 1317 | + ->set('componenttype', $query->createNamedParameter($extraData['componentType'])) |
|
| 1318 | + ->set('firstoccurence', $query->createNamedParameter($extraData['firstOccurence'])) |
|
| 1319 | + ->set('lastoccurence', $query->createNamedParameter($extraData['lastOccurence'])) |
|
| 1320 | + ->set('classification', $query->createNamedParameter($extraData['classification'])) |
|
| 1321 | + ->set('uid', $query->createNamedParameter($extraData['uid'])) |
|
| 1322 | + ->where($query->expr()->eq('calendarid', $query->createNamedParameter($calendarId))) |
|
| 1323 | + ->andWhere($query->expr()->eq('uri', $query->createNamedParameter($objectUri))) |
|
| 1324 | + ->andWhere($query->expr()->eq('calendartype', $query->createNamedParameter($calendarType))) |
|
| 1325 | + ->executeStatement(); |
|
| 1326 | + |
|
| 1327 | + $this->updateProperties($calendarId, $objectUri, $calendarData, $calendarType); |
|
| 1328 | + $this->addChange($calendarId, $objectUri, 2, $calendarType); |
|
| 1329 | + |
|
| 1330 | + $objectRow = $this->getCalendarObject($calendarId, $objectUri, $calendarType); |
|
| 1331 | + if (is_array($objectRow)) { |
|
| 1332 | + if ($calendarType === self::CALENDAR_TYPE_CALENDAR) { |
|
| 1333 | + $calendarRow = $this->getCalendarById($calendarId); |
|
| 1334 | + $shares = $this->getShares($calendarId); |
|
| 1335 | + |
|
| 1336 | + $this->dispatcher->dispatchTyped(new CalendarObjectUpdatedEvent($calendarId, $calendarRow, $shares, $objectRow)); |
|
| 1337 | + } else { |
|
| 1338 | + $subscriptionRow = $this->getSubscriptionById($calendarId); |
|
| 1339 | + |
|
| 1340 | + $this->dispatcher->dispatchTyped(new CachedCalendarObjectUpdatedEvent($calendarId, $subscriptionRow, [], $objectRow)); |
|
| 1341 | + } |
|
| 1342 | + } |
|
| 1343 | + |
|
| 1344 | + return '"' . $extraData['etag'] . '"'; |
|
| 1345 | + } |
|
| 1346 | + |
|
| 1347 | + /** |
|
| 1348 | + * Moves a calendar object from calendar to calendar. |
|
| 1349 | + * |
|
| 1350 | + * @param int $sourceCalendarId |
|
| 1351 | + * @param int $targetCalendarId |
|
| 1352 | + * @param int $objectId |
|
| 1353 | + * @param string $oldPrincipalUri |
|
| 1354 | + * @param string $newPrincipalUri |
|
| 1355 | + * @param int $calendarType |
|
| 1356 | + * @return bool |
|
| 1357 | + * @throws Exception |
|
| 1358 | + */ |
|
| 1359 | + public function moveCalendarObject(int $sourceCalendarId, int $targetCalendarId, int $objectId, string $oldPrincipalUri, string $newPrincipalUri, int $calendarType = self::CALENDAR_TYPE_CALENDAR): bool { |
|
| 1360 | + $object = $this->getCalendarObjectById($oldPrincipalUri, $objectId); |
|
| 1361 | + if (empty($object)) { |
|
| 1362 | + return false; |
|
| 1363 | + } |
|
| 1364 | + |
|
| 1365 | + $query = $this->db->getQueryBuilder(); |
|
| 1366 | + $query->update('calendarobjects') |
|
| 1367 | + ->set('calendarid', $query->createNamedParameter($targetCalendarId, IQueryBuilder::PARAM_INT)) |
|
| 1368 | + ->where($query->expr()->eq('id', $query->createNamedParameter($objectId, IQueryBuilder::PARAM_INT), IQueryBuilder::PARAM_INT)) |
|
| 1369 | + ->andWhere($query->expr()->eq('calendartype', $query->createNamedParameter($calendarType, IQueryBuilder::PARAM_INT), IQueryBuilder::PARAM_INT)) |
|
| 1370 | + ->executeStatement(); |
|
| 1371 | + |
|
| 1372 | + $this->purgeProperties($sourceCalendarId, $objectId); |
|
| 1373 | + $this->updateProperties($targetCalendarId, $object['uri'], $object['calendardata'], $calendarType); |
|
| 1374 | + |
|
| 1375 | + $this->addChange($sourceCalendarId, $object['uri'], 1, $calendarType); |
|
| 1376 | + $this->addChange($targetCalendarId, $object['uri'], 3, $calendarType); |
|
| 1377 | + |
|
| 1378 | + $object = $this->getCalendarObjectById($newPrincipalUri, $objectId); |
|
| 1379 | + // Calendar Object wasn't found - possibly because it was deleted in the meantime by a different client |
|
| 1380 | + if (empty($object)) { |
|
| 1381 | + return false; |
|
| 1382 | + } |
|
| 1383 | + |
|
| 1384 | + $targetCalendarRow = $this->getCalendarById($targetCalendarId); |
|
| 1385 | + // the calendar this event is being moved to does not exist any longer |
|
| 1386 | + if (empty($targetCalendarRow)) { |
|
| 1387 | + return false; |
|
| 1388 | + } |
|
| 1389 | + |
|
| 1390 | + if ($calendarType === self::CALENDAR_TYPE_CALENDAR) { |
|
| 1391 | + $sourceShares = $this->getShares($sourceCalendarId); |
|
| 1392 | + $targetShares = $this->getShares($targetCalendarId); |
|
| 1393 | + $sourceCalendarRow = $this->getCalendarById($sourceCalendarId); |
|
| 1394 | + $this->dispatcher->dispatchTyped(new CalendarObjectMovedEvent($sourceCalendarId, $sourceCalendarRow, $targetCalendarId, $targetCalendarRow, $sourceShares, $targetShares, $object)); |
|
| 1395 | + } |
|
| 1396 | + return true; |
|
| 1397 | + } |
|
| 1398 | + |
|
| 1399 | + |
|
| 1400 | + /** |
|
| 1401 | + * @param int $calendarObjectId |
|
| 1402 | + * @param int $classification |
|
| 1403 | + */ |
|
| 1404 | + public function setClassification($calendarObjectId, $classification) { |
|
| 1405 | + if (!in_array($classification, [ |
|
| 1406 | + self::CLASSIFICATION_PUBLIC, self::CLASSIFICATION_PRIVATE, self::CLASSIFICATION_CONFIDENTIAL |
|
| 1407 | + ])) { |
|
| 1408 | + throw new \InvalidArgumentException(); |
|
| 1409 | + } |
|
| 1410 | + $query = $this->db->getQueryBuilder(); |
|
| 1411 | + $query->update('calendarobjects') |
|
| 1412 | + ->set('classification', $query->createNamedParameter($classification)) |
|
| 1413 | + ->where($query->expr()->eq('id', $query->createNamedParameter($calendarObjectId))) |
|
| 1414 | + ->executeStatement(); |
|
| 1415 | + } |
|
| 1416 | + |
|
| 1417 | + /** |
|
| 1418 | + * Deletes an existing calendar object. |
|
| 1419 | + * |
|
| 1420 | + * The object uri is only the basename, or filename and not a full path. |
|
| 1421 | + * |
|
| 1422 | + * @param mixed $calendarId |
|
| 1423 | + * @param string $objectUri |
|
| 1424 | + * @param int $calendarType |
|
| 1425 | + * @param bool $forceDeletePermanently |
|
| 1426 | + * @return void |
|
| 1427 | + */ |
|
| 1428 | + public function deleteCalendarObject($calendarId, $objectUri, $calendarType = self::CALENDAR_TYPE_CALENDAR, bool $forceDeletePermanently = false) { |
|
| 1429 | + $data = $this->getCalendarObject($calendarId, $objectUri, $calendarType); |
|
| 1430 | + |
|
| 1431 | + if ($data === null) { |
|
| 1432 | + // Nothing to delete |
|
| 1433 | + return; |
|
| 1434 | + } |
|
| 1435 | + |
|
| 1436 | + if ($forceDeletePermanently || $this->config->getAppValue(Application::APP_ID, RetentionService::RETENTION_CONFIG_KEY) === '0') { |
|
| 1437 | + $stmt = $this->db->prepare('DELETE FROM `*PREFIX*calendarobjects` WHERE `calendarid` = ? AND `uri` = ? AND `calendartype` = ?'); |
|
| 1438 | + $stmt->execute([$calendarId, $objectUri, $calendarType]); |
|
| 1439 | + |
|
| 1440 | + $this->purgeProperties($calendarId, $data['id']); |
|
| 1441 | + |
|
| 1442 | + if ($calendarType === self::CALENDAR_TYPE_CALENDAR) { |
|
| 1443 | + $calendarRow = $this->getCalendarById($calendarId); |
|
| 1444 | + $shares = $this->getShares($calendarId); |
|
| 1445 | + |
|
| 1446 | + $this->dispatcher->dispatchTyped(new CalendarObjectDeletedEvent($calendarId, $calendarRow, $shares, $data)); |
|
| 1447 | + } else { |
|
| 1448 | + $subscriptionRow = $this->getSubscriptionById($calendarId); |
|
| 1449 | + |
|
| 1450 | + $this->dispatcher->dispatchTyped(new CachedCalendarObjectDeletedEvent($calendarId, $subscriptionRow, [], $data)); |
|
| 1451 | + } |
|
| 1452 | + } else { |
|
| 1453 | + $pathInfo = pathinfo($data['uri']); |
|
| 1454 | + if (!empty($pathInfo['extension'])) { |
|
| 1455 | + // Append a suffix to "free" the old URI for recreation |
|
| 1456 | + $newUri = sprintf( |
|
| 1457 | + "%s-deleted.%s", |
|
| 1458 | + $pathInfo['filename'], |
|
| 1459 | + $pathInfo['extension'] |
|
| 1460 | + ); |
|
| 1461 | + } else { |
|
| 1462 | + $newUri = sprintf( |
|
| 1463 | + "%s-deleted", |
|
| 1464 | + $pathInfo['filename'] |
|
| 1465 | + ); |
|
| 1466 | + } |
|
| 1467 | + |
|
| 1468 | + // Try to detect conflicts before the DB does |
|
| 1469 | + // As unlikely as it seems, this can happen when the user imports, then deletes, imports and deletes again |
|
| 1470 | + $newObject = $this->getCalendarObject($calendarId, $newUri, $calendarType); |
|
| 1471 | + if ($newObject !== null) { |
|
| 1472 | + throw new Forbidden("A calendar object with URI $newUri already exists in calendar $calendarId, therefore this object can't be moved into the trashbin"); |
|
| 1473 | + } |
|
| 1474 | + |
|
| 1475 | + $qb = $this->db->getQueryBuilder(); |
|
| 1476 | + $markObjectDeletedQuery = $qb->update('calendarobjects') |
|
| 1477 | + ->set('deleted_at', $qb->createNamedParameter(time(), IQueryBuilder::PARAM_INT)) |
|
| 1478 | + ->set('uri', $qb->createNamedParameter($newUri)) |
|
| 1479 | + ->where( |
|
| 1480 | + $qb->expr()->eq('calendarid', $qb->createNamedParameter($calendarId)), |
|
| 1481 | + $qb->expr()->eq('calendartype', $qb->createNamedParameter($calendarType, IQueryBuilder::PARAM_INT), IQueryBuilder::PARAM_INT), |
|
| 1482 | + $qb->expr()->eq('uri', $qb->createNamedParameter($objectUri)) |
|
| 1483 | + ); |
|
| 1484 | + $markObjectDeletedQuery->executeStatement(); |
|
| 1485 | + |
|
| 1486 | + $calendarData = $this->getCalendarById($calendarId); |
|
| 1487 | + if ($calendarData !== null) { |
|
| 1488 | + $this->dispatcher->dispatchTyped( |
|
| 1489 | + new CalendarObjectMovedToTrashEvent( |
|
| 1490 | + $calendarId, |
|
| 1491 | + $calendarData, |
|
| 1492 | + $this->getShares($calendarId), |
|
| 1493 | + $data |
|
| 1494 | + ) |
|
| 1495 | + ); |
|
| 1496 | + } |
|
| 1497 | + } |
|
| 1498 | + |
|
| 1499 | + $this->addChange($calendarId, $objectUri, 3, $calendarType); |
|
| 1500 | + } |
|
| 1501 | + |
|
| 1502 | + /** |
|
| 1503 | + * @param mixed $objectData |
|
| 1504 | + * |
|
| 1505 | + * @throws Forbidden |
|
| 1506 | + */ |
|
| 1507 | + public function restoreCalendarObject(array $objectData): void { |
|
| 1508 | + $id = (int) $objectData['id']; |
|
| 1509 | + $restoreUri = str_replace("-deleted.ics", ".ics", $objectData['uri']); |
|
| 1510 | + $targetObject = $this->getCalendarObject( |
|
| 1511 | + $objectData['calendarid'], |
|
| 1512 | + $restoreUri |
|
| 1513 | + ); |
|
| 1514 | + if ($targetObject !== null) { |
|
| 1515 | + throw new Forbidden("Can not restore calendar $id because a calendar object with the URI $restoreUri already exists"); |
|
| 1516 | + } |
|
| 1517 | + |
|
| 1518 | + $qb = $this->db->getQueryBuilder(); |
|
| 1519 | + $update = $qb->update('calendarobjects') |
|
| 1520 | + ->set('uri', $qb->createNamedParameter($restoreUri)) |
|
| 1521 | + ->set('deleted_at', $qb->createNamedParameter(null)) |
|
| 1522 | + ->where($qb->expr()->eq('id', $qb->createNamedParameter($id, IQueryBuilder::PARAM_INT), IQueryBuilder::PARAM_INT)); |
|
| 1523 | + $update->executeStatement(); |
|
| 1524 | + |
|
| 1525 | + // Make sure this change is tracked in the changes table |
|
| 1526 | + $qb2 = $this->db->getQueryBuilder(); |
|
| 1527 | + $selectObject = $qb2->select('calendardata', 'uri', 'calendarid', 'calendartype') |
|
| 1528 | + ->selectAlias('componenttype', 'component') |
|
| 1529 | + ->from('calendarobjects') |
|
| 1530 | + ->where($qb2->expr()->eq('id', $qb2->createNamedParameter($id, IQueryBuilder::PARAM_INT), IQueryBuilder::PARAM_INT)); |
|
| 1531 | + $result = $selectObject->executeQuery(); |
|
| 1532 | + $row = $result->fetch(); |
|
| 1533 | + $result->closeCursor(); |
|
| 1534 | + if ($row === false) { |
|
| 1535 | + // Welp, this should possibly not have happened, but let's ignore |
|
| 1536 | + return; |
|
| 1537 | + } |
|
| 1538 | + $this->addChange($row['calendarid'], $row['uri'], 1, (int) $row['calendartype']); |
|
| 1539 | + |
|
| 1540 | + $calendarRow = $this->getCalendarById((int) $row['calendarid']); |
|
| 1541 | + if ($calendarRow === null) { |
|
| 1542 | + throw new RuntimeException('Calendar object data that was just written can\'t be read back. Check your database configuration.'); |
|
| 1543 | + } |
|
| 1544 | + $this->dispatcher->dispatchTyped( |
|
| 1545 | + new CalendarObjectRestoredEvent( |
|
| 1546 | + (int) $objectData['calendarid'], |
|
| 1547 | + $calendarRow, |
|
| 1548 | + $this->getShares((int) $row['calendarid']), |
|
| 1549 | + $row |
|
| 1550 | + ) |
|
| 1551 | + ); |
|
| 1552 | + } |
|
| 1553 | + |
|
| 1554 | + /** |
|
| 1555 | + * Performs a calendar-query on the contents of this calendar. |
|
| 1556 | + * |
|
| 1557 | + * The calendar-query is defined in RFC4791 : CalDAV. Using the |
|
| 1558 | + * calendar-query it is possible for a client to request a specific set of |
|
| 1559 | + * object, based on contents of iCalendar properties, date-ranges and |
|
| 1560 | + * iCalendar component types (VTODO, VEVENT). |
|
| 1561 | + * |
|
| 1562 | + * This method should just return a list of (relative) urls that match this |
|
| 1563 | + * query. |
|
| 1564 | + * |
|
| 1565 | + * The list of filters are specified as an array. The exact array is |
|
| 1566 | + * documented by Sabre\CalDAV\CalendarQueryParser. |
|
| 1567 | + * |
|
| 1568 | + * Note that it is extremely likely that getCalendarObject for every path |
|
| 1569 | + * returned from this method will be called almost immediately after. You |
|
| 1570 | + * may want to anticipate this to speed up these requests. |
|
| 1571 | + * |
|
| 1572 | + * This method provides a default implementation, which parses *all* the |
|
| 1573 | + * iCalendar objects in the specified calendar. |
|
| 1574 | + * |
|
| 1575 | + * This default may well be good enough for personal use, and calendars |
|
| 1576 | + * that aren't very large. But if you anticipate high usage, big calendars |
|
| 1577 | + * or high loads, you are strongly advised to optimize certain paths. |
|
| 1578 | + * |
|
| 1579 | + * The best way to do so is override this method and to optimize |
|
| 1580 | + * specifically for 'common filters'. |
|
| 1581 | + * |
|
| 1582 | + * Requests that are extremely common are: |
|
| 1583 | + * * requests for just VEVENTS |
|
| 1584 | + * * requests for just VTODO |
|
| 1585 | + * * requests with a time-range-filter on either VEVENT or VTODO. |
|
| 1586 | + * |
|
| 1587 | + * ..and combinations of these requests. It may not be worth it to try to |
|
| 1588 | + * handle every possible situation and just rely on the (relatively |
|
| 1589 | + * easy to use) CalendarQueryValidator to handle the rest. |
|
| 1590 | + * |
|
| 1591 | + * Note that especially time-range-filters may be difficult to parse. A |
|
| 1592 | + * time-range filter specified on a VEVENT must for instance also handle |
|
| 1593 | + * recurrence rules correctly. |
|
| 1594 | + * A good example of how to interpret all these filters can also simply |
|
| 1595 | + * be found in Sabre\CalDAV\CalendarQueryFilter. This class is as correct |
|
| 1596 | + * as possible, so it gives you a good idea on what type of stuff you need |
|
| 1597 | + * to think of. |
|
| 1598 | + * |
|
| 1599 | + * @param mixed $calendarId |
|
| 1600 | + * @param array $filters |
|
| 1601 | + * @param int $calendarType |
|
| 1602 | + * @return array |
|
| 1603 | + */ |
|
| 1604 | + public function calendarQuery($calendarId, array $filters, $calendarType = self::CALENDAR_TYPE_CALENDAR):array { |
|
| 1605 | + $componentType = null; |
|
| 1606 | + $requirePostFilter = true; |
|
| 1607 | + $timeRange = null; |
|
| 1608 | + |
|
| 1609 | + // if no filters were specified, we don't need to filter after a query |
|
| 1610 | + if (!$filters['prop-filters'] && !$filters['comp-filters']) { |
|
| 1611 | + $requirePostFilter = false; |
|
| 1612 | + } |
|
| 1613 | + |
|
| 1614 | + // Figuring out if there's a component filter |
|
| 1615 | + if (count($filters['comp-filters']) > 0 && !$filters['comp-filters'][0]['is-not-defined']) { |
|
| 1616 | + $componentType = $filters['comp-filters'][0]['name']; |
|
| 1617 | + |
|
| 1618 | + // Checking if we need post-filters |
|
| 1619 | + if (!$filters['prop-filters'] && !$filters['comp-filters'][0]['comp-filters'] && !$filters['comp-filters'][0]['time-range'] && !$filters['comp-filters'][0]['prop-filters']) { |
|
| 1620 | + $requirePostFilter = false; |
|
| 1621 | + } |
|
| 1622 | + // There was a time-range filter |
|
| 1623 | + if ($componentType === 'VEVENT' && isset($filters['comp-filters'][0]['time-range']) && is_array($filters['comp-filters'][0]['time-range'])) { |
|
| 1624 | + $timeRange = $filters['comp-filters'][0]['time-range']; |
|
| 1625 | + |
|
| 1626 | + // If start time OR the end time is not specified, we can do a |
|
| 1627 | + // 100% accurate mysql query. |
|
| 1628 | + if (!$filters['prop-filters'] && !$filters['comp-filters'][0]['comp-filters'] && !$filters['comp-filters'][0]['prop-filters'] && (!$timeRange['start'] || !$timeRange['end'])) { |
|
| 1629 | + $requirePostFilter = false; |
|
| 1630 | + } |
|
| 1631 | + } |
|
| 1632 | + } |
|
| 1633 | + $columns = ['uri']; |
|
| 1634 | + if ($requirePostFilter) { |
|
| 1635 | + $columns = ['uri', 'calendardata']; |
|
| 1636 | + } |
|
| 1637 | + $query = $this->db->getQueryBuilder(); |
|
| 1638 | + $query->select($columns) |
|
| 1639 | + ->from('calendarobjects') |
|
| 1640 | + ->where($query->expr()->eq('calendarid', $query->createNamedParameter($calendarId))) |
|
| 1641 | + ->andWhere($query->expr()->eq('calendartype', $query->createNamedParameter($calendarType))) |
|
| 1642 | + ->andWhere($query->expr()->isNull('deleted_at')); |
|
| 1643 | + |
|
| 1644 | + if ($componentType) { |
|
| 1645 | + $query->andWhere($query->expr()->eq('componenttype', $query->createNamedParameter($componentType))); |
|
| 1646 | + } |
|
| 1647 | + |
|
| 1648 | + if ($timeRange && $timeRange['start']) { |
|
| 1649 | + $query->andWhere($query->expr()->gt('lastoccurence', $query->createNamedParameter($timeRange['start']->getTimeStamp()))); |
|
| 1650 | + } |
|
| 1651 | + if ($timeRange && $timeRange['end']) { |
|
| 1652 | + $query->andWhere($query->expr()->lt('firstoccurence', $query->createNamedParameter($timeRange['end']->getTimeStamp()))); |
|
| 1653 | + } |
|
| 1654 | + |
|
| 1655 | + $stmt = $query->executeQuery(); |
|
| 1656 | + |
|
| 1657 | + $result = []; |
|
| 1658 | + while ($row = $stmt->fetch()) { |
|
| 1659 | + if ($requirePostFilter) { |
|
| 1660 | + // validateFilterForObject will parse the calendar data |
|
| 1661 | + // catch parsing errors |
|
| 1662 | + try { |
|
| 1663 | + $matches = $this->validateFilterForObject($row, $filters); |
|
| 1664 | + } catch (ParseException $ex) { |
|
| 1665 | + $this->logger->error('Caught parsing exception for calendar data. This usually indicates invalid calendar data. calendar-id:'.$calendarId.' uri:'.$row['uri'], [ |
|
| 1666 | + 'app' => 'dav', |
|
| 1667 | + 'exception' => $ex, |
|
| 1668 | + ]); |
|
| 1669 | + continue; |
|
| 1670 | + } catch (InvalidDataException $ex) { |
|
| 1671 | + $this->logger->error('Caught invalid data exception for calendar data. This usually indicates invalid calendar data. calendar-id:'.$calendarId.' uri:'.$row['uri'], [ |
|
| 1672 | + 'app' => 'dav', |
|
| 1673 | + 'exception' => $ex, |
|
| 1674 | + ]); |
|
| 1675 | + continue; |
|
| 1676 | + } |
|
| 1677 | + |
|
| 1678 | + if (!$matches) { |
|
| 1679 | + continue; |
|
| 1680 | + } |
|
| 1681 | + } |
|
| 1682 | + $result[] = $row['uri']; |
|
| 1683 | + } |
|
| 1684 | + |
|
| 1685 | + return $result; |
|
| 1686 | + } |
|
| 1687 | + |
|
| 1688 | + /** |
|
| 1689 | + * custom Nextcloud search extension for CalDAV |
|
| 1690 | + * |
|
| 1691 | + * TODO - this should optionally cover cached calendar objects as well |
|
| 1692 | + * |
|
| 1693 | + * @param string $principalUri |
|
| 1694 | + * @param array $filters |
|
| 1695 | + * @param integer|null $limit |
|
| 1696 | + * @param integer|null $offset |
|
| 1697 | + * @return array |
|
| 1698 | + */ |
|
| 1699 | + public function calendarSearch($principalUri, array $filters, $limit = null, $offset = null) { |
|
| 1700 | + $calendars = $this->getCalendarsForUser($principalUri); |
|
| 1701 | + $ownCalendars = []; |
|
| 1702 | + $sharedCalendars = []; |
|
| 1703 | + |
|
| 1704 | + $uriMapper = []; |
|
| 1705 | + |
|
| 1706 | + foreach ($calendars as $calendar) { |
|
| 1707 | + if ($calendar['{http://owncloud.org/ns}owner-principal'] === $principalUri) { |
|
| 1708 | + $ownCalendars[] = $calendar['id']; |
|
| 1709 | + } else { |
|
| 1710 | + $sharedCalendars[] = $calendar['id']; |
|
| 1711 | + } |
|
| 1712 | + $uriMapper[$calendar['id']] = $calendar['uri']; |
|
| 1713 | + } |
|
| 1714 | + if (count($ownCalendars) === 0 && count($sharedCalendars) === 0) { |
|
| 1715 | + return []; |
|
| 1716 | + } |
|
| 1717 | + |
|
| 1718 | + $query = $this->db->getQueryBuilder(); |
|
| 1719 | + // Calendar id expressions |
|
| 1720 | + $calendarExpressions = []; |
|
| 1721 | + foreach ($ownCalendars as $id) { |
|
| 1722 | + $calendarExpressions[] = $query->expr()->andX( |
|
| 1723 | + $query->expr()->eq('c.calendarid', |
|
| 1724 | + $query->createNamedParameter($id)), |
|
| 1725 | + $query->expr()->eq('c.calendartype', |
|
| 1726 | + $query->createNamedParameter(self::CALENDAR_TYPE_CALENDAR))); |
|
| 1727 | + } |
|
| 1728 | + foreach ($sharedCalendars as $id) { |
|
| 1729 | + $calendarExpressions[] = $query->expr()->andX( |
|
| 1730 | + $query->expr()->eq('c.calendarid', |
|
| 1731 | + $query->createNamedParameter($id)), |
|
| 1732 | + $query->expr()->eq('c.classification', |
|
| 1733 | + $query->createNamedParameter(self::CLASSIFICATION_PUBLIC)), |
|
| 1734 | + $query->expr()->eq('c.calendartype', |
|
| 1735 | + $query->createNamedParameter(self::CALENDAR_TYPE_CALENDAR))); |
|
| 1736 | + } |
|
| 1737 | + |
|
| 1738 | + if (count($calendarExpressions) === 1) { |
|
| 1739 | + $calExpr = $calendarExpressions[0]; |
|
| 1740 | + } else { |
|
| 1741 | + $calExpr = call_user_func_array([$query->expr(), 'orX'], $calendarExpressions); |
|
| 1742 | + } |
|
| 1743 | + |
|
| 1744 | + // Component expressions |
|
| 1745 | + $compExpressions = []; |
|
| 1746 | + foreach ($filters['comps'] as $comp) { |
|
| 1747 | + $compExpressions[] = $query->expr() |
|
| 1748 | + ->eq('c.componenttype', $query->createNamedParameter($comp)); |
|
| 1749 | + } |
|
| 1750 | + |
|
| 1751 | + if (count($compExpressions) === 1) { |
|
| 1752 | + $compExpr = $compExpressions[0]; |
|
| 1753 | + } else { |
|
| 1754 | + $compExpr = call_user_func_array([$query->expr(), 'orX'], $compExpressions); |
|
| 1755 | + } |
|
| 1756 | + |
|
| 1757 | + if (!isset($filters['props'])) { |
|
| 1758 | + $filters['props'] = []; |
|
| 1759 | + } |
|
| 1760 | + if (!isset($filters['params'])) { |
|
| 1761 | + $filters['params'] = []; |
|
| 1762 | + } |
|
| 1763 | + |
|
| 1764 | + $propParamExpressions = []; |
|
| 1765 | + foreach ($filters['props'] as $prop) { |
|
| 1766 | + $propParamExpressions[] = $query->expr()->andX( |
|
| 1767 | + $query->expr()->eq('i.name', $query->createNamedParameter($prop)), |
|
| 1768 | + $query->expr()->isNull('i.parameter') |
|
| 1769 | + ); |
|
| 1770 | + } |
|
| 1771 | + foreach ($filters['params'] as $param) { |
|
| 1772 | + $propParamExpressions[] = $query->expr()->andX( |
|
| 1773 | + $query->expr()->eq('i.name', $query->createNamedParameter($param['property'])), |
|
| 1774 | + $query->expr()->eq('i.parameter', $query->createNamedParameter($param['parameter'])) |
|
| 1775 | + ); |
|
| 1776 | + } |
|
| 1777 | + |
|
| 1778 | + if (count($propParamExpressions) === 1) { |
|
| 1779 | + $propParamExpr = $propParamExpressions[0]; |
|
| 1780 | + } else { |
|
| 1781 | + $propParamExpr = call_user_func_array([$query->expr(), 'orX'], $propParamExpressions); |
|
| 1782 | + } |
|
| 1783 | + |
|
| 1784 | + $query->select(['c.calendarid', 'c.uri']) |
|
| 1785 | + ->from($this->dbObjectPropertiesTable, 'i') |
|
| 1786 | + ->join('i', 'calendarobjects', 'c', $query->expr()->eq('i.objectid', 'c.id')) |
|
| 1787 | + ->where($calExpr) |
|
| 1788 | + ->andWhere($compExpr) |
|
| 1789 | + ->andWhere($propParamExpr) |
|
| 1790 | + ->andWhere($query->expr()->iLike('i.value', |
|
| 1791 | + $query->createNamedParameter('%'.$this->db->escapeLikeParameter($filters['search-term']).'%'))) |
|
| 1792 | + ->andWhere($query->expr()->isNull('deleted_at')); |
|
| 1793 | + |
|
| 1794 | + if ($offset) { |
|
| 1795 | + $query->setFirstResult($offset); |
|
| 1796 | + } |
|
| 1797 | + if ($limit) { |
|
| 1798 | + $query->setMaxResults($limit); |
|
| 1799 | + } |
|
| 1800 | + |
|
| 1801 | + $stmt = $query->executeQuery(); |
|
| 1802 | + |
|
| 1803 | + $result = []; |
|
| 1804 | + while ($row = $stmt->fetch()) { |
|
| 1805 | + $path = $uriMapper[$row['calendarid']] . '/' . $row['uri']; |
|
| 1806 | + if (!in_array($path, $result)) { |
|
| 1807 | + $result[] = $path; |
|
| 1808 | + } |
|
| 1809 | + } |
|
| 1810 | + |
|
| 1811 | + return $result; |
|
| 1812 | + } |
|
| 1813 | + |
|
| 1814 | + /** |
|
| 1815 | + * used for Nextcloud's calendar API |
|
| 1816 | + * |
|
| 1817 | + * @param array $calendarInfo |
|
| 1818 | + * @param string $pattern |
|
| 1819 | + * @param array $searchProperties |
|
| 1820 | + * @param array $options |
|
| 1821 | + * @param integer|null $limit |
|
| 1822 | + * @param integer|null $offset |
|
| 1823 | + * |
|
| 1824 | + * @return array |
|
| 1825 | + */ |
|
| 1826 | + public function search(array $calendarInfo, $pattern, array $searchProperties, |
|
| 1827 | + array $options, $limit, $offset) { |
|
| 1828 | + $outerQuery = $this->db->getQueryBuilder(); |
|
| 1829 | + $innerQuery = $this->db->getQueryBuilder(); |
|
| 1830 | + |
|
| 1831 | + $innerQuery->selectDistinct('op.objectid') |
|
| 1832 | + ->from($this->dbObjectPropertiesTable, 'op') |
|
| 1833 | + ->andWhere($innerQuery->expr()->eq('op.calendarid', |
|
| 1834 | + $outerQuery->createNamedParameter($calendarInfo['id']))) |
|
| 1835 | + ->andWhere($innerQuery->expr()->eq('op.calendartype', |
|
| 1836 | + $outerQuery->createNamedParameter(self::CALENDAR_TYPE_CALENDAR))); |
|
| 1837 | + |
|
| 1838 | + // only return public items for shared calendars for now |
|
| 1839 | + if (isset($calendarInfo['{http://owncloud.org/ns}owner-principal']) === false || $calendarInfo['principaluri'] !== $calendarInfo['{http://owncloud.org/ns}owner-principal']) { |
|
| 1840 | + $innerQuery->andWhere($innerQuery->expr()->eq('c.classification', |
|
| 1841 | + $outerQuery->createNamedParameter(self::CLASSIFICATION_PUBLIC))); |
|
| 1842 | + } |
|
| 1843 | + |
|
| 1844 | + if (!empty($searchProperties)) { |
|
| 1845 | + $or = $innerQuery->expr()->orX(); |
|
| 1846 | + foreach ($searchProperties as $searchProperty) { |
|
| 1847 | + $or->add($innerQuery->expr()->eq('op.name', |
|
| 1848 | + $outerQuery->createNamedParameter($searchProperty))); |
|
| 1849 | + } |
|
| 1850 | + $innerQuery->andWhere($or); |
|
| 1851 | + } |
|
| 1852 | + |
|
| 1853 | + if ($pattern !== '') { |
|
| 1854 | + $innerQuery->andWhere($innerQuery->expr()->iLike('op.value', |
|
| 1855 | + $outerQuery->createNamedParameter('%' . |
|
| 1856 | + $this->db->escapeLikeParameter($pattern) . '%'))); |
|
| 1857 | + } |
|
| 1858 | + |
|
| 1859 | + $outerQuery->select('c.id', 'c.calendardata', 'c.componenttype', 'c.uid', 'c.uri') |
|
| 1860 | + ->from('calendarobjects', 'c') |
|
| 1861 | + ->where($outerQuery->expr()->isNull('deleted_at')); |
|
| 1862 | + |
|
| 1863 | + if (isset($options['timerange'])) { |
|
| 1864 | + if (isset($options['timerange']['start']) && $options['timerange']['start'] instanceof DateTimeInterface) { |
|
| 1865 | + $outerQuery->andWhere($outerQuery->expr()->gt('lastoccurence', |
|
| 1866 | + $outerQuery->createNamedParameter($options['timerange']['start']->getTimeStamp()))); |
|
| 1867 | + } |
|
| 1868 | + if (isset($options['timerange']['end']) && $options['timerange']['end'] instanceof DateTimeInterface) { |
|
| 1869 | + $outerQuery->andWhere($outerQuery->expr()->lt('firstoccurence', |
|
| 1870 | + $outerQuery->createNamedParameter($options['timerange']['end']->getTimeStamp()))); |
|
| 1871 | + } |
|
| 1872 | + } |
|
| 1873 | + |
|
| 1874 | + if(isset($options['uid'])) { |
|
| 1875 | + $outerQuery->andWhere($outerQuery->expr()->eq('uid', $outerQuery->createNamedParameter($options['uid']))); |
|
| 1876 | + } |
|
| 1877 | + |
|
| 1878 | + if (!empty($options['types'])) { |
|
| 1879 | + $or = $outerQuery->expr()->orX(); |
|
| 1880 | + foreach ($options['types'] as $type) { |
|
| 1881 | + $or->add($outerQuery->expr()->eq('componenttype', |
|
| 1882 | + $outerQuery->createNamedParameter($type))); |
|
| 1883 | + } |
|
| 1884 | + $outerQuery->andWhere($or); |
|
| 1885 | + } |
|
| 1886 | + |
|
| 1887 | + $outerQuery->andWhere($outerQuery->expr()->in('c.id', $outerQuery->createFunction($innerQuery->getSQL()))); |
|
| 1888 | + |
|
| 1889 | + if ($offset) { |
|
| 1890 | + $outerQuery->setFirstResult($offset); |
|
| 1891 | + } |
|
| 1892 | + if ($limit) { |
|
| 1893 | + $outerQuery->setMaxResults($limit); |
|
| 1894 | + } |
|
| 1895 | + |
|
| 1896 | + $result = $outerQuery->executeQuery(); |
|
| 1897 | + $calendarObjects = array_filter($result->fetchAll(), function (array $row) use ($options) { |
|
| 1898 | + $start = $options['timerange']['start'] ?? null; |
|
| 1899 | + $end = $options['timerange']['end'] ?? null; |
|
| 1900 | + |
|
| 1901 | + if ($start === null || !($start instanceof DateTimeInterface) || $end === null || !($end instanceof DateTimeInterface)) { |
|
| 1902 | + // No filter required |
|
| 1903 | + return true; |
|
| 1904 | + } |
|
| 1905 | + |
|
| 1906 | + $isValid = $this->validateFilterForObject($row, [ |
|
| 1907 | + 'name' => 'VCALENDAR', |
|
| 1908 | + 'comp-filters' => [ |
|
| 1909 | + [ |
|
| 1910 | + 'name' => 'VEVENT', |
|
| 1911 | + 'comp-filters' => [], |
|
| 1912 | + 'prop-filters' => [], |
|
| 1913 | + 'is-not-defined' => false, |
|
| 1914 | + 'time-range' => [ |
|
| 1915 | + 'start' => $start, |
|
| 1916 | + 'end' => $end, |
|
| 1917 | + ], |
|
| 1918 | + ], |
|
| 1919 | + ], |
|
| 1920 | + 'prop-filters' => [], |
|
| 1921 | + 'is-not-defined' => false, |
|
| 1922 | + 'time-range' => null, |
|
| 1923 | + ]); |
|
| 1924 | + if (is_resource($row['calendardata'])) { |
|
| 1925 | + // Put the stream back to the beginning so it can be read another time |
|
| 1926 | + rewind($row['calendardata']); |
|
| 1927 | + } |
|
| 1928 | + return $isValid; |
|
| 1929 | + }); |
|
| 1930 | + $result->closeCursor(); |
|
| 1931 | + |
|
| 1932 | + return array_map(function ($o) { |
|
| 1933 | + $calendarData = Reader::read($o['calendardata']); |
|
| 1934 | + $comps = $calendarData->getComponents(); |
|
| 1935 | + $objects = []; |
|
| 1936 | + $timezones = []; |
|
| 1937 | + foreach ($comps as $comp) { |
|
| 1938 | + if ($comp instanceof VTimeZone) { |
|
| 1939 | + $timezones[] = $comp; |
|
| 1940 | + } else { |
|
| 1941 | + $objects[] = $comp; |
|
| 1942 | + } |
|
| 1943 | + } |
|
| 1944 | + |
|
| 1945 | + return [ |
|
| 1946 | + 'id' => $o['id'], |
|
| 1947 | + 'type' => $o['componenttype'], |
|
| 1948 | + 'uid' => $o['uid'], |
|
| 1949 | + 'uri' => $o['uri'], |
|
| 1950 | + 'objects' => array_map(function ($c) { |
|
| 1951 | + return $this->transformSearchData($c); |
|
| 1952 | + }, $objects), |
|
| 1953 | + 'timezones' => array_map(function ($c) { |
|
| 1954 | + return $this->transformSearchData($c); |
|
| 1955 | + }, $timezones), |
|
| 1956 | + ]; |
|
| 1957 | + }, $calendarObjects); |
|
| 1958 | + } |
|
| 1959 | + |
|
| 1960 | + /** |
|
| 1961 | + * @param Component $comp |
|
| 1962 | + * @return array |
|
| 1963 | + */ |
|
| 1964 | + private function transformSearchData(Component $comp) { |
|
| 1965 | + $data = []; |
|
| 1966 | + /** @var Component[] $subComponents */ |
|
| 1967 | + $subComponents = $comp->getComponents(); |
|
| 1968 | + /** @var Property[] $properties */ |
|
| 1969 | + $properties = array_filter($comp->children(), function ($c) { |
|
| 1970 | + return $c instanceof Property; |
|
| 1971 | + }); |
|
| 1972 | + $validationRules = $comp->getValidationRules(); |
|
| 1973 | + |
|
| 1974 | + foreach ($subComponents as $subComponent) { |
|
| 1975 | + $name = $subComponent->name; |
|
| 1976 | + if (!isset($data[$name])) { |
|
| 1977 | + $data[$name] = []; |
|
| 1978 | + } |
|
| 1979 | + $data[$name][] = $this->transformSearchData($subComponent); |
|
| 1980 | + } |
|
| 1981 | + |
|
| 1982 | + foreach ($properties as $property) { |
|
| 1983 | + $name = $property->name; |
|
| 1984 | + if (!isset($validationRules[$name])) { |
|
| 1985 | + $validationRules[$name] = '*'; |
|
| 1986 | + } |
|
| 1987 | + |
|
| 1988 | + $rule = $validationRules[$property->name]; |
|
| 1989 | + if ($rule === '+' || $rule === '*') { // multiple |
|
| 1990 | + if (!isset($data[$name])) { |
|
| 1991 | + $data[$name] = []; |
|
| 1992 | + } |
|
| 1993 | + |
|
| 1994 | + $data[$name][] = $this->transformSearchProperty($property); |
|
| 1995 | + } else { // once |
|
| 1996 | + $data[$name] = $this->transformSearchProperty($property); |
|
| 1997 | + } |
|
| 1998 | + } |
|
| 1999 | + |
|
| 2000 | + return $data; |
|
| 2001 | + } |
|
| 2002 | + |
|
| 2003 | + /** |
|
| 2004 | + * @param Property $prop |
|
| 2005 | + * @return array |
|
| 2006 | + */ |
|
| 2007 | + private function transformSearchProperty(Property $prop) { |
|
| 2008 | + // No need to check Date, as it extends DateTime |
|
| 2009 | + if ($prop instanceof Property\ICalendar\DateTime) { |
|
| 2010 | + $value = $prop->getDateTime(); |
|
| 2011 | + } else { |
|
| 2012 | + $value = $prop->getValue(); |
|
| 2013 | + } |
|
| 2014 | + |
|
| 2015 | + return [ |
|
| 2016 | + $value, |
|
| 2017 | + $prop->parameters() |
|
| 2018 | + ]; |
|
| 2019 | + } |
|
| 2020 | + |
|
| 2021 | + /** |
|
| 2022 | + * @param string $principalUri |
|
| 2023 | + * @param string $pattern |
|
| 2024 | + * @param array $componentTypes |
|
| 2025 | + * @param array $searchProperties |
|
| 2026 | + * @param array $searchParameters |
|
| 2027 | + * @param array $options |
|
| 2028 | + * @return array |
|
| 2029 | + */ |
|
| 2030 | + public function searchPrincipalUri(string $principalUri, |
|
| 2031 | + string $pattern, |
|
| 2032 | + array $componentTypes, |
|
| 2033 | + array $searchProperties, |
|
| 2034 | + array $searchParameters, |
|
| 2035 | + array $options = []): array { |
|
| 2036 | + $escapePattern = !\array_key_exists('escape_like_param', $options) || $options['escape_like_param'] !== false; |
|
| 2037 | + |
|
| 2038 | + $calendarObjectIdQuery = $this->db->getQueryBuilder(); |
|
| 2039 | + $calendarOr = $calendarObjectIdQuery->expr()->orX(); |
|
| 2040 | + $searchOr = $calendarObjectIdQuery->expr()->orX(); |
|
| 2041 | + |
|
| 2042 | + // Fetch calendars and subscription |
|
| 2043 | + $calendars = $this->getCalendarsForUser($principalUri); |
|
| 2044 | + $subscriptions = $this->getSubscriptionsForUser($principalUri); |
|
| 2045 | + foreach ($calendars as $calendar) { |
|
| 2046 | + $calendarAnd = $calendarObjectIdQuery->expr()->andX(); |
|
| 2047 | + $calendarAnd->add($calendarObjectIdQuery->expr()->eq('cob.calendarid', $calendarObjectIdQuery->createNamedParameter((int)$calendar['id']))); |
|
| 2048 | + $calendarAnd->add($calendarObjectIdQuery->expr()->eq('cob.calendartype', $calendarObjectIdQuery->createNamedParameter(self::CALENDAR_TYPE_CALENDAR))); |
|
| 2049 | + |
|
| 2050 | + // If it's shared, limit search to public events |
|
| 2051 | + if (isset($calendar['{http://owncloud.org/ns}owner-principal']) |
|
| 2052 | + && $calendar['principaluri'] !== $calendar['{http://owncloud.org/ns}owner-principal']) { |
|
| 2053 | + $calendarAnd->add($calendarObjectIdQuery->expr()->eq('co.classification', $calendarObjectIdQuery->createNamedParameter(self::CLASSIFICATION_PUBLIC))); |
|
| 2054 | + } |
|
| 2055 | + |
|
| 2056 | + $calendarOr->add($calendarAnd); |
|
| 2057 | + } |
|
| 2058 | + foreach ($subscriptions as $subscription) { |
|
| 2059 | + $subscriptionAnd = $calendarObjectIdQuery->expr()->andX(); |
|
| 2060 | + $subscriptionAnd->add($calendarObjectIdQuery->expr()->eq('cob.calendarid', $calendarObjectIdQuery->createNamedParameter((int)$subscription['id']))); |
|
| 2061 | + $subscriptionAnd->add($calendarObjectIdQuery->expr()->eq('cob.calendartype', $calendarObjectIdQuery->createNamedParameter(self::CALENDAR_TYPE_SUBSCRIPTION))); |
|
| 2062 | + |
|
| 2063 | + // If it's shared, limit search to public events |
|
| 2064 | + if (isset($subscription['{http://owncloud.org/ns}owner-principal']) |
|
| 2065 | + && $subscription['principaluri'] !== $subscription['{http://owncloud.org/ns}owner-principal']) { |
|
| 2066 | + $subscriptionAnd->add($calendarObjectIdQuery->expr()->eq('co.classification', $calendarObjectIdQuery->createNamedParameter(self::CLASSIFICATION_PUBLIC))); |
|
| 2067 | + } |
|
| 2068 | + |
|
| 2069 | + $calendarOr->add($subscriptionAnd); |
|
| 2070 | + } |
|
| 2071 | + |
|
| 2072 | + foreach ($searchProperties as $property) { |
|
| 2073 | + $propertyAnd = $calendarObjectIdQuery->expr()->andX(); |
|
| 2074 | + $propertyAnd->add($calendarObjectIdQuery->expr()->eq('cob.name', $calendarObjectIdQuery->createNamedParameter($property, IQueryBuilder::PARAM_STR))); |
|
| 2075 | + $propertyAnd->add($calendarObjectIdQuery->expr()->isNull('cob.parameter')); |
|
| 2076 | + |
|
| 2077 | + $searchOr->add($propertyAnd); |
|
| 2078 | + } |
|
| 2079 | + foreach ($searchParameters as $property => $parameter) { |
|
| 2080 | + $parameterAnd = $calendarObjectIdQuery->expr()->andX(); |
|
| 2081 | + $parameterAnd->add($calendarObjectIdQuery->expr()->eq('cob.name', $calendarObjectIdQuery->createNamedParameter($property, IQueryBuilder::PARAM_STR))); |
|
| 2082 | + $parameterAnd->add($calendarObjectIdQuery->expr()->eq('cob.parameter', $calendarObjectIdQuery->createNamedParameter($parameter, IQueryBuilder::PARAM_STR_ARRAY))); |
|
| 2083 | + |
|
| 2084 | + $searchOr->add($parameterAnd); |
|
| 2085 | + } |
|
| 2086 | + |
|
| 2087 | + if ($calendarOr->count() === 0) { |
|
| 2088 | + return []; |
|
| 2089 | + } |
|
| 2090 | + if ($searchOr->count() === 0) { |
|
| 2091 | + return []; |
|
| 2092 | + } |
|
| 2093 | + |
|
| 2094 | + $calendarObjectIdQuery->selectDistinct('cob.objectid') |
|
| 2095 | + ->from($this->dbObjectPropertiesTable, 'cob') |
|
| 2096 | + ->leftJoin('cob', 'calendarobjects', 'co', $calendarObjectIdQuery->expr()->eq('co.id', 'cob.objectid')) |
|
| 2097 | + ->andWhere($calendarObjectIdQuery->expr()->in('co.componenttype', $calendarObjectIdQuery->createNamedParameter($componentTypes, IQueryBuilder::PARAM_STR_ARRAY))) |
|
| 2098 | + ->andWhere($calendarOr) |
|
| 2099 | + ->andWhere($searchOr) |
|
| 2100 | + ->andWhere($calendarObjectIdQuery->expr()->isNull('deleted_at')); |
|
| 2101 | + |
|
| 2102 | + if ('' !== $pattern) { |
|
| 2103 | + if (!$escapePattern) { |
|
| 2104 | + $calendarObjectIdQuery->andWhere($calendarObjectIdQuery->expr()->ilike('cob.value', $calendarObjectIdQuery->createNamedParameter($pattern))); |
|
| 2105 | + } else { |
|
| 2106 | + $calendarObjectIdQuery->andWhere($calendarObjectIdQuery->expr()->ilike('cob.value', $calendarObjectIdQuery->createNamedParameter('%' . $this->db->escapeLikeParameter($pattern) . '%'))); |
|
| 2107 | + } |
|
| 2108 | + } |
|
| 2109 | + |
|
| 2110 | + if (isset($options['limit'])) { |
|
| 2111 | + $calendarObjectIdQuery->setMaxResults($options['limit']); |
|
| 2112 | + } |
|
| 2113 | + if (isset($options['offset'])) { |
|
| 2114 | + $calendarObjectIdQuery->setFirstResult($options['offset']); |
|
| 2115 | + } |
|
| 2116 | + |
|
| 2117 | + $result = $calendarObjectIdQuery->executeQuery(); |
|
| 2118 | + $matches = $result->fetchAll(); |
|
| 2119 | + $result->closeCursor(); |
|
| 2120 | + $matches = array_map(static function (array $match):int { |
|
| 2121 | + return (int) $match['objectid']; |
|
| 2122 | + }, $matches); |
|
| 2123 | + |
|
| 2124 | + $query = $this->db->getQueryBuilder(); |
|
| 2125 | + $query->select('calendardata', 'uri', 'calendarid', 'calendartype') |
|
| 2126 | + ->from('calendarobjects') |
|
| 2127 | + ->where($query->expr()->in('id', $query->createNamedParameter($matches, IQueryBuilder::PARAM_INT_ARRAY))); |
|
| 2128 | + |
|
| 2129 | + $result = $query->executeQuery(); |
|
| 2130 | + $calendarObjects = $result->fetchAll(); |
|
| 2131 | + $result->closeCursor(); |
|
| 2132 | + |
|
| 2133 | + return array_map(function (array $array): array { |
|
| 2134 | + $array['calendarid'] = (int)$array['calendarid']; |
|
| 2135 | + $array['calendartype'] = (int)$array['calendartype']; |
|
| 2136 | + $array['calendardata'] = $this->readBlob($array['calendardata']); |
|
| 2137 | + |
|
| 2138 | + return $array; |
|
| 2139 | + }, $calendarObjects); |
|
| 2140 | + } |
|
| 2141 | + |
|
| 2142 | + /** |
|
| 2143 | + * Searches through all of a users calendars and calendar objects to find |
|
| 2144 | + * an object with a specific UID. |
|
| 2145 | + * |
|
| 2146 | + * This method should return the path to this object, relative to the |
|
| 2147 | + * calendar home, so this path usually only contains two parts: |
|
| 2148 | + * |
|
| 2149 | + * calendarpath/objectpath.ics |
|
| 2150 | + * |
|
| 2151 | + * If the uid is not found, return null. |
|
| 2152 | + * |
|
| 2153 | + * This method should only consider * objects that the principal owns, so |
|
| 2154 | + * any calendars owned by other principals that also appear in this |
|
| 2155 | + * collection should be ignored. |
|
| 2156 | + * |
|
| 2157 | + * @param string $principalUri |
|
| 2158 | + * @param string $uid |
|
| 2159 | + * @return string|null |
|
| 2160 | + */ |
|
| 2161 | + public function getCalendarObjectByUID($principalUri, $uid) { |
|
| 2162 | + $query = $this->db->getQueryBuilder(); |
|
| 2163 | + $query->selectAlias('c.uri', 'calendaruri')->selectAlias('co.uri', 'objecturi') |
|
| 2164 | + ->from('calendarobjects', 'co') |
|
| 2165 | + ->leftJoin('co', 'calendars', 'c', $query->expr()->eq('co.calendarid', 'c.id')) |
|
| 2166 | + ->where($query->expr()->eq('c.principaluri', $query->createNamedParameter($principalUri))) |
|
| 2167 | + ->andWhere($query->expr()->eq('co.uid', $query->createNamedParameter($uid))) |
|
| 2168 | + ->andWhere($query->expr()->isNull('co.deleted_at')); |
|
| 2169 | + $stmt = $query->executeQuery(); |
|
| 2170 | + $row = $stmt->fetch(); |
|
| 2171 | + $stmt->closeCursor(); |
|
| 2172 | + if ($row) { |
|
| 2173 | + return $row['calendaruri'] . '/' . $row['objecturi']; |
|
| 2174 | + } |
|
| 2175 | + |
|
| 2176 | + return null; |
|
| 2177 | + } |
|
| 2178 | + |
|
| 2179 | + public function getCalendarObjectById(string $principalUri, int $id): ?array { |
|
| 2180 | + $query = $this->db->getQueryBuilder(); |
|
| 2181 | + $query->select(['co.id', 'co.uri', 'co.lastmodified', 'co.etag', 'co.calendarid', 'co.size', 'co.calendardata', 'co.componenttype', 'co.classification', 'co.deleted_at']) |
|
| 2182 | + ->selectAlias('c.uri', 'calendaruri') |
|
| 2183 | + ->from('calendarobjects', 'co') |
|
| 2184 | + ->join('co', 'calendars', 'c', $query->expr()->eq('c.id', 'co.calendarid', IQueryBuilder::PARAM_INT)) |
|
| 2185 | + ->where($query->expr()->eq('c.principaluri', $query->createNamedParameter($principalUri))) |
|
| 2186 | + ->andWhere($query->expr()->eq('co.id', $query->createNamedParameter($id, IQueryBuilder::PARAM_INT), IQueryBuilder::PARAM_INT)); |
|
| 2187 | + $stmt = $query->executeQuery(); |
|
| 2188 | + $row = $stmt->fetch(); |
|
| 2189 | + $stmt->closeCursor(); |
|
| 2190 | + |
|
| 2191 | + if (!$row) { |
|
| 2192 | + return null; |
|
| 2193 | + } |
|
| 2194 | + |
|
| 2195 | + return [ |
|
| 2196 | + 'id' => $row['id'], |
|
| 2197 | + 'uri' => $row['uri'], |
|
| 2198 | + 'lastmodified' => $row['lastmodified'], |
|
| 2199 | + 'etag' => '"' . $row['etag'] . '"', |
|
| 2200 | + 'calendarid' => $row['calendarid'], |
|
| 2201 | + 'calendaruri' => $row['calendaruri'], |
|
| 2202 | + 'size' => (int)$row['size'], |
|
| 2203 | + 'calendardata' => $this->readBlob($row['calendardata']), |
|
| 2204 | + 'component' => strtolower($row['componenttype']), |
|
| 2205 | + 'classification' => (int)$row['classification'], |
|
| 2206 | + 'deleted_at' => isset($row['deleted_at']) ? ((int) $row['deleted_at']) : null, |
|
| 2207 | + ]; |
|
| 2208 | + } |
|
| 2209 | + |
|
| 2210 | + /** |
|
| 2211 | + * The getChanges method returns all the changes that have happened, since |
|
| 2212 | + * the specified syncToken in the specified calendar. |
|
| 2213 | + * |
|
| 2214 | + * This function should return an array, such as the following: |
|
| 2215 | + * |
|
| 2216 | + * [ |
|
| 2217 | + * 'syncToken' => 'The current synctoken', |
|
| 2218 | + * 'added' => [ |
|
| 2219 | + * 'new.txt', |
|
| 2220 | + * ], |
|
| 2221 | + * 'modified' => [ |
|
| 2222 | + * 'modified.txt', |
|
| 2223 | + * ], |
|
| 2224 | + * 'deleted' => [ |
|
| 2225 | + * 'foo.php.bak', |
|
| 2226 | + * 'old.txt' |
|
| 2227 | + * ] |
|
| 2228 | + * ); |
|
| 2229 | + * |
|
| 2230 | + * The returned syncToken property should reflect the *current* syncToken |
|
| 2231 | + * of the calendar, as reported in the {http://sabredav.org/ns}sync-token |
|
| 2232 | + * property This is * needed here too, to ensure the operation is atomic. |
|
| 2233 | + * |
|
| 2234 | + * If the $syncToken argument is specified as null, this is an initial |
|
| 2235 | + * sync, and all members should be reported. |
|
| 2236 | + * |
|
| 2237 | + * The modified property is an array of nodenames that have changed since |
|
| 2238 | + * the last token. |
|
| 2239 | + * |
|
| 2240 | + * The deleted property is an array with nodenames, that have been deleted |
|
| 2241 | + * from collection. |
|
| 2242 | + * |
|
| 2243 | + * The $syncLevel argument is basically the 'depth' of the report. If it's |
|
| 2244 | + * 1, you only have to report changes that happened only directly in |
|
| 2245 | + * immediate descendants. If it's 2, it should also include changes from |
|
| 2246 | + * the nodes below the child collections. (grandchildren) |
|
| 2247 | + * |
|
| 2248 | + * The $limit argument allows a client to specify how many results should |
|
| 2249 | + * be returned at most. If the limit is not specified, it should be treated |
|
| 2250 | + * as infinite. |
|
| 2251 | + * |
|
| 2252 | + * If the limit (infinite or not) is higher than you're willing to return, |
|
| 2253 | + * you should throw a Sabre\DAV\Exception\TooMuchMatches() exception. |
|
| 2254 | + * |
|
| 2255 | + * If the syncToken is expired (due to data cleanup) or unknown, you must |
|
| 2256 | + * return null. |
|
| 2257 | + * |
|
| 2258 | + * The limit is 'suggestive'. You are free to ignore it. |
|
| 2259 | + * |
|
| 2260 | + * @param string $calendarId |
|
| 2261 | + * @param string $syncToken |
|
| 2262 | + * @param int $syncLevel |
|
| 2263 | + * @param int|null $limit |
|
| 2264 | + * @param int $calendarType |
|
| 2265 | + * @return array |
|
| 2266 | + */ |
|
| 2267 | + public function getChangesForCalendar($calendarId, $syncToken, $syncLevel, $limit = null, $calendarType = self::CALENDAR_TYPE_CALENDAR) { |
|
| 2268 | + // Current synctoken |
|
| 2269 | + $qb = $this->db->getQueryBuilder(); |
|
| 2270 | + $qb->select('synctoken') |
|
| 2271 | + ->from('calendars') |
|
| 2272 | + ->where( |
|
| 2273 | + $qb->expr()->eq('id', $qb->createNamedParameter($calendarId)) |
|
| 2274 | + ); |
|
| 2275 | + $stmt = $qb->executeQuery(); |
|
| 2276 | + $currentToken = $stmt->fetchOne(); |
|
| 2277 | + |
|
| 2278 | + if ($currentToken === false) { |
|
| 2279 | + return null; |
|
| 2280 | + } |
|
| 2281 | + |
|
| 2282 | + $result = [ |
|
| 2283 | + 'syncToken' => $currentToken, |
|
| 2284 | + 'added' => [], |
|
| 2285 | + 'modified' => [], |
|
| 2286 | + 'deleted' => [], |
|
| 2287 | + ]; |
|
| 2288 | + |
|
| 2289 | + if ($syncToken) { |
|
| 2290 | + $qb = $this->db->getQueryBuilder(); |
|
| 2291 | + |
|
| 2292 | + $qb->select('uri', 'operation') |
|
| 2293 | + ->from('calendarchanges') |
|
| 2294 | + ->where( |
|
| 2295 | + $qb->expr()->andX( |
|
| 2296 | + $qb->expr()->gte('synctoken', $qb->createNamedParameter($syncToken)), |
|
| 2297 | + $qb->expr()->lt('synctoken', $qb->createNamedParameter($currentToken)), |
|
| 2298 | + $qb->expr()->eq('calendarid', $qb->createNamedParameter($calendarId)), |
|
| 2299 | + $qb->expr()->eq('calendartype', $qb->createNamedParameter($calendarType)) |
|
| 2300 | + ) |
|
| 2301 | + )->orderBy('synctoken'); |
|
| 2302 | + if (is_int($limit) && $limit > 0) { |
|
| 2303 | + $qb->setMaxResults($limit); |
|
| 2304 | + } |
|
| 2305 | + |
|
| 2306 | + // Fetching all changes |
|
| 2307 | + $stmt = $qb->executeQuery(); |
|
| 2308 | + $changes = []; |
|
| 2309 | + |
|
| 2310 | + // This loop ensures that any duplicates are overwritten, only the |
|
| 2311 | + // last change on a node is relevant. |
|
| 2312 | + while ($row = $stmt->fetch()) { |
|
| 2313 | + $changes[$row['uri']] = $row['operation']; |
|
| 2314 | + } |
|
| 2315 | + $stmt->closeCursor(); |
|
| 2316 | + |
|
| 2317 | + foreach ($changes as $uri => $operation) { |
|
| 2318 | + switch ($operation) { |
|
| 2319 | + case 1: |
|
| 2320 | + $result['added'][] = $uri; |
|
| 2321 | + break; |
|
| 2322 | + case 2: |
|
| 2323 | + $result['modified'][] = $uri; |
|
| 2324 | + break; |
|
| 2325 | + case 3: |
|
| 2326 | + $result['deleted'][] = $uri; |
|
| 2327 | + break; |
|
| 2328 | + } |
|
| 2329 | + } |
|
| 2330 | + } else { |
|
| 2331 | + // No synctoken supplied, this is the initial sync. |
|
| 2332 | + $qb = $this->db->getQueryBuilder(); |
|
| 2333 | + $qb->select('uri') |
|
| 2334 | + ->from('calendarobjects') |
|
| 2335 | + ->where( |
|
| 2336 | + $qb->expr()->andX( |
|
| 2337 | + $qb->expr()->eq('calendarid', $qb->createNamedParameter($calendarId)), |
|
| 2338 | + $qb->expr()->eq('calendartype', $qb->createNamedParameter($calendarType)) |
|
| 2339 | + ) |
|
| 2340 | + ); |
|
| 2341 | + $stmt = $qb->executeQuery(); |
|
| 2342 | + $result['added'] = $stmt->fetchAll(\PDO::FETCH_COLUMN); |
|
| 2343 | + $stmt->closeCursor(); |
|
| 2344 | + } |
|
| 2345 | + return $result; |
|
| 2346 | + } |
|
| 2347 | + |
|
| 2348 | + /** |
|
| 2349 | + * Returns a list of subscriptions for a principal. |
|
| 2350 | + * |
|
| 2351 | + * Every subscription is an array with the following keys: |
|
| 2352 | + * * id, a unique id that will be used by other functions to modify the |
|
| 2353 | + * subscription. This can be the same as the uri or a database key. |
|
| 2354 | + * * uri. This is just the 'base uri' or 'filename' of the subscription. |
|
| 2355 | + * * principaluri. The owner of the subscription. Almost always the same as |
|
| 2356 | + * principalUri passed to this method. |
|
| 2357 | + * |
|
| 2358 | + * Furthermore, all the subscription info must be returned too: |
|
| 2359 | + * |
|
| 2360 | + * 1. {DAV:}displayname |
|
| 2361 | + * 2. {http://apple.com/ns/ical/}refreshrate |
|
| 2362 | + * 3. {http://calendarserver.org/ns/}subscribed-strip-todos (omit if todos |
|
| 2363 | + * should not be stripped). |
|
| 2364 | + * 4. {http://calendarserver.org/ns/}subscribed-strip-alarms (omit if alarms |
|
| 2365 | + * should not be stripped). |
|
| 2366 | + * 5. {http://calendarserver.org/ns/}subscribed-strip-attachments (omit if |
|
| 2367 | + * attachments should not be stripped). |
|
| 2368 | + * 6. {http://calendarserver.org/ns/}source (Must be a |
|
| 2369 | + * Sabre\DAV\Property\Href). |
|
| 2370 | + * 7. {http://apple.com/ns/ical/}calendar-color |
|
| 2371 | + * 8. {http://apple.com/ns/ical/}calendar-order |
|
| 2372 | + * 9. {urn:ietf:params:xml:ns:caldav}supported-calendar-component-set |
|
| 2373 | + * (should just be an instance of |
|
| 2374 | + * Sabre\CalDAV\Property\SupportedCalendarComponentSet, with a bunch of |
|
| 2375 | + * default components). |
|
| 2376 | + * |
|
| 2377 | + * @param string $principalUri |
|
| 2378 | + * @return array |
|
| 2379 | + */ |
|
| 2380 | + public function getSubscriptionsForUser($principalUri) { |
|
| 2381 | + $fields = array_column($this->subscriptionPropertyMap, 0); |
|
| 2382 | + $fields[] = 'id'; |
|
| 2383 | + $fields[] = 'uri'; |
|
| 2384 | + $fields[] = 'source'; |
|
| 2385 | + $fields[] = 'principaluri'; |
|
| 2386 | + $fields[] = 'lastmodified'; |
|
| 2387 | + $fields[] = 'synctoken'; |
|
| 2388 | + |
|
| 2389 | + $query = $this->db->getQueryBuilder(); |
|
| 2390 | + $query->select($fields) |
|
| 2391 | + ->from('calendarsubscriptions') |
|
| 2392 | + ->where($query->expr()->eq('principaluri', $query->createNamedParameter($principalUri))) |
|
| 2393 | + ->orderBy('calendarorder', 'asc'); |
|
| 2394 | + $stmt = $query->executeQuery(); |
|
| 2395 | + |
|
| 2396 | + $subscriptions = []; |
|
| 2397 | + while ($row = $stmt->fetch()) { |
|
| 2398 | + $subscription = [ |
|
| 2399 | + 'id' => $row['id'], |
|
| 2400 | + 'uri' => $row['uri'], |
|
| 2401 | + 'principaluri' => $row['principaluri'], |
|
| 2402 | + 'source' => $row['source'], |
|
| 2403 | + 'lastmodified' => $row['lastmodified'], |
|
| 2404 | + |
|
| 2405 | + '{' . Plugin::NS_CALDAV . '}supported-calendar-component-set' => new SupportedCalendarComponentSet(['VTODO', 'VEVENT']), |
|
| 2406 | + '{http://sabredav.org/ns}sync-token' => $row['synctoken']?$row['synctoken']:'0', |
|
| 2407 | + ]; |
|
| 2408 | + |
|
| 2409 | + $subscriptions[] = $this->rowToSubscription($row, $subscription); |
|
| 2410 | + } |
|
| 2411 | + |
|
| 2412 | + return $subscriptions; |
|
| 2413 | + } |
|
| 2414 | + |
|
| 2415 | + /** |
|
| 2416 | + * Creates a new subscription for a principal. |
|
| 2417 | + * |
|
| 2418 | + * If the creation was a success, an id must be returned that can be used to reference |
|
| 2419 | + * this subscription in other methods, such as updateSubscription. |
|
| 2420 | + * |
|
| 2421 | + * @param string $principalUri |
|
| 2422 | + * @param string $uri |
|
| 2423 | + * @param array $properties |
|
| 2424 | + * @return mixed |
|
| 2425 | + */ |
|
| 2426 | + public function createSubscription($principalUri, $uri, array $properties) { |
|
| 2427 | + if (!isset($properties['{http://calendarserver.org/ns/}source'])) { |
|
| 2428 | + throw new Forbidden('The {http://calendarserver.org/ns/}source property is required when creating subscriptions'); |
|
| 2429 | + } |
|
| 2430 | + |
|
| 2431 | + $values = [ |
|
| 2432 | + 'principaluri' => $principalUri, |
|
| 2433 | + 'uri' => $uri, |
|
| 2434 | + 'source' => $properties['{http://calendarserver.org/ns/}source']->getHref(), |
|
| 2435 | + 'lastmodified' => time(), |
|
| 2436 | + ]; |
|
| 2437 | + |
|
| 2438 | + $propertiesBoolean = ['striptodos', 'stripalarms', 'stripattachments']; |
|
| 2439 | + |
|
| 2440 | + foreach ($this->subscriptionPropertyMap as $xmlName => [$dbName, $type]) { |
|
| 2441 | + if (array_key_exists($xmlName, $properties)) { |
|
| 2442 | + $values[$dbName] = $properties[$xmlName]; |
|
| 2443 | + if (in_array($dbName, $propertiesBoolean)) { |
|
| 2444 | + $values[$dbName] = true; |
|
| 2445 | + } |
|
| 2446 | + } |
|
| 2447 | + } |
|
| 2448 | + |
|
| 2449 | + $valuesToInsert = []; |
|
| 2450 | + |
|
| 2451 | + $query = $this->db->getQueryBuilder(); |
|
| 2452 | + |
|
| 2453 | + foreach (array_keys($values) as $name) { |
|
| 2454 | + $valuesToInsert[$name] = $query->createNamedParameter($values[$name]); |
|
| 2455 | + } |
|
| 2456 | + |
|
| 2457 | + $query->insert('calendarsubscriptions') |
|
| 2458 | + ->values($valuesToInsert) |
|
| 2459 | + ->executeStatement(); |
|
| 2460 | + |
|
| 2461 | + $subscriptionId = $query->getLastInsertId(); |
|
| 2462 | + |
|
| 2463 | + $subscriptionRow = $this->getSubscriptionById($subscriptionId); |
|
| 2464 | + $this->dispatcher->dispatchTyped(new SubscriptionCreatedEvent($subscriptionId, $subscriptionRow)); |
|
| 2465 | + |
|
| 2466 | + return $subscriptionId; |
|
| 2467 | + } |
|
| 2468 | + |
|
| 2469 | + /** |
|
| 2470 | + * Updates a subscription |
|
| 2471 | + * |
|
| 2472 | + * The list of mutations is stored in a Sabre\DAV\PropPatch object. |
|
| 2473 | + * To do the actual updates, you must tell this object which properties |
|
| 2474 | + * you're going to process with the handle() method. |
|
| 2475 | + * |
|
| 2476 | + * Calling the handle method is like telling the PropPatch object "I |
|
| 2477 | + * promise I can handle updating this property". |
|
| 2478 | + * |
|
| 2479 | + * Read the PropPatch documentation for more info and examples. |
|
| 2480 | + * |
|
| 2481 | + * @param mixed $subscriptionId |
|
| 2482 | + * @param PropPatch $propPatch |
|
| 2483 | + * @return void |
|
| 2484 | + */ |
|
| 2485 | + public function updateSubscription($subscriptionId, PropPatch $propPatch) { |
|
| 2486 | + $supportedProperties = array_keys($this->subscriptionPropertyMap); |
|
| 2487 | + $supportedProperties[] = '{http://calendarserver.org/ns/}source'; |
|
| 2488 | + |
|
| 2489 | + $propPatch->handle($supportedProperties, function ($mutations) use ($subscriptionId) { |
|
| 2490 | + $newValues = []; |
|
| 2491 | + |
|
| 2492 | + foreach ($mutations as $propertyName => $propertyValue) { |
|
| 2493 | + if ($propertyName === '{http://calendarserver.org/ns/}source') { |
|
| 2494 | + $newValues['source'] = $propertyValue->getHref(); |
|
| 2495 | + } else { |
|
| 2496 | + $fieldName = $this->subscriptionPropertyMap[$propertyName][0]; |
|
| 2497 | + $newValues[$fieldName] = $propertyValue; |
|
| 2498 | + } |
|
| 2499 | + } |
|
| 2500 | + |
|
| 2501 | + $query = $this->db->getQueryBuilder(); |
|
| 2502 | + $query->update('calendarsubscriptions') |
|
| 2503 | + ->set('lastmodified', $query->createNamedParameter(time())); |
|
| 2504 | + foreach ($newValues as $fieldName => $value) { |
|
| 2505 | + $query->set($fieldName, $query->createNamedParameter($value)); |
|
| 2506 | + } |
|
| 2507 | + $query->where($query->expr()->eq('id', $query->createNamedParameter($subscriptionId))) |
|
| 2508 | + ->executeStatement(); |
|
| 2509 | + |
|
| 2510 | + $subscriptionRow = $this->getSubscriptionById($subscriptionId); |
|
| 2511 | + $this->dispatcher->dispatchTyped(new SubscriptionUpdatedEvent((int)$subscriptionId, $subscriptionRow, [], $mutations)); |
|
| 2512 | + |
|
| 2513 | + return true; |
|
| 2514 | + }); |
|
| 2515 | + } |
|
| 2516 | + |
|
| 2517 | + /** |
|
| 2518 | + * Deletes a subscription. |
|
| 2519 | + * |
|
| 2520 | + * @param mixed $subscriptionId |
|
| 2521 | + * @return void |
|
| 2522 | + */ |
|
| 2523 | + public function deleteSubscription($subscriptionId) { |
|
| 2524 | + $subscriptionRow = $this->getSubscriptionById($subscriptionId); |
|
| 2525 | + |
|
| 2526 | + $query = $this->db->getQueryBuilder(); |
|
| 2527 | + $query->delete('calendarsubscriptions') |
|
| 2528 | + ->where($query->expr()->eq('id', $query->createNamedParameter($subscriptionId))) |
|
| 2529 | + ->executeStatement(); |
|
| 2530 | + |
|
| 2531 | + $query = $this->db->getQueryBuilder(); |
|
| 2532 | + $query->delete('calendarobjects') |
|
| 2533 | + ->where($query->expr()->eq('calendarid', $query->createNamedParameter($subscriptionId))) |
|
| 2534 | + ->andWhere($query->expr()->eq('calendartype', $query->createNamedParameter(self::CALENDAR_TYPE_SUBSCRIPTION))) |
|
| 2535 | + ->executeStatement(); |
|
| 2536 | + |
|
| 2537 | + $query->delete('calendarchanges') |
|
| 2538 | + ->where($query->expr()->eq('calendarid', $query->createNamedParameter($subscriptionId))) |
|
| 2539 | + ->andWhere($query->expr()->eq('calendartype', $query->createNamedParameter(self::CALENDAR_TYPE_SUBSCRIPTION))) |
|
| 2540 | + ->executeStatement(); |
|
| 2541 | + |
|
| 2542 | + $query->delete($this->dbObjectPropertiesTable) |
|
| 2543 | + ->where($query->expr()->eq('calendarid', $query->createNamedParameter($subscriptionId))) |
|
| 2544 | + ->andWhere($query->expr()->eq('calendartype', $query->createNamedParameter(self::CALENDAR_TYPE_SUBSCRIPTION))) |
|
| 2545 | + ->executeStatement(); |
|
| 2546 | + |
|
| 2547 | + if ($subscriptionRow) { |
|
| 2548 | + $this->dispatcher->dispatchTyped(new SubscriptionDeletedEvent((int)$subscriptionId, $subscriptionRow, [])); |
|
| 2549 | + } |
|
| 2550 | + } |
|
| 2551 | + |
|
| 2552 | + /** |
|
| 2553 | + * Returns a single scheduling object for the inbox collection. |
|
| 2554 | + * |
|
| 2555 | + * The returned array should contain the following elements: |
|
| 2556 | + * * uri - A unique basename for the object. This will be used to |
|
| 2557 | + * construct a full uri. |
|
| 2558 | + * * calendardata - The iCalendar object |
|
| 2559 | + * * lastmodified - The last modification date. Can be an int for a unix |
|
| 2560 | + * timestamp, or a PHP DateTime object. |
|
| 2561 | + * * etag - A unique token that must change if the object changed. |
|
| 2562 | + * * size - The size of the object, in bytes. |
|
| 2563 | + * |
|
| 2564 | + * @param string $principalUri |
|
| 2565 | + * @param string $objectUri |
|
| 2566 | + * @return array |
|
| 2567 | + */ |
|
| 2568 | + public function getSchedulingObject($principalUri, $objectUri) { |
|
| 2569 | + $query = $this->db->getQueryBuilder(); |
|
| 2570 | + $stmt = $query->select(['uri', 'calendardata', 'lastmodified', 'etag', 'size']) |
|
| 2571 | + ->from('schedulingobjects') |
|
| 2572 | + ->where($query->expr()->eq('principaluri', $query->createNamedParameter($principalUri))) |
|
| 2573 | + ->andWhere($query->expr()->eq('uri', $query->createNamedParameter($objectUri))) |
|
| 2574 | + ->executeQuery(); |
|
| 2575 | + |
|
| 2576 | + $row = $stmt->fetch(); |
|
| 2577 | + |
|
| 2578 | + if (!$row) { |
|
| 2579 | + return null; |
|
| 2580 | + } |
|
| 2581 | + |
|
| 2582 | + return [ |
|
| 2583 | + 'uri' => $row['uri'], |
|
| 2584 | + 'calendardata' => $row['calendardata'], |
|
| 2585 | + 'lastmodified' => $row['lastmodified'], |
|
| 2586 | + 'etag' => '"' . $row['etag'] . '"', |
|
| 2587 | + 'size' => (int)$row['size'], |
|
| 2588 | + ]; |
|
| 2589 | + } |
|
| 2590 | + |
|
| 2591 | + /** |
|
| 2592 | + * Returns all scheduling objects for the inbox collection. |
|
| 2593 | + * |
|
| 2594 | + * These objects should be returned as an array. Every item in the array |
|
| 2595 | + * should follow the same structure as returned from getSchedulingObject. |
|
| 2596 | + * |
|
| 2597 | + * The main difference is that 'calendardata' is optional. |
|
| 2598 | + * |
|
| 2599 | + * @param string $principalUri |
|
| 2600 | + * @return array |
|
| 2601 | + */ |
|
| 2602 | + public function getSchedulingObjects($principalUri) { |
|
| 2603 | + $query = $this->db->getQueryBuilder(); |
|
| 2604 | + $stmt = $query->select(['uri', 'calendardata', 'lastmodified', 'etag', 'size']) |
|
| 2605 | + ->from('schedulingobjects') |
|
| 2606 | + ->where($query->expr()->eq('principaluri', $query->createNamedParameter($principalUri))) |
|
| 2607 | + ->executeQuery(); |
|
| 2608 | + |
|
| 2609 | + $result = []; |
|
| 2610 | + foreach ($stmt->fetchAll() as $row) { |
|
| 2611 | + $result[] = [ |
|
| 2612 | + 'calendardata' => $row['calendardata'], |
|
| 2613 | + 'uri' => $row['uri'], |
|
| 2614 | + 'lastmodified' => $row['lastmodified'], |
|
| 2615 | + 'etag' => '"' . $row['etag'] . '"', |
|
| 2616 | + 'size' => (int)$row['size'], |
|
| 2617 | + ]; |
|
| 2618 | + } |
|
| 2619 | + $stmt->closeCursor(); |
|
| 2620 | + |
|
| 2621 | + return $result; |
|
| 2622 | + } |
|
| 2623 | + |
|
| 2624 | + /** |
|
| 2625 | + * Deletes a scheduling object from the inbox collection. |
|
| 2626 | + * |
|
| 2627 | + * @param string $principalUri |
|
| 2628 | + * @param string $objectUri |
|
| 2629 | + * @return void |
|
| 2630 | + */ |
|
| 2631 | + public function deleteSchedulingObject($principalUri, $objectUri) { |
|
| 2632 | + $query = $this->db->getQueryBuilder(); |
|
| 2633 | + $query->delete('schedulingobjects') |
|
| 2634 | + ->where($query->expr()->eq('principaluri', $query->createNamedParameter($principalUri))) |
|
| 2635 | + ->andWhere($query->expr()->eq('uri', $query->createNamedParameter($objectUri))) |
|
| 2636 | + ->executeStatement(); |
|
| 2637 | + } |
|
| 2638 | + |
|
| 2639 | + /** |
|
| 2640 | + * Creates a new scheduling object. This should land in a users' inbox. |
|
| 2641 | + * |
|
| 2642 | + * @param string $principalUri |
|
| 2643 | + * @param string $objectUri |
|
| 2644 | + * @param string $objectData |
|
| 2645 | + * @return void |
|
| 2646 | + */ |
|
| 2647 | + public function createSchedulingObject($principalUri, $objectUri, $objectData) { |
|
| 2648 | + $query = $this->db->getQueryBuilder(); |
|
| 2649 | + $query->insert('schedulingobjects') |
|
| 2650 | + ->values([ |
|
| 2651 | + 'principaluri' => $query->createNamedParameter($principalUri), |
|
| 2652 | + 'calendardata' => $query->createNamedParameter($objectData, IQueryBuilder::PARAM_LOB), |
|
| 2653 | + 'uri' => $query->createNamedParameter($objectUri), |
|
| 2654 | + 'lastmodified' => $query->createNamedParameter(time()), |
|
| 2655 | + 'etag' => $query->createNamedParameter(md5($objectData)), |
|
| 2656 | + 'size' => $query->createNamedParameter(strlen($objectData)) |
|
| 2657 | + ]) |
|
| 2658 | + ->executeStatement(); |
|
| 2659 | + } |
|
| 2660 | + |
|
| 2661 | + /** |
|
| 2662 | + * Adds a change record to the calendarchanges table. |
|
| 2663 | + * |
|
| 2664 | + * @param mixed $calendarId |
|
| 2665 | + * @param string $objectUri |
|
| 2666 | + * @param int $operation 1 = add, 2 = modify, 3 = delete. |
|
| 2667 | + * @param int $calendarType |
|
| 2668 | + * @return void |
|
| 2669 | + */ |
|
| 2670 | + protected function addChange($calendarId, $objectUri, $operation, $calendarType = self::CALENDAR_TYPE_CALENDAR) { |
|
| 2671 | + $table = $calendarType === self::CALENDAR_TYPE_CALENDAR ? 'calendars': 'calendarsubscriptions'; |
|
| 2672 | + |
|
| 2673 | + $query = $this->db->getQueryBuilder(); |
|
| 2674 | + $query->select('synctoken') |
|
| 2675 | + ->from($table) |
|
| 2676 | + ->where($query->expr()->eq('id', $query->createNamedParameter($calendarId))); |
|
| 2677 | + $result = $query->executeQuery(); |
|
| 2678 | + $syncToken = (int)$result->fetchOne(); |
|
| 2679 | + $result->closeCursor(); |
|
| 2680 | + |
|
| 2681 | + $query = $this->db->getQueryBuilder(); |
|
| 2682 | + $query->insert('calendarchanges') |
|
| 2683 | + ->values([ |
|
| 2684 | + 'uri' => $query->createNamedParameter($objectUri), |
|
| 2685 | + 'synctoken' => $query->createNamedParameter($syncToken), |
|
| 2686 | + 'calendarid' => $query->createNamedParameter($calendarId), |
|
| 2687 | + 'operation' => $query->createNamedParameter($operation), |
|
| 2688 | + 'calendartype' => $query->createNamedParameter($calendarType), |
|
| 2689 | + ]) |
|
| 2690 | + ->executeStatement(); |
|
| 2691 | + |
|
| 2692 | + $stmt = $this->db->prepare("UPDATE `*PREFIX*$table` SET `synctoken` = `synctoken` + 1 WHERE `id` = ?"); |
|
| 2693 | + $stmt->execute([ |
|
| 2694 | + $calendarId |
|
| 2695 | + ]); |
|
| 2696 | + } |
|
| 2697 | + |
|
| 2698 | + /** |
|
| 2699 | + * Parses some information from calendar objects, used for optimized |
|
| 2700 | + * calendar-queries. |
|
| 2701 | + * |
|
| 2702 | + * Returns an array with the following keys: |
|
| 2703 | + * * etag - An md5 checksum of the object without the quotes. |
|
| 2704 | + * * size - Size of the object in bytes |
|
| 2705 | + * * componentType - VEVENT, VTODO or VJOURNAL |
|
| 2706 | + * * firstOccurence |
|
| 2707 | + * * lastOccurence |
|
| 2708 | + * * uid - value of the UID property |
|
| 2709 | + * |
|
| 2710 | + * @param string $calendarData |
|
| 2711 | + * @return array |
|
| 2712 | + */ |
|
| 2713 | + public function getDenormalizedData($calendarData) { |
|
| 2714 | + $vObject = Reader::read($calendarData); |
|
| 2715 | + $vEvents = []; |
|
| 2716 | + $componentType = null; |
|
| 2717 | + $component = null; |
|
| 2718 | + $firstOccurrence = null; |
|
| 2719 | + $lastOccurrence = null; |
|
| 2720 | + $uid = null; |
|
| 2721 | + $classification = self::CLASSIFICATION_PUBLIC; |
|
| 2722 | + $hasDTSTART = false; |
|
| 2723 | + foreach ($vObject->getComponents() as $component) { |
|
| 2724 | + if ($component->name !== 'VTIMEZONE') { |
|
| 2725 | + // Finding all VEVENTs, and track them |
|
| 2726 | + if ($component->name === 'VEVENT') { |
|
| 2727 | + array_push($vEvents, $component); |
|
| 2728 | + if ($component->DTSTART) { |
|
| 2729 | + $hasDTSTART = true; |
|
| 2730 | + } |
|
| 2731 | + } |
|
| 2732 | + // Track first component type and uid |
|
| 2733 | + if ($uid === null) { |
|
| 2734 | + $componentType = $component->name; |
|
| 2735 | + $uid = (string)$component->UID; |
|
| 2736 | + } |
|
| 2737 | + } |
|
| 2738 | + } |
|
| 2739 | + if (!$componentType) { |
|
| 2740 | + throw new BadRequest('Calendar objects must have a VJOURNAL, VEVENT or VTODO component'); |
|
| 2741 | + } |
|
| 2742 | + |
|
| 2743 | + if ($hasDTSTART) { |
|
| 2744 | + $component = $vEvents[0]; |
|
| 2745 | + |
|
| 2746 | + // Finding the last occurrence is a bit harder |
|
| 2747 | + if (!isset($component->RRULE) && count($vEvents) === 1) { |
|
| 2748 | + $firstOccurrence = $component->DTSTART->getDateTime()->getTimeStamp(); |
|
| 2749 | + if (isset($component->DTEND)) { |
|
| 2750 | + $lastOccurrence = $component->DTEND->getDateTime()->getTimeStamp(); |
|
| 2751 | + } elseif (isset($component->DURATION)) { |
|
| 2752 | + $endDate = clone $component->DTSTART->getDateTime(); |
|
| 2753 | + $endDate->add(DateTimeParser::parse($component->DURATION->getValue())); |
|
| 2754 | + $lastOccurrence = $endDate->getTimeStamp(); |
|
| 2755 | + } elseif (!$component->DTSTART->hasTime()) { |
|
| 2756 | + $endDate = clone $component->DTSTART->getDateTime(); |
|
| 2757 | + $endDate->modify('+1 day'); |
|
| 2758 | + $lastOccurrence = $endDate->getTimeStamp(); |
|
| 2759 | + } else { |
|
| 2760 | + $lastOccurrence = $firstOccurrence; |
|
| 2761 | + } |
|
| 2762 | + } else { |
|
| 2763 | + $it = new EventIterator($vEvents); |
|
| 2764 | + $maxDate = new DateTime(self::MAX_DATE); |
|
| 2765 | + $firstOccurrence = $it->getDtStart()->getTimestamp(); |
|
| 2766 | + if ($it->isInfinite()) { |
|
| 2767 | + $lastOccurrence = $maxDate->getTimestamp(); |
|
| 2768 | + } else { |
|
| 2769 | + $end = $it->getDtEnd(); |
|
| 2770 | + while ($it->valid() && $end < $maxDate) { |
|
| 2771 | + $end = $it->getDtEnd(); |
|
| 2772 | + $it->next(); |
|
| 2773 | + } |
|
| 2774 | + $lastOccurrence = $end->getTimestamp(); |
|
| 2775 | + } |
|
| 2776 | + } |
|
| 2777 | + } |
|
| 2778 | + |
|
| 2779 | + if ($component->CLASS) { |
|
| 2780 | + $classification = CalDavBackend::CLASSIFICATION_PRIVATE; |
|
| 2781 | + switch ($component->CLASS->getValue()) { |
|
| 2782 | + case 'PUBLIC': |
|
| 2783 | + $classification = CalDavBackend::CLASSIFICATION_PUBLIC; |
|
| 2784 | + break; |
|
| 2785 | + case 'CONFIDENTIAL': |
|
| 2786 | + $classification = CalDavBackend::CLASSIFICATION_CONFIDENTIAL; |
|
| 2787 | + break; |
|
| 2788 | + } |
|
| 2789 | + } |
|
| 2790 | + return [ |
|
| 2791 | + 'etag' => md5($calendarData), |
|
| 2792 | + 'size' => strlen($calendarData), |
|
| 2793 | + 'componentType' => $componentType, |
|
| 2794 | + 'firstOccurence' => is_null($firstOccurrence) ? null : max(0, $firstOccurrence), |
|
| 2795 | + 'lastOccurence' => $lastOccurrence, |
|
| 2796 | + 'uid' => $uid, |
|
| 2797 | + 'classification' => $classification |
|
| 2798 | + ]; |
|
| 2799 | + } |
|
| 2800 | + |
|
| 2801 | + /** |
|
| 2802 | + * @param $cardData |
|
| 2803 | + * @return bool|string |
|
| 2804 | + */ |
|
| 2805 | + private function readBlob($cardData) { |
|
| 2806 | + if (is_resource($cardData)) { |
|
| 2807 | + return stream_get_contents($cardData); |
|
| 2808 | + } |
|
| 2809 | + |
|
| 2810 | + return $cardData; |
|
| 2811 | + } |
|
| 2812 | + |
|
| 2813 | + /** |
|
| 2814 | + * @param list<array{href: string, commonName: string, readOnly: bool}> $add |
|
| 2815 | + * @param list<string> $remove |
|
| 2816 | + */ |
|
| 2817 | + public function updateShares(IShareable $shareable, array $add, array $remove): void { |
|
| 2818 | + $calendarId = $shareable->getResourceId(); |
|
| 2819 | + $calendarRow = $this->getCalendarById($calendarId); |
|
| 2820 | + if ($calendarRow === null) { |
|
| 2821 | + throw new \RuntimeException('Trying to update shares for innexistant calendar: ' . $calendarId); |
|
| 2822 | + } |
|
| 2823 | + $oldShares = $this->getShares($calendarId); |
|
| 2824 | + |
|
| 2825 | + $this->calendarSharingBackend->updateShares($shareable, $add, $remove); |
|
| 2826 | + |
|
| 2827 | + $this->dispatcher->dispatchTyped(new CalendarShareUpdatedEvent($calendarId, $calendarRow, $oldShares, $add, $remove)); |
|
| 2828 | + } |
|
| 2829 | + |
|
| 2830 | + /** |
|
| 2831 | + * @return list<array{href: string, commonName: string, status: int, readOnly: bool, '{http://owncloud.org/ns}principal': string, '{http://owncloud.org/ns}group-share': bool}> |
|
| 2832 | + */ |
|
| 2833 | + public function getShares(int $resourceId): array { |
|
| 2834 | + return $this->calendarSharingBackend->getShares($resourceId); |
|
| 2835 | + } |
|
| 2836 | + |
|
| 2837 | + /** |
|
| 2838 | + * @param boolean $value |
|
| 2839 | + * @param \OCA\DAV\CalDAV\Calendar $calendar |
|
| 2840 | + * @return string|null |
|
| 2841 | + */ |
|
| 2842 | + public function setPublishStatus($value, $calendar) { |
|
| 2843 | + $calendarId = $calendar->getResourceId(); |
|
| 2844 | + $calendarData = $this->getCalendarById($calendarId); |
|
| 2845 | + |
|
| 2846 | + $query = $this->db->getQueryBuilder(); |
|
| 2847 | + if ($value) { |
|
| 2848 | + $publicUri = $this->random->generate(16, ISecureRandom::CHAR_HUMAN_READABLE); |
|
| 2849 | + $query->insert('dav_shares') |
|
| 2850 | + ->values([ |
|
| 2851 | + 'principaluri' => $query->createNamedParameter($calendar->getPrincipalURI()), |
|
| 2852 | + 'type' => $query->createNamedParameter('calendar'), |
|
| 2853 | + 'access' => $query->createNamedParameter(self::ACCESS_PUBLIC), |
|
| 2854 | + 'resourceid' => $query->createNamedParameter($calendar->getResourceId()), |
|
| 2855 | + 'publicuri' => $query->createNamedParameter($publicUri) |
|
| 2856 | + ]); |
|
| 2857 | + $query->executeStatement(); |
|
| 2858 | + |
|
| 2859 | + $this->dispatcher->dispatchTyped(new CalendarPublishedEvent($calendarId, $calendarData, $publicUri)); |
|
| 2860 | + return $publicUri; |
|
| 2861 | + } |
|
| 2862 | + $query->delete('dav_shares') |
|
| 2863 | + ->where($query->expr()->eq('resourceid', $query->createNamedParameter($calendar->getResourceId()))) |
|
| 2864 | + ->andWhere($query->expr()->eq('access', $query->createNamedParameter(self::ACCESS_PUBLIC))); |
|
| 2865 | + $query->executeStatement(); |
|
| 2866 | + |
|
| 2867 | + $this->dispatcher->dispatchTyped(new CalendarUnpublishedEvent($calendarId, $calendarData)); |
|
| 2868 | + return null; |
|
| 2869 | + } |
|
| 2870 | + |
|
| 2871 | + /** |
|
| 2872 | + * @param \OCA\DAV\CalDAV\Calendar $calendar |
|
| 2873 | + * @return mixed |
|
| 2874 | + */ |
|
| 2875 | + public function getPublishStatus($calendar) { |
|
| 2876 | + $query = $this->db->getQueryBuilder(); |
|
| 2877 | + $result = $query->select('publicuri') |
|
| 2878 | + ->from('dav_shares') |
|
| 2879 | + ->where($query->expr()->eq('resourceid', $query->createNamedParameter($calendar->getResourceId()))) |
|
| 2880 | + ->andWhere($query->expr()->eq('access', $query->createNamedParameter(self::ACCESS_PUBLIC))) |
|
| 2881 | + ->executeQuery(); |
|
| 2882 | + |
|
| 2883 | + $row = $result->fetch(); |
|
| 2884 | + $result->closeCursor(); |
|
| 2885 | + return $row ? reset($row) : false; |
|
| 2886 | + } |
|
| 2887 | + |
|
| 2888 | + /** |
|
| 2889 | + * @param int $resourceId |
|
| 2890 | + * @param list<array{privilege: string, principal: string, protected: bool}> $acl |
|
| 2891 | + * @return list<array{privilege: string, principal: string, protected: bool}> |
|
| 2892 | + */ |
|
| 2893 | + public function applyShareAcl(int $resourceId, array $acl): array { |
|
| 2894 | + return $this->calendarSharingBackend->applyShareAcl($resourceId, $acl); |
|
| 2895 | + } |
|
| 2896 | + |
|
| 2897 | + /** |
|
| 2898 | + * update properties table |
|
| 2899 | + * |
|
| 2900 | + * @param int $calendarId |
|
| 2901 | + * @param string $objectUri |
|
| 2902 | + * @param string $calendarData |
|
| 2903 | + * @param int $calendarType |
|
| 2904 | + */ |
|
| 2905 | + public function updateProperties($calendarId, $objectUri, $calendarData, $calendarType = self::CALENDAR_TYPE_CALENDAR) { |
|
| 2906 | + $objectId = $this->getCalendarObjectId($calendarId, $objectUri, $calendarType); |
|
| 2907 | + |
|
| 2908 | + try { |
|
| 2909 | + $vCalendar = $this->readCalendarData($calendarData); |
|
| 2910 | + } catch (\Exception $ex) { |
|
| 2911 | + return; |
|
| 2912 | + } |
|
| 2913 | + |
|
| 2914 | + $this->purgeProperties($calendarId, $objectId); |
|
| 2915 | + |
|
| 2916 | + $query = $this->db->getQueryBuilder(); |
|
| 2917 | + $query->insert($this->dbObjectPropertiesTable) |
|
| 2918 | + ->values( |
|
| 2919 | + [ |
|
| 2920 | + 'calendarid' => $query->createNamedParameter($calendarId), |
|
| 2921 | + 'calendartype' => $query->createNamedParameter($calendarType), |
|
| 2922 | + 'objectid' => $query->createNamedParameter($objectId), |
|
| 2923 | + 'name' => $query->createParameter('name'), |
|
| 2924 | + 'parameter' => $query->createParameter('parameter'), |
|
| 2925 | + 'value' => $query->createParameter('value'), |
|
| 2926 | + ] |
|
| 2927 | + ); |
|
| 2928 | + |
|
| 2929 | + $indexComponents = ['VEVENT', 'VJOURNAL', 'VTODO']; |
|
| 2930 | + foreach ($vCalendar->getComponents() as $component) { |
|
| 2931 | + if (!in_array($component->name, $indexComponents)) { |
|
| 2932 | + continue; |
|
| 2933 | + } |
|
| 2934 | + |
|
| 2935 | + foreach ($component->children() as $property) { |
|
| 2936 | + if (in_array($property->name, self::INDEXED_PROPERTIES, true)) { |
|
| 2937 | + $value = $property->getValue(); |
|
| 2938 | + // is this a shitty db? |
|
| 2939 | + if (!$this->db->supports4ByteText()) { |
|
| 2940 | + $value = preg_replace('/[\x{10000}-\x{10FFFF}]/u', "\xEF\xBF\xBD", $value); |
|
| 2941 | + } |
|
| 2942 | + $value = mb_strcut($value, 0, 254); |
|
| 2943 | + |
|
| 2944 | + $query->setParameter('name', $property->name); |
|
| 2945 | + $query->setParameter('parameter', null); |
|
| 2946 | + $query->setParameter('value', $value); |
|
| 2947 | + $query->executeStatement(); |
|
| 2948 | + } |
|
| 2949 | + |
|
| 2950 | + if (array_key_exists($property->name, self::$indexParameters)) { |
|
| 2951 | + $parameters = $property->parameters(); |
|
| 2952 | + $indexedParametersForProperty = self::$indexParameters[$property->name]; |
|
| 2953 | + |
|
| 2954 | + foreach ($parameters as $key => $value) { |
|
| 2955 | + if (in_array($key, $indexedParametersForProperty)) { |
|
| 2956 | + // is this a shitty db? |
|
| 2957 | + if ($this->db->supports4ByteText()) { |
|
| 2958 | + $value = preg_replace('/[\x{10000}-\x{10FFFF}]/u', "\xEF\xBF\xBD", $value); |
|
| 2959 | + } |
|
| 2960 | + |
|
| 2961 | + $query->setParameter('name', $property->name); |
|
| 2962 | + $query->setParameter('parameter', mb_strcut($key, 0, 254)); |
|
| 2963 | + $query->setParameter('value', mb_strcut($value, 0, 254)); |
|
| 2964 | + $query->executeStatement(); |
|
| 2965 | + } |
|
| 2966 | + } |
|
| 2967 | + } |
|
| 2968 | + } |
|
| 2969 | + } |
|
| 2970 | + } |
|
| 2971 | + |
|
| 2972 | + /** |
|
| 2973 | + * deletes all birthday calendars |
|
| 2974 | + */ |
|
| 2975 | + public function deleteAllBirthdayCalendars() { |
|
| 2976 | + $query = $this->db->getQueryBuilder(); |
|
| 2977 | + $result = $query->select(['id'])->from('calendars') |
|
| 2978 | + ->where($query->expr()->eq('uri', $query->createNamedParameter(BirthdayService::BIRTHDAY_CALENDAR_URI))) |
|
| 2979 | + ->executeQuery(); |
|
| 2980 | + |
|
| 2981 | + $ids = $result->fetchAll(); |
|
| 2982 | + $result->closeCursor(); |
|
| 2983 | + foreach ($ids as $id) { |
|
| 2984 | + $this->deleteCalendar( |
|
| 2985 | + $id['id'], |
|
| 2986 | + true // No data to keep in the trashbin, if the user re-enables then we regenerate |
|
| 2987 | + ); |
|
| 2988 | + } |
|
| 2989 | + } |
|
| 2990 | + |
|
| 2991 | + /** |
|
| 2992 | + * @param $subscriptionId |
|
| 2993 | + */ |
|
| 2994 | + public function purgeAllCachedEventsForSubscription($subscriptionId) { |
|
| 2995 | + $query = $this->db->getQueryBuilder(); |
|
| 2996 | + $query->select('uri') |
|
| 2997 | + ->from('calendarobjects') |
|
| 2998 | + ->where($query->expr()->eq('calendarid', $query->createNamedParameter($subscriptionId))) |
|
| 2999 | + ->andWhere($query->expr()->eq('calendartype', $query->createNamedParameter(self::CALENDAR_TYPE_SUBSCRIPTION))); |
|
| 3000 | + $stmt = $query->executeQuery(); |
|
| 3001 | + |
|
| 3002 | + $uris = []; |
|
| 3003 | + foreach ($stmt->fetchAll() as $row) { |
|
| 3004 | + $uris[] = $row['uri']; |
|
| 3005 | + } |
|
| 3006 | + $stmt->closeCursor(); |
|
| 3007 | + |
|
| 3008 | + $query = $this->db->getQueryBuilder(); |
|
| 3009 | + $query->delete('calendarobjects') |
|
| 3010 | + ->where($query->expr()->eq('calendarid', $query->createNamedParameter($subscriptionId))) |
|
| 3011 | + ->andWhere($query->expr()->eq('calendartype', $query->createNamedParameter(self::CALENDAR_TYPE_SUBSCRIPTION))) |
|
| 3012 | + ->executeStatement(); |
|
| 3013 | + |
|
| 3014 | + $query->delete('calendarchanges') |
|
| 3015 | + ->where($query->expr()->eq('calendarid', $query->createNamedParameter($subscriptionId))) |
|
| 3016 | + ->andWhere($query->expr()->eq('calendartype', $query->createNamedParameter(self::CALENDAR_TYPE_SUBSCRIPTION))) |
|
| 3017 | + ->executeStatement(); |
|
| 3018 | + |
|
| 3019 | + $query->delete($this->dbObjectPropertiesTable) |
|
| 3020 | + ->where($query->expr()->eq('calendarid', $query->createNamedParameter($subscriptionId))) |
|
| 3021 | + ->andWhere($query->expr()->eq('calendartype', $query->createNamedParameter(self::CALENDAR_TYPE_SUBSCRIPTION))) |
|
| 3022 | + ->executeStatement(); |
|
| 3023 | + |
|
| 3024 | + foreach ($uris as $uri) { |
|
| 3025 | + $this->addChange($subscriptionId, $uri, 3, self::CALENDAR_TYPE_SUBSCRIPTION); |
|
| 3026 | + } |
|
| 3027 | + } |
|
| 3028 | + |
|
| 3029 | + /** |
|
| 3030 | + * Move a calendar from one user to another |
|
| 3031 | + * |
|
| 3032 | + * @param string $uriName |
|
| 3033 | + * @param string $uriOrigin |
|
| 3034 | + * @param string $uriDestination |
|
| 3035 | + * @param string $newUriName (optional) the new uriName |
|
| 3036 | + */ |
|
| 3037 | + public function moveCalendar($uriName, $uriOrigin, $uriDestination, $newUriName = null) { |
|
| 3038 | + $query = $this->db->getQueryBuilder(); |
|
| 3039 | + $query->update('calendars') |
|
| 3040 | + ->set('principaluri', $query->createNamedParameter($uriDestination)) |
|
| 3041 | + ->set('uri', $query->createNamedParameter($newUriName ?: $uriName)) |
|
| 3042 | + ->where($query->expr()->eq('principaluri', $query->createNamedParameter($uriOrigin))) |
|
| 3043 | + ->andWhere($query->expr()->eq('uri', $query->createNamedParameter($uriName))) |
|
| 3044 | + ->executeStatement(); |
|
| 3045 | + } |
|
| 3046 | + |
|
| 3047 | + /** |
|
| 3048 | + * read VCalendar data into a VCalendar object |
|
| 3049 | + * |
|
| 3050 | + * @param string $objectData |
|
| 3051 | + * @return VCalendar |
|
| 3052 | + */ |
|
| 3053 | + protected function readCalendarData($objectData) { |
|
| 3054 | + return Reader::read($objectData); |
|
| 3055 | + } |
|
| 3056 | + |
|
| 3057 | + /** |
|
| 3058 | + * delete all properties from a given calendar object |
|
| 3059 | + * |
|
| 3060 | + * @param int $calendarId |
|
| 3061 | + * @param int $objectId |
|
| 3062 | + */ |
|
| 3063 | + protected function purgeProperties($calendarId, $objectId) { |
|
| 3064 | + $query = $this->db->getQueryBuilder(); |
|
| 3065 | + $query->delete($this->dbObjectPropertiesTable) |
|
| 3066 | + ->where($query->expr()->eq('objectid', $query->createNamedParameter($objectId))) |
|
| 3067 | + ->andWhere($query->expr()->eq('calendarid', $query->createNamedParameter($calendarId))); |
|
| 3068 | + $query->executeStatement(); |
|
| 3069 | + } |
|
| 3070 | + |
|
| 3071 | + /** |
|
| 3072 | + * get ID from a given calendar object |
|
| 3073 | + * |
|
| 3074 | + * @param int $calendarId |
|
| 3075 | + * @param string $uri |
|
| 3076 | + * @param int $calendarType |
|
| 3077 | + * @return int |
|
| 3078 | + */ |
|
| 3079 | + protected function getCalendarObjectId($calendarId, $uri, $calendarType):int { |
|
| 3080 | + $query = $this->db->getQueryBuilder(); |
|
| 3081 | + $query->select('id') |
|
| 3082 | + ->from('calendarobjects') |
|
| 3083 | + ->where($query->expr()->eq('uri', $query->createNamedParameter($uri))) |
|
| 3084 | + ->andWhere($query->expr()->eq('calendarid', $query->createNamedParameter($calendarId))) |
|
| 3085 | + ->andWhere($query->expr()->eq('calendartype', $query->createNamedParameter($calendarType))); |
|
| 3086 | + |
|
| 3087 | + $result = $query->executeQuery(); |
|
| 3088 | + $objectIds = $result->fetch(); |
|
| 3089 | + $result->closeCursor(); |
|
| 3090 | + |
|
| 3091 | + if (!isset($objectIds['id'])) { |
|
| 3092 | + throw new \InvalidArgumentException('Calendarobject does not exists: ' . $uri); |
|
| 3093 | + } |
|
| 3094 | + |
|
| 3095 | + return (int)$objectIds['id']; |
|
| 3096 | + } |
|
| 3097 | + |
|
| 3098 | + /** |
|
| 3099 | + * return legacy endpoint principal name to new principal name |
|
| 3100 | + * |
|
| 3101 | + * @param $principalUri |
|
| 3102 | + * @param $toV2 |
|
| 3103 | + * @return string |
|
| 3104 | + */ |
|
| 3105 | + private function convertPrincipal($principalUri, $toV2) { |
|
| 3106 | + if ($this->principalBackend->getPrincipalPrefix() === 'principals') { |
|
| 3107 | + [, $name] = Uri\split($principalUri); |
|
| 3108 | + if ($toV2 === true) { |
|
| 3109 | + return "principals/users/$name"; |
|
| 3110 | + } |
|
| 3111 | + return "principals/$name"; |
|
| 3112 | + } |
|
| 3113 | + return $principalUri; |
|
| 3114 | + } |
|
| 3115 | + |
|
| 3116 | + /** |
|
| 3117 | + * adds information about an owner to the calendar data |
|
| 3118 | + * |
|
| 3119 | + */ |
|
| 3120 | + private function addOwnerPrincipalToCalendar(array $calendarInfo): array { |
|
| 3121 | + $ownerPrincipalKey = '{' . \OCA\DAV\DAV\Sharing\Plugin::NS_OWNCLOUD . '}owner-principal'; |
|
| 3122 | + $displaynameKey = '{' . \OCA\DAV\DAV\Sharing\Plugin::NS_NEXTCLOUD . '}owner-displayname'; |
|
| 3123 | + if (isset($calendarInfo[$ownerPrincipalKey])) { |
|
| 3124 | + $uri = $calendarInfo[$ownerPrincipalKey]; |
|
| 3125 | + } else { |
|
| 3126 | + $uri = $calendarInfo['principaluri']; |
|
| 3127 | + } |
|
| 3128 | + |
|
| 3129 | + $principalInformation = $this->principalBackend->getPrincipalByPath($uri); |
|
| 3130 | + if (isset($principalInformation['{DAV:}displayname'])) { |
|
| 3131 | + $calendarInfo[$displaynameKey] = $principalInformation['{DAV:}displayname']; |
|
| 3132 | + } |
|
| 3133 | + return $calendarInfo; |
|
| 3134 | + } |
|
| 3135 | + |
|
| 3136 | + private function addResourceTypeToCalendar(array $row, array $calendar): array { |
|
| 3137 | + if (isset($row['deleted_at'])) { |
|
| 3138 | + // Columns is set and not null -> this is a deleted calendar |
|
| 3139 | + // we send a custom resourcetype to hide the deleted calendar |
|
| 3140 | + // from ordinary DAV clients, but the Calendar app will know |
|
| 3141 | + // how to handle this special resource. |
|
| 3142 | + $calendar['{DAV:}resourcetype'] = new DAV\Xml\Property\ResourceType([ |
|
| 3143 | + '{DAV:}collection', |
|
| 3144 | + sprintf('{%s}deleted-calendar', \OCA\DAV\DAV\Sharing\Plugin::NS_NEXTCLOUD), |
|
| 3145 | + ]); |
|
| 3146 | + } |
|
| 3147 | + return $calendar; |
|
| 3148 | + } |
|
| 3149 | + |
|
| 3150 | + /** |
|
| 3151 | + * Amend the calendar info with database row data |
|
| 3152 | + * |
|
| 3153 | + * @param array $row |
|
| 3154 | + * @param array $calendar |
|
| 3155 | + * |
|
| 3156 | + * @return array |
|
| 3157 | + */ |
|
| 3158 | + private function rowToCalendar($row, array $calendar): array { |
|
| 3159 | + foreach ($this->propertyMap as $xmlName => [$dbName, $type]) { |
|
| 3160 | + $value = $row[$dbName]; |
|
| 3161 | + if ($value !== null) { |
|
| 3162 | + settype($value, $type); |
|
| 3163 | + } |
|
| 3164 | + $calendar[$xmlName] = $value; |
|
| 3165 | + } |
|
| 3166 | + return $calendar; |
|
| 3167 | + } |
|
| 3168 | + |
|
| 3169 | + /** |
|
| 3170 | + * Amend the subscription info with database row data |
|
| 3171 | + * |
|
| 3172 | + * @param array $row |
|
| 3173 | + * @param array $subscription |
|
| 3174 | + * |
|
| 3175 | + * @return array |
|
| 3176 | + */ |
|
| 3177 | + private function rowToSubscription($row, array $subscription): array { |
|
| 3178 | + foreach ($this->subscriptionPropertyMap as $xmlName => [$dbName, $type]) { |
|
| 3179 | + $value = $row[$dbName]; |
|
| 3180 | + if ($value !== null) { |
|
| 3181 | + settype($value, $type); |
|
| 3182 | + } |
|
| 3183 | + $subscription[$xmlName] = $value; |
|
| 3184 | + } |
|
| 3185 | + return $subscription; |
|
| 3186 | + } |
|
| 3187 | 3187 | } |