util.php 40.8 KB
Newer Older
Jakob Sack's avatar
Jakob Sack committed
1
<?php
2

Jakob Sack's avatar
Jakob Sack committed
3
4
5
6
/**
 * Class for utility functions
 *
 */
Jakob Sack's avatar
Jakob Sack committed
7
class OC_Util {
8
9
10
11
12
	public static $scripts = array();
	public static $styles = array();
	public static $headers = array();
	private static $rootMounted = false;
	private static $fsSetup = false;
Jakob Sack's avatar
Jakob Sack committed
13

14
15
	private static function initLocalStorageRootFS() {
		// mount local file backend as root
16
		$configDataDirectory = OC_Config::getValue("datadirectory", OC::$SERVERROOT . "/data");
17
18
		//first set up the local "root" storage
		\OC\Files\Filesystem::initMounts();
19
20
		if (!self::$rootMounted) {
			\OC\Files\Filesystem::mount('\OC\Files\Storage\Local', array('datadir' => $configDataDirectory), '/');
21
22
23
			self::$rootMounted = true;
		}
	}
24

25
26
27
28
	/**
	 * mounting an object storage as the root fs will in essence remove the
	 * necessity of a data folder being present.
	 * TODO make home storage aware of this and use the object storage instead of local disk access
29
	 *
30
31
	 * @param array $config containing 'class' and optional 'arguments'
	 */
32
	private static function initObjectStoreRootFS($config) {
33
34
		// check misconfiguration
		if (empty($config['class'])) {
35
			\OCP\Util::writeLog('files', 'No class given for objectstore', \OCP\Util::ERROR);
36
37
38
39
		}
		if (!isset($config['arguments'])) {
			$config['arguments'] = array();
		}
40
41
42
43
44
45

		// instantiate object store implementation
		$config['arguments']['objectstore'] = new $config['class']($config['arguments']);
		// mount with plain / root object store implementation
		$config['class'] = '\OC\Files\ObjectStore\ObjectStoreStorage';

46
47
		// mount object storage as root
		\OC\Files\Filesystem::initMounts();
48
		if (!self::$rootMounted) {
49
50
51
52
			\OC\Files\Filesystem::mount($config['class'], $config['arguments'], '/');
			self::$rootMounted = true;
		}
	}
53

kondou's avatar
kondou committed
54
	/**
55
	 * Can be set up
56
	 *
kondou's avatar
kondou committed
57
	 * @param string $user
kondou's avatar
kondou committed
58
	 * @return boolean
59
	 * @description configure the initial filesystem based on the configuration
kondou's avatar
kondou committed
60
	 */
61
	public static function setupFS($user = '') {
62
		//setting up the filesystem twice can only lead to trouble
63
		if (self::$fsSetup) {
Jakob Sack's avatar
Jakob Sack committed
64
65
66
			return false;
		}

Robin Appelman's avatar
Robin Appelman committed
67
68
		\OC::$server->getEventLogger()->start('setup_fs', 'Setup filesystem');

69
		// If we are not forced to load a specific user we load the one that is logged in
70
		if ($user == "" && OC_User::isLoggedIn()) {
71
72
73
			$user = OC_User::getUser();
		}

74
		// load all filesystem apps before, so no setup-hook gets lost
75
		OC_App::loadApps(array('filesystem'));
76

77
78
79
80
		// the filesystem will finish when $user is not empty,
		// mark fs setup here to avoid doing the setup from loading
		// OC_Filesystem
		if ($user != '') {
81
			self::$fsSetup = true;
82
83
		}

84
		//check if we are using an object storage
85
86
		$objectStore = OC_Config::getValue('objectstore');
		if (isset($objectStore)) {
87
			self::initObjectStoreRootFS($objectStore);
88
89
		} else {
			self::initLocalStorageRootFS();
90
		}
Bart Visscher's avatar
Bart Visscher committed
91

92
		if ($user != '' && !OCP\User::userExists($user)) {
Robin Appelman's avatar
Robin Appelman committed
93
			\OC::$server->getEventLogger()->end('setup_fs');
94
95
96
			return false;
		}

kondou's avatar
kondou committed
97
		//if we aren't logged in, there is no use to set up the filesystem
98
99
		if ($user != "") {
			\OC\Files\Filesystem::addStorageWrapper('oc_quota', function ($mountPoint, $storage) {
100
101
102
				// set up quota for home storages, even for other users
				// which can happen when using sharing

103
104
105
				/**
				 * @var \OC\Files\Storage\Storage $storage
				 */
106
				if ($storage->instanceOfStorage('\OC\Files\Storage\Home')
107
					|| $storage->instanceOfStorage('\OC\Files\ObjectStore\HomeObjectStoreStorage')
108
				) {
109
110
111
					if (is_object($storage->getUser())) {
						$user = $storage->getUser()->getUID();
						$quota = OC_Util::getUserQuota($user);
Morris Jobke's avatar
Morris Jobke committed
112
						if ($quota !== \OCP\Files\FileInfo::SPACE_UNLIMITED) {
113
114
							return new \OC\Files\Storage\Wrapper\Quota(array('storage' => $storage, 'quota' => $quota, 'root' => 'files'));
						}
115
					}
116
117
118
119
120
				}

				return $storage;
			});

121
			$userDir = '/' . $user . '/files';
122

123
			//jail the user into his "home" directory
kondou's avatar
kondou committed
124
			\OC\Files\Filesystem::init($user, $userDir);
Robin Appelman's avatar
Robin Appelman committed
125

126
127
			$fileOperationProxy = new OC_FileProxy_FileOperations();
			OC_FileProxy::register($fileOperationProxy);
Robin Appelman's avatar
Robin Appelman committed
128

129
130
131
			//trigger creation of user home and /files folder
			\OC::$server->getUserFolder($user);

kondou's avatar
kondou committed
132
			OC_Hook::emit('OC_Filesystem', 'setup', array('user' => $user, 'user_dir' => $userDir));
Jakob Sack's avatar
Jakob Sack committed
133
		}
Robin Appelman's avatar
Robin Appelman committed
134
		\OC::$server->getEventLogger()->end('setup_fs');
Robin Appelman's avatar
Robin Appelman committed
135
		return true;
Jakob Sack's avatar
Jakob Sack committed
136
137
	}

138
	/**
139
	 * check if a password is required for each public link
140
	 *
141
142
143
144
145
146
147
148
	 * @return boolean
	 */
	public static function isPublicLinkPasswordRequired() {
		$appConfig = \OC::$server->getAppConfig();
		$enforcePassword = $appConfig->getValue('core', 'shareapi_enforce_links_password', 'no');
		return ($enforcePassword === 'yes') ? true : false;
	}

149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
	/**
	 * check if sharing is disabled for the current user
	 *
	 * @return boolean
	 */
	public static function isSharingDisabledForUser() {
		if (\OC_Appconfig::getValue('core', 'shareapi_exclude_groups', 'no') === 'yes') {
			$user = \OCP\User::getUser();
			$groupsList = \OC_Appconfig::getValue('core', 'shareapi_exclude_groups_list', '');
			$excludedGroups = explode(',', $groupsList);
			$usersGroups = \OC_Group::getUserGroups($user);
			if (!empty($usersGroups)) {
				$remainingGroups = array_diff($usersGroups, $excludedGroups);
				// if the user is only in groups which are disabled for sharing then
				// sharing is also disabled for the user
				if (empty($remainingGroups)) {
					return true;
				}
			}
		}
		return false;
	}

172
173
	/**
	 * check if share API enforces a default expire date
174
	 *
175
176
177
178
179
180
181
182
183
184
185
186
187
	 * @return boolean
	 */
	public static function isDefaultExpireDateEnforced() {
		$isDefaultExpireDateEnabled = \OCP\Config::getAppValue('core', 'shareapi_default_expire_date', 'no');
		$enforceDefaultExpireDate = false;
		if ($isDefaultExpireDateEnabled === 'yes') {
			$value = \OCP\Config::getAppValue('core', 'shareapi_enforce_expire_date', 'no');
			$enforceDefaultExpireDate = ($value === 'yes') ? true : false;
		}

		return $enforceDefaultExpireDate;
	}

188
	/**
Lukas Reschke's avatar
Lukas Reschke committed
189
	 * Get the quota of a user
190
	 *
191
	 * @param string $user
Lukas Reschke's avatar
Lukas Reschke committed
192
	 * @return int Quota bytes
193
	 */
194
	public static function getUserQuota($user) {
195
196
		$config = \OC::$server->getConfig();
		$userQuota = $config->getUserValue($user, 'files', 'quota', 'default');
197
		if ($userQuota === 'default') {
198
			$userQuota = $config->getAppValue('files', 'default_quota', 'none');
199
200
		}
		if($userQuota === 'none') {
Morris Jobke's avatar
Morris Jobke committed
201
			return \OCP\Files\FileInfo::SPACE_UNLIMITED;
202
203
204
205
206
		}else{
			return OC_Helper::computerFileSize($userQuota);
		}
	}

207
	/**
208
	 * copies the skeleton to the users /files
209
	 *
210
211
	 * @param \OC\User\User $user
	 * @param \OCP\Files\Folder $userDirectory
212
	 */
213
214
215
216
	public static function copySkeleton(\OC\User\User $user, \OCP\Files\Folder $userDirectory) {

		$skeletonDirectory = \OCP\Config::getSystemValue('skeletondirectory', \OC::$SERVERROOT . '/core/skeleton');

217
		if (!empty($skeletonDirectory)) {
218
219
220
221
222
223
224
225
			\OCP\Util::writeLog(
				'files_skeleton',
				'copying skeleton for '.$user->getUID().' from '.$skeletonDirectory.' to '.$userDirectory->getFullPath('/'),
				\OCP\Util::DEBUG
			);
			self::copyr($skeletonDirectory, $userDirectory);
			// update the file cache
			$userDirectory->getStorage()->getScanner()->scan('', \OC\Files\Cache\Scanner::SCAN_RECURSIVE);
226
		}
227
228
229
	}

