Completed
Push — master ( f6f7b5...3a3e6c )
by Lukas
40:35 queued 25:18
created

Server::__construct()   D

Complexity

Conditions 9
Paths 8

Size

Total Lines 197
Code Lines 129

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 9
eloc 129
nc 8
nop 2
dl 0
loc 197
rs 4.8196
c 0
b 0
f 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/**
3
 * @copyright Copyright (c) 2016, ownCloud, Inc.
4
 *
5
 * @author Arthur Schiwon <[email protected]>
6
 * @author Christoph Wurst <[email protected]>
7
 * @author Georg Ehrke <[email protected]>
8
 * @author Lukas Reschke <[email protected]>
9
 * @author Robin Appelman <[email protected]>
10
 * @author Roeland Jago Douma <[email protected]>
11
 * @author Thomas Müller <[email protected]>
12
 * @author Vincent Petry <[email protected]>
13
 *
14
 * @license AGPL-3.0
15
 *
16
 * This code is free software: you can redistribute it and/or modify
17
 * it under the terms of the GNU Affero General Public License, version 3,
18
 * as published by the Free Software Foundation.
19
 *
20
 * This program is distributed in the hope that it will be useful,
21
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23
 * GNU Affero General Public License for more details.
24
 *
25
 * You should have received a copy of the GNU Affero General Public License, version 3,
26
 * along with this program.  If not, see <http://www.gnu.org/licenses/>
27
 *
28
 */
