Completed
Push — master ( 1f48f6...828106 )
by Lukas
10:12
created

OC::checkMaintenanceMode()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 15
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 9
nc 2
nop 0
dl 0
loc 15
rs 9.4285
c 1
b 0
f 0
1
<?php
2
/**
3
 * @author Adam Williamson <[email protected]>
4
 * @author Andreas Fischer <[email protected]>
5
 * @author Arthur Schiwon <[email protected]>
6
 * @author Bart Visscher <[email protected]>
7
 * @author Bernhard Posselt <[email protected]>
8
 * @author Björn Schießle <[email protected]>
9
 * @author Christoph Wurst <[email protected]>
10
 * @author davidgumberg <[email protected]>
11
 * @author Florin Peter <[email protected]>
12
 * @author Georg Ehrke <[email protected]>
13
 * @author Hugo Gonzalez Labrador <[email protected]>
14
 * @author Individual IT Services <[email protected]>
15
 * @author Jakob Sack <[email protected]>
16
 * @author Joachim Bauch <[email protected]>
17
 * @author Joas Schilling <[email protected]>
18
 * @author Jörn Friedrich Dreyer <[email protected]>
19
 * @author Lukas Reschke <[email protected]>
20
 * @author Michael Gapczynski <[email protected]>
21
 * @author Morris Jobke <[email protected]>
22
 * @author Owen Winkler <[email protected]>
23
 * @author Phil Davis <[email protected]>
24
 * @author Ramiro Aparicio <[email protected]>
25
 * @author Robin Appelman <[email protected]>
26
 * @author Robin McCorkell <[email protected]>
27
 * @author Roeland Jago Douma <[email protected]>
28
 * @author scolebrook <[email protected]>
29
 * @author Stefan Weil <[email protected]>
30
 * @author Thomas Müller <[email protected]>
31
 * @author Thomas Tanghus <[email protected]>
32
 * @author Vincent Petry <[email protected]>
33
 * @author Volkan Gezer <[email protected]>
34
 *
35
 * @copyright Copyright (c) 2016, ownCloud, Inc.
36
 * @license AGPL-3.0
37
 *
38
 * This code is free software: you can redistribute it and/or modify
39
 * it under the terms of the GNU Affero General Public License, version 3,
40
 * as published by the Free Software Foundation.
41
 *
42
 * This program is distributed in the hope that it will be useful,
43
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
44
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
45
 * GNU Affero General Public License for more details.
46
 *
47
 * You should have received a copy of the GNU Affero General Public License, version 3,
48
 * along with this program.  If not, see <http://www.gnu.org/licenses/>
49
 *
50
 */
51
52
require_once 'public/Constants.php';
53
54
/**
55
 * Class that is a namespace for all global OC variables
56
 * No, we can not put this class in its own file because it is used by
57
 * OC_autoload!
58
 */
