|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace OCA\DAV\CalDAV\AppCalendar; |
| 4 | + |
| 5 | +use OCA\DAV\CalDAV\Plugin; |
| 6 | +use OCA\DAV\CalDAV\Integration\ExternalCalendar; |
| 7 | +use OCP\Calendar\ICalendar; |
| 8 | +use OCP\Calendar\ICreateFromString; |
| 9 | +use OCP\Constants; |
| 10 | +use Sabre\CalDAV\CalendarQueryValidator; |
| 11 | +use Sabre\CalDAV\ICalendarObject; |
| 12 | +use Sabre\CalDAV\Xml\Property\SupportedCalendarComponentSet; |
| 13 | +use Sabre\DAV\Exception\Forbidden; |
| 14 | +use Sabre\DAV\Exception\NotFound; |
| 15 | +use Sabre\DAV\PropPatch; |
| 16 | +use Sabre\VObject\Reader; |
| 17 | + |
| 18 | +class AppCalendar extends ExternalCalendar { |
| 19 | + protected string $principal; |
| 20 | + protected ICalendar $calendar; |
| 21 | + |
| 22 | + public function __construct(string $appId, ICalendar $calendar, string $principal) { |
| 23 | + parent::__construct($appId, $calendar->getUri()); |
| 24 | + $this->principal = $principal; |
| 25 | + $this->calendar = $calendar; |
| 26 | + } |
| 27 | + |
| 28 | + public function getOwner(): ?string { |
| 29 | + return $this->principal; |
| 30 | + } |
| 31 | + |
| 32 | + public function getGroup(): ?string { |
| 33 | + return null; |
| 34 | + } |
| 35 | + |
| 36 | + public function getACL(): array { |
| 37 | + $acl = [ |
| 38 | + [ |
| 39 | + 'privilege' => '{DAV:}read', |
| 40 | + 'principal' => $this->getOwner(), |
| 41 | + 'protected' => true, |
| 42 | + ], |
| 43 | + [ |
| 44 | + 'privilege' => '{DAV:}write-properties', |
| 45 | + 'principal' => $this->getOwner(), |
| 46 | + 'protected' => true, |
| 47 | + ] |
| 48 | + ]; |
| 49 | + if ($this->calendar instanceof ICreateFromString && |
| 50 | + $this->calendar->getPermissions() & Constants::PERMISSION_CREATE) { |
| 51 | + $acl[] = [ |
| 52 | + 'privilege' => '{DAV:}write', |
| 53 | + 'principal' => $this->getOwner(), |
| 54 | + 'protected' => true, |
| 55 | + ]; |
| 56 | + } |
| 57 | + return $acl; |
| 58 | + } |
| 59 | + |
| 60 | + public function setACL(array $acl): void { |
| 61 | + throw new Forbidden('Setting ACL is not supported on this node'); |
| 62 | + } |
| 63 | + |
| 64 | + public function getSupportedPrivilegeSet(): ?array { |
| 65 | + // Use the default one |
| 66 | + return null; |
| 67 | + } |
| 68 | + |
| 69 | + public function getLastModified(): ?int { |
| 70 | + // unknown |
| 71 | + return null; |
| 72 | + } |
| 73 | + |
| 74 | + public function delete(): void { |
| 75 | + // No delete method in OCP\Calendar\ICalendar |
| 76 | + throw new Forbidden('Deleting an entry is not implemented'); |
| 77 | + } |
| 78 | + |
| 79 | + public function createFile($name, $data = null) { |
| 80 | + if ($this->calendar instanceof ICreateFromString) { |
| 81 | + if (is_resource($data)) $data = stream_get_contents($data); |
| 82 | + $this->calendar->createFromString($name, is_null($data) ? '' : $data); |
| 83 | + return null; |
| 84 | + } else { |
| 85 | + throw new Forbidden('Creating a new entry is not implemented'); |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + public function getProperties($properties) { |
| 90 | + return [ |
| 91 | + '{DAV:}displayname' => $this->calendar->getDisplayName() ?: $this->calendar->getKey(), |
| 92 | + '{http://apple.com/ns/ical/}calendar-color' => '#' . ($this->calendar->getDisplayColor() ?: '0082c9'), |
| 93 | + '{' . Plugin::NS_CALDAV . '}supported-calendar-component-set' => new SupportedCalendarComponentSet(['VEVENT', 'VTODO']), |
| 94 | + ]; |
| 95 | + } |
| 96 | + |
| 97 | + public function calendarQuery(array $filters) { |
| 98 | + $result = []; |
| 99 | + $objects = $this->getChildren(); |
| 100 | + |
| 101 | + foreach ($objects as $object) { |
| 102 | + if ($this->validateFilterForObject($object, $filters)) { |
| 103 | + $result[] = $object->getName(); |
| 104 | + } |
| 105 | + } |
| 106 | + |
| 107 | + return $result; |
| 108 | + } |
| 109 | + |
| 110 | + protected function validateFilterForObject(ICalendarObject $object, array $filters): bool { |
| 111 | + /** @var \Sabre\VObject\Component\VCalendar */ |
| 112 | + $vObject = Reader::read($object->get()); |
| 113 | + |
| 114 | + $validator = new CalendarQueryValidator(); |
| 115 | + $result = $validator->validate($vObject, $filters); |
| 116 | + |
| 117 | + // Destroy circular references so PHP will GC the object. |
| 118 | + $vObject->destroy(); |
| 119 | + |
| 120 | + return $result; |
| 121 | + } |
| 122 | + |
| 123 | + public function childExists($name): bool { |
| 124 | + try { |
| 125 | + $this->getChild($name); |
| 126 | + return true; |
| 127 | + } catch (NotFound $error) { |
| 128 | + return false; |
| 129 | + } |
| 130 | + } |
| 131 | + |
| 132 | + public function getChild($name) { |
| 133 | + $pos = strrpos($name, '.ics'); |
| 134 | + $children = $this->calendar->search(substr($name, 0, $pos === FALSE ? null : $pos), ['UID'], [], 1); |
| 135 | + |
| 136 | + if (count($children) > 0) { |
| 137 | + return new CalendarObject($this, $children[0]); |
| 138 | + } |
| 139 | + |
| 140 | + throw new NotFound('Node not found'); |
| 141 | + } |
| 142 | + |
| 143 | + /** |
| 144 | + * @return ICalendarObject[] |
| 145 | + */ |
| 146 | + public function getChildren(): array { |
| 147 | + $children = array_map(function ($calendar) { |
| 148 | + return new CalendarObject($this, $calendar); |
| 149 | + }, $this->calendar->search('')); |
| 150 | + |
| 151 | + return $children; |
| 152 | + } |
| 153 | + |
| 154 | + public function propPatch(PropPatch $propPatch): void { |
| 155 | + // no setDisplayColor or setDisplayName in \OCP\Calendar\ICalendar |
| 156 | + } |
| 157 | +} |
0 commit comments