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

Jakob Sack's avatar
Jakob Sack committed
3
/**
4
 * Class for utility functions
Jakob Sack's avatar
Jakob Sack committed
5
6
 *
 */
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
16
17
	protected static function getAppManager() {
		return \OC::$server->getAppManager();
	}

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

29
30
31
32
	/**
	 * 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
33
	 *
34
35
	 * @param array $config containing 'class' and optional 'arguments'
	 */
36
	private static function initObjectStoreRootFS($config) {
37
38
		// check misconfiguration
		if (empty($config['class'])) {
39
			\OCP\Util::writeLog('files', 'No class given for objectstore', \OCP\Util::ERROR);
40
41
42
43
		}
		if (!isset($config['arguments'])) {
			$config['arguments'] = array();
		}
44
45
46
47
48
49

		// 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';

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

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

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

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

78
		// load all filesystem apps before, so no setup-hook gets lost
79
		OC_App::loadApps(array('filesystem'));
80

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

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

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

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

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

				return $storage;
			});

125
			$userDir = '/' . $user . '/files';
126

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

130
131
			$fileOperationProxy = new OC_FileProxy_FileOperations();
			OC_FileProxy::register($fileOperationProxy);
Robin Appelman's avatar
Robin Appelman committed
132

133
134
135
			//trigger creation of user home and /files folder
			\OC::$server->getUserFolder($user);

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

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

