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