Completed
Push — master ( a2678d...3adda3 )
by Morris
21:22 queued 05:07
created

OC_Mount_Config::registerBackend()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 5
nc 1
nop 2
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
1
<?php
2
/**
3
 * @copyright Copyright (c) 2016, ownCloud, Inc.
4
 *
5
 * @author Andreas Fischer <[email protected]>
6
 * @author Bart Visscher <[email protected]>
7
 * @author Björn Schießle <[email protected]>
8
 * @author Frank Karlitschek <[email protected]>
9
 * @author Jesús Macias <[email protected]>
10
 * @author Joas Schilling <[email protected]>
11
 * @author Juan Pablo Villafáñez <[email protected]>
12
 * @author Lukas Reschke <[email protected]>
13
 * @author Michael Gapczynski <[email protected]>
14
 * @author Morris Jobke <[email protected]>
15
 * @author Philipp Kapfer <[email protected]>
16
 * @author Robin Appelman <[email protected]>
17
 * @author Robin McCorkell <[email protected]>
18
 * @author Roeland Jago Douma <[email protected]>
19
 * @author Thomas Müller <[email protected]>
20
 * @author Vincent Petry <[email protected]>
21
 *
22
 * @license AGPL-3.0
23
 *
24
 * This code is free software: you can redistribute it and/or modify
25
 * it under the terms of the GNU Affero General Public License, version 3,
26
 * as published by the Free Software Foundation.
27
 *
28
 * This program is distributed in the hope that it will be useful,
29
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
30
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
31
 * GNU Affero General Public License for more details.
32
 *
33
 * You should have received a copy of the GNU Affero General Public License, version 3,
34
 * along with this program.  If not, see <http://www.gnu.org/licenses/>
35
 *
36
 */
37
38
use phpseclib\Crypt\AES;
39
use \OCA\Files_External\AppInfo\Application;
40
use \OCA\Files_External\Lib\Backend\LegacyBackend;
41
use \OCA\Files_External\Lib\StorageConfig;
42
use \OCA\Files_External\Lib\Backend\Backend;
43
use \OCP\Files\StorageNotAvailableException;
44
45
/**
46
 * Class to configure mount.json globally and for users
47
 */
