util.php 40.3 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
		OC_Util::loadVersion();
289
		return \OC::$server->getSession()->get('OC_Edition');
290
291
292
293
294
295
296
297
	}

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

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

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

Jakob Sack's avatar
Jakob Sack committed
333
	/**
Morris Jobke's avatar
Morris Jobke committed
334
	 * 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
335
	 *
Morris Jobke's avatar
Morris Jobke committed
336
337
338
339
	 * @param $application application to get the files from
	 * @param $directory directory withing this application (css, js, vendor, etc)
	 * @param $file the file inside of the above folder
	 * @return string the path
Jakob Sack's avatar
Jakob Sack committed
340
	 */
Morris Jobke's avatar
Morris Jobke committed
341
	private static function generatePath($application, $directory, $file) {
342
		if (is_null($file)) {
Jakob Sack's avatar
Jakob Sack committed
343
344
345
			$file = $application;
			$application = "";
		}
346
		if (!empty($application)) {
Morris Jobke's avatar
Morris Jobke committed
347
			return "$application/$directory/$file";
kondou's avatar
kondou committed
348
		} else {
Morris Jobke's avatar
Morris Jobke committed
349
			return "$directory/$file";
Jakob Sack's avatar
Jakob Sack committed
350
351
352
		}
	}

Morris Jobke's avatar
Morris Jobke committed
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
	/**
	 * add a javascript file
	 *
	 * @param string $application application id
	 * @param string|null $file filename
	 * @return void
	 */
	public static function addScript($application, $file = null) {
		self::$scripts[] = OC_Util::generatePath($application, 'js', $file);
	}

	/**
	 * 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) {
		self::$scripts[] = OC_Util::generatePath($application, 'vendor', $file);
	}

375
376
377
378
379
380
381
382
383
384
385
386
387
388
	/**
	 * 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)) {
			self::$scripts[] = "$application/l10n/$languageCode";
		} else {
389
			self::$scripts[] = "l10n/$languageCode";
390
391
392
		}
	}

Jakob Sack's avatar
Jakob Sack committed
393
	/**
394
	 * add a css file
Jakob Sack's avatar
Jakob Sack committed
395
	 *
396
	 * @param string $application application id
397
	 * @param string|null $file filename
kondou's avatar
kondou committed
398
	 * @return void
Jakob Sack's avatar
Jakob Sack committed
399
	 */
400
	public static function addStyle($application, $file = null) {
Morris Jobke's avatar
Morris Jobke committed
401
402
403
404
405
406
407
408
409
410
411
412
		self::$styles[] = OC_Util::generatePath($application, 'css', $file);
	}

	/**
	 * 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) {
		self::$styles[] = OC_Util::generatePath($application, 'vendor', $file);
Jakob Sack's avatar
Jakob Sack committed
413
414
415
	}

	/**
416
	 * Add a custom element to the header
417
418
	 * 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
419
	 * @param string $tag tag name of the element
420
	 * @param array $attributes array of attributes for the element
Jakob Sack's avatar
Jakob Sack committed
421
422
	 * @param string $text the text content for the element
	 */
423
	public static function addHeader($tag, $attributes, $text=null) {
kondou's avatar
kondou committed
424
		self::$headers[] = array(
425
426
427
			'tag' => $tag,
			'attributes' => $attributes,
			'text' => $text
kondou's avatar
kondou committed
428
		);
Jakob Sack's avatar
Jakob Sack committed
429
430
	}

Robin Appelman's avatar
Robin Appelman committed
431
	/**
432
	 * formats a timestamp in the "right" way
Robin Appelman's avatar
Robin Appelman committed
433
	 *
kondou's avatar
kondou committed
434
435
	 * @param int $timestamp
	 * @param bool $dateOnly option to omit time from the result
436
	 * @param DateTimeZone|string $timeZone where the given timestamp shall be converted to
kondou's avatar
kondou committed
437
	 * @return string timestamp
438
	 * @description adjust to clients timezone if we know it
Robin Appelman's avatar
Robin Appelman committed
439
	 */
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
	public static function formatDate($timestamp, $dateOnly = false, $timeZone = null) {
		if (is_null($timeZone)) {
			if (\OC::$server->getSession()->exists('timezone')) {
				$systemTimeZone = intval(date('O'));
				$systemTimeZone = (round($systemTimeZone / 100, 0) * 60) + ($systemTimeZone % 100);
				$clientTimeZone = \OC::$server->getSession()->get('timezone') * 60;
				$offset = $clientTimeZone - $systemTimeZone;
				$timestamp = $timestamp + $offset * 60;
			}
		} else {
			if (!$timeZone instanceof DateTimeZone) {
				$timeZone = new DateTimeZone($timeZone);
			}
			$dt = new DateTime("@$timestamp");
			$offset = $timeZone->getOffset($dt);
			$timestamp += $offset;
Robin Appelman's avatar
Robin Appelman committed
456
		}
457
		$l = \OC::$server->getL10N('lib');
458
		return $l->l($dateOnly ? 'date' : 'datetime', $timestamp);
Jakob Sack's avatar
Jakob Sack committed
459
460
461
	}

	/**
462
	 * check if the current server configuration is suitable for ownCloud
463
	 *
464
	 * @param \OCP\IConfig $config
Jakob Sack's avatar
Jakob Sack committed
465
466
	 * @return array arrays with error messages and hints
	 */
