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