You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

AppCalendar.php 5.7KB

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