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