Completed
Pull Request — master (#8097)
by Morris
19:14
created
lib/private/legacy/app.php 3 patches
Doc Comments   +5 added lines patch added patch discarded remove patch
@@ -1047,6 +1047,11 @@
 block discarded – undo
1047 1047
 		}
1048 1048
 	}
1049 1049
 
1050
+	/**
1051
+	 * @param string $lang
1052
+	 *
1053
+	 * @return string
1054
+	 */
1050 1055
 	protected static function findBestL10NOption($options, $lang) {
1051 1056
 		$fallback = $similarLangFallback = $englishFallback = false;
1052 1057
 
Please login to merge, or discard this patch.
Indentation   +1093 added lines, -1093 removed lines patch added patch discarded remove patch
@@ -62,1097 +62,1097 @@
 block discarded – undo
62 62
  * upgrading and removing apps.
63 63
  */
64 64
 class OC_App {
65
-	static private $adminForms = array();
66
-	static private $personalForms = array();
67
-	static private $appTypes = array();
68
-	static private $loadedApps = array();
69
-	static private $altLogin = array();
70
-	static private $alreadyRegistered = [];
71
-	const officialApp = 200;
72
-
73
-	/**
74
-	 * clean the appId
75
-	 *
76
-	 * @param string|boolean $app AppId that needs to be cleaned
77
-	 * @return string
78
-	 */
79
-	public static function cleanAppId($app) {
80
-		return str_replace(array('\0', '/', '\\', '..'), '', $app);
81
-	}
82
-
83
-	/**
84
-	 * Check if an app is loaded
85
-	 *
86
-	 * @param string $app
87
-	 * @return bool
88
-	 */
89
-	public static function isAppLoaded($app) {
90
-		return in_array($app, self::$loadedApps, true);
91
-	}
92
-
93
-	/**
94
-	 * loads all apps
95
-	 *
96
-	 * @param string[] | string | null $types
97
-	 * @return bool
98
-	 *
99
-	 * This function walks through the ownCloud directory and loads all apps
100
-	 * it can find. A directory contains an app if the file /appinfo/info.xml
101
-	 * exists.
102
-	 *
103
-	 * if $types is set, only apps of those types will be loaded
104
-	 */
105
-	public static function loadApps($types = null) {
106
-		if (\OC::$server->getSystemConfig()->getValue('maintenance', false)) {
107
-			return false;
108
-		}
109
-		// Load the enabled apps here
110
-		$apps = self::getEnabledApps();
111
-
112
-		// Add each apps' folder as allowed class path
113
-		foreach($apps as $app) {
114
-			$path = self::getAppPath($app);
115
-			if($path !== false) {
116
-				self::registerAutoloading($app, $path);
117
-			}
118
-		}
119
-
120
-		// prevent app.php from printing output
121
-		ob_start();
122
-		foreach ($apps as $app) {
123
-			if ((is_null($types) or self::isType($app, $types)) && !in_array($app, self::$loadedApps)) {
124
-				self::loadApp($app);
125
-			}
126
-		}
127
-		ob_end_clean();
128
-
129
-		return true;
130
-	}
131
-
132
-	/**
133
-	 * load a single app
134
-	 *
135
-	 * @param string $app
136
-	 */
137
-	public static function loadApp($app) {
138
-		self::$loadedApps[] = $app;
139
-		$appPath = self::getAppPath($app);
140
-		if($appPath === false) {
141
-			return;
142
-		}
143
-
144
-		// in case someone calls loadApp() directly
145
-		self::registerAutoloading($app, $appPath);
146
-
147
-		if (is_file($appPath . '/appinfo/app.php')) {
148
-			\OC::$server->getEventLogger()->start('load_app_' . $app, 'Load app: ' . $app);
149
-			self::requireAppFile($app);
150
-			if (self::isType($app, array('authentication'))) {
151
-				// since authentication apps affect the "is app enabled for group" check,
152
-				// the enabled apps cache needs to be cleared to make sure that the
153
-				// next time getEnableApps() is called it will also include apps that were
154
-				// enabled for groups
155
-				self::$enabledAppsCache = array();
156
-			}
157
-			\OC::$server->getEventLogger()->end('load_app_' . $app);
158
-		}
159
-
160
-		$info = self::getAppInfo($app);
161
-		if (!empty($info['activity']['filters'])) {
162
-			foreach ($info['activity']['filters'] as $filter) {
163
-				\OC::$server->getActivityManager()->registerFilter($filter);
164
-			}
165
-		}
166
-		if (!empty($info['activity']['settings'])) {
167
-			foreach ($info['activity']['settings'] as $setting) {
168
-				\OC::$server->getActivityManager()->registerSetting($setting);
169
-			}
170
-		}
171
-		if (!empty($info['activity']['providers'])) {
172
-			foreach ($info['activity']['providers'] as $provider) {
173
-				\OC::$server->getActivityManager()->registerProvider($provider);
174
-			}
175
-		}
176
-
177
-		if (!empty($info['settings']['admin'])) {
178
-			foreach ($info['settings']['admin'] as $setting) {
179
-				\OC::$server->getSettingsManager()->registerSetting('admin', $setting);
180
-			}
181
-		}
182
-		if (!empty($info['settings']['admin-section'])) {
183
-			foreach ($info['settings']['admin-section'] as $section) {
184
-				\OC::$server->getSettingsManager()->registerSection('admin', $section);
185
-			}
186
-		}
187
-		if (!empty($info['settings']['personal'])) {
188
-			foreach ($info['settings']['personal'] as $setting) {
189
-				\OC::$server->getSettingsManager()->registerSetting('personal', $setting);
190
-			}
191
-		}
192
-		if (!empty($info['settings']['personal-section'])) {
193
-			foreach ($info['settings']['personal-section'] as $section) {
194
-				\OC::$server->getSettingsManager()->registerSection('personal', $section);
195
-			}
196
-		}
197
-
198
-		if (!empty($info['collaboration']['plugins'])) {
199
-			// deal with one or many plugin entries
200
-			$plugins = isset($info['collaboration']['plugins']['plugin']['@value']) ?
201
-				[$info['collaboration']['plugins']['plugin']] : $info['collaboration']['plugins']['plugin'];
202
-			foreach ($plugins as $plugin) {
203
-				if($plugin['@attributes']['type'] === 'collaborator-search') {
204
-					$pluginInfo = [
205
-						'shareType' => $plugin['@attributes']['share-type'],
206
-						'class' => $plugin['@value'],
207
-					];
208
-					\OC::$server->getCollaboratorSearch()->registerPlugin($pluginInfo);
209
-				} else if ($plugin['@attributes']['type'] === 'autocomplete-sort') {
210
-					\OC::$server->getAutoCompleteManager()->registerSorter($plugin['@value']);
211
-				}
212
-			}
213
-		}
214
-	}
215
-
216
-	/**
217
-	 * @internal
218
-	 * @param string $app
219
-	 * @param string $path
220
-	 */
221
-	public static function registerAutoloading($app, $path) {
222
-		$key = $app . '-' . $path;
223
-		if(isset(self::$alreadyRegistered[$key])) {
224
-			return;
225
-		}
226
-
227
-		self::$alreadyRegistered[$key] = true;
228
-
229
-		// Register on PSR-4 composer autoloader
230
-		$appNamespace = \OC\AppFramework\App::buildAppNamespace($app);
231
-		\OC::$server->registerNamespace($app, $appNamespace);
232
-
233
-		if (file_exists($path . '/composer/autoload.php')) {
234
-			require_once $path . '/composer/autoload.php';
235
-		} else {
236
-			\OC::$composerAutoloader->addPsr4($appNamespace . '\\', $path . '/lib/', true);
237
-			// Register on legacy autoloader
238
-			\OC::$loader->addValidRoot($path);
239
-		}
240
-
241
-		// Register Test namespace only when testing
242
-		if (defined('PHPUNIT_RUN') || defined('CLI_TEST_RUN')) {
243
-			\OC::$composerAutoloader->addPsr4($appNamespace . '\\Tests\\', $path . '/tests/', true);
244
-		}
245
-	}
246
-
247
-	/**
248
-	 * Load app.php from the given app
249
-	 *
250
-	 * @param string $app app name
251
-	 */
252
-	private static function requireAppFile($app) {
253
-		try {
254
-			// encapsulated here to avoid variable scope conflicts
255
-			require_once $app . '/appinfo/app.php';
256
-		} catch (Error $ex) {
257
-			\OC::$server->getLogger()->logException($ex);
258
-			if (!\OC::$server->getAppManager()->isShipped($app)) {
259
-				// Only disable apps which are not shipped
260
-				self::disable($app);
261
-			}
262
-		}
263
-	}
264
-
265
-	/**
266
-	 * check if an app is of a specific type
267
-	 *
268
-	 * @param string $app
269
-	 * @param string|array $types
270
-	 * @return bool
271
-	 */
272
-	public static function isType($app, $types) {
273
-		if (is_string($types)) {
274
-			$types = array($types);
275
-		}
276
-		$appTypes = self::getAppTypes($app);
277
-		foreach ($types as $type) {
278
-			if (array_search($type, $appTypes) !== false) {
279
-				return true;
280
-			}
281
-		}
282
-		return false;
283
-	}
284
-
285
-	/**
286
-	 * get the types of an app
287
-	 *
288
-	 * @param string $app
289
-	 * @return array
290
-	 */
291
-	private static function getAppTypes($app) {
292
-		//load the cache
293
-		if (count(self::$appTypes) == 0) {
294
-			self::$appTypes = \OC::$server->getAppConfig()->getValues(false, 'types');
295
-		}
296
-
297
-		if (isset(self::$appTypes[$app])) {
298
-			return explode(',', self::$appTypes[$app]);
299
-		} else {
300
-			return array();
301
-		}
302
-	}
303
-
304
-	/**
305
-	 * read app types from info.xml and cache them in the database
306
-	 */
307
-	public static function setAppTypes($app) {
308
-		$appData = self::getAppInfo($app);
309
-		if(!is_array($appData)) {
310
-			return;
311
-		}
312
-
313
-		if (isset($appData['types'])) {
314
-			$appTypes = implode(',', $appData['types']);
315
-		} else {
316
-			$appTypes = '';
317
-			$appData['types'] = [];
318
-		}
319
-
320
-		\OC::$server->getConfig()->setAppValue($app, 'types', $appTypes);
321
-
322
-		if (\OC::$server->getAppManager()->hasProtectedAppType($appData['types'])) {
323
-			$enabled = \OC::$server->getConfig()->getAppValue($app, 'enabled', 'yes');
324
-			if ($enabled !== 'yes' && $enabled !== 'no') {
325
-				\OC::$server->getConfig()->setAppValue($app, 'enabled', 'yes');
326
-			}
327
-		}
328
-	}
329
-
330
-	/**
331
-	 * get all enabled apps
332
-	 */
333
-	protected static $enabledAppsCache = array();
334
-
335
-	/**
336
-	 * Returns apps enabled for the current user.
337
-	 *
338
-	 * @param bool $forceRefresh whether to refresh the cache
339
-	 * @param bool $all whether to return apps for all users, not only the
340
-	 * currently logged in one
341
-	 * @return string[]
342
-	 */
343
-	public static function getEnabledApps($forceRefresh = false, $all = false) {
344
-		if (!\OC::$server->getSystemConfig()->getValue('installed', false)) {
345
-			return array();
346
-		}
347
-		// in incognito mode or when logged out, $user will be false,
348
-		// which is also the case during an upgrade
349
-		$appManager = \OC::$server->getAppManager();
350
-		if ($all) {
351
-			$user = null;
352
-		} else {
353
-			$user = \OC::$server->getUserSession()->getUser();
354
-		}
355
-
356
-		if (is_null($user)) {
357
-			$apps = $appManager->getInstalledApps();
358
-		} else {
359
-			$apps = $appManager->getEnabledAppsForUser($user);
360
-		}
361
-		$apps = array_filter($apps, function ($app) {
362
-			return $app !== 'files';//we add this manually
363
-		});
364
-		sort($apps);
365
-		array_unshift($apps, 'files');
366
-		return $apps;
367
-	}
368
-
369
-	/**
370
-	 * checks whether or not an app is enabled
371
-	 *
372
-	 * @param string $app app
373
-	 * @return bool
374
-	 * @deprecated 13.0.0 use \OC::$server->getAppManager()->isEnabledForUser($appId)
375
-	 *
376
-	 * This function checks whether or not an app is enabled.
377
-	 */
378
-	public static function isEnabled($app) {
379
-		return \OC::$server->getAppManager()->isEnabledForUser($app);
380
-	}
381
-
382
-	/**
383
-	 * enables an app
384
-	 *
385
-	 * @param string $appId
386
-	 * @param array $groups (optional) when set, only these groups will have access to the app
387
-	 * @throws \Exception
388
-	 * @return void
389
-	 *
390
-	 * This function set an app as enabled in appconfig.
391
-	 */
392
-	public function enable($appId,
393
-						   $groups = null) {
394
-		self::$enabledAppsCache = []; // flush
395
-
396
-		// Check if app is already downloaded
397
-		$installer = \OC::$server->query(Installer::class);
398
-		$isDownloaded = $installer->isDownloaded($appId);
399
-
400
-		if(!$isDownloaded) {
401
-			$installer->downloadApp($appId);
402
-		}
403
-
404
-		$installer->installApp($appId);
405
-
406
-		$appManager = \OC::$server->getAppManager();
407
-		if (!is_null($groups)) {
408
-			$groupManager = \OC::$server->getGroupManager();
409
-			$groupsList = [];
410
-			foreach ($groups as $group) {
411
-				$groupItem = $groupManager->get($group);
412
-				if ($groupItem instanceof \OCP\IGroup) {
413
-					$groupsList[] = $groupManager->get($group);
414
-				}
415
-			}
416
-			$appManager->enableAppForGroups($appId, $groupsList);
417
-		} else {
418
-			$appManager->enableApp($appId);
419
-		}
420
-	}
421
-
422
-	/**
423
-	 * This function set an app as disabled in appconfig.
424
-	 *
425
-	 * @param string $app app
426
-	 * @throws Exception
427
-	 */
428
-	public static function disable($app) {
429
-		// flush
430
-		self::$enabledAppsCache = array();
431
-
432
-		// run uninstall steps
433
-		$appData = OC_App::getAppInfo($app);
434
-		if (!is_null($appData)) {
435
-			OC_App::executeRepairSteps($app, $appData['repair-steps']['uninstall']);
436
-		}
437
-
438
-		// emit disable hook - needed anymore ?
439
-		\OC_Hook::emit('OC_App', 'pre_disable', array('app' => $app));
440
-
441
-		// finally disable it
442
-		$appManager = \OC::$server->getAppManager();
443
-		$appManager->disableApp($app);
444
-	}
445
-
446
-	// This is private as well. It simply works, so don't ask for more details
447
-	private static function proceedNavigation($list) {
448
-		usort($list, function($a, $b) {
449
-			if (isset($a['order']) && isset($b['order'])) {
450
-				return ($a['order'] < $b['order']) ? -1 : 1;
451
-			} else if (isset($a['order']) || isset($b['order'])) {
452
-				return isset($a['order']) ? -1 : 1;
453
-			} else {
454
-				return ($a['name'] < $b['name']) ? -1 : 1;
455
-			}
456
-		});
457
-
458
-		$activeApp = OC::$server->getNavigationManager()->getActiveEntry();
459
-		foreach ($list as $index => &$navEntry) {
460
-			if ($navEntry['id'] == $activeApp) {
461
-				$navEntry['active'] = true;
462
-			} else {
463
-				$navEntry['active'] = false;
464
-			}
465
-		}
466
-		unset($navEntry);
467
-
468
-		return $list;
469
-	}
470
-
471
-	/**
472
-	 * Get the path where to install apps
473
-	 *
474
-	 * @return string|false
475
-	 */
476
-	public static function getInstallPath() {
477
-		if (\OC::$server->getSystemConfig()->getValue('appstoreenabled', true) == false) {
478
-			return false;
479
-		}
480
-
481
-		foreach (OC::$APPSROOTS as $dir) {
482
-			if (isset($dir['writable']) && $dir['writable'] === true) {
483
-				return $dir['path'];
484
-			}
485
-		}
486
-
487
-		\OCP\Util::writeLog('core', 'No application directories are marked as writable.', \OCP\Util::ERROR);
488
-		return null;
489
-	}
490
-
491
-
492
-	/**
493
-	 * search for an app in all app-directories
494
-	 *
495
-	 * @param string $appId
496
-	 * @return false|string
497
-	 */
498
-	public static function findAppInDirectories($appId) {
499
-		$sanitizedAppId = self::cleanAppId($appId);
500
-		if($sanitizedAppId !== $appId) {
501
-			return false;
502
-		}
503
-		static $app_dir = array();
504
-
505
-		if (isset($app_dir[$appId])) {
506
-			return $app_dir[$appId];
507
-		}
508
-
509
-		$possibleApps = array();
510
-		foreach (OC::$APPSROOTS as $dir) {
511
-			if (file_exists($dir['path'] . '/' . $appId)) {
512
-				$possibleApps[] = $dir;
513
-			}
514
-		}
515
-
516
-		if (empty($possibleApps)) {
517
-			return false;
518
-		} elseif (count($possibleApps) === 1) {
519
-			$dir = array_shift($possibleApps);
520
-			$app_dir[$appId] = $dir;
521
-			return $dir;
522
-		} else {
523
-			$versionToLoad = array();
524
-			foreach ($possibleApps as $possibleApp) {
525
-				$version = self::getAppVersionByPath($possibleApp['path']);
526
-				if (empty($versionToLoad) || version_compare($version, $versionToLoad['version'], '>')) {
527
-					$versionToLoad = array(
528
-						'dir' => $possibleApp,
529
-						'version' => $version,
530
-					);
531
-				}
532
-			}
533
-			$app_dir[$appId] = $versionToLoad['dir'];
534
-			return $versionToLoad['dir'];
535
-			//TODO - write test
536
-		}
537
-	}
538
-
539
-	/**
540
-	 * Get the directory for the given app.
541
-	 * If the app is defined in multiple directories, the first one is taken. (false if not found)
542
-	 *
543
-	 * @param string $appId
544
-	 * @return string|false
545
-	 */
546
-	public static function getAppPath($appId) {
547
-		if ($appId === null || trim($appId) === '') {
548
-			return false;
549
-		}
550
-
551
-		if (($dir = self::findAppInDirectories($appId)) != false) {
552
-			return $dir['path'] . '/' . $appId;
553
-		}
554
-		return false;
555
-	}
556
-
557
-	/**
558
-	 * Get the path for the given app on the access
559
-	 * If the app is defined in multiple directories, the first one is taken. (false if not found)
560
-	 *
561
-	 * @param string $appId
562
-	 * @return string|false
563
-	 */
564
-	public static function getAppWebPath($appId) {
565
-		if (($dir = self::findAppInDirectories($appId)) != false) {
566
-			return OC::$WEBROOT . $dir['url'] . '/' . $appId;
567
-		}
568
-		return false;
569
-	}
570
-
571
-	/**
572
-	 * get the last version of the app from appinfo/info.xml
573
-	 *
574
-	 * @param string $appId
575
-	 * @param bool $useCache
576
-	 * @return string
577
-	 * @deprecated 14.0.0 use \OC::$server->getAppManager()->getAppVersion()
578
-	 */
579
-	public static function getAppVersion($appId, $useCache = true) {
580
-		return \OC::$server->getAppManager()->getAppVersion($appId, $useCache);
581
-	}
582
-
583
-	/**
584
-	 * get app's version based on it's path
585
-	 *
586
-	 * @param string $path
587
-	 * @return string
588
-	 */
589
-	public static function getAppVersionByPath($path) {
590
-		$infoFile = $path . '/appinfo/info.xml';
591
-		$appData = \OC::$server->getAppManager()->getAppInfo($infoFile, true);
592
-		return isset($appData['version']) ? $appData['version'] : '';
593
-	}
594
-
595
-
596
-	/**
597
-	 * Read all app metadata from the info.xml file
598
-	 *
599
-	 * @param string $appId id of the app or the path of the info.xml file
600
-	 * @param bool $path
601
-	 * @param string $lang
602
-	 * @return array|null
603
-	 * @note all data is read from info.xml, not just pre-defined fields
604
-	 * @deprecated 14.0.0 use \OC::$server->getAppManager()->getAppInfo()
605
-	 */
606
-	public static function getAppInfo($appId, $path = false, $lang = null) {
607
-		return \OC::$server->getAppManager()->getAppInfo($appId, $path, $lang);
608
-	}
609
-
610
-	/**
611
-	 * Returns the navigation
612
-	 *
613
-	 * @return array
614
-	 *
615
-	 * This function returns an array containing all entries added. The
616
-	 * entries are sorted by the key 'order' ascending. Additional to the keys
617
-	 * given for each app the following keys exist:
618
-	 *   - active: boolean, signals if the user is on this navigation entry
619
-	 */
620
-	public static function getNavigation() {
621
-		$entries = OC::$server->getNavigationManager()->getAll();
622
-		return self::proceedNavigation($entries);
623
-	}
624
-
625
-	/**
626
-	 * Returns the Settings Navigation
627
-	 *
628
-	 * @return string[]
629
-	 *
630
-	 * This function returns an array containing all settings pages added. The
631
-	 * entries are sorted by the key 'order' ascending.
632
-	 */
633
-	public static function getSettingsNavigation() {
634
-		$entries = OC::$server->getNavigationManager()->getAll('settings');
635
-		return self::proceedNavigation($entries);
636
-	}
637
-
638
-	/**
639
-	 * get the id of loaded app
640
-	 *
641
-	 * @return string
642
-	 */
643
-	public static function getCurrentApp() {
644
-		$request = \OC::$server->getRequest();
645
-		$script = substr($request->getScriptName(), strlen(OC::$WEBROOT) + 1);
646
-		$topFolder = substr($script, 0, strpos($script, '/') ?: 0);
647
-		if (empty($topFolder)) {
648
-			$path_info = $request->getPathInfo();
649
-			if ($path_info) {
650
-				$topFolder = substr($path_info, 1, strpos($path_info, '/', 1) - 1);
651
-			}
652
-		}
653
-		if ($topFolder == 'apps') {
654
-			$length = strlen($topFolder);
655
-			return substr($script, $length + 1, strpos($script, '/', $length + 1) - $length - 1);
656
-		} else {
657
-			return $topFolder;
658
-		}
659
-	}
660
-
661
-	/**
662
-	 * @param string $type
663
-	 * @return array
664
-	 */
665
-	public static function getForms($type) {
666
-		$forms = array();
667
-		switch ($type) {
668
-			case 'admin':
669
-				$source = self::$adminForms;
670
-				break;
671
-			case 'personal':
672
-				$source = self::$personalForms;
673
-				break;
674
-			default:
675
-				return array();
676
-		}
677
-		foreach ($source as $form) {
678
-			$forms[] = include $form;
679
-		}
680
-		return $forms;
681
-	}
682
-
683
-	/**
684
-	 * register an admin form to be shown
685
-	 *
686
-	 * @param string $app
687
-	 * @param string $page
688
-	 */
689
-	public static function registerAdmin($app, $page) {
690
-		self::$adminForms[] = $app . '/' . $page . '.php';
691
-	}
692
-
693
-	/**
694
-	 * register a personal form to be shown
695
-	 * @param string $app
696
-	 * @param string $page
697
-	 */
698
-	public static function registerPersonal($app, $page) {
699
-		self::$personalForms[] = $app . '/' . $page . '.php';
700
-	}
701
-
702
-	/**
703
-	 * @param array $entry
704
-	 */
705
-	public static function registerLogIn(array $entry) {
706
-		self::$altLogin[] = $entry;
707
-	}
708
-
709
-	/**
710
-	 * @return array
711
-	 */
712
-	public static function getAlternativeLogIns() {
713
-		return self::$altLogin;
714
-	}
715
-
716
-	/**
717
-	 * get a list of all apps in the apps folder
718
-	 *
719
-	 * @return array an array of app names (string IDs)
720
-	 * @todo: change the name of this method to getInstalledApps, which is more accurate
721
-	 */
722
-	public static function getAllApps() {
723
-
724
-		$apps = array();
725
-
726
-		foreach (OC::$APPSROOTS as $apps_dir) {
727
-			if (!is_readable($apps_dir['path'])) {
728
-				\OCP\Util::writeLog('core', 'unable to read app folder : ' . $apps_dir['path'], \OCP\Util::WARN);
729
-				continue;
730
-			}
731
-			$dh = opendir($apps_dir['path']);
732
-
733
-			if (is_resource($dh)) {
734
-				while (($file = readdir($dh)) !== false) {
735
-
736
-					if ($file[0] != '.' and is_dir($apps_dir['path'] . '/' . $file) and is_file($apps_dir['path'] . '/' . $file . '/appinfo/info.xml')) {
737
-
738
-						$apps[] = $file;
739
-					}
740
-				}
741
-			}
742
-		}
743
-
744
-		$apps = array_unique($apps);
745
-
746
-		return $apps;
747
-	}
748
-
749
-	/**
750
-	 * List all apps, this is used in apps.php
751
-	 *
752
-	 * @return array
753
-	 */
754
-	public function listAllApps() {
755
-		$installedApps = OC_App::getAllApps();
756
-
757
-		$appManager = \OC::$server->getAppManager();
758
-		//we don't want to show configuration for these
759
-		$blacklist = $appManager->getAlwaysEnabledApps();
760
-		$appList = array();
761
-		$langCode = \OC::$server->getL10N('core')->getLanguageCode();
762
-		$urlGenerator = \OC::$server->getURLGenerator();
763
-
764
-		foreach ($installedApps as $app) {
765
-			if (array_search($app, $blacklist) === false) {
766
-
767
-				$info = OC_App::getAppInfo($app, false, $langCode);
768
-				if (!is_array($info)) {
769
-					\OCP\Util::writeLog('core', 'Could not read app info file for app "' . $app . '"', \OCP\Util::ERROR);
770
-					continue;
771
-				}
772
-
773
-				if (!isset($info['name'])) {
774
-					\OCP\Util::writeLog('core', 'App id "' . $app . '" has no name in appinfo', \OCP\Util::ERROR);
775
-					continue;
776
-				}
777
-
778
-				$enabled = \OC::$server->getConfig()->getAppValue($app, 'enabled', 'no');
779
-				$info['groups'] = null;
780
-				if ($enabled === 'yes') {
781
-					$active = true;
782
-				} else if ($enabled === 'no') {
783
-					$active = false;
784
-				} else {
785
-					$active = true;
786
-					$info['groups'] = $enabled;
787
-				}
788
-
789
-				$info['active'] = $active;
790
-
791
-				if ($appManager->isShipped($app)) {
792
-					$info['internal'] = true;
793
-					$info['level'] = self::officialApp;
794
-					$info['removable'] = false;
795
-				} else {
796
-					$info['internal'] = false;
797
-					$info['removable'] = true;
798
-				}
799
-
800
-				$appPath = self::getAppPath($app);
801
-				if($appPath !== false) {
802
-					$appIcon = $appPath . '/img/' . $app . '.svg';
803
-					if (file_exists($appIcon)) {
804
-						$info['preview'] = $urlGenerator->imagePath($app, $app . '.svg');
805
-						$info['previewAsIcon'] = true;
806
-					} else {
807
-						$appIcon = $appPath . '/img/app.svg';
808
-						if (file_exists($appIcon)) {
809
-							$info['preview'] = $urlGenerator->imagePath($app, 'app.svg');
810
-							$info['previewAsIcon'] = true;
811
-						}
812
-					}
813
-				}
814
-				// fix documentation
815
-				if (isset($info['documentation']) && is_array($info['documentation'])) {
816
-					foreach ($info['documentation'] as $key => $url) {
817
-						// If it is not an absolute URL we assume it is a key
818
-						// i.e. admin-ldap will get converted to go.php?to=admin-ldap
819
-						if (stripos($url, 'https://') !== 0 && stripos($url, 'http://') !== 0) {
820
-							$url = $urlGenerator->linkToDocs($url);
821
-						}
822
-
823
-						$info['documentation'][$key] = $url;
824
-					}
825
-				}
826
-
827
-				$info['version'] = OC_App::getAppVersion($app);
828
-				$appList[] = $info;
829
-			}
830
-		}
831
-
832
-		return $appList;
833
-	}
834
-
835
-	public static function shouldUpgrade($app) {
836
-		$versions = self::getAppVersions();
837
-		$currentVersion = OC_App::getAppVersion($app);
838
-		if ($currentVersion && isset($versions[$app])) {
839
-			$installedVersion = $versions[$app];
840
-			if (!version_compare($currentVersion, $installedVersion, '=')) {
841
-				return true;
842
-			}
843
-		}
844
-		return false;
845
-	}
846
-
847
-	/**
848
-	 * Adjust the number of version parts of $version1 to match
849
-	 * the number of version parts of $version2.
850
-	 *
851
-	 * @param string $version1 version to adjust
852
-	 * @param string $version2 version to take the number of parts from
853
-	 * @return string shortened $version1
854
-	 */
855
-	private static function adjustVersionParts($version1, $version2) {
856
-		$version1 = explode('.', $version1);
857
-		$version2 = explode('.', $version2);
858
-		// reduce $version1 to match the number of parts in $version2
859
-		while (count($version1) > count($version2)) {
860
-			array_pop($version1);
861
-		}
862
-		// if $version1 does not have enough parts, add some
863
-		while (count($version1) < count($version2)) {
864
-			$version1[] = '0';
865
-		}
866
-		return implode('.', $version1);
867
-	}
868
-
869
-	/**
870
-	 * Check whether the current ownCloud version matches the given
871
-	 * application's version requirements.
872
-	 *
873
-	 * The comparison is made based on the number of parts that the
874
-	 * app info version has. For example for ownCloud 6.0.3 if the
875
-	 * app info version is expecting version 6.0, the comparison is
876
-	 * made on the first two parts of the ownCloud version.
877
-	 * This means that it's possible to specify "requiremin" => 6
878
-	 * and "requiremax" => 6 and it will still match ownCloud 6.0.3.
879
-	 *
880
-	 * @param string $ocVersion ownCloud version to check against
881
-	 * @param array $appInfo app info (from xml)
882
-	 *
883
-	 * @return boolean true if compatible, otherwise false
884
-	 */
885
-	public static function isAppCompatible($ocVersion, $appInfo) {
886
-		$requireMin = '';
887
-		$requireMax = '';
888
-		if (isset($appInfo['dependencies']['nextcloud']['@attributes']['min-version'])) {
889
-			$requireMin = $appInfo['dependencies']['nextcloud']['@attributes']['min-version'];
890
-		} elseif (isset($appInfo['dependencies']['owncloud']['@attributes']['min-version'])) {
891
-			$requireMin = $appInfo['dependencies']['owncloud']['@attributes']['min-version'];
892
-		} else if (isset($appInfo['requiremin'])) {
893
-			$requireMin = $appInfo['requiremin'];
894
-		} else if (isset($appInfo['require'])) {
895
-			$requireMin = $appInfo['require'];
896
-		}
897
-
898
-		if (isset($appInfo['dependencies']['nextcloud']['@attributes']['max-version'])) {
899
-			$requireMax = $appInfo['dependencies']['nextcloud']['@attributes']['max-version'];
900
-		} elseif (isset($appInfo['dependencies']['owncloud']['@attributes']['max-version'])) {
901
-			$requireMax = $appInfo['dependencies']['owncloud']['@attributes']['max-version'];
902
-		} else if (isset($appInfo['requiremax'])) {
903
-			$requireMax = $appInfo['requiremax'];
904
-		}
905
-
906
-		if (is_array($ocVersion)) {
907
-			$ocVersion = implode('.', $ocVersion);
908
-		}
909
-
910
-		if (!empty($requireMin)
911
-			&& version_compare(self::adjustVersionParts($ocVersion, $requireMin), $requireMin, '<')
912
-		) {
913
-
914
-			return false;
915
-		}
916
-
917
-		if (!empty($requireMax)
918
-			&& version_compare(self::adjustVersionParts($ocVersion, $requireMax), $requireMax, '>')
919
-		) {
920
-			return false;
921
-		}
922
-
923
-		return true;
924
-	}
925
-
926
-	/**
927
-	 * get the installed version of all apps
928
-	 */
929
-	public static function getAppVersions() {
930
-		static $versions;
931
-
932
-		if(!$versions) {
933
-			$appConfig = \OC::$server->getAppConfig();
934
-			$versions = $appConfig->getValues(false, 'installed_version');
935
-		}
936
-		return $versions;
937
-	}
938
-
939
-	/**
940
-	 * update the database for the app and call the update script
941
-	 *
942
-	 * @param string $appId
943
-	 * @return bool
944
-	 */
945
-	public static function updateApp($appId) {
946
-		$appPath = self::getAppPath($appId);
947
-		if($appPath === false) {
948
-			return false;
949
-		}
950
-		self::registerAutoloading($appId, $appPath);
951
-
952
-		$appData = self::getAppInfo($appId);
953
-		self::executeRepairSteps($appId, $appData['repair-steps']['pre-migration']);
954
-
955
-		if (file_exists($appPath . '/appinfo/database.xml')) {
956
-			OC_DB::updateDbFromStructure($appPath . '/appinfo/database.xml');
957
-		} else {
958
-			$ms = new MigrationService($appId, \OC::$server->getDatabaseConnection());
959
-			$ms->migrate();
960
-		}
961
-
962
-		self::executeRepairSteps($appId, $appData['repair-steps']['post-migration']);
963
-		self::setupLiveMigrations($appId, $appData['repair-steps']['live-migration']);
964
-		// update appversion in app manager
965
-		\OC::$server->getAppManager()->getAppVersion($appId, false);
966
-
967
-		// run upgrade code
968
-		if (file_exists($appPath . '/appinfo/update.php')) {
969
-			self::loadApp($appId);
970
-			include $appPath . '/appinfo/update.php';
971
-		}
972
-		self::setupBackgroundJobs($appData['background-jobs']);
973
-
974
-		//set remote/public handlers
975
-		if (array_key_exists('ocsid', $appData)) {
976
-			\OC::$server->getConfig()->setAppValue($appId, 'ocsid', $appData['ocsid']);
977
-		} elseif(\OC::$server->getConfig()->getAppValue($appId, 'ocsid', null) !== null) {
978
-			\OC::$server->getConfig()->deleteAppValue($appId, 'ocsid');
979
-		}
980
-		foreach ($appData['remote'] as $name => $path) {
981
-			\OC::$server->getConfig()->setAppValue('core', 'remote_' . $name, $appId . '/' . $path);
982
-		}
983
-		foreach ($appData['public'] as $name => $path) {
984
-			\OC::$server->getConfig()->setAppValue('core', 'public_' . $name, $appId . '/' . $path);
985
-		}
986
-
987
-		self::setAppTypes($appId);
988
-
989
-		$version = \OC_App::getAppVersion($appId);
990
-		\OC::$server->getConfig()->setAppValue($appId, 'installed_version', $version);
991
-
992
-		\OC::$server->getEventDispatcher()->dispatch(ManagerEvent::EVENT_APP_UPDATE, new ManagerEvent(
993
-			ManagerEvent::EVENT_APP_UPDATE, $appId
994
-		));
995
-
996
-		return true;
997
-	}
998
-
999
-	/**
1000
-	 * @param string $appId
1001
-	 * @param string[] $steps
1002
-	 * @throws \OC\NeedsUpdateException
1003
-	 */
1004
-	public static function executeRepairSteps($appId, array $steps) {
1005
-		if (empty($steps)) {
1006
-			return;
1007
-		}
1008
-		// load the app
1009
-		self::loadApp($appId);
1010
-
1011
-		$dispatcher = OC::$server->getEventDispatcher();
1012
-
1013
-		// load the steps
1014
-		$r = new Repair([], $dispatcher);
1015
-		foreach ($steps as $step) {
1016
-			try {
1017
-				$r->addStep($step);
1018
-			} catch (Exception $ex) {
1019
-				$r->emit('\OC\Repair', 'error', [$ex->getMessage()]);
1020
-				\OC::$server->getLogger()->logException($ex);
1021
-			}
1022
-		}
1023
-		// run the steps
1024
-		$r->run();
1025
-	}
1026
-
1027
-	public static function setupBackgroundJobs(array $jobs) {
1028
-		$queue = \OC::$server->getJobList();
1029
-		foreach ($jobs as $job) {
1030
-			$queue->add($job);
1031
-		}
1032
-	}
1033
-
1034
-	/**
1035
-	 * @param string $appId
1036
-	 * @param string[] $steps
1037
-	 */
1038
-	private static function setupLiveMigrations($appId, array $steps) {
1039
-		$queue = \OC::$server->getJobList();
1040
-		foreach ($steps as $step) {
1041
-			$queue->add('OC\Migration\BackgroundRepair', [
1042
-				'app' => $appId,
1043
-				'step' => $step]);
1044
-		}
1045
-	}
1046
-
1047
-	/**
1048
-	 * @param string $appId
1049
-	 * @return \OC\Files\View|false
1050
-	 */
1051
-	public static function getStorage($appId) {
1052
-		if (\OC::$server->getAppManager()->isEnabledForUser($appId)) { //sanity check
1053
-			if (\OC::$server->getUserSession()->isLoggedIn()) {
1054
-				$view = new \OC\Files\View('/' . OC_User::getUser());
1055
-				if (!$view->file_exists($appId)) {
1056
-					$view->mkdir($appId);
1057
-				}
1058
-				return new \OC\Files\View('/' . OC_User::getUser() . '/' . $appId);
1059
-			} else {
1060
-				\OCP\Util::writeLog('core', 'Can\'t get app storage, app ' . $appId . ', user not logged in', \OCP\Util::ERROR);
1061
-				return false;
1062
-			}
1063
-		} else {
1064
-			\OCP\Util::writeLog('core', 'Can\'t get app storage, app ' . $appId . ' not enabled', \OCP\Util::ERROR);
1065
-			return false;
1066
-		}
1067
-	}
1068
-
1069
-	protected static function findBestL10NOption($options, $lang) {
1070
-		$fallback = $similarLangFallback = $englishFallback = false;
1071
-
1072
-		$lang = strtolower($lang);
1073
-		$similarLang = $lang;
1074
-		if (strpos($similarLang, '_')) {
1075
-			// For "de_DE" we want to find "de" and the other way around
1076
-			$similarLang = substr($lang, 0, strpos($lang, '_'));
1077
-		}
1078
-
1079
-		foreach ($options as $option) {
1080
-			if (is_array($option)) {
1081
-				if ($fallback === false) {
1082
-					$fallback = $option['@value'];
1083
-				}
1084
-
1085
-				if (!isset($option['@attributes']['lang'])) {
1086
-					continue;
1087
-				}
1088
-
1089
-				$attributeLang = strtolower($option['@attributes']['lang']);
1090
-				if ($attributeLang === $lang) {
1091
-					return $option['@value'];
1092
-				}
1093
-
1094
-				if ($attributeLang === $similarLang) {
1095
-					$similarLangFallback = $option['@value'];
1096
-				} else if (strpos($attributeLang, $similarLang . '_') === 0) {
1097
-					if ($similarLangFallback === false) {
1098
-						$similarLangFallback =  $option['@value'];
1099
-					}
1100
-				}
1101
-			} else {
1102
-				$englishFallback = $option;
1103
-			}
1104
-		}
1105
-
1106
-		if ($similarLangFallback !== false) {
1107
-			return $similarLangFallback;
1108
-		} else if ($englishFallback !== false) {
1109
-			return $englishFallback;
1110
-		}
1111
-		return (string) $fallback;
1112
-	}
1113
-
1114
-	/**
1115
-	 * parses the app data array and enhanced the 'description' value
1116
-	 *
1117
-	 * @param array $data the app data
1118
-	 * @param string $lang
1119
-	 * @return array improved app data
1120
-	 */
1121
-	public static function parseAppInfo(array $data, $lang = null) {
1122
-
1123
-		if ($lang && isset($data['name']) && is_array($data['name'])) {
1124
-			$data['name'] = self::findBestL10NOption($data['name'], $lang);
1125
-		}
1126
-		if ($lang && isset($data['summary']) && is_array($data['summary'])) {
1127
-			$data['summary'] = self::findBestL10NOption($data['summary'], $lang);
1128
-		}
1129
-		if ($lang && isset($data['description']) && is_array($data['description'])) {
1130
-			$data['description'] = trim(self::findBestL10NOption($data['description'], $lang));
1131
-		} else if (isset($data['description']) && is_string($data['description'])) {
1132
-			$data['description'] = trim($data['description']);
1133
-		} else  {
1134
-			$data['description'] = '';
1135
-		}
1136
-
1137
-		return $data;
1138
-	}
1139
-
1140
-	/**
1141
-	 * @param \OCP\IConfig $config
1142
-	 * @param \OCP\IL10N $l
1143
-	 * @param array $info
1144
-	 * @throws \Exception
1145
-	 */
1146
-	public static function checkAppDependencies($config, $l, $info) {
1147
-		$dependencyAnalyzer = new DependencyAnalyzer(new Platform($config), $l);
1148
-		$missing = $dependencyAnalyzer->analyze($info);
1149
-		if (!empty($missing)) {
1150
-			$missingMsg = implode(PHP_EOL, $missing);
1151
-			throw new \Exception(
1152
-				$l->t('App "%s" cannot be installed because the following dependencies are not fulfilled: %s',
1153
-					[$info['name'], $missingMsg]
1154
-				)
1155
-			);
1156
-		}
1157
-	}
65
+    static private $adminForms = array();
66
+    static private $personalForms = array();
67
+    static private $appTypes = array();
68
+    static private $loadedApps = array();
69
+    static private $altLogin = array();
70
+    static private $alreadyRegistered = [];
71
+    const officialApp = 200;
72
+
73
+    /**
74
+     * clean the appId
75
+     *
76
+     * @param string|boolean $app AppId that needs to be cleaned
77
+     * @return string
78
+     */
79
+    public static function cleanAppId($app) {
80
+        return str_replace(array('\0', '/', '\\', '..'), '', $app);
81
+    }
82
+
83
+    /**
84
+     * Check if an app is loaded
85
+     *
86
+     * @param string $app
87
+     * @return bool
88
+     */
89
+    public static function isAppLoaded($app) {
90
+        return in_array($app, self::$loadedApps, true);
91
+    }
92
+
93
+    /**
94
+     * loads all apps
95
+     *
96
+     * @param string[] | string | null $types
97
+     * @return bool
98
+     *
99
+     * This function walks through the ownCloud directory and loads all apps
100
+     * it can find. A directory contains an app if the file /appinfo/info.xml
101
+     * exists.
102
+     *
103
+     * if $types is set, only apps of those types will be loaded
104
+     */
105
+    public static function loadApps($types = null) {
106
+        if (\OC::$server->getSystemConfig()->getValue('maintenance', false)) {
107
+            return false;
108
+        }
109
+        // Load the enabled apps here
110
+        $apps = self::getEnabledApps();
111
+
112
+        // Add each apps' folder as allowed class path
113
+        foreach($apps as $app) {
114
+            $path = self::getAppPath($app);
115
+            if($path !== false) {
116
+                self::registerAutoloading($app, $path);
117
+            }
118
+        }
119
+
120
+        // prevent app.php from printing output
121
+        ob_start();
122
+        foreach ($apps as $app) {
123
+            if ((is_null($types) or self::isType($app, $types)) && !in_array($app, self::$loadedApps)) {
124
+                self::loadApp($app);
125
+            }
126
+        }
127
+        ob_end_clean();
128
+
129
+        return true;
130
+    }
131
+
132
+    /**
133
+     * load a single app
134
+     *
135
+     * @param string $app
136
+     */
137
+    public static function loadApp($app) {
138
+        self::$loadedApps[] = $app;
139
+        $appPath = self::getAppPath($app);
140
+        if($appPath === false) {
141
+            return;
142
+        }
143
+
144
+        // in case someone calls loadApp() directly
145
+        self::registerAutoloading($app, $appPath);
146
+
147
+        if (is_file($appPath . '/appinfo/app.php')) {
148
+            \OC::$server->getEventLogger()->start('load_app_' . $app, 'Load app: ' . $app);
149
+            self::requireAppFile($app);
150
+            if (self::isType($app, array('authentication'))) {
151
+                // since authentication apps affect the "is app enabled for group" check,
152
+                // the enabled apps cache needs to be cleared to make sure that the
153
+                // next time getEnableApps() is called it will also include apps that were
154
+                // enabled for groups
155
+                self::$enabledAppsCache = array();
156
+            }
157
+            \OC::$server->getEventLogger()->end('load_app_' . $app);
158
+        }
159
+
160
+        $info = self::getAppInfo($app);
161
+        if (!empty($info['activity']['filters'])) {
162
+            foreach ($info['activity']['filters'] as $filter) {
163
+                \OC::$server->getActivityManager()->registerFilter($filter);
164
+            }
165
+        }
166
+        if (!empty($info['activity']['settings'])) {
167
+            foreach ($info['activity']['settings'] as $setting) {
168
+                \OC::$server->getActivityManager()->registerSetting($setting);
169
+            }
170
+        }
171
+        if (!empty($info['activity']['providers'])) {
172
+            foreach ($info['activity']['providers'] as $provider) {
173
+                \OC::$server->getActivityManager()->registerProvider($provider);
174
+            }
175
+        }
176
+
177
+        if (!empty($info['settings']['admin'])) {
178
+            foreach ($info['settings']['admin'] as $setting) {
179
+                \OC::$server->getSettingsManager()->registerSetting('admin', $setting);
180
+            }
181
+        }
182
+        if (!empty($info['settings']['admin-section'])) {
183
+            foreach ($info['settings']['admin-section'] as $section) {
184
+                \OC::$server->getSettingsManager()->registerSection('admin', $section);
185
+            }
186
+        }
187
+        if (!empty($info['settings']['personal'])) {
188
+            foreach ($info['settings']['personal'] as $setting) {
189
+                \OC::$server->getSettingsManager()->registerSetting('personal', $setting);
190
+            }
191
+        }
192
+        if (!empty($info['settings']['personal-section'])) {
193
+            foreach ($info['settings']['personal-section'] as $section) {
194
+                \OC::$server->getSettingsManager()->registerSection('personal', $section);
195
+            }
196
+        }
197
+
198
+        if (!empty($info['collaboration']['plugins'])) {
199
+            // deal with one or many plugin entries
200
+            $plugins = isset($info['collaboration']['plugins']['plugin']['@value']) ?
201
+                [$info['collaboration']['plugins']['plugin']] : $info['collaboration']['plugins']['plugin'];
202
+            foreach ($plugins as $plugin) {
203
+                if($plugin['@attributes']['type'] === 'collaborator-search') {
204
+                    $pluginInfo = [
205
+                        'shareType' => $plugin['@attributes']['share-type'],
206
+                        'class' => $plugin['@value'],
207
+                    ];
208
+                    \OC::$server->getCollaboratorSearch()->registerPlugin($pluginInfo);
209
+                } else if ($plugin['@attributes']['type'] === 'autocomplete-sort') {
210
+                    \OC::$server->getAutoCompleteManager()->registerSorter($plugin['@value']);
211
+                }
212
+            }
213
+        }
214
+    }
215
+
216
+    /**
217
+     * @internal
218
+     * @param string $app
219
+     * @param string $path
220
+     */
221
+    public static function registerAutoloading($app, $path) {
222
+        $key = $app . '-' . $path;
223
+        if(isset(self::$alreadyRegistered[$key])) {
224
+            return;
225
+        }
226
+
227
+        self::$alreadyRegistered[$key] = true;
228
+
229
+        // Register on PSR-4 composer autoloader
230
+        $appNamespace = \OC\AppFramework\App::buildAppNamespace($app);
231
+        \OC::$server->registerNamespace($app, $appNamespace);
232
+
233
+        if (file_exists($path . '/composer/autoload.php')) {
234
+            require_once $path . '/composer/autoload.php';
235
+        } else {
236
+            \OC::$composerAutoloader->addPsr4($appNamespace . '\\', $path . '/lib/', true);
237
+            // Register on legacy autoloader
238
+            \OC::$loader->addValidRoot($path);
239
+        }
240
+
241
+        // Register Test namespace only when testing
242
+        if (defined('PHPUNIT_RUN') || defined('CLI_TEST_RUN')) {
243
+            \OC::$composerAutoloader->addPsr4($appNamespace . '\\Tests\\', $path . '/tests/', true);
244
+        }
245
+    }
246
+
247
+    /**
248
+     * Load app.php from the given app
249
+     *
250
+     * @param string $app app name
251
+     */
252
+    private static function requireAppFile($app) {
253
+        try {
254
+            // encapsulated here to avoid variable scope conflicts
255
+            require_once $app . '/appinfo/app.php';
256
+        } catch (Error $ex) {
257
+            \OC::$server->getLogger()->logException($ex);
258
+            if (!\OC::$server->getAppManager()->isShipped($app)) {
259
+                // Only disable apps which are not shipped
260
+                self::disable($app);
261
+            }
262
+        }
263
+    }
264
+
265
+    /**
266
+     * check if an app is of a specific type
267
+     *
268
+     * @param string $app
269
+     * @param string|array $types
270
+     * @return bool
271
+     */
272
+    public static function isType($app, $types) {
273
+        if (is_string($types)) {
274
+            $types = array($types);
275
+        }
276
+        $appTypes = self::getAppTypes($app);
277
+        foreach ($types as $type) {
278
+            if (array_search($type, $appTypes) !== false) {
279
+                return true;
280
+            }
281
+        }
282
+        return false;
283
+    }
284
+
285
+    /**
286
+     * get the types of an app
287
+     *
288
+     * @param string $app
289
+     * @return array
290
+     */
291
+    private static function getAppTypes($app) {
292
+        //load the cache
293
+        if (count(self::$appTypes) == 0) {
294
+            self::$appTypes = \OC::$server->getAppConfig()->getValues(false, 'types');
295
+        }
296
+
297
+        if (isset(self::$appTypes[$app])) {
298
+            return explode(',', self::$appTypes[$app]);
299
+        } else {
300
+            return array();
301
+        }
302
+    }
303
+
304
+    /**
305
+     * read app types from info.xml and cache them in the database
306
+     */
307
+    public static function setAppTypes($app) {
308
+        $appData = self::getAppInfo($app);
309
+        if(!is_array($appData)) {
310
+            return;
311
+        }
312
+
313
+        if (isset($appData['types'])) {
314
+            $appTypes = implode(',', $appData['types']);
315
+        } else {
316
+            $appTypes = '';
317
+            $appData['types'] = [];
318
+        }
319
+
320
+        \OC::$server->getConfig()->setAppValue($app, 'types', $appTypes);
321
+
322
+        if (\OC::$server->getAppManager()->hasProtectedAppType($appData['types'])) {
323
+            $enabled = \OC::$server->getConfig()->getAppValue($app, 'enabled', 'yes');
324
+            if ($enabled !== 'yes' && $enabled !== 'no') {
325
+                \OC::$server->getConfig()->setAppValue($app, 'enabled', 'yes');
326
+            }
327
+        }
328
+    }
329
+
330
+    /**
331
+     * get all enabled apps
332
+     */
333
+    protected static $enabledAppsCache = array();
334
+
335
+    /**
336
+     * Returns apps enabled for the current user.
337
+     *
338
+     * @param bool $forceRefresh whether to refresh the cache
339
+     * @param bool $all whether to return apps for all users, not only the
340
+     * currently logged in one
341
+     * @return string[]
342
+     */
343
+    public static function getEnabledApps($forceRefresh = false, $all = false) {
344
+        if (!\OC::$server->getSystemConfig()->getValue('installed', false)) {
345
+            return array();
346
+        }
347
+        // in incognito mode or when logged out, $user will be false,
348
+        // which is also the case during an upgrade
349
+        $appManager = \OC::$server->getAppManager();
350
+        if ($all) {
351
+            $user = null;
352
+        } else {
353
+            $user = \OC::$server->getUserSession()->getUser();
354
+        }
355
+
356
+        if (is_null($user)) {
357
+            $apps = $appManager->getInstalledApps();
358
+        } else {
359
+            $apps = $appManager->getEnabledAppsForUser($user);
360
+        }
361
+        $apps = array_filter($apps, function ($app) {
362
+            return $app !== 'files';//we add this manually
363
+        });
364
+        sort($apps);
365
+        array_unshift($apps, 'files');
366
+        return $apps;
367
+    }
368
+
369
+    /**
370
+     * checks whether or not an app is enabled
371
+     *
372
+     * @param string $app app
373
+     * @return bool
374
+     * @deprecated 13.0.0 use \OC::$server->getAppManager()->isEnabledForUser($appId)
375
+     *
376
+     * This function checks whether or not an app is enabled.
377
+     */
378
+    public static function isEnabled($app) {
379
+        return \OC::$server->getAppManager()->isEnabledForUser($app);
380
+    }
381
+
382
+    /**
383
+     * enables an app
384
+     *
385
+     * @param string $appId
386
+     * @param array $groups (optional) when set, only these groups will have access to the app
387
+     * @throws \Exception
388
+     * @return void
389
+     *
390
+     * This function set an app as enabled in appconfig.
391
+     */
392
+    public function enable($appId,
393
+                            $groups = null) {
394
+        self::$enabledAppsCache = []; // flush
395
+
396
+        // Check if app is already downloaded
397
+        $installer = \OC::$server->query(Installer::class);
398
+        $isDownloaded = $installer->isDownloaded($appId);
399
+
400
+        if(!$isDownloaded) {
401
+            $installer->downloadApp($appId);
402
+        }
403
+
404
+        $installer->installApp($appId);
405
+
406
+        $appManager = \OC::$server->getAppManager();
407
+        if (!is_null($groups)) {
408
+            $groupManager = \OC::$server->getGroupManager();
409
+            $groupsList = [];
410
+            foreach ($groups as $group) {
411
+                $groupItem = $groupManager->get($group);
412
+                if ($groupItem instanceof \OCP\IGroup) {
413
+                    $groupsList[] = $groupManager->get($group);
414
+                }
415
+            }
416
+            $appManager->enableAppForGroups($appId, $groupsList);
417
+        } else {
418
+            $appManager->enableApp($appId);
419
+        }
420
+    }
421
+
422
+    /**
423
+     * This function set an app as disabled in appconfig.
424
+     *
425
+     * @param string $app app
426
+     * @throws Exception
427
+     */
428
+    public static function disable($app) {
429
+        // flush
430
+        self::$enabledAppsCache = array();
431
+
432
+        // run uninstall steps
433
+        $appData = OC_App::getAppInfo($app);
434
+        if (!is_null($appData)) {
435
+            OC_App::executeRepairSteps($app, $appData['repair-steps']['uninstall']);
436
+        }
437
+
438
+        // emit disable hook - needed anymore ?
439
+        \OC_Hook::emit('OC_App', 'pre_disable', array('app' => $app));
440
+
441
+        // finally disable it
442
+        $appManager = \OC::$server->getAppManager();
443
+        $appManager->disableApp($app);
444
+    }
445
+
446
+    // This is private as well. It simply works, so don't ask for more details
447
+    private static function proceedNavigation($list) {
448
+        usort($list, function($a, $b) {
449
+            if (isset($a['order']) && isset($b['order'])) {
450
+                return ($a['order'] < $b['order']) ? -1 : 1;
451
+            } else if (isset($a['order']) || isset($b['order'])) {
452
+                return isset($a['order']) ? -1 : 1;
453
+            } else {
454
+                return ($a['name'] < $b['name']) ? -1 : 1;
455
+            }
456
+        });
457
+
458
+        $activeApp = OC::$server->getNavigationManager()->getActiveEntry();
459
+        foreach ($list as $index => &$navEntry) {
460
+            if ($navEntry['id'] == $activeApp) {
461
+                $navEntry['active'] = true;
462
+            } else {
463
+                $navEntry['active'] = false;
464
+            }
465
+        }
466
+        unset($navEntry);
467
+
468
+        return $list;
469
+    }
470
+
471
+    /**
472
+     * Get the path where to install apps
473
+     *
474
+     * @return string|false
475
+     */
476
+    public static function getInstallPath() {
477
+        if (\OC::$server->getSystemConfig()->getValue('appstoreenabled', true) == false) {
478
+            return false;
479
+        }
480
+
481
+        foreach (OC::$APPSROOTS as $dir) {
482
+            if (isset($dir['writable']) && $dir['writable'] === true) {
483
+                return $dir['path'];
484
+            }
485
+        }
486
+
487
+        \OCP\Util::writeLog('core', 'No application directories are marked as writable.', \OCP\Util::ERROR);
488
+        return null;
489
+    }
490
+
491
+
492
+    /**
493
+     * search for an app in all app-directories
494
+     *
495
+     * @param string $appId
496
+     * @return false|string
497
+     */
498
+    public static function findAppInDirectories($appId) {
499
+        $sanitizedAppId = self::cleanAppId($appId);
500
+        if($sanitizedAppId !== $appId) {
501
+            return false;
502
+        }
503
+        static $app_dir = array();
504
+
505
+        if (isset($app_dir[$appId])) {
506
+            return $app_dir[$appId];
507
+        }
508
+
509
+        $possibleApps = array();
510
+        foreach (OC::$APPSROOTS as $dir) {
511
+            if (file_exists($dir['path'] . '/' . $appId)) {
512
+                $possibleApps[] = $dir;
513
+            }
514
+        }
515
+
516
+        if (empty($possibleApps)) {
517
+            return false;
518
+        } elseif (count($possibleApps) === 1) {
519
+            $dir = array_shift($possibleApps);
520
+            $app_dir[$appId] = $dir;
521
+            return $dir;
522
+        } else {
523
+            $versionToLoad = array();
524
+            foreach ($possibleApps as $possibleApp) {
525
+                $version = self::getAppVersionByPath($possibleApp['path']);
526
+                if (empty($versionToLoad) || version_compare($version, $versionToLoad['version'], '>')) {
527
+                    $versionToLoad = array(
528
+                        'dir' => $possibleApp,
529
+                        'version' => $version,
530
+                    );
531
+                }
532
+            }
533
+            $app_dir[$appId] = $versionToLoad['dir'];
534
+            return $versionToLoad['dir'];
535
+            //TODO - write test
536
+        }
537
+    }
538
+
539
+    /**
540
+     * Get the directory for the given app.
541
+     * If the app is defined in multiple directories, the first one is taken. (false if not found)
542
+     *
543
+     * @param string $appId
544
+     * @return string|false
545
+     */
546
+    public static function getAppPath($appId) {
547
+        if ($appId === null || trim($appId) === '') {
548
+            return false;
549
+        }
550
+
551
+        if (($dir = self::findAppInDirectories($appId)) != false) {
552
+            return $dir['path'] . '/' . $appId;
553
+        }
554
+        return false;
555
+    }
556
+
557
+    /**
558
+     * Get the path for the given app on the access
559
+     * If the app is defined in multiple directories, the first one is taken. (false if not found)
560
+     *
561
+     * @param string $appId
562
+     * @return string|false
563
+     */
564
+    public static function getAppWebPath($appId) {
565
+        if (($dir = self::findAppInDirectories($appId)) != false) {
566
+            return OC::$WEBROOT . $dir['url'] . '/' . $appId;
567
+        }
568
+        return false;
569
+    }
570
+
571
+    /**
572
+     * get the last version of the app from appinfo/info.xml
573
+     *
574
+     * @param string $appId
575
+     * @param bool $useCache
576
+     * @return string
577
+     * @deprecated 14.0.0 use \OC::$server->getAppManager()->getAppVersion()
578
+     */
579
+    public static function getAppVersion($appId, $useCache = true) {
580
+        return \OC::$server->getAppManager()->getAppVersion($appId, $useCache);
581
+    }
582
+
583
+    /**
584
+     * get app's version based on it's path
585
+     *
586
+     * @param string $path
587
+     * @return string
588
+     */
589
+    public static function getAppVersionByPath($path) {
590
+        $infoFile = $path . '/appinfo/info.xml';
591
+        $appData = \OC::$server->getAppManager()->getAppInfo($infoFile, true);
592
+        return isset($appData['version']) ? $appData['version'] : '';
593
+    }
594
+
595
+
596
+    /**
597
+     * Read all app metadata from the info.xml file
598
+     *
599
+     * @param string $appId id of the app or the path of the info.xml file
600
+     * @param bool $path
601
+     * @param string $lang
602
+     * @return array|null
603
+     * @note all data is read from info.xml, not just pre-defined fields
604
+     * @deprecated 14.0.0 use \OC::$server->getAppManager()->getAppInfo()
605
+     */
606
+    public static function getAppInfo($appId, $path = false, $lang = null) {
607
+        return \OC::$server->getAppManager()->getAppInfo($appId, $path, $lang);
608
+    }
609
+
610
+    /**
611
+     * Returns the navigation
612
+     *
613
+     * @return array
614
+     *
615
+     * This function returns an array containing all entries added. The
616
+     * entries are sorted by the key 'order' ascending. Additional to the keys
617
+     * given for each app the following keys exist:
618
+     *   - active: boolean, signals if the user is on this navigation entry
619
+     */
620
+    public static function getNavigation() {
621
+        $entries = OC::$server->getNavigationManager()->getAll();
622
+        return self::proceedNavigation($entries);
623
+    }
624
+
625
+    /**
626
+     * Returns the Settings Navigation
627
+     *
628
+     * @return string[]
629
+     *
630
+     * This function returns an array containing all settings pages added. The
631
+     * entries are sorted by the key 'order' ascending.
632
+     */
633
+    public static function getSettingsNavigation() {
634
+        $entries = OC::$server->getNavigationManager()->getAll('settings');
635
+        return self::proceedNavigation($entries);
636
+    }
637
+
638
+    /**
639
+     * get the id of loaded app
640
+     *
641
+     * @return string
642
+     */
643
+    public static function getCurrentApp() {
644
+        $request = \OC::$server->getRequest();
645
+        $script = substr($request->getScriptName(), strlen(OC::$WEBROOT) + 1);
646
+        $topFolder = substr($script, 0, strpos($script, '/') ?: 0);
647
+        if (empty($topFolder)) {
648
+            $path_info = $request->getPathInfo();
649
+            if ($path_info) {
650
+                $topFolder = substr($path_info, 1, strpos($path_info, '/', 1) - 1);
651
+            }
652
+        }
653
+        if ($topFolder == 'apps') {
654
+            $length = strlen($topFolder);
655
+            return substr($script, $length + 1, strpos($script, '/', $length + 1) - $length - 1);
656
+        } else {
657
+            return $topFolder;
658
+        }
659
+    }
660
+
661
+    /**
662
+     * @param string $type
663
+     * @return array
664
+     */
665
+    public static function getForms($type) {
666
+        $forms = array();
667
+        switch ($type) {
668
+            case 'admin':
669
+                $source = self::$adminForms;
670
+                break;
671
+            case 'personal':
672
+                $source = self::$personalForms;
673
+                break;
674
+            default:
675
+                return array();
676
+        }
677
+        foreach ($source as $form) {
678
+            $forms[] = include $form;
679
+        }
680
+        return $forms;
681
+    }
682
+
683
+    /**
684
+     * register an admin form to be shown
685
+     *
686
+     * @param string $app
687
+     * @param string $page
688
+     */
689
+    public static function registerAdmin($app, $page) {
690
+        self::$adminForms[] = $app . '/' . $page . '.php';
691
+    }
692
+
693
+    /**
694
+     * register a personal form to be shown
695
+     * @param string $app
696
+     * @param string $page
697
+     */
698
+    public static function registerPersonal($app, $page) {
699
+        self::$personalForms[] = $app . '/' . $page . '.php';
700
+    }
701
+
702
+    /**
703
+     * @param array $entry
704
+     */
705
+    public static function registerLogIn(array $entry) {
706
+        self::$altLogin[] = $entry;
707
+    }
708
+
709
+    /**
710
+     * @return array
711
+     */
712
+    public static function getAlternativeLogIns() {
713
+        return self::$altLogin;
714
+    }
715
+
716
+    /**
717
+     * get a list of all apps in the apps folder
718
+     *
719
+     * @return array an array of app names (string IDs)
720
+     * @todo: change the name of this method to getInstalledApps, which is more accurate
721
+     */
722
+    public static function getAllApps() {
723
+
724
+        $apps = array();
725
+
726
+        foreach (OC::$APPSROOTS as $apps_dir) {
727
+            if (!is_readable($apps_dir['path'])) {
728
+                \OCP\Util::writeLog('core', 'unable to read app folder : ' . $apps_dir['path'], \OCP\Util::WARN);
729
+                continue;
730
+            }
731
+            $dh = opendir($apps_dir['path']);
732
+
733
+            if (is_resource($dh)) {
734
+                while (($file = readdir($dh)) !== false) {
735
+
736
+                    if ($file[0] != '.' and is_dir($apps_dir['path'] . '/' . $file) and is_file($apps_dir['path'] . '/' . $file . '/appinfo/info.xml')) {
737
+
738
+                        $apps[] = $file;
739
+                    }
740
+                }
741
+            }
742
+        }
743
+
744
+        $apps = array_unique($apps);
745
+
746
+        return $apps;
747
+    }
748
+
749
+    /**
750
+     * List all apps, this is used in apps.php
751
+     *
752
+     * @return array
753
+     */
754
+    public function listAllApps() {
755
+        $installedApps = OC_App::getAllApps();
756
+
757
+        $appManager = \OC::$server->getAppManager();
758
+        //we don't want to show configuration for these
759
+        $blacklist = $appManager->getAlwaysEnabledApps();
760
+        $appList = array();
761
+        $langCode = \OC::$server->getL10N('core')->getLanguageCode();
762
+        $urlGenerator = \OC::$server->getURLGenerator();
763
+
764
+        foreach ($installedApps as $app) {
765
+            if (array_search($app, $blacklist) === false) {
766
+
767
+                $info = OC_App::getAppInfo($app, false, $langCode);
768
+                if (!is_array($info)) {
769
+                    \OCP\Util::writeLog('core', 'Could not read app info file for app "' . $app . '"', \OCP\Util::ERROR);
770
+                    continue;
771
+                }
772
+
773
+                if (!isset($info['name'])) {
774
+                    \OCP\Util::writeLog('core', 'App id "' . $app . '" has no name in appinfo', \OCP\Util::ERROR);
775
+                    continue;
776
+                }
777
+
778
+                $enabled = \OC::$server->getConfig()->getAppValue($app, 'enabled', 'no');
779
+                $info['groups'] = null;
780
+                if ($enabled === 'yes') {
781
+                    $active = true;
782
+                } else if ($enabled === 'no') {
783
+                    $active = false;
784
+                } else {
785
+                    $active = true;
786
+                    $info['groups'] = $enabled;
787
+                }
788
+
789
+                $info['active'] = $active;
790
+
791
+                if ($appManager->isShipped($app)) {
792
+                    $info['internal'] = true;
793
+                    $info['level'] = self::officialApp;
794
+                    $info['removable'] = false;
795
+                } else {
796
+                    $info['internal'] = false;
797
+                    $info['removable'] = true;
798
+                }
799
+
800
+                $appPath = self::getAppPath($app);
801
+                if($appPath !== false) {
802
+                    $appIcon = $appPath . '/img/' . $app . '.svg';
803
+                    if (file_exists($appIcon)) {
804
+                        $info['preview'] = $urlGenerator->imagePath($app, $app . '.svg');
805
+                        $info['previewAsIcon'] = true;
806
+                    } else {
807
+                        $appIcon = $appPath . '/img/app.svg';
808
+                        if (file_exists($appIcon)) {
809
+                            $info['preview'] = $urlGenerator->imagePath($app, 'app.svg');
810
+                            $info['previewAsIcon'] = true;
811
+                        }
812
+                    }
813
+                }
814
+                // fix documentation
815
+                if (isset($info['documentation']) && is_array($info['documentation'])) {
816
+                    foreach ($info['documentation'] as $key => $url) {
817
+                        // If it is not an absolute URL we assume it is a key
818
+                        // i.e. admin-ldap will get converted to go.php?to=admin-ldap
819
+                        if (stripos($url, 'https://') !== 0 && stripos($url, 'http://') !== 0) {
820
+                            $url = $urlGenerator->linkToDocs($url);
821
+                        }
822
+
823
+                        $info['documentation'][$key] = $url;
824
+                    }
825
+                }
826
+
827
+                $info['version'] = OC_App::getAppVersion($app);
828
+                $appList[] = $info;
829
+            }
830
+        }
831
+
832
+        return $appList;
833
+    }
834
+
835
+    public static function shouldUpgrade($app) {
836
+        $versions = self::getAppVersions();
837
+        $currentVersion = OC_App::getAppVersion($app);
838
+        if ($currentVersion && isset($versions[$app])) {
839
+            $installedVersion = $versions[$app];
840
+            if (!version_compare($currentVersion, $installedVersion, '=')) {
841
+                return true;
842
+            }
843
+        }
844
+        return false;
845
+    }
846
+
847
+    /**
848
+     * Adjust the number of version parts of $version1 to match
849
+     * the number of version parts of $version2.
850
+     *
851
+     * @param string $version1 version to adjust
852
+     * @param string $version2 version to take the number of parts from
853
+     * @return string shortened $version1
854
+     */
855
+    private static function adjustVersionParts($version1, $version2) {
856
+        $version1 = explode('.', $version1);
857
+        $version2 = explode('.', $version2);
858
+        // reduce $version1 to match the number of parts in $version2
859
+        while (count($version1) > count($version2)) {
860
+            array_pop($version1);
861
+        }
862
+        // if $version1 does not have enough parts, add some
863
+        while (count($version1) < count($version2)) {
864
+            $version1[] = '0';
865
+        }
866
+        return implode('.', $version1);
867
+    }
868
+
869
+    /**
870
+     * Check whether the current ownCloud version matches the given
871
+     * application's version requirements.
872
+     *
873
+     * The comparison is made based on the number of parts that the
874
+     * app info version has. For example for ownCloud 6.0.3 if the
875
+     * app info version is expecting version 6.0, the comparison is
876
+     * made on the first two parts of the ownCloud version.
877
+     * This means that it's possible to specify "requiremin" => 6
878
+     * and "requiremax" => 6 and it will still match ownCloud 6.0.3.
879
+     *
880
+     * @param string $ocVersion ownCloud version to check against
881
+     * @param array $appInfo app info (from xml)
882
+     *
883
+     * @return boolean true if compatible, otherwise false
884
+     */
885
+    public static function isAppCompatible($ocVersion, $appInfo) {
886
+        $requireMin = '';
887
+        $requireMax = '';
888
+        if (isset($appInfo['dependencies']['nextcloud']['@attributes']['min-version'])) {
889
+            $requireMin = $appInfo['dependencies']['nextcloud']['@attributes']['min-version'];
890
+        } elseif (isset($appInfo['dependencies']['owncloud']['@attributes']['min-version'])) {
891
+            $requireMin = $appInfo['dependencies']['owncloud']['@attributes']['min-version'];
892
+        } else if (isset($appInfo['requiremin'])) {
893
+            $requireMin = $appInfo['requiremin'];
894
+        } else if (isset($appInfo['require'])) {
895
+            $requireMin = $appInfo['require'];
896
+        }
897
+
898
+        if (isset($appInfo['dependencies']['nextcloud']['@attributes']['max-version'])) {
899
+            $requireMax = $appInfo['dependencies']['nextcloud']['@attributes']['max-version'];
900
+        } elseif (isset($appInfo['dependencies']['owncloud']['@attributes']['max-version'])) {
901
+            $requireMax = $appInfo['dependencies']['owncloud']['@attributes']['max-version'];
902
+        } else if (isset($appInfo['requiremax'])) {
903
+            $requireMax = $appInfo['requiremax'];
904
+        }
905
+
906
+        if (is_array($ocVersion)) {
907
+            $ocVersion = implode('.', $ocVersion);
908
+        }
909
+
910
+        if (!empty($requireMin)
911
+            && version_compare(self::adjustVersionParts($ocVersion, $requireMin), $requireMin, '<')
912
+        ) {
913
+
914
+            return false;
915
+        }
916
+
917
+        if (!empty($requireMax)
918
+            && version_compare(self::adjustVersionParts($ocVersion, $requireMax), $requireMax, '>')
919
+        ) {
920
+            return false;
921
+        }
922
+
923
+        return true;
924
+    }
925
+
926
+    /**
927
+     * get the installed version of all apps
928
+     */
929
+    public static function getAppVersions() {
930
+        static $versions;
931
+
932
+        if(!$versions) {
933
+            $appConfig = \OC::$server->getAppConfig();
934
+            $versions = $appConfig->getValues(false, 'installed_version');
935
+        }
936
+        return $versions;
937
+    }
938
+
939
+    /**
940
+     * update the database for the app and call the update script
941
+     *
942
+     * @param string $appId
943
+     * @return bool
944
+     */
945
+    public static function updateApp($appId) {
946
+        $appPath = self::getAppPath($appId);
947
+        if($appPath === false) {
948
+            return false;
949
+        }
950
+        self::registerAutoloading($appId, $appPath);
951
+
952
+        $appData = self::getAppInfo($appId);
953
+        self::executeRepairSteps($appId, $appData['repair-steps']['pre-migration']);
954
+
955
+        if (file_exists($appPath . '/appinfo/database.xml')) {
956
+            OC_DB::updateDbFromStructure($appPath . '/appinfo/database.xml');
957
+        } else {
958
+            $ms = new MigrationService($appId, \OC::$server->getDatabaseConnection());
959
+            $ms->migrate();
960
+        }
961
+
962
+        self::executeRepairSteps($appId, $appData['repair-steps']['post-migration']);
963
+        self::setupLiveMigrations($appId, $appData['repair-steps']['live-migration']);
964
+        // update appversion in app manager
965
+        \OC::$server->getAppManager()->getAppVersion($appId, false);
966
+
967
+        // run upgrade code
968
+        if (file_exists($appPath . '/appinfo/update.php')) {
969
+            self::loadApp($appId);
970
+            include $appPath . '/appinfo/update.php';
971
+        }
972
+        self::setupBackgroundJobs($appData['background-jobs']);
973
+
974
+        //set remote/public handlers
975
+        if (array_key_exists('ocsid', $appData)) {
976
+            \OC::$server->getConfig()->setAppValue($appId, 'ocsid', $appData['ocsid']);
977
+        } elseif(\OC::$server->getConfig()->getAppValue($appId, 'ocsid', null) !== null) {
978
+            \OC::$server->getConfig()->deleteAppValue($appId, 'ocsid');
979
+        }
980
+        foreach ($appData['remote'] as $name => $path) {
981
+            \OC::$server->getConfig()->setAppValue('core', 'remote_' . $name, $appId . '/' . $path);
982
+        }
983
+        foreach ($appData['public'] as $name => $path) {
984
+            \OC::$server->getConfig()->setAppValue('core', 'public_' . $name, $appId . '/' . $path);
985
+        }
986
+
987
+        self::setAppTypes($appId);
988
+
989
+        $version = \OC_App::getAppVersion($appId);
990
+        \OC::$server->getConfig()->setAppValue($appId, 'installed_version', $version);
991
+
992
+        \OC::$server->getEventDispatcher()->dispatch(ManagerEvent::EVENT_APP_UPDATE, new ManagerEvent(
993
+            ManagerEvent::EVENT_APP_UPDATE, $appId
994
+        ));
995
+
996
+        return true;
997
+    }
998
+
999
+    /**
1000
+     * @param string $appId
1001
+     * @param string[] $steps
1002
+     * @throws \OC\NeedsUpdateException
1003
+     */
1004
+    public static function executeRepairSteps($appId, array $steps) {
1005
+        if (empty($steps)) {
1006
+            return;
1007
+        }
1008
+        // load the app
1009
+        self::loadApp($appId);
1010
+
1011
+        $dispatcher = OC::$server->getEventDispatcher();
1012
+
1013
+        // load the steps
1014
+        $r = new Repair([], $dispatcher);
1015
+        foreach ($steps as $step) {
1016
+            try {
1017
+                $r->addStep($step);
1018
+            } catch (Exception $ex) {
1019
+                $r->emit('\OC\Repair', 'error', [$ex->getMessage()]);
1020
+                \OC::$server->getLogger()->logException($ex);
1021
+            }
1022
+        }
1023
+        // run the steps
1024
+        $r->run();
1025
+    }
1026
+
1027
+    public static function setupBackgroundJobs(array $jobs) {
1028
+        $queue = \OC::$server->getJobList();
1029
+        foreach ($jobs as $job) {
1030
+            $queue->add($job);
1031
+        }
1032
+    }
1033
+
1034
+    /**
1035
+     * @param string $appId
1036
+     * @param string[] $steps
1037
+     */
1038
+    private static function setupLiveMigrations($appId, array $steps) {
1039
+        $queue = \OC::$server->getJobList();
1040
+        foreach ($steps as $step) {
1041
+            $queue->add('OC\Migration\BackgroundRepair', [
1042
+                'app' => $appId,
1043
+                'step' => $step]);
1044
+        }
1045
+    }
1046
+
1047
+    /**
1048
+     * @param string $appId
1049
+     * @return \OC\Files\View|false
1050
+     */
1051
+    public static function getStorage($appId) {
1052
+        if (\OC::$server->getAppManager()->isEnabledForUser($appId)) { //sanity check
1053
+            if (\OC::$server->getUserSession()->isLoggedIn()) {
1054
+                $view = new \OC\Files\View('/' . OC_User::getUser());
1055
+                if (!$view->file_exists($appId)) {
1056
+                    $view->mkdir($appId);
1057
+                }
1058
+                return new \OC\Files\View('/' . OC_User::getUser() . '/' . $appId);
1059
+            } else {
1060
+                \OCP\Util::writeLog('core', 'Can\'t get app storage, app ' . $appId . ', user not logged in', \OCP\Util::ERROR);
1061
+                return false;
1062
+            }
1063
+        } else {
1064
+            \OCP\Util::writeLog('core', 'Can\'t get app storage, app ' . $appId . ' not enabled', \OCP\Util::ERROR);
1065
+            return false;
1066
+        }
1067
+    }
1068
+
1069
+    protected static function findBestL10NOption($options, $lang) {
1070
+        $fallback = $similarLangFallback = $englishFallback = false;
1071
+
1072
+        $lang = strtolower($lang);
1073
+        $similarLang = $lang;
1074
+        if (strpos($similarLang, '_')) {
1075
+            // For "de_DE" we want to find "de" and the other way around
1076
+            $similarLang = substr($lang, 0, strpos($lang, '_'));
1077
+        }
1078
+
1079
+        foreach ($options as $option) {
1080
+            if (is_array($option)) {
1081
+                if ($fallback === false) {
1082
+                    $fallback = $option['@value'];
1083
+                }
1084
+
1085
+                if (!isset($option['@attributes']['lang'])) {
1086
+                    continue;
1087
+                }
1088
+
1089
+                $attributeLang = strtolower($option['@attributes']['lang']);
1090
+                if ($attributeLang === $lang) {
1091
+                    return $option['@value'];
1092
+                }
1093
+
1094
+                if ($attributeLang === $similarLang) {
1095
+                    $similarLangFallback = $option['@value'];
1096
+                } else if (strpos($attributeLang, $similarLang . '_') === 0) {
1097
+                    if ($similarLangFallback === false) {
1098
+                        $similarLangFallback =  $option['@value'];
1099
+                    }
1100
+                }
1101
+            } else {
1102
+                $englishFallback = $option;
1103
+            }
1104
+        }
1105
+
1106
+        if ($similarLangFallback !== false) {
1107
+            return $similarLangFallback;
1108
+        } else if ($englishFallback !== false) {
1109
+            return $englishFallback;
1110
+        }
1111
+        return (string) $fallback;
1112
+    }
1113
+
1114
+    /**
1115
+     * parses the app data array and enhanced the 'description' value
1116
+     *
1117
+     * @param array $data the app data
1118
+     * @param string $lang
1119
+     * @return array improved app data
1120
+     */
1121
+    public static function parseAppInfo(array $data, $lang = null) {
1122
+
1123
+        if ($lang && isset($data['name']) && is_array($data['name'])) {
1124
+            $data['name'] = self::findBestL10NOption($data['name'], $lang);
1125
+        }
1126
+        if ($lang && isset($data['summary']) && is_array($data['summary'])) {
1127
+            $data['summary'] = self::findBestL10NOption($data['summary'], $lang);
1128
+        }
1129
+        if ($lang && isset($data['description']) && is_array($data['description'])) {
1130
+            $data['description'] = trim(self::findBestL10NOption($data['description'], $lang));
1131
+        } else if (isset($data['description']) && is_string($data['description'])) {
1132
+            $data['description'] = trim($data['description']);
1133
+        } else  {
1134
+            $data['description'] = '';
1135
+        }
1136
+
1137
+        return $data;
1138
+    }
1139
+
1140
+    /**
1141
+     * @param \OCP\IConfig $config
1142
+     * @param \OCP\IL10N $l
1143
+     * @param array $info
1144
+     * @throws \Exception
1145
+     */
1146
+    public static function checkAppDependencies($config, $l, $info) {
1147
+        $dependencyAnalyzer = new DependencyAnalyzer(new Platform($config), $l);
1148
+        $missing = $dependencyAnalyzer->analyze($info);
1149
+        if (!empty($missing)) {
1150
+            $missingMsg = implode(PHP_EOL, $missing);
1151
+            throw new \Exception(
1152
+                $l->t('App "%s" cannot be installed because the following dependencies are not fulfilled: %s',
1153
+                    [$info['name'], $missingMsg]
1154
+                )
1155
+            );
1156
+        }
1157
+    }
1158 1158
 }
Please login to merge, or discard this patch.
Spacing   +49 added lines, -49 removed lines patch added patch discarded remove patch
@@ -110,9 +110,9 @@  discard block
 block discarded – undo
110 110
 		$apps = self::getEnabledApps();
111 111
 
112 112
 		// Add each apps' folder as allowed class path
113
-		foreach($apps as $app) {
113
+		foreach ($apps as $app) {
114 114
 			$path = self::getAppPath($app);
115
-			if($path !== false) {
115
+			if ($path !== false) {
116 116
 				self::registerAutoloading($app, $path);
117 117
 			}
118 118
 		}
@@ -137,15 +137,15 @@  discard block
 block discarded – undo
137 137
 	public static function loadApp($app) {
138 138
 		self::$loadedApps[] = $app;
139 139
 		$appPath = self::getAppPath($app);
140
-		if($appPath === false) {
140
+		if ($appPath === false) {
141 141
 			return;
142 142
 		}
143 143
 
144 144
 		// in case someone calls loadApp() directly
145 145
 		self::registerAutoloading($app, $appPath);
146 146
 
147
-		if (is_file($appPath . '/appinfo/app.php')) {
148
-			\OC::$server->getEventLogger()->start('load_app_' . $app, 'Load app: ' . $app);
147
+		if (is_file($appPath.'/appinfo/app.php')) {
148
+			\OC::$server->getEventLogger()->start('load_app_'.$app, 'Load app: '.$app);
149 149
 			self::requireAppFile($app);
150 150
 			if (self::isType($app, array('authentication'))) {
151 151
 				// since authentication apps affect the "is app enabled for group" check,
@@ -154,7 +154,7 @@  discard block
 block discarded – undo
154 154
 				// enabled for groups
155 155
 				self::$enabledAppsCache = array();
156 156
 			}
157
-			\OC::$server->getEventLogger()->end('load_app_' . $app);
157
+			\OC::$server->getEventLogger()->end('load_app_'.$app);
158 158
 		}
159 159
 
160 160
 		$info = self::getAppInfo($app);
@@ -200,7 +200,7 @@  discard block
 block discarded – undo
200 200
 			$plugins = isset($info['collaboration']['plugins']['plugin']['@value']) ?
201 201
 				[$info['collaboration']['plugins']['plugin']] : $info['collaboration']['plugins']['plugin'];
202 202
 			foreach ($plugins as $plugin) {
203
-				if($plugin['@attributes']['type'] === 'collaborator-search') {
203
+				if ($plugin['@attributes']['type'] === 'collaborator-search') {
204 204
 					$pluginInfo = [
205 205
 						'shareType' => $plugin['@attributes']['share-type'],
206 206
 						'class' => $plugin['@value'],
@@ -219,8 +219,8 @@  discard block
 block discarded – undo
219 219
 	 * @param string $path
220 220
 	 */
221 221
 	public static function registerAutoloading($app, $path) {
222
-		$key = $app . '-' . $path;
223
-		if(isset(self::$alreadyRegistered[$key])) {
222
+		$key = $app.'-'.$path;
223
+		if (isset(self::$alreadyRegistered[$key])) {
224 224
 			return;
225 225
 		}
226 226
 
@@ -230,17 +230,17 @@  discard block
 block discarded – undo
230 230
 		$appNamespace = \OC\AppFramework\App::buildAppNamespace($app);
231 231
 		\OC::$server->registerNamespace($app, $appNamespace);
232 232
 
233
-		if (file_exists($path . '/composer/autoload.php')) {
234
-			require_once $path . '/composer/autoload.php';
233
+		if (file_exists($path.'/composer/autoload.php')) {
234
+			require_once $path.'/composer/autoload.php';
235 235
 		} else {
236
-			\OC::$composerAutoloader->addPsr4($appNamespace . '\\', $path . '/lib/', true);
236
+			\OC::$composerAutoloader->addPsr4($appNamespace.'\\', $path.'/lib/', true);
237 237
 			// Register on legacy autoloader
238 238
 			\OC::$loader->addValidRoot($path);
239 239
 		}
240 240
 
241 241
 		// Register Test namespace only when testing
242 242
 		if (defined('PHPUNIT_RUN') || defined('CLI_TEST_RUN')) {
243
-			\OC::$composerAutoloader->addPsr4($appNamespace . '\\Tests\\', $path . '/tests/', true);
243
+			\OC::$composerAutoloader->addPsr4($appNamespace.'\\Tests\\', $path.'/tests/', true);
244 244
 		}
245 245
 	}
246 246
 
@@ -252,7 +252,7 @@  discard block
 block discarded – undo
252 252
 	private static function requireAppFile($app) {
253 253
 		try {
254 254
 			// encapsulated here to avoid variable scope conflicts
255
-			require_once $app . '/appinfo/app.php';
255
+			require_once $app.'/appinfo/app.php';
256 256
 		} catch (Error $ex) {
257 257
 			\OC::$server->getLogger()->logException($ex);
258 258
 			if (!\OC::$server->getAppManager()->isShipped($app)) {
@@ -306,7 +306,7 @@  discard block
 block discarded – undo
306 306
 	 */
307 307
 	public static function setAppTypes($app) {
308 308
 		$appData = self::getAppInfo($app);
309
-		if(!is_array($appData)) {
309
+		if (!is_array($appData)) {
310 310
 			return;
311 311
 		}
312 312
 
@@ -358,8 +358,8 @@  discard block
 block discarded – undo
358 358
 		} else {
359 359
 			$apps = $appManager->getEnabledAppsForUser($user);
360 360
 		}
361
-		$apps = array_filter($apps, function ($app) {
362
-			return $app !== 'files';//we add this manually
361
+		$apps = array_filter($apps, function($app) {
362
+			return $app !== 'files'; //we add this manually
363 363
 		});
364 364
 		sort($apps);
365 365
 		array_unshift($apps, 'files');
@@ -397,7 +397,7 @@  discard block
 block discarded – undo
397 397
 		$installer = \OC::$server->query(Installer::class);
398 398
 		$isDownloaded = $installer->isDownloaded($appId);
399 399
 
400
-		if(!$isDownloaded) {
400
+		if (!$isDownloaded) {
401 401
 			$installer->downloadApp($appId);
402 402
 		}
403 403
 
@@ -497,7 +497,7 @@  discard block
 block discarded – undo
497 497
 	 */
498 498
 	public static function findAppInDirectories($appId) {
499 499
 		$sanitizedAppId = self::cleanAppId($appId);
500
-		if($sanitizedAppId !== $appId) {
500
+		if ($sanitizedAppId !== $appId) {
501 501
 			return false;
502 502
 		}
503 503
 		static $app_dir = array();
@@ -508,7 +508,7 @@  discard block
 block discarded – undo
508 508
 
509 509
 		$possibleApps = array();
510 510
 		foreach (OC::$APPSROOTS as $dir) {
511
-			if (file_exists($dir['path'] . '/' . $appId)) {
511
+			if (file_exists($dir['path'].'/'.$appId)) {
512 512
 				$possibleApps[] = $dir;
513 513
 			}
514 514
 		}
@@ -549,7 +549,7 @@  discard block
 block discarded – undo
549 549
 		}
550 550
 
551 551
 		if (($dir = self::findAppInDirectories($appId)) != false) {
552
-			return $dir['path'] . '/' . $appId;
552
+			return $dir['path'].'/'.$appId;
553 553
 		}
554 554
 		return false;
555 555
 	}
@@ -563,7 +563,7 @@  discard block
 block discarded – undo
563 563
 	 */
564 564
 	public static function getAppWebPath($appId) {
565 565
 		if (($dir = self::findAppInDirectories($appId)) != false) {
566
-			return OC::$WEBROOT . $dir['url'] . '/' . $appId;
566
+			return OC::$WEBROOT.$dir['url'].'/'.$appId;
567 567
 		}
568 568
 		return false;
569 569
 	}
@@ -587,7 +587,7 @@  discard block
 block discarded – undo
587 587
 	 * @return string
588 588
 	 */
589 589
 	public static function getAppVersionByPath($path) {
590
-		$infoFile = $path . '/appinfo/info.xml';
590
+		$infoFile = $path.'/appinfo/info.xml';
591 591
 		$appData = \OC::$server->getAppManager()->getAppInfo($infoFile, true);
592 592
 		return isset($appData['version']) ? $appData['version'] : '';
593 593
 	}
@@ -687,7 +687,7 @@  discard block
 block discarded – undo
687 687
 	 * @param string $page
688 688
 	 */
689 689
 	public static function registerAdmin($app, $page) {
690
-		self::$adminForms[] = $app . '/' . $page . '.php';
690
+		self::$adminForms[] = $app.'/'.$page.'.php';
691 691
 	}
692 692
 
693 693
 	/**
@@ -696,7 +696,7 @@  discard block
 block discarded – undo
696 696
 	 * @param string $page
697 697
 	 */
698 698
 	public static function registerPersonal($app, $page) {
699
-		self::$personalForms[] = $app . '/' . $page . '.php';
699
+		self::$personalForms[] = $app.'/'.$page.'.php';
700 700
 	}
701 701
 
702 702
 	/**
@@ -725,7 +725,7 @@  discard block
 block discarded – undo
725 725
 
726 726
 		foreach (OC::$APPSROOTS as $apps_dir) {
727 727
 			if (!is_readable($apps_dir['path'])) {
728
-				\OCP\Util::writeLog('core', 'unable to read app folder : ' . $apps_dir['path'], \OCP\Util::WARN);
728
+				\OCP\Util::writeLog('core', 'unable to read app folder : '.$apps_dir['path'], \OCP\Util::WARN);
729 729
 				continue;
730 730
 			}
731 731
 			$dh = opendir($apps_dir['path']);
@@ -733,7 +733,7 @@  discard block
 block discarded – undo
733 733
 			if (is_resource($dh)) {
734 734
 				while (($file = readdir($dh)) !== false) {
735 735
 
736
-					if ($file[0] != '.' and is_dir($apps_dir['path'] . '/' . $file) and is_file($apps_dir['path'] . '/' . $file . '/appinfo/info.xml')) {
736
+					if ($file[0] != '.' and is_dir($apps_dir['path'].'/'.$file) and is_file($apps_dir['path'].'/'.$file.'/appinfo/info.xml')) {
737 737
 
738 738
 						$apps[] = $file;
739 739
 					}
@@ -766,12 +766,12 @@  discard block
 block discarded – undo
766 766
 
767 767
 				$info = OC_App::getAppInfo($app, false, $langCode);
768 768
 				if (!is_array($info)) {
769
-					\OCP\Util::writeLog('core', 'Could not read app info file for app "' . $app . '"', \OCP\Util::ERROR);
769
+					\OCP\Util::writeLog('core', 'Could not read app info file for app "'.$app.'"', \OCP\Util::ERROR);
770 770
 					continue;
771 771
 				}
772 772
 
773 773
 				if (!isset($info['name'])) {
774
-					\OCP\Util::writeLog('core', 'App id "' . $app . '" has no name in appinfo', \OCP\Util::ERROR);
774
+					\OCP\Util::writeLog('core', 'App id "'.$app.'" has no name in appinfo', \OCP\Util::ERROR);
775 775
 					continue;
776 776
 				}
777 777
 
@@ -798,13 +798,13 @@  discard block
 block discarded – undo
798 798
 				}
799 799
 
800 800
 				$appPath = self::getAppPath($app);
801
-				if($appPath !== false) {
802
-					$appIcon = $appPath . '/img/' . $app . '.svg';
801
+				if ($appPath !== false) {
802
+					$appIcon = $appPath.'/img/'.$app.'.svg';
803 803
 					if (file_exists($appIcon)) {
804
-						$info['preview'] = $urlGenerator->imagePath($app, $app . '.svg');
804
+						$info['preview'] = $urlGenerator->imagePath($app, $app.'.svg');
805 805
 						$info['previewAsIcon'] = true;
806 806
 					} else {
807
-						$appIcon = $appPath . '/img/app.svg';
807
+						$appIcon = $appPath.'/img/app.svg';
808 808
 						if (file_exists($appIcon)) {
809 809
 							$info['preview'] = $urlGenerator->imagePath($app, 'app.svg');
810 810
 							$info['previewAsIcon'] = true;
@@ -929,7 +929,7 @@  discard block
 block discarded – undo
929 929
 	public static function getAppVersions() {
930 930
 		static $versions;
931 931
 
932
-		if(!$versions) {
932
+		if (!$versions) {
933 933
 			$appConfig = \OC::$server->getAppConfig();
934 934
 			$versions = $appConfig->getValues(false, 'installed_version');
935 935
 		}
@@ -944,7 +944,7 @@  discard block
 block discarded – undo
944 944
 	 */
945 945
 	public static function updateApp($appId) {
946 946
 		$appPath = self::getAppPath($appId);
947
-		if($appPath === false) {
947
+		if ($appPath === false) {
948 948
 			return false;
949 949
 		}
950 950
 		self::registerAutoloading($appId, $appPath);
@@ -952,8 +952,8 @@  discard block
 block discarded – undo
952 952
 		$appData = self::getAppInfo($appId);
953 953
 		self::executeRepairSteps($appId, $appData['repair-steps']['pre-migration']);
954 954
 
955
-		if (file_exists($appPath . '/appinfo/database.xml')) {
956
-			OC_DB::updateDbFromStructure($appPath . '/appinfo/database.xml');
955
+		if (file_exists($appPath.'/appinfo/database.xml')) {
956
+			OC_DB::updateDbFromStructure($appPath.'/appinfo/database.xml');
957 957
 		} else {
958 958
 			$ms = new MigrationService($appId, \OC::$server->getDatabaseConnection());
959 959
 			$ms->migrate();
@@ -965,23 +965,23 @@  discard block
 block discarded – undo
965 965
 		\OC::$server->getAppManager()->getAppVersion($appId, false);
966 966
 
967 967
 		// run upgrade code
968
-		if (file_exists($appPath . '/appinfo/update.php')) {
968
+		if (file_exists($appPath.'/appinfo/update.php')) {
969 969
 			self::loadApp($appId);
970
-			include $appPath . '/appinfo/update.php';
970
+			include $appPath.'/appinfo/update.php';
971 971
 		}
972 972
 		self::setupBackgroundJobs($appData['background-jobs']);
973 973
 
974 974
 		//set remote/public handlers
975 975
 		if (array_key_exists('ocsid', $appData)) {
976 976
 			\OC::$server->getConfig()->setAppValue($appId, 'ocsid', $appData['ocsid']);
977
-		} elseif(\OC::$server->getConfig()->getAppValue($appId, 'ocsid', null) !== null) {
977
+		} elseif (\OC::$server->getConfig()->getAppValue($appId, 'ocsid', null) !== null) {
978 978
 			\OC::$server->getConfig()->deleteAppValue($appId, 'ocsid');
979 979
 		}
980 980
 		foreach ($appData['remote'] as $name => $path) {
981
-			\OC::$server->getConfig()->setAppValue('core', 'remote_' . $name, $appId . '/' . $path);
981
+			\OC::$server->getConfig()->setAppValue('core', 'remote_'.$name, $appId.'/'.$path);
982 982
 		}
983 983
 		foreach ($appData['public'] as $name => $path) {
984
-			\OC::$server->getConfig()->setAppValue('core', 'public_' . $name, $appId . '/' . $path);
984
+			\OC::$server->getConfig()->setAppValue('core', 'public_'.$name, $appId.'/'.$path);
985 985
 		}
986 986
 
987 987
 		self::setAppTypes($appId);
@@ -1051,17 +1051,17 @@  discard block
 block discarded – undo
1051 1051
 	public static function getStorage($appId) {
1052 1052
 		if (\OC::$server->getAppManager()->isEnabledForUser($appId)) { //sanity check
1053 1053
 			if (\OC::$server->getUserSession()->isLoggedIn()) {
1054
-				$view = new \OC\Files\View('/' . OC_User::getUser());
1054
+				$view = new \OC\Files\View('/'.OC_User::getUser());
1055 1055
 				if (!$view->file_exists($appId)) {
1056 1056
 					$view->mkdir($appId);
1057 1057
 				}
1058
-				return new \OC\Files\View('/' . OC_User::getUser() . '/' . $appId);
1058
+				return new \OC\Files\View('/'.OC_User::getUser().'/'.$appId);
1059 1059
 			} else {
1060
-				\OCP\Util::writeLog('core', 'Can\'t get app storage, app ' . $appId . ', user not logged in', \OCP\Util::ERROR);
1060
+				\OCP\Util::writeLog('core', 'Can\'t get app storage, app '.$appId.', user not logged in', \OCP\Util::ERROR);
1061 1061
 				return false;
1062 1062
 			}
1063 1063
 		} else {
1064
-			\OCP\Util::writeLog('core', 'Can\'t get app storage, app ' . $appId . ' not enabled', \OCP\Util::ERROR);
1064
+			\OCP\Util::writeLog('core', 'Can\'t get app storage, app '.$appId.' not enabled', \OCP\Util::ERROR);
1065 1065
 			return false;
1066 1066
 		}
1067 1067
 	}
@@ -1093,9 +1093,9 @@  discard block
 block discarded – undo
1093 1093
 
1094 1094
 				if ($attributeLang === $similarLang) {
1095 1095
 					$similarLangFallback = $option['@value'];
1096
-				} else if (strpos($attributeLang, $similarLang . '_') === 0) {
1096
+				} else if (strpos($attributeLang, $similarLang.'_') === 0) {
1097 1097
 					if ($similarLangFallback === false) {
1098
-						$similarLangFallback =  $option['@value'];
1098
+						$similarLangFallback = $option['@value'];
1099 1099
 					}
1100 1100
 				}
1101 1101
 			} else {
@@ -1130,7 +1130,7 @@  discard block
 block discarded – undo
1130 1130
 			$data['description'] = trim(self::findBestL10NOption($data['description'], $lang));
1131 1131
 		} else if (isset($data['description']) && is_string($data['description'])) {
1132 1132
 			$data['description'] = trim($data['description']);
1133
-		} else  {
1133
+		} else {
1134 1134
 			$data['description'] = '';
1135 1135
 		}
1136 1136
 
Please login to merge, or discard this patch.
apps/files_trashbin/lib/Storage.php 1 patch
Indentation   +279 added lines, -279 removed lines patch added patch discarded remove patch
@@ -40,284 +40,284 @@
 block discarded – undo
40 40
 
41 41
 class Storage extends Wrapper {
42 42
 
43
-	private $mountPoint;
44
-	// remember already deleted files to avoid infinite loops if the trash bin
45
-	// move files across storages
46
-	private $deletedFiles = array();
47
-
48
-	/**
49
-	 * Disable trash logic
50
-	 *
51
-	 * @var bool
52
-	 */
53
-	private static $disableTrash = false;
54
-
55
-	/**
56
-	 * remember which file/folder was moved out of s shared folder
57
-	 * in this case we want to add a copy to the owners trash bin
58
-	 *
59
-	 * @var array
60
-	 */
61
-	private static $moveOutOfSharedFolder = [];
62
-
63
-	/** @var  IUserManager */
64
-	private $userManager;
65
-
66
-	/** @var ILogger */
67
-	private $logger;
68
-
69
-	/** @var EventDispatcher */
70
-	private $eventDispatcher;
71
-
72
-	/** @var IRootFolder */
73
-	private $rootFolder;
74
-
75
-	/**
76
-	 * Storage constructor.
77
-	 *
78
-	 * @param array $parameters
79
-	 * @param IUserManager|null $userManager
80
-	 * @param ILogger|null $logger
81
-	 * @param EventDispatcher|null $eventDispatcher
82
-	 * @param IRootFolder|null $rootFolder
83
-	 */
84
-	public function __construct($parameters,
85
-								IUserManager $userManager = null,
86
-								ILogger $logger = null,
87
-								EventDispatcher $eventDispatcher = null,
88
-								IRootFolder $rootFolder = null) {
89
-		$this->mountPoint = $parameters['mountPoint'];
90
-		$this->userManager = $userManager;
91
-		$this->logger = $logger;
92
-		$this->eventDispatcher = $eventDispatcher;
93
-		$this->rootFolder = $rootFolder;
94
-		parent::__construct($parameters);
95
-	}
96
-
97
-	/**
98
-	 * @internal
99
-	 */
100
-	public static function preRenameHook($params) {
101
-		// in cross-storage cases, a rename is a copy + unlink,
102
-		// that last unlink must not go to trash, only exception:
103
-		// if the file was moved from a shared storage to a local folder,
104
-		// in this case the owner should get a copy in his trash bin so that
105
-		// they can restore the files again
106
-
107
-		$oldPath = $params['oldpath'];
108
-		$newPath = dirname($params['newpath']);
109
-		$currentUser = \OC::$server->getUserSession()->getUser();
110
-
111
-		$fileMovedOutOfSharedFolder = false;
112
-
113
-		try {
114
-			if ($currentUser) {
115
-				$currentUserId = $currentUser->getUID();
116
-
117
-				$view = new View($currentUserId . '/files');
118
-				$fileInfo = $view->getFileInfo($oldPath);
119
-				if ($fileInfo) {
120
-					$sourceStorage = $fileInfo->getStorage();
121
-					$sourceOwner = $view->getOwner($oldPath);
122
-					$targetOwner = $view->getOwner($newPath);
123
-
124
-					if ($sourceOwner !== $targetOwner
125
-						&& $sourceStorage->instanceOfStorage('OCA\Files_Sharing\SharedStorage')
126
-					) {
127
-						$fileMovedOutOfSharedFolder = true;
128
-					}
129
-				}
130
-			}
131
-		} catch (\Exception $e) {
132
-			// do nothing, in this case we just disable the trashbin and continue
133
-			\OC::$server->getLogger()->logException($e, [
134
-				'message' => 'Trashbin storage could not check if a file was moved out of a shared folder.',
135
-				'level' => \OCP\Util::DEBUG,
136
-				'app' => 'files_trashbin',
137
-			]);
138
-		}
139
-
140
-		if($fileMovedOutOfSharedFolder) {
141
-			self::$moveOutOfSharedFolder['/' . $currentUserId . '/files' . $oldPath] = true;
142
-		} else {
143
-			self::$disableTrash = true;
144
-		}
145
-
146
-	}
147
-
148
-	/**
149
-	 * @internal
150
-	 */
151
-	public static function postRenameHook($params) {
152
-		self::$disableTrash = false;
153
-	}
154
-
155
-	/**
156
-	 * Rename path1 to path2 by calling the wrapped storage.
157
-	 *
158
-	 * @param string $path1 first path
159
-	 * @param string $path2 second path
160
-	 * @return bool
161
-	 */
162
-	public function rename($path1, $path2) {
163
-		$result = $this->storage->rename($path1, $path2);
164
-		if ($result === false) {
165
-			// when rename failed, the post_rename hook isn't triggered,
166
-			// but we still want to reenable the trash logic
167
-			self::$disableTrash = false;
168
-		}
169
-		return $result;
170
-	}
171
-
172
-	/**
173
-	 * Deletes the given file by moving it into the trashbin.
174
-	 *
175
-	 * @param string $path path of file or folder to delete
176
-	 *
177
-	 * @return bool true if the operation succeeded, false otherwise
178
-	 */
179
-	public function unlink($path) {
180
-		try {
181
-			if (isset(self::$moveOutOfSharedFolder[$this->mountPoint . $path])) {
182
-				$result = $this->doDelete($path, 'unlink', true);
183
-				unset(self::$moveOutOfSharedFolder[$this->mountPoint . $path]);
184
-			} else {
185
-				$result = $this->doDelete($path, 'unlink');
186
-			}
187
-		} catch (GenericEncryptionException $e) {
188
-			// in case of a encryption exception we delete the file right away
189
-			$this->logger->info(
190
-				"Can't move file" .  $path .
191
-				"to the trash bin, therefore it was deleted right away");
192
-
193
-			$result = $this->storage->unlink($path);
194
-		}
195
-
196
-		return $result;
197
-	}
198
-
199
-	/**
200
-	 * Deletes the given folder by moving it into the trashbin.
201
-	 *
202
-	 * @param string $path path of folder to delete
203
-	 *
204
-	 * @return bool true if the operation succeeded, false otherwise
205
-	 */
206
-	public function rmdir($path) {
207
-		if (isset(self::$moveOutOfSharedFolder[$this->mountPoint . $path])) {
208
-			$result = $this->doDelete($path, 'rmdir', true);
209
-			unset(self::$moveOutOfSharedFolder[$this->mountPoint . $path]);
210
-		} else {
211
-			$result = $this->doDelete($path, 'rmdir');
212
-		}
213
-
214
-		return $result;
215
-	}
216
-
217
-	/**
218
-	 * check if it is a file located in data/user/files only files in the
219
-	 * 'files' directory should be moved to the trash
220
-	 *
221
-	 * @param $path
222
-	 * @return bool
223
-	 */
224
-	protected function shouldMoveToTrash($path){
225
-
226
-		// check if there is a app which want to disable the trash bin for this file
227
-		$fileId = $this->storage->getCache()->getId($path);
228
-		$nodes = $this->rootFolder->getById($fileId);
229
-		foreach ($nodes as $node) {
230
-			$event = $this->createMoveToTrashEvent($node);
231
-			$this->eventDispatcher->dispatch('OCA\Files_Trashbin::moveToTrash', $event);
232
-			if ($event->shouldMoveToTrashBin() === false) {
233
-				return false;
234
-			}
235
-		}
236
-
237
-		$normalized = Filesystem::normalizePath($this->mountPoint . '/' . $path);
238
-		$parts = explode('/', $normalized);
239
-		if (count($parts) < 4) {
240
-			return false;
241
-		}
242
-
243
-		if ($parts[2] === 'files' && $this->userManager->userExists($parts[1])) {
244
-			return true;
245
-		}
246
-
247
-		return false;
248
-	}
249
-
250
-	/**
251
-	 * get move to trash event
252
-	 *
253
-	 * @param Node $node
254
-	 * @return MoveToTrashEvent
255
-	 */
256
-	protected function createMoveToTrashEvent(Node $node) {
257
-		return new MoveToTrashEvent($node);
258
-	}
259
-
260
-	/**
261
-	 * Run the delete operation with the given method
262
-	 *
263
-	 * @param string $path path of file or folder to delete
264
-	 * @param string $method either "unlink" or "rmdir"
265
-	 * @param bool $ownerOnly delete for owner only (if file gets moved out of a shared folder)
266
-	 *
267
-	 * @return bool true if the operation succeeded, false otherwise
268
-	 */
269
-	private function doDelete($path, $method, $ownerOnly = false) {
270
-		if (self::$disableTrash
271
-			|| !\OC::$server->getAppManager()->isEnabledForUser('files_trashbin')
272
-			|| (pathinfo($path, PATHINFO_EXTENSION) === 'part')
273
-			|| $this->shouldMoveToTrash($path) === false
274
-		) {
275
-			return call_user_func_array([$this->storage, $method], [$path]);
276
-		}
277
-
278
-		// check permissions before we continue, this is especially important for
279
-		// shared files
280
-		if (!$this->isDeletable($path)) {
281
-			return false;
282
-		}
283
-
284
-		$normalized = Filesystem::normalizePath($this->mountPoint . '/' . $path, true, false, true);
285
-		$result = true;
286
-		$view = Filesystem::getView();
287
-		if (!isset($this->deletedFiles[$normalized]) && $view instanceof View) {
288
-			$this->deletedFiles[$normalized] = $normalized;
289
-			if ($filesPath = $view->getRelativePath($normalized)) {
290
-				$filesPath = trim($filesPath, '/');
291
-				$result = \OCA\Files_Trashbin\Trashbin::move2trash($filesPath, $ownerOnly);
292
-				// in cross-storage cases the file will be copied
293
-				// but not deleted, so we delete it here
294
-				if ($result) {
295
-					call_user_func_array([$this->storage, $method], [$path]);
296
-				}
297
-			} else {
298
-				$result = call_user_func_array([$this->storage, $method], [$path]);
299
-			}
300
-			unset($this->deletedFiles[$normalized]);
301
-		} else if ($this->storage->file_exists($path)) {
302
-			$result = call_user_func_array([$this->storage, $method], [$path]);
303
-		}
304
-
305
-		return $result;
306
-	}
307
-
308
-	/**
309
-	 * Setup the storate wrapper callback
310
-	 */
311
-	public static function setupStorage() {
312
-		\OC\Files\Filesystem::addStorageWrapper('oc_trashbin', function ($mountPoint, $storage) {
313
-			return new \OCA\Files_Trashbin\Storage(
314
-				array('storage' => $storage, 'mountPoint' => $mountPoint),
315
-				\OC::$server->getUserManager(),
316
-				\OC::$server->getLogger(),
317
-				\OC::$server->getEventDispatcher(),
318
-				\OC::$server->getLazyRootFolder()
319
-			);
320
-		}, 1);
321
-	}
43
+    private $mountPoint;
44
+    // remember already deleted files to avoid infinite loops if the trash bin
45
+    // move files across storages
46
+    private $deletedFiles = array();
47
+
48
+    /**
49
+     * Disable trash logic
50
+     *
51
+     * @var bool
52
+     */
53
+    private static $disableTrash = false;
54
+
55
+    /**
56
+     * remember which file/folder was moved out of s shared folder
57
+     * in this case we want to add a copy to the owners trash bin
58
+     *
59
+     * @var array
60
+     */
61
+    private static $moveOutOfSharedFolder = [];
62
+
63
+    /** @var  IUserManager */
64
+    private $userManager;
65
+
66
+    /** @var ILogger */
67
+    private $logger;
68
+
69
+    /** @var EventDispatcher */
70
+    private $eventDispatcher;
71
+
72
+    /** @var IRootFolder */
73
+    private $rootFolder;
74
+
75
+    /**
76
+     * Storage constructor.
77
+     *
78
+     * @param array $parameters
79
+     * @param IUserManager|null $userManager
80
+     * @param ILogger|null $logger
81
+     * @param EventDispatcher|null $eventDispatcher
82
+     * @param IRootFolder|null $rootFolder
83
+     */
84
+    public function __construct($parameters,
85
+                                IUserManager $userManager = null,
86
+                                ILogger $logger = null,
87
+                                EventDispatcher $eventDispatcher = null,
88
+                                IRootFolder $rootFolder = null) {
89
+        $this->mountPoint = $parameters['mountPoint'];
90
+        $this->userManager = $userManager;
91
+        $this->logger = $logger;
92
+        $this->eventDispatcher = $eventDispatcher;
93
+        $this->rootFolder = $rootFolder;
94
+        parent::__construct($parameters);
95
+    }
96
+
97
+    /**
98
+     * @internal
99
+     */
100
+    public static function preRenameHook($params) {
101
+        // in cross-storage cases, a rename is a copy + unlink,
102
+        // that last unlink must not go to trash, only exception:
103
+        // if the file was moved from a shared storage to a local folder,
104
+        // in this case the owner should get a copy in his trash bin so that
105
+        // they can restore the files again
106
+
107
+        $oldPath = $params['oldpath'];
108
+        $newPath = dirname($params['newpath']);
109
+        $currentUser = \OC::$server->getUserSession()->getUser();
110
+
111
+        $fileMovedOutOfSharedFolder = false;
112
+
113
+        try {
114
+            if ($currentUser) {
115
+                $currentUserId = $currentUser->getUID();
116
+
117
+                $view = new View($currentUserId . '/files');
118
+                $fileInfo = $view->getFileInfo($oldPath);
119
+                if ($fileInfo) {
120
+                    $sourceStorage = $fileInfo->getStorage();
121
+                    $sourceOwner = $view->getOwner($oldPath);
122
+                    $targetOwner = $view->getOwner($newPath);
123
+
124
+                    if ($sourceOwner !== $targetOwner
125
+                        && $sourceStorage->instanceOfStorage('OCA\Files_Sharing\SharedStorage')
126
+                    ) {
127
+                        $fileMovedOutOfSharedFolder = true;
128
+                    }
129
+                }
130
+            }
131
+        } catch (\Exception $e) {
132
+            // do nothing, in this case we just disable the trashbin and continue
133
+            \OC::$server->getLogger()->logException($e, [
134
+                'message' => 'Trashbin storage could not check if a file was moved out of a shared folder.',
135
+                'level' => \OCP\Util::DEBUG,
136
+                'app' => 'files_trashbin',
137
+            ]);
138
+        }
139
+
140
+        if($fileMovedOutOfSharedFolder) {
141
+            self::$moveOutOfSharedFolder['/' . $currentUserId . '/files' . $oldPath] = true;
142
+        } else {
143
+            self::$disableTrash = true;
144
+        }
145
+
146
+    }
147
+
148
+    /**
149
+     * @internal
150
+     */
151
+    public static function postRenameHook($params) {
152
+        self::$disableTrash = false;
153
+    }
154
+
155
+    /**
156
+     * Rename path1 to path2 by calling the wrapped storage.
157
+     *
158
+     * @param string $path1 first path
159
+     * @param string $path2 second path
160
+     * @return bool
161
+     */
162
+    public function rename($path1, $path2) {
163
+        $result = $this->storage->rename($path1, $path2);
164
+        if ($result === false) {
165
+            // when rename failed, the post_rename hook isn't triggered,
166
+            // but we still want to reenable the trash logic
167
+            self::$disableTrash = false;
168
+        }
169
+        return $result;
170
+    }
171
+
172
+    /**
173
+     * Deletes the given file by moving it into the trashbin.
174
+     *
175
+     * @param string $path path of file or folder to delete
176
+     *
177
+     * @return bool true if the operation succeeded, false otherwise
178
+     */
179
+    public function unlink($path) {
180
+        try {
181
+            if (isset(self::$moveOutOfSharedFolder[$this->mountPoint . $path])) {
182
+                $result = $this->doDelete($path, 'unlink', true);
183
+                unset(self::$moveOutOfSharedFolder[$this->mountPoint . $path]);
184
+            } else {
185
+                $result = $this->doDelete($path, 'unlink');
186
+            }
187
+        } catch (GenericEncryptionException $e) {
188
+            // in case of a encryption exception we delete the file right away
189
+            $this->logger->info(
190
+                "Can't move file" .  $path .
191
+                "to the trash bin, therefore it was deleted right away");
192
+
193
+            $result = $this->storage->unlink($path);
194
+        }
195
+
196
+        return $result;
197
+    }
198
+
199
+    /**
200
+     * Deletes the given folder by moving it into the trashbin.
201
+     *
202
+     * @param string $path path of folder to delete
203
+     *
204
+     * @return bool true if the operation succeeded, false otherwise
205
+     */
206
+    public function rmdir($path) {
207
+        if (isset(self::$moveOutOfSharedFolder[$this->mountPoint . $path])) {
208
+            $result = $this->doDelete($path, 'rmdir', true);
209
+            unset(self::$moveOutOfSharedFolder[$this->mountPoint . $path]);
210
+        } else {
211
+            $result = $this->doDelete($path, 'rmdir');
212
+        }
213
+
214
+        return $result;
215
+    }
216
+
217
+    /**
218
+     * check if it is a file located in data/user/files only files in the
219
+     * 'files' directory should be moved to the trash
220
+     *
221
+     * @param $path
222
+     * @return bool
223
+     */
224
+    protected function shouldMoveToTrash($path){
225
+
226
+        // check if there is a app which want to disable the trash bin for this file
227
+        $fileId = $this->storage->getCache()->getId($path);
228
+        $nodes = $this->rootFolder->getById($fileId);
229
+        foreach ($nodes as $node) {
230
+            $event = $this->createMoveToTrashEvent($node);
231
+            $this->eventDispatcher->dispatch('OCA\Files_Trashbin::moveToTrash', $event);
232
+            if ($event->shouldMoveToTrashBin() === false) {
233
+                return false;
234
+            }
235
+        }
236
+
237
+        $normalized = Filesystem::normalizePath($this->mountPoint . '/' . $path);
238
+        $parts = explode('/', $normalized);
239
+        if (count($parts) < 4) {
240
+            return false;
241
+        }
242
+
243
+        if ($parts[2] === 'files' && $this->userManager->userExists($parts[1])) {
244
+            return true;
245
+        }
246
+
247
+        return false;
248
+    }
249
+
250
+    /**
251
+     * get move to trash event
252
+     *
253
+     * @param Node $node
254
+     * @return MoveToTrashEvent
255
+     */
256
+    protected function createMoveToTrashEvent(Node $node) {
257
+        return new MoveToTrashEvent($node);
258
+    }
259
+
260
+    /**
261
+     * Run the delete operation with the given method
262
+     *
263
+     * @param string $path path of file or folder to delete
264
+     * @param string $method either "unlink" or "rmdir"
265
+     * @param bool $ownerOnly delete for owner only (if file gets moved out of a shared folder)
266
+     *
267
+     * @return bool true if the operation succeeded, false otherwise
268
+     */
269
+    private function doDelete($path, $method, $ownerOnly = false) {
270
+        if (self::$disableTrash
271
+            || !\OC::$server->getAppManager()->isEnabledForUser('files_trashbin')
272
+            || (pathinfo($path, PATHINFO_EXTENSION) === 'part')
273
+            || $this->shouldMoveToTrash($path) === false
274
+        ) {
275
+            return call_user_func_array([$this->storage, $method], [$path]);
276
+        }
277
+
278
+        // check permissions before we continue, this is especially important for
279
+        // shared files
280
+        if (!$this->isDeletable($path)) {
281
+            return false;
282
+        }
283
+
284
+        $normalized = Filesystem::normalizePath($this->mountPoint . '/' . $path, true, false, true);
285
+        $result = true;
286
+        $view = Filesystem::getView();
287
+        if (!isset($this->deletedFiles[$normalized]) && $view instanceof View) {
288
+            $this->deletedFiles[$normalized] = $normalized;
289
+            if ($filesPath = $view->getRelativePath($normalized)) {
290
+                $filesPath = trim($filesPath, '/');
291
+                $result = \OCA\Files_Trashbin\Trashbin::move2trash($filesPath, $ownerOnly);
292
+                // in cross-storage cases the file will be copied
293
+                // but not deleted, so we delete it here
294
+                if ($result) {
295
+                    call_user_func_array([$this->storage, $method], [$path]);
296
+                }
297
+            } else {
298
+                $result = call_user_func_array([$this->storage, $method], [$path]);
299
+            }
300
+            unset($this->deletedFiles[$normalized]);
301
+        } else if ($this->storage->file_exists($path)) {
302
+            $result = call_user_func_array([$this->storage, $method], [$path]);
303
+        }
304
+
305
+        return $result;
306
+    }
307
+
308
+    /**
309
+     * Setup the storate wrapper callback
310
+     */
311
+    public static function setupStorage() {
312
+        \OC\Files\Filesystem::addStorageWrapper('oc_trashbin', function ($mountPoint, $storage) {
313
+            return new \OCA\Files_Trashbin\Storage(
314
+                array('storage' => $storage, 'mountPoint' => $mountPoint),
315
+                \OC::$server->getUserManager(),
316
+                \OC::$server->getLogger(),
317
+                \OC::$server->getEventDispatcher(),
318
+                \OC::$server->getLazyRootFolder()
319
+            );
320
+        }, 1);
321
+    }
322 322
 
323 323
 }
Please login to merge, or discard this patch.
settings/ajax/uninstallapp.php 2 patches
Indentation   +11 added lines, -11 removed lines patch added patch discarded remove patch
@@ -27,14 +27,14 @@  discard block
 block discarded – undo
27 27
 
28 28
 $lastConfirm = (int) \OC::$server->getSession()->get('last-password-confirm');
29 29
 if ($lastConfirm < (time() - 30 * 60 + 15)) { // allow 15 seconds delay
30
-	$l = \OC::$server->getL10N('core');
31
-	OC_JSON::error(array( 'data' => array( 'message' => $l->t('Password confirmation is required'))));
32
-	exit();
30
+    $l = \OC::$server->getL10N('core');
31
+    OC_JSON::error(array( 'data' => array( 'message' => $l->t('Password confirmation is required'))));
32
+    exit();
33 33
 }
34 34
 
35 35
 if (!array_key_exists('appid', $_POST)) {
36
-	OC_JSON::error();
37
-	exit;
36
+    OC_JSON::error();
37
+    exit;
38 38
 }
39 39
 
40 40
 $appId = (string)$_POST['appid'];
@@ -45,11 +45,11 @@  discard block
 block discarded – undo
45 45
 $installer = \OC::$server->query(\OC\Installer::class);
46 46
 $result = $installer->removeApp($app);
47 47
 if($result !== false) {
48
-	// FIXME: Clear the cache - move that into some sane helper method
49
-	\OC::$server->getMemCacheFactory()->createDistributed('settings')->remove('listApps-0');
50
-	\OC::$server->getMemCacheFactory()->createDistributed('settings')->remove('listApps-1');
51
-	OC_JSON::success(array('data' => array('appid' => $appId)));
48
+    // FIXME: Clear the cache - move that into some sane helper method
49
+    \OC::$server->getMemCacheFactory()->createDistributed('settings')->remove('listApps-0');
50
+    \OC::$server->getMemCacheFactory()->createDistributed('settings')->remove('listApps-1');
51
+    OC_JSON::success(array('data' => array('appid' => $appId)));
52 52
 } else {
53
-	$l = \OC::$server->getL10N('settings');
54
-	OC_JSON::error(array('data' => array( 'message' => $l->t("Couldn't remove app.") )));
53
+    $l = \OC::$server->getL10N('settings');
54
+    OC_JSON::error(array('data' => array( 'message' => $l->t("Couldn't remove app.") )));
55 55
 }
Please login to merge, or discard this patch.
Spacing   +4 added lines, -4 removed lines patch added patch discarded remove patch
@@ -28,7 +28,7 @@  discard block
 block discarded – undo
28 28
 $lastConfirm = (int) \OC::$server->getSession()->get('last-password-confirm');
29 29
 if ($lastConfirm < (time() - 30 * 60 + 15)) { // allow 15 seconds delay
30 30
 	$l = \OC::$server->getL10N('core');
31
-	OC_JSON::error(array( 'data' => array( 'message' => $l->t('Password confirmation is required'))));
31
+	OC_JSON::error(array('data' => array('message' => $l->t('Password confirmation is required'))));
32 32
 	exit();
33 33
 }
34 34
 
@@ -37,19 +37,19 @@  discard block
 block discarded – undo
37 37
 	exit;
38 38
 }
39 39
 
40
-$appId = (string)$_POST['appid'];
40
+$appId = (string) $_POST['appid'];
41 41
 $appId = OC_App::cleanAppId($appId);
42 42
 
43 43
 // FIXME: move to controller
44 44
 /** @var \OC\Installer $installer */
45 45
 $installer = \OC::$server->query(\OC\Installer::class);
46 46
 $result = $installer->removeApp($app);
47
-if($result !== false) {
47
+if ($result !== false) {
48 48
 	// FIXME: Clear the cache - move that into some sane helper method
49 49
 	\OC::$server->getMemCacheFactory()->createDistributed('settings')->remove('listApps-0');
50 50
 	\OC::$server->getMemCacheFactory()->createDistributed('settings')->remove('listApps-1');
51 51
 	OC_JSON::success(array('data' => array('appid' => $appId)));
52 52
 } else {
53 53
 	$l = \OC::$server->getL10N('settings');
54
-	OC_JSON::error(array('data' => array( 'message' => $l->t("Couldn't remove app.") )));
54
+	OC_JSON::error(array('data' => array('message' => $l->t("Couldn't remove app."))));
55 55
 }
Please login to merge, or discard this patch.
lib/private/App/AppManager.php 2 patches
Spacing   +11 added lines, -11 removed lines patch added patch discarded remove patch
@@ -114,11 +114,11 @@  discard block
 block discarded – undo
114 114
 			$values = $this->appConfig->getValues(false, 'enabled');
115 115
 
116 116
 			$alwaysEnabledApps = $this->getAlwaysEnabledApps();
117
-			foreach($alwaysEnabledApps as $appId) {
117
+			foreach ($alwaysEnabledApps as $appId) {
118 118
 				$values[$appId] = 'yes';
119 119
 			}
120 120
 
121
-			$this->installedAppsCache = array_filter($values, function ($value) {
121
+			$this->installedAppsCache = array_filter($values, function($value) {
122 122
 				return $value !== 'no';
123 123
 			});
124 124
 			ksort($this->installedAppsCache);
@@ -143,7 +143,7 @@  discard block
 block discarded – undo
143 143
 	 */
144 144
 	public function getEnabledAppsForUser(IUser $user) {
145 145
 		$apps = $this->getInstalledAppsValues();
146
-		$appsForUser = array_filter($apps, function ($enabled) use ($user) {
146
+		$appsForUser = array_filter($apps, function($enabled) use ($user) {
147 147
 			return $this->checkAppForUser($enabled, $user);
148 148
 		});
149 149
 		return array_keys($appsForUser);
@@ -182,7 +182,7 @@  discard block
 block discarded – undo
182 182
 		} elseif ($user === null) {
183 183
 			return false;
184 184
 		} else {
185
-			if(empty($enabled)){
185
+			if (empty($enabled)) {
186 186
 				return false;
187 187
 			}
188 188
 
@@ -190,7 +190,7 @@  discard block
 block discarded – undo
190 190
 
191 191
 			if (!is_array($groupIds)) {
192 192
 				$jsonError = json_last_error();
193
-				\OC::$server->getLogger()->warning('AppManger::checkAppForUser - can\'t decode group IDs: ' . print_r($enabled, true) . ' - json error code: ' . $jsonError, ['app' => 'lib']);
193
+				\OC::$server->getLogger()->warning('AppManger::checkAppForUser - can\'t decode group IDs: '.print_r($enabled, true).' - json error code: '.$jsonError, ['app' => 'lib']);
194 194
 				return false;
195 195
 			}
196 196
 
@@ -264,7 +264,7 @@  discard block
 block discarded – undo
264 264
 			}
265 265
 		}
266 266
 
267
-		$groupIds = array_map(function ($group) {
267
+		$groupIds = array_map(function($group) {
268 268
 			/** @var \OCP\IGroup $group */
269 269
 			return $group->getGID();
270 270
 		}, $groups);
@@ -303,8 +303,8 @@  discard block
 block discarded – undo
303 303
 	 */
304 304
 	public function getAppPath($appId) {
305 305
 		$appPath = \OC_App::getAppPath($appId);
306
-		if($appPath === false) {
307
-			throw new AppPathNotFoundException('Could not find path for ' . $appId);
306
+		if ($appPath === false) {
307
+			throw new AppPathNotFoundException('Could not find path for '.$appId);
308 308
 		}
309 309
 		return $appPath;
310 310
 	}
@@ -364,7 +364,7 @@  discard block
 block discarded – undo
364 364
 			} catch (AppPathNotFoundException $e) {
365 365
 				return null;
366 366
 			}
367
-			$file = $appPath . '/appinfo/info.xml';
367
+			$file = $appPath.'/appinfo/info.xml';
368 368
 		}
369 369
 
370 370
 		$parser = new InfoParser($this->memCacheFactory->createLocal('core.appinfo'));
@@ -382,7 +382,7 @@  discard block
 block discarded – undo
382 382
 	}
383 383
 
384 384
 	public function getAppVersion(string $appId, bool $useCache = true) {
385
-		if(!$useCache || !isset($this->appVersions[$appId])) {
385
+		if (!$useCache || !isset($this->appVersions[$appId])) {
386 386
 			$appInfo = \OC::$server->getAppManager()->getAppInfo($appId);
387 387
 			$this->appVersions[$appId] = ($appInfo !== null) ? $appInfo['version'] : '0';
388 388
 		}
@@ -425,7 +425,7 @@  discard block
 block discarded – undo
425 425
 
426 426
 	private function loadShippedJson() {
427 427
 		if ($this->shippedApps === null) {
428
-			$shippedJson = \OC::$SERVERROOT . '/core/shipped.json';
428
+			$shippedJson = \OC::$SERVERROOT.'/core/shipped.json';
429 429
 			if (!file_exists($shippedJson)) {
430 430
 				throw new \Exception("File not found: $shippedJson");
431 431
 			}
Please login to merge, or discard this patch.
Indentation   +397 added lines, -397 removed lines patch added patch discarded remove patch
@@ -44,401 +44,401 @@
 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) {
384
-		if(!$useCache || !isset($this->appVersions[$appId])) {
385
-			$appInfo = \OC::$server->getAppManager()->getAppInfo($appId);
386
-			$this->appVersions[$appId] = ($appInfo !== null) ? $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
-	 */
415
-	public function isShipped($appId) {
416
-		$this->loadShippedJson();
417
-		return in_array($appId, $this->shippedApps, true);
418
-	}
419
-
420
-	private function isAlwaysEnabled($appId) {
421
-		$alwaysEnabled = $this->getAlwaysEnabledApps();
422
-		return in_array($appId, $alwaysEnabled, true);
423
-	}
424
-
425
-	private function loadShippedJson() {
426
-		if ($this->shippedApps === null) {
427
-			$shippedJson = \OC::$SERVERROOT . '/core/shipped.json';
428
-			if (!file_exists($shippedJson)) {
429
-				throw new \Exception("File not found: $shippedJson");
430
-			}
431
-			$content = json_decode(file_get_contents($shippedJson), true);
432
-			$this->shippedApps = $content['shippedApps'];
433
-			$this->alwaysEnabled = $content['alwaysEnabled'];
434
-		}
435
-	}
436
-
437
-	/**
438
-	 * @inheritdoc
439
-	 */
440
-	public function getAlwaysEnabledApps() {
441
-		$this->loadShippedJson();
442
-		return $this->alwaysEnabled;
443
-	}
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) {
384
+        if(!$useCache || !isset($this->appVersions[$appId])) {
385
+            $appInfo = \OC::$server->getAppManager()->getAppInfo($appId);
386
+            $this->appVersions[$appId] = ($appInfo !== null) ? $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
+     */
415
+    public function isShipped($appId) {
416
+        $this->loadShippedJson();
417
+        return in_array($appId, $this->shippedApps, true);
418
+    }
419
+
420
+    private function isAlwaysEnabled($appId) {
421
+        $alwaysEnabled = $this->getAlwaysEnabledApps();
422
+        return in_array($appId, $alwaysEnabled, true);
423
+    }
424
+
425
+    private function loadShippedJson() {
426
+        if ($this->shippedApps === null) {
427
+            $shippedJson = \OC::$SERVERROOT . '/core/shipped.json';
428
+            if (!file_exists($shippedJson)) {
429
+                throw new \Exception("File not found: $shippedJson");
430
+            }
431
+            $content = json_decode(file_get_contents($shippedJson), true);
432
+            $this->shippedApps = $content['shippedApps'];
433
+            $this->alwaysEnabled = $content['alwaysEnabled'];
434
+        }
435
+    }
436
+
437
+    /**
438
+     * @inheritdoc
439
+     */
440
+    public function getAlwaysEnabledApps() {
441
+        $this->loadShippedJson();
442
+        return $this->alwaysEnabled;
443
+    }
444 444
 }
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 mixed
55
-	 * @since 14.0.0
56
-	 */
57
-	public function getAppVersion(string $appId, bool $useCache = true);
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 mixed
55
+     * @since 14.0.0
56
+     */
57
+    public function getAppVersion(string $appId, bool $useCache = true);
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.
lib/public/App.php 2 patches
Indentation   +64 added lines, -64 removed lines patch added patch discarded remove patch
@@ -45,73 +45,73 @@
 block discarded – undo
45 45
 class App {
46 46
 
47 47
 
48
-	/**
49
-	 * Register a Configuration Screen that should appear in the personal settings section.
50
-	 * @param string $app appid
51
-	 * @param string $page page to be included
52
-	 * @return void
53
-	 * @since 4.0.0
54
-	 * @deprecated 14.0.0 Use settings section in appinfo.xml to register personal admin sections
55
-	*/
56
-	public static function registerPersonal( $app, $page ) {
57
-		\OC_App::registerPersonal( $app, $page );
58
-	}
48
+    /**
49
+     * Register a Configuration Screen that should appear in the personal settings section.
50
+     * @param string $app appid
51
+     * @param string $page page to be included
52
+     * @return void
53
+     * @since 4.0.0
54
+     * @deprecated 14.0.0 Use settings section in appinfo.xml to register personal admin sections
55
+     */
56
+    public static function registerPersonal( $app, $page ) {
57
+        \OC_App::registerPersonal( $app, $page );
58
+    }
59 59
 
60
-	/**
61
-	 * Register a Configuration Screen that should appear in the Admin section.
62
-	 * @param string $app string appid
63
-	 * @param string $page string page to be included
64
-	 * @return void
65
-	 * @since 4.0.0
66
-	 * @deprecated 14.0.0 Use settings section in appinfo.xml to register admin sections
67
-	 */
68
-	public static function registerAdmin( $app, $page ) {
69
-		\OC_App::registerAdmin( $app, $page );
70
-	}
60
+    /**
61
+     * Register a Configuration Screen that should appear in the Admin section.
62
+     * @param string $app string appid
63
+     * @param string $page string page to be included
64
+     * @return void
65
+     * @since 4.0.0
66
+     * @deprecated 14.0.0 Use settings section in appinfo.xml to register admin sections
67
+     */
68
+    public static function registerAdmin( $app, $page ) {
69
+        \OC_App::registerAdmin( $app, $page );
70
+    }
71 71
 
72
-	/**
73
-	 * Read app metadata from the info.xml file
74
-	 * @param string $app id of the app or the path of the info.xml file
75
-	 * @param boolean $path (optional)
76
-	 * @return array|null
77
-	 * @deprecated 14.0.0 ise \OC::$server->getAppManager()->getAppInfo($appId)
78
-	 * @since 4.0.0
79
-	*/
80
-	public static function getAppInfo( $app, $path=false ) {
81
-		return \OC_App::getAppInfo( $app, $path);
82
-	}
72
+    /**
73
+     * Read app metadata from the info.xml file
74
+     * @param string $app id of the app or the path of the info.xml file
75
+     * @param boolean $path (optional)
76
+     * @return array|null
77
+     * @deprecated 14.0.0 ise \OC::$server->getAppManager()->getAppInfo($appId)
78
+     * @since 4.0.0
79
+     */
80
+    public static function getAppInfo( $app, $path=false ) {
81
+        return \OC_App::getAppInfo( $app, $path);
82
+    }
83 83
 
84
-	/**
85
-	 * checks whether or not an app is enabled
86
-	 * @param string $app
87
-	 * @return boolean
88
-	 *
89
-	 * This function checks whether or not an app is enabled.
90
-	 * @since 4.0.0
91
-	 * @deprecated 13.0.0 use \OC::$server->getAppManager()->isEnabledForUser($appId)
92
-	 */
93
-	public static function isEnabled( $app ) {
94
-		return \OC::$server->getAppManager()->isEnabledForUser( $app );
95
-	}
84
+    /**
85
+     * checks whether or not an app is enabled
86
+     * @param string $app
87
+     * @return boolean
88
+     *
89
+     * This function checks whether or not an app is enabled.
90
+     * @since 4.0.0
91
+     * @deprecated 13.0.0 use \OC::$server->getAppManager()->isEnabledForUser($appId)
92
+     */
93
+    public static function isEnabled( $app ) {
94
+        return \OC::$server->getAppManager()->isEnabledForUser( $app );
95
+    }
96 96
 
97
-	/**
98
-	 * Check if the app is enabled, redirects to home if not
99
-	 * @param string $app
100
-	 * @return void
101
-	 * @since 4.0.0
102
-	 * @deprecated 9.0.0 ownCloud core will handle disabled apps and redirects to valid URLs
103
-	*/
104
-	public static function checkAppEnabled( $app ) {
105
-	}
97
+    /**
98
+     * Check if the app is enabled, redirects to home if not
99
+     * @param string $app
100
+     * @return void
101
+     * @since 4.0.0
102
+     * @deprecated 9.0.0 ownCloud core will handle disabled apps and redirects to valid URLs
103
+     */
104
+    public static function checkAppEnabled( $app ) {
105
+    }
106 106
 
107
-	/**
108
-	 * Get the last version of the app from appinfo/info.xml
109
-	 * @param string $app
110
-	 * @return string
111
-	 * @since 4.0.0
112
-	 * @deprecated 14.0.0 use \OC::$server->getAppManager()->getAppVersion($appId)
113
-	 */
114
-	public static function getAppVersion( $app ) {
115
-		return \OC::$server->getAppManager()->getAppVersion($app);
116
-	}
107
+    /**
108
+     * Get the last version of the app from appinfo/info.xml
109
+     * @param string $app
110
+     * @return string
111
+     * @since 4.0.0
112
+     * @deprecated 14.0.0 use \OC::$server->getAppManager()->getAppVersion($appId)
113
+     */
114
+    public static function getAppVersion( $app ) {
115
+        return \OC::$server->getAppManager()->getAppVersion($app);
116
+    }
117 117
 }
Please login to merge, or discard this patch.
Spacing   +10 added lines, -10 removed lines patch added patch discarded remove patch
@@ -53,8 +53,8 @@  discard block
 block discarded – undo
53 53
 	 * @since 4.0.0
54 54
 	 * @deprecated 14.0.0 Use settings section in appinfo.xml to register personal admin sections
55 55
 	*/
56
-	public static function registerPersonal( $app, $page ) {
57
-		\OC_App::registerPersonal( $app, $page );
56
+	public static function registerPersonal($app, $page) {
57
+		\OC_App::registerPersonal($app, $page);
58 58
 	}
59 59
 
60 60
 	/**
@@ -65,8 +65,8 @@  discard block
 block discarded – undo
65 65
 	 * @since 4.0.0
66 66
 	 * @deprecated 14.0.0 Use settings section in appinfo.xml to register admin sections
67 67
 	 */
68
-	public static function registerAdmin( $app, $page ) {
69
-		\OC_App::registerAdmin( $app, $page );
68
+	public static function registerAdmin($app, $page) {
69
+		\OC_App::registerAdmin($app, $page);
70 70
 	}
71 71
 
72 72
 	/**
@@ -77,8 +77,8 @@  discard block
 block discarded – undo
77 77
 	 * @deprecated 14.0.0 ise \OC::$server->getAppManager()->getAppInfo($appId)
78 78
 	 * @since 4.0.0
79 79
 	*/
80
-	public static function getAppInfo( $app, $path=false ) {
81
-		return \OC_App::getAppInfo( $app, $path);
80
+	public static function getAppInfo($app, $path = false) {
81
+		return \OC_App::getAppInfo($app, $path);
82 82
 	}
83 83
 
84 84
 	/**
@@ -90,8 +90,8 @@  discard block
 block discarded – undo
90 90
 	 * @since 4.0.0
91 91
 	 * @deprecated 13.0.0 use \OC::$server->getAppManager()->isEnabledForUser($appId)
92 92
 	 */
93
-	public static function isEnabled( $app ) {
94
-		return \OC::$server->getAppManager()->isEnabledForUser( $app );
93
+	public static function isEnabled($app) {
94
+		return \OC::$server->getAppManager()->isEnabledForUser($app);
95 95
 	}
96 96
 
97 97
 	/**
@@ -101,7 +101,7 @@  discard block
 block discarded – undo
101 101
 	 * @since 4.0.0
102 102
 	 * @deprecated 9.0.0 ownCloud core will handle disabled apps and redirects to valid URLs
103 103
 	*/
104
-	public static function checkAppEnabled( $app ) {
104
+	public static function checkAppEnabled($app) {
105 105
 	}
106 106
 
107 107
 	/**
@@ -111,7 +111,7 @@  discard block
 block discarded – undo
111 111
 	 * @since 4.0.0
112 112
 	 * @deprecated 14.0.0 use \OC::$server->getAppManager()->getAppVersion($appId)
113 113
 	 */
114
-	public static function getAppVersion( $app ) {
114
+	public static function getAppVersion($app) {
115 115
 		return \OC::$server->getAppManager()->getAppVersion($app);
116 116
 	}
117 117
 }
Please login to merge, or discard this patch.
lib/private/Installer.php 2 patches
Indentation   +559 added lines, -559 removed lines patch added patch discarded remove patch
@@ -57,563 +57,563 @@
 block discarded – undo
57 57
  * This class provides the functionality needed to install, update and remove apps
58 58
  */
59 59
 class Installer {
60
-	/** @var AppFetcher */
61
-	private $appFetcher;
62
-	/** @var IClientService */
63
-	private $clientService;
64
-	/** @var ITempManager */
65
-	private $tempManager;
66
-	/** @var ILogger */
67
-	private $logger;
68
-	/** @var IConfig */
69
-	private $config;
70
-	/** @var array - for caching the result of app fetcher */
71
-	private $apps = null;
72
-	/** @var bool|null - for caching the result of the ready status */
73
-	private $isInstanceReadyForUpdates = null;
74
-
75
-	/**
76
-	 * @param AppFetcher $appFetcher
77
-	 * @param IClientService $clientService
78
-	 * @param ITempManager $tempManager
79
-	 * @param ILogger $logger
80
-	 * @param IConfig $config
81
-	 */
82
-	public function __construct(AppFetcher $appFetcher,
83
-								IClientService $clientService,
84
-								ITempManager $tempManager,
85
-								ILogger $logger,
86
-								IConfig $config) {
87
-		$this->appFetcher = $appFetcher;
88
-		$this->clientService = $clientService;
89
-		$this->tempManager = $tempManager;
90
-		$this->logger = $logger;
91
-		$this->config = $config;
92
-	}
93
-
94
-	/**
95
-	 * Installs an app that is located in one of the app folders already
96
-	 *
97
-	 * @param string $appId App to install
98
-	 * @throws \Exception
99
-	 * @return string app ID
100
-	 */
101
-	public function installApp($appId) {
102
-		$app = \OC_App::findAppInDirectories($appId);
103
-		if($app === false) {
104
-			throw new \Exception('App not found in any app directory');
105
-		}
106
-
107
-		$basedir = $app['path'].'/'.$appId;
108
-		$info = OC_App::getAppInfo($basedir.'/appinfo/info.xml', true);
109
-
110
-		$l = \OC::$server->getL10N('core');
111
-
112
-		if(!is_array($info)) {
113
-			throw new \Exception(
114
-				$l->t('App "%s" cannot be installed because appinfo file cannot be read.',
115
-					[$info['name']]
116
-				)
117
-			);
118
-		}
119
-
120
-		$version = \OCP\Util::getVersion();
121
-		if (!\OC_App::isAppCompatible($version, $info)) {
122
-			throw new \Exception(
123
-				// TODO $l
124
-				$l->t('App "%s" cannot be installed because it is not compatible with this version of the server.',
125
-					[$info['name']]
126
-				)
127
-			);
128
-		}
129
-
130
-		// check for required dependencies
131
-		\OC_App::checkAppDependencies($this->config, $l, $info);
132
-		\OC_App::registerAutoloading($appId, $basedir);
133
-
134
-		//install the database
135
-		if(is_file($basedir.'/appinfo/database.xml')) {
136
-			if (\OC::$server->getConfig()->getAppValue($info['id'], 'installed_version') === null) {
137
-				OC_DB::createDbFromStructure($basedir.'/appinfo/database.xml');
138
-			} else {
139
-				OC_DB::updateDbFromStructure($basedir.'/appinfo/database.xml');
140
-			}
141
-		} else {
142
-			$ms = new \OC\DB\MigrationService($info['id'], \OC::$server->getDatabaseConnection());
143
-			$ms->migrate();
144
-		}
145
-
146
-		\OC_App::setupBackgroundJobs($info['background-jobs']);
147
-
148
-		//run appinfo/install.php
149
-		if(!isset($data['noinstall']) or $data['noinstall']==false) {
150
-			self::includeAppScript($basedir . '/appinfo/install.php');
151
-		}
152
-
153
-		$appData = OC_App::getAppInfo($appId);
154
-		OC_App::executeRepairSteps($appId, $appData['repair-steps']['install']);
155
-
156
-		//set the installed version
157
-		\OC::$server->getConfig()->setAppValue($info['id'], 'installed_version', OC_App::getAppVersion($info['id'], false));
158
-		\OC::$server->getConfig()->setAppValue($info['id'], 'enabled', 'no');
159
-
160
-		//set remote/public handlers
161
-		foreach($info['remote'] as $name=>$path) {
162
-			\OC::$server->getConfig()->setAppValue('core', 'remote_'.$name, $info['id'].'/'.$path);
163
-		}
164
-		foreach($info['public'] as $name=>$path) {
165
-			\OC::$server->getConfig()->setAppValue('core', 'public_'.$name, $info['id'].'/'.$path);
166
-		}
167
-
168
-		OC_App::setAppTypes($info['id']);
169
-
170
-		return $info['id'];
171
-	}
172
-
173
-	/**
174
-	 * @brief checks whether or not an app is installed
175
-	 * @param string $app app
176
-	 * @returns bool
177
-	 *
178
-	 * Checks whether or not an app is installed, i.e. registered in apps table.
179
-	 */
180
-	public static function isInstalled( $app ) {
181
-		return (\OC::$server->getConfig()->getAppValue($app, "installed_version", null) !== null);
182
-	}
183
-
184
-	/**
185
-	 * Updates the specified app from the appstore
186
-	 *
187
-	 * @param string $appId
188
-	 * @return bool
189
-	 */
190
-	public function updateAppstoreApp($appId) {
191
-		if($this->isUpdateAvailable($appId)) {
192
-			try {
193
-				$this->downloadApp($appId);
194
-			} catch (\Exception $e) {
195
-				$this->logger->logException($e, [
196
-					'level' => \OCP\Util::ERROR,
197
-					'app' => 'core',
198
-				]);
199
-				return false;
200
-			}
201
-			return OC_App::updateApp($appId);
202
-		}
203
-
204
-		return false;
205
-	}
206
-
207
-	/**
208
-	 * Downloads an app and puts it into the app directory
209
-	 *
210
-	 * @param string $appId
211
-	 *
212
-	 * @throws \Exception If the installation was not successful
213
-	 */
214
-	public function downloadApp($appId) {
215
-		$appId = strtolower($appId);
216
-
217
-		$apps = $this->appFetcher->get();
218
-		foreach($apps as $app) {
219
-			if($app['id'] === $appId) {
220
-				// Load the certificate
221
-				$certificate = new X509();
222
-				$certificate->loadCA(file_get_contents(__DIR__ . '/../../resources/codesigning/root.crt'));
223
-				$loadedCertificate = $certificate->loadX509($app['certificate']);
224
-
225
-				// Verify if the certificate has been revoked
226
-				$crl = new X509();
227
-				$crl->loadCA(file_get_contents(__DIR__ . '/../../resources/codesigning/root.crt'));
228
-				$crl->loadCRL(file_get_contents(__DIR__ . '/../../resources/codesigning/root.crl'));
229
-				if($crl->validateSignature() !== true) {
230
-					throw new \Exception('Could not validate CRL signature');
231
-				}
232
-				$csn = $loadedCertificate['tbsCertificate']['serialNumber']->toString();
233
-				$revoked = $crl->getRevoked($csn);
234
-				if ($revoked !== false) {
235
-					throw new \Exception(
236
-						sprintf(
237
-							'Certificate "%s" has been revoked',
238
-							$csn
239
-						)
240
-					);
241
-				}
242
-
243
-				// Verify if the certificate has been issued by the Nextcloud Code Authority CA
244
-				if($certificate->validateSignature() !== true) {
245
-					throw new \Exception(
246
-						sprintf(
247
-							'App with id %s has a certificate not issued by a trusted Code Signing Authority',
248
-							$appId
249
-						)
250
-					);
251
-				}
252
-
253
-				// Verify if the certificate is issued for the requested app id
254
-				$certInfo = openssl_x509_parse($app['certificate']);
255
-				if(!isset($certInfo['subject']['CN'])) {
256
-					throw new \Exception(
257
-						sprintf(
258
-							'App with id %s has a cert with no CN',
259
-							$appId
260
-						)
261
-					);
262
-				}
263
-				if($certInfo['subject']['CN'] !== $appId) {
264
-					throw new \Exception(
265
-						sprintf(
266
-							'App with id %s has a cert issued to %s',
267
-							$appId,
268
-							$certInfo['subject']['CN']
269
-						)
270
-					);
271
-				}
272
-
273
-				// Download the release
274
-				$tempFile = $this->tempManager->getTemporaryFile('.tar.gz');
275
-				$client = $this->clientService->newClient();
276
-				$client->get($app['releases'][0]['download'], ['save_to' => $tempFile]);
277
-
278
-				// Check if the signature actually matches the downloaded content
279
-				$certificate = openssl_get_publickey($app['certificate']);
280
-				$verified = (bool)openssl_verify(file_get_contents($tempFile), base64_decode($app['releases'][0]['signature']), $certificate, OPENSSL_ALGO_SHA512);
281
-				openssl_free_key($certificate);
282
-
283
-				if($verified === true) {
284
-					// Seems to match, let's proceed
285
-					$extractDir = $this->tempManager->getTemporaryFolder();
286
-					$archive = new TAR($tempFile);
287
-
288
-					if($archive) {
289
-						if (!$archive->extract($extractDir)) {
290
-							throw new \Exception(
291
-								sprintf(
292
-									'Could not extract app %s',
293
-									$appId
294
-								)
295
-							);
296
-						}
297
-						$allFiles = scandir($extractDir);
298
-						$folders = array_diff($allFiles, ['.', '..']);
299
-						$folders = array_values($folders);
300
-
301
-						if(count($folders) > 1) {
302
-							throw new \Exception(
303
-								sprintf(
304
-									'Extracted app %s has more than 1 folder',
305
-									$appId
306
-								)
307
-							);
308
-						}
309
-
310
-						// Check if appinfo/info.xml has the same app ID as well
311
-						$loadEntities = libxml_disable_entity_loader(false);
312
-						$xml = simplexml_load_file($extractDir . '/' . $folders[0] . '/appinfo/info.xml');
313
-						libxml_disable_entity_loader($loadEntities);
314
-						if((string)$xml->id !== $appId) {
315
-							throw new \Exception(
316
-								sprintf(
317
-									'App for id %s has a wrong app ID in info.xml: %s',
318
-									$appId,
319
-									(string)$xml->id
320
-								)
321
-							);
322
-						}
323
-
324
-						// Check if the version is lower than before
325
-						$currentVersion = OC_App::getAppVersion($appId);
326
-						$newVersion = (string)$xml->version;
327
-						if(version_compare($currentVersion, $newVersion) === 1) {
328
-							throw new \Exception(
329
-								sprintf(
330
-									'App for id %s has version %s and tried to update to lower version %s',
331
-									$appId,
332
-									$currentVersion,
333
-									$newVersion
334
-								)
335
-							);
336
-						}
337
-
338
-						$baseDir = OC_App::getInstallPath() . '/' . $appId;
339
-						// Remove old app with the ID if existent
340
-						OC_Helper::rmdirr($baseDir);
341
-						// Move to app folder
342
-						if(@mkdir($baseDir)) {
343
-							$extractDir .= '/' . $folders[0];
344
-							OC_Helper::copyr($extractDir, $baseDir);
345
-						}
346
-						OC_Helper::copyr($extractDir, $baseDir);
347
-						OC_Helper::rmdirr($extractDir);
348
-						return;
349
-					} else {
350
-						throw new \Exception(
351
-							sprintf(
352
-								'Could not extract app with ID %s to %s',
353
-								$appId,
354
-								$extractDir
355
-							)
356
-						);
357
-					}
358
-				} else {
359
-					// Signature does not match
360
-					throw new \Exception(
361
-						sprintf(
362
-							'App with id %s has invalid signature',
363
-							$appId
364
-						)
365
-					);
366
-				}
367
-			}
368
-		}
369
-
370
-		throw new \Exception(
371
-			sprintf(
372
-				'Could not download app %s',
373
-				$appId
374
-			)
375
-		);
376
-	}
377
-
378
-	/**
379
-	 * Check if an update for the app is available
380
-	 *
381
-	 * @param string $appId
382
-	 * @return string|false false or the version number of the update
383
-	 */
384
-	public function isUpdateAvailable($appId) {
385
-		if ($this->isInstanceReadyForUpdates === null) {
386
-			$installPath = OC_App::getInstallPath();
387
-			if ($installPath === false || $installPath === null) {
388
-				$this->isInstanceReadyForUpdates = false;
389
-			} else {
390
-				$this->isInstanceReadyForUpdates = true;
391
-			}
392
-		}
393
-
394
-		if ($this->isInstanceReadyForUpdates === false) {
395
-			return false;
396
-		}
397
-
398
-		if ($this->isInstalledFromGit($appId) === true) {
399
-			return false;
400
-		}
401
-
402
-		if ($this->apps === null) {
403
-			$this->apps = $this->appFetcher->get();
404
-		}
405
-
406
-		foreach($this->apps as $app) {
407
-			if($app['id'] === $appId) {
408
-				$currentVersion = OC_App::getAppVersion($appId);
409
-				$newestVersion = $app['releases'][0]['version'];
410
-				if (version_compare($newestVersion, $currentVersion, '>')) {
411
-					return $newestVersion;
412
-				} else {
413
-					return false;
414
-				}
415
-			}
416
-		}
417
-
418
-		return false;
419
-	}
420
-
421
-	/**
422
-	 * Check if app has been installed from git
423
-	 * @param string $name name of the application to remove
424
-	 * @return boolean
425
-	 *
426
-	 * The function will check if the path contains a .git folder
427
-	 */
428
-	private function isInstalledFromGit($appId) {
429
-		$app = \OC_App::findAppInDirectories($appId);
430
-		if($app === false) {
431
-			return false;
432
-		}
433
-		$basedir = $app['path'].'/'.$appId;
434
-		return file_exists($basedir.'/.git/');
435
-	}
436
-
437
-	/**
438
-	 * Check if app is already downloaded
439
-	 * @param string $name name of the application to remove
440
-	 * @return boolean
441
-	 *
442
-	 * The function will check if the app is already downloaded in the apps repository
443
-	 */
444
-	public function isDownloaded($name) {
445
-		foreach(\OC::$APPSROOTS as $dir) {
446
-			$dirToTest  = $dir['path'];
447
-			$dirToTest .= '/';
448
-			$dirToTest .= $name;
449
-			$dirToTest .= '/';
450
-
451
-			if (is_dir($dirToTest)) {
452
-				return true;
453
-			}
454
-		}
455
-
456
-		return false;
457
-	}
458
-
459
-	/**
460
-	 * Removes an app
461
-	 * @param string $appId ID of the application to remove
462
-	 * @return boolean
463
-	 *
464
-	 *
465
-	 * This function works as follows
466
-	 *   -# call uninstall repair steps
467
-	 *   -# removing the files
468
-	 *
469
-	 * The function will not delete preferences, tables and the configuration,
470
-	 * this has to be done by the function oc_app_uninstall().
471
-	 */
472
-	public function removeApp($appId) {
473
-		if($this->isDownloaded( $appId )) {
474
-			if (\OC::$server->getAppManager()->isShipped($appId)) {
475
-				return false;
476
-			}
477
-			$appDir = OC_App::getInstallPath() . '/' . $appId;
478
-			OC_Helper::rmdirr($appDir);
479
-			return true;
480
-		}else{
481
-			\OCP\Util::writeLog('core', 'can\'t remove app '.$appId.'. It is not installed.', \OCP\Util::ERROR);
482
-
483
-			return false;
484
-		}
485
-
486
-	}
487
-
488
-	/**
489
-	 * Installs the app within the bundle and marks the bundle as installed
490
-	 *
491
-	 * @param Bundle $bundle
492
-	 * @throws \Exception If app could not get installed
493
-	 */
494
-	public function installAppBundle(Bundle $bundle) {
495
-		$appIds = $bundle->getAppIdentifiers();
496
-		foreach($appIds as $appId) {
497
-			if(!$this->isDownloaded($appId)) {
498
-				$this->downloadApp($appId);
499
-			}
500
-			$this->installApp($appId);
501
-			$app = new OC_App();
502
-			$app->enable($appId);
503
-		}
504
-		$bundles = json_decode($this->config->getAppValue('core', 'installed.bundles', json_encode([])), true);
505
-		$bundles[] = $bundle->getIdentifier();
506
-		$this->config->setAppValue('core', 'installed.bundles', json_encode($bundles));
507
-	}
508
-
509
-	/**
510
-	 * Installs shipped apps
511
-	 *
512
-	 * This function installs all apps found in the 'apps' directory that should be enabled by default;
513
-	 * @param bool $softErrors When updating we ignore errors and simply log them, better to have a
514
-	 *                         working ownCloud at the end instead of an aborted update.
515
-	 * @return array Array of error messages (appid => Exception)
516
-	 */
517
-	public static function installShippedApps($softErrors = false) {
518
-		$errors = [];
519
-		foreach(\OC::$APPSROOTS as $app_dir) {
520
-			if($dir = opendir( $app_dir['path'] )) {
521
-				while( false !== ( $filename = readdir( $dir ))) {
522
-					if( $filename[0] !== '.' and is_dir($app_dir['path']."/$filename") ) {
523
-						if( file_exists( $app_dir['path']."/$filename/appinfo/info.xml" )) {
524
-							if(!Installer::isInstalled($filename)) {
525
-								$info=OC_App::getAppInfo($filename);
526
-								$enabled = isset($info['default_enable']);
527
-								if (($enabled || in_array($filename, \OC::$server->getAppManager()->getAlwaysEnabledApps()))
528
-									  && \OC::$server->getConfig()->getAppValue($filename, 'enabled') !== 'no') {
529
-									if ($softErrors) {
530
-										try {
531
-											Installer::installShippedApp($filename);
532
-										} catch (HintException $e) {
533
-											if ($e->getPrevious() instanceof TableExistsException) {
534
-												$errors[$filename] = $e;
535
-												continue;
536
-											}
537
-											throw $e;
538
-										}
539
-									} else {
540
-										Installer::installShippedApp($filename);
541
-									}
542
-									\OC::$server->getConfig()->setAppValue($filename, 'enabled', 'yes');
543
-								}
544
-							}
545
-						}
546
-					}
547
-				}
548
-				closedir( $dir );
549
-			}
550
-		}
551
-
552
-		return $errors;
553
-	}
554
-
555
-	/**
556
-	 * install an app already placed in the app folder
557
-	 * @param string $app id of the app to install
558
-	 * @return integer
559
-	 */
560
-	public static function installShippedApp($app) {
561
-		//install the database
562
-		$appPath = OC_App::getAppPath($app);
563
-		\OC_App::registerAutoloading($app, $appPath);
564
-
565
-		if(is_file("$appPath/appinfo/database.xml")) {
566
-			try {
567
-				OC_DB::createDbFromStructure("$appPath/appinfo/database.xml");
568
-			} catch (TableExistsException $e) {
569
-				throw new HintException(
570
-					'Failed to enable app ' . $app,
571
-					'Please ask for help via one of our <a href="https://nextcloud.com/support/" target="_blank" rel="noreferrer noopener">support channels</a>.',
572
-					0, $e
573
-				);
574
-			}
575
-		} else {
576
-			$ms = new \OC\DB\MigrationService($app, \OC::$server->getDatabaseConnection());
577
-			$ms->migrate();
578
-		}
579
-
580
-		//run appinfo/install.php
581
-		self::includeAppScript("$appPath/appinfo/install.php");
582
-
583
-		$info = OC_App::getAppInfo($app);
584
-		if (is_null($info)) {
585
-			return false;
586
-		}
587
-		\OC_App::setupBackgroundJobs($info['background-jobs']);
588
-
589
-		OC_App::executeRepairSteps($app, $info['repair-steps']['install']);
590
-
591
-		$config = \OC::$server->getConfig();
592
-
593
-		$config->setAppValue($app, 'installed_version', OC_App::getAppVersion($app));
594
-		if (array_key_exists('ocsid', $info)) {
595
-			$config->setAppValue($app, 'ocsid', $info['ocsid']);
596
-		}
597
-
598
-		//set remote/public handlers
599
-		foreach($info['remote'] as $name=>$path) {
600
-			$config->setAppValue('core', 'remote_'.$name, $app.'/'.$path);
601
-		}
602
-		foreach($info['public'] as $name=>$path) {
603
-			$config->setAppValue('core', 'public_'.$name, $app.'/'.$path);
604
-		}
605
-
606
-		OC_App::setAppTypes($info['id']);
607
-
608
-		return $info['id'];
609
-	}
610
-
611
-	/**
612
-	 * @param string $script
613
-	 */
614
-	private static function includeAppScript($script) {
615
-		if ( file_exists($script) ){
616
-			include $script;
617
-		}
618
-	}
60
+    /** @var AppFetcher */
61
+    private $appFetcher;
62
+    /** @var IClientService */
63
+    private $clientService;
64
+    /** @var ITempManager */
65
+    private $tempManager;
66
+    /** @var ILogger */
67
+    private $logger;
68
+    /** @var IConfig */
69
+    private $config;
70
+    /** @var array - for caching the result of app fetcher */
71
+    private $apps = null;
72
+    /** @var bool|null - for caching the result of the ready status */
73
+    private $isInstanceReadyForUpdates = null;
74
+
75
+    /**
76
+     * @param AppFetcher $appFetcher
77
+     * @param IClientService $clientService
78
+     * @param ITempManager $tempManager
79
+     * @param ILogger $logger
80
+     * @param IConfig $config
81
+     */
82
+    public function __construct(AppFetcher $appFetcher,
83
+                                IClientService $clientService,
84
+                                ITempManager $tempManager,
85
+                                ILogger $logger,
86
+                                IConfig $config) {
87
+        $this->appFetcher = $appFetcher;
88
+        $this->clientService = $clientService;
89
+        $this->tempManager = $tempManager;
90
+        $this->logger = $logger;
91
+        $this->config = $config;
92
+    }
93
+
94
+    /**
95
+     * Installs an app that is located in one of the app folders already
96
+     *
97
+     * @param string $appId App to install
98
+     * @throws \Exception
99
+     * @return string app ID
100
+     */
101
+    public function installApp($appId) {
102
+        $app = \OC_App::findAppInDirectories($appId);
103
+        if($app === false) {
104
+            throw new \Exception('App not found in any app directory');
105
+        }
106
+
107
+        $basedir = $app['path'].'/'.$appId;
108
+        $info = OC_App::getAppInfo($basedir.'/appinfo/info.xml', true);
109
+
110
+        $l = \OC::$server->getL10N('core');
111
+
112
+        if(!is_array($info)) {
113
+            throw new \Exception(
114
+                $l->t('App "%s" cannot be installed because appinfo file cannot be read.',
115
+                    [$info['name']]
116
+                )
117
+            );
118
+        }
119
+
120
+        $version = \OCP\Util::getVersion();
121
+        if (!\OC_App::isAppCompatible($version, $info)) {
122
+            throw new \Exception(
123
+                // TODO $l
124
+                $l->t('App "%s" cannot be installed because it is not compatible with this version of the server.',
125
+                    [$info['name']]
126
+                )
127
+            );
128
+        }
129
+
130
+        // check for required dependencies
131
+        \OC_App::checkAppDependencies($this->config, $l, $info);
132
+        \OC_App::registerAutoloading($appId, $basedir);
133
+
134
+        //install the database
135
+        if(is_file($basedir.'/appinfo/database.xml')) {
136
+            if (\OC::$server->getConfig()->getAppValue($info['id'], 'installed_version') === null) {
137
+                OC_DB::createDbFromStructure($basedir.'/appinfo/database.xml');
138
+            } else {
139
+                OC_DB::updateDbFromStructure($basedir.'/appinfo/database.xml');
140
+            }
141
+        } else {
142
+            $ms = new \OC\DB\MigrationService($info['id'], \OC::$server->getDatabaseConnection());
143
+            $ms->migrate();
144
+        }
145
+
146
+        \OC_App::setupBackgroundJobs($info['background-jobs']);
147
+
148
+        //run appinfo/install.php
149
+        if(!isset($data['noinstall']) or $data['noinstall']==false) {
150
+            self::includeAppScript($basedir . '/appinfo/install.php');
151
+        }
152
+
153
+        $appData = OC_App::getAppInfo($appId);
154
+        OC_App::executeRepairSteps($appId, $appData['repair-steps']['install']);
155
+
156
+        //set the installed version
157
+        \OC::$server->getConfig()->setAppValue($info['id'], 'installed_version', OC_App::getAppVersion($info['id'], false));
158
+        \OC::$server->getConfig()->setAppValue($info['id'], 'enabled', 'no');
159
+
160
+        //set remote/public handlers
161
+        foreach($info['remote'] as $name=>$path) {
162
+            \OC::$server->getConfig()->setAppValue('core', 'remote_'.$name, $info['id'].'/'.$path);
163
+        }
164
+        foreach($info['public'] as $name=>$path) {
165
+            \OC::$server->getConfig()->setAppValue('core', 'public_'.$name, $info['id'].'/'.$path);
166
+        }
167
+
168
+        OC_App::setAppTypes($info['id']);
169
+
170
+        return $info['id'];
171
+    }
172
+
173
+    /**
174
+     * @brief checks whether or not an app is installed
175
+     * @param string $app app
176
+     * @returns bool
177
+     *
178
+     * Checks whether or not an app is installed, i.e. registered in apps table.
179
+     */
180
+    public static function isInstalled( $app ) {
181
+        return (\OC::$server->getConfig()->getAppValue($app, "installed_version", null) !== null);
182
+    }
183
+
184
+    /**
185
+     * Updates the specified app from the appstore
186
+     *
187
+     * @param string $appId
188
+     * @return bool
189
+     */
190
+    public function updateAppstoreApp($appId) {
191
+        if($this->isUpdateAvailable($appId)) {
192
+            try {
193
+                $this->downloadApp($appId);
194
+            } catch (\Exception $e) {
195
+                $this->logger->logException($e, [
196
+                    'level' => \OCP\Util::ERROR,
197
+                    'app' => 'core',
198
+                ]);
199
+                return false;
200
+            }
201
+            return OC_App::updateApp($appId);
202
+        }
203
+
204
+        return false;
205
+    }
206
+
207
+    /**
208
+     * Downloads an app and puts it into the app directory
209
+     *
210
+     * @param string $appId
211
+     *
212
+     * @throws \Exception If the installation was not successful
213
+     */
214
+    public function downloadApp($appId) {
215
+        $appId = strtolower($appId);
216
+
217
+        $apps = $this->appFetcher->get();
218
+        foreach($apps as $app) {
219
+            if($app['id'] === $appId) {
220
+                // Load the certificate
221
+                $certificate = new X509();
222
+                $certificate->loadCA(file_get_contents(__DIR__ . '/../../resources/codesigning/root.crt'));
223
+                $loadedCertificate = $certificate->loadX509($app['certificate']);
224
+
225
+                // Verify if the certificate has been revoked
226
+                $crl = new X509();
227
+                $crl->loadCA(file_get_contents(__DIR__ . '/../../resources/codesigning/root.crt'));
228
+                $crl->loadCRL(file_get_contents(__DIR__ . '/../../resources/codesigning/root.crl'));
229
+                if($crl->validateSignature() !== true) {
230
+                    throw new \Exception('Could not validate CRL signature');
231
+                }
232
+                $csn = $loadedCertificate['tbsCertificate']['serialNumber']->toString();
233
+                $revoked = $crl->getRevoked($csn);
234
+                if ($revoked !== false) {
235
+                    throw new \Exception(
236
+                        sprintf(
237
+                            'Certificate "%s" has been revoked',
238
+                            $csn
239
+                        )
240
+                    );
241
+                }
242
+
243
+                // Verify if the certificate has been issued by the Nextcloud Code Authority CA
244
+                if($certificate->validateSignature() !== true) {
245
+                    throw new \Exception(
246
+                        sprintf(
247
+                            'App with id %s has a certificate not issued by a trusted Code Signing Authority',
248
+                            $appId
249
+                        )
250
+                    );
251
+                }
252
+
253
+                // Verify if the certificate is issued for the requested app id
254
+                $certInfo = openssl_x509_parse($app['certificate']);
255
+                if(!isset($certInfo['subject']['CN'])) {
256
+                    throw new \Exception(
257
+                        sprintf(
258
+                            'App with id %s has a cert with no CN',
259
+                            $appId
260
+                        )
261
+                    );
262
+                }
263
+                if($certInfo['subject']['CN'] !== $appId) {
264
+                    throw new \Exception(
265
+                        sprintf(
266
+                            'App with id %s has a cert issued to %s',
267
+                            $appId,
268
+                            $certInfo['subject']['CN']
269
+                        )
270
+                    );
271
+                }
272
+
273
+                // Download the release
274
+                $tempFile = $this->tempManager->getTemporaryFile('.tar.gz');
275
+                $client = $this->clientService->newClient();
276
+                $client->get($app['releases'][0]['download'], ['save_to' => $tempFile]);
277
+
278
+                // Check if the signature actually matches the downloaded content
279
+                $certificate = openssl_get_publickey($app['certificate']);
280
+                $verified = (bool)openssl_verify(file_get_contents($tempFile), base64_decode($app['releases'][0]['signature']), $certificate, OPENSSL_ALGO_SHA512);
281
+                openssl_free_key($certificate);
282
+
283
+                if($verified === true) {
284
+                    // Seems to match, let's proceed
285
+                    $extractDir = $this->tempManager->getTemporaryFolder();
286
+                    $archive = new TAR($tempFile);
287
+
288
+                    if($archive) {
289
+                        if (!$archive->extract($extractDir)) {
290
+                            throw new \Exception(
291
+                                sprintf(
292
+                                    'Could not extract app %s',
293
+                                    $appId
294
+                                )
295
+                            );
296
+                        }
297
+                        $allFiles = scandir($extractDir);
298
+                        $folders = array_diff($allFiles, ['.', '..']);
299
+                        $folders = array_values($folders);
300
+
301
+                        if(count($folders) > 1) {
302
+                            throw new \Exception(
303
+                                sprintf(
304
+                                    'Extracted app %s has more than 1 folder',
305
+                                    $appId
306
+                                )
307
+                            );
308
+                        }
309
+
310
+                        // Check if appinfo/info.xml has the same app ID as well
311
+                        $loadEntities = libxml_disable_entity_loader(false);
312
+                        $xml = simplexml_load_file($extractDir . '/' . $folders[0] . '/appinfo/info.xml');
313
+                        libxml_disable_entity_loader($loadEntities);
314
+                        if((string)$xml->id !== $appId) {
315
+                            throw new \Exception(
316
+                                sprintf(
317
+                                    'App for id %s has a wrong app ID in info.xml: %s',
318
+                                    $appId,
319
+                                    (string)$xml->id
320
+                                )
321
+                            );
322
+                        }
323
+
324
+                        // Check if the version is lower than before
325
+                        $currentVersion = OC_App::getAppVersion($appId);
326
+                        $newVersion = (string)$xml->version;
327
+                        if(version_compare($currentVersion, $newVersion) === 1) {
328
+                            throw new \Exception(
329
+                                sprintf(
330
+                                    'App for id %s has version %s and tried to update to lower version %s',
331
+                                    $appId,
332
+                                    $currentVersion,
333
+                                    $newVersion
334
+                                )
335
+                            );
336
+                        }
337
+
338
+                        $baseDir = OC_App::getInstallPath() . '/' . $appId;
339
+                        // Remove old app with the ID if existent
340
+                        OC_Helper::rmdirr($baseDir);
341
+                        // Move to app folder
342
+                        if(@mkdir($baseDir)) {
343
+                            $extractDir .= '/' . $folders[0];
344
+                            OC_Helper::copyr($extractDir, $baseDir);
345
+                        }
346
+                        OC_Helper::copyr($extractDir, $baseDir);
347
+                        OC_Helper::rmdirr($extractDir);
348
+                        return;
349
+                    } else {
350
+                        throw new \Exception(
351
+                            sprintf(
352
+                                'Could not extract app with ID %s to %s',
353
+                                $appId,
354
+                                $extractDir
355
+                            )
356
+                        );
357
+                    }
358
+                } else {
359
+                    // Signature does not match
360
+                    throw new \Exception(
361
+                        sprintf(
362
+                            'App with id %s has invalid signature',
363
+                            $appId
364
+                        )
365
+                    );
366
+                }
367
+            }
368
+        }
369
+
370
+        throw new \Exception(
371
+            sprintf(
372
+                'Could not download app %s',
373
+                $appId
374
+            )
375
+        );
376
+    }
377
+
378
+    /**
379
+     * Check if an update for the app is available
380
+     *
381
+     * @param string $appId
382
+     * @return string|false false or the version number of the update
383
+     */
384
+    public function isUpdateAvailable($appId) {
385
+        if ($this->isInstanceReadyForUpdates === null) {
386
+            $installPath = OC_App::getInstallPath();
387
+            if ($installPath === false || $installPath === null) {
388
+                $this->isInstanceReadyForUpdates = false;
389
+            } else {
390
+                $this->isInstanceReadyForUpdates = true;
391
+            }
392
+        }
393
+
394
+        if ($this->isInstanceReadyForUpdates === false) {
395
+            return false;
396
+        }
397
+
398
+        if ($this->isInstalledFromGit($appId) === true) {
399
+            return false;
400
+        }
401
+
402
+        if ($this->apps === null) {
403
+            $this->apps = $this->appFetcher->get();
404
+        }
405
+
406
+        foreach($this->apps as $app) {
407
+            if($app['id'] === $appId) {
408
+                $currentVersion = OC_App::getAppVersion($appId);
409
+                $newestVersion = $app['releases'][0]['version'];
410
+                if (version_compare($newestVersion, $currentVersion, '>')) {
411
+                    return $newestVersion;
412
+                } else {
413
+                    return false;
414
+                }
415
+            }
416
+        }
417
+
418
+        return false;
419
+    }
420
+
421
+    /**
422
+     * Check if app has been installed from git
423
+     * @param string $name name of the application to remove
424
+     * @return boolean
425
+     *
426
+     * The function will check if the path contains a .git folder
427
+     */
428
+    private function isInstalledFromGit($appId) {
429
+        $app = \OC_App::findAppInDirectories($appId);
430
+        if($app === false) {
431
+            return false;
432
+        }
433
+        $basedir = $app['path'].'/'.$appId;
434
+        return file_exists($basedir.'/.git/');
435
+    }
436
+
437
+    /**
438
+     * Check if app is already downloaded
439
+     * @param string $name name of the application to remove
440
+     * @return boolean
441
+     *
442
+     * The function will check if the app is already downloaded in the apps repository
443
+     */
444
+    public function isDownloaded($name) {
445
+        foreach(\OC::$APPSROOTS as $dir) {
446
+            $dirToTest  = $dir['path'];
447
+            $dirToTest .= '/';
448
+            $dirToTest .= $name;
449
+            $dirToTest .= '/';
450
+
451
+            if (is_dir($dirToTest)) {
452
+                return true;
453
+            }
454
+        }
455
+
456
+        return false;
457
+    }
458
+
459
+    /**
460
+     * Removes an app
461
+     * @param string $appId ID of the application to remove
462
+     * @return boolean
463
+     *
464
+     *
465
+     * This function works as follows
466
+     *   -# call uninstall repair steps
467
+     *   -# removing the files
468
+     *
469
+     * The function will not delete preferences, tables and the configuration,
470
+     * this has to be done by the function oc_app_uninstall().
471
+     */
472
+    public function removeApp($appId) {
473
+        if($this->isDownloaded( $appId )) {
474
+            if (\OC::$server->getAppManager()->isShipped($appId)) {
475
+                return false;
476
+            }
477
+            $appDir = OC_App::getInstallPath() . '/' . $appId;
478
+            OC_Helper::rmdirr($appDir);
479
+            return true;
480
+        }else{
481
+            \OCP\Util::writeLog('core', 'can\'t remove app '.$appId.'. It is not installed.', \OCP\Util::ERROR);
482
+
483
+            return false;
484
+        }
485
+
486
+    }
487
+
488
+    /**
489
+     * Installs the app within the bundle and marks the bundle as installed
490
+     *
491
+     * @param Bundle $bundle
492
+     * @throws \Exception If app could not get installed
493
+     */
494
+    public function installAppBundle(Bundle $bundle) {
495
+        $appIds = $bundle->getAppIdentifiers();
496
+        foreach($appIds as $appId) {
497
+            if(!$this->isDownloaded($appId)) {
498
+                $this->downloadApp($appId);
499
+            }
500
+            $this->installApp($appId);
501
+            $app = new OC_App();
502
+            $app->enable($appId);
503
+        }
504
+        $bundles = json_decode($this->config->getAppValue('core', 'installed.bundles', json_encode([])), true);
505
+        $bundles[] = $bundle->getIdentifier();
506
+        $this->config->setAppValue('core', 'installed.bundles', json_encode($bundles));
507
+    }
508
+
509
+    /**
510
+     * Installs shipped apps
511
+     *
512
+     * This function installs all apps found in the 'apps' directory that should be enabled by default;
513
+     * @param bool $softErrors When updating we ignore errors and simply log them, better to have a
514
+     *                         working ownCloud at the end instead of an aborted update.
515
+     * @return array Array of error messages (appid => Exception)
516
+     */
517
+    public static function installShippedApps($softErrors = false) {
518
+        $errors = [];
519
+        foreach(\OC::$APPSROOTS as $app_dir) {
520
+            if($dir = opendir( $app_dir['path'] )) {
521
+                while( false !== ( $filename = readdir( $dir ))) {
522
+                    if( $filename[0] !== '.' and is_dir($app_dir['path']."/$filename") ) {
523
+                        if( file_exists( $app_dir['path']."/$filename/appinfo/info.xml" )) {
524
+                            if(!Installer::isInstalled($filename)) {
525
+                                $info=OC_App::getAppInfo($filename);
526
+                                $enabled = isset($info['default_enable']);
527
+                                if (($enabled || in_array($filename, \OC::$server->getAppManager()->getAlwaysEnabledApps()))
528
+                                      && \OC::$server->getConfig()->getAppValue($filename, 'enabled') !== 'no') {
529
+                                    if ($softErrors) {
530
+                                        try {
531
+                                            Installer::installShippedApp($filename);
532
+                                        } catch (HintException $e) {
533
+                                            if ($e->getPrevious() instanceof TableExistsException) {
534
+                                                $errors[$filename] = $e;
535
+                                                continue;
536
+                                            }
537
+                                            throw $e;
538
+                                        }
539
+                                    } else {
540
+                                        Installer::installShippedApp($filename);
541
+                                    }
542
+                                    \OC::$server->getConfig()->setAppValue($filename, 'enabled', 'yes');
543
+                                }
544
+                            }
545
+                        }
546
+                    }
547
+                }
548
+                closedir( $dir );
549
+            }
550
+        }
551
+
552
+        return $errors;
553
+    }
554
+
555
+    /**
556
+     * install an app already placed in the app folder
557
+     * @param string $app id of the app to install
558
+     * @return integer
559
+     */
560
+    public static function installShippedApp($app) {
561
+        //install the database
562
+        $appPath = OC_App::getAppPath($app);
563
+        \OC_App::registerAutoloading($app, $appPath);
564
+
565
+        if(is_file("$appPath/appinfo/database.xml")) {
566
+            try {
567
+                OC_DB::createDbFromStructure("$appPath/appinfo/database.xml");
568
+            } catch (TableExistsException $e) {
569
+                throw new HintException(
570
+                    'Failed to enable app ' . $app,
571
+                    'Please ask for help via one of our <a href="https://nextcloud.com/support/" target="_blank" rel="noreferrer noopener">support channels</a>.',
572
+                    0, $e
573
+                );
574
+            }
575
+        } else {
576
+            $ms = new \OC\DB\MigrationService($app, \OC::$server->getDatabaseConnection());
577
+            $ms->migrate();
578
+        }
579
+
580
+        //run appinfo/install.php
581
+        self::includeAppScript("$appPath/appinfo/install.php");
582
+
583
+        $info = OC_App::getAppInfo($app);
584
+        if (is_null($info)) {
585
+            return false;
586
+        }
587
+        \OC_App::setupBackgroundJobs($info['background-jobs']);
588
+
589
+        OC_App::executeRepairSteps($app, $info['repair-steps']['install']);
590
+
591
+        $config = \OC::$server->getConfig();
592
+
593
+        $config->setAppValue($app, 'installed_version', OC_App::getAppVersion($app));
594
+        if (array_key_exists('ocsid', $info)) {
595
+            $config->setAppValue($app, 'ocsid', $info['ocsid']);
596
+        }
597
+
598
+        //set remote/public handlers
599
+        foreach($info['remote'] as $name=>$path) {
600
+            $config->setAppValue('core', 'remote_'.$name, $app.'/'.$path);
601
+        }
602
+        foreach($info['public'] as $name=>$path) {
603
+            $config->setAppValue('core', 'public_'.$name, $app.'/'.$path);
604
+        }
605
+
606
+        OC_App::setAppTypes($info['id']);
607
+
608
+        return $info['id'];
609
+    }
610
+
611
+    /**
612
+     * @param string $script
613
+     */
614
+    private static function includeAppScript($script) {
615
+        if ( file_exists($script) ){
616
+            include $script;
617
+        }
618
+    }
619 619
 }
Please login to merge, or discard this patch.
Spacing   +52 added lines, -52 removed lines patch added patch discarded remove patch
@@ -100,7 +100,7 @@  discard block
 block discarded – undo
100 100
 	 */
101 101
 	public function installApp($appId) {
102 102
 		$app = \OC_App::findAppInDirectories($appId);
103
-		if($app === false) {
103
+		if ($app === false) {
104 104
 			throw new \Exception('App not found in any app directory');
105 105
 		}
106 106
 
@@ -109,7 +109,7 @@  discard block
 block discarded – undo
109 109
 
110 110
 		$l = \OC::$server->getL10N('core');
111 111
 
112
-		if(!is_array($info)) {
112
+		if (!is_array($info)) {
113 113
 			throw new \Exception(
114 114
 				$l->t('App "%s" cannot be installed because appinfo file cannot be read.',
115 115
 					[$info['name']]
@@ -132,7 +132,7 @@  discard block
 block discarded – undo
132 132
 		\OC_App::registerAutoloading($appId, $basedir);
133 133
 
134 134
 		//install the database
135
-		if(is_file($basedir.'/appinfo/database.xml')) {
135
+		if (is_file($basedir.'/appinfo/database.xml')) {
136 136
 			if (\OC::$server->getConfig()->getAppValue($info['id'], 'installed_version') === null) {
137 137
 				OC_DB::createDbFromStructure($basedir.'/appinfo/database.xml');
138 138
 			} else {
@@ -146,8 +146,8 @@  discard block
 block discarded – undo
146 146
 		\OC_App::setupBackgroundJobs($info['background-jobs']);
147 147
 
148 148
 		//run appinfo/install.php
149
-		if(!isset($data['noinstall']) or $data['noinstall']==false) {
150
-			self::includeAppScript($basedir . '/appinfo/install.php');
149
+		if (!isset($data['noinstall']) or $data['noinstall'] == false) {
150
+			self::includeAppScript($basedir.'/appinfo/install.php');
151 151
 		}
152 152
 
153 153
 		$appData = OC_App::getAppInfo($appId);
@@ -158,10 +158,10 @@  discard block
 block discarded – undo
158 158
 		\OC::$server->getConfig()->setAppValue($info['id'], 'enabled', 'no');
159 159
 
160 160
 		//set remote/public handlers
161
-		foreach($info['remote'] as $name=>$path) {
161
+		foreach ($info['remote'] as $name=>$path) {
162 162
 			\OC::$server->getConfig()->setAppValue('core', 'remote_'.$name, $info['id'].'/'.$path);
163 163
 		}
164
-		foreach($info['public'] as $name=>$path) {
164
+		foreach ($info['public'] as $name=>$path) {
165 165
 			\OC::$server->getConfig()->setAppValue('core', 'public_'.$name, $info['id'].'/'.$path);
166 166
 		}
167 167
 
@@ -177,7 +177,7 @@  discard block
 block discarded – undo
177 177
 	 *
178 178
 	 * Checks whether or not an app is installed, i.e. registered in apps table.
179 179
 	 */
180
-	public static function isInstalled( $app ) {
180
+	public static function isInstalled($app) {
181 181
 		return (\OC::$server->getConfig()->getAppValue($app, "installed_version", null) !== null);
182 182
 	}
183 183
 
@@ -188,7 +188,7 @@  discard block
 block discarded – undo
188 188
 	 * @return bool
189 189
 	 */
190 190
 	public function updateAppstoreApp($appId) {
191
-		if($this->isUpdateAvailable($appId)) {
191
+		if ($this->isUpdateAvailable($appId)) {
192 192
 			try {
193 193
 				$this->downloadApp($appId);
194 194
 			} catch (\Exception $e) {
@@ -215,18 +215,18 @@  discard block
 block discarded – undo
215 215
 		$appId = strtolower($appId);
216 216
 
217 217
 		$apps = $this->appFetcher->get();
218
-		foreach($apps as $app) {
219
-			if($app['id'] === $appId) {
218
+		foreach ($apps as $app) {
219
+			if ($app['id'] === $appId) {
220 220
 				// Load the certificate
221 221
 				$certificate = new X509();
222
-				$certificate->loadCA(file_get_contents(__DIR__ . '/../../resources/codesigning/root.crt'));
222
+				$certificate->loadCA(file_get_contents(__DIR__.'/../../resources/codesigning/root.crt'));
223 223
 				$loadedCertificate = $certificate->loadX509($app['certificate']);
224 224
 
225 225
 				// Verify if the certificate has been revoked
226 226
 				$crl = new X509();
227
-				$crl->loadCA(file_get_contents(__DIR__ . '/../../resources/codesigning/root.crt'));
228
-				$crl->loadCRL(file_get_contents(__DIR__ . '/../../resources/codesigning/root.crl'));
229
-				if($crl->validateSignature() !== true) {
227
+				$crl->loadCA(file_get_contents(__DIR__.'/../../resources/codesigning/root.crt'));
228
+				$crl->loadCRL(file_get_contents(__DIR__.'/../../resources/codesigning/root.crl'));
229
+				if ($crl->validateSignature() !== true) {
230 230
 					throw new \Exception('Could not validate CRL signature');
231 231
 				}
232 232
 				$csn = $loadedCertificate['tbsCertificate']['serialNumber']->toString();
@@ -241,7 +241,7 @@  discard block
 block discarded – undo
241 241
 				}
242 242
 
243 243
 				// Verify if the certificate has been issued by the Nextcloud Code Authority CA
244
-				if($certificate->validateSignature() !== true) {
244
+				if ($certificate->validateSignature() !== true) {
245 245
 					throw new \Exception(
246 246
 						sprintf(
247 247
 							'App with id %s has a certificate not issued by a trusted Code Signing Authority',
@@ -252,7 +252,7 @@  discard block
 block discarded – undo
252 252
 
253 253
 				// Verify if the certificate is issued for the requested app id
254 254
 				$certInfo = openssl_x509_parse($app['certificate']);
255
-				if(!isset($certInfo['subject']['CN'])) {
255
+				if (!isset($certInfo['subject']['CN'])) {
256 256
 					throw new \Exception(
257 257
 						sprintf(
258 258
 							'App with id %s has a cert with no CN',
@@ -260,7 +260,7 @@  discard block
 block discarded – undo
260 260
 						)
261 261
 					);
262 262
 				}
263
-				if($certInfo['subject']['CN'] !== $appId) {
263
+				if ($certInfo['subject']['CN'] !== $appId) {
264 264
 					throw new \Exception(
265 265
 						sprintf(
266 266
 							'App with id %s has a cert issued to %s',
@@ -277,15 +277,15 @@  discard block
 block discarded – undo
277 277
 
278 278
 				// Check if the signature actually matches the downloaded content
279 279
 				$certificate = openssl_get_publickey($app['certificate']);
280
-				$verified = (bool)openssl_verify(file_get_contents($tempFile), base64_decode($app['releases'][0]['signature']), $certificate, OPENSSL_ALGO_SHA512);
280
+				$verified = (bool) openssl_verify(file_get_contents($tempFile), base64_decode($app['releases'][0]['signature']), $certificate, OPENSSL_ALGO_SHA512);
281 281
 				openssl_free_key($certificate);
282 282
 
283
-				if($verified === true) {
283
+				if ($verified === true) {
284 284
 					// Seems to match, let's proceed
285 285
 					$extractDir = $this->tempManager->getTemporaryFolder();
286 286
 					$archive = new TAR($tempFile);
287 287
 
288
-					if($archive) {
288
+					if ($archive) {
289 289
 						if (!$archive->extract($extractDir)) {
290 290
 							throw new \Exception(
291 291
 								sprintf(
@@ -298,7 +298,7 @@  discard block
 block discarded – undo
298 298
 						$folders = array_diff($allFiles, ['.', '..']);
299 299
 						$folders = array_values($folders);
300 300
 
301
-						if(count($folders) > 1) {
301
+						if (count($folders) > 1) {
302 302
 							throw new \Exception(
303 303
 								sprintf(
304 304
 									'Extracted app %s has more than 1 folder',
@@ -309,22 +309,22 @@  discard block
 block discarded – undo
309 309
 
310 310
 						// Check if appinfo/info.xml has the same app ID as well
311 311
 						$loadEntities = libxml_disable_entity_loader(false);
312
-						$xml = simplexml_load_file($extractDir . '/' . $folders[0] . '/appinfo/info.xml');
312
+						$xml = simplexml_load_file($extractDir.'/'.$folders[0].'/appinfo/info.xml');
313 313
 						libxml_disable_entity_loader($loadEntities);
314
-						if((string)$xml->id !== $appId) {
314
+						if ((string) $xml->id !== $appId) {
315 315
 							throw new \Exception(
316 316
 								sprintf(
317 317
 									'App for id %s has a wrong app ID in info.xml: %s',
318 318
 									$appId,
319
-									(string)$xml->id
319
+									(string) $xml->id
320 320
 								)
321 321
 							);
322 322
 						}
323 323
 
324 324
 						// Check if the version is lower than before
325 325
 						$currentVersion = OC_App::getAppVersion($appId);
326
-						$newVersion = (string)$xml->version;
327
-						if(version_compare($currentVersion, $newVersion) === 1) {
326
+						$newVersion = (string) $xml->version;
327
+						if (version_compare($currentVersion, $newVersion) === 1) {
328 328
 							throw new \Exception(
329 329
 								sprintf(
330 330
 									'App for id %s has version %s and tried to update to lower version %s',
@@ -335,12 +335,12 @@  discard block
 block discarded – undo
335 335
 							);
336 336
 						}
337 337
 
338
-						$baseDir = OC_App::getInstallPath() . '/' . $appId;
338
+						$baseDir = OC_App::getInstallPath().'/'.$appId;
339 339
 						// Remove old app with the ID if existent
340 340
 						OC_Helper::rmdirr($baseDir);
341 341
 						// Move to app folder
342
-						if(@mkdir($baseDir)) {
343
-							$extractDir .= '/' . $folders[0];
342
+						if (@mkdir($baseDir)) {
343
+							$extractDir .= '/'.$folders[0];
344 344
 							OC_Helper::copyr($extractDir, $baseDir);
345 345
 						}
346 346
 						OC_Helper::copyr($extractDir, $baseDir);
@@ -403,8 +403,8 @@  discard block
 block discarded – undo
403 403
 			$this->apps = $this->appFetcher->get();
404 404
 		}
405 405
 
406
-		foreach($this->apps as $app) {
407
-			if($app['id'] === $appId) {
406
+		foreach ($this->apps as $app) {
407
+			if ($app['id'] === $appId) {
408 408
 				$currentVersion = OC_App::getAppVersion($appId);
409 409
 				$newestVersion = $app['releases'][0]['version'];
410 410
 				if (version_compare($newestVersion, $currentVersion, '>')) {
@@ -427,7 +427,7 @@  discard block
 block discarded – undo
427 427
 	 */
428 428
 	private function isInstalledFromGit($appId) {
429 429
 		$app = \OC_App::findAppInDirectories($appId);
430
-		if($app === false) {
430
+		if ($app === false) {
431 431
 			return false;
432 432
 		}
433 433
 		$basedir = $app['path'].'/'.$appId;
@@ -442,7 +442,7 @@  discard block
 block discarded – undo
442 442
 	 * The function will check if the app is already downloaded in the apps repository
443 443
 	 */
444 444
 	public function isDownloaded($name) {
445
-		foreach(\OC::$APPSROOTS as $dir) {
445
+		foreach (\OC::$APPSROOTS as $dir) {
446 446
 			$dirToTest  = $dir['path'];
447 447
 			$dirToTest .= '/';
448 448
 			$dirToTest .= $name;
@@ -470,14 +470,14 @@  discard block
 block discarded – undo
470 470
 	 * this has to be done by the function oc_app_uninstall().
471 471
 	 */
472 472
 	public function removeApp($appId) {
473
-		if($this->isDownloaded( $appId )) {
473
+		if ($this->isDownloaded($appId)) {
474 474
 			if (\OC::$server->getAppManager()->isShipped($appId)) {
475 475
 				return false;
476 476
 			}
477
-			$appDir = OC_App::getInstallPath() . '/' . $appId;
477
+			$appDir = OC_App::getInstallPath().'/'.$appId;
478 478
 			OC_Helper::rmdirr($appDir);
479 479
 			return true;
480
-		}else{
480
+		} else {
481 481
 			\OCP\Util::writeLog('core', 'can\'t remove app '.$appId.'. It is not installed.', \OCP\Util::ERROR);
482 482
 
483 483
 			return false;
@@ -493,8 +493,8 @@  discard block
 block discarded – undo
493 493
 	 */
494 494
 	public function installAppBundle(Bundle $bundle) {
495 495
 		$appIds = $bundle->getAppIdentifiers();
496
-		foreach($appIds as $appId) {
497
-			if(!$this->isDownloaded($appId)) {
496
+		foreach ($appIds as $appId) {
497
+			if (!$this->isDownloaded($appId)) {
498 498
 				$this->downloadApp($appId);
499 499
 			}
500 500
 			$this->installApp($appId);
@@ -516,13 +516,13 @@  discard block
 block discarded – undo
516 516
 	 */
517 517
 	public static function installShippedApps($softErrors = false) {
518 518
 		$errors = [];
519
-		foreach(\OC::$APPSROOTS as $app_dir) {
520
-			if($dir = opendir( $app_dir['path'] )) {
521
-				while( false !== ( $filename = readdir( $dir ))) {
522
-					if( $filename[0] !== '.' and is_dir($app_dir['path']."/$filename") ) {
523
-						if( file_exists( $app_dir['path']."/$filename/appinfo/info.xml" )) {
524
-							if(!Installer::isInstalled($filename)) {
525
-								$info=OC_App::getAppInfo($filename);
519
+		foreach (\OC::$APPSROOTS as $app_dir) {
520
+			if ($dir = opendir($app_dir['path'])) {
521
+				while (false !== ($filename = readdir($dir))) {
522
+					if ($filename[0] !== '.' and is_dir($app_dir['path']."/$filename")) {
523
+						if (file_exists($app_dir['path']."/$filename/appinfo/info.xml")) {
524
+							if (!Installer::isInstalled($filename)) {
525
+								$info = OC_App::getAppInfo($filename);
526 526
 								$enabled = isset($info['default_enable']);
527 527
 								if (($enabled || in_array($filename, \OC::$server->getAppManager()->getAlwaysEnabledApps()))
528 528
 									  && \OC::$server->getConfig()->getAppValue($filename, 'enabled') !== 'no') {
@@ -545,7 +545,7 @@  discard block
 block discarded – undo
545 545
 						}
546 546
 					}
547 547
 				}
548
-				closedir( $dir );
548
+				closedir($dir);
549 549
 			}
550 550
 		}
551 551
 
@@ -562,12 +562,12 @@  discard block
 block discarded – undo
562 562
 		$appPath = OC_App::getAppPath($app);
563 563
 		\OC_App::registerAutoloading($app, $appPath);
564 564
 
565
-		if(is_file("$appPath/appinfo/database.xml")) {
565
+		if (is_file("$appPath/appinfo/database.xml")) {
566 566
 			try {
567 567
 				OC_DB::createDbFromStructure("$appPath/appinfo/database.xml");
568 568
 			} catch (TableExistsException $e) {
569 569
 				throw new HintException(
570
-					'Failed to enable app ' . $app,
570
+					'Failed to enable app '.$app,
571 571
 					'Please ask for help via one of our <a href="https://nextcloud.com/support/" target="_blank" rel="noreferrer noopener">support channels</a>.',
572 572
 					0, $e
573 573
 				);
@@ -596,10 +596,10 @@  discard block
 block discarded – undo
596 596
 		}
597 597
 
598 598
 		//set remote/public handlers
599
-		foreach($info['remote'] as $name=>$path) {
599
+		foreach ($info['remote'] as $name=>$path) {
600 600
 			$config->setAppValue('core', 'remote_'.$name, $app.'/'.$path);
601 601
 		}
602
-		foreach($info['public'] as $name=>$path) {
602
+		foreach ($info['public'] as $name=>$path) {
603 603
 			$config->setAppValue('core', 'public_'.$name, $app.'/'.$path);
604 604
 		}
605 605
 
@@ -612,7 +612,7 @@  discard block
 block discarded – undo
612 612
 	 * @param string $script
613 613
 	 */
614 614
 	private static function includeAppScript($script) {
615
-		if ( file_exists($script) ){
615
+		if (file_exists($script)) {
616 616
 			include $script;
617 617
 		}
618 618
 	}
Please login to merge, or discard this patch.
lib/private/App/CodeChecker/DeprecationCheck.php 1 patch
Indentation   +151 added lines, -151 removed lines patch added patch discarded remove patch
@@ -24,155 +24,155 @@
 block discarded – undo
24 24
 namespace OC\App\CodeChecker;
25 25
 
26 26
 class DeprecationCheck extends AbstractCheck {
27
-	/**
28
-	 * @return string
29
-	 */
30
-	protected function getLocalDescription() {
31
-		return 'deprecated';
32
-	}
33
-
34
-	/**
35
-	 * @return array E.g.: `'ClassName' => 'oc version',`
36
-	 */
37
-	protected function getLocalClasses() {
38
-		return [
39
-			'OC_JSON' => '8.2.0',
40
-
41
-			'OCP\Contacts' => '8.1.0',
42
-			'OCP\DB' => '8.1.0',
43
-			'OCP\IHelper' => '8.1.0',
44
-			'OCP\JSON' => '8.1.0',
45
-			'OCP\Response' => '8.1.0',
46
-			'OCP\AppFramework\IApi' => '8.0.0',
47
-			'OCP\User' => '13.0.0',
48
-			'OCP\BackgroundJob' => '14.0.0',
49
-			'OCP\App' => '14.0.0',
50
-		];
51
-	}
52
-
53
-	/**
54
-	 * @return array E.g.: `'ClassName::CONSTANT_NAME' => 'oc version',`
55
-	 */
56
-	protected function getLocalConstants() {
57
-		return [
58
-			'OC_API::GUEST_AUTH' => '8.2.0',
59
-			'OC_API::USER_AUTH' => '8.2.0',
60
-			'OC_API::SUBADMIN_AUTH' => '8.2.0',
61
-			'OC_API::ADMIN_AUTH' => '8.2.0',
62
-			'OC_API::RESPOND_UNAUTHORISED' => '8.2.0',
63
-			'OC_API::RESPOND_SERVER_ERROR' => '8.2.0',
64
-			'OC_API::RESPOND_NOT_FOUND' => '8.2.0',
65
-			'OC_API::RESPOND_UNKNOWN_ERROR' => '8.2.0',
66
-
67
-			'OCP::PERMISSION_CREATE' => '8.0.0',
68
-			'OCP::PERMISSION_READ' => '8.0.0',
69
-			'OCP::PERMISSION_UPDATE' => '8.0.0',
70
-			'OCP::PERMISSION_DELETE' => '8.0.0',
71
-			'OCP::PERMISSION_SHARE' => '8.0.0',
72
-			'OCP::PERMISSION_ALL' => '8.0.0',
73
-			'OCP::FILENAME_INVALID_CHARS' => '8.0.0',
74
-		];
75
-	}
76
-
77
-	/**
78
-	 * @return array E.g.: `'functionName' => 'oc version',`
79
-	 */
80
-	protected function getLocalFunctions() {
81
-		return [
82
-			'OCP::image_path' => '8.0.0',
83
-			'OCP::mimetype_icon' => '8.0.0',
84
-			'OCP::preview_icon' => '8.0.0',
85
-			'OCP::publicPreview_icon' => '8.0.0',
86
-			'OCP::human_file_size' => '8.0.0',
87
-			'OCP::relative_modified_date' => '8.0.0',
88
-			'OCP::simple_file_size' => '8.0.0',
89
-			'OCP::html_select_options' => '8.0.0',
90
-		];
91
-	}
92
-
93
-	/**
94
-	 * @return array E.g.: `'ClassName::methodName' => 'oc version',`
95
-	 */
96
-	protected function getLocalMethods() {
97
-		return [
98
-			'OC_L10N::get' => '8.2.0',
99
-
100
-			'OCP\Activity\IManager::publishActivity' => '8.2.0',
101
-
102
-			'OCP\App::register' => '8.1.0',
103
-			'OCP\App::addNavigationEntry' => '8.1.0',
104
-			'OCP\App::getActiveNavigationEntry' => '8.2.0',
105
-			'OCP\App::setActiveNavigationEntry' => '8.1.0',
106
-			'OCP\App::registerPersonal' => '14.0.0',
107
-			'OCP\App::registerAdmin' => '14.0.0',
108
-			'OC_App::getAppInfo' => '14.0.0',
109
-			'OCP\App::getAppInfo' => '14.0.0',
110
-			'OC_App::getAppVersion' => '14.0.0',
111
-			'OCP\App::getAppVersion' => '14.0.0',
112
-			'OCP\App::registerPersonal' => '14.0.0',
113
-
114
-			'OCP\AppFramework\Controller::params' => '7.0.0',
115
-			'OCP\AppFramework\Controller::getParams' => '7.0.0',
116
-			'OCP\AppFramework\Controller::method' => '7.0.0',
117
-			'OCP\AppFramework\Controller::getUploadedFile' => '7.0.0',
118
-			'OCP\AppFramework\Controller::env' => '7.0.0',
119
-			'OCP\AppFramework\Controller::cookie' => '7.0.0',
120
-			'OCP\AppFramework\Controller::render' => '7.0.0',
121
-
122
-			'OCP\AppFramework\IAppContainer::getCoreApi' => '8.0.0',
123
-			'OCP\AppFramework\IAppContainer::isLoggedIn' => '8.0.0',
124
-			'OCP\AppFramework\IAppContainer::isAdminUser' => '8.0.0',
125
-			'OCP\AppFramework\IAppContainer::log' => '8.0.0',
126
-
127
-			'OCP\BackgroundJob::registerJob' => '8.1.0',
128
-			'OCP\BackgroundJob::getExecutionType' => '14.0.0',
129
-			'OCP\BackgroundJob::setExecutionType' => '14.0.0',
130
-
131
-			'OCP\Files::tmpFile' => '8.1.0',
132
-			'OCP\Files::tmpFolder' => '8.1.0',
133
-
134
-			'OCP\IAppConfig::getValue' => '8.0.0',
135
-			'OCP\IAppConfig::deleteKey' => '8.0.0',
136
-			'OCP\IAppConfig::getKeys' => '8.0.0',
137
-			'OCP\IAppConfig::setValue' => '8.0.0',
138
-			'OCP\IAppConfig::deleteApp' => '8.0.0',
139
-
140
-			'OCP\IDBConnection::createQueryBuilder' => '8.2.0',
141
-			'OCP\IDBConnection::getExpressionBuilder' => '8.2.0',
142
-
143
-			'OCP\ISearch::search' => '8.0.0',
144
-
145
-			'OCP\IServerContainer::getCache' => '8.2.0',
146
-			'OCP\IServerContainer::getDb' => '8.1.0',
147
-			'OCP\IServerContainer::getHTTPHelper' => '8.1.0',
148
-
149
-			'OCP\User::getUser' => '8.0.0',
150
-			'OCP\User::getUsers' => '8.1.0',
151
-			'OCP\User::getDisplayName' => '8.1.0',
152
-			'OCP\User::getDisplayNames' => '8.1.0',
153
-			'OCP\User::userExists' => '8.1.0',
154
-			'OCP\User::logout' => '8.1.0',
155
-			'OCP\User::checkPassword' => '8.1.0',
156
-			'OCP\User::isLoggedIn' => '13.0.0',
157
-			'OCP\User::checkAdminUser' => '13.0.0',
158
-			'OCP\User::checkLoggedIn' => '13.0.0',
159
-
160
-			'OCP\Util::encryptedFiles' => '8.1.0',
161
-			'OCP\Util::formatDate' => '8.0.0',
162
-			'OCP\Util::generateRandomBytes' => '8.1.0',
163
-			'OCP\Util::getServerHost' => '8.1.0',
164
-			'OCP\Util::getServerProtocol' => '8.1.0',
165
-			'OCP\Util::getRequestUri' => '8.1.0',
166
-			'OCP\Util::getScriptName' => '8.1.0',
167
-			'OCP\Util::imagePath' => '8.1.0',
168
-			'OCP\Util::isValidFileName' => '8.1.0',
169
-			'OCP\Util::linkToRoute' => '8.1.0',
170
-			'OCP\Util::linkTo' => '8.1.0',
171
-			'OCP\Util::logException' => '8.2.0',
172
-			'OCP\Util::mb_str_replace' => '8.2.0',
173
-			'OCP\Util::mb_substr_replace' => '8.2.0',
174
-			'OCP\Util::sendMail' => '8.1.0',
175
-			'OCP\Util::writeLog' => '13.0.0',
176
-		];
177
-	}
27
+    /**
28
+     * @return string
29
+     */
30
+    protected function getLocalDescription() {
31
+        return 'deprecated';
32
+    }
33
+
34
+    /**
35
+     * @return array E.g.: `'ClassName' => 'oc version',`
36
+     */
37
+    protected function getLocalClasses() {
38
+        return [
39
+            'OC_JSON' => '8.2.0',
40
+
41
+            'OCP\Contacts' => '8.1.0',
42
+            'OCP\DB' => '8.1.0',
43
+            'OCP\IHelper' => '8.1.0',
44
+            'OCP\JSON' => '8.1.0',
45
+            'OCP\Response' => '8.1.0',
46
+            'OCP\AppFramework\IApi' => '8.0.0',
47
+            'OCP\User' => '13.0.0',
48
+            'OCP\BackgroundJob' => '14.0.0',
49
+            'OCP\App' => '14.0.0',
50
+        ];
51
+    }
52
+
53
+    /**
54
+     * @return array E.g.: `'ClassName::CONSTANT_NAME' => 'oc version',`
55
+     */
56
+    protected function getLocalConstants() {
57
+        return [
58
+            'OC_API::GUEST_AUTH' => '8.2.0',
59
+            'OC_API::USER_AUTH' => '8.2.0',
60
+            'OC_API::SUBADMIN_AUTH' => '8.2.0',
61
+            'OC_API::ADMIN_AUTH' => '8.2.0',
62
+            'OC_API::RESPOND_UNAUTHORISED' => '8.2.0',
63
+            'OC_API::RESPOND_SERVER_ERROR' => '8.2.0',
64
+            'OC_API::RESPOND_NOT_FOUND' => '8.2.0',
65
+            'OC_API::RESPOND_UNKNOWN_ERROR' => '8.2.0',
66
+
67
+            'OCP::PERMISSION_CREATE' => '8.0.0',
68
+            'OCP::PERMISSION_READ' => '8.0.0',
69
+            'OCP::PERMISSION_UPDATE' => '8.0.0',
70
+            'OCP::PERMISSION_DELETE' => '8.0.0',
71
+            'OCP::PERMISSION_SHARE' => '8.0.0',
72
+            'OCP::PERMISSION_ALL' => '8.0.0',
73
+            'OCP::FILENAME_INVALID_CHARS' => '8.0.0',
74
+        ];
75
+    }
76
+
77
+    /**
78
+     * @return array E.g.: `'functionName' => 'oc version',`
79
+     */
80
+    protected function getLocalFunctions() {
81
+        return [
82
+            'OCP::image_path' => '8.0.0',
83
+            'OCP::mimetype_icon' => '8.0.0',
84
+            'OCP::preview_icon' => '8.0.0',
85
+            'OCP::publicPreview_icon' => '8.0.0',
86
+            'OCP::human_file_size' => '8.0.0',
87
+            'OCP::relative_modified_date' => '8.0.0',
88
+            'OCP::simple_file_size' => '8.0.0',
89
+            'OCP::html_select_options' => '8.0.0',
90
+        ];
91
+    }
92
+
93
+    /**
94
+     * @return array E.g.: `'ClassName::methodName' => 'oc version',`
95
+     */
96
+    protected function getLocalMethods() {
97
+        return [
98
+            'OC_L10N::get' => '8.2.0',
99
+
100
+            'OCP\Activity\IManager::publishActivity' => '8.2.0',
101
+
102
+            'OCP\App::register' => '8.1.0',
103
+            'OCP\App::addNavigationEntry' => '8.1.0',
104
+            'OCP\App::getActiveNavigationEntry' => '8.2.0',
105
+            'OCP\App::setActiveNavigationEntry' => '8.1.0',
106
+            'OCP\App::registerPersonal' => '14.0.0',
107
+            'OCP\App::registerAdmin' => '14.0.0',
108
+            'OC_App::getAppInfo' => '14.0.0',
109
+            'OCP\App::getAppInfo' => '14.0.0',
110
+            'OC_App::getAppVersion' => '14.0.0',
111
+            'OCP\App::getAppVersion' => '14.0.0',
112
+            'OCP\App::registerPersonal' => '14.0.0',
113
+
114
+            'OCP\AppFramework\Controller::params' => '7.0.0',
115
+            'OCP\AppFramework\Controller::getParams' => '7.0.0',
116
+            'OCP\AppFramework\Controller::method' => '7.0.0',
117
+            'OCP\AppFramework\Controller::getUploadedFile' => '7.0.0',
118
+            'OCP\AppFramework\Controller::env' => '7.0.0',
119
+            'OCP\AppFramework\Controller::cookie' => '7.0.0',
120
+            'OCP\AppFramework\Controller::render' => '7.0.0',
121
+
122
+            'OCP\AppFramework\IAppContainer::getCoreApi' => '8.0.0',
123
+            'OCP\AppFramework\IAppContainer::isLoggedIn' => '8.0.0',
124
+            'OCP\AppFramework\IAppContainer::isAdminUser' => '8.0.0',
125
+            'OCP\AppFramework\IAppContainer::log' => '8.0.0',
126
+
127
+            'OCP\BackgroundJob::registerJob' => '8.1.0',
128
+            'OCP\BackgroundJob::getExecutionType' => '14.0.0',
129
+            'OCP\BackgroundJob::setExecutionType' => '14.0.0',
130
+
131
+            'OCP\Files::tmpFile' => '8.1.0',
132
+            'OCP\Files::tmpFolder' => '8.1.0',
133
+
134
+            'OCP\IAppConfig::getValue' => '8.0.0',
135
+            'OCP\IAppConfig::deleteKey' => '8.0.0',
136
+            'OCP\IAppConfig::getKeys' => '8.0.0',
137
+            'OCP\IAppConfig::setValue' => '8.0.0',
138
+            'OCP\IAppConfig::deleteApp' => '8.0.0',
139
+
140
+            'OCP\IDBConnection::createQueryBuilder' => '8.2.0',
141
+            'OCP\IDBConnection::getExpressionBuilder' => '8.2.0',
142
+
143
+            'OCP\ISearch::search' => '8.0.0',
144
+
145
+            'OCP\IServerContainer::getCache' => '8.2.0',
146
+            'OCP\IServerContainer::getDb' => '8.1.0',
147
+            'OCP\IServerContainer::getHTTPHelper' => '8.1.0',
148
+
149
+            'OCP\User::getUser' => '8.0.0',
150
+            'OCP\User::getUsers' => '8.1.0',
151
+            'OCP\User::getDisplayName' => '8.1.0',
152
+            'OCP\User::getDisplayNames' => '8.1.0',
153
+            'OCP\User::userExists' => '8.1.0',
154
+            'OCP\User::logout' => '8.1.0',
155
+            'OCP\User::checkPassword' => '8.1.0',
156
+            'OCP\User::isLoggedIn' => '13.0.0',
157
+            'OCP\User::checkAdminUser' => '13.0.0',
158
+            'OCP\User::checkLoggedIn' => '13.0.0',
159
+
160
+            'OCP\Util::encryptedFiles' => '8.1.0',
161
+            'OCP\Util::formatDate' => '8.0.0',
162
+            'OCP\Util::generateRandomBytes' => '8.1.0',
163
+            'OCP\Util::getServerHost' => '8.1.0',
164
+            'OCP\Util::getServerProtocol' => '8.1.0',
165
+            'OCP\Util::getRequestUri' => '8.1.0',
166
+            'OCP\Util::getScriptName' => '8.1.0',
167
+            'OCP\Util::imagePath' => '8.1.0',
168
+            'OCP\Util::isValidFileName' => '8.1.0',
169
+            'OCP\Util::linkToRoute' => '8.1.0',
170
+            'OCP\Util::linkTo' => '8.1.0',
171
+            'OCP\Util::logException' => '8.2.0',
172
+            'OCP\Util::mb_str_replace' => '8.2.0',
173
+            'OCP\Util::mb_substr_replace' => '8.2.0',
174
+            'OCP\Util::sendMail' => '8.1.0',
175
+            'OCP\Util::writeLog' => '13.0.0',
176
+        ];
177
+    }
178 178
 }
Please login to merge, or discard this patch.