59
class OC {
60
	/**
61
	 * Associative array for autoloading. classname => filename
62
	 */
63
	public static $CLASSPATH = array();
64
	/**
65
	 * The installation path for Nextcloud  on the server (e.g. /srv/http/nextcloud)
66
	 */
67
	public static $SERVERROOT = '';
68
	/**
69
	 * the current request path relative to the Nextcloud root (e.g. files/index.php)
70
	 */
71
	private static $SUBURI = '';
72
	/**
73
	 * the Nextcloud root path for http requests (e.g. nextcloud/)
74
	 */
75
	public static $WEBROOT = '';
76
	/**
77
	 * The installation path array of the apps folder on the server (e.g. /srv/http/nextcloud) 'path' and
78
	 * web path in 'url'
79
	 */
80
	public static $APPSROOTS = array();
81
82
	/**
83
	 * @var string
84
	 */
85
	public static $configDir;
86
87
	/**
88
	 * requested app
89
	 */
90
	public static $REQUESTEDAPP = '';
91
92
	/**
93
	 * check if Nextcloud runs in cli mode
94
	 */
95
	public static $CLI = false;
96
97
	/**
98
	 * @var \OC\Autoloader $loader
99
	 */
100
	public static $loader = null;
101
102
	/** @var \Composer\Autoload\ClassLoader $composerAutoloader */
103
	public static $composerAutoloader = null;
104
105
	/**
106
	 * @var \OC\Server
107
	 */
108
	public static $server = null;
109
110
	/**
111
	 * @var \OC\Config
112
	 */
113
	private static $config = null;
114
115
	/**
116
	 * @throws \RuntimeException when the 3rdparty directory is missing or
117
	 * the app path list is empty or contains an invalid path
118
	 */
119
	public static function initPaths() {
120
		if(defined('PHPUNIT_CONFIG_DIR')) {
121
			self::$configDir = OC::$SERVERROOT . '/' . PHPUNIT_CONFIG_DIR . '/';
122
		} elseif(defined('PHPUNIT_RUN') and PHPUNIT_RUN and is_dir(OC::$SERVERROOT . '/tests/config/')) {
123
			self::$configDir = OC::$SERVERROOT . '/tests/config/';
124
		} else {
125
			self::$configDir = OC::$SERVERROOT . '/config/';
126
		}
127
		self::$config = new \OC\Config(self::$configDir);
128
129
		OC::$SUBURI = str_replace("\\", "/", substr(realpath($_SERVER["SCRIPT_FILENAME"]), strlen(OC::$SERVERROOT)));
130
		/**
131
		 * FIXME: The following lines are required because we can't yet instantiiate
132
		 *        \OC::$server->getRequest() since \OC::$server does not yet exist.
133
		 */
134
		$params = [
135
			'server' => [
136
				'SCRIPT_NAME' => $_SERVER['SCRIPT_NAME'],
137
				'SCRIPT_FILENAME' => $_SERVER['SCRIPT_FILENAME'],
138
			],
139
		];
140
		$fakeRequest = new \OC\AppFramework\Http\Request($params, null, new \OC\AllConfig(new \OC\SystemConfig(self::$config)));
141
		$scriptName = $fakeRequest->getScriptName();
142
		if (substr($scriptName, -1) == '/') {
143
			$scriptName .= 'index.php';
144
			//make sure suburi follows the same rules as scriptName
145
			if (substr(OC::$SUBURI, -9) != 'index.php') {
146
				if (substr(OC::$SUBURI, -1) != '/') {
147
					OC::$SUBURI = OC::$SUBURI . '/';
148
				}
149
				OC::$SUBURI = OC::$SUBURI . 'index.php';
150
			}
151
		}
152
153
154
		if (OC::$CLI) {
155
			OC::$WEBROOT = self::$config->getValue('overwritewebroot', '');
156
		} else {
157
			if (substr($scriptName, 0 - strlen(OC::$SUBURI)) === OC::$SUBURI) {
158
				OC::$WEBROOT = substr($scriptName, 0, 0 - strlen(OC::$SUBURI));
159
160
				if (OC::$WEBROOT != '' && OC::$WEBROOT[0] !== '/') {
161
					OC::$WEBROOT = '/' . OC::$WEBROOT;
162
				}
163
			} else {
164
				// The scriptName is not ending with OC::$SUBURI
165
				// This most likely means that we are calling from CLI.
166
				// However some cron jobs still need to generate
167
				// a web URL, so we use overwritewebroot as a fallback.
168
				OC::$WEBROOT = self::$config->getValue('overwritewebroot', '');
169
			}
170
171
			// Resolve /nextcloud to /nextcloud/ to ensure to always have a trailing
172
			// slash which is required by URL generation.
173
			if($_SERVER['REQUEST_URI'] === \OC::$WEBROOT &&
174
					substr($_SERVER['REQUEST_URI'], -1) !== '/') {
175
				header('Location: '.\OC::$WEBROOT.'/');
176
				exit();
177
			}
178
		}
179
180
		// search the apps folder
181
		$config_paths = self::$config->getValue('apps_paths', array());
182
		if (!empty($config_paths)) {
183
			foreach ($config_paths as $paths) {
184
				if (isset($paths['url']) && isset($paths['path'])) {
185
					$paths['url'] = rtrim($paths['url'], '/');
186
					$paths['path'] = rtrim($paths['path'], '/');
187
					OC::$APPSROOTS[] = $paths;
188
				}
189
			}
190
		} elseif (file_exists(OC::$SERVERROOT . '/apps')) {
191
			OC::$APPSROOTS[] = array('path' => OC::$SERVERROOT . '/apps', 'url' => '/apps', 'writable' => true);
192
		} elseif (file_exists(OC::$SERVERROOT . '/../apps')) {
193
			OC::$APPSROOTS[] = array(
194
				'path' => rtrim(dirname(OC::$SERVERROOT), '/') . '/apps',
195
				'url' => '/apps',
196
				'writable' => true
197
			);
198
		}
199
200
		if (empty(OC::$APPSROOTS)) {
201
			throw new \RuntimeException('apps directory not found! Please put the Nextcloud apps folder in the Nextcloud folder'
202
				. ' or the folder above. You can also configure the location in the config.php file.');
203
		}
204
		$paths = array();
205
		foreach (OC::$APPSROOTS as $path) {
206
			$paths[] = $path['path'];
207
			if (!is_dir($path['path'])) {
208
				throw new \RuntimeException(sprintf('App directory "%s" not found! Please put the Nextcloud apps folder in the'
209
					. ' Nextcloud folder or the folder above. You can also configure the location in the'
210
					. ' config.php file.', $path['path']));
211
			}
212
		}
213
214
		// set the right include path
215
		set_include_path(
216
			OC::$SERVERROOT . '/lib/private' . PATH_SEPARATOR .
217
			OC::$SERVERROOT . '/config' . PATH_SEPARATOR .
218
			OC::$SERVERROOT . '/3rdparty' . PATH_SEPARATOR .
219
			implode(PATH_SEPARATOR, $paths) . PATH_SEPARATOR .
220
			get_include_path() . PATH_SEPARATOR .
221
			OC::$SERVERROOT
222
		);
223
	}
224
225
	public static function checkConfig() {
226
		$l = \OC::$server->getL10N('lib');
227
228
		// Create config if it does not already exist
229
		$configFilePath = self::$configDir .'/config.php';
230
		if(!file_exists($configFilePath)) {
231
			@touch($configFilePath);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
232
		}
233
234
		// Check if config is writable
235
		$configFileWritable = is_writable($configFilePath);
236
		if (!$configFileWritable && !OC_Helper::isReadOnlyConfigEnabled()
237
			|| !$configFileWritable && self::checkUpgrade(false)) {
238
239
			$urlGenerator = \OC::$server->getURLGenerator();
240
241
			if (self::$CLI) {
242
				echo $l->t('Cannot write into "config" directory!')."\n";
243
				echo $l->t('This can usually be fixed by giving the webserver write access to the config directory')."\n";
244
				echo "\n";
245
				echo $l->t('See %s', [ $urlGenerator->linkToDocs('admin-dir_permissions') ])."\n";
246
				exit;
247
			} else {
248
				OC_Template::printErrorPage(
249
					$l->t('Cannot write into "config" directory!'),
250
					$l->t('This can usually be fixed by '
251
					. '%sgiving the webserver write access to the config directory%s.',
252
					 array('<a href="' . $urlGenerator->linkToDocs('admin-dir_permissions') . '" target="_blank" rel="noreferrer">', '</a>'))
253
				);
254
			}
255
		}
256
	}
257
258
	public static function checkInstalled() {
259
		if (defined('OC_CONSOLE')) {
260
			return;
261
		}
262
		// Redirect to installer if not installed
263
		if (!\OC::$server->getSystemConfig()->getValue('installed', false) && OC::$SUBURI != '/index.php') {
264
			if (OC::$CLI) {
265
				throw new Exception('Not installed');
266
			} else {
267
				$url = 'http://' . $_SERVER['SERVER_NAME'] . OC::$WEBROOT . '/index.php';
268
				header('Location: ' . $url);
269
			}
270
			exit();
271
		}
272
	}
273
274
	public static function checkMaintenanceMode() {
275
		// Allow ajax update script to execute without being stopped
276
		if (\OC::$server->getSystemConfig()->getValue('maintenance', false) && OC::$SUBURI != '/core/ajax/update.php') {
277
			// send http status 503
278
			header('HTTP/1.1 503 Service Temporarily Unavailable');
279
			header('Status: 503 Service Temporarily Unavailable');
280
			header('Retry-After: 120');
281
282
			// render error page
283
			$template = new OC_Template('', 'update.user', 'guest');
284
			OC_Util::addScript('maintenance-check');
285
			$template->printPage();
286
			die();
287
		}
288
	}
289
290
	public static function checkSingleUserMode($lockIfNoUserLoggedIn = false) {
291
		if (!\OC::$server->getSystemConfig()->getValue('singleuser', false)) {
292
			return;
293
		}
294
		$user = OC_User::getUserSession()->getUser();
295
		if ($user) {
296
			$group = \OC::$server->getGroupManager()->get('admin');
297
			if ($group->inGroup($user)) {
0 ignored issues
show
Compatibility introduced by
$user of type object<OCP\IUser> is not a sub-type of object<OC\User\User>. It seems like you assume a concrete implementation of the interface OCP\IUser to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
298
				return;
299
			}
300
		} else {
301
			if(!$lockIfNoUserLoggedIn) {
302
				return;
303
			}
304
		}
305
		// send http status 503
306
		header('HTTP/1.1 503 Service Temporarily Unavailable');
307
		header('Status: 503 Service Temporarily Unavailable');
308
		header('Retry-After: 120');
309
310
		// render error page
311
		$template = new OC_Template('', 'singleuser.user', 'guest');
312
		$template->printPage();
313
		die();
314
	}
315
316
	/**
317
	 * Checks if the version requires an update and shows
318
	 * @param bool $showTemplate Whether an update screen should get shown
319
	 * @return bool|void
320
	 */
321
	public static function checkUpgrade($showTemplate = true) {
322
		if (\OCP\Util::needUpgrade()) {
323
			$systemConfig = \OC::$server->getSystemConfig();
324
			if ($showTemplate && !$systemConfig->getValue('maintenance', false)) {
325
				self::printUpgradePage();
326
				exit();
327
			} else {
328
				return true;
329
			}
330
		}
331
		return false;
332
	}
333
334
	/**
335
	 * Prints the upgrade page
336
	 */
337
	private static function printUpgradePage() {
338
		$systemConfig = \OC::$server->getSystemConfig();
339
340
		$disableWebUpdater = $systemConfig->getValue('upgrade.disable-web', false);
341
		$tooBig = false;
342
		if (!$disableWebUpdater) {
343
			$apps = \OC::$server->getAppManager();
344
			$tooBig = $apps->isInstalled('user_ldap') || $apps->isInstalled('user_shibboleth');
345
			if (!$tooBig) {
346
				// count users
347
				$stats = \OC::$server->getUserManager()->countUsers();
348
				$totalUsers = array_sum($stats);
349
				$tooBig = ($totalUsers > 50);
350
			}
351
		}
352
		if ($disableWebUpdater || $tooBig) {
353
			// send http status 503
354
			header('HTTP/1.1 503 Service Temporarily Unavailable');
355
			header('Status: 503 Service Temporarily Unavailable');
356
			header('Retry-After: 120');
357
358
			// render error page
359
			$template = new OC_Template('', 'update.use-cli', 'guest');
360
			$template->assign('productName', 'owncloud'); // for now
361
			$template->assign('version', OC_Util::getVersionString());
362
			$template->assign('tooBig', $tooBig);
363
364
			$template->printPage();
365
			die();
366
		}
367
368
		// check whether this is a core update or apps update
369
		$installedVersion = $systemConfig->getValue('version', '0.0.0');
370
		$currentVersion = implode('.', \OCP\Util::getVersion());
371
372
		// if not a core upgrade, then it's apps upgrade
373
		$isAppsOnlyUpgrade = (version_compare($currentVersion, $installedVersion, '='));
374
375
		$oldTheme = $systemConfig->getValue('theme');
376
		$systemConfig->setValue('theme', '');
377
		\OCP\Util::addScript('config'); // needed for web root
378
		\OCP\Util::addScript('update');
379
		\OCP\Util::addStyle('update');
380
381
		$appManager = \OC::$server->getAppManager();
382
383
		$tmpl = new OC_Template('', 'update.admin', 'guest');
384
		$tmpl->assign('version', OC_Util::getVersionString());
385
		$tmpl->assign('isAppsOnlyUpgrade', $isAppsOnlyUpgrade);
386
387
		// get third party apps
388
		$ocVersion = \OCP\Util::getVersion();
389
		$tmpl->assign('appsToUpgrade', $appManager->getAppsNeedingUpgrade($ocVersion));
390
		$tmpl->assign('incompatibleAppsList', $appManager->getIncompatibleApps($ocVersion));
391
		$tmpl->assign('productName', 'ownCloud'); // for now
392
		$tmpl->assign('oldTheme', $oldTheme);
393
		$tmpl->printPage();
394
	}
395
396
	public static function initSession() {
397
		// prevents javascript from accessing php session cookies
398
		ini_set('session.cookie_httponly', true);
399
400
		// set the cookie path to the Nextcloud directory
401
		$cookie_path = OC::$WEBROOT ? : '/';
402
		ini_set('session.cookie_path', $cookie_path);
403
404
		// Let the session name be changed in the initSession Hook
405
		$sessionName = OC_Util::getInstanceId();
406
407
		try {
408
			// Allow session apps to create a custom session object
409
			$useCustomSession = false;
410
			$session = self::$server->getSession();
411
			OC_Hook::emit('OC', 'initSession', array('session' => &$session, 'sessionName' => &$sessionName, 'useCustomSession' => &$useCustomSession));
412
			if (!$useCustomSession) {
413
				// set the session name to the instance id - which is unique
414
				$session = new \OC\Session\Internal($sessionName);
415
			}
416
417
			$cryptoWrapper = \OC::$server->getSessionCryptoWrapper();
418
			$session = $cryptoWrapper->wrapSession($session);
419
			self::$server->setSession($session);
420
421
			// if session can't be started break with http 500 error
422
		} catch (Exception $e) {
423
			\OCP\Util::logException('base', $e);
0 ignored issues
show
Deprecated Code introduced by
The method OCP\Util::logException() has been deprecated with message: 8.2.0 use logException of \OCP\ILogger

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
424
			//show the user a detailed error page
425
			OC_Response::setStatus(OC_Response::STATUS_INTERNAL_SERVER_ERROR);
426
			OC_Template::printExceptionErrorPage($e);
427
			die();
428
		}
429
430
		$sessionLifeTime = self::getSessionLifeTime();
431
432
		// session timeout
433
		if ($session->exists('LAST_ACTIVITY') && (time() - $session->get('LAST_ACTIVITY') > $sessionLifeTime)) {
434
			if (isset($_COOKIE[session_name()])) {
435
				setcookie(session_name(), null, -1, self::$WEBROOT ? : '/');
436
			}
437
			\OC::$server->getUserSession()->logout();
438
		}
439
440
		$session->set('LAST_ACTIVITY', time());
441
	}
442
443
	/**
444
	 * @return string
445
	 */
446
	private static function getSessionLifeTime() {
447
		return \OC::$server->getConfig()->getSystemValue('session_lifetime', 60 * 60 * 24);
448
	}
449
450
	public static function loadAppClassPaths() {
451 View Code Duplication
		foreach (OC_App::getEnabledApps() as $app) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
452
			$appPath = OC_App::getAppPath($app);
453
			if ($appPath === false) {
454
				continue;
455
			}
456
457
			$file = $appPath . '/appinfo/classpath.php';
458
			if (file_exists($file)) {
459
				require_once $file;
460
			}
461
		}
462
	}