	/**
230
	 * copies a directory recursively by using streams
231
	 *
232
	 * @param string $source
233
	 * @param \OCP\Files\Folder $target
234
235
	 * @return void
	 */
236
	public static function copyr($source, \OCP\Files\Folder $target) {
237
		$dir = opendir($source);
238
239
240
		while (false !== ($file = readdir($dir))) {
			if (!\OC\Files\Filesystem::isIgnoredDir($file)) {
				if (is_dir($source . '/' . $file)) {
241
242
					$child = $target->newFolder($file);
					self::copyr($source . '/' . $file, $child);
243
				} else {
244
245
					$child = $target->newFile($file);
					stream_copy_to_stream(fopen($source . '/' . $file,'r'), $child->fopen('w'));
246
247
248
249
250
251
				}
			}
		}
		closedir($dir);
	}

kondou's avatar
kondou committed
252
253
	/**
	 * @return void
254
	 */
Thomas Mueller's avatar
Thomas Mueller committed
255
	public static function tearDownFS() {
256
		\OC\Files\Filesystem::tearDown();
257
258
		self::$fsSetup = false;
		self::$rootMounted = false;
Jakob Sack's avatar
Jakob Sack committed
259
	}
260

Jakob Sack's avatar
Jakob Sack committed
261
	/**
262
	 * get the current installed version of ownCloud
263
	 *
Jakob Sack's avatar
Jakob Sack committed
264
265
	 * @return array
	 */
Thomas Mueller's avatar
Thomas Mueller committed
266
	public static function getVersion() {
267
		OC_Util::loadVersion();
268
		return \OC::$server->getSession()->get('OC_Version');
269
270
271
	}

