Completed
Push — master ( 188b96...0947dd )
by Morris
43s queued 10s
created

Server::__construct()   C

Complexity

Conditions 12
Paths 48

Size

Total Lines 207
Code Lines 135

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 12
eloc 135
nc 48
nop 2
dl 0
loc 207
rs 5.034
c 0
b 0
f 0

How to fix   Long Method    Complexity   

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