AppCalendar.php 5.0 KB

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