Passed
Push — master ( 62403d...0c3e2f )
by Joas
14:50 queued 14s
created
apps/dav/lib/CalDAV/CalDavBackend.php 1 patch
Indentation   +2560 added lines, -2560 removed lines patch added patch discarded remove patch
@@ -76,2564 +76,2564 @@
 block discarded – undo
76 76
  */
77 77
 class CalDavBackend extends AbstractBackend implements SyncSupport, SubscriptionSupport, SchedulingSupport {
78 78
 
79
-	const CALENDAR_TYPE_CALENDAR = 0;
80
-	const CALENDAR_TYPE_SUBSCRIPTION = 1;
81
-
82
-	const PERSONAL_CALENDAR_URI = 'personal';
83
-	const PERSONAL_CALENDAR_NAME = 'Personal';
84
-
85
-	const RESOURCE_BOOKING_CALENDAR_URI = 'calendar';
86
-	const RESOURCE_BOOKING_CALENDAR_NAME = 'Calendar';
87
-
88
-	/**
89
-	 * We need to specify a max date, because we need to stop *somewhere*
90
-	 *
91
-	 * On 32 bit system the maximum for a signed integer is 2147483647, so
92
-	 * MAX_DATE cannot be higher than date('Y-m-d', 2147483647) which results
93
-	 * in 2038-01-19 to avoid problems when the date is converted
94
-	 * to a unix timestamp.
95
-	 */
96
-	const MAX_DATE = '2038-01-01';
97
-
98
-	const ACCESS_PUBLIC = 4;
99
-	const CLASSIFICATION_PUBLIC = 0;
100
-	const CLASSIFICATION_PRIVATE = 1;
101
-	const CLASSIFICATION_CONFIDENTIAL = 2;
102
-
103
-	/**
104
-	 * List of CalDAV properties, and how they map to database field names
105
-	 * Add your own properties by simply adding on to this array.
106
-	 *
107
-	 * Note that only string-based properties are supported here.
108
-	 *
109
-	 * @var array
110
-	 */
111
-	public $propertyMap = [
112
-		'{DAV:}displayname'                          => 'displayname',
113
-		'{urn:ietf:params:xml:ns:caldav}calendar-description' => 'description',
114
-		'{urn:ietf:params:xml:ns:caldav}calendar-timezone'    => 'timezone',
115
-		'{http://apple.com/ns/ical/}calendar-order'  => 'calendarorder',
116
-		'{http://apple.com/ns/ical/}calendar-color'  => 'calendarcolor',
117
-	];
118
-
119
-	/**
120
-	 * List of subscription properties, and how they map to database field names.
121
-	 *
122
-	 * @var array
123
-	 */
124
-	public $subscriptionPropertyMap = [
125
-		'{DAV:}displayname'                                           => 'displayname',
126
-		'{http://apple.com/ns/ical/}refreshrate'                      => 'refreshrate',
127
-		'{http://apple.com/ns/ical/}calendar-order'                   => 'calendarorder',
128
-		'{http://apple.com/ns/ical/}calendar-color'                   => 'calendarcolor',
129
-		'{http://calendarserver.org/ns/}subscribed-strip-todos'       => 'striptodos',
130
-		'{http://calendarserver.org/ns/}subscribed-strip-alarms'      => 'stripalarms',
131
-		'{http://calendarserver.org/ns/}subscribed-strip-attachments' => 'stripattachments',
132
-	];
133
-
134
-	/** @var array properties to index */
135
-	public static $indexProperties = ['CATEGORIES', 'COMMENT', 'DESCRIPTION',
136
-		'LOCATION', 'RESOURCES', 'STATUS', 'SUMMARY', 'ATTENDEE', 'CONTACT',
137
-		'ORGANIZER'];
138
-
139
-	/** @var array parameters to index */
140
-	public static $indexParameters = [
141
-		'ATTENDEE' => ['CN'],
142
-		'ORGANIZER' => ['CN'],
143
-	];
144
-
145
-	/**
146
-	 * @var string[] Map of uid => display name
147
-	 */
148
-	protected $userDisplayNames;
149
-
150
-	/** @var IDBConnection */
151
-	private $db;
152
-
153
-	/** @var Backend */
154
-	private $calendarSharingBackend;
155
-
156
-	/** @var Principal */
157
-	private $principalBackend;
158
-
159
-	/** @var IUserManager */
160
-	private $userManager;
161
-
162
-	/** @var ISecureRandom */
163
-	private $random;
164
-
165
-	/** @var ILogger */
166
-	private $logger;
167
-
168
-	/** @var EventDispatcherInterface */
169
-	private $dispatcher;
170
-
171
-	/** @var bool */
172
-	private $legacyEndpoint;
173
-
174
-	/** @var string */
175
-	private $dbObjectPropertiesTable = 'calendarobjects_props';
176
-
177
-	/**
178
-	 * CalDavBackend constructor.
179
-	 *
180
-	 * @param IDBConnection $db
181
-	 * @param Principal $principalBackend
182
-	 * @param IUserManager $userManager
183
-	 * @param IGroupManager $groupManager
184
-	 * @param ISecureRandom $random
185
-	 * @param ILogger $logger
186
-	 * @param EventDispatcherInterface $dispatcher
187
-	 * @param bool $legacyEndpoint
188
-	 */
189
-	public function __construct(IDBConnection $db,
190
-								Principal $principalBackend,
191
-								IUserManager $userManager,
192
-								IGroupManager $groupManager,
193
-								ISecureRandom $random,
194
-								ILogger $logger,
195
-								EventDispatcherInterface $dispatcher,
196
-								bool $legacyEndpoint = false) {
197
-		$this->db = $db;
198
-		$this->principalBackend = $principalBackend;
199
-		$this->userManager = $userManager;
200
-		$this->calendarSharingBackend = new Backend($this->db, $this->userManager, $groupManager, $principalBackend, 'calendar');
201
-		$this->random = $random;
202
-		$this->logger = $logger;
203
-		$this->dispatcher = $dispatcher;
204
-		$this->legacyEndpoint = $legacyEndpoint;
205
-	}
206
-
207
-	/**
208
-	 * Return the number of calendars for a principal
209
-	 *
210
-	 * By default this excludes the automatically generated birthday calendar
211
-	 *
212
-	 * @param $principalUri
213
-	 * @param bool $excludeBirthday
214
-	 * @return int
215
-	 */
216
-	public function getCalendarsForUserCount($principalUri, $excludeBirthday = true) {
217
-		$principalUri = $this->convertPrincipal($principalUri, true);
218
-		$query = $this->db->getQueryBuilder();
219
-		$query->select($query->func()->count('*'))
220
-			->from('calendars')
221
-			->where($query->expr()->eq('principaluri', $query->createNamedParameter($principalUri)));
222
-
223
-		if ($excludeBirthday) {
224
-			$query->andWhere($query->expr()->neq('uri', $query->createNamedParameter(BirthdayService::BIRTHDAY_CALENDAR_URI)));
225
-		}
226
-
227
-		return (int)$query->execute()->fetchColumn();
228
-	}
229
-
230
-	/**
231
-	 * Returns a list of calendars for a principal.
232
-	 *
233
-	 * Every project is an array with the following keys:
234
-	 *  * id, a unique id that will be used by other functions to modify the
235
-	 *    calendar. This can be the same as the uri or a database key.
236
-	 *  * uri, which the basename of the uri with which the calendar is
237
-	 *    accessed.
238
-	 *  * principaluri. The owner of the calendar. Almost always the same as
239
-	 *    principalUri passed to this method.
240
-	 *
241
-	 * Furthermore it can contain webdav properties in clark notation. A very
242
-	 * common one is '{DAV:}displayname'.
243
-	 *
244
-	 * Many clients also require:
245
-	 * {urn:ietf:params:xml:ns:caldav}supported-calendar-component-set
246
-	 * For this property, you can just return an instance of
247
-	 * Sabre\CalDAV\Property\SupportedCalendarComponentSet.
248
-	 *
249
-	 * If you return {http://sabredav.org/ns}read-only and set the value to 1,
250
-	 * ACL will automatically be put in read-only mode.
251
-	 *
252
-	 * @param string $principalUri
253
-	 * @return array
254
-	 */
255
-	function getCalendarsForUser($principalUri) {
256
-		$principalUriOriginal = $principalUri;
257
-		$principalUri = $this->convertPrincipal($principalUri, true);
258
-		$fields = array_values($this->propertyMap);
259
-		$fields[] = 'id';
260
-		$fields[] = 'uri';
261
-		$fields[] = 'synctoken';
262
-		$fields[] = 'components';
263
-		$fields[] = 'principaluri';
264
-		$fields[] = 'transparent';
265
-
266
-		// Making fields a comma-delimited list
267
-		$query = $this->db->getQueryBuilder();
268
-		$query->select($fields)->from('calendars')
269
-				->where($query->expr()->eq('principaluri', $query->createNamedParameter($principalUri)))
270
-				->orderBy('calendarorder', 'ASC');
271
-		$stmt = $query->execute();
272
-
273
-		$calendars = [];
274
-		while($row = $stmt->fetch(\PDO::FETCH_ASSOC)) {
275
-
276
-			$components = [];
277
-			if ($row['components']) {
278
-				$components = explode(',',$row['components']);
279
-			}
280
-
281
-			$calendar = [
282
-				'id' => $row['id'],
283
-				'uri' => $row['uri'],
284
-				'principaluri' => $this->convertPrincipal($row['principaluri'], !$this->legacyEndpoint),
285
-				'{' . Plugin::NS_CALENDARSERVER . '}getctag' => 'http://sabre.io/ns/sync/' . ($row['synctoken']?$row['synctoken']:'0'),
286
-				'{http://sabredav.org/ns}sync-token' => $row['synctoken']?$row['synctoken']:'0',
287
-				'{' . Plugin::NS_CALDAV . '}supported-calendar-component-set' => new SupportedCalendarComponentSet($components),
288
-				'{' . Plugin::NS_CALDAV . '}schedule-calendar-transp' => new ScheduleCalendarTransp($row['transparent']?'transparent':'opaque'),
289
-				'{' . \OCA\DAV\DAV\Sharing\Plugin::NS_OWNCLOUD . '}owner-principal' => $this->convertPrincipal($principalUri, !$this->legacyEndpoint),
290
-			];
291
-
292
-			foreach($this->propertyMap as $xmlName=>$dbName) {
293
-				$calendar[$xmlName] = $row[$dbName];
294
-			}
295
-
296
-			$this->addOwnerPrincipal($calendar);
297
-
298
-			if (!isset($calendars[$calendar['id']])) {
299
-				$calendars[$calendar['id']] = $calendar;
300
-			}
301
-		}
302
-
303
-		$stmt->closeCursor();
304
-
305
-		// query for shared calendars
306
-		$principals = $this->principalBackend->getGroupMembership($principalUriOriginal, true);
307
-		$principals = array_merge($principals, $this->principalBackend->getCircleMembership($principalUriOriginal));
308
-
309
-		$principals = array_map(function($principal) {
310
-			return urldecode($principal);
311
-		}, $principals);
312
-		$principals[]= $principalUri;
313
-
314
-		$fields = array_values($this->propertyMap);
315
-		$fields[] = 'a.id';
316
-		$fields[] = 'a.uri';
317
-		$fields[] = 'a.synctoken';
318
-		$fields[] = 'a.components';
319
-		$fields[] = 'a.principaluri';
320
-		$fields[] = 'a.transparent';
321
-		$fields[] = 's.access';
322
-		$query = $this->db->getQueryBuilder();
323
-		$result = $query->select($fields)
324
-			->from('dav_shares', 's')
325
-			->join('s', 'calendars', 'a', $query->expr()->eq('s.resourceid', 'a.id'))
326
-			->where($query->expr()->in('s.principaluri', $query->createParameter('principaluri')))
327
-			->andWhere($query->expr()->eq('s.type', $query->createParameter('type')))
328
-			->setParameter('type', 'calendar')
329
-			->setParameter('principaluri', $principals, \Doctrine\DBAL\Connection::PARAM_STR_ARRAY)
330
-			->execute();
331
-
332
-		$readOnlyPropertyName = '{' . \OCA\DAV\DAV\Sharing\Plugin::NS_OWNCLOUD . '}read-only';
333
-		while($row = $result->fetch()) {
334
-			if ($row['principaluri'] === $principalUri) {
335
-				continue;
336
-			}
337
-
338
-			$readOnly = (int) $row['access'] === Backend::ACCESS_READ;
339
-			if (isset($calendars[$row['id']])) {
340
-				if ($readOnly) {
341
-					// New share can not have more permissions then the old one.
342
-					continue;
343
-				}
344
-				if (isset($calendars[$row['id']][$readOnlyPropertyName]) &&
345
-					$calendars[$row['id']][$readOnlyPropertyName] === 0) {
346
-					// Old share is already read-write, no more permissions can be gained
347
-					continue;
348
-				}
349
-			}
350
-
351
-			list(, $name) = Uri\split($row['principaluri']);
352
-			$uri = $row['uri'] . '_shared_by_' . $name;
353
-			$row['displayname'] = $row['displayname'] . ' (' . $this->getUserDisplayName($name) . ')';
354
-			$components = [];
355
-			if ($row['components']) {
356
-				$components = explode(',',$row['components']);
357
-			}
358
-			$calendar = [
359
-				'id' => $row['id'],
360
-				'uri' => $uri,
361
-				'principaluri' => $this->convertPrincipal($principalUri, !$this->legacyEndpoint),
362
-				'{' . Plugin::NS_CALENDARSERVER . '}getctag' => 'http://sabre.io/ns/sync/' . ($row['synctoken']?$row['synctoken']:'0'),
363
-				'{http://sabredav.org/ns}sync-token' => $row['synctoken']?$row['synctoken']:'0',
364
-				'{' . Plugin::NS_CALDAV . '}supported-calendar-component-set' => new SupportedCalendarComponentSet($components),
365
-				'{' . Plugin::NS_CALDAV . '}schedule-calendar-transp' => new ScheduleCalendarTransp('transparent'),
366
-				'{' . \OCA\DAV\DAV\Sharing\Plugin::NS_OWNCLOUD . '}owner-principal' => $this->convertPrincipal($row['principaluri'], !$this->legacyEndpoint),
367
-				$readOnlyPropertyName => $readOnly,
368
-			];
369
-
370
-			foreach($this->propertyMap as $xmlName=>$dbName) {
371
-				$calendar[$xmlName] = $row[$dbName];
372
-			}
373
-
374
-			$this->addOwnerPrincipal($calendar);
375
-
376
-			$calendars[$calendar['id']] = $calendar;
377
-		}
378
-		$result->closeCursor();
379
-
380
-		return array_values($calendars);
381
-	}
382
-
383
-	/**
384
-	 * @param $principalUri
385
-	 * @return array
386
-	 */
387
-	public function getUsersOwnCalendars($principalUri) {
388
-		$principalUri = $this->convertPrincipal($principalUri, true);
389
-		$fields = array_values($this->propertyMap);
390
-		$fields[] = 'id';
391
-		$fields[] = 'uri';
392
-		$fields[] = 'synctoken';
393
-		$fields[] = 'components';
394
-		$fields[] = 'principaluri';
395
-		$fields[] = 'transparent';
396
-		// Making fields a comma-delimited list
397
-		$query = $this->db->getQueryBuilder();
398
-		$query->select($fields)->from('calendars')
399
-			->where($query->expr()->eq('principaluri', $query->createNamedParameter($principalUri)))
400
-			->orderBy('calendarorder', 'ASC');
401
-		$stmt = $query->execute();
402
-		$calendars = [];
403
-		while($row = $stmt->fetch(\PDO::FETCH_ASSOC)) {
404
-			$components = [];
405
-			if ($row['components']) {
406
-				$components = explode(',',$row['components']);
407
-			}
408
-			$calendar = [
409
-				'id' => $row['id'],
410
-				'uri' => $row['uri'],
411
-				'principaluri' => $this->convertPrincipal($row['principaluri'], !$this->legacyEndpoint),
412
-				'{' . Plugin::NS_CALENDARSERVER . '}getctag' => 'http://sabre.io/ns/sync/' . ($row['synctoken']?$row['synctoken']:'0'),
413
-				'{http://sabredav.org/ns}sync-token' => $row['synctoken']?$row['synctoken']:'0',
414
-				'{' . Plugin::NS_CALDAV . '}supported-calendar-component-set' => new SupportedCalendarComponentSet($components),
415
-				'{' . Plugin::NS_CALDAV . '}schedule-calendar-transp' => new ScheduleCalendarTransp($row['transparent']?'transparent':'opaque'),
416
-			];
417
-			foreach($this->propertyMap as $xmlName=>$dbName) {
418
-				$calendar[$xmlName] = $row[$dbName];
419
-			}
420
-
421
-			$this->addOwnerPrincipal($calendar);
422
-
423
-			if (!isset($calendars[$calendar['id']])) {
424
-				$calendars[$calendar['id']] = $calendar;
425
-			}
426
-		}
427
-		$stmt->closeCursor();
428
-		return array_values($calendars);
429
-	}
430
-
431
-
432
-	/**
433
-	 * @param $uid
434
-	 * @return string
435
-	 */
436
-	private function getUserDisplayName($uid) {
437
-		if (!isset($this->userDisplayNames[$uid])) {
438
-			$user = $this->userManager->get($uid);
439
-
440
-			if ($user instanceof IUser) {
441
-				$this->userDisplayNames[$uid] = $user->getDisplayName();
442
-			} else {
443
-				$this->userDisplayNames[$uid] = $uid;
444
-			}
445
-		}
446
-
447
-		return $this->userDisplayNames[$uid];
448
-	}
449
-
450
-	/**
451
-	 * @return array
452
-	 */
453
-	public function getPublicCalendars() {
454
-		$fields = array_values($this->propertyMap);
455
-		$fields[] = 'a.id';
456
-		$fields[] = 'a.uri';
457
-		$fields[] = 'a.synctoken';
458
-		$fields[] = 'a.components';
459
-		$fields[] = 'a.principaluri';
460
-		$fields[] = 'a.transparent';
461
-		$fields[] = 's.access';
462
-		$fields[] = 's.publicuri';
463
-		$calendars = [];
464
-		$query = $this->db->getQueryBuilder();
465
-		$result = $query->select($fields)
466
-			->from('dav_shares', 's')
467
-			->join('s', 'calendars', 'a', $query->expr()->eq('s.resourceid', 'a.id'))
468
-			->where($query->expr()->in('s.access', $query->createNamedParameter(self::ACCESS_PUBLIC)))
469
-			->andWhere($query->expr()->eq('s.type', $query->createNamedParameter('calendar')))
470
-			->execute();
471
-
472
-		while($row = $result->fetch()) {
473
-			list(, $name) = Uri\split($row['principaluri']);
474
-			$row['displayname'] = $row['displayname'] . "($name)";
475
-			$components = [];
476
-			if ($row['components']) {
477
-				$components = explode(',',$row['components']);
478
-			}
479
-			$calendar = [
480
-				'id' => $row['id'],
481
-				'uri' => $row['publicuri'],
482
-				'principaluri' => $this->convertPrincipal($row['principaluri'], !$this->legacyEndpoint),
483
-				'{' . Plugin::NS_CALENDARSERVER . '}getctag' => 'http://sabre.io/ns/sync/' . ($row['synctoken']?$row['synctoken']:'0'),
484
-				'{http://sabredav.org/ns}sync-token' => $row['synctoken']?$row['synctoken']:'0',
485
-				'{' . Plugin::NS_CALDAV . '}supported-calendar-component-set' => new SupportedCalendarComponentSet($components),
486
-				'{' . Plugin::NS_CALDAV . '}schedule-calendar-transp' => new ScheduleCalendarTransp($row['transparent']?'transparent':'opaque'),
487
-				'{' . \OCA\DAV\DAV\Sharing\Plugin::NS_OWNCLOUD . '}owner-principal' => $this->convertPrincipal($row['principaluri'], $this->legacyEndpoint),
488
-				'{' . \OCA\DAV\DAV\Sharing\Plugin::NS_OWNCLOUD . '}read-only' => (int)$row['access'] === Backend::ACCESS_READ,
489
-				'{' . \OCA\DAV\DAV\Sharing\Plugin::NS_OWNCLOUD . '}public' => (int)$row['access'] === self::ACCESS_PUBLIC,
490
-			];
491
-
492
-			foreach($this->propertyMap as $xmlName=>$dbName) {
493
-				$calendar[$xmlName] = $row[$dbName];
494
-			}
495
-
496
-			$this->addOwnerPrincipal($calendar);
497
-
498
-			if (!isset($calendars[$calendar['id']])) {
499
-				$calendars[$calendar['id']] = $calendar;
500
-			}
501
-		}
502
-		$result->closeCursor();
503
-
504
-		return array_values($calendars);
505
-	}
506
-
507
-	/**
508
-	 * @param string $uri
509
-	 * @return array
510
-	 * @throws NotFound
511
-	 */
512
-	public function getPublicCalendar($uri) {
513
-		$fields = array_values($this->propertyMap);
514
-		$fields[] = 'a.id';
515
-		$fields[] = 'a.uri';
516
-		$fields[] = 'a.synctoken';
517
-		$fields[] = 'a.components';
518
-		$fields[] = 'a.principaluri';
519
-		$fields[] = 'a.transparent';
520
-		$fields[] = 's.access';
521
-		$fields[] = 's.publicuri';
522
-		$query = $this->db->getQueryBuilder();
523
-		$result = $query->select($fields)
524
-			->from('dav_shares', 's')
525
-			->join('s', 'calendars', 'a', $query->expr()->eq('s.resourceid', 'a.id'))
526
-			->where($query->expr()->in('s.access', $query->createNamedParameter(self::ACCESS_PUBLIC)))
527
-			->andWhere($query->expr()->eq('s.type', $query->createNamedParameter('calendar')))
528
-			->andWhere($query->expr()->eq('s.publicuri', $query->createNamedParameter($uri)))
529
-			->execute();
530
-
531
-		$row = $result->fetch(\PDO::FETCH_ASSOC);
532
-
533
-		$result->closeCursor();
534
-
535
-		if ($row === false) {
536
-			throw new NotFound('Node with name \'' . $uri . '\' could not be found');
537
-		}
538
-
539
-		list(, $name) = Uri\split($row['principaluri']);
540
-		$row['displayname'] = $row['displayname'] . ' ' . "($name)";
541
-		$components = [];
542
-		if ($row['components']) {
543
-			$components = explode(',',$row['components']);
544
-		}
545
-		$calendar = [
546
-			'id' => $row['id'],
547
-			'uri' => $row['publicuri'],
548
-			'principaluri' => $this->convertPrincipal($row['principaluri'], !$this->legacyEndpoint),
549
-			'{' . Plugin::NS_CALENDARSERVER . '}getctag' => 'http://sabre.io/ns/sync/' . ($row['synctoken']?$row['synctoken']:'0'),
550
-			'{http://sabredav.org/ns}sync-token' => $row['synctoken']?$row['synctoken']:'0',
551
-			'{' . Plugin::NS_CALDAV . '}supported-calendar-component-set' => new SupportedCalendarComponentSet($components),
552
-			'{' . Plugin::NS_CALDAV . '}schedule-calendar-transp' => new ScheduleCalendarTransp($row['transparent']?'transparent':'opaque'),
553
-			'{' . \OCA\DAV\DAV\Sharing\Plugin::NS_OWNCLOUD . '}owner-principal' => $this->convertPrincipal($row['principaluri'], !$this->legacyEndpoint),
554
-			'{' . \OCA\DAV\DAV\Sharing\Plugin::NS_OWNCLOUD . '}read-only' => (int)$row['access'] === Backend::ACCESS_READ,
555
-			'{' . \OCA\DAV\DAV\Sharing\Plugin::NS_OWNCLOUD . '}public' => (int)$row['access'] === self::ACCESS_PUBLIC,
556
-		];
557
-
558
-		foreach($this->propertyMap as $xmlName=>$dbName) {
559
-			$calendar[$xmlName] = $row[$dbName];
560
-		}
561
-
562
-		$this->addOwnerPrincipal($calendar);
563
-
564
-		return $calendar;
565
-
566
-	}
567
-
568
-	/**
569
-	 * @param string $principal
570
-	 * @param string $uri
571
-	 * @return array|null
572
-	 */
573
-	public function getCalendarByUri($principal, $uri) {
574
-		$fields = array_values($this->propertyMap);
575
-		$fields[] = 'id';
576
-		$fields[] = 'uri';
577
-		$fields[] = 'synctoken';
578
-		$fields[] = 'components';
579
-		$fields[] = 'principaluri';
580
-		$fields[] = 'transparent';
581
-
582
-		// Making fields a comma-delimited list
583
-		$query = $this->db->getQueryBuilder();
584
-		$query->select($fields)->from('calendars')
585
-			->where($query->expr()->eq('uri', $query->createNamedParameter($uri)))
586
-			->andWhere($query->expr()->eq('principaluri', $query->createNamedParameter($principal)))
587
-			->setMaxResults(1);
588
-		$stmt = $query->execute();
589
-
590
-		$row = $stmt->fetch(\PDO::FETCH_ASSOC);
591
-		$stmt->closeCursor();
592
-		if ($row === false) {
593
-			return null;
594
-		}
595
-
596
-		$components = [];
597
-		if ($row['components']) {
598
-			$components = explode(',',$row['components']);
599
-		}
600
-
601
-		$calendar = [
602
-			'id' => $row['id'],
603
-			'uri' => $row['uri'],
604
-			'principaluri' => $this->convertPrincipal($row['principaluri'], !$this->legacyEndpoint),
605
-			'{' . Plugin::NS_CALENDARSERVER . '}getctag' => 'http://sabre.io/ns/sync/' . ($row['synctoken']?$row['synctoken']:'0'),
606
-			'{http://sabredav.org/ns}sync-token' => $row['synctoken']?$row['synctoken']:'0',
607
-			'{' . Plugin::NS_CALDAV . '}supported-calendar-component-set' => new SupportedCalendarComponentSet($components),
608
-			'{' . Plugin::NS_CALDAV . '}schedule-calendar-transp' => new ScheduleCalendarTransp($row['transparent']?'transparent':'opaque'),
609
-		];
610
-
611
-		foreach($this->propertyMap as $xmlName=>$dbName) {
612
-			$calendar[$xmlName] = $row[$dbName];
613
-		}
614
-
615
-		$this->addOwnerPrincipal($calendar);
616
-
617
-		return $calendar;
618
-	}
619
-
620
-	/**
621
-	 * @param $calendarId
622
-	 * @return array|null
623
-	 */
624
-	public function getCalendarById($calendarId) {
625
-		$fields = array_values($this->propertyMap);
626
-		$fields[] = 'id';
627
-		$fields[] = 'uri';
628
-		$fields[] = 'synctoken';
629
-		$fields[] = 'components';
630
-		$fields[] = 'principaluri';
631
-		$fields[] = 'transparent';
632
-
633
-		// Making fields a comma-delimited list
634
-		$query = $this->db->getQueryBuilder();
635
-		$query->select($fields)->from('calendars')
636
-			->where($query->expr()->eq('id', $query->createNamedParameter($calendarId)))
637
-			->setMaxResults(1);
638
-		$stmt = $query->execute();
639
-
640
-		$row = $stmt->fetch(\PDO::FETCH_ASSOC);
641
-		$stmt->closeCursor();
642
-		if ($row === false) {
643
-			return null;
644
-		}
645
-
646
-		$components = [];
647
-		if ($row['components']) {
648
-			$components = explode(',',$row['components']);
649
-		}
650
-
651
-		$calendar = [
652
-			'id' => $row['id'],
653
-			'uri' => $row['uri'],
654
-			'principaluri' => $this->convertPrincipal($row['principaluri'], !$this->legacyEndpoint),
655
-			'{' . Plugin::NS_CALENDARSERVER . '}getctag' => 'http://sabre.io/ns/sync/' . ($row['synctoken']?$row['synctoken']:'0'),
656
-			'{http://sabredav.org/ns}sync-token' => $row['synctoken']?$row['synctoken']:'0',
657
-			'{' . Plugin::NS_CALDAV . '}supported-calendar-component-set' => new SupportedCalendarComponentSet($components),
658
-			'{' . Plugin::NS_CALDAV . '}schedule-calendar-transp' => new ScheduleCalendarTransp($row['transparent']?'transparent':'opaque'),
659
-		];
660
-
661
-		foreach($this->propertyMap as $xmlName=>$dbName) {
662
-			$calendar[$xmlName] = $row[$dbName];
663
-		}
664
-
665
-		$this->addOwnerPrincipal($calendar);
666
-
667
-		return $calendar;
668
-	}
669
-
670
-	/**
671
-	 * @param $subscriptionId
672
-	 */
673
-	public function getSubscriptionById($subscriptionId) {
674
-		$fields = array_values($this->subscriptionPropertyMap);
675
-		$fields[] = 'id';
676
-		$fields[] = 'uri';
677
-		$fields[] = 'source';
678
-		$fields[] = 'synctoken';
679
-		$fields[] = 'principaluri';
680
-		$fields[] = 'lastmodified';
681
-
682
-		$query = $this->db->getQueryBuilder();
683
-		$query->select($fields)
684
-			->from('calendarsubscriptions')
685
-			->where($query->expr()->eq('id', $query->createNamedParameter($subscriptionId)))
686
-			->orderBy('calendarorder', 'asc');
687
-		$stmt =$query->execute();
688
-
689
-		$row = $stmt->fetch(\PDO::FETCH_ASSOC);
690
-		$stmt->closeCursor();
691
-		if ($row === false) {
692
-			return null;
693
-		}
694
-
695
-		$subscription = [
696
-			'id'           => $row['id'],
697
-			'uri'          => $row['uri'],
698
-			'principaluri' => $row['principaluri'],
699
-			'source'       => $row['source'],
700
-			'lastmodified' => $row['lastmodified'],
701
-			'{' . Plugin::NS_CALDAV . '}supported-calendar-component-set' => new SupportedCalendarComponentSet(['VTODO', 'VEVENT']),
702
-			'{http://sabredav.org/ns}sync-token' => $row['synctoken']?$row['synctoken']:'0',
703
-		];
704
-
705
-		foreach($this->subscriptionPropertyMap as $xmlName=>$dbName) {
706
-			if (!is_null($row[$dbName])) {
707
-				$subscription[$xmlName] = $row[$dbName];
708
-			}
709
-		}
710
-
711
-		return $subscription;
712
-	}
713
-
714
-	/**
715
-	 * Creates a new calendar for a principal.
716
-	 *
717
-	 * If the creation was a success, an id must be returned that can be used to reference
718
-	 * this calendar in other methods, such as updateCalendar.
719
-	 *
720
-	 * @param string $principalUri
721
-	 * @param string $calendarUri
722
-	 * @param array $properties
723
-	 * @return int
724
-	 * @suppress SqlInjectionChecker
725
-	 */
726
-	function createCalendar($principalUri, $calendarUri, array $properties) {
727
-		$values = [
728
-			'principaluri' => $this->convertPrincipal($principalUri, true),
729
-			'uri'          => $calendarUri,
730
-			'synctoken'    => 1,
731
-			'transparent'  => 0,
732
-			'components'   => 'VEVENT,VTODO',
733
-			'displayname'  => $calendarUri
734
-		];
735
-
736
-		// Default value
737
-		$sccs = '{urn:ietf:params:xml:ns:caldav}supported-calendar-component-set';
738
-		if (isset($properties[$sccs])) {
739
-			if (!($properties[$sccs] instanceof SupportedCalendarComponentSet)) {
740
-				throw new DAV\Exception('The ' . $sccs . ' property must be of type: \Sabre\CalDAV\Property\SupportedCalendarComponentSet');
741
-			}
742
-			$values['components'] = implode(',',$properties[$sccs]->getValue());
743
-		} else if (isset($properties['components'])) {
744
-			// Allow to provide components internally without having
745
-			// to create a SupportedCalendarComponentSet object
746
-			$values['components'] = $properties['components'];
747
-		}
748
-
749
-		$transp = '{' . Plugin::NS_CALDAV . '}schedule-calendar-transp';
750
-		if (isset($properties[$transp])) {
751
-			$values['transparent'] = (int) ($properties[$transp]->getValue() === 'transparent');
752
-		}
753
-
754
-		foreach($this->propertyMap as $xmlName=>$dbName) {
755
-			if (isset($properties[$xmlName])) {
756
-				$values[$dbName] = $properties[$xmlName];
757
-			}
758
-		}
759
-
760
-		$query = $this->db->getQueryBuilder();
761
-		$query->insert('calendars');
762
-		foreach($values as $column => $value) {
763
-			$query->setValue($column, $query->createNamedParameter($value));
764
-		}
765
-		$query->execute();
766
-		$calendarId = $query->getLastInsertId();
767
-
768
-		$this->dispatcher->dispatch('\OCA\DAV\CalDAV\CalDavBackend::createCalendar', new GenericEvent(
769
-			'\OCA\DAV\CalDAV\CalDavBackend::createCalendar',
770
-			[
771
-				'calendarId' => $calendarId,
772
-				'calendarData' => $this->getCalendarById($calendarId),
773
-		]));
774
-
775
-		return $calendarId;
776
-	}
777
-
778
-	/**
779
-	 * Updates properties for a calendar.
780
-	 *
781
-	 * The list of mutations is stored in a Sabre\DAV\PropPatch object.
782
-	 * To do the actual updates, you must tell this object which properties
783
-	 * you're going to process with the handle() method.
784
-	 *
785
-	 * Calling the handle method is like telling the PropPatch object "I
786
-	 * promise I can handle updating this property".
787
-	 *
788
-	 * Read the PropPatch documentation for more info and examples.
789
-	 *
790
-	 * @param mixed $calendarId
791
-	 * @param PropPatch $propPatch
792
-	 * @return void
793
-	 */
794
-	function updateCalendar($calendarId, PropPatch $propPatch) {
795
-		$supportedProperties = array_keys($this->propertyMap);
796
-		$supportedProperties[] = '{' . Plugin::NS_CALDAV . '}schedule-calendar-transp';
797
-
798
-		/**
799
-		 * @suppress SqlInjectionChecker
800
-		 */
801
-		$propPatch->handle($supportedProperties, function($mutations) use ($calendarId) {
802
-			$newValues = [];
803
-			foreach ($mutations as $propertyName => $propertyValue) {
804
-
805
-				switch ($propertyName) {
806
-					case '{' . Plugin::NS_CALDAV . '}schedule-calendar-transp' :
807
-						$fieldName = 'transparent';
808
-						$newValues[$fieldName] = (int) ($propertyValue->getValue() === 'transparent');
809
-						break;
810
-					default :
811
-						$fieldName = $this->propertyMap[$propertyName];
812
-						$newValues[$fieldName] = $propertyValue;
813
-						break;
814
-				}
815
-
816
-			}
817
-			$query = $this->db->getQueryBuilder();
818
-			$query->update('calendars');
819
-			foreach ($newValues as $fieldName => $value) {
820
-				$query->set($fieldName, $query->createNamedParameter($value));
821
-			}
822
-			$query->where($query->expr()->eq('id', $query->createNamedParameter($calendarId)));
823
-			$query->execute();
824
-
825
-			$this->addChange($calendarId, "", 2);
826
-
827
-			$this->dispatcher->dispatch('\OCA\DAV\CalDAV\CalDavBackend::updateCalendar', new GenericEvent(
828
-				'\OCA\DAV\CalDAV\CalDavBackend::updateCalendar',
829
-				[
830
-					'calendarId' => $calendarId,
831
-					'calendarData' => $this->getCalendarById($calendarId),
832
-					'shares' => $this->getShares($calendarId),
833
-					'propertyMutations' => $mutations,
834
-			]));
835
-
836
-			return true;
837
-		});
838
-	}
839
-
840
-	/**
841
-	 * Delete a calendar and all it's objects
842
-	 *
843
-	 * @param mixed $calendarId
844
-	 * @return void
845
-	 */
846
-	function deleteCalendar($calendarId) {
847
-		$this->dispatcher->dispatch('\OCA\DAV\CalDAV\CalDavBackend::deleteCalendar', new GenericEvent(
848
-			'\OCA\DAV\CalDAV\CalDavBackend::deleteCalendar',
849
-			[
850
-				'calendarId' => $calendarId,
851
-				'calendarData' => $this->getCalendarById($calendarId),
852
-				'shares' => $this->getShares($calendarId),
853
-		]));
854
-
855
-		$stmt = $this->db->prepare('DELETE FROM `*PREFIX*calendarobjects` WHERE `calendarid` = ? AND `calendartype` = ?');
856
-		$stmt->execute([$calendarId, self::CALENDAR_TYPE_CALENDAR]);
857
-
858
-		$stmt = $this->db->prepare('DELETE FROM `*PREFIX*calendars` WHERE `id` = ?');
859
-		$stmt->execute([$calendarId]);
860
-
861
-		$stmt = $this->db->prepare('DELETE FROM `*PREFIX*calendarchanges` WHERE `calendarid` = ? AND `calendartype` = ?');
862
-		$stmt->execute([$calendarId, self::CALENDAR_TYPE_CALENDAR]);
863
-
864
-		$this->calendarSharingBackend->deleteAllShares($calendarId);
865
-
866
-		$query = $this->db->getQueryBuilder();
867
-		$query->delete($this->dbObjectPropertiesTable)
868
-			->where($query->expr()->eq('calendarid', $query->createNamedParameter($calendarId)))
869
-			->andWhere($query->expr()->eq('calendartype', $query->createNamedParameter(self::CALENDAR_TYPE_CALENDAR)))
870
-			->execute();
871
-	}
872
-
873
-	/**
874
-	 * Delete all of an user's shares
875
-	 *
876
-	 * @param string $principaluri
877
-	 * @return void
878
-	 */
879
-	function deleteAllSharesByUser($principaluri) {
880
-		$this->calendarSharingBackend->deleteAllSharesByUser($principaluri);
881
-	}
882
-
883
-	/**
884
-	 * Returns all calendar objects within a calendar.
885
-	 *
886
-	 * Every item contains an array with the following keys:
887
-	 *   * calendardata - The iCalendar-compatible calendar data
888
-	 *   * uri - a unique key which will be used to construct the uri. This can
889
-	 *     be any arbitrary string, but making sure it ends with '.ics' is a
890
-	 *     good idea. This is only the basename, or filename, not the full
891
-	 *     path.
892
-	 *   * lastmodified - a timestamp of the last modification time
893
-	 *   * etag - An arbitrary string, surrounded by double-quotes. (e.g.:
894
-	 *   '"abcdef"')
895
-	 *   * size - The size of the calendar objects, in bytes.
896
-	 *   * component - optional, a string containing the type of object, such
897
-	 *     as 'vevent' or 'vtodo'. If specified, this will be used to populate
898
-	 *     the Content-Type header.
899
-	 *
900
-	 * Note that the etag is optional, but it's highly encouraged to return for
901
-	 * speed reasons.
902
-	 *
903
-	 * The calendardata is also optional. If it's not returned
904
-	 * 'getCalendarObject' will be called later, which *is* expected to return
905
-	 * calendardata.
906
-	 *
907
-	 * If neither etag or size are specified, the calendardata will be
908
-	 * used/fetched to determine these numbers. If both are specified the
909
-	 * amount of times this is needed is reduced by a great degree.
910
-	 *
911
-	 * @param mixed $id
912
-	 * @param int $calendarType
913
-	 * @return array
914
-	 */
915
-	public function getCalendarObjects($id, $calendarType=self::CALENDAR_TYPE_CALENDAR):array {
916
-		$query = $this->db->getQueryBuilder();
917
-		$query->select(['id', 'uri', 'lastmodified', 'etag', 'calendarid', 'size', 'componenttype', 'classification'])
918
-			->from('calendarobjects')
919
-			->where($query->expr()->eq('calendarid', $query->createNamedParameter($id)))
920
-			->andWhere($query->expr()->eq('calendartype', $query->createNamedParameter($calendarType)));
921
-		$stmt = $query->execute();
922
-
923
-		$result = [];
924
-		foreach($stmt->fetchAll(\PDO::FETCH_ASSOC) as $row) {
925
-			$result[] = [
926
-				'id'           => $row['id'],
927
-				'uri'          => $row['uri'],
928
-				'lastmodified' => $row['lastmodified'],
929
-				'etag'         => '"' . $row['etag'] . '"',
930
-				'calendarid'   => $row['calendarid'],
931
-				'size'         => (int)$row['size'],
932
-				'component'    => strtolower($row['componenttype']),
933
-				'classification'=> (int)$row['classification']
934
-			];
935
-		}
936
-
937
-		return $result;
938
-	}
939
-
940
-	/**
941
-	 * Returns information from a single calendar object, based on it's object
942
-	 * uri.
943
-	 *
944
-	 * The object uri is only the basename, or filename and not a full path.
945
-	 *
946
-	 * The returned array must have the same keys as getCalendarObjects. The
947
-	 * 'calendardata' object is required here though, while it's not required
948
-	 * for getCalendarObjects.
949
-	 *
950
-	 * This method must return null if the object did not exist.
951
-	 *
952
-	 * @param mixed $id
953
-	 * @param string $objectUri
954
-	 * @param int $calendarType
955
-	 * @return array|null
956
-	 */
957
-	public function getCalendarObject($id, $objectUri, $calendarType=self::CALENDAR_TYPE_CALENDAR) {
958
-		$query = $this->db->getQueryBuilder();
959
-		$query->select(['id', 'uri', 'lastmodified', 'etag', 'calendarid', 'size', 'calendardata', 'componenttype', 'classification'])
960
-			->from('calendarobjects')
961
-			->where($query->expr()->eq('calendarid', $query->createNamedParameter($id)))
962
-			->andWhere($query->expr()->eq('uri', $query->createNamedParameter($objectUri)))
963
-			->andWhere($query->expr()->eq('calendartype', $query->createNamedParameter($calendarType)));
964
-		$stmt = $query->execute();
965
-		$row = $stmt->fetch(\PDO::FETCH_ASSOC);
966
-
967
-		if(!$row) {
968
-			return null;
969
-		}
970
-
971
-		return [
972
-			'id'            => $row['id'],
973
-			'uri'           => $row['uri'],
974
-			'lastmodified'  => $row['lastmodified'],
975
-			'etag'          => '"' . $row['etag'] . '"',
976
-			'calendarid'    => $row['calendarid'],
977
-			'size'          => (int)$row['size'],
978
-			'calendardata'  => $this->readBlob($row['calendardata']),
979
-			'component'     => strtolower($row['componenttype']),
980
-			'classification'=> (int)$row['classification']
981
-		];
982
-	}
983
-
984
-	/**
985
-	 * Returns a list of calendar objects.
986
-	 *
987
-	 * This method should work identical to getCalendarObject, but instead
988
-	 * return all the calendar objects in the list as an array.
989
-	 *
990
-	 * If the backend supports this, it may allow for some speed-ups.
991
-	 *
992
-	 * @param mixed $calendarId
993
-	 * @param string[] $uris
994
-	 * @param int $calendarType
995
-	 * @return array
996
-	 */
997
-	public function getMultipleCalendarObjects($id, array $uris, $calendarType=self::CALENDAR_TYPE_CALENDAR):array {
998
-		if (empty($uris)) {
999
-			return [];
1000
-		}
1001
-
1002
-		$chunks = array_chunk($uris, 100);
1003
-		$objects = [];
1004
-
1005
-		$query = $this->db->getQueryBuilder();
1006
-		$query->select(['id', 'uri', 'lastmodified', 'etag', 'calendarid', 'size', 'calendardata', 'componenttype', 'classification'])
1007
-			->from('calendarobjects')
1008
-			->where($query->expr()->eq('calendarid', $query->createNamedParameter($id)))
1009
-			->andWhere($query->expr()->in('uri', $query->createParameter('uri')))
1010
-			->andWhere($query->expr()->eq('calendartype', $query->createNamedParameter($calendarType)));
1011
-
1012
-		foreach ($chunks as $uris) {
1013
-			$query->setParameter('uri', $uris, IQueryBuilder::PARAM_STR_ARRAY);
1014
-			$result = $query->execute();
1015
-
1016
-			while ($row = $result->fetch()) {
1017
-				$objects[] = [
1018
-					'id'           => $row['id'],
1019
-					'uri'          => $row['uri'],
1020
-					'lastmodified' => $row['lastmodified'],
1021
-					'etag'         => '"' . $row['etag'] . '"',
1022
-					'calendarid'   => $row['calendarid'],
1023
-					'size'         => (int)$row['size'],
1024
-					'calendardata' => $this->readBlob($row['calendardata']),
1025
-					'component'    => strtolower($row['componenttype']),
1026
-					'classification' => (int)$row['classification']
1027
-				];
1028
-			}
1029
-			$result->closeCursor();
1030
-		}
1031
-
1032
-		return $objects;
1033
-	}
1034
-
1035
-	/**
1036
-	 * Creates a new calendar object.
1037
-	 *
1038
-	 * The object uri is only the basename, or filename and not a full path.
1039
-	 *
1040
-	 * It is possible return an etag from this function, which will be used in
1041
-	 * the response to this PUT request. Note that the ETag must be surrounded
1042
-	 * by double-quotes.
1043
-	 *
1044
-	 * However, you should only really return this ETag if you don't mangle the
1045
-	 * calendar-data. If the result of a subsequent GET to this object is not
1046
-	 * the exact same as this request body, you should omit the ETag.
1047
-	 *
1048
-	 * @param mixed $calendarId
1049
-	 * @param string $objectUri
1050
-	 * @param string $calendarData
1051
-	 * @param int $calendarType
1052
-	 * @return string
1053
-	 */
1054
-	function createCalendarObject($calendarId, $objectUri, $calendarData, $calendarType=self::CALENDAR_TYPE_CALENDAR) {
1055
-		$extraData = $this->getDenormalizedData($calendarData);
1056
-
1057
-		$q = $this->db->getQueryBuilder();
1058
-		$q->select($q->func()->count('*'))
1059
-			->from('calendarobjects')
1060
-			->where($q->expr()->eq('calendarid', $q->createNamedParameter($calendarId)))
1061
-			->andWhere($q->expr()->eq('uid', $q->createNamedParameter($extraData['uid'])))
1062
-			->andWhere($q->expr()->eq('calendartype', $q->createNamedParameter($calendarType)));
1063
-
1064
-		$result = $q->execute();
1065
-		$count = (int) $result->fetchColumn();
1066
-		$result->closeCursor();
1067
-
1068
-		if ($count !== 0) {
1069
-			throw new \Sabre\DAV\Exception\BadRequest('Calendar object with uid already exists in this calendar collection.');
1070
-		}
1071
-
1072
-		$query = $this->db->getQueryBuilder();
1073
-		$query->insert('calendarobjects')
1074
-			->values([
1075
-				'calendarid' => $query->createNamedParameter($calendarId),
1076
-				'uri' => $query->createNamedParameter($objectUri),
1077
-				'calendardata' => $query->createNamedParameter($calendarData, IQueryBuilder::PARAM_LOB),
1078
-				'lastmodified' => $query->createNamedParameter(time()),
1079
-				'etag' => $query->createNamedParameter($extraData['etag']),
1080
-				'size' => $query->createNamedParameter($extraData['size']),
1081
-				'componenttype' => $query->createNamedParameter($extraData['componentType']),
1082
-				'firstoccurence' => $query->createNamedParameter($extraData['firstOccurence']),
1083
-				'lastoccurence' => $query->createNamedParameter($extraData['lastOccurence']),
1084
-				'classification' => $query->createNamedParameter($extraData['classification']),
1085
-				'uid' => $query->createNamedParameter($extraData['uid']),
1086
-				'calendartype' => $query->createNamedParameter($calendarType),
1087
-			])
1088
-			->execute();
1089
-
1090
-		$this->updateProperties($calendarId, $objectUri, $calendarData, $calendarType);
1091
-
1092
-		if ($calendarType === self::CALENDAR_TYPE_CALENDAR) {
1093
-			$this->dispatcher->dispatch('\OCA\DAV\CalDAV\CalDavBackend::createCalendarObject', new GenericEvent(
1094
-				'\OCA\DAV\CalDAV\CalDavBackend::createCalendarObject',
1095
-				[
1096
-					'calendarId' => $calendarId,
1097
-					'calendarData' => $this->getCalendarById($calendarId),
1098
-					'shares' => $this->getShares($calendarId),
1099
-					'objectData' => $this->getCalendarObject($calendarId, $objectUri),
1100
-				]
1101
-			));
1102
-		} else {
1103
-			$this->dispatcher->dispatch('\OCA\DAV\CalDAV\CalDavBackend::createCachedCalendarObject', new GenericEvent(
1104
-				'\OCA\DAV\CalDAV\CalDavBackend::createCachedCalendarObject',
1105
-				[
1106
-					'subscriptionId' => $calendarId,
1107
-					'calendarData' => $this->getCalendarById($calendarId),
1108
-					'shares' => $this->getShares($calendarId),
1109
-					'objectData' => $this->getCalendarObject($calendarId, $objectUri),
1110
-				]
1111
-			));
1112
-		}
1113
-		$this->addChange($calendarId, $objectUri, 1, $calendarType);
1114
-
1115
-		return '"' . $extraData['etag'] . '"';
1116
-	}
1117
-
1118
-	/**
1119
-	 * Updates an existing calendarobject, based on it's uri.
1120
-	 *
1121
-	 * The object uri is only the basename, or filename and not a full path.
1122
-	 *
1123
-	 * It is possible return an etag from this function, which will be used in
1124
-	 * the response to this PUT request. Note that the ETag must be surrounded
1125
-	 * by double-quotes.
1126
-	 *
1127
-	 * However, you should only really return this ETag if you don't mangle the
1128
-	 * calendar-data. If the result of a subsequent GET to this object is not
1129
-	 * the exact same as this request body, you should omit the ETag.
1130
-	 *
1131
-	 * @param mixed $calendarId
1132
-	 * @param string $objectUri
1133
-	 * @param string $calendarData
1134
-	 * @param int $calendarType
1135
-	 * @return string
1136
-	 */
1137
-	function updateCalendarObject($calendarId, $objectUri, $calendarData, $calendarType=self::CALENDAR_TYPE_CALENDAR) {
1138
-		$extraData = $this->getDenormalizedData($calendarData);
1139
-		$query = $this->db->getQueryBuilder();
1140
-		$query->update('calendarobjects')
1141
-				->set('calendardata', $query->createNamedParameter($calendarData, IQueryBuilder::PARAM_LOB))
1142
-				->set('lastmodified', $query->createNamedParameter(time()))
1143
-				->set('etag', $query->createNamedParameter($extraData['etag']))
1144
-				->set('size', $query->createNamedParameter($extraData['size']))
1145
-				->set('componenttype', $query->createNamedParameter($extraData['componentType']))
1146
-				->set('firstoccurence', $query->createNamedParameter($extraData['firstOccurence']))
1147
-				->set('lastoccurence', $query->createNamedParameter($extraData['lastOccurence']))
1148
-				->set('classification', $query->createNamedParameter($extraData['classification']))
1149
-				->set('uid', $query->createNamedParameter($extraData['uid']))
1150
-			->where($query->expr()->eq('calendarid', $query->createNamedParameter($calendarId)))
1151
-			->andWhere($query->expr()->eq('uri', $query->createNamedParameter($objectUri)))
1152
-			->andWhere($query->expr()->eq('calendartype', $query->createNamedParameter($calendarType)))
1153
-			->execute();
1154
-
1155
-		$this->updateProperties($calendarId, $objectUri, $calendarData, $calendarType);
1156
-
1157
-		$data = $this->getCalendarObject($calendarId, $objectUri);
1158
-		if (is_array($data)) {
1159
-			if ($calendarType === self::CALENDAR_TYPE_CALENDAR) {
1160
-				$this->dispatcher->dispatch('\OCA\DAV\CalDAV\CalDavBackend::updateCalendarObject', new GenericEvent(
1161
-					'\OCA\DAV\CalDAV\CalDavBackend::updateCalendarObject',
1162
-					[
1163
-						'calendarId' => $calendarId,
1164
-						'calendarData' => $this->getCalendarById($calendarId),
1165
-						'shares' => $this->getShares($calendarId),
1166
-						'objectData' => $data,
1167
-					]
1168
-				));
1169
-			} else {
1170
-				$this->dispatcher->dispatch('\OCA\DAV\CalDAV\CalDavBackend::updateCachedCalendarObject', new GenericEvent(
1171
-					'\OCA\DAV\CalDAV\CalDavBackend::updateCachedCalendarObject',
1172
-					[
1173
-						'subscriptionId' => $calendarId,
1174
-						'calendarData' => $this->getCalendarById($calendarId),
1175
-						'shares' => $this->getShares($calendarId),
1176
-						'objectData' => $data,
1177
-					]
1178
-				));
1179
-			}
1180
-		}
1181
-		$this->addChange($calendarId, $objectUri, 2, $calendarType);
1182
-
1183
-		return '"' . $extraData['etag'] . '"';
1184
-	}
1185
-
1186
-	/**
1187
-	 * @param int $calendarObjectId
1188
-	 * @param int $classification
1189
-	 */
1190
-	public function setClassification($calendarObjectId, $classification) {
1191
-		if (!in_array($classification, [
1192
-			self::CLASSIFICATION_PUBLIC, self::CLASSIFICATION_PRIVATE, self::CLASSIFICATION_CONFIDENTIAL
1193
-		])) {
1194
-			throw new \InvalidArgumentException();
1195
-		}
1196
-		$query = $this->db->getQueryBuilder();
1197
-		$query->update('calendarobjects')
1198
-			->set('classification', $query->createNamedParameter($classification))
1199
-			->where($query->expr()->eq('id', $query->createNamedParameter($calendarObjectId)))
1200
-			->execute();
1201
-	}
1202
-
1203
-	/**
1204
-	 * Deletes an existing calendar object.
1205
-	 *
1206
-	 * The object uri is only the basename, or filename and not a full path.
1207
-	 *
1208
-	 * @param mixed $calendarId
1209
-	 * @param string $objectUri
1210
-	 * @param int $calendarType
1211
-	 * @return void
1212
-	 */
1213
-	function deleteCalendarObject($calendarId, $objectUri, $calendarType=self::CALENDAR_TYPE_CALENDAR) {
1214
-		$data = $this->getCalendarObject($calendarId, $objectUri, $calendarType);
1215
-		if (is_array($data)) {
1216
-			if ($calendarType === self::CALENDAR_TYPE_CALENDAR) {
1217
-				$this->dispatcher->dispatch('\OCA\DAV\CalDAV\CalDavBackend::deleteCalendarObject', new GenericEvent(
1218
-					'\OCA\DAV\CalDAV\CalDavBackend::deleteCalendarObject',
1219
-					[
1220
-						'calendarId' => $calendarId,
1221
-						'calendarData' => $this->getCalendarById($calendarId),
1222
-						'shares' => $this->getShares($calendarId),
1223
-						'objectData' => $data,
1224
-					]
1225
-				));
1226
-			} else {
1227
-				$this->dispatcher->dispatch('\OCA\DAV\CalDAV\CalDavBackend::deleteCachedCalendarObject', new GenericEvent(
1228
-					'\OCA\DAV\CalDAV\CalDavBackend::deleteCachedCalendarObject',
1229
-					[
1230
-						'subscriptionId' => $calendarId,
1231
-						'calendarData' => $this->getCalendarById($calendarId),
1232
-						'shares' => $this->getShares($calendarId),
1233
-						'objectData' => $data,
1234
-					]
1235
-				));
1236
-			}
1237
-		}
1238
-
1239
-		$stmt = $this->db->prepare('DELETE FROM `*PREFIX*calendarobjects` WHERE `calendarid` = ? AND `uri` = ? AND `calendartype` = ?');
1240
-		$stmt->execute([$calendarId, $objectUri, $calendarType]);
1241
-
1242
-		if (is_array($data)) {
1243
-			$this->purgeProperties($calendarId, $data['id'], $calendarType);
1244
-		}
1245
-
1246
-		$this->addChange($calendarId, $objectUri, 3, $calendarType);
1247
-	}
1248
-
1249
-	/**
1250
-	 * Performs a calendar-query on the contents of this calendar.
1251
-	 *
1252
-	 * The calendar-query is defined in RFC4791 : CalDAV. Using the
1253
-	 * calendar-query it is possible for a client to request a specific set of
1254
-	 * object, based on contents of iCalendar properties, date-ranges and
1255
-	 * iCalendar component types (VTODO, VEVENT).
1256
-	 *
1257
-	 * This method should just return a list of (relative) urls that match this
1258
-	 * query.
1259
-	 *
1260
-	 * The list of filters are specified as an array. The exact array is
1261
-	 * documented by Sabre\CalDAV\CalendarQueryParser.
1262
-	 *
1263
-	 * Note that it is extremely likely that getCalendarObject for every path
1264
-	 * returned from this method will be called almost immediately after. You
1265
-	 * may want to anticipate this to speed up these requests.
1266
-	 *
1267
-	 * This method provides a default implementation, which parses *all* the
1268
-	 * iCalendar objects in the specified calendar.
1269
-	 *
1270
-	 * This default may well be good enough for personal use, and calendars
1271
-	 * that aren't very large. But if you anticipate high usage, big calendars
1272
-	 * or high loads, you are strongly advised to optimize certain paths.
1273
-	 *
1274
-	 * The best way to do so is override this method and to optimize
1275
-	 * specifically for 'common filters'.
1276
-	 *
1277
-	 * Requests that are extremely common are:
1278
-	 *   * requests for just VEVENTS
1279
-	 *   * requests for just VTODO
1280
-	 *   * requests with a time-range-filter on either VEVENT or VTODO.
1281
-	 *
1282
-	 * ..and combinations of these requests. It may not be worth it to try to
1283
-	 * handle every possible situation and just rely on the (relatively
1284
-	 * easy to use) CalendarQueryValidator to handle the rest.
1285
-	 *
1286
-	 * Note that especially time-range-filters may be difficult to parse. A
1287
-	 * time-range filter specified on a VEVENT must for instance also handle
1288
-	 * recurrence rules correctly.
1289
-	 * A good example of how to interprete all these filters can also simply
1290
-	 * be found in Sabre\CalDAV\CalendarQueryFilter. This class is as correct
1291
-	 * as possible, so it gives you a good idea on what type of stuff you need
1292
-	 * to think of.
1293
-	 *
1294
-	 * @param mixed $id
1295
-	 * @param array $filters
1296
-	 * @param int $calendarType
1297
-	 * @return array
1298
-	 */
1299
-	public function calendarQuery($id, array $filters, $calendarType=self::CALENDAR_TYPE_CALENDAR):array {
1300
-		$componentType = null;
1301
-		$requirePostFilter = true;
1302
-		$timeRange = null;
1303
-
1304
-		// if no filters were specified, we don't need to filter after a query
1305
-		if (!$filters['prop-filters'] && !$filters['comp-filters']) {
1306
-			$requirePostFilter = false;
1307
-		}
1308
-
1309
-		// Figuring out if there's a component filter
1310
-		if (count($filters['comp-filters']) > 0 && !$filters['comp-filters'][0]['is-not-defined']) {
1311
-			$componentType = $filters['comp-filters'][0]['name'];
1312
-
1313
-			// Checking if we need post-filters
1314
-			if (!$filters['prop-filters'] && !$filters['comp-filters'][0]['comp-filters'] && !$filters['comp-filters'][0]['time-range'] && !$filters['comp-filters'][0]['prop-filters']) {
1315
-				$requirePostFilter = false;
1316
-			}
1317
-			// There was a time-range filter
1318
-			if ($componentType === 'VEVENT' && isset($filters['comp-filters'][0]['time-range'])) {
1319
-				$timeRange = $filters['comp-filters'][0]['time-range'];
1320
-
1321
-				// If start time OR the end time is not specified, we can do a
1322
-				// 100% accurate mysql query.
1323
-				if (!$filters['prop-filters'] && !$filters['comp-filters'][0]['comp-filters'] && !$filters['comp-filters'][0]['prop-filters'] && (!$timeRange['start'] || !$timeRange['end'])) {
1324
-					$requirePostFilter = false;
1325
-				}
1326
-			}
1327
-
1328
-		}
1329
-		$columns = ['uri'];
1330
-		if ($requirePostFilter) {
1331
-			$columns = ['uri', 'calendardata'];
1332
-		}
1333
-		$query = $this->db->getQueryBuilder();
1334
-		$query->select($columns)
1335
-			->from('calendarobjects')
1336
-			->where($query->expr()->eq('calendarid', $query->createNamedParameter($id)))
1337
-			->andWhere($query->expr()->eq('calendartype', $query->createNamedParameter($calendarType)));
1338
-
1339
-		if ($componentType) {
1340
-			$query->andWhere($query->expr()->eq('componenttype', $query->createNamedParameter($componentType)));
1341
-		}
1342
-
1343
-		if ($timeRange && $timeRange['start']) {
1344
-			$query->andWhere($query->expr()->gt('lastoccurence', $query->createNamedParameter($timeRange['start']->getTimeStamp())));
1345
-		}
1346
-		if ($timeRange && $timeRange['end']) {
1347
-			$query->andWhere($query->expr()->lt('firstoccurence', $query->createNamedParameter($timeRange['end']->getTimeStamp())));
1348
-		}
1349
-
1350
-		$stmt = $query->execute();
1351
-
1352
-		$result = [];
1353
-		while($row = $stmt->fetch(\PDO::FETCH_ASSOC)) {
1354
-			if ($requirePostFilter) {
1355
-				// validateFilterForObject will parse the calendar data
1356
-				// catch parsing errors
1357
-				try {
1358
-					$matches = $this->validateFilterForObject($row, $filters);
1359
-				} catch(ParseException $ex) {
1360
-					$this->logger->logException($ex, [
1361
-						'app' => 'dav',
1362
-						'message' => 'Caught parsing exception for calendar data. This usually indicates invalid calendar data. calendar-id:'.$id.' uri:'.$row['uri']
1363
-					]);
1364
-					continue;
1365
-				} catch (InvalidDataException $ex) {
1366
-					$this->logger->logException($ex, [
1367
-						'app' => 'dav',
1368
-						'message' => 'Caught invalid data exception for calendar data. This usually indicates invalid calendar data. calendar-id:'.$id.' uri:'.$row['uri']
1369
-					]);
1370
-					continue;
1371
-				}
1372
-
1373
-				if (!$matches) {
1374
-					continue;
1375
-				}
1376
-			}
1377
-			$result[] = $row['uri'];
1378
-		}
1379
-
1380
-		return $result;
1381
-	}
1382
-
1383
-	/**
1384
-	 * custom Nextcloud search extension for CalDAV
1385
-	 *
1386
-	 * TODO - this should optionally cover cached calendar objects as well
1387
-	 *
1388
-	 * @param string $principalUri
1389
-	 * @param array $filters
1390
-	 * @param integer|null $limit
1391
-	 * @param integer|null $offset
1392
-	 * @return array
1393
-	 */
1394
-	public function calendarSearch($principalUri, array $filters, $limit=null, $offset=null) {
1395
-		$calendars = $this->getCalendarsForUser($principalUri);
1396
-		$ownCalendars = [];
1397
-		$sharedCalendars = [];
1398
-
1399
-		$uriMapper = [];
1400
-
1401
-		foreach($calendars as $calendar) {
1402
-			if ($calendar['{http://owncloud.org/ns}owner-principal'] === $principalUri) {
1403
-				$ownCalendars[] = $calendar['id'];
1404
-			} else {
1405
-				$sharedCalendars[] = $calendar['id'];
1406
-			}
1407
-			$uriMapper[$calendar['id']] = $calendar['uri'];
1408
-		}
1409
-		if (count($ownCalendars) === 0 && count($sharedCalendars) === 0) {
1410
-			return [];
1411
-		}
1412
-
1413
-		$query = $this->db->getQueryBuilder();
1414
-		// Calendar id expressions
1415
-		$calendarExpressions = [];
1416
-		foreach($ownCalendars as $id) {
1417
-			$calendarExpressions[] = $query->expr()->andX(
1418
-				$query->expr()->eq('c.calendarid',
1419
-					$query->createNamedParameter($id)),
1420
-				$query->expr()->eq('c.calendartype',
1421
-						$query->createNamedParameter(self::CALENDAR_TYPE_CALENDAR)));
1422
-		}
1423
-		foreach($sharedCalendars as $id) {
1424
-			$calendarExpressions[] = $query->expr()->andX(
1425
-				$query->expr()->eq('c.calendarid',
1426
-					$query->createNamedParameter($id)),
1427
-				$query->expr()->eq('c.classification',
1428
-					$query->createNamedParameter(self::CLASSIFICATION_PUBLIC)),
1429
-				$query->expr()->eq('c.calendartype',
1430
-					$query->createNamedParameter(self::CALENDAR_TYPE_CALENDAR)));
1431
-		}
1432
-
1433
-		if (count($calendarExpressions) === 1) {
1434
-			$calExpr = $calendarExpressions[0];
1435
-		} else {
1436
-			$calExpr = call_user_func_array([$query->expr(), 'orX'], $calendarExpressions);
1437
-		}
1438
-
1439
-		// Component expressions
1440
-		$compExpressions = [];
1441
-		foreach($filters['comps'] as $comp) {
1442
-			$compExpressions[] = $query->expr()
1443
-				->eq('c.componenttype', $query->createNamedParameter($comp));
1444
-		}
1445
-
1446
-		if (count($compExpressions) === 1) {
1447
-			$compExpr = $compExpressions[0];
1448
-		} else {
1449
-			$compExpr = call_user_func_array([$query->expr(), 'orX'], $compExpressions);
1450
-		}
1451
-
1452
-		if (!isset($filters['props'])) {
1453
-			$filters['props'] = [];
1454
-		}
1455
-		if (!isset($filters['params'])) {
1456
-			$filters['params'] = [];
1457
-		}
1458
-
1459
-		$propParamExpressions = [];
1460
-		foreach($filters['props'] as $prop) {
1461
-			$propParamExpressions[] = $query->expr()->andX(
1462
-				$query->expr()->eq('i.name', $query->createNamedParameter($prop)),
1463
-				$query->expr()->isNull('i.parameter')
1464
-			);
1465
-		}
1466
-		foreach($filters['params'] as $param) {
1467
-			$propParamExpressions[] = $query->expr()->andX(
1468
-				$query->expr()->eq('i.name', $query->createNamedParameter($param['property'])),
1469
-				$query->expr()->eq('i.parameter', $query->createNamedParameter($param['parameter']))
1470
-			);
1471
-		}
1472
-
1473
-		if (count($propParamExpressions) === 1) {
1474
-			$propParamExpr = $propParamExpressions[0];
1475
-		} else {
1476
-			$propParamExpr = call_user_func_array([$query->expr(), 'orX'], $propParamExpressions);
1477
-		}
1478
-
1479
-		$query->select(['c.calendarid', 'c.uri'])
1480
-			->from($this->dbObjectPropertiesTable, 'i')
1481
-			->join('i', 'calendarobjects', 'c', $query->expr()->eq('i.objectid', 'c.id'))
1482
-			->where($calExpr)
1483
-			->andWhere($compExpr)
1484
-			->andWhere($propParamExpr)
1485
-			->andWhere($query->expr()->iLike('i.value',
1486
-				$query->createNamedParameter('%'.$this->db->escapeLikeParameter($filters['search-term']).'%')));
1487
-
1488
-		if ($offset) {
1489
-			$query->setFirstResult($offset);
1490
-		}
1491
-		if ($limit) {
1492
-			$query->setMaxResults($limit);
1493
-		}
1494
-
1495
-		$stmt = $query->execute();
1496
-
1497
-		$result = [];
1498
-		while($row = $stmt->fetch(\PDO::FETCH_ASSOC)) {
1499
-			$path = $uriMapper[$row['calendarid']] . '/' . $row['uri'];
1500
-			if (!in_array($path, $result)) {
1501
-				$result[] = $path;
1502
-			}
1503
-		}
1504
-
1505
-		return $result;
1506
-	}
1507
-
1508
-	/**
1509
-	 * used for Nextcloud's calendar API
1510
-	 *
1511
-	 * @param array $calendarInfo
1512
-	 * @param string $pattern
1513
-	 * @param array $searchProperties
1514
-	 * @param array $options
1515
-	 * @param integer|null $limit
1516
-	 * @param integer|null $offset
1517
-	 *
1518
-	 * @return array
1519
-	 */
1520
-	public function search(array $calendarInfo, $pattern, array $searchProperties,
1521
-						   array $options, $limit, $offset) {
1522
-		$outerQuery = $this->db->getQueryBuilder();
1523
-		$innerQuery = $this->db->getQueryBuilder();
1524
-
1525
-		$innerQuery->selectDistinct('op.objectid')
1526
-			->from($this->dbObjectPropertiesTable, 'op')
1527
-			->andWhere($innerQuery->expr()->eq('op.calendarid',
1528
-				$outerQuery->createNamedParameter($calendarInfo['id'])))
1529
-			->andWhere($innerQuery->expr()->eq('op.calendartype',
1530
-				$outerQuery->createNamedParameter(self::CALENDAR_TYPE_CALENDAR)));
1531
-
1532
-		// only return public items for shared calendars for now
1533
-		if ($calendarInfo['principaluri'] !== $calendarInfo['{http://owncloud.org/ns}owner-principal']) {
1534
-			$innerQuery->andWhere($innerQuery->expr()->eq('c.classification',
1535
-				$outerQuery->createNamedParameter(self::CLASSIFICATION_PUBLIC)));
1536
-		}
1537
-
1538
-		$or = $innerQuery->expr()->orX();
1539
-		foreach($searchProperties as $searchProperty) {
1540
-			$or->add($innerQuery->expr()->eq('op.name',
1541
-				$outerQuery->createNamedParameter($searchProperty)));
1542
-		}
1543
-		$innerQuery->andWhere($or);
1544
-
1545
-		if ($pattern !== '') {
1546
-			$innerQuery->andWhere($innerQuery->expr()->iLike('op.value',
1547
-				$outerQuery->createNamedParameter('%' .
1548
-					$this->db->escapeLikeParameter($pattern) . '%')));
1549
-		}
1550
-
1551
-		$outerQuery->select('c.id', 'c.calendardata', 'c.componenttype', 'c.uid', 'c.uri')
1552
-			->from('calendarobjects', 'c');
1553
-
1554
-		if (isset($options['timerange'])) {
1555
-			if (isset($options['timerange']['start']) && $options['timerange']['start'] instanceof DateTime) {
1556
-				$outerQuery->andWhere($outerQuery->expr()->gt('lastoccurence',
1557
-					$outerQuery->createNamedParameter($options['timerange']['start']->getTimeStamp())));
1558
-
1559
-			}
1560
-			if (isset($options['timerange']['end']) && $options['timerange']['end'] instanceof DateTime) {
1561
-				$outerQuery->andWhere($outerQuery->expr()->lt('firstoccurence',
1562
-					$outerQuery->createNamedParameter($options['timerange']['end']->getTimeStamp())));
1563
-			}
1564
-		}
1565
-
1566
-		if (isset($options['types'])) {
1567
-			$or = $outerQuery->expr()->orX();
1568
-			foreach($options['types'] as $type) {
1569
-				$or->add($outerQuery->expr()->eq('componenttype',
1570
-					$outerQuery->createNamedParameter($type)));
1571
-			}
1572
-			$outerQuery->andWhere($or);
1573
-		}
1574
-
1575
-		$outerQuery->andWhere($outerQuery->expr()->in('c.id',
1576
-			$outerQuery->createFunction($innerQuery->getSQL())));
1577
-
1578
-		if ($offset) {
1579
-			$outerQuery->setFirstResult($offset);
1580
-		}
1581
-		if ($limit) {
1582
-			$outerQuery->setMaxResults($limit);
1583
-		}
1584
-
1585
-		$result = $outerQuery->execute();
1586
-		$calendarObjects = $result->fetchAll();
1587
-
1588
-		return array_map(function($o) {
1589
-			$calendarData = Reader::read($o['calendardata']);
1590
-			$comps = $calendarData->getComponents();
1591
-			$objects = [];
1592
-			$timezones = [];
1593
-			foreach($comps as $comp) {
1594
-				if ($comp instanceof VTimeZone) {
1595
-					$timezones[] = $comp;
1596
-				} else {
1597
-					$objects[] = $comp;
1598
-				}
1599
-			}
1600
-
1601
-			return [
1602
-				'id' => $o['id'],
1603
-				'type' => $o['componenttype'],
1604
-				'uid' => $o['uid'],
1605
-				'uri' => $o['uri'],
1606
-				'objects' => array_map(function($c) {
1607
-					return $this->transformSearchData($c);
1608
-				}, $objects),
1609
-				'timezones' => array_map(function($c) {
1610
-					return $this->transformSearchData($c);
1611
-				}, $timezones),
1612
-			];
1613
-		}, $calendarObjects);
1614
-	}
1615
-
1616
-	/**
1617
-	 * @param Component $comp
1618
-	 * @return array
1619
-	 */
1620
-	private function transformSearchData(Component $comp) {
1621
-		$data = [];
1622
-		/** @var Component[] $subComponents */
1623
-		$subComponents = $comp->getComponents();
1624
-		/** @var Property[] $properties */
1625
-		$properties = array_filter($comp->children(), function($c) {
1626
-			return $c instanceof Property;
1627
-		});
1628
-		$validationRules = $comp->getValidationRules();
1629
-
1630
-		foreach($subComponents as $subComponent) {
1631
-			$name = $subComponent->name;
1632
-			if (!isset($data[$name])) {
1633
-				$data[$name] = [];
1634
-			}
1635
-			$data[$name][] = $this->transformSearchData($subComponent);
1636
-		}
1637
-
1638
-		foreach($properties as $property) {
1639
-			$name = $property->name;
1640
-			if (!isset($validationRules[$name])) {
1641
-				$validationRules[$name] = '*';
1642
-			}
1643
-
1644
-			$rule = $validationRules[$property->name];
1645
-			if ($rule === '+' || $rule === '*') { // multiple
1646
-				if (!isset($data[$name])) {
1647
-					$data[$name] = [];
1648
-				}
1649
-
1650
-				$data[$name][] = $this->transformSearchProperty($property);
1651
-			} else { // once
1652
-				$data[$name] = $this->transformSearchProperty($property);
1653
-			}
1654
-		}
1655
-
1656
-		return $data;
1657
-	}
1658
-
1659
-	/**
1660
-	 * @param Property $prop
1661
-	 * @return array
1662
-	 */
1663
-	private function transformSearchProperty(Property $prop) {
1664
-		// No need to check Date, as it extends DateTime
1665
-		if ($prop instanceof Property\ICalendar\DateTime) {
1666
-			$value = $prop->getDateTime();
1667
-		} else {
1668
-			$value = $prop->getValue();
1669
-		}
1670
-
1671
-		return [
1672
-			$value,
1673
-			$prop->parameters()
1674
-		];
1675
-	}
1676
-
1677
-	/**
1678
-	 * Searches through all of a users calendars and calendar objects to find
1679
-	 * an object with a specific UID.
1680
-	 *
1681
-	 * This method should return the path to this object, relative to the
1682
-	 * calendar home, so this path usually only contains two parts:
1683
-	 *
1684
-	 * calendarpath/objectpath.ics
1685
-	 *
1686
-	 * If the uid is not found, return null.
1687
-	 *
1688
-	 * This method should only consider * objects that the principal owns, so
1689
-	 * any calendars owned by other principals that also appear in this
1690
-	 * collection should be ignored.
1691
-	 *
1692
-	 * @param string $principalUri
1693
-	 * @param string $uid
1694
-	 * @return string|null
1695
-	 */
1696
-	function getCalendarObjectByUID($principalUri, $uid) {
1697
-
1698
-		$query = $this->db->getQueryBuilder();
1699
-		$query->selectAlias('c.uri', 'calendaruri')->selectAlias('co.uri', 'objecturi')
1700
-			->from('calendarobjects', 'co')
1701
-			->leftJoin('co', 'calendars', 'c', $query->expr()->eq('co.calendarid', 'c.id'))
1702
-			->where($query->expr()->eq('c.principaluri', $query->createNamedParameter($principalUri)))
1703
-			->andWhere($query->expr()->eq('co.uid', $query->createNamedParameter($uid)))
1704
-			->andWhere($query->expr()->eq('co.uid', $query->createNamedParameter($uid)));
1705
-
1706
-		$stmt = $query->execute();
1707
-
1708
-		if ($row = $stmt->fetch(\PDO::FETCH_ASSOC)) {
1709
-			return $row['calendaruri'] . '/' . $row['objecturi'];
1710
-		}
1711
-
1712
-		return null;
1713
-	}
1714
-
1715
-	/**
1716
-	 * The getChanges method returns all the changes that have happened, since
1717
-	 * the specified syncToken in the specified calendar.
1718
-	 *
1719
-	 * This function should return an array, such as the following:
1720
-	 *
1721
-	 * [
1722
-	 *   'syncToken' => 'The current synctoken',
1723
-	 *   'added'   => [
1724
-	 *      'new.txt',
1725
-	 *   ],
1726
-	 *   'modified'   => [
1727
-	 *      'modified.txt',
1728
-	 *   ],
1729
-	 *   'deleted' => [
1730
-	 *      'foo.php.bak',
1731
-	 *      'old.txt'
1732
-	 *   ]
1733
-	 * );
1734
-	 *
1735
-	 * The returned syncToken property should reflect the *current* syncToken
1736
-	 * of the calendar, as reported in the {http://sabredav.org/ns}sync-token
1737
-	 * property This is * needed here too, to ensure the operation is atomic.
1738
-	 *
1739
-	 * If the $syncToken argument is specified as null, this is an initial
1740
-	 * sync, and all members should be reported.
1741
-	 *
1742
-	 * The modified property is an array of nodenames that have changed since
1743
-	 * the last token.
1744
-	 *
1745
-	 * The deleted property is an array with nodenames, that have been deleted
1746
-	 * from collection.
1747
-	 *
1748
-	 * The $syncLevel argument is basically the 'depth' of the report. If it's
1749
-	 * 1, you only have to report changes that happened only directly in
1750
-	 * immediate descendants. If it's 2, it should also include changes from
1751
-	 * the nodes below the child collections. (grandchildren)
1752
-	 *
1753
-	 * The $limit argument allows a client to specify how many results should
1754
-	 * be returned at most. If the limit is not specified, it should be treated
1755
-	 * as infinite.
1756
-	 *
1757
-	 * If the limit (infinite or not) is higher than you're willing to return,
1758
-	 * you should throw a Sabre\DAV\Exception\TooMuchMatches() exception.
1759
-	 *
1760
-	 * If the syncToken is expired (due to data cleanup) or unknown, you must
1761
-	 * return null.
1762
-	 *
1763
-	 * The limit is 'suggestive'. You are free to ignore it.
1764
-	 *
1765
-	 * @param string $calendarId
1766
-	 * @param string $syncToken
1767
-	 * @param int $syncLevel
1768
-	 * @param int $limit
1769
-	 * @param int $calendarType
1770
-	 * @return array
1771
-	 */
1772
-	function getChangesForCalendar($calendarId, $syncToken, $syncLevel, $limit = null, $calendarType=self::CALENDAR_TYPE_CALENDAR) {
1773
-		// Current synctoken
1774
-		$stmt = $this->db->prepare('SELECT `synctoken` FROM `*PREFIX*calendars` WHERE `id` = ?');
1775
-		$stmt->execute([ $calendarId ]);
1776
-		$currentToken = $stmt->fetchColumn(0);
1777
-
1778
-		if (is_null($currentToken)) {
1779
-			return null;
1780
-		}
1781
-
1782
-		$result = [
1783
-			'syncToken' => $currentToken,
1784
-			'added'     => [],
1785
-			'modified'  => [],
1786
-			'deleted'   => [],
1787
-		];
1788
-
1789
-		if ($syncToken) {
1790
-
1791
-			$query = "SELECT `uri`, `operation` FROM `*PREFIX*calendarchanges` WHERE `synctoken` >= ? AND `synctoken` < ? AND `calendarid` = ? AND `calendartype` = ? ORDER BY `synctoken`";
1792
-			if ($limit>0) {
1793
-				$query.= " LIMIT " . (int)$limit;
1794
-			}
1795
-
1796
-			// Fetching all changes
1797
-			$stmt = $this->db->prepare($query);
1798
-			$stmt->execute([$syncToken, $currentToken, $calendarId, $calendarType]);
1799
-
1800
-			$changes = [];
1801
-
1802
-			// This loop ensures that any duplicates are overwritten, only the
1803
-			// last change on a node is relevant.
1804
-			while($row = $stmt->fetch(\PDO::FETCH_ASSOC)) {
1805
-
1806
-				$changes[$row['uri']] = $row['operation'];
1807
-
1808
-			}
1809
-
1810
-			foreach($changes as $uri => $operation) {
1811
-
1812
-				switch($operation) {
1813
-					case 1 :
1814
-						$result['added'][] = $uri;
1815
-						break;
1816
-					case 2 :
1817
-						$result['modified'][] = $uri;
1818
-						break;
1819
-					case 3 :
1820
-						$result['deleted'][] = $uri;
1821
-						break;
1822
-				}
1823
-
1824
-			}
1825
-		} else {
1826
-			// No synctoken supplied, this is the initial sync.
1827
-			$query = "SELECT `uri` FROM `*PREFIX*calendarobjects` WHERE `calendarid` = ? AND `calendartype` = ?";
1828
-			$stmt = $this->db->prepare($query);
1829
-			$stmt->execute([$calendarId, $calendarType]);
1830
-
1831
-			$result['added'] = $stmt->fetchAll(\PDO::FETCH_COLUMN);
1832
-		}
1833
-		return $result;
1834
-
1835
-	}
1836
-
1837
-	/**
1838
-	 * Returns a list of subscriptions for a principal.
1839
-	 *
1840
-	 * Every subscription is an array with the following keys:
1841
-	 *  * id, a unique id that will be used by other functions to modify the
1842
-	 *    subscription. This can be the same as the uri or a database key.
1843
-	 *  * uri. This is just the 'base uri' or 'filename' of the subscription.
1844
-	 *  * principaluri. The owner of the subscription. Almost always the same as
1845
-	 *    principalUri passed to this method.
1846
-	 *
1847
-	 * Furthermore, all the subscription info must be returned too:
1848
-	 *
1849
-	 * 1. {DAV:}displayname
1850
-	 * 2. {http://apple.com/ns/ical/}refreshrate
1851
-	 * 3. {http://calendarserver.org/ns/}subscribed-strip-todos (omit if todos
1852
-	 *    should not be stripped).
1853
-	 * 4. {http://calendarserver.org/ns/}subscribed-strip-alarms (omit if alarms
1854
-	 *    should not be stripped).
1855
-	 * 5. {http://calendarserver.org/ns/}subscribed-strip-attachments (omit if
1856
-	 *    attachments should not be stripped).
1857
-	 * 6. {http://calendarserver.org/ns/}source (Must be a
1858
-	 *     Sabre\DAV\Property\Href).
1859
-	 * 7. {http://apple.com/ns/ical/}calendar-color
1860
-	 * 8. {http://apple.com/ns/ical/}calendar-order
1861
-	 * 9. {urn:ietf:params:xml:ns:caldav}supported-calendar-component-set
1862
-	 *    (should just be an instance of
1863
-	 *    Sabre\CalDAV\Property\SupportedCalendarComponentSet, with a bunch of
1864
-	 *    default components).
1865
-	 *
1866
-	 * @param string $principalUri
1867
-	 * @return array
1868
-	 */
1869
-	function getSubscriptionsForUser($principalUri) {
1870
-		$fields = array_values($this->subscriptionPropertyMap);
1871
-		$fields[] = 'id';
1872
-		$fields[] = 'uri';
1873
-		$fields[] = 'source';
1874
-		$fields[] = 'principaluri';
1875
-		$fields[] = 'lastmodified';
1876
-		$fields[] = 'synctoken';
1877
-
1878
-		$query = $this->db->getQueryBuilder();
1879
-		$query->select($fields)
1880
-			->from('calendarsubscriptions')
1881
-			->where($query->expr()->eq('principaluri', $query->createNamedParameter($principalUri)))
1882
-			->orderBy('calendarorder', 'asc');
1883
-		$stmt =$query->execute();
1884
-
1885
-		$subscriptions = [];
1886
-		while($row = $stmt->fetch(\PDO::FETCH_ASSOC)) {
1887
-
1888
-			$subscription = [
1889
-				'id'           => $row['id'],
1890
-				'uri'          => $row['uri'],
1891
-				'principaluri' => $row['principaluri'],
1892
-				'source'       => $row['source'],
1893
-				'lastmodified' => $row['lastmodified'],
1894
-
1895
-				'{' . Plugin::NS_CALDAV . '}supported-calendar-component-set' => new SupportedCalendarComponentSet(['VTODO', 'VEVENT']),
1896
-				'{http://sabredav.org/ns}sync-token' => $row['synctoken']?$row['synctoken']:'0',
1897
-			];
1898
-
1899
-			foreach($this->subscriptionPropertyMap as $xmlName=>$dbName) {
1900
-				if (!is_null($row[$dbName])) {
1901
-					$subscription[$xmlName] = $row[$dbName];
1902
-				}
1903
-			}
1904
-
1905
-			$subscriptions[] = $subscription;
1906
-
1907
-		}
1908
-
1909
-		return $subscriptions;
1910
-	}
1911
-
1912
-	/**
1913
-	 * Creates a new subscription for a principal.
1914
-	 *
1915
-	 * If the creation was a success, an id must be returned that can be used to reference
1916
-	 * this subscription in other methods, such as updateSubscription.
1917
-	 *
1918
-	 * @param string $principalUri
1919
-	 * @param string $uri
1920
-	 * @param array $properties
1921
-	 * @return mixed
1922
-	 */
1923
-	function createSubscription($principalUri, $uri, array $properties) {
1924
-
1925
-		if (!isset($properties['{http://calendarserver.org/ns/}source'])) {
1926
-			throw new Forbidden('The {http://calendarserver.org/ns/}source property is required when creating subscriptions');
1927
-		}
1928
-
1929
-		$values = [
1930
-			'principaluri' => $principalUri,
1931
-			'uri'          => $uri,
1932
-			'source'       => $properties['{http://calendarserver.org/ns/}source']->getHref(),
1933
-			'lastmodified' => time(),
1934
-		];
1935
-
1936
-		$propertiesBoolean = ['striptodos', 'stripalarms', 'stripattachments'];
1937
-
1938
-		foreach($this->subscriptionPropertyMap as $xmlName=>$dbName) {
1939
-			if (array_key_exists($xmlName, $properties)) {
1940
-					$values[$dbName] = $properties[$xmlName];
1941
-					if (in_array($dbName, $propertiesBoolean)) {
1942
-						$values[$dbName] = true;
1943
-				}
1944
-			}
1945
-		}
1946
-
1947
-		$valuesToInsert = [];
1948
-
1949
-		$query = $this->db->getQueryBuilder();
1950
-
1951
-		foreach (array_keys($values) as $name) {
1952
-			$valuesToInsert[$name] = $query->createNamedParameter($values[$name]);
1953
-		}
1954
-
1955
-		$query->insert('calendarsubscriptions')
1956
-			->values($valuesToInsert)
1957
-			->execute();
1958
-
1959
-		$subscriptionId = $this->db->lastInsertId('*PREFIX*calendarsubscriptions');
1960
-
1961
-		$this->dispatcher->dispatch('\OCA\DAV\CalDAV\CalDavBackend::createSubscription', new GenericEvent(
1962
-			'\OCA\DAV\CalDAV\CalDavBackend::createSubscription',
1963
-			[
1964
-				'subscriptionId' => $subscriptionId,
1965
-				'subscriptionData' => $this->getSubscriptionById($subscriptionId),
1966
-			]));
1967
-
1968
-		return $subscriptionId;
1969
-	}
1970
-
1971
-	/**
1972
-	 * Updates a subscription
1973
-	 *
1974
-	 * The list of mutations is stored in a Sabre\DAV\PropPatch object.
1975
-	 * To do the actual updates, you must tell this object which properties
1976
-	 * you're going to process with the handle() method.
1977
-	 *
1978
-	 * Calling the handle method is like telling the PropPatch object "I
1979
-	 * promise I can handle updating this property".
1980
-	 *
1981
-	 * Read the PropPatch documentation for more info and examples.
1982
-	 *
1983
-	 * @param mixed $subscriptionId
1984
-	 * @param PropPatch $propPatch
1985
-	 * @return void
1986
-	 */
1987
-	function updateSubscription($subscriptionId, PropPatch $propPatch) {
1988
-		$supportedProperties = array_keys($this->subscriptionPropertyMap);
1989
-		$supportedProperties[] = '{http://calendarserver.org/ns/}source';
1990
-
1991
-		/**
1992
-		 * @suppress SqlInjectionChecker
1993
-		 */
1994
-		$propPatch->handle($supportedProperties, function($mutations) use ($subscriptionId) {
1995
-
1996
-			$newValues = [];
1997
-
1998
-			foreach($mutations as $propertyName=>$propertyValue) {
1999
-				if ($propertyName === '{http://calendarserver.org/ns/}source') {
2000
-					$newValues['source'] = $propertyValue->getHref();
2001
-				} else {
2002
-					$fieldName = $this->subscriptionPropertyMap[$propertyName];
2003
-					$newValues[$fieldName] = $propertyValue;
2004
-				}
2005
-			}
2006
-
2007
-			$query = $this->db->getQueryBuilder();
2008
-			$query->update('calendarsubscriptions')
2009
-				->set('lastmodified', $query->createNamedParameter(time()));
2010
-			foreach($newValues as $fieldName=>$value) {
2011
-				$query->set($fieldName, $query->createNamedParameter($value));
2012
-			}
2013
-			$query->where($query->expr()->eq('id', $query->createNamedParameter($subscriptionId)))
2014
-				->execute();
2015
-
2016
-			$this->dispatcher->dispatch('\OCA\DAV\CalDAV\CalDavBackend::updateSubscription', new GenericEvent(
2017
-				'\OCA\DAV\CalDAV\CalDavBackend::updateSubscription',
2018
-				[
2019
-					'subscriptionId' => $subscriptionId,
2020
-					'subscriptionData' => $this->getSubscriptionById($subscriptionId),
2021
-					'propertyMutations' => $mutations,
2022
-				]));
2023
-
2024
-			return true;
2025
-
2026
-		});
2027
-	}
2028
-
2029
-	/**
2030
-	 * Deletes a subscription.
2031
-	 *
2032
-	 * @param mixed $subscriptionId
2033
-	 * @return void
2034
-	 */
2035
-	function deleteSubscription($subscriptionId) {
2036
-		$this->dispatcher->dispatch('\OCA\DAV\CalDAV\CalDavBackend::deleteSubscription', new GenericEvent(
2037
-			'\OCA\DAV\CalDAV\CalDavBackend::deleteSubscription',
2038
-			[
2039
-				'subscriptionId' => $subscriptionId,
2040
-				'subscriptionData' => $this->getSubscriptionById($subscriptionId),
2041
-			]));
2042
-
2043
-		$query = $this->db->getQueryBuilder();
2044
-		$query->delete('calendarsubscriptions')
2045
-			->where($query->expr()->eq('id', $query->createNamedParameter($subscriptionId)))
2046
-			->execute();
2047
-
2048
-		$query = $this->db->getQueryBuilder();
2049
-		$query->delete('calendarobjects')
2050
-			->where($query->expr()->eq('calendarid', $query->createNamedParameter($subscriptionId)))
2051
-			->andWhere($query->expr()->eq('calendartype', $query->createNamedParameter(self::CALENDAR_TYPE_SUBSCRIPTION)))
2052
-			->execute();
2053
-
2054
-		$query->delete('calendarchanges')
2055
-			->where($query->expr()->eq('calendarid', $query->createNamedParameter($subscriptionId)))
2056
-			->andWhere($query->expr()->eq('calendartype', $query->createNamedParameter(self::CALENDAR_TYPE_SUBSCRIPTION)))
2057
-			->execute();
2058
-
2059
-		$query->delete($this->dbObjectPropertiesTable)
2060
-			->where($query->expr()->eq('calendarid', $query->createNamedParameter($subscriptionId)))
2061
-			->andWhere($query->expr()->eq('calendartype', $query->createNamedParameter(self::CALENDAR_TYPE_SUBSCRIPTION)))
2062
-			->execute();
2063
-	}
2064
-
2065
-	/**
2066
-	 * Returns a single scheduling object for the inbox collection.
2067
-	 *
2068
-	 * The returned array should contain the following elements:
2069
-	 *   * uri - A unique basename for the object. This will be used to
2070
-	 *           construct a full uri.
2071
-	 *   * calendardata - The iCalendar object
2072
-	 *   * lastmodified - The last modification date. Can be an int for a unix
2073
-	 *                    timestamp, or a PHP DateTime object.
2074
-	 *   * etag - A unique token that must change if the object changed.
2075
-	 *   * size - The size of the object, in bytes.
2076
-	 *
2077
-	 * @param string $principalUri
2078
-	 * @param string $objectUri
2079
-	 * @return array
2080
-	 */
2081
-	function getSchedulingObject($principalUri, $objectUri) {
2082
-		$query = $this->db->getQueryBuilder();
2083
-		$stmt = $query->select(['uri', 'calendardata', 'lastmodified', 'etag', 'size'])
2084
-			->from('schedulingobjects')
2085
-			->where($query->expr()->eq('principaluri', $query->createNamedParameter($principalUri)))
2086
-			->andWhere($query->expr()->eq('uri', $query->createNamedParameter($objectUri)))
2087
-			->execute();
2088
-
2089
-		$row = $stmt->fetch(\PDO::FETCH_ASSOC);
2090
-
2091
-		if(!$row) {
2092
-			return null;
2093
-		}
2094
-
2095
-		return [
2096
-				'uri'          => $row['uri'],
2097
-				'calendardata' => $row['calendardata'],
2098
-				'lastmodified' => $row['lastmodified'],
2099
-				'etag'         => '"' . $row['etag'] . '"',
2100
-				'size'         => (int)$row['size'],
2101
-		];
2102
-	}
2103
-
2104
-	/**
2105
-	 * Returns all scheduling objects for the inbox collection.
2106
-	 *
2107
-	 * These objects should be returned as an array. Every item in the array
2108
-	 * should follow the same structure as returned from getSchedulingObject.
2109
-	 *
2110
-	 * The main difference is that 'calendardata' is optional.
2111
-	 *
2112
-	 * @param string $principalUri
2113
-	 * @return array
2114
-	 */
2115
-	function getSchedulingObjects($principalUri) {
2116
-		$query = $this->db->getQueryBuilder();
2117
-		$stmt = $query->select(['uri', 'calendardata', 'lastmodified', 'etag', 'size'])
2118
-				->from('schedulingobjects')
2119
-				->where($query->expr()->eq('principaluri', $query->createNamedParameter($principalUri)))
2120
-				->execute();
2121
-
2122
-		$result = [];
2123
-		foreach($stmt->fetchAll(\PDO::FETCH_ASSOC) as $row) {
2124
-			$result[] = [
2125
-					'calendardata' => $row['calendardata'],
2126
-					'uri'          => $row['uri'],
2127
-					'lastmodified' => $row['lastmodified'],
2128
-					'etag'         => '"' . $row['etag'] . '"',
2129
-					'size'         => (int)$row['size'],
2130
-			];
2131
-		}
2132
-
2133
-		return $result;
2134
-	}
2135
-
2136
-	/**
2137
-	 * Deletes a scheduling object from the inbox collection.
2138
-	 *
2139
-	 * @param string $principalUri
2140
-	 * @param string $objectUri
2141
-	 * @return void
2142
-	 */
2143
-	function deleteSchedulingObject($principalUri, $objectUri) {
2144
-		$query = $this->db->getQueryBuilder();
2145
-		$query->delete('schedulingobjects')
2146
-				->where($query->expr()->eq('principaluri', $query->createNamedParameter($principalUri)))
2147
-				->andWhere($query->expr()->eq('uri', $query->createNamedParameter($objectUri)))
2148
-				->execute();
2149
-	}
2150
-
2151
-	/**
2152
-	 * Creates a new scheduling object. This should land in a users' inbox.
2153
-	 *
2154
-	 * @param string $principalUri
2155
-	 * @param string $objectUri
2156
-	 * @param string $objectData
2157
-	 * @return void
2158
-	 */
2159
-	function createSchedulingObject($principalUri, $objectUri, $objectData) {
2160
-		$query = $this->db->getQueryBuilder();
2161
-		$query->insert('schedulingobjects')
2162
-			->values([
2163
-				'principaluri' => $query->createNamedParameter($principalUri),
2164
-				'calendardata' => $query->createNamedParameter($objectData, IQueryBuilder::PARAM_LOB),
2165
-				'uri' => $query->createNamedParameter($objectUri),
2166
-				'lastmodified' => $query->createNamedParameter(time()),
2167
-				'etag' => $query->createNamedParameter(md5($objectData)),
2168
-				'size' => $query->createNamedParameter(strlen($objectData))
2169
-			])
2170
-			->execute();
2171
-	}
2172
-
2173
-	/**
2174
-	 * Adds a change record to the calendarchanges table.
2175
-	 *
2176
-	 * @param mixed $calendarId
2177
-	 * @param string $objectUri
2178
-	 * @param int $operation 1 = add, 2 = modify, 3 = delete.
2179
-	 * @param int $calendarType
2180
-	 * @return void
2181
-	 */
2182
-	protected function addChange($calendarId, $objectUri, $operation, $calendarType=self::CALENDAR_TYPE_CALENDAR) {
2183
-		$table = $calendarType === self::CALENDAR_TYPE_CALENDAR ? 'calendars': 'calendarsubscriptions';
2184
-
2185
-		$query = $this->db->getQueryBuilder();
2186
-		$query->select('synctoken')
2187
-			->from($table)
2188
-			->where($query->expr()->eq('id', $query->createNamedParameter($calendarId)));
2189
-		$syncToken = (int)$query->execute()->fetchColumn();
2190
-
2191
-		$query = $this->db->getQueryBuilder();
2192
-		$query->insert('calendarchanges')
2193
-			->values([
2194
-				'uri' => $query->createNamedParameter($objectUri),
2195
-				'synctoken' => $query->createNamedParameter($syncToken),
2196
-				'calendarid' => $query->createNamedParameter($calendarId),
2197
-				'operation' => $query->createNamedParameter($operation),
2198
-				'calendartype' => $query->createNamedParameter($calendarType),
2199
-			])
2200
-			->execute();
2201
-
2202
-		$stmt = $this->db->prepare("UPDATE `*PREFIX*$table` SET `synctoken` = `synctoken` + 1 WHERE `id` = ?");
2203
-		$stmt->execute([
2204
-			$calendarId
2205
-		]);
2206
-
2207
-	}
2208
-
2209
-	/**
2210
-	 * Parses some information from calendar objects, used for optimized
2211
-	 * calendar-queries.
2212
-	 *
2213
-	 * Returns an array with the following keys:
2214
-	 *   * etag - An md5 checksum of the object without the quotes.
2215
-	 *   * size - Size of the object in bytes
2216
-	 *   * componentType - VEVENT, VTODO or VJOURNAL
2217
-	 *   * firstOccurence
2218
-	 *   * lastOccurence
2219
-	 *   * uid - value of the UID property
2220
-	 *
2221
-	 * @param string $calendarData
2222
-	 * @return array
2223
-	 */
2224
-	public function getDenormalizedData($calendarData) {
2225
-
2226
-		$vObject = Reader::read($calendarData);
2227
-		$componentType = null;
2228
-		$component = null;
2229
-		$firstOccurrence = null;
2230
-		$lastOccurrence = null;
2231
-		$uid = null;
2232
-		$classification = self::CLASSIFICATION_PUBLIC;
2233
-		foreach($vObject->getComponents() as $component) {
2234
-			if ($component->name!=='VTIMEZONE') {
2235
-				$componentType = $component->name;
2236
-				$uid = (string)$component->UID;
2237
-				break;
2238
-			}
2239
-		}
2240
-		if (!$componentType) {
2241
-			throw new \Sabre\DAV\Exception\BadRequest('Calendar objects must have a VJOURNAL, VEVENT or VTODO component');
2242
-		}
2243
-		if ($componentType === 'VEVENT' && $component->DTSTART) {
2244
-			$firstOccurrence = $component->DTSTART->getDateTime()->getTimeStamp();
2245
-			// Finding the last occurrence is a bit harder
2246
-			if (!isset($component->RRULE)) {
2247
-				if (isset($component->DTEND)) {
2248
-					$lastOccurrence = $component->DTEND->getDateTime()->getTimeStamp();
2249
-				} elseif (isset($component->DURATION)) {
2250
-					$endDate = clone $component->DTSTART->getDateTime();
2251
-					$endDate->add(DateTimeParser::parse($component->DURATION->getValue()));
2252
-					$lastOccurrence = $endDate->getTimeStamp();
2253
-				} elseif (!$component->DTSTART->hasTime()) {
2254
-					$endDate = clone $component->DTSTART->getDateTime();
2255
-					$endDate->modify('+1 day');
2256
-					$lastOccurrence = $endDate->getTimeStamp();
2257
-				} else {
2258
-					$lastOccurrence = $firstOccurrence;
2259
-				}
2260
-			} else {
2261
-				$it = new EventIterator($vObject, (string)$component->UID);
2262
-				$maxDate = new DateTime(self::MAX_DATE);
2263
-				if ($it->isInfinite()) {
2264
-					$lastOccurrence = $maxDate->getTimestamp();
2265
-				} else {
2266
-					$end = $it->getDtEnd();
2267
-					while($it->valid() && $end < $maxDate) {
2268
-						$end = $it->getDtEnd();
2269
-						$it->next();
2270
-
2271
-					}
2272
-					$lastOccurrence = $end->getTimestamp();
2273
-				}
2274
-
2275
-			}
2276
-		}
2277
-
2278
-		if ($component->CLASS) {
2279
-			$classification = CalDavBackend::CLASSIFICATION_PRIVATE;
2280
-			switch ($component->CLASS->getValue()) {
2281
-				case 'PUBLIC':
2282
-					$classification = CalDavBackend::CLASSIFICATION_PUBLIC;
2283
-					break;
2284
-				case 'CONFIDENTIAL':
2285
-					$classification = CalDavBackend::CLASSIFICATION_CONFIDENTIAL;
2286
-					break;
2287
-			}
2288
-		}
2289
-		return [
2290
-			'etag' => md5($calendarData),
2291
-			'size' => strlen($calendarData),
2292
-			'componentType' => $componentType,
2293
-			'firstOccurence' => is_null($firstOccurrence) ? null : max(0, $firstOccurrence),
2294
-			'lastOccurence'  => $lastOccurrence,
2295
-			'uid' => $uid,
2296
-			'classification' => $classification
2297
-		];
2298
-
2299
-	}
2300
-
2301
-	/**
2302
-	 * @param $cardData
2303
-	 * @return bool|string
2304
-	 */
2305
-	private function readBlob($cardData) {
2306
-		if (is_resource($cardData)) {
2307
-			return stream_get_contents($cardData);
2308
-		}
2309
-
2310
-		return $cardData;
2311
-	}
2312
-
2313
-	/**
2314
-	 * @param IShareable $shareable
2315
-	 * @param array $add
2316
-	 * @param array $remove
2317
-	 */
2318
-	public function updateShares($shareable, $add, $remove) {
2319
-		$calendarId = $shareable->getResourceId();
2320
-		$this->dispatcher->dispatch('\OCA\DAV\CalDAV\CalDavBackend::updateShares', new GenericEvent(
2321
-			'\OCA\DAV\CalDAV\CalDavBackend::updateShares',
2322
-			[
2323
-				'calendarId' => $calendarId,
2324
-				'calendarData' => $this->getCalendarById($calendarId),
2325
-				'shares' => $this->getShares($calendarId),
2326
-				'add' => $add,
2327
-				'remove' => $remove,
2328
-			]));
2329
-		$this->calendarSharingBackend->updateShares($shareable, $add, $remove);
2330
-	}
2331
-
2332
-	/**
2333
-	 * @param int $resourceId
2334
-	 * @param int $calendarType
2335
-	 * @return array
2336
-	 */
2337
-	public function getShares($resourceId, $calendarType=self::CALENDAR_TYPE_CALENDAR) {
2338
-		return $this->calendarSharingBackend->getShares($resourceId);
2339
-	}
2340
-
2341
-	/**
2342
-	 * @param boolean $value
2343
-	 * @param \OCA\DAV\CalDAV\Calendar $calendar
2344
-	 * @return string|null
2345
-	 */
2346
-	public function setPublishStatus($value, $calendar) {
2347
-
2348
-		$calendarId = $calendar->getResourceId();
2349
-		$this->dispatcher->dispatch('\OCA\DAV\CalDAV\CalDavBackend::publishCalendar', new GenericEvent(
2350
-			'\OCA\DAV\CalDAV\CalDavBackend::updateShares',
2351
-			[
2352
-				'calendarId' => $calendarId,
2353
-				'calendarData' => $this->getCalendarById($calendarId),
2354
-				'public' => $value,
2355
-			]));
2356
-
2357
-		$query = $this->db->getQueryBuilder();
2358
-		if ($value) {
2359
-			$publicUri = $this->random->generate(16, ISecureRandom::CHAR_HUMAN_READABLE);
2360
-			$query->insert('dav_shares')
2361
-				->values([
2362
-					'principaluri' => $query->createNamedParameter($calendar->getPrincipalURI()),
2363
-					'type' => $query->createNamedParameter('calendar'),
2364
-					'access' => $query->createNamedParameter(self::ACCESS_PUBLIC),
2365
-					'resourceid' => $query->createNamedParameter($calendar->getResourceId()),
2366
-					'publicuri' => $query->createNamedParameter($publicUri)
2367
-				]);
2368
-			$query->execute();
2369
-			return $publicUri;
2370
-		}
2371
-		$query->delete('dav_shares')
2372
-			->where($query->expr()->eq('resourceid', $query->createNamedParameter($calendar->getResourceId())))
2373
-			->andWhere($query->expr()->eq('access', $query->createNamedParameter(self::ACCESS_PUBLIC)));
2374
-		$query->execute();
2375
-		return null;
2376
-	}
2377
-
2378
-	/**
2379
-	 * @param \OCA\DAV\CalDAV\Calendar $calendar
2380
-	 * @return mixed
2381
-	 */
2382
-	public function getPublishStatus($calendar) {
2383
-		$query = $this->db->getQueryBuilder();
2384
-		$result = $query->select('publicuri')
2385
-			->from('dav_shares')
2386
-			->where($query->expr()->eq('resourceid', $query->createNamedParameter($calendar->getResourceId())))
2387
-			->andWhere($query->expr()->eq('access', $query->createNamedParameter(self::ACCESS_PUBLIC)))
2388
-			->execute();
2389
-
2390
-		$row = $result->fetch();
2391
-		$result->closeCursor();
2392
-		return $row ? reset($row) : false;
2393
-	}
2394
-
2395
-	/**
2396
-	 * @param int $resourceId
2397
-	 * @param array $acl
2398
-	 * @return array
2399
-	 */
2400
-	public function applyShareAcl($resourceId, $acl) {
2401
-		return $this->calendarSharingBackend->applyShareAcl($resourceId, $acl);
2402
-	}
2403
-
2404
-
2405
-
2406
-	/**
2407
-	 * update properties table
2408
-	 *
2409
-	 * @param int $calendarId
2410
-	 * @param string $objectUri
2411
-	 * @param string $calendarData
2412
-	 * @param int $calendarType
2413
-	 */
2414
-	public function updateProperties($calendarId, $objectUri, $calendarData, $calendarType=self::CALENDAR_TYPE_CALENDAR) {
2415
-		$objectId = $this->getCalendarObjectId($calendarId, $objectUri, $calendarType);
2416
-
2417
-		try {
2418
-			$vCalendar = $this->readCalendarData($calendarData);
2419
-		} catch (\Exception $ex) {
2420
-			return;
2421
-		}
2422
-
2423
-		$this->purgeProperties($calendarId, $objectId);
2424
-
2425
-		$query = $this->db->getQueryBuilder();
2426
-		$query->insert($this->dbObjectPropertiesTable)
2427
-			->values(
2428
-				[
2429
-					'calendarid' => $query->createNamedParameter($calendarId),
2430
-					'calendartype' => $query->createNamedParameter($calendarType),
2431
-					'objectid' => $query->createNamedParameter($objectId),
2432
-					'name' => $query->createParameter('name'),
2433
-					'parameter' => $query->createParameter('parameter'),
2434
-					'value' => $query->createParameter('value'),
2435
-				]
2436
-			);
2437
-
2438
-		$indexComponents = ['VEVENT', 'VJOURNAL', 'VTODO'];
2439
-		foreach ($vCalendar->getComponents() as $component) {
2440
-			if (!in_array($component->name, $indexComponents)) {
2441
-				continue;
2442
-			}
2443
-
2444
-			foreach ($component->children() as $property) {
2445
-				if (in_array($property->name, self::$indexProperties)) {
2446
-					$value = $property->getValue();
2447
-					// is this a shitty db?
2448
-					if (!$this->db->supports4ByteText()) {
2449
-						$value = preg_replace('/[\x{10000}-\x{10FFFF}]/u', "\xEF\xBF\xBD", $value);
2450
-					}
2451
-					$value = mb_substr($value, 0, 254);
2452
-
2453
-					$query->setParameter('name', $property->name);
2454
-					$query->setParameter('parameter', null);
2455
-					$query->setParameter('value', $value);
2456
-					$query->execute();
2457
-				}
2458
-
2459
-				if (array_key_exists($property->name, self::$indexParameters)) {
2460
-					$parameters = $property->parameters();
2461
-					$indexedParametersForProperty = self::$indexParameters[$property->name];
2462
-
2463
-					foreach ($parameters as $key => $value) {
2464
-						if (in_array($key, $indexedParametersForProperty)) {
2465
-							// is this a shitty db?
2466
-							if ($this->db->supports4ByteText()) {
2467
-								$value = preg_replace('/[\x{10000}-\x{10FFFF}]/u', "\xEF\xBF\xBD", $value);
2468
-							}
2469
-
2470
-							$query->setParameter('name', $property->name);
2471
-							$query->setParameter('parameter', mb_substr($key, 0, 254));
2472
-							$query->setParameter('value', mb_substr($value, 0, 254));
2473
-							$query->execute();
2474
-						}
2475
-					}
2476
-				}
2477
-			}
2478
-		}
2479
-	}
2480
-
2481
-	/**
2482
-	 * deletes all birthday calendars
2483
-	 */
2484
-	public function deleteAllBirthdayCalendars() {
2485
-		$query = $this->db->getQueryBuilder();
2486
-		$result = $query->select(['id'])->from('calendars')
2487
-			->where($query->expr()->eq('uri', $query->createNamedParameter(BirthdayService::BIRTHDAY_CALENDAR_URI)))
2488
-			->execute();
2489
-
2490
-		$ids = $result->fetchAll();
2491
-		foreach($ids as $id) {
2492
-			$this->deleteCalendar($id['id']);
2493
-		}
2494
-	}
2495
-
2496
-	/**
2497
-	 * @param $subscriptionId
2498
-	 */
2499
-	public function purgeAllCachedEventsForSubscription($subscriptionId) {
2500
-		$query = $this->db->getQueryBuilder();
2501
-		$query->select('uri')
2502
-			->from('calendarobjects')
2503
-			->where($query->expr()->eq('calendarid', $query->createNamedParameter($subscriptionId)))
2504
-			->andWhere($query->expr()->eq('calendartype', $query->createNamedParameter(self::CALENDAR_TYPE_SUBSCRIPTION)));
2505
-		$stmt = $query->execute();
2506
-
2507
-		$uris = [];
2508
-		foreach($stmt->fetchAll(\PDO::FETCH_ASSOC) as $row) {
2509
-			$uris[] = $row['uri'];
2510
-		}
2511
-		$stmt->closeCursor();
2512
-
2513
-		$query = $this->db->getQueryBuilder();
2514
-		$query->delete('calendarobjects')
2515
-			->where($query->expr()->eq('calendarid', $query->createNamedParameter($subscriptionId)))
2516
-			->andWhere($query->expr()->eq('calendartype', $query->createNamedParameter(self::CALENDAR_TYPE_SUBSCRIPTION)))
2517
-			->execute();
2518
-
2519
-		$query->delete('calendarchanges')
2520
-			->where($query->expr()->eq('calendarid', $query->createNamedParameter($subscriptionId)))
2521
-			->andWhere($query->expr()->eq('calendartype', $query->createNamedParameter(self::CALENDAR_TYPE_SUBSCRIPTION)))
2522
-			->execute();
2523
-
2524
-		$query->delete($this->dbObjectPropertiesTable)
2525
-			->where($query->expr()->eq('calendarid', $query->createNamedParameter($subscriptionId)))
2526
-			->andWhere($query->expr()->eq('calendartype', $query->createNamedParameter(self::CALENDAR_TYPE_SUBSCRIPTION)))
2527
-			->execute();
2528
-
2529
-		foreach($uris as $uri) {
2530
-			$this->addChange($subscriptionId, $uri, 3, self::CALENDAR_TYPE_SUBSCRIPTION);
2531
-		}
2532
-	}
2533
-
2534
-	/**
2535
-	 * Move a calendar from one user to another
2536
-	 *
2537
-	 * @param string $uriName
2538
-	 * @param string $uriOrigin
2539
-	 * @param string $uriDestination
2540
-	 */
2541
-	public function moveCalendar($uriName, $uriOrigin, $uriDestination)
2542
-	{
2543
-		$query = $this->db->getQueryBuilder();
2544
-		$query->update('calendars')
2545
-			->set('principaluri', $query->createNamedParameter($uriDestination))
2546
-			->where($query->expr()->eq('principaluri', $query->createNamedParameter($uriOrigin)))
2547
-			->andWhere($query->expr()->eq('uri', $query->createNamedParameter($uriName)))
2548
-			->execute();
2549
-	}
2550
-
2551
-	/**
2552
-	 * read VCalendar data into a VCalendar object
2553
-	 *
2554
-	 * @param string $objectData
2555
-	 * @return VCalendar
2556
-	 */
2557
-	protected function readCalendarData($objectData) {
2558
-		return Reader::read($objectData);
2559
-	}
2560
-
2561
-	/**
2562
-	 * delete all properties from a given calendar object
2563
-	 *
2564
-	 * @param int $calendarId
2565
-	 * @param int $objectId
2566
-	 */
2567
-	protected function purgeProperties($calendarId, $objectId) {
2568
-		$query = $this->db->getQueryBuilder();
2569
-		$query->delete($this->dbObjectPropertiesTable)
2570
-			->where($query->expr()->eq('objectid', $query->createNamedParameter($objectId)))
2571
-			->andWhere($query->expr()->eq('calendarid', $query->createNamedParameter($calendarId)));
2572
-		$query->execute();
2573
-	}
2574
-
2575
-	/**
2576
-	 * get ID from a given calendar object
2577
-	 *
2578
-	 * @param int $calendarId
2579
-	 * @param string $uri
2580
-	 * @param int $calendarType
2581
-	 * @return int
2582
-	 */
2583
-	protected function getCalendarObjectId($calendarId, $uri, $calendarType):int {
2584
-		$query = $this->db->getQueryBuilder();
2585
-		$query->select('id')
2586
-			->from('calendarobjects')
2587
-			->where($query->expr()->eq('uri', $query->createNamedParameter($uri)))
2588
-			->andWhere($query->expr()->eq('calendarid', $query->createNamedParameter($calendarId)))
2589
-			->andWhere($query->expr()->eq('calendartype', $query->createNamedParameter($calendarType)));
2590
-
2591
-		$result = $query->execute();
2592
-		$objectIds = $result->fetch();
2593
-		$result->closeCursor();
2594
-
2595
-		if (!isset($objectIds['id'])) {
2596
-			throw new \InvalidArgumentException('Calendarobject does not exists: ' . $uri);
2597
-		}
2598
-
2599
-		return (int)$objectIds['id'];
2600
-	}
2601
-
2602
-	/**
2603
-	 * return legacy endpoint principal name to new principal name
2604
-	 *
2605
-	 * @param $principalUri
2606
-	 * @param $toV2
2607
-	 * @return string
2608
-	 */
2609
-	private function convertPrincipal($principalUri, $toV2) {
2610
-		if ($this->principalBackend->getPrincipalPrefix() === 'principals') {
2611
-			list(, $name) = Uri\split($principalUri);
2612
-			if ($toV2 === true) {
2613
-				return "principals/users/$name";
2614
-			}
2615
-			return "principals/$name";
2616
-		}
2617
-		return $principalUri;
2618
-	}
2619
-
2620
-	/**
2621
-	 * adds information about an owner to the calendar data
2622
-	 *
2623
-	 * @param $calendarInfo
2624
-	 */
2625
-	private function addOwnerPrincipal(&$calendarInfo) {
2626
-		$ownerPrincipalKey = '{' . \OCA\DAV\DAV\Sharing\Plugin::NS_OWNCLOUD . '}owner-principal';
2627
-		$displaynameKey = '{' . \OCA\DAV\DAV\Sharing\Plugin::NS_NEXTCLOUD . '}owner-displayname';
2628
-		if (isset($calendarInfo[$ownerPrincipalKey])) {
2629
-			$uri = $calendarInfo[$ownerPrincipalKey];
2630
-		} else {
2631
-			$uri = $calendarInfo['principaluri'];
2632
-		}
2633
-
2634
-		$principalInformation = $this->principalBackend->getPrincipalByPath($uri);
2635
-		if (isset($principalInformation['{DAV:}displayname'])) {
2636
-			$calendarInfo[$displaynameKey] = $principalInformation['{DAV:}displayname'];
2637
-		}
2638
-	}
79
+    const CALENDAR_TYPE_CALENDAR = 0;
80
+    const CALENDAR_TYPE_SUBSCRIPTION = 1;
81
+
82
+    const PERSONAL_CALENDAR_URI = 'personal';
83
+    const PERSONAL_CALENDAR_NAME = 'Personal';
84
+
85
+    const RESOURCE_BOOKING_CALENDAR_URI = 'calendar';
86
+    const RESOURCE_BOOKING_CALENDAR_NAME = 'Calendar';
87
+
88
+    /**
89
+     * We need to specify a max date, because we need to stop *somewhere*
90
+     *
91
+     * On 32 bit system the maximum for a signed integer is 2147483647, so
92
+     * MAX_DATE cannot be higher than date('Y-m-d', 2147483647) which results
93
+     * in 2038-01-19 to avoid problems when the date is converted
94
+     * to a unix timestamp.
95
+     */
96
+    const MAX_DATE = '2038-01-01';
97
+
98
+    const ACCESS_PUBLIC = 4;
99
+    const CLASSIFICATION_PUBLIC = 0;
100
+    const CLASSIFICATION_PRIVATE = 1;
101
+    const CLASSIFICATION_CONFIDENTIAL = 2;
102
+
103
+    /**
104
+     * List of CalDAV properties, and how they map to database field names
105
+     * Add your own properties by simply adding on to this array.
106
+     *
107
+     * Note that only string-based properties are supported here.
108
+     *
109
+     * @var array
110
+     */
111
+    public $propertyMap = [
112
+        '{DAV:}displayname'                          => 'displayname',
113
+        '{urn:ietf:params:xml:ns:caldav}calendar-description' => 'description',
114
+        '{urn:ietf:params:xml:ns:caldav}calendar-timezone'    => 'timezone',
115
+        '{http://apple.com/ns/ical/}calendar-order'  => 'calendarorder',
116
+        '{http://apple.com/ns/ical/}calendar-color'  => 'calendarcolor',
117
+    ];
118
+
119
+    /**
120
+     * List of subscription properties, and how they map to database field names.
121
+     *
122
+     * @var array
123
+     */
124
+    public $subscriptionPropertyMap = [
125
+        '{DAV:}displayname'                                           => 'displayname',
126
+        '{http://apple.com/ns/ical/}refreshrate'                      => 'refreshrate',
127
+        '{http://apple.com/ns/ical/}calendar-order'                   => 'calendarorder',
128
+        '{http://apple.com/ns/ical/}calendar-color'                   => 'calendarcolor',
129
+        '{http://calendarserver.org/ns/}subscribed-strip-todos'       => 'striptodos',
130
+        '{http://calendarserver.org/ns/}subscribed-strip-alarms'      => 'stripalarms',
131
+        '{http://calendarserver.org/ns/}subscribed-strip-attachments' => 'stripattachments',
132
+    ];
133
+
134
+    /** @var array properties to index */
135
+    public static $indexProperties = ['CATEGORIES', 'COMMENT', 'DESCRIPTION',
136
+        'LOCATION', 'RESOURCES', 'STATUS', 'SUMMARY', 'ATTENDEE', 'CONTACT',
137
+        'ORGANIZER'];
138
+
139
+    /** @var array parameters to index */
140
+    public static $indexParameters = [
141
+        'ATTENDEE' => ['CN'],
142
+        'ORGANIZER' => ['CN'],
143
+    ];
144
+
145
+    /**
146
+     * @var string[] Map of uid => display name
147
+     */
148
+    protected $userDisplayNames;
149
+
150
+    /** @var IDBConnection */
151
+    private $db;
152
+
153
+    /** @var Backend */
154
+    private $calendarSharingBackend;
155
+
156
+    /** @var Principal */
157
+    private $principalBackend;
158
+
159
+    /** @var IUserManager */
160
+    private $userManager;
161
+
162
+    /** @var ISecureRandom */
163
+    private $random;
164
+
165
+    /** @var ILogger */
166
+    private $logger;
167
+
168
+    /** @var EventDispatcherInterface */
169
+    private $dispatcher;
170
+
171
+    /** @var bool */
172
+    private $legacyEndpoint;
173
+
174
+    /** @var string */
175
+    private $dbObjectPropertiesTable = 'calendarobjects_props';
176
+
177
+    /**
178
+     * CalDavBackend constructor.
179
+     *
180
+     * @param IDBConnection $db
181
+     * @param Principal $principalBackend
182
+     * @param IUserManager $userManager
183
+     * @param IGroupManager $groupManager
184
+     * @param ISecureRandom $random
185
+     * @param ILogger $logger
186
+     * @param EventDispatcherInterface $dispatcher
187
+     * @param bool $legacyEndpoint
188
+     */
189
+    public function __construct(IDBConnection $db,
190
+                                Principal $principalBackend,
191
+                                IUserManager $userManager,
192
+                                IGroupManager $groupManager,
193
+                                ISecureRandom $random,
194
+                                ILogger $logger,
195
+                                EventDispatcherInterface $dispatcher,
196
+                                bool $legacyEndpoint = false) {
197
+        $this->db = $db;
198
+        $this->principalBackend = $principalBackend;
199
+        $this->userManager = $userManager;
200
+        $this->calendarSharingBackend = new Backend($this->db, $this->userManager, $groupManager, $principalBackend, 'calendar');
201
+        $this->random = $random;
202
+        $this->logger = $logger;
203
+        $this->dispatcher = $dispatcher;
204
+        $this->legacyEndpoint = $legacyEndpoint;
205
+    }
206
+
207
+    /**
208
+     * Return the number of calendars for a principal
209
+     *
210
+     * By default this excludes the automatically generated birthday calendar
211
+     *
212
+     * @param $principalUri
213
+     * @param bool $excludeBirthday
214
+     * @return int
215
+     */
216
+    public function getCalendarsForUserCount($principalUri, $excludeBirthday = true) {
217
+        $principalUri = $this->convertPrincipal($principalUri, true);
218
+        $query = $this->db->getQueryBuilder();
219
+        $query->select($query->func()->count('*'))
220
+            ->from('calendars')
221
+            ->where($query->expr()->eq('principaluri', $query->createNamedParameter($principalUri)));
222
+
223
+        if ($excludeBirthday) {
224
+            $query->andWhere($query->expr()->neq('uri', $query->createNamedParameter(BirthdayService::BIRTHDAY_CALENDAR_URI)));
225
+        }
226
+
227
+        return (int)$query->execute()->fetchColumn();
228
+    }
229
+
230
+    /**
231
+     * Returns a list of calendars for a principal.
232
+     *
233
+     * Every project is an array with the following keys:
234
+     *  * id, a unique id that will be used by other functions to modify the
235
+     *    calendar. This can be the same as the uri or a database key.
236
+     *  * uri, which the basename of the uri with which the calendar is
237
+     *    accessed.
238
+     *  * principaluri. The owner of the calendar. Almost always the same as
239
+     *    principalUri passed to this method.
240
+     *
241
+     * Furthermore it can contain webdav properties in clark notation. A very
242
+     * common one is '{DAV:}displayname'.
243
+     *
244
+     * Many clients also require:
245
+     * {urn:ietf:params:xml:ns:caldav}supported-calendar-component-set
246
+     * For this property, you can just return an instance of
247
+     * Sabre\CalDAV\Property\SupportedCalendarComponentSet.
248
+     *
249
+     * If you return {http://sabredav.org/ns}read-only and set the value to 1,
250
+     * ACL will automatically be put in read-only mode.
251
+     *
252
+     * @param string $principalUri
253
+     * @return array
254
+     */
255
+    function getCalendarsForUser($principalUri) {
256
+        $principalUriOriginal = $principalUri;
257
+        $principalUri = $this->convertPrincipal($principalUri, true);
258
+        $fields = array_values($this->propertyMap);
259
+        $fields[] = 'id';
260
+        $fields[] = 'uri';
261
+        $fields[] = 'synctoken';
262
+        $fields[] = 'components';
263
+        $fields[] = 'principaluri';
264
+        $fields[] = 'transparent';
265
+
266
+        // Making fields a comma-delimited list
267
+        $query = $this->db->getQueryBuilder();
268
+        $query->select($fields)->from('calendars')
269
+                ->where($query->expr()->eq('principaluri', $query->createNamedParameter($principalUri)))
270
+                ->orderBy('calendarorder', 'ASC');
271
+        $stmt = $query->execute();
272
+
273
+        $calendars = [];
274
+        while($row = $stmt->fetch(\PDO::FETCH_ASSOC)) {
275
+
276
+            $components = [];
277
+            if ($row['components']) {
278
+                $components = explode(',',$row['components']);
279
+            }
280
+
281
+            $calendar = [
282
+                'id' => $row['id'],
283
+                'uri' => $row['uri'],
284
+                'principaluri' => $this->convertPrincipal($row['principaluri'], !$this->legacyEndpoint),
285
+                '{' . Plugin::NS_CALENDARSERVER . '}getctag' => 'http://sabre.io/ns/sync/' . ($row['synctoken']?$row['synctoken']:'0'),
286
+                '{http://sabredav.org/ns}sync-token' => $row['synctoken']?$row['synctoken']:'0',
287
+                '{' . Plugin::NS_CALDAV . '}supported-calendar-component-set' => new SupportedCalendarComponentSet($components),
288
+                '{' . Plugin::NS_CALDAV . '}schedule-calendar-transp' => new ScheduleCalendarTransp($row['transparent']?'transparent':'opaque'),
289
+                '{' . \OCA\DAV\DAV\Sharing\Plugin::NS_OWNCLOUD . '}owner-principal' => $this->convertPrincipal($principalUri, !$this->legacyEndpoint),
290
+            ];
291
+
292
+            foreach($this->propertyMap as $xmlName=>$dbName) {
293
+                $calendar[$xmlName] = $row[$dbName];
294
+            }
295
+
296
+            $this->addOwnerPrincipal($calendar);
297
+
298
+            if (!isset($calendars[$calendar['id']])) {
299
+                $calendars[$calendar['id']] = $calendar;
300
+            }
301
+        }
302
+
303
+        $stmt->closeCursor();
304
+
305
+        // query for shared calendars
306
+        $principals = $this->principalBackend->getGroupMembership($principalUriOriginal, true);
307
+        $principals = array_merge($principals, $this->principalBackend->getCircleMembership($principalUriOriginal));
308
+
309
+        $principals = array_map(function($principal) {
310
+            return urldecode($principal);
311
+        }, $principals);
312
+        $principals[]= $principalUri;
313
+
314
+        $fields = array_values($this->propertyMap);
315
+        $fields[] = 'a.id';
316
+        $fields[] = 'a.uri';
317
+        $fields[] = 'a.synctoken';
318
+        $fields[] = 'a.components';
319
+        $fields[] = 'a.principaluri';
320
+        $fields[] = 'a.transparent';
321
+        $fields[] = 's.access';
322
+        $query = $this->db->getQueryBuilder();
323
+        $result = $query->select($fields)
324
+            ->from('dav_shares', 's')
325
+            ->join('s', 'calendars', 'a', $query->expr()->eq('s.resourceid', 'a.id'))
326
+            ->where($query->expr()->in('s.principaluri', $query->createParameter('principaluri')))
327
+            ->andWhere($query->expr()->eq('s.type', $query->createParameter('type')))
328
+            ->setParameter('type', 'calendar')
329
+            ->setParameter('principaluri', $principals, \Doctrine\DBAL\Connection::PARAM_STR_ARRAY)
330
+            ->execute();
331
+
332
+        $readOnlyPropertyName = '{' . \OCA\DAV\DAV\Sharing\Plugin::NS_OWNCLOUD . '}read-only';
333
+        while($row = $result->fetch()) {
334
+            if ($row['principaluri'] === $principalUri) {
335
+                continue;
336
+            }
337
+
338
+            $readOnly = (int) $row['access'] === Backend::ACCESS_READ;
339
+            if (isset($calendars[$row['id']])) {
340
+                if ($readOnly) {
341
+                    // New share can not have more permissions then the old one.
342
+                    continue;
343
+                }
344
+                if (isset($calendars[$row['id']][$readOnlyPropertyName]) &&
345
+                    $calendars[$row['id']][$readOnlyPropertyName] === 0) {
346
+                    // Old share is already read-write, no more permissions can be gained
347
+                    continue;
348
+                }
349
+            }
350
+
351
+            list(, $name) = Uri\split($row['principaluri']);
352
+            $uri = $row['uri'] . '_shared_by_' . $name;
353
+            $row['displayname'] = $row['displayname'] . ' (' . $this->getUserDisplayName($name) . ')';
354
+            $components = [];
355
+            if ($row['components']) {
356
+                $components = explode(',',$row['components']);
357
+            }
358
+            $calendar = [
359
+                'id' => $row['id'],
360
+                'uri' => $uri,
361
+                'principaluri' => $this->convertPrincipal($principalUri, !$this->legacyEndpoint),
362
+                '{' . Plugin::NS_CALENDARSERVER . '}getctag' => 'http://sabre.io/ns/sync/' . ($row['synctoken']?$row['synctoken']:'0'),
363
+                '{http://sabredav.org/ns}sync-token' => $row['synctoken']?$row['synctoken']:'0',
364
+                '{' . Plugin::NS_CALDAV . '}supported-calendar-component-set' => new SupportedCalendarComponentSet($components),
365
+                '{' . Plugin::NS_CALDAV . '}schedule-calendar-transp' => new ScheduleCalendarTransp('transparent'),
366
+                '{' . \OCA\DAV\DAV\Sharing\Plugin::NS_OWNCLOUD . '}owner-principal' => $this->convertPrincipal($row['principaluri'], !$this->legacyEndpoint),
367
+                $readOnlyPropertyName => $readOnly,
368
+            ];
369
+
370
+            foreach($this->propertyMap as $xmlName=>$dbName) {
371
+                $calendar[$xmlName] = $row[$dbName];
372
+            }
373
+
374
+            $this->addOwnerPrincipal($calendar);
375
+
376
+            $calendars[$calendar['id']] = $calendar;
377
+        }
378
+        $result->closeCursor();
379
+
380
+        return array_values($calendars);
381
+    }
382
+
383
+    /**
384
+     * @param $principalUri
385
+     * @return array
386
+     */
387
+    public function getUsersOwnCalendars($principalUri) {
388
+        $principalUri = $this->convertPrincipal($principalUri, true);
389
+        $fields = array_values($this->propertyMap);
390
+        $fields[] = 'id';
391
+        $fields[] = 'uri';
392
+        $fields[] = 'synctoken';
393
+        $fields[] = 'components';
394
+        $fields[] = 'principaluri';
395
+        $fields[] = 'transparent';
396
+        // Making fields a comma-delimited list
397
+        $query = $this->db->getQueryBuilder();
398
+        $query->select($fields)->from('calendars')
399
+            ->where($query->expr()->eq('principaluri', $query->createNamedParameter($principalUri)))
400
+            ->orderBy('calendarorder', 'ASC');
401
+        $stmt = $query->execute();
402
+        $calendars = [];
403
+        while($row = $stmt->fetch(\PDO::FETCH_ASSOC)) {
404
+            $components = [];
405
+            if ($row['components']) {
406
+                $components = explode(',',$row['components']);
407
+            }
408
+            $calendar = [
409
+                'id' => $row['id'],
410
+                'uri' => $row['uri'],
411
+                'principaluri' => $this->convertPrincipal($row['principaluri'], !$this->legacyEndpoint),
412
+                '{' . Plugin::NS_CALENDARSERVER . '}getctag' => 'http://sabre.io/ns/sync/' . ($row['synctoken']?$row['synctoken']:'0'),
413
+                '{http://sabredav.org/ns}sync-token' => $row['synctoken']?$row['synctoken']:'0',
414
+                '{' . Plugin::NS_CALDAV . '}supported-calendar-component-set' => new SupportedCalendarComponentSet($components),
415
+                '{' . Plugin::NS_CALDAV . '}schedule-calendar-transp' => new ScheduleCalendarTransp($row['transparent']?'transparent':'opaque'),
416
+            ];
417
+            foreach($this->propertyMap as $xmlName=>$dbName) {
418
+                $calendar[$xmlName] = $row[$dbName];
419
+            }
420
+
421
+            $this->addOwnerPrincipal($calendar);
422
+
423
+            if (!isset($calendars[$calendar['id']])) {
424
+                $calendars[$calendar['id']] = $calendar;
425
+            }
426
+        }
427
+        $stmt->closeCursor();
428
+        return array_values($calendars);
429
+    }
430
+
431
+
432
+    /**
433
+     * @param $uid
434
+     * @return string
435
+     */
436
+    private function getUserDisplayName($uid) {
437
+        if (!isset($this->userDisplayNames[$uid])) {
438
+            $user = $this->userManager->get($uid);
439
+
440
+            if ($user instanceof IUser) {
441
+                $this->userDisplayNames[$uid] = $user->getDisplayName();
442
+            } else {
443
+                $this->userDisplayNames[$uid] = $uid;
444
+            }
445
+        }
446
+
447
+        return $this->userDisplayNames[$uid];
448
+    }
449
+
450
+    /**
451
+     * @return array
452
+     */
453
+    public function getPublicCalendars() {
454
+        $fields = array_values($this->propertyMap);
455
+        $fields[] = 'a.id';
456
+        $fields[] = 'a.uri';
457
+        $fields[] = 'a.synctoken';
458
+        $fields[] = 'a.components';
459
+        $fields[] = 'a.principaluri';
460
+        $fields[] = 'a.transparent';
461
+        $fields[] = 's.access';
462
+        $fields[] = 's.publicuri';
463
+        $calendars = [];
464
+        $query = $this->db->getQueryBuilder();
465
+        $result = $query->select($fields)
466
+            ->from('dav_shares', 's')
467
+            ->join('s', 'calendars', 'a', $query->expr()->eq('s.resourceid', 'a.id'))
468
+            ->where($query->expr()->in('s.access', $query->createNamedParameter(self::ACCESS_PUBLIC)))
469
+            ->andWhere($query->expr()->eq('s.type', $query->createNamedParameter('calendar')))
470
+            ->execute();
471
+
472
+        while($row = $result->fetch()) {
473
+            list(, $name) = Uri\split($row['principaluri']);
474
+            $row['displayname'] = $row['displayname'] . "($name)";
475
+            $components = [];
476
+            if ($row['components']) {
477
+                $components = explode(',',$row['components']);
478
+            }
479
+            $calendar = [
480
+                'id' => $row['id'],
481
+                'uri' => $row['publicuri'],
482
+                'principaluri' => $this->convertPrincipal($row['principaluri'], !$this->legacyEndpoint),
483
+                '{' . Plugin::NS_CALENDARSERVER . '}getctag' => 'http://sabre.io/ns/sync/' . ($row['synctoken']?$row['synctoken']:'0'),
484
+                '{http://sabredav.org/ns}sync-token' => $row['synctoken']?$row['synctoken']:'0',
485
+                '{' . Plugin::NS_CALDAV . '}supported-calendar-component-set' => new SupportedCalendarComponentSet($components),
486
+                '{' . Plugin::NS_CALDAV . '}schedule-calendar-transp' => new ScheduleCalendarTransp($row['transparent']?'transparent':'opaque'),
487
+                '{' . \OCA\DAV\DAV\Sharing\Plugin::NS_OWNCLOUD . '}owner-principal' => $this->convertPrincipal($row['principaluri'], $this->legacyEndpoint),
488
+                '{' . \OCA\DAV\DAV\Sharing\Plugin::NS_OWNCLOUD . '}read-only' => (int)$row['access'] === Backend::ACCESS_READ,
489
+                '{' . \OCA\DAV\DAV\Sharing\Plugin::NS_OWNCLOUD . '}public' => (int)$row['access'] === self::ACCESS_PUBLIC,
490
+            ];
491
+
492
+            foreach($this->propertyMap as $xmlName=>$dbName) {
493
+                $calendar[$xmlName] = $row[$dbName];
494
+            }
495
+
496
+            $this->addOwnerPrincipal($calendar);
497
+
498
+            if (!isset($calendars[$calendar['id']])) {
499
+                $calendars[$calendar['id']] = $calendar;
500
+            }
501
+        }
502
+        $result->closeCursor();
503
+
504
+        return array_values($calendars);
505
+    }
506
+
507
+    /**
508
+     * @param string $uri
509
+     * @return array
510
+     * @throws NotFound
511
+     */
512
+    public function getPublicCalendar($uri) {
513
+        $fields = array_values($this->propertyMap);
514
+        $fields[] = 'a.id';
515
+        $fields[] = 'a.uri';
516
+        $fields[] = 'a.synctoken';
517
+        $fields[] = 'a.components';
518
+        $fields[] = 'a.principaluri';
519
+        $fields[] = 'a.transparent';
520
+        $fields[] = 's.access';
521
+        $fields[] = 's.publicuri';
522
+        $query = $this->db->getQueryBuilder();
523
+        $result = $query->select($fields)
524
+            ->from('dav_shares', 's')
525
+            ->join('s', 'calendars', 'a', $query->expr()->eq('s.resourceid', 'a.id'))
526
+            ->where($query->expr()->in('s.access', $query->createNamedParameter(self::ACCESS_PUBLIC)))
527
+            ->andWhere($query->expr()->eq('s.type', $query->createNamedParameter('calendar')))
528
+            ->andWhere($query->expr()->eq('s.publicuri', $query->createNamedParameter($uri)))
529
+            ->execute();
530
+
531
+        $row = $result->fetch(\PDO::FETCH_ASSOC);
532
+
533
+        $result->closeCursor();
534
+
535
+        if ($row === false) {
536
+            throw new NotFound('Node with name \'' . $uri . '\' could not be found');
537
+        }
538
+
539
+        list(, $name) = Uri\split($row['principaluri']);
540
+        $row['displayname'] = $row['displayname'] . ' ' . "($name)";
541
+        $components = [];
542
+        if ($row['components']) {
543
+            $components = explode(',',$row['components']);
544
+        }
545
+        $calendar = [
546
+            'id' => $row['id'],
547
+            'uri' => $row['publicuri'],
548
+            'principaluri' => $this->convertPrincipal($row['principaluri'], !$this->legacyEndpoint),
549
+            '{' . Plugin::NS_CALENDARSERVER . '}getctag' => 'http://sabre.io/ns/sync/' . ($row['synctoken']?$row['synctoken']:'0'),
550
+            '{http://sabredav.org/ns}sync-token' => $row['synctoken']?$row['synctoken']:'0',
551
+            '{' . Plugin::NS_CALDAV . '}supported-calendar-component-set' => new SupportedCalendarComponentSet($components),
552
+            '{' . Plugin::NS_CALDAV . '}schedule-calendar-transp' => new ScheduleCalendarTransp($row['transparent']?'transparent':'opaque'),
553
+            '{' . \OCA\DAV\DAV\Sharing\Plugin::NS_OWNCLOUD . '}owner-principal' => $this->convertPrincipal($row['principaluri'], !$this->legacyEndpoint),
554
+            '{' . \OCA\DAV\DAV\Sharing\Plugin::NS_OWNCLOUD . '}read-only' => (int)$row['access'] === Backend::ACCESS_READ,
555
+            '{' . \OCA\DAV\DAV\Sharing\Plugin::NS_OWNCLOUD . '}public' => (int)$row['access'] === self::ACCESS_PUBLIC,
556
+        ];
557
+
558
+        foreach($this->propertyMap as $xmlName=>$dbName) {
559
+            $calendar[$xmlName] = $row[$dbName];
560
+        }
561
+
562
+        $this->addOwnerPrincipal($calendar);
563
+
564
+        return $calendar;
565
+
566
+    }
567
+
568
+    /**
569
+     * @param string $principal
570
+     * @param string $uri
571
+     * @return array|null
572
+     */
573
+    public function getCalendarByUri($principal, $uri) {
574
+        $fields = array_values($this->propertyMap);
575
+        $fields[] = 'id';
576
+        $fields[] = 'uri';
577
+        $fields[] = 'synctoken';
578
+        $fields[] = 'components';
579
+        $fields[] = 'principaluri';
580
+        $fields[] = 'transparent';
581
+
582
+        // Making fields a comma-delimited list
583
+        $query = $this->db->getQueryBuilder();
584
+        $query->select($fields)->from('calendars')
585
+            ->where($query->expr()->eq('uri', $query->createNamedParameter($uri)))
586
+            ->andWhere($query->expr()->eq('principaluri', $query->createNamedParameter($principal)))
587
+            ->setMaxResults(1);
588
+        $stmt = $query->execute();
589
+
590
+        $row = $stmt->fetch(\PDO::FETCH_ASSOC);
591
+        $stmt->closeCursor();
592
+        if ($row === false) {
593
+            return null;
594
+        }
595
+
596
+        $components = [];
597
+        if ($row['components']) {
598
+            $components = explode(',',$row['components']);
599
+        }
600
+
601
+        $calendar = [
602
+            'id' => $row['id'],
603
+            'uri' => $row['uri'],
604
+            'principaluri' => $this->convertPrincipal($row['principaluri'], !$this->legacyEndpoint),
605
+            '{' . Plugin::NS_CALENDARSERVER . '}getctag' => 'http://sabre.io/ns/sync/' . ($row['synctoken']?$row['synctoken']:'0'),
606
+            '{http://sabredav.org/ns}sync-token' => $row['synctoken']?$row['synctoken']:'0',
607
+            '{' . Plugin::NS_CALDAV . '}supported-calendar-component-set' => new SupportedCalendarComponentSet($components),
608
+            '{' . Plugin::NS_CALDAV . '}schedule-calendar-transp' => new ScheduleCalendarTransp($row['transparent']?'transparent':'opaque'),
609
+        ];
610
+
611
+        foreach($this->propertyMap as $xmlName=>$dbName) {
612
+            $calendar[$xmlName] = $row[$dbName];
613
+        }
614
+
615
+        $this->addOwnerPrincipal($calendar);
616
+
617
+        return $calendar;
618
+    }
619
+
620
+    /**
621
+     * @param $calendarId
622
+     * @return array|null
623
+     */
624
+    public function getCalendarById($calendarId) {
625
+        $fields = array_values($this->propertyMap);
626
+        $fields[] = 'id';
627
+        $fields[] = 'uri';
628
+        $fields[] = 'synctoken';
629
+        $fields[] = 'components';
630
+        $fields[] = 'principaluri';
631
+        $fields[] = 'transparent';
632
+
633
+        // Making fields a comma-delimited list
634
+        $query = $this->db->getQueryBuilder();
635
+        $query->select($fields)->from('calendars')
636
+            ->where($query->expr()->eq('id', $query->createNamedParameter($calendarId)))
637
+            ->setMaxResults(1);
638
+        $stmt = $query->execute();
639
+
640
+        $row = $stmt->fetch(\PDO::FETCH_ASSOC);
641
+        $stmt->closeCursor();
642
+        if ($row === false) {
643
+            return null;
644
+        }
645
+
646
+        $components = [];
647
+        if ($row['components']) {
648
+            $components = explode(',',$row['components']);
649
+        }
650
+
651
+        $calendar = [
652
+            'id' => $row['id'],
653
+            'uri' => $row['uri'],
654
+            'principaluri' => $this->convertPrincipal($row['principaluri'], !$this->legacyEndpoint),
655
+            '{' . Plugin::NS_CALENDARSERVER . '}getctag' => 'http://sabre.io/ns/sync/' . ($row['synctoken']?$row['synctoken']:'0'),
656
+            '{http://sabredav.org/ns}sync-token' => $row['synctoken']?$row['synctoken']:'0',
657
+            '{' . Plugin::NS_CALDAV . '}supported-calendar-component-set' => new SupportedCalendarComponentSet($components),
658
+            '{' . Plugin::NS_CALDAV . '}schedule-calendar-transp' => new ScheduleCalendarTransp($row['transparent']?'transparent':'opaque'),
659
+        ];
660
+
661
+        foreach($this->propertyMap as $xmlName=>$dbName) {
662
+            $calendar[$xmlName] = $row[$dbName];
663
+        }
664
+
665
+        $this->addOwnerPrincipal($calendar);
666
+
667
+        return $calendar;
668
+    }
669
+
670
+    /**
671
+     * @param $subscriptionId
672
+     */
673
+    public function getSubscriptionById($subscriptionId) {
674
+        $fields = array_values($this->subscriptionPropertyMap);
675
+        $fields[] = 'id';
676
+        $fields[] = 'uri';
677
+        $fields[] = 'source';
678
+        $fields[] = 'synctoken';
679
+        $fields[] = 'principaluri';
680
+        $fields[] = 'lastmodified';
681
+
682
+        $query = $this->db->getQueryBuilder();
683
+        $query->select($fields)
684
+            ->from('calendarsubscriptions')
685
+            ->where($query->expr()->eq('id', $query->createNamedParameter($subscriptionId)))
686
+            ->orderBy('calendarorder', 'asc');
687
+        $stmt =$query->execute();
688
+
689
+        $row = $stmt->fetch(\PDO::FETCH_ASSOC);
690
+        $stmt->closeCursor();
691
+        if ($row === false) {
692
+            return null;
693
+        }
694
+
695
+        $subscription = [
696
+            'id'           => $row['id'],
697
+            'uri'          => $row['uri'],
698
+            'principaluri' => $row['principaluri'],
699
+            'source'       => $row['source'],
700
+            'lastmodified' => $row['lastmodified'],
701
+            '{' . Plugin::NS_CALDAV . '}supported-calendar-component-set' => new SupportedCalendarComponentSet(['VTODO', 'VEVENT']),
702
+            '{http://sabredav.org/ns}sync-token' => $row['synctoken']?$row['synctoken']:'0',
703
+        ];
704
+
705
+        foreach($this->subscriptionPropertyMap as $xmlName=>$dbName) {
706
+            if (!is_null($row[$dbName])) {
707
+                $subscription[$xmlName] = $row[$dbName];
708
+            }
709
+        }
710
+
711
+        return $subscription;
712
+    }
713
+
714
+    /**
715
+     * Creates a new calendar for a principal.
716
+     *
717
+     * If the creation was a success, an id must be returned that can be used to reference
718
+     * this calendar in other methods, such as updateCalendar.
719
+     *
720
+     * @param string $principalUri
721
+     * @param string $calendarUri
722
+     * @param array $properties
723
+     * @return int
724
+     * @suppress SqlInjectionChecker
725
+     */
726
+    function createCalendar($principalUri, $calendarUri, array $properties) {
727
+        $values = [
728
+            'principaluri' => $this->convertPrincipal($principalUri, true),
729
+            'uri'          => $calendarUri,
730
+            'synctoken'    => 1,
731
+            'transparent'  => 0,
732
+            'components'   => 'VEVENT,VTODO',
733
+            'displayname'  => $calendarUri
734
+        ];
735
+
736
+        // Default value
737
+        $sccs = '{urn:ietf:params:xml:ns:caldav}supported-calendar-component-set';
738
+        if (isset($properties[$sccs])) {
739
+            if (!($properties[$sccs] instanceof SupportedCalendarComponentSet)) {
740
+                throw new DAV\Exception('The ' . $sccs . ' property must be of type: \Sabre\CalDAV\Property\SupportedCalendarComponentSet');
741
+            }
742
+            $values['components'] = implode(',',$properties[$sccs]->getValue());
743
+        } else if (isset($properties['components'])) {
744
+            // Allow to provide components internally without having
745
+            // to create a SupportedCalendarComponentSet object
746
+            $values['components'] = $properties['components'];
747
+        }
748
+
749
+        $transp = '{' . Plugin::NS_CALDAV . '}schedule-calendar-transp';
750
+        if (isset($properties[$transp])) {
751
+            $values['transparent'] = (int) ($properties[$transp]->getValue() === 'transparent');
752
+        }
753
+
754
+        foreach($this->propertyMap as $xmlName=>$dbName) {
755
+            if (isset($properties[$xmlName])) {
756
+                $values[$dbName] = $properties[$xmlName];
757
+            }
758
+        }
759
+
760
+        $query = $this->db->getQueryBuilder();
761
+        $query->insert('calendars');
762
+        foreach($values as $column => $value) {
763
+            $query->setValue($column, $query->createNamedParameter($value));
764
+        }
765
+        $query->execute();
766
+        $calendarId = $query->getLastInsertId();
767
+
768
+        $this->dispatcher->dispatch('\OCA\DAV\CalDAV\CalDavBackend::createCalendar', new GenericEvent(
769
+            '\OCA\DAV\CalDAV\CalDavBackend::createCalendar',
770
+            [
771
+                'calendarId' => $calendarId,
772
+                'calendarData' => $this->getCalendarById($calendarId),
773
+        ]));
774
+
775
+        return $calendarId;
776
+    }
777
+
778
+    /**
779
+     * Updates properties for a calendar.
780
+     *
781
+     * The list of mutations is stored in a Sabre\DAV\PropPatch object.
782
+     * To do the actual updates, you must tell this object which properties
783
+     * you're going to process with the handle() method.
784
+     *
785
+     * Calling the handle method is like telling the PropPatch object "I
786
+     * promise I can handle updating this property".
787
+     *
788
+     * Read the PropPatch documentation for more info and examples.
789
+     *
790
+     * @param mixed $calendarId
791
+     * @param PropPatch $propPatch
792
+     * @return void
793
+     */
794
+    function updateCalendar($calendarId, PropPatch $propPatch) {
795
+        $supportedProperties = array_keys($this->propertyMap);
796
+        $supportedProperties[] = '{' . Plugin::NS_CALDAV . '}schedule-calendar-transp';
797
+
798
+        /**
799
+         * @suppress SqlInjectionChecker
800
+         */
801
+        $propPatch->handle($supportedProperties, function($mutations) use ($calendarId) {
802
+            $newValues = [];
803
+            foreach ($mutations as $propertyName => $propertyValue) {
804
+
805
+                switch ($propertyName) {
806
+                    case '{' . Plugin::NS_CALDAV . '}schedule-calendar-transp' :
807
+                        $fieldName = 'transparent';
808
+                        $newValues[$fieldName] = (int) ($propertyValue->getValue() === 'transparent');
809
+                        break;
810
+                    default :
811
+                        $fieldName = $this->propertyMap[$propertyName];
812
+                        $newValues[$fieldName] = $propertyValue;
813
+                        break;
814
+                }
815
+
816
+            }
817
+            $query = $this->db->getQueryBuilder();
818
+            $query->update('calendars');
819
+            foreach ($newValues as $fieldName => $value) {
820
+                $query->set($fieldName, $query->createNamedParameter($value));
821
+            }
822
+            $query->where($query->expr()->eq('id', $query->createNamedParameter($calendarId)));
823
+            $query->execute();
824
+
825
+            $this->addChange($calendarId, "", 2);
826
+
827
+            $this->dispatcher->dispatch('\OCA\DAV\CalDAV\CalDavBackend::updateCalendar', new GenericEvent(
828
+                '\OCA\DAV\CalDAV\CalDavBackend::updateCalendar',
829
+                [
830
+                    'calendarId' => $calendarId,
831
+                    'calendarData' => $this->getCalendarById($calendarId),
832
+                    'shares' => $this->getShares($calendarId),
833
+                    'propertyMutations' => $mutations,
834
+            ]));
835
+
836
+            return true;
837
+        });
838
+    }
839
+
840
+    /**
841
+     * Delete a calendar and all it's objects
842
+     *
843
+     * @param mixed $calendarId
844
+     * @return void
845
+     */
846
+    function deleteCalendar($calendarId) {
847
+        $this->dispatcher->dispatch('\OCA\DAV\CalDAV\CalDavBackend::deleteCalendar', new GenericEvent(
848
+            '\OCA\DAV\CalDAV\CalDavBackend::deleteCalendar',
849
+            [
850
+                'calendarId' => $calendarId,
851
+                'calendarData' => $this->getCalendarById($calendarId),
852
+                'shares' => $this->getShares($calendarId),
853
+        ]));
854
+
855
+        $stmt = $this->db->prepare('DELETE FROM `*PREFIX*calendarobjects` WHERE `calendarid` = ? AND `calendartype` = ?');
856
+        $stmt->execute([$calendarId, self::CALENDAR_TYPE_CALENDAR]);
857
+
858
+        $stmt = $this->db->prepare('DELETE FROM `*PREFIX*calendars` WHERE `id` = ?');
859
+        $stmt->execute([$calendarId]);
860
+
861
+        $stmt = $this->db->prepare('DELETE FROM `*PREFIX*calendarchanges` WHERE `calendarid` = ? AND `calendartype` = ?');
862
+        $stmt->execute([$calendarId, self::CALENDAR_TYPE_CALENDAR]);
863
+
864
+        $this->calendarSharingBackend->deleteAllShares($calendarId);
865
+
866
+        $query = $this->db->getQueryBuilder();
867
+        $query->delete($this->dbObjectPropertiesTable)
868
+            ->where($query->expr()->eq('calendarid', $query->createNamedParameter($calendarId)))
869
+            ->andWhere($query->expr()->eq('calendartype', $query->createNamedParameter(self::CALENDAR_TYPE_CALENDAR)))
870
+            ->execute();
871
+    }
872
+
873
+    /**
874
+     * Delete all of an user's shares
875
+     *
876
+     * @param string $principaluri
877
+     * @return void
878
+     */
879
+    function deleteAllSharesByUser($principaluri) {
880
+        $this->calendarSharingBackend->deleteAllSharesByUser($principaluri);
881
+    }
882
+
883
+    /**
884
+     * Returns all calendar objects within a calendar.
885
+     *
886
+     * Every item contains an array with the following keys:
887
+     *   * calendardata - The iCalendar-compatible calendar data
888
+     *   * uri - a unique key which will be used to construct the uri. This can
889
+     *     be any arbitrary string, but making sure it ends with '.ics' is a
890
+     *     good idea. This is only the basename, or filename, not the full
891
+     *     path.
892
+     *   * lastmodified - a timestamp of the last modification time
893
+     *   * etag - An arbitrary string, surrounded by double-quotes. (e.g.:
894
+     *   '"abcdef"')
895
+     *   * size - The size of the calendar objects, in bytes.
896
+     *   * component - optional, a string containing the type of object, such
897
+     *     as 'vevent' or 'vtodo'. If specified, this will be used to populate
898
+     *     the Content-Type header.
899
+     *
900
+     * Note that the etag is optional, but it's highly encouraged to return for
901
+     * speed reasons.
902
+     *
903
+     * The calendardata is also optional. If it's not returned
904
+     * 'getCalendarObject' will be called later, which *is* expected to return
905
+     * calendardata.
906
+     *
907
+     * If neither etag or size are specified, the calendardata will be
908
+     * used/fetched to determine these numbers. If both are specified the
909
+     * amount of times this is needed is reduced by a great degree.
910
+     *
911
+     * @param mixed $id
912
+     * @param int $calendarType
913
+     * @return array
914
+     */
915
+    public function getCalendarObjects($id, $calendarType=self::CALENDAR_TYPE_CALENDAR):array {
916
+        $query = $this->db->getQueryBuilder();
917
+        $query->select(['id', 'uri', 'lastmodified', 'etag', 'calendarid', 'size', 'componenttype', 'classification'])
918
+            ->from('calendarobjects')
919
+            ->where($query->expr()->eq('calendarid', $query->createNamedParameter($id)))
920
+            ->andWhere($query->expr()->eq('calendartype', $query->createNamedParameter($calendarType)));
921
+        $stmt = $query->execute();
922
+
923
+        $result = [];
924
+        foreach($stmt->fetchAll(\PDO::FETCH_ASSOC) as $row) {
925
+            $result[] = [
926
+                'id'           => $row['id'],
927
+                'uri'          => $row['uri'],
928
+                'lastmodified' => $row['lastmodified'],
929
+                'etag'         => '"' . $row['etag'] . '"',
930
+                'calendarid'   => $row['calendarid'],
931
+                'size'         => (int)$row['size'],
932
+                'component'    => strtolower($row['componenttype']),
933
+                'classification'=> (int)$row['classification']
934
+            ];
935
+        }
936
+
937
+        return $result;
938
+    }
939
+
940
+    /**
941
+     * Returns information from a single calendar object, based on it's object
942
+     * uri.
943
+     *
944
+     * The object uri is only the basename, or filename and not a full path.
945
+     *
946
+     * The returned array must have the same keys as getCalendarObjects. The
947
+     * 'calendardata' object is required here though, while it's not required
948
+     * for getCalendarObjects.
949
+     *
950
+     * This method must return null if the object did not exist.
951
+     *
952
+     * @param mixed $id
953
+     * @param string $objectUri
954
+     * @param int $calendarType
955
+     * @return array|null
956
+     */
957
+    public function getCalendarObject($id, $objectUri, $calendarType=self::CALENDAR_TYPE_CALENDAR) {
958
+        $query = $this->db->getQueryBuilder();
959
+        $query->select(['id', 'uri', 'lastmodified', 'etag', 'calendarid', 'size', 'calendardata', 'componenttype', 'classification'])
960
+            ->from('calendarobjects')
961
+            ->where($query->expr()->eq('calendarid', $query->createNamedParameter($id)))
962
+            ->andWhere($query->expr()->eq('uri', $query->createNamedParameter($objectUri)))
963
+            ->andWhere($query->expr()->eq('calendartype', $query->createNamedParameter($calendarType)));
964
+        $stmt = $query->execute();
965
+        $row = $stmt->fetch(\PDO::FETCH_ASSOC);
966
+
967
+        if(!$row) {
968
+            return null;
969
+        }
970
+
971
+        return [
972
+            'id'            => $row['id'],
973
+            'uri'           => $row['uri'],
974
+            'lastmodified'  => $row['lastmodified'],
975
+            'etag'          => '"' . $row['etag'] . '"',
976
+            'calendarid'    => $row['calendarid'],
977
+            'size'          => (int)$row['size'],
978
+            'calendardata'  => $this->readBlob($row['calendardata']),
979
+            'component'     => strtolower($row['componenttype']),
980
+            'classification'=> (int)$row['classification']
981
+        ];
982
+    }
983
+
984
+    /**
985
+     * Returns a list of calendar objects.
986
+     *
987
+     * This method should work identical to getCalendarObject, but instead
988
+     * return all the calendar objects in the list as an array.
989
+     *
990
+     * If the backend supports this, it may allow for some speed-ups.
991
+     *
992
+     * @param mixed $calendarId
993
+     * @param string[] $uris
994
+     * @param int $calendarType
995
+     * @return array
996
+     */
997
+    public function getMultipleCalendarObjects($id, array $uris, $calendarType=self::CALENDAR_TYPE_CALENDAR):array {
998
+        if (empty($uris)) {
999
+            return [];
1000
+        }
1001
+
1002
+        $chunks = array_chunk($uris, 100);
1003
+        $objects = [];
1004
+
1005
+        $query = $this->db->getQueryBuilder();
1006
+        $query->select(['id', 'uri', 'lastmodified', 'etag', 'calendarid', 'size', 'calendardata', 'componenttype', 'classification'])
1007
+            ->from('calendarobjects')
1008
+            ->where($query->expr()->eq('calendarid', $query->createNamedParameter($id)))
1009
+            ->andWhere($query->expr()->in('uri', $query->createParameter('uri')))
1010
+            ->andWhere($query->expr()->eq('calendartype', $query->createNamedParameter($calendarType)));
1011
+
1012
+        foreach ($chunks as $uris) {
1013
+            $query->setParameter('uri', $uris, IQueryBuilder::PARAM_STR_ARRAY);
1014
+            $result = $query->execute();
1015
+
1016
+            while ($row = $result->fetch()) {
1017
+                $objects[] = [
1018
+                    'id'           => $row['id'],
1019
+                    'uri'          => $row['uri'],
1020
+                    'lastmodified' => $row['lastmodified'],
1021
+                    'etag'         => '"' . $row['etag'] . '"',
1022
+                    'calendarid'   => $row['calendarid'],
1023
+                    'size'         => (int)$row['size'],
1024
+                    'calendardata' => $this->readBlob($row['calendardata']),
1025
+                    'component'    => strtolower($row['componenttype']),
1026
+                    'classification' => (int)$row['classification']
1027
+                ];
1028
+            }
1029
+            $result->closeCursor();
1030
+        }
1031
+
1032
+        return $objects;
1033
+    }
1034
+
1035
+    /**
1036
+     * Creates a new calendar object.
1037
+     *
1038
+     * The object uri is only the basename, or filename and not a full path.
1039
+     *
1040
+     * It is possible return an etag from this function, which will be used in
1041
+     * the response to this PUT request. Note that the ETag must be surrounded
1042
+     * by double-quotes.
1043
+     *
1044
+     * However, you should only really return this ETag if you don't mangle the
1045
+     * calendar-data. If the result of a subsequent GET to this object is not
1046
+     * the exact same as this request body, you should omit the ETag.
1047
+     *
1048
+     * @param mixed $calendarId
1049
+     * @param string $objectUri
1050
+     * @param string $calendarData
1051
+     * @param int $calendarType
1052
+     * @return string
1053
+     */
1054
+    function createCalendarObject($calendarId, $objectUri, $calendarData, $calendarType=self::CALENDAR_TYPE_CALENDAR) {
1055
+        $extraData = $this->getDenormalizedData($calendarData);
1056
+
1057
+        $q = $this->db->getQueryBuilder();
1058
+        $q->select($q->func()->count('*'))
1059
+            ->from('calendarobjects')
1060
+            ->where($q->expr()->eq('calendarid', $q->createNamedParameter($calendarId)))
1061
+            ->andWhere($q->expr()->eq('uid', $q->createNamedParameter($extraData['uid'])))
1062
+            ->andWhere($q->expr()->eq('calendartype', $q->createNamedParameter($calendarType)));
1063
+
1064
+        $result = $q->execute();
1065
+        $count = (int) $result->fetchColumn();
1066
+        $result->closeCursor();
1067
+
1068
+        if ($count !== 0) {
1069
+            throw new \Sabre\DAV\Exception\BadRequest('Calendar object with uid already exists in this calendar collection.');
1070
+        }
1071
+
1072
+        $query = $this->db->getQueryBuilder();
1073
+        $query->insert('calendarobjects')
1074
+            ->values([
1075
+                'calendarid' => $query->createNamedParameter($calendarId),
1076
+                'uri' => $query->createNamedParameter($objectUri),
1077
+                'calendardata' => $query->createNamedParameter($calendarData, IQueryBuilder::PARAM_LOB),
1078
+                'lastmodified' => $query->createNamedParameter(time()),
1079
+                'etag' => $query->createNamedParameter($extraData['etag']),
1080
+                'size' => $query->createNamedParameter($extraData['size']),
1081
+                'componenttype' => $query->createNamedParameter($extraData['componentType']),
1082
+                'firstoccurence' => $query->createNamedParameter($extraData['firstOccurence']),
1083
+                'lastoccurence' => $query->createNamedParameter($extraData['lastOccurence']),
1084
+                'classification' => $query->createNamedParameter($extraData['classification']),
1085
+                'uid' => $query->createNamedParameter($extraData['uid']),
1086
+                'calendartype' => $query->createNamedParameter($calendarType),
1087
+            ])
1088
+            ->execute();
1089
+
1090
+        $this->updateProperties($calendarId, $objectUri, $calendarData, $calendarType);
1091
+
1092
+        if ($calendarType === self::CALENDAR_TYPE_CALENDAR) {
1093
+            $this->dispatcher->dispatch('\OCA\DAV\CalDAV\CalDavBackend::createCalendarObject', new GenericEvent(
1094
+                '\OCA\DAV\CalDAV\CalDavBackend::createCalendarObject',
1095
+                [
1096
+                    'calendarId' => $calendarId,
1097
+                    'calendarData' => $this->getCalendarById($calendarId),
1098
+                    'shares' => $this->getShares($calendarId),
1099
+                    'objectData' => $this->getCalendarObject($calendarId, $objectUri),
1100
+                ]
1101
+            ));
1102
+        } else {
1103
+            $this->dispatcher->dispatch('\OCA\DAV\CalDAV\CalDavBackend::createCachedCalendarObject', new GenericEvent(
1104
+                '\OCA\DAV\CalDAV\CalDavBackend::createCachedCalendarObject',
1105
+                [
1106
+                    'subscriptionId' => $calendarId,
1107
+                    'calendarData' => $this->getCalendarById($calendarId),
1108
+                    'shares' => $this->getShares($calendarId),
1109
+                    'objectData' => $this->getCalendarObject($calendarId, $objectUri),
1110
+                ]
1111
+            ));
1112
+        }
1113
+        $this->addChange($calendarId, $objectUri, 1, $calendarType);
1114
+
1115
+        return '"' . $extraData['etag'] . '"';
1116
+    }
1117
+
1118
+    /**
1119
+     * Updates an existing calendarobject, based on it's uri.
1120
+     *
1121
+     * The object uri is only the basename, or filename and not a full path.
1122
+     *
1123
+     * It is possible return an etag from this function, which will be used in
1124
+     * the response to this PUT request. Note that the ETag must be surrounded
1125
+     * by double-quotes.
1126
+     *
1127
+     * However, you should only really return this ETag if you don't mangle the
1128
+     * calendar-data. If the result of a subsequent GET to this object is not
1129
+     * the exact same as this request body, you should omit the ETag.
1130
+     *
1131
+     * @param mixed $calendarId
1132
+     * @param string $objectUri
1133
+     * @param string $calendarData
1134
+     * @param int $calendarType
1135
+     * @return string
1136
+     */
1137
+    function updateCalendarObject($calendarId, $objectUri, $calendarData, $calendarType=self::CALENDAR_TYPE_CALENDAR) {
1138
+        $extraData = $this->getDenormalizedData($calendarData);
1139
+        $query = $this->db->getQueryBuilder();
1140
+        $query->update('calendarobjects')
1141
+                ->set('calendardata', $query->createNamedParameter($calendarData, IQueryBuilder::PARAM_LOB))
1142
+                ->set('lastmodified', $query->createNamedParameter(time()))
1143
+                ->set('etag', $query->createNamedParameter($extraData['etag']))
1144
+                ->set('size', $query->createNamedParameter($extraData['size']))
1145
+                ->set('componenttype', $query->createNamedParameter($extraData['componentType']))
1146
+                ->set('firstoccurence', $query->createNamedParameter($extraData['firstOccurence']))
1147
+                ->set('lastoccurence', $query->createNamedParameter($extraData['lastOccurence']))
1148
+                ->set('classification', $query->createNamedParameter($extraData['classification']))
1149
+                ->set('uid', $query->createNamedParameter($extraData['uid']))
1150
+            ->where($query->expr()->eq('calendarid', $query->createNamedParameter($calendarId)))
1151
+            ->andWhere($query->expr()->eq('uri', $query->createNamedParameter($objectUri)))
1152
+            ->andWhere($query->expr()->eq('calendartype', $query->createNamedParameter($calendarType)))
1153
+            ->execute();
1154
+
1155
+        $this->updateProperties($calendarId, $objectUri, $calendarData, $calendarType);
1156
+
1157
+        $data = $this->getCalendarObject($calendarId, $objectUri);
1158
+        if (is_array($data)) {
1159
+            if ($calendarType === self::CALENDAR_TYPE_CALENDAR) {
1160
+                $this->dispatcher->dispatch('\OCA\DAV\CalDAV\CalDavBackend::updateCalendarObject', new GenericEvent(
1161
+                    '\OCA\DAV\CalDAV\CalDavBackend::updateCalendarObject',
1162
+                    [
1163
+                        'calendarId' => $calendarId,
1164
+                        'calendarData' => $this->getCalendarById($calendarId),
1165
+                        'shares' => $this->getShares($calendarId),
1166
+                        'objectData' => $data,
1167
+                    ]
1168
+                ));
1169
+            } else {
1170
+                $this->dispatcher->dispatch('\OCA\DAV\CalDAV\CalDavBackend::updateCachedCalendarObject', new GenericEvent(
1171
+                    '\OCA\DAV\CalDAV\CalDavBackend::updateCachedCalendarObject',
1172
+                    [
1173
+                        'subscriptionId' => $calendarId,
1174
+                        'calendarData' => $this->getCalendarById($calendarId),
1175
+                        'shares' => $this->getShares($calendarId),
1176
+                        'objectData' => $data,
1177
+                    ]
1178
+                ));
1179
+            }
1180
+        }
1181
+        $this->addChange($calendarId, $objectUri, 2, $calendarType);
1182
+
1183
+        return '"' . $extraData['etag'] . '"';
1184
+    }
1185
+
1186
+    /**
1187
+     * @param int $calendarObjectId
1188
+     * @param int $classification
1189
+     */
1190
+    public function setClassification($calendarObjectId, $classification) {
1191
+        if (!in_array($classification, [
1192
+            self::CLASSIFICATION_PUBLIC, self::CLASSIFICATION_PRIVATE, self::CLASSIFICATION_CONFIDENTIAL
1193
+        ])) {
1194
+            throw new \InvalidArgumentException();
1195
+        }
1196
+        $query = $this->db->getQueryBuilder();
1197
+        $query->update('calendarobjects')
1198
+            ->set('classification', $query->createNamedParameter($classification))
1199
+            ->where($query->expr()->eq('id', $query->createNamedParameter($calendarObjectId)))
1200
+            ->execute();
1201
+    }
1202
+
1203
+    /**
1204
+     * Deletes an existing calendar object.
1205
+     *
1206
+     * The object uri is only the basename, or filename and not a full path.
1207
+     *
1208
+     * @param mixed $calendarId
1209
+     * @param string $objectUri
1210
+     * @param int $calendarType
1211
+     * @return void
1212
+     */
1213
+    function deleteCalendarObject($calendarId, $objectUri, $calendarType=self::CALENDAR_TYPE_CALENDAR) {
1214
+        $data = $this->getCalendarObject($calendarId, $objectUri, $calendarType);
1215
+        if (is_array($data)) {
1216
+            if ($calendarType === self::CALENDAR_TYPE_CALENDAR) {
1217
+                $this->dispatcher->dispatch('\OCA\DAV\CalDAV\CalDavBackend::deleteCalendarObject', new GenericEvent(
1218
+                    '\OCA\DAV\CalDAV\CalDavBackend::deleteCalendarObject',
1219
+                    [
1220
+                        'calendarId' => $calendarId,
1221
+                        'calendarData' => $this->getCalendarById($calendarId),
1222
+                        'shares' => $this->getShares($calendarId),
1223
+                        'objectData' => $data,
1224
+                    ]
1225
+                ));
1226
+            } else {
1227
+                $this->dispatcher->dispatch('\OCA\DAV\CalDAV\CalDavBackend::deleteCachedCalendarObject', new GenericEvent(
1228
+                    '\OCA\DAV\CalDAV\CalDavBackend::deleteCachedCalendarObject',
1229
+                    [
1230
+                        'subscriptionId' => $calendarId,
1231
+                        'calendarData' => $this->getCalendarById($calendarId),
1232
+                        'shares' => $this->getShares($calendarId),
1233
+                        'objectData' => $data,
1234
+                    ]
1235
+                ));
1236
+            }
1237
+        }
1238
+
1239
+        $stmt = $this->db->prepare('DELETE FROM `*PREFIX*calendarobjects` WHERE `calendarid` = ? AND `uri` = ? AND `calendartype` = ?');
1240
+        $stmt->execute([$calendarId, $objectUri, $calendarType]);
1241
+
1242
+        if (is_array($data)) {
1243
+            $this->purgeProperties($calendarId, $data['id'], $calendarType);
1244
+        }
1245
+
1246
+        $this->addChange($calendarId, $objectUri, 3, $calendarType);
1247
+    }
1248
+
1249
+    /**
1250
+     * Performs a calendar-query on the contents of this calendar.
1251
+     *
1252
+     * The calendar-query is defined in RFC4791 : CalDAV. Using the
1253
+     * calendar-query it is possible for a client to request a specific set of
1254
+     * object, based on contents of iCalendar properties, date-ranges and
1255
+     * iCalendar component types (VTODO, VEVENT).
1256
+     *
1257
+     * This method should just return a list of (relative) urls that match this
1258
+     * query.
1259
+     *
1260
+     * The list of filters are specified as an array. The exact array is
1261
+     * documented by Sabre\CalDAV\CalendarQueryParser.
1262
+     *
1263
+     * Note that it is extremely likely that getCalendarObject for every path
1264
+     * returned from this method will be called almost immediately after. You
1265
+     * may want to anticipate this to speed up these requests.
1266
+     *
1267
+     * This method provides a default implementation, which parses *all* the
1268
+     * iCalendar objects in the specified calendar.
1269
+     *
1270
+     * This default may well be good enough for personal use, and calendars
1271
+     * that aren't very large. But if you anticipate high usage, big calendars
1272
+     * or high loads, you are strongly advised to optimize certain paths.
1273
+     *
1274
+     * The best way to do so is override this method and to optimize
1275
+     * specifically for 'common filters'.
1276
+     *
1277
+     * Requests that are extremely common are:
1278
+     *   * requests for just VEVENTS
1279
+     *   * requests for just VTODO
1280
+     *   * requests with a time-range-filter on either VEVENT or VTODO.
1281
+     *
1282
+     * ..and combinations of these requests. It may not be worth it to try to
1283
+     * handle every possible situation and just rely on the (relatively
1284
+     * easy to use) CalendarQueryValidator to handle the rest.
1285
+     *
1286
+     * Note that especially time-range-filters may be difficult to parse. A
1287
+     * time-range filter specified on a VEVENT must for instance also handle
1288
+     * recurrence rules correctly.
1289
+     * A good example of how to interprete all these filters can also simply
1290
+     * be found in Sabre\CalDAV\CalendarQueryFilter. This class is as correct
1291
+     * as possible, so it gives you a good idea on what type of stuff you need
1292
+     * to think of.
1293
+     *
1294
+     * @param mixed $id
1295
+     * @param array $filters
1296
+     * @param int $calendarType
1297
+     * @return array
1298
+     */
1299
+    public function calendarQuery($id, array $filters, $calendarType=self::CALENDAR_TYPE_CALENDAR):array {
1300
+        $componentType = null;
1301
+        $requirePostFilter = true;
1302
+        $timeRange = null;
1303
+
1304
+        // if no filters were specified, we don't need to filter after a query
1305
+        if (!$filters['prop-filters'] && !$filters['comp-filters']) {
1306
+            $requirePostFilter = false;
1307
+        }
1308
+
1309
+        // Figuring out if there's a component filter
1310
+        if (count($filters['comp-filters']) > 0 && !$filters['comp-filters'][0]['is-not-defined']) {
1311
+            $componentType = $filters['comp-filters'][0]['name'];
1312
+
1313
+            // Checking if we need post-filters
1314
+            if (!$filters['prop-filters'] && !$filters['comp-filters'][0]['comp-filters'] && !$filters['comp-filters'][0]['time-range'] && !$filters['comp-filters'][0]['prop-filters']) {
1315
+                $requirePostFilter = false;
1316
+            }
1317
+            // There was a time-range filter
1318
+            if ($componentType === 'VEVENT' && isset($filters['comp-filters'][0]['time-range'])) {
1319
+                $timeRange = $filters['comp-filters'][0]['time-range'];
1320
+
1321
+                // If start time OR the end time is not specified, we can do a
1322
+                // 100% accurate mysql query.
1323
+                if (!$filters['prop-filters'] && !$filters['comp-filters'][0]['comp-filters'] && !$filters['comp-filters'][0]['prop-filters'] && (!$timeRange['start'] || !$timeRange['end'])) {
1324
+                    $requirePostFilter = false;
1325
+                }
1326
+            }
1327
+
1328
+        }
1329
+        $columns = ['uri'];
1330
+        if ($requirePostFilter) {
1331
+            $columns = ['uri', 'calendardata'];
1332
+        }
1333
+        $query = $this->db->getQueryBuilder();
1334
+        $query->select($columns)
1335
+            ->from('calendarobjects')
1336
+            ->where($query->expr()->eq('calendarid', $query->createNamedParameter($id)))
1337
+            ->andWhere($query->expr()->eq('calendartype', $query->createNamedParameter($calendarType)));
1338
+
1339
+        if ($componentType) {
1340
+            $query->andWhere($query->expr()->eq('componenttype', $query->createNamedParameter($componentType)));
1341
+        }
1342
+
1343
+        if ($timeRange && $timeRange['start']) {
1344
+            $query->andWhere($query->expr()->gt('lastoccurence', $query->createNamedParameter($timeRange['start']->getTimeStamp())));
1345
+        }
1346
+        if ($timeRange && $timeRange['end']) {
1347
+            $query->andWhere($query->expr()->lt('firstoccurence', $query->createNamedParameter($timeRange['end']->getTimeStamp())));
1348
+        }
1349
+
1350
+        $stmt = $query->execute();
1351
+
1352
+        $result = [];
1353
+        while($row = $stmt->fetch(\PDO::FETCH_ASSOC)) {
1354
+            if ($requirePostFilter) {
1355
+                // validateFilterForObject will parse the calendar data
1356
+                // catch parsing errors
1357
+                try {
1358
+                    $matches = $this->validateFilterForObject($row, $filters);
1359
+                } catch(ParseException $ex) {
1360
+                    $this->logger->logException($ex, [
1361
+                        'app' => 'dav',
1362
+                        'message' => 'Caught parsing exception for calendar data. This usually indicates invalid calendar data. calendar-id:'.$id.' uri:'.$row['uri']
1363
+                    ]);
1364
+                    continue;
1365
+                } catch (InvalidDataException $ex) {
1366
+                    $this->logger->logException($ex, [
1367
+                        'app' => 'dav',
1368
+                        'message' => 'Caught invalid data exception for calendar data. This usually indicates invalid calendar data. calendar-id:'.$id.' uri:'.$row['uri']
1369
+                    ]);
1370
+                    continue;
1371
+                }
1372
+
1373
+                if (!$matches) {
1374
+                    continue;
1375
+                }
1376
+            }
1377
+            $result[] = $row['uri'];
1378
+        }
1379
+
1380
+        return $result;
1381
+    }
1382
+
1383
+    /**
1384
+     * custom Nextcloud search extension for CalDAV
1385
+     *
1386
+     * TODO - this should optionally cover cached calendar objects as well
1387
+     *
1388
+     * @param string $principalUri
1389
+     * @param array $filters
1390
+     * @param integer|null $limit
1391
+     * @param integer|null $offset
1392
+     * @return array
1393
+     */
1394
+    public function calendarSearch($principalUri, array $filters, $limit=null, $offset=null) {
1395
+        $calendars = $this->getCalendarsForUser($principalUri);
1396
+        $ownCalendars = [];
1397
+        $sharedCalendars = [];
1398
+
1399
+        $uriMapper = [];
1400
+
1401
+        foreach($calendars as $calendar) {
1402
+            if ($calendar['{http://owncloud.org/ns}owner-principal'] === $principalUri) {
1403
+                $ownCalendars[] = $calendar['id'];
1404
+            } else {
1405
+                $sharedCalendars[] = $calendar['id'];
1406
+            }
1407
+            $uriMapper[$calendar['id']] = $calendar['uri'];
1408
+        }
1409
+        if (count($ownCalendars) === 0 && count($sharedCalendars) === 0) {
1410
+            return [];
1411
+        }
1412
+
1413
+        $query = $this->db->getQueryBuilder();
1414
+        // Calendar id expressions
1415
+        $calendarExpressions = [];
1416
+        foreach($ownCalendars as $id) {
1417
+            $calendarExpressions[] = $query->expr()->andX(
1418
+                $query->expr()->eq('c.calendarid',
1419
+                    $query->createNamedParameter($id)),
1420
+                $query->expr()->eq('c.calendartype',
1421
+                        $query->createNamedParameter(self::CALENDAR_TYPE_CALENDAR)));
1422
+        }
1423
+        foreach($sharedCalendars as $id) {
1424
+            $calendarExpressions[] = $query->expr()->andX(
1425
+                $query->expr()->eq('c.calendarid',
1426
+                    $query->createNamedParameter($id)),
1427
+                $query->expr()->eq('c.classification',
1428
+                    $query->createNamedParameter(self::CLASSIFICATION_PUBLIC)),
1429
+                $query->expr()->eq('c.calendartype',
1430
+                    $query->createNamedParameter(self::CALENDAR_TYPE_CALENDAR)));
1431
+        }
1432
+
1433
+        if (count($calendarExpressions) === 1) {
1434
+            $calExpr = $calendarExpressions[0];
1435
+        } else {
1436
+            $calExpr = call_user_func_array([$query->expr(), 'orX'], $calendarExpressions);
1437
+        }
1438
+
1439
+        // Component expressions
1440
+        $compExpressions = [];
1441
+        foreach($filters['comps'] as $comp) {
1442
+            $compExpressions[] = $query->expr()
1443
+                ->eq('c.componenttype', $query->createNamedParameter($comp));
1444
+        }
1445
+
1446
+        if (count($compExpressions) === 1) {
1447
+            $compExpr = $compExpressions[0];
1448
+        } else {
1449
+            $compExpr = call_user_func_array([$query->expr(), 'orX'], $compExpressions);
1450
+        }
1451
+
1452
+        if (!isset($filters['props'])) {
1453
+            $filters['props'] = [];
1454
+        }
1455
+        if (!isset($filters['params'])) {
1456
+            $filters['params'] = [];
1457
+        }
1458
+
1459
+        $propParamExpressions = [];
1460
+        foreach($filters['props'] as $prop) {
1461
+            $propParamExpressions[] = $query->expr()->andX(
1462
+                $query->expr()->eq('i.name', $query->createNamedParameter($prop)),
1463
+                $query->expr()->isNull('i.parameter')
1464
+            );
1465
+        }
1466
+        foreach($filters['params'] as $param) {
1467
+            $propParamExpressions[] = $query->expr()->andX(
1468
+                $query->expr()->eq('i.name', $query->createNamedParameter($param['property'])),
1469
+                $query->expr()->eq('i.parameter', $query->createNamedParameter($param['parameter']))
1470
+            );
1471
+        }
1472
+
1473
+        if (count($propParamExpressions) === 1) {
1474
+            $propParamExpr = $propParamExpressions[0];
1475
+        } else {
1476
+            $propParamExpr = call_user_func_array([$query->expr(), 'orX'], $propParamExpressions);
1477
+        }
1478
+
1479
+        $query->select(['c.calendarid', 'c.uri'])
1480
+            ->from($this->dbObjectPropertiesTable, 'i')
1481
+            ->join('i', 'calendarobjects', 'c', $query->expr()->eq('i.objectid', 'c.id'))
1482
+            ->where($calExpr)
1483
+            ->andWhere($compExpr)
1484
+            ->andWhere($propParamExpr)
1485
+            ->andWhere($query->expr()->iLike('i.value',
1486
+                $query->createNamedParameter('%'.$this->db->escapeLikeParameter($filters['search-term']).'%')));
1487
+
1488
+        if ($offset) {
1489
+            $query->setFirstResult($offset);
1490
+        }
1491
+        if ($limit) {
1492
+            $query->setMaxResults($limit);
1493
+        }
1494
+
1495
+        $stmt = $query->execute();
1496
+
1497
+        $result = [];
1498
+        while($row = $stmt->fetch(\PDO::FETCH_ASSOC)) {
1499
+            $path = $uriMapper[$row['calendarid']] . '/' . $row['uri'];
1500
+            if (!in_array($path, $result)) {
1501
+                $result[] = $path;
1502
+            }
1503
+        }
1504
+
1505
+        return $result;
1506
+    }
1507
+
1508
+    /**
1509
+     * used for Nextcloud's calendar API
1510
+     *
1511
+     * @param array $calendarInfo
1512
+     * @param string $pattern
1513
+     * @param array $searchProperties
1514
+     * @param array $options
1515
+     * @param integer|null $limit
1516
+     * @param integer|null $offset
1517
+     *
1518
+     * @return array
1519
+     */
1520
+    public function search(array $calendarInfo, $pattern, array $searchProperties,
1521
+                            array $options, $limit, $offset) {
1522
+        $outerQuery = $this->db->getQueryBuilder();
1523
+        $innerQuery = $this->db->getQueryBuilder();
1524
+
1525
+        $innerQuery->selectDistinct('op.objectid')
1526
+            ->from($this->dbObjectPropertiesTable, 'op')
1527
+            ->andWhere($innerQuery->expr()->eq('op.calendarid',
1528
+                $outerQuery->createNamedParameter($calendarInfo['id'])))
1529
+            ->andWhere($innerQuery->expr()->eq('op.calendartype',
1530
+                $outerQuery->createNamedParameter(self::CALENDAR_TYPE_CALENDAR)));
1531
+
1532
+        // only return public items for shared calendars for now
1533
+        if ($calendarInfo['principaluri'] !== $calendarInfo['{http://owncloud.org/ns}owner-principal']) {
1534
+            $innerQuery->andWhere($innerQuery->expr()->eq('c.classification',
1535
+                $outerQuery->createNamedParameter(self::CLASSIFICATION_PUBLIC)));
1536
+        }
1537
+
1538
+        $or = $innerQuery->expr()->orX();
1539
+        foreach($searchProperties as $searchProperty) {
1540
+            $or->add($innerQuery->expr()->eq('op.name',
1541
+                $outerQuery->createNamedParameter($searchProperty)));
1542
+        }
1543
+        $innerQuery->andWhere($or);
1544
+
1545
+        if ($pattern !== '') {
1546
+            $innerQuery->andWhere($innerQuery->expr()->iLike('op.value',
1547
+                $outerQuery->createNamedParameter('%' .
1548
+                    $this->db->escapeLikeParameter($pattern) . '%')));
1549
+        }
1550
+
1551
+        $outerQuery->select('c.id', 'c.calendardata', 'c.componenttype', 'c.uid', 'c.uri')
1552
+            ->from('calendarobjects', 'c');
1553
+
1554
+        if (isset($options['timerange'])) {
1555
+            if (isset($options['timerange']['start']) && $options['timerange']['start'] instanceof DateTime) {
1556
+                $outerQuery->andWhere($outerQuery->expr()->gt('lastoccurence',
1557
+                    $outerQuery->createNamedParameter($options['timerange']['start']->getTimeStamp())));
1558
+
1559
+            }
1560
+            if (isset($options['timerange']['end']) && $options['timerange']['end'] instanceof DateTime) {
1561
+                $outerQuery->andWhere($outerQuery->expr()->lt('firstoccurence',
1562
+                    $outerQuery->createNamedParameter($options['timerange']['end']->getTimeStamp())));
1563
+            }
1564
+        }
1565
+
1566
+        if (isset($options['types'])) {
1567
+            $or = $outerQuery->expr()->orX();
1568
+            foreach($options['types'] as $type) {
1569
+                $or->add($outerQuery->expr()->eq('componenttype',
1570
+                    $outerQuery->createNamedParameter($type)));
1571
+            }
1572
+            $outerQuery->andWhere($or);
1573
+        }
1574
+
1575
+        $outerQuery->andWhere($outerQuery->expr()->in('c.id',
1576
+            $outerQuery->createFunction($innerQuery->getSQL())));
1577
+
1578
+        if ($offset) {
1579
+            $outerQuery->setFirstResult($offset);
1580
+        }
1581
+        if ($limit) {
1582
+            $outerQuery->setMaxResults($limit);
1583
+        }
1584
+
1585
+        $result = $outerQuery->execute();
1586
+        $calendarObjects = $result->fetchAll();
1587
+
1588
+        return array_map(function($o) {
1589
+            $calendarData = Reader::read($o['calendardata']);
1590
+            $comps = $calendarData->getComponents();
1591
+            $objects = [];
1592
+            $timezones = [];
1593
+            foreach($comps as $comp) {
1594
+                if ($comp instanceof VTimeZone) {
1595
+                    $timezones[] = $comp;
1596
+                } else {
1597
+                    $objects[] = $comp;
1598
+                }
1599
+            }
1600
+
1601
+            return [
1602
+                'id' => $o['id'],
1603
+                'type' => $o['componenttype'],
1604
+                'uid' => $o['uid'],
1605
+                'uri' => $o['uri'],
1606
+                'objects' => array_map(function($c) {
1607
+                    return $this->transformSearchData($c);
1608
+                }, $objects),
1609
+                'timezones' => array_map(function($c) {
1610
+                    return $this->transformSearchData($c);
1611
+                }, $timezones),
1612
+            ];
1613
+        }, $calendarObjects);
1614
+    }
1615
+
1616
+    /**
1617
+     * @param Component $comp
1618
+     * @return array
1619
+     */
1620
+    private function transformSearchData(Component $comp) {
1621
+        $data = [];
1622
+        /** @var Component[] $subComponents */
1623
+        $subComponents = $comp->getComponents();
1624
+        /** @var Property[] $properties */
1625
+        $properties = array_filter($comp->children(), function($c) {
1626
+            return $c instanceof Property;
1627
+        });
1628
+        $validationRules = $comp->getValidationRules();
1629
+
1630
+        foreach($subComponents as $subComponent) {
1631
+            $name = $subComponent->name;
1632
+            if (!isset($data[$name])) {
1633
+                $data[$name] = [];
1634
+            }
1635
+            $data[$name][] = $this->transformSearchData($subComponent);
1636
+        }
1637
+
1638
+        foreach($properties as $property) {
1639
+            $name = $property->name;
1640
+            if (!isset($validationRules[$name])) {
1641
+                $validationRules[$name] = '*';
1642
+            }
1643
+
1644
+            $rule = $validationRules[$property->name];
1645
+            if ($rule === '+' || $rule === '*') { // multiple
1646
+                if (!isset($data[$name])) {
1647
+                    $data[$name] = [];
1648
+                }
1649
+
1650
+                $data[$name][] = $this->transformSearchProperty($property);
1651
+            } else { // once
1652
+                $data[$name] = $this->transformSearchProperty($property);
1653
+            }
1654
+        }
1655
+
1656
+        return $data;
1657
+    }
1658
+
1659
+    /**
1660
+     * @param Property $prop
1661
+     * @return array
1662
+     */
1663
+    private function transformSearchProperty(Property $prop) {
1664
+        // No need to check Date, as it extends DateTime
1665
+        if ($prop instanceof Property\ICalendar\DateTime) {
1666
+            $value = $prop->getDateTime();
1667
+        } else {
1668
+            $value = $prop->getValue();
1669
+        }
1670
+
1671
+        return [
1672
+            $value,
1673
+            $prop->parameters()
1674
+        ];
1675
+    }
1676
+
1677
+    /**
1678
+     * Searches through all of a users calendars and calendar objects to find
1679
+     * an object with a specific UID.
1680
+     *
1681
+     * This method should return the path to this object, relative to the
1682
+     * calendar home, so this path usually only contains two parts:
1683
+     *
1684
+     * calendarpath/objectpath.ics
1685
+     *
1686
+     * If the uid is not found, return null.
1687
+     *
1688
+     * This method should only consider * objects that the principal owns, so
1689
+     * any calendars owned by other principals that also appear in this
1690
+     * collection should be ignored.
1691
+     *
1692
+     * @param string $principalUri
1693
+     * @param string $uid
1694
+     * @return string|null
1695
+     */
1696
+    function getCalendarObjectByUID($principalUri, $uid) {
1697
+
1698
+        $query = $this->db->getQueryBuilder();
1699
+        $query->selectAlias('c.uri', 'calendaruri')->selectAlias('co.uri', 'objecturi')
1700
+            ->from('calendarobjects', 'co')
1701
+            ->leftJoin('co', 'calendars', 'c', $query->expr()->eq('co.calendarid', 'c.id'))
1702
+            ->where($query->expr()->eq('c.principaluri', $query->createNamedParameter($principalUri)))
1703
+            ->andWhere($query->expr()->eq('co.uid', $query->createNamedParameter($uid)))
1704
+            ->andWhere($query->expr()->eq('co.uid', $query->createNamedParameter($uid)));
1705
+
1706
+        $stmt = $query->execute();
1707
+
1708
+        if ($row = $stmt->fetch(\PDO::FETCH_ASSOC)) {
1709
+            return $row['calendaruri'] . '/' . $row['objecturi'];
1710
+        }
1711
+
1712
+        return null;
1713
+    }
1714
+
1715
+    /**
1716
+     * The getChanges method returns all the changes that have happened, since
1717
+     * the specified syncToken in the specified calendar.
1718
+     *
1719
+     * This function should return an array, such as the following:
1720
+     *
1721
+     * [
1722
+     *   'syncToken' => 'The current synctoken',
1723
+     *   'added'   => [
1724
+     *      'new.txt',
1725
+     *   ],
1726
+     *   'modified'   => [
1727
+     *      'modified.txt',
1728
+     *   ],
1729
+     *   'deleted' => [
1730
+     *      'foo.php.bak',
1731
+     *      'old.txt'
1732
+     *   ]
1733
+     * );
1734
+     *
1735
+     * The returned syncToken property should reflect the *current* syncToken
1736
+     * of the calendar, as reported in the {http://sabredav.org/ns}sync-token
1737
+     * property This is * needed here too, to ensure the operation is atomic.
1738
+     *
1739
+     * If the $syncToken argument is specified as null, this is an initial
1740
+     * sync, and all members should be reported.
1741
+     *
1742
+     * The modified property is an array of nodenames that have changed since
1743
+     * the last token.
1744
+     *
1745
+     * The deleted property is an array with nodenames, that have been deleted
1746
+     * from collection.
1747
+     *
1748
+     * The $syncLevel argument is basically the 'depth' of the report. If it's
1749
+     * 1, you only have to report changes that happened only directly in
1750
+     * immediate descendants. If it's 2, it should also include changes from
1751
+     * the nodes below the child collections. (grandchildren)
1752
+     *
1753
+     * The $limit argument allows a client to specify how many results should
1754
+     * be returned at most. If the limit is not specified, it should be treated
1755
+     * as infinite.
1756
+     *
1757
+     * If the limit (infinite or not) is higher than you're willing to return,
1758
+     * you should throw a Sabre\DAV\Exception\TooMuchMatches() exception.
1759
+     *
1760
+     * If the syncToken is expired (due to data cleanup) or unknown, you must
1761
+     * return null.
1762
+     *
1763
+     * The limit is 'suggestive'. You are free to ignore it.
1764
+     *
1765
+     * @param string $calendarId
1766
+     * @param string $syncToken
1767
+     * @param int $syncLevel
1768
+     * @param int $limit
1769
+     * @param int $calendarType
1770
+     * @return array
1771
+     */
1772
+    function getChangesForCalendar($calendarId, $syncToken, $syncLevel, $limit = null, $calendarType=self::CALENDAR_TYPE_CALENDAR) {
1773
+        // Current synctoken
1774
+        $stmt = $this->db->prepare('SELECT `synctoken` FROM `*PREFIX*calendars` WHERE `id` = ?');
1775
+        $stmt->execute([ $calendarId ]);
1776
+        $currentToken = $stmt->fetchColumn(0);
1777
+
1778
+        if (is_null($currentToken)) {
1779
+            return null;
1780
+        }
1781
+
1782
+        $result = [
1783
+            'syncToken' => $currentToken,
1784
+            'added'     => [],
1785
+            'modified'  => [],
1786
+            'deleted'   => [],
1787
+        ];
1788
+
1789
+        if ($syncToken) {
1790
+
1791
+            $query = "SELECT `uri`, `operation` FROM `*PREFIX*calendarchanges` WHERE `synctoken` >= ? AND `synctoken` < ? AND `calendarid` = ? AND `calendartype` = ? ORDER BY `synctoken`";
1792
+            if ($limit>0) {
1793
+                $query.= " LIMIT " . (int)$limit;
1794
+            }
1795
+
1796
+            // Fetching all changes
1797
+            $stmt = $this->db->prepare($query);
1798
+            $stmt->execute([$syncToken, $currentToken, $calendarId, $calendarType]);
1799
+
1800
+            $changes = [];
1801
+
1802
+            // This loop ensures that any duplicates are overwritten, only the
1803
+            // last change on a node is relevant.
1804
+            while($row = $stmt->fetch(\PDO::FETCH_ASSOC)) {
1805
+
1806
+                $changes[$row['uri']] = $row['operation'];
1807
+
1808
+            }
1809
+
1810
+            foreach($changes as $uri => $operation) {
1811
+
1812
+                switch($operation) {
1813
+                    case 1 :
1814
+                        $result['added'][] = $uri;
1815
+                        break;
1816
+                    case 2 :
1817
+                        $result['modified'][] = $uri;
1818
+                        break;
1819
+                    case 3 :
1820
+                        $result['deleted'][] = $uri;
1821
+                        break;
1822
+                }
1823
+
1824
+            }
1825
+        } else {
1826
+            // No synctoken supplied, this is the initial sync.
1827
+            $query = "SELECT `uri` FROM `*PREFIX*calendarobjects` WHERE `calendarid` = ? AND `calendartype` = ?";
1828
+            $stmt = $this->db->prepare($query);
1829
+            $stmt->execute([$calendarId, $calendarType]);
1830
+
1831
+            $result['added'] = $stmt->fetchAll(\PDO::FETCH_COLUMN);
1832
+        }
1833
+        return $result;
1834
+
1835
+    }
1836
+
1837
+    /**
1838
+     * Returns a list of subscriptions for a principal.
1839
+     *
1840
+     * Every subscription is an array with the following keys:
1841
+     *  * id, a unique id that will be used by other functions to modify the
1842
+     *    subscription. This can be the same as the uri or a database key.
1843
+     *  * uri. This is just the 'base uri' or 'filename' of the subscription.
1844
+     *  * principaluri. The owner of the subscription. Almost always the same as
1845
+     *    principalUri passed to this method.
1846
+     *
1847
+     * Furthermore, all the subscription info must be returned too:
1848
+     *
1849
+     * 1. {DAV:}displayname
1850
+     * 2. {http://apple.com/ns/ical/}refreshrate
1851
+     * 3. {http://calendarserver.org/ns/}subscribed-strip-todos (omit if todos
1852
+     *    should not be stripped).
1853
+     * 4. {http://calendarserver.org/ns/}subscribed-strip-alarms (omit if alarms
1854
+     *    should not be stripped).
1855
+     * 5. {http://calendarserver.org/ns/}subscribed-strip-attachments (omit if
1856
+     *    attachments should not be stripped).
1857
+     * 6. {http://calendarserver.org/ns/}source (Must be a
1858
+     *     Sabre\DAV\Property\Href).
1859
+     * 7. {http://apple.com/ns/ical/}calendar-color
1860
+     * 8. {http://apple.com/ns/ical/}calendar-order
1861
+     * 9. {urn:ietf:params:xml:ns:caldav}supported-calendar-component-set
1862
+     *    (should just be an instance of
1863
+     *    Sabre\CalDAV\Property\SupportedCalendarComponentSet, with a bunch of
1864
+     *    default components).
1865
+     *
1866
+     * @param string $principalUri
1867
+     * @return array
1868
+     */
1869
+    function getSubscriptionsForUser($principalUri) {
1870
+        $fields = array_values($this->subscriptionPropertyMap);
1871
+        $fields[] = 'id';
1872
+        $fields[] = 'uri';
1873
+        $fields[] = 'source';
1874
+        $fields[] = 'principaluri';
1875
+        $fields[] = 'lastmodified';
1876
+        $fields[] = 'synctoken';
1877
+
1878
+        $query = $this->db->getQueryBuilder();
1879
+        $query->select($fields)
1880
+            ->from('calendarsubscriptions')
1881
+            ->where($query->expr()->eq('principaluri', $query->createNamedParameter($principalUri)))
1882
+            ->orderBy('calendarorder', 'asc');
1883
+        $stmt =$query->execute();
1884
+
1885
+        $subscriptions = [];
1886
+        while($row = $stmt->fetch(\PDO::FETCH_ASSOC)) {
1887
+
1888
+            $subscription = [
1889
+                'id'           => $row['id'],
1890
+                'uri'          => $row['uri'],
1891
+                'principaluri' => $row['principaluri'],
1892
+                'source'       => $row['source'],
1893
+                'lastmodified' => $row['lastmodified'],
1894
+
1895
+                '{' . Plugin::NS_CALDAV . '}supported-calendar-component-set' => new SupportedCalendarComponentSet(['VTODO', 'VEVENT']),
1896
+                '{http://sabredav.org/ns}sync-token' => $row['synctoken']?$row['synctoken']:'0',
1897
+            ];
1898
+
1899
+            foreach($this->subscriptionPropertyMap as $xmlName=>$dbName) {
1900
+                if (!is_null($row[$dbName])) {
1901
+                    $subscription[$xmlName] = $row[$dbName];
1902
+                }
1903
+            }
1904
+
1905
+            $subscriptions[] = $subscription;
1906
+
1907
+        }
1908
+
1909
+        return $subscriptions;
1910
+    }
1911
+
1912
+    /**
1913
+     * Creates a new subscription for a principal.
1914
+     *
1915
+     * If the creation was a success, an id must be returned that can be used to reference
1916
+     * this subscription in other methods, such as updateSubscription.
1917
+     *
1918
+     * @param string $principalUri
1919
+     * @param string $uri
1920
+     * @param array $properties
1921
+     * @return mixed
1922
+     */
1923
+    function createSubscription($principalUri, $uri, array $properties) {
1924
+
1925
+        if (!isset($properties['{http://calendarserver.org/ns/}source'])) {
1926
+            throw new Forbidden('The {http://calendarserver.org/ns/}source property is required when creating subscriptions');
1927
+        }
1928
+
1929
+        $values = [
1930
+            'principaluri' => $principalUri,
1931
+            'uri'          => $uri,
1932
+            'source'       => $properties['{http://calendarserver.org/ns/}source']->getHref(),
1933
+            'lastmodified' => time(),
1934
+        ];
1935
+
1936
+        $propertiesBoolean = ['striptodos', 'stripalarms', 'stripattachments'];
1937
+
1938
+        foreach($this->subscriptionPropertyMap as $xmlName=>$dbName) {
1939
+            if (array_key_exists($xmlName, $properties)) {
1940
+                    $values[$dbName] = $properties[$xmlName];
1941
+                    if (in_array($dbName, $propertiesBoolean)) {
1942
+                        $values[$dbName] = true;
1943
+                }
1944
+            }
1945
+        }
1946
+
1947
+        $valuesToInsert = [];
1948
+
1949
+        $query = $this->db->getQueryBuilder();
1950
+
1951
+        foreach (array_keys($values) as $name) {
1952
+            $valuesToInsert[$name] = $query->createNamedParameter($values[$name]);
1953
+        }
1954
+
1955
+        $query->insert('calendarsubscriptions')
1956
+            ->values($valuesToInsert)
1957
+            ->execute();
1958
+
1959
+        $subscriptionId = $this->db->lastInsertId('*PREFIX*calendarsubscriptions');
1960
+
1961
+        $this->dispatcher->dispatch('\OCA\DAV\CalDAV\CalDavBackend::createSubscription', new GenericEvent(
1962
+            '\OCA\DAV\CalDAV\CalDavBackend::createSubscription',
1963
+            [
1964
+                'subscriptionId' => $subscriptionId,
1965
+                'subscriptionData' => $this->getSubscriptionById($subscriptionId),
1966
+            ]));
1967
+
1968
+        return $subscriptionId;
1969
+    }
1970
+
1971
+    /**
1972
+     * Updates a subscription
1973
+     *
1974
+     * The list of mutations is stored in a Sabre\DAV\PropPatch object.
1975
+     * To do the actual updates, you must tell this object which properties
1976
+     * you're going to process with the handle() method.
1977
+     *
1978
+     * Calling the handle method is like telling the PropPatch object "I
1979
+     * promise I can handle updating this property".
1980
+     *
1981
+     * Read the PropPatch documentation for more info and examples.
1982
+     *
1983
+     * @param mixed $subscriptionId
1984
+     * @param PropPatch $propPatch
1985
+     * @return void
1986
+     */
1987
+    function updateSubscription($subscriptionId, PropPatch $propPatch) {
1988
+        $supportedProperties = array_keys($this->subscriptionPropertyMap);
1989
+        $supportedProperties[] = '{http://calendarserver.org/ns/}source';
1990
+
1991
+        /**
1992
+         * @suppress SqlInjectionChecker
1993
+         */
1994
+        $propPatch->handle($supportedProperties, function($mutations) use ($subscriptionId) {
1995
+
1996
+            $newValues = [];
1997
+
1998
+            foreach($mutations as $propertyName=>$propertyValue) {
1999
+                if ($propertyName === '{http://calendarserver.org/ns/}source') {
2000
+                    $newValues['source'] = $propertyValue->getHref();
2001
+                } else {
2002
+                    $fieldName = $this->subscriptionPropertyMap[$propertyName];
2003
+                    $newValues[$fieldName] = $propertyValue;
2004
+                }
2005
+            }
2006
+
2007
+            $query = $this->db->getQueryBuilder();
2008
+            $query->update('calendarsubscriptions')
2009
+                ->set('lastmodified', $query->createNamedParameter(time()));
2010
+            foreach($newValues as $fieldName=>$value) {
2011
+                $query->set($fieldName, $query->createNamedParameter($value));
2012
+            }
2013
+            $query->where($query->expr()->eq('id', $query->createNamedParameter($subscriptionId)))
2014
+                ->execute();
2015
+
2016
+            $this->dispatcher->dispatch('\OCA\DAV\CalDAV\CalDavBackend::updateSubscription', new GenericEvent(
2017
+                '\OCA\DAV\CalDAV\CalDavBackend::updateSubscription',
2018
+                [
2019
+                    'subscriptionId' => $subscriptionId,
2020
+                    'subscriptionData' => $this->getSubscriptionById($subscriptionId),
2021
+                    'propertyMutations' => $mutations,
2022
+                ]));
2023
+
2024
+            return true;
2025
+
2026
+        });
2027
+    }
2028
+
2029
+    /**
2030
+     * Deletes a subscription.
2031
+     *
2032
+     * @param mixed $subscriptionId
2033
+     * @return void
2034
+     */
2035
+    function deleteSubscription($subscriptionId) {
2036
+        $this->dispatcher->dispatch('\OCA\DAV\CalDAV\CalDavBackend::deleteSubscription', new GenericEvent(
2037
+            '\OCA\DAV\CalDAV\CalDavBackend::deleteSubscription',
2038
+            [
2039
+                'subscriptionId' => $subscriptionId,
2040
+                'subscriptionData' => $this->getSubscriptionById($subscriptionId),
2041
+            ]));
2042
+
2043
+        $query = $this->db->getQueryBuilder();
2044
+        $query->delete('calendarsubscriptions')
2045
+            ->where($query->expr()->eq('id', $query->createNamedParameter($subscriptionId)))
2046
+            ->execute();
2047
+
2048
+        $query = $this->db->getQueryBuilder();
2049
+        $query->delete('calendarobjects')
2050
+            ->where($query->expr()->eq('calendarid', $query->createNamedParameter($subscriptionId)))
2051
+            ->andWhere($query->expr()->eq('calendartype', $query->createNamedParameter(self::CALENDAR_TYPE_SUBSCRIPTION)))
2052
+            ->execute();
2053
+
2054
+        $query->delete('calendarchanges')
2055
+            ->where($query->expr()->eq('calendarid', $query->createNamedParameter($subscriptionId)))
2056
+            ->andWhere($query->expr()->eq('calendartype', $query->createNamedParameter(self::CALENDAR_TYPE_SUBSCRIPTION)))
2057
+            ->execute();
2058
+
2059
+        $query->delete($this->dbObjectPropertiesTable)
2060
+            ->where($query->expr()->eq('calendarid', $query->createNamedParameter($subscriptionId)))
2061
+            ->andWhere($query->expr()->eq('calendartype', $query->createNamedParameter(self::CALENDAR_TYPE_SUBSCRIPTION)))
2062
+            ->execute();
2063
+    }
2064
+
2065
+    /**
2066
+     * Returns a single scheduling object for the inbox collection.
2067
+     *
2068
+     * The returned array should contain the following elements:
2069
+     *   * uri - A unique basename for the object. This will be used to
2070
+     *           construct a full uri.
2071
+     *   * calendardata - The iCalendar object
2072
+     *   * lastmodified - The last modification date. Can be an int for a unix
2073
+     *                    timestamp, or a PHP DateTime object.
2074
+     *   * etag - A unique token that must change if the object changed.
2075
+     *   * size - The size of the object, in bytes.
2076
+     *
2077
+     * @param string $principalUri
2078
+     * @param string $objectUri
2079
+     * @return array
2080
+     */
2081
+    function getSchedulingObject($principalUri, $objectUri) {
2082
+        $query = $this->db->getQueryBuilder();
2083
+        $stmt = $query->select(['uri', 'calendardata', 'lastmodified', 'etag', 'size'])
2084
+            ->from('schedulingobjects')
2085
+            ->where($query->expr()->eq('principaluri', $query->createNamedParameter($principalUri)))
2086
+            ->andWhere($query->expr()->eq('uri', $query->createNamedParameter($objectUri)))
2087
+            ->execute();
2088
+
2089
+        $row = $stmt->fetch(\PDO::FETCH_ASSOC);
2090
+
2091
+        if(!$row) {
2092
+            return null;
2093
+        }
2094
+
2095
+        return [
2096
+                'uri'          => $row['uri'],
2097
+                'calendardata' => $row['calendardata'],
2098
+                'lastmodified' => $row['lastmodified'],
2099
+                'etag'         => '"' . $row['etag'] . '"',
2100
+                'size'         => (int)$row['size'],
2101
+        ];
2102
+    }
2103
+
2104
+    /**
2105
+     * Returns all scheduling objects for the inbox collection.
2106
+     *
2107
+     * These objects should be returned as an array. Every item in the array
2108
+     * should follow the same structure as returned from getSchedulingObject.
2109
+     *
2110
+     * The main difference is that 'calendardata' is optional.
2111
+     *
2112
+     * @param string $principalUri
2113
+     * @return array
2114
+     */
2115
+    function getSchedulingObjects($principalUri) {
2116
+        $query = $this->db->getQueryBuilder();
2117
+        $stmt = $query->select(['uri', 'calendardata', 'lastmodified', 'etag', 'size'])
2118
+                ->from('schedulingobjects')
2119
+                ->where($query->expr()->eq('principaluri', $query->createNamedParameter($principalUri)))
2120
+                ->execute();
2121
+
2122
+        $result = [];
2123
+        foreach($stmt->fetchAll(\PDO::FETCH_ASSOC) as $row) {
2124
+            $result[] = [
2125
+                    'calendardata' => $row['calendardata'],
2126
+                    'uri'          => $row['uri'],
2127
+                    'lastmodified' => $row['lastmodified'],
2128
+                    'etag'         => '"' . $row['etag'] . '"',
2129
+                    'size'         => (int)$row['size'],
2130
+            ];
2131
+        }
2132
+
2133
+        return $result;
2134
+    }
2135
+
2136
+    /**
2137
+     * Deletes a scheduling object from the inbox collection.
2138
+     *
2139
+     * @param string $principalUri
2140
+     * @param string $objectUri
2141
+     * @return void
2142
+     */
2143
+    function deleteSchedulingObject($principalUri, $objectUri) {
2144
+        $query = $this->db->getQueryBuilder();
2145
+        $query->delete('schedulingobjects')
2146
+                ->where($query->expr()->eq('principaluri', $query->createNamedParameter($principalUri)))
2147
+                ->andWhere($query->expr()->eq('uri', $query->createNamedParameter($objectUri)))
2148
+                ->execute();
2149
+    }
2150
+
2151
+    /**
2152
+     * Creates a new scheduling object. This should land in a users' inbox.
2153
+     *
2154
+     * @param string $principalUri
2155
+     * @param string $objectUri
2156
+     * @param string $objectData
2157
+     * @return void
2158
+     */
2159
+    function createSchedulingObject($principalUri, $objectUri, $objectData) {
2160
+        $query = $this->db->getQueryBuilder();
2161
+        $query->insert('schedulingobjects')
2162
+            ->values([
2163
+                'principaluri' => $query->createNamedParameter($principalUri),
2164
+                'calendardata' => $query->createNamedParameter($objectData, IQueryBuilder::PARAM_LOB),
2165
+                'uri' => $query->createNamedParameter($objectUri),
2166
+                'lastmodified' => $query->createNamedParameter(time()),
2167
+                'etag' => $query->createNamedParameter(md5($objectData)),
2168
+                'size' => $query->createNamedParameter(strlen($objectData))
2169
+            ])
2170
+            ->execute();
2171
+    }
2172
+
2173
+    /**
2174
+     * Adds a change record to the calendarchanges table.
2175
+     *
2176
+     * @param mixed $calendarId
2177
+     * @param string $objectUri
2178
+     * @param int $operation 1 = add, 2 = modify, 3 = delete.
2179
+     * @param int $calendarType
2180
+     * @return void
2181
+     */
2182
+    protected function addChange($calendarId, $objectUri, $operation, $calendarType=self::CALENDAR_TYPE_CALENDAR) {
2183
+        $table = $calendarType === self::CALENDAR_TYPE_CALENDAR ? 'calendars': 'calendarsubscriptions';
2184
+
2185
+        $query = $this->db->getQueryBuilder();
2186
+        $query->select('synctoken')
2187
+            ->from($table)
2188
+            ->where($query->expr()->eq('id', $query->createNamedParameter($calendarId)));
2189
+        $syncToken = (int)$query->execute()->fetchColumn();
2190
+
2191
+        $query = $this->db->getQueryBuilder();
2192
+        $query->insert('calendarchanges')
2193
+            ->values([
2194
+                'uri' => $query->createNamedParameter($objectUri),
2195
+                'synctoken' => $query->createNamedParameter($syncToken),
2196
+                'calendarid' => $query->createNamedParameter($calendarId),
2197
+                'operation' => $query->createNamedParameter($operation),
2198
+                'calendartype' => $query->createNamedParameter($calendarType),
2199
+            ])
2200
+            ->execute();
2201
+
2202
+        $stmt = $this->db->prepare("UPDATE `*PREFIX*$table` SET `synctoken` = `synctoken` + 1 WHERE `id` = ?");
2203
+        $stmt->execute([
2204
+            $calendarId
2205
+        ]);
2206
+
2207
+    }
2208
+
2209
+    /**
2210
+     * Parses some information from calendar objects, used for optimized
2211
+     * calendar-queries.
2212
+     *
2213
+     * Returns an array with the following keys:
2214
+     *   * etag - An md5 checksum of the object without the quotes.
2215
+     *   * size - Size of the object in bytes
2216
+     *   * componentType - VEVENT, VTODO or VJOURNAL
2217
+     *   * firstOccurence
2218
+     *   * lastOccurence
2219
+     *   * uid - value of the UID property
2220
+     *
2221
+     * @param string $calendarData
2222
+     * @return array
2223
+     */
2224
+    public function getDenormalizedData($calendarData) {
2225
+
2226
+        $vObject = Reader::read($calendarData);
2227
+        $componentType = null;
2228
+        $component = null;
2229
+        $firstOccurrence = null;
2230
+        $lastOccurrence = null;
2231
+        $uid = null;
2232
+        $classification = self::CLASSIFICATION_PUBLIC;
2233
+        foreach($vObject->getComponents() as $component) {
2234
+            if ($component->name!=='VTIMEZONE') {
2235
+                $componentType = $component->name;
2236
+                $uid = (string)$component->UID;
2237
+                break;
2238
+            }
2239
+        }
2240
+        if (!$componentType) {
2241
+            throw new \Sabre\DAV\Exception\BadRequest('Calendar objects must have a VJOURNAL, VEVENT or VTODO component');
2242
+        }
2243
+        if ($componentType === 'VEVENT' && $component->DTSTART) {
2244
+            $firstOccurrence = $component->DTSTART->getDateTime()->getTimeStamp();
2245
+            // Finding the last occurrence is a bit harder
2246
+            if (!isset($component->RRULE)) {
2247
+                if (isset($component->DTEND)) {
2248
+                    $lastOccurrence = $component->DTEND->getDateTime()->getTimeStamp();
2249
+                } elseif (isset($component->DURATION)) {
2250
+                    $endDate = clone $component->DTSTART->getDateTime();
2251
+                    $endDate->add(DateTimeParser::parse($component->DURATION->getValue()));
2252
+                    $lastOccurrence = $endDate->getTimeStamp();
2253
+                } elseif (!$component->DTSTART->hasTime()) {
2254
+                    $endDate = clone $component->DTSTART->getDateTime();
2255
+                    $endDate->modify('+1 day');
2256
+                    $lastOccurrence = $endDate->getTimeStamp();
2257
+                } else {
2258
+                    $lastOccurrence = $firstOccurrence;
2259
+                }
2260
+            } else {
2261
+                $it = new EventIterator($vObject, (string)$component->UID);
2262
+                $maxDate = new DateTime(self::MAX_DATE);
2263
+                if ($it->isInfinite()) {
2264
+                    $lastOccurrence = $maxDate->getTimestamp();
2265
+                } else {
2266
+                    $end = $it->getDtEnd();
2267
+                    while($it->valid() && $end < $maxDate) {
2268
+                        $end = $it->getDtEnd();
2269
+                        $it->next();
2270
+
2271
+                    }
2272
+                    $lastOccurrence = $end->getTimestamp();
2273
+                }
2274
+
2275
+            }
2276
+        }
2277
+
2278
+        if ($component->CLASS) {
2279
+            $classification = CalDavBackend::CLASSIFICATION_PRIVATE;
2280
+            switch ($component->CLASS->getValue()) {
2281
+                case 'PUBLIC':
2282
+                    $classification = CalDavBackend::CLASSIFICATION_PUBLIC;
2283
+                    break;
2284
+                case 'CONFIDENTIAL':
2285
+                    $classification = CalDavBackend::CLASSIFICATION_CONFIDENTIAL;
2286
+                    break;
2287
+            }
2288
+        }
2289
+        return [
2290
+            'etag' => md5($calendarData),
2291
+            'size' => strlen($calendarData),
2292
+            'componentType' => $componentType,
2293
+            'firstOccurence' => is_null($firstOccurrence) ? null : max(0, $firstOccurrence),
2294
+            'lastOccurence'  => $lastOccurrence,
2295
+            'uid' => $uid,
2296
+            'classification' => $classification
2297
+        ];
2298
+
2299
+    }
2300
+
2301
+    /**
2302
+     * @param $cardData
2303
+     * @return bool|string
2304
+     */
2305
+    private function readBlob($cardData) {
2306
+        if (is_resource($cardData)) {
2307
+            return stream_get_contents($cardData);
2308
+        }
2309
+
2310
+        return $cardData;
2311
+    }
2312
+
2313
+    /**
2314
+     * @param IShareable $shareable
2315
+     * @param array $add
2316
+     * @param array $remove
2317
+     */
2318
+    public function updateShares($shareable, $add, $remove) {
2319
+        $calendarId = $shareable->getResourceId();
2320
+        $this->dispatcher->dispatch('\OCA\DAV\CalDAV\CalDavBackend::updateShares', new GenericEvent(
2321
+            '\OCA\DAV\CalDAV\CalDavBackend::updateShares',
2322
+            [
2323
+                'calendarId' => $calendarId,
2324
+                'calendarData' => $this->getCalendarById($calendarId),
2325
+                'shares' => $this->getShares($calendarId),
2326
+                'add' => $add,
2327
+                'remove' => $remove,
2328
+            ]));
2329
+        $this->calendarSharingBackend->updateShares($shareable, $add, $remove);
2330
+    }
2331
+
2332
+    /**
2333
+     * @param int $resourceId
2334
+     * @param int $calendarType
2335
+     * @return array
2336
+     */
2337
+    public function getShares($resourceId, $calendarType=self::CALENDAR_TYPE_CALENDAR) {
2338
+        return $this->calendarSharingBackend->getShares($resourceId);
2339
+    }
2340
+
2341
+    /**
2342
+     * @param boolean $value
2343
+     * @param \OCA\DAV\CalDAV\Calendar $calendar
2344
+     * @return string|null
2345
+     */
2346
+    public function setPublishStatus($value, $calendar) {
2347
+
2348
+        $calendarId = $calendar->getResourceId();
2349
+        $this->dispatcher->dispatch('\OCA\DAV\CalDAV\CalDavBackend::publishCalendar', new GenericEvent(
2350
+            '\OCA\DAV\CalDAV\CalDavBackend::updateShares',
2351
+            [
2352
+                'calendarId' => $calendarId,
2353
+                'calendarData' => $this->getCalendarById($calendarId),
2354
+                'public' => $value,
2355
+            ]));
2356
+
2357
+        $query = $this->db->getQueryBuilder();
2358
+        if ($value) {
2359
+            $publicUri = $this->random->generate(16, ISecureRandom::CHAR_HUMAN_READABLE);
2360
+            $query->insert('dav_shares')
2361
+                ->values([
2362
+                    'principaluri' => $query->createNamedParameter($calendar->getPrincipalURI()),
2363
+                    'type' => $query->createNamedParameter('calendar'),
2364
+                    'access' => $query->createNamedParameter(self::ACCESS_PUBLIC),
2365
+                    'resourceid' => $query->createNamedParameter($calendar->getResourceId()),
2366
+                    'publicuri' => $query->createNamedParameter($publicUri)
2367
+                ]);
2368
+            $query->execute();
2369
+            return $publicUri;
2370
+        }
2371
+        $query->delete('dav_shares')
2372
+            ->where($query->expr()->eq('resourceid', $query->createNamedParameter($calendar->getResourceId())))
2373
+            ->andWhere($query->expr()->eq('access', $query->createNamedParameter(self::ACCESS_PUBLIC)));
2374
+        $query->execute();
2375
+        return null;
2376
+    }
2377
+
2378
+    /**
2379
+     * @param \OCA\DAV\CalDAV\Calendar $calendar
2380
+     * @return mixed
2381
+     */
2382
+    public function getPublishStatus($calendar) {
2383
+        $query = $this->db->getQueryBuilder();
2384
+        $result = $query->select('publicuri')
2385
+            ->from('dav_shares')
2386
+            ->where($query->expr()->eq('resourceid', $query->createNamedParameter($calendar->getResourceId())))
2387
+            ->andWhere($query->expr()->eq('access', $query->createNamedParameter(self::ACCESS_PUBLIC)))
2388
+            ->execute();
2389
+
2390
+        $row = $result->fetch();
2391
+        $result->closeCursor();
2392
+        return $row ? reset($row) : false;
2393
+    }
2394
+
2395
+    /**
2396
+     * @param int $resourceId
2397
+     * @param array $acl
2398
+     * @return array
2399
+     */
2400
+    public function applyShareAcl($resourceId, $acl) {
2401
+        return $this->calendarSharingBackend->applyShareAcl($resourceId, $acl);
2402
+    }
2403
+
2404
+
2405
+
2406
+    /**
2407
+     * update properties table
2408
+     *
2409
+     * @param int $calendarId
2410
+     * @param string $objectUri
2411
+     * @param string $calendarData
2412
+     * @param int $calendarType
2413
+     */
2414
+    public function updateProperties($calendarId, $objectUri, $calendarData, $calendarType=self::CALENDAR_TYPE_CALENDAR) {
2415
+        $objectId = $this->getCalendarObjectId($calendarId, $objectUri, $calendarType);
2416
+
2417
+        try {
2418
+            $vCalendar = $this->readCalendarData($calendarData);
2419
+        } catch (\Exception $ex) {
2420
+            return;
2421
+        }
2422
+
2423
+        $this->purgeProperties($calendarId, $objectId);
2424
+
2425
+        $query = $this->db->getQueryBuilder();
2426
+        $query->insert($this->dbObjectPropertiesTable)
2427
+            ->values(
2428
+                [
2429
+                    'calendarid' => $query->createNamedParameter($calendarId),
2430
+                    'calendartype' => $query->createNamedParameter($calendarType),
2431
+                    'objectid' => $query->createNamedParameter($objectId),
2432
+                    'name' => $query->createParameter('name'),
2433
+                    'parameter' => $query->createParameter('parameter'),
2434
+                    'value' => $query->createParameter('value'),
2435
+                ]
2436
+            );
2437
+
2438
+        $indexComponents = ['VEVENT', 'VJOURNAL', 'VTODO'];
2439
+        foreach ($vCalendar->getComponents() as $component) {
2440
+            if (!in_array($component->name, $indexComponents)) {
2441
+                continue;
2442
+            }
2443
+
2444
+            foreach ($component->children() as $property) {
2445
+                if (in_array($property->name, self::$indexProperties)) {
2446
+                    $value = $property->getValue();
2447
+                    // is this a shitty db?
2448
+                    if (!$this->db->supports4ByteText()) {
2449
+                        $value = preg_replace('/[\x{10000}-\x{10FFFF}]/u', "\xEF\xBF\xBD", $value);
2450
+                    }
2451
+                    $value = mb_substr($value, 0, 254);
2452
+
2453
+                    $query->setParameter('name', $property->name);
2454
+                    $query->setParameter('parameter', null);
2455
+                    $query->setParameter('value', $value);
2456
+                    $query->execute();
2457
+                }
2458
+
2459
+                if (array_key_exists($property->name, self::$indexParameters)) {
2460
+                    $parameters = $property->parameters();
2461
+                    $indexedParametersForProperty = self::$indexParameters[$property->name];
2462
+
2463
+                    foreach ($parameters as $key => $value) {
2464
+                        if (in_array($key, $indexedParametersForProperty)) {
2465
+                            // is this a shitty db?
2466
+                            if ($this->db->supports4ByteText()) {
2467
+                                $value = preg_replace('/[\x{10000}-\x{10FFFF}]/u', "\xEF\xBF\xBD", $value);
2468
+                            }
2469
+
2470
+                            $query->setParameter('name', $property->name);
2471
+                            $query->setParameter('parameter', mb_substr($key, 0, 254));
2472
+                            $query->setParameter('value', mb_substr($value, 0, 254));
2473
+                            $query->execute();
2474
+                        }
2475
+                    }
2476
+                }
2477
+            }
2478
+        }
2479
+    }
2480
+
2481
+    /**
2482
+     * deletes all birthday calendars
2483
+     */
2484
+    public function deleteAllBirthdayCalendars() {
2485
+        $query = $this->db->getQueryBuilder();
2486
+        $result = $query->select(['id'])->from('calendars')
2487
+            ->where($query->expr()->eq('uri', $query->createNamedParameter(BirthdayService::BIRTHDAY_CALENDAR_URI)))
2488
+            ->execute();
2489
+
2490
+        $ids = $result->fetchAll();
2491
+        foreach($ids as $id) {
2492
+            $this->deleteCalendar($id['id']);
2493
+        }
2494
+    }
2495
+
2496
+    /**
2497
+     * @param $subscriptionId
2498
+     */
2499
+    public function purgeAllCachedEventsForSubscription($subscriptionId) {
2500
+        $query = $this->db->getQueryBuilder();
2501
+        $query->select('uri')
2502
+            ->from('calendarobjects')
2503
+            ->where($query->expr()->eq('calendarid', $query->createNamedParameter($subscriptionId)))
2504
+            ->andWhere($query->expr()->eq('calendartype', $query->createNamedParameter(self::CALENDAR_TYPE_SUBSCRIPTION)));
2505
+        $stmt = $query->execute();
2506
+
2507
+        $uris = [];
2508
+        foreach($stmt->fetchAll(\PDO::FETCH_ASSOC) as $row) {
2509
+            $uris[] = $row['uri'];
2510
+        }
2511
+        $stmt->closeCursor();
2512
+
2513
+        $query = $this->db->getQueryBuilder();
2514
+        $query->delete('calendarobjects')
2515
+            ->where($query->expr()->eq('calendarid', $query->createNamedParameter($subscriptionId)))
2516
+            ->andWhere($query->expr()->eq('calendartype', $query->createNamedParameter(self::CALENDAR_TYPE_SUBSCRIPTION)))
2517
+            ->execute();
2518
+
2519
+        $query->delete('calendarchanges')
2520
+            ->where($query->expr()->eq('calendarid', $query->createNamedParameter($subscriptionId)))
2521
+            ->andWhere($query->expr()->eq('calendartype', $query->createNamedParameter(self::CALENDAR_TYPE_SUBSCRIPTION)))
2522
+            ->execute();
2523
+
2524
+        $query->delete($this->dbObjectPropertiesTable)
2525
+            ->where($query->expr()->eq('calendarid', $query->createNamedParameter($subscriptionId)))
2526
+            ->andWhere($query->expr()->eq('calendartype', $query->createNamedParameter(self::CALENDAR_TYPE_SUBSCRIPTION)))
2527
+            ->execute();
2528
+
2529
+        foreach($uris as $uri) {
2530
+            $this->addChange($subscriptionId, $uri, 3, self::CALENDAR_TYPE_SUBSCRIPTION);
2531
+        }
2532
+    }
2533
+
2534
+    /**
2535
+     * Move a calendar from one user to another
2536
+     *
2537
+     * @param string $uriName
2538
+     * @param string $uriOrigin
2539
+     * @param string $uriDestination
2540
+     */
2541
+    public function moveCalendar($uriName, $uriOrigin, $uriDestination)
2542
+    {
2543
+        $query = $this->db->getQueryBuilder();
2544
+        $query->update('calendars')
2545
+            ->set('principaluri', $query->createNamedParameter($uriDestination))
2546
+            ->where($query->expr()->eq('principaluri', $query->createNamedParameter($uriOrigin)))
2547
+            ->andWhere($query->expr()->eq('uri', $query->createNamedParameter($uriName)))
2548
+            ->execute();
2549
+    }
2550
+
2551
+    /**
2552
+     * read VCalendar data into a VCalendar object
2553
+     *
2554
+     * @param string $objectData
2555
+     * @return VCalendar
2556
+     */
2557
+    protected function readCalendarData($objectData) {
2558
+        return Reader::read($objectData);
2559
+    }
2560
+
2561
+    /**
2562
+     * delete all properties from a given calendar object
2563
+     *
2564
+     * @param int $calendarId
2565
+     * @param int $objectId
2566
+     */
2567
+    protected function purgeProperties($calendarId, $objectId) {
2568
+        $query = $this->db->getQueryBuilder();
2569
+        $query->delete($this->dbObjectPropertiesTable)
2570
+            ->where($query->expr()->eq('objectid', $query->createNamedParameter($objectId)))
2571
+            ->andWhere($query->expr()->eq('calendarid', $query->createNamedParameter($calendarId)));
2572
+        $query->execute();
2573
+    }
2574
+
2575
+    /**
2576
+     * get ID from a given calendar object
2577
+     *
2578
+     * @param int $calendarId
2579
+     * @param string $uri
2580
+     * @param int $calendarType
2581
+     * @return int
2582
+     */
2583
+    protected function getCalendarObjectId($calendarId, $uri, $calendarType):int {
2584
+        $query = $this->db->getQueryBuilder();
2585
+        $query->select('id')
2586
+            ->from('calendarobjects')
2587
+            ->where($query->expr()->eq('uri', $query->createNamedParameter($uri)))
2588
+            ->andWhere($query->expr()->eq('calendarid', $query->createNamedParameter($calendarId)))
2589
+            ->andWhere($query->expr()->eq('calendartype', $query->createNamedParameter($calendarType)));
2590
+
2591
+        $result = $query->execute();
2592
+        $objectIds = $result->fetch();
2593
+        $result->closeCursor();
2594
+
2595
+        if (!isset($objectIds['id'])) {
2596
+            throw new \InvalidArgumentException('Calendarobject does not exists: ' . $uri);
2597
+        }
2598
+
2599
+        return (int)$objectIds['id'];
2600
+    }
2601
+
2602
+    /**
2603
+     * return legacy endpoint principal name to new principal name
2604
+     *
2605
+     * @param $principalUri
2606
+     * @param $toV2
2607
+     * @return string
2608
+     */
2609
+    private function convertPrincipal($principalUri, $toV2) {
2610
+        if ($this->principalBackend->getPrincipalPrefix() === 'principals') {
2611
+            list(, $name) = Uri\split($principalUri);
2612
+            if ($toV2 === true) {
2613
+                return "principals/users/$name";
2614
+            }
2615
+            return "principals/$name";
2616
+        }
2617
+        return $principalUri;
2618
+    }
2619
+
2620
+    /**
2621
+     * adds information about an owner to the calendar data
2622
+     *
2623
+     * @param $calendarInfo
2624
+     */
2625
+    private function addOwnerPrincipal(&$calendarInfo) {
2626
+        $ownerPrincipalKey = '{' . \OCA\DAV\DAV\Sharing\Plugin::NS_OWNCLOUD . '}owner-principal';
2627
+        $displaynameKey = '{' . \OCA\DAV\DAV\Sharing\Plugin::NS_NEXTCLOUD . '}owner-displayname';
2628
+        if (isset($calendarInfo[$ownerPrincipalKey])) {
2629
+            $uri = $calendarInfo[$ownerPrincipalKey];
2630
+        } else {
2631
+            $uri = $calendarInfo['principaluri'];
2632
+        }
2633
+
2634
+        $principalInformation = $this->principalBackend->getPrincipalByPath($uri);
2635
+        if (isset($principalInformation['{DAV:}displayname'])) {
2636
+            $calendarInfo[$displaynameKey] = $principalInformation['{DAV:}displayname'];
2637
+        }
2638
+    }
2639 2639
 }
Please login to merge, or discard this patch.
apps/dav/lib/CalDAV/Schedule/IMipPlugin.php 2 patches
Indentation   +537 added lines, -537 removed lines patch added patch discarded remove patch
@@ -67,205 +67,205 @@  discard block
 block discarded – undo
67 67
  */
68 68
 class IMipPlugin extends SabreIMipPlugin {
69 69
 
70
-	/** @var string */
71
-	private $userId;
72
-
73
-	/** @var IConfig */
74
-	private $config;
75
-
76
-	/** @var IMailer */
77
-	private $mailer;
78
-
79
-	/** @var ILogger */
80
-	private $logger;
81
-
82
-	/** @var ITimeFactory */
83
-	private $timeFactory;
84
-
85
-	/** @var L10NFactory */
86
-	private $l10nFactory;
87
-
88
-	/** @var IURLGenerator */
89
-	private $urlGenerator;
90
-
91
-	/** @var ISecureRandom */
92
-	private $random;
93
-
94
-	/** @var IDBConnection */
95
-	private $db;
96
-
97
-	/** @var Defaults */
98
-	private $defaults;
99
-
100
-	/** @var IUserManager */
101
-	private $userManager;
102
-
103
-	const MAX_DATE = '2038-01-01';
104
-
105
-	const METHOD_REQUEST = 'request';
106
-	const METHOD_REPLY = 'reply';
107
-	const METHOD_CANCEL = 'cancel';
108
-
109
-	/**
110
-	 * @param IConfig $config
111
-	 * @param IMailer $mailer
112
-	 * @param ILogger $logger
113
-	 * @param ITimeFactory $timeFactory
114
-	 * @param L10NFactory $l10nFactory
115
-	 * @param IUrlGenerator $urlGenerator
116
-	 * @param Defaults $defaults
117
-	 * @param ISecureRandom $random
118
-	 * @param IDBConnection $db
119
-	 * @param string $userId
120
-	 */
121
-	public function __construct(IConfig $config, IMailer $mailer, ILogger $logger,
122
-								ITimeFactory $timeFactory, L10NFactory $l10nFactory,
123
-								IURLGenerator $urlGenerator, Defaults $defaults,
124
-								ISecureRandom $random, IDBConnection $db, IUserManager $userManager,
125
-								$userId) {
126
-		parent::__construct('');
127
-		$this->userId = $userId;
128
-		$this->config = $config;
129
-		$this->mailer = $mailer;
130
-		$this->logger = $logger;
131
-		$this->timeFactory = $timeFactory;
132
-		$this->l10nFactory = $l10nFactory;
133
-		$this->urlGenerator = $urlGenerator;
134
-		$this->random = $random;
135
-		$this->db = $db;
136
-		$this->defaults = $defaults;
137
-		$this->userManager = $userManager;
138
-	}
139
-
140
-	/**
141
-	 * Event handler for the 'schedule' event.
142
-	 *
143
-	 * @param Message $iTipMessage
144
-	 * @return void
145
-	 */
146
-	public function schedule(Message $iTipMessage) {
147
-
148
-		// Not sending any emails if the system considers the update
149
-		// insignificant.
150
-		if (!$iTipMessage->significantChange) {
151
-			if (!$iTipMessage->scheduleStatus) {
152
-				$iTipMessage->scheduleStatus = '1.0;We got the message, but it\'s not significant enough to warrant an email';
153
-			}
154
-			return;
155
-		}
156
-
157
-		$summary = $iTipMessage->message->VEVENT->SUMMARY;
158
-
159
-		if (parse_url($iTipMessage->sender, PHP_URL_SCHEME) !== 'mailto') {
160
-			return;
161
-		}
162
-
163
-		if (parse_url($iTipMessage->recipient, PHP_URL_SCHEME) !== 'mailto') {
164
-			return;
165
-		}
166
-
167
-		// don't send out mails for events that already took place
168
-		$lastOccurrence = $this->getLastOccurrence($iTipMessage->message);
169
-		$currentTime = $this->timeFactory->getTime();
170
-		if ($lastOccurrence < $currentTime) {
171
-			return;
172
-		}
173
-
174
-		// Strip off mailto:
175
-		$sender = substr($iTipMessage->sender, 7);
176
-		$recipient = substr($iTipMessage->recipient, 7);
177
-
178
-		$senderName = $iTipMessage->senderName ?: null;
179
-		$recipientName = $iTipMessage->recipientName ?: null;
180
-
181
-		if ($senderName === null || empty(trim($senderName))) {
182
-			$user = $this->userManager->get($this->userId);
183
-			if ($user) {
184
-				// getDisplayName automatically uses the uid
185
-				// if no display-name is set
186
-				$senderName = $user->getDisplayName();
187
-			}
188
-		}
189
-
190
-		/** @var VEvent $vevent */
191
-		$vevent = $iTipMessage->message->VEVENT;
192
-
193
-		$attendee = $this->getCurrentAttendee($iTipMessage);
194
-		$defaultLang = $this->l10nFactory->findLanguage();
195
-		$lang = $this->getAttendeeLangOrDefault($defaultLang, $attendee);
196
-		$l10n = $this->l10nFactory->get('dav', $lang);
197
-
198
-		$meetingAttendeeName = $recipientName ?: $recipient;
199
-		$meetingInviteeName = $senderName ?: $sender;
200
-
201
-		$meetingTitle = $vevent->SUMMARY;
202
-		$meetingDescription = $vevent->DESCRIPTION;
203
-
204
-		$start = $vevent->DTSTART;
205
-		if (isset($vevent->DTEND)) {
206
-			$end = $vevent->DTEND;
207
-		} elseif (isset($vevent->DURATION)) {
208
-			$isFloating = $vevent->DTSTART->isFloating();
209
-			$end = clone $vevent->DTSTART;
210
-			$endDateTime = $end->getDateTime();
211
-			$endDateTime = $endDateTime->add(DateTimeParser::parse($vevent->DURATION->getValue()));
212
-			$end->setDateTime($endDateTime, $isFloating);
213
-		} elseif (!$vevent->DTSTART->hasTime()) {
214
-			$isFloating = $vevent->DTSTART->isFloating();
215
-			$end = clone $vevent->DTSTART;
216
-			$endDateTime = $end->getDateTime();
217
-			$endDateTime = $endDateTime->modify('+1 day');
218
-			$end->setDateTime($endDateTime, $isFloating);
219
-		} else {
220
-			$end = clone $vevent->DTSTART;
221
-		}
222
-
223
-		$meetingWhen = $this->generateWhenString($l10n, $start, $end);
224
-
225
-		$meetingUrl = $vevent->URL;
226
-		$meetingLocation = $vevent->LOCATION;
227
-
228
-		$defaultVal = '--';
229
-
230
-		$method = self::METHOD_REQUEST;
231
-		switch (strtolower($iTipMessage->method)) {
232
-			case self::METHOD_REPLY:
233
-				$method = self::METHOD_REPLY;
234
-				break;
235
-			case self::METHOD_CANCEL:
236
-				$method = self::METHOD_CANCEL;
237
-				break;
238
-		}
239
-
240
-		$data = [
241
-			'attendee_name' => (string)$meetingAttendeeName ?: $defaultVal,
242
-			'invitee_name' => (string)$meetingInviteeName ?: $defaultVal,
243
-			'meeting_title' => (string)$meetingTitle ?: $defaultVal,
244
-			'meeting_description' => (string)$meetingDescription ?: $defaultVal,
245
-			'meeting_url' => (string)$meetingUrl ?: $defaultVal,
246
-		];
247
-
248
-		$fromEMail = \OCP\Util::getDefaultEmailAddress('invitations-noreply');
249
-		$fromName = $l10n->t('%1$s via %2$s', [$senderName, $this->defaults->getName()]);
250
-
251
-		$message = $this->mailer->createMessage()
252
-			->setFrom([$fromEMail => $fromName])
253
-			->setReplyTo([$sender => $senderName])
254
-			->setTo([$recipient => $recipientName]);
255
-
256
-		$template = $this->mailer->createEMailTemplate('dav.calendarInvite.' . $method, $data);
257
-		$template->addHeader();
258
-
259
-		$this->addSubjectAndHeading($template, $l10n, $method, $summary,
260
-			$meetingAttendeeName, $meetingInviteeName);
261
-		$this->addBulletList($template, $l10n, $meetingWhen, $meetingLocation,
262
-			$meetingDescription, $meetingUrl);
263
-
264
-
265
-		// Only add response buttons to invitation requests: Fix Issue #11230
266
-		if (($method == self::METHOD_REQUEST) && $this->getAttendeeRSVP($attendee)) {
267
-
268
-			/*
70
+    /** @var string */
71
+    private $userId;
72
+
73
+    /** @var IConfig */
74
+    private $config;
75
+
76
+    /** @var IMailer */
77
+    private $mailer;
78
+
79
+    /** @var ILogger */
80
+    private $logger;
81
+
82
+    /** @var ITimeFactory */
83
+    private $timeFactory;
84
+
85
+    /** @var L10NFactory */
86
+    private $l10nFactory;
87
+
88
+    /** @var IURLGenerator */
89
+    private $urlGenerator;
90
+
91
+    /** @var ISecureRandom */
92
+    private $random;
93
+
94
+    /** @var IDBConnection */
95
+    private $db;
96
+
97
+    /** @var Defaults */
98
+    private $defaults;
99
+
100
+    /** @var IUserManager */
101
+    private $userManager;
102
+
103
+    const MAX_DATE = '2038-01-01';
104
+
105
+    const METHOD_REQUEST = 'request';
106
+    const METHOD_REPLY = 'reply';
107
+    const METHOD_CANCEL = 'cancel';
108
+
109
+    /**
110
+     * @param IConfig $config
111
+     * @param IMailer $mailer
112
+     * @param ILogger $logger
113
+     * @param ITimeFactory $timeFactory
114
+     * @param L10NFactory $l10nFactory
115
+     * @param IUrlGenerator $urlGenerator
116
+     * @param Defaults $defaults
117
+     * @param ISecureRandom $random
118
+     * @param IDBConnection $db
119
+     * @param string $userId
120
+     */
121
+    public function __construct(IConfig $config, IMailer $mailer, ILogger $logger,
122
+                                ITimeFactory $timeFactory, L10NFactory $l10nFactory,
123
+                                IURLGenerator $urlGenerator, Defaults $defaults,
124
+                                ISecureRandom $random, IDBConnection $db, IUserManager $userManager,
125
+                                $userId) {
126
+        parent::__construct('');
127
+        $this->userId = $userId;
128
+        $this->config = $config;
129
+        $this->mailer = $mailer;
130
+        $this->logger = $logger;
131
+        $this->timeFactory = $timeFactory;
132
+        $this->l10nFactory = $l10nFactory;
133
+        $this->urlGenerator = $urlGenerator;
134
+        $this->random = $random;
135
+        $this->db = $db;
136
+        $this->defaults = $defaults;
137
+        $this->userManager = $userManager;
138
+    }
139
+
140
+    /**
141
+     * Event handler for the 'schedule' event.
142
+     *
143
+     * @param Message $iTipMessage
144
+     * @return void
145
+     */
146
+    public function schedule(Message $iTipMessage) {
147
+
148
+        // Not sending any emails if the system considers the update
149
+        // insignificant.
150
+        if (!$iTipMessage->significantChange) {
151
+            if (!$iTipMessage->scheduleStatus) {
152
+                $iTipMessage->scheduleStatus = '1.0;We got the message, but it\'s not significant enough to warrant an email';
153
+            }
154
+            return;
155
+        }
156
+
157
+        $summary = $iTipMessage->message->VEVENT->SUMMARY;
158
+
159
+        if (parse_url($iTipMessage->sender, PHP_URL_SCHEME) !== 'mailto') {
160
+            return;
161
+        }
162
+
163
+        if (parse_url($iTipMessage->recipient, PHP_URL_SCHEME) !== 'mailto') {
164
+            return;
165
+        }
166
+
167
+        // don't send out mails for events that already took place
168
+        $lastOccurrence = $this->getLastOccurrence($iTipMessage->message);
169
+        $currentTime = $this->timeFactory->getTime();
170
+        if ($lastOccurrence < $currentTime) {
171
+            return;
172
+        }
173
+
174
+        // Strip off mailto:
175
+        $sender = substr($iTipMessage->sender, 7);
176
+        $recipient = substr($iTipMessage->recipient, 7);
177
+
178
+        $senderName = $iTipMessage->senderName ?: null;
179
+        $recipientName = $iTipMessage->recipientName ?: null;
180
+
181
+        if ($senderName === null || empty(trim($senderName))) {
182
+            $user = $this->userManager->get($this->userId);
183
+            if ($user) {
184
+                // getDisplayName automatically uses the uid
185
+                // if no display-name is set
186
+                $senderName = $user->getDisplayName();
187
+            }
188
+        }
189
+
190
+        /** @var VEvent $vevent */
191
+        $vevent = $iTipMessage->message->VEVENT;
192
+
193
+        $attendee = $this->getCurrentAttendee($iTipMessage);
194
+        $defaultLang = $this->l10nFactory->findLanguage();
195
+        $lang = $this->getAttendeeLangOrDefault($defaultLang, $attendee);
196
+        $l10n = $this->l10nFactory->get('dav', $lang);
197
+
198
+        $meetingAttendeeName = $recipientName ?: $recipient;
199
+        $meetingInviteeName = $senderName ?: $sender;
200
+
201
+        $meetingTitle = $vevent->SUMMARY;
202
+        $meetingDescription = $vevent->DESCRIPTION;
203
+
204
+        $start = $vevent->DTSTART;
205
+        if (isset($vevent->DTEND)) {
206
+            $end = $vevent->DTEND;
207
+        } elseif (isset($vevent->DURATION)) {
208
+            $isFloating = $vevent->DTSTART->isFloating();
209
+            $end = clone $vevent->DTSTART;
210
+            $endDateTime = $end->getDateTime();
211
+            $endDateTime = $endDateTime->add(DateTimeParser::parse($vevent->DURATION->getValue()));
212
+            $end->setDateTime($endDateTime, $isFloating);
213
+        } elseif (!$vevent->DTSTART->hasTime()) {
214
+            $isFloating = $vevent->DTSTART->isFloating();
215
+            $end = clone $vevent->DTSTART;
216
+            $endDateTime = $end->getDateTime();
217
+            $endDateTime = $endDateTime->modify('+1 day');
218
+            $end->setDateTime($endDateTime, $isFloating);
219
+        } else {
220
+            $end = clone $vevent->DTSTART;
221
+        }
222
+
223
+        $meetingWhen = $this->generateWhenString($l10n, $start, $end);
224
+
225
+        $meetingUrl = $vevent->URL;
226
+        $meetingLocation = $vevent->LOCATION;
227
+
228
+        $defaultVal = '--';
229
+
230
+        $method = self::METHOD_REQUEST;
231
+        switch (strtolower($iTipMessage->method)) {
232
+            case self::METHOD_REPLY:
233
+                $method = self::METHOD_REPLY;
234
+                break;
235
+            case self::METHOD_CANCEL:
236
+                $method = self::METHOD_CANCEL;
237
+                break;
238
+        }
239
+
240
+        $data = [
241
+            'attendee_name' => (string)$meetingAttendeeName ?: $defaultVal,
242
+            'invitee_name' => (string)$meetingInviteeName ?: $defaultVal,
243
+            'meeting_title' => (string)$meetingTitle ?: $defaultVal,
244
+            'meeting_description' => (string)$meetingDescription ?: $defaultVal,
245
+            'meeting_url' => (string)$meetingUrl ?: $defaultVal,
246
+        ];
247
+
248
+        $fromEMail = \OCP\Util::getDefaultEmailAddress('invitations-noreply');
249
+        $fromName = $l10n->t('%1$s via %2$s', [$senderName, $this->defaults->getName()]);
250
+
251
+        $message = $this->mailer->createMessage()
252
+            ->setFrom([$fromEMail => $fromName])
253
+            ->setReplyTo([$sender => $senderName])
254
+            ->setTo([$recipient => $recipientName]);
255
+
256
+        $template = $this->mailer->createEMailTemplate('dav.calendarInvite.' . $method, $data);
257
+        $template->addHeader();
258
+
259
+        $this->addSubjectAndHeading($template, $l10n, $method, $summary,
260
+            $meetingAttendeeName, $meetingInviteeName);
261
+        $this->addBulletList($template, $l10n, $meetingWhen, $meetingLocation,
262
+            $meetingDescription, $meetingUrl);
263
+
264
+
265
+        // Only add response buttons to invitation requests: Fix Issue #11230
266
+        if (($method == self::METHOD_REQUEST) && $this->getAttendeeRSVP($attendee)) {
267
+
268
+            /*
269 269
 			** Only offer invitation accept/reject buttons, which link back to the
270 270
 			** nextcloud server, to recipients who can access the nextcloud server via
271 271
 			** their internet/intranet.  Issue #12156
@@ -284,342 +284,342 @@  discard block
 block discarded – undo
284 284
 			** To suppress URLs entirely, set invitation_link_recipients to boolean "no".
285 285
 			*/
286 286
 
287
-			$recipientDomain = substr(strrchr($recipient, "@"), 1);
288
-			$invitationLinkRecipients = explode(',', preg_replace('/\s+/', '', strtolower($this->config->getAppValue('dav', 'invitation_link_recipients', 'yes'))));
289
-
290
-			if (strcmp('yes', $invitationLinkRecipients[0]) === 0
291
-				 || in_array(strtolower($recipient), $invitationLinkRecipients)
292
-				 || in_array(strtolower($recipientDomain), $invitationLinkRecipients)) {
293
-				$this->addResponseButtons($template, $l10n, $iTipMessage, $lastOccurrence);
294
-			}
295
-		}
296
-
297
-		$template->addFooter();
298
-
299
-		$message->useTemplate($template);
300
-
301
-		$attachment = $this->mailer->createAttachment(
302
-			$iTipMessage->message->serialize(),
303
-			'event.ics',// TODO(leon): Make file name unique, e.g. add event id
304
-			'text/calendar; method=' . $iTipMessage->method
305
-		);
306
-		$message->attach($attachment);
307
-
308
-		try {
309
-			$failed = $this->mailer->send($message);
310
-			$iTipMessage->scheduleStatus = '1.1; Scheduling message is sent via iMip';
311
-			if ($failed) {
312
-				$this->logger->error('Unable to deliver message to {failed}', ['app' => 'dav', 'failed' =>  implode(', ', $failed)]);
313
-				$iTipMessage->scheduleStatus = '5.0; EMail delivery failed';
314
-			}
315
-		} catch(\Exception $ex) {
316
-			$this->logger->logException($ex, ['app' => 'dav']);
317
-			$iTipMessage->scheduleStatus = '5.0; EMail delivery failed';
318
-		}
319
-	}
320
-
321
-	/**
322
-	 * check if event took place in the past already
323
-	 * @param VCalendar $vObject
324
-	 * @return int
325
-	 */
326
-	private function getLastOccurrence(VCalendar $vObject) {
327
-		/** @var VEvent $component */
328
-		$component = $vObject->VEVENT;
329
-
330
-		$firstOccurrence = $component->DTSTART->getDateTime()->getTimeStamp();
331
-		// Finding the last occurrence is a bit harder
332
-		if (!isset($component->RRULE)) {
333
-			if (isset($component->DTEND)) {
334
-				$lastOccurrence = $component->DTEND->getDateTime()->getTimeStamp();
335
-			} elseif (isset($component->DURATION)) {
336
-				/** @var \DateTime $endDate */
337
-				$endDate = clone $component->DTSTART->getDateTime();
338
-				// $component->DTEND->getDateTime() returns DateTimeImmutable
339
-				$endDate = $endDate->add(DateTimeParser::parse($component->DURATION->getValue()));
340
-				$lastOccurrence = $endDate->getTimestamp();
341
-			} elseif (!$component->DTSTART->hasTime()) {
342
-				/** @var \DateTime $endDate */
343
-				$endDate = clone $component->DTSTART->getDateTime();
344
-				// $component->DTSTART->getDateTime() returns DateTimeImmutable
345
-				$endDate = $endDate->modify('+1 day');
346
-				$lastOccurrence = $endDate->getTimestamp();
347
-			} else {
348
-				$lastOccurrence = $firstOccurrence;
349
-			}
350
-		} else {
351
-			$it = new EventIterator($vObject, (string)$component->UID);
352
-			$maxDate = new \DateTime(self::MAX_DATE);
353
-			if ($it->isInfinite()) {
354
-				$lastOccurrence = $maxDate->getTimestamp();
355
-			} else {
356
-				$end = $it->getDtEnd();
357
-				while($it->valid() && $end < $maxDate) {
358
-					$end = $it->getDtEnd();
359
-					$it->next();
360
-
361
-				}
362
-				$lastOccurrence = $end->getTimestamp();
363
-			}
364
-		}
365
-
366
-		return $lastOccurrence;
367
-	}
368
-
369
-
370
-	/**
371
-	 * @param Message $iTipMessage
372
-	 * @return null|Property
373
-	 */
374
-	private function getCurrentAttendee(Message $iTipMessage) {
375
-		/** @var VEvent $vevent */
376
-		$vevent = $iTipMessage->message->VEVENT;
377
-		$attendees = $vevent->select('ATTENDEE');
378
-		foreach ($attendees as $attendee) {
379
-			/** @var Property $attendee */
380
-			if (strcasecmp($attendee->getValue(), $iTipMessage->recipient) === 0) {
381
-				return $attendee;
382
-			}
383
-		}
384
-		return null;
385
-	}
386
-
387
-	/**
388
-	 * @param string $default
389
-	 * @param Property|null $attendee
390
-	 * @return string
391
-	 */
392
-	private function getAttendeeLangOrDefault($default, Property $attendee = null) {
393
-		if ($attendee !== null) {
394
-			$lang = $attendee->offsetGet('LANGUAGE');
395
-			if ($lang instanceof Parameter) {
396
-				return $lang->getValue();
397
-			}
398
-		}
399
-		return $default;
400
-	}
401
-
402
-	/**
403
-	 * @param Property|null $attendee
404
-	 * @return bool
405
-	 */
406
-	private function getAttendeeRSVP(Property $attendee = null) {
407
-		if ($attendee !== null) {
408
-			$rsvp = $attendee->offsetGet('RSVP');
409
-			if (($rsvp instanceof Parameter) && (strcasecmp($rsvp->getValue(), 'TRUE') === 0)) {
410
-				return true;
411
-			}
412
-		}
413
-		// RFC 5545 3.2.17: default RSVP is false
414
-		return false;
415
-	}
416
-
417
-	/**
418
-	 * @param IL10N $l10n
419
-	 * @param Property $dtstart
420
-	 * @param Property $dtend
421
-	 */
422
-	private function generateWhenString(IL10N $l10n, Property $dtstart, Property $dtend) {
423
-		$isAllDay = $dtstart instanceof Property\ICalendar\Date;
424
-
425
-		/** @var Property\ICalendar\Date | Property\ICalendar\DateTime $dtstart */
426
-		/** @var Property\ICalendar\Date | Property\ICalendar\DateTime $dtend */
427
-		/** @var \DateTimeImmutable $dtstartDt */
428
-		$dtstartDt = $dtstart->getDateTime();
429
-		/** @var \DateTimeImmutable $dtendDt */
430
-		$dtendDt = $dtend->getDateTime();
431
-
432
-		$diff = $dtstartDt->diff($dtendDt);
433
-
434
-		$dtstartDt = new \DateTime($dtstartDt->format(\DateTime::ATOM));
435
-		$dtendDt = new \DateTime($dtendDt->format(\DateTime::ATOM));
436
-
437
-		if ($isAllDay) {
438
-			// One day event
439
-			if ($diff->days === 1) {
440
-				return $l10n->l('date', $dtstartDt, ['width' => 'medium']);
441
-			}
442
-
443
-			// DTEND is exclusive, so if the ics data says 2020-01-01 to 2020-01-05,
444
-			// the email should show 2020-01-01 to 2020-01-04.
445
-			$dtendDt->modify('-1 day');
446
-
447
-			//event that spans over multiple days
448
-			$localeStart = $l10n->l('date', $dtstartDt, ['width' => 'medium']);
449
-			$localeEnd = $l10n->l('date', $dtendDt, ['width' => 'medium']);
450
-
451
-			return $localeStart . ' - ' . $localeEnd;
452
-		}
453
-
454
-		/** @var Property\ICalendar\DateTime $dtstart */
455
-		/** @var Property\ICalendar\DateTime $dtend */
456
-		$isFloating = $dtstart->isFloating();
457
-		$startTimezone = $endTimezone = null;
458
-		if (!$isFloating) {
459
-			$prop = $dtstart->offsetGet('TZID');
460
-			if ($prop instanceof Parameter) {
461
-				$startTimezone = $prop->getValue();
462
-			}
463
-
464
-			$prop = $dtend->offsetGet('TZID');
465
-			if ($prop instanceof Parameter) {
466
-				$endTimezone = $prop->getValue();
467
-			}
468
-		}
469
-
470
-		$localeStart = $l10n->l('weekdayName', $dtstartDt, ['width' => 'abbreviated']) . ', ' .
471
-			$l10n->l('datetime', $dtstartDt, ['width' => 'medium|short']);
472
-
473
-		// always show full date with timezone if timezones are different
474
-		if ($startTimezone !== $endTimezone) {
475
-			$localeEnd = $l10n->l('datetime', $dtendDt, ['width' => 'medium|short']);
476
-
477
-			return $localeStart . ' (' . $startTimezone . ') - ' .
478
-				$localeEnd . ' (' . $endTimezone . ')';
479
-		}
480
-
481
-		// show only end time if date is the same
482
-		if ($this->isDayEqual($dtstartDt, $dtendDt)) {
483
-			$localeEnd = $l10n->l('time', $dtendDt, ['width' => 'short']);
484
-		} else {
485
-			$localeEnd = $l10n->l('weekdayName', $dtendDt, ['width' => 'abbreviated']) . ', ' .
486
-				$l10n->l('datetime', $dtendDt, ['width' => 'medium|short']);
487
-		}
488
-
489
-		return  $localeStart . ' - ' . $localeEnd . ' (' . $startTimezone . ')';
490
-	}
491
-
492
-	/**
493
-	 * @param \DateTime $dtStart
494
-	 * @param \DateTime $dtEnd
495
-	 * @return bool
496
-	 */
497
-	private function isDayEqual(\DateTime $dtStart, \DateTime $dtEnd) {
498
-		return $dtStart->format('Y-m-d') === $dtEnd->format('Y-m-d');
499
-	}
500
-
501
-	/**
502
-	 * @param IEMailTemplate $template
503
-	 * @param IL10N $l10n
504
-	 * @param string $method
505
-	 * @param string $summary
506
-	 * @param string $attendeeName
507
-	 * @param string $inviteeName
508
-	 */
509
-	private function addSubjectAndHeading(IEMailTemplate $template, IL10N $l10n,
510
-										  $method, $summary, $attendeeName, $inviteeName) {
511
-		if ($method === self::METHOD_CANCEL) {
512
-			$template->setSubject('Cancelled: ' . $summary);
513
-			$template->addHeading($l10n->t('Invitation canceled'), $l10n->t('Hello %s,', [$attendeeName]));
514
-			$template->addBodyText($l10n->t('The meeting »%1$s« with %2$s was canceled.', [$summary, $inviteeName]));
515
-		} else if ($method === self::METHOD_REPLY) {
516
-			$template->setSubject('Re: ' . $summary);
517
-			$template->addHeading($l10n->t('Invitation updated'), $l10n->t('Hello %s,', [$attendeeName]));
518
-			$template->addBodyText($l10n->t('The meeting »%1$s« with %2$s was updated.', [$summary, $inviteeName]));
519
-		} else {
520
-			$template->setSubject('Invitation: ' . $summary);
521
-			$template->addHeading($l10n->t('%1$s invited you to »%2$s«', [$inviteeName, $summary]), $l10n->t('Hello %s,', [$attendeeName]));
522
-		}
523
-	}
524
-
525
-	/**
526
-	 * @param IEMailTemplate $template
527
-	 * @param IL10N $l10n
528
-	 * @param string $time
529
-	 * @param string $location
530
-	 * @param string $description
531
-	 * @param string $url
532
-	 */
533
-	private function addBulletList(IEMailTemplate $template, IL10N $l10n, $time, $location, $description, $url) {
534
-		$template->addBodyListItem($time, $l10n->t('When:'),
535
-			$this->getAbsoluteImagePath('filetypes/text-calendar.svg'));
536
-
537
-		if ($location) {
538
-			$template->addBodyListItem($location, $l10n->t('Where:'),
539
-				$this->getAbsoluteImagePath('filetypes/location.svg'));
540
-		}
541
-		if ($description) {
542
-			$template->addBodyListItem((string)$description, $l10n->t('Description:'),
543
-				$this->getAbsoluteImagePath('filetypes/text.svg'));
544
-		}
545
-		if ($url) {
546
-			$template->addBodyListItem((string)$url, $l10n->t('Link:'),
547
-				$this->getAbsoluteImagePath('filetypes/link.svg'));
548
-		}
549
-	}
550
-
551
-	/**
552
-	 * @param IEMailTemplate $template
553
-	 * @param IL10N $l10n
554
-	 * @param Message $iTipMessage
555
-	 * @param int $lastOccurrence
556
-	 */
557
-	private function addResponseButtons(IEMailTemplate $template, IL10N $l10n,
558
-										Message $iTipMessage, $lastOccurrence) {
559
-		$token = $this->createInvitationToken($iTipMessage, $lastOccurrence);
560
-
561
-		$template->addBodyButtonGroup(
562
-			$l10n->t('Accept'),
563
-			$this->urlGenerator->linkToRouteAbsolute('dav.invitation_response.accept', [
564
-				'token' => $token,
565
-			]),
566
-			$l10n->t('Decline'),
567
-			$this->urlGenerator->linkToRouteAbsolute('dav.invitation_response.decline', [
568
-				'token' => $token,
569
-			])
570
-		);
571
-
572
-		$moreOptionsURL = $this->urlGenerator->linkToRouteAbsolute('dav.invitation_response.options', [
573
-			'token' => $token,
574
-		]);
575
-		$html = vsprintf('<small><a href="%s">%s</a></small>', [
576
-			$moreOptionsURL, $l10n->t('More options …')
577
-		]);
578
-		$text = $l10n->t('More options at %s', [$moreOptionsURL]);
579
-
580
-		$template->addBodyText($html, $text);
581
-	}
582
-
583
-	/**
584
-	 * @param string $path
585
-	 * @return string
586
-	 */
587
-	private function getAbsoluteImagePath($path) {
588
-		return $this->urlGenerator->getAbsoluteURL(
589
-			$this->urlGenerator->imagePath('core', $path)
590
-		);
591
-	}
592
-
593
-	/**
594
-	 * @param Message $iTipMessage
595
-	 * @param int $lastOccurrence
596
-	 * @return string
597
-	 */
598
-	private function createInvitationToken(Message $iTipMessage, $lastOccurrence):string {
599
-		$token = $this->random->generate(60, ISecureRandom::CHAR_UPPER . ISecureRandom::CHAR_LOWER . ISecureRandom::CHAR_DIGITS);
600
-
601
-		/** @var VEvent $vevent */
602
-		$vevent = $iTipMessage->message->VEVENT;
603
-		$attendee = $iTipMessage->recipient;
604
-		$organizer = $iTipMessage->sender;
605
-		$sequence = $iTipMessage->sequence;
606
-		$recurrenceId = isset($vevent->{'RECURRENCE-ID'}) ?
607
-			$vevent->{'RECURRENCE-ID'}->serialize() : null;
608
-		$uid = $vevent->{'UID'};
609
-
610
-		$query = $this->db->getQueryBuilder();
611
-		$query->insert('calendar_invitations')
612
-			->values([
613
-				'token' => $query->createNamedParameter($token),
614
-				'attendee' => $query->createNamedParameter($attendee),
615
-				'organizer' => $query->createNamedParameter($organizer),
616
-				'sequence' => $query->createNamedParameter($sequence),
617
-				'recurrenceid' => $query->createNamedParameter($recurrenceId),
618
-				'expiration' => $query->createNamedParameter($lastOccurrence),
619
-				'uid' => $query->createNamedParameter($uid)
620
-			])
621
-			->execute();
622
-
623
-		return $token;
624
-	}
287
+            $recipientDomain = substr(strrchr($recipient, "@"), 1);
288
+            $invitationLinkRecipients = explode(',', preg_replace('/\s+/', '', strtolower($this->config->getAppValue('dav', 'invitation_link_recipients', 'yes'))));
289
+
290
+            if (strcmp('yes', $invitationLinkRecipients[0]) === 0
291
+                 || in_array(strtolower($recipient), $invitationLinkRecipients)
292
+                 || in_array(strtolower($recipientDomain), $invitationLinkRecipients)) {
293
+                $this->addResponseButtons($template, $l10n, $iTipMessage, $lastOccurrence);
294
+            }
295
+        }
296
+
297
+        $template->addFooter();
298
+
299
+        $message->useTemplate($template);
300
+
301
+        $attachment = $this->mailer->createAttachment(
302
+            $iTipMessage->message->serialize(),
303
+            'event.ics',// TODO(leon): Make file name unique, e.g. add event id
304
+            'text/calendar; method=' . $iTipMessage->method
305
+        );
306
+        $message->attach($attachment);
307
+
308
+        try {
309
+            $failed = $this->mailer->send($message);
310
+            $iTipMessage->scheduleStatus = '1.1; Scheduling message is sent via iMip';
311
+            if ($failed) {
312
+                $this->logger->error('Unable to deliver message to {failed}', ['app' => 'dav', 'failed' =>  implode(', ', $failed)]);
313
+                $iTipMessage->scheduleStatus = '5.0; EMail delivery failed';
314
+            }
315
+        } catch(\Exception $ex) {
316
+            $this->logger->logException($ex, ['app' => 'dav']);
317
+            $iTipMessage->scheduleStatus = '5.0; EMail delivery failed';
318
+        }
319
+    }
320
+
321
+    /**
322
+     * check if event took place in the past already
323
+     * @param VCalendar $vObject
324
+     * @return int
325
+     */
326
+    private function getLastOccurrence(VCalendar $vObject) {
327
+        /** @var VEvent $component */
328
+        $component = $vObject->VEVENT;
329
+
330
+        $firstOccurrence = $component->DTSTART->getDateTime()->getTimeStamp();
331
+        // Finding the last occurrence is a bit harder
332
+        if (!isset($component->RRULE)) {
333
+            if (isset($component->DTEND)) {
334
+                $lastOccurrence = $component->DTEND->getDateTime()->getTimeStamp();
335
+            } elseif (isset($component->DURATION)) {
336
+                /** @var \DateTime $endDate */
337
+                $endDate = clone $component->DTSTART->getDateTime();
338
+                // $component->DTEND->getDateTime() returns DateTimeImmutable
339
+                $endDate = $endDate->add(DateTimeParser::parse($component->DURATION->getValue()));
340
+                $lastOccurrence = $endDate->getTimestamp();
341
+            } elseif (!$component->DTSTART->hasTime()) {
342
+                /** @var \DateTime $endDate */
343
+                $endDate = clone $component->DTSTART->getDateTime();
344
+                // $component->DTSTART->getDateTime() returns DateTimeImmutable
345
+                $endDate = $endDate->modify('+1 day');
346
+                $lastOccurrence = $endDate->getTimestamp();
347
+            } else {
348
+                $lastOccurrence = $firstOccurrence;
349
+            }
350
+        } else {
351
+            $it = new EventIterator($vObject, (string)$component->UID);
352
+            $maxDate = new \DateTime(self::MAX_DATE);
353
+            if ($it->isInfinite()) {
354
+                $lastOccurrence = $maxDate->getTimestamp();
355
+            } else {
356
+                $end = $it->getDtEnd();
357
+                while($it->valid() && $end < $maxDate) {
358
+                    $end = $it->getDtEnd();
359
+                    $it->next();
360
+
361
+                }
362
+                $lastOccurrence = $end->getTimestamp();
363
+            }
364
+        }
365
+
366
+        return $lastOccurrence;
367
+    }
368
+
369
+
370
+    /**
371
+     * @param Message $iTipMessage
372
+     * @return null|Property
373
+     */
374
+    private function getCurrentAttendee(Message $iTipMessage) {
375
+        /** @var VEvent $vevent */
376
+        $vevent = $iTipMessage->message->VEVENT;
377
+        $attendees = $vevent->select('ATTENDEE');
378
+        foreach ($attendees as $attendee) {
379
+            /** @var Property $attendee */
380
+            if (strcasecmp($attendee->getValue(), $iTipMessage->recipient) === 0) {
381
+                return $attendee;
382
+            }
383
+        }
384
+        return null;
385
+    }
386
+
387
+    /**
388
+     * @param string $default
389
+     * @param Property|null $attendee
390
+     * @return string
391
+     */
392
+    private function getAttendeeLangOrDefault($default, Property $attendee = null) {
393
+        if ($attendee !== null) {
394
+            $lang = $attendee->offsetGet('LANGUAGE');
395
+            if ($lang instanceof Parameter) {
396
+                return $lang->getValue();
397
+            }
398
+        }
399
+        return $default;
400
+    }
401
+
402
+    /**
403
+     * @param Property|null $attendee
404
+     * @return bool
405
+     */
406
+    private function getAttendeeRSVP(Property $attendee = null) {
407
+        if ($attendee !== null) {
408
+            $rsvp = $attendee->offsetGet('RSVP');
409
+            if (($rsvp instanceof Parameter) && (strcasecmp($rsvp->getValue(), 'TRUE') === 0)) {
410
+                return true;
411
+            }
412
+        }
413
+        // RFC 5545 3.2.17: default RSVP is false
414
+        return false;
415
+    }
416
+
417
+    /**
418
+     * @param IL10N $l10n
419
+     * @param Property $dtstart
420
+     * @param Property $dtend
421
+     */
422
+    private function generateWhenString(IL10N $l10n, Property $dtstart, Property $dtend) {
423
+        $isAllDay = $dtstart instanceof Property\ICalendar\Date;
424
+
425
+        /** @var Property\ICalendar\Date | Property\ICalendar\DateTime $dtstart */
426
+        /** @var Property\ICalendar\Date | Property\ICalendar\DateTime $dtend */
427
+        /** @var \DateTimeImmutable $dtstartDt */
428
+        $dtstartDt = $dtstart->getDateTime();
429
+        /** @var \DateTimeImmutable $dtendDt */
430
+        $dtendDt = $dtend->getDateTime();
431
+
432
+        $diff = $dtstartDt->diff($dtendDt);
433
+
434
+        $dtstartDt = new \DateTime($dtstartDt->format(\DateTime::ATOM));
435
+        $dtendDt = new \DateTime($dtendDt->format(\DateTime::ATOM));
436
+
437
+        if ($isAllDay) {
438
+            // One day event
439
+            if ($diff->days === 1) {
440
+                return $l10n->l('date', $dtstartDt, ['width' => 'medium']);
441
+            }
442
+
443
+            // DTEND is exclusive, so if the ics data says 2020-01-01 to 2020-01-05,
444
+            // the email should show 2020-01-01 to 2020-01-04.
445
+            $dtendDt->modify('-1 day');
446
+
447
+            //event that spans over multiple days
448
+            $localeStart = $l10n->l('date', $dtstartDt, ['width' => 'medium']);
449
+            $localeEnd = $l10n->l('date', $dtendDt, ['width' => 'medium']);
450
+
451
+            return $localeStart . ' - ' . $localeEnd;
452
+        }
453
+
454
+        /** @var Property\ICalendar\DateTime $dtstart */
455
+        /** @var Property\ICalendar\DateTime $dtend */
456
+        $isFloating = $dtstart->isFloating();
457
+        $startTimezone = $endTimezone = null;
458
+        if (!$isFloating) {
459
+            $prop = $dtstart->offsetGet('TZID');
460
+            if ($prop instanceof Parameter) {
461
+                $startTimezone = $prop->getValue();
462
+            }
463
+
464
+            $prop = $dtend->offsetGet('TZID');
465
+            if ($prop instanceof Parameter) {
466
+                $endTimezone = $prop->getValue();
467
+            }
468
+        }
469
+
470
+        $localeStart = $l10n->l('weekdayName', $dtstartDt, ['width' => 'abbreviated']) . ', ' .
471
+            $l10n->l('datetime', $dtstartDt, ['width' => 'medium|short']);
472
+
473
+        // always show full date with timezone if timezones are different
474
+        if ($startTimezone !== $endTimezone) {
475
+            $localeEnd = $l10n->l('datetime', $dtendDt, ['width' => 'medium|short']);
476
+
477
+            return $localeStart . ' (' . $startTimezone . ') - ' .
478
+                $localeEnd . ' (' . $endTimezone . ')';
479
+        }
480
+
481
+        // show only end time if date is the same
482
+        if ($this->isDayEqual($dtstartDt, $dtendDt)) {
483
+            $localeEnd = $l10n->l('time', $dtendDt, ['width' => 'short']);
484
+        } else {
485
+            $localeEnd = $l10n->l('weekdayName', $dtendDt, ['width' => 'abbreviated']) . ', ' .
486
+                $l10n->l('datetime', $dtendDt, ['width' => 'medium|short']);
487
+        }
488
+
489
+        return  $localeStart . ' - ' . $localeEnd . ' (' . $startTimezone . ')';
490
+    }
491
+
492
+    /**
493
+     * @param \DateTime $dtStart
494
+     * @param \DateTime $dtEnd
495
+     * @return bool
496
+     */
497
+    private function isDayEqual(\DateTime $dtStart, \DateTime $dtEnd) {
498
+        return $dtStart->format('Y-m-d') === $dtEnd->format('Y-m-d');
499
+    }
500
+
501
+    /**
502
+     * @param IEMailTemplate $template
503
+     * @param IL10N $l10n
504
+     * @param string $method
505
+     * @param string $summary
506
+     * @param string $attendeeName
507
+     * @param string $inviteeName
508
+     */
509
+    private function addSubjectAndHeading(IEMailTemplate $template, IL10N $l10n,
510
+                                            $method, $summary, $attendeeName, $inviteeName) {
511
+        if ($method === self::METHOD_CANCEL) {
512
+            $template->setSubject('Cancelled: ' . $summary);
513
+            $template->addHeading($l10n->t('Invitation canceled'), $l10n->t('Hello %s,', [$attendeeName]));
514
+            $template->addBodyText($l10n->t('The meeting »%1$s« with %2$s was canceled.', [$summary, $inviteeName]));
515
+        } else if ($method === self::METHOD_REPLY) {
516
+            $template->setSubject('Re: ' . $summary);
517
+            $template->addHeading($l10n->t('Invitation updated'), $l10n->t('Hello %s,', [$attendeeName]));
518
+            $template->addBodyText($l10n->t('The meeting »%1$s« with %2$s was updated.', [$summary, $inviteeName]));
519
+        } else {
520
+            $template->setSubject('Invitation: ' . $summary);
521
+            $template->addHeading($l10n->t('%1$s invited you to »%2$s«', [$inviteeName, $summary]), $l10n->t('Hello %s,', [$attendeeName]));
522
+        }
523
+    }
524
+
525
+    /**
526
+     * @param IEMailTemplate $template
527
+     * @param IL10N $l10n
528
+     * @param string $time
529
+     * @param string $location
530
+     * @param string $description
531
+     * @param string $url
532
+     */
533
+    private function addBulletList(IEMailTemplate $template, IL10N $l10n, $time, $location, $description, $url) {
534
+        $template->addBodyListItem($time, $l10n->t('When:'),
535
+            $this->getAbsoluteImagePath('filetypes/text-calendar.svg'));
536
+
537
+        if ($location) {
538
+            $template->addBodyListItem($location, $l10n->t('Where:'),
539
+                $this->getAbsoluteImagePath('filetypes/location.svg'));
540
+        }
541
+        if ($description) {
542
+            $template->addBodyListItem((string)$description, $l10n->t('Description:'),
543
+                $this->getAbsoluteImagePath('filetypes/text.svg'));
544
+        }
545
+        if ($url) {
546
+            $template->addBodyListItem((string)$url, $l10n->t('Link:'),
547
+                $this->getAbsoluteImagePath('filetypes/link.svg'));
548
+        }
549
+    }
550
+
551
+    /**
552
+     * @param IEMailTemplate $template
553
+     * @param IL10N $l10n
554
+     * @param Message $iTipMessage
555
+     * @param int $lastOccurrence
556
+     */
557
+    private function addResponseButtons(IEMailTemplate $template, IL10N $l10n,
558
+                                        Message $iTipMessage, $lastOccurrence) {
559
+        $token = $this->createInvitationToken($iTipMessage, $lastOccurrence);
560
+
561
+        $template->addBodyButtonGroup(
562
+            $l10n->t('Accept'),
563
+            $this->urlGenerator->linkToRouteAbsolute('dav.invitation_response.accept', [
564
+                'token' => $token,
565
+            ]),
566
+            $l10n->t('Decline'),
567
+            $this->urlGenerator->linkToRouteAbsolute('dav.invitation_response.decline', [
568
+                'token' => $token,
569
+            ])
570
+        );
571
+
572
+        $moreOptionsURL = $this->urlGenerator->linkToRouteAbsolute('dav.invitation_response.options', [
573
+            'token' => $token,
574
+        ]);
575
+        $html = vsprintf('<small><a href="%s">%s</a></small>', [
576
+            $moreOptionsURL, $l10n->t('More options …')
577
+        ]);
578
+        $text = $l10n->t('More options at %s', [$moreOptionsURL]);
579
+
580
+        $template->addBodyText($html, $text);
581
+    }
582
+
583
+    /**
584
+     * @param string $path
585
+     * @return string
586
+     */
587
+    private function getAbsoluteImagePath($path) {
588
+        return $this->urlGenerator->getAbsoluteURL(
589
+            $this->urlGenerator->imagePath('core', $path)
590
+        );
591
+    }
592
+
593
+    /**
594
+     * @param Message $iTipMessage
595
+     * @param int $lastOccurrence
596
+     * @return string
597
+     */
598
+    private function createInvitationToken(Message $iTipMessage, $lastOccurrence):string {
599
+        $token = $this->random->generate(60, ISecureRandom::CHAR_UPPER . ISecureRandom::CHAR_LOWER . ISecureRandom::CHAR_DIGITS);
600
+
601
+        /** @var VEvent $vevent */
602
+        $vevent = $iTipMessage->message->VEVENT;
603
+        $attendee = $iTipMessage->recipient;
604
+        $organizer = $iTipMessage->sender;
605
+        $sequence = $iTipMessage->sequence;
606
+        $recurrenceId = isset($vevent->{'RECURRENCE-ID'}) ?
607
+            $vevent->{'RECURRENCE-ID'}->serialize() : null;
608
+        $uid = $vevent->{'UID'};
609
+
610
+        $query = $this->db->getQueryBuilder();
611
+        $query->insert('calendar_invitations')
612
+            ->values([
613
+                'token' => $query->createNamedParameter($token),
614
+                'attendee' => $query->createNamedParameter($attendee),
615
+                'organizer' => $query->createNamedParameter($organizer),
616
+                'sequence' => $query->createNamedParameter($sequence),
617
+                'recurrenceid' => $query->createNamedParameter($recurrenceId),
618
+                'expiration' => $query->createNamedParameter($lastOccurrence),
619
+                'uid' => $query->createNamedParameter($uid)
620
+            ])
621
+            ->execute();
622
+
623
+        return $token;
624
+    }
625 625
 }
Please login to merge, or discard this patch.
Spacing   +23 added lines, -23 removed lines patch added patch discarded remove patch
@@ -238,11 +238,11 @@  discard block
 block discarded – undo
238 238
 		}
239 239
 
240 240
 		$data = [
241
-			'attendee_name' => (string)$meetingAttendeeName ?: $defaultVal,
242
-			'invitee_name' => (string)$meetingInviteeName ?: $defaultVal,
243
-			'meeting_title' => (string)$meetingTitle ?: $defaultVal,
244
-			'meeting_description' => (string)$meetingDescription ?: $defaultVal,
245
-			'meeting_url' => (string)$meetingUrl ?: $defaultVal,
241
+			'attendee_name' => (string) $meetingAttendeeName ?: $defaultVal,
242
+			'invitee_name' => (string) $meetingInviteeName ?: $defaultVal,
243
+			'meeting_title' => (string) $meetingTitle ?: $defaultVal,
244
+			'meeting_description' => (string) $meetingDescription ?: $defaultVal,
245
+			'meeting_url' => (string) $meetingUrl ?: $defaultVal,
246 246
 		];
247 247
 
248 248
 		$fromEMail = \OCP\Util::getDefaultEmailAddress('invitations-noreply');
@@ -253,7 +253,7 @@  discard block
 block discarded – undo
253 253
 			->setReplyTo([$sender => $senderName])
254 254
 			->setTo([$recipient => $recipientName]);
255 255
 
256
-		$template = $this->mailer->createEMailTemplate('dav.calendarInvite.' . $method, $data);
256
+		$template = $this->mailer->createEMailTemplate('dav.calendarInvite.'.$method, $data);
257 257
 		$template->addHeader();
258 258
 
259 259
 		$this->addSubjectAndHeading($template, $l10n, $method, $summary,
@@ -300,8 +300,8 @@  discard block
 block discarded – undo
300 300
 
301 301
 		$attachment = $this->mailer->createAttachment(
302 302
 			$iTipMessage->message->serialize(),
303
-			'event.ics',// TODO(leon): Make file name unique, e.g. add event id
304
-			'text/calendar; method=' . $iTipMessage->method
303
+			'event.ics', // TODO(leon): Make file name unique, e.g. add event id
304
+			'text/calendar; method='.$iTipMessage->method
305 305
 		);
306 306
 		$message->attach($attachment);
307 307
 
@@ -312,7 +312,7 @@  discard block
 block discarded – undo
312 312
 				$this->logger->error('Unable to deliver message to {failed}', ['app' => 'dav', 'failed' =>  implode(', ', $failed)]);
313 313
 				$iTipMessage->scheduleStatus = '5.0; EMail delivery failed';
314 314
 			}
315
-		} catch(\Exception $ex) {
315
+		} catch (\Exception $ex) {
316 316
 			$this->logger->logException($ex, ['app' => 'dav']);
317 317
 			$iTipMessage->scheduleStatus = '5.0; EMail delivery failed';
318 318
 		}
@@ -348,13 +348,13 @@  discard block
 block discarded – undo
348 348
 				$lastOccurrence = $firstOccurrence;
349 349
 			}
350 350
 		} else {
351
-			$it = new EventIterator($vObject, (string)$component->UID);
351
+			$it = new EventIterator($vObject, (string) $component->UID);
352 352
 			$maxDate = new \DateTime(self::MAX_DATE);
353 353
 			if ($it->isInfinite()) {
354 354
 				$lastOccurrence = $maxDate->getTimestamp();
355 355
 			} else {
356 356
 				$end = $it->getDtEnd();
357
-				while($it->valid() && $end < $maxDate) {
357
+				while ($it->valid() && $end < $maxDate) {
358 358
 					$end = $it->getDtEnd();
359 359
 					$it->next();
360 360
 
@@ -448,7 +448,7 @@  discard block
 block discarded – undo
448 448
 			$localeStart = $l10n->l('date', $dtstartDt, ['width' => 'medium']);
449 449
 			$localeEnd = $l10n->l('date', $dtendDt, ['width' => 'medium']);
450 450
 
451
-			return $localeStart . ' - ' . $localeEnd;
451
+			return $localeStart.' - '.$localeEnd;
452 452
 		}
453 453
 
454 454
 		/** @var Property\ICalendar\DateTime $dtstart */
@@ -467,26 +467,26 @@  discard block
 block discarded – undo
467 467
 			}
468 468
 		}
469 469
 
470
-		$localeStart = $l10n->l('weekdayName', $dtstartDt, ['width' => 'abbreviated']) . ', ' .
470
+		$localeStart = $l10n->l('weekdayName', $dtstartDt, ['width' => 'abbreviated']).', '.
471 471
 			$l10n->l('datetime', $dtstartDt, ['width' => 'medium|short']);
472 472
 
473 473
 		// always show full date with timezone if timezones are different
474 474
 		if ($startTimezone !== $endTimezone) {
475 475
 			$localeEnd = $l10n->l('datetime', $dtendDt, ['width' => 'medium|short']);
476 476
 
477
-			return $localeStart . ' (' . $startTimezone . ') - ' .
478
-				$localeEnd . ' (' . $endTimezone . ')';
477
+			return $localeStart.' ('.$startTimezone.') - '.
478
+				$localeEnd.' ('.$endTimezone.')';
479 479
 		}
480 480
 
481 481
 		// show only end time if date is the same
482 482
 		if ($this->isDayEqual($dtstartDt, $dtendDt)) {
483 483
 			$localeEnd = $l10n->l('time', $dtendDt, ['width' => 'short']);
484 484
 		} else {
485
-			$localeEnd = $l10n->l('weekdayName', $dtendDt, ['width' => 'abbreviated']) . ', ' .
485
+			$localeEnd = $l10n->l('weekdayName', $dtendDt, ['width' => 'abbreviated']).', '.
486 486
 				$l10n->l('datetime', $dtendDt, ['width' => 'medium|short']);
487 487
 		}
488 488
 
489
-		return  $localeStart . ' - ' . $localeEnd . ' (' . $startTimezone . ')';
489
+		return  $localeStart.' - '.$localeEnd.' ('.$startTimezone.')';
490 490
 	}
491 491
 
492 492
 	/**
@@ -509,15 +509,15 @@  discard block
 block discarded – undo
509 509
 	private function addSubjectAndHeading(IEMailTemplate $template, IL10N $l10n,
510 510
 										  $method, $summary, $attendeeName, $inviteeName) {
511 511
 		if ($method === self::METHOD_CANCEL) {
512
-			$template->setSubject('Cancelled: ' . $summary);
512
+			$template->setSubject('Cancelled: '.$summary);
513 513
 			$template->addHeading($l10n->t('Invitation canceled'), $l10n->t('Hello %s,', [$attendeeName]));
514 514
 			$template->addBodyText($l10n->t('The meeting »%1$s« with %2$s was canceled.', [$summary, $inviteeName]));
515 515
 		} else if ($method === self::METHOD_REPLY) {
516
-			$template->setSubject('Re: ' . $summary);
516
+			$template->setSubject('Re: '.$summary);
517 517
 			$template->addHeading($l10n->t('Invitation updated'), $l10n->t('Hello %s,', [$attendeeName]));
518 518
 			$template->addBodyText($l10n->t('The meeting »%1$s« with %2$s was updated.', [$summary, $inviteeName]));
519 519
 		} else {
520
-			$template->setSubject('Invitation: ' . $summary);
520
+			$template->setSubject('Invitation: '.$summary);
521 521
 			$template->addHeading($l10n->t('%1$s invited you to »%2$s«', [$inviteeName, $summary]), $l10n->t('Hello %s,', [$attendeeName]));
522 522
 		}
523 523
 	}
@@ -539,11 +539,11 @@  discard block
 block discarded – undo
539 539
 				$this->getAbsoluteImagePath('filetypes/location.svg'));
540 540
 		}
541 541
 		if ($description) {
542
-			$template->addBodyListItem((string)$description, $l10n->t('Description:'),
542
+			$template->addBodyListItem((string) $description, $l10n->t('Description:'),
543 543
 				$this->getAbsoluteImagePath('filetypes/text.svg'));
544 544
 		}
545 545
 		if ($url) {
546
-			$template->addBodyListItem((string)$url, $l10n->t('Link:'),
546
+			$template->addBodyListItem((string) $url, $l10n->t('Link:'),
547 547
 				$this->getAbsoluteImagePath('filetypes/link.svg'));
548 548
 		}
549 549
 	}
@@ -596,7 +596,7 @@  discard block
 block discarded – undo
596 596
 	 * @return string
597 597
 	 */
598 598
 	private function createInvitationToken(Message $iTipMessage, $lastOccurrence):string {
599
-		$token = $this->random->generate(60, ISecureRandom::CHAR_UPPER . ISecureRandom::CHAR_LOWER . ISecureRandom::CHAR_DIGITS);
599
+		$token = $this->random->generate(60, ISecureRandom::CHAR_UPPER.ISecureRandom::CHAR_LOWER.ISecureRandom::CHAR_DIGITS);
600 600
 
601 601
 		/** @var VEvent $vevent */
602 602
 		$vevent = $iTipMessage->message->VEVENT;
Please login to merge, or discard this patch.
apps/dav/lib/Connector/Sabre/MaintenancePlugin.php 1 patch
Indentation   +48 added lines, -48 removed lines patch added patch discarded remove patch
@@ -33,58 +33,58 @@
 block discarded – undo
33 33
 
34 34
 class MaintenancePlugin extends ServerPlugin {
35 35
 
36
-	/** @var IConfig */
37
-	private $config;
36
+    /** @var IConfig */
37
+    private $config;
38 38
 
39
-	/**
40
-	 * Reference to main server object
41
-	 *
42
-	 * @var Server
43
-	 */
44
-	private $server;
39
+    /**
40
+     * Reference to main server object
41
+     *
42
+     * @var Server
43
+     */
44
+    private $server;
45 45
 
46
-	/**
47
-	 * @param IConfig $config
48
-	 */
49
-	public function __construct(IConfig $config = null) {
50
-		$this->config = $config;
51
-		if (is_null($config)) {
52
-			$this->config = \OC::$server->getConfig();
53
-		}
54
-	}
46
+    /**
47
+     * @param IConfig $config
48
+     */
49
+    public function __construct(IConfig $config = null) {
50
+        $this->config = $config;
51
+        if (is_null($config)) {
52
+            $this->config = \OC::$server->getConfig();
53
+        }
54
+    }
55 55
 
56 56
 
57
-	/**
58
-	 * This initializes the plugin.
59
-	 *
60
-	 * This function is called by \Sabre\DAV\Server, after
61
-	 * addPlugin is called.
62
-	 *
63
-	 * This method should set up the required event subscriptions.
64
-	 *
65
-	 * @param \Sabre\DAV\Server $server
66
-	 * @return void
67
-	 */
68
-	public function initialize(\Sabre\DAV\Server $server) {
69
-		$this->server = $server;
70
-		$this->server->on('beforeMethod', [$this, 'checkMaintenanceMode'], 1);
71
-	}
57
+    /**
58
+     * This initializes the plugin.
59
+     *
60
+     * This function is called by \Sabre\DAV\Server, after
61
+     * addPlugin is called.
62
+     *
63
+     * This method should set up the required event subscriptions.
64
+     *
65
+     * @param \Sabre\DAV\Server $server
66
+     * @return void
67
+     */
68
+    public function initialize(\Sabre\DAV\Server $server) {
69
+        $this->server = $server;
70
+        $this->server->on('beforeMethod', [$this, 'checkMaintenanceMode'], 1);
71
+    }
72 72
 
73
-	/**
74
-	 * This method is called before any HTTP method and returns http status code 503
75
-	 * in case the system is in maintenance mode.
76
-	 *
77
-	 * @throws ServiceUnavailable
78
-	 * @return bool
79
-	 */
80
-	public function checkMaintenanceMode() {
81
-		if ($this->config->getSystemValueBool('maintenance')) {
82
-			throw new ServiceUnavailable('System in maintenance mode.');
83
-		}
84
-		if (Util::needUpgrade()) {
85
-			throw new ServiceUnavailable('Upgrade needed');
86
-		}
73
+    /**
74
+     * This method is called before any HTTP method and returns http status code 503
75
+     * in case the system is in maintenance mode.
76
+     *
77
+     * @throws ServiceUnavailable
78
+     * @return bool
79
+     */
80
+    public function checkMaintenanceMode() {
81
+        if ($this->config->getSystemValueBool('maintenance')) {
82
+            throw new ServiceUnavailable('System in maintenance mode.');
83
+        }
84
+        if (Util::needUpgrade()) {
85
+            throw new ServiceUnavailable('Upgrade needed');
86
+        }
87 87
 
88
-		return true;
89
-	}
88
+        return true;
89
+    }
90 90
 }
Please login to merge, or discard this patch.
apps/dav/lib/Connector/Sabre/FilesPlugin.php 2 patches
Indentation   +462 added lines, -462 removed lines patch added patch discarded remove patch
@@ -52,470 +52,470 @@
 block discarded – undo
52 52
 
53 53
 class FilesPlugin extends ServerPlugin {
54 54
 
55
-	// namespace
56
-	const NS_OWNCLOUD = 'http://owncloud.org/ns';
57
-	const NS_NEXTCLOUD = 'http://nextcloud.org/ns';
58
-	const FILEID_PROPERTYNAME = '{http://owncloud.org/ns}id';
59
-	const INTERNAL_FILEID_PROPERTYNAME = '{http://owncloud.org/ns}fileid';
60
-	const PERMISSIONS_PROPERTYNAME = '{http://owncloud.org/ns}permissions';
61
-	const SHARE_PERMISSIONS_PROPERTYNAME = '{http://open-collaboration-services.org/ns}share-permissions';
62
-	const OCM_SHARE_PERMISSIONS_PROPERTYNAME = '{http://open-cloud-mesh.org/ns}share-permissions';
63
-	const DOWNLOADURL_PROPERTYNAME = '{http://owncloud.org/ns}downloadURL';
64
-	const SIZE_PROPERTYNAME = '{http://owncloud.org/ns}size';
65
-	const GETETAG_PROPERTYNAME = '{DAV:}getetag';
66
-	const LASTMODIFIED_PROPERTYNAME = '{DAV:}lastmodified';
67
-	const OWNER_ID_PROPERTYNAME = '{http://owncloud.org/ns}owner-id';
68
-	const OWNER_DISPLAY_NAME_PROPERTYNAME = '{http://owncloud.org/ns}owner-display-name';
69
-	const CHECKSUMS_PROPERTYNAME = '{http://owncloud.org/ns}checksums';
70
-	const DATA_FINGERPRINT_PROPERTYNAME = '{http://owncloud.org/ns}data-fingerprint';
71
-	const HAS_PREVIEW_PROPERTYNAME = '{http://nextcloud.org/ns}has-preview';
72
-	const MOUNT_TYPE_PROPERTYNAME = '{http://nextcloud.org/ns}mount-type';
73
-	const IS_ENCRYPTED_PROPERTYNAME = '{http://nextcloud.org/ns}is-encrypted';
74
-	const METADATA_ETAG_PROPERTYNAME = '{http://nextcloud.org/ns}metadata_etag';
75
-	const UPLOAD_TIME_PROPERTYNAME = '{http://nextcloud.org/ns}upload_time';
76
-	const CREATION_TIME_PROPERTYNAME = '{http://nextcloud.org/ns}creation_time';
77
-	const SHARE_NOTE = '{http://nextcloud.org/ns}note';
78
-
79
-	/**
80
-	 * Reference to main server object
81
-	 *
82
-	 * @var \Sabre\DAV\Server
83
-	 */
84
-	private $server;
85
-
86
-	/**
87
-	 * @var Tree
88
-	 */
89
-	private $tree;
90
-
91
-	/**
92
-	 * Whether this is public webdav.
93
-	 * If true, some returned information will be stripped off.
94
-	 *
95
-	 * @var bool
96
-	 */
97
-	private $isPublic;
98
-
99
-	/**
100
-	 * @var bool
101
-	 */
102
-	private $downloadAttachment;
103
-
104
-	/**
105
-	 * @var IConfig
106
-	 */
107
-	private $config;
108
-
109
-	/**
110
-	 * @var IRequest
111
-	 */
112
-	private $request;
113
-
114
-	/**
115
-	 * @var IPreview
116
-	 */
117
-	private $previewManager;
118
-
119
-	/**
120
-	 * @param Tree $tree
121
-	 * @param IConfig $config
122
-	 * @param IRequest $request
123
-	 * @param IPreview $previewManager
124
-	 * @param bool $isPublic
125
-	 * @param bool $downloadAttachment
126
-	 */
127
-	public function __construct(Tree $tree,
128
-								IConfig $config,
129
-								IRequest $request,
130
-								IPreview $previewManager,
131
-								$isPublic = false,
132
-								$downloadAttachment = true) {
133
-		$this->tree = $tree;
134
-		$this->config = $config;
135
-		$this->request = $request;
136
-		$this->isPublic = $isPublic;
137
-		$this->downloadAttachment = $downloadAttachment;
138
-		$this->previewManager = $previewManager;
139
-	}
140
-
141
-	/**
142
-	 * This initializes the plugin.
143
-	 *
144
-	 * This function is called by \Sabre\DAV\Server, after
145
-	 * addPlugin is called.
146
-	 *
147
-	 * This method should set up the required event subscriptions.
148
-	 *
149
-	 * @param \Sabre\DAV\Server $server
150
-	 * @return void
151
-	 */
152
-	public function initialize(\Sabre\DAV\Server $server) {
153
-		$server->xml->namespaceMap[self::NS_OWNCLOUD] = 'oc';
154
-		$server->xml->namespaceMap[self::NS_NEXTCLOUD] = 'nc';
155
-		$server->protectedProperties[] = self::FILEID_PROPERTYNAME;
156
-		$server->protectedProperties[] = self::INTERNAL_FILEID_PROPERTYNAME;
157
-		$server->protectedProperties[] = self::PERMISSIONS_PROPERTYNAME;
158
-		$server->protectedProperties[] = self::SHARE_PERMISSIONS_PROPERTYNAME;
159
-		$server->protectedProperties[] = self::OCM_SHARE_PERMISSIONS_PROPERTYNAME;
160
-		$server->protectedProperties[] = self::SIZE_PROPERTYNAME;
161
-		$server->protectedProperties[] = self::DOWNLOADURL_PROPERTYNAME;
162
-		$server->protectedProperties[] = self::OWNER_ID_PROPERTYNAME;
163
-		$server->protectedProperties[] = self::OWNER_DISPLAY_NAME_PROPERTYNAME;
164
-		$server->protectedProperties[] = self::CHECKSUMS_PROPERTYNAME;
165
-		$server->protectedProperties[] = self::DATA_FINGERPRINT_PROPERTYNAME;
166
-		$server->protectedProperties[] = self::HAS_PREVIEW_PROPERTYNAME;
167
-		$server->protectedProperties[] = self::MOUNT_TYPE_PROPERTYNAME;
168
-		$server->protectedProperties[] = self::IS_ENCRYPTED_PROPERTYNAME;
169
-		$server->protectedProperties[] = self::SHARE_NOTE;
170
-
171
-		// normally these cannot be changed (RFC4918), but we want them modifiable through PROPPATCH
172
-		$allowedProperties = ['{DAV:}getetag'];
173
-		$server->protectedProperties = array_diff($server->protectedProperties, $allowedProperties);
174
-
175
-		$this->server = $server;
176
-		$this->server->on('propFind', [$this, 'handleGetProperties']);
177
-		$this->server->on('propPatch', [$this, 'handleUpdateProperties']);
178
-		$this->server->on('afterBind', [$this, 'sendFileIdHeader']);
179
-		$this->server->on('afterWriteContent', [$this, 'sendFileIdHeader']);
180
-		$this->server->on('afterMethod:GET', [$this,'httpGet']);
181
-		$this->server->on('afterMethod:GET', [$this, 'handleDownloadToken']);
182
-		$this->server->on('afterResponse', function($request, ResponseInterface $response) {
183
-			$body = $response->getBody();
184
-			if (is_resource($body)) {
185
-				fclose($body);
186
-			}
187
-		});
188
-		$this->server->on('beforeMove', [$this, 'checkMove']);
189
-	}
190
-
191
-	/**
192
-	 * Plugin that checks if a move can actually be performed.
193
-	 *
194
-	 * @param string $source source path
195
-	 * @param string $destination destination path
196
-	 * @throws Forbidden
197
-	 * @throws NotFound
198
-	 */
199
-	function checkMove($source, $destination) {
200
-		$sourceNode = $this->tree->getNodeForPath($source);
201
-		if (!$sourceNode instanceof Node) {
202
-			return;
203
-		}
204
-		list($sourceDir,) = \Sabre\Uri\split($source);
205
-		list($destinationDir,) = \Sabre\Uri\split($destination);
206
-
207
-		if ($sourceDir !== $destinationDir) {
208
-			$sourceNodeFileInfo = $sourceNode->getFileInfo();
209
-			if ($sourceNodeFileInfo === null) {
210
-				throw new NotFound($source . ' does not exist');
211
- 			}
212
-
213
-			if (!$sourceNodeFileInfo->isDeletable()) {
214
-				throw new Forbidden($source . " cannot be deleted");
215
-			}
216
-		}
217
-	}
218
-
219
-	/**
220
-	 * This sets a cookie to be able to recognize the start of the download
221
-	 * the content must not be longer than 32 characters and must only contain
222
-	 * alphanumeric characters
223
-	 *
224
-	 * @param RequestInterface $request
225
-	 * @param ResponseInterface $response
226
-	 */
227
-	function handleDownloadToken(RequestInterface $request, ResponseInterface $response) {
228
-		$queryParams = $request->getQueryParameters();
229
-
230
-		/**
231
-		 * this sets a cookie to be able to recognize the start of the download
232
-		 * the content must not be longer than 32 characters and must only contain
233
-		 * alphanumeric characters
234
-		 */
235
-		if (isset($queryParams['downloadStartSecret'])) {
236
-			$token = $queryParams['downloadStartSecret'];
237
-			if (!isset($token[32])
238
-				&& preg_match('!^[a-zA-Z0-9]+$!', $token) === 1) {
239
-				// FIXME: use $response->setHeader() instead
240
-				setcookie('ocDownloadStarted', $token, time() + 20, '/');
241
-			}
242
-		}
243
-	}
244
-
245
-	/**
246
-	 * Add headers to file download
247
-	 *
248
-	 * @param RequestInterface $request
249
-	 * @param ResponseInterface $response
250
-	 */
251
-	function httpGet(RequestInterface $request, ResponseInterface $response) {
252
-		// Only handle valid files
253
-		$node = $this->tree->getNodeForPath($request->getPath());
254
-		if (!($node instanceof IFile)) return;
255
-
256
-		// adds a 'Content-Disposition: attachment' header in case no disposition
257
-		// header has been set before
258
-		if ($this->downloadAttachment &&
259
-			$response->getHeader('Content-Disposition') === null) {
260
-			$filename = $node->getName();
261
-			if ($this->request->isUserAgent(
262
-				[
263
-					Request::USER_AGENT_IE,
264
-					Request::USER_AGENT_ANDROID_MOBILE_CHROME,
265
-					Request::USER_AGENT_FREEBOX,
266
-				])) {
267
-				$response->addHeader('Content-Disposition', 'attachment; filename="' . rawurlencode($filename) . '"');
268
-			} else {
269
-				$response->addHeader('Content-Disposition', 'attachment; filename*=UTF-8\'\'' . rawurlencode($filename)
270
-													 . '; filename="' . rawurlencode($filename) . '"');
271
-			}
272
-		}
273
-
274
-		if ($node instanceof \OCA\DAV\Connector\Sabre\File) {
275
-			//Add OC-Checksum header
276
-			/** @var $node File */
277
-			$checksum = $node->getChecksum();
278
-			if ($checksum !== null && $checksum !== '') {
279
-				$response->addHeader('OC-Checksum', $checksum);
280
-			}
281
-		}
282
-	}
283
-
284
-	/**
285
-	 * Adds all ownCloud-specific properties
286
-	 *
287
-	 * @param PropFind $propFind
288
-	 * @param \Sabre\DAV\INode $node
289
-	 * @return void
290
-	 */
291
-	public function handleGetProperties(PropFind $propFind, \Sabre\DAV\INode $node) {
292
-
293
-		$httpRequest = $this->server->httpRequest;
294
-
295
-		if ($node instanceof \OCA\DAV\Connector\Sabre\Node) {
296
-			/**
297
-			 * This was disabled, because it made dir listing throw an exception,
298
-			 * so users were unable to navigate into folders where one subitem
299
-			 * is blocked by the files_accesscontrol app, see:
300
-			 * https://github.com/nextcloud/files_accesscontrol/issues/65
55
+    // namespace
56
+    const NS_OWNCLOUD = 'http://owncloud.org/ns';
57
+    const NS_NEXTCLOUD = 'http://nextcloud.org/ns';
58
+    const FILEID_PROPERTYNAME = '{http://owncloud.org/ns}id';
59
+    const INTERNAL_FILEID_PROPERTYNAME = '{http://owncloud.org/ns}fileid';
60
+    const PERMISSIONS_PROPERTYNAME = '{http://owncloud.org/ns}permissions';
61
+    const SHARE_PERMISSIONS_PROPERTYNAME = '{http://open-collaboration-services.org/ns}share-permissions';
62
+    const OCM_SHARE_PERMISSIONS_PROPERTYNAME = '{http://open-cloud-mesh.org/ns}share-permissions';
63
+    const DOWNLOADURL_PROPERTYNAME = '{http://owncloud.org/ns}downloadURL';
64
+    const SIZE_PROPERTYNAME = '{http://owncloud.org/ns}size';
65
+    const GETETAG_PROPERTYNAME = '{DAV:}getetag';
66
+    const LASTMODIFIED_PROPERTYNAME = '{DAV:}lastmodified';
67
+    const OWNER_ID_PROPERTYNAME = '{http://owncloud.org/ns}owner-id';
68
+    const OWNER_DISPLAY_NAME_PROPERTYNAME = '{http://owncloud.org/ns}owner-display-name';
69
+    const CHECKSUMS_PROPERTYNAME = '{http://owncloud.org/ns}checksums';
70
+    const DATA_FINGERPRINT_PROPERTYNAME = '{http://owncloud.org/ns}data-fingerprint';
71
+    const HAS_PREVIEW_PROPERTYNAME = '{http://nextcloud.org/ns}has-preview';
72
+    const MOUNT_TYPE_PROPERTYNAME = '{http://nextcloud.org/ns}mount-type';
73
+    const IS_ENCRYPTED_PROPERTYNAME = '{http://nextcloud.org/ns}is-encrypted';
74
+    const METADATA_ETAG_PROPERTYNAME = '{http://nextcloud.org/ns}metadata_etag';
75
+    const UPLOAD_TIME_PROPERTYNAME = '{http://nextcloud.org/ns}upload_time';
76
+    const CREATION_TIME_PROPERTYNAME = '{http://nextcloud.org/ns}creation_time';
77
+    const SHARE_NOTE = '{http://nextcloud.org/ns}note';
78
+
79
+    /**
80
+     * Reference to main server object
81
+     *
82
+     * @var \Sabre\DAV\Server
83
+     */
84
+    private $server;
85
+
86
+    /**
87
+     * @var Tree
88
+     */
89
+    private $tree;
90
+
91
+    /**
92
+     * Whether this is public webdav.
93
+     * If true, some returned information will be stripped off.
94
+     *
95
+     * @var bool
96
+     */
97
+    private $isPublic;
98
+
99
+    /**
100
+     * @var bool
101
+     */
102
+    private $downloadAttachment;
103
+
104
+    /**
105
+     * @var IConfig
106
+     */
107
+    private $config;
108
+
109
+    /**
110
+     * @var IRequest
111
+     */
112
+    private $request;
113
+
114
+    /**
115
+     * @var IPreview
116
+     */
117
+    private $previewManager;
118
+
119
+    /**
120
+     * @param Tree $tree
121
+     * @param IConfig $config
122
+     * @param IRequest $request
123
+     * @param IPreview $previewManager
124
+     * @param bool $isPublic
125
+     * @param bool $downloadAttachment
126
+     */
127
+    public function __construct(Tree $tree,
128
+                                IConfig $config,
129
+                                IRequest $request,
130
+                                IPreview $previewManager,
131
+                                $isPublic = false,
132
+                                $downloadAttachment = true) {
133
+        $this->tree = $tree;
134
+        $this->config = $config;
135
+        $this->request = $request;
136
+        $this->isPublic = $isPublic;
137
+        $this->downloadAttachment = $downloadAttachment;
138
+        $this->previewManager = $previewManager;
139
+    }
140
+
141
+    /**
142
+     * This initializes the plugin.
143
+     *
144
+     * This function is called by \Sabre\DAV\Server, after
145
+     * addPlugin is called.
146
+     *
147
+     * This method should set up the required event subscriptions.
148
+     *
149
+     * @param \Sabre\DAV\Server $server
150
+     * @return void
151
+     */
152
+    public function initialize(\Sabre\DAV\Server $server) {
153
+        $server->xml->namespaceMap[self::NS_OWNCLOUD] = 'oc';
154
+        $server->xml->namespaceMap[self::NS_NEXTCLOUD] = 'nc';
155
+        $server->protectedProperties[] = self::FILEID_PROPERTYNAME;
156
+        $server->protectedProperties[] = self::INTERNAL_FILEID_PROPERTYNAME;
157
+        $server->protectedProperties[] = self::PERMISSIONS_PROPERTYNAME;
158
+        $server->protectedProperties[] = self::SHARE_PERMISSIONS_PROPERTYNAME;
159
+        $server->protectedProperties[] = self::OCM_SHARE_PERMISSIONS_PROPERTYNAME;
160
+        $server->protectedProperties[] = self::SIZE_PROPERTYNAME;
161
+        $server->protectedProperties[] = self::DOWNLOADURL_PROPERTYNAME;
162
+        $server->protectedProperties[] = self::OWNER_ID_PROPERTYNAME;
163
+        $server->protectedProperties[] = self::OWNER_DISPLAY_NAME_PROPERTYNAME;
164
+        $server->protectedProperties[] = self::CHECKSUMS_PROPERTYNAME;
165
+        $server->protectedProperties[] = self::DATA_FINGERPRINT_PROPERTYNAME;
166
+        $server->protectedProperties[] = self::HAS_PREVIEW_PROPERTYNAME;
167
+        $server->protectedProperties[] = self::MOUNT_TYPE_PROPERTYNAME;
168
+        $server->protectedProperties[] = self::IS_ENCRYPTED_PROPERTYNAME;
169
+        $server->protectedProperties[] = self::SHARE_NOTE;
170
+
171
+        // normally these cannot be changed (RFC4918), but we want them modifiable through PROPPATCH
172
+        $allowedProperties = ['{DAV:}getetag'];
173
+        $server->protectedProperties = array_diff($server->protectedProperties, $allowedProperties);
174
+
175
+        $this->server = $server;
176
+        $this->server->on('propFind', [$this, 'handleGetProperties']);
177
+        $this->server->on('propPatch', [$this, 'handleUpdateProperties']);
178
+        $this->server->on('afterBind', [$this, 'sendFileIdHeader']);
179
+        $this->server->on('afterWriteContent', [$this, 'sendFileIdHeader']);
180
+        $this->server->on('afterMethod:GET', [$this,'httpGet']);
181
+        $this->server->on('afterMethod:GET', [$this, 'handleDownloadToken']);
182
+        $this->server->on('afterResponse', function($request, ResponseInterface $response) {
183
+            $body = $response->getBody();
184
+            if (is_resource($body)) {
185
+                fclose($body);
186
+            }
187
+        });
188
+        $this->server->on('beforeMove', [$this, 'checkMove']);
189
+    }
190
+
191
+    /**
192
+     * Plugin that checks if a move can actually be performed.
193
+     *
194
+     * @param string $source source path
195
+     * @param string $destination destination path
196
+     * @throws Forbidden
197
+     * @throws NotFound
198
+     */
199
+    function checkMove($source, $destination) {
200
+        $sourceNode = $this->tree->getNodeForPath($source);
201
+        if (!$sourceNode instanceof Node) {
202
+            return;
203
+        }
204
+        list($sourceDir,) = \Sabre\Uri\split($source);
205
+        list($destinationDir,) = \Sabre\Uri\split($destination);
206
+
207
+        if ($sourceDir !== $destinationDir) {
208
+            $sourceNodeFileInfo = $sourceNode->getFileInfo();
209
+            if ($sourceNodeFileInfo === null) {
210
+                throw new NotFound($source . ' does not exist');
211
+                }
212
+
213
+            if (!$sourceNodeFileInfo->isDeletable()) {
214
+                throw new Forbidden($source . " cannot be deleted");
215
+            }
216
+        }
217
+    }
218
+
219
+    /**
220
+     * This sets a cookie to be able to recognize the start of the download
221
+     * the content must not be longer than 32 characters and must only contain
222
+     * alphanumeric characters
223
+     *
224
+     * @param RequestInterface $request
225
+     * @param ResponseInterface $response
226
+     */
227
+    function handleDownloadToken(RequestInterface $request, ResponseInterface $response) {
228
+        $queryParams = $request->getQueryParameters();
229
+
230
+        /**
231
+         * this sets a cookie to be able to recognize the start of the download
232
+         * the content must not be longer than 32 characters and must only contain
233
+         * alphanumeric characters
234
+         */
235
+        if (isset($queryParams['downloadStartSecret'])) {
236
+            $token = $queryParams['downloadStartSecret'];
237
+            if (!isset($token[32])
238
+                && preg_match('!^[a-zA-Z0-9]+$!', $token) === 1) {
239
+                // FIXME: use $response->setHeader() instead
240
+                setcookie('ocDownloadStarted', $token, time() + 20, '/');
241
+            }
242
+        }
243
+    }
244
+
245
+    /**
246
+     * Add headers to file download
247
+     *
248
+     * @param RequestInterface $request
249
+     * @param ResponseInterface $response
250
+     */
251
+    function httpGet(RequestInterface $request, ResponseInterface $response) {
252
+        // Only handle valid files
253
+        $node = $this->tree->getNodeForPath($request->getPath());
254
+        if (!($node instanceof IFile)) return;
255
+
256
+        // adds a 'Content-Disposition: attachment' header in case no disposition
257
+        // header has been set before
258
+        if ($this->downloadAttachment &&
259
+            $response->getHeader('Content-Disposition') === null) {
260
+            $filename = $node->getName();
261
+            if ($this->request->isUserAgent(
262
+                [
263
+                    Request::USER_AGENT_IE,
264
+                    Request::USER_AGENT_ANDROID_MOBILE_CHROME,
265
+                    Request::USER_AGENT_FREEBOX,
266
+                ])) {
267
+                $response->addHeader('Content-Disposition', 'attachment; filename="' . rawurlencode($filename) . '"');
268
+            } else {
269
+                $response->addHeader('Content-Disposition', 'attachment; filename*=UTF-8\'\'' . rawurlencode($filename)
270
+                                                        . '; filename="' . rawurlencode($filename) . '"');
271
+            }
272
+        }
273
+
274
+        if ($node instanceof \OCA\DAV\Connector\Sabre\File) {
275
+            //Add OC-Checksum header
276
+            /** @var $node File */
277
+            $checksum = $node->getChecksum();
278
+            if ($checksum !== null && $checksum !== '') {
279
+                $response->addHeader('OC-Checksum', $checksum);
280
+            }
281
+        }
282
+    }
283
+
284
+    /**
285
+     * Adds all ownCloud-specific properties
286
+     *
287
+     * @param PropFind $propFind
288
+     * @param \Sabre\DAV\INode $node
289
+     * @return void
290
+     */
291
+    public function handleGetProperties(PropFind $propFind, \Sabre\DAV\INode $node) {
292
+
293
+        $httpRequest = $this->server->httpRequest;
294
+
295
+        if ($node instanceof \OCA\DAV\Connector\Sabre\Node) {
296
+            /**
297
+             * This was disabled, because it made dir listing throw an exception,
298
+             * so users were unable to navigate into folders where one subitem
299
+             * is blocked by the files_accesscontrol app, see:
300
+             * https://github.com/nextcloud/files_accesscontrol/issues/65
301 301
 			if (!$node->getFileInfo()->isReadable()) {
302 302
 				// avoid detecting files through this means
303 303
 				throw new NotFound();
304 304
 			}
305
-			 */
306
-
307
-			$propFind->handle(self::FILEID_PROPERTYNAME, function() use ($node) {
308
-				return $node->getFileId();
309
-			});
310
-
311
-			$propFind->handle(self::INTERNAL_FILEID_PROPERTYNAME, function() use ($node) {
312
-				return $node->getInternalFileId();
313
-			});
314
-
315
-			$propFind->handle(self::PERMISSIONS_PROPERTYNAME, function() use ($node) {
316
-				$perms = $node->getDavPermissions();
317
-				if ($this->isPublic) {
318
-					// remove mount information
319
-					$perms = str_replace(['S', 'M'], '', $perms);
320
-				}
321
-				return $perms;
322
-			});
323
-
324
-			$propFind->handle(self::SHARE_PERMISSIONS_PROPERTYNAME, function() use ($node, $httpRequest) {
325
-				return $node->getSharePermissions(
326
-					$httpRequest->getRawServerValue('PHP_AUTH_USER')
327
-				);
328
-			});
329
-
330
-			$propFind->handle(self::OCM_SHARE_PERMISSIONS_PROPERTYNAME, function() use ($node, $httpRequest) {
331
-				$ncPermissions = $node->getSharePermissions(
332
-					$httpRequest->getRawServerValue('PHP_AUTH_USER')
333
-				);
334
-				$ocmPermissions = $this->ncPermissions2ocmPermissions($ncPermissions);
335
-				return json_encode($ocmPermissions);
336
-			});
337
-
338
-			$propFind->handle(self::GETETAG_PROPERTYNAME, function() use ($node) {
339
-				return $node->getETag();
340
-			});
341
-
342
-			$propFind->handle(self::OWNER_ID_PROPERTYNAME, function() use ($node) {
343
-				$owner = $node->getOwner();
344
-				if (!$owner) {
345
-					return null;
346
-				} else {
347
-					return $owner->getUID();
348
-				}
349
-			});
350
-			$propFind->handle(self::OWNER_DISPLAY_NAME_PROPERTYNAME, function() use ($node) {
351
-				$owner = $node->getOwner();
352
-				if (!$owner) {
353
-					return null;
354
-				} else {
355
-					return $owner->getDisplayName();
356
-				}
357
-			});
358
-
359
-			$propFind->handle(self::HAS_PREVIEW_PROPERTYNAME, function () use ($node) {
360
-				return json_encode($this->previewManager->isAvailable($node->getFileInfo()));
361
-			});
362
-			$propFind->handle(self::SIZE_PROPERTYNAME, function() use ($node) {
363
-				return $node->getSize();
364
-			});
365
-			$propFind->handle(self::MOUNT_TYPE_PROPERTYNAME, function () use ($node) {
366
-				return $node->getFileInfo()->getMountPoint()->getMountType();
367
-			});
368
-
369
-			$propFind->handle(self::SHARE_NOTE, function() use ($node, $httpRequest) {
370
-				return $node->getNoteFromShare(
371
-					$httpRequest->getRawServerValue('PHP_AUTH_USER')
372
-				);
373
-			});
374
-		}
375
-
376
-		if ($node instanceof \OCA\DAV\Connector\Sabre\Node) {
377
-			$propFind->handle(self::DATA_FINGERPRINT_PROPERTYNAME, function() use ($node) {
378
-				return $this->config->getSystemValue('data-fingerprint', '');
379
-			});
380
-		}
381
-
382
-		if ($node instanceof \OCA\DAV\Connector\Sabre\File) {
383
-			$propFind->handle(self::DOWNLOADURL_PROPERTYNAME, function() use ($node) {
384
-				/** @var $node \OCA\DAV\Connector\Sabre\File */
385
-				try {
386
-					$directDownloadUrl = $node->getDirectDownload();
387
-					if (isset($directDownloadUrl['url'])) {
388
-						return $directDownloadUrl['url'];
389
-					}
390
-				} catch (StorageNotAvailableException $e) {
391
-					return false;
392
-				} catch (ForbiddenException $e) {
393
-					return false;
394
-				}
395
-				return false;
396
-			});
397
-
398
-			$propFind->handle(self::CHECKSUMS_PROPERTYNAME, function() use ($node) {
399
-				$checksum = $node->getChecksum();
400
-				if ($checksum === null || $checksum === '') {
401
-					return null;
402
-				}
403
-
404
-				return new ChecksumList($checksum);
405
-			});
406
-
407
-			$propFind->handle(self::CREATION_TIME_PROPERTYNAME, function() use ($node) {
408
-				return $node->getFileInfo()->getCreationTime();
409
-			});
410
-
411
-			$propFind->handle(self::UPLOAD_TIME_PROPERTYNAME, function() use ($node) {
412
-				return $node->getFileInfo()->getUploadTime();
413
-			});
414
-
415
-		}
416
-
417
-		if ($node instanceof \OCA\DAV\Connector\Sabre\Directory) {
418
-			$propFind->handle(self::SIZE_PROPERTYNAME, function() use ($node) {
419
-				return $node->getSize();
420
-			});
421
-
422
-			$propFind->handle(self::IS_ENCRYPTED_PROPERTYNAME, function() use ($node) {
423
-				return $node->getFileInfo()->isEncrypted() ? '1' : '0';
424
-			});
425
-		}
426
-	}
427
-
428
-	/**
429
-	 * translate Nextcloud permissions to OCM Permissions
430
-	 *
431
-	 * @param $ncPermissions
432
-	 * @return array
433
-	 */
434
-	protected function ncPermissions2ocmPermissions($ncPermissions) {
435
-
436
-		$ocmPermissions = [];
437
-
438
-		if ($ncPermissions & Constants::PERMISSION_SHARE) {
439
-			$ocmPermissions[] = 'share';
440
-		}
441
-
442
-		if ($ncPermissions & Constants::PERMISSION_READ) {
443
-			$ocmPermissions[] = 'read';
444
-		}
445
-
446
-		if (($ncPermissions & Constants::PERMISSION_CREATE) ||
447
-			($ncPermissions & Constants::PERMISSION_UPDATE)) {
448
-			$ocmPermissions[] = 'write';
449
-		}
450
-
451
-		return $ocmPermissions;
452
-
453
-	}
454
-
455
-	/**
456
-	 * Update ownCloud-specific properties
457
-	 *
458
-	 * @param string $path
459
-	 * @param PropPatch $propPatch
460
-	 *
461
-	 * @return void
462
-	 */
463
-	public function handleUpdateProperties($path, PropPatch $propPatch) {
464
-		$node = $this->tree->getNodeForPath($path);
465
-		if (!($node instanceof \OCA\DAV\Connector\Sabre\Node)) {
466
-			return;
467
-		}
468
-
469
-		$propPatch->handle(self::LASTMODIFIED_PROPERTYNAME, function($time) use ($node) {
470
-			if (empty($time)) {
471
-				return false;
472
-			}
473
-			$node->touch($time);
474
-			return true;
475
-		});
476
-		$propPatch->handle(self::GETETAG_PROPERTYNAME, function($etag) use ($node) {
477
-			if (empty($etag)) {
478
-				return false;
479
-			}
480
-			if ($node->setEtag($etag) !== -1) {
481
-				return true;
482
-			}
483
-			return false;
484
-		});
485
-		$propPatch->handle(self::CREATION_TIME_PROPERTYNAME, function($time) use ($node) {
486
-			if (empty($time)) {
487
-				return false;
488
-			}
489
-			$node->setCreationTime((int) $time);
490
-			return true;
491
-		});
492
-	}
493
-
494
-	/**
495
-	 * @param string $filePath
496
-	 * @param \Sabre\DAV\INode $node
497
-	 * @throws \Sabre\DAV\Exception\BadRequest
498
-	 */
499
-	public function sendFileIdHeader($filePath, \Sabre\DAV\INode $node = null) {
500
-		// chunked upload handling
501
-		if (isset($_SERVER['HTTP_OC_CHUNKED'])) {
502
-			list($path, $name) = \Sabre\Uri\split($filePath);
503
-			$info = \OC_FileChunking::decodeName($name);
504
-			if (!empty($info)) {
505
-				$filePath = $path . '/' . $info['name'];
506
-			}
507
-		}
508
-
509
-		// we get the node for the given $filePath here because in case of afterCreateFile $node is the parent folder
510
-		if (!$this->server->tree->nodeExists($filePath)) {
511
-			return;
512
-		}
513
-		$node = $this->server->tree->getNodeForPath($filePath);
514
-		if ($node instanceof \OCA\DAV\Connector\Sabre\Node) {
515
-			$fileId = $node->getFileId();
516
-			if (!is_null($fileId)) {
517
-				$this->server->httpResponse->setHeader('OC-FileId', $fileId);
518
-			}
519
-		}
520
-	}
305
+             */
306
+
307
+            $propFind->handle(self::FILEID_PROPERTYNAME, function() use ($node) {
308
+                return $node->getFileId();
309
+            });
310
+
311
+            $propFind->handle(self::INTERNAL_FILEID_PROPERTYNAME, function() use ($node) {
312
+                return $node->getInternalFileId();
313
+            });
314
+
315
+            $propFind->handle(self::PERMISSIONS_PROPERTYNAME, function() use ($node) {
316
+                $perms = $node->getDavPermissions();
317
+                if ($this->isPublic) {
318
+                    // remove mount information
319
+                    $perms = str_replace(['S', 'M'], '', $perms);
320
+                }
321
+                return $perms;
322
+            });
323
+
324
+            $propFind->handle(self::SHARE_PERMISSIONS_PROPERTYNAME, function() use ($node, $httpRequest) {
325
+                return $node->getSharePermissions(
326
+                    $httpRequest->getRawServerValue('PHP_AUTH_USER')
327
+                );
328
+            });
329
+
330
+            $propFind->handle(self::OCM_SHARE_PERMISSIONS_PROPERTYNAME, function() use ($node, $httpRequest) {
331
+                $ncPermissions = $node->getSharePermissions(
332
+                    $httpRequest->getRawServerValue('PHP_AUTH_USER')
333
+                );
334
+                $ocmPermissions = $this->ncPermissions2ocmPermissions($ncPermissions);
335
+                return json_encode($ocmPermissions);
336
+            });
337
+
338
+            $propFind->handle(self::GETETAG_PROPERTYNAME, function() use ($node) {
339
+                return $node->getETag();
340
+            });
341
+
342
+            $propFind->handle(self::OWNER_ID_PROPERTYNAME, function() use ($node) {
343
+                $owner = $node->getOwner();
344
+                if (!$owner) {
345
+                    return null;
346
+                } else {
347
+                    return $owner->getUID();
348
+                }
349
+            });
350
+            $propFind->handle(self::OWNER_DISPLAY_NAME_PROPERTYNAME, function() use ($node) {
351
+                $owner = $node->getOwner();
352
+                if (!$owner) {
353
+                    return null;
354
+                } else {
355
+                    return $owner->getDisplayName();
356
+                }
357
+            });
358
+
359
+            $propFind->handle(self::HAS_PREVIEW_PROPERTYNAME, function () use ($node) {
360
+                return json_encode($this->previewManager->isAvailable($node->getFileInfo()));
361
+            });
362
+            $propFind->handle(self::SIZE_PROPERTYNAME, function() use ($node) {
363
+                return $node->getSize();
364
+            });
365
+            $propFind->handle(self::MOUNT_TYPE_PROPERTYNAME, function () use ($node) {
366
+                return $node->getFileInfo()->getMountPoint()->getMountType();
367
+            });
368
+
369
+            $propFind->handle(self::SHARE_NOTE, function() use ($node, $httpRequest) {
370
+                return $node->getNoteFromShare(
371
+                    $httpRequest->getRawServerValue('PHP_AUTH_USER')
372
+                );
373
+            });
374
+        }
375
+
376
+        if ($node instanceof \OCA\DAV\Connector\Sabre\Node) {
377
+            $propFind->handle(self::DATA_FINGERPRINT_PROPERTYNAME, function() use ($node) {
378
+                return $this->config->getSystemValue('data-fingerprint', '');
379
+            });
380
+        }
381
+
382
+        if ($node instanceof \OCA\DAV\Connector\Sabre\File) {
383
+            $propFind->handle(self::DOWNLOADURL_PROPERTYNAME, function() use ($node) {
384
+                /** @var $node \OCA\DAV\Connector\Sabre\File */
385
+                try {
386
+                    $directDownloadUrl = $node->getDirectDownload();
387
+                    if (isset($directDownloadUrl['url'])) {
388
+                        return $directDownloadUrl['url'];
389
+                    }
390
+                } catch (StorageNotAvailableException $e) {
391
+                    return false;
392
+                } catch (ForbiddenException $e) {
393
+                    return false;
394
+                }
395
+                return false;
396
+            });
397
+
398
+            $propFind->handle(self::CHECKSUMS_PROPERTYNAME, function() use ($node) {
399
+                $checksum = $node->getChecksum();
400
+                if ($checksum === null || $checksum === '') {
401
+                    return null;
402
+                }
403
+
404
+                return new ChecksumList($checksum);
405
+            });
406
+
407
+            $propFind->handle(self::CREATION_TIME_PROPERTYNAME, function() use ($node) {
408
+                return $node->getFileInfo()->getCreationTime();
409
+            });
410
+
411
+            $propFind->handle(self::UPLOAD_TIME_PROPERTYNAME, function() use ($node) {
412
+                return $node->getFileInfo()->getUploadTime();
413
+            });
414
+
415
+        }
416
+
417
+        if ($node instanceof \OCA\DAV\Connector\Sabre\Directory) {
418
+            $propFind->handle(self::SIZE_PROPERTYNAME, function() use ($node) {
419
+                return $node->getSize();
420
+            });
421
+
422
+            $propFind->handle(self::IS_ENCRYPTED_PROPERTYNAME, function() use ($node) {
423
+                return $node->getFileInfo()->isEncrypted() ? '1' : '0';
424
+            });
425
+        }
426
+    }
427
+
428
+    /**
429
+     * translate Nextcloud permissions to OCM Permissions
430
+     *
431
+     * @param $ncPermissions
432
+     * @return array
433
+     */
434
+    protected function ncPermissions2ocmPermissions($ncPermissions) {
435
+
436
+        $ocmPermissions = [];
437
+
438
+        if ($ncPermissions & Constants::PERMISSION_SHARE) {
439
+            $ocmPermissions[] = 'share';
440
+        }
441
+
442
+        if ($ncPermissions & Constants::PERMISSION_READ) {
443
+            $ocmPermissions[] = 'read';
444
+        }
445
+
446
+        if (($ncPermissions & Constants::PERMISSION_CREATE) ||
447
+            ($ncPermissions & Constants::PERMISSION_UPDATE)) {
448
+            $ocmPermissions[] = 'write';
449
+        }
450
+
451
+        return $ocmPermissions;
452
+
453
+    }
454
+
455
+    /**
456
+     * Update ownCloud-specific properties
457
+     *
458
+     * @param string $path
459
+     * @param PropPatch $propPatch
460
+     *
461
+     * @return void
462
+     */
463
+    public function handleUpdateProperties($path, PropPatch $propPatch) {
464
+        $node = $this->tree->getNodeForPath($path);
465
+        if (!($node instanceof \OCA\DAV\Connector\Sabre\Node)) {
466
+            return;
467
+        }
468
+
469
+        $propPatch->handle(self::LASTMODIFIED_PROPERTYNAME, function($time) use ($node) {
470
+            if (empty($time)) {
471
+                return false;
472
+            }
473
+            $node->touch($time);
474
+            return true;
475
+        });
476
+        $propPatch->handle(self::GETETAG_PROPERTYNAME, function($etag) use ($node) {
477
+            if (empty($etag)) {
478
+                return false;
479
+            }
480
+            if ($node->setEtag($etag) !== -1) {
481
+                return true;
482
+            }
483
+            return false;
484
+        });
485
+        $propPatch->handle(self::CREATION_TIME_PROPERTYNAME, function($time) use ($node) {
486
+            if (empty($time)) {
487
+                return false;
488
+            }
489
+            $node->setCreationTime((int) $time);
490
+            return true;
491
+        });
492
+    }
493
+
494
+    /**
495
+     * @param string $filePath
496
+     * @param \Sabre\DAV\INode $node
497
+     * @throws \Sabre\DAV\Exception\BadRequest
498
+     */
499
+    public function sendFileIdHeader($filePath, \Sabre\DAV\INode $node = null) {
500
+        // chunked upload handling
501
+        if (isset($_SERVER['HTTP_OC_CHUNKED'])) {
502
+            list($path, $name) = \Sabre\Uri\split($filePath);
503
+            $info = \OC_FileChunking::decodeName($name);
504
+            if (!empty($info)) {
505
+                $filePath = $path . '/' . $info['name'];
506
+            }
507
+        }
508
+
509
+        // we get the node for the given $filePath here because in case of afterCreateFile $node is the parent folder
510
+        if (!$this->server->tree->nodeExists($filePath)) {
511
+            return;
512
+        }
513
+        $node = $this->server->tree->getNodeForPath($filePath);
514
+        if ($node instanceof \OCA\DAV\Connector\Sabre\Node) {
515
+            $fileId = $node->getFileId();
516
+            if (!is_null($fileId)) {
517
+                $this->server->httpResponse->setHeader('OC-FileId', $fileId);
518
+            }
519
+        }
520
+    }
521 521
 }
Please login to merge, or discard this patch.
Spacing   +9 added lines, -9 removed lines patch added patch discarded remove patch
@@ -177,7 +177,7 @@  discard block
 block discarded – undo
177 177
 		$this->server->on('propPatch', [$this, 'handleUpdateProperties']);
178 178
 		$this->server->on('afterBind', [$this, 'sendFileIdHeader']);
179 179
 		$this->server->on('afterWriteContent', [$this, 'sendFileIdHeader']);
180
-		$this->server->on('afterMethod:GET', [$this,'httpGet']);
180
+		$this->server->on('afterMethod:GET', [$this, 'httpGet']);
181 181
 		$this->server->on('afterMethod:GET', [$this, 'handleDownloadToken']);
182 182
 		$this->server->on('afterResponse', function($request, ResponseInterface $response) {
183 183
 			$body = $response->getBody();
@@ -207,11 +207,11 @@  discard block
 block discarded – undo
207 207
 		if ($sourceDir !== $destinationDir) {
208 208
 			$sourceNodeFileInfo = $sourceNode->getFileInfo();
209 209
 			if ($sourceNodeFileInfo === null) {
210
-				throw new NotFound($source . ' does not exist');
210
+				throw new NotFound($source.' does not exist');
211 211
  			}
212 212
 
213 213
 			if (!$sourceNodeFileInfo->isDeletable()) {
214
-				throw new Forbidden($source . " cannot be deleted");
214
+				throw new Forbidden($source." cannot be deleted");
215 215
 			}
216 216
 		}
217 217
 	}
@@ -264,10 +264,10 @@  discard block
 block discarded – undo
264 264
 					Request::USER_AGENT_ANDROID_MOBILE_CHROME,
265 265
 					Request::USER_AGENT_FREEBOX,
266 266
 				])) {
267
-				$response->addHeader('Content-Disposition', 'attachment; filename="' . rawurlencode($filename) . '"');
267
+				$response->addHeader('Content-Disposition', 'attachment; filename="'.rawurlencode($filename).'"');
268 268
 			} else {
269
-				$response->addHeader('Content-Disposition', 'attachment; filename*=UTF-8\'\'' . rawurlencode($filename)
270
-													 . '; filename="' . rawurlencode($filename) . '"');
269
+				$response->addHeader('Content-Disposition', 'attachment; filename*=UTF-8\'\''.rawurlencode($filename)
270
+													 . '; filename="'.rawurlencode($filename).'"');
271 271
 			}
272 272
 		}
273 273
 
@@ -356,13 +356,13 @@  discard block
 block discarded – undo
356 356
 				}
357 357
 			});
358 358
 
359
-			$propFind->handle(self::HAS_PREVIEW_PROPERTYNAME, function () use ($node) {
359
+			$propFind->handle(self::HAS_PREVIEW_PROPERTYNAME, function() use ($node) {
360 360
 				return json_encode($this->previewManager->isAvailable($node->getFileInfo()));
361 361
 			});
362 362
 			$propFind->handle(self::SIZE_PROPERTYNAME, function() use ($node) {
363 363
 				return $node->getSize();
364 364
 			});
365
-			$propFind->handle(self::MOUNT_TYPE_PROPERTYNAME, function () use ($node) {
365
+			$propFind->handle(self::MOUNT_TYPE_PROPERTYNAME, function() use ($node) {
366 366
 				return $node->getFileInfo()->getMountPoint()->getMountType();
367 367
 			});
368 368
 
@@ -502,7 +502,7 @@  discard block
 block discarded – undo
502 502
 			list($path, $name) = \Sabre\Uri\split($filePath);
503 503
 			$info = \OC_FileChunking::decodeName($name);
504 504
 			if (!empty($info)) {
505
-				$filePath = $path . '/' . $info['name'];
505
+				$filePath = $path.'/'.$info['name'];
506 506
 			}
507 507
 		}
508 508
 
Please login to merge, or discard this patch.
apps/dav/lib/Connector/Sabre/Node.php 1 patch
Indentation   +363 added lines, -363 removed lines patch added patch discarded remove patch
@@ -49,373 +49,373 @@
 block discarded – undo
49 49
 
50 50
 abstract class Node implements \Sabre\DAV\INode {
51 51
 
52
-	/**
53
-	 * @var \OC\Files\View
54
-	 */
55
-	protected $fileView;
56
-
57
-	/**
58
-	 * The path to the current node
59
-	 *
60
-	 * @var string
61
-	 */
62
-	protected $path;
63
-
64
-	/**
65
-	 * node properties cache
66
-	 *
67
-	 * @var array
68
-	 */
69
-	protected $property_cache = null;
70
-
71
-	/**
72
-	 * @var \OCP\Files\FileInfo
73
-	 */
74
-	protected $info;
75
-
76
-	/**
77
-	 * @var IManager
78
-	 */
79
-	protected $shareManager;
80
-
81
-	/**
82
-	 * Sets up the node, expects a full path name
83
-	 *
84
-	 * @param \OC\Files\View $view
85
-	 * @param \OCP\Files\FileInfo $info
86
-	 * @param IManager $shareManager
87
-	 */
88
-	public function __construct(View $view, FileInfo $info, IManager $shareManager = null) {
89
-		$this->fileView = $view;
90
-		$this->path = $this->fileView->getRelativePath($info->getPath());
91
-		$this->info = $info;
92
-		if ($shareManager) {
93
-			$this->shareManager = $shareManager;
94
-		} else {
95
-			$this->shareManager = \OC::$server->getShareManager();
96
-		}
97
-	}
98
-
99
-	protected function refreshInfo() {
100
-		$this->info = $this->fileView->getFileInfo($this->path);
101
-	}
102
-
103
-	/**
104
-	 *  Returns the name of the node
105
-	 *
106
-	 * @return string
107
-	 */
108
-	public function getName() {
109
-		return $this->info->getName();
110
-	}
111
-
112
-	/**
113
-	 * Returns the full path
114
-	 *
115
-	 * @return string
116
-	 */
117
-	public function getPath() {
118
-		return $this->path;
119
-	}
120
-
121
-	/**
122
-	 * Renames the node
123
-	 *
124
-	 * @param string $name The new name
125
-	 * @throws \Sabre\DAV\Exception\BadRequest
126
-	 * @throws \Sabre\DAV\Exception\Forbidden
127
-	 */
128
-	public function setName($name) {
129
-
130
-		// rename is only allowed if the update privilege is granted
131
-		if (!$this->info->isUpdateable()) {
132
-			throw new \Sabre\DAV\Exception\Forbidden();
133
-		}
134
-
135
-		list($parentPath,) = \Sabre\Uri\split($this->path);
136
-		list(, $newName) = \Sabre\Uri\split($name);
137
-
138
-		// verify path of the target
139
-		$this->verifyPath();
140
-
141
-		$newPath = $parentPath . '/' . $newName;
142
-
143
-		if (!$this->fileView->rename($this->path, $newPath)) {
144
-			throw new \Sabre\DAV\Exception('Failed to rename '. $this->path . ' to ' . $newPath);
145
-		}
146
-
147
-		$this->path = $newPath;
148
-
149
-		$this->refreshInfo();
150
-	}
151
-
152
-	public function setPropertyCache($property_cache) {
153
-		$this->property_cache = $property_cache;
154
-	}
155
-
156
-	/**
157
-	 * Returns the last modification time, as a unix timestamp
158
-	 *
159
-	 * @return int timestamp as integer
160
-	 */
161
-	public function getLastModified() {
162
-		$timestamp = $this->info->getMtime();
163
-		if (!empty($timestamp)) {
164
-			return (int)$timestamp;
165
-		}
166
-		return $timestamp;
167
-	}
168
-
169
-	/**
170
-	 *  sets the last modification time of the file (mtime) to the value given
171
-	 *  in the second parameter or to now if the second param is empty.
172
-	 *  Even if the modification time is set to a custom value the access time is set to now.
173
-	 */
174
-	public function touch($mtime) {
175
-		$mtime = $this->sanitizeMtime($mtime);
176
-		$this->fileView->touch($this->path, $mtime);
177
-		$this->refreshInfo();
178
-	}
179
-
180
-	/**
181
-	 * Returns the ETag for a file
182
-	 *
183
-	 * An ETag is a unique identifier representing the current version of the
184
-	 * file. If the file changes, the ETag MUST change.  The ETag is an
185
-	 * arbitrary string, but MUST be surrounded by double-quotes.
186
-	 *
187
-	 * Return null if the ETag can not effectively be determined
188
-	 *
189
-	 * @return string
190
-	 */
191
-	public function getETag() {
192
-		return '"' . $this->info->getEtag() . '"';
193
-	}
194
-
195
-	/**
196
-	 * Sets the ETag
197
-	 *
198
-	 * @param string $etag
199
-	 *
200
-	 * @return int file id of updated file or -1 on failure
201
-	 */
202
-	public function setETag($etag) {
203
-		return $this->fileView->putFileInfo($this->path, ['etag' => $etag]);
204
-	}
205
-
206
-	public function setCreationTime(int $time) {
207
-		return $this->fileView->putFileInfo($this->path, ['creation_time' => $time]);
208
-	}
209
-
210
-	public function setUploadTime(int $time) {
211
-		return $this->fileView->putFileInfo($this->path, ['upload_time' => $time]);
212
-	}
213
-
214
-	/**
215
-	 * Returns the size of the node, in bytes
216
-	 *
217
-	 * @return integer
218
-	 */
219
-	public function getSize() {
220
-		return $this->info->getSize();
221
-	}
222
-
223
-	/**
224
-	 * Returns the cache's file id
225
-	 *
226
-	 * @return int
227
-	 */
228
-	public function getId() {
229
-		return $this->info->getId();
230
-	}
231
-
232
-	/**
233
-	 * @return string|null
234
-	 */
235
-	public function getFileId() {
236
-		if ($this->info->getId()) {
237
-			$instanceId = \OC_Util::getInstanceId();
238
-			$id = sprintf('%08d', $this->info->getId());
239
-			return $id . $instanceId;
240
-		}
241
-
242
-		return null;
243
-	}
244
-
245
-	/**
246
-	 * @return integer
247
-	 */
248
-	public function getInternalFileId() {
249
-		return $this->info->getId();
250
-	}
251
-
252
-	/**
253
-	 * @param string $user
254
-	 * @return int
255
-	 */
256
-	public function getSharePermissions($user) {
257
-
258
-		// check of we access a federated share
259
-		if ($user !== null) {
260
-			try {
261
-				$share = $this->shareManager->getShareByToken($user);
262
-				return $share->getPermissions();
263
-			} catch (ShareNotFound $e) {
264
-				// ignore
265
-			}
266
-		}
267
-
268
-		try {
269
-			$storage = $this->info->getStorage();
270
-		} catch (StorageNotAvailableException $e) {
271
-			$storage = null;
272
-		}
273
-
274
-		if ($storage && $storage->instanceOfStorage('\OCA\Files_Sharing\SharedStorage')) {
275
-			/** @var \OCA\Files_Sharing\SharedStorage $storage */
276
-			$permissions = (int)$storage->getShare()->getPermissions();
277
-		} else {
278
-			$permissions = $this->info->getPermissions();
279
-		}
280
-
281
-		/*
52
+    /**
53
+     * @var \OC\Files\View
54
+     */
55
+    protected $fileView;
56
+
57
+    /**
58
+     * The path to the current node
59
+     *
60
+     * @var string
61
+     */
62
+    protected $path;
63
+
64
+    /**
65
+     * node properties cache
66
+     *
67
+     * @var array
68
+     */
69
+    protected $property_cache = null;
70
+
71
+    /**
72
+     * @var \OCP\Files\FileInfo
73
+     */
74
+    protected $info;
75
+
76
+    /**
77
+     * @var IManager
78
+     */
79
+    protected $shareManager;
80
+
81
+    /**
82
+     * Sets up the node, expects a full path name
83
+     *
84
+     * @param \OC\Files\View $view
85
+     * @param \OCP\Files\FileInfo $info
86
+     * @param IManager $shareManager
87
+     */
88
+    public function __construct(View $view, FileInfo $info, IManager $shareManager = null) {
89
+        $this->fileView = $view;
90
+        $this->path = $this->fileView->getRelativePath($info->getPath());
91
+        $this->info = $info;
92
+        if ($shareManager) {
93
+            $this->shareManager = $shareManager;
94
+        } else {
95
+            $this->shareManager = \OC::$server->getShareManager();
96
+        }
97
+    }
98
+
99
+    protected function refreshInfo() {
100
+        $this->info = $this->fileView->getFileInfo($this->path);
101
+    }
102
+
103
+    /**
104
+     *  Returns the name of the node
105
+     *
106
+     * @return string
107
+     */
108
+    public function getName() {
109
+        return $this->info->getName();
110
+    }
111
+
112
+    /**
113
+     * Returns the full path
114
+     *
115
+     * @return string
116
+     */
117
+    public function getPath() {
118
+        return $this->path;
119
+    }
120
+
121
+    /**
122
+     * Renames the node
123
+     *
124
+     * @param string $name The new name
125
+     * @throws \Sabre\DAV\Exception\BadRequest
126
+     * @throws \Sabre\DAV\Exception\Forbidden
127
+     */
128
+    public function setName($name) {
129
+
130
+        // rename is only allowed if the update privilege is granted
131
+        if (!$this->info->isUpdateable()) {
132
+            throw new \Sabre\DAV\Exception\Forbidden();
133
+        }
134
+
135
+        list($parentPath,) = \Sabre\Uri\split($this->path);
136
+        list(, $newName) = \Sabre\Uri\split($name);
137
+
138
+        // verify path of the target
139
+        $this->verifyPath();
140
+
141
+        $newPath = $parentPath . '/' . $newName;
142
+
143
+        if (!$this->fileView->rename($this->path, $newPath)) {
144
+            throw new \Sabre\DAV\Exception('Failed to rename '. $this->path . ' to ' . $newPath);
145
+        }
146
+
147
+        $this->path = $newPath;
148
+
149
+        $this->refreshInfo();
150
+    }
151
+
152
+    public function setPropertyCache($property_cache) {
153
+        $this->property_cache = $property_cache;
154
+    }
155
+
156
+    /**
157
+     * Returns the last modification time, as a unix timestamp
158
+     *
159
+     * @return int timestamp as integer
160
+     */
161
+    public function getLastModified() {
162
+        $timestamp = $this->info->getMtime();
163
+        if (!empty($timestamp)) {
164
+            return (int)$timestamp;
165
+        }
166
+        return $timestamp;
167
+    }
168
+
169
+    /**
170
+     *  sets the last modification time of the file (mtime) to the value given
171
+     *  in the second parameter or to now if the second param is empty.
172
+     *  Even if the modification time is set to a custom value the access time is set to now.
173
+     */
174
+    public function touch($mtime) {
175
+        $mtime = $this->sanitizeMtime($mtime);
176
+        $this->fileView->touch($this->path, $mtime);
177
+        $this->refreshInfo();
178
+    }
179
+
180
+    /**
181
+     * Returns the ETag for a file
182
+     *
183
+     * An ETag is a unique identifier representing the current version of the
184
+     * file. If the file changes, the ETag MUST change.  The ETag is an
185
+     * arbitrary string, but MUST be surrounded by double-quotes.
186
+     *
187
+     * Return null if the ETag can not effectively be determined
188
+     *
189
+     * @return string
190
+     */
191
+    public function getETag() {
192
+        return '"' . $this->info->getEtag() . '"';
193
+    }
194
+
195
+    /**
196
+     * Sets the ETag
197
+     *
198
+     * @param string $etag
199
+     *
200
+     * @return int file id of updated file or -1 on failure
201
+     */
202
+    public function setETag($etag) {
203
+        return $this->fileView->putFileInfo($this->path, ['etag' => $etag]);
204
+    }
205
+
206
+    public function setCreationTime(int $time) {
207
+        return $this->fileView->putFileInfo($this->path, ['creation_time' => $time]);
208
+    }
209
+
210
+    public function setUploadTime(int $time) {
211
+        return $this->fileView->putFileInfo($this->path, ['upload_time' => $time]);
212
+    }
213
+
214
+    /**
215
+     * Returns the size of the node, in bytes
216
+     *
217
+     * @return integer
218
+     */
219
+    public function getSize() {
220
+        return $this->info->getSize();
221
+    }
222
+
223
+    /**
224
+     * Returns the cache's file id
225
+     *
226
+     * @return int
227
+     */
228
+    public function getId() {
229
+        return $this->info->getId();
230
+    }
231
+
232
+    /**
233
+     * @return string|null
234
+     */
235
+    public function getFileId() {
236
+        if ($this->info->getId()) {
237
+            $instanceId = \OC_Util::getInstanceId();
238
+            $id = sprintf('%08d', $this->info->getId());
239
+            return $id . $instanceId;
240
+        }
241
+
242
+        return null;
243
+    }
244
+
245
+    /**
246
+     * @return integer
247
+     */
248
+    public function getInternalFileId() {
249
+        return $this->info->getId();
250
+    }
251
+
252
+    /**
253
+     * @param string $user
254
+     * @return int
255
+     */
256
+    public function getSharePermissions($user) {
257
+
258
+        // check of we access a federated share
259
+        if ($user !== null) {
260
+            try {
261
+                $share = $this->shareManager->getShareByToken($user);
262
+                return $share->getPermissions();
263
+            } catch (ShareNotFound $e) {
264
+                // ignore
265
+            }
266
+        }
267
+
268
+        try {
269
+            $storage = $this->info->getStorage();
270
+        } catch (StorageNotAvailableException $e) {
271
+            $storage = null;
272
+        }
273
+
274
+        if ($storage && $storage->instanceOfStorage('\OCA\Files_Sharing\SharedStorage')) {
275
+            /** @var \OCA\Files_Sharing\SharedStorage $storage */
276
+            $permissions = (int)$storage->getShare()->getPermissions();
277
+        } else {
278
+            $permissions = $this->info->getPermissions();
279
+        }
280
+
281
+        /*
282 282
 		 * We can always share non moveable mount points with DELETE and UPDATE
283 283
 		 * Eventually we need to do this properly
284 284
 		 */
285
-		$mountpoint = $this->info->getMountPoint();
286
-		if (!($mountpoint instanceof MoveableMount)) {
287
-			$mountpointpath = $mountpoint->getMountPoint();
288
-			if (substr($mountpointpath, -1) === '/') {
289
-				$mountpointpath = substr($mountpointpath, 0, -1);
290
-			}
291
-
292
-			if (!$mountpoint->getOption('readonly', false) && $mountpointpath === $this->info->getPath()) {
293
-				$permissions |= \OCP\Constants::PERMISSION_DELETE | \OCP\Constants::PERMISSION_UPDATE;
294
-			}
295
-		}
296
-
297
-		/*
285
+        $mountpoint = $this->info->getMountPoint();
286
+        if (!($mountpoint instanceof MoveableMount)) {
287
+            $mountpointpath = $mountpoint->getMountPoint();
288
+            if (substr($mountpointpath, -1) === '/') {
289
+                $mountpointpath = substr($mountpointpath, 0, -1);
290
+            }
291
+
292
+            if (!$mountpoint->getOption('readonly', false) && $mountpointpath === $this->info->getPath()) {
293
+                $permissions |= \OCP\Constants::PERMISSION_DELETE | \OCP\Constants::PERMISSION_UPDATE;
294
+            }
295
+        }
296
+
297
+        /*
298 298
 		 * Files can't have create or delete permissions
299 299
 		 */
300
-		if ($this->info->getType() === \OCP\Files\FileInfo::TYPE_FILE) {
301
-			$permissions &= ~(\OCP\Constants::PERMISSION_CREATE | \OCP\Constants::PERMISSION_DELETE);
302
-		}
303
-
304
-		return $permissions;
305
-	}
306
-
307
-	/**
308
-	 * @param string $user
309
-	 * @return string
310
-	 */
311
-	public function getNoteFromShare($user) {
312
-		if ($user === null) {
313
-			return '';
314
-		}
315
-
316
-		$types = [
317
-			Share::SHARE_TYPE_USER,
318
-			Share::SHARE_TYPE_GROUP,
319
-			Share::SHARE_TYPE_CIRCLE,
320
-			Share::SHARE_TYPE_ROOM
321
-		];
322
-
323
-		foreach ($types as $shareType) {
324
-			$shares = $this->shareManager->getSharedWith($user, $shareType, $this, -1);
325
-			foreach ($shares as $share) {
326
-				$note = $share->getNote();
327
-				if($share->getShareOwner() !== $user && !empty($note)) {
328
-					return $note;
329
-				}
330
-			}
331
-		}
332
-
333
-		return '';
334
-	}
335
-
336
-	/**
337
-	 * @return string
338
-	 */
339
-	public function getDavPermissions() {
340
-		$p = '';
341
-		if ($this->info->isShared()) {
342
-			$p .= 'S';
343
-		}
344
-		if ($this->info->isShareable()) {
345
-			$p .= 'R';
346
-		}
347
-		if ($this->info->isMounted()) {
348
-			$p .= 'M';
349
-		}
350
-		if ($this->info->isReadable()) {
351
-			$p .= 'G';
352
-		}
353
-		if ($this->info->isDeletable()) {
354
-			$p .= 'D';
355
-		}
356
-		if ($this->info->isUpdateable()) {
357
-			$p .= 'NV'; // Renameable, Moveable
358
-		}
359
-		if ($this->info->getType() === \OCP\Files\FileInfo::TYPE_FILE) {
360
-			if ($this->info->isUpdateable()) {
361
-				$p .= 'W';
362
-			}
363
-		} else {
364
-			if ($this->info->isCreatable()) {
365
-				$p .= 'CK';
366
-			}
367
-		}
368
-		return $p;
369
-	}
370
-
371
-	public function getOwner() {
372
-		return $this->info->getOwner();
373
-	}
374
-
375
-	protected function verifyPath() {
376
-		try {
377
-			$fileName = basename($this->info->getPath());
378
-			$this->fileView->verifyPath($this->path, $fileName);
379
-		} catch (\OCP\Files\InvalidPathException $ex) {
380
-			throw new InvalidPath($ex->getMessage());
381
-		}
382
-	}
383
-
384
-	/**
385
-	 * @param int $type \OCP\Lock\ILockingProvider::LOCK_SHARED or \OCP\Lock\ILockingProvider::LOCK_EXCLUSIVE
386
-	 */
387
-	public function acquireLock($type) {
388
-		$this->fileView->lockFile($this->path, $type);
389
-	}
390
-
391
-	/**
392
-	 * @param int $type \OCP\Lock\ILockingProvider::LOCK_SHARED or \OCP\Lock\ILockingProvider::LOCK_EXCLUSIVE
393
-	 */
394
-	public function releaseLock($type) {
395
-		$this->fileView->unlockFile($this->path, $type);
396
-	}
397
-
398
-	/**
399
-	 * @param int $type \OCP\Lock\ILockingProvider::LOCK_SHARED or \OCP\Lock\ILockingProvider::LOCK_EXCLUSIVE
400
-	 */
401
-	public function changeLock($type) {
402
-		$this->fileView->changeLock($this->path, $type);
403
-	}
404
-
405
-	public function getFileInfo() {
406
-		return $this->info;
407
-	}
408
-
409
-	protected function sanitizeMtime($mtimeFromRequest) {
410
-		// In PHP 5.X "is_numeric" returns true for strings in hexadecimal
411
-		// notation. This is no longer the case in PHP 7.X, so this check
412
-		// ensures that strings with hexadecimal notations fail too in PHP 5.X.
413
-		$isHexadecimal = is_string($mtimeFromRequest) && preg_match('/^\s*0[xX]/', $mtimeFromRequest);
414
-		if ($isHexadecimal || !is_numeric($mtimeFromRequest)) {
415
-			throw new \InvalidArgumentException('X-OC-MTime header must be an integer (unix timestamp).');
416
-		}
417
-
418
-		return (int)$mtimeFromRequest;
419
-	}
300
+        if ($this->info->getType() === \OCP\Files\FileInfo::TYPE_FILE) {
301
+            $permissions &= ~(\OCP\Constants::PERMISSION_CREATE | \OCP\Constants::PERMISSION_DELETE);
302
+        }
303
+
304
+        return $permissions;
305
+    }
306
+
307
+    /**
308
+     * @param string $user
309
+     * @return string
310
+     */
311
+    public function getNoteFromShare($user) {
312
+        if ($user === null) {
313
+            return '';
314
+        }
315
+
316
+        $types = [
317
+            Share::SHARE_TYPE_USER,
318
+            Share::SHARE_TYPE_GROUP,
319
+            Share::SHARE_TYPE_CIRCLE,
320
+            Share::SHARE_TYPE_ROOM
321
+        ];
322
+
323
+        foreach ($types as $shareType) {
324
+            $shares = $this->shareManager->getSharedWith($user, $shareType, $this, -1);
325
+            foreach ($shares as $share) {
326
+                $note = $share->getNote();
327
+                if($share->getShareOwner() !== $user && !empty($note)) {
328
+                    return $note;
329
+                }
330
+            }
331
+        }
332
+
333
+        return '';
334
+    }
335
+
336
+    /**
337
+     * @return string
338
+     */
339
+    public function getDavPermissions() {
340
+        $p = '';
341
+        if ($this->info->isShared()) {
342
+            $p .= 'S';
343
+        }
344
+        if ($this->info->isShareable()) {
345
+            $p .= 'R';
346
+        }
347
+        if ($this->info->isMounted()) {
348
+            $p .= 'M';
349
+        }
350
+        if ($this->info->isReadable()) {
351
+            $p .= 'G';
352
+        }
353
+        if ($this->info->isDeletable()) {
354
+            $p .= 'D';
355
+        }
356
+        if ($this->info->isUpdateable()) {
357
+            $p .= 'NV'; // Renameable, Moveable
358
+        }
359
+        if ($this->info->getType() === \OCP\Files\FileInfo::TYPE_FILE) {
360
+            if ($this->info->isUpdateable()) {
361
+                $p .= 'W';
362
+            }
363
+        } else {
364
+            if ($this->info->isCreatable()) {
365
+                $p .= 'CK';
366
+            }
367
+        }
368
+        return $p;
369
+    }
370
+
371
+    public function getOwner() {
372
+        return $this->info->getOwner();
373
+    }
374
+
375
+    protected function verifyPath() {
376
+        try {
377
+            $fileName = basename($this->info->getPath());
378
+            $this->fileView->verifyPath($this->path, $fileName);
379
+        } catch (\OCP\Files\InvalidPathException $ex) {
380
+            throw new InvalidPath($ex->getMessage());
381
+        }
382
+    }
383
+
384
+    /**
385
+     * @param int $type \OCP\Lock\ILockingProvider::LOCK_SHARED or \OCP\Lock\ILockingProvider::LOCK_EXCLUSIVE
386
+     */
387
+    public function acquireLock($type) {
388
+        $this->fileView->lockFile($this->path, $type);
389
+    }
390
+
391
+    /**
392
+     * @param int $type \OCP\Lock\ILockingProvider::LOCK_SHARED or \OCP\Lock\ILockingProvider::LOCK_EXCLUSIVE
393
+     */
394
+    public function releaseLock($type) {
395
+        $this->fileView->unlockFile($this->path, $type);
396
+    }
397
+
398
+    /**
399
+     * @param int $type \OCP\Lock\ILockingProvider::LOCK_SHARED or \OCP\Lock\ILockingProvider::LOCK_EXCLUSIVE
400
+     */
401
+    public function changeLock($type) {
402
+        $this->fileView->changeLock($this->path, $type);
403
+    }
404
+
405
+    public function getFileInfo() {
406
+        return $this->info;
407
+    }
408
+
409
+    protected function sanitizeMtime($mtimeFromRequest) {
410
+        // In PHP 5.X "is_numeric" returns true for strings in hexadecimal
411
+        // notation. This is no longer the case in PHP 7.X, so this check
412
+        // ensures that strings with hexadecimal notations fail too in PHP 5.X.
413
+        $isHexadecimal = is_string($mtimeFromRequest) && preg_match('/^\s*0[xX]/', $mtimeFromRequest);
414
+        if ($isHexadecimal || !is_numeric($mtimeFromRequest)) {
415
+            throw new \InvalidArgumentException('X-OC-MTime header must be an integer (unix timestamp).');
416
+        }
417
+
418
+        return (int)$mtimeFromRequest;
419
+    }
420 420
 
421 421
 }
Please login to merge, or discard this patch.
apps/dav/lib/Connector/Sabre/AppEnabledPlugin.php 1 patch
Indentation   +48 added lines, -48 removed lines patch added patch discarded remove patch
@@ -34,58 +34,58 @@
 block discarded – undo
34 34
  */
35 35
 class AppEnabledPlugin extends ServerPlugin {
36 36
 
37
-	/**
38
-	 * Reference to main server object
39
-	 *
40
-	 * @var \Sabre\DAV\Server
41
-	 */
42
-	private $server;
37
+    /**
38
+     * Reference to main server object
39
+     *
40
+     * @var \Sabre\DAV\Server
41
+     */
42
+    private $server;
43 43
 
44
-	/**
45
-	 * @var string
46
-	 */
47
-	private $app;
44
+    /**
45
+     * @var string
46
+     */
47
+    private $app;
48 48
 
49
-	/**
50
-	 * @var \OCP\App\IAppManager
51
-	 */
52
-	private $appManager;
49
+    /**
50
+     * @var \OCP\App\IAppManager
51
+     */
52
+    private $appManager;
53 53
 
54
-	/**
55
-	 * @param string $app
56
-	 * @param \OCP\App\IAppManager $appManager
57
-	 */
58
-	public function __construct($app, IAppManager $appManager) {
59
-		$this->app = $app;
60
-		$this->appManager = $appManager;
61
-	}
54
+    /**
55
+     * @param string $app
56
+     * @param \OCP\App\IAppManager $appManager
57
+     */
58
+    public function __construct($app, IAppManager $appManager) {
59
+        $this->app = $app;
60
+        $this->appManager = $appManager;
61
+    }
62 62
 
63
-	/**
64
-	 * This initializes the plugin.
65
-	 *
66
-	 * This function is called by \Sabre\DAV\Server, after
67
-	 * addPlugin is called.
68
-	 *
69
-	 * This method should set up the required event subscriptions.
70
-	 *
71
-	 * @param \Sabre\DAV\Server $server
72
-	 * @return void
73
-	 */
74
-	public function initialize(\Sabre\DAV\Server $server) {
63
+    /**
64
+     * This initializes the plugin.
65
+     *
66
+     * This function is called by \Sabre\DAV\Server, after
67
+     * addPlugin is called.
68
+     *
69
+     * This method should set up the required event subscriptions.
70
+     *
71
+     * @param \Sabre\DAV\Server $server
72
+     * @return void
73
+     */
74
+    public function initialize(\Sabre\DAV\Server $server) {
75 75
 
76
-		$this->server = $server;
77
-		$this->server->on('beforeMethod', [$this, 'checkAppEnabled'], 30);
78
-	}
76
+        $this->server = $server;
77
+        $this->server->on('beforeMethod', [$this, 'checkAppEnabled'], 30);
78
+    }
79 79
 
80
-	/**
81
-	 * This method is called before any HTTP after auth and checks if the user has access to the app
82
-	 *
83
-	 * @throws \Sabre\DAV\Exception\Forbidden
84
-	 * @return bool
85
-	 */
86
-	public function checkAppEnabled() {
87
-		if (!$this->appManager->isEnabledForUser($this->app)) {
88
-			throw new Forbidden();
89
-		}
90
-	}
80
+    /**
81
+     * This method is called before any HTTP after auth and checks if the user has access to the app
82
+     *
83
+     * @throws \Sabre\DAV\Exception\Forbidden
84
+     * @return bool
85
+     */
86
+    public function checkAppEnabled() {
87
+        if (!$this->appManager->isEnabledForUser($this->app)) {
88
+            throw new Forbidden();
89
+        }
90
+    }
91 91
 }
Please login to merge, or discard this patch.
apps/dav/lib/Connector/Sabre/ExceptionLoggerPlugin.php 1 patch
Indentation   +78 added lines, -78 removed lines patch added patch discarded remove patch
@@ -42,89 +42,89 @@
 block discarded – undo
42 42
 use Sabre\DAV\Exception\ServiceUnavailable;
43 43
 
44 44
 class ExceptionLoggerPlugin extends \Sabre\DAV\ServerPlugin {
45
-	protected $nonFatalExceptions = [
46
-		NotAuthenticated::class => true,
47
-		// If tokenauth can throw this exception (which is basically as
48
-		// NotAuthenticated. So not fatal.
49
-		PasswordLoginForbidden::class => true,
50
-		// basically a NotAuthenticated
51
-		InvalidSyncToken::class => true,
52
-		// the sync client uses this to find out whether files exist,
53
-		// so it is not always an error, log it as debug
54
-		NotFound::class => true,
55
-		// this one mostly happens when the same file is uploaded at
56
-		// exactly the same time from two clients, only one client
57
-		// wins, the second one gets "Precondition failed"
58
-		PreconditionFailed::class => true,
59
-		// forbidden can be expected when trying to upload to
60
-		// read-only folders for example
61
-		Forbidden::class => true,
62
-		// Happens when an external storage or federated share is temporarily
63
-		// not available
64
-		StorageNotAvailableException::class => true,
65
-		// happens if some a client uses the wrong method for a given URL
66
-		// the error message itself is visible on the client side anyways
67
-		NotImplemented::class => true,
68
-		// happens when the parent directory is not present (for example when a
69
-		// move is done to a non-existent directory)
70
-		Conflict::class => true,
71
-		// happens when a certain method is not allowed to be called
72
-		// for example creating a folder that already exists
73
-		MethodNotAllowed::class => true,
74
-		// A locked file is perfectly valid and can happen in various cases
75
-		FileLocked::class => true,
76
-	];
45
+    protected $nonFatalExceptions = [
46
+        NotAuthenticated::class => true,
47
+        // If tokenauth can throw this exception (which is basically as
48
+        // NotAuthenticated. So not fatal.
49
+        PasswordLoginForbidden::class => true,
50
+        // basically a NotAuthenticated
51
+        InvalidSyncToken::class => true,
52
+        // the sync client uses this to find out whether files exist,
53
+        // so it is not always an error, log it as debug
54
+        NotFound::class => true,
55
+        // this one mostly happens when the same file is uploaded at
56
+        // exactly the same time from two clients, only one client
57
+        // wins, the second one gets "Precondition failed"
58
+        PreconditionFailed::class => true,
59
+        // forbidden can be expected when trying to upload to
60
+        // read-only folders for example
61
+        Forbidden::class => true,
62
+        // Happens when an external storage or federated share is temporarily
63
+        // not available
64
+        StorageNotAvailableException::class => true,
65
+        // happens if some a client uses the wrong method for a given URL
66
+        // the error message itself is visible on the client side anyways
67
+        NotImplemented::class => true,
68
+        // happens when the parent directory is not present (for example when a
69
+        // move is done to a non-existent directory)
70
+        Conflict::class => true,
71
+        // happens when a certain method is not allowed to be called
72
+        // for example creating a folder that already exists
73
+        MethodNotAllowed::class => true,
74
+        // A locked file is perfectly valid and can happen in various cases
75
+        FileLocked::class => true,
76
+    ];
77 77
 
78
-	/** @var string */
79
-	private $appName;
78
+    /** @var string */
79
+    private $appName;
80 80
 
81
-	/** @var ILogger */
82
-	private $logger;
81
+    /** @var ILogger */
82
+    private $logger;
83 83
 
84
-	/**
85
-	 * @param string $loggerAppName app name to use when logging
86
-	 * @param ILogger $logger
87
-	 */
88
-	public function __construct($loggerAppName, $logger) {
89
-		$this->appName = $loggerAppName;
90
-		$this->logger = $logger;
91
-	}
84
+    /**
85
+     * @param string $loggerAppName app name to use when logging
86
+     * @param ILogger $logger
87
+     */
88
+    public function __construct($loggerAppName, $logger) {
89
+        $this->appName = $loggerAppName;
90
+        $this->logger = $logger;
91
+    }
92 92
 
93
-	/**
94
-	 * This initializes the plugin.
95
-	 *
96
-	 * This function is called by \Sabre\DAV\Server, after
97
-	 * addPlugin is called.
98
-	 *
99
-	 * This method should set up the required event subscriptions.
100
-	 *
101
-	 * @param \Sabre\DAV\Server $server
102
-	 * @return void
103
-	 */
104
-	public function initialize(\Sabre\DAV\Server $server) {
93
+    /**
94
+     * This initializes the plugin.
95
+     *
96
+     * This function is called by \Sabre\DAV\Server, after
97
+     * addPlugin is called.
98
+     *
99
+     * This method should set up the required event subscriptions.
100
+     *
101
+     * @param \Sabre\DAV\Server $server
102
+     * @return void
103
+     */
104
+    public function initialize(\Sabre\DAV\Server $server) {
105 105
 
106
-		$server->on('exception', [$this, 'logException'], 10);
107
-	}
106
+        $server->on('exception', [$this, 'logException'], 10);
107
+    }
108 108
 
109
-	/**
110
-	 * Log exception
111
-	 *
112
-	 */
113
-	public function logException(\Exception $ex) {
114
-		$exceptionClass = get_class($ex);
115
-		$level = ILogger::FATAL;
116
-		if (isset($this->nonFatalExceptions[$exceptionClass]) ||
117
-			(
118
-				$exceptionClass === ServiceUnavailable::class &&
119
-				$ex->getMessage() === 'System in maintenance mode.'
120
-			)
121
-		) {
122
-			$level = ILogger::DEBUG;
123
-		}
109
+    /**
110
+     * Log exception
111
+     *
112
+     */
113
+    public function logException(\Exception $ex) {
114
+        $exceptionClass = get_class($ex);
115
+        $level = ILogger::FATAL;
116
+        if (isset($this->nonFatalExceptions[$exceptionClass]) ||
117
+            (
118
+                $exceptionClass === ServiceUnavailable::class &&
119
+                $ex->getMessage() === 'System in maintenance mode.'
120
+            )
121
+        ) {
122
+            $level = ILogger::DEBUG;
123
+        }
124 124
 
125
-		$this->logger->logException($ex, [
126
-			'app' => $this->appName,
127
-			'level' => $level,
128
-		]);
129
-	}
125
+        $this->logger->logException($ex, [
126
+            'app' => $this->appName,
127
+            'level' => $level,
128
+        ]);
129
+    }
130 130
 }
Please login to merge, or discard this patch.
apps/dav/lib/Connector/Sabre/FilesReportPlugin.php 1 patch
Indentation   +394 added lines, -394 removed lines patch added patch discarded remove patch
@@ -46,398 +46,398 @@
 block discarded – undo
46 46
 
47 47
 class FilesReportPlugin extends ServerPlugin {
48 48
 
49
-	// namespace
50
-	const NS_OWNCLOUD = 'http://owncloud.org/ns';
51
-	const REPORT_NAME            = '{http://owncloud.org/ns}filter-files';
52
-	const SYSTEMTAG_PROPERTYNAME = '{http://owncloud.org/ns}systemtag';
53
-	const CIRCLE_PROPERTYNAME = '{http://owncloud.org/ns}circle';
54
-
55
-	/**
56
-	 * Reference to main server object
57
-	 *
58
-	 * @var \Sabre\DAV\Server
59
-	 */
60
-	private $server;
61
-
62
-	/**
63
-	 * @var Tree
64
-	 */
65
-	private $tree;
66
-
67
-	/**
68
-	 * @var View
69
-	 */
70
-	private $fileView;
71
-
72
-	/**
73
-	 * @var ISystemTagManager
74
-	 */
75
-	private $tagManager;
76
-
77
-	/**
78
-	 * @var ISystemTagObjectMapper
79
-	 */
80
-	private $tagMapper;
81
-
82
-	/**
83
-	 * Manager for private tags
84
-	 *
85
-	 * @var ITagManager
86
-	 */
87
-	private $fileTagger;
88
-
89
-	/**
90
-	 * @var IUserSession
91
-	 */
92
-	private $userSession;
93
-
94
-	/**
95
-	 * @var IGroupManager
96
-	 */
97
-	private $groupManager;
98
-
99
-	/**
100
-	 * @var Folder
101
-	 */
102
-	private $userFolder;
103
-
104
-	/**
105
-	 * @var IAppManager
106
-	 */
107
-	private $appManager;
108
-
109
-	/**
110
-	 * @param Tree $tree
111
-	 * @param View $view
112
-	 * @param ISystemTagManager $tagManager
113
-	 * @param ISystemTagObjectMapper $tagMapper
114
-	 * @param ITagManager $fileTagger manager for private tags
115
-	 * @param IUserSession $userSession
116
-	 * @param IGroupManager $groupManager
117
-	 * @param Folder $userFolder
118
-	 * @param IAppManager $appManager
119
-	 */
120
-	public function __construct(Tree $tree,
121
-								View $view,
122
-								ISystemTagManager $tagManager,
123
-								ISystemTagObjectMapper $tagMapper,
124
-								ITagManager $fileTagger,
125
-								IUserSession $userSession,
126
-								IGroupManager $groupManager,
127
-								Folder $userFolder,
128
-								IAppManager $appManager
129
-	) {
130
-		$this->tree = $tree;
131
-		$this->fileView = $view;
132
-		$this->tagManager = $tagManager;
133
-		$this->tagMapper = $tagMapper;
134
-		$this->fileTagger = $fileTagger;
135
-		$this->userSession = $userSession;
136
-		$this->groupManager = $groupManager;
137
-		$this->userFolder = $userFolder;
138
-		$this->appManager = $appManager;
139
-	}
140
-
141
-	/**
142
-	 * This initializes the plugin.
143
-	 *
144
-	 * This function is called by \Sabre\DAV\Server, after
145
-	 * addPlugin is called.
146
-	 *
147
-	 * This method should set up the required event subscriptions.
148
-	 *
149
-	 * @param \Sabre\DAV\Server $server
150
-	 * @return void
151
-	 */
152
-	public function initialize(\Sabre\DAV\Server $server) {
153
-
154
-		$server->xml->namespaceMap[self::NS_OWNCLOUD] = 'oc';
155
-
156
-		$this->server = $server;
157
-		$this->server->on('report', [$this, 'onReport']);
158
-	}
159
-
160
-	/**
161
-	 * Returns a list of reports this plugin supports.
162
-	 *
163
-	 * This will be used in the {DAV:}supported-report-set property.
164
-	 *
165
-	 * @param string $uri
166
-	 * @return array
167
-	 */
168
-	public function getSupportedReportSet($uri) {
169
-		return [self::REPORT_NAME];
170
-	}
171
-
172
-	/**
173
-	 * REPORT operations to look for files
174
-	 *
175
-	 * @param string $reportName
176
-	 * @param $report
177
-	 * @param string $uri
178
-	 * @return bool
179
-	 * @throws BadRequest
180
-	 * @throws PreconditionFailed
181
-	 * @internal param $ [] $report
182
-	 */
183
-	public function onReport($reportName, $report, $uri) {
184
-		$reportTargetNode = $this->server->tree->getNodeForPath($uri);
185
-		if (!$reportTargetNode instanceof Directory || $reportName !== self::REPORT_NAME) {
186
-			return;
187
-		}
188
-
189
-		$ns = '{' . $this::NS_OWNCLOUD . '}';
190
-		$requestedProps = [];
191
-		$filterRules = [];
192
-
193
-		// parse report properties and gather filter info
194
-		foreach ($report as $reportProps) {
195
-			$name = $reportProps['name'];
196
-			if ($name === $ns . 'filter-rules') {
197
-				$filterRules = $reportProps['value'];
198
-			} else if ($name === '{DAV:}prop') {
199
-				// propfind properties
200
-				foreach ($reportProps['value'] as $propVal) {
201
-					$requestedProps[] = $propVal['name'];
202
-				}
203
-			}
204
-		}
205
-
206
-		if (empty($filterRules)) {
207
-			// an empty filter would return all existing files which would be slow
208
-			throw new BadRequest('Missing filter-rule block in request');
209
-		}
210
-
211
-		// gather all file ids matching filter
212
-		try {
213
-			$resultFileIds = $this->processFilterRules($filterRules);
214
-		} catch (TagNotFoundException $e) {
215
-			throw new PreconditionFailed('Cannot filter by non-existing tag', 0, $e);
216
-		}
217
-
218
-		// find sabre nodes by file id, restricted to the root node path
219
-		$results = $this->findNodesByFileIds($reportTargetNode, $resultFileIds);
220
-
221
-		$filesUri = $this->getFilesBaseUri($uri, $reportTargetNode->getPath());
222
-		$responses = $this->prepareResponses($filesUri, $requestedProps, $results);
223
-
224
-		$xml = $this->server->xml->write(
225
-			'{DAV:}multistatus',
226
-			new MultiStatus($responses)
227
-		);
228
-
229
-		$this->server->httpResponse->setStatus(207);
230
-		$this->server->httpResponse->setHeader('Content-Type', 'application/xml; charset=utf-8');
231
-		$this->server->httpResponse->setBody($xml);
232
-
233
-		return false;
234
-	}
235
-
236
-	/**
237
-	 * Returns the base uri of the files root by removing
238
-	 * the subpath from the URI
239
-	 *
240
-	 * @param string $uri URI from this request
241
-	 * @param string $subPath subpath to remove from the URI
242
-	 *
243
-	 * @return string files base uri
244
-	 */
245
-	private function getFilesBaseUri($uri, $subPath) {
246
-		$uri = trim($uri, '/');
247
-		$subPath = trim($subPath, '/');
248
-		if (empty($subPath)) {
249
-			$filesUri = $uri;
250
-		} else {
251
-			$filesUri = substr($uri, 0, strlen($uri) - strlen($subPath));
252
-		}
253
-		$filesUri = trim($filesUri, '/');
254
-		if (empty($filesUri)) {
255
-			return '';
256
-		}
257
-		return '/' . $filesUri;
258
-	}
259
-
260
-	/**
261
-	 * Find file ids matching the given filter rules
262
-	 *
263
-	 * @param array $filterRules
264
-	 * @return array array of unique file id results
265
-	 *
266
-	 * @throws TagNotFoundException whenever a tag was not found
267
-	 */
268
-	protected function processFilterRules($filterRules) {
269
-		$ns = '{' . $this::NS_OWNCLOUD . '}';
270
-		$resultFileIds = null;
271
-		$systemTagIds = [];
272
-		$circlesIds = [];
273
-		$favoriteFilter = null;
274
-		foreach ($filterRules as $filterRule) {
275
-			if ($filterRule['name'] === $ns . 'systemtag') {
276
-				$systemTagIds[] = $filterRule['value'];
277
-			}
278
-			if ($filterRule['name'] === self::CIRCLE_PROPERTYNAME) {
279
-				$circlesIds[] = $filterRule['value'];
280
-			}
281
-			if ($filterRule['name'] === $ns . 'favorite') {
282
-				$favoriteFilter = true;
283
-			}
284
-
285
-		}
286
-
287
-		if ($favoriteFilter !== null) {
288
-			$resultFileIds = $this->fileTagger->load('files')->getFavorites();
289
-			if (empty($resultFileIds)) {
290
-				return [];
291
-			}
292
-		}
293
-
294
-		if (!empty($systemTagIds)) {
295
-			$fileIds = $this->getSystemTagFileIds($systemTagIds);
296
-			if (empty($resultFileIds)) {
297
-				$resultFileIds = $fileIds;
298
-			} else {
299
-				$resultFileIds = array_intersect($fileIds, $resultFileIds);
300
-			}
301
-		}
302
-
303
-		if (!empty($circlesIds)) {
304
-			$fileIds = $this->getCirclesFileIds($circlesIds);
305
-			if (empty($resultFileIds)) {
306
-				$resultFileIds = $fileIds;
307
-			} else {
308
-				$resultFileIds = array_intersect($fileIds, $resultFileIds);
309
-			}
310
-		}
311
-
312
-		return $resultFileIds;
313
-	}
314
-
315
-	private function getSystemTagFileIds($systemTagIds) {
316
-		$resultFileIds = null;
317
-
318
-		// check user permissions, if applicable
319
-		if (!$this->isAdmin()) {
320
-			// check visibility/permission
321
-			$tags = $this->tagManager->getTagsByIds($systemTagIds);
322
-			$unknownTagIds = [];
323
-			foreach ($tags as $tag) {
324
-				if (!$tag->isUserVisible()) {
325
-					$unknownTagIds[] = $tag->getId();
326
-				}
327
-			}
328
-
329
-			if (!empty($unknownTagIds)) {
330
-				throw new TagNotFoundException('Tag with ids ' . implode(', ', $unknownTagIds) . ' not found');
331
-			}
332
-		}
333
-
334
-		// fetch all file ids and intersect them
335
-		foreach ($systemTagIds as $systemTagId) {
336
-			$fileIds = $this->tagMapper->getObjectIdsForTags($systemTagId, 'files');
337
-
338
-			if (empty($fileIds)) {
339
-				// This tag has no files, nothing can ever show up
340
-				return [];
341
-			}
342
-
343
-			// first run ?
344
-			if ($resultFileIds === null) {
345
-				$resultFileIds = $fileIds;
346
-			} else {
347
-				$resultFileIds = array_intersect($resultFileIds, $fileIds);
348
-			}
349
-
350
-			if (empty($resultFileIds)) {
351
-				// Empty intersection, nothing can show up anymore
352
-				return [];
353
-			}
354
-		}
355
-		return $resultFileIds;
356
-	}
357
-
358
-	/**
359
-	 * @suppress PhanUndeclaredClassMethod
360
-	 * @param array $circlesIds
361
-	 * @return array
362
-	 */
363
-	private function getCirclesFileIds(array $circlesIds) {
364
-		if (!$this->appManager->isEnabledForUser('circles') || !class_exists('\OCA\Circles\Api\v1\Circles')) {
365
-			return [];
366
-		}
367
-		return \OCA\Circles\Api\v1\Circles::getFilesForCircles($circlesIds);
368
-	}
369
-
370
-
371
-	/**
372
-	 * Prepare propfind response for the given nodes
373
-	 *
374
-	 * @param string $filesUri $filesUri URI leading to root of the files URI,
375
-	 * with a leading slash but no trailing slash
376
-	 * @param string[] $requestedProps requested properties
377
-	 * @param Node[] nodes nodes for which to fetch and prepare responses
378
-	 * @return Response[]
379
-	 */
380
-	public function prepareResponses($filesUri, $requestedProps, $nodes) {
381
-		$responses = [];
382
-		foreach ($nodes as $node) {
383
-			$propFind = new PropFind($filesUri . $node->getPath(), $requestedProps);
384
-
385
-			$this->server->getPropertiesByNode($propFind, $node);
386
-			// copied from Sabre Server's getPropertiesForPath
387
-			$result = $propFind->getResultForMultiStatus();
388
-			$result['href'] = $propFind->getPath();
389
-
390
-			$resourceType = $this->server->getResourceTypeForNode($node);
391
-			if (in_array('{DAV:}collection', $resourceType) || in_array('{DAV:}principal', $resourceType)) {
392
-				$result['href'] .= '/';
393
-			}
394
-
395
-			$responses[] = new Response(
396
-				rtrim($this->server->getBaseUri(), '/') . $filesUri . $node->getPath(),
397
-				$result,
398
-				200
399
-			);
400
-		}
401
-		return $responses;
402
-	}
403
-
404
-	/**
405
-	 * Find Sabre nodes by file ids
406
-	 *
407
-	 * @param Node $rootNode root node for search
408
-	 * @param array $fileIds file ids
409
-	 * @return Node[] array of Sabre nodes
410
-	 */
411
-	public function findNodesByFileIds($rootNode, $fileIds) {
412
-		$folder = $this->userFolder;
413
-		if (trim($rootNode->getPath(), '/') !== '') {
414
-			$folder = $folder->get($rootNode->getPath());
415
-		}
416
-
417
-		$results = [];
418
-		foreach ($fileIds as $fileId) {
419
-			$entry = $folder->getById($fileId);
420
-			if ($entry) {
421
-				$entry = current($entry);
422
-				if ($entry instanceof \OCP\Files\File) {
423
-					$results[] = new File($this->fileView, $entry);
424
-				} else if ($entry instanceof \OCP\Files\Folder) {
425
-					$results[] = new Directory($this->fileView, $entry);
426
-				}
427
-			}
428
-		}
429
-
430
-		return $results;
431
-	}
432
-
433
-	/**
434
-	 * Returns whether the currently logged in user is an administrator
435
-	 */
436
-	private function isAdmin() {
437
-		$user = $this->userSession->getUser();
438
-		if ($user !== null) {
439
-			return $this->groupManager->isAdmin($user->getUID());
440
-		}
441
-		return false;
442
-	}
49
+    // namespace
50
+    const NS_OWNCLOUD = 'http://owncloud.org/ns';
51
+    const REPORT_NAME            = '{http://owncloud.org/ns}filter-files';
52
+    const SYSTEMTAG_PROPERTYNAME = '{http://owncloud.org/ns}systemtag';
53
+    const CIRCLE_PROPERTYNAME = '{http://owncloud.org/ns}circle';
54
+
55
+    /**
56
+     * Reference to main server object
57
+     *
58
+     * @var \Sabre\DAV\Server
59
+     */
60
+    private $server;
61
+
62
+    /**
63
+     * @var Tree
64
+     */
65
+    private $tree;
66
+
67
+    /**
68
+     * @var View
69
+     */
70
+    private $fileView;
71
+
72
+    /**
73
+     * @var ISystemTagManager
74
+     */
75
+    private $tagManager;
76
+
77
+    /**
78
+     * @var ISystemTagObjectMapper
79
+     */
80
+    private $tagMapper;
81
+
82
+    /**
83
+     * Manager for private tags
84
+     *
85
+     * @var ITagManager
86
+     */
87
+    private $fileTagger;
88
+
89
+    /**
90
+     * @var IUserSession
91
+     */
92
+    private $userSession;
93
+
94
+    /**
95
+     * @var IGroupManager
96
+     */
97
+    private $groupManager;
98
+
99
+    /**
100
+     * @var Folder
101
+     */
102
+    private $userFolder;
103
+
104
+    /**
105
+     * @var IAppManager
106
+     */
107
+    private $appManager;
108
+
109
+    /**
110
+     * @param Tree $tree
111
+     * @param View $view
112
+     * @param ISystemTagManager $tagManager
113
+     * @param ISystemTagObjectMapper $tagMapper
114
+     * @param ITagManager $fileTagger manager for private tags
115
+     * @param IUserSession $userSession
116
+     * @param IGroupManager $groupManager
117
+     * @param Folder $userFolder
118
+     * @param IAppManager $appManager
119
+     */
120
+    public function __construct(Tree $tree,
121
+                                View $view,
122
+                                ISystemTagManager $tagManager,
123
+                                ISystemTagObjectMapper $tagMapper,
124
+                                ITagManager $fileTagger,
125
+                                IUserSession $userSession,
126
+                                IGroupManager $groupManager,
127
+                                Folder $userFolder,
128
+                                IAppManager $appManager
129
+    ) {
130
+        $this->tree = $tree;
131
+        $this->fileView = $view;
132
+        $this->tagManager = $tagManager;
133
+        $this->tagMapper = $tagMapper;
134
+        $this->fileTagger = $fileTagger;
135
+        $this->userSession = $userSession;
136
+        $this->groupManager = $groupManager;
137
+        $this->userFolder = $userFolder;
138
+        $this->appManager = $appManager;
139
+    }
140
+
141
+    /**
142
+     * This initializes the plugin.
143
+     *
144
+     * This function is called by \Sabre\DAV\Server, after
145
+     * addPlugin is called.
146
+     *
147
+     * This method should set up the required event subscriptions.
148
+     *
149
+     * @param \Sabre\DAV\Server $server
150
+     * @return void
151
+     */
152
+    public function initialize(\Sabre\DAV\Server $server) {
153
+
154
+        $server->xml->namespaceMap[self::NS_OWNCLOUD] = 'oc';
155
+
156
+        $this->server = $server;
157
+        $this->server->on('report', [$this, 'onReport']);
158
+    }
159
+
160
+    /**
161
+     * Returns a list of reports this plugin supports.
162
+     *
163
+     * This will be used in the {DAV:}supported-report-set property.
164
+     *
165
+     * @param string $uri
166
+     * @return array
167
+     */
168
+    public function getSupportedReportSet($uri) {
169
+        return [self::REPORT_NAME];
170
+    }
171
+
172
+    /**
173
+     * REPORT operations to look for files
174
+     *
175
+     * @param string $reportName
176
+     * @param $report
177
+     * @param string $uri
178
+     * @return bool
179
+     * @throws BadRequest
180
+     * @throws PreconditionFailed
181
+     * @internal param $ [] $report
182
+     */
183
+    public function onReport($reportName, $report, $uri) {
184
+        $reportTargetNode = $this->server->tree->getNodeForPath($uri);
185
+        if (!$reportTargetNode instanceof Directory || $reportName !== self::REPORT_NAME) {
186
+            return;
187
+        }
188
+
189
+        $ns = '{' . $this::NS_OWNCLOUD . '}';
190
+        $requestedProps = [];
191
+        $filterRules = [];
192
+
193
+        // parse report properties and gather filter info
194
+        foreach ($report as $reportProps) {
195
+            $name = $reportProps['name'];
196
+            if ($name === $ns . 'filter-rules') {
197
+                $filterRules = $reportProps['value'];
198
+            } else if ($name === '{DAV:}prop') {
199
+                // propfind properties
200
+                foreach ($reportProps['value'] as $propVal) {
201
+                    $requestedProps[] = $propVal['name'];
202
+                }
203
+            }
204
+        }
205
+
206
+        if (empty($filterRules)) {
207
+            // an empty filter would return all existing files which would be slow
208
+            throw new BadRequest('Missing filter-rule block in request');
209
+        }
210
+
211
+        // gather all file ids matching filter
212
+        try {
213
+            $resultFileIds = $this->processFilterRules($filterRules);
214
+        } catch (TagNotFoundException $e) {
215
+            throw new PreconditionFailed('Cannot filter by non-existing tag', 0, $e);
216
+        }
217
+
218
+        // find sabre nodes by file id, restricted to the root node path
219
+        $results = $this->findNodesByFileIds($reportTargetNode, $resultFileIds);
220
+
221
+        $filesUri = $this->getFilesBaseUri($uri, $reportTargetNode->getPath());
222
+        $responses = $this->prepareResponses($filesUri, $requestedProps, $results);
223
+
224
+        $xml = $this->server->xml->write(
225
+            '{DAV:}multistatus',
226
+            new MultiStatus($responses)
227
+        );
228
+
229
+        $this->server->httpResponse->setStatus(207);
230
+        $this->server->httpResponse->setHeader('Content-Type', 'application/xml; charset=utf-8');
231
+        $this->server->httpResponse->setBody($xml);
232
+
233
+        return false;
234
+    }
235
+
236
+    /**
237
+     * Returns the base uri of the files root by removing
238
+     * the subpath from the URI
239
+     *
240
+     * @param string $uri URI from this request
241
+     * @param string $subPath subpath to remove from the URI
242
+     *
243
+     * @return string files base uri
244
+     */
245
+    private function getFilesBaseUri($uri, $subPath) {
246
+        $uri = trim($uri, '/');
247
+        $subPath = trim($subPath, '/');
248
+        if (empty($subPath)) {
249
+            $filesUri = $uri;
250
+        } else {
251
+            $filesUri = substr($uri, 0, strlen($uri) - strlen($subPath));
252
+        }
253
+        $filesUri = trim($filesUri, '/');
254
+        if (empty($filesUri)) {
255
+            return '';
256
+        }
257
+        return '/' . $filesUri;
258
+    }
259
+
260
+    /**
261
+     * Find file ids matching the given filter rules
262
+     *
263
+     * @param array $filterRules
264
+     * @return array array of unique file id results
265
+     *
266
+     * @throws TagNotFoundException whenever a tag was not found
267
+     */
268
+    protected function processFilterRules($filterRules) {
269
+        $ns = '{' . $this::NS_OWNCLOUD . '}';
270
+        $resultFileIds = null;
271
+        $systemTagIds = [];
272
+        $circlesIds = [];
273
+        $favoriteFilter = null;
274
+        foreach ($filterRules as $filterRule) {
275
+            if ($filterRule['name'] === $ns . 'systemtag') {
276
+                $systemTagIds[] = $filterRule['value'];
277
+            }
278
+            if ($filterRule['name'] === self::CIRCLE_PROPERTYNAME) {
279
+                $circlesIds[] = $filterRule['value'];
280
+            }
281
+            if ($filterRule['name'] === $ns . 'favorite') {
282
+                $favoriteFilter = true;
283
+            }
284
+
285
+        }
286
+
287
+        if ($favoriteFilter !== null) {
288
+            $resultFileIds = $this->fileTagger->load('files')->getFavorites();
289
+            if (empty($resultFileIds)) {
290
+                return [];
291
+            }
292
+        }
293
+
294
+        if (!empty($systemTagIds)) {
295
+            $fileIds = $this->getSystemTagFileIds($systemTagIds);
296
+            if (empty($resultFileIds)) {
297
+                $resultFileIds = $fileIds;
298
+            } else {
299
+                $resultFileIds = array_intersect($fileIds, $resultFileIds);
300
+            }
301
+        }
302
+
303
+        if (!empty($circlesIds)) {
304
+            $fileIds = $this->getCirclesFileIds($circlesIds);
305
+            if (empty($resultFileIds)) {
306
+                $resultFileIds = $fileIds;
307
+            } else {
308
+                $resultFileIds = array_intersect($fileIds, $resultFileIds);
309
+            }
310
+        }
311
+
312
+        return $resultFileIds;
313
+    }
314
+
315
+    private function getSystemTagFileIds($systemTagIds) {
316
+        $resultFileIds = null;
317
+
318
+        // check user permissions, if applicable
319
+        if (!$this->isAdmin()) {
320
+            // check visibility/permission
321
+            $tags = $this->tagManager->getTagsByIds($systemTagIds);
322
+            $unknownTagIds = [];
323
+            foreach ($tags as $tag) {
324
+                if (!$tag->isUserVisible()) {
325
+                    $unknownTagIds[] = $tag->getId();
326
+                }
327
+            }
328
+
329
+            if (!empty($unknownTagIds)) {
330
+                throw new TagNotFoundException('Tag with ids ' . implode(', ', $unknownTagIds) . ' not found');
331
+            }
332
+        }
333
+
334
+        // fetch all file ids and intersect them
335
+        foreach ($systemTagIds as $systemTagId) {
336
+            $fileIds = $this->tagMapper->getObjectIdsForTags($systemTagId, 'files');
337
+
338
+            if (empty($fileIds)) {
339
+                // This tag has no files, nothing can ever show up
340
+                return [];
341
+            }
342
+
343
+            // first run ?
344
+            if ($resultFileIds === null) {
345
+                $resultFileIds = $fileIds;
346
+            } else {
347
+                $resultFileIds = array_intersect($resultFileIds, $fileIds);
348
+            }
349
+
350
+            if (empty($resultFileIds)) {
351
+                // Empty intersection, nothing can show up anymore
352
+                return [];
353
+            }
354
+        }
355
+        return $resultFileIds;
356
+    }
357
+
358
+    /**
359
+     * @suppress PhanUndeclaredClassMethod
360
+     * @param array $circlesIds
361
+     * @return array
362
+     */
363
+    private function getCirclesFileIds(array $circlesIds) {
364
+        if (!$this->appManager->isEnabledForUser('circles') || !class_exists('\OCA\Circles\Api\v1\Circles')) {
365
+            return [];
366
+        }
367
+        return \OCA\Circles\Api\v1\Circles::getFilesForCircles($circlesIds);
368
+    }
369
+
370
+
371
+    /**
372
+     * Prepare propfind response for the given nodes
373
+     *
374
+     * @param string $filesUri $filesUri URI leading to root of the files URI,
375
+     * with a leading slash but no trailing slash
376
+     * @param string[] $requestedProps requested properties
377
+     * @param Node[] nodes nodes for which to fetch and prepare responses
378
+     * @return Response[]
379
+     */
380
+    public function prepareResponses($filesUri, $requestedProps, $nodes) {
381
+        $responses = [];
382
+        foreach ($nodes as $node) {
383
+            $propFind = new PropFind($filesUri . $node->getPath(), $requestedProps);
384
+
385
+            $this->server->getPropertiesByNode($propFind, $node);
386
+            // copied from Sabre Server's getPropertiesForPath
387
+            $result = $propFind->getResultForMultiStatus();
388
+            $result['href'] = $propFind->getPath();
389
+
390
+            $resourceType = $this->server->getResourceTypeForNode($node);
391
+            if (in_array('{DAV:}collection', $resourceType) || in_array('{DAV:}principal', $resourceType)) {
392
+                $result['href'] .= '/';
393
+            }
394
+
395
+            $responses[] = new Response(
396
+                rtrim($this->server->getBaseUri(), '/') . $filesUri . $node->getPath(),
397
+                $result,
398
+                200
399
+            );
400
+        }
401
+        return $responses;
402
+    }
403
+
404
+    /**
405
+     * Find Sabre nodes by file ids
406
+     *
407
+     * @param Node $rootNode root node for search
408
+     * @param array $fileIds file ids
409
+     * @return Node[] array of Sabre nodes
410
+     */
411
+    public function findNodesByFileIds($rootNode, $fileIds) {
412
+        $folder = $this->userFolder;
413
+        if (trim($rootNode->getPath(), '/') !== '') {
414
+            $folder = $folder->get($rootNode->getPath());
415
+        }
416
+
417
+        $results = [];
418
+        foreach ($fileIds as $fileId) {
419
+            $entry = $folder->getById($fileId);
420
+            if ($entry) {
421
+                $entry = current($entry);
422
+                if ($entry instanceof \OCP\Files\File) {
423
+                    $results[] = new File($this->fileView, $entry);
424
+                } else if ($entry instanceof \OCP\Files\Folder) {
425
+                    $results[] = new Directory($this->fileView, $entry);
426
+                }
427
+            }
428
+        }
429
+
430
+        return $results;
431
+    }
432
+
433
+    /**
434
+     * Returns whether the currently logged in user is an administrator
435
+     */
436
+    private function isAdmin() {
437
+        $user = $this->userSession->getUser();
438
+        if ($user !== null) {
439
+            return $this->groupManager->isAdmin($user->getUID());
440
+        }
441
+        return false;
442
+    }
443 443
 }
Please login to merge, or discard this patch.
apps/dav/lib/Connector/Sabre/TagsPlugin.php 1 patch
Indentation   +217 added lines, -217 removed lines patch added patch discarded remove patch
@@ -54,246 +54,246 @@
 block discarded – undo
54 54
 class TagsPlugin extends \Sabre\DAV\ServerPlugin
55 55
 {
56 56
 
57
-	// namespace
58
-	const NS_OWNCLOUD = 'http://owncloud.org/ns';
59
-	const TAGS_PROPERTYNAME = '{http://owncloud.org/ns}tags';
60
-	const FAVORITE_PROPERTYNAME = '{http://owncloud.org/ns}favorite';
61
-	const TAG_FAVORITE = '_$!<Favorite>!$_';
57
+    // namespace
58
+    const NS_OWNCLOUD = 'http://owncloud.org/ns';
59
+    const TAGS_PROPERTYNAME = '{http://owncloud.org/ns}tags';
60
+    const FAVORITE_PROPERTYNAME = '{http://owncloud.org/ns}favorite';
61
+    const TAG_FAVORITE = '_$!<Favorite>!$_';
62 62
 
63
-	/**
64
-	 * Reference to main server object
65
-	 *
66
-	 * @var \Sabre\DAV\Server
67
-	 */
68
-	private $server;
63
+    /**
64
+     * Reference to main server object
65
+     *
66
+     * @var \Sabre\DAV\Server
67
+     */
68
+    private $server;
69 69
 
70
-	/**
71
-	 * @var \OCP\ITagManager
72
-	 */
73
-	private $tagManager;
70
+    /**
71
+     * @var \OCP\ITagManager
72
+     */
73
+    private $tagManager;
74 74
 
75
-	/**
76
-	 * @var \OCP\ITags
77
-	 */
78
-	private $tagger;
75
+    /**
76
+     * @var \OCP\ITags
77
+     */
78
+    private $tagger;
79 79
 
80
-	/**
81
-	 * Array of file id to tags array
82
-	 * The null value means the cache wasn't initialized.
83
-	 *
84
-	 * @var array
85
-	 */
86
-	private $cachedTags;
80
+    /**
81
+     * Array of file id to tags array
82
+     * The null value means the cache wasn't initialized.
83
+     *
84
+     * @var array
85
+     */
86
+    private $cachedTags;
87 87
 
88
-	/**
89
-	 * @var \Sabre\DAV\Tree
90
-	 */
91
-	private $tree;
88
+    /**
89
+     * @var \Sabre\DAV\Tree
90
+     */
91
+    private $tree;
92 92
 
93
-	/**
94
-	 * @param \Sabre\DAV\Tree $tree tree
95
-	 * @param \OCP\ITagManager $tagManager tag manager
96
-	 */
97
-	public function __construct(\Sabre\DAV\Tree $tree, \OCP\ITagManager $tagManager) {
98
-		$this->tree = $tree;
99
-		$this->tagManager = $tagManager;
100
-		$this->tagger = null;
101
-		$this->cachedTags = [];
102
-	}
93
+    /**
94
+     * @param \Sabre\DAV\Tree $tree tree
95
+     * @param \OCP\ITagManager $tagManager tag manager
96
+     */
97
+    public function __construct(\Sabre\DAV\Tree $tree, \OCP\ITagManager $tagManager) {
98
+        $this->tree = $tree;
99
+        $this->tagManager = $tagManager;
100
+        $this->tagger = null;
101
+        $this->cachedTags = [];
102
+    }
103 103
 
104
-	/**
105
-	 * This initializes the plugin.
106
-	 *
107
-	 * This function is called by \Sabre\DAV\Server, after
108
-	 * addPlugin is called.
109
-	 *
110
-	 * This method should set up the required event subscriptions.
111
-	 *
112
-	 * @param \Sabre\DAV\Server $server
113
-	 * @return void
114
-	 */
115
-	public function initialize(\Sabre\DAV\Server $server) {
104
+    /**
105
+     * This initializes the plugin.
106
+     *
107
+     * This function is called by \Sabre\DAV\Server, after
108
+     * addPlugin is called.
109
+     *
110
+     * This method should set up the required event subscriptions.
111
+     *
112
+     * @param \Sabre\DAV\Server $server
113
+     * @return void
114
+     */
115
+    public function initialize(\Sabre\DAV\Server $server) {
116 116
 
117
-		$server->xml->namespaceMap[self::NS_OWNCLOUD] = 'oc';
118
-		$server->xml->elementMap[self::TAGS_PROPERTYNAME] = TagList::class;
117
+        $server->xml->namespaceMap[self::NS_OWNCLOUD] = 'oc';
118
+        $server->xml->elementMap[self::TAGS_PROPERTYNAME] = TagList::class;
119 119
 
120
-		$this->server = $server;
121
-		$this->server->on('propFind', [$this, 'handleGetProperties']);
122
-		$this->server->on('propPatch', [$this, 'handleUpdateProperties']);
123
-	}
120
+        $this->server = $server;
121
+        $this->server->on('propFind', [$this, 'handleGetProperties']);
122
+        $this->server->on('propPatch', [$this, 'handleUpdateProperties']);
123
+    }
124 124
 
125
-	/**
126
-	 * Returns the tagger
127
-	 *
128
-	 * @return \OCP\ITags tagger
129
-	 */
130
-	private function getTagger() {
131
-		if (!$this->tagger) {
132
-			$this->tagger = $this->tagManager->load('files');
133
-		}
134
-		return $this->tagger;
135
-	}
125
+    /**
126
+     * Returns the tagger
127
+     *
128
+     * @return \OCP\ITags tagger
129
+     */
130
+    private function getTagger() {
131
+        if (!$this->tagger) {
132
+            $this->tagger = $this->tagManager->load('files');
133
+        }
134
+        return $this->tagger;
135
+    }
136 136
 
137
-	/**
138
-	 * Returns tags and favorites.
139
-	 *
140
-	 * @param integer $fileId file id
141
-	 * @return array list($tags, $favorite) with $tags as tag array
142
-	 * and $favorite is a boolean whether the file was favorited
143
-	 */
144
-	private function getTagsAndFav($fileId) {
145
-		$isFav = false;
146
-		$tags = $this->getTags($fileId);
147
-		if ($tags) {
148
-			$favPos = array_search(self::TAG_FAVORITE, $tags);
149
-			if ($favPos !== false) {
150
-				$isFav = true;
151
-				unset($tags[$favPos]);
152
-			}
153
-		}
154
-		return [$tags, $isFav];
155
-	}
137
+    /**
138
+     * Returns tags and favorites.
139
+     *
140
+     * @param integer $fileId file id
141
+     * @return array list($tags, $favorite) with $tags as tag array
142
+     * and $favorite is a boolean whether the file was favorited
143
+     */
144
+    private function getTagsAndFav($fileId) {
145
+        $isFav = false;
146
+        $tags = $this->getTags($fileId);
147
+        if ($tags) {
148
+            $favPos = array_search(self::TAG_FAVORITE, $tags);
149
+            if ($favPos !== false) {
150
+                $isFav = true;
151
+                unset($tags[$favPos]);
152
+            }
153
+        }
154
+        return [$tags, $isFav];
155
+    }
156 156
 
157
-	/**
158
-	 * Returns tags for the given file id
159
-	 *
160
-	 * @param integer $fileId file id
161
-	 * @return array list of tags for that file
162
-	 */
163
-	private function getTags($fileId) {
164
-		if (isset($this->cachedTags[$fileId])) {
165
-			return $this->cachedTags[$fileId];
166
-		} else {
167
-			$tags = $this->getTagger()->getTagsForObjects([$fileId]);
168
-			if ($tags !== false) {
169
-				if (empty($tags)) {
170
-					return [];
171
-				}
172
-				return current($tags);
173
-			}
174
-		}
175
-		return null;
176
-	}
157
+    /**
158
+     * Returns tags for the given file id
159
+     *
160
+     * @param integer $fileId file id
161
+     * @return array list of tags for that file
162
+     */
163
+    private function getTags($fileId) {
164
+        if (isset($this->cachedTags[$fileId])) {
165
+            return $this->cachedTags[$fileId];
166
+        } else {
167
+            $tags = $this->getTagger()->getTagsForObjects([$fileId]);
168
+            if ($tags !== false) {
169
+                if (empty($tags)) {
170
+                    return [];
171
+                }
172
+                return current($tags);
173
+            }
174
+        }
175
+        return null;
176
+    }
177 177
 
178
-	/**
179
-	 * Updates the tags of the given file id
180
-	 *
181
-	 * @param int $fileId
182
-	 * @param array $tags array of tag strings
183
-	 */
184
-	private function updateTags($fileId, $tags) {
185
-		$tagger = $this->getTagger();
186
-		$currentTags = $this->getTags($fileId);
178
+    /**
179
+     * Updates the tags of the given file id
180
+     *
181
+     * @param int $fileId
182
+     * @param array $tags array of tag strings
183
+     */
184
+    private function updateTags($fileId, $tags) {
185
+        $tagger = $this->getTagger();
186
+        $currentTags = $this->getTags($fileId);
187 187
 
188
-		$newTags = array_diff($tags, $currentTags);
189
-		foreach ($newTags as $tag) {
190
-			if ($tag === self::TAG_FAVORITE) {
191
-				continue;
192
-			}
193
-			$tagger->tagAs($fileId, $tag);
194
-		}
195
-		$deletedTags = array_diff($currentTags, $tags);
196
-		foreach ($deletedTags as $tag) {
197
-			if ($tag === self::TAG_FAVORITE) {
198
-				continue;
199
-			}
200
-			$tagger->unTag($fileId, $tag);
201
-		}
202
-	}
188
+        $newTags = array_diff($tags, $currentTags);
189
+        foreach ($newTags as $tag) {
190
+            if ($tag === self::TAG_FAVORITE) {
191
+                continue;
192
+            }
193
+            $tagger->tagAs($fileId, $tag);
194
+        }
195
+        $deletedTags = array_diff($currentTags, $tags);
196
+        foreach ($deletedTags as $tag) {
197
+            if ($tag === self::TAG_FAVORITE) {
198
+                continue;
199
+            }
200
+            $tagger->unTag($fileId, $tag);
201
+        }
202
+    }
203 203
 
204
-	/**
205
-	 * Adds tags and favorites properties to the response,
206
-	 * if requested.
207
-	 *
208
-	 * @param PropFind $propFind
209
-	 * @param \Sabre\DAV\INode $node
210
-	 * @return void
211
-	 */
212
-	public function handleGetProperties(
213
-		PropFind $propFind,
214
-		\Sabre\DAV\INode $node
215
-	) {
216
-		if (!($node instanceof \OCA\DAV\Connector\Sabre\Node)) {
217
-			return;
218
-		}
204
+    /**
205
+     * Adds tags and favorites properties to the response,
206
+     * if requested.
207
+     *
208
+     * @param PropFind $propFind
209
+     * @param \Sabre\DAV\INode $node
210
+     * @return void
211
+     */
212
+    public function handleGetProperties(
213
+        PropFind $propFind,
214
+        \Sabre\DAV\INode $node
215
+    ) {
216
+        if (!($node instanceof \OCA\DAV\Connector\Sabre\Node)) {
217
+            return;
218
+        }
219 219
 
220
-		// need prefetch ?
221
-		if ($node instanceof \OCA\DAV\Connector\Sabre\Directory
222
-			&& $propFind->getDepth() !== 0
223
-			&& (!is_null($propFind->getStatus(self::TAGS_PROPERTYNAME))
224
-			|| !is_null($propFind->getStatus(self::FAVORITE_PROPERTYNAME))
225
-		)) {
226
-			// note: pre-fetching only supported for depth <= 1
227
-			$folderContent = $node->getChildren();
228
-			$fileIds[] = (int)$node->getId();
229
-			foreach ($folderContent as $info) {
230
-				$fileIds[] = (int)$info->getId();
231
-			}
232
-			$tags = $this->getTagger()->getTagsForObjects($fileIds);
233
-			if ($tags === false) {
234
-				// the tags API returns false on error...
235
-				$tags = [];
236
-			}
220
+        // need prefetch ?
221
+        if ($node instanceof \OCA\DAV\Connector\Sabre\Directory
222
+            && $propFind->getDepth() !== 0
223
+            && (!is_null($propFind->getStatus(self::TAGS_PROPERTYNAME))
224
+            || !is_null($propFind->getStatus(self::FAVORITE_PROPERTYNAME))
225
+        )) {
226
+            // note: pre-fetching only supported for depth <= 1
227
+            $folderContent = $node->getChildren();
228
+            $fileIds[] = (int)$node->getId();
229
+            foreach ($folderContent as $info) {
230
+                $fileIds[] = (int)$info->getId();
231
+            }
232
+            $tags = $this->getTagger()->getTagsForObjects($fileIds);
233
+            if ($tags === false) {
234
+                // the tags API returns false on error...
235
+                $tags = [];
236
+            }
237 237
 
238
-			$this->cachedTags = $this->cachedTags + $tags;
239
-			$emptyFileIds = array_diff($fileIds, array_keys($tags));
240
-			// also cache the ones that were not found
241
-			foreach ($emptyFileIds as $fileId) {
242
-				$this->cachedTags[$fileId] = [];
243
-			}
244
-		}
238
+            $this->cachedTags = $this->cachedTags + $tags;
239
+            $emptyFileIds = array_diff($fileIds, array_keys($tags));
240
+            // also cache the ones that were not found
241
+            foreach ($emptyFileIds as $fileId) {
242
+                $this->cachedTags[$fileId] = [];
243
+            }
244
+        }
245 245
 
246
-		$isFav = null;
246
+        $isFav = null;
247 247
 
248
-		$propFind->handle(self::TAGS_PROPERTYNAME, function() use (&$isFav, $node) {
249
-			list($tags, $isFav) = $this->getTagsAndFav($node->getId());
250
-			return new TagList($tags);
251
-		});
248
+        $propFind->handle(self::TAGS_PROPERTYNAME, function() use (&$isFav, $node) {
249
+            list($tags, $isFav) = $this->getTagsAndFav($node->getId());
250
+            return new TagList($tags);
251
+        });
252 252
 
253
-		$propFind->handle(self::FAVORITE_PROPERTYNAME, function() use ($isFav, $node) {
254
-			if (is_null($isFav)) {
255
-				list(, $isFav) = $this->getTagsAndFav($node->getId());
256
-			}
257
-			if ($isFav) {
258
-				return 1;
259
-			} else {
260
-				return 0;
261
-			}
262
-		});
263
-	}
253
+        $propFind->handle(self::FAVORITE_PROPERTYNAME, function() use ($isFav, $node) {
254
+            if (is_null($isFav)) {
255
+                list(, $isFav) = $this->getTagsAndFav($node->getId());
256
+            }
257
+            if ($isFav) {
258
+                return 1;
259
+            } else {
260
+                return 0;
261
+            }
262
+        });
263
+    }
264 264
 
265
-	/**
266
-	 * Updates tags and favorites properties, if applicable.
267
-	 *
268
-	 * @param string $path
269
-	 * @param PropPatch $propPatch
270
-	 *
271
-	 * @return void
272
-	 */
273
-	public function handleUpdateProperties($path, PropPatch $propPatch) {
274
-		$node = $this->tree->getNodeForPath($path);
275
-		if (!($node instanceof \OCA\DAV\Connector\Sabre\Node)) {
276
-			return;
277
-		}
265
+    /**
266
+     * Updates tags and favorites properties, if applicable.
267
+     *
268
+     * @param string $path
269
+     * @param PropPatch $propPatch
270
+     *
271
+     * @return void
272
+     */
273
+    public function handleUpdateProperties($path, PropPatch $propPatch) {
274
+        $node = $this->tree->getNodeForPath($path);
275
+        if (!($node instanceof \OCA\DAV\Connector\Sabre\Node)) {
276
+            return;
277
+        }
278 278
 
279
-		$propPatch->handle(self::TAGS_PROPERTYNAME, function($tagList) use ($node) {
280
-			$this->updateTags($node->getId(), $tagList->getTags());
281
-			return true;
282
-		});
279
+        $propPatch->handle(self::TAGS_PROPERTYNAME, function($tagList) use ($node) {
280
+            $this->updateTags($node->getId(), $tagList->getTags());
281
+            return true;
282
+        });
283 283
 
284
-		$propPatch->handle(self::FAVORITE_PROPERTYNAME, function($favState) use ($node) {
285
-			if ((int)$favState === 1 || $favState === 'true') {
286
-				$this->getTagger()->tagAs($node->getId(), self::TAG_FAVORITE);
287
-			} else {
288
-				$this->getTagger()->unTag($node->getId(), self::TAG_FAVORITE);
289
-			}
284
+        $propPatch->handle(self::FAVORITE_PROPERTYNAME, function($favState) use ($node) {
285
+            if ((int)$favState === 1 || $favState === 'true') {
286
+                $this->getTagger()->tagAs($node->getId(), self::TAG_FAVORITE);
287
+            } else {
288
+                $this->getTagger()->unTag($node->getId(), self::TAG_FAVORITE);
289
+            }
290 290
 
291
-			if (is_null($favState)) {
292
-				// confirm deletion
293
-				return 204;
294
-			}
291
+            if (is_null($favState)) {
292
+                // confirm deletion
293
+                return 204;
294
+            }
295 295
 
296
-			return 200;
297
-		});
298
-	}
296
+            return 200;
297
+        });
298
+    }
299 299
 }
Please login to merge, or discard this patch.