123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224 |
- <?php
- namespace OCA\DAV\CalDAV;
- use OCA\DAV\AppInfo\PluginManager;
- use OCA\DAV\CalDAV\Integration\ExternalCalendar;
- use OCA\DAV\CalDAV\Integration\ICalendarProvider;
- use OCA\DAV\CalDAV\Trashbin\TrashbinHome;
- use Psr\Log\LoggerInterface;
- use Sabre\CalDAV\Backend\BackendInterface;
- use Sabre\CalDAV\Backend\NotificationSupport;
- use Sabre\CalDAV\Backend\SchedulingSupport;
- use Sabre\CalDAV\Backend\SubscriptionSupport;
- use Sabre\CalDAV\Schedule\Inbox;
- use Sabre\CalDAV\Subscriptions\Subscription;
- use Sabre\DAV\Exception\MethodNotAllowed;
- use Sabre\DAV\Exception\NotFound;
- use Sabre\DAV\INode;
- use Sabre\DAV\MkCol;
- class CalendarHome extends \Sabre\CalDAV\CalendarHome {
-
- private $l10n;
-
- private $config;
-
- private $pluginManager;
-
- private $logger;
- private ?array $cachedChildren = null;
- public function __construct(
- BackendInterface $caldavBackend,
- array $principalInfo,
- LoggerInterface $logger,
- private bool $returnCachedSubscriptions
- ) {
- parent::__construct($caldavBackend, $principalInfo);
- $this->l10n = \OC::$server->getL10N('dav');
- $this->config = \OC::$server->getConfig();
- $this->pluginManager = new PluginManager(
- \OC::$server,
- \OC::$server->getAppManager()
- );
- $this->logger = $logger;
- }
-
- public function getCalDAVBackend() {
- return $this->caldavBackend;
- }
-
- public function createExtendedCollection($name, MkCol $mkCol): void {
- $reservedNames = [
- BirthdayService::BIRTHDAY_CALENDAR_URI,
- TrashbinHome::NAME,
- ];
- if (\in_array($name, $reservedNames, true) || ExternalCalendar::doesViolateReservedName($name)) {
- throw new MethodNotAllowed('The resource you tried to create has a reserved name');
- }
- parent::createExtendedCollection($name, $mkCol);
- }
-
- public function getChildren() {
- if ($this->cachedChildren) {
- return $this->cachedChildren;
- }
- $calendars = $this->caldavBackend->getCalendarsForUser($this->principalInfo['uri']);
- $objects = [];
- foreach ($calendars as $calendar) {
- $objects[] = new Calendar($this->caldavBackend, $calendar, $this->l10n, $this->config, $this->logger);
- }
- if ($this->caldavBackend instanceof SchedulingSupport) {
- $objects[] = new Inbox($this->caldavBackend, $this->principalInfo['uri']);
- $objects[] = new Outbox($this->config, $this->principalInfo['uri']);
- }
-
- if ($this->caldavBackend instanceof NotificationSupport) {
- $objects[] = new \Sabre\CalDAV\Notifications\Collection($this->caldavBackend, $this->principalInfo['uri']);
- }
- if ($this->caldavBackend instanceof CalDavBackend) {
- $objects[] = new TrashbinHome($this->caldavBackend, $this->principalInfo);
- }
-
- if ($this->caldavBackend instanceof SubscriptionSupport) {
- foreach ($this->caldavBackend->getSubscriptionsForUser($this->principalInfo['uri']) as $subscription) {
- if ($this->returnCachedSubscriptions) {
- $objects[] = new CachedSubscription($this->caldavBackend, $subscription);
- } else {
- $objects[] = new Subscription($this->caldavBackend, $subscription);
- }
- }
- }
- foreach ($this->pluginManager->getCalendarPlugins() as $calendarPlugin) {
-
- $calendars = $calendarPlugin->fetchAllForCalendarHome($this->principalInfo['uri']);
- foreach ($calendars as $calendar) {
- $objects[] = $calendar;
- }
- }
- $this->cachedChildren = $objects;
- return $objects;
- }
-
- public function getChild($name) {
-
- if ($name === 'inbox' && $this->caldavBackend instanceof SchedulingSupport) {
- return new Inbox($this->caldavBackend, $this->principalInfo['uri']);
- }
- if ($name === 'outbox' && $this->caldavBackend instanceof SchedulingSupport) {
- return new Outbox($this->config, $this->principalInfo['uri']);
- }
- if ($name === 'notifications' && $this->caldavBackend instanceof NotificationSupport) {
- return new \Sabre\CalDAV\Notifications\Collection($this->caldavBackend, $this->principalInfo['uri']);
- }
- if ($name === TrashbinHome::NAME && $this->caldavBackend instanceof CalDavBackend) {
- return new TrashbinHome($this->caldavBackend, $this->principalInfo);
- }
-
-
- if($this->caldavBackend instanceof CalDavBackend) {
- $calendar = $this->caldavBackend->getCalendarByUri($this->principalInfo['uri'], $name);
- if(!empty($calendar)) {
- return new Calendar($this->caldavBackend, $calendar, $this->l10n, $this->config, $this->logger);
- }
- }
-
- foreach ($this->caldavBackend->getCalendarsForUser($this->principalInfo['uri']) as $calendar) {
- if ($calendar['uri'] === $name) {
- return new Calendar($this->caldavBackend, $calendar, $this->l10n, $this->config, $this->logger);
- }
- }
- if ($this->caldavBackend instanceof SubscriptionSupport) {
- foreach ($this->caldavBackend->getSubscriptionsForUser($this->principalInfo['uri']) as $subscription) {
- if ($subscription['uri'] === $name) {
- if ($this->returnCachedSubscriptions) {
- return new CachedSubscription($this->caldavBackend, $subscription);
- }
- return new Subscription($this->caldavBackend, $subscription);
- }
- }
- }
- if (ExternalCalendar::isAppGeneratedCalendar($name)) {
- [$appId, $calendarUri] = ExternalCalendar::splitAppGeneratedCalendarUri($name);
- foreach ($this->pluginManager->getCalendarPlugins() as $calendarPlugin) {
-
- if ($calendarPlugin->getAppId() !== $appId) {
- continue;
- }
- if ($calendarPlugin->hasCalendarInCalendarHome($this->principalInfo['uri'], $calendarUri)) {
- return $calendarPlugin->getCalendarInCalendarHome($this->principalInfo['uri'], $calendarUri);
- }
- }
- }
- throw new NotFound('Node with name \'' . $name . '\' could not be found');
- }
-
- public function calendarSearch(array $filters, $limit = null, $offset = null) {
- $principalUri = $this->principalInfo['uri'];
- return $this->caldavBackend->calendarSearch($principalUri, $filters, $limit, $offset);
- }
- }
|