| Total Complexity | 53 |
| Total Lines | 407 |
| 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 |
||
| 57 | class OC_Mount_Config { |
||
| 58 | // TODO: make this class non-static and give it a proper namespace |
||
| 59 | |||
| 60 | const MOUNT_TYPE_GLOBAL = 'global'; |
||
| 61 | const MOUNT_TYPE_GROUP = 'group'; |
||
| 62 | const MOUNT_TYPE_USER = 'user'; |
||
| 63 | const MOUNT_TYPE_PERSONAL = 'personal'; |
||
| 64 | |||
| 65 | // whether to skip backend test (for unit tests, as this static class is not mockable) |
||
| 66 | public static $skipTest = false; |
||
| 67 | |||
| 68 | /** @var Application */ |
||
| 69 | public static $app; |
||
| 70 | |||
| 71 | /** |
||
| 72 | * @param string $class |
||
| 73 | * @param array $definition |
||
| 74 | * @return bool |
||
| 75 | * @deprecated 8.2.0 use \OCA\Files_External\Service\BackendService::registerBackend() |
||
| 76 | */ |
||
| 77 | public static function registerBackend($class, $definition) { |
||
| 84 | } |
||
| 85 | |||
| 86 | /** |
||
| 87 | * Returns the mount points for the given user. |
||
| 88 | * The mount point is relative to the data directory. |
||
| 89 | * |
||
| 90 | * @param string $uid user |
||
| 91 | * @return array of mount point string as key, mountpoint config as value |
||
| 92 | * |
||
| 93 | * @deprecated 8.2.0 use UserGlobalStoragesService::getStorages() and UserStoragesService::getStorages() |
||
| 94 | */ |
||
| 95 | public static function getAbsoluteMountPoints($uid) { |
||
| 96 | $mountPoints = array(); |
||
| 97 | |||
| 98 | $userGlobalStoragesService = self::$app->getContainer()->query(UserGlobalStoragesService::class); |
||
| 99 | $userStoragesService = self::$app->getContainer()->query(UserStoragesService::class); |
||
| 100 | $user = self::$app->getContainer()->query(IUserManager::class)->get($uid); |
||
| 101 | |||
| 102 | $userGlobalStoragesService->setUser($user); |
||
| 103 | $userStoragesService->setUser($user); |
||
| 104 | |||
| 105 | foreach ($userGlobalStoragesService->getStorages() as $storage) { |
||
| 106 | /** @var \OCA\Files_External\Lib\StorageConfig $storage */ |
||
| 107 | $mountPoint = '/'.$uid.'/files'.$storage->getMountPoint(); |
||
| 108 | $mountEntry = self::prepareMountPointEntry($storage, false); |
||
| 109 | foreach ($mountEntry['options'] as &$option) { |
||
| 110 | $option = self::substitutePlaceholdersInConfig($option); |
||
| 111 | } |
||
| 112 | $mountPoints[$mountPoint] = $mountEntry; |
||
| 113 | } |
||
| 114 | |||
| 115 | foreach ($userStoragesService->getStorages() as $storage) { |
||
| 116 | $mountPoint = '/'.$uid.'/files'.$storage->getMountPoint(); |
||
| 117 | $mountEntry = self::prepareMountPointEntry($storage, true); |
||
| 118 | foreach ($mountEntry['options'] as &$option) { |
||
| 119 | $option = self::substitutePlaceholdersInConfig($uid, $option); |
||
|
|
|||
| 120 | } |
||
| 121 | $mountPoints[$mountPoint] = $mountEntry; |
||
| 122 | } |
||
| 123 | |||
| 124 | $userGlobalStoragesService->resetUser(); |
||
| 125 | $userStoragesService->resetUser(); |
||
| 126 | |||
| 127 | return $mountPoints; |
||
| 128 | } |
||
| 129 | |||
| 130 | /** |
||
| 131 | * Get the system mount points |
||
| 132 | * |
||
| 133 | * @return array |
||
| 134 | * |
||
| 135 | * @deprecated 8.2.0 use GlobalStoragesService::getStorages() |
||
| 136 | */ |
||
| 137 | public static function getSystemMountPoints() { |
||
| 146 | } |
||
| 147 | |||
| 148 | /** |
||
| 149 | * Get the personal mount points of the current user |
||
| 150 | * |
||
| 151 | * @return array |
||
| 152 | * |
||
| 153 | * @deprecated 8.2.0 use UserStoragesService::getStorages() |
||
| 154 | */ |
||
| 155 | public static function getPersonalMountPoints() { |
||
| 156 | $mountPoints = []; |
||
| 157 | $service = self::$app->getContainer()->query(UserStoragesService::class); |
||
| 158 | |||
| 159 | foreach ($service->getStorages() as $storage) { |
||
| 160 | $mountPoints[] = self::prepareMountPointEntry($storage, true); |
||
| 161 | } |
||
| 162 | |||
| 163 | return $mountPoints; |
||
| 164 | } |
||
| 165 | |||
| 166 | /** |
||
| 167 | * Convert a StorageConfig to the legacy mountPoints array format |
||
| 168 | * There's a lot of extra information in here, to satisfy all of the legacy functions |
||
| 169 | * |
||
| 170 | * @param StorageConfig $storage |
||
| 171 | * @param bool $isPersonal |
||
| 172 | * @return array |
||
| 173 | */ |
||
| 174 | private static function prepareMountPointEntry(StorageConfig $storage, $isPersonal) { |
||
| 175 | $mountEntry = []; |
||
| 176 | |||
| 177 | $mountEntry['mountpoint'] = substr($storage->getMountPoint(), 1); // remove leading slash |
||
| 178 | $mountEntry['class'] = $storage->getBackend()->getIdentifier(); |
||
| 179 | $mountEntry['backend'] = $storage->getBackend()->getText(); |
||
| 180 | $mountEntry['authMechanism'] = $storage->getAuthMechanism()->getIdentifier(); |
||
| 181 | $mountEntry['personal'] = $isPersonal; |
||
| 182 | $mountEntry['options'] = self::decryptPasswords($storage->getBackendOptions()); |
||
| 183 | $mountEntry['mountOptions'] = $storage->getMountOptions(); |
||
| 184 | $mountEntry['priority'] = $storage->getPriority(); |
||
| 185 | $mountEntry['applicable'] = [ |
||
| 186 | 'groups' => $storage->getApplicableGroups(), |
||
| 187 | 'users' => $storage->getApplicableUsers(), |
||
| 188 | ]; |
||
| 189 | // if mountpoint is applicable to all users the old API expects ['all'] |
||
| 190 | if (empty($mountEntry['applicable']['groups']) && empty($mountEntry['applicable']['users'])) { |
||
| 191 | $mountEntry['applicable']['users'] = ['all']; |
||
| 192 | } |
||
| 193 | |||
| 194 | $mountEntry['id'] = $storage->getId(); |
||
| 195 | |||
| 196 | return $mountEntry; |
||
| 197 | } |
||
| 198 | |||
| 199 | /** |
||
| 200 | * fill in the correct values for $user |
||
| 201 | * |
||
| 202 | * @param string $user user value |
||
| 203 | * @param string|array $input |
||
| 204 | * @return string |
||
| 205 | * @deprecated use self::substitutePlaceholdersInConfig($input) |
||
| 206 | */ |
||
| 207 | public static function setUserVars($user, $input) { |
||
| 208 | $handler = self::$app->getContainer()->query(UserPlaceholderHandler::class); |
||
| 209 | return $handler->handle($input); |
||
| 210 | } |
||
| 211 | |||
| 212 | /** |
||
| 213 | * @param mixed $input |
||
| 214 | * @return mixed |
||
| 215 | * @throws \OCP\AppFramework\QueryException |
||
| 216 | * @since 16.0.0 |
||
| 217 | */ |
||
| 218 | public static function substitutePlaceholdersInConfig($input) { |
||
| 227 | } |
||
| 228 | |||
| 229 | /** |
||
| 230 | * Test connecting using the given backend configuration |
||
| 231 | * |
||
| 232 | * @param string $class backend class name |
||
| 233 | * @param array $options backend configuration options |
||
| 234 | * @param boolean $isPersonal |
||
| 235 | * @return int see self::STATUS_* |
||
| 236 | * @throws Exception |
||
| 237 | */ |
||
| 238 | public static function getBackendStatus($class, $options, $isPersonal, $testOnly = true) { |
||
| 284 | } |
||
| 285 | |||
| 286 | public static function arePlaceholdersSubstituted($option):bool { |
||
| 287 | $result = true; |
||
| 288 | if(is_array($option)) { |
||
| 289 | foreach ($option as $optionItem) { |
||
| 290 | $result = $result && self::arePlaceholdersSubstituted($optionItem); |
||
| 291 | } |
||
| 292 | } else if (is_string($option)) { |
||
| 293 | if (strpos(rtrim($option, '$'), '$') !== false) { |
||
| 294 | $result = false; |
||
| 295 | } |
||
| 296 | } |
||
| 297 | return $result; |
||
| 298 | } |
||
| 299 | |||
| 300 | /** |
||
| 301 | * Read the mount points in the config file into an array |
||
| 302 | * |
||
| 303 | * @param string|null $user If not null, personal for $user, otherwise system |
||
| 304 | * @return array |
||
| 305 | */ |
||
| 306 | public static function readData($user = null) { |
||
| 307 | if (isset($user)) { |
||
| 308 | $jsonFile = \OC::$server->getUserManager()->get($user)->getHome() . '/mount.json'; |
||
| 309 | } else { |
||
| 310 | $config = \OC::$server->getConfig(); |
||
| 311 | $datadir = $config->getSystemValue('datadirectory', \OC::$SERVERROOT . '/data/'); |
||
| 312 | $jsonFile = $config->getSystemValue('mount_file', $datadir . '/mount.json'); |
||
| 313 | } |
||
| 314 | if (is_file($jsonFile)) { |
||
| 315 | $mountPoints = json_decode(file_get_contents($jsonFile), true); |
||
| 316 | if (is_array($mountPoints)) { |
||
| 317 | return $mountPoints; |
||
| 318 | } |
||
| 319 | } |
||
| 320 | return array(); |
||
| 321 | } |
||
| 322 | |||
| 323 | /** |
||
| 324 | * Get backend dependency message |
||
| 325 | * TODO: move into AppFramework along with templates |
||
| 326 | * |
||
| 327 | * @param Backend[] $backends |
||
| 328 | * @return string |
||
| 329 | */ |
||
| 330 | public static function dependencyMessage($backends) { |
||
| 331 | $l = \OC::$server->getL10N('files_external'); |
||
| 332 | $message = ''; |
||
| 333 | $dependencyGroups = []; |
||
| 334 | |||
| 335 | foreach ($backends as $backend) { |
||
| 336 | foreach ($backend->checkDependencies() as $dependency) { |
||
| 337 | if ($message = $dependency->getMessage()) { |
||
| 338 | $message .= '<p>' . $message . '</p>'; |
||
| 339 | } else { |
||
| 340 | $dependencyGroups[$dependency->getDependency()][] = $backend; |
||
| 341 | } |
||
| 342 | } |
||
| 343 | } |
||
| 344 | |||
| 345 | foreach ($dependencyGroups as $module => $dependants) { |
||
| 346 | $backends = implode(', ', array_map(function($backend) { |
||
| 347 | return '"' . $backend->getText() . '"'; |
||
| 348 | }, $dependants)); |
||
| 349 | $message .= '<p>' . OC_Mount_Config::getSingleDependencyMessage($l, $module, $backends) . '</p>'; |
||
| 350 | } |
||
| 351 | |||
| 352 | return $message; |
||
| 353 | } |
||
| 354 | |||
| 355 | /** |
||
| 356 | * Returns a dependency missing message |
||
| 357 | * |
||
| 358 | * @param \OCP\IL10N $l |
||
| 359 | * @param string $module |
||
| 360 | * @param string $backend |
||
| 361 | * @return string |
||
| 362 | */ |
||
| 363 | private static function getSingleDependencyMessage(\OCP\IL10N $l, $module, $backend) { |
||
| 364 | switch (strtolower($module)) { |
||
| 365 | case 'curl': |
||
| 366 | 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]); |
||
| 367 | case 'ftp': |
||
| 368 | 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]); |
||
| 369 | default: |
||
| 370 | 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]); |
||
| 371 | } |
||
| 372 | } |
||
| 373 | |||
| 374 | /** |
||
| 375 | * Encrypt passwords in the given config options |
||
| 376 | * |
||
| 377 | * @param array $options mount options |
||
| 378 | * @return array updated options |
||
| 379 | */ |
||
| 380 | public static function encryptPasswords($options) { |
||
| 388 | } |
||
| 389 | |||
| 390 | /** |
||
| 391 | * Decrypt passwords in the given config options |
||
| 392 | * |
||
| 393 | * @param array $options mount options |
||
| 394 | * @return array updated options |
||
| 395 | */ |
||
| 396 | public static function decryptPasswords($options) { |
||
| 397 | // note: legacy options might still have the unencrypted password in the "password" field |
||
| 398 | if (isset($options['password_encrypted'])) { |
||
| 399 | $options['password'] = self::decryptPassword($options['password_encrypted']); |
||
| 400 | unset($options['password_encrypted']); |
||
| 401 | } |
||
| 402 | return $options; |
||
| 403 | } |
||
| 404 | |||
| 405 | /** |
||
| 406 | * Encrypt a single password |
||
| 407 | * |
||
| 408 | * @param string $password plain text password |
||
| 409 | * @return string encrypted password |
||
| 410 | */ |
||
| 411 | private static function encryptPassword($password) { |
||
| 416 | } |
||
| 417 | |||
| 418 | /** |
||
| 419 | * Decrypts a single password |
||
| 420 | * |
||
| 421 | * @param string $encryptedPassword encrypted password |
||
| 422 | * @return string plain text password |
||
| 423 | */ |
||
| 424 | private static function decryptPassword($encryptedPassword) { |
||
| 425 | $cipher = self::getCipher(); |
||
| 426 | $binaryPassword = base64_decode($encryptedPassword); |
||
| 427 | $iv = substr($binaryPassword, 0, 16); |
||
| 428 | $cipher->setIV($iv); |
||
| 429 | $binaryPassword = substr($binaryPassword, 16); |
||
| 430 | return $cipher->decrypt($binaryPassword); |
||
| 431 | } |
||
| 432 | |||
| 433 | /** |
||
| 434 | * Returns the encryption cipher |
||
| 435 | * |
||
| 436 | * @return AES |
||
| 437 | */ |
||
| 438 | private static function getCipher() { |
||
| 442 | } |
||
| 443 | |||
| 444 | /** |
||
| 445 | * Computes a hash based on the given configuration. |
||
| 446 | * This is mostly used to find out whether configurations |
||
| 447 | * are the same. |
||
| 448 | * |
||
| 449 | * @param array $config |
||
| 450 | * @return string |
||
| 451 | */ |
||
| 452 | public static function makeConfigHash($config) { |
||
| 464 | } |
||
| 465 | } |
||
| 466 |
This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.
If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.