aboutsummaryrefslogtreecommitdiffstats
path: root/apps/dav/lib/CalDAV/EmbeddedCalDavServer.php
blob: 21d8c06fa995af42cb1924b61cb2b27baa83a796 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
<?php

declare(strict_types=1);

/**
 * SPDX-FileCopyrightText: 2025 Nextcloud GmbH and Nextcloud contributors
 * SPDX-License-Identifier: AGPL-3.0-or-later
 */

namespace OCA\DAV\CalDAV;

use OCA\DAV\AppInfo\PluginManager;
use OCA\DAV\CalDAV\Auth\CustomPrincipalPlugin;
use OCA\DAV\CalDAV\Auth\PublicPrincipalPlugin;
use OCA\DAV\CalDAV\Publishing\PublishPlugin;
use OCA\DAV\Connector\Sabre\AnonymousOptionsPlugin;
use OCA\DAV\Connector\Sabre\BlockLegacyClientPlugin;
use OCA\DAV\Connector\Sabre\CachingTree;
use OCA\DAV\Connector\Sabre\DavAclPlugin;
use OCA\DAV\Connector\Sabre\ExceptionLoggerPlugin;
use OCA\DAV\Connector\Sabre\LockPlugin;
use OCA\DAV\Connector\Sabre\MaintenancePlugin;
use OCA\DAV\Events\SabrePluginAuthInitEvent;
use OCA\DAV\RootCollection;
use OCA\Theming\ThemingDefaults;
use OCP\App\IAppManager;
use OCP\EventDispatcher\IEventDispatcher;
use OCP\IAppConfig;
use OCP\IConfig;
use OCP\IURLGenerator;
use OCP\L10N\IFactory as IL10NFactory;
use OCP\Server;
use Psr\Log\LoggerInterface;

class EmbeddedCalDavServer {
	private readonly \OCA\DAV\Connector\Sabre\Server $server;

	public function __construct(bool $public = true) {
		$baseUri = \OC::$WEBROOT . '/remote.php/dav/';
		$logger = Server::get(LoggerInterface::class);
		$dispatcher = Server::get(IEventDispatcher::class);
		$appConfig = Server::get(IAppConfig::class);
		$l10nFactory = Server::get(IL10NFactory::class);
		$l10n = $l10nFactory->get('dav');

		$root = new RootCollection();
		$this->server = new \OCA\DAV\Connector\Sabre\Server(new CachingTree($root));

		// Add maintenance plugin
		$this->server->addPlugin(new MaintenancePlugin(Server::get(IConfig::class), $l10n));

		// Set URL explicitly due to reverse-proxy situations
		$this->server->httpRequest->setUrl($baseUri);
		$this->server->setBaseUri($baseUri);

		$this->server->addPlugin(new BlockLegacyClientPlugin(
			Server::get(IConfig::class),
			Server::get(ThemingDefaults::class),
		));
		$this->server->addPlugin(new AnonymousOptionsPlugin());

		// allow custom principal uri option
		if ($public) {
			$this->server->addPlugin(new PublicPrincipalPlugin());
		} else {
			$this->server->addPlugin(new CustomPrincipalPlugin());
		}

		// allow setup of additional auth backends
		$event = new SabrePluginAuthInitEvent($this->server);
		$dispatcher->dispatchTyped($event);

		$this->server->addPlugin(new ExceptionLoggerPlugin('webdav', $logger));
		$this->server->addPlugin(new LockPlugin());
		$this->server->addPlugin(new \Sabre\DAV\Sync\Plugin());

		// acl
		$acl = new DavAclPlugin();
		$acl->principalCollectionSet = [
			'principals/users', 'principals/groups'
		];
		$this->server->addPlugin($acl);

		// calendar plugins
		$this->server->addPlugin(new \OCA\DAV\CalDAV\Plugin());
		$this->server->addPlugin(new \Sabre\CalDAV\ICSExportPlugin());
		$this->server->addPlugin(new \OCA\DAV\CalDAV\Schedule\Plugin(Server::get(IConfig::class), Server::get(LoggerInterface::class), Server::get(DefaultCalendarValidator::class)));
		$this->server->addPlugin(new \Sabre\CalDAV\Subscriptions\Plugin());
		$this->server->addPlugin(new \Sabre\CalDAV\Notifications\Plugin());
		//$this->server->addPlugin(new \OCA\DAV\DAV\Sharing\Plugin($authBackend, \OC::$server->getRequest()));
		$this->server->addPlugin(new PublishPlugin(
			Server::get(IConfig::class),
			Server::get(IURLGenerator::class)
		));
		if ($appConfig->getValueString('dav', 'sendInvitations', 'yes') === 'yes') {
			$this->server->addPlugin(Server::get(\OCA\DAV\CalDAV\Schedule\IMipPlugin::class));
		}

		// wait with registering these until auth is handled and the filesystem is setup
		$this->server->on('beforeMethod:*', function () use ($root): void {
			// register plugins from apps
			$pluginManager = new PluginManager(
				\OC::$server,
				Server::get(IAppManager::class)
			);
			foreach ($pluginManager->getAppPlugins() as $appPlugin) {
				$this->server->addPlugin($appPlugin);
			}
			foreach ($pluginManager->getAppCollections() as $appCollection) {
				$root->addChild($appCollection);
			}
		});
	}

