1
0

CalendarHome.php 7.5 KB

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