463
464
	/**
465
	 * Try to set some values to the required Nextcloud default
466
	 */
467
	public static function setRequiredIniValues() {
468
		@ini_set('default_charset', 'UTF-8');
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
469
		@ini_set('gd.jpeg_ignore_warning', 1);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
470
	}
471
472
	public static function init() {
473
		// calculate the root directories
474
		OC::$SERVERROOT = str_replace("\\", '/', substr(__DIR__, 0, -4));
475
476
		// register autoloader
477
		$loaderStart = microtime(true);
478
		require_once __DIR__ . '/autoloader.php';
479
		self::$loader = new \OC\Autoloader([
480
			OC::$SERVERROOT . '/lib/private/legacy',
481
		]);
482
		if (defined('PHPUNIT_RUN')) {
483
			self::$loader->addValidRoot(OC::$SERVERROOT . '/tests');
484
		}
485
		spl_autoload_register(array(self::$loader, 'load'));
486
		$loaderEnd = microtime(true);
487
488
		self::$CLI = (php_sapi_name() == 'cli');
489
490
		// Add default composer PSR-4 autoloader
491
		self::$composerAutoloader = require_once OC::$SERVERROOT . '/lib/composer/autoload.php';
492
493
		try {
494
			self::initPaths();
495
			// setup 3rdparty autoloader
496
			$vendorAutoLoad = OC::$SERVERROOT. '/3rdparty/autoload.php';
497
			if (!file_exists($vendorAutoLoad)) {
498
				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".');
499
			}
500
			require_once $vendorAutoLoad;
501
502
		} catch (\RuntimeException $e) {
503
			if (!self::$CLI) {
504
				$claimedProtocol = strtoupper($_SERVER['SERVER_PROTOCOL']);
505
				$protocol = in_array($claimedProtocol, ['HTTP/1.0', 'HTTP/1.1', 'HTTP/2']) ? $claimedProtocol : 'HTTP/1.1';
506
				header($protocol . ' ' . OC_Response::STATUS_SERVICE_UNAVAILABLE);
507
			}
508
			// we can't use the template error page here, because this needs the
509
			// DI container which isn't available yet
510
			print($e->getMessage());
511
			exit();
512
		}
513
514
		// setup the basic server
515
		self::$server = new \OC\Server(\OC::$WEBROOT, self::$config);
516
		\OC::$server->getEventLogger()->log('autoloader', 'Autoloader', $loaderStart, $loaderEnd);
517
		\OC::$server->getEventLogger()->start('boot', 'Initialize');
518
519
		// Don't display errors and log them
520
		error_reporting(E_ALL | E_STRICT);
521
		@ini_set('display_errors', 0);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
522
		@ini_set('log_errors', 1);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
523
524
		date_default_timezone_set('UTC');
525
526
		//try to configure php to enable big file uploads.
527
		//this doesn´t work always depending on the webserver and php configuration.
528
		//Let´s try to overwrite some defaults anyway
529
530
		//try to set the maximum execution time to 60min
531
		@set_time_limit(3600);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
532
		@ini_set('max_execution_time', 3600);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
533
		@ini_set('max_input_time', 3600);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
534
535
		//try to set the maximum filesize to 10G
536
		@ini_set('upload_max_filesize', '10G');
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
537
		@ini_set('post_max_size', '10G');
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
538
		@ini_set('file_uploads', '50');
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
539
540
		self::setRequiredIniValues();
541
		self::handleAuthHeaders();
542
		self::registerAutoloaderCache();
543
544
		// initialize intl fallback is necessary
545
		\Patchwork\Utf8\Bootup::initIntl();
546
		OC_Util::isSetLocaleWorking();
547
548
		if (!defined('PHPUNIT_RUN')) {
549
			OC\Log\ErrorHandler::setLogger(\OC::$server->getLogger());
550
			$debug = \OC::$server->getConfig()->getSystemValue('debug', false);
551
			OC\Log\ErrorHandler::register($debug);
552
		}
553
554
		// register the stream wrappers
555
		stream_wrapper_register('fakedir', 'OC\Files\Stream\Dir');
556
		stream_wrapper_register('static', 'OC\Files\Stream\StaticStream');
557
		stream_wrapper_register('close', 'OC\Files\Stream\Close');
558
		stream_wrapper_register('quota', 'OC\Files\Stream\Quota');
559
		stream_wrapper_register('oc', 'OC\Files\Stream\OC');
560
561
		\OC::$server->getEventLogger()->start('init_session', 'Initialize session');
562
		OC_App::loadApps(array('session'));
563
		if (!self::$CLI) {
564
			self::initSession();
565
		}
566
		\OC::$server->getEventLogger()->end('init_session');
567
		self::checkConfig();
568
		self::checkInstalled();
569
570
		OC_Response::addSecurityHeaders();
571
		if(self::$server->getRequest()->getServerProtocol() === 'https') {
572
			ini_set('session.cookie_secure', true);
573
		}
574
575
		if (!defined('OC_CONSOLE')) {
576
			$errors = OC_Util::checkServer(\OC::$server->getConfig());
577
			if (count($errors) > 0) {
578
				if (self::$CLI) {
579
					// Convert l10n string into regular string for usage in database
580
					$staticErrors = [];
581
					foreach ($errors as $error) {
582
						echo $error['error'] . "\n";
583
						echo $error['hint'] . "\n\n";
584
						$staticErrors[] = [
585
							'error' => (string)$error['error'],
586
							'hint' => (string)$error['hint'],
587
						];
588
					}
589
590
					try {
591
						\OC::$server->getConfig()->setAppValue('core', 'cronErrors', json_encode($staticErrors));
592
					} catch (\Exception $e) {
593
						echo('Writing to database failed');
594
					}
595
					exit(1);
596
				} else {
597
					OC_Response::setStatus(OC_Response::STATUS_SERVICE_UNAVAILABLE);
598
					OC_Template::printGuestPage('', 'error', array('errors' => $errors));
599
					exit;
600
				}
601 View Code Duplication
			} elseif (self::$CLI && \OC::$server->getConfig()->getSystemValue('installed', false)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
602
				\OC::$server->getConfig()->deleteAppValue('core', 'cronErrors');
603
			}
604
		}
605
		//try to set the session lifetime
606
		$sessionLifeTime = self::getSessionLifeTime();
607
		@ini_set('gc_maxlifetime', (string)$sessionLifeTime);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
608
609
		$systemConfig = \OC::$server->getSystemConfig();
610
611
		// User and Groups
612
		if (!$systemConfig->getValue("installed", false)) {
613
			self::$server->getSession()->set('user_id', '');
614
		}
615
616
		OC_User::useBackend(new \OC\User\Database());
617
		OC_Group::useBackend(new \OC\Group\Database());
618
619
		// Subscribe to the hook
620
		\OCP\Util::connectHook(
621
			'\OCA\Files_Sharing\API\Server2Server',
622
			'preLoginNameUsedAsUserName',
623
			'\OC\User\Database',
624
			'preLoginNameUsedAsUserName'
625
		);
626
627
		//setup extra user backends
628
		if (!self::checkUpgrade(false)) {
0 ignored issues
show
Bug Best Practice introduced by
The expression self::checkUpgrade(false) of type null|boolean is loosely compared to false; this is ambiguous if the boolean can be false. You might want to explicitly use !== null instead.

If an expression can have both false, and null as possible values. It is generally a good practice to always use strict comparison to clearly distinguish between those two values.

$a = canBeFalseAndNull();

// Instead of
if ( ! $a) { }

// Better use one of the explicit versions:
if ($a !== null) { }
if ($a !== false) { }
if ($a !== null && $a !== false) { }
Loading history...
629
			OC_User::setupBackends();
630
		} else {
631
			// Run upgrades in incognito mode
632
			OC_User::setIncognitoMode(true);
633
		}
634
635
		self::registerCacheHooks();
636
		self::registerFilesystemHooks();
637
		if ($systemConfig->getValue('enable_previews', true)) {
638
			self::registerPreviewHooks();
639
		}
640
		self::registerShareHooks();
641
		self::registerLogRotate();
642
		self::registerEncryptionWrapper();
643
		self::registerEncryptionHooks();
644
645
		//make sure temporary files are cleaned up
646
		$tmpManager = \OC::$server->getTempManager();
647
		register_shutdown_function(array($tmpManager, 'clean'));
648
		$lockProvider = \OC::$server->getLockingProvider();
649
		register_shutdown_function(array($lockProvider, 'releaseAll'));
650
651
		// Check whether the sample configuration has been copied
652
		if($systemConfig->getValue('copied_sample_config', false)) {
653
			$l = \OC::$server->getL10N('lib');
654
			header('HTTP/1.1 503 Service Temporarily Unavailable');
655
			header('Status: 503 Service Temporarily Unavailable');
656
			OC_Template::printErrorPage(
657
				$l->t('Sample configuration detected'),
658
				$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')
659
			);
660
			return;
661
		}
662
663
		$request = \OC::$server->getRequest();
664
		$host = $request->getInsecureServerHost();
665
		/**
666
		 * if the host passed in headers isn't trusted
667
		 * FIXME: Should not be in here at all :see_no_evil:
668
		 */
669
		if (!OC::$CLI
670
			// overwritehost is always trusted, workaround to not have to make
671
			// \OC\AppFramework\Http\Request::getOverwriteHost public
672
			&& self::$server->getConfig()->getSystemValue('overwritehost') === ''
673
			&& !\OC::$server->getTrustedDomainHelper()->isTrustedDomain($host)
674
			&& self::$server->getConfig()->getSystemValue('installed', false)
675
		) {
676
			header('HTTP/1.1 400 Bad Request');
677
			header('Status: 400 Bad Request');
678
679
			\OC::$server->getLogger()->warning(
680
					'Trusted domain error. "{remoteAddress}" tried to access using "{host}" as host.',
681
					[
682
						'app' => 'core',
683
						'remoteAddress' => $request->getRemoteAddress(),
684
						'host' => $host,
685
					]
686
			);
687
688
			$tmpl = new OCP\Template('core', 'untrustedDomain', 'guest');
689
			$tmpl->assign('domain', $host);
690
			$tmpl->printPage();
691
692
			exit();
693
		}
694
		\OC::$server->getEventLogger()->end('boot');
695
	}
