CalendarHome.php 6.2 KB

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