Lukas Reschke's avatar
Lukas Reschke committed
467
	public static function checkServer(\OCP\IConfig $config) {
468
		$l = \OC::$server->getL10N('lib');
469
		$errors = array();
470
		$CONFIG_DATADIRECTORY = $config->getSystemValue('datadirectory', OC::$SERVERROOT . '/data');
471

472
		if (!self::needUpgrade($config) && $config->getSystemValue('installed', false)) {
473
474
475
476
			// this check needs to be done every time
			$errors = self::checkDataDirectoryValidity($CONFIG_DATADIRECTORY);
		}

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

kondou's avatar
kondou committed
482
		$webServerRestart = false;
483
484
485
		$setup = new OC_Setup($config);
		$availableDatabases = $setup->getSupportedDatabases();
		if (empty($availableDatabases)) {
kondou's avatar
kondou committed
486
			$errors[] = array(
487
488
				'error' => $l->t('No database drivers (sqlite, mysql, or postgresql) installed.'),
				'hint' => '' //TODO: sane hint
kondou's avatar
kondou committed
489
490
			);
			$webServerRestart = true;
Jakob Sack's avatar
Jakob Sack committed
491
492
		}

Thomas Müller's avatar
Thomas Müller committed
493
		//common hint for all file permissions error messages
494
		$permissionsHint = $l->t('Permissions can usually be fixed by '
495
496
			. '%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
497

498
		// Check if config folder is writable.
499
		if (!is_writable(OC::$configDir) or !is_readable(OC::$configDir)) {
500
			$errors[] = array(
501
502
				'error' => $l->t('Cannot write into "config" directory'),
				'hint' => $l->t('This can usually be fixed by '
503
504
505
					. '%sgiving the webserver write access to the config directory%s.',
					array('<a href="' . \OC_Helper::linkToDocs('admin-dir_permissions') . '" target="_blank">', '</a>'))
			);
506
507
		}

508
		// Check if there is a writable install folder.
509
		if ($config->getSystemValue('appstoreenabled', true)) {
510
			if (OC_App::getInstallPath() === null
Bart Visscher's avatar
Bart Visscher committed
511
				|| !is_writable(OC_App::getInstallPath())
512
513
				|| !is_readable(OC_App::getInstallPath())
			) {
514
				$errors[] = array(
515
516
					'error' => $l->t('Cannot write into "apps" directory'),
					'hint' => $l->t('This can usually be fixed by '
517
518
519
520
						. '%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>'))
				);
521
			}
522
523
		}
		// Create root dir.
524
525
526
527
528
529
530
531
532
533
534
535
536
537
		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)) {
538
				$errors[] = array(
539
540
					'error' => 'Data directory (' . $CONFIG_DATADIRECTORY . ') not writable by ownCloud',
					'hint' => $permissionsHint
541
				);
542
543
			} else {
				$errors = array_merge($errors, self::checkDataDirectoryPermissions($CONFIG_DATADIRECTORY));
544
			}
Jakob Sack's avatar
Jakob Sack committed
545
		}
kondou's avatar
kondou committed
546