	/**
272
	 * get the current installed version string of ownCloud
273
	 *
274
275
	 * @return string
	 */
Thomas Mueller's avatar
Thomas Mueller committed
276
	public static function getVersionString() {
277
		OC_Util::loadVersion();
278
		return \OC::$server->getSession()->get('OC_VersionString');
Jakob Sack's avatar
Jakob Sack committed
279
280
	}

Robin Appelman's avatar
Robin Appelman committed
281
	/**
kondou's avatar
kondou committed
282
	 * @description get the current installed edition of ownCloud. There is the community
Bart Visscher's avatar
Bart Visscher committed
283
284
	 * edition that just returns an empty string and the enterprise edition
	 * that returns "Enterprise".
Robin Appelman's avatar
Robin Appelman committed
285
286
	 * @return string
	 */
Thomas Mueller's avatar
Thomas Mueller committed
287
	public static function getEditionString() {
288
289
290
291
292
293
		if (OC_App::isEnabled('enterprise_key')) {
			return "Enterprise";
		} else {
			return "";
		}

294
295
296
297
298
299
300
301
	}

	/**
	 * @description get the update channel of the current installed of ownCloud.
	 * @return string
	 */
	public static function getChannel() {
		OC_Util::loadVersion();
302
		return \OC::$server->getSession()->get('OC_Channel');
303
	}
304

305
306
307
308
309
310
	/**
	 * @description get the build number of the current installed of ownCloud.
	 * @return string
	 */
	public static function getBuild() {
		OC_Util::loadVersion();
311
		return \OC::$server->getSession()->get('OC_Build');
312
313
314
315
316
317
	}

	/**
	 * @description load the version.php into the session as cache
	 */
	private static function loadVersion() {
318
319
		$timestamp = filemtime(OC::$SERVERROOT . '/version.php');
		if (!\OC::$server->getSession()->exists('OC_Version') or OC::$server->getSession()->get('OC_Version_Timestamp') != $timestamp) {
320
321
			require 'version.php';
			$session = \OC::$server->getSession();
322
323
			/** @var $timestamp int */
			$session->set('OC_Version_Timestamp', $timestamp);
324
325
326
327
328
329
330
331
			/** @var $OC_Version string */
			$session->set('OC_Version', $OC_Version);
			/** @var $OC_VersionString string */
			$session->set('OC_VersionString', $OC_VersionString);
			/** @var $OC_Channel string */
			$session->set('OC_Channel', $OC_Channel);
			/** @var $OC_Build string */
			$session->set('OC_Build', $OC_Build);
332
		}
Robin Appelman's avatar
Robin Appelman committed
333
	}
334

Jakob Sack's avatar
Jakob Sack committed
335
	/**
Morris Jobke's avatar
Morris Jobke committed
336
	 * generates a path for JS/CSS files. If no application is provided it will create the path for core.
Jakob Sack's avatar
Jakob Sack committed
337
	 *
338
339
340
	 * @param string $application application to get the files from
	 * @param string $directory directory withing this application (css, js, vendor, etc)
	 * @param string $file the file inside of the above folder
Morris Jobke's avatar
Morris Jobke committed
341
	 * @return string the path
Jakob Sack's avatar
Jakob Sack committed
342
	 */
Morris Jobke's avatar
Morris Jobke committed
343
	private static function generatePath($application, $directory, $file) {
344
		if (is_null($file)) {
Jakob Sack's avatar
Jakob Sack committed
345
346
347
			$file = $application;
			$application = "";
		}
348
		if (!empty($application)) {
Morris Jobke's avatar
Morris Jobke committed
349
			return "$application/$directory/$file";
kondou's avatar
kondou committed
350
		} else {
Morris Jobke's avatar
Morris Jobke committed
351
			return "$directory/$file";
Jakob Sack's avatar
Jakob Sack committed
352
353
354
		}
	}

Morris Jobke's avatar
Morris Jobke committed
355
356
357
358
359
360
361
362
	/**
	 * add a javascript file
	 *
	 * @param string $application application id
	 * @param string|null $file filename
	 * @return void
	 */
	public static function addScript($application, $file = null) {
363
364
		$path = OC_Util::generatePath($application, 'js', $file);
		if (!in_array($path, self::$scripts)) {
Bernhard Posselt's avatar
Bernhard Posselt committed
365
366
367
368
			// core js files need separate handling
			if ($application !== 'core' && $file !== null) {
				self::addTranslations($application);
			}
369
370
			self::$scripts[] = $path;
		}
Morris Jobke's avatar
Morris Jobke committed
371
372
373
374
375
376
377
378
379
380
	}

