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.

CalendarHome.php 6.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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 Sabre\CalDAV\Backend\BackendInterface;
  32. use Sabre\CalDAV\Backend\NotificationSupport;
  33. use Sabre\CalDAV\Backend\SchedulingSupport;
  34. use Sabre\CalDAV\Backend\SubscriptionSupport;
  35. use Sabre\CalDAV\Schedule\Inbox;
  36. use Sabre\CalDAV\Subscriptions\Subscription;
  37. use Sabre\DAV\Exception\MethodNotAllowed;
  38. use Sabre\DAV\Exception\NotFound;
  39. use Sabre\DAV\MkCol;
  40. class CalendarHome extends \Sabre\CalDAV\CalendarHome {
  41. /** @var \OCP\IL10N */
  42. private $l10n;
  43. /** @var \OCP\IConfig */
  44. private $config;
  45. /** @var PluginManager */
  46. private $pluginManager;
  47. /** @var bool */
  48. private $returnCachedSubscriptions = false;
  49. public function __construct(BackendInterface $caldavBackend, $principalInfo) {
  50. parent::__construct($caldavBackend, $principalInfo);
  51. $this->l10n = \OC::$server->getL10N('dav');
  52. $this->config = \OC::$server->getConfig();
  53. $this->pluginManager = new PluginManager(
  54. \OC::$server,
  55. \OC::$server->getAppManager()
  56. );
  57. }
  58. /**
  59. * @return BackendInterface
  60. */
  61. public function getCalDAVBackend() {
  62. return $this->caldavBackend;
  63. }
  64. /**
  65. * @inheritdoc
  66. */
  67. public function createExtendedCollection($name, MkCol $mkCol) {
  68. $reservedNames = [BirthdayService::BIRTHDAY_CALENDAR_URI];
  69. if (\in_array($name, $reservedNames, true) || ExternalCalendar::doesViolateReservedName($name)) {
  70. throw new MethodNotAllowed('The resource you tried to create has a reserved name');
  71. }
  72. parent::createExtendedCollection($name, $mkCol);
  73. }
  74. /**
  75. * @inheritdoc
  76. */
  77. public function getChildren() {
  78. $calendars = $this->caldavBackend->getCalendarsForUser($this->principalInfo['uri']);
  79. $objects = [];
  80. foreach ($calendars as $calendar) {
  81. $objects[] = new Calendar($this->caldavBackend, $calendar, $this->l10n, $this->config);
  82. }
  83. if ($this->caldavBackend instanceof SchedulingSupport) {
  84. $objects[] = new Inbox($this->caldavBackend, $this->principalInfo['uri']);
  85. $objects[] = new Outbox($this->config, $this->principalInfo['uri']);
  86. }
  87. // We're adding a notifications node, if it's supported by the backend.
  88. if ($this->caldavBackend instanceof NotificationSupport) {
  89. $objects[] = new \Sabre\CalDAV\Notifications\Collection($this->caldavBackend, $this->principalInfo['uri']);
  90. }
  91. // If the backend supports subscriptions, we'll add those as well,
  92. if ($this->caldavBackend instanceof SubscriptionSupport) {
  93. foreach ($this->caldavBackend->getSubscriptionsForUser($this->principalInfo['uri']) as $subscription) {
  94. if ($this->returnCachedSubscriptions) {
  95. $objects[] = new CachedSubscription($this->caldavBackend, $subscription);
  96. } else {
  97. $objects[] = new Subscription($this->caldavBackend, $subscription);
  98. }
  99. }
  100. }
  101. foreach ($this->pluginManager->getCalendarPlugins() as $calendarPlugin) {
  102. /** @var ICalendarProvider $calendarPlugin */
  103. $calendars = $calendarPlugin->fetchAllForCalendarHome($this->principalInfo['uri']);
  104. foreach ($calendars as $calendar) {
  105. $objects[] = $calendar;
  106. }
  107. }
  108. return $objects;
  109. }
  110. /**
  111. * @inheritdoc
  112. */
  113. public function getChild($name) {
  114. // Special nodes
  115. if ($name === 'inbox' && $this->caldavBackend instanceof SchedulingSupport) {
  116. return new Inbox($this->caldavBackend, $this->principalInfo['uri']);
  117. }
  118. if ($name === 'outbox' && $this->caldavBackend instanceof SchedulingSupport) {
  119. return new Outbox($this->config, $this->principalInfo['uri']);
  120. }
  121. if ($name === 'notifications' && $this->caldavBackend instanceof NotificationSupport) {
  122. return new \Sabre\CalDAv\Notifications\Collection($this->caldavBackend, $this->principalInfo['uri']);
  123. }
  124. // Calendars
  125. foreach ($this->caldavBackend->getCalendarsForUser($this->principalInfo['uri']) as $calendar) {
  126. if ($calendar['uri'] === $name) {
  127. return new Calendar($this->caldavBackend, $calendar, $this->l10n, $this->config);
  128. }
  129. }
  130. if ($this->caldavBackend instanceof SubscriptionSupport) {
  131. foreach ($this->caldavBackend->getSubscriptionsForUser($this->principalInfo['uri']) as $subscription) {
  132. if ($subscription['uri'] === $name) {
  133. if ($this->returnCachedSubscriptions) {
  134. return new CachedSubscription($this->caldavBackend, $subscription);
  135. }
  136. return new Subscription($this->caldavBackend, $subscription);
  137. }
  138. }
  139. }
  140. if (ExternalCalendar::isAppGeneratedCalendar($name)) {
  141. [$appId, $calendarUri] = ExternalCalendar::splitAppGeneratedCalendarUri($name);
  142. foreach ($this->pluginManager->getCalendarPlugins() as $calendarPlugin) {
  143. /** @var ICalendarProvider $calendarPlugin */
  144. if ($calendarPlugin->getAppId() !== $appId) {
  145. continue;
  146. }
  147. if ($calendarPlugin->hasCalendarInCalendarHome($this->principalInfo['uri'], $calendarUri)) {
  148. return $calendarPlugin->getCalendarInCalendarHome($this->principalInfo['uri'], $calendarUri);
  149. }
  150. }
  151. }
  152. throw new NotFound('Node with name \'' . $name . '\' could not be found');
  153. }
  154. /**
  155. * @param array $filters
  156. * @param integer|null $limit
  157. * @param integer|null $offset
  158. */
  159. public function calendarSearch(array $filters, $limit = null, $offset = null) {
  160. $principalUri = $this->principalInfo['uri'];
  161. return $this->caldavBackend->calendarSearch($principalUri, $filters, $limit, $offset);
  162. }
  163. public function enableCachedSubscriptionsForThisRequest() {
  164. $this->returnCachedSubscriptions = true;
  165. }
  166. }