Completed
Push — master ( d953db...ac63c2 )
by Morris
38s
created
lib/private/legacy/app.php 3 patches
Doc Comments   +1 added lines, -1 removed lines patch added patch discarded remove patch
@@ -269,7 +269,7 @@
 block discarded – undo
269 269
 	 * check if an app is of a specific type
270 270
 	 *
271 271
 	 * @param string $app
272
-	 * @param array $types
272
+	 * @param string[] $types
273 273
 	 * @return bool
274 274
 	 */
275 275
 	public static function isType(string $app, array $types): bool {
Please login to merge, or discard this patch.
Indentation   +1065 added lines, -1065 removed lines patch added patch discarded remove patch
@@ -63,1069 +63,1069 @@
 block discarded – undo
63 63
  * upgrading and removing apps.
64 64
  */
65 65
 class OC_App {
66
-	static private $adminForms = [];
67
-	static private $personalForms = [];
68
-	static private $appTypes = [];
69
-	static private $loadedApps = [];
70
-	static private $altLogin = [];
71
-	static private $alreadyRegistered = [];
72
-	const officialApp = 200;
73
-
74
-	/**
75
-	 * clean the appId
76
-	 *
77
-	 * @param string $app AppId that needs to be cleaned
78
-	 * @return string
79
-	 */
80
-	public static function cleanAppId(string $app): string {
81
-		return str_replace(array('\0', '/', '\\', '..'), '', $app);
82
-	}
83
-
84
-	/**
85
-	 * Check if an app is loaded
86
-	 *
87
-	 * @param string $app
88
-	 * @return bool
89
-	 */
90
-	public static function isAppLoaded(string $app): bool {
91
-		return in_array($app, self::$loadedApps, true);
92
-	}
93
-
94
-	/**
95
-	 * loads all apps
96
-	 *
97
-	 * @param string[] $types
98
-	 * @return bool
99
-	 *
100
-	 * This function walks through the ownCloud directory and loads all apps
101
-	 * it can find. A directory contains an app if the file /appinfo/info.xml
102
-	 * exists.
103
-	 *
104
-	 * if $types is set to non-empty array, only apps of those types will be loaded
105
-	 */
106
-	public static function loadApps(array $types = []): bool {
107
-		if (\OC::$server->getSystemConfig()->getValue('maintenance', false)) {
108
-			return false;
109
-		}
110
-		// Load the enabled apps here
111
-		$apps = self::getEnabledApps();
112
-
113
-		// Add each apps' folder as allowed class path
114
-		foreach($apps as $app) {
115
-			$path = self::getAppPath($app);
116
-			if($path !== false) {
117
-				self::registerAutoloading($app, $path);
118
-			}
119
-		}
120
-
121
-		// prevent app.php from printing output
122
-		ob_start();
123
-		foreach ($apps as $app) {
124
-			if (($types === [] or self::isType($app, $types)) && !in_array($app, self::$loadedApps)) {
125
-				self::loadApp($app);
126
-			}
127
-		}
128
-		ob_end_clean();
129
-
130
-		return true;
131
-	}
132
-
133
-	/**
134
-	 * load a single app
135
-	 *
136
-	 * @param string $app
137
-	 * @throws Exception
138
-	 */
139
-	public static function loadApp(string $app) {
140
-		self::$loadedApps[] = $app;
141
-		$appPath = self::getAppPath($app);
142
-		if($appPath === false) {
143
-			return;
144
-		}
145
-
146
-		// in case someone calls loadApp() directly
147
-		self::registerAutoloading($app, $appPath);
148
-
149
-		if (is_file($appPath . '/appinfo/app.php')) {
150
-			\OC::$server->getEventLogger()->start('load_app_' . $app, 'Load app: ' . $app);
151
-			try {
152
-				self::requireAppFile($app);
153
-			} catch (Error $ex) {
154
-				\OC::$server->getLogger()->logException($ex);
155
-				if (!\OC::$server->getAppManager()->isShipped($app)) {
156
-					// Only disable apps which are not shipped
157
-					self::disable($app);
158
-				}
159
-			}
160
-			if (self::isType($app, array('authentication'))) {
161
-				// since authentication apps affect the "is app enabled for group" check,
162
-				// the enabled apps cache needs to be cleared to make sure that the
163
-				// next time getEnableApps() is called it will also include apps that were
164
-				// enabled for groups
165
-				self::$enabledAppsCache = [];
166
-			}
167
-			\OC::$server->getEventLogger()->end('load_app_' . $app);
168
-		}
169
-
170
-		$info = self::getAppInfo($app);
171
-		if (!empty($info['activity']['filters'])) {
172
-			foreach ($info['activity']['filters'] as $filter) {
173
-				\OC::$server->getActivityManager()->registerFilter($filter);
174
-			}
175
-		}
176
-		if (!empty($info['activity']['settings'])) {
177
-			foreach ($info['activity']['settings'] as $setting) {
178
-				\OC::$server->getActivityManager()->registerSetting($setting);
179
-			}
180
-		}
181
-		if (!empty($info['activity']['providers'])) {
182
-			foreach ($info['activity']['providers'] as $provider) {
183
-				\OC::$server->getActivityManager()->registerProvider($provider);
184
-			}
185
-		}
186
-
187
-		if (!empty($info['settings']['admin'])) {
188
-			foreach ($info['settings']['admin'] as $setting) {
189
-				\OC::$server->getSettingsManager()->registerSetting('admin', $setting);
190
-			}
191
-		}
192
-		if (!empty($info['settings']['admin-section'])) {
193
-			foreach ($info['settings']['admin-section'] as $section) {
194
-				\OC::$server->getSettingsManager()->registerSection('admin', $section);
195
-			}
196
-		}
197
-		if (!empty($info['settings']['personal'])) {
198
-			foreach ($info['settings']['personal'] as $setting) {
199
-				\OC::$server->getSettingsManager()->registerSetting('personal', $setting);
200
-			}
201
-		}
202
-		if (!empty($info['settings']['personal-section'])) {
203
-			foreach ($info['settings']['personal-section'] as $section) {
204
-				\OC::$server->getSettingsManager()->registerSection('personal', $section);
205
-			}
206
-		}
207
-
208
-		if (!empty($info['collaboration']['plugins'])) {
209
-			// deal with one or many plugin entries
210
-			$plugins = isset($info['collaboration']['plugins']['plugin']['@value']) ?
211
-				[$info['collaboration']['plugins']['plugin']] : $info['collaboration']['plugins']['plugin'];
212
-			foreach ($plugins as $plugin) {
213
-				if($plugin['@attributes']['type'] === 'collaborator-search') {
214
-					$pluginInfo = [
215
-						'shareType' => $plugin['@attributes']['share-type'],
216
-						'class' => $plugin['@value'],
217
-					];
218
-					\OC::$server->getCollaboratorSearch()->registerPlugin($pluginInfo);
219
-				} else if ($plugin['@attributes']['type'] === 'autocomplete-sort') {
220
-					\OC::$server->getAutoCompleteManager()->registerSorter($plugin['@value']);
221
-				}
222
-			}
223
-		}
224
-	}
225
-
226
-	/**
227
-	 * @internal
228
-	 * @param string $app
229
-	 * @param string $path
230
-	 */
231
-	public static function registerAutoloading(string $app, string $path) {
232
-		$key = $app . '-' . $path;
233
-		if(isset(self::$alreadyRegistered[$key])) {
234
-			return;
235
-		}
236
-
237
-		self::$alreadyRegistered[$key] = true;
238
-
239
-		// Register on PSR-4 composer autoloader
240
-		$appNamespace = \OC\AppFramework\App::buildAppNamespace($app);
241
-		\OC::$server->registerNamespace($app, $appNamespace);
242
-
243
-		if (file_exists($path . '/composer/autoload.php')) {
244
-			require_once $path . '/composer/autoload.php';
245
-		} else {
246
-			\OC::$composerAutoloader->addPsr4($appNamespace . '\\', $path . '/lib/', true);
247
-			// Register on legacy autoloader
248
-			\OC::$loader->addValidRoot($path);
249
-		}
250
-
251
-		// Register Test namespace only when testing
252
-		if (defined('PHPUNIT_RUN') || defined('CLI_TEST_RUN')) {
253
-			\OC::$composerAutoloader->addPsr4($appNamespace . '\\Tests\\', $path . '/tests/', true);
254
-		}
255
-	}
256
-
257
-	/**
258
-	 * Load app.php from the given app
259
-	 *
260
-	 * @param string $app app name
261
-	 * @throws Error
262
-	 */
263
-	private static function requireAppFile(string $app) {
264
-		// encapsulated here to avoid variable scope conflicts
265
-		require_once $app . '/appinfo/app.php';
266
-	}
267
-
268
-	/**
269
-	 * check if an app is of a specific type
270
-	 *
271
-	 * @param string $app
272
-	 * @param array $types
273
-	 * @return bool
274
-	 */
275
-	public static function isType(string $app, array $types): bool {
276
-		$appTypes = self::getAppTypes($app);
277
-		foreach ($types as $type) {
278
-			if (array_search($type, $appTypes) !== false) {
279
-				return true;
280
-			}
281
-		}
282
-		return false;
283
-	}
284
-
285
-	/**
286
-	 * get the types of an app
287
-	 *
288
-	 * @param string $app
289
-	 * @return array
290
-	 */
291
-	private static function getAppTypes(string $app): array {
292
-		//load the cache
293
-		if (count(self::$appTypes) == 0) {
294
-			self::$appTypes = \OC::$server->getAppConfig()->getValues(false, 'types');
295
-		}
296
-
297
-		if (isset(self::$appTypes[$app])) {
298
-			return explode(',', self::$appTypes[$app]);
299
-		}
300
-
301
-		return [];
302
-	}
303
-
304
-	/**
305
-	 * read app types from info.xml and cache them in the database
306
-	 */
307
-	public static function setAppTypes(string $app) {
308
-		$appManager = \OC::$server->getAppManager();
309
-		$appData = $appManager->getAppInfo($app);
310
-		if(!is_array($appData)) {
311
-			return;
312
-		}
313
-
314
-		if (isset($appData['types'])) {
315
-			$appTypes = implode(',', $appData['types']);
316
-		} else {
317
-			$appTypes = '';
318
-			$appData['types'] = [];
319
-		}
320
-
321
-		$config = \OC::$server->getConfig();
322
-		$config->setAppValue($app, 'types', $appTypes);
323
-
324
-		if ($appManager->hasProtectedAppType($appData['types'])) {
325
-			$enabled = $config->getAppValue($app, 'enabled', 'yes');
326
-			if ($enabled !== 'yes' && $enabled !== 'no') {
327
-				$config->setAppValue($app, 'enabled', 'yes');
328
-			}
329
-		}
330
-	}
331
-
332
-	/**
333
-	 * get all enabled apps
334
-	 */
335
-	protected static $enabledAppsCache = [];
336
-
337
-	/**
338
-	 * Returns apps enabled for the current user.
339
-	 *
340
-	 * @param bool $forceRefresh whether to refresh the cache
341
-	 * @param bool $all whether to return apps for all users, not only the
342
-	 * currently logged in one
343
-	 * @return string[]
344
-	 */
345
-	public static function getEnabledApps(bool $forceRefresh = false, bool $all = false): array {
346
-		if (!\OC::$server->getSystemConfig()->getValue('installed', false)) {
347
-			return [];
348
-		}
349
-		// in incognito mode or when logged out, $user will be false,
350
-		// which is also the case during an upgrade
351
-		$appManager = \OC::$server->getAppManager();
352
-		if ($all) {
353
-			$user = null;
354
-		} else {
355
-			$user = \OC::$server->getUserSession()->getUser();
356
-		}
357
-
358
-		if (is_null($user)) {
359
-			$apps = $appManager->getInstalledApps();
360
-		} else {
361
-			$apps = $appManager->getEnabledAppsForUser($user);
362
-		}
363
-		$apps = array_filter($apps, function ($app) {
364
-			return $app !== 'files';//we add this manually
365
-		});
366
-		sort($apps);
367
-		array_unshift($apps, 'files');
368
-		return $apps;
369
-	}
370
-
371
-	/**
372
-	 * checks whether or not an app is enabled
373
-	 *
374
-	 * @param string $app app
375
-	 * @return bool
376
-	 * @deprecated 13.0.0 use \OC::$server->getAppManager()->isEnabledForUser($appId)
377
-	 *
378
-	 * This function checks whether or not an app is enabled.
379
-	 */
380
-	public static function isEnabled(string $app): bool {
381
-		return \OC::$server->getAppManager()->isEnabledForUser($app);
382
-	}
383
-
384
-	/**
385
-	 * enables an app
386
-	 *
387
-	 * @param string $appId
388
-	 * @param array $groups (optional) when set, only these groups will have access to the app
389
-	 * @throws \Exception
390
-	 * @return void
391
-	 *
392
-	 * This function set an app as enabled in appconfig.
393
-	 */
394
-	public function enable(string $appId,
395
-						   array $groups = []) {
396
-		self::$enabledAppsCache = []; // flush
397
-
398
-		// Check if app is already downloaded
399
-		$installer = \OC::$server->query(Installer::class);
400
-		$isDownloaded = $installer->isDownloaded($appId);
401
-
402
-		if(!$isDownloaded) {
403
-			$installer->downloadApp($appId);
404
-		}
405
-
406
-		$installer->installApp($appId);
407
-
408
-		$appManager = \OC::$server->getAppManager();
409
-		if ($groups !== []) {
410
-			$groupManager = \OC::$server->getGroupManager();
411
-			$groupsList = [];
412
-			foreach ($groups as $group) {
413
-				$groupItem = $groupManager->get($group);
414
-				if ($groupItem instanceof \OCP\IGroup) {
415
-					$groupsList[] = $groupManager->get($group);
416
-				}
417
-			}
418
-			$appManager->enableAppForGroups($appId, $groupsList);
419
-		} else {
420
-			$appManager->enableApp($appId);
421
-		}
422
-	}
423
-
424
-	/**
425
-	 * This function set an app as disabled in appconfig.
426
-	 *
427
-	 * @param string $app app
428
-	 * @throws Exception
429
-	 */
430
-	public static function disable(string $app) {
431
-		// flush
432
-		self::$enabledAppsCache = [];
433
-
434
-		// run uninstall steps
435
-		$appData = OC_App::getAppInfo($app);
436
-		if (!is_null($appData)) {
437
-			OC_App::executeRepairSteps($app, $appData['repair-steps']['uninstall']);
438
-		}
439
-
440
-		// emit disable hook - needed anymore ?
441
-		\OC_Hook::emit('OC_App', 'pre_disable', array('app' => $app));
442
-
443
-		// finally disable it
444
-		$appManager = \OC::$server->getAppManager();
445
-		$appManager->disableApp($app);
446
-	}
447
-
448
-	/**
449
-	 * Get the path where to install apps
450
-	 *
451
-	 * @return string|false
452
-	 */
453
-	public static function getInstallPath() {
454
-		if (\OC::$server->getSystemConfig()->getValue('appstoreenabled', true) == false) {
455
-			return false;
456
-		}
457
-
458
-		foreach (OC::$APPSROOTS as $dir) {
459
-			if (isset($dir['writable']) && $dir['writable'] === true) {
460
-				return $dir['path'];
461
-			}
462
-		}
463
-
464
-		\OCP\Util::writeLog('core', 'No application directories are marked as writable.', \OCP\Util::ERROR);
465
-		return null;
466
-	}
467
-
468
-
469
-	/**
470
-	 * search for an app in all app-directories
471
-	 *
472
-	 * @param string $appId
473
-	 * @return false|string
474
-	 */
475
-	public static function findAppInDirectories(string $appId) {
476
-		$sanitizedAppId = self::cleanAppId($appId);
477
-		if($sanitizedAppId !== $appId) {
478
-			return false;
479
-		}
480
-		static $app_dir = [];
481
-
482
-		if (isset($app_dir[$appId])) {
483
-			return $app_dir[$appId];
484
-		}
485
-
486
-		$possibleApps = [];
487
-		foreach (OC::$APPSROOTS as $dir) {
488
-			if (file_exists($dir['path'] . '/' . $appId)) {
489
-				$possibleApps[] = $dir;
490
-			}
491
-		}
492
-
493
-		if (empty($possibleApps)) {
494
-			return false;
495
-		} elseif (count($possibleApps) === 1) {
496
-			$dir = array_shift($possibleApps);
497
-			$app_dir[$appId] = $dir;
498
-			return $dir;
499
-		} else {
500
-			$versionToLoad = [];
501
-			foreach ($possibleApps as $possibleApp) {
502
-				$version = self::getAppVersionByPath($possibleApp['path']);
503
-				if (empty($versionToLoad) || version_compare($version, $versionToLoad['version'], '>')) {
504
-					$versionToLoad = array(
505
-						'dir' => $possibleApp,
506
-						'version' => $version,
507
-					);
508
-				}
509
-			}
510
-			$app_dir[$appId] = $versionToLoad['dir'];
511
-			return $versionToLoad['dir'];
512
-			//TODO - write test
513
-		}
514
-	}
515
-
516
-	/**
517
-	 * Get the directory for the given app.
518
-	 * If the app is defined in multiple directories, the first one is taken. (false if not found)
519
-	 *
520
-	 * @param string $appId
521
-	 * @return string|false
522
-	 */
523
-	public static function getAppPath(string $appId) {
524
-		if ($appId === null || trim($appId) === '') {
525
-			return false;
526
-		}
527
-
528
-		if (($dir = self::findAppInDirectories($appId)) != false) {
529
-			return $dir['path'] . '/' . $appId;
530
-		}
531
-		return false;
532
-	}
533
-
534
-	/**
535
-	 * Get the path for the given app on the access
536
-	 * If the app is defined in multiple directories, the first one is taken. (false if not found)
537
-	 *
538
-	 * @param string $appId
539
-	 * @return string|false
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
-	 */
682
-	public static function registerLogIn(array $entry) {
683
-		self::$altLogin[] = $entry;
684
-	}
685
-
686
-	/**
687
-	 * @return array
688
-	 */
689
-	public static function getAlternativeLogIns(): array {
690
-		return self::$altLogin;
691
-	}
692
-
693
-	/**
694
-	 * get a list of all apps in the apps folder
695
-	 *
696
-	 * @return array an array of app names (string IDs)
697
-	 * @todo: change the name of this method to getInstalledApps, which is more accurate
698
-	 */
699
-	public static function getAllApps(): array {
700
-
701
-		$apps = [];
702
-
703
-		foreach (OC::$APPSROOTS as $apps_dir) {
704
-			if (!is_readable($apps_dir['path'])) {
705
-				\OCP\Util::writeLog('core', 'unable to read app folder : ' . $apps_dir['path'], \OCP\Util::WARN);
706
-				continue;
707
-			}
708
-			$dh = opendir($apps_dir['path']);
709
-
710
-			if (is_resource($dh)) {
711
-				while (($file = readdir($dh)) !== false) {
712
-
713
-					if ($file[0] != '.' and is_dir($apps_dir['path'] . '/' . $file) and is_file($apps_dir['path'] . '/' . $file . '/appinfo/info.xml')) {
714
-
715
-						$apps[] = $file;
716
-					}
717
-				}
718
-			}
719
-		}
720
-
721
-		$apps = array_unique($apps);
722
-
723
-		return $apps;
724
-	}
725
-
726
-	/**
727
-	 * List all apps, this is used in apps.php
728
-	 *
729
-	 * @return array
730
-	 */
731
-	public function listAllApps(): array {
732
-		$installedApps = OC_App::getAllApps();
733
-
734
-		$appManager = \OC::$server->getAppManager();
735
-		//we don't want to show configuration for these
736
-		$blacklist = $appManager->getAlwaysEnabledApps();
737
-		$appList = [];
738
-		$langCode = \OC::$server->getL10N('core')->getLanguageCode();
739
-		$urlGenerator = \OC::$server->getURLGenerator();
740
-
741
-		foreach ($installedApps as $app) {
742
-			if (array_search($app, $blacklist) === false) {
743
-
744
-				$info = OC_App::getAppInfo($app, false, $langCode);
745
-				if (!is_array($info)) {
746
-					\OCP\Util::writeLog('core', 'Could not read app info file for app "' . $app . '"', \OCP\Util::ERROR);
747
-					continue;
748
-				}
749
-
750
-				if (!isset($info['name'])) {
751
-					\OCP\Util::writeLog('core', 'App id "' . $app . '" has no name in appinfo', \OCP\Util::ERROR);
752
-					continue;
753
-				}
754
-
755
-				$enabled = \OC::$server->getConfig()->getAppValue($app, 'enabled', 'no');
756
-				$info['groups'] = null;
757
-				if ($enabled === 'yes') {
758
-					$active = true;
759
-				} else if ($enabled === 'no') {
760
-					$active = false;
761
-				} else {
762
-					$active = true;
763
-					$info['groups'] = $enabled;
764
-				}
765
-
766
-				$info['active'] = $active;
767
-
768
-				if ($appManager->isShipped($app)) {
769
-					$info['internal'] = true;
770
-					$info['level'] = self::officialApp;
771
-					$info['removable'] = false;
772
-				} else {
773
-					$info['internal'] = false;
774
-					$info['removable'] = true;
775
-				}
776
-
777
-				$appPath = self::getAppPath($app);
778
-				if($appPath !== false) {
779
-					$appIcon = $appPath . '/img/' . $app . '.svg';
780
-					if (file_exists($appIcon)) {
781
-						$info['preview'] = $urlGenerator->imagePath($app, $app . '.svg');
782
-						$info['previewAsIcon'] = true;
783
-					} else {
784
-						$appIcon = $appPath . '/img/app.svg';
785
-						if (file_exists($appIcon)) {
786
-							$info['preview'] = $urlGenerator->imagePath($app, 'app.svg');
787
-							$info['previewAsIcon'] = true;
788
-						}
789
-					}
790
-				}
791
-				// fix documentation
792
-				if (isset($info['documentation']) && is_array($info['documentation'])) {
793
-					foreach ($info['documentation'] as $key => $url) {
794
-						// If it is not an absolute URL we assume it is a key
795
-						// i.e. admin-ldap will get converted to go.php?to=admin-ldap
796
-						if (stripos($url, 'https://') !== 0 && stripos($url, 'http://') !== 0) {
797
-							$url = $urlGenerator->linkToDocs($url);
798
-						}
799
-
800
-						$info['documentation'][$key] = $url;
801
-					}
802
-				}
803
-
804
-				$info['version'] = OC_App::getAppVersion($app);
805
-				$appList[] = $info;
806
-			}
807
-		}
808
-
809
-		return $appList;
810
-	}
811
-
812
-	public static function shouldUpgrade(string $app): bool {
813
-		$versions = self::getAppVersions();
814
-		$currentVersion = OC_App::getAppVersion($app);
815
-		if ($currentVersion && isset($versions[$app])) {
816
-			$installedVersion = $versions[$app];
817
-			if (!version_compare($currentVersion, $installedVersion, '=')) {
818
-				return true;
819
-			}
820
-		}
821
-		return false;
822
-	}
823
-
824
-	/**
825
-	 * Adjust the number of version parts of $version1 to match
826
-	 * the number of version parts of $version2.
827
-	 *
828
-	 * @param string $version1 version to adjust
829
-	 * @param string $version2 version to take the number of parts from
830
-	 * @return string shortened $version1
831
-	 */
832
-	private static function adjustVersionParts(string $version1, string $version2): string {
833
-		$version1 = explode('.', $version1);
834
-		$version2 = explode('.', $version2);
835
-		// reduce $version1 to match the number of parts in $version2
836
-		while (count($version1) > count($version2)) {
837
-			array_pop($version1);
838
-		}
839
-		// if $version1 does not have enough parts, add some
840
-		while (count($version1) < count($version2)) {
841
-			$version1[] = '0';
842
-		}
843
-		return implode('.', $version1);
844
-	}
845
-
846
-	/**
847
-	 * Check whether the current ownCloud version matches the given
848
-	 * application's version requirements.
849
-	 *
850
-	 * The comparison is made based on the number of parts that the
851
-	 * app info version has. For example for ownCloud 6.0.3 if the
852
-	 * app info version is expecting version 6.0, the comparison is
853
-	 * made on the first two parts of the ownCloud version.
854
-	 * This means that it's possible to specify "requiremin" => 6
855
-	 * and "requiremax" => 6 and it will still match ownCloud 6.0.3.
856
-	 *
857
-	 * @param string $ocVersion ownCloud version to check against
858
-	 * @param array $appInfo app info (from xml)
859
-	 *
860
-	 * @return boolean true if compatible, otherwise false
861
-	 */
862
-	public static function isAppCompatible(string $ocVersion, array $appInfo): bool {
863
-		$requireMin = '';
864
-		$requireMax = '';
865
-		if (isset($appInfo['dependencies']['nextcloud']['@attributes']['min-version'])) {
866
-			$requireMin = $appInfo['dependencies']['nextcloud']['@attributes']['min-version'];
867
-		} elseif (isset($appInfo['dependencies']['owncloud']['@attributes']['min-version'])) {
868
-			$requireMin = $appInfo['dependencies']['owncloud']['@attributes']['min-version'];
869
-		} else if (isset($appInfo['requiremin'])) {
870
-			$requireMin = $appInfo['requiremin'];
871
-		} else if (isset($appInfo['require'])) {
872
-			$requireMin = $appInfo['require'];
873
-		}
874
-
875
-		if (isset($appInfo['dependencies']['nextcloud']['@attributes']['max-version'])) {
876
-			$requireMax = $appInfo['dependencies']['nextcloud']['@attributes']['max-version'];
877
-		} elseif (isset($appInfo['dependencies']['owncloud']['@attributes']['max-version'])) {
878
-			$requireMax = $appInfo['dependencies']['owncloud']['@attributes']['max-version'];
879
-		} else if (isset($appInfo['requiremax'])) {
880
-			$requireMax = $appInfo['requiremax'];
881
-		}
882
-
883
-		if (!empty($requireMin)
884
-			&& version_compare(self::adjustVersionParts($ocVersion, $requireMin), $requireMin, '<')
885
-		) {
886
-
887
-			return false;
888
-		}
889
-
890
-		if (!empty($requireMax)
891
-			&& version_compare(self::adjustVersionParts($ocVersion, $requireMax), $requireMax, '>')
892
-		) {
893
-			return false;
894
-		}
895
-
896
-		return true;
897
-	}
898
-
899
-	/**
900
-	 * get the installed version of all apps
901
-	 */
902
-	public static function getAppVersions() {
903
-		static $versions;
904
-
905
-		if(!$versions) {
906
-			$appConfig = \OC::$server->getAppConfig();
907
-			$versions = $appConfig->getValues(false, 'installed_version');
908
-		}
909
-		return $versions;
910
-	}
911
-
912
-	/**
913
-	 * update the database for the app and call the update script
914
-	 *
915
-	 * @param string $appId
916
-	 * @return bool
917
-	 */
918
-	public static function updateApp(sstring $appId): bool {
919
-		$appPath = self::getAppPath($appId);
920
-		if($appPath === false) {
921
-			return false;
922
-		}
923
-		self::registerAutoloading($appId, $appPath);
924
-
925
-		$appData = self::getAppInfo($appId);
926
-		self::executeRepairSteps($appId, $appData['repair-steps']['pre-migration']);
927
-
928
-		if (file_exists($appPath . '/appinfo/database.xml')) {
929
-			OC_DB::updateDbFromStructure($appPath . '/appinfo/database.xml');
930
-		} else {
931
-			$ms = new MigrationService($appId, \OC::$server->getDatabaseConnection());
932
-			$ms->migrate();
933
-		}
934
-
935
-		self::executeRepairSteps($appId, $appData['repair-steps']['post-migration']);
936
-		self::setupLiveMigrations($appId, $appData['repair-steps']['live-migration']);
937
-		// update appversion in app manager
938
-		\OC::$server->getAppManager()->getAppVersion($appId, false);
939
-
940
-		// run upgrade code
941
-		if (file_exists($appPath . '/appinfo/update.php')) {
942
-			self::loadApp($appId);
943
-			include $appPath . '/appinfo/update.php';
944
-		}
945
-		self::setupBackgroundJobs($appData['background-jobs']);
946
-
947
-		//set remote/public handlers
948
-		if (array_key_exists('ocsid', $appData)) {
949
-			\OC::$server->getConfig()->setAppValue($appId, 'ocsid', $appData['ocsid']);
950
-		} elseif(\OC::$server->getConfig()->getAppValue($appId, 'ocsid', null) !== null) {
951
-			\OC::$server->getConfig()->deleteAppValue($appId, 'ocsid');
952
-		}
953
-		foreach ($appData['remote'] as $name => $path) {
954
-			\OC::$server->getConfig()->setAppValue('core', 'remote_' . $name, $appId . '/' . $path);
955
-		}
956
-		foreach ($appData['public'] as $name => $path) {
957
-			\OC::$server->getConfig()->setAppValue('core', 'public_' . $name, $appId . '/' . $path);
958
-		}
959
-
960
-		self::setAppTypes($appId);
961
-
962
-		$version = \OC_App::getAppVersion($appId);
963
-		\OC::$server->getConfig()->setAppValue($appId, 'installed_version', $version);
964
-
965
-		\OC::$server->getEventDispatcher()->dispatch(ManagerEvent::EVENT_APP_UPDATE, new ManagerEvent(
966
-			ManagerEvent::EVENT_APP_UPDATE, $appId
967
-		));
968
-
969
-		return true;
970
-	}
971
-
972
-	/**
973
-	 * @param string $appId
974
-	 * @param string[] $steps
975
-	 * @throws \OC\NeedsUpdateException
976
-	 */
977
-	public static function executeRepairSteps(string $appId, array $steps) {
978
-		if (empty($steps)) {
979
-			return;
980
-		}
981
-		// load the app
982
-		self::loadApp($appId);
983
-
984
-		$dispatcher = OC::$server->getEventDispatcher();
985
-
986
-		// load the steps
987
-		$r = new Repair([], $dispatcher);
988
-		foreach ($steps as $step) {
989
-			try {
990
-				$r->addStep($step);
991
-			} catch (Exception $ex) {
992
-				$r->emit('\OC\Repair', 'error', [$ex->getMessage()]);
993
-				\OC::$server->getLogger()->logException($ex);
994
-			}
995
-		}
996
-		// run the steps
997
-		$r->run();
998
-	}
999
-
1000
-	public static function setupBackgroundJobs(array $jobs) {
1001
-		$queue = \OC::$server->getJobList();
1002
-		foreach ($jobs as $job) {
1003
-			$queue->add($job);
1004
-		}
1005
-	}
1006
-
1007
-	/**
1008
-	 * @param string $appId
1009
-	 * @param string[] $steps
1010
-	 */
1011
-	private static function setupLiveMigrations(string $appId, array $steps) {
1012
-		$queue = \OC::$server->getJobList();
1013
-		foreach ($steps as $step) {
1014
-			$queue->add('OC\Migration\BackgroundRepair', [
1015
-				'app' => $appId,
1016
-				'step' => $step]);
1017
-		}
1018
-	}
1019
-
1020
-	/**
1021
-	 * @param string $appId
1022
-	 * @return \OC\Files\View|false
1023
-	 */
1024
-	public static function getStorage(string $appId) {
1025
-		if (\OC::$server->getAppManager()->isEnabledForUser($appId)) { //sanity check
1026
-			if (\OC::$server->getUserSession()->isLoggedIn()) {
1027
-				$view = new \OC\Files\View('/' . OC_User::getUser());
1028
-				if (!$view->file_exists($appId)) {
1029
-					$view->mkdir($appId);
1030
-				}
1031
-				return new \OC\Files\View('/' . OC_User::getUser() . '/' . $appId);
1032
-			} else {
1033
-				\OCP\Util::writeLog('core', 'Can\'t get app storage, app ' . $appId . ', user not logged in', \OCP\Util::ERROR);
1034
-				return false;
1035
-			}
1036
-		} else {
1037
-			\OCP\Util::writeLog('core', 'Can\'t get app storage, app ' . $appId . ' not enabled', \OCP\Util::ERROR);
1038
-			return false;
1039
-		}
1040
-	}
1041
-
1042
-	protected static function findBestL10NOption(array $options, string $lang): string {
1043
-		$fallback = $similarLangFallback = $englishFallback = false;
1044
-
1045
-		$lang = strtolower($lang);
1046
-		$similarLang = $lang;
1047
-		if (strpos($similarLang, '_')) {
1048
-			// For "de_DE" we want to find "de" and the other way around
1049
-			$similarLang = substr($lang, 0, strpos($lang, '_'));
1050
-		}
1051
-
1052
-		foreach ($options as $option) {
1053
-			if (is_array($option)) {
1054
-				if ($fallback === false) {
1055
-					$fallback = $option['@value'];
1056
-				}
1057
-
1058
-				if (!isset($option['@attributes']['lang'])) {
1059
-					continue;
1060
-				}
1061
-
1062
-				$attributeLang = strtolower($option['@attributes']['lang']);
1063
-				if ($attributeLang === $lang) {
1064
-					return $option['@value'];
1065
-				}
1066
-
1067
-				if ($attributeLang === $similarLang) {
1068
-					$similarLangFallback = $option['@value'];
1069
-				} else if (strpos($attributeLang, $similarLang . '_') === 0) {
1070
-					if ($similarLangFallback === false) {
1071
-						$similarLangFallback =  $option['@value'];
1072
-					}
1073
-				}
1074
-			} else {
1075
-				$englishFallback = $option;
1076
-			}
1077
-		}
1078
-
1079
-		if ($similarLangFallback !== false) {
1080
-			return $similarLangFallback;
1081
-		} else if ($englishFallback !== false) {
1082
-			return $englishFallback;
1083
-		}
1084
-		return (string) $fallback;
1085
-	}
1086
-
1087
-	/**
1088
-	 * parses the app data array and enhanced the 'description' value
1089
-	 *
1090
-	 * @param array $data the app data
1091
-	 * @param string $lang
1092
-	 * @return array improved app data
1093
-	 */
1094
-	public static function parseAppInfo(array $data, $lang = null): array {
1095
-
1096
-		if ($lang && isset($data['name']) && is_array($data['name'])) {
1097
-			$data['name'] = self::findBestL10NOption($data['name'], $lang);
1098
-		}
1099
-		if ($lang && isset($data['summary']) && is_array($data['summary'])) {
1100
-			$data['summary'] = self::findBestL10NOption($data['summary'], $lang);
1101
-		}
1102
-		if ($lang && isset($data['description']) && is_array($data['description'])) {
1103
-			$data['description'] = trim(self::findBestL10NOption($data['description'], $lang));
1104
-		} else if (isset($data['description']) && is_string($data['description'])) {
1105
-			$data['description'] = trim($data['description']);
1106
-		} else  {
1107
-			$data['description'] = '';
1108
-		}
1109
-
1110
-		return $data;
1111
-	}
1112
-
1113
-	/**
1114
-	 * @param \OCP\IConfig $config
1115
-	 * @param \OCP\IL10N $l
1116
-	 * @param array $info
1117
-	 * @throws \Exception
1118
-	 */
1119
-	public static function checkAppDependencies(\OCP\IConfig $config, \OCP\IL10N $l, array $info) {
1120
-		$dependencyAnalyzer = new DependencyAnalyzer(new Platform($config), $l);
1121
-		$missing = $dependencyAnalyzer->analyze($info);
1122
-		if (!empty($missing)) {
1123
-			$missingMsg = implode(PHP_EOL, $missing);
1124
-			throw new \Exception(
1125
-				$l->t('App "%s" cannot be installed because the following dependencies are not fulfilled: %s',
1126
-					[$info['name'], $missingMsg]
1127
-				)
1128
-			);
1129
-		}
1130
-	}
66
+    static private $adminForms = [];
67
+    static private $personalForms = [];
68
+    static private $appTypes = [];
69
+    static private $loadedApps = [];
70
+    static private $altLogin = [];
71
+    static private $alreadyRegistered = [];
72
+    const officialApp = 200;
73
+
74
+    /**
75
+     * clean the appId
76
+     *
77
+     * @param string $app AppId that needs to be cleaned
78
+     * @return string
79
+     */
80
+    public static function cleanAppId(string $app): string {
81
+        return str_replace(array('\0', '/', '\\', '..'), '', $app);
82
+    }
83
+
84
+    /**
85
+     * Check if an app is loaded
86
+     *
87
+     * @param string $app
88
+     * @return bool
89
+     */
90
+    public static function isAppLoaded(string $app): bool {
91
+        return in_array($app, self::$loadedApps, true);
92
+    }
93
+
94
+    /**
95
+     * loads all apps
96
+     *
97
+     * @param string[] $types
98
+     * @return bool
99
+     *
100
+     * This function walks through the ownCloud directory and loads all apps
101
+     * it can find. A directory contains an app if the file /appinfo/info.xml
102
+     * exists.
103
+     *
104
+     * if $types is set to non-empty array, only apps of those types will be loaded
105
+     */
106
+    public static function loadApps(array $types = []): bool {
107
+        if (\OC::$server->getSystemConfig()->getValue('maintenance', false)) {
108
+            return false;
109
+        }
110
+        // Load the enabled apps here
111
+        $apps = self::getEnabledApps();
112
+
113
+        // Add each apps' folder as allowed class path
114
+        foreach($apps as $app) {
115
+            $path = self::getAppPath($app);
116
+            if($path !== false) {
117
+                self::registerAutoloading($app, $path);
118
+            }
119
+        }
120
+
121
+        // prevent app.php from printing output
122
+        ob_start();
123
+        foreach ($apps as $app) {
124
+            if (($types === [] or self::isType($app, $types)) && !in_array($app, self::$loadedApps)) {
125
+                self::loadApp($app);
126
+            }
127
+        }
128
+        ob_end_clean();
129
+
130
+        return true;
131
+    }
132
+
133
+    /**
134
+     * load a single app
135
+     *
136
+     * @param string $app
137
+     * @throws Exception
138
+     */
139
+    public static function loadApp(string $app) {
140
+        self::$loadedApps[] = $app;
141
+        $appPath = self::getAppPath($app);
142
+        if($appPath === false) {
143
+            return;
144
+        }
145
+
146
+        // in case someone calls loadApp() directly
147
+        self::registerAutoloading($app, $appPath);
148
+
149
+        if (is_file($appPath . '/appinfo/app.php')) {
150
+            \OC::$server->getEventLogger()->start('load_app_' . $app, 'Load app: ' . $app);
151
+            try {
152
+                self::requireAppFile($app);
153
+            } catch (Error $ex) {
154
+                \OC::$server->getLogger()->logException($ex);
155
+                if (!\OC::$server->getAppManager()->isShipped($app)) {
156
+                    // Only disable apps which are not shipped
157
+                    self::disable($app);
158
+                }
159
+            }
160
+            if (self::isType($app, array('authentication'))) {
161
+                // since authentication apps affect the "is app enabled for group" check,
162
+                // the enabled apps cache needs to be cleared to make sure that the
163
+                // next time getEnableApps() is called it will also include apps that were
164
+                // enabled for groups
165
+                self::$enabledAppsCache = [];
166
+            }
167
+            \OC::$server->getEventLogger()->end('load_app_' . $app);
168
+        }
169
+
170
+        $info = self::getAppInfo($app);
171
+        if (!empty($info['activity']['filters'])) {
172
+            foreach ($info['activity']['filters'] as $filter) {
173
+                \OC::$server->getActivityManager()->registerFilter($filter);
174
+            }
175
+        }
176
+        if (!empty($info['activity']['settings'])) {
177
+            foreach ($info['activity']['settings'] as $setting) {
178
+                \OC::$server->getActivityManager()->registerSetting($setting);
179
+            }
180
+        }
181
+        if (!empty($info['activity']['providers'])) {
182
+            foreach ($info['activity']['providers'] as $provider) {
183
+                \OC::$server->getActivityManager()->registerProvider($provider);
184
+            }
185
+        }
186
+
187
+        if (!empty($info['settings']['admin'])) {
188
+            foreach ($info['settings']['admin'] as $setting) {
189
+                \OC::$server->getSettingsManager()->registerSetting('admin', $setting);
190
+            }
191
+        }
192
+        if (!empty($info['settings']['admin-section'])) {
193
+            foreach ($info['settings']['admin-section'] as $section) {
194
+                \OC::$server->getSettingsManager()->registerSection('admin', $section);
195
+            }
196
+        }
197
+        if (!empty($info['settings']['personal'])) {
198
+            foreach ($info['settings']['personal'] as $setting) {
199
+                \OC::$server->getSettingsManager()->registerSetting('personal', $setting);
200
+            }
201
+        }
202
+        if (!empty($info['settings']['personal-section'])) {
203
+            foreach ($info['settings']['personal-section'] as $section) {
204
+                \OC::$server->getSettingsManager()->registerSection('personal', $section);
205
+            }
206
+        }
207
+
208
+        if (!empty($info['collaboration']['plugins'])) {
209
+            // deal with one or many plugin entries
210
+            $plugins = isset($info['collaboration']['plugins']['plugin']['@value']) ?
211
+                [$info['collaboration']['plugins']['plugin']] : $info['collaboration']['plugins']['plugin'];
212
+            foreach ($plugins as $plugin) {
213
+                if($plugin['@attributes']['type'] === 'collaborator-search') {
214
+                    $pluginInfo = [
215
+                        'shareType' => $plugin['@attributes']['share-type'],
216
+                        'class' => $plugin['@value'],
217
+                    ];
218
+                    \OC::$server->getCollaboratorSearch()->registerPlugin($pluginInfo);
219
+                } else if ($plugin['@attributes']['type'] === 'autocomplete-sort') {
220
+                    \OC::$server->getAutoCompleteManager()->registerSorter($plugin['@value']);
221
+                }
222
+            }
223
+        }
224
+    }
225
+
226
+    /**
227
+     * @internal
228
+     * @param string $app
229
+     * @param string $path
230
+     */
231
+    public static function registerAutoloading(string $app, string $path) {
232
+        $key = $app . '-' . $path;
233
+        if(isset(self::$alreadyRegistered[$key])) {
234
+            return;
235
+        }
236
+
237
+        self::$alreadyRegistered[$key] = true;
238
+
239
+        // Register on PSR-4 composer autoloader
240
+        $appNamespace = \OC\AppFramework\App::buildAppNamespace($app);
241
+        \OC::$server->registerNamespace($app, $appNamespace);
242
+
243
+        if (file_exists($path . '/composer/autoload.php')) {
244
+            require_once $path . '/composer/autoload.php';
245
+        } else {
246
+            \OC::$composerAutoloader->addPsr4($appNamespace . '\\', $path . '/lib/', true);
247
+            // Register on legacy autoloader
248
+            \OC::$loader->addValidRoot($path);
249
+        }
250
+
251
+        // Register Test namespace only when testing
252
+        if (defined('PHPUNIT_RUN') || defined('CLI_TEST_RUN')) {
253
+            \OC::$composerAutoloader->addPsr4($appNamespace . '\\Tests\\', $path . '/tests/', true);
254
+        }
255
+    }
256
+
257
+    /**
258
+     * Load app.php from the given app
259
+     *
260
+     * @param string $app app name
261
+     * @throws Error
262
+     */
263
+    private static function requireAppFile(string $app) {
264
+        // encapsulated here to avoid variable scope conflicts
265
+        require_once $app . '/appinfo/app.php';
266
+    }
267
+
268
+    /**
269
+     * check if an app is of a specific type
270
+     *
271
+     * @param string $app
272
+     * @param array $types
273
+     * @return bool
274
+     */
275
+    public static function isType(string $app, array $types): bool {
276
+        $appTypes = self::getAppTypes($app);
277
+        foreach ($types as $type) {
278
+            if (array_search($type, $appTypes) !== false) {
279
+                return true;
280
+            }
281
+        }
282
+        return false;
283
+    }
284
+
285
+    /**
286
+     * get the types of an app
287
+     *
288
+     * @param string $app
289
+     * @return array
290
+     */
291
+    private static function getAppTypes(string $app): array {
292
+        //load the cache
293
+        if (count(self::$appTypes) == 0) {
294
+            self::$appTypes = \OC::$server->getAppConfig()->getValues(false, 'types');
295
+        }
296
+
297
+        if (isset(self::$appTypes[$app])) {
298
+            return explode(',', self::$appTypes[$app]);
299
+        }
300
+
301
+        return [];
302
+    }
303
+
304
+    /**
305
+     * read app types from info.xml and cache them in the database
306
+     */
307
+    public static function setAppTypes(string $app) {
308
+        $appManager = \OC::$server->getAppManager();
309
+        $appData = $appManager->getAppInfo($app);
310
+        if(!is_array($appData)) {
311
+            return;
312
+        }
313
+
314
+        if (isset($appData['types'])) {
315
+            $appTypes = implode(',', $appData['types']);
316
+        } else {
317
+            $appTypes = '';
318
+            $appData['types'] = [];
319
+        }
320
+
321
+        $config = \OC::$server->getConfig();
322
+        $config->setAppValue($app, 'types', $appTypes);
323
+
324
+        if ($appManager->hasProtectedAppType($appData['types'])) {
325
+            $enabled = $config->getAppValue($app, 'enabled', 'yes');
326
+            if ($enabled !== 'yes' && $enabled !== 'no') {
327
+                $config->setAppValue($app, 'enabled', 'yes');
328
+            }
329
+        }
330
+    }
331
+
332
+    /**
333
+     * get all enabled apps
334
+     */
335
+    protected static $enabledAppsCache = [];
336
+
337
+    /**
338
+     * Returns apps enabled for the current user.
339
+     *
340
+     * @param bool $forceRefresh whether to refresh the cache
341
+     * @param bool $all whether to return apps for all users, not only the
342
+     * currently logged in one
343
+     * @return string[]
344
+     */
345
+    public static function getEnabledApps(bool $forceRefresh = false, bool $all = false): array {
346
+        if (!\OC::$server->getSystemConfig()->getValue('installed', false)) {
347
+            return [];
348
+        }
349
+        // in incognito mode or when logged out, $user will be false,
350
+        // which is also the case during an upgrade
351
+        $appManager = \OC::$server->getAppManager();
352
+        if ($all) {
353
+            $user = null;
354
+        } else {
355
+            $user = \OC::$server->getUserSession()->getUser();
356
+        }
357
+
358
+        if (is_null($user)) {
359
+            $apps = $appManager->getInstalledApps();
360
+        } else {
361
+            $apps = $appManager->getEnabledAppsForUser($user);
362
+        }
363
+        $apps = array_filter($apps, function ($app) {
364
+            return $app !== 'files';//we add this manually
365
+        });
366
+        sort($apps);
367
+        array_unshift($apps, 'files');
368
+        return $apps;
369
+    }
370
+
371
+    /**
372
+     * checks whether or not an app is enabled
373
+     *
374
+     * @param string $app app
375
+     * @return bool
376
+     * @deprecated 13.0.0 use \OC::$server->getAppManager()->isEnabledForUser($appId)
377
+     *
378
+     * This function checks whether or not an app is enabled.
379
+     */
380
+    public static function isEnabled(string $app): bool {
381
+        return \OC::$server->getAppManager()->isEnabledForUser($app);
382
+    }
383
+
384
+    /**
385
+     * enables an app
386
+     *
387
+     * @param string $appId
388
+     * @param array $groups (optional) when set, only these groups will have access to the app
389
+     * @throws \Exception
390
+     * @return void
391
+     *
392
+     * This function set an app as enabled in appconfig.
393
+     */
394
+    public function enable(string $appId,
395
+                            array $groups = []) {
396
+        self::$enabledAppsCache = []; // flush
397
+
398
+        // Check if app is already downloaded
399
+        $installer = \OC::$server->query(Installer::class);
400
+        $isDownloaded = $installer->isDownloaded($appId);
401
+
402
+        if(!$isDownloaded) {
403
+            $installer->downloadApp($appId);
404
+        }
405
+
406
+        $installer->installApp($appId);
407
+
408
+        $appManager = \OC::$server->getAppManager();
409
+        if ($groups !== []) {
410
+            $groupManager = \OC::$server->getGroupManager();
411
+            $groupsList = [];
412
+            foreach ($groups as $group) {
413
+                $groupItem = $groupManager->get($group);
414
+                if ($groupItem instanceof \OCP\IGroup) {
415
+                    $groupsList[] = $groupManager->get($group);
416
+                }
417
+            }
418
+            $appManager->enableAppForGroups($appId, $groupsList);
419
+        } else {
420
+            $appManager->enableApp($appId);
421
+        }
422
+    }
423
+
424
+    /**
425
+     * This function set an app as disabled in appconfig.
426
+     *
427
+     * @param string $app app
428
+     * @throws Exception
429
+     */
430
+    public static function disable(string $app) {
431
+        // flush
432
+        self::$enabledAppsCache = [];
433
+
434
+        // run uninstall steps
435
+        $appData = OC_App::getAppInfo($app);
436
+        if (!is_null($appData)) {
437
+            OC_App::executeRepairSteps($app, $appData['repair-steps']['uninstall']);
438
+        }
439
+
440
+        // emit disable hook - needed anymore ?
441
+        \OC_Hook::emit('OC_App', 'pre_disable', array('app' => $app));
442
+
443
+        // finally disable it
444
+        $appManager = \OC::$server->getAppManager();
445
+        $appManager->disableApp($app);
446
+    }
447
+
448
+    /**
449
+     * Get the path where to install apps
450
+     *
451
+     * @return string|false
452
+     */
453
+    public static function getInstallPath() {
454
+        if (\OC::$server->getSystemConfig()->getValue('appstoreenabled', true) == false) {
455
+            return false;
456
+        }
457
+
458
+        foreach (OC::$APPSROOTS as $dir) {
459
+            if (isset($dir['writable']) && $dir['writable'] === true) {
460
+                return $dir['path'];
461
+            }
462
+        }
463
+
464
+        \OCP\Util::writeLog('core', 'No application directories are marked as writable.', \OCP\Util::ERROR);
465
+        return null;
466
+    }
467
+
468
+
469
+    /**
470
+     * search for an app in all app-directories
471
+     *
472
+     * @param string $appId
473
+     * @return false|string
474
+     */
475
+    public static function findAppInDirectories(string $appId) {
476
+        $sanitizedAppId = self::cleanAppId($appId);
477
+        if($sanitizedAppId !== $appId) {
478
+            return false;
479
+        }
480
+        static $app_dir = [];
481
+
482
+        if (isset($app_dir[$appId])) {
483
+            return $app_dir[$appId];
484
+        }
485
+
486
+        $possibleApps = [];
487
+        foreach (OC::$APPSROOTS as $dir) {
488
+            if (file_exists($dir['path'] . '/' . $appId)) {
489
+                $possibleApps[] = $dir;
490
+            }
491
+        }
492
+
493
+        if (empty($possibleApps)) {
494
+            return false;
495
+        } elseif (count($possibleApps) === 1) {
496
+            $dir = array_shift($possibleApps);
497
+            $app_dir[$appId] = $dir;
498
+            return $dir;
499
+        } else {
500
+            $versionToLoad = [];
501
+            foreach ($possibleApps as $possibleApp) {
502
+                $version = self::getAppVersionByPath($possibleApp['path']);
503
+                if (empty($versionToLoad) || version_compare($version, $versionToLoad['version'], '>')) {
504
+                    $versionToLoad = array(
505
+                        'dir' => $possibleApp,
506
+                        'version' => $version,
507
+                    );
508
+                }
509
+            }
510
+            $app_dir[$appId] = $versionToLoad['dir'];
511
+            return $versionToLoad['dir'];
512
+            //TODO - write test
513
+        }
514
+    }
515
+
516
+    /**
517
+     * Get the directory for the given app.
518
+     * If the app is defined in multiple directories, the first one is taken. (false if not found)
519
+     *
520
+     * @param string $appId
521
+     * @return string|false
522
+     */
523
+    public static function getAppPath(string $appId) {
524
+        if ($appId === null || trim($appId) === '') {
525
+            return false;
526
+        }
527
+
528
+        if (($dir = self::findAppInDirectories($appId)) != false) {
529
+            return $dir['path'] . '/' . $appId;
530
+        }
531
+        return false;
532
+    }
533
+
534
+    /**
535
+     * Get the path for the given app on the access
536
+     * If the app is defined in multiple directories, the first one is taken. (false if not found)
537
+     *
538
+     * @param string $appId
539
+     * @return string|false
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
+     */
682
+    public static function registerLogIn(array $entry) {
683
+        self::$altLogin[] = $entry;
684
+    }
685
+
686
+    /**
687
+     * @return array
688
+     */
689
+    public static function getAlternativeLogIns(): array {
690
+        return self::$altLogin;
691
+    }
692
+
693
+    /**
694
+     * get a list of all apps in the apps folder
695
+     *
696
+     * @return array an array of app names (string IDs)
697
+     * @todo: change the name of this method to getInstalledApps, which is more accurate
698
+     */
699
+    public static function getAllApps(): array {
700
+
701
+        $apps = [];
702
+
703
+        foreach (OC::$APPSROOTS as $apps_dir) {
704
+            if (!is_readable($apps_dir['path'])) {
705
+                \OCP\Util::writeLog('core', 'unable to read app folder : ' . $apps_dir['path'], \OCP\Util::WARN);
706
+                continue;
707
+            }
708
+            $dh = opendir($apps_dir['path']);
709
+
710
+            if (is_resource($dh)) {
711
+                while (($file = readdir($dh)) !== false) {
712
+
713
+                    if ($file[0] != '.' and is_dir($apps_dir['path'] . '/' . $file) and is_file($apps_dir['path'] . '/' . $file . '/appinfo/info.xml')) {
714
+
715
+                        $apps[] = $file;
716
+                    }
717
+                }
718
+            }
719
+        }
720
+
721
+        $apps = array_unique($apps);
722
+
723
+        return $apps;
724
+    }
725
+
726
+    /**
727
+     * List all apps, this is used in apps.php
728
+     *
729
+     * @return array
730
+     */
731
+    public function listAllApps(): array {
732
+        $installedApps = OC_App::getAllApps();
733
+
734
+        $appManager = \OC::$server->getAppManager();
735
+        //we don't want to show configuration for these
736
+        $blacklist = $appManager->getAlwaysEnabledApps();
737
+        $appList = [];
738
+        $langCode = \OC::$server->getL10N('core')->getLanguageCode();
739
+        $urlGenerator = \OC::$server->getURLGenerator();
740
+
741
+        foreach ($installedApps as $app) {
742
+            if (array_search($app, $blacklist) === false) {
743
+
744
+                $info = OC_App::getAppInfo($app, false, $langCode);
745
+                if (!is_array($info)) {
746
+                    \OCP\Util::writeLog('core', 'Could not read app info file for app "' . $app . '"', \OCP\Util::ERROR);
747
+                    continue;
748
+                }
749
+
750
+                if (!isset($info['name'])) {
751
+                    \OCP\Util::writeLog('core', 'App id "' . $app . '" has no name in appinfo', \OCP\Util::ERROR);
752
+                    continue;
753
+                }
754
+
755
+                $enabled = \OC::$server->getConfig()->getAppValue($app, 'enabled', 'no');
756
+                $info['groups'] = null;
757
+                if ($enabled === 'yes') {
758
+                    $active = true;
759
+                } else if ($enabled === 'no') {
760
+                    $active = false;
761
+                } else {
762
+                    $active = true;
763
+                    $info['groups'] = $enabled;
764
+                }
765
+
766
+                $info['active'] = $active;
767
+
768
+                if ($appManager->isShipped($app)) {
769
+                    $info['internal'] = true;
770
+                    $info['level'] = self::officialApp;
771
+                    $info['removable'] = false;
772
+                } else {
773
+                    $info['internal'] = false;
774
+                    $info['removable'] = true;
775
+                }
776
+
777
+                $appPath = self::getAppPath($app);
778
+                if($appPath !== false) {
779
+                    $appIcon = $appPath . '/img/' . $app . '.svg';
780
+                    if (file_exists($appIcon)) {
781
+                        $info['preview'] = $urlGenerator->imagePath($app, $app . '.svg');
782
+                        $info['previewAsIcon'] = true;
783
+                    } else {
784
+                        $appIcon = $appPath . '/img/app.svg';
785
+                        if (file_exists($appIcon)) {
786
+                            $info['preview'] = $urlGenerator->imagePath($app, 'app.svg');
787
+                            $info['previewAsIcon'] = true;
788
+                        }
789
+                    }
790
+                }
791
+                // fix documentation
792
+                if (isset($info['documentation']) && is_array($info['documentation'])) {
793
+                    foreach ($info['documentation'] as $key => $url) {
794
+                        // If it is not an absolute URL we assume it is a key
795
+                        // i.e. admin-ldap will get converted to go.php?to=admin-ldap
796
+                        if (stripos($url, 'https://') !== 0 && stripos($url, 'http://') !== 0) {
797
+                            $url = $urlGenerator->linkToDocs($url);
798
+                        }
799
+
800
+                        $info['documentation'][$key] = $url;
801
+                    }
802
+                }
803
+
804
+                $info['version'] = OC_App::getAppVersion($app);
805
+                $appList[] = $info;
806
+            }
807
+        }
808
+
809
+        return $appList;
810
+    }
811
+
812
+    public static function shouldUpgrade(string $app): bool {
813
+        $versions = self::getAppVersions();
814
+        $currentVersion = OC_App::getAppVersion($app);
815
+        if ($currentVersion && isset($versions[$app])) {
816
+            $installedVersion = $versions[$app];
817
+            if (!version_compare($currentVersion, $installedVersion, '=')) {
818
+                return true;
819
+            }
820
+        }
821
+        return false;
822
+    }
823
+
824
+    /**
825
+     * Adjust the number of version parts of $version1 to match
826
+     * the number of version parts of $version2.
827
+     *
828
+     * @param string $version1 version to adjust
829
+     * @param string $version2 version to take the number of parts from
830
+     * @return string shortened $version1
831
+     */
832
+    private static function adjustVersionParts(string $version1, string $version2): string {
833
+        $version1 = explode('.', $version1);
834
+        $version2 = explode('.', $version2);
835
+        // reduce $version1 to match the number of parts in $version2
836
+        while (count($version1) > count($version2)) {
837
+            array_pop($version1);
838
+        }
839
+        // if $version1 does not have enough parts, add some
840
+        while (count($version1) < count($version2)) {
841
+            $version1[] = '0';
842
+        }
843
+        return implode('.', $version1);
844
+    }
845
+
846
+    /**
847
+     * Check whether the current ownCloud version matches the given
848
+     * application's version requirements.
849
+     *
850
+     * The comparison is made based on the number of parts that the
851
+     * app info version has. For example for ownCloud 6.0.3 if the
852
+     * app info version is expecting version 6.0, the comparison is
853
+     * made on the first two parts of the ownCloud version.
854
+     * This means that it's possible to specify "requiremin" => 6
855
+     * and "requiremax" => 6 and it will still match ownCloud 6.0.3.
856
+     *
857
+     * @param string $ocVersion ownCloud version to check against
858
+     * @param array $appInfo app info (from xml)
859
+     *
860
+     * @return boolean true if compatible, otherwise false
861
+     */
862
+    public static function isAppCompatible(string $ocVersion, array $appInfo): bool {
863
+        $requireMin = '';
864
+        $requireMax = '';
865
+        if (isset($appInfo['dependencies']['nextcloud']['@attributes']['min-version'])) {
866
+            $requireMin = $appInfo['dependencies']['nextcloud']['@attributes']['min-version'];
867
+        } elseif (isset($appInfo['dependencies']['owncloud']['@attributes']['min-version'])) {
868
+            $requireMin = $appInfo['dependencies']['owncloud']['@attributes']['min-version'];
869
+        } else if (isset($appInfo['requiremin'])) {
870
+            $requireMin = $appInfo['requiremin'];
871
+        } else if (isset($appInfo['require'])) {
872
+            $requireMin = $appInfo['require'];
873
+        }
874
+
875
+        if (isset($appInfo['dependencies']['nextcloud']['@attributes']['max-version'])) {
876
+            $requireMax = $appInfo['dependencies']['nextcloud']['@attributes']['max-version'];
877
+        } elseif (isset($appInfo['dependencies']['owncloud']['@attributes']['max-version'])) {
878
+            $requireMax = $appInfo['dependencies']['owncloud']['@attributes']['max-version'];
879
+        } else if (isset($appInfo['requiremax'])) {
880
+            $requireMax = $appInfo['requiremax'];
881
+        }
882
+
883
+        if (!empty($requireMin)
884
+            && version_compare(self::adjustVersionParts($ocVersion, $requireMin), $requireMin, '<')
885
+        ) {
886
+
887
+            return false;
888
+        }
889
+
890
+        if (!empty($requireMax)
891
+            && version_compare(self::adjustVersionParts($ocVersion, $requireMax), $requireMax, '>')
892
+        ) {
893
+            return false;
894
+        }
895
+
896
+        return true;
897
+    }
898
+
899
+    /**
900
+     * get the installed version of all apps
901
+     */
902
+    public static function getAppVersions() {
903
+        static $versions;
904
+
905
+        if(!$versions) {
906
+            $appConfig = \OC::$server->getAppConfig();
907
+            $versions = $appConfig->getValues(false, 'installed_version');
908
+        }
909
+        return $versions;
910
+    }
911
+
912
+    /**
913
+     * update the database for the app and call the update script
914
+     *
915
+     * @param string $appId
916
+     * @return bool
917
+     */
918
+    public static function updateApp(sstring $appId): bool {
919
+        $appPath = self::getAppPath($appId);
920
+        if($appPath === false) {
921
+            return false;
922
+        }
923
+        self::registerAutoloading($appId, $appPath);
924
+
925
+        $appData = self::getAppInfo($appId);
926
+        self::executeRepairSteps($appId, $appData['repair-steps']['pre-migration']);
927
+
928
+        if (file_exists($appPath . '/appinfo/database.xml')) {
929
+            OC_DB::updateDbFromStructure($appPath . '/appinfo/database.xml');
930
+        } else {
931
+            $ms = new MigrationService($appId, \OC::$server->getDatabaseConnection());
932
+            $ms->migrate();
933
+        }
934
+
935
+        self::executeRepairSteps($appId, $appData['repair-steps']['post-migration']);
936
+        self::setupLiveMigrations($appId, $appData['repair-steps']['live-migration']);
937
+        // update appversion in app manager
938
+        \OC::$server->getAppManager()->getAppVersion($appId, false);
939
+
940
+        // run upgrade code
941
+        if (file_exists($appPath . '/appinfo/update.php')) {
942
+            self::loadApp($appId);
943
+            include $appPath . '/appinfo/update.php';
944
+        }
945
+        self::setupBackgroundJobs($appData['background-jobs']);
946
+
947
+        //set remote/public handlers
948
+        if (array_key_exists('ocsid', $appData)) {
949
+            \OC::$server->getConfig()->setAppValue($appId, 'ocsid', $appData['ocsid']);
950
+        } elseif(\OC::$server->getConfig()->getAppValue($appId, 'ocsid', null) !== null) {
951
+            \OC::$server->getConfig()->deleteAppValue($appId, 'ocsid');
952
+        }
953
+        foreach ($appData['remote'] as $name => $path) {
954
+            \OC::$server->getConfig()->setAppValue('core', 'remote_' . $name, $appId . '/' . $path);
955
+        }
956
+        foreach ($appData['public'] as $name => $path) {
957
+            \OC::$server->getConfig()->setAppValue('core', 'public_' . $name, $appId . '/' . $path);
958
+        }
959
+
960
+        self::setAppTypes($appId);
961
+
962
+        $version = \OC_App::getAppVersion($appId);
963
+        \OC::$server->getConfig()->setAppValue($appId, 'installed_version', $version);
964
+
965
+        \OC::$server->getEventDispatcher()->dispatch(ManagerEvent::EVENT_APP_UPDATE, new ManagerEvent(
966
+            ManagerEvent::EVENT_APP_UPDATE, $appId
967
+        ));
968
+
969
+        return true;
970
+    }
971
+
972
+    /**
973
+     * @param string $appId
974
+     * @param string[] $steps
975
+     * @throws \OC\NeedsUpdateException
976
+     */
977
+    public static function executeRepairSteps(string $appId, array $steps) {
978
+        if (empty($steps)) {
979
+            return;
980
+        }
981
+        // load the app
982
+        self::loadApp($appId);
983
+
984
+        $dispatcher = OC::$server->getEventDispatcher();
985
+
986
+        // load the steps
987
+        $r = new Repair([], $dispatcher);
988
+        foreach ($steps as $step) {
989
+            try {
990
+                $r->addStep($step);
991
+            } catch (Exception $ex) {
992
+                $r->emit('\OC\Repair', 'error', [$ex->getMessage()]);
993
+                \OC::$server->getLogger()->logException($ex);
994
+            }
995
+        }
996
+        // run the steps
997
+        $r->run();
998
+    }
999
+
1000
+    public static function setupBackgroundJobs(array $jobs) {
1001
+        $queue = \OC::$server->getJobList();
1002
+        foreach ($jobs as $job) {
1003
+            $queue->add($job);
1004
+        }
1005
+    }
1006
+
1007
+    /**
1008
+     * @param string $appId
1009
+     * @param string[] $steps
1010
+     */
1011
+    private static function setupLiveMigrations(string $appId, array $steps) {
1012
+        $queue = \OC::$server->getJobList();
1013
+        foreach ($steps as $step) {
1014
+            $queue->add('OC\Migration\BackgroundRepair', [
1015
+                'app' => $appId,
1016
+                'step' => $step]);
1017
+        }
1018
+    }
1019
+
1020
+    /**
1021
+     * @param string $appId
1022
+     * @return \OC\Files\View|false
1023
+     */
1024
+    public static function getStorage(string $appId) {
1025
+        if (\OC::$server->getAppManager()->isEnabledForUser($appId)) { //sanity check
1026
+            if (\OC::$server->getUserSession()->isLoggedIn()) {
1027
+                $view = new \OC\Files\View('/' . OC_User::getUser());
1028
+                if (!$view->file_exists($appId)) {
1029
+                    $view->mkdir($appId);
1030
+                }
1031
+                return new \OC\Files\View('/' . OC_User::getUser() . '/' . $appId);
1032
+            } else {
1033
+                \OCP\Util::writeLog('core', 'Can\'t get app storage, app ' . $appId . ', user not logged in', \OCP\Util::ERROR);
1034
+                return false;
1035
+            }
1036
+        } else {
1037
+            \OCP\Util::writeLog('core', 'Can\'t get app storage, app ' . $appId . ' not enabled', \OCP\Util::ERROR);
1038
+            return false;
1039
+        }
1040
+    }
1041
+
1042
+    protected static function findBestL10NOption(array $options, string $lang): string {
1043
+        $fallback = $similarLangFallback = $englishFallback = false;
1044
+
1045
+        $lang = strtolower($lang);
1046
+        $similarLang = $lang;
1047
+        if (strpos($similarLang, '_')) {
1048
+            // For "de_DE" we want to find "de" and the other way around
1049
+            $similarLang = substr($lang, 0, strpos($lang, '_'));
1050
+        }
1051
+
1052
+        foreach ($options as $option) {
1053
+            if (is_array($option)) {
1054
+                if ($fallback === false) {
1055
+                    $fallback = $option['@value'];
1056
+                }
1057
+
1058
+                if (!isset($option['@attributes']['lang'])) {
1059
+                    continue;
1060
+                }
1061
+
1062
+                $attributeLang = strtolower($option['@attributes']['lang']);
1063
+                if ($attributeLang === $lang) {
1064
+                    return $option['@value'];
1065
+                }
1066
+
1067
+                if ($attributeLang === $similarLang) {
1068
+                    $similarLangFallback = $option['@value'];
1069
+                } else if (strpos($attributeLang, $similarLang . '_') === 0) {
1070
+                    if ($similarLangFallback === false) {
1071
+                        $similarLangFallback =  $option['@value'];
1072
+                    }
1073
+                }
1074
+            } else {
1075
+                $englishFallback = $option;
1076
+            }
1077
+        }
1078
+
1079
+        if ($similarLangFallback !== false) {
1080
+            return $similarLangFallback;
1081
+        } else if ($englishFallback !== false) {
1082
+            return $englishFallback;
1083
+        }
1084
+        return (string) $fallback;
1085
+    }
1086
+
1087
+    /**
1088
+     * parses the app data array and enhanced the 'description' value
1089
+     *
1090
+     * @param array $data the app data
1091
+     * @param string $lang
1092
+     * @return array improved app data
1093
+     */
1094
+    public static function parseAppInfo(array $data, $lang = null): array {
1095
+
1096
+        if ($lang && isset($data['name']) && is_array($data['name'])) {
1097
+            $data['name'] = self::findBestL10NOption($data['name'], $lang);
1098
+        }
1099
+        if ($lang && isset($data['summary']) && is_array($data['summary'])) {
1100
+            $data['summary'] = self::findBestL10NOption($data['summary'], $lang);
1101
+        }
1102
+        if ($lang && isset($data['description']) && is_array($data['description'])) {
1103
+            $data['description'] = trim(self::findBestL10NOption($data['description'], $lang));
1104
+        } else if (isset($data['description']) && is_string($data['description'])) {
1105
+            $data['description'] = trim($data['description']);
1106
+        } else  {
1107
+            $data['description'] = '';
1108
+        }
1109
+
1110
+        return $data;
1111
+    }
1112
+
1113
+    /**
1114
+     * @param \OCP\IConfig $config
1115
+     * @param \OCP\IL10N $l
1116
+     * @param array $info
1117
+     * @throws \Exception
1118
+     */
1119
+    public static function checkAppDependencies(\OCP\IConfig $config, \OCP\IL10N $l, array $info) {
1120
+        $dependencyAnalyzer = new DependencyAnalyzer(new Platform($config), $l);
1121
+        $missing = $dependencyAnalyzer->analyze($info);
1122
+        if (!empty($missing)) {
1123
+            $missingMsg = implode(PHP_EOL, $missing);
1124
+            throw new \Exception(
1125
+                $l->t('App "%s" cannot be installed because the following dependencies are not fulfilled: %s',
1126
+                    [$info['name'], $missingMsg]
1127
+                )
1128
+            );
1129
+        }
1130
+    }
1131 1131
 }
Please login to merge, or discard this patch.
Spacing   +50 added lines, -50 removed lines patch added patch discarded remove patch
@@ -1,5 +1,5 @@  discard block
 block discarded – undo
1 1
 <?php
2
-declare(strict_types=1);
2
+declare(strict_types = 1);
3 3
 /**
4 4
  * @copyright Copyright (c) 2016, ownCloud, Inc.
5 5
  * @copyright Copyright (c) 2016, Lukas Reschke <[email protected]>
@@ -111,9 +111,9 @@  discard block
 block discarded – undo
111 111
 		$apps = self::getEnabledApps();
112 112
 
113 113
 		// Add each apps' folder as allowed class path
114
-		foreach($apps as $app) {
114
+		foreach ($apps as $app) {
115 115
 			$path = self::getAppPath($app);
116
-			if($path !== false) {
116
+			if ($path !== false) {
117 117
 				self::registerAutoloading($app, $path);
118 118
 			}
119 119
 		}
@@ -139,15 +139,15 @@  discard block
 block discarded – undo
139 139
 	public static function loadApp(string $app) {
140 140
 		self::$loadedApps[] = $app;
141 141
 		$appPath = self::getAppPath($app);
142
-		if($appPath === false) {
142
+		if ($appPath === false) {
143 143
 			return;
144 144
 		}
145 145
 
146 146
 		// in case someone calls loadApp() directly
147 147
 		self::registerAutoloading($app, $appPath);
148 148
 
149
-		if (is_file($appPath . '/appinfo/app.php')) {
150
-			\OC::$server->getEventLogger()->start('load_app_' . $app, 'Load app: ' . $app);
149
+		if (is_file($appPath.'/appinfo/app.php')) {
150
+			\OC::$server->getEventLogger()->start('load_app_'.$app, 'Load app: '.$app);
151 151
 			try {
152 152
 				self::requireAppFile($app);
153 153
 			} catch (Error $ex) {
@@ -164,7 +164,7 @@  discard block
 block discarded – undo
164 164
 				// enabled for groups
165 165
 				self::$enabledAppsCache = [];
166 166
 			}
167
-			\OC::$server->getEventLogger()->end('load_app_' . $app);
167
+			\OC::$server->getEventLogger()->end('load_app_'.$app);
168 168
 		}
169 169
 
170 170
 		$info = self::getAppInfo($app);
@@ -210,7 +210,7 @@  discard block
 block discarded – undo
210 210
 			$plugins = isset($info['collaboration']['plugins']['plugin']['@value']) ?
211 211
 				[$info['collaboration']['plugins']['plugin']] : $info['collaboration']['plugins']['plugin'];
212 212
 			foreach ($plugins as $plugin) {
213
-				if($plugin['@attributes']['type'] === 'collaborator-search') {
213
+				if ($plugin['@attributes']['type'] === 'collaborator-search') {
214 214
 					$pluginInfo = [
215 215
 						'shareType' => $plugin['@attributes']['share-type'],
216 216
 						'class' => $plugin['@value'],
@@ -229,8 +229,8 @@  discard block
 block discarded – undo
229 229
 	 * @param string $path
230 230
 	 */
231 231
 	public static function registerAutoloading(string $app, string $path) {
232
-		$key = $app . '-' . $path;
233
-		if(isset(self::$alreadyRegistered[$key])) {
232
+		$key = $app.'-'.$path;
233
+		if (isset(self::$alreadyRegistered[$key])) {
234 234
 			return;
235 235
 		}
236 236
 
@@ -240,17 +240,17 @@  discard block
 block discarded – undo
240 240
 		$appNamespace = \OC\AppFramework\App::buildAppNamespace($app);
241 241
 		\OC::$server->registerNamespace($app, $appNamespace);
242 242
 
243
-		if (file_exists($path . '/composer/autoload.php')) {
244
-			require_once $path . '/composer/autoload.php';
243
+		if (file_exists($path.'/composer/autoload.php')) {
244
+			require_once $path.'/composer/autoload.php';
245 245
 		} else {
246
-			\OC::$composerAutoloader->addPsr4($appNamespace . '\\', $path . '/lib/', true);
246
+			\OC::$composerAutoloader->addPsr4($appNamespace.'\\', $path.'/lib/', true);
247 247
 			// Register on legacy autoloader
248 248
 			\OC::$loader->addValidRoot($path);
249 249
 		}
250 250
 
251 251
 		// Register Test namespace only when testing
252 252
 		if (defined('PHPUNIT_RUN') || defined('CLI_TEST_RUN')) {
253
-			\OC::$composerAutoloader->addPsr4($appNamespace . '\\Tests\\', $path . '/tests/', true);
253
+			\OC::$composerAutoloader->addPsr4($appNamespace.'\\Tests\\', $path.'/tests/', true);
254 254
 		}
255 255
 	}
256 256
 
@@ -262,7 +262,7 @@  discard block
 block discarded – undo
262 262
 	 */
263 263
 	private static function requireAppFile(string $app) {
264 264
 		// encapsulated here to avoid variable scope conflicts
265
-		require_once $app . '/appinfo/app.php';
265
+		require_once $app.'/appinfo/app.php';
266 266
 	}
267 267
 
268 268
 	/**
@@ -307,7 +307,7 @@  discard block
 block discarded – undo
307 307
 	public static function setAppTypes(string $app) {
308 308
 		$appManager = \OC::$server->getAppManager();
309 309
 		$appData = $appManager->getAppInfo($app);
310
-		if(!is_array($appData)) {
310
+		if (!is_array($appData)) {
311 311
 			return;
312 312
 		}
313 313
 
@@ -360,8 +360,8 @@  discard block
 block discarded – undo
360 360
 		} else {
361 361
 			$apps = $appManager->getEnabledAppsForUser($user);
362 362
 		}
363
-		$apps = array_filter($apps, function ($app) {
364
-			return $app !== 'files';//we add this manually
363
+		$apps = array_filter($apps, function($app) {
364
+			return $app !== 'files'; //we add this manually
365 365
 		});
366 366
 		sort($apps);
367 367
 		array_unshift($apps, 'files');
@@ -399,7 +399,7 @@  discard block
 block discarded – undo
399 399
 		$installer = \OC::$server->query(Installer::class);
400 400
 		$isDownloaded = $installer->isDownloaded($appId);
401 401
 
402
-		if(!$isDownloaded) {
402
+		if (!$isDownloaded) {
403 403
 			$installer->downloadApp($appId);
404 404
 		}
405 405
 
@@ -474,7 +474,7 @@  discard block
 block discarded – undo
474 474
 	 */
475 475
 	public static function findAppInDirectories(string $appId) {
476 476
 		$sanitizedAppId = self::cleanAppId($appId);
477
-		if($sanitizedAppId !== $appId) {
477
+		if ($sanitizedAppId !== $appId) {
478 478
 			return false;
479 479
 		}
480 480
 		static $app_dir = [];
@@ -485,7 +485,7 @@  discard block
 block discarded – undo
485 485
 
486 486
 		$possibleApps = [];
487 487
 		foreach (OC::$APPSROOTS as $dir) {
488
-			if (file_exists($dir['path'] . '/' . $appId)) {
488
+			if (file_exists($dir['path'].'/'.$appId)) {
489 489
 				$possibleApps[] = $dir;
490 490
 			}
491 491
 		}
@@ -526,7 +526,7 @@  discard block
 block discarded – undo
526 526
 		}
527 527
 
528 528
 		if (($dir = self::findAppInDirectories($appId)) != false) {
529
-			return $dir['path'] . '/' . $appId;
529
+			return $dir['path'].'/'.$appId;
530 530
 		}
531 531
 		return false;
532 532
 	}
@@ -540,7 +540,7 @@  discard block
 block discarded – undo
540 540
 	 */
541 541
 	public static function getAppWebPath(string $appId) {
542 542
 		if (($dir = self::findAppInDirectories($appId)) != false) {
543
-			return OC::$WEBROOT . $dir['url'] . '/' . $appId;
543
+			return OC::$WEBROOT.$dir['url'].'/'.$appId;
544 544
 		}
545 545
 		return false;
546 546
 	}
@@ -564,7 +564,7 @@  discard block
 block discarded – undo
564 564
 	 * @return string
565 565
 	 */
566 566
 	public static function getAppVersionByPath(string $path): string {
567
-		$infoFile = $path . '/appinfo/info.xml';
567
+		$infoFile = $path.'/appinfo/info.xml';
568 568
 		$appData = \OC::$server->getAppManager()->getAppInfo($infoFile, true);
569 569
 		return isset($appData['version']) ? $appData['version'] : '';
570 570
 	}
@@ -664,7 +664,7 @@  discard block
 block discarded – undo
664 664
 	 * @param string $page
665 665
 	 */
666 666
 	public static function registerAdmin(string $app, string $page) {
667
-		self::$adminForms[] = $app . '/' . $page . '.php';
667
+		self::$adminForms[] = $app.'/'.$page.'.php';
668 668
 	}
669 669
 
670 670
 	/**
@@ -673,7 +673,7 @@  discard block
 block discarded – undo
673 673
 	 * @param string $page
674 674
 	 */
675 675
 	public static function registerPersonal(string $app, string $page) {
676
-		self::$personalForms[] = $app . '/' . $page . '.php';
676
+		self::$personalForms[] = $app.'/'.$page.'.php';
677 677
 	}
678 678
 
679 679
 	/**
@@ -702,7 +702,7 @@  discard block
 block discarded – undo
702 702
 
703 703
 		foreach (OC::$APPSROOTS as $apps_dir) {
704 704
 			if (!is_readable($apps_dir['path'])) {
705
-				\OCP\Util::writeLog('core', 'unable to read app folder : ' . $apps_dir['path'], \OCP\Util::WARN);
705
+				\OCP\Util::writeLog('core', 'unable to read app folder : '.$apps_dir['path'], \OCP\Util::WARN);
706 706
 				continue;
707 707
 			}
708 708
 			$dh = opendir($apps_dir['path']);
@@ -710,7 +710,7 @@  discard block
 block discarded – undo
710 710
 			if (is_resource($dh)) {
711 711
 				while (($file = readdir($dh)) !== false) {
712 712
 
713
-					if ($file[0] != '.' and is_dir($apps_dir['path'] . '/' . $file) and is_file($apps_dir['path'] . '/' . $file . '/appinfo/info.xml')) {
713
+					if ($file[0] != '.' and is_dir($apps_dir['path'].'/'.$file) and is_file($apps_dir['path'].'/'.$file.'/appinfo/info.xml')) {
714 714
 
715 715
 						$apps[] = $file;
716 716
 					}
@@ -743,12 +743,12 @@  discard block
 block discarded – undo
743 743
 
744 744
 				$info = OC_App::getAppInfo($app, false, $langCode);
745 745
 				if (!is_array($info)) {
746
-					\OCP\Util::writeLog('core', 'Could not read app info file for app "' . $app . '"', \OCP\Util::ERROR);
746
+					\OCP\Util::writeLog('core', 'Could not read app info file for app "'.$app.'"', \OCP\Util::ERROR);
747 747
 					continue;
748 748
 				}
749 749
 
750 750
 				if (!isset($info['name'])) {
751
-					\OCP\Util::writeLog('core', 'App id "' . $app . '" has no name in appinfo', \OCP\Util::ERROR);
751
+					\OCP\Util::writeLog('core', 'App id "'.$app.'" has no name in appinfo', \OCP\Util::ERROR);
752 752
 					continue;
753 753
 				}
754 754
 
@@ -775,13 +775,13 @@  discard block
 block discarded – undo
775 775
 				}
776 776
 
777 777
 				$appPath = self::getAppPath($app);
778
-				if($appPath !== false) {
779
-					$appIcon = $appPath . '/img/' . $app . '.svg';
778
+				if ($appPath !== false) {
779
+					$appIcon = $appPath.'/img/'.$app.'.svg';
780 780
 					if (file_exists($appIcon)) {
781
-						$info['preview'] = $urlGenerator->imagePath($app, $app . '.svg');
781
+						$info['preview'] = $urlGenerator->imagePath($app, $app.'.svg');
782 782
 						$info['previewAsIcon'] = true;
783 783
 					} else {
784
-						$appIcon = $appPath . '/img/app.svg';
784
+						$appIcon = $appPath.'/img/app.svg';
785 785
 						if (file_exists($appIcon)) {
786 786
 							$info['preview'] = $urlGenerator->imagePath($app, 'app.svg');
787 787
 							$info['previewAsIcon'] = true;
@@ -902,7 +902,7 @@  discard block
 block discarded – undo
902 902
 	public static function getAppVersions() {
903 903
 		static $versions;
904 904
 
905
-		if(!$versions) {
905
+		if (!$versions) {
906 906
 			$appConfig = \OC::$server->getAppConfig();
907 907
 			$versions = $appConfig->getValues(false, 'installed_version');
908 908
 		}
@@ -917,7 +917,7 @@  discard block
 block discarded – undo
917 917
 	 */
918 918
 	public static function updateApp(sstring $appId): bool {
919 919
 		$appPath = self::getAppPath($appId);
920
-		if($appPath === false) {
920
+		if ($appPath === false) {
921 921
 			return false;
922 922
 		}
923 923
 		self::registerAutoloading($appId, $appPath);
@@ -925,8 +925,8 @@  discard block
 block discarded – undo
925 925
 		$appData = self::getAppInfo($appId);
926 926
 		self::executeRepairSteps($appId, $appData['repair-steps']['pre-migration']);
927 927
 
928
-		if (file_exists($appPath . '/appinfo/database.xml')) {
929
-			OC_DB::updateDbFromStructure($appPath . '/appinfo/database.xml');
928
+		if (file_exists($appPath.'/appinfo/database.xml')) {
929
+			OC_DB::updateDbFromStructure($appPath.'/appinfo/database.xml');
930 930
 		} else {
931 931
 			$ms = new MigrationService($appId, \OC::$server->getDatabaseConnection());
932 932
 			$ms->migrate();
@@ -938,23 +938,23 @@  discard block
 block discarded – undo
938 938
 		\OC::$server->getAppManager()->getAppVersion($appId, false);
939 939
 
940 940
 		// run upgrade code
941
-		if (file_exists($appPath . '/appinfo/update.php')) {
941
+		if (file_exists($appPath.'/appinfo/update.php')) {
942 942
 			self::loadApp($appId);
943
-			include $appPath . '/appinfo/update.php';
943
+			include $appPath.'/appinfo/update.php';
944 944
 		}
945 945
 		self::setupBackgroundJobs($appData['background-jobs']);
946 946
 
947 947
 		//set remote/public handlers
948 948
 		if (array_key_exists('ocsid', $appData)) {
949 949
 			\OC::$server->getConfig()->setAppValue($appId, 'ocsid', $appData['ocsid']);
950
-		} elseif(\OC::$server->getConfig()->getAppValue($appId, 'ocsid', null) !== null) {
950
+		} elseif (\OC::$server->getConfig()->getAppValue($appId, 'ocsid', null) !== null) {
951 951
 			\OC::$server->getConfig()->deleteAppValue($appId, 'ocsid');
952 952
 		}
953 953
 		foreach ($appData['remote'] as $name => $path) {
954
-			\OC::$server->getConfig()->setAppValue('core', 'remote_' . $name, $appId . '/' . $path);
954
+			\OC::$server->getConfig()->setAppValue('core', 'remote_'.$name, $appId.'/'.$path);
955 955
 		}
956 956
 		foreach ($appData['public'] as $name => $path) {
957
-			\OC::$server->getConfig()->setAppValue('core', 'public_' . $name, $appId . '/' . $path);
957
+			\OC::$server->getConfig()->setAppValue('core', 'public_'.$name, $appId.'/'.$path);
958 958
 		}
959 959
 
960 960
 		self::setAppTypes($appId);
@@ -1024,17 +1024,17 @@  discard block
 block discarded – undo
1024 1024
 	public static function getStorage(string $appId) {
1025 1025
 		if (\OC::$server->getAppManager()->isEnabledForUser($appId)) { //sanity check
1026 1026
 			if (\OC::$server->getUserSession()->isLoggedIn()) {
1027
-				$view = new \OC\Files\View('/' . OC_User::getUser());
1027
+				$view = new \OC\Files\View('/'.OC_User::getUser());
1028 1028
 				if (!$view->file_exists($appId)) {
1029 1029
 					$view->mkdir($appId);
1030 1030
 				}
1031
-				return new \OC\Files\View('/' . OC_User::getUser() . '/' . $appId);
1031
+				return new \OC\Files\View('/'.OC_User::getUser().'/'.$appId);
1032 1032
 			} else {
1033
-				\OCP\Util::writeLog('core', 'Can\'t get app storage, app ' . $appId . ', user not logged in', \OCP\Util::ERROR);
1033
+				\OCP\Util::writeLog('core', 'Can\'t get app storage, app '.$appId.', user not logged in', \OCP\Util::ERROR);
1034 1034
 				return false;
1035 1035
 			}
1036 1036
 		} else {
1037
-			\OCP\Util::writeLog('core', 'Can\'t get app storage, app ' . $appId . ' not enabled', \OCP\Util::ERROR);
1037
+			\OCP\Util::writeLog('core', 'Can\'t get app storage, app '.$appId.' not enabled', \OCP\Util::ERROR);
1038 1038
 			return false;
1039 1039
 		}
1040 1040
 	}
@@ -1066,9 +1066,9 @@  discard block
 block discarded – undo
1066 1066
 
1067 1067
 				if ($attributeLang === $similarLang) {
1068 1068
 					$similarLangFallback = $option['@value'];
1069
-				} else if (strpos($attributeLang, $similarLang . '_') === 0) {
1069
+				} else if (strpos($attributeLang, $similarLang.'_') === 0) {
1070 1070
 					if ($similarLangFallback === false) {
1071
-						$similarLangFallback =  $option['@value'];
1071
+						$similarLangFallback = $option['@value'];
1072 1072
 					}
1073 1073
 				}
1074 1074
 			} else {
@@ -1103,7 +1103,7 @@  discard block
 block discarded – undo
1103 1103
 			$data['description'] = trim(self::findBestL10NOption($data['description'], $lang));
1104 1104
 		} else if (isset($data['description']) && is_string($data['description'])) {
1105 1105
 			$data['description'] = trim($data['description']);
1106
-		} else  {
1106
+		} else {
1107 1107
 			$data['description'] = '';
1108 1108
 		}
1109 1109
 
Please login to merge, or discard this patch.
lib/private/Installer.php 1 patch
Indentation   +559 added lines, -559 removed lines patch added patch discarded remove patch
@@ -52,563 +52,563 @@
 block discarded – undo
52 52
  * This class provides the functionality needed to install, update and remove apps
53 53
  */
54 54
 class Installer {
55
-	/** @var AppFetcher */
56
-	private $appFetcher;
57
-	/** @var IClientService */
58
-	private $clientService;
59
-	/** @var ITempManager */
60
-	private $tempManager;
61
-	/** @var ILogger */
62
-	private $logger;
63
-	/** @var IConfig */
64
-	private $config;
65
-	/** @var array - for caching the result of app fetcher */
66
-	private $apps = null;
67
-	/** @var bool|null - for caching the result of the ready status */
68
-	private $isInstanceReadyForUpdates = null;
69
-
70
-	/**
71
-	 * @param AppFetcher $appFetcher
72
-	 * @param IClientService $clientService
73
-	 * @param ITempManager $tempManager
74
-	 * @param ILogger $logger
75
-	 * @param IConfig $config
76
-	 */
77
-	public function __construct(AppFetcher $appFetcher,
78
-								IClientService $clientService,
79
-								ITempManager $tempManager,
80
-								ILogger $logger,
81
-								IConfig $config) {
82
-		$this->appFetcher = $appFetcher;
83
-		$this->clientService = $clientService;
84
-		$this->tempManager = $tempManager;
85
-		$this->logger = $logger;
86
-		$this->config = $config;
87
-	}
88
-
89
-	/**
90
-	 * Installs an app that is located in one of the app folders already
91
-	 *
92
-	 * @param string $appId App to install
93
-	 * @throws \Exception
94
-	 * @return string app ID
95
-	 */
96
-	public function installApp($appId) {
97
-		$app = \OC_App::findAppInDirectories($appId);
98
-		if($app === false) {
99
-			throw new \Exception('App not found in any app directory');
100
-		}
101
-
102
-		$basedir = $app['path'].'/'.$appId;
103
-		$info = OC_App::getAppInfo($basedir.'/appinfo/info.xml', true);
104
-
105
-		$l = \OC::$server->getL10N('core');
106
-
107
-		if(!is_array($info)) {
108
-			throw new \Exception(
109
-				$l->t('App "%s" cannot be installed because appinfo file cannot be read.',
110
-					[$info['name']]
111
-				)
112
-			);
113
-		}
114
-
115
-		$version = implode('.', \OCP\Util::getVersion());
116
-		if (!\OC_App::isAppCompatible($version, $info)) {
117
-			throw new \Exception(
118
-				// TODO $l
119
-				$l->t('App "%s" cannot be installed because it is not compatible with this version of the server.',
120
-					[$info['name']]
121
-				)
122
-			);
123
-		}
124
-
125
-		// check for required dependencies
126
-		\OC_App::checkAppDependencies($this->config, $l, $info);
127
-		\OC_App::registerAutoloading($appId, $basedir);
128
-
129
-		//install the database
130
-		if(is_file($basedir.'/appinfo/database.xml')) {
131
-			if (\OC::$server->getConfig()->getAppValue($info['id'], 'installed_version') === null) {
132
-				OC_DB::createDbFromStructure($basedir.'/appinfo/database.xml');
133
-			} else {
134
-				OC_DB::updateDbFromStructure($basedir.'/appinfo/database.xml');
135
-			}
136
-		} else {
137
-			$ms = new \OC\DB\MigrationService($info['id'], \OC::$server->getDatabaseConnection());
138
-			$ms->migrate();
139
-		}
140
-
141
-		\OC_App::setupBackgroundJobs($info['background-jobs']);
142
-
143
-		//run appinfo/install.php
144
-		if(!isset($data['noinstall']) or $data['noinstall']==false) {
145
-			self::includeAppScript($basedir . '/appinfo/install.php');
146
-		}
147
-
148
-		$appData = OC_App::getAppInfo($appId);
149
-		OC_App::executeRepairSteps($appId, $appData['repair-steps']['install']);
150
-
151
-		//set the installed version
152
-		\OC::$server->getConfig()->setAppValue($info['id'], 'installed_version', OC_App::getAppVersion($info['id'], false));
153
-		\OC::$server->getConfig()->setAppValue($info['id'], 'enabled', 'no');
154
-
155
-		//set remote/public handlers
156
-		foreach($info['remote'] as $name=>$path) {
157
-			\OC::$server->getConfig()->setAppValue('core', 'remote_'.$name, $info['id'].'/'.$path);
158
-		}
159
-		foreach($info['public'] as $name=>$path) {
160
-			\OC::$server->getConfig()->setAppValue('core', 'public_'.$name, $info['id'].'/'.$path);
161
-		}
162
-
163
-		OC_App::setAppTypes($info['id']);
164
-
165
-		return $info['id'];
166
-	}
167
-
168
-	/**
169
-	 * @brief checks whether or not an app is installed
170
-	 * @param string $app app
171
-	 * @returns bool
172
-	 *
173
-	 * Checks whether or not an app is installed, i.e. registered in apps table.
174
-	 */
175
-	public static function isInstalled( $app ) {
176
-		return (\OC::$server->getConfig()->getAppValue($app, "installed_version", null) !== null);
177
-	}
178
-
179
-	/**
180
-	 * Updates the specified app from the appstore
181
-	 *
182
-	 * @param string $appId
183
-	 * @return bool
184
-	 */
185
-	public function updateAppstoreApp($appId) {
186
-		if($this->isUpdateAvailable($appId)) {
187
-			try {
188
-				$this->downloadApp($appId);
189
-			} catch (\Exception $e) {
190
-				$this->logger->logException($e, [
191
-					'level' => \OCP\Util::ERROR,
192
-					'app' => 'core',
193
-				]);
194
-				return false;
195
-			}
196
-			return OC_App::updateApp($appId);
197
-		}
198
-
199
-		return false;
200
-	}
201
-
202
-	/**
203
-	 * Downloads an app and puts it into the app directory
204
-	 *
205
-	 * @param string $appId
206
-	 *
207
-	 * @throws \Exception If the installation was not successful
208
-	 */
209
-	public function downloadApp($appId) {
210
-		$appId = strtolower($appId);
211
-
212
-		$apps = $this->appFetcher->get();
213
-		foreach($apps as $app) {
214
-			if($app['id'] === $appId) {
215
-				// Load the certificate
216
-				$certificate = new X509();
217
-				$certificate->loadCA(file_get_contents(__DIR__ . '/../../resources/codesigning/root.crt'));
218
-				$loadedCertificate = $certificate->loadX509($app['certificate']);
219
-
220
-				// Verify if the certificate has been revoked
221
-				$crl = new X509();
222
-				$crl->loadCA(file_get_contents(__DIR__ . '/../../resources/codesigning/root.crt'));
223
-				$crl->loadCRL(file_get_contents(__DIR__ . '/../../resources/codesigning/root.crl'));
224
-				if($crl->validateSignature() !== true) {
225
-					throw new \Exception('Could not validate CRL signature');
226
-				}
227
-				$csn = $loadedCertificate['tbsCertificate']['serialNumber']->toString();
228
-				$revoked = $crl->getRevoked($csn);
229
-				if ($revoked !== false) {
230
-					throw new \Exception(
231
-						sprintf(
232
-							'Certificate "%s" has been revoked',
233
-							$csn
234
-						)
235
-					);
236
-				}
237
-
238
-				// Verify if the certificate has been issued by the Nextcloud Code Authority CA
239
-				if($certificate->validateSignature() !== true) {
240
-					throw new \Exception(
241
-						sprintf(
242
-							'App with id %s has a certificate not issued by a trusted Code Signing Authority',
243
-							$appId
244
-						)
245
-					);
246
-				}
247
-
248
-				// Verify if the certificate is issued for the requested app id
249
-				$certInfo = openssl_x509_parse($app['certificate']);
250
-				if(!isset($certInfo['subject']['CN'])) {
251
-					throw new \Exception(
252
-						sprintf(
253
-							'App with id %s has a cert with no CN',
254
-							$appId
255
-						)
256
-					);
257
-				}
258
-				if($certInfo['subject']['CN'] !== $appId) {
259
-					throw new \Exception(
260
-						sprintf(
261
-							'App with id %s has a cert issued to %s',
262
-							$appId,
263
-							$certInfo['subject']['CN']
264
-						)
265
-					);
266
-				}
267
-
268
-				// Download the release
269
-				$tempFile = $this->tempManager->getTemporaryFile('.tar.gz');
270
-				$client = $this->clientService->newClient();
271
-				$client->get($app['releases'][0]['download'], ['save_to' => $tempFile]);
272
-
273
-				// Check if the signature actually matches the downloaded content
274
-				$certificate = openssl_get_publickey($app['certificate']);
275
-				$verified = (bool)openssl_verify(file_get_contents($tempFile), base64_decode($app['releases'][0]['signature']), $certificate, OPENSSL_ALGO_SHA512);
276
-				openssl_free_key($certificate);
277
-
278
-				if($verified === true) {
279
-					// Seems to match, let's proceed
280
-					$extractDir = $this->tempManager->getTemporaryFolder();
281
-					$archive = new TAR($tempFile);
282
-
283
-					if($archive) {
284
-						if (!$archive->extract($extractDir)) {
285
-							throw new \Exception(
286
-								sprintf(
287
-									'Could not extract app %s',
288
-									$appId
289
-								)
290
-							);
291
-						}
292
-						$allFiles = scandir($extractDir);
293
-						$folders = array_diff($allFiles, ['.', '..']);
294
-						$folders = array_values($folders);
295
-
296
-						if(count($folders) > 1) {
297
-							throw new \Exception(
298
-								sprintf(
299
-									'Extracted app %s has more than 1 folder',
300
-									$appId
301
-								)
302
-							);
303
-						}
304
-
305
-						// Check if appinfo/info.xml has the same app ID as well
306
-						$loadEntities = libxml_disable_entity_loader(false);
307
-						$xml = simplexml_load_file($extractDir . '/' . $folders[0] . '/appinfo/info.xml');
308
-						libxml_disable_entity_loader($loadEntities);
309
-						if((string)$xml->id !== $appId) {
310
-							throw new \Exception(
311
-								sprintf(
312
-									'App for id %s has a wrong app ID in info.xml: %s',
313
-									$appId,
314
-									(string)$xml->id
315
-								)
316
-							);
317
-						}
318
-
319
-						// Check if the version is lower than before
320
-						$currentVersion = OC_App::getAppVersion($appId);
321
-						$newVersion = (string)$xml->version;
322
-						if(version_compare($currentVersion, $newVersion) === 1) {
323
-							throw new \Exception(
324
-								sprintf(
325
-									'App for id %s has version %s and tried to update to lower version %s',
326
-									$appId,
327
-									$currentVersion,
328
-									$newVersion
329
-								)
330
-							);
331
-						}
332
-
333
-						$baseDir = OC_App::getInstallPath() . '/' . $appId;
334
-						// Remove old app with the ID if existent
335
-						OC_Helper::rmdirr($baseDir);
336
-						// Move to app folder
337
-						if(@mkdir($baseDir)) {
338
-							$extractDir .= '/' . $folders[0];
339
-							OC_Helper::copyr($extractDir, $baseDir);
340
-						}
341
-						OC_Helper::copyr($extractDir, $baseDir);
342
-						OC_Helper::rmdirr($extractDir);
343
-						return;
344
-					} else {
345
-						throw new \Exception(
346
-							sprintf(
347
-								'Could not extract app with ID %s to %s',
348
-								$appId,
349
-								$extractDir
350
-							)
351
-						);
352
-					}
353
-				} else {
354
-					// Signature does not match
355
-					throw new \Exception(
356
-						sprintf(
357
-							'App with id %s has invalid signature',
358
-							$appId
359
-						)
360
-					);
361
-				}
362
-			}
363
-		}
364
-
365
-		throw new \Exception(
366
-			sprintf(
367
-				'Could not download app %s',
368
-				$appId
369
-			)
370
-		);
371
-	}
372
-
373
-	/**
374
-	 * Check if an update for the app is available
375
-	 *
376
-	 * @param string $appId
377
-	 * @return string|false false or the version number of the update
378
-	 */
379
-	public function isUpdateAvailable($appId) {
380
-		if ($this->isInstanceReadyForUpdates === null) {
381
-			$installPath = OC_App::getInstallPath();
382
-			if ($installPath === false || $installPath === null) {
383
-				$this->isInstanceReadyForUpdates = false;
384
-			} else {
385
-				$this->isInstanceReadyForUpdates = true;
386
-			}
387
-		}
388
-
389
-		if ($this->isInstanceReadyForUpdates === false) {
390
-			return false;
391
-		}
392
-
393
-		if ($this->isInstalledFromGit($appId) === true) {
394
-			return false;
395
-		}
396
-
397
-		if ($this->apps === null) {
398
-			$this->apps = $this->appFetcher->get();
399
-		}
400
-
401
-		foreach($this->apps as $app) {
402
-			if($app['id'] === $appId) {
403
-				$currentVersion = OC_App::getAppVersion($appId);
404
-				$newestVersion = $app['releases'][0]['version'];
405
-				if (version_compare($newestVersion, $currentVersion, '>')) {
406
-					return $newestVersion;
407
-				} else {
408
-					return false;
409
-				}
410
-			}
411
-		}
412
-
413
-		return false;
414
-	}
415
-
416
-	/**
417
-	 * Check if app has been installed from git
418
-	 * @param string $name name of the application to remove
419
-	 * @return boolean
420
-	 *
421
-	 * The function will check if the path contains a .git folder
422
-	 */
423
-	private function isInstalledFromGit($appId) {
424
-		$app = \OC_App::findAppInDirectories($appId);
425
-		if($app === false) {
426
-			return false;
427
-		}
428
-		$basedir = $app['path'].'/'.$appId;
429
-		return file_exists($basedir.'/.git/');
430
-	}
431
-
432
-	/**
433
-	 * Check if app is already downloaded
434
-	 * @param string $name name of the application to remove
435
-	 * @return boolean
436
-	 *
437
-	 * The function will check if the app is already downloaded in the apps repository
438
-	 */
439
-	public function isDownloaded($name) {
440
-		foreach(\OC::$APPSROOTS as $dir) {
441
-			$dirToTest  = $dir['path'];
442
-			$dirToTest .= '/';
443
-			$dirToTest .= $name;
444
-			$dirToTest .= '/';
445
-
446
-			if (is_dir($dirToTest)) {
447
-				return true;
448
-			}
449
-		}
450
-
451
-		return false;
452
-	}
453
-
454
-	/**
455
-	 * Removes an app
456
-	 * @param string $appId ID of the application to remove
457
-	 * @return boolean
458
-	 *
459
-	 *
460
-	 * This function works as follows
461
-	 *   -# call uninstall repair steps
462
-	 *   -# removing the files
463
-	 *
464
-	 * The function will not delete preferences, tables and the configuration,
465
-	 * this has to be done by the function oc_app_uninstall().
466
-	 */
467
-	public function removeApp($appId) {
468
-		if($this->isDownloaded( $appId )) {
469
-			if (\OC::$server->getAppManager()->isShipped($appId)) {
470
-				return false;
471
-			}
472
-			$appDir = OC_App::getInstallPath() . '/' . $appId;
473
-			OC_Helper::rmdirr($appDir);
474
-			return true;
475
-		}else{
476
-			\OCP\Util::writeLog('core', 'can\'t remove app '.$appId.'. It is not installed.', \OCP\Util::ERROR);
477
-
478
-			return false;
479
-		}
480
-
481
-	}
482
-
483
-	/**
484
-	 * Installs the app within the bundle and marks the bundle as installed
485
-	 *
486
-	 * @param Bundle $bundle
487
-	 * @throws \Exception If app could not get installed
488
-	 */
489
-	public function installAppBundle(Bundle $bundle) {
490
-		$appIds = $bundle->getAppIdentifiers();
491
-		foreach($appIds as $appId) {
492
-			if(!$this->isDownloaded($appId)) {
493
-				$this->downloadApp($appId);
494
-			}
495
-			$this->installApp($appId);
496
-			$app = new OC_App();
497
-			$app->enable($appId);
498
-		}
499
-		$bundles = json_decode($this->config->getAppValue('core', 'installed.bundles', json_encode([])), true);
500
-		$bundles[] = $bundle->getIdentifier();
501
-		$this->config->setAppValue('core', 'installed.bundles', json_encode($bundles));
502
-	}
503
-
504
-	/**
505
-	 * Installs shipped apps
506
-	 *
507
-	 * This function installs all apps found in the 'apps' directory that should be enabled by default;
508
-	 * @param bool $softErrors When updating we ignore errors and simply log them, better to have a
509
-	 *                         working ownCloud at the end instead of an aborted update.
510
-	 * @return array Array of error messages (appid => Exception)
511
-	 */
512
-	public static function installShippedApps($softErrors = false) {
513
-		$errors = [];
514
-		foreach(\OC::$APPSROOTS as $app_dir) {
515
-			if($dir = opendir( $app_dir['path'] )) {
516
-				while( false !== ( $filename = readdir( $dir ))) {
517
-					if( $filename[0] !== '.' and is_dir($app_dir['path']."/$filename") ) {
518
-						if( file_exists( $app_dir['path']."/$filename/appinfo/info.xml" )) {
519
-							if(!Installer::isInstalled($filename)) {
520
-								$info=OC_App::getAppInfo($filename);
521
-								$enabled = isset($info['default_enable']);
522
-								if (($enabled || in_array($filename, \OC::$server->getAppManager()->getAlwaysEnabledApps()))
523
-									  && \OC::$server->getConfig()->getAppValue($filename, 'enabled') !== 'no') {
524
-									if ($softErrors) {
525
-										try {
526
-											Installer::installShippedApp($filename);
527
-										} catch (HintException $e) {
528
-											if ($e->getPrevious() instanceof TableExistsException) {
529
-												$errors[$filename] = $e;
530
-												continue;
531
-											}
532
-											throw $e;
533
-										}
534
-									} else {
535
-										Installer::installShippedApp($filename);
536
-									}
537
-									\OC::$server->getConfig()->setAppValue($filename, 'enabled', 'yes');
538
-								}
539
-							}
540
-						}
541
-					}
542
-				}
543
-				closedir( $dir );
544
-			}
545
-		}
546
-
547
-		return $errors;
548
-	}
549
-
550
-	/**
551
-	 * install an app already placed in the app folder
552
-	 * @param string $app id of the app to install
553
-	 * @return integer
554
-	 */
555
-	public static function installShippedApp($app) {
556
-		//install the database
557
-		$appPath = OC_App::getAppPath($app);
558
-		\OC_App::registerAutoloading($app, $appPath);
559
-
560
-		if(is_file("$appPath/appinfo/database.xml")) {
561
-			try {
562
-				OC_DB::createDbFromStructure("$appPath/appinfo/database.xml");
563
-			} catch (TableExistsException $e) {
564
-				throw new HintException(
565
-					'Failed to enable app ' . $app,
566
-					'Please ask for help via one of our <a href="https://nextcloud.com/support/" target="_blank" rel="noreferrer noopener">support channels</a>.',
567
-					0, $e
568
-				);
569
-			}
570
-		} else {
571
-			$ms = new \OC\DB\MigrationService($app, \OC::$server->getDatabaseConnection());
572
-			$ms->migrate();
573
-		}
574
-
575
-		//run appinfo/install.php
576
-		self::includeAppScript("$appPath/appinfo/install.php");
577
-
578
-		$info = OC_App::getAppInfo($app);
579
-		if (is_null($info)) {
580
-			return false;
581
-		}
582
-		\OC_App::setupBackgroundJobs($info['background-jobs']);
583
-
584
-		OC_App::executeRepairSteps($app, $info['repair-steps']['install']);
585
-
586
-		$config = \OC::$server->getConfig();
587
-
588
-		$config->setAppValue($app, 'installed_version', OC_App::getAppVersion($app));
589
-		if (array_key_exists('ocsid', $info)) {
590
-			$config->setAppValue($app, 'ocsid', $info['ocsid']);
591
-		}
592
-
593
-		//set remote/public handlers
594
-		foreach($info['remote'] as $name=>$path) {
595
-			$config->setAppValue('core', 'remote_'.$name, $app.'/'.$path);
596
-		}
597
-		foreach($info['public'] as $name=>$path) {
598
-			$config->setAppValue('core', 'public_'.$name, $app.'/'.$path);
599
-		}
600
-
601
-		OC_App::setAppTypes($info['id']);
602
-
603
-		return $info['id'];
604
-	}
605
-
606
-	/**
607
-	 * @param string $script
608
-	 */
609
-	private static function includeAppScript($script) {
610
-		if ( file_exists($script) ){
611
-			include $script;
612
-		}
613
-	}
55
+    /** @var AppFetcher */
56
+    private $appFetcher;
57
+    /** @var IClientService */
58
+    private $clientService;
59
+    /** @var ITempManager */
60
+    private $tempManager;
61
+    /** @var ILogger */
62
+    private $logger;
63
+    /** @var IConfig */
64
+    private $config;
65
+    /** @var array - for caching the result of app fetcher */
66
+    private $apps = null;
67
+    /** @var bool|null - for caching the result of the ready status */
68
+    private $isInstanceReadyForUpdates = null;
69
+
70
+    /**
71
+     * @param AppFetcher $appFetcher
72
+     * @param IClientService $clientService
73
+     * @param ITempManager $tempManager
74
+     * @param ILogger $logger
75
+     * @param IConfig $config
76
+     */
77
+    public function __construct(AppFetcher $appFetcher,
78
+                                IClientService $clientService,
79
+                                ITempManager $tempManager,
80
+                                ILogger $logger,
81
+                                IConfig $config) {
82
+        $this->appFetcher = $appFetcher;
83
+        $this->clientService = $clientService;
84
+        $this->tempManager = $tempManager;
85
+        $this->logger = $logger;
86
+        $this->config = $config;
87
+    }
88
+
89
+    /**
90
+     * Installs an app that is located in one of the app folders already
91
+     *
92
+     * @param string $appId App to install
93
+     * @throws \Exception
94
+     * @return string app ID
95
+     */
96
+    public function installApp($appId) {
97
+        $app = \OC_App::findAppInDirectories($appId);
98
+        if($app === false) {
99
+            throw new \Exception('App not found in any app directory');
100
+        }
101
+
102
+        $basedir = $app['path'].'/'.$appId;
103
+        $info = OC_App::getAppInfo($basedir.'/appinfo/info.xml', true);
104
+
105
+        $l = \OC::$server->getL10N('core');
106
+
107
+        if(!is_array($info)) {
108
+            throw new \Exception(
109
+                $l->t('App "%s" cannot be installed because appinfo file cannot be read.',
110
+                    [$info['name']]
111
+                )
112
+            );
113
+        }
114
+
115
+        $version = implode('.', \OCP\Util::getVersion());
116
+        if (!\OC_App::isAppCompatible($version, $info)) {
117
+            throw new \Exception(
118
+                // TODO $l
119
+                $l->t('App "%s" cannot be installed because it is not compatible with this version of the server.',
120
+                    [$info['name']]
121
+                )
122
+            );
123
+        }
124
+
125
+        // check for required dependencies
126
+        \OC_App::checkAppDependencies($this->config, $l, $info);
127
+        \OC_App::registerAutoloading($appId, $basedir);
128
+
129
+        //install the database
130
+        if(is_file($basedir.'/appinfo/database.xml')) {
131
+            if (\OC::$server->getConfig()->getAppValue($info['id'], 'installed_version') === null) {
132
+                OC_DB::createDbFromStructure($basedir.'/appinfo/database.xml');
133
+            } else {
134
+                OC_DB::updateDbFromStructure($basedir.'/appinfo/database.xml');
135
+            }
136
+        } else {
137
+            $ms = new \OC\DB\MigrationService($info['id'], \OC::$server->getDatabaseConnection());
138
+            $ms->migrate();
139
+        }
140
+
141
+        \OC_App::setupBackgroundJobs($info['background-jobs']);
142
+
143
+        //run appinfo/install.php
144
+        if(!isset($data['noinstall']) or $data['noinstall']==false) {
145
+            self::includeAppScript($basedir . '/appinfo/install.php');
146
+        }
147
+
148
+        $appData = OC_App::getAppInfo($appId);
149
+        OC_App::executeRepairSteps($appId, $appData['repair-steps']['install']);
150
+
151
+        //set the installed version
152
+        \OC::$server->getConfig()->setAppValue($info['id'], 'installed_version', OC_App::getAppVersion($info['id'], false));
153
+        \OC::$server->getConfig()->setAppValue($info['id'], 'enabled', 'no');
154
+
155
+        //set remote/public handlers
156
+        foreach($info['remote'] as $name=>$path) {
157
+            \OC::$server->getConfig()->setAppValue('core', 'remote_'.$name, $info['id'].'/'.$path);
158
+        }
159
+        foreach($info['public'] as $name=>$path) {
160
+            \OC::$server->getConfig()->setAppValue('core', 'public_'.$name, $info['id'].'/'.$path);
161
+        }
162
+
163
+        OC_App::setAppTypes($info['id']);
164
+
165
+        return $info['id'];
166
+    }
167
+
168
+    /**
169
+     * @brief checks whether or not an app is installed
170
+     * @param string $app app
171
+     * @returns bool
172
+     *
173
+     * Checks whether or not an app is installed, i.e. registered in apps table.
174
+     */
175
+    public static function isInstalled( $app ) {
176
+        return (\OC::$server->getConfig()->getAppValue($app, "installed_version", null) !== null);
177
+    }
178
+
179
+    /**
180
+     * Updates the specified app from the appstore
181
+     *
182
+     * @param string $appId
183
+     * @return bool
184
+     */
185
+    public function updateAppstoreApp($appId) {
186
+        if($this->isUpdateAvailable($appId)) {
187
+            try {
188
+                $this->downloadApp($appId);
189
+            } catch (\Exception $e) {
190
+                $this->logger->logException($e, [
191
+                    'level' => \OCP\Util::ERROR,
192
+                    'app' => 'core',
193
+                ]);
194
+                return false;
195
+            }
196
+            return OC_App::updateApp($appId);
197
+        }
198
+
199
+        return false;
200
+    }
201
+
202
+    /**
203
+     * Downloads an app and puts it into the app directory
204
+     *
205
+     * @param string $appId
206
+     *
207
+     * @throws \Exception If the installation was not successful
208
+     */
209
+    public function downloadApp($appId) {
210
+        $appId = strtolower($appId);
211
+
212
+        $apps = $this->appFetcher->get();
213
+        foreach($apps as $app) {
214
+            if($app['id'] === $appId) {
215
+                // Load the certificate
216
+                $certificate = new X509();
217
+                $certificate->loadCA(file_get_contents(__DIR__ . '/../../resources/codesigning/root.crt'));
218
+                $loadedCertificate = $certificate->loadX509($app['certificate']);
219
+
220
+                // Verify if the certificate has been revoked
221
+                $crl = new X509();
222
+                $crl->loadCA(file_get_contents(__DIR__ . '/../../resources/codesigning/root.crt'));
223
+                $crl->loadCRL(file_get_contents(__DIR__ . '/../../resources/codesigning/root.crl'));
224
+                if($crl->validateSignature() !== true) {
225
+                    throw new \Exception('Could not validate CRL signature');
226
+                }
227
+                $csn = $loadedCertificate['tbsCertificate']['serialNumber']->toString();
228
+                $revoked = $crl->getRevoked($csn);
229
+                if ($revoked !== false) {
230
+                    throw new \Exception(
231
+                        sprintf(
232
+                            'Certificate "%s" has been revoked',
233
+                            $csn
234
+                        )
235
+                    );
236
+                }
237
+
238
+                // Verify if the certificate has been issued by the Nextcloud Code Authority CA
239
+                if($certificate->validateSignature() !== true) {
240
+                    throw new \Exception(
241
+                        sprintf(
242
+                            'App with id %s has a certificate not issued by a trusted Code Signing Authority',
243
+                            $appId
244
+                        )
245
+                    );
246
+                }
247
+
248
+                // Verify if the certificate is issued for the requested app id
249
+                $certInfo = openssl_x509_parse($app['certificate']);
250
+                if(!isset($certInfo['subject']['CN'])) {
251
+                    throw new \Exception(
252
+                        sprintf(
253
+                            'App with id %s has a cert with no CN',
254
+                            $appId
255
+                        )
256
+                    );
257
+                }
258
+                if($certInfo['subject']['CN'] !== $appId) {
259
+                    throw new \Exception(
260
+                        sprintf(
261
+                            'App with id %s has a cert issued to %s',
262
+                            $appId,
263
+                            $certInfo['subject']['CN']
264
+                        )
265
+                    );
266
+                }
267
+
268
+                // Download the release
269
+                $tempFile = $this->tempManager->getTemporaryFile('.tar.gz');
270
+                $client = $this->clientService->newClient();
271
+                $client->get($app['releases'][0]['download'], ['save_to' => $tempFile]);
272
+
273
+                // Check if the signature actually matches the downloaded content
274
+                $certificate = openssl_get_publickey($app['certificate']);
275
+                $verified = (bool)openssl_verify(file_get_contents($tempFile), base64_decode($app['releases'][0]['signature']), $certificate, OPENSSL_ALGO_SHA512);
276
+                openssl_free_key($certificate);
277
+
278
+                if($verified === true) {
279
+                    // Seems to match, let's proceed
280
+                    $extractDir = $this->tempManager->getTemporaryFolder();
281
+                    $archive = new TAR($tempFile);
282
+
283
+                    if($archive) {
284
+                        if (!$archive->extract($extractDir)) {
285
+                            throw new \Exception(
286
+                                sprintf(
287
+                                    'Could not extract app %s',
288
+                                    $appId
289
+                                )
290
+                            );
291
+                        }
292
+                        $allFiles = scandir($extractDir);
293
+                        $folders = array_diff($allFiles, ['.', '..']);
294
+                        $folders = array_values($folders);
295
+
296
+                        if(count($folders) > 1) {
297
+                            throw new \Exception(
298
+                                sprintf(
299
+                                    'Extracted app %s has more than 1 folder',
300
+                                    $appId
301
+                                )
302
+                            );
303
+                        }
304
+
305
+                        // Check if appinfo/info.xml has the same app ID as well
306
+                        $loadEntities = libxml_disable_entity_loader(false);
307
+                        $xml = simplexml_load_file($extractDir . '/' . $folders[0] . '/appinfo/info.xml');
308
+                        libxml_disable_entity_loader($loadEntities);
309
+                        if((string)$xml->id !== $appId) {
310
+                            throw new \Exception(
311
+                                sprintf(
312
+                                    'App for id %s has a wrong app ID in info.xml: %s',
313
+                                    $appId,
314
+                                    (string)$xml->id
315
+                                )
316
+                            );
317
+                        }
318
+
319
+                        // Check if the version is lower than before
320
+                        $currentVersion = OC_App::getAppVersion($appId);
321
+                        $newVersion = (string)$xml->version;
322
+                        if(version_compare($currentVersion, $newVersion) === 1) {
323
+                            throw new \Exception(
324
+                                sprintf(
325
+                                    'App for id %s has version %s and tried to update to lower version %s',
326
+                                    $appId,
327
+                                    $currentVersion,
328
+                                    $newVersion
329
+                                )
330
+                            );
331
+                        }
332
+
333
+                        $baseDir = OC_App::getInstallPath() . '/' . $appId;
334
+                        // Remove old app with the ID if existent
335
+                        OC_Helper::rmdirr($baseDir);
336
+                        // Move to app folder
337
+                        if(@mkdir($baseDir)) {
338
+                            $extractDir .= '/' . $folders[0];
339
+                            OC_Helper::copyr($extractDir, $baseDir);
340
+                        }
341
+                        OC_Helper::copyr($extractDir, $baseDir);
342
+                        OC_Helper::rmdirr($extractDir);
343
+                        return;
344
+                    } else {
345
+                        throw new \Exception(
346
+                            sprintf(
347
+                                'Could not extract app with ID %s to %s',
348
+                                $appId,
349
+                                $extractDir
350
+                            )
351
+                        );
352
+                    }
353
+                } else {
354
+                    // Signature does not match
355
+                    throw new \Exception(
356
+                        sprintf(
357
+                            'App with id %s has invalid signature',
358
+                            $appId
359
+                        )
360
+                    );
361
+                }
362
+            }
363
+        }
364
+
365
+        throw new \Exception(
366
+            sprintf(
367
+                'Could not download app %s',
368
+                $appId
369
+            )
370
+        );
371
+    }
372
+
373
+    /**
374
+     * Check if an update for the app is available
375
+     *
376
+     * @param string $appId
377
+     * @return string|false false or the version number of the update
378
+     */
379
+    public function isUpdateAvailable($appId) {
380
+        if ($this->isInstanceReadyForUpdates === null) {
381
+            $installPath = OC_App::getInstallPath();
382
+            if ($installPath === false || $installPath === null) {
383
+                $this->isInstanceReadyForUpdates = false;
384
+            } else {
385
+                $this->isInstanceReadyForUpdates = true;
386
+            }
387
+        }
388
+
389
+        if ($this->isInstanceReadyForUpdates === false) {
390
+            return false;
391
+        }
392
+
393
+        if ($this->isInstalledFromGit($appId) === true) {
394
+            return false;
395
+        }
396
+
397
+        if ($this->apps === null) {
398
+            $this->apps = $this->appFetcher->get();
399
+        }
400
+
401
+        foreach($this->apps as $app) {
402
+            if($app['id'] === $appId) {
403
+                $currentVersion = OC_App::getAppVersion($appId);
404
+                $newestVersion = $app['releases'][0]['version'];
405
+                if (version_compare($newestVersion, $currentVersion, '>')) {
406
+                    return $newestVersion;
407
+                } else {
408
+                    return false;
409
+                }
410
+            }
411
+        }
412
+
413
+        return false;
414
+    }
415
+
416
+    /**
417
+     * Check if app has been installed from git
418
+     * @param string $name name of the application to remove
419
+     * @return boolean
420
+     *
421
+     * The function will check if the path contains a .git folder
422
+     */
423
+    private function isInstalledFromGit($appId) {
424
+        $app = \OC_App::findAppInDirectories($appId);
425
+        if($app === false) {
426
+            return false;
427
+        }
428
+        $basedir = $app['path'].'/'.$appId;
429
+        return file_exists($basedir.'/.git/');
430
+    }
431
+
432
+    /**
433
+     * Check if app is already downloaded
434
+     * @param string $name name of the application to remove
435
+     * @return boolean
436
+     *
437
+     * The function will check if the app is already downloaded in the apps repository
438
+     */
439
+    public function isDownloaded($name) {
440
+        foreach(\OC::$APPSROOTS as $dir) {
441
+            $dirToTest  = $dir['path'];
442
+            $dirToTest .= '/';
443
+            $dirToTest .= $name;
444
+            $dirToTest .= '/';
445
+
446
+            if (is_dir($dirToTest)) {
447
+                return true;
448
+            }
449
+        }
450
+
451
+        return false;
452
+    }
453
+
454
+    /**
455
+     * Removes an app
456
+     * @param string $appId ID of the application to remove
457
+     * @return boolean
458
+     *
459
+     *
460
+     * This function works as follows
461
+     *   -# call uninstall repair steps
462
+     *   -# removing the files
463
+     *
464
+     * The function will not delete preferences, tables and the configuration,
465
+     * this has to be done by the function oc_app_uninstall().
466
+     */
467
+    public function removeApp($appId) {
468
+        if($this->isDownloaded( $appId )) {
469
+            if (\OC::$server->getAppManager()->isShipped($appId)) {
470
+                return false;
471
+            }
472
+            $appDir = OC_App::getInstallPath() . '/' . $appId;
473
+            OC_Helper::rmdirr($appDir);
474
+            return true;
475
+        }else{
476
+            \OCP\Util::writeLog('core', 'can\'t remove app '.$appId.'. It is not installed.', \OCP\Util::ERROR);
477
+
478
+            return false;
479
+        }
480
+
481
+    }
482
+
483
+    /**
484
+     * Installs the app within the bundle and marks the bundle as installed
485
+     *
486
+     * @param Bundle $bundle
487
+     * @throws \Exception If app could not get installed
488
+     */
489
+    public function installAppBundle(Bundle $bundle) {
490
+        $appIds = $bundle->getAppIdentifiers();
491
+        foreach($appIds as $appId) {
492
+            if(!$this->isDownloaded($appId)) {
493
+                $this->downloadApp($appId);
494
+            }
495
+            $this->installApp($appId);
496
+            $app = new OC_App();
497
+            $app->enable($appId);
498
+        }
499
+        $bundles = json_decode($this->config->getAppValue('core', 'installed.bundles', json_encode([])), true);
500
+        $bundles[] = $bundle->getIdentifier();
501
+        $this->config->setAppValue('core', 'installed.bundles', json_encode($bundles));
502
+    }
503
+
504
+    /**
505
+     * Installs shipped apps
506
+     *
507
+     * This function installs all apps found in the 'apps' directory that should be enabled by default;
508
+     * @param bool $softErrors When updating we ignore errors and simply log them, better to have a
509
+     *                         working ownCloud at the end instead of an aborted update.
510
+     * @return array Array of error messages (appid => Exception)
511
+     */
512
+    public static function installShippedApps($softErrors = false) {
513
+        $errors = [];
514
+        foreach(\OC::$APPSROOTS as $app_dir) {
515
+            if($dir = opendir( $app_dir['path'] )) {
516
+                while( false !== ( $filename = readdir( $dir ))) {
517
+                    if( $filename[0] !== '.' and is_dir($app_dir['path']."/$filename") ) {
518
+                        if( file_exists( $app_dir['path']."/$filename/appinfo/info.xml" )) {
519
+                            if(!Installer::isInstalled($filename)) {
520
+                                $info=OC_App::getAppInfo($filename);
521
+                                $enabled = isset($info['default_enable']);
522
+                                if (($enabled || in_array($filename, \OC::$server->getAppManager()->getAlwaysEnabledApps()))
523
+                                      && \OC::$server->getConfig()->getAppValue($filename, 'enabled') !== 'no') {
524
+                                    if ($softErrors) {
525
+                                        try {
526
+                                            Installer::installShippedApp($filename);
527
+                                        } catch (HintException $e) {
528
+                                            if ($e->getPrevious() instanceof TableExistsException) {
529
+                                                $errors[$filename] = $e;
530
+                                                continue;
531
+                                            }
532
+                                            throw $e;
533
+                                        }
534
+                                    } else {
535
+                                        Installer::installShippedApp($filename);
536
+                                    }
537
+                                    \OC::$server->getConfig()->setAppValue($filename, 'enabled', 'yes');
538
+                                }
539
+                            }
540
+                        }
541
+                    }
542
+                }
543
+                closedir( $dir );
544
+            }
545
+        }
546
+
547
+        return $errors;
548
+    }
549
+
550
+    /**
551
+     * install an app already placed in the app folder
552
+     * @param string $app id of the app to install
553
+     * @return integer
554
+     */
555
+    public static function installShippedApp($app) {
556
+        //install the database
557
+        $appPath = OC_App::getAppPath($app);
558
+        \OC_App::registerAutoloading($app, $appPath);
559
+
560
+        if(is_file("$appPath/appinfo/database.xml")) {
561
+            try {
562
+                OC_DB::createDbFromStructure("$appPath/appinfo/database.xml");
563
+            } catch (TableExistsException $e) {
564
+                throw new HintException(
565
+                    'Failed to enable app ' . $app,
566
+                    'Please ask for help via one of our <a href="https://nextcloud.com/support/" target="_blank" rel="noreferrer noopener">support channels</a>.',
567
+                    0, $e
568
+                );
569
+            }
570
+        } else {
571
+            $ms = new \OC\DB\MigrationService($app, \OC::$server->getDatabaseConnection());
572
+            $ms->migrate();
573
+        }
574
+
575
+        //run appinfo/install.php
576
+        self::includeAppScript("$appPath/appinfo/install.php");
577
+
578
+        $info = OC_App::getAppInfo($app);
579
+        if (is_null($info)) {
580
+            return false;
581
+        }
582
+        \OC_App::setupBackgroundJobs($info['background-jobs']);
583
+
584
+        OC_App::executeRepairSteps($app, $info['repair-steps']['install']);
585
+
586
+        $config = \OC::$server->getConfig();
587
+
588
+        $config->setAppValue($app, 'installed_version', OC_App::getAppVersion($app));
589
+        if (array_key_exists('ocsid', $info)) {
590
+            $config->setAppValue($app, 'ocsid', $info['ocsid']);
591
+        }
592
+
593
+        //set remote/public handlers
594
+        foreach($info['remote'] as $name=>$path) {
595
+            $config->setAppValue('core', 'remote_'.$name, $app.'/'.$path);
596
+        }
597
+        foreach($info['public'] as $name=>$path) {
598
+            $config->setAppValue('core', 'public_'.$name, $app.'/'.$path);
599
+        }
600
+
601
+        OC_App::setAppTypes($info['id']);
602
+
603
+        return $info['id'];
604
+    }
605
+
606
+    /**
607
+     * @param string $script
608
+     */
609
+    private static function includeAppScript($script) {
610
+        if ( file_exists($script) ){
611
+            include $script;
612
+        }
613
+    }
614 614
 }
Please login to merge, or discard this patch.
lib/private/Updater.php 2 patches
Indentation   +550 added lines, -550 removed lines patch added patch discarded remove patch
@@ -54,556 +54,556 @@
 block discarded – undo
54 54
  */
55 55
 class Updater extends BasicEmitter {
56 56
 
57
-	/** @var ILogger $log */
58
-	private $log;
59
-
60
-	/** @var IConfig */
61
-	private $config;
62
-
63
-	/** @var Checker */
64
-	private $checker;
65
-
66
-	/** @var Installer */
67
-	private $installer;
68
-
69
-	private $logLevelNames = [
70
-		0 => 'Debug',
71
-		1 => 'Info',
72
-		2 => 'Warning',
73
-		3 => 'Error',
74
-		4 => 'Fatal',
75
-	];
76
-
77
-	/**
78
-	 * @param IConfig $config
79
-	 * @param Checker $checker
80
-	 * @param ILogger $log
81
-	 * @param Installer $installer
82
-	 */
83
-	public function __construct(IConfig $config,
84
-								Checker $checker,
85
-								ILogger $log = null,
86
-								Installer $installer) {
87
-		$this->log = $log;
88
-		$this->config = $config;
89
-		$this->checker = $checker;
90
-		$this->installer = $installer;
91
-	}
92
-
93
-	/**
94
-	 * runs the update actions in maintenance mode, does not upgrade the source files
95
-	 * except the main .htaccess file
96
-	 *
97
-	 * @return bool true if the operation succeeded, false otherwise
98
-	 */
99
-	public function upgrade() {
100
-		$this->emitRepairEvents();
101
-		$this->logAllEvents();
102
-
103
-		$logLevel = $this->config->getSystemValue('loglevel', Util::WARN);
104
-		$this->emit('\OC\Updater', 'setDebugLogLevel', [ $logLevel, $this->logLevelNames[$logLevel] ]);
105
-		$this->config->setSystemValue('loglevel', Util::DEBUG);
106
-
107
-		$wasMaintenanceModeEnabled = $this->config->getSystemValue('maintenance', false);
108
-
109
-		if(!$wasMaintenanceModeEnabled) {
110
-			$this->config->setSystemValue('maintenance', true);
111
-			$this->emit('\OC\Updater', 'maintenanceEnabled');
112
-		}
113
-
114
-		$installedVersion = $this->config->getSystemValue('version', '0.0.0');
115
-		$currentVersion = implode('.', \OCP\Util::getVersion());
116
-		$this->log->debug('starting upgrade from ' . $installedVersion . ' to ' . $currentVersion, array('app' => 'core'));
117
-
118
-		$success = true;
119
-		try {
120
-			$this->doUpgrade($currentVersion, $installedVersion);
121
-		} catch (HintException $exception) {
122
-			$this->log->logException($exception, ['app' => 'core']);
123
-			$this->emit('\OC\Updater', 'failure', array($exception->getMessage() . ': ' .$exception->getHint()));
124
-			$success = false;
125
-		} catch (\Exception $exception) {
126
-			$this->log->logException($exception, ['app' => 'core']);
127
-			$this->emit('\OC\Updater', 'failure', array(get_class($exception) . ': ' .$exception->getMessage()));
128
-			$success = false;
129
-		}
130
-
131
-		$this->emit('\OC\Updater', 'updateEnd', array($success));
132
-
133
-		if(!$wasMaintenanceModeEnabled && $success) {
134
-			$this->config->setSystemValue('maintenance', false);
135
-			$this->emit('\OC\Updater', 'maintenanceDisabled');
136
-		} else {
137
-			$this->emit('\OC\Updater', 'maintenanceActive');
138
-		}
139
-
140
-		$this->emit('\OC\Updater', 'resetLogLevel', [ $logLevel, $this->logLevelNames[$logLevel] ]);
141
-		$this->config->setSystemValue('loglevel', $logLevel);
142
-		$this->config->setSystemValue('installed', true);
143
-
144
-		return $success;
145
-	}
146
-
147
-	/**
148
-	 * Return version from which this version is allowed to upgrade from
149
-	 *
150
-	 * @return array allowed previous versions per vendor
151
-	 */
152
-	private function getAllowedPreviousVersions() {
153
-		// this should really be a JSON file
154
-		require \OC::$SERVERROOT . '/version.php';
155
-		/** @var array $OC_VersionCanBeUpgradedFrom */
156
-		return $OC_VersionCanBeUpgradedFrom;
157
-	}
158
-
159
-	/**
160
-	 * Return vendor from which this version was published
161
-	 *
162
-	 * @return string Get the vendor
163
-	 */
164
-	private function getVendor() {
165
-		// this should really be a JSON file
166
-		require \OC::$SERVERROOT . '/version.php';
167
-		/** @var string $vendor */
168
-		return (string) $vendor;
169
-	}
170
-
171
-	/**
172
-	 * Whether an upgrade to a specified version is possible
173
-	 * @param string $oldVersion
174
-	 * @param string $newVersion
175
-	 * @param array $allowedPreviousVersions
176
-	 * @return bool
177
-	 */
178
-	public function isUpgradePossible($oldVersion, $newVersion, array $allowedPreviousVersions) {
179
-		$version = explode('.', $oldVersion);
180
-		$majorMinor = $version[0] . '.' . $version[1];
181
-
182
-		$currentVendor = $this->config->getAppValue('core', 'vendor', '');
183
-
184
-		// Vendor was not set correctly on install, so we have to white-list known versions
185
-		if ($currentVendor === '' && isset($allowedPreviousVersions['owncloud'][$oldVersion])) {
186
-			$currentVendor = 'owncloud';
187
-		}
188
-
189
-		if ($currentVendor === 'nextcloud') {
190
-			return isset($allowedPreviousVersions[$currentVendor][$majorMinor])
191
-				&& (version_compare($oldVersion, $newVersion, '<=') ||
192
-					$this->config->getSystemValue('debug', false));
193
-		}
194
-
195
-		// Check if the instance can be migrated
196
-		return isset($allowedPreviousVersions[$currentVendor][$majorMinor]) ||
197
-			isset($allowedPreviousVersions[$currentVendor][$oldVersion]);
198
-	}
199
-
200
-	/**
201
-	 * runs the update actions in maintenance mode, does not upgrade the source files
202
-	 * except the main .htaccess file
203
-	 *
204
-	 * @param string $currentVersion current version to upgrade to
205
-	 * @param string $installedVersion previous version from which to upgrade from
206
-	 *
207
-	 * @throws \Exception
208
-	 */
209
-	private function doUpgrade($currentVersion, $installedVersion) {
210
-		// Stop update if the update is over several major versions
211
-		$allowedPreviousVersions = $this->getAllowedPreviousVersions();
212
-		if (!$this->isUpgradePossible($installedVersion, $currentVersion, $allowedPreviousVersions)) {
213
-			throw new \Exception('Updates between multiple major versions and downgrades are unsupported.');
214
-		}
215
-
216
-		// Update .htaccess files
217
-		try {
218
-			Setup::updateHtaccess();
219
-			Setup::protectDataDirectory();
220
-		} catch (\Exception $e) {
221
-			throw new \Exception($e->getMessage());
222
-		}
223
-
224
-		// create empty file in data dir, so we can later find
225
-		// out that this is indeed an ownCloud data directory
226
-		// (in case it didn't exist before)
227
-		file_put_contents($this->config->getSystemValue('datadirectory', \OC::$SERVERROOT . '/data') . '/.ocdata', '');
228
-
229
-		// pre-upgrade repairs
230
-		$repair = new Repair(Repair::getBeforeUpgradeRepairSteps(), \OC::$server->getEventDispatcher());
231
-		$repair->run();
232
-
233
-		$this->doCoreUpgrade();
234
-
235
-		try {
236
-			// TODO: replace with the new repair step mechanism https://github.com/owncloud/core/pull/24378
237
-			Setup::installBackgroundJobs();
238
-		} catch (\Exception $e) {
239
-			throw new \Exception($e->getMessage());
240
-		}
241
-
242
-		// update all shipped apps
243
-		$this->checkAppsRequirements();
244
-		$this->doAppUpgrade();
245
-
246
-		// Update the appfetchers version so it downloads the correct list from the appstore
247
-		\OC::$server->getAppFetcher()->setVersion($currentVersion);
248
-
249
-		// upgrade appstore apps
250
-		$this->upgradeAppStoreApps(\OC::$server->getAppManager()->getInstalledApps());
251
-
252
-		// install new shipped apps on upgrade
253
-		OC_App::loadApps(['authentication']);
254
-		$errors = Installer::installShippedApps(true);
255
-		foreach ($errors as $appId => $exception) {
256
-			/** @var \Exception $exception */
257
-			$this->log->logException($exception, ['app' => $appId]);
258
-			$this->emit('\OC\Updater', 'failure', [$appId . ': ' . $exception->getMessage()]);
259
-		}
260
-
261
-		// post-upgrade repairs
262
-		$repair = new Repair(Repair::getRepairSteps(), \OC::$server->getEventDispatcher());
263
-		$repair->run();
264
-
265
-		//Invalidate update feed
266
-		$this->config->setAppValue('core', 'lastupdatedat', 0);
267
-
268
-		// Check for code integrity if not disabled
269
-		if(\OC::$server->getIntegrityCodeChecker()->isCodeCheckEnforced()) {
270
-			$this->emit('\OC\Updater', 'startCheckCodeIntegrity');
271
-			$this->checker->runInstanceVerification();
272
-			$this->emit('\OC\Updater', 'finishedCheckCodeIntegrity');
273
-		}
274
-
275
-		// only set the final version if everything went well
276
-		$this->config->setSystemValue('version', implode('.', Util::getVersion()));
277
-		$this->config->setAppValue('core', 'vendor', $this->getVendor());
278
-	}
279
-
280
-	protected function doCoreUpgrade() {
281
-		$this->emit('\OC\Updater', 'dbUpgradeBefore');
282
-
283
-		// execute core migrations
284
-		$ms = new MigrationService('core', \OC::$server->getDatabaseConnection());
285
-		$ms->migrate();
286
-
287
-		$this->emit('\OC\Updater', 'dbUpgrade');
288
-	}
289
-
290
-	/**
291
-	 * @param string $version the oc version to check app compatibility with
292
-	 */
293
-	protected function checkAppUpgrade($version) {
294
-		$apps = \OC_App::getEnabledApps();
295
-		$this->emit('\OC\Updater', 'appUpgradeCheckBefore');
296
-
297
-		$appManager = \OC::$server->getAppManager();
298
-		foreach ($apps as $appId) {
299
-			$info = \OC_App::getAppInfo($appId);
300
-			$compatible = \OC_App::isAppCompatible($version, $info);
301
-			$isShipped = $appManager->isShipped($appId);
302
-
303
-			if ($compatible && $isShipped && \OC_App::shouldUpgrade($appId)) {
304
-				/**
305
-				 * FIXME: The preupdate check is performed before the database migration, otherwise database changes
306
-				 * are not possible anymore within it. - Consider this when touching the code.
307
-				 * @link https://github.com/owncloud/core/issues/10980
308
-				 * @see \OC_App::updateApp
309
-				 */
310
-				if (file_exists(\OC_App::getAppPath($appId) . '/appinfo/preupdate.php')) {
311
-					$this->includePreUpdate($appId);
312
-				}
313
-				if (file_exists(\OC_App::getAppPath($appId) . '/appinfo/database.xml')) {
314
-					$this->emit('\OC\Updater', 'appSimulateUpdate', array($appId));
315
-					\OC_DB::simulateUpdateDbFromStructure(\OC_App::getAppPath($appId) . '/appinfo/database.xml');
316
-				}
317
-			}
318
-		}
319
-
320
-		$this->emit('\OC\Updater', 'appUpgradeCheck');
321
-	}
322
-
323
-	/**
324
-	 * Includes the pre-update file. Done here to prevent namespace mixups.
325
-	 * @param string $appId
326
-	 */
327
-	private function includePreUpdate($appId) {
328
-		include \OC_App::getAppPath($appId) . '/appinfo/preupdate.php';
329
-	}
330
-
331
-	/**
332
-	 * upgrades all apps within a major ownCloud upgrade. Also loads "priority"
333
-	 * (types authentication, filesystem, logging, in that order) afterwards.
334
-	 *
335
-	 * @throws NeedsUpdateException
336
-	 */
337
-	protected function doAppUpgrade() {
338
-		$apps = \OC_App::getEnabledApps();
339
-		$priorityTypes = array('authentication', 'filesystem', 'logging');
340
-		$pseudoOtherType = 'other';
341
-		$stacks = array($pseudoOtherType => array());
342
-
343
-		foreach ($apps as $appId) {
344
-			$priorityType = false;
345
-			foreach ($priorityTypes as $type) {
346
-				if(!isset($stacks[$type])) {
347
-					$stacks[$type] = array();
348
-				}
349
-				if (\OC_App::isType($appId, [$type])) {
350
-					$stacks[$type][] = $appId;
351
-					$priorityType = true;
352
-					break;
353
-				}
354
-			}
355
-			if (!$priorityType) {
356
-				$stacks[$pseudoOtherType][] = $appId;
357
-			}
358
-		}
359
-		foreach ($stacks as $type => $stack) {
360
-			foreach ($stack as $appId) {
361
-				if (\OC_App::shouldUpgrade($appId)) {
362
-					$this->emit('\OC\Updater', 'appUpgradeStarted', [$appId, \OC_App::getAppVersion($appId)]);
363
-					\OC_App::updateApp($appId);
364
-					$this->emit('\OC\Updater', 'appUpgrade', [$appId, \OC_App::getAppVersion($appId)]);
365
-				}
366
-				if($type !== $pseudoOtherType) {
367
-					// load authentication, filesystem and logging apps after
368
-					// upgrading them. Other apps my need to rely on modifying
369
-					// user and/or filesystem aspects.
370
-					\OC_App::loadApp($appId);
371
-				}
372
-			}
373
-		}
374
-	}
375
-
376
-	/**
377
-	 * check if the current enabled apps are compatible with the current
378
-	 * ownCloud version. disable them if not.
379
-	 * This is important if you upgrade ownCloud and have non ported 3rd
380
-	 * party apps installed.
381
-	 *
382
-	 * @return array
383
-	 * @throws \Exception
384
-	 */
385
-	private function checkAppsRequirements() {
386
-		$isCoreUpgrade = $this->isCodeUpgrade();
387
-		$apps = OC_App::getEnabledApps();
388
-		$version = Util::getVersion();
389
-		$disabledApps = [];
390
-		$appManager = \OC::$server->getAppManager();
391
-		foreach ($apps as $app) {
392
-			// check if the app is compatible with this version of ownCloud
393
-			$info = OC_App::getAppInfo($app);
394
-			if(!OC_App::isAppCompatible($version, $info)) {
395
-				if ($appManager->isShipped($app)) {
396
-					throw new \UnexpectedValueException('The files of the app "' . $app . '" were not correctly replaced before running the update');
397
-				}
398
-				OC_App::disable($app);
399
-				$this->emit('\OC\Updater', 'incompatibleAppDisabled', array($app));
400
-			}
401
-			// no need to disable any app in case this is a non-core upgrade
402
-			if (!$isCoreUpgrade) {
403
-				continue;
404
-			}
405
-			// shipped apps will remain enabled
406
-			if ($appManager->isShipped($app)) {
407
-				continue;
408
-			}
409
-			// authentication and session apps will remain enabled as well
410
-			if (OC_App::isType($app, ['session', 'authentication'])) {
411
-				continue;
412
-			}
413
-		}
414
-		return $disabledApps;
415
-	}
416
-
417
-	/**
418
-	 * @return bool
419
-	 */
420
-	private function isCodeUpgrade() {
421
-		$installedVersion = $this->config->getSystemValue('version', '0.0.0');
422
-		$currentVersion = implode('.', Util::getVersion());
423
-		if (version_compare($currentVersion, $installedVersion, '>')) {
424
-			return true;
425
-		}
426
-		return false;
427
-	}
428
-
429
-	/**
430
-	 * @param array $disabledApps
431
-	 * @throws \Exception
432
-	 */
433
-	private function upgradeAppStoreApps(array $disabledApps) {
434
-		foreach($disabledApps as $app) {
435
-			try {
436
-				$this->emit('\OC\Updater', 'checkAppStoreAppBefore', [$app]);
437
-				if ($this->installer->isUpdateAvailable($app)) {
438
-					$this->emit('\OC\Updater', 'upgradeAppStoreApp', [$app]);
439
-					$this->installer->updateAppstoreApp($app);
440
-				}
441
-				$this->emit('\OC\Updater', 'checkAppStoreApp', [$app]);
442
-			} catch (\Exception $ex) {
443
-				$this->log->logException($ex, ['app' => 'core']);
444
-			}
445
-		}
446
-	}
447
-
448
-	/**
449
-	 * Forward messages emitted by the repair routine
450
-	 */
451
-	private function emitRepairEvents() {
452
-		$dispatcher = \OC::$server->getEventDispatcher();
453
-		$dispatcher->addListener('\OC\Repair::warning', function ($event) {
454
-			if ($event instanceof GenericEvent) {
455
-				$this->emit('\OC\Updater', 'repairWarning', $event->getArguments());
456
-			}
457
-		});
458
-		$dispatcher->addListener('\OC\Repair::error', function ($event) {
459
-			if ($event instanceof GenericEvent) {
460
-				$this->emit('\OC\Updater', 'repairError', $event->getArguments());
461
-			}
462
-		});
463
-		$dispatcher->addListener('\OC\Repair::info', function ($event) {
464
-			if ($event instanceof GenericEvent) {
465
-				$this->emit('\OC\Updater', 'repairInfo', $event->getArguments());
466
-			}
467
-		});
468
-		$dispatcher->addListener('\OC\Repair::step', function ($event) {
469
-			if ($event instanceof GenericEvent) {
470
-				$this->emit('\OC\Updater', 'repairStep', $event->getArguments());
471
-			}
472
-		});
473
-	}
474
-
475
-	private function logAllEvents() {
476
-		$log = $this->log;
477
-
478
-		$dispatcher = \OC::$server->getEventDispatcher();
479
-		$dispatcher->addListener('\OC\DB\Migrator::executeSql', function($event) use ($log) {
480
-			if (!$event instanceof GenericEvent) {
481
-				return;
482
-			}
483
-			$log->info('\OC\DB\Migrator::executeSql: ' . $event->getSubject() . ' (' . $event->getArgument(0) . ' of ' . $event->getArgument(1) . ')', ['app' => 'updater']);
484
-		});
485
-		$dispatcher->addListener('\OC\DB\Migrator::checkTable', function($event) use ($log) {
486
-			if (!$event instanceof GenericEvent) {
487
-				return;
488
-			}
489
-			$log->info('\OC\DB\Migrator::checkTable: ' . $event->getSubject() . ' (' . $event->getArgument(0) . ' of ' . $event->getArgument(1) . ')', ['app' => 'updater']);
490
-		});
491
-
492
-		$repairListener = function($event) use ($log) {
493
-			if (!$event instanceof GenericEvent) {
494
-				return;
495
-			}
496
-			switch ($event->getSubject()) {
497
-				case '\OC\Repair::startProgress':
498
-					$log->info('\OC\Repair::startProgress: Starting ... ' . $event->getArgument(1) .  ' (' . $event->getArgument(0) . ')', ['app' => 'updater']);
499
-					break;
500
-				case '\OC\Repair::advance':
501
-					$desc = $event->getArgument(1);
502
-					if (empty($desc)) {
503
-						$desc = '';
504
-					}
505
-					$log->info('\OC\Repair::advance: ' . $desc . ' (' . $event->getArgument(0) . ')', ['app' => 'updater']);
506
-
507
-					break;
508
-				case '\OC\Repair::finishProgress':
509
-					$log->info('\OC\Repair::finishProgress', ['app' => 'updater']);
510
-					break;
511
-				case '\OC\Repair::step':
512
-					$log->info('\OC\Repair::step: Repair step: ' . $event->getArgument(0), ['app' => 'updater']);
513
-					break;
514
-				case '\OC\Repair::info':
515
-					$log->info('\OC\Repair::info: Repair info: ' . $event->getArgument(0), ['app' => 'updater']);
516
-					break;
517
-				case '\OC\Repair::warning':
518
-					$log->warning('\OC\Repair::warning: Repair warning: ' . $event->getArgument(0), ['app' => 'updater']);
519
-					break;
520
-				case '\OC\Repair::error':
521
-					$log->error('\OC\Repair::error: Repair error: ' . $event->getArgument(0), ['app' => 'updater']);
522
-					break;
523
-			}
524
-		};
525
-
526
-		$dispatcher->addListener('\OC\Repair::startProgress', $repairListener);
527
-		$dispatcher->addListener('\OC\Repair::advance', $repairListener);
528
-		$dispatcher->addListener('\OC\Repair::finishProgress', $repairListener);
529
-		$dispatcher->addListener('\OC\Repair::step', $repairListener);
530
-		$dispatcher->addListener('\OC\Repair::info', $repairListener);
531
-		$dispatcher->addListener('\OC\Repair::warning', $repairListener);
532
-		$dispatcher->addListener('\OC\Repair::error', $repairListener);
533
-
534
-
535
-		$this->listen('\OC\Updater', 'maintenanceEnabled', function () use($log) {
536
-			$log->info('\OC\Updater::maintenanceEnabled: Turned on maintenance mode', ['app' => 'updater']);
537
-		});
538
-		$this->listen('\OC\Updater', 'maintenanceDisabled', function () use($log) {
539
-			$log->info('\OC\Updater::maintenanceDisabled: Turned off maintenance mode', ['app' => 'updater']);
540
-		});
541
-		$this->listen('\OC\Updater', 'maintenanceActive', function () use($log) {
542
-			$log->info('\OC\Updater::maintenanceActive: Maintenance mode is kept active', ['app' => 'updater']);
543
-		});
544
-		$this->listen('\OC\Updater', 'updateEnd', function ($success) use($log) {
545
-			if ($success) {
546
-				$log->info('\OC\Updater::updateEnd: Update successful', ['app' => 'updater']);
547
-			} else {
548
-				$log->error('\OC\Updater::updateEnd: Update failed', ['app' => 'updater']);
549
-			}
550
-		});
551
-		$this->listen('\OC\Updater', 'dbUpgradeBefore', function () use($log) {
552
-			$log->info('\OC\Updater::dbUpgradeBefore: Updating database schema', ['app' => 'updater']);
553
-		});
554
-		$this->listen('\OC\Updater', 'dbUpgrade', function () use($log) {
555
-			$log->info('\OC\Updater::dbUpgrade: Updated database', ['app' => 'updater']);
556
-		});
557
-		$this->listen('\OC\Updater', 'dbSimulateUpgradeBefore', function () use($log) {
558
-			$log->info('\OC\Updater::dbSimulateUpgradeBefore: Checking whether the database schema can be updated (this can take a long time depending on the database size)', ['app' => 'updater']);
559
-		});
560
-		$this->listen('\OC\Updater', 'dbSimulateUpgrade', function () use($log) {
561
-			$log->info('\OC\Updater::dbSimulateUpgrade: Checked database schema update', ['app' => 'updater']);
562
-		});
563
-		$this->listen('\OC\Updater', 'incompatibleAppDisabled', function ($app) use($log) {
564
-			$log->info('\OC\Updater::incompatibleAppDisabled: Disabled incompatible app: ' . $app, ['app' => 'updater']);
565
-		});
566
-		$this->listen('\OC\Updater', 'checkAppStoreAppBefore', function ($app) use($log) {
567
-			$log->info('\OC\Updater::checkAppStoreAppBefore: Checking for update of app "' . $app . '" in appstore', ['app' => 'updater']);
568
-		});
569
-		$this->listen('\OC\Updater', 'upgradeAppStoreApp', function ($app) use($log) {
570
-			$log->info('\OC\Updater::upgradeAppStoreApp: Update app "' . $app . '" from appstore', ['app' => 'updater']);
571
-		});
572
-		$this->listen('\OC\Updater', 'checkAppStoreApp', function ($app) use($log) {
573
-			$log->info('\OC\Updater::checkAppStoreApp: Checked for update of app "' . $app . '" in appstore', ['app' => 'updater']);
574
-		});
575
-		$this->listen('\OC\Updater', 'appUpgradeCheckBefore', function () use ($log) {
576
-			$log->info('\OC\Updater::appUpgradeCheckBefore: Checking updates of apps', ['app' => 'updater']);
577
-		});
578
-		$this->listen('\OC\Updater', 'appSimulateUpdate', function ($app) use ($log) {
579
-			$log->info('\OC\Updater::appSimulateUpdate: Checking whether the database schema for <' . $app . '> can be updated (this can take a long time depending on the database size)', ['app' => 'updater']);
580
-		});
581
-		$this->listen('\OC\Updater', 'appUpgradeCheck', function () use ($log) {
582
-			$log->info('\OC\Updater::appUpgradeCheck: Checked database schema update for apps', ['app' => 'updater']);
583
-		});
584
-		$this->listen('\OC\Updater', 'appUpgradeStarted', function ($app) use ($log) {
585
-			$log->info('\OC\Updater::appUpgradeStarted: Updating <' . $app . '> ...', ['app' => 'updater']);
586
-		});
587
-		$this->listen('\OC\Updater', 'appUpgrade', function ($app, $version) use ($log) {
588
-			$log->info('\OC\Updater::appUpgrade: Updated <' . $app . '> to ' . $version, ['app' => 'updater']);
589
-		});
590
-		$this->listen('\OC\Updater', 'failure', function ($message) use($log) {
591
-			$log->error('\OC\Updater::failure: ' . $message, ['app' => 'updater']);
592
-		});
593
-		$this->listen('\OC\Updater', 'setDebugLogLevel', function () use($log) {
594
-			$log->info('\OC\Updater::setDebugLogLevel: Set log level to debug', ['app' => 'updater']);
595
-		});
596
-		$this->listen('\OC\Updater', 'resetLogLevel', function ($logLevel, $logLevelName) use($log) {
597
-			$log->info('\OC\Updater::resetLogLevel: Reset log level to ' . $logLevelName . '(' . $logLevel . ')', ['app' => 'updater']);
598
-		});
599
-		$this->listen('\OC\Updater', 'startCheckCodeIntegrity', function () use($log) {
600
-			$log->info('\OC\Updater::startCheckCodeIntegrity: Starting code integrity check...', ['app' => 'updater']);
601
-		});
602
-		$this->listen('\OC\Updater', 'finishedCheckCodeIntegrity', function () use($log) {
603
-			$log->info('\OC\Updater::finishedCheckCodeIntegrity: Finished code integrity check', ['app' => 'updater']);
604
-		});
605
-
606
-	}
57
+    /** @var ILogger $log */
58
+    private $log;
59
+
60
+    /** @var IConfig */
61
+    private $config;
62
+
63
+    /** @var Checker */
64
+    private $checker;
65
+
66
+    /** @var Installer */
67
+    private $installer;
68
+
69
+    private $logLevelNames = [
70
+        0 => 'Debug',
71
+        1 => 'Info',
72
+        2 => 'Warning',
73
+        3 => 'Error',
74
+        4 => 'Fatal',
75
+    ];
76
+
77
+    /**
78
+     * @param IConfig $config
79
+     * @param Checker $checker
80
+     * @param ILogger $log
81
+     * @param Installer $installer
82
+     */
83
+    public function __construct(IConfig $config,
84
+                                Checker $checker,
85
+                                ILogger $log = null,
86
+                                Installer $installer) {
87
+        $this->log = $log;
88
+        $this->config = $config;
89
+        $this->checker = $checker;
90
+        $this->installer = $installer;
91
+    }
92
+
93
+    /**
94
+     * runs the update actions in maintenance mode, does not upgrade the source files
95
+     * except the main .htaccess file
96
+     *
97
+     * @return bool true if the operation succeeded, false otherwise
98
+     */
99
+    public function upgrade() {
100
+        $this->emitRepairEvents();
101
+        $this->logAllEvents();
102
+
103
+        $logLevel = $this->config->getSystemValue('loglevel', Util::WARN);
104
+        $this->emit('\OC\Updater', 'setDebugLogLevel', [ $logLevel, $this->logLevelNames[$logLevel] ]);
105
+        $this->config->setSystemValue('loglevel', Util::DEBUG);
106
+
107
+        $wasMaintenanceModeEnabled = $this->config->getSystemValue('maintenance', false);
108
+
109
+        if(!$wasMaintenanceModeEnabled) {
110
+            $this->config->setSystemValue('maintenance', true);
111
+            $this->emit('\OC\Updater', 'maintenanceEnabled');
112
+        }
113
+
114
+        $installedVersion = $this->config->getSystemValue('version', '0.0.0');
115
+        $currentVersion = implode('.', \OCP\Util::getVersion());
116
+        $this->log->debug('starting upgrade from ' . $installedVersion . ' to ' . $currentVersion, array('app' => 'core'));
117
+
118
+        $success = true;
119
+        try {
120
+            $this->doUpgrade($currentVersion, $installedVersion);
121
+        } catch (HintException $exception) {
122
+            $this->log->logException($exception, ['app' => 'core']);
123
+            $this->emit('\OC\Updater', 'failure', array($exception->getMessage() . ': ' .$exception->getHint()));
124
+            $success = false;
125
+        } catch (\Exception $exception) {
126
+            $this->log->logException($exception, ['app' => 'core']);
127
+            $this->emit('\OC\Updater', 'failure', array(get_class($exception) . ': ' .$exception->getMessage()));
128
+            $success = false;
129
+        }
130
+
131
+        $this->emit('\OC\Updater', 'updateEnd', array($success));
132
+
133
+        if(!$wasMaintenanceModeEnabled && $success) {
134
+            $this->config->setSystemValue('maintenance', false);
135
+            $this->emit('\OC\Updater', 'maintenanceDisabled');
136
+        } else {
137
+            $this->emit('\OC\Updater', 'maintenanceActive');
138
+        }
139
+
140
+        $this->emit('\OC\Updater', 'resetLogLevel', [ $logLevel, $this->logLevelNames[$logLevel] ]);
141
+        $this->config->setSystemValue('loglevel', $logLevel);
142
+        $this->config->setSystemValue('installed', true);
143
+
144
+        return $success;
145
+    }
146
+
147
+    /**
148
+     * Return version from which this version is allowed to upgrade from
149
+     *
150
+     * @return array allowed previous versions per vendor
151
+     */
152
+    private function getAllowedPreviousVersions() {
153
+        // this should really be a JSON file
154
+        require \OC::$SERVERROOT . '/version.php';
155
+        /** @var array $OC_VersionCanBeUpgradedFrom */
156
+        return $OC_VersionCanBeUpgradedFrom;
157
+    }
158
+
159
+    /**
160
+     * Return vendor from which this version was published
161
+     *
162
+     * @return string Get the vendor
163
+     */
164
+    private function getVendor() {
165
+        // this should really be a JSON file
166
+        require \OC::$SERVERROOT . '/version.php';
167
+        /** @var string $vendor */
168
+        return (string) $vendor;
169
+    }
170
+
171
+    /**
172
+     * Whether an upgrade to a specified version is possible
173
+     * @param string $oldVersion
174
+     * @param string $newVersion
175
+     * @param array $allowedPreviousVersions
176
+     * @return bool
177
+     */
178
+    public function isUpgradePossible($oldVersion, $newVersion, array $allowedPreviousVersions) {
179
+        $version = explode('.', $oldVersion);
180
+        $majorMinor = $version[0] . '.' . $version[1];
181
+
182
+        $currentVendor = $this->config->getAppValue('core', 'vendor', '');
183
+
184
+        // Vendor was not set correctly on install, so we have to white-list known versions
185
+        if ($currentVendor === '' && isset($allowedPreviousVersions['owncloud'][$oldVersion])) {
186
+            $currentVendor = 'owncloud';
187
+        }
188
+
189
+        if ($currentVendor === 'nextcloud') {
190
+            return isset($allowedPreviousVersions[$currentVendor][$majorMinor])
191
+                && (version_compare($oldVersion, $newVersion, '<=') ||
192
+                    $this->config->getSystemValue('debug', false));
193
+        }
194
+
195
+        // Check if the instance can be migrated
196
+        return isset($allowedPreviousVersions[$currentVendor][$majorMinor]) ||
197
+            isset($allowedPreviousVersions[$currentVendor][$oldVersion]);
198
+    }
199
+
200
+    /**
201
+     * runs the update actions in maintenance mode, does not upgrade the source files
202
+     * except the main .htaccess file
203
+     *
204
+     * @param string $currentVersion current version to upgrade to
205
+     * @param string $installedVersion previous version from which to upgrade from
206
+     *
207
+     * @throws \Exception
208
+     */
209
+    private function doUpgrade($currentVersion, $installedVersion) {
210
+        // Stop update if the update is over several major versions
211
+        $allowedPreviousVersions = $this->getAllowedPreviousVersions();
212
+        if (!$this->isUpgradePossible($installedVersion, $currentVersion, $allowedPreviousVersions)) {
213
+            throw new \Exception('Updates between multiple major versions and downgrades are unsupported.');
214
+        }
215
+
216
+        // Update .htaccess files
217
+        try {
218
+            Setup::updateHtaccess();
219
+            Setup::protectDataDirectory();
220
+        } catch (\Exception $e) {
221
+            throw new \Exception($e->getMessage());
222
+        }
223
+
224
+        // create empty file in data dir, so we can later find
225
+        // out that this is indeed an ownCloud data directory
226
+        // (in case it didn't exist before)
227
+        file_put_contents($this->config->getSystemValue('datadirectory', \OC::$SERVERROOT . '/data') . '/.ocdata', '');
228
+
229
+        // pre-upgrade repairs
230
+        $repair = new Repair(Repair::getBeforeUpgradeRepairSteps(), \OC::$server->getEventDispatcher());
231
+        $repair->run();
232
+
233
+        $this->doCoreUpgrade();
234
+
235
+        try {
236
+            // TODO: replace with the new repair step mechanism https://github.com/owncloud/core/pull/24378
237
+            Setup::installBackgroundJobs();
238
+        } catch (\Exception $e) {
239
+            throw new \Exception($e->getMessage());
240
+        }
241
+
242
+        // update all shipped apps
243
+        $this->checkAppsRequirements();
244
+        $this->doAppUpgrade();
245
+
246
+        // Update the appfetchers version so it downloads the correct list from the appstore
247
+        \OC::$server->getAppFetcher()->setVersion($currentVersion);
248
+
249
+        // upgrade appstore apps
250
+        $this->upgradeAppStoreApps(\OC::$server->getAppManager()->getInstalledApps());
251
+
252
+        // install new shipped apps on upgrade
253
+        OC_App::loadApps(['authentication']);
254
+        $errors = Installer::installShippedApps(true);
255
+        foreach ($errors as $appId => $exception) {
256
+            /** @var \Exception $exception */
257
+            $this->log->logException($exception, ['app' => $appId]);
258
+            $this->emit('\OC\Updater', 'failure', [$appId . ': ' . $exception->getMessage()]);
259
+        }
260
+
261
+        // post-upgrade repairs
262
+        $repair = new Repair(Repair::getRepairSteps(), \OC::$server->getEventDispatcher());
263
+        $repair->run();
264
+
265
+        //Invalidate update feed
266
+        $this->config->setAppValue('core', 'lastupdatedat', 0);
267
+
268
+        // Check for code integrity if not disabled
269
+        if(\OC::$server->getIntegrityCodeChecker()->isCodeCheckEnforced()) {
270
+            $this->emit('\OC\Updater', 'startCheckCodeIntegrity');
271
+            $this->checker->runInstanceVerification();
272
+            $this->emit('\OC\Updater', 'finishedCheckCodeIntegrity');
273
+        }
274
+
275
+        // only set the final version if everything went well
276
+        $this->config->setSystemValue('version', implode('.', Util::getVersion()));
277
+        $this->config->setAppValue('core', 'vendor', $this->getVendor());
278
+    }
279
+
280
+    protected function doCoreUpgrade() {
281
+        $this->emit('\OC\Updater', 'dbUpgradeBefore');
282
+
283
+        // execute core migrations
284
+        $ms = new MigrationService('core', \OC::$server->getDatabaseConnection());
285
+        $ms->migrate();
286
+
287
+        $this->emit('\OC\Updater', 'dbUpgrade');
288
+    }
289
+
290
+    /**
291
+     * @param string $version the oc version to check app compatibility with
292
+     */
293
+    protected function checkAppUpgrade($version) {
294
+        $apps = \OC_App::getEnabledApps();
295
+        $this->emit('\OC\Updater', 'appUpgradeCheckBefore');
296
+
297
+        $appManager = \OC::$server->getAppManager();
298
+        foreach ($apps as $appId) {
299
+            $info = \OC_App::getAppInfo($appId);
300
+            $compatible = \OC_App::isAppCompatible($version, $info);
301
+            $isShipped = $appManager->isShipped($appId);
302
+
303
+            if ($compatible && $isShipped && \OC_App::shouldUpgrade($appId)) {
304
+                /**
305
+                 * FIXME: The preupdate check is performed before the database migration, otherwise database changes
306
+                 * are not possible anymore within it. - Consider this when touching the code.
307
+                 * @link https://github.com/owncloud/core/issues/10980
308
+                 * @see \OC_App::updateApp
309
+                 */
310
+                if (file_exists(\OC_App::getAppPath($appId) . '/appinfo/preupdate.php')) {
311
+                    $this->includePreUpdate($appId);
312
+                }
313
+                if (file_exists(\OC_App::getAppPath($appId) . '/appinfo/database.xml')) {
314
+                    $this->emit('\OC\Updater', 'appSimulateUpdate', array($appId));
315
+                    \OC_DB::simulateUpdateDbFromStructure(\OC_App::getAppPath($appId) . '/appinfo/database.xml');
316
+                }
317
+            }
318
+        }
319
+
320
+        $this->emit('\OC\Updater', 'appUpgradeCheck');
321
+    }
322
+
323
+    /**
324
+     * Includes the pre-update file. Done here to prevent namespace mixups.
325
+     * @param string $appId
326
+     */
327
+    private function includePreUpdate($appId) {
328
+        include \OC_App::getAppPath($appId) . '/appinfo/preupdate.php';
329
+    }
330
+
331
+    /**
332
+     * upgrades all apps within a major ownCloud upgrade. Also loads "priority"
333
+     * (types authentication, filesystem, logging, in that order) afterwards.
334
+     *
335
+     * @throws NeedsUpdateException
336
+     */
337
+    protected function doAppUpgrade() {
338
+        $apps = \OC_App::getEnabledApps();
339
+        $priorityTypes = array('authentication', 'filesystem', 'logging');
340
+        $pseudoOtherType = 'other';
341
+        $stacks = array($pseudoOtherType => array());
342
+
343
+        foreach ($apps as $appId) {
344
+            $priorityType = false;
345
+            foreach ($priorityTypes as $type) {
346
+                if(!isset($stacks[$type])) {
347
+                    $stacks[$type] = array();
348
+                }
349
+                if (\OC_App::isType($appId, [$type])) {
350
+                    $stacks[$type][] = $appId;
351
+                    $priorityType = true;
352
+                    break;
353
+                }
354
+            }
355
+            if (!$priorityType) {
356
+                $stacks[$pseudoOtherType][] = $appId;
357
+            }
358
+        }
359
+        foreach ($stacks as $type => $stack) {
360
+            foreach ($stack as $appId) {
361
+                if (\OC_App::shouldUpgrade($appId)) {
362
+                    $this->emit('\OC\Updater', 'appUpgradeStarted', [$appId, \OC_App::getAppVersion($appId)]);
363
+                    \OC_App::updateApp($appId);
364
+                    $this->emit('\OC\Updater', 'appUpgrade', [$appId, \OC_App::getAppVersion($appId)]);
365
+                }
366
+                if($type !== $pseudoOtherType) {
367
+                    // load authentication, filesystem and logging apps after
368
+                    // upgrading them. Other apps my need to rely on modifying
369
+                    // user and/or filesystem aspects.
370
+                    \OC_App::loadApp($appId);
371
+                }
372
+            }
373
+        }
374
+    }
375
+
376
+    /**
377
+     * check if the current enabled apps are compatible with the current
378
+     * ownCloud version. disable them if not.
379
+     * This is important if you upgrade ownCloud and have non ported 3rd
380
+     * party apps installed.
381
+     *
382
+     * @return array
383
+     * @throws \Exception
384
+     */
385
+    private function checkAppsRequirements() {
386
+        $isCoreUpgrade = $this->isCodeUpgrade();
387
+        $apps = OC_App::getEnabledApps();
388
+        $version = Util::getVersion();
389
+        $disabledApps = [];
390
+        $appManager = \OC::$server->getAppManager();
391
+        foreach ($apps as $app) {
392
+            // check if the app is compatible with this version of ownCloud
393
+            $info = OC_App::getAppInfo($app);
394
+            if(!OC_App::isAppCompatible($version, $info)) {
395
+                if ($appManager->isShipped($app)) {
396
+                    throw new \UnexpectedValueException('The files of the app "' . $app . '" were not correctly replaced before running the update');
397
+                }
398
+                OC_App::disable($app);
399
+                $this->emit('\OC\Updater', 'incompatibleAppDisabled', array($app));
400
+            }
401
+            // no need to disable any app in case this is a non-core upgrade
402
+            if (!$isCoreUpgrade) {
403
+                continue;
404
+            }
405
+            // shipped apps will remain enabled
406
+            if ($appManager->isShipped($app)) {
407
+                continue;
408
+            }
409
+            // authentication and session apps will remain enabled as well
410
+            if (OC_App::isType($app, ['session', 'authentication'])) {
411
+                continue;
412
+            }
413
+        }
414
+        return $disabledApps;
415
+    }
416
+
417
+    /**
418
+     * @return bool
419
+     */
420
+    private function isCodeUpgrade() {
421
+        $installedVersion = $this->config->getSystemValue('version', '0.0.0');
422
+        $currentVersion = implode('.', Util::getVersion());
423
+        if (version_compare($currentVersion, $installedVersion, '>')) {
424
+            return true;
425
+        }
426
+        return false;
427
+    }
428
+
429
+    /**
430
+     * @param array $disabledApps
431
+     * @throws \Exception
432
+     */
433
+    private function upgradeAppStoreApps(array $disabledApps) {
434
+        foreach($disabledApps as $app) {
435
+            try {
436
+                $this->emit('\OC\Updater', 'checkAppStoreAppBefore', [$app]);
437
+                if ($this->installer->isUpdateAvailable($app)) {
438
+                    $this->emit('\OC\Updater', 'upgradeAppStoreApp', [$app]);
439
+                    $this->installer->updateAppstoreApp($app);
440
+                }
441
+                $this->emit('\OC\Updater', 'checkAppStoreApp', [$app]);
442
+            } catch (\Exception $ex) {
443
+                $this->log->logException($ex, ['app' => 'core']);
444
+            }
445
+        }
446
+    }
447
+
448
+    /**
449
+     * Forward messages emitted by the repair routine
450
+     */
451
+    private function emitRepairEvents() {
452
+        $dispatcher = \OC::$server->getEventDispatcher();
453
+        $dispatcher->addListener('\OC\Repair::warning', function ($event) {
454
+            if ($event instanceof GenericEvent) {
455
+                $this->emit('\OC\Updater', 'repairWarning', $event->getArguments());
456
+            }
457
+        });
458
+        $dispatcher->addListener('\OC\Repair::error', function ($event) {
459
+            if ($event instanceof GenericEvent) {
460
+                $this->emit('\OC\Updater', 'repairError', $event->getArguments());
461
+            }
462
+        });
463
+        $dispatcher->addListener('\OC\Repair::info', function ($event) {
464
+            if ($event instanceof GenericEvent) {
465
+                $this->emit('\OC\Updater', 'repairInfo', $event->getArguments());
466
+            }
467
+        });
468
+        $dispatcher->addListener('\OC\Repair::step', function ($event) {
469
+            if ($event instanceof GenericEvent) {
470
+                $this->emit('\OC\Updater', 'repairStep', $event->getArguments());
471
+            }
472
+        });
473
+    }
474
+
475
+    private function logAllEvents() {
476
+        $log = $this->log;
477
+
478
+        $dispatcher = \OC::$server->getEventDispatcher();
479
+        $dispatcher->addListener('\OC\DB\Migrator::executeSql', function($event) use ($log) {
480
+            if (!$event instanceof GenericEvent) {
481
+                return;
482
+            }
483
+            $log->info('\OC\DB\Migrator::executeSql: ' . $event->getSubject() . ' (' . $event->getArgument(0) . ' of ' . $event->getArgument(1) . ')', ['app' => 'updater']);
484
+        });
485
+        $dispatcher->addListener('\OC\DB\Migrator::checkTable', function($event) use ($log) {
486
+            if (!$event instanceof GenericEvent) {
487
+                return;
488
+            }
489
+            $log->info('\OC\DB\Migrator::checkTable: ' . $event->getSubject() . ' (' . $event->getArgument(0) . ' of ' . $event->getArgument(1) . ')', ['app' => 'updater']);
490
+        });
491
+
492
+        $repairListener = function($event) use ($log) {
493
+            if (!$event instanceof GenericEvent) {
494
+                return;
495
+            }
496
+            switch ($event->getSubject()) {
497
+                case '\OC\Repair::startProgress':
498
+                    $log->info('\OC\Repair::startProgress: Starting ... ' . $event->getArgument(1) .  ' (' . $event->getArgument(0) . ')', ['app' => 'updater']);
499
+                    break;
500
+                case '\OC\Repair::advance':
501
+                    $desc = $event->getArgument(1);
502
+                    if (empty($desc)) {
503
+                        $desc = '';
504
+                    }
505
+                    $log->info('\OC\Repair::advance: ' . $desc . ' (' . $event->getArgument(0) . ')', ['app' => 'updater']);
506
+
507
+                    break;
508
+                case '\OC\Repair::finishProgress':
509
+                    $log->info('\OC\Repair::finishProgress', ['app' => 'updater']);
510
+                    break;
511
+                case '\OC\Repair::step':
512
+                    $log->info('\OC\Repair::step: Repair step: ' . $event->getArgument(0), ['app' => 'updater']);
513
+                    break;
514
+                case '\OC\Repair::info':
515
+                    $log->info('\OC\Repair::info: Repair info: ' . $event->getArgument(0), ['app' => 'updater']);
516
+                    break;
517
+                case '\OC\Repair::warning':
518
+                    $log->warning('\OC\Repair::warning: Repair warning: ' . $event->getArgument(0), ['app' => 'updater']);
519
+                    break;
520
+                case '\OC\Repair::error':
521
+                    $log->error('\OC\Repair::error: Repair error: ' . $event->getArgument(0), ['app' => 'updater']);
522
+                    break;
523
+            }
524
+        };
525
+
526
+        $dispatcher->addListener('\OC\Repair::startProgress', $repairListener);
527
+        $dispatcher->addListener('\OC\Repair::advance', $repairListener);
528
+        $dispatcher->addListener('\OC\Repair::finishProgress', $repairListener);
529
+        $dispatcher->addListener('\OC\Repair::step', $repairListener);
530
+        $dispatcher->addListener('\OC\Repair::info', $repairListener);
531
+        $dispatcher->addListener('\OC\Repair::warning', $repairListener);
532
+        $dispatcher->addListener('\OC\Repair::error', $repairListener);
533
+
534
+
535
+        $this->listen('\OC\Updater', 'maintenanceEnabled', function () use($log) {
536
+            $log->info('\OC\Updater::maintenanceEnabled: Turned on maintenance mode', ['app' => 'updater']);
537
+        });
538
+        $this->listen('\OC\Updater', 'maintenanceDisabled', function () use($log) {
539
+            $log->info('\OC\Updater::maintenanceDisabled: Turned off maintenance mode', ['app' => 'updater']);
540
+        });
541
+        $this->listen('\OC\Updater', 'maintenanceActive', function () use($log) {
542
+            $log->info('\OC\Updater::maintenanceActive: Maintenance mode is kept active', ['app' => 'updater']);
543
+        });
544
+        $this->listen('\OC\Updater', 'updateEnd', function ($success) use($log) {
545
+            if ($success) {
546
+                $log->info('\OC\Updater::updateEnd: Update successful', ['app' => 'updater']);
547
+            } else {
548
+                $log->error('\OC\Updater::updateEnd: Update failed', ['app' => 'updater']);
549
+            }
550
+        });
551
+        $this->listen('\OC\Updater', 'dbUpgradeBefore', function () use($log) {
552
+            $log->info('\OC\Updater::dbUpgradeBefore: Updating database schema', ['app' => 'updater']);
553
+        });
554
+        $this->listen('\OC\Updater', 'dbUpgrade', function () use($log) {
555
+            $log->info('\OC\Updater::dbUpgrade: Updated database', ['app' => 'updater']);
556
+        });
557
+        $this->listen('\OC\Updater', 'dbSimulateUpgradeBefore', function () use($log) {
558
+            $log->info('\OC\Updater::dbSimulateUpgradeBefore: Checking whether the database schema can be updated (this can take a long time depending on the database size)', ['app' => 'updater']);
559
+        });
560
+        $this->listen('\OC\Updater', 'dbSimulateUpgrade', function () use($log) {
561
+            $log->info('\OC\Updater::dbSimulateUpgrade: Checked database schema update', ['app' => 'updater']);
562
+        });
563
+        $this->listen('\OC\Updater', 'incompatibleAppDisabled', function ($app) use($log) {
564
+            $log->info('\OC\Updater::incompatibleAppDisabled: Disabled incompatible app: ' . $app, ['app' => 'updater']);
565
+        });
566
+        $this->listen('\OC\Updater', 'checkAppStoreAppBefore', function ($app) use($log) {
567
+            $log->info('\OC\Updater::checkAppStoreAppBefore: Checking for update of app "' . $app . '" in appstore', ['app' => 'updater']);
568
+        });
569
+        $this->listen('\OC\Updater', 'upgradeAppStoreApp', function ($app) use($log) {
570
+            $log->info('\OC\Updater::upgradeAppStoreApp: Update app "' . $app . '" from appstore', ['app' => 'updater']);
571
+        });
572
+        $this->listen('\OC\Updater', 'checkAppStoreApp', function ($app) use($log) {
573
+            $log->info('\OC\Updater::checkAppStoreApp: Checked for update of app "' . $app . '" in appstore', ['app' => 'updater']);
574
+        });
575
+        $this->listen('\OC\Updater', 'appUpgradeCheckBefore', function () use ($log) {
576
+            $log->info('\OC\Updater::appUpgradeCheckBefore: Checking updates of apps', ['app' => 'updater']);
577
+        });
578
+        $this->listen('\OC\Updater', 'appSimulateUpdate', function ($app) use ($log) {
579
+            $log->info('\OC\Updater::appSimulateUpdate: Checking whether the database schema for <' . $app . '> can be updated (this can take a long time depending on the database size)', ['app' => 'updater']);
580
+        });
581
+        $this->listen('\OC\Updater', 'appUpgradeCheck', function () use ($log) {
582
+            $log->info('\OC\Updater::appUpgradeCheck: Checked database schema update for apps', ['app' => 'updater']);
583
+        });
584
+        $this->listen('\OC\Updater', 'appUpgradeStarted', function ($app) use ($log) {
585
+            $log->info('\OC\Updater::appUpgradeStarted: Updating <' . $app . '> ...', ['app' => 'updater']);
586
+        });
587
+        $this->listen('\OC\Updater', 'appUpgrade', function ($app, $version) use ($log) {
588
+            $log->info('\OC\Updater::appUpgrade: Updated <' . $app . '> to ' . $version, ['app' => 'updater']);
589
+        });
590
+        $this->listen('\OC\Updater', 'failure', function ($message) use($log) {
591
+            $log->error('\OC\Updater::failure: ' . $message, ['app' => 'updater']);
592
+        });
593
+        $this->listen('\OC\Updater', 'setDebugLogLevel', function () use($log) {
594
+            $log->info('\OC\Updater::setDebugLogLevel: Set log level to debug', ['app' => 'updater']);
595
+        });
596
+        $this->listen('\OC\Updater', 'resetLogLevel', function ($logLevel, $logLevelName) use($log) {
597
+            $log->info('\OC\Updater::resetLogLevel: Reset log level to ' . $logLevelName . '(' . $logLevel . ')', ['app' => 'updater']);
598
+        });
599
+        $this->listen('\OC\Updater', 'startCheckCodeIntegrity', function () use($log) {
600
+            $log->info('\OC\Updater::startCheckCodeIntegrity: Starting code integrity check...', ['app' => 'updater']);
601
+        });
602
+        $this->listen('\OC\Updater', 'finishedCheckCodeIntegrity', function () use($log) {
603
+            $log->info('\OC\Updater::finishedCheckCodeIntegrity: Finished code integrity check', ['app' => 'updater']);
604
+        });
605
+
606
+    }
607 607
 
608 608
 }
609 609
 
Please login to merge, or discard this patch.
Spacing   +65 added lines, -65 removed lines patch added patch discarded remove patch
@@ -101,43 +101,43 @@  discard block
 block discarded – undo
101 101
 		$this->logAllEvents();
102 102
 
103 103
 		$logLevel = $this->config->getSystemValue('loglevel', Util::WARN);
104
-		$this->emit('\OC\Updater', 'setDebugLogLevel', [ $logLevel, $this->logLevelNames[$logLevel] ]);
104
+		$this->emit('\OC\Updater', 'setDebugLogLevel', [$logLevel, $this->logLevelNames[$logLevel]]);
105 105
 		$this->config->setSystemValue('loglevel', Util::DEBUG);
106 106
 
107 107
 		$wasMaintenanceModeEnabled = $this->config->getSystemValue('maintenance', false);
108 108
 
109
-		if(!$wasMaintenanceModeEnabled) {
109
+		if (!$wasMaintenanceModeEnabled) {
110 110
 			$this->config->setSystemValue('maintenance', true);
111 111
 			$this->emit('\OC\Updater', 'maintenanceEnabled');
112 112
 		}
113 113
 
114 114
 		$installedVersion = $this->config->getSystemValue('version', '0.0.0');
115 115
 		$currentVersion = implode('.', \OCP\Util::getVersion());
116
-		$this->log->debug('starting upgrade from ' . $installedVersion . ' to ' . $currentVersion, array('app' => 'core'));
116
+		$this->log->debug('starting upgrade from '.$installedVersion.' to '.$currentVersion, array('app' => 'core'));
117 117
 
118 118
 		$success = true;
119 119
 		try {
120 120
 			$this->doUpgrade($currentVersion, $installedVersion);
121 121
 		} catch (HintException $exception) {
122 122
 			$this->log->logException($exception, ['app' => 'core']);
123
-			$this->emit('\OC\Updater', 'failure', array($exception->getMessage() . ': ' .$exception->getHint()));
123
+			$this->emit('\OC\Updater', 'failure', array($exception->getMessage().': '.$exception->getHint()));
124 124
 			$success = false;
125 125
 		} catch (\Exception $exception) {
126 126
 			$this->log->logException($exception, ['app' => 'core']);
127
-			$this->emit('\OC\Updater', 'failure', array(get_class($exception) . ': ' .$exception->getMessage()));
127
+			$this->emit('\OC\Updater', 'failure', array(get_class($exception).': '.$exception->getMessage()));
128 128
 			$success = false;
129 129
 		}
130 130
 
131 131
 		$this->emit('\OC\Updater', 'updateEnd', array($success));
132 132
 
133
-		if(!$wasMaintenanceModeEnabled && $success) {
133
+		if (!$wasMaintenanceModeEnabled && $success) {
134 134
 			$this->config->setSystemValue('maintenance', false);
135 135
 			$this->emit('\OC\Updater', 'maintenanceDisabled');
136 136
 		} else {
137 137
 			$this->emit('\OC\Updater', 'maintenanceActive');
138 138
 		}
139 139
 
140
-		$this->emit('\OC\Updater', 'resetLogLevel', [ $logLevel, $this->logLevelNames[$logLevel] ]);
140
+		$this->emit('\OC\Updater', 'resetLogLevel', [$logLevel, $this->logLevelNames[$logLevel]]);
141 141
 		$this->config->setSystemValue('loglevel', $logLevel);
142 142
 		$this->config->setSystemValue('installed', true);
143 143
 
@@ -151,7 +151,7 @@  discard block
 block discarded – undo
151 151
 	 */
152 152
 	private function getAllowedPreviousVersions() {
153 153
 		// this should really be a JSON file
154
-		require \OC::$SERVERROOT . '/version.php';
154
+		require \OC::$SERVERROOT.'/version.php';
155 155
 		/** @var array $OC_VersionCanBeUpgradedFrom */
156 156
 		return $OC_VersionCanBeUpgradedFrom;
157 157
 	}
@@ -163,7 +163,7 @@  discard block
 block discarded – undo
163 163
 	 */
164 164
 	private function getVendor() {
165 165
 		// this should really be a JSON file
166
-		require \OC::$SERVERROOT . '/version.php';
166
+		require \OC::$SERVERROOT.'/version.php';
167 167
 		/** @var string $vendor */
168 168
 		return (string) $vendor;
169 169
 	}
@@ -177,7 +177,7 @@  discard block
 block discarded – undo
177 177
 	 */
178 178
 	public function isUpgradePossible($oldVersion, $newVersion, array $allowedPreviousVersions) {
179 179
 		$version = explode('.', $oldVersion);
180
-		$majorMinor = $version[0] . '.' . $version[1];
180
+		$majorMinor = $version[0].'.'.$version[1];
181 181
 
182 182
 		$currentVendor = $this->config->getAppValue('core', 'vendor', '');
183 183
 
@@ -224,7 +224,7 @@  discard block
 block discarded – undo
224 224
 		// create empty file in data dir, so we can later find
225 225
 		// out that this is indeed an ownCloud data directory
226 226
 		// (in case it didn't exist before)
227
-		file_put_contents($this->config->getSystemValue('datadirectory', \OC::$SERVERROOT . '/data') . '/.ocdata', '');
227
+		file_put_contents($this->config->getSystemValue('datadirectory', \OC::$SERVERROOT.'/data').'/.ocdata', '');
228 228
 
229 229
 		// pre-upgrade repairs
230 230
 		$repair = new Repair(Repair::getBeforeUpgradeRepairSteps(), \OC::$server->getEventDispatcher());
@@ -255,7 +255,7 @@  discard block
 block discarded – undo
255 255
 		foreach ($errors as $appId => $exception) {
256 256
 			/** @var \Exception $exception */
257 257
 			$this->log->logException($exception, ['app' => $appId]);
258
-			$this->emit('\OC\Updater', 'failure', [$appId . ': ' . $exception->getMessage()]);
258
+			$this->emit('\OC\Updater', 'failure', [$appId.': '.$exception->getMessage()]);
259 259
 		}
260 260
 
261 261
 		// post-upgrade repairs
@@ -266,7 +266,7 @@  discard block
 block discarded – undo
266 266
 		$this->config->setAppValue('core', 'lastupdatedat', 0);
267 267
 
268 268
 		// Check for code integrity if not disabled
269
-		if(\OC::$server->getIntegrityCodeChecker()->isCodeCheckEnforced()) {
269
+		if (\OC::$server->getIntegrityCodeChecker()->isCodeCheckEnforced()) {
270 270
 			$this->emit('\OC\Updater', 'startCheckCodeIntegrity');
271 271
 			$this->checker->runInstanceVerification();
272 272
 			$this->emit('\OC\Updater', 'finishedCheckCodeIntegrity');
@@ -307,12 +307,12 @@  discard block
 block discarded – undo
307 307
 				 * @link https://github.com/owncloud/core/issues/10980
308 308
 				 * @see \OC_App::updateApp
309 309
 				 */
310
-				if (file_exists(\OC_App::getAppPath($appId) . '/appinfo/preupdate.php')) {
310
+				if (file_exists(\OC_App::getAppPath($appId).'/appinfo/preupdate.php')) {
311 311
 					$this->includePreUpdate($appId);
312 312
 				}
313
-				if (file_exists(\OC_App::getAppPath($appId) . '/appinfo/database.xml')) {
313
+				if (file_exists(\OC_App::getAppPath($appId).'/appinfo/database.xml')) {
314 314
 					$this->emit('\OC\Updater', 'appSimulateUpdate', array($appId));
315
-					\OC_DB::simulateUpdateDbFromStructure(\OC_App::getAppPath($appId) . '/appinfo/database.xml');
315
+					\OC_DB::simulateUpdateDbFromStructure(\OC_App::getAppPath($appId).'/appinfo/database.xml');
316 316
 				}
317 317
 			}
318 318
 		}
@@ -325,7 +325,7 @@  discard block
 block discarded – undo
325 325
 	 * @param string $appId
326 326
 	 */
327 327
 	private function includePreUpdate($appId) {
328
-		include \OC_App::getAppPath($appId) . '/appinfo/preupdate.php';
328
+		include \OC_App::getAppPath($appId).'/appinfo/preupdate.php';
329 329
 	}
330 330
 
331 331
 	/**
@@ -343,7 +343,7 @@  discard block
 block discarded – undo
343 343
 		foreach ($apps as $appId) {
344 344
 			$priorityType = false;
345 345
 			foreach ($priorityTypes as $type) {
346
-				if(!isset($stacks[$type])) {
346
+				if (!isset($stacks[$type])) {
347 347
 					$stacks[$type] = array();
348 348
 				}
349 349
 				if (\OC_App::isType($appId, [$type])) {
@@ -363,7 +363,7 @@  discard block
 block discarded – undo
363 363
 					\OC_App::updateApp($appId);
364 364
 					$this->emit('\OC\Updater', 'appUpgrade', [$appId, \OC_App::getAppVersion($appId)]);
365 365
 				}
366
-				if($type !== $pseudoOtherType) {
366
+				if ($type !== $pseudoOtherType) {
367 367
 					// load authentication, filesystem and logging apps after
368 368
 					// upgrading them. Other apps my need to rely on modifying
369 369
 					// user and/or filesystem aspects.
@@ -391,9 +391,9 @@  discard block
 block discarded – undo
391 391
 		foreach ($apps as $app) {
392 392
 			// check if the app is compatible with this version of ownCloud
393 393
 			$info = OC_App::getAppInfo($app);
394
-			if(!OC_App::isAppCompatible($version, $info)) {
394
+			if (!OC_App::isAppCompatible($version, $info)) {
395 395
 				if ($appManager->isShipped($app)) {
396
-					throw new \UnexpectedValueException('The files of the app "' . $app . '" were not correctly replaced before running the update');
396
+					throw new \UnexpectedValueException('The files of the app "'.$app.'" were not correctly replaced before running the update');
397 397
 				}
398 398
 				OC_App::disable($app);
399 399
 				$this->emit('\OC\Updater', 'incompatibleAppDisabled', array($app));
@@ -431,7 +431,7 @@  discard block
 block discarded – undo
431 431
 	 * @throws \Exception
432 432
 	 */
433 433
 	private function upgradeAppStoreApps(array $disabledApps) {
434
-		foreach($disabledApps as $app) {
434
+		foreach ($disabledApps as $app) {
435 435
 			try {
436 436
 				$this->emit('\OC\Updater', 'checkAppStoreAppBefore', [$app]);
437 437
 				if ($this->installer->isUpdateAvailable($app)) {
@@ -450,22 +450,22 @@  discard block
 block discarded – undo
450 450
 	 */
451 451
 	private function emitRepairEvents() {
452 452
 		$dispatcher = \OC::$server->getEventDispatcher();
453
-		$dispatcher->addListener('\OC\Repair::warning', function ($event) {
453
+		$dispatcher->addListener('\OC\Repair::warning', function($event) {
454 454
 			if ($event instanceof GenericEvent) {
455 455
 				$this->emit('\OC\Updater', 'repairWarning', $event->getArguments());
456 456
 			}
457 457
 		});
458
-		$dispatcher->addListener('\OC\Repair::error', function ($event) {
458
+		$dispatcher->addListener('\OC\Repair::error', function($event) {
459 459
 			if ($event instanceof GenericEvent) {
460 460
 				$this->emit('\OC\Updater', 'repairError', $event->getArguments());
461 461
 			}
462 462
 		});
463
-		$dispatcher->addListener('\OC\Repair::info', function ($event) {
463
+		$dispatcher->addListener('\OC\Repair::info', function($event) {
464 464
 			if ($event instanceof GenericEvent) {
465 465
 				$this->emit('\OC\Updater', 'repairInfo', $event->getArguments());
466 466
 			}
467 467
 		});
468
-		$dispatcher->addListener('\OC\Repair::step', function ($event) {
468
+		$dispatcher->addListener('\OC\Repair::step', function($event) {
469 469
 			if ($event instanceof GenericEvent) {
470 470
 				$this->emit('\OC\Updater', 'repairStep', $event->getArguments());
471 471
 			}
@@ -480,13 +480,13 @@  discard block
 block discarded – undo
480 480
 			if (!$event instanceof GenericEvent) {
481 481
 				return;
482 482
 			}
483
-			$log->info('\OC\DB\Migrator::executeSql: ' . $event->getSubject() . ' (' . $event->getArgument(0) . ' of ' . $event->getArgument(1) . ')', ['app' => 'updater']);
483
+			$log->info('\OC\DB\Migrator::executeSql: '.$event->getSubject().' ('.$event->getArgument(0).' of '.$event->getArgument(1).')', ['app' => 'updater']);
484 484
 		});
485 485
 		$dispatcher->addListener('\OC\DB\Migrator::checkTable', function($event) use ($log) {
486 486
 			if (!$event instanceof GenericEvent) {
487 487
 				return;
488 488
 			}
489
-			$log->info('\OC\DB\Migrator::checkTable: ' . $event->getSubject() . ' (' . $event->getArgument(0) . ' of ' . $event->getArgument(1) . ')', ['app' => 'updater']);
489
+			$log->info('\OC\DB\Migrator::checkTable: '.$event->getSubject().' ('.$event->getArgument(0).' of '.$event->getArgument(1).')', ['app' => 'updater']);
490 490
 		});
491 491
 
492 492
 		$repairListener = function($event) use ($log) {
@@ -495,30 +495,30 @@  discard block
 block discarded – undo
495 495
 			}
496 496
 			switch ($event->getSubject()) {
497 497
 				case '\OC\Repair::startProgress':
498
-					$log->info('\OC\Repair::startProgress: Starting ... ' . $event->getArgument(1) .  ' (' . $event->getArgument(0) . ')', ['app' => 'updater']);
498
+					$log->info('\OC\Repair::startProgress: Starting ... '.$event->getArgument(1).' ('.$event->getArgument(0).')', ['app' => 'updater']);
499 499
 					break;
500 500
 				case '\OC\Repair::advance':
501 501
 					$desc = $event->getArgument(1);
502 502
 					if (empty($desc)) {
503 503
 						$desc = '';
504 504
 					}
505
-					$log->info('\OC\Repair::advance: ' . $desc . ' (' . $event->getArgument(0) . ')', ['app' => 'updater']);
505
+					$log->info('\OC\Repair::advance: '.$desc.' ('.$event->getArgument(0).')', ['app' => 'updater']);
506 506
 
507 507
 					break;
508 508
 				case '\OC\Repair::finishProgress':
509 509
 					$log->info('\OC\Repair::finishProgress', ['app' => 'updater']);
510 510
 					break;
511 511
 				case '\OC\Repair::step':
512
-					$log->info('\OC\Repair::step: Repair step: ' . $event->getArgument(0), ['app' => 'updater']);
512
+					$log->info('\OC\Repair::step: Repair step: '.$event->getArgument(0), ['app' => 'updater']);
513 513
 					break;
514 514
 				case '\OC\Repair::info':
515
-					$log->info('\OC\Repair::info: Repair info: ' . $event->getArgument(0), ['app' => 'updater']);
515
+					$log->info('\OC\Repair::info: Repair info: '.$event->getArgument(0), ['app' => 'updater']);
516 516
 					break;
517 517
 				case '\OC\Repair::warning':
518
-					$log->warning('\OC\Repair::warning: Repair warning: ' . $event->getArgument(0), ['app' => 'updater']);
518
+					$log->warning('\OC\Repair::warning: Repair warning: '.$event->getArgument(0), ['app' => 'updater']);
519 519
 					break;
520 520
 				case '\OC\Repair::error':
521
-					$log->error('\OC\Repair::error: Repair error: ' . $event->getArgument(0), ['app' => 'updater']);
521
+					$log->error('\OC\Repair::error: Repair error: '.$event->getArgument(0), ['app' => 'updater']);
522 522
 					break;
523 523
 			}
524 524
 		};
@@ -532,74 +532,74 @@  discard block
 block discarded – undo
532 532
 		$dispatcher->addListener('\OC\Repair::error', $repairListener);
533 533
 
534 534
 
535
-		$this->listen('\OC\Updater', 'maintenanceEnabled', function () use($log) {
535
+		$this->listen('\OC\Updater', 'maintenanceEnabled', function() use($log) {
536 536
 			$log->info('\OC\Updater::maintenanceEnabled: Turned on maintenance mode', ['app' => 'updater']);
537 537
 		});
538
-		$this->listen('\OC\Updater', 'maintenanceDisabled', function () use($log) {
538
+		$this->listen('\OC\Updater', 'maintenanceDisabled', function() use($log) {
539 539
 			$log->info('\OC\Updater::maintenanceDisabled: Turned off maintenance mode', ['app' => 'updater']);
540 540
 		});
541
-		$this->listen('\OC\Updater', 'maintenanceActive', function () use($log) {
541
+		$this->listen('\OC\Updater', 'maintenanceActive', function() use($log) {
542 542
 			$log->info('\OC\Updater::maintenanceActive: Maintenance mode is kept active', ['app' => 'updater']);
543 543
 		});
544
-		$this->listen('\OC\Updater', 'updateEnd', function ($success) use($log) {
544
+		$this->listen('\OC\Updater', 'updateEnd', function($success) use($log) {
545 545
 			if ($success) {
546 546
 				$log->info('\OC\Updater::updateEnd: Update successful', ['app' => 'updater']);
547 547
 			} else {
548 548
 				$log->error('\OC\Updater::updateEnd: Update failed', ['app' => 'updater']);
549 549
 			}
550 550
 		});
551
-		$this->listen('\OC\Updater', 'dbUpgradeBefore', function () use($log) {
551
+		$this->listen('\OC\Updater', 'dbUpgradeBefore', function() use($log) {
552 552
 			$log->info('\OC\Updater::dbUpgradeBefore: Updating database schema', ['app' => 'updater']);
553 553
 		});
554
-		$this->listen('\OC\Updater', 'dbUpgrade', function () use($log) {
554
+		$this->listen('\OC\Updater', 'dbUpgrade', function() use($log) {
555 555
 			$log->info('\OC\Updater::dbUpgrade: Updated database', ['app' => 'updater']);
556 556
 		});
557
-		$this->listen('\OC\Updater', 'dbSimulateUpgradeBefore', function () use($log) {
557
+		$this->listen('\OC\Updater', 'dbSimulateUpgradeBefore', function() use($log) {
558 558
 			$log->info('\OC\Updater::dbSimulateUpgradeBefore: Checking whether the database schema can be updated (this can take a long time depending on the database size)', ['app' => 'updater']);
559 559
 		});
560
-		$this->listen('\OC\Updater', 'dbSimulateUpgrade', function () use($log) {
560
+		$this->listen('\OC\Updater', 'dbSimulateUpgrade', function() use($log) {
561 561
 			$log->info('\OC\Updater::dbSimulateUpgrade: Checked database schema update', ['app' => 'updater']);
562 562
 		});
563
-		$this->listen('\OC\Updater', 'incompatibleAppDisabled', function ($app) use($log) {
564
-			$log->info('\OC\Updater::incompatibleAppDisabled: Disabled incompatible app: ' . $app, ['app' => 'updater']);
563
+		$this->listen('\OC\Updater', 'incompatibleAppDisabled', function($app) use($log) {
564
+			$log->info('\OC\Updater::incompatibleAppDisabled: Disabled incompatible app: '.$app, ['app' => 'updater']);
565 565
 		});
566
-		$this->listen('\OC\Updater', 'checkAppStoreAppBefore', function ($app) use($log) {
567
-			$log->info('\OC\Updater::checkAppStoreAppBefore: Checking for update of app "' . $app . '" in appstore', ['app' => 'updater']);
566
+		$this->listen('\OC\Updater', 'checkAppStoreAppBefore', function($app) use($log) {
567
+			$log->info('\OC\Updater::checkAppStoreAppBefore: Checking for update of app "'.$app.'" in appstore', ['app' => 'updater']);
568 568
 		});
569
-		$this->listen('\OC\Updater', 'upgradeAppStoreApp', function ($app) use($log) {
570
-			$log->info('\OC\Updater::upgradeAppStoreApp: Update app "' . $app . '" from appstore', ['app' => 'updater']);
569
+		$this->listen('\OC\Updater', 'upgradeAppStoreApp', function($app) use($log) {
570
+			$log->info('\OC\Updater::upgradeAppStoreApp: Update app "'.$app.'" from appstore', ['app' => 'updater']);
571 571
 		});
572
-		$this->listen('\OC\Updater', 'checkAppStoreApp', function ($app) use($log) {
573
-			$log->info('\OC\Updater::checkAppStoreApp: Checked for update of app "' . $app . '" in appstore', ['app' => 'updater']);
572
+		$this->listen('\OC\Updater', 'checkAppStoreApp', function($app) use($log) {
573
+			$log->info('\OC\Updater::checkAppStoreApp: Checked for update of app "'.$app.'" in appstore', ['app' => 'updater']);
574 574
 		});
575
-		$this->listen('\OC\Updater', 'appUpgradeCheckBefore', function () use ($log) {
575
+		$this->listen('\OC\Updater', 'appUpgradeCheckBefore', function() use ($log) {
576 576
 			$log->info('\OC\Updater::appUpgradeCheckBefore: Checking updates of apps', ['app' => 'updater']);
577 577
 		});
578
-		$this->listen('\OC\Updater', 'appSimulateUpdate', function ($app) use ($log) {
579
-			$log->info('\OC\Updater::appSimulateUpdate: Checking whether the database schema for <' . $app . '> can be updated (this can take a long time depending on the database size)', ['app' => 'updater']);
578
+		$this->listen('\OC\Updater', 'appSimulateUpdate', function($app) use ($log) {
579
+			$log->info('\OC\Updater::appSimulateUpdate: Checking whether the database schema for <'.$app.'> can be updated (this can take a long time depending on the database size)', ['app' => 'updater']);
580 580
 		});
581
-		$this->listen('\OC\Updater', 'appUpgradeCheck', function () use ($log) {
581
+		$this->listen('\OC\Updater', 'appUpgradeCheck', function() use ($log) {
582 582
 			$log->info('\OC\Updater::appUpgradeCheck: Checked database schema update for apps', ['app' => 'updater']);
583 583
 		});
584
-		$this->listen('\OC\Updater', 'appUpgradeStarted', function ($app) use ($log) {
585
-			$log->info('\OC\Updater::appUpgradeStarted: Updating <' . $app . '> ...', ['app' => 'updater']);
584
+		$this->listen('\OC\Updater', 'appUpgradeStarted', function($app) use ($log) {
585
+			$log->info('\OC\Updater::appUpgradeStarted: Updating <'.$app.'> ...', ['app' => 'updater']);
586 586
 		});
587
-		$this->listen('\OC\Updater', 'appUpgrade', function ($app, $version) use ($log) {
588
-			$log->info('\OC\Updater::appUpgrade: Updated <' . $app . '> to ' . $version, ['app' => 'updater']);
587
+		$this->listen('\OC\Updater', 'appUpgrade', function($app, $version) use ($log) {
588
+			$log->info('\OC\Updater::appUpgrade: Updated <'.$app.'> to '.$version, ['app' => 'updater']);
589 589
 		});
590
-		$this->listen('\OC\Updater', 'failure', function ($message) use($log) {
591
-			$log->error('\OC\Updater::failure: ' . $message, ['app' => 'updater']);
590
+		$this->listen('\OC\Updater', 'failure', function($message) use($log) {
591
+			$log->error('\OC\Updater::failure: '.$message, ['app' => 'updater']);
592 592
 		});
593
-		$this->listen('\OC\Updater', 'setDebugLogLevel', function () use($log) {
593
+		$this->listen('\OC\Updater', 'setDebugLogLevel', function() use($log) {
594 594
 			$log->info('\OC\Updater::setDebugLogLevel: Set log level to debug', ['app' => 'updater']);
595 595
 		});
596
-		$this->listen('\OC\Updater', 'resetLogLevel', function ($logLevel, $logLevelName) use($log) {
597
-			$log->info('\OC\Updater::resetLogLevel: Reset log level to ' . $logLevelName . '(' . $logLevel . ')', ['app' => 'updater']);
596
+		$this->listen('\OC\Updater', 'resetLogLevel', function($logLevel, $logLevelName) use($log) {
597
+			$log->info('\OC\Updater::resetLogLevel: Reset log level to '.$logLevelName.'('.$logLevel.')', ['app' => 'updater']);
598 598
 		});
599
-		$this->listen('\OC\Updater', 'startCheckCodeIntegrity', function () use($log) {
599
+		$this->listen('\OC\Updater', 'startCheckCodeIntegrity', function() use($log) {
600 600
 			$log->info('\OC\Updater::startCheckCodeIntegrity: Starting code integrity check...', ['app' => 'updater']);
601 601
 		});
602
-		$this->listen('\OC\Updater', 'finishedCheckCodeIntegrity', function () use($log) {
602
+		$this->listen('\OC\Updater', 'finishedCheckCodeIntegrity', function() use($log) {
603 603
 			$log->info('\OC\Updater::finishedCheckCodeIntegrity: Finished code integrity check', ['app' => 'updater']);
604 604
 		});
605 605
 
Please login to merge, or discard this patch.
lib/private/App/AppManager.php 2 patches
Indentation   +402 added lines, -402 removed lines patch added patch discarded remove patch
@@ -44,406 +44,406 @@
 block discarded – undo
44 44
 
45 45
 class AppManager implements IAppManager {
46 46
 
47
-	/**
48
-	 * Apps with these types can not be enabled for certain groups only
49
-	 * @var string[]
50
-	 */
51
-	protected $protectedAppTypes = [
52
-		'filesystem',
53
-		'prelogin',
54
-		'authentication',
55
-		'logging',
56
-		'prevent_group_restriction',
57
-	];
58
-
59
-	/** @var IUserSession */
60
-	private $userSession;
61
-
62
-	/** @var AppConfig */
63
-	private $appConfig;
64
-
65
-	/** @var IGroupManager */
66
-	private $groupManager;
67
-
68
-	/** @var ICacheFactory */
69
-	private $memCacheFactory;
70
-
71
-	/** @var EventDispatcherInterface */
72
-	private $dispatcher;
73
-
74
-	/** @var string[] $appId => $enabled */
75
-	private $installedAppsCache;
76
-
77
-	/** @var string[] */
78
-	private $shippedApps;
79
-
80
-	/** @var string[] */
81
-	private $alwaysEnabled;
82
-
83
-	/** @var array */
84
-	private $appInfos = [];
85
-
86
-	/** @var array */
87
-	private $appVersions = [];
88
-
89
-	/**
90
-	 * @param IUserSession $userSession
91
-	 * @param AppConfig $appConfig
92
-	 * @param IGroupManager $groupManager
93
-	 * @param ICacheFactory $memCacheFactory
94
-	 * @param EventDispatcherInterface $dispatcher
95
-	 */
96
-	public function __construct(IUserSession $userSession,
97
-								AppConfig $appConfig,
98
-								IGroupManager $groupManager,
99
-								ICacheFactory $memCacheFactory,
100
-								EventDispatcherInterface $dispatcher) {
101
-		$this->userSession = $userSession;
102
-		$this->appConfig = $appConfig;
103
-		$this->groupManager = $groupManager;
104
-		$this->memCacheFactory = $memCacheFactory;
105
-		$this->dispatcher = $dispatcher;
106
-	}
107
-
108
-	/**
109
-	 * @return string[] $appId => $enabled
110
-	 */
111
-	private function getInstalledAppsValues() {
112
-		if (!$this->installedAppsCache) {
113
-			$values = $this->appConfig->getValues(false, 'enabled');
114
-
115
-			$alwaysEnabledApps = $this->getAlwaysEnabledApps();
116
-			foreach($alwaysEnabledApps as $appId) {
117
-				$values[$appId] = 'yes';
118
-			}
119
-
120
-			$this->installedAppsCache = array_filter($values, function ($value) {
121
-				return $value !== 'no';
122
-			});
123
-			ksort($this->installedAppsCache);
124
-		}
125
-		return $this->installedAppsCache;
126
-	}
127
-
128
-	/**
129
-	 * List all installed apps
130
-	 *
131
-	 * @return string[]
132
-	 */
133
-	public function getInstalledApps() {
134
-		return array_keys($this->getInstalledAppsValues());
135
-	}
136
-
137
-	/**
138
-	 * List all apps enabled for a user
139
-	 *
140
-	 * @param \OCP\IUser $user
141
-	 * @return string[]
142
-	 */
143
-	public function getEnabledAppsForUser(IUser $user) {
144
-		$apps = $this->getInstalledAppsValues();
145
-		$appsForUser = array_filter($apps, function ($enabled) use ($user) {
146
-			return $this->checkAppForUser($enabled, $user);
147
-		});
148
-		return array_keys($appsForUser);
149
-	}
150
-
151
-	/**
152
-	 * Check if an app is enabled for user
153
-	 *
154
-	 * @param string $appId
155
-	 * @param \OCP\IUser $user (optional) if not defined, the currently logged in user will be used
156
-	 * @return bool
157
-	 */
158
-	public function isEnabledForUser($appId, $user = null) {
159
-		if ($this->isAlwaysEnabled($appId)) {
160
-			return true;
161
-		}
162
-		if ($user === null) {
163
-			$user = $this->userSession->getUser();
164
-		}
165
-		$installedApps = $this->getInstalledAppsValues();
166
-		if (isset($installedApps[$appId])) {
167
-			return $this->checkAppForUser($installedApps[$appId], $user);
168
-		} else {
169
-			return false;
170
-		}
171
-	}
172
-
173
-	/**
174
-	 * @param string $enabled
175
-	 * @param IUser $user
176
-	 * @return bool
177
-	 */
178
-	private function checkAppForUser($enabled, $user) {
179
-		if ($enabled === 'yes') {
180
-			return true;
181
-		} elseif ($user === null) {
182
-			return false;
183
-		} else {
184
-			if(empty($enabled)){
185
-				return false;
186
-			}
187
-
188
-			$groupIds = json_decode($enabled);
189
-
190
-			if (!is_array($groupIds)) {
191
-				$jsonError = json_last_error();
192
-				\OC::$server->getLogger()->warning('AppManger::checkAppForUser - can\'t decode group IDs: ' . print_r($enabled, true) . ' - json error code: ' . $jsonError, ['app' => 'lib']);
193
-				return false;
194
-			}
195
-
196
-			$userGroups = $this->groupManager->getUserGroupIds($user);
197
-			foreach ($userGroups as $groupId) {
198
-				if (in_array($groupId, $groupIds, true)) {
199
-					return true;
200
-				}
201
-			}
202
-			return false;
203
-		}
204
-	}
205
-
206
-	/**
207
-	 * Check if an app is installed in the instance
208
-	 *
209
-	 * @param string $appId
210
-	 * @return bool
211
-	 */
212
-	public function isInstalled($appId) {
213
-		$installedApps = $this->getInstalledAppsValues();
214
-		return isset($installedApps[$appId]);
215
-	}
216
-
217
-	/**
218
-	 * Enable an app for every user
219
-	 *
220
-	 * @param string $appId
221
-	 * @throws AppPathNotFoundException
222
-	 */
223
-	public function enableApp($appId) {
224
-		// Check if app exists
225
-		$this->getAppPath($appId);
226
-
227
-		$this->installedAppsCache[$appId] = 'yes';
228
-		$this->appConfig->setValue($appId, 'enabled', 'yes');
229
-		$this->dispatcher->dispatch(ManagerEvent::EVENT_APP_ENABLE, new ManagerEvent(
230
-			ManagerEvent::EVENT_APP_ENABLE, $appId
231
-		));
232
-		$this->clearAppsCache();
233
-	}
234
-
235
-	/**
236
-	 * Whether a list of types contains a protected app type
237
-	 *
238
-	 * @param string[] $types
239
-	 * @return bool
240
-	 */
241
-	public function hasProtectedAppType($types) {
242
-		if (empty($types)) {
243
-			return false;
244
-		}
245
-
246
-		$protectedTypes = array_intersect($this->protectedAppTypes, $types);
247
-		return !empty($protectedTypes);
248
-	}
249
-
250
-	/**
251
-	 * Enable an app only for specific groups
252
-	 *
253
-	 * @param string $appId
254
-	 * @param \OCP\IGroup[] $groups
255
-	 * @throws \Exception if app can't be enabled for groups
256
-	 */
257
-	public function enableAppForGroups($appId, $groups) {
258
-		$info = $this->getAppInfo($appId);
259
-		if (!empty($info['types'])) {
260
-			$protectedTypes = array_intersect($this->protectedAppTypes, $info['types']);
261
-			if (!empty($protectedTypes)) {
262
-				throw new \Exception("$appId can't be enabled for groups.");
263
-			}
264
-		}
265
-
266
-		$groupIds = array_map(function ($group) {
267
-			/** @var \OCP\IGroup $group */
268
-			return $group->getGID();
269
-		}, $groups);
270
-		$this->installedAppsCache[$appId] = json_encode($groupIds);
271
-		$this->appConfig->setValue($appId, 'enabled', json_encode($groupIds));
272
-		$this->dispatcher->dispatch(ManagerEvent::EVENT_APP_ENABLE_FOR_GROUPS, new ManagerEvent(
273
-			ManagerEvent::EVENT_APP_ENABLE_FOR_GROUPS, $appId, $groups
274
-		));
275
-		$this->clearAppsCache();
276
-	}
277
-
278
-	/**
279
-	 * Disable an app for every user
280
-	 *
281
-	 * @param string $appId
282
-	 * @throws \Exception if app can't be disabled
283
-	 */
284
-	public function disableApp($appId) {
285
-		if ($this->isAlwaysEnabled($appId)) {
286
-			throw new \Exception("$appId can't be disabled.");
287
-		}
288
-		unset($this->installedAppsCache[$appId]);
289
-		$this->appConfig->setValue($appId, 'enabled', 'no');
290
-		$this->dispatcher->dispatch(ManagerEvent::EVENT_APP_DISABLE, new ManagerEvent(
291
-			ManagerEvent::EVENT_APP_DISABLE, $appId
292
-		));
293
-		$this->clearAppsCache();
294
-	}
295
-
296
-	/**
297
-	 * Get the directory for the given app.
298
-	 *
299
-	 * @param string $appId
300
-	 * @return string
301
-	 * @throws AppPathNotFoundException if app folder can't be found
302
-	 */
303
-	public function getAppPath($appId) {
304
-		$appPath = \OC_App::getAppPath($appId);
305
-		if($appPath === false) {
306
-			throw new AppPathNotFoundException('Could not find path for ' . $appId);
307
-		}
308
-		return $appPath;
309
-	}
310
-
311
-	/**
312
-	 * Clear the cached list of apps when enabling/disabling an app
313
-	 */
314
-	public function clearAppsCache() {
315
-		$settingsMemCache = $this->memCacheFactory->createDistributed('settings');
316
-		$settingsMemCache->clear('listApps');
317
-	}
318
-
319
-	/**
320
-	 * Returns a list of apps that need upgrade
321
-	 *
322
-	 * @param string $version Nextcloud version as array of version components
323
-	 * @return array list of app info from apps that need an upgrade
324
-	 *
325
-	 * @internal
326
-	 */
327
-	public function getAppsNeedingUpgrade($version) {
328
-		$appsToUpgrade = [];
329
-		$apps = $this->getInstalledApps();
330
-		foreach ($apps as $appId) {
331
-			$appInfo = $this->getAppInfo($appId);
332
-			$appDbVersion = $this->appConfig->getValue($appId, 'installed_version');
333
-			if ($appDbVersion
334
-				&& isset($appInfo['version'])
335
-				&& version_compare($appInfo['version'], $appDbVersion, '>')
336
-				&& \OC_App::isAppCompatible($version, $appInfo)
337
-			) {
338
-				$appsToUpgrade[] = $appInfo;
339
-			}
340
-		}
341
-
342
-		return $appsToUpgrade;
343
-	}
344
-
345
-	/**
346
-	 * Returns the app information from "appinfo/info.xml".
347
-	 *
348
-	 * @param string $appId app id
349
-	 *
350
-	 * @param bool $path
351
-	 * @param null $lang
352
-	 * @return array app info
353
-	 */
354
-	public function getAppInfo(string $appId, bool $path = false, $lang = null) {
355
-		if ($path) {
356
-			$file = $appId;
357
-		} else {
358
-			if ($lang === null && isset($this->appInfos[$appId])) {
359
-				return $this->appInfos[$appId];
360
-			}
361
-			try {
362
-				$appPath = $this->getAppPath($appId);
363
-			} catch (AppPathNotFoundException $e) {
364
-				return null;
365
-			}
366
-			$file = $appPath . '/appinfo/info.xml';
367
-		}
368
-
369
-		$parser = new InfoParser($this->memCacheFactory->createLocal('core.appinfo'));
370
-		$data = $parser->parse($file);
371
-
372
-		if (is_array($data)) {
373
-			$data = \OC_App::parseAppInfo($data, $lang);
374
-		}
375
-
376
-		if ($lang === null) {
377
-			$this->appInfos[$appId] = $data;
378
-		}
379
-
380
-		return $data;
381
-	}
382
-
383
-	public function getAppVersion(string $appId, bool $useCache = true): string {
384
-		if(!$useCache || !isset($this->appVersions[$appId])) {
385
-			$appInfo = \OC::$server->getAppManager()->getAppInfo($appId);
386
-			$this->appVersions[$appId] = ($appInfo !== null && isset($appInfo['version'])) ? $appInfo['version'] : '0';
387
-		}
388
-		return $this->appVersions[$appId];
389
-	}
390
-
391
-	/**
392
-	 * Returns a list of apps incompatible with the given version
393
-	 *
394
-	 * @param string $version Nextcloud version as array of version components
395
-	 *
396
-	 * @return array list of app info from incompatible apps
397
-	 *
398
-	 * @internal
399
-	 */
400
-	public function getIncompatibleApps($version) {
401
-		$apps = $this->getInstalledApps();
402
-		$incompatibleApps = array();
403
-		foreach ($apps as $appId) {
404
-			$info = $this->getAppInfo($appId);
405
-			if (!\OC_App::isAppCompatible($version, $info)) {
406
-				$incompatibleApps[] = $info;
407
-			}
408
-		}
409
-		return $incompatibleApps;
410
-	}
411
-
412
-	/**
413
-	 * @inheritdoc
414
-	 * In case you change this method, also change \OC\App\CodeChecker\InfoChecker::isShipped()
415
-	 */
416
-	public function isShipped($appId) {
417
-		$this->loadShippedJson();
418
-		return in_array($appId, $this->shippedApps, true);
419
-	}
420
-
421
-	private function isAlwaysEnabled($appId) {
422
-		$alwaysEnabled = $this->getAlwaysEnabledApps();
423
-		return in_array($appId, $alwaysEnabled, true);
424
-	}
425
-
426
-	/**
427
-	 * In case you change this method, also change \OC\App\CodeChecker\InfoChecker::loadShippedJson()
428
-	 * @throws \Exception
429
-	 */
430
-	private function loadShippedJson() {
431
-		if ($this->shippedApps === null) {
432
-			$shippedJson = \OC::$SERVERROOT . '/core/shipped.json';
433
-			if (!file_exists($shippedJson)) {
434
-				throw new \Exception("File not found: $shippedJson");
435
-			}
436
-			$content = json_decode(file_get_contents($shippedJson), true);
437
-			$this->shippedApps = $content['shippedApps'];
438
-			$this->alwaysEnabled = $content['alwaysEnabled'];
439
-		}
440
-	}
441
-
442
-	/**
443
-	 * @inheritdoc
444
-	 */
445
-	public function getAlwaysEnabledApps() {
446
-		$this->loadShippedJson();
447
-		return $this->alwaysEnabled;
448
-	}
47
+    /**
48
+     * Apps with these types can not be enabled for certain groups only
49
+     * @var string[]
50
+     */
51
+    protected $protectedAppTypes = [
52
+        'filesystem',
53
+        'prelogin',
54
+        'authentication',
55
+        'logging',
56
+        'prevent_group_restriction',
57
+    ];
58
+
59
+    /** @var IUserSession */
60
+    private $userSession;
61
+
62
+    /** @var AppConfig */
63
+    private $appConfig;
64
+
65
+    /** @var IGroupManager */
66
+    private $groupManager;
67
+
68
+    /** @var ICacheFactory */
69
+    private $memCacheFactory;
70
+
71
+    /** @var EventDispatcherInterface */
72
+    private $dispatcher;
73
+
74
+    /** @var string[] $appId => $enabled */
75
+    private $installedAppsCache;
76
+
77
+    /** @var string[] */
78
+    private $shippedApps;
79
+
80
+    /** @var string[] */
81
+    private $alwaysEnabled;
82
+
83
+    /** @var array */
84
+    private $appInfos = [];
85
+
86
+    /** @var array */
87
+    private $appVersions = [];
88
+
89
+    /**
90
+     * @param IUserSession $userSession
91
+     * @param AppConfig $appConfig
92
+     * @param IGroupManager $groupManager
93
+     * @param ICacheFactory $memCacheFactory
94
+     * @param EventDispatcherInterface $dispatcher
95
+     */
96
+    public function __construct(IUserSession $userSession,
97
+                                AppConfig $appConfig,
98
+                                IGroupManager $groupManager,
99
+                                ICacheFactory $memCacheFactory,
100
+                                EventDispatcherInterface $dispatcher) {
101
+        $this->userSession = $userSession;
102
+        $this->appConfig = $appConfig;
103
+        $this->groupManager = $groupManager;
104
+        $this->memCacheFactory = $memCacheFactory;
105
+        $this->dispatcher = $dispatcher;
106
+    }
107
+
108
+    /**
109
+     * @return string[] $appId => $enabled
110
+     */
111
+    private function getInstalledAppsValues() {
112
+        if (!$this->installedAppsCache) {
113
+            $values = $this->appConfig->getValues(false, 'enabled');
114
+
115
+            $alwaysEnabledApps = $this->getAlwaysEnabledApps();
116
+            foreach($alwaysEnabledApps as $appId) {
117
+                $values[$appId] = 'yes';
118
+            }
119
+
120
+            $this->installedAppsCache = array_filter($values, function ($value) {
121
+                return $value !== 'no';
122
+            });
123
+            ksort($this->installedAppsCache);
124
+        }
125
+        return $this->installedAppsCache;
126
+    }
127
+
128
+    /**
129
+     * List all installed apps
130
+     *
131
+     * @return string[]
132
+     */
133
+    public function getInstalledApps() {
134
+        return array_keys($this->getInstalledAppsValues());
135
+    }
136
+
137
+    /**
138
+     * List all apps enabled for a user
139
+     *
140
+     * @param \OCP\IUser $user
141
+     * @return string[]
142
+     */
143
+    public function getEnabledAppsForUser(IUser $user) {
144
+        $apps = $this->getInstalledAppsValues();
145
+        $appsForUser = array_filter($apps, function ($enabled) use ($user) {
146
+            return $this->checkAppForUser($enabled, $user);
147
+        });
148
+        return array_keys($appsForUser);
149
+    }
150
+
151
+    /**
152
+     * Check if an app is enabled for user
153
+     *
154
+     * @param string $appId
155
+     * @param \OCP\IUser $user (optional) if not defined, the currently logged in user will be used
156
+     * @return bool
157
+     */
158
+    public function isEnabledForUser($appId, $user = null) {
159
+        if ($this->isAlwaysEnabled($appId)) {
160
+            return true;
161
+        }
162
+        if ($user === null) {
163
+            $user = $this->userSession->getUser();
164
+        }
165
+        $installedApps = $this->getInstalledAppsValues();
166
+        if (isset($installedApps[$appId])) {
167
+            return $this->checkAppForUser($installedApps[$appId], $user);
168
+        } else {
169
+            return false;
170
+        }
171
+    }
172
+
173
+    /**
174
+     * @param string $enabled
175
+     * @param IUser $user
176
+     * @return bool
177
+     */
178
+    private function checkAppForUser($enabled, $user) {
179
+        if ($enabled === 'yes') {
180
+            return true;
181
+        } elseif ($user === null) {
182
+            return false;
183
+        } else {
184
+            if(empty($enabled)){
185
+                return false;
186
+            }
187
+
188
+            $groupIds = json_decode($enabled);
189
+
190
+            if (!is_array($groupIds)) {
191
+                $jsonError = json_last_error();
192
+                \OC::$server->getLogger()->warning('AppManger::checkAppForUser - can\'t decode group IDs: ' . print_r($enabled, true) . ' - json error code: ' . $jsonError, ['app' => 'lib']);
193
+                return false;
194
+            }
195
+
196
+            $userGroups = $this->groupManager->getUserGroupIds($user);
197
+            foreach ($userGroups as $groupId) {
198
+                if (in_array($groupId, $groupIds, true)) {
199
+                    return true;
200
+                }
201
+            }
202
+            return false;
203
+        }
204
+    }
205
+
206
+    /**
207
+     * Check if an app is installed in the instance
208
+     *
209
+     * @param string $appId
210
+     * @return bool
211
+     */
212
+    public function isInstalled($appId) {
213
+        $installedApps = $this->getInstalledAppsValues();
214
+        return isset($installedApps[$appId]);
215
+    }
216
+
217
+    /**
218
+     * Enable an app for every user
219
+     *
220
+     * @param string $appId
221
+     * @throws AppPathNotFoundException
222
+     */
223
+    public function enableApp($appId) {
224
+        // Check if app exists
225
+        $this->getAppPath($appId);
226
+
227
+        $this->installedAppsCache[$appId] = 'yes';
228
+        $this->appConfig->setValue($appId, 'enabled', 'yes');
229
+        $this->dispatcher->dispatch(ManagerEvent::EVENT_APP_ENABLE, new ManagerEvent(
230
+            ManagerEvent::EVENT_APP_ENABLE, $appId
231
+        ));
232
+        $this->clearAppsCache();
233
+    }
234
+
235
+    /**
236
+     * Whether a list of types contains a protected app type
237
+     *
238
+     * @param string[] $types
239
+     * @return bool
240
+     */
241
+    public function hasProtectedAppType($types) {
242
+        if (empty($types)) {
243
+            return false;
244
+        }
245
+
246
+        $protectedTypes = array_intersect($this->protectedAppTypes, $types);
247
+        return !empty($protectedTypes);
248
+    }
249
+
250
+    /**
251
+     * Enable an app only for specific groups
252
+     *
253
+     * @param string $appId
254
+     * @param \OCP\IGroup[] $groups
255
+     * @throws \Exception if app can't be enabled for groups
256
+     */
257
+    public function enableAppForGroups($appId, $groups) {
258
+        $info = $this->getAppInfo($appId);
259
+        if (!empty($info['types'])) {
260
+            $protectedTypes = array_intersect($this->protectedAppTypes, $info['types']);
261
+            if (!empty($protectedTypes)) {
262
+                throw new \Exception("$appId can't be enabled for groups.");
263
+            }
264
+        }
265
+
266
+        $groupIds = array_map(function ($group) {
267
+            /** @var \OCP\IGroup $group */
268
+            return $group->getGID();
269
+        }, $groups);
270
+        $this->installedAppsCache[$appId] = json_encode($groupIds);
271
+        $this->appConfig->setValue($appId, 'enabled', json_encode($groupIds));
272
+        $this->dispatcher->dispatch(ManagerEvent::EVENT_APP_ENABLE_FOR_GROUPS, new ManagerEvent(
273
+            ManagerEvent::EVENT_APP_ENABLE_FOR_GROUPS, $appId, $groups
274
+        ));
275
+        $this->clearAppsCache();
276
+    }
277
+
278
+    /**
279
+     * Disable an app for every user
280
+     *
281
+     * @param string $appId
282
+     * @throws \Exception if app can't be disabled
283
+     */
284
+    public function disableApp($appId) {
285
+        if ($this->isAlwaysEnabled($appId)) {
286
+            throw new \Exception("$appId can't be disabled.");
287
+        }
288
+        unset($this->installedAppsCache[$appId]);
289
+        $this->appConfig->setValue($appId, 'enabled', 'no');
290
+        $this->dispatcher->dispatch(ManagerEvent::EVENT_APP_DISABLE, new ManagerEvent(
291
+            ManagerEvent::EVENT_APP_DISABLE, $appId
292
+        ));
293
+        $this->clearAppsCache();
294
+    }
295
+
296
+    /**
297
+     * Get the directory for the given app.
298
+     *
299
+     * @param string $appId
300
+     * @return string
301
+     * @throws AppPathNotFoundException if app folder can't be found
302
+     */
303
+    public function getAppPath($appId) {
304
+        $appPath = \OC_App::getAppPath($appId);
305
+        if($appPath === false) {
306
+            throw new AppPathNotFoundException('Could not find path for ' . $appId);
307
+        }
308
+        return $appPath;
309
+    }
310
+
311
+    /**
312
+     * Clear the cached list of apps when enabling/disabling an app
313
+     */
314
+    public function clearAppsCache() {
315
+        $settingsMemCache = $this->memCacheFactory->createDistributed('settings');
316
+        $settingsMemCache->clear('listApps');
317
+    }
318
+
319
+    /**
320
+     * Returns a list of apps that need upgrade
321
+     *
322
+     * @param string $version Nextcloud version as array of version components
323
+     * @return array list of app info from apps that need an upgrade
324
+     *
325
+     * @internal
326
+     */
327
+    public function getAppsNeedingUpgrade($version) {
328
+        $appsToUpgrade = [];
329
+        $apps = $this->getInstalledApps();
330
+        foreach ($apps as $appId) {
331
+            $appInfo = $this->getAppInfo($appId);
332
+            $appDbVersion = $this->appConfig->getValue($appId, 'installed_version');
333
+            if ($appDbVersion
334
+                && isset($appInfo['version'])
335
+                && version_compare($appInfo['version'], $appDbVersion, '>')
336
+                && \OC_App::isAppCompatible($version, $appInfo)
337
+            ) {
338
+                $appsToUpgrade[] = $appInfo;
339
+            }
340
+        }
341
+
342
+        return $appsToUpgrade;
343
+    }
344
+
345
+    /**
346
+     * Returns the app information from "appinfo/info.xml".
347
+     *
348
+     * @param string $appId app id
349
+     *
350
+     * @param bool $path
351
+     * @param null $lang
352
+     * @return array app info
353
+     */
354
+    public function getAppInfo(string $appId, bool $path = false, $lang = null) {
355
+        if ($path) {
356
+            $file = $appId;
357
+        } else {
358
+            if ($lang === null && isset($this->appInfos[$appId])) {
359
+                return $this->appInfos[$appId];
360
+            }
361
+            try {
362
+                $appPath = $this->getAppPath($appId);
363
+            } catch (AppPathNotFoundException $e) {
364
+                return null;
365
+            }
366
+            $file = $appPath . '/appinfo/info.xml';
367
+        }
368
+
369
+        $parser = new InfoParser($this->memCacheFactory->createLocal('core.appinfo'));
370
+        $data = $parser->parse($file);
371
+
372
+        if (is_array($data)) {
373
+            $data = \OC_App::parseAppInfo($data, $lang);
374
+        }
375
+
376
+        if ($lang === null) {
377
+            $this->appInfos[$appId] = $data;
378
+        }
379
+
380
+        return $data;
381
+    }
382
+
383
+    public function getAppVersion(string $appId, bool $useCache = true): string {
384
+        if(!$useCache || !isset($this->appVersions[$appId])) {
385
+            $appInfo = \OC::$server->getAppManager()->getAppInfo($appId);
386
+            $this->appVersions[$appId] = ($appInfo !== null && isset($appInfo['version'])) ? $appInfo['version'] : '0';
387
+        }
388
+        return $this->appVersions[$appId];
389
+    }
390
+
391
+    /**
392
+     * Returns a list of apps incompatible with the given version
393
+     *
394
+     * @param string $version Nextcloud version as array of version components
395
+     *
396
+     * @return array list of app info from incompatible apps
397
+     *
398
+     * @internal
399
+     */
400
+    public function getIncompatibleApps($version) {
401
+        $apps = $this->getInstalledApps();
402
+        $incompatibleApps = array();
403
+        foreach ($apps as $appId) {
404
+            $info = $this->getAppInfo($appId);
405
+            if (!\OC_App::isAppCompatible($version, $info)) {
406
+                $incompatibleApps[] = $info;
407
+            }
408
+        }
409
+        return $incompatibleApps;
410
+    }
411
+
412
+    /**
413
+     * @inheritdoc
414
+     * In case you change this method, also change \OC\App\CodeChecker\InfoChecker::isShipped()
415
+     */
416
+    public function isShipped($appId) {
417
+        $this->loadShippedJson();
418
+        return in_array($appId, $this->shippedApps, true);
419
+    }
420
+
421
+    private function isAlwaysEnabled($appId) {
422
+        $alwaysEnabled = $this->getAlwaysEnabledApps();
423
+        return in_array($appId, $alwaysEnabled, true);
424
+    }
425
+
426
+    /**
427
+     * In case you change this method, also change \OC\App\CodeChecker\InfoChecker::loadShippedJson()
428
+     * @throws \Exception
429
+     */
430
+    private function loadShippedJson() {
431
+        if ($this->shippedApps === null) {
432
+            $shippedJson = \OC::$SERVERROOT . '/core/shipped.json';
433
+            if (!file_exists($shippedJson)) {
434
+                throw new \Exception("File not found: $shippedJson");
435
+            }
436
+            $content = json_decode(file_get_contents($shippedJson), true);
437
+            $this->shippedApps = $content['shippedApps'];
438
+            $this->alwaysEnabled = $content['alwaysEnabled'];
439
+        }
440
+    }
441
+
442
+    /**
443
+     * @inheritdoc
444
+     */
445
+    public function getAlwaysEnabledApps() {
446
+        $this->loadShippedJson();
447
+        return $this->alwaysEnabled;
448
+    }
449 449
 }
Please login to merge, or discard this patch.
Spacing   +11 added lines, -11 removed lines patch added patch discarded remove patch
@@ -113,11 +113,11 @@  discard block
 block discarded – undo
113 113
 			$values = $this->appConfig->getValues(false, 'enabled');
114 114
 
115 115
 			$alwaysEnabledApps = $this->getAlwaysEnabledApps();
116
-			foreach($alwaysEnabledApps as $appId) {
116
+			foreach ($alwaysEnabledApps as $appId) {
117 117
 				$values[$appId] = 'yes';
118 118
 			}
119 119
 
120
-			$this->installedAppsCache = array_filter($values, function ($value) {
120
+			$this->installedAppsCache = array_filter($values, function($value) {
121 121
 				return $value !== 'no';
122 122
 			});
123 123
 			ksort($this->installedAppsCache);
@@ -142,7 +142,7 @@  discard block
 block discarded – undo
142 142
 	 */
143 143
 	public function getEnabledAppsForUser(IUser $user) {
144 144
 		$apps = $this->getInstalledAppsValues();
145
-		$appsForUser = array_filter($apps, function ($enabled) use ($user) {
145
+		$appsForUser = array_filter($apps, function($enabled) use ($user) {
146 146
 			return $this->checkAppForUser($enabled, $user);
147 147
 		});
148 148
 		return array_keys($appsForUser);
@@ -181,7 +181,7 @@  discard block
 block discarded – undo
181 181
 		} elseif ($user === null) {
182 182
 			return false;
183 183
 		} else {
184
-			if(empty($enabled)){
184
+			if (empty($enabled)) {
185 185
 				return false;
186 186
 			}
187 187
 
@@ -189,7 +189,7 @@  discard block
 block discarded – undo
189 189
 
190 190
 			if (!is_array($groupIds)) {
191 191
 				$jsonError = json_last_error();
192
-				\OC::$server->getLogger()->warning('AppManger::checkAppForUser - can\'t decode group IDs: ' . print_r($enabled, true) . ' - json error code: ' . $jsonError, ['app' => 'lib']);
192
+				\OC::$server->getLogger()->warning('AppManger::checkAppForUser - can\'t decode group IDs: '.print_r($enabled, true).' - json error code: '.$jsonError, ['app' => 'lib']);
193 193
 				return false;
194 194
 			}
195 195
 
@@ -263,7 +263,7 @@  discard block
 block discarded – undo
263 263
 			}
264 264
 		}
265 265
 
266
-		$groupIds = array_map(function ($group) {
266
+		$groupIds = array_map(function($group) {
267 267
 			/** @var \OCP\IGroup $group */
268 268
 			return $group->getGID();
269 269
 		}, $groups);
@@ -302,8 +302,8 @@  discard block
 block discarded – undo
302 302
 	 */
303 303
 	public function getAppPath($appId) {
304 304
 		$appPath = \OC_App::getAppPath($appId);
305
-		if($appPath === false) {
306
-			throw new AppPathNotFoundException('Could not find path for ' . $appId);
305
+		if ($appPath === false) {
306
+			throw new AppPathNotFoundException('Could not find path for '.$appId);
307 307
 		}
308 308
 		return $appPath;
309 309
 	}
@@ -363,7 +363,7 @@  discard block
 block discarded – undo
363 363
 			} catch (AppPathNotFoundException $e) {
364 364
 				return null;
365 365
 			}
366
-			$file = $appPath . '/appinfo/info.xml';
366
+			$file = $appPath.'/appinfo/info.xml';
367 367
 		}
368 368
 
369 369
 		$parser = new InfoParser($this->memCacheFactory->createLocal('core.appinfo'));
@@ -381,7 +381,7 @@  discard block
 block discarded – undo
381 381
 	}
382 382
 
383 383
 	public function getAppVersion(string $appId, bool $useCache = true): string {
384
-		if(!$useCache || !isset($this->appVersions[$appId])) {
384
+		if (!$useCache || !isset($this->appVersions[$appId])) {
385 385
 			$appInfo = \OC::$server->getAppManager()->getAppInfo($appId);
386 386
 			$this->appVersions[$appId] = ($appInfo !== null && isset($appInfo['version'])) ? $appInfo['version'] : '0';
387 387
 		}
@@ -429,7 +429,7 @@  discard block
 block discarded – undo
429 429
 	 */
430 430
 	private function loadShippedJson() {
431 431
 		if ($this->shippedApps === null) {
432
-			$shippedJson = \OC::$SERVERROOT . '/core/shipped.json';
432
+			$shippedJson = \OC::$SERVERROOT.'/core/shipped.json';
433 433
 			if (!file_exists($shippedJson)) {
434 434
 				throw new \Exception("File not found: $shippedJson");
435 435
 			}
Please login to merge, or discard this patch.
lib/public/App/IAppManager.php 1 patch
Indentation   +118 added lines, -118 removed lines patch added patch discarded remove patch
@@ -37,122 +37,122 @@
 block discarded – undo
37 37
  */
38 38
 interface IAppManager {
39 39
 
40
-	/**
41
-	 * Returns the app information from "appinfo/info.xml".
42
-	 *
43
-	 * @param string $appId
44
-	 * @return mixed
45
-	 * @since 14.0.0
46
-	 */
47
-	public function getAppInfo(string $appId, bool $path = false, $lang = null);
48
-
49
-	/**
50
-	 * Returns the app information from "appinfo/info.xml".
51
-	 *
52
-	 * @param string $appId
53
-	 * @param bool $useCache
54
-	 * @return string
55
-	 * @since 14.0.0
56
-	 */
57
-	public function getAppVersion(string $appId, bool $useCache = true): string;
58
-
59
-	/**
60
-	 * Check if an app is enabled for user
61
-	 *
62
-	 * @param string $appId
63
-	 * @param \OCP\IUser $user (optional) if not defined, the currently loggedin user will be used
64
-	 * @return bool
65
-	 * @since 8.0.0
66
-	 */
67
-	public function isEnabledForUser($appId, $user = null);
68
-
69
-	/**
70
-	 * Check if an app is installed in the instance
71
-	 *
72
-	 * @param string $appId
73
-	 * @return bool
74
-	 * @since 8.0.0
75
-	 */
76
-	public function isInstalled($appId);
77
-
78
-	/**
79
-	 * Enable an app for every user
80
-	 *
81
-	 * @param string $appId
82
-	 * @throws AppPathNotFoundException
83
-	 * @since 8.0.0
84
-	 */
85
-	public function enableApp($appId);
86
-
87
-	/**
88
-	 * Whether a list of types contains a protected app type
89
-	 *
90
-	 * @param string[] $types
91
-	 * @return bool
92
-	 * @since 12.0.0
93
-	 */
94
-	public function hasProtectedAppType($types);
95
-
96
-	/**
97
-	 * Enable an app only for specific groups
98
-	 *
99
-	 * @param string $appId
100
-	 * @param \OCP\IGroup[] $groups
101
-	 * @since 8.0.0
102
-	 */
103
-	public function enableAppForGroups($appId, $groups);
104
-
105
-	/**
106
-	 * Disable an app for every user
107
-	 *
108
-	 * @param string $appId
109
-	 * @since 8.0.0
110
-	 */
111
-	public function disableApp($appId);
112
-
113
-	/**
114
-	 * Get the directory for the given app.
115
-	 *
116
-	 * @param string $appId
117
-	 * @return string
118
-	 * @since 11.0.0
119
-	 * @throws AppPathNotFoundException
120
-	 */
121
-	public function getAppPath($appId);
122
-
123
-	/**
124
-	 * List all apps enabled for a user
125
-	 *
126
-	 * @param \OCP\IUser $user
127
-	 * @return string[]
128
-	 * @since 8.1.0
129
-	 */
130
-	public function getEnabledAppsForUser(IUser $user);
131
-
132
-	/**
133
-	 * List all installed apps
134
-	 *
135
-	 * @return string[]
136
-	 * @since 8.1.0
137
-	 */
138
-	public function getInstalledApps();
139
-
140
-	/**
141
-	 * Clear the cached list of apps when enabling/disabling an app
142
-	 * @since 8.1.0
143
-	 */
144
-	public function clearAppsCache();
145
-
146
-	/**
147
-	 * @param string $appId
148
-	 * @return boolean
149
-	 * @since 9.0.0
150
-	 */
151
-	public function isShipped($appId);
152
-
153
-	/**
154
-	 * @return string[]
155
-	 * @since 9.0.0
156
-	 */
157
-	public function getAlwaysEnabledApps();
40
+    /**
41
+     * Returns the app information from "appinfo/info.xml".
42
+     *
43
+     * @param string $appId
44
+     * @return mixed
45
+     * @since 14.0.0
46
+     */
47
+    public function getAppInfo(string $appId, bool $path = false, $lang = null);
48
+
49
+    /**
50
+     * Returns the app information from "appinfo/info.xml".
51
+     *
52
+     * @param string $appId
53
+     * @param bool $useCache
54
+     * @return string
55
+     * @since 14.0.0
56
+     */
57
+    public function getAppVersion(string $appId, bool $useCache = true): string;
58
+
59
+    /**
60
+     * Check if an app is enabled for user
61
+     *
62
+     * @param string $appId
63
+     * @param \OCP\IUser $user (optional) if not defined, the currently loggedin user will be used
64
+     * @return bool
65
+     * @since 8.0.0
66
+     */
67
+    public function isEnabledForUser($appId, $user = null);
68
+
69
+    /**
70
+     * Check if an app is installed in the instance
71
+     *
72
+     * @param string $appId
73
+     * @return bool
74
+     * @since 8.0.0
75
+     */
76
+    public function isInstalled($appId);
77
+
78
+    /**
79
+     * Enable an app for every user
80
+     *
81
+     * @param string $appId
82
+     * @throws AppPathNotFoundException
83
+     * @since 8.0.0
84
+     */
85
+    public function enableApp($appId);
86
+
87
+    /**
88
+     * Whether a list of types contains a protected app type
89
+     *
90
+     * @param string[] $types
91
+     * @return bool
92
+     * @since 12.0.0
93
+     */
94
+    public function hasProtectedAppType($types);
95
+
96
+    /**
97
+     * Enable an app only for specific groups
98
+     *
99
+     * @param string $appId
100
+     * @param \OCP\IGroup[] $groups
101
+     * @since 8.0.0
102
+     */
103
+    public function enableAppForGroups($appId, $groups);
104
+
105
+    /**
106
+     * Disable an app for every user
107
+     *
108
+     * @param string $appId
109
+     * @since 8.0.0
110
+     */
111
+    public function disableApp($appId);
112
+
113
+    /**
114
+     * Get the directory for the given app.
115
+     *
116
+     * @param string $appId
117
+     * @return string
118
+     * @since 11.0.0
119
+     * @throws AppPathNotFoundException
120
+     */
121
+    public function getAppPath($appId);
122
+
123
+    /**
124
+     * List all apps enabled for a user
125
+     *
126
+     * @param \OCP\IUser $user
127
+     * @return string[]
128
+     * @since 8.1.0
129
+     */
130
+    public function getEnabledAppsForUser(IUser $user);
131
+
132
+    /**
133
+     * List all installed apps
134
+     *
135
+     * @return string[]
136
+     * @since 8.1.0
137
+     */
138
+    public function getInstalledApps();
139
+
140
+    /**
141
+     * Clear the cached list of apps when enabling/disabling an app
142
+     * @since 8.1.0
143
+     */
144
+    public function clearAppsCache();
145
+
146
+    /**
147
+     * @param string $appId
148
+     * @return boolean
149
+     * @since 9.0.0
150
+     */
151
+    public function isShipped($appId);
152
+
153
+    /**
154
+     * @return string[]
155
+     * @since 9.0.0
156
+     */
157
+    public function getAlwaysEnabledApps();
158 158
 }
Please login to merge, or discard this patch.
settings/ajax/enableapp.php 2 patches
Indentation   +18 added lines, -18 removed lines patch added patch discarded remove patch
@@ -29,30 +29,30 @@
 block discarded – undo
29 29
 
30 30
 $lastConfirm = (int) \OC::$server->getSession()->get('last-password-confirm');
31 31
 if ($lastConfirm < (time() - 30 * 60 + 15)) { // allow 15 seconds delay
32
-	$l = \OC::$server->getL10N('core');
33
-	OC_JSON::error(array( 'data' => array( 'message' => $l->t('Password confirmation is required'))));
34
-	exit();
32
+    $l = \OC::$server->getL10N('core');
33
+    OC_JSON::error(array( 'data' => array( 'message' => $l->t('Password confirmation is required'))));
34
+    exit();
35 35
 }
36 36
 
37 37
 $groups = isset($_POST['groups']) ? (array)$_POST['groups'] : [];
38 38
 $appIds = isset($_POST['appIds']) ? (array)$_POST['appIds'] : [];
39 39
 
40 40
 try {
41
-	$updateRequired = false;
42
-	foreach($appIds as $appId) {
43
-		$app = new OC_App();
44
-		$appId = OC_App::cleanAppId($appId);
45
-		$app->enable($appId, $groups);
46
-		if(\OC_App::shouldUpgrade($appId)) {
47
-			$updateRequired = true;
48
-		}
49
-	}
41
+    $updateRequired = false;
42
+    foreach($appIds as $appId) {
43
+        $app = new OC_App();
44
+        $appId = OC_App::cleanAppId($appId);
45
+        $app->enable($appId, $groups);
46
+        if(\OC_App::shouldUpgrade($appId)) {
47
+            $updateRequired = true;
48
+        }
49
+    }
50 50
 
51
-	OC_JSON::success(['data' => ['update_required' => $updateRequired]]);
51
+    OC_JSON::success(['data' => ['update_required' => $updateRequired]]);
52 52
 } catch (Exception $e) {
53
-	\OC::$server->getLogger()->logException($e, [
54
-		'level' => \OCP\Util::DEBUG,
55
-		'app' => 'core',
56
-	]);
57
-	OC_JSON::error(array("data" => array("message" => $e->getMessage()) ));
53
+    \OC::$server->getLogger()->logException($e, [
54
+        'level' => \OCP\Util::DEBUG,
55
+        'app' => 'core',
56
+    ]);
57
+    OC_JSON::error(array("data" => array("message" => $e->getMessage()) ));
58 58
 }
Please login to merge, or discard this patch.
Spacing   +6 added lines, -6 removed lines patch added patch discarded remove patch
@@ -30,20 +30,20 @@  discard block
 block discarded – undo
30 30
 $lastConfirm = (int) \OC::$server->getSession()->get('last-password-confirm');
31 31
 if ($lastConfirm < (time() - 30 * 60 + 15)) { // allow 15 seconds delay
32 32
 	$l = \OC::$server->getL10N('core');
33
-	OC_JSON::error(array( 'data' => array( 'message' => $l->t('Password confirmation is required'))));
33
+	OC_JSON::error(array('data' => array('message' => $l->t('Password confirmation is required'))));
34 34
 	exit();
35 35
 }
36 36
 
37
-$groups = isset($_POST['groups']) ? (array)$_POST['groups'] : [];
38
-$appIds = isset($_POST['appIds']) ? (array)$_POST['appIds'] : [];
37
+$groups = isset($_POST['groups']) ? (array) $_POST['groups'] : [];
38
+$appIds = isset($_POST['appIds']) ? (array) $_POST['appIds'] : [];
39 39
 
40 40
 try {
41 41
 	$updateRequired = false;
42
-	foreach($appIds as $appId) {
42
+	foreach ($appIds as $appId) {
43 43
 		$app = new OC_App();
44 44
 		$appId = OC_App::cleanAppId($appId);
45 45
 		$app->enable($appId, $groups);
46
-		if(\OC_App::shouldUpgrade($appId)) {
46
+		if (\OC_App::shouldUpgrade($appId)) {
47 47
 			$updateRequired = true;
48 48
 		}
49 49
 	}
@@ -54,5 +54,5 @@  discard block
 block discarded – undo
54 54
 		'level' => \OCP\Util::DEBUG,
55 55
 		'app' => 'core',
56 56
 	]);
57
-	OC_JSON::error(array("data" => array("message" => $e->getMessage()) ));
57
+	OC_JSON::error(array("data" => array("message" => $e->getMessage())));
58 58
 }
Please login to merge, or discard this patch.