Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like OC 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. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
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, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 61 | class OC { |
||
| 62 | /** |
||
| 63 | * Associative array for autoloading. classname => filename |
||
| 64 | */ |
||
| 65 | public static $CLASSPATH = array(); |
||
| 66 | /** |
||
| 67 | * The installation path for Nextcloud on the server (e.g. /srv/http/nextcloud) |
||
| 68 | */ |
||
| 69 | public static $SERVERROOT = ''; |
||
| 70 | /** |
||
| 71 | * the current request path relative to the Nextcloud root (e.g. files/index.php) |
||
| 72 | */ |
||
| 73 | private static $SUBURI = ''; |
||
| 74 | /** |
||
| 75 | * the Nextcloud root path for http requests (e.g. nextcloud/) |
||
| 76 | */ |
||
| 77 | public static $WEBROOT = ''; |
||
| 78 | /** |
||
| 79 | * The installation path array of the apps folder on the server (e.g. /srv/http/nextcloud) 'path' and |
||
| 80 | * web path in 'url' |
||
| 81 | */ |
||
| 82 | public static $APPSROOTS = array(); |
||
| 83 | |||
| 84 | /** |
||
| 85 | * @var string |
||
| 86 | */ |
||
| 87 | public static $configDir; |
||
| 88 | |||
| 89 | /** |
||
| 90 | * requested app |
||
| 91 | */ |
||
| 92 | public static $REQUESTEDAPP = ''; |
||
| 93 | |||
| 94 | /** |
||
| 95 | * check if Nextcloud runs in cli mode |
||
| 96 | */ |
||
| 97 | public static $CLI = false; |
||
| 98 | |||
| 99 | /** |
||
| 100 | * @var \OC\Autoloader $loader |
||
| 101 | */ |
||
| 102 | public static $loader = null; |
||
| 103 | |||
| 104 | /** @var \Composer\Autoload\ClassLoader $composerAutoloader */ |
||
| 105 | public static $composerAutoloader = null; |
||
| 106 | |||
| 107 | /** |
||
| 108 | * @var \OC\Server |
||
| 109 | */ |
||
| 110 | public static $server = null; |
||
| 111 | |||
| 112 | /** |
||
| 113 | * @var \OC\Config |
||
| 114 | */ |
||
| 115 | private static $config = null; |
||
| 116 | |||
| 117 | /** |
||
| 118 | * @throws \RuntimeException when the 3rdparty directory is missing or |
||
| 119 | * the app path list is empty or contains an invalid path |
||
| 120 | */ |
||
| 121 | public static function initPaths() { |
||
| 122 | if(defined('PHPUNIT_CONFIG_DIR')) { |
||
| 123 | self::$configDir = OC::$SERVERROOT . '/' . PHPUNIT_CONFIG_DIR . '/'; |
||
| 124 | } elseif(defined('PHPUNIT_RUN') and PHPUNIT_RUN and is_dir(OC::$SERVERROOT . '/tests/config/')) { |
||
| 125 | self::$configDir = OC::$SERVERROOT . '/tests/config/'; |
||
| 126 | } elseif($dir = getenv('NEXTCLOUD_CONFIG_DIR')) { |
||
| 127 | self::$configDir = rtrim($dir, '/') . '/'; |
||
| 128 | } else { |
||
| 129 | self::$configDir = OC::$SERVERROOT . '/config/'; |
||
| 130 | } |
||
| 131 | self::$config = new \OC\Config(self::$configDir); |
||
| 132 | |||
| 133 | OC::$SUBURI = str_replace("\\", "/", substr(realpath($_SERVER["SCRIPT_FILENAME"]), strlen(OC::$SERVERROOT))); |
||
| 134 | /** |
||
| 135 | * FIXME: The following lines are required because we can't yet instantiate |
||
| 136 | * \OC::$server->getRequest() since \OC::$server does not yet exist. |
||
| 137 | */ |
||
| 138 | $params = [ |
||
| 139 | 'server' => [ |
||
| 140 | 'SCRIPT_NAME' => $_SERVER['SCRIPT_NAME'], |
||
| 141 | 'SCRIPT_FILENAME' => $_SERVER['SCRIPT_FILENAME'], |
||
| 142 | ], |
||
| 143 | ]; |
||
| 144 | $fakeRequest = new \OC\AppFramework\Http\Request($params, null, new \OC\AllConfig(new \OC\SystemConfig(self::$config))); |
||
| 145 | $scriptName = $fakeRequest->getScriptName(); |
||
| 146 | if (substr($scriptName, -1) == '/') { |
||
| 147 | $scriptName .= 'index.php'; |
||
| 148 | //make sure suburi follows the same rules as scriptName |
||
| 149 | if (substr(OC::$SUBURI, -9) != 'index.php') { |
||
| 150 | if (substr(OC::$SUBURI, -1) != '/') { |
||
| 151 | OC::$SUBURI = OC::$SUBURI . '/'; |
||
| 152 | } |
||
| 153 | OC::$SUBURI = OC::$SUBURI . 'index.php'; |
||
| 154 | } |
||
| 155 | } |
||
| 156 | |||
| 157 | |||
| 158 | if (OC::$CLI) { |
||
| 159 | OC::$WEBROOT = self::$config->getValue('overwritewebroot', ''); |
||
| 160 | } else { |
||
| 161 | if (substr($scriptName, 0 - strlen(OC::$SUBURI)) === OC::$SUBURI) { |
||
| 162 | OC::$WEBROOT = substr($scriptName, 0, 0 - strlen(OC::$SUBURI)); |
||
| 163 | |||
| 164 | if (OC::$WEBROOT != '' && OC::$WEBROOT[0] !== '/') { |
||
| 165 | OC::$WEBROOT = '/' . OC::$WEBROOT; |
||
| 166 | } |
||
| 167 | } else { |
||
| 168 | // The scriptName is not ending with OC::$SUBURI |
||
| 169 | // This most likely means that we are calling from CLI. |
||
| 170 | // However some cron jobs still need to generate |
||
| 171 | // a web URL, so we use overwritewebroot as a fallback. |
||
| 172 | OC::$WEBROOT = self::$config->getValue('overwritewebroot', ''); |
||
| 173 | } |
||
| 174 | |||
| 175 | // Resolve /nextcloud to /nextcloud/ to ensure to always have a trailing |
||
| 176 | // slash which is required by URL generation. |
||
| 177 | if (isset($_SERVER['REQUEST_URI']) && $_SERVER['REQUEST_URI'] === \OC::$WEBROOT && |
||
| 178 | substr($_SERVER['REQUEST_URI'], -1) !== '/') { |
||
| 179 | header('Location: '.\OC::$WEBROOT.'/'); |
||
| 180 | exit(); |
||
| 181 | } |
||
| 182 | } |
||
| 183 | |||
| 184 | // search the apps folder |
||
| 185 | $config_paths = self::$config->getValue('apps_paths', array()); |
||
| 186 | if (!empty($config_paths)) { |
||
| 187 | foreach ($config_paths as $paths) { |
||
| 188 | if (isset($paths['url']) && isset($paths['path'])) { |
||
| 189 | $paths['url'] = rtrim($paths['url'], '/'); |
||
| 190 | $paths['path'] = rtrim($paths['path'], '/'); |
||
| 191 | OC::$APPSROOTS[] = $paths; |
||
| 192 | } |
||
| 193 | } |
||
| 194 | } elseif (file_exists(OC::$SERVERROOT . '/apps')) { |
||
| 195 | OC::$APPSROOTS[] = array('path' => OC::$SERVERROOT . '/apps', 'url' => '/apps', 'writable' => true); |
||
| 196 | } elseif (file_exists(OC::$SERVERROOT . '/../apps')) { |
||
| 197 | OC::$APPSROOTS[] = array( |
||
| 198 | 'path' => rtrim(dirname(OC::$SERVERROOT), '/') . '/apps', |
||
| 199 | 'url' => '/apps', |
||
| 200 | 'writable' => true |
||
| 201 | ); |
||
| 202 | } |
||
| 203 | |||
| 204 | if (empty(OC::$APPSROOTS)) { |
||
| 205 | throw new \RuntimeException('apps directory not found! Please put the Nextcloud apps folder in the Nextcloud folder' |
||
| 206 | . ' or the folder above. You can also configure the location in the config.php file.'); |
||
| 207 | } |
||
| 208 | $paths = array(); |
||
| 209 | foreach (OC::$APPSROOTS as $path) { |
||
| 210 | $paths[] = $path['path']; |
||
| 211 | if (!is_dir($path['path'])) { |
||
| 212 | throw new \RuntimeException(sprintf('App directory "%s" not found! Please put the Nextcloud apps folder in the' |
||
| 213 | . ' Nextcloud folder or the folder above. You can also configure the location in the' |
||
| 214 | . ' config.php file.', $path['path'])); |
||
| 215 | } |
||
| 216 | } |
||
| 217 | |||
| 218 | // set the right include path |
||
| 219 | set_include_path( |
||
| 220 | implode(PATH_SEPARATOR, $paths) |
||
| 221 | ); |
||
| 222 | } |
||
| 223 | |||
| 224 | public static function checkConfig() { |
||
| 225 | $l = \OC::$server->getL10N('lib'); |
||
| 226 | |||
| 227 | // Create config if it does not already exist |
||
| 228 | $configFilePath = self::$configDir .'/config.php'; |
||
| 229 | if(!file_exists($configFilePath)) { |
||
| 230 | @touch($configFilePath); |
||
|
|
|||
| 231 | } |
||
| 232 | |||
| 233 | // Check if config is writable |
||
| 234 | $configFileWritable = is_writable($configFilePath); |
||
| 235 | if (!$configFileWritable && !OC_Helper::isReadOnlyConfigEnabled() |
||
| 236 | || !$configFileWritable && self::checkUpgrade(false)) { |
||
| 237 | |||
| 238 | $urlGenerator = \OC::$server->getURLGenerator(); |
||
| 239 | |||
| 240 | if (self::$CLI) { |
||
| 241 | echo $l->t('Cannot write into "config" directory!')."\n"; |
||
| 242 | echo $l->t('This can usually be fixed by giving the webserver write access to the config directory')."\n"; |
||
| 243 | echo "\n"; |
||
| 244 | echo $l->t('See %s', [ $urlGenerator->linkToDocs('admin-dir_permissions') ])."\n"; |
||
| 245 | exit; |
||
| 246 | } else { |
||
| 247 | OC_Template::printErrorPage( |
||
| 248 | $l->t('Cannot write into "config" directory!'), |
||
| 249 | $l->t('This can usually be fixed by giving the webserver write access to the config directory. See %s', |
||
| 250 | [ $urlGenerator->linkToDocs('admin-dir_permissions') ]) |
||
| 251 | ); |
||
| 252 | } |
||
| 253 | } |
||
| 254 | } |
||
| 255 | |||
| 256 | public static function checkInstalled() { |
||
| 257 | if (defined('OC_CONSOLE')) { |
||
| 258 | return; |
||
| 259 | } |
||
| 260 | // Redirect to installer if not installed |
||
| 261 | if (!\OC::$server->getSystemConfig()->getValue('installed', false) && OC::$SUBURI !== '/index.php' && OC::$SUBURI !== '/status.php') { |
||
| 262 | if (OC::$CLI) { |
||
| 263 | throw new Exception('Not installed'); |
||
| 264 | } else { |
||
| 265 | $url = OC::$WEBROOT . '/index.php'; |
||
| 266 | header('Location: ' . $url); |
||
| 267 | } |
||
| 268 | exit(); |
||
| 269 | } |
||
| 270 | } |
||
| 271 | |||
| 272 | public static function checkMaintenanceMode() { |
||
| 273 | // Allow ajax update script to execute without being stopped |
||
| 274 | if (\OC::$server->getSystemConfig()->getValue('maintenance', false) && OC::$SUBURI != '/core/ajax/update.php') { |
||
| 275 | // send http status 503 |
||
| 276 | header('HTTP/1.1 503 Service Temporarily Unavailable'); |
||
| 277 | header('Status: 503 Service Temporarily Unavailable'); |
||
| 278 | header('Retry-After: 120'); |
||
| 279 | |||
| 280 | // render error page |
||
| 281 | $template = new OC_Template('', 'update.user', 'guest'); |
||
| 282 | OC_Util::addScript('maintenance-check'); |
||
| 283 | $template->printPage(); |
||
| 284 | die(); |
||
| 285 | } |
||
| 286 | } |
||
| 287 | |||
| 288 | /** |
||
| 289 | * Checks if the version requires an update and shows |
||
| 290 | * @param bool $showTemplate Whether an update screen should get shown |
||
| 291 | * @return bool|void |
||
| 292 | */ |
||
| 293 | public static function checkUpgrade($showTemplate = true) { |
||
| 294 | if (\OCP\Util::needUpgrade()) { |
||
| 295 | if (function_exists('opcache_reset')) { |
||
| 296 | opcache_reset(); |
||
| 297 | } |
||
| 298 | $systemConfig = \OC::$server->getSystemConfig(); |
||
| 299 | if ($showTemplate && !$systemConfig->getValue('maintenance', false)) { |
||
| 300 | self::printUpgradePage(); |
||
| 301 | exit(); |
||
| 302 | } else { |
||
| 303 | return true; |
||
| 304 | } |
||
| 305 | } |
||
| 306 | return false; |
||
| 307 | } |
||
| 308 | |||
| 309 | /** |
||
| 310 | * Prints the upgrade page |
||
| 311 | */ |
||
| 312 | private static function printUpgradePage() { |
||
| 313 | $systemConfig = \OC::$server->getSystemConfig(); |
||
| 314 | |||
| 315 | $disableWebUpdater = $systemConfig->getValue('upgrade.disable-web', false); |
||
| 316 | $tooBig = false; |
||
| 317 | if (!$disableWebUpdater) { |
||
| 318 | $apps = \OC::$server->getAppManager(); |
||
| 319 | $tooBig = false; |
||
| 320 | View Code Duplication | if ($apps->isInstalled('user_ldap')) { |
|
| 321 | $qb = \OC::$server->getDatabaseConnection()->getQueryBuilder(); |
||
| 322 | |||
| 323 | $result = $qb->selectAlias($qb->createFunction('COUNT(*)'), 'user_count') |
||
| 324 | ->from('ldap_user_mapping') |
||
| 325 | ->execute(); |
||
| 326 | $row = $result->fetch(); |
||
| 327 | $result->closeCursor(); |
||
| 328 | |||
| 329 | $tooBig = ($row['user_count'] > 50); |
||
| 330 | } |
||
| 331 | View Code Duplication | if (!$tooBig && $apps->isInstalled('user_saml')) { |
|
| 332 | $qb = \OC::$server->getDatabaseConnection()->getQueryBuilder(); |
||
| 333 | |||
| 334 | $result = $qb->selectAlias($qb->createFunction('COUNT(*)'), 'user_count') |
||
| 335 | ->from('user_saml_users') |
||
| 336 | ->execute(); |
||
| 337 | $row = $result->fetch(); |
||
| 338 | $result->closeCursor(); |
||
| 339 | |||
| 340 | $tooBig = ($row['user_count'] > 50); |
||
| 341 | } |
||
| 342 | if (!$tooBig) { |
||
| 343 | // count users |
||
| 344 | $stats = \OC::$server->getUserManager()->countUsers(); |
||
| 345 | $totalUsers = array_sum($stats); |
||
| 346 | $tooBig = ($totalUsers > 50); |
||
| 347 | } |
||
| 348 | } |
||
| 349 | $ignoreTooBigWarning = isset($_GET['IKnowThatThisIsABigInstanceAndTheUpdateRequestCouldRunIntoATimeoutAndHowToRestoreABackup']) && |
||
| 350 | $_GET['IKnowThatThisIsABigInstanceAndTheUpdateRequestCouldRunIntoATimeoutAndHowToRestoreABackup'] === 'IAmSuperSureToDoThis'; |
||
| 351 | |||
| 352 | if ($disableWebUpdater || ($tooBig && !$ignoreTooBigWarning)) { |
||
| 353 | // send http status 503 |
||
| 354 | header('HTTP/1.1 503 Service Temporarily Unavailable'); |
||
| 355 | header('Status: 503 Service Temporarily Unavailable'); |
||
| 356 | header('Retry-After: 120'); |
||
| 357 | |||
| 358 | // render error page |
||
| 359 | $template = new OC_Template('', 'update.use-cli', 'guest'); |
||
| 360 | $template->assign('productName', 'nextcloud'); // for now |
||
| 361 | $template->assign('version', OC_Util::getVersionString()); |
||
| 362 | $template->assign('tooBig', $tooBig); |
||
| 363 | |||
| 364 | $template->printPage(); |
||
| 365 | die(); |
||
| 366 | } |
||
| 367 | |||
| 368 | // check whether this is a core update or apps update |
||
| 369 | $installedVersion = $systemConfig->getValue('version', '0.0.0'); |
||
| 370 | $currentVersion = implode('.', \OCP\Util::getVersion()); |
||
| 371 | |||
| 372 | // if not a core upgrade, then it's apps upgrade |
||
| 373 | $isAppsOnlyUpgrade = (version_compare($currentVersion, $installedVersion, '=')); |
||
| 374 | |||
| 375 | $oldTheme = $systemConfig->getValue('theme'); |
||
| 376 | $systemConfig->setValue('theme', ''); |
||
| 377 | OC_Util::addScript('config'); // needed for web root |
||
| 378 | OC_Util::addScript('update'); |
||
| 379 | |||
| 380 | /** @var \OC\App\AppManager $appManager */ |
||
| 381 | $appManager = \OC::$server->getAppManager(); |
||
| 382 | |||
| 383 | $tmpl = new OC_Template('', 'update.admin', 'guest'); |
||
| 384 | $tmpl->assign('version', OC_Util::getVersionString()); |
||
| 385 | $tmpl->assign('isAppsOnlyUpgrade', $isAppsOnlyUpgrade); |
||
| 386 | |||
| 387 | // get third party apps |
||
| 388 | $ocVersion = \OCP\Util::getVersion(); |
||
| 389 | $incompatibleApps = $appManager->getIncompatibleApps($ocVersion); |
||
| 390 | $incompatibleShippedApps = []; |
||
| 391 | foreach ($incompatibleApps as $appInfo) { |
||
| 392 | if ($appManager->isShipped($appInfo['id'])) { |
||
| 393 | $incompatibleShippedApps[] = $appInfo['name'] . ' (' . $appInfo['id'] . ')'; |
||
| 394 | } |
||
| 395 | } |
||
| 396 | |||
| 397 | if (!empty($incompatibleShippedApps)) { |
||
| 398 | $l = \OC::$server->getL10N('core'); |
||
| 399 | $hint = $l->t('The files of the app %$1s were not replaced correctly. Make sure it is a version compatible with the server.', [implode(', ', $incompatibleShippedApps)]); |
||
| 400 | throw new \OC\HintException('The files of the app ' . implode(', ', $incompatibleShippedApps) . ' were not replaced correctly. Make sure it is a version compatible with the server.', $hint); |
||
| 401 | } |
||
| 402 | |||
| 403 | $tmpl->assign('appsToUpgrade', $appManager->getAppsNeedingUpgrade($ocVersion)); |
||
| 404 | $tmpl->assign('incompatibleAppsList', $incompatibleApps); |
||
| 405 | $tmpl->assign('productName', 'Nextcloud'); // for now |
||
| 406 | $tmpl->assign('oldTheme', $oldTheme); |
||
| 407 | $tmpl->printPage(); |
||
| 408 | } |
||
| 409 | |||
| 410 | public static function initSession() { |
||
| 456 | |||
| 457 | /** |
||
| 458 | * @return string |
||
| 459 | */ |
||
| 460 | private static function getSessionLifeTime() { |
||
| 463 | |||
| 464 | public static function loadAppClassPaths() { |
||
| 477 | |||
| 478 | /** |
||
| 479 | * Try to set some values to the required Nextcloud default |
||
| 480 | */ |
||
| 481 | public static function setRequiredIniValues() { |
||
| 485 | |||
| 486 | /** |
||
| 487 | * Send the same site cookies |
||
| 488 | */ |
||
| 489 | private static function sendSameSiteCookies() { |
||
| 516 | |||
| 517 | /** |
||
| 518 | * Same Site cookie to further mitigate CSRF attacks. This cookie has to |
||
| 519 | * be set in every request if cookies are sent to add a second level of |
||
| 520 | * defense against CSRF. |
||
| 521 | * |
||
| 522 | * If the cookie is not sent this will set the cookie and reload the page. |
||
| 523 | * We use an additional cookie since we want to protect logout CSRF and |
||
| 524 | * also we can't directly interfere with PHP's session mechanism. |
||
| 525 | */ |
||
| 526 | private static function performSameSiteCookieProtection() { |
||
| 584 | |||
| 585 | public static function init() { |
||
| 819 | |||
| 820 | /** |
||
| 821 | * register hooks for the cache |
||
| 822 | */ |
||
| 823 | public static function registerCacheHooks() { |
||
| 846 | |||
| 847 | public static function registerSettingsHooks() { |
||
| 862 | |||
| 863 | private static function registerEncryptionWrapper() { |
||
| 867 | |||
| 868 | private static function registerEncryptionHooks() { |
||
| 877 | |||
| 878 | private static function registerAccountHooks() { |
||
| 882 | |||
| 883 | /** |
||
| 884 | * register hooks for the cache |
||
| 885 | */ |
||
| 886 | public static function registerLogRotate() { |
||
| 894 | |||
| 895 | /** |
||
| 896 | * register hooks for the filesystem |
||
| 897 | */ |
||
| 898 | public static function registerFilesystemHooks() { |
||
| 903 | |||
| 904 | /** |
||
| 905 | * register hooks for sharing |
||
| 906 | */ |
||
| 907 | public static function registerShareHooks() { |
||
| 914 | |||
| 915 | protected static function registerAutoloaderCache() { |
||
| 931 | |||
| 932 | /** |
||
| 933 | * Handle the request |
||
| 934 | */ |
||
| 935 | public static function handleRequest() { |
||
| 1036 | |||
| 1037 | /** |
||
| 1038 | * Check login: apache auth, auth token, basic auth |
||
| 1039 | * |
||
| 1040 | * @param OCP\IRequest $request |
||
| 1041 | * @return boolean |
||
| 1042 | */ |
||
| 1043 | static function handleLogin(OCP\IRequest $request) { |
||
| 1062 | |||
| 1063 | protected static function handleAuthHeaders() { |
||
| 1083 | } |
||
| 1084 | |||
| 1086 |
If you suppress an error, we recommend checking for the error condition explicitly: