@@ -34,1430 +34,1430 @@ |
||
| 34 | 34 | use Sabre\VObject\Reader; |
| 35 | 35 | |
| 36 | 36 | class CardDavBackend implements BackendInterface, SyncSupport { |
| 37 | - use TTransactional; |
|
| 38 | - public const PERSONAL_ADDRESSBOOK_URI = 'contacts'; |
|
| 39 | - public const PERSONAL_ADDRESSBOOK_NAME = 'Contacts'; |
|
| 40 | - |
|
| 41 | - private string $dbCardsTable = 'cards'; |
|
| 42 | - private string $dbCardsPropertiesTable = 'cards_properties'; |
|
| 43 | - |
|
| 44 | - /** @var array properties to index */ |
|
| 45 | - public static array $indexProperties = [ |
|
| 46 | - 'BDAY', 'UID', 'N', 'FN', 'TITLE', 'ROLE', 'NOTE', 'NICKNAME', |
|
| 47 | - 'ORG', 'CATEGORIES', 'EMAIL', 'TEL', 'IMPP', 'ADR', 'URL', 'GEO', |
|
| 48 | - 'CLOUD', 'X-SOCIALPROFILE']; |
|
| 49 | - |
|
| 50 | - /** |
|
| 51 | - * @var string[] Map of uid => display name |
|
| 52 | - */ |
|
| 53 | - protected array $userDisplayNames; |
|
| 54 | - private array $etagCache = []; |
|
| 55 | - |
|
| 56 | - public function __construct( |
|
| 57 | - private IDBConnection $db, |
|
| 58 | - private Principal $principalBackend, |
|
| 59 | - private IUserManager $userManager, |
|
| 60 | - private IEventDispatcher $dispatcher, |
|
| 61 | - private Sharing\Backend $sharingBackend, |
|
| 62 | - ) { |
|
| 63 | - } |
|
| 64 | - |
|
| 65 | - /** |
|
| 66 | - * Return the number of address books for a principal |
|
| 67 | - * |
|
| 68 | - * @param $principalUri |
|
| 69 | - * @return int |
|
| 70 | - */ |
|
| 71 | - public function getAddressBooksForUserCount($principalUri) { |
|
| 72 | - $principalUri = $this->convertPrincipal($principalUri, true); |
|
| 73 | - $query = $this->db->getQueryBuilder(); |
|
| 74 | - $query->select($query->func()->count('*')) |
|
| 75 | - ->from('addressbooks') |
|
| 76 | - ->where($query->expr()->eq('principaluri', $query->createNamedParameter($principalUri))); |
|
| 77 | - |
|
| 78 | - $result = $query->executeQuery(); |
|
| 79 | - $column = (int)$result->fetchOne(); |
|
| 80 | - $result->closeCursor(); |
|
| 81 | - return $column; |
|
| 82 | - } |
|
| 83 | - |
|
| 84 | - /** |
|
| 85 | - * Returns the list of address books for a specific user. |
|
| 86 | - * |
|
| 87 | - * Every addressbook should have the following properties: |
|
| 88 | - * id - an arbitrary unique id |
|
| 89 | - * uri - the 'basename' part of the url |
|
| 90 | - * principaluri - Same as the passed parameter |
|
| 91 | - * |
|
| 92 | - * Any additional clark-notation property may be passed besides this. Some |
|
| 93 | - * common ones are : |
|
| 94 | - * {DAV:}displayname |
|
| 95 | - * {urn:ietf:params:xml:ns:carddav}addressbook-description |
|
| 96 | - * {http://calendarserver.org/ns/}getctag |
|
| 97 | - * |
|
| 98 | - * @param string $principalUri |
|
| 99 | - * @return array |
|
| 100 | - */ |
|
| 101 | - public function getAddressBooksForUser($principalUri) { |
|
| 102 | - return $this->atomic(function () use ($principalUri) { |
|
| 103 | - $principalUriOriginal = $principalUri; |
|
| 104 | - $principalUri = $this->convertPrincipal($principalUri, true); |
|
| 105 | - $select = $this->db->getQueryBuilder(); |
|
| 106 | - $select->select(['id', 'uri', 'displayname', 'principaluri', 'description', 'synctoken']) |
|
| 107 | - ->from('addressbooks') |
|
| 108 | - ->where($select->expr()->eq('principaluri', $select->createNamedParameter($principalUri))); |
|
| 109 | - |
|
| 110 | - $addressBooks = []; |
|
| 111 | - |
|
| 112 | - $result = $select->executeQuery(); |
|
| 113 | - while ($row = $result->fetch()) { |
|
| 114 | - $addressBooks[$row['id']] = [ |
|
| 115 | - 'id' => $row['id'], |
|
| 116 | - 'uri' => $row['uri'], |
|
| 117 | - 'principaluri' => $this->convertPrincipal($row['principaluri'], false), |
|
| 118 | - '{DAV:}displayname' => $row['displayname'], |
|
| 119 | - '{' . Plugin::NS_CARDDAV . '}addressbook-description' => $row['description'], |
|
| 120 | - '{http://calendarserver.org/ns/}getctag' => $row['synctoken'], |
|
| 121 | - '{http://sabredav.org/ns}sync-token' => $row['synctoken'] ?: '0', |
|
| 122 | - ]; |
|
| 123 | - |
|
| 124 | - $this->addOwnerPrincipal($addressBooks[$row['id']]); |
|
| 125 | - } |
|
| 126 | - $result->closeCursor(); |
|
| 127 | - |
|
| 128 | - // query for shared addressbooks |
|
| 129 | - $principals = $this->principalBackend->getGroupMembership($principalUriOriginal, true); |
|
| 130 | - $principals = array_merge($principals, $this->principalBackend->getCircleMembership($principalUriOriginal)); |
|
| 131 | - |
|
| 132 | - $principals[] = $principalUri; |
|
| 133 | - |
|
| 134 | - $select = $this->db->getQueryBuilder(); |
|
| 135 | - $subSelect = $this->db->getQueryBuilder(); |
|
| 136 | - |
|
| 137 | - $subSelect->select('id') |
|
| 138 | - ->from('dav_shares', 'd') |
|
| 139 | - ->where($subSelect->expr()->eq('d.access', $select->createNamedParameter(\OCA\DAV\CardDAV\Sharing\Backend::ACCESS_UNSHARED, IQueryBuilder::PARAM_INT), IQueryBuilder::PARAM_INT)) |
|
| 140 | - ->andWhere($subSelect->expr()->eq('d.principaluri', $select->createNamedParameter($principalUri, IQueryBuilder::PARAM_STR), IQueryBuilder::PARAM_STR)); |
|
| 141 | - |
|
| 142 | - |
|
| 143 | - $select->select(['a.id', 'a.uri', 'a.displayname', 'a.principaluri', 'a.description', 'a.synctoken', 's.access']) |
|
| 144 | - ->from('dav_shares', 's') |
|
| 145 | - ->join('s', 'addressbooks', 'a', $select->expr()->eq('s.resourceid', 'a.id')) |
|
| 146 | - ->where($select->expr()->in('s.principaluri', $select->createNamedParameter($principals, IQueryBuilder::PARAM_STR_ARRAY))) |
|
| 147 | - ->andWhere($select->expr()->eq('s.type', $select->createNamedParameter('addressbook', IQueryBuilder::PARAM_STR))) |
|
| 148 | - ->andWhere($select->expr()->notIn('s.id', $select->createFunction($subSelect->getSQL()), IQueryBuilder::PARAM_INT_ARRAY)); |
|
| 149 | - $result = $select->executeQuery(); |
|
| 150 | - |
|
| 151 | - $readOnlyPropertyName = '{' . \OCA\DAV\DAV\Sharing\Plugin::NS_OWNCLOUD . '}read-only'; |
|
| 152 | - while ($row = $result->fetch()) { |
|
| 153 | - if ($row['principaluri'] === $principalUri) { |
|
| 154 | - continue; |
|
| 155 | - } |
|
| 156 | - |
|
| 157 | - $readOnly = (int)$row['access'] === Backend::ACCESS_READ; |
|
| 158 | - if (isset($addressBooks[$row['id']])) { |
|
| 159 | - if ($readOnly) { |
|
| 160 | - // New share can not have more permissions then the old one. |
|
| 161 | - continue; |
|
| 162 | - } |
|
| 163 | - if (isset($addressBooks[$row['id']][$readOnlyPropertyName]) && |
|
| 164 | - $addressBooks[$row['id']][$readOnlyPropertyName] === 0) { |
|
| 165 | - // Old share is already read-write, no more permissions can be gained |
|
| 166 | - continue; |
|
| 167 | - } |
|
| 168 | - } |
|
| 169 | - |
|
| 170 | - [, $name] = \Sabre\Uri\split($row['principaluri']); |
|
| 171 | - $uri = $row['uri'] . '_shared_by_' . $name; |
|
| 172 | - $displayName = $row['displayname'] . ' (' . ($this->userManager->getDisplayName($name) ?? $name ?? '') . ')'; |
|
| 173 | - |
|
| 174 | - $addressBooks[$row['id']] = [ |
|
| 175 | - 'id' => $row['id'], |
|
| 176 | - 'uri' => $uri, |
|
| 177 | - 'principaluri' => $principalUriOriginal, |
|
| 178 | - '{DAV:}displayname' => $displayName, |
|
| 179 | - '{' . Plugin::NS_CARDDAV . '}addressbook-description' => $row['description'], |
|
| 180 | - '{http://calendarserver.org/ns/}getctag' => $row['synctoken'], |
|
| 181 | - '{http://sabredav.org/ns}sync-token' => $row['synctoken'] ?: '0', |
|
| 182 | - '{' . \OCA\DAV\DAV\Sharing\Plugin::NS_OWNCLOUD . '}owner-principal' => $row['principaluri'], |
|
| 183 | - $readOnlyPropertyName => $readOnly, |
|
| 184 | - ]; |
|
| 185 | - |
|
| 186 | - $this->addOwnerPrincipal($addressBooks[$row['id']]); |
|
| 187 | - } |
|
| 188 | - $result->closeCursor(); |
|
| 189 | - |
|
| 190 | - return array_values($addressBooks); |
|
| 191 | - }, $this->db); |
|
| 192 | - } |
|
| 193 | - |
|
| 194 | - public function getUsersOwnAddressBooks($principalUri) { |
|
| 195 | - $principalUri = $this->convertPrincipal($principalUri, true); |
|
| 196 | - $query = $this->db->getQueryBuilder(); |
|
| 197 | - $query->select(['id', 'uri', 'displayname', 'principaluri', 'description', 'synctoken']) |
|
| 198 | - ->from('addressbooks') |
|
| 199 | - ->where($query->expr()->eq('principaluri', $query->createNamedParameter($principalUri))); |
|
| 200 | - |
|
| 201 | - $addressBooks = []; |
|
| 202 | - |
|
| 203 | - $result = $query->executeQuery(); |
|
| 204 | - while ($row = $result->fetch()) { |
|
| 205 | - $addressBooks[$row['id']] = [ |
|
| 206 | - 'id' => $row['id'], |
|
| 207 | - 'uri' => $row['uri'], |
|
| 208 | - 'principaluri' => $this->convertPrincipal($row['principaluri'], false), |
|
| 209 | - '{DAV:}displayname' => $row['displayname'], |
|
| 210 | - '{' . Plugin::NS_CARDDAV . '}addressbook-description' => $row['description'], |
|
| 211 | - '{http://calendarserver.org/ns/}getctag' => $row['synctoken'], |
|
| 212 | - '{http://sabredav.org/ns}sync-token' => $row['synctoken'] ?: '0', |
|
| 213 | - ]; |
|
| 214 | - |
|
| 215 | - $this->addOwnerPrincipal($addressBooks[$row['id']]); |
|
| 216 | - } |
|
| 217 | - $result->closeCursor(); |
|
| 218 | - |
|
| 219 | - return array_values($addressBooks); |
|
| 220 | - } |
|
| 221 | - |
|
| 222 | - /** |
|
| 223 | - * @param int $addressBookId |
|
| 224 | - */ |
|
| 225 | - public function getAddressBookById(int $addressBookId): ?array { |
|
| 226 | - $query = $this->db->getQueryBuilder(); |
|
| 227 | - $result = $query->select(['id', 'uri', 'displayname', 'principaluri', 'description', 'synctoken']) |
|
| 228 | - ->from('addressbooks') |
|
| 229 | - ->where($query->expr()->eq('id', $query->createNamedParameter($addressBookId, IQueryBuilder::PARAM_INT))) |
|
| 230 | - ->executeQuery(); |
|
| 231 | - $row = $result->fetch(); |
|
| 232 | - $result->closeCursor(); |
|
| 233 | - if (!$row) { |
|
| 234 | - return null; |
|
| 235 | - } |
|
| 236 | - |
|
| 237 | - $addressBook = [ |
|
| 238 | - 'id' => $row['id'], |
|
| 239 | - 'uri' => $row['uri'], |
|
| 240 | - 'principaluri' => $row['principaluri'], |
|
| 241 | - '{DAV:}displayname' => $row['displayname'], |
|
| 242 | - '{' . Plugin::NS_CARDDAV . '}addressbook-description' => $row['description'], |
|
| 243 | - '{http://calendarserver.org/ns/}getctag' => $row['synctoken'], |
|
| 244 | - '{http://sabredav.org/ns}sync-token' => $row['synctoken'] ?: '0', |
|
| 245 | - ]; |
|
| 246 | - |
|
| 247 | - $this->addOwnerPrincipal($addressBook); |
|
| 248 | - |
|
| 249 | - return $addressBook; |
|
| 250 | - } |
|
| 251 | - |
|
| 252 | - public function getAddressBooksByUri(string $principal, string $addressBookUri): ?array { |
|
| 253 | - $query = $this->db->getQueryBuilder(); |
|
| 254 | - $result = $query->select(['id', 'uri', 'displayname', 'principaluri', 'description', 'synctoken']) |
|
| 255 | - ->from('addressbooks') |
|
| 256 | - ->where($query->expr()->eq('uri', $query->createNamedParameter($addressBookUri))) |
|
| 257 | - ->andWhere($query->expr()->eq('principaluri', $query->createNamedParameter($principal))) |
|
| 258 | - ->setMaxResults(1) |
|
| 259 | - ->executeQuery(); |
|
| 260 | - |
|
| 261 | - $row = $result->fetch(); |
|
| 262 | - $result->closeCursor(); |
|
| 263 | - if ($row === false) { |
|
| 264 | - return null; |
|
| 265 | - } |
|
| 266 | - |
|
| 267 | - $addressBook = [ |
|
| 268 | - 'id' => $row['id'], |
|
| 269 | - 'uri' => $row['uri'], |
|
| 270 | - 'principaluri' => $row['principaluri'], |
|
| 271 | - '{DAV:}displayname' => $row['displayname'], |
|
| 272 | - '{' . Plugin::NS_CARDDAV . '}addressbook-description' => $row['description'], |
|
| 273 | - '{http://calendarserver.org/ns/}getctag' => $row['synctoken'], |
|
| 274 | - '{http://sabredav.org/ns}sync-token' => $row['synctoken'] ?: '0', |
|
| 275 | - |
|
| 276 | - ]; |
|
| 277 | - |
|
| 278 | - // system address books are always read only |
|
| 279 | - if ($principal === 'principals/system/system') { |
|
| 280 | - $addressBook['{' . \OCA\DAV\DAV\Sharing\Plugin::NS_OWNCLOUD . '}owner-principal'] = $row['principaluri']; |
|
| 281 | - $addressBook['{' . \OCA\DAV\DAV\Sharing\Plugin::NS_OWNCLOUD . '}read-only'] = true; |
|
| 282 | - } |
|
| 283 | - |
|
| 284 | - $this->addOwnerPrincipal($addressBook); |
|
| 285 | - |
|
| 286 | - return $addressBook; |
|
| 287 | - } |
|
| 288 | - |
|
| 289 | - /** |
|
| 290 | - * Updates properties for an address book. |
|
| 291 | - * |
|
| 292 | - * The list of mutations is stored in a Sabre\DAV\PropPatch object. |
|
| 293 | - * To do the actual updates, you must tell this object which properties |
|
| 294 | - * you're going to process with the handle() method. |
|
| 295 | - * |
|
| 296 | - * Calling the handle method is like telling the PropPatch object "I |
|
| 297 | - * promise I can handle updating this property". |
|
| 298 | - * |
|
| 299 | - * Read the PropPatch documentation for more info and examples. |
|
| 300 | - * |
|
| 301 | - * @param string $addressBookId |
|
| 302 | - * @param \Sabre\DAV\PropPatch $propPatch |
|
| 303 | - * @return void |
|
| 304 | - */ |
|
| 305 | - public function updateAddressBook($addressBookId, \Sabre\DAV\PropPatch $propPatch) { |
|
| 306 | - $supportedProperties = [ |
|
| 307 | - '{DAV:}displayname', |
|
| 308 | - '{' . Plugin::NS_CARDDAV . '}addressbook-description', |
|
| 309 | - ]; |
|
| 310 | - |
|
| 311 | - $propPatch->handle($supportedProperties, function ($mutations) use ($addressBookId) { |
|
| 312 | - $updates = []; |
|
| 313 | - foreach ($mutations as $property => $newValue) { |
|
| 314 | - switch ($property) { |
|
| 315 | - case '{DAV:}displayname': |
|
| 316 | - $updates['displayname'] = $newValue; |
|
| 317 | - break; |
|
| 318 | - case '{' . Plugin::NS_CARDDAV . '}addressbook-description': |
|
| 319 | - $updates['description'] = $newValue; |
|
| 320 | - break; |
|
| 321 | - } |
|
| 322 | - } |
|
| 323 | - [$addressBookRow, $shares] = $this->atomic(function () use ($addressBookId, $updates) { |
|
| 324 | - $query = $this->db->getQueryBuilder(); |
|
| 325 | - $query->update('addressbooks'); |
|
| 326 | - |
|
| 327 | - foreach ($updates as $key => $value) { |
|
| 328 | - $query->set($key, $query->createNamedParameter($value)); |
|
| 329 | - } |
|
| 330 | - $query->where($query->expr()->eq('id', $query->createNamedParameter($addressBookId))) |
|
| 331 | - ->executeStatement(); |
|
| 332 | - |
|
| 333 | - $this->addChange($addressBookId, '', 2); |
|
| 334 | - |
|
| 335 | - $addressBookRow = $this->getAddressBookById((int)$addressBookId); |
|
| 336 | - $shares = $this->getShares((int)$addressBookId); |
|
| 337 | - return [$addressBookRow, $shares]; |
|
| 338 | - }, $this->db); |
|
| 339 | - |
|
| 340 | - $this->dispatcher->dispatchTyped(new AddressBookUpdatedEvent((int)$addressBookId, $addressBookRow, $shares, $mutations)); |
|
| 341 | - |
|
| 342 | - return true; |
|
| 343 | - }); |
|
| 344 | - } |
|
| 345 | - |
|
| 346 | - /** |
|
| 347 | - * Creates a new address book |
|
| 348 | - * |
|
| 349 | - * @param string $principalUri |
|
| 350 | - * @param string $url Just the 'basename' of the url. |
|
| 351 | - * @param array $properties |
|
| 352 | - * @return int |
|
| 353 | - * @throws BadRequest |
|
| 354 | - * @throws Exception |
|
| 355 | - */ |
|
| 356 | - public function createAddressBook($principalUri, $url, array $properties) { |
|
| 357 | - if (strlen($url) > 255) { |
|
| 358 | - throw new BadRequest('URI too long. Address book not created'); |
|
| 359 | - } |
|
| 360 | - |
|
| 361 | - $values = [ |
|
| 362 | - 'displayname' => null, |
|
| 363 | - 'description' => null, |
|
| 364 | - 'principaluri' => $principalUri, |
|
| 365 | - 'uri' => $url, |
|
| 366 | - 'synctoken' => 1 |
|
| 367 | - ]; |
|
| 368 | - |
|
| 369 | - foreach ($properties as $property => $newValue) { |
|
| 370 | - switch ($property) { |
|
| 371 | - case '{DAV:}displayname': |
|
| 372 | - $values['displayname'] = $newValue; |
|
| 373 | - break; |
|
| 374 | - case '{' . Plugin::NS_CARDDAV . '}addressbook-description': |
|
| 375 | - $values['description'] = $newValue; |
|
| 376 | - break; |
|
| 377 | - default: |
|
| 378 | - throw new BadRequest('Unknown property: ' . $property); |
|
| 379 | - } |
|
| 380 | - } |
|
| 381 | - |
|
| 382 | - // Fallback to make sure the displayname is set. Some clients may refuse |
|
| 383 | - // to work with addressbooks not having a displayname. |
|
| 384 | - if (is_null($values['displayname'])) { |
|
| 385 | - $values['displayname'] = $url; |
|
| 386 | - } |
|
| 387 | - |
|
| 388 | - [$addressBookId, $addressBookRow] = $this->atomic(function () use ($values) { |
|
| 389 | - $query = $this->db->getQueryBuilder(); |
|
| 390 | - $query->insert('addressbooks') |
|
| 391 | - ->values([ |
|
| 392 | - 'uri' => $query->createParameter('uri'), |
|
| 393 | - 'displayname' => $query->createParameter('displayname'), |
|
| 394 | - 'description' => $query->createParameter('description'), |
|
| 395 | - 'principaluri' => $query->createParameter('principaluri'), |
|
| 396 | - 'synctoken' => $query->createParameter('synctoken'), |
|
| 397 | - ]) |
|
| 398 | - ->setParameters($values) |
|
| 399 | - ->executeStatement(); |
|
| 400 | - |
|
| 401 | - $addressBookId = $query->getLastInsertId(); |
|
| 402 | - return [ |
|
| 403 | - $addressBookId, |
|
| 404 | - $this->getAddressBookById($addressBookId), |
|
| 405 | - ]; |
|
| 406 | - }, $this->db); |
|
| 407 | - |
|
| 408 | - $this->dispatcher->dispatchTyped(new AddressBookCreatedEvent($addressBookId, $addressBookRow)); |
|
| 409 | - |
|
| 410 | - return $addressBookId; |
|
| 411 | - } |
|
| 412 | - |
|
| 413 | - /** |
|
| 414 | - * Deletes an entire addressbook and all its contents |
|
| 415 | - * |
|
| 416 | - * @param mixed $addressBookId |
|
| 417 | - * @return void |
|
| 418 | - */ |
|
| 419 | - public function deleteAddressBook($addressBookId) { |
|
| 420 | - $this->atomic(function () use ($addressBookId): void { |
|
| 421 | - $addressBookId = (int)$addressBookId; |
|
| 422 | - $addressBookData = $this->getAddressBookById($addressBookId); |
|
| 423 | - $shares = $this->getShares($addressBookId); |
|
| 424 | - |
|
| 425 | - $query = $this->db->getQueryBuilder(); |
|
| 426 | - $query->delete($this->dbCardsTable) |
|
| 427 | - ->where($query->expr()->eq('addressbookid', $query->createParameter('addressbookid'))) |
|
| 428 | - ->setParameter('addressbookid', $addressBookId, IQueryBuilder::PARAM_INT) |
|
| 429 | - ->executeStatement(); |
|
| 430 | - |
|
| 431 | - $query = $this->db->getQueryBuilder(); |
|
| 432 | - $query->delete('addressbookchanges') |
|
| 433 | - ->where($query->expr()->eq('addressbookid', $query->createParameter('addressbookid'))) |
|
| 434 | - ->setParameter('addressbookid', $addressBookId, IQueryBuilder::PARAM_INT) |
|
| 435 | - ->executeStatement(); |
|
| 436 | - |
|
| 437 | - $query = $this->db->getQueryBuilder(); |
|
| 438 | - $query->delete('addressbooks') |
|
| 439 | - ->where($query->expr()->eq('id', $query->createParameter('id'))) |
|
| 440 | - ->setParameter('id', $addressBookId, IQueryBuilder::PARAM_INT) |
|
| 441 | - ->executeStatement(); |
|
| 442 | - |
|
| 443 | - $this->sharingBackend->deleteAllShares($addressBookId); |
|
| 444 | - |
|
| 445 | - $query = $this->db->getQueryBuilder(); |
|
| 446 | - $query->delete($this->dbCardsPropertiesTable) |
|
| 447 | - ->where($query->expr()->eq('addressbookid', $query->createNamedParameter($addressBookId, IQueryBuilder::PARAM_INT))) |
|
| 448 | - ->executeStatement(); |
|
| 449 | - |
|
| 450 | - if ($addressBookData) { |
|
| 451 | - $this->dispatcher->dispatchTyped(new AddressBookDeletedEvent($addressBookId, $addressBookData, $shares)); |
|
| 452 | - } |
|
| 453 | - }, $this->db); |
|
| 454 | - } |
|
| 455 | - |
|
| 456 | - /** |
|
| 457 | - * Returns all cards for a specific addressbook id. |
|
| 458 | - * |
|
| 459 | - * This method should return the following properties for each card: |
|
| 460 | - * * carddata - raw vcard data |
|
| 461 | - * * uri - Some unique url |
|
| 462 | - * * lastmodified - A unix timestamp |
|
| 463 | - * |
|
| 464 | - * It's recommended to also return the following properties: |
|
| 465 | - * * etag - A unique etag. This must change every time the card changes. |
|
| 466 | - * * size - The size of the card in bytes. |
|
| 467 | - * |
|
| 468 | - * If these last two properties are provided, less time will be spent |
|
| 469 | - * calculating them. If they are specified, you can also omit carddata. |
|
| 470 | - * This may speed up certain requests, especially with large cards. |
|
| 471 | - * |
|
| 472 | - * @param mixed $addressbookId |
|
| 473 | - * @return array |
|
| 474 | - */ |
|
| 475 | - public function getCards($addressbookId) { |
|
| 476 | - $query = $this->db->getQueryBuilder(); |
|
| 477 | - $query->select(['id', 'uri', 'lastmodified', 'etag', 'size', 'carddata', 'uid']) |
|
| 478 | - ->from($this->dbCardsTable) |
|
| 479 | - ->where($query->expr()->eq('addressbookid', $query->createNamedParameter($addressbookId))); |
|
| 480 | - |
|
| 481 | - $cards = []; |
|
| 482 | - |
|
| 483 | - $result = $query->executeQuery(); |
|
| 484 | - while ($row = $result->fetch()) { |
|
| 485 | - $row['etag'] = '"' . $row['etag'] . '"'; |
|
| 486 | - |
|
| 487 | - $modified = false; |
|
| 488 | - $row['carddata'] = $this->readBlob($row['carddata'], $modified); |
|
| 489 | - if ($modified) { |
|
| 490 | - $row['size'] = strlen($row['carddata']); |
|
| 491 | - } |
|
| 492 | - |
|
| 493 | - $cards[] = $row; |
|
| 494 | - } |
|
| 495 | - $result->closeCursor(); |
|
| 496 | - |
|
| 497 | - return $cards; |
|
| 498 | - } |
|
| 499 | - |
|
| 500 | - /** |
|
| 501 | - * Returns a specific card. |
|
| 502 | - * |
|
| 503 | - * The same set of properties must be returned as with getCards. The only |
|
| 504 | - * exception is that 'carddata' is absolutely required. |
|
| 505 | - * |
|
| 506 | - * If the card does not exist, you must return false. |
|
| 507 | - * |
|
| 508 | - * @param mixed $addressBookId |
|
| 509 | - * @param string $cardUri |
|
| 510 | - * @return array |
|
| 511 | - */ |
|
| 512 | - public function getCard($addressBookId, $cardUri) { |
|
| 513 | - $query = $this->db->getQueryBuilder(); |
|
| 514 | - $query->select(['id', 'uri', 'lastmodified', 'etag', 'size', 'carddata', 'uid']) |
|
| 515 | - ->from($this->dbCardsTable) |
|
| 516 | - ->where($query->expr()->eq('addressbookid', $query->createNamedParameter($addressBookId))) |
|
| 517 | - ->andWhere($query->expr()->eq('uri', $query->createNamedParameter($cardUri))) |
|
| 518 | - ->setMaxResults(1); |
|
| 519 | - |
|
| 520 | - $result = $query->executeQuery(); |
|
| 521 | - $row = $result->fetch(); |
|
| 522 | - if (!$row) { |
|
| 523 | - return false; |
|
| 524 | - } |
|
| 525 | - $row['etag'] = '"' . $row['etag'] . '"'; |
|
| 526 | - |
|
| 527 | - $modified = false; |
|
| 528 | - $row['carddata'] = $this->readBlob($row['carddata'], $modified); |
|
| 529 | - if ($modified) { |
|
| 530 | - $row['size'] = strlen($row['carddata']); |
|
| 531 | - } |
|
| 532 | - |
|
| 533 | - return $row; |
|
| 534 | - } |
|
| 535 | - |
|
| 536 | - /** |
|
| 537 | - * Returns a list of cards. |
|
| 538 | - * |
|
| 539 | - * This method should work identical to getCard, but instead return all the |
|
| 540 | - * cards in the list as an array. |
|
| 541 | - * |
|
| 542 | - * If the backend supports this, it may allow for some speed-ups. |
|
| 543 | - * |
|
| 544 | - * @param mixed $addressBookId |
|
| 545 | - * @param array $uris |
|
| 546 | - * @return array |
|
| 547 | - */ |
|
| 548 | - public function getMultipleCards($addressBookId, array $uris) { |
|
| 549 | - if (empty($uris)) { |
|
| 550 | - return []; |
|
| 551 | - } |
|
| 552 | - |
|
| 553 | - $chunks = array_chunk($uris, 100); |
|
| 554 | - $cards = []; |
|
| 555 | - |
|
| 556 | - $query = $this->db->getQueryBuilder(); |
|
| 557 | - $query->select(['id', 'uri', 'lastmodified', 'etag', 'size', 'carddata', 'uid']) |
|
| 558 | - ->from($this->dbCardsTable) |
|
| 559 | - ->where($query->expr()->eq('addressbookid', $query->createNamedParameter($addressBookId))) |
|
| 560 | - ->andWhere($query->expr()->in('uri', $query->createParameter('uri'))); |
|
| 561 | - |
|
| 562 | - foreach ($chunks as $uris) { |
|
| 563 | - $query->setParameter('uri', $uris, IQueryBuilder::PARAM_STR_ARRAY); |
|
| 564 | - $result = $query->executeQuery(); |
|
| 565 | - |
|
| 566 | - while ($row = $result->fetch()) { |
|
| 567 | - $row['etag'] = '"' . $row['etag'] . '"'; |
|
| 568 | - |
|
| 569 | - $modified = false; |
|
| 570 | - $row['carddata'] = $this->readBlob($row['carddata'], $modified); |
|
| 571 | - if ($modified) { |
|
| 572 | - $row['size'] = strlen($row['carddata']); |
|
| 573 | - } |
|
| 574 | - |
|
| 575 | - $cards[] = $row; |
|
| 576 | - } |
|
| 577 | - $result->closeCursor(); |
|
| 578 | - } |
|
| 579 | - return $cards; |
|
| 580 | - } |
|
| 581 | - |
|
| 582 | - /** |
|
| 583 | - * Creates a new card. |
|
| 584 | - * |
|
| 585 | - * The addressbook id will be passed as the first argument. This is the |
|
| 586 | - * same id as it is returned from the getAddressBooksForUser method. |
|
| 587 | - * |
|
| 588 | - * The cardUri is a base uri, and doesn't include the full path. The |
|
| 589 | - * cardData argument is the vcard body, and is passed as a string. |
|
| 590 | - * |
|
| 591 | - * It is possible to return an ETag from this method. This ETag is for the |
|
| 592 | - * newly created resource, and must be enclosed with double quotes (that |
|
| 593 | - * is, the string itself must contain the double quotes). |
|
| 594 | - * |
|
| 595 | - * You should only return the ETag if you store the carddata as-is. If a |
|
| 596 | - * subsequent GET request on the same card does not have the same body, |
|
| 597 | - * byte-by-byte and you did return an ETag here, clients tend to get |
|
| 598 | - * confused. |
|
| 599 | - * |
|
| 600 | - * If you don't return an ETag, you can just return null. |
|
| 601 | - * |
|
| 602 | - * @param mixed $addressBookId |
|
| 603 | - * @param string $cardUri |
|
| 604 | - * @param string $cardData |
|
| 605 | - * @param bool $checkAlreadyExists |
|
| 606 | - * @return string |
|
| 607 | - */ |
|
| 608 | - public function createCard($addressBookId, $cardUri, $cardData, bool $checkAlreadyExists = true) { |
|
| 609 | - $etag = md5($cardData); |
|
| 610 | - $uid = $this->getUID($cardData); |
|
| 611 | - return $this->atomic(function () use ($addressBookId, $cardUri, $cardData, $checkAlreadyExists, $etag, $uid) { |
|
| 612 | - if ($checkAlreadyExists) { |
|
| 613 | - $q = $this->db->getQueryBuilder(); |
|
| 614 | - $q->select('uid') |
|
| 615 | - ->from($this->dbCardsTable) |
|
| 616 | - ->where($q->expr()->eq('addressbookid', $q->createNamedParameter($addressBookId))) |
|
| 617 | - ->andWhere($q->expr()->eq('uid', $q->createNamedParameter($uid))) |
|
| 618 | - ->setMaxResults(1); |
|
| 619 | - $result = $q->executeQuery(); |
|
| 620 | - $count = (bool)$result->fetchOne(); |
|
| 621 | - $result->closeCursor(); |
|
| 622 | - if ($count) { |
|
| 623 | - throw new \Sabre\DAV\Exception\BadRequest('VCard object with uid already exists in this addressbook collection.'); |
|
| 624 | - } |
|
| 625 | - } |
|
| 626 | - |
|
| 627 | - $query = $this->db->getQueryBuilder(); |
|
| 628 | - $query->insert('cards') |
|
| 629 | - ->values([ |
|
| 630 | - 'carddata' => $query->createNamedParameter($cardData, IQueryBuilder::PARAM_LOB), |
|
| 631 | - 'uri' => $query->createNamedParameter($cardUri), |
|
| 632 | - 'lastmodified' => $query->createNamedParameter(time()), |
|
| 633 | - 'addressbookid' => $query->createNamedParameter($addressBookId), |
|
| 634 | - 'size' => $query->createNamedParameter(strlen($cardData)), |
|
| 635 | - 'etag' => $query->createNamedParameter($etag), |
|
| 636 | - 'uid' => $query->createNamedParameter($uid), |
|
| 637 | - ]) |
|
| 638 | - ->executeStatement(); |
|
| 639 | - |
|
| 640 | - $etagCacheKey = "$addressBookId#$cardUri"; |
|
| 641 | - $this->etagCache[$etagCacheKey] = $etag; |
|
| 642 | - |
|
| 643 | - $this->addChange($addressBookId, $cardUri, 1); |
|
| 644 | - $this->updateProperties($addressBookId, $cardUri, $cardData); |
|
| 645 | - |
|
| 646 | - $addressBookData = $this->getAddressBookById($addressBookId); |
|
| 647 | - $shares = $this->getShares($addressBookId); |
|
| 648 | - $objectRow = $this->getCard($addressBookId, $cardUri); |
|
| 649 | - $this->dispatcher->dispatchTyped(new CardCreatedEvent($addressBookId, $addressBookData, $shares, $objectRow)); |
|
| 650 | - |
|
| 651 | - return '"' . $etag . '"'; |
|
| 652 | - }, $this->db); |
|
| 653 | - } |
|
| 654 | - |
|
| 655 | - /** |
|
| 656 | - * Updates a card. |
|
| 657 | - * |
|
| 658 | - * The addressbook id will be passed as the first argument. This is the |
|
| 659 | - * same id as it is returned from the getAddressBooksForUser method. |
|
| 660 | - * |
|
| 661 | - * The cardUri is a base uri, and doesn't include the full path. The |
|
| 662 | - * cardData argument is the vcard body, and is passed as a string. |
|
| 663 | - * |
|
| 664 | - * It is possible to return an ETag from this method. This ETag should |
|
| 665 | - * match that of the updated resource, and must be enclosed with double |
|
| 666 | - * quotes (that is: the string itself must contain the actual quotes). |
|
| 667 | - * |
|
| 668 | - * You should only return the ETag if you store the carddata as-is. If a |
|
| 669 | - * subsequent GET request on the same card does not have the same body, |
|
| 670 | - * byte-by-byte and you did return an ETag here, clients tend to get |
|
| 671 | - * confused. |
|
| 672 | - * |
|
| 673 | - * If you don't return an ETag, you can just return null. |
|
| 674 | - * |
|
| 675 | - * @param mixed $addressBookId |
|
| 676 | - * @param string $cardUri |
|
| 677 | - * @param string $cardData |
|
| 678 | - * @return string |
|
| 679 | - */ |
|
| 680 | - public function updateCard($addressBookId, $cardUri, $cardData) { |
|
| 681 | - $uid = $this->getUID($cardData); |
|
| 682 | - $etag = md5($cardData); |
|
| 683 | - |
|
| 684 | - return $this->atomic(function () use ($addressBookId, $cardUri, $cardData, $uid, $etag) { |
|
| 685 | - $query = $this->db->getQueryBuilder(); |
|
| 686 | - |
|
| 687 | - // check for recently stored etag and stop if it is the same |
|
| 688 | - $etagCacheKey = "$addressBookId#$cardUri"; |
|
| 689 | - if (isset($this->etagCache[$etagCacheKey]) && $this->etagCache[$etagCacheKey] === $etag) { |
|
| 690 | - return '"' . $etag . '"'; |
|
| 691 | - } |
|
| 692 | - |
|
| 693 | - $query->update($this->dbCardsTable) |
|
| 694 | - ->set('carddata', $query->createNamedParameter($cardData, IQueryBuilder::PARAM_LOB)) |
|
| 695 | - ->set('lastmodified', $query->createNamedParameter(time())) |
|
| 696 | - ->set('size', $query->createNamedParameter(strlen($cardData))) |
|
| 697 | - ->set('etag', $query->createNamedParameter($etag)) |
|
| 698 | - ->set('uid', $query->createNamedParameter($uid)) |
|
| 699 | - ->where($query->expr()->eq('uri', $query->createNamedParameter($cardUri))) |
|
| 700 | - ->andWhere($query->expr()->eq('addressbookid', $query->createNamedParameter($addressBookId))) |
|
| 701 | - ->executeStatement(); |
|
| 702 | - |
|
| 703 | - $this->etagCache[$etagCacheKey] = $etag; |
|
| 704 | - |
|
| 705 | - $this->addChange($addressBookId, $cardUri, 2); |
|
| 706 | - $this->updateProperties($addressBookId, $cardUri, $cardData); |
|
| 707 | - |
|
| 708 | - $addressBookData = $this->getAddressBookById($addressBookId); |
|
| 709 | - $shares = $this->getShares($addressBookId); |
|
| 710 | - $objectRow = $this->getCard($addressBookId, $cardUri); |
|
| 711 | - $this->dispatcher->dispatchTyped(new CardUpdatedEvent($addressBookId, $addressBookData, $shares, $objectRow)); |
|
| 712 | - return '"' . $etag . '"'; |
|
| 713 | - }, $this->db); |
|
| 714 | - } |
|
| 715 | - |
|
| 716 | - /** |
|
| 717 | - * @throws Exception |
|
| 718 | - */ |
|
| 719 | - public function moveCard(int $sourceAddressBookId, int $targetAddressBookId, string $cardUri, string $oldPrincipalUri): bool { |
|
| 720 | - return $this->atomic(function () use ($sourceAddressBookId, $targetAddressBookId, $cardUri, $oldPrincipalUri) { |
|
| 721 | - $card = $this->getCard($sourceAddressBookId, $cardUri); |
|
| 722 | - if (empty($card)) { |
|
| 723 | - return false; |
|
| 724 | - } |
|
| 725 | - |
|
| 726 | - $query = $this->db->getQueryBuilder(); |
|
| 727 | - $query->update('cards') |
|
| 728 | - ->set('addressbookid', $query->createNamedParameter($targetAddressBookId, IQueryBuilder::PARAM_INT)) |
|
| 729 | - ->where($query->expr()->eq('uri', $query->createNamedParameter($cardUri, IQueryBuilder::PARAM_STR), IQueryBuilder::PARAM_STR)) |
|
| 730 | - ->andWhere($query->expr()->eq('addressbookid', $query->createNamedParameter($sourceAddressBookId, IQueryBuilder::PARAM_INT), IQueryBuilder::PARAM_INT)) |
|
| 731 | - ->executeStatement(); |
|
| 732 | - |
|
| 733 | - $this->purgeProperties($sourceAddressBookId, (int)$card['id']); |
|
| 734 | - $this->updateProperties($sourceAddressBookId, $card['uri'], $card['carddata']); |
|
| 735 | - |
|
| 736 | - $this->addChange($sourceAddressBookId, $card['uri'], 3); |
|
| 737 | - $this->addChange($targetAddressBookId, $card['uri'], 1); |
|
| 738 | - |
|
| 739 | - $card = $this->getCard($targetAddressBookId, $cardUri); |
|
| 740 | - // Card wasn't found - possibly because it was deleted in the meantime by a different client |
|
| 741 | - if (empty($card)) { |
|
| 742 | - return false; |
|
| 743 | - } |
|
| 744 | - |
|
| 745 | - $targetAddressBookRow = $this->getAddressBookById($targetAddressBookId); |
|
| 746 | - // the address book this card is being moved to does not exist any longer |
|
| 747 | - if (empty($targetAddressBookRow)) { |
|
| 748 | - return false; |
|
| 749 | - } |
|
| 750 | - |
|
| 751 | - $sourceShares = $this->getShares($sourceAddressBookId); |
|
| 752 | - $targetShares = $this->getShares($targetAddressBookId); |
|
| 753 | - $sourceAddressBookRow = $this->getAddressBookById($sourceAddressBookId); |
|
| 754 | - $this->dispatcher->dispatchTyped(new CardMovedEvent($sourceAddressBookId, $sourceAddressBookRow, $targetAddressBookId, $targetAddressBookRow, $sourceShares, $targetShares, $card)); |
|
| 755 | - return true; |
|
| 756 | - }, $this->db); |
|
| 757 | - } |
|
| 758 | - |
|
| 759 | - /** |
|
| 760 | - * Deletes a card |
|
| 761 | - * |
|
| 762 | - * @param mixed $addressBookId |
|
| 763 | - * @param string $cardUri |
|
| 764 | - * @return bool |
|
| 765 | - */ |
|
| 766 | - public function deleteCard($addressBookId, $cardUri) { |
|
| 767 | - return $this->atomic(function () use ($addressBookId, $cardUri) { |
|
| 768 | - $addressBookData = $this->getAddressBookById($addressBookId); |
|
| 769 | - $shares = $this->getShares($addressBookId); |
|
| 770 | - $objectRow = $this->getCard($addressBookId, $cardUri); |
|
| 771 | - |
|
| 772 | - try { |
|
| 773 | - $cardId = $this->getCardId($addressBookId, $cardUri); |
|
| 774 | - } catch (\InvalidArgumentException $e) { |
|
| 775 | - $cardId = null; |
|
| 776 | - } |
|
| 777 | - $query = $this->db->getQueryBuilder(); |
|
| 778 | - $ret = $query->delete($this->dbCardsTable) |
|
| 779 | - ->where($query->expr()->eq('addressbookid', $query->createNamedParameter($addressBookId))) |
|
| 780 | - ->andWhere($query->expr()->eq('uri', $query->createNamedParameter($cardUri))) |
|
| 781 | - ->executeStatement(); |
|
| 782 | - |
|
| 783 | - $this->addChange($addressBookId, $cardUri, 3); |
|
| 784 | - |
|
| 785 | - if ($ret === 1) { |
|
| 786 | - if ($cardId !== null) { |
|
| 787 | - $this->dispatcher->dispatchTyped(new CardDeletedEvent($addressBookId, $addressBookData, $shares, $objectRow)); |
|
| 788 | - $this->purgeProperties($addressBookId, $cardId); |
|
| 789 | - } |
|
| 790 | - return true; |
|
| 791 | - } |
|
| 792 | - |
|
| 793 | - return false; |
|
| 794 | - }, $this->db); |
|
| 795 | - } |
|
| 796 | - |
|
| 797 | - /** |
|
| 798 | - * The getChanges method returns all the changes that have happened, since |
|
| 799 | - * the specified syncToken in the specified address book. |
|
| 800 | - * |
|
| 801 | - * This function should return an array, such as the following: |
|
| 802 | - * |
|
| 803 | - * [ |
|
| 804 | - * 'syncToken' => 'The current synctoken', |
|
| 805 | - * 'added' => [ |
|
| 806 | - * 'new.txt', |
|
| 807 | - * ], |
|
| 808 | - * 'modified' => [ |
|
| 809 | - * 'modified.txt', |
|
| 810 | - * ], |
|
| 811 | - * 'deleted' => [ |
|
| 812 | - * 'foo.php.bak', |
|
| 813 | - * 'old.txt' |
|
| 814 | - * ] |
|
| 815 | - * ]; |
|
| 816 | - * |
|
| 817 | - * The returned syncToken property should reflect the *current* syncToken |
|
| 818 | - * of the calendar, as reported in the {http://sabredav.org/ns}sync-token |
|
| 819 | - * property. This is needed here too, to ensure the operation is atomic. |
|
| 820 | - * |
|
| 821 | - * If the $syncToken argument is specified as null, this is an initial |
|
| 822 | - * sync, and all members should be reported. |
|
| 823 | - * |
|
| 824 | - * The modified property is an array of nodenames that have changed since |
|
| 825 | - * the last token. |
|
| 826 | - * |
|
| 827 | - * The deleted property is an array with nodenames, that have been deleted |
|
| 828 | - * from collection. |
|
| 829 | - * |
|
| 830 | - * The $syncLevel argument is basically the 'depth' of the report. If it's |
|
| 831 | - * 1, you only have to report changes that happened only directly in |
|
| 832 | - * immediate descendants. If it's 2, it should also include changes from |
|
| 833 | - * the nodes below the child collections. (grandchildren) |
|
| 834 | - * |
|
| 835 | - * The $limit argument allows a client to specify how many results should |
|
| 836 | - * be returned at most. If the limit is not specified, it should be treated |
|
| 837 | - * as infinite. |
|
| 838 | - * |
|
| 839 | - * If the limit (infinite or not) is higher than you're willing to return, |
|
| 840 | - * you should throw a Sabre\DAV\Exception\TooMuchMatches() exception. |
|
| 841 | - * |
|
| 842 | - * If the syncToken is expired (due to data cleanup) or unknown, you must |
|
| 843 | - * return null. |
|
| 844 | - * |
|
| 845 | - * The limit is 'suggestive'. You are free to ignore it. |
|
| 846 | - * |
|
| 847 | - * @param string $addressBookId |
|
| 848 | - * @param string $syncToken |
|
| 849 | - * @param int $syncLevel |
|
| 850 | - * @param int|null $limit |
|
| 851 | - * @return array |
|
| 852 | - */ |
|
| 853 | - public function getChangesForAddressBook($addressBookId, $syncToken, $syncLevel, $limit = null) { |
|
| 854 | - // Current synctoken |
|
| 855 | - return $this->atomic(function () use ($addressBookId, $syncToken, $syncLevel, $limit) { |
|
| 856 | - $qb = $this->db->getQueryBuilder(); |
|
| 857 | - $qb->select('synctoken') |
|
| 858 | - ->from('addressbooks') |
|
| 859 | - ->where( |
|
| 860 | - $qb->expr()->eq('id', $qb->createNamedParameter($addressBookId)) |
|
| 861 | - ); |
|
| 862 | - $stmt = $qb->executeQuery(); |
|
| 863 | - $currentToken = $stmt->fetchOne(); |
|
| 864 | - $stmt->closeCursor(); |
|
| 865 | - |
|
| 866 | - if (is_null($currentToken)) { |
|
| 867 | - return []; |
|
| 868 | - } |
|
| 869 | - |
|
| 870 | - $result = [ |
|
| 871 | - 'syncToken' => $currentToken, |
|
| 872 | - 'added' => [], |
|
| 873 | - 'modified' => [], |
|
| 874 | - 'deleted' => [], |
|
| 875 | - ]; |
|
| 876 | - |
|
| 877 | - if ($syncToken) { |
|
| 878 | - $qb = $this->db->getQueryBuilder(); |
|
| 879 | - $qb->select('uri', 'operation') |
|
| 880 | - ->from('addressbookchanges') |
|
| 881 | - ->where( |
|
| 882 | - $qb->expr()->andX( |
|
| 883 | - $qb->expr()->gte('synctoken', $qb->createNamedParameter($syncToken)), |
|
| 884 | - $qb->expr()->lt('synctoken', $qb->createNamedParameter($currentToken)), |
|
| 885 | - $qb->expr()->eq('addressbookid', $qb->createNamedParameter($addressBookId)) |
|
| 886 | - ) |
|
| 887 | - )->orderBy('synctoken'); |
|
| 888 | - |
|
| 889 | - if (is_int($limit) && $limit > 0) { |
|
| 890 | - $qb->setMaxResults($limit); |
|
| 891 | - } |
|
| 892 | - |
|
| 893 | - // Fetching all changes |
|
| 894 | - $stmt = $qb->executeQuery(); |
|
| 895 | - |
|
| 896 | - $changes = []; |
|
| 897 | - |
|
| 898 | - // This loop ensures that any duplicates are overwritten, only the |
|
| 899 | - // last change on a node is relevant. |
|
| 900 | - while ($row = $stmt->fetch(\PDO::FETCH_ASSOC)) { |
|
| 901 | - $changes[$row['uri']] = $row['operation']; |
|
| 902 | - } |
|
| 903 | - $stmt->closeCursor(); |
|
| 904 | - |
|
| 905 | - foreach ($changes as $uri => $operation) { |
|
| 906 | - switch ($operation) { |
|
| 907 | - case 1: |
|
| 908 | - $result['added'][] = $uri; |
|
| 909 | - break; |
|
| 910 | - case 2: |
|
| 911 | - $result['modified'][] = $uri; |
|
| 912 | - break; |
|
| 913 | - case 3: |
|
| 914 | - $result['deleted'][] = $uri; |
|
| 915 | - break; |
|
| 916 | - } |
|
| 917 | - } |
|
| 918 | - } else { |
|
| 919 | - $qb = $this->db->getQueryBuilder(); |
|
| 920 | - $qb->select('uri') |
|
| 921 | - ->from('cards') |
|
| 922 | - ->where( |
|
| 923 | - $qb->expr()->eq('addressbookid', $qb->createNamedParameter($addressBookId)) |
|
| 924 | - ); |
|
| 925 | - // No synctoken supplied, this is the initial sync. |
|
| 926 | - $stmt = $qb->executeQuery(); |
|
| 927 | - $result['added'] = $stmt->fetchAll(\PDO::FETCH_COLUMN); |
|
| 928 | - $stmt->closeCursor(); |
|
| 929 | - } |
|
| 930 | - return $result; |
|
| 931 | - }, $this->db); |
|
| 932 | - } |
|
| 933 | - |
|
| 934 | - /** |
|
| 935 | - * Adds a change record to the addressbookchanges table. |
|
| 936 | - * |
|
| 937 | - * @param mixed $addressBookId |
|
| 938 | - * @param string $objectUri |
|
| 939 | - * @param int $operation 1 = add, 2 = modify, 3 = delete |
|
| 940 | - * @return void |
|
| 941 | - */ |
|
| 942 | - protected function addChange(int $addressBookId, string $objectUri, int $operation): void { |
|
| 943 | - $this->atomic(function () use ($addressBookId, $objectUri, $operation): void { |
|
| 944 | - $query = $this->db->getQueryBuilder(); |
|
| 945 | - $query->select('synctoken') |
|
| 946 | - ->from('addressbooks') |
|
| 947 | - ->where($query->expr()->eq('id', $query->createNamedParameter($addressBookId))); |
|
| 948 | - $result = $query->executeQuery(); |
|
| 949 | - $syncToken = (int)$result->fetchOne(); |
|
| 950 | - $result->closeCursor(); |
|
| 951 | - |
|
| 952 | - $query = $this->db->getQueryBuilder(); |
|
| 953 | - $query->insert('addressbookchanges') |
|
| 954 | - ->values([ |
|
| 955 | - 'uri' => $query->createNamedParameter($objectUri), |
|
| 956 | - 'synctoken' => $query->createNamedParameter($syncToken), |
|
| 957 | - 'addressbookid' => $query->createNamedParameter($addressBookId), |
|
| 958 | - 'operation' => $query->createNamedParameter($operation), |
|
| 959 | - 'created_at' => time(), |
|
| 960 | - ]) |
|
| 961 | - ->executeStatement(); |
|
| 962 | - |
|
| 963 | - $query = $this->db->getQueryBuilder(); |
|
| 964 | - $query->update('addressbooks') |
|
| 965 | - ->set('synctoken', $query->createNamedParameter($syncToken + 1, IQueryBuilder::PARAM_INT)) |
|
| 966 | - ->where($query->expr()->eq('id', $query->createNamedParameter($addressBookId))) |
|
| 967 | - ->executeStatement(); |
|
| 968 | - }, $this->db); |
|
| 969 | - } |
|
| 970 | - |
|
| 971 | - /** |
|
| 972 | - * @param resource|string $cardData |
|
| 973 | - * @param bool $modified |
|
| 974 | - * @return string |
|
| 975 | - */ |
|
| 976 | - private function readBlob($cardData, &$modified = false) { |
|
| 977 | - if (is_resource($cardData)) { |
|
| 978 | - $cardData = stream_get_contents($cardData); |
|
| 979 | - } |
|
| 980 | - |
|
| 981 | - // Micro optimisation |
|
| 982 | - // don't loop through |
|
| 983 | - if (str_starts_with($cardData, 'PHOTO:data:')) { |
|
| 984 | - return $cardData; |
|
| 985 | - } |
|
| 986 | - |
|
| 987 | - $cardDataArray = explode("\r\n", $cardData); |
|
| 988 | - |
|
| 989 | - $cardDataFiltered = []; |
|
| 990 | - $removingPhoto = false; |
|
| 991 | - foreach ($cardDataArray as $line) { |
|
| 992 | - if (str_starts_with($line, 'PHOTO:data:') |
|
| 993 | - && !str_starts_with($line, 'PHOTO:data:image/')) { |
|
| 994 | - // Filter out PHOTO data of non-images |
|
| 995 | - $removingPhoto = true; |
|
| 996 | - $modified = true; |
|
| 997 | - continue; |
|
| 998 | - } |
|
| 999 | - |
|
| 1000 | - if ($removingPhoto) { |
|
| 1001 | - if (str_starts_with($line, ' ')) { |
|
| 1002 | - continue; |
|
| 1003 | - } |
|
| 1004 | - // No leading space means this is a new property |
|
| 1005 | - $removingPhoto = false; |
|
| 1006 | - } |
|
| 1007 | - |
|
| 1008 | - $cardDataFiltered[] = $line; |
|
| 1009 | - } |
|
| 1010 | - return implode("\r\n", $cardDataFiltered); |
|
| 1011 | - } |
|
| 1012 | - |
|
| 1013 | - /** |
|
| 1014 | - * @param list<array{href: string, commonName: string, readOnly: bool}> $add |
|
| 1015 | - * @param list<string> $remove |
|
| 1016 | - */ |
|
| 1017 | - public function updateShares(IShareable $shareable, array $add, array $remove): void { |
|
| 1018 | - $this->atomic(function () use ($shareable, $add, $remove): void { |
|
| 1019 | - $addressBookId = $shareable->getResourceId(); |
|
| 1020 | - $addressBookData = $this->getAddressBookById($addressBookId); |
|
| 1021 | - $oldShares = $this->getShares($addressBookId); |
|
| 1022 | - |
|
| 1023 | - $this->sharingBackend->updateShares($shareable, $add, $remove, $oldShares); |
|
| 1024 | - |
|
| 1025 | - $this->dispatcher->dispatchTyped(new AddressBookShareUpdatedEvent($addressBookId, $addressBookData, $oldShares, $add, $remove)); |
|
| 1026 | - }, $this->db); |
|
| 1027 | - } |
|
| 1028 | - |
|
| 1029 | - /** |
|
| 1030 | - * Search contacts in a specific address-book |
|
| 1031 | - * |
|
| 1032 | - * @param int $addressBookId |
|
| 1033 | - * @param string $pattern which should match within the $searchProperties |
|
| 1034 | - * @param array $searchProperties defines the properties within the query pattern should match |
|
| 1035 | - * @param array $options = array() to define the search behavior |
|
| 1036 | - * - 'types' boolean (since 15.0.0) If set to true, fields that come with a TYPE property will be an array |
|
| 1037 | - * - 'escape_like_param' - If set to false wildcards _ and % are not escaped, otherwise they are |
|
| 1038 | - * - 'limit' - Set a numeric limit for the search results |
|
| 1039 | - * - 'offset' - Set the offset for the limited search results |
|
| 1040 | - * - 'wildcard' - Whether the search should use wildcards |
|
| 1041 | - * @psalm-param array{types?: bool, escape_like_param?: bool, limit?: int, offset?: int, wildcard?: bool} $options |
|
| 1042 | - * @return array an array of contacts which are arrays of key-value-pairs |
|
| 1043 | - */ |
|
| 1044 | - public function search($addressBookId, $pattern, $searchProperties, $options = []): array { |
|
| 1045 | - return $this->atomic(function () use ($addressBookId, $pattern, $searchProperties, $options) { |
|
| 1046 | - return $this->searchByAddressBookIds([$addressBookId], $pattern, $searchProperties, $options); |
|
| 1047 | - }, $this->db); |
|
| 1048 | - } |
|
| 1049 | - |
|
| 1050 | - /** |
|
| 1051 | - * Search contacts in all address-books accessible by a user |
|
| 1052 | - * |
|
| 1053 | - * @param string $principalUri |
|
| 1054 | - * @param string $pattern |
|
| 1055 | - * @param array $searchProperties |
|
| 1056 | - * @param array $options |
|
| 1057 | - * @return array |
|
| 1058 | - */ |
|
| 1059 | - public function searchPrincipalUri(string $principalUri, |
|
| 1060 | - string $pattern, |
|
| 1061 | - array $searchProperties, |
|
| 1062 | - array $options = []): array { |
|
| 1063 | - return $this->atomic(function () use ($principalUri, $pattern, $searchProperties, $options) { |
|
| 1064 | - $addressBookIds = array_map(static function ($row):int { |
|
| 1065 | - return (int)$row['id']; |
|
| 1066 | - }, $this->getAddressBooksForUser($principalUri)); |
|
| 1067 | - |
|
| 1068 | - return $this->searchByAddressBookIds($addressBookIds, $pattern, $searchProperties, $options); |
|
| 1069 | - }, $this->db); |
|
| 1070 | - } |
|
| 1071 | - |
|
| 1072 | - /** |
|
| 1073 | - * @param int[] $addressBookIds |
|
| 1074 | - * @param string $pattern |
|
| 1075 | - * @param array $searchProperties |
|
| 1076 | - * @param array $options |
|
| 1077 | - * @psalm-param array{ |
|
| 1078 | - * types?: bool, |
|
| 1079 | - * escape_like_param?: bool, |
|
| 1080 | - * limit?: int, |
|
| 1081 | - * offset?: int, |
|
| 1082 | - * wildcard?: bool, |
|
| 1083 | - * since?: DateTimeFilter|null, |
|
| 1084 | - * until?: DateTimeFilter|null, |
|
| 1085 | - * person?: string |
|
| 1086 | - * } $options |
|
| 1087 | - * @return array |
|
| 1088 | - */ |
|
| 1089 | - private function searchByAddressBookIds(array $addressBookIds, |
|
| 1090 | - string $pattern, |
|
| 1091 | - array $searchProperties, |
|
| 1092 | - array $options = []): array { |
|
| 1093 | - if (empty($addressBookIds)) { |
|
| 1094 | - return []; |
|
| 1095 | - } |
|
| 1096 | - $escapePattern = !\array_key_exists('escape_like_param', $options) || $options['escape_like_param'] !== false; |
|
| 1097 | - $useWildcards = !\array_key_exists('wildcard', $options) || $options['wildcard'] !== false; |
|
| 1098 | - |
|
| 1099 | - if ($escapePattern) { |
|
| 1100 | - $searchProperties = array_filter($searchProperties, function ($property) use ($pattern) { |
|
| 1101 | - if ($property === 'EMAIL' && str_contains($pattern, ' ')) { |
|
| 1102 | - // There can be no spaces in emails |
|
| 1103 | - return false; |
|
| 1104 | - } |
|
| 1105 | - |
|
| 1106 | - if ($property === 'CLOUD' && preg_match('/[^a-zA-Z0-9 :_.@\/\-\']/', $pattern) === 1) { |
|
| 1107 | - // There can be no chars in cloud ids which are not valid for user ids plus :/ |
|
| 1108 | - // worst case: CA61590A-BBBC-423E-84AF-E6DF01455A53@https://my.nxt/srv/ |
|
| 1109 | - return false; |
|
| 1110 | - } |
|
| 1111 | - |
|
| 1112 | - return true; |
|
| 1113 | - }); |
|
| 1114 | - } |
|
| 1115 | - |
|
| 1116 | - if (empty($searchProperties)) { |
|
| 1117 | - return []; |
|
| 1118 | - } |
|
| 1119 | - |
|
| 1120 | - $query2 = $this->db->getQueryBuilder(); |
|
| 1121 | - $query2->selectDistinct('cp.cardid') |
|
| 1122 | - ->from($this->dbCardsPropertiesTable, 'cp') |
|
| 1123 | - ->where($query2->expr()->in('cp.addressbookid', $query2->createNamedParameter($addressBookIds, IQueryBuilder::PARAM_INT_ARRAY), IQueryBuilder::PARAM_INT_ARRAY)) |
|
| 1124 | - ->andWhere($query2->expr()->in('cp.name', $query2->createNamedParameter($searchProperties, IQueryBuilder::PARAM_STR_ARRAY))); |
|
| 1125 | - |
|
| 1126 | - // No need for like when the pattern is empty |
|
| 1127 | - if ($pattern !== '') { |
|
| 1128 | - if (!$useWildcards) { |
|
| 1129 | - $query2->andWhere($query2->expr()->eq('cp.value', $query2->createNamedParameter($pattern))); |
|
| 1130 | - } elseif (!$escapePattern) { |
|
| 1131 | - $query2->andWhere($query2->expr()->ilike('cp.value', $query2->createNamedParameter($pattern))); |
|
| 1132 | - } else { |
|
| 1133 | - $query2->andWhere($query2->expr()->ilike('cp.value', $query2->createNamedParameter('%' . $this->db->escapeLikeParameter($pattern) . '%'))); |
|
| 1134 | - } |
|
| 1135 | - } |
|
| 1136 | - if (isset($options['limit'])) { |
|
| 1137 | - $query2->setMaxResults($options['limit']); |
|
| 1138 | - } |
|
| 1139 | - if (isset($options['offset'])) { |
|
| 1140 | - $query2->setFirstResult($options['offset']); |
|
| 1141 | - } |
|
| 1142 | - |
|
| 1143 | - if (isset($options['person'])) { |
|
| 1144 | - $query2->andWhere($query2->expr()->ilike('cp.value', $query2->createNamedParameter('%' . $this->db->escapeLikeParameter($options['person']) . '%'))); |
|
| 1145 | - } |
|
| 1146 | - if (isset($options['since']) || isset($options['until'])) { |
|
| 1147 | - $query2->join('cp', $this->dbCardsPropertiesTable, 'cp_bday', 'cp.cardid = cp_bday.cardid'); |
|
| 1148 | - $query2->andWhere($query2->expr()->eq('cp_bday.name', $query2->createNamedParameter('BDAY'))); |
|
| 1149 | - /** |
|
| 1150 | - * FIXME Find a way to match only 4 last digits |
|
| 1151 | - * BDAY can be --1018 without year or 20001019 with it |
|
| 1152 | - * $bDayOr = []; |
|
| 1153 | - * if ($options['since'] instanceof DateTimeFilter) { |
|
| 1154 | - * $bDayOr[] = |
|
| 1155 | - * $query2->expr()->gte('SUBSTR(cp_bday.value, -4)', |
|
| 1156 | - * $query2->createNamedParameter($options['since']->get()->format('md')) |
|
| 1157 | - * ); |
|
| 1158 | - * } |
|
| 1159 | - * if ($options['until'] instanceof DateTimeFilter) { |
|
| 1160 | - * $bDayOr[] = |
|
| 1161 | - * $query2->expr()->lte('SUBSTR(cp_bday.value, -4)', |
|
| 1162 | - * $query2->createNamedParameter($options['until']->get()->format('md')) |
|
| 1163 | - * ); |
|
| 1164 | - * } |
|
| 1165 | - * $query2->andWhere($query2->expr()->orX(...$bDayOr)); |
|
| 1166 | - */ |
|
| 1167 | - } |
|
| 1168 | - |
|
| 1169 | - $result = $query2->executeQuery(); |
|
| 1170 | - $matches = $result->fetchAll(); |
|
| 1171 | - $result->closeCursor(); |
|
| 1172 | - $matches = array_map(function ($match) { |
|
| 1173 | - return (int)$match['cardid']; |
|
| 1174 | - }, $matches); |
|
| 1175 | - |
|
| 1176 | - $cards = []; |
|
| 1177 | - $query = $this->db->getQueryBuilder(); |
|
| 1178 | - $query->select('c.addressbookid', 'c.carddata', 'c.uri') |
|
| 1179 | - ->from($this->dbCardsTable, 'c') |
|
| 1180 | - ->where($query->expr()->in('c.id', $query->createParameter('matches'))); |
|
| 1181 | - |
|
| 1182 | - foreach (array_chunk($matches, 1000) as $matchesChunk) { |
|
| 1183 | - $query->setParameter('matches', $matchesChunk, IQueryBuilder::PARAM_INT_ARRAY); |
|
| 1184 | - $result = $query->executeQuery(); |
|
| 1185 | - $cards = array_merge($cards, $result->fetchAll()); |
|
| 1186 | - $result->closeCursor(); |
|
| 1187 | - } |
|
| 1188 | - |
|
| 1189 | - return array_map(function ($array) { |
|
| 1190 | - $array['addressbookid'] = (int)$array['addressbookid']; |
|
| 1191 | - $modified = false; |
|
| 1192 | - $array['carddata'] = $this->readBlob($array['carddata'], $modified); |
|
| 1193 | - if ($modified) { |
|
| 1194 | - $array['size'] = strlen($array['carddata']); |
|
| 1195 | - } |
|
| 1196 | - return $array; |
|
| 1197 | - }, $cards); |
|
| 1198 | - } |
|
| 1199 | - |
|
| 1200 | - /** |
|
| 1201 | - * @param int $bookId |
|
| 1202 | - * @param string $name |
|
| 1203 | - * @return array |
|
| 1204 | - */ |
|
| 1205 | - public function collectCardProperties($bookId, $name) { |
|
| 1206 | - $query = $this->db->getQueryBuilder(); |
|
| 1207 | - $result = $query->selectDistinct('value') |
|
| 1208 | - ->from($this->dbCardsPropertiesTable) |
|
| 1209 | - ->where($query->expr()->eq('name', $query->createNamedParameter($name))) |
|
| 1210 | - ->andWhere($query->expr()->eq('addressbookid', $query->createNamedParameter($bookId))) |
|
| 1211 | - ->executeQuery(); |
|
| 1212 | - |
|
| 1213 | - $all = $result->fetchAll(PDO::FETCH_COLUMN); |
|
| 1214 | - $result->closeCursor(); |
|
| 1215 | - |
|
| 1216 | - return $all; |
|
| 1217 | - } |
|
| 1218 | - |
|
| 1219 | - /** |
|
| 1220 | - * get URI from a given contact |
|
| 1221 | - * |
|
| 1222 | - * @param int $id |
|
| 1223 | - * @return string |
|
| 1224 | - */ |
|
| 1225 | - public function getCardUri($id) { |
|
| 1226 | - $query = $this->db->getQueryBuilder(); |
|
| 1227 | - $query->select('uri')->from($this->dbCardsTable) |
|
| 1228 | - ->where($query->expr()->eq('id', $query->createParameter('id'))) |
|
| 1229 | - ->setParameter('id', $id); |
|
| 1230 | - |
|
| 1231 | - $result = $query->executeQuery(); |
|
| 1232 | - $uri = $result->fetch(); |
|
| 1233 | - $result->closeCursor(); |
|
| 1234 | - |
|
| 1235 | - if (!isset($uri['uri'])) { |
|
| 1236 | - throw new \InvalidArgumentException('Card does not exists: ' . $id); |
|
| 1237 | - } |
|
| 1238 | - |
|
| 1239 | - return $uri['uri']; |
|
| 1240 | - } |
|
| 1241 | - |
|
| 1242 | - /** |
|
| 1243 | - * return contact with the given URI |
|
| 1244 | - * |
|
| 1245 | - * @param int $addressBookId |
|
| 1246 | - * @param string $uri |
|
| 1247 | - * @returns array |
|
| 1248 | - */ |
|
| 1249 | - public function getContact($addressBookId, $uri) { |
|
| 1250 | - $result = []; |
|
| 1251 | - $query = $this->db->getQueryBuilder(); |
|
| 1252 | - $query->select('*')->from($this->dbCardsTable) |
|
| 1253 | - ->where($query->expr()->eq('uri', $query->createNamedParameter($uri))) |
|
| 1254 | - ->andWhere($query->expr()->eq('addressbookid', $query->createNamedParameter($addressBookId))); |
|
| 1255 | - $queryResult = $query->executeQuery(); |
|
| 1256 | - $contact = $queryResult->fetch(); |
|
| 1257 | - $queryResult->closeCursor(); |
|
| 1258 | - |
|
| 1259 | - if (is_array($contact)) { |
|
| 1260 | - $modified = false; |
|
| 1261 | - $contact['etag'] = '"' . $contact['etag'] . '"'; |
|
| 1262 | - $contact['carddata'] = $this->readBlob($contact['carddata'], $modified); |
|
| 1263 | - if ($modified) { |
|
| 1264 | - $contact['size'] = strlen($contact['carddata']); |
|
| 1265 | - } |
|
| 1266 | - |
|
| 1267 | - $result = $contact; |
|
| 1268 | - } |
|
| 1269 | - |
|
| 1270 | - return $result; |
|
| 1271 | - } |
|
| 1272 | - |
|
| 1273 | - /** |
|
| 1274 | - * Returns the list of people whom this address book is shared with. |
|
| 1275 | - * |
|
| 1276 | - * Every element in this array should have the following properties: |
|
| 1277 | - * * href - Often a mailto: address |
|
| 1278 | - * * commonName - Optional, for example a first + last name |
|
| 1279 | - * * status - See the Sabre\CalDAV\SharingPlugin::STATUS_ constants. |
|
| 1280 | - * * readOnly - boolean |
|
| 1281 | - * |
|
| 1282 | - * @return list<array{href: string, commonName: string, status: int, readOnly: bool, '{http://owncloud.org/ns}principal': string, '{http://owncloud.org/ns}group-share': bool}> |
|
| 1283 | - */ |
|
| 1284 | - public function getShares(int $addressBookId): array { |
|
| 1285 | - return $this->sharingBackend->getShares($addressBookId); |
|
| 1286 | - } |
|
| 1287 | - |
|
| 1288 | - /** |
|
| 1289 | - * update properties table |
|
| 1290 | - * |
|
| 1291 | - * @param int $addressBookId |
|
| 1292 | - * @param string $cardUri |
|
| 1293 | - * @param string $vCardSerialized |
|
| 1294 | - */ |
|
| 1295 | - protected function updateProperties($addressBookId, $cardUri, $vCardSerialized) { |
|
| 1296 | - $this->atomic(function () use ($addressBookId, $cardUri, $vCardSerialized): void { |
|
| 1297 | - $cardId = $this->getCardId($addressBookId, $cardUri); |
|
| 1298 | - $vCard = $this->readCard($vCardSerialized); |
|
| 1299 | - |
|
| 1300 | - $this->purgeProperties($addressBookId, $cardId); |
|
| 1301 | - |
|
| 1302 | - $query = $this->db->getQueryBuilder(); |
|
| 1303 | - $query->insert($this->dbCardsPropertiesTable) |
|
| 1304 | - ->values( |
|
| 1305 | - [ |
|
| 1306 | - 'addressbookid' => $query->createNamedParameter($addressBookId), |
|
| 1307 | - 'cardid' => $query->createNamedParameter($cardId), |
|
| 1308 | - 'name' => $query->createParameter('name'), |
|
| 1309 | - 'value' => $query->createParameter('value'), |
|
| 1310 | - 'preferred' => $query->createParameter('preferred') |
|
| 1311 | - ] |
|
| 1312 | - ); |
|
| 1313 | - |
|
| 1314 | - foreach ($vCard->children() as $property) { |
|
| 1315 | - if (!in_array($property->name, self::$indexProperties)) { |
|
| 1316 | - continue; |
|
| 1317 | - } |
|
| 1318 | - $preferred = 0; |
|
| 1319 | - foreach ($property->parameters as $parameter) { |
|
| 1320 | - if ($parameter->name === 'TYPE' && strtoupper($parameter->getValue()) === 'PREF') { |
|
| 1321 | - $preferred = 1; |
|
| 1322 | - break; |
|
| 1323 | - } |
|
| 1324 | - } |
|
| 1325 | - $query->setParameter('name', $property->name); |
|
| 1326 | - $query->setParameter('value', mb_strcut($property->getValue(), 0, 254)); |
|
| 1327 | - $query->setParameter('preferred', $preferred); |
|
| 1328 | - $query->executeStatement(); |
|
| 1329 | - } |
|
| 1330 | - }, $this->db); |
|
| 1331 | - } |
|
| 1332 | - |
|
| 1333 | - /** |
|
| 1334 | - * read vCard data into a vCard object |
|
| 1335 | - * |
|
| 1336 | - * @param string $cardData |
|
| 1337 | - * @return VCard |
|
| 1338 | - */ |
|
| 1339 | - protected function readCard($cardData) { |
|
| 1340 | - return Reader::read($cardData); |
|
| 1341 | - } |
|
| 1342 | - |
|
| 1343 | - /** |
|
| 1344 | - * delete all properties from a given card |
|
| 1345 | - * |
|
| 1346 | - * @param int $addressBookId |
|
| 1347 | - * @param int $cardId |
|
| 1348 | - */ |
|
| 1349 | - protected function purgeProperties($addressBookId, $cardId) { |
|
| 1350 | - $query = $this->db->getQueryBuilder(); |
|
| 1351 | - $query->delete($this->dbCardsPropertiesTable) |
|
| 1352 | - ->where($query->expr()->eq('cardid', $query->createNamedParameter($cardId))) |
|
| 1353 | - ->andWhere($query->expr()->eq('addressbookid', $query->createNamedParameter($addressBookId))); |
|
| 1354 | - $query->executeStatement(); |
|
| 1355 | - } |
|
| 1356 | - |
|
| 1357 | - /** |
|
| 1358 | - * Get ID from a given contact |
|
| 1359 | - */ |
|
| 1360 | - protected function getCardId(int $addressBookId, string $uri): int { |
|
| 1361 | - $query = $this->db->getQueryBuilder(); |
|
| 1362 | - $query->select('id')->from($this->dbCardsTable) |
|
| 1363 | - ->where($query->expr()->eq('uri', $query->createNamedParameter($uri))) |
|
| 1364 | - ->andWhere($query->expr()->eq('addressbookid', $query->createNamedParameter($addressBookId))); |
|
| 1365 | - |
|
| 1366 | - $result = $query->executeQuery(); |
|
| 1367 | - $cardIds = $result->fetch(); |
|
| 1368 | - $result->closeCursor(); |
|
| 1369 | - |
|
| 1370 | - if (!isset($cardIds['id'])) { |
|
| 1371 | - throw new \InvalidArgumentException('Card does not exists: ' . $uri); |
|
| 1372 | - } |
|
| 1373 | - |
|
| 1374 | - return (int)$cardIds['id']; |
|
| 1375 | - } |
|
| 1376 | - |
|
| 1377 | - /** |
|
| 1378 | - * For shared address books the sharee is set in the ACL of the address book |
|
| 1379 | - * |
|
| 1380 | - * @param int $addressBookId |
|
| 1381 | - * @param list<array{privilege: string, principal: string, protected: bool}> $acl |
|
| 1382 | - * @return list<array{privilege: string, principal: string, protected: bool}> |
|
| 1383 | - */ |
|
| 1384 | - public function applyShareAcl(int $addressBookId, array $acl): array { |
|
| 1385 | - $shares = $this->sharingBackend->getShares($addressBookId); |
|
| 1386 | - return $this->sharingBackend->applyShareAcl($shares, $acl); |
|
| 1387 | - } |
|
| 1388 | - |
|
| 1389 | - /** |
|
| 1390 | - * @throws \InvalidArgumentException |
|
| 1391 | - */ |
|
| 1392 | - public function pruneOutdatedSyncTokens(int $keep, int $retention): int { |
|
| 1393 | - if ($keep < 0) { |
|
| 1394 | - throw new \InvalidArgumentException(); |
|
| 1395 | - } |
|
| 1396 | - |
|
| 1397 | - $query = $this->db->getQueryBuilder(); |
|
| 1398 | - $query->select($query->func()->max('id')) |
|
| 1399 | - ->from('addressbookchanges'); |
|
| 1400 | - |
|
| 1401 | - $result = $query->executeQuery(); |
|
| 1402 | - $maxId = (int)$result->fetchOne(); |
|
| 1403 | - $result->closeCursor(); |
|
| 1404 | - if (!$maxId || $maxId < $keep) { |
|
| 1405 | - return 0; |
|
| 1406 | - } |
|
| 1407 | - |
|
| 1408 | - $query = $this->db->getQueryBuilder(); |
|
| 1409 | - $query->delete('addressbookchanges') |
|
| 1410 | - ->where( |
|
| 1411 | - $query->expr()->lte('id', $query->createNamedParameter($maxId - $keep, IQueryBuilder::PARAM_INT), IQueryBuilder::PARAM_INT), |
|
| 1412 | - $query->expr()->lte('created_at', $query->createNamedParameter($retention)), |
|
| 1413 | - ); |
|
| 1414 | - return $query->executeStatement(); |
|
| 1415 | - } |
|
| 1416 | - |
|
| 1417 | - private function convertPrincipal(string $principalUri, bool $toV2): string { |
|
| 1418 | - if ($this->principalBackend->getPrincipalPrefix() === 'principals') { |
|
| 1419 | - [, $name] = \Sabre\Uri\split($principalUri); |
|
| 1420 | - if ($toV2 === true) { |
|
| 1421 | - return "principals/users/$name"; |
|
| 1422 | - } |
|
| 1423 | - return "principals/$name"; |
|
| 1424 | - } |
|
| 1425 | - return $principalUri; |
|
| 1426 | - } |
|
| 1427 | - |
|
| 1428 | - private function addOwnerPrincipal(array &$addressbookInfo): void { |
|
| 1429 | - $ownerPrincipalKey = '{' . \OCA\DAV\DAV\Sharing\Plugin::NS_OWNCLOUD . '}owner-principal'; |
|
| 1430 | - $displaynameKey = '{' . \OCA\DAV\DAV\Sharing\Plugin::NS_NEXTCLOUD . '}owner-displayname'; |
|
| 1431 | - if (isset($addressbookInfo[$ownerPrincipalKey])) { |
|
| 1432 | - $uri = $addressbookInfo[$ownerPrincipalKey]; |
|
| 1433 | - } else { |
|
| 1434 | - $uri = $addressbookInfo['principaluri']; |
|
| 1435 | - } |
|
| 1436 | - |
|
| 1437 | - $principalInformation = $this->principalBackend->getPrincipalByPath($uri); |
|
| 1438 | - if (isset($principalInformation['{DAV:}displayname'])) { |
|
| 1439 | - $addressbookInfo[$displaynameKey] = $principalInformation['{DAV:}displayname']; |
|
| 1440 | - } |
|
| 1441 | - } |
|
| 1442 | - |
|
| 1443 | - /** |
|
| 1444 | - * Extract UID from vcard |
|
| 1445 | - * |
|
| 1446 | - * @param string $cardData the vcard raw data |
|
| 1447 | - * @return string the uid |
|
| 1448 | - * @throws BadRequest if no UID is available or vcard is empty |
|
| 1449 | - */ |
|
| 1450 | - private function getUID(string $cardData): string { |
|
| 1451 | - if ($cardData !== '') { |
|
| 1452 | - $vCard = Reader::read($cardData); |
|
| 1453 | - if ($vCard->UID) { |
|
| 1454 | - $uid = $vCard->UID->getValue(); |
|
| 1455 | - return $uid; |
|
| 1456 | - } |
|
| 1457 | - // should already be handled, but just in case |
|
| 1458 | - throw new BadRequest('vCards on CardDAV servers MUST have a UID property'); |
|
| 1459 | - } |
|
| 1460 | - // should already be handled, but just in case |
|
| 1461 | - throw new BadRequest('vCard can not be empty'); |
|
| 1462 | - } |
|
| 37 | + use TTransactional; |
|
| 38 | + public const PERSONAL_ADDRESSBOOK_URI = 'contacts'; |
|
| 39 | + public const PERSONAL_ADDRESSBOOK_NAME = 'Contacts'; |
|
| 40 | + |
|
| 41 | + private string $dbCardsTable = 'cards'; |
|
| 42 | + private string $dbCardsPropertiesTable = 'cards_properties'; |
|
| 43 | + |
|
| 44 | + /** @var array properties to index */ |
|
| 45 | + public static array $indexProperties = [ |
|
| 46 | + 'BDAY', 'UID', 'N', 'FN', 'TITLE', 'ROLE', 'NOTE', 'NICKNAME', |
|
| 47 | + 'ORG', 'CATEGORIES', 'EMAIL', 'TEL', 'IMPP', 'ADR', 'URL', 'GEO', |
|
| 48 | + 'CLOUD', 'X-SOCIALPROFILE']; |
|
| 49 | + |
|
| 50 | + /** |
|
| 51 | + * @var string[] Map of uid => display name |
|
| 52 | + */ |
|
| 53 | + protected array $userDisplayNames; |
|
| 54 | + private array $etagCache = []; |
|
| 55 | + |
|
| 56 | + public function __construct( |
|
| 57 | + private IDBConnection $db, |
|
| 58 | + private Principal $principalBackend, |
|
| 59 | + private IUserManager $userManager, |
|
| 60 | + private IEventDispatcher $dispatcher, |
|
| 61 | + private Sharing\Backend $sharingBackend, |
|
| 62 | + ) { |
|
| 63 | + } |
|
| 64 | + |
|
| 65 | + /** |
|
| 66 | + * Return the number of address books for a principal |
|
| 67 | + * |
|
| 68 | + * @param $principalUri |
|
| 69 | + * @return int |
|
| 70 | + */ |
|
| 71 | + public function getAddressBooksForUserCount($principalUri) { |
|
| 72 | + $principalUri = $this->convertPrincipal($principalUri, true); |
|
| 73 | + $query = $this->db->getQueryBuilder(); |
|
| 74 | + $query->select($query->func()->count('*')) |
|
| 75 | + ->from('addressbooks') |
|
| 76 | + ->where($query->expr()->eq('principaluri', $query->createNamedParameter($principalUri))); |
|
| 77 | + |
|
| 78 | + $result = $query->executeQuery(); |
|
| 79 | + $column = (int)$result->fetchOne(); |
|
| 80 | + $result->closeCursor(); |
|
| 81 | + return $column; |
|
| 82 | + } |
|
| 83 | + |
|
| 84 | + /** |
|
| 85 | + * Returns the list of address books for a specific user. |
|
| 86 | + * |
|
| 87 | + * Every addressbook should have the following properties: |
|
| 88 | + * id - an arbitrary unique id |
|
| 89 | + * uri - the 'basename' part of the url |
|
| 90 | + * principaluri - Same as the passed parameter |
|
| 91 | + * |
|
| 92 | + * Any additional clark-notation property may be passed besides this. Some |
|
| 93 | + * common ones are : |
|
| 94 | + * {DAV:}displayname |
|
| 95 | + * {urn:ietf:params:xml:ns:carddav}addressbook-description |
|
| 96 | + * {http://calendarserver.org/ns/}getctag |
|
| 97 | + * |
|
| 98 | + * @param string $principalUri |
|
| 99 | + * @return array |
|
| 100 | + */ |
|
| 101 | + public function getAddressBooksForUser($principalUri) { |
|
| 102 | + return $this->atomic(function () use ($principalUri) { |
|
| 103 | + $principalUriOriginal = $principalUri; |
|
| 104 | + $principalUri = $this->convertPrincipal($principalUri, true); |
|
| 105 | + $select = $this->db->getQueryBuilder(); |
|
| 106 | + $select->select(['id', 'uri', 'displayname', 'principaluri', 'description', 'synctoken']) |
|
| 107 | + ->from('addressbooks') |
|
| 108 | + ->where($select->expr()->eq('principaluri', $select->createNamedParameter($principalUri))); |
|
| 109 | + |
|
| 110 | + $addressBooks = []; |
|
| 111 | + |
|
| 112 | + $result = $select->executeQuery(); |
|
| 113 | + while ($row = $result->fetch()) { |
|
| 114 | + $addressBooks[$row['id']] = [ |
|
| 115 | + 'id' => $row['id'], |
|
| 116 | + 'uri' => $row['uri'], |
|
| 117 | + 'principaluri' => $this->convertPrincipal($row['principaluri'], false), |
|
| 118 | + '{DAV:}displayname' => $row['displayname'], |
|
| 119 | + '{' . Plugin::NS_CARDDAV . '}addressbook-description' => $row['description'], |
|
| 120 | + '{http://calendarserver.org/ns/}getctag' => $row['synctoken'], |
|
| 121 | + '{http://sabredav.org/ns}sync-token' => $row['synctoken'] ?: '0', |
|
| 122 | + ]; |
|
| 123 | + |
|
| 124 | + $this->addOwnerPrincipal($addressBooks[$row['id']]); |
|
| 125 | + } |
|
| 126 | + $result->closeCursor(); |
|
| 127 | + |
|
| 128 | + // query for shared addressbooks |
|
| 129 | + $principals = $this->principalBackend->getGroupMembership($principalUriOriginal, true); |
|
| 130 | + $principals = array_merge($principals, $this->principalBackend->getCircleMembership($principalUriOriginal)); |
|
| 131 | + |
|
| 132 | + $principals[] = $principalUri; |
|
| 133 | + |
|
| 134 | + $select = $this->db->getQueryBuilder(); |
|
| 135 | + $subSelect = $this->db->getQueryBuilder(); |
|
| 136 | + |
|
| 137 | + $subSelect->select('id') |
|
| 138 | + ->from('dav_shares', 'd') |
|
| 139 | + ->where($subSelect->expr()->eq('d.access', $select->createNamedParameter(\OCA\DAV\CardDAV\Sharing\Backend::ACCESS_UNSHARED, IQueryBuilder::PARAM_INT), IQueryBuilder::PARAM_INT)) |
|
| 140 | + ->andWhere($subSelect->expr()->eq('d.principaluri', $select->createNamedParameter($principalUri, IQueryBuilder::PARAM_STR), IQueryBuilder::PARAM_STR)); |
|
| 141 | + |
|
| 142 | + |
|
| 143 | + $select->select(['a.id', 'a.uri', 'a.displayname', 'a.principaluri', 'a.description', 'a.synctoken', 's.access']) |
|
| 144 | + ->from('dav_shares', 's') |
|
| 145 | + ->join('s', 'addressbooks', 'a', $select->expr()->eq('s.resourceid', 'a.id')) |
|
| 146 | + ->where($select->expr()->in('s.principaluri', $select->createNamedParameter($principals, IQueryBuilder::PARAM_STR_ARRAY))) |
|
| 147 | + ->andWhere($select->expr()->eq('s.type', $select->createNamedParameter('addressbook', IQueryBuilder::PARAM_STR))) |
|
| 148 | + ->andWhere($select->expr()->notIn('s.id', $select->createFunction($subSelect->getSQL()), IQueryBuilder::PARAM_INT_ARRAY)); |
|
| 149 | + $result = $select->executeQuery(); |
|
| 150 | + |
|
| 151 | + $readOnlyPropertyName = '{' . \OCA\DAV\DAV\Sharing\Plugin::NS_OWNCLOUD . '}read-only'; |
|
| 152 | + while ($row = $result->fetch()) { |
|
| 153 | + if ($row['principaluri'] === $principalUri) { |
|
| 154 | + continue; |
|
| 155 | + } |
|
| 156 | + |
|
| 157 | + $readOnly = (int)$row['access'] === Backend::ACCESS_READ; |
|
| 158 | + if (isset($addressBooks[$row['id']])) { |
|
| 159 | + if ($readOnly) { |
|
| 160 | + // New share can not have more permissions then the old one. |
|
| 161 | + continue; |
|
| 162 | + } |
|
| 163 | + if (isset($addressBooks[$row['id']][$readOnlyPropertyName]) && |
|
| 164 | + $addressBooks[$row['id']][$readOnlyPropertyName] === 0) { |
|
| 165 | + // Old share is already read-write, no more permissions can be gained |
|
| 166 | + continue; |
|
| 167 | + } |
|
| 168 | + } |
|
| 169 | + |
|
| 170 | + [, $name] = \Sabre\Uri\split($row['principaluri']); |
|
| 171 | + $uri = $row['uri'] . '_shared_by_' . $name; |
|
| 172 | + $displayName = $row['displayname'] . ' (' . ($this->userManager->getDisplayName($name) ?? $name ?? '') . ')'; |
|
| 173 | + |
|
| 174 | + $addressBooks[$row['id']] = [ |
|
| 175 | + 'id' => $row['id'], |
|
| 176 | + 'uri' => $uri, |
|
| 177 | + 'principaluri' => $principalUriOriginal, |
|
| 178 | + '{DAV:}displayname' => $displayName, |
|
| 179 | + '{' . Plugin::NS_CARDDAV . '}addressbook-description' => $row['description'], |
|
| 180 | + '{http://calendarserver.org/ns/}getctag' => $row['synctoken'], |
|
| 181 | + '{http://sabredav.org/ns}sync-token' => $row['synctoken'] ?: '0', |
|
| 182 | + '{' . \OCA\DAV\DAV\Sharing\Plugin::NS_OWNCLOUD . '}owner-principal' => $row['principaluri'], |
|
| 183 | + $readOnlyPropertyName => $readOnly, |
|
| 184 | + ]; |
|
| 185 | + |
|
| 186 | + $this->addOwnerPrincipal($addressBooks[$row['id']]); |
|
| 187 | + } |
|
| 188 | + $result->closeCursor(); |
|
| 189 | + |
|
| 190 | + return array_values($addressBooks); |
|
| 191 | + }, $this->db); |
|
| 192 | + } |
|
| 193 | + |
|
| 194 | + public function getUsersOwnAddressBooks($principalUri) { |
|
| 195 | + $principalUri = $this->convertPrincipal($principalUri, true); |
|
| 196 | + $query = $this->db->getQueryBuilder(); |
|
| 197 | + $query->select(['id', 'uri', 'displayname', 'principaluri', 'description', 'synctoken']) |
|
| 198 | + ->from('addressbooks') |
|
| 199 | + ->where($query->expr()->eq('principaluri', $query->createNamedParameter($principalUri))); |
|
| 200 | + |
|
| 201 | + $addressBooks = []; |
|
| 202 | + |
|
| 203 | + $result = $query->executeQuery(); |
|
| 204 | + while ($row = $result->fetch()) { |
|
| 205 | + $addressBooks[$row['id']] = [ |
|
| 206 | + 'id' => $row['id'], |
|
| 207 | + 'uri' => $row['uri'], |
|
| 208 | + 'principaluri' => $this->convertPrincipal($row['principaluri'], false), |
|
| 209 | + '{DAV:}displayname' => $row['displayname'], |
|
| 210 | + '{' . Plugin::NS_CARDDAV . '}addressbook-description' => $row['description'], |
|
| 211 | + '{http://calendarserver.org/ns/}getctag' => $row['synctoken'], |
|
| 212 | + '{http://sabredav.org/ns}sync-token' => $row['synctoken'] ?: '0', |
|
| 213 | + ]; |
|
| 214 | + |
|
| 215 | + $this->addOwnerPrincipal($addressBooks[$row['id']]); |
|
| 216 | + } |
|
| 217 | + $result->closeCursor(); |
|
| 218 | + |
|
| 219 | + return array_values($addressBooks); |
|
| 220 | + } |
|
| 221 | + |
|
| 222 | + /** |
|
| 223 | + * @param int $addressBookId |
|
| 224 | + */ |
|
| 225 | + public function getAddressBookById(int $addressBookId): ?array { |
|
| 226 | + $query = $this->db->getQueryBuilder(); |
|
| 227 | + $result = $query->select(['id', 'uri', 'displayname', 'principaluri', 'description', 'synctoken']) |
|
| 228 | + ->from('addressbooks') |
|
| 229 | + ->where($query->expr()->eq('id', $query->createNamedParameter($addressBookId, IQueryBuilder::PARAM_INT))) |
|
| 230 | + ->executeQuery(); |
|
| 231 | + $row = $result->fetch(); |
|
| 232 | + $result->closeCursor(); |
|
| 233 | + if (!$row) { |
|
| 234 | + return null; |
|
| 235 | + } |
|
| 236 | + |
|
| 237 | + $addressBook = [ |
|
| 238 | + 'id' => $row['id'], |
|
| 239 | + 'uri' => $row['uri'], |
|
| 240 | + 'principaluri' => $row['principaluri'], |
|
| 241 | + '{DAV:}displayname' => $row['displayname'], |
|
| 242 | + '{' . Plugin::NS_CARDDAV . '}addressbook-description' => $row['description'], |
|
| 243 | + '{http://calendarserver.org/ns/}getctag' => $row['synctoken'], |
|
| 244 | + '{http://sabredav.org/ns}sync-token' => $row['synctoken'] ?: '0', |
|
| 245 | + ]; |
|
| 246 | + |
|
| 247 | + $this->addOwnerPrincipal($addressBook); |
|
| 248 | + |
|
| 249 | + return $addressBook; |
|
| 250 | + } |
|
| 251 | + |
|
| 252 | + public function getAddressBooksByUri(string $principal, string $addressBookUri): ?array { |
|
| 253 | + $query = $this->db->getQueryBuilder(); |
|
| 254 | + $result = $query->select(['id', 'uri', 'displayname', 'principaluri', 'description', 'synctoken']) |
|
| 255 | + ->from('addressbooks') |
|
| 256 | + ->where($query->expr()->eq('uri', $query->createNamedParameter($addressBookUri))) |
|
| 257 | + ->andWhere($query->expr()->eq('principaluri', $query->createNamedParameter($principal))) |
|
| 258 | + ->setMaxResults(1) |
|
| 259 | + ->executeQuery(); |
|
| 260 | + |
|
| 261 | + $row = $result->fetch(); |
|
| 262 | + $result->closeCursor(); |
|
| 263 | + if ($row === false) { |
|
| 264 | + return null; |
|
| 265 | + } |
|
| 266 | + |
|
| 267 | + $addressBook = [ |
|
| 268 | + 'id' => $row['id'], |
|
| 269 | + 'uri' => $row['uri'], |
|
| 270 | + 'principaluri' => $row['principaluri'], |
|
| 271 | + '{DAV:}displayname' => $row['displayname'], |
|
| 272 | + '{' . Plugin::NS_CARDDAV . '}addressbook-description' => $row['description'], |
|
| 273 | + '{http://calendarserver.org/ns/}getctag' => $row['synctoken'], |
|
| 274 | + '{http://sabredav.org/ns}sync-token' => $row['synctoken'] ?: '0', |
|
| 275 | + |
|
| 276 | + ]; |
|
| 277 | + |
|
| 278 | + // system address books are always read only |
|
| 279 | + if ($principal === 'principals/system/system') { |
|
| 280 | + $addressBook['{' . \OCA\DAV\DAV\Sharing\Plugin::NS_OWNCLOUD . '}owner-principal'] = $row['principaluri']; |
|
| 281 | + $addressBook['{' . \OCA\DAV\DAV\Sharing\Plugin::NS_OWNCLOUD . '}read-only'] = true; |
|
| 282 | + } |
|
| 283 | + |
|
| 284 | + $this->addOwnerPrincipal($addressBook); |
|
| 285 | + |
|
| 286 | + return $addressBook; |
|
| 287 | + } |
|
| 288 | + |
|
| 289 | + /** |
|
| 290 | + * Updates properties for an address book. |
|
| 291 | + * |
|
| 292 | + * The list of mutations is stored in a Sabre\DAV\PropPatch object. |
|
| 293 | + * To do the actual updates, you must tell this object which properties |
|
| 294 | + * you're going to process with the handle() method. |
|
| 295 | + * |
|
| 296 | + * Calling the handle method is like telling the PropPatch object "I |
|
| 297 | + * promise I can handle updating this property". |
|
| 298 | + * |
|
| 299 | + * Read the PropPatch documentation for more info and examples. |
|
| 300 | + * |
|
| 301 | + * @param string $addressBookId |
|
| 302 | + * @param \Sabre\DAV\PropPatch $propPatch |
|
| 303 | + * @return void |
|
| 304 | + */ |
|
| 305 | + public function updateAddressBook($addressBookId, \Sabre\DAV\PropPatch $propPatch) { |
|
| 306 | + $supportedProperties = [ |
|
| 307 | + '{DAV:}displayname', |
|
| 308 | + '{' . Plugin::NS_CARDDAV . '}addressbook-description', |
|
| 309 | + ]; |
|
| 310 | + |
|
| 311 | + $propPatch->handle($supportedProperties, function ($mutations) use ($addressBookId) { |
|
| 312 | + $updates = []; |
|
| 313 | + foreach ($mutations as $property => $newValue) { |
|
| 314 | + switch ($property) { |
|
| 315 | + case '{DAV:}displayname': |
|
| 316 | + $updates['displayname'] = $newValue; |
|
| 317 | + break; |
|
| 318 | + case '{' . Plugin::NS_CARDDAV . '}addressbook-description': |
|
| 319 | + $updates['description'] = $newValue; |
|
| 320 | + break; |
|
| 321 | + } |
|
| 322 | + } |
|
| 323 | + [$addressBookRow, $shares] = $this->atomic(function () use ($addressBookId, $updates) { |
|
| 324 | + $query = $this->db->getQueryBuilder(); |
|
| 325 | + $query->update('addressbooks'); |
|
| 326 | + |
|
| 327 | + foreach ($updates as $key => $value) { |
|
| 328 | + $query->set($key, $query->createNamedParameter($value)); |
|
| 329 | + } |
|
| 330 | + $query->where($query->expr()->eq('id', $query->createNamedParameter($addressBookId))) |
|
| 331 | + ->executeStatement(); |
|
| 332 | + |
|
| 333 | + $this->addChange($addressBookId, '', 2); |
|
| 334 | + |
|
| 335 | + $addressBookRow = $this->getAddressBookById((int)$addressBookId); |
|
| 336 | + $shares = $this->getShares((int)$addressBookId); |
|
| 337 | + return [$addressBookRow, $shares]; |
|
| 338 | + }, $this->db); |
|
| 339 | + |
|
| 340 | + $this->dispatcher->dispatchTyped(new AddressBookUpdatedEvent((int)$addressBookId, $addressBookRow, $shares, $mutations)); |
|
| 341 | + |
|
| 342 | + return true; |
|
| 343 | + }); |
|
| 344 | + } |
|
| 345 | + |
|
| 346 | + /** |
|
| 347 | + * Creates a new address book |
|
| 348 | + * |
|
| 349 | + * @param string $principalUri |
|
| 350 | + * @param string $url Just the 'basename' of the url. |
|
| 351 | + * @param array $properties |
|
| 352 | + * @return int |
|
| 353 | + * @throws BadRequest |
|
| 354 | + * @throws Exception |
|
| 355 | + */ |
|
| 356 | + public function createAddressBook($principalUri, $url, array $properties) { |
|
| 357 | + if (strlen($url) > 255) { |
|
| 358 | + throw new BadRequest('URI too long. Address book not created'); |
|
| 359 | + } |
|
| 360 | + |
|
| 361 | + $values = [ |
|
| 362 | + 'displayname' => null, |
|
| 363 | + 'description' => null, |
|
| 364 | + 'principaluri' => $principalUri, |
|
| 365 | + 'uri' => $url, |
|
| 366 | + 'synctoken' => 1 |
|
| 367 | + ]; |
|
| 368 | + |
|
| 369 | + foreach ($properties as $property => $newValue) { |
|
| 370 | + switch ($property) { |
|
| 371 | + case '{DAV:}displayname': |
|
| 372 | + $values['displayname'] = $newValue; |
|
| 373 | + break; |
|
| 374 | + case '{' . Plugin::NS_CARDDAV . '}addressbook-description': |
|
| 375 | + $values['description'] = $newValue; |
|
| 376 | + break; |
|
| 377 | + default: |
|
| 378 | + throw new BadRequest('Unknown property: ' . $property); |
|
| 379 | + } |
|
| 380 | + } |
|
| 381 | + |
|
| 382 | + // Fallback to make sure the displayname is set. Some clients may refuse |
|
| 383 | + // to work with addressbooks not having a displayname. |
|
| 384 | + if (is_null($values['displayname'])) { |
|
| 385 | + $values['displayname'] = $url; |
|
| 386 | + } |
|
| 387 | + |
|
| 388 | + [$addressBookId, $addressBookRow] = $this->atomic(function () use ($values) { |
|
| 389 | + $query = $this->db->getQueryBuilder(); |
|
| 390 | + $query->insert('addressbooks') |
|
| 391 | + ->values([ |
|
| 392 | + 'uri' => $query->createParameter('uri'), |
|
| 393 | + 'displayname' => $query->createParameter('displayname'), |
|
| 394 | + 'description' => $query->createParameter('description'), |
|
| 395 | + 'principaluri' => $query->createParameter('principaluri'), |
|
| 396 | + 'synctoken' => $query->createParameter('synctoken'), |
|
| 397 | + ]) |
|
| 398 | + ->setParameters($values) |
|
| 399 | + ->executeStatement(); |
|
| 400 | + |
|
| 401 | + $addressBookId = $query->getLastInsertId(); |
|
| 402 | + return [ |
|
| 403 | + $addressBookId, |
|
| 404 | + $this->getAddressBookById($addressBookId), |
|
| 405 | + ]; |
|
| 406 | + }, $this->db); |
|
| 407 | + |
|
| 408 | + $this->dispatcher->dispatchTyped(new AddressBookCreatedEvent($addressBookId, $addressBookRow)); |
|
| 409 | + |
|
| 410 | + return $addressBookId; |
|
| 411 | + } |
|
| 412 | + |
|
| 413 | + /** |
|
| 414 | + * Deletes an entire addressbook and all its contents |
|
| 415 | + * |
|
| 416 | + * @param mixed $addressBookId |
|
| 417 | + * @return void |
|
| 418 | + */ |
|
| 419 | + public function deleteAddressBook($addressBookId) { |
|
| 420 | + $this->atomic(function () use ($addressBookId): void { |
|
| 421 | + $addressBookId = (int)$addressBookId; |
|
| 422 | + $addressBookData = $this->getAddressBookById($addressBookId); |
|
| 423 | + $shares = $this->getShares($addressBookId); |
|
| 424 | + |
|
| 425 | + $query = $this->db->getQueryBuilder(); |
|
| 426 | + $query->delete($this->dbCardsTable) |
|
| 427 | + ->where($query->expr()->eq('addressbookid', $query->createParameter('addressbookid'))) |
|
| 428 | + ->setParameter('addressbookid', $addressBookId, IQueryBuilder::PARAM_INT) |
|
| 429 | + ->executeStatement(); |
|
| 430 | + |
|
| 431 | + $query = $this->db->getQueryBuilder(); |
|
| 432 | + $query->delete('addressbookchanges') |
|
| 433 | + ->where($query->expr()->eq('addressbookid', $query->createParameter('addressbookid'))) |
|
| 434 | + ->setParameter('addressbookid', $addressBookId, IQueryBuilder::PARAM_INT) |
|
| 435 | + ->executeStatement(); |
|
| 436 | + |
|
| 437 | + $query = $this->db->getQueryBuilder(); |
|
| 438 | + $query->delete('addressbooks') |
|
| 439 | + ->where($query->expr()->eq('id', $query->createParameter('id'))) |
|
| 440 | + ->setParameter('id', $addressBookId, IQueryBuilder::PARAM_INT) |
|
| 441 | + ->executeStatement(); |
|
| 442 | + |
|
| 443 | + $this->sharingBackend->deleteAllShares($addressBookId); |
|
| 444 | + |
|
| 445 | + $query = $this->db->getQueryBuilder(); |
|
| 446 | + $query->delete($this->dbCardsPropertiesTable) |
|
| 447 | + ->where($query->expr()->eq('addressbookid', $query->createNamedParameter($addressBookId, IQueryBuilder::PARAM_INT))) |
|
| 448 | + ->executeStatement(); |
|
| 449 | + |
|
| 450 | + if ($addressBookData) { |
|
| 451 | + $this->dispatcher->dispatchTyped(new AddressBookDeletedEvent($addressBookId, $addressBookData, $shares)); |
|
| 452 | + } |
|
| 453 | + }, $this->db); |
|
| 454 | + } |
|
| 455 | + |
|
| 456 | + /** |
|
| 457 | + * Returns all cards for a specific addressbook id. |
|
| 458 | + * |
|
| 459 | + * This method should return the following properties for each card: |
|
| 460 | + * * carddata - raw vcard data |
|
| 461 | + * * uri - Some unique url |
|
| 462 | + * * lastmodified - A unix timestamp |
|
| 463 | + * |
|
| 464 | + * It's recommended to also return the following properties: |
|
| 465 | + * * etag - A unique etag. This must change every time the card changes. |
|
| 466 | + * * size - The size of the card in bytes. |
|
| 467 | + * |
|
| 468 | + * If these last two properties are provided, less time will be spent |
|
| 469 | + * calculating them. If they are specified, you can also omit carddata. |
|
| 470 | + * This may speed up certain requests, especially with large cards. |
|
| 471 | + * |
|
| 472 | + * @param mixed $addressbookId |
|
| 473 | + * @return array |
|
| 474 | + */ |
|
| 475 | + public function getCards($addressbookId) { |
|
| 476 | + $query = $this->db->getQueryBuilder(); |
|
| 477 | + $query->select(['id', 'uri', 'lastmodified', 'etag', 'size', 'carddata', 'uid']) |
|
| 478 | + ->from($this->dbCardsTable) |
|
| 479 | + ->where($query->expr()->eq('addressbookid', $query->createNamedParameter($addressbookId))); |
|
| 480 | + |
|
| 481 | + $cards = []; |
|
| 482 | + |
|
| 483 | + $result = $query->executeQuery(); |
|
| 484 | + while ($row = $result->fetch()) { |
|
| 485 | + $row['etag'] = '"' . $row['etag'] . '"'; |
|
| 486 | + |
|
| 487 | + $modified = false; |
|
| 488 | + $row['carddata'] = $this->readBlob($row['carddata'], $modified); |
|
| 489 | + if ($modified) { |
|
| 490 | + $row['size'] = strlen($row['carddata']); |
|
| 491 | + } |
|
| 492 | + |
|
| 493 | + $cards[] = $row; |
|
| 494 | + } |
|
| 495 | + $result->closeCursor(); |
|
| 496 | + |
|
| 497 | + return $cards; |
|
| 498 | + } |
|
| 499 | + |
|
| 500 | + /** |
|
| 501 | + * Returns a specific card. |
|
| 502 | + * |
|
| 503 | + * The same set of properties must be returned as with getCards. The only |
|
| 504 | + * exception is that 'carddata' is absolutely required. |
|
| 505 | + * |
|
| 506 | + * If the card does not exist, you must return false. |
|
| 507 | + * |
|
| 508 | + * @param mixed $addressBookId |
|
| 509 | + * @param string $cardUri |
|
| 510 | + * @return array |
|
| 511 | + */ |
|
| 512 | + public function getCard($addressBookId, $cardUri) { |
|
| 513 | + $query = $this->db->getQueryBuilder(); |
|
| 514 | + $query->select(['id', 'uri', 'lastmodified', 'etag', 'size', 'carddata', 'uid']) |
|
| 515 | + ->from($this->dbCardsTable) |
|
| 516 | + ->where($query->expr()->eq('addressbookid', $query->createNamedParameter($addressBookId))) |
|
| 517 | + ->andWhere($query->expr()->eq('uri', $query->createNamedParameter($cardUri))) |
|
| 518 | + ->setMaxResults(1); |
|
| 519 | + |
|
| 520 | + $result = $query->executeQuery(); |
|
| 521 | + $row = $result->fetch(); |
|
| 522 | + if (!$row) { |
|
| 523 | + return false; |
|
| 524 | + } |
|
| 525 | + $row['etag'] = '"' . $row['etag'] . '"'; |
|
| 526 | + |
|
| 527 | + $modified = false; |
|
| 528 | + $row['carddata'] = $this->readBlob($row['carddata'], $modified); |
|
| 529 | + if ($modified) { |
|
| 530 | + $row['size'] = strlen($row['carddata']); |
|
| 531 | + } |
|
| 532 | + |
|
| 533 | + return $row; |
|
| 534 | + } |
|
| 535 | + |
|
| 536 | + /** |
|
| 537 | + * Returns a list of cards. |
|
| 538 | + * |
|
| 539 | + * This method should work identical to getCard, but instead return all the |
|
| 540 | + * cards in the list as an array. |
|
| 541 | + * |
|
| 542 | + * If the backend supports this, it may allow for some speed-ups. |
|
| 543 | + * |
|
| 544 | + * @param mixed $addressBookId |
|
| 545 | + * @param array $uris |
|
| 546 | + * @return array |
|
| 547 | + */ |
|
| 548 | + public function getMultipleCards($addressBookId, array $uris) { |
|
| 549 | + if (empty($uris)) { |
|
| 550 | + return []; |
|
| 551 | + } |
|
| 552 | + |
|
| 553 | + $chunks = array_chunk($uris, 100); |
|
| 554 | + $cards = []; |
|
| 555 | + |
|
| 556 | + $query = $this->db->getQueryBuilder(); |
|
| 557 | + $query->select(['id', 'uri', 'lastmodified', 'etag', 'size', 'carddata', 'uid']) |
|
| 558 | + ->from($this->dbCardsTable) |
|
| 559 | + ->where($query->expr()->eq('addressbookid', $query->createNamedParameter($addressBookId))) |
|
| 560 | + ->andWhere($query->expr()->in('uri', $query->createParameter('uri'))); |
|
| 561 | + |
|
| 562 | + foreach ($chunks as $uris) { |
|
| 563 | + $query->setParameter('uri', $uris, IQueryBuilder::PARAM_STR_ARRAY); |
|
| 564 | + $result = $query->executeQuery(); |
|
| 565 | + |
|
| 566 | + while ($row = $result->fetch()) { |
|
| 567 | + $row['etag'] = '"' . $row['etag'] . '"'; |
|
| 568 | + |
|
| 569 | + $modified = false; |
|
| 570 | + $row['carddata'] = $this->readBlob($row['carddata'], $modified); |
|
| 571 | + if ($modified) { |
|
| 572 | + $row['size'] = strlen($row['carddata']); |
|
| 573 | + } |
|
| 574 | + |
|
| 575 | + $cards[] = $row; |
|
| 576 | + } |
|
| 577 | + $result->closeCursor(); |
|
| 578 | + } |
|
| 579 | + return $cards; |
|
| 580 | + } |
|
| 581 | + |
|
| 582 | + /** |
|
| 583 | + * Creates a new card. |
|
| 584 | + * |
|
| 585 | + * The addressbook id will be passed as the first argument. This is the |
|
| 586 | + * same id as it is returned from the getAddressBooksForUser method. |
|
| 587 | + * |
|
| 588 | + * The cardUri is a base uri, and doesn't include the full path. The |
|
| 589 | + * cardData argument is the vcard body, and is passed as a string. |
|
| 590 | + * |
|
| 591 | + * It is possible to return an ETag from this method. This ETag is for the |
|
| 592 | + * newly created resource, and must be enclosed with double quotes (that |
|
| 593 | + * is, the string itself must contain the double quotes). |
|
| 594 | + * |
|
| 595 | + * You should only return the ETag if you store the carddata as-is. If a |
|
| 596 | + * subsequent GET request on the same card does not have the same body, |
|
| 597 | + * byte-by-byte and you did return an ETag here, clients tend to get |
|
| 598 | + * confused. |
|
| 599 | + * |
|
| 600 | + * If you don't return an ETag, you can just return null. |
|
| 601 | + * |
|
| 602 | + * @param mixed $addressBookId |
|
| 603 | + * @param string $cardUri |
|
| 604 | + * @param string $cardData |
|
| 605 | + * @param bool $checkAlreadyExists |
|
| 606 | + * @return string |
|
| 607 | + */ |
|
| 608 | + public function createCard($addressBookId, $cardUri, $cardData, bool $checkAlreadyExists = true) { |
|
| 609 | + $etag = md5($cardData); |
|
| 610 | + $uid = $this->getUID($cardData); |
|
| 611 | + return $this->atomic(function () use ($addressBookId, $cardUri, $cardData, $checkAlreadyExists, $etag, $uid) { |
|
| 612 | + if ($checkAlreadyExists) { |
|
| 613 | + $q = $this->db->getQueryBuilder(); |
|
| 614 | + $q->select('uid') |
|
| 615 | + ->from($this->dbCardsTable) |
|
| 616 | + ->where($q->expr()->eq('addressbookid', $q->createNamedParameter($addressBookId))) |
|
| 617 | + ->andWhere($q->expr()->eq('uid', $q->createNamedParameter($uid))) |
|
| 618 | + ->setMaxResults(1); |
|
| 619 | + $result = $q->executeQuery(); |
|
| 620 | + $count = (bool)$result->fetchOne(); |
|
| 621 | + $result->closeCursor(); |
|
| 622 | + if ($count) { |
|
| 623 | + throw new \Sabre\DAV\Exception\BadRequest('VCard object with uid already exists in this addressbook collection.'); |
|
| 624 | + } |
|
| 625 | + } |
|
| 626 | + |
|
| 627 | + $query = $this->db->getQueryBuilder(); |
|
| 628 | + $query->insert('cards') |
|
| 629 | + ->values([ |
|
| 630 | + 'carddata' => $query->createNamedParameter($cardData, IQueryBuilder::PARAM_LOB), |
|
| 631 | + 'uri' => $query->createNamedParameter($cardUri), |
|
| 632 | + 'lastmodified' => $query->createNamedParameter(time()), |
|
| 633 | + 'addressbookid' => $query->createNamedParameter($addressBookId), |
|
| 634 | + 'size' => $query->createNamedParameter(strlen($cardData)), |
|
| 635 | + 'etag' => $query->createNamedParameter($etag), |
|
| 636 | + 'uid' => $query->createNamedParameter($uid), |
|
| 637 | + ]) |
|
| 638 | + ->executeStatement(); |
|
| 639 | + |
|
| 640 | + $etagCacheKey = "$addressBookId#$cardUri"; |
|
| 641 | + $this->etagCache[$etagCacheKey] = $etag; |
|
| 642 | + |
|
| 643 | + $this->addChange($addressBookId, $cardUri, 1); |
|
| 644 | + $this->updateProperties($addressBookId, $cardUri, $cardData); |
|
| 645 | + |
|
| 646 | + $addressBookData = $this->getAddressBookById($addressBookId); |
|
| 647 | + $shares = $this->getShares($addressBookId); |
|
| 648 | + $objectRow = $this->getCard($addressBookId, $cardUri); |
|
| 649 | + $this->dispatcher->dispatchTyped(new CardCreatedEvent($addressBookId, $addressBookData, $shares, $objectRow)); |
|
| 650 | + |
|
| 651 | + return '"' . $etag . '"'; |
|
| 652 | + }, $this->db); |
|
| 653 | + } |
|
| 654 | + |
|
| 655 | + /** |
|
| 656 | + * Updates a card. |
|
| 657 | + * |
|
| 658 | + * The addressbook id will be passed as the first argument. This is the |
|
| 659 | + * same id as it is returned from the getAddressBooksForUser method. |
|
| 660 | + * |
|
| 661 | + * The cardUri is a base uri, and doesn't include the full path. The |
|
| 662 | + * cardData argument is the vcard body, and is passed as a string. |
|
| 663 | + * |
|
| 664 | + * It is possible to return an ETag from this method. This ETag should |
|
| 665 | + * match that of the updated resource, and must be enclosed with double |
|
| 666 | + * quotes (that is: the string itself must contain the actual quotes). |
|
| 667 | + * |
|
| 668 | + * You should only return the ETag if you store the carddata as-is. If a |
|
| 669 | + * subsequent GET request on the same card does not have the same body, |
|
| 670 | + * byte-by-byte and you did return an ETag here, clients tend to get |
|
| 671 | + * confused. |
|
| 672 | + * |
|
| 673 | + * If you don't return an ETag, you can just return null. |
|
| 674 | + * |
|
| 675 | + * @param mixed $addressBookId |
|
| 676 | + * @param string $cardUri |
|
| 677 | + * @param string $cardData |
|
| 678 | + * @return string |
|
| 679 | + */ |
|
| 680 | + public function updateCard($addressBookId, $cardUri, $cardData) { |
|
| 681 | + $uid = $this->getUID($cardData); |
|
| 682 | + $etag = md5($cardData); |
|
| 683 | + |
|
| 684 | + return $this->atomic(function () use ($addressBookId, $cardUri, $cardData, $uid, $etag) { |
|
| 685 | + $query = $this->db->getQueryBuilder(); |
|
| 686 | + |
|
| 687 | + // check for recently stored etag and stop if it is the same |
|
| 688 | + $etagCacheKey = "$addressBookId#$cardUri"; |
|
| 689 | + if (isset($this->etagCache[$etagCacheKey]) && $this->etagCache[$etagCacheKey] === $etag) { |
|
| 690 | + return '"' . $etag . '"'; |
|
| 691 | + } |
|
| 692 | + |
|
| 693 | + $query->update($this->dbCardsTable) |
|
| 694 | + ->set('carddata', $query->createNamedParameter($cardData, IQueryBuilder::PARAM_LOB)) |
|
| 695 | + ->set('lastmodified', $query->createNamedParameter(time())) |
|
| 696 | + ->set('size', $query->createNamedParameter(strlen($cardData))) |
|
| 697 | + ->set('etag', $query->createNamedParameter($etag)) |
|
| 698 | + ->set('uid', $query->createNamedParameter($uid)) |
|
| 699 | + ->where($query->expr()->eq('uri', $query->createNamedParameter($cardUri))) |
|
| 700 | + ->andWhere($query->expr()->eq('addressbookid', $query->createNamedParameter($addressBookId))) |
|
| 701 | + ->executeStatement(); |
|
| 702 | + |
|
| 703 | + $this->etagCache[$etagCacheKey] = $etag; |
|
| 704 | + |
|
| 705 | + $this->addChange($addressBookId, $cardUri, 2); |
|
| 706 | + $this->updateProperties($addressBookId, $cardUri, $cardData); |
|
| 707 | + |
|
| 708 | + $addressBookData = $this->getAddressBookById($addressBookId); |
|
| 709 | + $shares = $this->getShares($addressBookId); |
|
| 710 | + $objectRow = $this->getCard($addressBookId, $cardUri); |
|
| 711 | + $this->dispatcher->dispatchTyped(new CardUpdatedEvent($addressBookId, $addressBookData, $shares, $objectRow)); |
|
| 712 | + return '"' . $etag . '"'; |
|
| 713 | + }, $this->db); |
|
| 714 | + } |
|
| 715 | + |
|
| 716 | + /** |
|
| 717 | + * @throws Exception |
|
| 718 | + */ |
|
| 719 | + public function moveCard(int $sourceAddressBookId, int $targetAddressBookId, string $cardUri, string $oldPrincipalUri): bool { |
|
| 720 | + return $this->atomic(function () use ($sourceAddressBookId, $targetAddressBookId, $cardUri, $oldPrincipalUri) { |
|
| 721 | + $card = $this->getCard($sourceAddressBookId, $cardUri); |
|
| 722 | + if (empty($card)) { |
|
| 723 | + return false; |
|
| 724 | + } |
|
| 725 | + |
|
| 726 | + $query = $this->db->getQueryBuilder(); |
|
| 727 | + $query->update('cards') |
|
| 728 | + ->set('addressbookid', $query->createNamedParameter($targetAddressBookId, IQueryBuilder::PARAM_INT)) |
|
| 729 | + ->where($query->expr()->eq('uri', $query->createNamedParameter($cardUri, IQueryBuilder::PARAM_STR), IQueryBuilder::PARAM_STR)) |
|
| 730 | + ->andWhere($query->expr()->eq('addressbookid', $query->createNamedParameter($sourceAddressBookId, IQueryBuilder::PARAM_INT), IQueryBuilder::PARAM_INT)) |
|
| 731 | + ->executeStatement(); |
|
| 732 | + |
|
| 733 | + $this->purgeProperties($sourceAddressBookId, (int)$card['id']); |
|
| 734 | + $this->updateProperties($sourceAddressBookId, $card['uri'], $card['carddata']); |
|
| 735 | + |
|
| 736 | + $this->addChange($sourceAddressBookId, $card['uri'], 3); |
|
| 737 | + $this->addChange($targetAddressBookId, $card['uri'], 1); |
|
| 738 | + |
|
| 739 | + $card = $this->getCard($targetAddressBookId, $cardUri); |
|
| 740 | + // Card wasn't found - possibly because it was deleted in the meantime by a different client |
|
| 741 | + if (empty($card)) { |
|
| 742 | + return false; |
|
| 743 | + } |
|
| 744 | + |
|
| 745 | + $targetAddressBookRow = $this->getAddressBookById($targetAddressBookId); |
|
| 746 | + // the address book this card is being moved to does not exist any longer |
|
| 747 | + if (empty($targetAddressBookRow)) { |
|
| 748 | + return false; |
|
| 749 | + } |
|
| 750 | + |
|
| 751 | + $sourceShares = $this->getShares($sourceAddressBookId); |
|
| 752 | + $targetShares = $this->getShares($targetAddressBookId); |
|
| 753 | + $sourceAddressBookRow = $this->getAddressBookById($sourceAddressBookId); |
|
| 754 | + $this->dispatcher->dispatchTyped(new CardMovedEvent($sourceAddressBookId, $sourceAddressBookRow, $targetAddressBookId, $targetAddressBookRow, $sourceShares, $targetShares, $card)); |
|
| 755 | + return true; |
|
| 756 | + }, $this->db); |
|
| 757 | + } |
|
| 758 | + |
|
| 759 | + /** |
|
| 760 | + * Deletes a card |
|
| 761 | + * |
|
| 762 | + * @param mixed $addressBookId |
|
| 763 | + * @param string $cardUri |
|
| 764 | + * @return bool |
|
| 765 | + */ |
|
| 766 | + public function deleteCard($addressBookId, $cardUri) { |
|
| 767 | + return $this->atomic(function () use ($addressBookId, $cardUri) { |
|
| 768 | + $addressBookData = $this->getAddressBookById($addressBookId); |
|
| 769 | + $shares = $this->getShares($addressBookId); |
|
| 770 | + $objectRow = $this->getCard($addressBookId, $cardUri); |
|
| 771 | + |
|
| 772 | + try { |
|
| 773 | + $cardId = $this->getCardId($addressBookId, $cardUri); |
|
| 774 | + } catch (\InvalidArgumentException $e) { |
|
| 775 | + $cardId = null; |
|
| 776 | + } |
|
| 777 | + $query = $this->db->getQueryBuilder(); |
|
| 778 | + $ret = $query->delete($this->dbCardsTable) |
|
| 779 | + ->where($query->expr()->eq('addressbookid', $query->createNamedParameter($addressBookId))) |
|
| 780 | + ->andWhere($query->expr()->eq('uri', $query->createNamedParameter($cardUri))) |
|
| 781 | + ->executeStatement(); |
|
| 782 | + |
|
| 783 | + $this->addChange($addressBookId, $cardUri, 3); |
|
| 784 | + |
|
| 785 | + if ($ret === 1) { |
|
| 786 | + if ($cardId !== null) { |
|
| 787 | + $this->dispatcher->dispatchTyped(new CardDeletedEvent($addressBookId, $addressBookData, $shares, $objectRow)); |
|
| 788 | + $this->purgeProperties($addressBookId, $cardId); |
|
| 789 | + } |
|
| 790 | + return true; |
|
| 791 | + } |
|
| 792 | + |
|
| 793 | + return false; |
|
| 794 | + }, $this->db); |
|
| 795 | + } |
|
| 796 | + |
|
| 797 | + /** |
|
| 798 | + * The getChanges method returns all the changes that have happened, since |
|
| 799 | + * the specified syncToken in the specified address book. |
|
| 800 | + * |
|
| 801 | + * This function should return an array, such as the following: |
|
| 802 | + * |
|
| 803 | + * [ |
|
| 804 | + * 'syncToken' => 'The current synctoken', |
|
| 805 | + * 'added' => [ |
|
| 806 | + * 'new.txt', |
|
| 807 | + * ], |
|
| 808 | + * 'modified' => [ |
|
| 809 | + * 'modified.txt', |
|
| 810 | + * ], |
|
| 811 | + * 'deleted' => [ |
|
| 812 | + * 'foo.php.bak', |
|
| 813 | + * 'old.txt' |
|
| 814 | + * ] |
|
| 815 | + * ]; |
|
| 816 | + * |
|
| 817 | + * The returned syncToken property should reflect the *current* syncToken |
|
| 818 | + * of the calendar, as reported in the {http://sabredav.org/ns}sync-token |
|
| 819 | + * property. This is needed here too, to ensure the operation is atomic. |
|
| 820 | + * |
|
| 821 | + * If the $syncToken argument is specified as null, this is an initial |
|
| 822 | + * sync, and all members should be reported. |
|
| 823 | + * |
|
| 824 | + * The modified property is an array of nodenames that have changed since |
|
| 825 | + * the last token. |
|
| 826 | + * |
|
| 827 | + * The deleted property is an array with nodenames, that have been deleted |
|
| 828 | + * from collection. |
|
| 829 | + * |
|
| 830 | + * The $syncLevel argument is basically the 'depth' of the report. If it's |
|
| 831 | + * 1, you only have to report changes that happened only directly in |
|
| 832 | + * immediate descendants. If it's 2, it should also include changes from |
|
| 833 | + * the nodes below the child collections. (grandchildren) |
|
| 834 | + * |
|
| 835 | + * The $limit argument allows a client to specify how many results should |
|
| 836 | + * be returned at most. If the limit is not specified, it should be treated |
|
| 837 | + * as infinite. |
|
| 838 | + * |
|
| 839 | + * If the limit (infinite or not) is higher than you're willing to return, |
|
| 840 | + * you should throw a Sabre\DAV\Exception\TooMuchMatches() exception. |
|
| 841 | + * |
|
| 842 | + * If the syncToken is expired (due to data cleanup) or unknown, you must |
|
| 843 | + * return null. |
|
| 844 | + * |
|
| 845 | + * The limit is 'suggestive'. You are free to ignore it. |
|
| 846 | + * |
|
| 847 | + * @param string $addressBookId |
|
| 848 | + * @param string $syncToken |
|
| 849 | + * @param int $syncLevel |
|
| 850 | + * @param int|null $limit |
|
| 851 | + * @return array |
|
| 852 | + */ |
|
| 853 | + public function getChangesForAddressBook($addressBookId, $syncToken, $syncLevel, $limit = null) { |
|
| 854 | + // Current synctoken |
|
| 855 | + return $this->atomic(function () use ($addressBookId, $syncToken, $syncLevel, $limit) { |
|
| 856 | + $qb = $this->db->getQueryBuilder(); |
|
| 857 | + $qb->select('synctoken') |
|
| 858 | + ->from('addressbooks') |
|
| 859 | + ->where( |
|
| 860 | + $qb->expr()->eq('id', $qb->createNamedParameter($addressBookId)) |
|
| 861 | + ); |
|
| 862 | + $stmt = $qb->executeQuery(); |
|
| 863 | + $currentToken = $stmt->fetchOne(); |
|
| 864 | + $stmt->closeCursor(); |
|
| 865 | + |
|
| 866 | + if (is_null($currentToken)) { |
|
| 867 | + return []; |
|
| 868 | + } |
|
| 869 | + |
|
| 870 | + $result = [ |
|
| 871 | + 'syncToken' => $currentToken, |
|
| 872 | + 'added' => [], |
|
| 873 | + 'modified' => [], |
|
| 874 | + 'deleted' => [], |
|
| 875 | + ]; |
|
| 876 | + |
|
| 877 | + if ($syncToken) { |
|
| 878 | + $qb = $this->db->getQueryBuilder(); |
|
| 879 | + $qb->select('uri', 'operation') |
|
| 880 | + ->from('addressbookchanges') |
|
| 881 | + ->where( |
|
| 882 | + $qb->expr()->andX( |
|
| 883 | + $qb->expr()->gte('synctoken', $qb->createNamedParameter($syncToken)), |
|
| 884 | + $qb->expr()->lt('synctoken', $qb->createNamedParameter($currentToken)), |
|
| 885 | + $qb->expr()->eq('addressbookid', $qb->createNamedParameter($addressBookId)) |
|
| 886 | + ) |
|
| 887 | + )->orderBy('synctoken'); |
|
| 888 | + |
|
| 889 | + if (is_int($limit) && $limit > 0) { |
|
| 890 | + $qb->setMaxResults($limit); |
|
| 891 | + } |
|
| 892 | + |
|
| 893 | + // Fetching all changes |
|
| 894 | + $stmt = $qb->executeQuery(); |
|
| 895 | + |
|
| 896 | + $changes = []; |
|
| 897 | + |
|
| 898 | + // This loop ensures that any duplicates are overwritten, only the |
|
| 899 | + // last change on a node is relevant. |
|
| 900 | + while ($row = $stmt->fetch(\PDO::FETCH_ASSOC)) { |
|
| 901 | + $changes[$row['uri']] = $row['operation']; |
|
| 902 | + } |
|
| 903 | + $stmt->closeCursor(); |
|
| 904 | + |
|
| 905 | + foreach ($changes as $uri => $operation) { |
|
| 906 | + switch ($operation) { |
|
| 907 | + case 1: |
|
| 908 | + $result['added'][] = $uri; |
|
| 909 | + break; |
|
| 910 | + case 2: |
|
| 911 | + $result['modified'][] = $uri; |
|
| 912 | + break; |
|
| 913 | + case 3: |
|
| 914 | + $result['deleted'][] = $uri; |
|
| 915 | + break; |
|
| 916 | + } |
|
| 917 | + } |
|
| 918 | + } else { |
|
| 919 | + $qb = $this->db->getQueryBuilder(); |
|
| 920 | + $qb->select('uri') |
|
| 921 | + ->from('cards') |
|
| 922 | + ->where( |
|
| 923 | + $qb->expr()->eq('addressbookid', $qb->createNamedParameter($addressBookId)) |
|
| 924 | + ); |
|
| 925 | + // No synctoken supplied, this is the initial sync. |
|
| 926 | + $stmt = $qb->executeQuery(); |
|
| 927 | + $result['added'] = $stmt->fetchAll(\PDO::FETCH_COLUMN); |
|
| 928 | + $stmt->closeCursor(); |
|
| 929 | + } |
|
| 930 | + return $result; |
|
| 931 | + }, $this->db); |
|
| 932 | + } |
|
| 933 | + |
|
| 934 | + /** |
|
| 935 | + * Adds a change record to the addressbookchanges table. |
|
| 936 | + * |
|
| 937 | + * @param mixed $addressBookId |
|
| 938 | + * @param string $objectUri |
|
| 939 | + * @param int $operation 1 = add, 2 = modify, 3 = delete |
|
| 940 | + * @return void |
|
| 941 | + */ |
|
| 942 | + protected function addChange(int $addressBookId, string $objectUri, int $operation): void { |
|
| 943 | + $this->atomic(function () use ($addressBookId, $objectUri, $operation): void { |
|
| 944 | + $query = $this->db->getQueryBuilder(); |
|
| 945 | + $query->select('synctoken') |
|
| 946 | + ->from('addressbooks') |
|
| 947 | + ->where($query->expr()->eq('id', $query->createNamedParameter($addressBookId))); |
|
| 948 | + $result = $query->executeQuery(); |
|
| 949 | + $syncToken = (int)$result->fetchOne(); |
|
| 950 | + $result->closeCursor(); |
|
| 951 | + |
|
| 952 | + $query = $this->db->getQueryBuilder(); |
|
| 953 | + $query->insert('addressbookchanges') |
|
| 954 | + ->values([ |
|
| 955 | + 'uri' => $query->createNamedParameter($objectUri), |
|
| 956 | + 'synctoken' => $query->createNamedParameter($syncToken), |
|
| 957 | + 'addressbookid' => $query->createNamedParameter($addressBookId), |
|
| 958 | + 'operation' => $query->createNamedParameter($operation), |
|
| 959 | + 'created_at' => time(), |
|
| 960 | + ]) |
|
| 961 | + ->executeStatement(); |
|
| 962 | + |
|
| 963 | + $query = $this->db->getQueryBuilder(); |
|
| 964 | + $query->update('addressbooks') |
|
| 965 | + ->set('synctoken', $query->createNamedParameter($syncToken + 1, IQueryBuilder::PARAM_INT)) |
|
| 966 | + ->where($query->expr()->eq('id', $query->createNamedParameter($addressBookId))) |
|
| 967 | + ->executeStatement(); |
|
| 968 | + }, $this->db); |
|
| 969 | + } |
|
| 970 | + |
|
| 971 | + /** |
|
| 972 | + * @param resource|string $cardData |
|
| 973 | + * @param bool $modified |
|
| 974 | + * @return string |
|
| 975 | + */ |
|
| 976 | + private function readBlob($cardData, &$modified = false) { |
|
| 977 | + if (is_resource($cardData)) { |
|
| 978 | + $cardData = stream_get_contents($cardData); |
|
| 979 | + } |
|
| 980 | + |
|
| 981 | + // Micro optimisation |
|
| 982 | + // don't loop through |
|
| 983 | + if (str_starts_with($cardData, 'PHOTO:data:')) { |
|
| 984 | + return $cardData; |
|
| 985 | + } |
|
| 986 | + |
|
| 987 | + $cardDataArray = explode("\r\n", $cardData); |
|
| 988 | + |
|
| 989 | + $cardDataFiltered = []; |
|
| 990 | + $removingPhoto = false; |
|
| 991 | + foreach ($cardDataArray as $line) { |
|
| 992 | + if (str_starts_with($line, 'PHOTO:data:') |
|
| 993 | + && !str_starts_with($line, 'PHOTO:data:image/')) { |
|
| 994 | + // Filter out PHOTO data of non-images |
|
| 995 | + $removingPhoto = true; |
|
| 996 | + $modified = true; |
|
| 997 | + continue; |
|
| 998 | + } |
|
| 999 | + |
|
| 1000 | + if ($removingPhoto) { |
|
| 1001 | + if (str_starts_with($line, ' ')) { |
|
| 1002 | + continue; |
|
| 1003 | + } |
|
| 1004 | + // No leading space means this is a new property |
|
| 1005 | + $removingPhoto = false; |
|
| 1006 | + } |
|
| 1007 | + |
|
| 1008 | + $cardDataFiltered[] = $line; |
|
| 1009 | + } |
|
| 1010 | + return implode("\r\n", $cardDataFiltered); |
|
| 1011 | + } |
|
| 1012 | + |
|
| 1013 | + /** |
|
| 1014 | + * @param list<array{href: string, commonName: string, readOnly: bool}> $add |
|
| 1015 | + * @param list<string> $remove |
|
| 1016 | + */ |
|
| 1017 | + public function updateShares(IShareable $shareable, array $add, array $remove): void { |
|
| 1018 | + $this->atomic(function () use ($shareable, $add, $remove): void { |
|
| 1019 | + $addressBookId = $shareable->getResourceId(); |
|
| 1020 | + $addressBookData = $this->getAddressBookById($addressBookId); |
|
| 1021 | + $oldShares = $this->getShares($addressBookId); |
|
| 1022 | + |
|
| 1023 | + $this->sharingBackend->updateShares($shareable, $add, $remove, $oldShares); |
|
| 1024 | + |
|
| 1025 | + $this->dispatcher->dispatchTyped(new AddressBookShareUpdatedEvent($addressBookId, $addressBookData, $oldShares, $add, $remove)); |
|
| 1026 | + }, $this->db); |
|
| 1027 | + } |
|
| 1028 | + |
|
| 1029 | + /** |
|
| 1030 | + * Search contacts in a specific address-book |
|
| 1031 | + * |
|
| 1032 | + * @param int $addressBookId |
|
| 1033 | + * @param string $pattern which should match within the $searchProperties |
|
| 1034 | + * @param array $searchProperties defines the properties within the query pattern should match |
|
| 1035 | + * @param array $options = array() to define the search behavior |
|
| 1036 | + * - 'types' boolean (since 15.0.0) If set to true, fields that come with a TYPE property will be an array |
|
| 1037 | + * - 'escape_like_param' - If set to false wildcards _ and % are not escaped, otherwise they are |
|
| 1038 | + * - 'limit' - Set a numeric limit for the search results |
|
| 1039 | + * - 'offset' - Set the offset for the limited search results |
|
| 1040 | + * - 'wildcard' - Whether the search should use wildcards |
|
| 1041 | + * @psalm-param array{types?: bool, escape_like_param?: bool, limit?: int, offset?: int, wildcard?: bool} $options |
|
| 1042 | + * @return array an array of contacts which are arrays of key-value-pairs |
|
| 1043 | + */ |
|
| 1044 | + public function search($addressBookId, $pattern, $searchProperties, $options = []): array { |
|
| 1045 | + return $this->atomic(function () use ($addressBookId, $pattern, $searchProperties, $options) { |
|
| 1046 | + return $this->searchByAddressBookIds([$addressBookId], $pattern, $searchProperties, $options); |
|
| 1047 | + }, $this->db); |
|
| 1048 | + } |
|
| 1049 | + |
|
| 1050 | + /** |
|
| 1051 | + * Search contacts in all address-books accessible by a user |
|
| 1052 | + * |
|
| 1053 | + * @param string $principalUri |
|
| 1054 | + * @param string $pattern |
|
| 1055 | + * @param array $searchProperties |
|
| 1056 | + * @param array $options |
|
| 1057 | + * @return array |
|
| 1058 | + */ |
|
| 1059 | + public function searchPrincipalUri(string $principalUri, |
|
| 1060 | + string $pattern, |
|
| 1061 | + array $searchProperties, |
|
| 1062 | + array $options = []): array { |
|
| 1063 | + return $this->atomic(function () use ($principalUri, $pattern, $searchProperties, $options) { |
|
| 1064 | + $addressBookIds = array_map(static function ($row):int { |
|
| 1065 | + return (int)$row['id']; |
|
| 1066 | + }, $this->getAddressBooksForUser($principalUri)); |
|
| 1067 | + |
|
| 1068 | + return $this->searchByAddressBookIds($addressBookIds, $pattern, $searchProperties, $options); |
|
| 1069 | + }, $this->db); |
|
| 1070 | + } |
|
| 1071 | + |
|
| 1072 | + /** |
|
| 1073 | + * @param int[] $addressBookIds |
|
| 1074 | + * @param string $pattern |
|
| 1075 | + * @param array $searchProperties |
|
| 1076 | + * @param array $options |
|
| 1077 | + * @psalm-param array{ |
|
| 1078 | + * types?: bool, |
|
| 1079 | + * escape_like_param?: bool, |
|
| 1080 | + * limit?: int, |
|
| 1081 | + * offset?: int, |
|
| 1082 | + * wildcard?: bool, |
|
| 1083 | + * since?: DateTimeFilter|null, |
|
| 1084 | + * until?: DateTimeFilter|null, |
|
| 1085 | + * person?: string |
|
| 1086 | + * } $options |
|
| 1087 | + * @return array |
|
| 1088 | + */ |
|
| 1089 | + private function searchByAddressBookIds(array $addressBookIds, |
|
| 1090 | + string $pattern, |
|
| 1091 | + array $searchProperties, |
|
| 1092 | + array $options = []): array { |
|
| 1093 | + if (empty($addressBookIds)) { |
|
| 1094 | + return []; |
|
| 1095 | + } |
|
| 1096 | + $escapePattern = !\array_key_exists('escape_like_param', $options) || $options['escape_like_param'] !== false; |
|
| 1097 | + $useWildcards = !\array_key_exists('wildcard', $options) || $options['wildcard'] !== false; |
|
| 1098 | + |
|
| 1099 | + if ($escapePattern) { |
|
| 1100 | + $searchProperties = array_filter($searchProperties, function ($property) use ($pattern) { |
|
| 1101 | + if ($property === 'EMAIL' && str_contains($pattern, ' ')) { |
|
| 1102 | + // There can be no spaces in emails |
|
| 1103 | + return false; |
|
| 1104 | + } |
|
| 1105 | + |
|
| 1106 | + if ($property === 'CLOUD' && preg_match('/[^a-zA-Z0-9 :_.@\/\-\']/', $pattern) === 1) { |
|
| 1107 | + // There can be no chars in cloud ids which are not valid for user ids plus :/ |
|
| 1108 | + // worst case: CA61590A-BBBC-423E-84AF-E6DF01455A53@https://my.nxt/srv/ |
|
| 1109 | + return false; |
|
| 1110 | + } |
|
| 1111 | + |
|
| 1112 | + return true; |
|
| 1113 | + }); |
|
| 1114 | + } |
|
| 1115 | + |
|
| 1116 | + if (empty($searchProperties)) { |
|
| 1117 | + return []; |
|
| 1118 | + } |
|
| 1119 | + |
|
| 1120 | + $query2 = $this->db->getQueryBuilder(); |
|
| 1121 | + $query2->selectDistinct('cp.cardid') |
|
| 1122 | + ->from($this->dbCardsPropertiesTable, 'cp') |
|
| 1123 | + ->where($query2->expr()->in('cp.addressbookid', $query2->createNamedParameter($addressBookIds, IQueryBuilder::PARAM_INT_ARRAY), IQueryBuilder::PARAM_INT_ARRAY)) |
|
| 1124 | + ->andWhere($query2->expr()->in('cp.name', $query2->createNamedParameter($searchProperties, IQueryBuilder::PARAM_STR_ARRAY))); |
|
| 1125 | + |
|
| 1126 | + // No need for like when the pattern is empty |
|
| 1127 | + if ($pattern !== '') { |
|
| 1128 | + if (!$useWildcards) { |
|
| 1129 | + $query2->andWhere($query2->expr()->eq('cp.value', $query2->createNamedParameter($pattern))); |
|
| 1130 | + } elseif (!$escapePattern) { |
|
| 1131 | + $query2->andWhere($query2->expr()->ilike('cp.value', $query2->createNamedParameter($pattern))); |
|
| 1132 | + } else { |
|
| 1133 | + $query2->andWhere($query2->expr()->ilike('cp.value', $query2->createNamedParameter('%' . $this->db->escapeLikeParameter($pattern) . '%'))); |
|
| 1134 | + } |
|
| 1135 | + } |
|
| 1136 | + if (isset($options['limit'])) { |
|
| 1137 | + $query2->setMaxResults($options['limit']); |
|
| 1138 | + } |
|
| 1139 | + if (isset($options['offset'])) { |
|
| 1140 | + $query2->setFirstResult($options['offset']); |
|
| 1141 | + } |
|
| 1142 | + |
|
| 1143 | + if (isset($options['person'])) { |
|
| 1144 | + $query2->andWhere($query2->expr()->ilike('cp.value', $query2->createNamedParameter('%' . $this->db->escapeLikeParameter($options['person']) . '%'))); |
|
| 1145 | + } |
|
| 1146 | + if (isset($options['since']) || isset($options['until'])) { |
|
| 1147 | + $query2->join('cp', $this->dbCardsPropertiesTable, 'cp_bday', 'cp.cardid = cp_bday.cardid'); |
|
| 1148 | + $query2->andWhere($query2->expr()->eq('cp_bday.name', $query2->createNamedParameter('BDAY'))); |
|
| 1149 | + /** |
|
| 1150 | + * FIXME Find a way to match only 4 last digits |
|
| 1151 | + * BDAY can be --1018 without year or 20001019 with it |
|
| 1152 | + * $bDayOr = []; |
|
| 1153 | + * if ($options['since'] instanceof DateTimeFilter) { |
|
| 1154 | + * $bDayOr[] = |
|
| 1155 | + * $query2->expr()->gte('SUBSTR(cp_bday.value, -4)', |
|
| 1156 | + * $query2->createNamedParameter($options['since']->get()->format('md')) |
|
| 1157 | + * ); |
|
| 1158 | + * } |
|
| 1159 | + * if ($options['until'] instanceof DateTimeFilter) { |
|
| 1160 | + * $bDayOr[] = |
|
| 1161 | + * $query2->expr()->lte('SUBSTR(cp_bday.value, -4)', |
|
| 1162 | + * $query2->createNamedParameter($options['until']->get()->format('md')) |
|
| 1163 | + * ); |
|
| 1164 | + * } |
|
| 1165 | + * $query2->andWhere($query2->expr()->orX(...$bDayOr)); |
|
| 1166 | + */ |
|
| 1167 | + } |
|
| 1168 | + |
|
| 1169 | + $result = $query2->executeQuery(); |
|
| 1170 | + $matches = $result->fetchAll(); |
|
| 1171 | + $result->closeCursor(); |
|
| 1172 | + $matches = array_map(function ($match) { |
|
| 1173 | + return (int)$match['cardid']; |
|
| 1174 | + }, $matches); |
|
| 1175 | + |
|
| 1176 | + $cards = []; |
|
| 1177 | + $query = $this->db->getQueryBuilder(); |
|
| 1178 | + $query->select('c.addressbookid', 'c.carddata', 'c.uri') |
|
| 1179 | + ->from($this->dbCardsTable, 'c') |
|
| 1180 | + ->where($query->expr()->in('c.id', $query->createParameter('matches'))); |
|
| 1181 | + |
|
| 1182 | + foreach (array_chunk($matches, 1000) as $matchesChunk) { |
|
| 1183 | + $query->setParameter('matches', $matchesChunk, IQueryBuilder::PARAM_INT_ARRAY); |
|
| 1184 | + $result = $query->executeQuery(); |
|
| 1185 | + $cards = array_merge($cards, $result->fetchAll()); |
|
| 1186 | + $result->closeCursor(); |
|
| 1187 | + } |
|
| 1188 | + |
|
| 1189 | + return array_map(function ($array) { |
|
| 1190 | + $array['addressbookid'] = (int)$array['addressbookid']; |
|
| 1191 | + $modified = false; |
|
| 1192 | + $array['carddata'] = $this->readBlob($array['carddata'], $modified); |
|
| 1193 | + if ($modified) { |
|
| 1194 | + $array['size'] = strlen($array['carddata']); |
|
| 1195 | + } |
|
| 1196 | + return $array; |
|
| 1197 | + }, $cards); |
|
| 1198 | + } |
|
| 1199 | + |
|
| 1200 | + /** |
|
| 1201 | + * @param int $bookId |
|
| 1202 | + * @param string $name |
|
| 1203 | + * @return array |
|
| 1204 | + */ |
|
| 1205 | + public function collectCardProperties($bookId, $name) { |
|
| 1206 | + $query = $this->db->getQueryBuilder(); |
|
| 1207 | + $result = $query->selectDistinct('value') |
|
| 1208 | + ->from($this->dbCardsPropertiesTable) |
|
| 1209 | + ->where($query->expr()->eq('name', $query->createNamedParameter($name))) |
|
| 1210 | + ->andWhere($query->expr()->eq('addressbookid', $query->createNamedParameter($bookId))) |
|
| 1211 | + ->executeQuery(); |
|
| 1212 | + |
|
| 1213 | + $all = $result->fetchAll(PDO::FETCH_COLUMN); |
|
| 1214 | + $result->closeCursor(); |
|
| 1215 | + |
|
| 1216 | + return $all; |
|
| 1217 | + } |
|
| 1218 | + |
|
| 1219 | + /** |
|
| 1220 | + * get URI from a given contact |
|
| 1221 | + * |
|
| 1222 | + * @param int $id |
|
| 1223 | + * @return string |
|
| 1224 | + */ |
|
| 1225 | + public function getCardUri($id) { |
|
| 1226 | + $query = $this->db->getQueryBuilder(); |
|
| 1227 | + $query->select('uri')->from($this->dbCardsTable) |
|
| 1228 | + ->where($query->expr()->eq('id', $query->createParameter('id'))) |
|
| 1229 | + ->setParameter('id', $id); |
|
| 1230 | + |
|
| 1231 | + $result = $query->executeQuery(); |
|
| 1232 | + $uri = $result->fetch(); |
|
| 1233 | + $result->closeCursor(); |
|
| 1234 | + |
|
| 1235 | + if (!isset($uri['uri'])) { |
|
| 1236 | + throw new \InvalidArgumentException('Card does not exists: ' . $id); |
|
| 1237 | + } |
|
| 1238 | + |
|
| 1239 | + return $uri['uri']; |
|
| 1240 | + } |
|
| 1241 | + |
|
| 1242 | + /** |
|
| 1243 | + * return contact with the given URI |
|
| 1244 | + * |
|
| 1245 | + * @param int $addressBookId |
|
| 1246 | + * @param string $uri |
|
| 1247 | + * @returns array |
|
| 1248 | + */ |
|
| 1249 | + public function getContact($addressBookId, $uri) { |
|
| 1250 | + $result = []; |
|
| 1251 | + $query = $this->db->getQueryBuilder(); |
|
| 1252 | + $query->select('*')->from($this->dbCardsTable) |
|
| 1253 | + ->where($query->expr()->eq('uri', $query->createNamedParameter($uri))) |
|
| 1254 | + ->andWhere($query->expr()->eq('addressbookid', $query->createNamedParameter($addressBookId))); |
|
| 1255 | + $queryResult = $query->executeQuery(); |
|
| 1256 | + $contact = $queryResult->fetch(); |
|
| 1257 | + $queryResult->closeCursor(); |
|
| 1258 | + |
|
| 1259 | + if (is_array($contact)) { |
|
| 1260 | + $modified = false; |
|
| 1261 | + $contact['etag'] = '"' . $contact['etag'] . '"'; |
|
| 1262 | + $contact['carddata'] = $this->readBlob($contact['carddata'], $modified); |
|
| 1263 | + if ($modified) { |
|
| 1264 | + $contact['size'] = strlen($contact['carddata']); |
|
| 1265 | + } |
|
| 1266 | + |
|
| 1267 | + $result = $contact; |
|
| 1268 | + } |
|
| 1269 | + |
|
| 1270 | + return $result; |
|
| 1271 | + } |
|
| 1272 | + |
|
| 1273 | + /** |
|
| 1274 | + * Returns the list of people whom this address book is shared with. |
|
| 1275 | + * |
|
| 1276 | + * Every element in this array should have the following properties: |
|
| 1277 | + * * href - Often a mailto: address |
|
| 1278 | + * * commonName - Optional, for example a first + last name |
|
| 1279 | + * * status - See the Sabre\CalDAV\SharingPlugin::STATUS_ constants. |
|
| 1280 | + * * readOnly - boolean |
|
| 1281 | + * |
|
| 1282 | + * @return list<array{href: string, commonName: string, status: int, readOnly: bool, '{http://owncloud.org/ns}principal': string, '{http://owncloud.org/ns}group-share': bool}> |
|
| 1283 | + */ |
|
| 1284 | + public function getShares(int $addressBookId): array { |
|
| 1285 | + return $this->sharingBackend->getShares($addressBookId); |
|
| 1286 | + } |
|
| 1287 | + |
|
| 1288 | + /** |
|
| 1289 | + * update properties table |
|
| 1290 | + * |
|
| 1291 | + * @param int $addressBookId |
|
| 1292 | + * @param string $cardUri |
|
| 1293 | + * @param string $vCardSerialized |
|
| 1294 | + */ |
|
| 1295 | + protected function updateProperties($addressBookId, $cardUri, $vCardSerialized) { |
|
| 1296 | + $this->atomic(function () use ($addressBookId, $cardUri, $vCardSerialized): void { |
|
| 1297 | + $cardId = $this->getCardId($addressBookId, $cardUri); |
|
| 1298 | + $vCard = $this->readCard($vCardSerialized); |
|
| 1299 | + |
|
| 1300 | + $this->purgeProperties($addressBookId, $cardId); |
|
| 1301 | + |
|
| 1302 | + $query = $this->db->getQueryBuilder(); |
|
| 1303 | + $query->insert($this->dbCardsPropertiesTable) |
|
| 1304 | + ->values( |
|
| 1305 | + [ |
|
| 1306 | + 'addressbookid' => $query->createNamedParameter($addressBookId), |
|
| 1307 | + 'cardid' => $query->createNamedParameter($cardId), |
|
| 1308 | + 'name' => $query->createParameter('name'), |
|
| 1309 | + 'value' => $query->createParameter('value'), |
|
| 1310 | + 'preferred' => $query->createParameter('preferred') |
|
| 1311 | + ] |
|
| 1312 | + ); |
|
| 1313 | + |
|
| 1314 | + foreach ($vCard->children() as $property) { |
|
| 1315 | + if (!in_array($property->name, self::$indexProperties)) { |
|
| 1316 | + continue; |
|
| 1317 | + } |
|
| 1318 | + $preferred = 0; |
|
| 1319 | + foreach ($property->parameters as $parameter) { |
|
| 1320 | + if ($parameter->name === 'TYPE' && strtoupper($parameter->getValue()) === 'PREF') { |
|
| 1321 | + $preferred = 1; |
|
| 1322 | + break; |
|
| 1323 | + } |
|
| 1324 | + } |
|
| 1325 | + $query->setParameter('name', $property->name); |
|
| 1326 | + $query->setParameter('value', mb_strcut($property->getValue(), 0, 254)); |
|
| 1327 | + $query->setParameter('preferred', $preferred); |
|
| 1328 | + $query->executeStatement(); |
|
| 1329 | + } |
|
| 1330 | + }, $this->db); |
|
| 1331 | + } |
|
| 1332 | + |
|
| 1333 | + /** |
|
| 1334 | + * read vCard data into a vCard object |
|
| 1335 | + * |
|
| 1336 | + * @param string $cardData |
|
| 1337 | + * @return VCard |
|
| 1338 | + */ |
|
| 1339 | + protected function readCard($cardData) { |
|
| 1340 | + return Reader::read($cardData); |
|
| 1341 | + } |
|
| 1342 | + |
|
| 1343 | + /** |
|
| 1344 | + * delete all properties from a given card |
|
| 1345 | + * |
|
| 1346 | + * @param int $addressBookId |
|
| 1347 | + * @param int $cardId |
|
| 1348 | + */ |
|
| 1349 | + protected function purgeProperties($addressBookId, $cardId) { |
|
| 1350 | + $query = $this->db->getQueryBuilder(); |
|
| 1351 | + $query->delete($this->dbCardsPropertiesTable) |
|
| 1352 | + ->where($query->expr()->eq('cardid', $query->createNamedParameter($cardId))) |
|
| 1353 | + ->andWhere($query->expr()->eq('addressbookid', $query->createNamedParameter($addressBookId))); |
|
| 1354 | + $query->executeStatement(); |
|
| 1355 | + } |
|
| 1356 | + |
|
| 1357 | + /** |
|
| 1358 | + * Get ID from a given contact |
|
| 1359 | + */ |
|
| 1360 | + protected function getCardId(int $addressBookId, string $uri): int { |
|
| 1361 | + $query = $this->db->getQueryBuilder(); |
|
| 1362 | + $query->select('id')->from($this->dbCardsTable) |
|
| 1363 | + ->where($query->expr()->eq('uri', $query->createNamedParameter($uri))) |
|
| 1364 | + ->andWhere($query->expr()->eq('addressbookid', $query->createNamedParameter($addressBookId))); |
|
| 1365 | + |
|
| 1366 | + $result = $query->executeQuery(); |
|
| 1367 | + $cardIds = $result->fetch(); |
|
| 1368 | + $result->closeCursor(); |
|
| 1369 | + |
|
| 1370 | + if (!isset($cardIds['id'])) { |
|
| 1371 | + throw new \InvalidArgumentException('Card does not exists: ' . $uri); |
|
| 1372 | + } |
|
| 1373 | + |
|
| 1374 | + return (int)$cardIds['id']; |
|
| 1375 | + } |
|
| 1376 | + |
|
| 1377 | + /** |
|
| 1378 | + * For shared address books the sharee is set in the ACL of the address book |
|
| 1379 | + * |
|
| 1380 | + * @param int $addressBookId |
|
| 1381 | + * @param list<array{privilege: string, principal: string, protected: bool}> $acl |
|
| 1382 | + * @return list<array{privilege: string, principal: string, protected: bool}> |
|
| 1383 | + */ |
|
| 1384 | + public function applyShareAcl(int $addressBookId, array $acl): array { |
|
| 1385 | + $shares = $this->sharingBackend->getShares($addressBookId); |
|
| 1386 | + return $this->sharingBackend->applyShareAcl($shares, $acl); |
|
| 1387 | + } |
|
| 1388 | + |
|
| 1389 | + /** |
|
| 1390 | + * @throws \InvalidArgumentException |
|
| 1391 | + */ |
|
| 1392 | + public function pruneOutdatedSyncTokens(int $keep, int $retention): int { |
|
| 1393 | + if ($keep < 0) { |
|
| 1394 | + throw new \InvalidArgumentException(); |
|
| 1395 | + } |
|
| 1396 | + |
|
| 1397 | + $query = $this->db->getQueryBuilder(); |
|
| 1398 | + $query->select($query->func()->max('id')) |
|
| 1399 | + ->from('addressbookchanges'); |
|
| 1400 | + |
|
| 1401 | + $result = $query->executeQuery(); |
|
| 1402 | + $maxId = (int)$result->fetchOne(); |
|
| 1403 | + $result->closeCursor(); |
|
| 1404 | + if (!$maxId || $maxId < $keep) { |
|
| 1405 | + return 0; |
|
| 1406 | + } |
|
| 1407 | + |
|
| 1408 | + $query = $this->db->getQueryBuilder(); |
|
| 1409 | + $query->delete('addressbookchanges') |
|
| 1410 | + ->where( |
|
| 1411 | + $query->expr()->lte('id', $query->createNamedParameter($maxId - $keep, IQueryBuilder::PARAM_INT), IQueryBuilder::PARAM_INT), |
|
| 1412 | + $query->expr()->lte('created_at', $query->createNamedParameter($retention)), |
|
| 1413 | + ); |
|
| 1414 | + return $query->executeStatement(); |
|
| 1415 | + } |
|
| 1416 | + |
|
| 1417 | + private function convertPrincipal(string $principalUri, bool $toV2): string { |
|
| 1418 | + if ($this->principalBackend->getPrincipalPrefix() === 'principals') { |
|
| 1419 | + [, $name] = \Sabre\Uri\split($principalUri); |
|
| 1420 | + if ($toV2 === true) { |
|
| 1421 | + return "principals/users/$name"; |
|
| 1422 | + } |
|
| 1423 | + return "principals/$name"; |
|
| 1424 | + } |
|
| 1425 | + return $principalUri; |
|
| 1426 | + } |
|
| 1427 | + |
|
| 1428 | + private function addOwnerPrincipal(array &$addressbookInfo): void { |
|
| 1429 | + $ownerPrincipalKey = '{' . \OCA\DAV\DAV\Sharing\Plugin::NS_OWNCLOUD . '}owner-principal'; |
|
| 1430 | + $displaynameKey = '{' . \OCA\DAV\DAV\Sharing\Plugin::NS_NEXTCLOUD . '}owner-displayname'; |
|
| 1431 | + if (isset($addressbookInfo[$ownerPrincipalKey])) { |
|
| 1432 | + $uri = $addressbookInfo[$ownerPrincipalKey]; |
|
| 1433 | + } else { |
|
| 1434 | + $uri = $addressbookInfo['principaluri']; |
|
| 1435 | + } |
|
| 1436 | + |
|
| 1437 | + $principalInformation = $this->principalBackend->getPrincipalByPath($uri); |
|
| 1438 | + if (isset($principalInformation['{DAV:}displayname'])) { |
|
| 1439 | + $addressbookInfo[$displaynameKey] = $principalInformation['{DAV:}displayname']; |
|
| 1440 | + } |
|
| 1441 | + } |
|
| 1442 | + |
|
| 1443 | + /** |
|
| 1444 | + * Extract UID from vcard |
|
| 1445 | + * |
|
| 1446 | + * @param string $cardData the vcard raw data |
|
| 1447 | + * @return string the uid |
|
| 1448 | + * @throws BadRequest if no UID is available or vcard is empty |
|
| 1449 | + */ |
|
| 1450 | + private function getUID(string $cardData): string { |
|
| 1451 | + if ($cardData !== '') { |
|
| 1452 | + $vCard = Reader::read($cardData); |
|
| 1453 | + if ($vCard->UID) { |
|
| 1454 | + $uid = $vCard->UID->getValue(); |
|
| 1455 | + return $uid; |
|
| 1456 | + } |
|
| 1457 | + // should already be handled, but just in case |
|
| 1458 | + throw new BadRequest('vCards on CardDAV servers MUST have a UID property'); |
|
| 1459 | + } |
|
| 1460 | + // should already be handled, but just in case |
|
| 1461 | + throw new BadRequest('vCard can not be empty'); |
|
| 1462 | + } |
|
| 1463 | 1463 | } |
@@ -23,109 +23,109 @@ |
||
| 23 | 23 | use Symfony\Component\Console\Output\OutputInterface; |
| 24 | 24 | |
| 25 | 25 | #[AsCommand( |
| 26 | - name: 'dav:list-calendar-shares', |
|
| 27 | - description: 'List all calendar shares for a user', |
|
| 28 | - hidden: false, |
|
| 26 | + name: 'dav:list-calendar-shares', |
|
| 27 | + description: 'List all calendar shares for a user', |
|
| 28 | + hidden: false, |
|
| 29 | 29 | )] |
| 30 | 30 | class ListCalendarShares extends Command { |
| 31 | - public function __construct( |
|
| 32 | - private IUserManager $userManager, |
|
| 33 | - private Principal $principal, |
|
| 34 | - private CalDavBackend $caldav, |
|
| 35 | - private SharingMapper $mapper, |
|
| 36 | - ) { |
|
| 37 | - parent::__construct(); |
|
| 38 | - } |
|
| 39 | - |
|
| 40 | - protected function configure(): void { |
|
| 41 | - $this->addArgument( |
|
| 42 | - 'uid', |
|
| 43 | - InputArgument::REQUIRED, |
|
| 44 | - 'User whose calendar shares will be listed' |
|
| 45 | - ); |
|
| 46 | - $this->addOption( |
|
| 47 | - 'calendar-id', |
|
| 48 | - '', |
|
| 49 | - InputOption::VALUE_REQUIRED, |
|
| 50 | - 'List only shares for the given calendar id id', |
|
| 51 | - null, |
|
| 52 | - ); |
|
| 53 | - } |
|
| 54 | - |
|
| 55 | - protected function execute(InputInterface $input, OutputInterface $output): int { |
|
| 56 | - $user = (string)$input->getArgument('uid'); |
|
| 57 | - if (!$this->userManager->userExists($user)) { |
|
| 58 | - throw new \InvalidArgumentException("User $user is unknown"); |
|
| 59 | - } |
|
| 60 | - |
|
| 61 | - $principal = $this->principal->getPrincipalByPath('principals/users/' . $user); |
|
| 62 | - if ($principal === null) { |
|
| 63 | - throw new \InvalidArgumentException("Unable to fetch principal for user $user"); |
|
| 64 | - } |
|
| 65 | - |
|
| 66 | - $memberships = array_merge( |
|
| 67 | - [$principal['uri']], |
|
| 68 | - $this->principal->getGroupMembership($principal['uri']), |
|
| 69 | - $this->principal->getCircleMembership($principal['uri']), |
|
| 70 | - ); |
|
| 71 | - |
|
| 72 | - $shares = $this->mapper->getSharesByPrincipals($memberships, 'calendar'); |
|
| 73 | - |
|
| 74 | - $calendarId = $input->getOption('calendar-id'); |
|
| 75 | - if ($calendarId !== null) { |
|
| 76 | - $shares = array_filter($shares, fn ($share) => $share['resourceid'] === (int)$calendarId); |
|
| 77 | - } |
|
| 78 | - |
|
| 79 | - $rows = array_map(fn ($share) => $this->formatCalendarShare($share), $shares); |
|
| 80 | - |
|
| 81 | - if (count($rows) > 0) { |
|
| 82 | - $table = new Table($output); |
|
| 83 | - $table |
|
| 84 | - ->setHeaders(['Share Id', 'Calendar Id', 'Calendar URI', 'Calendar Name', 'Calendar Owner', 'Access By', 'Permissions']) |
|
| 85 | - ->setRows($rows) |
|
| 86 | - ->render(); |
|
| 87 | - } else { |
|
| 88 | - $output->writeln("User $user has no calendar shares"); |
|
| 89 | - } |
|
| 90 | - |
|
| 91 | - return self::SUCCESS; |
|
| 92 | - } |
|
| 93 | - |
|
| 94 | - private function formatCalendarShare(array $share): array { |
|
| 95 | - $calendarInfo = $this->caldav->getCalendarById($share['resourceid']); |
|
| 96 | - |
|
| 97 | - $calendarUri = 'Resource not found'; |
|
| 98 | - $calendarName = ''; |
|
| 99 | - $calendarOwner = ''; |
|
| 100 | - |
|
| 101 | - if ($calendarInfo !== null) { |
|
| 102 | - $calendarUri = $calendarInfo['uri']; |
|
| 103 | - $calendarName = $calendarInfo['{DAV:}displayname']; |
|
| 104 | - $calendarOwner = $calendarInfo['{http://nextcloud.com/ns}owner-displayname'] . ' (' . $calendarInfo['principaluri'] . ')'; |
|
| 105 | - } |
|
| 106 | - |
|
| 107 | - $accessBy = match (true) { |
|
| 108 | - str_starts_with($share['principaluri'], 'principals/users/') => 'Individual', |
|
| 109 | - str_starts_with($share['principaluri'], 'principals/groups/') => 'Group (' . $share['principaluri'] . ')', |
|
| 110 | - str_starts_with($share['principaluri'], 'principals/circles/') => 'Team (' . $share['principaluri'] . ')', |
|
| 111 | - default => $share['principaluri'], |
|
| 112 | - }; |
|
| 113 | - |
|
| 114 | - $permissions = match ($share['access']) { |
|
| 115 | - Backend::ACCESS_READ => 'Read', |
|
| 116 | - Backend::ACCESS_READ_WRITE => 'Read/Write', |
|
| 117 | - Backend::ACCESS_UNSHARED => 'Unshare', |
|
| 118 | - default => $share['access'], |
|
| 119 | - }; |
|
| 120 | - |
|
| 121 | - return [ |
|
| 122 | - $share['id'], |
|
| 123 | - $share['resourceid'], |
|
| 124 | - $calendarUri, |
|
| 125 | - $calendarName, |
|
| 126 | - $calendarOwner, |
|
| 127 | - $accessBy, |
|
| 128 | - $permissions, |
|
| 129 | - ]; |
|
| 130 | - } |
|
| 31 | + public function __construct( |
|
| 32 | + private IUserManager $userManager, |
|
| 33 | + private Principal $principal, |
|
| 34 | + private CalDavBackend $caldav, |
|
| 35 | + private SharingMapper $mapper, |
|
| 36 | + ) { |
|
| 37 | + parent::__construct(); |
|
| 38 | + } |
|
| 39 | + |
|
| 40 | + protected function configure(): void { |
|
| 41 | + $this->addArgument( |
|
| 42 | + 'uid', |
|
| 43 | + InputArgument::REQUIRED, |
|
| 44 | + 'User whose calendar shares will be listed' |
|
| 45 | + ); |
|
| 46 | + $this->addOption( |
|
| 47 | + 'calendar-id', |
|
| 48 | + '', |
|
| 49 | + InputOption::VALUE_REQUIRED, |
|
| 50 | + 'List only shares for the given calendar id id', |
|
| 51 | + null, |
|
| 52 | + ); |
|
| 53 | + } |
|
| 54 | + |
|
| 55 | + protected function execute(InputInterface $input, OutputInterface $output): int { |
|
| 56 | + $user = (string)$input->getArgument('uid'); |
|
| 57 | + if (!$this->userManager->userExists($user)) { |
|
| 58 | + throw new \InvalidArgumentException("User $user is unknown"); |
|
| 59 | + } |
|
| 60 | + |
|
| 61 | + $principal = $this->principal->getPrincipalByPath('principals/users/' . $user); |
|
| 62 | + if ($principal === null) { |
|
| 63 | + throw new \InvalidArgumentException("Unable to fetch principal for user $user"); |
|
| 64 | + } |
|
| 65 | + |
|
| 66 | + $memberships = array_merge( |
|
| 67 | + [$principal['uri']], |
|
| 68 | + $this->principal->getGroupMembership($principal['uri']), |
|
| 69 | + $this->principal->getCircleMembership($principal['uri']), |
|
| 70 | + ); |
|
| 71 | + |
|
| 72 | + $shares = $this->mapper->getSharesByPrincipals($memberships, 'calendar'); |
|
| 73 | + |
|
| 74 | + $calendarId = $input->getOption('calendar-id'); |
|
| 75 | + if ($calendarId !== null) { |
|
| 76 | + $shares = array_filter($shares, fn ($share) => $share['resourceid'] === (int)$calendarId); |
|
| 77 | + } |
|
| 78 | + |
|
| 79 | + $rows = array_map(fn ($share) => $this->formatCalendarShare($share), $shares); |
|
| 80 | + |
|
| 81 | + if (count($rows) > 0) { |
|
| 82 | + $table = new Table($output); |
|
| 83 | + $table |
|
| 84 | + ->setHeaders(['Share Id', 'Calendar Id', 'Calendar URI', 'Calendar Name', 'Calendar Owner', 'Access By', 'Permissions']) |
|
| 85 | + ->setRows($rows) |
|
| 86 | + ->render(); |
|
| 87 | + } else { |
|
| 88 | + $output->writeln("User $user has no calendar shares"); |
|
| 89 | + } |
|
| 90 | + |
|
| 91 | + return self::SUCCESS; |
|
| 92 | + } |
|
| 93 | + |
|
| 94 | + private function formatCalendarShare(array $share): array { |
|
| 95 | + $calendarInfo = $this->caldav->getCalendarById($share['resourceid']); |
|
| 96 | + |
|
| 97 | + $calendarUri = 'Resource not found'; |
|
| 98 | + $calendarName = ''; |
|
| 99 | + $calendarOwner = ''; |
|
| 100 | + |
|
| 101 | + if ($calendarInfo !== null) { |
|
| 102 | + $calendarUri = $calendarInfo['uri']; |
|
| 103 | + $calendarName = $calendarInfo['{DAV:}displayname']; |
|
| 104 | + $calendarOwner = $calendarInfo['{http://nextcloud.com/ns}owner-displayname'] . ' (' . $calendarInfo['principaluri'] . ')'; |
|
| 105 | + } |
|
| 106 | + |
|
| 107 | + $accessBy = match (true) { |
|
| 108 | + str_starts_with($share['principaluri'], 'principals/users/') => 'Individual', |
|
| 109 | + str_starts_with($share['principaluri'], 'principals/groups/') => 'Group (' . $share['principaluri'] . ')', |
|
| 110 | + str_starts_with($share['principaluri'], 'principals/circles/') => 'Team (' . $share['principaluri'] . ')', |
|
| 111 | + default => $share['principaluri'], |
|
| 112 | + }; |
|
| 113 | + |
|
| 114 | + $permissions = match ($share['access']) { |
|
| 115 | + Backend::ACCESS_READ => 'Read', |
|
| 116 | + Backend::ACCESS_READ_WRITE => 'Read/Write', |
|
| 117 | + Backend::ACCESS_UNSHARED => 'Unshare', |
|
| 118 | + default => $share['access'], |
|
| 119 | + }; |
|
| 120 | + |
|
| 121 | + return [ |
|
| 122 | + $share['id'], |
|
| 123 | + $share['resourceid'], |
|
| 124 | + $calendarUri, |
|
| 125 | + $calendarName, |
|
| 126 | + $calendarOwner, |
|
| 127 | + $accessBy, |
|
| 128 | + $permissions, |
|
| 129 | + ]; |
|
| 130 | + } |
|
| 131 | 131 | } |
@@ -53,12 +53,12 @@ discard block |
||
| 53 | 53 | } |
| 54 | 54 | |
| 55 | 55 | protected function execute(InputInterface $input, OutputInterface $output): int { |
| 56 | - $user = (string)$input->getArgument('uid'); |
|
| 56 | + $user = (string) $input->getArgument('uid'); |
|
| 57 | 57 | if (!$this->userManager->userExists($user)) { |
| 58 | 58 | throw new \InvalidArgumentException("User $user is unknown"); |
| 59 | 59 | } |
| 60 | 60 | |
| 61 | - $principal = $this->principal->getPrincipalByPath('principals/users/' . $user); |
|
| 61 | + $principal = $this->principal->getPrincipalByPath('principals/users/'.$user); |
|
| 62 | 62 | if ($principal === null) { |
| 63 | 63 | throw new \InvalidArgumentException("Unable to fetch principal for user $user"); |
| 64 | 64 | } |
@@ -73,7 +73,7 @@ discard block |
||
| 73 | 73 | |
| 74 | 74 | $calendarId = $input->getOption('calendar-id'); |
| 75 | 75 | if ($calendarId !== null) { |
| 76 | - $shares = array_filter($shares, fn ($share) => $share['resourceid'] === (int)$calendarId); |
|
| 76 | + $shares = array_filter($shares, fn ($share) => $share['resourceid'] === (int) $calendarId); |
|
| 77 | 77 | } |
| 78 | 78 | |
| 79 | 79 | $rows = array_map(fn ($share) => $this->formatCalendarShare($share), $shares); |
@@ -101,13 +101,13 @@ discard block |
||
| 101 | 101 | if ($calendarInfo !== null) { |
| 102 | 102 | $calendarUri = $calendarInfo['uri']; |
| 103 | 103 | $calendarName = $calendarInfo['{DAV:}displayname']; |
| 104 | - $calendarOwner = $calendarInfo['{http://nextcloud.com/ns}owner-displayname'] . ' (' . $calendarInfo['principaluri'] . ')'; |
|
| 104 | + $calendarOwner = $calendarInfo['{http://nextcloud.com/ns}owner-displayname'].' ('.$calendarInfo['principaluri'].')'; |
|
| 105 | 105 | } |
| 106 | 106 | |
| 107 | 107 | $accessBy = match (true) { |
| 108 | 108 | str_starts_with($share['principaluri'], 'principals/users/') => 'Individual', |
| 109 | - str_starts_with($share['principaluri'], 'principals/groups/') => 'Group (' . $share['principaluri'] . ')', |
|
| 110 | - str_starts_with($share['principaluri'], 'principals/circles/') => 'Team (' . $share['principaluri'] . ')', |
|
| 109 | + str_starts_with($share['principaluri'], 'principals/groups/') => 'Group ('.$share['principaluri'].')', |
|
| 110 | + str_starts_with($share['principaluri'], 'principals/circles/') => 'Team ('.$share['principaluri'].')', |
|
| 111 | 111 | default => $share['principaluri'], |
| 112 | 112 | }; |
| 113 | 113 | |
@@ -27,88 +27,88 @@ |
||
| 27 | 27 | use Symfony\Component\Console\Question\ConfirmationQuestion; |
| 28 | 28 | |
| 29 | 29 | #[AsCommand( |
| 30 | - name: 'dav:clear-calendar-unshares', |
|
| 31 | - description: 'Clear calendar unshares for a user', |
|
| 32 | - hidden: false, |
|
| 30 | + name: 'dav:clear-calendar-unshares', |
|
| 31 | + description: 'Clear calendar unshares for a user', |
|
| 32 | + hidden: false, |
|
| 33 | 33 | )] |
| 34 | 34 | class ClearCalendarUnshares extends Command { |
| 35 | - public function __construct( |
|
| 36 | - private IUserManager $userManager, |
|
| 37 | - private IAppConfig $appConfig, |
|
| 38 | - private Principal $principal, |
|
| 39 | - private CalDavBackend $caldav, |
|
| 40 | - private Backend $sharingBackend, |
|
| 41 | - private Service $sharingService, |
|
| 42 | - private SharingMapper $mapper, |
|
| 43 | - ) { |
|
| 44 | - parent::__construct(); |
|
| 45 | - } |
|
| 46 | - |
|
| 47 | - protected function configure(): void { |
|
| 48 | - $this->addArgument( |
|
| 49 | - 'uid', |
|
| 50 | - InputArgument::REQUIRED, |
|
| 51 | - 'User whose unshares to clear' |
|
| 52 | - ); |
|
| 53 | - } |
|
| 54 | - |
|
| 55 | - protected function execute(InputInterface $input, OutputInterface $output): int { |
|
| 56 | - $user = (string)$input->getArgument('uid'); |
|
| 57 | - if (!$this->userManager->userExists($user)) { |
|
| 58 | - throw new \InvalidArgumentException("User $user is unknown"); |
|
| 59 | - } |
|
| 60 | - |
|
| 61 | - $principal = $this->principal->getPrincipalByPath('principals/users/' . $user); |
|
| 62 | - if ($principal === null) { |
|
| 63 | - throw new \InvalidArgumentException("Unable to fetch principal for user $user "); |
|
| 64 | - } |
|
| 65 | - |
|
| 66 | - $shares = $this->mapper->getSharesByPrincipals([$principal['uri']], 'calendar'); |
|
| 67 | - $unshares = array_filter($shares, static fn ($share) => $share['access'] === BackendAlias::ACCESS_UNSHARED); |
|
| 68 | - |
|
| 69 | - if (count($unshares) === 0) { |
|
| 70 | - $output->writeln("User $user has no calendar unshares"); |
|
| 71 | - return self::SUCCESS; |
|
| 72 | - } |
|
| 73 | - |
|
| 74 | - $rows = array_map(fn ($share) => $this->formatCalendarUnshare($share), $shares); |
|
| 75 | - |
|
| 76 | - $table = new Table($output); |
|
| 77 | - $table |
|
| 78 | - ->setHeaders(['Share Id', 'Calendar Id', 'Calendar URI', 'Calendar Name']) |
|
| 79 | - ->setRows($rows) |
|
| 80 | - ->render(); |
|
| 81 | - |
|
| 82 | - $output->writeln(''); |
|
| 83 | - |
|
| 84 | - /** @var QuestionHelper $helper */ |
|
| 85 | - $helper = $this->getHelper('question'); |
|
| 86 | - $question = new ConfirmationQuestion('Please confirm to delete the above calendar unshare entries [y/n]', false); |
|
| 87 | - |
|
| 88 | - if ($helper->ask($input, $output, $question)) { |
|
| 89 | - $this->mapper->deleteUnsharesByPrincipal($principal['uri'], 'calendar'); |
|
| 90 | - $output->writeln("Calendar unshares for user $user deleted"); |
|
| 91 | - } |
|
| 92 | - |
|
| 93 | - return self::SUCCESS; |
|
| 94 | - } |
|
| 95 | - |
|
| 96 | - private function formatCalendarUnshare(array $share): array { |
|
| 97 | - $calendarInfo = $this->caldav->getCalendarById($share['resourceid']); |
|
| 98 | - |
|
| 99 | - $resourceUri = 'Resource not found'; |
|
| 100 | - $resourceName = ''; |
|
| 101 | - |
|
| 102 | - if ($calendarInfo !== null) { |
|
| 103 | - $resourceUri = $calendarInfo['uri']; |
|
| 104 | - $resourceName = $calendarInfo['{DAV:}displayname']; |
|
| 105 | - } |
|
| 106 | - |
|
| 107 | - return [ |
|
| 108 | - $share['id'], |
|
| 109 | - $share['resourceid'], |
|
| 110 | - $resourceUri, |
|
| 111 | - $resourceName, |
|
| 112 | - ]; |
|
| 113 | - } |
|
| 35 | + public function __construct( |
|
| 36 | + private IUserManager $userManager, |
|
| 37 | + private IAppConfig $appConfig, |
|
| 38 | + private Principal $principal, |
|
| 39 | + private CalDavBackend $caldav, |
|
| 40 | + private Backend $sharingBackend, |
|
| 41 | + private Service $sharingService, |
|
| 42 | + private SharingMapper $mapper, |
|
| 43 | + ) { |
|
| 44 | + parent::__construct(); |
|
| 45 | + } |
|
| 46 | + |
|
| 47 | + protected function configure(): void { |
|
| 48 | + $this->addArgument( |
|
| 49 | + 'uid', |
|
| 50 | + InputArgument::REQUIRED, |
|
| 51 | + 'User whose unshares to clear' |
|
| 52 | + ); |
|
| 53 | + } |
|
| 54 | + |
|
| 55 | + protected function execute(InputInterface $input, OutputInterface $output): int { |
|
| 56 | + $user = (string)$input->getArgument('uid'); |
|
| 57 | + if (!$this->userManager->userExists($user)) { |
|
| 58 | + throw new \InvalidArgumentException("User $user is unknown"); |
|
| 59 | + } |
|
| 60 | + |
|
| 61 | + $principal = $this->principal->getPrincipalByPath('principals/users/' . $user); |
|
| 62 | + if ($principal === null) { |
|
| 63 | + throw new \InvalidArgumentException("Unable to fetch principal for user $user "); |
|
| 64 | + } |
|
| 65 | + |
|
| 66 | + $shares = $this->mapper->getSharesByPrincipals([$principal['uri']], 'calendar'); |
|
| 67 | + $unshares = array_filter($shares, static fn ($share) => $share['access'] === BackendAlias::ACCESS_UNSHARED); |
|
| 68 | + |
|
| 69 | + if (count($unshares) === 0) { |
|
| 70 | + $output->writeln("User $user has no calendar unshares"); |
|
| 71 | + return self::SUCCESS; |
|
| 72 | + } |
|
| 73 | + |
|
| 74 | + $rows = array_map(fn ($share) => $this->formatCalendarUnshare($share), $shares); |
|
| 75 | + |
|
| 76 | + $table = new Table($output); |
|
| 77 | + $table |
|
| 78 | + ->setHeaders(['Share Id', 'Calendar Id', 'Calendar URI', 'Calendar Name']) |
|
| 79 | + ->setRows($rows) |
|
| 80 | + ->render(); |
|
| 81 | + |
|
| 82 | + $output->writeln(''); |
|
| 83 | + |
|
| 84 | + /** @var QuestionHelper $helper */ |
|
| 85 | + $helper = $this->getHelper('question'); |
|
| 86 | + $question = new ConfirmationQuestion('Please confirm to delete the above calendar unshare entries [y/n]', false); |
|
| 87 | + |
|
| 88 | + if ($helper->ask($input, $output, $question)) { |
|
| 89 | + $this->mapper->deleteUnsharesByPrincipal($principal['uri'], 'calendar'); |
|
| 90 | + $output->writeln("Calendar unshares for user $user deleted"); |
|
| 91 | + } |
|
| 92 | + |
|
| 93 | + return self::SUCCESS; |
|
| 94 | + } |
|
| 95 | + |
|
| 96 | + private function formatCalendarUnshare(array $share): array { |
|
| 97 | + $calendarInfo = $this->caldav->getCalendarById($share['resourceid']); |
|
| 98 | + |
|
| 99 | + $resourceUri = 'Resource not found'; |
|
| 100 | + $resourceName = ''; |
|
| 101 | + |
|
| 102 | + if ($calendarInfo !== null) { |
|
| 103 | + $resourceUri = $calendarInfo['uri']; |
|
| 104 | + $resourceName = $calendarInfo['{DAV:}displayname']; |
|
| 105 | + } |
|
| 106 | + |
|
| 107 | + return [ |
|
| 108 | + $share['id'], |
|
| 109 | + $share['resourceid'], |
|
| 110 | + $resourceUri, |
|
| 111 | + $resourceName, |
|
| 112 | + ]; |
|
| 113 | + } |
|
| 114 | 114 | } |
@@ -53,12 +53,12 @@ |
||
| 53 | 53 | } |
| 54 | 54 | |
| 55 | 55 | protected function execute(InputInterface $input, OutputInterface $output): int { |
| 56 | - $user = (string)$input->getArgument('uid'); |
|
| 56 | + $user = (string) $input->getArgument('uid'); |
|
| 57 | 57 | if (!$this->userManager->userExists($user)) { |
| 58 | 58 | throw new \InvalidArgumentException("User $user is unknown"); |
| 59 | 59 | } |
| 60 | 60 | |
| 61 | - $principal = $this->principal->getPrincipalByPath('principals/users/' . $user); |
|
| 61 | + $principal = $this->principal->getPrincipalByPath('principals/users/'.$user); |
|
| 62 | 62 | if ($principal === null) { |
| 63 | 63 | throw new \InvalidArgumentException("Unable to fetch principal for user $user "); |
| 64 | 64 | } |
@@ -8,46 +8,46 @@ |
||
| 8 | 8 | namespace OCA\DAV\DAV\Sharing; |
| 9 | 9 | |
| 10 | 10 | abstract class SharingService { |
| 11 | - protected string $resourceType = ''; |
|
| 12 | - public function __construct( |
|
| 13 | - protected SharingMapper $mapper, |
|
| 14 | - ) { |
|
| 15 | - } |
|
| 16 | - |
|
| 17 | - public function getResourceType(): string { |
|
| 18 | - return $this->resourceType; |
|
| 19 | - } |
|
| 20 | - public function shareWith(int $resourceId, string $principal, int $access): void { |
|
| 21 | - // remove the share if it already exists |
|
| 22 | - $this->mapper->deleteShare($resourceId, $this->getResourceType(), $principal); |
|
| 23 | - $this->mapper->share($resourceId, $this->getResourceType(), $access, $principal); |
|
| 24 | - } |
|
| 25 | - |
|
| 26 | - public function unshare(int $resourceId, string $principal): void { |
|
| 27 | - $this->mapper->unshare($resourceId, $this->getResourceType(), $principal); |
|
| 28 | - } |
|
| 29 | - |
|
| 30 | - public function deleteShare(int $resourceId, string $principal): void { |
|
| 31 | - $this->mapper->deleteShare($resourceId, $this->getResourceType(), $principal); |
|
| 32 | - } |
|
| 33 | - |
|
| 34 | - public function deleteAllShares(int $resourceId): void { |
|
| 35 | - $this->mapper->deleteAllShares($resourceId, $this->getResourceType()); |
|
| 36 | - } |
|
| 37 | - |
|
| 38 | - public function deleteAllSharesByUser(string $principaluri): void { |
|
| 39 | - $this->mapper->deleteAllSharesByUser($principaluri, $this->getResourceType()); |
|
| 40 | - } |
|
| 41 | - |
|
| 42 | - public function getShares(int $resourceId): array { |
|
| 43 | - return $this->mapper->getSharesForId($resourceId, $this->getResourceType()); |
|
| 44 | - } |
|
| 45 | - |
|
| 46 | - public function getUnshares(int $resourceId): array { |
|
| 47 | - return $this->mapper->getUnsharesForId($resourceId, $this->getResourceType()); |
|
| 48 | - } |
|
| 49 | - |
|
| 50 | - public function getSharesForIds(array $resourceIds): array { |
|
| 51 | - return $this->mapper->getSharesForIds($resourceIds, $this->getResourceType()); |
|
| 52 | - } |
|
| 11 | + protected string $resourceType = ''; |
|
| 12 | + public function __construct( |
|
| 13 | + protected SharingMapper $mapper, |
|
| 14 | + ) { |
|
| 15 | + } |
|
| 16 | + |
|
| 17 | + public function getResourceType(): string { |
|
| 18 | + return $this->resourceType; |
|
| 19 | + } |
|
| 20 | + public function shareWith(int $resourceId, string $principal, int $access): void { |
|
| 21 | + // remove the share if it already exists |
|
| 22 | + $this->mapper->deleteShare($resourceId, $this->getResourceType(), $principal); |
|
| 23 | + $this->mapper->share($resourceId, $this->getResourceType(), $access, $principal); |
|
| 24 | + } |
|
| 25 | + |
|
| 26 | + public function unshare(int $resourceId, string $principal): void { |
|
| 27 | + $this->mapper->unshare($resourceId, $this->getResourceType(), $principal); |
|
| 28 | + } |
|
| 29 | + |
|
| 30 | + public function deleteShare(int $resourceId, string $principal): void { |
|
| 31 | + $this->mapper->deleteShare($resourceId, $this->getResourceType(), $principal); |
|
| 32 | + } |
|
| 33 | + |
|
| 34 | + public function deleteAllShares(int $resourceId): void { |
|
| 35 | + $this->mapper->deleteAllShares($resourceId, $this->getResourceType()); |
|
| 36 | + } |
|
| 37 | + |
|
| 38 | + public function deleteAllSharesByUser(string $principaluri): void { |
|
| 39 | + $this->mapper->deleteAllSharesByUser($principaluri, $this->getResourceType()); |
|
| 40 | + } |
|
| 41 | + |
|
| 42 | + public function getShares(int $resourceId): array { |
|
| 43 | + return $this->mapper->getSharesForId($resourceId, $this->getResourceType()); |
|
| 44 | + } |
|
| 45 | + |
|
| 46 | + public function getUnshares(int $resourceId): array { |
|
| 47 | + return $this->mapper->getUnsharesForId($resourceId, $this->getResourceType()); |
|
| 48 | + } |
|
| 49 | + |
|
| 50 | + public function getSharesForIds(array $resourceIds): array { |
|
| 51 | + return $this->mapper->getSharesForIds($resourceIds, $this->getResourceType()); |
|
| 52 | + } |
|
| 53 | 53 | } |
@@ -11,127 +11,127 @@ |
||
| 11 | 11 | use OCP\IDBConnection; |
| 12 | 12 | |
| 13 | 13 | class SharingMapper { |
| 14 | - public function __construct( |
|
| 15 | - private IDBConnection $db, |
|
| 16 | - ) { |
|
| 17 | - } |
|
| 18 | - |
|
| 19 | - protected function getSharesForIdByAccess(int $resourceId, string $resourceType, bool $sharesWithAccess): array { |
|
| 20 | - $query = $this->db->getQueryBuilder(); |
|
| 21 | - $query->select(['principaluri', 'access']) |
|
| 22 | - ->from('dav_shares') |
|
| 23 | - ->where($query->expr()->eq('resourceid', $query->createNamedParameter($resourceId, IQueryBuilder::PARAM_INT))) |
|
| 24 | - ->andWhere($query->expr()->eq('type', $query->createNamedParameter($resourceType, IQueryBuilder::PARAM_STR))) |
|
| 25 | - ->groupBy(['principaluri', 'access']); |
|
| 26 | - |
|
| 27 | - if ($sharesWithAccess) { |
|
| 28 | - $query->andWhere($query->expr()->neq('access', $query->createNamedParameter(Backend::ACCESS_UNSHARED, IQueryBuilder::PARAM_INT))); |
|
| 29 | - } else { |
|
| 30 | - $query->andWhere($query->expr()->eq('access', $query->createNamedParameter(Backend::ACCESS_UNSHARED, IQueryBuilder::PARAM_INT))); |
|
| 31 | - } |
|
| 32 | - |
|
| 33 | - $result = $query->executeQuery(); |
|
| 34 | - $rows = $result->fetchAll(); |
|
| 35 | - $result->closeCursor(); |
|
| 36 | - return $rows; |
|
| 37 | - } |
|
| 38 | - |
|
| 39 | - public function getSharesForId(int $resourceId, string $resourceType): array { |
|
| 40 | - return $this->getSharesForIdByAccess($resourceId, $resourceType, true); |
|
| 41 | - } |
|
| 42 | - |
|
| 43 | - public function getUnsharesForId(int $resourceId, string $resourceType): array { |
|
| 44 | - return $this->getSharesForIdByAccess($resourceId, $resourceType, false); |
|
| 45 | - } |
|
| 46 | - |
|
| 47 | - public function getSharesForIds(array $resourceIds, string $resourceType): array { |
|
| 48 | - $query = $this->db->getQueryBuilder(); |
|
| 49 | - $result = $query->select(['resourceid', 'principaluri', 'access']) |
|
| 50 | - ->from('dav_shares') |
|
| 51 | - ->where($query->expr()->in('resourceid', $query->createNamedParameter($resourceIds, IQueryBuilder::PARAM_INT_ARRAY))) |
|
| 52 | - ->andWhere($query->expr()->eq('type', $query->createNamedParameter($resourceType))) |
|
| 53 | - ->andWhere($query->expr()->neq('access', $query->createNamedParameter(Backend::ACCESS_UNSHARED, IQueryBuilder::PARAM_INT))) |
|
| 54 | - ->groupBy(['principaluri', 'access', 'resourceid']) |
|
| 55 | - ->executeQuery(); |
|
| 56 | - |
|
| 57 | - $rows = $result->fetchAll(); |
|
| 58 | - $result->closeCursor(); |
|
| 59 | - return $rows; |
|
| 60 | - } |
|
| 61 | - |
|
| 62 | - public function unshare(int $resourceId, string $resourceType, string $principal): void { |
|
| 63 | - $query = $this->db->getQueryBuilder(); |
|
| 64 | - $query->insert('dav_shares') |
|
| 65 | - ->values([ |
|
| 66 | - 'principaluri' => $query->createNamedParameter($principal), |
|
| 67 | - 'type' => $query->createNamedParameter($resourceType), |
|
| 68 | - 'access' => $query->createNamedParameter(Backend::ACCESS_UNSHARED), |
|
| 69 | - 'resourceid' => $query->createNamedParameter($resourceId) |
|
| 70 | - ]); |
|
| 71 | - $query->executeStatement(); |
|
| 72 | - } |
|
| 73 | - |
|
| 74 | - public function share(int $resourceId, string $resourceType, int $access, string $principal): void { |
|
| 75 | - $query = $this->db->getQueryBuilder(); |
|
| 76 | - $query->insert('dav_shares') |
|
| 77 | - ->values([ |
|
| 78 | - 'principaluri' => $query->createNamedParameter($principal), |
|
| 79 | - 'type' => $query->createNamedParameter($resourceType), |
|
| 80 | - 'access' => $query->createNamedParameter($access), |
|
| 81 | - 'resourceid' => $query->createNamedParameter($resourceId) |
|
| 82 | - ]); |
|
| 83 | - $query->executeStatement(); |
|
| 84 | - } |
|
| 85 | - |
|
| 86 | - public function deleteShare(int $resourceId, string $resourceType, string $principal): void { |
|
| 87 | - $query = $this->db->getQueryBuilder(); |
|
| 88 | - $query->delete('dav_shares'); |
|
| 89 | - $query->where( |
|
| 90 | - $query->expr()->eq('resourceid', $query->createNamedParameter($resourceId, IQueryBuilder::PARAM_INT)), |
|
| 91 | - $query->expr()->eq('type', $query->createNamedParameter($resourceType)), |
|
| 92 | - $query->expr()->eq('principaluri', $query->createNamedParameter($principal)) |
|
| 93 | - ); |
|
| 94 | - $query->executeStatement(); |
|
| 95 | - |
|
| 96 | - } |
|
| 97 | - |
|
| 98 | - public function deleteAllShares(int $resourceId, string $resourceType): void { |
|
| 99 | - $query = $this->db->getQueryBuilder(); |
|
| 100 | - $query->delete('dav_shares') |
|
| 101 | - ->where($query->expr()->eq('resourceid', $query->createNamedParameter($resourceId))) |
|
| 102 | - ->andWhere($query->expr()->eq('type', $query->createNamedParameter($resourceType))) |
|
| 103 | - ->executeStatement(); |
|
| 104 | - } |
|
| 105 | - |
|
| 106 | - public function deleteAllSharesByUser(string $principaluri, string $resourceType): void { |
|
| 107 | - $query = $this->db->getQueryBuilder(); |
|
| 108 | - $query->delete('dav_shares') |
|
| 109 | - ->where($query->expr()->eq('principaluri', $query->createNamedParameter($principaluri))) |
|
| 110 | - ->andWhere($query->expr()->eq('type', $query->createNamedParameter($resourceType))) |
|
| 111 | - ->executeStatement(); |
|
| 112 | - } |
|
| 113 | - |
|
| 114 | - public function getSharesByPrincipals(array $principals, string $resourceType): array { |
|
| 115 | - $query = $this->db->getQueryBuilder(); |
|
| 116 | - $result = $query->select(['id', 'principaluri', 'type', 'access', 'resourceid']) |
|
| 117 | - ->from('dav_shares') |
|
| 118 | - ->where($query->expr()->in('principaluri', $query->createNamedParameter($principals, IQueryBuilder::PARAM_STR_ARRAY), IQueryBuilder::PARAM_STR_ARRAY)) |
|
| 119 | - ->andWhere($query->expr()->eq('type', $query->createNamedParameter($resourceType))) |
|
| 120 | - ->orderBy('id') |
|
| 121 | - ->executeQuery(); |
|
| 122 | - |
|
| 123 | - $rows = $result->fetchAll(); |
|
| 124 | - $result->closeCursor(); |
|
| 125 | - |
|
| 126 | - return $rows; |
|
| 127 | - } |
|
| 128 | - |
|
| 129 | - public function deleteUnsharesByPrincipal(string $principal, string $resourceType): void { |
|
| 130 | - $query = $this->db->getQueryBuilder(); |
|
| 131 | - $query->delete('dav_shares') |
|
| 132 | - ->where($query->expr()->eq('principaluri', $query->createNamedParameter($principal))) |
|
| 133 | - ->andWhere($query->expr()->eq('type', $query->createNamedParameter($resourceType))) |
|
| 134 | - ->andWhere($query->expr()->eq('access', $query->createNamedParameter(Backend::ACCESS_UNSHARED, IQueryBuilder::PARAM_INT))) |
|
| 135 | - ->executeStatement(); |
|
| 136 | - } |
|
| 14 | + public function __construct( |
|
| 15 | + private IDBConnection $db, |
|
| 16 | + ) { |
|
| 17 | + } |
|
| 18 | + |
|
| 19 | + protected function getSharesForIdByAccess(int $resourceId, string $resourceType, bool $sharesWithAccess): array { |
|
| 20 | + $query = $this->db->getQueryBuilder(); |
|
| 21 | + $query->select(['principaluri', 'access']) |
|
| 22 | + ->from('dav_shares') |
|
| 23 | + ->where($query->expr()->eq('resourceid', $query->createNamedParameter($resourceId, IQueryBuilder::PARAM_INT))) |
|
| 24 | + ->andWhere($query->expr()->eq('type', $query->createNamedParameter($resourceType, IQueryBuilder::PARAM_STR))) |
|
| 25 | + ->groupBy(['principaluri', 'access']); |
|
| 26 | + |
|
| 27 | + if ($sharesWithAccess) { |
|
| 28 | + $query->andWhere($query->expr()->neq('access', $query->createNamedParameter(Backend::ACCESS_UNSHARED, IQueryBuilder::PARAM_INT))); |
|
| 29 | + } else { |
|
| 30 | + $query->andWhere($query->expr()->eq('access', $query->createNamedParameter(Backend::ACCESS_UNSHARED, IQueryBuilder::PARAM_INT))); |
|
| 31 | + } |
|
| 32 | + |
|
| 33 | + $result = $query->executeQuery(); |
|
| 34 | + $rows = $result->fetchAll(); |
|
| 35 | + $result->closeCursor(); |
|
| 36 | + return $rows; |
|
| 37 | + } |
|
| 38 | + |
|
| 39 | + public function getSharesForId(int $resourceId, string $resourceType): array { |
|
| 40 | + return $this->getSharesForIdByAccess($resourceId, $resourceType, true); |
|
| 41 | + } |
|
| 42 | + |
|
| 43 | + public function getUnsharesForId(int $resourceId, string $resourceType): array { |
|
| 44 | + return $this->getSharesForIdByAccess($resourceId, $resourceType, false); |
|
| 45 | + } |
|
| 46 | + |
|
| 47 | + public function getSharesForIds(array $resourceIds, string $resourceType): array { |
|
| 48 | + $query = $this->db->getQueryBuilder(); |
|
| 49 | + $result = $query->select(['resourceid', 'principaluri', 'access']) |
|
| 50 | + ->from('dav_shares') |
|
| 51 | + ->where($query->expr()->in('resourceid', $query->createNamedParameter($resourceIds, IQueryBuilder::PARAM_INT_ARRAY))) |
|
| 52 | + ->andWhere($query->expr()->eq('type', $query->createNamedParameter($resourceType))) |
|
| 53 | + ->andWhere($query->expr()->neq('access', $query->createNamedParameter(Backend::ACCESS_UNSHARED, IQueryBuilder::PARAM_INT))) |
|
| 54 | + ->groupBy(['principaluri', 'access', 'resourceid']) |
|
| 55 | + ->executeQuery(); |
|
| 56 | + |
|
| 57 | + $rows = $result->fetchAll(); |
|
| 58 | + $result->closeCursor(); |
|
| 59 | + return $rows; |
|
| 60 | + } |
|
| 61 | + |
|
| 62 | + public function unshare(int $resourceId, string $resourceType, string $principal): void { |
|
| 63 | + $query = $this->db->getQueryBuilder(); |
|
| 64 | + $query->insert('dav_shares') |
|
| 65 | + ->values([ |
|
| 66 | + 'principaluri' => $query->createNamedParameter($principal), |
|
| 67 | + 'type' => $query->createNamedParameter($resourceType), |
|
| 68 | + 'access' => $query->createNamedParameter(Backend::ACCESS_UNSHARED), |
|
| 69 | + 'resourceid' => $query->createNamedParameter($resourceId) |
|
| 70 | + ]); |
|
| 71 | + $query->executeStatement(); |
|
| 72 | + } |
|
| 73 | + |
|
| 74 | + public function share(int $resourceId, string $resourceType, int $access, string $principal): void { |
|
| 75 | + $query = $this->db->getQueryBuilder(); |
|
| 76 | + $query->insert('dav_shares') |
|
| 77 | + ->values([ |
|
| 78 | + 'principaluri' => $query->createNamedParameter($principal), |
|
| 79 | + 'type' => $query->createNamedParameter($resourceType), |
|
| 80 | + 'access' => $query->createNamedParameter($access), |
|
| 81 | + 'resourceid' => $query->createNamedParameter($resourceId) |
|
| 82 | + ]); |
|
| 83 | + $query->executeStatement(); |
|
| 84 | + } |
|
| 85 | + |
|
| 86 | + public function deleteShare(int $resourceId, string $resourceType, string $principal): void { |
|
| 87 | + $query = $this->db->getQueryBuilder(); |
|
| 88 | + $query->delete('dav_shares'); |
|
| 89 | + $query->where( |
|
| 90 | + $query->expr()->eq('resourceid', $query->createNamedParameter($resourceId, IQueryBuilder::PARAM_INT)), |
|
| 91 | + $query->expr()->eq('type', $query->createNamedParameter($resourceType)), |
|
| 92 | + $query->expr()->eq('principaluri', $query->createNamedParameter($principal)) |
|
| 93 | + ); |
|
| 94 | + $query->executeStatement(); |
|
| 95 | + |
|
| 96 | + } |
|
| 97 | + |
|
| 98 | + public function deleteAllShares(int $resourceId, string $resourceType): void { |
|
| 99 | + $query = $this->db->getQueryBuilder(); |
|
| 100 | + $query->delete('dav_shares') |
|
| 101 | + ->where($query->expr()->eq('resourceid', $query->createNamedParameter($resourceId))) |
|
| 102 | + ->andWhere($query->expr()->eq('type', $query->createNamedParameter($resourceType))) |
|
| 103 | + ->executeStatement(); |
|
| 104 | + } |
|
| 105 | + |
|
| 106 | + public function deleteAllSharesByUser(string $principaluri, string $resourceType): void { |
|
| 107 | + $query = $this->db->getQueryBuilder(); |
|
| 108 | + $query->delete('dav_shares') |
|
| 109 | + ->where($query->expr()->eq('principaluri', $query->createNamedParameter($principaluri))) |
|
| 110 | + ->andWhere($query->expr()->eq('type', $query->createNamedParameter($resourceType))) |
|
| 111 | + ->executeStatement(); |
|
| 112 | + } |
|
| 113 | + |
|
| 114 | + public function getSharesByPrincipals(array $principals, string $resourceType): array { |
|
| 115 | + $query = $this->db->getQueryBuilder(); |
|
| 116 | + $result = $query->select(['id', 'principaluri', 'type', 'access', 'resourceid']) |
|
| 117 | + ->from('dav_shares') |
|
| 118 | + ->where($query->expr()->in('principaluri', $query->createNamedParameter($principals, IQueryBuilder::PARAM_STR_ARRAY), IQueryBuilder::PARAM_STR_ARRAY)) |
|
| 119 | + ->andWhere($query->expr()->eq('type', $query->createNamedParameter($resourceType))) |
|
| 120 | + ->orderBy('id') |
|
| 121 | + ->executeQuery(); |
|
| 122 | + |
|
| 123 | + $rows = $result->fetchAll(); |
|
| 124 | + $result->closeCursor(); |
|
| 125 | + |
|
| 126 | + return $rows; |
|
| 127 | + } |
|
| 128 | + |
|
| 129 | + public function deleteUnsharesByPrincipal(string $principal, string $resourceType): void { |
|
| 130 | + $query = $this->db->getQueryBuilder(); |
|
| 131 | + $query->delete('dav_shares') |
|
| 132 | + ->where($query->expr()->eq('principaluri', $query->createNamedParameter($principal))) |
|
| 133 | + ->andWhere($query->expr()->eq('type', $query->createNamedParameter($resourceType))) |
|
| 134 | + ->andWhere($query->expr()->eq('access', $query->createNamedParameter(Backend::ACCESS_UNSHARED, IQueryBuilder::PARAM_INT))) |
|
| 135 | + ->executeStatement(); |
|
| 136 | + } |
|
| 137 | 137 | } |
@@ -17,224 +17,224 @@ |
||
| 17 | 17 | use Psr\Log\LoggerInterface; |
| 18 | 18 | |
| 19 | 19 | abstract class Backend { |
| 20 | - use TTransactional; |
|
| 21 | - public const ACCESS_OWNER = 1; |
|
| 22 | - |
|
| 23 | - public const ACCESS_READ_WRITE = 2; |
|
| 24 | - public const ACCESS_READ = 3; |
|
| 25 | - // 4 is already in use for public calendars |
|
| 26 | - public const ACCESS_UNSHARED = 5; |
|
| 27 | - |
|
| 28 | - private ICache $shareCache; |
|
| 29 | - |
|
| 30 | - public function __construct( |
|
| 31 | - private IUserManager $userManager, |
|
| 32 | - private IGroupManager $groupManager, |
|
| 33 | - private Principal $principalBackend, |
|
| 34 | - private ICacheFactory $cacheFactory, |
|
| 35 | - private SharingService $service, |
|
| 36 | - private LoggerInterface $logger, |
|
| 37 | - ) { |
|
| 38 | - $this->shareCache = $this->cacheFactory->createInMemory(); |
|
| 39 | - } |
|
| 40 | - |
|
| 41 | - /** |
|
| 42 | - * @param list<array{href: string, commonName: string, readOnly: bool}> $add |
|
| 43 | - * @param list<string> $remove |
|
| 44 | - */ |
|
| 45 | - public function updateShares(IShareable $shareable, array $add, array $remove, array $oldShares = []): void { |
|
| 46 | - $this->shareCache->clear(); |
|
| 47 | - foreach ($add as $element) { |
|
| 48 | - $principal = $this->principalBackend->findByUri($element['href'], ''); |
|
| 49 | - if (empty($principal)) { |
|
| 50 | - continue; |
|
| 51 | - } |
|
| 52 | - |
|
| 53 | - // We need to validate manually because some principals are only virtual |
|
| 54 | - // i.e. Group principals |
|
| 55 | - $principalparts = explode('/', $principal, 3); |
|
| 56 | - if (count($principalparts) !== 3 || $principalparts[0] !== 'principals' || !in_array($principalparts[1], ['users', 'groups', 'circles'], true)) { |
|
| 57 | - // Invalid principal |
|
| 58 | - continue; |
|
| 59 | - } |
|
| 60 | - |
|
| 61 | - // Don't add share for owner |
|
| 62 | - if ($shareable->getOwner() !== null && strcasecmp($shareable->getOwner(), $principal) === 0) { |
|
| 63 | - continue; |
|
| 64 | - } |
|
| 65 | - |
|
| 66 | - $principalparts[2] = urldecode($principalparts[2]); |
|
| 67 | - if (($principalparts[1] === 'users' && !$this->userManager->userExists($principalparts[2])) || |
|
| 68 | - ($principalparts[1] === 'groups' && !$this->groupManager->groupExists($principalparts[2]))) { |
|
| 69 | - // User or group does not exist |
|
| 70 | - continue; |
|
| 71 | - } |
|
| 72 | - |
|
| 73 | - $access = Backend::ACCESS_READ; |
|
| 74 | - if (isset($element['readOnly'])) { |
|
| 75 | - $access = $element['readOnly'] ? Backend::ACCESS_READ : Backend::ACCESS_READ_WRITE; |
|
| 76 | - } |
|
| 77 | - |
|
| 78 | - $this->service->shareWith($shareable->getResourceId(), $principal, $access); |
|
| 79 | - } |
|
| 80 | - foreach ($remove as $element) { |
|
| 81 | - $principal = $this->principalBackend->findByUri($element, ''); |
|
| 82 | - if (empty($principal)) { |
|
| 83 | - continue; |
|
| 84 | - } |
|
| 85 | - |
|
| 86 | - // Don't add unshare for owner |
|
| 87 | - if ($shareable->getOwner() !== null && strcasecmp($shareable->getOwner(), $principal) === 0) { |
|
| 88 | - continue; |
|
| 89 | - } |
|
| 90 | - |
|
| 91 | - // Delete any possible direct shares (since the frontend does not separate between them) |
|
| 92 | - $this->service->deleteShare($shareable->getResourceId(), $principal); |
|
| 93 | - } |
|
| 94 | - } |
|
| 95 | - |
|
| 96 | - public function deleteAllShares(int $resourceId): void { |
|
| 97 | - $this->shareCache->clear(); |
|
| 98 | - $this->service->deleteAllShares($resourceId); |
|
| 99 | - } |
|
| 100 | - |
|
| 101 | - public function deleteAllSharesByUser(string $principaluri): void { |
|
| 102 | - $this->shareCache->clear(); |
|
| 103 | - $this->service->deleteAllSharesByUser($principaluri); |
|
| 104 | - } |
|
| 105 | - |
|
| 106 | - /** |
|
| 107 | - * Returns the list of people whom this resource is shared with. |
|
| 108 | - * |
|
| 109 | - * Every element in this array should have the following properties: |
|
| 110 | - * * href - Often a mailto: address |
|
| 111 | - * * commonName - Optional, for example a first + last name |
|
| 112 | - * * status - See the Sabre\CalDAV\SharingPlugin::STATUS_ constants. |
|
| 113 | - * * readOnly - boolean |
|
| 114 | - * |
|
| 115 | - * @param int $resourceId |
|
| 116 | - * @return list<array{href: string, commonName: string, status: int, readOnly: bool, '{http://owncloud.org/ns}principal': string, '{http://owncloud.org/ns}group-share': bool}> |
|
| 117 | - */ |
|
| 118 | - public function getShares(int $resourceId): array { |
|
| 119 | - $cached = $this->shareCache->get((string)$resourceId); |
|
| 120 | - if ($cached) { |
|
| 121 | - return $cached; |
|
| 122 | - } |
|
| 123 | - |
|
| 124 | - $rows = $this->service->getShares($resourceId); |
|
| 125 | - $shares = []; |
|
| 126 | - foreach ($rows as $row) { |
|
| 127 | - $p = $this->principalBackend->getPrincipalByPath($row['principaluri']); |
|
| 128 | - $shares[] = [ |
|
| 129 | - 'href' => "principal:{$row['principaluri']}", |
|
| 130 | - 'commonName' => isset($p['{DAV:}displayname']) ? (string)$p['{DAV:}displayname'] : '', |
|
| 131 | - 'status' => 1, |
|
| 132 | - 'readOnly' => (int)$row['access'] === Backend::ACCESS_READ, |
|
| 133 | - '{http://owncloud.org/ns}principal' => (string)$row['principaluri'], |
|
| 134 | - '{http://owncloud.org/ns}group-share' => isset($p['uri']) && (str_starts_with($p['uri'], 'principals/groups') || str_starts_with($p['uri'], 'principals/circles')) |
|
| 135 | - ]; |
|
| 136 | - } |
|
| 137 | - $this->shareCache->set((string)$resourceId, $shares); |
|
| 138 | - return $shares; |
|
| 139 | - } |
|
| 140 | - |
|
| 141 | - public function preloadShares(array $resourceIds): void { |
|
| 142 | - $resourceIds = array_filter($resourceIds, function (int $resourceId) { |
|
| 143 | - return empty($this->shareCache->get((string)$resourceId)); |
|
| 144 | - }); |
|
| 145 | - if (empty($resourceIds)) { |
|
| 146 | - return; |
|
| 147 | - } |
|
| 148 | - |
|
| 149 | - $rows = $this->service->getSharesForIds($resourceIds); |
|
| 150 | - $sharesByResource = array_fill_keys($resourceIds, []); |
|
| 151 | - foreach ($rows as $row) { |
|
| 152 | - $resourceId = (int)$row['resourceid']; |
|
| 153 | - $p = $this->principalBackend->getPrincipalByPath($row['principaluri']); |
|
| 154 | - $sharesByResource[$resourceId][] = [ |
|
| 155 | - 'href' => "principal:{$row['principaluri']}", |
|
| 156 | - 'commonName' => isset($p['{DAV:}displayname']) ? (string)$p['{DAV:}displayname'] : '', |
|
| 157 | - 'status' => 1, |
|
| 158 | - 'readOnly' => (int)$row['access'] === self::ACCESS_READ, |
|
| 159 | - '{http://owncloud.org/ns}principal' => (string)$row['principaluri'], |
|
| 160 | - '{http://owncloud.org/ns}group-share' => isset($p['uri']) && str_starts_with($p['uri'], 'principals/groups') |
|
| 161 | - ]; |
|
| 162 | - $this->shareCache->set((string)$resourceId, $sharesByResource[$resourceId]); |
|
| 163 | - } |
|
| 164 | - } |
|
| 165 | - |
|
| 166 | - /** |
|
| 167 | - * For shared resources the sharee is set in the ACL of the resource |
|
| 168 | - * |
|
| 169 | - * @param int $resourceId |
|
| 170 | - * @param list<array{privilege: string, principal: string, protected: bool}> $acl |
|
| 171 | - * @param list<array{href: string, commonName: string, status: int, readOnly: bool, '{http://owncloud.org/ns}principal': string, '{http://owncloud.org/ns}group-share': bool}> $shares |
|
| 172 | - * @return list<array{principal: string, privilege: string, protected: bool}> |
|
| 173 | - */ |
|
| 174 | - public function applyShareAcl(array $shares, array $acl): array { |
|
| 175 | - foreach ($shares as $share) { |
|
| 176 | - $acl[] = [ |
|
| 177 | - 'privilege' => '{DAV:}read', |
|
| 178 | - 'principal' => $share['{' . \OCA\DAV\DAV\Sharing\Plugin::NS_OWNCLOUD . '}principal'], |
|
| 179 | - 'protected' => true, |
|
| 180 | - ]; |
|
| 181 | - if (!$share['readOnly']) { |
|
| 182 | - $acl[] = [ |
|
| 183 | - 'privilege' => '{DAV:}write', |
|
| 184 | - 'principal' => $share['{' . \OCA\DAV\DAV\Sharing\Plugin::NS_OWNCLOUD . '}principal'], |
|
| 185 | - 'protected' => true, |
|
| 186 | - ]; |
|
| 187 | - } elseif (in_array($this->service->getResourceType(), ['calendar','addressbook'])) { |
|
| 188 | - // Allow changing the properties of read only calendars, |
|
| 189 | - // so users can change the visibility. |
|
| 190 | - $acl[] = [ |
|
| 191 | - 'privilege' => '{DAV:}write-properties', |
|
| 192 | - 'principal' => $share['{' . \OCA\DAV\DAV\Sharing\Plugin::NS_OWNCLOUD . '}principal'], |
|
| 193 | - 'protected' => true, |
|
| 194 | - ]; |
|
| 195 | - } |
|
| 196 | - } |
|
| 197 | - return $acl; |
|
| 198 | - } |
|
| 199 | - |
|
| 200 | - public function unshare(IShareable $shareable, string $principalUri): bool { |
|
| 201 | - $this->shareCache->clear(); |
|
| 20 | + use TTransactional; |
|
| 21 | + public const ACCESS_OWNER = 1; |
|
| 22 | + |
|
| 23 | + public const ACCESS_READ_WRITE = 2; |
|
| 24 | + public const ACCESS_READ = 3; |
|
| 25 | + // 4 is already in use for public calendars |
|
| 26 | + public const ACCESS_UNSHARED = 5; |
|
| 27 | + |
|
| 28 | + private ICache $shareCache; |
|
| 29 | + |
|
| 30 | + public function __construct( |
|
| 31 | + private IUserManager $userManager, |
|
| 32 | + private IGroupManager $groupManager, |
|
| 33 | + private Principal $principalBackend, |
|
| 34 | + private ICacheFactory $cacheFactory, |
|
| 35 | + private SharingService $service, |
|
| 36 | + private LoggerInterface $logger, |
|
| 37 | + ) { |
|
| 38 | + $this->shareCache = $this->cacheFactory->createInMemory(); |
|
| 39 | + } |
|
| 40 | + |
|
| 41 | + /** |
|
| 42 | + * @param list<array{href: string, commonName: string, readOnly: bool}> $add |
|
| 43 | + * @param list<string> $remove |
|
| 44 | + */ |
|
| 45 | + public function updateShares(IShareable $shareable, array $add, array $remove, array $oldShares = []): void { |
|
| 46 | + $this->shareCache->clear(); |
|
| 47 | + foreach ($add as $element) { |
|
| 48 | + $principal = $this->principalBackend->findByUri($element['href'], ''); |
|
| 49 | + if (empty($principal)) { |
|
| 50 | + continue; |
|
| 51 | + } |
|
| 52 | + |
|
| 53 | + // We need to validate manually because some principals are only virtual |
|
| 54 | + // i.e. Group principals |
|
| 55 | + $principalparts = explode('/', $principal, 3); |
|
| 56 | + if (count($principalparts) !== 3 || $principalparts[0] !== 'principals' || !in_array($principalparts[1], ['users', 'groups', 'circles'], true)) { |
|
| 57 | + // Invalid principal |
|
| 58 | + continue; |
|
| 59 | + } |
|
| 60 | + |
|
| 61 | + // Don't add share for owner |
|
| 62 | + if ($shareable->getOwner() !== null && strcasecmp($shareable->getOwner(), $principal) === 0) { |
|
| 63 | + continue; |
|
| 64 | + } |
|
| 65 | + |
|
| 66 | + $principalparts[2] = urldecode($principalparts[2]); |
|
| 67 | + if (($principalparts[1] === 'users' && !$this->userManager->userExists($principalparts[2])) || |
|
| 68 | + ($principalparts[1] === 'groups' && !$this->groupManager->groupExists($principalparts[2]))) { |
|
| 69 | + // User or group does not exist |
|
| 70 | + continue; |
|
| 71 | + } |
|
| 72 | + |
|
| 73 | + $access = Backend::ACCESS_READ; |
|
| 74 | + if (isset($element['readOnly'])) { |
|
| 75 | + $access = $element['readOnly'] ? Backend::ACCESS_READ : Backend::ACCESS_READ_WRITE; |
|
| 76 | + } |
|
| 77 | + |
|
| 78 | + $this->service->shareWith($shareable->getResourceId(), $principal, $access); |
|
| 79 | + } |
|
| 80 | + foreach ($remove as $element) { |
|
| 81 | + $principal = $this->principalBackend->findByUri($element, ''); |
|
| 82 | + if (empty($principal)) { |
|
| 83 | + continue; |
|
| 84 | + } |
|
| 85 | + |
|
| 86 | + // Don't add unshare for owner |
|
| 87 | + if ($shareable->getOwner() !== null && strcasecmp($shareable->getOwner(), $principal) === 0) { |
|
| 88 | + continue; |
|
| 89 | + } |
|
| 90 | + |
|
| 91 | + // Delete any possible direct shares (since the frontend does not separate between them) |
|
| 92 | + $this->service->deleteShare($shareable->getResourceId(), $principal); |
|
| 93 | + } |
|
| 94 | + } |
|
| 95 | + |
|
| 96 | + public function deleteAllShares(int $resourceId): void { |
|
| 97 | + $this->shareCache->clear(); |
|
| 98 | + $this->service->deleteAllShares($resourceId); |
|
| 99 | + } |
|
| 100 | + |
|
| 101 | + public function deleteAllSharesByUser(string $principaluri): void { |
|
| 102 | + $this->shareCache->clear(); |
|
| 103 | + $this->service->deleteAllSharesByUser($principaluri); |
|
| 104 | + } |
|
| 105 | + |
|
| 106 | + /** |
|
| 107 | + * Returns the list of people whom this resource is shared with. |
|
| 108 | + * |
|
| 109 | + * Every element in this array should have the following properties: |
|
| 110 | + * * href - Often a mailto: address |
|
| 111 | + * * commonName - Optional, for example a first + last name |
|
| 112 | + * * status - See the Sabre\CalDAV\SharingPlugin::STATUS_ constants. |
|
| 113 | + * * readOnly - boolean |
|
| 114 | + * |
|
| 115 | + * @param int $resourceId |
|
| 116 | + * @return list<array{href: string, commonName: string, status: int, readOnly: bool, '{http://owncloud.org/ns}principal': string, '{http://owncloud.org/ns}group-share': bool}> |
|
| 117 | + */ |
|
| 118 | + public function getShares(int $resourceId): array { |
|
| 119 | + $cached = $this->shareCache->get((string)$resourceId); |
|
| 120 | + if ($cached) { |
|
| 121 | + return $cached; |
|
| 122 | + } |
|
| 123 | + |
|
| 124 | + $rows = $this->service->getShares($resourceId); |
|
| 125 | + $shares = []; |
|
| 126 | + foreach ($rows as $row) { |
|
| 127 | + $p = $this->principalBackend->getPrincipalByPath($row['principaluri']); |
|
| 128 | + $shares[] = [ |
|
| 129 | + 'href' => "principal:{$row['principaluri']}", |
|
| 130 | + 'commonName' => isset($p['{DAV:}displayname']) ? (string)$p['{DAV:}displayname'] : '', |
|
| 131 | + 'status' => 1, |
|
| 132 | + 'readOnly' => (int)$row['access'] === Backend::ACCESS_READ, |
|
| 133 | + '{http://owncloud.org/ns}principal' => (string)$row['principaluri'], |
|
| 134 | + '{http://owncloud.org/ns}group-share' => isset($p['uri']) && (str_starts_with($p['uri'], 'principals/groups') || str_starts_with($p['uri'], 'principals/circles')) |
|
| 135 | + ]; |
|
| 136 | + } |
|
| 137 | + $this->shareCache->set((string)$resourceId, $shares); |
|
| 138 | + return $shares; |
|
| 139 | + } |
|
| 140 | + |
|
| 141 | + public function preloadShares(array $resourceIds): void { |
|
| 142 | + $resourceIds = array_filter($resourceIds, function (int $resourceId) { |
|
| 143 | + return empty($this->shareCache->get((string)$resourceId)); |
|
| 144 | + }); |
|
| 145 | + if (empty($resourceIds)) { |
|
| 146 | + return; |
|
| 147 | + } |
|
| 148 | + |
|
| 149 | + $rows = $this->service->getSharesForIds($resourceIds); |
|
| 150 | + $sharesByResource = array_fill_keys($resourceIds, []); |
|
| 151 | + foreach ($rows as $row) { |
|
| 152 | + $resourceId = (int)$row['resourceid']; |
|
| 153 | + $p = $this->principalBackend->getPrincipalByPath($row['principaluri']); |
|
| 154 | + $sharesByResource[$resourceId][] = [ |
|
| 155 | + 'href' => "principal:{$row['principaluri']}", |
|
| 156 | + 'commonName' => isset($p['{DAV:}displayname']) ? (string)$p['{DAV:}displayname'] : '', |
|
| 157 | + 'status' => 1, |
|
| 158 | + 'readOnly' => (int)$row['access'] === self::ACCESS_READ, |
|
| 159 | + '{http://owncloud.org/ns}principal' => (string)$row['principaluri'], |
|
| 160 | + '{http://owncloud.org/ns}group-share' => isset($p['uri']) && str_starts_with($p['uri'], 'principals/groups') |
|
| 161 | + ]; |
|
| 162 | + $this->shareCache->set((string)$resourceId, $sharesByResource[$resourceId]); |
|
| 163 | + } |
|
| 164 | + } |
|
| 165 | + |
|
| 166 | + /** |
|
| 167 | + * For shared resources the sharee is set in the ACL of the resource |
|
| 168 | + * |
|
| 169 | + * @param int $resourceId |
|
| 170 | + * @param list<array{privilege: string, principal: string, protected: bool}> $acl |
|
| 171 | + * @param list<array{href: string, commonName: string, status: int, readOnly: bool, '{http://owncloud.org/ns}principal': string, '{http://owncloud.org/ns}group-share': bool}> $shares |
|
| 172 | + * @return list<array{principal: string, privilege: string, protected: bool}> |
|
| 173 | + */ |
|
| 174 | + public function applyShareAcl(array $shares, array $acl): array { |
|
| 175 | + foreach ($shares as $share) { |
|
| 176 | + $acl[] = [ |
|
| 177 | + 'privilege' => '{DAV:}read', |
|
| 178 | + 'principal' => $share['{' . \OCA\DAV\DAV\Sharing\Plugin::NS_OWNCLOUD . '}principal'], |
|
| 179 | + 'protected' => true, |
|
| 180 | + ]; |
|
| 181 | + if (!$share['readOnly']) { |
|
| 182 | + $acl[] = [ |
|
| 183 | + 'privilege' => '{DAV:}write', |
|
| 184 | + 'principal' => $share['{' . \OCA\DAV\DAV\Sharing\Plugin::NS_OWNCLOUD . '}principal'], |
|
| 185 | + 'protected' => true, |
|
| 186 | + ]; |
|
| 187 | + } elseif (in_array($this->service->getResourceType(), ['calendar','addressbook'])) { |
|
| 188 | + // Allow changing the properties of read only calendars, |
|
| 189 | + // so users can change the visibility. |
|
| 190 | + $acl[] = [ |
|
| 191 | + 'privilege' => '{DAV:}write-properties', |
|
| 192 | + 'principal' => $share['{' . \OCA\DAV\DAV\Sharing\Plugin::NS_OWNCLOUD . '}principal'], |
|
| 193 | + 'protected' => true, |
|
| 194 | + ]; |
|
| 195 | + } |
|
| 196 | + } |
|
| 197 | + return $acl; |
|
| 198 | + } |
|
| 199 | + |
|
| 200 | + public function unshare(IShareable $shareable, string $principalUri): bool { |
|
| 201 | + $this->shareCache->clear(); |
|
| 202 | 202 | |
| 203 | - $principal = $this->principalBackend->findByUri($principalUri, ''); |
|
| 204 | - if (empty($principal)) { |
|
| 205 | - return false; |
|
| 206 | - } |
|
| 207 | - |
|
| 208 | - if ($shareable->getOwner() === $principal) { |
|
| 209 | - return false; |
|
| 210 | - } |
|
| 211 | - |
|
| 212 | - // Delete any possible direct shares (since the frontend does not separate between them) |
|
| 213 | - $this->service->deleteShare($shareable->getResourceId(), $principal); |
|
| 214 | - |
|
| 215 | - $needsUnshare = $this->hasAccessByGroupOrCirclesMembership( |
|
| 216 | - $shareable->getResourceId(), |
|
| 217 | - $principal |
|
| 218 | - ); |
|
| 219 | - |
|
| 220 | - if ($needsUnshare) { |
|
| 221 | - $this->service->unshare($shareable->getResourceId(), $principal); |
|
| 222 | - } |
|
| 223 | - |
|
| 224 | - return true; |
|
| 225 | - } |
|
| 226 | - |
|
| 227 | - private function hasAccessByGroupOrCirclesMembership(int $resourceId, string $principal) { |
|
| 228 | - $memberships = array_merge( |
|
| 229 | - $this->principalBackend->getGroupMembership($principal, true), |
|
| 230 | - $this->principalBackend->getCircleMembership($principal) |
|
| 231 | - ); |
|
| 232 | - |
|
| 233 | - $shares = array_column( |
|
| 234 | - $this->service->getShares($resourceId), |
|
| 235 | - 'principaluri' |
|
| 236 | - ); |
|
| 237 | - |
|
| 238 | - return count(array_intersect($memberships, $shares)) > 0; |
|
| 239 | - } |
|
| 203 | + $principal = $this->principalBackend->findByUri($principalUri, ''); |
|
| 204 | + if (empty($principal)) { |
|
| 205 | + return false; |
|
| 206 | + } |
|
| 207 | + |
|
| 208 | + if ($shareable->getOwner() === $principal) { |
|
| 209 | + return false; |
|
| 210 | + } |
|
| 211 | + |
|
| 212 | + // Delete any possible direct shares (since the frontend does not separate between them) |
|
| 213 | + $this->service->deleteShare($shareable->getResourceId(), $principal); |
|
| 214 | + |
|
| 215 | + $needsUnshare = $this->hasAccessByGroupOrCirclesMembership( |
|
| 216 | + $shareable->getResourceId(), |
|
| 217 | + $principal |
|
| 218 | + ); |
|
| 219 | + |
|
| 220 | + if ($needsUnshare) { |
|
| 221 | + $this->service->unshare($shareable->getResourceId(), $principal); |
|
| 222 | + } |
|
| 223 | + |
|
| 224 | + return true; |
|
| 225 | + } |
|
| 226 | + |
|
| 227 | + private function hasAccessByGroupOrCirclesMembership(int $resourceId, string $principal) { |
|
| 228 | + $memberships = array_merge( |
|
| 229 | + $this->principalBackend->getGroupMembership($principal, true), |
|
| 230 | + $this->principalBackend->getCircleMembership($principal) |
|
| 231 | + ); |
|
| 232 | + |
|
| 233 | + $shares = array_column( |
|
| 234 | + $this->service->getShares($resourceId), |
|
| 235 | + 'principaluri' |
|
| 236 | + ); |
|
| 237 | + |
|
| 238 | + return count(array_intersect($memberships, $shares)) > 0; |
|
| 239 | + } |
|
| 240 | 240 | } |
@@ -19,64 +19,64 @@ |
||
| 19 | 19 | * @psalm-import-type CalendarInfo from \OCA\DAV\CalDAV\CalDavBackend |
| 20 | 20 | */ |
| 21 | 21 | class CalendarShareUpdatedEvent extends Event { |
| 22 | - /** |
|
| 23 | - * CalendarShareUpdatedEvent constructor. |
|
| 24 | - * |
|
| 25 | - * @param int $calendarId |
|
| 26 | - * @psalm-param CalendarInfo $calendarData |
|
| 27 | - * @param array $calendarData |
|
| 28 | - * @param list<array{href: string, commonName: string, status: int, readOnly: bool, '{http://owncloud.org/ns}principal': string, '{http://owncloud.org/ns}group-share': bool}> $oldShares |
|
| 29 | - * @param list<array{href: string, commonName: string, readOnly: bool}> $added |
|
| 30 | - * @param list<string> $removed |
|
| 31 | - * @since 20.0.0 |
|
| 32 | - */ |
|
| 33 | - public function __construct( |
|
| 34 | - private int $calendarId, |
|
| 35 | - private array $calendarData, |
|
| 36 | - private array $oldShares, |
|
| 37 | - private array $added, |
|
| 38 | - private array $removed, |
|
| 39 | - ) { |
|
| 40 | - parent::__construct(); |
|
| 41 | - } |
|
| 22 | + /** |
|
| 23 | + * CalendarShareUpdatedEvent constructor. |
|
| 24 | + * |
|
| 25 | + * @param int $calendarId |
|
| 26 | + * @psalm-param CalendarInfo $calendarData |
|
| 27 | + * @param array $calendarData |
|
| 28 | + * @param list<array{href: string, commonName: string, status: int, readOnly: bool, '{http://owncloud.org/ns}principal': string, '{http://owncloud.org/ns}group-share': bool}> $oldShares |
|
| 29 | + * @param list<array{href: string, commonName: string, readOnly: bool}> $added |
|
| 30 | + * @param list<string> $removed |
|
| 31 | + * @since 20.0.0 |
|
| 32 | + */ |
|
| 33 | + public function __construct( |
|
| 34 | + private int $calendarId, |
|
| 35 | + private array $calendarData, |
|
| 36 | + private array $oldShares, |
|
| 37 | + private array $added, |
|
| 38 | + private array $removed, |
|
| 39 | + ) { |
|
| 40 | + parent::__construct(); |
|
| 41 | + } |
|
| 42 | 42 | |
| 43 | - /** |
|
| 44 | - * @since 20.0.0 |
|
| 45 | - */ |
|
| 46 | - public function getCalendarId(): int { |
|
| 47 | - return $this->calendarId; |
|
| 48 | - } |
|
| 43 | + /** |
|
| 44 | + * @since 20.0.0 |
|
| 45 | + */ |
|
| 46 | + public function getCalendarId(): int { |
|
| 47 | + return $this->calendarId; |
|
| 48 | + } |
|
| 49 | 49 | |
| 50 | - /** |
|
| 51 | - * @psalm-return CalendarInfo |
|
| 52 | - * @return array |
|
| 53 | - * @since 20.0.0 |
|
| 54 | - */ |
|
| 55 | - public function getCalendarData(): array { |
|
| 56 | - return $this->calendarData; |
|
| 57 | - } |
|
| 50 | + /** |
|
| 51 | + * @psalm-return CalendarInfo |
|
| 52 | + * @return array |
|
| 53 | + * @since 20.0.0 |
|
| 54 | + */ |
|
| 55 | + public function getCalendarData(): array { |
|
| 56 | + return $this->calendarData; |
|
| 57 | + } |
|
| 58 | 58 | |
| 59 | - /** |
|
| 60 | - * @return list<array{href: string, commonName: string, status: int, readOnly: bool, '{http://owncloud.org/ns}principal': string, '{http://owncloud.org/ns}group-share': bool}> |
|
| 61 | - * @since 20.0.0 |
|
| 62 | - */ |
|
| 63 | - public function getOldShares(): array { |
|
| 64 | - return $this->oldShares; |
|
| 65 | - } |
|
| 59 | + /** |
|
| 60 | + * @return list<array{href: string, commonName: string, status: int, readOnly: bool, '{http://owncloud.org/ns}principal': string, '{http://owncloud.org/ns}group-share': bool}> |
|
| 61 | + * @since 20.0.0 |
|
| 62 | + */ |
|
| 63 | + public function getOldShares(): array { |
|
| 64 | + return $this->oldShares; |
|
| 65 | + } |
|
| 66 | 66 | |
| 67 | - /** |
|
| 68 | - * @return list<array{href: string, commonName: string, readOnly: bool}> |
|
| 69 | - * @since 20.0.0 |
|
| 70 | - */ |
|
| 71 | - public function getAdded(): array { |
|
| 72 | - return $this->added; |
|
| 73 | - } |
|
| 67 | + /** |
|
| 68 | + * @return list<array{href: string, commonName: string, readOnly: bool}> |
|
| 69 | + * @since 20.0.0 |
|
| 70 | + */ |
|
| 71 | + public function getAdded(): array { |
|
| 72 | + return $this->added; |
|
| 73 | + } |
|
| 74 | 74 | |
| 75 | - /** |
|
| 76 | - * @return list<string> |
|
| 77 | - * @since 20.0.0 |
|
| 78 | - */ |
|
| 79 | - public function getRemoved(): array { |
|
| 80 | - return $this->removed; |
|
| 81 | - } |
|
| 75 | + /** |
|
| 76 | + * @return list<string> |
|
| 77 | + * @since 20.0.0 |
|
| 78 | + */ |
|
| 79 | + public function getRemoved(): array { |
|
| 80 | + return $this->removed; |
|
| 81 | + } |
|
| 82 | 82 | } |
@@ -105,3606 +105,3606 @@ |
||
| 105 | 105 | * } |
| 106 | 106 | */ |
| 107 | 107 | class CalDavBackend extends AbstractBackend implements SyncSupport, SubscriptionSupport, SchedulingSupport { |
| 108 | - use TTransactional; |
|
| 109 | - |
|
| 110 | - public const CALENDAR_TYPE_CALENDAR = 0; |
|
| 111 | - public const CALENDAR_TYPE_SUBSCRIPTION = 1; |
|
| 112 | - |
|
| 113 | - public const PERSONAL_CALENDAR_URI = 'personal'; |
|
| 114 | - public const PERSONAL_CALENDAR_NAME = 'Personal'; |
|
| 115 | - |
|
| 116 | - public const RESOURCE_BOOKING_CALENDAR_URI = 'calendar'; |
|
| 117 | - public const RESOURCE_BOOKING_CALENDAR_NAME = 'Calendar'; |
|
| 118 | - |
|
| 119 | - /** |
|
| 120 | - * We need to specify a max date, because we need to stop *somewhere* |
|
| 121 | - * |
|
| 122 | - * On 32 bit system the maximum for a signed integer is 2147483647, so |
|
| 123 | - * MAX_DATE cannot be higher than date('Y-m-d', 2147483647) which results |
|
| 124 | - * in 2038-01-19 to avoid problems when the date is converted |
|
| 125 | - * to a unix timestamp. |
|
| 126 | - */ |
|
| 127 | - public const MAX_DATE = '2038-01-01'; |
|
| 128 | - |
|
| 129 | - public const ACCESS_PUBLIC = 4; |
|
| 130 | - public const CLASSIFICATION_PUBLIC = 0; |
|
| 131 | - public const CLASSIFICATION_PRIVATE = 1; |
|
| 132 | - public const CLASSIFICATION_CONFIDENTIAL = 2; |
|
| 133 | - |
|
| 134 | - /** |
|
| 135 | - * List of CalDAV properties, and how they map to database field names and their type |
|
| 136 | - * Add your own properties by simply adding on to this array. |
|
| 137 | - * |
|
| 138 | - * @var array |
|
| 139 | - * @psalm-var array<string, string[]> |
|
| 140 | - */ |
|
| 141 | - public array $propertyMap = [ |
|
| 142 | - '{DAV:}displayname' => ['displayname', 'string'], |
|
| 143 | - '{urn:ietf:params:xml:ns:caldav}calendar-description' => ['description', 'string'], |
|
| 144 | - '{urn:ietf:params:xml:ns:caldav}calendar-timezone' => ['timezone', 'string'], |
|
| 145 | - '{http://apple.com/ns/ical/}calendar-order' => ['calendarorder', 'int'], |
|
| 146 | - '{http://apple.com/ns/ical/}calendar-color' => ['calendarcolor', 'string'], |
|
| 147 | - '{' . \OCA\DAV\DAV\Sharing\Plugin::NS_NEXTCLOUD . '}deleted-at' => ['deleted_at', 'int'], |
|
| 148 | - ]; |
|
| 149 | - |
|
| 150 | - /** |
|
| 151 | - * List of subscription properties, and how they map to database field names. |
|
| 152 | - * |
|
| 153 | - * @var array |
|
| 154 | - */ |
|
| 155 | - public array $subscriptionPropertyMap = [ |
|
| 156 | - '{DAV:}displayname' => ['displayname', 'string'], |
|
| 157 | - '{http://apple.com/ns/ical/}refreshrate' => ['refreshrate', 'string'], |
|
| 158 | - '{http://apple.com/ns/ical/}calendar-order' => ['calendarorder', 'int'], |
|
| 159 | - '{http://apple.com/ns/ical/}calendar-color' => ['calendarcolor', 'string'], |
|
| 160 | - '{http://calendarserver.org/ns/}subscribed-strip-todos' => ['striptodos', 'bool'], |
|
| 161 | - '{http://calendarserver.org/ns/}subscribed-strip-alarms' => ['stripalarms', 'string'], |
|
| 162 | - '{http://calendarserver.org/ns/}subscribed-strip-attachments' => ['stripattachments', 'string'], |
|
| 163 | - ]; |
|
| 164 | - |
|
| 165 | - /** |
|
| 166 | - * properties to index |
|
| 167 | - * |
|
| 168 | - * This list has to be kept in sync with ICalendarQuery::SEARCH_PROPERTY_* |
|
| 169 | - * |
|
| 170 | - * @see \OCP\Calendar\ICalendarQuery |
|
| 171 | - */ |
|
| 172 | - private const INDEXED_PROPERTIES = [ |
|
| 173 | - 'CATEGORIES', |
|
| 174 | - 'COMMENT', |
|
| 175 | - 'DESCRIPTION', |
|
| 176 | - 'LOCATION', |
|
| 177 | - 'RESOURCES', |
|
| 178 | - 'STATUS', |
|
| 179 | - 'SUMMARY', |
|
| 180 | - 'ATTENDEE', |
|
| 181 | - 'CONTACT', |
|
| 182 | - 'ORGANIZER' |
|
| 183 | - ]; |
|
| 184 | - |
|
| 185 | - /** @var array parameters to index */ |
|
| 186 | - public static array $indexParameters = [ |
|
| 187 | - 'ATTENDEE' => ['CN'], |
|
| 188 | - 'ORGANIZER' => ['CN'], |
|
| 189 | - ]; |
|
| 190 | - |
|
| 191 | - /** |
|
| 192 | - * @var string[] Map of uid => display name |
|
| 193 | - */ |
|
| 194 | - protected array $userDisplayNames; |
|
| 195 | - |
|
| 196 | - private string $dbObjectsTable = 'calendarobjects'; |
|
| 197 | - private string $dbObjectPropertiesTable = 'calendarobjects_props'; |
|
| 198 | - private string $dbObjectInvitationsTable = 'calendar_invitations'; |
|
| 199 | - private array $cachedObjects = []; |
|
| 200 | - |
|
| 201 | - public function __construct( |
|
| 202 | - private IDBConnection $db, |
|
| 203 | - private Principal $principalBackend, |
|
| 204 | - private IUserManager $userManager, |
|
| 205 | - private ISecureRandom $random, |
|
| 206 | - private LoggerInterface $logger, |
|
| 207 | - private IEventDispatcher $dispatcher, |
|
| 208 | - private IConfig $config, |
|
| 209 | - private Sharing\Backend $calendarSharingBackend, |
|
| 210 | - private bool $legacyEndpoint = false, |
|
| 211 | - ) { |
|
| 212 | - } |
|
| 213 | - |
|
| 214 | - /** |
|
| 215 | - * Return the number of calendars for a principal |
|
| 216 | - * |
|
| 217 | - * By default this excludes the automatically generated birthday calendar |
|
| 218 | - * |
|
| 219 | - * @param $principalUri |
|
| 220 | - * @param bool $excludeBirthday |
|
| 221 | - * @return int |
|
| 222 | - */ |
|
| 223 | - public function getCalendarsForUserCount($principalUri, $excludeBirthday = true) { |
|
| 224 | - $principalUri = $this->convertPrincipal($principalUri, true); |
|
| 225 | - $query = $this->db->getQueryBuilder(); |
|
| 226 | - $query->select($query->func()->count('*')) |
|
| 227 | - ->from('calendars'); |
|
| 228 | - |
|
| 229 | - if ($principalUri === '') { |
|
| 230 | - $query->where($query->expr()->emptyString('principaluri')); |
|
| 231 | - } else { |
|
| 232 | - $query->where($query->expr()->eq('principaluri', $query->createNamedParameter($principalUri))); |
|
| 233 | - } |
|
| 234 | - |
|
| 235 | - if ($excludeBirthday) { |
|
| 236 | - $query->andWhere($query->expr()->neq('uri', $query->createNamedParameter(BirthdayService::BIRTHDAY_CALENDAR_URI))); |
|
| 237 | - } |
|
| 238 | - |
|
| 239 | - $result = $query->executeQuery(); |
|
| 240 | - $column = (int)$result->fetchOne(); |
|
| 241 | - $result->closeCursor(); |
|
| 242 | - return $column; |
|
| 243 | - } |
|
| 244 | - |
|
| 245 | - /** |
|
| 246 | - * Return the number of subscriptions for a principal |
|
| 247 | - */ |
|
| 248 | - public function getSubscriptionsForUserCount(string $principalUri): int { |
|
| 249 | - $principalUri = $this->convertPrincipal($principalUri, true); |
|
| 250 | - $query = $this->db->getQueryBuilder(); |
|
| 251 | - $query->select($query->func()->count('*')) |
|
| 252 | - ->from('calendarsubscriptions'); |
|
| 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 | - $result = $query->executeQuery(); |
|
| 261 | - $column = (int)$result->fetchOne(); |
|
| 262 | - $result->closeCursor(); |
|
| 263 | - return $column; |
|
| 264 | - } |
|
| 265 | - |
|
| 266 | - /** |
|
| 267 | - * @return array{id: int, deleted_at: int}[] |
|
| 268 | - */ |
|
| 269 | - public function getDeletedCalendars(int $deletedBefore): array { |
|
| 270 | - $qb = $this->db->getQueryBuilder(); |
|
| 271 | - $qb->select(['id', 'deleted_at']) |
|
| 272 | - ->from('calendars') |
|
| 273 | - ->where($qb->expr()->isNotNull('deleted_at')) |
|
| 274 | - ->andWhere($qb->expr()->lt('deleted_at', $qb->createNamedParameter($deletedBefore))); |
|
| 275 | - $result = $qb->executeQuery(); |
|
| 276 | - $calendars = []; |
|
| 277 | - while (($row = $result->fetch()) !== false) { |
|
| 278 | - $calendars[] = [ |
|
| 279 | - 'id' => (int)$row['id'], |
|
| 280 | - 'deleted_at' => (int)$row['deleted_at'], |
|
| 281 | - ]; |
|
| 282 | - } |
|
| 283 | - $result->closeCursor(); |
|
| 284 | - return $calendars; |
|
| 285 | - } |
|
| 286 | - |
|
| 287 | - /** |
|
| 288 | - * Returns a list of calendars for a principal. |
|
| 289 | - * |
|
| 290 | - * Every project is an array with the following keys: |
|
| 291 | - * * id, a unique id that will be used by other functions to modify the |
|
| 292 | - * calendar. This can be the same as the uri or a database key. |
|
| 293 | - * * uri, which the basename of the uri with which the calendar is |
|
| 294 | - * accessed. |
|
| 295 | - * * principaluri. The owner of the calendar. Almost always the same as |
|
| 296 | - * principalUri passed to this method. |
|
| 297 | - * |
|
| 298 | - * Furthermore it can contain webdav properties in clark notation. A very |
|
| 299 | - * common one is '{DAV:}displayname'. |
|
| 300 | - * |
|
| 301 | - * Many clients also require: |
|
| 302 | - * {urn:ietf:params:xml:ns:caldav}supported-calendar-component-set |
|
| 303 | - * For this property, you can just return an instance of |
|
| 304 | - * Sabre\CalDAV\Property\SupportedCalendarComponentSet. |
|
| 305 | - * |
|
| 306 | - * If you return {http://sabredav.org/ns}read-only and set the value to 1, |
|
| 307 | - * ACL will automatically be put in read-only mode. |
|
| 308 | - * |
|
| 309 | - * @param string $principalUri |
|
| 310 | - * @return array |
|
| 311 | - */ |
|
| 312 | - public function getCalendarsForUser($principalUri) { |
|
| 313 | - return $this->atomic(function () use ($principalUri) { |
|
| 314 | - $principalUriOriginal = $principalUri; |
|
| 315 | - $principalUri = $this->convertPrincipal($principalUri, true); |
|
| 316 | - $fields = array_column($this->propertyMap, 0); |
|
| 317 | - $fields[] = 'id'; |
|
| 318 | - $fields[] = 'uri'; |
|
| 319 | - $fields[] = 'synctoken'; |
|
| 320 | - $fields[] = 'components'; |
|
| 321 | - $fields[] = 'principaluri'; |
|
| 322 | - $fields[] = 'transparent'; |
|
| 323 | - |
|
| 324 | - // Making fields a comma-delimited list |
|
| 325 | - $query = $this->db->getQueryBuilder(); |
|
| 326 | - $query->select($fields) |
|
| 327 | - ->from('calendars') |
|
| 328 | - ->orderBy('calendarorder', 'ASC'); |
|
| 329 | - |
|
| 330 | - if ($principalUri === '') { |
|
| 331 | - $query->where($query->expr()->emptyString('principaluri')); |
|
| 332 | - } else { |
|
| 333 | - $query->where($query->expr()->eq('principaluri', $query->createNamedParameter($principalUri))); |
|
| 334 | - } |
|
| 335 | - |
|
| 336 | - $result = $query->executeQuery(); |
|
| 337 | - |
|
| 338 | - $calendars = []; |
|
| 339 | - while ($row = $result->fetch()) { |
|
| 340 | - $row['principaluri'] = (string)$row['principaluri']; |
|
| 341 | - $components = []; |
|
| 342 | - if ($row['components']) { |
|
| 343 | - $components = explode(',', $row['components']); |
|
| 344 | - } |
|
| 345 | - |
|
| 346 | - $calendar = [ |
|
| 347 | - 'id' => $row['id'], |
|
| 348 | - 'uri' => $row['uri'], |
|
| 349 | - 'principaluri' => $this->convertPrincipal($row['principaluri'], !$this->legacyEndpoint), |
|
| 350 | - '{' . Plugin::NS_CALENDARSERVER . '}getctag' => 'http://sabre.io/ns/sync/' . ($row['synctoken'] ?: '0'), |
|
| 351 | - '{http://sabredav.org/ns}sync-token' => $row['synctoken'] ?: '0', |
|
| 352 | - '{' . Plugin::NS_CALDAV . '}supported-calendar-component-set' => new SupportedCalendarComponentSet($components), |
|
| 353 | - '{' . Plugin::NS_CALDAV . '}schedule-calendar-transp' => new ScheduleCalendarTransp($row['transparent']?'transparent':'opaque'), |
|
| 354 | - '{' . \OCA\DAV\DAV\Sharing\Plugin::NS_OWNCLOUD . '}owner-principal' => $this->convertPrincipal($principalUri, !$this->legacyEndpoint), |
|
| 355 | - ]; |
|
| 356 | - |
|
| 357 | - $calendar = $this->rowToCalendar($row, $calendar); |
|
| 358 | - $calendar = $this->addOwnerPrincipalToCalendar($calendar); |
|
| 359 | - $calendar = $this->addResourceTypeToCalendar($row, $calendar); |
|
| 360 | - |
|
| 361 | - if (!isset($calendars[$calendar['id']])) { |
|
| 362 | - $calendars[$calendar['id']] = $calendar; |
|
| 363 | - } |
|
| 364 | - } |
|
| 365 | - $result->closeCursor(); |
|
| 366 | - |
|
| 367 | - // query for shared calendars |
|
| 368 | - $principals = $this->principalBackend->getGroupMembership($principalUriOriginal, true); |
|
| 369 | - $principals = array_merge($principals, $this->principalBackend->getCircleMembership($principalUriOriginal)); |
|
| 370 | - $principals[] = $principalUri; |
|
| 371 | - |
|
| 372 | - $fields = array_column($this->propertyMap, 0); |
|
| 373 | - $fields = array_map(function (string $field) { |
|
| 374 | - return 'a.' . $field; |
|
| 375 | - }, $fields); |
|
| 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 | - |
|
| 384 | - $select = $this->db->getQueryBuilder(); |
|
| 385 | - $subSelect = $this->db->getQueryBuilder(); |
|
| 386 | - |
|
| 387 | - $subSelect->select('resourceid') |
|
| 388 | - ->from('dav_shares', 'd') |
|
| 389 | - ->where($subSelect->expr()->eq('d.access', $select->createNamedParameter(Backend::ACCESS_UNSHARED, IQueryBuilder::PARAM_INT), IQueryBuilder::PARAM_INT)) |
|
| 390 | - ->andWhere($subSelect->expr()->eq('d.principaluri', $select->createNamedParameter($principalUri, IQueryBuilder::PARAM_STR), IQueryBuilder::PARAM_STR)); |
|
| 391 | - |
|
| 392 | - $select->select($fields) |
|
| 393 | - ->from('dav_shares', 's') |
|
| 394 | - ->join('s', 'calendars', 'a', $select->expr()->eq('s.resourceid', 'a.id', IQueryBuilder::PARAM_INT)) |
|
| 395 | - ->where($select->expr()->in('s.principaluri', $select->createNamedParameter($principals, IQueryBuilder::PARAM_STR_ARRAY), IQueryBuilder::PARAM_STR_ARRAY)) |
|
| 396 | - ->andWhere($select->expr()->eq('s.type', $select->createNamedParameter('calendar', IQueryBuilder::PARAM_STR), IQueryBuilder::PARAM_STR)) |
|
| 397 | - ->andWhere($select->expr()->notIn('a.id', $select->createFunction($subSelect->getSQL()), IQueryBuilder::PARAM_INT_ARRAY)); |
|
| 398 | - |
|
| 399 | - $results = $select->executeQuery(); |
|
| 400 | - |
|
| 401 | - $readOnlyPropertyName = '{' . \OCA\DAV\DAV\Sharing\Plugin::NS_OWNCLOUD . '}read-only'; |
|
| 402 | - while ($row = $results->fetch()) { |
|
| 403 | - $row['principaluri'] = (string)$row['principaluri']; |
|
| 404 | - if ($row['principaluri'] === $principalUri) { |
|
| 405 | - continue; |
|
| 406 | - } |
|
| 407 | - |
|
| 408 | - $readOnly = (int)$row['access'] === Backend::ACCESS_READ; |
|
| 409 | - if (isset($calendars[$row['id']])) { |
|
| 410 | - if ($readOnly) { |
|
| 411 | - // New share can not have more permissions than the old one. |
|
| 412 | - continue; |
|
| 413 | - } |
|
| 414 | - if (isset($calendars[$row['id']][$readOnlyPropertyName]) && |
|
| 415 | - $calendars[$row['id']][$readOnlyPropertyName] === 0) { |
|
| 416 | - // Old share is already read-write, no more permissions can be gained |
|
| 417 | - continue; |
|
| 418 | - } |
|
| 419 | - } |
|
| 420 | - |
|
| 421 | - [, $name] = Uri\split($row['principaluri']); |
|
| 422 | - $uri = $row['uri'] . '_shared_by_' . $name; |
|
| 423 | - $row['displayname'] = $row['displayname'] . ' (' . ($this->userManager->getDisplayName($name) ?? ($name ?? '')) . ')'; |
|
| 424 | - $components = []; |
|
| 425 | - if ($row['components']) { |
|
| 426 | - $components = explode(',', $row['components']); |
|
| 427 | - } |
|
| 428 | - $calendar = [ |
|
| 429 | - 'id' => $row['id'], |
|
| 430 | - 'uri' => $uri, |
|
| 431 | - 'principaluri' => $this->convertPrincipal($principalUri, !$this->legacyEndpoint), |
|
| 432 | - '{' . Plugin::NS_CALENDARSERVER . '}getctag' => 'http://sabre.io/ns/sync/' . ($row['synctoken'] ?: '0'), |
|
| 433 | - '{http://sabredav.org/ns}sync-token' => $row['synctoken'] ?: '0', |
|
| 434 | - '{' . Plugin::NS_CALDAV . '}supported-calendar-component-set' => new SupportedCalendarComponentSet($components), |
|
| 435 | - '{' . Plugin::NS_CALDAV . '}schedule-calendar-transp' => new ScheduleCalendarTransp('transparent'), |
|
| 436 | - '{' . \OCA\DAV\DAV\Sharing\Plugin::NS_OWNCLOUD . '}owner-principal' => $this->convertPrincipal($row['principaluri'], !$this->legacyEndpoint), |
|
| 437 | - $readOnlyPropertyName => $readOnly, |
|
| 438 | - ]; |
|
| 439 | - |
|
| 440 | - $calendar = $this->rowToCalendar($row, $calendar); |
|
| 441 | - $calendar = $this->addOwnerPrincipalToCalendar($calendar); |
|
| 442 | - $calendar = $this->addResourceTypeToCalendar($row, $calendar); |
|
| 443 | - |
|
| 444 | - $calendars[$calendar['id']] = $calendar; |
|
| 445 | - } |
|
| 446 | - $result->closeCursor(); |
|
| 447 | - |
|
| 448 | - return array_values($calendars); |
|
| 449 | - }, $this->db); |
|
| 450 | - } |
|
| 451 | - |
|
| 452 | - /** |
|
| 453 | - * @param $principalUri |
|
| 454 | - * @return array |
|
| 455 | - */ |
|
| 456 | - public function getUsersOwnCalendars($principalUri) { |
|
| 457 | - $principalUri = $this->convertPrincipal($principalUri, true); |
|
| 458 | - $fields = array_column($this->propertyMap, 0); |
|
| 459 | - $fields[] = 'id'; |
|
| 460 | - $fields[] = 'uri'; |
|
| 461 | - $fields[] = 'synctoken'; |
|
| 462 | - $fields[] = 'components'; |
|
| 463 | - $fields[] = 'principaluri'; |
|
| 464 | - $fields[] = 'transparent'; |
|
| 465 | - // Making fields a comma-delimited list |
|
| 466 | - $query = $this->db->getQueryBuilder(); |
|
| 467 | - $query->select($fields)->from('calendars') |
|
| 468 | - ->where($query->expr()->eq('principaluri', $query->createNamedParameter($principalUri))) |
|
| 469 | - ->orderBy('calendarorder', 'ASC'); |
|
| 470 | - $stmt = $query->executeQuery(); |
|
| 471 | - $calendars = []; |
|
| 472 | - while ($row = $stmt->fetch()) { |
|
| 473 | - $row['principaluri'] = (string)$row['principaluri']; |
|
| 474 | - $components = []; |
|
| 475 | - if ($row['components']) { |
|
| 476 | - $components = explode(',', $row['components']); |
|
| 477 | - } |
|
| 478 | - $calendar = [ |
|
| 479 | - 'id' => $row['id'], |
|
| 480 | - 'uri' => $row['uri'], |
|
| 481 | - 'principaluri' => $this->convertPrincipal($row['principaluri'], !$this->legacyEndpoint), |
|
| 482 | - '{' . Plugin::NS_CALENDARSERVER . '}getctag' => 'http://sabre.io/ns/sync/' . ($row['synctoken'] ?: '0'), |
|
| 483 | - '{http://sabredav.org/ns}sync-token' => $row['synctoken'] ?: '0', |
|
| 484 | - '{' . Plugin::NS_CALDAV . '}supported-calendar-component-set' => new SupportedCalendarComponentSet($components), |
|
| 485 | - '{' . Plugin::NS_CALDAV . '}schedule-calendar-transp' => new ScheduleCalendarTransp($row['transparent']?'transparent':'opaque'), |
|
| 486 | - ]; |
|
| 487 | - |
|
| 488 | - $calendar = $this->rowToCalendar($row, $calendar); |
|
| 489 | - $calendar = $this->addOwnerPrincipalToCalendar($calendar); |
|
| 490 | - $calendar = $this->addResourceTypeToCalendar($row, $calendar); |
|
| 491 | - |
|
| 492 | - if (!isset($calendars[$calendar['id']])) { |
|
| 493 | - $calendars[$calendar['id']] = $calendar; |
|
| 494 | - } |
|
| 495 | - } |
|
| 496 | - $stmt->closeCursor(); |
|
| 497 | - return array_values($calendars); |
|
| 498 | - } |
|
| 499 | - |
|
| 500 | - /** |
|
| 501 | - * @return array |
|
| 502 | - */ |
|
| 503 | - public function getPublicCalendars() { |
|
| 504 | - $fields = array_column($this->propertyMap, 0); |
|
| 505 | - $fields[] = 'a.id'; |
|
| 506 | - $fields[] = 'a.uri'; |
|
| 507 | - $fields[] = 'a.synctoken'; |
|
| 508 | - $fields[] = 'a.components'; |
|
| 509 | - $fields[] = 'a.principaluri'; |
|
| 510 | - $fields[] = 'a.transparent'; |
|
| 511 | - $fields[] = 's.access'; |
|
| 512 | - $fields[] = 's.publicuri'; |
|
| 513 | - $calendars = []; |
|
| 514 | - $query = $this->db->getQueryBuilder(); |
|
| 515 | - $result = $query->select($fields) |
|
| 516 | - ->from('dav_shares', 's') |
|
| 517 | - ->join('s', 'calendars', 'a', $query->expr()->eq('s.resourceid', 'a.id')) |
|
| 518 | - ->where($query->expr()->in('s.access', $query->createNamedParameter(self::ACCESS_PUBLIC))) |
|
| 519 | - ->andWhere($query->expr()->eq('s.type', $query->createNamedParameter('calendar'))) |
|
| 520 | - ->executeQuery(); |
|
| 521 | - |
|
| 522 | - while ($row = $result->fetch()) { |
|
| 523 | - $row['principaluri'] = (string)$row['principaluri']; |
|
| 524 | - [, $name] = Uri\split($row['principaluri']); |
|
| 525 | - $row['displayname'] = $row['displayname'] . "($name)"; |
|
| 526 | - $components = []; |
|
| 527 | - if ($row['components']) { |
|
| 528 | - $components = explode(',', $row['components']); |
|
| 529 | - } |
|
| 530 | - $calendar = [ |
|
| 531 | - 'id' => $row['id'], |
|
| 532 | - 'uri' => $row['publicuri'], |
|
| 533 | - 'principaluri' => $this->convertPrincipal($row['principaluri'], !$this->legacyEndpoint), |
|
| 534 | - '{' . Plugin::NS_CALENDARSERVER . '}getctag' => 'http://sabre.io/ns/sync/' . ($row['synctoken'] ?: '0'), |
|
| 535 | - '{http://sabredav.org/ns}sync-token' => $row['synctoken'] ?: '0', |
|
| 536 | - '{' . Plugin::NS_CALDAV . '}supported-calendar-component-set' => new SupportedCalendarComponentSet($components), |
|
| 537 | - '{' . Plugin::NS_CALDAV . '}schedule-calendar-transp' => new ScheduleCalendarTransp($row['transparent']?'transparent':'opaque'), |
|
| 538 | - '{' . \OCA\DAV\DAV\Sharing\Plugin::NS_OWNCLOUD . '}owner-principal' => $this->convertPrincipal($row['principaluri'], $this->legacyEndpoint), |
|
| 539 | - '{' . \OCA\DAV\DAV\Sharing\Plugin::NS_OWNCLOUD . '}read-only' => (int)$row['access'] === Backend::ACCESS_READ, |
|
| 540 | - '{' . \OCA\DAV\DAV\Sharing\Plugin::NS_OWNCLOUD . '}public' => (int)$row['access'] === self::ACCESS_PUBLIC, |
|
| 541 | - ]; |
|
| 542 | - |
|
| 543 | - $calendar = $this->rowToCalendar($row, $calendar); |
|
| 544 | - $calendar = $this->addOwnerPrincipalToCalendar($calendar); |
|
| 545 | - $calendar = $this->addResourceTypeToCalendar($row, $calendar); |
|
| 546 | - |
|
| 547 | - if (!isset($calendars[$calendar['id']])) { |
|
| 548 | - $calendars[$calendar['id']] = $calendar; |
|
| 549 | - } |
|
| 550 | - } |
|
| 551 | - $result->closeCursor(); |
|
| 552 | - |
|
| 553 | - return array_values($calendars); |
|
| 554 | - } |
|
| 555 | - |
|
| 556 | - /** |
|
| 557 | - * @param string $uri |
|
| 558 | - * @return array |
|
| 559 | - * @throws NotFound |
|
| 560 | - */ |
|
| 561 | - public function getPublicCalendar($uri) { |
|
| 562 | - $fields = array_column($this->propertyMap, 0); |
|
| 563 | - $fields[] = 'a.id'; |
|
| 564 | - $fields[] = 'a.uri'; |
|
| 565 | - $fields[] = 'a.synctoken'; |
|
| 566 | - $fields[] = 'a.components'; |
|
| 567 | - $fields[] = 'a.principaluri'; |
|
| 568 | - $fields[] = 'a.transparent'; |
|
| 569 | - $fields[] = 's.access'; |
|
| 570 | - $fields[] = 's.publicuri'; |
|
| 571 | - $query = $this->db->getQueryBuilder(); |
|
| 572 | - $result = $query->select($fields) |
|
| 573 | - ->from('dav_shares', 's') |
|
| 574 | - ->join('s', 'calendars', 'a', $query->expr()->eq('s.resourceid', 'a.id')) |
|
| 575 | - ->where($query->expr()->in('s.access', $query->createNamedParameter(self::ACCESS_PUBLIC))) |
|
| 576 | - ->andWhere($query->expr()->eq('s.type', $query->createNamedParameter('calendar'))) |
|
| 577 | - ->andWhere($query->expr()->eq('s.publicuri', $query->createNamedParameter($uri))) |
|
| 578 | - ->executeQuery(); |
|
| 579 | - |
|
| 580 | - $row = $result->fetch(); |
|
| 581 | - |
|
| 582 | - $result->closeCursor(); |
|
| 583 | - |
|
| 584 | - if ($row === false) { |
|
| 585 | - throw new NotFound('Node with name \'' . $uri . '\' could not be found'); |
|
| 586 | - } |
|
| 587 | - |
|
| 588 | - $row['principaluri'] = (string)$row['principaluri']; |
|
| 589 | - [, $name] = Uri\split($row['principaluri']); |
|
| 590 | - $row['displayname'] = $row['displayname'] . ' ' . "($name)"; |
|
| 591 | - $components = []; |
|
| 592 | - if ($row['components']) { |
|
| 593 | - $components = explode(',', $row['components']); |
|
| 594 | - } |
|
| 595 | - $calendar = [ |
|
| 596 | - 'id' => $row['id'], |
|
| 597 | - 'uri' => $row['publicuri'], |
|
| 598 | - 'principaluri' => $this->convertPrincipal($row['principaluri'], !$this->legacyEndpoint), |
|
| 599 | - '{' . Plugin::NS_CALENDARSERVER . '}getctag' => 'http://sabre.io/ns/sync/' . ($row['synctoken'] ?: '0'), |
|
| 600 | - '{http://sabredav.org/ns}sync-token' => $row['synctoken'] ?: '0', |
|
| 601 | - '{' . Plugin::NS_CALDAV . '}supported-calendar-component-set' => new SupportedCalendarComponentSet($components), |
|
| 602 | - '{' . Plugin::NS_CALDAV . '}schedule-calendar-transp' => new ScheduleCalendarTransp($row['transparent']?'transparent':'opaque'), |
|
| 603 | - '{' . \OCA\DAV\DAV\Sharing\Plugin::NS_OWNCLOUD . '}owner-principal' => $this->convertPrincipal($row['principaluri'], !$this->legacyEndpoint), |
|
| 604 | - '{' . \OCA\DAV\DAV\Sharing\Plugin::NS_OWNCLOUD . '}read-only' => (int)$row['access'] === Backend::ACCESS_READ, |
|
| 605 | - '{' . \OCA\DAV\DAV\Sharing\Plugin::NS_OWNCLOUD . '}public' => (int)$row['access'] === self::ACCESS_PUBLIC, |
|
| 606 | - ]; |
|
| 607 | - |
|
| 608 | - $calendar = $this->rowToCalendar($row, $calendar); |
|
| 609 | - $calendar = $this->addOwnerPrincipalToCalendar($calendar); |
|
| 610 | - $calendar = $this->addResourceTypeToCalendar($row, $calendar); |
|
| 611 | - |
|
| 612 | - return $calendar; |
|
| 613 | - } |
|
| 614 | - |
|
| 615 | - /** |
|
| 616 | - * @param string $principal |
|
| 617 | - * @param string $uri |
|
| 618 | - * @return array|null |
|
| 619 | - */ |
|
| 620 | - public function getCalendarByUri($principal, $uri) { |
|
| 621 | - $fields = array_column($this->propertyMap, 0); |
|
| 622 | - $fields[] = 'id'; |
|
| 623 | - $fields[] = 'uri'; |
|
| 624 | - $fields[] = 'synctoken'; |
|
| 625 | - $fields[] = 'components'; |
|
| 626 | - $fields[] = 'principaluri'; |
|
| 627 | - $fields[] = 'transparent'; |
|
| 628 | - |
|
| 629 | - // Making fields a comma-delimited list |
|
| 630 | - $query = $this->db->getQueryBuilder(); |
|
| 631 | - $query->select($fields)->from('calendars') |
|
| 632 | - ->where($query->expr()->eq('uri', $query->createNamedParameter($uri))) |
|
| 633 | - ->andWhere($query->expr()->eq('principaluri', $query->createNamedParameter($principal))) |
|
| 634 | - ->setMaxResults(1); |
|
| 635 | - $stmt = $query->executeQuery(); |
|
| 636 | - |
|
| 637 | - $row = $stmt->fetch(); |
|
| 638 | - $stmt->closeCursor(); |
|
| 639 | - if ($row === false) { |
|
| 640 | - return null; |
|
| 641 | - } |
|
| 642 | - |
|
| 643 | - $row['principaluri'] = (string)$row['principaluri']; |
|
| 644 | - $components = []; |
|
| 645 | - if ($row['components']) { |
|
| 646 | - $components = explode(',', $row['components']); |
|
| 647 | - } |
|
| 648 | - |
|
| 649 | - $calendar = [ |
|
| 650 | - 'id' => $row['id'], |
|
| 651 | - 'uri' => $row['uri'], |
|
| 652 | - 'principaluri' => $this->convertPrincipal($row['principaluri'], !$this->legacyEndpoint), |
|
| 653 | - '{' . Plugin::NS_CALENDARSERVER . '}getctag' => 'http://sabre.io/ns/sync/' . ($row['synctoken'] ?: '0'), |
|
| 654 | - '{http://sabredav.org/ns}sync-token' => $row['synctoken'] ?: '0', |
|
| 655 | - '{' . Plugin::NS_CALDAV . '}supported-calendar-component-set' => new SupportedCalendarComponentSet($components), |
|
| 656 | - '{' . Plugin::NS_CALDAV . '}schedule-calendar-transp' => new ScheduleCalendarTransp($row['transparent']?'transparent':'opaque'), |
|
| 657 | - ]; |
|
| 658 | - |
|
| 659 | - $calendar = $this->rowToCalendar($row, $calendar); |
|
| 660 | - $calendar = $this->addOwnerPrincipalToCalendar($calendar); |
|
| 661 | - $calendar = $this->addResourceTypeToCalendar($row, $calendar); |
|
| 662 | - |
|
| 663 | - return $calendar; |
|
| 664 | - } |
|
| 665 | - |
|
| 666 | - /** |
|
| 667 | - * @psalm-return CalendarInfo|null |
|
| 668 | - * @return array|null |
|
| 669 | - */ |
|
| 670 | - public function getCalendarById(int $calendarId): ?array { |
|
| 671 | - $fields = array_column($this->propertyMap, 0); |
|
| 672 | - $fields[] = 'id'; |
|
| 673 | - $fields[] = 'uri'; |
|
| 674 | - $fields[] = 'synctoken'; |
|
| 675 | - $fields[] = 'components'; |
|
| 676 | - $fields[] = 'principaluri'; |
|
| 677 | - $fields[] = 'transparent'; |
|
| 678 | - |
|
| 679 | - // Making fields a comma-delimited list |
|
| 680 | - $query = $this->db->getQueryBuilder(); |
|
| 681 | - $query->select($fields)->from('calendars') |
|
| 682 | - ->where($query->expr()->eq('id', $query->createNamedParameter($calendarId))) |
|
| 683 | - ->setMaxResults(1); |
|
| 684 | - $stmt = $query->executeQuery(); |
|
| 685 | - |
|
| 686 | - $row = $stmt->fetch(); |
|
| 687 | - $stmt->closeCursor(); |
|
| 688 | - if ($row === false) { |
|
| 689 | - return null; |
|
| 690 | - } |
|
| 691 | - |
|
| 692 | - $row['principaluri'] = (string)$row['principaluri']; |
|
| 693 | - $components = []; |
|
| 694 | - if ($row['components']) { |
|
| 695 | - $components = explode(',', $row['components']); |
|
| 696 | - } |
|
| 697 | - |
|
| 698 | - $calendar = [ |
|
| 699 | - 'id' => $row['id'], |
|
| 700 | - 'uri' => $row['uri'], |
|
| 701 | - 'principaluri' => $this->convertPrincipal($row['principaluri'], !$this->legacyEndpoint), |
|
| 702 | - '{' . Plugin::NS_CALENDARSERVER . '}getctag' => 'http://sabre.io/ns/sync/' . ($row['synctoken'] ?: '0'), |
|
| 703 | - '{http://sabredav.org/ns}sync-token' => $row['synctoken'] ?? 0, |
|
| 704 | - '{' . Plugin::NS_CALDAV . '}supported-calendar-component-set' => new SupportedCalendarComponentSet($components), |
|
| 705 | - '{' . Plugin::NS_CALDAV . '}schedule-calendar-transp' => new ScheduleCalendarTransp($row['transparent']?'transparent':'opaque'), |
|
| 706 | - ]; |
|
| 707 | - |
|
| 708 | - $calendar = $this->rowToCalendar($row, $calendar); |
|
| 709 | - $calendar = $this->addOwnerPrincipalToCalendar($calendar); |
|
| 710 | - $calendar = $this->addResourceTypeToCalendar($row, $calendar); |
|
| 711 | - |
|
| 712 | - return $calendar; |
|
| 713 | - } |
|
| 714 | - |
|
| 715 | - /** |
|
| 716 | - * @param $subscriptionId |
|
| 717 | - */ |
|
| 718 | - public function getSubscriptionById($subscriptionId) { |
|
| 719 | - $fields = array_column($this->subscriptionPropertyMap, 0); |
|
| 720 | - $fields[] = 'id'; |
|
| 721 | - $fields[] = 'uri'; |
|
| 722 | - $fields[] = 'source'; |
|
| 723 | - $fields[] = 'synctoken'; |
|
| 724 | - $fields[] = 'principaluri'; |
|
| 725 | - $fields[] = 'lastmodified'; |
|
| 726 | - |
|
| 727 | - $query = $this->db->getQueryBuilder(); |
|
| 728 | - $query->select($fields) |
|
| 729 | - ->from('calendarsubscriptions') |
|
| 730 | - ->where($query->expr()->eq('id', $query->createNamedParameter($subscriptionId))) |
|
| 731 | - ->orderBy('calendarorder', 'asc'); |
|
| 732 | - $stmt = $query->executeQuery(); |
|
| 733 | - |
|
| 734 | - $row = $stmt->fetch(); |
|
| 735 | - $stmt->closeCursor(); |
|
| 736 | - if ($row === false) { |
|
| 737 | - return null; |
|
| 738 | - } |
|
| 739 | - |
|
| 740 | - $row['principaluri'] = (string)$row['principaluri']; |
|
| 741 | - $subscription = [ |
|
| 742 | - 'id' => $row['id'], |
|
| 743 | - 'uri' => $row['uri'], |
|
| 744 | - 'principaluri' => $row['principaluri'], |
|
| 745 | - 'source' => $row['source'], |
|
| 746 | - 'lastmodified' => $row['lastmodified'], |
|
| 747 | - '{' . Plugin::NS_CALDAV . '}supported-calendar-component-set' => new SupportedCalendarComponentSet(['VTODO', 'VEVENT']), |
|
| 748 | - '{http://sabredav.org/ns}sync-token' => $row['synctoken'] ?: '0', |
|
| 749 | - ]; |
|
| 750 | - |
|
| 751 | - return $this->rowToSubscription($row, $subscription); |
|
| 752 | - } |
|
| 753 | - |
|
| 754 | - public function getSubscriptionByUri(string $principal, string $uri): ?array { |
|
| 755 | - $fields = array_column($this->subscriptionPropertyMap, 0); |
|
| 756 | - $fields[] = 'id'; |
|
| 757 | - $fields[] = 'uri'; |
|
| 758 | - $fields[] = 'source'; |
|
| 759 | - $fields[] = 'synctoken'; |
|
| 760 | - $fields[] = 'principaluri'; |
|
| 761 | - $fields[] = 'lastmodified'; |
|
| 762 | - |
|
| 763 | - $query = $this->db->getQueryBuilder(); |
|
| 764 | - $query->select($fields) |
|
| 765 | - ->from('calendarsubscriptions') |
|
| 766 | - ->where($query->expr()->eq('uri', $query->createNamedParameter($uri))) |
|
| 767 | - ->andWhere($query->expr()->eq('principaluri', $query->createNamedParameter($principal))) |
|
| 768 | - ->setMaxResults(1); |
|
| 769 | - $stmt = $query->executeQuery(); |
|
| 770 | - |
|
| 771 | - $row = $stmt->fetch(); |
|
| 772 | - $stmt->closeCursor(); |
|
| 773 | - if ($row === false) { |
|
| 774 | - return null; |
|
| 775 | - } |
|
| 776 | - |
|
| 777 | - $row['principaluri'] = (string)$row['principaluri']; |
|
| 778 | - $subscription = [ |
|
| 779 | - 'id' => $row['id'], |
|
| 780 | - 'uri' => $row['uri'], |
|
| 781 | - 'principaluri' => $row['principaluri'], |
|
| 782 | - 'source' => $row['source'], |
|
| 783 | - 'lastmodified' => $row['lastmodified'], |
|
| 784 | - '{' . Plugin::NS_CALDAV . '}supported-calendar-component-set' => new SupportedCalendarComponentSet(['VTODO', 'VEVENT']), |
|
| 785 | - '{http://sabredav.org/ns}sync-token' => $row['synctoken'] ?: '0', |
|
| 786 | - ]; |
|
| 787 | - |
|
| 788 | - return $this->rowToSubscription($row, $subscription); |
|
| 789 | - } |
|
| 790 | - |
|
| 791 | - /** |
|
| 792 | - * Creates a new calendar for a principal. |
|
| 793 | - * |
|
| 794 | - * If the creation was a success, an id must be returned that can be used to reference |
|
| 795 | - * this calendar in other methods, such as updateCalendar. |
|
| 796 | - * |
|
| 797 | - * @param string $principalUri |
|
| 798 | - * @param string $calendarUri |
|
| 799 | - * @param array $properties |
|
| 800 | - * @return int |
|
| 801 | - * |
|
| 802 | - * @throws CalendarException |
|
| 803 | - */ |
|
| 804 | - public function createCalendar($principalUri, $calendarUri, array $properties) { |
|
| 805 | - if (strlen($calendarUri) > 255) { |
|
| 806 | - throw new CalendarException('URI too long. Calendar not created'); |
|
| 807 | - } |
|
| 808 | - |
|
| 809 | - $values = [ |
|
| 810 | - 'principaluri' => $this->convertPrincipal($principalUri, true), |
|
| 811 | - 'uri' => $calendarUri, |
|
| 812 | - 'synctoken' => 1, |
|
| 813 | - 'transparent' => 0, |
|
| 814 | - 'components' => 'VEVENT,VTODO,VJOURNAL', |
|
| 815 | - 'displayname' => $calendarUri |
|
| 816 | - ]; |
|
| 817 | - |
|
| 818 | - // Default value |
|
| 819 | - $sccs = '{urn:ietf:params:xml:ns:caldav}supported-calendar-component-set'; |
|
| 820 | - if (isset($properties[$sccs])) { |
|
| 821 | - if (!($properties[$sccs] instanceof SupportedCalendarComponentSet)) { |
|
| 822 | - throw new DAV\Exception('The ' . $sccs . ' property must be of type: \Sabre\CalDAV\Property\SupportedCalendarComponentSet'); |
|
| 823 | - } |
|
| 824 | - $values['components'] = implode(',', $properties[$sccs]->getValue()); |
|
| 825 | - } elseif (isset($properties['components'])) { |
|
| 826 | - // Allow to provide components internally without having |
|
| 827 | - // to create a SupportedCalendarComponentSet object |
|
| 828 | - $values['components'] = $properties['components']; |
|
| 829 | - } |
|
| 830 | - |
|
| 831 | - $transp = '{' . Plugin::NS_CALDAV . '}schedule-calendar-transp'; |
|
| 832 | - if (isset($properties[$transp])) { |
|
| 833 | - $values['transparent'] = (int)($properties[$transp]->getValue() === 'transparent'); |
|
| 834 | - } |
|
| 835 | - |
|
| 836 | - foreach ($this->propertyMap as $xmlName => [$dbName, $type]) { |
|
| 837 | - if (isset($properties[$xmlName])) { |
|
| 838 | - $values[$dbName] = $properties[$xmlName]; |
|
| 839 | - } |
|
| 840 | - } |
|
| 841 | - |
|
| 842 | - [$calendarId, $calendarData] = $this->atomic(function () use ($values) { |
|
| 843 | - $query = $this->db->getQueryBuilder(); |
|
| 844 | - $query->insert('calendars'); |
|
| 845 | - foreach ($values as $column => $value) { |
|
| 846 | - $query->setValue($column, $query->createNamedParameter($value)); |
|
| 847 | - } |
|
| 848 | - $query->executeStatement(); |
|
| 849 | - $calendarId = $query->getLastInsertId(); |
|
| 850 | - |
|
| 851 | - $calendarData = $this->getCalendarById($calendarId); |
|
| 852 | - return [$calendarId, $calendarData]; |
|
| 853 | - }, $this->db); |
|
| 854 | - |
|
| 855 | - $this->dispatcher->dispatchTyped(new CalendarCreatedEvent((int)$calendarId, $calendarData)); |
|
| 856 | - |
|
| 857 | - return $calendarId; |
|
| 858 | - } |
|
| 859 | - |
|
| 860 | - /** |
|
| 861 | - * Updates properties for a calendar. |
|
| 862 | - * |
|
| 863 | - * The list of mutations is stored in a Sabre\DAV\PropPatch object. |
|
| 864 | - * To do the actual updates, you must tell this object which properties |
|
| 865 | - * you're going to process with the handle() method. |
|
| 866 | - * |
|
| 867 | - * Calling the handle method is like telling the PropPatch object "I |
|
| 868 | - * promise I can handle updating this property". |
|
| 869 | - * |
|
| 870 | - * Read the PropPatch documentation for more info and examples. |
|
| 871 | - * |
|
| 872 | - * @param mixed $calendarId |
|
| 873 | - * @param PropPatch $propPatch |
|
| 874 | - * @return void |
|
| 875 | - */ |
|
| 876 | - public function updateCalendar($calendarId, PropPatch $propPatch) { |
|
| 877 | - $supportedProperties = array_keys($this->propertyMap); |
|
| 878 | - $supportedProperties[] = '{' . Plugin::NS_CALDAV . '}schedule-calendar-transp'; |
|
| 879 | - |
|
| 880 | - $propPatch->handle($supportedProperties, function ($mutations) use ($calendarId) { |
|
| 881 | - $newValues = []; |
|
| 882 | - foreach ($mutations as $propertyName => $propertyValue) { |
|
| 883 | - switch ($propertyName) { |
|
| 884 | - case '{' . Plugin::NS_CALDAV . '}schedule-calendar-transp': |
|
| 885 | - $fieldName = 'transparent'; |
|
| 886 | - $newValues[$fieldName] = (int)($propertyValue->getValue() === 'transparent'); |
|
| 887 | - break; |
|
| 888 | - default: |
|
| 889 | - $fieldName = $this->propertyMap[$propertyName][0]; |
|
| 890 | - $newValues[$fieldName] = $propertyValue; |
|
| 891 | - break; |
|
| 892 | - } |
|
| 893 | - } |
|
| 894 | - [$calendarData, $shares] = $this->atomic(function () use ($calendarId, $newValues) { |
|
| 895 | - $query = $this->db->getQueryBuilder(); |
|
| 896 | - $query->update('calendars'); |
|
| 897 | - foreach ($newValues as $fieldName => $value) { |
|
| 898 | - $query->set($fieldName, $query->createNamedParameter($value)); |
|
| 899 | - } |
|
| 900 | - $query->where($query->expr()->eq('id', $query->createNamedParameter($calendarId))); |
|
| 901 | - $query->executeStatement(); |
|
| 902 | - |
|
| 903 | - $this->addChanges($calendarId, [''], 2); |
|
| 904 | - |
|
| 905 | - $calendarData = $this->getCalendarById($calendarId); |
|
| 906 | - $shares = $this->getShares($calendarId); |
|
| 907 | - return [$calendarData, $shares]; |
|
| 908 | - }, $this->db); |
|
| 909 | - |
|
| 910 | - $this->dispatcher->dispatchTyped(new CalendarUpdatedEvent($calendarId, $calendarData, $shares, $mutations)); |
|
| 911 | - |
|
| 912 | - return true; |
|
| 913 | - }); |
|
| 914 | - } |
|
| 915 | - |
|
| 916 | - /** |
|
| 917 | - * Delete a calendar and all it's objects |
|
| 918 | - * |
|
| 919 | - * @param mixed $calendarId |
|
| 920 | - * @return void |
|
| 921 | - */ |
|
| 922 | - public function deleteCalendar($calendarId, bool $forceDeletePermanently = false) { |
|
| 923 | - $this->atomic(function () use ($calendarId, $forceDeletePermanently): void { |
|
| 924 | - // The calendar is deleted right away if this is either enforced by the caller |
|
| 925 | - // or the special contacts birthday calendar or when the preference of an empty |
|
| 926 | - // retention (0 seconds) is set, which signals a disabled trashbin. |
|
| 927 | - $calendarData = $this->getCalendarById($calendarId); |
|
| 928 | - $isBirthdayCalendar = isset($calendarData['uri']) && $calendarData['uri'] === BirthdayService::BIRTHDAY_CALENDAR_URI; |
|
| 929 | - $trashbinDisabled = $this->config->getAppValue(Application::APP_ID, RetentionService::RETENTION_CONFIG_KEY) === '0'; |
|
| 930 | - if ($forceDeletePermanently || $isBirthdayCalendar || $trashbinDisabled) { |
|
| 931 | - $calendarData = $this->getCalendarById($calendarId); |
|
| 932 | - $shares = $this->getShares($calendarId); |
|
| 933 | - |
|
| 934 | - $this->purgeCalendarInvitations($calendarId); |
|
| 935 | - |
|
| 936 | - $qbDeleteCalendarObjectProps = $this->db->getQueryBuilder(); |
|
| 937 | - $qbDeleteCalendarObjectProps->delete($this->dbObjectPropertiesTable) |
|
| 938 | - ->where($qbDeleteCalendarObjectProps->expr()->eq('calendarid', $qbDeleteCalendarObjectProps->createNamedParameter($calendarId))) |
|
| 939 | - ->andWhere($qbDeleteCalendarObjectProps->expr()->eq('calendartype', $qbDeleteCalendarObjectProps->createNamedParameter(self::CALENDAR_TYPE_CALENDAR))) |
|
| 940 | - ->executeStatement(); |
|
| 941 | - |
|
| 942 | - $qbDeleteCalendarObjects = $this->db->getQueryBuilder(); |
|
| 943 | - $qbDeleteCalendarObjects->delete('calendarobjects') |
|
| 944 | - ->where($qbDeleteCalendarObjects->expr()->eq('calendarid', $qbDeleteCalendarObjects->createNamedParameter($calendarId))) |
|
| 945 | - ->andWhere($qbDeleteCalendarObjects->expr()->eq('calendartype', $qbDeleteCalendarObjects->createNamedParameter(self::CALENDAR_TYPE_CALENDAR))) |
|
| 946 | - ->executeStatement(); |
|
| 947 | - |
|
| 948 | - $qbDeleteCalendarChanges = $this->db->getQueryBuilder(); |
|
| 949 | - $qbDeleteCalendarChanges->delete('calendarchanges') |
|
| 950 | - ->where($qbDeleteCalendarChanges->expr()->eq('calendarid', $qbDeleteCalendarChanges->createNamedParameter($calendarId))) |
|
| 951 | - ->andWhere($qbDeleteCalendarChanges->expr()->eq('calendartype', $qbDeleteCalendarChanges->createNamedParameter(self::CALENDAR_TYPE_CALENDAR))) |
|
| 952 | - ->executeStatement(); |
|
| 953 | - |
|
| 954 | - $this->calendarSharingBackend->deleteAllShares($calendarId); |
|
| 955 | - |
|
| 956 | - $qbDeleteCalendar = $this->db->getQueryBuilder(); |
|
| 957 | - $qbDeleteCalendar->delete('calendars') |
|
| 958 | - ->where($qbDeleteCalendar->expr()->eq('id', $qbDeleteCalendar->createNamedParameter($calendarId))) |
|
| 959 | - ->executeStatement(); |
|
| 960 | - |
|
| 961 | - // Only dispatch if we actually deleted anything |
|
| 962 | - if ($calendarData) { |
|
| 963 | - $this->dispatcher->dispatchTyped(new CalendarDeletedEvent($calendarId, $calendarData, $shares)); |
|
| 964 | - } |
|
| 965 | - } else { |
|
| 966 | - $qbMarkCalendarDeleted = $this->db->getQueryBuilder(); |
|
| 967 | - $qbMarkCalendarDeleted->update('calendars') |
|
| 968 | - ->set('deleted_at', $qbMarkCalendarDeleted->createNamedParameter(time())) |
|
| 969 | - ->where($qbMarkCalendarDeleted->expr()->eq('id', $qbMarkCalendarDeleted->createNamedParameter($calendarId))) |
|
| 970 | - ->executeStatement(); |
|
| 971 | - |
|
| 972 | - $calendarData = $this->getCalendarById($calendarId); |
|
| 973 | - $shares = $this->getShares($calendarId); |
|
| 974 | - if ($calendarData) { |
|
| 975 | - $this->dispatcher->dispatchTyped(new CalendarMovedToTrashEvent( |
|
| 976 | - $calendarId, |
|
| 977 | - $calendarData, |
|
| 978 | - $shares |
|
| 979 | - )); |
|
| 980 | - } |
|
| 981 | - } |
|
| 982 | - }, $this->db); |
|
| 983 | - } |
|
| 984 | - |
|
| 985 | - public function restoreCalendar(int $id): void { |
|
| 986 | - $this->atomic(function () use ($id): void { |
|
| 987 | - $qb = $this->db->getQueryBuilder(); |
|
| 988 | - $update = $qb->update('calendars') |
|
| 989 | - ->set('deleted_at', $qb->createNamedParameter(null)) |
|
| 990 | - ->where($qb->expr()->eq('id', $qb->createNamedParameter($id, IQueryBuilder::PARAM_INT), IQueryBuilder::PARAM_INT)); |
|
| 991 | - $update->executeStatement(); |
|
| 992 | - |
|
| 993 | - $calendarData = $this->getCalendarById($id); |
|
| 994 | - $shares = $this->getShares($id); |
|
| 995 | - if ($calendarData === null) { |
|
| 996 | - throw new RuntimeException('Calendar data that was just written can\'t be read back. Check your database configuration.'); |
|
| 997 | - } |
|
| 998 | - $this->dispatcher->dispatchTyped(new CalendarRestoredEvent( |
|
| 999 | - $id, |
|
| 1000 | - $calendarData, |
|
| 1001 | - $shares |
|
| 1002 | - )); |
|
| 1003 | - }, $this->db); |
|
| 1004 | - } |
|
| 1005 | - |
|
| 1006 | - /** |
|
| 1007 | - * Returns all calendar entries as a stream of data |
|
| 1008 | - * |
|
| 1009 | - * @since 32.0.0 |
|
| 1010 | - * |
|
| 1011 | - * @return Generator<array> |
|
| 1012 | - */ |
|
| 1013 | - public function exportCalendar(int $calendarId, int $calendarType = self::CALENDAR_TYPE_CALENDAR, ?CalendarExportOptions $options = null): Generator { |
|
| 1014 | - // extract options |
|
| 1015 | - $rangeStart = $options?->getRangeStart(); |
|
| 1016 | - $rangeCount = $options?->getRangeCount(); |
|
| 1017 | - // construct query |
|
| 1018 | - $qb = $this->db->getQueryBuilder(); |
|
| 1019 | - $qb->select('*') |
|
| 1020 | - ->from('calendarobjects') |
|
| 1021 | - ->where($qb->expr()->eq('calendarid', $qb->createNamedParameter($calendarId))) |
|
| 1022 | - ->andWhere($qb->expr()->eq('calendartype', $qb->createNamedParameter($calendarType))) |
|
| 1023 | - ->andWhere($qb->expr()->isNull('deleted_at')); |
|
| 1024 | - if ($rangeStart !== null) { |
|
| 1025 | - $qb->andWhere($qb->expr()->gt('uid', $qb->createNamedParameter($rangeStart))); |
|
| 1026 | - } |
|
| 1027 | - if ($rangeCount !== null) { |
|
| 1028 | - $qb->setMaxResults($rangeCount); |
|
| 1029 | - } |
|
| 1030 | - if ($rangeStart !== null || $rangeCount !== null) { |
|
| 1031 | - $qb->orderBy('uid', 'ASC'); |
|
| 1032 | - } |
|
| 1033 | - $rs = $qb->executeQuery(); |
|
| 1034 | - // iterate through results |
|
| 1035 | - try { |
|
| 1036 | - while (($row = $rs->fetch()) !== false) { |
|
| 1037 | - yield $row; |
|
| 1038 | - } |
|
| 1039 | - } finally { |
|
| 1040 | - $rs->closeCursor(); |
|
| 1041 | - } |
|
| 1042 | - } |
|
| 108 | + use TTransactional; |
|
| 109 | + |
|
| 110 | + public const CALENDAR_TYPE_CALENDAR = 0; |
|
| 111 | + public const CALENDAR_TYPE_SUBSCRIPTION = 1; |
|
| 112 | + |
|
| 113 | + public const PERSONAL_CALENDAR_URI = 'personal'; |
|
| 114 | + public const PERSONAL_CALENDAR_NAME = 'Personal'; |
|
| 115 | + |
|
| 116 | + public const RESOURCE_BOOKING_CALENDAR_URI = 'calendar'; |
|
| 117 | + public const RESOURCE_BOOKING_CALENDAR_NAME = 'Calendar'; |
|
| 118 | + |
|
| 119 | + /** |
|
| 120 | + * We need to specify a max date, because we need to stop *somewhere* |
|
| 121 | + * |
|
| 122 | + * On 32 bit system the maximum for a signed integer is 2147483647, so |
|
| 123 | + * MAX_DATE cannot be higher than date('Y-m-d', 2147483647) which results |
|
| 124 | + * in 2038-01-19 to avoid problems when the date is converted |
|
| 125 | + * to a unix timestamp. |
|
| 126 | + */ |
|
| 127 | + public const MAX_DATE = '2038-01-01'; |
|
| 128 | + |
|
| 129 | + public const ACCESS_PUBLIC = 4; |
|
| 130 | + public const CLASSIFICATION_PUBLIC = 0; |
|
| 131 | + public const CLASSIFICATION_PRIVATE = 1; |
|
| 132 | + public const CLASSIFICATION_CONFIDENTIAL = 2; |
|
| 133 | + |
|
| 134 | + /** |
|
| 135 | + * List of CalDAV properties, and how they map to database field names and their type |
|
| 136 | + * Add your own properties by simply adding on to this array. |
|
| 137 | + * |
|
| 138 | + * @var array |
|
| 139 | + * @psalm-var array<string, string[]> |
|
| 140 | + */ |
|
| 141 | + public array $propertyMap = [ |
|
| 142 | + '{DAV:}displayname' => ['displayname', 'string'], |
|
| 143 | + '{urn:ietf:params:xml:ns:caldav}calendar-description' => ['description', 'string'], |
|
| 144 | + '{urn:ietf:params:xml:ns:caldav}calendar-timezone' => ['timezone', 'string'], |
|
| 145 | + '{http://apple.com/ns/ical/}calendar-order' => ['calendarorder', 'int'], |
|
| 146 | + '{http://apple.com/ns/ical/}calendar-color' => ['calendarcolor', 'string'], |
|
| 147 | + '{' . \OCA\DAV\DAV\Sharing\Plugin::NS_NEXTCLOUD . '}deleted-at' => ['deleted_at', 'int'], |
|
| 148 | + ]; |
|
| 149 | + |
|
| 150 | + /** |
|
| 151 | + * List of subscription properties, and how they map to database field names. |
|
| 152 | + * |
|
| 153 | + * @var array |
|
| 154 | + */ |
|
| 155 | + public array $subscriptionPropertyMap = [ |
|
| 156 | + '{DAV:}displayname' => ['displayname', 'string'], |
|
| 157 | + '{http://apple.com/ns/ical/}refreshrate' => ['refreshrate', 'string'], |
|
| 158 | + '{http://apple.com/ns/ical/}calendar-order' => ['calendarorder', 'int'], |
|
| 159 | + '{http://apple.com/ns/ical/}calendar-color' => ['calendarcolor', 'string'], |
|
| 160 | + '{http://calendarserver.org/ns/}subscribed-strip-todos' => ['striptodos', 'bool'], |
|
| 161 | + '{http://calendarserver.org/ns/}subscribed-strip-alarms' => ['stripalarms', 'string'], |
|
| 162 | + '{http://calendarserver.org/ns/}subscribed-strip-attachments' => ['stripattachments', 'string'], |
|
| 163 | + ]; |
|
| 164 | + |
|
| 165 | + /** |
|
| 166 | + * properties to index |
|
| 167 | + * |
|
| 168 | + * This list has to be kept in sync with ICalendarQuery::SEARCH_PROPERTY_* |
|
| 169 | + * |
|
| 170 | + * @see \OCP\Calendar\ICalendarQuery |
|
| 171 | + */ |
|
| 172 | + private const INDEXED_PROPERTIES = [ |
|
| 173 | + 'CATEGORIES', |
|
| 174 | + 'COMMENT', |
|
| 175 | + 'DESCRIPTION', |
|
| 176 | + 'LOCATION', |
|
| 177 | + 'RESOURCES', |
|
| 178 | + 'STATUS', |
|
| 179 | + 'SUMMARY', |
|
| 180 | + 'ATTENDEE', |
|
| 181 | + 'CONTACT', |
|
| 182 | + 'ORGANIZER' |
|
| 183 | + ]; |
|
| 184 | + |
|
| 185 | + /** @var array parameters to index */ |
|
| 186 | + public static array $indexParameters = [ |
|
| 187 | + 'ATTENDEE' => ['CN'], |
|
| 188 | + 'ORGANIZER' => ['CN'], |
|
| 189 | + ]; |
|
| 190 | + |
|
| 191 | + /** |
|
| 192 | + * @var string[] Map of uid => display name |
|
| 193 | + */ |
|
| 194 | + protected array $userDisplayNames; |
|
| 195 | + |
|
| 196 | + private string $dbObjectsTable = 'calendarobjects'; |
|
| 197 | + private string $dbObjectPropertiesTable = 'calendarobjects_props'; |
|
| 198 | + private string $dbObjectInvitationsTable = 'calendar_invitations'; |
|
| 199 | + private array $cachedObjects = []; |
|
| 200 | + |
|
| 201 | + public function __construct( |
|
| 202 | + private IDBConnection $db, |
|
| 203 | + private Principal $principalBackend, |
|
| 204 | + private IUserManager $userManager, |
|
| 205 | + private ISecureRandom $random, |
|
| 206 | + private LoggerInterface $logger, |
|
| 207 | + private IEventDispatcher $dispatcher, |
|
| 208 | + private IConfig $config, |
|
| 209 | + private Sharing\Backend $calendarSharingBackend, |
|
| 210 | + private bool $legacyEndpoint = false, |
|
| 211 | + ) { |
|
| 212 | + } |
|
| 213 | + |
|
| 214 | + /** |
|
| 215 | + * Return the number of calendars for a principal |
|
| 216 | + * |
|
| 217 | + * By default this excludes the automatically generated birthday calendar |
|
| 218 | + * |
|
| 219 | + * @param $principalUri |
|
| 220 | + * @param bool $excludeBirthday |
|
| 221 | + * @return int |
|
| 222 | + */ |
|
| 223 | + public function getCalendarsForUserCount($principalUri, $excludeBirthday = true) { |
|
| 224 | + $principalUri = $this->convertPrincipal($principalUri, true); |
|
| 225 | + $query = $this->db->getQueryBuilder(); |
|
| 226 | + $query->select($query->func()->count('*')) |
|
| 227 | + ->from('calendars'); |
|
| 228 | + |
|
| 229 | + if ($principalUri === '') { |
|
| 230 | + $query->where($query->expr()->emptyString('principaluri')); |
|
| 231 | + } else { |
|
| 232 | + $query->where($query->expr()->eq('principaluri', $query->createNamedParameter($principalUri))); |
|
| 233 | + } |
|
| 234 | + |
|
| 235 | + if ($excludeBirthday) { |
|
| 236 | + $query->andWhere($query->expr()->neq('uri', $query->createNamedParameter(BirthdayService::BIRTHDAY_CALENDAR_URI))); |
|
| 237 | + } |
|
| 238 | + |
|
| 239 | + $result = $query->executeQuery(); |
|
| 240 | + $column = (int)$result->fetchOne(); |
|
| 241 | + $result->closeCursor(); |
|
| 242 | + return $column; |
|
| 243 | + } |
|
| 244 | + |
|
| 245 | + /** |
|
| 246 | + * Return the number of subscriptions for a principal |
|
| 247 | + */ |
|
| 248 | + public function getSubscriptionsForUserCount(string $principalUri): int { |
|
| 249 | + $principalUri = $this->convertPrincipal($principalUri, true); |
|
| 250 | + $query = $this->db->getQueryBuilder(); |
|
| 251 | + $query->select($query->func()->count('*')) |
|
| 252 | + ->from('calendarsubscriptions'); |
|
| 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 | + $result = $query->executeQuery(); |
|
| 261 | + $column = (int)$result->fetchOne(); |
|
| 262 | + $result->closeCursor(); |
|
| 263 | + return $column; |
|
| 264 | + } |
|
| 265 | + |
|
| 266 | + /** |
|
| 267 | + * @return array{id: int, deleted_at: int}[] |
|
| 268 | + */ |
|
| 269 | + public function getDeletedCalendars(int $deletedBefore): array { |
|
| 270 | + $qb = $this->db->getQueryBuilder(); |
|
| 271 | + $qb->select(['id', 'deleted_at']) |
|
| 272 | + ->from('calendars') |
|
| 273 | + ->where($qb->expr()->isNotNull('deleted_at')) |
|
| 274 | + ->andWhere($qb->expr()->lt('deleted_at', $qb->createNamedParameter($deletedBefore))); |
|
| 275 | + $result = $qb->executeQuery(); |
|
| 276 | + $calendars = []; |
|
| 277 | + while (($row = $result->fetch()) !== false) { |
|
| 278 | + $calendars[] = [ |
|
| 279 | + 'id' => (int)$row['id'], |
|
| 280 | + 'deleted_at' => (int)$row['deleted_at'], |
|
| 281 | + ]; |
|
| 282 | + } |
|
| 283 | + $result->closeCursor(); |
|
| 284 | + return $calendars; |
|
| 285 | + } |
|
| 286 | + |
|
| 287 | + /** |
|
| 288 | + * Returns a list of calendars for a principal. |
|
| 289 | + * |
|
| 290 | + * Every project is an array with the following keys: |
|
| 291 | + * * id, a unique id that will be used by other functions to modify the |
|
| 292 | + * calendar. This can be the same as the uri or a database key. |
|
| 293 | + * * uri, which the basename of the uri with which the calendar is |
|
| 294 | + * accessed. |
|
| 295 | + * * principaluri. The owner of the calendar. Almost always the same as |
|
| 296 | + * principalUri passed to this method. |
|
| 297 | + * |
|
| 298 | + * Furthermore it can contain webdav properties in clark notation. A very |
|
| 299 | + * common one is '{DAV:}displayname'. |
|
| 300 | + * |
|
| 301 | + * Many clients also require: |
|
| 302 | + * {urn:ietf:params:xml:ns:caldav}supported-calendar-component-set |
|
| 303 | + * For this property, you can just return an instance of |
|
| 304 | + * Sabre\CalDAV\Property\SupportedCalendarComponentSet. |
|
| 305 | + * |
|
| 306 | + * If you return {http://sabredav.org/ns}read-only and set the value to 1, |
|
| 307 | + * ACL will automatically be put in read-only mode. |
|
| 308 | + * |
|
| 309 | + * @param string $principalUri |
|
| 310 | + * @return array |
|
| 311 | + */ |
|
| 312 | + public function getCalendarsForUser($principalUri) { |
|
| 313 | + return $this->atomic(function () use ($principalUri) { |
|
| 314 | + $principalUriOriginal = $principalUri; |
|
| 315 | + $principalUri = $this->convertPrincipal($principalUri, true); |
|
| 316 | + $fields = array_column($this->propertyMap, 0); |
|
| 317 | + $fields[] = 'id'; |
|
| 318 | + $fields[] = 'uri'; |
|
| 319 | + $fields[] = 'synctoken'; |
|
| 320 | + $fields[] = 'components'; |
|
| 321 | + $fields[] = 'principaluri'; |
|
| 322 | + $fields[] = 'transparent'; |
|
| 323 | + |
|
| 324 | + // Making fields a comma-delimited list |
|
| 325 | + $query = $this->db->getQueryBuilder(); |
|
| 326 | + $query->select($fields) |
|
| 327 | + ->from('calendars') |
|
| 328 | + ->orderBy('calendarorder', 'ASC'); |
|
| 329 | + |
|
| 330 | + if ($principalUri === '') { |
|
| 331 | + $query->where($query->expr()->emptyString('principaluri')); |
|
| 332 | + } else { |
|
| 333 | + $query->where($query->expr()->eq('principaluri', $query->createNamedParameter($principalUri))); |
|
| 334 | + } |
|
| 335 | + |
|
| 336 | + $result = $query->executeQuery(); |
|
| 337 | + |
|
| 338 | + $calendars = []; |
|
| 339 | + while ($row = $result->fetch()) { |
|
| 340 | + $row['principaluri'] = (string)$row['principaluri']; |
|
| 341 | + $components = []; |
|
| 342 | + if ($row['components']) { |
|
| 343 | + $components = explode(',', $row['components']); |
|
| 344 | + } |
|
| 345 | + |
|
| 346 | + $calendar = [ |
|
| 347 | + 'id' => $row['id'], |
|
| 348 | + 'uri' => $row['uri'], |
|
| 349 | + 'principaluri' => $this->convertPrincipal($row['principaluri'], !$this->legacyEndpoint), |
|
| 350 | + '{' . Plugin::NS_CALENDARSERVER . '}getctag' => 'http://sabre.io/ns/sync/' . ($row['synctoken'] ?: '0'), |
|
| 351 | + '{http://sabredav.org/ns}sync-token' => $row['synctoken'] ?: '0', |
|
| 352 | + '{' . Plugin::NS_CALDAV . '}supported-calendar-component-set' => new SupportedCalendarComponentSet($components), |
|
| 353 | + '{' . Plugin::NS_CALDAV . '}schedule-calendar-transp' => new ScheduleCalendarTransp($row['transparent']?'transparent':'opaque'), |
|
| 354 | + '{' . \OCA\DAV\DAV\Sharing\Plugin::NS_OWNCLOUD . '}owner-principal' => $this->convertPrincipal($principalUri, !$this->legacyEndpoint), |
|
| 355 | + ]; |
|
| 356 | + |
|
| 357 | + $calendar = $this->rowToCalendar($row, $calendar); |
|
| 358 | + $calendar = $this->addOwnerPrincipalToCalendar($calendar); |
|
| 359 | + $calendar = $this->addResourceTypeToCalendar($row, $calendar); |
|
| 360 | + |
|
| 361 | + if (!isset($calendars[$calendar['id']])) { |
|
| 362 | + $calendars[$calendar['id']] = $calendar; |
|
| 363 | + } |
|
| 364 | + } |
|
| 365 | + $result->closeCursor(); |
|
| 366 | + |
|
| 367 | + // query for shared calendars |
|
| 368 | + $principals = $this->principalBackend->getGroupMembership($principalUriOriginal, true); |
|
| 369 | + $principals = array_merge($principals, $this->principalBackend->getCircleMembership($principalUriOriginal)); |
|
| 370 | + $principals[] = $principalUri; |
|
| 371 | + |
|
| 372 | + $fields = array_column($this->propertyMap, 0); |
|
| 373 | + $fields = array_map(function (string $field) { |
|
| 374 | + return 'a.' . $field; |
|
| 375 | + }, $fields); |
|
| 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 | + |
|
| 384 | + $select = $this->db->getQueryBuilder(); |
|
| 385 | + $subSelect = $this->db->getQueryBuilder(); |
|
| 386 | + |
|
| 387 | + $subSelect->select('resourceid') |
|
| 388 | + ->from('dav_shares', 'd') |
|
| 389 | + ->where($subSelect->expr()->eq('d.access', $select->createNamedParameter(Backend::ACCESS_UNSHARED, IQueryBuilder::PARAM_INT), IQueryBuilder::PARAM_INT)) |
|
| 390 | + ->andWhere($subSelect->expr()->eq('d.principaluri', $select->createNamedParameter($principalUri, IQueryBuilder::PARAM_STR), IQueryBuilder::PARAM_STR)); |
|
| 391 | + |
|
| 392 | + $select->select($fields) |
|
| 393 | + ->from('dav_shares', 's') |
|
| 394 | + ->join('s', 'calendars', 'a', $select->expr()->eq('s.resourceid', 'a.id', IQueryBuilder::PARAM_INT)) |
|
| 395 | + ->where($select->expr()->in('s.principaluri', $select->createNamedParameter($principals, IQueryBuilder::PARAM_STR_ARRAY), IQueryBuilder::PARAM_STR_ARRAY)) |
|
| 396 | + ->andWhere($select->expr()->eq('s.type', $select->createNamedParameter('calendar', IQueryBuilder::PARAM_STR), IQueryBuilder::PARAM_STR)) |
|
| 397 | + ->andWhere($select->expr()->notIn('a.id', $select->createFunction($subSelect->getSQL()), IQueryBuilder::PARAM_INT_ARRAY)); |
|
| 398 | + |
|
| 399 | + $results = $select->executeQuery(); |
|
| 400 | + |
|
| 401 | + $readOnlyPropertyName = '{' . \OCA\DAV\DAV\Sharing\Plugin::NS_OWNCLOUD . '}read-only'; |
|
| 402 | + while ($row = $results->fetch()) { |
|
| 403 | + $row['principaluri'] = (string)$row['principaluri']; |
|
| 404 | + if ($row['principaluri'] === $principalUri) { |
|
| 405 | + continue; |
|
| 406 | + } |
|
| 407 | + |
|
| 408 | + $readOnly = (int)$row['access'] === Backend::ACCESS_READ; |
|
| 409 | + if (isset($calendars[$row['id']])) { |
|
| 410 | + if ($readOnly) { |
|
| 411 | + // New share can not have more permissions than the old one. |
|
| 412 | + continue; |
|
| 413 | + } |
|
| 414 | + if (isset($calendars[$row['id']][$readOnlyPropertyName]) && |
|
| 415 | + $calendars[$row['id']][$readOnlyPropertyName] === 0) { |
|
| 416 | + // Old share is already read-write, no more permissions can be gained |
|
| 417 | + continue; |
|
| 418 | + } |
|
| 419 | + } |
|
| 420 | + |
|
| 421 | + [, $name] = Uri\split($row['principaluri']); |
|
| 422 | + $uri = $row['uri'] . '_shared_by_' . $name; |
|
| 423 | + $row['displayname'] = $row['displayname'] . ' (' . ($this->userManager->getDisplayName($name) ?? ($name ?? '')) . ')'; |
|
| 424 | + $components = []; |
|
| 425 | + if ($row['components']) { |
|
| 426 | + $components = explode(',', $row['components']); |
|
| 427 | + } |
|
| 428 | + $calendar = [ |
|
| 429 | + 'id' => $row['id'], |
|
| 430 | + 'uri' => $uri, |
|
| 431 | + 'principaluri' => $this->convertPrincipal($principalUri, !$this->legacyEndpoint), |
|
| 432 | + '{' . Plugin::NS_CALENDARSERVER . '}getctag' => 'http://sabre.io/ns/sync/' . ($row['synctoken'] ?: '0'), |
|
| 433 | + '{http://sabredav.org/ns}sync-token' => $row['synctoken'] ?: '0', |
|
| 434 | + '{' . Plugin::NS_CALDAV . '}supported-calendar-component-set' => new SupportedCalendarComponentSet($components), |
|
| 435 | + '{' . Plugin::NS_CALDAV . '}schedule-calendar-transp' => new ScheduleCalendarTransp('transparent'), |
|
| 436 | + '{' . \OCA\DAV\DAV\Sharing\Plugin::NS_OWNCLOUD . '}owner-principal' => $this->convertPrincipal($row['principaluri'], !$this->legacyEndpoint), |
|
| 437 | + $readOnlyPropertyName => $readOnly, |
|
| 438 | + ]; |
|
| 439 | + |
|
| 440 | + $calendar = $this->rowToCalendar($row, $calendar); |
|
| 441 | + $calendar = $this->addOwnerPrincipalToCalendar($calendar); |
|
| 442 | + $calendar = $this->addResourceTypeToCalendar($row, $calendar); |
|
| 443 | + |
|
| 444 | + $calendars[$calendar['id']] = $calendar; |
|
| 445 | + } |
|
| 446 | + $result->closeCursor(); |
|
| 447 | + |
|
| 448 | + return array_values($calendars); |
|
| 449 | + }, $this->db); |
|
| 450 | + } |
|
| 451 | + |
|
| 452 | + /** |
|
| 453 | + * @param $principalUri |
|
| 454 | + * @return array |
|
| 455 | + */ |
|
| 456 | + public function getUsersOwnCalendars($principalUri) { |
|
| 457 | + $principalUri = $this->convertPrincipal($principalUri, true); |
|
| 458 | + $fields = array_column($this->propertyMap, 0); |
|
| 459 | + $fields[] = 'id'; |
|
| 460 | + $fields[] = 'uri'; |
|
| 461 | + $fields[] = 'synctoken'; |
|
| 462 | + $fields[] = 'components'; |
|
| 463 | + $fields[] = 'principaluri'; |
|
| 464 | + $fields[] = 'transparent'; |
|
| 465 | + // Making fields a comma-delimited list |
|
| 466 | + $query = $this->db->getQueryBuilder(); |
|
| 467 | + $query->select($fields)->from('calendars') |
|
| 468 | + ->where($query->expr()->eq('principaluri', $query->createNamedParameter($principalUri))) |
|
| 469 | + ->orderBy('calendarorder', 'ASC'); |
|
| 470 | + $stmt = $query->executeQuery(); |
|
| 471 | + $calendars = []; |
|
| 472 | + while ($row = $stmt->fetch()) { |
|
| 473 | + $row['principaluri'] = (string)$row['principaluri']; |
|
| 474 | + $components = []; |
|
| 475 | + if ($row['components']) { |
|
| 476 | + $components = explode(',', $row['components']); |
|
| 477 | + } |
|
| 478 | + $calendar = [ |
|
| 479 | + 'id' => $row['id'], |
|
| 480 | + 'uri' => $row['uri'], |
|
| 481 | + 'principaluri' => $this->convertPrincipal($row['principaluri'], !$this->legacyEndpoint), |
|
| 482 | + '{' . Plugin::NS_CALENDARSERVER . '}getctag' => 'http://sabre.io/ns/sync/' . ($row['synctoken'] ?: '0'), |
|
| 483 | + '{http://sabredav.org/ns}sync-token' => $row['synctoken'] ?: '0', |
|
| 484 | + '{' . Plugin::NS_CALDAV . '}supported-calendar-component-set' => new SupportedCalendarComponentSet($components), |
|
| 485 | + '{' . Plugin::NS_CALDAV . '}schedule-calendar-transp' => new ScheduleCalendarTransp($row['transparent']?'transparent':'opaque'), |
|
| 486 | + ]; |
|
| 487 | + |
|
| 488 | + $calendar = $this->rowToCalendar($row, $calendar); |
|
| 489 | + $calendar = $this->addOwnerPrincipalToCalendar($calendar); |
|
| 490 | + $calendar = $this->addResourceTypeToCalendar($row, $calendar); |
|
| 491 | + |
|
| 492 | + if (!isset($calendars[$calendar['id']])) { |
|
| 493 | + $calendars[$calendar['id']] = $calendar; |
|
| 494 | + } |
|
| 495 | + } |
|
| 496 | + $stmt->closeCursor(); |
|
| 497 | + return array_values($calendars); |
|
| 498 | + } |
|
| 499 | + |
|
| 500 | + /** |
|
| 501 | + * @return array |
|
| 502 | + */ |
|
| 503 | + public function getPublicCalendars() { |
|
| 504 | + $fields = array_column($this->propertyMap, 0); |
|
| 505 | + $fields[] = 'a.id'; |
|
| 506 | + $fields[] = 'a.uri'; |
|
| 507 | + $fields[] = 'a.synctoken'; |
|
| 508 | + $fields[] = 'a.components'; |
|
| 509 | + $fields[] = 'a.principaluri'; |
|
| 510 | + $fields[] = 'a.transparent'; |
|
| 511 | + $fields[] = 's.access'; |
|
| 512 | + $fields[] = 's.publicuri'; |
|
| 513 | + $calendars = []; |
|
| 514 | + $query = $this->db->getQueryBuilder(); |
|
| 515 | + $result = $query->select($fields) |
|
| 516 | + ->from('dav_shares', 's') |
|
| 517 | + ->join('s', 'calendars', 'a', $query->expr()->eq('s.resourceid', 'a.id')) |
|
| 518 | + ->where($query->expr()->in('s.access', $query->createNamedParameter(self::ACCESS_PUBLIC))) |
|
| 519 | + ->andWhere($query->expr()->eq('s.type', $query->createNamedParameter('calendar'))) |
|
| 520 | + ->executeQuery(); |
|
| 521 | + |
|
| 522 | + while ($row = $result->fetch()) { |
|
| 523 | + $row['principaluri'] = (string)$row['principaluri']; |
|
| 524 | + [, $name] = Uri\split($row['principaluri']); |
|
| 525 | + $row['displayname'] = $row['displayname'] . "($name)"; |
|
| 526 | + $components = []; |
|
| 527 | + if ($row['components']) { |
|
| 528 | + $components = explode(',', $row['components']); |
|
| 529 | + } |
|
| 530 | + $calendar = [ |
|
| 531 | + 'id' => $row['id'], |
|
| 532 | + 'uri' => $row['publicuri'], |
|
| 533 | + 'principaluri' => $this->convertPrincipal($row['principaluri'], !$this->legacyEndpoint), |
|
| 534 | + '{' . Plugin::NS_CALENDARSERVER . '}getctag' => 'http://sabre.io/ns/sync/' . ($row['synctoken'] ?: '0'), |
|
| 535 | + '{http://sabredav.org/ns}sync-token' => $row['synctoken'] ?: '0', |
|
| 536 | + '{' . Plugin::NS_CALDAV . '}supported-calendar-component-set' => new SupportedCalendarComponentSet($components), |
|
| 537 | + '{' . Plugin::NS_CALDAV . '}schedule-calendar-transp' => new ScheduleCalendarTransp($row['transparent']?'transparent':'opaque'), |
|
| 538 | + '{' . \OCA\DAV\DAV\Sharing\Plugin::NS_OWNCLOUD . '}owner-principal' => $this->convertPrincipal($row['principaluri'], $this->legacyEndpoint), |
|
| 539 | + '{' . \OCA\DAV\DAV\Sharing\Plugin::NS_OWNCLOUD . '}read-only' => (int)$row['access'] === Backend::ACCESS_READ, |
|
| 540 | + '{' . \OCA\DAV\DAV\Sharing\Plugin::NS_OWNCLOUD . '}public' => (int)$row['access'] === self::ACCESS_PUBLIC, |
|
| 541 | + ]; |
|
| 542 | + |
|
| 543 | + $calendar = $this->rowToCalendar($row, $calendar); |
|
| 544 | + $calendar = $this->addOwnerPrincipalToCalendar($calendar); |
|
| 545 | + $calendar = $this->addResourceTypeToCalendar($row, $calendar); |
|
| 546 | + |
|
| 547 | + if (!isset($calendars[$calendar['id']])) { |
|
| 548 | + $calendars[$calendar['id']] = $calendar; |
|
| 549 | + } |
|
| 550 | + } |
|
| 551 | + $result->closeCursor(); |
|
| 552 | + |
|
| 553 | + return array_values($calendars); |
|
| 554 | + } |
|
| 555 | + |
|
| 556 | + /** |
|
| 557 | + * @param string $uri |
|
| 558 | + * @return array |
|
| 559 | + * @throws NotFound |
|
| 560 | + */ |
|
| 561 | + public function getPublicCalendar($uri) { |
|
| 562 | + $fields = array_column($this->propertyMap, 0); |
|
| 563 | + $fields[] = 'a.id'; |
|
| 564 | + $fields[] = 'a.uri'; |
|
| 565 | + $fields[] = 'a.synctoken'; |
|
| 566 | + $fields[] = 'a.components'; |
|
| 567 | + $fields[] = 'a.principaluri'; |
|
| 568 | + $fields[] = 'a.transparent'; |
|
| 569 | + $fields[] = 's.access'; |
|
| 570 | + $fields[] = 's.publicuri'; |
|
| 571 | + $query = $this->db->getQueryBuilder(); |
|
| 572 | + $result = $query->select($fields) |
|
| 573 | + ->from('dav_shares', 's') |
|
| 574 | + ->join('s', 'calendars', 'a', $query->expr()->eq('s.resourceid', 'a.id')) |
|
| 575 | + ->where($query->expr()->in('s.access', $query->createNamedParameter(self::ACCESS_PUBLIC))) |
|
| 576 | + ->andWhere($query->expr()->eq('s.type', $query->createNamedParameter('calendar'))) |
|
| 577 | + ->andWhere($query->expr()->eq('s.publicuri', $query->createNamedParameter($uri))) |
|
| 578 | + ->executeQuery(); |
|
| 579 | + |
|
| 580 | + $row = $result->fetch(); |
|
| 581 | + |
|
| 582 | + $result->closeCursor(); |
|
| 583 | + |
|
| 584 | + if ($row === false) { |
|
| 585 | + throw new NotFound('Node with name \'' . $uri . '\' could not be found'); |
|
| 586 | + } |
|
| 587 | + |
|
| 588 | + $row['principaluri'] = (string)$row['principaluri']; |
|
| 589 | + [, $name] = Uri\split($row['principaluri']); |
|
| 590 | + $row['displayname'] = $row['displayname'] . ' ' . "($name)"; |
|
| 591 | + $components = []; |
|
| 592 | + if ($row['components']) { |
|
| 593 | + $components = explode(',', $row['components']); |
|
| 594 | + } |
|
| 595 | + $calendar = [ |
|
| 596 | + 'id' => $row['id'], |
|
| 597 | + 'uri' => $row['publicuri'], |
|
| 598 | + 'principaluri' => $this->convertPrincipal($row['principaluri'], !$this->legacyEndpoint), |
|
| 599 | + '{' . Plugin::NS_CALENDARSERVER . '}getctag' => 'http://sabre.io/ns/sync/' . ($row['synctoken'] ?: '0'), |
|
| 600 | + '{http://sabredav.org/ns}sync-token' => $row['synctoken'] ?: '0', |
|
| 601 | + '{' . Plugin::NS_CALDAV . '}supported-calendar-component-set' => new SupportedCalendarComponentSet($components), |
|
| 602 | + '{' . Plugin::NS_CALDAV . '}schedule-calendar-transp' => new ScheduleCalendarTransp($row['transparent']?'transparent':'opaque'), |
|
| 603 | + '{' . \OCA\DAV\DAV\Sharing\Plugin::NS_OWNCLOUD . '}owner-principal' => $this->convertPrincipal($row['principaluri'], !$this->legacyEndpoint), |
|
| 604 | + '{' . \OCA\DAV\DAV\Sharing\Plugin::NS_OWNCLOUD . '}read-only' => (int)$row['access'] === Backend::ACCESS_READ, |
|
| 605 | + '{' . \OCA\DAV\DAV\Sharing\Plugin::NS_OWNCLOUD . '}public' => (int)$row['access'] === self::ACCESS_PUBLIC, |
|
| 606 | + ]; |
|
| 607 | + |
|
| 608 | + $calendar = $this->rowToCalendar($row, $calendar); |
|
| 609 | + $calendar = $this->addOwnerPrincipalToCalendar($calendar); |
|
| 610 | + $calendar = $this->addResourceTypeToCalendar($row, $calendar); |
|
| 611 | + |
|
| 612 | + return $calendar; |
|
| 613 | + } |
|
| 614 | + |
|
| 615 | + /** |
|
| 616 | + * @param string $principal |
|
| 617 | + * @param string $uri |
|
| 618 | + * @return array|null |
|
| 619 | + */ |
|
| 620 | + public function getCalendarByUri($principal, $uri) { |
|
| 621 | + $fields = array_column($this->propertyMap, 0); |
|
| 622 | + $fields[] = 'id'; |
|
| 623 | + $fields[] = 'uri'; |
|
| 624 | + $fields[] = 'synctoken'; |
|
| 625 | + $fields[] = 'components'; |
|
| 626 | + $fields[] = 'principaluri'; |
|
| 627 | + $fields[] = 'transparent'; |
|
| 628 | + |
|
| 629 | + // Making fields a comma-delimited list |
|
| 630 | + $query = $this->db->getQueryBuilder(); |
|
| 631 | + $query->select($fields)->from('calendars') |
|
| 632 | + ->where($query->expr()->eq('uri', $query->createNamedParameter($uri))) |
|
| 633 | + ->andWhere($query->expr()->eq('principaluri', $query->createNamedParameter($principal))) |
|
| 634 | + ->setMaxResults(1); |
|
| 635 | + $stmt = $query->executeQuery(); |
|
| 636 | + |
|
| 637 | + $row = $stmt->fetch(); |
|
| 638 | + $stmt->closeCursor(); |
|
| 639 | + if ($row === false) { |
|
| 640 | + return null; |
|
| 641 | + } |
|
| 642 | + |
|
| 643 | + $row['principaluri'] = (string)$row['principaluri']; |
|
| 644 | + $components = []; |
|
| 645 | + if ($row['components']) { |
|
| 646 | + $components = explode(',', $row['components']); |
|
| 647 | + } |
|
| 648 | + |
|
| 649 | + $calendar = [ |
|
| 650 | + 'id' => $row['id'], |
|
| 651 | + 'uri' => $row['uri'], |
|
| 652 | + 'principaluri' => $this->convertPrincipal($row['principaluri'], !$this->legacyEndpoint), |
|
| 653 | + '{' . Plugin::NS_CALENDARSERVER . '}getctag' => 'http://sabre.io/ns/sync/' . ($row['synctoken'] ?: '0'), |
|
| 654 | + '{http://sabredav.org/ns}sync-token' => $row['synctoken'] ?: '0', |
|
| 655 | + '{' . Plugin::NS_CALDAV . '}supported-calendar-component-set' => new SupportedCalendarComponentSet($components), |
|
| 656 | + '{' . Plugin::NS_CALDAV . '}schedule-calendar-transp' => new ScheduleCalendarTransp($row['transparent']?'transparent':'opaque'), |
|
| 657 | + ]; |
|
| 658 | + |
|
| 659 | + $calendar = $this->rowToCalendar($row, $calendar); |
|
| 660 | + $calendar = $this->addOwnerPrincipalToCalendar($calendar); |
|
| 661 | + $calendar = $this->addResourceTypeToCalendar($row, $calendar); |
|
| 662 | + |
|
| 663 | + return $calendar; |
|
| 664 | + } |
|
| 665 | + |
|
| 666 | + /** |
|
| 667 | + * @psalm-return CalendarInfo|null |
|
| 668 | + * @return array|null |
|
| 669 | + */ |
|
| 670 | + public function getCalendarById(int $calendarId): ?array { |
|
| 671 | + $fields = array_column($this->propertyMap, 0); |
|
| 672 | + $fields[] = 'id'; |
|
| 673 | + $fields[] = 'uri'; |
|
| 674 | + $fields[] = 'synctoken'; |
|
| 675 | + $fields[] = 'components'; |
|
| 676 | + $fields[] = 'principaluri'; |
|
| 677 | + $fields[] = 'transparent'; |
|
| 678 | + |
|
| 679 | + // Making fields a comma-delimited list |
|
| 680 | + $query = $this->db->getQueryBuilder(); |
|
| 681 | + $query->select($fields)->from('calendars') |
|
| 682 | + ->where($query->expr()->eq('id', $query->createNamedParameter($calendarId))) |
|
| 683 | + ->setMaxResults(1); |
|
| 684 | + $stmt = $query->executeQuery(); |
|
| 685 | + |
|
| 686 | + $row = $stmt->fetch(); |
|
| 687 | + $stmt->closeCursor(); |
|
| 688 | + if ($row === false) { |
|
| 689 | + return null; |
|
| 690 | + } |
|
| 691 | + |
|
| 692 | + $row['principaluri'] = (string)$row['principaluri']; |
|
| 693 | + $components = []; |
|
| 694 | + if ($row['components']) { |
|
| 695 | + $components = explode(',', $row['components']); |
|
| 696 | + } |
|
| 697 | + |
|
| 698 | + $calendar = [ |
|
| 699 | + 'id' => $row['id'], |
|
| 700 | + 'uri' => $row['uri'], |
|
| 701 | + 'principaluri' => $this->convertPrincipal($row['principaluri'], !$this->legacyEndpoint), |
|
| 702 | + '{' . Plugin::NS_CALENDARSERVER . '}getctag' => 'http://sabre.io/ns/sync/' . ($row['synctoken'] ?: '0'), |
|
| 703 | + '{http://sabredav.org/ns}sync-token' => $row['synctoken'] ?? 0, |
|
| 704 | + '{' . Plugin::NS_CALDAV . '}supported-calendar-component-set' => new SupportedCalendarComponentSet($components), |
|
| 705 | + '{' . Plugin::NS_CALDAV . '}schedule-calendar-transp' => new ScheduleCalendarTransp($row['transparent']?'transparent':'opaque'), |
|
| 706 | + ]; |
|
| 707 | + |
|
| 708 | + $calendar = $this->rowToCalendar($row, $calendar); |
|
| 709 | + $calendar = $this->addOwnerPrincipalToCalendar($calendar); |
|
| 710 | + $calendar = $this->addResourceTypeToCalendar($row, $calendar); |
|
| 711 | + |
|
| 712 | + return $calendar; |
|
| 713 | + } |
|
| 714 | + |
|
| 715 | + /** |
|
| 716 | + * @param $subscriptionId |
|
| 717 | + */ |
|
| 718 | + public function getSubscriptionById($subscriptionId) { |
|
| 719 | + $fields = array_column($this->subscriptionPropertyMap, 0); |
|
| 720 | + $fields[] = 'id'; |
|
| 721 | + $fields[] = 'uri'; |
|
| 722 | + $fields[] = 'source'; |
|
| 723 | + $fields[] = 'synctoken'; |
|
| 724 | + $fields[] = 'principaluri'; |
|
| 725 | + $fields[] = 'lastmodified'; |
|
| 726 | + |
|
| 727 | + $query = $this->db->getQueryBuilder(); |
|
| 728 | + $query->select($fields) |
|
| 729 | + ->from('calendarsubscriptions') |
|
| 730 | + ->where($query->expr()->eq('id', $query->createNamedParameter($subscriptionId))) |
|
| 731 | + ->orderBy('calendarorder', 'asc'); |
|
| 732 | + $stmt = $query->executeQuery(); |
|
| 733 | + |
|
| 734 | + $row = $stmt->fetch(); |
|
| 735 | + $stmt->closeCursor(); |
|
| 736 | + if ($row === false) { |
|
| 737 | + return null; |
|
| 738 | + } |
|
| 739 | + |
|
| 740 | + $row['principaluri'] = (string)$row['principaluri']; |
|
| 741 | + $subscription = [ |
|
| 742 | + 'id' => $row['id'], |
|
| 743 | + 'uri' => $row['uri'], |
|
| 744 | + 'principaluri' => $row['principaluri'], |
|
| 745 | + 'source' => $row['source'], |
|
| 746 | + 'lastmodified' => $row['lastmodified'], |
|
| 747 | + '{' . Plugin::NS_CALDAV . '}supported-calendar-component-set' => new SupportedCalendarComponentSet(['VTODO', 'VEVENT']), |
|
| 748 | + '{http://sabredav.org/ns}sync-token' => $row['synctoken'] ?: '0', |
|
| 749 | + ]; |
|
| 750 | + |
|
| 751 | + return $this->rowToSubscription($row, $subscription); |
|
| 752 | + } |
|
| 753 | + |
|
| 754 | + public function getSubscriptionByUri(string $principal, string $uri): ?array { |
|
| 755 | + $fields = array_column($this->subscriptionPropertyMap, 0); |
|
| 756 | + $fields[] = 'id'; |
|
| 757 | + $fields[] = 'uri'; |
|
| 758 | + $fields[] = 'source'; |
|
| 759 | + $fields[] = 'synctoken'; |
|
| 760 | + $fields[] = 'principaluri'; |
|
| 761 | + $fields[] = 'lastmodified'; |
|
| 762 | + |
|
| 763 | + $query = $this->db->getQueryBuilder(); |
|
| 764 | + $query->select($fields) |
|
| 765 | + ->from('calendarsubscriptions') |
|
| 766 | + ->where($query->expr()->eq('uri', $query->createNamedParameter($uri))) |
|
| 767 | + ->andWhere($query->expr()->eq('principaluri', $query->createNamedParameter($principal))) |
|
| 768 | + ->setMaxResults(1); |
|
| 769 | + $stmt = $query->executeQuery(); |
|
| 770 | + |
|
| 771 | + $row = $stmt->fetch(); |
|
| 772 | + $stmt->closeCursor(); |
|
| 773 | + if ($row === false) { |
|
| 774 | + return null; |
|
| 775 | + } |
|
| 776 | + |
|
| 777 | + $row['principaluri'] = (string)$row['principaluri']; |
|
| 778 | + $subscription = [ |
|
| 779 | + 'id' => $row['id'], |
|
| 780 | + 'uri' => $row['uri'], |
|
| 781 | + 'principaluri' => $row['principaluri'], |
|
| 782 | + 'source' => $row['source'], |
|
| 783 | + 'lastmodified' => $row['lastmodified'], |
|
| 784 | + '{' . Plugin::NS_CALDAV . '}supported-calendar-component-set' => new SupportedCalendarComponentSet(['VTODO', 'VEVENT']), |
|
| 785 | + '{http://sabredav.org/ns}sync-token' => $row['synctoken'] ?: '0', |
|
| 786 | + ]; |
|
| 787 | + |
|
| 788 | + return $this->rowToSubscription($row, $subscription); |
|
| 789 | + } |
|
| 790 | + |
|
| 791 | + /** |
|
| 792 | + * Creates a new calendar for a principal. |
|
| 793 | + * |
|
| 794 | + * If the creation was a success, an id must be returned that can be used to reference |
|
| 795 | + * this calendar in other methods, such as updateCalendar. |
|
| 796 | + * |
|
| 797 | + * @param string $principalUri |
|
| 798 | + * @param string $calendarUri |
|
| 799 | + * @param array $properties |
|
| 800 | + * @return int |
|
| 801 | + * |
|
| 802 | + * @throws CalendarException |
|
| 803 | + */ |
|
| 804 | + public function createCalendar($principalUri, $calendarUri, array $properties) { |
|
| 805 | + if (strlen($calendarUri) > 255) { |
|
| 806 | + throw new CalendarException('URI too long. Calendar not created'); |
|
| 807 | + } |
|
| 808 | + |
|
| 809 | + $values = [ |
|
| 810 | + 'principaluri' => $this->convertPrincipal($principalUri, true), |
|
| 811 | + 'uri' => $calendarUri, |
|
| 812 | + 'synctoken' => 1, |
|
| 813 | + 'transparent' => 0, |
|
| 814 | + 'components' => 'VEVENT,VTODO,VJOURNAL', |
|
| 815 | + 'displayname' => $calendarUri |
|
| 816 | + ]; |
|
| 817 | + |
|
| 818 | + // Default value |
|
| 819 | + $sccs = '{urn:ietf:params:xml:ns:caldav}supported-calendar-component-set'; |
|
| 820 | + if (isset($properties[$sccs])) { |
|
| 821 | + if (!($properties[$sccs] instanceof SupportedCalendarComponentSet)) { |
|
| 822 | + throw new DAV\Exception('The ' . $sccs . ' property must be of type: \Sabre\CalDAV\Property\SupportedCalendarComponentSet'); |
|
| 823 | + } |
|
| 824 | + $values['components'] = implode(',', $properties[$sccs]->getValue()); |
|
| 825 | + } elseif (isset($properties['components'])) { |
|
| 826 | + // Allow to provide components internally without having |
|
| 827 | + // to create a SupportedCalendarComponentSet object |
|
| 828 | + $values['components'] = $properties['components']; |
|
| 829 | + } |
|
| 830 | + |
|
| 831 | + $transp = '{' . Plugin::NS_CALDAV . '}schedule-calendar-transp'; |
|
| 832 | + if (isset($properties[$transp])) { |
|
| 833 | + $values['transparent'] = (int)($properties[$transp]->getValue() === 'transparent'); |
|
| 834 | + } |
|
| 835 | + |
|
| 836 | + foreach ($this->propertyMap as $xmlName => [$dbName, $type]) { |
|
| 837 | + if (isset($properties[$xmlName])) { |
|
| 838 | + $values[$dbName] = $properties[$xmlName]; |
|
| 839 | + } |
|
| 840 | + } |
|
| 841 | + |
|
| 842 | + [$calendarId, $calendarData] = $this->atomic(function () use ($values) { |
|
| 843 | + $query = $this->db->getQueryBuilder(); |
|
| 844 | + $query->insert('calendars'); |
|
| 845 | + foreach ($values as $column => $value) { |
|
| 846 | + $query->setValue($column, $query->createNamedParameter($value)); |
|
| 847 | + } |
|
| 848 | + $query->executeStatement(); |
|
| 849 | + $calendarId = $query->getLastInsertId(); |
|
| 850 | + |
|
| 851 | + $calendarData = $this->getCalendarById($calendarId); |
|
| 852 | + return [$calendarId, $calendarData]; |
|
| 853 | + }, $this->db); |
|
| 854 | + |
|
| 855 | + $this->dispatcher->dispatchTyped(new CalendarCreatedEvent((int)$calendarId, $calendarData)); |
|
| 856 | + |
|
| 857 | + return $calendarId; |
|
| 858 | + } |
|
| 859 | + |
|
| 860 | + /** |
|
| 861 | + * Updates properties for a calendar. |
|
| 862 | + * |
|
| 863 | + * The list of mutations is stored in a Sabre\DAV\PropPatch object. |
|
| 864 | + * To do the actual updates, you must tell this object which properties |
|
| 865 | + * you're going to process with the handle() method. |
|
| 866 | + * |
|
| 867 | + * Calling the handle method is like telling the PropPatch object "I |
|
| 868 | + * promise I can handle updating this property". |
|
| 869 | + * |
|
| 870 | + * Read the PropPatch documentation for more info and examples. |
|
| 871 | + * |
|
| 872 | + * @param mixed $calendarId |
|
| 873 | + * @param PropPatch $propPatch |
|
| 874 | + * @return void |
|
| 875 | + */ |
|
| 876 | + public function updateCalendar($calendarId, PropPatch $propPatch) { |
|
| 877 | + $supportedProperties = array_keys($this->propertyMap); |
|
| 878 | + $supportedProperties[] = '{' . Plugin::NS_CALDAV . '}schedule-calendar-transp'; |
|
| 879 | + |
|
| 880 | + $propPatch->handle($supportedProperties, function ($mutations) use ($calendarId) { |
|
| 881 | + $newValues = []; |
|
| 882 | + foreach ($mutations as $propertyName => $propertyValue) { |
|
| 883 | + switch ($propertyName) { |
|
| 884 | + case '{' . Plugin::NS_CALDAV . '}schedule-calendar-transp': |
|
| 885 | + $fieldName = 'transparent'; |
|
| 886 | + $newValues[$fieldName] = (int)($propertyValue->getValue() === 'transparent'); |
|
| 887 | + break; |
|
| 888 | + default: |
|
| 889 | + $fieldName = $this->propertyMap[$propertyName][0]; |
|
| 890 | + $newValues[$fieldName] = $propertyValue; |
|
| 891 | + break; |
|
| 892 | + } |
|
| 893 | + } |
|
| 894 | + [$calendarData, $shares] = $this->atomic(function () use ($calendarId, $newValues) { |
|
| 895 | + $query = $this->db->getQueryBuilder(); |
|
| 896 | + $query->update('calendars'); |
|
| 897 | + foreach ($newValues as $fieldName => $value) { |
|
| 898 | + $query->set($fieldName, $query->createNamedParameter($value)); |
|
| 899 | + } |
|
| 900 | + $query->where($query->expr()->eq('id', $query->createNamedParameter($calendarId))); |
|
| 901 | + $query->executeStatement(); |
|
| 902 | + |
|
| 903 | + $this->addChanges($calendarId, [''], 2); |
|
| 904 | + |
|
| 905 | + $calendarData = $this->getCalendarById($calendarId); |
|
| 906 | + $shares = $this->getShares($calendarId); |
|
| 907 | + return [$calendarData, $shares]; |
|
| 908 | + }, $this->db); |
|
| 909 | + |
|
| 910 | + $this->dispatcher->dispatchTyped(new CalendarUpdatedEvent($calendarId, $calendarData, $shares, $mutations)); |
|
| 911 | + |
|
| 912 | + return true; |
|
| 913 | + }); |
|
| 914 | + } |
|
| 915 | + |
|
| 916 | + /** |
|
| 917 | + * Delete a calendar and all it's objects |
|
| 918 | + * |
|
| 919 | + * @param mixed $calendarId |
|
| 920 | + * @return void |
|
| 921 | + */ |
|
| 922 | + public function deleteCalendar($calendarId, bool $forceDeletePermanently = false) { |
|
| 923 | + $this->atomic(function () use ($calendarId, $forceDeletePermanently): void { |
|
| 924 | + // The calendar is deleted right away if this is either enforced by the caller |
|
| 925 | + // or the special contacts birthday calendar or when the preference of an empty |
|
| 926 | + // retention (0 seconds) is set, which signals a disabled trashbin. |
|
| 927 | + $calendarData = $this->getCalendarById($calendarId); |
|
| 928 | + $isBirthdayCalendar = isset($calendarData['uri']) && $calendarData['uri'] === BirthdayService::BIRTHDAY_CALENDAR_URI; |
|
| 929 | + $trashbinDisabled = $this->config->getAppValue(Application::APP_ID, RetentionService::RETENTION_CONFIG_KEY) === '0'; |
|
| 930 | + if ($forceDeletePermanently || $isBirthdayCalendar || $trashbinDisabled) { |
|
| 931 | + $calendarData = $this->getCalendarById($calendarId); |
|
| 932 | + $shares = $this->getShares($calendarId); |
|
| 933 | + |
|
| 934 | + $this->purgeCalendarInvitations($calendarId); |
|
| 935 | + |
|
| 936 | + $qbDeleteCalendarObjectProps = $this->db->getQueryBuilder(); |
|
| 937 | + $qbDeleteCalendarObjectProps->delete($this->dbObjectPropertiesTable) |
|
| 938 | + ->where($qbDeleteCalendarObjectProps->expr()->eq('calendarid', $qbDeleteCalendarObjectProps->createNamedParameter($calendarId))) |
|
| 939 | + ->andWhere($qbDeleteCalendarObjectProps->expr()->eq('calendartype', $qbDeleteCalendarObjectProps->createNamedParameter(self::CALENDAR_TYPE_CALENDAR))) |
|
| 940 | + ->executeStatement(); |
|
| 941 | + |
|
| 942 | + $qbDeleteCalendarObjects = $this->db->getQueryBuilder(); |
|
| 943 | + $qbDeleteCalendarObjects->delete('calendarobjects') |
|
| 944 | + ->where($qbDeleteCalendarObjects->expr()->eq('calendarid', $qbDeleteCalendarObjects->createNamedParameter($calendarId))) |
|
| 945 | + ->andWhere($qbDeleteCalendarObjects->expr()->eq('calendartype', $qbDeleteCalendarObjects->createNamedParameter(self::CALENDAR_TYPE_CALENDAR))) |
|
| 946 | + ->executeStatement(); |
|
| 947 | + |
|
| 948 | + $qbDeleteCalendarChanges = $this->db->getQueryBuilder(); |
|
| 949 | + $qbDeleteCalendarChanges->delete('calendarchanges') |
|
| 950 | + ->where($qbDeleteCalendarChanges->expr()->eq('calendarid', $qbDeleteCalendarChanges->createNamedParameter($calendarId))) |
|
| 951 | + ->andWhere($qbDeleteCalendarChanges->expr()->eq('calendartype', $qbDeleteCalendarChanges->createNamedParameter(self::CALENDAR_TYPE_CALENDAR))) |
|
| 952 | + ->executeStatement(); |
|
| 953 | + |
|
| 954 | + $this->calendarSharingBackend->deleteAllShares($calendarId); |
|
| 955 | + |
|
| 956 | + $qbDeleteCalendar = $this->db->getQueryBuilder(); |
|
| 957 | + $qbDeleteCalendar->delete('calendars') |
|
| 958 | + ->where($qbDeleteCalendar->expr()->eq('id', $qbDeleteCalendar->createNamedParameter($calendarId))) |
|
| 959 | + ->executeStatement(); |
|
| 960 | + |
|
| 961 | + // Only dispatch if we actually deleted anything |
|
| 962 | + if ($calendarData) { |
|
| 963 | + $this->dispatcher->dispatchTyped(new CalendarDeletedEvent($calendarId, $calendarData, $shares)); |
|
| 964 | + } |
|
| 965 | + } else { |
|
| 966 | + $qbMarkCalendarDeleted = $this->db->getQueryBuilder(); |
|
| 967 | + $qbMarkCalendarDeleted->update('calendars') |
|
| 968 | + ->set('deleted_at', $qbMarkCalendarDeleted->createNamedParameter(time())) |
|
| 969 | + ->where($qbMarkCalendarDeleted->expr()->eq('id', $qbMarkCalendarDeleted->createNamedParameter($calendarId))) |
|
| 970 | + ->executeStatement(); |
|
| 971 | + |
|
| 972 | + $calendarData = $this->getCalendarById($calendarId); |
|
| 973 | + $shares = $this->getShares($calendarId); |
|
| 974 | + if ($calendarData) { |
|
| 975 | + $this->dispatcher->dispatchTyped(new CalendarMovedToTrashEvent( |
|
| 976 | + $calendarId, |
|
| 977 | + $calendarData, |
|
| 978 | + $shares |
|
| 979 | + )); |
|
| 980 | + } |
|
| 981 | + } |
|
| 982 | + }, $this->db); |
|
| 983 | + } |
|
| 984 | + |
|
| 985 | + public function restoreCalendar(int $id): void { |
|
| 986 | + $this->atomic(function () use ($id): void { |
|
| 987 | + $qb = $this->db->getQueryBuilder(); |
|
| 988 | + $update = $qb->update('calendars') |
|
| 989 | + ->set('deleted_at', $qb->createNamedParameter(null)) |
|
| 990 | + ->where($qb->expr()->eq('id', $qb->createNamedParameter($id, IQueryBuilder::PARAM_INT), IQueryBuilder::PARAM_INT)); |
|
| 991 | + $update->executeStatement(); |
|
| 992 | + |
|
| 993 | + $calendarData = $this->getCalendarById($id); |
|
| 994 | + $shares = $this->getShares($id); |
|
| 995 | + if ($calendarData === null) { |
|
| 996 | + throw new RuntimeException('Calendar data that was just written can\'t be read back. Check your database configuration.'); |
|
| 997 | + } |
|
| 998 | + $this->dispatcher->dispatchTyped(new CalendarRestoredEvent( |
|
| 999 | + $id, |
|
| 1000 | + $calendarData, |
|
| 1001 | + $shares |
|
| 1002 | + )); |
|
| 1003 | + }, $this->db); |
|
| 1004 | + } |
|
| 1005 | + |
|
| 1006 | + /** |
|
| 1007 | + * Returns all calendar entries as a stream of data |
|
| 1008 | + * |
|
| 1009 | + * @since 32.0.0 |
|
| 1010 | + * |
|
| 1011 | + * @return Generator<array> |
|
| 1012 | + */ |
|
| 1013 | + public function exportCalendar(int $calendarId, int $calendarType = self::CALENDAR_TYPE_CALENDAR, ?CalendarExportOptions $options = null): Generator { |
|
| 1014 | + // extract options |
|
| 1015 | + $rangeStart = $options?->getRangeStart(); |
|
| 1016 | + $rangeCount = $options?->getRangeCount(); |
|
| 1017 | + // construct query |
|
| 1018 | + $qb = $this->db->getQueryBuilder(); |
|
| 1019 | + $qb->select('*') |
|
| 1020 | + ->from('calendarobjects') |
|
| 1021 | + ->where($qb->expr()->eq('calendarid', $qb->createNamedParameter($calendarId))) |
|
| 1022 | + ->andWhere($qb->expr()->eq('calendartype', $qb->createNamedParameter($calendarType))) |
|
| 1023 | + ->andWhere($qb->expr()->isNull('deleted_at')); |
|
| 1024 | + if ($rangeStart !== null) { |
|
| 1025 | + $qb->andWhere($qb->expr()->gt('uid', $qb->createNamedParameter($rangeStart))); |
|
| 1026 | + } |
|
| 1027 | + if ($rangeCount !== null) { |
|
| 1028 | + $qb->setMaxResults($rangeCount); |
|
| 1029 | + } |
|
| 1030 | + if ($rangeStart !== null || $rangeCount !== null) { |
|
| 1031 | + $qb->orderBy('uid', 'ASC'); |
|
| 1032 | + } |
|
| 1033 | + $rs = $qb->executeQuery(); |
|
| 1034 | + // iterate through results |
|
| 1035 | + try { |
|
| 1036 | + while (($row = $rs->fetch()) !== false) { |
|
| 1037 | + yield $row; |
|
| 1038 | + } |
|
| 1039 | + } finally { |
|
| 1040 | + $rs->closeCursor(); |
|
| 1041 | + } |
|
| 1042 | + } |
|
| 1043 | 1043 | |
| 1044 | - /** |
|
| 1045 | - * Returns all calendar objects with limited metadata for a calendar |
|
| 1046 | - * |
|
| 1047 | - * Every item contains an array with the following keys: |
|
| 1048 | - * * id - the table row id |
|
| 1049 | - * * etag - An arbitrary string |
|
| 1050 | - * * uri - a unique key which will be used to construct the uri. This can |
|
| 1051 | - * be any arbitrary string. |
|
| 1052 | - * * calendardata - The iCalendar-compatible calendar data |
|
| 1053 | - * |
|
| 1054 | - * @param mixed $calendarId |
|
| 1055 | - * @param int $calendarType |
|
| 1056 | - * @return array |
|
| 1057 | - */ |
|
| 1058 | - public function getLimitedCalendarObjects(int $calendarId, int $calendarType = self::CALENDAR_TYPE_CALENDAR):array { |
|
| 1059 | - $query = $this->db->getQueryBuilder(); |
|
| 1060 | - $query->select(['id','uid', 'etag', 'uri', 'calendardata']) |
|
| 1061 | - ->from('calendarobjects') |
|
| 1062 | - ->where($query->expr()->eq('calendarid', $query->createNamedParameter($calendarId))) |
|
| 1063 | - ->andWhere($query->expr()->eq('calendartype', $query->createNamedParameter($calendarType))) |
|
| 1064 | - ->andWhere($query->expr()->isNull('deleted_at')); |
|
| 1065 | - $stmt = $query->executeQuery(); |
|
| 1066 | - |
|
| 1067 | - $result = []; |
|
| 1068 | - while (($row = $stmt->fetch()) !== false) { |
|
| 1069 | - $result[$row['uid']] = [ |
|
| 1070 | - 'id' => $row['id'], |
|
| 1071 | - 'etag' => $row['etag'], |
|
| 1072 | - 'uri' => $row['uri'], |
|
| 1073 | - 'calendardata' => $row['calendardata'], |
|
| 1074 | - ]; |
|
| 1075 | - } |
|
| 1076 | - $stmt->closeCursor(); |
|
| 1077 | - |
|
| 1078 | - return $result; |
|
| 1079 | - } |
|
| 1080 | - |
|
| 1081 | - /** |
|
| 1082 | - * Delete all of an user's shares |
|
| 1083 | - * |
|
| 1084 | - * @param string $principaluri |
|
| 1085 | - * @return void |
|
| 1086 | - */ |
|
| 1087 | - public function deleteAllSharesByUser($principaluri) { |
|
| 1088 | - $this->calendarSharingBackend->deleteAllSharesByUser($principaluri); |
|
| 1089 | - } |
|
| 1090 | - |
|
| 1091 | - /** |
|
| 1092 | - * Returns all calendar objects within a calendar. |
|
| 1093 | - * |
|
| 1094 | - * Every item contains an array with the following keys: |
|
| 1095 | - * * calendardata - The iCalendar-compatible calendar data |
|
| 1096 | - * * uri - a unique key which will be used to construct the uri. This can |
|
| 1097 | - * be any arbitrary string, but making sure it ends with '.ics' is a |
|
| 1098 | - * good idea. This is only the basename, or filename, not the full |
|
| 1099 | - * path. |
|
| 1100 | - * * lastmodified - a timestamp of the last modification time |
|
| 1101 | - * * etag - An arbitrary string, surrounded by double-quotes. (e.g.: |
|
| 1102 | - * '"abcdef"') |
|
| 1103 | - * * size - The size of the calendar objects, in bytes. |
|
| 1104 | - * * component - optional, a string containing the type of object, such |
|
| 1105 | - * as 'vevent' or 'vtodo'. If specified, this will be used to populate |
|
| 1106 | - * the Content-Type header. |
|
| 1107 | - * |
|
| 1108 | - * Note that the etag is optional, but it's highly encouraged to return for |
|
| 1109 | - * speed reasons. |
|
| 1110 | - * |
|
| 1111 | - * The calendardata is also optional. If it's not returned |
|
| 1112 | - * 'getCalendarObject' will be called later, which *is* expected to return |
|
| 1113 | - * calendardata. |
|
| 1114 | - * |
|
| 1115 | - * If neither etag or size are specified, the calendardata will be |
|
| 1116 | - * used/fetched to determine these numbers. If both are specified the |
|
| 1117 | - * amount of times this is needed is reduced by a great degree. |
|
| 1118 | - * |
|
| 1119 | - * @param mixed $calendarId |
|
| 1120 | - * @param int $calendarType |
|
| 1121 | - * @return array |
|
| 1122 | - */ |
|
| 1123 | - public function getCalendarObjects($calendarId, $calendarType = self::CALENDAR_TYPE_CALENDAR):array { |
|
| 1124 | - $query = $this->db->getQueryBuilder(); |
|
| 1125 | - $query->select(['id', 'uri', 'lastmodified', 'etag', 'calendarid', 'size', 'componenttype', 'classification']) |
|
| 1126 | - ->from('calendarobjects') |
|
| 1127 | - ->where($query->expr()->eq('calendarid', $query->createNamedParameter($calendarId))) |
|
| 1128 | - ->andWhere($query->expr()->eq('calendartype', $query->createNamedParameter($calendarType))) |
|
| 1129 | - ->andWhere($query->expr()->isNull('deleted_at')); |
|
| 1130 | - $stmt = $query->executeQuery(); |
|
| 1131 | - |
|
| 1132 | - $result = []; |
|
| 1133 | - while (($row = $stmt->fetch()) !== false) { |
|
| 1134 | - $result[] = [ |
|
| 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 | - 'component' => strtolower($row['componenttype']), |
|
| 1142 | - 'classification' => (int)$row['classification'] |
|
| 1143 | - ]; |
|
| 1144 | - } |
|
| 1145 | - $stmt->closeCursor(); |
|
| 1146 | - |
|
| 1147 | - return $result; |
|
| 1148 | - } |
|
| 1149 | - |
|
| 1150 | - public function getDeletedCalendarObjects(int $deletedBefore): array { |
|
| 1151 | - $query = $this->db->getQueryBuilder(); |
|
| 1152 | - $query->select(['co.id', 'co.uri', 'co.lastmodified', 'co.etag', 'co.calendarid', 'co.calendartype', 'co.size', 'co.componenttype', 'co.classification', 'co.deleted_at']) |
|
| 1153 | - ->from('calendarobjects', 'co') |
|
| 1154 | - ->join('co', 'calendars', 'c', $query->expr()->eq('c.id', 'co.calendarid', IQueryBuilder::PARAM_INT)) |
|
| 1155 | - ->where($query->expr()->isNotNull('co.deleted_at')) |
|
| 1156 | - ->andWhere($query->expr()->lt('co.deleted_at', $query->createNamedParameter($deletedBefore))); |
|
| 1157 | - $stmt = $query->executeQuery(); |
|
| 1158 | - |
|
| 1159 | - $result = []; |
|
| 1160 | - while (($row = $stmt->fetch()) !== false) { |
|
| 1161 | - $result[] = [ |
|
| 1162 | - 'id' => $row['id'], |
|
| 1163 | - 'uri' => $row['uri'], |
|
| 1164 | - 'lastmodified' => $row['lastmodified'], |
|
| 1165 | - 'etag' => '"' . $row['etag'] . '"', |
|
| 1166 | - 'calendarid' => (int)$row['calendarid'], |
|
| 1167 | - 'calendartype' => (int)$row['calendartype'], |
|
| 1168 | - 'size' => (int)$row['size'], |
|
| 1169 | - 'component' => strtolower($row['componenttype']), |
|
| 1170 | - 'classification' => (int)$row['classification'], |
|
| 1171 | - '{' . \OCA\DAV\DAV\Sharing\Plugin::NS_NEXTCLOUD . '}deleted-at' => $row['deleted_at'] === null ? $row['deleted_at'] : (int)$row['deleted_at'], |
|
| 1172 | - ]; |
|
| 1173 | - } |
|
| 1174 | - $stmt->closeCursor(); |
|
| 1175 | - |
|
| 1176 | - return $result; |
|
| 1177 | - } |
|
| 1178 | - |
|
| 1179 | - /** |
|
| 1180 | - * Return all deleted calendar objects by the given principal that are not |
|
| 1181 | - * in deleted calendars. |
|
| 1182 | - * |
|
| 1183 | - * @param string $principalUri |
|
| 1184 | - * @return array |
|
| 1185 | - * @throws Exception |
|
| 1186 | - */ |
|
| 1187 | - public function getDeletedCalendarObjectsByPrincipal(string $principalUri): array { |
|
| 1188 | - $query = $this->db->getQueryBuilder(); |
|
| 1189 | - $query->select(['co.id', 'co.uri', 'co.lastmodified', 'co.etag', 'co.calendarid', 'co.size', 'co.componenttype', 'co.classification', 'co.deleted_at']) |
|
| 1190 | - ->selectAlias('c.uri', 'calendaruri') |
|
| 1191 | - ->from('calendarobjects', 'co') |
|
| 1192 | - ->join('co', 'calendars', 'c', $query->expr()->eq('c.id', 'co.calendarid', IQueryBuilder::PARAM_INT)) |
|
| 1193 | - ->where($query->expr()->eq('principaluri', $query->createNamedParameter($principalUri))) |
|
| 1194 | - ->andWhere($query->expr()->isNotNull('co.deleted_at')) |
|
| 1195 | - ->andWhere($query->expr()->isNull('c.deleted_at')); |
|
| 1196 | - $stmt = $query->executeQuery(); |
|
| 1197 | - |
|
| 1198 | - $result = []; |
|
| 1199 | - while ($row = $stmt->fetch()) { |
|
| 1200 | - $result[] = [ |
|
| 1201 | - 'id' => $row['id'], |
|
| 1202 | - 'uri' => $row['uri'], |
|
| 1203 | - 'lastmodified' => $row['lastmodified'], |
|
| 1204 | - 'etag' => '"' . $row['etag'] . '"', |
|
| 1205 | - 'calendarid' => $row['calendarid'], |
|
| 1206 | - 'calendaruri' => $row['calendaruri'], |
|
| 1207 | - 'size' => (int)$row['size'], |
|
| 1208 | - 'component' => strtolower($row['componenttype']), |
|
| 1209 | - 'classification' => (int)$row['classification'], |
|
| 1210 | - '{' . \OCA\DAV\DAV\Sharing\Plugin::NS_NEXTCLOUD . '}deleted-at' => $row['deleted_at'] === null ? $row['deleted_at'] : (int)$row['deleted_at'], |
|
| 1211 | - ]; |
|
| 1212 | - } |
|
| 1213 | - $stmt->closeCursor(); |
|
| 1214 | - |
|
| 1215 | - return $result; |
|
| 1216 | - } |
|
| 1217 | - |
|
| 1218 | - /** |
|
| 1219 | - * Returns information from a single calendar object, based on it's object |
|
| 1220 | - * uri. |
|
| 1221 | - * |
|
| 1222 | - * The object uri is only the basename, or filename and not a full path. |
|
| 1223 | - * |
|
| 1224 | - * The returned array must have the same keys as getCalendarObjects. The |
|
| 1225 | - * 'calendardata' object is required here though, while it's not required |
|
| 1226 | - * for getCalendarObjects. |
|
| 1227 | - * |
|
| 1228 | - * This method must return null if the object did not exist. |
|
| 1229 | - * |
|
| 1230 | - * @param mixed $calendarId |
|
| 1231 | - * @param string $objectUri |
|
| 1232 | - * @param int $calendarType |
|
| 1233 | - * @return array|null |
|
| 1234 | - */ |
|
| 1235 | - public function getCalendarObject($calendarId, $objectUri, int $calendarType = self::CALENDAR_TYPE_CALENDAR) { |
|
| 1236 | - $key = $calendarId . '::' . $objectUri . '::' . $calendarType; |
|
| 1237 | - if (isset($this->cachedObjects[$key])) { |
|
| 1238 | - return $this->cachedObjects[$key]; |
|
| 1239 | - } |
|
| 1240 | - $query = $this->db->getQueryBuilder(); |
|
| 1241 | - $query->select(['id', 'uri', 'uid', 'lastmodified', 'etag', 'calendarid', 'size', 'calendardata', 'componenttype', 'classification', 'deleted_at']) |
|
| 1242 | - ->from('calendarobjects') |
|
| 1243 | - ->where($query->expr()->eq('calendarid', $query->createNamedParameter($calendarId))) |
|
| 1244 | - ->andWhere($query->expr()->eq('uri', $query->createNamedParameter($objectUri))) |
|
| 1245 | - ->andWhere($query->expr()->eq('calendartype', $query->createNamedParameter($calendarType))); |
|
| 1246 | - $stmt = $query->executeQuery(); |
|
| 1247 | - $row = $stmt->fetch(); |
|
| 1248 | - $stmt->closeCursor(); |
|
| 1249 | - |
|
| 1250 | - if (!$row) { |
|
| 1251 | - return null; |
|
| 1252 | - } |
|
| 1253 | - |
|
| 1254 | - $object = $this->rowToCalendarObject($row); |
|
| 1255 | - $this->cachedObjects[$key] = $object; |
|
| 1256 | - return $object; |
|
| 1257 | - } |
|
| 1258 | - |
|
| 1259 | - private function rowToCalendarObject(array $row): array { |
|
| 1260 | - return [ |
|
| 1261 | - 'id' => $row['id'], |
|
| 1262 | - 'uri' => $row['uri'], |
|
| 1263 | - 'uid' => $row['uid'], |
|
| 1264 | - 'lastmodified' => $row['lastmodified'], |
|
| 1265 | - 'etag' => '"' . $row['etag'] . '"', |
|
| 1266 | - 'calendarid' => $row['calendarid'], |
|
| 1267 | - 'size' => (int)$row['size'], |
|
| 1268 | - 'calendardata' => $this->readBlob($row['calendardata']), |
|
| 1269 | - 'component' => strtolower($row['componenttype']), |
|
| 1270 | - 'classification' => (int)$row['classification'], |
|
| 1271 | - '{' . \OCA\DAV\DAV\Sharing\Plugin::NS_NEXTCLOUD . '}deleted-at' => $row['deleted_at'] === null ? $row['deleted_at'] : (int)$row['deleted_at'], |
|
| 1272 | - ]; |
|
| 1273 | - } |
|
| 1274 | - |
|
| 1275 | - /** |
|
| 1276 | - * Returns a list of calendar objects. |
|
| 1277 | - * |
|
| 1278 | - * This method should work identical to getCalendarObject, but instead |
|
| 1279 | - * return all the calendar objects in the list as an array. |
|
| 1280 | - * |
|
| 1281 | - * If the backend supports this, it may allow for some speed-ups. |
|
| 1282 | - * |
|
| 1283 | - * @param mixed $calendarId |
|
| 1284 | - * @param string[] $uris |
|
| 1285 | - * @param int $calendarType |
|
| 1286 | - * @return array |
|
| 1287 | - */ |
|
| 1288 | - public function getMultipleCalendarObjects($calendarId, array $uris, $calendarType = self::CALENDAR_TYPE_CALENDAR):array { |
|
| 1289 | - if (empty($uris)) { |
|
| 1290 | - return []; |
|
| 1291 | - } |
|
| 1292 | - |
|
| 1293 | - $chunks = array_chunk($uris, 100); |
|
| 1294 | - $objects = []; |
|
| 1295 | - |
|
| 1296 | - $query = $this->db->getQueryBuilder(); |
|
| 1297 | - $query->select(['id', 'uri', 'lastmodified', 'etag', 'calendarid', 'size', 'calendardata', 'componenttype', 'classification']) |
|
| 1298 | - ->from('calendarobjects') |
|
| 1299 | - ->where($query->expr()->eq('calendarid', $query->createNamedParameter($calendarId))) |
|
| 1300 | - ->andWhere($query->expr()->in('uri', $query->createParameter('uri'))) |
|
| 1301 | - ->andWhere($query->expr()->eq('calendartype', $query->createNamedParameter($calendarType))) |
|
| 1302 | - ->andWhere($query->expr()->isNull('deleted_at')); |
|
| 1303 | - |
|
| 1304 | - foreach ($chunks as $uris) { |
|
| 1305 | - $query->setParameter('uri', $uris, IQueryBuilder::PARAM_STR_ARRAY); |
|
| 1306 | - $result = $query->executeQuery(); |
|
| 1307 | - |
|
| 1308 | - while ($row = $result->fetch()) { |
|
| 1309 | - $objects[] = [ |
|
| 1310 | - 'id' => $row['id'], |
|
| 1311 | - 'uri' => $row['uri'], |
|
| 1312 | - 'lastmodified' => $row['lastmodified'], |
|
| 1313 | - 'etag' => '"' . $row['etag'] . '"', |
|
| 1314 | - 'calendarid' => $row['calendarid'], |
|
| 1315 | - 'size' => (int)$row['size'], |
|
| 1316 | - 'calendardata' => $this->readBlob($row['calendardata']), |
|
| 1317 | - 'component' => strtolower($row['componenttype']), |
|
| 1318 | - 'classification' => (int)$row['classification'] |
|
| 1319 | - ]; |
|
| 1320 | - } |
|
| 1321 | - $result->closeCursor(); |
|
| 1322 | - } |
|
| 1323 | - |
|
| 1324 | - return $objects; |
|
| 1325 | - } |
|
| 1326 | - |
|
| 1327 | - /** |
|
| 1328 | - * Creates a new calendar object. |
|
| 1329 | - * |
|
| 1330 | - * The object uri is only the basename, or filename and not a full path. |
|
| 1331 | - * |
|
| 1332 | - * It is possible return an etag from this function, which will be used in |
|
| 1333 | - * the response to this PUT request. Note that the ETag must be surrounded |
|
| 1334 | - * by double-quotes. |
|
| 1335 | - * |
|
| 1336 | - * However, you should only really return this ETag if you don't mangle the |
|
| 1337 | - * calendar-data. If the result of a subsequent GET to this object is not |
|
| 1338 | - * the exact same as this request body, you should omit the ETag. |
|
| 1339 | - * |
|
| 1340 | - * @param mixed $calendarId |
|
| 1341 | - * @param string $objectUri |
|
| 1342 | - * @param string $calendarData |
|
| 1343 | - * @param int $calendarType |
|
| 1344 | - * @return string |
|
| 1345 | - */ |
|
| 1346 | - public function createCalendarObject($calendarId, $objectUri, $calendarData, $calendarType = self::CALENDAR_TYPE_CALENDAR) { |
|
| 1347 | - $this->cachedObjects = []; |
|
| 1348 | - $extraData = $this->getDenormalizedData($calendarData); |
|
| 1349 | - |
|
| 1350 | - return $this->atomic(function () use ($calendarId, $objectUri, $calendarData, $extraData, $calendarType) { |
|
| 1351 | - // Try to detect duplicates |
|
| 1352 | - $qb = $this->db->getQueryBuilder(); |
|
| 1353 | - $qb->select($qb->func()->count('*')) |
|
| 1354 | - ->from('calendarobjects') |
|
| 1355 | - ->where($qb->expr()->eq('calendarid', $qb->createNamedParameter($calendarId))) |
|
| 1356 | - ->andWhere($qb->expr()->eq('uid', $qb->createNamedParameter($extraData['uid']))) |
|
| 1357 | - ->andWhere($qb->expr()->eq('calendartype', $qb->createNamedParameter($calendarType))) |
|
| 1358 | - ->andWhere($qb->expr()->isNull('deleted_at')); |
|
| 1359 | - $result = $qb->executeQuery(); |
|
| 1360 | - $count = (int)$result->fetchOne(); |
|
| 1361 | - $result->closeCursor(); |
|
| 1362 | - |
|
| 1363 | - if ($count !== 0) { |
|
| 1364 | - throw new BadRequest('Calendar object with uid already exists in this calendar collection.'); |
|
| 1365 | - } |
|
| 1366 | - // For a more specific error message we also try to explicitly look up the UID but as a deleted entry |
|
| 1367 | - $qbDel = $this->db->getQueryBuilder(); |
|
| 1368 | - $qbDel->select('*') |
|
| 1369 | - ->from('calendarobjects') |
|
| 1370 | - ->where($qbDel->expr()->eq('calendarid', $qbDel->createNamedParameter($calendarId))) |
|
| 1371 | - ->andWhere($qbDel->expr()->eq('uid', $qbDel->createNamedParameter($extraData['uid']))) |
|
| 1372 | - ->andWhere($qbDel->expr()->eq('calendartype', $qbDel->createNamedParameter($calendarType))) |
|
| 1373 | - ->andWhere($qbDel->expr()->isNotNull('deleted_at')); |
|
| 1374 | - $result = $qbDel->executeQuery(); |
|
| 1375 | - $found = $result->fetch(); |
|
| 1376 | - $result->closeCursor(); |
|
| 1377 | - if ($found !== false) { |
|
| 1378 | - // the object existed previously but has been deleted |
|
| 1379 | - // remove the trashbin entry and continue as if it was a new object |
|
| 1380 | - $this->deleteCalendarObject($calendarId, $found['uri']); |
|
| 1381 | - } |
|
| 1382 | - |
|
| 1383 | - $query = $this->db->getQueryBuilder(); |
|
| 1384 | - $query->insert('calendarobjects') |
|
| 1385 | - ->values([ |
|
| 1386 | - 'calendarid' => $query->createNamedParameter($calendarId), |
|
| 1387 | - 'uri' => $query->createNamedParameter($objectUri), |
|
| 1388 | - 'calendardata' => $query->createNamedParameter($calendarData, IQueryBuilder::PARAM_LOB), |
|
| 1389 | - 'lastmodified' => $query->createNamedParameter(time()), |
|
| 1390 | - 'etag' => $query->createNamedParameter($extraData['etag']), |
|
| 1391 | - 'size' => $query->createNamedParameter($extraData['size']), |
|
| 1392 | - 'componenttype' => $query->createNamedParameter($extraData['componentType']), |
|
| 1393 | - 'firstoccurence' => $query->createNamedParameter($extraData['firstOccurence']), |
|
| 1394 | - 'lastoccurence' => $query->createNamedParameter($extraData['lastOccurence']), |
|
| 1395 | - 'classification' => $query->createNamedParameter($extraData['classification']), |
|
| 1396 | - 'uid' => $query->createNamedParameter($extraData['uid']), |
|
| 1397 | - 'calendartype' => $query->createNamedParameter($calendarType), |
|
| 1398 | - ]) |
|
| 1399 | - ->executeStatement(); |
|
| 1400 | - |
|
| 1401 | - $this->updateProperties($calendarId, $objectUri, $calendarData, $calendarType); |
|
| 1402 | - $this->addChanges($calendarId, [$objectUri], 1, $calendarType); |
|
| 1403 | - |
|
| 1404 | - $objectRow = $this->getCalendarObject($calendarId, $objectUri, $calendarType); |
|
| 1405 | - assert($objectRow !== null); |
|
| 1406 | - |
|
| 1407 | - if ($calendarType === self::CALENDAR_TYPE_CALENDAR) { |
|
| 1408 | - $calendarRow = $this->getCalendarById($calendarId); |
|
| 1409 | - $shares = $this->getShares($calendarId); |
|
| 1410 | - |
|
| 1411 | - $this->dispatcher->dispatchTyped(new CalendarObjectCreatedEvent($calendarId, $calendarRow, $shares, $objectRow)); |
|
| 1412 | - } else { |
|
| 1413 | - $subscriptionRow = $this->getSubscriptionById($calendarId); |
|
| 1414 | - |
|
| 1415 | - $this->dispatcher->dispatchTyped(new CachedCalendarObjectCreatedEvent($calendarId, $subscriptionRow, [], $objectRow)); |
|
| 1416 | - } |
|
| 1417 | - |
|
| 1418 | - return '"' . $extraData['etag'] . '"'; |
|
| 1419 | - }, $this->db); |
|
| 1420 | - } |
|
| 1421 | - |
|
| 1422 | - /** |
|
| 1423 | - * Updates an existing calendarobject, based on it's uri. |
|
| 1424 | - * |
|
| 1425 | - * The object uri is only the basename, or filename and not a full path. |
|
| 1426 | - * |
|
| 1427 | - * It is possible return an etag from this function, which will be used in |
|
| 1428 | - * the response to this PUT request. Note that the ETag must be surrounded |
|
| 1429 | - * by double-quotes. |
|
| 1430 | - * |
|
| 1431 | - * However, you should only really return this ETag if you don't mangle the |
|
| 1432 | - * calendar-data. If the result of a subsequent GET to this object is not |
|
| 1433 | - * the exact same as this request body, you should omit the ETag. |
|
| 1434 | - * |
|
| 1435 | - * @param mixed $calendarId |
|
| 1436 | - * @param string $objectUri |
|
| 1437 | - * @param string $calendarData |
|
| 1438 | - * @param int $calendarType |
|
| 1439 | - * @return string |
|
| 1440 | - */ |
|
| 1441 | - public function updateCalendarObject($calendarId, $objectUri, $calendarData, $calendarType = self::CALENDAR_TYPE_CALENDAR) { |
|
| 1442 | - $this->cachedObjects = []; |
|
| 1443 | - $extraData = $this->getDenormalizedData($calendarData); |
|
| 1444 | - |
|
| 1445 | - return $this->atomic(function () use ($calendarId, $objectUri, $calendarData, $extraData, $calendarType) { |
|
| 1446 | - $query = $this->db->getQueryBuilder(); |
|
| 1447 | - $query->update('calendarobjects') |
|
| 1448 | - ->set('calendardata', $query->createNamedParameter($calendarData, IQueryBuilder::PARAM_LOB)) |
|
| 1449 | - ->set('lastmodified', $query->createNamedParameter(time())) |
|
| 1450 | - ->set('etag', $query->createNamedParameter($extraData['etag'])) |
|
| 1451 | - ->set('size', $query->createNamedParameter($extraData['size'])) |
|
| 1452 | - ->set('componenttype', $query->createNamedParameter($extraData['componentType'])) |
|
| 1453 | - ->set('firstoccurence', $query->createNamedParameter($extraData['firstOccurence'])) |
|
| 1454 | - ->set('lastoccurence', $query->createNamedParameter($extraData['lastOccurence'])) |
|
| 1455 | - ->set('classification', $query->createNamedParameter($extraData['classification'])) |
|
| 1456 | - ->set('uid', $query->createNamedParameter($extraData['uid'])) |
|
| 1457 | - ->where($query->expr()->eq('calendarid', $query->createNamedParameter($calendarId))) |
|
| 1458 | - ->andWhere($query->expr()->eq('uri', $query->createNamedParameter($objectUri))) |
|
| 1459 | - ->andWhere($query->expr()->eq('calendartype', $query->createNamedParameter($calendarType))) |
|
| 1460 | - ->executeStatement(); |
|
| 1461 | - |
|
| 1462 | - $this->updateProperties($calendarId, $objectUri, $calendarData, $calendarType); |
|
| 1463 | - $this->addChanges($calendarId, [$objectUri], 2, $calendarType); |
|
| 1464 | - |
|
| 1465 | - $objectRow = $this->getCalendarObject($calendarId, $objectUri, $calendarType); |
|
| 1466 | - if (is_array($objectRow)) { |
|
| 1467 | - if ($calendarType === self::CALENDAR_TYPE_CALENDAR) { |
|
| 1468 | - $calendarRow = $this->getCalendarById($calendarId); |
|
| 1469 | - $shares = $this->getShares($calendarId); |
|
| 1470 | - |
|
| 1471 | - $this->dispatcher->dispatchTyped(new CalendarObjectUpdatedEvent($calendarId, $calendarRow, $shares, $objectRow)); |
|
| 1472 | - } else { |
|
| 1473 | - $subscriptionRow = $this->getSubscriptionById($calendarId); |
|
| 1474 | - |
|
| 1475 | - $this->dispatcher->dispatchTyped(new CachedCalendarObjectUpdatedEvent($calendarId, $subscriptionRow, [], $objectRow)); |
|
| 1476 | - } |
|
| 1477 | - } |
|
| 1478 | - |
|
| 1479 | - return '"' . $extraData['etag'] . '"'; |
|
| 1480 | - }, $this->db); |
|
| 1481 | - } |
|
| 1482 | - |
|
| 1483 | - /** |
|
| 1484 | - * Moves a calendar object from calendar to calendar. |
|
| 1485 | - * |
|
| 1486 | - * @param string $sourcePrincipalUri |
|
| 1487 | - * @param int $sourceObjectId |
|
| 1488 | - * @param string $targetPrincipalUri |
|
| 1489 | - * @param int $targetCalendarId |
|
| 1490 | - * @param string $tragetObjectUri |
|
| 1491 | - * @param int $calendarType |
|
| 1492 | - * @return bool |
|
| 1493 | - * @throws Exception |
|
| 1494 | - */ |
|
| 1495 | - public function moveCalendarObject(string $sourcePrincipalUri, int $sourceObjectId, string $targetPrincipalUri, int $targetCalendarId, string $tragetObjectUri, int $calendarType = self::CALENDAR_TYPE_CALENDAR): bool { |
|
| 1496 | - $this->cachedObjects = []; |
|
| 1497 | - return $this->atomic(function () use ($sourcePrincipalUri, $sourceObjectId, $targetPrincipalUri, $targetCalendarId, $tragetObjectUri, $calendarType) { |
|
| 1498 | - $object = $this->getCalendarObjectById($sourcePrincipalUri, $sourceObjectId); |
|
| 1499 | - if (empty($object)) { |
|
| 1500 | - return false; |
|
| 1501 | - } |
|
| 1502 | - |
|
| 1503 | - $sourceCalendarId = $object['calendarid']; |
|
| 1504 | - $sourceObjectUri = $object['uri']; |
|
| 1505 | - |
|
| 1506 | - $query = $this->db->getQueryBuilder(); |
|
| 1507 | - $query->update('calendarobjects') |
|
| 1508 | - ->set('calendarid', $query->createNamedParameter($targetCalendarId, IQueryBuilder::PARAM_INT)) |
|
| 1509 | - ->set('uri', $query->createNamedParameter($tragetObjectUri, IQueryBuilder::PARAM_STR)) |
|
| 1510 | - ->where($query->expr()->eq('id', $query->createNamedParameter($sourceObjectId, IQueryBuilder::PARAM_INT), IQueryBuilder::PARAM_INT)) |
|
| 1511 | - ->executeStatement(); |
|
| 1512 | - |
|
| 1513 | - $this->purgeProperties($sourceCalendarId, $sourceObjectId); |
|
| 1514 | - $this->updateProperties($targetCalendarId, $tragetObjectUri, $object['calendardata'], $calendarType); |
|
| 1515 | - |
|
| 1516 | - $this->addChanges($sourceCalendarId, [$sourceObjectUri], 3, $calendarType); |
|
| 1517 | - $this->addChanges($targetCalendarId, [$tragetObjectUri], 1, $calendarType); |
|
| 1518 | - |
|
| 1519 | - $object = $this->getCalendarObjectById($targetPrincipalUri, $sourceObjectId); |
|
| 1520 | - // Calendar Object wasn't found - possibly because it was deleted in the meantime by a different client |
|
| 1521 | - if (empty($object)) { |
|
| 1522 | - return false; |
|
| 1523 | - } |
|
| 1524 | - |
|
| 1525 | - $targetCalendarRow = $this->getCalendarById($targetCalendarId); |
|
| 1526 | - // the calendar this event is being moved to does not exist any longer |
|
| 1527 | - if (empty($targetCalendarRow)) { |
|
| 1528 | - return false; |
|
| 1529 | - } |
|
| 1530 | - |
|
| 1531 | - if ($calendarType === self::CALENDAR_TYPE_CALENDAR) { |
|
| 1532 | - $sourceShares = $this->getShares($sourceCalendarId); |
|
| 1533 | - $targetShares = $this->getShares($targetCalendarId); |
|
| 1534 | - $sourceCalendarRow = $this->getCalendarById($sourceCalendarId); |
|
| 1535 | - $this->dispatcher->dispatchTyped(new CalendarObjectMovedEvent($sourceCalendarId, $sourceCalendarRow, $targetCalendarId, $targetCalendarRow, $sourceShares, $targetShares, $object)); |
|
| 1536 | - } |
|
| 1537 | - return true; |
|
| 1538 | - }, $this->db); |
|
| 1539 | - } |
|
| 1540 | - |
|
| 1541 | - |
|
| 1542 | - /** |
|
| 1543 | - * @param int $calendarObjectId |
|
| 1544 | - * @param int $classification |
|
| 1545 | - */ |
|
| 1546 | - public function setClassification($calendarObjectId, $classification) { |
|
| 1547 | - $this->cachedObjects = []; |
|
| 1548 | - if (!in_array($classification, [ |
|
| 1549 | - self::CLASSIFICATION_PUBLIC, self::CLASSIFICATION_PRIVATE, self::CLASSIFICATION_CONFIDENTIAL |
|
| 1550 | - ])) { |
|
| 1551 | - throw new \InvalidArgumentException(); |
|
| 1552 | - } |
|
| 1553 | - $query = $this->db->getQueryBuilder(); |
|
| 1554 | - $query->update('calendarobjects') |
|
| 1555 | - ->set('classification', $query->createNamedParameter($classification)) |
|
| 1556 | - ->where($query->expr()->eq('id', $query->createNamedParameter($calendarObjectId))) |
|
| 1557 | - ->executeStatement(); |
|
| 1558 | - } |
|
| 1559 | - |
|
| 1560 | - /** |
|
| 1561 | - * Deletes an existing calendar object. |
|
| 1562 | - * |
|
| 1563 | - * The object uri is only the basename, or filename and not a full path. |
|
| 1564 | - * |
|
| 1565 | - * @param mixed $calendarId |
|
| 1566 | - * @param string $objectUri |
|
| 1567 | - * @param int $calendarType |
|
| 1568 | - * @param bool $forceDeletePermanently |
|
| 1569 | - * @return void |
|
| 1570 | - */ |
|
| 1571 | - public function deleteCalendarObject($calendarId, $objectUri, $calendarType = self::CALENDAR_TYPE_CALENDAR, bool $forceDeletePermanently = false) { |
|
| 1572 | - $this->cachedObjects = []; |
|
| 1573 | - $this->atomic(function () use ($calendarId, $objectUri, $calendarType, $forceDeletePermanently): void { |
|
| 1574 | - $data = $this->getCalendarObject($calendarId, $objectUri, $calendarType); |
|
| 1575 | - |
|
| 1576 | - if ($data === null) { |
|
| 1577 | - // Nothing to delete |
|
| 1578 | - return; |
|
| 1579 | - } |
|
| 1580 | - |
|
| 1581 | - if ($forceDeletePermanently || $this->config->getAppValue(Application::APP_ID, RetentionService::RETENTION_CONFIG_KEY) === '0') { |
|
| 1582 | - $stmt = $this->db->prepare('DELETE FROM `*PREFIX*calendarobjects` WHERE `calendarid` = ? AND `uri` = ? AND `calendartype` = ?'); |
|
| 1583 | - $stmt->execute([$calendarId, $objectUri, $calendarType]); |
|
| 1584 | - |
|
| 1585 | - $this->purgeProperties($calendarId, $data['id']); |
|
| 1586 | - |
|
| 1587 | - $this->purgeObjectInvitations($data['uid']); |
|
| 1588 | - |
|
| 1589 | - if ($calendarType === self::CALENDAR_TYPE_CALENDAR) { |
|
| 1590 | - $calendarRow = $this->getCalendarById($calendarId); |
|
| 1591 | - $shares = $this->getShares($calendarId); |
|
| 1592 | - |
|
| 1593 | - $this->dispatcher->dispatchTyped(new CalendarObjectDeletedEvent($calendarId, $calendarRow, $shares, $data)); |
|
| 1594 | - } else { |
|
| 1595 | - $subscriptionRow = $this->getSubscriptionById($calendarId); |
|
| 1596 | - |
|
| 1597 | - $this->dispatcher->dispatchTyped(new CachedCalendarObjectDeletedEvent($calendarId, $subscriptionRow, [], $data)); |
|
| 1598 | - } |
|
| 1599 | - } else { |
|
| 1600 | - $pathInfo = pathinfo($data['uri']); |
|
| 1601 | - if (!empty($pathInfo['extension'])) { |
|
| 1602 | - // Append a suffix to "free" the old URI for recreation |
|
| 1603 | - $newUri = sprintf( |
|
| 1604 | - '%s-deleted.%s', |
|
| 1605 | - $pathInfo['filename'], |
|
| 1606 | - $pathInfo['extension'] |
|
| 1607 | - ); |
|
| 1608 | - } else { |
|
| 1609 | - $newUri = sprintf( |
|
| 1610 | - '%s-deleted', |
|
| 1611 | - $pathInfo['filename'] |
|
| 1612 | - ); |
|
| 1613 | - } |
|
| 1614 | - |
|
| 1615 | - // Try to detect conflicts before the DB does |
|
| 1616 | - // As unlikely as it seems, this can happen when the user imports, then deletes, imports and deletes again |
|
| 1617 | - $newObject = $this->getCalendarObject($calendarId, $newUri, $calendarType); |
|
| 1618 | - if ($newObject !== null) { |
|
| 1619 | - throw new Forbidden("A calendar object with URI $newUri already exists in calendar $calendarId, therefore this object can't be moved into the trashbin"); |
|
| 1620 | - } |
|
| 1621 | - |
|
| 1622 | - $qb = $this->db->getQueryBuilder(); |
|
| 1623 | - $markObjectDeletedQuery = $qb->update('calendarobjects') |
|
| 1624 | - ->set('deleted_at', $qb->createNamedParameter(time(), IQueryBuilder::PARAM_INT)) |
|
| 1625 | - ->set('uri', $qb->createNamedParameter($newUri)) |
|
| 1626 | - ->where( |
|
| 1627 | - $qb->expr()->eq('calendarid', $qb->createNamedParameter($calendarId)), |
|
| 1628 | - $qb->expr()->eq('calendartype', $qb->createNamedParameter($calendarType, IQueryBuilder::PARAM_INT), IQueryBuilder::PARAM_INT), |
|
| 1629 | - $qb->expr()->eq('uri', $qb->createNamedParameter($objectUri)) |
|
| 1630 | - ); |
|
| 1631 | - $markObjectDeletedQuery->executeStatement(); |
|
| 1632 | - |
|
| 1633 | - $calendarData = $this->getCalendarById($calendarId); |
|
| 1634 | - if ($calendarData !== null) { |
|
| 1635 | - $this->dispatcher->dispatchTyped( |
|
| 1636 | - new CalendarObjectMovedToTrashEvent( |
|
| 1637 | - $calendarId, |
|
| 1638 | - $calendarData, |
|
| 1639 | - $this->getShares($calendarId), |
|
| 1640 | - $data |
|
| 1641 | - ) |
|
| 1642 | - ); |
|
| 1643 | - } |
|
| 1644 | - } |
|
| 1645 | - |
|
| 1646 | - $this->addChanges($calendarId, [$objectUri], 3, $calendarType); |
|
| 1647 | - }, $this->db); |
|
| 1648 | - } |
|
| 1649 | - |
|
| 1650 | - /** |
|
| 1651 | - * @param mixed $objectData |
|
| 1652 | - * |
|
| 1653 | - * @throws Forbidden |
|
| 1654 | - */ |
|
| 1655 | - public function restoreCalendarObject(array $objectData): void { |
|
| 1656 | - $this->cachedObjects = []; |
|
| 1657 | - $this->atomic(function () use ($objectData): void { |
|
| 1658 | - $id = (int)$objectData['id']; |
|
| 1659 | - $restoreUri = str_replace('-deleted.ics', '.ics', $objectData['uri']); |
|
| 1660 | - $targetObject = $this->getCalendarObject( |
|
| 1661 | - $objectData['calendarid'], |
|
| 1662 | - $restoreUri |
|
| 1663 | - ); |
|
| 1664 | - if ($targetObject !== null) { |
|
| 1665 | - throw new Forbidden("Can not restore calendar $id because a calendar object with the URI $restoreUri already exists"); |
|
| 1666 | - } |
|
| 1667 | - |
|
| 1668 | - $qb = $this->db->getQueryBuilder(); |
|
| 1669 | - $update = $qb->update('calendarobjects') |
|
| 1670 | - ->set('uri', $qb->createNamedParameter($restoreUri)) |
|
| 1671 | - ->set('deleted_at', $qb->createNamedParameter(null)) |
|
| 1672 | - ->where($qb->expr()->eq('id', $qb->createNamedParameter($id, IQueryBuilder::PARAM_INT), IQueryBuilder::PARAM_INT)); |
|
| 1673 | - $update->executeStatement(); |
|
| 1674 | - |
|
| 1675 | - // Make sure this change is tracked in the changes table |
|
| 1676 | - $qb2 = $this->db->getQueryBuilder(); |
|
| 1677 | - $selectObject = $qb2->select('calendardata', 'uri', 'calendarid', 'calendartype') |
|
| 1678 | - ->selectAlias('componenttype', 'component') |
|
| 1679 | - ->from('calendarobjects') |
|
| 1680 | - ->where($qb2->expr()->eq('id', $qb2->createNamedParameter($id, IQueryBuilder::PARAM_INT), IQueryBuilder::PARAM_INT)); |
|
| 1681 | - $result = $selectObject->executeQuery(); |
|
| 1682 | - $row = $result->fetch(); |
|
| 1683 | - $result->closeCursor(); |
|
| 1684 | - if ($row === false) { |
|
| 1685 | - // Welp, this should possibly not have happened, but let's ignore |
|
| 1686 | - return; |
|
| 1687 | - } |
|
| 1688 | - $this->addChanges($row['calendarid'], [$row['uri']], 1, (int)$row['calendartype']); |
|
| 1689 | - |
|
| 1690 | - $calendarRow = $this->getCalendarById((int)$row['calendarid']); |
|
| 1691 | - if ($calendarRow === null) { |
|
| 1692 | - throw new RuntimeException('Calendar object data that was just written can\'t be read back. Check your database configuration.'); |
|
| 1693 | - } |
|
| 1694 | - $this->dispatcher->dispatchTyped( |
|
| 1695 | - new CalendarObjectRestoredEvent( |
|
| 1696 | - (int)$objectData['calendarid'], |
|
| 1697 | - $calendarRow, |
|
| 1698 | - $this->getShares((int)$row['calendarid']), |
|
| 1699 | - $row |
|
| 1700 | - ) |
|
| 1701 | - ); |
|
| 1702 | - }, $this->db); |
|
| 1703 | - } |
|
| 1704 | - |
|
| 1705 | - /** |
|
| 1706 | - * Performs a calendar-query on the contents of this calendar. |
|
| 1707 | - * |
|
| 1708 | - * The calendar-query is defined in RFC4791 : CalDAV. Using the |
|
| 1709 | - * calendar-query it is possible for a client to request a specific set of |
|
| 1710 | - * object, based on contents of iCalendar properties, date-ranges and |
|
| 1711 | - * iCalendar component types (VTODO, VEVENT). |
|
| 1712 | - * |
|
| 1713 | - * This method should just return a list of (relative) urls that match this |
|
| 1714 | - * query. |
|
| 1715 | - * |
|
| 1716 | - * The list of filters are specified as an array. The exact array is |
|
| 1717 | - * documented by Sabre\CalDAV\CalendarQueryParser. |
|
| 1718 | - * |
|
| 1719 | - * Note that it is extremely likely that getCalendarObject for every path |
|
| 1720 | - * returned from this method will be called almost immediately after. You |
|
| 1721 | - * may want to anticipate this to speed up these requests. |
|
| 1722 | - * |
|
| 1723 | - * This method provides a default implementation, which parses *all* the |
|
| 1724 | - * iCalendar objects in the specified calendar. |
|
| 1725 | - * |
|
| 1726 | - * This default may well be good enough for personal use, and calendars |
|
| 1727 | - * that aren't very large. But if you anticipate high usage, big calendars |
|
| 1728 | - * or high loads, you are strongly advised to optimize certain paths. |
|
| 1729 | - * |
|
| 1730 | - * The best way to do so is override this method and to optimize |
|
| 1731 | - * specifically for 'common filters'. |
|
| 1732 | - * |
|
| 1733 | - * Requests that are extremely common are: |
|
| 1734 | - * * requests for just VEVENTS |
|
| 1735 | - * * requests for just VTODO |
|
| 1736 | - * * requests with a time-range-filter on either VEVENT or VTODO. |
|
| 1737 | - * |
|
| 1738 | - * ..and combinations of these requests. It may not be worth it to try to |
|
| 1739 | - * handle every possible situation and just rely on the (relatively |
|
| 1740 | - * easy to use) CalendarQueryValidator to handle the rest. |
|
| 1741 | - * |
|
| 1742 | - * Note that especially time-range-filters may be difficult to parse. A |
|
| 1743 | - * time-range filter specified on a VEVENT must for instance also handle |
|
| 1744 | - * recurrence rules correctly. |
|
| 1745 | - * A good example of how to interpret all these filters can also simply |
|
| 1746 | - * be found in Sabre\CalDAV\CalendarQueryFilter. This class is as correct |
|
| 1747 | - * as possible, so it gives you a good idea on what type of stuff you need |
|
| 1748 | - * to think of. |
|
| 1749 | - * |
|
| 1750 | - * @param mixed $calendarId |
|
| 1751 | - * @param array $filters |
|
| 1752 | - * @param int $calendarType |
|
| 1753 | - * @return array |
|
| 1754 | - */ |
|
| 1755 | - public function calendarQuery($calendarId, array $filters, $calendarType = self::CALENDAR_TYPE_CALENDAR):array { |
|
| 1756 | - $componentType = null; |
|
| 1757 | - $requirePostFilter = true; |
|
| 1758 | - $timeRange = null; |
|
| 1759 | - |
|
| 1760 | - // if no filters were specified, we don't need to filter after a query |
|
| 1761 | - if (!$filters['prop-filters'] && !$filters['comp-filters']) { |
|
| 1762 | - $requirePostFilter = false; |
|
| 1763 | - } |
|
| 1764 | - |
|
| 1765 | - // Figuring out if there's a component filter |
|
| 1766 | - if (count($filters['comp-filters']) > 0 && !$filters['comp-filters'][0]['is-not-defined']) { |
|
| 1767 | - $componentType = $filters['comp-filters'][0]['name']; |
|
| 1768 | - |
|
| 1769 | - // Checking if we need post-filters |
|
| 1770 | - if (!$filters['prop-filters'] && !$filters['comp-filters'][0]['comp-filters'] && !$filters['comp-filters'][0]['time-range'] && !$filters['comp-filters'][0]['prop-filters']) { |
|
| 1771 | - $requirePostFilter = false; |
|
| 1772 | - } |
|
| 1773 | - // There was a time-range filter |
|
| 1774 | - if ($componentType === 'VEVENT' && isset($filters['comp-filters'][0]['time-range']) && is_array($filters['comp-filters'][0]['time-range'])) { |
|
| 1775 | - $timeRange = $filters['comp-filters'][0]['time-range']; |
|
| 1776 | - |
|
| 1777 | - // If start time OR the end time is not specified, we can do a |
|
| 1778 | - // 100% accurate mysql query. |
|
| 1779 | - if (!$filters['prop-filters'] && !$filters['comp-filters'][0]['comp-filters'] && !$filters['comp-filters'][0]['prop-filters'] && (!$timeRange['start'] || !$timeRange['end'])) { |
|
| 1780 | - $requirePostFilter = false; |
|
| 1781 | - } |
|
| 1782 | - } |
|
| 1783 | - } |
|
| 1784 | - $query = $this->db->getQueryBuilder(); |
|
| 1785 | - $query->select(['id', 'uri', 'uid', 'lastmodified', 'etag', 'calendarid', 'size', 'calendardata', 'componenttype', 'classification', 'deleted_at']) |
|
| 1786 | - ->from('calendarobjects') |
|
| 1787 | - ->where($query->expr()->eq('calendarid', $query->createNamedParameter($calendarId))) |
|
| 1788 | - ->andWhere($query->expr()->eq('calendartype', $query->createNamedParameter($calendarType))) |
|
| 1789 | - ->andWhere($query->expr()->isNull('deleted_at')); |
|
| 1790 | - |
|
| 1791 | - if ($componentType) { |
|
| 1792 | - $query->andWhere($query->expr()->eq('componenttype', $query->createNamedParameter($componentType))); |
|
| 1793 | - } |
|
| 1794 | - |
|
| 1795 | - if ($timeRange && $timeRange['start']) { |
|
| 1796 | - $query->andWhere($query->expr()->gt('lastoccurence', $query->createNamedParameter($timeRange['start']->getTimeStamp()))); |
|
| 1797 | - } |
|
| 1798 | - if ($timeRange && $timeRange['end']) { |
|
| 1799 | - $query->andWhere($query->expr()->lt('firstoccurence', $query->createNamedParameter($timeRange['end']->getTimeStamp()))); |
|
| 1800 | - } |
|
| 1801 | - |
|
| 1802 | - $stmt = $query->executeQuery(); |
|
| 1803 | - |
|
| 1804 | - $result = []; |
|
| 1805 | - while ($row = $stmt->fetch()) { |
|
| 1806 | - // if we leave it as a blob we can't read it both from the post filter and the rowToCalendarObject |
|
| 1807 | - if (isset($row['calendardata'])) { |
|
| 1808 | - $row['calendardata'] = $this->readBlob($row['calendardata']); |
|
| 1809 | - } |
|
| 1810 | - |
|
| 1811 | - if ($requirePostFilter) { |
|
| 1812 | - // validateFilterForObject will parse the calendar data |
|
| 1813 | - // catch parsing errors |
|
| 1814 | - try { |
|
| 1815 | - $matches = $this->validateFilterForObject($row, $filters); |
|
| 1816 | - } catch (ParseException $ex) { |
|
| 1817 | - $this->logger->error('Caught parsing exception for calendar data. This usually indicates invalid calendar data. calendar-id:' . $calendarId . ' uri:' . $row['uri'], [ |
|
| 1818 | - 'app' => 'dav', |
|
| 1819 | - 'exception' => $ex, |
|
| 1820 | - ]); |
|
| 1821 | - continue; |
|
| 1822 | - } catch (InvalidDataException $ex) { |
|
| 1823 | - $this->logger->error('Caught invalid data exception for calendar data. This usually indicates invalid calendar data. calendar-id:' . $calendarId . ' uri:' . $row['uri'], [ |
|
| 1824 | - 'app' => 'dav', |
|
| 1825 | - 'exception' => $ex, |
|
| 1826 | - ]); |
|
| 1827 | - continue; |
|
| 1828 | - } catch (MaxInstancesExceededException $ex) { |
|
| 1829 | - $this->logger->warning('Caught max instances exceeded exception for calendar data. This usually indicates too much recurring (more than 3500) event in calendar data. Object uri: ' . $row['uri'], [ |
|
| 1830 | - 'app' => 'dav', |
|
| 1831 | - 'exception' => $ex, |
|
| 1832 | - ]); |
|
| 1833 | - continue; |
|
| 1834 | - } |
|
| 1835 | - |
|
| 1836 | - if (!$matches) { |
|
| 1837 | - continue; |
|
| 1838 | - } |
|
| 1839 | - } |
|
| 1840 | - $result[] = $row['uri']; |
|
| 1841 | - $key = $calendarId . '::' . $row['uri'] . '::' . $calendarType; |
|
| 1842 | - $this->cachedObjects[$key] = $this->rowToCalendarObject($row); |
|
| 1843 | - } |
|
| 1844 | - |
|
| 1845 | - return $result; |
|
| 1846 | - } |
|
| 1847 | - |
|
| 1848 | - /** |
|
| 1849 | - * custom Nextcloud search extension for CalDAV |
|
| 1850 | - * |
|
| 1851 | - * TODO - this should optionally cover cached calendar objects as well |
|
| 1852 | - * |
|
| 1853 | - * @param string $principalUri |
|
| 1854 | - * @param array $filters |
|
| 1855 | - * @param integer|null $limit |
|
| 1856 | - * @param integer|null $offset |
|
| 1857 | - * @return array |
|
| 1858 | - */ |
|
| 1859 | - public function calendarSearch($principalUri, array $filters, $limit = null, $offset = null) { |
|
| 1860 | - return $this->atomic(function () use ($principalUri, $filters, $limit, $offset) { |
|
| 1861 | - $calendars = $this->getCalendarsForUser($principalUri); |
|
| 1862 | - $ownCalendars = []; |
|
| 1863 | - $sharedCalendars = []; |
|
| 1864 | - |
|
| 1865 | - $uriMapper = []; |
|
| 1866 | - |
|
| 1867 | - foreach ($calendars as $calendar) { |
|
| 1868 | - if ($calendar['{http://owncloud.org/ns}owner-principal'] === $principalUri) { |
|
| 1869 | - $ownCalendars[] = $calendar['id']; |
|
| 1870 | - } else { |
|
| 1871 | - $sharedCalendars[] = $calendar['id']; |
|
| 1872 | - } |
|
| 1873 | - $uriMapper[$calendar['id']] = $calendar['uri']; |
|
| 1874 | - } |
|
| 1875 | - if (count($ownCalendars) === 0 && count($sharedCalendars) === 0) { |
|
| 1876 | - return []; |
|
| 1877 | - } |
|
| 1878 | - |
|
| 1879 | - $query = $this->db->getQueryBuilder(); |
|
| 1880 | - // Calendar id expressions |
|
| 1881 | - $calendarExpressions = []; |
|
| 1882 | - foreach ($ownCalendars as $id) { |
|
| 1883 | - $calendarExpressions[] = $query->expr()->andX( |
|
| 1884 | - $query->expr()->eq('c.calendarid', |
|
| 1885 | - $query->createNamedParameter($id)), |
|
| 1886 | - $query->expr()->eq('c.calendartype', |
|
| 1887 | - $query->createNamedParameter(self::CALENDAR_TYPE_CALENDAR))); |
|
| 1888 | - } |
|
| 1889 | - foreach ($sharedCalendars as $id) { |
|
| 1890 | - $calendarExpressions[] = $query->expr()->andX( |
|
| 1891 | - $query->expr()->eq('c.calendarid', |
|
| 1892 | - $query->createNamedParameter($id)), |
|
| 1893 | - $query->expr()->eq('c.classification', |
|
| 1894 | - $query->createNamedParameter(self::CLASSIFICATION_PUBLIC)), |
|
| 1895 | - $query->expr()->eq('c.calendartype', |
|
| 1896 | - $query->createNamedParameter(self::CALENDAR_TYPE_CALENDAR))); |
|
| 1897 | - } |
|
| 1898 | - |
|
| 1899 | - if (count($calendarExpressions) === 1) { |
|
| 1900 | - $calExpr = $calendarExpressions[0]; |
|
| 1901 | - } else { |
|
| 1902 | - $calExpr = call_user_func_array([$query->expr(), 'orX'], $calendarExpressions); |
|
| 1903 | - } |
|
| 1904 | - |
|
| 1905 | - // Component expressions |
|
| 1906 | - $compExpressions = []; |
|
| 1907 | - foreach ($filters['comps'] as $comp) { |
|
| 1908 | - $compExpressions[] = $query->expr() |
|
| 1909 | - ->eq('c.componenttype', $query->createNamedParameter($comp)); |
|
| 1910 | - } |
|
| 1911 | - |
|
| 1912 | - if (count($compExpressions) === 1) { |
|
| 1913 | - $compExpr = $compExpressions[0]; |
|
| 1914 | - } else { |
|
| 1915 | - $compExpr = call_user_func_array([$query->expr(), 'orX'], $compExpressions); |
|
| 1916 | - } |
|
| 1917 | - |
|
| 1918 | - if (!isset($filters['props'])) { |
|
| 1919 | - $filters['props'] = []; |
|
| 1920 | - } |
|
| 1921 | - if (!isset($filters['params'])) { |
|
| 1922 | - $filters['params'] = []; |
|
| 1923 | - } |
|
| 1924 | - |
|
| 1925 | - $propParamExpressions = []; |
|
| 1926 | - foreach ($filters['props'] as $prop) { |
|
| 1927 | - $propParamExpressions[] = $query->expr()->andX( |
|
| 1928 | - $query->expr()->eq('i.name', $query->createNamedParameter($prop)), |
|
| 1929 | - $query->expr()->isNull('i.parameter') |
|
| 1930 | - ); |
|
| 1931 | - } |
|
| 1932 | - foreach ($filters['params'] as $param) { |
|
| 1933 | - $propParamExpressions[] = $query->expr()->andX( |
|
| 1934 | - $query->expr()->eq('i.name', $query->createNamedParameter($param['property'])), |
|
| 1935 | - $query->expr()->eq('i.parameter', $query->createNamedParameter($param['parameter'])) |
|
| 1936 | - ); |
|
| 1937 | - } |
|
| 1938 | - |
|
| 1939 | - if (count($propParamExpressions) === 1) { |
|
| 1940 | - $propParamExpr = $propParamExpressions[0]; |
|
| 1941 | - } else { |
|
| 1942 | - $propParamExpr = call_user_func_array([$query->expr(), 'orX'], $propParamExpressions); |
|
| 1943 | - } |
|
| 1944 | - |
|
| 1945 | - $query->select(['c.calendarid', 'c.uri']) |
|
| 1946 | - ->from($this->dbObjectPropertiesTable, 'i') |
|
| 1947 | - ->join('i', 'calendarobjects', 'c', $query->expr()->eq('i.objectid', 'c.id')) |
|
| 1948 | - ->where($calExpr) |
|
| 1949 | - ->andWhere($compExpr) |
|
| 1950 | - ->andWhere($propParamExpr) |
|
| 1951 | - ->andWhere($query->expr()->iLike('i.value', |
|
| 1952 | - $query->createNamedParameter('%' . $this->db->escapeLikeParameter($filters['search-term']) . '%'))) |
|
| 1953 | - ->andWhere($query->expr()->isNull('deleted_at')); |
|
| 1954 | - |
|
| 1955 | - if ($offset) { |
|
| 1956 | - $query->setFirstResult($offset); |
|
| 1957 | - } |
|
| 1958 | - if ($limit) { |
|
| 1959 | - $query->setMaxResults($limit); |
|
| 1960 | - } |
|
| 1961 | - |
|
| 1962 | - $stmt = $query->executeQuery(); |
|
| 1963 | - |
|
| 1964 | - $result = []; |
|
| 1965 | - while ($row = $stmt->fetch()) { |
|
| 1966 | - $path = $uriMapper[$row['calendarid']] . '/' . $row['uri']; |
|
| 1967 | - if (!in_array($path, $result)) { |
|
| 1968 | - $result[] = $path; |
|
| 1969 | - } |
|
| 1970 | - } |
|
| 1971 | - |
|
| 1972 | - return $result; |
|
| 1973 | - }, $this->db); |
|
| 1974 | - } |
|
| 1975 | - |
|
| 1976 | - /** |
|
| 1977 | - * used for Nextcloud's calendar API |
|
| 1978 | - * |
|
| 1979 | - * @param array $calendarInfo |
|
| 1980 | - * @param string $pattern |
|
| 1981 | - * @param array $searchProperties |
|
| 1982 | - * @param array $options |
|
| 1983 | - * @param integer|null $limit |
|
| 1984 | - * @param integer|null $offset |
|
| 1985 | - * |
|
| 1986 | - * @return array |
|
| 1987 | - */ |
|
| 1988 | - public function search( |
|
| 1989 | - array $calendarInfo, |
|
| 1990 | - $pattern, |
|
| 1991 | - array $searchProperties, |
|
| 1992 | - array $options, |
|
| 1993 | - $limit, |
|
| 1994 | - $offset, |
|
| 1995 | - ) { |
|
| 1996 | - $outerQuery = $this->db->getQueryBuilder(); |
|
| 1997 | - $innerQuery = $this->db->getQueryBuilder(); |
|
| 1998 | - |
|
| 1999 | - if (isset($calendarInfo['source'])) { |
|
| 2000 | - $calendarType = self::CALENDAR_TYPE_SUBSCRIPTION; |
|
| 2001 | - } else { |
|
| 2002 | - $calendarType = self::CALENDAR_TYPE_CALENDAR; |
|
| 2003 | - } |
|
| 2004 | - |
|
| 2005 | - $innerQuery->selectDistinct('op.objectid') |
|
| 2006 | - ->from($this->dbObjectPropertiesTable, 'op') |
|
| 2007 | - ->andWhere($innerQuery->expr()->eq('op.calendarid', |
|
| 2008 | - $outerQuery->createNamedParameter($calendarInfo['id']))) |
|
| 2009 | - ->andWhere($innerQuery->expr()->eq('op.calendartype', |
|
| 2010 | - $outerQuery->createNamedParameter($calendarType))); |
|
| 2011 | - |
|
| 2012 | - $outerQuery->select('c.id', 'c.calendardata', 'c.componenttype', 'c.uid', 'c.uri') |
|
| 2013 | - ->from('calendarobjects', 'c') |
|
| 2014 | - ->where($outerQuery->expr()->isNull('deleted_at')); |
|
| 2015 | - |
|
| 2016 | - // only return public items for shared calendars for now |
|
| 2017 | - if (isset($calendarInfo['{http://owncloud.org/ns}owner-principal']) === false || $calendarInfo['principaluri'] !== $calendarInfo['{http://owncloud.org/ns}owner-principal']) { |
|
| 2018 | - $outerQuery->andWhere($outerQuery->expr()->eq('c.classification', |
|
| 2019 | - $outerQuery->createNamedParameter(self::CLASSIFICATION_PUBLIC))); |
|
| 2020 | - } |
|
| 2021 | - |
|
| 2022 | - if (!empty($searchProperties)) { |
|
| 2023 | - $or = []; |
|
| 2024 | - foreach ($searchProperties as $searchProperty) { |
|
| 2025 | - $or[] = $innerQuery->expr()->eq('op.name', |
|
| 2026 | - $outerQuery->createNamedParameter($searchProperty)); |
|
| 2027 | - } |
|
| 2028 | - $innerQuery->andWhere($innerQuery->expr()->orX(...$or)); |
|
| 2029 | - } |
|
| 2030 | - |
|
| 2031 | - if ($pattern !== '') { |
|
| 2032 | - $innerQuery->andWhere($innerQuery->expr()->iLike('op.value', |
|
| 2033 | - $outerQuery->createNamedParameter('%' . |
|
| 2034 | - $this->db->escapeLikeParameter($pattern) . '%'))); |
|
| 2035 | - } |
|
| 2036 | - |
|
| 2037 | - $start = null; |
|
| 2038 | - $end = null; |
|
| 2039 | - |
|
| 2040 | - $hasLimit = is_int($limit); |
|
| 2041 | - $hasTimeRange = false; |
|
| 2042 | - |
|
| 2043 | - if (isset($options['timerange']['start']) && $options['timerange']['start'] instanceof DateTimeInterface) { |
|
| 2044 | - /** @var DateTimeInterface $start */ |
|
| 2045 | - $start = $options['timerange']['start']; |
|
| 2046 | - $outerQuery->andWhere( |
|
| 2047 | - $outerQuery->expr()->gt( |
|
| 2048 | - 'lastoccurence', |
|
| 2049 | - $outerQuery->createNamedParameter($start->getTimestamp()) |
|
| 2050 | - ) |
|
| 2051 | - ); |
|
| 2052 | - $hasTimeRange = true; |
|
| 2053 | - } |
|
| 2054 | - |
|
| 2055 | - if (isset($options['timerange']['end']) && $options['timerange']['end'] instanceof DateTimeInterface) { |
|
| 2056 | - /** @var DateTimeInterface $end */ |
|
| 2057 | - $end = $options['timerange']['end']; |
|
| 2058 | - $outerQuery->andWhere( |
|
| 2059 | - $outerQuery->expr()->lt( |
|
| 2060 | - 'firstoccurence', |
|
| 2061 | - $outerQuery->createNamedParameter($end->getTimestamp()) |
|
| 2062 | - ) |
|
| 2063 | - ); |
|
| 2064 | - $hasTimeRange = true; |
|
| 2065 | - } |
|
| 2066 | - |
|
| 2067 | - if (isset($options['uid'])) { |
|
| 2068 | - $outerQuery->andWhere($outerQuery->expr()->eq('uid', $outerQuery->createNamedParameter($options['uid']))); |
|
| 2069 | - } |
|
| 2070 | - |
|
| 2071 | - if (!empty($options['types'])) { |
|
| 2072 | - $or = []; |
|
| 2073 | - foreach ($options['types'] as $type) { |
|
| 2074 | - $or[] = $outerQuery->expr()->eq('componenttype', |
|
| 2075 | - $outerQuery->createNamedParameter($type)); |
|
| 2076 | - } |
|
| 2077 | - $outerQuery->andWhere($outerQuery->expr()->orX(...$or)); |
|
| 2078 | - } |
|
| 2079 | - |
|
| 2080 | - $outerQuery->andWhere($outerQuery->expr()->in('c.id', $outerQuery->createFunction($innerQuery->getSQL()))); |
|
| 2081 | - |
|
| 2082 | - // Without explicit order by its undefined in which order the SQL server returns the events. |
|
| 2083 | - // For the pagination with hasLimit and hasTimeRange, a stable ordering is helpful. |
|
| 2084 | - $outerQuery->addOrderBy('id'); |
|
| 2085 | - |
|
| 2086 | - $offset = (int)$offset; |
|
| 2087 | - $outerQuery->setFirstResult($offset); |
|
| 2088 | - |
|
| 2089 | - $calendarObjects = []; |
|
| 2090 | - |
|
| 2091 | - if ($hasLimit && $hasTimeRange) { |
|
| 2092 | - /** |
|
| 2093 | - * Event recurrences are evaluated at runtime because the database only knows the first and last occurrence. |
|
| 2094 | - * |
|
| 2095 | - * Given, a user created 8 events with a yearly reoccurrence and two for events tomorrow. |
|
| 2096 | - * The upcoming event widget asks the CalDAV backend for 7 events within the next 14 days. |
|
| 2097 | - * |
|
| 2098 | - * If limit 7 is applied to the SQL query, we find the 7 events with a yearly reoccurrence |
|
| 2099 | - * and discard the events after evaluating the reoccurrence rules because they are not due within |
|
| 2100 | - * the next 14 days and end up with an empty result even if there are two events to show. |
|
| 2101 | - * |
|
| 2102 | - * The workaround for search requests with a limit and time range is asking for more row than requested |
|
| 2103 | - * and retrying if we have not reached the limit. |
|
| 2104 | - * |
|
| 2105 | - * 25 rows and 3 retries is entirely arbitrary. |
|
| 2106 | - */ |
|
| 2107 | - $maxResults = (int)max($limit, 25); |
|
| 2108 | - $outerQuery->setMaxResults($maxResults); |
|
| 2109 | - |
|
| 2110 | - for ($attempt = $objectsCount = 0; $attempt < 3 && $objectsCount < $limit; $attempt++) { |
|
| 2111 | - $objectsCount = array_push($calendarObjects, ...$this->searchCalendarObjects($outerQuery, $start, $end)); |
|
| 2112 | - $outerQuery->setFirstResult($offset += $maxResults); |
|
| 2113 | - } |
|
| 2114 | - |
|
| 2115 | - $calendarObjects = array_slice($calendarObjects, 0, $limit, false); |
|
| 2116 | - } else { |
|
| 2117 | - $outerQuery->setMaxResults($limit); |
|
| 2118 | - $calendarObjects = $this->searchCalendarObjects($outerQuery, $start, $end); |
|
| 2119 | - } |
|
| 2120 | - |
|
| 2121 | - $calendarObjects = array_map(function ($o) use ($options) { |
|
| 2122 | - $calendarData = Reader::read($o['calendardata']); |
|
| 2123 | - |
|
| 2124 | - // Expand recurrences if an explicit time range is requested |
|
| 2125 | - if ($calendarData instanceof VCalendar |
|
| 2126 | - && isset($options['timerange']['start'], $options['timerange']['end'])) { |
|
| 2127 | - $calendarData = $calendarData->expand( |
|
| 2128 | - $options['timerange']['start'], |
|
| 2129 | - $options['timerange']['end'], |
|
| 2130 | - ); |
|
| 2131 | - } |
|
| 2132 | - |
|
| 2133 | - $comps = $calendarData->getComponents(); |
|
| 2134 | - $objects = []; |
|
| 2135 | - $timezones = []; |
|
| 2136 | - foreach ($comps as $comp) { |
|
| 2137 | - if ($comp instanceof VTimeZone) { |
|
| 2138 | - $timezones[] = $comp; |
|
| 2139 | - } else { |
|
| 2140 | - $objects[] = $comp; |
|
| 2141 | - } |
|
| 2142 | - } |
|
| 2143 | - |
|
| 2144 | - return [ |
|
| 2145 | - 'id' => $o['id'], |
|
| 2146 | - 'type' => $o['componenttype'], |
|
| 2147 | - 'uid' => $o['uid'], |
|
| 2148 | - 'uri' => $o['uri'], |
|
| 2149 | - 'objects' => array_map(function ($c) { |
|
| 2150 | - return $this->transformSearchData($c); |
|
| 2151 | - }, $objects), |
|
| 2152 | - 'timezones' => array_map(function ($c) { |
|
| 2153 | - return $this->transformSearchData($c); |
|
| 2154 | - }, $timezones), |
|
| 2155 | - ]; |
|
| 2156 | - }, $calendarObjects); |
|
| 2157 | - |
|
| 2158 | - usort($calendarObjects, function (array $a, array $b) { |
|
| 2159 | - /** @var DateTimeImmutable $startA */ |
|
| 2160 | - $startA = $a['objects'][0]['DTSTART'][0] ?? new DateTimeImmutable(self::MAX_DATE); |
|
| 2161 | - /** @var DateTimeImmutable $startB */ |
|
| 2162 | - $startB = $b['objects'][0]['DTSTART'][0] ?? new DateTimeImmutable(self::MAX_DATE); |
|
| 2163 | - |
|
| 2164 | - return $startA->getTimestamp() <=> $startB->getTimestamp(); |
|
| 2165 | - }); |
|
| 2166 | - |
|
| 2167 | - return $calendarObjects; |
|
| 2168 | - } |
|
| 2169 | - |
|
| 2170 | - private function searchCalendarObjects(IQueryBuilder $query, ?DateTimeInterface $start, ?DateTimeInterface $end): array { |
|
| 2171 | - $calendarObjects = []; |
|
| 2172 | - $filterByTimeRange = ($start instanceof DateTimeInterface) || ($end instanceof DateTimeInterface); |
|
| 2173 | - |
|
| 2174 | - $result = $query->executeQuery(); |
|
| 2175 | - |
|
| 2176 | - while (($row = $result->fetch()) !== false) { |
|
| 2177 | - if ($filterByTimeRange === false) { |
|
| 2178 | - // No filter required |
|
| 2179 | - $calendarObjects[] = $row; |
|
| 2180 | - continue; |
|
| 2181 | - } |
|
| 2182 | - |
|
| 2183 | - try { |
|
| 2184 | - $isValid = $this->validateFilterForObject($row, [ |
|
| 2185 | - 'name' => 'VCALENDAR', |
|
| 2186 | - 'comp-filters' => [ |
|
| 2187 | - [ |
|
| 2188 | - 'name' => 'VEVENT', |
|
| 2189 | - 'comp-filters' => [], |
|
| 2190 | - 'prop-filters' => [], |
|
| 2191 | - 'is-not-defined' => false, |
|
| 2192 | - 'time-range' => [ |
|
| 2193 | - 'start' => $start, |
|
| 2194 | - 'end' => $end, |
|
| 2195 | - ], |
|
| 2196 | - ], |
|
| 2197 | - ], |
|
| 2198 | - 'prop-filters' => [], |
|
| 2199 | - 'is-not-defined' => false, |
|
| 2200 | - 'time-range' => null, |
|
| 2201 | - ]); |
|
| 2202 | - } catch (MaxInstancesExceededException $ex) { |
|
| 2203 | - $this->logger->warning('Caught max instances exceeded exception for calendar data. This usually indicates too much recurring (more than 3500) event in calendar data. Object uri: ' . $row['uri'], [ |
|
| 2204 | - 'app' => 'dav', |
|
| 2205 | - 'exception' => $ex, |
|
| 2206 | - ]); |
|
| 2207 | - continue; |
|
| 2208 | - } |
|
| 2209 | - |
|
| 2210 | - if (is_resource($row['calendardata'])) { |
|
| 2211 | - // Put the stream back to the beginning so it can be read another time |
|
| 2212 | - rewind($row['calendardata']); |
|
| 2213 | - } |
|
| 2214 | - |
|
| 2215 | - if ($isValid) { |
|
| 2216 | - $calendarObjects[] = $row; |
|
| 2217 | - } |
|
| 2218 | - } |
|
| 2219 | - |
|
| 2220 | - $result->closeCursor(); |
|
| 2221 | - |
|
| 2222 | - return $calendarObjects; |
|
| 2223 | - } |
|
| 2224 | - |
|
| 2225 | - /** |
|
| 2226 | - * @param Component $comp |
|
| 2227 | - * @return array |
|
| 2228 | - */ |
|
| 2229 | - private function transformSearchData(Component $comp) { |
|
| 2230 | - $data = []; |
|
| 2231 | - /** @var Component[] $subComponents */ |
|
| 2232 | - $subComponents = $comp->getComponents(); |
|
| 2233 | - /** @var Property[] $properties */ |
|
| 2234 | - $properties = array_filter($comp->children(), function ($c) { |
|
| 2235 | - return $c instanceof Property; |
|
| 2236 | - }); |
|
| 2237 | - $validationRules = $comp->getValidationRules(); |
|
| 2238 | - |
|
| 2239 | - foreach ($subComponents as $subComponent) { |
|
| 2240 | - $name = $subComponent->name; |
|
| 2241 | - if (!isset($data[$name])) { |
|
| 2242 | - $data[$name] = []; |
|
| 2243 | - } |
|
| 2244 | - $data[$name][] = $this->transformSearchData($subComponent); |
|
| 2245 | - } |
|
| 2246 | - |
|
| 2247 | - foreach ($properties as $property) { |
|
| 2248 | - $name = $property->name; |
|
| 2249 | - if (!isset($validationRules[$name])) { |
|
| 2250 | - $validationRules[$name] = '*'; |
|
| 2251 | - } |
|
| 2252 | - |
|
| 2253 | - $rule = $validationRules[$property->name]; |
|
| 2254 | - if ($rule === '+' || $rule === '*') { // multiple |
|
| 2255 | - if (!isset($data[$name])) { |
|
| 2256 | - $data[$name] = []; |
|
| 2257 | - } |
|
| 2258 | - |
|
| 2259 | - $data[$name][] = $this->transformSearchProperty($property); |
|
| 2260 | - } else { // once |
|
| 2261 | - $data[$name] = $this->transformSearchProperty($property); |
|
| 2262 | - } |
|
| 2263 | - } |
|
| 2264 | - |
|
| 2265 | - return $data; |
|
| 2266 | - } |
|
| 2267 | - |
|
| 2268 | - /** |
|
| 2269 | - * @param Property $prop |
|
| 2270 | - * @return array |
|
| 2271 | - */ |
|
| 2272 | - private function transformSearchProperty(Property $prop) { |
|
| 2273 | - // No need to check Date, as it extends DateTime |
|
| 2274 | - if ($prop instanceof Property\ICalendar\DateTime) { |
|
| 2275 | - $value = $prop->getDateTime(); |
|
| 2276 | - } else { |
|
| 2277 | - $value = $prop->getValue(); |
|
| 2278 | - } |
|
| 2279 | - |
|
| 2280 | - return [ |
|
| 2281 | - $value, |
|
| 2282 | - $prop->parameters() |
|
| 2283 | - ]; |
|
| 2284 | - } |
|
| 2285 | - |
|
| 2286 | - /** |
|
| 2287 | - * @param string $principalUri |
|
| 2288 | - * @param string $pattern |
|
| 2289 | - * @param array $componentTypes |
|
| 2290 | - * @param array $searchProperties |
|
| 2291 | - * @param array $searchParameters |
|
| 2292 | - * @param array $options |
|
| 2293 | - * @return array |
|
| 2294 | - */ |
|
| 2295 | - public function searchPrincipalUri(string $principalUri, |
|
| 2296 | - string $pattern, |
|
| 2297 | - array $componentTypes, |
|
| 2298 | - array $searchProperties, |
|
| 2299 | - array $searchParameters, |
|
| 2300 | - array $options = [], |
|
| 2301 | - ): array { |
|
| 2302 | - return $this->atomic(function () use ($principalUri, $pattern, $componentTypes, $searchProperties, $searchParameters, $options) { |
|
| 2303 | - $escapePattern = !\array_key_exists('escape_like_param', $options) || $options['escape_like_param'] !== false; |
|
| 2304 | - |
|
| 2305 | - $calendarObjectIdQuery = $this->db->getQueryBuilder(); |
|
| 2306 | - $calendarOr = []; |
|
| 2307 | - $searchOr = []; |
|
| 2308 | - |
|
| 2309 | - // Fetch calendars and subscription |
|
| 2310 | - $calendars = $this->getCalendarsForUser($principalUri); |
|
| 2311 | - $subscriptions = $this->getSubscriptionsForUser($principalUri); |
|
| 2312 | - foreach ($calendars as $calendar) { |
|
| 2313 | - $calendarAnd = $calendarObjectIdQuery->expr()->andX( |
|
| 2314 | - $calendarObjectIdQuery->expr()->eq('cob.calendarid', $calendarObjectIdQuery->createNamedParameter((int)$calendar['id'])), |
|
| 2315 | - $calendarObjectIdQuery->expr()->eq('cob.calendartype', $calendarObjectIdQuery->createNamedParameter(self::CALENDAR_TYPE_CALENDAR)), |
|
| 2316 | - ); |
|
| 2317 | - |
|
| 2318 | - // If it's shared, limit search to public events |
|
| 2319 | - if (isset($calendar['{http://owncloud.org/ns}owner-principal']) |
|
| 2320 | - && $calendar['principaluri'] !== $calendar['{http://owncloud.org/ns}owner-principal']) { |
|
| 2321 | - $calendarAnd->add($calendarObjectIdQuery->expr()->eq('co.classification', $calendarObjectIdQuery->createNamedParameter(self::CLASSIFICATION_PUBLIC))); |
|
| 2322 | - } |
|
| 2323 | - |
|
| 2324 | - $calendarOr[] = $calendarAnd; |
|
| 2325 | - } |
|
| 2326 | - foreach ($subscriptions as $subscription) { |
|
| 2327 | - $subscriptionAnd = $calendarObjectIdQuery->expr()->andX( |
|
| 2328 | - $calendarObjectIdQuery->expr()->eq('cob.calendarid', $calendarObjectIdQuery->createNamedParameter((int)$subscription['id'])), |
|
| 2329 | - $calendarObjectIdQuery->expr()->eq('cob.calendartype', $calendarObjectIdQuery->createNamedParameter(self::CALENDAR_TYPE_SUBSCRIPTION)), |
|
| 2330 | - ); |
|
| 2331 | - |
|
| 2332 | - // If it's shared, limit search to public events |
|
| 2333 | - if (isset($subscription['{http://owncloud.org/ns}owner-principal']) |
|
| 2334 | - && $subscription['principaluri'] !== $subscription['{http://owncloud.org/ns}owner-principal']) { |
|
| 2335 | - $subscriptionAnd->add($calendarObjectIdQuery->expr()->eq('co.classification', $calendarObjectIdQuery->createNamedParameter(self::CLASSIFICATION_PUBLIC))); |
|
| 2336 | - } |
|
| 2337 | - |
|
| 2338 | - $calendarOr[] = $subscriptionAnd; |
|
| 2339 | - } |
|
| 2340 | - |
|
| 2341 | - foreach ($searchProperties as $property) { |
|
| 2342 | - $propertyAnd = $calendarObjectIdQuery->expr()->andX( |
|
| 2343 | - $calendarObjectIdQuery->expr()->eq('cob.name', $calendarObjectIdQuery->createNamedParameter($property, IQueryBuilder::PARAM_STR)), |
|
| 2344 | - $calendarObjectIdQuery->expr()->isNull('cob.parameter'), |
|
| 2345 | - ); |
|
| 2346 | - |
|
| 2347 | - $searchOr[] = $propertyAnd; |
|
| 2348 | - } |
|
| 2349 | - foreach ($searchParameters as $property => $parameter) { |
|
| 2350 | - $parameterAnd = $calendarObjectIdQuery->expr()->andX( |
|
| 2351 | - $calendarObjectIdQuery->expr()->eq('cob.name', $calendarObjectIdQuery->createNamedParameter($property, IQueryBuilder::PARAM_STR)), |
|
| 2352 | - $calendarObjectIdQuery->expr()->eq('cob.parameter', $calendarObjectIdQuery->createNamedParameter($parameter, IQueryBuilder::PARAM_STR_ARRAY)), |
|
| 2353 | - ); |
|
| 2354 | - |
|
| 2355 | - $searchOr[] = $parameterAnd; |
|
| 2356 | - } |
|
| 2357 | - |
|
| 2358 | - if (empty($calendarOr)) { |
|
| 2359 | - return []; |
|
| 2360 | - } |
|
| 2361 | - if (empty($searchOr)) { |
|
| 2362 | - return []; |
|
| 2363 | - } |
|
| 2364 | - |
|
| 2365 | - $calendarObjectIdQuery->selectDistinct('cob.objectid') |
|
| 2366 | - ->from($this->dbObjectPropertiesTable, 'cob') |
|
| 2367 | - ->leftJoin('cob', 'calendarobjects', 'co', $calendarObjectIdQuery->expr()->eq('co.id', 'cob.objectid')) |
|
| 2368 | - ->andWhere($calendarObjectIdQuery->expr()->in('co.componenttype', $calendarObjectIdQuery->createNamedParameter($componentTypes, IQueryBuilder::PARAM_STR_ARRAY))) |
|
| 2369 | - ->andWhere($calendarObjectIdQuery->expr()->orX(...$calendarOr)) |
|
| 2370 | - ->andWhere($calendarObjectIdQuery->expr()->orX(...$searchOr)) |
|
| 2371 | - ->andWhere($calendarObjectIdQuery->expr()->isNull('deleted_at')); |
|
| 2372 | - |
|
| 2373 | - if ($pattern !== '') { |
|
| 2374 | - if (!$escapePattern) { |
|
| 2375 | - $calendarObjectIdQuery->andWhere($calendarObjectIdQuery->expr()->ilike('cob.value', $calendarObjectIdQuery->createNamedParameter($pattern))); |
|
| 2376 | - } else { |
|
| 2377 | - $calendarObjectIdQuery->andWhere($calendarObjectIdQuery->expr()->ilike('cob.value', $calendarObjectIdQuery->createNamedParameter('%' . $this->db->escapeLikeParameter($pattern) . '%'))); |
|
| 2378 | - } |
|
| 2379 | - } |
|
| 2380 | - |
|
| 2381 | - if (isset($options['limit'])) { |
|
| 2382 | - $calendarObjectIdQuery->setMaxResults($options['limit']); |
|
| 2383 | - } |
|
| 2384 | - if (isset($options['offset'])) { |
|
| 2385 | - $calendarObjectIdQuery->setFirstResult($options['offset']); |
|
| 2386 | - } |
|
| 2387 | - if (isset($options['timerange'])) { |
|
| 2388 | - if (isset($options['timerange']['start']) && $options['timerange']['start'] instanceof DateTimeInterface) { |
|
| 2389 | - $calendarObjectIdQuery->andWhere($calendarObjectIdQuery->expr()->gt( |
|
| 2390 | - 'lastoccurence', |
|
| 2391 | - $calendarObjectIdQuery->createNamedParameter($options['timerange']['start']->getTimeStamp()), |
|
| 2392 | - )); |
|
| 2393 | - } |
|
| 2394 | - if (isset($options['timerange']['end']) && $options['timerange']['end'] instanceof DateTimeInterface) { |
|
| 2395 | - $calendarObjectIdQuery->andWhere($calendarObjectIdQuery->expr()->lt( |
|
| 2396 | - 'firstoccurence', |
|
| 2397 | - $calendarObjectIdQuery->createNamedParameter($options['timerange']['end']->getTimeStamp()), |
|
| 2398 | - )); |
|
| 2399 | - } |
|
| 2400 | - } |
|
| 2401 | - |
|
| 2402 | - $result = $calendarObjectIdQuery->executeQuery(); |
|
| 2403 | - $matches = []; |
|
| 2404 | - while (($row = $result->fetch()) !== false) { |
|
| 2405 | - $matches[] = (int)$row['objectid']; |
|
| 2406 | - } |
|
| 2407 | - $result->closeCursor(); |
|
| 2408 | - |
|
| 2409 | - $query = $this->db->getQueryBuilder(); |
|
| 2410 | - $query->select('calendardata', 'uri', 'calendarid', 'calendartype') |
|
| 2411 | - ->from('calendarobjects') |
|
| 2412 | - ->where($query->expr()->in('id', $query->createNamedParameter($matches, IQueryBuilder::PARAM_INT_ARRAY))); |
|
| 2413 | - |
|
| 2414 | - $result = $query->executeQuery(); |
|
| 2415 | - $calendarObjects = []; |
|
| 2416 | - while (($array = $result->fetch()) !== false) { |
|
| 2417 | - $array['calendarid'] = (int)$array['calendarid']; |
|
| 2418 | - $array['calendartype'] = (int)$array['calendartype']; |
|
| 2419 | - $array['calendardata'] = $this->readBlob($array['calendardata']); |
|
| 2420 | - |
|
| 2421 | - $calendarObjects[] = $array; |
|
| 2422 | - } |
|
| 2423 | - $result->closeCursor(); |
|
| 2424 | - return $calendarObjects; |
|
| 2425 | - }, $this->db); |
|
| 2426 | - } |
|
| 2427 | - |
|
| 2428 | - /** |
|
| 2429 | - * Searches through all of a users calendars and calendar objects to find |
|
| 2430 | - * an object with a specific UID. |
|
| 2431 | - * |
|
| 2432 | - * This method should return the path to this object, relative to the |
|
| 2433 | - * calendar home, so this path usually only contains two parts: |
|
| 2434 | - * |
|
| 2435 | - * calendarpath/objectpath.ics |
|
| 2436 | - * |
|
| 2437 | - * If the uid is not found, return null. |
|
| 2438 | - * |
|
| 2439 | - * This method should only consider * objects that the principal owns, so |
|
| 2440 | - * any calendars owned by other principals that also appear in this |
|
| 2441 | - * collection should be ignored. |
|
| 2442 | - * |
|
| 2443 | - * @param string $principalUri |
|
| 2444 | - * @param string $uid |
|
| 2445 | - * @return string|null |
|
| 2446 | - */ |
|
| 2447 | - public function getCalendarObjectByUID($principalUri, $uid) { |
|
| 2448 | - $query = $this->db->getQueryBuilder(); |
|
| 2449 | - $query->selectAlias('c.uri', 'calendaruri')->selectAlias('co.uri', 'objecturi') |
|
| 2450 | - ->from('calendarobjects', 'co') |
|
| 2451 | - ->leftJoin('co', 'calendars', 'c', $query->expr()->eq('co.calendarid', 'c.id')) |
|
| 2452 | - ->where($query->expr()->eq('c.principaluri', $query->createNamedParameter($principalUri))) |
|
| 2453 | - ->andWhere($query->expr()->eq('co.uid', $query->createNamedParameter($uid))) |
|
| 2454 | - ->andWhere($query->expr()->isNull('co.deleted_at')); |
|
| 2455 | - $stmt = $query->executeQuery(); |
|
| 2456 | - $row = $stmt->fetch(); |
|
| 2457 | - $stmt->closeCursor(); |
|
| 2458 | - if ($row) { |
|
| 2459 | - return $row['calendaruri'] . '/' . $row['objecturi']; |
|
| 2460 | - } |
|
| 2461 | - |
|
| 2462 | - return null; |
|
| 2463 | - } |
|
| 2464 | - |
|
| 2465 | - public function getCalendarObjectById(string $principalUri, int $id): ?array { |
|
| 2466 | - $query = $this->db->getQueryBuilder(); |
|
| 2467 | - $query->select(['co.id', 'co.uri', 'co.lastmodified', 'co.etag', 'co.calendarid', 'co.size', 'co.calendardata', 'co.componenttype', 'co.classification', 'co.deleted_at']) |
|
| 2468 | - ->selectAlias('c.uri', 'calendaruri') |
|
| 2469 | - ->from('calendarobjects', 'co') |
|
| 2470 | - ->join('co', 'calendars', 'c', $query->expr()->eq('c.id', 'co.calendarid', IQueryBuilder::PARAM_INT)) |
|
| 2471 | - ->where($query->expr()->eq('c.principaluri', $query->createNamedParameter($principalUri))) |
|
| 2472 | - ->andWhere($query->expr()->eq('co.id', $query->createNamedParameter($id, IQueryBuilder::PARAM_INT), IQueryBuilder::PARAM_INT)); |
|
| 2473 | - $stmt = $query->executeQuery(); |
|
| 2474 | - $row = $stmt->fetch(); |
|
| 2475 | - $stmt->closeCursor(); |
|
| 2476 | - |
|
| 2477 | - if (!$row) { |
|
| 2478 | - return null; |
|
| 2479 | - } |
|
| 2480 | - |
|
| 2481 | - return [ |
|
| 2482 | - 'id' => $row['id'], |
|
| 2483 | - 'uri' => $row['uri'], |
|
| 2484 | - 'lastmodified' => $row['lastmodified'], |
|
| 2485 | - 'etag' => '"' . $row['etag'] . '"', |
|
| 2486 | - 'calendarid' => $row['calendarid'], |
|
| 2487 | - 'calendaruri' => $row['calendaruri'], |
|
| 2488 | - 'size' => (int)$row['size'], |
|
| 2489 | - 'calendardata' => $this->readBlob($row['calendardata']), |
|
| 2490 | - 'component' => strtolower($row['componenttype']), |
|
| 2491 | - 'classification' => (int)$row['classification'], |
|
| 2492 | - 'deleted_at' => isset($row['deleted_at']) ? ((int)$row['deleted_at']) : null, |
|
| 2493 | - ]; |
|
| 2494 | - } |
|
| 2495 | - |
|
| 2496 | - /** |
|
| 2497 | - * The getChanges method returns all the changes that have happened, since |
|
| 2498 | - * the specified syncToken in the specified calendar. |
|
| 2499 | - * |
|
| 2500 | - * This function should return an array, such as the following: |
|
| 2501 | - * |
|
| 2502 | - * [ |
|
| 2503 | - * 'syncToken' => 'The current synctoken', |
|
| 2504 | - * 'added' => [ |
|
| 2505 | - * 'new.txt', |
|
| 2506 | - * ], |
|
| 2507 | - * 'modified' => [ |
|
| 2508 | - * 'modified.txt', |
|
| 2509 | - * ], |
|
| 2510 | - * 'deleted' => [ |
|
| 2511 | - * 'foo.php.bak', |
|
| 2512 | - * 'old.txt' |
|
| 2513 | - * ] |
|
| 2514 | - * ); |
|
| 2515 | - * |
|
| 2516 | - * The returned syncToken property should reflect the *current* syncToken |
|
| 2517 | - * of the calendar, as reported in the {http://sabredav.org/ns}sync-token |
|
| 2518 | - * property This is * needed here too, to ensure the operation is atomic. |
|
| 2519 | - * |
|
| 2520 | - * If the $syncToken argument is specified as null, this is an initial |
|
| 2521 | - * sync, and all members should be reported. |
|
| 2522 | - * |
|
| 2523 | - * The modified property is an array of nodenames that have changed since |
|
| 2524 | - * the last token. |
|
| 2525 | - * |
|
| 2526 | - * The deleted property is an array with nodenames, that have been deleted |
|
| 2527 | - * from collection. |
|
| 2528 | - * |
|
| 2529 | - * The $syncLevel argument is basically the 'depth' of the report. If it's |
|
| 2530 | - * 1, you only have to report changes that happened only directly in |
|
| 2531 | - * immediate descendants. If it's 2, it should also include changes from |
|
| 2532 | - * the nodes below the child collections. (grandchildren) |
|
| 2533 | - * |
|
| 2534 | - * The $limit argument allows a client to specify how many results should |
|
| 2535 | - * be returned at most. If the limit is not specified, it should be treated |
|
| 2536 | - * as infinite. |
|
| 2537 | - * |
|
| 2538 | - * If the limit (infinite or not) is higher than you're willing to return, |
|
| 2539 | - * you should throw a Sabre\DAV\Exception\TooMuchMatches() exception. |
|
| 2540 | - * |
|
| 2541 | - * If the syncToken is expired (due to data cleanup) or unknown, you must |
|
| 2542 | - * return null. |
|
| 2543 | - * |
|
| 2544 | - * The limit is 'suggestive'. You are free to ignore it. |
|
| 2545 | - * |
|
| 2546 | - * @param string $calendarId |
|
| 2547 | - * @param string $syncToken |
|
| 2548 | - * @param int $syncLevel |
|
| 2549 | - * @param int|null $limit |
|
| 2550 | - * @param int $calendarType |
|
| 2551 | - * @return ?array |
|
| 2552 | - */ |
|
| 2553 | - public function getChangesForCalendar($calendarId, $syncToken, $syncLevel, $limit = null, $calendarType = self::CALENDAR_TYPE_CALENDAR) { |
|
| 2554 | - $table = $calendarType === self::CALENDAR_TYPE_CALENDAR ? 'calendars': 'calendarsubscriptions'; |
|
| 2555 | - |
|
| 2556 | - return $this->atomic(function () use ($calendarId, $syncToken, $syncLevel, $limit, $calendarType, $table) { |
|
| 2557 | - // Current synctoken |
|
| 2558 | - $qb = $this->db->getQueryBuilder(); |
|
| 2559 | - $qb->select('synctoken') |
|
| 2560 | - ->from($table) |
|
| 2561 | - ->where( |
|
| 2562 | - $qb->expr()->eq('id', $qb->createNamedParameter($calendarId)) |
|
| 2563 | - ); |
|
| 2564 | - $stmt = $qb->executeQuery(); |
|
| 2565 | - $currentToken = $stmt->fetchOne(); |
|
| 2566 | - $initialSync = !is_numeric($syncToken); |
|
| 2567 | - |
|
| 2568 | - if ($currentToken === false) { |
|
| 2569 | - return null; |
|
| 2570 | - } |
|
| 2571 | - |
|
| 2572 | - // evaluate if this is a initial sync and construct appropriate command |
|
| 2573 | - if ($initialSync) { |
|
| 2574 | - $qb = $this->db->getQueryBuilder(); |
|
| 2575 | - $qb->select('uri') |
|
| 2576 | - ->from('calendarobjects') |
|
| 2577 | - ->where($qb->expr()->eq('calendarid', $qb->createNamedParameter($calendarId))) |
|
| 2578 | - ->andWhere($qb->expr()->eq('calendartype', $qb->createNamedParameter($calendarType))) |
|
| 2579 | - ->andWhere($qb->expr()->isNull('deleted_at')); |
|
| 2580 | - } else { |
|
| 2581 | - $qb = $this->db->getQueryBuilder(); |
|
| 2582 | - $qb->select('uri', $qb->func()->max('operation')) |
|
| 2583 | - ->from('calendarchanges') |
|
| 2584 | - ->where($qb->expr()->eq('calendarid', $qb->createNamedParameter($calendarId))) |
|
| 2585 | - ->andWhere($qb->expr()->eq('calendartype', $qb->createNamedParameter($calendarType))) |
|
| 2586 | - ->andWhere($qb->expr()->gte('synctoken', $qb->createNamedParameter($syncToken))) |
|
| 2587 | - ->andWhere($qb->expr()->lt('synctoken', $qb->createNamedParameter($currentToken))) |
|
| 2588 | - ->groupBy('uri'); |
|
| 2589 | - } |
|
| 2590 | - // evaluate if limit exists |
|
| 2591 | - if (is_numeric($limit)) { |
|
| 2592 | - $qb->setMaxResults($limit); |
|
| 2593 | - } |
|
| 2594 | - // execute command |
|
| 2595 | - $stmt = $qb->executeQuery(); |
|
| 2596 | - // build results |
|
| 2597 | - $result = ['syncToken' => $currentToken, 'added' => [], 'modified' => [], 'deleted' => []]; |
|
| 2598 | - // retrieve results |
|
| 2599 | - if ($initialSync) { |
|
| 2600 | - $result['added'] = $stmt->fetchAll(\PDO::FETCH_COLUMN); |
|
| 2601 | - } else { |
|
| 2602 | - // \PDO::FETCH_NUM is needed due to the inconsistent field names |
|
| 2603 | - // produced by doctrine for MAX() with different databases |
|
| 2604 | - while ($entry = $stmt->fetch(\PDO::FETCH_NUM)) { |
|
| 2605 | - // assign uri (column 0) to appropriate mutation based on operation (column 1) |
|
| 2606 | - // forced (int) is needed as doctrine with OCI returns the operation field as string not integer |
|
| 2607 | - match ((int)$entry[1]) { |
|
| 2608 | - 1 => $result['added'][] = $entry[0], |
|
| 2609 | - 2 => $result['modified'][] = $entry[0], |
|
| 2610 | - 3 => $result['deleted'][] = $entry[0], |
|
| 2611 | - default => $this->logger->debug('Unknown calendar change operation detected') |
|
| 2612 | - }; |
|
| 2613 | - } |
|
| 2614 | - } |
|
| 2615 | - $stmt->closeCursor(); |
|
| 2616 | - |
|
| 2617 | - return $result; |
|
| 2618 | - }, $this->db); |
|
| 2619 | - } |
|
| 2620 | - |
|
| 2621 | - /** |
|
| 2622 | - * Returns a list of subscriptions for a principal. |
|
| 2623 | - * |
|
| 2624 | - * Every subscription is an array with the following keys: |
|
| 2625 | - * * id, a unique id that will be used by other functions to modify the |
|
| 2626 | - * subscription. This can be the same as the uri or a database key. |
|
| 2627 | - * * uri. This is just the 'base uri' or 'filename' of the subscription. |
|
| 2628 | - * * principaluri. The owner of the subscription. Almost always the same as |
|
| 2629 | - * principalUri passed to this method. |
|
| 2630 | - * |
|
| 2631 | - * Furthermore, all the subscription info must be returned too: |
|
| 2632 | - * |
|
| 2633 | - * 1. {DAV:}displayname |
|
| 2634 | - * 2. {http://apple.com/ns/ical/}refreshrate |
|
| 2635 | - * 3. {http://calendarserver.org/ns/}subscribed-strip-todos (omit if todos |
|
| 2636 | - * should not be stripped). |
|
| 2637 | - * 4. {http://calendarserver.org/ns/}subscribed-strip-alarms (omit if alarms |
|
| 2638 | - * should not be stripped). |
|
| 2639 | - * 5. {http://calendarserver.org/ns/}subscribed-strip-attachments (omit if |
|
| 2640 | - * attachments should not be stripped). |
|
| 2641 | - * 6. {http://calendarserver.org/ns/}source (Must be a |
|
| 2642 | - * Sabre\DAV\Property\Href). |
|
| 2643 | - * 7. {http://apple.com/ns/ical/}calendar-color |
|
| 2644 | - * 8. {http://apple.com/ns/ical/}calendar-order |
|
| 2645 | - * 9. {urn:ietf:params:xml:ns:caldav}supported-calendar-component-set |
|
| 2646 | - * (should just be an instance of |
|
| 2647 | - * Sabre\CalDAV\Property\SupportedCalendarComponentSet, with a bunch of |
|
| 2648 | - * default components). |
|
| 2649 | - * |
|
| 2650 | - * @param string $principalUri |
|
| 2651 | - * @return array |
|
| 2652 | - */ |
|
| 2653 | - public function getSubscriptionsForUser($principalUri) { |
|
| 2654 | - $fields = array_column($this->subscriptionPropertyMap, 0); |
|
| 2655 | - $fields[] = 'id'; |
|
| 2656 | - $fields[] = 'uri'; |
|
| 2657 | - $fields[] = 'source'; |
|
| 2658 | - $fields[] = 'principaluri'; |
|
| 2659 | - $fields[] = 'lastmodified'; |
|
| 2660 | - $fields[] = 'synctoken'; |
|
| 2661 | - |
|
| 2662 | - $query = $this->db->getQueryBuilder(); |
|
| 2663 | - $query->select($fields) |
|
| 2664 | - ->from('calendarsubscriptions') |
|
| 2665 | - ->where($query->expr()->eq('principaluri', $query->createNamedParameter($principalUri))) |
|
| 2666 | - ->orderBy('calendarorder', 'asc'); |
|
| 2667 | - $stmt = $query->executeQuery(); |
|
| 2668 | - |
|
| 2669 | - $subscriptions = []; |
|
| 2670 | - while ($row = $stmt->fetch()) { |
|
| 2671 | - $subscription = [ |
|
| 2672 | - 'id' => $row['id'], |
|
| 2673 | - 'uri' => $row['uri'], |
|
| 2674 | - 'principaluri' => $row['principaluri'], |
|
| 2675 | - 'source' => $row['source'], |
|
| 2676 | - 'lastmodified' => $row['lastmodified'], |
|
| 2677 | - |
|
| 2678 | - '{' . Plugin::NS_CALDAV . '}supported-calendar-component-set' => new SupportedCalendarComponentSet(['VTODO', 'VEVENT']), |
|
| 2679 | - '{http://sabredav.org/ns}sync-token' => $row['synctoken'] ?: '0', |
|
| 2680 | - ]; |
|
| 2681 | - |
|
| 2682 | - $subscriptions[] = $this->rowToSubscription($row, $subscription); |
|
| 2683 | - } |
|
| 2684 | - |
|
| 2685 | - return $subscriptions; |
|
| 2686 | - } |
|
| 2687 | - |
|
| 2688 | - /** |
|
| 2689 | - * Creates a new subscription for a principal. |
|
| 2690 | - * |
|
| 2691 | - * If the creation was a success, an id must be returned that can be used to reference |
|
| 2692 | - * this subscription in other methods, such as updateSubscription. |
|
| 2693 | - * |
|
| 2694 | - * @param string $principalUri |
|
| 2695 | - * @param string $uri |
|
| 2696 | - * @param array $properties |
|
| 2697 | - * @return mixed |
|
| 2698 | - */ |
|
| 2699 | - public function createSubscription($principalUri, $uri, array $properties) { |
|
| 2700 | - if (!isset($properties['{http://calendarserver.org/ns/}source'])) { |
|
| 2701 | - throw new Forbidden('The {http://calendarserver.org/ns/}source property is required when creating subscriptions'); |
|
| 2702 | - } |
|
| 2703 | - |
|
| 2704 | - $values = [ |
|
| 2705 | - 'principaluri' => $principalUri, |
|
| 2706 | - 'uri' => $uri, |
|
| 2707 | - 'source' => $properties['{http://calendarserver.org/ns/}source']->getHref(), |
|
| 2708 | - 'lastmodified' => time(), |
|
| 2709 | - ]; |
|
| 2710 | - |
|
| 2711 | - $propertiesBoolean = ['striptodos', 'stripalarms', 'stripattachments']; |
|
| 2712 | - |
|
| 2713 | - foreach ($this->subscriptionPropertyMap as $xmlName => [$dbName, $type]) { |
|
| 2714 | - if (array_key_exists($xmlName, $properties)) { |
|
| 2715 | - $values[$dbName] = $properties[$xmlName]; |
|
| 2716 | - if (in_array($dbName, $propertiesBoolean)) { |
|
| 2717 | - $values[$dbName] = true; |
|
| 2718 | - } |
|
| 2719 | - } |
|
| 2720 | - } |
|
| 2721 | - |
|
| 2722 | - [$subscriptionId, $subscriptionRow] = $this->atomic(function () use ($values) { |
|
| 2723 | - $valuesToInsert = []; |
|
| 2724 | - $query = $this->db->getQueryBuilder(); |
|
| 2725 | - foreach (array_keys($values) as $name) { |
|
| 2726 | - $valuesToInsert[$name] = $query->createNamedParameter($values[$name]); |
|
| 2727 | - } |
|
| 2728 | - $query->insert('calendarsubscriptions') |
|
| 2729 | - ->values($valuesToInsert) |
|
| 2730 | - ->executeStatement(); |
|
| 2731 | - |
|
| 2732 | - $subscriptionId = $query->getLastInsertId(); |
|
| 2733 | - |
|
| 2734 | - $subscriptionRow = $this->getSubscriptionById($subscriptionId); |
|
| 2735 | - return [$subscriptionId, $subscriptionRow]; |
|
| 2736 | - }, $this->db); |
|
| 2737 | - |
|
| 2738 | - $this->dispatcher->dispatchTyped(new SubscriptionCreatedEvent($subscriptionId, $subscriptionRow)); |
|
| 2739 | - |
|
| 2740 | - return $subscriptionId; |
|
| 2741 | - } |
|
| 2742 | - |
|
| 2743 | - /** |
|
| 2744 | - * Updates a subscription |
|
| 2745 | - * |
|
| 2746 | - * The list of mutations is stored in a Sabre\DAV\PropPatch object. |
|
| 2747 | - * To do the actual updates, you must tell this object which properties |
|
| 2748 | - * you're going to process with the handle() method. |
|
| 2749 | - * |
|
| 2750 | - * Calling the handle method is like telling the PropPatch object "I |
|
| 2751 | - * promise I can handle updating this property". |
|
| 2752 | - * |
|
| 2753 | - * Read the PropPatch documentation for more info and examples. |
|
| 2754 | - * |
|
| 2755 | - * @param mixed $subscriptionId |
|
| 2756 | - * @param PropPatch $propPatch |
|
| 2757 | - * @return void |
|
| 2758 | - */ |
|
| 2759 | - public function updateSubscription($subscriptionId, PropPatch $propPatch) { |
|
| 2760 | - $supportedProperties = array_keys($this->subscriptionPropertyMap); |
|
| 2761 | - $supportedProperties[] = '{http://calendarserver.org/ns/}source'; |
|
| 2762 | - |
|
| 2763 | - $propPatch->handle($supportedProperties, function ($mutations) use ($subscriptionId) { |
|
| 2764 | - $newValues = []; |
|
| 2765 | - |
|
| 2766 | - foreach ($mutations as $propertyName => $propertyValue) { |
|
| 2767 | - if ($propertyName === '{http://calendarserver.org/ns/}source') { |
|
| 2768 | - $newValues['source'] = $propertyValue->getHref(); |
|
| 2769 | - } else { |
|
| 2770 | - $fieldName = $this->subscriptionPropertyMap[$propertyName][0]; |
|
| 2771 | - $newValues[$fieldName] = $propertyValue; |
|
| 2772 | - } |
|
| 2773 | - } |
|
| 2774 | - |
|
| 2775 | - $subscriptionRow = $this->atomic(function () use ($subscriptionId, $newValues) { |
|
| 2776 | - $query = $this->db->getQueryBuilder(); |
|
| 2777 | - $query->update('calendarsubscriptions') |
|
| 2778 | - ->set('lastmodified', $query->createNamedParameter(time())); |
|
| 2779 | - foreach ($newValues as $fieldName => $value) { |
|
| 2780 | - $query->set($fieldName, $query->createNamedParameter($value)); |
|
| 2781 | - } |
|
| 2782 | - $query->where($query->expr()->eq('id', $query->createNamedParameter($subscriptionId))) |
|
| 2783 | - ->executeStatement(); |
|
| 2784 | - |
|
| 2785 | - return $this->getSubscriptionById($subscriptionId); |
|
| 2786 | - }, $this->db); |
|
| 2787 | - |
|
| 2788 | - $this->dispatcher->dispatchTyped(new SubscriptionUpdatedEvent((int)$subscriptionId, $subscriptionRow, [], $mutations)); |
|
| 2789 | - |
|
| 2790 | - return true; |
|
| 2791 | - }); |
|
| 2792 | - } |
|
| 2793 | - |
|
| 2794 | - /** |
|
| 2795 | - * Deletes a subscription. |
|
| 2796 | - * |
|
| 2797 | - * @param mixed $subscriptionId |
|
| 2798 | - * @return void |
|
| 2799 | - */ |
|
| 2800 | - public function deleteSubscription($subscriptionId) { |
|
| 2801 | - $this->atomic(function () use ($subscriptionId): void { |
|
| 2802 | - $subscriptionRow = $this->getSubscriptionById($subscriptionId); |
|
| 2803 | - |
|
| 2804 | - $query = $this->db->getQueryBuilder(); |
|
| 2805 | - $query->delete('calendarsubscriptions') |
|
| 2806 | - ->where($query->expr()->eq('id', $query->createNamedParameter($subscriptionId))) |
|
| 2807 | - ->executeStatement(); |
|
| 2808 | - |
|
| 2809 | - $query = $this->db->getQueryBuilder(); |
|
| 2810 | - $query->delete('calendarobjects') |
|
| 2811 | - ->where($query->expr()->eq('calendarid', $query->createNamedParameter($subscriptionId))) |
|
| 2812 | - ->andWhere($query->expr()->eq('calendartype', $query->createNamedParameter(self::CALENDAR_TYPE_SUBSCRIPTION))) |
|
| 2813 | - ->executeStatement(); |
|
| 2814 | - |
|
| 2815 | - $query->delete('calendarchanges') |
|
| 2816 | - ->where($query->expr()->eq('calendarid', $query->createNamedParameter($subscriptionId))) |
|
| 2817 | - ->andWhere($query->expr()->eq('calendartype', $query->createNamedParameter(self::CALENDAR_TYPE_SUBSCRIPTION))) |
|
| 2818 | - ->executeStatement(); |
|
| 2819 | - |
|
| 2820 | - $query->delete($this->dbObjectPropertiesTable) |
|
| 2821 | - ->where($query->expr()->eq('calendarid', $query->createNamedParameter($subscriptionId))) |
|
| 2822 | - ->andWhere($query->expr()->eq('calendartype', $query->createNamedParameter(self::CALENDAR_TYPE_SUBSCRIPTION))) |
|
| 2823 | - ->executeStatement(); |
|
| 2824 | - |
|
| 2825 | - if ($subscriptionRow) { |
|
| 2826 | - $this->dispatcher->dispatchTyped(new SubscriptionDeletedEvent((int)$subscriptionId, $subscriptionRow, [])); |
|
| 2827 | - } |
|
| 2828 | - }, $this->db); |
|
| 2829 | - } |
|
| 2830 | - |
|
| 2831 | - /** |
|
| 2832 | - * Returns a single scheduling object for the inbox collection. |
|
| 2833 | - * |
|
| 2834 | - * The returned array should contain the following elements: |
|
| 2835 | - * * uri - A unique basename for the object. This will be used to |
|
| 2836 | - * construct a full uri. |
|
| 2837 | - * * calendardata - The iCalendar object |
|
| 2838 | - * * lastmodified - The last modification date. Can be an int for a unix |
|
| 2839 | - * timestamp, or a PHP DateTime object. |
|
| 2840 | - * * etag - A unique token that must change if the object changed. |
|
| 2841 | - * * size - The size of the object, in bytes. |
|
| 2842 | - * |
|
| 2843 | - * @param string $principalUri |
|
| 2844 | - * @param string $objectUri |
|
| 2845 | - * @return array |
|
| 2846 | - */ |
|
| 2847 | - public function getSchedulingObject($principalUri, $objectUri) { |
|
| 2848 | - $query = $this->db->getQueryBuilder(); |
|
| 2849 | - $stmt = $query->select(['uri', 'calendardata', 'lastmodified', 'etag', 'size']) |
|
| 2850 | - ->from('schedulingobjects') |
|
| 2851 | - ->where($query->expr()->eq('principaluri', $query->createNamedParameter($principalUri))) |
|
| 2852 | - ->andWhere($query->expr()->eq('uri', $query->createNamedParameter($objectUri))) |
|
| 2853 | - ->executeQuery(); |
|
| 2854 | - |
|
| 2855 | - $row = $stmt->fetch(); |
|
| 2856 | - |
|
| 2857 | - if (!$row) { |
|
| 2858 | - return null; |
|
| 2859 | - } |
|
| 2860 | - |
|
| 2861 | - return [ |
|
| 2862 | - 'uri' => $row['uri'], |
|
| 2863 | - 'calendardata' => $row['calendardata'], |
|
| 2864 | - 'lastmodified' => $row['lastmodified'], |
|
| 2865 | - 'etag' => '"' . $row['etag'] . '"', |
|
| 2866 | - 'size' => (int)$row['size'], |
|
| 2867 | - ]; |
|
| 2868 | - } |
|
| 2869 | - |
|
| 2870 | - /** |
|
| 2871 | - * Returns all scheduling objects for the inbox collection. |
|
| 2872 | - * |
|
| 2873 | - * These objects should be returned as an array. Every item in the array |
|
| 2874 | - * should follow the same structure as returned from getSchedulingObject. |
|
| 2875 | - * |
|
| 2876 | - * The main difference is that 'calendardata' is optional. |
|
| 2877 | - * |
|
| 2878 | - * @param string $principalUri |
|
| 2879 | - * @return array |
|
| 2880 | - */ |
|
| 2881 | - public function getSchedulingObjects($principalUri) { |
|
| 2882 | - $query = $this->db->getQueryBuilder(); |
|
| 2883 | - $stmt = $query->select(['uri', 'calendardata', 'lastmodified', 'etag', 'size']) |
|
| 2884 | - ->from('schedulingobjects') |
|
| 2885 | - ->where($query->expr()->eq('principaluri', $query->createNamedParameter($principalUri))) |
|
| 2886 | - ->executeQuery(); |
|
| 2887 | - |
|
| 2888 | - $results = []; |
|
| 2889 | - while (($row = $stmt->fetch()) !== false) { |
|
| 2890 | - $results[] = [ |
|
| 2891 | - 'calendardata' => $row['calendardata'], |
|
| 2892 | - 'uri' => $row['uri'], |
|
| 2893 | - 'lastmodified' => $row['lastmodified'], |
|
| 2894 | - 'etag' => '"' . $row['etag'] . '"', |
|
| 2895 | - 'size' => (int)$row['size'], |
|
| 2896 | - ]; |
|
| 2897 | - } |
|
| 2898 | - $stmt->closeCursor(); |
|
| 2899 | - |
|
| 2900 | - return $results; |
|
| 2901 | - } |
|
| 2902 | - |
|
| 2903 | - /** |
|
| 2904 | - * Deletes a scheduling object from the inbox collection. |
|
| 2905 | - * |
|
| 2906 | - * @param string $principalUri |
|
| 2907 | - * @param string $objectUri |
|
| 2908 | - * @return void |
|
| 2909 | - */ |
|
| 2910 | - public function deleteSchedulingObject($principalUri, $objectUri) { |
|
| 2911 | - $this->cachedObjects = []; |
|
| 2912 | - $query = $this->db->getQueryBuilder(); |
|
| 2913 | - $query->delete('schedulingobjects') |
|
| 2914 | - ->where($query->expr()->eq('principaluri', $query->createNamedParameter($principalUri))) |
|
| 2915 | - ->andWhere($query->expr()->eq('uri', $query->createNamedParameter($objectUri))) |
|
| 2916 | - ->executeStatement(); |
|
| 2917 | - } |
|
| 2918 | - |
|
| 2919 | - /** |
|
| 2920 | - * Deletes all scheduling objects last modified before $modifiedBefore from the inbox collection. |
|
| 2921 | - * |
|
| 2922 | - * @param int $modifiedBefore |
|
| 2923 | - * @param int $limit |
|
| 2924 | - * @return void |
|
| 2925 | - */ |
|
| 2926 | - public function deleteOutdatedSchedulingObjects(int $modifiedBefore, int $limit): void { |
|
| 2927 | - $query = $this->db->getQueryBuilder(); |
|
| 2928 | - $query->select('id') |
|
| 2929 | - ->from('schedulingobjects') |
|
| 2930 | - ->where($query->expr()->lt('lastmodified', $query->createNamedParameter($modifiedBefore))) |
|
| 2931 | - ->setMaxResults($limit); |
|
| 2932 | - $result = $query->executeQuery(); |
|
| 2933 | - $count = $result->rowCount(); |
|
| 2934 | - if ($count === 0) { |
|
| 2935 | - return; |
|
| 2936 | - } |
|
| 2937 | - $ids = array_map(static function (array $id) { |
|
| 2938 | - return (int)$id[0]; |
|
| 2939 | - }, $result->fetchAll(\PDO::FETCH_NUM)); |
|
| 2940 | - $result->closeCursor(); |
|
| 2941 | - |
|
| 2942 | - $numDeleted = 0; |
|
| 2943 | - $deleteQuery = $this->db->getQueryBuilder(); |
|
| 2944 | - $deleteQuery->delete('schedulingobjects') |
|
| 2945 | - ->where($deleteQuery->expr()->in('id', $deleteQuery->createParameter('ids'), IQueryBuilder::PARAM_INT_ARRAY)); |
|
| 2946 | - foreach (array_chunk($ids, 1000) as $chunk) { |
|
| 2947 | - $deleteQuery->setParameter('ids', $chunk, IQueryBuilder::PARAM_INT_ARRAY); |
|
| 2948 | - $numDeleted += $deleteQuery->executeStatement(); |
|
| 2949 | - } |
|
| 2950 | - |
|
| 2951 | - if ($numDeleted === $limit) { |
|
| 2952 | - $this->logger->info("Deleted $limit scheduling objects, continuing with next batch"); |
|
| 2953 | - $this->deleteOutdatedSchedulingObjects($modifiedBefore, $limit); |
|
| 2954 | - } |
|
| 2955 | - } |
|
| 2956 | - |
|
| 2957 | - /** |
|
| 2958 | - * Creates a new scheduling object. This should land in a users' inbox. |
|
| 2959 | - * |
|
| 2960 | - * @param string $principalUri |
|
| 2961 | - * @param string $objectUri |
|
| 2962 | - * @param string $objectData |
|
| 2963 | - * @return void |
|
| 2964 | - */ |
|
| 2965 | - public function createSchedulingObject($principalUri, $objectUri, $objectData) { |
|
| 2966 | - $this->cachedObjects = []; |
|
| 2967 | - $query = $this->db->getQueryBuilder(); |
|
| 2968 | - $query->insert('schedulingobjects') |
|
| 2969 | - ->values([ |
|
| 2970 | - 'principaluri' => $query->createNamedParameter($principalUri), |
|
| 2971 | - 'calendardata' => $query->createNamedParameter($objectData, IQueryBuilder::PARAM_LOB), |
|
| 2972 | - 'uri' => $query->createNamedParameter($objectUri), |
|
| 2973 | - 'lastmodified' => $query->createNamedParameter(time()), |
|
| 2974 | - 'etag' => $query->createNamedParameter(md5($objectData)), |
|
| 2975 | - 'size' => $query->createNamedParameter(strlen($objectData)) |
|
| 2976 | - ]) |
|
| 2977 | - ->executeStatement(); |
|
| 2978 | - } |
|
| 2979 | - |
|
| 2980 | - /** |
|
| 2981 | - * Adds a change record to the calendarchanges table. |
|
| 2982 | - * |
|
| 2983 | - * @param mixed $calendarId |
|
| 2984 | - * @param string[] $objectUris |
|
| 2985 | - * @param int $operation 1 = add, 2 = modify, 3 = delete. |
|
| 2986 | - * @param int $calendarType |
|
| 2987 | - * @return void |
|
| 2988 | - */ |
|
| 2989 | - protected function addChanges(int $calendarId, array $objectUris, int $operation, int $calendarType = self::CALENDAR_TYPE_CALENDAR): void { |
|
| 2990 | - $this->cachedObjects = []; |
|
| 2991 | - $table = $calendarType === self::CALENDAR_TYPE_CALENDAR ? 'calendars': 'calendarsubscriptions'; |
|
| 2992 | - |
|
| 2993 | - $this->atomic(function () use ($calendarId, $objectUris, $operation, $calendarType, $table): void { |
|
| 2994 | - $query = $this->db->getQueryBuilder(); |
|
| 2995 | - $query->select('synctoken') |
|
| 2996 | - ->from($table) |
|
| 2997 | - ->where($query->expr()->eq('id', $query->createNamedParameter($calendarId))); |
|
| 2998 | - $result = $query->executeQuery(); |
|
| 2999 | - $syncToken = (int)$result->fetchOne(); |
|
| 3000 | - $result->closeCursor(); |
|
| 3001 | - |
|
| 3002 | - $query = $this->db->getQueryBuilder(); |
|
| 3003 | - $query->insert('calendarchanges') |
|
| 3004 | - ->values([ |
|
| 3005 | - 'uri' => $query->createParameter('uri'), |
|
| 3006 | - 'synctoken' => $query->createNamedParameter($syncToken), |
|
| 3007 | - 'calendarid' => $query->createNamedParameter($calendarId), |
|
| 3008 | - 'operation' => $query->createNamedParameter($operation), |
|
| 3009 | - 'calendartype' => $query->createNamedParameter($calendarType), |
|
| 3010 | - 'created_at' => time(), |
|
| 3011 | - ]); |
|
| 3012 | - foreach ($objectUris as $uri) { |
|
| 3013 | - $query->setParameter('uri', $uri); |
|
| 3014 | - $query->executeStatement(); |
|
| 3015 | - } |
|
| 3016 | - |
|
| 3017 | - $query = $this->db->getQueryBuilder(); |
|
| 3018 | - $query->update($table) |
|
| 3019 | - ->set('synctoken', $query->createNamedParameter($syncToken + 1, IQueryBuilder::PARAM_INT)) |
|
| 3020 | - ->where($query->expr()->eq('id', $query->createNamedParameter($calendarId))) |
|
| 3021 | - ->executeStatement(); |
|
| 3022 | - }, $this->db); |
|
| 3023 | - } |
|
| 3024 | - |
|
| 3025 | - public function restoreChanges(int $calendarId, int $calendarType = self::CALENDAR_TYPE_CALENDAR): void { |
|
| 3026 | - $this->cachedObjects = []; |
|
| 3027 | - |
|
| 3028 | - $this->atomic(function () use ($calendarId, $calendarType): void { |
|
| 3029 | - $qbAdded = $this->db->getQueryBuilder(); |
|
| 3030 | - $qbAdded->select('uri') |
|
| 3031 | - ->from('calendarobjects') |
|
| 3032 | - ->where( |
|
| 3033 | - $qbAdded->expr()->andX( |
|
| 3034 | - $qbAdded->expr()->eq('calendarid', $qbAdded->createNamedParameter($calendarId)), |
|
| 3035 | - $qbAdded->expr()->eq('calendartype', $qbAdded->createNamedParameter($calendarType)), |
|
| 3036 | - $qbAdded->expr()->isNull('deleted_at'), |
|
| 3037 | - ) |
|
| 3038 | - ); |
|
| 3039 | - $resultAdded = $qbAdded->executeQuery(); |
|
| 3040 | - $addedUris = $resultAdded->fetchAll(\PDO::FETCH_COLUMN); |
|
| 3041 | - $resultAdded->closeCursor(); |
|
| 3042 | - // Track everything as changed |
|
| 3043 | - // Tracking the creation is not necessary because \OCA\DAV\CalDAV\CalDavBackend::getChangesForCalendar |
|
| 3044 | - // only returns the last change per object. |
|
| 3045 | - $this->addChanges($calendarId, $addedUris, 2, $calendarType); |
|
| 3046 | - |
|
| 3047 | - $qbDeleted = $this->db->getQueryBuilder(); |
|
| 3048 | - $qbDeleted->select('uri') |
|
| 3049 | - ->from('calendarobjects') |
|
| 3050 | - ->where( |
|
| 3051 | - $qbDeleted->expr()->andX( |
|
| 3052 | - $qbDeleted->expr()->eq('calendarid', $qbDeleted->createNamedParameter($calendarId)), |
|
| 3053 | - $qbDeleted->expr()->eq('calendartype', $qbDeleted->createNamedParameter($calendarType)), |
|
| 3054 | - $qbDeleted->expr()->isNotNull('deleted_at'), |
|
| 3055 | - ) |
|
| 3056 | - ); |
|
| 3057 | - $resultDeleted = $qbDeleted->executeQuery(); |
|
| 3058 | - $deletedUris = array_map(function (string $uri) { |
|
| 3059 | - return str_replace('-deleted.ics', '.ics', $uri); |
|
| 3060 | - }, $resultDeleted->fetchAll(\PDO::FETCH_COLUMN)); |
|
| 3061 | - $resultDeleted->closeCursor(); |
|
| 3062 | - $this->addChanges($calendarId, $deletedUris, 3, $calendarType); |
|
| 3063 | - }, $this->db); |
|
| 3064 | - } |
|
| 3065 | - |
|
| 3066 | - /** |
|
| 3067 | - * Parses some information from calendar objects, used for optimized |
|
| 3068 | - * calendar-queries. |
|
| 3069 | - * |
|
| 3070 | - * Returns an array with the following keys: |
|
| 3071 | - * * etag - An md5 checksum of the object without the quotes. |
|
| 3072 | - * * size - Size of the object in bytes |
|
| 3073 | - * * componentType - VEVENT, VTODO or VJOURNAL |
|
| 3074 | - * * firstOccurence |
|
| 3075 | - * * lastOccurence |
|
| 3076 | - * * uid - value of the UID property |
|
| 3077 | - * |
|
| 3078 | - * @param string $calendarData |
|
| 3079 | - * @return array |
|
| 3080 | - */ |
|
| 3081 | - public function getDenormalizedData(string $calendarData): array { |
|
| 3082 | - $vObject = Reader::read($calendarData); |
|
| 3083 | - $vEvents = []; |
|
| 3084 | - $componentType = null; |
|
| 3085 | - $component = null; |
|
| 3086 | - $firstOccurrence = null; |
|
| 3087 | - $lastOccurrence = null; |
|
| 3088 | - $uid = null; |
|
| 3089 | - $classification = self::CLASSIFICATION_PUBLIC; |
|
| 3090 | - $hasDTSTART = false; |
|
| 3091 | - foreach ($vObject->getComponents() as $component) { |
|
| 3092 | - if ($component->name !== 'VTIMEZONE') { |
|
| 3093 | - // Finding all VEVENTs, and track them |
|
| 3094 | - if ($component->name === 'VEVENT') { |
|
| 3095 | - $vEvents[] = $component; |
|
| 3096 | - if ($component->DTSTART) { |
|
| 3097 | - $hasDTSTART = true; |
|
| 3098 | - } |
|
| 3099 | - } |
|
| 3100 | - // Track first component type and uid |
|
| 3101 | - if ($uid === null) { |
|
| 3102 | - $componentType = $component->name; |
|
| 3103 | - $uid = (string)$component->UID; |
|
| 3104 | - } |
|
| 3105 | - } |
|
| 3106 | - } |
|
| 3107 | - if (!$componentType) { |
|
| 3108 | - throw new BadRequest('Calendar objects must have a VJOURNAL, VEVENT or VTODO component'); |
|
| 3109 | - } |
|
| 3110 | - |
|
| 3111 | - if ($hasDTSTART) { |
|
| 3112 | - $component = $vEvents[0]; |
|
| 3113 | - |
|
| 3114 | - // Finding the last occurrence is a bit harder |
|
| 3115 | - if (!isset($component->RRULE) && count($vEvents) === 1) { |
|
| 3116 | - $firstOccurrence = $component->DTSTART->getDateTime()->getTimeStamp(); |
|
| 3117 | - if (isset($component->DTEND)) { |
|
| 3118 | - $lastOccurrence = $component->DTEND->getDateTime()->getTimeStamp(); |
|
| 3119 | - } elseif (isset($component->DURATION)) { |
|
| 3120 | - $endDate = clone $component->DTSTART->getDateTime(); |
|
| 3121 | - $endDate->add(DateTimeParser::parse($component->DURATION->getValue())); |
|
| 3122 | - $lastOccurrence = $endDate->getTimeStamp(); |
|
| 3123 | - } elseif (!$component->DTSTART->hasTime()) { |
|
| 3124 | - $endDate = clone $component->DTSTART->getDateTime(); |
|
| 3125 | - $endDate->modify('+1 day'); |
|
| 3126 | - $lastOccurrence = $endDate->getTimeStamp(); |
|
| 3127 | - } else { |
|
| 3128 | - $lastOccurrence = $firstOccurrence; |
|
| 3129 | - } |
|
| 3130 | - } else { |
|
| 3131 | - try { |
|
| 3132 | - $it = new EventIterator($vEvents); |
|
| 3133 | - } catch (NoInstancesException $e) { |
|
| 3134 | - $this->logger->debug('Caught no instance exception for calendar data. This usually indicates invalid calendar data.', [ |
|
| 3135 | - 'app' => 'dav', |
|
| 3136 | - 'exception' => $e, |
|
| 3137 | - ]); |
|
| 3138 | - throw new Forbidden($e->getMessage()); |
|
| 3139 | - } |
|
| 3140 | - $maxDate = new DateTime(self::MAX_DATE); |
|
| 3141 | - $firstOccurrence = $it->getDtStart()->getTimestamp(); |
|
| 3142 | - if ($it->isInfinite()) { |
|
| 3143 | - $lastOccurrence = $maxDate->getTimestamp(); |
|
| 3144 | - } else { |
|
| 3145 | - $end = $it->getDtEnd(); |
|
| 3146 | - while ($it->valid() && $end < $maxDate) { |
|
| 3147 | - $end = $it->getDtEnd(); |
|
| 3148 | - $it->next(); |
|
| 3149 | - } |
|
| 3150 | - $lastOccurrence = $end->getTimestamp(); |
|
| 3151 | - } |
|
| 3152 | - } |
|
| 3153 | - } |
|
| 3154 | - |
|
| 3155 | - if ($component->CLASS) { |
|
| 3156 | - $classification = CalDavBackend::CLASSIFICATION_PRIVATE; |
|
| 3157 | - switch ($component->CLASS->getValue()) { |
|
| 3158 | - case 'PUBLIC': |
|
| 3159 | - $classification = CalDavBackend::CLASSIFICATION_PUBLIC; |
|
| 3160 | - break; |
|
| 3161 | - case 'CONFIDENTIAL': |
|
| 3162 | - $classification = CalDavBackend::CLASSIFICATION_CONFIDENTIAL; |
|
| 3163 | - break; |
|
| 3164 | - } |
|
| 3165 | - } |
|
| 3166 | - return [ |
|
| 3167 | - 'etag' => md5($calendarData), |
|
| 3168 | - 'size' => strlen($calendarData), |
|
| 3169 | - 'componentType' => $componentType, |
|
| 3170 | - 'firstOccurence' => is_null($firstOccurrence) ? null : max(0, $firstOccurrence), |
|
| 3171 | - 'lastOccurence' => is_null($lastOccurrence) ? null : max(0, $lastOccurrence), |
|
| 3172 | - 'uid' => $uid, |
|
| 3173 | - 'classification' => $classification |
|
| 3174 | - ]; |
|
| 3175 | - } |
|
| 3176 | - |
|
| 3177 | - /** |
|
| 3178 | - * @param $cardData |
|
| 3179 | - * @return bool|string |
|
| 3180 | - */ |
|
| 3181 | - private function readBlob($cardData) { |
|
| 3182 | - if (is_resource($cardData)) { |
|
| 3183 | - return stream_get_contents($cardData); |
|
| 3184 | - } |
|
| 3185 | - |
|
| 3186 | - return $cardData; |
|
| 3187 | - } |
|
| 3188 | - |
|
| 3189 | - /** |
|
| 3190 | - * @param list<array{href: string, commonName: string, readOnly: bool}> $add |
|
| 3191 | - * @param list<string> $remove |
|
| 3192 | - */ |
|
| 3193 | - public function updateShares(IShareable $shareable, array $add, array $remove): void { |
|
| 3194 | - $this->atomic(function () use ($shareable, $add, $remove): void { |
|
| 3195 | - $calendarId = $shareable->getResourceId(); |
|
| 3196 | - $calendarRow = $this->getCalendarById($calendarId); |
|
| 3197 | - if ($calendarRow === null) { |
|
| 3198 | - throw new \RuntimeException('Trying to update shares for non-existing calendar: ' . $calendarId); |
|
| 3199 | - } |
|
| 3200 | - $oldShares = $this->getShares($calendarId); |
|
| 3201 | - |
|
| 3202 | - $this->calendarSharingBackend->updateShares($shareable, $add, $remove, $oldShares); |
|
| 3203 | - |
|
| 3204 | - $this->dispatcher->dispatchTyped(new CalendarShareUpdatedEvent($calendarId, $calendarRow, $oldShares, $add, $remove)); |
|
| 3205 | - }, $this->db); |
|
| 3206 | - } |
|
| 3207 | - |
|
| 3208 | - /** |
|
| 3209 | - * @return list<array{href: string, commonName: string, status: int, readOnly: bool, '{http://owncloud.org/ns}principal': string, '{http://owncloud.org/ns}group-share': bool}> |
|
| 3210 | - */ |
|
| 3211 | - public function getShares(int $resourceId): array { |
|
| 3212 | - return $this->calendarSharingBackend->getShares($resourceId); |
|
| 3213 | - } |
|
| 3214 | - |
|
| 3215 | - public function preloadShares(array $resourceIds): void { |
|
| 3216 | - $this->calendarSharingBackend->preloadShares($resourceIds); |
|
| 3217 | - } |
|
| 3218 | - |
|
| 3219 | - /** |
|
| 3220 | - * @param boolean $value |
|
| 3221 | - * @param Calendar $calendar |
|
| 3222 | - * @return string|null |
|
| 3223 | - */ |
|
| 3224 | - public function setPublishStatus($value, $calendar) { |
|
| 3225 | - return $this->atomic(function () use ($value, $calendar) { |
|
| 3226 | - $calendarId = $calendar->getResourceId(); |
|
| 3227 | - $calendarData = $this->getCalendarById($calendarId); |
|
| 3228 | - |
|
| 3229 | - $query = $this->db->getQueryBuilder(); |
|
| 3230 | - if ($value) { |
|
| 3231 | - $publicUri = $this->random->generate(16, ISecureRandom::CHAR_HUMAN_READABLE); |
|
| 3232 | - $query->insert('dav_shares') |
|
| 3233 | - ->values([ |
|
| 3234 | - 'principaluri' => $query->createNamedParameter($calendar->getPrincipalURI()), |
|
| 3235 | - 'type' => $query->createNamedParameter('calendar'), |
|
| 3236 | - 'access' => $query->createNamedParameter(self::ACCESS_PUBLIC), |
|
| 3237 | - 'resourceid' => $query->createNamedParameter($calendar->getResourceId()), |
|
| 3238 | - 'publicuri' => $query->createNamedParameter($publicUri) |
|
| 3239 | - ]); |
|
| 3240 | - $query->executeStatement(); |
|
| 3241 | - |
|
| 3242 | - $this->dispatcher->dispatchTyped(new CalendarPublishedEvent($calendarId, $calendarData, $publicUri)); |
|
| 3243 | - return $publicUri; |
|
| 3244 | - } |
|
| 3245 | - $query->delete('dav_shares') |
|
| 3246 | - ->where($query->expr()->eq('resourceid', $query->createNamedParameter($calendar->getResourceId()))) |
|
| 3247 | - ->andWhere($query->expr()->eq('access', $query->createNamedParameter(self::ACCESS_PUBLIC))); |
|
| 3248 | - $query->executeStatement(); |
|
| 3249 | - |
|
| 3250 | - $this->dispatcher->dispatchTyped(new CalendarUnpublishedEvent($calendarId, $calendarData)); |
|
| 3251 | - return null; |
|
| 3252 | - }, $this->db); |
|
| 3253 | - } |
|
| 3254 | - |
|
| 3255 | - /** |
|
| 3256 | - * @param Calendar $calendar |
|
| 3257 | - * @return mixed |
|
| 3258 | - */ |
|
| 3259 | - public function getPublishStatus($calendar) { |
|
| 3260 | - $query = $this->db->getQueryBuilder(); |
|
| 3261 | - $result = $query->select('publicuri') |
|
| 3262 | - ->from('dav_shares') |
|
| 3263 | - ->where($query->expr()->eq('resourceid', $query->createNamedParameter($calendar->getResourceId()))) |
|
| 3264 | - ->andWhere($query->expr()->eq('access', $query->createNamedParameter(self::ACCESS_PUBLIC))) |
|
| 3265 | - ->executeQuery(); |
|
| 3266 | - |
|
| 3267 | - $row = $result->fetch(); |
|
| 3268 | - $result->closeCursor(); |
|
| 3269 | - return $row ? reset($row) : false; |
|
| 3270 | - } |
|
| 3271 | - |
|
| 3272 | - /** |
|
| 3273 | - * @param int $resourceId |
|
| 3274 | - * @param list<array{privilege: string, principal: string, protected: bool}> $acl |
|
| 3275 | - * @return list<array{privilege: string, principal: string, protected: bool}> |
|
| 3276 | - */ |
|
| 3277 | - public function applyShareAcl(int $resourceId, array $acl): array { |
|
| 3278 | - $shares = $this->calendarSharingBackend->getShares($resourceId); |
|
| 3279 | - return $this->calendarSharingBackend->applyShareAcl($shares, $acl); |
|
| 3280 | - } |
|
| 3281 | - |
|
| 3282 | - /** |
|
| 3283 | - * update properties table |
|
| 3284 | - * |
|
| 3285 | - * @param int $calendarId |
|
| 3286 | - * @param string $objectUri |
|
| 3287 | - * @param string $calendarData |
|
| 3288 | - * @param int $calendarType |
|
| 3289 | - */ |
|
| 3290 | - public function updateProperties($calendarId, $objectUri, $calendarData, $calendarType = self::CALENDAR_TYPE_CALENDAR) { |
|
| 3291 | - $this->cachedObjects = []; |
|
| 3292 | - $this->atomic(function () use ($calendarId, $objectUri, $calendarData, $calendarType): void { |
|
| 3293 | - $objectId = $this->getCalendarObjectId($calendarId, $objectUri, $calendarType); |
|
| 3294 | - |
|
| 3295 | - try { |
|
| 3296 | - $vCalendar = $this->readCalendarData($calendarData); |
|
| 3297 | - } catch (\Exception $ex) { |
|
| 3298 | - return; |
|
| 3299 | - } |
|
| 3300 | - |
|
| 3301 | - $this->purgeProperties($calendarId, $objectId); |
|
| 3302 | - |
|
| 3303 | - $query = $this->db->getQueryBuilder(); |
|
| 3304 | - $query->insert($this->dbObjectPropertiesTable) |
|
| 3305 | - ->values( |
|
| 3306 | - [ |
|
| 3307 | - 'calendarid' => $query->createNamedParameter($calendarId), |
|
| 3308 | - 'calendartype' => $query->createNamedParameter($calendarType), |
|
| 3309 | - 'objectid' => $query->createNamedParameter($objectId), |
|
| 3310 | - 'name' => $query->createParameter('name'), |
|
| 3311 | - 'parameter' => $query->createParameter('parameter'), |
|
| 3312 | - 'value' => $query->createParameter('value'), |
|
| 3313 | - ] |
|
| 3314 | - ); |
|
| 3315 | - |
|
| 3316 | - $indexComponents = ['VEVENT', 'VJOURNAL', 'VTODO']; |
|
| 3317 | - foreach ($vCalendar->getComponents() as $component) { |
|
| 3318 | - if (!in_array($component->name, $indexComponents)) { |
|
| 3319 | - continue; |
|
| 3320 | - } |
|
| 3321 | - |
|
| 3322 | - foreach ($component->children() as $property) { |
|
| 3323 | - if (in_array($property->name, self::INDEXED_PROPERTIES, true)) { |
|
| 3324 | - $value = $property->getValue(); |
|
| 3325 | - // is this a shitty db? |
|
| 3326 | - if (!$this->db->supports4ByteText()) { |
|
| 3327 | - $value = preg_replace('/[\x{10000}-\x{10FFFF}]/u', "\xEF\xBF\xBD", $value); |
|
| 3328 | - } |
|
| 3329 | - $value = mb_strcut($value, 0, 254); |
|
| 3330 | - |
|
| 3331 | - $query->setParameter('name', $property->name); |
|
| 3332 | - $query->setParameter('parameter', null); |
|
| 3333 | - $query->setParameter('value', mb_strcut($value, 0, 254)); |
|
| 3334 | - $query->executeStatement(); |
|
| 3335 | - } |
|
| 3336 | - |
|
| 3337 | - if (array_key_exists($property->name, self::$indexParameters)) { |
|
| 3338 | - $parameters = $property->parameters(); |
|
| 3339 | - $indexedParametersForProperty = self::$indexParameters[$property->name]; |
|
| 3340 | - |
|
| 3341 | - foreach ($parameters as $key => $value) { |
|
| 3342 | - if (in_array($key, $indexedParametersForProperty)) { |
|
| 3343 | - // is this a shitty db? |
|
| 3344 | - if ($this->db->supports4ByteText()) { |
|
| 3345 | - $value = preg_replace('/[\x{10000}-\x{10FFFF}]/u', "\xEF\xBF\xBD", $value); |
|
| 3346 | - } |
|
| 3347 | - |
|
| 3348 | - $query->setParameter('name', $property->name); |
|
| 3349 | - $query->setParameter('parameter', mb_strcut($key, 0, 254)); |
|
| 3350 | - $query->setParameter('value', mb_strcut($value, 0, 254)); |
|
| 3351 | - $query->executeStatement(); |
|
| 3352 | - } |
|
| 3353 | - } |
|
| 3354 | - } |
|
| 3355 | - } |
|
| 3356 | - } |
|
| 3357 | - }, $this->db); |
|
| 3358 | - } |
|
| 3359 | - |
|
| 3360 | - /** |
|
| 3361 | - * deletes all birthday calendars |
|
| 3362 | - */ |
|
| 3363 | - public function deleteAllBirthdayCalendars() { |
|
| 3364 | - $this->atomic(function (): void { |
|
| 3365 | - $query = $this->db->getQueryBuilder(); |
|
| 3366 | - $result = $query->select(['id'])->from('calendars') |
|
| 3367 | - ->where($query->expr()->eq('uri', $query->createNamedParameter(BirthdayService::BIRTHDAY_CALENDAR_URI))) |
|
| 3368 | - ->executeQuery(); |
|
| 3369 | - |
|
| 3370 | - while (($row = $result->fetch()) !== false) { |
|
| 3371 | - $this->deleteCalendar( |
|
| 3372 | - $row['id'], |
|
| 3373 | - true // No data to keep in the trashbin, if the user re-enables then we regenerate |
|
| 3374 | - ); |
|
| 3375 | - } |
|
| 3376 | - $result->closeCursor(); |
|
| 3377 | - }, $this->db); |
|
| 3378 | - } |
|
| 3379 | - |
|
| 3380 | - /** |
|
| 3381 | - * @param $subscriptionId |
|
| 3382 | - */ |
|
| 3383 | - public function purgeAllCachedEventsForSubscription($subscriptionId) { |
|
| 3384 | - $this->atomic(function () use ($subscriptionId): void { |
|
| 3385 | - $query = $this->db->getQueryBuilder(); |
|
| 3386 | - $query->select('uri') |
|
| 3387 | - ->from('calendarobjects') |
|
| 3388 | - ->where($query->expr()->eq('calendarid', $query->createNamedParameter($subscriptionId))) |
|
| 3389 | - ->andWhere($query->expr()->eq('calendartype', $query->createNamedParameter(self::CALENDAR_TYPE_SUBSCRIPTION))); |
|
| 3390 | - $stmt = $query->executeQuery(); |
|
| 3391 | - |
|
| 3392 | - $uris = []; |
|
| 3393 | - while (($row = $stmt->fetch()) !== false) { |
|
| 3394 | - $uris[] = $row['uri']; |
|
| 3395 | - } |
|
| 3396 | - $stmt->closeCursor(); |
|
| 3397 | - |
|
| 3398 | - $query = $this->db->getQueryBuilder(); |
|
| 3399 | - $query->delete('calendarobjects') |
|
| 3400 | - ->where($query->expr()->eq('calendarid', $query->createNamedParameter($subscriptionId))) |
|
| 3401 | - ->andWhere($query->expr()->eq('calendartype', $query->createNamedParameter(self::CALENDAR_TYPE_SUBSCRIPTION))) |
|
| 3402 | - ->executeStatement(); |
|
| 3403 | - |
|
| 3404 | - $query = $this->db->getQueryBuilder(); |
|
| 3405 | - $query->delete('calendarchanges') |
|
| 3406 | - ->where($query->expr()->eq('calendarid', $query->createNamedParameter($subscriptionId))) |
|
| 3407 | - ->andWhere($query->expr()->eq('calendartype', $query->createNamedParameter(self::CALENDAR_TYPE_SUBSCRIPTION))) |
|
| 3408 | - ->executeStatement(); |
|
| 3409 | - |
|
| 3410 | - $query = $this->db->getQueryBuilder(); |
|
| 3411 | - $query->delete($this->dbObjectPropertiesTable) |
|
| 3412 | - ->where($query->expr()->eq('calendarid', $query->createNamedParameter($subscriptionId))) |
|
| 3413 | - ->andWhere($query->expr()->eq('calendartype', $query->createNamedParameter(self::CALENDAR_TYPE_SUBSCRIPTION))) |
|
| 3414 | - ->executeStatement(); |
|
| 3415 | - |
|
| 3416 | - $this->addChanges($subscriptionId, $uris, 3, self::CALENDAR_TYPE_SUBSCRIPTION); |
|
| 3417 | - }, $this->db); |
|
| 3418 | - } |
|
| 3419 | - |
|
| 3420 | - /** |
|
| 3421 | - * @param int $subscriptionId |
|
| 3422 | - * @param array<int> $calendarObjectIds |
|
| 3423 | - * @param array<string> $calendarObjectUris |
|
| 3424 | - */ |
|
| 3425 | - public function purgeCachedEventsForSubscription(int $subscriptionId, array $calendarObjectIds, array $calendarObjectUris): void { |
|
| 3426 | - if (empty($calendarObjectUris)) { |
|
| 3427 | - return; |
|
| 3428 | - } |
|
| 3429 | - |
|
| 3430 | - $this->atomic(function () use ($subscriptionId, $calendarObjectIds, $calendarObjectUris): void { |
|
| 3431 | - foreach (array_chunk($calendarObjectIds, 1000) as $chunk) { |
|
| 3432 | - $query = $this->db->getQueryBuilder(); |
|
| 3433 | - $query->delete($this->dbObjectPropertiesTable) |
|
| 3434 | - ->where($query->expr()->eq('calendarid', $query->createNamedParameter($subscriptionId))) |
|
| 3435 | - ->andWhere($query->expr()->eq('calendartype', $query->createNamedParameter(self::CALENDAR_TYPE_SUBSCRIPTION))) |
|
| 3436 | - ->andWhere($query->expr()->in('id', $query->createNamedParameter($chunk, IQueryBuilder::PARAM_INT_ARRAY), IQueryBuilder::PARAM_INT_ARRAY)) |
|
| 3437 | - ->executeStatement(); |
|
| 3438 | - |
|
| 3439 | - $query = $this->db->getQueryBuilder(); |
|
| 3440 | - $query->delete('calendarobjects') |
|
| 3441 | - ->where($query->expr()->eq('calendarid', $query->createNamedParameter($subscriptionId))) |
|
| 3442 | - ->andWhere($query->expr()->eq('calendartype', $query->createNamedParameter(self::CALENDAR_TYPE_SUBSCRIPTION))) |
|
| 3443 | - ->andWhere($query->expr()->in('id', $query->createNamedParameter($chunk, IQueryBuilder::PARAM_INT_ARRAY), IQueryBuilder::PARAM_INT_ARRAY)) |
|
| 3444 | - ->executeStatement(); |
|
| 3445 | - } |
|
| 3446 | - |
|
| 3447 | - foreach (array_chunk($calendarObjectUris, 1000) as $chunk) { |
|
| 3448 | - $query = $this->db->getQueryBuilder(); |
|
| 3449 | - $query->delete('calendarchanges') |
|
| 3450 | - ->where($query->expr()->eq('calendarid', $query->createNamedParameter($subscriptionId))) |
|
| 3451 | - ->andWhere($query->expr()->eq('calendartype', $query->createNamedParameter(self::CALENDAR_TYPE_SUBSCRIPTION))) |
|
| 3452 | - ->andWhere($query->expr()->in('uri', $query->createNamedParameter($chunk, IQueryBuilder::PARAM_STR_ARRAY), IQueryBuilder::PARAM_STR_ARRAY)) |
|
| 3453 | - ->executeStatement(); |
|
| 3454 | - } |
|
| 3455 | - $this->addChanges($subscriptionId, $calendarObjectUris, 3, self::CALENDAR_TYPE_SUBSCRIPTION); |
|
| 3456 | - }, $this->db); |
|
| 3457 | - } |
|
| 3458 | - |
|
| 3459 | - /** |
|
| 3460 | - * Move a calendar from one user to another |
|
| 3461 | - * |
|
| 3462 | - * @param string $uriName |
|
| 3463 | - * @param string $uriOrigin |
|
| 3464 | - * @param string $uriDestination |
|
| 3465 | - * @param string $newUriName (optional) the new uriName |
|
| 3466 | - */ |
|
| 3467 | - public function moveCalendar($uriName, $uriOrigin, $uriDestination, $newUriName = null) { |
|
| 3468 | - $query = $this->db->getQueryBuilder(); |
|
| 3469 | - $query->update('calendars') |
|
| 3470 | - ->set('principaluri', $query->createNamedParameter($uriDestination)) |
|
| 3471 | - ->set('uri', $query->createNamedParameter($newUriName ?: $uriName)) |
|
| 3472 | - ->where($query->expr()->eq('principaluri', $query->createNamedParameter($uriOrigin))) |
|
| 3473 | - ->andWhere($query->expr()->eq('uri', $query->createNamedParameter($uriName))) |
|
| 3474 | - ->executeStatement(); |
|
| 3475 | - } |
|
| 3476 | - |
|
| 3477 | - /** |
|
| 3478 | - * read VCalendar data into a VCalendar object |
|
| 3479 | - * |
|
| 3480 | - * @param string $objectData |
|
| 3481 | - * @return VCalendar |
|
| 3482 | - */ |
|
| 3483 | - protected function readCalendarData($objectData) { |
|
| 3484 | - return Reader::read($objectData); |
|
| 3485 | - } |
|
| 3486 | - |
|
| 3487 | - /** |
|
| 3488 | - * delete all properties from a given calendar object |
|
| 3489 | - * |
|
| 3490 | - * @param int $calendarId |
|
| 3491 | - * @param int $objectId |
|
| 3492 | - */ |
|
| 3493 | - protected function purgeProperties($calendarId, $objectId) { |
|
| 3494 | - $this->cachedObjects = []; |
|
| 3495 | - $query = $this->db->getQueryBuilder(); |
|
| 3496 | - $query->delete($this->dbObjectPropertiesTable) |
|
| 3497 | - ->where($query->expr()->eq('objectid', $query->createNamedParameter($objectId))) |
|
| 3498 | - ->andWhere($query->expr()->eq('calendarid', $query->createNamedParameter($calendarId))); |
|
| 3499 | - $query->executeStatement(); |
|
| 3500 | - } |
|
| 3501 | - |
|
| 3502 | - /** |
|
| 3503 | - * get ID from a given calendar object |
|
| 3504 | - * |
|
| 3505 | - * @param int $calendarId |
|
| 3506 | - * @param string $uri |
|
| 3507 | - * @param int $calendarType |
|
| 3508 | - * @return int |
|
| 3509 | - */ |
|
| 3510 | - protected function getCalendarObjectId($calendarId, $uri, $calendarType):int { |
|
| 3511 | - $query = $this->db->getQueryBuilder(); |
|
| 3512 | - $query->select('id') |
|
| 3513 | - ->from('calendarobjects') |
|
| 3514 | - ->where($query->expr()->eq('uri', $query->createNamedParameter($uri))) |
|
| 3515 | - ->andWhere($query->expr()->eq('calendarid', $query->createNamedParameter($calendarId))) |
|
| 3516 | - ->andWhere($query->expr()->eq('calendartype', $query->createNamedParameter($calendarType))); |
|
| 3517 | - |
|
| 3518 | - $result = $query->executeQuery(); |
|
| 3519 | - $objectIds = $result->fetch(); |
|
| 3520 | - $result->closeCursor(); |
|
| 3521 | - |
|
| 3522 | - if (!isset($objectIds['id'])) { |
|
| 3523 | - throw new \InvalidArgumentException('Calendarobject does not exists: ' . $uri); |
|
| 3524 | - } |
|
| 3525 | - |
|
| 3526 | - return (int)$objectIds['id']; |
|
| 3527 | - } |
|
| 3528 | - |
|
| 3529 | - /** |
|
| 3530 | - * @throws \InvalidArgumentException |
|
| 3531 | - */ |
|
| 3532 | - public function pruneOutdatedSyncTokens(int $keep, int $retention): int { |
|
| 3533 | - if ($keep < 0) { |
|
| 3534 | - throw new \InvalidArgumentException(); |
|
| 3535 | - } |
|
| 3536 | - |
|
| 3537 | - $query = $this->db->getQueryBuilder(); |
|
| 3538 | - $query->select($query->func()->max('id')) |
|
| 3539 | - ->from('calendarchanges'); |
|
| 3540 | - |
|
| 3541 | - $result = $query->executeQuery(); |
|
| 3542 | - $maxId = (int)$result->fetchOne(); |
|
| 3543 | - $result->closeCursor(); |
|
| 3544 | - if (!$maxId || $maxId < $keep) { |
|
| 3545 | - return 0; |
|
| 3546 | - } |
|
| 3547 | - |
|
| 3548 | - $query = $this->db->getQueryBuilder(); |
|
| 3549 | - $query->delete('calendarchanges') |
|
| 3550 | - ->where( |
|
| 3551 | - $query->expr()->lte('id', $query->createNamedParameter($maxId - $keep, IQueryBuilder::PARAM_INT), IQueryBuilder::PARAM_INT), |
|
| 3552 | - $query->expr()->lte('created_at', $query->createNamedParameter($retention)), |
|
| 3553 | - ); |
|
| 3554 | - return $query->executeStatement(); |
|
| 3555 | - } |
|
| 3556 | - |
|
| 3557 | - /** |
|
| 3558 | - * return legacy endpoint principal name to new principal name |
|
| 3559 | - * |
|
| 3560 | - * @param $principalUri |
|
| 3561 | - * @param $toV2 |
|
| 3562 | - * @return string |
|
| 3563 | - */ |
|
| 3564 | - private function convertPrincipal($principalUri, $toV2) { |
|
| 3565 | - if ($this->principalBackend->getPrincipalPrefix() === 'principals') { |
|
| 3566 | - [, $name] = Uri\split($principalUri); |
|
| 3567 | - if ($toV2 === true) { |
|
| 3568 | - return "principals/users/$name"; |
|
| 3569 | - } |
|
| 3570 | - return "principals/$name"; |
|
| 3571 | - } |
|
| 3572 | - return $principalUri; |
|
| 3573 | - } |
|
| 3574 | - |
|
| 3575 | - /** |
|
| 3576 | - * adds information about an owner to the calendar data |
|
| 3577 | - * |
|
| 3578 | - */ |
|
| 3579 | - private function addOwnerPrincipalToCalendar(array $calendarInfo): array { |
|
| 3580 | - $ownerPrincipalKey = '{' . \OCA\DAV\DAV\Sharing\Plugin::NS_OWNCLOUD . '}owner-principal'; |
|
| 3581 | - $displaynameKey = '{' . \OCA\DAV\DAV\Sharing\Plugin::NS_NEXTCLOUD . '}owner-displayname'; |
|
| 3582 | - if (isset($calendarInfo[$ownerPrincipalKey])) { |
|
| 3583 | - $uri = $calendarInfo[$ownerPrincipalKey]; |
|
| 3584 | - } else { |
|
| 3585 | - $uri = $calendarInfo['principaluri']; |
|
| 3586 | - } |
|
| 3587 | - |
|
| 3588 | - $principalInformation = $this->principalBackend->getPrincipalByPath($uri); |
|
| 3589 | - if (isset($principalInformation['{DAV:}displayname'])) { |
|
| 3590 | - $calendarInfo[$displaynameKey] = $principalInformation['{DAV:}displayname']; |
|
| 3591 | - } |
|
| 3592 | - return $calendarInfo; |
|
| 3593 | - } |
|
| 3594 | - |
|
| 3595 | - private function addResourceTypeToCalendar(array $row, array $calendar): array { |
|
| 3596 | - if (isset($row['deleted_at'])) { |
|
| 3597 | - // Columns is set and not null -> this is a deleted calendar |
|
| 3598 | - // we send a custom resourcetype to hide the deleted calendar |
|
| 3599 | - // from ordinary DAV clients, but the Calendar app will know |
|
| 3600 | - // how to handle this special resource. |
|
| 3601 | - $calendar['{DAV:}resourcetype'] = new DAV\Xml\Property\ResourceType([ |
|
| 3602 | - '{DAV:}collection', |
|
| 3603 | - sprintf('{%s}deleted-calendar', \OCA\DAV\DAV\Sharing\Plugin::NS_NEXTCLOUD), |
|
| 3604 | - ]); |
|
| 3605 | - } |
|
| 3606 | - return $calendar; |
|
| 3607 | - } |
|
| 3608 | - |
|
| 3609 | - /** |
|
| 3610 | - * Amend the calendar info with database row data |
|
| 3611 | - * |
|
| 3612 | - * @param array $row |
|
| 3613 | - * @param array $calendar |
|
| 3614 | - * |
|
| 3615 | - * @return array |
|
| 3616 | - */ |
|
| 3617 | - private function rowToCalendar($row, array $calendar): array { |
|
| 3618 | - foreach ($this->propertyMap as $xmlName => [$dbName, $type]) { |
|
| 3619 | - $value = $row[$dbName]; |
|
| 3620 | - if ($value !== null) { |
|
| 3621 | - settype($value, $type); |
|
| 3622 | - } |
|
| 3623 | - $calendar[$xmlName] = $value; |
|
| 3624 | - } |
|
| 3625 | - return $calendar; |
|
| 3626 | - } |
|
| 3627 | - |
|
| 3628 | - /** |
|
| 3629 | - * Amend the subscription info with database row data |
|
| 3630 | - * |
|
| 3631 | - * @param array $row |
|
| 3632 | - * @param array $subscription |
|
| 3633 | - * |
|
| 3634 | - * @return array |
|
| 3635 | - */ |
|
| 3636 | - private function rowToSubscription($row, array $subscription): array { |
|
| 3637 | - foreach ($this->subscriptionPropertyMap as $xmlName => [$dbName, $type]) { |
|
| 3638 | - $value = $row[$dbName]; |
|
| 3639 | - if ($value !== null) { |
|
| 3640 | - settype($value, $type); |
|
| 3641 | - } |
|
| 3642 | - $subscription[$xmlName] = $value; |
|
| 3643 | - } |
|
| 3644 | - return $subscription; |
|
| 3645 | - } |
|
| 3646 | - |
|
| 3647 | - /** |
|
| 3648 | - * delete all invitations from a given calendar |
|
| 3649 | - * |
|
| 3650 | - * @since 31.0.0 |
|
| 3651 | - * |
|
| 3652 | - * @param int $calendarId |
|
| 3653 | - * |
|
| 3654 | - * @return void |
|
| 3655 | - */ |
|
| 3656 | - protected function purgeCalendarInvitations(int $calendarId): void { |
|
| 3657 | - // select all calendar object uid's |
|
| 3658 | - $cmd = $this->db->getQueryBuilder(); |
|
| 3659 | - $cmd->select('uid') |
|
| 3660 | - ->from($this->dbObjectsTable) |
|
| 3661 | - ->where($cmd->expr()->eq('calendarid', $cmd->createNamedParameter($calendarId))); |
|
| 3662 | - $allIds = $cmd->executeQuery()->fetchAll(\PDO::FETCH_COLUMN); |
|
| 3663 | - // delete all links that match object uid's |
|
| 3664 | - $cmd = $this->db->getQueryBuilder(); |
|
| 3665 | - $cmd->delete($this->dbObjectInvitationsTable) |
|
| 3666 | - ->where($cmd->expr()->in('uid', $cmd->createParameter('uids'), IQueryBuilder::PARAM_STR_ARRAY)); |
|
| 3667 | - foreach (array_chunk($allIds, 1000) as $chunkIds) { |
|
| 3668 | - $cmd->setParameter('uids', $chunkIds, IQueryBuilder::PARAM_STR_ARRAY); |
|
| 3669 | - $cmd->executeStatement(); |
|
| 3670 | - } |
|
| 3671 | - } |
|
| 3672 | - |
|
| 3673 | - /** |
|
| 3674 | - * Delete all invitations from a given calendar event |
|
| 3675 | - * |
|
| 3676 | - * @since 31.0.0 |
|
| 3677 | - * |
|
| 3678 | - * @param string $eventId UID of the event |
|
| 3679 | - * |
|
| 3680 | - * @return void |
|
| 3681 | - */ |
|
| 3682 | - protected function purgeObjectInvitations(string $eventId): void { |
|
| 3683 | - $cmd = $this->db->getQueryBuilder(); |
|
| 3684 | - $cmd->delete($this->dbObjectInvitationsTable) |
|
| 3685 | - ->where($cmd->expr()->eq('uid', $cmd->createNamedParameter($eventId, IQueryBuilder::PARAM_STR), IQueryBuilder::PARAM_STR)); |
|
| 3686 | - $cmd->executeStatement(); |
|
| 3687 | - } |
|
| 3688 | - |
|
| 3689 | - public function unshare(IShareable $shareable, string $principal): void { |
|
| 3690 | - $this->atomic(function () use ($shareable, $principal): void { |
|
| 3691 | - $calendarData = $this->getCalendarById($shareable->getResourceId()); |
|
| 3692 | - if ($calendarData === null) { |
|
| 3693 | - throw new \RuntimeException('Trying to update shares for non-existing calendar: ' . $shareable->getResourceId()); |
|
| 3694 | - } |
|
| 3695 | - |
|
| 3696 | - $oldShares = $this->getShares($shareable->getResourceId()); |
|
| 3697 | - $unshare = $this->calendarSharingBackend->unshare($shareable, $principal); |
|
| 3698 | - |
|
| 3699 | - if ($unshare) { |
|
| 3700 | - $this->dispatcher->dispatchTyped(new CalendarShareUpdatedEvent( |
|
| 3701 | - $shareable->getResourceId(), |
|
| 3702 | - $calendarData, |
|
| 3703 | - $oldShares, |
|
| 3704 | - [], |
|
| 3705 | - [$principal] |
|
| 3706 | - )); |
|
| 3707 | - } |
|
| 3708 | - }, $this->db); |
|
| 3709 | - } |
|
| 1044 | + /** |
|
| 1045 | + * Returns all calendar objects with limited metadata for a calendar |
|
| 1046 | + * |
|
| 1047 | + * Every item contains an array with the following keys: |
|
| 1048 | + * * id - the table row id |
|
| 1049 | + * * etag - An arbitrary string |
|
| 1050 | + * * uri - a unique key which will be used to construct the uri. This can |
|
| 1051 | + * be any arbitrary string. |
|
| 1052 | + * * calendardata - The iCalendar-compatible calendar data |
|
| 1053 | + * |
|
| 1054 | + * @param mixed $calendarId |
|
| 1055 | + * @param int $calendarType |
|
| 1056 | + * @return array |
|
| 1057 | + */ |
|
| 1058 | + public function getLimitedCalendarObjects(int $calendarId, int $calendarType = self::CALENDAR_TYPE_CALENDAR):array { |
|
| 1059 | + $query = $this->db->getQueryBuilder(); |
|
| 1060 | + $query->select(['id','uid', 'etag', 'uri', 'calendardata']) |
|
| 1061 | + ->from('calendarobjects') |
|
| 1062 | + ->where($query->expr()->eq('calendarid', $query->createNamedParameter($calendarId))) |
|
| 1063 | + ->andWhere($query->expr()->eq('calendartype', $query->createNamedParameter($calendarType))) |
|
| 1064 | + ->andWhere($query->expr()->isNull('deleted_at')); |
|
| 1065 | + $stmt = $query->executeQuery(); |
|
| 1066 | + |
|
| 1067 | + $result = []; |
|
| 1068 | + while (($row = $stmt->fetch()) !== false) { |
|
| 1069 | + $result[$row['uid']] = [ |
|
| 1070 | + 'id' => $row['id'], |
|
| 1071 | + 'etag' => $row['etag'], |
|
| 1072 | + 'uri' => $row['uri'], |
|
| 1073 | + 'calendardata' => $row['calendardata'], |
|
| 1074 | + ]; |
|
| 1075 | + } |
|
| 1076 | + $stmt->closeCursor(); |
|
| 1077 | + |
|
| 1078 | + return $result; |
|
| 1079 | + } |
|
| 1080 | + |
|
| 1081 | + /** |
|
| 1082 | + * Delete all of an user's shares |
|
| 1083 | + * |
|
| 1084 | + * @param string $principaluri |
|
| 1085 | + * @return void |
|
| 1086 | + */ |
|
| 1087 | + public function deleteAllSharesByUser($principaluri) { |
|
| 1088 | + $this->calendarSharingBackend->deleteAllSharesByUser($principaluri); |
|
| 1089 | + } |
|
| 1090 | + |
|
| 1091 | + /** |
|
| 1092 | + * Returns all calendar objects within a calendar. |
|
| 1093 | + * |
|
| 1094 | + * Every item contains an array with the following keys: |
|
| 1095 | + * * calendardata - The iCalendar-compatible calendar data |
|
| 1096 | + * * uri - a unique key which will be used to construct the uri. This can |
|
| 1097 | + * be any arbitrary string, but making sure it ends with '.ics' is a |
|
| 1098 | + * good idea. This is only the basename, or filename, not the full |
|
| 1099 | + * path. |
|
| 1100 | + * * lastmodified - a timestamp of the last modification time |
|
| 1101 | + * * etag - An arbitrary string, surrounded by double-quotes. (e.g.: |
|
| 1102 | + * '"abcdef"') |
|
| 1103 | + * * size - The size of the calendar objects, in bytes. |
|
| 1104 | + * * component - optional, a string containing the type of object, such |
|
| 1105 | + * as 'vevent' or 'vtodo'. If specified, this will be used to populate |
|
| 1106 | + * the Content-Type header. |
|
| 1107 | + * |
|
| 1108 | + * Note that the etag is optional, but it's highly encouraged to return for |
|
| 1109 | + * speed reasons. |
|
| 1110 | + * |
|
| 1111 | + * The calendardata is also optional. If it's not returned |
|
| 1112 | + * 'getCalendarObject' will be called later, which *is* expected to return |
|
| 1113 | + * calendardata. |
|
| 1114 | + * |
|
| 1115 | + * If neither etag or size are specified, the calendardata will be |
|
| 1116 | + * used/fetched to determine these numbers. If both are specified the |
|
| 1117 | + * amount of times this is needed is reduced by a great degree. |
|
| 1118 | + * |
|
| 1119 | + * @param mixed $calendarId |
|
| 1120 | + * @param int $calendarType |
|
| 1121 | + * @return array |
|
| 1122 | + */ |
|
| 1123 | + public function getCalendarObjects($calendarId, $calendarType = self::CALENDAR_TYPE_CALENDAR):array { |
|
| 1124 | + $query = $this->db->getQueryBuilder(); |
|
| 1125 | + $query->select(['id', 'uri', 'lastmodified', 'etag', 'calendarid', 'size', 'componenttype', 'classification']) |
|
| 1126 | + ->from('calendarobjects') |
|
| 1127 | + ->where($query->expr()->eq('calendarid', $query->createNamedParameter($calendarId))) |
|
| 1128 | + ->andWhere($query->expr()->eq('calendartype', $query->createNamedParameter($calendarType))) |
|
| 1129 | + ->andWhere($query->expr()->isNull('deleted_at')); |
|
| 1130 | + $stmt = $query->executeQuery(); |
|
| 1131 | + |
|
| 1132 | + $result = []; |
|
| 1133 | + while (($row = $stmt->fetch()) !== false) { |
|
| 1134 | + $result[] = [ |
|
| 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 | + 'component' => strtolower($row['componenttype']), |
|
| 1142 | + 'classification' => (int)$row['classification'] |
|
| 1143 | + ]; |
|
| 1144 | + } |
|
| 1145 | + $stmt->closeCursor(); |
|
| 1146 | + |
|
| 1147 | + return $result; |
|
| 1148 | + } |
|
| 1149 | + |
|
| 1150 | + public function getDeletedCalendarObjects(int $deletedBefore): array { |
|
| 1151 | + $query = $this->db->getQueryBuilder(); |
|
| 1152 | + $query->select(['co.id', 'co.uri', 'co.lastmodified', 'co.etag', 'co.calendarid', 'co.calendartype', 'co.size', 'co.componenttype', 'co.classification', 'co.deleted_at']) |
|
| 1153 | + ->from('calendarobjects', 'co') |
|
| 1154 | + ->join('co', 'calendars', 'c', $query->expr()->eq('c.id', 'co.calendarid', IQueryBuilder::PARAM_INT)) |
|
| 1155 | + ->where($query->expr()->isNotNull('co.deleted_at')) |
|
| 1156 | + ->andWhere($query->expr()->lt('co.deleted_at', $query->createNamedParameter($deletedBefore))); |
|
| 1157 | + $stmt = $query->executeQuery(); |
|
| 1158 | + |
|
| 1159 | + $result = []; |
|
| 1160 | + while (($row = $stmt->fetch()) !== false) { |
|
| 1161 | + $result[] = [ |
|
| 1162 | + 'id' => $row['id'], |
|
| 1163 | + 'uri' => $row['uri'], |
|
| 1164 | + 'lastmodified' => $row['lastmodified'], |
|
| 1165 | + 'etag' => '"' . $row['etag'] . '"', |
|
| 1166 | + 'calendarid' => (int)$row['calendarid'], |
|
| 1167 | + 'calendartype' => (int)$row['calendartype'], |
|
| 1168 | + 'size' => (int)$row['size'], |
|
| 1169 | + 'component' => strtolower($row['componenttype']), |
|
| 1170 | + 'classification' => (int)$row['classification'], |
|
| 1171 | + '{' . \OCA\DAV\DAV\Sharing\Plugin::NS_NEXTCLOUD . '}deleted-at' => $row['deleted_at'] === null ? $row['deleted_at'] : (int)$row['deleted_at'], |
|
| 1172 | + ]; |
|
| 1173 | + } |
|
| 1174 | + $stmt->closeCursor(); |
|
| 1175 | + |
|
| 1176 | + return $result; |
|
| 1177 | + } |
|
| 1178 | + |
|
| 1179 | + /** |
|
| 1180 | + * Return all deleted calendar objects by the given principal that are not |
|
| 1181 | + * in deleted calendars. |
|
| 1182 | + * |
|
| 1183 | + * @param string $principalUri |
|
| 1184 | + * @return array |
|
| 1185 | + * @throws Exception |
|
| 1186 | + */ |
|
| 1187 | + public function getDeletedCalendarObjectsByPrincipal(string $principalUri): array { |
|
| 1188 | + $query = $this->db->getQueryBuilder(); |
|
| 1189 | + $query->select(['co.id', 'co.uri', 'co.lastmodified', 'co.etag', 'co.calendarid', 'co.size', 'co.componenttype', 'co.classification', 'co.deleted_at']) |
|
| 1190 | + ->selectAlias('c.uri', 'calendaruri') |
|
| 1191 | + ->from('calendarobjects', 'co') |
|
| 1192 | + ->join('co', 'calendars', 'c', $query->expr()->eq('c.id', 'co.calendarid', IQueryBuilder::PARAM_INT)) |
|
| 1193 | + ->where($query->expr()->eq('principaluri', $query->createNamedParameter($principalUri))) |
|
| 1194 | + ->andWhere($query->expr()->isNotNull('co.deleted_at')) |
|
| 1195 | + ->andWhere($query->expr()->isNull('c.deleted_at')); |
|
| 1196 | + $stmt = $query->executeQuery(); |
|
| 1197 | + |
|
| 1198 | + $result = []; |
|
| 1199 | + while ($row = $stmt->fetch()) { |
|
| 1200 | + $result[] = [ |
|
| 1201 | + 'id' => $row['id'], |
|
| 1202 | + 'uri' => $row['uri'], |
|
| 1203 | + 'lastmodified' => $row['lastmodified'], |
|
| 1204 | + 'etag' => '"' . $row['etag'] . '"', |
|
| 1205 | + 'calendarid' => $row['calendarid'], |
|
| 1206 | + 'calendaruri' => $row['calendaruri'], |
|
| 1207 | + 'size' => (int)$row['size'], |
|
| 1208 | + 'component' => strtolower($row['componenttype']), |
|
| 1209 | + 'classification' => (int)$row['classification'], |
|
| 1210 | + '{' . \OCA\DAV\DAV\Sharing\Plugin::NS_NEXTCLOUD . '}deleted-at' => $row['deleted_at'] === null ? $row['deleted_at'] : (int)$row['deleted_at'], |
|
| 1211 | + ]; |
|
| 1212 | + } |
|
| 1213 | + $stmt->closeCursor(); |
|
| 1214 | + |
|
| 1215 | + return $result; |
|
| 1216 | + } |
|
| 1217 | + |
|
| 1218 | + /** |
|
| 1219 | + * Returns information from a single calendar object, based on it's object |
|
| 1220 | + * uri. |
|
| 1221 | + * |
|
| 1222 | + * The object uri is only the basename, or filename and not a full path. |
|
| 1223 | + * |
|
| 1224 | + * The returned array must have the same keys as getCalendarObjects. The |
|
| 1225 | + * 'calendardata' object is required here though, while it's not required |
|
| 1226 | + * for getCalendarObjects. |
|
| 1227 | + * |
|
| 1228 | + * This method must return null if the object did not exist. |
|
| 1229 | + * |
|
| 1230 | + * @param mixed $calendarId |
|
| 1231 | + * @param string $objectUri |
|
| 1232 | + * @param int $calendarType |
|
| 1233 | + * @return array|null |
|
| 1234 | + */ |
|
| 1235 | + public function getCalendarObject($calendarId, $objectUri, int $calendarType = self::CALENDAR_TYPE_CALENDAR) { |
|
| 1236 | + $key = $calendarId . '::' . $objectUri . '::' . $calendarType; |
|
| 1237 | + if (isset($this->cachedObjects[$key])) { |
|
| 1238 | + return $this->cachedObjects[$key]; |
|
| 1239 | + } |
|
| 1240 | + $query = $this->db->getQueryBuilder(); |
|
| 1241 | + $query->select(['id', 'uri', 'uid', 'lastmodified', 'etag', 'calendarid', 'size', 'calendardata', 'componenttype', 'classification', 'deleted_at']) |
|
| 1242 | + ->from('calendarobjects') |
|
| 1243 | + ->where($query->expr()->eq('calendarid', $query->createNamedParameter($calendarId))) |
|
| 1244 | + ->andWhere($query->expr()->eq('uri', $query->createNamedParameter($objectUri))) |
|
| 1245 | + ->andWhere($query->expr()->eq('calendartype', $query->createNamedParameter($calendarType))); |
|
| 1246 | + $stmt = $query->executeQuery(); |
|
| 1247 | + $row = $stmt->fetch(); |
|
| 1248 | + $stmt->closeCursor(); |
|
| 1249 | + |
|
| 1250 | + if (!$row) { |
|
| 1251 | + return null; |
|
| 1252 | + } |
|
| 1253 | + |
|
| 1254 | + $object = $this->rowToCalendarObject($row); |
|
| 1255 | + $this->cachedObjects[$key] = $object; |
|
| 1256 | + return $object; |
|
| 1257 | + } |
|
| 1258 | + |
|
| 1259 | + private function rowToCalendarObject(array $row): array { |
|
| 1260 | + return [ |
|
| 1261 | + 'id' => $row['id'], |
|
| 1262 | + 'uri' => $row['uri'], |
|
| 1263 | + 'uid' => $row['uid'], |
|
| 1264 | + 'lastmodified' => $row['lastmodified'], |
|
| 1265 | + 'etag' => '"' . $row['etag'] . '"', |
|
| 1266 | + 'calendarid' => $row['calendarid'], |
|
| 1267 | + 'size' => (int)$row['size'], |
|
| 1268 | + 'calendardata' => $this->readBlob($row['calendardata']), |
|
| 1269 | + 'component' => strtolower($row['componenttype']), |
|
| 1270 | + 'classification' => (int)$row['classification'], |
|
| 1271 | + '{' . \OCA\DAV\DAV\Sharing\Plugin::NS_NEXTCLOUD . '}deleted-at' => $row['deleted_at'] === null ? $row['deleted_at'] : (int)$row['deleted_at'], |
|
| 1272 | + ]; |
|
| 1273 | + } |
|
| 1274 | + |
|
| 1275 | + /** |
|
| 1276 | + * Returns a list of calendar objects. |
|
| 1277 | + * |
|
| 1278 | + * This method should work identical to getCalendarObject, but instead |
|
| 1279 | + * return all the calendar objects in the list as an array. |
|
| 1280 | + * |
|
| 1281 | + * If the backend supports this, it may allow for some speed-ups. |
|
| 1282 | + * |
|
| 1283 | + * @param mixed $calendarId |
|
| 1284 | + * @param string[] $uris |
|
| 1285 | + * @param int $calendarType |
|
| 1286 | + * @return array |
|
| 1287 | + */ |
|
| 1288 | + public function getMultipleCalendarObjects($calendarId, array $uris, $calendarType = self::CALENDAR_TYPE_CALENDAR):array { |
|
| 1289 | + if (empty($uris)) { |
|
| 1290 | + return []; |
|
| 1291 | + } |
|
| 1292 | + |
|
| 1293 | + $chunks = array_chunk($uris, 100); |
|
| 1294 | + $objects = []; |
|
| 1295 | + |
|
| 1296 | + $query = $this->db->getQueryBuilder(); |
|
| 1297 | + $query->select(['id', 'uri', 'lastmodified', 'etag', 'calendarid', 'size', 'calendardata', 'componenttype', 'classification']) |
|
| 1298 | + ->from('calendarobjects') |
|
| 1299 | + ->where($query->expr()->eq('calendarid', $query->createNamedParameter($calendarId))) |
|
| 1300 | + ->andWhere($query->expr()->in('uri', $query->createParameter('uri'))) |
|
| 1301 | + ->andWhere($query->expr()->eq('calendartype', $query->createNamedParameter($calendarType))) |
|
| 1302 | + ->andWhere($query->expr()->isNull('deleted_at')); |
|
| 1303 | + |
|
| 1304 | + foreach ($chunks as $uris) { |
|
| 1305 | + $query->setParameter('uri', $uris, IQueryBuilder::PARAM_STR_ARRAY); |
|
| 1306 | + $result = $query->executeQuery(); |
|
| 1307 | + |
|
| 1308 | + while ($row = $result->fetch()) { |
|
| 1309 | + $objects[] = [ |
|
| 1310 | + 'id' => $row['id'], |
|
| 1311 | + 'uri' => $row['uri'], |
|
| 1312 | + 'lastmodified' => $row['lastmodified'], |
|
| 1313 | + 'etag' => '"' . $row['etag'] . '"', |
|
| 1314 | + 'calendarid' => $row['calendarid'], |
|
| 1315 | + 'size' => (int)$row['size'], |
|
| 1316 | + 'calendardata' => $this->readBlob($row['calendardata']), |
|
| 1317 | + 'component' => strtolower($row['componenttype']), |
|
| 1318 | + 'classification' => (int)$row['classification'] |
|
| 1319 | + ]; |
|
| 1320 | + } |
|
| 1321 | + $result->closeCursor(); |
|
| 1322 | + } |
|
| 1323 | + |
|
| 1324 | + return $objects; |
|
| 1325 | + } |
|
| 1326 | + |
|
| 1327 | + /** |
|
| 1328 | + * Creates a new calendar object. |
|
| 1329 | + * |
|
| 1330 | + * The object uri is only the basename, or filename and not a full path. |
|
| 1331 | + * |
|
| 1332 | + * It is possible return an etag from this function, which will be used in |
|
| 1333 | + * the response to this PUT request. Note that the ETag must be surrounded |
|
| 1334 | + * by double-quotes. |
|
| 1335 | + * |
|
| 1336 | + * However, you should only really return this ETag if you don't mangle the |
|
| 1337 | + * calendar-data. If the result of a subsequent GET to this object is not |
|
| 1338 | + * the exact same as this request body, you should omit the ETag. |
|
| 1339 | + * |
|
| 1340 | + * @param mixed $calendarId |
|
| 1341 | + * @param string $objectUri |
|
| 1342 | + * @param string $calendarData |
|
| 1343 | + * @param int $calendarType |
|
| 1344 | + * @return string |
|
| 1345 | + */ |
|
| 1346 | + public function createCalendarObject($calendarId, $objectUri, $calendarData, $calendarType = self::CALENDAR_TYPE_CALENDAR) { |
|
| 1347 | + $this->cachedObjects = []; |
|
| 1348 | + $extraData = $this->getDenormalizedData($calendarData); |
|
| 1349 | + |
|
| 1350 | + return $this->atomic(function () use ($calendarId, $objectUri, $calendarData, $extraData, $calendarType) { |
|
| 1351 | + // Try to detect duplicates |
|
| 1352 | + $qb = $this->db->getQueryBuilder(); |
|
| 1353 | + $qb->select($qb->func()->count('*')) |
|
| 1354 | + ->from('calendarobjects') |
|
| 1355 | + ->where($qb->expr()->eq('calendarid', $qb->createNamedParameter($calendarId))) |
|
| 1356 | + ->andWhere($qb->expr()->eq('uid', $qb->createNamedParameter($extraData['uid']))) |
|
| 1357 | + ->andWhere($qb->expr()->eq('calendartype', $qb->createNamedParameter($calendarType))) |
|
| 1358 | + ->andWhere($qb->expr()->isNull('deleted_at')); |
|
| 1359 | + $result = $qb->executeQuery(); |
|
| 1360 | + $count = (int)$result->fetchOne(); |
|
| 1361 | + $result->closeCursor(); |
|
| 1362 | + |
|
| 1363 | + if ($count !== 0) { |
|
| 1364 | + throw new BadRequest('Calendar object with uid already exists in this calendar collection.'); |
|
| 1365 | + } |
|
| 1366 | + // For a more specific error message we also try to explicitly look up the UID but as a deleted entry |
|
| 1367 | + $qbDel = $this->db->getQueryBuilder(); |
|
| 1368 | + $qbDel->select('*') |
|
| 1369 | + ->from('calendarobjects') |
|
| 1370 | + ->where($qbDel->expr()->eq('calendarid', $qbDel->createNamedParameter($calendarId))) |
|
| 1371 | + ->andWhere($qbDel->expr()->eq('uid', $qbDel->createNamedParameter($extraData['uid']))) |
|
| 1372 | + ->andWhere($qbDel->expr()->eq('calendartype', $qbDel->createNamedParameter($calendarType))) |
|
| 1373 | + ->andWhere($qbDel->expr()->isNotNull('deleted_at')); |
|
| 1374 | + $result = $qbDel->executeQuery(); |
|
| 1375 | + $found = $result->fetch(); |
|
| 1376 | + $result->closeCursor(); |
|
| 1377 | + if ($found !== false) { |
|
| 1378 | + // the object existed previously but has been deleted |
|
| 1379 | + // remove the trashbin entry and continue as if it was a new object |
|
| 1380 | + $this->deleteCalendarObject($calendarId, $found['uri']); |
|
| 1381 | + } |
|
| 1382 | + |
|
| 1383 | + $query = $this->db->getQueryBuilder(); |
|
| 1384 | + $query->insert('calendarobjects') |
|
| 1385 | + ->values([ |
|
| 1386 | + 'calendarid' => $query->createNamedParameter($calendarId), |
|
| 1387 | + 'uri' => $query->createNamedParameter($objectUri), |
|
| 1388 | + 'calendardata' => $query->createNamedParameter($calendarData, IQueryBuilder::PARAM_LOB), |
|
| 1389 | + 'lastmodified' => $query->createNamedParameter(time()), |
|
| 1390 | + 'etag' => $query->createNamedParameter($extraData['etag']), |
|
| 1391 | + 'size' => $query->createNamedParameter($extraData['size']), |
|
| 1392 | + 'componenttype' => $query->createNamedParameter($extraData['componentType']), |
|
| 1393 | + 'firstoccurence' => $query->createNamedParameter($extraData['firstOccurence']), |
|
| 1394 | + 'lastoccurence' => $query->createNamedParameter($extraData['lastOccurence']), |
|
| 1395 | + 'classification' => $query->createNamedParameter($extraData['classification']), |
|
| 1396 | + 'uid' => $query->createNamedParameter($extraData['uid']), |
|
| 1397 | + 'calendartype' => $query->createNamedParameter($calendarType), |
|
| 1398 | + ]) |
|
| 1399 | + ->executeStatement(); |
|
| 1400 | + |
|
| 1401 | + $this->updateProperties($calendarId, $objectUri, $calendarData, $calendarType); |
|
| 1402 | + $this->addChanges($calendarId, [$objectUri], 1, $calendarType); |
|
| 1403 | + |
|
| 1404 | + $objectRow = $this->getCalendarObject($calendarId, $objectUri, $calendarType); |
|
| 1405 | + assert($objectRow !== null); |
|
| 1406 | + |
|
| 1407 | + if ($calendarType === self::CALENDAR_TYPE_CALENDAR) { |
|
| 1408 | + $calendarRow = $this->getCalendarById($calendarId); |
|
| 1409 | + $shares = $this->getShares($calendarId); |
|
| 1410 | + |
|
| 1411 | + $this->dispatcher->dispatchTyped(new CalendarObjectCreatedEvent($calendarId, $calendarRow, $shares, $objectRow)); |
|
| 1412 | + } else { |
|
| 1413 | + $subscriptionRow = $this->getSubscriptionById($calendarId); |
|
| 1414 | + |
|
| 1415 | + $this->dispatcher->dispatchTyped(new CachedCalendarObjectCreatedEvent($calendarId, $subscriptionRow, [], $objectRow)); |
|
| 1416 | + } |
|
| 1417 | + |
|
| 1418 | + return '"' . $extraData['etag'] . '"'; |
|
| 1419 | + }, $this->db); |
|
| 1420 | + } |
|
| 1421 | + |
|
| 1422 | + /** |
|
| 1423 | + * Updates an existing calendarobject, based on it's uri. |
|
| 1424 | + * |
|
| 1425 | + * The object uri is only the basename, or filename and not a full path. |
|
| 1426 | + * |
|
| 1427 | + * It is possible return an etag from this function, which will be used in |
|
| 1428 | + * the response to this PUT request. Note that the ETag must be surrounded |
|
| 1429 | + * by double-quotes. |
|
| 1430 | + * |
|
| 1431 | + * However, you should only really return this ETag if you don't mangle the |
|
| 1432 | + * calendar-data. If the result of a subsequent GET to this object is not |
|
| 1433 | + * the exact same as this request body, you should omit the ETag. |
|
| 1434 | + * |
|
| 1435 | + * @param mixed $calendarId |
|
| 1436 | + * @param string $objectUri |
|
| 1437 | + * @param string $calendarData |
|
| 1438 | + * @param int $calendarType |
|
| 1439 | + * @return string |
|
| 1440 | + */ |
|
| 1441 | + public function updateCalendarObject($calendarId, $objectUri, $calendarData, $calendarType = self::CALENDAR_TYPE_CALENDAR) { |
|
| 1442 | + $this->cachedObjects = []; |
|
| 1443 | + $extraData = $this->getDenormalizedData($calendarData); |
|
| 1444 | + |
|
| 1445 | + return $this->atomic(function () use ($calendarId, $objectUri, $calendarData, $extraData, $calendarType) { |
|
| 1446 | + $query = $this->db->getQueryBuilder(); |
|
| 1447 | + $query->update('calendarobjects') |
|
| 1448 | + ->set('calendardata', $query->createNamedParameter($calendarData, IQueryBuilder::PARAM_LOB)) |
|
| 1449 | + ->set('lastmodified', $query->createNamedParameter(time())) |
|
| 1450 | + ->set('etag', $query->createNamedParameter($extraData['etag'])) |
|
| 1451 | + ->set('size', $query->createNamedParameter($extraData['size'])) |
|
| 1452 | + ->set('componenttype', $query->createNamedParameter($extraData['componentType'])) |
|
| 1453 | + ->set('firstoccurence', $query->createNamedParameter($extraData['firstOccurence'])) |
|
| 1454 | + ->set('lastoccurence', $query->createNamedParameter($extraData['lastOccurence'])) |
|
| 1455 | + ->set('classification', $query->createNamedParameter($extraData['classification'])) |
|
| 1456 | + ->set('uid', $query->createNamedParameter($extraData['uid'])) |
|
| 1457 | + ->where($query->expr()->eq('calendarid', $query->createNamedParameter($calendarId))) |
|
| 1458 | + ->andWhere($query->expr()->eq('uri', $query->createNamedParameter($objectUri))) |
|
| 1459 | + ->andWhere($query->expr()->eq('calendartype', $query->createNamedParameter($calendarType))) |
|
| 1460 | + ->executeStatement(); |
|
| 1461 | + |
|
| 1462 | + $this->updateProperties($calendarId, $objectUri, $calendarData, $calendarType); |
|
| 1463 | + $this->addChanges($calendarId, [$objectUri], 2, $calendarType); |
|
| 1464 | + |
|
| 1465 | + $objectRow = $this->getCalendarObject($calendarId, $objectUri, $calendarType); |
|
| 1466 | + if (is_array($objectRow)) { |
|
| 1467 | + if ($calendarType === self::CALENDAR_TYPE_CALENDAR) { |
|
| 1468 | + $calendarRow = $this->getCalendarById($calendarId); |
|
| 1469 | + $shares = $this->getShares($calendarId); |
|
| 1470 | + |
|
| 1471 | + $this->dispatcher->dispatchTyped(new CalendarObjectUpdatedEvent($calendarId, $calendarRow, $shares, $objectRow)); |
|
| 1472 | + } else { |
|
| 1473 | + $subscriptionRow = $this->getSubscriptionById($calendarId); |
|
| 1474 | + |
|
| 1475 | + $this->dispatcher->dispatchTyped(new CachedCalendarObjectUpdatedEvent($calendarId, $subscriptionRow, [], $objectRow)); |
|
| 1476 | + } |
|
| 1477 | + } |
|
| 1478 | + |
|
| 1479 | + return '"' . $extraData['etag'] . '"'; |
|
| 1480 | + }, $this->db); |
|
| 1481 | + } |
|
| 1482 | + |
|
| 1483 | + /** |
|
| 1484 | + * Moves a calendar object from calendar to calendar. |
|
| 1485 | + * |
|
| 1486 | + * @param string $sourcePrincipalUri |
|
| 1487 | + * @param int $sourceObjectId |
|
| 1488 | + * @param string $targetPrincipalUri |
|
| 1489 | + * @param int $targetCalendarId |
|
| 1490 | + * @param string $tragetObjectUri |
|
| 1491 | + * @param int $calendarType |
|
| 1492 | + * @return bool |
|
| 1493 | + * @throws Exception |
|
| 1494 | + */ |
|
| 1495 | + public function moveCalendarObject(string $sourcePrincipalUri, int $sourceObjectId, string $targetPrincipalUri, int $targetCalendarId, string $tragetObjectUri, int $calendarType = self::CALENDAR_TYPE_CALENDAR): bool { |
|
| 1496 | + $this->cachedObjects = []; |
|
| 1497 | + return $this->atomic(function () use ($sourcePrincipalUri, $sourceObjectId, $targetPrincipalUri, $targetCalendarId, $tragetObjectUri, $calendarType) { |
|
| 1498 | + $object = $this->getCalendarObjectById($sourcePrincipalUri, $sourceObjectId); |
|
| 1499 | + if (empty($object)) { |
|
| 1500 | + return false; |
|
| 1501 | + } |
|
| 1502 | + |
|
| 1503 | + $sourceCalendarId = $object['calendarid']; |
|
| 1504 | + $sourceObjectUri = $object['uri']; |
|
| 1505 | + |
|
| 1506 | + $query = $this->db->getQueryBuilder(); |
|
| 1507 | + $query->update('calendarobjects') |
|
| 1508 | + ->set('calendarid', $query->createNamedParameter($targetCalendarId, IQueryBuilder::PARAM_INT)) |
|
| 1509 | + ->set('uri', $query->createNamedParameter($tragetObjectUri, IQueryBuilder::PARAM_STR)) |
|
| 1510 | + ->where($query->expr()->eq('id', $query->createNamedParameter($sourceObjectId, IQueryBuilder::PARAM_INT), IQueryBuilder::PARAM_INT)) |
|
| 1511 | + ->executeStatement(); |
|
| 1512 | + |
|
| 1513 | + $this->purgeProperties($sourceCalendarId, $sourceObjectId); |
|
| 1514 | + $this->updateProperties($targetCalendarId, $tragetObjectUri, $object['calendardata'], $calendarType); |
|
| 1515 | + |
|
| 1516 | + $this->addChanges($sourceCalendarId, [$sourceObjectUri], 3, $calendarType); |
|
| 1517 | + $this->addChanges($targetCalendarId, [$tragetObjectUri], 1, $calendarType); |
|
| 1518 | + |
|
| 1519 | + $object = $this->getCalendarObjectById($targetPrincipalUri, $sourceObjectId); |
|
| 1520 | + // Calendar Object wasn't found - possibly because it was deleted in the meantime by a different client |
|
| 1521 | + if (empty($object)) { |
|
| 1522 | + return false; |
|
| 1523 | + } |
|
| 1524 | + |
|
| 1525 | + $targetCalendarRow = $this->getCalendarById($targetCalendarId); |
|
| 1526 | + // the calendar this event is being moved to does not exist any longer |
|
| 1527 | + if (empty($targetCalendarRow)) { |
|
| 1528 | + return false; |
|
| 1529 | + } |
|
| 1530 | + |
|
| 1531 | + if ($calendarType === self::CALENDAR_TYPE_CALENDAR) { |
|
| 1532 | + $sourceShares = $this->getShares($sourceCalendarId); |
|
| 1533 | + $targetShares = $this->getShares($targetCalendarId); |
|
| 1534 | + $sourceCalendarRow = $this->getCalendarById($sourceCalendarId); |
|
| 1535 | + $this->dispatcher->dispatchTyped(new CalendarObjectMovedEvent($sourceCalendarId, $sourceCalendarRow, $targetCalendarId, $targetCalendarRow, $sourceShares, $targetShares, $object)); |
|
| 1536 | + } |
|
| 1537 | + return true; |
|
| 1538 | + }, $this->db); |
|
| 1539 | + } |
|
| 1540 | + |
|
| 1541 | + |
|
| 1542 | + /** |
|
| 1543 | + * @param int $calendarObjectId |
|
| 1544 | + * @param int $classification |
|
| 1545 | + */ |
|
| 1546 | + public function setClassification($calendarObjectId, $classification) { |
|
| 1547 | + $this->cachedObjects = []; |
|
| 1548 | + if (!in_array($classification, [ |
|
| 1549 | + self::CLASSIFICATION_PUBLIC, self::CLASSIFICATION_PRIVATE, self::CLASSIFICATION_CONFIDENTIAL |
|
| 1550 | + ])) { |
|
| 1551 | + throw new \InvalidArgumentException(); |
|
| 1552 | + } |
|
| 1553 | + $query = $this->db->getQueryBuilder(); |
|
| 1554 | + $query->update('calendarobjects') |
|
| 1555 | + ->set('classification', $query->createNamedParameter($classification)) |
|
| 1556 | + ->where($query->expr()->eq('id', $query->createNamedParameter($calendarObjectId))) |
|
| 1557 | + ->executeStatement(); |
|
| 1558 | + } |
|
| 1559 | + |
|
| 1560 | + /** |
|
| 1561 | + * Deletes an existing calendar object. |
|
| 1562 | + * |
|
| 1563 | + * The object uri is only the basename, or filename and not a full path. |
|
| 1564 | + * |
|
| 1565 | + * @param mixed $calendarId |
|
| 1566 | + * @param string $objectUri |
|
| 1567 | + * @param int $calendarType |
|
| 1568 | + * @param bool $forceDeletePermanently |
|
| 1569 | + * @return void |
|
| 1570 | + */ |
|
| 1571 | + public function deleteCalendarObject($calendarId, $objectUri, $calendarType = self::CALENDAR_TYPE_CALENDAR, bool $forceDeletePermanently = false) { |
|
| 1572 | + $this->cachedObjects = []; |
|
| 1573 | + $this->atomic(function () use ($calendarId, $objectUri, $calendarType, $forceDeletePermanently): void { |
|
| 1574 | + $data = $this->getCalendarObject($calendarId, $objectUri, $calendarType); |
|
| 1575 | + |
|
| 1576 | + if ($data === null) { |
|
| 1577 | + // Nothing to delete |
|
| 1578 | + return; |
|
| 1579 | + } |
|
| 1580 | + |
|
| 1581 | + if ($forceDeletePermanently || $this->config->getAppValue(Application::APP_ID, RetentionService::RETENTION_CONFIG_KEY) === '0') { |
|
| 1582 | + $stmt = $this->db->prepare('DELETE FROM `*PREFIX*calendarobjects` WHERE `calendarid` = ? AND `uri` = ? AND `calendartype` = ?'); |
|
| 1583 | + $stmt->execute([$calendarId, $objectUri, $calendarType]); |
|
| 1584 | + |
|
| 1585 | + $this->purgeProperties($calendarId, $data['id']); |
|
| 1586 | + |
|
| 1587 | + $this->purgeObjectInvitations($data['uid']); |
|
| 1588 | + |
|
| 1589 | + if ($calendarType === self::CALENDAR_TYPE_CALENDAR) { |
|
| 1590 | + $calendarRow = $this->getCalendarById($calendarId); |
|
| 1591 | + $shares = $this->getShares($calendarId); |
|
| 1592 | + |
|
| 1593 | + $this->dispatcher->dispatchTyped(new CalendarObjectDeletedEvent($calendarId, $calendarRow, $shares, $data)); |
|
| 1594 | + } else { |
|
| 1595 | + $subscriptionRow = $this->getSubscriptionById($calendarId); |
|
| 1596 | + |
|
| 1597 | + $this->dispatcher->dispatchTyped(new CachedCalendarObjectDeletedEvent($calendarId, $subscriptionRow, [], $data)); |
|
| 1598 | + } |
|
| 1599 | + } else { |
|
| 1600 | + $pathInfo = pathinfo($data['uri']); |
|
| 1601 | + if (!empty($pathInfo['extension'])) { |
|
| 1602 | + // Append a suffix to "free" the old URI for recreation |
|
| 1603 | + $newUri = sprintf( |
|
| 1604 | + '%s-deleted.%s', |
|
| 1605 | + $pathInfo['filename'], |
|
| 1606 | + $pathInfo['extension'] |
|
| 1607 | + ); |
|
| 1608 | + } else { |
|
| 1609 | + $newUri = sprintf( |
|
| 1610 | + '%s-deleted', |
|
| 1611 | + $pathInfo['filename'] |
|
| 1612 | + ); |
|
| 1613 | + } |
|
| 1614 | + |
|
| 1615 | + // Try to detect conflicts before the DB does |
|
| 1616 | + // As unlikely as it seems, this can happen when the user imports, then deletes, imports and deletes again |
|
| 1617 | + $newObject = $this->getCalendarObject($calendarId, $newUri, $calendarType); |
|
| 1618 | + if ($newObject !== null) { |
|
| 1619 | + throw new Forbidden("A calendar object with URI $newUri already exists in calendar $calendarId, therefore this object can't be moved into the trashbin"); |
|
| 1620 | + } |
|
| 1621 | + |
|
| 1622 | + $qb = $this->db->getQueryBuilder(); |
|
| 1623 | + $markObjectDeletedQuery = $qb->update('calendarobjects') |
|
| 1624 | + ->set('deleted_at', $qb->createNamedParameter(time(), IQueryBuilder::PARAM_INT)) |
|
| 1625 | + ->set('uri', $qb->createNamedParameter($newUri)) |
|
| 1626 | + ->where( |
|
| 1627 | + $qb->expr()->eq('calendarid', $qb->createNamedParameter($calendarId)), |
|
| 1628 | + $qb->expr()->eq('calendartype', $qb->createNamedParameter($calendarType, IQueryBuilder::PARAM_INT), IQueryBuilder::PARAM_INT), |
|
| 1629 | + $qb->expr()->eq('uri', $qb->createNamedParameter($objectUri)) |
|
| 1630 | + ); |
|
| 1631 | + $markObjectDeletedQuery->executeStatement(); |
|
| 1632 | + |
|
| 1633 | + $calendarData = $this->getCalendarById($calendarId); |
|
| 1634 | + if ($calendarData !== null) { |
|
| 1635 | + $this->dispatcher->dispatchTyped( |
|
| 1636 | + new CalendarObjectMovedToTrashEvent( |
|
| 1637 | + $calendarId, |
|
| 1638 | + $calendarData, |
|
| 1639 | + $this->getShares($calendarId), |
|
| 1640 | + $data |
|
| 1641 | + ) |
|
| 1642 | + ); |
|
| 1643 | + } |
|
| 1644 | + } |
|
| 1645 | + |
|
| 1646 | + $this->addChanges($calendarId, [$objectUri], 3, $calendarType); |
|
| 1647 | + }, $this->db); |
|
| 1648 | + } |
|
| 1649 | + |
|
| 1650 | + /** |
|
| 1651 | + * @param mixed $objectData |
|
| 1652 | + * |
|
| 1653 | + * @throws Forbidden |
|
| 1654 | + */ |
|
| 1655 | + public function restoreCalendarObject(array $objectData): void { |
|
| 1656 | + $this->cachedObjects = []; |
|
| 1657 | + $this->atomic(function () use ($objectData): void { |
|
| 1658 | + $id = (int)$objectData['id']; |
|
| 1659 | + $restoreUri = str_replace('-deleted.ics', '.ics', $objectData['uri']); |
|
| 1660 | + $targetObject = $this->getCalendarObject( |
|
| 1661 | + $objectData['calendarid'], |
|
| 1662 | + $restoreUri |
|
| 1663 | + ); |
|
| 1664 | + if ($targetObject !== null) { |
|
| 1665 | + throw new Forbidden("Can not restore calendar $id because a calendar object with the URI $restoreUri already exists"); |
|
| 1666 | + } |
|
| 1667 | + |
|
| 1668 | + $qb = $this->db->getQueryBuilder(); |
|
| 1669 | + $update = $qb->update('calendarobjects') |
|
| 1670 | + ->set('uri', $qb->createNamedParameter($restoreUri)) |
|
| 1671 | + ->set('deleted_at', $qb->createNamedParameter(null)) |
|
| 1672 | + ->where($qb->expr()->eq('id', $qb->createNamedParameter($id, IQueryBuilder::PARAM_INT), IQueryBuilder::PARAM_INT)); |
|
| 1673 | + $update->executeStatement(); |
|
| 1674 | + |
|
| 1675 | + // Make sure this change is tracked in the changes table |
|
| 1676 | + $qb2 = $this->db->getQueryBuilder(); |
|
| 1677 | + $selectObject = $qb2->select('calendardata', 'uri', 'calendarid', 'calendartype') |
|
| 1678 | + ->selectAlias('componenttype', 'component') |
|
| 1679 | + ->from('calendarobjects') |
|
| 1680 | + ->where($qb2->expr()->eq('id', $qb2->createNamedParameter($id, IQueryBuilder::PARAM_INT), IQueryBuilder::PARAM_INT)); |
|
| 1681 | + $result = $selectObject->executeQuery(); |
|
| 1682 | + $row = $result->fetch(); |
|
| 1683 | + $result->closeCursor(); |
|
| 1684 | + if ($row === false) { |
|
| 1685 | + // Welp, this should possibly not have happened, but let's ignore |
|
| 1686 | + return; |
|
| 1687 | + } |
|
| 1688 | + $this->addChanges($row['calendarid'], [$row['uri']], 1, (int)$row['calendartype']); |
|
| 1689 | + |
|
| 1690 | + $calendarRow = $this->getCalendarById((int)$row['calendarid']); |
|
| 1691 | + if ($calendarRow === null) { |
|
| 1692 | + throw new RuntimeException('Calendar object data that was just written can\'t be read back. Check your database configuration.'); |
|
| 1693 | + } |
|
| 1694 | + $this->dispatcher->dispatchTyped( |
|
| 1695 | + new CalendarObjectRestoredEvent( |
|
| 1696 | + (int)$objectData['calendarid'], |
|
| 1697 | + $calendarRow, |
|
| 1698 | + $this->getShares((int)$row['calendarid']), |
|
| 1699 | + $row |
|
| 1700 | + ) |
|
| 1701 | + ); |
|
| 1702 | + }, $this->db); |
|
| 1703 | + } |
|
| 1704 | + |
|
| 1705 | + /** |
|
| 1706 | + * Performs a calendar-query on the contents of this calendar. |
|
| 1707 | + * |
|
| 1708 | + * The calendar-query is defined in RFC4791 : CalDAV. Using the |
|
| 1709 | + * calendar-query it is possible for a client to request a specific set of |
|
| 1710 | + * object, based on contents of iCalendar properties, date-ranges and |
|
| 1711 | + * iCalendar component types (VTODO, VEVENT). |
|
| 1712 | + * |
|
| 1713 | + * This method should just return a list of (relative) urls that match this |
|
| 1714 | + * query. |
|
| 1715 | + * |
|
| 1716 | + * The list of filters are specified as an array. The exact array is |
|
| 1717 | + * documented by Sabre\CalDAV\CalendarQueryParser. |
|
| 1718 | + * |
|
| 1719 | + * Note that it is extremely likely that getCalendarObject for every path |
|
| 1720 | + * returned from this method will be called almost immediately after. You |
|
| 1721 | + * may want to anticipate this to speed up these requests. |
|
| 1722 | + * |
|
| 1723 | + * This method provides a default implementation, which parses *all* the |
|
| 1724 | + * iCalendar objects in the specified calendar. |
|
| 1725 | + * |
|
| 1726 | + * This default may well be good enough for personal use, and calendars |
|
| 1727 | + * that aren't very large. But if you anticipate high usage, big calendars |
|
| 1728 | + * or high loads, you are strongly advised to optimize certain paths. |
|
| 1729 | + * |
|
| 1730 | + * The best way to do so is override this method and to optimize |
|
| 1731 | + * specifically for 'common filters'. |
|
| 1732 | + * |
|
| 1733 | + * Requests that are extremely common are: |
|
| 1734 | + * * requests for just VEVENTS |
|
| 1735 | + * * requests for just VTODO |
|
| 1736 | + * * requests with a time-range-filter on either VEVENT or VTODO. |
|
| 1737 | + * |
|
| 1738 | + * ..and combinations of these requests. It may not be worth it to try to |
|
| 1739 | + * handle every possible situation and just rely on the (relatively |
|
| 1740 | + * easy to use) CalendarQueryValidator to handle the rest. |
|
| 1741 | + * |
|
| 1742 | + * Note that especially time-range-filters may be difficult to parse. A |
|
| 1743 | + * time-range filter specified on a VEVENT must for instance also handle |
|
| 1744 | + * recurrence rules correctly. |
|
| 1745 | + * A good example of how to interpret all these filters can also simply |
|
| 1746 | + * be found in Sabre\CalDAV\CalendarQueryFilter. This class is as correct |
|
| 1747 | + * as possible, so it gives you a good idea on what type of stuff you need |
|
| 1748 | + * to think of. |
|
| 1749 | + * |
|
| 1750 | + * @param mixed $calendarId |
|
| 1751 | + * @param array $filters |
|
| 1752 | + * @param int $calendarType |
|
| 1753 | + * @return array |
|
| 1754 | + */ |
|
| 1755 | + public function calendarQuery($calendarId, array $filters, $calendarType = self::CALENDAR_TYPE_CALENDAR):array { |
|
| 1756 | + $componentType = null; |
|
| 1757 | + $requirePostFilter = true; |
|
| 1758 | + $timeRange = null; |
|
| 1759 | + |
|
| 1760 | + // if no filters were specified, we don't need to filter after a query |
|
| 1761 | + if (!$filters['prop-filters'] && !$filters['comp-filters']) { |
|
| 1762 | + $requirePostFilter = false; |
|
| 1763 | + } |
|
| 1764 | + |
|
| 1765 | + // Figuring out if there's a component filter |
|
| 1766 | + if (count($filters['comp-filters']) > 0 && !$filters['comp-filters'][0]['is-not-defined']) { |
|
| 1767 | + $componentType = $filters['comp-filters'][0]['name']; |
|
| 1768 | + |
|
| 1769 | + // Checking if we need post-filters |
|
| 1770 | + if (!$filters['prop-filters'] && !$filters['comp-filters'][0]['comp-filters'] && !$filters['comp-filters'][0]['time-range'] && !$filters['comp-filters'][0]['prop-filters']) { |
|
| 1771 | + $requirePostFilter = false; |
|
| 1772 | + } |
|
| 1773 | + // There was a time-range filter |
|
| 1774 | + if ($componentType === 'VEVENT' && isset($filters['comp-filters'][0]['time-range']) && is_array($filters['comp-filters'][0]['time-range'])) { |
|
| 1775 | + $timeRange = $filters['comp-filters'][0]['time-range']; |
|
| 1776 | + |
|
| 1777 | + // If start time OR the end time is not specified, we can do a |
|
| 1778 | + // 100% accurate mysql query. |
|
| 1779 | + if (!$filters['prop-filters'] && !$filters['comp-filters'][0]['comp-filters'] && !$filters['comp-filters'][0]['prop-filters'] && (!$timeRange['start'] || !$timeRange['end'])) { |
|
| 1780 | + $requirePostFilter = false; |
|
| 1781 | + } |
|
| 1782 | + } |
|
| 1783 | + } |
|
| 1784 | + $query = $this->db->getQueryBuilder(); |
|
| 1785 | + $query->select(['id', 'uri', 'uid', 'lastmodified', 'etag', 'calendarid', 'size', 'calendardata', 'componenttype', 'classification', 'deleted_at']) |
|
| 1786 | + ->from('calendarobjects') |
|
| 1787 | + ->where($query->expr()->eq('calendarid', $query->createNamedParameter($calendarId))) |
|
| 1788 | + ->andWhere($query->expr()->eq('calendartype', $query->createNamedParameter($calendarType))) |
|
| 1789 | + ->andWhere($query->expr()->isNull('deleted_at')); |
|
| 1790 | + |
|
| 1791 | + if ($componentType) { |
|
| 1792 | + $query->andWhere($query->expr()->eq('componenttype', $query->createNamedParameter($componentType))); |
|
| 1793 | + } |
|
| 1794 | + |
|
| 1795 | + if ($timeRange && $timeRange['start']) { |
|
| 1796 | + $query->andWhere($query->expr()->gt('lastoccurence', $query->createNamedParameter($timeRange['start']->getTimeStamp()))); |
|
| 1797 | + } |
|
| 1798 | + if ($timeRange && $timeRange['end']) { |
|
| 1799 | + $query->andWhere($query->expr()->lt('firstoccurence', $query->createNamedParameter($timeRange['end']->getTimeStamp()))); |
|
| 1800 | + } |
|
| 1801 | + |
|
| 1802 | + $stmt = $query->executeQuery(); |
|
| 1803 | + |
|
| 1804 | + $result = []; |
|
| 1805 | + while ($row = $stmt->fetch()) { |
|
| 1806 | + // if we leave it as a blob we can't read it both from the post filter and the rowToCalendarObject |
|
| 1807 | + if (isset($row['calendardata'])) { |
|
| 1808 | + $row['calendardata'] = $this->readBlob($row['calendardata']); |
|
| 1809 | + } |
|
| 1810 | + |
|
| 1811 | + if ($requirePostFilter) { |
|
| 1812 | + // validateFilterForObject will parse the calendar data |
|
| 1813 | + // catch parsing errors |
|
| 1814 | + try { |
|
| 1815 | + $matches = $this->validateFilterForObject($row, $filters); |
|
| 1816 | + } catch (ParseException $ex) { |
|
| 1817 | + $this->logger->error('Caught parsing exception for calendar data. This usually indicates invalid calendar data. calendar-id:' . $calendarId . ' uri:' . $row['uri'], [ |
|
| 1818 | + 'app' => 'dav', |
|
| 1819 | + 'exception' => $ex, |
|
| 1820 | + ]); |
|
| 1821 | + continue; |
|
| 1822 | + } catch (InvalidDataException $ex) { |
|
| 1823 | + $this->logger->error('Caught invalid data exception for calendar data. This usually indicates invalid calendar data. calendar-id:' . $calendarId . ' uri:' . $row['uri'], [ |
|
| 1824 | + 'app' => 'dav', |
|
| 1825 | + 'exception' => $ex, |
|
| 1826 | + ]); |
|
| 1827 | + continue; |
|
| 1828 | + } catch (MaxInstancesExceededException $ex) { |
|
| 1829 | + $this->logger->warning('Caught max instances exceeded exception for calendar data. This usually indicates too much recurring (more than 3500) event in calendar data. Object uri: ' . $row['uri'], [ |
|
| 1830 | + 'app' => 'dav', |
|
| 1831 | + 'exception' => $ex, |
|
| 1832 | + ]); |
|
| 1833 | + continue; |
|
| 1834 | + } |
|
| 1835 | + |
|
| 1836 | + if (!$matches) { |
|
| 1837 | + continue; |
|
| 1838 | + } |
|
| 1839 | + } |
|
| 1840 | + $result[] = $row['uri']; |
|
| 1841 | + $key = $calendarId . '::' . $row['uri'] . '::' . $calendarType; |
|
| 1842 | + $this->cachedObjects[$key] = $this->rowToCalendarObject($row); |
|
| 1843 | + } |
|
| 1844 | + |
|
| 1845 | + return $result; |
|
| 1846 | + } |
|
| 1847 | + |
|
| 1848 | + /** |
|
| 1849 | + * custom Nextcloud search extension for CalDAV |
|
| 1850 | + * |
|
| 1851 | + * TODO - this should optionally cover cached calendar objects as well |
|
| 1852 | + * |
|
| 1853 | + * @param string $principalUri |
|
| 1854 | + * @param array $filters |
|
| 1855 | + * @param integer|null $limit |
|
| 1856 | + * @param integer|null $offset |
|
| 1857 | + * @return array |
|
| 1858 | + */ |
|
| 1859 | + public function calendarSearch($principalUri, array $filters, $limit = null, $offset = null) { |
|
| 1860 | + return $this->atomic(function () use ($principalUri, $filters, $limit, $offset) { |
|
| 1861 | + $calendars = $this->getCalendarsForUser($principalUri); |
|
| 1862 | + $ownCalendars = []; |
|
| 1863 | + $sharedCalendars = []; |
|
| 1864 | + |
|
| 1865 | + $uriMapper = []; |
|
| 1866 | + |
|
| 1867 | + foreach ($calendars as $calendar) { |
|
| 1868 | + if ($calendar['{http://owncloud.org/ns}owner-principal'] === $principalUri) { |
|
| 1869 | + $ownCalendars[] = $calendar['id']; |
|
| 1870 | + } else { |
|
| 1871 | + $sharedCalendars[] = $calendar['id']; |
|
| 1872 | + } |
|
| 1873 | + $uriMapper[$calendar['id']] = $calendar['uri']; |
|
| 1874 | + } |
|
| 1875 | + if (count($ownCalendars) === 0 && count($sharedCalendars) === 0) { |
|
| 1876 | + return []; |
|
| 1877 | + } |
|
| 1878 | + |
|
| 1879 | + $query = $this->db->getQueryBuilder(); |
|
| 1880 | + // Calendar id expressions |
|
| 1881 | + $calendarExpressions = []; |
|
| 1882 | + foreach ($ownCalendars as $id) { |
|
| 1883 | + $calendarExpressions[] = $query->expr()->andX( |
|
| 1884 | + $query->expr()->eq('c.calendarid', |
|
| 1885 | + $query->createNamedParameter($id)), |
|
| 1886 | + $query->expr()->eq('c.calendartype', |
|
| 1887 | + $query->createNamedParameter(self::CALENDAR_TYPE_CALENDAR))); |
|
| 1888 | + } |
|
| 1889 | + foreach ($sharedCalendars as $id) { |
|
| 1890 | + $calendarExpressions[] = $query->expr()->andX( |
|
| 1891 | + $query->expr()->eq('c.calendarid', |
|
| 1892 | + $query->createNamedParameter($id)), |
|
| 1893 | + $query->expr()->eq('c.classification', |
|
| 1894 | + $query->createNamedParameter(self::CLASSIFICATION_PUBLIC)), |
|
| 1895 | + $query->expr()->eq('c.calendartype', |
|
| 1896 | + $query->createNamedParameter(self::CALENDAR_TYPE_CALENDAR))); |
|
| 1897 | + } |
|
| 1898 | + |
|
| 1899 | + if (count($calendarExpressions) === 1) { |
|
| 1900 | + $calExpr = $calendarExpressions[0]; |
|
| 1901 | + } else { |
|
| 1902 | + $calExpr = call_user_func_array([$query->expr(), 'orX'], $calendarExpressions); |
|
| 1903 | + } |
|
| 1904 | + |
|
| 1905 | + // Component expressions |
|
| 1906 | + $compExpressions = []; |
|
| 1907 | + foreach ($filters['comps'] as $comp) { |
|
| 1908 | + $compExpressions[] = $query->expr() |
|
| 1909 | + ->eq('c.componenttype', $query->createNamedParameter($comp)); |
|
| 1910 | + } |
|
| 1911 | + |
|
| 1912 | + if (count($compExpressions) === 1) { |
|
| 1913 | + $compExpr = $compExpressions[0]; |
|
| 1914 | + } else { |
|
| 1915 | + $compExpr = call_user_func_array([$query->expr(), 'orX'], $compExpressions); |
|
| 1916 | + } |
|
| 1917 | + |
|
| 1918 | + if (!isset($filters['props'])) { |
|
| 1919 | + $filters['props'] = []; |
|
| 1920 | + } |
|
| 1921 | + if (!isset($filters['params'])) { |
|
| 1922 | + $filters['params'] = []; |
|
| 1923 | + } |
|
| 1924 | + |
|
| 1925 | + $propParamExpressions = []; |
|
| 1926 | + foreach ($filters['props'] as $prop) { |
|
| 1927 | + $propParamExpressions[] = $query->expr()->andX( |
|
| 1928 | + $query->expr()->eq('i.name', $query->createNamedParameter($prop)), |
|
| 1929 | + $query->expr()->isNull('i.parameter') |
|
| 1930 | + ); |
|
| 1931 | + } |
|
| 1932 | + foreach ($filters['params'] as $param) { |
|
| 1933 | + $propParamExpressions[] = $query->expr()->andX( |
|
| 1934 | + $query->expr()->eq('i.name', $query->createNamedParameter($param['property'])), |
|
| 1935 | + $query->expr()->eq('i.parameter', $query->createNamedParameter($param['parameter'])) |
|
| 1936 | + ); |
|
| 1937 | + } |
|
| 1938 | + |
|
| 1939 | + if (count($propParamExpressions) === 1) { |
|
| 1940 | + $propParamExpr = $propParamExpressions[0]; |
|
| 1941 | + } else { |
|
| 1942 | + $propParamExpr = call_user_func_array([$query->expr(), 'orX'], $propParamExpressions); |
|
| 1943 | + } |
|
| 1944 | + |
|
| 1945 | + $query->select(['c.calendarid', 'c.uri']) |
|
| 1946 | + ->from($this->dbObjectPropertiesTable, 'i') |
|
| 1947 | + ->join('i', 'calendarobjects', 'c', $query->expr()->eq('i.objectid', 'c.id')) |
|
| 1948 | + ->where($calExpr) |
|
| 1949 | + ->andWhere($compExpr) |
|
| 1950 | + ->andWhere($propParamExpr) |
|
| 1951 | + ->andWhere($query->expr()->iLike('i.value', |
|
| 1952 | + $query->createNamedParameter('%' . $this->db->escapeLikeParameter($filters['search-term']) . '%'))) |
|
| 1953 | + ->andWhere($query->expr()->isNull('deleted_at')); |
|
| 1954 | + |
|
| 1955 | + if ($offset) { |
|
| 1956 | + $query->setFirstResult($offset); |
|
| 1957 | + } |
|
| 1958 | + if ($limit) { |
|
| 1959 | + $query->setMaxResults($limit); |
|
| 1960 | + } |
|
| 1961 | + |
|
| 1962 | + $stmt = $query->executeQuery(); |
|
| 1963 | + |
|
| 1964 | + $result = []; |
|
| 1965 | + while ($row = $stmt->fetch()) { |
|
| 1966 | + $path = $uriMapper[$row['calendarid']] . '/' . $row['uri']; |
|
| 1967 | + if (!in_array($path, $result)) { |
|
| 1968 | + $result[] = $path; |
|
| 1969 | + } |
|
| 1970 | + } |
|
| 1971 | + |
|
| 1972 | + return $result; |
|
| 1973 | + }, $this->db); |
|
| 1974 | + } |
|
| 1975 | + |
|
| 1976 | + /** |
|
| 1977 | + * used for Nextcloud's calendar API |
|
| 1978 | + * |
|
| 1979 | + * @param array $calendarInfo |
|
| 1980 | + * @param string $pattern |
|
| 1981 | + * @param array $searchProperties |
|
| 1982 | + * @param array $options |
|
| 1983 | + * @param integer|null $limit |
|
| 1984 | + * @param integer|null $offset |
|
| 1985 | + * |
|
| 1986 | + * @return array |
|
| 1987 | + */ |
|
| 1988 | + public function search( |
|
| 1989 | + array $calendarInfo, |
|
| 1990 | + $pattern, |
|
| 1991 | + array $searchProperties, |
|
| 1992 | + array $options, |
|
| 1993 | + $limit, |
|
| 1994 | + $offset, |
|
| 1995 | + ) { |
|
| 1996 | + $outerQuery = $this->db->getQueryBuilder(); |
|
| 1997 | + $innerQuery = $this->db->getQueryBuilder(); |
|
| 1998 | + |
|
| 1999 | + if (isset($calendarInfo['source'])) { |
|
| 2000 | + $calendarType = self::CALENDAR_TYPE_SUBSCRIPTION; |
|
| 2001 | + } else { |
|
| 2002 | + $calendarType = self::CALENDAR_TYPE_CALENDAR; |
|
| 2003 | + } |
|
| 2004 | + |
|
| 2005 | + $innerQuery->selectDistinct('op.objectid') |
|
| 2006 | + ->from($this->dbObjectPropertiesTable, 'op') |
|
| 2007 | + ->andWhere($innerQuery->expr()->eq('op.calendarid', |
|
| 2008 | + $outerQuery->createNamedParameter($calendarInfo['id']))) |
|
| 2009 | + ->andWhere($innerQuery->expr()->eq('op.calendartype', |
|
| 2010 | + $outerQuery->createNamedParameter($calendarType))); |
|
| 2011 | + |
|
| 2012 | + $outerQuery->select('c.id', 'c.calendardata', 'c.componenttype', 'c.uid', 'c.uri') |
|
| 2013 | + ->from('calendarobjects', 'c') |
|
| 2014 | + ->where($outerQuery->expr()->isNull('deleted_at')); |
|
| 2015 | + |
|
| 2016 | + // only return public items for shared calendars for now |
|
| 2017 | + if (isset($calendarInfo['{http://owncloud.org/ns}owner-principal']) === false || $calendarInfo['principaluri'] !== $calendarInfo['{http://owncloud.org/ns}owner-principal']) { |
|
| 2018 | + $outerQuery->andWhere($outerQuery->expr()->eq('c.classification', |
|
| 2019 | + $outerQuery->createNamedParameter(self::CLASSIFICATION_PUBLIC))); |
|
| 2020 | + } |
|
| 2021 | + |
|
| 2022 | + if (!empty($searchProperties)) { |
|
| 2023 | + $or = []; |
|
| 2024 | + foreach ($searchProperties as $searchProperty) { |
|
| 2025 | + $or[] = $innerQuery->expr()->eq('op.name', |
|
| 2026 | + $outerQuery->createNamedParameter($searchProperty)); |
|
| 2027 | + } |
|
| 2028 | + $innerQuery->andWhere($innerQuery->expr()->orX(...$or)); |
|
| 2029 | + } |
|
| 2030 | + |
|
| 2031 | + if ($pattern !== '') { |
|
| 2032 | + $innerQuery->andWhere($innerQuery->expr()->iLike('op.value', |
|
| 2033 | + $outerQuery->createNamedParameter('%' . |
|
| 2034 | + $this->db->escapeLikeParameter($pattern) . '%'))); |
|
| 2035 | + } |
|
| 2036 | + |
|
| 2037 | + $start = null; |
|
| 2038 | + $end = null; |
|
| 2039 | + |
|
| 2040 | + $hasLimit = is_int($limit); |
|
| 2041 | + $hasTimeRange = false; |
|
| 2042 | + |
|
| 2043 | + if (isset($options['timerange']['start']) && $options['timerange']['start'] instanceof DateTimeInterface) { |
|
| 2044 | + /** @var DateTimeInterface $start */ |
|
| 2045 | + $start = $options['timerange']['start']; |
|
| 2046 | + $outerQuery->andWhere( |
|
| 2047 | + $outerQuery->expr()->gt( |
|
| 2048 | + 'lastoccurence', |
|
| 2049 | + $outerQuery->createNamedParameter($start->getTimestamp()) |
|
| 2050 | + ) |
|
| 2051 | + ); |
|
| 2052 | + $hasTimeRange = true; |
|
| 2053 | + } |
|
| 2054 | + |
|
| 2055 | + if (isset($options['timerange']['end']) && $options['timerange']['end'] instanceof DateTimeInterface) { |
|
| 2056 | + /** @var DateTimeInterface $end */ |
|
| 2057 | + $end = $options['timerange']['end']; |
|
| 2058 | + $outerQuery->andWhere( |
|
| 2059 | + $outerQuery->expr()->lt( |
|
| 2060 | + 'firstoccurence', |
|
| 2061 | + $outerQuery->createNamedParameter($end->getTimestamp()) |
|
| 2062 | + ) |
|
| 2063 | + ); |
|
| 2064 | + $hasTimeRange = true; |
|
| 2065 | + } |
|
| 2066 | + |
|
| 2067 | + if (isset($options['uid'])) { |
|
| 2068 | + $outerQuery->andWhere($outerQuery->expr()->eq('uid', $outerQuery->createNamedParameter($options['uid']))); |
|
| 2069 | + } |
|
| 2070 | + |
|
| 2071 | + if (!empty($options['types'])) { |
|
| 2072 | + $or = []; |
|
| 2073 | + foreach ($options['types'] as $type) { |
|
| 2074 | + $or[] = $outerQuery->expr()->eq('componenttype', |
|
| 2075 | + $outerQuery->createNamedParameter($type)); |
|
| 2076 | + } |
|
| 2077 | + $outerQuery->andWhere($outerQuery->expr()->orX(...$or)); |
|
| 2078 | + } |
|
| 2079 | + |
|
| 2080 | + $outerQuery->andWhere($outerQuery->expr()->in('c.id', $outerQuery->createFunction($innerQuery->getSQL()))); |
|
| 2081 | + |
|
| 2082 | + // Without explicit order by its undefined in which order the SQL server returns the events. |
|
| 2083 | + // For the pagination with hasLimit and hasTimeRange, a stable ordering is helpful. |
|
| 2084 | + $outerQuery->addOrderBy('id'); |
|
| 2085 | + |
|
| 2086 | + $offset = (int)$offset; |
|
| 2087 | + $outerQuery->setFirstResult($offset); |
|
| 2088 | + |
|
| 2089 | + $calendarObjects = []; |
|
| 2090 | + |
|
| 2091 | + if ($hasLimit && $hasTimeRange) { |
|
| 2092 | + /** |
|
| 2093 | + * Event recurrences are evaluated at runtime because the database only knows the first and last occurrence. |
|
| 2094 | + * |
|
| 2095 | + * Given, a user created 8 events with a yearly reoccurrence and two for events tomorrow. |
|
| 2096 | + * The upcoming event widget asks the CalDAV backend for 7 events within the next 14 days. |
|
| 2097 | + * |
|
| 2098 | + * If limit 7 is applied to the SQL query, we find the 7 events with a yearly reoccurrence |
|
| 2099 | + * and discard the events after evaluating the reoccurrence rules because they are not due within |
|
| 2100 | + * the next 14 days and end up with an empty result even if there are two events to show. |
|
| 2101 | + * |
|
| 2102 | + * The workaround for search requests with a limit and time range is asking for more row than requested |
|
| 2103 | + * and retrying if we have not reached the limit. |
|
| 2104 | + * |
|
| 2105 | + * 25 rows and 3 retries is entirely arbitrary. |
|
| 2106 | + */ |
|
| 2107 | + $maxResults = (int)max($limit, 25); |
|
| 2108 | + $outerQuery->setMaxResults($maxResults); |
|
| 2109 | + |
|
| 2110 | + for ($attempt = $objectsCount = 0; $attempt < 3 && $objectsCount < $limit; $attempt++) { |
|
| 2111 | + $objectsCount = array_push($calendarObjects, ...$this->searchCalendarObjects($outerQuery, $start, $end)); |
|
| 2112 | + $outerQuery->setFirstResult($offset += $maxResults); |
|
| 2113 | + } |
|
| 2114 | + |
|
| 2115 | + $calendarObjects = array_slice($calendarObjects, 0, $limit, false); |
|
| 2116 | + } else { |
|
| 2117 | + $outerQuery->setMaxResults($limit); |
|
| 2118 | + $calendarObjects = $this->searchCalendarObjects($outerQuery, $start, $end); |
|
| 2119 | + } |
|
| 2120 | + |
|
| 2121 | + $calendarObjects = array_map(function ($o) use ($options) { |
|
| 2122 | + $calendarData = Reader::read($o['calendardata']); |
|
| 2123 | + |
|
| 2124 | + // Expand recurrences if an explicit time range is requested |
|
| 2125 | + if ($calendarData instanceof VCalendar |
|
| 2126 | + && isset($options['timerange']['start'], $options['timerange']['end'])) { |
|
| 2127 | + $calendarData = $calendarData->expand( |
|
| 2128 | + $options['timerange']['start'], |
|
| 2129 | + $options['timerange']['end'], |
|
| 2130 | + ); |
|
| 2131 | + } |
|
| 2132 | + |
|
| 2133 | + $comps = $calendarData->getComponents(); |
|
| 2134 | + $objects = []; |
|
| 2135 | + $timezones = []; |
|
| 2136 | + foreach ($comps as $comp) { |
|
| 2137 | + if ($comp instanceof VTimeZone) { |
|
| 2138 | + $timezones[] = $comp; |
|
| 2139 | + } else { |
|
| 2140 | + $objects[] = $comp; |
|
| 2141 | + } |
|
| 2142 | + } |
|
| 2143 | + |
|
| 2144 | + return [ |
|
| 2145 | + 'id' => $o['id'], |
|
| 2146 | + 'type' => $o['componenttype'], |
|
| 2147 | + 'uid' => $o['uid'], |
|
| 2148 | + 'uri' => $o['uri'], |
|
| 2149 | + 'objects' => array_map(function ($c) { |
|
| 2150 | + return $this->transformSearchData($c); |
|
| 2151 | + }, $objects), |
|
| 2152 | + 'timezones' => array_map(function ($c) { |
|
| 2153 | + return $this->transformSearchData($c); |
|
| 2154 | + }, $timezones), |
|
| 2155 | + ]; |
|
| 2156 | + }, $calendarObjects); |
|
| 2157 | + |
|
| 2158 | + usort($calendarObjects, function (array $a, array $b) { |
|
| 2159 | + /** @var DateTimeImmutable $startA */ |
|
| 2160 | + $startA = $a['objects'][0]['DTSTART'][0] ?? new DateTimeImmutable(self::MAX_DATE); |
|
| 2161 | + /** @var DateTimeImmutable $startB */ |
|
| 2162 | + $startB = $b['objects'][0]['DTSTART'][0] ?? new DateTimeImmutable(self::MAX_DATE); |
|
| 2163 | + |
|
| 2164 | + return $startA->getTimestamp() <=> $startB->getTimestamp(); |
|
| 2165 | + }); |
|
| 2166 | + |
|
| 2167 | + return $calendarObjects; |
|
| 2168 | + } |
|
| 2169 | + |
|
| 2170 | + private function searchCalendarObjects(IQueryBuilder $query, ?DateTimeInterface $start, ?DateTimeInterface $end): array { |
|
| 2171 | + $calendarObjects = []; |
|
| 2172 | + $filterByTimeRange = ($start instanceof DateTimeInterface) || ($end instanceof DateTimeInterface); |
|
| 2173 | + |
|
| 2174 | + $result = $query->executeQuery(); |
|
| 2175 | + |
|
| 2176 | + while (($row = $result->fetch()) !== false) { |
|
| 2177 | + if ($filterByTimeRange === false) { |
|
| 2178 | + // No filter required |
|
| 2179 | + $calendarObjects[] = $row; |
|
| 2180 | + continue; |
|
| 2181 | + } |
|
| 2182 | + |
|
| 2183 | + try { |
|
| 2184 | + $isValid = $this->validateFilterForObject($row, [ |
|
| 2185 | + 'name' => 'VCALENDAR', |
|
| 2186 | + 'comp-filters' => [ |
|
| 2187 | + [ |
|
| 2188 | + 'name' => 'VEVENT', |
|
| 2189 | + 'comp-filters' => [], |
|
| 2190 | + 'prop-filters' => [], |
|
| 2191 | + 'is-not-defined' => false, |
|
| 2192 | + 'time-range' => [ |
|
| 2193 | + 'start' => $start, |
|
| 2194 | + 'end' => $end, |
|
| 2195 | + ], |
|
| 2196 | + ], |
|
| 2197 | + ], |
|
| 2198 | + 'prop-filters' => [], |
|
| 2199 | + 'is-not-defined' => false, |
|
| 2200 | + 'time-range' => null, |
|
| 2201 | + ]); |
|
| 2202 | + } catch (MaxInstancesExceededException $ex) { |
|
| 2203 | + $this->logger->warning('Caught max instances exceeded exception for calendar data. This usually indicates too much recurring (more than 3500) event in calendar data. Object uri: ' . $row['uri'], [ |
|
| 2204 | + 'app' => 'dav', |
|
| 2205 | + 'exception' => $ex, |
|
| 2206 | + ]); |
|
| 2207 | + continue; |
|
| 2208 | + } |
|
| 2209 | + |
|
| 2210 | + if (is_resource($row['calendardata'])) { |
|
| 2211 | + // Put the stream back to the beginning so it can be read another time |
|
| 2212 | + rewind($row['calendardata']); |
|
| 2213 | + } |
|
| 2214 | + |
|
| 2215 | + if ($isValid) { |
|
| 2216 | + $calendarObjects[] = $row; |
|
| 2217 | + } |
|
| 2218 | + } |
|
| 2219 | + |
|
| 2220 | + $result->closeCursor(); |
|
| 2221 | + |
|
| 2222 | + return $calendarObjects; |
|
| 2223 | + } |
|
| 2224 | + |
|
| 2225 | + /** |
|
| 2226 | + * @param Component $comp |
|
| 2227 | + * @return array |
|
| 2228 | + */ |
|
| 2229 | + private function transformSearchData(Component $comp) { |
|
| 2230 | + $data = []; |
|
| 2231 | + /** @var Component[] $subComponents */ |
|
| 2232 | + $subComponents = $comp->getComponents(); |
|
| 2233 | + /** @var Property[] $properties */ |
|
| 2234 | + $properties = array_filter($comp->children(), function ($c) { |
|
| 2235 | + return $c instanceof Property; |
|
| 2236 | + }); |
|
| 2237 | + $validationRules = $comp->getValidationRules(); |
|
| 2238 | + |
|
| 2239 | + foreach ($subComponents as $subComponent) { |
|
| 2240 | + $name = $subComponent->name; |
|
| 2241 | + if (!isset($data[$name])) { |
|
| 2242 | + $data[$name] = []; |
|
| 2243 | + } |
|
| 2244 | + $data[$name][] = $this->transformSearchData($subComponent); |
|
| 2245 | + } |
|
| 2246 | + |
|
| 2247 | + foreach ($properties as $property) { |
|
| 2248 | + $name = $property->name; |
|
| 2249 | + if (!isset($validationRules[$name])) { |
|
| 2250 | + $validationRules[$name] = '*'; |
|
| 2251 | + } |
|
| 2252 | + |
|
| 2253 | + $rule = $validationRules[$property->name]; |
|
| 2254 | + if ($rule === '+' || $rule === '*') { // multiple |
|
| 2255 | + if (!isset($data[$name])) { |
|
| 2256 | + $data[$name] = []; |
|
| 2257 | + } |
|
| 2258 | + |
|
| 2259 | + $data[$name][] = $this->transformSearchProperty($property); |
|
| 2260 | + } else { // once |
|
| 2261 | + $data[$name] = $this->transformSearchProperty($property); |
|
| 2262 | + } |
|
| 2263 | + } |
|
| 2264 | + |
|
| 2265 | + return $data; |
|
| 2266 | + } |
|
| 2267 | + |
|
| 2268 | + /** |
|
| 2269 | + * @param Property $prop |
|
| 2270 | + * @return array |
|
| 2271 | + */ |
|
| 2272 | + private function transformSearchProperty(Property $prop) { |
|
| 2273 | + // No need to check Date, as it extends DateTime |
|
| 2274 | + if ($prop instanceof Property\ICalendar\DateTime) { |
|
| 2275 | + $value = $prop->getDateTime(); |
|
| 2276 | + } else { |
|
| 2277 | + $value = $prop->getValue(); |
|
| 2278 | + } |
|
| 2279 | + |
|
| 2280 | + return [ |
|
| 2281 | + $value, |
|
| 2282 | + $prop->parameters() |
|
| 2283 | + ]; |
|
| 2284 | + } |
|
| 2285 | + |
|
| 2286 | + /** |
|
| 2287 | + * @param string $principalUri |
|
| 2288 | + * @param string $pattern |
|
| 2289 | + * @param array $componentTypes |
|
| 2290 | + * @param array $searchProperties |
|
| 2291 | + * @param array $searchParameters |
|
| 2292 | + * @param array $options |
|
| 2293 | + * @return array |
|
| 2294 | + */ |
|
| 2295 | + public function searchPrincipalUri(string $principalUri, |
|
| 2296 | + string $pattern, |
|
| 2297 | + array $componentTypes, |
|
| 2298 | + array $searchProperties, |
|
| 2299 | + array $searchParameters, |
|
| 2300 | + array $options = [], |
|
| 2301 | + ): array { |
|
| 2302 | + return $this->atomic(function () use ($principalUri, $pattern, $componentTypes, $searchProperties, $searchParameters, $options) { |
|
| 2303 | + $escapePattern = !\array_key_exists('escape_like_param', $options) || $options['escape_like_param'] !== false; |
|
| 2304 | + |
|
| 2305 | + $calendarObjectIdQuery = $this->db->getQueryBuilder(); |
|
| 2306 | + $calendarOr = []; |
|
| 2307 | + $searchOr = []; |
|
| 2308 | + |
|
| 2309 | + // Fetch calendars and subscription |
|
| 2310 | + $calendars = $this->getCalendarsForUser($principalUri); |
|
| 2311 | + $subscriptions = $this->getSubscriptionsForUser($principalUri); |
|
| 2312 | + foreach ($calendars as $calendar) { |
|
| 2313 | + $calendarAnd = $calendarObjectIdQuery->expr()->andX( |
|
| 2314 | + $calendarObjectIdQuery->expr()->eq('cob.calendarid', $calendarObjectIdQuery->createNamedParameter((int)$calendar['id'])), |
|
| 2315 | + $calendarObjectIdQuery->expr()->eq('cob.calendartype', $calendarObjectIdQuery->createNamedParameter(self::CALENDAR_TYPE_CALENDAR)), |
|
| 2316 | + ); |
|
| 2317 | + |
|
| 2318 | + // If it's shared, limit search to public events |
|
| 2319 | + if (isset($calendar['{http://owncloud.org/ns}owner-principal']) |
|
| 2320 | + && $calendar['principaluri'] !== $calendar['{http://owncloud.org/ns}owner-principal']) { |
|
| 2321 | + $calendarAnd->add($calendarObjectIdQuery->expr()->eq('co.classification', $calendarObjectIdQuery->createNamedParameter(self::CLASSIFICATION_PUBLIC))); |
|
| 2322 | + } |
|
| 2323 | + |
|
| 2324 | + $calendarOr[] = $calendarAnd; |
|
| 2325 | + } |
|
| 2326 | + foreach ($subscriptions as $subscription) { |
|
| 2327 | + $subscriptionAnd = $calendarObjectIdQuery->expr()->andX( |
|
| 2328 | + $calendarObjectIdQuery->expr()->eq('cob.calendarid', $calendarObjectIdQuery->createNamedParameter((int)$subscription['id'])), |
|
| 2329 | + $calendarObjectIdQuery->expr()->eq('cob.calendartype', $calendarObjectIdQuery->createNamedParameter(self::CALENDAR_TYPE_SUBSCRIPTION)), |
|
| 2330 | + ); |
|
| 2331 | + |
|
| 2332 | + // If it's shared, limit search to public events |
|
| 2333 | + if (isset($subscription['{http://owncloud.org/ns}owner-principal']) |
|
| 2334 | + && $subscription['principaluri'] !== $subscription['{http://owncloud.org/ns}owner-principal']) { |
|
| 2335 | + $subscriptionAnd->add($calendarObjectIdQuery->expr()->eq('co.classification', $calendarObjectIdQuery->createNamedParameter(self::CLASSIFICATION_PUBLIC))); |
|
| 2336 | + } |
|
| 2337 | + |
|
| 2338 | + $calendarOr[] = $subscriptionAnd; |
|
| 2339 | + } |
|
| 2340 | + |
|
| 2341 | + foreach ($searchProperties as $property) { |
|
| 2342 | + $propertyAnd = $calendarObjectIdQuery->expr()->andX( |
|
| 2343 | + $calendarObjectIdQuery->expr()->eq('cob.name', $calendarObjectIdQuery->createNamedParameter($property, IQueryBuilder::PARAM_STR)), |
|
| 2344 | + $calendarObjectIdQuery->expr()->isNull('cob.parameter'), |
|
| 2345 | + ); |
|
| 2346 | + |
|
| 2347 | + $searchOr[] = $propertyAnd; |
|
| 2348 | + } |
|
| 2349 | + foreach ($searchParameters as $property => $parameter) { |
|
| 2350 | + $parameterAnd = $calendarObjectIdQuery->expr()->andX( |
|
| 2351 | + $calendarObjectIdQuery->expr()->eq('cob.name', $calendarObjectIdQuery->createNamedParameter($property, IQueryBuilder::PARAM_STR)), |
|
| 2352 | + $calendarObjectIdQuery->expr()->eq('cob.parameter', $calendarObjectIdQuery->createNamedParameter($parameter, IQueryBuilder::PARAM_STR_ARRAY)), |
|
| 2353 | + ); |
|
| 2354 | + |
|
| 2355 | + $searchOr[] = $parameterAnd; |
|
| 2356 | + } |
|
| 2357 | + |
|
| 2358 | + if (empty($calendarOr)) { |
|
| 2359 | + return []; |
|
| 2360 | + } |
|
| 2361 | + if (empty($searchOr)) { |
|
| 2362 | + return []; |
|
| 2363 | + } |
|
| 2364 | + |
|
| 2365 | + $calendarObjectIdQuery->selectDistinct('cob.objectid') |
|
| 2366 | + ->from($this->dbObjectPropertiesTable, 'cob') |
|
| 2367 | + ->leftJoin('cob', 'calendarobjects', 'co', $calendarObjectIdQuery->expr()->eq('co.id', 'cob.objectid')) |
|
| 2368 | + ->andWhere($calendarObjectIdQuery->expr()->in('co.componenttype', $calendarObjectIdQuery->createNamedParameter($componentTypes, IQueryBuilder::PARAM_STR_ARRAY))) |
|
| 2369 | + ->andWhere($calendarObjectIdQuery->expr()->orX(...$calendarOr)) |
|
| 2370 | + ->andWhere($calendarObjectIdQuery->expr()->orX(...$searchOr)) |
|
| 2371 | + ->andWhere($calendarObjectIdQuery->expr()->isNull('deleted_at')); |
|
| 2372 | + |
|
| 2373 | + if ($pattern !== '') { |
|
| 2374 | + if (!$escapePattern) { |
|
| 2375 | + $calendarObjectIdQuery->andWhere($calendarObjectIdQuery->expr()->ilike('cob.value', $calendarObjectIdQuery->createNamedParameter($pattern))); |
|
| 2376 | + } else { |
|
| 2377 | + $calendarObjectIdQuery->andWhere($calendarObjectIdQuery->expr()->ilike('cob.value', $calendarObjectIdQuery->createNamedParameter('%' . $this->db->escapeLikeParameter($pattern) . '%'))); |
|
| 2378 | + } |
|
| 2379 | + } |
|
| 2380 | + |
|
| 2381 | + if (isset($options['limit'])) { |
|
| 2382 | + $calendarObjectIdQuery->setMaxResults($options['limit']); |
|
| 2383 | + } |
|
| 2384 | + if (isset($options['offset'])) { |
|
| 2385 | + $calendarObjectIdQuery->setFirstResult($options['offset']); |
|
| 2386 | + } |
|
| 2387 | + if (isset($options['timerange'])) { |
|
| 2388 | + if (isset($options['timerange']['start']) && $options['timerange']['start'] instanceof DateTimeInterface) { |
|
| 2389 | + $calendarObjectIdQuery->andWhere($calendarObjectIdQuery->expr()->gt( |
|
| 2390 | + 'lastoccurence', |
|
| 2391 | + $calendarObjectIdQuery->createNamedParameter($options['timerange']['start']->getTimeStamp()), |
|
| 2392 | + )); |
|
| 2393 | + } |
|
| 2394 | + if (isset($options['timerange']['end']) && $options['timerange']['end'] instanceof DateTimeInterface) { |
|
| 2395 | + $calendarObjectIdQuery->andWhere($calendarObjectIdQuery->expr()->lt( |
|
| 2396 | + 'firstoccurence', |
|
| 2397 | + $calendarObjectIdQuery->createNamedParameter($options['timerange']['end']->getTimeStamp()), |
|
| 2398 | + )); |
|
| 2399 | + } |
|
| 2400 | + } |
|
| 2401 | + |
|
| 2402 | + $result = $calendarObjectIdQuery->executeQuery(); |
|
| 2403 | + $matches = []; |
|
| 2404 | + while (($row = $result->fetch()) !== false) { |
|
| 2405 | + $matches[] = (int)$row['objectid']; |
|
| 2406 | + } |
|
| 2407 | + $result->closeCursor(); |
|
| 2408 | + |
|
| 2409 | + $query = $this->db->getQueryBuilder(); |
|
| 2410 | + $query->select('calendardata', 'uri', 'calendarid', 'calendartype') |
|
| 2411 | + ->from('calendarobjects') |
|
| 2412 | + ->where($query->expr()->in('id', $query->createNamedParameter($matches, IQueryBuilder::PARAM_INT_ARRAY))); |
|
| 2413 | + |
|
| 2414 | + $result = $query->executeQuery(); |
|
| 2415 | + $calendarObjects = []; |
|
| 2416 | + while (($array = $result->fetch()) !== false) { |
|
| 2417 | + $array['calendarid'] = (int)$array['calendarid']; |
|
| 2418 | + $array['calendartype'] = (int)$array['calendartype']; |
|
| 2419 | + $array['calendardata'] = $this->readBlob($array['calendardata']); |
|
| 2420 | + |
|
| 2421 | + $calendarObjects[] = $array; |
|
| 2422 | + } |
|
| 2423 | + $result->closeCursor(); |
|
| 2424 | + return $calendarObjects; |
|
| 2425 | + }, $this->db); |
|
| 2426 | + } |
|
| 2427 | + |
|
| 2428 | + /** |
|
| 2429 | + * Searches through all of a users calendars and calendar objects to find |
|
| 2430 | + * an object with a specific UID. |
|
| 2431 | + * |
|
| 2432 | + * This method should return the path to this object, relative to the |
|
| 2433 | + * calendar home, so this path usually only contains two parts: |
|
| 2434 | + * |
|
| 2435 | + * calendarpath/objectpath.ics |
|
| 2436 | + * |
|
| 2437 | + * If the uid is not found, return null. |
|
| 2438 | + * |
|
| 2439 | + * This method should only consider * objects that the principal owns, so |
|
| 2440 | + * any calendars owned by other principals that also appear in this |
|
| 2441 | + * collection should be ignored. |
|
| 2442 | + * |
|
| 2443 | + * @param string $principalUri |
|
| 2444 | + * @param string $uid |
|
| 2445 | + * @return string|null |
|
| 2446 | + */ |
|
| 2447 | + public function getCalendarObjectByUID($principalUri, $uid) { |
|
| 2448 | + $query = $this->db->getQueryBuilder(); |
|
| 2449 | + $query->selectAlias('c.uri', 'calendaruri')->selectAlias('co.uri', 'objecturi') |
|
| 2450 | + ->from('calendarobjects', 'co') |
|
| 2451 | + ->leftJoin('co', 'calendars', 'c', $query->expr()->eq('co.calendarid', 'c.id')) |
|
| 2452 | + ->where($query->expr()->eq('c.principaluri', $query->createNamedParameter($principalUri))) |
|
| 2453 | + ->andWhere($query->expr()->eq('co.uid', $query->createNamedParameter($uid))) |
|
| 2454 | + ->andWhere($query->expr()->isNull('co.deleted_at')); |
|
| 2455 | + $stmt = $query->executeQuery(); |
|
| 2456 | + $row = $stmt->fetch(); |
|
| 2457 | + $stmt->closeCursor(); |
|
| 2458 | + if ($row) { |
|
| 2459 | + return $row['calendaruri'] . '/' . $row['objecturi']; |
|
| 2460 | + } |
|
| 2461 | + |
|
| 2462 | + return null; |
|
| 2463 | + } |
|
| 2464 | + |
|
| 2465 | + public function getCalendarObjectById(string $principalUri, int $id): ?array { |
|
| 2466 | + $query = $this->db->getQueryBuilder(); |
|
| 2467 | + $query->select(['co.id', 'co.uri', 'co.lastmodified', 'co.etag', 'co.calendarid', 'co.size', 'co.calendardata', 'co.componenttype', 'co.classification', 'co.deleted_at']) |
|
| 2468 | + ->selectAlias('c.uri', 'calendaruri') |
|
| 2469 | + ->from('calendarobjects', 'co') |
|
| 2470 | + ->join('co', 'calendars', 'c', $query->expr()->eq('c.id', 'co.calendarid', IQueryBuilder::PARAM_INT)) |
|
| 2471 | + ->where($query->expr()->eq('c.principaluri', $query->createNamedParameter($principalUri))) |
|
| 2472 | + ->andWhere($query->expr()->eq('co.id', $query->createNamedParameter($id, IQueryBuilder::PARAM_INT), IQueryBuilder::PARAM_INT)); |
|
| 2473 | + $stmt = $query->executeQuery(); |
|
| 2474 | + $row = $stmt->fetch(); |
|
| 2475 | + $stmt->closeCursor(); |
|
| 2476 | + |
|
| 2477 | + if (!$row) { |
|
| 2478 | + return null; |
|
| 2479 | + } |
|
| 2480 | + |
|
| 2481 | + return [ |
|
| 2482 | + 'id' => $row['id'], |
|
| 2483 | + 'uri' => $row['uri'], |
|
| 2484 | + 'lastmodified' => $row['lastmodified'], |
|
| 2485 | + 'etag' => '"' . $row['etag'] . '"', |
|
| 2486 | + 'calendarid' => $row['calendarid'], |
|
| 2487 | + 'calendaruri' => $row['calendaruri'], |
|
| 2488 | + 'size' => (int)$row['size'], |
|
| 2489 | + 'calendardata' => $this->readBlob($row['calendardata']), |
|
| 2490 | + 'component' => strtolower($row['componenttype']), |
|
| 2491 | + 'classification' => (int)$row['classification'], |
|
| 2492 | + 'deleted_at' => isset($row['deleted_at']) ? ((int)$row['deleted_at']) : null, |
|
| 2493 | + ]; |
|
| 2494 | + } |
|
| 2495 | + |
|
| 2496 | + /** |
|
| 2497 | + * The getChanges method returns all the changes that have happened, since |
|
| 2498 | + * the specified syncToken in the specified calendar. |
|
| 2499 | + * |
|
| 2500 | + * This function should return an array, such as the following: |
|
| 2501 | + * |
|
| 2502 | + * [ |
|
| 2503 | + * 'syncToken' => 'The current synctoken', |
|
| 2504 | + * 'added' => [ |
|
| 2505 | + * 'new.txt', |
|
| 2506 | + * ], |
|
| 2507 | + * 'modified' => [ |
|
| 2508 | + * 'modified.txt', |
|
| 2509 | + * ], |
|
| 2510 | + * 'deleted' => [ |
|
| 2511 | + * 'foo.php.bak', |
|
| 2512 | + * 'old.txt' |
|
| 2513 | + * ] |
|
| 2514 | + * ); |
|
| 2515 | + * |
|
| 2516 | + * The returned syncToken property should reflect the *current* syncToken |
|
| 2517 | + * of the calendar, as reported in the {http://sabredav.org/ns}sync-token |
|
| 2518 | + * property This is * needed here too, to ensure the operation is atomic. |
|
| 2519 | + * |
|
| 2520 | + * If the $syncToken argument is specified as null, this is an initial |
|
| 2521 | + * sync, and all members should be reported. |
|
| 2522 | + * |
|
| 2523 | + * The modified property is an array of nodenames that have changed since |
|
| 2524 | + * the last token. |
|
| 2525 | + * |
|
| 2526 | + * The deleted property is an array with nodenames, that have been deleted |
|
| 2527 | + * from collection. |
|
| 2528 | + * |
|
| 2529 | + * The $syncLevel argument is basically the 'depth' of the report. If it's |
|
| 2530 | + * 1, you only have to report changes that happened only directly in |
|
| 2531 | + * immediate descendants. If it's 2, it should also include changes from |
|
| 2532 | + * the nodes below the child collections. (grandchildren) |
|
| 2533 | + * |
|
| 2534 | + * The $limit argument allows a client to specify how many results should |
|
| 2535 | + * be returned at most. If the limit is not specified, it should be treated |
|
| 2536 | + * as infinite. |
|
| 2537 | + * |
|
| 2538 | + * If the limit (infinite or not) is higher than you're willing to return, |
|
| 2539 | + * you should throw a Sabre\DAV\Exception\TooMuchMatches() exception. |
|
| 2540 | + * |
|
| 2541 | + * If the syncToken is expired (due to data cleanup) or unknown, you must |
|
| 2542 | + * return null. |
|
| 2543 | + * |
|
| 2544 | + * The limit is 'suggestive'. You are free to ignore it. |
|
| 2545 | + * |
|
| 2546 | + * @param string $calendarId |
|
| 2547 | + * @param string $syncToken |
|
| 2548 | + * @param int $syncLevel |
|
| 2549 | + * @param int|null $limit |
|
| 2550 | + * @param int $calendarType |
|
| 2551 | + * @return ?array |
|
| 2552 | + */ |
|
| 2553 | + public function getChangesForCalendar($calendarId, $syncToken, $syncLevel, $limit = null, $calendarType = self::CALENDAR_TYPE_CALENDAR) { |
|
| 2554 | + $table = $calendarType === self::CALENDAR_TYPE_CALENDAR ? 'calendars': 'calendarsubscriptions'; |
|
| 2555 | + |
|
| 2556 | + return $this->atomic(function () use ($calendarId, $syncToken, $syncLevel, $limit, $calendarType, $table) { |
|
| 2557 | + // Current synctoken |
|
| 2558 | + $qb = $this->db->getQueryBuilder(); |
|
| 2559 | + $qb->select('synctoken') |
|
| 2560 | + ->from($table) |
|
| 2561 | + ->where( |
|
| 2562 | + $qb->expr()->eq('id', $qb->createNamedParameter($calendarId)) |
|
| 2563 | + ); |
|
| 2564 | + $stmt = $qb->executeQuery(); |
|
| 2565 | + $currentToken = $stmt->fetchOne(); |
|
| 2566 | + $initialSync = !is_numeric($syncToken); |
|
| 2567 | + |
|
| 2568 | + if ($currentToken === false) { |
|
| 2569 | + return null; |
|
| 2570 | + } |
|
| 2571 | + |
|
| 2572 | + // evaluate if this is a initial sync and construct appropriate command |
|
| 2573 | + if ($initialSync) { |
|
| 2574 | + $qb = $this->db->getQueryBuilder(); |
|
| 2575 | + $qb->select('uri') |
|
| 2576 | + ->from('calendarobjects') |
|
| 2577 | + ->where($qb->expr()->eq('calendarid', $qb->createNamedParameter($calendarId))) |
|
| 2578 | + ->andWhere($qb->expr()->eq('calendartype', $qb->createNamedParameter($calendarType))) |
|
| 2579 | + ->andWhere($qb->expr()->isNull('deleted_at')); |
|
| 2580 | + } else { |
|
| 2581 | + $qb = $this->db->getQueryBuilder(); |
|
| 2582 | + $qb->select('uri', $qb->func()->max('operation')) |
|
| 2583 | + ->from('calendarchanges') |
|
| 2584 | + ->where($qb->expr()->eq('calendarid', $qb->createNamedParameter($calendarId))) |
|
| 2585 | + ->andWhere($qb->expr()->eq('calendartype', $qb->createNamedParameter($calendarType))) |
|
| 2586 | + ->andWhere($qb->expr()->gte('synctoken', $qb->createNamedParameter($syncToken))) |
|
| 2587 | + ->andWhere($qb->expr()->lt('synctoken', $qb->createNamedParameter($currentToken))) |
|
| 2588 | + ->groupBy('uri'); |
|
| 2589 | + } |
|
| 2590 | + // evaluate if limit exists |
|
| 2591 | + if (is_numeric($limit)) { |
|
| 2592 | + $qb->setMaxResults($limit); |
|
| 2593 | + } |
|
| 2594 | + // execute command |
|
| 2595 | + $stmt = $qb->executeQuery(); |
|
| 2596 | + // build results |
|
| 2597 | + $result = ['syncToken' => $currentToken, 'added' => [], 'modified' => [], 'deleted' => []]; |
|
| 2598 | + // retrieve results |
|
| 2599 | + if ($initialSync) { |
|
| 2600 | + $result['added'] = $stmt->fetchAll(\PDO::FETCH_COLUMN); |
|
| 2601 | + } else { |
|
| 2602 | + // \PDO::FETCH_NUM is needed due to the inconsistent field names |
|
| 2603 | + // produced by doctrine for MAX() with different databases |
|
| 2604 | + while ($entry = $stmt->fetch(\PDO::FETCH_NUM)) { |
|
| 2605 | + // assign uri (column 0) to appropriate mutation based on operation (column 1) |
|
| 2606 | + // forced (int) is needed as doctrine with OCI returns the operation field as string not integer |
|
| 2607 | + match ((int)$entry[1]) { |
|
| 2608 | + 1 => $result['added'][] = $entry[0], |
|
| 2609 | + 2 => $result['modified'][] = $entry[0], |
|
| 2610 | + 3 => $result['deleted'][] = $entry[0], |
|
| 2611 | + default => $this->logger->debug('Unknown calendar change operation detected') |
|
| 2612 | + }; |
|
| 2613 | + } |
|
| 2614 | + } |
|
| 2615 | + $stmt->closeCursor(); |
|
| 2616 | + |
|
| 2617 | + return $result; |
|
| 2618 | + }, $this->db); |
|
| 2619 | + } |
|
| 2620 | + |
|
| 2621 | + /** |
|
| 2622 | + * Returns a list of subscriptions for a principal. |
|
| 2623 | + * |
|
| 2624 | + * Every subscription is an array with the following keys: |
|
| 2625 | + * * id, a unique id that will be used by other functions to modify the |
|
| 2626 | + * subscription. This can be the same as the uri or a database key. |
|
| 2627 | + * * uri. This is just the 'base uri' or 'filename' of the subscription. |
|
| 2628 | + * * principaluri. The owner of the subscription. Almost always the same as |
|
| 2629 | + * principalUri passed to this method. |
|
| 2630 | + * |
|
| 2631 | + * Furthermore, all the subscription info must be returned too: |
|
| 2632 | + * |
|
| 2633 | + * 1. {DAV:}displayname |
|
| 2634 | + * 2. {http://apple.com/ns/ical/}refreshrate |
|
| 2635 | + * 3. {http://calendarserver.org/ns/}subscribed-strip-todos (omit if todos |
|
| 2636 | + * should not be stripped). |
|
| 2637 | + * 4. {http://calendarserver.org/ns/}subscribed-strip-alarms (omit if alarms |
|
| 2638 | + * should not be stripped). |
|
| 2639 | + * 5. {http://calendarserver.org/ns/}subscribed-strip-attachments (omit if |
|
| 2640 | + * attachments should not be stripped). |
|
| 2641 | + * 6. {http://calendarserver.org/ns/}source (Must be a |
|
| 2642 | + * Sabre\DAV\Property\Href). |
|
| 2643 | + * 7. {http://apple.com/ns/ical/}calendar-color |
|
| 2644 | + * 8. {http://apple.com/ns/ical/}calendar-order |
|
| 2645 | + * 9. {urn:ietf:params:xml:ns:caldav}supported-calendar-component-set |
|
| 2646 | + * (should just be an instance of |
|
| 2647 | + * Sabre\CalDAV\Property\SupportedCalendarComponentSet, with a bunch of |
|
| 2648 | + * default components). |
|
| 2649 | + * |
|
| 2650 | + * @param string $principalUri |
|
| 2651 | + * @return array |
|
| 2652 | + */ |
|
| 2653 | + public function getSubscriptionsForUser($principalUri) { |
|
| 2654 | + $fields = array_column($this->subscriptionPropertyMap, 0); |
|
| 2655 | + $fields[] = 'id'; |
|
| 2656 | + $fields[] = 'uri'; |
|
| 2657 | + $fields[] = 'source'; |
|
| 2658 | + $fields[] = 'principaluri'; |
|
| 2659 | + $fields[] = 'lastmodified'; |
|
| 2660 | + $fields[] = 'synctoken'; |
|
| 2661 | + |
|
| 2662 | + $query = $this->db->getQueryBuilder(); |
|
| 2663 | + $query->select($fields) |
|
| 2664 | + ->from('calendarsubscriptions') |
|
| 2665 | + ->where($query->expr()->eq('principaluri', $query->createNamedParameter($principalUri))) |
|
| 2666 | + ->orderBy('calendarorder', 'asc'); |
|
| 2667 | + $stmt = $query->executeQuery(); |
|
| 2668 | + |
|
| 2669 | + $subscriptions = []; |
|
| 2670 | + while ($row = $stmt->fetch()) { |
|
| 2671 | + $subscription = [ |
|
| 2672 | + 'id' => $row['id'], |
|
| 2673 | + 'uri' => $row['uri'], |
|
| 2674 | + 'principaluri' => $row['principaluri'], |
|
| 2675 | + 'source' => $row['source'], |
|
| 2676 | + 'lastmodified' => $row['lastmodified'], |
|
| 2677 | + |
|
| 2678 | + '{' . Plugin::NS_CALDAV . '}supported-calendar-component-set' => new SupportedCalendarComponentSet(['VTODO', 'VEVENT']), |
|
| 2679 | + '{http://sabredav.org/ns}sync-token' => $row['synctoken'] ?: '0', |
|
| 2680 | + ]; |
|
| 2681 | + |
|
| 2682 | + $subscriptions[] = $this->rowToSubscription($row, $subscription); |
|
| 2683 | + } |
|
| 2684 | + |
|
| 2685 | + return $subscriptions; |
|
| 2686 | + } |
|
| 2687 | + |
|
| 2688 | + /** |
|
| 2689 | + * Creates a new subscription for a principal. |
|
| 2690 | + * |
|
| 2691 | + * If the creation was a success, an id must be returned that can be used to reference |
|
| 2692 | + * this subscription in other methods, such as updateSubscription. |
|
| 2693 | + * |
|
| 2694 | + * @param string $principalUri |
|
| 2695 | + * @param string $uri |
|
| 2696 | + * @param array $properties |
|
| 2697 | + * @return mixed |
|
| 2698 | + */ |
|
| 2699 | + public function createSubscription($principalUri, $uri, array $properties) { |
|
| 2700 | + if (!isset($properties['{http://calendarserver.org/ns/}source'])) { |
|
| 2701 | + throw new Forbidden('The {http://calendarserver.org/ns/}source property is required when creating subscriptions'); |
|
| 2702 | + } |
|
| 2703 | + |
|
| 2704 | + $values = [ |
|
| 2705 | + 'principaluri' => $principalUri, |
|
| 2706 | + 'uri' => $uri, |
|
| 2707 | + 'source' => $properties['{http://calendarserver.org/ns/}source']->getHref(), |
|
| 2708 | + 'lastmodified' => time(), |
|
| 2709 | + ]; |
|
| 2710 | + |
|
| 2711 | + $propertiesBoolean = ['striptodos', 'stripalarms', 'stripattachments']; |
|
| 2712 | + |
|
| 2713 | + foreach ($this->subscriptionPropertyMap as $xmlName => [$dbName, $type]) { |
|
| 2714 | + if (array_key_exists($xmlName, $properties)) { |
|
| 2715 | + $values[$dbName] = $properties[$xmlName]; |
|
| 2716 | + if (in_array($dbName, $propertiesBoolean)) { |
|
| 2717 | + $values[$dbName] = true; |
|
| 2718 | + } |
|
| 2719 | + } |
|
| 2720 | + } |
|
| 2721 | + |
|
| 2722 | + [$subscriptionId, $subscriptionRow] = $this->atomic(function () use ($values) { |
|
| 2723 | + $valuesToInsert = []; |
|
| 2724 | + $query = $this->db->getQueryBuilder(); |
|
| 2725 | + foreach (array_keys($values) as $name) { |
|
| 2726 | + $valuesToInsert[$name] = $query->createNamedParameter($values[$name]); |
|
| 2727 | + } |
|
| 2728 | + $query->insert('calendarsubscriptions') |
|
| 2729 | + ->values($valuesToInsert) |
|
| 2730 | + ->executeStatement(); |
|
| 2731 | + |
|
| 2732 | + $subscriptionId = $query->getLastInsertId(); |
|
| 2733 | + |
|
| 2734 | + $subscriptionRow = $this->getSubscriptionById($subscriptionId); |
|
| 2735 | + return [$subscriptionId, $subscriptionRow]; |
|
| 2736 | + }, $this->db); |
|
| 2737 | + |
|
| 2738 | + $this->dispatcher->dispatchTyped(new SubscriptionCreatedEvent($subscriptionId, $subscriptionRow)); |
|
| 2739 | + |
|
| 2740 | + return $subscriptionId; |
|
| 2741 | + } |
|
| 2742 | + |
|
| 2743 | + /** |
|
| 2744 | + * Updates a subscription |
|
| 2745 | + * |
|
| 2746 | + * The list of mutations is stored in a Sabre\DAV\PropPatch object. |
|
| 2747 | + * To do the actual updates, you must tell this object which properties |
|
| 2748 | + * you're going to process with the handle() method. |
|
| 2749 | + * |
|
| 2750 | + * Calling the handle method is like telling the PropPatch object "I |
|
| 2751 | + * promise I can handle updating this property". |
|
| 2752 | + * |
|
| 2753 | + * Read the PropPatch documentation for more info and examples. |
|
| 2754 | + * |
|
| 2755 | + * @param mixed $subscriptionId |
|
| 2756 | + * @param PropPatch $propPatch |
|
| 2757 | + * @return void |
|
| 2758 | + */ |
|
| 2759 | + public function updateSubscription($subscriptionId, PropPatch $propPatch) { |
|
| 2760 | + $supportedProperties = array_keys($this->subscriptionPropertyMap); |
|
| 2761 | + $supportedProperties[] = '{http://calendarserver.org/ns/}source'; |
|
| 2762 | + |
|
| 2763 | + $propPatch->handle($supportedProperties, function ($mutations) use ($subscriptionId) { |
|
| 2764 | + $newValues = []; |
|
| 2765 | + |
|
| 2766 | + foreach ($mutations as $propertyName => $propertyValue) { |
|
| 2767 | + if ($propertyName === '{http://calendarserver.org/ns/}source') { |
|
| 2768 | + $newValues['source'] = $propertyValue->getHref(); |
|
| 2769 | + } else { |
|
| 2770 | + $fieldName = $this->subscriptionPropertyMap[$propertyName][0]; |
|
| 2771 | + $newValues[$fieldName] = $propertyValue; |
|
| 2772 | + } |
|
| 2773 | + } |
|
| 2774 | + |
|
| 2775 | + $subscriptionRow = $this->atomic(function () use ($subscriptionId, $newValues) { |
|
| 2776 | + $query = $this->db->getQueryBuilder(); |
|
| 2777 | + $query->update('calendarsubscriptions') |
|
| 2778 | + ->set('lastmodified', $query->createNamedParameter(time())); |
|
| 2779 | + foreach ($newValues as $fieldName => $value) { |
|
| 2780 | + $query->set($fieldName, $query->createNamedParameter($value)); |
|
| 2781 | + } |
|
| 2782 | + $query->where($query->expr()->eq('id', $query->createNamedParameter($subscriptionId))) |
|
| 2783 | + ->executeStatement(); |
|
| 2784 | + |
|
| 2785 | + return $this->getSubscriptionById($subscriptionId); |
|
| 2786 | + }, $this->db); |
|
| 2787 | + |
|
| 2788 | + $this->dispatcher->dispatchTyped(new SubscriptionUpdatedEvent((int)$subscriptionId, $subscriptionRow, [], $mutations)); |
|
| 2789 | + |
|
| 2790 | + return true; |
|
| 2791 | + }); |
|
| 2792 | + } |
|
| 2793 | + |
|
| 2794 | + /** |
|
| 2795 | + * Deletes a subscription. |
|
| 2796 | + * |
|
| 2797 | + * @param mixed $subscriptionId |
|
| 2798 | + * @return void |
|
| 2799 | + */ |
|
| 2800 | + public function deleteSubscription($subscriptionId) { |
|
| 2801 | + $this->atomic(function () use ($subscriptionId): void { |
|
| 2802 | + $subscriptionRow = $this->getSubscriptionById($subscriptionId); |
|
| 2803 | + |
|
| 2804 | + $query = $this->db->getQueryBuilder(); |
|
| 2805 | + $query->delete('calendarsubscriptions') |
|
| 2806 | + ->where($query->expr()->eq('id', $query->createNamedParameter($subscriptionId))) |
|
| 2807 | + ->executeStatement(); |
|
| 2808 | + |
|
| 2809 | + $query = $this->db->getQueryBuilder(); |
|
| 2810 | + $query->delete('calendarobjects') |
|
| 2811 | + ->where($query->expr()->eq('calendarid', $query->createNamedParameter($subscriptionId))) |
|
| 2812 | + ->andWhere($query->expr()->eq('calendartype', $query->createNamedParameter(self::CALENDAR_TYPE_SUBSCRIPTION))) |
|
| 2813 | + ->executeStatement(); |
|
| 2814 | + |
|
| 2815 | + $query->delete('calendarchanges') |
|
| 2816 | + ->where($query->expr()->eq('calendarid', $query->createNamedParameter($subscriptionId))) |
|
| 2817 | + ->andWhere($query->expr()->eq('calendartype', $query->createNamedParameter(self::CALENDAR_TYPE_SUBSCRIPTION))) |
|
| 2818 | + ->executeStatement(); |
|
| 2819 | + |
|
| 2820 | + $query->delete($this->dbObjectPropertiesTable) |
|
| 2821 | + ->where($query->expr()->eq('calendarid', $query->createNamedParameter($subscriptionId))) |
|
| 2822 | + ->andWhere($query->expr()->eq('calendartype', $query->createNamedParameter(self::CALENDAR_TYPE_SUBSCRIPTION))) |
|
| 2823 | + ->executeStatement(); |
|
| 2824 | + |
|
| 2825 | + if ($subscriptionRow) { |
|
| 2826 | + $this->dispatcher->dispatchTyped(new SubscriptionDeletedEvent((int)$subscriptionId, $subscriptionRow, [])); |
|
| 2827 | + } |
|
| 2828 | + }, $this->db); |
|
| 2829 | + } |
|
| 2830 | + |
|
| 2831 | + /** |
|
| 2832 | + * Returns a single scheduling object for the inbox collection. |
|
| 2833 | + * |
|
| 2834 | + * The returned array should contain the following elements: |
|
| 2835 | + * * uri - A unique basename for the object. This will be used to |
|
| 2836 | + * construct a full uri. |
|
| 2837 | + * * calendardata - The iCalendar object |
|
| 2838 | + * * lastmodified - The last modification date. Can be an int for a unix |
|
| 2839 | + * timestamp, or a PHP DateTime object. |
|
| 2840 | + * * etag - A unique token that must change if the object changed. |
|
| 2841 | + * * size - The size of the object, in bytes. |
|
| 2842 | + * |
|
| 2843 | + * @param string $principalUri |
|
| 2844 | + * @param string $objectUri |
|
| 2845 | + * @return array |
|
| 2846 | + */ |
|
| 2847 | + public function getSchedulingObject($principalUri, $objectUri) { |
|
| 2848 | + $query = $this->db->getQueryBuilder(); |
|
| 2849 | + $stmt = $query->select(['uri', 'calendardata', 'lastmodified', 'etag', 'size']) |
|
| 2850 | + ->from('schedulingobjects') |
|
| 2851 | + ->where($query->expr()->eq('principaluri', $query->createNamedParameter($principalUri))) |
|
| 2852 | + ->andWhere($query->expr()->eq('uri', $query->createNamedParameter($objectUri))) |
|
| 2853 | + ->executeQuery(); |
|
| 2854 | + |
|
| 2855 | + $row = $stmt->fetch(); |
|
| 2856 | + |
|
| 2857 | + if (!$row) { |
|
| 2858 | + return null; |
|
| 2859 | + } |
|
| 2860 | + |
|
| 2861 | + return [ |
|
| 2862 | + 'uri' => $row['uri'], |
|
| 2863 | + 'calendardata' => $row['calendardata'], |
|
| 2864 | + 'lastmodified' => $row['lastmodified'], |
|
| 2865 | + 'etag' => '"' . $row['etag'] . '"', |
|
| 2866 | + 'size' => (int)$row['size'], |
|
| 2867 | + ]; |
|
| 2868 | + } |
|
| 2869 | + |
|
| 2870 | + /** |
|
| 2871 | + * Returns all scheduling objects for the inbox collection. |
|
| 2872 | + * |
|
| 2873 | + * These objects should be returned as an array. Every item in the array |
|
| 2874 | + * should follow the same structure as returned from getSchedulingObject. |
|
| 2875 | + * |
|
| 2876 | + * The main difference is that 'calendardata' is optional. |
|
| 2877 | + * |
|
| 2878 | + * @param string $principalUri |
|
| 2879 | + * @return array |
|
| 2880 | + */ |
|
| 2881 | + public function getSchedulingObjects($principalUri) { |
|
| 2882 | + $query = $this->db->getQueryBuilder(); |
|
| 2883 | + $stmt = $query->select(['uri', 'calendardata', 'lastmodified', 'etag', 'size']) |
|
| 2884 | + ->from('schedulingobjects') |
|
| 2885 | + ->where($query->expr()->eq('principaluri', $query->createNamedParameter($principalUri))) |
|
| 2886 | + ->executeQuery(); |
|
| 2887 | + |
|
| 2888 | + $results = []; |
|
| 2889 | + while (($row = $stmt->fetch()) !== false) { |
|
| 2890 | + $results[] = [ |
|
| 2891 | + 'calendardata' => $row['calendardata'], |
|
| 2892 | + 'uri' => $row['uri'], |
|
| 2893 | + 'lastmodified' => $row['lastmodified'], |
|
| 2894 | + 'etag' => '"' . $row['etag'] . '"', |
|
| 2895 | + 'size' => (int)$row['size'], |
|
| 2896 | + ]; |
|
| 2897 | + } |
|
| 2898 | + $stmt->closeCursor(); |
|
| 2899 | + |
|
| 2900 | + return $results; |
|
| 2901 | + } |
|
| 2902 | + |
|
| 2903 | + /** |
|
| 2904 | + * Deletes a scheduling object from the inbox collection. |
|
| 2905 | + * |
|
| 2906 | + * @param string $principalUri |
|
| 2907 | + * @param string $objectUri |
|
| 2908 | + * @return void |
|
| 2909 | + */ |
|
| 2910 | + public function deleteSchedulingObject($principalUri, $objectUri) { |
|
| 2911 | + $this->cachedObjects = []; |
|
| 2912 | + $query = $this->db->getQueryBuilder(); |
|
| 2913 | + $query->delete('schedulingobjects') |
|
| 2914 | + ->where($query->expr()->eq('principaluri', $query->createNamedParameter($principalUri))) |
|
| 2915 | + ->andWhere($query->expr()->eq('uri', $query->createNamedParameter($objectUri))) |
|
| 2916 | + ->executeStatement(); |
|
| 2917 | + } |
|
| 2918 | + |
|
| 2919 | + /** |
|
| 2920 | + * Deletes all scheduling objects last modified before $modifiedBefore from the inbox collection. |
|
| 2921 | + * |
|
| 2922 | + * @param int $modifiedBefore |
|
| 2923 | + * @param int $limit |
|
| 2924 | + * @return void |
|
| 2925 | + */ |
|
| 2926 | + public function deleteOutdatedSchedulingObjects(int $modifiedBefore, int $limit): void { |
|
| 2927 | + $query = $this->db->getQueryBuilder(); |
|
| 2928 | + $query->select('id') |
|
| 2929 | + ->from('schedulingobjects') |
|
| 2930 | + ->where($query->expr()->lt('lastmodified', $query->createNamedParameter($modifiedBefore))) |
|
| 2931 | + ->setMaxResults($limit); |
|
| 2932 | + $result = $query->executeQuery(); |
|
| 2933 | + $count = $result->rowCount(); |
|
| 2934 | + if ($count === 0) { |
|
| 2935 | + return; |
|
| 2936 | + } |
|
| 2937 | + $ids = array_map(static function (array $id) { |
|
| 2938 | + return (int)$id[0]; |
|
| 2939 | + }, $result->fetchAll(\PDO::FETCH_NUM)); |
|
| 2940 | + $result->closeCursor(); |
|
| 2941 | + |
|
| 2942 | + $numDeleted = 0; |
|
| 2943 | + $deleteQuery = $this->db->getQueryBuilder(); |
|
| 2944 | + $deleteQuery->delete('schedulingobjects') |
|
| 2945 | + ->where($deleteQuery->expr()->in('id', $deleteQuery->createParameter('ids'), IQueryBuilder::PARAM_INT_ARRAY)); |
|
| 2946 | + foreach (array_chunk($ids, 1000) as $chunk) { |
|
| 2947 | + $deleteQuery->setParameter('ids', $chunk, IQueryBuilder::PARAM_INT_ARRAY); |
|
| 2948 | + $numDeleted += $deleteQuery->executeStatement(); |
|
| 2949 | + } |
|
| 2950 | + |
|
| 2951 | + if ($numDeleted === $limit) { |
|
| 2952 | + $this->logger->info("Deleted $limit scheduling objects, continuing with next batch"); |
|
| 2953 | + $this->deleteOutdatedSchedulingObjects($modifiedBefore, $limit); |
|
| 2954 | + } |
|
| 2955 | + } |
|
| 2956 | + |
|
| 2957 | + /** |
|
| 2958 | + * Creates a new scheduling object. This should land in a users' inbox. |
|
| 2959 | + * |
|
| 2960 | + * @param string $principalUri |
|
| 2961 | + * @param string $objectUri |
|
| 2962 | + * @param string $objectData |
|
| 2963 | + * @return void |
|
| 2964 | + */ |
|
| 2965 | + public function createSchedulingObject($principalUri, $objectUri, $objectData) { |
|
| 2966 | + $this->cachedObjects = []; |
|
| 2967 | + $query = $this->db->getQueryBuilder(); |
|
| 2968 | + $query->insert('schedulingobjects') |
|
| 2969 | + ->values([ |
|
| 2970 | + 'principaluri' => $query->createNamedParameter($principalUri), |
|
| 2971 | + 'calendardata' => $query->createNamedParameter($objectData, IQueryBuilder::PARAM_LOB), |
|
| 2972 | + 'uri' => $query->createNamedParameter($objectUri), |
|
| 2973 | + 'lastmodified' => $query->createNamedParameter(time()), |
|
| 2974 | + 'etag' => $query->createNamedParameter(md5($objectData)), |
|
| 2975 | + 'size' => $query->createNamedParameter(strlen($objectData)) |
|
| 2976 | + ]) |
|
| 2977 | + ->executeStatement(); |
|
| 2978 | + } |
|
| 2979 | + |
|
| 2980 | + /** |
|
| 2981 | + * Adds a change record to the calendarchanges table. |
|
| 2982 | + * |
|
| 2983 | + * @param mixed $calendarId |
|
| 2984 | + * @param string[] $objectUris |
|
| 2985 | + * @param int $operation 1 = add, 2 = modify, 3 = delete. |
|
| 2986 | + * @param int $calendarType |
|
| 2987 | + * @return void |
|
| 2988 | + */ |
|
| 2989 | + protected function addChanges(int $calendarId, array $objectUris, int $operation, int $calendarType = self::CALENDAR_TYPE_CALENDAR): void { |
|
| 2990 | + $this->cachedObjects = []; |
|
| 2991 | + $table = $calendarType === self::CALENDAR_TYPE_CALENDAR ? 'calendars': 'calendarsubscriptions'; |
|
| 2992 | + |
|
| 2993 | + $this->atomic(function () use ($calendarId, $objectUris, $operation, $calendarType, $table): void { |
|
| 2994 | + $query = $this->db->getQueryBuilder(); |
|
| 2995 | + $query->select('synctoken') |
|
| 2996 | + ->from($table) |
|
| 2997 | + ->where($query->expr()->eq('id', $query->createNamedParameter($calendarId))); |
|
| 2998 | + $result = $query->executeQuery(); |
|
| 2999 | + $syncToken = (int)$result->fetchOne(); |
|
| 3000 | + $result->closeCursor(); |
|
| 3001 | + |
|
| 3002 | + $query = $this->db->getQueryBuilder(); |
|
| 3003 | + $query->insert('calendarchanges') |
|
| 3004 | + ->values([ |
|
| 3005 | + 'uri' => $query->createParameter('uri'), |
|
| 3006 | + 'synctoken' => $query->createNamedParameter($syncToken), |
|
| 3007 | + 'calendarid' => $query->createNamedParameter($calendarId), |
|
| 3008 | + 'operation' => $query->createNamedParameter($operation), |
|
| 3009 | + 'calendartype' => $query->createNamedParameter($calendarType), |
|
| 3010 | + 'created_at' => time(), |
|
| 3011 | + ]); |
|
| 3012 | + foreach ($objectUris as $uri) { |
|
| 3013 | + $query->setParameter('uri', $uri); |
|
| 3014 | + $query->executeStatement(); |
|
| 3015 | + } |
|
| 3016 | + |
|
| 3017 | + $query = $this->db->getQueryBuilder(); |
|
| 3018 | + $query->update($table) |
|
| 3019 | + ->set('synctoken', $query->createNamedParameter($syncToken + 1, IQueryBuilder::PARAM_INT)) |
|
| 3020 | + ->where($query->expr()->eq('id', $query->createNamedParameter($calendarId))) |
|
| 3021 | + ->executeStatement(); |
|
| 3022 | + }, $this->db); |
|
| 3023 | + } |
|
| 3024 | + |
|
| 3025 | + public function restoreChanges(int $calendarId, int $calendarType = self::CALENDAR_TYPE_CALENDAR): void { |
|
| 3026 | + $this->cachedObjects = []; |
|
| 3027 | + |
|
| 3028 | + $this->atomic(function () use ($calendarId, $calendarType): void { |
|
| 3029 | + $qbAdded = $this->db->getQueryBuilder(); |
|
| 3030 | + $qbAdded->select('uri') |
|
| 3031 | + ->from('calendarobjects') |
|
| 3032 | + ->where( |
|
| 3033 | + $qbAdded->expr()->andX( |
|
| 3034 | + $qbAdded->expr()->eq('calendarid', $qbAdded->createNamedParameter($calendarId)), |
|
| 3035 | + $qbAdded->expr()->eq('calendartype', $qbAdded->createNamedParameter($calendarType)), |
|
| 3036 | + $qbAdded->expr()->isNull('deleted_at'), |
|
| 3037 | + ) |
|
| 3038 | + ); |
|
| 3039 | + $resultAdded = $qbAdded->executeQuery(); |
|
| 3040 | + $addedUris = $resultAdded->fetchAll(\PDO::FETCH_COLUMN); |
|
| 3041 | + $resultAdded->closeCursor(); |
|
| 3042 | + // Track everything as changed |
|
| 3043 | + // Tracking the creation is not necessary because \OCA\DAV\CalDAV\CalDavBackend::getChangesForCalendar |
|
| 3044 | + // only returns the last change per object. |
|
| 3045 | + $this->addChanges($calendarId, $addedUris, 2, $calendarType); |
|
| 3046 | + |
|
| 3047 | + $qbDeleted = $this->db->getQueryBuilder(); |
|
| 3048 | + $qbDeleted->select('uri') |
|
| 3049 | + ->from('calendarobjects') |
|
| 3050 | + ->where( |
|
| 3051 | + $qbDeleted->expr()->andX( |
|
| 3052 | + $qbDeleted->expr()->eq('calendarid', $qbDeleted->createNamedParameter($calendarId)), |
|
| 3053 | + $qbDeleted->expr()->eq('calendartype', $qbDeleted->createNamedParameter($calendarType)), |
|
| 3054 | + $qbDeleted->expr()->isNotNull('deleted_at'), |
|
| 3055 | + ) |
|
| 3056 | + ); |
|
| 3057 | + $resultDeleted = $qbDeleted->executeQuery(); |
|
| 3058 | + $deletedUris = array_map(function (string $uri) { |
|
| 3059 | + return str_replace('-deleted.ics', '.ics', $uri); |
|
| 3060 | + }, $resultDeleted->fetchAll(\PDO::FETCH_COLUMN)); |
|
| 3061 | + $resultDeleted->closeCursor(); |
|
| 3062 | + $this->addChanges($calendarId, $deletedUris, 3, $calendarType); |
|
| 3063 | + }, $this->db); |
|
| 3064 | + } |
|
| 3065 | + |
|
| 3066 | + /** |
|
| 3067 | + * Parses some information from calendar objects, used for optimized |
|
| 3068 | + * calendar-queries. |
|
| 3069 | + * |
|
| 3070 | + * Returns an array with the following keys: |
|
| 3071 | + * * etag - An md5 checksum of the object without the quotes. |
|
| 3072 | + * * size - Size of the object in bytes |
|
| 3073 | + * * componentType - VEVENT, VTODO or VJOURNAL |
|
| 3074 | + * * firstOccurence |
|
| 3075 | + * * lastOccurence |
|
| 3076 | + * * uid - value of the UID property |
|
| 3077 | + * |
|
| 3078 | + * @param string $calendarData |
|
| 3079 | + * @return array |
|
| 3080 | + */ |
|
| 3081 | + public function getDenormalizedData(string $calendarData): array { |
|
| 3082 | + $vObject = Reader::read($calendarData); |
|
| 3083 | + $vEvents = []; |
|
| 3084 | + $componentType = null; |
|
| 3085 | + $component = null; |
|
| 3086 | + $firstOccurrence = null; |
|
| 3087 | + $lastOccurrence = null; |
|
| 3088 | + $uid = null; |
|
| 3089 | + $classification = self::CLASSIFICATION_PUBLIC; |
|
| 3090 | + $hasDTSTART = false; |
|
| 3091 | + foreach ($vObject->getComponents() as $component) { |
|
| 3092 | + if ($component->name !== 'VTIMEZONE') { |
|
| 3093 | + // Finding all VEVENTs, and track them |
|
| 3094 | + if ($component->name === 'VEVENT') { |
|
| 3095 | + $vEvents[] = $component; |
|
| 3096 | + if ($component->DTSTART) { |
|
| 3097 | + $hasDTSTART = true; |
|
| 3098 | + } |
|
| 3099 | + } |
|
| 3100 | + // Track first component type and uid |
|
| 3101 | + if ($uid === null) { |
|
| 3102 | + $componentType = $component->name; |
|
| 3103 | + $uid = (string)$component->UID; |
|
| 3104 | + } |
|
| 3105 | + } |
|
| 3106 | + } |
|
| 3107 | + if (!$componentType) { |
|
| 3108 | + throw new BadRequest('Calendar objects must have a VJOURNAL, VEVENT or VTODO component'); |
|
| 3109 | + } |
|
| 3110 | + |
|
| 3111 | + if ($hasDTSTART) { |
|
| 3112 | + $component = $vEvents[0]; |
|
| 3113 | + |
|
| 3114 | + // Finding the last occurrence is a bit harder |
|
| 3115 | + if (!isset($component->RRULE) && count($vEvents) === 1) { |
|
| 3116 | + $firstOccurrence = $component->DTSTART->getDateTime()->getTimeStamp(); |
|
| 3117 | + if (isset($component->DTEND)) { |
|
| 3118 | + $lastOccurrence = $component->DTEND->getDateTime()->getTimeStamp(); |
|
| 3119 | + } elseif (isset($component->DURATION)) { |
|
| 3120 | + $endDate = clone $component->DTSTART->getDateTime(); |
|
| 3121 | + $endDate->add(DateTimeParser::parse($component->DURATION->getValue())); |
|
| 3122 | + $lastOccurrence = $endDate->getTimeStamp(); |
|
| 3123 | + } elseif (!$component->DTSTART->hasTime()) { |
|
| 3124 | + $endDate = clone $component->DTSTART->getDateTime(); |
|
| 3125 | + $endDate->modify('+1 day'); |
|
| 3126 | + $lastOccurrence = $endDate->getTimeStamp(); |
|
| 3127 | + } else { |
|
| 3128 | + $lastOccurrence = $firstOccurrence; |
|
| 3129 | + } |
|
| 3130 | + } else { |
|
| 3131 | + try { |
|
| 3132 | + $it = new EventIterator($vEvents); |
|
| 3133 | + } catch (NoInstancesException $e) { |
|
| 3134 | + $this->logger->debug('Caught no instance exception for calendar data. This usually indicates invalid calendar data.', [ |
|
| 3135 | + 'app' => 'dav', |
|
| 3136 | + 'exception' => $e, |
|
| 3137 | + ]); |
|
| 3138 | + throw new Forbidden($e->getMessage()); |
|
| 3139 | + } |
|
| 3140 | + $maxDate = new DateTime(self::MAX_DATE); |
|
| 3141 | + $firstOccurrence = $it->getDtStart()->getTimestamp(); |
|
| 3142 | + if ($it->isInfinite()) { |
|
| 3143 | + $lastOccurrence = $maxDate->getTimestamp(); |
|
| 3144 | + } else { |
|
| 3145 | + $end = $it->getDtEnd(); |
|
| 3146 | + while ($it->valid() && $end < $maxDate) { |
|
| 3147 | + $end = $it->getDtEnd(); |
|
| 3148 | + $it->next(); |
|
| 3149 | + } |
|
| 3150 | + $lastOccurrence = $end->getTimestamp(); |
|
| 3151 | + } |
|
| 3152 | + } |
|
| 3153 | + } |
|
| 3154 | + |
|
| 3155 | + if ($component->CLASS) { |
|
| 3156 | + $classification = CalDavBackend::CLASSIFICATION_PRIVATE; |
|
| 3157 | + switch ($component->CLASS->getValue()) { |
|
| 3158 | + case 'PUBLIC': |
|
| 3159 | + $classification = CalDavBackend::CLASSIFICATION_PUBLIC; |
|
| 3160 | + break; |
|
| 3161 | + case 'CONFIDENTIAL': |
|
| 3162 | + $classification = CalDavBackend::CLASSIFICATION_CONFIDENTIAL; |
|
| 3163 | + break; |
|
| 3164 | + } |
|
| 3165 | + } |
|
| 3166 | + return [ |
|
| 3167 | + 'etag' => md5($calendarData), |
|
| 3168 | + 'size' => strlen($calendarData), |
|
| 3169 | + 'componentType' => $componentType, |
|
| 3170 | + 'firstOccurence' => is_null($firstOccurrence) ? null : max(0, $firstOccurrence), |
|
| 3171 | + 'lastOccurence' => is_null($lastOccurrence) ? null : max(0, $lastOccurrence), |
|
| 3172 | + 'uid' => $uid, |
|
| 3173 | + 'classification' => $classification |
|
| 3174 | + ]; |
|
| 3175 | + } |
|
| 3176 | + |
|
| 3177 | + /** |
|
| 3178 | + * @param $cardData |
|
| 3179 | + * @return bool|string |
|
| 3180 | + */ |
|
| 3181 | + private function readBlob($cardData) { |
|
| 3182 | + if (is_resource($cardData)) { |
|
| 3183 | + return stream_get_contents($cardData); |
|
| 3184 | + } |
|
| 3185 | + |
|
| 3186 | + return $cardData; |
|
| 3187 | + } |
|
| 3188 | + |
|
| 3189 | + /** |
|
| 3190 | + * @param list<array{href: string, commonName: string, readOnly: bool}> $add |
|
| 3191 | + * @param list<string> $remove |
|
| 3192 | + */ |
|
| 3193 | + public function updateShares(IShareable $shareable, array $add, array $remove): void { |
|
| 3194 | + $this->atomic(function () use ($shareable, $add, $remove): void { |
|
| 3195 | + $calendarId = $shareable->getResourceId(); |
|
| 3196 | + $calendarRow = $this->getCalendarById($calendarId); |
|
| 3197 | + if ($calendarRow === null) { |
|
| 3198 | + throw new \RuntimeException('Trying to update shares for non-existing calendar: ' . $calendarId); |
|
| 3199 | + } |
|
| 3200 | + $oldShares = $this->getShares($calendarId); |
|
| 3201 | + |
|
| 3202 | + $this->calendarSharingBackend->updateShares($shareable, $add, $remove, $oldShares); |
|
| 3203 | + |
|
| 3204 | + $this->dispatcher->dispatchTyped(new CalendarShareUpdatedEvent($calendarId, $calendarRow, $oldShares, $add, $remove)); |
|
| 3205 | + }, $this->db); |
|
| 3206 | + } |
|
| 3207 | + |
|
| 3208 | + /** |
|
| 3209 | + * @return list<array{href: string, commonName: string, status: int, readOnly: bool, '{http://owncloud.org/ns}principal': string, '{http://owncloud.org/ns}group-share': bool}> |
|
| 3210 | + */ |
|
| 3211 | + public function getShares(int $resourceId): array { |
|
| 3212 | + return $this->calendarSharingBackend->getShares($resourceId); |
|
| 3213 | + } |
|
| 3214 | + |
|
| 3215 | + public function preloadShares(array $resourceIds): void { |
|
| 3216 | + $this->calendarSharingBackend->preloadShares($resourceIds); |
|
| 3217 | + } |
|
| 3218 | + |
|
| 3219 | + /** |
|
| 3220 | + * @param boolean $value |
|
| 3221 | + * @param Calendar $calendar |
|
| 3222 | + * @return string|null |
|
| 3223 | + */ |
|
| 3224 | + public function setPublishStatus($value, $calendar) { |
|
| 3225 | + return $this->atomic(function () use ($value, $calendar) { |
|
| 3226 | + $calendarId = $calendar->getResourceId(); |
|
| 3227 | + $calendarData = $this->getCalendarById($calendarId); |
|
| 3228 | + |
|
| 3229 | + $query = $this->db->getQueryBuilder(); |
|
| 3230 | + if ($value) { |
|
| 3231 | + $publicUri = $this->random->generate(16, ISecureRandom::CHAR_HUMAN_READABLE); |
|
| 3232 | + $query->insert('dav_shares') |
|
| 3233 | + ->values([ |
|
| 3234 | + 'principaluri' => $query->createNamedParameter($calendar->getPrincipalURI()), |
|
| 3235 | + 'type' => $query->createNamedParameter('calendar'), |
|
| 3236 | + 'access' => $query->createNamedParameter(self::ACCESS_PUBLIC), |
|
| 3237 | + 'resourceid' => $query->createNamedParameter($calendar->getResourceId()), |
|
| 3238 | + 'publicuri' => $query->createNamedParameter($publicUri) |
|
| 3239 | + ]); |
|
| 3240 | + $query->executeStatement(); |
|
| 3241 | + |
|
| 3242 | + $this->dispatcher->dispatchTyped(new CalendarPublishedEvent($calendarId, $calendarData, $publicUri)); |
|
| 3243 | + return $publicUri; |
|
| 3244 | + } |
|
| 3245 | + $query->delete('dav_shares') |
|
| 3246 | + ->where($query->expr()->eq('resourceid', $query->createNamedParameter($calendar->getResourceId()))) |
|
| 3247 | + ->andWhere($query->expr()->eq('access', $query->createNamedParameter(self::ACCESS_PUBLIC))); |
|
| 3248 | + $query->executeStatement(); |
|
| 3249 | + |
|
| 3250 | + $this->dispatcher->dispatchTyped(new CalendarUnpublishedEvent($calendarId, $calendarData)); |
|
| 3251 | + return null; |
|
| 3252 | + }, $this->db); |
|
| 3253 | + } |
|
| 3254 | + |
|
| 3255 | + /** |
|
| 3256 | + * @param Calendar $calendar |
|
| 3257 | + * @return mixed |
|
| 3258 | + */ |
|
| 3259 | + public function getPublishStatus($calendar) { |
|
| 3260 | + $query = $this->db->getQueryBuilder(); |
|
| 3261 | + $result = $query->select('publicuri') |
|
| 3262 | + ->from('dav_shares') |
|
| 3263 | + ->where($query->expr()->eq('resourceid', $query->createNamedParameter($calendar->getResourceId()))) |
|
| 3264 | + ->andWhere($query->expr()->eq('access', $query->createNamedParameter(self::ACCESS_PUBLIC))) |
|
| 3265 | + ->executeQuery(); |
|
| 3266 | + |
|
| 3267 | + $row = $result->fetch(); |
|
| 3268 | + $result->closeCursor(); |
|
| 3269 | + return $row ? reset($row) : false; |
|
| 3270 | + } |
|
| 3271 | + |
|
| 3272 | + /** |
|
| 3273 | + * @param int $resourceId |
|
| 3274 | + * @param list<array{privilege: string, principal: string, protected: bool}> $acl |
|
| 3275 | + * @return list<array{privilege: string, principal: string, protected: bool}> |
|
| 3276 | + */ |
|
| 3277 | + public function applyShareAcl(int $resourceId, array $acl): array { |
|
| 3278 | + $shares = $this->calendarSharingBackend->getShares($resourceId); |
|
| 3279 | + return $this->calendarSharingBackend->applyShareAcl($shares, $acl); |
|
| 3280 | + } |
|
| 3281 | + |
|
| 3282 | + /** |
|
| 3283 | + * update properties table |
|
| 3284 | + * |
|
| 3285 | + * @param int $calendarId |
|
| 3286 | + * @param string $objectUri |
|
| 3287 | + * @param string $calendarData |
|
| 3288 | + * @param int $calendarType |
|
| 3289 | + */ |
|
| 3290 | + public function updateProperties($calendarId, $objectUri, $calendarData, $calendarType = self::CALENDAR_TYPE_CALENDAR) { |
|
| 3291 | + $this->cachedObjects = []; |
|
| 3292 | + $this->atomic(function () use ($calendarId, $objectUri, $calendarData, $calendarType): void { |
|
| 3293 | + $objectId = $this->getCalendarObjectId($calendarId, $objectUri, $calendarType); |
|
| 3294 | + |
|
| 3295 | + try { |
|
| 3296 | + $vCalendar = $this->readCalendarData($calendarData); |
|
| 3297 | + } catch (\Exception $ex) { |
|
| 3298 | + return; |
|
| 3299 | + } |
|
| 3300 | + |
|
| 3301 | + $this->purgeProperties($calendarId, $objectId); |
|
| 3302 | + |
|
| 3303 | + $query = $this->db->getQueryBuilder(); |
|
| 3304 | + $query->insert($this->dbObjectPropertiesTable) |
|
| 3305 | + ->values( |
|
| 3306 | + [ |
|
| 3307 | + 'calendarid' => $query->createNamedParameter($calendarId), |
|
| 3308 | + 'calendartype' => $query->createNamedParameter($calendarType), |
|
| 3309 | + 'objectid' => $query->createNamedParameter($objectId), |
|
| 3310 | + 'name' => $query->createParameter('name'), |
|
| 3311 | + 'parameter' => $query->createParameter('parameter'), |
|
| 3312 | + 'value' => $query->createParameter('value'), |
|
| 3313 | + ] |
|
| 3314 | + ); |
|
| 3315 | + |
|
| 3316 | + $indexComponents = ['VEVENT', 'VJOURNAL', 'VTODO']; |
|
| 3317 | + foreach ($vCalendar->getComponents() as $component) { |
|
| 3318 | + if (!in_array($component->name, $indexComponents)) { |
|
| 3319 | + continue; |
|
| 3320 | + } |
|
| 3321 | + |
|
| 3322 | + foreach ($component->children() as $property) { |
|
| 3323 | + if (in_array($property->name, self::INDEXED_PROPERTIES, true)) { |
|
| 3324 | + $value = $property->getValue(); |
|
| 3325 | + // is this a shitty db? |
|
| 3326 | + if (!$this->db->supports4ByteText()) { |
|
| 3327 | + $value = preg_replace('/[\x{10000}-\x{10FFFF}]/u', "\xEF\xBF\xBD", $value); |
|
| 3328 | + } |
|
| 3329 | + $value = mb_strcut($value, 0, 254); |
|
| 3330 | + |
|
| 3331 | + $query->setParameter('name', $property->name); |
|
| 3332 | + $query->setParameter('parameter', null); |
|
| 3333 | + $query->setParameter('value', mb_strcut($value, 0, 254)); |
|
| 3334 | + $query->executeStatement(); |
|
| 3335 | + } |
|
| 3336 | + |
|
| 3337 | + if (array_key_exists($property->name, self::$indexParameters)) { |
|
| 3338 | + $parameters = $property->parameters(); |
|
| 3339 | + $indexedParametersForProperty = self::$indexParameters[$property->name]; |
|
| 3340 | + |
|
| 3341 | + foreach ($parameters as $key => $value) { |
|
| 3342 | + if (in_array($key, $indexedParametersForProperty)) { |
|
| 3343 | + // is this a shitty db? |
|
| 3344 | + if ($this->db->supports4ByteText()) { |
|
| 3345 | + $value = preg_replace('/[\x{10000}-\x{10FFFF}]/u', "\xEF\xBF\xBD", $value); |
|
| 3346 | + } |
|
| 3347 | + |
|
| 3348 | + $query->setParameter('name', $property->name); |
|
| 3349 | + $query->setParameter('parameter', mb_strcut($key, 0, 254)); |
|
| 3350 | + $query->setParameter('value', mb_strcut($value, 0, 254)); |
|
| 3351 | + $query->executeStatement(); |
|
| 3352 | + } |
|
| 3353 | + } |
|
| 3354 | + } |
|
| 3355 | + } |
|
| 3356 | + } |
|
| 3357 | + }, $this->db); |
|
| 3358 | + } |
|
| 3359 | + |
|
| 3360 | + /** |
|
| 3361 | + * deletes all birthday calendars |
|
| 3362 | + */ |
|
| 3363 | + public function deleteAllBirthdayCalendars() { |
|
| 3364 | + $this->atomic(function (): void { |
|
| 3365 | + $query = $this->db->getQueryBuilder(); |
|
| 3366 | + $result = $query->select(['id'])->from('calendars') |
|
| 3367 | + ->where($query->expr()->eq('uri', $query->createNamedParameter(BirthdayService::BIRTHDAY_CALENDAR_URI))) |
|
| 3368 | + ->executeQuery(); |
|
| 3369 | + |
|
| 3370 | + while (($row = $result->fetch()) !== false) { |
|
| 3371 | + $this->deleteCalendar( |
|
| 3372 | + $row['id'], |
|
| 3373 | + true // No data to keep in the trashbin, if the user re-enables then we regenerate |
|
| 3374 | + ); |
|
| 3375 | + } |
|
| 3376 | + $result->closeCursor(); |
|
| 3377 | + }, $this->db); |
|
| 3378 | + } |
|
| 3379 | + |
|
| 3380 | + /** |
|
| 3381 | + * @param $subscriptionId |
|
| 3382 | + */ |
|
| 3383 | + public function purgeAllCachedEventsForSubscription($subscriptionId) { |
|
| 3384 | + $this->atomic(function () use ($subscriptionId): void { |
|
| 3385 | + $query = $this->db->getQueryBuilder(); |
|
| 3386 | + $query->select('uri') |
|
| 3387 | + ->from('calendarobjects') |
|
| 3388 | + ->where($query->expr()->eq('calendarid', $query->createNamedParameter($subscriptionId))) |
|
| 3389 | + ->andWhere($query->expr()->eq('calendartype', $query->createNamedParameter(self::CALENDAR_TYPE_SUBSCRIPTION))); |
|
| 3390 | + $stmt = $query->executeQuery(); |
|
| 3391 | + |
|
| 3392 | + $uris = []; |
|
| 3393 | + while (($row = $stmt->fetch()) !== false) { |
|
| 3394 | + $uris[] = $row['uri']; |
|
| 3395 | + } |
|
| 3396 | + $stmt->closeCursor(); |
|
| 3397 | + |
|
| 3398 | + $query = $this->db->getQueryBuilder(); |
|
| 3399 | + $query->delete('calendarobjects') |
|
| 3400 | + ->where($query->expr()->eq('calendarid', $query->createNamedParameter($subscriptionId))) |
|
| 3401 | + ->andWhere($query->expr()->eq('calendartype', $query->createNamedParameter(self::CALENDAR_TYPE_SUBSCRIPTION))) |
|
| 3402 | + ->executeStatement(); |
|
| 3403 | + |
|
| 3404 | + $query = $this->db->getQueryBuilder(); |
|
| 3405 | + $query->delete('calendarchanges') |
|
| 3406 | + ->where($query->expr()->eq('calendarid', $query->createNamedParameter($subscriptionId))) |
|
| 3407 | + ->andWhere($query->expr()->eq('calendartype', $query->createNamedParameter(self::CALENDAR_TYPE_SUBSCRIPTION))) |
|
| 3408 | + ->executeStatement(); |
|
| 3409 | + |
|
| 3410 | + $query = $this->db->getQueryBuilder(); |
|
| 3411 | + $query->delete($this->dbObjectPropertiesTable) |
|
| 3412 | + ->where($query->expr()->eq('calendarid', $query->createNamedParameter($subscriptionId))) |
|
| 3413 | + ->andWhere($query->expr()->eq('calendartype', $query->createNamedParameter(self::CALENDAR_TYPE_SUBSCRIPTION))) |
|
| 3414 | + ->executeStatement(); |
|
| 3415 | + |
|
| 3416 | + $this->addChanges($subscriptionId, $uris, 3, self::CALENDAR_TYPE_SUBSCRIPTION); |
|
| 3417 | + }, $this->db); |
|
| 3418 | + } |
|
| 3419 | + |
|
| 3420 | + /** |
|
| 3421 | + * @param int $subscriptionId |
|
| 3422 | + * @param array<int> $calendarObjectIds |
|
| 3423 | + * @param array<string> $calendarObjectUris |
|
| 3424 | + */ |
|
| 3425 | + public function purgeCachedEventsForSubscription(int $subscriptionId, array $calendarObjectIds, array $calendarObjectUris): void { |
|
| 3426 | + if (empty($calendarObjectUris)) { |
|
| 3427 | + return; |
|
| 3428 | + } |
|
| 3429 | + |
|
| 3430 | + $this->atomic(function () use ($subscriptionId, $calendarObjectIds, $calendarObjectUris): void { |
|
| 3431 | + foreach (array_chunk($calendarObjectIds, 1000) as $chunk) { |
|
| 3432 | + $query = $this->db->getQueryBuilder(); |
|
| 3433 | + $query->delete($this->dbObjectPropertiesTable) |
|
| 3434 | + ->where($query->expr()->eq('calendarid', $query->createNamedParameter($subscriptionId))) |
|
| 3435 | + ->andWhere($query->expr()->eq('calendartype', $query->createNamedParameter(self::CALENDAR_TYPE_SUBSCRIPTION))) |
|
| 3436 | + ->andWhere($query->expr()->in('id', $query->createNamedParameter($chunk, IQueryBuilder::PARAM_INT_ARRAY), IQueryBuilder::PARAM_INT_ARRAY)) |
|
| 3437 | + ->executeStatement(); |
|
| 3438 | + |
|
| 3439 | + $query = $this->db->getQueryBuilder(); |
|
| 3440 | + $query->delete('calendarobjects') |
|
| 3441 | + ->where($query->expr()->eq('calendarid', $query->createNamedParameter($subscriptionId))) |
|
| 3442 | + ->andWhere($query->expr()->eq('calendartype', $query->createNamedParameter(self::CALENDAR_TYPE_SUBSCRIPTION))) |
|
| 3443 | + ->andWhere($query->expr()->in('id', $query->createNamedParameter($chunk, IQueryBuilder::PARAM_INT_ARRAY), IQueryBuilder::PARAM_INT_ARRAY)) |
|
| 3444 | + ->executeStatement(); |
|
| 3445 | + } |
|
| 3446 | + |
|
| 3447 | + foreach (array_chunk($calendarObjectUris, 1000) as $chunk) { |
|
| 3448 | + $query = $this->db->getQueryBuilder(); |
|
| 3449 | + $query->delete('calendarchanges') |
|
| 3450 | + ->where($query->expr()->eq('calendarid', $query->createNamedParameter($subscriptionId))) |
|
| 3451 | + ->andWhere($query->expr()->eq('calendartype', $query->createNamedParameter(self::CALENDAR_TYPE_SUBSCRIPTION))) |
|
| 3452 | + ->andWhere($query->expr()->in('uri', $query->createNamedParameter($chunk, IQueryBuilder::PARAM_STR_ARRAY), IQueryBuilder::PARAM_STR_ARRAY)) |
|
| 3453 | + ->executeStatement(); |
|
| 3454 | + } |
|
| 3455 | + $this->addChanges($subscriptionId, $calendarObjectUris, 3, self::CALENDAR_TYPE_SUBSCRIPTION); |
|
| 3456 | + }, $this->db); |
|
| 3457 | + } |
|
| 3458 | + |
|
| 3459 | + /** |
|
| 3460 | + * Move a calendar from one user to another |
|
| 3461 | + * |
|
| 3462 | + * @param string $uriName |
|
| 3463 | + * @param string $uriOrigin |
|
| 3464 | + * @param string $uriDestination |
|
| 3465 | + * @param string $newUriName (optional) the new uriName |
|
| 3466 | + */ |
|
| 3467 | + public function moveCalendar($uriName, $uriOrigin, $uriDestination, $newUriName = null) { |
|
| 3468 | + $query = $this->db->getQueryBuilder(); |
|
| 3469 | + $query->update('calendars') |
|
| 3470 | + ->set('principaluri', $query->createNamedParameter($uriDestination)) |
|
| 3471 | + ->set('uri', $query->createNamedParameter($newUriName ?: $uriName)) |
|
| 3472 | + ->where($query->expr()->eq('principaluri', $query->createNamedParameter($uriOrigin))) |
|
| 3473 | + ->andWhere($query->expr()->eq('uri', $query->createNamedParameter($uriName))) |
|
| 3474 | + ->executeStatement(); |
|
| 3475 | + } |
|
| 3476 | + |
|
| 3477 | + /** |
|
| 3478 | + * read VCalendar data into a VCalendar object |
|
| 3479 | + * |
|
| 3480 | + * @param string $objectData |
|
| 3481 | + * @return VCalendar |
|
| 3482 | + */ |
|
| 3483 | + protected function readCalendarData($objectData) { |
|
| 3484 | + return Reader::read($objectData); |
|
| 3485 | + } |
|
| 3486 | + |
|
| 3487 | + /** |
|
| 3488 | + * delete all properties from a given calendar object |
|
| 3489 | + * |
|
| 3490 | + * @param int $calendarId |
|
| 3491 | + * @param int $objectId |
|
| 3492 | + */ |
|
| 3493 | + protected function purgeProperties($calendarId, $objectId) { |
|
| 3494 | + $this->cachedObjects = []; |
|
| 3495 | + $query = $this->db->getQueryBuilder(); |
|
| 3496 | + $query->delete($this->dbObjectPropertiesTable) |
|
| 3497 | + ->where($query->expr()->eq('objectid', $query->createNamedParameter($objectId))) |
|
| 3498 | + ->andWhere($query->expr()->eq('calendarid', $query->createNamedParameter($calendarId))); |
|
| 3499 | + $query->executeStatement(); |
|
| 3500 | + } |
|
| 3501 | + |
|
| 3502 | + /** |
|
| 3503 | + * get ID from a given calendar object |
|
| 3504 | + * |
|
| 3505 | + * @param int $calendarId |
|
| 3506 | + * @param string $uri |
|
| 3507 | + * @param int $calendarType |
|
| 3508 | + * @return int |
|
| 3509 | + */ |
|
| 3510 | + protected function getCalendarObjectId($calendarId, $uri, $calendarType):int { |
|
| 3511 | + $query = $this->db->getQueryBuilder(); |
|
| 3512 | + $query->select('id') |
|
| 3513 | + ->from('calendarobjects') |
|
| 3514 | + ->where($query->expr()->eq('uri', $query->createNamedParameter($uri))) |
|
| 3515 | + ->andWhere($query->expr()->eq('calendarid', $query->createNamedParameter($calendarId))) |
|
| 3516 | + ->andWhere($query->expr()->eq('calendartype', $query->createNamedParameter($calendarType))); |
|
| 3517 | + |
|
| 3518 | + $result = $query->executeQuery(); |
|
| 3519 | + $objectIds = $result->fetch(); |
|
| 3520 | + $result->closeCursor(); |
|
| 3521 | + |
|
| 3522 | + if (!isset($objectIds['id'])) { |
|
| 3523 | + throw new \InvalidArgumentException('Calendarobject does not exists: ' . $uri); |
|
| 3524 | + } |
|
| 3525 | + |
|
| 3526 | + return (int)$objectIds['id']; |
|
| 3527 | + } |
|
| 3528 | + |
|
| 3529 | + /** |
|
| 3530 | + * @throws \InvalidArgumentException |
|
| 3531 | + */ |
|
| 3532 | + public function pruneOutdatedSyncTokens(int $keep, int $retention): int { |
|
| 3533 | + if ($keep < 0) { |
|
| 3534 | + throw new \InvalidArgumentException(); |
|
| 3535 | + } |
|
| 3536 | + |
|
| 3537 | + $query = $this->db->getQueryBuilder(); |
|
| 3538 | + $query->select($query->func()->max('id')) |
|
| 3539 | + ->from('calendarchanges'); |
|
| 3540 | + |
|
| 3541 | + $result = $query->executeQuery(); |
|
| 3542 | + $maxId = (int)$result->fetchOne(); |
|
| 3543 | + $result->closeCursor(); |
|
| 3544 | + if (!$maxId || $maxId < $keep) { |
|
| 3545 | + return 0; |
|
| 3546 | + } |
|
| 3547 | + |
|
| 3548 | + $query = $this->db->getQueryBuilder(); |
|
| 3549 | + $query->delete('calendarchanges') |
|
| 3550 | + ->where( |
|
| 3551 | + $query->expr()->lte('id', $query->createNamedParameter($maxId - $keep, IQueryBuilder::PARAM_INT), IQueryBuilder::PARAM_INT), |
|
| 3552 | + $query->expr()->lte('created_at', $query->createNamedParameter($retention)), |
|
| 3553 | + ); |
|
| 3554 | + return $query->executeStatement(); |
|
| 3555 | + } |
|
| 3556 | + |
|
| 3557 | + /** |
|
| 3558 | + * return legacy endpoint principal name to new principal name |
|
| 3559 | + * |
|
| 3560 | + * @param $principalUri |
|
| 3561 | + * @param $toV2 |
|
| 3562 | + * @return string |
|
| 3563 | + */ |
|
| 3564 | + private function convertPrincipal($principalUri, $toV2) { |
|
| 3565 | + if ($this->principalBackend->getPrincipalPrefix() === 'principals') { |
|
| 3566 | + [, $name] = Uri\split($principalUri); |
|
| 3567 | + if ($toV2 === true) { |
|
| 3568 | + return "principals/users/$name"; |
|
| 3569 | + } |
|
| 3570 | + return "principals/$name"; |
|
| 3571 | + } |
|
| 3572 | + return $principalUri; |
|
| 3573 | + } |
|
| 3574 | + |
|
| 3575 | + /** |
|
| 3576 | + * adds information about an owner to the calendar data |
|
| 3577 | + * |
|
| 3578 | + */ |
|
| 3579 | + private function addOwnerPrincipalToCalendar(array $calendarInfo): array { |
|
| 3580 | + $ownerPrincipalKey = '{' . \OCA\DAV\DAV\Sharing\Plugin::NS_OWNCLOUD . '}owner-principal'; |
|
| 3581 | + $displaynameKey = '{' . \OCA\DAV\DAV\Sharing\Plugin::NS_NEXTCLOUD . '}owner-displayname'; |
|
| 3582 | + if (isset($calendarInfo[$ownerPrincipalKey])) { |
|
| 3583 | + $uri = $calendarInfo[$ownerPrincipalKey]; |
|
| 3584 | + } else { |
|
| 3585 | + $uri = $calendarInfo['principaluri']; |
|
| 3586 | + } |
|
| 3587 | + |
|
| 3588 | + $principalInformation = $this->principalBackend->getPrincipalByPath($uri); |
|
| 3589 | + if (isset($principalInformation['{DAV:}displayname'])) { |
|
| 3590 | + $calendarInfo[$displaynameKey] = $principalInformation['{DAV:}displayname']; |
|
| 3591 | + } |
|
| 3592 | + return $calendarInfo; |
|
| 3593 | + } |
|
| 3594 | + |
|
| 3595 | + private function addResourceTypeToCalendar(array $row, array $calendar): array { |
|
| 3596 | + if (isset($row['deleted_at'])) { |
|
| 3597 | + // Columns is set and not null -> this is a deleted calendar |
|
| 3598 | + // we send a custom resourcetype to hide the deleted calendar |
|
| 3599 | + // from ordinary DAV clients, but the Calendar app will know |
|
| 3600 | + // how to handle this special resource. |
|
| 3601 | + $calendar['{DAV:}resourcetype'] = new DAV\Xml\Property\ResourceType([ |
|
| 3602 | + '{DAV:}collection', |
|
| 3603 | + sprintf('{%s}deleted-calendar', \OCA\DAV\DAV\Sharing\Plugin::NS_NEXTCLOUD), |
|
| 3604 | + ]); |
|
| 3605 | + } |
|
| 3606 | + return $calendar; |
|
| 3607 | + } |
|
| 3608 | + |
|
| 3609 | + /** |
|
| 3610 | + * Amend the calendar info with database row data |
|
| 3611 | + * |
|
| 3612 | + * @param array $row |
|
| 3613 | + * @param array $calendar |
|
| 3614 | + * |
|
| 3615 | + * @return array |
|
| 3616 | + */ |
|
| 3617 | + private function rowToCalendar($row, array $calendar): array { |
|
| 3618 | + foreach ($this->propertyMap as $xmlName => [$dbName, $type]) { |
|
| 3619 | + $value = $row[$dbName]; |
|
| 3620 | + if ($value !== null) { |
|
| 3621 | + settype($value, $type); |
|
| 3622 | + } |
|
| 3623 | + $calendar[$xmlName] = $value; |
|
| 3624 | + } |
|
| 3625 | + return $calendar; |
|
| 3626 | + } |
|
| 3627 | + |
|
| 3628 | + /** |
|
| 3629 | + * Amend the subscription info with database row data |
|
| 3630 | + * |
|
| 3631 | + * @param array $row |
|
| 3632 | + * @param array $subscription |
|
| 3633 | + * |
|
| 3634 | + * @return array |
|
| 3635 | + */ |
|
| 3636 | + private function rowToSubscription($row, array $subscription): array { |
|
| 3637 | + foreach ($this->subscriptionPropertyMap as $xmlName => [$dbName, $type]) { |
|
| 3638 | + $value = $row[$dbName]; |
|
| 3639 | + if ($value !== null) { |
|
| 3640 | + settype($value, $type); |
|
| 3641 | + } |
|
| 3642 | + $subscription[$xmlName] = $value; |
|
| 3643 | + } |
|
| 3644 | + return $subscription; |
|
| 3645 | + } |
|
| 3646 | + |
|
| 3647 | + /** |
|
| 3648 | + * delete all invitations from a given calendar |
|
| 3649 | + * |
|
| 3650 | + * @since 31.0.0 |
|
| 3651 | + * |
|
| 3652 | + * @param int $calendarId |
|
| 3653 | + * |
|
| 3654 | + * @return void |
|
| 3655 | + */ |
|
| 3656 | + protected function purgeCalendarInvitations(int $calendarId): void { |
|
| 3657 | + // select all calendar object uid's |
|
| 3658 | + $cmd = $this->db->getQueryBuilder(); |
|
| 3659 | + $cmd->select('uid') |
|
| 3660 | + ->from($this->dbObjectsTable) |
|
| 3661 | + ->where($cmd->expr()->eq('calendarid', $cmd->createNamedParameter($calendarId))); |
|
| 3662 | + $allIds = $cmd->executeQuery()->fetchAll(\PDO::FETCH_COLUMN); |
|
| 3663 | + // delete all links that match object uid's |
|
| 3664 | + $cmd = $this->db->getQueryBuilder(); |
|
| 3665 | + $cmd->delete($this->dbObjectInvitationsTable) |
|
| 3666 | + ->where($cmd->expr()->in('uid', $cmd->createParameter('uids'), IQueryBuilder::PARAM_STR_ARRAY)); |
|
| 3667 | + foreach (array_chunk($allIds, 1000) as $chunkIds) { |
|
| 3668 | + $cmd->setParameter('uids', $chunkIds, IQueryBuilder::PARAM_STR_ARRAY); |
|
| 3669 | + $cmd->executeStatement(); |
|
| 3670 | + } |
|
| 3671 | + } |
|
| 3672 | + |
|
| 3673 | + /** |
|
| 3674 | + * Delete all invitations from a given calendar event |
|
| 3675 | + * |
|
| 3676 | + * @since 31.0.0 |
|
| 3677 | + * |
|
| 3678 | + * @param string $eventId UID of the event |
|
| 3679 | + * |
|
| 3680 | + * @return void |
|
| 3681 | + */ |
|
| 3682 | + protected function purgeObjectInvitations(string $eventId): void { |
|
| 3683 | + $cmd = $this->db->getQueryBuilder(); |
|
| 3684 | + $cmd->delete($this->dbObjectInvitationsTable) |
|
| 3685 | + ->where($cmd->expr()->eq('uid', $cmd->createNamedParameter($eventId, IQueryBuilder::PARAM_STR), IQueryBuilder::PARAM_STR)); |
|
| 3686 | + $cmd->executeStatement(); |
|
| 3687 | + } |
|
| 3688 | + |
|
| 3689 | + public function unshare(IShareable $shareable, string $principal): void { |
|
| 3690 | + $this->atomic(function () use ($shareable, $principal): void { |
|
| 3691 | + $calendarData = $this->getCalendarById($shareable->getResourceId()); |
|
| 3692 | + if ($calendarData === null) { |
|
| 3693 | + throw new \RuntimeException('Trying to update shares for non-existing calendar: ' . $shareable->getResourceId()); |
|
| 3694 | + } |
|
| 3695 | + |
|
| 3696 | + $oldShares = $this->getShares($shareable->getResourceId()); |
|
| 3697 | + $unshare = $this->calendarSharingBackend->unshare($shareable, $principal); |
|
| 3698 | + |
|
| 3699 | + if ($unshare) { |
|
| 3700 | + $this->dispatcher->dispatchTyped(new CalendarShareUpdatedEvent( |
|
| 3701 | + $shareable->getResourceId(), |
|
| 3702 | + $calendarData, |
|
| 3703 | + $oldShares, |
|
| 3704 | + [], |
|
| 3705 | + [$principal] |
|
| 3706 | + )); |
|
| 3707 | + } |
|
| 3708 | + }, $this->db); |
|
| 3709 | + } |
|
| 3710 | 3710 | } |
@@ -144,7 +144,7 @@ discard block |
||
| 144 | 144 | '{urn:ietf:params:xml:ns:caldav}calendar-timezone' => ['timezone', 'string'], |
| 145 | 145 | '{http://apple.com/ns/ical/}calendar-order' => ['calendarorder', 'int'], |
| 146 | 146 | '{http://apple.com/ns/ical/}calendar-color' => ['calendarcolor', 'string'], |
| 147 | - '{' . \OCA\DAV\DAV\Sharing\Plugin::NS_NEXTCLOUD . '}deleted-at' => ['deleted_at', 'int'], |
|
| 147 | + '{'.\OCA\DAV\DAV\Sharing\Plugin::NS_NEXTCLOUD.'}deleted-at' => ['deleted_at', 'int'], |
|
| 148 | 148 | ]; |
| 149 | 149 | |
| 150 | 150 | /** |
@@ -237,7 +237,7 @@ discard block |
||
| 237 | 237 | } |
| 238 | 238 | |
| 239 | 239 | $result = $query->executeQuery(); |
| 240 | - $column = (int)$result->fetchOne(); |
|
| 240 | + $column = (int) $result->fetchOne(); |
|
| 241 | 241 | $result->closeCursor(); |
| 242 | 242 | return $column; |
| 243 | 243 | } |
@@ -258,7 +258,7 @@ discard block |
||
| 258 | 258 | } |
| 259 | 259 | |
| 260 | 260 | $result = $query->executeQuery(); |
| 261 | - $column = (int)$result->fetchOne(); |
|
| 261 | + $column = (int) $result->fetchOne(); |
|
| 262 | 262 | $result->closeCursor(); |
| 263 | 263 | return $column; |
| 264 | 264 | } |
@@ -276,8 +276,8 @@ discard block |
||
| 276 | 276 | $calendars = []; |
| 277 | 277 | while (($row = $result->fetch()) !== false) { |
| 278 | 278 | $calendars[] = [ |
| 279 | - 'id' => (int)$row['id'], |
|
| 280 | - 'deleted_at' => (int)$row['deleted_at'], |
|
| 279 | + 'id' => (int) $row['id'], |
|
| 280 | + 'deleted_at' => (int) $row['deleted_at'], |
|
| 281 | 281 | ]; |
| 282 | 282 | } |
| 283 | 283 | $result->closeCursor(); |
@@ -310,7 +310,7 @@ discard block |
||
| 310 | 310 | * @return array |
| 311 | 311 | */ |
| 312 | 312 | public function getCalendarsForUser($principalUri) { |
| 313 | - return $this->atomic(function () use ($principalUri) { |
|
| 313 | + return $this->atomic(function() use ($principalUri) { |
|
| 314 | 314 | $principalUriOriginal = $principalUri; |
| 315 | 315 | $principalUri = $this->convertPrincipal($principalUri, true); |
| 316 | 316 | $fields = array_column($this->propertyMap, 0); |
@@ -337,7 +337,7 @@ discard block |
||
| 337 | 337 | |
| 338 | 338 | $calendars = []; |
| 339 | 339 | while ($row = $result->fetch()) { |
| 340 | - $row['principaluri'] = (string)$row['principaluri']; |
|
| 340 | + $row['principaluri'] = (string) $row['principaluri']; |
|
| 341 | 341 | $components = []; |
| 342 | 342 | if ($row['components']) { |
| 343 | 343 | $components = explode(',', $row['components']); |
@@ -347,11 +347,11 @@ discard block |
||
| 347 | 347 | 'id' => $row['id'], |
| 348 | 348 | 'uri' => $row['uri'], |
| 349 | 349 | 'principaluri' => $this->convertPrincipal($row['principaluri'], !$this->legacyEndpoint), |
| 350 | - '{' . Plugin::NS_CALENDARSERVER . '}getctag' => 'http://sabre.io/ns/sync/' . ($row['synctoken'] ?: '0'), |
|
| 350 | + '{'.Plugin::NS_CALENDARSERVER.'}getctag' => 'http://sabre.io/ns/sync/'.($row['synctoken'] ?: '0'), |
|
| 351 | 351 | '{http://sabredav.org/ns}sync-token' => $row['synctoken'] ?: '0', |
| 352 | - '{' . Plugin::NS_CALDAV . '}supported-calendar-component-set' => new SupportedCalendarComponentSet($components), |
|
| 353 | - '{' . Plugin::NS_CALDAV . '}schedule-calendar-transp' => new ScheduleCalendarTransp($row['transparent']?'transparent':'opaque'), |
|
| 354 | - '{' . \OCA\DAV\DAV\Sharing\Plugin::NS_OWNCLOUD . '}owner-principal' => $this->convertPrincipal($principalUri, !$this->legacyEndpoint), |
|
| 352 | + '{'.Plugin::NS_CALDAV.'}supported-calendar-component-set' => new SupportedCalendarComponentSet($components), |
|
| 353 | + '{'.Plugin::NS_CALDAV.'}schedule-calendar-transp' => new ScheduleCalendarTransp($row['transparent'] ? 'transparent' : 'opaque'), |
|
| 354 | + '{'.\OCA\DAV\DAV\Sharing\Plugin::NS_OWNCLOUD.'}owner-principal' => $this->convertPrincipal($principalUri, !$this->legacyEndpoint), |
|
| 355 | 355 | ]; |
| 356 | 356 | |
| 357 | 357 | $calendar = $this->rowToCalendar($row, $calendar); |
@@ -370,8 +370,8 @@ discard block |
||
| 370 | 370 | $principals[] = $principalUri; |
| 371 | 371 | |
| 372 | 372 | $fields = array_column($this->propertyMap, 0); |
| 373 | - $fields = array_map(function (string $field) { |
|
| 374 | - return 'a.' . $field; |
|
| 373 | + $fields = array_map(function(string $field) { |
|
| 374 | + return 'a.'.$field; |
|
| 375 | 375 | }, $fields); |
| 376 | 376 | $fields[] = 'a.id'; |
| 377 | 377 | $fields[] = 'a.uri'; |
@@ -398,14 +398,14 @@ discard block |
||
| 398 | 398 | |
| 399 | 399 | $results = $select->executeQuery(); |
| 400 | 400 | |
| 401 | - $readOnlyPropertyName = '{' . \OCA\DAV\DAV\Sharing\Plugin::NS_OWNCLOUD . '}read-only'; |
|
| 401 | + $readOnlyPropertyName = '{'.\OCA\DAV\DAV\Sharing\Plugin::NS_OWNCLOUD.'}read-only'; |
|
| 402 | 402 | while ($row = $results->fetch()) { |
| 403 | - $row['principaluri'] = (string)$row['principaluri']; |
|
| 403 | + $row['principaluri'] = (string) $row['principaluri']; |
|
| 404 | 404 | if ($row['principaluri'] === $principalUri) { |
| 405 | 405 | continue; |
| 406 | 406 | } |
| 407 | 407 | |
| 408 | - $readOnly = (int)$row['access'] === Backend::ACCESS_READ; |
|
| 408 | + $readOnly = (int) $row['access'] === Backend::ACCESS_READ; |
|
| 409 | 409 | if (isset($calendars[$row['id']])) { |
| 410 | 410 | if ($readOnly) { |
| 411 | 411 | // New share can not have more permissions than the old one. |
@@ -419,8 +419,8 @@ discard block |
||
| 419 | 419 | } |
| 420 | 420 | |
| 421 | 421 | [, $name] = Uri\split($row['principaluri']); |
| 422 | - $uri = $row['uri'] . '_shared_by_' . $name; |
|
| 423 | - $row['displayname'] = $row['displayname'] . ' (' . ($this->userManager->getDisplayName($name) ?? ($name ?? '')) . ')'; |
|
| 422 | + $uri = $row['uri'].'_shared_by_'.$name; |
|
| 423 | + $row['displayname'] = $row['displayname'].' ('.($this->userManager->getDisplayName($name) ?? ($name ?? '')).')'; |
|
| 424 | 424 | $components = []; |
| 425 | 425 | if ($row['components']) { |
| 426 | 426 | $components = explode(',', $row['components']); |
@@ -429,11 +429,11 @@ discard block |
||
| 429 | 429 | 'id' => $row['id'], |
| 430 | 430 | 'uri' => $uri, |
| 431 | 431 | 'principaluri' => $this->convertPrincipal($principalUri, !$this->legacyEndpoint), |
| 432 | - '{' . Plugin::NS_CALENDARSERVER . '}getctag' => 'http://sabre.io/ns/sync/' . ($row['synctoken'] ?: '0'), |
|
| 432 | + '{'.Plugin::NS_CALENDARSERVER.'}getctag' => 'http://sabre.io/ns/sync/'.($row['synctoken'] ?: '0'), |
|
| 433 | 433 | '{http://sabredav.org/ns}sync-token' => $row['synctoken'] ?: '0', |
| 434 | - '{' . Plugin::NS_CALDAV . '}supported-calendar-component-set' => new SupportedCalendarComponentSet($components), |
|
| 435 | - '{' . Plugin::NS_CALDAV . '}schedule-calendar-transp' => new ScheduleCalendarTransp('transparent'), |
|
| 436 | - '{' . \OCA\DAV\DAV\Sharing\Plugin::NS_OWNCLOUD . '}owner-principal' => $this->convertPrincipal($row['principaluri'], !$this->legacyEndpoint), |
|
| 434 | + '{'.Plugin::NS_CALDAV.'}supported-calendar-component-set' => new SupportedCalendarComponentSet($components), |
|
| 435 | + '{'.Plugin::NS_CALDAV.'}schedule-calendar-transp' => new ScheduleCalendarTransp('transparent'), |
|
| 436 | + '{'.\OCA\DAV\DAV\Sharing\Plugin::NS_OWNCLOUD.'}owner-principal' => $this->convertPrincipal($row['principaluri'], !$this->legacyEndpoint), |
|
| 437 | 437 | $readOnlyPropertyName => $readOnly, |
| 438 | 438 | ]; |
| 439 | 439 | |
@@ -470,7 +470,7 @@ discard block |
||
| 470 | 470 | $stmt = $query->executeQuery(); |
| 471 | 471 | $calendars = []; |
| 472 | 472 | while ($row = $stmt->fetch()) { |
| 473 | - $row['principaluri'] = (string)$row['principaluri']; |
|
| 473 | + $row['principaluri'] = (string) $row['principaluri']; |
|
| 474 | 474 | $components = []; |
| 475 | 475 | if ($row['components']) { |
| 476 | 476 | $components = explode(',', $row['components']); |
@@ -479,10 +479,10 @@ discard block |
||
| 479 | 479 | 'id' => $row['id'], |
| 480 | 480 | 'uri' => $row['uri'], |
| 481 | 481 | 'principaluri' => $this->convertPrincipal($row['principaluri'], !$this->legacyEndpoint), |
| 482 | - '{' . Plugin::NS_CALENDARSERVER . '}getctag' => 'http://sabre.io/ns/sync/' . ($row['synctoken'] ?: '0'), |
|
| 482 | + '{'.Plugin::NS_CALENDARSERVER.'}getctag' => 'http://sabre.io/ns/sync/'.($row['synctoken'] ?: '0'), |
|
| 483 | 483 | '{http://sabredav.org/ns}sync-token' => $row['synctoken'] ?: '0', |
| 484 | - '{' . Plugin::NS_CALDAV . '}supported-calendar-component-set' => new SupportedCalendarComponentSet($components), |
|
| 485 | - '{' . Plugin::NS_CALDAV . '}schedule-calendar-transp' => new ScheduleCalendarTransp($row['transparent']?'transparent':'opaque'), |
|
| 484 | + '{'.Plugin::NS_CALDAV.'}supported-calendar-component-set' => new SupportedCalendarComponentSet($components), |
|
| 485 | + '{'.Plugin::NS_CALDAV.'}schedule-calendar-transp' => new ScheduleCalendarTransp($row['transparent'] ? 'transparent' : 'opaque'), |
|
| 486 | 486 | ]; |
| 487 | 487 | |
| 488 | 488 | $calendar = $this->rowToCalendar($row, $calendar); |
@@ -520,9 +520,9 @@ discard block |
||
| 520 | 520 | ->executeQuery(); |
| 521 | 521 | |
| 522 | 522 | while ($row = $result->fetch()) { |
| 523 | - $row['principaluri'] = (string)$row['principaluri']; |
|
| 523 | + $row['principaluri'] = (string) $row['principaluri']; |
|
| 524 | 524 | [, $name] = Uri\split($row['principaluri']); |
| 525 | - $row['displayname'] = $row['displayname'] . "($name)"; |
|
| 525 | + $row['displayname'] = $row['displayname']."($name)"; |
|
| 526 | 526 | $components = []; |
| 527 | 527 | if ($row['components']) { |
| 528 | 528 | $components = explode(',', $row['components']); |
@@ -531,13 +531,13 @@ discard block |
||
| 531 | 531 | 'id' => $row['id'], |
| 532 | 532 | 'uri' => $row['publicuri'], |
| 533 | 533 | 'principaluri' => $this->convertPrincipal($row['principaluri'], !$this->legacyEndpoint), |
| 534 | - '{' . Plugin::NS_CALENDARSERVER . '}getctag' => 'http://sabre.io/ns/sync/' . ($row['synctoken'] ?: '0'), |
|
| 534 | + '{'.Plugin::NS_CALENDARSERVER.'}getctag' => 'http://sabre.io/ns/sync/'.($row['synctoken'] ?: '0'), |
|
| 535 | 535 | '{http://sabredav.org/ns}sync-token' => $row['synctoken'] ?: '0', |
| 536 | - '{' . Plugin::NS_CALDAV . '}supported-calendar-component-set' => new SupportedCalendarComponentSet($components), |
|
| 537 | - '{' . Plugin::NS_CALDAV . '}schedule-calendar-transp' => new ScheduleCalendarTransp($row['transparent']?'transparent':'opaque'), |
|
| 538 | - '{' . \OCA\DAV\DAV\Sharing\Plugin::NS_OWNCLOUD . '}owner-principal' => $this->convertPrincipal($row['principaluri'], $this->legacyEndpoint), |
|
| 539 | - '{' . \OCA\DAV\DAV\Sharing\Plugin::NS_OWNCLOUD . '}read-only' => (int)$row['access'] === Backend::ACCESS_READ, |
|
| 540 | - '{' . \OCA\DAV\DAV\Sharing\Plugin::NS_OWNCLOUD . '}public' => (int)$row['access'] === self::ACCESS_PUBLIC, |
|
| 536 | + '{'.Plugin::NS_CALDAV.'}supported-calendar-component-set' => new SupportedCalendarComponentSet($components), |
|
| 537 | + '{'.Plugin::NS_CALDAV.'}schedule-calendar-transp' => new ScheduleCalendarTransp($row['transparent'] ? 'transparent' : 'opaque'), |
|
| 538 | + '{'.\OCA\DAV\DAV\Sharing\Plugin::NS_OWNCLOUD.'}owner-principal' => $this->convertPrincipal($row['principaluri'], $this->legacyEndpoint), |
|
| 539 | + '{'.\OCA\DAV\DAV\Sharing\Plugin::NS_OWNCLOUD.'}read-only' => (int) $row['access'] === Backend::ACCESS_READ, |
|
| 540 | + '{'.\OCA\DAV\DAV\Sharing\Plugin::NS_OWNCLOUD.'}public' => (int) $row['access'] === self::ACCESS_PUBLIC, |
|
| 541 | 541 | ]; |
| 542 | 542 | |
| 543 | 543 | $calendar = $this->rowToCalendar($row, $calendar); |
@@ -582,12 +582,12 @@ discard block |
||
| 582 | 582 | $result->closeCursor(); |
| 583 | 583 | |
| 584 | 584 | if ($row === false) { |
| 585 | - throw new NotFound('Node with name \'' . $uri . '\' could not be found'); |
|
| 585 | + throw new NotFound('Node with name \''.$uri.'\' could not be found'); |
|
| 586 | 586 | } |
| 587 | 587 | |
| 588 | - $row['principaluri'] = (string)$row['principaluri']; |
|
| 588 | + $row['principaluri'] = (string) $row['principaluri']; |
|
| 589 | 589 | [, $name] = Uri\split($row['principaluri']); |
| 590 | - $row['displayname'] = $row['displayname'] . ' ' . "($name)"; |
|
| 590 | + $row['displayname'] = $row['displayname'].' '."($name)"; |
|
| 591 | 591 | $components = []; |
| 592 | 592 | if ($row['components']) { |
| 593 | 593 | $components = explode(',', $row['components']); |
@@ -596,13 +596,13 @@ discard block |
||
| 596 | 596 | 'id' => $row['id'], |
| 597 | 597 | 'uri' => $row['publicuri'], |
| 598 | 598 | 'principaluri' => $this->convertPrincipal($row['principaluri'], !$this->legacyEndpoint), |
| 599 | - '{' . Plugin::NS_CALENDARSERVER . '}getctag' => 'http://sabre.io/ns/sync/' . ($row['synctoken'] ?: '0'), |
|
| 599 | + '{'.Plugin::NS_CALENDARSERVER.'}getctag' => 'http://sabre.io/ns/sync/'.($row['synctoken'] ?: '0'), |
|
| 600 | 600 | '{http://sabredav.org/ns}sync-token' => $row['synctoken'] ?: '0', |
| 601 | - '{' . Plugin::NS_CALDAV . '}supported-calendar-component-set' => new SupportedCalendarComponentSet($components), |
|
| 602 | - '{' . Plugin::NS_CALDAV . '}schedule-calendar-transp' => new ScheduleCalendarTransp($row['transparent']?'transparent':'opaque'), |
|
| 603 | - '{' . \OCA\DAV\DAV\Sharing\Plugin::NS_OWNCLOUD . '}owner-principal' => $this->convertPrincipal($row['principaluri'], !$this->legacyEndpoint), |
|
| 604 | - '{' . \OCA\DAV\DAV\Sharing\Plugin::NS_OWNCLOUD . '}read-only' => (int)$row['access'] === Backend::ACCESS_READ, |
|
| 605 | - '{' . \OCA\DAV\DAV\Sharing\Plugin::NS_OWNCLOUD . '}public' => (int)$row['access'] === self::ACCESS_PUBLIC, |
|
| 601 | + '{'.Plugin::NS_CALDAV.'}supported-calendar-component-set' => new SupportedCalendarComponentSet($components), |
|
| 602 | + '{'.Plugin::NS_CALDAV.'}schedule-calendar-transp' => new ScheduleCalendarTransp($row['transparent'] ? 'transparent' : 'opaque'), |
|
| 603 | + '{'.\OCA\DAV\DAV\Sharing\Plugin::NS_OWNCLOUD.'}owner-principal' => $this->convertPrincipal($row['principaluri'], !$this->legacyEndpoint), |
|
| 604 | + '{'.\OCA\DAV\DAV\Sharing\Plugin::NS_OWNCLOUD.'}read-only' => (int) $row['access'] === Backend::ACCESS_READ, |
|
| 605 | + '{'.\OCA\DAV\DAV\Sharing\Plugin::NS_OWNCLOUD.'}public' => (int) $row['access'] === self::ACCESS_PUBLIC, |
|
| 606 | 606 | ]; |
| 607 | 607 | |
| 608 | 608 | $calendar = $this->rowToCalendar($row, $calendar); |
@@ -640,7 +640,7 @@ discard block |
||
| 640 | 640 | return null; |
| 641 | 641 | } |
| 642 | 642 | |
| 643 | - $row['principaluri'] = (string)$row['principaluri']; |
|
| 643 | + $row['principaluri'] = (string) $row['principaluri']; |
|
| 644 | 644 | $components = []; |
| 645 | 645 | if ($row['components']) { |
| 646 | 646 | $components = explode(',', $row['components']); |
@@ -650,10 +650,10 @@ discard block |
||
| 650 | 650 | 'id' => $row['id'], |
| 651 | 651 | 'uri' => $row['uri'], |
| 652 | 652 | 'principaluri' => $this->convertPrincipal($row['principaluri'], !$this->legacyEndpoint), |
| 653 | - '{' . Plugin::NS_CALENDARSERVER . '}getctag' => 'http://sabre.io/ns/sync/' . ($row['synctoken'] ?: '0'), |
|
| 653 | + '{'.Plugin::NS_CALENDARSERVER.'}getctag' => 'http://sabre.io/ns/sync/'.($row['synctoken'] ?: '0'), |
|
| 654 | 654 | '{http://sabredav.org/ns}sync-token' => $row['synctoken'] ?: '0', |
| 655 | - '{' . Plugin::NS_CALDAV . '}supported-calendar-component-set' => new SupportedCalendarComponentSet($components), |
|
| 656 | - '{' . Plugin::NS_CALDAV . '}schedule-calendar-transp' => new ScheduleCalendarTransp($row['transparent']?'transparent':'opaque'), |
|
| 655 | + '{'.Plugin::NS_CALDAV.'}supported-calendar-component-set' => new SupportedCalendarComponentSet($components), |
|
| 656 | + '{'.Plugin::NS_CALDAV.'}schedule-calendar-transp' => new ScheduleCalendarTransp($row['transparent'] ? 'transparent' : 'opaque'), |
|
| 657 | 657 | ]; |
| 658 | 658 | |
| 659 | 659 | $calendar = $this->rowToCalendar($row, $calendar); |
@@ -689,7 +689,7 @@ discard block |
||
| 689 | 689 | return null; |
| 690 | 690 | } |
| 691 | 691 | |
| 692 | - $row['principaluri'] = (string)$row['principaluri']; |
|
| 692 | + $row['principaluri'] = (string) $row['principaluri']; |
|
| 693 | 693 | $components = []; |
| 694 | 694 | if ($row['components']) { |
| 695 | 695 | $components = explode(',', $row['components']); |
@@ -699,10 +699,10 @@ discard block |
||
| 699 | 699 | 'id' => $row['id'], |
| 700 | 700 | 'uri' => $row['uri'], |
| 701 | 701 | 'principaluri' => $this->convertPrincipal($row['principaluri'], !$this->legacyEndpoint), |
| 702 | - '{' . Plugin::NS_CALENDARSERVER . '}getctag' => 'http://sabre.io/ns/sync/' . ($row['synctoken'] ?: '0'), |
|
| 702 | + '{'.Plugin::NS_CALENDARSERVER.'}getctag' => 'http://sabre.io/ns/sync/'.($row['synctoken'] ?: '0'), |
|
| 703 | 703 | '{http://sabredav.org/ns}sync-token' => $row['synctoken'] ?? 0, |
| 704 | - '{' . Plugin::NS_CALDAV . '}supported-calendar-component-set' => new SupportedCalendarComponentSet($components), |
|
| 705 | - '{' . Plugin::NS_CALDAV . '}schedule-calendar-transp' => new ScheduleCalendarTransp($row['transparent']?'transparent':'opaque'), |
|
| 704 | + '{'.Plugin::NS_CALDAV.'}supported-calendar-component-set' => new SupportedCalendarComponentSet($components), |
|
| 705 | + '{'.Plugin::NS_CALDAV.'}schedule-calendar-transp' => new ScheduleCalendarTransp($row['transparent'] ? 'transparent' : 'opaque'), |
|
| 706 | 706 | ]; |
| 707 | 707 | |
| 708 | 708 | $calendar = $this->rowToCalendar($row, $calendar); |
@@ -737,14 +737,14 @@ discard block |
||
| 737 | 737 | return null; |
| 738 | 738 | } |
| 739 | 739 | |
| 740 | - $row['principaluri'] = (string)$row['principaluri']; |
|
| 740 | + $row['principaluri'] = (string) $row['principaluri']; |
|
| 741 | 741 | $subscription = [ |
| 742 | 742 | 'id' => $row['id'], |
| 743 | 743 | 'uri' => $row['uri'], |
| 744 | 744 | 'principaluri' => $row['principaluri'], |
| 745 | 745 | 'source' => $row['source'], |
| 746 | 746 | 'lastmodified' => $row['lastmodified'], |
| 747 | - '{' . Plugin::NS_CALDAV . '}supported-calendar-component-set' => new SupportedCalendarComponentSet(['VTODO', 'VEVENT']), |
|
| 747 | + '{'.Plugin::NS_CALDAV.'}supported-calendar-component-set' => new SupportedCalendarComponentSet(['VTODO', 'VEVENT']), |
|
| 748 | 748 | '{http://sabredav.org/ns}sync-token' => $row['synctoken'] ?: '0', |
| 749 | 749 | ]; |
| 750 | 750 | |
@@ -774,14 +774,14 @@ discard block |
||
| 774 | 774 | return null; |
| 775 | 775 | } |
| 776 | 776 | |
| 777 | - $row['principaluri'] = (string)$row['principaluri']; |
|
| 777 | + $row['principaluri'] = (string) $row['principaluri']; |
|
| 778 | 778 | $subscription = [ |
| 779 | 779 | 'id' => $row['id'], |
| 780 | 780 | 'uri' => $row['uri'], |
| 781 | 781 | 'principaluri' => $row['principaluri'], |
| 782 | 782 | 'source' => $row['source'], |
| 783 | 783 | 'lastmodified' => $row['lastmodified'], |
| 784 | - '{' . Plugin::NS_CALDAV . '}supported-calendar-component-set' => new SupportedCalendarComponentSet(['VTODO', 'VEVENT']), |
|
| 784 | + '{'.Plugin::NS_CALDAV.'}supported-calendar-component-set' => new SupportedCalendarComponentSet(['VTODO', 'VEVENT']), |
|
| 785 | 785 | '{http://sabredav.org/ns}sync-token' => $row['synctoken'] ?: '0', |
| 786 | 786 | ]; |
| 787 | 787 | |
@@ -819,7 +819,7 @@ discard block |
||
| 819 | 819 | $sccs = '{urn:ietf:params:xml:ns:caldav}supported-calendar-component-set'; |
| 820 | 820 | if (isset($properties[$sccs])) { |
| 821 | 821 | if (!($properties[$sccs] instanceof SupportedCalendarComponentSet)) { |
| 822 | - throw new DAV\Exception('The ' . $sccs . ' property must be of type: \Sabre\CalDAV\Property\SupportedCalendarComponentSet'); |
|
| 822 | + throw new DAV\Exception('The '.$sccs.' property must be of type: \Sabre\CalDAV\Property\SupportedCalendarComponentSet'); |
|
| 823 | 823 | } |
| 824 | 824 | $values['components'] = implode(',', $properties[$sccs]->getValue()); |
| 825 | 825 | } elseif (isset($properties['components'])) { |
@@ -828,9 +828,9 @@ discard block |
||
| 828 | 828 | $values['components'] = $properties['components']; |
| 829 | 829 | } |
| 830 | 830 | |
| 831 | - $transp = '{' . Plugin::NS_CALDAV . '}schedule-calendar-transp'; |
|
| 831 | + $transp = '{'.Plugin::NS_CALDAV.'}schedule-calendar-transp'; |
|
| 832 | 832 | if (isset($properties[$transp])) { |
| 833 | - $values['transparent'] = (int)($properties[$transp]->getValue() === 'transparent'); |
|
| 833 | + $values['transparent'] = (int) ($properties[$transp]->getValue() === 'transparent'); |
|
| 834 | 834 | } |
| 835 | 835 | |
| 836 | 836 | foreach ($this->propertyMap as $xmlName => [$dbName, $type]) { |
@@ -839,7 +839,7 @@ discard block |
||
| 839 | 839 | } |
| 840 | 840 | } |
| 841 | 841 | |
| 842 | - [$calendarId, $calendarData] = $this->atomic(function () use ($values) { |
|
| 842 | + [$calendarId, $calendarData] = $this->atomic(function() use ($values) { |
|
| 843 | 843 | $query = $this->db->getQueryBuilder(); |
| 844 | 844 | $query->insert('calendars'); |
| 845 | 845 | foreach ($values as $column => $value) { |
@@ -852,7 +852,7 @@ discard block |
||
| 852 | 852 | return [$calendarId, $calendarData]; |
| 853 | 853 | }, $this->db); |
| 854 | 854 | |
| 855 | - $this->dispatcher->dispatchTyped(new CalendarCreatedEvent((int)$calendarId, $calendarData)); |
|
| 855 | + $this->dispatcher->dispatchTyped(new CalendarCreatedEvent((int) $calendarId, $calendarData)); |
|
| 856 | 856 | |
| 857 | 857 | return $calendarId; |
| 858 | 858 | } |
@@ -875,15 +875,15 @@ discard block |
||
| 875 | 875 | */ |
| 876 | 876 | public function updateCalendar($calendarId, PropPatch $propPatch) { |
| 877 | 877 | $supportedProperties = array_keys($this->propertyMap); |
| 878 | - $supportedProperties[] = '{' . Plugin::NS_CALDAV . '}schedule-calendar-transp'; |
|
| 878 | + $supportedProperties[] = '{'.Plugin::NS_CALDAV.'}schedule-calendar-transp'; |
|
| 879 | 879 | |
| 880 | - $propPatch->handle($supportedProperties, function ($mutations) use ($calendarId) { |
|
| 880 | + $propPatch->handle($supportedProperties, function($mutations) use ($calendarId) { |
|
| 881 | 881 | $newValues = []; |
| 882 | 882 | foreach ($mutations as $propertyName => $propertyValue) { |
| 883 | 883 | switch ($propertyName) { |
| 884 | - case '{' . Plugin::NS_CALDAV . '}schedule-calendar-transp': |
|
| 884 | + case '{'.Plugin::NS_CALDAV.'}schedule-calendar-transp': |
|
| 885 | 885 | $fieldName = 'transparent'; |
| 886 | - $newValues[$fieldName] = (int)($propertyValue->getValue() === 'transparent'); |
|
| 886 | + $newValues[$fieldName] = (int) ($propertyValue->getValue() === 'transparent'); |
|
| 887 | 887 | break; |
| 888 | 888 | default: |
| 889 | 889 | $fieldName = $this->propertyMap[$propertyName][0]; |
@@ -891,7 +891,7 @@ discard block |
||
| 891 | 891 | break; |
| 892 | 892 | } |
| 893 | 893 | } |
| 894 | - [$calendarData, $shares] = $this->atomic(function () use ($calendarId, $newValues) { |
|
| 894 | + [$calendarData, $shares] = $this->atomic(function() use ($calendarId, $newValues) { |
|
| 895 | 895 | $query = $this->db->getQueryBuilder(); |
| 896 | 896 | $query->update('calendars'); |
| 897 | 897 | foreach ($newValues as $fieldName => $value) { |
@@ -920,7 +920,7 @@ discard block |
||
| 920 | 920 | * @return void |
| 921 | 921 | */ |
| 922 | 922 | public function deleteCalendar($calendarId, bool $forceDeletePermanently = false) { |
| 923 | - $this->atomic(function () use ($calendarId, $forceDeletePermanently): void { |
|
| 923 | + $this->atomic(function() use ($calendarId, $forceDeletePermanently): void { |
|
| 924 | 924 | // The calendar is deleted right away if this is either enforced by the caller |
| 925 | 925 | // or the special contacts birthday calendar or when the preference of an empty |
| 926 | 926 | // retention (0 seconds) is set, which signals a disabled trashbin. |
@@ -983,7 +983,7 @@ discard block |
||
| 983 | 983 | } |
| 984 | 984 | |
| 985 | 985 | public function restoreCalendar(int $id): void { |
| 986 | - $this->atomic(function () use ($id): void { |
|
| 986 | + $this->atomic(function() use ($id): void { |
|
| 987 | 987 | $qb = $this->db->getQueryBuilder(); |
| 988 | 988 | $update = $qb->update('calendars') |
| 989 | 989 | ->set('deleted_at', $qb->createNamedParameter(null)) |
@@ -1057,7 +1057,7 @@ discard block |
||
| 1057 | 1057 | */ |
| 1058 | 1058 | public function getLimitedCalendarObjects(int $calendarId, int $calendarType = self::CALENDAR_TYPE_CALENDAR):array { |
| 1059 | 1059 | $query = $this->db->getQueryBuilder(); |
| 1060 | - $query->select(['id','uid', 'etag', 'uri', 'calendardata']) |
|
| 1060 | + $query->select(['id', 'uid', 'etag', 'uri', 'calendardata']) |
|
| 1061 | 1061 | ->from('calendarobjects') |
| 1062 | 1062 | ->where($query->expr()->eq('calendarid', $query->createNamedParameter($calendarId))) |
| 1063 | 1063 | ->andWhere($query->expr()->eq('calendartype', $query->createNamedParameter($calendarType))) |
@@ -1135,11 +1135,11 @@ discard block |
||
| 1135 | 1135 | 'id' => $row['id'], |
| 1136 | 1136 | 'uri' => $row['uri'], |
| 1137 | 1137 | 'lastmodified' => $row['lastmodified'], |
| 1138 | - 'etag' => '"' . $row['etag'] . '"', |
|
| 1138 | + 'etag' => '"'.$row['etag'].'"', |
|
| 1139 | 1139 | 'calendarid' => $row['calendarid'], |
| 1140 | - 'size' => (int)$row['size'], |
|
| 1140 | + 'size' => (int) $row['size'], |
|
| 1141 | 1141 | 'component' => strtolower($row['componenttype']), |
| 1142 | - 'classification' => (int)$row['classification'] |
|
| 1142 | + 'classification' => (int) $row['classification'] |
|
| 1143 | 1143 | ]; |
| 1144 | 1144 | } |
| 1145 | 1145 | $stmt->closeCursor(); |
@@ -1162,13 +1162,13 @@ discard block |
||
| 1162 | 1162 | 'id' => $row['id'], |
| 1163 | 1163 | 'uri' => $row['uri'], |
| 1164 | 1164 | 'lastmodified' => $row['lastmodified'], |
| 1165 | - 'etag' => '"' . $row['etag'] . '"', |
|
| 1166 | - 'calendarid' => (int)$row['calendarid'], |
|
| 1167 | - 'calendartype' => (int)$row['calendartype'], |
|
| 1168 | - 'size' => (int)$row['size'], |
|
| 1165 | + 'etag' => '"'.$row['etag'].'"', |
|
| 1166 | + 'calendarid' => (int) $row['calendarid'], |
|
| 1167 | + 'calendartype' => (int) $row['calendartype'], |
|
| 1168 | + 'size' => (int) $row['size'], |
|
| 1169 | 1169 | 'component' => strtolower($row['componenttype']), |
| 1170 | - 'classification' => (int)$row['classification'], |
|
| 1171 | - '{' . \OCA\DAV\DAV\Sharing\Plugin::NS_NEXTCLOUD . '}deleted-at' => $row['deleted_at'] === null ? $row['deleted_at'] : (int)$row['deleted_at'], |
|
| 1170 | + 'classification' => (int) $row['classification'], |
|
| 1171 | + '{'.\OCA\DAV\DAV\Sharing\Plugin::NS_NEXTCLOUD.'}deleted-at' => $row['deleted_at'] === null ? $row['deleted_at'] : (int) $row['deleted_at'], |
|
| 1172 | 1172 | ]; |
| 1173 | 1173 | } |
| 1174 | 1174 | $stmt->closeCursor(); |
@@ -1201,13 +1201,13 @@ discard block |
||
| 1201 | 1201 | 'id' => $row['id'], |
| 1202 | 1202 | 'uri' => $row['uri'], |
| 1203 | 1203 | 'lastmodified' => $row['lastmodified'], |
| 1204 | - 'etag' => '"' . $row['etag'] . '"', |
|
| 1204 | + 'etag' => '"'.$row['etag'].'"', |
|
| 1205 | 1205 | 'calendarid' => $row['calendarid'], |
| 1206 | 1206 | 'calendaruri' => $row['calendaruri'], |
| 1207 | - 'size' => (int)$row['size'], |
|
| 1207 | + 'size' => (int) $row['size'], |
|
| 1208 | 1208 | 'component' => strtolower($row['componenttype']), |
| 1209 | - 'classification' => (int)$row['classification'], |
|
| 1210 | - '{' . \OCA\DAV\DAV\Sharing\Plugin::NS_NEXTCLOUD . '}deleted-at' => $row['deleted_at'] === null ? $row['deleted_at'] : (int)$row['deleted_at'], |
|
| 1209 | + 'classification' => (int) $row['classification'], |
|
| 1210 | + '{'.\OCA\DAV\DAV\Sharing\Plugin::NS_NEXTCLOUD.'}deleted-at' => $row['deleted_at'] === null ? $row['deleted_at'] : (int) $row['deleted_at'], |
|
| 1211 | 1211 | ]; |
| 1212 | 1212 | } |
| 1213 | 1213 | $stmt->closeCursor(); |
@@ -1233,7 +1233,7 @@ discard block |
||
| 1233 | 1233 | * @return array|null |
| 1234 | 1234 | */ |
| 1235 | 1235 | public function getCalendarObject($calendarId, $objectUri, int $calendarType = self::CALENDAR_TYPE_CALENDAR) { |
| 1236 | - $key = $calendarId . '::' . $objectUri . '::' . $calendarType; |
|
| 1236 | + $key = $calendarId.'::'.$objectUri.'::'.$calendarType; |
|
| 1237 | 1237 | if (isset($this->cachedObjects[$key])) { |
| 1238 | 1238 | return $this->cachedObjects[$key]; |
| 1239 | 1239 | } |
@@ -1262,13 +1262,13 @@ discard block |
||
| 1262 | 1262 | 'uri' => $row['uri'], |
| 1263 | 1263 | 'uid' => $row['uid'], |
| 1264 | 1264 | 'lastmodified' => $row['lastmodified'], |
| 1265 | - 'etag' => '"' . $row['etag'] . '"', |
|
| 1265 | + 'etag' => '"'.$row['etag'].'"', |
|
| 1266 | 1266 | 'calendarid' => $row['calendarid'], |
| 1267 | - 'size' => (int)$row['size'], |
|
| 1267 | + 'size' => (int) $row['size'], |
|
| 1268 | 1268 | 'calendardata' => $this->readBlob($row['calendardata']), |
| 1269 | 1269 | 'component' => strtolower($row['componenttype']), |
| 1270 | - 'classification' => (int)$row['classification'], |
|
| 1271 | - '{' . \OCA\DAV\DAV\Sharing\Plugin::NS_NEXTCLOUD . '}deleted-at' => $row['deleted_at'] === null ? $row['deleted_at'] : (int)$row['deleted_at'], |
|
| 1270 | + 'classification' => (int) $row['classification'], |
|
| 1271 | + '{'.\OCA\DAV\DAV\Sharing\Plugin::NS_NEXTCLOUD.'}deleted-at' => $row['deleted_at'] === null ? $row['deleted_at'] : (int) $row['deleted_at'], |
|
| 1272 | 1272 | ]; |
| 1273 | 1273 | } |
| 1274 | 1274 | |
@@ -1310,12 +1310,12 @@ discard block |
||
| 1310 | 1310 | 'id' => $row['id'], |
| 1311 | 1311 | 'uri' => $row['uri'], |
| 1312 | 1312 | 'lastmodified' => $row['lastmodified'], |
| 1313 | - 'etag' => '"' . $row['etag'] . '"', |
|
| 1313 | + 'etag' => '"'.$row['etag'].'"', |
|
| 1314 | 1314 | 'calendarid' => $row['calendarid'], |
| 1315 | - 'size' => (int)$row['size'], |
|
| 1315 | + 'size' => (int) $row['size'], |
|
| 1316 | 1316 | 'calendardata' => $this->readBlob($row['calendardata']), |
| 1317 | 1317 | 'component' => strtolower($row['componenttype']), |
| 1318 | - 'classification' => (int)$row['classification'] |
|
| 1318 | + 'classification' => (int) $row['classification'] |
|
| 1319 | 1319 | ]; |
| 1320 | 1320 | } |
| 1321 | 1321 | $result->closeCursor(); |
@@ -1347,7 +1347,7 @@ discard block |
||
| 1347 | 1347 | $this->cachedObjects = []; |
| 1348 | 1348 | $extraData = $this->getDenormalizedData($calendarData); |
| 1349 | 1349 | |
| 1350 | - return $this->atomic(function () use ($calendarId, $objectUri, $calendarData, $extraData, $calendarType) { |
|
| 1350 | + return $this->atomic(function() use ($calendarId, $objectUri, $calendarData, $extraData, $calendarType) { |
|
| 1351 | 1351 | // Try to detect duplicates |
| 1352 | 1352 | $qb = $this->db->getQueryBuilder(); |
| 1353 | 1353 | $qb->select($qb->func()->count('*')) |
@@ -1357,7 +1357,7 @@ discard block |
||
| 1357 | 1357 | ->andWhere($qb->expr()->eq('calendartype', $qb->createNamedParameter($calendarType))) |
| 1358 | 1358 | ->andWhere($qb->expr()->isNull('deleted_at')); |
| 1359 | 1359 | $result = $qb->executeQuery(); |
| 1360 | - $count = (int)$result->fetchOne(); |
|
| 1360 | + $count = (int) $result->fetchOne(); |
|
| 1361 | 1361 | $result->closeCursor(); |
| 1362 | 1362 | |
| 1363 | 1363 | if ($count !== 0) { |
@@ -1415,7 +1415,7 @@ discard block |
||
| 1415 | 1415 | $this->dispatcher->dispatchTyped(new CachedCalendarObjectCreatedEvent($calendarId, $subscriptionRow, [], $objectRow)); |
| 1416 | 1416 | } |
| 1417 | 1417 | |
| 1418 | - return '"' . $extraData['etag'] . '"'; |
|
| 1418 | + return '"'.$extraData['etag'].'"'; |
|
| 1419 | 1419 | }, $this->db); |
| 1420 | 1420 | } |
| 1421 | 1421 | |
@@ -1442,7 +1442,7 @@ discard block |
||
| 1442 | 1442 | $this->cachedObjects = []; |
| 1443 | 1443 | $extraData = $this->getDenormalizedData($calendarData); |
| 1444 | 1444 | |
| 1445 | - return $this->atomic(function () use ($calendarId, $objectUri, $calendarData, $extraData, $calendarType) { |
|
| 1445 | + return $this->atomic(function() use ($calendarId, $objectUri, $calendarData, $extraData, $calendarType) { |
|
| 1446 | 1446 | $query = $this->db->getQueryBuilder(); |
| 1447 | 1447 | $query->update('calendarobjects') |
| 1448 | 1448 | ->set('calendardata', $query->createNamedParameter($calendarData, IQueryBuilder::PARAM_LOB)) |
@@ -1476,7 +1476,7 @@ discard block |
||
| 1476 | 1476 | } |
| 1477 | 1477 | } |
| 1478 | 1478 | |
| 1479 | - return '"' . $extraData['etag'] . '"'; |
|
| 1479 | + return '"'.$extraData['etag'].'"'; |
|
| 1480 | 1480 | }, $this->db); |
| 1481 | 1481 | } |
| 1482 | 1482 | |
@@ -1494,7 +1494,7 @@ discard block |
||
| 1494 | 1494 | */ |
| 1495 | 1495 | public function moveCalendarObject(string $sourcePrincipalUri, int $sourceObjectId, string $targetPrincipalUri, int $targetCalendarId, string $tragetObjectUri, int $calendarType = self::CALENDAR_TYPE_CALENDAR): bool { |
| 1496 | 1496 | $this->cachedObjects = []; |
| 1497 | - return $this->atomic(function () use ($sourcePrincipalUri, $sourceObjectId, $targetPrincipalUri, $targetCalendarId, $tragetObjectUri, $calendarType) { |
|
| 1497 | + return $this->atomic(function() use ($sourcePrincipalUri, $sourceObjectId, $targetPrincipalUri, $targetCalendarId, $tragetObjectUri, $calendarType) { |
|
| 1498 | 1498 | $object = $this->getCalendarObjectById($sourcePrincipalUri, $sourceObjectId); |
| 1499 | 1499 | if (empty($object)) { |
| 1500 | 1500 | return false; |
@@ -1570,7 +1570,7 @@ discard block |
||
| 1570 | 1570 | */ |
| 1571 | 1571 | public function deleteCalendarObject($calendarId, $objectUri, $calendarType = self::CALENDAR_TYPE_CALENDAR, bool $forceDeletePermanently = false) { |
| 1572 | 1572 | $this->cachedObjects = []; |
| 1573 | - $this->atomic(function () use ($calendarId, $objectUri, $calendarType, $forceDeletePermanently): void { |
|
| 1573 | + $this->atomic(function() use ($calendarId, $objectUri, $calendarType, $forceDeletePermanently): void { |
|
| 1574 | 1574 | $data = $this->getCalendarObject($calendarId, $objectUri, $calendarType); |
| 1575 | 1575 | |
| 1576 | 1576 | if ($data === null) { |
@@ -1654,8 +1654,8 @@ discard block |
||
| 1654 | 1654 | */ |
| 1655 | 1655 | public function restoreCalendarObject(array $objectData): void { |
| 1656 | 1656 | $this->cachedObjects = []; |
| 1657 | - $this->atomic(function () use ($objectData): void { |
|
| 1658 | - $id = (int)$objectData['id']; |
|
| 1657 | + $this->atomic(function() use ($objectData): void { |
|
| 1658 | + $id = (int) $objectData['id']; |
|
| 1659 | 1659 | $restoreUri = str_replace('-deleted.ics', '.ics', $objectData['uri']); |
| 1660 | 1660 | $targetObject = $this->getCalendarObject( |
| 1661 | 1661 | $objectData['calendarid'], |
@@ -1685,17 +1685,17 @@ discard block |
||
| 1685 | 1685 | // Welp, this should possibly not have happened, but let's ignore |
| 1686 | 1686 | return; |
| 1687 | 1687 | } |
| 1688 | - $this->addChanges($row['calendarid'], [$row['uri']], 1, (int)$row['calendartype']); |
|
| 1688 | + $this->addChanges($row['calendarid'], [$row['uri']], 1, (int) $row['calendartype']); |
|
| 1689 | 1689 | |
| 1690 | - $calendarRow = $this->getCalendarById((int)$row['calendarid']); |
|
| 1690 | + $calendarRow = $this->getCalendarById((int) $row['calendarid']); |
|
| 1691 | 1691 | if ($calendarRow === null) { |
| 1692 | 1692 | throw new RuntimeException('Calendar object data that was just written can\'t be read back. Check your database configuration.'); |
| 1693 | 1693 | } |
| 1694 | 1694 | $this->dispatcher->dispatchTyped( |
| 1695 | 1695 | new CalendarObjectRestoredEvent( |
| 1696 | - (int)$objectData['calendarid'], |
|
| 1696 | + (int) $objectData['calendarid'], |
|
| 1697 | 1697 | $calendarRow, |
| 1698 | - $this->getShares((int)$row['calendarid']), |
|
| 1698 | + $this->getShares((int) $row['calendarid']), |
|
| 1699 | 1699 | $row |
| 1700 | 1700 | ) |
| 1701 | 1701 | ); |
@@ -1814,19 +1814,19 @@ discard block |
||
| 1814 | 1814 | try { |
| 1815 | 1815 | $matches = $this->validateFilterForObject($row, $filters); |
| 1816 | 1816 | } catch (ParseException $ex) { |
| 1817 | - $this->logger->error('Caught parsing exception for calendar data. This usually indicates invalid calendar data. calendar-id:' . $calendarId . ' uri:' . $row['uri'], [ |
|
| 1817 | + $this->logger->error('Caught parsing exception for calendar data. This usually indicates invalid calendar data. calendar-id:'.$calendarId.' uri:'.$row['uri'], [ |
|
| 1818 | 1818 | 'app' => 'dav', |
| 1819 | 1819 | 'exception' => $ex, |
| 1820 | 1820 | ]); |
| 1821 | 1821 | continue; |
| 1822 | 1822 | } catch (InvalidDataException $ex) { |
| 1823 | - $this->logger->error('Caught invalid data exception for calendar data. This usually indicates invalid calendar data. calendar-id:' . $calendarId . ' uri:' . $row['uri'], [ |
|
| 1823 | + $this->logger->error('Caught invalid data exception for calendar data. This usually indicates invalid calendar data. calendar-id:'.$calendarId.' uri:'.$row['uri'], [ |
|
| 1824 | 1824 | 'app' => 'dav', |
| 1825 | 1825 | 'exception' => $ex, |
| 1826 | 1826 | ]); |
| 1827 | 1827 | continue; |
| 1828 | 1828 | } catch (MaxInstancesExceededException $ex) { |
| 1829 | - $this->logger->warning('Caught max instances exceeded exception for calendar data. This usually indicates too much recurring (more than 3500) event in calendar data. Object uri: ' . $row['uri'], [ |
|
| 1829 | + $this->logger->warning('Caught max instances exceeded exception for calendar data. This usually indicates too much recurring (more than 3500) event in calendar data. Object uri: '.$row['uri'], [ |
|
| 1830 | 1830 | 'app' => 'dav', |
| 1831 | 1831 | 'exception' => $ex, |
| 1832 | 1832 | ]); |
@@ -1838,7 +1838,7 @@ discard block |
||
| 1838 | 1838 | } |
| 1839 | 1839 | } |
| 1840 | 1840 | $result[] = $row['uri']; |
| 1841 | - $key = $calendarId . '::' . $row['uri'] . '::' . $calendarType; |
|
| 1841 | + $key = $calendarId.'::'.$row['uri'].'::'.$calendarType; |
|
| 1842 | 1842 | $this->cachedObjects[$key] = $this->rowToCalendarObject($row); |
| 1843 | 1843 | } |
| 1844 | 1844 | |
@@ -1857,7 +1857,7 @@ discard block |
||
| 1857 | 1857 | * @return array |
| 1858 | 1858 | */ |
| 1859 | 1859 | public function calendarSearch($principalUri, array $filters, $limit = null, $offset = null) { |
| 1860 | - return $this->atomic(function () use ($principalUri, $filters, $limit, $offset) { |
|
| 1860 | + return $this->atomic(function() use ($principalUri, $filters, $limit, $offset) { |
|
| 1861 | 1861 | $calendars = $this->getCalendarsForUser($principalUri); |
| 1862 | 1862 | $ownCalendars = []; |
| 1863 | 1863 | $sharedCalendars = []; |
@@ -1949,7 +1949,7 @@ discard block |
||
| 1949 | 1949 | ->andWhere($compExpr) |
| 1950 | 1950 | ->andWhere($propParamExpr) |
| 1951 | 1951 | ->andWhere($query->expr()->iLike('i.value', |
| 1952 | - $query->createNamedParameter('%' . $this->db->escapeLikeParameter($filters['search-term']) . '%'))) |
|
| 1952 | + $query->createNamedParameter('%'.$this->db->escapeLikeParameter($filters['search-term']).'%'))) |
|
| 1953 | 1953 | ->andWhere($query->expr()->isNull('deleted_at')); |
| 1954 | 1954 | |
| 1955 | 1955 | if ($offset) { |
@@ -1963,7 +1963,7 @@ discard block |
||
| 1963 | 1963 | |
| 1964 | 1964 | $result = []; |
| 1965 | 1965 | while ($row = $stmt->fetch()) { |
| 1966 | - $path = $uriMapper[$row['calendarid']] . '/' . $row['uri']; |
|
| 1966 | + $path = $uriMapper[$row['calendarid']].'/'.$row['uri']; |
|
| 1967 | 1967 | if (!in_array($path, $result)) { |
| 1968 | 1968 | $result[] = $path; |
| 1969 | 1969 | } |
@@ -2030,8 +2030,8 @@ discard block |
||
| 2030 | 2030 | |
| 2031 | 2031 | if ($pattern !== '') { |
| 2032 | 2032 | $innerQuery->andWhere($innerQuery->expr()->iLike('op.value', |
| 2033 | - $outerQuery->createNamedParameter('%' . |
|
| 2034 | - $this->db->escapeLikeParameter($pattern) . '%'))); |
|
| 2033 | + $outerQuery->createNamedParameter('%'. |
|
| 2034 | + $this->db->escapeLikeParameter($pattern).'%'))); |
|
| 2035 | 2035 | } |
| 2036 | 2036 | |
| 2037 | 2037 | $start = null; |
@@ -2083,7 +2083,7 @@ discard block |
||
| 2083 | 2083 | // For the pagination with hasLimit and hasTimeRange, a stable ordering is helpful. |
| 2084 | 2084 | $outerQuery->addOrderBy('id'); |
| 2085 | 2085 | |
| 2086 | - $offset = (int)$offset; |
|
| 2086 | + $offset = (int) $offset; |
|
| 2087 | 2087 | $outerQuery->setFirstResult($offset); |
| 2088 | 2088 | |
| 2089 | 2089 | $calendarObjects = []; |
@@ -2104,7 +2104,7 @@ discard block |
||
| 2104 | 2104 | * |
| 2105 | 2105 | * 25 rows and 3 retries is entirely arbitrary. |
| 2106 | 2106 | */ |
| 2107 | - $maxResults = (int)max($limit, 25); |
|
| 2107 | + $maxResults = (int) max($limit, 25); |
|
| 2108 | 2108 | $outerQuery->setMaxResults($maxResults); |
| 2109 | 2109 | |
| 2110 | 2110 | for ($attempt = $objectsCount = 0; $attempt < 3 && $objectsCount < $limit; $attempt++) { |
@@ -2118,7 +2118,7 @@ discard block |
||
| 2118 | 2118 | $calendarObjects = $this->searchCalendarObjects($outerQuery, $start, $end); |
| 2119 | 2119 | } |
| 2120 | 2120 | |
| 2121 | - $calendarObjects = array_map(function ($o) use ($options) { |
|
| 2121 | + $calendarObjects = array_map(function($o) use ($options) { |
|
| 2122 | 2122 | $calendarData = Reader::read($o['calendardata']); |
| 2123 | 2123 | |
| 2124 | 2124 | // Expand recurrences if an explicit time range is requested |
@@ -2146,16 +2146,16 @@ discard block |
||
| 2146 | 2146 | 'type' => $o['componenttype'], |
| 2147 | 2147 | 'uid' => $o['uid'], |
| 2148 | 2148 | 'uri' => $o['uri'], |
| 2149 | - 'objects' => array_map(function ($c) { |
|
| 2149 | + 'objects' => array_map(function($c) { |
|
| 2150 | 2150 | return $this->transformSearchData($c); |
| 2151 | 2151 | }, $objects), |
| 2152 | - 'timezones' => array_map(function ($c) { |
|
| 2152 | + 'timezones' => array_map(function($c) { |
|
| 2153 | 2153 | return $this->transformSearchData($c); |
| 2154 | 2154 | }, $timezones), |
| 2155 | 2155 | ]; |
| 2156 | 2156 | }, $calendarObjects); |
| 2157 | 2157 | |
| 2158 | - usort($calendarObjects, function (array $a, array $b) { |
|
| 2158 | + usort($calendarObjects, function(array $a, array $b) { |
|
| 2159 | 2159 | /** @var DateTimeImmutable $startA */ |
| 2160 | 2160 | $startA = $a['objects'][0]['DTSTART'][0] ?? new DateTimeImmutable(self::MAX_DATE); |
| 2161 | 2161 | /** @var DateTimeImmutable $startB */ |
@@ -2200,7 +2200,7 @@ discard block |
||
| 2200 | 2200 | 'time-range' => null, |
| 2201 | 2201 | ]); |
| 2202 | 2202 | } catch (MaxInstancesExceededException $ex) { |
| 2203 | - $this->logger->warning('Caught max instances exceeded exception for calendar data. This usually indicates too much recurring (more than 3500) event in calendar data. Object uri: ' . $row['uri'], [ |
|
| 2203 | + $this->logger->warning('Caught max instances exceeded exception for calendar data. This usually indicates too much recurring (more than 3500) event in calendar data. Object uri: '.$row['uri'], [ |
|
| 2204 | 2204 | 'app' => 'dav', |
| 2205 | 2205 | 'exception' => $ex, |
| 2206 | 2206 | ]); |
@@ -2231,7 +2231,7 @@ discard block |
||
| 2231 | 2231 | /** @var Component[] $subComponents */ |
| 2232 | 2232 | $subComponents = $comp->getComponents(); |
| 2233 | 2233 | /** @var Property[] $properties */ |
| 2234 | - $properties = array_filter($comp->children(), function ($c) { |
|
| 2234 | + $properties = array_filter($comp->children(), function($c) { |
|
| 2235 | 2235 | return $c instanceof Property; |
| 2236 | 2236 | }); |
| 2237 | 2237 | $validationRules = $comp->getValidationRules(); |
@@ -2299,7 +2299,7 @@ discard block |
||
| 2299 | 2299 | array $searchParameters, |
| 2300 | 2300 | array $options = [], |
| 2301 | 2301 | ): array { |
| 2302 | - return $this->atomic(function () use ($principalUri, $pattern, $componentTypes, $searchProperties, $searchParameters, $options) { |
|
| 2302 | + return $this->atomic(function() use ($principalUri, $pattern, $componentTypes, $searchProperties, $searchParameters, $options) { |
|
| 2303 | 2303 | $escapePattern = !\array_key_exists('escape_like_param', $options) || $options['escape_like_param'] !== false; |
| 2304 | 2304 | |
| 2305 | 2305 | $calendarObjectIdQuery = $this->db->getQueryBuilder(); |
@@ -2311,7 +2311,7 @@ discard block |
||
| 2311 | 2311 | $subscriptions = $this->getSubscriptionsForUser($principalUri); |
| 2312 | 2312 | foreach ($calendars as $calendar) { |
| 2313 | 2313 | $calendarAnd = $calendarObjectIdQuery->expr()->andX( |
| 2314 | - $calendarObjectIdQuery->expr()->eq('cob.calendarid', $calendarObjectIdQuery->createNamedParameter((int)$calendar['id'])), |
|
| 2314 | + $calendarObjectIdQuery->expr()->eq('cob.calendarid', $calendarObjectIdQuery->createNamedParameter((int) $calendar['id'])), |
|
| 2315 | 2315 | $calendarObjectIdQuery->expr()->eq('cob.calendartype', $calendarObjectIdQuery->createNamedParameter(self::CALENDAR_TYPE_CALENDAR)), |
| 2316 | 2316 | ); |
| 2317 | 2317 | |
@@ -2325,7 +2325,7 @@ discard block |
||
| 2325 | 2325 | } |
| 2326 | 2326 | foreach ($subscriptions as $subscription) { |
| 2327 | 2327 | $subscriptionAnd = $calendarObjectIdQuery->expr()->andX( |
| 2328 | - $calendarObjectIdQuery->expr()->eq('cob.calendarid', $calendarObjectIdQuery->createNamedParameter((int)$subscription['id'])), |
|
| 2328 | + $calendarObjectIdQuery->expr()->eq('cob.calendarid', $calendarObjectIdQuery->createNamedParameter((int) $subscription['id'])), |
|
| 2329 | 2329 | $calendarObjectIdQuery->expr()->eq('cob.calendartype', $calendarObjectIdQuery->createNamedParameter(self::CALENDAR_TYPE_SUBSCRIPTION)), |
| 2330 | 2330 | ); |
| 2331 | 2331 | |
@@ -2374,7 +2374,7 @@ discard block |
||
| 2374 | 2374 | if (!$escapePattern) { |
| 2375 | 2375 | $calendarObjectIdQuery->andWhere($calendarObjectIdQuery->expr()->ilike('cob.value', $calendarObjectIdQuery->createNamedParameter($pattern))); |
| 2376 | 2376 | } else { |
| 2377 | - $calendarObjectIdQuery->andWhere($calendarObjectIdQuery->expr()->ilike('cob.value', $calendarObjectIdQuery->createNamedParameter('%' . $this->db->escapeLikeParameter($pattern) . '%'))); |
|
| 2377 | + $calendarObjectIdQuery->andWhere($calendarObjectIdQuery->expr()->ilike('cob.value', $calendarObjectIdQuery->createNamedParameter('%'.$this->db->escapeLikeParameter($pattern).'%'))); |
|
| 2378 | 2378 | } |
| 2379 | 2379 | } |
| 2380 | 2380 | |
@@ -2402,7 +2402,7 @@ discard block |
||
| 2402 | 2402 | $result = $calendarObjectIdQuery->executeQuery(); |
| 2403 | 2403 | $matches = []; |
| 2404 | 2404 | while (($row = $result->fetch()) !== false) { |
| 2405 | - $matches[] = (int)$row['objectid']; |
|
| 2405 | + $matches[] = (int) $row['objectid']; |
|
| 2406 | 2406 | } |
| 2407 | 2407 | $result->closeCursor(); |
| 2408 | 2408 | |
@@ -2414,8 +2414,8 @@ discard block |
||
| 2414 | 2414 | $result = $query->executeQuery(); |
| 2415 | 2415 | $calendarObjects = []; |
| 2416 | 2416 | while (($array = $result->fetch()) !== false) { |
| 2417 | - $array['calendarid'] = (int)$array['calendarid']; |
|
| 2418 | - $array['calendartype'] = (int)$array['calendartype']; |
|
| 2417 | + $array['calendarid'] = (int) $array['calendarid']; |
|
| 2418 | + $array['calendartype'] = (int) $array['calendartype']; |
|
| 2419 | 2419 | $array['calendardata'] = $this->readBlob($array['calendardata']); |
| 2420 | 2420 | |
| 2421 | 2421 | $calendarObjects[] = $array; |
@@ -2456,7 +2456,7 @@ discard block |
||
| 2456 | 2456 | $row = $stmt->fetch(); |
| 2457 | 2457 | $stmt->closeCursor(); |
| 2458 | 2458 | if ($row) { |
| 2459 | - return $row['calendaruri'] . '/' . $row['objecturi']; |
|
| 2459 | + return $row['calendaruri'].'/'.$row['objecturi']; |
|
| 2460 | 2460 | } |
| 2461 | 2461 | |
| 2462 | 2462 | return null; |
@@ -2482,14 +2482,14 @@ discard block |
||
| 2482 | 2482 | 'id' => $row['id'], |
| 2483 | 2483 | 'uri' => $row['uri'], |
| 2484 | 2484 | 'lastmodified' => $row['lastmodified'], |
| 2485 | - 'etag' => '"' . $row['etag'] . '"', |
|
| 2485 | + 'etag' => '"'.$row['etag'].'"', |
|
| 2486 | 2486 | 'calendarid' => $row['calendarid'], |
| 2487 | 2487 | 'calendaruri' => $row['calendaruri'], |
| 2488 | - 'size' => (int)$row['size'], |
|
| 2488 | + 'size' => (int) $row['size'], |
|
| 2489 | 2489 | 'calendardata' => $this->readBlob($row['calendardata']), |
| 2490 | 2490 | 'component' => strtolower($row['componenttype']), |
| 2491 | - 'classification' => (int)$row['classification'], |
|
| 2492 | - 'deleted_at' => isset($row['deleted_at']) ? ((int)$row['deleted_at']) : null, |
|
| 2491 | + 'classification' => (int) $row['classification'], |
|
| 2492 | + 'deleted_at' => isset($row['deleted_at']) ? ((int) $row['deleted_at']) : null, |
|
| 2493 | 2493 | ]; |
| 2494 | 2494 | } |
| 2495 | 2495 | |
@@ -2551,9 +2551,9 @@ discard block |
||
| 2551 | 2551 | * @return ?array |
| 2552 | 2552 | */ |
| 2553 | 2553 | public function getChangesForCalendar($calendarId, $syncToken, $syncLevel, $limit = null, $calendarType = self::CALENDAR_TYPE_CALENDAR) { |
| 2554 | - $table = $calendarType === self::CALENDAR_TYPE_CALENDAR ? 'calendars': 'calendarsubscriptions'; |
|
| 2554 | + $table = $calendarType === self::CALENDAR_TYPE_CALENDAR ? 'calendars' : 'calendarsubscriptions'; |
|
| 2555 | 2555 | |
| 2556 | - return $this->atomic(function () use ($calendarId, $syncToken, $syncLevel, $limit, $calendarType, $table) { |
|
| 2556 | + return $this->atomic(function() use ($calendarId, $syncToken, $syncLevel, $limit, $calendarType, $table) { |
|
| 2557 | 2557 | // Current synctoken |
| 2558 | 2558 | $qb = $this->db->getQueryBuilder(); |
| 2559 | 2559 | $qb->select('synctoken') |
@@ -2604,7 +2604,7 @@ discard block |
||
| 2604 | 2604 | while ($entry = $stmt->fetch(\PDO::FETCH_NUM)) { |
| 2605 | 2605 | // assign uri (column 0) to appropriate mutation based on operation (column 1) |
| 2606 | 2606 | // forced (int) is needed as doctrine with OCI returns the operation field as string not integer |
| 2607 | - match ((int)$entry[1]) { |
|
| 2607 | + match ((int) $entry[1]) { |
|
| 2608 | 2608 | 1 => $result['added'][] = $entry[0], |
| 2609 | 2609 | 2 => $result['modified'][] = $entry[0], |
| 2610 | 2610 | 3 => $result['deleted'][] = $entry[0], |
@@ -2675,7 +2675,7 @@ discard block |
||
| 2675 | 2675 | 'source' => $row['source'], |
| 2676 | 2676 | 'lastmodified' => $row['lastmodified'], |
| 2677 | 2677 | |
| 2678 | - '{' . Plugin::NS_CALDAV . '}supported-calendar-component-set' => new SupportedCalendarComponentSet(['VTODO', 'VEVENT']), |
|
| 2678 | + '{'.Plugin::NS_CALDAV.'}supported-calendar-component-set' => new SupportedCalendarComponentSet(['VTODO', 'VEVENT']), |
|
| 2679 | 2679 | '{http://sabredav.org/ns}sync-token' => $row['synctoken'] ?: '0', |
| 2680 | 2680 | ]; |
| 2681 | 2681 | |
@@ -2719,7 +2719,7 @@ discard block |
||
| 2719 | 2719 | } |
| 2720 | 2720 | } |
| 2721 | 2721 | |
| 2722 | - [$subscriptionId, $subscriptionRow] = $this->atomic(function () use ($values) { |
|
| 2722 | + [$subscriptionId, $subscriptionRow] = $this->atomic(function() use ($values) { |
|
| 2723 | 2723 | $valuesToInsert = []; |
| 2724 | 2724 | $query = $this->db->getQueryBuilder(); |
| 2725 | 2725 | foreach (array_keys($values) as $name) { |
@@ -2760,7 +2760,7 @@ discard block |
||
| 2760 | 2760 | $supportedProperties = array_keys($this->subscriptionPropertyMap); |
| 2761 | 2761 | $supportedProperties[] = '{http://calendarserver.org/ns/}source'; |
| 2762 | 2762 | |
| 2763 | - $propPatch->handle($supportedProperties, function ($mutations) use ($subscriptionId) { |
|
| 2763 | + $propPatch->handle($supportedProperties, function($mutations) use ($subscriptionId) { |
|
| 2764 | 2764 | $newValues = []; |
| 2765 | 2765 | |
| 2766 | 2766 | foreach ($mutations as $propertyName => $propertyValue) { |
@@ -2772,7 +2772,7 @@ discard block |
||
| 2772 | 2772 | } |
| 2773 | 2773 | } |
| 2774 | 2774 | |
| 2775 | - $subscriptionRow = $this->atomic(function () use ($subscriptionId, $newValues) { |
|
| 2775 | + $subscriptionRow = $this->atomic(function() use ($subscriptionId, $newValues) { |
|
| 2776 | 2776 | $query = $this->db->getQueryBuilder(); |
| 2777 | 2777 | $query->update('calendarsubscriptions') |
| 2778 | 2778 | ->set('lastmodified', $query->createNamedParameter(time())); |
@@ -2785,7 +2785,7 @@ discard block |
||
| 2785 | 2785 | return $this->getSubscriptionById($subscriptionId); |
| 2786 | 2786 | }, $this->db); |
| 2787 | 2787 | |
| 2788 | - $this->dispatcher->dispatchTyped(new SubscriptionUpdatedEvent((int)$subscriptionId, $subscriptionRow, [], $mutations)); |
|
| 2788 | + $this->dispatcher->dispatchTyped(new SubscriptionUpdatedEvent((int) $subscriptionId, $subscriptionRow, [], $mutations)); |
|
| 2789 | 2789 | |
| 2790 | 2790 | return true; |
| 2791 | 2791 | }); |
@@ -2798,7 +2798,7 @@ discard block |
||
| 2798 | 2798 | * @return void |
| 2799 | 2799 | */ |
| 2800 | 2800 | public function deleteSubscription($subscriptionId) { |
| 2801 | - $this->atomic(function () use ($subscriptionId): void { |
|
| 2801 | + $this->atomic(function() use ($subscriptionId): void { |
|
| 2802 | 2802 | $subscriptionRow = $this->getSubscriptionById($subscriptionId); |
| 2803 | 2803 | |
| 2804 | 2804 | $query = $this->db->getQueryBuilder(); |
@@ -2823,7 +2823,7 @@ discard block |
||
| 2823 | 2823 | ->executeStatement(); |
| 2824 | 2824 | |
| 2825 | 2825 | if ($subscriptionRow) { |
| 2826 | - $this->dispatcher->dispatchTyped(new SubscriptionDeletedEvent((int)$subscriptionId, $subscriptionRow, [])); |
|
| 2826 | + $this->dispatcher->dispatchTyped(new SubscriptionDeletedEvent((int) $subscriptionId, $subscriptionRow, [])); |
|
| 2827 | 2827 | } |
| 2828 | 2828 | }, $this->db); |
| 2829 | 2829 | } |
@@ -2862,8 +2862,8 @@ discard block |
||
| 2862 | 2862 | 'uri' => $row['uri'], |
| 2863 | 2863 | 'calendardata' => $row['calendardata'], |
| 2864 | 2864 | 'lastmodified' => $row['lastmodified'], |
| 2865 | - 'etag' => '"' . $row['etag'] . '"', |
|
| 2866 | - 'size' => (int)$row['size'], |
|
| 2865 | + 'etag' => '"'.$row['etag'].'"', |
|
| 2866 | + 'size' => (int) $row['size'], |
|
| 2867 | 2867 | ]; |
| 2868 | 2868 | } |
| 2869 | 2869 | |
@@ -2891,8 +2891,8 @@ discard block |
||
| 2891 | 2891 | 'calendardata' => $row['calendardata'], |
| 2892 | 2892 | 'uri' => $row['uri'], |
| 2893 | 2893 | 'lastmodified' => $row['lastmodified'], |
| 2894 | - 'etag' => '"' . $row['etag'] . '"', |
|
| 2895 | - 'size' => (int)$row['size'], |
|
| 2894 | + 'etag' => '"'.$row['etag'].'"', |
|
| 2895 | + 'size' => (int) $row['size'], |
|
| 2896 | 2896 | ]; |
| 2897 | 2897 | } |
| 2898 | 2898 | $stmt->closeCursor(); |
@@ -2934,8 +2934,8 @@ discard block |
||
| 2934 | 2934 | if ($count === 0) { |
| 2935 | 2935 | return; |
| 2936 | 2936 | } |
| 2937 | - $ids = array_map(static function (array $id) { |
|
| 2938 | - return (int)$id[0]; |
|
| 2937 | + $ids = array_map(static function(array $id) { |
|
| 2938 | + return (int) $id[0]; |
|
| 2939 | 2939 | }, $result->fetchAll(\PDO::FETCH_NUM)); |
| 2940 | 2940 | $result->closeCursor(); |
| 2941 | 2941 | |
@@ -2988,15 +2988,15 @@ discard block |
||
| 2988 | 2988 | */ |
| 2989 | 2989 | protected function addChanges(int $calendarId, array $objectUris, int $operation, int $calendarType = self::CALENDAR_TYPE_CALENDAR): void { |
| 2990 | 2990 | $this->cachedObjects = []; |
| 2991 | - $table = $calendarType === self::CALENDAR_TYPE_CALENDAR ? 'calendars': 'calendarsubscriptions'; |
|
| 2991 | + $table = $calendarType === self::CALENDAR_TYPE_CALENDAR ? 'calendars' : 'calendarsubscriptions'; |
|
| 2992 | 2992 | |
| 2993 | - $this->atomic(function () use ($calendarId, $objectUris, $operation, $calendarType, $table): void { |
|
| 2993 | + $this->atomic(function() use ($calendarId, $objectUris, $operation, $calendarType, $table): void { |
|
| 2994 | 2994 | $query = $this->db->getQueryBuilder(); |
| 2995 | 2995 | $query->select('synctoken') |
| 2996 | 2996 | ->from($table) |
| 2997 | 2997 | ->where($query->expr()->eq('id', $query->createNamedParameter($calendarId))); |
| 2998 | 2998 | $result = $query->executeQuery(); |
| 2999 | - $syncToken = (int)$result->fetchOne(); |
|
| 2999 | + $syncToken = (int) $result->fetchOne(); |
|
| 3000 | 3000 | $result->closeCursor(); |
| 3001 | 3001 | |
| 3002 | 3002 | $query = $this->db->getQueryBuilder(); |
@@ -3025,7 +3025,7 @@ discard block |
||
| 3025 | 3025 | public function restoreChanges(int $calendarId, int $calendarType = self::CALENDAR_TYPE_CALENDAR): void { |
| 3026 | 3026 | $this->cachedObjects = []; |
| 3027 | 3027 | |
| 3028 | - $this->atomic(function () use ($calendarId, $calendarType): void { |
|
| 3028 | + $this->atomic(function() use ($calendarId, $calendarType): void { |
|
| 3029 | 3029 | $qbAdded = $this->db->getQueryBuilder(); |
| 3030 | 3030 | $qbAdded->select('uri') |
| 3031 | 3031 | ->from('calendarobjects') |
@@ -3055,7 +3055,7 @@ discard block |
||
| 3055 | 3055 | ) |
| 3056 | 3056 | ); |
| 3057 | 3057 | $resultDeleted = $qbDeleted->executeQuery(); |
| 3058 | - $deletedUris = array_map(function (string $uri) { |
|
| 3058 | + $deletedUris = array_map(function(string $uri) { |
|
| 3059 | 3059 | return str_replace('-deleted.ics', '.ics', $uri); |
| 3060 | 3060 | }, $resultDeleted->fetchAll(\PDO::FETCH_COLUMN)); |
| 3061 | 3061 | $resultDeleted->closeCursor(); |
@@ -3100,7 +3100,7 @@ discard block |
||
| 3100 | 3100 | // Track first component type and uid |
| 3101 | 3101 | if ($uid === null) { |
| 3102 | 3102 | $componentType = $component->name; |
| 3103 | - $uid = (string)$component->UID; |
|
| 3103 | + $uid = (string) $component->UID; |
|
| 3104 | 3104 | } |
| 3105 | 3105 | } |
| 3106 | 3106 | } |
@@ -3191,11 +3191,11 @@ discard block |
||
| 3191 | 3191 | * @param list<string> $remove |
| 3192 | 3192 | */ |
| 3193 | 3193 | public function updateShares(IShareable $shareable, array $add, array $remove): void { |
| 3194 | - $this->atomic(function () use ($shareable, $add, $remove): void { |
|
| 3194 | + $this->atomic(function() use ($shareable, $add, $remove): void { |
|
| 3195 | 3195 | $calendarId = $shareable->getResourceId(); |
| 3196 | 3196 | $calendarRow = $this->getCalendarById($calendarId); |
| 3197 | 3197 | if ($calendarRow === null) { |
| 3198 | - throw new \RuntimeException('Trying to update shares for non-existing calendar: ' . $calendarId); |
|
| 3198 | + throw new \RuntimeException('Trying to update shares for non-existing calendar: '.$calendarId); |
|
| 3199 | 3199 | } |
| 3200 | 3200 | $oldShares = $this->getShares($calendarId); |
| 3201 | 3201 | |
@@ -3222,7 +3222,7 @@ discard block |
||
| 3222 | 3222 | * @return string|null |
| 3223 | 3223 | */ |
| 3224 | 3224 | public function setPublishStatus($value, $calendar) { |
| 3225 | - return $this->atomic(function () use ($value, $calendar) { |
|
| 3225 | + return $this->atomic(function() use ($value, $calendar) { |
|
| 3226 | 3226 | $calendarId = $calendar->getResourceId(); |
| 3227 | 3227 | $calendarData = $this->getCalendarById($calendarId); |
| 3228 | 3228 | |
@@ -3289,7 +3289,7 @@ discard block |
||
| 3289 | 3289 | */ |
| 3290 | 3290 | public function updateProperties($calendarId, $objectUri, $calendarData, $calendarType = self::CALENDAR_TYPE_CALENDAR) { |
| 3291 | 3291 | $this->cachedObjects = []; |
| 3292 | - $this->atomic(function () use ($calendarId, $objectUri, $calendarData, $calendarType): void { |
|
| 3292 | + $this->atomic(function() use ($calendarId, $objectUri, $calendarData, $calendarType): void { |
|
| 3293 | 3293 | $objectId = $this->getCalendarObjectId($calendarId, $objectUri, $calendarType); |
| 3294 | 3294 | |
| 3295 | 3295 | try { |
@@ -3361,7 +3361,7 @@ discard block |
||
| 3361 | 3361 | * deletes all birthday calendars |
| 3362 | 3362 | */ |
| 3363 | 3363 | public function deleteAllBirthdayCalendars() { |
| 3364 | - $this->atomic(function (): void { |
|
| 3364 | + $this->atomic(function(): void { |
|
| 3365 | 3365 | $query = $this->db->getQueryBuilder(); |
| 3366 | 3366 | $result = $query->select(['id'])->from('calendars') |
| 3367 | 3367 | ->where($query->expr()->eq('uri', $query->createNamedParameter(BirthdayService::BIRTHDAY_CALENDAR_URI))) |
@@ -3381,7 +3381,7 @@ discard block |
||
| 3381 | 3381 | * @param $subscriptionId |
| 3382 | 3382 | */ |
| 3383 | 3383 | public function purgeAllCachedEventsForSubscription($subscriptionId) { |
| 3384 | - $this->atomic(function () use ($subscriptionId): void { |
|
| 3384 | + $this->atomic(function() use ($subscriptionId): void { |
|
| 3385 | 3385 | $query = $this->db->getQueryBuilder(); |
| 3386 | 3386 | $query->select('uri') |
| 3387 | 3387 | ->from('calendarobjects') |
@@ -3427,7 +3427,7 @@ discard block |
||
| 3427 | 3427 | return; |
| 3428 | 3428 | } |
| 3429 | 3429 | |
| 3430 | - $this->atomic(function () use ($subscriptionId, $calendarObjectIds, $calendarObjectUris): void { |
|
| 3430 | + $this->atomic(function() use ($subscriptionId, $calendarObjectIds, $calendarObjectUris): void { |
|
| 3431 | 3431 | foreach (array_chunk($calendarObjectIds, 1000) as $chunk) { |
| 3432 | 3432 | $query = $this->db->getQueryBuilder(); |
| 3433 | 3433 | $query->delete($this->dbObjectPropertiesTable) |
@@ -3520,10 +3520,10 @@ discard block |
||
| 3520 | 3520 | $result->closeCursor(); |
| 3521 | 3521 | |
| 3522 | 3522 | if (!isset($objectIds['id'])) { |
| 3523 | - throw new \InvalidArgumentException('Calendarobject does not exists: ' . $uri); |
|
| 3523 | + throw new \InvalidArgumentException('Calendarobject does not exists: '.$uri); |
|
| 3524 | 3524 | } |
| 3525 | 3525 | |
| 3526 | - return (int)$objectIds['id']; |
|
| 3526 | + return (int) $objectIds['id']; |
|
| 3527 | 3527 | } |
| 3528 | 3528 | |
| 3529 | 3529 | /** |
@@ -3539,7 +3539,7 @@ discard block |
||
| 3539 | 3539 | ->from('calendarchanges'); |
| 3540 | 3540 | |
| 3541 | 3541 | $result = $query->executeQuery(); |
| 3542 | - $maxId = (int)$result->fetchOne(); |
|
| 3542 | + $maxId = (int) $result->fetchOne(); |
|
| 3543 | 3543 | $result->closeCursor(); |
| 3544 | 3544 | if (!$maxId || $maxId < $keep) { |
| 3545 | 3545 | return 0; |
@@ -3577,8 +3577,8 @@ discard block |
||
| 3577 | 3577 | * |
| 3578 | 3578 | */ |
| 3579 | 3579 | private function addOwnerPrincipalToCalendar(array $calendarInfo): array { |
| 3580 | - $ownerPrincipalKey = '{' . \OCA\DAV\DAV\Sharing\Plugin::NS_OWNCLOUD . '}owner-principal'; |
|
| 3581 | - $displaynameKey = '{' . \OCA\DAV\DAV\Sharing\Plugin::NS_NEXTCLOUD . '}owner-displayname'; |
|
| 3580 | + $ownerPrincipalKey = '{'.\OCA\DAV\DAV\Sharing\Plugin::NS_OWNCLOUD.'}owner-principal'; |
|
| 3581 | + $displaynameKey = '{'.\OCA\DAV\DAV\Sharing\Plugin::NS_NEXTCLOUD.'}owner-displayname'; |
|
| 3582 | 3582 | if (isset($calendarInfo[$ownerPrincipalKey])) { |
| 3583 | 3583 | $uri = $calendarInfo[$ownerPrincipalKey]; |
| 3584 | 3584 | } else { |
@@ -3687,10 +3687,10 @@ discard block |
||
| 3687 | 3687 | } |
| 3688 | 3688 | |
| 3689 | 3689 | public function unshare(IShareable $shareable, string $principal): void { |
| 3690 | - $this->atomic(function () use ($shareable, $principal): void { |
|
| 3690 | + $this->atomic(function() use ($shareable, $principal): void { |
|
| 3691 | 3691 | $calendarData = $this->getCalendarById($shareable->getResourceId()); |
| 3692 | 3692 | if ($calendarData === null) { |
| 3693 | - throw new \RuntimeException('Trying to update shares for non-existing calendar: ' . $shareable->getResourceId()); |
|
| 3693 | + throw new \RuntimeException('Trying to update shares for non-existing calendar: '.$shareable->getResourceId()); |
|
| 3694 | 3694 | } |
| 3695 | 3695 | |
| 3696 | 3696 | $oldShares = $this->getShares($shareable->getResourceId()); |
@@ -31,373 +31,373 @@ |
||
| 31 | 31 | * @property CalDavBackend $caldavBackend |
| 32 | 32 | */ |
| 33 | 33 | class Calendar extends \Sabre\CalDAV\Calendar implements IRestorable, IShareable, IMoveTarget { |
| 34 | - protected IL10N $l10n; |
|
| 35 | - private bool $useTrashbin = true; |
|
| 36 | - |
|
| 37 | - public function __construct( |
|
| 38 | - BackendInterface $caldavBackend, |
|
| 39 | - $calendarInfo, |
|
| 40 | - IL10N $l10n, |
|
| 41 | - private IConfig $config, |
|
| 42 | - private LoggerInterface $logger, |
|
| 43 | - ) { |
|
| 44 | - // Convert deletion date to ISO8601 string |
|
| 45 | - if (isset($calendarInfo[TrashbinPlugin::PROPERTY_DELETED_AT])) { |
|
| 46 | - $calendarInfo[TrashbinPlugin::PROPERTY_DELETED_AT] = (new DateTimeImmutable()) |
|
| 47 | - ->setTimestamp($calendarInfo[TrashbinPlugin::PROPERTY_DELETED_AT]) |
|
| 48 | - ->format(DateTimeInterface::ATOM); |
|
| 49 | - } |
|
| 50 | - |
|
| 51 | - parent::__construct($caldavBackend, $calendarInfo); |
|
| 52 | - |
|
| 53 | - if ($this->getName() === BirthdayService::BIRTHDAY_CALENDAR_URI && strcasecmp($this->calendarInfo['{DAV:}displayname'], 'Contact birthdays') === 0) { |
|
| 54 | - $this->calendarInfo['{DAV:}displayname'] = $l10n->t('Contact birthdays'); |
|
| 55 | - } |
|
| 56 | - if ($this->getName() === CalDavBackend::PERSONAL_CALENDAR_URI && |
|
| 57 | - $this->calendarInfo['{DAV:}displayname'] === CalDavBackend::PERSONAL_CALENDAR_NAME) { |
|
| 58 | - $this->calendarInfo['{DAV:}displayname'] = $l10n->t('Personal'); |
|
| 59 | - } |
|
| 60 | - $this->l10n = $l10n; |
|
| 61 | - } |
|
| 62 | - |
|
| 63 | - /** |
|
| 64 | - * {@inheritdoc} |
|
| 65 | - * @throws Forbidden |
|
| 66 | - */ |
|
| 67 | - public function updateShares(array $add, array $remove): void { |
|
| 68 | - if ($this->isShared()) { |
|
| 69 | - throw new Forbidden(); |
|
| 70 | - } |
|
| 71 | - $this->caldavBackend->updateShares($this, $add, $remove); |
|
| 72 | - } |
|
| 73 | - |
|
| 74 | - /** |
|
| 75 | - * Returns the list of people whom this resource is shared with. |
|
| 76 | - * |
|
| 77 | - * Every element in this array should have the following properties: |
|
| 78 | - * * href - Often a mailto: address |
|
| 79 | - * * commonName - Optional, for example a first + last name |
|
| 80 | - * * status - See the Sabre\CalDAV\SharingPlugin::STATUS_ constants. |
|
| 81 | - * * readOnly - boolean |
|
| 82 | - * * summary - Optional, a description for the share |
|
| 83 | - * |
|
| 84 | - * @return list<array{href: string, commonName: string, status: int, readOnly: bool, '{http://owncloud.org/ns}principal': string, '{http://owncloud.org/ns}group-share': bool}> |
|
| 85 | - */ |
|
| 86 | - public function getShares(): array { |
|
| 87 | - if ($this->isShared()) { |
|
| 88 | - return []; |
|
| 89 | - } |
|
| 90 | - return $this->caldavBackend->getShares($this->getResourceId()); |
|
| 91 | - } |
|
| 92 | - |
|
| 93 | - public function getResourceId(): int { |
|
| 94 | - return $this->calendarInfo['id']; |
|
| 95 | - } |
|
| 96 | - |
|
| 97 | - /** |
|
| 98 | - * @return string |
|
| 99 | - */ |
|
| 100 | - public function getPrincipalURI() { |
|
| 101 | - return $this->calendarInfo['principaluri']; |
|
| 102 | - } |
|
| 103 | - |
|
| 104 | - /** |
|
| 105 | - * @param int $resourceId |
|
| 106 | - * @param list<array{privilege: string, principal: string, protected: bool}> $acl |
|
| 107 | - * @return list<array{privilege: string, principal: ?string, protected: bool}> |
|
| 108 | - */ |
|
| 109 | - public function getACL() { |
|
| 110 | - $acl = [ |
|
| 111 | - [ |
|
| 112 | - 'privilege' => '{DAV:}read', |
|
| 113 | - 'principal' => $this->getOwner(), |
|
| 114 | - 'protected' => true, |
|
| 115 | - ], |
|
| 116 | - [ |
|
| 117 | - 'privilege' => '{DAV:}read', |
|
| 118 | - 'principal' => $this->getOwner() . '/calendar-proxy-write', |
|
| 119 | - 'protected' => true, |
|
| 120 | - ], |
|
| 121 | - [ |
|
| 122 | - 'privilege' => '{DAV:}read', |
|
| 123 | - 'principal' => $this->getOwner() . '/calendar-proxy-read', |
|
| 124 | - 'protected' => true, |
|
| 125 | - ], |
|
| 126 | - ]; |
|
| 127 | - |
|
| 128 | - if ($this->getName() !== BirthdayService::BIRTHDAY_CALENDAR_URI) { |
|
| 129 | - $acl[] = [ |
|
| 130 | - 'privilege' => '{DAV:}write', |
|
| 131 | - 'principal' => $this->getOwner(), |
|
| 132 | - 'protected' => true, |
|
| 133 | - ]; |
|
| 134 | - $acl[] = [ |
|
| 135 | - 'privilege' => '{DAV:}write', |
|
| 136 | - 'principal' => $this->getOwner() . '/calendar-proxy-write', |
|
| 137 | - 'protected' => true, |
|
| 138 | - ]; |
|
| 139 | - } else { |
|
| 140 | - $acl[] = [ |
|
| 141 | - 'privilege' => '{DAV:}write-properties', |
|
| 142 | - 'principal' => $this->getOwner(), |
|
| 143 | - 'protected' => true, |
|
| 144 | - ]; |
|
| 145 | - $acl[] = [ |
|
| 146 | - 'privilege' => '{DAV:}write-properties', |
|
| 147 | - 'principal' => $this->getOwner() . '/calendar-proxy-write', |
|
| 148 | - 'protected' => true, |
|
| 149 | - ]; |
|
| 150 | - } |
|
| 151 | - |
|
| 152 | - $acl[] = [ |
|
| 153 | - 'privilege' => '{DAV:}write-properties', |
|
| 154 | - 'principal' => $this->getOwner() . '/calendar-proxy-read', |
|
| 155 | - 'protected' => true, |
|
| 156 | - ]; |
|
| 157 | - |
|
| 158 | - if (!$this->isShared()) { |
|
| 159 | - return $acl; |
|
| 160 | - } |
|
| 161 | - |
|
| 162 | - if ($this->getOwner() !== parent::getOwner()) { |
|
| 163 | - $acl[] = [ |
|
| 164 | - 'privilege' => '{DAV:}read', |
|
| 165 | - 'principal' => parent::getOwner(), |
|
| 166 | - 'protected' => true, |
|
| 167 | - ]; |
|
| 168 | - if ($this->canWrite()) { |
|
| 169 | - $acl[] = [ |
|
| 170 | - 'privilege' => '{DAV:}write', |
|
| 171 | - 'principal' => parent::getOwner(), |
|
| 172 | - 'protected' => true, |
|
| 173 | - ]; |
|
| 174 | - } else { |
|
| 175 | - $acl[] = [ |
|
| 176 | - 'privilege' => '{DAV:}write-properties', |
|
| 177 | - 'principal' => parent::getOwner(), |
|
| 178 | - 'protected' => true, |
|
| 179 | - ]; |
|
| 180 | - } |
|
| 181 | - } |
|
| 182 | - if ($this->isPublic()) { |
|
| 183 | - $acl[] = [ |
|
| 184 | - 'privilege' => '{DAV:}read', |
|
| 185 | - 'principal' => 'principals/system/public', |
|
| 186 | - 'protected' => true, |
|
| 187 | - ]; |
|
| 188 | - } |
|
| 189 | - |
|
| 190 | - $acl = $this->caldavBackend->applyShareAcl($this->getResourceId(), $acl); |
|
| 191 | - $allowedPrincipals = [ |
|
| 192 | - $this->getOwner(), |
|
| 193 | - $this->getOwner() . '/calendar-proxy-read', |
|
| 194 | - $this->getOwner() . '/calendar-proxy-write', |
|
| 195 | - parent::getOwner(), |
|
| 196 | - 'principals/system/public' |
|
| 197 | - ]; |
|
| 198 | - /** @var list<array{privilege: string, principal: string, protected: bool}> $acl */ |
|
| 199 | - $acl = array_filter($acl, function (array $rule) use ($allowedPrincipals): bool { |
|
| 200 | - return \in_array($rule['principal'], $allowedPrincipals, true); |
|
| 201 | - }); |
|
| 202 | - return $acl; |
|
| 203 | - } |
|
| 204 | - |
|
| 205 | - public function getChildACL() { |
|
| 206 | - return $this->getACL(); |
|
| 207 | - } |
|
| 208 | - |
|
| 209 | - public function getOwner(): ?string { |
|
| 210 | - if (isset($this->calendarInfo['{http://owncloud.org/ns}owner-principal'])) { |
|
| 211 | - return $this->calendarInfo['{http://owncloud.org/ns}owner-principal']; |
|
| 212 | - } |
|
| 213 | - return parent::getOwner(); |
|
| 214 | - } |
|
| 215 | - |
|
| 216 | - public function delete() { |
|
| 217 | - if ($this->isShared()) { |
|
| 218 | - $this->caldavBackend->unshare($this, 'principal:' . $this->getPrincipalURI()); |
|
| 219 | - return; |
|
| 220 | - } |
|
| 221 | - |
|
| 222 | - // Remember when a user deleted their birthday calendar |
|
| 223 | - // in order to not regenerate it on the next contacts change |
|
| 224 | - if ($this->getName() === BirthdayService::BIRTHDAY_CALENDAR_URI) { |
|
| 225 | - $principalURI = $this->getPrincipalURI(); |
|
| 226 | - $userId = substr($principalURI, 17); |
|
| 227 | - |
|
| 228 | - $this->config->setUserValue($userId, 'dav', 'generateBirthdayCalendar', 'no'); |
|
| 229 | - } |
|
| 230 | - |
|
| 231 | - $this->caldavBackend->deleteCalendar( |
|
| 232 | - $this->calendarInfo['id'], |
|
| 233 | - !$this->useTrashbin |
|
| 234 | - ); |
|
| 235 | - } |
|
| 236 | - |
|
| 237 | - public function propPatch(PropPatch $propPatch) { |
|
| 238 | - // parent::propPatch will only update calendars table |
|
| 239 | - // if calendar is shared, changes have to be made to the properties table |
|
| 240 | - if (!$this->isShared()) { |
|
| 241 | - parent::propPatch($propPatch); |
|
| 242 | - } |
|
| 243 | - } |
|
| 244 | - |
|
| 245 | - public function getChild($name) { |
|
| 246 | - $obj = $this->caldavBackend->getCalendarObject($this->calendarInfo['id'], $name); |
|
| 247 | - |
|
| 248 | - if (!$obj) { |
|
| 249 | - throw new NotFound('Calendar object not found'); |
|
| 250 | - } |
|
| 251 | - |
|
| 252 | - if ($obj['classification'] === CalDavBackend::CLASSIFICATION_PRIVATE && $this->isShared()) { |
|
| 253 | - throw new NotFound('Calendar object not found'); |
|
| 254 | - } |
|
| 255 | - |
|
| 256 | - $obj['acl'] = $this->getChildACL(); |
|
| 257 | - |
|
| 258 | - return new CalendarObject($this->caldavBackend, $this->l10n, $this->calendarInfo, $obj); |
|
| 259 | - } |
|
| 260 | - |
|
| 261 | - public function getChildren() { |
|
| 262 | - $objs = $this->caldavBackend->getCalendarObjects($this->calendarInfo['id']); |
|
| 263 | - $children = []; |
|
| 264 | - foreach ($objs as $obj) { |
|
| 265 | - if ($obj['classification'] === CalDavBackend::CLASSIFICATION_PRIVATE && $this->isShared()) { |
|
| 266 | - continue; |
|
| 267 | - } |
|
| 268 | - $obj['acl'] = $this->getChildACL(); |
|
| 269 | - $children[] = new CalendarObject($this->caldavBackend, $this->l10n, $this->calendarInfo, $obj); |
|
| 270 | - } |
|
| 271 | - return $children; |
|
| 272 | - } |
|
| 273 | - |
|
| 274 | - public function getMultipleChildren(array $paths) { |
|
| 275 | - $objs = $this->caldavBackend->getMultipleCalendarObjects($this->calendarInfo['id'], $paths); |
|
| 276 | - $children = []; |
|
| 277 | - foreach ($objs as $obj) { |
|
| 278 | - if ($obj['classification'] === CalDavBackend::CLASSIFICATION_PRIVATE && $this->isShared()) { |
|
| 279 | - continue; |
|
| 280 | - } |
|
| 281 | - $obj['acl'] = $this->getChildACL(); |
|
| 282 | - $children[] = new CalendarObject($this->caldavBackend, $this->l10n, $this->calendarInfo, $obj); |
|
| 283 | - } |
|
| 284 | - return $children; |
|
| 285 | - } |
|
| 286 | - |
|
| 287 | - public function childExists($name) { |
|
| 288 | - $obj = $this->caldavBackend->getCalendarObject($this->calendarInfo['id'], $name); |
|
| 289 | - if (!$obj) { |
|
| 290 | - return false; |
|
| 291 | - } |
|
| 292 | - if ($obj['classification'] === CalDavBackend::CLASSIFICATION_PRIVATE && $this->isShared()) { |
|
| 293 | - return false; |
|
| 294 | - } |
|
| 295 | - |
|
| 296 | - return true; |
|
| 297 | - } |
|
| 298 | - |
|
| 299 | - public function calendarQuery(array $filters) { |
|
| 300 | - $uris = $this->caldavBackend->calendarQuery($this->calendarInfo['id'], $filters); |
|
| 301 | - if ($this->isShared()) { |
|
| 302 | - return array_filter($uris, function ($uri) { |
|
| 303 | - return $this->childExists($uri); |
|
| 304 | - }); |
|
| 305 | - } |
|
| 306 | - |
|
| 307 | - return $uris; |
|
| 308 | - } |
|
| 309 | - |
|
| 310 | - /** |
|
| 311 | - * @param boolean $value |
|
| 312 | - * @return string|null |
|
| 313 | - */ |
|
| 314 | - public function setPublishStatus($value) { |
|
| 315 | - $publicUri = $this->caldavBackend->setPublishStatus($value, $this); |
|
| 316 | - $this->calendarInfo['publicuri'] = $publicUri; |
|
| 317 | - return $publicUri; |
|
| 318 | - } |
|
| 319 | - |
|
| 320 | - /** |
|
| 321 | - * @return mixed $value |
|
| 322 | - */ |
|
| 323 | - public function getPublishStatus() { |
|
| 324 | - return $this->caldavBackend->getPublishStatus($this); |
|
| 325 | - } |
|
| 326 | - |
|
| 327 | - public function canWrite() { |
|
| 328 | - if ($this->getName() === BirthdayService::BIRTHDAY_CALENDAR_URI) { |
|
| 329 | - return false; |
|
| 330 | - } |
|
| 331 | - |
|
| 332 | - if (isset($this->calendarInfo['{http://owncloud.org/ns}read-only'])) { |
|
| 333 | - return !$this->calendarInfo['{http://owncloud.org/ns}read-only']; |
|
| 334 | - } |
|
| 335 | - return true; |
|
| 336 | - } |
|
| 337 | - |
|
| 338 | - private function isPublic() { |
|
| 339 | - return isset($this->calendarInfo['{http://owncloud.org/ns}public']); |
|
| 340 | - } |
|
| 341 | - |
|
| 342 | - public function isShared() { |
|
| 343 | - if (!isset($this->calendarInfo['{http://owncloud.org/ns}owner-principal'])) { |
|
| 344 | - return false; |
|
| 345 | - } |
|
| 346 | - |
|
| 347 | - return $this->calendarInfo['{http://owncloud.org/ns}owner-principal'] !== $this->calendarInfo['principaluri']; |
|
| 348 | - } |
|
| 349 | - |
|
| 350 | - public function isSubscription() { |
|
| 351 | - return isset($this->calendarInfo['{http://calendarserver.org/ns/}source']); |
|
| 352 | - } |
|
| 353 | - |
|
| 354 | - public function isDeleted(): bool { |
|
| 355 | - if (!isset($this->calendarInfo[TrashbinPlugin::PROPERTY_DELETED_AT])) { |
|
| 356 | - return false; |
|
| 357 | - } |
|
| 358 | - return $this->calendarInfo[TrashbinPlugin::PROPERTY_DELETED_AT] !== null; |
|
| 359 | - } |
|
| 360 | - |
|
| 361 | - /** |
|
| 362 | - * @inheritDoc |
|
| 363 | - */ |
|
| 364 | - public function getChanges($syncToken, $syncLevel, $limit = null) { |
|
| 365 | - if (!$syncToken && $limit) { |
|
| 366 | - throw new UnsupportedLimitOnInitialSyncException(); |
|
| 367 | - } |
|
| 368 | - |
|
| 369 | - return parent::getChanges($syncToken, $syncLevel, $limit); |
|
| 370 | - } |
|
| 371 | - |
|
| 372 | - /** |
|
| 373 | - * @inheritDoc |
|
| 374 | - */ |
|
| 375 | - public function restore(): void { |
|
| 376 | - $this->caldavBackend->restoreCalendar((int)$this->calendarInfo['id']); |
|
| 377 | - } |
|
| 378 | - |
|
| 379 | - public function disableTrashbin(): void { |
|
| 380 | - $this->useTrashbin = false; |
|
| 381 | - } |
|
| 382 | - |
|
| 383 | - /** |
|
| 384 | - * @inheritDoc |
|
| 385 | - */ |
|
| 386 | - public function moveInto($targetName, $sourcePath, INode $sourceNode) { |
|
| 387 | - if (!($sourceNode instanceof CalendarObject)) { |
|
| 388 | - return false; |
|
| 389 | - } |
|
| 390 | - try { |
|
| 391 | - return $this->caldavBackend->moveCalendarObject( |
|
| 392 | - $sourceNode->getOwner(), |
|
| 393 | - $sourceNode->getId(), |
|
| 394 | - $this->getOwner(), |
|
| 395 | - $this->getResourceId(), |
|
| 396 | - $targetName, |
|
| 397 | - ); |
|
| 398 | - } catch (Exception $e) { |
|
| 399 | - $this->logger->error('Could not move calendar object: ' . $e->getMessage(), ['exception' => $e]); |
|
| 400 | - return false; |
|
| 401 | - } |
|
| 402 | - } |
|
| 34 | + protected IL10N $l10n; |
|
| 35 | + private bool $useTrashbin = true; |
|
| 36 | + |
|
| 37 | + public function __construct( |
|
| 38 | + BackendInterface $caldavBackend, |
|
| 39 | + $calendarInfo, |
|
| 40 | + IL10N $l10n, |
|
| 41 | + private IConfig $config, |
|
| 42 | + private LoggerInterface $logger, |
|
| 43 | + ) { |
|
| 44 | + // Convert deletion date to ISO8601 string |
|
| 45 | + if (isset($calendarInfo[TrashbinPlugin::PROPERTY_DELETED_AT])) { |
|
| 46 | + $calendarInfo[TrashbinPlugin::PROPERTY_DELETED_AT] = (new DateTimeImmutable()) |
|
| 47 | + ->setTimestamp($calendarInfo[TrashbinPlugin::PROPERTY_DELETED_AT]) |
|
| 48 | + ->format(DateTimeInterface::ATOM); |
|
| 49 | + } |
|
| 50 | + |
|
| 51 | + parent::__construct($caldavBackend, $calendarInfo); |
|
| 52 | + |
|
| 53 | + if ($this->getName() === BirthdayService::BIRTHDAY_CALENDAR_URI && strcasecmp($this->calendarInfo['{DAV:}displayname'], 'Contact birthdays') === 0) { |
|
| 54 | + $this->calendarInfo['{DAV:}displayname'] = $l10n->t('Contact birthdays'); |
|
| 55 | + } |
|
| 56 | + if ($this->getName() === CalDavBackend::PERSONAL_CALENDAR_URI && |
|
| 57 | + $this->calendarInfo['{DAV:}displayname'] === CalDavBackend::PERSONAL_CALENDAR_NAME) { |
|
| 58 | + $this->calendarInfo['{DAV:}displayname'] = $l10n->t('Personal'); |
|
| 59 | + } |
|
| 60 | + $this->l10n = $l10n; |
|
| 61 | + } |
|
| 62 | + |
|
| 63 | + /** |
|
| 64 | + * {@inheritdoc} |
|
| 65 | + * @throws Forbidden |
|
| 66 | + */ |
|
| 67 | + public function updateShares(array $add, array $remove): void { |
|
| 68 | + if ($this->isShared()) { |
|
| 69 | + throw new Forbidden(); |
|
| 70 | + } |
|
| 71 | + $this->caldavBackend->updateShares($this, $add, $remove); |
|
| 72 | + } |
|
| 73 | + |
|
| 74 | + /** |
|
| 75 | + * Returns the list of people whom this resource is shared with. |
|
| 76 | + * |
|
| 77 | + * Every element in this array should have the following properties: |
|
| 78 | + * * href - Often a mailto: address |
|
| 79 | + * * commonName - Optional, for example a first + last name |
|
| 80 | + * * status - See the Sabre\CalDAV\SharingPlugin::STATUS_ constants. |
|
| 81 | + * * readOnly - boolean |
|
| 82 | + * * summary - Optional, a description for the share |
|
| 83 | + * |
|
| 84 | + * @return list<array{href: string, commonName: string, status: int, readOnly: bool, '{http://owncloud.org/ns}principal': string, '{http://owncloud.org/ns}group-share': bool}> |
|
| 85 | + */ |
|
| 86 | + public function getShares(): array { |
|
| 87 | + if ($this->isShared()) { |
|
| 88 | + return []; |
|
| 89 | + } |
|
| 90 | + return $this->caldavBackend->getShares($this->getResourceId()); |
|
| 91 | + } |
|
| 92 | + |
|
| 93 | + public function getResourceId(): int { |
|
| 94 | + return $this->calendarInfo['id']; |
|
| 95 | + } |
|
| 96 | + |
|
| 97 | + /** |
|
| 98 | + * @return string |
|
| 99 | + */ |
|
| 100 | + public function getPrincipalURI() { |
|
| 101 | + return $this->calendarInfo['principaluri']; |
|
| 102 | + } |
|
| 103 | + |
|
| 104 | + /** |
|
| 105 | + * @param int $resourceId |
|
| 106 | + * @param list<array{privilege: string, principal: string, protected: bool}> $acl |
|
| 107 | + * @return list<array{privilege: string, principal: ?string, protected: bool}> |
|
| 108 | + */ |
|
| 109 | + public function getACL() { |
|
| 110 | + $acl = [ |
|
| 111 | + [ |
|
| 112 | + 'privilege' => '{DAV:}read', |
|
| 113 | + 'principal' => $this->getOwner(), |
|
| 114 | + 'protected' => true, |
|
| 115 | + ], |
|
| 116 | + [ |
|
| 117 | + 'privilege' => '{DAV:}read', |
|
| 118 | + 'principal' => $this->getOwner() . '/calendar-proxy-write', |
|
| 119 | + 'protected' => true, |
|
| 120 | + ], |
|
| 121 | + [ |
|
| 122 | + 'privilege' => '{DAV:}read', |
|
| 123 | + 'principal' => $this->getOwner() . '/calendar-proxy-read', |
|
| 124 | + 'protected' => true, |
|
| 125 | + ], |
|
| 126 | + ]; |
|
| 127 | + |
|
| 128 | + if ($this->getName() !== BirthdayService::BIRTHDAY_CALENDAR_URI) { |
|
| 129 | + $acl[] = [ |
|
| 130 | + 'privilege' => '{DAV:}write', |
|
| 131 | + 'principal' => $this->getOwner(), |
|
| 132 | + 'protected' => true, |
|
| 133 | + ]; |
|
| 134 | + $acl[] = [ |
|
| 135 | + 'privilege' => '{DAV:}write', |
|
| 136 | + 'principal' => $this->getOwner() . '/calendar-proxy-write', |
|
| 137 | + 'protected' => true, |
|
| 138 | + ]; |
|
| 139 | + } else { |
|
| 140 | + $acl[] = [ |
|
| 141 | + 'privilege' => '{DAV:}write-properties', |
|
| 142 | + 'principal' => $this->getOwner(), |
|
| 143 | + 'protected' => true, |
|
| 144 | + ]; |
|
| 145 | + $acl[] = [ |
|
| 146 | + 'privilege' => '{DAV:}write-properties', |
|
| 147 | + 'principal' => $this->getOwner() . '/calendar-proxy-write', |
|
| 148 | + 'protected' => true, |
|
| 149 | + ]; |
|
| 150 | + } |
|
| 151 | + |
|
| 152 | + $acl[] = [ |
|
| 153 | + 'privilege' => '{DAV:}write-properties', |
|
| 154 | + 'principal' => $this->getOwner() . '/calendar-proxy-read', |
|
| 155 | + 'protected' => true, |
|
| 156 | + ]; |
|
| 157 | + |
|
| 158 | + if (!$this->isShared()) { |
|
| 159 | + return $acl; |
|
| 160 | + } |
|
| 161 | + |
|
| 162 | + if ($this->getOwner() !== parent::getOwner()) { |
|
| 163 | + $acl[] = [ |
|
| 164 | + 'privilege' => '{DAV:}read', |
|
| 165 | + 'principal' => parent::getOwner(), |
|
| 166 | + 'protected' => true, |
|
| 167 | + ]; |
|
| 168 | + if ($this->canWrite()) { |
|
| 169 | + $acl[] = [ |
|
| 170 | + 'privilege' => '{DAV:}write', |
|
| 171 | + 'principal' => parent::getOwner(), |
|
| 172 | + 'protected' => true, |
|
| 173 | + ]; |
|
| 174 | + } else { |
|
| 175 | + $acl[] = [ |
|
| 176 | + 'privilege' => '{DAV:}write-properties', |
|
| 177 | + 'principal' => parent::getOwner(), |
|
| 178 | + 'protected' => true, |
|
| 179 | + ]; |
|
| 180 | + } |
|
| 181 | + } |
|
| 182 | + if ($this->isPublic()) { |
|
| 183 | + $acl[] = [ |
|
| 184 | + 'privilege' => '{DAV:}read', |
|
| 185 | + 'principal' => 'principals/system/public', |
|
| 186 | + 'protected' => true, |
|
| 187 | + ]; |
|
| 188 | + } |
|
| 189 | + |
|
| 190 | + $acl = $this->caldavBackend->applyShareAcl($this->getResourceId(), $acl); |
|
| 191 | + $allowedPrincipals = [ |
|
| 192 | + $this->getOwner(), |
|
| 193 | + $this->getOwner() . '/calendar-proxy-read', |
|
| 194 | + $this->getOwner() . '/calendar-proxy-write', |
|
| 195 | + parent::getOwner(), |
|
| 196 | + 'principals/system/public' |
|
| 197 | + ]; |
|
| 198 | + /** @var list<array{privilege: string, principal: string, protected: bool}> $acl */ |
|
| 199 | + $acl = array_filter($acl, function (array $rule) use ($allowedPrincipals): bool { |
|
| 200 | + return \in_array($rule['principal'], $allowedPrincipals, true); |
|
| 201 | + }); |
|
| 202 | + return $acl; |
|
| 203 | + } |
|
| 204 | + |
|
| 205 | + public function getChildACL() { |
|
| 206 | + return $this->getACL(); |
|
| 207 | + } |
|
| 208 | + |
|
| 209 | + public function getOwner(): ?string { |
|
| 210 | + if (isset($this->calendarInfo['{http://owncloud.org/ns}owner-principal'])) { |
|
| 211 | + return $this->calendarInfo['{http://owncloud.org/ns}owner-principal']; |
|
| 212 | + } |
|
| 213 | + return parent::getOwner(); |
|
| 214 | + } |
|
| 215 | + |
|
| 216 | + public function delete() { |
|
| 217 | + if ($this->isShared()) { |
|
| 218 | + $this->caldavBackend->unshare($this, 'principal:' . $this->getPrincipalURI()); |
|
| 219 | + return; |
|
| 220 | + } |
|
| 221 | + |
|
| 222 | + // Remember when a user deleted their birthday calendar |
|
| 223 | + // in order to not regenerate it on the next contacts change |
|
| 224 | + if ($this->getName() === BirthdayService::BIRTHDAY_CALENDAR_URI) { |
|
| 225 | + $principalURI = $this->getPrincipalURI(); |
|
| 226 | + $userId = substr($principalURI, 17); |
|
| 227 | + |
|
| 228 | + $this->config->setUserValue($userId, 'dav', 'generateBirthdayCalendar', 'no'); |
|
| 229 | + } |
|
| 230 | + |
|
| 231 | + $this->caldavBackend->deleteCalendar( |
|
| 232 | + $this->calendarInfo['id'], |
|
| 233 | + !$this->useTrashbin |
|
| 234 | + ); |
|
| 235 | + } |
|
| 236 | + |
|
| 237 | + public function propPatch(PropPatch $propPatch) { |
|
| 238 | + // parent::propPatch will only update calendars table |
|
| 239 | + // if calendar is shared, changes have to be made to the properties table |
|
| 240 | + if (!$this->isShared()) { |
|
| 241 | + parent::propPatch($propPatch); |
|
| 242 | + } |
|
| 243 | + } |
|
| 244 | + |
|
| 245 | + public function getChild($name) { |
|
| 246 | + $obj = $this->caldavBackend->getCalendarObject($this->calendarInfo['id'], $name); |
|
| 247 | + |
|
| 248 | + if (!$obj) { |
|
| 249 | + throw new NotFound('Calendar object not found'); |
|
| 250 | + } |
|
| 251 | + |
|
| 252 | + if ($obj['classification'] === CalDavBackend::CLASSIFICATION_PRIVATE && $this->isShared()) { |
|
| 253 | + throw new NotFound('Calendar object not found'); |
|
| 254 | + } |
|
| 255 | + |
|
| 256 | + $obj['acl'] = $this->getChildACL(); |
|
| 257 | + |
|
| 258 | + return new CalendarObject($this->caldavBackend, $this->l10n, $this->calendarInfo, $obj); |
|
| 259 | + } |
|
| 260 | + |
|
| 261 | + public function getChildren() { |
|
| 262 | + $objs = $this->caldavBackend->getCalendarObjects($this->calendarInfo['id']); |
|
| 263 | + $children = []; |
|
| 264 | + foreach ($objs as $obj) { |
|
| 265 | + if ($obj['classification'] === CalDavBackend::CLASSIFICATION_PRIVATE && $this->isShared()) { |
|
| 266 | + continue; |
|
| 267 | + } |
|
| 268 | + $obj['acl'] = $this->getChildACL(); |
|
| 269 | + $children[] = new CalendarObject($this->caldavBackend, $this->l10n, $this->calendarInfo, $obj); |
|
| 270 | + } |
|
| 271 | + return $children; |
|
| 272 | + } |
|
| 273 | + |
|
| 274 | + public function getMultipleChildren(array $paths) { |
|
| 275 | + $objs = $this->caldavBackend->getMultipleCalendarObjects($this->calendarInfo['id'], $paths); |
|
| 276 | + $children = []; |
|
| 277 | + foreach ($objs as $obj) { |
|
| 278 | + if ($obj['classification'] === CalDavBackend::CLASSIFICATION_PRIVATE && $this->isShared()) { |
|
| 279 | + continue; |
|
| 280 | + } |
|
| 281 | + $obj['acl'] = $this->getChildACL(); |
|
| 282 | + $children[] = new CalendarObject($this->caldavBackend, $this->l10n, $this->calendarInfo, $obj); |
|
| 283 | + } |
|
| 284 | + return $children; |
|
| 285 | + } |
|
| 286 | + |
|
| 287 | + public function childExists($name) { |
|
| 288 | + $obj = $this->caldavBackend->getCalendarObject($this->calendarInfo['id'], $name); |
|
| 289 | + if (!$obj) { |
|
| 290 | + return false; |
|
| 291 | + } |
|
| 292 | + if ($obj['classification'] === CalDavBackend::CLASSIFICATION_PRIVATE && $this->isShared()) { |
|
| 293 | + return false; |
|
| 294 | + } |
|
| 295 | + |
|
| 296 | + return true; |
|
| 297 | + } |
|
| 298 | + |
|
| 299 | + public function calendarQuery(array $filters) { |
|
| 300 | + $uris = $this->caldavBackend->calendarQuery($this->calendarInfo['id'], $filters); |
|
| 301 | + if ($this->isShared()) { |
|
| 302 | + return array_filter($uris, function ($uri) { |
|
| 303 | + return $this->childExists($uri); |
|
| 304 | + }); |
|
| 305 | + } |
|
| 306 | + |
|
| 307 | + return $uris; |
|
| 308 | + } |
|
| 309 | + |
|
| 310 | + /** |
|
| 311 | + * @param boolean $value |
|
| 312 | + * @return string|null |
|
| 313 | + */ |
|
| 314 | + public function setPublishStatus($value) { |
|
| 315 | + $publicUri = $this->caldavBackend->setPublishStatus($value, $this); |
|
| 316 | + $this->calendarInfo['publicuri'] = $publicUri; |
|
| 317 | + return $publicUri; |
|
| 318 | + } |
|
| 319 | + |
|
| 320 | + /** |
|
| 321 | + * @return mixed $value |
|
| 322 | + */ |
|
| 323 | + public function getPublishStatus() { |
|
| 324 | + return $this->caldavBackend->getPublishStatus($this); |
|
| 325 | + } |
|
| 326 | + |
|
| 327 | + public function canWrite() { |
|
| 328 | + if ($this->getName() === BirthdayService::BIRTHDAY_CALENDAR_URI) { |
|
| 329 | + return false; |
|
| 330 | + } |
|
| 331 | + |
|
| 332 | + if (isset($this->calendarInfo['{http://owncloud.org/ns}read-only'])) { |
|
| 333 | + return !$this->calendarInfo['{http://owncloud.org/ns}read-only']; |
|
| 334 | + } |
|
| 335 | + return true; |
|
| 336 | + } |
|
| 337 | + |
|
| 338 | + private function isPublic() { |
|
| 339 | + return isset($this->calendarInfo['{http://owncloud.org/ns}public']); |
|
| 340 | + } |
|
| 341 | + |
|
| 342 | + public function isShared() { |
|
| 343 | + if (!isset($this->calendarInfo['{http://owncloud.org/ns}owner-principal'])) { |
|
| 344 | + return false; |
|
| 345 | + } |
|
| 346 | + |
|
| 347 | + return $this->calendarInfo['{http://owncloud.org/ns}owner-principal'] !== $this->calendarInfo['principaluri']; |
|
| 348 | + } |
|
| 349 | + |
|
| 350 | + public function isSubscription() { |
|
| 351 | + return isset($this->calendarInfo['{http://calendarserver.org/ns/}source']); |
|
| 352 | + } |
|
| 353 | + |
|
| 354 | + public function isDeleted(): bool { |
|
| 355 | + if (!isset($this->calendarInfo[TrashbinPlugin::PROPERTY_DELETED_AT])) { |
|
| 356 | + return false; |
|
| 357 | + } |
|
| 358 | + return $this->calendarInfo[TrashbinPlugin::PROPERTY_DELETED_AT] !== null; |
|
| 359 | + } |
|
| 360 | + |
|
| 361 | + /** |
|
| 362 | + * @inheritDoc |
|
| 363 | + */ |
|
| 364 | + public function getChanges($syncToken, $syncLevel, $limit = null) { |
|
| 365 | + if (!$syncToken && $limit) { |
|
| 366 | + throw new UnsupportedLimitOnInitialSyncException(); |
|
| 367 | + } |
|
| 368 | + |
|
| 369 | + return parent::getChanges($syncToken, $syncLevel, $limit); |
|
| 370 | + } |
|
| 371 | + |
|
| 372 | + /** |
|
| 373 | + * @inheritDoc |
|
| 374 | + */ |
|
| 375 | + public function restore(): void { |
|
| 376 | + $this->caldavBackend->restoreCalendar((int)$this->calendarInfo['id']); |
|
| 377 | + } |
|
| 378 | + |
|
| 379 | + public function disableTrashbin(): void { |
|
| 380 | + $this->useTrashbin = false; |
|
| 381 | + } |
|
| 382 | + |
|
| 383 | + /** |
|
| 384 | + * @inheritDoc |
|
| 385 | + */ |
|
| 386 | + public function moveInto($targetName, $sourcePath, INode $sourceNode) { |
|
| 387 | + if (!($sourceNode instanceof CalendarObject)) { |
|
| 388 | + return false; |
|
| 389 | + } |
|
| 390 | + try { |
|
| 391 | + return $this->caldavBackend->moveCalendarObject( |
|
| 392 | + $sourceNode->getOwner(), |
|
| 393 | + $sourceNode->getId(), |
|
| 394 | + $this->getOwner(), |
|
| 395 | + $this->getResourceId(), |
|
| 396 | + $targetName, |
|
| 397 | + ); |
|
| 398 | + } catch (Exception $e) { |
|
| 399 | + $this->logger->error('Could not move calendar object: ' . $e->getMessage(), ['exception' => $e]); |
|
| 400 | + return false; |
|
| 401 | + } |
|
| 402 | + } |
|
| 403 | 403 | } |
@@ -115,12 +115,12 @@ discard block |
||
| 115 | 115 | ], |
| 116 | 116 | [ |
| 117 | 117 | 'privilege' => '{DAV:}read', |
| 118 | - 'principal' => $this->getOwner() . '/calendar-proxy-write', |
|
| 118 | + 'principal' => $this->getOwner().'/calendar-proxy-write', |
|
| 119 | 119 | 'protected' => true, |
| 120 | 120 | ], |
| 121 | 121 | [ |
| 122 | 122 | 'privilege' => '{DAV:}read', |
| 123 | - 'principal' => $this->getOwner() . '/calendar-proxy-read', |
|
| 123 | + 'principal' => $this->getOwner().'/calendar-proxy-read', |
|
| 124 | 124 | 'protected' => true, |
| 125 | 125 | ], |
| 126 | 126 | ]; |
@@ -133,7 +133,7 @@ discard block |
||
| 133 | 133 | ]; |
| 134 | 134 | $acl[] = [ |
| 135 | 135 | 'privilege' => '{DAV:}write', |
| 136 | - 'principal' => $this->getOwner() . '/calendar-proxy-write', |
|
| 136 | + 'principal' => $this->getOwner().'/calendar-proxy-write', |
|
| 137 | 137 | 'protected' => true, |
| 138 | 138 | ]; |
| 139 | 139 | } else { |
@@ -144,14 +144,14 @@ discard block |
||
| 144 | 144 | ]; |
| 145 | 145 | $acl[] = [ |
| 146 | 146 | 'privilege' => '{DAV:}write-properties', |
| 147 | - 'principal' => $this->getOwner() . '/calendar-proxy-write', |
|
| 147 | + 'principal' => $this->getOwner().'/calendar-proxy-write', |
|
| 148 | 148 | 'protected' => true, |
| 149 | 149 | ]; |
| 150 | 150 | } |
| 151 | 151 | |
| 152 | 152 | $acl[] = [ |
| 153 | 153 | 'privilege' => '{DAV:}write-properties', |
| 154 | - 'principal' => $this->getOwner() . '/calendar-proxy-read', |
|
| 154 | + 'principal' => $this->getOwner().'/calendar-proxy-read', |
|
| 155 | 155 | 'protected' => true, |
| 156 | 156 | ]; |
| 157 | 157 | |
@@ -190,13 +190,13 @@ discard block |
||
| 190 | 190 | $acl = $this->caldavBackend->applyShareAcl($this->getResourceId(), $acl); |
| 191 | 191 | $allowedPrincipals = [ |
| 192 | 192 | $this->getOwner(), |
| 193 | - $this->getOwner() . '/calendar-proxy-read', |
|
| 194 | - $this->getOwner() . '/calendar-proxy-write', |
|
| 193 | + $this->getOwner().'/calendar-proxy-read', |
|
| 194 | + $this->getOwner().'/calendar-proxy-write', |
|
| 195 | 195 | parent::getOwner(), |
| 196 | 196 | 'principals/system/public' |
| 197 | 197 | ]; |
| 198 | 198 | /** @var list<array{privilege: string, principal: string, protected: bool}> $acl */ |
| 199 | - $acl = array_filter($acl, function (array $rule) use ($allowedPrincipals): bool { |
|
| 199 | + $acl = array_filter($acl, function(array $rule) use ($allowedPrincipals): bool { |
|
| 200 | 200 | return \in_array($rule['principal'], $allowedPrincipals, true); |
| 201 | 201 | }); |
| 202 | 202 | return $acl; |
@@ -215,7 +215,7 @@ discard block |
||
| 215 | 215 | |
| 216 | 216 | public function delete() { |
| 217 | 217 | if ($this->isShared()) { |
| 218 | - $this->caldavBackend->unshare($this, 'principal:' . $this->getPrincipalURI()); |
|
| 218 | + $this->caldavBackend->unshare($this, 'principal:'.$this->getPrincipalURI()); |
|
| 219 | 219 | return; |
| 220 | 220 | } |
| 221 | 221 | |
@@ -299,7 +299,7 @@ discard block |
||
| 299 | 299 | public function calendarQuery(array $filters) { |
| 300 | 300 | $uris = $this->caldavBackend->calendarQuery($this->calendarInfo['id'], $filters); |
| 301 | 301 | if ($this->isShared()) { |
| 302 | - return array_filter($uris, function ($uri) { |
|
| 302 | + return array_filter($uris, function($uri) { |
|
| 303 | 303 | return $this->childExists($uri); |
| 304 | 304 | }); |
| 305 | 305 | } |
@@ -373,7 +373,7 @@ discard block |
||
| 373 | 373 | * @inheritDoc |
| 374 | 374 | */ |
| 375 | 375 | public function restore(): void { |
| 376 | - $this->caldavBackend->restoreCalendar((int)$this->calendarInfo['id']); |
|
| 376 | + $this->caldavBackend->restoreCalendar((int) $this->calendarInfo['id']); |
|
| 377 | 377 | } |
| 378 | 378 | |
| 379 | 379 | public function disableTrashbin(): void { |
@@ -396,7 +396,7 @@ discard block |
||
| 396 | 396 | $targetName, |
| 397 | 397 | ); |
| 398 | 398 | } catch (Exception $e) { |
| 399 | - $this->logger->error('Could not move calendar object: ' . $e->getMessage(), ['exception' => $e]); |
|
| 399 | + $this->logger->error('Could not move calendar object: '.$e->getMessage(), ['exception' => $e]); |
|
| 400 | 400 | return false; |
| 401 | 401 | } |
| 402 | 402 | } |