153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
	/**
	 * 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;
	}

176
177
	/**
	 * check if share API enforces a default expire date
178
	 *
179
180
181
182
183
184
185
186
187
188
189
190
191
	 * @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;
	}

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

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

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

221
		if (!empty($skeletonDirectory)) {
222
223
224
225
226
227
228
229
			\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);
230
		}
231
232
233
	}

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

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

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

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

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

298
299
300
301
302
303
304
305
	}

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

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

	/**
	 * @description load the version.php into the session as cache
	 */
	private static function loadVersion() {
322
323
		$timestamp = filemtime(OC::$SERVERROOT . '/version.php');
		if (!\OC::$server->getSession()->exists('OC_Version') or OC::$server->getSession()->get('OC_Version_Timestamp') != $timestamp) {
324
			require OC::$SERVERROOT . '/version.php';
325
			$session = \OC::$server->getSession();
326
327
			/** @var $timestamp int */
			$session->set('OC_Version_Timestamp', $timestamp);
328
329
330
331
332
333
334
335
			/** @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);
336
		}
Robin Appelman's avatar
Robin Appelman committed
337
	}
338

Jakob Sack's avatar
Jakob Sack committed
339
	/**
Morris Jobke's avatar
Morris Jobke committed
340
	 * 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
341
	 *
342
343
344
	 * @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
345
	 * @return string the path
Jakob Sack's avatar
Jakob Sack committed
346
	 */
Morris Jobke's avatar
Morris Jobke committed
347
	private static function generatePath($application, $directory, $file) {
348
		if (is_null($file)) {
Jakob Sack's avatar
Jakob Sack committed
349
350
351
			$file = $application;
			$application = "";
		}
352
		if (!empty($application)) {
Morris Jobke's avatar
Morris Jobke committed
353
			return "$application/$directory/$file";
kondou's avatar
kondou committed
354
		} else {
Morris Jobke's avatar
Morris Jobke committed
355
			return "$directory/$file";
Jakob Sack's avatar
Jakob Sack committed
356
357
358
		}
	}

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

	/**
	 * 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) {
385
386
387
388
		$path = OC_Util::generatePath($application, 'vendor', $file);
		if (!in_array($path, self::$scripts)) {
			self::$scripts[] = $path;
		}
Morris Jobke's avatar
Morris Jobke committed
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)) {
399
			$languageCode = \OC_L10N::findLanguage($application);
400
401
		}
		if (!empty($application)) {
402
			$path = "$application/l10n/$languageCode";
403
		} else {
404
405
406
407
			$path = "l10n/$languageCode";
		}
		if (!in_array($path, self::$scripts)) {
			self::$scripts[] = $path;
408
409
410
		}
	}

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

	/**
	 * 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) {
433
434
435
436
		$path = OC_Util::generatePath($application, 'vendor', $file);
		if (!in_array($path, self::$styles)) {
			self::$styles[] = $path;
		}
Jakob Sack's avatar
Jakob Sack committed
437
438
439
	}

	/**
440
	 * Add a custom element to the header
441
442
	 * 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
443
	 * @param string $tag tag name of the element
444
	 * @param array $attributes array of attributes for the element
Jakob Sack's avatar
Jakob Sack committed
445
446
	 * @param string $text the text content for the element
	 */
447
	public static function addHeader($tag, $attributes, $text=null) {
kondou's avatar
kondou committed
448
		self::$headers[] = array(
449
450
451
			'tag' => $tag,
			'attributes' => $attributes,
			'text' => $text
kondou's avatar
kondou committed
452
		);
Jakob Sack's avatar
Jakob Sack committed
453
454
	}

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

		/** @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
476
477
478
	}

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

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

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

kondou's avatar
kondou committed
499
		$webServerRestart = false;
500
		$setup = new \OC\Setup($config, \OC::$server->getIniWrapper(), \OC::$server->getL10N('lib'), new \OC_Defaults());
501
502
		$availableDatabases = $setup->getSupportedDatabases();
		if (empty($availableDatabases)) {
kondou's avatar
kondou committed
503
			$errors[] = array(
504
505
				'error' => $l->t('No database drivers (sqlite, mysql, or postgresql) installed.'),
				'hint' => '' //TODO: sane hint
kondou's avatar
kondou committed
506
507
			);
			$webServerRestart = true;
Jakob Sack's avatar
Jakob Sack committed
508
509
		}

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

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

563
		if (!OC_Util::isSetLocaleWorking()) {
564
			$errors[] = array(
565
				'error' => $l->t('Setting locale to %s failed',
566
567
					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
568
				'hint' => $l->t('Please install one of these locales on your system and restart your webserver.')
569
570
571
			);
		}

Lukas Reschke's avatar
Lukas Reschke committed
572
573
574
575
		// Contains the dependencies that should be checked against
		// classes = class_exists
		// functions = function_exists
		// defined = defined
576
		// ini = ini_get
Lukas Reschke's avatar
Lukas Reschke committed
577
		// If the dependency is not found the missing module name is shown to the EndUser
Lukas Reschke's avatar
Lukas Reschke committed
578
579
		// When adding new checks always verify that they pass on Travis as well
		// for ini settings, see https://github.com/owncloud/administration/blob/master/travis-ci/custom.ini
Lukas Reschke's avatar
Lukas Reschke committed
580
581
582
583
		$dependencies = array(
			'classes' => array(
				'ZipArchive' => 'zip',
				'DOMDocument' => 'dom',
Lukas Reschke's avatar
Lukas Reschke committed
584
				'XMLWriter' => 'XMLWriter'
Lukas Reschke's avatar
Lukas Reschke committed
585
			),
Lukas Reschke's avatar
Lukas Reschke committed
586
			'functions' => [
Lukas Reschke's avatar
Lukas Reschke committed
587
588
589
590
591
592
593
				'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
594
				'simplexml_load_string' => 'SimpleXML',
Lukas Reschke's avatar
Lukas Reschke committed
595
596
597
				'hash' => 'HASH Message Digest Framework',
				'curl_init' => 'cURL',
			],
Lukas Reschke's avatar
Lukas Reschke committed
598
599
			'defined' => array(
				'PDO::ATTR_DRIVER_NAME' => 'PDO'
600
601
602
603
			),
			'ini' => [
				'mbstring.func_overload' => 0,
				'output_buffering' => false,
Lukas Reschke's avatar
Lukas Reschke committed
604
				'default_charset' => 'UTF-8',
605
			],
Lukas Reschke's avatar
Lukas Reschke committed
606
607
		);
		$missingDependencies = array();
608
		$invalidIniSettings = [];
609
		$moduleHint = $l->t('Please ask your server administrator to install the module.');
Lukas Reschke's avatar
Lukas Reschke committed
610

611
612
613
614
615
616
617
618
619
620
621
		/**
		 * FIXME: The dependency check does not work properly on HHVM on the moment
		 *        and prevents installation. Once HHVM is more compatible with our
		 *        approach to check for these values we should re-enable those
		 *        checks.
		 */
		if (!self::runningOnHhvm()) {
			foreach ($dependencies['classes'] as $class => $module) {
				if (!class_exists($class)) {
					$missingDependencies[] = $module;
				}
Lukas Reschke's avatar
Lukas Reschke committed
622
			}
623
624
625
			foreach ($dependencies['functions'] as $function => $module) {
				if (!function_exists($function)) {
					$missingDependencies[] = $module;
626
627
				}
			}
628
629
630
			foreach ($dependencies['defined'] as $defined => $module) {
				if (!defined($defined)) {
					$missingDependencies[] = $module;
631
632
				}
			}
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
			foreach ($dependencies['ini'] as $setting => $expected) {
				$iniWrapper = \OC::$server->getIniWrapper();
				if (is_bool($expected)) {
					if ($iniWrapper->getBool($setting) !== $expected) {
						$invalidIniSettings[] = [$setting, $expected];
					}
				}
				if (is_int($expected)) {
					if ($iniWrapper->getNumeric($setting) !== $expected) {
						$invalidIniSettings[] = [$setting, $expected];
					}
				}
				if (is_string($expected)) {
					if (strtolower($iniWrapper->getString($setting)) !== strtolower($expected)) {
						$invalidIniSettings[] = [$setting, $expected];
					}
Lukas Reschke's avatar
Lukas Reschke committed
649
650
				}
			}
651
		}
Lukas Reschke's avatar
Lukas Reschke committed
652
653

		foreach($missingDependencies as $missingDependency) {
kondou's avatar
kondou committed
654
			$errors[] = array(
Lukas Reschke's avatar
Lukas Reschke committed
655
				'error' => $l->t('PHP module %s not installed.', array($missingDependency)),
656
				'hint' => $moduleHint
kondou's avatar
kondou committed
657
658
			);
			$webServerRestart = true;
659
		}
660
		foreach($invalidIniSettings as $setting) {
661
662
663
			if(is_bool($setting[1])) {
				$setting[1] = ($setting[1]) ? 'on' : 'off';
			}
664
			$errors[] = [
665
				'error' => $l->t('PHP setting "%s" is not set to "%s".', [$setting[0], var_export($setting[1], true)]),
666
667
668
669
				'hint' =>  $l->t('Adjusting this setting in php.ini will make ownCloud run again')
			];
			$webServerRestart = true;
		}
Lukas Reschke's avatar
Lukas Reschke committed
670

671
		if (version_compare(phpversion(), '5.4.0', '<')) {
kondou's avatar
kondou committed
672
			$errors[] = array(
673
				'error' => $l->t('PHP %s or higher is required.', '5.4.0'),
674
675
				'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
676
677
			);
			$webServerRestart = true;
678
		}
679
680
681
682
683

		/**
		 * 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
		 *
684
685
686
687
		 * 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.
		 *
688
689
690
		 * @link https://github.com/owncloud/core/issues/13592
		 */
		if(version_compare(phpversion(), '5.6.0', '>=') &&
691
			!self::runningOnHhvm() &&
692
693
			\OC::$server->getIniWrapper()->getNumeric('always_populate_raw_post_data') !== -1) {
			$errors[] = array(
Morris Jobke's avatar
Morris Jobke committed
694
695
				'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')
696
697
698
			);
		}

699
700
		if (!self::isAnnotationsWorking()) {
			$errors[] = array(
Morris Jobke's avatar
Morris Jobke committed
701
702
				'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.')
703
704
			);
		}
