123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246 |
- <?php
- namespace OCA\Files_External;
- use OC\Files\Storage\Common;
- use OCA\Files_External\Config\IConfigHandler;
- use OCA\Files_External\Config\UserContext;
- use OCA\Files_External\Lib\Backend\Backend;
- use OCA\Files_External\Service\BackendService;
- use OCA\Files_External\Service\GlobalStoragesService;
- use OCA\Files_External\Service\UserGlobalStoragesService;
- use OCA\Files_External\Service\UserStoragesService;
- use OCP\AppFramework\QueryException;
- use OCP\Files\StorageNotAvailableException;
- use OCP\IL10N;
- use OCP\Util;
- use phpseclib\Crypt\AES;
- use Psr\Log\LoggerInterface;
- class MountConfig {
-
- public const MOUNT_TYPE_GLOBAL = 'global';
- public const MOUNT_TYPE_GROUP = 'group';
- public const MOUNT_TYPE_USER = 'user';
- public const MOUNT_TYPE_PERSONAL = 'personal';
-
- public static $skipTest = false;
- public function __construct(
- private UserGlobalStoragesService $userGlobalStorageService,
- private UserStoragesService $userStorageService,
- private GlobalStoragesService $globalStorageService,
- ) {
- }
-
- public static function substitutePlaceholdersInConfig($input, ?string $userId = null) {
-
- $backendService = \OC::$server->get(BackendService::class);
-
- $handlers = $backendService->getConfigHandlers();
- foreach ($handlers as $handler) {
- if ($handler instanceof UserContext && $userId !== null) {
- $handler->setUserId($userId);
- }
- $input = $handler->handle($input);
- }
- return $input;
- }
-
- public static function getBackendStatus($class, $options, $isPersonal, $testOnly = true) {
- if (self::$skipTest) {
- return StorageNotAvailableException::STATUS_SUCCESS;
- }
- foreach ($options as $key => &$option) {
- if ($key === 'password') {
-
- continue;
- }
- $option = self::substitutePlaceholdersInConfig($option);
- }
- if (class_exists($class)) {
- try {
-
- $storage = new $class($options);
- try {
- $result = $storage->test($isPersonal, $testOnly);
- $storage->setAvailability($result);
- if ($result) {
- return StorageNotAvailableException::STATUS_SUCCESS;
- }
- } catch (\Exception $e) {
- $storage->setAvailability(false);
- throw $e;
- }
- } catch (\Exception $exception) {
- \OC::$server->get(LoggerInterface::class)->error($exception->getMessage(), ['exception' => $exception, 'app' => 'files_external']);
- throw $exception;
- }
- }
- return StorageNotAvailableException::STATUS_ERROR;
- }
-
- public static function dependencyMessage(array $backends): string {
- $l = Util::getL10N('files_external');
- $message = '';
- $dependencyGroups = [];
- foreach ($backends as $backend) {
- foreach ($backend->checkDependencies() as $dependency) {
- $dependencyMessage = $dependency->getMessage();
- if ($dependencyMessage !== null) {
- $message .= '<p>' . $dependencyMessage . '</p>';
- } else {
- $dependencyGroups[$dependency->getDependency()][] = $backend;
- }
- }
- }
- foreach ($dependencyGroups as $module => $dependants) {
- $backends = implode(', ', array_map(function (Backend $backend): string {
- return '"' . $backend->getText() . '"';
- }, $dependants));
- $message .= '<p>' . MountConfig::getSingleDependencyMessage($l, $module, $backends) . '</p>';
- }
- return $message;
- }
-
- private static function getSingleDependencyMessage(IL10N $l, string $module, string $backend): string {
- switch (strtolower($module)) {
- case 'curl':
- return $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]);
- case 'ftp':
- return $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]);
- default:
- return $l->t('"%1$s" is not installed. Mounting of %2$s is not possible. Please ask your system administrator to install it.', [$module, $backend]);
- }
- }
-
- public static function encryptPasswords($options) {
- if (isset($options['password'])) {
- $options['password_encrypted'] = self::encryptPassword($options['password']);
-
-
- $options['password'] = '';
- }
- return $options;
- }
-
- public static function decryptPasswords($options) {
-
- if (isset($options['password_encrypted'])) {
- $options['password'] = self::decryptPassword($options['password_encrypted']);
- unset($options['password_encrypted']);
- }
- return $options;
- }
-
- private static function encryptPassword($password) {
- $cipher = self::getCipher();
- $iv = \OC::$server->getSecureRandom()->generate(16);
- $cipher->setIV($iv);
- return base64_encode($iv . $cipher->encrypt($password));
- }
-
- private static function decryptPassword($encryptedPassword) {
- $cipher = self::getCipher();
- $binaryPassword = base64_decode($encryptedPassword);
- $iv = substr($binaryPassword, 0, 16);
- $cipher->setIV($iv);
- $binaryPassword = substr($binaryPassword, 16);
- return $cipher->decrypt($binaryPassword);
- }
-
- private static function getCipher() {
- $cipher = new AES(AES::MODE_CBC);
- $cipher->setKey(\OC::$server->getConfig()->getSystemValue('passwordsalt', null));
- return $cipher;
- }
-
- public static function makeConfigHash($config) {
- $data = json_encode(
- [
- 'c' => $config['backend'],
- 'a' => $config['authMechanism'],
- 'm' => $config['mountpoint'],
- 'o' => $config['options'],
- 'p' => $config['priority'] ?? -1,
- 'mo' => $config['mountOptions'] ?? [],
- ]
- );
- return hash('md5', $data);
- }
- }
|