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