| @@ -29,40 +29,40 @@ | ||
| 29 | 29 | use OCA\User_LDAP\User_Proxy; | 
| 30 | 30 | |
| 31 | 31 |  class ExtStorageConfigHandler extends UserContext implements IConfigHandler { | 
| 32 | - use SimpleSubstitutionTrait; | |
| 32 | + use SimpleSubstitutionTrait; | |
| 33 | 33 | |
| 34 | - /** | |
| 35 | - * @param mixed $optionValue | |
| 36 | - * @return mixed the same type as $optionValue | |
| 37 | - * @since 16.0.0 | |
| 38 | - * @throws \Exception | |
| 39 | - */ | |
| 40 | -	public function handle($optionValue) { | |
| 41 | - $this->placeholder = 'home'; | |
| 42 | - $user = $this->getUser(); | |
| 34 | + /** | |
| 35 | + * @param mixed $optionValue | |
| 36 | + * @return mixed the same type as $optionValue | |
| 37 | + * @since 16.0.0 | |
| 38 | + * @throws \Exception | |
| 39 | + */ | |
| 40 | +    public function handle($optionValue) { | |
| 41 | + $this->placeholder = 'home'; | |
| 42 | + $user = $this->getUser(); | |
| 43 | 43 | |
| 44 | -		if($user === null) { | |
| 45 | - return $optionValue; | |
| 46 | - } | |
| 44 | +        if($user === null) { | |
| 45 | + return $optionValue; | |
| 46 | + } | |
| 47 | 47 | |
| 48 | - $backend = $user->getBackend(); | |
| 49 | -		if(!$backend instanceof User_Proxy) { | |
| 50 | - return $optionValue; | |
| 51 | - } | |
| 48 | + $backend = $user->getBackend(); | |
| 49 | +        if(!$backend instanceof User_Proxy) { | |
| 50 | + return $optionValue; | |
| 51 | + } | |
| 52 | 52 | |
| 53 | - $access = $backend->getLDAPAccess($user->getUID()); | |
| 54 | -		if(!$access) { | |
| 55 | - return $optionValue; | |
| 56 | - } | |
| 53 | + $access = $backend->getLDAPAccess($user->getUID()); | |
| 54 | +        if(!$access) { | |
| 55 | + return $optionValue; | |
| 56 | + } | |
| 57 | 57 | |
| 58 | - $attribute = $access->connection->ldapExtStorageHomeAttribute; | |
| 59 | -		if(empty($attribute)) { | |
| 60 | - return $optionValue; | |
| 61 | - } | |
| 58 | + $attribute = $access->connection->ldapExtStorageHomeAttribute; | |
| 59 | +        if(empty($attribute)) { | |
| 60 | + return $optionValue; | |
| 61 | + } | |
| 62 | 62 | |
| 63 | - $ldapUser = $access->userManager->get($user->getUID()); | |
| 64 | - $extHome = $ldapUser->getExtStorageHome(); | |
| 63 | + $ldapUser = $access->userManager->get($user->getUID()); | |
| 64 | + $extHome = $ldapUser->getExtStorageHome(); | |
| 65 | 65 | |
| 66 | - return $this->processInput($optionValue, $extHome); | |
| 67 | - } | |
| 66 | + return $this->processInput($optionValue, $extHome); | |
| 67 | + } | |
| 68 | 68 | } | 
| @@ -41,22 +41,22 @@ | ||
| 41 | 41 | $this->placeholder = 'home'; | 
| 42 | 42 | $user = $this->getUser(); | 
| 43 | 43 | |
| 44 | -		if($user === null) { | |
| 44 | +		if ($user === null) { | |
| 45 | 45 | return $optionValue; | 
| 46 | 46 | } | 
| 47 | 47 | |
| 48 | 48 | $backend = $user->getBackend(); | 
| 49 | -		if(!$backend instanceof User_Proxy) { | |
| 49 | +		if (!$backend instanceof User_Proxy) { | |
| 50 | 50 | return $optionValue; | 
| 51 | 51 | } | 
| 52 | 52 | |
| 53 | 53 | $access = $backend->getLDAPAccess($user->getUID()); | 
| 54 | -		if(!$access) { | |
| 54 | +		if (!$access) { | |
| 55 | 55 | return $optionValue; | 
| 56 | 56 | } | 
| 57 | 57 | |
| 58 | 58 | $attribute = $access->connection->ldapExtStorageHomeAttribute; | 
| 59 | -		if(empty($attribute)) { | |
| 59 | +		if (empty($attribute)) { | |
| 60 | 60 | return $optionValue; | 
| 61 | 61 | } | 
| 62 | 62 | |
| @@ -30,57 +30,57 @@ | ||
| 30 | 30 | * @since 16.0.0 | 
| 31 | 31 | */ | 
| 32 | 32 |  trait SimpleSubstitutionTrait { | 
| 33 | - /** | |
| 34 | - * @var string the placeholder without $ prefix | |
| 35 | - * @since 16.0.0 | |
| 36 | - */ | |
| 37 | - protected $placeholder; | |
| 33 | + /** | |
| 34 | + * @var string the placeholder without $ prefix | |
| 35 | + * @since 16.0.0 | |
| 36 | + */ | |
| 37 | + protected $placeholder; | |
| 38 | 38 | |
| 39 | - /** @var string */ | |
| 40 | - protected $sanitizedPlaceholder; | |
| 39 | + /** @var string */ | |
| 40 | + protected $sanitizedPlaceholder; | |
| 41 | 41 | |
| 42 | - /** | |
| 43 | - * @param mixed $optionValue | |
| 44 | - * @param string $replacement | |
| 45 | - * @return mixed | |
| 46 | - * @since 16.0.0 | |
| 47 | - */ | |
| 48 | -	private function processInput($optionValue, string $replacement) { | |
| 49 | - $this->checkPlaceholder(); | |
| 50 | -		if (is_array($optionValue)) { | |
| 51 | -			foreach ($optionValue as &$value) { | |
| 52 | - $value = $this->substituteIfString($value, $replacement); | |
| 53 | - } | |
| 54 | -		} else { | |
| 55 | - $optionValue = $this->substituteIfString($optionValue, $replacement); | |
| 56 | - } | |
| 57 | - return $optionValue; | |
| 58 | - } | |
| 42 | + /** | |
| 43 | + * @param mixed $optionValue | |
| 44 | + * @param string $replacement | |
| 45 | + * @return mixed | |
| 46 | + * @since 16.0.0 | |
| 47 | + */ | |
| 48 | +    private function processInput($optionValue, string $replacement) { | |
| 49 | + $this->checkPlaceholder(); | |
| 50 | +        if (is_array($optionValue)) { | |
| 51 | +            foreach ($optionValue as &$value) { | |
| 52 | + $value = $this->substituteIfString($value, $replacement); | |
| 53 | + } | |
| 54 | +        } else { | |
| 55 | + $optionValue = $this->substituteIfString($optionValue, $replacement); | |
| 56 | + } | |
| 57 | + return $optionValue; | |
| 58 | + } | |
| 59 | 59 | |
| 60 | - /** | |
| 61 | - * @throws \RuntimeException | |
| 62 | - */ | |
| 63 | -	protected function checkPlaceholder(): void { | |
| 64 | - $this->sanitizedPlaceholder = trim(strtolower($this->placeholder)); | |
| 65 | -		if(!(bool)\preg_match('/^[a-z0-9]*$/', $this->sanitizedPlaceholder)) { | |
| 66 | - throw new \RuntimeException(sprintf( | |
| 67 | - 'Invalid placeholder %s, only [a-z0-9] are allowed', $this->sanitizedPlaceholder | |
| 68 | - )); | |
| 69 | - } | |
| 70 | -		if($this->sanitizedPlaceholder === '') { | |
| 71 | -			throw new \RuntimeException('Invalid empty placeholder'); | |
| 72 | - } | |
| 73 | - } | |
| 60 | + /** | |
| 61 | + * @throws \RuntimeException | |
| 62 | + */ | |
| 63 | +    protected function checkPlaceholder(): void { | |
| 64 | + $this->sanitizedPlaceholder = trim(strtolower($this->placeholder)); | |
| 65 | +        if(!(bool)\preg_match('/^[a-z0-9]*$/', $this->sanitizedPlaceholder)) { | |
| 66 | + throw new \RuntimeException(sprintf( | |
| 67 | + 'Invalid placeholder %s, only [a-z0-9] are allowed', $this->sanitizedPlaceholder | |
| 68 | + )); | |
| 69 | + } | |
| 70 | +        if($this->sanitizedPlaceholder === '') { | |
| 71 | +            throw new \RuntimeException('Invalid empty placeholder'); | |
| 72 | + } | |
| 73 | + } | |
| 74 | 74 | |
| 75 | - /** | |
| 76 | - * @param mixed $value | |
| 77 | - * @param string $replacement | |
| 78 | - * @return mixed | |
| 79 | - */ | |
| 80 | -	protected function substituteIfString($value, string $replacement) { | |
| 81 | -		if(is_string($value)) { | |
| 82 | -			return str_ireplace('$' . $this->sanitizedPlaceholder, $replacement, $value); | |
| 83 | - } | |
| 84 | - return $value; | |
| 85 | - } | |
| 75 | + /** | |
| 76 | + * @param mixed $value | |
| 77 | + * @param string $replacement | |
| 78 | + * @return mixed | |
| 79 | + */ | |
| 80 | +    protected function substituteIfString($value, string $replacement) { | |
| 81 | +        if(is_string($value)) { | |
| 82 | +            return str_ireplace('$' . $this->sanitizedPlaceholder, $replacement, $value); | |
| 83 | + } | |
| 84 | + return $value; | |
| 85 | + } | |
| 86 | 86 | } | 
| @@ -24,19 +24,19 @@ | ||
| 24 | 24 | namespace OCA\Files_External\Config; | 
| 25 | 25 | |
| 26 | 26 |  class UserPlaceholderHandler extends UserContext implements IConfigHandler { | 
| 27 | - use SimpleSubstitutionTrait; | |
| 27 | + use SimpleSubstitutionTrait; | |
| 28 | 28 | |
| 29 | - /** | |
| 30 | - * @param mixed $optionValue | |
| 31 | - * @return mixed the same type as $optionValue | |
| 32 | - * @since 16.0.0 | |
| 33 | - */ | |
| 34 | -	public function handle($optionValue) { | |
| 35 | - $this->placeholder = 'user'; | |
| 36 | - $uid = $this->getUserId(); | |
| 37 | -		if($uid === null) { | |
| 38 | - return $optionValue; | |
| 39 | - } | |
| 40 | - return $this->processInput($optionValue, $uid); | |
| 41 | - } | |
| 29 | + /** | |
| 30 | + * @param mixed $optionValue | |
| 31 | + * @return mixed the same type as $optionValue | |
| 32 | + * @since 16.0.0 | |
| 33 | + */ | |
| 34 | +    public function handle($optionValue) { | |
| 35 | + $this->placeholder = 'user'; | |
| 36 | + $uid = $this->getUserId(); | |
| 37 | +        if($uid === null) { | |
| 38 | + return $optionValue; | |
| 39 | + } | |
| 40 | + return $this->processInput($optionValue, $uid); | |
| 41 | + } | |
| 42 | 42 | } | 
| @@ -34,7 +34,7 @@ | ||
| 34 | 34 |  	public function handle($optionValue) { | 
| 35 | 35 | $this->placeholder = 'user'; | 
| 36 | 36 | $uid = $this->getUserId(); | 
| 37 | -		if($uid === null) { | |
| 37 | +		if ($uid === null) { | |
| 38 | 38 | return $optionValue; | 
| 39 | 39 | } | 
| 40 | 40 | return $this->processInput($optionValue, $uid); | 
| @@ -44,142 +44,142 @@ | ||
| 44 | 44 | */ | 
| 45 | 45 |  class ConfigAdapter implements IMountProvider { | 
| 46 | 46 | |
| 47 | - /** @var UserStoragesService */ | |
| 48 | - private $userStoragesService; | |
| 49 | - | |
| 50 | - /** @var UserGlobalStoragesService */ | |
| 51 | - private $userGlobalStoragesService; | |
| 52 | - /** @var StorageMigrator */ | |
| 53 | - private $migrator; | |
| 54 | - | |
| 55 | - /** | |
| 56 | - * @param UserStoragesService $userStoragesService | |
| 57 | - * @param UserGlobalStoragesService $userGlobalStoragesService | |
| 58 | - * @param StorageMigrator $migrator | |
| 59 | - */ | |
| 60 | - public function __construct( | |
| 61 | - UserStoragesService $userStoragesService, | |
| 62 | - UserGlobalStoragesService $userGlobalStoragesService, | |
| 63 | - StorageMigrator $migrator | |
| 64 | -	) { | |
| 65 | - $this->userStoragesService = $userStoragesService; | |
| 66 | - $this->userGlobalStoragesService = $userGlobalStoragesService; | |
| 67 | - $this->migrator = $migrator; | |
| 68 | - } | |
| 69 | - | |
| 70 | - /** | |
| 71 | - * Process storage ready for mounting | |
| 72 | - * | |
| 73 | - * @param StorageConfig $storage | |
| 74 | - * @param IUser $user | |
| 75 | - * @throws \OCP\AppFramework\QueryException | |
| 76 | - */ | |
| 77 | -	private function prepareStorageConfig(StorageConfig &$storage, IUser $user) { | |
| 78 | -		foreach ($storage->getBackendOptions() as $option => $value) { | |
| 79 | - $storage->setBackendOption($option, \OC_Mount_Config::substitutePlaceholdersInConfig($value, $user->getUID())); | |
| 80 | - } | |
| 81 | - | |
| 82 | -		$objectStore = $storage->getBackendOption('objectstore'); | |
| 83 | -		if ($objectStore) { | |
| 84 | - $objectClass = $objectStore['class']; | |
| 85 | -			if (!is_subclass_of($objectClass, '\OCP\Files\ObjectStore\IObjectStore')) { | |
| 86 | -				throw new \InvalidArgumentException('Invalid object store'); | |
| 87 | - } | |
| 88 | -			$storage->setBackendOption('objectstore', new $objectClass($objectStore)); | |
| 89 | - } | |
| 90 | - | |
| 91 | - $storage->getAuthMechanism()->manipulateStorageConfig($storage, $user); | |
| 92 | - $storage->getBackend()->manipulateStorageConfig($storage, $user); | |
| 93 | - } | |
| 94 | - | |
| 95 | - /** | |
| 96 | - * Construct the storage implementation | |
| 97 | - * | |
| 98 | - * @param StorageConfig $storageConfig | |
| 99 | - * @return Storage | |
| 100 | - */ | |
| 101 | -	private function constructStorage(StorageConfig $storageConfig) { | |
| 102 | - $class = $storageConfig->getBackend()->getStorageClass(); | |
| 103 | - $storage = new $class($storageConfig->getBackendOptions()); | |
| 104 | - | |
| 105 | - // auth mechanism should fire first | |
| 106 | - $storage = $storageConfig->getBackend()->wrapStorage($storage); | |
| 107 | - $storage = $storageConfig->getAuthMechanism()->wrapStorage($storage); | |
| 108 | - | |
| 109 | - return $storage; | |
| 110 | - } | |
| 111 | - | |
| 112 | - /** | |
| 113 | - * Get all mountpoints applicable for the user | |
| 114 | - * | |
| 115 | - * @param \OCP\IUser $user | |
| 116 | - * @param \OCP\Files\Storage\IStorageFactory $loader | |
| 117 | - * @return \OCP\Files\Mount\IMountPoint[] | |
| 118 | - */ | |
| 119 | -	public function getMountsForUser(IUser $user, IStorageFactory $loader) { | |
| 120 | - $this->migrator->migrateUser($user); | |
| 121 | - | |
| 122 | - $this->userStoragesService->setUser($user); | |
| 123 | - $this->userGlobalStoragesService->setUser($user); | |
| 124 | - | |
| 125 | - $storageConfigs = $this->userGlobalStoragesService->getAllStoragesForUser(); | |
| 126 | - | |
| 127 | -		$storages = array_map(function(StorageConfig $storageConfig) use ($user) { | |
| 128 | -			try { | |
| 129 | - $this->prepareStorageConfig($storageConfig, $user); | |
| 130 | - return $this->constructStorage($storageConfig); | |
| 131 | -			} catch (\Exception $e) { | |
| 132 | - // propagate exception into filesystem | |
| 133 | - return new FailedStorage(['exception' => $e]); | |
| 134 | - } | |
| 135 | - }, $storageConfigs); | |
| 136 | - | |
| 137 | - | |
| 138 | -		\OC\Files\Cache\Storage::getGlobalCache()->loadForStorageIds(array_map(function(Storage\IStorage $storage) { | |
| 139 | - return $storage->getId(); | |
| 140 | - }, $storages)); | |
| 141 | - | |
| 142 | -		$availableStorages = array_map(function (Storage\IStorage $storage, StorageConfig $storageConfig) { | |
| 143 | -			try { | |
| 144 | - $availability = $storage->getAvailability(); | |
| 145 | -				if (!$availability['available'] && !Availability::shouldRecheck($availability)) { | |
| 146 | - $storage = new FailedStorage([ | |
| 147 | -						'exception' => new StorageNotAvailableException('Storage with mount id ' . $storageConfig->getId() . ' is not available') | |
| 148 | - ]); | |
| 149 | - } | |
| 150 | -			} catch (\Exception $e) { | |
| 151 | - // propagate exception into filesystem | |
| 152 | - $storage = new FailedStorage(['exception' => $e]); | |
| 153 | - } | |
| 154 | - return $storage; | |
| 155 | - }, $storages, $storageConfigs); | |
| 156 | - | |
| 157 | -		$mounts = array_map(function(StorageConfig $storageConfig, Storage\IStorage $storage) use ($user, $loader) { | |
| 158 | -			if ($storageConfig->getType() === StorageConfig::MOUNT_TYPE_PERSONAl) { | |
| 159 | - return new PersonalMount( | |
| 160 | - $this->userStoragesService, | |
| 161 | - $storageConfig->getId(), | |
| 162 | - $storage, | |
| 163 | - '/' . $user->getUID() . '/files' . $storageConfig->getMountPoint(), | |
| 164 | - null, | |
| 165 | - $loader, | |
| 166 | - $storageConfig->getMountOptions() | |
| 167 | - ); | |
| 168 | -			} else { | |
| 169 | - return new ExternalMountPoint( | |
| 170 | - $storage, | |
| 171 | - '/' . $user->getUID() . '/files' . $storageConfig->getMountPoint(), | |
| 172 | - null, | |
| 173 | - $loader, | |
| 174 | - $storageConfig->getMountOptions(), | |
| 175 | - $storageConfig->getId() | |
| 176 | - ); | |
| 177 | - } | |
| 178 | - }, $storageConfigs, $availableStorages); | |
| 179 | - | |
| 180 | - $this->userStoragesService->resetUser(); | |
| 181 | - $this->userGlobalStoragesService->resetUser(); | |
| 182 | - | |
| 183 | - return $mounts; | |
| 184 | - } | |
| 47 | + /** @var UserStoragesService */ | |
| 48 | + private $userStoragesService; | |
| 49 | + | |
| 50 | + /** @var UserGlobalStoragesService */ | |
| 51 | + private $userGlobalStoragesService; | |
| 52 | + /** @var StorageMigrator */ | |
| 53 | + private $migrator; | |
| 54 | + | |
| 55 | + /** | |
| 56 | + * @param UserStoragesService $userStoragesService | |
| 57 | + * @param UserGlobalStoragesService $userGlobalStoragesService | |
| 58 | + * @param StorageMigrator $migrator | |
| 59 | + */ | |
| 60 | + public function __construct( | |
| 61 | + UserStoragesService $userStoragesService, | |
| 62 | + UserGlobalStoragesService $userGlobalStoragesService, | |
| 63 | + StorageMigrator $migrator | |
| 64 | +    ) { | |
| 65 | + $this->userStoragesService = $userStoragesService; | |
| 66 | + $this->userGlobalStoragesService = $userGlobalStoragesService; | |
| 67 | + $this->migrator = $migrator; | |
| 68 | + } | |
| 69 | + | |
| 70 | + /** | |
| 71 | + * Process storage ready for mounting | |
| 72 | + * | |
| 73 | + * @param StorageConfig $storage | |
| 74 | + * @param IUser $user | |
| 75 | + * @throws \OCP\AppFramework\QueryException | |
| 76 | + */ | |
| 77 | +    private function prepareStorageConfig(StorageConfig &$storage, IUser $user) { | |
| 78 | +        foreach ($storage->getBackendOptions() as $option => $value) { | |
| 79 | + $storage->setBackendOption($option, \OC_Mount_Config::substitutePlaceholdersInConfig($value, $user->getUID())); | |
| 80 | + } | |
| 81 | + | |
| 82 | +        $objectStore = $storage->getBackendOption('objectstore'); | |
| 83 | +        if ($objectStore) { | |
| 84 | + $objectClass = $objectStore['class']; | |
| 85 | +            if (!is_subclass_of($objectClass, '\OCP\Files\ObjectStore\IObjectStore')) { | |
| 86 | +                throw new \InvalidArgumentException('Invalid object store'); | |
| 87 | + } | |
| 88 | +            $storage->setBackendOption('objectstore', new $objectClass($objectStore)); | |
| 89 | + } | |
| 90 | + | |
| 91 | + $storage->getAuthMechanism()->manipulateStorageConfig($storage, $user); | |
| 92 | + $storage->getBackend()->manipulateStorageConfig($storage, $user); | |
| 93 | + } | |
| 94 | + | |
| 95 | + /** | |
| 96 | + * Construct the storage implementation | |
| 97 | + * | |
| 98 | + * @param StorageConfig $storageConfig | |
| 99 | + * @return Storage | |
| 100 | + */ | |
| 101 | +    private function constructStorage(StorageConfig $storageConfig) { | |
| 102 | + $class = $storageConfig->getBackend()->getStorageClass(); | |
| 103 | + $storage = new $class($storageConfig->getBackendOptions()); | |
| 104 | + | |
| 105 | + // auth mechanism should fire first | |
| 106 | + $storage = $storageConfig->getBackend()->wrapStorage($storage); | |
| 107 | + $storage = $storageConfig->getAuthMechanism()->wrapStorage($storage); | |
| 108 | + | |
| 109 | + return $storage; | |
| 110 | + } | |
| 111 | + | |
| 112 | + /** | |
| 113 | + * Get all mountpoints applicable for the user | |
| 114 | + * | |
| 115 | + * @param \OCP\IUser $user | |
| 116 | + * @param \OCP\Files\Storage\IStorageFactory $loader | |
| 117 | + * @return \OCP\Files\Mount\IMountPoint[] | |
| 118 | + */ | |
| 119 | +    public function getMountsForUser(IUser $user, IStorageFactory $loader) { | |
| 120 | + $this->migrator->migrateUser($user); | |
| 121 | + | |
| 122 | + $this->userStoragesService->setUser($user); | |
| 123 | + $this->userGlobalStoragesService->setUser($user); | |
| 124 | + | |
| 125 | + $storageConfigs = $this->userGlobalStoragesService->getAllStoragesForUser(); | |
| 126 | + | |
| 127 | +        $storages = array_map(function(StorageConfig $storageConfig) use ($user) { | |
| 128 | +            try { | |
| 129 | + $this->prepareStorageConfig($storageConfig, $user); | |
| 130 | + return $this->constructStorage($storageConfig); | |
| 131 | +            } catch (\Exception $e) { | |
| 132 | + // propagate exception into filesystem | |
| 133 | + return new FailedStorage(['exception' => $e]); | |
| 134 | + } | |
| 135 | + }, $storageConfigs); | |
| 136 | + | |
| 137 | + | |
| 138 | +        \OC\Files\Cache\Storage::getGlobalCache()->loadForStorageIds(array_map(function(Storage\IStorage $storage) { | |
| 139 | + return $storage->getId(); | |
| 140 | + }, $storages)); | |
| 141 | + | |
| 142 | +        $availableStorages = array_map(function (Storage\IStorage $storage, StorageConfig $storageConfig) { | |
| 143 | +            try { | |
| 144 | + $availability = $storage->getAvailability(); | |
| 145 | +                if (!$availability['available'] && !Availability::shouldRecheck($availability)) { | |
| 146 | + $storage = new FailedStorage([ | |
| 147 | +                        'exception' => new StorageNotAvailableException('Storage with mount id ' . $storageConfig->getId() . ' is not available') | |
| 148 | + ]); | |
| 149 | + } | |
| 150 | +            } catch (\Exception $e) { | |
| 151 | + // propagate exception into filesystem | |
| 152 | + $storage = new FailedStorage(['exception' => $e]); | |
| 153 | + } | |
| 154 | + return $storage; | |
| 155 | + }, $storages, $storageConfigs); | |
| 156 | + | |
| 157 | +        $mounts = array_map(function(StorageConfig $storageConfig, Storage\IStorage $storage) use ($user, $loader) { | |
| 158 | +            if ($storageConfig->getType() === StorageConfig::MOUNT_TYPE_PERSONAl) { | |
| 159 | + return new PersonalMount( | |
| 160 | + $this->userStoragesService, | |
| 161 | + $storageConfig->getId(), | |
| 162 | + $storage, | |
| 163 | + '/' . $user->getUID() . '/files' . $storageConfig->getMountPoint(), | |
| 164 | + null, | |
| 165 | + $loader, | |
| 166 | + $storageConfig->getMountOptions() | |
| 167 | + ); | |
| 168 | +            } else { | |
| 169 | + return new ExternalMountPoint( | |
| 170 | + $storage, | |
| 171 | + '/' . $user->getUID() . '/files' . $storageConfig->getMountPoint(), | |
| 172 | + null, | |
| 173 | + $loader, | |
| 174 | + $storageConfig->getMountOptions(), | |
| 175 | + $storageConfig->getId() | |
| 176 | + ); | |
| 177 | + } | |
| 178 | + }, $storageConfigs, $availableStorages); | |
| 179 | + | |
| 180 | + $this->userStoragesService->resetUser(); | |
| 181 | + $this->userGlobalStoragesService->resetUser(); | |
| 182 | + | |
| 183 | + return $mounts; | |
| 184 | + } | |
| 185 | 185 | } | 
| @@ -32,58 +32,58 @@ | ||
| 32 | 32 | |
| 33 | 33 |  class UserContext { | 
| 34 | 34 | |
| 35 | - /** @var IUserSession */ | |
| 36 | - private $session; | |
| 35 | + /** @var IUserSession */ | |
| 36 | + private $session; | |
| 37 | 37 | |
| 38 | - /** @var ShareManager */ | |
| 39 | - private $shareManager; | |
| 38 | + /** @var ShareManager */ | |
| 39 | + private $shareManager; | |
| 40 | 40 | |
| 41 | - /** @var IRequest */ | |
| 42 | - private $request; | |
| 41 | + /** @var IRequest */ | |
| 42 | + private $request; | |
| 43 | 43 | |
| 44 | - /** @var string */ | |
| 45 | - private $userId; | |
| 44 | + /** @var string */ | |
| 45 | + private $userId; | |
| 46 | 46 | |
| 47 | - /** @var IUserManager */ | |
| 48 | - private $userManager; | |
| 47 | + /** @var IUserManager */ | |
| 48 | + private $userManager; | |
| 49 | 49 | |
| 50 | -	public function __construct(IUserSession $session, ShareManager $manager, IRequest $request, IUserManager $userManager) { | |
| 51 | - $this->session = $session; | |
| 52 | - $this->shareManager = $manager; | |
| 53 | - $this->request = $request; | |
| 54 | - $this->userManager = $userManager; | |
| 55 | - } | |
| 50 | +    public function __construct(IUserSession $session, ShareManager $manager, IRequest $request, IUserManager $userManager) { | |
| 51 | + $this->session = $session; | |
| 52 | + $this->shareManager = $manager; | |
| 53 | + $this->request = $request; | |
| 54 | + $this->userManager = $userManager; | |
| 55 | + } | |
| 56 | 56 | |
| 57 | -	public function getSession(): IUserSession { | |
| 58 | - return $this->session; | |
| 59 | - } | |
| 57 | +    public function getSession(): IUserSession { | |
| 58 | + return $this->session; | |
| 59 | + } | |
| 60 | 60 | |
| 61 | -	public function setUserId(string $userId): void { | |
| 62 | - $this->userId = $userId; | |
| 63 | - } | |
| 61 | +    public function setUserId(string $userId): void { | |
| 62 | + $this->userId = $userId; | |
| 63 | + } | |
| 64 | 64 | |
| 65 | -	protected function getUserId(): ?string { | |
| 66 | -		if ($this->userId !== null) { | |
| 67 | - return $this->userId; | |
| 68 | - } | |
| 69 | -		if($this->session && $this->session->getUser() !== null) { | |
| 70 | - return $this->session->getUser()->getUID(); | |
| 71 | - } | |
| 72 | -		try { | |
| 73 | -			$shareToken = $this->request->getParam('token'); | |
| 74 | - $share = $this->shareManager->getShareByToken($shareToken); | |
| 75 | - return $share->getShareOwner(); | |
| 76 | -		} catch (ShareNotFound $e) {} | |
| 65 | +    protected function getUserId(): ?string { | |
| 66 | +        if ($this->userId !== null) { | |
| 67 | + return $this->userId; | |
| 68 | + } | |
| 69 | +        if($this->session && $this->session->getUser() !== null) { | |
| 70 | + return $this->session->getUser()->getUID(); | |
| 71 | + } | |
| 72 | +        try { | |
| 73 | +            $shareToken = $this->request->getParam('token'); | |
| 74 | + $share = $this->shareManager->getShareByToken($shareToken); | |
| 75 | + return $share->getShareOwner(); | |
| 76 | +        } catch (ShareNotFound $e) {} | |
| 77 | 77 | |
| 78 | - return null; | |
| 79 | - } | |
| 78 | + return null; | |
| 79 | + } | |
| 80 | 80 | |
| 81 | -	protected function getUser(): ?IUser { | |
| 82 | - $userId = $this->getUserId(); | |
| 83 | -		if($userId !== null) { | |
| 84 | - return $this->userManager->get($userId); | |
| 85 | - } | |
| 86 | - return null; | |
| 87 | - } | |
| 81 | +    protected function getUser(): ?IUser { | |
| 82 | + $userId = $this->getUserId(); | |
| 83 | +        if($userId !== null) { | |
| 84 | + return $this->userManager->get($userId); | |
| 85 | + } | |
| 86 | + return null; | |
| 87 | + } | |
| 88 | 88 | |
| 89 | 89 | } | 
| @@ -66,7 +66,7 @@ discard block | ||
| 66 | 66 |  		if ($this->userId !== null) { | 
| 67 | 67 | return $this->userId; | 
| 68 | 68 | } | 
| 69 | -		if($this->session && $this->session->getUser() !== null) { | |
| 69 | +		if ($this->session && $this->session->getUser() !== null) { | |
| 70 | 70 | return $this->session->getUser()->getUID(); | 
| 71 | 71 | } | 
| 72 | 72 |  		try { | 
| @@ -80,7 +80,7 @@ discard block | ||
| 80 | 80 | |
| 81 | 81 |  	protected function getUser(): ?IUser { | 
| 82 | 82 | $userId = $this->getUserId(); | 
| 83 | -		if($userId !== null) { | |
| 83 | +		if ($userId !== null) { | |
| 84 | 84 | return $this->userManager->get($userId); | 
| 85 | 85 | } | 
| 86 | 86 | return null; | 
| @@ -56,415 +56,415 @@ | ||
| 56 | 56 | * Class to configure mount.json globally and for users | 
| 57 | 57 | */ | 
| 58 | 58 |  class OC_Mount_Config { | 
| 59 | - // TODO: make this class non-static and give it a proper namespace | |
| 60 | - | |
| 61 | - const MOUNT_TYPE_GLOBAL = 'global'; | |
| 62 | - const MOUNT_TYPE_GROUP = 'group'; | |
| 63 | - const MOUNT_TYPE_USER = 'user'; | |
| 64 | - const MOUNT_TYPE_PERSONAL = 'personal'; | |
| 65 | - | |
| 66 | - // whether to skip backend test (for unit tests, as this static class is not mockable) | |
| 67 | - public static $skipTest = false; | |
| 68 | - | |
| 69 | - /** @var Application */ | |
| 70 | - public static $app; | |
| 71 | - | |
| 72 | - /** | |
| 73 | - * @param string $class | |
| 74 | - * @param array $definition | |
| 75 | - * @return bool | |
| 76 | - * @deprecated 8.2.0 use \OCA\Files_External\Service\BackendService::registerBackend() | |
| 77 | - */ | |
| 78 | -	public static function registerBackend($class, $definition) { | |
| 79 | - $backendService = self::$app->getContainer()->query(BackendService::class); | |
| 80 | - $auth = self::$app->getContainer()->query(Builtin::class); | |
| 81 | - | |
| 82 | - $backendService->registerBackend(new LegacyBackend($class, $definition, $auth)); | |
| 83 | - | |
| 84 | - return true; | |
| 85 | - } | |
| 86 | - | |
| 87 | - /** | |
| 88 | - * Returns the mount points for the given user. | |
| 89 | - * The mount point is relative to the data directory. | |
| 90 | - * | |
| 91 | - * @param string $uid user | |
| 92 | - * @return array of mount point string as key, mountpoint config as value | |
| 93 | - * | |
| 94 | - * @deprecated 8.2.0 use UserGlobalStoragesService::getStorages() and UserStoragesService::getStorages() | |
| 95 | - */ | |
| 96 | -	public static function getAbsoluteMountPoints($uid) { | |
| 97 | - $mountPoints = array(); | |
| 98 | - | |
| 99 | - $userGlobalStoragesService = self::$app->getContainer()->query(UserGlobalStoragesService::class); | |
| 100 | - $userStoragesService = self::$app->getContainer()->query(UserStoragesService::class); | |
| 101 | - $user = self::$app->getContainer()->query(IUserManager::class)->get($uid); | |
| 102 | - | |
| 103 | - $userGlobalStoragesService->setUser($user); | |
| 104 | - $userStoragesService->setUser($user); | |
| 105 | - | |
| 106 | -		foreach ($userGlobalStoragesService->getStorages() as $storage) { | |
| 107 | - /** @var \OCA\Files_External\Lib\StorageConfig $storage */ | |
| 108 | - $mountPoint = '/'.$uid.'/files'.$storage->getMountPoint(); | |
| 109 | - $mountEntry = self::prepareMountPointEntry($storage, false); | |
| 110 | -			foreach ($mountEntry['options'] as &$option) { | |
| 111 | - $option = self::substitutePlaceholdersInConfig($option, $uid); | |
| 112 | - } | |
| 113 | - $mountPoints[$mountPoint] = $mountEntry; | |
| 114 | - } | |
| 115 | - | |
| 116 | -		foreach ($userStoragesService->getStorages() as $storage) { | |
| 117 | - $mountPoint = '/'.$uid.'/files'.$storage->getMountPoint(); | |
| 118 | - $mountEntry = self::prepareMountPointEntry($storage, true); | |
| 119 | -			foreach ($mountEntry['options'] as &$option) { | |
| 120 | - $option = self::substitutePlaceholdersInConfig($option, $uid); | |
| 121 | - } | |
| 122 | - $mountPoints[$mountPoint] = $mountEntry; | |
| 123 | - } | |
| 124 | - | |
| 125 | - $userGlobalStoragesService->resetUser(); | |
| 126 | - $userStoragesService->resetUser(); | |
| 127 | - | |
| 128 | - return $mountPoints; | |
| 129 | - } | |
| 130 | - | |
| 131 | - /** | |
| 132 | - * Get the system mount points | |
| 133 | - * | |
| 134 | - * @return array | |
| 135 | - * | |
| 136 | - * @deprecated 8.2.0 use GlobalStoragesService::getStorages() | |
| 137 | - */ | |
| 138 | -	public static function getSystemMountPoints() { | |
| 139 | - $mountPoints = []; | |
| 140 | - $service = self::$app->getContainer()->query(GlobalStoragesService::class); | |
| 141 | - | |
| 142 | -		foreach ($service->getStorages() as $storage) { | |
| 143 | - $mountPoints[] = self::prepareMountPointEntry($storage, false); | |
| 144 | - } | |
| 145 | - | |
| 146 | - return $mountPoints; | |
| 147 | - } | |
| 148 | - | |
| 149 | - /** | |
| 150 | - * Get the personal mount points of the current user | |
| 151 | - * | |
| 152 | - * @return array | |
| 153 | - * | |
| 154 | - * @deprecated 8.2.0 use UserStoragesService::getStorages() | |
| 155 | - */ | |
| 156 | -	public static function getPersonalMountPoints() { | |
| 157 | - $mountPoints = []; | |
| 158 | - $service = self::$app->getContainer()->query(UserStoragesService::class); | |
| 159 | - | |
| 160 | -		foreach ($service->getStorages() as $storage) { | |
| 161 | - $mountPoints[] = self::prepareMountPointEntry($storage, true); | |
| 162 | - } | |
| 163 | - | |
| 164 | - return $mountPoints; | |
| 165 | - } | |
| 166 | - | |
| 167 | - /** | |
| 168 | - * Convert a StorageConfig to the legacy mountPoints array format | |
| 169 | - * There's a lot of extra information in here, to satisfy all of the legacy functions | |
| 170 | - * | |
| 171 | - * @param StorageConfig $storage | |
| 172 | - * @param bool $isPersonal | |
| 173 | - * @return array | |
| 174 | - */ | |
| 175 | -	private static function prepareMountPointEntry(StorageConfig $storage, $isPersonal) { | |
| 176 | - $mountEntry = []; | |
| 177 | - | |
| 178 | - $mountEntry['mountpoint'] = substr($storage->getMountPoint(), 1); // remove leading slash | |
| 179 | - $mountEntry['class'] = $storage->getBackend()->getIdentifier(); | |
| 180 | - $mountEntry['backend'] = $storage->getBackend()->getText(); | |
| 181 | - $mountEntry['authMechanism'] = $storage->getAuthMechanism()->getIdentifier(); | |
| 182 | - $mountEntry['personal'] = $isPersonal; | |
| 183 | - $mountEntry['options'] = self::decryptPasswords($storage->getBackendOptions()); | |
| 184 | - $mountEntry['mountOptions'] = $storage->getMountOptions(); | |
| 185 | - $mountEntry['priority'] = $storage->getPriority(); | |
| 186 | - $mountEntry['applicable'] = [ | |
| 187 | - 'groups' => $storage->getApplicableGroups(), | |
| 188 | - 'users' => $storage->getApplicableUsers(), | |
| 189 | - ]; | |
| 190 | - // if mountpoint is applicable to all users the old API expects ['all'] | |
| 191 | -		if (empty($mountEntry['applicable']['groups']) && empty($mountEntry['applicable']['users'])) { | |
| 192 | - $mountEntry['applicable']['users'] = ['all']; | |
| 193 | - } | |
| 194 | - | |
| 195 | - $mountEntry['id'] = $storage->getId(); | |
| 196 | - | |
| 197 | - return $mountEntry; | |
| 198 | - } | |
| 199 | - | |
| 200 | - /** | |
| 201 | - * fill in the correct values for $user | |
| 202 | - * | |
| 203 | - * @param string $user user value | |
| 204 | - * @param string|array $input | |
| 205 | - * @return string | |
| 206 | - * @deprecated use self::substitutePlaceholdersInConfig($input) | |
| 207 | - */ | |
| 208 | -	public static function setUserVars($user, $input) { | |
| 209 | - $handler = self::$app->getContainer()->query(UserPlaceholderHandler::class); | |
| 210 | - return $handler->handle($input); | |
| 211 | - } | |
| 212 | - | |
| 213 | - /** | |
| 214 | - * @param mixed $input | |
| 215 | - * @param string|null $userId | |
| 216 | - * @return mixed | |
| 217 | - * @throws \OCP\AppFramework\QueryException | |
| 218 | - * @since 16.0.0 | |
| 219 | - */ | |
| 220 | -	public static function substitutePlaceholdersInConfig($input, string $userId = null) { | |
| 221 | - /** @var BackendService $backendService */ | |
| 222 | - $backendService = self::$app->getContainer()->query(BackendService::class); | |
| 223 | - /** @var IConfigHandler[] $handlers */ | |
| 224 | - $handlers = $backendService->getConfigHandlers(); | |
| 225 | -		foreach ($handlers as $handler) { | |
| 226 | -			if ($handler instanceof UserContext && $userId !== null) { | |
| 227 | - $handler->setUserId($userId); | |
| 228 | - } | |
| 229 | - $input = $handler->handle($input); | |
| 230 | - } | |
| 231 | - return $input; | |
| 232 | - } | |
| 233 | - | |
| 234 | - /** | |
| 235 | - * Test connecting using the given backend configuration | |
| 236 | - * | |
| 237 | - * @param string $class backend class name | |
| 238 | - * @param array $options backend configuration options | |
| 239 | - * @param boolean $isPersonal | |
| 240 | - * @return int see self::STATUS_* | |
| 241 | - * @throws Exception | |
| 242 | - */ | |
| 243 | -	public static function getBackendStatus($class, $options, $isPersonal, $testOnly = true) { | |
| 244 | -		if (self::$skipTest) { | |
| 245 | - return StorageNotAvailableException::STATUS_SUCCESS; | |
| 246 | - } | |
| 247 | -		foreach ($options as $key => &$option) { | |
| 248 | -			if($key === 'password') { | |
| 249 | - // no replacements in passwords | |
| 250 | - continue; | |
| 251 | - } | |
| 252 | - $option = self::substitutePlaceholdersInConfig($option); | |
| 253 | -			if(!self::arePlaceholdersSubstituted($option)) { | |
| 254 | - \OC::$server->getLogger()->error( | |
| 255 | -					'A placeholder was not substituted: {option} for mount type {class}', | |
| 256 | - [ | |
| 257 | - 'app' => 'files_external', | |
| 258 | - 'option' => $option, | |
| 259 | - 'class' => $class, | |
| 260 | - ] | |
| 261 | - ); | |
| 262 | - throw new StorageNotAvailableException( | |
| 263 | - 'Mount configuration incomplete', | |
| 264 | - StorageNotAvailableException::STATUS_INCOMPLETE_CONF | |
| 265 | - ); | |
| 266 | - } | |
| 267 | - } | |
| 268 | -		if (class_exists($class)) { | |
| 269 | -			try { | |
| 270 | - /** @var \OC\Files\Storage\Common $storage */ | |
| 271 | - $storage = new $class($options); | |
| 272 | - | |
| 273 | -				try { | |
| 274 | - $result = $storage->test($isPersonal, $testOnly); | |
| 275 | - $storage->setAvailability($result); | |
| 276 | -					if ($result) { | |
| 277 | - return StorageNotAvailableException::STATUS_SUCCESS; | |
| 278 | - } | |
| 279 | -				} catch (\Exception $e) { | |
| 280 | - $storage->setAvailability(false); | |
| 281 | - throw $e; | |
| 282 | - } | |
| 283 | -			} catch (Exception $exception) { | |
| 284 | - \OC::$server->getLogger()->logException($exception, ['app' => 'files_external']); | |
| 285 | - throw $exception; | |
| 286 | - } | |
| 287 | - } | |
| 288 | - return StorageNotAvailableException::STATUS_ERROR; | |
| 289 | - } | |
| 290 | - | |
| 291 | -	public static function arePlaceholdersSubstituted($option):bool { | |
| 292 | - $result = true; | |
| 293 | -		if(is_array($option)) { | |
| 294 | -			foreach ($option as $optionItem) { | |
| 295 | - $result = $result && self::arePlaceholdersSubstituted($optionItem); | |
| 296 | - } | |
| 297 | -		} else if (is_string($option)) { | |
| 298 | -			if (strpos(rtrim($option, '$'), '$') !== false) { | |
| 299 | - $result = false; | |
| 300 | - } | |
| 301 | - } | |
| 302 | - return $result; | |
| 303 | - } | |
| 304 | - | |
| 305 | - /** | |
| 306 | - * Read the mount points in the config file into an array | |
| 307 | - * | |
| 308 | - * @param string|null $user If not null, personal for $user, otherwise system | |
| 309 | - * @return array | |
| 310 | - */ | |
| 311 | -	public static function readData($user = null) { | |
| 312 | -		if (isset($user)) { | |
| 313 | - $jsonFile = \OC::$server->getUserManager()->get($user)->getHome() . '/mount.json'; | |
| 314 | -		} else { | |
| 315 | - $config = \OC::$server->getConfig(); | |
| 316 | -			$datadir = $config->getSystemValue('datadirectory', \OC::$SERVERROOT . '/data/'); | |
| 317 | -			$jsonFile = $config->getSystemValue('mount_file', $datadir . '/mount.json'); | |
| 318 | - } | |
| 319 | -		if (is_file($jsonFile)) { | |
| 320 | - $mountPoints = json_decode(file_get_contents($jsonFile), true); | |
| 321 | -			if (is_array($mountPoints)) { | |
| 322 | - return $mountPoints; | |
| 323 | - } | |
| 324 | - } | |
| 325 | - return array(); | |
| 326 | - } | |
| 327 | - | |
| 328 | - /** | |
| 329 | - * Get backend dependency message | |
| 330 | - * TODO: move into AppFramework along with templates | |
| 331 | - * | |
| 332 | - * @param Backend[] $backends | |
| 333 | - * @return string | |
| 334 | - */ | |
| 335 | -	public static function dependencyMessage($backends) { | |
| 336 | -		$l = \OC::$server->getL10N('files_external'); | |
| 337 | - $message = ''; | |
| 338 | - $dependencyGroups = []; | |
| 339 | - | |
| 340 | -		foreach ($backends as $backend) { | |
| 341 | -			foreach ($backend->checkDependencies() as $dependency) { | |
| 342 | -				if ($message = $dependency->getMessage()) { | |
| 343 | - $message .= '<p>' . $message . '</p>'; | |
| 344 | -				} else { | |
| 345 | - $dependencyGroups[$dependency->getDependency()][] = $backend; | |
| 346 | - } | |
| 347 | - } | |
| 348 | - } | |
| 349 | - | |
| 350 | -		foreach ($dependencyGroups as $module => $dependants) { | |
| 351 | -			$backends = implode(', ', array_map(function($backend) { | |
| 352 | - return '"' . $backend->getText() . '"'; | |
| 353 | - }, $dependants)); | |
| 354 | - $message .= '<p>' . OC_Mount_Config::getSingleDependencyMessage($l, $module, $backends) . '</p>'; | |
| 355 | - } | |
| 356 | - | |
| 357 | - return $message; | |
| 358 | - } | |
| 359 | - | |
| 360 | - /** | |
| 361 | - * Returns a dependency missing message | |
| 362 | - * | |
| 363 | - * @param \OCP\IL10N $l | |
| 364 | - * @param string $module | |
| 365 | - * @param string $backend | |
| 366 | - * @return string | |
| 367 | - */ | |
| 368 | -	private static function getSingleDependencyMessage(\OCP\IL10N $l, $module, $backend) { | |
| 369 | -		switch (strtolower($module)) { | |
| 370 | - case 'curl': | |
| 371 | -				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]); | |
| 372 | - case 'ftp': | |
| 373 | -				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]); | |
| 374 | - default: | |
| 375 | -				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]); | |
| 376 | - } | |
| 377 | - } | |
| 378 | - | |
| 379 | - /** | |
| 380 | - * Encrypt passwords in the given config options | |
| 381 | - * | |
| 382 | - * @param array $options mount options | |
| 383 | - * @return array updated options | |
| 384 | - */ | |
| 385 | -	public static function encryptPasswords($options) { | |
| 386 | -		if (isset($options['password'])) { | |
| 387 | - $options['password_encrypted'] = self::encryptPassword($options['password']); | |
| 388 | - // do not unset the password, we want to keep the keys order | |
| 389 | - // on load... because that's how the UI currently works | |
| 390 | - $options['password'] = ''; | |
| 391 | - } | |
| 392 | - return $options; | |
| 393 | - } | |
| 394 | - | |
| 395 | - /** | |
| 396 | - * Decrypt passwords in the given config options | |
| 397 | - * | |
| 398 | - * @param array $options mount options | |
| 399 | - * @return array updated options | |
| 400 | - */ | |
| 401 | -	public static function decryptPasswords($options) { | |
| 402 | - // note: legacy options might still have the unencrypted password in the "password" field | |
| 403 | -		if (isset($options['password_encrypted'])) { | |
| 404 | - $options['password'] = self::decryptPassword($options['password_encrypted']); | |
| 405 | - unset($options['password_encrypted']); | |
| 406 | - } | |
| 407 | - return $options; | |
| 408 | - } | |
| 409 | - | |
| 410 | - /** | |
| 411 | - * Encrypt a single password | |
| 412 | - * | |
| 413 | - * @param string $password plain text password | |
| 414 | - * @return string encrypted password | |
| 415 | - */ | |
| 416 | -	private static function encryptPassword($password) { | |
| 417 | - $cipher = self::getCipher(); | |
| 418 | - $iv = \OC::$server->getSecureRandom()->generate(16); | |
| 419 | - $cipher->setIV($iv); | |
| 420 | - return base64_encode($iv . $cipher->encrypt($password)); | |
| 421 | - } | |
| 422 | - | |
| 423 | - /** | |
| 424 | - * Decrypts a single password | |
| 425 | - * | |
| 426 | - * @param string $encryptedPassword encrypted password | |
| 427 | - * @return string plain text password | |
| 428 | - */ | |
| 429 | -	private static function decryptPassword($encryptedPassword) { | |
| 430 | - $cipher = self::getCipher(); | |
| 431 | - $binaryPassword = base64_decode($encryptedPassword); | |
| 432 | - $iv = substr($binaryPassword, 0, 16); | |
| 433 | - $cipher->setIV($iv); | |
| 434 | - $binaryPassword = substr($binaryPassword, 16); | |
| 435 | - return $cipher->decrypt($binaryPassword); | |
| 436 | - } | |
| 437 | - | |
| 438 | - /** | |
| 439 | - * Returns the encryption cipher | |
| 440 | - * | |
| 441 | - * @return AES | |
| 442 | - */ | |
| 443 | -	private static function getCipher() { | |
| 444 | - $cipher = new AES(AES::MODE_CBC); | |
| 445 | -		$cipher->setKey(\OC::$server->getConfig()->getSystemValue('passwordsalt', null)); | |
| 446 | - return $cipher; | |
| 447 | - } | |
| 448 | - | |
| 449 | - /** | |
| 450 | - * Computes a hash based on the given configuration. | |
| 451 | - * This is mostly used to find out whether configurations | |
| 452 | - * are the same. | |
| 453 | - * | |
| 454 | - * @param array $config | |
| 455 | - * @return string | |
| 456 | - */ | |
| 457 | -	public static function makeConfigHash($config) { | |
| 458 | - $data = json_encode( | |
| 459 | - array( | |
| 460 | - 'c' => $config['backend'], | |
| 461 | - 'a' => $config['authMechanism'], | |
| 462 | - 'm' => $config['mountpoint'], | |
| 463 | - 'o' => $config['options'], | |
| 464 | - 'p' => isset($config['priority']) ? $config['priority'] : -1, | |
| 465 | - 'mo' => isset($config['mountOptions']) ? $config['mountOptions'] : [], | |
| 466 | - ) | |
| 467 | - ); | |
| 468 | -		return hash('md5', $data); | |
| 469 | - } | |
| 59 | + // TODO: make this class non-static and give it a proper namespace | |
| 60 | + | |
| 61 | + const MOUNT_TYPE_GLOBAL = 'global'; | |
| 62 | + const MOUNT_TYPE_GROUP = 'group'; | |
| 63 | + const MOUNT_TYPE_USER = 'user'; | |
| 64 | + const MOUNT_TYPE_PERSONAL = 'personal'; | |
| 65 | + | |
| 66 | + // whether to skip backend test (for unit tests, as this static class is not mockable) | |
| 67 | + public static $skipTest = false; | |
| 68 | + | |
| 69 | + /** @var Application */ | |
| 70 | + public static $app; | |
| 71 | + | |
| 72 | + /** | |
| 73 | + * @param string $class | |
| 74 | + * @param array $definition | |
| 75 | + * @return bool | |
| 76 | + * @deprecated 8.2.0 use \OCA\Files_External\Service\BackendService::registerBackend() | |
| 77 | + */ | |
| 78 | +    public static function registerBackend($class, $definition) { | |
| 79 | + $backendService = self::$app->getContainer()->query(BackendService::class); | |
| 80 | + $auth = self::$app->getContainer()->query(Builtin::class); | |
| 81 | + | |
| 82 | + $backendService->registerBackend(new LegacyBackend($class, $definition, $auth)); | |
| 83 | + | |
| 84 | + return true; | |
| 85 | + } | |
| 86 | + | |
| 87 | + /** | |
| 88 | + * Returns the mount points for the given user. | |
| 89 | + * The mount point is relative to the data directory. | |
| 90 | + * | |
| 91 | + * @param string $uid user | |
| 92 | + * @return array of mount point string as key, mountpoint config as value | |
| 93 | + * | |
| 94 | + * @deprecated 8.2.0 use UserGlobalStoragesService::getStorages() and UserStoragesService::getStorages() | |
| 95 | + */ | |
| 96 | +    public static function getAbsoluteMountPoints($uid) { | |
| 97 | + $mountPoints = array(); | |
| 98 | + | |
| 99 | + $userGlobalStoragesService = self::$app->getContainer()->query(UserGlobalStoragesService::class); | |
| 100 | + $userStoragesService = self::$app->getContainer()->query(UserStoragesService::class); | |
| 101 | + $user = self::$app->getContainer()->query(IUserManager::class)->get($uid); | |
| 102 | + | |
| 103 | + $userGlobalStoragesService->setUser($user); | |
| 104 | + $userStoragesService->setUser($user); | |
| 105 | + | |
| 106 | +        foreach ($userGlobalStoragesService->getStorages() as $storage) { | |
| 107 | + /** @var \OCA\Files_External\Lib\StorageConfig $storage */ | |
| 108 | + $mountPoint = '/'.$uid.'/files'.$storage->getMountPoint(); | |
| 109 | + $mountEntry = self::prepareMountPointEntry($storage, false); | |
| 110 | +            foreach ($mountEntry['options'] as &$option) { | |
| 111 | + $option = self::substitutePlaceholdersInConfig($option, $uid); | |
| 112 | + } | |
| 113 | + $mountPoints[$mountPoint] = $mountEntry; | |
| 114 | + } | |
| 115 | + | |
| 116 | +        foreach ($userStoragesService->getStorages() as $storage) { | |
| 117 | + $mountPoint = '/'.$uid.'/files'.$storage->getMountPoint(); | |
| 118 | + $mountEntry = self::prepareMountPointEntry($storage, true); | |
| 119 | +            foreach ($mountEntry['options'] as &$option) { | |
| 120 | + $option = self::substitutePlaceholdersInConfig($option, $uid); | |
| 121 | + } | |
| 122 | + $mountPoints[$mountPoint] = $mountEntry; | |
| 123 | + } | |
| 124 | + | |
| 125 | + $userGlobalStoragesService->resetUser(); | |
| 126 | + $userStoragesService->resetUser(); | |
| 127 | + | |
| 128 | + return $mountPoints; | |
| 129 | + } | |
| 130 | + | |
| 131 | + /** | |
| 132 | + * Get the system mount points | |
| 133 | + * | |
| 134 | + * @return array | |
| 135 | + * | |
| 136 | + * @deprecated 8.2.0 use GlobalStoragesService::getStorages() | |
| 137 | + */ | |
| 138 | +    public static function getSystemMountPoints() { | |
| 139 | + $mountPoints = []; | |
| 140 | + $service = self::$app->getContainer()->query(GlobalStoragesService::class); | |
| 141 | + | |
| 142 | +        foreach ($service->getStorages() as $storage) { | |
| 143 | + $mountPoints[] = self::prepareMountPointEntry($storage, false); | |
| 144 | + } | |
| 145 | + | |
| 146 | + return $mountPoints; | |
| 147 | + } | |
| 148 | + | |
| 149 | + /** | |
| 150 | + * Get the personal mount points of the current user | |
| 151 | + * | |
| 152 | + * @return array | |
| 153 | + * | |
| 154 | + * @deprecated 8.2.0 use UserStoragesService::getStorages() | |
| 155 | + */ | |
| 156 | +    public static function getPersonalMountPoints() { | |
| 157 | + $mountPoints = []; | |
| 158 | + $service = self::$app->getContainer()->query(UserStoragesService::class); | |
| 159 | + | |
| 160 | +        foreach ($service->getStorages() as $storage) { | |
| 161 | + $mountPoints[] = self::prepareMountPointEntry($storage, true); | |
| 162 | + } | |
| 163 | + | |
| 164 | + return $mountPoints; | |
| 165 | + } | |
| 166 | + | |
| 167 | + /** | |
| 168 | + * Convert a StorageConfig to the legacy mountPoints array format | |
| 169 | + * There's a lot of extra information in here, to satisfy all of the legacy functions | |
| 170 | + * | |
| 171 | + * @param StorageConfig $storage | |
| 172 | + * @param bool $isPersonal | |
| 173 | + * @return array | |
| 174 | + */ | |
| 175 | +    private static function prepareMountPointEntry(StorageConfig $storage, $isPersonal) { | |
| 176 | + $mountEntry = []; | |
| 177 | + | |
| 178 | + $mountEntry['mountpoint'] = substr($storage->getMountPoint(), 1); // remove leading slash | |
| 179 | + $mountEntry['class'] = $storage->getBackend()->getIdentifier(); | |
| 180 | + $mountEntry['backend'] = $storage->getBackend()->getText(); | |
| 181 | + $mountEntry['authMechanism'] = $storage->getAuthMechanism()->getIdentifier(); | |
| 182 | + $mountEntry['personal'] = $isPersonal; | |
| 183 | + $mountEntry['options'] = self::decryptPasswords($storage->getBackendOptions()); | |
| 184 | + $mountEntry['mountOptions'] = $storage->getMountOptions(); | |
| 185 | + $mountEntry['priority'] = $storage->getPriority(); | |
| 186 | + $mountEntry['applicable'] = [ | |
| 187 | + 'groups' => $storage->getApplicableGroups(), | |
| 188 | + 'users' => $storage->getApplicableUsers(), | |
| 189 | + ]; | |
| 190 | + // if mountpoint is applicable to all users the old API expects ['all'] | |
| 191 | +        if (empty($mountEntry['applicable']['groups']) && empty($mountEntry['applicable']['users'])) { | |
| 192 | + $mountEntry['applicable']['users'] = ['all']; | |
| 193 | + } | |
| 194 | + | |
| 195 | + $mountEntry['id'] = $storage->getId(); | |
| 196 | + | |
| 197 | + return $mountEntry; | |
| 198 | + } | |
| 199 | + | |
| 200 | + /** | |
| 201 | + * fill in the correct values for $user | |
| 202 | + * | |
| 203 | + * @param string $user user value | |
| 204 | + * @param string|array $input | |
| 205 | + * @return string | |
| 206 | + * @deprecated use self::substitutePlaceholdersInConfig($input) | |
| 207 | + */ | |
| 208 | +    public static function setUserVars($user, $input) { | |
| 209 | + $handler = self::$app->getContainer()->query(UserPlaceholderHandler::class); | |
| 210 | + return $handler->handle($input); | |
| 211 | + } | |
| 212 | + | |
| 213 | + /** | |
| 214 | + * @param mixed $input | |
| 215 | + * @param string|null $userId | |
| 216 | + * @return mixed | |
| 217 | + * @throws \OCP\AppFramework\QueryException | |
| 218 | + * @since 16.0.0 | |
| 219 | + */ | |
| 220 | +    public static function substitutePlaceholdersInConfig($input, string $userId = null) { | |
| 221 | + /** @var BackendService $backendService */ | |
| 222 | + $backendService = self::$app->getContainer()->query(BackendService::class); | |
| 223 | + /** @var IConfigHandler[] $handlers */ | |
| 224 | + $handlers = $backendService->getConfigHandlers(); | |
| 225 | +        foreach ($handlers as $handler) { | |
| 226 | +            if ($handler instanceof UserContext && $userId !== null) { | |
| 227 | + $handler->setUserId($userId); | |
| 228 | + } | |
| 229 | + $input = $handler->handle($input); | |
| 230 | + } | |
| 231 | + return $input; | |
| 232 | + } | |
| 233 | + | |
| 234 | + /** | |
| 235 | + * Test connecting using the given backend configuration | |
| 236 | + * | |
| 237 | + * @param string $class backend class name | |
| 238 | + * @param array $options backend configuration options | |
| 239 | + * @param boolean $isPersonal | |
| 240 | + * @return int see self::STATUS_* | |
| 241 | + * @throws Exception | |
| 242 | + */ | |
| 243 | +    public static function getBackendStatus($class, $options, $isPersonal, $testOnly = true) { | |
| 244 | +        if (self::$skipTest) { | |
| 245 | + return StorageNotAvailableException::STATUS_SUCCESS; | |
| 246 | + } | |
| 247 | +        foreach ($options as $key => &$option) { | |
| 248 | +            if($key === 'password') { | |
| 249 | + // no replacements in passwords | |
| 250 | + continue; | |
| 251 | + } | |
| 252 | + $option = self::substitutePlaceholdersInConfig($option); | |
| 253 | +            if(!self::arePlaceholdersSubstituted($option)) { | |
| 254 | + \OC::$server->getLogger()->error( | |
| 255 | +                    'A placeholder was not substituted: {option} for mount type {class}', | |
| 256 | + [ | |
| 257 | + 'app' => 'files_external', | |
| 258 | + 'option' => $option, | |
| 259 | + 'class' => $class, | |
| 260 | + ] | |
| 261 | + ); | |
| 262 | + throw new StorageNotAvailableException( | |
| 263 | + 'Mount configuration incomplete', | |
| 264 | + StorageNotAvailableException::STATUS_INCOMPLETE_CONF | |
| 265 | + ); | |
| 266 | + } | |
| 267 | + } | |
| 268 | +        if (class_exists($class)) { | |
| 269 | +            try { | |
| 270 | + /** @var \OC\Files\Storage\Common $storage */ | |
| 271 | + $storage = new $class($options); | |
| 272 | + | |
| 273 | +                try { | |
| 274 | + $result = $storage->test($isPersonal, $testOnly); | |
| 275 | + $storage->setAvailability($result); | |
| 276 | +                    if ($result) { | |
| 277 | + return StorageNotAvailableException::STATUS_SUCCESS; | |
| 278 | + } | |
| 279 | +                } catch (\Exception $e) { | |
| 280 | + $storage->setAvailability(false); | |
| 281 | + throw $e; | |
| 282 | + } | |
| 283 | +            } catch (Exception $exception) { | |
| 284 | + \OC::$server->getLogger()->logException($exception, ['app' => 'files_external']); | |
| 285 | + throw $exception; | |
| 286 | + } | |
| 287 | + } | |
| 288 | + return StorageNotAvailableException::STATUS_ERROR; | |
| 289 | + } | |
| 290 | + | |
| 291 | +    public static function arePlaceholdersSubstituted($option):bool { | |
| 292 | + $result = true; | |
| 293 | +        if(is_array($option)) { | |
| 294 | +            foreach ($option as $optionItem) { | |
| 295 | + $result = $result && self::arePlaceholdersSubstituted($optionItem); | |
| 296 | + } | |
| 297 | +        } else if (is_string($option)) { | |
| 298 | +            if (strpos(rtrim($option, '$'), '$') !== false) { | |
| 299 | + $result = false; | |
| 300 | + } | |
| 301 | + } | |
| 302 | + return $result; | |
| 303 | + } | |
| 304 | + | |
| 305 | + /** | |
| 306 | + * Read the mount points in the config file into an array | |
| 307 | + * | |
| 308 | + * @param string|null $user If not null, personal for $user, otherwise system | |
| 309 | + * @return array | |
| 310 | + */ | |
| 311 | +    public static function readData($user = null) { | |
| 312 | +        if (isset($user)) { | |
| 313 | + $jsonFile = \OC::$server->getUserManager()->get($user)->getHome() . '/mount.json'; | |
| 314 | +        } else { | |
| 315 | + $config = \OC::$server->getConfig(); | |
| 316 | +            $datadir = $config->getSystemValue('datadirectory', \OC::$SERVERROOT . '/data/'); | |
| 317 | +            $jsonFile = $config->getSystemValue('mount_file', $datadir . '/mount.json'); | |
| 318 | + } | |
| 319 | +        if (is_file($jsonFile)) { | |
| 320 | + $mountPoints = json_decode(file_get_contents($jsonFile), true); | |
| 321 | +            if (is_array($mountPoints)) { | |
| 322 | + return $mountPoints; | |
| 323 | + } | |
| 324 | + } | |
| 325 | + return array(); | |
| 326 | + } | |
| 327 | + | |
| 328 | + /** | |
| 329 | + * Get backend dependency message | |
| 330 | + * TODO: move into AppFramework along with templates | |
| 331 | + * | |
| 332 | + * @param Backend[] $backends | |
| 333 | + * @return string | |
| 334 | + */ | |
| 335 | +    public static function dependencyMessage($backends) { | |
| 336 | +        $l = \OC::$server->getL10N('files_external'); | |
| 337 | + $message = ''; | |
| 338 | + $dependencyGroups = []; | |
| 339 | + | |
| 340 | +        foreach ($backends as $backend) { | |
| 341 | +            foreach ($backend->checkDependencies() as $dependency) { | |
| 342 | +                if ($message = $dependency->getMessage()) { | |
| 343 | + $message .= '<p>' . $message . '</p>'; | |
| 344 | +                } else { | |
| 345 | + $dependencyGroups[$dependency->getDependency()][] = $backend; | |
| 346 | + } | |
| 347 | + } | |
| 348 | + } | |
| 349 | + | |
| 350 | +        foreach ($dependencyGroups as $module => $dependants) { | |
| 351 | +            $backends = implode(', ', array_map(function($backend) { | |
| 352 | + return '"' . $backend->getText() . '"'; | |
| 353 | + }, $dependants)); | |
| 354 | + $message .= '<p>' . OC_Mount_Config::getSingleDependencyMessage($l, $module, $backends) . '</p>'; | |
| 355 | + } | |
| 356 | + | |
| 357 | + return $message; | |
| 358 | + } | |
| 359 | + | |
| 360 | + /** | |
| 361 | + * Returns a dependency missing message | |
| 362 | + * | |
| 363 | + * @param \OCP\IL10N $l | |
| 364 | + * @param string $module | |
| 365 | + * @param string $backend | |
| 366 | + * @return string | |
| 367 | + */ | |
| 368 | +    private static function getSingleDependencyMessage(\OCP\IL10N $l, $module, $backend) { | |
| 369 | +        switch (strtolower($module)) { | |
| 370 | + case 'curl': | |
| 371 | +                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]); | |
| 372 | + case 'ftp': | |
| 373 | +                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]); | |
| 374 | + default: | |
| 375 | +                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]); | |
| 376 | + } | |
| 377 | + } | |
| 378 | + | |
| 379 | + /** | |
| 380 | + * Encrypt passwords in the given config options | |
| 381 | + * | |
| 382 | + * @param array $options mount options | |
| 383 | + * @return array updated options | |
| 384 | + */ | |
| 385 | +    public static function encryptPasswords($options) { | |
| 386 | +        if (isset($options['password'])) { | |
| 387 | + $options['password_encrypted'] = self::encryptPassword($options['password']); | |
| 388 | + // do not unset the password, we want to keep the keys order | |
| 389 | + // on load... because that's how the UI currently works | |
| 390 | + $options['password'] = ''; | |
| 391 | + } | |
| 392 | + return $options; | |
| 393 | + } | |
| 394 | + | |
| 395 | + /** | |
| 396 | + * Decrypt passwords in the given config options | |
| 397 | + * | |
| 398 | + * @param array $options mount options | |
| 399 | + * @return array updated options | |
| 400 | + */ | |
| 401 | +    public static function decryptPasswords($options) { | |
| 402 | + // note: legacy options might still have the unencrypted password in the "password" field | |
| 403 | +        if (isset($options['password_encrypted'])) { | |
| 404 | + $options['password'] = self::decryptPassword($options['password_encrypted']); | |
| 405 | + unset($options['password_encrypted']); | |
| 406 | + } | |
| 407 | + return $options; | |
| 408 | + } | |
| 409 | + | |
| 410 | + /** | |
| 411 | + * Encrypt a single password | |
| 412 | + * | |
| 413 | + * @param string $password plain text password | |
| 414 | + * @return string encrypted password | |
| 415 | + */ | |
| 416 | +    private static function encryptPassword($password) { | |
| 417 | + $cipher = self::getCipher(); | |
| 418 | + $iv = \OC::$server->getSecureRandom()->generate(16); | |
| 419 | + $cipher->setIV($iv); | |
| 420 | + return base64_encode($iv . $cipher->encrypt($password)); | |
| 421 | + } | |
| 422 | + | |
| 423 | + /** | |
| 424 | + * Decrypts a single password | |
| 425 | + * | |
| 426 | + * @param string $encryptedPassword encrypted password | |
| 427 | + * @return string plain text password | |
| 428 | + */ | |
| 429 | +    private static function decryptPassword($encryptedPassword) { | |
| 430 | + $cipher = self::getCipher(); | |
| 431 | + $binaryPassword = base64_decode($encryptedPassword); | |
| 432 | + $iv = substr($binaryPassword, 0, 16); | |
| 433 | + $cipher->setIV($iv); | |
| 434 | + $binaryPassword = substr($binaryPassword, 16); | |
| 435 | + return $cipher->decrypt($binaryPassword); | |
| 436 | + } | |
| 437 | + | |
| 438 | + /** | |
| 439 | + * Returns the encryption cipher | |
| 440 | + * | |
| 441 | + * @return AES | |
| 442 | + */ | |
| 443 | +    private static function getCipher() { | |
| 444 | + $cipher = new AES(AES::MODE_CBC); | |
| 445 | +        $cipher->setKey(\OC::$server->getConfig()->getSystemValue('passwordsalt', null)); | |
| 446 | + return $cipher; | |
| 447 | + } | |
| 448 | + | |
| 449 | + /** | |
| 450 | + * Computes a hash based on the given configuration. | |
| 451 | + * This is mostly used to find out whether configurations | |
| 452 | + * are the same. | |
| 453 | + * | |
| 454 | + * @param array $config | |
| 455 | + * @return string | |
| 456 | + */ | |
| 457 | +    public static function makeConfigHash($config) { | |
| 458 | + $data = json_encode( | |
| 459 | + array( | |
| 460 | + 'c' => $config['backend'], | |
| 461 | + 'a' => $config['authMechanism'], | |
| 462 | + 'm' => $config['mountpoint'], | |
| 463 | + 'o' => $config['options'], | |
| 464 | + 'p' => isset($config['priority']) ? $config['priority'] : -1, | |
| 465 | + 'mo' => isset($config['mountOptions']) ? $config['mountOptions'] : [], | |
| 466 | + ) | |
| 467 | + ); | |
| 468 | +        return hash('md5', $data); | |
| 469 | + } | |
| 470 | 470 | } |