547
		if (!OC_Util::isSetLocaleWorking()) {
548
			$errors[] = array(
549
				'error' => $l->t('Setting locale to %s failed',
550
551
					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
552
				'hint' => $l->t('Please install one of these locales on your system and restart your webserver.')
553
554
555
			);
		}

Lukas Reschke's avatar
Lukas Reschke committed
556
557
558
559
560
561
562
563
564
		// 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
565
				'XMLWriter' => 'XMLWriter'
Lukas Reschke's avatar
Lukas Reschke committed
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
			),
			'functions' => array(
				'xml_parser_create' => 'libxml',
				'mb_detect_encoding' => 'mb multibyte',
				'ctype_digit' => 'ctype',
				'json_encode' => 'JSON',
				'gd_info' => 'GD',
				'gzencode' => 'zlib',
				'iconv' => 'iconv',
				'simplexml_load_string' => 'SimpleXML'
			),
			'defined' => array(
				'PDO::ATTR_DRIVER_NAME' => 'PDO'
			)
		);
		$missingDependencies = array();
582
		$moduleHint = $l->t('Please ask your server administrator to install the module.');
Lukas Reschke's avatar
Lukas Reschke committed
583
584
585
586
587

		foreach ($dependencies['classes'] as $class => $module) {
			if (!class_exists($class)) {
				$missingDependencies[] = $module;
			}
588
		}
Lukas Reschke's avatar
Lukas Reschke committed
589
590
591
592
		foreach ($dependencies['functions'] as $function => $module) {
			if (!function_exists($function)) {
				$missingDependencies[] = $module;
			}
Frank Karlitschek's avatar
Frank Karlitschek committed
593
		}
Lukas Reschke's avatar
Lukas Reschke committed
594
595
596
597
		foreach ($dependencies['defined'] as $defined => $module) {
			if (!defined($defined)) {
				$missingDependencies[] = $module;
			}
Lukas Reschke's avatar
Lukas Reschke committed
598
		}
Lukas Reschke's avatar
Lukas Reschke committed
599
600

		foreach($missingDependencies as $missingDependency) {
kondou's avatar
kondou committed
601
			$errors[] = array(
Lukas Reschke's avatar
Lukas Reschke committed
602
				'error' => $l->t('PHP module %s not installed.', array($missingDependency)),
603
				'hint' => $moduleHint
kondou's avatar
kondou committed
604
605
			);
			$webServerRestart = true;
606
		}
Lukas Reschke's avatar
Lukas Reschke committed
607

608
		if (version_compare(phpversion(), '5.4.0', '<')) {
kondou's avatar
kondou committed
609
			$errors[] = array(
610
				'error' => $l->t('PHP %s or higher is required.', '5.4.0'),
611
612
				'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
613
614
			);
			$webServerRestart = true;
615
		}
616
617
618
		if (((strtolower(@ini_get('safe_mode')) == 'on')
			|| (strtolower(@ini_get('safe_mode')) == 'yes')
			|| (strtolower(@ini_get('safe_mode')) == 'true')
619
620
			|| (ini_get("safe_mode") == 1))
		) {
kondou's avatar
kondou committed
621
			$errors[] = array(
622
623
624
				'error' => $l->t('PHP Safe Mode is enabled. ownCloud requires that it is disabled to work properly.'),
				'hint' => $l->t('PHP Safe Mode is a deprecated and mostly useless setting that should be disabled. '
					. 'Please ask your server administrator to disable it in php.ini or in your webserver config.')
kondou's avatar
kondou committed
625
626
			);
			$webServerRestart = true;
Frank Karlitschek's avatar
Frank Karlitschek committed
627
		}
628
		if (get_magic_quotes_gpc() == 1) {
kondou's avatar
kondou committed
629
			$errors[] = array(
630
631
632
				'error' => $l->t('Magic Quotes is enabled. ownCloud requires that it is disabled to work properly.'),
				'hint' => $l->t('Magic Quotes is a deprecated and mostly useless setting that should be disabled. '
					. 'Please ask your server administrator to disable it in php.ini or in your webserver config.')
kondou's avatar
kondou committed
633
634
			);
			$webServerRestart = true;
Bart Visscher's avatar
Bart Visscher committed
635
		}
636
637
		if (!self::isAnnotationsWorking()) {
			$errors[] = array(
638
639
				'error' => 'PHP is apparently setup to strip inline doc blocks. This will make several core apps inaccessible.',
				'hint' => 'This is probably caused by a cache/accelerator such as Zend OPcache or eAccelerator.'
640
641
			);
		}
642

643
		if ($webServerRestart) {
kondou's avatar
kondou committed
644
			$errors[] = array(
645
646
				'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
647
			);
Frank Karlitschek's avatar
Frank Karlitschek committed
648
		}
Jakob Sack's avatar
Jakob Sack committed
649

650
651
		$errors = array_merge($errors, self::checkDatabaseVersion());

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