	public function getServer(): \OCA\DAV\Connector\Sabre\Server {
		return $this->server;
	}
}
span class="s1">'apps'] = array('files', 'gallery'); $this->assertAttributeEquals($expectedConfig, 'cache', $this->config); $content = file_get_contents($this->configFile); $expected = "<?php\n\$CONFIG = array (\n 'foo' => 'moo',\n 'beers' => \n array (\n 0 => 'Appenzeller',\n " . " 1 => 'Guinness',\n 2 => 'Kölsch',\n ),\n 'alcohol_free' => false,\n 'bar' => 'red',\n 'apps' => \n " . " array (\n 0 => 'files',\n 1 => 'gallery',\n ),\n);\n"; $this->assertEquals($expected, $content); } public function testSetValues() { $content = file_get_contents($this->configFile); $this->assertEquals(self::TESTCONTENT, $content); // Changing configs to existing values and deleting non-existing once // should not rewrite the config.php $this->config->setValues([ 'foo' => 'bar', 'not_exists' => null, ]); $this->assertAttributeEquals($this->initialConfig, 'cache', $this->config); $content = file_get_contents($this->configFile); $this->assertEquals(self::TESTCONTENT, $content); $this->config->setValues([ 'foo' => 'moo', 'alcohol_free' => null, ]); $expectedConfig = $this->initialConfig; $expectedConfig['foo'] = 'moo'; unset($expectedConfig['alcohol_free']); $this->assertAttributeEquals($expectedConfig, 'cache', $this->config); $content = file_get_contents($this->configFile); $expected = "<?php\n\$CONFIG = array (\n 'foo' => 'moo',\n 'beers' => \n array (\n 0 => 'Appenzeller',\n " . " 1 => 'Guinness',\n 2 => 'Kölsch',\n ),\n);\n"; $this->assertEquals($expected, $content); } public function testDeleteKey() { $this->config->deleteKey('foo'); $expectedConfig = $this->initialConfig; unset($expectedConfig['foo']); $this->assertAttributeEquals($expectedConfig, 'cache', $this->config); $content = file_get_contents($this->configFile); $expected = "<?php\n\$CONFIG = array (\n 'beers' => \n array (\n 0 => 'Appenzeller',\n " . " 1 => 'Guinness',\n 2 => 'Kölsch',\n ),\n 'alcohol_free' => false,\n);\n"; $this->assertEquals($expected, $content); } public function testConfigMerge() { // Create additional config $additionalConfig = '<?php $CONFIG=array("php53"=>"totallyOutdated");'; $additionalConfigPath = $this->randomTmpDir.'additionalConfig.testconfig.php'; file_put_contents($additionalConfigPath, $additionalConfig); // Reinstantiate the config to force a read-in of the additional configs $this->config = new \OC\Config($this->randomTmpDir, 'testconfig.php'); // Ensure that the config value can be read and the config has not been modified $this->assertSame('totallyOutdated', $this->config->getValue('php53', 'bogusValue')); $this->assertEquals(self::TESTCONTENT, file_get_contents($this->configFile)); // Write a new value to the config $this->config->setValue('CoolWebsites', array('demo.owncloud.org', 'owncloud.org', 'owncloud.com')); $expected = "<?php\n\$CONFIG = array (\n 'foo' => 'bar',\n 'beers' => \n array (\n 0 => 'Appenzeller',\n " . " 1 => 'Guinness',\n 2 => 'Kölsch',\n ),\n 'alcohol_free' => false,\n 'php53' => 'totallyOutdated',\n 'CoolWebsites' => \n array (\n " . " 0 => 'demo.owncloud.org',\n 1 => 'owncloud.org',\n 2 => 'owncloud.com',\n ),\n);\n"; $this->assertEquals($expected, file_get_contents($this->configFile)); // Cleanup unlink($additionalConfigPath); } }