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 |
||
| 65 | class OC { |
||
| 66 | /** |
||
| 67 | * Associative array for autoloading. classname => filename |
||
| 68 | */ |
||
| 69 | public static $CLASSPATH = array(); |
||
| 70 | /** |
||
| 71 | * The installation path for Nextcloud on the server (e.g. /srv/http/nextcloud) |
||
| 72 | */ |
||
| 73 | public static $SERVERROOT = ''; |
||
| 74 | /** |
||
| 75 | * the current request path relative to the Nextcloud root (e.g. files/index.php) |
||
| 76 | */ |
||
| 77 | private static $SUBURI = ''; |
||
| 78 | /** |
||
| 79 | * the Nextcloud root path for http requests (e.g. nextcloud/) |
||
| 80 | */ |
||
| 81 | public static $WEBROOT = ''; |
||
| 82 | /** |
||
| 83 | * The installation path of the 3rdparty folder on the server (e.g. /srv/http/nextcloud/3rdparty) |
||
| 84 | */ |
||
| 85 | public static $THIRDPARTYROOT = ''; |
||
| 86 | /** |
||
| 87 | * the root path of the 3rdparty folder for http requests (e.g. nextcloud/3rdparty) |
||
| 88 | */ |
||
| 89 | public static $THIRDPARTYWEBROOT = ''; |
||
| 90 | /** |
||
| 91 | * The installation path array of the apps folder on the server (e.g. /srv/http/nextcloud) 'path' and |
||
| 92 | * web path in 'url' |
||
| 93 | */ |
||
| 94 | public static $APPSROOTS = array(); |
||
| 95 | |||
| 96 | public static $configDir; |
||
| 97 | |||
| 98 | /** |
||
| 99 | * requested app |
||
| 100 | */ |
||
| 101 | public static $REQUESTEDAPP = ''; |
||
| 102 | |||
| 103 | /** |
||
| 104 | * check if Nextcloud runs in cli mode |
||
| 105 | */ |
||
| 106 | public static $CLI = false; |
||
| 107 | |||
| 108 | /** |
||
| 109 | * @var \OC\Autoloader $loader |
||
| 110 | */ |
||
| 111 | public static $loader = null; |
||
| 112 | |||
| 113 | /** |
||
| 114 | * @var \OC\Server |
||
| 115 | */ |
||
| 116 | public static $server = null; |
||
| 117 | |||
| 118 | /** |
||
| 119 | * @var \OC\Config |
||
| 120 | */ |
||
| 121 | private static $config = null; |
||
| 122 | |||
| 123 | /** |
||
| 124 | * @throws \RuntimeException when the 3rdparty directory is missing or |
||
| 125 | * the app path list is empty or contains an invalid path |
||
| 126 | */ |
||
| 127 | public static function initPaths() { |
||
| 128 | if(defined('PHPUNIT_CONFIG_DIR')) { |
||
| 129 | self::$configDir = OC::$SERVERROOT . '/' . PHPUNIT_CONFIG_DIR . '/'; |
||
| 130 | } elseif(defined('PHPUNIT_RUN') and PHPUNIT_RUN and is_dir(OC::$SERVERROOT . '/tests/config/')) { |
||
| 131 | self::$configDir = OC::$SERVERROOT . '/tests/config/'; |
||
| 132 | } else { |
||
| 133 | self::$configDir = OC::$SERVERROOT . '/config/'; |
||
| 134 | } |
||
| 135 | self::$config = new \OC\Config(self::$configDir); |
||
| 136 | |||
| 137 | OC::$SUBURI = str_replace("\\", "/", substr(realpath($_SERVER["SCRIPT_FILENAME"]), strlen(OC::$SERVERROOT))); |
||
| 138 | /** |
||
| 139 | * FIXME: The following lines are required because we can't yet instantiiate |
||
| 140 | * \OC::$server->getRequest() since \OC::$server does not yet exist. |
||
| 141 | */ |
||
| 142 | $params = [ |
||
| 143 | 'server' => [ |
||
| 144 | 'SCRIPT_NAME' => $_SERVER['SCRIPT_NAME'], |
||
| 145 | 'SCRIPT_FILENAME' => $_SERVER['SCRIPT_FILENAME'], |
||
| 146 | ], |
||
| 147 | ]; |
||
| 148 | $fakeRequest = new \OC\AppFramework\Http\Request($params, null, new \OC\AllConfig(new \OC\SystemConfig(self::$config))); |
||
| 149 | $scriptName = $fakeRequest->getScriptName(); |
||
| 150 | if (substr($scriptName, -1) == '/') { |
||
| 151 | $scriptName .= 'index.php'; |
||
| 152 | //make sure suburi follows the same rules as scriptName |
||
| 153 | if (substr(OC::$SUBURI, -9) != 'index.php') { |
||
| 154 | if (substr(OC::$SUBURI, -1) != '/') { |
||
| 155 | OC::$SUBURI = OC::$SUBURI . '/'; |
||
| 156 | } |
||
| 157 | OC::$SUBURI = OC::$SUBURI . 'index.php'; |
||
| 158 | } |
||
| 159 | } |
||
| 160 | |||
| 161 | |||
| 162 | if (OC::$CLI) { |
||
| 163 | OC::$WEBROOT = self::$config->getValue('overwritewebroot', ''); |
||
| 164 | } else { |
||
| 165 | if (substr($scriptName, 0 - strlen(OC::$SUBURI)) === OC::$SUBURI) { |
||
| 166 | OC::$WEBROOT = substr($scriptName, 0, 0 - strlen(OC::$SUBURI)); |
||
| 167 | |||
| 168 | if (OC::$WEBROOT != '' && OC::$WEBROOT[0] !== '/') { |
||
| 169 | OC::$WEBROOT = '/' . OC::$WEBROOT; |
||
| 170 | } |
||
| 171 | } else { |
||
| 172 | // The scriptName is not ending with OC::$SUBURI |
||
| 173 | // This most likely means that we are calling from CLI. |
||
| 174 | // However some cron jobs still need to generate |
||
| 175 | // a web URL, so we use overwritewebroot as a fallback. |
||
| 176 | OC::$WEBROOT = self::$config->getValue('overwritewebroot', ''); |
||
| 177 | } |
||
| 178 | |||
| 179 | // Resolve /nextcloud to /nextcloud/ to ensure to always have a trailing |
||
| 180 | // slash which is required by URL generation. |
||
| 181 | if($_SERVER['REQUEST_URI'] === \OC::$WEBROOT && |
||
| 182 | substr($_SERVER['REQUEST_URI'], -1) !== '/') { |
||
| 183 | header('Location: '.\OC::$WEBROOT.'/'); |
||
| 184 | exit(); |
||
| 185 | } |
||
| 186 | } |
||
| 187 | |||
| 188 | // search the 3rdparty folder |
||
| 189 | OC::$THIRDPARTYROOT = self::$config->getValue('3rdpartyroot', null); |
||
| 190 | OC::$THIRDPARTYWEBROOT = self::$config->getValue('3rdpartyurl', null); |
||
| 191 | |||
| 192 | if (empty(OC::$THIRDPARTYROOT) && empty(OC::$THIRDPARTYWEBROOT)) { |
||
| 193 | if (file_exists(OC::$SERVERROOT . '/3rdparty')) { |
||
| 194 | OC::$THIRDPARTYROOT = OC::$SERVERROOT; |
||
| 195 | OC::$THIRDPARTYWEBROOT = OC::$WEBROOT; |
||
| 196 | } elseif (file_exists(OC::$SERVERROOT . '/../3rdparty')) { |
||
| 197 | OC::$THIRDPARTYWEBROOT = rtrim(dirname(OC::$WEBROOT), '/'); |
||
| 198 | OC::$THIRDPARTYROOT = rtrim(dirname(OC::$SERVERROOT), '/'); |
||
| 199 | } |
||
| 200 | } |
||
| 201 | if (empty(OC::$THIRDPARTYROOT) || !file_exists(OC::$THIRDPARTYROOT)) { |
||
| 202 | throw new \RuntimeException('3rdparty directory not found! Please put the Nextcloud 3rdparty' |
||
| 203 | . ' folder in the Nextcloud folder or the folder above.' |
||
| 204 | . ' You can also configure the location in the config.php file.'); |
||
| 205 | } |
||
| 206 | |||
| 207 | // search the apps folder |
||
| 208 | $config_paths = self::$config->getValue('apps_paths', array()); |
||
| 209 | if (!empty($config_paths)) { |
||
| 210 | foreach ($config_paths as $paths) { |
||
| 211 | if (isset($paths['url']) && isset($paths['path'])) { |
||
| 212 | $paths['url'] = rtrim($paths['url'], '/'); |
||
| 213 | $paths['path'] = rtrim($paths['path'], '/'); |
||
| 214 | OC::$APPSROOTS[] = $paths; |
||
| 215 | } |
||
| 216 | } |
||
| 217 | } elseif (file_exists(OC::$SERVERROOT . '/apps')) { |
||
| 218 | OC::$APPSROOTS[] = array('path' => OC::$SERVERROOT . '/apps', 'url' => '/apps', 'writable' => true); |
||
| 219 | } elseif (file_exists(OC::$SERVERROOT . '/../apps')) { |
||
| 220 | OC::$APPSROOTS[] = array( |
||
| 221 | 'path' => rtrim(dirname(OC::$SERVERROOT), '/') . '/apps', |
||
| 222 | 'url' => '/apps', |
||
| 223 | 'writable' => true |
||
| 224 | ); |
||
| 225 | } |
||
| 226 | |||
| 227 | if (empty(OC::$APPSROOTS)) { |
||
| 228 | throw new \RuntimeException('apps directory not found! Please put the Nextcloud apps folder in the Nextcloud folder' |
||
| 229 | . ' or the folder above. You can also configure the location in the config.php file.'); |
||
| 230 | } |
||
| 231 | $paths = array(); |
||
| 232 | foreach (OC::$APPSROOTS as $path) { |
||
| 233 | $paths[] = $path['path']; |
||
| 234 | if (!is_dir($path['path'])) { |
||
| 235 | throw new \RuntimeException(sprintf('App directory "%s" not found! Please put the Nextcloud apps folder in the' |
||
| 236 | . ' Nextcloud folder or the folder above. You can also configure the location in the' |
||
| 237 | . ' config.php file.', $path['path'])); |
||
| 238 | } |
||
| 239 | } |
||
| 240 | |||
| 241 | // set the right include path |
||
| 242 | set_include_path( |
||
| 243 | OC::$SERVERROOT . '/lib/private' . PATH_SEPARATOR . |
||
| 244 | OC::$SERVERROOT . '/config' . PATH_SEPARATOR . |
||
| 245 | OC::$THIRDPARTYROOT . '/3rdparty' . PATH_SEPARATOR . |
||
| 246 | implode(PATH_SEPARATOR, $paths) . PATH_SEPARATOR . |
||
| 247 | get_include_path() . PATH_SEPARATOR . |
||
| 248 | OC::$SERVERROOT |
||
| 249 | ); |
||
| 250 | } |
||
| 251 | |||
| 252 | public static function checkConfig() { |
||
| 253 | $l = \OC::$server->getL10N('lib'); |
||
| 254 | |||
| 255 | // Create config if it does not already exist |
||
| 256 | $configFilePath = self::$configDir .'/config.php'; |
||
| 257 | if(!file_exists($configFilePath)) { |
||
| 258 | @touch($configFilePath); |
||
|
|
|||
| 259 | } |
||
| 260 | |||
| 261 | // Check if config is writable |
||
| 262 | $configFileWritable = is_writable($configFilePath); |
||
| 263 | if (!$configFileWritable && !OC_Helper::isReadOnlyConfigEnabled() |
||
| 264 | || !$configFileWritable && self::checkUpgrade(false)) { |
||
| 265 | |||
| 266 | $urlGenerator = \OC::$server->getURLGenerator(); |
||
| 267 | |||
| 268 | if (self::$CLI) { |
||
| 269 | echo $l->t('Cannot write into "config" directory!')."\n"; |
||
| 270 | echo $l->t('This can usually be fixed by giving the webserver write access to the config directory')."\n"; |
||
| 271 | echo "\n"; |
||
| 272 | echo $l->t('See %s', [ $urlGenerator->linkToDocs('admin-dir_permissions') ])."\n"; |
||
| 273 | exit; |
||
| 274 | } else { |
||
| 275 | OC_Template::printErrorPage( |
||
| 276 | $l->t('Cannot write into "config" directory!'), |
||
| 277 | $l->t('This can usually be fixed by ' |
||
| 278 | . '%sgiving the webserver write access to the config directory%s.', |
||
| 279 | array('<a href="' . $urlGenerator->linkToDocs('admin-dir_permissions') . '" target="_blank">', '</a>')) |
||
| 280 | ); |
||
| 281 | } |
||
| 282 | } |
||
| 283 | } |
||
| 284 | |||
| 285 | public static function checkInstalled() { |
||
| 286 | if (defined('OC_CONSOLE')) { |
||
| 287 | return; |
||
| 288 | } |
||
| 289 | // Redirect to installer if not installed |
||
| 290 | if (!\OC::$server->getSystemConfig()->getValue('installed', false) && OC::$SUBURI !== '/index.php' && OC::$SUBURI !== '/status.php') { |
||
| 291 | if (OC::$CLI) { |
||
| 292 | throw new Exception('Not installed'); |
||
| 293 | } else { |
||
| 294 | $url = 'http://' . $_SERVER['SERVER_NAME'] . OC::$WEBROOT . '/index.php'; |
||
| 295 | header('Location: ' . $url); |
||
| 296 | } |
||
| 297 | exit(); |
||
| 298 | } |
||
| 299 | } |
||
| 300 | |||
| 301 | public static function checkMaintenanceMode() { |
||
| 302 | // Allow ajax update script to execute without being stopped |
||
| 303 | if (\OC::$server->getSystemConfig()->getValue('maintenance', false) && OC::$SUBURI != '/core/ajax/update.php') { |
||
| 304 | // send http status 503 |
||
| 305 | header('HTTP/1.1 503 Service Temporarily Unavailable'); |
||
| 306 | header('Status: 503 Service Temporarily Unavailable'); |
||
| 307 | header('Retry-After: 120'); |
||
| 308 | |||
| 309 | // render error page |
||
| 310 | $template = new OC_Template('', 'update.user', 'guest'); |
||
| 311 | OC_Util::addscript('maintenance-check'); |
||
| 312 | $template->printPage(); |
||
| 313 | die(); |
||
| 314 | } |
||
| 315 | } |
||
| 316 | |||
| 317 | public static function checkSingleUserMode($lockIfNoUserLoggedIn = false) { |
||
| 318 | if (!\OC::$server->getSystemConfig()->getValue('singleuser', false)) { |
||
| 319 | return; |
||
| 320 | } |
||
| 321 | $user = OC_User::getUserSession()->getUser(); |
||
| 322 | if ($user) { |
||
| 323 | $group = \OC::$server->getGroupManager()->get('admin'); |
||
| 324 | if ($group->inGroup($user)) { |
||
| 325 | return; |
||
| 326 | } |
||
| 327 | } else { |
||
| 328 | if(!$lockIfNoUserLoggedIn) { |
||
| 329 | return; |
||
| 330 | } |
||
| 331 | } |
||
| 332 | // send http status 503 |
||
| 333 | header('HTTP/1.1 503 Service Temporarily Unavailable'); |
||
| 334 | header('Status: 503 Service Temporarily Unavailable'); |
||
| 335 | header('Retry-After: 120'); |
||
| 336 | |||
| 337 | // render error page |
||
| 338 | $template = new OC_Template('', 'singleuser.user', 'guest'); |
||
| 339 | $template->printPage(); |
||
| 340 | die(); |
||
| 341 | } |
||
| 342 | |||
| 343 | /** |
||
| 344 | * check if the instance needs to perform an upgrade |
||
| 345 | * |
||
| 346 | * @return bool |
||
| 347 | * @deprecated use \OCP\Util::needUpgrade() instead |
||
| 348 | */ |
||
| 349 | public static function needUpgrade() { |
||
| 350 | return \OCP\Util::needUpgrade(); |
||
| 351 | } |
||
| 352 | |||
| 353 | /** |
||
| 354 | * Checks if the version requires an update and shows |
||
| 355 | * @param bool $showTemplate Whether an update screen should get shown |
||
| 356 | * @return bool|void |
||
| 357 | */ |
||
| 358 | public static function checkUpgrade($showTemplate = true) { |
||
| 359 | if (\OCP\Util::needUpgrade()) { |
||
| 360 | $systemConfig = \OC::$server->getSystemConfig(); |
||
| 361 | if ($showTemplate && !$systemConfig->getValue('maintenance', false)) { |
||
| 362 | self::printUpgradePage(); |
||
| 363 | exit(); |
||
| 364 | } else { |
||
| 365 | return true; |
||
| 366 | } |
||
| 367 | } |
||
| 368 | return false; |
||
| 369 | } |
||
| 370 | |||
| 371 | /** |
||
| 372 | * Prints the upgrade page |
||
| 373 | */ |
||
| 374 | private static function printUpgradePage() { |
||
| 375 | $systemConfig = \OC::$server->getSystemConfig(); |
||
| 376 | $oldTheme = $systemConfig->getValue('theme'); |
||
| 377 | $systemConfig->setValue('theme', ''); |
||
| 378 | \OCP\Util::addScript('config'); // needed for web root |
||
| 379 | \OCP\Util::addScript('update'); |
||
| 380 | |||
| 381 | // check whether this is a core update or apps update |
||
| 382 | $installedVersion = $systemConfig->getValue('version', '0.0.0'); |
||
| 383 | $currentVersion = implode('.', \OCP\Util::getVersion()); |
||
| 384 | |||
| 385 | $appManager = \OC::$server->getAppManager(); |
||
| 386 | |||
| 387 | $tmpl = new OC_Template('', 'update.admin', 'guest'); |
||
| 388 | $tmpl->assign('version', OC_Util::getVersionString()); |
||
| 389 | |||
| 390 | // if not a core upgrade, then it's apps upgrade |
||
| 391 | if (version_compare($currentVersion, $installedVersion, '=')) { |
||
| 392 | $tmpl->assign('isAppsOnlyUpgrade', true); |
||
| 393 | } else { |
||
| 394 | $tmpl->assign('isAppsOnlyUpgrade', false); |
||
| 395 | } |
||
| 396 | |||
| 397 | $releaseNotes = new \OC\ReleaseNotes(\OC::$server->getDatabaseConnection()); |
||
| 398 | |||
| 399 | // get third party apps |
||
| 400 | $ocVersion = \OCP\Util::getVersion(); |
||
| 401 | $tmpl->assign('appsToUpgrade', $appManager->getAppsNeedingUpgrade($ocVersion)); |
||
| 402 | $tmpl->assign('incompatibleAppsList', $appManager->getIncompatibleApps($ocVersion)); |
||
| 403 | $tmpl->assign('productName', 'Nextcloud'); // for now |
||
| 404 | $tmpl->assign('oldTheme', $oldTheme); |
||
| 405 | $tmpl->assign('releaseNotes', $releaseNotes->getReleaseNotes($installedVersion, $currentVersion)); |
||
| 406 | $tmpl->printPage(); |
||
| 407 | } |
||
| 408 | |||
| 409 | public static function initSession() { |
||
| 455 | |||
| 456 | /** |
||
| 457 | * @return string |
||
| 458 | */ |
||
| 459 | private static function getSessionLifeTime() { |
||
| 462 | |||
| 463 | public static function loadAppClassPaths() { |
||
| 476 | |||
| 477 | /** |
||
| 478 | * Try to set some values to the required Nextcloud default |
||
| 479 | */ |
||
| 480 | public static function setRequiredIniValues() { |
||
| 484 | |||
| 485 | /** |
||
| 486 | * Send the same site cookies |
||
| 487 | */ |
||
| 488 | private static function sendSameSiteCookies() { |
||
| 489 | $cookieParams = session_get_cookie_params(); |
||
| 507 | |||
| 508 | /** |
||
| 509 | * Same Site cookie to further mitigate CSRF attacks. This cookie has to |
||
| 510 | * be set in every request if cookies are sent to add a second level of |
||
| 511 | * defense against CSRF. |
||
| 512 | * |
||
| 513 | * If the cookie is not sent this will set the cookie and reload the page. |
||
| 514 | * We use an additional cookie since we want to protect logout CSRF and |
||
| 515 | * also we can't directly interfere with PHP's session mechanism. |
||
| 516 | */ |
||
| 517 | private static function performSameSiteCookieProtection() { |
||
| 518 | if(count($_COOKIE) > 0) { |
||
| 519 | $request = \OC::$server->getRequest(); |
||
| 520 | $requestUri = $request->getScriptName(); |
||
| 521 | $processingScript = explode('/', $requestUri); |
||
| 522 | $processingScript = $processingScript[count($processingScript)-1]; |
||
| 523 | |||
| 524 | // FIXME: In a SAML scenario we don't get any strict or lax cookie |
||
| 525 | // send for the ACS endpoint. Since we have some legacy code in Nextcloud |
||
| 526 | // (direct PHP files) the enforcement of lax cookies is performed here |
||
| 527 | // instead of the middleware. |
||
| 528 | // |
||
| 529 | // This means we cannot exclude some routes from the cookie validation, |
||
| 530 | // which normally is not a problem but is a little bit cumbersome for |
||
| 531 | // this use-case. |
||
| 532 | // Once the old legacy PHP endpoints have been removed we can move |
||
| 533 | // the verification into a middleware and also adds some exemptions. |
||
| 534 | // |
||
| 535 | // Questions about this code? Ask Lukas ;-) |
||
| 536 | $currentUrl = substr(explode('?',$request->getRequestUri(), 2)[0], strlen(\OC::$WEBROOT)); |
||
| 537 | if($currentUrl === '/index.php/apps/user_saml/saml/acs') { |
||
| 538 | return; |
||
| 539 | } |
||
| 540 | |||
| 541 | // For the "index.php" endpoint only a lax cookie is required. |
||
| 542 | if($processingScript === 'index.php') { |
||
| 543 | if(!$request->passesLaxCookieCheck()) { |
||
| 544 | self::sendSameSiteCookies(); |
||
| 545 | header('Location: '.$_SERVER['REQUEST_URI']); |
||
| 546 | exit(); |
||
| 547 | } |
||
| 548 | } else { |
||
| 549 | // All other endpoints require the lax and the strict cookie |
||
| 550 | if(!$request->passesStrictCookieCheck()) { |
||
| 551 | self::sendSameSiteCookies(); |
||
| 552 | // Debug mode gets access to the resources without strict cookie |
||
| 553 | // due to the fact that the SabreDAV browser also lives there. |
||
| 554 | if(!\OC::$server->getConfig()->getSystemValue('debug', false)) { |
||
| 555 | http_response_code(\OCP\AppFramework\Http::STATUS_SERVICE_UNAVAILABLE); |
||
| 556 | exit(); |
||
| 557 | } |
||
| 558 | } |
||
| 559 | } |
||
| 560 | } elseif(!isset($_COOKIE['nc_sameSiteCookielax']) || !isset($_COOKIE['nc_sameSiteCookiestrict'])) { |
||
| 561 | self::sendSameSiteCookies(); |
||
| 562 | } |
||
| 563 | } |
||
| 564 | |||
| 565 | |||
| 566 | public static function init() { |
||
| 795 | |||
| 796 | /** |
||
| 797 | * register hooks for the cache |
||
| 798 | */ |
||
| 799 | public static function registerCacheHooks() { |
||
| 820 | |||
| 821 | private static function registerEncryptionWrapper() { |
||
| 825 | |||
| 826 | private static function registerEncryptionHooks() { |
||
| 835 | |||
| 836 | /** |
||
| 837 | * register hooks for the cache |
||
| 838 | */ |
||
| 839 | public static function registerLogRotate() { |
||
| 847 | |||
| 848 | /** |
||
| 849 | * register hooks for the filesystem |
||
| 850 | */ |
||
| 851 | public static function registerFilesystemHooks() { |
||
| 856 | |||
| 857 | /** |
||
| 858 | * register hooks for previews |
||
| 859 | */ |
||
| 860 | public static function registerPreviewHooks() { |
||
| 870 | |||
| 871 | /** |
||
| 872 | * register hooks for sharing |
||
| 873 | */ |
||
| 874 | public static function registerShareHooks() { |
||
| 883 | |||
| 884 | protected static function registerAutoloaderCache() { |
||
| 900 | |||
| 901 | /** |
||
| 902 | * Handle the request |
||
| 903 | */ |
||
| 904 | public static function handleRequest() { |
||
| 905 | |||
| 906 | \OC::$server->getEventLogger()->start('handle_request', 'Handle request'); |
||
| 907 | $systemConfig = \OC::$server->getSystemConfig(); |
||
| 908 | // load all the classpaths from the enabled apps so they are available |
||
| 909 | // in the routing files of each app |
||
| 910 | OC::loadAppClassPaths(); |
||
| 911 | |||
| 912 | // Check if Nextcloud is installed or in maintenance (update) mode |
||
| 913 | if (!$systemConfig->getValue('installed', false)) { |
||
| 914 | \OC::$server->getSession()->clear(); |
||
| 915 | $setupHelper = new OC\Setup(\OC::$server->getConfig(), \OC::$server->getIniWrapper(), |
||
| 916 | \OC::$server->getL10N('lib'), \OC::$server->getThemingDefaults(), \OC::$server->getLogger(), |
||
| 917 | \OC::$server->getSecureRandom()); |
||
| 918 | $controller = new OC\Core\Controller\SetupController($setupHelper); |
||
| 919 | $controller->run($_POST); |
||
| 920 | exit(); |
||
| 921 | } |
||
| 922 | |||
| 923 | $request = \OC::$server->getRequest(); |
||
| 924 | $requestPath = $request->getRawPathInfo(); |
||
| 925 | if (substr($requestPath, -3) !== '.js') { // we need these files during the upgrade |
||
| 926 | self::checkMaintenanceMode(); |
||
| 927 | self::checkUpgrade(); |
||
| 928 | } |
||
| 929 | |||
| 930 | // emergency app disabling |
||
| 931 | if ($requestPath === '/disableapp' |
||
| 932 | && $request->getMethod() === 'POST' |
||
| 933 | && ((string)$request->getParam('appid')) !== '' |
||
| 934 | ) { |
||
| 935 | \OCP\JSON::callCheck(); |
||
| 936 | \OCP\JSON::checkAdminUser(); |
||
| 937 | $appId = (string)$request->getParam('appid'); |
||
| 938 | $appId = \OC_App::cleanAppId($appId); |
||
| 939 | |||
| 940 | \OC_App::disable($appId); |
||
| 941 | \OC_JSON::success(); |
||
| 942 | exit(); |
||
| 943 | } |
||
| 944 | |||
| 945 | // Always load authentication apps |
||
| 946 | OC_App::loadApps(['authentication']); |
||
| 947 | |||
| 948 | // Load minimum set of apps |
||
| 949 | if (!self::checkUpgrade(false) |
||
| 950 | && !$systemConfig->getValue('maintenance', false)) { |
||
| 951 | // For logged-in users: Load everything |
||
| 952 | if(OC_User::isLoggedIn()) { |
||
| 953 | OC_App::loadApps(); |
||
| 954 | } else { |
||
| 955 | // For guests: Load only filesystem and logging |
||
| 956 | OC_App::loadApps(array('filesystem', 'logging')); |
||
| 957 | \OC_User::tryBasicAuthLogin(); |
||
| 958 | } |
||
| 959 | } |
||
| 960 | |||
| 961 | if (!self::$CLI and (!isset($_GET["logout"]) or ($_GET["logout"] !== 'true'))) { |
||
| 962 | try { |
||
| 963 | if (!$systemConfig->getValue('maintenance', false) && !self::checkUpgrade(false)) { |
||
| 964 | OC_App::loadApps(array('filesystem', 'logging')); |
||
| 965 | OC_App::loadApps(); |
||
| 966 | } |
||
| 967 | self::checkSingleUserMode(); |
||
| 968 | OC_Util::setupFS(); |
||
| 969 | OC::$server->getRouter()->match(\OC::$server->getRequest()->getRawPathInfo()); |
||
| 970 | return; |
||
| 971 | } catch (Symfony\Component\Routing\Exception\ResourceNotFoundException $e) { |
||
| 972 | //header('HTTP/1.0 404 Not Found'); |
||
| 973 | } catch (Symfony\Component\Routing\Exception\MethodNotAllowedException $e) { |
||
| 974 | OC_Response::setStatus(405); |
||
| 975 | return; |
||
| 976 | } |
||
| 977 | } |
||
| 978 | |||
| 979 | // Handle redirect URL for logged in users |
||
| 980 | if (isset($_REQUEST['redirect_url']) && OC_User::isLoggedIn()) { |
||
| 981 | $location = \OC::$server->getURLGenerator()->getAbsoluteURL(urldecode($_REQUEST['redirect_url'])); |
||
| 982 | |||
| 983 | // Deny the redirect if the URL contains a @ |
||
| 984 | // This prevents unvalidated redirects like ?redirect_url=:[email protected] |
||
| 985 | if (strpos($location, '@') === false) { |
||
| 986 | header('Location: ' . $location); |
||
| 987 | return; |
||
| 988 | } |
||
| 989 | } |
||
| 990 | // Handle WebDAV |
||
| 991 | if ($_SERVER['REQUEST_METHOD'] == 'PROPFIND') { |
||
| 992 | // not allowed any more to prevent people |
||
| 993 | // mounting this root directly. |
||
| 994 | // Users need to mount remote.php/webdav instead. |
||
| 995 | header('HTTP/1.1 405 Method Not Allowed'); |
||
| 996 | header('Status: 405 Method Not Allowed'); |
||
| 997 | return; |
||
| 998 | } |
||
| 999 | |||
| 1000 | // Redirect to index if the logout link is accessed without valid session |
||
| 1001 | // this is needed to prevent "Token expired" messages while login if a session is expired |
||
| 1002 | // @see https://github.com/owncloud/core/pull/8443#issuecomment-42425583 |
||
| 1003 | if(isset($_GET['logout']) && !OC_User::isLoggedIn()) { |
||
| 1004 | header("Location: " . \OC::$server->getURLGenerator()->getAbsoluteURL('/')); |
||
| 1005 | return; |
||
| 1006 | } |
||
| 1007 | |||
| 1008 | // Someone is logged in |
||
| 1009 | if (OC_User::isLoggedIn()) { |
||
| 1010 | OC_App::loadApps(); |
||
| 1011 | OC_User::setupBackends(); |
||
| 1012 | OC_Util::setupFS(); |
||
| 1013 | if (isset($_GET["logout"]) and ($_GET["logout"])) { |
||
| 1014 | OC_JSON::callCheck(); |
||
| 1015 | if (isset($_COOKIE['oc_token'])) { |
||
| 1016 | \OC::$server->getConfig()->deleteUserValue(OC_User::getUser(), 'login_token', $_COOKIE['oc_token']); |
||
| 1017 | } |
||
| 1018 | OC_User::logout(); |
||
| 1019 | // redirect to webroot and add slash if webroot is empty |
||
| 1020 | header("Location: " . \OC::$server->getURLGenerator()->getAbsoluteURL('/')); |
||
| 1021 | } else { |
||
| 1022 | // Redirect to default application |
||
| 1023 | OC_Util::redirectToDefaultPage(); |
||
| 1024 | } |
||
| 1025 | } else { |
||
| 1026 | // Not handled and not logged in |
||
| 1027 | self::handleLogin(); |
||
| 1028 | } |
||
| 1029 | } |
||
| 1030 | |||
| 1031 | protected static function handleAuthHeaders() { |
||
| 1051 | |||
| 1052 | protected static function handleLogin() { |
||
| 1078 | |||
| 1079 | /** |
||
| 1080 | * Remove outdated and therefore invalid tokens for a user |
||
| 1081 | * @param string $user |
||
| 1082 | */ |
||
| 1083 | protected static function cleanupLoginTokens($user) { |
||
| 1094 | |||
| 1095 | /** |
||
| 1096 | * Try to login a user via HTTP authentication |
||
| 1097 | * @return bool|void |
||
| 1098 | */ |
||
| 1099 | protected static function tryApacheAuth() { |
||
| 1112 | |||
| 1113 | /** |
||
| 1114 | * Try to login a user using the remember me cookie. |
||
| 1115 | * @return bool Whether the provided cookie was valid |
||
| 1116 | */ |
||
| 1117 | protected static function tryRememberLogin() { |
||
| 1151 | |||
| 1152 | /** |
||
| 1153 | * Tries to login a user using the form based authentication |
||
| 1154 | * @return bool|void |
||
| 1155 | */ |
||
| 1156 | protected static function tryFormLogin() { |
||
| 1195 | |||
| 1196 | } |
||
| 1197 | |||
| 1198 | |||
| 1199 | OC::init(); |
||
| 1200 |
If you suppress an error, we recommend checking for the error condition explicitly: