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