| Total Complexity | 181 |
| Total Lines | 1054 |
| Duplicated Lines | 0 % |
| Changes | 5 | ||
| Bugs | 0 | 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 |
||
| 92 | class OC { |
||
| 93 | /** |
||
| 94 | * Associative array for autoloading. classname => filename |
||
| 95 | */ |
||
| 96 | public static array $CLASSPATH = []; |
||
| 97 | /** |
||
| 98 | * The installation path for Nextcloud on the server (e.g. /srv/http/nextcloud) |
||
| 99 | */ |
||
| 100 | public static string $SERVERROOT = ''; |
||
| 101 | /** |
||
| 102 | * the current request path relative to the Nextcloud root (e.g. files/index.php) |
||
| 103 | */ |
||
| 104 | private static string $SUBURI = ''; |
||
| 105 | /** |
||
| 106 | * the Nextcloud root path for http requests (e.g. nextcloud/) |
||
| 107 | */ |
||
| 108 | public static string $WEBROOT = ''; |
||
| 109 | /** |
||
| 110 | * The installation path array of the apps folder on the server (e.g. /srv/http/nextcloud) 'path' and |
||
| 111 | * web path in 'url' |
||
| 112 | */ |
||
| 113 | public static array $APPSROOTS = []; |
||
| 114 | |||
| 115 | public static string $configDir; |
||
| 116 | |||
| 117 | /** |
||
| 118 | * requested app |
||
| 119 | */ |
||
| 120 | public static string $REQUESTEDAPP = ''; |
||
| 121 | |||
| 122 | /** |
||
| 123 | * check if Nextcloud runs in cli mode |
||
| 124 | */ |
||
| 125 | public static bool $CLI = false; |
||
| 126 | |||
| 127 | public static \OC\Autoloader $loader; |
||
| 128 | |||
| 129 | public static \Composer\Autoload\ClassLoader $composerAutoloader; |
||
| 130 | |||
| 131 | public static \OC\Server $server; |
||
| 132 | |||
| 133 | private static \OC\Config $config; |
||
| 134 | |||
| 135 | /** |
||
| 136 | * @throws \RuntimeException when the 3rdparty directory is missing or |
||
| 137 | * the app path list is empty or contains an invalid path |
||
| 138 | */ |
||
| 139 | public static function initPaths(): void { |
||
| 140 | if (defined('PHPUNIT_CONFIG_DIR')) { |
||
| 141 | self::$configDir = OC::$SERVERROOT . '/' . PHPUNIT_CONFIG_DIR . '/'; |
||
|
|
|||
| 142 | } elseif (defined('PHPUNIT_RUN') and PHPUNIT_RUN and is_dir(OC::$SERVERROOT . '/tests/config/')) { |
||
| 143 | self::$configDir = OC::$SERVERROOT . '/tests/config/'; |
||
| 144 | } elseif ($dir = getenv('NEXTCLOUD_CONFIG_DIR')) { |
||
| 145 | self::$configDir = rtrim($dir, '/') . '/'; |
||
| 146 | } else { |
||
| 147 | self::$configDir = OC::$SERVERROOT . '/config/'; |
||
| 148 | } |
||
| 149 | self::$config = new \OC\Config(self::$configDir); |
||
| 150 | |||
| 151 | OC::$SUBURI = str_replace("\\", "/", substr(realpath($_SERVER["SCRIPT_FILENAME"] ?? ''), strlen(OC::$SERVERROOT))); |
||
| 152 | /** |
||
| 153 | * FIXME: The following lines are required because we can't yet instantiate |
||
| 154 | * Server::get(\OCP\IRequest::class) since \OC::$server does not yet exist. |
||
| 155 | */ |
||
| 156 | $params = [ |
||
| 157 | 'server' => [ |
||
| 158 | 'SCRIPT_NAME' => $_SERVER['SCRIPT_NAME'] ?? null, |
||
| 159 | 'SCRIPT_FILENAME' => $_SERVER['SCRIPT_FILENAME'] ?? null, |
||
| 160 | ], |
||
| 161 | ]; |
||
| 162 | $fakeRequest = new \OC\AppFramework\Http\Request( |
||
| 163 | $params, |
||
| 164 | new \OC\AppFramework\Http\RequestId($_SERVER['UNIQUE_ID'] ?? '', new \OC\Security\SecureRandom()), |
||
| 165 | new \OC\AllConfig(new \OC\SystemConfig(self::$config)) |
||
| 166 | ); |
||
| 167 | $scriptName = $fakeRequest->getScriptName(); |
||
| 168 | if (substr($scriptName, -1) == '/') { |
||
| 169 | $scriptName .= 'index.php'; |
||
| 170 | //make sure suburi follows the same rules as scriptName |
||
| 171 | if (substr(OC::$SUBURI, -9) != 'index.php') { |
||
| 172 | if (substr(OC::$SUBURI, -1) != '/') { |
||
| 173 | OC::$SUBURI = OC::$SUBURI . '/'; |
||
| 174 | } |
||
| 175 | OC::$SUBURI = OC::$SUBURI . 'index.php'; |
||
| 176 | } |
||
| 177 | } |
||
| 178 | |||
| 179 | |||
| 180 | if (OC::$CLI) { |
||
| 181 | OC::$WEBROOT = self::$config->getValue('overwritewebroot', ''); |
||
| 182 | } else { |
||
| 183 | if (substr($scriptName, 0 - strlen(OC::$SUBURI)) === OC::$SUBURI) { |
||
| 184 | OC::$WEBROOT = substr($scriptName, 0, 0 - strlen(OC::$SUBURI)); |
||
| 185 | |||
| 186 | if (OC::$WEBROOT != '' && OC::$WEBROOT[0] !== '/') { |
||
| 187 | OC::$WEBROOT = '/' . OC::$WEBROOT; |
||
| 188 | } |
||
| 189 | } else { |
||
| 190 | // The scriptName is not ending with OC::$SUBURI |
||
| 191 | // This most likely means that we are calling from CLI. |
||
| 192 | // However some cron jobs still need to generate |
||
| 193 | // a web URL, so we use overwritewebroot as a fallback. |
||
| 194 | OC::$WEBROOT = self::$config->getValue('overwritewebroot', ''); |
||
| 195 | } |
||
| 196 | |||
| 197 | // Resolve /nextcloud to /nextcloud/ to ensure to always have a trailing |
||
| 198 | // slash which is required by URL generation. |
||
| 199 | if (isset($_SERVER['REQUEST_URI']) && $_SERVER['REQUEST_URI'] === \OC::$WEBROOT && |
||
| 200 | substr($_SERVER['REQUEST_URI'], -1) !== '/') { |
||
| 201 | header('Location: '.\OC::$WEBROOT.'/'); |
||
| 202 | exit(); |
||
| 203 | } |
||
| 204 | } |
||
| 205 | |||
| 206 | // search the apps folder |
||
| 207 | $config_paths = self::$config->getValue('apps_paths', []); |
||
| 208 | if (!empty($config_paths)) { |
||
| 209 | foreach ($config_paths as $paths) { |
||
| 210 | if (isset($paths['url']) && isset($paths['path'])) { |
||
| 211 | $paths['url'] = rtrim($paths['url'], '/'); |
||
| 212 | $paths['path'] = rtrim($paths['path'], '/'); |
||
| 213 | OC::$APPSROOTS[] = $paths; |
||
| 214 | } |
||
| 215 | } |
||
| 216 | } elseif (file_exists(OC::$SERVERROOT . '/apps')) { |
||
| 217 | OC::$APPSROOTS[] = ['path' => OC::$SERVERROOT . '/apps', 'url' => '/apps', 'writable' => true]; |
||
| 218 | } |
||
| 219 | |||
| 220 | if (empty(OC::$APPSROOTS)) { |
||
| 221 | throw new \RuntimeException('apps directory not found! Please put the Nextcloud apps folder in the Nextcloud folder' |
||
| 222 | . '. You can also configure the location in the config.php file.'); |
||
| 223 | } |
||
| 224 | $paths = []; |
||
| 225 | foreach (OC::$APPSROOTS as $path) { |
||
| 226 | $paths[] = $path['path']; |
||
| 227 | if (!is_dir($path['path'])) { |
||
| 228 | throw new \RuntimeException(sprintf('App directory "%s" not found! Please put the Nextcloud apps folder in the' |
||
| 229 | . ' Nextcloud folder. You can also configure the location in the config.php file.', $path['path'])); |
||
| 230 | } |
||
| 231 | } |
||
| 232 | |||
| 233 | // set the right include path |
||
| 234 | set_include_path( |
||
| 235 | implode(PATH_SEPARATOR, $paths) |
||
| 236 | ); |
||
| 237 | } |
||
| 238 | |||
| 239 | public static function checkConfig(): void { |
||
| 240 | $l = Server::get(\OCP\L10N\IFactory::class)->get('lib'); |
||
| 241 | |||
| 242 | // Create config if it does not already exist |
||
| 243 | $configFilePath = self::$configDir .'/config.php'; |
||
| 244 | if (!file_exists($configFilePath)) { |
||
| 245 | @touch($configFilePath); |
||
| 246 | } |
||
| 247 | |||
| 248 | // Check if config is writable |
||
| 249 | $configFileWritable = is_writable($configFilePath); |
||
| 250 | if (!$configFileWritable && !OC_Helper::isReadOnlyConfigEnabled() |
||
| 251 | || !$configFileWritable && \OCP\Util::needUpgrade()) { |
||
| 252 | $urlGenerator = Server::get(IURLGenerator::class); |
||
| 253 | |||
| 254 | if (self::$CLI) { |
||
| 255 | echo $l->t('Cannot write into "config" directory!')."\n"; |
||
| 256 | echo $l->t('This can usually be fixed by giving the web server write access to the config directory.')."\n"; |
||
| 257 | echo "\n"; |
||
| 258 | echo $l->t('But, if you prefer to keep config.php file read only, set the option "config_is_read_only" to true in it.')."\n"; |
||
| 259 | echo $l->t('See %s', [ $urlGenerator->linkToDocs('admin-config') ])."\n"; |
||
| 260 | exit; |
||
| 261 | } else { |
||
| 262 | OC_Template::printErrorPage( |
||
| 263 | $l->t('Cannot write into "config" directory!'), |
||
| 264 | $l->t('This can usually be fixed by giving the web server write access to the config directory.') . ' ' |
||
| 265 | . $l->t('But, if you prefer to keep config.php file read only, set the option "config_is_read_only" to true in it.') . ' ' |
||
| 266 | . $l->t('See %s', [ $urlGenerator->linkToDocs('admin-config') ]), |
||
| 267 | 503 |
||
| 268 | ); |
||
| 269 | } |
||
| 270 | } |
||
| 271 | } |
||
| 272 | |||
| 273 | public static function checkInstalled(\OC\SystemConfig $systemConfig): void { |
||
| 274 | if (defined('OC_CONSOLE')) { |
||
| 275 | return; |
||
| 276 | } |
||
| 277 | // Redirect to installer if not installed |
||
| 278 | if (!$systemConfig->getValue('installed', false) && OC::$SUBURI !== '/index.php' && OC::$SUBURI !== '/status.php') { |
||
| 279 | if (OC::$CLI) { |
||
| 280 | throw new Exception('Not installed'); |
||
| 281 | } else { |
||
| 282 | $url = OC::$WEBROOT . '/index.php'; |
||
| 283 | header('Location: ' . $url); |
||
| 284 | } |
||
| 285 | exit(); |
||
| 286 | } |
||
| 287 | } |
||
| 288 | |||
| 289 | public static function checkMaintenanceMode(\OC\SystemConfig $systemConfig): void { |
||
| 290 | // Allow ajax update script to execute without being stopped |
||
| 291 | if (((bool) $systemConfig->getValue('maintenance', false)) && OC::$SUBURI != '/core/ajax/update.php') { |
||
| 292 | // send http status 503 |
||
| 293 | http_response_code(503); |
||
| 294 | header('X-Nextcloud-Maintenance-Mode: 1'); |
||
| 295 | header('Retry-After: 120'); |
||
| 296 | |||
| 297 | // render error page |
||
| 298 | $template = new OC_Template('', 'update.user', 'guest'); |
||
| 299 | \OCP\Util::addScript('core', 'maintenance'); |
||
| 300 | \OCP\Util::addStyle('core', 'guest'); |
||
| 301 | $template->printPage(); |
||
| 302 | die(); |
||
| 303 | } |
||
| 304 | } |
||
| 305 | |||
| 306 | /** |
||
| 307 | * Prints the upgrade page |
||
| 308 | */ |
||
| 309 | private static function printUpgradePage(\OC\SystemConfig $systemConfig): void { |
||
| 310 | $disableWebUpdater = $systemConfig->getValue('upgrade.disable-web', false); |
||
| 311 | $tooBig = false; |
||
| 312 | if (!$disableWebUpdater) { |
||
| 313 | $apps = Server::get(\OCP\App\IAppManager::class); |
||
| 314 | if ($apps->isInstalled('user_ldap')) { |
||
| 315 | $qb = Server::get(\OCP\IDBConnection::class)->getQueryBuilder(); |
||
| 316 | |||
| 317 | $result = $qb->select($qb->func()->count('*', 'user_count')) |
||
| 318 | ->from('ldap_user_mapping') |
||
| 319 | ->executeQuery(); |
||
| 320 | $row = $result->fetch(); |
||
| 321 | $result->closeCursor(); |
||
| 322 | |||
| 323 | $tooBig = ($row['user_count'] > 50); |
||
| 324 | } |
||
| 325 | if (!$tooBig && $apps->isInstalled('user_saml')) { |
||
| 326 | $qb = Server::get(\OCP\IDBConnection::class)->getQueryBuilder(); |
||
| 327 | |||
| 328 | $result = $qb->select($qb->func()->count('*', 'user_count')) |
||
| 329 | ->from('user_saml_users') |
||
| 330 | ->executeQuery(); |
||
| 331 | $row = $result->fetch(); |
||
| 332 | $result->closeCursor(); |
||
| 333 | |||
| 334 | $tooBig = ($row['user_count'] > 50); |
||
| 335 | } |
||
| 336 | if (!$tooBig) { |
||
| 337 | // count users |
||
| 338 | $stats = Server::get(\OCP\IUserManager::class)->countUsers(); |
||
| 339 | $totalUsers = array_sum($stats); |
||
| 340 | $tooBig = ($totalUsers > 50); |
||
| 341 | } |
||
| 342 | } |
||
| 343 | $ignoreTooBigWarning = isset($_GET['IKnowThatThisIsABigInstanceAndTheUpdateRequestCouldRunIntoATimeoutAndHowToRestoreABackup']) && |
||
| 344 | $_GET['IKnowThatThisIsABigInstanceAndTheUpdateRequestCouldRunIntoATimeoutAndHowToRestoreABackup'] === 'IAmSuperSureToDoThis'; |
||
| 345 | |||
| 346 | if ($disableWebUpdater || ($tooBig && !$ignoreTooBigWarning)) { |
||
| 347 | // send http status 503 |
||
| 348 | http_response_code(503); |
||
| 349 | header('Retry-After: 120'); |
||
| 350 | |||
| 351 | // render error page |
||
| 352 | $template = new OC_Template('', 'update.use-cli', 'guest'); |
||
| 353 | $template->assign('productName', 'nextcloud'); // for now |
||
| 354 | $template->assign('version', OC_Util::getVersionString()); |
||
| 355 | $template->assign('tooBig', $tooBig); |
||
| 356 | |||
| 357 | $template->printPage(); |
||
| 358 | die(); |
||
| 359 | } |
||
| 360 | |||
| 361 | // check whether this is a core update or apps update |
||
| 362 | $installedVersion = $systemConfig->getValue('version', '0.0.0'); |
||
| 363 | $currentVersion = implode('.', \OCP\Util::getVersion()); |
||
| 364 | |||
| 365 | // if not a core upgrade, then it's apps upgrade |
||
| 366 | $isAppsOnlyUpgrade = version_compare($currentVersion, $installedVersion, '='); |
||
| 367 | |||
| 368 | $oldTheme = $systemConfig->getValue('theme'); |
||
| 369 | $systemConfig->setValue('theme', ''); |
||
| 370 | \OCP\Util::addScript('core', 'common'); |
||
| 371 | \OCP\Util::addScript('core', 'main'); |
||
| 372 | \OCP\Util::addTranslations('core'); |
||
| 373 | \OCP\Util::addScript('core', 'update'); |
||
| 374 | |||
| 375 | /** @var \OC\App\AppManager $appManager */ |
||
| 376 | $appManager = Server::get(\OCP\App\IAppManager::class); |
||
| 377 | |||
| 378 | $tmpl = new OC_Template('', 'update.admin', 'guest'); |
||
| 379 | $tmpl->assign('version', OC_Util::getVersionString()); |
||
| 380 | $tmpl->assign('isAppsOnlyUpgrade', $isAppsOnlyUpgrade); |
||
| 381 | |||
| 382 | // get third party apps |
||
| 383 | $ocVersion = \OCP\Util::getVersion(); |
||
| 384 | $ocVersion = implode('.', $ocVersion); |
||
| 385 | $incompatibleApps = $appManager->getIncompatibleApps($ocVersion); |
||
| 386 | $incompatibleShippedApps = []; |
||
| 387 | foreach ($incompatibleApps as $appInfo) { |
||
| 388 | if ($appManager->isShipped($appInfo['id'])) { |
||
| 389 | $incompatibleShippedApps[] = $appInfo['name'] . ' (' . $appInfo['id'] . ')'; |
||
| 390 | } |
||
| 391 | } |
||
| 392 | |||
| 393 | if (!empty($incompatibleShippedApps)) { |
||
| 394 | $l = Server::get(\OCP\L10N\IFactory::class)->get('core'); |
||
| 395 | $hint = $l->t('The files of the app %1$s were not replaced correctly. Make sure it is a version compatible with the server.', [implode(', ', $incompatibleShippedApps)]); |
||
| 396 | throw new \OCP\HintException('The files of the app ' . implode(', ', $incompatibleShippedApps) . ' were not replaced correctly. Make sure it is a version compatible with the server.', $hint); |
||
| 397 | } |
||
| 398 | |||
| 399 | $tmpl->assign('appsToUpgrade', $appManager->getAppsNeedingUpgrade($ocVersion)); |
||
| 400 | $tmpl->assign('incompatibleAppsList', $incompatibleApps); |
||
| 401 | try { |
||
| 402 | $defaults = new \OC_Defaults(); |
||
| 403 | $tmpl->assign('productName', $defaults->getName()); |
||
| 404 | } catch (Throwable $error) { |
||
| 405 | $tmpl->assign('productName', 'Nextcloud'); |
||
| 406 | } |
||
| 407 | $tmpl->assign('oldTheme', $oldTheme); |
||
| 408 | $tmpl->printPage(); |
||
| 409 | } |
||
| 410 | |||
| 411 | public static function initSession(): void { |
||
| 412 | $request = Server::get(IRequest::class); |
||
| 413 | $isDavRequest = strpos($request->getRequestUri(), '/remote.php/dav') === 0 || strpos($request->getRequestUri(), '/remote.php/webdav') === 0; |
||
| 414 | if ($request->getHeader('Authorization') !== '' && is_null($request->getCookie('cookie_test')) && $isDavRequest) { |
||
| 415 | setcookie('cookie_test', 'test', time() + 3600); |
||
| 416 | // Do not initialize the session if a request is authenticated directly |
||
| 417 | // unless there is a session cookie already sent along |
||
| 418 | return; |
||
| 419 | } |
||
| 420 | |||
| 421 | if ($request->getServerProtocol() === 'https') { |
||
| 422 | ini_set('session.cookie_secure', 'true'); |
||
| 423 | } |
||
| 424 | |||
| 425 | // prevents javascript from accessing php session cookies |
||
| 426 | ini_set('session.cookie_httponly', 'true'); |
||
| 427 | |||
| 428 | // set the cookie path to the Nextcloud directory |
||
| 429 | $cookie_path = OC::$WEBROOT ? : '/'; |
||
| 430 | ini_set('session.cookie_path', $cookie_path); |
||
| 431 | |||
| 432 | // Let the session name be changed in the initSession Hook |
||
| 433 | $sessionName = OC_Util::getInstanceId(); |
||
| 434 | |||
| 435 | try { |
||
| 436 | // set the session name to the instance id - which is unique |
||
| 437 | $session = new \OC\Session\Internal($sessionName); |
||
| 438 | |||
| 439 | $cryptoWrapper = Server::get(\OC\Session\CryptoWrapper::class); |
||
| 440 | $session = $cryptoWrapper->wrapSession($session); |
||
| 441 | self::$server->setSession($session); |
||
| 442 | |||
| 443 | // if session can't be started break with http 500 error |
||
| 444 | } catch (Exception $e) { |
||
| 445 | Server::get(LoggerInterface::class)->error($e->getMessage(), ['app' => 'base','exception' => $e]); |
||
| 446 | //show the user a detailed error page |
||
| 447 | OC_Template::printExceptionErrorPage($e, 500); |
||
| 448 | die(); |
||
| 449 | } |
||
| 450 | |||
| 451 | //try to set the session lifetime |
||
| 452 | $sessionLifeTime = self::getSessionLifeTime(); |
||
| 453 | @ini_set('gc_maxlifetime', (string)$sessionLifeTime); |
||
| 454 | |||
| 455 | // session timeout |
||
| 456 | if ($session->exists('LAST_ACTIVITY') && (time() - $session->get('LAST_ACTIVITY') > $sessionLifeTime)) { |
||
| 457 | if (isset($_COOKIE[session_name()])) { |
||
| 458 | setcookie(session_name(), '', -1, self::$WEBROOT ? : '/'); |
||
| 459 | } |
||
| 460 | Server::get(IUserSession::class)->logout(); |
||
| 461 | } |
||
| 462 | |||
| 463 | if (!self::hasSessionRelaxedExpiry()) { |
||
| 464 | $session->set('LAST_ACTIVITY', time()); |
||
| 465 | } |
||
| 466 | $session->close(); |
||
| 467 | } |
||
| 468 | |||
| 469 | private static function getSessionLifeTime(): int { |
||
| 470 | return Server::get(\OC\AllConfig::class)->getSystemValueInt('session_lifetime', 60 * 60 * 24); |
||
| 471 | } |
||
| 472 | |||
| 473 | /** |
||
| 474 | * @return bool true if the session expiry should only be done by gc instead of an explicit timeout |
||
| 475 | */ |
||
| 476 | public static function hasSessionRelaxedExpiry(): bool { |
||
| 477 | return Server::get(\OC\AllConfig::class)->getSystemValueBool('session_relaxed_expiry', false); |
||
| 478 | } |
||
| 479 | |||
| 480 | /** |
||
| 481 | * Try to set some values to the required Nextcloud default |
||
| 482 | */ |
||
| 483 | public static function setRequiredIniValues(): void { |
||
| 486 | } |
||
| 487 | |||
| 488 | /** |
||
| 489 | * Send the same site cookies |
||
| 490 | */ |
||
| 491 | private static function sendSameSiteCookies(): void { |
||
| 515 | ); |
||
| 516 | } |
||
| 517 | } |
||
| 518 | |||
| 519 | /** |
||
| 520 | * Same Site cookie to further mitigate CSRF attacks. This cookie has to |
||
| 521 | * be set in every request if cookies are sent to add a second level of |
||
| 522 | * defense against CSRF. |
||
| 523 | * |
||
| 524 | * If the cookie is not sent this will set the cookie and reload the page. |
||
| 525 | * We use an additional cookie since we want to protect logout CSRF and |
||
| 526 | * also we can't directly interfere with PHP's session mechanism. |
||
| 527 | */ |
||
| 528 | private static function performSameSiteCookieProtection(\OCP\IConfig $config): void { |
||
| 573 | } |
||
| 574 | } |
||
| 575 | |||
| 576 | public static function init(): void { |
||
| 838 | }); |
||
| 839 | } |
||
| 840 | |||
| 841 | /** |
||
| 842 | * register hooks for the cleanup of cache and bruteforce protection |
||
| 843 | */ |
||
| 844 | public static function registerCleanupHooks(\OC\SystemConfig $systemConfig): void { |
||
| 872 | ]); |
||
| 873 | } |
||
| 874 | }); |
||
| 875 | } |
||
| 876 | } |
||
| 877 | |||
| 878 | private static function registerEncryptionWrapperAndHooks(): void { |
||
| 879 | $manager = Server::get(\OCP\Encryption\IManager::class); |
||
| 880 | \OCP\Util::connectHook('OC_Filesystem', 'preSetup', $manager, 'setupStorage'); |
||
| 881 | |||
| 882 | $enabled = $manager->isEnabled(); |
||
| 883 | if ($enabled) { |
||
| 884 | \OCP\Util::connectHook(Share::class, 'post_shared', HookManager::class, 'postShared'); |
||
| 885 | \OCP\Util::connectHook(Share::class, 'post_unshare', HookManager::class, 'postUnshared'); |
||
| 886 | \OCP\Util::connectHook('OC_Filesystem', 'post_rename', HookManager::class, 'postRename'); |
||
| 887 | \OCP\Util::connectHook('\OCA\Files_Trashbin\Trashbin', 'post_restore', HookManager::class, 'postRestore'); |
||
| 888 | } |
||
| 889 | } |
||
| 890 | |||
| 891 | private static function registerAccountHooks(): void { |
||
| 892 | /** @var IEventDispatcher $dispatcher */ |
||
| 893 | $dispatcher = Server::get(IEventDispatcher::class); |
||
| 894 | $dispatcher->addServiceListener(UserChangedEvent::class, \OC\Accounts\Hooks::class); |
||
| 895 | } |
||
| 896 | |||
| 897 | private static function registerAppRestrictionsHooks(): void { |
||
| 898 | /** @var \OC\Group\Manager $groupManager */ |
||
| 899 | $groupManager = Server::get(\OCP\IGroupManager::class); |
||
| 900 | $groupManager->listen('\OC\Group', 'postDelete', function (\OCP\IGroup $group) { |
||
| 901 | $appManager = Server::get(\OCP\App\IAppManager::class); |
||
| 902 | $apps = $appManager->getEnabledAppsForGroup($group); |
||
| 903 | foreach ($apps as $appId) { |
||
| 904 | $restrictions = $appManager->getAppRestriction($appId); |
||
| 905 | if (empty($restrictions)) { |
||
| 906 | continue; |
||
| 907 | } |
||
| 908 | $key = array_search($group->getGID(), $restrictions); |
||
| 909 | unset($restrictions[$key]); |
||
| 910 | $restrictions = array_values($restrictions); |
||
| 911 | if (empty($restrictions)) { |
||
| 912 | $appManager->disableApp($appId); |
||
| 913 | } else { |
||
| 914 | $appManager->enableAppForGroups($appId, $restrictions); |
||
| 915 | } |
||
| 916 | } |
||
| 917 | }); |
||
| 918 | } |
||
| 919 | |||
| 920 | private static function registerResourceCollectionHooks(): void { |
||
| 921 | \OC\Collaboration\Resources\Listener::register(Server::get(SymfonyAdapter::class), Server::get(IEventDispatcher::class)); |
||
| 922 | } |
||
| 923 | |||
| 924 | private static function registerFileReferenceEventListener(): void { |
||
| 925 | \OC\Collaboration\Reference\File\FileReferenceEventListener::register(Server::get(IEventDispatcher::class)); |
||
| 926 | } |
||
| 927 | |||
| 928 | /** |
||
| 929 | * register hooks for sharing |
||
| 930 | */ |
||
| 931 | public static function registerShareHooks(\OC\SystemConfig $systemConfig): void { |
||
| 932 | if ($systemConfig->getValue('installed')) { |
||
| 933 | OC_Hook::connect('OC_User', 'post_deleteUser', Hooks::class, 'post_deleteUser'); |
||
| 934 | OC_Hook::connect('OC_User', 'post_deleteGroup', Hooks::class, 'post_deleteGroup'); |
||
| 935 | |||
| 936 | /** @var IEventDispatcher $dispatcher */ |
||
| 937 | $dispatcher = Server::get(IEventDispatcher::class); |
||
| 938 | $dispatcher->addServiceListener(UserRemovedEvent::class, \OC\Share20\UserRemovedListener::class); |
||
| 939 | } |
||
| 940 | } |
||
| 941 | |||
| 942 | protected static function registerAutoloaderCache(\OC\SystemConfig $systemConfig): void { |
||
| 943 | // The class loader takes an optional low-latency cache, which MUST be |
||
| 944 | // namespaced. The instanceid is used for namespacing, but might be |
||
| 945 | // unavailable at this point. Furthermore, it might not be possible to |
||
| 946 | // generate an instanceid via \OC_Util::getInstanceId() because the |
||
| 947 | // config file may not be writable. As such, we only register a class |
||
| 948 | // loader cache if instanceid is available without trying to create one. |
||
| 949 | $instanceId = $systemConfig->getValue('instanceid', null); |
||
| 950 | if ($instanceId) { |
||
| 951 | try { |
||
| 952 | $memcacheFactory = Server::get(\OCP\ICacheFactory::class); |
||
| 953 | self::$loader->setMemoryCache($memcacheFactory->createLocal('Autoloader')); |
||
| 954 | } catch (\Exception $ex) { |
||
| 955 | } |
||
| 956 | } |
||
| 957 | } |
||
| 958 | |||
| 959 | /** |
||
| 960 | * Handle the request |
||
| 961 | */ |
||
| 962 | public static function handleRequest(): void { |
||
| 963 | Server::get(\OCP\Diagnostics\IEventLogger::class)->start('handle_request', 'Handle request'); |
||
| 964 | $systemConfig = Server::get(\OC\SystemConfig::class); |
||
| 965 | |||
| 966 | // Check if Nextcloud is installed or in maintenance (update) mode |
||
| 967 | if (!$systemConfig->getValue('installed', false)) { |
||
| 968 | \OC::$server->getSession()->clear(); |
||
| 969 | $setupHelper = new OC\Setup( |
||
| 970 | $systemConfig, |
||
| 971 | Server::get(\bantu\IniGetWrapper\IniGetWrapper::class), |
||
| 972 | Server::get(\OCP\L10N\IFactory::class)->get('lib'), |
||
| 973 | Server::get(\OCP\Defaults::class), |
||
| 974 | Server::get(\Psr\Log\LoggerInterface::class), |
||
| 975 | Server::get(\OCP\Security\ISecureRandom::class), |
||
| 976 | Server::get(\OC\Installer::class) |
||
| 977 | ); |
||
| 978 | $controller = new OC\Core\Controller\SetupController($setupHelper); |
||
| 979 | $controller->run($_POST); |
||
| 980 | exit(); |
||
| 981 | } |
||
| 982 | |||
| 983 | $request = Server::get(IRequest::class); |
||
| 984 | $requestPath = $request->getRawPathInfo(); |
||
| 985 | if ($requestPath === '/heartbeat') { |
||
| 986 | return; |
||
| 987 | } |
||
| 988 | if (substr($requestPath, -3) !== '.js') { // we need these files during the upgrade |
||
| 989 | self::checkMaintenanceMode($systemConfig); |
||
| 990 | |||
| 991 | if (\OCP\Util::needUpgrade()) { |
||
| 992 | if (function_exists('opcache_reset')) { |
||
| 993 | opcache_reset(); |
||
| 994 | } |
||
| 995 | if (!((bool) $systemConfig->getValue('maintenance', false))) { |
||
| 996 | self::printUpgradePage($systemConfig); |
||
| 997 | exit(); |
||
| 998 | } |
||
| 999 | } |
||
| 1000 | } |
||
| 1001 | |||
| 1002 | // emergency app disabling |
||
| 1003 | if ($requestPath === '/disableapp' |
||
| 1004 | && $request->getMethod() === 'POST' |
||
| 1005 | ) { |
||
| 1006 | \OC_JSON::callCheck(); |
||
| 1007 | \OC_JSON::checkAdminUser(); |
||
| 1008 | $appIds = (array)$request->getParam('appid'); |
||
| 1009 | foreach ($appIds as $appId) { |
||
| 1010 | $appId = \OC_App::cleanAppId($appId); |
||
| 1011 | Server::get(\OCP\App\IAppManager::class)->disableApp($appId); |
||
| 1012 | } |
||
| 1013 | \OC_JSON::success(); |
||
| 1014 | exit(); |
||
| 1015 | } |
||
| 1016 | |||
| 1017 | // Always load authentication apps |
||
| 1018 | OC_App::loadApps(['authentication']); |
||
| 1019 | |||
| 1020 | // Load minimum set of apps |
||
| 1021 | if (!\OCP\Util::needUpgrade() |
||
| 1022 | && !((bool) $systemConfig->getValue('maintenance', false))) { |
||
| 1023 | // For logged-in users: Load everything |
||
| 1024 | if (Server::get(IUserSession::class)->isLoggedIn()) { |
||
| 1025 | OC_App::loadApps(); |
||
| 1026 | } else { |
||
| 1027 | // For guests: Load only filesystem and logging |
||
| 1028 | OC_App::loadApps(['filesystem', 'logging']); |
||
| 1029 | |||
| 1030 | // Don't try to login when a client is trying to get a OAuth token. |
||
| 1031 | // OAuth needs to support basic auth too, so the login is not valid |
||
| 1032 | // inside Nextcloud and the Login exception would ruin it. |
||
| 1033 | if ($request->getRawPathInfo() !== '/apps/oauth2/api/v1/token') { |
||
| 1034 | self::handleLogin($request); |
||
| 1035 | } |
||
| 1036 | } |
||
| 1037 | } |
||
| 1038 | |||
| 1039 | if (!self::$CLI) { |
||
| 1040 | try { |
||
| 1041 | if (!((bool) $systemConfig->getValue('maintenance', false)) && !\OCP\Util::needUpgrade()) { |
||
| 1042 | OC_App::loadApps(['filesystem', 'logging']); |
||
| 1043 | OC_App::loadApps(); |
||
| 1044 | } |
||
| 1045 | Server::get(\OC\Route\Router::class)->match($request->getRawPathInfo()); |
||
| 1046 | return; |
||
| 1047 | } catch (Symfony\Component\Routing\Exception\ResourceNotFoundException $e) { |
||
| 1048 | //header('HTTP/1.0 404 Not Found'); |
||
| 1049 | } catch (Symfony\Component\Routing\Exception\MethodNotAllowedException $e) { |
||
| 1050 | http_response_code(405); |
||
| 1051 | return; |
||
| 1052 | } |
||
| 1053 | } |
||
| 1054 | |||
| 1055 | // Handle WebDAV |
||
| 1056 | if (isset($_SERVER['REQUEST_METHOD']) && $_SERVER['REQUEST_METHOD'] === 'PROPFIND') { |
||
| 1057 | // not allowed any more to prevent people |
||
| 1058 | // mounting this root directly. |
||
| 1059 | // Users need to mount remote.php/webdav instead. |
||
| 1060 | http_response_code(405); |
||
| 1061 | return; |
||
| 1062 | } |
||
| 1063 | |||
| 1064 | // Handle requests for JSON or XML |
||
| 1065 | $acceptHeader = $request->getHeader('Accept'); |
||
| 1066 | if (in_array($acceptHeader, ['application/json', 'application/xml'], true)) { |
||
| 1067 | http_response_code(404); |
||
| 1068 | return; |
||
| 1069 | } |
||
| 1070 | |||
| 1071 | // Handle resources that can't be found |
||
| 1072 | // This prevents browsers from redirecting to the default page and then |
||
| 1073 | // attempting to parse HTML as CSS and similar. |
||
| 1074 | $destinationHeader = $request->getHeader('Sec-Fetch-Dest'); |
||
| 1075 | if (in_array($destinationHeader, ['font', 'script', 'style'])) { |
||
| 1076 | http_response_code(404); |
||
| 1077 | return; |
||
| 1078 | } |
||
| 1079 | |||
| 1080 | // Redirect to the default app or login only as an entry point |
||
| 1081 | if ($requestPath === '') { |
||
| 1082 | // Someone is logged in |
||
| 1083 | if (Server::get(IUserSession::class)->isLoggedIn()) { |
||
| 1084 | header('Location: ' . Server::get(IURLGenerator::class)->linkToDefaultPageUrl()); |
||
| 1085 | } else { |
||
| 1086 | // Not handled and not logged in |
||
| 1087 | header('Location: ' . Server::get(IURLGenerator::class)->linkToRouteAbsolute('core.login.showLoginForm')); |
||
| 1088 | } |
||
| 1089 | return; |
||
| 1090 | } |
||
| 1091 | |||
| 1092 | try { |
||
| 1093 | Server::get(\OC\Route\Router::class)->match('/error/404'); |
||
| 1094 | } catch (\Exception $e) { |
||
| 1095 | logger('core')->emergency($e->getMessage(), ['exception' => $e]); |
||
| 1096 | $l = Server::get(\OCP\L10N\IFactory::class)->get('lib'); |
||
| 1097 | OC_Template::printErrorPage( |
||
| 1098 | $l->t('404'), |
||
| 1099 | $l->t('The page could not be found on the server.'), |
||
| 1100 | 404 |
||
| 1101 | ); |
||
| 1102 | } |
||
| 1103 | } |
||
| 1104 | |||
| 1105 | /** |
||
| 1106 | * Check login: apache auth, auth token, basic auth |
||
| 1107 | */ |
||
| 1108 | public static function handleLogin(OCP\IRequest $request): bool { |
||
| 1126 | } |
||
| 1127 | |||
| 1128 | protected static function handleAuthHeaders(): void { |
||
| 1129 | //copy http auth headers for apache+php-fcgid work around |
||
| 1130 | if (isset($_SERVER['HTTP_XAUTHORIZATION']) && !isset($_SERVER['HTTP_AUTHORIZATION'])) { |
||
| 1131 | $_SERVER['HTTP_AUTHORIZATION'] = $_SERVER['HTTP_XAUTHORIZATION']; |
||
| 1132 | } |
||
| 1146 | } |
||
| 1147 | } |
||
| 1148 | } |
||
| 1149 | } |
||
| 1150 | } |
||
| 1151 | |||
| 1152 | OC::init(); |
||
| 1153 |