serverfactory.php 4.28 KB
Newer Older
1
2
<?php
/**
3
 * @author Joas Schilling <nickvergessen@owncloud.com>
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
 * @author Robin Appelman <icewind@owncloud.com>
 *
 * @copyright Copyright (c) 2015, ownCloud, Inc.
 * @license AGPL-3.0
 *
 * This code is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License, version 3,
 * as published by the Free Software Foundation.
 *
 * 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, version 3,
 * along with this program.  If not, see <http://www.gnu.org/licenses/>
 *
 */

23
namespace OCA\DAV\Connector\Sabre;
24
25
26
27
28
29
30
31

use OCP\Files\Mount\IMountManager;
use OCP\IConfig;
use OCP\IDBConnection;
use OCP\ILogger;
use OCP\ITagManager;
use OCP\IUserSession;
use Sabre\DAV\Auth\Backend\BackendInterface;
32
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
33
34
35
36
37
38
39
40

class ServerFactory {
	public function __construct(
		IConfig $config,
		ILogger $logger,
		IDBConnection $databaseConnection,
		IUserSession $userSession,
		IMountManager $mountManager,
41
42
		ITagManager $tagManager,
		EventDispatcherInterface $dispatcher
43
44
45
46
47
48
49
	) {
		$this->config = $config;
		$this->logger = $logger;
		$this->databaseConnection = $databaseConnection;
		$this->userSession = $userSession;
		$this->mountManager = $mountManager;
		$this->tagManager = $tagManager;
50
		$this->dispatcher = $dispatcher;
51
52
53
54
55
56
57
58
59
60
61
	}

	/**
	 * @param string $baseUri
	 * @param string $requestUri
	 * @param BackendInterface $authBackend
	 * @param callable $viewCallBack callback that should return the view for the dav endpoint
	 * @return Server
	 */
	public function createServer($baseUri, $requestUri, BackendInterface $authBackend, callable $viewCallBack) {
		// Fire up server
62
63
		$objectTree = new \OCA\DAV\Connector\Sabre\ObjectTree();
		$server = new \OCA\DAV\Connector\Sabre\Server($objectTree);
64
65
66
67
68
69
		// Set URL explicitly due to reverse-proxy situations
		$server->httpRequest->setUrl($requestUri);
		$server->setBaseUri($baseUri);

		// Load plugins
		$defaults = new \OC_Defaults();
70
71
		$server->addPlugin(new \OCA\DAV\Connector\Sabre\MaintenancePlugin($this->config));
		$server->addPlugin(new \OCA\DAV\Connector\Sabre\BlockLegacyClientPlugin($this->config));
72
73
		$server->addPlugin(new \Sabre\DAV\Auth\Plugin($authBackend, $defaults->getName()));
		// FIXME: The following line is a workaround for legacy components relying on being able to send a GET to /
74
75
76
77
		$server->addPlugin(new \OCA\DAV\Connector\Sabre\DummyGetResponsePlugin());
		$server->addPlugin(new \OCA\DAV\Connector\Sabre\ExceptionLoggerPlugin('webdav', $this->logger));
		$server->addPlugin(new \OCA\DAV\Connector\Sabre\LockPlugin($objectTree));
		$server->addPlugin(new \OCA\DAV\Connector\Sabre\ListenerPlugin($this->dispatcher));
78
79
80

		// wait with registering these until auth is handled and the filesystem is setup
		$server->on('beforeMethod', function () use ($server, $objectTree, $viewCallBack) {
Thomas Müller's avatar
Thomas Müller committed
81
82
83
			// ensure the skeleton is copied
			\OC::$server->getUserFolder();
			
84
85
86
87
88
			/** @var \OC\Files\View $view */
			$view = $viewCallBack();
			$rootInfo = $view->getFileInfo('');

			// Create ownCloud Dir
89
			if ($rootInfo->getType() === 'dir') {
90
				$root = new \OCA\DAV\Connector\Sabre\Directory($view, $rootInfo);
91
			} else {
92
				$root = new \OCA\DAV\Connector\Sabre\File($view, $rootInfo);
93
94
			}
			$objectTree->init($root, $view, $this->mountManager);
95

96
97
			$server->addPlugin(new \OCA\DAV\Connector\Sabre\FilesPlugin($objectTree, $view));
			$server->addPlugin(new \OCA\DAV\Connector\Sabre\QuotaPlugin($view));
98
99

			if($this->userSession->isLoggedIn()) {
100
				$server->addPlugin(new \OCA\DAV\Connector\Sabre\TagsPlugin($objectTree, $this->tagManager));
101
				// custom properties plugin must be the last one
102
103
				$server->addPlugin(
					new \Sabre\DAV\PropertyStorage\Plugin(
104
						new \OCA\DAV\Connector\Sabre\CustomPropertiesBackend(
105
106
107
108
109
110
111
							$objectTree,
							$this->databaseConnection,
							$this->userSession->getUser()
						)
					)
				);
			}
112
			$server->addPlugin(new \OCA\DAV\Connector\Sabre\CopyEtagHeaderPlugin());
113
114
115
116
		}, 30); // priority 30: after auth (10) and acl(20), before lock(50) and handling the request
		return $server;
	}
}