@@ -77,1005 +77,1005 @@ |
||
77 | 77 | * OC_autoload! |
78 | 78 | */ |
79 | 79 | class OC { |
80 | - /** |
|
81 | - * Associative array for autoloading. classname => filename |
|
82 | - */ |
|
83 | - public static $CLASSPATH = []; |
|
84 | - /** |
|
85 | - * The installation path for Nextcloud on the server (e.g. /srv/http/nextcloud) |
|
86 | - */ |
|
87 | - public static $SERVERROOT = ''; |
|
88 | - /** |
|
89 | - * the current request path relative to the Nextcloud root (e.g. files/index.php) |
|
90 | - */ |
|
91 | - private static $SUBURI = ''; |
|
92 | - /** |
|
93 | - * the Nextcloud root path for http requests (e.g. nextcloud/) |
|
94 | - */ |
|
95 | - public static $WEBROOT = ''; |
|
96 | - /** |
|
97 | - * The installation path array of the apps folder on the server (e.g. /srv/http/nextcloud) 'path' and |
|
98 | - * web path in 'url' |
|
99 | - */ |
|
100 | - public static $APPSROOTS = []; |
|
101 | - |
|
102 | - /** |
|
103 | - * @var string |
|
104 | - */ |
|
105 | - public static $configDir; |
|
106 | - |
|
107 | - /** |
|
108 | - * requested app |
|
109 | - */ |
|
110 | - public static $REQUESTEDAPP = ''; |
|
111 | - |
|
112 | - /** |
|
113 | - * check if Nextcloud runs in cli mode |
|
114 | - */ |
|
115 | - public static $CLI = false; |
|
116 | - |
|
117 | - /** |
|
118 | - * @var \OC\Autoloader $loader |
|
119 | - */ |
|
120 | - public static $loader = null; |
|
121 | - |
|
122 | - /** @var \Composer\Autoload\ClassLoader $composerAutoloader */ |
|
123 | - public static $composerAutoloader = null; |
|
124 | - |
|
125 | - /** |
|
126 | - * @var \OC\Server |
|
127 | - */ |
|
128 | - public static $server = null; |
|
129 | - |
|
130 | - /** |
|
131 | - * @var \OC\Config |
|
132 | - */ |
|
133 | - private static $config = null; |
|
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() { |
|
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 | - * \OC::$server->getRequest() since \OC::$server does not yet exist. |
|
155 | - */ |
|
156 | - $params = [ |
|
157 | - 'server' => [ |
|
158 | - 'SCRIPT_NAME' => $_SERVER['SCRIPT_NAME'], |
|
159 | - 'SCRIPT_FILENAME' => $_SERVER['SCRIPT_FILENAME'], |
|
160 | - ], |
|
161 | - ]; |
|
162 | - $fakeRequest = new \OC\AppFramework\Http\Request($params, new \OC\Security\SecureRandom(), new \OC\AllConfig(new \OC\SystemConfig(self::$config))); |
|
163 | - $scriptName = $fakeRequest->getScriptName(); |
|
164 | - if (substr($scriptName, -1) == '/') { |
|
165 | - $scriptName .= 'index.php'; |
|
166 | - //make sure suburi follows the same rules as scriptName |
|
167 | - if (substr(OC::$SUBURI, -9) != 'index.php') { |
|
168 | - if (substr(OC::$SUBURI, -1) != '/') { |
|
169 | - OC::$SUBURI = OC::$SUBURI . '/'; |
|
170 | - } |
|
171 | - OC::$SUBURI = OC::$SUBURI . 'index.php'; |
|
172 | - } |
|
173 | - } |
|
174 | - |
|
175 | - |
|
176 | - if (OC::$CLI) { |
|
177 | - OC::$WEBROOT = self::$config->getValue('overwritewebroot', ''); |
|
178 | - } else { |
|
179 | - if (substr($scriptName, 0 - strlen(OC::$SUBURI)) === OC::$SUBURI) { |
|
180 | - OC::$WEBROOT = substr($scriptName, 0, 0 - strlen(OC::$SUBURI)); |
|
181 | - |
|
182 | - if (OC::$WEBROOT != '' && OC::$WEBROOT[0] !== '/') { |
|
183 | - OC::$WEBROOT = '/' . OC::$WEBROOT; |
|
184 | - } |
|
185 | - } else { |
|
186 | - // The scriptName is not ending with OC::$SUBURI |
|
187 | - // This most likely means that we are calling from CLI. |
|
188 | - // However some cron jobs still need to generate |
|
189 | - // a web URL, so we use overwritewebroot as a fallback. |
|
190 | - OC::$WEBROOT = self::$config->getValue('overwritewebroot', ''); |
|
191 | - } |
|
192 | - |
|
193 | - // Resolve /nextcloud to /nextcloud/ to ensure to always have a trailing |
|
194 | - // slash which is required by URL generation. |
|
195 | - if (isset($_SERVER['REQUEST_URI']) && $_SERVER['REQUEST_URI'] === \OC::$WEBROOT && |
|
196 | - substr($_SERVER['REQUEST_URI'], -1) !== '/') { |
|
197 | - header('Location: '.\OC::$WEBROOT.'/'); |
|
198 | - exit(); |
|
199 | - } |
|
200 | - } |
|
201 | - |
|
202 | - // search the apps folder |
|
203 | - $config_paths = self::$config->getValue('apps_paths', []); |
|
204 | - if (!empty($config_paths)) { |
|
205 | - foreach ($config_paths as $paths) { |
|
206 | - if (isset($paths['url']) && isset($paths['path'])) { |
|
207 | - $paths['url'] = rtrim($paths['url'], '/'); |
|
208 | - $paths['path'] = rtrim($paths['path'], '/'); |
|
209 | - OC::$APPSROOTS[] = $paths; |
|
210 | - } |
|
211 | - } |
|
212 | - } elseif (file_exists(OC::$SERVERROOT . '/apps')) { |
|
213 | - OC::$APPSROOTS[] = ['path' => OC::$SERVERROOT . '/apps', 'url' => '/apps', 'writable' => true]; |
|
214 | - } |
|
215 | - |
|
216 | - if (empty(OC::$APPSROOTS)) { |
|
217 | - throw new \RuntimeException('apps directory not found! Please put the Nextcloud apps folder in the Nextcloud folder' |
|
218 | - . '. You can also configure the location in the config.php file.'); |
|
219 | - } |
|
220 | - $paths = []; |
|
221 | - foreach (OC::$APPSROOTS as $path) { |
|
222 | - $paths[] = $path['path']; |
|
223 | - if (!is_dir($path['path'])) { |
|
224 | - throw new \RuntimeException(sprintf('App directory "%s" not found! Please put the Nextcloud apps folder in the' |
|
225 | - . ' Nextcloud folder. You can also configure the location in the config.php file.', $path['path'])); |
|
226 | - } |
|
227 | - } |
|
228 | - |
|
229 | - // set the right include path |
|
230 | - set_include_path( |
|
231 | - implode(PATH_SEPARATOR, $paths) |
|
232 | - ); |
|
233 | - } |
|
234 | - |
|
235 | - public static function checkConfig() { |
|
236 | - $l = \OC::$server->getL10N('lib'); |
|
237 | - |
|
238 | - // Create config if it does not already exist |
|
239 | - $configFilePath = self::$configDir .'/config.php'; |
|
240 | - if (!file_exists($configFilePath)) { |
|
241 | - @touch($configFilePath); |
|
242 | - } |
|
243 | - |
|
244 | - // Check if config is writable |
|
245 | - $configFileWritable = is_writable($configFilePath); |
|
246 | - if (!$configFileWritable && !OC_Helper::isReadOnlyConfigEnabled() |
|
247 | - || !$configFileWritable && \OCP\Util::needUpgrade()) { |
|
248 | - $urlGenerator = \OC::$server->getURLGenerator(); |
|
249 | - |
|
250 | - if (self::$CLI) { |
|
251 | - echo $l->t('Cannot write into "config" directory!')."\n"; |
|
252 | - echo $l->t('This can usually be fixed by giving the webserver write access to the config directory.')."\n"; |
|
253 | - echo "\n"; |
|
254 | - 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"; |
|
255 | - echo $l->t('See %s', [ $urlGenerator->linkToDocs('admin-config') ])."\n"; |
|
256 | - exit; |
|
257 | - } else { |
|
258 | - OC_Template::printErrorPage( |
|
259 | - $l->t('Cannot write into "config" directory!'), |
|
260 | - $l->t('This can usually be fixed by giving the webserver write access to the config directory.') . ' ' |
|
261 | - . $l->t('But, if you prefer to keep config.php file read only, set the option "config_is_read_only" to true in it.') . ' ' |
|
262 | - . $l->t('See %s', [ $urlGenerator->linkToDocs('admin-config') ]), |
|
263 | - 503 |
|
264 | - ); |
|
265 | - } |
|
266 | - } |
|
267 | - } |
|
268 | - |
|
269 | - public static function checkInstalled(\OC\SystemConfig $systemConfig) { |
|
270 | - if (defined('OC_CONSOLE')) { |
|
271 | - return; |
|
272 | - } |
|
273 | - // Redirect to installer if not installed |
|
274 | - if (!$systemConfig->getValue('installed', false) && OC::$SUBURI !== '/index.php' && OC::$SUBURI !== '/status.php') { |
|
275 | - if (OC::$CLI) { |
|
276 | - throw new Exception('Not installed'); |
|
277 | - } else { |
|
278 | - $url = OC::$WEBROOT . '/index.php'; |
|
279 | - header('Location: ' . $url); |
|
280 | - } |
|
281 | - exit(); |
|
282 | - } |
|
283 | - } |
|
284 | - |
|
285 | - public static function checkMaintenanceMode(\OC\SystemConfig $systemConfig) { |
|
286 | - // Allow ajax update script to execute without being stopped |
|
287 | - if (((bool) $systemConfig->getValue('maintenance', false)) && OC::$SUBURI != '/core/ajax/update.php') { |
|
288 | - // send http status 503 |
|
289 | - http_response_code(503); |
|
290 | - header('Retry-After: 120'); |
|
291 | - |
|
292 | - // render error page |
|
293 | - $template = new OC_Template('', 'update.user', 'guest'); |
|
294 | - OC_Util::addScript('dist/maintenance'); |
|
295 | - OC_Util::addStyle('core', 'guest'); |
|
296 | - $template->printPage(); |
|
297 | - die(); |
|
298 | - } |
|
299 | - } |
|
300 | - |
|
301 | - /** |
|
302 | - * Prints the upgrade page |
|
303 | - * |
|
304 | - * @param \OC\SystemConfig $systemConfig |
|
305 | - */ |
|
306 | - private static function printUpgradePage(\OC\SystemConfig $systemConfig) { |
|
307 | - $disableWebUpdater = $systemConfig->getValue('upgrade.disable-web', false); |
|
308 | - $tooBig = false; |
|
309 | - if (!$disableWebUpdater) { |
|
310 | - $apps = \OC::$server->getAppManager(); |
|
311 | - if ($apps->isInstalled('user_ldap')) { |
|
312 | - $qb = \OC::$server->getDatabaseConnection()->getQueryBuilder(); |
|
313 | - |
|
314 | - $result = $qb->select($qb->func()->count('*', 'user_count')) |
|
315 | - ->from('ldap_user_mapping') |
|
316 | - ->execute(); |
|
317 | - $row = $result->fetch(); |
|
318 | - $result->closeCursor(); |
|
319 | - |
|
320 | - $tooBig = ($row['user_count'] > 50); |
|
321 | - } |
|
322 | - if (!$tooBig && $apps->isInstalled('user_saml')) { |
|
323 | - $qb = \OC::$server->getDatabaseConnection()->getQueryBuilder(); |
|
324 | - |
|
325 | - $result = $qb->select($qb->func()->count('*', 'user_count')) |
|
326 | - ->from('user_saml_users') |
|
327 | - ->execute(); |
|
328 | - $row = $result->fetch(); |
|
329 | - $result->closeCursor(); |
|
330 | - |
|
331 | - $tooBig = ($row['user_count'] > 50); |
|
332 | - } |
|
333 | - if (!$tooBig) { |
|
334 | - // count users |
|
335 | - $stats = \OC::$server->getUserManager()->countUsers(); |
|
336 | - $totalUsers = array_sum($stats); |
|
337 | - $tooBig = ($totalUsers > 50); |
|
338 | - } |
|
339 | - } |
|
340 | - $ignoreTooBigWarning = isset($_GET['IKnowThatThisIsABigInstanceAndTheUpdateRequestCouldRunIntoATimeoutAndHowToRestoreABackup']) && |
|
341 | - $_GET['IKnowThatThisIsABigInstanceAndTheUpdateRequestCouldRunIntoATimeoutAndHowToRestoreABackup'] === 'IAmSuperSureToDoThis'; |
|
342 | - |
|
343 | - if ($disableWebUpdater || ($tooBig && !$ignoreTooBigWarning)) { |
|
344 | - // send http status 503 |
|
345 | - http_response_code(503); |
|
346 | - header('Retry-After: 120'); |
|
347 | - |
|
348 | - // render error page |
|
349 | - $template = new OC_Template('', 'update.use-cli', 'guest'); |
|
350 | - $template->assign('productName', 'nextcloud'); // for now |
|
351 | - $template->assign('version', OC_Util::getVersionString()); |
|
352 | - $template->assign('tooBig', $tooBig); |
|
353 | - |
|
354 | - $template->printPage(); |
|
355 | - die(); |
|
356 | - } |
|
357 | - |
|
358 | - // check whether this is a core update or apps update |
|
359 | - $installedVersion = $systemConfig->getValue('version', '0.0.0'); |
|
360 | - $currentVersion = implode('.', \OCP\Util::getVersion()); |
|
361 | - |
|
362 | - // if not a core upgrade, then it's apps upgrade |
|
363 | - $isAppsOnlyUpgrade = version_compare($currentVersion, $installedVersion, '='); |
|
364 | - |
|
365 | - $oldTheme = $systemConfig->getValue('theme'); |
|
366 | - $systemConfig->setValue('theme', ''); |
|
367 | - OC_Util::addScript('update'); |
|
368 | - |
|
369 | - /** @var \OC\App\AppManager $appManager */ |
|
370 | - $appManager = \OC::$server->getAppManager(); |
|
371 | - |
|
372 | - $tmpl = new OC_Template('', 'update.admin', 'guest'); |
|
373 | - $tmpl->assign('version', OC_Util::getVersionString()); |
|
374 | - $tmpl->assign('isAppsOnlyUpgrade', $isAppsOnlyUpgrade); |
|
375 | - |
|
376 | - // get third party apps |
|
377 | - $ocVersion = \OCP\Util::getVersion(); |
|
378 | - $ocVersion = implode('.', $ocVersion); |
|
379 | - $incompatibleApps = $appManager->getIncompatibleApps($ocVersion); |
|
380 | - $incompatibleShippedApps = []; |
|
381 | - foreach ($incompatibleApps as $appInfo) { |
|
382 | - if ($appManager->isShipped($appInfo['id'])) { |
|
383 | - $incompatibleShippedApps[] = $appInfo['name'] . ' (' . $appInfo['id'] . ')'; |
|
384 | - } |
|
385 | - } |
|
386 | - |
|
387 | - if (!empty($incompatibleShippedApps)) { |
|
388 | - $l = \OC::$server->getL10N('core'); |
|
389 | - $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)]); |
|
390 | - 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); |
|
391 | - } |
|
392 | - |
|
393 | - $tmpl->assign('appsToUpgrade', $appManager->getAppsNeedingUpgrade($ocVersion)); |
|
394 | - $tmpl->assign('incompatibleAppsList', $incompatibleApps); |
|
395 | - try { |
|
396 | - $defaults = new \OC_Defaults(); |
|
397 | - $tmpl->assign('productName', $defaults->getName()); |
|
398 | - } catch (Throwable $error) { |
|
399 | - $tmpl->assign('productName', 'Nextcloud'); |
|
400 | - } |
|
401 | - $tmpl->assign('oldTheme', $oldTheme); |
|
402 | - $tmpl->printPage(); |
|
403 | - } |
|
404 | - |
|
405 | - public static function initSession() { |
|
406 | - if (self::$server->getRequest()->getServerProtocol() === 'https') { |
|
407 | - ini_set('session.cookie_secure', 'true'); |
|
408 | - } |
|
409 | - |
|
410 | - // prevents javascript from accessing php session cookies |
|
411 | - ini_set('session.cookie_httponly', 'true'); |
|
412 | - |
|
413 | - // set the cookie path to the Nextcloud directory |
|
414 | - $cookie_path = OC::$WEBROOT ? : '/'; |
|
415 | - ini_set('session.cookie_path', $cookie_path); |
|
416 | - |
|
417 | - // Let the session name be changed in the initSession Hook |
|
418 | - $sessionName = OC_Util::getInstanceId(); |
|
419 | - |
|
420 | - try { |
|
421 | - // set the session name to the instance id - which is unique |
|
422 | - $session = new \OC\Session\Internal($sessionName); |
|
423 | - |
|
424 | - $cryptoWrapper = \OC::$server->getSessionCryptoWrapper(); |
|
425 | - $session = $cryptoWrapper->wrapSession($session); |
|
426 | - self::$server->setSession($session); |
|
427 | - |
|
428 | - // if session can't be started break with http 500 error |
|
429 | - } catch (Exception $e) { |
|
430 | - \OC::$server->getLogger()->logException($e, ['app' => 'base']); |
|
431 | - //show the user a detailed error page |
|
432 | - OC_Template::printExceptionErrorPage($e, 500); |
|
433 | - die(); |
|
434 | - } |
|
435 | - |
|
436 | - $sessionLifeTime = self::getSessionLifeTime(); |
|
437 | - |
|
438 | - // session timeout |
|
439 | - if ($session->exists('LAST_ACTIVITY') && (time() - $session->get('LAST_ACTIVITY') > $sessionLifeTime)) { |
|
440 | - if (isset($_COOKIE[session_name()])) { |
|
441 | - setcookie(session_name(), '', -1, self::$WEBROOT ? : '/'); |
|
442 | - } |
|
443 | - \OC::$server->getUserSession()->logout(); |
|
444 | - } |
|
445 | - |
|
446 | - $session->set('LAST_ACTIVITY', time()); |
|
447 | - } |
|
448 | - |
|
449 | - /** |
|
450 | - * @return string |
|
451 | - */ |
|
452 | - private static function getSessionLifeTime() { |
|
453 | - return \OC::$server->getConfig()->getSystemValue('session_lifetime', 60 * 60 * 24); |
|
454 | - } |
|
455 | - |
|
456 | - /** |
|
457 | - * Try to set some values to the required Nextcloud default |
|
458 | - */ |
|
459 | - public static function setRequiredIniValues() { |
|
460 | - @ini_set('default_charset', 'UTF-8'); |
|
461 | - @ini_set('gd.jpeg_ignore_warning', '1'); |
|
462 | - } |
|
463 | - |
|
464 | - /** |
|
465 | - * Send the same site cookies |
|
466 | - */ |
|
467 | - private static function sendSameSiteCookies() { |
|
468 | - $cookieParams = session_get_cookie_params(); |
|
469 | - $secureCookie = ($cookieParams['secure'] === true) ? 'secure; ' : ''; |
|
470 | - $policies = [ |
|
471 | - 'lax', |
|
472 | - 'strict', |
|
473 | - ]; |
|
474 | - |
|
475 | - // Append __Host to the cookie if it meets the requirements |
|
476 | - $cookiePrefix = ''; |
|
477 | - if ($cookieParams['secure'] === true && $cookieParams['path'] === '/') { |
|
478 | - $cookiePrefix = '__Host-'; |
|
479 | - } |
|
480 | - |
|
481 | - foreach ($policies as $policy) { |
|
482 | - header( |
|
483 | - sprintf( |
|
484 | - 'Set-Cookie: %snc_sameSiteCookie%s=true; path=%s; httponly;' . $secureCookie . 'expires=Fri, 31-Dec-2100 23:59:59 GMT; SameSite=%s', |
|
485 | - $cookiePrefix, |
|
486 | - $policy, |
|
487 | - $cookieParams['path'], |
|
488 | - $policy |
|
489 | - ), |
|
490 | - false |
|
491 | - ); |
|
492 | - } |
|
493 | - } |
|
494 | - |
|
495 | - /** |
|
496 | - * Same Site cookie to further mitigate CSRF attacks. This cookie has to |
|
497 | - * be set in every request if cookies are sent to add a second level of |
|
498 | - * defense against CSRF. |
|
499 | - * |
|
500 | - * If the cookie is not sent this will set the cookie and reload the page. |
|
501 | - * We use an additional cookie since we want to protect logout CSRF and |
|
502 | - * also we can't directly interfere with PHP's session mechanism. |
|
503 | - */ |
|
504 | - private static function performSameSiteCookieProtection(\OCP\IConfig $config) { |
|
505 | - $request = \OC::$server->getRequest(); |
|
506 | - |
|
507 | - // Some user agents are notorious and don't really properly follow HTTP |
|
508 | - // specifications. For those, have an automated opt-out. Since the protection |
|
509 | - // for remote.php is applied in base.php as starting point we need to opt out |
|
510 | - // here. |
|
511 | - $incompatibleUserAgents = $config->getSystemValue('csrf.optout'); |
|
512 | - |
|
513 | - // Fallback, if csrf.optout is unset |
|
514 | - if (!is_array($incompatibleUserAgents)) { |
|
515 | - $incompatibleUserAgents = [ |
|
516 | - // OS X Finder |
|
517 | - '/^WebDAVFS/', |
|
518 | - // Windows webdav drive |
|
519 | - '/^Microsoft-WebDAV-MiniRedir/', |
|
520 | - ]; |
|
521 | - } |
|
522 | - |
|
523 | - if ($request->isUserAgent($incompatibleUserAgents)) { |
|
524 | - return; |
|
525 | - } |
|
526 | - |
|
527 | - if (count($_COOKIE) > 0) { |
|
528 | - $requestUri = $request->getScriptName(); |
|
529 | - $processingScript = explode('/', $requestUri); |
|
530 | - $processingScript = $processingScript[count($processingScript) - 1]; |
|
531 | - |
|
532 | - // index.php routes are handled in the middleware |
|
533 | - if ($processingScript === 'index.php') { |
|
534 | - return; |
|
535 | - } |
|
536 | - |
|
537 | - // All other endpoints require the lax and the strict cookie |
|
538 | - if (!$request->passesStrictCookieCheck()) { |
|
539 | - self::sendSameSiteCookies(); |
|
540 | - // Debug mode gets access to the resources without strict cookie |
|
541 | - // due to the fact that the SabreDAV browser also lives there. |
|
542 | - if (!$config->getSystemValue('debug', false)) { |
|
543 | - http_response_code(\OCP\AppFramework\Http::STATUS_SERVICE_UNAVAILABLE); |
|
544 | - exit(); |
|
545 | - } |
|
546 | - } |
|
547 | - } elseif (!isset($_COOKIE['nc_sameSiteCookielax']) || !isset($_COOKIE['nc_sameSiteCookiestrict'])) { |
|
548 | - self::sendSameSiteCookies(); |
|
549 | - } |
|
550 | - } |
|
551 | - |
|
552 | - public static function init() { |
|
553 | - // calculate the root directories |
|
554 | - OC::$SERVERROOT = str_replace("\\", '/', substr(__DIR__, 0, -4)); |
|
555 | - |
|
556 | - // register autoloader |
|
557 | - $loaderStart = microtime(true); |
|
558 | - require_once __DIR__ . '/autoloader.php'; |
|
559 | - self::$loader = new \OC\Autoloader([ |
|
560 | - OC::$SERVERROOT . '/lib/private/legacy', |
|
561 | - ]); |
|
562 | - if (defined('PHPUNIT_RUN')) { |
|
563 | - self::$loader->addValidRoot(OC::$SERVERROOT . '/tests'); |
|
564 | - } |
|
565 | - spl_autoload_register([self::$loader, 'load']); |
|
566 | - $loaderEnd = microtime(true); |
|
567 | - |
|
568 | - self::$CLI = (php_sapi_name() == 'cli'); |
|
569 | - |
|
570 | - // Add default composer PSR-4 autoloader |
|
571 | - self::$composerAutoloader = require_once OC::$SERVERROOT . '/lib/composer/autoload.php'; |
|
572 | - |
|
573 | - try { |
|
574 | - self::initPaths(); |
|
575 | - // setup 3rdparty autoloader |
|
576 | - $vendorAutoLoad = OC::$SERVERROOT. '/3rdparty/autoload.php'; |
|
577 | - if (!file_exists($vendorAutoLoad)) { |
|
578 | - throw new \RuntimeException('Composer autoloader not found, unable to continue. Check the folder "3rdparty". Running "git submodule update --init" will initialize the git submodule that handles the subfolder "3rdparty".'); |
|
579 | - } |
|
580 | - require_once $vendorAutoLoad; |
|
581 | - } catch (\RuntimeException $e) { |
|
582 | - if (!self::$CLI) { |
|
583 | - http_response_code(503); |
|
584 | - } |
|
585 | - // we can't use the template error page here, because this needs the |
|
586 | - // DI container which isn't available yet |
|
587 | - print($e->getMessage()); |
|
588 | - exit(); |
|
589 | - } |
|
590 | - |
|
591 | - // setup the basic server |
|
592 | - self::$server = new \OC\Server(\OC::$WEBROOT, self::$config); |
|
593 | - self::$server->boot(); |
|
594 | - $eventLogger = \OC::$server->getEventLogger(); |
|
595 | - $eventLogger->log('autoloader', 'Autoloader', $loaderStart, $loaderEnd); |
|
596 | - $eventLogger->start('boot', 'Initialize'); |
|
597 | - |
|
598 | - // Override php.ini and log everything if we're troubleshooting |
|
599 | - if (self::$config->getValue('loglevel') === ILogger::DEBUG) { |
|
600 | - error_reporting(E_ALL); |
|
601 | - } |
|
602 | - |
|
603 | - // Don't display errors and log them |
|
604 | - @ini_set('display_errors', '0'); |
|
605 | - @ini_set('log_errors', '1'); |
|
606 | - |
|
607 | - if (!date_default_timezone_set('UTC')) { |
|
608 | - throw new \RuntimeException('Could not set timezone to UTC'); |
|
609 | - } |
|
610 | - |
|
611 | - //try to configure php to enable big file uploads. |
|
612 | - //this doesn´t work always depending on the webserver and php configuration. |
|
613 | - //Let´s try to overwrite some defaults anyway |
|
614 | - |
|
615 | - //try to set the maximum execution time to 60min |
|
616 | - if (strpos(@ini_get('disable_functions'), 'set_time_limit') === false) { |
|
617 | - @set_time_limit(3600); |
|
618 | - } |
|
619 | - @ini_set('max_execution_time', '3600'); |
|
620 | - @ini_set('max_input_time', '3600'); |
|
621 | - |
|
622 | - self::setRequiredIniValues(); |
|
623 | - self::handleAuthHeaders(); |
|
624 | - $systemConfig = \OC::$server->get(\OC\SystemConfig::class); |
|
625 | - self::registerAutoloaderCache($systemConfig); |
|
626 | - |
|
627 | - // initialize intl fallback if necessary |
|
628 | - OC_Util::isSetLocaleWorking(); |
|
629 | - |
|
630 | - $config = \OC::$server->get(\OCP\IConfig::class); |
|
631 | - if (!defined('PHPUNIT_RUN')) { |
|
632 | - OC\Log\ErrorHandler::setLogger(\OC::$server->getLogger()); |
|
633 | - $debug = $config->getSystemValue('debug', false); |
|
634 | - OC\Log\ErrorHandler::register($debug); |
|
635 | - } |
|
636 | - |
|
637 | - /** @var \OC\AppFramework\Bootstrap\Coordinator $bootstrapCoordinator */ |
|
638 | - $bootstrapCoordinator = \OC::$server->query(\OC\AppFramework\Bootstrap\Coordinator::class); |
|
639 | - $bootstrapCoordinator->runInitialRegistration(); |
|
640 | - |
|
641 | - $eventLogger->start('init_session', 'Initialize session'); |
|
642 | - OC_App::loadApps(['session']); |
|
643 | - if (!self::$CLI) { |
|
644 | - self::initSession(); |
|
645 | - } |
|
646 | - $eventLogger->end('init_session'); |
|
647 | - self::checkConfig(); |
|
648 | - self::checkInstalled($systemConfig); |
|
649 | - |
|
650 | - OC_Response::addSecurityHeaders(); |
|
651 | - |
|
652 | - self::performSameSiteCookieProtection($config); |
|
653 | - |
|
654 | - if (!defined('OC_CONSOLE')) { |
|
655 | - $errors = OC_Util::checkServer($systemConfig); |
|
656 | - if (count($errors) > 0) { |
|
657 | - if (!self::$CLI) { |
|
658 | - http_response_code(503); |
|
659 | - OC_Util::addStyle('guest'); |
|
660 | - try { |
|
661 | - OC_Template::printGuestPage('', 'error', ['errors' => $errors]); |
|
662 | - exit; |
|
663 | - } catch (\Exception $e) { |
|
664 | - // In case any error happens when showing the error page, we simply fall back to posting the text. |
|
665 | - // This might be the case when e.g. the data directory is broken and we can not load/write SCSS to/from it. |
|
666 | - } |
|
667 | - } |
|
668 | - |
|
669 | - // Convert l10n string into regular string for usage in database |
|
670 | - $staticErrors = []; |
|
671 | - foreach ($errors as $error) { |
|
672 | - echo $error['error'] . "\n"; |
|
673 | - echo $error['hint'] . "\n\n"; |
|
674 | - $staticErrors[] = [ |
|
675 | - 'error' => (string)$error['error'], |
|
676 | - 'hint' => (string)$error['hint'], |
|
677 | - ]; |
|
678 | - } |
|
679 | - |
|
680 | - try { |
|
681 | - $config->setAppValue('core', 'cronErrors', json_encode($staticErrors)); |
|
682 | - } catch (\Exception $e) { |
|
683 | - echo('Writing to database failed'); |
|
684 | - } |
|
685 | - exit(1); |
|
686 | - } elseif (self::$CLI && $config->getSystemValue('installed', false)) { |
|
687 | - $config->deleteAppValue('core', 'cronErrors'); |
|
688 | - } |
|
689 | - } |
|
690 | - //try to set the session lifetime |
|
691 | - $sessionLifeTime = self::getSessionLifeTime(); |
|
692 | - @ini_set('gc_maxlifetime', (string)$sessionLifeTime); |
|
693 | - |
|
694 | - // User and Groups |
|
695 | - if (!$systemConfig->getValue("installed", false)) { |
|
696 | - self::$server->getSession()->set('user_id', ''); |
|
697 | - } |
|
698 | - |
|
699 | - OC_User::useBackend(new \OC\User\Database()); |
|
700 | - \OC::$server->getGroupManager()->addBackend(new \OC\Group\Database()); |
|
701 | - |
|
702 | - // Subscribe to the hook |
|
703 | - \OCP\Util::connectHook( |
|
704 | - '\OCA\Files_Sharing\API\Server2Server', |
|
705 | - 'preLoginNameUsedAsUserName', |
|
706 | - '\OC\User\Database', |
|
707 | - 'preLoginNameUsedAsUserName' |
|
708 | - ); |
|
709 | - |
|
710 | - //setup extra user backends |
|
711 | - if (!\OCP\Util::needUpgrade()) { |
|
712 | - OC_User::setupBackends(); |
|
713 | - } else { |
|
714 | - // Run upgrades in incognito mode |
|
715 | - OC_User::setIncognitoMode(true); |
|
716 | - } |
|
717 | - |
|
718 | - self::registerCleanupHooks($systemConfig); |
|
719 | - self::registerFilesystemHooks(); |
|
720 | - self::registerShareHooks($systemConfig); |
|
721 | - self::registerEncryptionWrapperAndHooks(); |
|
722 | - self::registerAccountHooks(); |
|
723 | - self::registerResourceCollectionHooks(); |
|
724 | - self::registerAppRestrictionsHooks(); |
|
725 | - |
|
726 | - // Make sure that the application class is not loaded before the database is setup |
|
727 | - if ($systemConfig->getValue("installed", false)) { |
|
728 | - OC_App::loadApp('settings'); |
|
729 | - } |
|
730 | - |
|
731 | - //make sure temporary files are cleaned up |
|
732 | - $tmpManager = \OC::$server->getTempManager(); |
|
733 | - register_shutdown_function([$tmpManager, 'clean']); |
|
734 | - $lockProvider = \OC::$server->getLockingProvider(); |
|
735 | - register_shutdown_function([$lockProvider, 'releaseAll']); |
|
736 | - |
|
737 | - // Check whether the sample configuration has been copied |
|
738 | - if ($systemConfig->getValue('copied_sample_config', false)) { |
|
739 | - $l = \OC::$server->getL10N('lib'); |
|
740 | - OC_Template::printErrorPage( |
|
741 | - $l->t('Sample configuration detected'), |
|
742 | - $l->t('It has been detected that the sample configuration has been copied. This can break your installation and is unsupported. Please read the documentation before performing changes on config.php'), |
|
743 | - 503 |
|
744 | - ); |
|
745 | - return; |
|
746 | - } |
|
747 | - |
|
748 | - $request = \OC::$server->getRequest(); |
|
749 | - $host = $request->getInsecureServerHost(); |
|
750 | - /** |
|
751 | - * if the host passed in headers isn't trusted |
|
752 | - * FIXME: Should not be in here at all :see_no_evil: |
|
753 | - */ |
|
754 | - if (!OC::$CLI |
|
755 | - && !\OC::$server->getTrustedDomainHelper()->isTrustedDomain($host) |
|
756 | - && $config->getSystemValue('installed', false) |
|
757 | - ) { |
|
758 | - // Allow access to CSS resources |
|
759 | - $isScssRequest = false; |
|
760 | - if (strpos($request->getPathInfo(), '/css/') === 0) { |
|
761 | - $isScssRequest = true; |
|
762 | - } |
|
763 | - |
|
764 | - if (substr($request->getRequestUri(), -11) === '/status.php') { |
|
765 | - http_response_code(400); |
|
766 | - header('Content-Type: application/json'); |
|
767 | - echo '{"error": "Trusted domain error.", "code": 15}'; |
|
768 | - exit(); |
|
769 | - } |
|
770 | - |
|
771 | - if (!$isScssRequest) { |
|
772 | - http_response_code(400); |
|
773 | - |
|
774 | - \OC::$server->getLogger()->info( |
|
775 | - 'Trusted domain error. "{remoteAddress}" tried to access using "{host}" as host.', |
|
776 | - [ |
|
777 | - 'app' => 'core', |
|
778 | - 'remoteAddress' => $request->getRemoteAddress(), |
|
779 | - 'host' => $host, |
|
780 | - ] |
|
781 | - ); |
|
782 | - |
|
783 | - $tmpl = new OCP\Template('core', 'untrustedDomain', 'guest'); |
|
784 | - $tmpl->assign('docUrl', \OC::$server->getURLGenerator()->linkToDocs('admin-trusted-domains')); |
|
785 | - $tmpl->printPage(); |
|
786 | - |
|
787 | - exit(); |
|
788 | - } |
|
789 | - } |
|
790 | - $eventLogger->end('boot'); |
|
791 | - } |
|
792 | - |
|
793 | - /** |
|
794 | - * register hooks for the cleanup of cache and bruteforce protection |
|
795 | - */ |
|
796 | - public static function registerCleanupHooks(\OC\SystemConfig $systemConfig) { |
|
797 | - //don't try to do this before we are properly setup |
|
798 | - if ($systemConfig->getValue('installed', false) && !\OCP\Util::needUpgrade()) { |
|
799 | - |
|
800 | - // NOTE: This will be replaced to use OCP |
|
801 | - $userSession = self::$server->getUserSession(); |
|
802 | - $userSession->listen('\OC\User', 'postLogin', function () use ($userSession) { |
|
803 | - if (!defined('PHPUNIT_RUN') && $userSession->isLoggedIn()) { |
|
804 | - // reset brute force delay for this IP address and username |
|
805 | - $uid = \OC::$server->getUserSession()->getUser()->getUID(); |
|
806 | - $request = \OC::$server->getRequest(); |
|
807 | - $throttler = \OC::$server->getBruteForceThrottler(); |
|
808 | - $throttler->resetDelay($request->getRemoteAddress(), 'login', ['user' => $uid]); |
|
809 | - } |
|
810 | - |
|
811 | - try { |
|
812 | - $cache = new \OC\Cache\File(); |
|
813 | - $cache->gc(); |
|
814 | - } catch (\OC\ServerNotAvailableException $e) { |
|
815 | - // not a GC exception, pass it on |
|
816 | - throw $e; |
|
817 | - } catch (\OC\ForbiddenException $e) { |
|
818 | - // filesystem blocked for this request, ignore |
|
819 | - } catch (\Exception $e) { |
|
820 | - // a GC exception should not prevent users from using OC, |
|
821 | - // so log the exception |
|
822 | - \OC::$server->getLogger()->logException($e, [ |
|
823 | - 'message' => 'Exception when running cache gc.', |
|
824 | - 'level' => ILogger::WARN, |
|
825 | - 'app' => 'core', |
|
826 | - ]); |
|
827 | - } |
|
828 | - }); |
|
829 | - } |
|
830 | - } |
|
831 | - |
|
832 | - private static function registerEncryptionWrapperAndHooks() { |
|
833 | - $manager = self::$server->getEncryptionManager(); |
|
834 | - \OCP\Util::connectHook('OC_Filesystem', 'preSetup', $manager, 'setupStorage'); |
|
835 | - |
|
836 | - $enabled = $manager->isEnabled(); |
|
837 | - if ($enabled) { |
|
838 | - \OCP\Util::connectHook(Share::class, 'post_shared', HookManager::class, 'postShared'); |
|
839 | - \OCP\Util::connectHook(Share::class, 'post_unshare', HookManager::class, 'postUnshared'); |
|
840 | - \OCP\Util::connectHook('OC_Filesystem', 'post_rename', HookManager::class, 'postRename'); |
|
841 | - \OCP\Util::connectHook('\OCA\Files_Trashbin\Trashbin', 'post_restore', HookManager::class, 'postRestore'); |
|
842 | - } |
|
843 | - } |
|
844 | - |
|
845 | - private static function registerAccountHooks() { |
|
846 | - $hookHandler = \OC::$server->get(\OC\Accounts\Hooks::class); |
|
847 | - \OCP\Util::connectHook('OC_User', 'changeUser', $hookHandler, 'changeUserHook'); |
|
848 | - } |
|
849 | - |
|
850 | - private static function registerAppRestrictionsHooks() { |
|
851 | - /** @var \OC\Group\Manager $groupManager */ |
|
852 | - $groupManager = self::$server->query(\OCP\IGroupManager::class); |
|
853 | - $groupManager->listen('\OC\Group', 'postDelete', function (\OCP\IGroup $group) { |
|
854 | - $appManager = self::$server->getAppManager(); |
|
855 | - $apps = $appManager->getEnabledAppsForGroup($group); |
|
856 | - foreach ($apps as $appId) { |
|
857 | - $restrictions = $appManager->getAppRestriction($appId); |
|
858 | - if (empty($restrictions)) { |
|
859 | - continue; |
|
860 | - } |
|
861 | - $key = array_search($group->getGID(), $restrictions); |
|
862 | - unset($restrictions[$key]); |
|
863 | - $restrictions = array_values($restrictions); |
|
864 | - if (empty($restrictions)) { |
|
865 | - $appManager->disableApp($appId); |
|
866 | - } else { |
|
867 | - $appManager->enableAppForGroups($appId, $restrictions); |
|
868 | - } |
|
869 | - } |
|
870 | - }); |
|
871 | - } |
|
872 | - |
|
873 | - private static function registerResourceCollectionHooks() { |
|
874 | - \OC\Collaboration\Resources\Listener::register(\OC::$server->getEventDispatcher()); |
|
875 | - } |
|
876 | - |
|
877 | - /** |
|
878 | - * register hooks for the filesystem |
|
879 | - */ |
|
880 | - public static function registerFilesystemHooks() { |
|
881 | - // Check for blacklisted files |
|
882 | - OC_Hook::connect('OC_Filesystem', 'write', Filesystem::class, 'isBlacklisted'); |
|
883 | - OC_Hook::connect('OC_Filesystem', 'rename', Filesystem::class, 'isBlacklisted'); |
|
884 | - } |
|
885 | - |
|
886 | - /** |
|
887 | - * register hooks for sharing |
|
888 | - */ |
|
889 | - public static function registerShareHooks(\OC\SystemConfig $systemConfig) { |
|
890 | - if ($systemConfig->getValue('installed')) { |
|
891 | - OC_Hook::connect('OC_User', 'post_deleteUser', Hooks::class, 'post_deleteUser'); |
|
892 | - OC_Hook::connect('OC_User', 'post_deleteGroup', Hooks::class, 'post_deleteGroup'); |
|
893 | - |
|
894 | - /** @var IEventDispatcher $dispatcher */ |
|
895 | - $dispatcher = \OC::$server->get(IEventDispatcher::class); |
|
896 | - $dispatcher->addServiceListener(UserRemovedEvent::class, \OC\Share20\UserRemovedListener::class); |
|
897 | - } |
|
898 | - } |
|
899 | - |
|
900 | - protected static function registerAutoloaderCache(\OC\SystemConfig $systemConfig) { |
|
901 | - // The class loader takes an optional low-latency cache, which MUST be |
|
902 | - // namespaced. The instanceid is used for namespacing, but might be |
|
903 | - // unavailable at this point. Furthermore, it might not be possible to |
|
904 | - // generate an instanceid via \OC_Util::getInstanceId() because the |
|
905 | - // config file may not be writable. As such, we only register a class |
|
906 | - // loader cache if instanceid is available without trying to create one. |
|
907 | - $instanceId = $systemConfig->getValue('instanceid', null); |
|
908 | - if ($instanceId) { |
|
909 | - try { |
|
910 | - $memcacheFactory = \OC::$server->getMemCacheFactory(); |
|
911 | - self::$loader->setMemoryCache($memcacheFactory->createLocal('Autoloader')); |
|
912 | - } catch (\Exception $ex) { |
|
913 | - } |
|
914 | - } |
|
915 | - } |
|
916 | - |
|
917 | - /** |
|
918 | - * Handle the request |
|
919 | - */ |
|
920 | - public static function handleRequest() { |
|
921 | - \OC::$server->getEventLogger()->start('handle_request', 'Handle request'); |
|
922 | - $systemConfig = \OC::$server->getSystemConfig(); |
|
923 | - |
|
924 | - // Check if Nextcloud is installed or in maintenance (update) mode |
|
925 | - if (!$systemConfig->getValue('installed', false)) { |
|
926 | - \OC::$server->getSession()->clear(); |
|
927 | - $setupHelper = new OC\Setup( |
|
928 | - $systemConfig, |
|
929 | - \OC::$server->get(\bantu\IniGetWrapper\IniGetWrapper::class), |
|
930 | - \OC::$server->getL10N('lib'), |
|
931 | - \OC::$server->query(\OCP\Defaults::class), |
|
932 | - \OC::$server->get(\Psr\Log\LoggerInterface::class), |
|
933 | - \OC::$server->getSecureRandom(), |
|
934 | - \OC::$server->query(\OC\Installer::class) |
|
935 | - ); |
|
936 | - $controller = new OC\Core\Controller\SetupController($setupHelper); |
|
937 | - $controller->run($_POST); |
|
938 | - exit(); |
|
939 | - } |
|
940 | - |
|
941 | - $request = \OC::$server->getRequest(); |
|
942 | - $requestPath = $request->getRawPathInfo(); |
|
943 | - if ($requestPath === '/heartbeat') { |
|
944 | - return; |
|
945 | - } |
|
946 | - if (substr($requestPath, -3) !== '.js') { // we need these files during the upgrade |
|
947 | - self::checkMaintenanceMode($systemConfig); |
|
948 | - |
|
949 | - if (\OCP\Util::needUpgrade()) { |
|
950 | - if (function_exists('opcache_reset')) { |
|
951 | - opcache_reset(); |
|
952 | - } |
|
953 | - if (!((bool) $systemConfig->getValue('maintenance', false))) { |
|
954 | - self::printUpgradePage($systemConfig); |
|
955 | - exit(); |
|
956 | - } |
|
957 | - } |
|
958 | - } |
|
959 | - |
|
960 | - // emergency app disabling |
|
961 | - if ($requestPath === '/disableapp' |
|
962 | - && $request->getMethod() === 'POST' |
|
963 | - && ((array)$request->getParam('appid')) !== '' |
|
964 | - ) { |
|
965 | - \OC_JSON::callCheck(); |
|
966 | - \OC_JSON::checkAdminUser(); |
|
967 | - $appIds = (array)$request->getParam('appid'); |
|
968 | - foreach ($appIds as $appId) { |
|
969 | - $appId = \OC_App::cleanAppId($appId); |
|
970 | - \OC::$server->getAppManager()->disableApp($appId); |
|
971 | - } |
|
972 | - \OC_JSON::success(); |
|
973 | - exit(); |
|
974 | - } |
|
975 | - |
|
976 | - // Always load authentication apps |
|
977 | - OC_App::loadApps(['authentication']); |
|
978 | - |
|
979 | - // Load minimum set of apps |
|
980 | - if (!\OCP\Util::needUpgrade() |
|
981 | - && !((bool) $systemConfig->getValue('maintenance', false))) { |
|
982 | - // For logged-in users: Load everything |
|
983 | - if (\OC::$server->getUserSession()->isLoggedIn()) { |
|
984 | - OC_App::loadApps(); |
|
985 | - } else { |
|
986 | - // For guests: Load only filesystem and logging |
|
987 | - OC_App::loadApps(['filesystem', 'logging']); |
|
988 | - self::handleLogin($request); |
|
989 | - } |
|
990 | - } |
|
991 | - |
|
992 | - if (!self::$CLI) { |
|
993 | - try { |
|
994 | - if (!((bool) $systemConfig->getValue('maintenance', false)) && !\OCP\Util::needUpgrade()) { |
|
995 | - OC_App::loadApps(['filesystem', 'logging']); |
|
996 | - OC_App::loadApps(); |
|
997 | - } |
|
998 | - OC::$server->get(\OC\Route\Router::class)->match($request->getRawPathInfo()); |
|
999 | - return; |
|
1000 | - } catch (Symfony\Component\Routing\Exception\ResourceNotFoundException $e) { |
|
1001 | - //header('HTTP/1.0 404 Not Found'); |
|
1002 | - } catch (Symfony\Component\Routing\Exception\MethodNotAllowedException $e) { |
|
1003 | - http_response_code(405); |
|
1004 | - return; |
|
1005 | - } |
|
1006 | - } |
|
1007 | - |
|
1008 | - // Handle WebDAV |
|
1009 | - if (isset($_SERVER['REQUEST_METHOD']) && $_SERVER['REQUEST_METHOD'] === 'PROPFIND') { |
|
1010 | - // not allowed any more to prevent people |
|
1011 | - // mounting this root directly. |
|
1012 | - // Users need to mount remote.php/webdav instead. |
|
1013 | - http_response_code(405); |
|
1014 | - return; |
|
1015 | - } |
|
1016 | - |
|
1017 | - // Someone is logged in |
|
1018 | - if (\OC::$server->getUserSession()->isLoggedIn()) { |
|
1019 | - OC_App::loadApps(); |
|
1020 | - OC_User::setupBackends(); |
|
1021 | - OC_Util::setupFS(); |
|
1022 | - // FIXME |
|
1023 | - // Redirect to default application |
|
1024 | - OC_Util::redirectToDefaultPage(); |
|
1025 | - } else { |
|
1026 | - // Not handled and not logged in |
|
1027 | - header('Location: '.\OC::$server->getURLGenerator()->linkToRouteAbsolute('core.login.showLoginForm')); |
|
1028 | - } |
|
1029 | - } |
|
1030 | - |
|
1031 | - /** |
|
1032 | - * Check login: apache auth, auth token, basic auth |
|
1033 | - * |
|
1034 | - * @param OCP\IRequest $request |
|
1035 | - * @return boolean |
|
1036 | - */ |
|
1037 | - public static function handleLogin(OCP\IRequest $request) { |
|
1038 | - $userSession = self::$server->getUserSession(); |
|
1039 | - if (OC_User::handleApacheAuth()) { |
|
1040 | - return true; |
|
1041 | - } |
|
1042 | - if ($userSession->tryTokenLogin($request)) { |
|
1043 | - return true; |
|
1044 | - } |
|
1045 | - if (isset($_COOKIE['nc_username']) |
|
1046 | - && isset($_COOKIE['nc_token']) |
|
1047 | - && isset($_COOKIE['nc_session_id']) |
|
1048 | - && $userSession->loginWithCookie($_COOKIE['nc_username'], $_COOKIE['nc_token'], $_COOKIE['nc_session_id'])) { |
|
1049 | - return true; |
|
1050 | - } |
|
1051 | - if ($userSession->tryBasicAuthLogin($request, \OC::$server->getBruteForceThrottler())) { |
|
1052 | - return true; |
|
1053 | - } |
|
1054 | - return false; |
|
1055 | - } |
|
1056 | - |
|
1057 | - protected static function handleAuthHeaders() { |
|
1058 | - //copy http auth headers for apache+php-fcgid work around |
|
1059 | - if (isset($_SERVER['HTTP_XAUTHORIZATION']) && !isset($_SERVER['HTTP_AUTHORIZATION'])) { |
|
1060 | - $_SERVER['HTTP_AUTHORIZATION'] = $_SERVER['HTTP_XAUTHORIZATION']; |
|
1061 | - } |
|
1062 | - |
|
1063 | - // Extract PHP_AUTH_USER/PHP_AUTH_PW from other headers if necessary. |
|
1064 | - $vars = [ |
|
1065 | - 'HTTP_AUTHORIZATION', // apache+php-cgi work around |
|
1066 | - 'REDIRECT_HTTP_AUTHORIZATION', // apache+php-cgi alternative |
|
1067 | - ]; |
|
1068 | - foreach ($vars as $var) { |
|
1069 | - if (isset($_SERVER[$var]) && preg_match('/Basic\s+(.*)$/i', $_SERVER[$var], $matches)) { |
|
1070 | - $credentials = explode(':', base64_decode($matches[1]), 2); |
|
1071 | - if (count($credentials) === 2) { |
|
1072 | - $_SERVER['PHP_AUTH_USER'] = $credentials[0]; |
|
1073 | - $_SERVER['PHP_AUTH_PW'] = $credentials[1]; |
|
1074 | - break; |
|
1075 | - } |
|
1076 | - } |
|
1077 | - } |
|
1078 | - } |
|
80 | + /** |
|
81 | + * Associative array for autoloading. classname => filename |
|
82 | + */ |
|
83 | + public static $CLASSPATH = []; |
|
84 | + /** |
|
85 | + * The installation path for Nextcloud on the server (e.g. /srv/http/nextcloud) |
|
86 | + */ |
|
87 | + public static $SERVERROOT = ''; |
|
88 | + /** |
|
89 | + * the current request path relative to the Nextcloud root (e.g. files/index.php) |
|
90 | + */ |
|
91 | + private static $SUBURI = ''; |
|
92 | + /** |
|
93 | + * the Nextcloud root path for http requests (e.g. nextcloud/) |
|
94 | + */ |
|
95 | + public static $WEBROOT = ''; |
|
96 | + /** |
|
97 | + * The installation path array of the apps folder on the server (e.g. /srv/http/nextcloud) 'path' and |
|
98 | + * web path in 'url' |
|
99 | + */ |
|
100 | + public static $APPSROOTS = []; |
|
101 | + |
|
102 | + /** |
|
103 | + * @var string |
|
104 | + */ |
|
105 | + public static $configDir; |
|
106 | + |
|
107 | + /** |
|
108 | + * requested app |
|
109 | + */ |
|
110 | + public static $REQUESTEDAPP = ''; |
|
111 | + |
|
112 | + /** |
|
113 | + * check if Nextcloud runs in cli mode |
|
114 | + */ |
|
115 | + public static $CLI = false; |
|
116 | + |
|
117 | + /** |
|
118 | + * @var \OC\Autoloader $loader |
|
119 | + */ |
|
120 | + public static $loader = null; |
|
121 | + |
|
122 | + /** @var \Composer\Autoload\ClassLoader $composerAutoloader */ |
|
123 | + public static $composerAutoloader = null; |
|
124 | + |
|
125 | + /** |
|
126 | + * @var \OC\Server |
|
127 | + */ |
|
128 | + public static $server = null; |
|
129 | + |
|
130 | + /** |
|
131 | + * @var \OC\Config |
|
132 | + */ |
|
133 | + private static $config = null; |
|
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() { |
|
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 | + * \OC::$server->getRequest() since \OC::$server does not yet exist. |
|
155 | + */ |
|
156 | + $params = [ |
|
157 | + 'server' => [ |
|
158 | + 'SCRIPT_NAME' => $_SERVER['SCRIPT_NAME'], |
|
159 | + 'SCRIPT_FILENAME' => $_SERVER['SCRIPT_FILENAME'], |
|
160 | + ], |
|
161 | + ]; |
|
162 | + $fakeRequest = new \OC\AppFramework\Http\Request($params, new \OC\Security\SecureRandom(), new \OC\AllConfig(new \OC\SystemConfig(self::$config))); |
|
163 | + $scriptName = $fakeRequest->getScriptName(); |
|
164 | + if (substr($scriptName, -1) == '/') { |
|
165 | + $scriptName .= 'index.php'; |
|
166 | + //make sure suburi follows the same rules as scriptName |
|
167 | + if (substr(OC::$SUBURI, -9) != 'index.php') { |
|
168 | + if (substr(OC::$SUBURI, -1) != '/') { |
|
169 | + OC::$SUBURI = OC::$SUBURI . '/'; |
|
170 | + } |
|
171 | + OC::$SUBURI = OC::$SUBURI . 'index.php'; |
|
172 | + } |
|
173 | + } |
|
174 | + |
|
175 | + |
|
176 | + if (OC::$CLI) { |
|
177 | + OC::$WEBROOT = self::$config->getValue('overwritewebroot', ''); |
|
178 | + } else { |
|
179 | + if (substr($scriptName, 0 - strlen(OC::$SUBURI)) === OC::$SUBURI) { |
|
180 | + OC::$WEBROOT = substr($scriptName, 0, 0 - strlen(OC::$SUBURI)); |
|
181 | + |
|
182 | + if (OC::$WEBROOT != '' && OC::$WEBROOT[0] !== '/') { |
|
183 | + OC::$WEBROOT = '/' . OC::$WEBROOT; |
|
184 | + } |
|
185 | + } else { |
|
186 | + // The scriptName is not ending with OC::$SUBURI |
|
187 | + // This most likely means that we are calling from CLI. |
|
188 | + // However some cron jobs still need to generate |
|
189 | + // a web URL, so we use overwritewebroot as a fallback. |
|
190 | + OC::$WEBROOT = self::$config->getValue('overwritewebroot', ''); |
|
191 | + } |
|
192 | + |
|
193 | + // Resolve /nextcloud to /nextcloud/ to ensure to always have a trailing |
|
194 | + // slash which is required by URL generation. |
|
195 | + if (isset($_SERVER['REQUEST_URI']) && $_SERVER['REQUEST_URI'] === \OC::$WEBROOT && |
|
196 | + substr($_SERVER['REQUEST_URI'], -1) !== '/') { |
|
197 | + header('Location: '.\OC::$WEBROOT.'/'); |
|
198 | + exit(); |
|
199 | + } |
|
200 | + } |
|
201 | + |
|
202 | + // search the apps folder |
|
203 | + $config_paths = self::$config->getValue('apps_paths', []); |
|
204 | + if (!empty($config_paths)) { |
|
205 | + foreach ($config_paths as $paths) { |
|
206 | + if (isset($paths['url']) && isset($paths['path'])) { |
|
207 | + $paths['url'] = rtrim($paths['url'], '/'); |
|
208 | + $paths['path'] = rtrim($paths['path'], '/'); |
|
209 | + OC::$APPSROOTS[] = $paths; |
|
210 | + } |
|
211 | + } |
|
212 | + } elseif (file_exists(OC::$SERVERROOT . '/apps')) { |
|
213 | + OC::$APPSROOTS[] = ['path' => OC::$SERVERROOT . '/apps', 'url' => '/apps', 'writable' => true]; |
|
214 | + } |
|
215 | + |
|
216 | + if (empty(OC::$APPSROOTS)) { |
|
217 | + throw new \RuntimeException('apps directory not found! Please put the Nextcloud apps folder in the Nextcloud folder' |
|
218 | + . '. You can also configure the location in the config.php file.'); |
|
219 | + } |
|
220 | + $paths = []; |
|
221 | + foreach (OC::$APPSROOTS as $path) { |
|
222 | + $paths[] = $path['path']; |
|
223 | + if (!is_dir($path['path'])) { |
|
224 | + throw new \RuntimeException(sprintf('App directory "%s" not found! Please put the Nextcloud apps folder in the' |
|
225 | + . ' Nextcloud folder. You can also configure the location in the config.php file.', $path['path'])); |
|
226 | + } |
|
227 | + } |
|
228 | + |
|
229 | + // set the right include path |
|
230 | + set_include_path( |
|
231 | + implode(PATH_SEPARATOR, $paths) |
|
232 | + ); |
|
233 | + } |
|
234 | + |
|
235 | + public static function checkConfig() { |
|
236 | + $l = \OC::$server->getL10N('lib'); |
|
237 | + |
|
238 | + // Create config if it does not already exist |
|
239 | + $configFilePath = self::$configDir .'/config.php'; |
|
240 | + if (!file_exists($configFilePath)) { |
|
241 | + @touch($configFilePath); |
|
242 | + } |
|
243 | + |
|
244 | + // Check if config is writable |
|
245 | + $configFileWritable = is_writable($configFilePath); |
|
246 | + if (!$configFileWritable && !OC_Helper::isReadOnlyConfigEnabled() |
|
247 | + || !$configFileWritable && \OCP\Util::needUpgrade()) { |
|
248 | + $urlGenerator = \OC::$server->getURLGenerator(); |
|
249 | + |
|
250 | + if (self::$CLI) { |
|
251 | + echo $l->t('Cannot write into "config" directory!')."\n"; |
|
252 | + echo $l->t('This can usually be fixed by giving the webserver write access to the config directory.')."\n"; |
|
253 | + echo "\n"; |
|
254 | + 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"; |
|
255 | + echo $l->t('See %s', [ $urlGenerator->linkToDocs('admin-config') ])."\n"; |
|
256 | + exit; |
|
257 | + } else { |
|
258 | + OC_Template::printErrorPage( |
|
259 | + $l->t('Cannot write into "config" directory!'), |
|
260 | + $l->t('This can usually be fixed by giving the webserver write access to the config directory.') . ' ' |
|
261 | + . $l->t('But, if you prefer to keep config.php file read only, set the option "config_is_read_only" to true in it.') . ' ' |
|
262 | + . $l->t('See %s', [ $urlGenerator->linkToDocs('admin-config') ]), |
|
263 | + 503 |
|
264 | + ); |
|
265 | + } |
|
266 | + } |
|
267 | + } |
|
268 | + |
|
269 | + public static function checkInstalled(\OC\SystemConfig $systemConfig) { |
|
270 | + if (defined('OC_CONSOLE')) { |
|
271 | + return; |
|
272 | + } |
|
273 | + // Redirect to installer if not installed |
|
274 | + if (!$systemConfig->getValue('installed', false) && OC::$SUBURI !== '/index.php' && OC::$SUBURI !== '/status.php') { |
|
275 | + if (OC::$CLI) { |
|
276 | + throw new Exception('Not installed'); |
|
277 | + } else { |
|
278 | + $url = OC::$WEBROOT . '/index.php'; |
|
279 | + header('Location: ' . $url); |
|
280 | + } |
|
281 | + exit(); |
|
282 | + } |
|
283 | + } |
|
284 | + |
|
285 | + public static function checkMaintenanceMode(\OC\SystemConfig $systemConfig) { |
|
286 | + // Allow ajax update script to execute without being stopped |
|
287 | + if (((bool) $systemConfig->getValue('maintenance', false)) && OC::$SUBURI != '/core/ajax/update.php') { |
|
288 | + // send http status 503 |
|
289 | + http_response_code(503); |
|
290 | + header('Retry-After: 120'); |
|
291 | + |
|
292 | + // render error page |
|
293 | + $template = new OC_Template('', 'update.user', 'guest'); |
|
294 | + OC_Util::addScript('dist/maintenance'); |
|
295 | + OC_Util::addStyle('core', 'guest'); |
|
296 | + $template->printPage(); |
|
297 | + die(); |
|
298 | + } |
|
299 | + } |
|
300 | + |
|
301 | + /** |
|
302 | + * Prints the upgrade page |
|
303 | + * |
|
304 | + * @param \OC\SystemConfig $systemConfig |
|
305 | + */ |
|
306 | + private static function printUpgradePage(\OC\SystemConfig $systemConfig) { |
|
307 | + $disableWebUpdater = $systemConfig->getValue('upgrade.disable-web', false); |
|
308 | + $tooBig = false; |
|
309 | + if (!$disableWebUpdater) { |
|
310 | + $apps = \OC::$server->getAppManager(); |
|
311 | + if ($apps->isInstalled('user_ldap')) { |
|
312 | + $qb = \OC::$server->getDatabaseConnection()->getQueryBuilder(); |
|
313 | + |
|
314 | + $result = $qb->select($qb->func()->count('*', 'user_count')) |
|
315 | + ->from('ldap_user_mapping') |
|
316 | + ->execute(); |
|
317 | + $row = $result->fetch(); |
|
318 | + $result->closeCursor(); |
|
319 | + |
|
320 | + $tooBig = ($row['user_count'] > 50); |
|
321 | + } |
|
322 | + if (!$tooBig && $apps->isInstalled('user_saml')) { |
|
323 | + $qb = \OC::$server->getDatabaseConnection()->getQueryBuilder(); |
|
324 | + |
|
325 | + $result = $qb->select($qb->func()->count('*', 'user_count')) |
|
326 | + ->from('user_saml_users') |
|
327 | + ->execute(); |
|
328 | + $row = $result->fetch(); |
|
329 | + $result->closeCursor(); |
|
330 | + |
|
331 | + $tooBig = ($row['user_count'] > 50); |
|
332 | + } |
|
333 | + if (!$tooBig) { |
|
334 | + // count users |
|
335 | + $stats = \OC::$server->getUserManager()->countUsers(); |
|
336 | + $totalUsers = array_sum($stats); |
|
337 | + $tooBig = ($totalUsers > 50); |
|
338 | + } |
|
339 | + } |
|
340 | + $ignoreTooBigWarning = isset($_GET['IKnowThatThisIsABigInstanceAndTheUpdateRequestCouldRunIntoATimeoutAndHowToRestoreABackup']) && |
|
341 | + $_GET['IKnowThatThisIsABigInstanceAndTheUpdateRequestCouldRunIntoATimeoutAndHowToRestoreABackup'] === 'IAmSuperSureToDoThis'; |
|
342 | + |
|
343 | + if ($disableWebUpdater || ($tooBig && !$ignoreTooBigWarning)) { |
|
344 | + // send http status 503 |
|
345 | + http_response_code(503); |
|
346 | + header('Retry-After: 120'); |
|
347 | + |
|
348 | + // render error page |
|
349 | + $template = new OC_Template('', 'update.use-cli', 'guest'); |
|
350 | + $template->assign('productName', 'nextcloud'); // for now |
|
351 | + $template->assign('version', OC_Util::getVersionString()); |
|
352 | + $template->assign('tooBig', $tooBig); |
|
353 | + |
|
354 | + $template->printPage(); |
|
355 | + die(); |
|
356 | + } |
|
357 | + |
|
358 | + // check whether this is a core update or apps update |
|
359 | + $installedVersion = $systemConfig->getValue('version', '0.0.0'); |
|
360 | + $currentVersion = implode('.', \OCP\Util::getVersion()); |
|
361 | + |
|
362 | + // if not a core upgrade, then it's apps upgrade |
|
363 | + $isAppsOnlyUpgrade = version_compare($currentVersion, $installedVersion, '='); |
|
364 | + |
|
365 | + $oldTheme = $systemConfig->getValue('theme'); |
|
366 | + $systemConfig->setValue('theme', ''); |
|
367 | + OC_Util::addScript('update'); |
|
368 | + |
|
369 | + /** @var \OC\App\AppManager $appManager */ |
|
370 | + $appManager = \OC::$server->getAppManager(); |
|
371 | + |
|
372 | + $tmpl = new OC_Template('', 'update.admin', 'guest'); |
|
373 | + $tmpl->assign('version', OC_Util::getVersionString()); |
|
374 | + $tmpl->assign('isAppsOnlyUpgrade', $isAppsOnlyUpgrade); |
|
375 | + |
|
376 | + // get third party apps |
|
377 | + $ocVersion = \OCP\Util::getVersion(); |
|
378 | + $ocVersion = implode('.', $ocVersion); |
|
379 | + $incompatibleApps = $appManager->getIncompatibleApps($ocVersion); |
|
380 | + $incompatibleShippedApps = []; |
|
381 | + foreach ($incompatibleApps as $appInfo) { |
|
382 | + if ($appManager->isShipped($appInfo['id'])) { |
|
383 | + $incompatibleShippedApps[] = $appInfo['name'] . ' (' . $appInfo['id'] . ')'; |
|
384 | + } |
|
385 | + } |
|
386 | + |
|
387 | + if (!empty($incompatibleShippedApps)) { |
|
388 | + $l = \OC::$server->getL10N('core'); |
|
389 | + $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)]); |
|
390 | + 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); |
|
391 | + } |
|
392 | + |
|
393 | + $tmpl->assign('appsToUpgrade', $appManager->getAppsNeedingUpgrade($ocVersion)); |
|
394 | + $tmpl->assign('incompatibleAppsList', $incompatibleApps); |
|
395 | + try { |
|
396 | + $defaults = new \OC_Defaults(); |
|
397 | + $tmpl->assign('productName', $defaults->getName()); |
|
398 | + } catch (Throwable $error) { |
|
399 | + $tmpl->assign('productName', 'Nextcloud'); |
|
400 | + } |
|
401 | + $tmpl->assign('oldTheme', $oldTheme); |
|
402 | + $tmpl->printPage(); |
|
403 | + } |
|
404 | + |
|
405 | + public static function initSession() { |
|
406 | + if (self::$server->getRequest()->getServerProtocol() === 'https') { |
|
407 | + ini_set('session.cookie_secure', 'true'); |
|
408 | + } |
|
409 | + |
|
410 | + // prevents javascript from accessing php session cookies |
|
411 | + ini_set('session.cookie_httponly', 'true'); |
|
412 | + |
|
413 | + // set the cookie path to the Nextcloud directory |
|
414 | + $cookie_path = OC::$WEBROOT ? : '/'; |
|
415 | + ini_set('session.cookie_path', $cookie_path); |
|
416 | + |
|
417 | + // Let the session name be changed in the initSession Hook |
|
418 | + $sessionName = OC_Util::getInstanceId(); |
|
419 | + |
|
420 | + try { |
|
421 | + // set the session name to the instance id - which is unique |
|
422 | + $session = new \OC\Session\Internal($sessionName); |
|
423 | + |
|
424 | + $cryptoWrapper = \OC::$server->getSessionCryptoWrapper(); |
|
425 | + $session = $cryptoWrapper->wrapSession($session); |
|
426 | + self::$server->setSession($session); |
|
427 | + |
|
428 | + // if session can't be started break with http 500 error |
|
429 | + } catch (Exception $e) { |
|
430 | + \OC::$server->getLogger()->logException($e, ['app' => 'base']); |
|
431 | + //show the user a detailed error page |
|
432 | + OC_Template::printExceptionErrorPage($e, 500); |
|
433 | + die(); |
|
434 | + } |
|
435 | + |
|
436 | + $sessionLifeTime = self::getSessionLifeTime(); |
|
437 | + |
|
438 | + // session timeout |
|
439 | + if ($session->exists('LAST_ACTIVITY') && (time() - $session->get('LAST_ACTIVITY') > $sessionLifeTime)) { |
|
440 | + if (isset($_COOKIE[session_name()])) { |
|
441 | + setcookie(session_name(), '', -1, self::$WEBROOT ? : '/'); |
|
442 | + } |
|
443 | + \OC::$server->getUserSession()->logout(); |
|
444 | + } |
|
445 | + |
|
446 | + $session->set('LAST_ACTIVITY', time()); |
|
447 | + } |
|
448 | + |
|
449 | + /** |
|
450 | + * @return string |
|
451 | + */ |
|
452 | + private static function getSessionLifeTime() { |
|
453 | + return \OC::$server->getConfig()->getSystemValue('session_lifetime', 60 * 60 * 24); |
|
454 | + } |
|
455 | + |
|
456 | + /** |
|
457 | + * Try to set some values to the required Nextcloud default |
|
458 | + */ |
|
459 | + public static function setRequiredIniValues() { |
|
460 | + @ini_set('default_charset', 'UTF-8'); |
|
461 | + @ini_set('gd.jpeg_ignore_warning', '1'); |
|
462 | + } |
|
463 | + |
|
464 | + /** |
|
465 | + * Send the same site cookies |
|
466 | + */ |
|
467 | + private static function sendSameSiteCookies() { |
|
468 | + $cookieParams = session_get_cookie_params(); |
|
469 | + $secureCookie = ($cookieParams['secure'] === true) ? 'secure; ' : ''; |
|
470 | + $policies = [ |
|
471 | + 'lax', |
|
472 | + 'strict', |
|
473 | + ]; |
|
474 | + |
|
475 | + // Append __Host to the cookie if it meets the requirements |
|
476 | + $cookiePrefix = ''; |
|
477 | + if ($cookieParams['secure'] === true && $cookieParams['path'] === '/') { |
|
478 | + $cookiePrefix = '__Host-'; |
|
479 | + } |
|
480 | + |
|
481 | + foreach ($policies as $policy) { |
|
482 | + header( |
|
483 | + sprintf( |
|
484 | + 'Set-Cookie: %snc_sameSiteCookie%s=true; path=%s; httponly;' . $secureCookie . 'expires=Fri, 31-Dec-2100 23:59:59 GMT; SameSite=%s', |
|
485 | + $cookiePrefix, |
|
486 | + $policy, |
|
487 | + $cookieParams['path'], |
|
488 | + $policy |
|
489 | + ), |
|
490 | + false |
|
491 | + ); |
|
492 | + } |
|
493 | + } |
|
494 | + |
|
495 | + /** |
|
496 | + * Same Site cookie to further mitigate CSRF attacks. This cookie has to |
|
497 | + * be set in every request if cookies are sent to add a second level of |
|
498 | + * defense against CSRF. |
|
499 | + * |
|
500 | + * If the cookie is not sent this will set the cookie and reload the page. |
|
501 | + * We use an additional cookie since we want to protect logout CSRF and |
|
502 | + * also we can't directly interfere with PHP's session mechanism. |
|
503 | + */ |
|
504 | + private static function performSameSiteCookieProtection(\OCP\IConfig $config) { |
|
505 | + $request = \OC::$server->getRequest(); |
|
506 | + |
|
507 | + // Some user agents are notorious and don't really properly follow HTTP |
|
508 | + // specifications. For those, have an automated opt-out. Since the protection |
|
509 | + // for remote.php is applied in base.php as starting point we need to opt out |
|
510 | + // here. |
|
511 | + $incompatibleUserAgents = $config->getSystemValue('csrf.optout'); |
|
512 | + |
|
513 | + // Fallback, if csrf.optout is unset |
|
514 | + if (!is_array($incompatibleUserAgents)) { |
|
515 | + $incompatibleUserAgents = [ |
|
516 | + // OS X Finder |
|
517 | + '/^WebDAVFS/', |
|
518 | + // Windows webdav drive |
|
519 | + '/^Microsoft-WebDAV-MiniRedir/', |
|
520 | + ]; |
|
521 | + } |
|
522 | + |
|
523 | + if ($request->isUserAgent($incompatibleUserAgents)) { |
|
524 | + return; |
|
525 | + } |
|
526 | + |
|
527 | + if (count($_COOKIE) > 0) { |
|
528 | + $requestUri = $request->getScriptName(); |
|
529 | + $processingScript = explode('/', $requestUri); |
|
530 | + $processingScript = $processingScript[count($processingScript) - 1]; |
|
531 | + |
|
532 | + // index.php routes are handled in the middleware |
|
533 | + if ($processingScript === 'index.php') { |
|
534 | + return; |
|
535 | + } |
|
536 | + |
|
537 | + // All other endpoints require the lax and the strict cookie |
|
538 | + if (!$request->passesStrictCookieCheck()) { |
|
539 | + self::sendSameSiteCookies(); |
|
540 | + // Debug mode gets access to the resources without strict cookie |
|
541 | + // due to the fact that the SabreDAV browser also lives there. |
|
542 | + if (!$config->getSystemValue('debug', false)) { |
|
543 | + http_response_code(\OCP\AppFramework\Http::STATUS_SERVICE_UNAVAILABLE); |
|
544 | + exit(); |
|
545 | + } |
|
546 | + } |
|
547 | + } elseif (!isset($_COOKIE['nc_sameSiteCookielax']) || !isset($_COOKIE['nc_sameSiteCookiestrict'])) { |
|
548 | + self::sendSameSiteCookies(); |
|
549 | + } |
|
550 | + } |
|
551 | + |
|
552 | + public static function init() { |
|
553 | + // calculate the root directories |
|
554 | + OC::$SERVERROOT = str_replace("\\", '/', substr(__DIR__, 0, -4)); |
|
555 | + |
|
556 | + // register autoloader |
|
557 | + $loaderStart = microtime(true); |
|
558 | + require_once __DIR__ . '/autoloader.php'; |
|
559 | + self::$loader = new \OC\Autoloader([ |
|
560 | + OC::$SERVERROOT . '/lib/private/legacy', |
|
561 | + ]); |
|
562 | + if (defined('PHPUNIT_RUN')) { |
|
563 | + self::$loader->addValidRoot(OC::$SERVERROOT . '/tests'); |
|
564 | + } |
|
565 | + spl_autoload_register([self::$loader, 'load']); |
|
566 | + $loaderEnd = microtime(true); |
|
567 | + |
|
568 | + self::$CLI = (php_sapi_name() == 'cli'); |
|
569 | + |
|
570 | + // Add default composer PSR-4 autoloader |
|
571 | + self::$composerAutoloader = require_once OC::$SERVERROOT . '/lib/composer/autoload.php'; |
|
572 | + |
|
573 | + try { |
|
574 | + self::initPaths(); |
|
575 | + // setup 3rdparty autoloader |
|
576 | + $vendorAutoLoad = OC::$SERVERROOT. '/3rdparty/autoload.php'; |
|
577 | + if (!file_exists($vendorAutoLoad)) { |
|
578 | + throw new \RuntimeException('Composer autoloader not found, unable to continue. Check the folder "3rdparty". Running "git submodule update --init" will initialize the git submodule that handles the subfolder "3rdparty".'); |
|
579 | + } |
|
580 | + require_once $vendorAutoLoad; |
|
581 | + } catch (\RuntimeException $e) { |
|
582 | + if (!self::$CLI) { |
|
583 | + http_response_code(503); |
|
584 | + } |
|
585 | + // we can't use the template error page here, because this needs the |
|
586 | + // DI container which isn't available yet |
|
587 | + print($e->getMessage()); |
|
588 | + exit(); |
|
589 | + } |
|
590 | + |
|
591 | + // setup the basic server |
|
592 | + self::$server = new \OC\Server(\OC::$WEBROOT, self::$config); |
|
593 | + self::$server->boot(); |
|
594 | + $eventLogger = \OC::$server->getEventLogger(); |
|
595 | + $eventLogger->log('autoloader', 'Autoloader', $loaderStart, $loaderEnd); |
|
596 | + $eventLogger->start('boot', 'Initialize'); |
|
597 | + |
|
598 | + // Override php.ini and log everything if we're troubleshooting |
|
599 | + if (self::$config->getValue('loglevel') === ILogger::DEBUG) { |
|
600 | + error_reporting(E_ALL); |
|
601 | + } |
|
602 | + |
|
603 | + // Don't display errors and log them |
|
604 | + @ini_set('display_errors', '0'); |
|
605 | + @ini_set('log_errors', '1'); |
|
606 | + |
|
607 | + if (!date_default_timezone_set('UTC')) { |
|
608 | + throw new \RuntimeException('Could not set timezone to UTC'); |
|
609 | + } |
|
610 | + |
|
611 | + //try to configure php to enable big file uploads. |
|
612 | + //this doesn´t work always depending on the webserver and php configuration. |
|
613 | + //Let´s try to overwrite some defaults anyway |
|
614 | + |
|
615 | + //try to set the maximum execution time to 60min |
|
616 | + if (strpos(@ini_get('disable_functions'), 'set_time_limit') === false) { |
|
617 | + @set_time_limit(3600); |
|
618 | + } |
|
619 | + @ini_set('max_execution_time', '3600'); |
|
620 | + @ini_set('max_input_time', '3600'); |
|
621 | + |
|
622 | + self::setRequiredIniValues(); |
|
623 | + self::handleAuthHeaders(); |
|
624 | + $systemConfig = \OC::$server->get(\OC\SystemConfig::class); |
|
625 | + self::registerAutoloaderCache($systemConfig); |
|
626 | + |
|
627 | + // initialize intl fallback if necessary |
|
628 | + OC_Util::isSetLocaleWorking(); |
|
629 | + |
|
630 | + $config = \OC::$server->get(\OCP\IConfig::class); |
|
631 | + if (!defined('PHPUNIT_RUN')) { |
|
632 | + OC\Log\ErrorHandler::setLogger(\OC::$server->getLogger()); |
|
633 | + $debug = $config->getSystemValue('debug', false); |
|
634 | + OC\Log\ErrorHandler::register($debug); |
|
635 | + } |
|
636 | + |
|
637 | + /** @var \OC\AppFramework\Bootstrap\Coordinator $bootstrapCoordinator */ |
|
638 | + $bootstrapCoordinator = \OC::$server->query(\OC\AppFramework\Bootstrap\Coordinator::class); |
|
639 | + $bootstrapCoordinator->runInitialRegistration(); |
|
640 | + |
|
641 | + $eventLogger->start('init_session', 'Initialize session'); |
|
642 | + OC_App::loadApps(['session']); |
|
643 | + if (!self::$CLI) { |
|
644 | + self::initSession(); |
|
645 | + } |
|
646 | + $eventLogger->end('init_session'); |
|
647 | + self::checkConfig(); |
|
648 | + self::checkInstalled($systemConfig); |
|
649 | + |
|
650 | + OC_Response::addSecurityHeaders(); |
|
651 | + |
|
652 | + self::performSameSiteCookieProtection($config); |
|
653 | + |
|
654 | + if (!defined('OC_CONSOLE')) { |
|
655 | + $errors = OC_Util::checkServer($systemConfig); |
|
656 | + if (count($errors) > 0) { |
|
657 | + if (!self::$CLI) { |
|
658 | + http_response_code(503); |
|
659 | + OC_Util::addStyle('guest'); |
|
660 | + try { |
|
661 | + OC_Template::printGuestPage('', 'error', ['errors' => $errors]); |
|
662 | + exit; |
|
663 | + } catch (\Exception $e) { |
|
664 | + // In case any error happens when showing the error page, we simply fall back to posting the text. |
|
665 | + // This might be the case when e.g. the data directory is broken and we can not load/write SCSS to/from it. |
|
666 | + } |
|
667 | + } |
|
668 | + |
|
669 | + // Convert l10n string into regular string for usage in database |
|
670 | + $staticErrors = []; |
|
671 | + foreach ($errors as $error) { |
|
672 | + echo $error['error'] . "\n"; |
|
673 | + echo $error['hint'] . "\n\n"; |
|
674 | + $staticErrors[] = [ |
|
675 | + 'error' => (string)$error['error'], |
|
676 | + 'hint' => (string)$error['hint'], |
|
677 | + ]; |
|
678 | + } |
|
679 | + |
|
680 | + try { |
|
681 | + $config->setAppValue('core', 'cronErrors', json_encode($staticErrors)); |
|
682 | + } catch (\Exception $e) { |
|
683 | + echo('Writing to database failed'); |
|
684 | + } |
|
685 | + exit(1); |
|
686 | + } elseif (self::$CLI && $config->getSystemValue('installed', false)) { |
|
687 | + $config->deleteAppValue('core', 'cronErrors'); |
|
688 | + } |
|
689 | + } |
|
690 | + //try to set the session lifetime |
|
691 | + $sessionLifeTime = self::getSessionLifeTime(); |
|
692 | + @ini_set('gc_maxlifetime', (string)$sessionLifeTime); |
|
693 | + |
|
694 | + // User and Groups |
|
695 | + if (!$systemConfig->getValue("installed", false)) { |
|
696 | + self::$server->getSession()->set('user_id', ''); |
|
697 | + } |
|
698 | + |
|
699 | + OC_User::useBackend(new \OC\User\Database()); |
|
700 | + \OC::$server->getGroupManager()->addBackend(new \OC\Group\Database()); |
|
701 | + |
|
702 | + // Subscribe to the hook |
|
703 | + \OCP\Util::connectHook( |
|
704 | + '\OCA\Files_Sharing\API\Server2Server', |
|
705 | + 'preLoginNameUsedAsUserName', |
|
706 | + '\OC\User\Database', |
|
707 | + 'preLoginNameUsedAsUserName' |
|
708 | + ); |
|
709 | + |
|
710 | + //setup extra user backends |
|
711 | + if (!\OCP\Util::needUpgrade()) { |
|
712 | + OC_User::setupBackends(); |
|
713 | + } else { |
|
714 | + // Run upgrades in incognito mode |
|
715 | + OC_User::setIncognitoMode(true); |
|
716 | + } |
|
717 | + |
|
718 | + self::registerCleanupHooks($systemConfig); |
|
719 | + self::registerFilesystemHooks(); |
|
720 | + self::registerShareHooks($systemConfig); |
|
721 | + self::registerEncryptionWrapperAndHooks(); |
|
722 | + self::registerAccountHooks(); |
|
723 | + self::registerResourceCollectionHooks(); |
|
724 | + self::registerAppRestrictionsHooks(); |
|
725 | + |
|
726 | + // Make sure that the application class is not loaded before the database is setup |
|
727 | + if ($systemConfig->getValue("installed", false)) { |
|
728 | + OC_App::loadApp('settings'); |
|
729 | + } |
|
730 | + |
|
731 | + //make sure temporary files are cleaned up |
|
732 | + $tmpManager = \OC::$server->getTempManager(); |
|
733 | + register_shutdown_function([$tmpManager, 'clean']); |
|
734 | + $lockProvider = \OC::$server->getLockingProvider(); |
|
735 | + register_shutdown_function([$lockProvider, 'releaseAll']); |
|
736 | + |
|
737 | + // Check whether the sample configuration has been copied |
|
738 | + if ($systemConfig->getValue('copied_sample_config', false)) { |
|
739 | + $l = \OC::$server->getL10N('lib'); |
|
740 | + OC_Template::printErrorPage( |
|
741 | + $l->t('Sample configuration detected'), |
|
742 | + $l->t('It has been detected that the sample configuration has been copied. This can break your installation and is unsupported. Please read the documentation before performing changes on config.php'), |
|
743 | + 503 |
|
744 | + ); |
|
745 | + return; |
|
746 | + } |
|
747 | + |
|
748 | + $request = \OC::$server->getRequest(); |
|
749 | + $host = $request->getInsecureServerHost(); |
|
750 | + /** |
|
751 | + * if the host passed in headers isn't trusted |
|
752 | + * FIXME: Should not be in here at all :see_no_evil: |
|
753 | + */ |
|
754 | + if (!OC::$CLI |
|
755 | + && !\OC::$server->getTrustedDomainHelper()->isTrustedDomain($host) |
|
756 | + && $config->getSystemValue('installed', false) |
|
757 | + ) { |
|
758 | + // Allow access to CSS resources |
|
759 | + $isScssRequest = false; |
|
760 | + if (strpos($request->getPathInfo(), '/css/') === 0) { |
|
761 | + $isScssRequest = true; |
|
762 | + } |
|
763 | + |
|
764 | + if (substr($request->getRequestUri(), -11) === '/status.php') { |
|
765 | + http_response_code(400); |
|
766 | + header('Content-Type: application/json'); |
|
767 | + echo '{"error": "Trusted domain error.", "code": 15}'; |
|
768 | + exit(); |
|
769 | + } |
|
770 | + |
|
771 | + if (!$isScssRequest) { |
|
772 | + http_response_code(400); |
|
773 | + |
|
774 | + \OC::$server->getLogger()->info( |
|
775 | + 'Trusted domain error. "{remoteAddress}" tried to access using "{host}" as host.', |
|
776 | + [ |
|
777 | + 'app' => 'core', |
|
778 | + 'remoteAddress' => $request->getRemoteAddress(), |
|
779 | + 'host' => $host, |
|
780 | + ] |
|
781 | + ); |
|
782 | + |
|
783 | + $tmpl = new OCP\Template('core', 'untrustedDomain', 'guest'); |
|
784 | + $tmpl->assign('docUrl', \OC::$server->getURLGenerator()->linkToDocs('admin-trusted-domains')); |
|
785 | + $tmpl->printPage(); |
|
786 | + |
|
787 | + exit(); |
|
788 | + } |
|
789 | + } |
|
790 | + $eventLogger->end('boot'); |
|
791 | + } |
|
792 | + |
|
793 | + /** |
|
794 | + * register hooks for the cleanup of cache and bruteforce protection |
|
795 | + */ |
|
796 | + public static function registerCleanupHooks(\OC\SystemConfig $systemConfig) { |
|
797 | + //don't try to do this before we are properly setup |
|
798 | + if ($systemConfig->getValue('installed', false) && !\OCP\Util::needUpgrade()) { |
|
799 | + |
|
800 | + // NOTE: This will be replaced to use OCP |
|
801 | + $userSession = self::$server->getUserSession(); |
|
802 | + $userSession->listen('\OC\User', 'postLogin', function () use ($userSession) { |
|
803 | + if (!defined('PHPUNIT_RUN') && $userSession->isLoggedIn()) { |
|
804 | + // reset brute force delay for this IP address and username |
|
805 | + $uid = \OC::$server->getUserSession()->getUser()->getUID(); |
|
806 | + $request = \OC::$server->getRequest(); |
|
807 | + $throttler = \OC::$server->getBruteForceThrottler(); |
|
808 | + $throttler->resetDelay($request->getRemoteAddress(), 'login', ['user' => $uid]); |
|
809 | + } |
|
810 | + |
|
811 | + try { |
|
812 | + $cache = new \OC\Cache\File(); |
|
813 | + $cache->gc(); |
|
814 | + } catch (\OC\ServerNotAvailableException $e) { |
|
815 | + // not a GC exception, pass it on |
|
816 | + throw $e; |
|
817 | + } catch (\OC\ForbiddenException $e) { |
|
818 | + // filesystem blocked for this request, ignore |
|
819 | + } catch (\Exception $e) { |
|
820 | + // a GC exception should not prevent users from using OC, |
|
821 | + // so log the exception |
|
822 | + \OC::$server->getLogger()->logException($e, [ |
|
823 | + 'message' => 'Exception when running cache gc.', |
|
824 | + 'level' => ILogger::WARN, |
|
825 | + 'app' => 'core', |
|
826 | + ]); |
|
827 | + } |
|
828 | + }); |
|
829 | + } |
|
830 | + } |
|
831 | + |
|
832 | + private static function registerEncryptionWrapperAndHooks() { |
|
833 | + $manager = self::$server->getEncryptionManager(); |
|
834 | + \OCP\Util::connectHook('OC_Filesystem', 'preSetup', $manager, 'setupStorage'); |
|
835 | + |
|
836 | + $enabled = $manager->isEnabled(); |
|
837 | + if ($enabled) { |
|
838 | + \OCP\Util::connectHook(Share::class, 'post_shared', HookManager::class, 'postShared'); |
|
839 | + \OCP\Util::connectHook(Share::class, 'post_unshare', HookManager::class, 'postUnshared'); |
|
840 | + \OCP\Util::connectHook('OC_Filesystem', 'post_rename', HookManager::class, 'postRename'); |
|
841 | + \OCP\Util::connectHook('\OCA\Files_Trashbin\Trashbin', 'post_restore', HookManager::class, 'postRestore'); |
|
842 | + } |
|
843 | + } |
|
844 | + |
|
845 | + private static function registerAccountHooks() { |
|
846 | + $hookHandler = \OC::$server->get(\OC\Accounts\Hooks::class); |
|
847 | + \OCP\Util::connectHook('OC_User', 'changeUser', $hookHandler, 'changeUserHook'); |
|
848 | + } |
|
849 | + |
|
850 | + private static function registerAppRestrictionsHooks() { |
|
851 | + /** @var \OC\Group\Manager $groupManager */ |
|
852 | + $groupManager = self::$server->query(\OCP\IGroupManager::class); |
|
853 | + $groupManager->listen('\OC\Group', 'postDelete', function (\OCP\IGroup $group) { |
|
854 | + $appManager = self::$server->getAppManager(); |
|
855 | + $apps = $appManager->getEnabledAppsForGroup($group); |
|
856 | + foreach ($apps as $appId) { |
|
857 | + $restrictions = $appManager->getAppRestriction($appId); |
|
858 | + if (empty($restrictions)) { |
|
859 | + continue; |
|
860 | + } |
|
861 | + $key = array_search($group->getGID(), $restrictions); |
|
862 | + unset($restrictions[$key]); |
|
863 | + $restrictions = array_values($restrictions); |
|
864 | + if (empty($restrictions)) { |
|
865 | + $appManager->disableApp($appId); |
|
866 | + } else { |
|
867 | + $appManager->enableAppForGroups($appId, $restrictions); |
|
868 | + } |
|
869 | + } |
|
870 | + }); |
|
871 | + } |
|
872 | + |
|
873 | + private static function registerResourceCollectionHooks() { |
|
874 | + \OC\Collaboration\Resources\Listener::register(\OC::$server->getEventDispatcher()); |
|
875 | + } |
|
876 | + |
|
877 | + /** |
|
878 | + * register hooks for the filesystem |
|
879 | + */ |
|
880 | + public static function registerFilesystemHooks() { |
|
881 | + // Check for blacklisted files |
|
882 | + OC_Hook::connect('OC_Filesystem', 'write', Filesystem::class, 'isBlacklisted'); |
|
883 | + OC_Hook::connect('OC_Filesystem', 'rename', Filesystem::class, 'isBlacklisted'); |
|
884 | + } |
|
885 | + |
|
886 | + /** |
|
887 | + * register hooks for sharing |
|
888 | + */ |
|
889 | + public static function registerShareHooks(\OC\SystemConfig $systemConfig) { |
|
890 | + if ($systemConfig->getValue('installed')) { |
|
891 | + OC_Hook::connect('OC_User', 'post_deleteUser', Hooks::class, 'post_deleteUser'); |
|
892 | + OC_Hook::connect('OC_User', 'post_deleteGroup', Hooks::class, 'post_deleteGroup'); |
|
893 | + |
|
894 | + /** @var IEventDispatcher $dispatcher */ |
|
895 | + $dispatcher = \OC::$server->get(IEventDispatcher::class); |
|
896 | + $dispatcher->addServiceListener(UserRemovedEvent::class, \OC\Share20\UserRemovedListener::class); |
|
897 | + } |
|
898 | + } |
|
899 | + |
|
900 | + protected static function registerAutoloaderCache(\OC\SystemConfig $systemConfig) { |
|
901 | + // The class loader takes an optional low-latency cache, which MUST be |
|
902 | + // namespaced. The instanceid is used for namespacing, but might be |
|
903 | + // unavailable at this point. Furthermore, it might not be possible to |
|
904 | + // generate an instanceid via \OC_Util::getInstanceId() because the |
|
905 | + // config file may not be writable. As such, we only register a class |
|
906 | + // loader cache if instanceid is available without trying to create one. |
|
907 | + $instanceId = $systemConfig->getValue('instanceid', null); |
|
908 | + if ($instanceId) { |
|
909 | + try { |
|
910 | + $memcacheFactory = \OC::$server->getMemCacheFactory(); |
|
911 | + self::$loader->setMemoryCache($memcacheFactory->createLocal('Autoloader')); |
|
912 | + } catch (\Exception $ex) { |
|
913 | + } |
|
914 | + } |
|
915 | + } |
|
916 | + |
|
917 | + /** |
|
918 | + * Handle the request |
|
919 | + */ |
|
920 | + public static function handleRequest() { |
|
921 | + \OC::$server->getEventLogger()->start('handle_request', 'Handle request'); |
|
922 | + $systemConfig = \OC::$server->getSystemConfig(); |
|
923 | + |
|
924 | + // Check if Nextcloud is installed or in maintenance (update) mode |
|
925 | + if (!$systemConfig->getValue('installed', false)) { |
|
926 | + \OC::$server->getSession()->clear(); |
|
927 | + $setupHelper = new OC\Setup( |
|
928 | + $systemConfig, |
|
929 | + \OC::$server->get(\bantu\IniGetWrapper\IniGetWrapper::class), |
|
930 | + \OC::$server->getL10N('lib'), |
|
931 | + \OC::$server->query(\OCP\Defaults::class), |
|
932 | + \OC::$server->get(\Psr\Log\LoggerInterface::class), |
|
933 | + \OC::$server->getSecureRandom(), |
|
934 | + \OC::$server->query(\OC\Installer::class) |
|
935 | + ); |
|
936 | + $controller = new OC\Core\Controller\SetupController($setupHelper); |
|
937 | + $controller->run($_POST); |
|
938 | + exit(); |
|
939 | + } |
|
940 | + |
|
941 | + $request = \OC::$server->getRequest(); |
|
942 | + $requestPath = $request->getRawPathInfo(); |
|
943 | + if ($requestPath === '/heartbeat') { |
|
944 | + return; |
|
945 | + } |
|
946 | + if (substr($requestPath, -3) !== '.js') { // we need these files during the upgrade |
|
947 | + self::checkMaintenanceMode($systemConfig); |
|
948 | + |
|
949 | + if (\OCP\Util::needUpgrade()) { |
|
950 | + if (function_exists('opcache_reset')) { |
|
951 | + opcache_reset(); |
|
952 | + } |
|
953 | + if (!((bool) $systemConfig->getValue('maintenance', false))) { |
|
954 | + self::printUpgradePage($systemConfig); |
|
955 | + exit(); |
|
956 | + } |
|
957 | + } |
|
958 | + } |
|
959 | + |
|
960 | + // emergency app disabling |
|
961 | + if ($requestPath === '/disableapp' |
|
962 | + && $request->getMethod() === 'POST' |
|
963 | + && ((array)$request->getParam('appid')) !== '' |
|
964 | + ) { |
|
965 | + \OC_JSON::callCheck(); |
|
966 | + \OC_JSON::checkAdminUser(); |
|
967 | + $appIds = (array)$request->getParam('appid'); |
|
968 | + foreach ($appIds as $appId) { |
|
969 | + $appId = \OC_App::cleanAppId($appId); |
|
970 | + \OC::$server->getAppManager()->disableApp($appId); |
|
971 | + } |
|
972 | + \OC_JSON::success(); |
|
973 | + exit(); |
|
974 | + } |
|
975 | + |
|
976 | + // Always load authentication apps |
|
977 | + OC_App::loadApps(['authentication']); |
|
978 | + |
|
979 | + // Load minimum set of apps |
|
980 | + if (!\OCP\Util::needUpgrade() |
|
981 | + && !((bool) $systemConfig->getValue('maintenance', false))) { |
|
982 | + // For logged-in users: Load everything |
|
983 | + if (\OC::$server->getUserSession()->isLoggedIn()) { |
|
984 | + OC_App::loadApps(); |
|
985 | + } else { |
|
986 | + // For guests: Load only filesystem and logging |
|
987 | + OC_App::loadApps(['filesystem', 'logging']); |
|
988 | + self::handleLogin($request); |
|
989 | + } |
|
990 | + } |
|
991 | + |
|
992 | + if (!self::$CLI) { |
|
993 | + try { |
|
994 | + if (!((bool) $systemConfig->getValue('maintenance', false)) && !\OCP\Util::needUpgrade()) { |
|
995 | + OC_App::loadApps(['filesystem', 'logging']); |
|
996 | + OC_App::loadApps(); |
|
997 | + } |
|
998 | + OC::$server->get(\OC\Route\Router::class)->match($request->getRawPathInfo()); |
|
999 | + return; |
|
1000 | + } catch (Symfony\Component\Routing\Exception\ResourceNotFoundException $e) { |
|
1001 | + //header('HTTP/1.0 404 Not Found'); |
|
1002 | + } catch (Symfony\Component\Routing\Exception\MethodNotAllowedException $e) { |
|
1003 | + http_response_code(405); |
|
1004 | + return; |
|
1005 | + } |
|
1006 | + } |
|
1007 | + |
|
1008 | + // Handle WebDAV |
|
1009 | + if (isset($_SERVER['REQUEST_METHOD']) && $_SERVER['REQUEST_METHOD'] === 'PROPFIND') { |
|
1010 | + // not allowed any more to prevent people |
|
1011 | + // mounting this root directly. |
|
1012 | + // Users need to mount remote.php/webdav instead. |
|
1013 | + http_response_code(405); |
|
1014 | + return; |
|
1015 | + } |
|
1016 | + |
|
1017 | + // Someone is logged in |
|
1018 | + if (\OC::$server->getUserSession()->isLoggedIn()) { |
|
1019 | + OC_App::loadApps(); |
|
1020 | + OC_User::setupBackends(); |
|
1021 | + OC_Util::setupFS(); |
|
1022 | + // FIXME |
|
1023 | + // Redirect to default application |
|
1024 | + OC_Util::redirectToDefaultPage(); |
|
1025 | + } else { |
|
1026 | + // Not handled and not logged in |
|
1027 | + header('Location: '.\OC::$server->getURLGenerator()->linkToRouteAbsolute('core.login.showLoginForm')); |
|
1028 | + } |
|
1029 | + } |
|
1030 | + |
|
1031 | + /** |
|
1032 | + * Check login: apache auth, auth token, basic auth |
|
1033 | + * |
|
1034 | + * @param OCP\IRequest $request |
|
1035 | + * @return boolean |
|
1036 | + */ |
|
1037 | + public static function handleLogin(OCP\IRequest $request) { |
|
1038 | + $userSession = self::$server->getUserSession(); |
|
1039 | + if (OC_User::handleApacheAuth()) { |
|
1040 | + return true; |
|
1041 | + } |
|
1042 | + if ($userSession->tryTokenLogin($request)) { |
|
1043 | + return true; |
|
1044 | + } |
|
1045 | + if (isset($_COOKIE['nc_username']) |
|
1046 | + && isset($_COOKIE['nc_token']) |
|
1047 | + && isset($_COOKIE['nc_session_id']) |
|
1048 | + && $userSession->loginWithCookie($_COOKIE['nc_username'], $_COOKIE['nc_token'], $_COOKIE['nc_session_id'])) { |
|
1049 | + return true; |
|
1050 | + } |
|
1051 | + if ($userSession->tryBasicAuthLogin($request, \OC::$server->getBruteForceThrottler())) { |
|
1052 | + return true; |
|
1053 | + } |
|
1054 | + return false; |
|
1055 | + } |
|
1056 | + |
|
1057 | + protected static function handleAuthHeaders() { |
|
1058 | + //copy http auth headers for apache+php-fcgid work around |
|
1059 | + if (isset($_SERVER['HTTP_XAUTHORIZATION']) && !isset($_SERVER['HTTP_AUTHORIZATION'])) { |
|
1060 | + $_SERVER['HTTP_AUTHORIZATION'] = $_SERVER['HTTP_XAUTHORIZATION']; |
|
1061 | + } |
|
1062 | + |
|
1063 | + // Extract PHP_AUTH_USER/PHP_AUTH_PW from other headers if necessary. |
|
1064 | + $vars = [ |
|
1065 | + 'HTTP_AUTHORIZATION', // apache+php-cgi work around |
|
1066 | + 'REDIRECT_HTTP_AUTHORIZATION', // apache+php-cgi alternative |
|
1067 | + ]; |
|
1068 | + foreach ($vars as $var) { |
|
1069 | + if (isset($_SERVER[$var]) && preg_match('/Basic\s+(.*)$/i', $_SERVER[$var], $matches)) { |
|
1070 | + $credentials = explode(':', base64_decode($matches[1]), 2); |
|
1071 | + if (count($credentials) === 2) { |
|
1072 | + $_SERVER['PHP_AUTH_USER'] = $credentials[0]; |
|
1073 | + $_SERVER['PHP_AUTH_PW'] = $credentials[1]; |
|
1074 | + break; |
|
1075 | + } |
|
1076 | + } |
|
1077 | + } |
|
1078 | + } |
|
1079 | 1079 | } |
1080 | 1080 | |
1081 | 1081 | OC::init(); |
@@ -138,13 +138,13 @@ discard block |
||
138 | 138 | */ |
139 | 139 | public static function initPaths() { |
140 | 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/'; |
|
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 | 144 | } elseif ($dir = getenv('NEXTCLOUD_CONFIG_DIR')) { |
145 | - self::$configDir = rtrim($dir, '/') . '/'; |
|
145 | + self::$configDir = rtrim($dir, '/').'/'; |
|
146 | 146 | } else { |
147 | - self::$configDir = OC::$SERVERROOT . '/config/'; |
|
147 | + self::$configDir = OC::$SERVERROOT.'/config/'; |
|
148 | 148 | } |
149 | 149 | self::$config = new \OC\Config(self::$configDir); |
150 | 150 | |
@@ -166,9 +166,9 @@ discard block |
||
166 | 166 | //make sure suburi follows the same rules as scriptName |
167 | 167 | if (substr(OC::$SUBURI, -9) != 'index.php') { |
168 | 168 | if (substr(OC::$SUBURI, -1) != '/') { |
169 | - OC::$SUBURI = OC::$SUBURI . '/'; |
|
169 | + OC::$SUBURI = OC::$SUBURI.'/'; |
|
170 | 170 | } |
171 | - OC::$SUBURI = OC::$SUBURI . 'index.php'; |
|
171 | + OC::$SUBURI = OC::$SUBURI.'index.php'; |
|
172 | 172 | } |
173 | 173 | } |
174 | 174 | |
@@ -180,7 +180,7 @@ discard block |
||
180 | 180 | OC::$WEBROOT = substr($scriptName, 0, 0 - strlen(OC::$SUBURI)); |
181 | 181 | |
182 | 182 | if (OC::$WEBROOT != '' && OC::$WEBROOT[0] !== '/') { |
183 | - OC::$WEBROOT = '/' . OC::$WEBROOT; |
|
183 | + OC::$WEBROOT = '/'.OC::$WEBROOT; |
|
184 | 184 | } |
185 | 185 | } else { |
186 | 186 | // The scriptName is not ending with OC::$SUBURI |
@@ -209,8 +209,8 @@ discard block |
||
209 | 209 | OC::$APPSROOTS[] = $paths; |
210 | 210 | } |
211 | 211 | } |
212 | - } elseif (file_exists(OC::$SERVERROOT . '/apps')) { |
|
213 | - OC::$APPSROOTS[] = ['path' => OC::$SERVERROOT . '/apps', 'url' => '/apps', 'writable' => true]; |
|
212 | + } elseif (file_exists(OC::$SERVERROOT.'/apps')) { |
|
213 | + OC::$APPSROOTS[] = ['path' => OC::$SERVERROOT.'/apps', 'url' => '/apps', 'writable' => true]; |
|
214 | 214 | } |
215 | 215 | |
216 | 216 | if (empty(OC::$APPSROOTS)) { |
@@ -236,7 +236,7 @@ discard block |
||
236 | 236 | $l = \OC::$server->getL10N('lib'); |
237 | 237 | |
238 | 238 | // Create config if it does not already exist |
239 | - $configFilePath = self::$configDir .'/config.php'; |
|
239 | + $configFilePath = self::$configDir.'/config.php'; |
|
240 | 240 | if (!file_exists($configFilePath)) { |
241 | 241 | @touch($configFilePath); |
242 | 242 | } |
@@ -252,14 +252,14 @@ discard block |
||
252 | 252 | echo $l->t('This can usually be fixed by giving the webserver write access to the config directory.')."\n"; |
253 | 253 | echo "\n"; |
254 | 254 | 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"; |
255 | - echo $l->t('See %s', [ $urlGenerator->linkToDocs('admin-config') ])."\n"; |
|
255 | + echo $l->t('See %s', [$urlGenerator->linkToDocs('admin-config')])."\n"; |
|
256 | 256 | exit; |
257 | 257 | } else { |
258 | 258 | OC_Template::printErrorPage( |
259 | 259 | $l->t('Cannot write into "config" directory!'), |
260 | - $l->t('This can usually be fixed by giving the webserver write access to the config directory.') . ' ' |
|
261 | - . $l->t('But, if you prefer to keep config.php file read only, set the option "config_is_read_only" to true in it.') . ' ' |
|
262 | - . $l->t('See %s', [ $urlGenerator->linkToDocs('admin-config') ]), |
|
260 | + $l->t('This can usually be fixed by giving the webserver write access to the config directory.').' ' |
|
261 | + . $l->t('But, if you prefer to keep config.php file read only, set the option "config_is_read_only" to true in it.').' ' |
|
262 | + . $l->t('See %s', [$urlGenerator->linkToDocs('admin-config')]), |
|
263 | 263 | 503 |
264 | 264 | ); |
265 | 265 | } |
@@ -275,8 +275,8 @@ discard block |
||
275 | 275 | if (OC::$CLI) { |
276 | 276 | throw new Exception('Not installed'); |
277 | 277 | } else { |
278 | - $url = OC::$WEBROOT . '/index.php'; |
|
279 | - header('Location: ' . $url); |
|
278 | + $url = OC::$WEBROOT.'/index.php'; |
|
279 | + header('Location: '.$url); |
|
280 | 280 | } |
281 | 281 | exit(); |
282 | 282 | } |
@@ -380,14 +380,14 @@ discard block |
||
380 | 380 | $incompatibleShippedApps = []; |
381 | 381 | foreach ($incompatibleApps as $appInfo) { |
382 | 382 | if ($appManager->isShipped($appInfo['id'])) { |
383 | - $incompatibleShippedApps[] = $appInfo['name'] . ' (' . $appInfo['id'] . ')'; |
|
383 | + $incompatibleShippedApps[] = $appInfo['name'].' ('.$appInfo['id'].')'; |
|
384 | 384 | } |
385 | 385 | } |
386 | 386 | |
387 | 387 | if (!empty($incompatibleShippedApps)) { |
388 | 388 | $l = \OC::$server->getL10N('core'); |
389 | 389 | $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)]); |
390 | - 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); |
|
390 | + 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); |
|
391 | 391 | } |
392 | 392 | |
393 | 393 | $tmpl->assign('appsToUpgrade', $appManager->getAppsNeedingUpgrade($ocVersion)); |
@@ -411,7 +411,7 @@ discard block |
||
411 | 411 | ini_set('session.cookie_httponly', 'true'); |
412 | 412 | |
413 | 413 | // set the cookie path to the Nextcloud directory |
414 | - $cookie_path = OC::$WEBROOT ? : '/'; |
|
414 | + $cookie_path = OC::$WEBROOT ?: '/'; |
|
415 | 415 | ini_set('session.cookie_path', $cookie_path); |
416 | 416 | |
417 | 417 | // Let the session name be changed in the initSession Hook |
@@ -438,7 +438,7 @@ discard block |
||
438 | 438 | // session timeout |
439 | 439 | if ($session->exists('LAST_ACTIVITY') && (time() - $session->get('LAST_ACTIVITY') > $sessionLifeTime)) { |
440 | 440 | if (isset($_COOKIE[session_name()])) { |
441 | - setcookie(session_name(), '', -1, self::$WEBROOT ? : '/'); |
|
441 | + setcookie(session_name(), '', -1, self::$WEBROOT ?: '/'); |
|
442 | 442 | } |
443 | 443 | \OC::$server->getUserSession()->logout(); |
444 | 444 | } |
@@ -481,7 +481,7 @@ discard block |
||
481 | 481 | foreach ($policies as $policy) { |
482 | 482 | header( |
483 | 483 | sprintf( |
484 | - 'Set-Cookie: %snc_sameSiteCookie%s=true; path=%s; httponly;' . $secureCookie . 'expires=Fri, 31-Dec-2100 23:59:59 GMT; SameSite=%s', |
|
484 | + 'Set-Cookie: %snc_sameSiteCookie%s=true; path=%s; httponly;'.$secureCookie.'expires=Fri, 31-Dec-2100 23:59:59 GMT; SameSite=%s', |
|
485 | 485 | $cookiePrefix, |
486 | 486 | $policy, |
487 | 487 | $cookieParams['path'], |
@@ -555,12 +555,12 @@ discard block |
||
555 | 555 | |
556 | 556 | // register autoloader |
557 | 557 | $loaderStart = microtime(true); |
558 | - require_once __DIR__ . '/autoloader.php'; |
|
558 | + require_once __DIR__.'/autoloader.php'; |
|
559 | 559 | self::$loader = new \OC\Autoloader([ |
560 | - OC::$SERVERROOT . '/lib/private/legacy', |
|
560 | + OC::$SERVERROOT.'/lib/private/legacy', |
|
561 | 561 | ]); |
562 | 562 | if (defined('PHPUNIT_RUN')) { |
563 | - self::$loader->addValidRoot(OC::$SERVERROOT . '/tests'); |
|
563 | + self::$loader->addValidRoot(OC::$SERVERROOT.'/tests'); |
|
564 | 564 | } |
565 | 565 | spl_autoload_register([self::$loader, 'load']); |
566 | 566 | $loaderEnd = microtime(true); |
@@ -568,12 +568,12 @@ discard block |
||
568 | 568 | self::$CLI = (php_sapi_name() == 'cli'); |
569 | 569 | |
570 | 570 | // Add default composer PSR-4 autoloader |
571 | - self::$composerAutoloader = require_once OC::$SERVERROOT . '/lib/composer/autoload.php'; |
|
571 | + self::$composerAutoloader = require_once OC::$SERVERROOT.'/lib/composer/autoload.php'; |
|
572 | 572 | |
573 | 573 | try { |
574 | 574 | self::initPaths(); |
575 | 575 | // setup 3rdparty autoloader |
576 | - $vendorAutoLoad = OC::$SERVERROOT. '/3rdparty/autoload.php'; |
|
576 | + $vendorAutoLoad = OC::$SERVERROOT.'/3rdparty/autoload.php'; |
|
577 | 577 | if (!file_exists($vendorAutoLoad)) { |
578 | 578 | throw new \RuntimeException('Composer autoloader not found, unable to continue. Check the folder "3rdparty". Running "git submodule update --init" will initialize the git submodule that handles the subfolder "3rdparty".'); |
579 | 579 | } |
@@ -669,11 +669,11 @@ discard block |
||
669 | 669 | // Convert l10n string into regular string for usage in database |
670 | 670 | $staticErrors = []; |
671 | 671 | foreach ($errors as $error) { |
672 | - echo $error['error'] . "\n"; |
|
673 | - echo $error['hint'] . "\n\n"; |
|
672 | + echo $error['error']."\n"; |
|
673 | + echo $error['hint']."\n\n"; |
|
674 | 674 | $staticErrors[] = [ |
675 | - 'error' => (string)$error['error'], |
|
676 | - 'hint' => (string)$error['hint'], |
|
675 | + 'error' => (string) $error['error'], |
|
676 | + 'hint' => (string) $error['hint'], |
|
677 | 677 | ]; |
678 | 678 | } |
679 | 679 | |
@@ -689,7 +689,7 @@ discard block |
||
689 | 689 | } |
690 | 690 | //try to set the session lifetime |
691 | 691 | $sessionLifeTime = self::getSessionLifeTime(); |
692 | - @ini_set('gc_maxlifetime', (string)$sessionLifeTime); |
|
692 | + @ini_set('gc_maxlifetime', (string) $sessionLifeTime); |
|
693 | 693 | |
694 | 694 | // User and Groups |
695 | 695 | if (!$systemConfig->getValue("installed", false)) { |
@@ -799,7 +799,7 @@ discard block |
||
799 | 799 | |
800 | 800 | // NOTE: This will be replaced to use OCP |
801 | 801 | $userSession = self::$server->getUserSession(); |
802 | - $userSession->listen('\OC\User', 'postLogin', function () use ($userSession) { |
|
802 | + $userSession->listen('\OC\User', 'postLogin', function() use ($userSession) { |
|
803 | 803 | if (!defined('PHPUNIT_RUN') && $userSession->isLoggedIn()) { |
804 | 804 | // reset brute force delay for this IP address and username |
805 | 805 | $uid = \OC::$server->getUserSession()->getUser()->getUID(); |
@@ -850,7 +850,7 @@ discard block |
||
850 | 850 | private static function registerAppRestrictionsHooks() { |
851 | 851 | /** @var \OC\Group\Manager $groupManager */ |
852 | 852 | $groupManager = self::$server->query(\OCP\IGroupManager::class); |
853 | - $groupManager->listen('\OC\Group', 'postDelete', function (\OCP\IGroup $group) { |
|
853 | + $groupManager->listen('\OC\Group', 'postDelete', function(\OCP\IGroup $group) { |
|
854 | 854 | $appManager = self::$server->getAppManager(); |
855 | 855 | $apps = $appManager->getEnabledAppsForGroup($group); |
856 | 856 | foreach ($apps as $appId) { |
@@ -960,11 +960,11 @@ discard block |
||
960 | 960 | // emergency app disabling |
961 | 961 | if ($requestPath === '/disableapp' |
962 | 962 | && $request->getMethod() === 'POST' |
963 | - && ((array)$request->getParam('appid')) !== '' |
|
963 | + && ((array) $request->getParam('appid')) !== '' |
|
964 | 964 | ) { |
965 | 965 | \OC_JSON::callCheck(); |
966 | 966 | \OC_JSON::checkAdminUser(); |
967 | - $appIds = (array)$request->getParam('appid'); |
|
967 | + $appIds = (array) $request->getParam('appid'); |
|
968 | 968 | foreach ($appIds as $appId) { |
969 | 969 | $appId = \OC_App::cleanAppId($appId); |
970 | 970 | \OC::$server->getAppManager()->disableApp($appId); |