696
697
	/**
698
	 * register hooks for the cache
699
	 */
700
	public static function registerCacheHooks() {
701
		//don't try to do this before we are properly setup
702
		if (\OC::$server->getSystemConfig()->getValue('installed', false) && !self::checkUpgrade(false)) {
0 ignored issues
show
Bug Best Practice introduced by
The expression self::checkUpgrade(false) of type null|boolean is loosely compared to false; this is ambiguous if the boolean can be false. You might want to explicitly use !== null instead.

If an expression can have both false, and null as possible values. It is generally a good practice to always use strict comparison to clearly distinguish between those two values.

$a = canBeFalseAndNull();

// Instead of
if ( ! $a) { }

// Better use one of the explicit versions:
if ($a !== null) { }
if ($a !== false) { }
if ($a !== null && $a !== false) { }
Loading history...
703
704
			// NOTE: This will be replaced to use OCP
705
			$userSession = self::$server->getUserSession();
706
			$userSession->listen('\OC\User', 'postLogin', function () {
707
				try {
708
					$cache = new \OC\Cache\File();
709
					$cache->gc();
710
				} catch (\OC\ServerNotAvailableException $e) {
711
					// not a GC exception, pass it on
712
					throw $e;
713
				} catch (\Exception $e) {
714
					// a GC exception should not prevent users from using OC,
715
					// so log the exception
716
					\OC::$server->getLogger()->warning('Exception when running cache gc: ' . $e->getMessage(), array('app' => 'core'));
717
				}
718
			});
719
		}
720
	}