	/**
	 * add a javascript file from the vendor sub folder
	 *
	 * @param string $application application id
	 * @param string|null $file filename
	 * @return void
	 */
	public static function addVendorScript($application, $file = null) {
381
382
383
384
		$path = OC_Util::generatePath($application, 'vendor', $file);
		if (!in_array($path, self::$scripts)) {
			self::$scripts[] = $path;
		}
Morris Jobke's avatar
Morris Jobke committed
385
386
	}

387
388
389
390
391
392
393
394
395
396
397
398
	/**
	 * add a translation JS file
	 *
	 * @param string $application application id
	 * @param string $languageCode language code, defaults to the current language
	 */
	public static function addTranslations($application, $languageCode = null) {
		if (is_null($languageCode)) {
			$l = new \OC_L10N($application);
			$languageCode = $l->getLanguageCode($application);
		}
		if (!empty($application)) {
399
			$path = "$application/l10n/$languageCode";
400
		} else {
401
402
403
404
			$path = "l10n/$languageCode";
		}
		if (!in_array($path, self::$scripts)) {
			self::$scripts[] = $path;
405
406
407
		}
	}

Jakob Sack's avatar
Jakob Sack committed
408
	/**
409
	 * add a css file
Jakob Sack's avatar
Jakob Sack committed
410
	 *
411
	 * @param string $application application id
412
	 * @param string|null $file filename
kondou's avatar
kondou committed
413
	 * @return void
Jakob Sack's avatar
Jakob Sack committed
414
	 */
415
	public static function addStyle($application, $file = null) {
416
417
418
419
		$path = OC_Util::generatePath($application, 'css', $file);
		if (!in_array($path, self::$styles)) {
			self::$styles[] = $path;
		}
Morris Jobke's avatar
Morris Jobke committed
420
421
422
423
424
425
426
427
428
429
	}

	/**
	 * add a css file from the vendor sub folder
	 *
	 * @param string $application application id
	 * @param string|null $file filename
	 * @return void
	 */
	public static function addVendorStyle($application, $file = null) {
430
431
432
433
		$path = OC_Util::generatePath($application, 'vendor', $file);
		if (!in_array($path, self::$styles)) {
			self::$styles[] = $path;
		}
Jakob Sack's avatar
Jakob Sack committed
434
435
436
	}

	/**
437
	 * Add a custom element to the header
438
439
	 * If $text is null then the element will be written as empty element.
	 * So use "" to get a closing tag.
kondou's avatar
kondou committed
440
	 * @param string $tag tag name of the element
441
	 * @param array $attributes array of attributes for the element
Jakob Sack's avatar
Jakob Sack committed
442
443
	 * @param string $text the text content for the element
	 */
444
	public static function addHeader($tag, $attributes, $text=null) {
kondou's avatar
kondou committed
445
		self::$headers[] = array(
446
447
448
			'tag' => $tag,
			'attributes' => $attributes,
			'text' => $text
kondou's avatar
kondou committed
449
		);
Jakob Sack's avatar
Jakob Sack committed
450
451
	}

Robin Appelman's avatar
Robin Appelman committed
452
	/**
453
	 * formats a timestamp in the "right" way
Robin Appelman's avatar
Robin Appelman committed
454
	 *
kondou's avatar
kondou committed
455
456
	 * @param int $timestamp
	 * @param bool $dateOnly option to omit time from the result
457
	 * @param DateTimeZone|string $timeZone where the given timestamp shall be converted to
kondou's avatar
kondou committed
458
	 * @return string timestamp
Joas Schilling's avatar
Joas Schilling committed
459
460
	 *
	 * @deprecated Use \OC::$server->query('DateTimeFormatter') instead
Robin Appelman's avatar
Robin Appelman committed
461
	 */
462
	public static function formatDate($timestamp, $dateOnly = false, $timeZone = null) {
Joas Schilling's avatar
Joas Schilling committed
463
464
		if ($timeZone !== null && !$timeZone instanceof \DateTimeZone) {
			$timeZone = new \DateTimeZone($timeZone);
Robin Appelman's avatar
Robin Appelman committed
465
		}
Joas Schilling's avatar
Joas Schilling committed
466
467
468
469
470
471
472

		/** @var \OC\DateTimeFormatter $formatter */
		$formatter = \OC::$server->query('DateTimeFormatter');
		if ($dateOnly) {
			return $formatter->formatDate($timestamp, 'long', $timeZone);
		}
		return $formatter->formatDateTime($timestamp, 'long', 'long', $timeZone);
Jakob Sack's avatar
Jakob Sack committed
473
474
475
	}

