Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
151 changes: 79 additions & 72 deletions apps/dav/lib/CalDAV/CalDavBackend.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,13 +96,15 @@
use Sabre\VObject\Recur\EventIterator;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\EventDispatcher\GenericEvent;
use function array_column;
use function array_merge;
use function array_values;
use function explode;
use function is_array;
use function is_resource;
use function pathinfo;
use function rewind;
use function settype;
use function sprintf;
use function str_replace;
use function strtolower;
Expand Down Expand Up @@ -141,20 +143,19 @@ class CalDavBackend extends AbstractBackend implements SyncSupport, Subscription
public const CLASSIFICATION_CONFIDENTIAL = 2;

/**
* List of CalDAV properties, and how they map to database field names
* List of CalDAV properties, and how they map to database field names and their type
* Add your own properties by simply adding on to this array.
*
* Note that only string-based properties are supported here.
*
* @var array
* @psalm-var array<string, string[]>
*/
public $propertyMap = [
'{DAV:}displayname' => 'displayname',
'{urn:ietf:params:xml:ns:caldav}calendar-description' => 'description',
'{urn:ietf:params:xml:ns:caldav}calendar-timezone' => 'timezone',
'{http://apple.com/ns/ical/}calendar-order' => 'calendarorder',
'{http://apple.com/ns/ical/}calendar-color' => 'calendarcolor',
'{' . \OCA\DAV\DAV\Sharing\Plugin::NS_NEXTCLOUD . '}deleted-at' => 'deleted_at',
'{DAV:}displayname' => ['displayname', 'string'],
'{urn:ietf:params:xml:ns:caldav}calendar-description' => ['description', 'string'],
'{urn:ietf:params:xml:ns:caldav}calendar-timezone' => ['timezone', 'string'],
'{http://apple.com/ns/ical/}calendar-order' => ['calendarorder', 'int'],
'{http://apple.com/ns/ical/}calendar-color' => ['calendarcolor', 'string'],
'{' . \OCA\DAV\DAV\Sharing\Plugin::NS_NEXTCLOUD . '}deleted-at' => ['deleted_at', 'int'],
];

/**
Expand All @@ -163,13 +164,13 @@ class CalDavBackend extends AbstractBackend implements SyncSupport, Subscription
* @var array
*/
public $subscriptionPropertyMap = [
'{DAV:}displayname' => 'displayname',
'{http://apple.com/ns/ical/}refreshrate' => 'refreshrate',
'{http://apple.com/ns/ical/}calendar-order' => 'calendarorder',
'{http://apple.com/ns/ical/}calendar-color' => 'calendarcolor',
'{http://calendarserver.org/ns/}subscribed-strip-todos' => 'striptodos',
'{http://calendarserver.org/ns/}subscribed-strip-alarms' => 'stripalarms',
'{http://calendarserver.org/ns/}subscribed-strip-attachments' => 'stripattachments',
'{DAV:}displayname' => ['displayname', 'string'],
'{http://apple.com/ns/ical/}refreshrate' => ['refreshrate', 'string'],
'{http://apple.com/ns/ical/}calendar-order' => ['calendarorder', 'int'],
'{http://apple.com/ns/ical/}calendar-color' => ['calendarcolor', 'string'],
'{http://calendarserver.org/ns/}subscribed-strip-todos' => ['striptodos', 'bool'],
'{http://calendarserver.org/ns/}subscribed-strip-alarms' => ['stripalarms', 'string'],
'{http://calendarserver.org/ns/}subscribed-strip-attachments' => ['stripattachments', 'string'],
];

