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