705

706
		if (!\OC::$CLI && $webServerRestart) {
kondou's avatar
kondou committed
707
			$errors[] = array(
708
709
				'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
710
			);
Frank Karlitschek's avatar
Frank Karlitschek committed
711
		}
Jakob Sack's avatar
Jakob Sack committed
712

713
714
		$errors = array_merge($errors, self::checkDatabaseVersion());

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

Jakob Sack's avatar
Jakob Sack committed
718
719
		return $errors;
	}
720

721
722
	/**
	 * Check the database version
723
	 *
724
725
726
	 * @return array errors array
	 */
	public static function checkDatabaseVersion() {
727
		$l = \OC::$server->getL10N('lib');
728
729
730
731
732
733
734
735
736
737
738
		$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(
739
740
							'error' => $l->t('PostgreSQL >= 9 required'),
							'hint' => $l->t('Please upgrade your database version')
741
742
743
						);
					}
				}
Thomas Müller's avatar
Thomas Müller committed
744
			} catch (\Doctrine\DBAL\DBALException $e) {
745
				\OCP\Util::logException('core', $e);
746
				$errors[] = array(
747
748
					'error' => $l->t('Error occurred while checking PostgreSQL version'),
					'hint' => $l->t('Please make sure you have PostgreSQL >= 9 or'
749
						. ' check the logs for more information about the error')
750
751
752
753
754
755
756
				);
			}
		}
		return $errors;
	}