721
722
	private static function registerEncryptionWrapper() {
723
		$manager = self::$server->getEncryptionManager();
724
		\OCP\Util::connectHook('OC_Filesystem', 'preSetup', $manager, 'setupStorage');
725
	}
726
727
	private static function registerEncryptionHooks() {
728
		$enabled = self::$server->getEncryptionManager()->isEnabled();
729
		if ($enabled) {
730
			\OCP\Util::connectHook('OCP\Share', 'post_shared', 'OC\Encryption\HookManager', 'postShared');
731
			\OCP\Util::connectHook('OCP\Share', 'post_unshare', 'OC\Encryption\HookManager', 'postUnshared');
732
			\OCP\Util::connectHook('OC_Filesystem', 'post_rename', 'OC\Encryption\HookManager', 'postRename');
733
			\OCP\Util::connectHook('\OCA\Files_Trashbin\Trashbin', 'post_restore', 'OC\Encryption\HookManager', 'postRestore');
734
		}
735
	}
736
737
	/**
738
	 * register hooks for the cache
739
	 */
740
	public static function registerLogRotate() {
741
		$systemConfig = \OC::$server->getSystemConfig();
742
		if ($systemConfig->getValue('installed', false) && $systemConfig->getValue('log_rotate_size', false) && !self::checkUpgrade(false)) {
0 ignored issues
show
Bug Best Practice introduced by
The expression self::checkUpgrade(false) of type null|boolean is loosely compared to false; this is ambiguous if the boolean can be false. You might want to explicitly use !== null instead.

If an expression can have both false, and null as possible values. It is generally a good practice to always use strict comparison to clearly distinguish between those two values.

$a = canBeFalseAndNull();

// Instead of
if ( ! $a) { }

// Better use one of the explicit versions:
if ($a !== null) { }
if ($a !== false) { }
if ($a !== null && $a !== false) { }
Loading history...
743
			//don't try to do this before we are properly setup
744
			//use custom logfile path if defined, otherwise use default of nextcloud.log in data directory
745
			\OCP\BackgroundJob::registerJob('OC\Log\Rotate', $systemConfig->getValue('logfile', $systemConfig->getValue('datadirectory', OC::$SERVERROOT . '/data') . '/nextcloud.log'));
0 ignored issues
show
Deprecated Code introduced by
The method OCP\BackgroundJob::registerJob() has been deprecated with message: 8.1.0 Use \OC::$server->getJobList()->add() instead

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
746
		}
747
	}
