| Total Complexity | 179 |
| Total Lines | 1065 |
| Duplicated Lines | 0 % |
| Changes | 2 | ||
| Bugs | 2 | Features | 0 |
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.
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 |
||
| 93 | class OC { |
||
| 94 | /** |
||
| 95 | * Associative array for autoloading. classname => filename |
||
| 96 | */ |
||
| 97 | public static array $CLASSPATH = []; |
||
| 98 | /** |
||
| 99 | * The installation path for Nextcloud on the server (e.g. /srv/http/nextcloud) |
||
| 100 | */ |
||
| 101 | public static string $SERVERROOT = ''; |
||
| 102 | /** |
||
| 103 | * the current request path relative to the Nextcloud root (e.g. files/index.php) |
||
| 104 | */ |
||
| 105 | private static string $SUBURI = ''; |
||
| 106 | /** |
||
| 107 | * the Nextcloud root path for http requests (e.g. nextcloud/) |
||
| 108 | */ |
||
| 109 | public static string $WEBROOT = ''; |
||
| 110 | /** |
||
| 111 | * The installation path array of the apps folder on the server (e.g. /srv/http/nextcloud) 'path' and |
||
| 112 | * web path in 'url' |
||
| 113 | */ |
||
| 114 | public static array $APPSROOTS = []; |
||
| 115 | |||
| 116 | public static string $configDir; |
||
| 117 | |||
| 118 | /** |
||
| 119 | * requested app |
||
| 120 | */ |
||
| 121 | public static string $REQUESTEDAPP = ''; |
||
| 122 | |||
| 123 | /** |
||
| 124 | * check if Nextcloud runs in cli mode |
||
| 125 | */ |
||
| 126 | public static bool $CLI = false; |
||
| 127 | |||
| 128 | public static \OC\Autoloader $loader; |
||
| 129 | |||
| 130 | public static \Composer\Autoload\ClassLoader $composerAutoloader; |
||
| 131 | |||
| 132 | public static \OC\Server $server; |
||
| 133 | |||
| 134 | private static \OC\Config $config; |
||
| 135 | |||
| 136 | /** |
||
| 137 | * @throws \RuntimeException when the 3rdparty directory is missing or |
||
| 138 | * the app path list is empty or contains an invalid path |
||
| 139 | */ |
||
| 140 | public static function initPaths(): void { |
||
| 141 | if (defined('PHPUNIT_CONFIG_DIR')) { |
||
| 142 | self::$configDir = OC::$SERVERROOT . '/' . PHPUNIT_CONFIG_DIR . '/'; |
||
|
|
|||
| 143 | } elseif (defined('PHPUNIT_RUN') and PHPUNIT_RUN and is_dir(OC::$SERVERROOT . '/tests/config/')) { |
||
| 144 | self::$configDir = OC::$SERVERROOT . '/tests/config/'; |
||
| 145 | } elseif ($dir = getenv('NEXTCLOUD_CONFIG_DIR')) { |
||
| 146 | self::$configDir = rtrim($dir, '/') . '/'; |
||
| 147 | } else { |
||
| 148 | self::$configDir = OC::$SERVERROOT . '/config/'; |
||
| 149 | } |
||
| 150 | self::$config = new \OC\Config(self::$configDir); |
||
| 151 | |||
| 152 | OC::$SUBURI = str_replace("\\", "/", substr(realpath($_SERVER["SCRIPT_FILENAME"] ?? ''), strlen(OC::$SERVERROOT))); |
||
| 153 | /** |
||
| 154 | * FIXME: The following lines are required because we can't yet instantiate |
||
| 155 | * Server::get(\OCP\IRequest::class) since \OC::$server does not yet exist. |
||
| 156 | */ |
||
| 157 | $params = [ |
||
| 158 | 'server' => [ |
||
| 159 | 'SCRIPT_NAME' => $_SERVER['SCRIPT_NAME'] ?? null, |
||
| 160 | 'SCRIPT_FILENAME' => $_SERVER['SCRIPT_FILENAME'] ?? null, |
||
| 161 | ], |
||
| 162 | ]; |
||
| 163 | $fakeRequest = new \OC\AppFramework\Http\Request( |
||
| 164 | $params, |
||
| 165 | new \OC\AppFramework\Http\RequestId($_SERVER['UNIQUE_ID'] ?? '', new \OC\Security\SecureRandom()), |
||
| 166 | new \OC\AllConfig(new \OC\SystemConfig(self::$config)) |
||
| 167 | ); |
||
| 168 | $scriptName = $fakeRequest->getScriptName(); |
||
| 169 | if (substr($scriptName, -1) == '/') { |
||
| 170 | $scriptName .= 'index.php'; |
||
| 171 | //make sure suburi follows the same rules as scriptName |
||
| 172 | if (substr(OC::$SUBURI, -9) != 'index.php') { |
||
| 173 | if (substr(OC::$SUBURI, -1) != '/') { |
||
| 174 | OC::$SUBURI = OC::$SUBURI . '/'; |
||
| 175 | } |
||
| 176 | OC::$SUBURI = OC::$SUBURI . 'index.php'; |
||
| 177 | } |
||
| 178 | } |
||
| 179 | |||
| 180 | |||
| 181 | if (OC::$CLI) { |
||
| 182 | OC::$WEBROOT = self::$config->getValue('overwritewebroot', ''); |
||
| 183 | } else { |
||
| 184 | if (substr($scriptName, 0 - strlen(OC::$SUBURI)) === OC::$SUBURI) { |
||
| 185 | OC::$WEBROOT = substr($scriptName, 0, 0 - strlen(OC::$SUBURI)); |
||
| 186 | |||
| 187 | if (OC::$WEBROOT != '' && OC::$WEBROOT[0] !== '/') { |
||
| 188 | OC::$WEBROOT = '/' . OC::$WEBROOT; |
||
| 189 | } |
||
| 190 | } else { |
||
| 191 | // The scriptName is not ending with OC::$SUBURI |
||
| 192 | // This most likely means that we are calling from CLI. |
||
| 193 | // However some cron jobs still need to generate |
||
| 194 | // a web URL, so we use overwritewebroot as a fallback. |
||
| 195 | OC::$WEBROOT = self::$config->getValue('overwritewebroot', ''); |
||
| 196 | } |
||
| 197 | |||
| 198 | // Resolve /nextcloud to /nextcloud/ to ensure to always have a trailing |
||
| 199 | // slash which is required by URL generation. |
||
| 200 | if (isset($_SERVER['REQUEST_URI']) && $_SERVER['REQUEST_URI'] === \OC::$WEBROOT && |
||
| 201 | substr($_SERVER['REQUEST_URI'], -1) !== '/') { |
||
| 202 | header('Location: '.\OC::$WEBROOT.'/'); |
||
| 203 | exit(); |
||
| 204 | } |
||
| 205 | } |
||
| 206 | |||
| 207 | // search the apps folder |
||
| 208 | $config_paths = self::$config->getValue('apps_paths', []); |
||
| 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[] = ['path' => OC::$SERVERROOT . '/apps', 'url' => '/apps', 'writable' => true]; |
||
| 219 | } |
||
| 220 | |||
| 221 | if (empty(OC::$APPSROOTS)) { |
||
| 222 | throw new \RuntimeException('apps directory not found! Please put the Nextcloud apps folder in the Nextcloud folder' |
||
| 223 | . '. You can also configure the location in the config.php file.'); |
||
| 224 | } |
||
| 225 | $paths = []; |
||
| 226 | foreach (OC::$APPSROOTS as $path) { |
||
| 227 | $paths[] = $path['path']; |
||
| 228 | if (!is_dir($path['path'])) { |
||
| 229 | throw new \RuntimeException(sprintf('App directory "%s" not found! Please put the Nextcloud apps folder in the' |
||
| 230 | . ' Nextcloud folder. You can also configure the location in the config.php file.', $path['path'])); |
||
| 231 | } |
||
| 232 | } |
||
| 233 | |||
| 234 | // set the right include path |
||
| 235 | set_include_path( |
||
| 236 | implode(PATH_SEPARATOR, $paths) |
||
| 237 | ); |
||
| 238 | } |
||
| 239 | |||
| 240 | public static function checkConfig(): void { |
||
| 269 | ); |
||
| 270 | } |
||
| 271 | } |
||
| 272 | } |
||
| 273 | |||
| 274 | public static function checkInstalled(\OC\SystemConfig $systemConfig): void { |
||
| 287 | } |
||
| 288 | } |
||
| 289 | |||
| 290 | public static function checkMaintenanceMode(\OC\SystemConfig $systemConfig): void { |
||
| 304 | } |
||
| 305 | } |
||
| 306 | |||
| 307 | /** |
||
| 308 | * Prints the upgrade page |
||
| 309 | */ |
||
| 310 | private static function printUpgradePage(\OC\SystemConfig $systemConfig): void { |
||
| 410 | } |
||
| 411 | |||
| 412 | public static function initSession(): void { |
||
| 413 | $request = Server::get(IRequest::class); |
||
| 414 | |||
| 415 | // TODO: Temporary disabled again to solve issues with CalDAV/CardDAV clients like DAVx5 that use cookies |
||
| 416 | // TODO: See https://github.com/nextcloud/server/issues/37277#issuecomment-1476366147 and the other comments |
||
| 417 | // TODO: for further information. |
||
| 418 | // $isDavRequest = strpos($request->getRequestUri(), '/remote.php/dav') === 0 || strpos($request->getRequestUri(), '/remote.php/webdav') === 0; |
||
| 419 | // if ($request->getHeader('Authorization') !== '' && is_null($request->getCookie('cookie_test')) && $isDavRequest && !isset($_COOKIE['nc_session_id'])) { |
||
| 420 | // setcookie('cookie_test', 'test', time() + 3600); |
||
| 421 | // // Do not initialize the session if a request is authenticated directly |
||
| 422 | // // unless there is a session cookie already sent along |
||
| 423 | // return; |
||
| 424 | // } |
||
| 425 | |||
| 426 | if ($request->getServerProtocol() === 'https') { |
||
| 427 | ini_set('session.cookie_secure', 'true'); |
||
| 428 | } |
||
| 429 | |||
| 430 | // prevents javascript from accessing php session cookies |
||
| 431 | ini_set('session.cookie_httponly', 'true'); |
||
| 432 | |||
| 433 | // set the cookie path to the Nextcloud directory |
||
| 434 | $cookie_path = OC::$WEBROOT ? : '/'; |
||
| 435 | ini_set('session.cookie_path', $cookie_path); |
||
| 436 | |||
| 437 | // Let the session name be changed in the initSession Hook |
||
| 438 | $sessionName = OC_Util::getInstanceId(); |
||
| 439 | |||
| 440 | try { |
||
| 441 | // set the session name to the instance id - which is unique |
||
| 442 | $session = new \OC\Session\Internal($sessionName); |
||
| 443 | |||
| 444 | $cryptoWrapper = Server::get(\OC\Session\CryptoWrapper::class); |
||
| 445 | $session = $cryptoWrapper->wrapSession($session); |
||
| 446 | self::$server->setSession($session); |
||
| 447 | |||
| 448 | // if session can't be started break with http 500 error |
||
| 449 | } catch (Exception $e) { |
||
| 450 | Server::get(LoggerInterface::class)->error($e->getMessage(), ['app' => 'base','exception' => $e]); |
||
| 451 | //show the user a detailed error page |
||
| 452 | OC_Template::printExceptionErrorPage($e, 500); |
||
| 453 | die(); |
||
| 454 | } |
||
| 455 | |||
| 456 | //try to set the session lifetime |
||
| 457 | $sessionLifeTime = self::getSessionLifeTime(); |
||
| 458 | @ini_set('gc_maxlifetime', (string)$sessionLifeTime); |
||
| 459 | |||
| 460 | // session timeout |
||
| 461 | if ($session->exists('LAST_ACTIVITY') && (time() - $session->get('LAST_ACTIVITY') > $sessionLifeTime)) { |
||
| 462 | if (isset($_COOKIE[session_name()])) { |
||
| 463 | setcookie(session_name(), '', -1, self::$WEBROOT ? : '/'); |
||
| 464 | } |
||
| 465 | Server::get(IUserSession::class)->logout(); |
||
| 466 | } |
||
| 467 | |||
| 468 | if (!self::hasSessionRelaxedExpiry()) { |
||
| 469 | $session->set('LAST_ACTIVITY', time()); |
||
| 470 | } |
||
| 471 | $session->close(); |
||
| 472 | } |
||
| 473 | |||
| 474 | private static function getSessionLifeTime(): int { |
||
| 475 | return Server::get(\OC\AllConfig::class)->getSystemValueInt('session_lifetime', 60 * 60 * 24); |
||
| 476 | } |
||
| 477 | |||
| 478 | /** |
||
| 479 | * @return bool true if the session expiry should only be done by gc instead of an explicit timeout |
||
| 480 | */ |
||
| 481 | public static function hasSessionRelaxedExpiry(): bool { |
||
| 482 | return Server::get(\OC\AllConfig::class)->getSystemValueBool('session_relaxed_expiry', false); |
||
| 483 | } |
||
| 484 | |||
| 485 | /** |
||
| 486 | * Try to set some values to the required Nextcloud default |
||
| 487 | */ |
||
| 488 | public static function setRequiredIniValues(): void { |
||
| 489 | @ini_set('default_charset', 'UTF-8'); |
||
| 490 | @ini_set('gd.jpeg_ignore_warning', '1'); |
||
| 491 | } |
||
| 492 | |||
| 493 | /** |
||
| 494 | * Send the same site cookies |
||
| 495 | */ |
||
| 496 | private static function sendSameSiteCookies(): void { |
||
| 497 | $cookieParams = session_get_cookie_params(); |
||
| 498 | $secureCookie = ($cookieParams['secure'] === true) ? 'secure; ' : ''; |
||
| 499 | $policies = [ |
||
| 500 | 'lax', |
||
| 501 | 'strict', |
||
| 502 | ]; |
||
| 503 | |||
| 504 | // Append __Host to the cookie if it meets the requirements |
||
| 505 | $cookiePrefix = ''; |
||
| 506 | if ($cookieParams['secure'] === true && $cookieParams['path'] === '/') { |
||
| 507 | $cookiePrefix = '__Host-'; |
||
| 508 | } |
||
| 509 | |||
| 510 | foreach ($policies as $policy) { |
||
| 511 | header( |
||
| 512 | sprintf( |
||
| 513 | 'Set-Cookie: %snc_sameSiteCookie%s=true; path=%s; httponly;' . $secureCookie . 'expires=Fri, 31-Dec-2100 23:59:59 GMT; SameSite=%s', |
||
| 514 | $cookiePrefix, |
||
| 515 | $policy, |
||
| 516 | $cookieParams['path'], |
||
| 517 | $policy |
||
| 518 | ), |
||
| 519 | false |
||
| 520 | ); |
||
| 521 | } |
||
| 522 | } |
||
| 523 | |||
| 524 | /** |
||
| 525 | * Same Site cookie to further mitigate CSRF attacks. This cookie has to |
||
| 526 | * be set in every request if cookies are sent to add a second level of |
||
| 527 | * defense against CSRF. |
||
| 528 | * |
||
| 529 | * If the cookie is not sent this will set the cookie and reload the page. |
||
| 530 | * We use an additional cookie since we want to protect logout CSRF and |
||
| 531 | * also we can't directly interfere with PHP's session mechanism. |
||
| 532 | */ |
||
| 533 | private static function performSameSiteCookieProtection(\OCP\IConfig $config): void { |
||
| 534 | $request = Server::get(IRequest::class); |
||
| 535 | |||
| 536 | // Some user agents are notorious and don't really properly follow HTTP |
||
| 537 | // specifications. For those, have an automated opt-out. Since the protection |
||
| 538 | // for remote.php is applied in base.php as starting point we need to opt out |
||
| 539 | // here. |
||
| 540 | $incompatibleUserAgents = $config->getSystemValue('csrf.optout'); |
||
| 541 | |||
| 542 | // Fallback, if csrf.optout is unset |
||
| 543 | if (!is_array($incompatibleUserAgents)) { |
||
| 544 | $incompatibleUserAgents = [ |
||
| 545 | // OS X Finder |
||
| 546 | '/^WebDAVFS/', |
||
| 547 | // Windows webdav drive |
||
| 548 | '/^Microsoft-WebDAV-MiniRedir/', |
||
| 549 | ]; |
||
| 550 | } |
||
| 551 | |||
| 552 | if ($request->isUserAgent($incompatibleUserAgents)) { |
||
| 553 | return; |
||
| 554 | } |
||
| 555 | |||
| 556 | if (count($_COOKIE) > 0) { |
||
| 557 | $requestUri = $request->getScriptName(); |
||
| 558 | $processingScript = explode('/', $requestUri); |
||
| 559 | $processingScript = $processingScript[count($processingScript) - 1]; |
||
| 560 | |||
| 561 | // index.php routes are handled in the middleware |
||
| 562 | if ($processingScript === 'index.php') { |
||
| 563 | return; |
||
| 564 | } |
||
| 565 | |||
| 566 | // All other endpoints require the lax and the strict cookie |
||
| 567 | if (!$request->passesStrictCookieCheck()) { |
||
| 568 | self::sendSameSiteCookies(); |
||
| 569 | // Debug mode gets access to the resources without strict cookie |
||
| 570 | // due to the fact that the SabreDAV browser also lives there. |
||
| 571 | if (!$config->getSystemValue('debug', false)) { |
||
| 572 | http_response_code(\OCP\AppFramework\Http::STATUS_SERVICE_UNAVAILABLE); |
||
| 573 | exit(); |
||
| 574 | } |
||
| 575 | } |
||
| 576 | } elseif (!isset($_COOKIE['nc_sameSiteCookielax']) || !isset($_COOKIE['nc_sameSiteCookiestrict'])) { |
||
| 577 | self::sendSameSiteCookies(); |
||
| 578 | } |
||
| 579 | } |
||
| 580 | |||
| 581 | public static function init(): void { |
||
| 844 | }); |
||
| 845 | } |
||
| 846 | |||
| 847 | /** |
||
| 848 | * register hooks for the cleanup of cache and bruteforce protection |
||
| 849 | */ |
||
| 850 | public static function registerCleanupHooks(\OC\SystemConfig $systemConfig): void { |
||
| 878 | ]); |
||
| 879 | } |
||
| 880 | }); |
||
| 881 | } |
||
| 882 | } |
||
| 883 | |||
| 884 | private static function registerEncryptionWrapperAndHooks(): void { |
||
| 885 | $manager = Server::get(\OCP\Encryption\IManager::class); |
||
| 886 | \OCP\Util::connectHook('OC_Filesystem', 'preSetup', $manager, 'setupStorage'); |
||
| 887 | |||
| 888 | $enabled = $manager->isEnabled(); |
||
| 889 | if ($enabled) { |
||
| 890 | \OCP\Util::connectHook(Share::class, 'post_shared', HookManager::class, 'postShared'); |
||
| 891 | \OCP\Util::connectHook(Share::class, 'post_unshare', HookManager::class, 'postUnshared'); |
||
| 892 | \OCP\Util::connectHook('OC_Filesystem', 'post_rename', HookManager::class, 'postRename'); |
||
| 893 | \OCP\Util::connectHook('\OCA\Files_Trashbin\Trashbin', 'post_restore', HookManager::class, 'postRestore'); |
||
| 894 | } |
||
| 895 | } |
||
| 896 | |||
| 897 | private static function registerAccountHooks(): void { |
||
| 898 | /** @var IEventDispatcher $dispatcher */ |
||
| 899 | $dispatcher = Server::get(IEventDispatcher::class); |
||
| 900 | $dispatcher->addServiceListener(UserChangedEvent::class, \OC\Accounts\Hooks::class); |
||
| 901 | } |
||
| 902 | |||
| 903 | private static function registerAppRestrictionsHooks(): void { |
||
| 904 | /** @var \OC\Group\Manager $groupManager */ |
||
| 905 | $groupManager = Server::get(\OCP\IGroupManager::class); |
||
| 906 | $groupManager->listen('\OC\Group', 'postDelete', function (\OCP\IGroup $group) { |
||
| 907 | $appManager = Server::get(\OCP\App\IAppManager::class); |
||
| 908 | $apps = $appManager->getEnabledAppsForGroup($group); |
||
| 909 | foreach ($apps as $appId) { |
||
| 910 | $restrictions = $appManager->getAppRestriction($appId); |
||
| 911 | if (empty($restrictions)) { |
||
| 912 | continue; |
||
| 913 | } |
||
| 914 | $key = array_search($group->getGID(), $restrictions); |
||
| 915 | unset($restrictions[$key]); |
||
| 916 | $restrictions = array_values($restrictions); |
||
| 917 | if (empty($restrictions)) { |
||
| 918 | $appManager->disableApp($appId); |
||
| 919 | } else { |
||
| 920 | $appManager->enableAppForGroups($appId, $restrictions); |
||
| 921 | } |
||
| 922 | } |
||
| 923 | }); |
||
| 924 | } |
||
| 925 | |||
| 926 | private static function registerResourceCollectionHooks(): void { |
||
| 927 | \OC\Collaboration\Resources\Listener::register(Server::get(SymfonyAdapter::class), Server::get(IEventDispatcher::class)); |
||
| 928 | } |
||
| 929 | |||
| 930 | private static function registerFileReferenceEventListener(): void { |
||
| 931 | \OC\Collaboration\Reference\File\FileReferenceEventListener::register(Server::get(IEventDispatcher::class)); |
||
| 932 | } |
||
| 933 | |||
| 934 | private static function registerRenderReferenceEventListener() { |
||
| 935 | \OC\Collaboration\Reference\RenderReferenceEventListener::register(Server::get(IEventDispatcher::class)); |
||
| 936 | } |
||
| 937 | |||
| 938 | /** |
||
| 939 | * register hooks for sharing |
||
| 940 | */ |
||
| 941 | public static function registerShareHooks(\OC\SystemConfig $systemConfig): void { |
||
| 942 | if ($systemConfig->getValue('installed')) { |
||
| 943 | OC_Hook::connect('OC_User', 'post_deleteUser', Hooks::class, 'post_deleteUser'); |
||
| 944 | OC_Hook::connect('OC_User', 'post_deleteGroup', Hooks::class, 'post_deleteGroup'); |
||
| 945 | |||
| 946 | /** @var IEventDispatcher $dispatcher */ |
||
| 947 | $dispatcher = Server::get(IEventDispatcher::class); |
||
| 948 | $dispatcher->addServiceListener(UserRemovedEvent::class, \OC\Share20\UserRemovedListener::class); |
||
| 949 | } |
||
| 950 | } |
||
| 951 | |||
| 952 | protected static function registerAutoloaderCache(\OC\SystemConfig $systemConfig): void { |
||
| 953 | // The class loader takes an optional low-latency cache, which MUST be |
||
| 954 | // namespaced. The instanceid is used for namespacing, but might be |
||
| 955 | // unavailable at this point. Furthermore, it might not be possible to |
||
| 956 | // generate an instanceid via \OC_Util::getInstanceId() because the |
||
| 957 | // config file may not be writable. As such, we only register a class |
||
| 958 | // loader cache if instanceid is available without trying to create one. |
||
| 959 | $instanceId = $systemConfig->getValue('instanceid', null); |
||
| 960 | if ($instanceId) { |
||
| 961 | try { |
||
| 962 | $memcacheFactory = Server::get(\OCP\ICacheFactory::class); |
||
| 963 | self::$loader->setMemoryCache($memcacheFactory->createLocal('Autoloader')); |
||
| 964 | } catch (\Exception $ex) { |
||
| 965 | } |
||
| 966 | } |
||
| 967 | } |
||
| 968 | |||
| 969 | /** |
||
| 970 | * Handle the request |
||
| 971 | */ |
||
| 972 | public static function handleRequest(): void { |
||
| 973 | Server::get(\OCP\Diagnostics\IEventLogger::class)->start('handle_request', 'Handle request'); |
||
| 974 | $systemConfig = Server::get(\OC\SystemConfig::class); |
||
| 975 | |||
| 976 | // Check if Nextcloud is installed or in maintenance (update) mode |
||
| 977 | if (!$systemConfig->getValue('installed', false)) { |
||
| 978 | \OC::$server->getSession()->clear(); |
||
| 979 | $setupHelper = new OC\Setup( |
||
| 980 | $systemConfig, |
||
| 981 | Server::get(\bantu\IniGetWrapper\IniGetWrapper::class), |
||
| 982 | Server::get(\OCP\L10N\IFactory::class)->get('lib'), |
||
| 983 | Server::get(\OCP\Defaults::class), |
||
| 984 | Server::get(\Psr\Log\LoggerInterface::class), |
||
| 985 | Server::get(\OCP\Security\ISecureRandom::class), |
||
| 986 | Server::get(\OC\Installer::class) |
||
| 987 | ); |
||
| 988 | $controller = new OC\Core\Controller\SetupController($setupHelper); |
||
| 989 | $controller->run($_POST); |
||
| 990 | exit(); |
||
| 991 | } |
||
| 992 | |||
| 993 | $request = Server::get(IRequest::class); |
||
| 994 | $requestPath = $request->getRawPathInfo(); |
||
| 995 | if ($requestPath === '/heartbeat') { |
||
| 996 | return; |
||
| 997 | } |
||
| 998 | if (substr($requestPath, -3) !== '.js') { // we need these files during the upgrade |
||
| 999 | self::checkMaintenanceMode($systemConfig); |
||
| 1000 | |||
| 1001 | if (\OCP\Util::needUpgrade()) { |
||
| 1002 | if (function_exists('opcache_reset')) { |
||
| 1003 | opcache_reset(); |
||
| 1004 | } |
||
| 1005 | if (!((bool) $systemConfig->getValue('maintenance', false))) { |
||
| 1006 | self::printUpgradePage($systemConfig); |
||
| 1007 | exit(); |
||
| 1008 | } |
||
| 1009 | } |
||
| 1010 | } |
||
| 1011 | |||
| 1012 | // emergency app disabling |
||
| 1013 | if ($requestPath === '/disableapp' |
||
| 1014 | && $request->getMethod() === 'POST' |
||
| 1015 | ) { |
||
| 1016 | \OC_JSON::callCheck(); |
||
| 1017 | \OC_JSON::checkAdminUser(); |
||
| 1018 | $appIds = (array)$request->getParam('appid'); |
||
| 1019 | foreach ($appIds as $appId) { |
||
| 1020 | $appId = \OC_App::cleanAppId($appId); |
||
| 1021 | Server::get(\OCP\App\IAppManager::class)->disableApp($appId); |
||
| 1022 | } |
||
| 1023 | \OC_JSON::success(); |
||
| 1024 | exit(); |
||
| 1025 | } |
||
| 1026 | |||
| 1027 | // Always load authentication apps |
||
| 1028 | OC_App::loadApps(['authentication']); |
||
| 1029 | |||
| 1030 | // Load minimum set of apps |
||
| 1031 | if (!\OCP\Util::needUpgrade() |
||
| 1032 | && !((bool) $systemConfig->getValue('maintenance', false))) { |
||
| 1033 | // For logged-in users: Load everything |
||
| 1034 | if (Server::get(IUserSession::class)->isLoggedIn()) { |
||
| 1035 | OC_App::loadApps(); |
||
| 1036 | } else { |
||
| 1037 | // For guests: Load only filesystem and logging |
||
| 1038 | OC_App::loadApps(['filesystem', 'logging']); |
||
| 1039 | |||
| 1040 | // Don't try to login when a client is trying to get a OAuth token. |
||
| 1041 | // OAuth needs to support basic auth too, so the login is not valid |
||
| 1042 | // inside Nextcloud and the Login exception would ruin it. |
||
| 1043 | if ($request->getRawPathInfo() !== '/apps/oauth2/api/v1/token') { |
||
| 1044 | self::handleLogin($request); |
||
| 1045 | } |
||
| 1046 | } |
||
| 1047 | } |
||
| 1048 | |||
| 1049 | if (!self::$CLI) { |
||
| 1050 | try { |
||
| 1051 | if (!((bool) $systemConfig->getValue('maintenance', false)) && !\OCP\Util::needUpgrade()) { |
||
| 1052 | OC_App::loadApps(['filesystem', 'logging']); |
||
| 1053 | OC_App::loadApps(); |
||
| 1054 | } |
||
| 1055 | Server::get(\OC\Route\Router::class)->match($request->getRawPathInfo()); |
||
| 1056 | return; |
||
| 1057 | } catch (Symfony\Component\Routing\Exception\ResourceNotFoundException $e) { |
||
| 1058 | //header('HTTP/1.0 404 Not Found'); |
||
| 1059 | } catch (Symfony\Component\Routing\Exception\MethodNotAllowedException $e) { |
||
| 1060 | http_response_code(405); |
||
| 1061 | return; |
||
| 1062 | } |
||
| 1063 | } |
||
| 1064 | |||
| 1065 | // Handle WebDAV |
||
| 1066 | if (isset($_SERVER['REQUEST_METHOD']) && $_SERVER['REQUEST_METHOD'] === 'PROPFIND') { |
||
| 1067 | // not allowed any more to prevent people |
||
| 1068 | // mounting this root directly. |
||
| 1069 | // Users need to mount remote.php/webdav instead. |
||
| 1070 | http_response_code(405); |
||
| 1071 | return; |
||
| 1072 | } |
||
| 1073 | |||
| 1074 | // Handle requests for JSON or XML |
||
| 1075 | $acceptHeader = $request->getHeader('Accept'); |
||
| 1076 | if (in_array($acceptHeader, ['application/json', 'application/xml'], true)) { |
||
| 1077 | http_response_code(404); |
||
| 1078 | return; |
||
| 1079 | } |
||
| 1080 | |||
| 1081 | // Handle resources that can't be found |
||
| 1082 | // This prevents browsers from redirecting to the default page and then |
||
| 1083 | // attempting to parse HTML as CSS and similar. |
||
| 1084 | $destinationHeader = $request->getHeader('Sec-Fetch-Dest'); |
||
| 1085 | if (in_array($destinationHeader, ['font', 'script', 'style'])) { |
||
| 1086 | http_response_code(404); |
||
| 1087 | return; |
||
| 1088 | } |
||
| 1089 | |||
| 1090 | // Redirect to the default app or login only as an entry point |
||
| 1091 | if ($requestPath === '') { |
||
| 1092 | // Someone is logged in |
||
| 1093 | if (Server::get(IUserSession::class)->isLoggedIn()) { |
||
| 1094 | header('Location: ' . Server::get(IURLGenerator::class)->linkToDefaultPageUrl()); |
||
| 1095 | } else { |
||
| 1096 | // Not handled and not logged in |
||
| 1097 | header('Location: ' . Server::get(IURLGenerator::class)->linkToRouteAbsolute('core.login.showLoginForm')); |
||
| 1098 | } |
||
| 1099 | return; |
||
| 1100 | } |
||
| 1101 | |||
| 1102 | try { |
||
| 1103 | Server::get(\OC\Route\Router::class)->match('/error/404'); |
||
| 1104 | } catch (\Exception $e) { |
||
| 1105 | if (!$e instanceof MethodNotAllowedException) { |
||
| 1106 | logger('core')->emergency($e->getMessage(), ['exception' => $e]); |
||
| 1107 | } |
||
| 1108 | $l = Server::get(\OCP\L10N\IFactory::class)->get('lib'); |
||
| 1109 | OC_Template::printErrorPage( |
||
| 1110 | $l->t('404'), |
||
| 1111 | $l->t('The page could not be found on the server.'), |
||
| 1112 | 404 |
||
| 1113 | ); |
||
| 1114 | } |
||
| 1115 | } |
||
| 1116 | |||
| 1117 | /** |
||
| 1118 | * Check login: apache auth, auth token, basic auth |
||
| 1119 | */ |
||
| 1120 | public static function handleLogin(OCP\IRequest $request): bool { |
||
| 1138 | } |
||
| 1139 | |||
| 1140 | protected static function handleAuthHeaders(): void { |
||
| 1141 | //copy http auth headers for apache+php-fcgid work around |
||
| 1142 | if (isset($_SERVER['HTTP_XAUTHORIZATION']) && !isset($_SERVER['HTTP_AUTHORIZATION'])) { |
||
| 1143 | $_SERVER['HTTP_AUTHORIZATION'] = $_SERVER['HTTP_XAUTHORIZATION']; |
||
| 1144 | } |
||
| 1158 | } |
||
| 1159 | } |
||
| 1160 | } |
||
| 1161 | } |
||
| 1162 | } |
||
| 1163 | |||
| 1164 | OC::init(); |
||
| 1165 |