757
	/**
758
	 * check if there are still some encrypted files stored
759
	 *
760
761
762
763
764
765
766
	 * @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());
767
768
769
			$keysPath = '/files_encryption/keys';
			if ($view->is_dir($keysPath)) {
				$dircontent = $view->getDirectoryContent($keysPath);
770
771
772
				if (!empty($dircontent)) {
					$encryptedFiles = true;
				}
773
774
			}
		}
775

776
777
		return $encryptedFiles;
	}
778

779
	/**
780
	 * check if a backup from the encryption keys exists
781
	 *
782
783
784
785
786
787
788
	 * @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());
789
			$backupPath = '/files_encryption/backup.decryptAll';
790
791
792
793
794
795
796
797
798
799
800
			if ($view->is_dir($backupPath)) {
				$dircontent = $view->getDirectoryContent($backupPath);
				if (!empty($dircontent)) {
					$backupExists = true;
				}
			}
		}

		return $backupExists;
	}

801
	/**
802
	 * Check for correct file permissions of data directory
803
	 *
804
	 * @param string $dataDirectory
kondou's avatar
kondou committed
805
806
	 * @return array arrays with error messages and hints
	 */
807
	public static function checkDataDirectoryPermissions($dataDirectory) {
808
		$l = \OC::$server->getL10N('lib');
809
		$errors = array();
kondou's avatar
kondou committed
810
		if (self::runningOnWindows()) {
811
812
			//TODO: permissions checks for windows hosts
		} else {
813
			$permissionsModHint = $l->t('Please change the permissions to 0770 so that the directory'
814
				. ' cannot be listed by other users.');
kondou's avatar
kondou committed
815
816
			$perms = substr(decoct(@fileperms($dataDirectory)), -3);
			if (substr($perms, -1) != '0') {
817
				chmod($dataDirectory, 0770);
818
				clearstatcache();
kondou's avatar
kondou committed
819
820
821
				$perms = substr(decoct(@fileperms($dataDirectory)), -3);
				if (substr($perms, 2, 1) != '0') {
					$errors[] = array(
822
						'error' => $l->t('Data directory (%s) is readable by other users', array($dataDirectory)),
kondou's avatar
kondou committed
823
824
						'hint' => $permissionsModHint
					);
825
826
827
828
829
830
				}
			}
		}
		return $errors;
	}

831
832
833
834
835
836
837
838
	/**
	 * 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) {
839
		$l = \OC::$server->getL10N('lib');
840
		$errors = array();
841
		if (!file_exists($dataDirectory . '/.ocdata')) {
842
			$errors[] = array(
843
844
845
				'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.')
846
847
848
849
850
			);
		}
		return $errors;
	}

kondou's avatar
kondou committed
851
	/**
Lukas Reschke's avatar
Lukas Reschke committed
852
	 * @param array $errors
853
	 * @param string[] $messages
kondou's avatar
kondou committed
854
	 */
855
	public static function displayLoginPage($errors = array(), $messages = []) {
856
		$parameters = array();
857
		foreach ($errors as $value) {
858
859
			$parameters[$value] = true;
		}
860
		$parameters['messages'] = $messages;
861
862
		if (!empty($_REQUEST['user'])) {
			$parameters["username"] = $_REQUEST['user'];
863
			$parameters['user_autofocus'] = false;
Bart Visscher's avatar
Bart Visscher committed
864
865
		} else {
			$parameters["username"] = '';
866
			$parameters['user_autofocus'] = true;
867
		}
868
		if (isset($_REQUEST['redirect_url'])) {
869
			$parameters['redirect_url'] = $_REQUEST['redirect_url'];
Thomas Mueller's avatar
Thomas Mueller committed
870
		}
871
872

		$parameters['alt_login'] = OC_App::getAlternativeLogIns();
873
		$parameters['rememberLoginAllowed'] = self::rememberLoginAllowed();
874
875
		OC_Template::printGuestPage("", "login", $parameters);
	}
876
877


878
	/**
879
	 * Check if the app is enabled, redirects to home if not
880
	 *
881
	 * @param string $app
kondou's avatar
kondou committed
882
	 * @return void
Thomas Mueller's avatar
Thomas Mueller committed
883
	 */
Thomas Mueller's avatar
Thomas Mueller committed
884
	public static function checkAppEnabled($app) {
885
886
		if (!OC_App::isEnabled($app)) {
			header('Location: ' . OC_Helper::linkToAbsolute('', 'index.php'));
887
888
889
890
			exit();
		}
	}

891
	/**
Thomas Mueller's avatar
Thomas Mueller committed
892
893
	 * Check if the user is logged in, redirects to home if not. With
	 * redirect URL parameter to the request URI.
894
	 *
kondou's avatar
kondou committed
895
	 * @return void
Thomas Mueller's avatar
Thomas Mueller committed
896
	 */
Thomas Mueller's avatar
Thomas Mueller committed
897
	public static function checkLoggedIn() {
898
		// Check if we are a user