CalendarHome.php 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  6. * @author Georg Ehrke <oc.list@georgehrke.com>
  7. * @author Joas Schilling <coding@schilljs.com>
  8. * @author Lukas Reschke <lukas@statuscode.ch>
  9. * @author Roeland Jago Douma <roeland@famdouma.nl>
  10. * @author Thomas Müller <thomas.mueller@tmit.eu>
  11. *
  12. * @license AGPL-3.0
  13. *
  14. * This code is free software: you can redistribute it and/or modify
  15. * it under the terms of the GNU Affero General Public License, version 3,
  16. * as published by the Free Software Foundation.
  17. *
  18. * This program is distributed in the hope that it will be useful,
  19. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. * GNU Affero General Public License for more details.
  22. *
  23. * You should have received a copy of the GNU Affero General Public License, version 3,
  24. * along with this program. If not, see <http://www.gnu.org/licenses/>
  25. *
  26. */
  27. namespace OCA\DAV\CalDAV;
  28. use OCA\DAV\AppInfo\PluginManager;
  29. use OCA\DAV\CalDAV\Integration\ExternalCalendar;
  30. use OCA\DAV\CalDAV\Integration\ICalendarProvider;
  31. use OCA\DAV\CalDAV\Trashbin\TrashbinHome;
  32. use Psr\Log\LoggerInterface;
  33. use Sabre\CalDAV\Backend\BackendInterface;
  34. use Sabre\CalDAV\Backend\NotificationSupport;
  35. use Sabre\CalDAV\Backend\SchedulingSupport;
  36. use Sabre\CalDAV\Backend\SubscriptionSupport;
  37. use Sabre\CalDAV\Schedule\Inbox;
  38. use Sabre\CalDAV\Subscriptions\Subscription;
  39. use Sabre\DAV\Exception\MethodNotAllowed;
  40. use Sabre\DAV\Exception\NotFound;
  41. use Sabre\DAV\INode;
  42. use Sabre\DAV\MkCol;
  43. class CalendarHome extends \Sabre\CalDAV\CalendarHome {
  44. /** @var \OCP\IL10N */
  45. private $l10n;
  46. /** @var \OCP\IConfig */
  47. private $config;
  48. /** @var PluginManager */
  49. private $pluginManager;
  50. /** @var LoggerInterface */
  51. private $logger;
  52. private ?array $cachedChildren = null;
  53. public function __construct(
  54. BackendInterface $caldavBackend,
  55. array $principalInfo,
  56. LoggerInterface $logger,
  57. private bool $returnCachedSubscriptions
  58. ) {
  59. parent::__construct($caldavBackend, $principalInfo);
  60. $this->l10n = \OC::$server->getL10N('dav');
  61. $this->config = \OC::$server->getConfig();
  62. $this->pluginManager = new PluginManager(
  63. \OC::$server,
  64. \OC::$server->getAppManager()
  65. );
  66. $this->logger = $logger;
  67. }
  68. /**
  69. * @return BackendInterface
  70. */
  71. public function getCalDAVBackend() {
  72. return $this->caldavBackend;
  73. }
  74. /**
  75. * @inheritdoc
  76. */
  77. public function createExtendedCollection($name, MkCol $mkCol): void {
  78. $reservedNames = [
  79. BirthdayService::BIRTHDAY_CALENDAR_URI,
  80. TrashbinHome::NAME,
  81. ];
  82. if (\in_array($name, $reservedNames, true) || ExternalCalendar::doesViolateReservedName($name)) {
  83. throw new MethodNotAllowed('The resource you tried to create has a reserved name');
  84. }
  85. parent::createExtendedCollection($name, $mkCol);
  86. }
  87. /**
  88. * @inheritdoc
  89. */
  90. public function getChildren() {
  91. if ($this->cachedChildren) {
  92. return $this->cachedChildren;
  93. }
  94. $calendars = $this->caldavBackend->getCalendarsForUser($this->principalInfo['uri']);
  95. $objects = [];
  96. foreach ($calendars as $calendar) {
  97. $objects[] = new Calendar($this->caldavBackend, $calendar, $this->l10n, $this->config, $this->logger);
  98. }
  99. if ($this->caldavBackend instanceof SchedulingSupport) {
  100. $objects[] = new Inbox($this->caldavBackend, $this->principalInfo['uri']);
  101. $objects[] = new Outbox($this->config, $this->principalInfo['uri']);
  102. }
  103. // We're adding a notifications node, if it's supported by the backend.
  104. if ($this->caldavBackend instanceof NotificationSupport) {
  105. $objects[] = new \Sabre\CalDAV\Notifications\Collection($this->caldavBackend, $this->principalInfo['uri']);
  106. }
  107. if ($this->caldavBackend instanceof CalDavBackend) {
  108. $objects[] = new TrashbinHome($this->caldavBackend, $this->principalInfo);
  109. }
  110. // If the backend supports subscriptions, we'll add those as well,
  111. if ($this->caldavBackend instanceof SubscriptionSupport) {
  112. foreach ($this->caldavBackend->getSubscriptionsForUser($this->principalInfo['uri']) as $subscription) {
  113. if ($this->returnCachedSubscriptions) {
  114. $objects[] = new CachedSubscription($this->caldavBackend, $subscription);
  115. } else {
  116. $objects[] = new Subscription($this->caldavBackend, $subscription);
  117. }
  118. }
  119. }
  120. foreach ($this->pluginManager->getCalendarPlugins() as $calendarPlugin) {
  121. /** @var ICalendarProvider $calendarPlugin */
  122. $calendars = $calendarPlugin->fetchAllForCalendarHome($this->principalInfo['uri']);
  123. foreach ($calendars as $calendar) {
  124. $objects[] = $calendar;
  125. }
  126. }
  127. $this->cachedChildren = $objects;
  128. return $objects;
  129. }
  130. /**
  131. * @param string $name
  132. *
  133. * @return INode
  134. */
  135. public function getChild($name) {
  136. // Special nodes
  137. if ($name === 'inbox' && $this->caldavBackend instanceof SchedulingSupport) {
  138. return new Inbox($this->caldavBackend, $this->principalInfo['uri']);
  139. }
  140. if ($name === 'outbox' && $this->caldavBackend instanceof SchedulingSupport) {
  141. return new Outbox($this->config, $this->principalInfo['uri']);
  142. }
  143. if ($name === 'notifications' && $this->caldavBackend instanceof NotificationSupport) {
  144. return new \Sabre\CalDAV\Notifications\Collection($this->caldavBackend, $this->principalInfo['uri']);
  145. }
  146. if ($name === TrashbinHome::NAME && $this->caldavBackend instanceof CalDavBackend) {
  147. return new TrashbinHome($this->caldavBackend, $this->principalInfo);
  148. }
  149. // Calendar - this covers all "regular" calendars, but not shared
  150. // only check if the method is available
  151. if($this->caldavBackend instanceof CalDavBackend) {
  152. $calendar = $this->caldavBackend->getCalendarByUri($this->principalInfo['uri'], $name);
  153. if(!empty($calendar)) {
  154. return new Calendar($this->caldavBackend, $calendar, $this->l10n, $this->config, $this->logger);
  155. }
  156. }
  157. // Fallback to cover shared calendars
  158. foreach ($this->caldavBackend->getCalendarsForUser($this->principalInfo['uri']) as $calendar) {
  159. if ($calendar['uri'] === $name) {
  160. return new Calendar($this->caldavBackend, $calendar, $this->l10n, $this->config, $this->logger);
  161. }
  162. }
  163. if ($this->caldavBackend instanceof SubscriptionSupport) {
  164. foreach ($this->caldavBackend->getSubscriptionsForUser($this->principalInfo['uri']) as $subscription) {
  165. if ($subscription['uri'] === $name) {
  166. if ($this->returnCachedSubscriptions) {
  167. return new CachedSubscription($this->caldavBackend, $subscription);
  168. }
  169. return new Subscription($this->caldavBackend, $subscription);
  170. }
  171. }
  172. }
  173. if (ExternalCalendar::isAppGeneratedCalendar($name)) {
  174. [$appId, $calendarUri] = ExternalCalendar::splitAppGeneratedCalendarUri($name);
  175. foreach ($this->pluginManager->getCalendarPlugins() as $calendarPlugin) {
  176. /** @var ICalendarProvider $calendarPlugin */
  177. if ($calendarPlugin->getAppId() !== $appId) {
  178. continue;
  179. }
  180. if ($calendarPlugin->hasCalendarInCalendarHome($this->principalInfo['uri'], $calendarUri)) {
  181. return $calendarPlugin->getCalendarInCalendarHome($this->principalInfo['uri'], $calendarUri);
  182. }
  183. }
  184. }
  185. throw new NotFound('Node with name \'' . $name . '\' could not be found');
  186. }
  187. /**
  188. * @param array $filters
  189. * @param integer|null $limit
  190. * @param integer|null $offset
  191. */
  192. public function calendarSearch(array $filters, $limit = null, $offset = null) {
  193. $principalUri = $this->principalInfo['uri'];
  194. return $this->caldavBackend->calendarSearch($principalUri, $filters, $limit, $offset);
  195. }
  196. }