48
class OC_Mount_Config {
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
49
	// TODO: make this class non-static and give it a proper namespace
50
51
	const MOUNT_TYPE_GLOBAL = 'global';
52
	const MOUNT_TYPE_GROUP = 'group';
53
	const MOUNT_TYPE_USER = 'user';
54
	const MOUNT_TYPE_PERSONAL = 'personal';
55
56
	// whether to skip backend test (for unit tests, as this static class is not mockable)
57
	public static $skipTest = false;
58
59
	/** @var Application */
60
	public static $app;
61
62
	/**
63
	 * @param string $class
64
	 * @param array $definition
65
	 * @return bool
66
	 * @deprecated 8.2.0 use \OCA\Files_External\Service\BackendService::registerBackend()
67
	 */
68
	public static function registerBackend($class, $definition) {
69
		$backendService = self::$app->getContainer()->query('OCA\Files_External\Service\BackendService');
70
		$auth = self::$app->getContainer()->query('OCA\Files_External\Lib\Auth\Builtin');
71
72
		$backendService->registerBackend(new LegacyBackend($class, $definition, $auth));
73
74
		return true;
75
	}
76
77
	/**
78
	 * Returns the mount points for the given user.
79
	 * The mount point is relative to the data directory.
80
	 *
81
	 * @param string $uid user
82
	 * @return array of mount point string as key, mountpoint config as value
83
	 *
84
	 * @deprecated 8.2.0 use UserGlobalStoragesService::getStorages() and UserStoragesService::getStorages()
85
	 */
86
	public static function getAbsoluteMountPoints($uid) {
87
		$mountPoints = array();
88
89
		$userGlobalStoragesService = self::$app->getContainer()->query('OCA\Files_External\Service\UserGlobalStoragesService');
90
		$userStoragesService = self::$app->getContainer()->query('OCA\Files_External\Service\UserStoragesService');
91
		$user = self::$app->getContainer()->query('OCP\IUserManager')->get($uid);
92
93
		$userGlobalStoragesService->setUser($user);
94
		$userStoragesService->setUser($user);
95
96 View Code Duplication
		foreach ($userGlobalStoragesService->getStorages() as $storage) {
97
			/** @var \OCA\Files_External\Lib\StorageConfig $storage */
98
			$mountPoint = '/'.$uid.'/files'.$storage->getMountPoint();
99
			$mountEntry = self::prepareMountPointEntry($storage, false);
100
			foreach ($mountEntry['options'] as &$option) {
101
				$option = self::setUserVars($uid, $option);
102
			}
103
			$mountPoints[$mountPoint] = $mountEntry;
104
		}
105
106 View Code Duplication
		foreach ($userStoragesService->getStorages() as $storage) {
107
			$mountPoint = '/'.$uid.'/files'.$storage->getMountPoint();
108
			$mountEntry = self::prepareMountPointEntry($storage, true);
109
			foreach ($mountEntry['options'] as &$option) {
110
				$option = self::setUserVars($uid, $option);
111
			}
112
			$mountPoints[$mountPoint] = $mountEntry;
113
		}
114
115
		$userGlobalStoragesService->resetUser();
116
		$userStoragesService->resetUser();
117
118
		return $mountPoints;
119
	}
120
121
	/**
122
	 * Get the system mount points
123
	 *
124
	 * @return array
125
	 *
126
	 * @deprecated 8.2.0 use GlobalStoragesService::getStorages()
127
	 */
128 View Code Duplication
	public static function getSystemMountPoints() {
129
		$mountPoints = [];
130
		$service = self::$app->getContainer()->query('OCA\Files_External\Service\GlobalStoragesService');
131
132
		foreach ($service->getStorages() as $storage) {
133
			$mountPoints[] = self::prepareMountPointEntry($storage, false);
134
		}
135
136
		return $mountPoints;
137
	}
138
139
	/**
140
	 * Get the personal mount points of the current user
141
	 *
142
	 * @return array
143
	 *
144
	 * @deprecated 8.2.0 use UserStoragesService::getStorages()
145
	 */
146 View Code Duplication
	public static function getPersonalMountPoints() {
147
		$mountPoints = [];
148
		$service = self::$app->getContainer()->query('OCA\Files_External\Service\UserStoragesService');
149
150
		foreach ($service->getStorages() as $storage) {
151
			$mountPoints[] = self::prepareMountPointEntry($storage, true);
152
		}
153
154
		return $mountPoints;
155
	}
156
157
	/**
158
	 * Convert a StorageConfig to the legacy mountPoints array format
159
	 * There's a lot of extra information in here, to satisfy all of the legacy functions
160
	 *
161
	 * @param StorageConfig $storage
162
	 * @param bool $isPersonal
163
	 * @return array
164
	 */
165
	private static function prepareMountPointEntry(StorageConfig $storage, $isPersonal) {
166
		$mountEntry = [];
167
168
		$mountEntry['mountpoint'] = substr($storage->getMountPoint(), 1); // remove leading slash
169
		$mountEntry['class'] = $storage->getBackend()->getIdentifier();
170
		$mountEntry['backend'] = $storage->getBackend()->getText();
171
		$mountEntry['authMechanism'] = $storage->getAuthMechanism()->getIdentifier();
172
		$mountEntry['personal'] = $isPersonal;
173
		$mountEntry['options'] = self::decryptPasswords($storage->getBackendOptions());
174
		$mountEntry['mountOptions'] = $storage->getMountOptions();
175
		$mountEntry['priority'] = $storage->getPriority();
176
		$mountEntry['applicable'] = [
177
			'groups' => $storage->getApplicableGroups(),
178
			'users' => $storage->getApplicableUsers(),
179
		];
180
		// if mountpoint is applicable to all users the old API expects ['all']
181
		if (empty($mountEntry['applicable']['groups']) && empty($mountEntry['applicable']['users'])) {
182
			$mountEntry['applicable']['users'] = ['all'];
183
		}
184
185
		$mountEntry['id'] = $storage->getId();
186
187
		return $mountEntry;
188
	}
189
190
	/**
191
	 * fill in the correct values for $user
192
	 *
193
	 * @param string $user user value
194
	 * @param string|array $input
195
	 * @return string
196
	 */
197
	public static function setUserVars($user, $input) {
198
		if (is_array($input)) {
199
			foreach ($input as &$value) {
200
				if (is_string($value)) {
201
					$value = str_replace('$user', $user, $value);
202
				}
203
			}
204
		} else {
205
			if (is_string($input)) {
206
				$input = str_replace('$user', $user, $input);
207
			}
208
		}
209
		return $input;
210
	}
211
212
	/**
213
	 * Test connecting using the given backend configuration
214
	 *
215
	 * @param string $class backend class name
216
	 * @param array $options backend configuration options
217
	 * @param boolean $isPersonal
218
	 * @return int see self::STATUS_*
219
	 * @throws Exception
220
	 */
221
	public static function getBackendStatus($class, $options, $isPersonal, $testOnly = true) {
222
		if (self::$skipTest) {
223
			return StorageNotAvailableException::STATUS_SUCCESS;
224
		}
225
		foreach ($options as &$option) {
226
			$option = self::setUserVars(OCP\User::getUser(), $option);
227
		}
228
		if (class_exists($class)) {
229
			try {
230
				/** @var \OC\Files\Storage\Common $storage */
231
				$storage = new $class($options);
232
233
				try {
234
					$result = $storage->test($isPersonal, $testOnly);
235
					$storage->setAvailability($result);
236
					if ($result) {
237
						return StorageNotAvailableException::STATUS_SUCCESS;
238
					}
239
				} catch (\Exception $e) {
240
					$storage->setAvailability(false);
241
					throw $e;
242
				}
243
			} catch (Exception $exception) {
244
				\OCP\Util::logException('files_external', $exception);
245
				throw $exception;
246
			}
247
		}
248
		return StorageNotAvailableException::STATUS_ERROR;
249
	}
250
251
	/**
252
	 * Read the mount points in the config file into an array
253
	 *
254
	 * @param string|null $user If not null, personal for $user, otherwise system
255
	 * @return array
256
	 */
257
	public static function readData($user = null) {
258
		if (isset($user)) {
259
			$jsonFile = \OC::$server->getUserManager()->get($user)->getHome() . '/mount.json';
260
		} else {
261
			$config = \OC::$server->getConfig();
262
			$datadir = $config->getSystemValue('datadirectory', \OC::$SERVERROOT . '/data/');
263
			$jsonFile = $config->getSystemValue('mount_file', $datadir . '/mount.json');
264
		}
265
		if (is_file($jsonFile)) {
266
			$mountPoints = json_decode(file_get_contents($jsonFile), true);
267
			if (is_array($mountPoints)) {
268
				return $mountPoints;
269
			}
270
		}
271
		return array();
272
	}
273
274
	/**
275
	 * Get backend dependency message
276
	 * TODO: move into AppFramework along with templates
277
	 *
278
	 * @param Backend[] $backends
279
	 * @return string
280
	 */
281
	public static function dependencyMessage($backends) {
282
		$l = \OC::$server->getL10N('files_external');
283
		$message = '';
284
		$dependencyGroups = [];
285
286
		foreach ($backends as $backend) {
287
			foreach ($backend->checkDependencies() as $dependency) {
288
				if ($message = $dependency->getMessage()) {
289
					$message .= '<p>' . $message . '</p>';
290
				} else {
291
					$dependencyGroups[$dependency->getDependency()][] = $backend;
292
				}
293
			}
294
		}
295
296
		foreach ($dependencyGroups as $module => $dependants) {
297
			$backends = implode(', ', array_map(function($backend) {
298
				return '"' . $backend->getText() . '"';
299
			}, $dependants));
300
			$message .= '<p>' . OC_Mount_Config::getSingleDependencyMessage($l, $module, $backends) . '</p>';
301
		}
302
303
		return $message;
304
	}
305
306
	/**
307
	 * Returns a dependency missing message
308
	 *
309
	 * @param \OCP\IL10N $l
310
	 * @param string $module
311
	 * @param string $backend
312
	 * @return string
313
	 */
314
	private static function getSingleDependencyMessage(\OCP\IL10N $l, $module, $backend) {
315
		switch (strtolower($module)) {
316
			case 'curl':
317
				return (string)$l->t('The cURL support in PHP is not enabled or installed. Mounting of %s is not possible. Please ask your system administrator to install it.', [$backend]);
318
			case 'ftp':
319
				return (string)$l->t('The FTP support in PHP is not enabled or installed. Mounting of %s is not possible. Please ask your system administrator to install it.', [$backend]);
320
			default:
321
				return (string)$l->t('"%s" is not installed. Mounting of %s is not possible. Please ask your system administrator to install it.', [$module, $backend]);
322
		}
323
	}
324
325
	/**
326
	 * Encrypt passwords in the given config options
327
	 *
328
	 * @param array $options mount options
329
	 * @return array updated options
330
	 */
331 View Code Duplication
	public static function encryptPasswords($options) {
332
		if (isset($options['password'])) {
333
			$options['password_encrypted'] = self::encryptPassword($options['password']);
334
			// do not unset the password, we want to keep the keys order
335
			// on load... because that's how the UI currently works
336
			$options['password'] = '';
337
		}
338
		return $options;
339
	}
340
341
	/**
342
	 * Decrypt passwords in the given config options
343
	 *
344
	 * @param array $options mount options
345
	 * @return array updated options
346
	 */
347 View Code Duplication
	public static function decryptPasswords($options) {
348
		// note: legacy options might still have the unencrypted password in the "password" field
349
		if (isset($options['password_encrypted'])) {
350
			$options['password'] = self::decryptPassword($options['password_encrypted']);
351
			unset($options['password_encrypted']);
352
		}
353
		return $options;
354
	}
355
356
	/**
357
	 * Encrypt a single password
358
	 *
359
	 * @param string $password plain text password
360
	 * @return string encrypted password
361
	 */
362
	private static function encryptPassword($password) {
363
		$cipher = self::getCipher();
364
		$iv = \OC::$server->getSecureRandom()->generate(16);
365
		$cipher->setIV($iv);
366
		return base64_encode($iv . $cipher->encrypt($password));
367
	}
368
369
	/**
370
	 * Decrypts a single password
371
	 *
372
	 * @param string $encryptedPassword encrypted password
373
	 * @return string plain text password
374
	 */
375
	private static function decryptPassword($encryptedPassword) {
376
		$cipher = self::getCipher();
377
		$binaryPassword = base64_decode($encryptedPassword);
378
		$iv = substr($binaryPassword, 0, 16);
379
		$cipher->setIV($iv);
380
		$binaryPassword = substr($binaryPassword, 16);
381
		return $cipher->decrypt($binaryPassword);
382
	}
383
384
	/**
385
	 * Returns the encryption cipher
386
	 *
387
	 * @return AES
388
	 */
389
	private static function getCipher() {
390
		$cipher = new AES(AES::MODE_CBC);
391
		$cipher->setKey(\OC::$server->getConfig()->getSystemValue('passwordsalt', null));
392
		return $cipher;
393
	}
394
395
	/**
396
	 * Computes a hash based on the given configuration.
397
	 * This is mostly used to find out whether configurations
398
	 * are the same.
399
	 *
400
	 * @param array $config
401
	 * @return string
402
	 */
403
	public static function makeConfigHash($config) {
404
		$data = json_encode(
405
			array(
406
				'c' => $config['backend'],
407
				'a' => $config['authMechanism'],
408
				'm' => $config['mountpoint'],
409
				'o' => $config['options'],
410
				'p' => isset($config['priority']) ? $config['priority'] : -1,
411
				'mo' => isset($config['mountOptions']) ? $config['mountOptions'] : [],
412
			)
413
		);
414
		return hash('md5', $data);
415
	}
416
}
417