	/**
476
	 * check if the current server configuration is suitable for ownCloud
477
	 *
478
	 * @param \OCP\IConfig $config
Jakob Sack's avatar
Jakob Sack committed
479
480
	 * @return array arrays with error messages and hints
	 */
Lukas Reschke's avatar
Lukas Reschke committed
481
	public static function checkServer(\OCP\IConfig $config) {
482
		$l = \OC::$server->getL10N('lib');
483
		$errors = array();
484
		$CONFIG_DATADIRECTORY = $config->getSystemValue('datadirectory', OC::$SERVERROOT . '/data');
485

486
		if (!self::needUpgrade($config) && $config->getSystemValue('installed', false)) {
487
488
489
490
			// this check needs to be done every time
			$errors = self::checkDataDirectoryValidity($CONFIG_DATADIRECTORY);
		}

491
		// Assume that if checkServer() succeeded before in this session, then all is fine.
Jörn Friedrich Dreyer's avatar
Jörn Friedrich Dreyer committed
492
		if (\OC::$server->getSession()->exists('checkServer_succeeded') && \OC::$server->getSession()->get('checkServer_succeeded')) {
493
			return $errors;
kondou's avatar
kondou committed
494
		}
495

kondou's avatar
kondou committed
496
		$webServerRestart = false;
497
498
499
		$setup = new OC_Setup($config);
		$availableDatabases = $setup->getSupportedDatabases();
		if (empty($availableDatabases)) {
kondou's avatar
kondou committed
500
			$errors[] = array(
501
502
				'error' => $l->t('No database drivers (sqlite, mysql, or postgresql) installed.'),
				'hint' => '' //TODO: sane hint
kondou's avatar
kondou committed
503
504
			);
			$webServerRestart = true;
Jakob Sack's avatar
Jakob Sack committed
505
506
		}

Thomas Müller's avatar
Thomas Müller committed
507
		//common hint for all file permissions error messages
508
		$permissionsHint = $l->t('Permissions can usually be fixed by '
509
510
			. '%sgiving the webserver write access to the root directory%s.',
			array('<a href="' . \OC_Helper::linkToDocs('admin-dir_permissions') . '" target="_blank">', '</a>'));
Jakob Sack's avatar
Jakob Sack committed
511

512
		// Check if config folder is writable.
513
		if (!is_writable(OC::$configDir) or !is_readable(OC::$configDir)) {
514
			$errors[] = array(
515
516
				'error' => $l->t('Cannot write into "config" directory'),
				'hint' => $l->t('This can usually be fixed by '
517
518
519
					. '%sgiving the webserver write access to the config directory%s.',
					array('<a href="' . \OC_Helper::linkToDocs('admin-dir_permissions') . '" target="_blank">', '</a>'))
			);
520
521
		}

522
		// Check if there is a writable install folder.
523
		if ($config->getSystemValue('appstoreenabled', true)) {
524
			if (OC_App::getInstallPath() === null
Bart Visscher's avatar
Bart Visscher committed
525
				|| !is_writable(OC_App::getInstallPath())
526
527
				|| !is_readable(OC_App::getInstallPath())
			) {
528
				$errors[] = array(
529
530
					'error' => $l->t('Cannot write into "apps" directory'),
					'hint' => $l->t('This can usually be fixed by '
531
532
533
534
						. '%sgiving the webserver write access to the apps directory%s'
						. ' or disabling the appstore in the config file.',
						array('<a href="' . \OC_Helper::linkToDocs('admin-dir_permissions') . '" target="_blank">', '</a>'))
				);
535
			}
536
537
		}
		// Create root dir.
538
539
540
541
542
543
544
545
546
547
548
549
550
551
		if ($config->getSystemValue('installed', false)) {
			if (!is_dir($CONFIG_DATADIRECTORY)) {
				$success = @mkdir($CONFIG_DATADIRECTORY);
				if ($success) {
					$errors = array_merge($errors, self::checkDataDirectoryPermissions($CONFIG_DATADIRECTORY));
				} else {
					$errors[] = array(
						'error' => $l->t('Cannot create "data" directory (%s)', array($CONFIG_DATADIRECTORY)),
						'hint' => $l->t('This can usually be fixed by '
							. '<a href="%s" target="_blank">giving the webserver write access to the root directory</a>.',
							array(OC_Helper::linkToDocs('admin-dir_permissions')))
					);
				}
			} else if (!is_writable($CONFIG_DATADIRECTORY) or !is_readable($CONFIG_DATADIRECTORY)) {
552
				$errors[] = array(
553
554
					'error' => 'Data directory (' . $CONFIG_DATADIRECTORY . ') not writable by ownCloud',
					'hint' => $permissionsHint
555
				);
556
557
			} else {
				$errors = array_merge($errors, self::checkDataDirectoryPermissions($CONFIG_DATADIRECTORY));
558
			}
Jakob Sack's avatar
Jakob Sack committed
559
		}
kondou's avatar
kondou committed
560

561
		if (!OC_Util::isSetLocaleWorking()) {
562
			$errors[] = array(
563
				'error' => $l->t('Setting locale to %s failed',
564
565
					array('en_US.UTF-8/fr_FR.UTF-8/es_ES.UTF-8/de_DE.UTF-8/ru_RU.UTF-8/'
						. 'pt_BR.UTF-8/it_IT.UTF-8/ja_JP.UTF-8/zh_CN.UTF-8')),
helix84's avatar
helix84 committed
566
				'hint' => $l->t('Please install one of these locales on your system and restart your webserver.')
567
568
569
			);
		}

Lukas Reschke's avatar
Lukas Reschke committed
570
571
572
573
574
575
576
577
578
		// Contains the dependencies that should be checked against
		// classes = class_exists
		// functions = function_exists
		// defined = defined
		// If the dependency is not found the missing module name is shown to the EndUser
		$dependencies = array(
			'classes' => array(
				'ZipArchive' => 'zip',
				'DOMDocument' => 'dom',
Lukas Reschke's avatar
Lukas Reschke committed
579
				'XMLWriter' => 'XMLWriter'
Lukas Reschke's avatar
Lukas Reschke committed
580
581
582
583
584
585
586
587
588
			),
			'functions' => array(
				'xml_parser_create' => 'libxml',
				'mb_detect_encoding' => 'mb multibyte',
				'ctype_digit' => 'ctype',
				'json_encode' => 'JSON',
				'gd_info' => 'GD',
				'gzencode' => 'zlib',
				'iconv' => 'iconv',
Lukas Reschke's avatar
Lukas Reschke committed
589
590
				'simplexml_load_string' => 'SimpleXML',
				'hash' => 'HASH Message Digest Framework'
Lukas Reschke's avatar
Lukas Reschke committed
591
592
593
594
595
596
			),
			'defined' => array(
				'PDO::ATTR_DRIVER_NAME' => 'PDO'
			)
		);
		$missingDependencies = array();
597
		$moduleHint = $l->t('Please ask your server administrator to install the module.');
Lukas Reschke's avatar
Lukas Reschke committed
598
599
600
601
602

		foreach ($dependencies['classes'] as $class => $module) {
			if (!class_exists($class)) {
				$missingDependencies[] = $module;
			}
603
		}
Lukas Reschke's avatar
Lukas Reschke committed
604
605
606
607
		foreach ($dependencies['functions'] as $function => $module) {
			if (!function_exists($function)) {
				$missingDependencies[] = $module;
			}
Frank Karlitschek's avatar
Frank Karlitschek committed
608
		}
Lukas Reschke's avatar
Lukas Reschke committed
609
610
611
612
		foreach ($dependencies['defined'] as $defined => $module) {
			if (!defined($defined)) {
				$missingDependencies[] = $module;
			}
Lukas Reschke's avatar
Lukas Reschke committed
613
		}
Lukas Reschke's avatar
Lukas Reschke committed
614
615

		foreach($missingDependencies as $missingDependency) {
kondou's avatar
kondou committed
616
			$errors[] = array(
Lukas Reschke's avatar
Lukas Reschke committed
617
				'error' => $l->t('PHP module %s not installed.', array($missingDependency)),
618
				'hint' => $moduleHint
kondou's avatar
kondou committed
619
620
			);
			$webServerRestart = true;
621
		}
Lukas Reschke's avatar
Lukas Reschke committed
622

623
		if (version_compare(phpversion(), '5.4.0', '<')) {
kondou's avatar
kondou committed
624
			$errors[] = array(
625
				'error' => $l->t('PHP %s or higher is required.', '5.4.0'),
626
627
				'hint' => $l->t('Please ask your server administrator to update PHP to the latest version.'
					. ' Your PHP version is no longer supported by ownCloud and the PHP community.')
kondou's avatar
kondou committed
628
629
			);
			$webServerRestart = true;
630
		}
631
632
633
634
635

		/**
		 * PHP 5.6 ships with a PHP setting which throws notices by default for a
		 * lot of endpoints. Thus we need to ensure that the value is set to -1
		 *
636
637
638
639
		 * FIXME: Due to https://github.com/owncloud/core/pull/13593#issuecomment-71178078
		 * this check is disabled for HHVM at the moment. This should get re-evaluated
		 * at a later point.
		 *
640
641
642
		 * @link https://github.com/owncloud/core/issues/13592
		 */
		if(version_compare(phpversion(), '5.6.0', '>=') &&
643
			!self::runningOnHhvm() &&
644
645
			\OC::$server->getIniWrapper()->getNumeric('always_populate_raw_post_data') !== -1) {
			$errors[] = array(
Morris Jobke's avatar
Morris Jobke committed
646
647
				'error' => $l->t('PHP is configured to populate raw post data. Since PHP 5.6 this will lead to PHP throwing notices for perfectly valid code.'),
				'hint' => $l->t('To fix this issue set <code>always_populate_raw_post_data</code> to <code>-1</code> in your php.ini')
648
649
650
			);
		}

651
652
		if (!self::isAnnotationsWorking()) {
			$errors[] = array(
Morris Jobke's avatar
Morris Jobke committed
653
654
				'error' => $l->t('PHP is apparently setup to strip inline doc blocks. This will make several core apps inaccessible.'),
				'hint' => $l->t('This is probably caused by a cache/accelerator such as Zend OPcache or eAccelerator.')
655
656
			);
		}
657

658
		if ($webServerRestart) {
kondou's avatar
kondou committed
659
			$errors[] = array(
660
661
				'error' => $l->t('PHP modules have been installed, but they are still listed as missing?'),
				'hint' => $l->t('Please ask your server administrator to restart the web server.')
kondou's avatar
kondou committed
662
			);
Frank Karlitschek's avatar
Frank Karlitschek committed
663
		}
Jakob Sack's avatar
Jakob Sack committed
664

665
666
		$errors = array_merge($errors, self::checkDatabaseVersion());

667
		// Cache the result of this function
Jörn Friedrich Dreyer's avatar
Jörn Friedrich Dreyer committed
668
		\OC::$server->getSession()->set('checkServer_succeeded', count($errors) == 0);
669

Jakob Sack's avatar
Jakob Sack committed
670
671
		return $errors;
	}
672

673
674
	/**
	 * Check the database version
675
	 *
676
677
678
	 * @return array errors array
	 */
	public static function checkDatabaseVersion() {
679
		$l = \OC::$server->getL10N('lib');
680
681
682
683
684
685
686
687
688
689
690
		$errors = array();
		$dbType = \OC_Config::getValue('dbtype', 'sqlite');
		if ($dbType === 'pgsql') {
			// check PostgreSQL version
			try {
				$result = \OC_DB::executeAudited('SHOW SERVER_VERSION');
				$data = $result->fetchRow();
				if (isset($data['server_version'])) {
					$version = $data['server_version'];
					if (version_compare($version, '9.0.0', '<')) {
						$errors[] = array(
691
692
							'error' => $l->t('PostgreSQL >= 9 required'),
							'hint' => $l->t('Please upgrade your database version')
693
694
695
						);
					}
				}
Thomas Müller's avatar
Thomas Müller committed
696
			} catch (\Doctrine\DBAL\DBALException $e) {
697
				\OCP\Util::logException('core', $e);
698
				$errors[] = array(
699
700
					'error' => $l->t('Error occurred while checking PostgreSQL version'),
					'hint' => $l->t('Please make sure you have PostgreSQL >= 9 or'
701
						. ' check the logs for more information about the error')
702
703
704
705
706
707
708
				);
			}
		}
		return $errors;
	}


709
	/**
710
	 * check if there are still some encrypted files stored
711
	 *
712
713
714
715
716
717
718
	 * @return boolean
	 */
	public static function encryptedFiles() {
		//check if encryption was enabled in the past
		$encryptedFiles = false;
		if (OC_App::isEnabled('files_encryption') === false) {
			$view = new OC\Files\View('/' . OCP\User::getUser());
719
720
721
			$keysPath = '/files_encryption/keys';
			if ($view->is_dir($keysPath)) {
				$dircontent = $view->getDirectoryContent($keysPath);
722
723
724
				if (!empty($dircontent)) {
					$encryptedFiles = true;
				}
725
726
			}
		}
727

728
729
		return $encryptedFiles;
	}
730

731
	/**
732
	 * check if a backup from the encryption keys exists
733
	 *
734
735
736
737
738
739
740
	 * @return boolean
	 */
	public static function backupKeysExists() {
		//check if encryption was enabled in the past
		$backupExists = false;
		if (OC_App::isEnabled('files_encryption') === false) {
			$view = new OC\Files\View('/' . OCP\User::getUser());
741
			$backupPath = '/files_encryption/backup.decryptAll';
742
743
744
745
746
747
748
749
750
751
752
			if ($view->is_dir($backupPath)) {
				$dircontent = $view->getDirectoryContent($backupPath);
				if (!empty($dircontent)) {
					$backupExists = true;
				}
			}
		}

		return $backupExists;
	}

753
	/**
754
	 * Check for correct file permissions of data directory
755
	 *
756
	 * @param string $dataDirectory
kondou's avatar
kondou committed
757
758
	 * @return array arrays with error messages and hints
	 */
759
	public static function checkDataDirectoryPermissions($dataDirectory) {
760
		$l = \OC::$server->getL10N('lib');
761
		$errors = array();
kondou's avatar
kondou committed
762
		if (self::runningOnWindows()) {
763
764
			//TODO: permissions checks for windows hosts
		} else {
765
			$permissionsModHint = $l->t('Please change the permissions to 0770 so that the directory'
766
				. ' cannot be listed by other users.');
kondou's avatar
kondou committed
767
768
			$perms = substr(decoct(@fileperms($dataDirectory)), -3);
			if (substr($perms, -1) != '0') {
769
				chmod($dataDirectory, 0770);
770
				clearstatcache();
kondou's avatar
kondou committed
771
772
773
				$perms = substr(decoct(@fileperms($dataDirectory)), -3);
				if (substr($perms, 2, 1) != '0') {
					$errors[] = array(
774
						'error' => $l->t('Data directory (%s) is readable by other users', array($dataDirectory)),
kondou's avatar
kondou committed
775
776
						'hint' => $permissionsModHint
					);
777
778
779
780
781
782
				}
			}
		}
		return $errors;
	}

783
784
785
786
787
788
789
790
	/**
	 * Check that the data directory exists and is valid by
	 * checking the existence of the ".ocdata" file.
	 *
	 * @param string $dataDirectory data directory path
	 * @return bool true if the data directory is valid, false otherwise
	 */
	public static function checkDataDirectoryValidity($dataDirectory) {
791
		$l = \OC::$server->getL10N('lib');
792
		$errors = array();
793
		if (!file_exists($dataDirectory . '/.ocdata')) {
794
			$errors[] = array(
795
796
797
				'error' => $l->t('Data directory (%s) is invalid', array($dataDirectory)),
				'hint' => $l->t('Please check that the data directory contains a file' .
					' ".ocdata" in its root.')
798
799
800
801
802
			);
		}
		return $errors;
	}

kondou's avatar
kondou committed
803
	/**
Lukas Reschke's avatar
Lukas Reschke committed
804
	 * @param array $errors
805
	 * @param string[] $messages
kondou's avatar
kondou committed
806
	 */
807
	public static function displayLoginPage($errors = array(), $messages = []) {
808
		$parameters = array();
809
		foreach ($errors as $value) {
810
811
			$parameters[$value] = true;
		}
812
		$parameters['messages'] = $messages;
813
814
		if (!empty($_REQUEST['user'])) {
			$parameters["username"] = $_REQUEST['user'];
815
			$parameters['user_autofocus'] = false;
Bart Visscher's avatar
Bart Visscher committed
816
817
		} else {
			$parameters["username"] = '';
818
			$parameters['user_autofocus'] = true;
819
		}
820
		if (isset($_REQUEST['redirect_url'])) {
kondou's avatar
kondou committed
821
822
			$redirectUrl = $_REQUEST['redirect_url'];
			$parameters['redirect_url'] = urlencode($redirectUrl);
Thomas Mueller's avatar
Thomas Mueller committed
823
		}
824
825

		$parameters['alt_login'] = OC_App::getAlternativeLogIns();
826
		$parameters['rememberLoginAllowed'] = self::rememberLoginAllowed();
827
828
		OC_Template::printGuestPage("", "login", $parameters);
	}
829
830


831
	/**
832
	 * Check if the app is enabled, redirects to home if not
833
	 *
834
	 * @param string $app
kondou's avatar
kondou committed
835
	 * @return void
Thomas Mueller's avatar
Thomas Mueller committed
836
	 */
Thomas Mueller's avatar
Thomas Mueller committed
837
	public static function checkAppEnabled($app) {
838
839
		if (!OC_App::isEnabled($app)) {
			header('Location: ' . OC_Helper::linkToAbsolute('', 'index.php'));
840
841
842
843
			exit();
		}
	}

844
	/**
Thomas Mueller's avatar
Thomas Mueller committed
845
846
	 * Check if the user is logged in, redirects to home if not. With
	 * redirect URL parameter to the request URI.
847
	 *
kondou's avatar
kondou committed
848
	 * @return void
Thomas Mueller's avatar
Thomas Mueller committed
849
	 */
Thomas Mueller's avatar
Thomas Mueller committed
850
	public static function checkLoggedIn() {
851
		// Check if we are a user
852
853
854
855
		if (!OC_User::isLoggedIn()) {
			header('Location: ' . OC_Helper::linkToAbsolute('', 'index.php',
					array('redirect_url' => OC_Request::requestUri())
				));
856
857
858
859
860
			exit();
		}
	}