29
namespace OCA\DAV;
30
31
use OC\AppFramework\Utility\TimeFactory;
32
use OCA\DAV\CalDAV\Schedule\IMipPlugin;
33
use OCA\DAV\CardDAV\ImageExportPlugin;
34
use OCA\DAV\CardDAV\PhotoCache;
35
use OCA\DAV\Comments\CommentsPlugin;
36
use OCA\DAV\Connector\Sabre\Auth;
37
use OCA\DAV\Connector\Sabre\BearerAuth;
38
use OCA\DAV\Connector\Sabre\BlockLegacyClientPlugin;
39
use OCA\DAV\Connector\Sabre\CachingTree;
40
use OCA\DAV\Connector\Sabre\CommentPropertiesPlugin;
41
use OCA\DAV\Connector\Sabre\CopyEtagHeaderPlugin;
42
use OCA\DAV\Connector\Sabre\DavAclPlugin;
43
use OCA\DAV\Connector\Sabre\DummyGetResponsePlugin;
44
use OCA\DAV\Connector\Sabre\FakeLockerPlugin;
45
use OCA\DAV\Connector\Sabre\FilesPlugin;
46
use OCA\DAV\Connector\Sabre\FilesReportPlugin;
47
use OCA\DAV\Connector\Sabre\SharesPlugin;
48
use OCA\DAV\DAV\PublicAuth;
49
use OCA\DAV\DAV\CustomPropertiesBackend;
50
use OCA\DAV\Connector\Sabre\QuotaPlugin;
51
use OCA\DAV\Files\BrowserErrorPagePlugin;
52
use OCA\DAV\SystemTag\SystemTagPlugin;
53
use OCP\IRequest;
54
use OCP\SabrePluginEvent;
55
use Sabre\CardDAV\VCFExportPlugin;
56
use Sabre\DAV\Auth\Plugin;
57
use OCA\DAV\Connector\Sabre\TagsPlugin;
58
use SearchDAV\DAV\SearchPlugin;
59
use OCA\DAV\AppInfo\PluginManager;
60
61
class Server {
62
63
	/** @var IRequest */
64
	private $request;
65
66
	/** @var  string */
67
	private $baseUri;
68
69
	/** @var Connector\Sabre\Server  */
70
	private $server;
71
72
	public function __construct(IRequest $request, $baseUri) {
73
		$this->request = $request;
74
		$this->baseUri = $baseUri;
75
		$logger = \OC::$server->getLogger();
76
		$mailer = \OC::$server->getMailer();
77
		$dispatcher = \OC::$server->getEventDispatcher();
78
		$timezone = new TimeFactory();
79
80
		$root = new RootCollection();
81
		$this->server = new \OCA\DAV\Connector\Sabre\Server(new CachingTree($root));
82
83
		// Add maintenance plugin
84
		$this->server->addPlugin(new \OCA\DAV\Connector\Sabre\MaintenancePlugin(\OC::$server->getConfig()));
85
86
		// Backends
87
		$authBackend = new Auth(
88
			\OC::$server->getSession(),
89
			\OC::$server->getUserSession(),
90
			\OC::$server->getRequest(),
91
			\OC::$server->getTwoFactorAuthManager(),
92
			\OC::$server->getBruteForceThrottler()
93
		);
94
95
		// Set URL explicitly due to reverse-proxy situations
96
		$this->server->httpRequest->setUrl($this->request->getRequestUri());
97
		$this->server->setBaseUri($this->baseUri);
98
99
		$this->server->addPlugin(new BlockLegacyClientPlugin(\OC::$server->getConfig()));
100
		$authPlugin = new Plugin();
101
		$authPlugin->addBackend(new PublicAuth());
102
		$this->server->addPlugin($authPlugin);
103
104
		// allow setup of additional auth backends
105
		$event = new SabrePluginEvent($this->server);
106
		$dispatcher->dispatch('OCA\DAV\Connector\Sabre::authInit', $event);
107
108
		$bearerAuthBackend = new BearerAuth(
109
			\OC::$server->getUserSession(),
110
			\OC::$server->getSession(),
111
			\OC::$server->getRequest()
112
		);
113
		$authPlugin->addBackend($bearerAuthBackend);
114
		// because we are throwing exceptions this plugin has to be the last one
115
		$authPlugin->addBackend($authBackend);
116
117
		// debugging
118
		if(\OC::$server->getConfig()->getSystemValue('debug', false)) {
119
			$this->server->addPlugin(new \Sabre\DAV\Browser\Plugin());
120
		} else {
121
			$this->server->addPlugin(new DummyGetResponsePlugin());
122
		}
123
124
		$this->server->addPlugin(new \OCA\DAV\Connector\Sabre\ExceptionLoggerPlugin('webdav', $logger));
125
		$this->server->addPlugin(new \OCA\DAV\Connector\Sabre\LockPlugin());
126
		$this->server->addPlugin(new \Sabre\DAV\Sync\Plugin());
127
128
		// acl
129
		$acl = new DavAclPlugin();
130
		$acl->principalCollectionSet = [
131
			'principals/users', 'principals/groups'
132
		];
133
		$acl->defaultUsernamePath = 'principals/users';
134
		$this->server->addPlugin($acl);
135
136
		// calendar plugins
137
		$this->server->addPlugin(new \OCA\DAV\CalDAV\Plugin());
138
		$this->server->addPlugin(new \Sabre\CalDAV\ICSExportPlugin());
139
		$this->server->addPlugin(new \OCA\DAV\CalDAV\Schedule\Plugin());
140
		$this->server->addPlugin(new IMipPlugin($mailer, $logger, $timezone));
141
		$this->server->addPlugin(new \Sabre\CalDAV\Subscriptions\Plugin());
142
		$this->server->addPlugin(new \Sabre\CalDAV\Notifications\Plugin());
143
		$this->server->addPlugin(new DAV\Sharing\Plugin($authBackend, \OC::$server->getRequest()));
144
		$this->server->addPlugin(new \OCA\DAV\CalDAV\Publishing\PublishPlugin(
145
			\OC::$server->getConfig(),
146
			\OC::$server->getURLGenerator()
147
		));
148
149
		// addressbook plugins
150
		$this->server->addPlugin(new \OCA\DAV\CardDAV\Plugin());
151
		$this->server->addPlugin(new VCFExportPlugin());
152
		$this->server->addPlugin(new ImageExportPlugin(new PhotoCache(\OC::$server->getAppDataDir('dav-photocache'))));
153
154
		// system tags plugins
155
		$this->server->addPlugin(new SystemTagPlugin(
156
			\OC::$server->getSystemTagManager(),
157
			\OC::$server->getGroupManager(),
158
			\OC::$server->getUserSession()
159
		));
160
161
		// comments plugin
162
		$this->server->addPlugin(new CommentsPlugin(
163
			\OC::$server->getCommentsManager(),
164
			\OC::$server->getUserSession()
165
		));
166
167
		$this->server->addPlugin(new CopyEtagHeaderPlugin());
168
169
		// allow setup of additional plugins
170
		$dispatcher->dispatch('OCA\DAV\Connector\Sabre::addPlugin', $event);
171
172
		// Some WebDAV clients do require Class 2 WebDAV support (locking), since
173
		// we do not provide locking we emulate it using a fake locking plugin.
174
		if($request->isUserAgent([
175
			'/WebDAVFS/',
176
			'/Microsoft Office OneNote 2013/',
177
			'/^Microsoft-WebDAV/',// Microsoft-WebDAV-MiniRedir/6.1.7601
178
		])) {
179
			$this->server->addPlugin(new FakeLockerPlugin());
180
		}
181
182
		if (BrowserErrorPagePlugin::isBrowserRequest($request)) {
183
			$this->server->addPlugin(new BrowserErrorPagePlugin());
184
		}
185
186
		// wait with registering these until auth is handled and the filesystem is setup
187
		$this->server->on('beforeMethod', function () use ($root) {
188
			// custom properties plugin must be the last one
189
			$userSession = \OC::$server->getUserSession();
190
			$user = $userSession->getUser();
191
			if ($user !== null) {
192
				$view = \OC\Files\Filesystem::getView();
193
				$this->server->addPlugin(
194
					new FilesPlugin(
195
						$this->server->tree,
196
						\OC::$server->getConfig(),
197
						$this->request,
198
						\OC::$server->getPreviewManager(),
199
						false,
200
						!\OC::$server->getConfig()->getSystemValue('debug', false)
201
					)
202
				);
203
204
				$this->server->addPlugin(
205
					new \Sabre\DAV\PropertyStorage\Plugin(
206
						new CustomPropertiesBackend(
207
							$this->server->tree,
208
							\OC::$server->getDatabaseConnection(),
209
							\OC::$server->getUserSession()->getUser()
210
						)
211
					)
212
				);
213
				if ($view !== null) {
214
					$this->server->addPlugin(
215
						new QuotaPlugin($view, false));
216
				}
217
				$this->server->addPlugin(
218
					new TagsPlugin(
219
						$this->server->tree, \OC::$server->getTagManager()
220
					)
221
				);
222
				// TODO: switch to LazyUserFolder
223
				$userFolder = \OC::$server->getUserFolder();
224
				$this->server->addPlugin(new SharesPlugin(
225
					$this->server->tree,
226
					$userSession,
227
					$userFolder,
228
					\OC::$server->getShareManager()
229
				));
230
				$this->server->addPlugin(new CommentPropertiesPlugin(
231
					\OC::$server->getCommentsManager(),
232
					$userSession
233
				));
234
				$this->server->addPlugin(new \OCA\DAV\CalDAV\Search\SearchPlugin());
235
				if ($view !== null) {
236
					$this->server->addPlugin(new FilesReportPlugin(
237
						$this->server->tree,
238
						$view,
239
						\OC::$server->getSystemTagManager(),
240
						\OC::$server->getSystemTagObjectMapper(),
241
						\OC::$server->getTagManager(),
242
						$userSession,
243
						\OC::$server->getGroupManager(),
244
						$userFolder
245
					));
246
					$this->server->addPlugin(new SearchPlugin(new \OCA\DAV\Files\FileSearchBackend(
247
						$this->server->tree,
248
						$user,
249
						\OC::$server->getRootFolder(),
250
						\OC::$server->getShareManager(),
251
						$view
252
					)));
253
				}
254
			}
255
256
			// register plugins from apps
257
			$pluginManager = new PluginManager(
258
				\OC::$server,
259
				\OC::$server->getAppManager()
260
			);
261
			foreach ($pluginManager->getAppPlugins() as $appPlugin) {
262
				$this->server->addPlugin($appPlugin);
263
			}
264
			foreach ($pluginManager->getAppCollections() as $appCollection) {
265
				$root->addChild($appCollection);
266
			}
267
		});
268
	}
269
270
	public function exec() {
271
		$this->server->exec();
272
	}
273
}
274