@@ -121,3071 +121,3071 @@ |
||
| 121 | 121 | */ |
| 122 | 122 | class CalDavBackend extends AbstractBackend implements SyncSupport, SubscriptionSupport, SchedulingSupport { |
| 123 | 123 | |
| 124 | - use TTransactional; |
|
| 125 | - |
|
| 126 | - public const CALENDAR_TYPE_CALENDAR = 0; |
|
| 127 | - public const CALENDAR_TYPE_SUBSCRIPTION = 1; |
|
| 128 | - |
|
| 129 | - public const PERSONAL_CALENDAR_URI = 'personal'; |
|
| 130 | - public const PERSONAL_CALENDAR_NAME = 'Personal'; |
|
| 131 | - |
|
| 132 | - public const RESOURCE_BOOKING_CALENDAR_URI = 'calendar'; |
|
| 133 | - public const RESOURCE_BOOKING_CALENDAR_NAME = 'Calendar'; |
|
| 134 | - |
|
| 135 | - /** |
|
| 136 | - * We need to specify a max date, because we need to stop *somewhere* |
|
| 137 | - * |
|
| 138 | - * On 32 bit system the maximum for a signed integer is 2147483647, so |
|
| 139 | - * MAX_DATE cannot be higher than date('Y-m-d', 2147483647) which results |
|
| 140 | - * in 2038-01-19 to avoid problems when the date is converted |
|
| 141 | - * to a unix timestamp. |
|
| 142 | - */ |
|
| 143 | - public const MAX_DATE = '2038-01-01'; |
|
| 144 | - |
|
| 145 | - public const ACCESS_PUBLIC = 4; |
|
| 146 | - public const CLASSIFICATION_PUBLIC = 0; |
|
| 147 | - public const CLASSIFICATION_PRIVATE = 1; |
|
| 148 | - public const CLASSIFICATION_CONFIDENTIAL = 2; |
|
| 149 | - |
|
| 150 | - /** |
|
| 151 | - * List of CalDAV properties, and how they map to database field names and their type |
|
| 152 | - * Add your own properties by simply adding on to this array. |
|
| 153 | - * |
|
| 154 | - * @var array |
|
| 155 | - * @psalm-var array<string, string[]> |
|
| 156 | - */ |
|
| 157 | - public array $propertyMap = [ |
|
| 158 | - '{DAV:}displayname' => ['displayname', 'string'], |
|
| 159 | - '{urn:ietf:params:xml:ns:caldav}calendar-description' => ['description', 'string'], |
|
| 160 | - '{urn:ietf:params:xml:ns:caldav}calendar-timezone' => ['timezone', 'string'], |
|
| 161 | - '{http://apple.com/ns/ical/}calendar-order' => ['calendarorder', 'int'], |
|
| 162 | - '{http://apple.com/ns/ical/}calendar-color' => ['calendarcolor', 'string'], |
|
| 163 | - '{' . \OCA\DAV\DAV\Sharing\Plugin::NS_NEXTCLOUD . '}deleted-at' => ['deleted_at', 'int'], |
|
| 164 | - ]; |
|
| 165 | - |
|
| 166 | - /** |
|
| 167 | - * List of subscription properties, and how they map to database field names. |
|
| 168 | - * |
|
| 169 | - * @var array |
|
| 170 | - */ |
|
| 171 | - public array $subscriptionPropertyMap = [ |
|
| 172 | - '{DAV:}displayname' => ['displayname', 'string'], |
|
| 173 | - '{http://apple.com/ns/ical/}refreshrate' => ['refreshrate', 'string'], |
|
| 174 | - '{http://apple.com/ns/ical/}calendar-order' => ['calendarorder', 'int'], |
|
| 175 | - '{http://apple.com/ns/ical/}calendar-color' => ['calendarcolor', 'string'], |
|
| 176 | - '{http://calendarserver.org/ns/}subscribed-strip-todos' => ['striptodos', 'bool'], |
|
| 177 | - '{http://calendarserver.org/ns/}subscribed-strip-alarms' => ['stripalarms', 'string'], |
|
| 178 | - '{http://calendarserver.org/ns/}subscribed-strip-attachments' => ['stripattachments', 'string'], |
|
| 179 | - ]; |
|
| 180 | - |
|
| 181 | - /** |
|
| 182 | - * properties to index |
|
| 183 | - * |
|
| 184 | - * This list has to be kept in sync with ICalendarQuery::SEARCH_PROPERTY_* |
|
| 185 | - * |
|
| 186 | - * @see \OCP\Calendar\ICalendarQuery |
|
| 187 | - */ |
|
| 188 | - private const INDEXED_PROPERTIES = [ |
|
| 189 | - 'CATEGORIES', |
|
| 190 | - 'COMMENT', |
|
| 191 | - 'DESCRIPTION', |
|
| 192 | - 'LOCATION', |
|
| 193 | - 'RESOURCES', |
|
| 194 | - 'STATUS', |
|
| 195 | - 'SUMMARY', |
|
| 196 | - 'ATTENDEE', |
|
| 197 | - 'CONTACT', |
|
| 198 | - 'ORGANIZER' |
|
| 199 | - ]; |
|
| 200 | - |
|
| 201 | - /** @var array parameters to index */ |
|
| 202 | - public static array $indexParameters = [ |
|
| 203 | - 'ATTENDEE' => ['CN'], |
|
| 204 | - 'ORGANIZER' => ['CN'], |
|
| 205 | - ]; |
|
| 206 | - |
|
| 207 | - /** |
|
| 208 | - * @var string[] Map of uid => display name |
|
| 209 | - */ |
|
| 210 | - protected array $userDisplayNames; |
|
| 211 | - |
|
| 212 | - private IDBConnection $db; |
|
| 213 | - private Backend $calendarSharingBackend; |
|
| 214 | - private Principal $principalBackend; |
|
| 215 | - private IUserManager $userManager; |
|
| 216 | - private ISecureRandom $random; |
|
| 217 | - private LoggerInterface $logger; |
|
| 218 | - private IEventDispatcher $dispatcher; |
|
| 219 | - private IConfig $config; |
|
| 220 | - private bool $legacyEndpoint; |
|
| 221 | - private string $dbObjectPropertiesTable = 'calendarobjects_props'; |
|
| 222 | - |
|
| 223 | - public function __construct(IDBConnection $db, |
|
| 224 | - Principal $principalBackend, |
|
| 225 | - IUserManager $userManager, |
|
| 226 | - IGroupManager $groupManager, |
|
| 227 | - ISecureRandom $random, |
|
| 228 | - LoggerInterface $logger, |
|
| 229 | - IEventDispatcher $dispatcher, |
|
| 230 | - IConfig $config, |
|
| 231 | - bool $legacyEndpoint = false) { |
|
| 232 | - $this->db = $db; |
|
| 233 | - $this->principalBackend = $principalBackend; |
|
| 234 | - $this->userManager = $userManager; |
|
| 235 | - $this->calendarSharingBackend = new Backend($this->db, $this->userManager, $groupManager, $principalBackend, 'calendar'); |
|
| 236 | - $this->random = $random; |
|
| 237 | - $this->logger = $logger; |
|
| 238 | - $this->dispatcher = $dispatcher; |
|
| 239 | - $this->config = $config; |
|
| 240 | - $this->legacyEndpoint = $legacyEndpoint; |
|
| 241 | - } |
|
| 242 | - |
|
| 243 | - /** |
|
| 244 | - * Return the number of calendars for a principal |
|
| 245 | - * |
|
| 246 | - * By default this excludes the automatically generated birthday calendar |
|
| 247 | - * |
|
| 248 | - * @param $principalUri |
|
| 249 | - * @param bool $excludeBirthday |
|
| 250 | - * @return int |
|
| 251 | - */ |
|
| 252 | - public function getCalendarsForUserCount($principalUri, $excludeBirthday = true) { |
|
| 253 | - $principalUri = $this->convertPrincipal($principalUri, true); |
|
| 254 | - $query = $this->db->getQueryBuilder(); |
|
| 255 | - $query->select($query->func()->count('*')) |
|
| 256 | - ->from('calendars'); |
|
| 257 | - |
|
| 258 | - if ($principalUri === '') { |
|
| 259 | - $query->where($query->expr()->emptyString('principaluri')); |
|
| 260 | - } else { |
|
| 261 | - $query->where($query->expr()->eq('principaluri', $query->createNamedParameter($principalUri))); |
|
| 262 | - } |
|
| 263 | - |
|
| 264 | - if ($excludeBirthday) { |
|
| 265 | - $query->andWhere($query->expr()->neq('uri', $query->createNamedParameter(BirthdayService::BIRTHDAY_CALENDAR_URI))); |
|
| 266 | - } |
|
| 267 | - |
|
| 268 | - $result = $query->executeQuery(); |
|
| 269 | - $column = (int)$result->fetchOne(); |
|
| 270 | - $result->closeCursor(); |
|
| 271 | - return $column; |
|
| 272 | - } |
|
| 273 | - |
|
| 274 | - /** |
|
| 275 | - * @return array{id: int, deleted_at: int}[] |
|
| 276 | - */ |
|
| 277 | - public function getDeletedCalendars(int $deletedBefore): array { |
|
| 278 | - $qb = $this->db->getQueryBuilder(); |
|
| 279 | - $qb->select(['id', 'deleted_at']) |
|
| 280 | - ->from('calendars') |
|
| 281 | - ->where($qb->expr()->isNotNull('deleted_at')) |
|
| 282 | - ->andWhere($qb->expr()->lt('deleted_at', $qb->createNamedParameter($deletedBefore))); |
|
| 283 | - $result = $qb->executeQuery(); |
|
| 284 | - $raw = $result->fetchAll(); |
|
| 285 | - $result->closeCursor(); |
|
| 286 | - return array_map(function ($row) { |
|
| 287 | - return [ |
|
| 288 | - 'id' => (int) $row['id'], |
|
| 289 | - 'deleted_at' => (int) $row['deleted_at'], |
|
| 290 | - ]; |
|
| 291 | - }, $raw); |
|
| 292 | - } |
|
| 293 | - |
|
| 294 | - /** |
|
| 295 | - * Returns a list of calendars for a principal. |
|
| 296 | - * |
|
| 297 | - * Every project is an array with the following keys: |
|
| 298 | - * * id, a unique id that will be used by other functions to modify the |
|
| 299 | - * calendar. This can be the same as the uri or a database key. |
|
| 300 | - * * uri, which the basename of the uri with which the calendar is |
|
| 301 | - * accessed. |
|
| 302 | - * * principaluri. The owner of the calendar. Almost always the same as |
|
| 303 | - * principalUri passed to this method. |
|
| 304 | - * |
|
| 305 | - * Furthermore it can contain webdav properties in clark notation. A very |
|
| 306 | - * common one is '{DAV:}displayname'. |
|
| 307 | - * |
|
| 308 | - * Many clients also require: |
|
| 309 | - * {urn:ietf:params:xml:ns:caldav}supported-calendar-component-set |
|
| 310 | - * For this property, you can just return an instance of |
|
| 311 | - * Sabre\CalDAV\Property\SupportedCalendarComponentSet. |
|
| 312 | - * |
|
| 313 | - * If you return {http://sabredav.org/ns}read-only and set the value to 1, |
|
| 314 | - * ACL will automatically be put in read-only mode. |
|
| 315 | - * |
|
| 316 | - * @param string $principalUri |
|
| 317 | - * @return array |
|
| 318 | - */ |
|
| 319 | - public function getCalendarsForUser($principalUri) { |
|
| 320 | - $principalUriOriginal = $principalUri; |
|
| 321 | - $principalUri = $this->convertPrincipal($principalUri, true); |
|
| 322 | - $fields = array_column($this->propertyMap, 0); |
|
| 323 | - $fields[] = 'id'; |
|
| 324 | - $fields[] = 'uri'; |
|
| 325 | - $fields[] = 'synctoken'; |
|
| 326 | - $fields[] = 'components'; |
|
| 327 | - $fields[] = 'principaluri'; |
|
| 328 | - $fields[] = 'transparent'; |
|
| 329 | - |
|
| 330 | - // Making fields a comma-delimited list |
|
| 331 | - $query = $this->db->getQueryBuilder(); |
|
| 332 | - $query->select($fields) |
|
| 333 | - ->from('calendars') |
|
| 334 | - ->orderBy('calendarorder', 'ASC'); |
|
| 335 | - |
|
| 336 | - if ($principalUri === '') { |
|
| 337 | - $query->where($query->expr()->emptyString('principaluri')); |
|
| 338 | - } else { |
|
| 339 | - $query->where($query->expr()->eq('principaluri', $query->createNamedParameter($principalUri))); |
|
| 340 | - } |
|
| 341 | - |
|
| 342 | - $result = $query->executeQuery(); |
|
| 343 | - |
|
| 344 | - $calendars = []; |
|
| 345 | - while ($row = $result->fetch()) { |
|
| 346 | - $row['principaluri'] = (string) $row['principaluri']; |
|
| 347 | - $components = []; |
|
| 348 | - if ($row['components']) { |
|
| 349 | - $components = explode(',',$row['components']); |
|
| 350 | - } |
|
| 351 | - |
|
| 352 | - $calendar = [ |
|
| 353 | - 'id' => $row['id'], |
|
| 354 | - 'uri' => $row['uri'], |
|
| 355 | - 'principaluri' => $this->convertPrincipal($row['principaluri'], !$this->legacyEndpoint), |
|
| 356 | - '{' . Plugin::NS_CALENDARSERVER . '}getctag' => 'http://sabre.io/ns/sync/' . ($row['synctoken']?$row['synctoken']:'0'), |
|
| 357 | - '{http://sabredav.org/ns}sync-token' => $row['synctoken']?$row['synctoken']:'0', |
|
| 358 | - '{' . Plugin::NS_CALDAV . '}supported-calendar-component-set' => new SupportedCalendarComponentSet($components), |
|
| 359 | - '{' . Plugin::NS_CALDAV . '}schedule-calendar-transp' => new ScheduleCalendarTransp($row['transparent']?'transparent':'opaque'), |
|
| 360 | - '{' . \OCA\DAV\DAV\Sharing\Plugin::NS_OWNCLOUD . '}owner-principal' => $this->convertPrincipal($principalUri, !$this->legacyEndpoint), |
|
| 361 | - ]; |
|
| 362 | - |
|
| 363 | - $calendar = $this->rowToCalendar($row, $calendar); |
|
| 364 | - $calendar = $this->addOwnerPrincipalToCalendar($calendar); |
|
| 365 | - $calendar = $this->addResourceTypeToCalendar($row, $calendar); |
|
| 366 | - |
|
| 367 | - if (!isset($calendars[$calendar['id']])) { |
|
| 368 | - $calendars[$calendar['id']] = $calendar; |
|
| 369 | - } |
|
| 370 | - } |
|
| 371 | - $result->closeCursor(); |
|
| 372 | - |
|
| 373 | - // query for shared calendars |
|
| 374 | - $principals = $this->principalBackend->getGroupMembership($principalUriOriginal, true); |
|
| 375 | - $principals = array_merge($principals, $this->principalBackend->getCircleMembership($principalUriOriginal)); |
|
| 376 | - |
|
| 377 | - $principals[] = $principalUri; |
|
| 378 | - |
|
| 379 | - $fields = array_column($this->propertyMap, 0); |
|
| 380 | - $fields[] = 'a.id'; |
|
| 381 | - $fields[] = 'a.uri'; |
|
| 382 | - $fields[] = 'a.synctoken'; |
|
| 383 | - $fields[] = 'a.components'; |
|
| 384 | - $fields[] = 'a.principaluri'; |
|
| 385 | - $fields[] = 'a.transparent'; |
|
| 386 | - $fields[] = 's.access'; |
|
| 387 | - $query = $this->db->getQueryBuilder(); |
|
| 388 | - $query->select($fields) |
|
| 389 | - ->from('dav_shares', 's') |
|
| 390 | - ->join('s', 'calendars', 'a', $query->expr()->eq('s.resourceid', 'a.id')) |
|
| 391 | - ->where($query->expr()->in('s.principaluri', $query->createParameter('principaluri'))) |
|
| 392 | - ->andWhere($query->expr()->eq('s.type', $query->createParameter('type'))) |
|
| 393 | - ->setParameter('type', 'calendar') |
|
| 394 | - ->setParameter('principaluri', $principals, \Doctrine\DBAL\Connection::PARAM_STR_ARRAY); |
|
| 395 | - |
|
| 396 | - $result = $query->executeQuery(); |
|
| 397 | - |
|
| 398 | - $readOnlyPropertyName = '{' . \OCA\DAV\DAV\Sharing\Plugin::NS_OWNCLOUD . '}read-only'; |
|
| 399 | - while ($row = $result->fetch()) { |
|
| 400 | - $row['principaluri'] = (string) $row['principaluri']; |
|
| 401 | - if ($row['principaluri'] === $principalUri) { |
|
| 402 | - continue; |
|
| 403 | - } |
|
| 404 | - |
|
| 405 | - $readOnly = (int) $row['access'] === Backend::ACCESS_READ; |
|
| 406 | - if (isset($calendars[$row['id']])) { |
|
| 407 | - if ($readOnly) { |
|
| 408 | - // New share can not have more permissions then the old one. |
|
| 409 | - continue; |
|
| 410 | - } |
|
| 411 | - if (isset($calendars[$row['id']][$readOnlyPropertyName]) && |
|
| 412 | - $calendars[$row['id']][$readOnlyPropertyName] === 0) { |
|
| 413 | - // Old share is already read-write, no more permissions can be gained |
|
| 414 | - continue; |
|
| 415 | - } |
|
| 416 | - } |
|
| 417 | - |
|
| 418 | - [, $name] = Uri\split($row['principaluri']); |
|
| 419 | - $uri = $row['uri'] . '_shared_by_' . $name; |
|
| 420 | - $row['displayname'] = $row['displayname'] . ' (' . ($this->userManager->getDisplayName($name) ?? ($name ?? '')) . ')'; |
|
| 421 | - $components = []; |
|
| 422 | - if ($row['components']) { |
|
| 423 | - $components = explode(',',$row['components']); |
|
| 424 | - } |
|
| 425 | - $calendar = [ |
|
| 426 | - 'id' => $row['id'], |
|
| 427 | - 'uri' => $uri, |
|
| 428 | - 'principaluri' => $this->convertPrincipal($principalUri, !$this->legacyEndpoint), |
|
| 429 | - '{' . Plugin::NS_CALENDARSERVER . '}getctag' => 'http://sabre.io/ns/sync/' . ($row['synctoken']?$row['synctoken']:'0'), |
|
| 430 | - '{http://sabredav.org/ns}sync-token' => $row['synctoken']?$row['synctoken']:'0', |
|
| 431 | - '{' . Plugin::NS_CALDAV . '}supported-calendar-component-set' => new SupportedCalendarComponentSet($components), |
|
| 432 | - '{' . Plugin::NS_CALDAV . '}schedule-calendar-transp' => new ScheduleCalendarTransp('transparent'), |
|
| 433 | - '{' . \OCA\DAV\DAV\Sharing\Plugin::NS_OWNCLOUD . '}owner-principal' => $this->convertPrincipal($row['principaluri'], !$this->legacyEndpoint), |
|
| 434 | - $readOnlyPropertyName => $readOnly, |
|
| 435 | - ]; |
|
| 436 | - |
|
| 437 | - $calendar = $this->rowToCalendar($row, $calendar); |
|
| 438 | - $calendar = $this->addOwnerPrincipalToCalendar($calendar); |
|
| 439 | - $calendar = $this->addResourceTypeToCalendar($row, $calendar); |
|
| 440 | - |
|
| 441 | - $calendars[$calendar['id']] = $calendar; |
|
| 442 | - } |
|
| 443 | - $result->closeCursor(); |
|
| 444 | - |
|
| 445 | - return array_values($calendars); |
|
| 446 | - } |
|
| 447 | - |
|
| 448 | - /** |
|
| 449 | - * @param $principalUri |
|
| 450 | - * @return array |
|
| 451 | - */ |
|
| 452 | - public function getUsersOwnCalendars($principalUri) { |
|
| 453 | - $principalUri = $this->convertPrincipal($principalUri, true); |
|
| 454 | - $fields = array_column($this->propertyMap, 0); |
|
| 455 | - $fields[] = 'id'; |
|
| 456 | - $fields[] = 'uri'; |
|
| 457 | - $fields[] = 'synctoken'; |
|
| 458 | - $fields[] = 'components'; |
|
| 459 | - $fields[] = 'principaluri'; |
|
| 460 | - $fields[] = 'transparent'; |
|
| 461 | - // Making fields a comma-delimited list |
|
| 462 | - $query = $this->db->getQueryBuilder(); |
|
| 463 | - $query->select($fields)->from('calendars') |
|
| 464 | - ->where($query->expr()->eq('principaluri', $query->createNamedParameter($principalUri))) |
|
| 465 | - ->orderBy('calendarorder', 'ASC'); |
|
| 466 | - $stmt = $query->executeQuery(); |
|
| 467 | - $calendars = []; |
|
| 468 | - while ($row = $stmt->fetch()) { |
|
| 469 | - $row['principaluri'] = (string) $row['principaluri']; |
|
| 470 | - $components = []; |
|
| 471 | - if ($row['components']) { |
|
| 472 | - $components = explode(',',$row['components']); |
|
| 473 | - } |
|
| 474 | - $calendar = [ |
|
| 475 | - 'id' => $row['id'], |
|
| 476 | - 'uri' => $row['uri'], |
|
| 477 | - 'principaluri' => $this->convertPrincipal($row['principaluri'], !$this->legacyEndpoint), |
|
| 478 | - '{' . Plugin::NS_CALENDARSERVER . '}getctag' => 'http://sabre.io/ns/sync/' . ($row['synctoken']?$row['synctoken']:'0'), |
|
| 479 | - '{http://sabredav.org/ns}sync-token' => $row['synctoken']?$row['synctoken']:'0', |
|
| 480 | - '{' . Plugin::NS_CALDAV . '}supported-calendar-component-set' => new SupportedCalendarComponentSet($components), |
|
| 481 | - '{' . Plugin::NS_CALDAV . '}schedule-calendar-transp' => new ScheduleCalendarTransp($row['transparent']?'transparent':'opaque'), |
|
| 482 | - ]; |
|
| 483 | - |
|
| 484 | - $calendar = $this->rowToCalendar($row, $calendar); |
|
| 485 | - $calendar = $this->addOwnerPrincipalToCalendar($calendar); |
|
| 486 | - $calendar = $this->addResourceTypeToCalendar($row, $calendar); |
|
| 487 | - |
|
| 488 | - if (!isset($calendars[$calendar['id']])) { |
|
| 489 | - $calendars[$calendar['id']] = $calendar; |
|
| 490 | - } |
|
| 491 | - } |
|
| 492 | - $stmt->closeCursor(); |
|
| 493 | - return array_values($calendars); |
|
| 494 | - } |
|
| 495 | - |
|
| 496 | - /** |
|
| 497 | - * @return array |
|
| 498 | - */ |
|
| 499 | - public function getPublicCalendars() { |
|
| 500 | - $fields = array_column($this->propertyMap, 0); |
|
| 501 | - $fields[] = 'a.id'; |
|
| 502 | - $fields[] = 'a.uri'; |
|
| 503 | - $fields[] = 'a.synctoken'; |
|
| 504 | - $fields[] = 'a.components'; |
|
| 505 | - $fields[] = 'a.principaluri'; |
|
| 506 | - $fields[] = 'a.transparent'; |
|
| 507 | - $fields[] = 's.access'; |
|
| 508 | - $fields[] = 's.publicuri'; |
|
| 509 | - $calendars = []; |
|
| 510 | - $query = $this->db->getQueryBuilder(); |
|
| 511 | - $result = $query->select($fields) |
|
| 512 | - ->from('dav_shares', 's') |
|
| 513 | - ->join('s', 'calendars', 'a', $query->expr()->eq('s.resourceid', 'a.id')) |
|
| 514 | - ->where($query->expr()->in('s.access', $query->createNamedParameter(self::ACCESS_PUBLIC))) |
|
| 515 | - ->andWhere($query->expr()->eq('s.type', $query->createNamedParameter('calendar'))) |
|
| 516 | - ->executeQuery(); |
|
| 517 | - |
|
| 518 | - while ($row = $result->fetch()) { |
|
| 519 | - $row['principaluri'] = (string) $row['principaluri']; |
|
| 520 | - [, $name] = Uri\split($row['principaluri']); |
|
| 521 | - $row['displayname'] = $row['displayname'] . "($name)"; |
|
| 522 | - $components = []; |
|
| 523 | - if ($row['components']) { |
|
| 524 | - $components = explode(',',$row['components']); |
|
| 525 | - } |
|
| 526 | - $calendar = [ |
|
| 527 | - 'id' => $row['id'], |
|
| 528 | - 'uri' => $row['publicuri'], |
|
| 529 | - 'principaluri' => $this->convertPrincipal($row['principaluri'], !$this->legacyEndpoint), |
|
| 530 | - '{' . Plugin::NS_CALENDARSERVER . '}getctag' => 'http://sabre.io/ns/sync/' . ($row['synctoken']?$row['synctoken']:'0'), |
|
| 531 | - '{http://sabredav.org/ns}sync-token' => $row['synctoken']?$row['synctoken']:'0', |
|
| 532 | - '{' . Plugin::NS_CALDAV . '}supported-calendar-component-set' => new SupportedCalendarComponentSet($components), |
|
| 533 | - '{' . Plugin::NS_CALDAV . '}schedule-calendar-transp' => new ScheduleCalendarTransp($row['transparent']?'transparent':'opaque'), |
|
| 534 | - '{' . \OCA\DAV\DAV\Sharing\Plugin::NS_OWNCLOUD . '}owner-principal' => $this->convertPrincipal($row['principaluri'], $this->legacyEndpoint), |
|
| 535 | - '{' . \OCA\DAV\DAV\Sharing\Plugin::NS_OWNCLOUD . '}read-only' => (int)$row['access'] === Backend::ACCESS_READ, |
|
| 536 | - '{' . \OCA\DAV\DAV\Sharing\Plugin::NS_OWNCLOUD . '}public' => (int)$row['access'] === self::ACCESS_PUBLIC, |
|
| 537 | - ]; |
|
| 538 | - |
|
| 539 | - $calendar = $this->rowToCalendar($row, $calendar); |
|
| 540 | - $calendar = $this->addOwnerPrincipalToCalendar($calendar); |
|
| 541 | - $calendar = $this->addResourceTypeToCalendar($row, $calendar); |
|
| 542 | - |
|
| 543 | - if (!isset($calendars[$calendar['id']])) { |
|
| 544 | - $calendars[$calendar['id']] = $calendar; |
|
| 545 | - } |
|
| 546 | - } |
|
| 547 | - $result->closeCursor(); |
|
| 548 | - |
|
| 549 | - return array_values($calendars); |
|
| 550 | - } |
|
| 551 | - |
|
| 552 | - /** |
|
| 553 | - * @param string $uri |
|
| 554 | - * @return array |
|
| 555 | - * @throws NotFound |
|
| 556 | - */ |
|
| 557 | - public function getPublicCalendar($uri) { |
|
| 558 | - $fields = array_column($this->propertyMap, 0); |
|
| 559 | - $fields[] = 'a.id'; |
|
| 560 | - $fields[] = 'a.uri'; |
|
| 561 | - $fields[] = 'a.synctoken'; |
|
| 562 | - $fields[] = 'a.components'; |
|
| 563 | - $fields[] = 'a.principaluri'; |
|
| 564 | - $fields[] = 'a.transparent'; |
|
| 565 | - $fields[] = 's.access'; |
|
| 566 | - $fields[] = 's.publicuri'; |
|
| 567 | - $query = $this->db->getQueryBuilder(); |
|
| 568 | - $result = $query->select($fields) |
|
| 569 | - ->from('dav_shares', 's') |
|
| 570 | - ->join('s', 'calendars', 'a', $query->expr()->eq('s.resourceid', 'a.id')) |
|
| 571 | - ->where($query->expr()->in('s.access', $query->createNamedParameter(self::ACCESS_PUBLIC))) |
|
| 572 | - ->andWhere($query->expr()->eq('s.type', $query->createNamedParameter('calendar'))) |
|
| 573 | - ->andWhere($query->expr()->eq('s.publicuri', $query->createNamedParameter($uri))) |
|
| 574 | - ->executeQuery(); |
|
| 575 | - |
|
| 576 | - $row = $result->fetch(); |
|
| 577 | - |
|
| 578 | - $result->closeCursor(); |
|
| 579 | - |
|
| 580 | - if ($row === false) { |
|
| 581 | - throw new NotFound('Node with name \'' . $uri . '\' could not be found'); |
|
| 582 | - } |
|
| 583 | - |
|
| 584 | - $row['principaluri'] = (string) $row['principaluri']; |
|
| 585 | - [, $name] = Uri\split($row['principaluri']); |
|
| 586 | - $row['displayname'] = $row['displayname'] . ' ' . "($name)"; |
|
| 587 | - $components = []; |
|
| 588 | - if ($row['components']) { |
|
| 589 | - $components = explode(',',$row['components']); |
|
| 590 | - } |
|
| 591 | - $calendar = [ |
|
| 592 | - 'id' => $row['id'], |
|
| 593 | - 'uri' => $row['publicuri'], |
|
| 594 | - 'principaluri' => $this->convertPrincipal($row['principaluri'], !$this->legacyEndpoint), |
|
| 595 | - '{' . Plugin::NS_CALENDARSERVER . '}getctag' => 'http://sabre.io/ns/sync/' . ($row['synctoken']?$row['synctoken']:'0'), |
|
| 596 | - '{http://sabredav.org/ns}sync-token' => $row['synctoken']?$row['synctoken']:'0', |
|
| 597 | - '{' . Plugin::NS_CALDAV . '}supported-calendar-component-set' => new SupportedCalendarComponentSet($components), |
|
| 598 | - '{' . Plugin::NS_CALDAV . '}schedule-calendar-transp' => new ScheduleCalendarTransp($row['transparent']?'transparent':'opaque'), |
|
| 599 | - '{' . \OCA\DAV\DAV\Sharing\Plugin::NS_OWNCLOUD . '}owner-principal' => $this->convertPrincipal($row['principaluri'], !$this->legacyEndpoint), |
|
| 600 | - '{' . \OCA\DAV\DAV\Sharing\Plugin::NS_OWNCLOUD . '}read-only' => (int)$row['access'] === Backend::ACCESS_READ, |
|
| 601 | - '{' . \OCA\DAV\DAV\Sharing\Plugin::NS_OWNCLOUD . '}public' => (int)$row['access'] === self::ACCESS_PUBLIC, |
|
| 602 | - ]; |
|
| 603 | - |
|
| 604 | - $calendar = $this->rowToCalendar($row, $calendar); |
|
| 605 | - $calendar = $this->addOwnerPrincipalToCalendar($calendar); |
|
| 606 | - $calendar = $this->addResourceTypeToCalendar($row, $calendar); |
|
| 607 | - |
|
| 608 | - return $calendar; |
|
| 609 | - } |
|
| 610 | - |
|
| 611 | - /** |
|
| 612 | - * @param string $principal |
|
| 613 | - * @param string $uri |
|
| 614 | - * @return array|null |
|
| 615 | - */ |
|
| 616 | - public function getCalendarByUri($principal, $uri) { |
|
| 617 | - $fields = array_column($this->propertyMap, 0); |
|
| 618 | - $fields[] = 'id'; |
|
| 619 | - $fields[] = 'uri'; |
|
| 620 | - $fields[] = 'synctoken'; |
|
| 621 | - $fields[] = 'components'; |
|
| 622 | - $fields[] = 'principaluri'; |
|
| 623 | - $fields[] = 'transparent'; |
|
| 624 | - |
|
| 625 | - // Making fields a comma-delimited list |
|
| 626 | - $query = $this->db->getQueryBuilder(); |
|
| 627 | - $query->select($fields)->from('calendars') |
|
| 628 | - ->where($query->expr()->eq('uri', $query->createNamedParameter($uri))) |
|
| 629 | - ->andWhere($query->expr()->eq('principaluri', $query->createNamedParameter($principal))) |
|
| 630 | - ->setMaxResults(1); |
|
| 631 | - $stmt = $query->executeQuery(); |
|
| 632 | - |
|
| 633 | - $row = $stmt->fetch(); |
|
| 634 | - $stmt->closeCursor(); |
|
| 635 | - if ($row === false) { |
|
| 636 | - return null; |
|
| 637 | - } |
|
| 638 | - |
|
| 639 | - $row['principaluri'] = (string) $row['principaluri']; |
|
| 640 | - $components = []; |
|
| 641 | - if ($row['components']) { |
|
| 642 | - $components = explode(',',$row['components']); |
|
| 643 | - } |
|
| 644 | - |
|
| 645 | - $calendar = [ |
|
| 646 | - 'id' => $row['id'], |
|
| 647 | - 'uri' => $row['uri'], |
|
| 648 | - 'principaluri' => $this->convertPrincipal($row['principaluri'], !$this->legacyEndpoint), |
|
| 649 | - '{' . Plugin::NS_CALENDARSERVER . '}getctag' => 'http://sabre.io/ns/sync/' . ($row['synctoken']?$row['synctoken']:'0'), |
|
| 650 | - '{http://sabredav.org/ns}sync-token' => $row['synctoken']?$row['synctoken']:'0', |
|
| 651 | - '{' . Plugin::NS_CALDAV . '}supported-calendar-component-set' => new SupportedCalendarComponentSet($components), |
|
| 652 | - '{' . Plugin::NS_CALDAV . '}schedule-calendar-transp' => new ScheduleCalendarTransp($row['transparent']?'transparent':'opaque'), |
|
| 653 | - ]; |
|
| 654 | - |
|
| 655 | - $calendar = $this->rowToCalendar($row, $calendar); |
|
| 656 | - $calendar = $this->addOwnerPrincipalToCalendar($calendar); |
|
| 657 | - $calendar = $this->addResourceTypeToCalendar($row, $calendar); |
|
| 658 | - |
|
| 659 | - return $calendar; |
|
| 660 | - } |
|
| 661 | - |
|
| 662 | - /** |
|
| 663 | - * @return array{id: int, uri: string, '{http://calendarserver.org/ns/}getctag': string, '{http://sabredav.org/ns}sync-token': int, '{urn:ietf:params:xml:ns:caldav}supported-calendar-component-set': SupportedCalendarComponentSet, '{urn:ietf:params:xml:ns:caldav}schedule-calendar-transp': ScheduleCalendarTransp }|null |
|
| 664 | - */ |
|
| 665 | - public function getCalendarById(int $calendarId): ?array { |
|
| 666 | - $fields = array_column($this->propertyMap, 0); |
|
| 667 | - $fields[] = 'id'; |
|
| 668 | - $fields[] = 'uri'; |
|
| 669 | - $fields[] = 'synctoken'; |
|
| 670 | - $fields[] = 'components'; |
|
| 671 | - $fields[] = 'principaluri'; |
|
| 672 | - $fields[] = 'transparent'; |
|
| 673 | - |
|
| 674 | - // Making fields a comma-delimited list |
|
| 675 | - $query = $this->db->getQueryBuilder(); |
|
| 676 | - $query->select($fields)->from('calendars') |
|
| 677 | - ->where($query->expr()->eq('id', $query->createNamedParameter($calendarId))) |
|
| 678 | - ->setMaxResults(1); |
|
| 679 | - $stmt = $query->executeQuery(); |
|
| 680 | - |
|
| 681 | - $row = $stmt->fetch(); |
|
| 682 | - $stmt->closeCursor(); |
|
| 683 | - if ($row === false) { |
|
| 684 | - return null; |
|
| 685 | - } |
|
| 686 | - |
|
| 687 | - $row['principaluri'] = (string) $row['principaluri']; |
|
| 688 | - $components = []; |
|
| 689 | - if ($row['components']) { |
|
| 690 | - $components = explode(',',$row['components']); |
|
| 691 | - } |
|
| 692 | - |
|
| 693 | - $calendar = [ |
|
| 694 | - 'id' => $row['id'], |
|
| 695 | - 'uri' => $row['uri'], |
|
| 696 | - 'principaluri' => $this->convertPrincipal($row['principaluri'], !$this->legacyEndpoint), |
|
| 697 | - '{' . Plugin::NS_CALENDARSERVER . '}getctag' => 'http://sabre.io/ns/sync/' . ($row['synctoken']?$row['synctoken']:'0'), |
|
| 698 | - '{http://sabredav.org/ns}sync-token' => $row['synctoken'] ?? 0, |
|
| 699 | - '{' . Plugin::NS_CALDAV . '}supported-calendar-component-set' => new SupportedCalendarComponentSet($components), |
|
| 700 | - '{' . Plugin::NS_CALDAV . '}schedule-calendar-transp' => new ScheduleCalendarTransp($row['transparent']?'transparent':'opaque'), |
|
| 701 | - ]; |
|
| 702 | - |
|
| 703 | - $calendar = $this->rowToCalendar($row, $calendar); |
|
| 704 | - $calendar = $this->addOwnerPrincipalToCalendar($calendar); |
|
| 705 | - $calendar = $this->addResourceTypeToCalendar($row, $calendar); |
|
| 706 | - |
|
| 707 | - return $calendar; |
|
| 708 | - } |
|
| 709 | - |
|
| 710 | - /** |
|
| 711 | - * @param $subscriptionId |
|
| 712 | - */ |
|
| 713 | - public function getSubscriptionById($subscriptionId) { |
|
| 714 | - $fields = array_column($this->subscriptionPropertyMap, 0); |
|
| 715 | - $fields[] = 'id'; |
|
| 716 | - $fields[] = 'uri'; |
|
| 717 | - $fields[] = 'source'; |
|
| 718 | - $fields[] = 'synctoken'; |
|
| 719 | - $fields[] = 'principaluri'; |
|
| 720 | - $fields[] = 'lastmodified'; |
|
| 721 | - |
|
| 722 | - $query = $this->db->getQueryBuilder(); |
|
| 723 | - $query->select($fields) |
|
| 724 | - ->from('calendarsubscriptions') |
|
| 725 | - ->where($query->expr()->eq('id', $query->createNamedParameter($subscriptionId))) |
|
| 726 | - ->orderBy('calendarorder', 'asc'); |
|
| 727 | - $stmt = $query->executeQuery(); |
|
| 728 | - |
|
| 729 | - $row = $stmt->fetch(); |
|
| 730 | - $stmt->closeCursor(); |
|
| 731 | - if ($row === false) { |
|
| 732 | - return null; |
|
| 733 | - } |
|
| 734 | - |
|
| 735 | - $row['principaluri'] = (string) $row['principaluri']; |
|
| 736 | - $subscription = [ |
|
| 737 | - 'id' => $row['id'], |
|
| 738 | - 'uri' => $row['uri'], |
|
| 739 | - 'principaluri' => $row['principaluri'], |
|
| 740 | - 'source' => $row['source'], |
|
| 741 | - 'lastmodified' => $row['lastmodified'], |
|
| 742 | - '{' . Plugin::NS_CALDAV . '}supported-calendar-component-set' => new SupportedCalendarComponentSet(['VTODO', 'VEVENT']), |
|
| 743 | - '{http://sabredav.org/ns}sync-token' => $row['synctoken']?$row['synctoken']:'0', |
|
| 744 | - ]; |
|
| 745 | - |
|
| 746 | - return $this->rowToSubscription($row, $subscription); |
|
| 747 | - } |
|
| 748 | - |
|
| 749 | - /** |
|
| 750 | - * Creates a new calendar for a principal. |
|
| 751 | - * |
|
| 752 | - * If the creation was a success, an id must be returned that can be used to reference |
|
| 753 | - * this calendar in other methods, such as updateCalendar. |
|
| 754 | - * |
|
| 755 | - * @param string $principalUri |
|
| 756 | - * @param string $calendarUri |
|
| 757 | - * @param array $properties |
|
| 758 | - * @return int |
|
| 759 | - * |
|
| 760 | - * @throws CalendarException |
|
| 761 | - */ |
|
| 762 | - public function createCalendar($principalUri, $calendarUri, array $properties) { |
|
| 763 | - if (strlen($calendarUri) > 255) { |
|
| 764 | - throw new CalendarException('URI too long. Calendar not created'); |
|
| 765 | - } |
|
| 766 | - |
|
| 767 | - $values = [ |
|
| 768 | - 'principaluri' => $this->convertPrincipal($principalUri, true), |
|
| 769 | - 'uri' => $calendarUri, |
|
| 770 | - 'synctoken' => 1, |
|
| 771 | - 'transparent' => 0, |
|
| 772 | - 'components' => 'VEVENT,VTODO', |
|
| 773 | - 'displayname' => $calendarUri |
|
| 774 | - ]; |
|
| 775 | - |
|
| 776 | - // Default value |
|
| 777 | - $sccs = '{urn:ietf:params:xml:ns:caldav}supported-calendar-component-set'; |
|
| 778 | - if (isset($properties[$sccs])) { |
|
| 779 | - if (!($properties[$sccs] instanceof SupportedCalendarComponentSet)) { |
|
| 780 | - throw new DAV\Exception('The ' . $sccs . ' property must be of type: \Sabre\CalDAV\Property\SupportedCalendarComponentSet'); |
|
| 781 | - } |
|
| 782 | - $values['components'] = implode(',',$properties[$sccs]->getValue()); |
|
| 783 | - } elseif (isset($properties['components'])) { |
|
| 784 | - // Allow to provide components internally without having |
|
| 785 | - // to create a SupportedCalendarComponentSet object |
|
| 786 | - $values['components'] = $properties['components']; |
|
| 787 | - } |
|
| 788 | - |
|
| 789 | - $transp = '{' . Plugin::NS_CALDAV . '}schedule-calendar-transp'; |
|
| 790 | - if (isset($properties[$transp])) { |
|
| 791 | - $values['transparent'] = (int) ($properties[$transp]->getValue() === 'transparent'); |
|
| 792 | - } |
|
| 793 | - |
|
| 794 | - foreach ($this->propertyMap as $xmlName => [$dbName, $type]) { |
|
| 795 | - if (isset($properties[$xmlName])) { |
|
| 796 | - $values[$dbName] = $properties[$xmlName]; |
|
| 797 | - } |
|
| 798 | - } |
|
| 799 | - |
|
| 800 | - [$calendarId, $calendarData] = $this->atomic(function() use ($values) { |
|
| 801 | - $query = $this->db->getQueryBuilder(); |
|
| 802 | - $query->insert('calendars'); |
|
| 803 | - foreach ($values as $column => $value) { |
|
| 804 | - $query->setValue($column, $query->createNamedParameter($value)); |
|
| 805 | - } |
|
| 806 | - $query->executeStatement(); |
|
| 807 | - $calendarId = $query->getLastInsertId(); |
|
| 808 | - |
|
| 809 | - $calendarData = $this->getCalendarById($calendarId); |
|
| 810 | - return [$calendarId, $calendarData]; |
|
| 811 | - }, $this->db); |
|
| 812 | - |
|
| 813 | - $this->dispatcher->dispatchTyped(new CalendarCreatedEvent((int)$calendarId, $calendarData)); |
|
| 814 | - |
|
| 815 | - return $calendarId; |
|
| 816 | - } |
|
| 817 | - |
|
| 818 | - /** |
|
| 819 | - * Updates properties for a calendar. |
|
| 820 | - * |
|
| 821 | - * The list of mutations is stored in a Sabre\DAV\PropPatch object. |
|
| 822 | - * To do the actual updates, you must tell this object which properties |
|
| 823 | - * you're going to process with the handle() method. |
|
| 824 | - * |
|
| 825 | - * Calling the handle method is like telling the PropPatch object "I |
|
| 826 | - * promise I can handle updating this property". |
|
| 827 | - * |
|
| 828 | - * Read the PropPatch documentation for more info and examples. |
|
| 829 | - * |
|
| 830 | - * @param mixed $calendarId |
|
| 831 | - * @param PropPatch $propPatch |
|
| 832 | - * @return void |
|
| 833 | - */ |
|
| 834 | - public function updateCalendar($calendarId, PropPatch $propPatch) { |
|
| 835 | - $supportedProperties = array_keys($this->propertyMap); |
|
| 836 | - $supportedProperties[] = '{' . Plugin::NS_CALDAV . '}schedule-calendar-transp'; |
|
| 837 | - |
|
| 838 | - $propPatch->handle($supportedProperties, function ($mutations) use ($calendarId) { |
|
| 839 | - $newValues = []; |
|
| 840 | - foreach ($mutations as $propertyName => $propertyValue) { |
|
| 841 | - switch ($propertyName) { |
|
| 842 | - case '{' . Plugin::NS_CALDAV . '}schedule-calendar-transp': |
|
| 843 | - $fieldName = 'transparent'; |
|
| 844 | - $newValues[$fieldName] = (int) ($propertyValue->getValue() === 'transparent'); |
|
| 845 | - break; |
|
| 846 | - default: |
|
| 847 | - $fieldName = $this->propertyMap[$propertyName][0]; |
|
| 848 | - $newValues[$fieldName] = $propertyValue; |
|
| 849 | - break; |
|
| 850 | - } |
|
| 851 | - } |
|
| 852 | - $query = $this->db->getQueryBuilder(); |
|
| 853 | - $query->update('calendars'); |
|
| 854 | - foreach ($newValues as $fieldName => $value) { |
|
| 855 | - $query->set($fieldName, $query->createNamedParameter($value)); |
|
| 856 | - } |
|
| 857 | - $query->where($query->expr()->eq('id', $query->createNamedParameter($calendarId))); |
|
| 858 | - $query->executeStatement(); |
|
| 859 | - |
|
| 860 | - $this->addChange($calendarId, "", 2); |
|
| 861 | - |
|
| 862 | - $calendarData = $this->getCalendarById($calendarId); |
|
| 863 | - $shares = $this->getShares($calendarId); |
|
| 864 | - $this->dispatcher->dispatchTyped(new CalendarUpdatedEvent($calendarId, $calendarData, $shares, $mutations)); |
|
| 865 | - |
|
| 866 | - return true; |
|
| 867 | - }); |
|
| 868 | - } |
|
| 869 | - |
|
| 870 | - /** |
|
| 871 | - * Delete a calendar and all it's objects |
|
| 872 | - * |
|
| 873 | - * @param mixed $calendarId |
|
| 874 | - * @return void |
|
| 875 | - */ |
|
| 876 | - public function deleteCalendar($calendarId, bool $forceDeletePermanently = false) { |
|
| 877 | - // The calendar is deleted right away if this is either enforced by the caller |
|
| 878 | - // or the special contacts birthday calendar or when the preference of an empty |
|
| 879 | - // retention (0 seconds) is set, which signals a disabled trashbin. |
|
| 880 | - $calendarData = $this->getCalendarById($calendarId); |
|
| 881 | - $isBirthdayCalendar = isset($calendarData['uri']) && $calendarData['uri'] === BirthdayService::BIRTHDAY_CALENDAR_URI; |
|
| 882 | - $trashbinDisabled = $this->config->getAppValue(Application::APP_ID, RetentionService::RETENTION_CONFIG_KEY) === '0'; |
|
| 883 | - if ($forceDeletePermanently || $isBirthdayCalendar || $trashbinDisabled) { |
|
| 884 | - $calendarData = $this->getCalendarById($calendarId); |
|
| 885 | - $shares = $this->getShares($calendarId); |
|
| 886 | - |
|
| 887 | - $qbDeleteCalendarObjectProps = $this->db->getQueryBuilder(); |
|
| 888 | - $qbDeleteCalendarObjectProps->delete($this->dbObjectPropertiesTable) |
|
| 889 | - ->where($qbDeleteCalendarObjectProps->expr()->eq('calendarid', $qbDeleteCalendarObjectProps->createNamedParameter($calendarId))) |
|
| 890 | - ->andWhere($qbDeleteCalendarObjectProps->expr()->eq('calendartype', $qbDeleteCalendarObjectProps->createNamedParameter(self::CALENDAR_TYPE_CALENDAR))) |
|
| 891 | - ->executeStatement(); |
|
| 892 | - |
|
| 893 | - $qbDeleteCalendarObjects = $this->db->getQueryBuilder(); |
|
| 894 | - $qbDeleteCalendarObjects->delete('calendarobjects') |
|
| 895 | - ->where($qbDeleteCalendarObjects->expr()->eq('calendarid', $qbDeleteCalendarObjects->createNamedParameter($calendarId))) |
|
| 896 | - ->andWhere($qbDeleteCalendarObjects->expr()->eq('calendartype', $qbDeleteCalendarObjects->createNamedParameter(self::CALENDAR_TYPE_CALENDAR))) |
|
| 897 | - ->executeStatement(); |
|
| 898 | - |
|
| 899 | - $qbDeleteCalendarChanges = $this->db->getQueryBuilder(); |
|
| 900 | - $qbDeleteCalendarChanges->delete('calendarchanges') |
|
| 901 | - ->where($qbDeleteCalendarChanges->expr()->eq('calendarid', $qbDeleteCalendarChanges->createNamedParameter($calendarId))) |
|
| 902 | - ->andWhere($qbDeleteCalendarChanges->expr()->eq('calendartype', $qbDeleteCalendarChanges->createNamedParameter(self::CALENDAR_TYPE_CALENDAR))) |
|
| 903 | - ->executeStatement(); |
|
| 904 | - |
|
| 905 | - $this->calendarSharingBackend->deleteAllShares($calendarId); |
|
| 906 | - |
|
| 907 | - $qbDeleteCalendar = $this->db->getQueryBuilder(); |
|
| 908 | - $qbDeleteCalendar->delete('calendars') |
|
| 909 | - ->where($qbDeleteCalendar->expr()->eq('id', $qbDeleteCalendar->createNamedParameter($calendarId))) |
|
| 910 | - ->executeStatement(); |
|
| 911 | - |
|
| 912 | - // Only dispatch if we actually deleted anything |
|
| 913 | - if ($calendarData) { |
|
| 914 | - $this->dispatcher->dispatchTyped(new CalendarDeletedEvent($calendarId, $calendarData, $shares)); |
|
| 915 | - } |
|
| 916 | - } else { |
|
| 917 | - $qbMarkCalendarDeleted = $this->db->getQueryBuilder(); |
|
| 918 | - $qbMarkCalendarDeleted->update('calendars') |
|
| 919 | - ->set('deleted_at', $qbMarkCalendarDeleted->createNamedParameter(time())) |
|
| 920 | - ->where($qbMarkCalendarDeleted->expr()->eq('id', $qbMarkCalendarDeleted->createNamedParameter($calendarId))) |
|
| 921 | - ->executeStatement(); |
|
| 922 | - |
|
| 923 | - $calendarData = $this->getCalendarById($calendarId); |
|
| 924 | - $shares = $this->getShares($calendarId); |
|
| 925 | - if ($calendarData) { |
|
| 926 | - $this->dispatcher->dispatchTyped(new CalendarMovedToTrashEvent( |
|
| 927 | - $calendarId, |
|
| 928 | - $calendarData, |
|
| 929 | - $shares |
|
| 930 | - )); |
|
| 931 | - } |
|
| 932 | - } |
|
| 933 | - } |
|
| 934 | - |
|
| 935 | - public function restoreCalendar(int $id): void { |
|
| 936 | - $qb = $this->db->getQueryBuilder(); |
|
| 937 | - $update = $qb->update('calendars') |
|
| 938 | - ->set('deleted_at', $qb->createNamedParameter(null)) |
|
| 939 | - ->where($qb->expr()->eq('id', $qb->createNamedParameter($id, IQueryBuilder::PARAM_INT), IQueryBuilder::PARAM_INT)); |
|
| 940 | - $update->executeStatement(); |
|
| 941 | - |
|
| 942 | - $calendarData = $this->getCalendarById($id); |
|
| 943 | - $shares = $this->getShares($id); |
|
| 944 | - if ($calendarData === null) { |
|
| 945 | - throw new RuntimeException('Calendar data that was just written can\'t be read back. Check your database configuration.'); |
|
| 946 | - } |
|
| 947 | - $this->dispatcher->dispatchTyped(new CalendarRestoredEvent( |
|
| 948 | - $id, |
|
| 949 | - $calendarData, |
|
| 950 | - $shares |
|
| 951 | - )); |
|
| 952 | - } |
|
| 953 | - |
|
| 954 | - /** |
|
| 955 | - * Delete all of an user's shares |
|
| 956 | - * |
|
| 957 | - * @param string $principaluri |
|
| 958 | - * @return void |
|
| 959 | - */ |
|
| 960 | - public function deleteAllSharesByUser($principaluri) { |
|
| 961 | - $this->calendarSharingBackend->deleteAllSharesByUser($principaluri); |
|
| 962 | - } |
|
| 963 | - |
|
| 964 | - /** |
|
| 965 | - * Returns all calendar objects within a calendar. |
|
| 966 | - * |
|
| 967 | - * Every item contains an array with the following keys: |
|
| 968 | - * * calendardata - The iCalendar-compatible calendar data |
|
| 969 | - * * uri - a unique key which will be used to construct the uri. This can |
|
| 970 | - * be any arbitrary string, but making sure it ends with '.ics' is a |
|
| 971 | - * good idea. This is only the basename, or filename, not the full |
|
| 972 | - * path. |
|
| 973 | - * * lastmodified - a timestamp of the last modification time |
|
| 974 | - * * etag - An arbitrary string, surrounded by double-quotes. (e.g.: |
|
| 975 | - * '"abcdef"') |
|
| 976 | - * * size - The size of the calendar objects, in bytes. |
|
| 977 | - * * component - optional, a string containing the type of object, such |
|
| 978 | - * as 'vevent' or 'vtodo'. If specified, this will be used to populate |
|
| 979 | - * the Content-Type header. |
|
| 980 | - * |
|
| 981 | - * Note that the etag is optional, but it's highly encouraged to return for |
|
| 982 | - * speed reasons. |
|
| 983 | - * |
|
| 984 | - * The calendardata is also optional. If it's not returned |
|
| 985 | - * 'getCalendarObject' will be called later, which *is* expected to return |
|
| 986 | - * calendardata. |
|
| 987 | - * |
|
| 988 | - * If neither etag or size are specified, the calendardata will be |
|
| 989 | - * used/fetched to determine these numbers. If both are specified the |
|
| 990 | - * amount of times this is needed is reduced by a great degree. |
|
| 991 | - * |
|
| 992 | - * @param mixed $calendarId |
|
| 993 | - * @param int $calendarType |
|
| 994 | - * @return array |
|
| 995 | - */ |
|
| 996 | - public function getCalendarObjects($calendarId, $calendarType = self::CALENDAR_TYPE_CALENDAR):array { |
|
| 997 | - $query = $this->db->getQueryBuilder(); |
|
| 998 | - $query->select(['id', 'uri', 'lastmodified', 'etag', 'calendarid', 'size', 'componenttype', 'classification']) |
|
| 999 | - ->from('calendarobjects') |
|
| 1000 | - ->where($query->expr()->eq('calendarid', $query->createNamedParameter($calendarId))) |
|
| 1001 | - ->andWhere($query->expr()->eq('calendartype', $query->createNamedParameter($calendarType))) |
|
| 1002 | - ->andWhere($query->expr()->isNull('deleted_at')); |
|
| 1003 | - $stmt = $query->executeQuery(); |
|
| 1004 | - |
|
| 1005 | - $result = []; |
|
| 1006 | - foreach ($stmt->fetchAll() as $row) { |
|
| 1007 | - $result[] = [ |
|
| 1008 | - 'id' => $row['id'], |
|
| 1009 | - 'uri' => $row['uri'], |
|
| 1010 | - 'lastmodified' => $row['lastmodified'], |
|
| 1011 | - 'etag' => '"' . $row['etag'] . '"', |
|
| 1012 | - 'calendarid' => $row['calendarid'], |
|
| 1013 | - 'size' => (int)$row['size'], |
|
| 1014 | - 'component' => strtolower($row['componenttype']), |
|
| 1015 | - 'classification' => (int)$row['classification'] |
|
| 1016 | - ]; |
|
| 1017 | - } |
|
| 1018 | - $stmt->closeCursor(); |
|
| 1019 | - |
|
| 1020 | - return $result; |
|
| 1021 | - } |
|
| 1022 | - |
|
| 1023 | - public function getDeletedCalendarObjects(int $deletedBefore): array { |
|
| 1024 | - $query = $this->db->getQueryBuilder(); |
|
| 1025 | - $query->select(['co.id', 'co.uri', 'co.lastmodified', 'co.etag', 'co.calendarid', 'co.calendartype', 'co.size', 'co.componenttype', 'co.classification', 'co.deleted_at']) |
|
| 1026 | - ->from('calendarobjects', 'co') |
|
| 1027 | - ->join('co', 'calendars', 'c', $query->expr()->eq('c.id', 'co.calendarid', IQueryBuilder::PARAM_INT)) |
|
| 1028 | - ->where($query->expr()->isNotNull('co.deleted_at')) |
|
| 1029 | - ->andWhere($query->expr()->lt('co.deleted_at', $query->createNamedParameter($deletedBefore))); |
|
| 1030 | - $stmt = $query->executeQuery(); |
|
| 1031 | - |
|
| 1032 | - $result = []; |
|
| 1033 | - foreach ($stmt->fetchAll() as $row) { |
|
| 1034 | - $result[] = [ |
|
| 1035 | - 'id' => $row['id'], |
|
| 1036 | - 'uri' => $row['uri'], |
|
| 1037 | - 'lastmodified' => $row['lastmodified'], |
|
| 1038 | - 'etag' => '"' . $row['etag'] . '"', |
|
| 1039 | - 'calendarid' => (int) $row['calendarid'], |
|
| 1040 | - 'calendartype' => (int) $row['calendartype'], |
|
| 1041 | - 'size' => (int) $row['size'], |
|
| 1042 | - 'component' => strtolower($row['componenttype']), |
|
| 1043 | - 'classification' => (int) $row['classification'], |
|
| 1044 | - '{' . \OCA\DAV\DAV\Sharing\Plugin::NS_NEXTCLOUD . '}deleted-at' => $row['deleted_at'] === null ? $row['deleted_at'] : (int) $row['deleted_at'], |
|
| 1045 | - ]; |
|
| 1046 | - } |
|
| 1047 | - $stmt->closeCursor(); |
|
| 1048 | - |
|
| 1049 | - return $result; |
|
| 1050 | - } |
|
| 1051 | - |
|
| 1052 | - /** |
|
| 1053 | - * Return all deleted calendar objects by the given principal that are not |
|
| 1054 | - * in deleted calendars. |
|
| 1055 | - * |
|
| 1056 | - * @param string $principalUri |
|
| 1057 | - * @return array |
|
| 1058 | - * @throws Exception |
|
| 1059 | - */ |
|
| 1060 | - public function getDeletedCalendarObjectsByPrincipal(string $principalUri): array { |
|
| 1061 | - $query = $this->db->getQueryBuilder(); |
|
| 1062 | - $query->select(['co.id', 'co.uri', 'co.lastmodified', 'co.etag', 'co.calendarid', 'co.size', 'co.componenttype', 'co.classification', 'co.deleted_at']) |
|
| 1063 | - ->selectAlias('c.uri', 'calendaruri') |
|
| 1064 | - ->from('calendarobjects', 'co') |
|
| 1065 | - ->join('co', 'calendars', 'c', $query->expr()->eq('c.id', 'co.calendarid', IQueryBuilder::PARAM_INT)) |
|
| 1066 | - ->where($query->expr()->eq('principaluri', $query->createNamedParameter($principalUri))) |
|
| 1067 | - ->andWhere($query->expr()->isNotNull('co.deleted_at')) |
|
| 1068 | - ->andWhere($query->expr()->isNull('c.deleted_at')); |
|
| 1069 | - $stmt = $query->executeQuery(); |
|
| 1070 | - |
|
| 1071 | - $result = []; |
|
| 1072 | - while ($row = $stmt->fetch()) { |
|
| 1073 | - $result[] = [ |
|
| 1074 | - 'id' => $row['id'], |
|
| 1075 | - 'uri' => $row['uri'], |
|
| 1076 | - 'lastmodified' => $row['lastmodified'], |
|
| 1077 | - 'etag' => '"' . $row['etag'] . '"', |
|
| 1078 | - 'calendarid' => $row['calendarid'], |
|
| 1079 | - 'calendaruri' => $row['calendaruri'], |
|
| 1080 | - 'size' => (int)$row['size'], |
|
| 1081 | - 'component' => strtolower($row['componenttype']), |
|
| 1082 | - 'classification' => (int)$row['classification'], |
|
| 1083 | - '{' . \OCA\DAV\DAV\Sharing\Plugin::NS_NEXTCLOUD . '}deleted-at' => $row['deleted_at'] === null ? $row['deleted_at'] : (int) $row['deleted_at'], |
|
| 1084 | - ]; |
|
| 1085 | - } |
|
| 1086 | - $stmt->closeCursor(); |
|
| 1087 | - |
|
| 1088 | - return $result; |
|
| 1089 | - } |
|
| 1090 | - |
|
| 1091 | - /** |
|
| 1092 | - * Returns information from a single calendar object, based on it's object |
|
| 1093 | - * uri. |
|
| 1094 | - * |
|
| 1095 | - * The object uri is only the basename, or filename and not a full path. |
|
| 1096 | - * |
|
| 1097 | - * The returned array must have the same keys as getCalendarObjects. The |
|
| 1098 | - * 'calendardata' object is required here though, while it's not required |
|
| 1099 | - * for getCalendarObjects. |
|
| 1100 | - * |
|
| 1101 | - * This method must return null if the object did not exist. |
|
| 1102 | - * |
|
| 1103 | - * @param mixed $calendarId |
|
| 1104 | - * @param string $objectUri |
|
| 1105 | - * @param int $calendarType |
|
| 1106 | - * @return array|null |
|
| 1107 | - */ |
|
| 1108 | - public function getCalendarObject($calendarId, $objectUri, int $calendarType = self::CALENDAR_TYPE_CALENDAR) { |
|
| 1109 | - $query = $this->db->getQueryBuilder(); |
|
| 1110 | - $query->select(['id', 'uri', 'lastmodified', 'etag', 'calendarid', 'size', 'calendardata', 'componenttype', 'classification', 'deleted_at']) |
|
| 1111 | - ->from('calendarobjects') |
|
| 1112 | - ->where($query->expr()->eq('calendarid', $query->createNamedParameter($calendarId))) |
|
| 1113 | - ->andWhere($query->expr()->eq('uri', $query->createNamedParameter($objectUri))) |
|
| 1114 | - ->andWhere($query->expr()->eq('calendartype', $query->createNamedParameter($calendarType))); |
|
| 1115 | - $stmt = $query->executeQuery(); |
|
| 1116 | - $row = $stmt->fetch(); |
|
| 1117 | - $stmt->closeCursor(); |
|
| 1118 | - |
|
| 1119 | - if (!$row) { |
|
| 1120 | - return null; |
|
| 1121 | - } |
|
| 1122 | - |
|
| 1123 | - return [ |
|
| 1124 | - 'id' => $row['id'], |
|
| 1125 | - 'uri' => $row['uri'], |
|
| 1126 | - 'lastmodified' => $row['lastmodified'], |
|
| 1127 | - 'etag' => '"' . $row['etag'] . '"', |
|
| 1128 | - 'calendarid' => $row['calendarid'], |
|
| 1129 | - 'size' => (int)$row['size'], |
|
| 1130 | - 'calendardata' => $this->readBlob($row['calendardata']), |
|
| 1131 | - 'component' => strtolower($row['componenttype']), |
|
| 1132 | - 'classification' => (int)$row['classification'], |
|
| 1133 | - '{' . \OCA\DAV\DAV\Sharing\Plugin::NS_NEXTCLOUD . '}deleted-at' => $row['deleted_at'] === null ? $row['deleted_at'] : (int) $row['deleted_at'], |
|
| 1134 | - ]; |
|
| 1135 | - } |
|
| 1136 | - |
|
| 1137 | - /** |
|
| 1138 | - * Returns a list of calendar objects. |
|
| 1139 | - * |
|
| 1140 | - * This method should work identical to getCalendarObject, but instead |
|
| 1141 | - * return all the calendar objects in the list as an array. |
|
| 1142 | - * |
|
| 1143 | - * If the backend supports this, it may allow for some speed-ups. |
|
| 1144 | - * |
|
| 1145 | - * @param mixed $calendarId |
|
| 1146 | - * @param string[] $uris |
|
| 1147 | - * @param int $calendarType |
|
| 1148 | - * @return array |
|
| 1149 | - */ |
|
| 1150 | - public function getMultipleCalendarObjects($calendarId, array $uris, $calendarType = self::CALENDAR_TYPE_CALENDAR):array { |
|
| 1151 | - if (empty($uris)) { |
|
| 1152 | - return []; |
|
| 1153 | - } |
|
| 1154 | - |
|
| 1155 | - $chunks = array_chunk($uris, 100); |
|
| 1156 | - $objects = []; |
|
| 1157 | - |
|
| 1158 | - $query = $this->db->getQueryBuilder(); |
|
| 1159 | - $query->select(['id', 'uri', 'lastmodified', 'etag', 'calendarid', 'size', 'calendardata', 'componenttype', 'classification']) |
|
| 1160 | - ->from('calendarobjects') |
|
| 1161 | - ->where($query->expr()->eq('calendarid', $query->createNamedParameter($calendarId))) |
|
| 1162 | - ->andWhere($query->expr()->in('uri', $query->createParameter('uri'))) |
|
| 1163 | - ->andWhere($query->expr()->eq('calendartype', $query->createNamedParameter($calendarType))) |
|
| 1164 | - ->andWhere($query->expr()->isNull('deleted_at')); |
|
| 1165 | - |
|
| 1166 | - foreach ($chunks as $uris) { |
|
| 1167 | - $query->setParameter('uri', $uris, IQueryBuilder::PARAM_STR_ARRAY); |
|
| 1168 | - $result = $query->executeQuery(); |
|
| 1169 | - |
|
| 1170 | - while ($row = $result->fetch()) { |
|
| 1171 | - $objects[] = [ |
|
| 1172 | - 'id' => $row['id'], |
|
| 1173 | - 'uri' => $row['uri'], |
|
| 1174 | - 'lastmodified' => $row['lastmodified'], |
|
| 1175 | - 'etag' => '"' . $row['etag'] . '"', |
|
| 1176 | - 'calendarid' => $row['calendarid'], |
|
| 1177 | - 'size' => (int)$row['size'], |
|
| 1178 | - 'calendardata' => $this->readBlob($row['calendardata']), |
|
| 1179 | - 'component' => strtolower($row['componenttype']), |
|
| 1180 | - 'classification' => (int)$row['classification'] |
|
| 1181 | - ]; |
|
| 1182 | - } |
|
| 1183 | - $result->closeCursor(); |
|
| 1184 | - } |
|
| 1185 | - |
|
| 1186 | - return $objects; |
|
| 1187 | - } |
|
| 1188 | - |
|
| 1189 | - /** |
|
| 1190 | - * Creates a new calendar object. |
|
| 1191 | - * |
|
| 1192 | - * The object uri is only the basename, or filename and not a full path. |
|
| 1193 | - * |
|
| 1194 | - * It is possible return an etag from this function, which will be used in |
|
| 1195 | - * the response to this PUT request. Note that the ETag must be surrounded |
|
| 1196 | - * by double-quotes. |
|
| 1197 | - * |
|
| 1198 | - * However, you should only really return this ETag if you don't mangle the |
|
| 1199 | - * calendar-data. If the result of a subsequent GET to this object is not |
|
| 1200 | - * the exact same as this request body, you should omit the ETag. |
|
| 1201 | - * |
|
| 1202 | - * @param mixed $calendarId |
|
| 1203 | - * @param string $objectUri |
|
| 1204 | - * @param string $calendarData |
|
| 1205 | - * @param int $calendarType |
|
| 1206 | - * @return string |
|
| 1207 | - */ |
|
| 1208 | - public function createCalendarObject($calendarId, $objectUri, $calendarData, $calendarType = self::CALENDAR_TYPE_CALENDAR) { |
|
| 1209 | - $extraData = $this->getDenormalizedData($calendarData); |
|
| 1210 | - |
|
| 1211 | - // Try to detect duplicates |
|
| 1212 | - $qb = $this->db->getQueryBuilder(); |
|
| 1213 | - $qb->select($qb->func()->count('*')) |
|
| 1214 | - ->from('calendarobjects') |
|
| 1215 | - ->where($qb->expr()->eq('calendarid', $qb->createNamedParameter($calendarId))) |
|
| 1216 | - ->andWhere($qb->expr()->eq('uid', $qb->createNamedParameter($extraData['uid']))) |
|
| 1217 | - ->andWhere($qb->expr()->eq('calendartype', $qb->createNamedParameter($calendarType))) |
|
| 1218 | - ->andWhere($qb->expr()->isNull('deleted_at')); |
|
| 1219 | - $result = $qb->executeQuery(); |
|
| 1220 | - $count = (int) $result->fetchOne(); |
|
| 1221 | - $result->closeCursor(); |
|
| 1222 | - |
|
| 1223 | - if ($count !== 0) { |
|
| 1224 | - throw new BadRequest('Calendar object with uid already exists in this calendar collection.'); |
|
| 1225 | - } |
|
| 1226 | - // For a more specific error message we also try to explicitly look up the UID but as a deleted entry |
|
| 1227 | - $qbDel = $this->db->getQueryBuilder(); |
|
| 1228 | - $qbDel->select($qb->func()->count('*')) |
|
| 1229 | - ->from('calendarobjects') |
|
| 1230 | - ->where($qbDel->expr()->eq('calendarid', $qbDel->createNamedParameter($calendarId))) |
|
| 1231 | - ->andWhere($qbDel->expr()->eq('uid', $qbDel->createNamedParameter($extraData['uid']))) |
|
| 1232 | - ->andWhere($qbDel->expr()->eq('calendartype', $qbDel->createNamedParameter($calendarType))) |
|
| 1233 | - ->andWhere($qbDel->expr()->isNotNull('deleted_at')); |
|
| 1234 | - $result = $qbDel->executeQuery(); |
|
| 1235 | - $count = (int) $result->fetchOne(); |
|
| 1236 | - $result->closeCursor(); |
|
| 1237 | - if ($count !== 0) { |
|
| 1238 | - throw new BadRequest('Deleted calendar object with uid already exists in this calendar collection.'); |
|
| 1239 | - } |
|
| 1240 | - |
|
| 1241 | - $query = $this->db->getQueryBuilder(); |
|
| 1242 | - $query->insert('calendarobjects') |
|
| 1243 | - ->values([ |
|
| 1244 | - 'calendarid' => $query->createNamedParameter($calendarId), |
|
| 1245 | - 'uri' => $query->createNamedParameter($objectUri), |
|
| 1246 | - 'calendardata' => $query->createNamedParameter($calendarData, IQueryBuilder::PARAM_LOB), |
|
| 1247 | - 'lastmodified' => $query->createNamedParameter(time()), |
|
| 1248 | - 'etag' => $query->createNamedParameter($extraData['etag']), |
|
| 1249 | - 'size' => $query->createNamedParameter($extraData['size']), |
|
| 1250 | - 'componenttype' => $query->createNamedParameter($extraData['componentType']), |
|
| 1251 | - 'firstoccurence' => $query->createNamedParameter($extraData['firstOccurence']), |
|
| 1252 | - 'lastoccurence' => $query->createNamedParameter($extraData['lastOccurence']), |
|
| 1253 | - 'classification' => $query->createNamedParameter($extraData['classification']), |
|
| 1254 | - 'uid' => $query->createNamedParameter($extraData['uid']), |
|
| 1255 | - 'calendartype' => $query->createNamedParameter($calendarType), |
|
| 1256 | - ]) |
|
| 1257 | - ->executeStatement(); |
|
| 1258 | - |
|
| 1259 | - $this->updateProperties($calendarId, $objectUri, $calendarData, $calendarType); |
|
| 1260 | - $this->addChange($calendarId, $objectUri, 1, $calendarType); |
|
| 1261 | - |
|
| 1262 | - $objectRow = $this->getCalendarObject($calendarId, $objectUri, $calendarType); |
|
| 1263 | - assert($objectRow !== null); |
|
| 1264 | - |
|
| 1265 | - if ($calendarType === self::CALENDAR_TYPE_CALENDAR) { |
|
| 1266 | - $calendarRow = $this->getCalendarById($calendarId); |
|
| 1267 | - $shares = $this->getShares($calendarId); |
|
| 1268 | - |
|
| 1269 | - $this->dispatcher->dispatchTyped(new CalendarObjectCreatedEvent($calendarId, $calendarRow, $shares, $objectRow)); |
|
| 1270 | - } else { |
|
| 1271 | - $subscriptionRow = $this->getSubscriptionById($calendarId); |
|
| 1272 | - |
|
| 1273 | - $this->dispatcher->dispatchTyped(new CachedCalendarObjectCreatedEvent($calendarId, $subscriptionRow, [], $objectRow)); |
|
| 1274 | - } |
|
| 1275 | - |
|
| 1276 | - return '"' . $extraData['etag'] . '"'; |
|
| 1277 | - } |
|
| 1278 | - |
|
| 1279 | - /** |
|
| 1280 | - * Updates an existing calendarobject, based on it's uri. |
|
| 1281 | - * |
|
| 1282 | - * The object uri is only the basename, or filename and not a full path. |
|
| 1283 | - * |
|
| 1284 | - * It is possible return an etag from this function, which will be used in |
|
| 1285 | - * the response to this PUT request. Note that the ETag must be surrounded |
|
| 1286 | - * by double-quotes. |
|
| 1287 | - * |
|
| 1288 | - * However, you should only really return this ETag if you don't mangle the |
|
| 1289 | - * calendar-data. If the result of a subsequent GET to this object is not |
|
| 1290 | - * the exact same as this request body, you should omit the ETag. |
|
| 1291 | - * |
|
| 1292 | - * @param mixed $calendarId |
|
| 1293 | - * @param string $objectUri |
|
| 1294 | - * @param string $calendarData |
|
| 1295 | - * @param int $calendarType |
|
| 1296 | - * @return string |
|
| 1297 | - */ |
|
| 1298 | - public function updateCalendarObject($calendarId, $objectUri, $calendarData, $calendarType = self::CALENDAR_TYPE_CALENDAR) { |
|
| 1299 | - $extraData = $this->getDenormalizedData($calendarData); |
|
| 1300 | - $query = $this->db->getQueryBuilder(); |
|
| 1301 | - $query->update('calendarobjects') |
|
| 1302 | - ->set('calendardata', $query->createNamedParameter($calendarData, IQueryBuilder::PARAM_LOB)) |
|
| 1303 | - ->set('lastmodified', $query->createNamedParameter(time())) |
|
| 1304 | - ->set('etag', $query->createNamedParameter($extraData['etag'])) |
|
| 1305 | - ->set('size', $query->createNamedParameter($extraData['size'])) |
|
| 1306 | - ->set('componenttype', $query->createNamedParameter($extraData['componentType'])) |
|
| 1307 | - ->set('firstoccurence', $query->createNamedParameter($extraData['firstOccurence'])) |
|
| 1308 | - ->set('lastoccurence', $query->createNamedParameter($extraData['lastOccurence'])) |
|
| 1309 | - ->set('classification', $query->createNamedParameter($extraData['classification'])) |
|
| 1310 | - ->set('uid', $query->createNamedParameter($extraData['uid'])) |
|
| 1311 | - ->where($query->expr()->eq('calendarid', $query->createNamedParameter($calendarId))) |
|
| 1312 | - ->andWhere($query->expr()->eq('uri', $query->createNamedParameter($objectUri))) |
|
| 1313 | - ->andWhere($query->expr()->eq('calendartype', $query->createNamedParameter($calendarType))) |
|
| 1314 | - ->executeStatement(); |
|
| 1315 | - |
|
| 1316 | - $this->updateProperties($calendarId, $objectUri, $calendarData, $calendarType); |
|
| 1317 | - $this->addChange($calendarId, $objectUri, 2, $calendarType); |
|
| 1318 | - |
|
| 1319 | - $objectRow = $this->getCalendarObject($calendarId, $objectUri, $calendarType); |
|
| 1320 | - if (is_array($objectRow)) { |
|
| 1321 | - if ($calendarType === self::CALENDAR_TYPE_CALENDAR) { |
|
| 1322 | - $calendarRow = $this->getCalendarById($calendarId); |
|
| 1323 | - $shares = $this->getShares($calendarId); |
|
| 1324 | - |
|
| 1325 | - $this->dispatcher->dispatchTyped(new CalendarObjectUpdatedEvent($calendarId, $calendarRow, $shares, $objectRow)); |
|
| 1326 | - } else { |
|
| 1327 | - $subscriptionRow = $this->getSubscriptionById($calendarId); |
|
| 1328 | - |
|
| 1329 | - $this->dispatcher->dispatchTyped(new CachedCalendarObjectUpdatedEvent($calendarId, $subscriptionRow, [], $objectRow)); |
|
| 1330 | - } |
|
| 1331 | - } |
|
| 1332 | - |
|
| 1333 | - return '"' . $extraData['etag'] . '"'; |
|
| 1334 | - } |
|
| 1335 | - |
|
| 1336 | - /** |
|
| 1337 | - * Moves a calendar object from calendar to calendar. |
|
| 1338 | - * |
|
| 1339 | - * @param int $sourceCalendarId |
|
| 1340 | - * @param int $targetCalendarId |
|
| 1341 | - * @param int $objectId |
|
| 1342 | - * @param string $oldPrincipalUri |
|
| 1343 | - * @param string $newPrincipalUri |
|
| 1344 | - * @param int $calendarType |
|
| 1345 | - * @return bool |
|
| 1346 | - * @throws Exception |
|
| 1347 | - */ |
|
| 1348 | - public function moveCalendarObject(int $sourceCalendarId, int $targetCalendarId, int $objectId, string $oldPrincipalUri, string $newPrincipalUri, int $calendarType = self::CALENDAR_TYPE_CALENDAR): bool { |
|
| 1349 | - $object = $this->getCalendarObjectById($oldPrincipalUri, $objectId); |
|
| 1350 | - if (empty($object)) { |
|
| 1351 | - return false; |
|
| 1352 | - } |
|
| 1353 | - |
|
| 1354 | - $query = $this->db->getQueryBuilder(); |
|
| 1355 | - $query->update('calendarobjects') |
|
| 1356 | - ->set('calendarid', $query->createNamedParameter($targetCalendarId, IQueryBuilder::PARAM_INT)) |
|
| 1357 | - ->where($query->expr()->eq('id', $query->createNamedParameter($objectId, IQueryBuilder::PARAM_INT), IQueryBuilder::PARAM_INT)) |
|
| 1358 | - ->andWhere($query->expr()->eq('calendartype', $query->createNamedParameter($calendarType, IQueryBuilder::PARAM_INT), IQueryBuilder::PARAM_INT)) |
|
| 1359 | - ->executeStatement(); |
|
| 1360 | - |
|
| 1361 | - $this->purgeProperties($sourceCalendarId, $objectId); |
|
| 1362 | - $this->updateProperties($targetCalendarId, $object['uri'], $object['calendardata'], $calendarType); |
|
| 1363 | - |
|
| 1364 | - $this->addChange($sourceCalendarId, $object['uri'], 1, $calendarType); |
|
| 1365 | - $this->addChange($targetCalendarId, $object['uri'], 3, $calendarType); |
|
| 1366 | - |
|
| 1367 | - $object = $this->getCalendarObjectById($newPrincipalUri, $objectId); |
|
| 1368 | - // Calendar Object wasn't found - possibly because it was deleted in the meantime by a different client |
|
| 1369 | - if (empty($object)) { |
|
| 1370 | - return false; |
|
| 1371 | - } |
|
| 1372 | - |
|
| 1373 | - $targetCalendarRow = $this->getCalendarById($targetCalendarId); |
|
| 1374 | - // the calendar this event is being moved to does not exist any longer |
|
| 1375 | - if (empty($targetCalendarRow)) { |
|
| 1376 | - return false; |
|
| 1377 | - } |
|
| 1378 | - |
|
| 1379 | - if ($calendarType === self::CALENDAR_TYPE_CALENDAR) { |
|
| 1380 | - $sourceShares = $this->getShares($sourceCalendarId); |
|
| 1381 | - $targetShares = $this->getShares($targetCalendarId); |
|
| 1382 | - $sourceCalendarRow = $this->getCalendarById($sourceCalendarId); |
|
| 1383 | - $this->dispatcher->dispatchTyped(new CalendarObjectMovedEvent($sourceCalendarId, $sourceCalendarRow, $targetCalendarId, $targetCalendarRow, $sourceShares, $targetShares, $object)); |
|
| 1384 | - } |
|
| 1385 | - return true; |
|
| 1386 | - } |
|
| 1387 | - |
|
| 1388 | - |
|
| 1389 | - /** |
|
| 1390 | - * @param int $calendarObjectId |
|
| 1391 | - * @param int $classification |
|
| 1392 | - */ |
|
| 1393 | - public function setClassification($calendarObjectId, $classification) { |
|
| 1394 | - if (!in_array($classification, [ |
|
| 1395 | - self::CLASSIFICATION_PUBLIC, self::CLASSIFICATION_PRIVATE, self::CLASSIFICATION_CONFIDENTIAL |
|
| 1396 | - ])) { |
|
| 1397 | - throw new \InvalidArgumentException(); |
|
| 1398 | - } |
|
| 1399 | - $query = $this->db->getQueryBuilder(); |
|
| 1400 | - $query->update('calendarobjects') |
|
| 1401 | - ->set('classification', $query->createNamedParameter($classification)) |
|
| 1402 | - ->where($query->expr()->eq('id', $query->createNamedParameter($calendarObjectId))) |
|
| 1403 | - ->executeStatement(); |
|
| 1404 | - } |
|
| 1405 | - |
|
| 1406 | - /** |
|
| 1407 | - * Deletes an existing calendar object. |
|
| 1408 | - * |
|
| 1409 | - * The object uri is only the basename, or filename and not a full path. |
|
| 1410 | - * |
|
| 1411 | - * @param mixed $calendarId |
|
| 1412 | - * @param string $objectUri |
|
| 1413 | - * @param int $calendarType |
|
| 1414 | - * @param bool $forceDeletePermanently |
|
| 1415 | - * @return void |
|
| 1416 | - */ |
|
| 1417 | - public function deleteCalendarObject($calendarId, $objectUri, $calendarType = self::CALENDAR_TYPE_CALENDAR, bool $forceDeletePermanently = false) { |
|
| 1418 | - $data = $this->getCalendarObject($calendarId, $objectUri, $calendarType); |
|
| 1419 | - |
|
| 1420 | - if ($data === null) { |
|
| 1421 | - // Nothing to delete |
|
| 1422 | - return; |
|
| 1423 | - } |
|
| 1424 | - |
|
| 1425 | - if ($forceDeletePermanently || $this->config->getAppValue(Application::APP_ID, RetentionService::RETENTION_CONFIG_KEY) === '0') { |
|
| 1426 | - $stmt = $this->db->prepare('DELETE FROM `*PREFIX*calendarobjects` WHERE `calendarid` = ? AND `uri` = ? AND `calendartype` = ?'); |
|
| 1427 | - $stmt->execute([$calendarId, $objectUri, $calendarType]); |
|
| 1428 | - |
|
| 1429 | - $this->purgeProperties($calendarId, $data['id']); |
|
| 1430 | - |
|
| 1431 | - if ($calendarType === self::CALENDAR_TYPE_CALENDAR) { |
|
| 1432 | - $calendarRow = $this->getCalendarById($calendarId); |
|
| 1433 | - $shares = $this->getShares($calendarId); |
|
| 1434 | - |
|
| 1435 | - $this->dispatcher->dispatchTyped(new CalendarObjectDeletedEvent($calendarId, $calendarRow, $shares, $data)); |
|
| 1436 | - } else { |
|
| 1437 | - $subscriptionRow = $this->getSubscriptionById($calendarId); |
|
| 1438 | - |
|
| 1439 | - $this->dispatcher->dispatchTyped(new CachedCalendarObjectDeletedEvent($calendarId, $subscriptionRow, [], $data)); |
|
| 1440 | - } |
|
| 1441 | - } else { |
|
| 1442 | - $pathInfo = pathinfo($data['uri']); |
|
| 1443 | - if (!empty($pathInfo['extension'])) { |
|
| 1444 | - // Append a suffix to "free" the old URI for recreation |
|
| 1445 | - $newUri = sprintf( |
|
| 1446 | - "%s-deleted.%s", |
|
| 1447 | - $pathInfo['filename'], |
|
| 1448 | - $pathInfo['extension'] |
|
| 1449 | - ); |
|
| 1450 | - } else { |
|
| 1451 | - $newUri = sprintf( |
|
| 1452 | - "%s-deleted", |
|
| 1453 | - $pathInfo['filename'] |
|
| 1454 | - ); |
|
| 1455 | - } |
|
| 1456 | - |
|
| 1457 | - // Try to detect conflicts before the DB does |
|
| 1458 | - // As unlikely as it seems, this can happen when the user imports, then deletes, imports and deletes again |
|
| 1459 | - $newObject = $this->getCalendarObject($calendarId, $newUri, $calendarType); |
|
| 1460 | - if ($newObject !== null) { |
|
| 1461 | - throw new Forbidden("A calendar object with URI $newUri already exists in calendar $calendarId, therefore this object can't be moved into the trashbin"); |
|
| 1462 | - } |
|
| 1463 | - |
|
| 1464 | - $qb = $this->db->getQueryBuilder(); |
|
| 1465 | - $markObjectDeletedQuery = $qb->update('calendarobjects') |
|
| 1466 | - ->set('deleted_at', $qb->createNamedParameter(time(), IQueryBuilder::PARAM_INT)) |
|
| 1467 | - ->set('uri', $qb->createNamedParameter($newUri)) |
|
| 1468 | - ->where( |
|
| 1469 | - $qb->expr()->eq('calendarid', $qb->createNamedParameter($calendarId)), |
|
| 1470 | - $qb->expr()->eq('calendartype', $qb->createNamedParameter($calendarType, IQueryBuilder::PARAM_INT), IQueryBuilder::PARAM_INT), |
|
| 1471 | - $qb->expr()->eq('uri', $qb->createNamedParameter($objectUri)) |
|
| 1472 | - ); |
|
| 1473 | - $markObjectDeletedQuery->executeStatement(); |
|
| 1474 | - |
|
| 1475 | - $calendarData = $this->getCalendarById($calendarId); |
|
| 1476 | - if ($calendarData !== null) { |
|
| 1477 | - $this->dispatcher->dispatchTyped( |
|
| 1478 | - new CalendarObjectMovedToTrashEvent( |
|
| 1479 | - $calendarId, |
|
| 1480 | - $calendarData, |
|
| 1481 | - $this->getShares($calendarId), |
|
| 1482 | - $data |
|
| 1483 | - ) |
|
| 1484 | - ); |
|
| 1485 | - } |
|
| 1486 | - } |
|
| 1487 | - |
|
| 1488 | - $this->addChange($calendarId, $objectUri, 3, $calendarType); |
|
| 1489 | - } |
|
| 1490 | - |
|
| 1491 | - /** |
|
| 1492 | - * @param mixed $objectData |
|
| 1493 | - * |
|
| 1494 | - * @throws Forbidden |
|
| 1495 | - */ |
|
| 1496 | - public function restoreCalendarObject(array $objectData): void { |
|
| 1497 | - $id = (int) $objectData['id']; |
|
| 1498 | - $restoreUri = str_replace("-deleted.ics", ".ics", $objectData['uri']); |
|
| 1499 | - $targetObject = $this->getCalendarObject( |
|
| 1500 | - $objectData['calendarid'], |
|
| 1501 | - $restoreUri |
|
| 1502 | - ); |
|
| 1503 | - if ($targetObject !== null) { |
|
| 1504 | - throw new Forbidden("Can not restore calendar $id because a calendar object with the URI $restoreUri already exists"); |
|
| 1505 | - } |
|
| 1506 | - |
|
| 1507 | - $qb = $this->db->getQueryBuilder(); |
|
| 1508 | - $update = $qb->update('calendarobjects') |
|
| 1509 | - ->set('uri', $qb->createNamedParameter($restoreUri)) |
|
| 1510 | - ->set('deleted_at', $qb->createNamedParameter(null)) |
|
| 1511 | - ->where($qb->expr()->eq('id', $qb->createNamedParameter($id, IQueryBuilder::PARAM_INT), IQueryBuilder::PARAM_INT)); |
|
| 1512 | - $update->executeStatement(); |
|
| 1513 | - |
|
| 1514 | - // Make sure this change is tracked in the changes table |
|
| 1515 | - $qb2 = $this->db->getQueryBuilder(); |
|
| 1516 | - $selectObject = $qb2->select('calendardata', 'uri', 'calendarid', 'calendartype') |
|
| 1517 | - ->selectAlias('componenttype', 'component') |
|
| 1518 | - ->from('calendarobjects') |
|
| 1519 | - ->where($qb2->expr()->eq('id', $qb2->createNamedParameter($id, IQueryBuilder::PARAM_INT), IQueryBuilder::PARAM_INT)); |
|
| 1520 | - $result = $selectObject->executeQuery(); |
|
| 1521 | - $row = $result->fetch(); |
|
| 1522 | - $result->closeCursor(); |
|
| 1523 | - if ($row === false) { |
|
| 1524 | - // Welp, this should possibly not have happened, but let's ignore |
|
| 1525 | - return; |
|
| 1526 | - } |
|
| 1527 | - $this->addChange($row['calendarid'], $row['uri'], 1, (int) $row['calendartype']); |
|
| 1528 | - |
|
| 1529 | - $calendarRow = $this->getCalendarById((int) $row['calendarid']); |
|
| 1530 | - if ($calendarRow === null) { |
|
| 1531 | - throw new RuntimeException('Calendar object data that was just written can\'t be read back. Check your database configuration.'); |
|
| 1532 | - } |
|
| 1533 | - $this->dispatcher->dispatchTyped( |
|
| 1534 | - new CalendarObjectRestoredEvent( |
|
| 1535 | - (int) $objectData['calendarid'], |
|
| 1536 | - $calendarRow, |
|
| 1537 | - $this->getShares((int) $row['calendarid']), |
|
| 1538 | - $row |
|
| 1539 | - ) |
|
| 1540 | - ); |
|
| 1541 | - } |
|
| 1542 | - |
|
| 1543 | - /** |
|
| 1544 | - * Performs a calendar-query on the contents of this calendar. |
|
| 1545 | - * |
|
| 1546 | - * The calendar-query is defined in RFC4791 : CalDAV. Using the |
|
| 1547 | - * calendar-query it is possible for a client to request a specific set of |
|
| 1548 | - * object, based on contents of iCalendar properties, date-ranges and |
|
| 1549 | - * iCalendar component types (VTODO, VEVENT). |
|
| 1550 | - * |
|
| 1551 | - * This method should just return a list of (relative) urls that match this |
|
| 1552 | - * query. |
|
| 1553 | - * |
|
| 1554 | - * The list of filters are specified as an array. The exact array is |
|
| 1555 | - * documented by Sabre\CalDAV\CalendarQueryParser. |
|
| 1556 | - * |
|
| 1557 | - * Note that it is extremely likely that getCalendarObject for every path |
|
| 1558 | - * returned from this method will be called almost immediately after. You |
|
| 1559 | - * may want to anticipate this to speed up these requests. |
|
| 1560 | - * |
|
| 1561 | - * This method provides a default implementation, which parses *all* the |
|
| 1562 | - * iCalendar objects in the specified calendar. |
|
| 1563 | - * |
|
| 1564 | - * This default may well be good enough for personal use, and calendars |
|
| 1565 | - * that aren't very large. But if you anticipate high usage, big calendars |
|
| 1566 | - * or high loads, you are strongly advised to optimize certain paths. |
|
| 1567 | - * |
|
| 1568 | - * The best way to do so is override this method and to optimize |
|
| 1569 | - * specifically for 'common filters'. |
|
| 1570 | - * |
|
| 1571 | - * Requests that are extremely common are: |
|
| 1572 | - * * requests for just VEVENTS |
|
| 1573 | - * * requests for just VTODO |
|
| 1574 | - * * requests with a time-range-filter on either VEVENT or VTODO. |
|
| 1575 | - * |
|
| 1576 | - * ..and combinations of these requests. It may not be worth it to try to |
|
| 1577 | - * handle every possible situation and just rely on the (relatively |
|
| 1578 | - * easy to use) CalendarQueryValidator to handle the rest. |
|
| 1579 | - * |
|
| 1580 | - * Note that especially time-range-filters may be difficult to parse. A |
|
| 1581 | - * time-range filter specified on a VEVENT must for instance also handle |
|
| 1582 | - * recurrence rules correctly. |
|
| 1583 | - * A good example of how to interpret all these filters can also simply |
|
| 1584 | - * be found in Sabre\CalDAV\CalendarQueryFilter. This class is as correct |
|
| 1585 | - * as possible, so it gives you a good idea on what type of stuff you need |
|
| 1586 | - * to think of. |
|
| 1587 | - * |
|
| 1588 | - * @param mixed $calendarId |
|
| 1589 | - * @param array $filters |
|
| 1590 | - * @param int $calendarType |
|
| 1591 | - * @return array |
|
| 1592 | - */ |
|
| 1593 | - public function calendarQuery($calendarId, array $filters, $calendarType = self::CALENDAR_TYPE_CALENDAR):array { |
|
| 1594 | - $componentType = null; |
|
| 1595 | - $requirePostFilter = true; |
|
| 1596 | - $timeRange = null; |
|
| 1597 | - |
|
| 1598 | - // if no filters were specified, we don't need to filter after a query |
|
| 1599 | - if (!$filters['prop-filters'] && !$filters['comp-filters']) { |
|
| 1600 | - $requirePostFilter = false; |
|
| 1601 | - } |
|
| 1602 | - |
|
| 1603 | - // Figuring out if there's a component filter |
|
| 1604 | - if (count($filters['comp-filters']) > 0 && !$filters['comp-filters'][0]['is-not-defined']) { |
|
| 1605 | - $componentType = $filters['comp-filters'][0]['name']; |
|
| 1606 | - |
|
| 1607 | - // Checking if we need post-filters |
|
| 1608 | - if (!$filters['prop-filters'] && !$filters['comp-filters'][0]['comp-filters'] && !$filters['comp-filters'][0]['time-range'] && !$filters['comp-filters'][0]['prop-filters']) { |
|
| 1609 | - $requirePostFilter = false; |
|
| 1610 | - } |
|
| 1611 | - // There was a time-range filter |
|
| 1612 | - if ($componentType === 'VEVENT' && isset($filters['comp-filters'][0]['time-range']) && is_array($filters['comp-filters'][0]['time-range'])) { |
|
| 1613 | - $timeRange = $filters['comp-filters'][0]['time-range']; |
|
| 1614 | - |
|
| 1615 | - // If start time OR the end time is not specified, we can do a |
|
| 1616 | - // 100% accurate mysql query. |
|
| 1617 | - if (!$filters['prop-filters'] && !$filters['comp-filters'][0]['comp-filters'] && !$filters['comp-filters'][0]['prop-filters'] && (!$timeRange['start'] || !$timeRange['end'])) { |
|
| 1618 | - $requirePostFilter = false; |
|
| 1619 | - } |
|
| 1620 | - } |
|
| 1621 | - } |
|
| 1622 | - $columns = ['uri']; |
|
| 1623 | - if ($requirePostFilter) { |
|
| 1624 | - $columns = ['uri', 'calendardata']; |
|
| 1625 | - } |
|
| 1626 | - $query = $this->db->getQueryBuilder(); |
|
| 1627 | - $query->select($columns) |
|
| 1628 | - ->from('calendarobjects') |
|
| 1629 | - ->where($query->expr()->eq('calendarid', $query->createNamedParameter($calendarId))) |
|
| 1630 | - ->andWhere($query->expr()->eq('calendartype', $query->createNamedParameter($calendarType))) |
|
| 1631 | - ->andWhere($query->expr()->isNull('deleted_at')); |
|
| 1632 | - |
|
| 1633 | - if ($componentType) { |
|
| 1634 | - $query->andWhere($query->expr()->eq('componenttype', $query->createNamedParameter($componentType))); |
|
| 1635 | - } |
|
| 1636 | - |
|
| 1637 | - if ($timeRange && $timeRange['start']) { |
|
| 1638 | - $query->andWhere($query->expr()->gt('lastoccurence', $query->createNamedParameter($timeRange['start']->getTimeStamp()))); |
|
| 1639 | - } |
|
| 1640 | - if ($timeRange && $timeRange['end']) { |
|
| 1641 | - $query->andWhere($query->expr()->lt('firstoccurence', $query->createNamedParameter($timeRange['end']->getTimeStamp()))); |
|
| 1642 | - } |
|
| 1643 | - |
|
| 1644 | - $stmt = $query->executeQuery(); |
|
| 1645 | - |
|
| 1646 | - $result = []; |
|
| 1647 | - while ($row = $stmt->fetch()) { |
|
| 1648 | - if ($requirePostFilter) { |
|
| 1649 | - // validateFilterForObject will parse the calendar data |
|
| 1650 | - // catch parsing errors |
|
| 1651 | - try { |
|
| 1652 | - $matches = $this->validateFilterForObject($row, $filters); |
|
| 1653 | - } catch (ParseException $ex) { |
|
| 1654 | - $this->logger->error('Caught parsing exception for calendar data. This usually indicates invalid calendar data. calendar-id:'.$calendarId.' uri:'.$row['uri'], [ |
|
| 1655 | - 'app' => 'dav', |
|
| 1656 | - 'exception' => $ex, |
|
| 1657 | - ]); |
|
| 1658 | - continue; |
|
| 1659 | - } catch (InvalidDataException $ex) { |
|
| 1660 | - $this->logger->error('Caught invalid data exception for calendar data. This usually indicates invalid calendar data. calendar-id:'.$calendarId.' uri:'.$row['uri'], [ |
|
| 1661 | - 'app' => 'dav', |
|
| 1662 | - 'exception' => $ex, |
|
| 1663 | - ]); |
|
| 1664 | - continue; |
|
| 1665 | - } |
|
| 1666 | - |
|
| 1667 | - if (!$matches) { |
|
| 1668 | - continue; |
|
| 1669 | - } |
|
| 1670 | - } |
|
| 1671 | - $result[] = $row['uri']; |
|
| 1672 | - } |
|
| 1673 | - |
|
| 1674 | - return $result; |
|
| 1675 | - } |
|
| 1676 | - |
|
| 1677 | - /** |
|
| 1678 | - * custom Nextcloud search extension for CalDAV |
|
| 1679 | - * |
|
| 1680 | - * TODO - this should optionally cover cached calendar objects as well |
|
| 1681 | - * |
|
| 1682 | - * @param string $principalUri |
|
| 1683 | - * @param array $filters |
|
| 1684 | - * @param integer|null $limit |
|
| 1685 | - * @param integer|null $offset |
|
| 1686 | - * @return array |
|
| 1687 | - */ |
|
| 1688 | - public function calendarSearch($principalUri, array $filters, $limit = null, $offset = null) { |
|
| 1689 | - $calendars = $this->getCalendarsForUser($principalUri); |
|
| 1690 | - $ownCalendars = []; |
|
| 1691 | - $sharedCalendars = []; |
|
| 1692 | - |
|
| 1693 | - $uriMapper = []; |
|
| 1694 | - |
|
| 1695 | - foreach ($calendars as $calendar) { |
|
| 1696 | - if ($calendar['{http://owncloud.org/ns}owner-principal'] === $principalUri) { |
|
| 1697 | - $ownCalendars[] = $calendar['id']; |
|
| 1698 | - } else { |
|
| 1699 | - $sharedCalendars[] = $calendar['id']; |
|
| 1700 | - } |
|
| 1701 | - $uriMapper[$calendar['id']] = $calendar['uri']; |
|
| 1702 | - } |
|
| 1703 | - if (count($ownCalendars) === 0 && count($sharedCalendars) === 0) { |
|
| 1704 | - return []; |
|
| 1705 | - } |
|
| 1706 | - |
|
| 1707 | - $query = $this->db->getQueryBuilder(); |
|
| 1708 | - // Calendar id expressions |
|
| 1709 | - $calendarExpressions = []; |
|
| 1710 | - foreach ($ownCalendars as $id) { |
|
| 1711 | - $calendarExpressions[] = $query->expr()->andX( |
|
| 1712 | - $query->expr()->eq('c.calendarid', |
|
| 1713 | - $query->createNamedParameter($id)), |
|
| 1714 | - $query->expr()->eq('c.calendartype', |
|
| 1715 | - $query->createNamedParameter(self::CALENDAR_TYPE_CALENDAR))); |
|
| 1716 | - } |
|
| 1717 | - foreach ($sharedCalendars as $id) { |
|
| 1718 | - $calendarExpressions[] = $query->expr()->andX( |
|
| 1719 | - $query->expr()->eq('c.calendarid', |
|
| 1720 | - $query->createNamedParameter($id)), |
|
| 1721 | - $query->expr()->eq('c.classification', |
|
| 1722 | - $query->createNamedParameter(self::CLASSIFICATION_PUBLIC)), |
|
| 1723 | - $query->expr()->eq('c.calendartype', |
|
| 1724 | - $query->createNamedParameter(self::CALENDAR_TYPE_CALENDAR))); |
|
| 1725 | - } |
|
| 1726 | - |
|
| 1727 | - if (count($calendarExpressions) === 1) { |
|
| 1728 | - $calExpr = $calendarExpressions[0]; |
|
| 1729 | - } else { |
|
| 1730 | - $calExpr = call_user_func_array([$query->expr(), 'orX'], $calendarExpressions); |
|
| 1731 | - } |
|
| 1732 | - |
|
| 1733 | - // Component expressions |
|
| 1734 | - $compExpressions = []; |
|
| 1735 | - foreach ($filters['comps'] as $comp) { |
|
| 1736 | - $compExpressions[] = $query->expr() |
|
| 1737 | - ->eq('c.componenttype', $query->createNamedParameter($comp)); |
|
| 1738 | - } |
|
| 1739 | - |
|
| 1740 | - if (count($compExpressions) === 1) { |
|
| 1741 | - $compExpr = $compExpressions[0]; |
|
| 1742 | - } else { |
|
| 1743 | - $compExpr = call_user_func_array([$query->expr(), 'orX'], $compExpressions); |
|
| 1744 | - } |
|
| 1745 | - |
|
| 1746 | - if (!isset($filters['props'])) { |
|
| 1747 | - $filters['props'] = []; |
|
| 1748 | - } |
|
| 1749 | - if (!isset($filters['params'])) { |
|
| 1750 | - $filters['params'] = []; |
|
| 1751 | - } |
|
| 1752 | - |
|
| 1753 | - $propParamExpressions = []; |
|
| 1754 | - foreach ($filters['props'] as $prop) { |
|
| 1755 | - $propParamExpressions[] = $query->expr()->andX( |
|
| 1756 | - $query->expr()->eq('i.name', $query->createNamedParameter($prop)), |
|
| 1757 | - $query->expr()->isNull('i.parameter') |
|
| 1758 | - ); |
|
| 1759 | - } |
|
| 1760 | - foreach ($filters['params'] as $param) { |
|
| 1761 | - $propParamExpressions[] = $query->expr()->andX( |
|
| 1762 | - $query->expr()->eq('i.name', $query->createNamedParameter($param['property'])), |
|
| 1763 | - $query->expr()->eq('i.parameter', $query->createNamedParameter($param['parameter'])) |
|
| 1764 | - ); |
|
| 1765 | - } |
|
| 1766 | - |
|
| 1767 | - if (count($propParamExpressions) === 1) { |
|
| 1768 | - $propParamExpr = $propParamExpressions[0]; |
|
| 1769 | - } else { |
|
| 1770 | - $propParamExpr = call_user_func_array([$query->expr(), 'orX'], $propParamExpressions); |
|
| 1771 | - } |
|
| 1772 | - |
|
| 1773 | - $query->select(['c.calendarid', 'c.uri']) |
|
| 1774 | - ->from($this->dbObjectPropertiesTable, 'i') |
|
| 1775 | - ->join('i', 'calendarobjects', 'c', $query->expr()->eq('i.objectid', 'c.id')) |
|
| 1776 | - ->where($calExpr) |
|
| 1777 | - ->andWhere($compExpr) |
|
| 1778 | - ->andWhere($propParamExpr) |
|
| 1779 | - ->andWhere($query->expr()->iLike('i.value', |
|
| 1780 | - $query->createNamedParameter('%'.$this->db->escapeLikeParameter($filters['search-term']).'%'))) |
|
| 1781 | - ->andWhere($query->expr()->isNull('deleted_at')); |
|
| 1782 | - |
|
| 1783 | - if ($offset) { |
|
| 1784 | - $query->setFirstResult($offset); |
|
| 1785 | - } |
|
| 1786 | - if ($limit) { |
|
| 1787 | - $query->setMaxResults($limit); |
|
| 1788 | - } |
|
| 1789 | - |
|
| 1790 | - $stmt = $query->executeQuery(); |
|
| 1791 | - |
|
| 1792 | - $result = []; |
|
| 1793 | - while ($row = $stmt->fetch()) { |
|
| 1794 | - $path = $uriMapper[$row['calendarid']] . '/' . $row['uri']; |
|
| 1795 | - if (!in_array($path, $result)) { |
|
| 1796 | - $result[] = $path; |
|
| 1797 | - } |
|
| 1798 | - } |
|
| 1799 | - |
|
| 1800 | - return $result; |
|
| 1801 | - } |
|
| 1802 | - |
|
| 1803 | - /** |
|
| 1804 | - * used for Nextcloud's calendar API |
|
| 1805 | - * |
|
| 1806 | - * @param array $calendarInfo |
|
| 1807 | - * @param string $pattern |
|
| 1808 | - * @param array $searchProperties |
|
| 1809 | - * @param array $options |
|
| 1810 | - * @param integer|null $limit |
|
| 1811 | - * @param integer|null $offset |
|
| 1812 | - * |
|
| 1813 | - * @return array |
|
| 1814 | - */ |
|
| 1815 | - public function search(array $calendarInfo, $pattern, array $searchProperties, |
|
| 1816 | - array $options, $limit, $offset) { |
|
| 1817 | - $outerQuery = $this->db->getQueryBuilder(); |
|
| 1818 | - $innerQuery = $this->db->getQueryBuilder(); |
|
| 1819 | - |
|
| 1820 | - $innerQuery->selectDistinct('op.objectid') |
|
| 1821 | - ->from($this->dbObjectPropertiesTable, 'op') |
|
| 1822 | - ->andWhere($innerQuery->expr()->eq('op.calendarid', |
|
| 1823 | - $outerQuery->createNamedParameter($calendarInfo['id']))) |
|
| 1824 | - ->andWhere($innerQuery->expr()->eq('op.calendartype', |
|
| 1825 | - $outerQuery->createNamedParameter(self::CALENDAR_TYPE_CALENDAR))); |
|
| 1826 | - |
|
| 1827 | - // only return public items for shared calendars for now |
|
| 1828 | - if (isset($calendarInfo['{http://owncloud.org/ns}owner-principal']) === false || $calendarInfo['principaluri'] !== $calendarInfo['{http://owncloud.org/ns}owner-principal']) { |
|
| 1829 | - $innerQuery->andWhere($innerQuery->expr()->eq('c.classification', |
|
| 1830 | - $outerQuery->createNamedParameter(self::CLASSIFICATION_PUBLIC))); |
|
| 1831 | - } |
|
| 1832 | - |
|
| 1833 | - if (!empty($searchProperties)) { |
|
| 1834 | - $or = $innerQuery->expr()->orX(); |
|
| 1835 | - foreach ($searchProperties as $searchProperty) { |
|
| 1836 | - $or->add($innerQuery->expr()->eq('op.name', |
|
| 1837 | - $outerQuery->createNamedParameter($searchProperty))); |
|
| 1838 | - } |
|
| 1839 | - $innerQuery->andWhere($or); |
|
| 1840 | - } |
|
| 1841 | - |
|
| 1842 | - if ($pattern !== '') { |
|
| 1843 | - $innerQuery->andWhere($innerQuery->expr()->iLike('op.value', |
|
| 1844 | - $outerQuery->createNamedParameter('%' . |
|
| 1845 | - $this->db->escapeLikeParameter($pattern) . '%'))); |
|
| 1846 | - } |
|
| 1847 | - |
|
| 1848 | - $outerQuery->select('c.id', 'c.calendardata', 'c.componenttype', 'c.uid', 'c.uri') |
|
| 1849 | - ->from('calendarobjects', 'c') |
|
| 1850 | - ->where($outerQuery->expr()->isNull('deleted_at')); |
|
| 1851 | - |
|
| 1852 | - if (isset($options['timerange'])) { |
|
| 1853 | - if (isset($options['timerange']['start']) && $options['timerange']['start'] instanceof DateTimeInterface) { |
|
| 1854 | - $outerQuery->andWhere($outerQuery->expr()->gt('lastoccurence', |
|
| 1855 | - $outerQuery->createNamedParameter($options['timerange']['start']->getTimeStamp()))); |
|
| 1856 | - } |
|
| 1857 | - if (isset($options['timerange']['end']) && $options['timerange']['end'] instanceof DateTimeInterface) { |
|
| 1858 | - $outerQuery->andWhere($outerQuery->expr()->lt('firstoccurence', |
|
| 1859 | - $outerQuery->createNamedParameter($options['timerange']['end']->getTimeStamp()))); |
|
| 1860 | - } |
|
| 1861 | - } |
|
| 1862 | - |
|
| 1863 | - if(isset($options['uid'])) { |
|
| 1864 | - $outerQuery->andWhere($outerQuery->expr()->eq('uid', $outerQuery->createNamedParameter($options['uid']))); |
|
| 1865 | - } |
|
| 1866 | - |
|
| 1867 | - if (!empty($options['types'])) { |
|
| 1868 | - $or = $outerQuery->expr()->orX(); |
|
| 1869 | - foreach ($options['types'] as $type) { |
|
| 1870 | - $or->add($outerQuery->expr()->eq('componenttype', |
|
| 1871 | - $outerQuery->createNamedParameter($type))); |
|
| 1872 | - } |
|
| 1873 | - $outerQuery->andWhere($or); |
|
| 1874 | - } |
|
| 1875 | - |
|
| 1876 | - $outerQuery->andWhere($outerQuery->expr()->in('c.id', $outerQuery->createFunction($innerQuery->getSQL()))); |
|
| 1877 | - |
|
| 1878 | - if ($offset) { |
|
| 1879 | - $outerQuery->setFirstResult($offset); |
|
| 1880 | - } |
|
| 1881 | - if ($limit) { |
|
| 1882 | - $outerQuery->setMaxResults($limit); |
|
| 1883 | - } |
|
| 1884 | - |
|
| 1885 | - $result = $outerQuery->executeQuery(); |
|
| 1886 | - $calendarObjects = array_filter($result->fetchAll(), function (array $row) use ($options) { |
|
| 1887 | - $start = $options['timerange']['start'] ?? null; |
|
| 1888 | - $end = $options['timerange']['end'] ?? null; |
|
| 1889 | - |
|
| 1890 | - if ($start === null || !($start instanceof DateTimeInterface) || $end === null || !($end instanceof DateTimeInterface)) { |
|
| 1891 | - // No filter required |
|
| 1892 | - return true; |
|
| 1893 | - } |
|
| 1894 | - |
|
| 1895 | - $isValid = $this->validateFilterForObject($row, [ |
|
| 1896 | - 'name' => 'VCALENDAR', |
|
| 1897 | - 'comp-filters' => [ |
|
| 1898 | - [ |
|
| 1899 | - 'name' => 'VEVENT', |
|
| 1900 | - 'comp-filters' => [], |
|
| 1901 | - 'prop-filters' => [], |
|
| 1902 | - 'is-not-defined' => false, |
|
| 1903 | - 'time-range' => [ |
|
| 1904 | - 'start' => $start, |
|
| 1905 | - 'end' => $end, |
|
| 1906 | - ], |
|
| 1907 | - ], |
|
| 1908 | - ], |
|
| 1909 | - 'prop-filters' => [], |
|
| 1910 | - 'is-not-defined' => false, |
|
| 1911 | - 'time-range' => null, |
|
| 1912 | - ]); |
|
| 1913 | - if (is_resource($row['calendardata'])) { |
|
| 1914 | - // Put the stream back to the beginning so it can be read another time |
|
| 1915 | - rewind($row['calendardata']); |
|
| 1916 | - } |
|
| 1917 | - return $isValid; |
|
| 1918 | - }); |
|
| 1919 | - $result->closeCursor(); |
|
| 1920 | - |
|
| 1921 | - return array_map(function ($o) { |
|
| 1922 | - $calendarData = Reader::read($o['calendardata']); |
|
| 1923 | - $comps = $calendarData->getComponents(); |
|
| 1924 | - $objects = []; |
|
| 1925 | - $timezones = []; |
|
| 1926 | - foreach ($comps as $comp) { |
|
| 1927 | - if ($comp instanceof VTimeZone) { |
|
| 1928 | - $timezones[] = $comp; |
|
| 1929 | - } else { |
|
| 1930 | - $objects[] = $comp; |
|
| 1931 | - } |
|
| 1932 | - } |
|
| 1933 | - |
|
| 1934 | - return [ |
|
| 1935 | - 'id' => $o['id'], |
|
| 1936 | - 'type' => $o['componenttype'], |
|
| 1937 | - 'uid' => $o['uid'], |
|
| 1938 | - 'uri' => $o['uri'], |
|
| 1939 | - 'objects' => array_map(function ($c) { |
|
| 1940 | - return $this->transformSearchData($c); |
|
| 1941 | - }, $objects), |
|
| 1942 | - 'timezones' => array_map(function ($c) { |
|
| 1943 | - return $this->transformSearchData($c); |
|
| 1944 | - }, $timezones), |
|
| 1945 | - ]; |
|
| 1946 | - }, $calendarObjects); |
|
| 1947 | - } |
|
| 1948 | - |
|
| 1949 | - /** |
|
| 1950 | - * @param Component $comp |
|
| 1951 | - * @return array |
|
| 1952 | - */ |
|
| 1953 | - private function transformSearchData(Component $comp) { |
|
| 1954 | - $data = []; |
|
| 1955 | - /** @var Component[] $subComponents */ |
|
| 1956 | - $subComponents = $comp->getComponents(); |
|
| 1957 | - /** @var Property[] $properties */ |
|
| 1958 | - $properties = array_filter($comp->children(), function ($c) { |
|
| 1959 | - return $c instanceof Property; |
|
| 1960 | - }); |
|
| 1961 | - $validationRules = $comp->getValidationRules(); |
|
| 1962 | - |
|
| 1963 | - foreach ($subComponents as $subComponent) { |
|
| 1964 | - $name = $subComponent->name; |
|
| 1965 | - if (!isset($data[$name])) { |
|
| 1966 | - $data[$name] = []; |
|
| 1967 | - } |
|
| 1968 | - $data[$name][] = $this->transformSearchData($subComponent); |
|
| 1969 | - } |
|
| 1970 | - |
|
| 1971 | - foreach ($properties as $property) { |
|
| 1972 | - $name = $property->name; |
|
| 1973 | - if (!isset($validationRules[$name])) { |
|
| 1974 | - $validationRules[$name] = '*'; |
|
| 1975 | - } |
|
| 1976 | - |
|
| 1977 | - $rule = $validationRules[$property->name]; |
|
| 1978 | - if ($rule === '+' || $rule === '*') { // multiple |
|
| 1979 | - if (!isset($data[$name])) { |
|
| 1980 | - $data[$name] = []; |
|
| 1981 | - } |
|
| 1982 | - |
|
| 1983 | - $data[$name][] = $this->transformSearchProperty($property); |
|
| 1984 | - } else { // once |
|
| 1985 | - $data[$name] = $this->transformSearchProperty($property); |
|
| 1986 | - } |
|
| 1987 | - } |
|
| 1988 | - |
|
| 1989 | - return $data; |
|
| 1990 | - } |
|
| 1991 | - |
|
| 1992 | - /** |
|
| 1993 | - * @param Property $prop |
|
| 1994 | - * @return array |
|
| 1995 | - */ |
|
| 1996 | - private function transformSearchProperty(Property $prop) { |
|
| 1997 | - // No need to check Date, as it extends DateTime |
|
| 1998 | - if ($prop instanceof Property\ICalendar\DateTime) { |
|
| 1999 | - $value = $prop->getDateTime(); |
|
| 2000 | - } else { |
|
| 2001 | - $value = $prop->getValue(); |
|
| 2002 | - } |
|
| 2003 | - |
|
| 2004 | - return [ |
|
| 2005 | - $value, |
|
| 2006 | - $prop->parameters() |
|
| 2007 | - ]; |
|
| 2008 | - } |
|
| 2009 | - |
|
| 2010 | - /** |
|
| 2011 | - * @param string $principalUri |
|
| 2012 | - * @param string $pattern |
|
| 2013 | - * @param array $componentTypes |
|
| 2014 | - * @param array $searchProperties |
|
| 2015 | - * @param array $searchParameters |
|
| 2016 | - * @param array $options |
|
| 2017 | - * @return array |
|
| 2018 | - */ |
|
| 2019 | - public function searchPrincipalUri(string $principalUri, |
|
| 2020 | - string $pattern, |
|
| 2021 | - array $componentTypes, |
|
| 2022 | - array $searchProperties, |
|
| 2023 | - array $searchParameters, |
|
| 2024 | - array $options = []): array { |
|
| 2025 | - $escapePattern = !\array_key_exists('escape_like_param', $options) || $options['escape_like_param'] !== false; |
|
| 2026 | - |
|
| 2027 | - $calendarObjectIdQuery = $this->db->getQueryBuilder(); |
|
| 2028 | - $calendarOr = $calendarObjectIdQuery->expr()->orX(); |
|
| 2029 | - $searchOr = $calendarObjectIdQuery->expr()->orX(); |
|
| 2030 | - |
|
| 2031 | - // Fetch calendars and subscription |
|
| 2032 | - $calendars = $this->getCalendarsForUser($principalUri); |
|
| 2033 | - $subscriptions = $this->getSubscriptionsForUser($principalUri); |
|
| 2034 | - foreach ($calendars as $calendar) { |
|
| 2035 | - $calendarAnd = $calendarObjectIdQuery->expr()->andX(); |
|
| 2036 | - $calendarAnd->add($calendarObjectIdQuery->expr()->eq('cob.calendarid', $calendarObjectIdQuery->createNamedParameter((int)$calendar['id']))); |
|
| 2037 | - $calendarAnd->add($calendarObjectIdQuery->expr()->eq('cob.calendartype', $calendarObjectIdQuery->createNamedParameter(self::CALENDAR_TYPE_CALENDAR))); |
|
| 2038 | - |
|
| 2039 | - // If it's shared, limit search to public events |
|
| 2040 | - if (isset($calendar['{http://owncloud.org/ns}owner-principal']) |
|
| 2041 | - && $calendar['principaluri'] !== $calendar['{http://owncloud.org/ns}owner-principal']) { |
|
| 2042 | - $calendarAnd->add($calendarObjectIdQuery->expr()->eq('co.classification', $calendarObjectIdQuery->createNamedParameter(self::CLASSIFICATION_PUBLIC))); |
|
| 2043 | - } |
|
| 2044 | - |
|
| 2045 | - $calendarOr->add($calendarAnd); |
|
| 2046 | - } |
|
| 2047 | - foreach ($subscriptions as $subscription) { |
|
| 2048 | - $subscriptionAnd = $calendarObjectIdQuery->expr()->andX(); |
|
| 2049 | - $subscriptionAnd->add($calendarObjectIdQuery->expr()->eq('cob.calendarid', $calendarObjectIdQuery->createNamedParameter((int)$subscription['id']))); |
|
| 2050 | - $subscriptionAnd->add($calendarObjectIdQuery->expr()->eq('cob.calendartype', $calendarObjectIdQuery->createNamedParameter(self::CALENDAR_TYPE_SUBSCRIPTION))); |
|
| 2051 | - |
|
| 2052 | - // If it's shared, limit search to public events |
|
| 2053 | - if (isset($subscription['{http://owncloud.org/ns}owner-principal']) |
|
| 2054 | - && $subscription['principaluri'] !== $subscription['{http://owncloud.org/ns}owner-principal']) { |
|
| 2055 | - $subscriptionAnd->add($calendarObjectIdQuery->expr()->eq('co.classification', $calendarObjectIdQuery->createNamedParameter(self::CLASSIFICATION_PUBLIC))); |
|
| 2056 | - } |
|
| 2057 | - |
|
| 2058 | - $calendarOr->add($subscriptionAnd); |
|
| 2059 | - } |
|
| 2060 | - |
|
| 2061 | - foreach ($searchProperties as $property) { |
|
| 2062 | - $propertyAnd = $calendarObjectIdQuery->expr()->andX(); |
|
| 2063 | - $propertyAnd->add($calendarObjectIdQuery->expr()->eq('cob.name', $calendarObjectIdQuery->createNamedParameter($property, IQueryBuilder::PARAM_STR))); |
|
| 2064 | - $propertyAnd->add($calendarObjectIdQuery->expr()->isNull('cob.parameter')); |
|
| 2065 | - |
|
| 2066 | - $searchOr->add($propertyAnd); |
|
| 2067 | - } |
|
| 2068 | - foreach ($searchParameters as $property => $parameter) { |
|
| 2069 | - $parameterAnd = $calendarObjectIdQuery->expr()->andX(); |
|
| 2070 | - $parameterAnd->add($calendarObjectIdQuery->expr()->eq('cob.name', $calendarObjectIdQuery->createNamedParameter($property, IQueryBuilder::PARAM_STR))); |
|
| 2071 | - $parameterAnd->add($calendarObjectIdQuery->expr()->eq('cob.parameter', $calendarObjectIdQuery->createNamedParameter($parameter, IQueryBuilder::PARAM_STR_ARRAY))); |
|
| 2072 | - |
|
| 2073 | - $searchOr->add($parameterAnd); |
|
| 2074 | - } |
|
| 2075 | - |
|
| 2076 | - if ($calendarOr->count() === 0) { |
|
| 2077 | - return []; |
|
| 2078 | - } |
|
| 2079 | - if ($searchOr->count() === 0) { |
|
| 2080 | - return []; |
|
| 2081 | - } |
|
| 2082 | - |
|
| 2083 | - $calendarObjectIdQuery->selectDistinct('cob.objectid') |
|
| 2084 | - ->from($this->dbObjectPropertiesTable, 'cob') |
|
| 2085 | - ->leftJoin('cob', 'calendarobjects', 'co', $calendarObjectIdQuery->expr()->eq('co.id', 'cob.objectid')) |
|
| 2086 | - ->andWhere($calendarObjectIdQuery->expr()->in('co.componenttype', $calendarObjectIdQuery->createNamedParameter($componentTypes, IQueryBuilder::PARAM_STR_ARRAY))) |
|
| 2087 | - ->andWhere($calendarOr) |
|
| 2088 | - ->andWhere($searchOr) |
|
| 2089 | - ->andWhere($calendarObjectIdQuery->expr()->isNull('deleted_at')); |
|
| 2090 | - |
|
| 2091 | - if ('' !== $pattern) { |
|
| 2092 | - if (!$escapePattern) { |
|
| 2093 | - $calendarObjectIdQuery->andWhere($calendarObjectIdQuery->expr()->ilike('cob.value', $calendarObjectIdQuery->createNamedParameter($pattern))); |
|
| 2094 | - } else { |
|
| 2095 | - $calendarObjectIdQuery->andWhere($calendarObjectIdQuery->expr()->ilike('cob.value', $calendarObjectIdQuery->createNamedParameter('%' . $this->db->escapeLikeParameter($pattern) . '%'))); |
|
| 2096 | - } |
|
| 2097 | - } |
|
| 2098 | - |
|
| 2099 | - if (isset($options['limit'])) { |
|
| 2100 | - $calendarObjectIdQuery->setMaxResults($options['limit']); |
|
| 2101 | - } |
|
| 2102 | - if (isset($options['offset'])) { |
|
| 2103 | - $calendarObjectIdQuery->setFirstResult($options['offset']); |
|
| 2104 | - } |
|
| 2105 | - |
|
| 2106 | - $result = $calendarObjectIdQuery->executeQuery(); |
|
| 2107 | - $matches = $result->fetchAll(); |
|
| 2108 | - $result->closeCursor(); |
|
| 2109 | - $matches = array_map(static function (array $match):int { |
|
| 2110 | - return (int) $match['objectid']; |
|
| 2111 | - }, $matches); |
|
| 2112 | - |
|
| 2113 | - $query = $this->db->getQueryBuilder(); |
|
| 2114 | - $query->select('calendardata', 'uri', 'calendarid', 'calendartype') |
|
| 2115 | - ->from('calendarobjects') |
|
| 2116 | - ->where($query->expr()->in('id', $query->createNamedParameter($matches, IQueryBuilder::PARAM_INT_ARRAY))); |
|
| 2117 | - |
|
| 2118 | - $result = $query->executeQuery(); |
|
| 2119 | - $calendarObjects = $result->fetchAll(); |
|
| 2120 | - $result->closeCursor(); |
|
| 2121 | - |
|
| 2122 | - return array_map(function (array $array): array { |
|
| 2123 | - $array['calendarid'] = (int)$array['calendarid']; |
|
| 2124 | - $array['calendartype'] = (int)$array['calendartype']; |
|
| 2125 | - $array['calendardata'] = $this->readBlob($array['calendardata']); |
|
| 2126 | - |
|
| 2127 | - return $array; |
|
| 2128 | - }, $calendarObjects); |
|
| 2129 | - } |
|
| 2130 | - |
|
| 2131 | - /** |
|
| 2132 | - * Searches through all of a users calendars and calendar objects to find |
|
| 2133 | - * an object with a specific UID. |
|
| 2134 | - * |
|
| 2135 | - * This method should return the path to this object, relative to the |
|
| 2136 | - * calendar home, so this path usually only contains two parts: |
|
| 2137 | - * |
|
| 2138 | - * calendarpath/objectpath.ics |
|
| 2139 | - * |
|
| 2140 | - * If the uid is not found, return null. |
|
| 2141 | - * |
|
| 2142 | - * This method should only consider * objects that the principal owns, so |
|
| 2143 | - * any calendars owned by other principals that also appear in this |
|
| 2144 | - * collection should be ignored. |
|
| 2145 | - * |
|
| 2146 | - * @param string $principalUri |
|
| 2147 | - * @param string $uid |
|
| 2148 | - * @return string|null |
|
| 2149 | - */ |
|
| 2150 | - public function getCalendarObjectByUID($principalUri, $uid) { |
|
| 2151 | - $query = $this->db->getQueryBuilder(); |
|
| 2152 | - $query->selectAlias('c.uri', 'calendaruri')->selectAlias('co.uri', 'objecturi') |
|
| 2153 | - ->from('calendarobjects', 'co') |
|
| 2154 | - ->leftJoin('co', 'calendars', 'c', $query->expr()->eq('co.calendarid', 'c.id')) |
|
| 2155 | - ->where($query->expr()->eq('c.principaluri', $query->createNamedParameter($principalUri))) |
|
| 2156 | - ->andWhere($query->expr()->eq('co.uid', $query->createNamedParameter($uid))) |
|
| 2157 | - ->andWhere($query->expr()->isNull('co.deleted_at')); |
|
| 2158 | - $stmt = $query->executeQuery(); |
|
| 2159 | - $row = $stmt->fetch(); |
|
| 2160 | - $stmt->closeCursor(); |
|
| 2161 | - if ($row) { |
|
| 2162 | - return $row['calendaruri'] . '/' . $row['objecturi']; |
|
| 2163 | - } |
|
| 2164 | - |
|
| 2165 | - return null; |
|
| 2166 | - } |
|
| 2167 | - |
|
| 2168 | - public function getCalendarObjectById(string $principalUri, int $id): ?array { |
|
| 2169 | - $query = $this->db->getQueryBuilder(); |
|
| 2170 | - $query->select(['co.id', 'co.uri', 'co.lastmodified', 'co.etag', 'co.calendarid', 'co.size', 'co.calendardata', 'co.componenttype', 'co.classification', 'co.deleted_at']) |
|
| 2171 | - ->selectAlias('c.uri', 'calendaruri') |
|
| 2172 | - ->from('calendarobjects', 'co') |
|
| 2173 | - ->join('co', 'calendars', 'c', $query->expr()->eq('c.id', 'co.calendarid', IQueryBuilder::PARAM_INT)) |
|
| 2174 | - ->where($query->expr()->eq('c.principaluri', $query->createNamedParameter($principalUri))) |
|
| 2175 | - ->andWhere($query->expr()->eq('co.id', $query->createNamedParameter($id, IQueryBuilder::PARAM_INT), IQueryBuilder::PARAM_INT)); |
|
| 2176 | - $stmt = $query->executeQuery(); |
|
| 2177 | - $row = $stmt->fetch(); |
|
| 2178 | - $stmt->closeCursor(); |
|
| 2179 | - |
|
| 2180 | - if (!$row) { |
|
| 2181 | - return null; |
|
| 2182 | - } |
|
| 2183 | - |
|
| 2184 | - return [ |
|
| 2185 | - 'id' => $row['id'], |
|
| 2186 | - 'uri' => $row['uri'], |
|
| 2187 | - 'lastmodified' => $row['lastmodified'], |
|
| 2188 | - 'etag' => '"' . $row['etag'] . '"', |
|
| 2189 | - 'calendarid' => $row['calendarid'], |
|
| 2190 | - 'calendaruri' => $row['calendaruri'], |
|
| 2191 | - 'size' => (int)$row['size'], |
|
| 2192 | - 'calendardata' => $this->readBlob($row['calendardata']), |
|
| 2193 | - 'component' => strtolower($row['componenttype']), |
|
| 2194 | - 'classification' => (int)$row['classification'], |
|
| 2195 | - 'deleted_at' => isset($row['deleted_at']) ? ((int) $row['deleted_at']) : null, |
|
| 2196 | - ]; |
|
| 2197 | - } |
|
| 2198 | - |
|
| 2199 | - /** |
|
| 2200 | - * The getChanges method returns all the changes that have happened, since |
|
| 2201 | - * the specified syncToken in the specified calendar. |
|
| 2202 | - * |
|
| 2203 | - * This function should return an array, such as the following: |
|
| 2204 | - * |
|
| 2205 | - * [ |
|
| 2206 | - * 'syncToken' => 'The current synctoken', |
|
| 2207 | - * 'added' => [ |
|
| 2208 | - * 'new.txt', |
|
| 2209 | - * ], |
|
| 2210 | - * 'modified' => [ |
|
| 2211 | - * 'modified.txt', |
|
| 2212 | - * ], |
|
| 2213 | - * 'deleted' => [ |
|
| 2214 | - * 'foo.php.bak', |
|
| 2215 | - * 'old.txt' |
|
| 2216 | - * ] |
|
| 2217 | - * ); |
|
| 2218 | - * |
|
| 2219 | - * The returned syncToken property should reflect the *current* syncToken |
|
| 2220 | - * of the calendar, as reported in the {http://sabredav.org/ns}sync-token |
|
| 2221 | - * property This is * needed here too, to ensure the operation is atomic. |
|
| 2222 | - * |
|
| 2223 | - * If the $syncToken argument is specified as null, this is an initial |
|
| 2224 | - * sync, and all members should be reported. |
|
| 2225 | - * |
|
| 2226 | - * The modified property is an array of nodenames that have changed since |
|
| 2227 | - * the last token. |
|
| 2228 | - * |
|
| 2229 | - * The deleted property is an array with nodenames, that have been deleted |
|
| 2230 | - * from collection. |
|
| 2231 | - * |
|
| 2232 | - * The $syncLevel argument is basically the 'depth' of the report. If it's |
|
| 2233 | - * 1, you only have to report changes that happened only directly in |
|
| 2234 | - * immediate descendants. If it's 2, it should also include changes from |
|
| 2235 | - * the nodes below the child collections. (grandchildren) |
|
| 2236 | - * |
|
| 2237 | - * The $limit argument allows a client to specify how many results should |
|
| 2238 | - * be returned at most. If the limit is not specified, it should be treated |
|
| 2239 | - * as infinite. |
|
| 2240 | - * |
|
| 2241 | - * If the limit (infinite or not) is higher than you're willing to return, |
|
| 2242 | - * you should throw a Sabre\DAV\Exception\TooMuchMatches() exception. |
|
| 2243 | - * |
|
| 2244 | - * If the syncToken is expired (due to data cleanup) or unknown, you must |
|
| 2245 | - * return null. |
|
| 2246 | - * |
|
| 2247 | - * The limit is 'suggestive'. You are free to ignore it. |
|
| 2248 | - * |
|
| 2249 | - * @param string $calendarId |
|
| 2250 | - * @param string $syncToken |
|
| 2251 | - * @param int $syncLevel |
|
| 2252 | - * @param int|null $limit |
|
| 2253 | - * @param int $calendarType |
|
| 2254 | - * @return array |
|
| 2255 | - */ |
|
| 2256 | - public function getChangesForCalendar($calendarId, $syncToken, $syncLevel, $limit = null, $calendarType = self::CALENDAR_TYPE_CALENDAR) { |
|
| 2257 | - // Current synctoken |
|
| 2258 | - $qb = $this->db->getQueryBuilder(); |
|
| 2259 | - $qb->select('synctoken') |
|
| 2260 | - ->from('calendars') |
|
| 2261 | - ->where( |
|
| 2262 | - $qb->expr()->eq('id', $qb->createNamedParameter($calendarId)) |
|
| 2263 | - ); |
|
| 2264 | - $stmt = $qb->executeQuery(); |
|
| 2265 | - $currentToken = $stmt->fetchOne(); |
|
| 2266 | - |
|
| 2267 | - if ($currentToken === false) { |
|
| 2268 | - return null; |
|
| 2269 | - } |
|
| 2270 | - |
|
| 2271 | - $result = [ |
|
| 2272 | - 'syncToken' => $currentToken, |
|
| 2273 | - 'added' => [], |
|
| 2274 | - 'modified' => [], |
|
| 2275 | - 'deleted' => [], |
|
| 2276 | - ]; |
|
| 2277 | - |
|
| 2278 | - if ($syncToken) { |
|
| 2279 | - $qb = $this->db->getQueryBuilder(); |
|
| 2280 | - |
|
| 2281 | - $qb->select('uri', 'operation') |
|
| 2282 | - ->from('calendarchanges') |
|
| 2283 | - ->where( |
|
| 2284 | - $qb->expr()->andX( |
|
| 2285 | - $qb->expr()->gte('synctoken', $qb->createNamedParameter($syncToken)), |
|
| 2286 | - $qb->expr()->lt('synctoken', $qb->createNamedParameter($currentToken)), |
|
| 2287 | - $qb->expr()->eq('calendarid', $qb->createNamedParameter($calendarId)), |
|
| 2288 | - $qb->expr()->eq('calendartype', $qb->createNamedParameter($calendarType)) |
|
| 2289 | - ) |
|
| 2290 | - )->orderBy('synctoken'); |
|
| 2291 | - if (is_int($limit) && $limit > 0) { |
|
| 2292 | - $qb->setMaxResults($limit); |
|
| 2293 | - } |
|
| 2294 | - |
|
| 2295 | - // Fetching all changes |
|
| 2296 | - $stmt = $qb->executeQuery(); |
|
| 2297 | - $changes = []; |
|
| 2298 | - |
|
| 2299 | - // This loop ensures that any duplicates are overwritten, only the |
|
| 2300 | - // last change on a node is relevant. |
|
| 2301 | - while ($row = $stmt->fetch()) { |
|
| 2302 | - $changes[$row['uri']] = $row['operation']; |
|
| 2303 | - } |
|
| 2304 | - $stmt->closeCursor(); |
|
| 2305 | - |
|
| 2306 | - foreach ($changes as $uri => $operation) { |
|
| 2307 | - switch ($operation) { |
|
| 2308 | - case 1: |
|
| 2309 | - $result['added'][] = $uri; |
|
| 2310 | - break; |
|
| 2311 | - case 2: |
|
| 2312 | - $result['modified'][] = $uri; |
|
| 2313 | - break; |
|
| 2314 | - case 3: |
|
| 2315 | - $result['deleted'][] = $uri; |
|
| 2316 | - break; |
|
| 2317 | - } |
|
| 2318 | - } |
|
| 2319 | - } else { |
|
| 2320 | - // No synctoken supplied, this is the initial sync. |
|
| 2321 | - $qb = $this->db->getQueryBuilder(); |
|
| 2322 | - $qb->select('uri') |
|
| 2323 | - ->from('calendarobjects') |
|
| 2324 | - ->where( |
|
| 2325 | - $qb->expr()->andX( |
|
| 2326 | - $qb->expr()->eq('calendarid', $qb->createNamedParameter($calendarId)), |
|
| 2327 | - $qb->expr()->eq('calendartype', $qb->createNamedParameter($calendarType)) |
|
| 2328 | - ) |
|
| 2329 | - ); |
|
| 2330 | - $stmt = $qb->executeQuery(); |
|
| 2331 | - $result['added'] = $stmt->fetchAll(\PDO::FETCH_COLUMN); |
|
| 2332 | - $stmt->closeCursor(); |
|
| 2333 | - } |
|
| 2334 | - return $result; |
|
| 2335 | - } |
|
| 2336 | - |
|
| 2337 | - /** |
|
| 2338 | - * Returns a list of subscriptions for a principal. |
|
| 2339 | - * |
|
| 2340 | - * Every subscription is an array with the following keys: |
|
| 2341 | - * * id, a unique id that will be used by other functions to modify the |
|
| 2342 | - * subscription. This can be the same as the uri or a database key. |
|
| 2343 | - * * uri. This is just the 'base uri' or 'filename' of the subscription. |
|
| 2344 | - * * principaluri. The owner of the subscription. Almost always the same as |
|
| 2345 | - * principalUri passed to this method. |
|
| 2346 | - * |
|
| 2347 | - * Furthermore, all the subscription info must be returned too: |
|
| 2348 | - * |
|
| 2349 | - * 1. {DAV:}displayname |
|
| 2350 | - * 2. {http://apple.com/ns/ical/}refreshrate |
|
| 2351 | - * 3. {http://calendarserver.org/ns/}subscribed-strip-todos (omit if todos |
|
| 2352 | - * should not be stripped). |
|
| 2353 | - * 4. {http://calendarserver.org/ns/}subscribed-strip-alarms (omit if alarms |
|
| 2354 | - * should not be stripped). |
|
| 2355 | - * 5. {http://calendarserver.org/ns/}subscribed-strip-attachments (omit if |
|
| 2356 | - * attachments should not be stripped). |
|
| 2357 | - * 6. {http://calendarserver.org/ns/}source (Must be a |
|
| 2358 | - * Sabre\DAV\Property\Href). |
|
| 2359 | - * 7. {http://apple.com/ns/ical/}calendar-color |
|
| 2360 | - * 8. {http://apple.com/ns/ical/}calendar-order |
|
| 2361 | - * 9. {urn:ietf:params:xml:ns:caldav}supported-calendar-component-set |
|
| 2362 | - * (should just be an instance of |
|
| 2363 | - * Sabre\CalDAV\Property\SupportedCalendarComponentSet, with a bunch of |
|
| 2364 | - * default components). |
|
| 2365 | - * |
|
| 2366 | - * @param string $principalUri |
|
| 2367 | - * @return array |
|
| 2368 | - */ |
|
| 2369 | - public function getSubscriptionsForUser($principalUri) { |
|
| 2370 | - $fields = array_column($this->subscriptionPropertyMap, 0); |
|
| 2371 | - $fields[] = 'id'; |
|
| 2372 | - $fields[] = 'uri'; |
|
| 2373 | - $fields[] = 'source'; |
|
| 2374 | - $fields[] = 'principaluri'; |
|
| 2375 | - $fields[] = 'lastmodified'; |
|
| 2376 | - $fields[] = 'synctoken'; |
|
| 2377 | - |
|
| 2378 | - $query = $this->db->getQueryBuilder(); |
|
| 2379 | - $query->select($fields) |
|
| 2380 | - ->from('calendarsubscriptions') |
|
| 2381 | - ->where($query->expr()->eq('principaluri', $query->createNamedParameter($principalUri))) |
|
| 2382 | - ->orderBy('calendarorder', 'asc'); |
|
| 2383 | - $stmt = $query->executeQuery(); |
|
| 2384 | - |
|
| 2385 | - $subscriptions = []; |
|
| 2386 | - while ($row = $stmt->fetch()) { |
|
| 2387 | - $subscription = [ |
|
| 2388 | - 'id' => $row['id'], |
|
| 2389 | - 'uri' => $row['uri'], |
|
| 2390 | - 'principaluri' => $row['principaluri'], |
|
| 2391 | - 'source' => $row['source'], |
|
| 2392 | - 'lastmodified' => $row['lastmodified'], |
|
| 2393 | - |
|
| 2394 | - '{' . Plugin::NS_CALDAV . '}supported-calendar-component-set' => new SupportedCalendarComponentSet(['VTODO', 'VEVENT']), |
|
| 2395 | - '{http://sabredav.org/ns}sync-token' => $row['synctoken']?$row['synctoken']:'0', |
|
| 2396 | - ]; |
|
| 2397 | - |
|
| 2398 | - $subscriptions[] = $this->rowToSubscription($row, $subscription); |
|
| 2399 | - } |
|
| 2400 | - |
|
| 2401 | - return $subscriptions; |
|
| 2402 | - } |
|
| 2403 | - |
|
| 2404 | - /** |
|
| 2405 | - * Creates a new subscription for a principal. |
|
| 2406 | - * |
|
| 2407 | - * If the creation was a success, an id must be returned that can be used to reference |
|
| 2408 | - * this subscription in other methods, such as updateSubscription. |
|
| 2409 | - * |
|
| 2410 | - * @param string $principalUri |
|
| 2411 | - * @param string $uri |
|
| 2412 | - * @param array $properties |
|
| 2413 | - * @return mixed |
|
| 2414 | - */ |
|
| 2415 | - public function createSubscription($principalUri, $uri, array $properties) { |
|
| 2416 | - if (!isset($properties['{http://calendarserver.org/ns/}source'])) { |
|
| 2417 | - throw new Forbidden('The {http://calendarserver.org/ns/}source property is required when creating subscriptions'); |
|
| 2418 | - } |
|
| 2419 | - |
|
| 2420 | - $values = [ |
|
| 2421 | - 'principaluri' => $principalUri, |
|
| 2422 | - 'uri' => $uri, |
|
| 2423 | - 'source' => $properties['{http://calendarserver.org/ns/}source']->getHref(), |
|
| 2424 | - 'lastmodified' => time(), |
|
| 2425 | - ]; |
|
| 2426 | - |
|
| 2427 | - $propertiesBoolean = ['striptodos', 'stripalarms', 'stripattachments']; |
|
| 2428 | - |
|
| 2429 | - foreach ($this->subscriptionPropertyMap as $xmlName => [$dbName, $type]) { |
|
| 2430 | - if (array_key_exists($xmlName, $properties)) { |
|
| 2431 | - $values[$dbName] = $properties[$xmlName]; |
|
| 2432 | - if (in_array($dbName, $propertiesBoolean)) { |
|
| 2433 | - $values[$dbName] = true; |
|
| 2434 | - } |
|
| 2435 | - } |
|
| 2436 | - } |
|
| 2437 | - |
|
| 2438 | - [$subscriptionId, $subscriptionRow] = $this->atomic(function() use ($values) { |
|
| 2439 | - $valuesToInsert = []; |
|
| 2440 | - $query = $this->db->getQueryBuilder(); |
|
| 2441 | - foreach (array_keys($values) as $name) { |
|
| 2442 | - $valuesToInsert[$name] = $query->createNamedParameter($values[$name]); |
|
| 2443 | - } |
|
| 2444 | - $query->insert('calendarsubscriptions') |
|
| 2445 | - ->values($valuesToInsert) |
|
| 2446 | - ->executeStatement(); |
|
| 2447 | - |
|
| 2448 | - $subscriptionId = $query->getLastInsertId(); |
|
| 2449 | - |
|
| 2450 | - $subscriptionRow = $this->getSubscriptionById($subscriptionId); |
|
| 2451 | - return [$subscriptionId, $subscriptionRow]; |
|
| 2452 | - }, $this->db); |
|
| 2453 | - |
|
| 2454 | - $this->dispatcher->dispatchTyped(new SubscriptionCreatedEvent($subscriptionId, $subscriptionRow)); |
|
| 2455 | - |
|
| 2456 | - return $subscriptionId; |
|
| 2457 | - } |
|
| 2458 | - |
|
| 2459 | - /** |
|
| 2460 | - * Updates a subscription |
|
| 2461 | - * |
|
| 2462 | - * The list of mutations is stored in a Sabre\DAV\PropPatch object. |
|
| 2463 | - * To do the actual updates, you must tell this object which properties |
|
| 2464 | - * you're going to process with the handle() method. |
|
| 2465 | - * |
|
| 2466 | - * Calling the handle method is like telling the PropPatch object "I |
|
| 2467 | - * promise I can handle updating this property". |
|
| 2468 | - * |
|
| 2469 | - * Read the PropPatch documentation for more info and examples. |
|
| 2470 | - * |
|
| 2471 | - * @param mixed $subscriptionId |
|
| 2472 | - * @param PropPatch $propPatch |
|
| 2473 | - * @return void |
|
| 2474 | - */ |
|
| 2475 | - public function updateSubscription($subscriptionId, PropPatch $propPatch) { |
|
| 2476 | - $supportedProperties = array_keys($this->subscriptionPropertyMap); |
|
| 2477 | - $supportedProperties[] = '{http://calendarserver.org/ns/}source'; |
|
| 2478 | - |
|
| 2479 | - $propPatch->handle($supportedProperties, function ($mutations) use ($subscriptionId) { |
|
| 2480 | - $newValues = []; |
|
| 2481 | - |
|
| 2482 | - foreach ($mutations as $propertyName => $propertyValue) { |
|
| 2483 | - if ($propertyName === '{http://calendarserver.org/ns/}source') { |
|
| 2484 | - $newValues['source'] = $propertyValue->getHref(); |
|
| 2485 | - } else { |
|
| 2486 | - $fieldName = $this->subscriptionPropertyMap[$propertyName][0]; |
|
| 2487 | - $newValues[$fieldName] = $propertyValue; |
|
| 2488 | - } |
|
| 2489 | - } |
|
| 2490 | - |
|
| 2491 | - $query = $this->db->getQueryBuilder(); |
|
| 2492 | - $query->update('calendarsubscriptions') |
|
| 2493 | - ->set('lastmodified', $query->createNamedParameter(time())); |
|
| 2494 | - foreach ($newValues as $fieldName => $value) { |
|
| 2495 | - $query->set($fieldName, $query->createNamedParameter($value)); |
|
| 2496 | - } |
|
| 2497 | - $query->where($query->expr()->eq('id', $query->createNamedParameter($subscriptionId))) |
|
| 2498 | - ->executeStatement(); |
|
| 2499 | - |
|
| 2500 | - $subscriptionRow = $this->getSubscriptionById($subscriptionId); |
|
| 2501 | - $this->dispatcher->dispatchTyped(new SubscriptionUpdatedEvent((int)$subscriptionId, $subscriptionRow, [], $mutations)); |
|
| 2502 | - |
|
| 2503 | - return true; |
|
| 2504 | - }); |
|
| 2505 | - } |
|
| 2506 | - |
|
| 2507 | - /** |
|
| 2508 | - * Deletes a subscription. |
|
| 2509 | - * |
|
| 2510 | - * @param mixed $subscriptionId |
|
| 2511 | - * @return void |
|
| 2512 | - */ |
|
| 2513 | - public function deleteSubscription($subscriptionId) { |
|
| 2514 | - $subscriptionRow = $this->getSubscriptionById($subscriptionId); |
|
| 2515 | - |
|
| 2516 | - $query = $this->db->getQueryBuilder(); |
|
| 2517 | - $query->delete('calendarsubscriptions') |
|
| 2518 | - ->where($query->expr()->eq('id', $query->createNamedParameter($subscriptionId))) |
|
| 2519 | - ->executeStatement(); |
|
| 2520 | - |
|
| 2521 | - $query = $this->db->getQueryBuilder(); |
|
| 2522 | - $query->delete('calendarobjects') |
|
| 2523 | - ->where($query->expr()->eq('calendarid', $query->createNamedParameter($subscriptionId))) |
|
| 2524 | - ->andWhere($query->expr()->eq('calendartype', $query->createNamedParameter(self::CALENDAR_TYPE_SUBSCRIPTION))) |
|
| 2525 | - ->executeStatement(); |
|
| 2526 | - |
|
| 2527 | - $query->delete('calendarchanges') |
|
| 2528 | - ->where($query->expr()->eq('calendarid', $query->createNamedParameter($subscriptionId))) |
|
| 2529 | - ->andWhere($query->expr()->eq('calendartype', $query->createNamedParameter(self::CALENDAR_TYPE_SUBSCRIPTION))) |
|
| 2530 | - ->executeStatement(); |
|
| 2531 | - |
|
| 2532 | - $query->delete($this->dbObjectPropertiesTable) |
|
| 2533 | - ->where($query->expr()->eq('calendarid', $query->createNamedParameter($subscriptionId))) |
|
| 2534 | - ->andWhere($query->expr()->eq('calendartype', $query->createNamedParameter(self::CALENDAR_TYPE_SUBSCRIPTION))) |
|
| 2535 | - ->executeStatement(); |
|
| 2536 | - |
|
| 2537 | - if ($subscriptionRow) { |
|
| 2538 | - $this->dispatcher->dispatchTyped(new SubscriptionDeletedEvent((int)$subscriptionId, $subscriptionRow, [])); |
|
| 2539 | - } |
|
| 2540 | - } |
|
| 2541 | - |
|
| 2542 | - /** |
|
| 2543 | - * Returns a single scheduling object for the inbox collection. |
|
| 2544 | - * |
|
| 2545 | - * The returned array should contain the following elements: |
|
| 2546 | - * * uri - A unique basename for the object. This will be used to |
|
| 2547 | - * construct a full uri. |
|
| 2548 | - * * calendardata - The iCalendar object |
|
| 2549 | - * * lastmodified - The last modification date. Can be an int for a unix |
|
| 2550 | - * timestamp, or a PHP DateTime object. |
|
| 2551 | - * * etag - A unique token that must change if the object changed. |
|
| 2552 | - * * size - The size of the object, in bytes. |
|
| 2553 | - * |
|
| 2554 | - * @param string $principalUri |
|
| 2555 | - * @param string $objectUri |
|
| 2556 | - * @return array |
|
| 2557 | - */ |
|
| 2558 | - public function getSchedulingObject($principalUri, $objectUri) { |
|
| 2559 | - $query = $this->db->getQueryBuilder(); |
|
| 2560 | - $stmt = $query->select(['uri', 'calendardata', 'lastmodified', 'etag', 'size']) |
|
| 2561 | - ->from('schedulingobjects') |
|
| 2562 | - ->where($query->expr()->eq('principaluri', $query->createNamedParameter($principalUri))) |
|
| 2563 | - ->andWhere($query->expr()->eq('uri', $query->createNamedParameter($objectUri))) |
|
| 2564 | - ->executeQuery(); |
|
| 2565 | - |
|
| 2566 | - $row = $stmt->fetch(); |
|
| 2567 | - |
|
| 2568 | - if (!$row) { |
|
| 2569 | - return null; |
|
| 2570 | - } |
|
| 2571 | - |
|
| 2572 | - return [ |
|
| 2573 | - 'uri' => $row['uri'], |
|
| 2574 | - 'calendardata' => $row['calendardata'], |
|
| 2575 | - 'lastmodified' => $row['lastmodified'], |
|
| 2576 | - 'etag' => '"' . $row['etag'] . '"', |
|
| 2577 | - 'size' => (int)$row['size'], |
|
| 2578 | - ]; |
|
| 2579 | - } |
|
| 2580 | - |
|
| 2581 | - /** |
|
| 2582 | - * Returns all scheduling objects for the inbox collection. |
|
| 2583 | - * |
|
| 2584 | - * These objects should be returned as an array. Every item in the array |
|
| 2585 | - * should follow the same structure as returned from getSchedulingObject. |
|
| 2586 | - * |
|
| 2587 | - * The main difference is that 'calendardata' is optional. |
|
| 2588 | - * |
|
| 2589 | - * @param string $principalUri |
|
| 2590 | - * @return array |
|
| 2591 | - */ |
|
| 2592 | - public function getSchedulingObjects($principalUri) { |
|
| 2593 | - $query = $this->db->getQueryBuilder(); |
|
| 2594 | - $stmt = $query->select(['uri', 'calendardata', 'lastmodified', 'etag', 'size']) |
|
| 2595 | - ->from('schedulingobjects') |
|
| 2596 | - ->where($query->expr()->eq('principaluri', $query->createNamedParameter($principalUri))) |
|
| 2597 | - ->executeQuery(); |
|
| 2598 | - |
|
| 2599 | - $result = []; |
|
| 2600 | - foreach ($stmt->fetchAll() as $row) { |
|
| 2601 | - $result[] = [ |
|
| 2602 | - 'calendardata' => $row['calendardata'], |
|
| 2603 | - 'uri' => $row['uri'], |
|
| 2604 | - 'lastmodified' => $row['lastmodified'], |
|
| 2605 | - 'etag' => '"' . $row['etag'] . '"', |
|
| 2606 | - 'size' => (int)$row['size'], |
|
| 2607 | - ]; |
|
| 2608 | - } |
|
| 2609 | - $stmt->closeCursor(); |
|
| 2610 | - |
|
| 2611 | - return $result; |
|
| 2612 | - } |
|
| 2613 | - |
|
| 2614 | - /** |
|
| 2615 | - * Deletes a scheduling object from the inbox collection. |
|
| 2616 | - * |
|
| 2617 | - * @param string $principalUri |
|
| 2618 | - * @param string $objectUri |
|
| 2619 | - * @return void |
|
| 2620 | - */ |
|
| 2621 | - public function deleteSchedulingObject($principalUri, $objectUri) { |
|
| 2622 | - $query = $this->db->getQueryBuilder(); |
|
| 2623 | - $query->delete('schedulingobjects') |
|
| 2624 | - ->where($query->expr()->eq('principaluri', $query->createNamedParameter($principalUri))) |
|
| 2625 | - ->andWhere($query->expr()->eq('uri', $query->createNamedParameter($objectUri))) |
|
| 2626 | - ->executeStatement(); |
|
| 2627 | - } |
|
| 2628 | - |
|
| 2629 | - /** |
|
| 2630 | - * Creates a new scheduling object. This should land in a users' inbox. |
|
| 2631 | - * |
|
| 2632 | - * @param string $principalUri |
|
| 2633 | - * @param string $objectUri |
|
| 2634 | - * @param string $objectData |
|
| 2635 | - * @return void |
|
| 2636 | - */ |
|
| 2637 | - public function createSchedulingObject($principalUri, $objectUri, $objectData) { |
|
| 2638 | - $query = $this->db->getQueryBuilder(); |
|
| 2639 | - $query->insert('schedulingobjects') |
|
| 2640 | - ->values([ |
|
| 2641 | - 'principaluri' => $query->createNamedParameter($principalUri), |
|
| 2642 | - 'calendardata' => $query->createNamedParameter($objectData, IQueryBuilder::PARAM_LOB), |
|
| 2643 | - 'uri' => $query->createNamedParameter($objectUri), |
|
| 2644 | - 'lastmodified' => $query->createNamedParameter(time()), |
|
| 2645 | - 'etag' => $query->createNamedParameter(md5($objectData)), |
|
| 2646 | - 'size' => $query->createNamedParameter(strlen($objectData)) |
|
| 2647 | - ]) |
|
| 2648 | - ->executeStatement(); |
|
| 2649 | - } |
|
| 2650 | - |
|
| 2651 | - /** |
|
| 2652 | - * Adds a change record to the calendarchanges table. |
|
| 2653 | - * |
|
| 2654 | - * @param mixed $calendarId |
|
| 2655 | - * @param string $objectUri |
|
| 2656 | - * @param int $operation 1 = add, 2 = modify, 3 = delete. |
|
| 2657 | - * @param int $calendarType |
|
| 2658 | - * @return void |
|
| 2659 | - */ |
|
| 2660 | - protected function addChange($calendarId, $objectUri, $operation, $calendarType = self::CALENDAR_TYPE_CALENDAR) { |
|
| 2661 | - $table = $calendarType === self::CALENDAR_TYPE_CALENDAR ? 'calendars': 'calendarsubscriptions'; |
|
| 2662 | - |
|
| 2663 | - $query = $this->db->getQueryBuilder(); |
|
| 2664 | - $query->select('synctoken') |
|
| 2665 | - ->from($table) |
|
| 2666 | - ->where($query->expr()->eq('id', $query->createNamedParameter($calendarId))); |
|
| 2667 | - $result = $query->executeQuery(); |
|
| 2668 | - $syncToken = (int)$result->fetchOne(); |
|
| 2669 | - $result->closeCursor(); |
|
| 2670 | - |
|
| 2671 | - $query = $this->db->getQueryBuilder(); |
|
| 2672 | - $query->insert('calendarchanges') |
|
| 2673 | - ->values([ |
|
| 2674 | - 'uri' => $query->createNamedParameter($objectUri), |
|
| 2675 | - 'synctoken' => $query->createNamedParameter($syncToken), |
|
| 2676 | - 'calendarid' => $query->createNamedParameter($calendarId), |
|
| 2677 | - 'operation' => $query->createNamedParameter($operation), |
|
| 2678 | - 'calendartype' => $query->createNamedParameter($calendarType), |
|
| 2679 | - ]) |
|
| 2680 | - ->executeStatement(); |
|
| 2681 | - |
|
| 2682 | - $stmt = $this->db->prepare("UPDATE `*PREFIX*$table` SET `synctoken` = `synctoken` + 1 WHERE `id` = ?"); |
|
| 2683 | - $stmt->execute([ |
|
| 2684 | - $calendarId |
|
| 2685 | - ]); |
|
| 2686 | - } |
|
| 2687 | - |
|
| 2688 | - /** |
|
| 2689 | - * Parses some information from calendar objects, used for optimized |
|
| 2690 | - * calendar-queries. |
|
| 2691 | - * |
|
| 2692 | - * Returns an array with the following keys: |
|
| 2693 | - * * etag - An md5 checksum of the object without the quotes. |
|
| 2694 | - * * size - Size of the object in bytes |
|
| 2695 | - * * componentType - VEVENT, VTODO or VJOURNAL |
|
| 2696 | - * * firstOccurence |
|
| 2697 | - * * lastOccurence |
|
| 2698 | - * * uid - value of the UID property |
|
| 2699 | - * |
|
| 2700 | - * @param string $calendarData |
|
| 2701 | - * @return array |
|
| 2702 | - */ |
|
| 2703 | - public function getDenormalizedData($calendarData) { |
|
| 2704 | - $vObject = Reader::read($calendarData); |
|
| 2705 | - $vEvents = []; |
|
| 2706 | - $componentType = null; |
|
| 2707 | - $component = null; |
|
| 2708 | - $firstOccurrence = null; |
|
| 2709 | - $lastOccurrence = null; |
|
| 2710 | - $uid = null; |
|
| 2711 | - $classification = self::CLASSIFICATION_PUBLIC; |
|
| 2712 | - $hasDTSTART = false; |
|
| 2713 | - foreach ($vObject->getComponents() as $component) { |
|
| 2714 | - if ($component->name !== 'VTIMEZONE') { |
|
| 2715 | - // Finding all VEVENTs, and track them |
|
| 2716 | - if ($component->name === 'VEVENT') { |
|
| 2717 | - array_push($vEvents, $component); |
|
| 2718 | - if ($component->DTSTART) { |
|
| 2719 | - $hasDTSTART = true; |
|
| 2720 | - } |
|
| 2721 | - } |
|
| 2722 | - // Track first component type and uid |
|
| 2723 | - if ($uid === null) { |
|
| 2724 | - $componentType = $component->name; |
|
| 2725 | - $uid = (string)$component->UID; |
|
| 2726 | - } |
|
| 2727 | - } |
|
| 2728 | - } |
|
| 2729 | - if (!$componentType) { |
|
| 2730 | - throw new BadRequest('Calendar objects must have a VJOURNAL, VEVENT or VTODO component'); |
|
| 2731 | - } |
|
| 2732 | - |
|
| 2733 | - if ($hasDTSTART) { |
|
| 2734 | - $component = $vEvents[0]; |
|
| 2735 | - |
|
| 2736 | - // Finding the last occurrence is a bit harder |
|
| 2737 | - if (!isset($component->RRULE) && count($vEvents) === 1) { |
|
| 2738 | - $firstOccurrence = $component->DTSTART->getDateTime()->getTimeStamp(); |
|
| 2739 | - if (isset($component->DTEND)) { |
|
| 2740 | - $lastOccurrence = $component->DTEND->getDateTime()->getTimeStamp(); |
|
| 2741 | - } elseif (isset($component->DURATION)) { |
|
| 2742 | - $endDate = clone $component->DTSTART->getDateTime(); |
|
| 2743 | - $endDate->add(DateTimeParser::parse($component->DURATION->getValue())); |
|
| 2744 | - $lastOccurrence = $endDate->getTimeStamp(); |
|
| 2745 | - } elseif (!$component->DTSTART->hasTime()) { |
|
| 2746 | - $endDate = clone $component->DTSTART->getDateTime(); |
|
| 2747 | - $endDate->modify('+1 day'); |
|
| 2748 | - $lastOccurrence = $endDate->getTimeStamp(); |
|
| 2749 | - } else { |
|
| 2750 | - $lastOccurrence = $firstOccurrence; |
|
| 2751 | - } |
|
| 2752 | - } else { |
|
| 2753 | - $it = new EventIterator($vEvents); |
|
| 2754 | - $maxDate = new DateTime(self::MAX_DATE); |
|
| 2755 | - $firstOccurrence = $it->getDtStart()->getTimestamp(); |
|
| 2756 | - if ($it->isInfinite()) { |
|
| 2757 | - $lastOccurrence = $maxDate->getTimestamp(); |
|
| 2758 | - } else { |
|
| 2759 | - $end = $it->getDtEnd(); |
|
| 2760 | - while ($it->valid() && $end < $maxDate) { |
|
| 2761 | - $end = $it->getDtEnd(); |
|
| 2762 | - $it->next(); |
|
| 2763 | - } |
|
| 2764 | - $lastOccurrence = $end->getTimestamp(); |
|
| 2765 | - } |
|
| 2766 | - } |
|
| 2767 | - } |
|
| 2768 | - |
|
| 2769 | - if ($component->CLASS) { |
|
| 2770 | - $classification = CalDavBackend::CLASSIFICATION_PRIVATE; |
|
| 2771 | - switch ($component->CLASS->getValue()) { |
|
| 2772 | - case 'PUBLIC': |
|
| 2773 | - $classification = CalDavBackend::CLASSIFICATION_PUBLIC; |
|
| 2774 | - break; |
|
| 2775 | - case 'CONFIDENTIAL': |
|
| 2776 | - $classification = CalDavBackend::CLASSIFICATION_CONFIDENTIAL; |
|
| 2777 | - break; |
|
| 2778 | - } |
|
| 2779 | - } |
|
| 2780 | - return [ |
|
| 2781 | - 'etag' => md5($calendarData), |
|
| 2782 | - 'size' => strlen($calendarData), |
|
| 2783 | - 'componentType' => $componentType, |
|
| 2784 | - 'firstOccurence' => is_null($firstOccurrence) ? null : max(0, $firstOccurrence), |
|
| 2785 | - 'lastOccurence' => $lastOccurrence, |
|
| 2786 | - 'uid' => $uid, |
|
| 2787 | - 'classification' => $classification |
|
| 2788 | - ]; |
|
| 2789 | - } |
|
| 2790 | - |
|
| 2791 | - /** |
|
| 2792 | - * @param $cardData |
|
| 2793 | - * @return bool|string |
|
| 2794 | - */ |
|
| 2795 | - private function readBlob($cardData) { |
|
| 2796 | - if (is_resource($cardData)) { |
|
| 2797 | - return stream_get_contents($cardData); |
|
| 2798 | - } |
|
| 2799 | - |
|
| 2800 | - return $cardData; |
|
| 2801 | - } |
|
| 2802 | - |
|
| 2803 | - /** |
|
| 2804 | - * @param list<array{href: string, commonName: string, readOnly: bool}> $add |
|
| 2805 | - * @param list<string> $remove |
|
| 2806 | - */ |
|
| 2807 | - public function updateShares(IShareable $shareable, array $add, array $remove): void { |
|
| 2808 | - $calendarId = $shareable->getResourceId(); |
|
| 2809 | - $calendarRow = $this->getCalendarById($calendarId); |
|
| 2810 | - if ($calendarRow === null) { |
|
| 2811 | - throw new \RuntimeException('Trying to update shares for innexistant calendar: ' . $calendarId); |
|
| 2812 | - } |
|
| 2813 | - $oldShares = $this->getShares($calendarId); |
|
| 2814 | - |
|
| 2815 | - $this->calendarSharingBackend->updateShares($shareable, $add, $remove); |
|
| 2816 | - |
|
| 2817 | - $this->dispatcher->dispatchTyped(new CalendarShareUpdatedEvent($calendarId, $calendarRow, $oldShares, $add, $remove)); |
|
| 2818 | - } |
|
| 2819 | - |
|
| 2820 | - /** |
|
| 2821 | - * @return list<array{href: string, commonName: string, status: int, readOnly: bool, '{http://owncloud.org/ns}principal': string, '{http://owncloud.org/ns}group-share': bool}> |
|
| 2822 | - */ |
|
| 2823 | - public function getShares(int $resourceId): array { |
|
| 2824 | - return $this->calendarSharingBackend->getShares($resourceId); |
|
| 2825 | - } |
|
| 2826 | - |
|
| 2827 | - /** |
|
| 2828 | - * @param boolean $value |
|
| 2829 | - * @param \OCA\DAV\CalDAV\Calendar $calendar |
|
| 2830 | - * @return string|null |
|
| 2831 | - */ |
|
| 2832 | - public function setPublishStatus($value, $calendar) { |
|
| 2833 | - $calendarId = $calendar->getResourceId(); |
|
| 2834 | - $calendarData = $this->getCalendarById($calendarId); |
|
| 2835 | - |
|
| 2836 | - $query = $this->db->getQueryBuilder(); |
|
| 2837 | - if ($value) { |
|
| 2838 | - $publicUri = $this->random->generate(16, ISecureRandom::CHAR_HUMAN_READABLE); |
|
| 2839 | - $query->insert('dav_shares') |
|
| 2840 | - ->values([ |
|
| 2841 | - 'principaluri' => $query->createNamedParameter($calendar->getPrincipalURI()), |
|
| 2842 | - 'type' => $query->createNamedParameter('calendar'), |
|
| 2843 | - 'access' => $query->createNamedParameter(self::ACCESS_PUBLIC), |
|
| 2844 | - 'resourceid' => $query->createNamedParameter($calendar->getResourceId()), |
|
| 2845 | - 'publicuri' => $query->createNamedParameter($publicUri) |
|
| 2846 | - ]); |
|
| 2847 | - $query->executeStatement(); |
|
| 2848 | - |
|
| 2849 | - $this->dispatcher->dispatchTyped(new CalendarPublishedEvent($calendarId, $calendarData, $publicUri)); |
|
| 2850 | - return $publicUri; |
|
| 2851 | - } |
|
| 2852 | - $query->delete('dav_shares') |
|
| 2853 | - ->where($query->expr()->eq('resourceid', $query->createNamedParameter($calendar->getResourceId()))) |
|
| 2854 | - ->andWhere($query->expr()->eq('access', $query->createNamedParameter(self::ACCESS_PUBLIC))); |
|
| 2855 | - $query->executeStatement(); |
|
| 2856 | - |
|
| 2857 | - $this->dispatcher->dispatchTyped(new CalendarUnpublishedEvent($calendarId, $calendarData)); |
|
| 2858 | - return null; |
|
| 2859 | - } |
|
| 2860 | - |
|
| 2861 | - /** |
|
| 2862 | - * @param \OCA\DAV\CalDAV\Calendar $calendar |
|
| 2863 | - * @return mixed |
|
| 2864 | - */ |
|
| 2865 | - public function getPublishStatus($calendar) { |
|
| 2866 | - $query = $this->db->getQueryBuilder(); |
|
| 2867 | - $result = $query->select('publicuri') |
|
| 2868 | - ->from('dav_shares') |
|
| 2869 | - ->where($query->expr()->eq('resourceid', $query->createNamedParameter($calendar->getResourceId()))) |
|
| 2870 | - ->andWhere($query->expr()->eq('access', $query->createNamedParameter(self::ACCESS_PUBLIC))) |
|
| 2871 | - ->executeQuery(); |
|
| 2872 | - |
|
| 2873 | - $row = $result->fetch(); |
|
| 2874 | - $result->closeCursor(); |
|
| 2875 | - return $row ? reset($row) : false; |
|
| 2876 | - } |
|
| 2877 | - |
|
| 2878 | - /** |
|
| 2879 | - * @param int $resourceId |
|
| 2880 | - * @param list<array{privilege: string, principal: string, protected: bool}> $acl |
|
| 2881 | - * @return list<array{privilege: string, principal: string, protected: bool}> |
|
| 2882 | - */ |
|
| 2883 | - public function applyShareAcl(int $resourceId, array $acl): array { |
|
| 2884 | - return $this->calendarSharingBackend->applyShareAcl($resourceId, $acl); |
|
| 2885 | - } |
|
| 2886 | - |
|
| 2887 | - /** |
|
| 2888 | - * update properties table |
|
| 2889 | - * |
|
| 2890 | - * @param int $calendarId |
|
| 2891 | - * @param string $objectUri |
|
| 2892 | - * @param string $calendarData |
|
| 2893 | - * @param int $calendarType |
|
| 2894 | - */ |
|
| 2895 | - public function updateProperties($calendarId, $objectUri, $calendarData, $calendarType = self::CALENDAR_TYPE_CALENDAR) { |
|
| 2896 | - $objectId = $this->getCalendarObjectId($calendarId, $objectUri, $calendarType); |
|
| 2897 | - |
|
| 2898 | - try { |
|
| 2899 | - $vCalendar = $this->readCalendarData($calendarData); |
|
| 2900 | - } catch (\Exception $ex) { |
|
| 2901 | - return; |
|
| 2902 | - } |
|
| 2903 | - |
|
| 2904 | - $this->purgeProperties($calendarId, $objectId); |
|
| 2905 | - |
|
| 2906 | - $query = $this->db->getQueryBuilder(); |
|
| 2907 | - $query->insert($this->dbObjectPropertiesTable) |
|
| 2908 | - ->values( |
|
| 2909 | - [ |
|
| 2910 | - 'calendarid' => $query->createNamedParameter($calendarId), |
|
| 2911 | - 'calendartype' => $query->createNamedParameter($calendarType), |
|
| 2912 | - 'objectid' => $query->createNamedParameter($objectId), |
|
| 2913 | - 'name' => $query->createParameter('name'), |
|
| 2914 | - 'parameter' => $query->createParameter('parameter'), |
|
| 2915 | - 'value' => $query->createParameter('value'), |
|
| 2916 | - ] |
|
| 2917 | - ); |
|
| 2918 | - |
|
| 2919 | - $indexComponents = ['VEVENT', 'VJOURNAL', 'VTODO']; |
|
| 2920 | - foreach ($vCalendar->getComponents() as $component) { |
|
| 2921 | - if (!in_array($component->name, $indexComponents)) { |
|
| 2922 | - continue; |
|
| 2923 | - } |
|
| 2924 | - |
|
| 2925 | - foreach ($component->children() as $property) { |
|
| 2926 | - if (in_array($property->name, self::INDEXED_PROPERTIES, true)) { |
|
| 2927 | - $value = $property->getValue(); |
|
| 2928 | - // is this a shitty db? |
|
| 2929 | - if (!$this->db->supports4ByteText()) { |
|
| 2930 | - $value = preg_replace('/[\x{10000}-\x{10FFFF}]/u', "\xEF\xBF\xBD", $value); |
|
| 2931 | - } |
|
| 2932 | - $value = mb_strcut($value, 0, 254); |
|
| 2933 | - |
|
| 2934 | - $query->setParameter('name', $property->name); |
|
| 2935 | - $query->setParameter('parameter', null); |
|
| 2936 | - $query->setParameter('value', $value); |
|
| 2937 | - $query->executeStatement(); |
|
| 2938 | - } |
|
| 2939 | - |
|
| 2940 | - if (array_key_exists($property->name, self::$indexParameters)) { |
|
| 2941 | - $parameters = $property->parameters(); |
|
| 2942 | - $indexedParametersForProperty = self::$indexParameters[$property->name]; |
|
| 2943 | - |
|
| 2944 | - foreach ($parameters as $key => $value) { |
|
| 2945 | - if (in_array($key, $indexedParametersForProperty)) { |
|
| 2946 | - // is this a shitty db? |
|
| 2947 | - if ($this->db->supports4ByteText()) { |
|
| 2948 | - $value = preg_replace('/[\x{10000}-\x{10FFFF}]/u', "\xEF\xBF\xBD", $value); |
|
| 2949 | - } |
|
| 2950 | - |
|
| 2951 | - $query->setParameter('name', $property->name); |
|
| 2952 | - $query->setParameter('parameter', mb_strcut($key, 0, 254)); |
|
| 2953 | - $query->setParameter('value', mb_strcut($value, 0, 254)); |
|
| 2954 | - $query->executeStatement(); |
|
| 2955 | - } |
|
| 2956 | - } |
|
| 2957 | - } |
|
| 2958 | - } |
|
| 2959 | - } |
|
| 2960 | - } |
|
| 2961 | - |
|
| 2962 | - /** |
|
| 2963 | - * deletes all birthday calendars |
|
| 2964 | - */ |
|
| 2965 | - public function deleteAllBirthdayCalendars() { |
|
| 2966 | - $query = $this->db->getQueryBuilder(); |
|
| 2967 | - $result = $query->select(['id'])->from('calendars') |
|
| 2968 | - ->where($query->expr()->eq('uri', $query->createNamedParameter(BirthdayService::BIRTHDAY_CALENDAR_URI))) |
|
| 2969 | - ->executeQuery(); |
|
| 2970 | - |
|
| 2971 | - $ids = $result->fetchAll(); |
|
| 2972 | - $result->closeCursor(); |
|
| 2973 | - foreach ($ids as $id) { |
|
| 2974 | - $this->deleteCalendar( |
|
| 2975 | - $id['id'], |
|
| 2976 | - true // No data to keep in the trashbin, if the user re-enables then we regenerate |
|
| 2977 | - ); |
|
| 2978 | - } |
|
| 2979 | - } |
|
| 2980 | - |
|
| 2981 | - /** |
|
| 2982 | - * @param $subscriptionId |
|
| 2983 | - */ |
|
| 2984 | - public function purgeAllCachedEventsForSubscription($subscriptionId) { |
|
| 2985 | - $query = $this->db->getQueryBuilder(); |
|
| 2986 | - $query->select('uri') |
|
| 2987 | - ->from('calendarobjects') |
|
| 2988 | - ->where($query->expr()->eq('calendarid', $query->createNamedParameter($subscriptionId))) |
|
| 2989 | - ->andWhere($query->expr()->eq('calendartype', $query->createNamedParameter(self::CALENDAR_TYPE_SUBSCRIPTION))); |
|
| 2990 | - $stmt = $query->executeQuery(); |
|
| 2991 | - |
|
| 2992 | - $uris = []; |
|
| 2993 | - foreach ($stmt->fetchAll() as $row) { |
|
| 2994 | - $uris[] = $row['uri']; |
|
| 2995 | - } |
|
| 2996 | - $stmt->closeCursor(); |
|
| 2997 | - |
|
| 2998 | - $query = $this->db->getQueryBuilder(); |
|
| 2999 | - $query->delete('calendarobjects') |
|
| 3000 | - ->where($query->expr()->eq('calendarid', $query->createNamedParameter($subscriptionId))) |
|
| 3001 | - ->andWhere($query->expr()->eq('calendartype', $query->createNamedParameter(self::CALENDAR_TYPE_SUBSCRIPTION))) |
|
| 3002 | - ->executeStatement(); |
|
| 3003 | - |
|
| 3004 | - $query->delete('calendarchanges') |
|
| 3005 | - ->where($query->expr()->eq('calendarid', $query->createNamedParameter($subscriptionId))) |
|
| 3006 | - ->andWhere($query->expr()->eq('calendartype', $query->createNamedParameter(self::CALENDAR_TYPE_SUBSCRIPTION))) |
|
| 3007 | - ->executeStatement(); |
|
| 3008 | - |
|
| 3009 | - $query->delete($this->dbObjectPropertiesTable) |
|
| 3010 | - ->where($query->expr()->eq('calendarid', $query->createNamedParameter($subscriptionId))) |
|
| 3011 | - ->andWhere($query->expr()->eq('calendartype', $query->createNamedParameter(self::CALENDAR_TYPE_SUBSCRIPTION))) |
|
| 3012 | - ->executeStatement(); |
|
| 3013 | - |
|
| 3014 | - foreach ($uris as $uri) { |
|
| 3015 | - $this->addChange($subscriptionId, $uri, 3, self::CALENDAR_TYPE_SUBSCRIPTION); |
|
| 3016 | - } |
|
| 3017 | - } |
|
| 3018 | - |
|
| 3019 | - /** |
|
| 3020 | - * Move a calendar from one user to another |
|
| 3021 | - * |
|
| 3022 | - * @param string $uriName |
|
| 3023 | - * @param string $uriOrigin |
|
| 3024 | - * @param string $uriDestination |
|
| 3025 | - * @param string $newUriName (optional) the new uriName |
|
| 3026 | - */ |
|
| 3027 | - public function moveCalendar($uriName, $uriOrigin, $uriDestination, $newUriName = null) { |
|
| 3028 | - $query = $this->db->getQueryBuilder(); |
|
| 3029 | - $query->update('calendars') |
|
| 3030 | - ->set('principaluri', $query->createNamedParameter($uriDestination)) |
|
| 3031 | - ->set('uri', $query->createNamedParameter($newUriName ?: $uriName)) |
|
| 3032 | - ->where($query->expr()->eq('principaluri', $query->createNamedParameter($uriOrigin))) |
|
| 3033 | - ->andWhere($query->expr()->eq('uri', $query->createNamedParameter($uriName))) |
|
| 3034 | - ->executeStatement(); |
|
| 3035 | - } |
|
| 3036 | - |
|
| 3037 | - /** |
|
| 3038 | - * read VCalendar data into a VCalendar object |
|
| 3039 | - * |
|
| 3040 | - * @param string $objectData |
|
| 3041 | - * @return VCalendar |
|
| 3042 | - */ |
|
| 3043 | - protected function readCalendarData($objectData) { |
|
| 3044 | - return Reader::read($objectData); |
|
| 3045 | - } |
|
| 3046 | - |
|
| 3047 | - /** |
|
| 3048 | - * delete all properties from a given calendar object |
|
| 3049 | - * |
|
| 3050 | - * @param int $calendarId |
|
| 3051 | - * @param int $objectId |
|
| 3052 | - */ |
|
| 3053 | - protected function purgeProperties($calendarId, $objectId) { |
|
| 3054 | - $query = $this->db->getQueryBuilder(); |
|
| 3055 | - $query->delete($this->dbObjectPropertiesTable) |
|
| 3056 | - ->where($query->expr()->eq('objectid', $query->createNamedParameter($objectId))) |
|
| 3057 | - ->andWhere($query->expr()->eq('calendarid', $query->createNamedParameter($calendarId))); |
|
| 3058 | - $query->executeStatement(); |
|
| 3059 | - } |
|
| 3060 | - |
|
| 3061 | - /** |
|
| 3062 | - * get ID from a given calendar object |
|
| 3063 | - * |
|
| 3064 | - * @param int $calendarId |
|
| 3065 | - * @param string $uri |
|
| 3066 | - * @param int $calendarType |
|
| 3067 | - * @return int |
|
| 3068 | - */ |
|
| 3069 | - protected function getCalendarObjectId($calendarId, $uri, $calendarType):int { |
|
| 3070 | - $query = $this->db->getQueryBuilder(); |
|
| 3071 | - $query->select('id') |
|
| 3072 | - ->from('calendarobjects') |
|
| 3073 | - ->where($query->expr()->eq('uri', $query->createNamedParameter($uri))) |
|
| 3074 | - ->andWhere($query->expr()->eq('calendarid', $query->createNamedParameter($calendarId))) |
|
| 3075 | - ->andWhere($query->expr()->eq('calendartype', $query->createNamedParameter($calendarType))); |
|
| 3076 | - |
|
| 3077 | - $result = $query->executeQuery(); |
|
| 3078 | - $objectIds = $result->fetch(); |
|
| 3079 | - $result->closeCursor(); |
|
| 3080 | - |
|
| 3081 | - if (!isset($objectIds['id'])) { |
|
| 3082 | - throw new \InvalidArgumentException('Calendarobject does not exists: ' . $uri); |
|
| 3083 | - } |
|
| 3084 | - |
|
| 3085 | - return (int)$objectIds['id']; |
|
| 3086 | - } |
|
| 3087 | - |
|
| 3088 | - /** |
|
| 3089 | - * @throws \InvalidArgumentException |
|
| 3090 | - */ |
|
| 3091 | - public function pruneOutdatedSyncTokens(int $keep = 10_000): int { |
|
| 3092 | - if ($keep < 0) { |
|
| 3093 | - throw new \InvalidArgumentException(); |
|
| 3094 | - } |
|
| 3095 | - $query = $this->db->getQueryBuilder(); |
|
| 3096 | - $query->delete('calendarchanges') |
|
| 3097 | - ->orderBy('id', 'DESC') |
|
| 3098 | - ->setFirstResult($keep); |
|
| 3099 | - return $query->executeStatement(); |
|
| 3100 | - } |
|
| 3101 | - |
|
| 3102 | - /** |
|
| 3103 | - * return legacy endpoint principal name to new principal name |
|
| 3104 | - * |
|
| 3105 | - * @param $principalUri |
|
| 3106 | - * @param $toV2 |
|
| 3107 | - * @return string |
|
| 3108 | - */ |
|
| 3109 | - private function convertPrincipal($principalUri, $toV2) { |
|
| 3110 | - if ($this->principalBackend->getPrincipalPrefix() === 'principals') { |
|
| 3111 | - [, $name] = Uri\split($principalUri); |
|
| 3112 | - if ($toV2 === true) { |
|
| 3113 | - return "principals/users/$name"; |
|
| 3114 | - } |
|
| 3115 | - return "principals/$name"; |
|
| 3116 | - } |
|
| 3117 | - return $principalUri; |
|
| 3118 | - } |
|
| 3119 | - |
|
| 3120 | - /** |
|
| 3121 | - * adds information about an owner to the calendar data |
|
| 3122 | - * |
|
| 3123 | - */ |
|
| 3124 | - private function addOwnerPrincipalToCalendar(array $calendarInfo): array { |
|
| 3125 | - $ownerPrincipalKey = '{' . \OCA\DAV\DAV\Sharing\Plugin::NS_OWNCLOUD . '}owner-principal'; |
|
| 3126 | - $displaynameKey = '{' . \OCA\DAV\DAV\Sharing\Plugin::NS_NEXTCLOUD . '}owner-displayname'; |
|
| 3127 | - if (isset($calendarInfo[$ownerPrincipalKey])) { |
|
| 3128 | - $uri = $calendarInfo[$ownerPrincipalKey]; |
|
| 3129 | - } else { |
|
| 3130 | - $uri = $calendarInfo['principaluri']; |
|
| 3131 | - } |
|
| 3132 | - |
|
| 3133 | - $principalInformation = $this->principalBackend->getPrincipalByPath($uri); |
|
| 3134 | - if (isset($principalInformation['{DAV:}displayname'])) { |
|
| 3135 | - $calendarInfo[$displaynameKey] = $principalInformation['{DAV:}displayname']; |
|
| 3136 | - } |
|
| 3137 | - return $calendarInfo; |
|
| 3138 | - } |
|
| 3139 | - |
|
| 3140 | - private function addResourceTypeToCalendar(array $row, array $calendar): array { |
|
| 3141 | - if (isset($row['deleted_at'])) { |
|
| 3142 | - // Columns is set and not null -> this is a deleted calendar |
|
| 3143 | - // we send a custom resourcetype to hide the deleted calendar |
|
| 3144 | - // from ordinary DAV clients, but the Calendar app will know |
|
| 3145 | - // how to handle this special resource. |
|
| 3146 | - $calendar['{DAV:}resourcetype'] = new DAV\Xml\Property\ResourceType([ |
|
| 3147 | - '{DAV:}collection', |
|
| 3148 | - sprintf('{%s}deleted-calendar', \OCA\DAV\DAV\Sharing\Plugin::NS_NEXTCLOUD), |
|
| 3149 | - ]); |
|
| 3150 | - } |
|
| 3151 | - return $calendar; |
|
| 3152 | - } |
|
| 3153 | - |
|
| 3154 | - /** |
|
| 3155 | - * Amend the calendar info with database row data |
|
| 3156 | - * |
|
| 3157 | - * @param array $row |
|
| 3158 | - * @param array $calendar |
|
| 3159 | - * |
|
| 3160 | - * @return array |
|
| 3161 | - */ |
|
| 3162 | - private function rowToCalendar($row, array $calendar): array { |
|
| 3163 | - foreach ($this->propertyMap as $xmlName => [$dbName, $type]) { |
|
| 3164 | - $value = $row[$dbName]; |
|
| 3165 | - if ($value !== null) { |
|
| 3166 | - settype($value, $type); |
|
| 3167 | - } |
|
| 3168 | - $calendar[$xmlName] = $value; |
|
| 3169 | - } |
|
| 3170 | - return $calendar; |
|
| 3171 | - } |
|
| 3172 | - |
|
| 3173 | - /** |
|
| 3174 | - * Amend the subscription info with database row data |
|
| 3175 | - * |
|
| 3176 | - * @param array $row |
|
| 3177 | - * @param array $subscription |
|
| 3178 | - * |
|
| 3179 | - * @return array |
|
| 3180 | - */ |
|
| 3181 | - private function rowToSubscription($row, array $subscription): array { |
|
| 3182 | - foreach ($this->subscriptionPropertyMap as $xmlName => [$dbName, $type]) { |
|
| 3183 | - $value = $row[$dbName]; |
|
| 3184 | - if ($value !== null) { |
|
| 3185 | - settype($value, $type); |
|
| 3186 | - } |
|
| 3187 | - $subscription[$xmlName] = $value; |
|
| 3188 | - } |
|
| 3189 | - return $subscription; |
|
| 3190 | - } |
|
| 124 | + use TTransactional; |
|
| 125 | + |
|
| 126 | + public const CALENDAR_TYPE_CALENDAR = 0; |
|
| 127 | + public const CALENDAR_TYPE_SUBSCRIPTION = 1; |
|
| 128 | + |
|
| 129 | + public const PERSONAL_CALENDAR_URI = 'personal'; |
|
| 130 | + public const PERSONAL_CALENDAR_NAME = 'Personal'; |
|
| 131 | + |
|
| 132 | + public const RESOURCE_BOOKING_CALENDAR_URI = 'calendar'; |
|
| 133 | + public const RESOURCE_BOOKING_CALENDAR_NAME = 'Calendar'; |
|
| 134 | + |
|
| 135 | + /** |
|
| 136 | + * We need to specify a max date, because we need to stop *somewhere* |
|
| 137 | + * |
|
| 138 | + * On 32 bit system the maximum for a signed integer is 2147483647, so |
|
| 139 | + * MAX_DATE cannot be higher than date('Y-m-d', 2147483647) which results |
|
| 140 | + * in 2038-01-19 to avoid problems when the date is converted |
|
| 141 | + * to a unix timestamp. |
|
| 142 | + */ |
|
| 143 | + public const MAX_DATE = '2038-01-01'; |
|
| 144 | + |
|
| 145 | + public const ACCESS_PUBLIC = 4; |
|
| 146 | + public const CLASSIFICATION_PUBLIC = 0; |
|
| 147 | + public const CLASSIFICATION_PRIVATE = 1; |
|
| 148 | + public const CLASSIFICATION_CONFIDENTIAL = 2; |
|
| 149 | + |
|
| 150 | + /** |
|
| 151 | + * List of CalDAV properties, and how they map to database field names and their type |
|
| 152 | + * Add your own properties by simply adding on to this array. |
|
| 153 | + * |
|
| 154 | + * @var array |
|
| 155 | + * @psalm-var array<string, string[]> |
|
| 156 | + */ |
|
| 157 | + public array $propertyMap = [ |
|
| 158 | + '{DAV:}displayname' => ['displayname', 'string'], |
|
| 159 | + '{urn:ietf:params:xml:ns:caldav}calendar-description' => ['description', 'string'], |
|
| 160 | + '{urn:ietf:params:xml:ns:caldav}calendar-timezone' => ['timezone', 'string'], |
|
| 161 | + '{http://apple.com/ns/ical/}calendar-order' => ['calendarorder', 'int'], |
|
| 162 | + '{http://apple.com/ns/ical/}calendar-color' => ['calendarcolor', 'string'], |
|
| 163 | + '{' . \OCA\DAV\DAV\Sharing\Plugin::NS_NEXTCLOUD . '}deleted-at' => ['deleted_at', 'int'], |
|
| 164 | + ]; |
|
| 165 | + |
|
| 166 | + /** |
|
| 167 | + * List of subscription properties, and how they map to database field names. |
|
| 168 | + * |
|
| 169 | + * @var array |
|
| 170 | + */ |
|
| 171 | + public array $subscriptionPropertyMap = [ |
|
| 172 | + '{DAV:}displayname' => ['displayname', 'string'], |
|
| 173 | + '{http://apple.com/ns/ical/}refreshrate' => ['refreshrate', 'string'], |
|
| 174 | + '{http://apple.com/ns/ical/}calendar-order' => ['calendarorder', 'int'], |
|
| 175 | + '{http://apple.com/ns/ical/}calendar-color' => ['calendarcolor', 'string'], |
|
| 176 | + '{http://calendarserver.org/ns/}subscribed-strip-todos' => ['striptodos', 'bool'], |
|
| 177 | + '{http://calendarserver.org/ns/}subscribed-strip-alarms' => ['stripalarms', 'string'], |
|
| 178 | + '{http://calendarserver.org/ns/}subscribed-strip-attachments' => ['stripattachments', 'string'], |
|
| 179 | + ]; |
|
| 180 | + |
|
| 181 | + /** |
|
| 182 | + * properties to index |
|
| 183 | + * |
|
| 184 | + * This list has to be kept in sync with ICalendarQuery::SEARCH_PROPERTY_* |
|
| 185 | + * |
|
| 186 | + * @see \OCP\Calendar\ICalendarQuery |
|
| 187 | + */ |
|
| 188 | + private const INDEXED_PROPERTIES = [ |
|
| 189 | + 'CATEGORIES', |
|
| 190 | + 'COMMENT', |
|
| 191 | + 'DESCRIPTION', |
|
| 192 | + 'LOCATION', |
|
| 193 | + 'RESOURCES', |
|
| 194 | + 'STATUS', |
|
| 195 | + 'SUMMARY', |
|
| 196 | + 'ATTENDEE', |
|
| 197 | + 'CONTACT', |
|
| 198 | + 'ORGANIZER' |
|
| 199 | + ]; |
|
| 200 | + |
|
| 201 | + /** @var array parameters to index */ |
|
| 202 | + public static array $indexParameters = [ |
|
| 203 | + 'ATTENDEE' => ['CN'], |
|
| 204 | + 'ORGANIZER' => ['CN'], |
|
| 205 | + ]; |
|
| 206 | + |
|
| 207 | + /** |
|
| 208 | + * @var string[] Map of uid => display name |
|
| 209 | + */ |
|
| 210 | + protected array $userDisplayNames; |
|
| 211 | + |
|
| 212 | + private IDBConnection $db; |
|
| 213 | + private Backend $calendarSharingBackend; |
|
| 214 | + private Principal $principalBackend; |
|
| 215 | + private IUserManager $userManager; |
|
| 216 | + private ISecureRandom $random; |
|
| 217 | + private LoggerInterface $logger; |
|
| 218 | + private IEventDispatcher $dispatcher; |
|
| 219 | + private IConfig $config; |
|
| 220 | + private bool $legacyEndpoint; |
|
| 221 | + private string $dbObjectPropertiesTable = 'calendarobjects_props'; |
|
| 222 | + |
|
| 223 | + public function __construct(IDBConnection $db, |
|
| 224 | + Principal $principalBackend, |
|
| 225 | + IUserManager $userManager, |
|
| 226 | + IGroupManager $groupManager, |
|
| 227 | + ISecureRandom $random, |
|
| 228 | + LoggerInterface $logger, |
|
| 229 | + IEventDispatcher $dispatcher, |
|
| 230 | + IConfig $config, |
|
| 231 | + bool $legacyEndpoint = false) { |
|
| 232 | + $this->db = $db; |
|
| 233 | + $this->principalBackend = $principalBackend; |
|
| 234 | + $this->userManager = $userManager; |
|
| 235 | + $this->calendarSharingBackend = new Backend($this->db, $this->userManager, $groupManager, $principalBackend, 'calendar'); |
|
| 236 | + $this->random = $random; |
|
| 237 | + $this->logger = $logger; |
|
| 238 | + $this->dispatcher = $dispatcher; |
|
| 239 | + $this->config = $config; |
|
| 240 | + $this->legacyEndpoint = $legacyEndpoint; |
|
| 241 | + } |
|
| 242 | + |
|
| 243 | + /** |
|
| 244 | + * Return the number of calendars for a principal |
|
| 245 | + * |
|
| 246 | + * By default this excludes the automatically generated birthday calendar |
|
| 247 | + * |
|
| 248 | + * @param $principalUri |
|
| 249 | + * @param bool $excludeBirthday |
|
| 250 | + * @return int |
|
| 251 | + */ |
|
| 252 | + public function getCalendarsForUserCount($principalUri, $excludeBirthday = true) { |
|
| 253 | + $principalUri = $this->convertPrincipal($principalUri, true); |
|
| 254 | + $query = $this->db->getQueryBuilder(); |
|
| 255 | + $query->select($query->func()->count('*')) |
|
| 256 | + ->from('calendars'); |
|
| 257 | + |
|
| 258 | + if ($principalUri === '') { |
|
| 259 | + $query->where($query->expr()->emptyString('principaluri')); |
|
| 260 | + } else { |
|
| 261 | + $query->where($query->expr()->eq('principaluri', $query->createNamedParameter($principalUri))); |
|
| 262 | + } |
|
| 263 | + |
|
| 264 | + if ($excludeBirthday) { |
|
| 265 | + $query->andWhere($query->expr()->neq('uri', $query->createNamedParameter(BirthdayService::BIRTHDAY_CALENDAR_URI))); |
|
| 266 | + } |
|
| 267 | + |
|
| 268 | + $result = $query->executeQuery(); |
|
| 269 | + $column = (int)$result->fetchOne(); |
|
| 270 | + $result->closeCursor(); |
|
| 271 | + return $column; |
|
| 272 | + } |
|
| 273 | + |
|
| 274 | + /** |
|
| 275 | + * @return array{id: int, deleted_at: int}[] |
|
| 276 | + */ |
|
| 277 | + public function getDeletedCalendars(int $deletedBefore): array { |
|
| 278 | + $qb = $this->db->getQueryBuilder(); |
|
| 279 | + $qb->select(['id', 'deleted_at']) |
|
| 280 | + ->from('calendars') |
|
| 281 | + ->where($qb->expr()->isNotNull('deleted_at')) |
|
| 282 | + ->andWhere($qb->expr()->lt('deleted_at', $qb->createNamedParameter($deletedBefore))); |
|
| 283 | + $result = $qb->executeQuery(); |
|
| 284 | + $raw = $result->fetchAll(); |
|
| 285 | + $result->closeCursor(); |
|
| 286 | + return array_map(function ($row) { |
|
| 287 | + return [ |
|
| 288 | + 'id' => (int) $row['id'], |
|
| 289 | + 'deleted_at' => (int) $row['deleted_at'], |
|
| 290 | + ]; |
|
| 291 | + }, $raw); |
|
| 292 | + } |
|
| 293 | + |
|
| 294 | + /** |
|
| 295 | + * Returns a list of calendars for a principal. |
|
| 296 | + * |
|
| 297 | + * Every project is an array with the following keys: |
|
| 298 | + * * id, a unique id that will be used by other functions to modify the |
|
| 299 | + * calendar. This can be the same as the uri or a database key. |
|
| 300 | + * * uri, which the basename of the uri with which the calendar is |
|
| 301 | + * accessed. |
|
| 302 | + * * principaluri. The owner of the calendar. Almost always the same as |
|
| 303 | + * principalUri passed to this method. |
|
| 304 | + * |
|
| 305 | + * Furthermore it can contain webdav properties in clark notation. A very |
|
| 306 | + * common one is '{DAV:}displayname'. |
|
| 307 | + * |
|
| 308 | + * Many clients also require: |
|
| 309 | + * {urn:ietf:params:xml:ns:caldav}supported-calendar-component-set |
|
| 310 | + * For this property, you can just return an instance of |
|
| 311 | + * Sabre\CalDAV\Property\SupportedCalendarComponentSet. |
|
| 312 | + * |
|
| 313 | + * If you return {http://sabredav.org/ns}read-only and set the value to 1, |
|
| 314 | + * ACL will automatically be put in read-only mode. |
|
| 315 | + * |
|
| 316 | + * @param string $principalUri |
|
| 317 | + * @return array |
|
| 318 | + */ |
|
| 319 | + public function getCalendarsForUser($principalUri) { |
|
| 320 | + $principalUriOriginal = $principalUri; |
|
| 321 | + $principalUri = $this->convertPrincipal($principalUri, true); |
|
| 322 | + $fields = array_column($this->propertyMap, 0); |
|
| 323 | + $fields[] = 'id'; |
|
| 324 | + $fields[] = 'uri'; |
|
| 325 | + $fields[] = 'synctoken'; |
|
| 326 | + $fields[] = 'components'; |
|
| 327 | + $fields[] = 'principaluri'; |
|
| 328 | + $fields[] = 'transparent'; |
|
| 329 | + |
|
| 330 | + // Making fields a comma-delimited list |
|
| 331 | + $query = $this->db->getQueryBuilder(); |
|
| 332 | + $query->select($fields) |
|
| 333 | + ->from('calendars') |
|
| 334 | + ->orderBy('calendarorder', 'ASC'); |
|
| 335 | + |
|
| 336 | + if ($principalUri === '') { |
|
| 337 | + $query->where($query->expr()->emptyString('principaluri')); |
|
| 338 | + } else { |
|
| 339 | + $query->where($query->expr()->eq('principaluri', $query->createNamedParameter($principalUri))); |
|
| 340 | + } |
|
| 341 | + |
|
| 342 | + $result = $query->executeQuery(); |
|
| 343 | + |
|
| 344 | + $calendars = []; |
|
| 345 | + while ($row = $result->fetch()) { |
|
| 346 | + $row['principaluri'] = (string) $row['principaluri']; |
|
| 347 | + $components = []; |
|
| 348 | + if ($row['components']) { |
|
| 349 | + $components = explode(',',$row['components']); |
|
| 350 | + } |
|
| 351 | + |
|
| 352 | + $calendar = [ |
|
| 353 | + 'id' => $row['id'], |
|
| 354 | + 'uri' => $row['uri'], |
|
| 355 | + 'principaluri' => $this->convertPrincipal($row['principaluri'], !$this->legacyEndpoint), |
|
| 356 | + '{' . Plugin::NS_CALENDARSERVER . '}getctag' => 'http://sabre.io/ns/sync/' . ($row['synctoken']?$row['synctoken']:'0'), |
|
| 357 | + '{http://sabredav.org/ns}sync-token' => $row['synctoken']?$row['synctoken']:'0', |
|
| 358 | + '{' . Plugin::NS_CALDAV . '}supported-calendar-component-set' => new SupportedCalendarComponentSet($components), |
|
| 359 | + '{' . Plugin::NS_CALDAV . '}schedule-calendar-transp' => new ScheduleCalendarTransp($row['transparent']?'transparent':'opaque'), |
|
| 360 | + '{' . \OCA\DAV\DAV\Sharing\Plugin::NS_OWNCLOUD . '}owner-principal' => $this->convertPrincipal($principalUri, !$this->legacyEndpoint), |
|
| 361 | + ]; |
|
| 362 | + |
|
| 363 | + $calendar = $this->rowToCalendar($row, $calendar); |
|
| 364 | + $calendar = $this->addOwnerPrincipalToCalendar($calendar); |
|
| 365 | + $calendar = $this->addResourceTypeToCalendar($row, $calendar); |
|
| 366 | + |
|
| 367 | + if (!isset($calendars[$calendar['id']])) { |
|
| 368 | + $calendars[$calendar['id']] = $calendar; |
|
| 369 | + } |
|
| 370 | + } |
|
| 371 | + $result->closeCursor(); |
|
| 372 | + |
|
| 373 | + // query for shared calendars |
|
| 374 | + $principals = $this->principalBackend->getGroupMembership($principalUriOriginal, true); |
|
| 375 | + $principals = array_merge($principals, $this->principalBackend->getCircleMembership($principalUriOriginal)); |
|
| 376 | + |
|
| 377 | + $principals[] = $principalUri; |
|
| 378 | + |
|
| 379 | + $fields = array_column($this->propertyMap, 0); |
|
| 380 | + $fields[] = 'a.id'; |
|
| 381 | + $fields[] = 'a.uri'; |
|
| 382 | + $fields[] = 'a.synctoken'; |
|
| 383 | + $fields[] = 'a.components'; |
|
| 384 | + $fields[] = 'a.principaluri'; |
|
| 385 | + $fields[] = 'a.transparent'; |
|
| 386 | + $fields[] = 's.access'; |
|
| 387 | + $query = $this->db->getQueryBuilder(); |
|
| 388 | + $query->select($fields) |
|
| 389 | + ->from('dav_shares', 's') |
|
| 390 | + ->join('s', 'calendars', 'a', $query->expr()->eq('s.resourceid', 'a.id')) |
|
| 391 | + ->where($query->expr()->in('s.principaluri', $query->createParameter('principaluri'))) |
|
| 392 | + ->andWhere($query->expr()->eq('s.type', $query->createParameter('type'))) |
|
| 393 | + ->setParameter('type', 'calendar') |
|
| 394 | + ->setParameter('principaluri', $principals, \Doctrine\DBAL\Connection::PARAM_STR_ARRAY); |
|
| 395 | + |
|
| 396 | + $result = $query->executeQuery(); |
|
| 397 | + |
|
| 398 | + $readOnlyPropertyName = '{' . \OCA\DAV\DAV\Sharing\Plugin::NS_OWNCLOUD . '}read-only'; |
|
| 399 | + while ($row = $result->fetch()) { |
|
| 400 | + $row['principaluri'] = (string) $row['principaluri']; |
|
| 401 | + if ($row['principaluri'] === $principalUri) { |
|
| 402 | + continue; |
|
| 403 | + } |
|
| 404 | + |
|
| 405 | + $readOnly = (int) $row['access'] === Backend::ACCESS_READ; |
|
| 406 | + if (isset($calendars[$row['id']])) { |
|
| 407 | + if ($readOnly) { |
|
| 408 | + // New share can not have more permissions then the old one. |
|
| 409 | + continue; |
|
| 410 | + } |
|
| 411 | + if (isset($calendars[$row['id']][$readOnlyPropertyName]) && |
|
| 412 | + $calendars[$row['id']][$readOnlyPropertyName] === 0) { |
|
| 413 | + // Old share is already read-write, no more permissions can be gained |
|
| 414 | + continue; |
|
| 415 | + } |
|
| 416 | + } |
|
| 417 | + |
|
| 418 | + [, $name] = Uri\split($row['principaluri']); |
|
| 419 | + $uri = $row['uri'] . '_shared_by_' . $name; |
|
| 420 | + $row['displayname'] = $row['displayname'] . ' (' . ($this->userManager->getDisplayName($name) ?? ($name ?? '')) . ')'; |
|
| 421 | + $components = []; |
|
| 422 | + if ($row['components']) { |
|
| 423 | + $components = explode(',',$row['components']); |
|
| 424 | + } |
|
| 425 | + $calendar = [ |
|
| 426 | + 'id' => $row['id'], |
|
| 427 | + 'uri' => $uri, |
|
| 428 | + 'principaluri' => $this->convertPrincipal($principalUri, !$this->legacyEndpoint), |
|
| 429 | + '{' . Plugin::NS_CALENDARSERVER . '}getctag' => 'http://sabre.io/ns/sync/' . ($row['synctoken']?$row['synctoken']:'0'), |
|
| 430 | + '{http://sabredav.org/ns}sync-token' => $row['synctoken']?$row['synctoken']:'0', |
|
| 431 | + '{' . Plugin::NS_CALDAV . '}supported-calendar-component-set' => new SupportedCalendarComponentSet($components), |
|
| 432 | + '{' . Plugin::NS_CALDAV . '}schedule-calendar-transp' => new ScheduleCalendarTransp('transparent'), |
|
| 433 | + '{' . \OCA\DAV\DAV\Sharing\Plugin::NS_OWNCLOUD . '}owner-principal' => $this->convertPrincipal($row['principaluri'], !$this->legacyEndpoint), |
|
| 434 | + $readOnlyPropertyName => $readOnly, |
|
| 435 | + ]; |
|
| 436 | + |
|
| 437 | + $calendar = $this->rowToCalendar($row, $calendar); |
|
| 438 | + $calendar = $this->addOwnerPrincipalToCalendar($calendar); |
|
| 439 | + $calendar = $this->addResourceTypeToCalendar($row, $calendar); |
|
| 440 | + |
|
| 441 | + $calendars[$calendar['id']] = $calendar; |
|
| 442 | + } |
|
| 443 | + $result->closeCursor(); |
|
| 444 | + |
|
| 445 | + return array_values($calendars); |
|
| 446 | + } |
|
| 447 | + |
|
| 448 | + /** |
|
| 449 | + * @param $principalUri |
|
| 450 | + * @return array |
|
| 451 | + */ |
|
| 452 | + public function getUsersOwnCalendars($principalUri) { |
|
| 453 | + $principalUri = $this->convertPrincipal($principalUri, true); |
|
| 454 | + $fields = array_column($this->propertyMap, 0); |
|
| 455 | + $fields[] = 'id'; |
|
| 456 | + $fields[] = 'uri'; |
|
| 457 | + $fields[] = 'synctoken'; |
|
| 458 | + $fields[] = 'components'; |
|
| 459 | + $fields[] = 'principaluri'; |
|
| 460 | + $fields[] = 'transparent'; |
|
| 461 | + // Making fields a comma-delimited list |
|
| 462 | + $query = $this->db->getQueryBuilder(); |
|
| 463 | + $query->select($fields)->from('calendars') |
|
| 464 | + ->where($query->expr()->eq('principaluri', $query->createNamedParameter($principalUri))) |
|
| 465 | + ->orderBy('calendarorder', 'ASC'); |
|
| 466 | + $stmt = $query->executeQuery(); |
|
| 467 | + $calendars = []; |
|
| 468 | + while ($row = $stmt->fetch()) { |
|
| 469 | + $row['principaluri'] = (string) $row['principaluri']; |
|
| 470 | + $components = []; |
|
| 471 | + if ($row['components']) { |
|
| 472 | + $components = explode(',',$row['components']); |
|
| 473 | + } |
|
| 474 | + $calendar = [ |
|
| 475 | + 'id' => $row['id'], |
|
| 476 | + 'uri' => $row['uri'], |
|
| 477 | + 'principaluri' => $this->convertPrincipal($row['principaluri'], !$this->legacyEndpoint), |
|
| 478 | + '{' . Plugin::NS_CALENDARSERVER . '}getctag' => 'http://sabre.io/ns/sync/' . ($row['synctoken']?$row['synctoken']:'0'), |
|
| 479 | + '{http://sabredav.org/ns}sync-token' => $row['synctoken']?$row['synctoken']:'0', |
|
| 480 | + '{' . Plugin::NS_CALDAV . '}supported-calendar-component-set' => new SupportedCalendarComponentSet($components), |
|
| 481 | + '{' . Plugin::NS_CALDAV . '}schedule-calendar-transp' => new ScheduleCalendarTransp($row['transparent']?'transparent':'opaque'), |
|
| 482 | + ]; |
|
| 483 | + |
|
| 484 | + $calendar = $this->rowToCalendar($row, $calendar); |
|
| 485 | + $calendar = $this->addOwnerPrincipalToCalendar($calendar); |
|
| 486 | + $calendar = $this->addResourceTypeToCalendar($row, $calendar); |
|
| 487 | + |
|
| 488 | + if (!isset($calendars[$calendar['id']])) { |
|
| 489 | + $calendars[$calendar['id']] = $calendar; |
|
| 490 | + } |
|
| 491 | + } |
|
| 492 | + $stmt->closeCursor(); |
|
| 493 | + return array_values($calendars); |
|
| 494 | + } |
|
| 495 | + |
|
| 496 | + /** |
|
| 497 | + * @return array |
|
| 498 | + */ |
|
| 499 | + public function getPublicCalendars() { |
|
| 500 | + $fields = array_column($this->propertyMap, 0); |
|
| 501 | + $fields[] = 'a.id'; |
|
| 502 | + $fields[] = 'a.uri'; |
|
| 503 | + $fields[] = 'a.synctoken'; |
|
| 504 | + $fields[] = 'a.components'; |
|
| 505 | + $fields[] = 'a.principaluri'; |
|
| 506 | + $fields[] = 'a.transparent'; |
|
| 507 | + $fields[] = 's.access'; |
|
| 508 | + $fields[] = 's.publicuri'; |
|
| 509 | + $calendars = []; |
|
| 510 | + $query = $this->db->getQueryBuilder(); |
|
| 511 | + $result = $query->select($fields) |
|
| 512 | + ->from('dav_shares', 's') |
|
| 513 | + ->join('s', 'calendars', 'a', $query->expr()->eq('s.resourceid', 'a.id')) |
|
| 514 | + ->where($query->expr()->in('s.access', $query->createNamedParameter(self::ACCESS_PUBLIC))) |
|
| 515 | + ->andWhere($query->expr()->eq('s.type', $query->createNamedParameter('calendar'))) |
|
| 516 | + ->executeQuery(); |
|
| 517 | + |
|
| 518 | + while ($row = $result->fetch()) { |
|
| 519 | + $row['principaluri'] = (string) $row['principaluri']; |
|
| 520 | + [, $name] = Uri\split($row['principaluri']); |
|
| 521 | + $row['displayname'] = $row['displayname'] . "($name)"; |
|
| 522 | + $components = []; |
|
| 523 | + if ($row['components']) { |
|
| 524 | + $components = explode(',',$row['components']); |
|
| 525 | + } |
|
| 526 | + $calendar = [ |
|
| 527 | + 'id' => $row['id'], |
|
| 528 | + 'uri' => $row['publicuri'], |
|
| 529 | + 'principaluri' => $this->convertPrincipal($row['principaluri'], !$this->legacyEndpoint), |
|
| 530 | + '{' . Plugin::NS_CALENDARSERVER . '}getctag' => 'http://sabre.io/ns/sync/' . ($row['synctoken']?$row['synctoken']:'0'), |
|
| 531 | + '{http://sabredav.org/ns}sync-token' => $row['synctoken']?$row['synctoken']:'0', |
|
| 532 | + '{' . Plugin::NS_CALDAV . '}supported-calendar-component-set' => new SupportedCalendarComponentSet($components), |
|
| 533 | + '{' . Plugin::NS_CALDAV . '}schedule-calendar-transp' => new ScheduleCalendarTransp($row['transparent']?'transparent':'opaque'), |
|
| 534 | + '{' . \OCA\DAV\DAV\Sharing\Plugin::NS_OWNCLOUD . '}owner-principal' => $this->convertPrincipal($row['principaluri'], $this->legacyEndpoint), |
|
| 535 | + '{' . \OCA\DAV\DAV\Sharing\Plugin::NS_OWNCLOUD . '}read-only' => (int)$row['access'] === Backend::ACCESS_READ, |
|
| 536 | + '{' . \OCA\DAV\DAV\Sharing\Plugin::NS_OWNCLOUD . '}public' => (int)$row['access'] === self::ACCESS_PUBLIC, |
|
| 537 | + ]; |
|
| 538 | + |
|
| 539 | + $calendar = $this->rowToCalendar($row, $calendar); |
|
| 540 | + $calendar = $this->addOwnerPrincipalToCalendar($calendar); |
|
| 541 | + $calendar = $this->addResourceTypeToCalendar($row, $calendar); |
|
| 542 | + |
|
| 543 | + if (!isset($calendars[$calendar['id']])) { |
|
| 544 | + $calendars[$calendar['id']] = $calendar; |
|
| 545 | + } |
|
| 546 | + } |
|
| 547 | + $result->closeCursor(); |
|
| 548 | + |
|
| 549 | + return array_values($calendars); |
|
| 550 | + } |
|
| 551 | + |
|
| 552 | + /** |
|
| 553 | + * @param string $uri |
|
| 554 | + * @return array |
|
| 555 | + * @throws NotFound |
|
| 556 | + */ |
|
| 557 | + public function getPublicCalendar($uri) { |
|
| 558 | + $fields = array_column($this->propertyMap, 0); |
|
| 559 | + $fields[] = 'a.id'; |
|
| 560 | + $fields[] = 'a.uri'; |
|
| 561 | + $fields[] = 'a.synctoken'; |
|
| 562 | + $fields[] = 'a.components'; |
|
| 563 | + $fields[] = 'a.principaluri'; |
|
| 564 | + $fields[] = 'a.transparent'; |
|
| 565 | + $fields[] = 's.access'; |
|
| 566 | + $fields[] = 's.publicuri'; |
|
| 567 | + $query = $this->db->getQueryBuilder(); |
|
| 568 | + $result = $query->select($fields) |
|
| 569 | + ->from('dav_shares', 's') |
|
| 570 | + ->join('s', 'calendars', 'a', $query->expr()->eq('s.resourceid', 'a.id')) |
|
| 571 | + ->where($query->expr()->in('s.access', $query->createNamedParameter(self::ACCESS_PUBLIC))) |
|
| 572 | + ->andWhere($query->expr()->eq('s.type', $query->createNamedParameter('calendar'))) |
|
| 573 | + ->andWhere($query->expr()->eq('s.publicuri', $query->createNamedParameter($uri))) |
|
| 574 | + ->executeQuery(); |
|
| 575 | + |
|
| 576 | + $row = $result->fetch(); |
|
| 577 | + |
|
| 578 | + $result->closeCursor(); |
|
| 579 | + |
|
| 580 | + if ($row === false) { |
|
| 581 | + throw new NotFound('Node with name \'' . $uri . '\' could not be found'); |
|
| 582 | + } |
|
| 583 | + |
|
| 584 | + $row['principaluri'] = (string) $row['principaluri']; |
|
| 585 | + [, $name] = Uri\split($row['principaluri']); |
|
| 586 | + $row['displayname'] = $row['displayname'] . ' ' . "($name)"; |
|
| 587 | + $components = []; |
|
| 588 | + if ($row['components']) { |
|
| 589 | + $components = explode(',',$row['components']); |
|
| 590 | + } |
|
| 591 | + $calendar = [ |
|
| 592 | + 'id' => $row['id'], |
|
| 593 | + 'uri' => $row['publicuri'], |
|
| 594 | + 'principaluri' => $this->convertPrincipal($row['principaluri'], !$this->legacyEndpoint), |
|
| 595 | + '{' . Plugin::NS_CALENDARSERVER . '}getctag' => 'http://sabre.io/ns/sync/' . ($row['synctoken']?$row['synctoken']:'0'), |
|
| 596 | + '{http://sabredav.org/ns}sync-token' => $row['synctoken']?$row['synctoken']:'0', |
|
| 597 | + '{' . Plugin::NS_CALDAV . '}supported-calendar-component-set' => new SupportedCalendarComponentSet($components), |
|
| 598 | + '{' . Plugin::NS_CALDAV . '}schedule-calendar-transp' => new ScheduleCalendarTransp($row['transparent']?'transparent':'opaque'), |
|
| 599 | + '{' . \OCA\DAV\DAV\Sharing\Plugin::NS_OWNCLOUD . '}owner-principal' => $this->convertPrincipal($row['principaluri'], !$this->legacyEndpoint), |
|
| 600 | + '{' . \OCA\DAV\DAV\Sharing\Plugin::NS_OWNCLOUD . '}read-only' => (int)$row['access'] === Backend::ACCESS_READ, |
|
| 601 | + '{' . \OCA\DAV\DAV\Sharing\Plugin::NS_OWNCLOUD . '}public' => (int)$row['access'] === self::ACCESS_PUBLIC, |
|
| 602 | + ]; |
|
| 603 | + |
|
| 604 | + $calendar = $this->rowToCalendar($row, $calendar); |
|
| 605 | + $calendar = $this->addOwnerPrincipalToCalendar($calendar); |
|
| 606 | + $calendar = $this->addResourceTypeToCalendar($row, $calendar); |
|
| 607 | + |
|
| 608 | + return $calendar; |
|
| 609 | + } |
|
| 610 | + |
|
| 611 | + /** |
|
| 612 | + * @param string $principal |
|
| 613 | + * @param string $uri |
|
| 614 | + * @return array|null |
|
| 615 | + */ |
|
| 616 | + public function getCalendarByUri($principal, $uri) { |
|
| 617 | + $fields = array_column($this->propertyMap, 0); |
|
| 618 | + $fields[] = 'id'; |
|
| 619 | + $fields[] = 'uri'; |
|
| 620 | + $fields[] = 'synctoken'; |
|
| 621 | + $fields[] = 'components'; |
|
| 622 | + $fields[] = 'principaluri'; |
|
| 623 | + $fields[] = 'transparent'; |
|
| 624 | + |
|
| 625 | + // Making fields a comma-delimited list |
|
| 626 | + $query = $this->db->getQueryBuilder(); |
|
| 627 | + $query->select($fields)->from('calendars') |
|
| 628 | + ->where($query->expr()->eq('uri', $query->createNamedParameter($uri))) |
|
| 629 | + ->andWhere($query->expr()->eq('principaluri', $query->createNamedParameter($principal))) |
|
| 630 | + ->setMaxResults(1); |
|
| 631 | + $stmt = $query->executeQuery(); |
|
| 632 | + |
|
| 633 | + $row = $stmt->fetch(); |
|
| 634 | + $stmt->closeCursor(); |
|
| 635 | + if ($row === false) { |
|
| 636 | + return null; |
|
| 637 | + } |
|
| 638 | + |
|
| 639 | + $row['principaluri'] = (string) $row['principaluri']; |
|
| 640 | + $components = []; |
|
| 641 | + if ($row['components']) { |
|
| 642 | + $components = explode(',',$row['components']); |
|
| 643 | + } |
|
| 644 | + |
|
| 645 | + $calendar = [ |
|
| 646 | + 'id' => $row['id'], |
|
| 647 | + 'uri' => $row['uri'], |
|
| 648 | + 'principaluri' => $this->convertPrincipal($row['principaluri'], !$this->legacyEndpoint), |
|
| 649 | + '{' . Plugin::NS_CALENDARSERVER . '}getctag' => 'http://sabre.io/ns/sync/' . ($row['synctoken']?$row['synctoken']:'0'), |
|
| 650 | + '{http://sabredav.org/ns}sync-token' => $row['synctoken']?$row['synctoken']:'0', |
|
| 651 | + '{' . Plugin::NS_CALDAV . '}supported-calendar-component-set' => new SupportedCalendarComponentSet($components), |
|
| 652 | + '{' . Plugin::NS_CALDAV . '}schedule-calendar-transp' => new ScheduleCalendarTransp($row['transparent']?'transparent':'opaque'), |
|
| 653 | + ]; |
|
| 654 | + |
|
| 655 | + $calendar = $this->rowToCalendar($row, $calendar); |
|
| 656 | + $calendar = $this->addOwnerPrincipalToCalendar($calendar); |
|
| 657 | + $calendar = $this->addResourceTypeToCalendar($row, $calendar); |
|
| 658 | + |
|
| 659 | + return $calendar; |
|
| 660 | + } |
|
| 661 | + |
|
| 662 | + /** |
|
| 663 | + * @return array{id: int, uri: string, '{http://calendarserver.org/ns/}getctag': string, '{http://sabredav.org/ns}sync-token': int, '{urn:ietf:params:xml:ns:caldav}supported-calendar-component-set': SupportedCalendarComponentSet, '{urn:ietf:params:xml:ns:caldav}schedule-calendar-transp': ScheduleCalendarTransp }|null |
|
| 664 | + */ |
|
| 665 | + public function getCalendarById(int $calendarId): ?array { |
|
| 666 | + $fields = array_column($this->propertyMap, 0); |
|
| 667 | + $fields[] = 'id'; |
|
| 668 | + $fields[] = 'uri'; |
|
| 669 | + $fields[] = 'synctoken'; |
|
| 670 | + $fields[] = 'components'; |
|
| 671 | + $fields[] = 'principaluri'; |
|
| 672 | + $fields[] = 'transparent'; |
|
| 673 | + |
|
| 674 | + // Making fields a comma-delimited list |
|
| 675 | + $query = $this->db->getQueryBuilder(); |
|
| 676 | + $query->select($fields)->from('calendars') |
|
| 677 | + ->where($query->expr()->eq('id', $query->createNamedParameter($calendarId))) |
|
| 678 | + ->setMaxResults(1); |
|
| 679 | + $stmt = $query->executeQuery(); |
|
| 680 | + |
|
| 681 | + $row = $stmt->fetch(); |
|
| 682 | + $stmt->closeCursor(); |
|
| 683 | + if ($row === false) { |
|
| 684 | + return null; |
|
| 685 | + } |
|
| 686 | + |
|
| 687 | + $row['principaluri'] = (string) $row['principaluri']; |
|
| 688 | + $components = []; |
|
| 689 | + if ($row['components']) { |
|
| 690 | + $components = explode(',',$row['components']); |
|
| 691 | + } |
|
| 692 | + |
|
| 693 | + $calendar = [ |
|
| 694 | + 'id' => $row['id'], |
|
| 695 | + 'uri' => $row['uri'], |
|
| 696 | + 'principaluri' => $this->convertPrincipal($row['principaluri'], !$this->legacyEndpoint), |
|
| 697 | + '{' . Plugin::NS_CALENDARSERVER . '}getctag' => 'http://sabre.io/ns/sync/' . ($row['synctoken']?$row['synctoken']:'0'), |
|
| 698 | + '{http://sabredav.org/ns}sync-token' => $row['synctoken'] ?? 0, |
|
| 699 | + '{' . Plugin::NS_CALDAV . '}supported-calendar-component-set' => new SupportedCalendarComponentSet($components), |
|
| 700 | + '{' . Plugin::NS_CALDAV . '}schedule-calendar-transp' => new ScheduleCalendarTransp($row['transparent']?'transparent':'opaque'), |
|
| 701 | + ]; |
|
| 702 | + |
|
| 703 | + $calendar = $this->rowToCalendar($row, $calendar); |
|
| 704 | + $calendar = $this->addOwnerPrincipalToCalendar($calendar); |
|
| 705 | + $calendar = $this->addResourceTypeToCalendar($row, $calendar); |
|
| 706 | + |
|
| 707 | + return $calendar; |
|
| 708 | + } |
|
| 709 | + |
|
| 710 | + /** |
|
| 711 | + * @param $subscriptionId |
|
| 712 | + */ |
|
| 713 | + public function getSubscriptionById($subscriptionId) { |
|
| 714 | + $fields = array_column($this->subscriptionPropertyMap, 0); |
|
| 715 | + $fields[] = 'id'; |
|
| 716 | + $fields[] = 'uri'; |
|
| 717 | + $fields[] = 'source'; |
|
| 718 | + $fields[] = 'synctoken'; |
|
| 719 | + $fields[] = 'principaluri'; |
|
| 720 | + $fields[] = 'lastmodified'; |
|
| 721 | + |
|
| 722 | + $query = $this->db->getQueryBuilder(); |
|
| 723 | + $query->select($fields) |
|
| 724 | + ->from('calendarsubscriptions') |
|
| 725 | + ->where($query->expr()->eq('id', $query->createNamedParameter($subscriptionId))) |
|
| 726 | + ->orderBy('calendarorder', 'asc'); |
|
| 727 | + $stmt = $query->executeQuery(); |
|
| 728 | + |
|
| 729 | + $row = $stmt->fetch(); |
|
| 730 | + $stmt->closeCursor(); |
|
| 731 | + if ($row === false) { |
|
| 732 | + return null; |
|
| 733 | + } |
|
| 734 | + |
|
| 735 | + $row['principaluri'] = (string) $row['principaluri']; |
|
| 736 | + $subscription = [ |
|
| 737 | + 'id' => $row['id'], |
|
| 738 | + 'uri' => $row['uri'], |
|
| 739 | + 'principaluri' => $row['principaluri'], |
|
| 740 | + 'source' => $row['source'], |
|
| 741 | + 'lastmodified' => $row['lastmodified'], |
|
| 742 | + '{' . Plugin::NS_CALDAV . '}supported-calendar-component-set' => new SupportedCalendarComponentSet(['VTODO', 'VEVENT']), |
|
| 743 | + '{http://sabredav.org/ns}sync-token' => $row['synctoken']?$row['synctoken']:'0', |
|
| 744 | + ]; |
|
| 745 | + |
|
| 746 | + return $this->rowToSubscription($row, $subscription); |
|
| 747 | + } |
|
| 748 | + |
|
| 749 | + /** |
|
| 750 | + * Creates a new calendar for a principal. |
|
| 751 | + * |
|
| 752 | + * If the creation was a success, an id must be returned that can be used to reference |
|
| 753 | + * this calendar in other methods, such as updateCalendar. |
|
| 754 | + * |
|
| 755 | + * @param string $principalUri |
|
| 756 | + * @param string $calendarUri |
|
| 757 | + * @param array $properties |
|
| 758 | + * @return int |
|
| 759 | + * |
|
| 760 | + * @throws CalendarException |
|
| 761 | + */ |
|
| 762 | + public function createCalendar($principalUri, $calendarUri, array $properties) { |
|
| 763 | + if (strlen($calendarUri) > 255) { |
|
| 764 | + throw new CalendarException('URI too long. Calendar not created'); |
|
| 765 | + } |
|
| 766 | + |
|
| 767 | + $values = [ |
|
| 768 | + 'principaluri' => $this->convertPrincipal($principalUri, true), |
|
| 769 | + 'uri' => $calendarUri, |
|
| 770 | + 'synctoken' => 1, |
|
| 771 | + 'transparent' => 0, |
|
| 772 | + 'components' => 'VEVENT,VTODO', |
|
| 773 | + 'displayname' => $calendarUri |
|
| 774 | + ]; |
|
| 775 | + |
|
| 776 | + // Default value |
|
| 777 | + $sccs = '{urn:ietf:params:xml:ns:caldav}supported-calendar-component-set'; |
|
| 778 | + if (isset($properties[$sccs])) { |
|
| 779 | + if (!($properties[$sccs] instanceof SupportedCalendarComponentSet)) { |
|
| 780 | + throw new DAV\Exception('The ' . $sccs . ' property must be of type: \Sabre\CalDAV\Property\SupportedCalendarComponentSet'); |
|
| 781 | + } |
|
| 782 | + $values['components'] = implode(',',$properties[$sccs]->getValue()); |
|
| 783 | + } elseif (isset($properties['components'])) { |
|
| 784 | + // Allow to provide components internally without having |
|
| 785 | + // to create a SupportedCalendarComponentSet object |
|
| 786 | + $values['components'] = $properties['components']; |
|
| 787 | + } |
|
| 788 | + |
|
| 789 | + $transp = '{' . Plugin::NS_CALDAV . '}schedule-calendar-transp'; |
|
| 790 | + if (isset($properties[$transp])) { |
|
| 791 | + $values['transparent'] = (int) ($properties[$transp]->getValue() === 'transparent'); |
|
| 792 | + } |
|
| 793 | + |
|
| 794 | + foreach ($this->propertyMap as $xmlName => [$dbName, $type]) { |
|
| 795 | + if (isset($properties[$xmlName])) { |
|
| 796 | + $values[$dbName] = $properties[$xmlName]; |
|
| 797 | + } |
|
| 798 | + } |
|
| 799 | + |
|
| 800 | + [$calendarId, $calendarData] = $this->atomic(function() use ($values) { |
|
| 801 | + $query = $this->db->getQueryBuilder(); |
|
| 802 | + $query->insert('calendars'); |
|
| 803 | + foreach ($values as $column => $value) { |
|
| 804 | + $query->setValue($column, $query->createNamedParameter($value)); |
|
| 805 | + } |
|
| 806 | + $query->executeStatement(); |
|
| 807 | + $calendarId = $query->getLastInsertId(); |
|
| 808 | + |
|
| 809 | + $calendarData = $this->getCalendarById($calendarId); |
|
| 810 | + return [$calendarId, $calendarData]; |
|
| 811 | + }, $this->db); |
|
| 812 | + |
|
| 813 | + $this->dispatcher->dispatchTyped(new CalendarCreatedEvent((int)$calendarId, $calendarData)); |
|
| 814 | + |
|
| 815 | + return $calendarId; |
|
| 816 | + } |
|
| 817 | + |
|
| 818 | + /** |
|
| 819 | + * Updates properties for a calendar. |
|
| 820 | + * |
|
| 821 | + * The list of mutations is stored in a Sabre\DAV\PropPatch object. |
|
| 822 | + * To do the actual updates, you must tell this object which properties |
|
| 823 | + * you're going to process with the handle() method. |
|
| 824 | + * |
|
| 825 | + * Calling the handle method is like telling the PropPatch object "I |
|
| 826 | + * promise I can handle updating this property". |
|
| 827 | + * |
|
| 828 | + * Read the PropPatch documentation for more info and examples. |
|
| 829 | + * |
|
| 830 | + * @param mixed $calendarId |
|
| 831 | + * @param PropPatch $propPatch |
|
| 832 | + * @return void |
|
| 833 | + */ |
|
| 834 | + public function updateCalendar($calendarId, PropPatch $propPatch) { |
|
| 835 | + $supportedProperties = array_keys($this->propertyMap); |
|
| 836 | + $supportedProperties[] = '{' . Plugin::NS_CALDAV . '}schedule-calendar-transp'; |
|
| 837 | + |
|
| 838 | + $propPatch->handle($supportedProperties, function ($mutations) use ($calendarId) { |
|
| 839 | + $newValues = []; |
|
| 840 | + foreach ($mutations as $propertyName => $propertyValue) { |
|
| 841 | + switch ($propertyName) { |
|
| 842 | + case '{' . Plugin::NS_CALDAV . '}schedule-calendar-transp': |
|
| 843 | + $fieldName = 'transparent'; |
|
| 844 | + $newValues[$fieldName] = (int) ($propertyValue->getValue() === 'transparent'); |
|
| 845 | + break; |
|
| 846 | + default: |
|
| 847 | + $fieldName = $this->propertyMap[$propertyName][0]; |
|
| 848 | + $newValues[$fieldName] = $propertyValue; |
|
| 849 | + break; |
|
| 850 | + } |
|
| 851 | + } |
|
| 852 | + $query = $this->db->getQueryBuilder(); |
|
| 853 | + $query->update('calendars'); |
|
| 854 | + foreach ($newValues as $fieldName => $value) { |
|
| 855 | + $query->set($fieldName, $query->createNamedParameter($value)); |
|
| 856 | + } |
|
| 857 | + $query->where($query->expr()->eq('id', $query->createNamedParameter($calendarId))); |
|
| 858 | + $query->executeStatement(); |
|
| 859 | + |
|
| 860 | + $this->addChange($calendarId, "", 2); |
|
| 861 | + |
|
| 862 | + $calendarData = $this->getCalendarById($calendarId); |
|
| 863 | + $shares = $this->getShares($calendarId); |
|
| 864 | + $this->dispatcher->dispatchTyped(new CalendarUpdatedEvent($calendarId, $calendarData, $shares, $mutations)); |
|
| 865 | + |
|
| 866 | + return true; |
|
| 867 | + }); |
|
| 868 | + } |
|
| 869 | + |
|
| 870 | + /** |
|
| 871 | + * Delete a calendar and all it's objects |
|
| 872 | + * |
|
| 873 | + * @param mixed $calendarId |
|
| 874 | + * @return void |
|
| 875 | + */ |
|
| 876 | + public function deleteCalendar($calendarId, bool $forceDeletePermanently = false) { |
|
| 877 | + // The calendar is deleted right away if this is either enforced by the caller |
|
| 878 | + // or the special contacts birthday calendar or when the preference of an empty |
|
| 879 | + // retention (0 seconds) is set, which signals a disabled trashbin. |
|
| 880 | + $calendarData = $this->getCalendarById($calendarId); |
|
| 881 | + $isBirthdayCalendar = isset($calendarData['uri']) && $calendarData['uri'] === BirthdayService::BIRTHDAY_CALENDAR_URI; |
|
| 882 | + $trashbinDisabled = $this->config->getAppValue(Application::APP_ID, RetentionService::RETENTION_CONFIG_KEY) === '0'; |
|
| 883 | + if ($forceDeletePermanently || $isBirthdayCalendar || $trashbinDisabled) { |
|
| 884 | + $calendarData = $this->getCalendarById($calendarId); |
|
| 885 | + $shares = $this->getShares($calendarId); |
|
| 886 | + |
|
| 887 | + $qbDeleteCalendarObjectProps = $this->db->getQueryBuilder(); |
|
| 888 | + $qbDeleteCalendarObjectProps->delete($this->dbObjectPropertiesTable) |
|
| 889 | + ->where($qbDeleteCalendarObjectProps->expr()->eq('calendarid', $qbDeleteCalendarObjectProps->createNamedParameter($calendarId))) |
|
| 890 | + ->andWhere($qbDeleteCalendarObjectProps->expr()->eq('calendartype', $qbDeleteCalendarObjectProps->createNamedParameter(self::CALENDAR_TYPE_CALENDAR))) |
|
| 891 | + ->executeStatement(); |
|
| 892 | + |
|
| 893 | + $qbDeleteCalendarObjects = $this->db->getQueryBuilder(); |
|
| 894 | + $qbDeleteCalendarObjects->delete('calendarobjects') |
|
| 895 | + ->where($qbDeleteCalendarObjects->expr()->eq('calendarid', $qbDeleteCalendarObjects->createNamedParameter($calendarId))) |
|
| 896 | + ->andWhere($qbDeleteCalendarObjects->expr()->eq('calendartype', $qbDeleteCalendarObjects->createNamedParameter(self::CALENDAR_TYPE_CALENDAR))) |
|
| 897 | + ->executeStatement(); |
|
| 898 | + |
|
| 899 | + $qbDeleteCalendarChanges = $this->db->getQueryBuilder(); |
|
| 900 | + $qbDeleteCalendarChanges->delete('calendarchanges') |
|
| 901 | + ->where($qbDeleteCalendarChanges->expr()->eq('calendarid', $qbDeleteCalendarChanges->createNamedParameter($calendarId))) |
|
| 902 | + ->andWhere($qbDeleteCalendarChanges->expr()->eq('calendartype', $qbDeleteCalendarChanges->createNamedParameter(self::CALENDAR_TYPE_CALENDAR))) |
|
| 903 | + ->executeStatement(); |
|
| 904 | + |
|
| 905 | + $this->calendarSharingBackend->deleteAllShares($calendarId); |
|
| 906 | + |
|
| 907 | + $qbDeleteCalendar = $this->db->getQueryBuilder(); |
|
| 908 | + $qbDeleteCalendar->delete('calendars') |
|
| 909 | + ->where($qbDeleteCalendar->expr()->eq('id', $qbDeleteCalendar->createNamedParameter($calendarId))) |
|
| 910 | + ->executeStatement(); |
|
| 911 | + |
|
| 912 | + // Only dispatch if we actually deleted anything |
|
| 913 | + if ($calendarData) { |
|
| 914 | + $this->dispatcher->dispatchTyped(new CalendarDeletedEvent($calendarId, $calendarData, $shares)); |
|
| 915 | + } |
|
| 916 | + } else { |
|
| 917 | + $qbMarkCalendarDeleted = $this->db->getQueryBuilder(); |
|
| 918 | + $qbMarkCalendarDeleted->update('calendars') |
|
| 919 | + ->set('deleted_at', $qbMarkCalendarDeleted->createNamedParameter(time())) |
|
| 920 | + ->where($qbMarkCalendarDeleted->expr()->eq('id', $qbMarkCalendarDeleted->createNamedParameter($calendarId))) |
|
| 921 | + ->executeStatement(); |
|
| 922 | + |
|
| 923 | + $calendarData = $this->getCalendarById($calendarId); |
|
| 924 | + $shares = $this->getShares($calendarId); |
|
| 925 | + if ($calendarData) { |
|
| 926 | + $this->dispatcher->dispatchTyped(new CalendarMovedToTrashEvent( |
|
| 927 | + $calendarId, |
|
| 928 | + $calendarData, |
|
| 929 | + $shares |
|
| 930 | + )); |
|
| 931 | + } |
|
| 932 | + } |
|
| 933 | + } |
|
| 934 | + |
|
| 935 | + public function restoreCalendar(int $id): void { |
|
| 936 | + $qb = $this->db->getQueryBuilder(); |
|
| 937 | + $update = $qb->update('calendars') |
|
| 938 | + ->set('deleted_at', $qb->createNamedParameter(null)) |
|
| 939 | + ->where($qb->expr()->eq('id', $qb->createNamedParameter($id, IQueryBuilder::PARAM_INT), IQueryBuilder::PARAM_INT)); |
|
| 940 | + $update->executeStatement(); |
|
| 941 | + |
|
| 942 | + $calendarData = $this->getCalendarById($id); |
|
| 943 | + $shares = $this->getShares($id); |
|
| 944 | + if ($calendarData === null) { |
|
| 945 | + throw new RuntimeException('Calendar data that was just written can\'t be read back. Check your database configuration.'); |
|
| 946 | + } |
|
| 947 | + $this->dispatcher->dispatchTyped(new CalendarRestoredEvent( |
|
| 948 | + $id, |
|
| 949 | + $calendarData, |
|
| 950 | + $shares |
|
| 951 | + )); |
|
| 952 | + } |
|
| 953 | + |
|
| 954 | + /** |
|
| 955 | + * Delete all of an user's shares |
|
| 956 | + * |
|
| 957 | + * @param string $principaluri |
|
| 958 | + * @return void |
|
| 959 | + */ |
|
| 960 | + public function deleteAllSharesByUser($principaluri) { |
|
| 961 | + $this->calendarSharingBackend->deleteAllSharesByUser($principaluri); |
|
| 962 | + } |
|
| 963 | + |
|
| 964 | + /** |
|
| 965 | + * Returns all calendar objects within a calendar. |
|
| 966 | + * |
|
| 967 | + * Every item contains an array with the following keys: |
|
| 968 | + * * calendardata - The iCalendar-compatible calendar data |
|
| 969 | + * * uri - a unique key which will be used to construct the uri. This can |
|
| 970 | + * be any arbitrary string, but making sure it ends with '.ics' is a |
|
| 971 | + * good idea. This is only the basename, or filename, not the full |
|
| 972 | + * path. |
|
| 973 | + * * lastmodified - a timestamp of the last modification time |
|
| 974 | + * * etag - An arbitrary string, surrounded by double-quotes. (e.g.: |
|
| 975 | + * '"abcdef"') |
|
| 976 | + * * size - The size of the calendar objects, in bytes. |
|
| 977 | + * * component - optional, a string containing the type of object, such |
|
| 978 | + * as 'vevent' or 'vtodo'. If specified, this will be used to populate |
|
| 979 | + * the Content-Type header. |
|
| 980 | + * |
|
| 981 | + * Note that the etag is optional, but it's highly encouraged to return for |
|
| 982 | + * speed reasons. |
|
| 983 | + * |
|
| 984 | + * The calendardata is also optional. If it's not returned |
|
| 985 | + * 'getCalendarObject' will be called later, which *is* expected to return |
|
| 986 | + * calendardata. |
|
| 987 | + * |
|
| 988 | + * If neither etag or size are specified, the calendardata will be |
|
| 989 | + * used/fetched to determine these numbers. If both are specified the |
|
| 990 | + * amount of times this is needed is reduced by a great degree. |
|
| 991 | + * |
|
| 992 | + * @param mixed $calendarId |
|
| 993 | + * @param int $calendarType |
|
| 994 | + * @return array |
|
| 995 | + */ |
|
| 996 | + public function getCalendarObjects($calendarId, $calendarType = self::CALENDAR_TYPE_CALENDAR):array { |
|
| 997 | + $query = $this->db->getQueryBuilder(); |
|
| 998 | + $query->select(['id', 'uri', 'lastmodified', 'etag', 'calendarid', 'size', 'componenttype', 'classification']) |
|
| 999 | + ->from('calendarobjects') |
|
| 1000 | + ->where($query->expr()->eq('calendarid', $query->createNamedParameter($calendarId))) |
|
| 1001 | + ->andWhere($query->expr()->eq('calendartype', $query->createNamedParameter($calendarType))) |
|
| 1002 | + ->andWhere($query->expr()->isNull('deleted_at')); |
|
| 1003 | + $stmt = $query->executeQuery(); |
|
| 1004 | + |
|
| 1005 | + $result = []; |
|
| 1006 | + foreach ($stmt->fetchAll() as $row) { |
|
| 1007 | + $result[] = [ |
|
| 1008 | + 'id' => $row['id'], |
|
| 1009 | + 'uri' => $row['uri'], |
|
| 1010 | + 'lastmodified' => $row['lastmodified'], |
|
| 1011 | + 'etag' => '"' . $row['etag'] . '"', |
|
| 1012 | + 'calendarid' => $row['calendarid'], |
|
| 1013 | + 'size' => (int)$row['size'], |
|
| 1014 | + 'component' => strtolower($row['componenttype']), |
|
| 1015 | + 'classification' => (int)$row['classification'] |
|
| 1016 | + ]; |
|
| 1017 | + } |
|
| 1018 | + $stmt->closeCursor(); |
|
| 1019 | + |
|
| 1020 | + return $result; |
|
| 1021 | + } |
|
| 1022 | + |
|
| 1023 | + public function getDeletedCalendarObjects(int $deletedBefore): array { |
|
| 1024 | + $query = $this->db->getQueryBuilder(); |
|
| 1025 | + $query->select(['co.id', 'co.uri', 'co.lastmodified', 'co.etag', 'co.calendarid', 'co.calendartype', 'co.size', 'co.componenttype', 'co.classification', 'co.deleted_at']) |
|
| 1026 | + ->from('calendarobjects', 'co') |
|
| 1027 | + ->join('co', 'calendars', 'c', $query->expr()->eq('c.id', 'co.calendarid', IQueryBuilder::PARAM_INT)) |
|
| 1028 | + ->where($query->expr()->isNotNull('co.deleted_at')) |
|
| 1029 | + ->andWhere($query->expr()->lt('co.deleted_at', $query->createNamedParameter($deletedBefore))); |
|
| 1030 | + $stmt = $query->executeQuery(); |
|
| 1031 | + |
|
| 1032 | + $result = []; |
|
| 1033 | + foreach ($stmt->fetchAll() as $row) { |
|
| 1034 | + $result[] = [ |
|
| 1035 | + 'id' => $row['id'], |
|
| 1036 | + 'uri' => $row['uri'], |
|
| 1037 | + 'lastmodified' => $row['lastmodified'], |
|
| 1038 | + 'etag' => '"' . $row['etag'] . '"', |
|
| 1039 | + 'calendarid' => (int) $row['calendarid'], |
|
| 1040 | + 'calendartype' => (int) $row['calendartype'], |
|
| 1041 | + 'size' => (int) $row['size'], |
|
| 1042 | + 'component' => strtolower($row['componenttype']), |
|
| 1043 | + 'classification' => (int) $row['classification'], |
|
| 1044 | + '{' . \OCA\DAV\DAV\Sharing\Plugin::NS_NEXTCLOUD . '}deleted-at' => $row['deleted_at'] === null ? $row['deleted_at'] : (int) $row['deleted_at'], |
|
| 1045 | + ]; |
|
| 1046 | + } |
|
| 1047 | + $stmt->closeCursor(); |
|
| 1048 | + |
|
| 1049 | + return $result; |
|
| 1050 | + } |
|
| 1051 | + |
|
| 1052 | + /** |
|
| 1053 | + * Return all deleted calendar objects by the given principal that are not |
|
| 1054 | + * in deleted calendars. |
|
| 1055 | + * |
|
| 1056 | + * @param string $principalUri |
|
| 1057 | + * @return array |
|
| 1058 | + * @throws Exception |
|
| 1059 | + */ |
|
| 1060 | + public function getDeletedCalendarObjectsByPrincipal(string $principalUri): array { |
|
| 1061 | + $query = $this->db->getQueryBuilder(); |
|
| 1062 | + $query->select(['co.id', 'co.uri', 'co.lastmodified', 'co.etag', 'co.calendarid', 'co.size', 'co.componenttype', 'co.classification', 'co.deleted_at']) |
|
| 1063 | + ->selectAlias('c.uri', 'calendaruri') |
|
| 1064 | + ->from('calendarobjects', 'co') |
|
| 1065 | + ->join('co', 'calendars', 'c', $query->expr()->eq('c.id', 'co.calendarid', IQueryBuilder::PARAM_INT)) |
|
| 1066 | + ->where($query->expr()->eq('principaluri', $query->createNamedParameter($principalUri))) |
|
| 1067 | + ->andWhere($query->expr()->isNotNull('co.deleted_at')) |
|
| 1068 | + ->andWhere($query->expr()->isNull('c.deleted_at')); |
|
| 1069 | + $stmt = $query->executeQuery(); |
|
| 1070 | + |
|
| 1071 | + $result = []; |
|
| 1072 | + while ($row = $stmt->fetch()) { |
|
| 1073 | + $result[] = [ |
|
| 1074 | + 'id' => $row['id'], |
|
| 1075 | + 'uri' => $row['uri'], |
|
| 1076 | + 'lastmodified' => $row['lastmodified'], |
|
| 1077 | + 'etag' => '"' . $row['etag'] . '"', |
|
| 1078 | + 'calendarid' => $row['calendarid'], |
|
| 1079 | + 'calendaruri' => $row['calendaruri'], |
|
| 1080 | + 'size' => (int)$row['size'], |
|
| 1081 | + 'component' => strtolower($row['componenttype']), |
|
| 1082 | + 'classification' => (int)$row['classification'], |
|
| 1083 | + '{' . \OCA\DAV\DAV\Sharing\Plugin::NS_NEXTCLOUD . '}deleted-at' => $row['deleted_at'] === null ? $row['deleted_at'] : (int) $row['deleted_at'], |
|
| 1084 | + ]; |
|
| 1085 | + } |
|
| 1086 | + $stmt->closeCursor(); |
|
| 1087 | + |
|
| 1088 | + return $result; |
|
| 1089 | + } |
|
| 1090 | + |
|
| 1091 | + /** |
|
| 1092 | + * Returns information from a single calendar object, based on it's object |
|
| 1093 | + * uri. |
|
| 1094 | + * |
|
| 1095 | + * The object uri is only the basename, or filename and not a full path. |
|
| 1096 | + * |
|
| 1097 | + * The returned array must have the same keys as getCalendarObjects. The |
|
| 1098 | + * 'calendardata' object is required here though, while it's not required |
|
| 1099 | + * for getCalendarObjects. |
|
| 1100 | + * |
|
| 1101 | + * This method must return null if the object did not exist. |
|
| 1102 | + * |
|
| 1103 | + * @param mixed $calendarId |
|
| 1104 | + * @param string $objectUri |
|
| 1105 | + * @param int $calendarType |
|
| 1106 | + * @return array|null |
|
| 1107 | + */ |
|
| 1108 | + public function getCalendarObject($calendarId, $objectUri, int $calendarType = self::CALENDAR_TYPE_CALENDAR) { |
|
| 1109 | + $query = $this->db->getQueryBuilder(); |
|
| 1110 | + $query->select(['id', 'uri', 'lastmodified', 'etag', 'calendarid', 'size', 'calendardata', 'componenttype', 'classification', 'deleted_at']) |
|
| 1111 | + ->from('calendarobjects') |
|
| 1112 | + ->where($query->expr()->eq('calendarid', $query->createNamedParameter($calendarId))) |
|
| 1113 | + ->andWhere($query->expr()->eq('uri', $query->createNamedParameter($objectUri))) |
|
| 1114 | + ->andWhere($query->expr()->eq('calendartype', $query->createNamedParameter($calendarType))); |
|
| 1115 | + $stmt = $query->executeQuery(); |
|
| 1116 | + $row = $stmt->fetch(); |
|
| 1117 | + $stmt->closeCursor(); |
|
| 1118 | + |
|
| 1119 | + if (!$row) { |
|
| 1120 | + return null; |
|
| 1121 | + } |
|
| 1122 | + |
|
| 1123 | + return [ |
|
| 1124 | + 'id' => $row['id'], |
|
| 1125 | + 'uri' => $row['uri'], |
|
| 1126 | + 'lastmodified' => $row['lastmodified'], |
|
| 1127 | + 'etag' => '"' . $row['etag'] . '"', |
|
| 1128 | + 'calendarid' => $row['calendarid'], |
|
| 1129 | + 'size' => (int)$row['size'], |
|
| 1130 | + 'calendardata' => $this->readBlob($row['calendardata']), |
|
| 1131 | + 'component' => strtolower($row['componenttype']), |
|
| 1132 | + 'classification' => (int)$row['classification'], |
|
| 1133 | + '{' . \OCA\DAV\DAV\Sharing\Plugin::NS_NEXTCLOUD . '}deleted-at' => $row['deleted_at'] === null ? $row['deleted_at'] : (int) $row['deleted_at'], |
|
| 1134 | + ]; |
|
| 1135 | + } |
|
| 1136 | + |
|
| 1137 | + /** |
|
| 1138 | + * Returns a list of calendar objects. |
|
| 1139 | + * |
|
| 1140 | + * This method should work identical to getCalendarObject, but instead |
|
| 1141 | + * return all the calendar objects in the list as an array. |
|
| 1142 | + * |
|
| 1143 | + * If the backend supports this, it may allow for some speed-ups. |
|
| 1144 | + * |
|
| 1145 | + * @param mixed $calendarId |
|
| 1146 | + * @param string[] $uris |
|
| 1147 | + * @param int $calendarType |
|
| 1148 | + * @return array |
|
| 1149 | + */ |
|
| 1150 | + public function getMultipleCalendarObjects($calendarId, array $uris, $calendarType = self::CALENDAR_TYPE_CALENDAR):array { |
|
| 1151 | + if (empty($uris)) { |
|
| 1152 | + return []; |
|
| 1153 | + } |
|
| 1154 | + |
|
| 1155 | + $chunks = array_chunk($uris, 100); |
|
| 1156 | + $objects = []; |
|
| 1157 | + |
|
| 1158 | + $query = $this->db->getQueryBuilder(); |
|
| 1159 | + $query->select(['id', 'uri', 'lastmodified', 'etag', 'calendarid', 'size', 'calendardata', 'componenttype', 'classification']) |
|
| 1160 | + ->from('calendarobjects') |
|
| 1161 | + ->where($query->expr()->eq('calendarid', $query->createNamedParameter($calendarId))) |
|
| 1162 | + ->andWhere($query->expr()->in('uri', $query->createParameter('uri'))) |
|
| 1163 | + ->andWhere($query->expr()->eq('calendartype', $query->createNamedParameter($calendarType))) |
|
| 1164 | + ->andWhere($query->expr()->isNull('deleted_at')); |
|
| 1165 | + |
|
| 1166 | + foreach ($chunks as $uris) { |
|
| 1167 | + $query->setParameter('uri', $uris, IQueryBuilder::PARAM_STR_ARRAY); |
|
| 1168 | + $result = $query->executeQuery(); |
|
| 1169 | + |
|
| 1170 | + while ($row = $result->fetch()) { |
|
| 1171 | + $objects[] = [ |
|
| 1172 | + 'id' => $row['id'], |
|
| 1173 | + 'uri' => $row['uri'], |
|
| 1174 | + 'lastmodified' => $row['lastmodified'], |
|
| 1175 | + 'etag' => '"' . $row['etag'] . '"', |
|
| 1176 | + 'calendarid' => $row['calendarid'], |
|
| 1177 | + 'size' => (int)$row['size'], |
|
| 1178 | + 'calendardata' => $this->readBlob($row['calendardata']), |
|
| 1179 | + 'component' => strtolower($row['componenttype']), |
|
| 1180 | + 'classification' => (int)$row['classification'] |
|
| 1181 | + ]; |
|
| 1182 | + } |
|
| 1183 | + $result->closeCursor(); |
|
| 1184 | + } |
|
| 1185 | + |
|
| 1186 | + return $objects; |
|
| 1187 | + } |
|
| 1188 | + |
|
| 1189 | + /** |
|
| 1190 | + * Creates a new calendar object. |
|
| 1191 | + * |
|
| 1192 | + * The object uri is only the basename, or filename and not a full path. |
|
| 1193 | + * |
|
| 1194 | + * It is possible return an etag from this function, which will be used in |
|
| 1195 | + * the response to this PUT request. Note that the ETag must be surrounded |
|
| 1196 | + * by double-quotes. |
|
| 1197 | + * |
|
| 1198 | + * However, you should only really return this ETag if you don't mangle the |
|
| 1199 | + * calendar-data. If the result of a subsequent GET to this object is not |
|
| 1200 | + * the exact same as this request body, you should omit the ETag. |
|
| 1201 | + * |
|
| 1202 | + * @param mixed $calendarId |
|
| 1203 | + * @param string $objectUri |
|
| 1204 | + * @param string $calendarData |
|
| 1205 | + * @param int $calendarType |
|
| 1206 | + * @return string |
|
| 1207 | + */ |
|
| 1208 | + public function createCalendarObject($calendarId, $objectUri, $calendarData, $calendarType = self::CALENDAR_TYPE_CALENDAR) { |
|
| 1209 | + $extraData = $this->getDenormalizedData($calendarData); |
|
| 1210 | + |
|
| 1211 | + // Try to detect duplicates |
|
| 1212 | + $qb = $this->db->getQueryBuilder(); |
|
| 1213 | + $qb->select($qb->func()->count('*')) |
|
| 1214 | + ->from('calendarobjects') |
|
| 1215 | + ->where($qb->expr()->eq('calendarid', $qb->createNamedParameter($calendarId))) |
|
| 1216 | + ->andWhere($qb->expr()->eq('uid', $qb->createNamedParameter($extraData['uid']))) |
|
| 1217 | + ->andWhere($qb->expr()->eq('calendartype', $qb->createNamedParameter($calendarType))) |
|
| 1218 | + ->andWhere($qb->expr()->isNull('deleted_at')); |
|
| 1219 | + $result = $qb->executeQuery(); |
|
| 1220 | + $count = (int) $result->fetchOne(); |
|
| 1221 | + $result->closeCursor(); |
|
| 1222 | + |
|
| 1223 | + if ($count !== 0) { |
|
| 1224 | + throw new BadRequest('Calendar object with uid already exists in this calendar collection.'); |
|
| 1225 | + } |
|
| 1226 | + // For a more specific error message we also try to explicitly look up the UID but as a deleted entry |
|
| 1227 | + $qbDel = $this->db->getQueryBuilder(); |
|
| 1228 | + $qbDel->select($qb->func()->count('*')) |
|
| 1229 | + ->from('calendarobjects') |
|
| 1230 | + ->where($qbDel->expr()->eq('calendarid', $qbDel->createNamedParameter($calendarId))) |
|
| 1231 | + ->andWhere($qbDel->expr()->eq('uid', $qbDel->createNamedParameter($extraData['uid']))) |
|
| 1232 | + ->andWhere($qbDel->expr()->eq('calendartype', $qbDel->createNamedParameter($calendarType))) |
|
| 1233 | + ->andWhere($qbDel->expr()->isNotNull('deleted_at')); |
|
| 1234 | + $result = $qbDel->executeQuery(); |
|
| 1235 | + $count = (int) $result->fetchOne(); |
|
| 1236 | + $result->closeCursor(); |
|
| 1237 | + if ($count !== 0) { |
|
| 1238 | + throw new BadRequest('Deleted calendar object with uid already exists in this calendar collection.'); |
|
| 1239 | + } |
|
| 1240 | + |
|
| 1241 | + $query = $this->db->getQueryBuilder(); |
|
| 1242 | + $query->insert('calendarobjects') |
|
| 1243 | + ->values([ |
|
| 1244 | + 'calendarid' => $query->createNamedParameter($calendarId), |
|
| 1245 | + 'uri' => $query->createNamedParameter($objectUri), |
|
| 1246 | + 'calendardata' => $query->createNamedParameter($calendarData, IQueryBuilder::PARAM_LOB), |
|
| 1247 | + 'lastmodified' => $query->createNamedParameter(time()), |
|
| 1248 | + 'etag' => $query->createNamedParameter($extraData['etag']), |
|
| 1249 | + 'size' => $query->createNamedParameter($extraData['size']), |
|
| 1250 | + 'componenttype' => $query->createNamedParameter($extraData['componentType']), |
|
| 1251 | + 'firstoccurence' => $query->createNamedParameter($extraData['firstOccurence']), |
|
| 1252 | + 'lastoccurence' => $query->createNamedParameter($extraData['lastOccurence']), |
|
| 1253 | + 'classification' => $query->createNamedParameter($extraData['classification']), |
|
| 1254 | + 'uid' => $query->createNamedParameter($extraData['uid']), |
|
| 1255 | + 'calendartype' => $query->createNamedParameter($calendarType), |
|
| 1256 | + ]) |
|
| 1257 | + ->executeStatement(); |
|
| 1258 | + |
|
| 1259 | + $this->updateProperties($calendarId, $objectUri, $calendarData, $calendarType); |
|
| 1260 | + $this->addChange($calendarId, $objectUri, 1, $calendarType); |
|
| 1261 | + |
|
| 1262 | + $objectRow = $this->getCalendarObject($calendarId, $objectUri, $calendarType); |
|
| 1263 | + assert($objectRow !== null); |
|
| 1264 | + |
|
| 1265 | + if ($calendarType === self::CALENDAR_TYPE_CALENDAR) { |
|
| 1266 | + $calendarRow = $this->getCalendarById($calendarId); |
|
| 1267 | + $shares = $this->getShares($calendarId); |
|
| 1268 | + |
|
| 1269 | + $this->dispatcher->dispatchTyped(new CalendarObjectCreatedEvent($calendarId, $calendarRow, $shares, $objectRow)); |
|
| 1270 | + } else { |
|
| 1271 | + $subscriptionRow = $this->getSubscriptionById($calendarId); |
|
| 1272 | + |
|
| 1273 | + $this->dispatcher->dispatchTyped(new CachedCalendarObjectCreatedEvent($calendarId, $subscriptionRow, [], $objectRow)); |
|
| 1274 | + } |
|
| 1275 | + |
|
| 1276 | + return '"' . $extraData['etag'] . '"'; |
|
| 1277 | + } |
|
| 1278 | + |
|
| 1279 | + /** |
|
| 1280 | + * Updates an existing calendarobject, based on it's uri. |
|
| 1281 | + * |
|
| 1282 | + * The object uri is only the basename, or filename and not a full path. |
|
| 1283 | + * |
|
| 1284 | + * It is possible return an etag from this function, which will be used in |
|
| 1285 | + * the response to this PUT request. Note that the ETag must be surrounded |
|
| 1286 | + * by double-quotes. |
|
| 1287 | + * |
|
| 1288 | + * However, you should only really return this ETag if you don't mangle the |
|
| 1289 | + * calendar-data. If the result of a subsequent GET to this object is not |
|
| 1290 | + * the exact same as this request body, you should omit the ETag. |
|
| 1291 | + * |
|
| 1292 | + * @param mixed $calendarId |
|
| 1293 | + * @param string $objectUri |
|
| 1294 | + * @param string $calendarData |
|
| 1295 | + * @param int $calendarType |
|
| 1296 | + * @return string |
|
| 1297 | + */ |
|
| 1298 | + public function updateCalendarObject($calendarId, $objectUri, $calendarData, $calendarType = self::CALENDAR_TYPE_CALENDAR) { |
|
| 1299 | + $extraData = $this->getDenormalizedData($calendarData); |
|
| 1300 | + $query = $this->db->getQueryBuilder(); |
|
| 1301 | + $query->update('calendarobjects') |
|
| 1302 | + ->set('calendardata', $query->createNamedParameter($calendarData, IQueryBuilder::PARAM_LOB)) |
|
| 1303 | + ->set('lastmodified', $query->createNamedParameter(time())) |
|
| 1304 | + ->set('etag', $query->createNamedParameter($extraData['etag'])) |
|
| 1305 | + ->set('size', $query->createNamedParameter($extraData['size'])) |
|
| 1306 | + ->set('componenttype', $query->createNamedParameter($extraData['componentType'])) |
|
| 1307 | + ->set('firstoccurence', $query->createNamedParameter($extraData['firstOccurence'])) |
|
| 1308 | + ->set('lastoccurence', $query->createNamedParameter($extraData['lastOccurence'])) |
|
| 1309 | + ->set('classification', $query->createNamedParameter($extraData['classification'])) |
|
| 1310 | + ->set('uid', $query->createNamedParameter($extraData['uid'])) |
|
| 1311 | + ->where($query->expr()->eq('calendarid', $query->createNamedParameter($calendarId))) |
|
| 1312 | + ->andWhere($query->expr()->eq('uri', $query->createNamedParameter($objectUri))) |
|
| 1313 | + ->andWhere($query->expr()->eq('calendartype', $query->createNamedParameter($calendarType))) |
|
| 1314 | + ->executeStatement(); |
|
| 1315 | + |
|
| 1316 | + $this->updateProperties($calendarId, $objectUri, $calendarData, $calendarType); |
|
| 1317 | + $this->addChange($calendarId, $objectUri, 2, $calendarType); |
|
| 1318 | + |
|
| 1319 | + $objectRow = $this->getCalendarObject($calendarId, $objectUri, $calendarType); |
|
| 1320 | + if (is_array($objectRow)) { |
|
| 1321 | + if ($calendarType === self::CALENDAR_TYPE_CALENDAR) { |
|
| 1322 | + $calendarRow = $this->getCalendarById($calendarId); |
|
| 1323 | + $shares = $this->getShares($calendarId); |
|
| 1324 | + |
|
| 1325 | + $this->dispatcher->dispatchTyped(new CalendarObjectUpdatedEvent($calendarId, $calendarRow, $shares, $objectRow)); |
|
| 1326 | + } else { |
|
| 1327 | + $subscriptionRow = $this->getSubscriptionById($calendarId); |
|
| 1328 | + |
|
| 1329 | + $this->dispatcher->dispatchTyped(new CachedCalendarObjectUpdatedEvent($calendarId, $subscriptionRow, [], $objectRow)); |
|
| 1330 | + } |
|
| 1331 | + } |
|
| 1332 | + |
|
| 1333 | + return '"' . $extraData['etag'] . '"'; |
|
| 1334 | + } |
|
| 1335 | + |
|
| 1336 | + /** |
|
| 1337 | + * Moves a calendar object from calendar to calendar. |
|
| 1338 | + * |
|
| 1339 | + * @param int $sourceCalendarId |
|
| 1340 | + * @param int $targetCalendarId |
|
| 1341 | + * @param int $objectId |
|
| 1342 | + * @param string $oldPrincipalUri |
|
| 1343 | + * @param string $newPrincipalUri |
|
| 1344 | + * @param int $calendarType |
|
| 1345 | + * @return bool |
|
| 1346 | + * @throws Exception |
|
| 1347 | + */ |
|
| 1348 | + public function moveCalendarObject(int $sourceCalendarId, int $targetCalendarId, int $objectId, string $oldPrincipalUri, string $newPrincipalUri, int $calendarType = self::CALENDAR_TYPE_CALENDAR): bool { |
|
| 1349 | + $object = $this->getCalendarObjectById($oldPrincipalUri, $objectId); |
|
| 1350 | + if (empty($object)) { |
|
| 1351 | + return false; |
|
| 1352 | + } |
|
| 1353 | + |
|
| 1354 | + $query = $this->db->getQueryBuilder(); |
|
| 1355 | + $query->update('calendarobjects') |
|
| 1356 | + ->set('calendarid', $query->createNamedParameter($targetCalendarId, IQueryBuilder::PARAM_INT)) |
|
| 1357 | + ->where($query->expr()->eq('id', $query->createNamedParameter($objectId, IQueryBuilder::PARAM_INT), IQueryBuilder::PARAM_INT)) |
|
| 1358 | + ->andWhere($query->expr()->eq('calendartype', $query->createNamedParameter($calendarType, IQueryBuilder::PARAM_INT), IQueryBuilder::PARAM_INT)) |
|
| 1359 | + ->executeStatement(); |
|
| 1360 | + |
|
| 1361 | + $this->purgeProperties($sourceCalendarId, $objectId); |
|
| 1362 | + $this->updateProperties($targetCalendarId, $object['uri'], $object['calendardata'], $calendarType); |
|
| 1363 | + |
|
| 1364 | + $this->addChange($sourceCalendarId, $object['uri'], 1, $calendarType); |
|
| 1365 | + $this->addChange($targetCalendarId, $object['uri'], 3, $calendarType); |
|
| 1366 | + |
|
| 1367 | + $object = $this->getCalendarObjectById($newPrincipalUri, $objectId); |
|
| 1368 | + // Calendar Object wasn't found - possibly because it was deleted in the meantime by a different client |
|
| 1369 | + if (empty($object)) { |
|
| 1370 | + return false; |
|
| 1371 | + } |
|
| 1372 | + |
|
| 1373 | + $targetCalendarRow = $this->getCalendarById($targetCalendarId); |
|
| 1374 | + // the calendar this event is being moved to does not exist any longer |
|
| 1375 | + if (empty($targetCalendarRow)) { |
|
| 1376 | + return false; |
|
| 1377 | + } |
|
| 1378 | + |
|
| 1379 | + if ($calendarType === self::CALENDAR_TYPE_CALENDAR) { |
|
| 1380 | + $sourceShares = $this->getShares($sourceCalendarId); |
|
| 1381 | + $targetShares = $this->getShares($targetCalendarId); |
|
| 1382 | + $sourceCalendarRow = $this->getCalendarById($sourceCalendarId); |
|
| 1383 | + $this->dispatcher->dispatchTyped(new CalendarObjectMovedEvent($sourceCalendarId, $sourceCalendarRow, $targetCalendarId, $targetCalendarRow, $sourceShares, $targetShares, $object)); |
|
| 1384 | + } |
|
| 1385 | + return true; |
|
| 1386 | + } |
|
| 1387 | + |
|
| 1388 | + |
|
| 1389 | + /** |
|
| 1390 | + * @param int $calendarObjectId |
|
| 1391 | + * @param int $classification |
|
| 1392 | + */ |
|
| 1393 | + public function setClassification($calendarObjectId, $classification) { |
|
| 1394 | + if (!in_array($classification, [ |
|
| 1395 | + self::CLASSIFICATION_PUBLIC, self::CLASSIFICATION_PRIVATE, self::CLASSIFICATION_CONFIDENTIAL |
|
| 1396 | + ])) { |
|
| 1397 | + throw new \InvalidArgumentException(); |
|
| 1398 | + } |
|
| 1399 | + $query = $this->db->getQueryBuilder(); |
|
| 1400 | + $query->update('calendarobjects') |
|
| 1401 | + ->set('classification', $query->createNamedParameter($classification)) |
|
| 1402 | + ->where($query->expr()->eq('id', $query->createNamedParameter($calendarObjectId))) |
|
| 1403 | + ->executeStatement(); |
|
| 1404 | + } |
|
| 1405 | + |
|
| 1406 | + /** |
|
| 1407 | + * Deletes an existing calendar object. |
|
| 1408 | + * |
|
| 1409 | + * The object uri is only the basename, or filename and not a full path. |
|
| 1410 | + * |
|
| 1411 | + * @param mixed $calendarId |
|
| 1412 | + * @param string $objectUri |
|
| 1413 | + * @param int $calendarType |
|
| 1414 | + * @param bool $forceDeletePermanently |
|
| 1415 | + * @return void |
|
| 1416 | + */ |
|
| 1417 | + public function deleteCalendarObject($calendarId, $objectUri, $calendarType = self::CALENDAR_TYPE_CALENDAR, bool $forceDeletePermanently = false) { |
|
| 1418 | + $data = $this->getCalendarObject($calendarId, $objectUri, $calendarType); |
|
| 1419 | + |
|
| 1420 | + if ($data === null) { |
|
| 1421 | + // Nothing to delete |
|
| 1422 | + return; |
|
| 1423 | + } |
|
| 1424 | + |
|
| 1425 | + if ($forceDeletePermanently || $this->config->getAppValue(Application::APP_ID, RetentionService::RETENTION_CONFIG_KEY) === '0') { |
|
| 1426 | + $stmt = $this->db->prepare('DELETE FROM `*PREFIX*calendarobjects` WHERE `calendarid` = ? AND `uri` = ? AND `calendartype` = ?'); |
|
| 1427 | + $stmt->execute([$calendarId, $objectUri, $calendarType]); |
|
| 1428 | + |
|
| 1429 | + $this->purgeProperties($calendarId, $data['id']); |
|
| 1430 | + |
|
| 1431 | + if ($calendarType === self::CALENDAR_TYPE_CALENDAR) { |
|
| 1432 | + $calendarRow = $this->getCalendarById($calendarId); |
|
| 1433 | + $shares = $this->getShares($calendarId); |
|
| 1434 | + |
|
| 1435 | + $this->dispatcher->dispatchTyped(new CalendarObjectDeletedEvent($calendarId, $calendarRow, $shares, $data)); |
|
| 1436 | + } else { |
|
| 1437 | + $subscriptionRow = $this->getSubscriptionById($calendarId); |
|
| 1438 | + |
|
| 1439 | + $this->dispatcher->dispatchTyped(new CachedCalendarObjectDeletedEvent($calendarId, $subscriptionRow, [], $data)); |
|
| 1440 | + } |
|
| 1441 | + } else { |
|
| 1442 | + $pathInfo = pathinfo($data['uri']); |
|
| 1443 | + if (!empty($pathInfo['extension'])) { |
|
| 1444 | + // Append a suffix to "free" the old URI for recreation |
|
| 1445 | + $newUri = sprintf( |
|
| 1446 | + "%s-deleted.%s", |
|
| 1447 | + $pathInfo['filename'], |
|
| 1448 | + $pathInfo['extension'] |
|
| 1449 | + ); |
|
| 1450 | + } else { |
|
| 1451 | + $newUri = sprintf( |
|
| 1452 | + "%s-deleted", |
|
| 1453 | + $pathInfo['filename'] |
|
| 1454 | + ); |
|
| 1455 | + } |
|
| 1456 | + |
|
| 1457 | + // Try to detect conflicts before the DB does |
|
| 1458 | + // As unlikely as it seems, this can happen when the user imports, then deletes, imports and deletes again |
|
| 1459 | + $newObject = $this->getCalendarObject($calendarId, $newUri, $calendarType); |
|
| 1460 | + if ($newObject !== null) { |
|
| 1461 | + throw new Forbidden("A calendar object with URI $newUri already exists in calendar $calendarId, therefore this object can't be moved into the trashbin"); |
|
| 1462 | + } |
|
| 1463 | + |
|
| 1464 | + $qb = $this->db->getQueryBuilder(); |
|
| 1465 | + $markObjectDeletedQuery = $qb->update('calendarobjects') |
|
| 1466 | + ->set('deleted_at', $qb->createNamedParameter(time(), IQueryBuilder::PARAM_INT)) |
|
| 1467 | + ->set('uri', $qb->createNamedParameter($newUri)) |
|
| 1468 | + ->where( |
|
| 1469 | + $qb->expr()->eq('calendarid', $qb->createNamedParameter($calendarId)), |
|
| 1470 | + $qb->expr()->eq('calendartype', $qb->createNamedParameter($calendarType, IQueryBuilder::PARAM_INT), IQueryBuilder::PARAM_INT), |
|
| 1471 | + $qb->expr()->eq('uri', $qb->createNamedParameter($objectUri)) |
|
| 1472 | + ); |
|
| 1473 | + $markObjectDeletedQuery->executeStatement(); |
|
| 1474 | + |
|
| 1475 | + $calendarData = $this->getCalendarById($calendarId); |
|
| 1476 | + if ($calendarData !== null) { |
|
| 1477 | + $this->dispatcher->dispatchTyped( |
|
| 1478 | + new CalendarObjectMovedToTrashEvent( |
|
| 1479 | + $calendarId, |
|
| 1480 | + $calendarData, |
|
| 1481 | + $this->getShares($calendarId), |
|
| 1482 | + $data |
|
| 1483 | + ) |
|
| 1484 | + ); |
|
| 1485 | + } |
|
| 1486 | + } |
|
| 1487 | + |
|
| 1488 | + $this->addChange($calendarId, $objectUri, 3, $calendarType); |
|
| 1489 | + } |
|
| 1490 | + |
|
| 1491 | + /** |
|
| 1492 | + * @param mixed $objectData |
|
| 1493 | + * |
|
| 1494 | + * @throws Forbidden |
|
| 1495 | + */ |
|
| 1496 | + public function restoreCalendarObject(array $objectData): void { |
|
| 1497 | + $id = (int) $objectData['id']; |
|
| 1498 | + $restoreUri = str_replace("-deleted.ics", ".ics", $objectData['uri']); |
|
| 1499 | + $targetObject = $this->getCalendarObject( |
|
| 1500 | + $objectData['calendarid'], |
|
| 1501 | + $restoreUri |
|
| 1502 | + ); |
|
| 1503 | + if ($targetObject !== null) { |
|
| 1504 | + throw new Forbidden("Can not restore calendar $id because a calendar object with the URI $restoreUri already exists"); |
|
| 1505 | + } |
|
| 1506 | + |
|
| 1507 | + $qb = $this->db->getQueryBuilder(); |
|
| 1508 | + $update = $qb->update('calendarobjects') |
|
| 1509 | + ->set('uri', $qb->createNamedParameter($restoreUri)) |
|
| 1510 | + ->set('deleted_at', $qb->createNamedParameter(null)) |
|
| 1511 | + ->where($qb->expr()->eq('id', $qb->createNamedParameter($id, IQueryBuilder::PARAM_INT), IQueryBuilder::PARAM_INT)); |
|
| 1512 | + $update->executeStatement(); |
|
| 1513 | + |
|
| 1514 | + // Make sure this change is tracked in the changes table |
|
| 1515 | + $qb2 = $this->db->getQueryBuilder(); |
|
| 1516 | + $selectObject = $qb2->select('calendardata', 'uri', 'calendarid', 'calendartype') |
|
| 1517 | + ->selectAlias('componenttype', 'component') |
|
| 1518 | + ->from('calendarobjects') |
|
| 1519 | + ->where($qb2->expr()->eq('id', $qb2->createNamedParameter($id, IQueryBuilder::PARAM_INT), IQueryBuilder::PARAM_INT)); |
|
| 1520 | + $result = $selectObject->executeQuery(); |
|
| 1521 | + $row = $result->fetch(); |
|
| 1522 | + $result->closeCursor(); |
|
| 1523 | + if ($row === false) { |
|
| 1524 | + // Welp, this should possibly not have happened, but let's ignore |
|
| 1525 | + return; |
|
| 1526 | + } |
|
| 1527 | + $this->addChange($row['calendarid'], $row['uri'], 1, (int) $row['calendartype']); |
|
| 1528 | + |
|
| 1529 | + $calendarRow = $this->getCalendarById((int) $row['calendarid']); |
|
| 1530 | + if ($calendarRow === null) { |
|
| 1531 | + throw new RuntimeException('Calendar object data that was just written can\'t be read back. Check your database configuration.'); |
|
| 1532 | + } |
|
| 1533 | + $this->dispatcher->dispatchTyped( |
|
| 1534 | + new CalendarObjectRestoredEvent( |
|
| 1535 | + (int) $objectData['calendarid'], |
|
| 1536 | + $calendarRow, |
|
| 1537 | + $this->getShares((int) $row['calendarid']), |
|
| 1538 | + $row |
|
| 1539 | + ) |
|
| 1540 | + ); |
|
| 1541 | + } |
|
| 1542 | + |
|
| 1543 | + /** |
|
| 1544 | + * Performs a calendar-query on the contents of this calendar. |
|
| 1545 | + * |
|
| 1546 | + * The calendar-query is defined in RFC4791 : CalDAV. Using the |
|
| 1547 | + * calendar-query it is possible for a client to request a specific set of |
|
| 1548 | + * object, based on contents of iCalendar properties, date-ranges and |
|
| 1549 | + * iCalendar component types (VTODO, VEVENT). |
|
| 1550 | + * |
|
| 1551 | + * This method should just return a list of (relative) urls that match this |
|
| 1552 | + * query. |
|
| 1553 | + * |
|
| 1554 | + * The list of filters are specified as an array. The exact array is |
|
| 1555 | + * documented by Sabre\CalDAV\CalendarQueryParser. |
|
| 1556 | + * |
|
| 1557 | + * Note that it is extremely likely that getCalendarObject for every path |
|
| 1558 | + * returned from this method will be called almost immediately after. You |
|
| 1559 | + * may want to anticipate this to speed up these requests. |
|
| 1560 | + * |
|
| 1561 | + * This method provides a default implementation, which parses *all* the |
|
| 1562 | + * iCalendar objects in the specified calendar. |
|
| 1563 | + * |
|
| 1564 | + * This default may well be good enough for personal use, and calendars |
|
| 1565 | + * that aren't very large. But if you anticipate high usage, big calendars |
|
| 1566 | + * or high loads, you are strongly advised to optimize certain paths. |
|
| 1567 | + * |
|
| 1568 | + * The best way to do so is override this method and to optimize |
|
| 1569 | + * specifically for 'common filters'. |
|
| 1570 | + * |
|
| 1571 | + * Requests that are extremely common are: |
|
| 1572 | + * * requests for just VEVENTS |
|
| 1573 | + * * requests for just VTODO |
|
| 1574 | + * * requests with a time-range-filter on either VEVENT or VTODO. |
|
| 1575 | + * |
|
| 1576 | + * ..and combinations of these requests. It may not be worth it to try to |
|
| 1577 | + * handle every possible situation and just rely on the (relatively |
|
| 1578 | + * easy to use) CalendarQueryValidator to handle the rest. |
|
| 1579 | + * |
|
| 1580 | + * Note that especially time-range-filters may be difficult to parse. A |
|
| 1581 | + * time-range filter specified on a VEVENT must for instance also handle |
|
| 1582 | + * recurrence rules correctly. |
|
| 1583 | + * A good example of how to interpret all these filters can also simply |
|
| 1584 | + * be found in Sabre\CalDAV\CalendarQueryFilter. This class is as correct |
|
| 1585 | + * as possible, so it gives you a good idea on what type of stuff you need |
|
| 1586 | + * to think of. |
|
| 1587 | + * |
|
| 1588 | + * @param mixed $calendarId |
|
| 1589 | + * @param array $filters |
|
| 1590 | + * @param int $calendarType |
|
| 1591 | + * @return array |
|
| 1592 | + */ |
|
| 1593 | + public function calendarQuery($calendarId, array $filters, $calendarType = self::CALENDAR_TYPE_CALENDAR):array { |
|
| 1594 | + $componentType = null; |
|
| 1595 | + $requirePostFilter = true; |
|
| 1596 | + $timeRange = null; |
|
| 1597 | + |
|
| 1598 | + // if no filters were specified, we don't need to filter after a query |
|
| 1599 | + if (!$filters['prop-filters'] && !$filters['comp-filters']) { |
|
| 1600 | + $requirePostFilter = false; |
|
| 1601 | + } |
|
| 1602 | + |
|
| 1603 | + // Figuring out if there's a component filter |
|
| 1604 | + if (count($filters['comp-filters']) > 0 && !$filters['comp-filters'][0]['is-not-defined']) { |
|
| 1605 | + $componentType = $filters['comp-filters'][0]['name']; |
|
| 1606 | + |
|
| 1607 | + // Checking if we need post-filters |
|
| 1608 | + if (!$filters['prop-filters'] && !$filters['comp-filters'][0]['comp-filters'] && !$filters['comp-filters'][0]['time-range'] && !$filters['comp-filters'][0]['prop-filters']) { |
|
| 1609 | + $requirePostFilter = false; |
|
| 1610 | + } |
|
| 1611 | + // There was a time-range filter |
|
| 1612 | + if ($componentType === 'VEVENT' && isset($filters['comp-filters'][0]['time-range']) && is_array($filters['comp-filters'][0]['time-range'])) { |
|
| 1613 | + $timeRange = $filters['comp-filters'][0]['time-range']; |
|
| 1614 | + |
|
| 1615 | + // If start time OR the end time is not specified, we can do a |
|
| 1616 | + // 100% accurate mysql query. |
|
| 1617 | + if (!$filters['prop-filters'] && !$filters['comp-filters'][0]['comp-filters'] && !$filters['comp-filters'][0]['prop-filters'] && (!$timeRange['start'] || !$timeRange['end'])) { |
|
| 1618 | + $requirePostFilter = false; |
|
| 1619 | + } |
|
| 1620 | + } |
|
| 1621 | + } |
|
| 1622 | + $columns = ['uri']; |
|
| 1623 | + if ($requirePostFilter) { |
|
| 1624 | + $columns = ['uri', 'calendardata']; |
|
| 1625 | + } |
|
| 1626 | + $query = $this->db->getQueryBuilder(); |
|
| 1627 | + $query->select($columns) |
|
| 1628 | + ->from('calendarobjects') |
|
| 1629 | + ->where($query->expr()->eq('calendarid', $query->createNamedParameter($calendarId))) |
|
| 1630 | + ->andWhere($query->expr()->eq('calendartype', $query->createNamedParameter($calendarType))) |
|
| 1631 | + ->andWhere($query->expr()->isNull('deleted_at')); |
|
| 1632 | + |
|
| 1633 | + if ($componentType) { |
|
| 1634 | + $query->andWhere($query->expr()->eq('componenttype', $query->createNamedParameter($componentType))); |
|
| 1635 | + } |
|
| 1636 | + |
|
| 1637 | + if ($timeRange && $timeRange['start']) { |
|
| 1638 | + $query->andWhere($query->expr()->gt('lastoccurence', $query->createNamedParameter($timeRange['start']->getTimeStamp()))); |
|
| 1639 | + } |
|
| 1640 | + if ($timeRange && $timeRange['end']) { |
|
| 1641 | + $query->andWhere($query->expr()->lt('firstoccurence', $query->createNamedParameter($timeRange['end']->getTimeStamp()))); |
|
| 1642 | + } |
|
| 1643 | + |
|
| 1644 | + $stmt = $query->executeQuery(); |
|
| 1645 | + |
|
| 1646 | + $result = []; |
|
| 1647 | + while ($row = $stmt->fetch()) { |
|
| 1648 | + if ($requirePostFilter) { |
|
| 1649 | + // validateFilterForObject will parse the calendar data |
|
| 1650 | + // catch parsing errors |
|
| 1651 | + try { |
|
| 1652 | + $matches = $this->validateFilterForObject($row, $filters); |
|
| 1653 | + } catch (ParseException $ex) { |
|
| 1654 | + $this->logger->error('Caught parsing exception for calendar data. This usually indicates invalid calendar data. calendar-id:'.$calendarId.' uri:'.$row['uri'], [ |
|
| 1655 | + 'app' => 'dav', |
|
| 1656 | + 'exception' => $ex, |
|
| 1657 | + ]); |
|
| 1658 | + continue; |
|
| 1659 | + } catch (InvalidDataException $ex) { |
|
| 1660 | + $this->logger->error('Caught invalid data exception for calendar data. This usually indicates invalid calendar data. calendar-id:'.$calendarId.' uri:'.$row['uri'], [ |
|
| 1661 | + 'app' => 'dav', |
|
| 1662 | + 'exception' => $ex, |
|
| 1663 | + ]); |
|
| 1664 | + continue; |
|
| 1665 | + } |
|
| 1666 | + |
|
| 1667 | + if (!$matches) { |
|
| 1668 | + continue; |
|
| 1669 | + } |
|
| 1670 | + } |
|
| 1671 | + $result[] = $row['uri']; |
|
| 1672 | + } |
|
| 1673 | + |
|
| 1674 | + return $result; |
|
| 1675 | + } |
|
| 1676 | + |
|
| 1677 | + /** |
|
| 1678 | + * custom Nextcloud search extension for CalDAV |
|
| 1679 | + * |
|
| 1680 | + * TODO - this should optionally cover cached calendar objects as well |
|
| 1681 | + * |
|
| 1682 | + * @param string $principalUri |
|
| 1683 | + * @param array $filters |
|
| 1684 | + * @param integer|null $limit |
|
| 1685 | + * @param integer|null $offset |
|
| 1686 | + * @return array |
|
| 1687 | + */ |
|
| 1688 | + public function calendarSearch($principalUri, array $filters, $limit = null, $offset = null) { |
|
| 1689 | + $calendars = $this->getCalendarsForUser($principalUri); |
|
| 1690 | + $ownCalendars = []; |
|
| 1691 | + $sharedCalendars = []; |
|
| 1692 | + |
|
| 1693 | + $uriMapper = []; |
|
| 1694 | + |
|
| 1695 | + foreach ($calendars as $calendar) { |
|
| 1696 | + if ($calendar['{http://owncloud.org/ns}owner-principal'] === $principalUri) { |
|
| 1697 | + $ownCalendars[] = $calendar['id']; |
|
| 1698 | + } else { |
|
| 1699 | + $sharedCalendars[] = $calendar['id']; |
|
| 1700 | + } |
|
| 1701 | + $uriMapper[$calendar['id']] = $calendar['uri']; |
|
| 1702 | + } |
|
| 1703 | + if (count($ownCalendars) === 0 && count($sharedCalendars) === 0) { |
|
| 1704 | + return []; |
|
| 1705 | + } |
|
| 1706 | + |
|
| 1707 | + $query = $this->db->getQueryBuilder(); |
|
| 1708 | + // Calendar id expressions |
|
| 1709 | + $calendarExpressions = []; |
|
| 1710 | + foreach ($ownCalendars as $id) { |
|
| 1711 | + $calendarExpressions[] = $query->expr()->andX( |
|
| 1712 | + $query->expr()->eq('c.calendarid', |
|
| 1713 | + $query->createNamedParameter($id)), |
|
| 1714 | + $query->expr()->eq('c.calendartype', |
|
| 1715 | + $query->createNamedParameter(self::CALENDAR_TYPE_CALENDAR))); |
|
| 1716 | + } |
|
| 1717 | + foreach ($sharedCalendars as $id) { |
|
| 1718 | + $calendarExpressions[] = $query->expr()->andX( |
|
| 1719 | + $query->expr()->eq('c.calendarid', |
|
| 1720 | + $query->createNamedParameter($id)), |
|
| 1721 | + $query->expr()->eq('c.classification', |
|
| 1722 | + $query->createNamedParameter(self::CLASSIFICATION_PUBLIC)), |
|
| 1723 | + $query->expr()->eq('c.calendartype', |
|
| 1724 | + $query->createNamedParameter(self::CALENDAR_TYPE_CALENDAR))); |
|
| 1725 | + } |
|
| 1726 | + |
|
| 1727 | + if (count($calendarExpressions) === 1) { |
|
| 1728 | + $calExpr = $calendarExpressions[0]; |
|
| 1729 | + } else { |
|
| 1730 | + $calExpr = call_user_func_array([$query->expr(), 'orX'], $calendarExpressions); |
|
| 1731 | + } |
|
| 1732 | + |
|
| 1733 | + // Component expressions |
|
| 1734 | + $compExpressions = []; |
|
| 1735 | + foreach ($filters['comps'] as $comp) { |
|
| 1736 | + $compExpressions[] = $query->expr() |
|
| 1737 | + ->eq('c.componenttype', $query->createNamedParameter($comp)); |
|
| 1738 | + } |
|
| 1739 | + |
|
| 1740 | + if (count($compExpressions) === 1) { |
|
| 1741 | + $compExpr = $compExpressions[0]; |
|
| 1742 | + } else { |
|
| 1743 | + $compExpr = call_user_func_array([$query->expr(), 'orX'], $compExpressions); |
|
| 1744 | + } |
|
| 1745 | + |
|
| 1746 | + if (!isset($filters['props'])) { |
|
| 1747 | + $filters['props'] = []; |
|
| 1748 | + } |
|
| 1749 | + if (!isset($filters['params'])) { |
|
| 1750 | + $filters['params'] = []; |
|
| 1751 | + } |
|
| 1752 | + |
|
| 1753 | + $propParamExpressions = []; |
|
| 1754 | + foreach ($filters['props'] as $prop) { |
|
| 1755 | + $propParamExpressions[] = $query->expr()->andX( |
|
| 1756 | + $query->expr()->eq('i.name', $query->createNamedParameter($prop)), |
|
| 1757 | + $query->expr()->isNull('i.parameter') |
|
| 1758 | + ); |
|
| 1759 | + } |
|
| 1760 | + foreach ($filters['params'] as $param) { |
|
| 1761 | + $propParamExpressions[] = $query->expr()->andX( |
|
| 1762 | + $query->expr()->eq('i.name', $query->createNamedParameter($param['property'])), |
|
| 1763 | + $query->expr()->eq('i.parameter', $query->createNamedParameter($param['parameter'])) |
|
| 1764 | + ); |
|
| 1765 | + } |
|
| 1766 | + |
|
| 1767 | + if (count($propParamExpressions) === 1) { |
|
| 1768 | + $propParamExpr = $propParamExpressions[0]; |
|
| 1769 | + } else { |
|
| 1770 | + $propParamExpr = call_user_func_array([$query->expr(), 'orX'], $propParamExpressions); |
|
| 1771 | + } |
|
| 1772 | + |
|
| 1773 | + $query->select(['c.calendarid', 'c.uri']) |
|
| 1774 | + ->from($this->dbObjectPropertiesTable, 'i') |
|
| 1775 | + ->join('i', 'calendarobjects', 'c', $query->expr()->eq('i.objectid', 'c.id')) |
|
| 1776 | + ->where($calExpr) |
|
| 1777 | + ->andWhere($compExpr) |
|
| 1778 | + ->andWhere($propParamExpr) |
|
| 1779 | + ->andWhere($query->expr()->iLike('i.value', |
|
| 1780 | + $query->createNamedParameter('%'.$this->db->escapeLikeParameter($filters['search-term']).'%'))) |
|
| 1781 | + ->andWhere($query->expr()->isNull('deleted_at')); |
|
| 1782 | + |
|
| 1783 | + if ($offset) { |
|
| 1784 | + $query->setFirstResult($offset); |
|
| 1785 | + } |
|
| 1786 | + if ($limit) { |
|
| 1787 | + $query->setMaxResults($limit); |
|
| 1788 | + } |
|
| 1789 | + |
|
| 1790 | + $stmt = $query->executeQuery(); |
|
| 1791 | + |
|
| 1792 | + $result = []; |
|
| 1793 | + while ($row = $stmt->fetch()) { |
|
| 1794 | + $path = $uriMapper[$row['calendarid']] . '/' . $row['uri']; |
|
| 1795 | + if (!in_array($path, $result)) { |
|
| 1796 | + $result[] = $path; |
|
| 1797 | + } |
|
| 1798 | + } |
|
| 1799 | + |
|
| 1800 | + return $result; |
|
| 1801 | + } |
|
| 1802 | + |
|
| 1803 | + /** |
|
| 1804 | + * used for Nextcloud's calendar API |
|
| 1805 | + * |
|
| 1806 | + * @param array $calendarInfo |
|
| 1807 | + * @param string $pattern |
|
| 1808 | + * @param array $searchProperties |
|
| 1809 | + * @param array $options |
|
| 1810 | + * @param integer|null $limit |
|
| 1811 | + * @param integer|null $offset |
|
| 1812 | + * |
|
| 1813 | + * @return array |
|
| 1814 | + */ |
|
| 1815 | + public function search(array $calendarInfo, $pattern, array $searchProperties, |
|
| 1816 | + array $options, $limit, $offset) { |
|
| 1817 | + $outerQuery = $this->db->getQueryBuilder(); |
|
| 1818 | + $innerQuery = $this->db->getQueryBuilder(); |
|
| 1819 | + |
|
| 1820 | + $innerQuery->selectDistinct('op.objectid') |
|
| 1821 | + ->from($this->dbObjectPropertiesTable, 'op') |
|
| 1822 | + ->andWhere($innerQuery->expr()->eq('op.calendarid', |
|
| 1823 | + $outerQuery->createNamedParameter($calendarInfo['id']))) |
|
| 1824 | + ->andWhere($innerQuery->expr()->eq('op.calendartype', |
|
| 1825 | + $outerQuery->createNamedParameter(self::CALENDAR_TYPE_CALENDAR))); |
|
| 1826 | + |
|
| 1827 | + // only return public items for shared calendars for now |
|
| 1828 | + if (isset($calendarInfo['{http://owncloud.org/ns}owner-principal']) === false || $calendarInfo['principaluri'] !== $calendarInfo['{http://owncloud.org/ns}owner-principal']) { |
|
| 1829 | + $innerQuery->andWhere($innerQuery->expr()->eq('c.classification', |
|
| 1830 | + $outerQuery->createNamedParameter(self::CLASSIFICATION_PUBLIC))); |
|
| 1831 | + } |
|
| 1832 | + |
|
| 1833 | + if (!empty($searchProperties)) { |
|
| 1834 | + $or = $innerQuery->expr()->orX(); |
|
| 1835 | + foreach ($searchProperties as $searchProperty) { |
|
| 1836 | + $or->add($innerQuery->expr()->eq('op.name', |
|
| 1837 | + $outerQuery->createNamedParameter($searchProperty))); |
|
| 1838 | + } |
|
| 1839 | + $innerQuery->andWhere($or); |
|
| 1840 | + } |
|
| 1841 | + |
|
| 1842 | + if ($pattern !== '') { |
|
| 1843 | + $innerQuery->andWhere($innerQuery->expr()->iLike('op.value', |
|
| 1844 | + $outerQuery->createNamedParameter('%' . |
|
| 1845 | + $this->db->escapeLikeParameter($pattern) . '%'))); |
|
| 1846 | + } |
|
| 1847 | + |
|
| 1848 | + $outerQuery->select('c.id', 'c.calendardata', 'c.componenttype', 'c.uid', 'c.uri') |
|
| 1849 | + ->from('calendarobjects', 'c') |
|
| 1850 | + ->where($outerQuery->expr()->isNull('deleted_at')); |
|
| 1851 | + |
|
| 1852 | + if (isset($options['timerange'])) { |
|
| 1853 | + if (isset($options['timerange']['start']) && $options['timerange']['start'] instanceof DateTimeInterface) { |
|
| 1854 | + $outerQuery->andWhere($outerQuery->expr()->gt('lastoccurence', |
|
| 1855 | + $outerQuery->createNamedParameter($options['timerange']['start']->getTimeStamp()))); |
|
| 1856 | + } |
|
| 1857 | + if (isset($options['timerange']['end']) && $options['timerange']['end'] instanceof DateTimeInterface) { |
|
| 1858 | + $outerQuery->andWhere($outerQuery->expr()->lt('firstoccurence', |
|
| 1859 | + $outerQuery->createNamedParameter($options['timerange']['end']->getTimeStamp()))); |
|
| 1860 | + } |
|
| 1861 | + } |
|
| 1862 | + |
|
| 1863 | + if(isset($options['uid'])) { |
|
| 1864 | + $outerQuery->andWhere($outerQuery->expr()->eq('uid', $outerQuery->createNamedParameter($options['uid']))); |
|
| 1865 | + } |
|
| 1866 | + |
|
| 1867 | + if (!empty($options['types'])) { |
|
| 1868 | + $or = $outerQuery->expr()->orX(); |
|
| 1869 | + foreach ($options['types'] as $type) { |
|
| 1870 | + $or->add($outerQuery->expr()->eq('componenttype', |
|
| 1871 | + $outerQuery->createNamedParameter($type))); |
|
| 1872 | + } |
|
| 1873 | + $outerQuery->andWhere($or); |
|
| 1874 | + } |
|
| 1875 | + |
|
| 1876 | + $outerQuery->andWhere($outerQuery->expr()->in('c.id', $outerQuery->createFunction($innerQuery->getSQL()))); |
|
| 1877 | + |
|
| 1878 | + if ($offset) { |
|
| 1879 | + $outerQuery->setFirstResult($offset); |
|
| 1880 | + } |
|
| 1881 | + if ($limit) { |
|
| 1882 | + $outerQuery->setMaxResults($limit); |
|
| 1883 | + } |
|
| 1884 | + |
|
| 1885 | + $result = $outerQuery->executeQuery(); |
|
| 1886 | + $calendarObjects = array_filter($result->fetchAll(), function (array $row) use ($options) { |
|
| 1887 | + $start = $options['timerange']['start'] ?? null; |
|
| 1888 | + $end = $options['timerange']['end'] ?? null; |
|
| 1889 | + |
|
| 1890 | + if ($start === null || !($start instanceof DateTimeInterface) || $end === null || !($end instanceof DateTimeInterface)) { |
|
| 1891 | + // No filter required |
|
| 1892 | + return true; |
|
| 1893 | + } |
|
| 1894 | + |
|
| 1895 | + $isValid = $this->validateFilterForObject($row, [ |
|
| 1896 | + 'name' => 'VCALENDAR', |
|
| 1897 | + 'comp-filters' => [ |
|
| 1898 | + [ |
|
| 1899 | + 'name' => 'VEVENT', |
|
| 1900 | + 'comp-filters' => [], |
|
| 1901 | + 'prop-filters' => [], |
|
| 1902 | + 'is-not-defined' => false, |
|
| 1903 | + 'time-range' => [ |
|
| 1904 | + 'start' => $start, |
|
| 1905 | + 'end' => $end, |
|
| 1906 | + ], |
|
| 1907 | + ], |
|
| 1908 | + ], |
|
| 1909 | + 'prop-filters' => [], |
|
| 1910 | + 'is-not-defined' => false, |
|
| 1911 | + 'time-range' => null, |
|
| 1912 | + ]); |
|
| 1913 | + if (is_resource($row['calendardata'])) { |
|
| 1914 | + // Put the stream back to the beginning so it can be read another time |
|
| 1915 | + rewind($row['calendardata']); |
|
| 1916 | + } |
|
| 1917 | + return $isValid; |
|
| 1918 | + }); |
|
| 1919 | + $result->closeCursor(); |
|
| 1920 | + |
|
| 1921 | + return array_map(function ($o) { |
|
| 1922 | + $calendarData = Reader::read($o['calendardata']); |
|
| 1923 | + $comps = $calendarData->getComponents(); |
|
| 1924 | + $objects = []; |
|
| 1925 | + $timezones = []; |
|
| 1926 | + foreach ($comps as $comp) { |
|
| 1927 | + if ($comp instanceof VTimeZone) { |
|
| 1928 | + $timezones[] = $comp; |
|
| 1929 | + } else { |
|
| 1930 | + $objects[] = $comp; |
|
| 1931 | + } |
|
| 1932 | + } |
|
| 1933 | + |
|
| 1934 | + return [ |
|
| 1935 | + 'id' => $o['id'], |
|
| 1936 | + 'type' => $o['componenttype'], |
|
| 1937 | + 'uid' => $o['uid'], |
|
| 1938 | + 'uri' => $o['uri'], |
|
| 1939 | + 'objects' => array_map(function ($c) { |
|
| 1940 | + return $this->transformSearchData($c); |
|
| 1941 | + }, $objects), |
|
| 1942 | + 'timezones' => array_map(function ($c) { |
|
| 1943 | + return $this->transformSearchData($c); |
|
| 1944 | + }, $timezones), |
|
| 1945 | + ]; |
|
| 1946 | + }, $calendarObjects); |
|
| 1947 | + } |
|
| 1948 | + |
|
| 1949 | + /** |
|
| 1950 | + * @param Component $comp |
|
| 1951 | + * @return array |
|
| 1952 | + */ |
|
| 1953 | + private function transformSearchData(Component $comp) { |
|
| 1954 | + $data = []; |
|
| 1955 | + /** @var Component[] $subComponents */ |
|
| 1956 | + $subComponents = $comp->getComponents(); |
|
| 1957 | + /** @var Property[] $properties */ |
|
| 1958 | + $properties = array_filter($comp->children(), function ($c) { |
|
| 1959 | + return $c instanceof Property; |
|
| 1960 | + }); |
|
| 1961 | + $validationRules = $comp->getValidationRules(); |
|
| 1962 | + |
|
| 1963 | + foreach ($subComponents as $subComponent) { |
|
| 1964 | + $name = $subComponent->name; |
|
| 1965 | + if (!isset($data[$name])) { |
|
| 1966 | + $data[$name] = []; |
|
| 1967 | + } |
|
| 1968 | + $data[$name][] = $this->transformSearchData($subComponent); |
|
| 1969 | + } |
|
| 1970 | + |
|
| 1971 | + foreach ($properties as $property) { |
|
| 1972 | + $name = $property->name; |
|
| 1973 | + if (!isset($validationRules[$name])) { |
|
| 1974 | + $validationRules[$name] = '*'; |
|
| 1975 | + } |
|
| 1976 | + |
|
| 1977 | + $rule = $validationRules[$property->name]; |
|
| 1978 | + if ($rule === '+' || $rule === '*') { // multiple |
|
| 1979 | + if (!isset($data[$name])) { |
|
| 1980 | + $data[$name] = []; |
|
| 1981 | + } |
|
| 1982 | + |
|
| 1983 | + $data[$name][] = $this->transformSearchProperty($property); |
|
| 1984 | + } else { // once |
|
| 1985 | + $data[$name] = $this->transformSearchProperty($property); |
|
| 1986 | + } |
|
| 1987 | + } |
|
| 1988 | + |
|
| 1989 | + return $data; |
|
| 1990 | + } |
|
| 1991 | + |
|
| 1992 | + /** |
|
| 1993 | + * @param Property $prop |
|
| 1994 | + * @return array |
|
| 1995 | + */ |
|
| 1996 | + private function transformSearchProperty(Property $prop) { |
|
| 1997 | + // No need to check Date, as it extends DateTime |
|
| 1998 | + if ($prop instanceof Property\ICalendar\DateTime) { |
|
| 1999 | + $value = $prop->getDateTime(); |
|
| 2000 | + } else { |
|
| 2001 | + $value = $prop->getValue(); |
|
| 2002 | + } |
|
| 2003 | + |
|
| 2004 | + return [ |
|
| 2005 | + $value, |
|
| 2006 | + $prop->parameters() |
|
| 2007 | + ]; |
|
| 2008 | + } |
|
| 2009 | + |
|
| 2010 | + /** |
|
| 2011 | + * @param string $principalUri |
|
| 2012 | + * @param string $pattern |
|
| 2013 | + * @param array $componentTypes |
|
| 2014 | + * @param array $searchProperties |
|
| 2015 | + * @param array $searchParameters |
|
| 2016 | + * @param array $options |
|
| 2017 | + * @return array |
|
| 2018 | + */ |
|
| 2019 | + public function searchPrincipalUri(string $principalUri, |
|
| 2020 | + string $pattern, |
|
| 2021 | + array $componentTypes, |
|
| 2022 | + array $searchProperties, |
|
| 2023 | + array $searchParameters, |
|
| 2024 | + array $options = []): array { |
|
| 2025 | + $escapePattern = !\array_key_exists('escape_like_param', $options) || $options['escape_like_param'] !== false; |
|
| 2026 | + |
|
| 2027 | + $calendarObjectIdQuery = $this->db->getQueryBuilder(); |
|
| 2028 | + $calendarOr = $calendarObjectIdQuery->expr()->orX(); |
|
| 2029 | + $searchOr = $calendarObjectIdQuery->expr()->orX(); |
|
| 2030 | + |
|
| 2031 | + // Fetch calendars and subscription |
|
| 2032 | + $calendars = $this->getCalendarsForUser($principalUri); |
|
| 2033 | + $subscriptions = $this->getSubscriptionsForUser($principalUri); |
|
| 2034 | + foreach ($calendars as $calendar) { |
|
| 2035 | + $calendarAnd = $calendarObjectIdQuery->expr()->andX(); |
|
| 2036 | + $calendarAnd->add($calendarObjectIdQuery->expr()->eq('cob.calendarid', $calendarObjectIdQuery->createNamedParameter((int)$calendar['id']))); |
|
| 2037 | + $calendarAnd->add($calendarObjectIdQuery->expr()->eq('cob.calendartype', $calendarObjectIdQuery->createNamedParameter(self::CALENDAR_TYPE_CALENDAR))); |
|
| 2038 | + |
|
| 2039 | + // If it's shared, limit search to public events |
|
| 2040 | + if (isset($calendar['{http://owncloud.org/ns}owner-principal']) |
|
| 2041 | + && $calendar['principaluri'] !== $calendar['{http://owncloud.org/ns}owner-principal']) { |
|
| 2042 | + $calendarAnd->add($calendarObjectIdQuery->expr()->eq('co.classification', $calendarObjectIdQuery->createNamedParameter(self::CLASSIFICATION_PUBLIC))); |
|
| 2043 | + } |
|
| 2044 | + |
|
| 2045 | + $calendarOr->add($calendarAnd); |
|
| 2046 | + } |
|
| 2047 | + foreach ($subscriptions as $subscription) { |
|
| 2048 | + $subscriptionAnd = $calendarObjectIdQuery->expr()->andX(); |
|
| 2049 | + $subscriptionAnd->add($calendarObjectIdQuery->expr()->eq('cob.calendarid', $calendarObjectIdQuery->createNamedParameter((int)$subscription['id']))); |
|
| 2050 | + $subscriptionAnd->add($calendarObjectIdQuery->expr()->eq('cob.calendartype', $calendarObjectIdQuery->createNamedParameter(self::CALENDAR_TYPE_SUBSCRIPTION))); |
|
| 2051 | + |
|
| 2052 | + // If it's shared, limit search to public events |
|
| 2053 | + if (isset($subscription['{http://owncloud.org/ns}owner-principal']) |
|
| 2054 | + && $subscription['principaluri'] !== $subscription['{http://owncloud.org/ns}owner-principal']) { |
|
| 2055 | + $subscriptionAnd->add($calendarObjectIdQuery->expr()->eq('co.classification', $calendarObjectIdQuery->createNamedParameter(self::CLASSIFICATION_PUBLIC))); |
|
| 2056 | + } |
|
| 2057 | + |
|
| 2058 | + $calendarOr->add($subscriptionAnd); |
|
| 2059 | + } |
|
| 2060 | + |
|
| 2061 | + foreach ($searchProperties as $property) { |
|
| 2062 | + $propertyAnd = $calendarObjectIdQuery->expr()->andX(); |
|
| 2063 | + $propertyAnd->add($calendarObjectIdQuery->expr()->eq('cob.name', $calendarObjectIdQuery->createNamedParameter($property, IQueryBuilder::PARAM_STR))); |
|
| 2064 | + $propertyAnd->add($calendarObjectIdQuery->expr()->isNull('cob.parameter')); |
|
| 2065 | + |
|
| 2066 | + $searchOr->add($propertyAnd); |
|
| 2067 | + } |
|
| 2068 | + foreach ($searchParameters as $property => $parameter) { |
|
| 2069 | + $parameterAnd = $calendarObjectIdQuery->expr()->andX(); |
|
| 2070 | + $parameterAnd->add($calendarObjectIdQuery->expr()->eq('cob.name', $calendarObjectIdQuery->createNamedParameter($property, IQueryBuilder::PARAM_STR))); |
|
| 2071 | + $parameterAnd->add($calendarObjectIdQuery->expr()->eq('cob.parameter', $calendarObjectIdQuery->createNamedParameter($parameter, IQueryBuilder::PARAM_STR_ARRAY))); |
|
| 2072 | + |
|
| 2073 | + $searchOr->add($parameterAnd); |
|
| 2074 | + } |
|
| 2075 | + |
|
| 2076 | + if ($calendarOr->count() === 0) { |
|
| 2077 | + return []; |
|
| 2078 | + } |
|
| 2079 | + if ($searchOr->count() === 0) { |
|
| 2080 | + return []; |
|
| 2081 | + } |
|
| 2082 | + |
|
| 2083 | + $calendarObjectIdQuery->selectDistinct('cob.objectid') |
|
| 2084 | + ->from($this->dbObjectPropertiesTable, 'cob') |
|
| 2085 | + ->leftJoin('cob', 'calendarobjects', 'co', $calendarObjectIdQuery->expr()->eq('co.id', 'cob.objectid')) |
|
| 2086 | + ->andWhere($calendarObjectIdQuery->expr()->in('co.componenttype', $calendarObjectIdQuery->createNamedParameter($componentTypes, IQueryBuilder::PARAM_STR_ARRAY))) |
|
| 2087 | + ->andWhere($calendarOr) |
|
| 2088 | + ->andWhere($searchOr) |
|
| 2089 | + ->andWhere($calendarObjectIdQuery->expr()->isNull('deleted_at')); |
|
| 2090 | + |
|
| 2091 | + if ('' !== $pattern) { |
|
| 2092 | + if (!$escapePattern) { |
|
| 2093 | + $calendarObjectIdQuery->andWhere($calendarObjectIdQuery->expr()->ilike('cob.value', $calendarObjectIdQuery->createNamedParameter($pattern))); |
|
| 2094 | + } else { |
|
| 2095 | + $calendarObjectIdQuery->andWhere($calendarObjectIdQuery->expr()->ilike('cob.value', $calendarObjectIdQuery->createNamedParameter('%' . $this->db->escapeLikeParameter($pattern) . '%'))); |
|
| 2096 | + } |
|
| 2097 | + } |
|
| 2098 | + |
|
| 2099 | + if (isset($options['limit'])) { |
|
| 2100 | + $calendarObjectIdQuery->setMaxResults($options['limit']); |
|
| 2101 | + } |
|
| 2102 | + if (isset($options['offset'])) { |
|
| 2103 | + $calendarObjectIdQuery->setFirstResult($options['offset']); |
|
| 2104 | + } |
|
| 2105 | + |
|
| 2106 | + $result = $calendarObjectIdQuery->executeQuery(); |
|
| 2107 | + $matches = $result->fetchAll(); |
|
| 2108 | + $result->closeCursor(); |
|
| 2109 | + $matches = array_map(static function (array $match):int { |
|
| 2110 | + return (int) $match['objectid']; |
|
| 2111 | + }, $matches); |
|
| 2112 | + |
|
| 2113 | + $query = $this->db->getQueryBuilder(); |
|
| 2114 | + $query->select('calendardata', 'uri', 'calendarid', 'calendartype') |
|
| 2115 | + ->from('calendarobjects') |
|
| 2116 | + ->where($query->expr()->in('id', $query->createNamedParameter($matches, IQueryBuilder::PARAM_INT_ARRAY))); |
|
| 2117 | + |
|
| 2118 | + $result = $query->executeQuery(); |
|
| 2119 | + $calendarObjects = $result->fetchAll(); |
|
| 2120 | + $result->closeCursor(); |
|
| 2121 | + |
|
| 2122 | + return array_map(function (array $array): array { |
|
| 2123 | + $array['calendarid'] = (int)$array['calendarid']; |
|
| 2124 | + $array['calendartype'] = (int)$array['calendartype']; |
|
| 2125 | + $array['calendardata'] = $this->readBlob($array['calendardata']); |
|
| 2126 | + |
|
| 2127 | + return $array; |
|
| 2128 | + }, $calendarObjects); |
|
| 2129 | + } |
|
| 2130 | + |
|
| 2131 | + /** |
|
| 2132 | + * Searches through all of a users calendars and calendar objects to find |
|
| 2133 | + * an object with a specific UID. |
|
| 2134 | + * |
|
| 2135 | + * This method should return the path to this object, relative to the |
|
| 2136 | + * calendar home, so this path usually only contains two parts: |
|
| 2137 | + * |
|
| 2138 | + * calendarpath/objectpath.ics |
|
| 2139 | + * |
|
| 2140 | + * If the uid is not found, return null. |
|
| 2141 | + * |
|
| 2142 | + * This method should only consider * objects that the principal owns, so |
|
| 2143 | + * any calendars owned by other principals that also appear in this |
|
| 2144 | + * collection should be ignored. |
|
| 2145 | + * |
|
| 2146 | + * @param string $principalUri |
|
| 2147 | + * @param string $uid |
|
| 2148 | + * @return string|null |
|
| 2149 | + */ |
|
| 2150 | + public function getCalendarObjectByUID($principalUri, $uid) { |
|
| 2151 | + $query = $this->db->getQueryBuilder(); |
|
| 2152 | + $query->selectAlias('c.uri', 'calendaruri')->selectAlias('co.uri', 'objecturi') |
|
| 2153 | + ->from('calendarobjects', 'co') |
|
| 2154 | + ->leftJoin('co', 'calendars', 'c', $query->expr()->eq('co.calendarid', 'c.id')) |
|
| 2155 | + ->where($query->expr()->eq('c.principaluri', $query->createNamedParameter($principalUri))) |
|
| 2156 | + ->andWhere($query->expr()->eq('co.uid', $query->createNamedParameter($uid))) |
|
| 2157 | + ->andWhere($query->expr()->isNull('co.deleted_at')); |
|
| 2158 | + $stmt = $query->executeQuery(); |
|
| 2159 | + $row = $stmt->fetch(); |
|
| 2160 | + $stmt->closeCursor(); |
|
| 2161 | + if ($row) { |
|
| 2162 | + return $row['calendaruri'] . '/' . $row['objecturi']; |
|
| 2163 | + } |
|
| 2164 | + |
|
| 2165 | + return null; |
|
| 2166 | + } |
|
| 2167 | + |
|
| 2168 | + public function getCalendarObjectById(string $principalUri, int $id): ?array { |
|
| 2169 | + $query = $this->db->getQueryBuilder(); |
|
| 2170 | + $query->select(['co.id', 'co.uri', 'co.lastmodified', 'co.etag', 'co.calendarid', 'co.size', 'co.calendardata', 'co.componenttype', 'co.classification', 'co.deleted_at']) |
|
| 2171 | + ->selectAlias('c.uri', 'calendaruri') |
|
| 2172 | + ->from('calendarobjects', 'co') |
|
| 2173 | + ->join('co', 'calendars', 'c', $query->expr()->eq('c.id', 'co.calendarid', IQueryBuilder::PARAM_INT)) |
|
| 2174 | + ->where($query->expr()->eq('c.principaluri', $query->createNamedParameter($principalUri))) |
|
| 2175 | + ->andWhere($query->expr()->eq('co.id', $query->createNamedParameter($id, IQueryBuilder::PARAM_INT), IQueryBuilder::PARAM_INT)); |
|
| 2176 | + $stmt = $query->executeQuery(); |
|
| 2177 | + $row = $stmt->fetch(); |
|
| 2178 | + $stmt->closeCursor(); |
|
| 2179 | + |
|
| 2180 | + if (!$row) { |
|
| 2181 | + return null; |
|
| 2182 | + } |
|
| 2183 | + |
|
| 2184 | + return [ |
|
| 2185 | + 'id' => $row['id'], |
|
| 2186 | + 'uri' => $row['uri'], |
|
| 2187 | + 'lastmodified' => $row['lastmodified'], |
|
| 2188 | + 'etag' => '"' . $row['etag'] . '"', |
|
| 2189 | + 'calendarid' => $row['calendarid'], |
|
| 2190 | + 'calendaruri' => $row['calendaruri'], |
|
| 2191 | + 'size' => (int)$row['size'], |
|
| 2192 | + 'calendardata' => $this->readBlob($row['calendardata']), |
|
| 2193 | + 'component' => strtolower($row['componenttype']), |
|
| 2194 | + 'classification' => (int)$row['classification'], |
|
| 2195 | + 'deleted_at' => isset($row['deleted_at']) ? ((int) $row['deleted_at']) : null, |
|
| 2196 | + ]; |
|
| 2197 | + } |
|
| 2198 | + |
|
| 2199 | + /** |
|
| 2200 | + * The getChanges method returns all the changes that have happened, since |
|
| 2201 | + * the specified syncToken in the specified calendar. |
|
| 2202 | + * |
|
| 2203 | + * This function should return an array, such as the following: |
|
| 2204 | + * |
|
| 2205 | + * [ |
|
| 2206 | + * 'syncToken' => 'The current synctoken', |
|
| 2207 | + * 'added' => [ |
|
| 2208 | + * 'new.txt', |
|
| 2209 | + * ], |
|
| 2210 | + * 'modified' => [ |
|
| 2211 | + * 'modified.txt', |
|
| 2212 | + * ], |
|
| 2213 | + * 'deleted' => [ |
|
| 2214 | + * 'foo.php.bak', |
|
| 2215 | + * 'old.txt' |
|
| 2216 | + * ] |
|
| 2217 | + * ); |
|
| 2218 | + * |
|
| 2219 | + * The returned syncToken property should reflect the *current* syncToken |
|
| 2220 | + * of the calendar, as reported in the {http://sabredav.org/ns}sync-token |
|
| 2221 | + * property This is * needed here too, to ensure the operation is atomic. |
|
| 2222 | + * |
|
| 2223 | + * If the $syncToken argument is specified as null, this is an initial |
|
| 2224 | + * sync, and all members should be reported. |
|
| 2225 | + * |
|
| 2226 | + * The modified property is an array of nodenames that have changed since |
|
| 2227 | + * the last token. |
|
| 2228 | + * |
|
| 2229 | + * The deleted property is an array with nodenames, that have been deleted |
|
| 2230 | + * from collection. |
|
| 2231 | + * |
|
| 2232 | + * The $syncLevel argument is basically the 'depth' of the report. If it's |
|
| 2233 | + * 1, you only have to report changes that happened only directly in |
|
| 2234 | + * immediate descendants. If it's 2, it should also include changes from |
|
| 2235 | + * the nodes below the child collections. (grandchildren) |
|
| 2236 | + * |
|
| 2237 | + * The $limit argument allows a client to specify how many results should |
|
| 2238 | + * be returned at most. If the limit is not specified, it should be treated |
|
| 2239 | + * as infinite. |
|
| 2240 | + * |
|
| 2241 | + * If the limit (infinite or not) is higher than you're willing to return, |
|
| 2242 | + * you should throw a Sabre\DAV\Exception\TooMuchMatches() exception. |
|
| 2243 | + * |
|
| 2244 | + * If the syncToken is expired (due to data cleanup) or unknown, you must |
|
| 2245 | + * return null. |
|
| 2246 | + * |
|
| 2247 | + * The limit is 'suggestive'. You are free to ignore it. |
|
| 2248 | + * |
|
| 2249 | + * @param string $calendarId |
|
| 2250 | + * @param string $syncToken |
|
| 2251 | + * @param int $syncLevel |
|
| 2252 | + * @param int|null $limit |
|
| 2253 | + * @param int $calendarType |
|
| 2254 | + * @return array |
|
| 2255 | + */ |
|
| 2256 | + public function getChangesForCalendar($calendarId, $syncToken, $syncLevel, $limit = null, $calendarType = self::CALENDAR_TYPE_CALENDAR) { |
|
| 2257 | + // Current synctoken |
|
| 2258 | + $qb = $this->db->getQueryBuilder(); |
|
| 2259 | + $qb->select('synctoken') |
|
| 2260 | + ->from('calendars') |
|
| 2261 | + ->where( |
|
| 2262 | + $qb->expr()->eq('id', $qb->createNamedParameter($calendarId)) |
|
| 2263 | + ); |
|
| 2264 | + $stmt = $qb->executeQuery(); |
|
| 2265 | + $currentToken = $stmt->fetchOne(); |
|
| 2266 | + |
|
| 2267 | + if ($currentToken === false) { |
|
| 2268 | + return null; |
|
| 2269 | + } |
|
| 2270 | + |
|
| 2271 | + $result = [ |
|
| 2272 | + 'syncToken' => $currentToken, |
|
| 2273 | + 'added' => [], |
|
| 2274 | + 'modified' => [], |
|
| 2275 | + 'deleted' => [], |
|
| 2276 | + ]; |
|
| 2277 | + |
|
| 2278 | + if ($syncToken) { |
|
| 2279 | + $qb = $this->db->getQueryBuilder(); |
|
| 2280 | + |
|
| 2281 | + $qb->select('uri', 'operation') |
|
| 2282 | + ->from('calendarchanges') |
|
| 2283 | + ->where( |
|
| 2284 | + $qb->expr()->andX( |
|
| 2285 | + $qb->expr()->gte('synctoken', $qb->createNamedParameter($syncToken)), |
|
| 2286 | + $qb->expr()->lt('synctoken', $qb->createNamedParameter($currentToken)), |
|
| 2287 | + $qb->expr()->eq('calendarid', $qb->createNamedParameter($calendarId)), |
|
| 2288 | + $qb->expr()->eq('calendartype', $qb->createNamedParameter($calendarType)) |
|
| 2289 | + ) |
|
| 2290 | + )->orderBy('synctoken'); |
|
| 2291 | + if (is_int($limit) && $limit > 0) { |
|
| 2292 | + $qb->setMaxResults($limit); |
|
| 2293 | + } |
|
| 2294 | + |
|
| 2295 | + // Fetching all changes |
|
| 2296 | + $stmt = $qb->executeQuery(); |
|
| 2297 | + $changes = []; |
|
| 2298 | + |
|
| 2299 | + // This loop ensures that any duplicates are overwritten, only the |
|
| 2300 | + // last change on a node is relevant. |
|
| 2301 | + while ($row = $stmt->fetch()) { |
|
| 2302 | + $changes[$row['uri']] = $row['operation']; |
|
| 2303 | + } |
|
| 2304 | + $stmt->closeCursor(); |
|
| 2305 | + |
|
| 2306 | + foreach ($changes as $uri => $operation) { |
|
| 2307 | + switch ($operation) { |
|
| 2308 | + case 1: |
|
| 2309 | + $result['added'][] = $uri; |
|
| 2310 | + break; |
|
| 2311 | + case 2: |
|
| 2312 | + $result['modified'][] = $uri; |
|
| 2313 | + break; |
|
| 2314 | + case 3: |
|
| 2315 | + $result['deleted'][] = $uri; |
|
| 2316 | + break; |
|
| 2317 | + } |
|
| 2318 | + } |
|
| 2319 | + } else { |
|
| 2320 | + // No synctoken supplied, this is the initial sync. |
|
| 2321 | + $qb = $this->db->getQueryBuilder(); |
|
| 2322 | + $qb->select('uri') |
|
| 2323 | + ->from('calendarobjects') |
|
| 2324 | + ->where( |
|
| 2325 | + $qb->expr()->andX( |
|
| 2326 | + $qb->expr()->eq('calendarid', $qb->createNamedParameter($calendarId)), |
|
| 2327 | + $qb->expr()->eq('calendartype', $qb->createNamedParameter($calendarType)) |
|
| 2328 | + ) |
|
| 2329 | + ); |
|
| 2330 | + $stmt = $qb->executeQuery(); |
|
| 2331 | + $result['added'] = $stmt->fetchAll(\PDO::FETCH_COLUMN); |
|
| 2332 | + $stmt->closeCursor(); |
|
| 2333 | + } |
|
| 2334 | + return $result; |
|
| 2335 | + } |
|
| 2336 | + |
|
| 2337 | + /** |
|
| 2338 | + * Returns a list of subscriptions for a principal. |
|
| 2339 | + * |
|
| 2340 | + * Every subscription is an array with the following keys: |
|
| 2341 | + * * id, a unique id that will be used by other functions to modify the |
|
| 2342 | + * subscription. This can be the same as the uri or a database key. |
|
| 2343 | + * * uri. This is just the 'base uri' or 'filename' of the subscription. |
|
| 2344 | + * * principaluri. The owner of the subscription. Almost always the same as |
|
| 2345 | + * principalUri passed to this method. |
|
| 2346 | + * |
|
| 2347 | + * Furthermore, all the subscription info must be returned too: |
|
| 2348 | + * |
|
| 2349 | + * 1. {DAV:}displayname |
|
| 2350 | + * 2. {http://apple.com/ns/ical/}refreshrate |
|
| 2351 | + * 3. {http://calendarserver.org/ns/}subscribed-strip-todos (omit if todos |
|
| 2352 | + * should not be stripped). |
|
| 2353 | + * 4. {http://calendarserver.org/ns/}subscribed-strip-alarms (omit if alarms |
|
| 2354 | + * should not be stripped). |
|
| 2355 | + * 5. {http://calendarserver.org/ns/}subscribed-strip-attachments (omit if |
|
| 2356 | + * attachments should not be stripped). |
|
| 2357 | + * 6. {http://calendarserver.org/ns/}source (Must be a |
|
| 2358 | + * Sabre\DAV\Property\Href). |
|
| 2359 | + * 7. {http://apple.com/ns/ical/}calendar-color |
|
| 2360 | + * 8. {http://apple.com/ns/ical/}calendar-order |
|
| 2361 | + * 9. {urn:ietf:params:xml:ns:caldav}supported-calendar-component-set |
|
| 2362 | + * (should just be an instance of |
|
| 2363 | + * Sabre\CalDAV\Property\SupportedCalendarComponentSet, with a bunch of |
|
| 2364 | + * default components). |
|
| 2365 | + * |
|
| 2366 | + * @param string $principalUri |
|
| 2367 | + * @return array |
|
| 2368 | + */ |
|
| 2369 | + public function getSubscriptionsForUser($principalUri) { |
|
| 2370 | + $fields = array_column($this->subscriptionPropertyMap, 0); |
|
| 2371 | + $fields[] = 'id'; |
|
| 2372 | + $fields[] = 'uri'; |
|
| 2373 | + $fields[] = 'source'; |
|
| 2374 | + $fields[] = 'principaluri'; |
|
| 2375 | + $fields[] = 'lastmodified'; |
|
| 2376 | + $fields[] = 'synctoken'; |
|
| 2377 | + |
|
| 2378 | + $query = $this->db->getQueryBuilder(); |
|
| 2379 | + $query->select($fields) |
|
| 2380 | + ->from('calendarsubscriptions') |
|
| 2381 | + ->where($query->expr()->eq('principaluri', $query->createNamedParameter($principalUri))) |
|
| 2382 | + ->orderBy('calendarorder', 'asc'); |
|
| 2383 | + $stmt = $query->executeQuery(); |
|
| 2384 | + |
|
| 2385 | + $subscriptions = []; |
|
| 2386 | + while ($row = $stmt->fetch()) { |
|
| 2387 | + $subscription = [ |
|
| 2388 | + 'id' => $row['id'], |
|
| 2389 | + 'uri' => $row['uri'], |
|
| 2390 | + 'principaluri' => $row['principaluri'], |
|
| 2391 | + 'source' => $row['source'], |
|
| 2392 | + 'lastmodified' => $row['lastmodified'], |
|
| 2393 | + |
|
| 2394 | + '{' . Plugin::NS_CALDAV . '}supported-calendar-component-set' => new SupportedCalendarComponentSet(['VTODO', 'VEVENT']), |
|
| 2395 | + '{http://sabredav.org/ns}sync-token' => $row['synctoken']?$row['synctoken']:'0', |
|
| 2396 | + ]; |
|
| 2397 | + |
|
| 2398 | + $subscriptions[] = $this->rowToSubscription($row, $subscription); |
|
| 2399 | + } |
|
| 2400 | + |
|
| 2401 | + return $subscriptions; |
|
| 2402 | + } |
|
| 2403 | + |
|
| 2404 | + /** |
|
| 2405 | + * Creates a new subscription for a principal. |
|
| 2406 | + * |
|
| 2407 | + * If the creation was a success, an id must be returned that can be used to reference |
|
| 2408 | + * this subscription in other methods, such as updateSubscription. |
|
| 2409 | + * |
|
| 2410 | + * @param string $principalUri |
|
| 2411 | + * @param string $uri |
|
| 2412 | + * @param array $properties |
|
| 2413 | + * @return mixed |
|
| 2414 | + */ |
|
| 2415 | + public function createSubscription($principalUri, $uri, array $properties) { |
|
| 2416 | + if (!isset($properties['{http://calendarserver.org/ns/}source'])) { |
|
| 2417 | + throw new Forbidden('The {http://calendarserver.org/ns/}source property is required when creating subscriptions'); |
|
| 2418 | + } |
|
| 2419 | + |
|
| 2420 | + $values = [ |
|
| 2421 | + 'principaluri' => $principalUri, |
|
| 2422 | + 'uri' => $uri, |
|
| 2423 | + 'source' => $properties['{http://calendarserver.org/ns/}source']->getHref(), |
|
| 2424 | + 'lastmodified' => time(), |
|
| 2425 | + ]; |
|
| 2426 | + |
|
| 2427 | + $propertiesBoolean = ['striptodos', 'stripalarms', 'stripattachments']; |
|
| 2428 | + |
|
| 2429 | + foreach ($this->subscriptionPropertyMap as $xmlName => [$dbName, $type]) { |
|
| 2430 | + if (array_key_exists($xmlName, $properties)) { |
|
| 2431 | + $values[$dbName] = $properties[$xmlName]; |
|
| 2432 | + if (in_array($dbName, $propertiesBoolean)) { |
|
| 2433 | + $values[$dbName] = true; |
|
| 2434 | + } |
|
| 2435 | + } |
|
| 2436 | + } |
|
| 2437 | + |
|
| 2438 | + [$subscriptionId, $subscriptionRow] = $this->atomic(function() use ($values) { |
|
| 2439 | + $valuesToInsert = []; |
|
| 2440 | + $query = $this->db->getQueryBuilder(); |
|
| 2441 | + foreach (array_keys($values) as $name) { |
|
| 2442 | + $valuesToInsert[$name] = $query->createNamedParameter($values[$name]); |
|
| 2443 | + } |
|
| 2444 | + $query->insert('calendarsubscriptions') |
|
| 2445 | + ->values($valuesToInsert) |
|
| 2446 | + ->executeStatement(); |
|
| 2447 | + |
|
| 2448 | + $subscriptionId = $query->getLastInsertId(); |
|
| 2449 | + |
|
| 2450 | + $subscriptionRow = $this->getSubscriptionById($subscriptionId); |
|
| 2451 | + return [$subscriptionId, $subscriptionRow]; |
|
| 2452 | + }, $this->db); |
|
| 2453 | + |
|
| 2454 | + $this->dispatcher->dispatchTyped(new SubscriptionCreatedEvent($subscriptionId, $subscriptionRow)); |
|
| 2455 | + |
|
| 2456 | + return $subscriptionId; |
|
| 2457 | + } |
|
| 2458 | + |
|
| 2459 | + /** |
|
| 2460 | + * Updates a subscription |
|
| 2461 | + * |
|
| 2462 | + * The list of mutations is stored in a Sabre\DAV\PropPatch object. |
|
| 2463 | + * To do the actual updates, you must tell this object which properties |
|
| 2464 | + * you're going to process with the handle() method. |
|
| 2465 | + * |
|
| 2466 | + * Calling the handle method is like telling the PropPatch object "I |
|
| 2467 | + * promise I can handle updating this property". |
|
| 2468 | + * |
|
| 2469 | + * Read the PropPatch documentation for more info and examples. |
|
| 2470 | + * |
|
| 2471 | + * @param mixed $subscriptionId |
|
| 2472 | + * @param PropPatch $propPatch |
|
| 2473 | + * @return void |
|
| 2474 | + */ |
|
| 2475 | + public function updateSubscription($subscriptionId, PropPatch $propPatch) { |
|
| 2476 | + $supportedProperties = array_keys($this->subscriptionPropertyMap); |
|
| 2477 | + $supportedProperties[] = '{http://calendarserver.org/ns/}source'; |
|
| 2478 | + |
|
| 2479 | + $propPatch->handle($supportedProperties, function ($mutations) use ($subscriptionId) { |
|
| 2480 | + $newValues = []; |
|
| 2481 | + |
|
| 2482 | + foreach ($mutations as $propertyName => $propertyValue) { |
|
| 2483 | + if ($propertyName === '{http://calendarserver.org/ns/}source') { |
|
| 2484 | + $newValues['source'] = $propertyValue->getHref(); |
|
| 2485 | + } else { |
|
| 2486 | + $fieldName = $this->subscriptionPropertyMap[$propertyName][0]; |
|
| 2487 | + $newValues[$fieldName] = $propertyValue; |
|
| 2488 | + } |
|
| 2489 | + } |
|
| 2490 | + |
|
| 2491 | + $query = $this->db->getQueryBuilder(); |
|
| 2492 | + $query->update('calendarsubscriptions') |
|
| 2493 | + ->set('lastmodified', $query->createNamedParameter(time())); |
|
| 2494 | + foreach ($newValues as $fieldName => $value) { |
|
| 2495 | + $query->set($fieldName, $query->createNamedParameter($value)); |
|
| 2496 | + } |
|
| 2497 | + $query->where($query->expr()->eq('id', $query->createNamedParameter($subscriptionId))) |
|
| 2498 | + ->executeStatement(); |
|
| 2499 | + |
|
| 2500 | + $subscriptionRow = $this->getSubscriptionById($subscriptionId); |
|
| 2501 | + $this->dispatcher->dispatchTyped(new SubscriptionUpdatedEvent((int)$subscriptionId, $subscriptionRow, [], $mutations)); |
|
| 2502 | + |
|
| 2503 | + return true; |
|
| 2504 | + }); |
|
| 2505 | + } |
|
| 2506 | + |
|
| 2507 | + /** |
|
| 2508 | + * Deletes a subscription. |
|
| 2509 | + * |
|
| 2510 | + * @param mixed $subscriptionId |
|
| 2511 | + * @return void |
|
| 2512 | + */ |
|
| 2513 | + public function deleteSubscription($subscriptionId) { |
|
| 2514 | + $subscriptionRow = $this->getSubscriptionById($subscriptionId); |
|
| 2515 | + |
|
| 2516 | + $query = $this->db->getQueryBuilder(); |
|
| 2517 | + $query->delete('calendarsubscriptions') |
|
| 2518 | + ->where($query->expr()->eq('id', $query->createNamedParameter($subscriptionId))) |
|
| 2519 | + ->executeStatement(); |
|
| 2520 | + |
|
| 2521 | + $query = $this->db->getQueryBuilder(); |
|
| 2522 | + $query->delete('calendarobjects') |
|
| 2523 | + ->where($query->expr()->eq('calendarid', $query->createNamedParameter($subscriptionId))) |
|
| 2524 | + ->andWhere($query->expr()->eq('calendartype', $query->createNamedParameter(self::CALENDAR_TYPE_SUBSCRIPTION))) |
|
| 2525 | + ->executeStatement(); |
|
| 2526 | + |
|
| 2527 | + $query->delete('calendarchanges') |
|
| 2528 | + ->where($query->expr()->eq('calendarid', $query->createNamedParameter($subscriptionId))) |
|
| 2529 | + ->andWhere($query->expr()->eq('calendartype', $query->createNamedParameter(self::CALENDAR_TYPE_SUBSCRIPTION))) |
|
| 2530 | + ->executeStatement(); |
|
| 2531 | + |
|
| 2532 | + $query->delete($this->dbObjectPropertiesTable) |
|
| 2533 | + ->where($query->expr()->eq('calendarid', $query->createNamedParameter($subscriptionId))) |
|
| 2534 | + ->andWhere($query->expr()->eq('calendartype', $query->createNamedParameter(self::CALENDAR_TYPE_SUBSCRIPTION))) |
|
| 2535 | + ->executeStatement(); |
|
| 2536 | + |
|
| 2537 | + if ($subscriptionRow) { |
|
| 2538 | + $this->dispatcher->dispatchTyped(new SubscriptionDeletedEvent((int)$subscriptionId, $subscriptionRow, [])); |
|
| 2539 | + } |
|
| 2540 | + } |
|
| 2541 | + |
|
| 2542 | + /** |
|
| 2543 | + * Returns a single scheduling object for the inbox collection. |
|
| 2544 | + * |
|
| 2545 | + * The returned array should contain the following elements: |
|
| 2546 | + * * uri - A unique basename for the object. This will be used to |
|
| 2547 | + * construct a full uri. |
|
| 2548 | + * * calendardata - The iCalendar object |
|
| 2549 | + * * lastmodified - The last modification date. Can be an int for a unix |
|
| 2550 | + * timestamp, or a PHP DateTime object. |
|
| 2551 | + * * etag - A unique token that must change if the object changed. |
|
| 2552 | + * * size - The size of the object, in bytes. |
|
| 2553 | + * |
|
| 2554 | + * @param string $principalUri |
|
| 2555 | + * @param string $objectUri |
|
| 2556 | + * @return array |
|
| 2557 | + */ |
|
| 2558 | + public function getSchedulingObject($principalUri, $objectUri) { |
|
| 2559 | + $query = $this->db->getQueryBuilder(); |
|
| 2560 | + $stmt = $query->select(['uri', 'calendardata', 'lastmodified', 'etag', 'size']) |
|
| 2561 | + ->from('schedulingobjects') |
|
| 2562 | + ->where($query->expr()->eq('principaluri', $query->createNamedParameter($principalUri))) |
|
| 2563 | + ->andWhere($query->expr()->eq('uri', $query->createNamedParameter($objectUri))) |
|
| 2564 | + ->executeQuery(); |
|
| 2565 | + |
|
| 2566 | + $row = $stmt->fetch(); |
|
| 2567 | + |
|
| 2568 | + if (!$row) { |
|
| 2569 | + return null; |
|
| 2570 | + } |
|
| 2571 | + |
|
| 2572 | + return [ |
|
| 2573 | + 'uri' => $row['uri'], |
|
| 2574 | + 'calendardata' => $row['calendardata'], |
|
| 2575 | + 'lastmodified' => $row['lastmodified'], |
|
| 2576 | + 'etag' => '"' . $row['etag'] . '"', |
|
| 2577 | + 'size' => (int)$row['size'], |
|
| 2578 | + ]; |
|
| 2579 | + } |
|
| 2580 | + |
|
| 2581 | + /** |
|
| 2582 | + * Returns all scheduling objects for the inbox collection. |
|
| 2583 | + * |
|
| 2584 | + * These objects should be returned as an array. Every item in the array |
|
| 2585 | + * should follow the same structure as returned from getSchedulingObject. |
|
| 2586 | + * |
|
| 2587 | + * The main difference is that 'calendardata' is optional. |
|
| 2588 | + * |
|
| 2589 | + * @param string $principalUri |
|
| 2590 | + * @return array |
|
| 2591 | + */ |
|
| 2592 | + public function getSchedulingObjects($principalUri) { |
|
| 2593 | + $query = $this->db->getQueryBuilder(); |
|
| 2594 | + $stmt = $query->select(['uri', 'calendardata', 'lastmodified', 'etag', 'size']) |
|
| 2595 | + ->from('schedulingobjects') |
|
| 2596 | + ->where($query->expr()->eq('principaluri', $query->createNamedParameter($principalUri))) |
|
| 2597 | + ->executeQuery(); |
|
| 2598 | + |
|
| 2599 | + $result = []; |
|
| 2600 | + foreach ($stmt->fetchAll() as $row) { |
|
| 2601 | + $result[] = [ |
|
| 2602 | + 'calendardata' => $row['calendardata'], |
|
| 2603 | + 'uri' => $row['uri'], |
|
| 2604 | + 'lastmodified' => $row['lastmodified'], |
|
| 2605 | + 'etag' => '"' . $row['etag'] . '"', |
|
| 2606 | + 'size' => (int)$row['size'], |
|
| 2607 | + ]; |
|
| 2608 | + } |
|
| 2609 | + $stmt->closeCursor(); |
|
| 2610 | + |
|
| 2611 | + return $result; |
|
| 2612 | + } |
|
| 2613 | + |
|
| 2614 | + /** |
|
| 2615 | + * Deletes a scheduling object from the inbox collection. |
|
| 2616 | + * |
|
| 2617 | + * @param string $principalUri |
|
| 2618 | + * @param string $objectUri |
|
| 2619 | + * @return void |
|
| 2620 | + */ |
|
| 2621 | + public function deleteSchedulingObject($principalUri, $objectUri) { |
|
| 2622 | + $query = $this->db->getQueryBuilder(); |
|
| 2623 | + $query->delete('schedulingobjects') |
|
| 2624 | + ->where($query->expr()->eq('principaluri', $query->createNamedParameter($principalUri))) |
|
| 2625 | + ->andWhere($query->expr()->eq('uri', $query->createNamedParameter($objectUri))) |
|
| 2626 | + ->executeStatement(); |
|
| 2627 | + } |
|
| 2628 | + |
|
| 2629 | + /** |
|
| 2630 | + * Creates a new scheduling object. This should land in a users' inbox. |
|
| 2631 | + * |
|
| 2632 | + * @param string $principalUri |
|
| 2633 | + * @param string $objectUri |
|
| 2634 | + * @param string $objectData |
|
| 2635 | + * @return void |
|
| 2636 | + */ |
|
| 2637 | + public function createSchedulingObject($principalUri, $objectUri, $objectData) { |
|
| 2638 | + $query = $this->db->getQueryBuilder(); |
|
| 2639 | + $query->insert('schedulingobjects') |
|
| 2640 | + ->values([ |
|
| 2641 | + 'principaluri' => $query->createNamedParameter($principalUri), |
|
| 2642 | + 'calendardata' => $query->createNamedParameter($objectData, IQueryBuilder::PARAM_LOB), |
|
| 2643 | + 'uri' => $query->createNamedParameter($objectUri), |
|
| 2644 | + 'lastmodified' => $query->createNamedParameter(time()), |
|
| 2645 | + 'etag' => $query->createNamedParameter(md5($objectData)), |
|
| 2646 | + 'size' => $query->createNamedParameter(strlen($objectData)) |
|
| 2647 | + ]) |
|
| 2648 | + ->executeStatement(); |
|
| 2649 | + } |
|
| 2650 | + |
|
| 2651 | + /** |
|
| 2652 | + * Adds a change record to the calendarchanges table. |
|
| 2653 | + * |
|
| 2654 | + * @param mixed $calendarId |
|
| 2655 | + * @param string $objectUri |
|
| 2656 | + * @param int $operation 1 = add, 2 = modify, 3 = delete. |
|
| 2657 | + * @param int $calendarType |
|
| 2658 | + * @return void |
|
| 2659 | + */ |
|
| 2660 | + protected function addChange($calendarId, $objectUri, $operation, $calendarType = self::CALENDAR_TYPE_CALENDAR) { |
|
| 2661 | + $table = $calendarType === self::CALENDAR_TYPE_CALENDAR ? 'calendars': 'calendarsubscriptions'; |
|
| 2662 | + |
|
| 2663 | + $query = $this->db->getQueryBuilder(); |
|
| 2664 | + $query->select('synctoken') |
|
| 2665 | + ->from($table) |
|
| 2666 | + ->where($query->expr()->eq('id', $query->createNamedParameter($calendarId))); |
|
| 2667 | + $result = $query->executeQuery(); |
|
| 2668 | + $syncToken = (int)$result->fetchOne(); |
|
| 2669 | + $result->closeCursor(); |
|
| 2670 | + |
|
| 2671 | + $query = $this->db->getQueryBuilder(); |
|
| 2672 | + $query->insert('calendarchanges') |
|
| 2673 | + ->values([ |
|
| 2674 | + 'uri' => $query->createNamedParameter($objectUri), |
|
| 2675 | + 'synctoken' => $query->createNamedParameter($syncToken), |
|
| 2676 | + 'calendarid' => $query->createNamedParameter($calendarId), |
|
| 2677 | + 'operation' => $query->createNamedParameter($operation), |
|
| 2678 | + 'calendartype' => $query->createNamedParameter($calendarType), |
|
| 2679 | + ]) |
|
| 2680 | + ->executeStatement(); |
|
| 2681 | + |
|
| 2682 | + $stmt = $this->db->prepare("UPDATE `*PREFIX*$table` SET `synctoken` = `synctoken` + 1 WHERE `id` = ?"); |
|
| 2683 | + $stmt->execute([ |
|
| 2684 | + $calendarId |
|
| 2685 | + ]); |
|
| 2686 | + } |
|
| 2687 | + |
|
| 2688 | + /** |
|
| 2689 | + * Parses some information from calendar objects, used for optimized |
|
| 2690 | + * calendar-queries. |
|
| 2691 | + * |
|
| 2692 | + * Returns an array with the following keys: |
|
| 2693 | + * * etag - An md5 checksum of the object without the quotes. |
|
| 2694 | + * * size - Size of the object in bytes |
|
| 2695 | + * * componentType - VEVENT, VTODO or VJOURNAL |
|
| 2696 | + * * firstOccurence |
|
| 2697 | + * * lastOccurence |
|
| 2698 | + * * uid - value of the UID property |
|
| 2699 | + * |
|
| 2700 | + * @param string $calendarData |
|
| 2701 | + * @return array |
|
| 2702 | + */ |
|
| 2703 | + public function getDenormalizedData($calendarData) { |
|
| 2704 | + $vObject = Reader::read($calendarData); |
|
| 2705 | + $vEvents = []; |
|
| 2706 | + $componentType = null; |
|
| 2707 | + $component = null; |
|
| 2708 | + $firstOccurrence = null; |
|
| 2709 | + $lastOccurrence = null; |
|
| 2710 | + $uid = null; |
|
| 2711 | + $classification = self::CLASSIFICATION_PUBLIC; |
|
| 2712 | + $hasDTSTART = false; |
|
| 2713 | + foreach ($vObject->getComponents() as $component) { |
|
| 2714 | + if ($component->name !== 'VTIMEZONE') { |
|
| 2715 | + // Finding all VEVENTs, and track them |
|
| 2716 | + if ($component->name === 'VEVENT') { |
|
| 2717 | + array_push($vEvents, $component); |
|
| 2718 | + if ($component->DTSTART) { |
|
| 2719 | + $hasDTSTART = true; |
|
| 2720 | + } |
|
| 2721 | + } |
|
| 2722 | + // Track first component type and uid |
|
| 2723 | + if ($uid === null) { |
|
| 2724 | + $componentType = $component->name; |
|
| 2725 | + $uid = (string)$component->UID; |
|
| 2726 | + } |
|
| 2727 | + } |
|
| 2728 | + } |
|
| 2729 | + if (!$componentType) { |
|
| 2730 | + throw new BadRequest('Calendar objects must have a VJOURNAL, VEVENT or VTODO component'); |
|
| 2731 | + } |
|
| 2732 | + |
|
| 2733 | + if ($hasDTSTART) { |
|
| 2734 | + $component = $vEvents[0]; |
|
| 2735 | + |
|
| 2736 | + // Finding the last occurrence is a bit harder |
|
| 2737 | + if (!isset($component->RRULE) && count($vEvents) === 1) { |
|
| 2738 | + $firstOccurrence = $component->DTSTART->getDateTime()->getTimeStamp(); |
|
| 2739 | + if (isset($component->DTEND)) { |
|
| 2740 | + $lastOccurrence = $component->DTEND->getDateTime()->getTimeStamp(); |
|
| 2741 | + } elseif (isset($component->DURATION)) { |
|
| 2742 | + $endDate = clone $component->DTSTART->getDateTime(); |
|
| 2743 | + $endDate->add(DateTimeParser::parse($component->DURATION->getValue())); |
|
| 2744 | + $lastOccurrence = $endDate->getTimeStamp(); |
|
| 2745 | + } elseif (!$component->DTSTART->hasTime()) { |
|
| 2746 | + $endDate = clone $component->DTSTART->getDateTime(); |
|
| 2747 | + $endDate->modify('+1 day'); |
|
| 2748 | + $lastOccurrence = $endDate->getTimeStamp(); |
|
| 2749 | + } else { |
|
| 2750 | + $lastOccurrence = $firstOccurrence; |
|
| 2751 | + } |
|
| 2752 | + } else { |
|
| 2753 | + $it = new EventIterator($vEvents); |
|
| 2754 | + $maxDate = new DateTime(self::MAX_DATE); |
|
| 2755 | + $firstOccurrence = $it->getDtStart()->getTimestamp(); |
|
| 2756 | + if ($it->isInfinite()) { |
|
| 2757 | + $lastOccurrence = $maxDate->getTimestamp(); |
|
| 2758 | + } else { |
|
| 2759 | + $end = $it->getDtEnd(); |
|
| 2760 | + while ($it->valid() && $end < $maxDate) { |
|
| 2761 | + $end = $it->getDtEnd(); |
|
| 2762 | + $it->next(); |
|
| 2763 | + } |
|
| 2764 | + $lastOccurrence = $end->getTimestamp(); |
|
| 2765 | + } |
|
| 2766 | + } |
|
| 2767 | + } |
|
| 2768 | + |
|
| 2769 | + if ($component->CLASS) { |
|
| 2770 | + $classification = CalDavBackend::CLASSIFICATION_PRIVATE; |
|
| 2771 | + switch ($component->CLASS->getValue()) { |
|
| 2772 | + case 'PUBLIC': |
|
| 2773 | + $classification = CalDavBackend::CLASSIFICATION_PUBLIC; |
|
| 2774 | + break; |
|
| 2775 | + case 'CONFIDENTIAL': |
|
| 2776 | + $classification = CalDavBackend::CLASSIFICATION_CONFIDENTIAL; |
|
| 2777 | + break; |
|
| 2778 | + } |
|
| 2779 | + } |
|
| 2780 | + return [ |
|
| 2781 | + 'etag' => md5($calendarData), |
|
| 2782 | + 'size' => strlen($calendarData), |
|
| 2783 | + 'componentType' => $componentType, |
|
| 2784 | + 'firstOccurence' => is_null($firstOccurrence) ? null : max(0, $firstOccurrence), |
|
| 2785 | + 'lastOccurence' => $lastOccurrence, |
|
| 2786 | + 'uid' => $uid, |
|
| 2787 | + 'classification' => $classification |
|
| 2788 | + ]; |
|
| 2789 | + } |
|
| 2790 | + |
|
| 2791 | + /** |
|
| 2792 | + * @param $cardData |
|
| 2793 | + * @return bool|string |
|
| 2794 | + */ |
|
| 2795 | + private function readBlob($cardData) { |
|
| 2796 | + if (is_resource($cardData)) { |
|
| 2797 | + return stream_get_contents($cardData); |
|
| 2798 | + } |
|
| 2799 | + |
|
| 2800 | + return $cardData; |
|
| 2801 | + } |
|
| 2802 | + |
|
| 2803 | + /** |
|
| 2804 | + * @param list<array{href: string, commonName: string, readOnly: bool}> $add |
|
| 2805 | + * @param list<string> $remove |
|
| 2806 | + */ |
|
| 2807 | + public function updateShares(IShareable $shareable, array $add, array $remove): void { |
|
| 2808 | + $calendarId = $shareable->getResourceId(); |
|
| 2809 | + $calendarRow = $this->getCalendarById($calendarId); |
|
| 2810 | + if ($calendarRow === null) { |
|
| 2811 | + throw new \RuntimeException('Trying to update shares for innexistant calendar: ' . $calendarId); |
|
| 2812 | + } |
|
| 2813 | + $oldShares = $this->getShares($calendarId); |
|
| 2814 | + |
|
| 2815 | + $this->calendarSharingBackend->updateShares($shareable, $add, $remove); |
|
| 2816 | + |
|
| 2817 | + $this->dispatcher->dispatchTyped(new CalendarShareUpdatedEvent($calendarId, $calendarRow, $oldShares, $add, $remove)); |
|
| 2818 | + } |
|
| 2819 | + |
|
| 2820 | + /** |
|
| 2821 | + * @return list<array{href: string, commonName: string, status: int, readOnly: bool, '{http://owncloud.org/ns}principal': string, '{http://owncloud.org/ns}group-share': bool}> |
|
| 2822 | + */ |
|
| 2823 | + public function getShares(int $resourceId): array { |
|
| 2824 | + return $this->calendarSharingBackend->getShares($resourceId); |
|
| 2825 | + } |
|
| 2826 | + |
|
| 2827 | + /** |
|
| 2828 | + * @param boolean $value |
|
| 2829 | + * @param \OCA\DAV\CalDAV\Calendar $calendar |
|
| 2830 | + * @return string|null |
|
| 2831 | + */ |
|
| 2832 | + public function setPublishStatus($value, $calendar) { |
|
| 2833 | + $calendarId = $calendar->getResourceId(); |
|
| 2834 | + $calendarData = $this->getCalendarById($calendarId); |
|
| 2835 | + |
|
| 2836 | + $query = $this->db->getQueryBuilder(); |
|
| 2837 | + if ($value) { |
|
| 2838 | + $publicUri = $this->random->generate(16, ISecureRandom::CHAR_HUMAN_READABLE); |
|
| 2839 | + $query->insert('dav_shares') |
|
| 2840 | + ->values([ |
|
| 2841 | + 'principaluri' => $query->createNamedParameter($calendar->getPrincipalURI()), |
|
| 2842 | + 'type' => $query->createNamedParameter('calendar'), |
|
| 2843 | + 'access' => $query->createNamedParameter(self::ACCESS_PUBLIC), |
|
| 2844 | + 'resourceid' => $query->createNamedParameter($calendar->getResourceId()), |
|
| 2845 | + 'publicuri' => $query->createNamedParameter($publicUri) |
|
| 2846 | + ]); |
|
| 2847 | + $query->executeStatement(); |
|
| 2848 | + |
|
| 2849 | + $this->dispatcher->dispatchTyped(new CalendarPublishedEvent($calendarId, $calendarData, $publicUri)); |
|
| 2850 | + return $publicUri; |
|
| 2851 | + } |
|
| 2852 | + $query->delete('dav_shares') |
|
| 2853 | + ->where($query->expr()->eq('resourceid', $query->createNamedParameter($calendar->getResourceId()))) |
|
| 2854 | + ->andWhere($query->expr()->eq('access', $query->createNamedParameter(self::ACCESS_PUBLIC))); |
|
| 2855 | + $query->executeStatement(); |
|
| 2856 | + |
|
| 2857 | + $this->dispatcher->dispatchTyped(new CalendarUnpublishedEvent($calendarId, $calendarData)); |
|
| 2858 | + return null; |
|
| 2859 | + } |
|
| 2860 | + |
|
| 2861 | + /** |
|
| 2862 | + * @param \OCA\DAV\CalDAV\Calendar $calendar |
|
| 2863 | + * @return mixed |
|
| 2864 | + */ |
|
| 2865 | + public function getPublishStatus($calendar) { |
|
| 2866 | + $query = $this->db->getQueryBuilder(); |
|
| 2867 | + $result = $query->select('publicuri') |
|
| 2868 | + ->from('dav_shares') |
|
| 2869 | + ->where($query->expr()->eq('resourceid', $query->createNamedParameter($calendar->getResourceId()))) |
|
| 2870 | + ->andWhere($query->expr()->eq('access', $query->createNamedParameter(self::ACCESS_PUBLIC))) |
|
| 2871 | + ->executeQuery(); |
|
| 2872 | + |
|
| 2873 | + $row = $result->fetch(); |
|
| 2874 | + $result->closeCursor(); |
|
| 2875 | + return $row ? reset($row) : false; |
|
| 2876 | + } |
|
| 2877 | + |
|
| 2878 | + /** |
|
| 2879 | + * @param int $resourceId |
|
| 2880 | + * @param list<array{privilege: string, principal: string, protected: bool}> $acl |
|
| 2881 | + * @return list<array{privilege: string, principal: string, protected: bool}> |
|
| 2882 | + */ |
|
| 2883 | + public function applyShareAcl(int $resourceId, array $acl): array { |
|
| 2884 | + return $this->calendarSharingBackend->applyShareAcl($resourceId, $acl); |
|
| 2885 | + } |
|
| 2886 | + |
|
| 2887 | + /** |
|
| 2888 | + * update properties table |
|
| 2889 | + * |
|
| 2890 | + * @param int $calendarId |
|
| 2891 | + * @param string $objectUri |
|
| 2892 | + * @param string $calendarData |
|
| 2893 | + * @param int $calendarType |
|
| 2894 | + */ |
|
| 2895 | + public function updateProperties($calendarId, $objectUri, $calendarData, $calendarType = self::CALENDAR_TYPE_CALENDAR) { |
|
| 2896 | + $objectId = $this->getCalendarObjectId($calendarId, $objectUri, $calendarType); |
|
| 2897 | + |
|
| 2898 | + try { |
|
| 2899 | + $vCalendar = $this->readCalendarData($calendarData); |
|
| 2900 | + } catch (\Exception $ex) { |
|
| 2901 | + return; |
|
| 2902 | + } |
|
| 2903 | + |
|
| 2904 | + $this->purgeProperties($calendarId, $objectId); |
|
| 2905 | + |
|
| 2906 | + $query = $this->db->getQueryBuilder(); |
|
| 2907 | + $query->insert($this->dbObjectPropertiesTable) |
|
| 2908 | + ->values( |
|
| 2909 | + [ |
|
| 2910 | + 'calendarid' => $query->createNamedParameter($calendarId), |
|
| 2911 | + 'calendartype' => $query->createNamedParameter($calendarType), |
|
| 2912 | + 'objectid' => $query->createNamedParameter($objectId), |
|
| 2913 | + 'name' => $query->createParameter('name'), |
|
| 2914 | + 'parameter' => $query->createParameter('parameter'), |
|
| 2915 | + 'value' => $query->createParameter('value'), |
|
| 2916 | + ] |
|
| 2917 | + ); |
|
| 2918 | + |
|
| 2919 | + $indexComponents = ['VEVENT', 'VJOURNAL', 'VTODO']; |
|
| 2920 | + foreach ($vCalendar->getComponents() as $component) { |
|
| 2921 | + if (!in_array($component->name, $indexComponents)) { |
|
| 2922 | + continue; |
|
| 2923 | + } |
|
| 2924 | + |
|
| 2925 | + foreach ($component->children() as $property) { |
|
| 2926 | + if (in_array($property->name, self::INDEXED_PROPERTIES, true)) { |
|
| 2927 | + $value = $property->getValue(); |
|
| 2928 | + // is this a shitty db? |
|
| 2929 | + if (!$this->db->supports4ByteText()) { |
|
| 2930 | + $value = preg_replace('/[\x{10000}-\x{10FFFF}]/u', "\xEF\xBF\xBD", $value); |
|
| 2931 | + } |
|
| 2932 | + $value = mb_strcut($value, 0, 254); |
|
| 2933 | + |
|
| 2934 | + $query->setParameter('name', $property->name); |
|
| 2935 | + $query->setParameter('parameter', null); |
|
| 2936 | + $query->setParameter('value', $value); |
|
| 2937 | + $query->executeStatement(); |
|
| 2938 | + } |
|
| 2939 | + |
|
| 2940 | + if (array_key_exists($property->name, self::$indexParameters)) { |
|
| 2941 | + $parameters = $property->parameters(); |
|
| 2942 | + $indexedParametersForProperty = self::$indexParameters[$property->name]; |
|
| 2943 | + |
|
| 2944 | + foreach ($parameters as $key => $value) { |
|
| 2945 | + if (in_array($key, $indexedParametersForProperty)) { |
|
| 2946 | + // is this a shitty db? |
|
| 2947 | + if ($this->db->supports4ByteText()) { |
|
| 2948 | + $value = preg_replace('/[\x{10000}-\x{10FFFF}]/u', "\xEF\xBF\xBD", $value); |
|
| 2949 | + } |
|
| 2950 | + |
|
| 2951 | + $query->setParameter('name', $property->name); |
|
| 2952 | + $query->setParameter('parameter', mb_strcut($key, 0, 254)); |
|
| 2953 | + $query->setParameter('value', mb_strcut($value, 0, 254)); |
|
| 2954 | + $query->executeStatement(); |
|
| 2955 | + } |
|
| 2956 | + } |
|
| 2957 | + } |
|
| 2958 | + } |
|
| 2959 | + } |
|
| 2960 | + } |
|
| 2961 | + |
|
| 2962 | + /** |
|
| 2963 | + * deletes all birthday calendars |
|
| 2964 | + */ |
|
| 2965 | + public function deleteAllBirthdayCalendars() { |
|
| 2966 | + $query = $this->db->getQueryBuilder(); |
|
| 2967 | + $result = $query->select(['id'])->from('calendars') |
|
| 2968 | + ->where($query->expr()->eq('uri', $query->createNamedParameter(BirthdayService::BIRTHDAY_CALENDAR_URI))) |
|
| 2969 | + ->executeQuery(); |
|
| 2970 | + |
|
| 2971 | + $ids = $result->fetchAll(); |
|
| 2972 | + $result->closeCursor(); |
|
| 2973 | + foreach ($ids as $id) { |
|
| 2974 | + $this->deleteCalendar( |
|
| 2975 | + $id['id'], |
|
| 2976 | + true // No data to keep in the trashbin, if the user re-enables then we regenerate |
|
| 2977 | + ); |
|
| 2978 | + } |
|
| 2979 | + } |
|
| 2980 | + |
|
| 2981 | + /** |
|
| 2982 | + * @param $subscriptionId |
|
| 2983 | + */ |
|
| 2984 | + public function purgeAllCachedEventsForSubscription($subscriptionId) { |
|
| 2985 | + $query = $this->db->getQueryBuilder(); |
|
| 2986 | + $query->select('uri') |
|
| 2987 | + ->from('calendarobjects') |
|
| 2988 | + ->where($query->expr()->eq('calendarid', $query->createNamedParameter($subscriptionId))) |
|
| 2989 | + ->andWhere($query->expr()->eq('calendartype', $query->createNamedParameter(self::CALENDAR_TYPE_SUBSCRIPTION))); |
|
| 2990 | + $stmt = $query->executeQuery(); |
|
| 2991 | + |
|
| 2992 | + $uris = []; |
|
| 2993 | + foreach ($stmt->fetchAll() as $row) { |
|
| 2994 | + $uris[] = $row['uri']; |
|
| 2995 | + } |
|
| 2996 | + $stmt->closeCursor(); |
|
| 2997 | + |
|
| 2998 | + $query = $this->db->getQueryBuilder(); |
|
| 2999 | + $query->delete('calendarobjects') |
|
| 3000 | + ->where($query->expr()->eq('calendarid', $query->createNamedParameter($subscriptionId))) |
|
| 3001 | + ->andWhere($query->expr()->eq('calendartype', $query->createNamedParameter(self::CALENDAR_TYPE_SUBSCRIPTION))) |
|
| 3002 | + ->executeStatement(); |
|
| 3003 | + |
|
| 3004 | + $query->delete('calendarchanges') |
|
| 3005 | + ->where($query->expr()->eq('calendarid', $query->createNamedParameter($subscriptionId))) |
|
| 3006 | + ->andWhere($query->expr()->eq('calendartype', $query->createNamedParameter(self::CALENDAR_TYPE_SUBSCRIPTION))) |
|
| 3007 | + ->executeStatement(); |
|
| 3008 | + |
|
| 3009 | + $query->delete($this->dbObjectPropertiesTable) |
|
| 3010 | + ->where($query->expr()->eq('calendarid', $query->createNamedParameter($subscriptionId))) |
|
| 3011 | + ->andWhere($query->expr()->eq('calendartype', $query->createNamedParameter(self::CALENDAR_TYPE_SUBSCRIPTION))) |
|
| 3012 | + ->executeStatement(); |
|
| 3013 | + |
|
| 3014 | + foreach ($uris as $uri) { |
|
| 3015 | + $this->addChange($subscriptionId, $uri, 3, self::CALENDAR_TYPE_SUBSCRIPTION); |
|
| 3016 | + } |
|
| 3017 | + } |
|
| 3018 | + |
|
| 3019 | + /** |
|
| 3020 | + * Move a calendar from one user to another |
|
| 3021 | + * |
|
| 3022 | + * @param string $uriName |
|
| 3023 | + * @param string $uriOrigin |
|
| 3024 | + * @param string $uriDestination |
|
| 3025 | + * @param string $newUriName (optional) the new uriName |
|
| 3026 | + */ |
|
| 3027 | + public function moveCalendar($uriName, $uriOrigin, $uriDestination, $newUriName = null) { |
|
| 3028 | + $query = $this->db->getQueryBuilder(); |
|
| 3029 | + $query->update('calendars') |
|
| 3030 | + ->set('principaluri', $query->createNamedParameter($uriDestination)) |
|
| 3031 | + ->set('uri', $query->createNamedParameter($newUriName ?: $uriName)) |
|
| 3032 | + ->where($query->expr()->eq('principaluri', $query->createNamedParameter($uriOrigin))) |
|
| 3033 | + ->andWhere($query->expr()->eq('uri', $query->createNamedParameter($uriName))) |
|
| 3034 | + ->executeStatement(); |
|
| 3035 | + } |
|
| 3036 | + |
|
| 3037 | + /** |
|
| 3038 | + * read VCalendar data into a VCalendar object |
|
| 3039 | + * |
|
| 3040 | + * @param string $objectData |
|
| 3041 | + * @return VCalendar |
|
| 3042 | + */ |
|
| 3043 | + protected function readCalendarData($objectData) { |
|
| 3044 | + return Reader::read($objectData); |
|
| 3045 | + } |
|
| 3046 | + |
|
| 3047 | + /** |
|
| 3048 | + * delete all properties from a given calendar object |
|
| 3049 | + * |
|
| 3050 | + * @param int $calendarId |
|
| 3051 | + * @param int $objectId |
|
| 3052 | + */ |
|
| 3053 | + protected function purgeProperties($calendarId, $objectId) { |
|
| 3054 | + $query = $this->db->getQueryBuilder(); |
|
| 3055 | + $query->delete($this->dbObjectPropertiesTable) |
|
| 3056 | + ->where($query->expr()->eq('objectid', $query->createNamedParameter($objectId))) |
|
| 3057 | + ->andWhere($query->expr()->eq('calendarid', $query->createNamedParameter($calendarId))); |
|
| 3058 | + $query->executeStatement(); |
|
| 3059 | + } |
|
| 3060 | + |
|
| 3061 | + /** |
|
| 3062 | + * get ID from a given calendar object |
|
| 3063 | + * |
|
| 3064 | + * @param int $calendarId |
|
| 3065 | + * @param string $uri |
|
| 3066 | + * @param int $calendarType |
|
| 3067 | + * @return int |
|
| 3068 | + */ |
|
| 3069 | + protected function getCalendarObjectId($calendarId, $uri, $calendarType):int { |
|
| 3070 | + $query = $this->db->getQueryBuilder(); |
|
| 3071 | + $query->select('id') |
|
| 3072 | + ->from('calendarobjects') |
|
| 3073 | + ->where($query->expr()->eq('uri', $query->createNamedParameter($uri))) |
|
| 3074 | + ->andWhere($query->expr()->eq('calendarid', $query->createNamedParameter($calendarId))) |
|
| 3075 | + ->andWhere($query->expr()->eq('calendartype', $query->createNamedParameter($calendarType))); |
|
| 3076 | + |
|
| 3077 | + $result = $query->executeQuery(); |
|
| 3078 | + $objectIds = $result->fetch(); |
|
| 3079 | + $result->closeCursor(); |
|
| 3080 | + |
|
| 3081 | + if (!isset($objectIds['id'])) { |
|
| 3082 | + throw new \InvalidArgumentException('Calendarobject does not exists: ' . $uri); |
|
| 3083 | + } |
|
| 3084 | + |
|
| 3085 | + return (int)$objectIds['id']; |
|
| 3086 | + } |
|
| 3087 | + |
|
| 3088 | + /** |
|
| 3089 | + * @throws \InvalidArgumentException |
|
| 3090 | + */ |
|
| 3091 | + public function pruneOutdatedSyncTokens(int $keep = 10_000): int { |
|
| 3092 | + if ($keep < 0) { |
|
| 3093 | + throw new \InvalidArgumentException(); |
|
| 3094 | + } |
|
| 3095 | + $query = $this->db->getQueryBuilder(); |
|
| 3096 | + $query->delete('calendarchanges') |
|
| 3097 | + ->orderBy('id', 'DESC') |
|
| 3098 | + ->setFirstResult($keep); |
|
| 3099 | + return $query->executeStatement(); |
|
| 3100 | + } |
|
| 3101 | + |
|
| 3102 | + /** |
|
| 3103 | + * return legacy endpoint principal name to new principal name |
|
| 3104 | + * |
|
| 3105 | + * @param $principalUri |
|
| 3106 | + * @param $toV2 |
|
| 3107 | + * @return string |
|
| 3108 | + */ |
|
| 3109 | + private function convertPrincipal($principalUri, $toV2) { |
|
| 3110 | + if ($this->principalBackend->getPrincipalPrefix() === 'principals') { |
|
| 3111 | + [, $name] = Uri\split($principalUri); |
|
| 3112 | + if ($toV2 === true) { |
|
| 3113 | + return "principals/users/$name"; |
|
| 3114 | + } |
|
| 3115 | + return "principals/$name"; |
|
| 3116 | + } |
|
| 3117 | + return $principalUri; |
|
| 3118 | + } |
|
| 3119 | + |
|
| 3120 | + /** |
|
| 3121 | + * adds information about an owner to the calendar data |
|
| 3122 | + * |
|
| 3123 | + */ |
|
| 3124 | + private function addOwnerPrincipalToCalendar(array $calendarInfo): array { |
|
| 3125 | + $ownerPrincipalKey = '{' . \OCA\DAV\DAV\Sharing\Plugin::NS_OWNCLOUD . '}owner-principal'; |
|
| 3126 | + $displaynameKey = '{' . \OCA\DAV\DAV\Sharing\Plugin::NS_NEXTCLOUD . '}owner-displayname'; |
|
| 3127 | + if (isset($calendarInfo[$ownerPrincipalKey])) { |
|
| 3128 | + $uri = $calendarInfo[$ownerPrincipalKey]; |
|
| 3129 | + } else { |
|
| 3130 | + $uri = $calendarInfo['principaluri']; |
|
| 3131 | + } |
|
| 3132 | + |
|
| 3133 | + $principalInformation = $this->principalBackend->getPrincipalByPath($uri); |
|
| 3134 | + if (isset($principalInformation['{DAV:}displayname'])) { |
|
| 3135 | + $calendarInfo[$displaynameKey] = $principalInformation['{DAV:}displayname']; |
|
| 3136 | + } |
|
| 3137 | + return $calendarInfo; |
|
| 3138 | + } |
|
| 3139 | + |
|
| 3140 | + private function addResourceTypeToCalendar(array $row, array $calendar): array { |
|
| 3141 | + if (isset($row['deleted_at'])) { |
|
| 3142 | + // Columns is set and not null -> this is a deleted calendar |
|
| 3143 | + // we send a custom resourcetype to hide the deleted calendar |
|
| 3144 | + // from ordinary DAV clients, but the Calendar app will know |
|
| 3145 | + // how to handle this special resource. |
|
| 3146 | + $calendar['{DAV:}resourcetype'] = new DAV\Xml\Property\ResourceType([ |
|
| 3147 | + '{DAV:}collection', |
|
| 3148 | + sprintf('{%s}deleted-calendar', \OCA\DAV\DAV\Sharing\Plugin::NS_NEXTCLOUD), |
|
| 3149 | + ]); |
|
| 3150 | + } |
|
| 3151 | + return $calendar; |
|
| 3152 | + } |
|
| 3153 | + |
|
| 3154 | + /** |
|
| 3155 | + * Amend the calendar info with database row data |
|
| 3156 | + * |
|
| 3157 | + * @param array $row |
|
| 3158 | + * @param array $calendar |
|
| 3159 | + * |
|
| 3160 | + * @return array |
|
| 3161 | + */ |
|
| 3162 | + private function rowToCalendar($row, array $calendar): array { |
|
| 3163 | + foreach ($this->propertyMap as $xmlName => [$dbName, $type]) { |
|
| 3164 | + $value = $row[$dbName]; |
|
| 3165 | + if ($value !== null) { |
|
| 3166 | + settype($value, $type); |
|
| 3167 | + } |
|
| 3168 | + $calendar[$xmlName] = $value; |
|
| 3169 | + } |
|
| 3170 | + return $calendar; |
|
| 3171 | + } |
|
| 3172 | + |
|
| 3173 | + /** |
|
| 3174 | + * Amend the subscription info with database row data |
|
| 3175 | + * |
|
| 3176 | + * @param array $row |
|
| 3177 | + * @param array $subscription |
|
| 3178 | + * |
|
| 3179 | + * @return array |
|
| 3180 | + */ |
|
| 3181 | + private function rowToSubscription($row, array $subscription): array { |
|
| 3182 | + foreach ($this->subscriptionPropertyMap as $xmlName => [$dbName, $type]) { |
|
| 3183 | + $value = $row[$dbName]; |
|
| 3184 | + if ($value !== null) { |
|
| 3185 | + settype($value, $type); |
|
| 3186 | + } |
|
| 3187 | + $subscription[$xmlName] = $value; |
|
| 3188 | + } |
|
| 3189 | + return $subscription; |
|
| 3190 | + } |
|
| 3191 | 3191 | } |
@@ -62,1318 +62,1318 @@ |
||
| 62 | 62 | |
| 63 | 63 | class CardDavBackend implements BackendInterface, SyncSupport { |
| 64 | 64 | |
| 65 | - use TTransactional; |
|
| 66 | - |
|
| 67 | - public const PERSONAL_ADDRESSBOOK_URI = 'contacts'; |
|
| 68 | - public const PERSONAL_ADDRESSBOOK_NAME = 'Contacts'; |
|
| 69 | - |
|
| 70 | - private Principal $principalBackend; |
|
| 71 | - private string $dbCardsTable = 'cards'; |
|
| 72 | - private string $dbCardsPropertiesTable = 'cards_properties'; |
|
| 73 | - private IDBConnection $db; |
|
| 74 | - private Backend $sharingBackend; |
|
| 75 | - |
|
| 76 | - /** @var array properties to index */ |
|
| 77 | - public static array $indexProperties = [ |
|
| 78 | - 'BDAY', 'UID', 'N', 'FN', 'TITLE', 'ROLE', 'NOTE', 'NICKNAME', |
|
| 79 | - 'ORG', 'CATEGORIES', 'EMAIL', 'TEL', 'IMPP', 'ADR', 'URL', 'GEO', |
|
| 80 | - 'CLOUD', 'X-SOCIALPROFILE']; |
|
| 81 | - |
|
| 82 | - /** |
|
| 83 | - * @var string[] Map of uid => display name |
|
| 84 | - */ |
|
| 85 | - protected array $userDisplayNames; |
|
| 86 | - private IUserManager $userManager; |
|
| 87 | - private IEventDispatcher $dispatcher; |
|
| 88 | - private array $etagCache = []; |
|
| 89 | - |
|
| 90 | - /** |
|
| 91 | - * CardDavBackend constructor. |
|
| 92 | - * |
|
| 93 | - * @param IDBConnection $db |
|
| 94 | - * @param Principal $principalBackend |
|
| 95 | - * @param IUserManager $userManager |
|
| 96 | - * @param IGroupManager $groupManager |
|
| 97 | - * @param IEventDispatcher $dispatcher |
|
| 98 | - */ |
|
| 99 | - public function __construct(IDBConnection $db, |
|
| 100 | - Principal $principalBackend, |
|
| 101 | - IUserManager $userManager, |
|
| 102 | - IGroupManager $groupManager, |
|
| 103 | - IEventDispatcher $dispatcher) { |
|
| 104 | - $this->db = $db; |
|
| 105 | - $this->principalBackend = $principalBackend; |
|
| 106 | - $this->userManager = $userManager; |
|
| 107 | - $this->dispatcher = $dispatcher; |
|
| 108 | - $this->sharingBackend = new Backend($this->db, $this->userManager, $groupManager, $principalBackend, 'addressbook'); |
|
| 109 | - } |
|
| 110 | - |
|
| 111 | - /** |
|
| 112 | - * Return the number of address books for a principal |
|
| 113 | - * |
|
| 114 | - * @param $principalUri |
|
| 115 | - * @return int |
|
| 116 | - */ |
|
| 117 | - public function getAddressBooksForUserCount($principalUri) { |
|
| 118 | - $principalUri = $this->convertPrincipal($principalUri, true); |
|
| 119 | - $query = $this->db->getQueryBuilder(); |
|
| 120 | - $query->select($query->func()->count('*')) |
|
| 121 | - ->from('addressbooks') |
|
| 122 | - ->where($query->expr()->eq('principaluri', $query->createNamedParameter($principalUri))); |
|
| 123 | - |
|
| 124 | - $result = $query->executeQuery(); |
|
| 125 | - $column = (int) $result->fetchOne(); |
|
| 126 | - $result->closeCursor(); |
|
| 127 | - return $column; |
|
| 128 | - } |
|
| 129 | - |
|
| 130 | - /** |
|
| 131 | - * Returns the list of address books for a specific user. |
|
| 132 | - * |
|
| 133 | - * Every addressbook should have the following properties: |
|
| 134 | - * id - an arbitrary unique id |
|
| 135 | - * uri - the 'basename' part of the url |
|
| 136 | - * principaluri - Same as the passed parameter |
|
| 137 | - * |
|
| 138 | - * Any additional clark-notation property may be passed besides this. Some |
|
| 139 | - * common ones are : |
|
| 140 | - * {DAV:}displayname |
|
| 141 | - * {urn:ietf:params:xml:ns:carddav}addressbook-description |
|
| 142 | - * {http://calendarserver.org/ns/}getctag |
|
| 143 | - * |
|
| 144 | - * @param string $principalUri |
|
| 145 | - * @return array |
|
| 146 | - */ |
|
| 147 | - public function getAddressBooksForUser($principalUri) { |
|
| 148 | - $principalUriOriginal = $principalUri; |
|
| 149 | - $principalUri = $this->convertPrincipal($principalUri, true); |
|
| 150 | - $query = $this->db->getQueryBuilder(); |
|
| 151 | - $query->select(['id', 'uri', 'displayname', 'principaluri', 'description', 'synctoken']) |
|
| 152 | - ->from('addressbooks') |
|
| 153 | - ->where($query->expr()->eq('principaluri', $query->createNamedParameter($principalUri))); |
|
| 154 | - |
|
| 155 | - $addressBooks = []; |
|
| 156 | - |
|
| 157 | - $result = $query->execute(); |
|
| 158 | - while ($row = $result->fetch()) { |
|
| 159 | - $addressBooks[$row['id']] = [ |
|
| 160 | - 'id' => $row['id'], |
|
| 161 | - 'uri' => $row['uri'], |
|
| 162 | - 'principaluri' => $this->convertPrincipal($row['principaluri'], false), |
|
| 163 | - '{DAV:}displayname' => $row['displayname'], |
|
| 164 | - '{' . Plugin::NS_CARDDAV . '}addressbook-description' => $row['description'], |
|
| 165 | - '{http://calendarserver.org/ns/}getctag' => $row['synctoken'], |
|
| 166 | - '{http://sabredav.org/ns}sync-token' => $row['synctoken'] ?: '0', |
|
| 167 | - ]; |
|
| 168 | - |
|
| 169 | - $this->addOwnerPrincipal($addressBooks[$row['id']]); |
|
| 170 | - } |
|
| 171 | - $result->closeCursor(); |
|
| 172 | - |
|
| 173 | - // query for shared addressbooks |
|
| 174 | - $principals = $this->principalBackend->getGroupMembership($principalUriOriginal, true); |
|
| 175 | - $principals = array_merge($principals, $this->principalBackend->getCircleMembership($principalUriOriginal)); |
|
| 176 | - |
|
| 177 | - $principals[] = $principalUri; |
|
| 178 | - |
|
| 179 | - $query = $this->db->getQueryBuilder(); |
|
| 180 | - $result = $query->select(['a.id', 'a.uri', 'a.displayname', 'a.principaluri', 'a.description', 'a.synctoken', 's.access']) |
|
| 181 | - ->from('dav_shares', 's') |
|
| 182 | - ->join('s', 'addressbooks', 'a', $query->expr()->eq('s.resourceid', 'a.id')) |
|
| 183 | - ->where($query->expr()->in('s.principaluri', $query->createParameter('principaluri'))) |
|
| 184 | - ->andWhere($query->expr()->eq('s.type', $query->createParameter('type'))) |
|
| 185 | - ->setParameter('type', 'addressbook') |
|
| 186 | - ->setParameter('principaluri', $principals, IQueryBuilder::PARAM_STR_ARRAY) |
|
| 187 | - ->execute(); |
|
| 188 | - |
|
| 189 | - $readOnlyPropertyName = '{' . \OCA\DAV\DAV\Sharing\Plugin::NS_OWNCLOUD . '}read-only'; |
|
| 190 | - while ($row = $result->fetch()) { |
|
| 191 | - if ($row['principaluri'] === $principalUri) { |
|
| 192 | - continue; |
|
| 193 | - } |
|
| 194 | - |
|
| 195 | - $readOnly = (int)$row['access'] === Backend::ACCESS_READ; |
|
| 196 | - if (isset($addressBooks[$row['id']])) { |
|
| 197 | - if ($readOnly) { |
|
| 198 | - // New share can not have more permissions then the old one. |
|
| 199 | - continue; |
|
| 200 | - } |
|
| 201 | - if (isset($addressBooks[$row['id']][$readOnlyPropertyName]) && |
|
| 202 | - $addressBooks[$row['id']][$readOnlyPropertyName] === 0) { |
|
| 203 | - // Old share is already read-write, no more permissions can be gained |
|
| 204 | - continue; |
|
| 205 | - } |
|
| 206 | - } |
|
| 207 | - |
|
| 208 | - [, $name] = \Sabre\Uri\split($row['principaluri']); |
|
| 209 | - $uri = $row['uri'] . '_shared_by_' . $name; |
|
| 210 | - $displayName = $row['displayname'] . ' (' . ($this->userManager->getDisplayName($name) ?? $name ?? '') . ')'; |
|
| 211 | - |
|
| 212 | - $addressBooks[$row['id']] = [ |
|
| 213 | - 'id' => $row['id'], |
|
| 214 | - 'uri' => $uri, |
|
| 215 | - 'principaluri' => $principalUriOriginal, |
|
| 216 | - '{DAV:}displayname' => $displayName, |
|
| 217 | - '{' . Plugin::NS_CARDDAV . '}addressbook-description' => $row['description'], |
|
| 218 | - '{http://calendarserver.org/ns/}getctag' => $row['synctoken'], |
|
| 219 | - '{http://sabredav.org/ns}sync-token' => $row['synctoken'] ?: '0', |
|
| 220 | - '{' . \OCA\DAV\DAV\Sharing\Plugin::NS_OWNCLOUD . '}owner-principal' => $row['principaluri'], |
|
| 221 | - $readOnlyPropertyName => $readOnly, |
|
| 222 | - ]; |
|
| 223 | - |
|
| 224 | - $this->addOwnerPrincipal($addressBooks[$row['id']]); |
|
| 225 | - } |
|
| 226 | - $result->closeCursor(); |
|
| 227 | - |
|
| 228 | - return array_values($addressBooks); |
|
| 229 | - } |
|
| 230 | - |
|
| 231 | - public function getUsersOwnAddressBooks($principalUri) { |
|
| 232 | - $principalUri = $this->convertPrincipal($principalUri, true); |
|
| 233 | - $query = $this->db->getQueryBuilder(); |
|
| 234 | - $query->select(['id', 'uri', 'displayname', 'principaluri', 'description', 'synctoken']) |
|
| 235 | - ->from('addressbooks') |
|
| 236 | - ->where($query->expr()->eq('principaluri', $query->createNamedParameter($principalUri))); |
|
| 237 | - |
|
| 238 | - $addressBooks = []; |
|
| 239 | - |
|
| 240 | - $result = $query->execute(); |
|
| 241 | - while ($row = $result->fetch()) { |
|
| 242 | - $addressBooks[$row['id']] = [ |
|
| 243 | - 'id' => $row['id'], |
|
| 244 | - 'uri' => $row['uri'], |
|
| 245 | - 'principaluri' => $this->convertPrincipal($row['principaluri'], false), |
|
| 246 | - '{DAV:}displayname' => $row['displayname'], |
|
| 247 | - '{' . Plugin::NS_CARDDAV . '}addressbook-description' => $row['description'], |
|
| 248 | - '{http://calendarserver.org/ns/}getctag' => $row['synctoken'], |
|
| 249 | - '{http://sabredav.org/ns}sync-token' => $row['synctoken'] ?: '0', |
|
| 250 | - ]; |
|
| 251 | - |
|
| 252 | - $this->addOwnerPrincipal($addressBooks[$row['id']]); |
|
| 253 | - } |
|
| 254 | - $result->closeCursor(); |
|
| 255 | - |
|
| 256 | - return array_values($addressBooks); |
|
| 257 | - } |
|
| 258 | - |
|
| 259 | - /** |
|
| 260 | - * @param int $addressBookId |
|
| 261 | - */ |
|
| 262 | - public function getAddressBookById(int $addressBookId): ?array { |
|
| 263 | - $query = $this->db->getQueryBuilder(); |
|
| 264 | - $result = $query->select(['id', 'uri', 'displayname', 'principaluri', 'description', 'synctoken']) |
|
| 265 | - ->from('addressbooks') |
|
| 266 | - ->where($query->expr()->eq('id', $query->createNamedParameter($addressBookId, IQueryBuilder::PARAM_INT))) |
|
| 267 | - ->executeQuery(); |
|
| 268 | - $row = $result->fetch(); |
|
| 269 | - $result->closeCursor(); |
|
| 270 | - if (!$row) { |
|
| 271 | - return null; |
|
| 272 | - } |
|
| 273 | - |
|
| 274 | - $addressBook = [ |
|
| 275 | - 'id' => $row['id'], |
|
| 276 | - 'uri' => $row['uri'], |
|
| 277 | - 'principaluri' => $row['principaluri'], |
|
| 278 | - '{DAV:}displayname' => $row['displayname'], |
|
| 279 | - '{' . Plugin::NS_CARDDAV . '}addressbook-description' => $row['description'], |
|
| 280 | - '{http://calendarserver.org/ns/}getctag' => $row['synctoken'], |
|
| 281 | - '{http://sabredav.org/ns}sync-token' => $row['synctoken'] ?: '0', |
|
| 282 | - ]; |
|
| 283 | - |
|
| 284 | - $this->addOwnerPrincipal($addressBook); |
|
| 285 | - |
|
| 286 | - return $addressBook; |
|
| 287 | - } |
|
| 288 | - |
|
| 289 | - public function getAddressBooksByUri(string $principal, string $addressBookUri): ?array { |
|
| 290 | - $query = $this->db->getQueryBuilder(); |
|
| 291 | - $result = $query->select(['id', 'uri', 'displayname', 'principaluri', 'description', 'synctoken']) |
|
| 292 | - ->from('addressbooks') |
|
| 293 | - ->where($query->expr()->eq('uri', $query->createNamedParameter($addressBookUri))) |
|
| 294 | - ->andWhere($query->expr()->eq('principaluri', $query->createNamedParameter($principal))) |
|
| 295 | - ->setMaxResults(1) |
|
| 296 | - ->executeQuery(); |
|
| 297 | - |
|
| 298 | - $row = $result->fetch(); |
|
| 299 | - $result->closeCursor(); |
|
| 300 | - if ($row === false) { |
|
| 301 | - return null; |
|
| 302 | - } |
|
| 303 | - |
|
| 304 | - $addressBook = [ |
|
| 305 | - 'id' => $row['id'], |
|
| 306 | - 'uri' => $row['uri'], |
|
| 307 | - 'principaluri' => $row['principaluri'], |
|
| 308 | - '{DAV:}displayname' => $row['displayname'], |
|
| 309 | - '{' . Plugin::NS_CARDDAV . '}addressbook-description' => $row['description'], |
|
| 310 | - '{http://calendarserver.org/ns/}getctag' => $row['synctoken'], |
|
| 311 | - '{http://sabredav.org/ns}sync-token' => $row['synctoken'] ?: '0', |
|
| 312 | - ]; |
|
| 313 | - |
|
| 314 | - $this->addOwnerPrincipal($addressBook); |
|
| 315 | - |
|
| 316 | - return $addressBook; |
|
| 317 | - } |
|
| 318 | - |
|
| 319 | - /** |
|
| 320 | - * Updates properties for an address book. |
|
| 321 | - * |
|
| 322 | - * The list of mutations is stored in a Sabre\DAV\PropPatch object. |
|
| 323 | - * To do the actual updates, you must tell this object which properties |
|
| 324 | - * you're going to process with the handle() method. |
|
| 325 | - * |
|
| 326 | - * Calling the handle method is like telling the PropPatch object "I |
|
| 327 | - * promise I can handle updating this property". |
|
| 328 | - * |
|
| 329 | - * Read the PropPatch documentation for more info and examples. |
|
| 330 | - * |
|
| 331 | - * @param string $addressBookId |
|
| 332 | - * @param \Sabre\DAV\PropPatch $propPatch |
|
| 333 | - * @return void |
|
| 334 | - */ |
|
| 335 | - public function updateAddressBook($addressBookId, \Sabre\DAV\PropPatch $propPatch) { |
|
| 336 | - $supportedProperties = [ |
|
| 337 | - '{DAV:}displayname', |
|
| 338 | - '{' . Plugin::NS_CARDDAV . '}addressbook-description', |
|
| 339 | - ]; |
|
| 340 | - |
|
| 341 | - $propPatch->handle($supportedProperties, function ($mutations) use ($addressBookId) { |
|
| 342 | - $updates = []; |
|
| 343 | - foreach ($mutations as $property => $newValue) { |
|
| 344 | - switch ($property) { |
|
| 345 | - case '{DAV:}displayname': |
|
| 346 | - $updates['displayname'] = $newValue; |
|
| 347 | - break; |
|
| 348 | - case '{' . Plugin::NS_CARDDAV . '}addressbook-description': |
|
| 349 | - $updates['description'] = $newValue; |
|
| 350 | - break; |
|
| 351 | - } |
|
| 352 | - } |
|
| 353 | - $query = $this->db->getQueryBuilder(); |
|
| 354 | - $query->update('addressbooks'); |
|
| 355 | - |
|
| 356 | - foreach ($updates as $key => $value) { |
|
| 357 | - $query->set($key, $query->createNamedParameter($value)); |
|
| 358 | - } |
|
| 359 | - $query->where($query->expr()->eq('id', $query->createNamedParameter($addressBookId))) |
|
| 360 | - ->executeStatement(); |
|
| 361 | - |
|
| 362 | - $this->addChange($addressBookId, "", 2); |
|
| 363 | - |
|
| 364 | - $addressBookRow = $this->getAddressBookById((int)$addressBookId); |
|
| 365 | - $shares = $this->getShares((int)$addressBookId); |
|
| 366 | - $this->dispatcher->dispatchTyped(new AddressBookUpdatedEvent((int)$addressBookId, $addressBookRow, $shares, $mutations)); |
|
| 367 | - |
|
| 368 | - return true; |
|
| 369 | - }); |
|
| 370 | - } |
|
| 371 | - |
|
| 372 | - /** |
|
| 373 | - * Creates a new address book |
|
| 374 | - * |
|
| 375 | - * @param string $principalUri |
|
| 376 | - * @param string $url Just the 'basename' of the url. |
|
| 377 | - * @param array $properties |
|
| 378 | - * @return int |
|
| 379 | - * @throws BadRequest |
|
| 380 | - */ |
|
| 381 | - public function createAddressBook($principalUri, $url, array $properties) { |
|
| 382 | - if (strlen($url) > 255) { |
|
| 383 | - throw new BadRequest('URI too long. Address book not created'); |
|
| 384 | - } |
|
| 385 | - |
|
| 386 | - $values = [ |
|
| 387 | - 'displayname' => null, |
|
| 388 | - 'description' => null, |
|
| 389 | - 'principaluri' => $principalUri, |
|
| 390 | - 'uri' => $url, |
|
| 391 | - 'synctoken' => 1 |
|
| 392 | - ]; |
|
| 393 | - |
|
| 394 | - foreach ($properties as $property => $newValue) { |
|
| 395 | - switch ($property) { |
|
| 396 | - case '{DAV:}displayname': |
|
| 397 | - $values['displayname'] = $newValue; |
|
| 398 | - break; |
|
| 399 | - case '{' . Plugin::NS_CARDDAV . '}addressbook-description': |
|
| 400 | - $values['description'] = $newValue; |
|
| 401 | - break; |
|
| 402 | - default: |
|
| 403 | - throw new BadRequest('Unknown property: ' . $property); |
|
| 404 | - } |
|
| 405 | - } |
|
| 406 | - |
|
| 407 | - // Fallback to make sure the displayname is set. Some clients may refuse |
|
| 408 | - // to work with addressbooks not having a displayname. |
|
| 409 | - if (is_null($values['displayname'])) { |
|
| 410 | - $values['displayname'] = $url; |
|
| 411 | - } |
|
| 412 | - |
|
| 413 | - [$addressBookId, $addressBookRow] = $this->atomic(function() use ($values) { |
|
| 414 | - $query = $this->db->getQueryBuilder(); |
|
| 415 | - $query->insert('addressbooks') |
|
| 416 | - ->values([ |
|
| 417 | - 'uri' => $query->createParameter('uri'), |
|
| 418 | - 'displayname' => $query->createParameter('displayname'), |
|
| 419 | - 'description' => $query->createParameter('description'), |
|
| 420 | - 'principaluri' => $query->createParameter('principaluri'), |
|
| 421 | - 'synctoken' => $query->createParameter('synctoken'), |
|
| 422 | - ]) |
|
| 423 | - ->setParameters($values) |
|
| 424 | - ->execute(); |
|
| 425 | - |
|
| 426 | - $addressBookId = $query->getLastInsertId(); |
|
| 427 | - return [ |
|
| 428 | - $addressBookId, |
|
| 429 | - $this->getAddressBookById($addressBookId), |
|
| 430 | - ]; |
|
| 431 | - }, $this->db); |
|
| 432 | - |
|
| 433 | - $this->dispatcher->dispatchTyped(new AddressBookCreatedEvent($addressBookId, $addressBookRow)); |
|
| 434 | - |
|
| 435 | - return $addressBookId; |
|
| 436 | - } |
|
| 437 | - |
|
| 438 | - /** |
|
| 439 | - * Deletes an entire addressbook and all its contents |
|
| 440 | - * |
|
| 441 | - * @param mixed $addressBookId |
|
| 442 | - * @return void |
|
| 443 | - */ |
|
| 444 | - public function deleteAddressBook($addressBookId) { |
|
| 445 | - $addressBookId = (int)$addressBookId; |
|
| 446 | - $addressBookData = $this->getAddressBookById($addressBookId); |
|
| 447 | - $shares = $this->getShares($addressBookId); |
|
| 448 | - |
|
| 449 | - $query = $this->db->getQueryBuilder(); |
|
| 450 | - $query->delete($this->dbCardsTable) |
|
| 451 | - ->where($query->expr()->eq('addressbookid', $query->createParameter('addressbookid'))) |
|
| 452 | - ->setParameter('addressbookid', $addressBookId, IQueryBuilder::PARAM_INT) |
|
| 453 | - ->executeStatement(); |
|
| 454 | - |
|
| 455 | - $query = $this->db->getQueryBuilder(); |
|
| 456 | - $query->delete('addressbookchanges') |
|
| 457 | - ->where($query->expr()->eq('addressbookid', $query->createParameter('addressbookid'))) |
|
| 458 | - ->setParameter('addressbookid', $addressBookId, IQueryBuilder::PARAM_INT) |
|
| 459 | - ->executeStatement(); |
|
| 460 | - |
|
| 461 | - $query = $this->db->getQueryBuilder(); |
|
| 462 | - $query->delete('addressbooks') |
|
| 463 | - ->where($query->expr()->eq('id', $query->createParameter('id'))) |
|
| 464 | - ->setParameter('id', $addressBookId, IQueryBuilder::PARAM_INT) |
|
| 465 | - ->executeStatement(); |
|
| 466 | - |
|
| 467 | - $this->sharingBackend->deleteAllShares($addressBookId); |
|
| 468 | - |
|
| 469 | - $query = $this->db->getQueryBuilder(); |
|
| 470 | - $query->delete($this->dbCardsPropertiesTable) |
|
| 471 | - ->where($query->expr()->eq('addressbookid', $query->createNamedParameter($addressBookId, IQueryBuilder::PARAM_INT))) |
|
| 472 | - ->executeStatement(); |
|
| 473 | - |
|
| 474 | - if ($addressBookData) { |
|
| 475 | - $this->dispatcher->dispatchTyped(new AddressBookDeletedEvent($addressBookId, $addressBookData, $shares)); |
|
| 476 | - } |
|
| 477 | - } |
|
| 478 | - |
|
| 479 | - /** |
|
| 480 | - * Returns all cards for a specific addressbook id. |
|
| 481 | - * |
|
| 482 | - * This method should return the following properties for each card: |
|
| 483 | - * * carddata - raw vcard data |
|
| 484 | - * * uri - Some unique url |
|
| 485 | - * * lastmodified - A unix timestamp |
|
| 486 | - * |
|
| 487 | - * It's recommended to also return the following properties: |
|
| 488 | - * * etag - A unique etag. This must change every time the card changes. |
|
| 489 | - * * size - The size of the card in bytes. |
|
| 490 | - * |
|
| 491 | - * If these last two properties are provided, less time will be spent |
|
| 492 | - * calculating them. If they are specified, you can also omit carddata. |
|
| 493 | - * This may speed up certain requests, especially with large cards. |
|
| 494 | - * |
|
| 495 | - * @param mixed $addressbookId |
|
| 496 | - * @return array |
|
| 497 | - */ |
|
| 498 | - public function getCards($addressbookId) { |
|
| 499 | - $query = $this->db->getQueryBuilder(); |
|
| 500 | - $query->select(['id', 'uri', 'lastmodified', 'etag', 'size', 'carddata', 'uid']) |
|
| 501 | - ->from($this->dbCardsTable) |
|
| 502 | - ->where($query->expr()->eq('addressbookid', $query->createNamedParameter($addressbookId))); |
|
| 503 | - |
|
| 504 | - $cards = []; |
|
| 505 | - |
|
| 506 | - $result = $query->execute(); |
|
| 507 | - while ($row = $result->fetch()) { |
|
| 508 | - $row['etag'] = '"' . $row['etag'] . '"'; |
|
| 509 | - |
|
| 510 | - $modified = false; |
|
| 511 | - $row['carddata'] = $this->readBlob($row['carddata'], $modified); |
|
| 512 | - if ($modified) { |
|
| 513 | - $row['size'] = strlen($row['carddata']); |
|
| 514 | - } |
|
| 515 | - |
|
| 516 | - $cards[] = $row; |
|
| 517 | - } |
|
| 518 | - $result->closeCursor(); |
|
| 519 | - |
|
| 520 | - return $cards; |
|
| 521 | - } |
|
| 522 | - |
|
| 523 | - /** |
|
| 524 | - * Returns a specific card. |
|
| 525 | - * |
|
| 526 | - * The same set of properties must be returned as with getCards. The only |
|
| 527 | - * exception is that 'carddata' is absolutely required. |
|
| 528 | - * |
|
| 529 | - * If the card does not exist, you must return false. |
|
| 530 | - * |
|
| 531 | - * @param mixed $addressBookId |
|
| 532 | - * @param string $cardUri |
|
| 533 | - * @return array |
|
| 534 | - */ |
|
| 535 | - public function getCard($addressBookId, $cardUri) { |
|
| 536 | - $query = $this->db->getQueryBuilder(); |
|
| 537 | - $query->select(['id', 'uri', 'lastmodified', 'etag', 'size', 'carddata', 'uid']) |
|
| 538 | - ->from($this->dbCardsTable) |
|
| 539 | - ->where($query->expr()->eq('addressbookid', $query->createNamedParameter($addressBookId))) |
|
| 540 | - ->andWhere($query->expr()->eq('uri', $query->createNamedParameter($cardUri))) |
|
| 541 | - ->setMaxResults(1); |
|
| 542 | - |
|
| 543 | - $result = $query->execute(); |
|
| 544 | - $row = $result->fetch(); |
|
| 545 | - if (!$row) { |
|
| 546 | - return false; |
|
| 547 | - } |
|
| 548 | - $row['etag'] = '"' . $row['etag'] . '"'; |
|
| 549 | - |
|
| 550 | - $modified = false; |
|
| 551 | - $row['carddata'] = $this->readBlob($row['carddata'], $modified); |
|
| 552 | - if ($modified) { |
|
| 553 | - $row['size'] = strlen($row['carddata']); |
|
| 554 | - } |
|
| 555 | - |
|
| 556 | - return $row; |
|
| 557 | - } |
|
| 558 | - |
|
| 559 | - /** |
|
| 560 | - * Returns a list of cards. |
|
| 561 | - * |
|
| 562 | - * This method should work identical to getCard, but instead return all the |
|
| 563 | - * cards in the list as an array. |
|
| 564 | - * |
|
| 565 | - * If the backend supports this, it may allow for some speed-ups. |
|
| 566 | - * |
|
| 567 | - * @param mixed $addressBookId |
|
| 568 | - * @param array $uris |
|
| 569 | - * @return array |
|
| 570 | - */ |
|
| 571 | - public function getMultipleCards($addressBookId, array $uris) { |
|
| 572 | - if (empty($uris)) { |
|
| 573 | - return []; |
|
| 574 | - } |
|
| 575 | - |
|
| 576 | - $chunks = array_chunk($uris, 100); |
|
| 577 | - $cards = []; |
|
| 578 | - |
|
| 579 | - $query = $this->db->getQueryBuilder(); |
|
| 580 | - $query->select(['id', 'uri', 'lastmodified', 'etag', 'size', 'carddata', 'uid']) |
|
| 581 | - ->from($this->dbCardsTable) |
|
| 582 | - ->where($query->expr()->eq('addressbookid', $query->createNamedParameter($addressBookId))) |
|
| 583 | - ->andWhere($query->expr()->in('uri', $query->createParameter('uri'))); |
|
| 584 | - |
|
| 585 | - foreach ($chunks as $uris) { |
|
| 586 | - $query->setParameter('uri', $uris, IQueryBuilder::PARAM_STR_ARRAY); |
|
| 587 | - $result = $query->execute(); |
|
| 588 | - |
|
| 589 | - while ($row = $result->fetch()) { |
|
| 590 | - $row['etag'] = '"' . $row['etag'] . '"'; |
|
| 591 | - |
|
| 592 | - $modified = false; |
|
| 593 | - $row['carddata'] = $this->readBlob($row['carddata'], $modified); |
|
| 594 | - if ($modified) { |
|
| 595 | - $row['size'] = strlen($row['carddata']); |
|
| 596 | - } |
|
| 597 | - |
|
| 598 | - $cards[] = $row; |
|
| 599 | - } |
|
| 600 | - $result->closeCursor(); |
|
| 601 | - } |
|
| 602 | - return $cards; |
|
| 603 | - } |
|
| 604 | - |
|
| 605 | - /** |
|
| 606 | - * Creates a new card. |
|
| 607 | - * |
|
| 608 | - * The addressbook id will be passed as the first argument. This is the |
|
| 609 | - * same id as it is returned from the getAddressBooksForUser method. |
|
| 610 | - * |
|
| 611 | - * The cardUri is a base uri, and doesn't include the full path. The |
|
| 612 | - * cardData argument is the vcard body, and is passed as a string. |
|
| 613 | - * |
|
| 614 | - * It is possible to return an ETag from this method. This ETag is for the |
|
| 615 | - * newly created resource, and must be enclosed with double quotes (that |
|
| 616 | - * is, the string itself must contain the double quotes). |
|
| 617 | - * |
|
| 618 | - * You should only return the ETag if you store the carddata as-is. If a |
|
| 619 | - * subsequent GET request on the same card does not have the same body, |
|
| 620 | - * byte-by-byte and you did return an ETag here, clients tend to get |
|
| 621 | - * confused. |
|
| 622 | - * |
|
| 623 | - * If you don't return an ETag, you can just return null. |
|
| 624 | - * |
|
| 625 | - * @param mixed $addressBookId |
|
| 626 | - * @param string $cardUri |
|
| 627 | - * @param string $cardData |
|
| 628 | - * @param bool $checkAlreadyExists |
|
| 629 | - * @return string |
|
| 630 | - */ |
|
| 631 | - public function createCard($addressBookId, $cardUri, $cardData, bool $checkAlreadyExists = true) { |
|
| 632 | - $etag = md5($cardData); |
|
| 633 | - $uid = $this->getUID($cardData); |
|
| 634 | - |
|
| 635 | - if ($checkAlreadyExists) { |
|
| 636 | - $q = $this->db->getQueryBuilder(); |
|
| 637 | - $q->select('uid') |
|
| 638 | - ->from($this->dbCardsTable) |
|
| 639 | - ->where($q->expr()->eq('addressbookid', $q->createNamedParameter($addressBookId))) |
|
| 640 | - ->andWhere($q->expr()->eq('uid', $q->createNamedParameter($uid))) |
|
| 641 | - ->setMaxResults(1); |
|
| 642 | - $result = $q->executeQuery(); |
|
| 643 | - $count = (bool)$result->fetchOne(); |
|
| 644 | - $result->closeCursor(); |
|
| 645 | - if ($count) { |
|
| 646 | - throw new \Sabre\DAV\Exception\BadRequest('VCard object with uid already exists in this addressbook collection.'); |
|
| 647 | - } |
|
| 648 | - } |
|
| 649 | - |
|
| 650 | - $query = $this->db->getQueryBuilder(); |
|
| 651 | - $query->insert('cards') |
|
| 652 | - ->values([ |
|
| 653 | - 'carddata' => $query->createNamedParameter($cardData, IQueryBuilder::PARAM_LOB), |
|
| 654 | - 'uri' => $query->createNamedParameter($cardUri), |
|
| 655 | - 'lastmodified' => $query->createNamedParameter(time()), |
|
| 656 | - 'addressbookid' => $query->createNamedParameter($addressBookId), |
|
| 657 | - 'size' => $query->createNamedParameter(strlen($cardData)), |
|
| 658 | - 'etag' => $query->createNamedParameter($etag), |
|
| 659 | - 'uid' => $query->createNamedParameter($uid), |
|
| 660 | - ]) |
|
| 661 | - ->execute(); |
|
| 662 | - |
|
| 663 | - $etagCacheKey = "$addressBookId#$cardUri"; |
|
| 664 | - $this->etagCache[$etagCacheKey] = $etag; |
|
| 665 | - |
|
| 666 | - $this->addChange($addressBookId, $cardUri, 1); |
|
| 667 | - $this->updateProperties($addressBookId, $cardUri, $cardData); |
|
| 668 | - |
|
| 669 | - $addressBookData = $this->getAddressBookById($addressBookId); |
|
| 670 | - $shares = $this->getShares($addressBookId); |
|
| 671 | - $objectRow = $this->getCard($addressBookId, $cardUri); |
|
| 672 | - $this->dispatcher->dispatchTyped(new CardCreatedEvent($addressBookId, $addressBookData, $shares, $objectRow)); |
|
| 673 | - |
|
| 674 | - return '"' . $etag . '"'; |
|
| 675 | - } |
|
| 676 | - |
|
| 677 | - /** |
|
| 678 | - * Updates a card. |
|
| 679 | - * |
|
| 680 | - * The addressbook id will be passed as the first argument. This is the |
|
| 681 | - * same id as it is returned from the getAddressBooksForUser method. |
|
| 682 | - * |
|
| 683 | - * The cardUri is a base uri, and doesn't include the full path. The |
|
| 684 | - * cardData argument is the vcard body, and is passed as a string. |
|
| 685 | - * |
|
| 686 | - * It is possible to return an ETag from this method. This ETag should |
|
| 687 | - * match that of the updated resource, and must be enclosed with double |
|
| 688 | - * quotes (that is: the string itself must contain the actual quotes). |
|
| 689 | - * |
|
| 690 | - * You should only return the ETag if you store the carddata as-is. If a |
|
| 691 | - * subsequent GET request on the same card does not have the same body, |
|
| 692 | - * byte-by-byte and you did return an ETag here, clients tend to get |
|
| 693 | - * confused. |
|
| 694 | - * |
|
| 695 | - * If you don't return an ETag, you can just return null. |
|
| 696 | - * |
|
| 697 | - * @param mixed $addressBookId |
|
| 698 | - * @param string $cardUri |
|
| 699 | - * @param string $cardData |
|
| 700 | - * @return string |
|
| 701 | - */ |
|
| 702 | - public function updateCard($addressBookId, $cardUri, $cardData) { |
|
| 703 | - $uid = $this->getUID($cardData); |
|
| 704 | - $etag = md5($cardData); |
|
| 705 | - $query = $this->db->getQueryBuilder(); |
|
| 706 | - |
|
| 707 | - // check for recently stored etag and stop if it is the same |
|
| 708 | - $etagCacheKey = "$addressBookId#$cardUri"; |
|
| 709 | - if (isset($this->etagCache[$etagCacheKey]) && $this->etagCache[$etagCacheKey] === $etag) { |
|
| 710 | - return '"' . $etag . '"'; |
|
| 711 | - } |
|
| 712 | - |
|
| 713 | - $query->update($this->dbCardsTable) |
|
| 714 | - ->set('carddata', $query->createNamedParameter($cardData, IQueryBuilder::PARAM_LOB)) |
|
| 715 | - ->set('lastmodified', $query->createNamedParameter(time())) |
|
| 716 | - ->set('size', $query->createNamedParameter(strlen($cardData))) |
|
| 717 | - ->set('etag', $query->createNamedParameter($etag)) |
|
| 718 | - ->set('uid', $query->createNamedParameter($uid)) |
|
| 719 | - ->where($query->expr()->eq('uri', $query->createNamedParameter($cardUri))) |
|
| 720 | - ->andWhere($query->expr()->eq('addressbookid', $query->createNamedParameter($addressBookId))) |
|
| 721 | - ->execute(); |
|
| 722 | - |
|
| 723 | - $this->etagCache[$etagCacheKey] = $etag; |
|
| 724 | - |
|
| 725 | - $this->addChange($addressBookId, $cardUri, 2); |
|
| 726 | - $this->updateProperties($addressBookId, $cardUri, $cardData); |
|
| 727 | - |
|
| 728 | - $addressBookData = $this->getAddressBookById($addressBookId); |
|
| 729 | - $shares = $this->getShares($addressBookId); |
|
| 730 | - $objectRow = $this->getCard($addressBookId, $cardUri); |
|
| 731 | - $this->dispatcher->dispatchTyped(new CardUpdatedEvent($addressBookId, $addressBookData, $shares, $objectRow)); |
|
| 732 | - return '"' . $etag . '"'; |
|
| 733 | - } |
|
| 734 | - |
|
| 735 | - /** |
|
| 736 | - * Deletes a card |
|
| 737 | - * |
|
| 738 | - * @param mixed $addressBookId |
|
| 739 | - * @param string $cardUri |
|
| 740 | - * @return bool |
|
| 741 | - */ |
|
| 742 | - public function deleteCard($addressBookId, $cardUri) { |
|
| 743 | - $addressBookData = $this->getAddressBookById($addressBookId); |
|
| 744 | - $shares = $this->getShares($addressBookId); |
|
| 745 | - $objectRow = $this->getCard($addressBookId, $cardUri); |
|
| 746 | - |
|
| 747 | - try { |
|
| 748 | - $cardId = $this->getCardId($addressBookId, $cardUri); |
|
| 749 | - } catch (\InvalidArgumentException $e) { |
|
| 750 | - $cardId = null; |
|
| 751 | - } |
|
| 752 | - $query = $this->db->getQueryBuilder(); |
|
| 753 | - $ret = $query->delete($this->dbCardsTable) |
|
| 754 | - ->where($query->expr()->eq('addressbookid', $query->createNamedParameter($addressBookId))) |
|
| 755 | - ->andWhere($query->expr()->eq('uri', $query->createNamedParameter($cardUri))) |
|
| 756 | - ->executeStatement(); |
|
| 757 | - |
|
| 758 | - $this->addChange($addressBookId, $cardUri, 3); |
|
| 759 | - |
|
| 760 | - if ($ret === 1) { |
|
| 761 | - if ($cardId !== null) { |
|
| 762 | - $this->dispatcher->dispatchTyped(new CardDeletedEvent($addressBookId, $addressBookData, $shares, $objectRow)); |
|
| 763 | - $this->purgeProperties($addressBookId, $cardId); |
|
| 764 | - } |
|
| 765 | - return true; |
|
| 766 | - } |
|
| 767 | - |
|
| 768 | - return false; |
|
| 769 | - } |
|
| 770 | - |
|
| 771 | - /** |
|
| 772 | - * The getChanges method returns all the changes that have happened, since |
|
| 773 | - * the specified syncToken in the specified address book. |
|
| 774 | - * |
|
| 775 | - * This function should return an array, such as the following: |
|
| 776 | - * |
|
| 777 | - * [ |
|
| 778 | - * 'syncToken' => 'The current synctoken', |
|
| 779 | - * 'added' => [ |
|
| 780 | - * 'new.txt', |
|
| 781 | - * ], |
|
| 782 | - * 'modified' => [ |
|
| 783 | - * 'modified.txt', |
|
| 784 | - * ], |
|
| 785 | - * 'deleted' => [ |
|
| 786 | - * 'foo.php.bak', |
|
| 787 | - * 'old.txt' |
|
| 788 | - * ] |
|
| 789 | - * ]; |
|
| 790 | - * |
|
| 791 | - * The returned syncToken property should reflect the *current* syncToken |
|
| 792 | - * of the calendar, as reported in the {http://sabredav.org/ns}sync-token |
|
| 793 | - * property. This is needed here too, to ensure the operation is atomic. |
|
| 794 | - * |
|
| 795 | - * If the $syncToken argument is specified as null, this is an initial |
|
| 796 | - * sync, and all members should be reported. |
|
| 797 | - * |
|
| 798 | - * The modified property is an array of nodenames that have changed since |
|
| 799 | - * the last token. |
|
| 800 | - * |
|
| 801 | - * The deleted property is an array with nodenames, that have been deleted |
|
| 802 | - * from collection. |
|
| 803 | - * |
|
| 804 | - * The $syncLevel argument is basically the 'depth' of the report. If it's |
|
| 805 | - * 1, you only have to report changes that happened only directly in |
|
| 806 | - * immediate descendants. If it's 2, it should also include changes from |
|
| 807 | - * the nodes below the child collections. (grandchildren) |
|
| 808 | - * |
|
| 809 | - * The $limit argument allows a client to specify how many results should |
|
| 810 | - * be returned at most. If the limit is not specified, it should be treated |
|
| 811 | - * as infinite. |
|
| 812 | - * |
|
| 813 | - * If the limit (infinite or not) is higher than you're willing to return, |
|
| 814 | - * you should throw a Sabre\DAV\Exception\TooMuchMatches() exception. |
|
| 815 | - * |
|
| 816 | - * If the syncToken is expired (due to data cleanup) or unknown, you must |
|
| 817 | - * return null. |
|
| 818 | - * |
|
| 819 | - * The limit is 'suggestive'. You are free to ignore it. |
|
| 820 | - * |
|
| 821 | - * @param string $addressBookId |
|
| 822 | - * @param string $syncToken |
|
| 823 | - * @param int $syncLevel |
|
| 824 | - * @param int|null $limit |
|
| 825 | - * @return array |
|
| 826 | - */ |
|
| 827 | - public function getChangesForAddressBook($addressBookId, $syncToken, $syncLevel, $limit = null) { |
|
| 828 | - // Current synctoken |
|
| 829 | - $qb = $this->db->getQueryBuilder(); |
|
| 830 | - $qb->select('synctoken') |
|
| 831 | - ->from('addressbooks') |
|
| 832 | - ->where( |
|
| 833 | - $qb->expr()->eq('id', $qb->createNamedParameter($addressBookId)) |
|
| 834 | - ); |
|
| 835 | - $stmt = $qb->executeQuery(); |
|
| 836 | - $currentToken = $stmt->fetchOne(); |
|
| 837 | - $stmt->closeCursor(); |
|
| 838 | - |
|
| 839 | - if (is_null($currentToken)) { |
|
| 840 | - return []; |
|
| 841 | - } |
|
| 842 | - |
|
| 843 | - $result = [ |
|
| 844 | - 'syncToken' => $currentToken, |
|
| 845 | - 'added' => [], |
|
| 846 | - 'modified' => [], |
|
| 847 | - 'deleted' => [], |
|
| 848 | - ]; |
|
| 849 | - |
|
| 850 | - if ($syncToken) { |
|
| 851 | - $qb = $this->db->getQueryBuilder(); |
|
| 852 | - $qb->select('uri', 'operation') |
|
| 853 | - ->from('addressbookchanges') |
|
| 854 | - ->where( |
|
| 855 | - $qb->expr()->andX( |
|
| 856 | - $qb->expr()->gte('synctoken', $qb->createNamedParameter($syncToken)), |
|
| 857 | - $qb->expr()->lt('synctoken', $qb->createNamedParameter($currentToken)), |
|
| 858 | - $qb->expr()->eq('addressbookid', $qb->createNamedParameter($addressBookId)) |
|
| 859 | - ) |
|
| 860 | - )->orderBy('synctoken'); |
|
| 861 | - |
|
| 862 | - if (is_int($limit) && $limit > 0) { |
|
| 863 | - $qb->setMaxResults($limit); |
|
| 864 | - } |
|
| 865 | - |
|
| 866 | - // Fetching all changes |
|
| 867 | - $stmt = $qb->executeQuery(); |
|
| 868 | - |
|
| 869 | - $changes = []; |
|
| 870 | - |
|
| 871 | - // This loop ensures that any duplicates are overwritten, only the |
|
| 872 | - // last change on a node is relevant. |
|
| 873 | - while ($row = $stmt->fetch(\PDO::FETCH_ASSOC)) { |
|
| 874 | - $changes[$row['uri']] = $row['operation']; |
|
| 875 | - } |
|
| 876 | - $stmt->closeCursor(); |
|
| 877 | - |
|
| 878 | - foreach ($changes as $uri => $operation) { |
|
| 879 | - switch ($operation) { |
|
| 880 | - case 1: |
|
| 881 | - $result['added'][] = $uri; |
|
| 882 | - break; |
|
| 883 | - case 2: |
|
| 884 | - $result['modified'][] = $uri; |
|
| 885 | - break; |
|
| 886 | - case 3: |
|
| 887 | - $result['deleted'][] = $uri; |
|
| 888 | - break; |
|
| 889 | - } |
|
| 890 | - } |
|
| 891 | - } else { |
|
| 892 | - $qb = $this->db->getQueryBuilder(); |
|
| 893 | - $qb->select('uri') |
|
| 894 | - ->from('cards') |
|
| 895 | - ->where( |
|
| 896 | - $qb->expr()->eq('addressbookid', $qb->createNamedParameter($addressBookId)) |
|
| 897 | - ); |
|
| 898 | - // No synctoken supplied, this is the initial sync. |
|
| 899 | - $stmt = $qb->executeQuery(); |
|
| 900 | - $result['added'] = $stmt->fetchAll(\PDO::FETCH_COLUMN); |
|
| 901 | - $stmt->closeCursor(); |
|
| 902 | - } |
|
| 903 | - return $result; |
|
| 904 | - } |
|
| 905 | - |
|
| 906 | - /** |
|
| 907 | - * Adds a change record to the addressbookchanges table. |
|
| 908 | - * |
|
| 909 | - * @param mixed $addressBookId |
|
| 910 | - * @param string $objectUri |
|
| 911 | - * @param int $operation 1 = add, 2 = modify, 3 = delete |
|
| 912 | - * @return void |
|
| 913 | - */ |
|
| 914 | - protected function addChange($addressBookId, $objectUri, $operation) { |
|
| 915 | - $sql = 'INSERT INTO `*PREFIX*addressbookchanges`(`uri`, `synctoken`, `addressbookid`, `operation`) SELECT ?, `synctoken`, ?, ? FROM `*PREFIX*addressbooks` WHERE `id` = ?'; |
|
| 916 | - $stmt = $this->db->prepare($sql); |
|
| 917 | - $stmt->execute([ |
|
| 918 | - $objectUri, |
|
| 919 | - $addressBookId, |
|
| 920 | - $operation, |
|
| 921 | - $addressBookId |
|
| 922 | - ]); |
|
| 923 | - $stmt = $this->db->prepare('UPDATE `*PREFIX*addressbooks` SET `synctoken` = `synctoken` + 1 WHERE `id` = ?'); |
|
| 924 | - $stmt->execute([ |
|
| 925 | - $addressBookId |
|
| 926 | - ]); |
|
| 927 | - } |
|
| 928 | - |
|
| 929 | - /** |
|
| 930 | - * @param resource|string $cardData |
|
| 931 | - * @param bool $modified |
|
| 932 | - * @return string |
|
| 933 | - */ |
|
| 934 | - private function readBlob($cardData, &$modified = false) { |
|
| 935 | - if (is_resource($cardData)) { |
|
| 936 | - $cardData = stream_get_contents($cardData); |
|
| 937 | - } |
|
| 938 | - |
|
| 939 | - // Micro optimisation |
|
| 940 | - // don't loop through |
|
| 941 | - if (strpos($cardData, 'PHOTO:data:') === 0) { |
|
| 942 | - return $cardData; |
|
| 943 | - } |
|
| 944 | - |
|
| 945 | - $cardDataArray = explode("\r\n", $cardData); |
|
| 946 | - |
|
| 947 | - $cardDataFiltered = []; |
|
| 948 | - $removingPhoto = false; |
|
| 949 | - foreach ($cardDataArray as $line) { |
|
| 950 | - if (strpos($line, 'PHOTO:data:') === 0 |
|
| 951 | - && strpos($line, 'PHOTO:data:image/') !== 0) { |
|
| 952 | - // Filter out PHOTO data of non-images |
|
| 953 | - $removingPhoto = true; |
|
| 954 | - $modified = true; |
|
| 955 | - continue; |
|
| 956 | - } |
|
| 957 | - |
|
| 958 | - if ($removingPhoto) { |
|
| 959 | - if (strpos($line, ' ') === 0) { |
|
| 960 | - continue; |
|
| 961 | - } |
|
| 962 | - // No leading space means this is a new property |
|
| 963 | - $removingPhoto = false; |
|
| 964 | - } |
|
| 965 | - |
|
| 966 | - $cardDataFiltered[] = $line; |
|
| 967 | - } |
|
| 968 | - return implode("\r\n", $cardDataFiltered); |
|
| 969 | - } |
|
| 970 | - |
|
| 971 | - /** |
|
| 972 | - * @param list<array{href: string, commonName: string, readOnly: bool}> $add |
|
| 973 | - * @param list<string> $remove |
|
| 974 | - */ |
|
| 975 | - public function updateShares(IShareable $shareable, array $add, array $remove): void { |
|
| 976 | - $addressBookId = $shareable->getResourceId(); |
|
| 977 | - $addressBookData = $this->getAddressBookById($addressBookId); |
|
| 978 | - $oldShares = $this->getShares($addressBookId); |
|
| 979 | - |
|
| 980 | - $this->sharingBackend->updateShares($shareable, $add, $remove); |
|
| 981 | - |
|
| 982 | - $this->dispatcher->dispatchTyped(new AddressBookShareUpdatedEvent($addressBookId, $addressBookData, $oldShares, $add, $remove)); |
|
| 983 | - } |
|
| 984 | - |
|
| 985 | - /** |
|
| 986 | - * Search contacts in a specific address-book |
|
| 987 | - * |
|
| 988 | - * @param int $addressBookId |
|
| 989 | - * @param string $pattern which should match within the $searchProperties |
|
| 990 | - * @param array $searchProperties defines the properties within the query pattern should match |
|
| 991 | - * @param array $options = array() to define the search behavior |
|
| 992 | - * - 'escape_like_param' - If set to false wildcards _ and % are not escaped, otherwise they are |
|
| 993 | - * - 'limit' - Set a numeric limit for the search results |
|
| 994 | - * - 'offset' - Set the offset for the limited search results |
|
| 995 | - * - 'wildcard' - Whether the search should use wildcards |
|
| 996 | - * @psalm-param array{escape_like_param?: bool, limit?: int, offset?: int, wildcard?: bool} $options |
|
| 997 | - * @return array an array of contacts which are arrays of key-value-pairs |
|
| 998 | - */ |
|
| 999 | - public function search($addressBookId, $pattern, $searchProperties, $options = []): array { |
|
| 1000 | - return $this->searchByAddressBookIds([$addressBookId], $pattern, $searchProperties, $options); |
|
| 1001 | - } |
|
| 1002 | - |
|
| 1003 | - /** |
|
| 1004 | - * Search contacts in all address-books accessible by a user |
|
| 1005 | - * |
|
| 1006 | - * @param string $principalUri |
|
| 1007 | - * @param string $pattern |
|
| 1008 | - * @param array $searchProperties |
|
| 1009 | - * @param array $options |
|
| 1010 | - * @return array |
|
| 1011 | - */ |
|
| 1012 | - public function searchPrincipalUri(string $principalUri, |
|
| 1013 | - string $pattern, |
|
| 1014 | - array $searchProperties, |
|
| 1015 | - array $options = []): array { |
|
| 1016 | - $addressBookIds = array_map(static function ($row):int { |
|
| 1017 | - return (int) $row['id']; |
|
| 1018 | - }, $this->getAddressBooksForUser($principalUri)); |
|
| 1019 | - |
|
| 1020 | - return $this->searchByAddressBookIds($addressBookIds, $pattern, $searchProperties, $options); |
|
| 1021 | - } |
|
| 1022 | - |
|
| 1023 | - /** |
|
| 1024 | - * @param array $addressBookIds |
|
| 1025 | - * @param string $pattern |
|
| 1026 | - * @param array $searchProperties |
|
| 1027 | - * @param array $options |
|
| 1028 | - * @psalm-param array{types?: bool, escape_like_param?: bool, limit?: int, offset?: int, wildcard?: bool} $options |
|
| 1029 | - * @return array |
|
| 1030 | - */ |
|
| 1031 | - private function searchByAddressBookIds(array $addressBookIds, |
|
| 1032 | - string $pattern, |
|
| 1033 | - array $searchProperties, |
|
| 1034 | - array $options = []): array { |
|
| 1035 | - $escapePattern = !\array_key_exists('escape_like_param', $options) || $options['escape_like_param'] !== false; |
|
| 1036 | - $useWildcards = !\array_key_exists('wildcard', $options) || $options['wildcard'] !== false; |
|
| 1037 | - |
|
| 1038 | - $query2 = $this->db->getQueryBuilder(); |
|
| 1039 | - |
|
| 1040 | - $addressBookOr = $query2->expr()->orX(); |
|
| 1041 | - foreach ($addressBookIds as $addressBookId) { |
|
| 1042 | - $addressBookOr->add($query2->expr()->eq('cp.addressbookid', $query2->createNamedParameter($addressBookId))); |
|
| 1043 | - } |
|
| 1044 | - |
|
| 1045 | - if ($addressBookOr->count() === 0) { |
|
| 1046 | - return []; |
|
| 1047 | - } |
|
| 1048 | - |
|
| 1049 | - $propertyOr = $query2->expr()->orX(); |
|
| 1050 | - foreach ($searchProperties as $property) { |
|
| 1051 | - if ($escapePattern) { |
|
| 1052 | - if ($property === 'EMAIL' && strpos($pattern, ' ') !== false) { |
|
| 1053 | - // There can be no spaces in emails |
|
| 1054 | - continue; |
|
| 1055 | - } |
|
| 1056 | - |
|
| 1057 | - if ($property === 'CLOUD' && preg_match('/[^a-zA-Z0-9 :_.@\/\-\']/', $pattern) === 1) { |
|
| 1058 | - // There can be no chars in cloud ids which are not valid for user ids plus :/ |
|
| 1059 | - // worst case: CA61590A-BBBC-423E-84AF-E6DF01455A53@https://my.nxt/srv/ |
|
| 1060 | - continue; |
|
| 1061 | - } |
|
| 1062 | - } |
|
| 1063 | - |
|
| 1064 | - $propertyOr->add($query2->expr()->eq('cp.name', $query2->createNamedParameter($property))); |
|
| 1065 | - } |
|
| 1066 | - |
|
| 1067 | - if ($propertyOr->count() === 0) { |
|
| 1068 | - return []; |
|
| 1069 | - } |
|
| 1070 | - |
|
| 1071 | - $query2->selectDistinct('cp.cardid') |
|
| 1072 | - ->from($this->dbCardsPropertiesTable, 'cp') |
|
| 1073 | - ->andWhere($addressBookOr) |
|
| 1074 | - ->andWhere($propertyOr); |
|
| 1075 | - |
|
| 1076 | - // No need for like when the pattern is empty |
|
| 1077 | - if ('' !== $pattern) { |
|
| 1078 | - if (!$useWildcards) { |
|
| 1079 | - $query2->andWhere($query2->expr()->eq('cp.value', $query2->createNamedParameter($pattern))); |
|
| 1080 | - } elseif (!$escapePattern) { |
|
| 1081 | - $query2->andWhere($query2->expr()->ilike('cp.value', $query2->createNamedParameter($pattern))); |
|
| 1082 | - } else { |
|
| 1083 | - $query2->andWhere($query2->expr()->ilike('cp.value', $query2->createNamedParameter('%' . $this->db->escapeLikeParameter($pattern) . '%'))); |
|
| 1084 | - } |
|
| 1085 | - } |
|
| 1086 | - |
|
| 1087 | - if (isset($options['limit'])) { |
|
| 1088 | - $query2->setMaxResults($options['limit']); |
|
| 1089 | - } |
|
| 1090 | - if (isset($options['offset'])) { |
|
| 1091 | - $query2->setFirstResult($options['offset']); |
|
| 1092 | - } |
|
| 1093 | - |
|
| 1094 | - $result = $query2->execute(); |
|
| 1095 | - $matches = $result->fetchAll(); |
|
| 1096 | - $result->closeCursor(); |
|
| 1097 | - $matches = array_map(function ($match) { |
|
| 1098 | - return (int)$match['cardid']; |
|
| 1099 | - }, $matches); |
|
| 1100 | - |
|
| 1101 | - $cards = []; |
|
| 1102 | - $query = $this->db->getQueryBuilder(); |
|
| 1103 | - $query->select('c.addressbookid', 'c.carddata', 'c.uri') |
|
| 1104 | - ->from($this->dbCardsTable, 'c') |
|
| 1105 | - ->where($query->expr()->in('c.id', $query->createParameter('matches'))); |
|
| 1106 | - |
|
| 1107 | - foreach (array_chunk($matches, 1000) as $matchesChunk) { |
|
| 1108 | - $query->setParameter('matches', $matchesChunk, IQueryBuilder::PARAM_INT_ARRAY); |
|
| 1109 | - $result = $query->executeQuery(); |
|
| 1110 | - $cards = array_merge($cards, $result->fetchAll()); |
|
| 1111 | - $result->closeCursor(); |
|
| 1112 | - } |
|
| 1113 | - |
|
| 1114 | - return array_map(function ($array) { |
|
| 1115 | - $array['addressbookid'] = (int) $array['addressbookid']; |
|
| 1116 | - $modified = false; |
|
| 1117 | - $array['carddata'] = $this->readBlob($array['carddata'], $modified); |
|
| 1118 | - if ($modified) { |
|
| 1119 | - $array['size'] = strlen($array['carddata']); |
|
| 1120 | - } |
|
| 1121 | - return $array; |
|
| 1122 | - }, $cards); |
|
| 1123 | - } |
|
| 1124 | - |
|
| 1125 | - /** |
|
| 1126 | - * @param int $bookId |
|
| 1127 | - * @param string $name |
|
| 1128 | - * @return array |
|
| 1129 | - */ |
|
| 1130 | - public function collectCardProperties($bookId, $name) { |
|
| 1131 | - $query = $this->db->getQueryBuilder(); |
|
| 1132 | - $result = $query->selectDistinct('value') |
|
| 1133 | - ->from($this->dbCardsPropertiesTable) |
|
| 1134 | - ->where($query->expr()->eq('name', $query->createNamedParameter($name))) |
|
| 1135 | - ->andWhere($query->expr()->eq('addressbookid', $query->createNamedParameter($bookId))) |
|
| 1136 | - ->execute(); |
|
| 1137 | - |
|
| 1138 | - $all = $result->fetchAll(PDO::FETCH_COLUMN); |
|
| 1139 | - $result->closeCursor(); |
|
| 1140 | - |
|
| 1141 | - return $all; |
|
| 1142 | - } |
|
| 1143 | - |
|
| 1144 | - /** |
|
| 1145 | - * get URI from a given contact |
|
| 1146 | - * |
|
| 1147 | - * @param int $id |
|
| 1148 | - * @return string |
|
| 1149 | - */ |
|
| 1150 | - public function getCardUri($id) { |
|
| 1151 | - $query = $this->db->getQueryBuilder(); |
|
| 1152 | - $query->select('uri')->from($this->dbCardsTable) |
|
| 1153 | - ->where($query->expr()->eq('id', $query->createParameter('id'))) |
|
| 1154 | - ->setParameter('id', $id); |
|
| 1155 | - |
|
| 1156 | - $result = $query->execute(); |
|
| 1157 | - $uri = $result->fetch(); |
|
| 1158 | - $result->closeCursor(); |
|
| 1159 | - |
|
| 1160 | - if (!isset($uri['uri'])) { |
|
| 1161 | - throw new \InvalidArgumentException('Card does not exists: ' . $id); |
|
| 1162 | - } |
|
| 1163 | - |
|
| 1164 | - return $uri['uri']; |
|
| 1165 | - } |
|
| 1166 | - |
|
| 1167 | - /** |
|
| 1168 | - * return contact with the given URI |
|
| 1169 | - * |
|
| 1170 | - * @param int $addressBookId |
|
| 1171 | - * @param string $uri |
|
| 1172 | - * @returns array |
|
| 1173 | - */ |
|
| 1174 | - public function getContact($addressBookId, $uri) { |
|
| 1175 | - $result = []; |
|
| 1176 | - $query = $this->db->getQueryBuilder(); |
|
| 1177 | - $query->select('*')->from($this->dbCardsTable) |
|
| 1178 | - ->where($query->expr()->eq('uri', $query->createNamedParameter($uri))) |
|
| 1179 | - ->andWhere($query->expr()->eq('addressbookid', $query->createNamedParameter($addressBookId))); |
|
| 1180 | - $queryResult = $query->execute(); |
|
| 1181 | - $contact = $queryResult->fetch(); |
|
| 1182 | - $queryResult->closeCursor(); |
|
| 1183 | - |
|
| 1184 | - if (is_array($contact)) { |
|
| 1185 | - $modified = false; |
|
| 1186 | - $contact['etag'] = '"' . $contact['etag'] . '"'; |
|
| 1187 | - $contact['carddata'] = $this->readBlob($contact['carddata'], $modified); |
|
| 1188 | - if ($modified) { |
|
| 1189 | - $contact['size'] = strlen($contact['carddata']); |
|
| 1190 | - } |
|
| 1191 | - |
|
| 1192 | - $result = $contact; |
|
| 1193 | - } |
|
| 1194 | - |
|
| 1195 | - return $result; |
|
| 1196 | - } |
|
| 1197 | - |
|
| 1198 | - /** |
|
| 1199 | - * Returns the list of people whom this address book is shared with. |
|
| 1200 | - * |
|
| 1201 | - * Every element in this array should have the following properties: |
|
| 1202 | - * * href - Often a mailto: address |
|
| 1203 | - * * commonName - Optional, for example a first + last name |
|
| 1204 | - * * status - See the Sabre\CalDAV\SharingPlugin::STATUS_ constants. |
|
| 1205 | - * * readOnly - boolean |
|
| 1206 | - * |
|
| 1207 | - * @return list<array{href: string, commonName: string, status: int, readOnly: bool, '{http://owncloud.org/ns}principal': string, '{http://owncloud.org/ns}group-share': bool}> |
|
| 1208 | - */ |
|
| 1209 | - public function getShares(int $addressBookId): array { |
|
| 1210 | - return $this->sharingBackend->getShares($addressBookId); |
|
| 1211 | - } |
|
| 1212 | - |
|
| 1213 | - /** |
|
| 1214 | - * update properties table |
|
| 1215 | - * |
|
| 1216 | - * @param int $addressBookId |
|
| 1217 | - * @param string $cardUri |
|
| 1218 | - * @param string $vCardSerialized |
|
| 1219 | - */ |
|
| 1220 | - protected function updateProperties($addressBookId, $cardUri, $vCardSerialized) { |
|
| 1221 | - $cardId = $this->getCardId($addressBookId, $cardUri); |
|
| 1222 | - $vCard = $this->readCard($vCardSerialized); |
|
| 1223 | - |
|
| 1224 | - $this->purgeProperties($addressBookId, $cardId); |
|
| 1225 | - |
|
| 1226 | - $query = $this->db->getQueryBuilder(); |
|
| 1227 | - $query->insert($this->dbCardsPropertiesTable) |
|
| 1228 | - ->values( |
|
| 1229 | - [ |
|
| 1230 | - 'addressbookid' => $query->createNamedParameter($addressBookId), |
|
| 1231 | - 'cardid' => $query->createNamedParameter($cardId), |
|
| 1232 | - 'name' => $query->createParameter('name'), |
|
| 1233 | - 'value' => $query->createParameter('value'), |
|
| 1234 | - 'preferred' => $query->createParameter('preferred') |
|
| 1235 | - ] |
|
| 1236 | - ); |
|
| 1237 | - |
|
| 1238 | - |
|
| 1239 | - $this->db->beginTransaction(); |
|
| 1240 | - |
|
| 1241 | - try { |
|
| 1242 | - foreach ($vCard->children() as $property) { |
|
| 1243 | - if (!in_array($property->name, self::$indexProperties)) { |
|
| 1244 | - continue; |
|
| 1245 | - } |
|
| 1246 | - $preferred = 0; |
|
| 1247 | - foreach ($property->parameters as $parameter) { |
|
| 1248 | - if ($parameter->name === 'TYPE' && strtoupper($parameter->getValue()) === 'PREF') { |
|
| 1249 | - $preferred = 1; |
|
| 1250 | - break; |
|
| 1251 | - } |
|
| 1252 | - } |
|
| 1253 | - $query->setParameter('name', $property->name); |
|
| 1254 | - $query->setParameter('value', mb_strcut($property->getValue(), 0, 254)); |
|
| 1255 | - $query->setParameter('preferred', $preferred); |
|
| 1256 | - $query->execute(); |
|
| 1257 | - } |
|
| 1258 | - $this->db->commit(); |
|
| 1259 | - } catch (\Exception $e) { |
|
| 1260 | - $this->db->rollBack(); |
|
| 1261 | - } |
|
| 1262 | - } |
|
| 1263 | - |
|
| 1264 | - /** |
|
| 1265 | - * read vCard data into a vCard object |
|
| 1266 | - * |
|
| 1267 | - * @param string $cardData |
|
| 1268 | - * @return VCard |
|
| 1269 | - */ |
|
| 1270 | - protected function readCard($cardData) { |
|
| 1271 | - return Reader::read($cardData); |
|
| 1272 | - } |
|
| 1273 | - |
|
| 1274 | - /** |
|
| 1275 | - * delete all properties from a given card |
|
| 1276 | - * |
|
| 1277 | - * @param int $addressBookId |
|
| 1278 | - * @param int $cardId |
|
| 1279 | - */ |
|
| 1280 | - protected function purgeProperties($addressBookId, $cardId) { |
|
| 1281 | - $query = $this->db->getQueryBuilder(); |
|
| 1282 | - $query->delete($this->dbCardsPropertiesTable) |
|
| 1283 | - ->where($query->expr()->eq('cardid', $query->createNamedParameter($cardId))) |
|
| 1284 | - ->andWhere($query->expr()->eq('addressbookid', $query->createNamedParameter($addressBookId))); |
|
| 1285 | - $query->execute(); |
|
| 1286 | - } |
|
| 1287 | - |
|
| 1288 | - /** |
|
| 1289 | - * Get ID from a given contact |
|
| 1290 | - */ |
|
| 1291 | - protected function getCardId(int $addressBookId, string $uri): int { |
|
| 1292 | - $query = $this->db->getQueryBuilder(); |
|
| 1293 | - $query->select('id')->from($this->dbCardsTable) |
|
| 1294 | - ->where($query->expr()->eq('uri', $query->createNamedParameter($uri))) |
|
| 1295 | - ->andWhere($query->expr()->eq('addressbookid', $query->createNamedParameter($addressBookId))); |
|
| 1296 | - |
|
| 1297 | - $result = $query->execute(); |
|
| 1298 | - $cardIds = $result->fetch(); |
|
| 1299 | - $result->closeCursor(); |
|
| 1300 | - |
|
| 1301 | - if (!isset($cardIds['id'])) { |
|
| 1302 | - throw new \InvalidArgumentException('Card does not exists: ' . $uri); |
|
| 1303 | - } |
|
| 1304 | - |
|
| 1305 | - return (int)$cardIds['id']; |
|
| 1306 | - } |
|
| 1307 | - |
|
| 1308 | - /** |
|
| 1309 | - * For shared address books the sharee is set in the ACL of the address book |
|
| 1310 | - * |
|
| 1311 | - * @param int $addressBookId |
|
| 1312 | - * @param list<array{privilege: string, principal: string, protected: bool}> $acl |
|
| 1313 | - * @return list<array{privilege: string, principal: string, protected: bool}> |
|
| 1314 | - */ |
|
| 1315 | - public function applyShareAcl(int $addressBookId, array $acl): array { |
|
| 1316 | - return $this->sharingBackend->applyShareAcl($addressBookId, $acl); |
|
| 1317 | - } |
|
| 1318 | - |
|
| 1319 | - /** |
|
| 1320 | - * @throws \InvalidArgumentException |
|
| 1321 | - */ |
|
| 1322 | - public function pruneOutdatedSyncTokens(int $keep = 10_000): int { |
|
| 1323 | - if ($keep < 0) { |
|
| 1324 | - throw new \InvalidArgumentException(); |
|
| 1325 | - } |
|
| 1326 | - $query = $this->db->getQueryBuilder(); |
|
| 1327 | - $query->delete('addressbookchanges') |
|
| 1328 | - ->orderBy('id', 'DESC') |
|
| 1329 | - ->setFirstResult($keep); |
|
| 1330 | - return $query->executeStatement(); |
|
| 1331 | - } |
|
| 1332 | - |
|
| 1333 | - private function convertPrincipal(string $principalUri, bool $toV2): string { |
|
| 1334 | - if ($this->principalBackend->getPrincipalPrefix() === 'principals') { |
|
| 1335 | - [, $name] = \Sabre\Uri\split($principalUri); |
|
| 1336 | - if ($toV2 === true) { |
|
| 1337 | - return "principals/users/$name"; |
|
| 1338 | - } |
|
| 1339 | - return "principals/$name"; |
|
| 1340 | - } |
|
| 1341 | - return $principalUri; |
|
| 1342 | - } |
|
| 1343 | - |
|
| 1344 | - private function addOwnerPrincipal(array &$addressbookInfo): void { |
|
| 1345 | - $ownerPrincipalKey = '{' . \OCA\DAV\DAV\Sharing\Plugin::NS_OWNCLOUD . '}owner-principal'; |
|
| 1346 | - $displaynameKey = '{' . \OCA\DAV\DAV\Sharing\Plugin::NS_NEXTCLOUD . '}owner-displayname'; |
|
| 1347 | - if (isset($addressbookInfo[$ownerPrincipalKey])) { |
|
| 1348 | - $uri = $addressbookInfo[$ownerPrincipalKey]; |
|
| 1349 | - } else { |
|
| 1350 | - $uri = $addressbookInfo['principaluri']; |
|
| 1351 | - } |
|
| 1352 | - |
|
| 1353 | - $principalInformation = $this->principalBackend->getPrincipalByPath($uri); |
|
| 1354 | - if (isset($principalInformation['{DAV:}displayname'])) { |
|
| 1355 | - $addressbookInfo[$displaynameKey] = $principalInformation['{DAV:}displayname']; |
|
| 1356 | - } |
|
| 1357 | - } |
|
| 1358 | - |
|
| 1359 | - /** |
|
| 1360 | - * Extract UID from vcard |
|
| 1361 | - * |
|
| 1362 | - * @param string $cardData the vcard raw data |
|
| 1363 | - * @return string the uid |
|
| 1364 | - * @throws BadRequest if no UID is available or vcard is empty |
|
| 1365 | - */ |
|
| 1366 | - private function getUID(string $cardData): string { |
|
| 1367 | - if ($cardData !== '') { |
|
| 1368 | - $vCard = Reader::read($cardData); |
|
| 1369 | - if ($vCard->UID) { |
|
| 1370 | - $uid = $vCard->UID->getValue(); |
|
| 1371 | - return $uid; |
|
| 1372 | - } |
|
| 1373 | - // should already be handled, but just in case |
|
| 1374 | - throw new BadRequest('vCards on CardDAV servers MUST have a UID property'); |
|
| 1375 | - } |
|
| 1376 | - // should already be handled, but just in case |
|
| 1377 | - throw new BadRequest('vCard can not be empty'); |
|
| 1378 | - } |
|
| 65 | + use TTransactional; |
|
| 66 | + |
|
| 67 | + public const PERSONAL_ADDRESSBOOK_URI = 'contacts'; |
|
| 68 | + public const PERSONAL_ADDRESSBOOK_NAME = 'Contacts'; |
|
| 69 | + |
|
| 70 | + private Principal $principalBackend; |
|
| 71 | + private string $dbCardsTable = 'cards'; |
|
| 72 | + private string $dbCardsPropertiesTable = 'cards_properties'; |
|
| 73 | + private IDBConnection $db; |
|
| 74 | + private Backend $sharingBackend; |
|
| 75 | + |
|
| 76 | + /** @var array properties to index */ |
|
| 77 | + public static array $indexProperties = [ |
|
| 78 | + 'BDAY', 'UID', 'N', 'FN', 'TITLE', 'ROLE', 'NOTE', 'NICKNAME', |
|
| 79 | + 'ORG', 'CATEGORIES', 'EMAIL', 'TEL', 'IMPP', 'ADR', 'URL', 'GEO', |
|
| 80 | + 'CLOUD', 'X-SOCIALPROFILE']; |
|
| 81 | + |
|
| 82 | + /** |
|
| 83 | + * @var string[] Map of uid => display name |
|
| 84 | + */ |
|
| 85 | + protected array $userDisplayNames; |
|
| 86 | + private IUserManager $userManager; |
|
| 87 | + private IEventDispatcher $dispatcher; |
|
| 88 | + private array $etagCache = []; |
|
| 89 | + |
|
| 90 | + /** |
|
| 91 | + * CardDavBackend constructor. |
|
| 92 | + * |
|
| 93 | + * @param IDBConnection $db |
|
| 94 | + * @param Principal $principalBackend |
|
| 95 | + * @param IUserManager $userManager |
|
| 96 | + * @param IGroupManager $groupManager |
|
| 97 | + * @param IEventDispatcher $dispatcher |
|
| 98 | + */ |
|
| 99 | + public function __construct(IDBConnection $db, |
|
| 100 | + Principal $principalBackend, |
|
| 101 | + IUserManager $userManager, |
|
| 102 | + IGroupManager $groupManager, |
|
| 103 | + IEventDispatcher $dispatcher) { |
|
| 104 | + $this->db = $db; |
|
| 105 | + $this->principalBackend = $principalBackend; |
|
| 106 | + $this->userManager = $userManager; |
|
| 107 | + $this->dispatcher = $dispatcher; |
|
| 108 | + $this->sharingBackend = new Backend($this->db, $this->userManager, $groupManager, $principalBackend, 'addressbook'); |
|
| 109 | + } |
|
| 110 | + |
|
| 111 | + /** |
|
| 112 | + * Return the number of address books for a principal |
|
| 113 | + * |
|
| 114 | + * @param $principalUri |
|
| 115 | + * @return int |
|
| 116 | + */ |
|
| 117 | + public function getAddressBooksForUserCount($principalUri) { |
|
| 118 | + $principalUri = $this->convertPrincipal($principalUri, true); |
|
| 119 | + $query = $this->db->getQueryBuilder(); |
|
| 120 | + $query->select($query->func()->count('*')) |
|
| 121 | + ->from('addressbooks') |
|
| 122 | + ->where($query->expr()->eq('principaluri', $query->createNamedParameter($principalUri))); |
|
| 123 | + |
|
| 124 | + $result = $query->executeQuery(); |
|
| 125 | + $column = (int) $result->fetchOne(); |
|
| 126 | + $result->closeCursor(); |
|
| 127 | + return $column; |
|
| 128 | + } |
|
| 129 | + |
|
| 130 | + /** |
|
| 131 | + * Returns the list of address books for a specific user. |
|
| 132 | + * |
|
| 133 | + * Every addressbook should have the following properties: |
|
| 134 | + * id - an arbitrary unique id |
|
| 135 | + * uri - the 'basename' part of the url |
|
| 136 | + * principaluri - Same as the passed parameter |
|
| 137 | + * |
|
| 138 | + * Any additional clark-notation property may be passed besides this. Some |
|
| 139 | + * common ones are : |
|
| 140 | + * {DAV:}displayname |
|
| 141 | + * {urn:ietf:params:xml:ns:carddav}addressbook-description |
|
| 142 | + * {http://calendarserver.org/ns/}getctag |
|
| 143 | + * |
|
| 144 | + * @param string $principalUri |
|
| 145 | + * @return array |
|
| 146 | + */ |
|
| 147 | + public function getAddressBooksForUser($principalUri) { |
|
| 148 | + $principalUriOriginal = $principalUri; |
|
| 149 | + $principalUri = $this->convertPrincipal($principalUri, true); |
|
| 150 | + $query = $this->db->getQueryBuilder(); |
|
| 151 | + $query->select(['id', 'uri', 'displayname', 'principaluri', 'description', 'synctoken']) |
|
| 152 | + ->from('addressbooks') |
|
| 153 | + ->where($query->expr()->eq('principaluri', $query->createNamedParameter($principalUri))); |
|
| 154 | + |
|
| 155 | + $addressBooks = []; |
|
| 156 | + |
|
| 157 | + $result = $query->execute(); |
|
| 158 | + while ($row = $result->fetch()) { |
|
| 159 | + $addressBooks[$row['id']] = [ |
|
| 160 | + 'id' => $row['id'], |
|
| 161 | + 'uri' => $row['uri'], |
|
| 162 | + 'principaluri' => $this->convertPrincipal($row['principaluri'], false), |
|
| 163 | + '{DAV:}displayname' => $row['displayname'], |
|
| 164 | + '{' . Plugin::NS_CARDDAV . '}addressbook-description' => $row['description'], |
|
| 165 | + '{http://calendarserver.org/ns/}getctag' => $row['synctoken'], |
|
| 166 | + '{http://sabredav.org/ns}sync-token' => $row['synctoken'] ?: '0', |
|
| 167 | + ]; |
|
| 168 | + |
|
| 169 | + $this->addOwnerPrincipal($addressBooks[$row['id']]); |
|
| 170 | + } |
|
| 171 | + $result->closeCursor(); |
|
| 172 | + |
|
| 173 | + // query for shared addressbooks |
|
| 174 | + $principals = $this->principalBackend->getGroupMembership($principalUriOriginal, true); |
|
| 175 | + $principals = array_merge($principals, $this->principalBackend->getCircleMembership($principalUriOriginal)); |
|
| 176 | + |
|
| 177 | + $principals[] = $principalUri; |
|
| 178 | + |
|
| 179 | + $query = $this->db->getQueryBuilder(); |
|
| 180 | + $result = $query->select(['a.id', 'a.uri', 'a.displayname', 'a.principaluri', 'a.description', 'a.synctoken', 's.access']) |
|
| 181 | + ->from('dav_shares', 's') |
|
| 182 | + ->join('s', 'addressbooks', 'a', $query->expr()->eq('s.resourceid', 'a.id')) |
|
| 183 | + ->where($query->expr()->in('s.principaluri', $query->createParameter('principaluri'))) |
|
| 184 | + ->andWhere($query->expr()->eq('s.type', $query->createParameter('type'))) |
|
| 185 | + ->setParameter('type', 'addressbook') |
|
| 186 | + ->setParameter('principaluri', $principals, IQueryBuilder::PARAM_STR_ARRAY) |
|
| 187 | + ->execute(); |
|
| 188 | + |
|
| 189 | + $readOnlyPropertyName = '{' . \OCA\DAV\DAV\Sharing\Plugin::NS_OWNCLOUD . '}read-only'; |
|
| 190 | + while ($row = $result->fetch()) { |
|
| 191 | + if ($row['principaluri'] === $principalUri) { |
|
| 192 | + continue; |
|
| 193 | + } |
|
| 194 | + |
|
| 195 | + $readOnly = (int)$row['access'] === Backend::ACCESS_READ; |
|
| 196 | + if (isset($addressBooks[$row['id']])) { |
|
| 197 | + if ($readOnly) { |
|
| 198 | + // New share can not have more permissions then the old one. |
|
| 199 | + continue; |
|
| 200 | + } |
|
| 201 | + if (isset($addressBooks[$row['id']][$readOnlyPropertyName]) && |
|
| 202 | + $addressBooks[$row['id']][$readOnlyPropertyName] === 0) { |
|
| 203 | + // Old share is already read-write, no more permissions can be gained |
|
| 204 | + continue; |
|
| 205 | + } |
|
| 206 | + } |
|
| 207 | + |
|
| 208 | + [, $name] = \Sabre\Uri\split($row['principaluri']); |
|
| 209 | + $uri = $row['uri'] . '_shared_by_' . $name; |
|
| 210 | + $displayName = $row['displayname'] . ' (' . ($this->userManager->getDisplayName($name) ?? $name ?? '') . ')'; |
|
| 211 | + |
|
| 212 | + $addressBooks[$row['id']] = [ |
|
| 213 | + 'id' => $row['id'], |
|
| 214 | + 'uri' => $uri, |
|
| 215 | + 'principaluri' => $principalUriOriginal, |
|
| 216 | + '{DAV:}displayname' => $displayName, |
|
| 217 | + '{' . Plugin::NS_CARDDAV . '}addressbook-description' => $row['description'], |
|
| 218 | + '{http://calendarserver.org/ns/}getctag' => $row['synctoken'], |
|
| 219 | + '{http://sabredav.org/ns}sync-token' => $row['synctoken'] ?: '0', |
|
| 220 | + '{' . \OCA\DAV\DAV\Sharing\Plugin::NS_OWNCLOUD . '}owner-principal' => $row['principaluri'], |
|
| 221 | + $readOnlyPropertyName => $readOnly, |
|
| 222 | + ]; |
|
| 223 | + |
|
| 224 | + $this->addOwnerPrincipal($addressBooks[$row['id']]); |
|
| 225 | + } |
|
| 226 | + $result->closeCursor(); |
|
| 227 | + |
|
| 228 | + return array_values($addressBooks); |
|
| 229 | + } |
|
| 230 | + |
|
| 231 | + public function getUsersOwnAddressBooks($principalUri) { |
|
| 232 | + $principalUri = $this->convertPrincipal($principalUri, true); |
|
| 233 | + $query = $this->db->getQueryBuilder(); |
|
| 234 | + $query->select(['id', 'uri', 'displayname', 'principaluri', 'description', 'synctoken']) |
|
| 235 | + ->from('addressbooks') |
|
| 236 | + ->where($query->expr()->eq('principaluri', $query->createNamedParameter($principalUri))); |
|
| 237 | + |
|
| 238 | + $addressBooks = []; |
|
| 239 | + |
|
| 240 | + $result = $query->execute(); |
|
| 241 | + while ($row = $result->fetch()) { |
|
| 242 | + $addressBooks[$row['id']] = [ |
|
| 243 | + 'id' => $row['id'], |
|
| 244 | + 'uri' => $row['uri'], |
|
| 245 | + 'principaluri' => $this->convertPrincipal($row['principaluri'], false), |
|
| 246 | + '{DAV:}displayname' => $row['displayname'], |
|
| 247 | + '{' . Plugin::NS_CARDDAV . '}addressbook-description' => $row['description'], |
|
| 248 | + '{http://calendarserver.org/ns/}getctag' => $row['synctoken'], |
|
| 249 | + '{http://sabredav.org/ns}sync-token' => $row['synctoken'] ?: '0', |
|
| 250 | + ]; |
|
| 251 | + |
|
| 252 | + $this->addOwnerPrincipal($addressBooks[$row['id']]); |
|
| 253 | + } |
|
| 254 | + $result->closeCursor(); |
|
| 255 | + |
|
| 256 | + return array_values($addressBooks); |
|
| 257 | + } |
|
| 258 | + |
|
| 259 | + /** |
|
| 260 | + * @param int $addressBookId |
|
| 261 | + */ |
|
| 262 | + public function getAddressBookById(int $addressBookId): ?array { |
|
| 263 | + $query = $this->db->getQueryBuilder(); |
|
| 264 | + $result = $query->select(['id', 'uri', 'displayname', 'principaluri', 'description', 'synctoken']) |
|
| 265 | + ->from('addressbooks') |
|
| 266 | + ->where($query->expr()->eq('id', $query->createNamedParameter($addressBookId, IQueryBuilder::PARAM_INT))) |
|
| 267 | + ->executeQuery(); |
|
| 268 | + $row = $result->fetch(); |
|
| 269 | + $result->closeCursor(); |
|
| 270 | + if (!$row) { |
|
| 271 | + return null; |
|
| 272 | + } |
|
| 273 | + |
|
| 274 | + $addressBook = [ |
|
| 275 | + 'id' => $row['id'], |
|
| 276 | + 'uri' => $row['uri'], |
|
| 277 | + 'principaluri' => $row['principaluri'], |
|
| 278 | + '{DAV:}displayname' => $row['displayname'], |
|
| 279 | + '{' . Plugin::NS_CARDDAV . '}addressbook-description' => $row['description'], |
|
| 280 | + '{http://calendarserver.org/ns/}getctag' => $row['synctoken'], |
|
| 281 | + '{http://sabredav.org/ns}sync-token' => $row['synctoken'] ?: '0', |
|
| 282 | + ]; |
|
| 283 | + |
|
| 284 | + $this->addOwnerPrincipal($addressBook); |
|
| 285 | + |
|
| 286 | + return $addressBook; |
|
| 287 | + } |
|
| 288 | + |
|
| 289 | + public function getAddressBooksByUri(string $principal, string $addressBookUri): ?array { |
|
| 290 | + $query = $this->db->getQueryBuilder(); |
|
| 291 | + $result = $query->select(['id', 'uri', 'displayname', 'principaluri', 'description', 'synctoken']) |
|
| 292 | + ->from('addressbooks') |
|
| 293 | + ->where($query->expr()->eq('uri', $query->createNamedParameter($addressBookUri))) |
|
| 294 | + ->andWhere($query->expr()->eq('principaluri', $query->createNamedParameter($principal))) |
|
| 295 | + ->setMaxResults(1) |
|
| 296 | + ->executeQuery(); |
|
| 297 | + |
|
| 298 | + $row = $result->fetch(); |
|
| 299 | + $result->closeCursor(); |
|
| 300 | + if ($row === false) { |
|
| 301 | + return null; |
|
| 302 | + } |
|
| 303 | + |
|
| 304 | + $addressBook = [ |
|
| 305 | + 'id' => $row['id'], |
|
| 306 | + 'uri' => $row['uri'], |
|
| 307 | + 'principaluri' => $row['principaluri'], |
|
| 308 | + '{DAV:}displayname' => $row['displayname'], |
|
| 309 | + '{' . Plugin::NS_CARDDAV . '}addressbook-description' => $row['description'], |
|
| 310 | + '{http://calendarserver.org/ns/}getctag' => $row['synctoken'], |
|
| 311 | + '{http://sabredav.org/ns}sync-token' => $row['synctoken'] ?: '0', |
|
| 312 | + ]; |
|
| 313 | + |
|
| 314 | + $this->addOwnerPrincipal($addressBook); |
|
| 315 | + |
|
| 316 | + return $addressBook; |
|
| 317 | + } |
|
| 318 | + |
|
| 319 | + /** |
|
| 320 | + * Updates properties for an address book. |
|
| 321 | + * |
|
| 322 | + * The list of mutations is stored in a Sabre\DAV\PropPatch object. |
|
| 323 | + * To do the actual updates, you must tell this object which properties |
|
| 324 | + * you're going to process with the handle() method. |
|
| 325 | + * |
|
| 326 | + * Calling the handle method is like telling the PropPatch object "I |
|
| 327 | + * promise I can handle updating this property". |
|
| 328 | + * |
|
| 329 | + * Read the PropPatch documentation for more info and examples. |
|
| 330 | + * |
|
| 331 | + * @param string $addressBookId |
|
| 332 | + * @param \Sabre\DAV\PropPatch $propPatch |
|
| 333 | + * @return void |
|
| 334 | + */ |
|
| 335 | + public function updateAddressBook($addressBookId, \Sabre\DAV\PropPatch $propPatch) { |
|
| 336 | + $supportedProperties = [ |
|
| 337 | + '{DAV:}displayname', |
|
| 338 | + '{' . Plugin::NS_CARDDAV . '}addressbook-description', |
|
| 339 | + ]; |
|
| 340 | + |
|
| 341 | + $propPatch->handle($supportedProperties, function ($mutations) use ($addressBookId) { |
|
| 342 | + $updates = []; |
|
| 343 | + foreach ($mutations as $property => $newValue) { |
|
| 344 | + switch ($property) { |
|
| 345 | + case '{DAV:}displayname': |
|
| 346 | + $updates['displayname'] = $newValue; |
|
| 347 | + break; |
|
| 348 | + case '{' . Plugin::NS_CARDDAV . '}addressbook-description': |
|
| 349 | + $updates['description'] = $newValue; |
|
| 350 | + break; |
|
| 351 | + } |
|
| 352 | + } |
|
| 353 | + $query = $this->db->getQueryBuilder(); |
|
| 354 | + $query->update('addressbooks'); |
|
| 355 | + |
|
| 356 | + foreach ($updates as $key => $value) { |
|
| 357 | + $query->set($key, $query->createNamedParameter($value)); |
|
| 358 | + } |
|
| 359 | + $query->where($query->expr()->eq('id', $query->createNamedParameter($addressBookId))) |
|
| 360 | + ->executeStatement(); |
|
| 361 | + |
|
| 362 | + $this->addChange($addressBookId, "", 2); |
|
| 363 | + |
|
| 364 | + $addressBookRow = $this->getAddressBookById((int)$addressBookId); |
|
| 365 | + $shares = $this->getShares((int)$addressBookId); |
|
| 366 | + $this->dispatcher->dispatchTyped(new AddressBookUpdatedEvent((int)$addressBookId, $addressBookRow, $shares, $mutations)); |
|
| 367 | + |
|
| 368 | + return true; |
|
| 369 | + }); |
|
| 370 | + } |
|
| 371 | + |
|
| 372 | + /** |
|
| 373 | + * Creates a new address book |
|
| 374 | + * |
|
| 375 | + * @param string $principalUri |
|
| 376 | + * @param string $url Just the 'basename' of the url. |
|
| 377 | + * @param array $properties |
|
| 378 | + * @return int |
|
| 379 | + * @throws BadRequest |
|
| 380 | + */ |
|
| 381 | + public function createAddressBook($principalUri, $url, array $properties) { |
|
| 382 | + if (strlen($url) > 255) { |
|
| 383 | + throw new BadRequest('URI too long. Address book not created'); |
|
| 384 | + } |
|
| 385 | + |
|
| 386 | + $values = [ |
|
| 387 | + 'displayname' => null, |
|
| 388 | + 'description' => null, |
|
| 389 | + 'principaluri' => $principalUri, |
|
| 390 | + 'uri' => $url, |
|
| 391 | + 'synctoken' => 1 |
|
| 392 | + ]; |
|
| 393 | + |
|
| 394 | + foreach ($properties as $property => $newValue) { |
|
| 395 | + switch ($property) { |
|
| 396 | + case '{DAV:}displayname': |
|
| 397 | + $values['displayname'] = $newValue; |
|
| 398 | + break; |
|
| 399 | + case '{' . Plugin::NS_CARDDAV . '}addressbook-description': |
|
| 400 | + $values['description'] = $newValue; |
|
| 401 | + break; |
|
| 402 | + default: |
|
| 403 | + throw new BadRequest('Unknown property: ' . $property); |
|
| 404 | + } |
|
| 405 | + } |
|
| 406 | + |
|
| 407 | + // Fallback to make sure the displayname is set. Some clients may refuse |
|
| 408 | + // to work with addressbooks not having a displayname. |
|
| 409 | + if (is_null($values['displayname'])) { |
|
| 410 | + $values['displayname'] = $url; |
|
| 411 | + } |
|
| 412 | + |
|
| 413 | + [$addressBookId, $addressBookRow] = $this->atomic(function() use ($values) { |
|
| 414 | + $query = $this->db->getQueryBuilder(); |
|
| 415 | + $query->insert('addressbooks') |
|
| 416 | + ->values([ |
|
| 417 | + 'uri' => $query->createParameter('uri'), |
|
| 418 | + 'displayname' => $query->createParameter('displayname'), |
|
| 419 | + 'description' => $query->createParameter('description'), |
|
| 420 | + 'principaluri' => $query->createParameter('principaluri'), |
|
| 421 | + 'synctoken' => $query->createParameter('synctoken'), |
|
| 422 | + ]) |
|
| 423 | + ->setParameters($values) |
|
| 424 | + ->execute(); |
|
| 425 | + |
|
| 426 | + $addressBookId = $query->getLastInsertId(); |
|
| 427 | + return [ |
|
| 428 | + $addressBookId, |
|
| 429 | + $this->getAddressBookById($addressBookId), |
|
| 430 | + ]; |
|
| 431 | + }, $this->db); |
|
| 432 | + |
|
| 433 | + $this->dispatcher->dispatchTyped(new AddressBookCreatedEvent($addressBookId, $addressBookRow)); |
|
| 434 | + |
|
| 435 | + return $addressBookId; |
|
| 436 | + } |
|
| 437 | + |
|
| 438 | + /** |
|
| 439 | + * Deletes an entire addressbook and all its contents |
|
| 440 | + * |
|
| 441 | + * @param mixed $addressBookId |
|
| 442 | + * @return void |
|
| 443 | + */ |
|
| 444 | + public function deleteAddressBook($addressBookId) { |
|
| 445 | + $addressBookId = (int)$addressBookId; |
|
| 446 | + $addressBookData = $this->getAddressBookById($addressBookId); |
|
| 447 | + $shares = $this->getShares($addressBookId); |
|
| 448 | + |
|
| 449 | + $query = $this->db->getQueryBuilder(); |
|
| 450 | + $query->delete($this->dbCardsTable) |
|
| 451 | + ->where($query->expr()->eq('addressbookid', $query->createParameter('addressbookid'))) |
|
| 452 | + ->setParameter('addressbookid', $addressBookId, IQueryBuilder::PARAM_INT) |
|
| 453 | + ->executeStatement(); |
|
| 454 | + |
|
| 455 | + $query = $this->db->getQueryBuilder(); |
|
| 456 | + $query->delete('addressbookchanges') |
|
| 457 | + ->where($query->expr()->eq('addressbookid', $query->createParameter('addressbookid'))) |
|
| 458 | + ->setParameter('addressbookid', $addressBookId, IQueryBuilder::PARAM_INT) |
|
| 459 | + ->executeStatement(); |
|
| 460 | + |
|
| 461 | + $query = $this->db->getQueryBuilder(); |
|
| 462 | + $query->delete('addressbooks') |
|
| 463 | + ->where($query->expr()->eq('id', $query->createParameter('id'))) |
|
| 464 | + ->setParameter('id', $addressBookId, IQueryBuilder::PARAM_INT) |
|
| 465 | + ->executeStatement(); |
|
| 466 | + |
|
| 467 | + $this->sharingBackend->deleteAllShares($addressBookId); |
|
| 468 | + |
|
| 469 | + $query = $this->db->getQueryBuilder(); |
|
| 470 | + $query->delete($this->dbCardsPropertiesTable) |
|
| 471 | + ->where($query->expr()->eq('addressbookid', $query->createNamedParameter($addressBookId, IQueryBuilder::PARAM_INT))) |
|
| 472 | + ->executeStatement(); |
|
| 473 | + |
|
| 474 | + if ($addressBookData) { |
|
| 475 | + $this->dispatcher->dispatchTyped(new AddressBookDeletedEvent($addressBookId, $addressBookData, $shares)); |
|
| 476 | + } |
|
| 477 | + } |
|
| 478 | + |
|
| 479 | + /** |
|
| 480 | + * Returns all cards for a specific addressbook id. |
|
| 481 | + * |
|
| 482 | + * This method should return the following properties for each card: |
|
| 483 | + * * carddata - raw vcard data |
|
| 484 | + * * uri - Some unique url |
|
| 485 | + * * lastmodified - A unix timestamp |
|
| 486 | + * |
|
| 487 | + * It's recommended to also return the following properties: |
|
| 488 | + * * etag - A unique etag. This must change every time the card changes. |
|
| 489 | + * * size - The size of the card in bytes. |
|
| 490 | + * |
|
| 491 | + * If these last two properties are provided, less time will be spent |
|
| 492 | + * calculating them. If they are specified, you can also omit carddata. |
|
| 493 | + * This may speed up certain requests, especially with large cards. |
|
| 494 | + * |
|
| 495 | + * @param mixed $addressbookId |
|
| 496 | + * @return array |
|
| 497 | + */ |
|
| 498 | + public function getCards($addressbookId) { |
|
| 499 | + $query = $this->db->getQueryBuilder(); |
|
| 500 | + $query->select(['id', 'uri', 'lastmodified', 'etag', 'size', 'carddata', 'uid']) |
|
| 501 | + ->from($this->dbCardsTable) |
|
| 502 | + ->where($query->expr()->eq('addressbookid', $query->createNamedParameter($addressbookId))); |
|
| 503 | + |
|
| 504 | + $cards = []; |
|
| 505 | + |
|
| 506 | + $result = $query->execute(); |
|
| 507 | + while ($row = $result->fetch()) { |
|
| 508 | + $row['etag'] = '"' . $row['etag'] . '"'; |
|
| 509 | + |
|
| 510 | + $modified = false; |
|
| 511 | + $row['carddata'] = $this->readBlob($row['carddata'], $modified); |
|
| 512 | + if ($modified) { |
|
| 513 | + $row['size'] = strlen($row['carddata']); |
|
| 514 | + } |
|
| 515 | + |
|
| 516 | + $cards[] = $row; |
|
| 517 | + } |
|
| 518 | + $result->closeCursor(); |
|
| 519 | + |
|
| 520 | + return $cards; |
|
| 521 | + } |
|
| 522 | + |
|
| 523 | + /** |
|
| 524 | + * Returns a specific card. |
|
| 525 | + * |
|
| 526 | + * The same set of properties must be returned as with getCards. The only |
|
| 527 | + * exception is that 'carddata' is absolutely required. |
|
| 528 | + * |
|
| 529 | + * If the card does not exist, you must return false. |
|
| 530 | + * |
|
| 531 | + * @param mixed $addressBookId |
|
| 532 | + * @param string $cardUri |
|
| 533 | + * @return array |
|
| 534 | + */ |
|
| 535 | + public function getCard($addressBookId, $cardUri) { |
|
| 536 | + $query = $this->db->getQueryBuilder(); |
|
| 537 | + $query->select(['id', 'uri', 'lastmodified', 'etag', 'size', 'carddata', 'uid']) |
|
| 538 | + ->from($this->dbCardsTable) |
|
| 539 | + ->where($query->expr()->eq('addressbookid', $query->createNamedParameter($addressBookId))) |
|
| 540 | + ->andWhere($query->expr()->eq('uri', $query->createNamedParameter($cardUri))) |
|
| 541 | + ->setMaxResults(1); |
|
| 542 | + |
|
| 543 | + $result = $query->execute(); |
|
| 544 | + $row = $result->fetch(); |
|
| 545 | + if (!$row) { |
|
| 546 | + return false; |
|
| 547 | + } |
|
| 548 | + $row['etag'] = '"' . $row['etag'] . '"'; |
|
| 549 | + |
|
| 550 | + $modified = false; |
|
| 551 | + $row['carddata'] = $this->readBlob($row['carddata'], $modified); |
|
| 552 | + if ($modified) { |
|
| 553 | + $row['size'] = strlen($row['carddata']); |
|
| 554 | + } |
|
| 555 | + |
|
| 556 | + return $row; |
|
| 557 | + } |
|
| 558 | + |
|
| 559 | + /** |
|
| 560 | + * Returns a list of cards. |
|
| 561 | + * |
|
| 562 | + * This method should work identical to getCard, but instead return all the |
|
| 563 | + * cards in the list as an array. |
|
| 564 | + * |
|
| 565 | + * If the backend supports this, it may allow for some speed-ups. |
|
| 566 | + * |
|
| 567 | + * @param mixed $addressBookId |
|
| 568 | + * @param array $uris |
|
| 569 | + * @return array |
|
| 570 | + */ |
|
| 571 | + public function getMultipleCards($addressBookId, array $uris) { |
|
| 572 | + if (empty($uris)) { |
|
| 573 | + return []; |
|
| 574 | + } |
|
| 575 | + |
|
| 576 | + $chunks = array_chunk($uris, 100); |
|
| 577 | + $cards = []; |
|
| 578 | + |
|
| 579 | + $query = $this->db->getQueryBuilder(); |
|
| 580 | + $query->select(['id', 'uri', 'lastmodified', 'etag', 'size', 'carddata', 'uid']) |
|
| 581 | + ->from($this->dbCardsTable) |
|
| 582 | + ->where($query->expr()->eq('addressbookid', $query->createNamedParameter($addressBookId))) |
|
| 583 | + ->andWhere($query->expr()->in('uri', $query->createParameter('uri'))); |
|
| 584 | + |
|
| 585 | + foreach ($chunks as $uris) { |
|
| 586 | + $query->setParameter('uri', $uris, IQueryBuilder::PARAM_STR_ARRAY); |
|
| 587 | + $result = $query->execute(); |
|
| 588 | + |
|
| 589 | + while ($row = $result->fetch()) { |
|
| 590 | + $row['etag'] = '"' . $row['etag'] . '"'; |
|
| 591 | + |
|
| 592 | + $modified = false; |
|
| 593 | + $row['carddata'] = $this->readBlob($row['carddata'], $modified); |
|
| 594 | + if ($modified) { |
|
| 595 | + $row['size'] = strlen($row['carddata']); |
|
| 596 | + } |
|
| 597 | + |
|
| 598 | + $cards[] = $row; |
|
| 599 | + } |
|
| 600 | + $result->closeCursor(); |
|
| 601 | + } |
|
| 602 | + return $cards; |
|
| 603 | + } |
|
| 604 | + |
|
| 605 | + /** |
|
| 606 | + * Creates a new card. |
|
| 607 | + * |
|
| 608 | + * The addressbook id will be passed as the first argument. This is the |
|
| 609 | + * same id as it is returned from the getAddressBooksForUser method. |
|
| 610 | + * |
|
| 611 | + * The cardUri is a base uri, and doesn't include the full path. The |
|
| 612 | + * cardData argument is the vcard body, and is passed as a string. |
|
| 613 | + * |
|
| 614 | + * It is possible to return an ETag from this method. This ETag is for the |
|
| 615 | + * newly created resource, and must be enclosed with double quotes (that |
|
| 616 | + * is, the string itself must contain the double quotes). |
|
| 617 | + * |
|
| 618 | + * You should only return the ETag if you store the carddata as-is. If a |
|
| 619 | + * subsequent GET request on the same card does not have the same body, |
|
| 620 | + * byte-by-byte and you did return an ETag here, clients tend to get |
|
| 621 | + * confused. |
|
| 622 | + * |
|
| 623 | + * If you don't return an ETag, you can just return null. |
|
| 624 | + * |
|
| 625 | + * @param mixed $addressBookId |
|
| 626 | + * @param string $cardUri |
|
| 627 | + * @param string $cardData |
|
| 628 | + * @param bool $checkAlreadyExists |
|
| 629 | + * @return string |
|
| 630 | + */ |
|
| 631 | + public function createCard($addressBookId, $cardUri, $cardData, bool $checkAlreadyExists = true) { |
|
| 632 | + $etag = md5($cardData); |
|
| 633 | + $uid = $this->getUID($cardData); |
|
| 634 | + |
|
| 635 | + if ($checkAlreadyExists) { |
|
| 636 | + $q = $this->db->getQueryBuilder(); |
|
| 637 | + $q->select('uid') |
|
| 638 | + ->from($this->dbCardsTable) |
|
| 639 | + ->where($q->expr()->eq('addressbookid', $q->createNamedParameter($addressBookId))) |
|
| 640 | + ->andWhere($q->expr()->eq('uid', $q->createNamedParameter($uid))) |
|
| 641 | + ->setMaxResults(1); |
|
| 642 | + $result = $q->executeQuery(); |
|
| 643 | + $count = (bool)$result->fetchOne(); |
|
| 644 | + $result->closeCursor(); |
|
| 645 | + if ($count) { |
|
| 646 | + throw new \Sabre\DAV\Exception\BadRequest('VCard object with uid already exists in this addressbook collection.'); |
|
| 647 | + } |
|
| 648 | + } |
|
| 649 | + |
|
| 650 | + $query = $this->db->getQueryBuilder(); |
|
| 651 | + $query->insert('cards') |
|
| 652 | + ->values([ |
|
| 653 | + 'carddata' => $query->createNamedParameter($cardData, IQueryBuilder::PARAM_LOB), |
|
| 654 | + 'uri' => $query->createNamedParameter($cardUri), |
|
| 655 | + 'lastmodified' => $query->createNamedParameter(time()), |
|
| 656 | + 'addressbookid' => $query->createNamedParameter($addressBookId), |
|
| 657 | + 'size' => $query->createNamedParameter(strlen($cardData)), |
|
| 658 | + 'etag' => $query->createNamedParameter($etag), |
|
| 659 | + 'uid' => $query->createNamedParameter($uid), |
|
| 660 | + ]) |
|
| 661 | + ->execute(); |
|
| 662 | + |
|
| 663 | + $etagCacheKey = "$addressBookId#$cardUri"; |
|
| 664 | + $this->etagCache[$etagCacheKey] = $etag; |
|
| 665 | + |
|
| 666 | + $this->addChange($addressBookId, $cardUri, 1); |
|
| 667 | + $this->updateProperties($addressBookId, $cardUri, $cardData); |
|
| 668 | + |
|
| 669 | + $addressBookData = $this->getAddressBookById($addressBookId); |
|
| 670 | + $shares = $this->getShares($addressBookId); |
|
| 671 | + $objectRow = $this->getCard($addressBookId, $cardUri); |
|
| 672 | + $this->dispatcher->dispatchTyped(new CardCreatedEvent($addressBookId, $addressBookData, $shares, $objectRow)); |
|
| 673 | + |
|
| 674 | + return '"' . $etag . '"'; |
|
| 675 | + } |
|
| 676 | + |
|
| 677 | + /** |
|
| 678 | + * Updates a card. |
|
| 679 | + * |
|
| 680 | + * The addressbook id will be passed as the first argument. This is the |
|
| 681 | + * same id as it is returned from the getAddressBooksForUser method. |
|
| 682 | + * |
|
| 683 | + * The cardUri is a base uri, and doesn't include the full path. The |
|
| 684 | + * cardData argument is the vcard body, and is passed as a string. |
|
| 685 | + * |
|
| 686 | + * It is possible to return an ETag from this method. This ETag should |
|
| 687 | + * match that of the updated resource, and must be enclosed with double |
|
| 688 | + * quotes (that is: the string itself must contain the actual quotes). |
|
| 689 | + * |
|
| 690 | + * You should only return the ETag if you store the carddata as-is. If a |
|
| 691 | + * subsequent GET request on the same card does not have the same body, |
|
| 692 | + * byte-by-byte and you did return an ETag here, clients tend to get |
|
| 693 | + * confused. |
|
| 694 | + * |
|
| 695 | + * If you don't return an ETag, you can just return null. |
|
| 696 | + * |
|
| 697 | + * @param mixed $addressBookId |
|
| 698 | + * @param string $cardUri |
|
| 699 | + * @param string $cardData |
|
| 700 | + * @return string |
|
| 701 | + */ |
|
| 702 | + public function updateCard($addressBookId, $cardUri, $cardData) { |
|
| 703 | + $uid = $this->getUID($cardData); |
|
| 704 | + $etag = md5($cardData); |
|
| 705 | + $query = $this->db->getQueryBuilder(); |
|
| 706 | + |
|
| 707 | + // check for recently stored etag and stop if it is the same |
|
| 708 | + $etagCacheKey = "$addressBookId#$cardUri"; |
|
| 709 | + if (isset($this->etagCache[$etagCacheKey]) && $this->etagCache[$etagCacheKey] === $etag) { |
|
| 710 | + return '"' . $etag . '"'; |
|
| 711 | + } |
|
| 712 | + |
|
| 713 | + $query->update($this->dbCardsTable) |
|
| 714 | + ->set('carddata', $query->createNamedParameter($cardData, IQueryBuilder::PARAM_LOB)) |
|
| 715 | + ->set('lastmodified', $query->createNamedParameter(time())) |
|
| 716 | + ->set('size', $query->createNamedParameter(strlen($cardData))) |
|
| 717 | + ->set('etag', $query->createNamedParameter($etag)) |
|
| 718 | + ->set('uid', $query->createNamedParameter($uid)) |
|
| 719 | + ->where($query->expr()->eq('uri', $query->createNamedParameter($cardUri))) |
|
| 720 | + ->andWhere($query->expr()->eq('addressbookid', $query->createNamedParameter($addressBookId))) |
|
| 721 | + ->execute(); |
|
| 722 | + |
|
| 723 | + $this->etagCache[$etagCacheKey] = $etag; |
|
| 724 | + |
|
| 725 | + $this->addChange($addressBookId, $cardUri, 2); |
|
| 726 | + $this->updateProperties($addressBookId, $cardUri, $cardData); |
|
| 727 | + |
|
| 728 | + $addressBookData = $this->getAddressBookById($addressBookId); |
|
| 729 | + $shares = $this->getShares($addressBookId); |
|
| 730 | + $objectRow = $this->getCard($addressBookId, $cardUri); |
|
| 731 | + $this->dispatcher->dispatchTyped(new CardUpdatedEvent($addressBookId, $addressBookData, $shares, $objectRow)); |
|
| 732 | + return '"' . $etag . '"'; |
|
| 733 | + } |
|
| 734 | + |
|
| 735 | + /** |
|
| 736 | + * Deletes a card |
|
| 737 | + * |
|
| 738 | + * @param mixed $addressBookId |
|
| 739 | + * @param string $cardUri |
|
| 740 | + * @return bool |
|
| 741 | + */ |
|
| 742 | + public function deleteCard($addressBookId, $cardUri) { |
|
| 743 | + $addressBookData = $this->getAddressBookById($addressBookId); |
|
| 744 | + $shares = $this->getShares($addressBookId); |
|
| 745 | + $objectRow = $this->getCard($addressBookId, $cardUri); |
|
| 746 | + |
|
| 747 | + try { |
|
| 748 | + $cardId = $this->getCardId($addressBookId, $cardUri); |
|
| 749 | + } catch (\InvalidArgumentException $e) { |
|
| 750 | + $cardId = null; |
|
| 751 | + } |
|
| 752 | + $query = $this->db->getQueryBuilder(); |
|
| 753 | + $ret = $query->delete($this->dbCardsTable) |
|
| 754 | + ->where($query->expr()->eq('addressbookid', $query->createNamedParameter($addressBookId))) |
|
| 755 | + ->andWhere($query->expr()->eq('uri', $query->createNamedParameter($cardUri))) |
|
| 756 | + ->executeStatement(); |
|
| 757 | + |
|
| 758 | + $this->addChange($addressBookId, $cardUri, 3); |
|
| 759 | + |
|
| 760 | + if ($ret === 1) { |
|
| 761 | + if ($cardId !== null) { |
|
| 762 | + $this->dispatcher->dispatchTyped(new CardDeletedEvent($addressBookId, $addressBookData, $shares, $objectRow)); |
|
| 763 | + $this->purgeProperties($addressBookId, $cardId); |
|
| 764 | + } |
|
| 765 | + return true; |
|
| 766 | + } |
|
| 767 | + |
|
| 768 | + return false; |
|
| 769 | + } |
|
| 770 | + |
|
| 771 | + /** |
|
| 772 | + * The getChanges method returns all the changes that have happened, since |
|
| 773 | + * the specified syncToken in the specified address book. |
|
| 774 | + * |
|
| 775 | + * This function should return an array, such as the following: |
|
| 776 | + * |
|
| 777 | + * [ |
|
| 778 | + * 'syncToken' => 'The current synctoken', |
|
| 779 | + * 'added' => [ |
|
| 780 | + * 'new.txt', |
|
| 781 | + * ], |
|
| 782 | + * 'modified' => [ |
|
| 783 | + * 'modified.txt', |
|
| 784 | + * ], |
|
| 785 | + * 'deleted' => [ |
|
| 786 | + * 'foo.php.bak', |
|
| 787 | + * 'old.txt' |
|
| 788 | + * ] |
|
| 789 | + * ]; |
|
| 790 | + * |
|
| 791 | + * The returned syncToken property should reflect the *current* syncToken |
|
| 792 | + * of the calendar, as reported in the {http://sabredav.org/ns}sync-token |
|
| 793 | + * property. This is needed here too, to ensure the operation is atomic. |
|
| 794 | + * |
|
| 795 | + * If the $syncToken argument is specified as null, this is an initial |
|
| 796 | + * sync, and all members should be reported. |
|
| 797 | + * |
|
| 798 | + * The modified property is an array of nodenames that have changed since |
|
| 799 | + * the last token. |
|
| 800 | + * |
|
| 801 | + * The deleted property is an array with nodenames, that have been deleted |
|
| 802 | + * from collection. |
|
| 803 | + * |
|
| 804 | + * The $syncLevel argument is basically the 'depth' of the report. If it's |
|
| 805 | + * 1, you only have to report changes that happened only directly in |
|
| 806 | + * immediate descendants. If it's 2, it should also include changes from |
|
| 807 | + * the nodes below the child collections. (grandchildren) |
|
| 808 | + * |
|
| 809 | + * The $limit argument allows a client to specify how many results should |
|
| 810 | + * be returned at most. If the limit is not specified, it should be treated |
|
| 811 | + * as infinite. |
|
| 812 | + * |
|
| 813 | + * If the limit (infinite or not) is higher than you're willing to return, |
|
| 814 | + * you should throw a Sabre\DAV\Exception\TooMuchMatches() exception. |
|
| 815 | + * |
|
| 816 | + * If the syncToken is expired (due to data cleanup) or unknown, you must |
|
| 817 | + * return null. |
|
| 818 | + * |
|
| 819 | + * The limit is 'suggestive'. You are free to ignore it. |
|
| 820 | + * |
|
| 821 | + * @param string $addressBookId |
|
| 822 | + * @param string $syncToken |
|
| 823 | + * @param int $syncLevel |
|
| 824 | + * @param int|null $limit |
|
| 825 | + * @return array |
|
| 826 | + */ |
|
| 827 | + public function getChangesForAddressBook($addressBookId, $syncToken, $syncLevel, $limit = null) { |
|
| 828 | + // Current synctoken |
|
| 829 | + $qb = $this->db->getQueryBuilder(); |
|
| 830 | + $qb->select('synctoken') |
|
| 831 | + ->from('addressbooks') |
|
| 832 | + ->where( |
|
| 833 | + $qb->expr()->eq('id', $qb->createNamedParameter($addressBookId)) |
|
| 834 | + ); |
|
| 835 | + $stmt = $qb->executeQuery(); |
|
| 836 | + $currentToken = $stmt->fetchOne(); |
|
| 837 | + $stmt->closeCursor(); |
|
| 838 | + |
|
| 839 | + if (is_null($currentToken)) { |
|
| 840 | + return []; |
|
| 841 | + } |
|
| 842 | + |
|
| 843 | + $result = [ |
|
| 844 | + 'syncToken' => $currentToken, |
|
| 845 | + 'added' => [], |
|
| 846 | + 'modified' => [], |
|
| 847 | + 'deleted' => [], |
|
| 848 | + ]; |
|
| 849 | + |
|
| 850 | + if ($syncToken) { |
|
| 851 | + $qb = $this->db->getQueryBuilder(); |
|
| 852 | + $qb->select('uri', 'operation') |
|
| 853 | + ->from('addressbookchanges') |
|
| 854 | + ->where( |
|
| 855 | + $qb->expr()->andX( |
|
| 856 | + $qb->expr()->gte('synctoken', $qb->createNamedParameter($syncToken)), |
|
| 857 | + $qb->expr()->lt('synctoken', $qb->createNamedParameter($currentToken)), |
|
| 858 | + $qb->expr()->eq('addressbookid', $qb->createNamedParameter($addressBookId)) |
|
| 859 | + ) |
|
| 860 | + )->orderBy('synctoken'); |
|
| 861 | + |
|
| 862 | + if (is_int($limit) && $limit > 0) { |
|
| 863 | + $qb->setMaxResults($limit); |
|
| 864 | + } |
|
| 865 | + |
|
| 866 | + // Fetching all changes |
|
| 867 | + $stmt = $qb->executeQuery(); |
|
| 868 | + |
|
| 869 | + $changes = []; |
|
| 870 | + |
|
| 871 | + // This loop ensures that any duplicates are overwritten, only the |
|
| 872 | + // last change on a node is relevant. |
|
| 873 | + while ($row = $stmt->fetch(\PDO::FETCH_ASSOC)) { |
|
| 874 | + $changes[$row['uri']] = $row['operation']; |
|
| 875 | + } |
|
| 876 | + $stmt->closeCursor(); |
|
| 877 | + |
|
| 878 | + foreach ($changes as $uri => $operation) { |
|
| 879 | + switch ($operation) { |
|
| 880 | + case 1: |
|
| 881 | + $result['added'][] = $uri; |
|
| 882 | + break; |
|
| 883 | + case 2: |
|
| 884 | + $result['modified'][] = $uri; |
|
| 885 | + break; |
|
| 886 | + case 3: |
|
| 887 | + $result['deleted'][] = $uri; |
|
| 888 | + break; |
|
| 889 | + } |
|
| 890 | + } |
|
| 891 | + } else { |
|
| 892 | + $qb = $this->db->getQueryBuilder(); |
|
| 893 | + $qb->select('uri') |
|
| 894 | + ->from('cards') |
|
| 895 | + ->where( |
|
| 896 | + $qb->expr()->eq('addressbookid', $qb->createNamedParameter($addressBookId)) |
|
| 897 | + ); |
|
| 898 | + // No synctoken supplied, this is the initial sync. |
|
| 899 | + $stmt = $qb->executeQuery(); |
|
| 900 | + $result['added'] = $stmt->fetchAll(\PDO::FETCH_COLUMN); |
|
| 901 | + $stmt->closeCursor(); |
|
| 902 | + } |
|
| 903 | + return $result; |
|
| 904 | + } |
|
| 905 | + |
|
| 906 | + /** |
|
| 907 | + * Adds a change record to the addressbookchanges table. |
|
| 908 | + * |
|
| 909 | + * @param mixed $addressBookId |
|
| 910 | + * @param string $objectUri |
|
| 911 | + * @param int $operation 1 = add, 2 = modify, 3 = delete |
|
| 912 | + * @return void |
|
| 913 | + */ |
|
| 914 | + protected function addChange($addressBookId, $objectUri, $operation) { |
|
| 915 | + $sql = 'INSERT INTO `*PREFIX*addressbookchanges`(`uri`, `synctoken`, `addressbookid`, `operation`) SELECT ?, `synctoken`, ?, ? FROM `*PREFIX*addressbooks` WHERE `id` = ?'; |
|
| 916 | + $stmt = $this->db->prepare($sql); |
|
| 917 | + $stmt->execute([ |
|
| 918 | + $objectUri, |
|
| 919 | + $addressBookId, |
|
| 920 | + $operation, |
|
| 921 | + $addressBookId |
|
| 922 | + ]); |
|
| 923 | + $stmt = $this->db->prepare('UPDATE `*PREFIX*addressbooks` SET `synctoken` = `synctoken` + 1 WHERE `id` = ?'); |
|
| 924 | + $stmt->execute([ |
|
| 925 | + $addressBookId |
|
| 926 | + ]); |
|
| 927 | + } |
|
| 928 | + |
|
| 929 | + /** |
|
| 930 | + * @param resource|string $cardData |
|
| 931 | + * @param bool $modified |
|
| 932 | + * @return string |
|
| 933 | + */ |
|
| 934 | + private function readBlob($cardData, &$modified = false) { |
|
| 935 | + if (is_resource($cardData)) { |
|
| 936 | + $cardData = stream_get_contents($cardData); |
|
| 937 | + } |
|
| 938 | + |
|
| 939 | + // Micro optimisation |
|
| 940 | + // don't loop through |
|
| 941 | + if (strpos($cardData, 'PHOTO:data:') === 0) { |
|
| 942 | + return $cardData; |
|
| 943 | + } |
|
| 944 | + |
|
| 945 | + $cardDataArray = explode("\r\n", $cardData); |
|
| 946 | + |
|
| 947 | + $cardDataFiltered = []; |
|
| 948 | + $removingPhoto = false; |
|
| 949 | + foreach ($cardDataArray as $line) { |
|
| 950 | + if (strpos($line, 'PHOTO:data:') === 0 |
|
| 951 | + && strpos($line, 'PHOTO:data:image/') !== 0) { |
|
| 952 | + // Filter out PHOTO data of non-images |
|
| 953 | + $removingPhoto = true; |
|
| 954 | + $modified = true; |
|
| 955 | + continue; |
|
| 956 | + } |
|
| 957 | + |
|
| 958 | + if ($removingPhoto) { |
|
| 959 | + if (strpos($line, ' ') === 0) { |
|
| 960 | + continue; |
|
| 961 | + } |
|
| 962 | + // No leading space means this is a new property |
|
| 963 | + $removingPhoto = false; |
|
| 964 | + } |
|
| 965 | + |
|
| 966 | + $cardDataFiltered[] = $line; |
|
| 967 | + } |
|
| 968 | + return implode("\r\n", $cardDataFiltered); |
|
| 969 | + } |
|
| 970 | + |
|
| 971 | + /** |
|
| 972 | + * @param list<array{href: string, commonName: string, readOnly: bool}> $add |
|
| 973 | + * @param list<string> $remove |
|
| 974 | + */ |
|
| 975 | + public function updateShares(IShareable $shareable, array $add, array $remove): void { |
|
| 976 | + $addressBookId = $shareable->getResourceId(); |
|
| 977 | + $addressBookData = $this->getAddressBookById($addressBookId); |
|
| 978 | + $oldShares = $this->getShares($addressBookId); |
|
| 979 | + |
|
| 980 | + $this->sharingBackend->updateShares($shareable, $add, $remove); |
|
| 981 | + |
|
| 982 | + $this->dispatcher->dispatchTyped(new AddressBookShareUpdatedEvent($addressBookId, $addressBookData, $oldShares, $add, $remove)); |
|
| 983 | + } |
|
| 984 | + |
|
| 985 | + /** |
|
| 986 | + * Search contacts in a specific address-book |
|
| 987 | + * |
|
| 988 | + * @param int $addressBookId |
|
| 989 | + * @param string $pattern which should match within the $searchProperties |
|
| 990 | + * @param array $searchProperties defines the properties within the query pattern should match |
|
| 991 | + * @param array $options = array() to define the search behavior |
|
| 992 | + * - 'escape_like_param' - If set to false wildcards _ and % are not escaped, otherwise they are |
|
| 993 | + * - 'limit' - Set a numeric limit for the search results |
|
| 994 | + * - 'offset' - Set the offset for the limited search results |
|
| 995 | + * - 'wildcard' - Whether the search should use wildcards |
|
| 996 | + * @psalm-param array{escape_like_param?: bool, limit?: int, offset?: int, wildcard?: bool} $options |
|
| 997 | + * @return array an array of contacts which are arrays of key-value-pairs |
|
| 998 | + */ |
|
| 999 | + public function search($addressBookId, $pattern, $searchProperties, $options = []): array { |
|
| 1000 | + return $this->searchByAddressBookIds([$addressBookId], $pattern, $searchProperties, $options); |
|
| 1001 | + } |
|
| 1002 | + |
|
| 1003 | + /** |
|
| 1004 | + * Search contacts in all address-books accessible by a user |
|
| 1005 | + * |
|
| 1006 | + * @param string $principalUri |
|
| 1007 | + * @param string $pattern |
|
| 1008 | + * @param array $searchProperties |
|
| 1009 | + * @param array $options |
|
| 1010 | + * @return array |
|
| 1011 | + */ |
|
| 1012 | + public function searchPrincipalUri(string $principalUri, |
|
| 1013 | + string $pattern, |
|
| 1014 | + array $searchProperties, |
|
| 1015 | + array $options = []): array { |
|
| 1016 | + $addressBookIds = array_map(static function ($row):int { |
|
| 1017 | + return (int) $row['id']; |
|
| 1018 | + }, $this->getAddressBooksForUser($principalUri)); |
|
| 1019 | + |
|
| 1020 | + return $this->searchByAddressBookIds($addressBookIds, $pattern, $searchProperties, $options); |
|
| 1021 | + } |
|
| 1022 | + |
|
| 1023 | + /** |
|
| 1024 | + * @param array $addressBookIds |
|
| 1025 | + * @param string $pattern |
|
| 1026 | + * @param array $searchProperties |
|
| 1027 | + * @param array $options |
|
| 1028 | + * @psalm-param array{types?: bool, escape_like_param?: bool, limit?: int, offset?: int, wildcard?: bool} $options |
|
| 1029 | + * @return array |
|
| 1030 | + */ |
|
| 1031 | + private function searchByAddressBookIds(array $addressBookIds, |
|
| 1032 | + string $pattern, |
|
| 1033 | + array $searchProperties, |
|
| 1034 | + array $options = []): array { |
|
| 1035 | + $escapePattern = !\array_key_exists('escape_like_param', $options) || $options['escape_like_param'] !== false; |
|
| 1036 | + $useWildcards = !\array_key_exists('wildcard', $options) || $options['wildcard'] !== false; |
|
| 1037 | + |
|
| 1038 | + $query2 = $this->db->getQueryBuilder(); |
|
| 1039 | + |
|
| 1040 | + $addressBookOr = $query2->expr()->orX(); |
|
| 1041 | + foreach ($addressBookIds as $addressBookId) { |
|
| 1042 | + $addressBookOr->add($query2->expr()->eq('cp.addressbookid', $query2->createNamedParameter($addressBookId))); |
|
| 1043 | + } |
|
| 1044 | + |
|
| 1045 | + if ($addressBookOr->count() === 0) { |
|
| 1046 | + return []; |
|
| 1047 | + } |
|
| 1048 | + |
|
| 1049 | + $propertyOr = $query2->expr()->orX(); |
|
| 1050 | + foreach ($searchProperties as $property) { |
|
| 1051 | + if ($escapePattern) { |
|
| 1052 | + if ($property === 'EMAIL' && strpos($pattern, ' ') !== false) { |
|
| 1053 | + // There can be no spaces in emails |
|
| 1054 | + continue; |
|
| 1055 | + } |
|
| 1056 | + |
|
| 1057 | + if ($property === 'CLOUD' && preg_match('/[^a-zA-Z0-9 :_.@\/\-\']/', $pattern) === 1) { |
|
| 1058 | + // There can be no chars in cloud ids which are not valid for user ids plus :/ |
|
| 1059 | + // worst case: CA61590A-BBBC-423E-84AF-E6DF01455A53@https://my.nxt/srv/ |
|
| 1060 | + continue; |
|
| 1061 | + } |
|
| 1062 | + } |
|
| 1063 | + |
|
| 1064 | + $propertyOr->add($query2->expr()->eq('cp.name', $query2->createNamedParameter($property))); |
|
| 1065 | + } |
|
| 1066 | + |
|
| 1067 | + if ($propertyOr->count() === 0) { |
|
| 1068 | + return []; |
|
| 1069 | + } |
|
| 1070 | + |
|
| 1071 | + $query2->selectDistinct('cp.cardid') |
|
| 1072 | + ->from($this->dbCardsPropertiesTable, 'cp') |
|
| 1073 | + ->andWhere($addressBookOr) |
|
| 1074 | + ->andWhere($propertyOr); |
|
| 1075 | + |
|
| 1076 | + // No need for like when the pattern is empty |
|
| 1077 | + if ('' !== $pattern) { |
|
| 1078 | + if (!$useWildcards) { |
|
| 1079 | + $query2->andWhere($query2->expr()->eq('cp.value', $query2->createNamedParameter($pattern))); |
|
| 1080 | + } elseif (!$escapePattern) { |
|
| 1081 | + $query2->andWhere($query2->expr()->ilike('cp.value', $query2->createNamedParameter($pattern))); |
|
| 1082 | + } else { |
|
| 1083 | + $query2->andWhere($query2->expr()->ilike('cp.value', $query2->createNamedParameter('%' . $this->db->escapeLikeParameter($pattern) . '%'))); |
|
| 1084 | + } |
|
| 1085 | + } |
|
| 1086 | + |
|
| 1087 | + if (isset($options['limit'])) { |
|
| 1088 | + $query2->setMaxResults($options['limit']); |
|
| 1089 | + } |
|
| 1090 | + if (isset($options['offset'])) { |
|
| 1091 | + $query2->setFirstResult($options['offset']); |
|
| 1092 | + } |
|
| 1093 | + |
|
| 1094 | + $result = $query2->execute(); |
|
| 1095 | + $matches = $result->fetchAll(); |
|
| 1096 | + $result->closeCursor(); |
|
| 1097 | + $matches = array_map(function ($match) { |
|
| 1098 | + return (int)$match['cardid']; |
|
| 1099 | + }, $matches); |
|
| 1100 | + |
|
| 1101 | + $cards = []; |
|
| 1102 | + $query = $this->db->getQueryBuilder(); |
|
| 1103 | + $query->select('c.addressbookid', 'c.carddata', 'c.uri') |
|
| 1104 | + ->from($this->dbCardsTable, 'c') |
|
| 1105 | + ->where($query->expr()->in('c.id', $query->createParameter('matches'))); |
|
| 1106 | + |
|
| 1107 | + foreach (array_chunk($matches, 1000) as $matchesChunk) { |
|
| 1108 | + $query->setParameter('matches', $matchesChunk, IQueryBuilder::PARAM_INT_ARRAY); |
|
| 1109 | + $result = $query->executeQuery(); |
|
| 1110 | + $cards = array_merge($cards, $result->fetchAll()); |
|
| 1111 | + $result->closeCursor(); |
|
| 1112 | + } |
|
| 1113 | + |
|
| 1114 | + return array_map(function ($array) { |
|
| 1115 | + $array['addressbookid'] = (int) $array['addressbookid']; |
|
| 1116 | + $modified = false; |
|
| 1117 | + $array['carddata'] = $this->readBlob($array['carddata'], $modified); |
|
| 1118 | + if ($modified) { |
|
| 1119 | + $array['size'] = strlen($array['carddata']); |
|
| 1120 | + } |
|
| 1121 | + return $array; |
|
| 1122 | + }, $cards); |
|
| 1123 | + } |
|
| 1124 | + |
|
| 1125 | + /** |
|
| 1126 | + * @param int $bookId |
|
| 1127 | + * @param string $name |
|
| 1128 | + * @return array |
|
| 1129 | + */ |
|
| 1130 | + public function collectCardProperties($bookId, $name) { |
|
| 1131 | + $query = $this->db->getQueryBuilder(); |
|
| 1132 | + $result = $query->selectDistinct('value') |
|
| 1133 | + ->from($this->dbCardsPropertiesTable) |
|
| 1134 | + ->where($query->expr()->eq('name', $query->createNamedParameter($name))) |
|
| 1135 | + ->andWhere($query->expr()->eq('addressbookid', $query->createNamedParameter($bookId))) |
|
| 1136 | + ->execute(); |
|
| 1137 | + |
|
| 1138 | + $all = $result->fetchAll(PDO::FETCH_COLUMN); |
|
| 1139 | + $result->closeCursor(); |
|
| 1140 | + |
|
| 1141 | + return $all; |
|
| 1142 | + } |
|
| 1143 | + |
|
| 1144 | + /** |
|
| 1145 | + * get URI from a given contact |
|
| 1146 | + * |
|
| 1147 | + * @param int $id |
|
| 1148 | + * @return string |
|
| 1149 | + */ |
|
| 1150 | + public function getCardUri($id) { |
|
| 1151 | + $query = $this->db->getQueryBuilder(); |
|
| 1152 | + $query->select('uri')->from($this->dbCardsTable) |
|
| 1153 | + ->where($query->expr()->eq('id', $query->createParameter('id'))) |
|
| 1154 | + ->setParameter('id', $id); |
|
| 1155 | + |
|
| 1156 | + $result = $query->execute(); |
|
| 1157 | + $uri = $result->fetch(); |
|
| 1158 | + $result->closeCursor(); |
|
| 1159 | + |
|
| 1160 | + if (!isset($uri['uri'])) { |
|
| 1161 | + throw new \InvalidArgumentException('Card does not exists: ' . $id); |
|
| 1162 | + } |
|
| 1163 | + |
|
| 1164 | + return $uri['uri']; |
|
| 1165 | + } |
|
| 1166 | + |
|
| 1167 | + /** |
|
| 1168 | + * return contact with the given URI |
|
| 1169 | + * |
|
| 1170 | + * @param int $addressBookId |
|
| 1171 | + * @param string $uri |
|
| 1172 | + * @returns array |
|
| 1173 | + */ |
|
| 1174 | + public function getContact($addressBookId, $uri) { |
|
| 1175 | + $result = []; |
|
| 1176 | + $query = $this->db->getQueryBuilder(); |
|
| 1177 | + $query->select('*')->from($this->dbCardsTable) |
|
| 1178 | + ->where($query->expr()->eq('uri', $query->createNamedParameter($uri))) |
|
| 1179 | + ->andWhere($query->expr()->eq('addressbookid', $query->createNamedParameter($addressBookId))); |
|
| 1180 | + $queryResult = $query->execute(); |
|
| 1181 | + $contact = $queryResult->fetch(); |
|
| 1182 | + $queryResult->closeCursor(); |
|
| 1183 | + |
|
| 1184 | + if (is_array($contact)) { |
|
| 1185 | + $modified = false; |
|
| 1186 | + $contact['etag'] = '"' . $contact['etag'] . '"'; |
|
| 1187 | + $contact['carddata'] = $this->readBlob($contact['carddata'], $modified); |
|
| 1188 | + if ($modified) { |
|
| 1189 | + $contact['size'] = strlen($contact['carddata']); |
|
| 1190 | + } |
|
| 1191 | + |
|
| 1192 | + $result = $contact; |
|
| 1193 | + } |
|
| 1194 | + |
|
| 1195 | + return $result; |
|
| 1196 | + } |
|
| 1197 | + |
|
| 1198 | + /** |
|
| 1199 | + * Returns the list of people whom this address book is shared with. |
|
| 1200 | + * |
|
| 1201 | + * Every element in this array should have the following properties: |
|
| 1202 | + * * href - Often a mailto: address |
|
| 1203 | + * * commonName - Optional, for example a first + last name |
|
| 1204 | + * * status - See the Sabre\CalDAV\SharingPlugin::STATUS_ constants. |
|
| 1205 | + * * readOnly - boolean |
|
| 1206 | + * |
|
| 1207 | + * @return list<array{href: string, commonName: string, status: int, readOnly: bool, '{http://owncloud.org/ns}principal': string, '{http://owncloud.org/ns}group-share': bool}> |
|
| 1208 | + */ |
|
| 1209 | + public function getShares(int $addressBookId): array { |
|
| 1210 | + return $this->sharingBackend->getShares($addressBookId); |
|
| 1211 | + } |
|
| 1212 | + |
|
| 1213 | + /** |
|
| 1214 | + * update properties table |
|
| 1215 | + * |
|
| 1216 | + * @param int $addressBookId |
|
| 1217 | + * @param string $cardUri |
|
| 1218 | + * @param string $vCardSerialized |
|
| 1219 | + */ |
|
| 1220 | + protected function updateProperties($addressBookId, $cardUri, $vCardSerialized) { |
|
| 1221 | + $cardId = $this->getCardId($addressBookId, $cardUri); |
|
| 1222 | + $vCard = $this->readCard($vCardSerialized); |
|
| 1223 | + |
|
| 1224 | + $this->purgeProperties($addressBookId, $cardId); |
|
| 1225 | + |
|
| 1226 | + $query = $this->db->getQueryBuilder(); |
|
| 1227 | + $query->insert($this->dbCardsPropertiesTable) |
|
| 1228 | + ->values( |
|
| 1229 | + [ |
|
| 1230 | + 'addressbookid' => $query->createNamedParameter($addressBookId), |
|
| 1231 | + 'cardid' => $query->createNamedParameter($cardId), |
|
| 1232 | + 'name' => $query->createParameter('name'), |
|
| 1233 | + 'value' => $query->createParameter('value'), |
|
| 1234 | + 'preferred' => $query->createParameter('preferred') |
|
| 1235 | + ] |
|
| 1236 | + ); |
|
| 1237 | + |
|
| 1238 | + |
|
| 1239 | + $this->db->beginTransaction(); |
|
| 1240 | + |
|
| 1241 | + try { |
|
| 1242 | + foreach ($vCard->children() as $property) { |
|
| 1243 | + if (!in_array($property->name, self::$indexProperties)) { |
|
| 1244 | + continue; |
|
| 1245 | + } |
|
| 1246 | + $preferred = 0; |
|
| 1247 | + foreach ($property->parameters as $parameter) { |
|
| 1248 | + if ($parameter->name === 'TYPE' && strtoupper($parameter->getValue()) === 'PREF') { |
|
| 1249 | + $preferred = 1; |
|
| 1250 | + break; |
|
| 1251 | + } |
|
| 1252 | + } |
|
| 1253 | + $query->setParameter('name', $property->name); |
|
| 1254 | + $query->setParameter('value', mb_strcut($property->getValue(), 0, 254)); |
|
| 1255 | + $query->setParameter('preferred', $preferred); |
|
| 1256 | + $query->execute(); |
|
| 1257 | + } |
|
| 1258 | + $this->db->commit(); |
|
| 1259 | + } catch (\Exception $e) { |
|
| 1260 | + $this->db->rollBack(); |
|
| 1261 | + } |
|
| 1262 | + } |
|
| 1263 | + |
|
| 1264 | + /** |
|
| 1265 | + * read vCard data into a vCard object |
|
| 1266 | + * |
|
| 1267 | + * @param string $cardData |
|
| 1268 | + * @return VCard |
|
| 1269 | + */ |
|
| 1270 | + protected function readCard($cardData) { |
|
| 1271 | + return Reader::read($cardData); |
|
| 1272 | + } |
|
| 1273 | + |
|
| 1274 | + /** |
|
| 1275 | + * delete all properties from a given card |
|
| 1276 | + * |
|
| 1277 | + * @param int $addressBookId |
|
| 1278 | + * @param int $cardId |
|
| 1279 | + */ |
|
| 1280 | + protected function purgeProperties($addressBookId, $cardId) { |
|
| 1281 | + $query = $this->db->getQueryBuilder(); |
|
| 1282 | + $query->delete($this->dbCardsPropertiesTable) |
|
| 1283 | + ->where($query->expr()->eq('cardid', $query->createNamedParameter($cardId))) |
|
| 1284 | + ->andWhere($query->expr()->eq('addressbookid', $query->createNamedParameter($addressBookId))); |
|
| 1285 | + $query->execute(); |
|
| 1286 | + } |
|
| 1287 | + |
|
| 1288 | + /** |
|
| 1289 | + * Get ID from a given contact |
|
| 1290 | + */ |
|
| 1291 | + protected function getCardId(int $addressBookId, string $uri): int { |
|
| 1292 | + $query = $this->db->getQueryBuilder(); |
|
| 1293 | + $query->select('id')->from($this->dbCardsTable) |
|
| 1294 | + ->where($query->expr()->eq('uri', $query->createNamedParameter($uri))) |
|
| 1295 | + ->andWhere($query->expr()->eq('addressbookid', $query->createNamedParameter($addressBookId))); |
|
| 1296 | + |
|
| 1297 | + $result = $query->execute(); |
|
| 1298 | + $cardIds = $result->fetch(); |
|
| 1299 | + $result->closeCursor(); |
|
| 1300 | + |
|
| 1301 | + if (!isset($cardIds['id'])) { |
|
| 1302 | + throw new \InvalidArgumentException('Card does not exists: ' . $uri); |
|
| 1303 | + } |
|
| 1304 | + |
|
| 1305 | + return (int)$cardIds['id']; |
|
| 1306 | + } |
|
| 1307 | + |
|
| 1308 | + /** |
|
| 1309 | + * For shared address books the sharee is set in the ACL of the address book |
|
| 1310 | + * |
|
| 1311 | + * @param int $addressBookId |
|
| 1312 | + * @param list<array{privilege: string, principal: string, protected: bool}> $acl |
|
| 1313 | + * @return list<array{privilege: string, principal: string, protected: bool}> |
|
| 1314 | + */ |
|
| 1315 | + public function applyShareAcl(int $addressBookId, array $acl): array { |
|
| 1316 | + return $this->sharingBackend->applyShareAcl($addressBookId, $acl); |
|
| 1317 | + } |
|
| 1318 | + |
|
| 1319 | + /** |
|
| 1320 | + * @throws \InvalidArgumentException |
|
| 1321 | + */ |
|
| 1322 | + public function pruneOutdatedSyncTokens(int $keep = 10_000): int { |
|
| 1323 | + if ($keep < 0) { |
|
| 1324 | + throw new \InvalidArgumentException(); |
|
| 1325 | + } |
|
| 1326 | + $query = $this->db->getQueryBuilder(); |
|
| 1327 | + $query->delete('addressbookchanges') |
|
| 1328 | + ->orderBy('id', 'DESC') |
|
| 1329 | + ->setFirstResult($keep); |
|
| 1330 | + return $query->executeStatement(); |
|
| 1331 | + } |
|
| 1332 | + |
|
| 1333 | + private function convertPrincipal(string $principalUri, bool $toV2): string { |
|
| 1334 | + if ($this->principalBackend->getPrincipalPrefix() === 'principals') { |
|
| 1335 | + [, $name] = \Sabre\Uri\split($principalUri); |
|
| 1336 | + if ($toV2 === true) { |
|
| 1337 | + return "principals/users/$name"; |
|
| 1338 | + } |
|
| 1339 | + return "principals/$name"; |
|
| 1340 | + } |
|
| 1341 | + return $principalUri; |
|
| 1342 | + } |
|
| 1343 | + |
|
| 1344 | + private function addOwnerPrincipal(array &$addressbookInfo): void { |
|
| 1345 | + $ownerPrincipalKey = '{' . \OCA\DAV\DAV\Sharing\Plugin::NS_OWNCLOUD . '}owner-principal'; |
|
| 1346 | + $displaynameKey = '{' . \OCA\DAV\DAV\Sharing\Plugin::NS_NEXTCLOUD . '}owner-displayname'; |
|
| 1347 | + if (isset($addressbookInfo[$ownerPrincipalKey])) { |
|
| 1348 | + $uri = $addressbookInfo[$ownerPrincipalKey]; |
|
| 1349 | + } else { |
|
| 1350 | + $uri = $addressbookInfo['principaluri']; |
|
| 1351 | + } |
|
| 1352 | + |
|
| 1353 | + $principalInformation = $this->principalBackend->getPrincipalByPath($uri); |
|
| 1354 | + if (isset($principalInformation['{DAV:}displayname'])) { |
|
| 1355 | + $addressbookInfo[$displaynameKey] = $principalInformation['{DAV:}displayname']; |
|
| 1356 | + } |
|
| 1357 | + } |
|
| 1358 | + |
|
| 1359 | + /** |
|
| 1360 | + * Extract UID from vcard |
|
| 1361 | + * |
|
| 1362 | + * @param string $cardData the vcard raw data |
|
| 1363 | + * @return string the uid |
|
| 1364 | + * @throws BadRequest if no UID is available or vcard is empty |
|
| 1365 | + */ |
|
| 1366 | + private function getUID(string $cardData): string { |
|
| 1367 | + if ($cardData !== '') { |
|
| 1368 | + $vCard = Reader::read($cardData); |
|
| 1369 | + if ($vCard->UID) { |
|
| 1370 | + $uid = $vCard->UID->getValue(); |
|
| 1371 | + return $uid; |
|
| 1372 | + } |
|
| 1373 | + // should already be handled, but just in case |
|
| 1374 | + throw new BadRequest('vCards on CardDAV servers MUST have a UID property'); |
|
| 1375 | + } |
|
| 1376 | + // should already be handled, but just in case |
|
| 1377 | + throw new BadRequest('vCard can not be empty'); |
|
| 1378 | + } |
|
| 1379 | 1379 | } |