748
749
	/**
750
	 * register hooks for the filesystem
751
	 */
752
	public static function registerFilesystemHooks() {
753
		// Check for blacklisted files
754
		OC_Hook::connect('OC_Filesystem', 'write', 'OC\Files\Filesystem', 'isBlacklisted');
755
		OC_Hook::connect('OC_Filesystem', 'rename', 'OC\Files\Filesystem', 'isBlacklisted');
756
	}
757
758
	/**
759
	 * register hooks for previews
760
	 */
761
	public static function registerPreviewHooks() {
762
		OC_Hook::connect('OC_Filesystem', 'post_write', 'OC\Preview', 'post_write');
763
		OC_Hook::connect('OC_Filesystem', 'delete', 'OC\Preview', 'prepare_delete_files');
764
		OC_Hook::connect('\OCP\Versions', 'preDelete', 'OC\Preview', 'prepare_delete');
765
		OC_Hook::connect('\OCP\Trashbin', 'preDelete', 'OC\Preview', 'prepare_delete');
766
		OC_Hook::connect('OC_Filesystem', 'post_delete', 'OC\Preview', 'post_delete_files');
767
		OC_Hook::connect('\OCP\Versions', 'delete', 'OC\Preview', 'post_delete_versions');
768
		OC_Hook::connect('\OCP\Trashbin', 'delete', 'OC\Preview', 'post_delete');
769
		OC_Hook::connect('\OCP\Versions', 'rollback', 'OC\Preview', 'post_delete_versions');
770
	}
