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