CalendarHome.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
  5. * SPDX-License-Identifier: AGPL-3.0-only
  6. */
  7. namespace OCA\DAV\CalDAV;
  8. use OCA\DAV\AppInfo\PluginManager;
  9. use OCA\DAV\CalDAV\Integration\ExternalCalendar;
  10. use OCA\DAV\CalDAV\Integration\ICalendarProvider;
  11. use OCA\DAV\CalDAV\Trashbin\TrashbinHome;
  12. use Psr\Log\LoggerInterface;
  13. use Sabre\CalDAV\Backend\BackendInterface;
  14. use Sabre\CalDAV\Backend\NotificationSupport;
  15. use Sabre\CalDAV\Backend\SchedulingSupport;
  16. use Sabre\CalDAV\Backend\SubscriptionSupport;
  17. use Sabre\CalDAV\Schedule\Inbox;
  18. use Sabre\CalDAV\Subscriptions\Subscription;
  19. use Sabre\DAV\Exception\MethodNotAllowed;
  20. use Sabre\DAV\Exception\NotFound;
  21. use Sabre\DAV\INode;
  22. use Sabre\DAV\MkCol;
  23. class CalendarHome extends \Sabre\CalDAV\CalendarHome {
  24. /** @var \OCP\IL10N */
  25. private $l10n;
  26. /** @var \OCP\IConfig */
  27. private $config;
  28. /** @var PluginManager */
  29. private $pluginManager;
  30. /** @var LoggerInterface */
  31. private $logger;
  32. private ?array $cachedChildren = null;
  33. public function __construct(
  34. BackendInterface $caldavBackend,
  35. array $principalInfo,
  36. LoggerInterface $logger,
  37. private bool $returnCachedSubscriptions
  38. ) {
  39. parent::__construct($caldavBackend, $principalInfo);
  40. $this->l10n = \OC::$server->getL10N('dav');
  41. $this->config = \OC::$server->getConfig();
  42. $this->pluginManager = new PluginManager(
  43. \OC::$server,
  44. \OC::$server->getAppManager()
  45. );
  46. $this->logger = $logger;
  47. }
  48. /**
  49. * @return BackendInterface
  50. */
  51. public function getCalDAVBackend() {
  52. return $this->caldavBackend;
  53. }
  54. /**
  55. * @inheritdoc
  56. */
  57. public function createExtendedCollection($name, MkCol $mkCol): void {
  58. $reservedNames = [
  59. BirthdayService::BIRTHDAY_CALENDAR_URI,
  60. TrashbinHome::NAME,
  61. ];
  62. if (\in_array($name, $reservedNames, true) || ExternalCalendar::doesViolateReservedName($name)) {
  63. throw new MethodNotAllowed('The resource you tried to create has a reserved name');
  64. }
  65. parent::createExtendedCollection($name, $mkCol);
  66. }
  67. /**
  68. * @inheritdoc
  69. */
  70. public function getChildren() {
  71. if ($this->cachedChildren) {
  72. return $this->cachedChildren;
  73. }
  74. $calendars = $this->caldavBackend->getCalendarsForUser($this->principalInfo['uri']);
  75. $objects = [];
  76. foreach ($calendars as $calendar) {
  77. $objects[] = new Calendar($this->caldavBackend, $calendar, $this->l10n, $this->config, $this->logger);
  78. }
  79. if ($this->caldavBackend instanceof SchedulingSupport) {
  80. $objects[] = new Inbox($this->caldavBackend, $this->principalInfo['uri']);
  81. $objects[] = new Outbox($this->config, $this->principalInfo['uri']);
  82. }
  83. // We're adding a notifications node, if it's supported by the backend.
  84. if ($this->caldavBackend instanceof NotificationSupport) {
  85. $objects[] = new \Sabre\CalDAV\Notifications\Collection($this->caldavBackend, $this->principalInfo['uri']);
  86. }
  87. if ($this->caldavBackend instanceof CalDavBackend) {
  88. $objects[] = new TrashbinHome($this->caldavBackend, $this->principalInfo);
  89. }
  90. // If the backend supports subscriptions, we'll add those as well,
  91. if ($this->caldavBackend instanceof SubscriptionSupport) {
  92. foreach ($this->caldavBackend->getSubscriptionsForUser($this->principalInfo['uri']) as $subscription) {
  93. if ($this->returnCachedSubscriptions) {
  94. $objects[] = new CachedSubscription($this->caldavBackend, $subscription);
  95. } else {
  96. $objects[] = new Subscription($this->caldavBackend, $subscription);
  97. }
  98. }
  99. }
  100. foreach ($this->pluginManager->getCalendarPlugins() as $calendarPlugin) {
  101. /** @var ICalendarProvider $calendarPlugin */
  102. $calendars = $calendarPlugin->fetchAllForCalendarHome($this->principalInfo['uri']);
  103. foreach ($calendars as $calendar) {
  104. $objects[] = $calendar;
  105. }
  106. }
  107. $this->cachedChildren = $objects;
  108. return $objects;
  109. }
  110. /**
  111. * @param string $name
  112. *
  113. * @return INode
  114. */
  115. public function getChild($name) {
  116. // Special nodes
  117. if ($name === 'inbox' && $this->caldavBackend instanceof SchedulingSupport) {
  118. return new Inbox($this->caldavBackend, $this->principalInfo['uri']);
  119. }
  120. if ($name === 'outbox' && $this->caldavBackend instanceof SchedulingSupport) {
  121. return new Outbox($this->config, $this->principalInfo['uri']);
  122. }
  123. if ($name === 'notifications' && $this->caldavBackend instanceof NotificationSupport) {
  124. return new \Sabre\CalDAV\Notifications\Collection($this->caldavBackend, $this->principalInfo['uri']);
  125. }
  126. if ($name === TrashbinHome::NAME && $this->caldavBackend instanceof CalDavBackend) {
  127. return new TrashbinHome($this->caldavBackend, $this->principalInfo);
  128. }
  129. // Calendar - this covers all "regular" calendars, but not shared
  130. // only check if the method is available
  131. if($this->caldavBackend instanceof CalDavBackend) {
  132. $calendar = $this->caldavBackend->getCalendarByUri($this->principalInfo['uri'], $name);
  133. if(!empty($calendar)) {
  134. return new Calendar($this->caldavBackend, $calendar, $this->l10n, $this->config, $this->logger);
  135. }
  136. }
  137. // Fallback to cover shared calendars
  138. foreach ($this->caldavBackend->getCalendarsForUser($this->principalInfo['uri']) as $calendar) {
  139. if ($calendar['uri'] === $name) {
  140. return new Calendar($this->caldavBackend, $calendar, $this->l10n, $this->config, $this->logger);
  141. }
  142. }
  143. if ($this->caldavBackend instanceof SubscriptionSupport) {
  144. foreach ($this->caldavBackend->getSubscriptionsForUser($this->principalInfo['uri']) as $subscription) {
  145. if ($subscription['uri'] === $name) {
  146. if ($this->returnCachedSubscriptions) {
  147. return new CachedSubscription($this->caldavBackend, $subscription);
  148. }
  149. return new Subscription($this->caldavBackend, $subscription);
  150. }
  151. }
  152. }
  153. if (ExternalCalendar::isAppGeneratedCalendar($name)) {
  154. [$appId, $calendarUri] = ExternalCalendar::splitAppGeneratedCalendarUri($name);
  155. foreach ($this->pluginManager->getCalendarPlugins() as $calendarPlugin) {
  156. /** @var ICalendarProvider $calendarPlugin */
  157. if ($calendarPlugin->getAppId() !== $appId) {
  158. continue;
  159. }
  160. if ($calendarPlugin->hasCalendarInCalendarHome($this->principalInfo['uri'], $calendarUri)) {
  161. return $calendarPlugin->getCalendarInCalendarHome($this->principalInfo['uri'], $calendarUri);
  162. }
  163. }
  164. }
  165. throw new NotFound('Node with name \'' . $name . '\' could not be found');
  166. }
  167. /**
  168. * @param array $filters
  169. * @param integer|null $limit
  170. * @param integer|null $offset
  171. */
  172. public function calendarSearch(array $filters, $limit = null, $offset = null) {
  173. $principalUri = $this->principalInfo['uri'];
  174. return $this->caldavBackend->calendarSearch($principalUri, $filters, $limit, $offset);
  175. }
  176. }