771
772
	/**
773
	 * register hooks for sharing
774
	 */
775
	public static function registerShareHooks() {
776
		if (\OC::$server->getSystemConfig()->getValue('installed')) {
777
			OC_Hook::connect('OC_User', 'post_deleteUser', 'OC\Share20\Hooks', 'post_deleteUser');
778
			OC_Hook::connect('OC_User', 'post_removeFromGroup', 'OC\Share20\Hooks', 'post_removeFromGroup');
779
			OC_Hook::connect('OC_User', 'post_deleteGroup', 'OC\Share20\Hooks', 'post_deleteGroup');
780
		}
781
	}
782
783
	protected static function registerAutoloaderCache() {
784
		// The class loader takes an optional low-latency cache, which MUST be
785
		// namespaced. The instanceid is used for namespacing, but might be
786
		// unavailable at this point. Furthermore, it might not be possible to
787
		// generate an instanceid via \OC_Util::getInstanceId() because the
788
		// config file may not be writable. As such, we only register a class
789
		// loader cache if instanceid is available without trying to create one.
790
		$instanceId = \OC::$server->getSystemConfig()->getValue('instanceid', null);
791
		if ($instanceId) {
792
			try {
793
				$memcacheFactory = \OC::$server->getMemCacheFactory();
794
				self::$loader->setMemoryCache($memcacheFactory->createLocal('Autoloader'));
0 ignored issues
show
Bug introduced by
The method createLocal() does not exist on OCP\ICacheFactory. Did you maybe mean create()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
795
			} catch (\Exception $ex) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
796
			}
797
		}
798
	}
799
800
	/**
801
	 * Handle the request
802
	 */
