Completed
Pull Request — master (#5833)
by Tobia
13:54
created
settings/ajax/disableapp.php 2 patches
Indentation   +7 added lines, -7 removed lines patch added patch discarded remove patch
@@ -26,19 +26,19 @@
 block discarded – undo
26 26
 
27 27
 $lastConfirm = (int) \OC::$server->getSession()->get('last-password-confirm');
28 28
 if ($lastConfirm < (time() - 30 * 60 + 15)) { // allow 15 seconds delay
29
-	$l = \OC::$server->getL10N('core');
30
-	OC_JSON::error(array( 'data' => array( 'message' => $l->t('Password confirmation is required'))));
31
-	exit();
29
+    $l = \OC::$server->getL10N('core');
30
+    OC_JSON::error(array( 'data' => array( 'message' => $l->t('Password confirmation is required'))));
31
+    exit();
32 32
 }
33 33
 
34 34
 if (!array_key_exists('appid', $_POST)) {
35
-	OC_JSON::error();
36
-	exit;
35
+    OC_JSON::error();
36
+    exit;
37 37
 }
38 38
 
39 39
 $appIds = (array)$_POST['appid'];
40 40
 foreach($appIds as $appId) {
41
-	$appId = OC_App::cleanAppId($appId);
42
-	\OC::$server->getAppManager()->disableApp($appId);
41
+    $appId = OC_App::cleanAppId($appId);
42
+    \OC::$server->getAppManager()->disableApp($appId);
43 43
 }
44 44
 OC_JSON::success();
Please login to merge, or discard this patch.
Spacing   +3 added lines, -3 removed lines patch added patch discarded remove patch
@@ -27,7 +27,7 @@  discard block
 block discarded – undo
27 27
 $lastConfirm = (int) \OC::$server->getSession()->get('last-password-confirm');
28 28
 if ($lastConfirm < (time() - 30 * 60 + 15)) { // allow 15 seconds delay
29 29
 	$l = \OC::$server->getL10N('core');
30
-	OC_JSON::error(array( 'data' => array( 'message' => $l->t('Password confirmation is required'))));
30
+	OC_JSON::error(array('data' => array('message' => $l->t('Password confirmation is required'))));
31 31
 	exit();
32 32
 }
33 33
 
@@ -36,8 +36,8 @@  discard block
 block discarded – undo
36 36
 	exit;
37 37
 }
38 38
 