/** @var array properties to index */
Expand Down Expand Up @@ -335,7 +336,7 @@ public function getDeletedCalendars(int $deletedBefore): array {
public function getCalendarsForUser($principalUri) {
$principalUriOriginal = $principalUri;
$principalUri = $this->convertPrincipal($principalUri, true);
$fields = array_values($this->propertyMap);
$fields = array_column($this->propertyMap, 0);
$fields[] = 'id';
$fields[] = 'uri';
$fields[] = 'synctoken';
Expand Down Expand Up @@ -376,10 +377,7 @@ public function getCalendarsForUser($principalUri) {
'{' . \OCA\DAV\DAV\Sharing\Plugin::NS_OWNCLOUD . '}owner-principal' => $this->convertPrincipal($principalUri, !$this->legacyEndpoint),
];

foreach ($this->propertyMap as $xmlName => $dbName) {
$calendar[$xmlName] = $row[$dbName];
}

$calendar = $this->rowToCalendar($row, $calendar);
$calendar = $this->addOwnerPrincipalToCalendar($calendar);
$calendar = $this->addResourceTypeToCalendar($row, $calendar);

Expand All @@ -395,7 +393,7 @@ public function getCalendarsForUser($principalUri) {

$principals[] = $principalUri;

$fields = array_values($this->propertyMap);
$fields = array_column($this->propertyMap, 0);
$fields[] = 'a.id';
$fields[] = 'a.uri';
$fields[] = 'a.synctoken';
Expand Down Expand Up @@ -453,10 +451,7 @@ public function getCalendarsForUser($principalUri) {
$readOnlyPropertyName => $readOnly,
];

foreach ($this->propertyMap as $xmlName => $dbName) {
$calendar[$xmlName] = $row[$dbName];
}

$calendar = $this->rowToCalendar($row, $calendar);
$calendar = $this->addOwnerPrincipalToCalendar($calendar);
$calendar = $this->addResourceTypeToCalendar($row, $calendar);

Expand All @@ -473,7 +468,7 @@ public function getCalendarsForUser($principalUri) {
*/
public function getUsersOwnCalendars($principalUri) {
$principalUri = $this->convertPrincipal($principalUri, true);
$fields = array_values($this->propertyMap);
$fields = array_column($this->propertyMap, 0);
$fields[] = 'id';
$fields[] = 'uri';
$fields[] = 'synctoken';
Expand Down Expand Up @@ -502,10 +497,8 @@ public function getUsersOwnCalendars($principalUri) {
'{' . Plugin::NS_CALDAV . '}supported-calendar-component-set' => new SupportedCalendarComponentSet($components),
'{' . Plugin::NS_CALDAV . '}schedule-calendar-transp' => new ScheduleCalendarTransp($row['transparent']?'transparent':'opaque'),
];
foreach ($this->propertyMap as $xmlName => $dbName) {
$calendar[$xmlName] = $row[$dbName];
}

$calendar = $this->rowToCalendar($row, $calendar);
$calendar = $this->addOwnerPrincipalToCalendar($calendar);
$calendar = $this->addResourceTypeToCalendar($row, $calendar);

Expand Down Expand Up @@ -540,7 +533,7 @@ private function getUserDisplayName($uid) {
* @return array
*/
public function getPublicCalendars() {
$fields = array_values($this->propertyMap);
$fields = array_column($this->propertyMap, 0);
$fields[] = 'a.id';
$fields[] = 'a.uri';
$fields[] = 'a.synctoken';
Expand Down Expand Up @@ -579,10 +572,7 @@ public function getPublicCalendars() {
'{' . \OCA\DAV\DAV\Sharing\Plugin::NS_OWNCLOUD . '}public' => (int)$row['access'] === self::ACCESS_PUBLIC,
];

foreach ($this->propertyMap as $xmlName => $dbName) {
$calendar[$xmlName] = $row[$dbName];
}

$calendar = $this->rowToCalendar($row, $calendar);
$calendar = $this->addOwnerPrincipalToCalendar($calendar);
$calendar = $this->addResourceTypeToCalendar($row, $calendar);

Expand All @@ -601,7 +591,7 @@ public function getPublicCalendars() {
* @throws NotFound
*/
public function getPublicCalendar($uri) {
$fields = array_values($this->propertyMap);
$fields = array_column($this->propertyMap, 0);
$fields[] = 'a.id';
$fields[] = 'a.uri';
$fields[] = 'a.synctoken';
Expand Down Expand Up @@ -647,10 +637,7 @@ public function getPublicCalendar($uri) {
'{' . \OCA\DAV\DAV\Sharing\Plugin::NS_OWNCLOUD . '}public' => (int)$row['access'] === self::ACCESS_PUBLIC,
];

foreach ($this->propertyMap as $xmlName => $dbName) {
$calendar[$xmlName] = $row[$dbName];
}

$calendar = $this->rowToCalendar($row, $calendar);
$calendar = $this->addOwnerPrincipalToCalendar($calendar);
$calendar = $this->addResourceTypeToCalendar($row, $calendar);

Expand All @@ -663,7 +650,7 @@ public function getPublicCalendar($uri) {
* @return array|null
*/
public function getCalendarByUri($principal, $uri) {
$fields = array_values($this->propertyMap);
$fields = array_column($this->propertyMap, 0);
$fields[] = 'id';
$fields[] = 'uri';
$fields[] = 'synctoken';
Expand Down Expand Up @@ -701,10 +688,7 @@ public function getCalendarByUri($principal, $uri) {
'{' . Plugin::NS_CALDAV . '}schedule-calendar-transp' => new ScheduleCalendarTransp($row['transparent']?'transparent':'opaque'),
];

foreach ($this->propertyMap as $xmlName => $dbName) {
$calendar[$xmlName] = $row[$dbName];
}

$calendar = $this->rowToCalendar($row, $calendar);
$calendar = $this->addOwnerPrincipalToCalendar($calendar);
$calendar = $this->addResourceTypeToCalendar($row, $calendar);

Expand All @@ -716,7 +700,7 @@ public function getCalendarByUri($principal, $uri) {
* @return array|null
*/
public function getCalendarById($calendarId) {
$fields = array_values($this->propertyMap);
$fields = array_column($this->propertyMap, 0);
$fields[] = 'id';
$fields[] = 'uri';
$fields[] = 'synctoken';
Expand Down Expand Up @@ -753,10 +737,7 @@ public function getCalendarById($calendarId) {
'{' . Plugin::NS_CALDAV . '}schedule-calendar-transp' => new ScheduleCalendarTransp($row['transparent']?'transparent':'opaque'),
];

foreach ($this->propertyMap as $xmlName => $dbName) {
$calendar[$xmlName] = $row[$dbName];
}

$calendar = $this->rowToCalendar($row, $calendar);
$calendar = $this->addOwnerPrincipalToCalendar($calendar);
$calendar = $this->addResourceTypeToCalendar($row, $calendar);

Expand All @@ -767,7 +748,7 @@ public function getCalendarById($calendarId) {
* @param $subscriptionId
*/
public function getSubscriptionById($subscriptionId) {
$fields = array_values($this->subscriptionPropertyMap);
$fields = array_column($this->subscriptionPropertyMap, 0);
$fields[] = 'id';
$fields[] = 'uri';
$fields[] = 'source';
Expand Down Expand Up @@ -799,13 +780,7 @@ public function getSubscriptionById($subscriptionId) {
'{http://sabredav.org/ns}sync-token' => $row['synctoken']?$row['synctoken']:'0',
];

foreach ($this->subscriptionPropertyMap as $xmlName => $dbName) {
if (!is_null($row[$dbName])) {
$subscription[$xmlName] = $row[$dbName];
}
}

return $subscription;
return $this->rowToSubscription($row, $subscription);
}

/**
Expand Down Expand Up @@ -847,7 +822,7 @@ public function createCalendar($principalUri, $calendarUri, array $properties) {
$values['transparent'] = (int) ($properties[$transp]->getValue() === 'transparent');
}

foreach ($this->propertyMap as $xmlName => $dbName) {
foreach ($this->propertyMap as $xmlName => [$dbName, $type]) {
if (isset($properties[$xmlName])) {
$values[$dbName] = $properties[$xmlName];
}
Expand Down Expand Up @@ -896,7 +871,7 @@ public function updateCalendar($calendarId, PropPatch $propPatch) {
$newValues[$fieldName] = (int) ($propertyValue->getValue() === 'transparent');
break;
default:
$fieldName = $this->propertyMap[$propertyName];
$fieldName = $this->propertyMap[$propertyName][0];
$newValues[$fieldName] = $propertyValue;
break;
}
Expand Down Expand Up @@ -1093,7 +1068,7 @@ public function getDeletedCalendarObjects(int $deletedBefore): array {
'size' => (int) $row['size'],
'component' => strtolower($row['componenttype']),
'classification' => (int) $row['classification'],
'{' . \OCA\DAV\DAV\Sharing\Plugin::NS_NEXTCLOUD . '}deleted-at' => $row['deleted_at'],
'{' . \OCA\DAV\DAV\Sharing\Plugin::NS_NEXTCLOUD . '}deleted-at' => $row['deleted_at'] === null ? $row['deleted_at'] : (int) $row['deleted_at'],
];
}
$stmt->closeCursor();
Expand Down Expand Up @@ -1132,7 +1107,7 @@ public function getDeletedCalendarObjectsByPrincipal(string $principalUri): arra
'size' => (int)$row['size'],
'component' => strtolower($row['componenttype']),
'classification' => (int)$row['classification'],
'{' . \OCA\DAV\DAV\Sharing\Plugin::NS_NEXTCLOUD . '}deleted-at' => $row['deleted_at'],
'{' . \OCA\DAV\DAV\Sharing\Plugin::NS_NEXTCLOUD . '}deleted-at' => $row['deleted_at'] === null ? $row['deleted_at'] : (int) $row['deleted_at'],
];
}
$stmt->closeCursor();
Expand Down Expand Up @@ -2386,7 +2361,7 @@ public function getChangesForCalendar($calendarId, $syncToken, $syncLevel, $limi
* @return array
*/
public function getSubscriptionsForUser($principalUri) {
$fields = array_values($this->subscriptionPropertyMap);
$fields = array_column($this->subscriptionPropertyMap, 0);
$fields[] = 'id';
$fields[] = 'uri';
$fields[] = 'source';
Expand Down Expand Up @@ -2414,13 +2389,7 @@ public function getSubscriptionsForUser($principalUri) {
'{http://sabredav.org/ns}sync-token' => $row['synctoken']?$row['synctoken']:'0',
];

foreach ($this->subscriptionPropertyMap as $xmlName => $dbName) {
if (!is_null($row[$dbName])) {
$subscription[$xmlName] = $row[$dbName];
}
}

$subscriptions[] = $subscription;
$subscriptions[] = $this->rowToSubscription($row, $subscription);
}

return $subscriptions;
Expand Down Expand Up @@ -2451,7 +2420,7 @@ public function createSubscription($principalUri, $uri, array $properties) {

$propertiesBoolean = ['striptodos', 'stripalarms', 'stripattachments'];

foreach ($this->subscriptionPropertyMap as $xmlName => $dbName) {
foreach ($this->subscriptionPropertyMap as $xmlName => [$dbName, $type]) {
if (array_key_exists($xmlName, $properties)) {
$values[$dbName] = $properties[$xmlName];
if (in_array($dbName, $propertiesBoolean)) {
Expand Down Expand Up @@ -2513,7 +2482,7 @@ public function updateSubscription($subscriptionId, PropPatch $propPatch) {
if ($propertyName === '{http://calendarserver.org/ns/}source') {
$newValues['source'] = $propertyValue->getHref();
} else {
$fieldName = $this->subscriptionPropertyMap[$propertyName];
$fieldName = $this->subscriptionPropertyMap[$propertyName][0];
$newValues[$fieldName] = $propertyValue;
}
}
Expand Down Expand Up @@ -3197,4 +3166,42 @@ private function addResourceTypeToCalendar(array $row, array $calendar): array {
}
return $calendar;
}

/**
* Amend the calendar info with database row data
*
* @param array $row
* @param array $calendar
*
* @return array
*/
private function rowToCalendar($row, array $calendar): array {
foreach ($this->propertyMap as $xmlName => [$dbName, $type]) {
$value = $row[$dbName];
if ($value !== null) {
settype($value, $type);
}
$calendar[$xmlName] = $value;
}
return $calendar;
}

/**
* Amend the subscription info with database row data
*
* @param array $row
* @param array $subscription
*
* @return array
*/
private function rowToSubscription($row, array $subscription): array {
foreach ($this->subscriptionPropertyMap as $xmlName => [$dbName, $type]) {
$value = $row[$dbName];
if ($value !== null) {
settype($value, $type);
}
$subscription[$xmlName] = $value;
}
return $subscription;
}
}