803
	public static function handleRequest() {
804
805
		\OC::$server->getEventLogger()->start('handle_request', 'Handle request');
806
		$systemConfig = \OC::$server->getSystemConfig();
807
		// load all the classpaths from the enabled apps so they are available
808
		// in the routing files of each app
809
		OC::loadAppClassPaths();
810
811
		// Check if Nextcloud is installed or in maintenance (update) mode
812
		if (!$systemConfig->getValue('installed', false)) {
813
			\OC::$server->getSession()->clear();
814
			$setupHelper = new OC\Setup(\OC::$server->getConfig(), \OC::$server->getIniWrapper(),
815
				\OC::$server->getL10N('lib'), new \OC_Defaults(), \OC::$server->getLogger(),
816
				\OC::$server->getSecureRandom());
817
			$controller = new OC\Core\Controller\SetupController($setupHelper);
818
			$controller->run($_POST);
819
			exit();
820
		}
821
822
		$request = \OC::$server->getRequest();
823
		$requestPath = $request->getRawPathInfo();
824
		if (substr($requestPath, -3) !== '.js') { // we need these files during the upgrade
825
			self::checkMaintenanceMode();
826
			self::checkUpgrade();
827
		}
828
829
		// emergency app disabling
830
		if ($requestPath === '/disableapp'
831
			&& $request->getMethod() === 'POST'
832
			&& ((string)$request->getParam('appid')) !== ''
833
		) {
834
			\OCP\JSON::callCheck();
0 ignored issues
show
Deprecated Code introduced by
The method OCP\JSON::callCheck() has been deprecated with message: 8.1.0 Use annotation based CSRF checks from the AppFramework instead

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
835
			\OCP\JSON::checkAdminUser();
0 ignored issues
show
Deprecated Code introduced by
The method OCP\JSON::checkAdminUser() has been deprecated with message: 8.1.0 Use annotation based ACLs from the AppFramework instead

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
836
			$appId = (string)$request->getParam('appid');
837
			$appId = \OC_App::cleanAppId($appId);
838
839
			\OC_App::disable($appId);
840
			\OC_JSON::success();
0 ignored issues
show
Deprecated Code introduced by
The method OC_JSON::success() has been deprecated with message: Use a AppFramework JSONResponse instead

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
841
			exit();
842
		}
843
844
		// Always load authentication apps
845
		OC_App::loadApps(['authentication']);
846
847
		// Load minimum set of apps
848
		if (!self::checkUpgrade(false)
0 ignored issues
show
Bug Best Practice introduced by
The expression self::checkUpgrade(false) of type null|boolean is loosely compared to false; this is ambiguous if the boolean can be false. You might want to explicitly use !== null instead.

If an expression can have both false, and null as possible values. It is generally a good practice to always use strict comparison to clearly distinguish between those two values.

$a = canBeFalseAndNull();

// Instead of
if ( ! $a) { }

// Better use one of the explicit versions:
if ($a !== null) { }
if ($a !== false) { }
if ($a !== null && $a !== false) { }
Loading history...
849
			&& !$systemConfig->getValue('maintenance', false)) {
850
			// For logged-in users: Load everything
851
			if(OC_User::isLoggedIn()) {
0 ignored issues
show
Deprecated Code introduced by
The method OC_User::isLoggedIn() has been deprecated with message: use \OC::$server->getUserSession()->isLoggedIn()

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
852
				OC_App::loadApps();
853
			} else {
854
				// For guests: Load only filesystem and logging
855
				OC_App::loadApps(array('filesystem', 'logging'));
856
				self::handleLogin($request);
857
			}
858
		}
859
860
		if (!self::$CLI) {
861
			try {
862
				if (!$systemConfig->getValue('maintenance', false) && !self::checkUpgrade(false)) {
0 ignored issues
show
Bug Best Practice introduced by
The expression self::checkUpgrade(false) of type null|boolean is loosely compared to false; this is ambiguous if the boolean can be false. You might want to explicitly use !== null instead.

If an expression can have both false, and null as possible values. It is generally a good practice to always use strict comparison to clearly distinguish between those two values.

$a = canBeFalseAndNull();

// Instead of
if ( ! $a) { }

// Better use one of the explicit versions:
if ($a !== null) { }
if ($a !== false) { }
if ($a !== null && $a !== false) { }
Loading history...
863
					OC_App::loadApps(array('filesystem', 'logging'));
864
					OC_App::loadApps();
865
				}
866
				self::checkSingleUserMode();
867
				OC_Util::setupFS();
868
				OC::$server->getRouter()->match(\OC::$server->getRequest()->getRawPathInfo());
0 ignored issues
show
Deprecated Code introduced by
The method OCP\Route\IRouter::match() has been deprecated with message: 9.0.0

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
869
				return;
870
			} catch (Symfony\Component\Routing\Exception\ResourceNotFoundException $e) {
0 ignored issues
show
Bug introduced by
The class Symfony\Component\Routin...sourceNotFoundException does not exist. Did you forget a USE statement, or did you not list all dependencies?

Scrutinizer analyzes your composer.json/composer.lock file if available to determine the classes, and functions that are defined by your dependencies.

It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.

Loading history...
871
				//header('HTTP/1.0 404 Not Found');
872
			} catch (Symfony\Component\Routing\Exception\MethodNotAllowedException $e) {
0 ignored issues
show
Bug introduced by
The class Symfony\Component\Routin...thodNotAllowedException does not exist. Did you forget a USE statement, or did you not list all dependencies?

Scrutinizer analyzes your composer.json/composer.lock file if available to determine the classes, and functions that are defined by your dependencies.

It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.

Loading history...
873
				OC_Response::setStatus(405);
874
				return;
875
			}
876
		}
877
878
		// Handle WebDAV
879
		if ($_SERVER['REQUEST_METHOD'] == 'PROPFIND') {
880
			// not allowed any more to prevent people
881
			// mounting this root directly.
882
			// Users need to mount remote.php/webdav instead.
883
			header('HTTP/1.1 405 Method Not Allowed');
884
			header('Status: 405 Method Not Allowed');
885
			return;
886
		}
887
888
		// Someone is logged in
889
		if (OC_User::isLoggedIn()) {
0 ignored issues
show
Deprecated Code introduced by
The method OC_User::isLoggedIn() has been deprecated with message: use \OC::$server->getUserSession()->isLoggedIn()

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
890
			OC_App::loadApps();
891
			OC_User::setupBackends();
892
			OC_Util::setupFS();
893
			// FIXME
894
			// Redirect to default application
895
			OC_Util::redirectToDefaultPage();
896
		} else {
897
			// Not handled and not logged in
898
			header('Location: '.\OC::$server->getURLGenerator()->linkToRouteAbsolute('core.login.showLoginForm'));
899
		}
900
	}
901
902
	/**
903
	 * Check login: apache auth, auth token, basic auth
904
	 *
905
	 * @param OCP\IRequest $request
906
	 * @return boolean
907
	 */
908
	private static function handleLogin(OCP\IRequest $request) {
909
		$userSession = self::$server->getUserSession();
910
		if (OC_User::handleApacheAuth()) {
911
			return true;
912
		}
913
		if ($userSession->tryTokenLogin($request)) {
914
			return true;
915
		}
916
		if ($userSession->tryBasicAuthLogin($request)) {
917
			return true;
918
		}
919
		return false;
920
	}
921
922
	protected static function handleAuthHeaders() {
923
		//copy http auth headers for apache+php-fcgid work around
924
		if (isset($_SERVER['HTTP_XAUTHORIZATION']) && !isset($_SERVER['HTTP_AUTHORIZATION'])) {
925
			$_SERVER['HTTP_AUTHORIZATION'] = $_SERVER['HTTP_XAUTHORIZATION'];
926
		}
927
928
		// Extract PHP_AUTH_USER/PHP_AUTH_PW from other headers if necessary.
929
		$vars = array(
930
			'HTTP_AUTHORIZATION', // apache+php-cgi work around
931
			'REDIRECT_HTTP_AUTHORIZATION', // apache+php-cgi alternative
932
		);
933
		foreach ($vars as $var) {
934
			if (isset($_SERVER[$var]) && preg_match('/Basic\s+(.*)$/i', $_SERVER[$var], $matches)) {
935
				list($name, $password) = explode(':', base64_decode($matches[1]), 2);
936
				$_SERVER['PHP_AUTH_USER'] = $name;
937
				$_SERVER['PHP_AUTH_PW'] = $password;
938
				break;
939
			}
940
		}
941
	}
942
}
943
944
OC::init();
945