	/**
861
	 * Check if the user is a admin, redirects to home if not
862
	 *
kondou's avatar
kondou committed
863
	 * @return void
Thomas Mueller's avatar
Thomas Mueller committed
864
	 */
Thomas Mueller's avatar
Thomas Mueller committed
865
	public static function checkAdminUser() {
866
		OC_Util::checkLoggedIn();
867
868
		if (!OC_User::isAdminUser(OC_User::getUser())) {
			header('Location: ' . OC_Helper::linkToAbsolute('', 'index.php'));
869
870
871
872
			exit();
		}
	}

Georg Ehrke's avatar
Georg Ehrke committed
873
	/**
874
	 * Check if it is allowed to remember login.
875
876
	 *
	 * @note Every app can set 'rememberlogin' to 'false' to disable the remember login feature
877
878
879
880
	 *
	 * @return bool
	 */
	public static function rememberLoginAllowed() {
881
882
883
884
885
886
887
888
889
890
891

		$apps = OC_App::getEnabledApps();

		foreach ($apps as $app) {
			$appInfo = OC_App::getAppInfo($app);
			if (isset($appInfo['rememberlogin']) && $appInfo['rememberlogin'] === 'false') {
				return false;
			}

		}
		return true;
892
893
	}

Georg Ehrke's avatar
Georg Ehrke committed
894
	/**
895
	 * Check if the user is a subadmin, redirects to home if not
896
	 *
897
	 * @return null|boolean $groups where the current user is subadmin
Thomas Mueller's avatar
Thomas Mueller committed
898
	 */
Thomas Mueller's avatar
Thomas Mueller committed
899
	public static function checkSubAdminUser() {
900
		OC_Util::checkLoggedIn();
901
902
		if (!OC_SubAdmin::isSubAdmin(OC_User::getUser())) {
			header('Location: ' . OC_Helper::linkToAbsolute('', 'index.php'));
Georg Ehrke's avatar
Georg Ehrke committed
903
904
			exit();
		}
Georg Ehrke's avatar
Georg Ehrke committed
905
		return true;