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