Jakob Sack's avatar
Jakob Sack committed
655
656
		return $errors;
	}
657

658
659
	/**
	 * Check the database version
660
	 *
661
662
663
	 * @return array errors array
	 */
	public static function checkDatabaseVersion() {
664
		$l = \OC::$server->getL10N('lib');
665
666
667
668
669
670
671
672
673
674
675
		$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(
676
677
							'error' => $l->t('PostgreSQL >= 9 required'),
							'hint' => $l->t('Please upgrade your database version')
678
679
680
						);
					}
				}
Thomas Müller's avatar
Thomas Müller committed
681
			} catch (\Doctrine\DBAL\DBALException $e) {
682
				\OCP\Util::logException('core', $e);
683
				$errors[] = array(
684
685
					'error' => $l->t('Error occurred while checking PostgreSQL version'),
					'hint' => $l->t('Please make sure you have PostgreSQL >= 9 or'
686
						. ' check the logs for more information about the error')
687
688
689
690
691
692
693
				);
			}
		}
		return $errors;
	}


694
	/**
695
	 * check if there are still some encrypted files stored
696
	 *
697
698
699
700
701
702
703
	 * @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());
704
705
706
707
708
709
			$keyfilePath = '/files_encryption/keyfiles';
			if ($view->is_dir($keyfilePath)) {
				$dircontent = $view->getDirectoryContent($keyfilePath);
				if (!empty($dircontent)) {
					$encryptedFiles = true;
				}
710
711
			}
		}
712

713
714
		return $encryptedFiles;
	}
715

716
	/**
717
	 * check if a backup from the encryption keys exists
718
	 *
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
	 * @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());
			$backupPath = '/files_encryption/keyfiles.backup';
			if ($view->is_dir($backupPath)) {
				$dircontent = $view->getDirectoryContent($backupPath);
				if (!empty($dircontent)) {
					$backupExists = true;
				}
			}
		}

		return $backupExists;
	}

738
	/**
739
	 * Check for correct file permissions of data directory
740
	 *
741
	 * @param string $dataDirectory
kondou's avatar
kondou committed
742
743
	 * @return array arrays with error messages and hints
	 */
744
	public static function checkDataDirectoryPermissions($dataDirectory) {
745
		$l = \OC::$server->getL10N('lib');
746
		$errors = array();
kondou's avatar
kondou committed
747
		if (self::runningOnWindows()) {
748
749
			//TODO: permissions checks for windows hosts
		} else {
750
			$permissionsModHint = $l->t('Please change the permissions to 0770 so that the directory'
751
				. ' cannot be listed by other users.');
kondou's avatar
kondou committed
752
753
			$perms = substr(decoct(@fileperms($dataDirectory)), -3);
			if (substr($perms, -1) != '0') {
754
				chmod($dataDirectory, 0770);
755
				clearstatcache();
kondou's avatar
kondou committed
756
757
758
				$perms = substr(decoct(@fileperms($dataDirectory)), -3);
				if (substr($perms, 2, 1) != '0') {
					$errors[] = array(
759
						'error' => $l->t('Data directory (%s) is readable by other users', array($dataDirectory)),
kondou's avatar
kondou committed
760
761
						'hint' => $permissionsModHint
					);
762
763
764
765
766
767
				}
			}
		}
		return $errors;
	}

768
769
770
771
772
773
774
775
	/**
	 * 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) {
776
		$l = \OC::$server->getL10N('lib');
777
		$errors = array();
778
		if (!file_exists($dataDirectory . '/.ocdata')) {
779
			$errors[] = array(
780
781
782
				'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.')
783
784
785
786
787
			);
		}
		return $errors;
	}

kondou's avatar
kondou committed
788
	/**
Lukas Reschke's avatar
Lukas Reschke committed
789
	 * @param array $errors
kondou's avatar
kondou committed
790
	 */
791
	public static function displayLoginPage($errors = array()) {
792
		$parameters = array();
793
		foreach ($errors as $value) {
794
795
			$parameters[$value] = true;
		}
796
797
		if (!empty($_REQUEST['user'])) {
			$parameters["username"] = $_REQUEST['user'];
798
			$parameters['user_autofocus'] = false;
Bart Visscher's avatar
Bart Visscher committed
799
800
		} else {
			$parameters["username"] = '';
801
			$parameters['user_autofocus'] = true;
802
		}
803
		if (isset($_REQUEST['redirect_url'])) {
kondou's avatar
kondou committed
804
805
			$redirectUrl = $_REQUEST['redirect_url'];
			$parameters['redirect_url'] = urlencode($redirectUrl);
Thomas Mueller's avatar
Thomas Mueller committed
806
		}
807
808

		$parameters['alt_login'] = OC_App::getAlternativeLogIns();
809
		$parameters['rememberLoginAllowed'] = self::rememberLoginAllowed();
810
811
		OC_Template::printGuestPage("", "login", $parameters);
	}