39
-$appIds = (array)$_POST['appid'];
40
-foreach($appIds as $appId) {
39
+$appIds = (array) $_POST['appid'];
40
+foreach ($appIds as $appId) {
41 41
 	$appId = OC_App::cleanAppId($appId);
42 42
 	\OC::$server->getAppManager()->disableApp($appId);
43 43
 }
Please login to merge, or discard this patch.
lib/private/legacy/app.php 1 patch
Indentation   +1181 added lines, -1181 removed lines patch added patch discarded remove patch
@@ -61,1185 +61,1185 @@
 block discarded – undo
61 61
  * upgrading and removing apps.
62 62
  */
63 63
 class OC_App {
64
-	static private $appVersion = [];
65
-	static private $adminForms = array();
66
-	static private $personalForms = array();
67
-	static private $appInfo = array();
68
-	static private $appTypes = array();
69
-	static private $loadedApps = array();
70
-	static private $altLogin = array();
71
-	static private $alreadyRegistered = [];
72
-	const officialApp = 200;
73
-
74
-	/**
75
-	 * clean the appId
76
-	 *
77
-	 * @param string|boolean $app AppId that needs to be cleaned
78
-	 * @return string
79
-	 */
80
-	public static function cleanAppId($app) {
81
-		return str_replace(array('\0', '/', '\\', '..'), '', $app);
82
-	}
83
-
84
-	/**
85
-	 * Check if an app is loaded
86
-	 *
87
-	 * @param string $app
88
-	 * @return bool
89
-	 */
90
-	public static function isAppLoaded($app) {
91
-		return in_array($app, self::$loadedApps, true);
92
-	}
93
-
94
-	/**
95
-	 * loads all apps
96
-	 *
97
-	 * @param string[] | string | null $types
98
-	 * @return bool
99
-	 *
100
-	 * This function walks through the ownCloud directory and loads all apps
101
-	 * it can find. A directory contains an app if the file /appinfo/info.xml
102
-	 * exists.
103
-	 *
104
-	 * if $types is set, only apps of those types will be loaded
105
-	 */
106
-	public static function loadApps($types = null) {
107
-		if (\OC::$server->getSystemConfig()->getValue('maintenance', false)) {
108
-			return false;
109
-		}
110
-		// Load the enabled apps here
111
-		$apps = self::getEnabledApps();
112
-
113
-		// Add each apps' folder as allowed class path
114
-		foreach($apps as $app) {
115
-			$path = self::getAppPath($app);
116
-			if($path !== false) {
117
-				self::registerAutoloading($app, $path);
118
-			}
119
-		}
120
-
121
-		// prevent app.php from printing output
122
-		ob_start();
123
-		foreach ($apps as $app) {
124
-			if ((is_null($types) or self::isType($app, $types)) && !in_array($app, self::$loadedApps)) {
125
-				self::loadApp($app);
126
-			}
127
-		}
128
-		ob_end_clean();
129
-
130
-		return true;
131
-	}
132
-
133
-	/**
134
-	 * load a single app
135
-	 *
136
-	 * @param string $app
137
-	 */
138
-	public static function loadApp($app) {
139
-		self::$loadedApps[] = $app;
140
-		$appPath = self::getAppPath($app);
141
-		if($appPath === false) {
142
-			return;
143
-		}
144
-
145
-		// in case someone calls loadApp() directly
146
-		self::registerAutoloading($app, $appPath);
147
-
148
-		if (is_file($appPath . '/appinfo/app.php')) {
149
-			\OC::$server->getEventLogger()->start('load_app_' . $app, 'Load app: ' . $app);
150
-			self::requireAppFile($app);
151
-			if (self::isType($app, array('authentication'))) {
152
-				// since authentication apps affect the "is app enabled for group" check,
153
-				// the enabled apps cache needs to be cleared to make sure that the
154
-				// next time getEnableApps() is called it will also include apps that were
155
-				// enabled for groups
156
-				self::$enabledAppsCache = array();
157
-			}
158
-			\OC::$server->getEventLogger()->end('load_app_' . $app);
159
-		}
160
-
161
-		$info = self::getAppInfo($app);
162
-		if (!empty($info['activity']['filters'])) {
163
-			foreach ($info['activity']['filters'] as $filter) {
164
-				\OC::$server->getActivityManager()->registerFilter($filter);
165
-			}
166
-		}
167
-		if (!empty($info['activity']['settings'])) {
168
-			foreach ($info['activity']['settings'] as $setting) {
169
-				\OC::$server->getActivityManager()->registerSetting($setting);
170
-			}
171
-		}
172
-		if (!empty($info['activity']['providers'])) {
173
-			foreach ($info['activity']['providers'] as $provider) {
174
-				\OC::$server->getActivityManager()->registerProvider($provider);
175
-			}
176
-		}
177
-	}
178
-
179
-	/**
180
-	 * @internal
181
-	 * @param string $app
182
-	 * @param string $path
183
-	 */
184
-	public static function registerAutoloading($app, $path) {
185
-		$key = $app . '-' . $path;
186
-		if(isset(self::$alreadyRegistered[$key])) {
187
-			return;
188
-		}
189
-		self::$alreadyRegistered[$key] = true;
190
-		// Register on PSR-4 composer autoloader
191
-		$appNamespace = \OC\AppFramework\App::buildAppNamespace($app);
192
-		\OC::$server->registerNamespace($app, $appNamespace);
193
-		\OC::$composerAutoloader->addPsr4($appNamespace . '\\', $path . '/lib/', true);
194
-		if (defined('PHPUNIT_RUN') || defined('CLI_TEST_RUN')) {
195
-			\OC::$composerAutoloader->addPsr4($appNamespace . '\\Tests\\', $path . '/tests/', true);
196
-		}
197
-
198
-		// Register on legacy autoloader
199
-		\OC::$loader->addValidRoot($path);
200
-	}
201
-
202
-	/**
203
-	 * Load app.php from the given app
204
-	 *
205
-	 * @param string $app app name
206
-	 */
207
-	private static function requireAppFile($app) {
208
-		try {
209
-			// encapsulated here to avoid variable scope conflicts
210
-			require_once $app . '/appinfo/app.php';
211
-		} catch (Error $ex) {
212
-			\OC::$server->getLogger()->logException($ex);
213
-			$blacklist = \OC::$server->getAppManager()->getAlwaysEnabledApps();
214
-			if (!in_array($app, $blacklist)) {
215
-				self::disable($app);
216
-			}
217
-		}
218
-	}
219
-
220
-	/**
221
-	 * check if an app is of a specific type
222
-	 *
223
-	 * @param string $app
224
-	 * @param string|array $types
225
-	 * @return bool
226
-	 */
227
-	public static function isType($app, $types) {
228
-		if (is_string($types)) {
229
-			$types = array($types);
230
-		}
231
-		$appTypes = self::getAppTypes($app);
232
-		foreach ($types as $type) {
233
-			if (array_search($type, $appTypes) !== false) {
234
-				return true;
235
-			}
236
-		}
237
-		return false;
238
-	}
239
-
240
-	/**
241
-	 * get the types of an app
242
-	 *
243
-	 * @param string $app
244
-	 * @return array
245
-	 */
246
-	private static function getAppTypes($app) {
247
-		//load the cache
248
-		if (count(self::$appTypes) == 0) {
249
-			self::$appTypes = \OC::$server->getAppConfig()->getValues(false, 'types');
250
-		}
251
-
252
-		if (isset(self::$appTypes[$app])) {
253
-			return explode(',', self::$appTypes[$app]);
254
-		} else {
255
-			return array();
256
-		}
257
-	}
258
-
259
-	/**
260
-	 * read app types from info.xml and cache them in the database
261
-	 */
262
-	public static function setAppTypes($app) {
263
-		$appData = self::getAppInfo($app);
264
-		if(!is_array($appData)) {
265
-			return;
266
-		}
267
-
268
-		if (isset($appData['types'])) {
269
-			$appTypes = implode(',', $appData['types']);
270
-		} else {
271
-			$appTypes = '';
272
-			$appData['types'] = [];
273
-		}
274
-
275
-		\OC::$server->getAppConfig()->setValue($app, 'types', $appTypes);
276
-
277
-		if (\OC::$server->getAppManager()->hasProtectedAppType($appData['types'])) {
278
-			$enabled = \OC::$server->getAppConfig()->getValue($app, 'enabled', 'yes');
279
-			if ($enabled !== 'yes' && $enabled !== 'no') {
280
-				\OC::$server->getAppConfig()->setValue($app, 'enabled', 'yes');
281
-			}
282
-		}
283
-	}
284
-
285
-	/**
286
-	 * check if app is shipped
287
-	 *
288
-	 * @param string $appId the id of the app to check
289
-	 * @return bool
290
-	 *
291
-	 * Check if an app that is installed is a shipped app or installed from the appstore.
292
-	 */
293
-	public static function isShipped($appId) {
294
-		return \OC::$server->getAppManager()->isShipped($appId);
295
-	}
296
-
297
-	/**
298
-	 * get all enabled apps
299
-	 */
300
-	protected static $enabledAppsCache = array();
301
-
302
-	/**
303
-	 * Returns apps enabled for the current user.
304
-	 *
305
-	 * @param bool $forceRefresh whether to refresh the cache
306
-	 * @param bool $all whether to return apps for all users, not only the
307
-	 * currently logged in one
308
-	 * @return string[]
309
-	 */
310
-	public static function getEnabledApps($forceRefresh = false, $all = false) {
311
-		if (!\OC::$server->getSystemConfig()->getValue('installed', false)) {
312
-			return array();
313
-		}
314
-		// in incognito mode or when logged out, $user will be false,
315
-		// which is also the case during an upgrade
316
-		$appManager = \OC::$server->getAppManager();
317
-		if ($all) {
318
-			$user = null;
319
-		} else {
320
-			$user = \OC::$server->getUserSession()->getUser();
321
-		}
322
-
323
-		if (is_null($user)) {
324
-			$apps = $appManager->getInstalledApps();
325
-		} else {
326
-			$apps = $appManager->getEnabledAppsForUser($user);
327
-		}
328
-		$apps = array_filter($apps, function ($app) {
329
-			return $app !== 'files';//we add this manually
330
-		});
331
-		sort($apps);
332
-		array_unshift($apps, 'files');
333
-		return $apps;
334
-	}
335
-
336
-	/**
337
-	 * checks whether or not an app is enabled
338
-	 *
339
-	 * @param string $app app
340
-	 * @return bool
341
-	 *
342
-	 * This function checks whether or not an app is enabled.
343
-	 */
344
-	public static function isEnabled($app) {
345
-		return \OC::$server->getAppManager()->isEnabledForUser($app);
346
-	}
347
-
348
-	/**
349
-	 * enables an app
350
-	 *
351
-	 * @param string $appId
352
-	 * @param array $groups (optional) when set, only these groups will have access to the app
353
-	 * @throws \Exception
354
-	 * @return void
355
-	 *
356
-	 * This function set an app as enabled in appconfig.
357
-	 */
358
-	public function enable($appId,
359
-						   $groups = null) {
360
-		self::$enabledAppsCache = []; // flush
361
-
362
-		// Check if app is already downloaded
363
-		$installer = new Installer(
364
-			\OC::$server->getAppFetcher(),
365
-			\OC::$server->getHTTPClientService(),
366
-			\OC::$server->getTempManager(),
367
-			\OC::$server->getLogger(),
368
-			\OC::$server->getConfig()
369
-		);
370
-		$isDownloaded = $installer->isDownloaded($appId);
371
-
372
-		if(!$isDownloaded) {
373
-			$installer->downloadApp($appId);
374
-		}
375
-
376
-		$installer->installApp($appId);
377
-
378
-		$appManager = \OC::$server->getAppManager();
379
-		if (!is_null($groups)) {
380
-			$groupManager = \OC::$server->getGroupManager();
381
-			$groupsList = [];
382
-			foreach ($groups as $group) {
383
-				$groupItem = $groupManager->get($group);
384
-				if ($groupItem instanceof \OCP\IGroup) {
385
-					$groupsList[] = $groupManager->get($group);
386
-				}
387
-			}
388
-			$appManager->enableAppForGroups($appId, $groupsList);
389
-		} else {
390
-			$appManager->enableApp($appId);
391
-		}
392
-	}
393
-
394
-	/**
395
-	 * @param string $app
396
-	 * @return bool
397
-	 */
398
-	public static function removeApp($app) {
399
-		if (self::isShipped($app)) {
400
-			return false;
401
-		}
402
-
403
-		$installer = new Installer(
404
-			\OC::$server->getAppFetcher(),
405
-			\OC::$server->getHTTPClientService(),
406
-			\OC::$server->getTempManager(),
407
-			\OC::$server->getLogger(),
408
-			\OC::$server->getConfig()
409
-		);
410
-		return $installer->removeApp($app);
411
-	}
412
-
413
-	/**
414
-	 * This function set an app as disabled in appconfig.
415
-	 *
416
-	 * @param string $app app
417
-	 * @throws Exception
418
-	 */
419
-	public static function disable($app) {
420
-		// flush
421
-		self::$enabledAppsCache = array();
422
-
423
-		$appManager = \OC::$server->getAppManager();
424
-		$appManager->disableApp($app);
425
-	}
426
-
427
-	// This is private as well. It simply works, so don't ask for more details
428
-	private static function proceedNavigation($list) {
429
-		usort($list, function($a, $b) {
430
-			if (isset($a['order']) && isset($b['order'])) {
431
-				return ($a['order'] < $b['order']) ? -1 : 1;
432
-			} else if (isset($a['order']) || isset($b['order'])) {
433
-				return isset($a['order']) ? -1 : 1;
434
-			} else {
435
-				return ($a['name'] < $b['name']) ? -1 : 1;
436
-			}
437
-		});
438
-
439
-		$activeApp = OC::$server->getNavigationManager()->getActiveEntry();
440
-		foreach ($list as $index => &$navEntry) {
441
-			if ($navEntry['id'] == $activeApp) {
442
-				$navEntry['active'] = true;
443
-			} else {
444
-				$navEntry['active'] = false;
445
-			}
446
-		}
447
-		unset($navEntry);
448
-
449
-		return $list;
450
-	}
451
-
452
-	/**
453
-	 * Get the path where to install apps
454
-	 *
455
-	 * @return string|false
456
-	 */
457
-	public static function getInstallPath() {
458
-		if (\OC::$server->getSystemConfig()->getValue('appstoreenabled', true) == false) {
459
-			return false;
460
-		}
461
-
462
-		foreach (OC::$APPSROOTS as $dir) {
463
-			if (isset($dir['writable']) && $dir['writable'] === true) {
464
-				return $dir['path'];
465
-			}
466
-		}
467
-
468
-		\OCP\Util::writeLog('core', 'No application directories are marked as writable.', \OCP\Util::ERROR);
469
-		return null;
470
-	}
471
-
472
-
473
-	/**
474
-	 * search for an app in all app-directories
475
-	 *
476
-	 * @param string $appId
477
-	 * @return false|string
478
-	 */
479
-	public static function findAppInDirectories($appId) {
480
-		$sanitizedAppId = self::cleanAppId($appId);
481
-		if($sanitizedAppId !== $appId) {
482
-			return false;
483
-		}
484
-		static $app_dir = array();
485
-
486
-		if (isset($app_dir[$appId])) {
487
-			return $app_dir[$appId];
488
-		}
489
-
490
-		$possibleApps = array();
491
-		foreach (OC::$APPSROOTS as $dir) {
492
-			if (file_exists($dir['path'] . '/' . $appId)) {
493
-				$possibleApps[] = $dir;
494
-			}
495
-		}
496
-
497
-		if (empty($possibleApps)) {
498
-			return false;
499
-		} elseif (count($possibleApps) === 1) {
500
-			$dir = array_shift($possibleApps);
501
-			$app_dir[$appId] = $dir;
502
-			return $dir;
503
-		} else {
504
-			$versionToLoad = array();
505
-			foreach ($possibleApps as $possibleApp) {
506
-				$version = self::getAppVersionByPath($possibleApp['path']);
507
-				if (empty($versionToLoad) || version_compare($version, $versionToLoad['version'], '>')) {
508
-					$versionToLoad = array(
509
-						'dir' => $possibleApp,
510
-						'version' => $version,
511
-					);
512
-				}
513
-			}
514
-			$app_dir[$appId] = $versionToLoad['dir'];
515
-			return $versionToLoad['dir'];
516
-			//TODO - write test
517
-		}
518
-	}
519
-
520
-	/**
521
-	 * Get the directory for the given app.
522
-	 * If the app is defined in multiple directories, the first one is taken. (false if not found)
523
-	 *
524
-	 * @param string $appId
525
-	 * @return string|false
526
-	 */
527
-	public static function getAppPath($appId) {
528
-		if ($appId === null || trim($appId) === '') {
529
-			return false;
530
-		}
531
-
532
-		if (($dir = self::findAppInDirectories($appId)) != false) {
533
-			return $dir['path'] . '/' . $appId;
534
-		}
535
-		return false;
536
-	}
537
-
538
-	/**
539
-	 * Get the path for the given app on the access
540
-	 * If the app is defined in multiple directories, the first one is taken. (false if not found)
541
-	 *
542
-	 * @param string $appId
543
-	 * @return string|false
544
-	 */
545
-	public static function getAppWebPath($appId) {
546
-		if (($dir = self::findAppInDirectories($appId)) != false) {
547
-			return OC::$WEBROOT . $dir['url'] . '/' . $appId;
548
-		}
549
-		return false;
550
-	}
551
-
552
-	/**
553
-	 * get the last version of the app from appinfo/info.xml
554
-	 *
555
-	 * @param string $appId
556
-	 * @param bool $useCache
557
-	 * @return string
558
-	 */
559
-	public static function getAppVersion($appId, $useCache = true) {
560
-		if($useCache && isset(self::$appVersion[$appId])) {
561
-			return self::$appVersion[$appId];
562
-		}
563
-
564
-		$file = self::getAppPath($appId);
565
-		self::$appVersion[$appId] = ($file !== false) ? self::getAppVersionByPath($file) : '0';
566
-		return self::$appVersion[$appId];
567
-	}
568
-
569
-	/**
570
-	 * get app's version based on it's path
571
-	 *
572
-	 * @param string $path
573
-	 * @return string
574
-	 */
575
-	public static function getAppVersionByPath($path) {
576
-		$infoFile = $path . '/appinfo/info.xml';
577
-		$appData = self::getAppInfo($infoFile, true);
578
-		return isset($appData['version']) ? $appData['version'] : '';
579
-	}
580
-
581
-
582
-	/**
583
-	 * Read all app metadata from the info.xml file
584
-	 *
585
-	 * @param string $appId id of the app or the path of the info.xml file
586
-	 * @param bool $path
587
-	 * @param string $lang
588
-	 * @return array|null
589
-	 * @note all data is read from info.xml, not just pre-defined fields
590
-	 */
591
-	public static function getAppInfo($appId, $path = false, $lang = null) {
592
-		if ($path) {
593
-			$file = $appId;
594
-		} else {
595
-			if ($lang === null && isset(self::$appInfo[$appId])) {
596
-				return self::$appInfo[$appId];
597
-			}
598
-			$appPath = self::getAppPath($appId);
599
-			if($appPath === false) {
600
-				return null;
601
-			}
602
-			$file = $appPath . '/appinfo/info.xml';
603
-		}
604
-
605
-		$parser = new InfoParser(\OC::$server->getMemCacheFactory()->create('core.appinfo'));
606
-		$data = $parser->parse($file);
607
-
608
-		if (is_array($data)) {
609
-			$data = OC_App::parseAppInfo($data, $lang);
610
-		}
611
-		if(isset($data['ocsid'])) {
612
-			$storedId = \OC::$server->getConfig()->getAppValue($appId, 'ocsid');
613
-			if($storedId !== '' && $storedId !== $data['ocsid']) {
614
-				$data['ocsid'] = $storedId;
615
-			}
616
-		}
617
-
618
-		if ($lang === null) {
619
-			self::$appInfo[$appId] = $data;
620
-		}
621
-
622
-		return $data;
623
-	}
624
-
625
-	/**
626
-	 * Returns the navigation
627
-	 *
628
-	 * @return array
629
-	 *
630
-	 * This function returns an array containing all entries added. The
631
-	 * entries are sorted by the key 'order' ascending. Additional to the keys
632
-	 * given for each app the following keys exist:
633
-	 *   - active: boolean, signals if the user is on this navigation entry
634
-	 */
635
-	public static function getNavigation() {
636
-		$entries = OC::$server->getNavigationManager()->getAll();
637
-		return self::proceedNavigation($entries);
638
-	}
639
-
640
-	/**
641
-	 * Returns the Settings Navigation
642
-	 *
643
-	 * @return string[]
644
-	 *
645
-	 * This function returns an array containing all settings pages added. The
646
-	 * entries are sorted by the key 'order' ascending.
647
-	 */
648
-	public static function getSettingsNavigation() {
649
-		$entries = OC::$server->getNavigationManager()->getAll('settings');
650
-		return self::proceedNavigation($entries);
651
-	}
652
-
653
-	/**
654
-	 * get the id of loaded app
655
-	 *
656
-	 * @return string
657
-	 */
658
-	public static function getCurrentApp() {
659
-		$request = \OC::$server->getRequest();
660
-		$script = substr($request->getScriptName(), strlen(OC::$WEBROOT) + 1);
661
-		$topFolder = substr($script, 0, strpos($script, '/'));
662
-		if (empty($topFolder)) {
663
-			$path_info = $request->getPathInfo();
664
-			if ($path_info) {
665
-				$topFolder = substr($path_info, 1, strpos($path_info, '/', 1) - 1);
666
-			}
667
-		}
668
-		if ($topFolder == 'apps') {
669
-			$length = strlen($topFolder);
670
-			return substr($script, $length + 1, strpos($script, '/', $length + 1) - $length - 1);
671
-		} else {
672
-			return $topFolder;
673
-		}
674
-	}
675
-
676
-	/**
677
-	 * @param string $type
678
-	 * @return array
679
-	 */
680
-	public static function getForms($type) {
681
-		$forms = array();
682
-		switch ($type) {
683
-			case 'admin':
684
-				$source = self::$adminForms;
685
-				break;
686
-			case 'personal':
687
-				$source = self::$personalForms;
688
-				break;
689
-			default:
690
-				return array();
691
-		}
692
-		foreach ($source as $form) {
693
-			$forms[] = include $form;
694
-		}
695
-		return $forms;
696
-	}
697
-
698
-	/**
699
-	 * register an admin form to be shown
700
-	 *
701
-	 * @param string $app
702
-	 * @param string $page
703
-	 */
704
-	public static function registerAdmin($app, $page) {
705
-		self::$adminForms[] = $app . '/' . $page . '.php';
706
-	}
707
-
708
-	/**
709
-	 * register a personal form to be shown
710
-	 * @param string $app
711
-	 * @param string $page
712
-	 */
713
-	public static function registerPersonal($app, $page) {
714
-		self::$personalForms[] = $app . '/' . $page . '.php';
715
-	}
716
-
717
-	/**
718
-	 * @param array $entry
719
-	 */
720
-	public static function registerLogIn(array $entry) {
721
-		self::$altLogin[] = $entry;
722
-	}
723
-
724
-	/**
725
-	 * @return array
726
-	 */
727
-	public static function getAlternativeLogIns() {
728
-		return self::$altLogin;
729
-	}
730
-
731
-	/**
732
-	 * get a list of all apps in the apps folder
733
-	 *
734
-	 * @return array an array of app names (string IDs)
735
-	 * @todo: change the name of this method to getInstalledApps, which is more accurate
736
-	 */
737
-	public static function getAllApps() {
738
-
739
-		$apps = array();
740
-
741
-		foreach (OC::$APPSROOTS as $apps_dir) {
742
-			if (!is_readable($apps_dir['path'])) {
743
-				\OCP\Util::writeLog('core', 'unable to read app folder : ' . $apps_dir['path'], \OCP\Util::WARN);
744
-				continue;
745
-			}
746
-			$dh = opendir($apps_dir['path']);
747
-
748
-			if (is_resource($dh)) {
749
-				while (($file = readdir($dh)) !== false) {
750
-
751
-					if ($file[0] != '.' and is_dir($apps_dir['path'] . '/' . $file) and is_file($apps_dir['path'] . '/' . $file . '/appinfo/info.xml')) {
752
-
753
-						$apps[] = $file;
754
-					}
755
-				}
756
-			}
757
-		}
758
-
759
-		return $apps;
760
-	}
761
-
762
-	/**
763
-	 * List all apps, this is used in apps.php
764
-	 *
765
-	 * @return array
766
-	 */
767
-	public function listAllApps() {
768
-		$installedApps = OC_App::getAllApps();
769
-
770
-		//we don't want to show configuration for these
771
-		$blacklist = \OC::$server->getAppManager()->getAlwaysEnabledApps();
772
-		$appList = array();
773
-		$langCode = \OC::$server->getL10N('core')->getLanguageCode();
774
-		$urlGenerator = \OC::$server->getURLGenerator();
775
-
776
-		foreach ($installedApps as $app) {
777
-			if (array_search($app, $blacklist) === false) {
778
-
779
-				$info = OC_App::getAppInfo($app, false, $langCode);
780
-				if (!is_array($info)) {
781
-					\OCP\Util::writeLog('core', 'Could not read app info file for app "' . $app . '"', \OCP\Util::ERROR);
782
-					continue;
783
-				}
784
-
785
-				if (!isset($info['name'])) {
786
-					\OCP\Util::writeLog('core', 'App id "' . $app . '" has no name in appinfo', \OCP\Util::ERROR);
787
-					continue;
788
-				}
789
-
790
-				$enabled = \OC::$server->getAppConfig()->getValue($app, 'enabled', 'no');
791
-				$info['groups'] = null;
792
-				if ($enabled === 'yes') {
793
-					$active = true;
794
-				} else if ($enabled === 'no') {
795
-					$active = false;
796
-				} else {
797
-					$active = true;
798
-					$info['groups'] = $enabled;
799
-				}
800
-
801
-				$info['active'] = $active;
802
-
803
-				if (self::isShipped($app)) {
804
-					$info['internal'] = true;
805
-					$info['level'] = self::officialApp;
806
-					$info['removable'] = false;
807
-				} else {
808
-					$info['internal'] = false;
809
-					$info['removable'] = true;
810
-				}
811
-
812
-				$appPath = self::getAppPath($app);
813
-				if($appPath !== false) {
814
-					$appIcon = $appPath . '/img/' . $app . '.svg';
815
-					if (file_exists($appIcon)) {
816
-						$info['preview'] = \OC::$server->getURLGenerator()->imagePath($app, $app . '.svg');
817
-						$info['previewAsIcon'] = true;
818
-					} else {
819
-						$appIcon = $appPath . '/img/app.svg';
820
-						if (file_exists($appIcon)) {
821
-							$info['preview'] = \OC::$server->getURLGenerator()->imagePath($app, 'app.svg');
822
-							$info['previewAsIcon'] = true;
823
-						}
824
-					}
825
-				}
826
-				// fix documentation
827
-				if (isset($info['documentation']) && is_array($info['documentation'])) {
828
-					foreach ($info['documentation'] as $key => $url) {
829
-						// If it is not an absolute URL we assume it is a key
830
-						// i.e. admin-ldap will get converted to go.php?to=admin-ldap
831
-						if (stripos($url, 'https://') !== 0 && stripos($url, 'http://') !== 0) {
832
-							$url = $urlGenerator->linkToDocs($url);
833
-						}
834
-
835
-						$info['documentation'][$key] = $url;
836
-					}
837
-				}
838
-
839
-				$info['version'] = OC_App::getAppVersion($app);
840
-				$appList[] = $info;
841
-			}
842
-		}
843
-
844
-		return $appList;
845
-	}
846
-
847
-	/**
848
-	 * Returns the internal app ID or false
849
-	 * @param string $ocsID
850
-	 * @return string|false
851
-	 */
852
-	public static function getInternalAppIdByOcs($ocsID) {
853
-		if(is_numeric($ocsID)) {
854
-			$idArray = \OC::$server->getAppConfig()->getValues(false, 'ocsid');
855
-			if(array_search($ocsID, $idArray)) {
856
-				return array_search($ocsID, $idArray);
857
-			}
858
-		}
859
-		return false;
860
-	}
861
-
862
-	public static function shouldUpgrade($app) {
863
-		$versions = self::getAppVersions();
864
-		$currentVersion = OC_App::getAppVersion($app);
865
-		if ($currentVersion && isset($versions[$app])) {
866
-			$installedVersion = $versions[$app];
867
-			if (!version_compare($currentVersion, $installedVersion, '=')) {
868
-				return true;
869
-			}
870
-		}
871
-		return false;
872
-	}
873
-
874
-	/**
875
-	 * Adjust the number of version parts of $version1 to match
876
-	 * the number of version parts of $version2.
877
-	 *
878
-	 * @param string $version1 version to adjust
879
-	 * @param string $version2 version to take the number of parts from
880
-	 * @return string shortened $version1
881
-	 */
882
-	private static function adjustVersionParts($version1, $version2) {
883
-		$version1 = explode('.', $version1);
884
-		$version2 = explode('.', $version2);
885
-		// reduce $version1 to match the number of parts in $version2
886
-		while (count($version1) > count($version2)) {
887
-			array_pop($version1);
888
-		}
889
-		// if $version1 does not have enough parts, add some
890
-		while (count($version1) < count($version2)) {
891
-			$version1[] = '0';
892
-		}
893
-		return implode('.', $version1);
894
-	}
895
-
896
-	/**
897
-	 * Check whether the current ownCloud version matches the given
898
-	 * application's version requirements.
899
-	 *
900
-	 * The comparison is made based on the number of parts that the
901
-	 * app info version has. For example for ownCloud 6.0.3 if the
902
-	 * app info version is expecting version 6.0, the comparison is
903
-	 * made on the first two parts of the ownCloud version.
904
-	 * This means that it's possible to specify "requiremin" => 6
905
-	 * and "requiremax" => 6 and it will still match ownCloud 6.0.3.
906
-	 *
907
-	 * @param string $ocVersion ownCloud version to check against
908
-	 * @param array $appInfo app info (from xml)
909
-	 *
910
-	 * @return boolean true if compatible, otherwise false
911
-	 */
912
-	public static function isAppCompatible($ocVersion, $appInfo) {
913
-		$requireMin = '';
914
-		$requireMax = '';
915
-		if (isset($appInfo['dependencies']['nextcloud']['@attributes']['min-version'])) {
916
-			$requireMin = $appInfo['dependencies']['nextcloud']['@attributes']['min-version'];
917
-		} elseif (isset($appInfo['dependencies']['owncloud']['@attributes']['min-version'])) {
918
-			$requireMin = $appInfo['dependencies']['owncloud']['@attributes']['min-version'];
919
-		} else if (isset($appInfo['requiremin'])) {
920
-			$requireMin = $appInfo['requiremin'];
921
-		} else if (isset($appInfo['require'])) {
922
-			$requireMin = $appInfo['require'];
923
-		}
924
-
925
-		if (isset($appInfo['dependencies']['nextcloud']['@attributes']['max-version'])) {
926
-			$requireMax = $appInfo['dependencies']['nextcloud']['@attributes']['max-version'];
927
-		} elseif (isset($appInfo['dependencies']['owncloud']['@attributes']['max-version'])) {
928
-			$requireMax = $appInfo['dependencies']['owncloud']['@attributes']['max-version'];
929
-		} else if (isset($appInfo['requiremax'])) {
930
-			$requireMax = $appInfo['requiremax'];
931
-		}
932
-
933
-		if (is_array($ocVersion)) {
934
-			$ocVersion = implode('.', $ocVersion);
935
-		}
936
-
937
-		if (!empty($requireMin)
938
-			&& version_compare(self::adjustVersionParts($ocVersion, $requireMin), $requireMin, '<')
939
-		) {
940
-
941
-			return false;
942
-		}
943
-
944
-		if (!empty($requireMax)
945
-			&& version_compare(self::adjustVersionParts($ocVersion, $requireMax), $requireMax, '>')
946
-		) {
947
-			return false;
948
-		}
949
-
950
-		return true;
951
-	}
952
-
953
-	/**
954
-	 * get the installed version of all apps
955
-	 */
956
-	public static function getAppVersions() {
957
-		static $versions;
958
-
959
-		if(!$versions) {
960
-			$appConfig = \OC::$server->getAppConfig();
961
-			$versions = $appConfig->getValues(false, 'installed_version');
962
-		}
963
-		return $versions;
964
-	}
965
-
966
-	/**
967
-	 * @param string $app
968
-	 * @param \OCP\IConfig $config
969
-	 * @param \OCP\IL10N $l
970
-	 * @return bool
971
-	 *
972
-	 * @throws Exception if app is not compatible with this version of ownCloud
973
-	 * @throws Exception if no app-name was specified
974
-	 */
975
-	public function installApp($app,
976
-							   \OCP\IConfig $config,
977
-							   \OCP\IL10N $l) {
978
-		if ($app !== false) {
979
-			// check if the app is compatible with this version of ownCloud
980
-			$info = self::getAppInfo($app);
981
-			if(!is_array($info)) {
982
-				throw new \Exception(
983
-					$l->t('App "%s" cannot be installed because appinfo file cannot be read.',
984
-						[$info['name']]
985
-					)
986
-				);
987
-			}
988
-
989
-			$version = \OCP\Util::getVersion();
990
-			if (!self::isAppCompatible($version, $info)) {
991
-				throw new \Exception(
992
-					$l->t('App "%s" cannot be installed because it is not compatible with this version of the server.',
993
-						array($info['name'])
994
-					)
995
-				);
996
-			}
997
-
998
-			// check for required dependencies
999
-			self::checkAppDependencies($config, $l, $info);
1000
-
1001
-			$config->setAppValue($app, 'enabled', 'yes');
1002
-			if (isset($appData['id'])) {
1003
-				$config->setAppValue($app, 'ocsid', $appData['id']);
1004
-			}
1005
-
1006
-			if(isset($info['settings']) && is_array($info['settings'])) {
1007
-				$appPath = self::getAppPath($app);
1008
-				self::registerAutoloading($app, $appPath);
1009
-				\OC::$server->getSettingsManager()->setupSettings($info['settings']);
1010
-			}
1011
-
1012
-			\OC_Hook::emit('OC_App', 'post_enable', array('app' => $app));
1013
-		} else {
1014
-			if(empty($appName) ) {
1015
-				throw new \Exception($l->t("No app name specified"));
1016
-			} else {
1017
-				throw new \Exception($l->t("App '%s' could not be installed!", $appName));
1018
-			}
1019
-		}
1020
-
1021
-		return $app;
1022
-	}
1023
-
1024
-	/**
1025
-	 * update the database for the app and call the update script
1026
-	 *
1027
-	 * @param string $appId
1028
-	 * @return bool
1029
-	 */
1030
-	public static function updateApp($appId) {
1031
-		$appPath = self::getAppPath($appId);
1032
-		if($appPath === false) {
1033
-			return false;
1034
-		}
1035
-		self::registerAutoloading($appId, $appPath);
1036
-
1037
-		$appData = self::getAppInfo($appId);
1038
-		self::executeRepairSteps($appId, $appData['repair-steps']['pre-migration']);
1039
-
1040
-		if (file_exists($appPath . '/appinfo/database.xml')) {
1041
-			OC_DB::updateDbFromStructure($appPath . '/appinfo/database.xml');
1042
-		} else {
1043
-			$ms = new MigrationService($appId, \OC::$server->getDatabaseConnection());
1044
-			$ms->migrate();
1045
-		}
1046
-
1047
-		self::executeRepairSteps($appId, $appData['repair-steps']['post-migration']);
1048
-		self::setupLiveMigrations($appId, $appData['repair-steps']['live-migration']);
1049
-		unset(self::$appVersion[$appId]);
1050
-
1051
-		// run upgrade code
1052
-		if (file_exists($appPath . '/appinfo/update.php')) {
1053
-			self::loadApp($appId);
1054
-			include $appPath . '/appinfo/update.php';
1055
-		}
1056
-		self::setupBackgroundJobs($appData['background-jobs']);
1057
-		if(isset($appData['settings']) && is_array($appData['settings'])) {
1058
-			\OC::$server->getSettingsManager()->setupSettings($appData['settings']);
1059
-		}
1060
-
1061
-		//set remote/public handlers
1062
-		if (array_key_exists('ocsid', $appData)) {
1063
-			\OC::$server->getConfig()->setAppValue($appId, 'ocsid', $appData['ocsid']);
1064
-		} elseif(\OC::$server->getConfig()->getAppValue($appId, 'ocsid', null) !== null) {
1065
-			\OC::$server->getConfig()->deleteAppValue($appId, 'ocsid');
1066
-		}
1067
-		foreach ($appData['remote'] as $name => $path) {
1068
-			\OC::$server->getConfig()->setAppValue('core', 'remote_' . $name, $appId . '/' . $path);
1069
-		}
1070
-		foreach ($appData['public'] as $name => $path) {
1071
-			\OC::$server->getConfig()->setAppValue('core', 'public_' . $name, $appId . '/' . $path);
1072
-		}
1073
-
1074
-		self::setAppTypes($appId);
1075
-
1076
-		$version = \OC_App::getAppVersion($appId);
1077
-		\OC::$server->getAppConfig()->setValue($appId, 'installed_version', $version);
1078
-
1079
-		\OC::$server->getEventDispatcher()->dispatch(ManagerEvent::EVENT_APP_UPDATE, new ManagerEvent(
1080
-			ManagerEvent::EVENT_APP_UPDATE, $appId
1081
-		));
1082
-
1083
-		return true;
1084
-	}
1085
-
1086
-	/**
1087
-	 * @param string $appId
1088
-	 * @param string[] $steps
1089
-	 * @throws \OC\NeedsUpdateException
1090
-	 */
1091
-	public static function executeRepairSteps($appId, array $steps) {
1092
-		if (empty($steps)) {
1093
-			return;
1094
-		}
1095
-		// load the app
1096
-		self::loadApp($appId);
1097
-
1098
-		$dispatcher = OC::$server->getEventDispatcher();
1099
-
1100
-		// load the steps
1101
-		$r = new Repair([], $dispatcher);
1102
-		foreach ($steps as $step) {
1103
-			try {
1104
-				$r->addStep($step);
1105
-			} catch (Exception $ex) {
1106
-				$r->emit('\OC\Repair', 'error', [$ex->getMessage()]);
1107
-				\OC::$server->getLogger()->logException($ex);
1108
-			}
1109
-		}
1110
-		// run the steps
1111
-		$r->run();
1112
-	}
1113
-
1114
-	public static function setupBackgroundJobs(array $jobs) {
1115
-		$queue = \OC::$server->getJobList();
1116
-		foreach ($jobs as $job) {
1117
-			$queue->add($job);
1118
-		}
1119
-	}
1120
-
1121
-	/**
1122
-	 * @param string $appId
1123
-	 * @param string[] $steps
1124
-	 */
1125
-	private static function setupLiveMigrations($appId, array $steps) {
1126
-		$queue = \OC::$server->getJobList();
1127
-		foreach ($steps as $step) {
1128
-			$queue->add('OC\Migration\BackgroundRepair', [
1129
-				'app' => $appId,
1130
-				'step' => $step]);
1131
-		}
1132
-	}
1133
-
1134
-	/**
1135
-	 * @param string $appId
1136
-	 * @return \OC\Files\View|false
1137
-	 */
1138
-	public static function getStorage($appId) {
1139
-		if (OC_App::isEnabled($appId)) { //sanity check
1140
-			if (\OC::$server->getUserSession()->isLoggedIn()) {
1141
-				$view = new \OC\Files\View('/' . OC_User::getUser());
1142
-				if (!$view->file_exists($appId)) {
1143
-					$view->mkdir($appId);
1144
-				}
1145
-				return new \OC\Files\View('/' . OC_User::getUser() . '/' . $appId);
1146
-			} else {
1147
-				\OCP\Util::writeLog('core', 'Can\'t get app storage, app ' . $appId . ', user not logged in', \OCP\Util::ERROR);
1148
-				return false;
1149
-			}
1150
-		} else {
1151
-			\OCP\Util::writeLog('core', 'Can\'t get app storage, app ' . $appId . ' not enabled', \OCP\Util::ERROR);
1152
-			return false;
1153
-		}
1154
-	}
1155
-
1156
-	protected static function findBestL10NOption($options, $lang) {
1157
-		$fallback = $similarLangFallback = $englishFallback = false;
1158
-
1159
-		$lang = strtolower($lang);
1160
-		$similarLang = $lang;
1161
-		if (strpos($similarLang, '_')) {
1162
-			// For "de_DE" we want to find "de" and the other way around
1163
-			$similarLang = substr($lang, 0, strpos($lang, '_'));
1164
-		}
1165
-
1166
-		foreach ($options as $option) {
1167
-			if (is_array($option)) {
1168
-				if ($fallback === false) {
1169
-					$fallback = $option['@value'];
1170
-				}
1171
-
1172
-				if (!isset($option['@attributes']['lang'])) {
1173
-					continue;
1174
-				}
1175
-
1176
-				$attributeLang = strtolower($option['@attributes']['lang']);
1177
-				if ($attributeLang === $lang) {
1178
-					return $option['@value'];
1179
-				}
1180
-
1181
-				if ($attributeLang === $similarLang) {
1182
-					$similarLangFallback = $option['@value'];
1183
-				} else if (strpos($attributeLang, $similarLang . '_') === 0) {
1184
-					if ($similarLangFallback === false) {
1185
-						$similarLangFallback =  $option['@value'];
1186
-					}
1187
-				}
1188
-			} else {
1189
-				$englishFallback = $option;
1190
-			}
1191
-		}
1192
-
1193
-		if ($similarLangFallback !== false) {
1194
-			return $similarLangFallback;
1195
-		} else if ($englishFallback !== false) {
1196
-			return $englishFallback;
1197
-		}
1198
-		return (string) $fallback;
1199
-	}
1200
-
1201
-	/**
1202
-	 * parses the app data array and enhanced the 'description' value
1203
-	 *
1204
-	 * @param array $data the app data
1205
-	 * @param string $lang
1206
-	 * @return array improved app data
1207
-	 */
1208
-	public static function parseAppInfo(array $data, $lang = null) {
1209
-
1210
-		if ($lang && isset($data['name']) && is_array($data['name'])) {
1211
-			$data['name'] = self::findBestL10NOption($data['name'], $lang);
1212
-		}
1213
-		if ($lang && isset($data['summary']) && is_array($data['summary'])) {
1214
-			$data['summary'] = self::findBestL10NOption($data['summary'], $lang);
1215
-		}
1216
-		if ($lang && isset($data['description']) && is_array($data['description'])) {
1217
-			$data['description'] = trim(self::findBestL10NOption($data['description'], $lang));
1218
-		} else if (isset($data['description']) && is_string($data['description'])) {
1219
-			$data['description'] = trim($data['description']);
1220
-		} else  {
1221
-			$data['description'] = '';
1222
-		}
1223
-
1224
-		return $data;
1225
-	}
1226
-
1227
-	/**
1228
-	 * @param \OCP\IConfig $config
1229
-	 * @param \OCP\IL10N $l
1230
-	 * @param array $info
1231
-	 * @throws \Exception
1232
-	 */
1233
-	public static function checkAppDependencies($config, $l, $info) {
1234
-		$dependencyAnalyzer = new DependencyAnalyzer(new Platform($config), $l);
1235
-		$missing = $dependencyAnalyzer->analyze($info);
1236
-		if (!empty($missing)) {
1237
-			$missingMsg = join(PHP_EOL, $missing);
1238
-			throw new \Exception(
1239
-				$l->t('App "%s" cannot be installed because the following dependencies are not fulfilled: %s',
1240
-					[$info['name'], $missingMsg]
1241
-				)
1242
-			);
1243
-		}
1244
-	}
64
+    static private $appVersion = [];
65
+    static private $adminForms = array();
66
+    static private $personalForms = array();
67
+    static private $appInfo = array();
68
+    static private $appTypes = array();
69
+    static private $loadedApps = array();
70
+    static private $altLogin = array();
71
+    static private $alreadyRegistered = [];
72
+    const officialApp = 200;
73
+
74
+    /**
75
+     * clean the appId
76
+     *
77
+     * @param string|boolean $app AppId that needs to be cleaned
78
+     * @return string
79
+     */
80
+    public static function cleanAppId($app) {
81
+        return str_replace(array('\0', '/', '\\', '..'), '', $app);
82
+    }
83
+
84
+    /**
85
+     * Check if an app is loaded
86
+     *
87
+     * @param string $app
88
+     * @return bool
89
+     */
90
+    public static function isAppLoaded($app) {
91
+        return in_array($app, self::$loadedApps, true);
92
+    }
93
+
94
+    /**
95
+     * loads all apps
96
+     *
97
+     * @param string[] | string | null $types
98
+     * @return bool
99
+     *
100
+     * This function walks through the ownCloud directory and loads all apps
101
+     * it can find. A directory contains an app if the file /appinfo/info.xml
102
+     * exists.
103
+     *
104
+     * if $types is set, only apps of those types will be loaded
105
+     */
106
+    public static function loadApps($types = null) {
107
+        if (\OC::$server->getSystemConfig()->getValue('maintenance', false)) {
108
+            return false;
109
+        }
110
+        // Load the enabled apps here
111
+        $apps = self::getEnabledApps();
112
+
113
+        // Add each apps' folder as allowed class path
114
+        foreach($apps as $app) {
115
+            $path = self::getAppPath($app);
116
+            if($path !== false) {
117
+                self::registerAutoloading($app, $path);
118
+            }
119
+        }
120
+
121
+        // prevent app.php from printing output
122
+        ob_start();
123
+        foreach ($apps as $app) {
124
+            if ((is_null($types) or self::isType($app, $types)) && !in_array($app, self::$loadedApps)) {
125
+                self::loadApp($app);
126
+            }
127
+        }
128
+        ob_end_clean();
129
+
130
+        return true;
131
+    }
132
+
133
+    /**
134
+     * load a single app
135
+     *
136
+     * @param string $app
137
+     */
138
+    public static function loadApp($app) {
139
+        self::$loadedApps[] = $app;
140
+        $appPath = self::getAppPath($app);
141
+        if($appPath === false) {
142
+            return;
143
+        }
144
+
145
+        // in case someone calls loadApp() directly
146
+        self::registerAutoloading($app, $appPath);
147
+
148
+        if (is_file($appPath . '/appinfo/app.php')) {
149
+            \OC::$server->getEventLogger()->start('load_app_' . $app, 'Load app: ' . $app);
150
+            self::requireAppFile($app);
151
+            if (self::isType($app, array('authentication'))) {
152
+                // since authentication apps affect the "is app enabled for group" check,
153
+                // the enabled apps cache needs to be cleared to make sure that the
154
+                // next time getEnableApps() is called it will also include apps that were
155
+                // enabled for groups
156
+                self::$enabledAppsCache = array();
157
+            }
158
+            \OC::$server->getEventLogger()->end('load_app_' . $app);
159
+        }
160
+
161
+        $info = self::getAppInfo($app);
162
+        if (!empty($info['activity']['filters'])) {
163
+            foreach ($info['activity']['filters'] as $filter) {
164
+                \OC::$server->getActivityManager()->registerFilter($filter);
165
+            }
166
+        }
167
+        if (!empty($info['activity']['settings'])) {
168
+            foreach ($info['activity']['settings'] as $setting) {
169
+                \OC::$server->getActivityManager()->registerSetting($setting);
170
+            }
171
+        }
172
+        if (!empty($info['activity']['providers'])) {
173
+            foreach ($info['activity']['providers'] as $provider) {
174
+                \OC::$server->getActivityManager()->registerProvider($provider);
175
+            }
176
+        }
177
+    }
178
+
179
+    /**
180
+     * @internal
181
+     * @param string $app
182
+     * @param string $path
183
+     */
184
+    public static function registerAutoloading($app, $path) {
185
+        $key = $app . '-' . $path;
186
+        if(isset(self::$alreadyRegistered[$key])) {
187
+            return;
188
+        }
189
+        self::$alreadyRegistered[$key] = true;
190
+        // Register on PSR-4 composer autoloader
191
+        $appNamespace = \OC\AppFramework\App::buildAppNamespace($app);
192
+        \OC::$server->registerNamespace($app, $appNamespace);
193
+        \OC::$composerAutoloader->addPsr4($appNamespace . '\\', $path . '/lib/', true);
194
+        if (defined('PHPUNIT_RUN') || defined('CLI_TEST_RUN')) {
195
+            \OC::$composerAutoloader->addPsr4($appNamespace . '\\Tests\\', $path . '/tests/', true);
196
+        }
197
+
198
+        // Register on legacy autoloader
199
+        \OC::$loader->addValidRoot($path);
200
+    }
201
+
202
+    /**
203
+     * Load app.php from the given app
204
+     *
205
+     * @param string $app app name
206
+     */
207
+    private static function requireAppFile($app) {
208
+        try {
209
+            // encapsulated here to avoid variable scope conflicts
210
+            require_once $app . '/appinfo/app.php';
211
+        } catch (Error $ex) {
212
+            \OC::$server->getLogger()->logException($ex);
213
+            $blacklist = \OC::$server->getAppManager()->getAlwaysEnabledApps();
214
+            if (!in_array($app, $blacklist)) {
215
+                self::disable($app);
216
+            }
217
+        }
218
+    }
219
+
220
+    /**
221
+     * check if an app is of a specific type
222
+     *
223
+     * @param string $app
224
+     * @param string|array $types
225
+     * @return bool
226
+     */
227
+    public static function isType($app, $types) {
228
+        if (is_string($types)) {
229
+            $types = array($types);
230
+        }
231
+        $appTypes = self::getAppTypes($app);
232
+        foreach ($types as $type) {
233
+            if (array_search($type, $appTypes) !== false) {
234
+                return true;
235
+            }
236
+        }
237
+        return false;
238
+    }
239
+
240
+    /**
241
+     * get the types of an app
242
+     *
243
+     * @param string $app
244
+     * @return array
245
+     */
246
+    private static function getAppTypes($app) {
247
+        //load the cache
248
+        if (count(self::$appTypes) == 0) {
249
+            self::$appTypes = \OC::$server->getAppConfig()->getValues(false, 'types');
250
+        }
251
+
252
+        if (isset(self::$appTypes[$app])) {
253
+            return explode(',', self::$appTypes[$app]);
254
+        } else {
255
+            return array();
256
+        }
257
+    }
258
+
259
+    /**
260
+     * read app types from info.xml and cache them in the database
261
+     */
262
+    public static function setAppTypes($app) {
263
+        $appData = self::getAppInfo($app);
264
+        if(!is_array($appData)) {
265
+            return;
266
+        }
267
+
268
+        if (isset($appData['types'])) {
269
+            $appTypes = implode(',', $appData['types']);
270
+        } else {
271
+            $appTypes = '';
272
+            $appData['types'] = [];
273
+        }
274
+
275
+        \OC::$server->getAppConfig()->setValue($app, 'types', $appTypes);
276
+
277
+        if (\OC::$server->getAppManager()->hasProtectedAppType($appData['types'])) {
278
+            $enabled = \OC::$server->getAppConfig()->getValue($app, 'enabled', 'yes');
279
+            if ($enabled !== 'yes' && $enabled !== 'no') {
280
+                \OC::$server->getAppConfig()->setValue($app, 'enabled', 'yes');
281
+            }
282
+        }
283
+    }
284
+
285
+    /**
286
+     * check if app is shipped
287
+     *
288
+     * @param string $appId the id of the app to check
289
+     * @return bool
290
+     *
291
+     * Check if an app that is installed is a shipped app or installed from the appstore.
292
+     */
293
+    public static function isShipped($appId) {
294
+        return \OC::$server->getAppManager()->isShipped($appId);
295
+    }
296
+
297
+    /**
298
+     * get all enabled apps
299
+     */
300
+    protected static $enabledAppsCache = array();
301
+
302
+    /**
303
+     * Returns apps enabled for the current user.
304
+     *
305
+     * @param bool $forceRefresh whether to refresh the cache
306
+     * @param bool $all whether to return apps for all users, not only the
307
+     * currently logged in one
308
+     * @return string[]
309
+     */
310
+    public static function getEnabledApps($forceRefresh = false, $all = false) {
311
+        if (!\OC::$server->getSystemConfig()->getValue('installed', false)) {
312
+            return array();
313
+        }
314
+        // in incognito mode or when logged out, $user will be false,
315
+        // which is also the case during an upgrade
316
+        $appManager = \OC::$server->getAppManager();
317
+        if ($all) {
318
+            $user = null;
319
+        } else {
320
+            $user = \OC::$server->getUserSession()->getUser();
321
+        }
322
+
323
+        if (is_null($user)) {
324
+            $apps = $appManager->getInstalledApps();
325
+        } else {
326
+            $apps = $appManager->getEnabledAppsForUser($user);
327
+        }
328
+        $apps = array_filter($apps, function ($app) {
329
+            return $app !== 'files';//we add this manually
330
+        });
331
+        sort($apps);
332
+        array_unshift($apps, 'files');
333
+        return $apps;
334
+    }
335
+
336
+    /**
337
+     * checks whether or not an app is enabled
338
+     *
339
+     * @param string $app app
340
+     * @return bool
341
+     *
342
+     * This function checks whether or not an app is enabled.
343
+     */
344
+    public static function isEnabled($app) {
345
+        return \OC::$server->getAppManager()->isEnabledForUser($app);
346
+    }
347
+
348
+    /**
349
+     * enables an app
350
+     *
351
+     * @param string $appId
352
+     * @param array $groups (optional) when set, only these groups will have access to the app
353
+     * @throws \Exception
354
+     * @return void
355
+     *
356
+     * This function set an app as enabled in appconfig.
357
+     */
358
+    public function enable($appId,
359
+                            $groups = null) {
360
+        self::$enabledAppsCache = []; // flush
361
+
362
+        // Check if app is already downloaded
363
+        $installer = new Installer(
364
+            \OC::$server->getAppFetcher(),
365
+            \OC::$server->getHTTPClientService(),
366
+            \OC::$server->getTempManager(),
367
+            \OC::$server->getLogger(),
368
+            \OC::$server->getConfig()
369
+        );
370
+        $isDownloaded = $installer->isDownloaded($appId);
371
+
372
+        if(!$isDownloaded) {
373
+            $installer->downloadApp($appId);
374
+        }
375
+
376
+        $installer->installApp($appId);
377
+
378
+        $appManager = \OC::$server->getAppManager();
379
+        if (!is_null($groups)) {
380
+            $groupManager = \OC::$server->getGroupManager();
381
+            $groupsList = [];
382
+            foreach ($groups as $group) {
383
+                $groupItem = $groupManager->get($group);
384
+                if ($groupItem instanceof \OCP\IGroup) {
385
+                    $groupsList[] = $groupManager->get($group);
386
+                }
387
+            }
388
+            $appManager->enableAppForGroups($appId, $groupsList);
389
+        } else {
390
+            $appManager->enableApp($appId);
391
+        }
392
+    }
393
+
394
+    /**
395
+     * @param string $app
396
+     * @return bool
397
+     */
398
+    public static function removeApp($app) {
399
+        if (self::isShipped($app)) {
400
+            return false;
401
+        }
402
+
403
+        $installer = new Installer(
404
+            \OC::$server->getAppFetcher(),
405
+            \OC::$server->getHTTPClientService(),
406
+            \OC::$server->getTempManager(),
407
+            \OC::$server->getLogger(),
408
+            \OC::$server->getConfig()
409
+        );
410
+        return $installer->removeApp($app);
411
+    }
412
+
413
+    /**
414
+     * This function set an app as disabled in appconfig.
415
+     *
416
+     * @param string $app app
417
+     * @throws Exception
418
+     */
419
+    public static function disable($app) {
420
+        // flush
421
+        self::$enabledAppsCache = array();
422
+
423
+        $appManager = \OC::$server->getAppManager();
424
+        $appManager->disableApp($app);
425
+    }
426
+
427
+    // This is private as well. It simply works, so don't ask for more details
428
+    private static function proceedNavigation($list) {
429
+        usort($list, function($a, $b) {
430
+            if (isset($a['order']) && isset($b['order'])) {
431
+                return ($a['order'] < $b['order']) ? -1 : 1;
432
+            } else if (isset($a['order']) || isset($b['order'])) {
433
+                return isset($a['order']) ? -1 : 1;
434
+            } else {
435
+                return ($a['name'] < $b['name']) ? -1 : 1;
436
+            }
437
+        });
438
+
439
+        $activeApp = OC::$server->getNavigationManager()->getActiveEntry();
440
+        foreach ($list as $index => &$navEntry) {
441
+            if ($navEntry['id'] == $activeApp) {
442
+                $navEntry['active'] = true;
443
+            } else {
444
+                $navEntry['active'] = false;
445
+            }
446
+        }
447
+        unset($navEntry);
448
+
449
+        return $list;
450
+    }
451
+
452
+    /**
453
+     * Get the path where to install apps
454
+     *
455
+     * @return string|false
456
+     */
457
+    public static function getInstallPath() {
458
+        if (\OC::$server->getSystemConfig()->getValue('appstoreenabled', true) == false) {
459
+            return false;
460
+        }
461
+
462
+        foreach (OC::$APPSROOTS as $dir) {
463
+            if (isset($dir['writable']) && $dir['writable'] === true) {
464
+                return $dir['path'];
465
+            }
466
+        }
467
+
468
+        \OCP\Util::writeLog('core', 'No application directories are marked as writable.', \OCP\Util::ERROR);
469
+        return null;
470
+    }
471
+
472
+
473
+    /**
474
+     * search for an app in all app-directories
475
+     *
476
+     * @param string $appId
477
+     * @return false|string
478
+     */
479
+    public static function findAppInDirectories($appId) {
480
+        $sanitizedAppId = self::cleanAppId($appId);
481
+        if($sanitizedAppId !== $appId) {
482
+            return false;
483
+        }
484
+        static $app_dir = array();
485
+
486
+        if (isset($app_dir[$appId])) {
487
+            return $app_dir[$appId];
488
+        }
489
+
490
+        $possibleApps = array();
491
+        foreach (OC::$APPSROOTS as $dir) {
492
+            if (file_exists($dir['path'] . '/' . $appId)) {
493
+                $possibleApps[] = $dir;
494
+            }
495
+        }
496
+
497
+        if (empty($possibleApps)) {
498
+            return false;
499
+        } elseif (count($possibleApps) === 1) {
500
+            $dir = array_shift($possibleApps);
501
+            $app_dir[$appId] = $dir;
502
+            return $dir;
503
+        } else {
504
+            $versionToLoad = array();
505
+            foreach ($possibleApps as $possibleApp) {
506
+                $version = self::getAppVersionByPath($possibleApp['path']);
507
+                if (empty($versionToLoad) || version_compare($version, $versionToLoad['version'], '>')) {
508
+                    $versionToLoad = array(
509
+                        'dir' => $possibleApp,
510
+                        'version' => $version,
511
+                    );
512
+                }
513
+            }
514
+            $app_dir[$appId] = $versionToLoad['dir'];
515
+            return $versionToLoad['dir'];
516
+            //TODO - write test
517
+        }
518
+    }
519
+
520
+    /**
521
+     * Get the directory for the given app.
522
+     * If the app is defined in multiple directories, the first one is taken. (false if not found)
523
+     *
524
+     * @param string $appId
525
+     * @return string|false
526
+     */
527
+    public static function getAppPath($appId) {
528
+        if ($appId === null || trim($appId) === '') {
529
+            return false;
530
+        }
531
+
532
+        if (($dir = self::findAppInDirectories($appId)) != false) {
533
+            return $dir['path'] . '/' . $appId;
534
+        }
535
+        return false;
536
+    }
537
+
538
+    /**
539
+     * Get the path for the given app on the access
540
+     * If the app is defined in multiple directories, the first one is taken. (false if not found)
541
+     *
542
+     * @param string $appId
543
+     * @return string|false
544
+     */
545
+    public static function getAppWebPath($appId) {
546
+        if (($dir = self::findAppInDirectories($appId)) != false) {
547
+            return OC::$WEBROOT . $dir['url'] . '/' . $appId;
548
+        }
549
+        return false;
550
+    }
551
+
552
+    /**
553
+     * get the last version of the app from appinfo/info.xml
554
+     *
555
+     * @param string $appId
556
+     * @param bool $useCache
557
+     * @return string
558
+     */
559
+    public static function getAppVersion($appId, $useCache = true) {
560
+        if($useCache && isset(self::$appVersion[$appId])) {
561
+            return self::$appVersion[$appId];
562
+        }
563
+
564
+        $file = self::getAppPath($appId);
565
+        self::$appVersion[$appId] = ($file !== false) ? self::getAppVersionByPath($file) : '0';
566
+        return self::$appVersion[$appId];
567
+    }
568
+
569
+    /**
570
+     * get app's version based on it's path
571
+     *
572
+     * @param string $path
573
+     * @return string
574
+     */
575
+    public static function getAppVersionByPath($path) {
576
+        $infoFile = $path . '/appinfo/info.xml';
577
+        $appData = self::getAppInfo($infoFile, true);
578
+        return isset($appData['version']) ? $appData['version'] : '';
579
+    }
580
+
581
+
582
+    /**
583
+     * Read all app metadata from the info.xml file
584
+     *
585
+     * @param string $appId id of the app or the path of the info.xml file
586
+     * @param bool $path
587
+     * @param string $lang
588
+     * @return array|null
589
+     * @note all data is read from info.xml, not just pre-defined fields
590
+     */
591
+    public static function getAppInfo($appId, $path = false, $lang = null) {
592
+        if ($path) {
593
+            $file = $appId;
594
+        } else {
595
+            if ($lang === null && isset(self::$appInfo[$appId])) {
596
+                return self::$appInfo[$appId];
597
+            }
598
+            $appPath = self::getAppPath($appId);
599
+            if($appPath === false) {
600
+                return null;
601
+            }
602
+            $file = $appPath . '/appinfo/info.xml';
603
+        }
604
+
605
+        $parser = new InfoParser(\OC::$server->getMemCacheFactory()->create('core.appinfo'));
606
+        $data = $parser->parse($file);
607
+
608
+        if (is_array($data)) {
609
+            $data = OC_App::parseAppInfo($data, $lang);
610
+        }
611
+        if(isset($data['ocsid'])) {
612
+            $storedId = \OC::$server->getConfig()->getAppValue($appId, 'ocsid');
613
+            if($storedId !== '' && $storedId !== $data['ocsid']) {
614
+                $data['ocsid'] = $storedId;
615
+            }
616
+        }
617
+
618
+        if ($lang === null) {
619
+            self::$appInfo[$appId] = $data;
620
+        }
621
+
622
+        return $data;
623
+    }
624
+
625
+    /**
626
+     * Returns the navigation
627
+     *
628
+     * @return array
629
+     *
630
+     * This function returns an array containing all entries added. The
631
+     * entries are sorted by the key 'order' ascending. Additional to the keys
632
+     * given for each app the following keys exist:
633
+     *   - active: boolean, signals if the user is on this navigation entry
634
+     */
635
+    public static function getNavigation() {
636
+        $entries = OC::$server->getNavigationManager()->getAll();
637
+        return self::proceedNavigation($entries);
638
+    }
639
+
640
+    /**
641
+     * Returns the Settings Navigation
642
+     *
643
+     * @return string[]
644
+     *
645
+     * This function returns an array containing all settings pages added. The
646
+     * entries are sorted by the key 'order' ascending.
647
+     */
648
+    public static function getSettingsNavigation() {
649
+        $entries = OC::$server->getNavigationManager()->getAll('settings');
650
+        return self::proceedNavigation($entries);
651
+    }
652
+
653
+    /**
654
+     * get the id of loaded app
655
+     *
656
+     * @return string
657
+     */
658
+    public static function getCurrentApp() {
659
+        $request = \OC::$server->getRequest();
660
+        $script = substr($request->getScriptName(), strlen(OC::$WEBROOT) + 1);
661
+        $topFolder = substr($script, 0, strpos($script, '/'));
662
+        if (empty($topFolder)) {
663
+            $path_info = $request->getPathInfo();
664
+            if ($path_info) {
665
+                $topFolder = substr($path_info, 1, strpos($path_info, '/', 1) - 1);
666
+            }
667
+        }
668
+        if ($topFolder == 'apps') {
669
+            $length = strlen($topFolder);
670
+            return substr($script, $length + 1, strpos($script, '/', $length + 1) - $length - 1);
671
+        } else {
672
+            return $topFolder;
673
+        }
674
+    }
675
+
676
+    /**
677
+     * @param string $type
678
+     * @return array
679
+     */
680
+    public static function getForms($type) {
681
+        $forms = array();
682
+        switch ($type) {
683
+            case 'admin':
684
+                $source = self::$adminForms;
685
+                break;
686
+            case 'personal':
687
+                $source = self::$personalForms;
688
+                break;
689
+            default:
690
+                return array();
691
+        }
692
+        foreach ($source as $form) {
693
+            $forms[] = include $form;
694
+        }
695
+        return $forms;
696
+    }
697
+
698
+    /**
699
+     * register an admin form to be shown
700
+     *
701
+     * @param string $app
702
+     * @param string $page
703
+     */
704
+    public static function registerAdmin($app, $page) {
705
+        self::$adminForms[] = $app . '/' . $page . '.php';
706
+    }
707
+
708
+    /**
709
+     * register a personal form to be shown
710
+     * @param string $app
711
+     * @param string $page
712
+     */
713
+    public static function registerPersonal($app, $page) {
714
+        self::$personalForms[] = $app . '/' . $page . '.php';
715
+    }
716
+
717
+    /**
718
+     * @param array $entry
719
+     */
720
+    public static function registerLogIn(array $entry) {
721
+        self::$altLogin[] = $entry;
722
+    }
723
+
724
+    /**
725
+     * @return array
726
+     */
727
+    public static function getAlternativeLogIns() {
728
+        return self::$altLogin;
729
+    }
730
+
731
+    /**
732
+     * get a list of all apps in the apps folder
733
+     *
734
+     * @return array an array of app names (string IDs)
735
+     * @todo: change the name of this method to getInstalledApps, which is more accurate
736
+     */
737
+    public static function getAllApps() {
738
+
739
+        $apps = array();
740
+
741
+        foreach (OC::$APPSROOTS as $apps_dir) {
742
+            if (!is_readable($apps_dir['path'])) {
743
+                \OCP\Util::writeLog('core', 'unable to read app folder : ' . $apps_dir['path'], \OCP\Util::WARN);
744
+                continue;
745
+            }
746
+            $dh = opendir($apps_dir['path']);
747
+
748
+            if (is_resource($dh)) {
749
+                while (($file = readdir($dh)) !== false) {
750
+
751
+                    if ($file[0] != '.' and is_dir($apps_dir['path'] . '/' . $file) and is_file($apps_dir['path'] . '/' . $file . '/appinfo/info.xml')) {
752
+
753
+                        $apps[] = $file;
754
+                    }
755
+                }
756
+            }
757
+        }
758
+
759
+        return $apps;
760
+    }
761
+
762
+    /**
763
+     * List all apps, this is used in apps.php
764
+     *
765
+     * @return array
766
+     */
767
+    public function listAllApps() {
768
+        $installedApps = OC_App::getAllApps();
769
+
770
+        //we don't want to show configuration for these
771
+        $blacklist = \OC::$server->getAppManager()->getAlwaysEnabledApps();
772
+        $appList = array();
773
+        $langCode = \OC::$server->getL10N('core')->getLanguageCode();
774
+        $urlGenerator = \OC::$server->getURLGenerator();
775
+
776
+        foreach ($installedApps as $app) {
777
+            if (array_search($app, $blacklist) === false) {
778
+
779
+                $info = OC_App::getAppInfo($app, false, $langCode);
780
+                if (!is_array($info)) {
781
+                    \OCP\Util::writeLog('core', 'Could not read app info file for app "' . $app . '"', \OCP\Util::ERROR);
782
+                    continue;
783
+                }
784
+
785
+                if (!isset($info['name'])) {
786
+                    \OCP\Util::writeLog('core', 'App id "' . $app . '" has no name in appinfo', \OCP\Util::ERROR);
787
+                    continue;
788
+                }
789
+
790
+                $enabled = \OC::$server->getAppConfig()->getValue($app, 'enabled', 'no');
791
+                $info['groups'] = null;
792
+                if ($enabled === 'yes') {
793
+                    $active = true;
794
+                } else if ($enabled === 'no') {
795
+                    $active = false;
796
+                } else {
797
+                    $active = true;
798
+                    $info['groups'] = $enabled;
799
+                }
800
+
801
+                $info['active'] = $active;
802
+
803
+                if (self::isShipped($app)) {
804
+                    $info['internal'] = true;
805
+                    $info['level'] = self::officialApp;
806
+                    $info['removable'] = false;
807
+                } else {
808
+                    $info['internal'] = false;
809
+                    $info['removable'] = true;
810
+                }
811
+
812
+                $appPath = self::getAppPath($app);
813
+                if($appPath !== false) {
814
+                    $appIcon = $appPath . '/img/' . $app . '.svg';
815
+                    if (file_exists($appIcon)) {
816
+                        $info['preview'] = \OC::$server->getURLGenerator()->imagePath($app, $app . '.svg');
817
+                        $info['previewAsIcon'] = true;
818
+                    } else {
819
+                        $appIcon = $appPath . '/img/app.svg';
820
+                        if (file_exists($appIcon)) {
821
+                            $info['preview'] = \OC::$server->getURLGenerator()->imagePath($app, 'app.svg');
822
+                            $info['previewAsIcon'] = true;
823
+                        }
824
+                    }
825
+                }
826
+                // fix documentation
827
+                if (isset($info['documentation']) && is_array($info['documentation'])) {
828
+                    foreach ($info['documentation'] as $key => $url) {
829
+                        // If it is not an absolute URL we assume it is a key
830
+                        // i.e. admin-ldap will get converted to go.php?to=admin-ldap
831
+                        if (stripos($url, 'https://') !== 0 && stripos($url, 'http://') !== 0) {
832
+                            $url = $urlGenerator->linkToDocs($url);
833
+                        }
834
+
835
+                        $info['documentation'][$key] = $url;
836
+                    }
837
+                }
838
+
839
+                $info['version'] = OC_App::getAppVersion($app);
840
+                $appList[] = $info;
841
+            }
842
+        }
843
+
844
+        return $appList;
845
+    }
846
+
847
+    /**
848
+     * Returns the internal app ID or false
849
+     * @param string $ocsID
850
+     * @return string|false
851
+     */
852
+    public static function getInternalAppIdByOcs($ocsID) {
853
+        if(is_numeric($ocsID)) {
854
+            $idArray = \OC::$server->getAppConfig()->getValues(false, 'ocsid');
855
+            if(array_search($ocsID, $idArray)) {
856
+                return array_search($ocsID, $idArray);
857
+            }
858
+        }
859
+        return false;
860
+    }
861
+
862
+    public static function shouldUpgrade($app) {
863
+        $versions = self::getAppVersions();
864
+        $currentVersion = OC_App::getAppVersion($app);
865
+        if ($currentVersion && isset($versions[$app])) {
866
+            $installedVersion = $versions[$app];
867
+            if (!version_compare($currentVersion, $installedVersion, '=')) {
868
+                return true;
869
+            }
870
+        }
871
+        return false;
872
+    }
873
+
874
+    /**
875
+     * Adjust the number of version parts of $version1 to match
876
+     * the number of version parts of $version2.
877
+     *
878
+     * @param string $version1 version to adjust
879
+     * @param string $version2 version to take the number of parts from
880
+     * @return string shortened $version1
881
+     */
882
+    private static function adjustVersionParts($version1, $version2) {
883
+        $version1 = explode('.', $version1);
884
+        $version2 = explode('.', $version2);
885
+        // reduce $version1 to match the number of parts in $version2
886
+        while (count($version1) > count($version2)) {
887
+            array_pop($version1);
888
+        }
889
+        // if $version1 does not have enough parts, add some
890
+        while (count($version1) < count($version2)) {
891
+            $version1[] = '0';
892
+        }
893
+        return implode('.', $version1);
894
+    }
895
+
896
+    /**
897
+     * Check whether the current ownCloud version matches the given
898
+     * application's version requirements.
899
+     *
900
+     * The comparison is made based on the number of parts that the
901
+     * app info version has. For example for ownCloud 6.0.3 if the
902
+     * app info version is expecting version 6.0, the comparison is
903
+     * made on the first two parts of the ownCloud version.
904
+     * This means that it's possible to specify "requiremin" => 6
905
+     * and "requiremax" => 6 and it will still match ownCloud 6.0.3.
906
+     *
907
+     * @param string $ocVersion ownCloud version to check against
908
+     * @param array $appInfo app info (from xml)
909
+     *
910
+     * @return boolean true if compatible, otherwise false
911
+     */
912
+    public static function isAppCompatible($ocVersion, $appInfo) {
913
+        $requireMin = '';
914
+        $requireMax = '';
915
+        if (isset($appInfo['dependencies']['nextcloud']['@attributes']['min-version'])) {
916
+            $requireMin = $appInfo['dependencies']['nextcloud']['@attributes']['min-version'];
917
+        } elseif (isset($appInfo['dependencies']['owncloud']['@attributes']['min-version'])) {
918
+            $requireMin = $appInfo['dependencies']['owncloud']['@attributes']['min-version'];
919
+        } else if (isset($appInfo['requiremin'])) {
920
+            $requireMin = $appInfo['requiremin'];
921
+        } else if (isset($appInfo['require'])) {
922
+            $requireMin = $appInfo['require'];
923
+        }
924
+
925
+        if (isset($appInfo['dependencies']['nextcloud']['@attributes']['max-version'])) {
926
+            $requireMax = $appInfo['dependencies']['nextcloud']['@attributes']['max-version'];
927
+        } elseif (isset($appInfo['dependencies']['owncloud']['@attributes']['max-version'])) {
928
+            $requireMax = $appInfo['dependencies']['owncloud']['@attributes']['max-version'];
929
+        } else if (isset($appInfo['requiremax'])) {
930
+            $requireMax = $appInfo['requiremax'];
931
+        }
932
+
933
+        if (is_array($ocVersion)) {
934
+            $ocVersion = implode('.', $ocVersion);
935
+        }
936
+
937
+        if (!empty($requireMin)
938
+            && version_compare(self::adjustVersionParts($ocVersion, $requireMin), $requireMin, '<')
939
+        ) {
940
+
941
+            return false;
942
+        }
943
+
944
+        if (!empty($requireMax)
945
+            && version_compare(self::adjustVersionParts($ocVersion, $requireMax), $requireMax, '>')
946
+        ) {
947
+            return false;
948
+        }
949
+
950
+        return true;
951
+    }
952
+
953
+    /**
954
+     * get the installed version of all apps
955
+     */
956
+    public static function getAppVersions() {
957
+        static $versions;
958
+
959
+        if(!$versions) {
960
+            $appConfig = \OC::$server->getAppConfig();
961
+            $versions = $appConfig->getValues(false, 'installed_version');
962
+        }
963
+        return $versions;
964
+    }
965
+
966
+    /**
967
+     * @param string $app
968
+     * @param \OCP\IConfig $config
969
+     * @param \OCP\IL10N $l
970
+     * @return bool
971
+     *
972
+     * @throws Exception if app is not compatible with this version of ownCloud
973
+     * @throws Exception if no app-name was specified
974
+     */
975
+    public function installApp($app,
976
+                                \OCP\IConfig $config,
977
+                                \OCP\IL10N $l) {
978
+        if ($app !== false) {
979
+            // check if the app is compatible with this version of ownCloud
980
+            $info = self::getAppInfo($app);
981
+            if(!is_array($info)) {
982
+                throw new \Exception(
983
+                    $l->t('App "%s" cannot be installed because appinfo file cannot be read.',
984
+                        [$info['name']]
985
+                    )
986
+                );
987
+            }
988
+
989
+            $version = \OCP\Util::getVersion();
990
+            if (!self::isAppCompatible($version, $info)) {
991
+                throw new \Exception(
992
+                    $l->t('App "%s" cannot be installed because it is not compatible with this version of the server.',
993
+                        array($info['name'])
994
+                    )
995
+                );
996
+            }
997
+
998
+            // check for required dependencies
999
+            self::checkAppDependencies($config, $l, $info);
1000
+
1001
+            $config->setAppValue($app, 'enabled', 'yes');
1002
+            if (isset($appData['id'])) {
1003
+                $config->setAppValue($app, 'ocsid', $appData['id']);
1004
+            }
1005
+
1006
+            if(isset($info['settings']) && is_array($info['settings'])) {
1007
+                $appPath = self::getAppPath($app);
1008
+                self::registerAutoloading($app, $appPath);
1009
+                \OC::$server->getSettingsManager()->setupSettings($info['settings']);
1010
+            }
1011
+
1012
+            \OC_Hook::emit('OC_App', 'post_enable', array('app' => $app));
1013
+        } else {
1014
+            if(empty($appName) ) {
1015
+                throw new \Exception($l->t("No app name specified"));
1016
+            } else {
1017
+                throw new \Exception($l->t("App '%s' could not be installed!", $appName));
1018
+            }
1019
+        }
1020
+
1021
+        return $app;
1022
+    }
1023
+
1024
+    /**
1025
+     * update the database for the app and call the update script
1026
+     *
1027
+     * @param string $appId
1028
+     * @return bool
1029
+     */
1030
+    public static function updateApp($appId) {
1031
+        $appPath = self::getAppPath($appId);
1032
+        if($appPath === false) {
1033
+            return false;
1034
+        }
1035
+        self::registerAutoloading($appId, $appPath);
1036
+
1037
+        $appData = self::getAppInfo($appId);
1038
+        self::executeRepairSteps($appId, $appData['repair-steps']['pre-migration']);
1039
+
1040
+        if (file_exists($appPath . '/appinfo/database.xml')) {
1041
+            OC_DB::updateDbFromStructure($appPath . '/appinfo/database.xml');
1042
+        } else {
1043
+            $ms = new MigrationService($appId, \OC::$server->getDatabaseConnection());
1044
+            $ms->migrate();
1045
+        }
1046
+
1047
+        self::executeRepairSteps($appId, $appData['repair-steps']['post-migration']);
1048
+        self::setupLiveMigrations($appId, $appData['repair-steps']['live-migration']);
1049
+        unset(self::$appVersion[$appId]);
1050
+
1051
+        // run upgrade code
1052
+        if (file_exists($appPath . '/appinfo/update.php')) {
1053
+            self::loadApp($appId);
1054
+            include $appPath . '/appinfo/update.php';
1055
+        }
1056
+        self::setupBackgroundJobs($appData['background-jobs']);
1057
+        if(isset($appData['settings']) && is_array($appData['settings'])) {
1058
+            \OC::$server->getSettingsManager()->setupSettings($appData['settings']);
1059
+        }
1060
+
1061
+        //set remote/public handlers
1062
+        if (array_key_exists('ocsid', $appData)) {
1063
+            \OC::$server->getConfig()->setAppValue($appId, 'ocsid', $appData['ocsid']);
1064
+        } elseif(\OC::$server->getConfig()->getAppValue($appId, 'ocsid', null) !== null) {
1065
+            \OC::$server->getConfig()->deleteAppValue($appId, 'ocsid');
1066
+        }
1067
+        foreach ($appData['remote'] as $name => $path) {
1068
+            \OC::$server->getConfig()->setAppValue('core', 'remote_' . $name, $appId . '/' . $path);
1069
+        }
1070
+        foreach ($appData['public'] as $name => $path) {
1071
+            \OC::$server->getConfig()->setAppValue('core', 'public_' . $name, $appId . '/' . $path);
1072
+        }
1073
+
1074
+        self::setAppTypes($appId);
1075
+
1076
+        $version = \OC_App::getAppVersion($appId);
1077
+        \OC::$server->getAppConfig()->setValue($appId, 'installed_version', $version);
1078
+
1079
+        \OC::$server->getEventDispatcher()->dispatch(ManagerEvent::EVENT_APP_UPDATE, new ManagerEvent(
1080
+            ManagerEvent::EVENT_APP_UPDATE, $appId
1081
+        ));
1082
+
1083
+        return true;
1084
+    }
1085
+
1086
+    /**
1087
+     * @param string $appId
1088
+     * @param string[] $steps
1089
+     * @throws \OC\NeedsUpdateException
1090
+     */
1091
+    public static function executeRepairSteps($appId, array $steps) {
1092
+        if (empty($steps)) {
1093
+            return;
1094
+        }
1095
+        // load the app
1096
+        self::loadApp($appId);
1097
+
1098
+        $dispatcher = OC::$server->getEventDispatcher();
1099
+
1100
+        // load the steps
1101
+        $r = new Repair([], $dispatcher);
1102
+        foreach ($steps as $step) {
1103
+            try {
1104
+                $r->addStep($step);
1105
+            } catch (Exception $ex) {
1106
+                $r->emit('\OC\Repair', 'error', [$ex->getMessage()]);
1107
+                \OC::$server->getLogger()->logException($ex);
1108
+            }
1109
+        }
1110
+        // run the steps
1111
+        $r->run();
1112
+    }
1113
+
1114
+    public static function setupBackgroundJobs(array $jobs) {
1115
+        $queue = \OC::$server->getJobList();
1116
+        foreach ($jobs as $job) {
1117
+            $queue->add($job);
1118
+        }
1119
+    }
1120
+
1121
+    /**
1122
+     * @param string $appId
1123
+     * @param string[] $steps
1124
+     */
1125
+    private static function setupLiveMigrations($appId, array $steps) {
1126
+        $queue = \OC::$server->getJobList();
1127
+        foreach ($steps as $step) {
1128
+            $queue->add('OC\Migration\BackgroundRepair', [
1129
+                'app' => $appId,
1130
+                'step' => $step]);
1131
+        }
1132
+    }
1133
+
1134
+    /**
1135
+     * @param string $appId
1136
+     * @return \OC\Files\View|false
1137
+     */
1138
+    public static function getStorage($appId) {
1139
+        if (OC_App::isEnabled($appId)) { //sanity check
1140
+            if (\OC::$server->getUserSession()->isLoggedIn()) {
1141
+                $view = new \OC\Files\View('/' . OC_User::getUser());
1142
+                if (!$view->file_exists($appId)) {
1143
+                    $view->mkdir($appId);
1144
+                }
1145
+                return new \OC\Files\View('/' . OC_User::getUser() . '/' . $appId);
1146
+            } else {
1147
+                \OCP\Util::writeLog('core', 'Can\'t get app storage, app ' . $appId . ', user not logged in', \OCP\Util::ERROR);
1148
+                return false;
1149
+            }
1150
+        } else {
1151
+            \OCP\Util::writeLog('core', 'Can\'t get app storage, app ' . $appId . ' not enabled', \OCP\Util::ERROR);
1152
+            return false;
1153
+        }
1154
+    }
1155
+
1156
+    protected static function findBestL10NOption($options, $lang) {
1157
+        $fallback = $similarLangFallback = $englishFallback = false;
1158
+
1159
+        $lang = strtolower($lang);
1160
+        $similarLang = $lang;
1161
+        if (strpos($similarLang, '_')) {
1162
+            // For "de_DE" we want to find "de" and the other way around
1163
+            $similarLang = substr($lang, 0, strpos($lang, '_'));
1164
+        }
1165
+
1166
+        foreach ($options as $option) {
1167
+            if (is_array($option)) {
1168
+                if ($fallback === false) {
1169
+                    $fallback = $option['@value'];
1170
+                }
1171
+
1172
+                if (!isset($option['@attributes']['lang'])) {
1173
+                    continue;
1174
+                }
1175
+
1176
+                $attributeLang = strtolower($option['@attributes']['lang']);
1177
+                if ($attributeLang === $lang) {
1178
+                    return $option['@value'];
1179
+                }
1180
+
1181
+                if ($attributeLang === $similarLang) {
1182
+                    $similarLangFallback = $option['@value'];
1183
+                } else if (strpos($attributeLang, $similarLang . '_') === 0) {
1184
+                    if ($similarLangFallback === false) {
1185
+                        $similarLangFallback =  $option['@value'];
1186
+                    }
1187
+                }
1188
+            } else {
1189
+                $englishFallback = $option;
1190
+            }
1191
+        }
1192
+
1193
+        if ($similarLangFallback !== false) {
1194
+            return $similarLangFallback;
1195
+        } else if ($englishFallback !== false) {
1196
+            return $englishFallback;
1197
+        }
1198
+        return (string) $fallback;
1199
+    }
1200
+
1201
+    /**
1202
+     * parses the app data array and enhanced the 'description' value
1203
+     *
1204
+     * @param array $data the app data
1205
+     * @param string $lang
1206
+     * @return array improved app data
1207
+     */
1208
+    public static function parseAppInfo(array $data, $lang = null) {
1209
+
1210
+        if ($lang && isset($data['name']) && is_array($data['name'])) {
1211
+            $data['name'] = self::findBestL10NOption($data['name'], $lang);
1212
+        }
1213
+        if ($lang && isset($data['summary']) && is_array($data['summary'])) {
1214
+            $data['summary'] = self::findBestL10NOption($data['summary'], $lang);
1215
+        }
1216
+        if ($lang && isset($data['description']) && is_array($data['description'])) {
1217
+            $data['description'] = trim(self::findBestL10NOption($data['description'], $lang));
1218
+        } else if (isset($data['description']) && is_string($data['description'])) {
1219
+            $data['description'] = trim($data['description']);
1220
+        } else  {
1221
+            $data['description'] = '';
1222
+        }
1223
+
1224
+        return $data;
1225
+    }
1226
+
1227
+    /**
1228
+     * @param \OCP\IConfig $config
1229
+     * @param \OCP\IL10N $l
1230
+     * @param array $info
1231
+     * @throws \Exception
1232
+     */
1233
+    public static function checkAppDependencies($config, $l, $info) {
1234
+        $dependencyAnalyzer = new DependencyAnalyzer(new Platform($config), $l);
1235
+        $missing = $dependencyAnalyzer->analyze($info);
1236
+        if (!empty($missing)) {
1237
+            $missingMsg = join(PHP_EOL, $missing);
1238
+            throw new \Exception(
1239
+                $l->t('App "%s" cannot be installed because the following dependencies are not fulfilled: %s',
1240
+                    [$info['name'], $missingMsg]
1241
+                )
1242
+            );
1243
+        }
1244
+    }
1245 1245
 }
Please login to merge, or discard this patch.
lib/private/App/AppManager.php 1 patch
Indentation   +373 added lines, -373 removed lines patch added patch discarded remove patch
@@ -43,377 +43,377 @@
 block discarded – undo
43 43
 
44 44
 class AppManager implements IAppManager {
45 45
 
46
-	/**
47
-	 * Apps with these types can not be enabled for certain groups only
48
-	 * @var string[]
49
-	 */
50
-	protected $protectedAppTypes = [
51
-		'filesystem',
52
-		'prelogin',
53
-		'authentication',
54
-		'logging',
55
-		'prevent_group_restriction',
56
-	];
57
-
58
-	/** @var IUserSession */
59
-	private $userSession;
60
-
61
-	/** @var IAppConfig */
62
-	private $appConfig;
63
-
64
-	/** @var IGroupManager */
65
-	private $groupManager;
66
-
67
-	/** @var ICacheFactory */
68
-	private $memCacheFactory;
69
-
70
-	/** @var EventDispatcherInterface */
71
-	private $dispatcher;
72
-
73
-	/** @var string[] $appId => $enabled */
74
-	private $installedAppsCache;
75
-
76
-	/** @var string[] */
77
-	private $shippedApps;
78
-
79
-	/** @var string[] */
80
-	private $alwaysEnabled;
81
-
82
-	/**
83
-	 * @param IUserSession $userSession
84
-	 * @param IAppConfig $appConfig
85
-	 * @param IGroupManager $groupManager
86
-	 * @param ICacheFactory $memCacheFactory
87
-	 * @param EventDispatcherInterface $dispatcher
88
-	 */
89
-	public function __construct(IUserSession $userSession,
90
-								IAppConfig $appConfig,
91
-								IGroupManager $groupManager,
92
-								ICacheFactory $memCacheFactory,
93
-								EventDispatcherInterface $dispatcher) {
94
-		$this->userSession = $userSession;
95
-		$this->appConfig = $appConfig;
96
-		$this->groupManager = $groupManager;
97
-		$this->memCacheFactory = $memCacheFactory;
98
-		$this->dispatcher = $dispatcher;
99
-	}
100
-
101
-	/**
102
-	 * @return string[] $appId => $enabled
103
-	 */
104
-	private function getInstalledAppsValues() {
105
-		if (!$this->installedAppsCache) {
106
-			$values = $this->appConfig->getValues(false, 'enabled');
107
-
108
-			$alwaysEnabledApps = $this->getAlwaysEnabledApps();
109
-			foreach($alwaysEnabledApps as $appId) {
110
-				$values[$appId] = 'yes';
111
-			}
112
-
113
-			$this->installedAppsCache = array_filter($values, function ($value) {
114
-				return $value !== 'no';
115
-			});
116
-			ksort($this->installedAppsCache);
117
-		}
118
-		return $this->installedAppsCache;
119
-	}
120
-
121
-	/**
122
-	 * List all installed apps
123
-	 *
124
-	 * @return string[]
125
-	 */
126
-	public function getInstalledApps() {
127
-		return array_keys($this->getInstalledAppsValues());
128
-	}
129
-
130
-	/**
131
-	 * List all apps enabled for a user
132
-	 *
133
-	 * @param \OCP\IUser $user
134
-	 * @return string[]
135
-	 */
136
-	public function getEnabledAppsForUser(IUser $user) {
137
-		$apps = $this->getInstalledAppsValues();
138
-		$appsForUser = array_filter($apps, function ($enabled) use ($user) {
139
-			return $this->checkAppForUser($enabled, $user);
140
-		});
141
-		return array_keys($appsForUser);
142
-	}
143
-
144
-	/**
145
-	 * Check if an app is enabled for user
146
-	 *
147
-	 * @param string $appId
148
-	 * @param \OCP\IUser $user (optional) if not defined, the currently logged in user will be used
149
-	 * @return bool
150
-	 */
151
-	public function isEnabledForUser($appId, $user = null) {
152
-		if ($this->isAlwaysEnabled($appId)) {
153
-			return true;
154
-		}
155
-		if ($user === null) {
156
-			$user = $this->userSession->getUser();
157
-		}
158
-		$installedApps = $this->getInstalledAppsValues();
159
-		if (isset($installedApps[$appId])) {
160
-			return $this->checkAppForUser($installedApps[$appId], $user);
161
-		} else {
162
-			return false;
163
-		}
164
-	}
165
-
166
-	/**
167
-	 * @param string $enabled
168
-	 * @param IUser $user
169
-	 * @return bool
170
-	 */
171
-	private function checkAppForUser($enabled, $user) {
172
-		if ($enabled === 'yes') {
173
-			return true;
174
-		} elseif ($user === null) {
175
-			return false;
176
-		} else {
177
-			if(empty($enabled)){
178
-				return false;
179
-			}
180
-
181
-			$groupIds = json_decode($enabled);
182
-
183
-			if (!is_array($groupIds)) {
184
-				$jsonError = json_last_error();
185
-				\OC::$server->getLogger()->warning('AppManger::checkAppForUser - can\'t decode group IDs: ' . print_r($enabled, true) . ' - json error code: ' . $jsonError, ['app' => 'lib']);
186
-				return false;
187
-			}
188
-
189
-			$userGroups = $this->groupManager->getUserGroupIds($user);
190
-			foreach ($userGroups as $groupId) {
191
-				if (in_array($groupId, $groupIds, true)) {
192
-					return true;
193
-				}
194
-			}
195
-			return false;
196
-		}
197
-	}
198
-
199
-	/**
200
-	 * Check if an app is installed in the instance
201
-	 *
202
-	 * @param string $appId
203
-	 * @return bool
204
-	 */
205
-	public function isInstalled($appId) {
206
-		$installedApps = $this->getInstalledAppsValues();
207
-		return isset($installedApps[$appId]);
208
-	}
209
-
210
-	/**
211
-	 * Enable an app for every user
212
-	 *
213
-	 * @param string $appId
214
-	 * @throws AppPathNotFoundException
215
-	 */
216
-	public function enableApp($appId) {
217
-		// Check if app exists
218
-		$this->getAppPath($appId);
219
-
220
-		$this->installedAppsCache[$appId] = 'yes';
221
-		$this->appConfig->setValue($appId, 'enabled', 'yes');
222
-		$this->dispatcher->dispatch(ManagerEvent::EVENT_APP_ENABLE, new ManagerEvent(
223
-			ManagerEvent::EVENT_APP_ENABLE, $appId
224
-		));
225
-		$this->clearAppsCache();
226
-	}
227
-
228
-	/**
229
-	 * Whether a list of types contains a protected app type
230
-	 *
231
-	 * @param string[] $types
232
-	 * @return bool
233
-	 */
234
-	public function hasProtectedAppType($types) {
235
-		if (empty($types)) {
236
-			return false;
237
-		}
238
-
239
-		$protectedTypes = array_intersect($this->protectedAppTypes, $types);
240
-		return !empty($protectedTypes);
241
-	}
242
-
243
-	/**
244
-	 * Enable an app only for specific groups
245
-	 *
246
-	 * @param string $appId
247
-	 * @param \OCP\IGroup[] $groups
248
-	 * @throws \Exception if app can't be enabled for groups
249
-	 */
250
-	public function enableAppForGroups($appId, $groups) {
251
-		$info = $this->getAppInfo($appId);
252
-		if (!empty($info['types'])) {
253
-			$protectedTypes = array_intersect($this->protectedAppTypes, $info['types']);
254
-			if (!empty($protectedTypes)) {
255
-				throw new \Exception("$appId can't be enabled for groups.");
256
-			}
257
-		}
258
-
259
-		$groupIds = array_map(function ($group) {
260
-			/** @var \OCP\IGroup $group */
261
-			return $group->getGID();
262
-		}, $groups);
263
-		$this->installedAppsCache[$appId] = json_encode($groupIds);
264
-		$this->appConfig->setValue($appId, 'enabled', json_encode($groupIds));
265
-		$this->dispatcher->dispatch(ManagerEvent::EVENT_APP_ENABLE_FOR_GROUPS, new ManagerEvent(
266
-			ManagerEvent::EVENT_APP_ENABLE_FOR_GROUPS, $appId, $groups
267
-		));
268
-		$this->clearAppsCache();
269
-	}
270
-
271
-	/**
272
-	 * Disable an app for every user
273
-	 *
274
-	 * @param string $appId
275
-	 * @throws \Exception if app can't be disabled
276
-	 */
277
-	public function disableApp($appId) {
278
-		if ($this->isAlwaysEnabled($appId)) {
279
-			throw new \Exception("$appId can't be disabled.");
280
-		}
281
-		unset($this->installedAppsCache[$appId]);
282
-		$this->appConfig->setValue($appId, 'enabled', 'no');
283
-
284
-		// run uninstall steps
285
-		$appData = $this->getAppInfo($appId);
286
-		if (!is_null($appData)) {
287
-			\OC_App::executeRepairSteps($appId, $appData['repair-steps']['uninstall']);
288
-		}
289
-
290
-		// emit disable hook - needed anymore ?
291
-		\OC_Hook::emit('OC_App', 'pre_disable', array('app' => $appId));
292
-
293
-		$this->dispatcher->dispatch(ManagerEvent::EVENT_APP_DISABLE, new ManagerEvent(
294
-			ManagerEvent::EVENT_APP_DISABLE, $appId
295
-		));
296
-		$this->clearAppsCache();
297
-	}
298
-
299
-	/**
300
-	 * Get the directory for the given app.
301
-	 *
302
-	 * @param string $appId
303
-	 * @return string
304
-	 * @throws AppPathNotFoundException if app folder can't be found
305
-	 */
306
-	public function getAppPath($appId) {
307
-		$appPath = \OC_App::getAppPath($appId);
308
-		if($appPath === false) {
309
-			throw new AppPathNotFoundException('Could not find path for ' . $appId);
310
-		}
311
-		return $appPath;
312
-	}
313
-
314
-	/**
315
-	 * Clear the cached list of apps when enabling/disabling an app
316
-	 */
317
-	public function clearAppsCache() {
318
-		$settingsMemCache = $this->memCacheFactory->create('settings');
319
-		$settingsMemCache->clear('listApps');
320
-	}
321
-
322
-	/**
323
-	 * Returns a list of apps that need upgrade
324
-	 *
325
-	 * @param string $version Nextcloud version as array of version components
326
-	 * @return array list of app info from apps that need an upgrade
327
-	 *
328
-	 * @internal
329
-	 */
330
-	public function getAppsNeedingUpgrade($version) {
331
-		$appsToUpgrade = [];
332
-		$apps = $this->getInstalledApps();
333
-		foreach ($apps as $appId) {
334
-			$appInfo = $this->getAppInfo($appId);
335
-			$appDbVersion = $this->appConfig->getValue($appId, 'installed_version');
336
-			if ($appDbVersion
337
-				&& isset($appInfo['version'])
338
-				&& version_compare($appInfo['version'], $appDbVersion, '>')
339
-				&& \OC_App::isAppCompatible($version, $appInfo)
340
-			) {
341
-				$appsToUpgrade[] = $appInfo;
342
-			}
343
-		}
344
-
345
-		return $appsToUpgrade;
346
-	}
347
-
348
-	/**
349
-	 * Returns the app information from "appinfo/info.xml".
350
-	 *
351
-	 * @param string $appId app id
352
-	 *
353
-	 * @return array app info
354
-	 *
355
-	 * @internal
356
-	 */
357
-	public function getAppInfo($appId) {
358
-		$appInfo = \OC_App::getAppInfo($appId);
359
-		if (!isset($appInfo['version'])) {
360
-			// read version from separate file
361
-			$appInfo['version'] = \OC_App::getAppVersion($appId);
362
-		}
363
-		return $appInfo;
364
-	}
365
-
366
-	/**
367
-	 * Returns a list of apps incompatible with the given version
368
-	 *
369
-	 * @param string $version Nextcloud version as array of version components
370
-	 *
371
-	 * @return array list of app info from incompatible apps
372
-	 *
373
-	 * @internal
374
-	 */
375
-	public function getIncompatibleApps($version) {
376
-		$apps = $this->getInstalledApps();
377
-		$incompatibleApps = array();
378
-		foreach ($apps as $appId) {
379
-			$info = $this->getAppInfo($appId);
380
-			if (!\OC_App::isAppCompatible($version, $info)) {
381
-				$incompatibleApps[] = $info;
382
-			}
383
-		}
384
-		return $incompatibleApps;
385
-	}
386
-
387
-	/**
388
-	 * @inheritdoc
389
-	 */
390
-	public function isShipped($appId) {
391
-		$this->loadShippedJson();
392
-		return in_array($appId, $this->shippedApps, true);
393
-	}
394
-
395
-	private function isAlwaysEnabled($appId) {
396
-		$alwaysEnabled = $this->getAlwaysEnabledApps();
397
-		return in_array($appId, $alwaysEnabled, true);
398
-	}
399
-
400
-	private function loadShippedJson() {
401
-		if ($this->shippedApps === null) {
402
-			$shippedJson = \OC::$SERVERROOT . '/core/shipped.json';
403
-			if (!file_exists($shippedJson)) {
404
-				throw new \Exception("File not found: $shippedJson");
405
-			}
406
-			$content = json_decode(file_get_contents($shippedJson), true);
407
-			$this->shippedApps = $content['shippedApps'];
408
-			$this->alwaysEnabled = $content['alwaysEnabled'];
409
-		}
410
-	}
411
-
412
-	/**
413
-	 * @inheritdoc
414
-	 */
415
-	public function getAlwaysEnabledApps() {
416
-		$this->loadShippedJson();
417
-		return $this->alwaysEnabled;
418
-	}
46
+    /**
47
+     * Apps with these types can not be enabled for certain groups only
48
+     * @var string[]
49
+     */
50
+    protected $protectedAppTypes = [
51
+        'filesystem',
52
+        'prelogin',
53
+        'authentication',
54
+        'logging',
55
+        'prevent_group_restriction',
56
+    ];
57
+
58
+    /** @var IUserSession */
59
+    private $userSession;
60
+
61
+    /** @var IAppConfig */
62
+    private $appConfig;
63
+
64
+    /** @var IGroupManager */
65
+    private $groupManager;
66
+
67
+    /** @var ICacheFactory */
68
+    private $memCacheFactory;
69
+
70
+    /** @var EventDispatcherInterface */
71
+    private $dispatcher;
72
+
73
+    /** @var string[] $appId => $enabled */
74
+    private $installedAppsCache;
75
+
76
+    /** @var string[] */
77
+    private $shippedApps;
78
+
79
+    /** @var string[] */
80
+    private $alwaysEnabled;
81
+
82
+    /**
83
+     * @param IUserSession $userSession
84
+     * @param IAppConfig $appConfig
85
+     * @param IGroupManager $groupManager
86
+     * @param ICacheFactory $memCacheFactory
87
+     * @param EventDispatcherInterface $dispatcher
88
+     */
89
+    public function __construct(IUserSession $userSession,
90
+                                IAppConfig $appConfig,
91
+                                IGroupManager $groupManager,
92
+                                ICacheFactory $memCacheFactory,
93
+                                EventDispatcherInterface $dispatcher) {
94
+        $this->userSession = $userSession;
95
+        $this->appConfig = $appConfig;
96
+        $this->groupManager = $groupManager;
97
+        $this->memCacheFactory = $memCacheFactory;
98
+        $this->dispatcher = $dispatcher;
99
+    }
100
+
101
+    /**
102
+     * @return string[] $appId => $enabled
103
+     */
104
+    private function getInstalledAppsValues() {
105
+        if (!$this->installedAppsCache) {
106
+            $values = $this->appConfig->getValues(false, 'enabled');
107
+
108
+            $alwaysEnabledApps = $this->getAlwaysEnabledApps();
109
+            foreach($alwaysEnabledApps as $appId) {
110
+                $values[$appId] = 'yes';
111
+            }
112
+
113
+            $this->installedAppsCache = array_filter($values, function ($value) {
114
+                return $value !== 'no';
115
+            });
116
+            ksort($this->installedAppsCache);
117
+        }
118
+        return $this->installedAppsCache;
119
+    }
120
+
121
+    /**
122
+     * List all installed apps
123
+     *
124
+     * @return string[]
125
+     */
126
+    public function getInstalledApps() {
127
+        return array_keys($this->getInstalledAppsValues());
128
+    }
129
+
130
+    /**
131
+     * List all apps enabled for a user
132
+     *
133
+     * @param \OCP\IUser $user
134
+     * @return string[]
135
+     */
136
+    public function getEnabledAppsForUser(IUser $user) {
137
+        $apps = $this->getInstalledAppsValues();
138
+        $appsForUser = array_filter($apps, function ($enabled) use ($user) {
139
+            return $this->checkAppForUser($enabled, $user);
140
+        });
141
+        return array_keys($appsForUser);
142
+    }
143
+
144
+    /**
145
+     * Check if an app is enabled for user
146
+     *
147
+     * @param string $appId
148
+     * @param \OCP\IUser $user (optional) if not defined, the currently logged in user will be used
149
+     * @return bool
150
+     */
151
+    public function isEnabledForUser($appId, $user = null) {
152
+        if ($this->isAlwaysEnabled($appId)) {
153
+            return true;
154
+        }
155
+        if ($user === null) {
156
+            $user = $this->userSession->getUser();
157
+        }
158
+        $installedApps = $this->getInstalledAppsValues();
159
+        if (isset($installedApps[$appId])) {
160
+            return $this->checkAppForUser($installedApps[$appId], $user);
161
+        } else {
162
+            return false;
163
+        }
164
+    }
165
+
166
+    /**
167
+     * @param string $enabled
168
+     * @param IUser $user
169
+     * @return bool
170
+     */
171
+    private function checkAppForUser($enabled, $user) {
172
+        if ($enabled === 'yes') {
173
+            return true;
174
+        } elseif ($user === null) {
175
+            return false;
176
+        } else {
177
+            if(empty($enabled)){
178
+                return false;
179
+            }
180
+
181
+            $groupIds = json_decode($enabled);
182
+
183
+            if (!is_array($groupIds)) {
184
+                $jsonError = json_last_error();
185
+                \OC::$server->getLogger()->warning('AppManger::checkAppForUser - can\'t decode group IDs: ' . print_r($enabled, true) . ' - json error code: ' . $jsonError, ['app' => 'lib']);
186
+                return false;
187
+            }
188
+
189
+            $userGroups = $this->groupManager->getUserGroupIds($user);
190
+            foreach ($userGroups as $groupId) {
191
+                if (in_array($groupId, $groupIds, true)) {
192
+                    return true;
193
+                }
194
+            }
195
+            return false;
196
+        }
197
+    }
198
+
199
+    /**
200
+     * Check if an app is installed in the instance
201
+     *
202
+     * @param string $appId
203
+     * @return bool
204
+     */
205
+    public function isInstalled($appId) {
206
+        $installedApps = $this->getInstalledAppsValues();
207
+        return isset($installedApps[$appId]);
208
+    }
209
+
210
+    /**
211
+     * Enable an app for every user
212
+     *
213
+     * @param string $appId
214
+     * @throws AppPathNotFoundException
215
+     */
216
+    public function enableApp($appId) {
217
+        // Check if app exists
218
+        $this->getAppPath($appId);
219
+
220
+        $this->installedAppsCache[$appId] = 'yes';
221
+        $this->appConfig->setValue($appId, 'enabled', 'yes');
222
+        $this->dispatcher->dispatch(ManagerEvent::EVENT_APP_ENABLE, new ManagerEvent(
223
+            ManagerEvent::EVENT_APP_ENABLE, $appId
224
+        ));
225
+        $this->clearAppsCache();
226
+    }
227
+
228
+    /**
229
+     * Whether a list of types contains a protected app type
230
+     *
231
+     * @param string[] $types
232
+     * @return bool
233
+     */
234
+    public function hasProtectedAppType($types) {
235
+        if (empty($types)) {
236
+            return false;
237
+        }
238
+
239
+        $protectedTypes = array_intersect($this->protectedAppTypes, $types);
240
+        return !empty($protectedTypes);
241
+    }
242
+
243
+    /**
244
+     * Enable an app only for specific groups
245
+     *
246
+     * @param string $appId
247
+     * @param \OCP\IGroup[] $groups
248
+     * @throws \Exception if app can't be enabled for groups
249
+     */
250
+    public function enableAppForGroups($appId, $groups) {
251
+        $info = $this->getAppInfo($appId);
252
+        if (!empty($info['types'])) {
253
+            $protectedTypes = array_intersect($this->protectedAppTypes, $info['types']);
254
+            if (!empty($protectedTypes)) {
255
+                throw new \Exception("$appId can't be enabled for groups.");
256
+            }
257
+        }
258
+
259
+        $groupIds = array_map(function ($group) {
260
+            /** @var \OCP\IGroup $group */
261
+            return $group->getGID();
262
+        }, $groups);
263
+        $this->installedAppsCache[$appId] = json_encode($groupIds);
264
+        $this->appConfig->setValue($appId, 'enabled', json_encode($groupIds));
265
+        $this->dispatcher->dispatch(ManagerEvent::EVENT_APP_ENABLE_FOR_GROUPS, new ManagerEvent(
266
+            ManagerEvent::EVENT_APP_ENABLE_FOR_GROUPS, $appId, $groups
267
+        ));
268
+        $this->clearAppsCache();
269
+    }
270
+
271
+    /**
272
+     * Disable an app for every user
273
+     *
274
+     * @param string $appId
275
+     * @throws \Exception if app can't be disabled
276
+     */
277
+    public function disableApp($appId) {
278
+        if ($this->isAlwaysEnabled($appId)) {
279
+            throw new \Exception("$appId can't be disabled.");
280
+        }
281
+        unset($this->installedAppsCache[$appId]);
282
+        $this->appConfig->setValue($appId, 'enabled', 'no');
283
+
284
+        // run uninstall steps
285
+        $appData = $this->getAppInfo($appId);
286
+        if (!is_null($appData)) {
287
+            \OC_App::executeRepairSteps($appId, $appData['repair-steps']['uninstall']);
288
+        }
289
+
290
+        // emit disable hook - needed anymore ?
291
+        \OC_Hook::emit('OC_App', 'pre_disable', array('app' => $appId));
292
+
293
+        $this->dispatcher->dispatch(ManagerEvent::EVENT_APP_DISABLE, new ManagerEvent(
294
+            ManagerEvent::EVENT_APP_DISABLE, $appId
295
+        ));
296
+        $this->clearAppsCache();
297
+    }
298
+
299
+    /**
300
+     * Get the directory for the given app.
301
+     *
302
+     * @param string $appId
303
+     * @return string
304
+     * @throws AppPathNotFoundException if app folder can't be found
305
+     */
306
+    public function getAppPath($appId) {
307
+        $appPath = \OC_App::getAppPath($appId);
308
+        if($appPath === false) {
309
+            throw new AppPathNotFoundException('Could not find path for ' . $appId);
310
+        }
311
+        return $appPath;
312
+    }
313
+
314
+    /**
315
+     * Clear the cached list of apps when enabling/disabling an app
316
+     */
317
+    public function clearAppsCache() {
318
+        $settingsMemCache = $this->memCacheFactory->create('settings');
319
+        $settingsMemCache->clear('listApps');
320
+    }
321
+
322
+    /**
323
+     * Returns a list of apps that need upgrade
324
+     *
325
+     * @param string $version Nextcloud version as array of version components
326
+     * @return array list of app info from apps that need an upgrade
327
+     *
328
+     * @internal
329
+     */
330
+    public function getAppsNeedingUpgrade($version) {
331
+        $appsToUpgrade = [];
332
+        $apps = $this->getInstalledApps();
333
+        foreach ($apps as $appId) {
334
+            $appInfo = $this->getAppInfo($appId);
335
+            $appDbVersion = $this->appConfig->getValue($appId, 'installed_version');
336
+            if ($appDbVersion
337
+                && isset($appInfo['version'])
338
+                && version_compare($appInfo['version'], $appDbVersion, '>')
339
+                && \OC_App::isAppCompatible($version, $appInfo)
340
+            ) {
341
+                $appsToUpgrade[] = $appInfo;
342
+            }
343
+        }
344
+
345
+        return $appsToUpgrade;
346
+    }
347
+
348
+    /**
349
+     * Returns the app information from "appinfo/info.xml".
350
+     *
351
+     * @param string $appId app id
352
+     *
353
+     * @return array app info
354
+     *
355
+     * @internal
356
+     */
357
+    public function getAppInfo($appId) {
358
+        $appInfo = \OC_App::getAppInfo($appId);
359
+        if (!isset($appInfo['version'])) {
360
+            // read version from separate file
361
+            $appInfo['version'] = \OC_App::getAppVersion($appId);
362
+        }
363
+        return $appInfo;
364
+    }
365
+
366
+    /**
367
+     * Returns a list of apps incompatible with the given version
368
+     *
369
+     * @param string $version Nextcloud version as array of version components
370
+     *
371
+     * @return array list of app info from incompatible apps
372
+     *
373
+     * @internal
374
+     */
375
+    public function getIncompatibleApps($version) {
376
+        $apps = $this->getInstalledApps();
377
+        $incompatibleApps = array();
378
+        foreach ($apps as $appId) {
379
+            $info = $this->getAppInfo($appId);
380
+            if (!\OC_App::isAppCompatible($version, $info)) {
381
+                $incompatibleApps[] = $info;
382
+            }
383
+        }
384
+        return $incompatibleApps;
385
+    }
386
+
387
+    /**
388
+     * @inheritdoc
389
+     */
390
+    public function isShipped($appId) {
391
+        $this->loadShippedJson();
392
+        return in_array($appId, $this->shippedApps, true);
393
+    }
394
+
395
+    private function isAlwaysEnabled($appId) {
396
+        $alwaysEnabled = $this->getAlwaysEnabledApps();
397
+        return in_array($appId, $alwaysEnabled, true);
398
+    }
399
+
400
+    private function loadShippedJson() {
401
+        if ($this->shippedApps === null) {
402
+            $shippedJson = \OC::$SERVERROOT . '/core/shipped.json';
403
+            if (!file_exists($shippedJson)) {
404
+                throw new \Exception("File not found: $shippedJson");
405
+            }
406
+            $content = json_decode(file_get_contents($shippedJson), true);
407
+            $this->shippedApps = $content['shippedApps'];
408
+            $this->alwaysEnabled = $content['alwaysEnabled'];
409
+        }
410
+    }
411
+
412
+    /**
413
+     * @inheritdoc
414
+     */
415
+    public function getAlwaysEnabledApps() {
416
+        $this->loadShippedJson();
417
+        return $this->alwaysEnabled;
418
+    }
419 419
 }
Please login to merge, or discard this patch.