Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 44 additions & 0 deletions apps/dav/lib/CalDAV/ICalendarHomePlugin.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php
/**
* @copyright Copyright (c) 2017 Julius Härtl <jus@bitgrid.net>
*
* @author Julius Härtl <jus@bitgrid.net>
*
* @license GNU AGPL version 3 or any later version
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/

namespace OCA\DAV\CalDAV;

/**
* Interface that allows a SabreDAV plugin to register a calendar home to be
* returned as calendar-home-set
*/
interface ICalendarHomePlugin {

/**
* Returns the path to a principal's calendar home.
*
* The return url must not end with a slash.
* This function should return null in case a principal did not have
* a calendar home.
*
* @param string $principalUrl
* @return string|null
*/
public function getCalendarHomeForPrincipal($principalUrl);

}
61 changes: 55 additions & 6 deletions apps/dav/lib/CalDAV/Plugin.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?php
/**
* @author Julius Härtl <jus@bitgrid.net>
* @author Thomas Müller <thomas.mueller@tmit.eu>
*
* @copyright Copyright (c) 2016, ownCloud GmbH.
Expand All @@ -21,21 +22,69 @@

namespace OCA\DAV\CalDAV;

use Sabre\HTTP\URLUtil;
use Sabre\DAV\Server;
use Sabre\DAV;
use Sabre\DAV\Xml\Property\LocalHref;
use Sabre\DAVACL\IPrincipal;

class Plugin extends \Sabre\CalDAV\Plugin {
class Plugin extends \Sabre\CalDAV\Plugin implements ICalendarHomePlugin {

/**
* @inheritdoc
* Initializes the plugin
*
* @param Server $server
* @return void
*/
function getCalendarHomeForPrincipal($principalUrl) {
public function initialize(Server $server) {
$this->server = $server;
$server->on('propFind', [$this, 'propFind'], 90);
}

/**
* PropFind
*
* This method handler is invoked before any after properties for a
* resource are fetched. This allows us to add in any CalDAV specific
* properties.
*
* @param DAV\PropFind $propFind
* @param DAV\INode $node
* @return void
*/
public function propFind(DAV\PropFind $propFind, DAV\INode $node) {
if ($node instanceof IPrincipal) {
$principalUrl = $node->getPrincipalUrl();
$propFind->handle('{' . self::NS_CALDAV . '}calendar-home-set', function () use ($principalUrl) {
$calendarHomes = [];
// Make sure the dav apps caldav endpoint is at the first place
$calendarHomePath = $this->getCalendarHomeForPrincipal($principalUrl);
if ($calendarHomePath !== null) {
$calendarHomes[] = $calendarHomePath;
}
foreach ($this->server->getPlugins() as $plugin) {
if ($plugin instanceof ICalendarHomePlugin && $plugin !== $this) {
$calendarHomePath = $plugin->getCalendarHomeForPrincipal($principalUrl);
if ($calendarHomePath !== null) {
$calendarHomes[] = $calendarHomePath;
}
}
}
return new LocalHref($calendarHomes);
});
}
}

/**
* Add the Nextcloud default calendar home
*
* @inheritdoc
*/
public function getCalendarHomeForPrincipal($principalUrl) {
if (strrpos($principalUrl, 'principals/users', -strlen($principalUrl)) !== false) {
list(, $principalId) = \Sabre\Uri\split($principalUrl);
return self::CALENDAR_ROOT .'/' . $principalId;
}

return;
return null;
}

}
1 change: 1 addition & 0 deletions apps/dav/lib/Server.php
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ public function __construct(IRequest $request, $baseUri) {
$this->server->addPlugin($acl);

// calendar plugins
$this->server->addPlugin(new \Sabre\CalDAV\Plugin());
$this->server->addPlugin(new \OCA\DAV\CalDAV\Plugin());
$this->server->addPlugin(new \Sabre\CalDAV\ICSExportPlugin());
$this->server->addPlugin(new \OCA\DAV\CalDAV\Schedule\Plugin());
Expand Down