812
813


814
	/**
815
	 * Check if the app is enabled, redirects to home if not
816
	 *
817
	 * @param string $app
kondou's avatar
kondou committed
818
	 * @return void
Thomas Mueller's avatar
Thomas Mueller committed
819
	 */
Thomas Mueller's avatar
Thomas Mueller committed
820
	public static function checkAppEnabled($app) {
821
822
		if (!OC_App::isEnabled($app)) {
			header('Location: ' . OC_Helper::linkToAbsolute('', 'index.php'));
823
824
825
826
			exit();
		}
	}

827
	/**
Thomas Mueller's avatar
Thomas Mueller committed
828
829
	 * Check if the user is logged in, redirects to home if not. With
	 * redirect URL parameter to the request URI.
830
	 *
kondou's avatar
kondou committed
831
	 * @return void
Thomas Mueller's avatar
Thomas Mueller committed
832
	 */
Thomas Mueller's avatar
Thomas Mueller committed
833
	public static function checkLoggedIn() {
834
		// Check if we are a user
835
836
837
838
		if (!OC_User::isLoggedIn()) {
			header('Location: ' . OC_Helper::linkToAbsolute('', 'index.php',
					array('redirect_url' => OC_Request::requestUri())
				));
839
840
841
842
843
			exit();
		}
	}

	/**
844
	 * Check if the user is a admin, redirects to home if not
845
	 *
kondou's avatar
kondou committed
846
	 * @return void
Thomas Mueller's avatar
Thomas Mueller committed
847
	 */
Thomas Mueller's avatar
Thomas Mueller committed
848
	public static function checkAdminUser() {
849
		OC_Util::checkLoggedIn();
850
851
		if (!OC_User::isAdminUser(OC_User::getUser())) {
			header('Location: ' . OC_Helper::linkToAbsolute('', 'index.php'));
852
853
854
855
			exit();
		}
	}

Georg Ehrke's avatar
Georg Ehrke committed
856
	/**
857
	 * Check if it is allowed to remember login.
858
859
	 *
	 * @note Every app can set 'rememberlogin' to 'false' to disable the remember login feature
860
861
862
863
	 *
	 * @return bool
	 */
	public static function rememberLoginAllowed() {
864
865
866
867
868
869
870
871
872
873
874

		$apps = OC_App::getEnabledApps();

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

		}
		return true;
875
876
	}

Georg Ehrke's avatar
Georg Ehrke committed
877
	/**
878
	 * Check if the user is a subadmin, redirects to home if not
879
	 *
880
	 * @return null|boolean $groups where the current user is subadmin
Thomas Mueller's avatar
Thomas Mueller committed
881
	 */
Thomas Mueller's avatar
Thomas Mueller committed
882
	public static function checkSubAdminUser() {
883
		OC_Util::checkLoggedIn();
884
885
		if (!OC_SubAdmin::isSubAdmin(OC_User::getUser())) {
			header('Location: ' . OC_Helper::linkToAbsolute('', 'index.php'));
Georg Ehrke's avatar
Georg Ehrke committed
886
887
			exit();
		}
Georg Ehrke's avatar
Georg Ehrke committed
888
		return true;
Georg Ehrke's avatar
Georg Ehrke committed
889
890
	}

891
	/**
892
893
894
895
896
	 * Returns the URL of the default page
	 * based on the system configuration and
	 * the apps visible for the current user
	 *
	 * @return string URL
Thomas Mueller's avatar
Thomas Mueller committed
897
	 */
898
	public static function getDefaultPageUrl() {
899
		$urlGenerator = \OC::$server->getURLGenerator();
Lukas Reschke's avatar
Lukas Reschke committed
900
901
902
903
		// Deny the redirect if the URL contains a @
		// This prevents unvalidated redirects like ?redirect_url=:user@domain.com
		if (isset($_REQUEST['redirect_url']) && strpos($_REQUEST['redirect_url'], '@') === false) {
			$location = $urlGenerator->getAbsoluteURL(urldecode($_REQUEST['redirect_url']));
kondou's avatar
kondou committed