CalendarHome.php 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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 bool */
  51. private $returnCachedSubscriptions = false;
  52. /** @var LoggerInterface */
  53. private $logger;
  54. public function __construct(BackendInterface $caldavBackend, $principalInfo, LoggerInterface $logger) {
  55. parent::__construct($caldavBackend, $principalInfo);
  56. $this->l10n = \OC::$server->getL10N('dav');
  57. $this->config = \OC::$server->getConfig();
  58. $this->pluginManager = new PluginManager(
  59. \OC::$server,
  60. \OC::$server->getAppManager()
  61. );
  62. $this->logger = $logger;
  63. }
  64. /**
  65. * @return BackendInterface
  66. */
  67. public function getCalDAVBackend() {
  68. return $this->caldavBackend;
  69. }
  70. /**
  71. * @inheritdoc
  72. */
  73. public function createExtendedCollection($name, MkCol $mkCol): void {
  74. $reservedNames = [
  75. BirthdayService::BIRTHDAY_CALENDAR_URI,
  76. TrashbinHome::NAME,
  77. ];
  78. if (\in_array($name, $reservedNames, true) || ExternalCalendar::doesViolateReservedName($name)) {
  79. throw new MethodNotAllowed('The resource you tried to create has a reserved name');
  80. }
  81. parent::createExtendedCollection($name, $mkCol);
  82. }
  83. /**
  84. * @inheritdoc
  85. */
  86. public function getChildren() {
  87. $calendars = $this->caldavBackend->getCalendarsForUser($this->principalInfo['uri']);
  88. $objects = [];
  89. foreach ($calendars as $calendar) {
  90. $objects[] = new Calendar($this->caldavBackend, $calendar, $this->l10n, $this->config, $this->logger);
  91. }
  92. if ($this->caldavBackend instanceof SchedulingSupport) {
  93. $objects[] = new Inbox($this->caldavBackend, $this->principalInfo['uri']);
  94. $objects[] = new Outbox($this->config, $this->principalInfo['uri']);
  95. }
  96. // We're adding a notifications node, if it's supported by the backend.
  97. if ($this->caldavBackend instanceof NotificationSupport) {
  98. $objects[] = new \Sabre\CalDAV\Notifications\Collection($this->caldavBackend, $this->principalInfo['uri']);
  99. }
  100. if ($this->caldavBackend instanceof CalDavBackend) {
  101. $objects[] = new TrashbinHome($this->caldavBackend, $this->principalInfo);
  102. }
  103. // If the backend supports subscriptions, we'll add those as well,
  104. if ($this->caldavBackend instanceof SubscriptionSupport) {
  105. foreach ($this->caldavBackend->getSubscriptionsForUser($this->principalInfo['uri']) as $subscription) {
  106. if ($this->returnCachedSubscriptions) {
  107. $objects[] = new CachedSubscription($this->caldavBackend, $subscription);
  108. } else {
  109. $objects[] = new Subscription($this->caldavBackend, $subscription);
  110. }
  111. }
  112. }
  113. foreach ($this->pluginManager->getCalendarPlugins() as $calendarPlugin) {
  114. /** @var ICalendarProvider $calendarPlugin */
  115. $calendars = $calendarPlugin->fetchAllForCalendarHome($this->principalInfo['uri']);
  116. foreach ($calendars as $calendar) {
  117. $objects[] = $calendar;
  118. }
  119. }
  120. return $objects;
  121. }
  122. /**
  123. * @param string $name
  124. *
  125. * @return INode
  126. */
  127. public function getChild($name) {
  128. // Special nodes
  129. if ($name === 'inbox' && $this->caldavBackend instanceof SchedulingSupport) {
  130. return new Inbox($this->caldavBackend, $this->principalInfo['uri']);
  131. }
  132. if ($name === 'outbox' && $this->caldavBackend instanceof SchedulingSupport) {
  133. return new Outbox($this->config, $this->principalInfo['uri']);
  134. }
  135. if ($name === 'notifications' && $this->caldavBackend instanceof NotificationSupport) {
  136. return new \Sabre\CalDAV\Notifications\Collection($this->caldavBackend, $this->principalInfo['uri']);
  137. }
  138. if ($name === TrashbinHome::NAME && $this->caldavBackend instanceof CalDavBackend) {
  139. return new TrashbinHome($this->caldavBackend, $this->principalInfo);
  140. }
  141. // Calendar - this covers all "regular" calendars, but not shared
  142. // only check if the method is available
  143. if($this->caldavBackend instanceof CalDavBackend) {
  144. $calendar = $this->caldavBackend->getCalendarByUri($this->principalInfo['uri'], $name);
  145. if(!empty($calendar)) {
  146. return new Calendar($this->caldavBackend, $calendar, $this->l10n, $this->config, $this->logger);
  147. }
  148. }
  149. // Fallback to cover shared calendars
  150. foreach ($this->caldavBackend->getCalendarsForUser($this->principalInfo['uri']) as $calendar) {
  151. if ($calendar['uri'] === $name) {
  152. return new Calendar($this->caldavBackend, $calendar, $this->l10n, $this->config, $this->logger);
  153. }
  154. }
  155. if ($this->caldavBackend instanceof SubscriptionSupport) {
  156. foreach ($this->caldavBackend->getSubscriptionsForUser($this->principalInfo['uri']) as $subscription) {
  157. if ($subscription['uri'] === $name) {
  158. if ($this->returnCachedSubscriptions) {
  159. return new CachedSubscription($this->caldavBackend, $subscription);
  160. }
  161. return new Subscription($this->caldavBackend, $subscription);
  162. }
  163. }
  164. }
  165. if (ExternalCalendar::isAppGeneratedCalendar($name)) {
  166. [$appId, $calendarUri] = ExternalCalendar::splitAppGeneratedCalendarUri($name);
  167. foreach ($this->pluginManager->getCalendarPlugins() as $calendarPlugin) {
  168. /** @var ICalendarProvider $calendarPlugin */
  169. if ($calendarPlugin->getAppId() !== $appId) {
  170. continue;
  171. }
  172. if ($calendarPlugin->hasCalendarInCalendarHome($this->principalInfo['uri'], $calendarUri)) {
  173. return $calendarPlugin->getCalendarInCalendarHome($this->principalInfo['uri'], $calendarUri);
  174. }
  175. }
  176. }
  177. throw new NotFound('Node with name \'' . $name . '\' could not be found');
  178. }
  179. /**
  180. * @param array $filters
  181. * @param integer|null $limit
  182. * @param integer|null $offset
  183. */
  184. public function calendarSearch(array $filters, $limit = null, $offset = null) {
  185. $principalUri = $this->principalInfo['uri'];
  186. return $this->caldavBackend->calendarSearch($principalUri, $filters, $limit, $offset);
  187. }
  188. public function enableCachedSubscriptionsForThisRequest() {
  189. $this->returnCachedSubscriptions = true;
  190. }
  191. }