Completed
Pull Request — master (#5173)
by Robin
14:45
created
lib/private/legacy/app.php 2 patches
Indentation   +1207 added lines, -1207 removed lines patch added patch discarded remove patch
@@ -60,1211 +60,1211 @@
 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
-			if (is_callable($form)) {
703
-				$result = $form();
704
-				if (is_string($result)){
705
-					$forms[] = include $result;
706
-				}
707
-			} else {
708
-				$forms[] = include $form;
709
-			}
710
-		}
711
-		return $forms;
712
-	}
713
-
714
-	/**
715
-	 * register an admin form to be shown
716
-	 *
717
-	 * @param string $app
718
-	 * @param string|callable $page
719
-	 */
720
-	public static function registerAdmin($app, $page) {
721
-		if (is_callable($page)) {
722
-			self::$adminForms[] = function() use ($app, $page) {
723
-				$result = $page();
724
-				return is_string($page) ? $app . '/' . $result . '.php' : false;
725
-			};
726
-		} else {
727
-			self::$adminForms[] = $app . '/' . $page . '.php';
728
-		}
729
-	}
730
-
731
-	/**
732
-	 * register a personal form to be shown
733
-	 *
734
-	 * @param string $app
735
-	 * @param string|callable $page
736
-	 */
737
-	public static function registerPersonal($app, $page) {
738
-		if (is_callable($page)) {
739
-			self::$personalForms[] = function() use ($app, $page) {
740
-				$result = $page();
741
-				return is_string($result) ? $app . '/' . $result . '.php' : false;
742
-			};
743
-		} else {
744
-			self::$personalForms[] = $app . '/' . $page . '.php';
745
-		}
746
-	}
747
-
748
-	/**
749
-	 * @param array $entry
750
-	 */
751
-	public static function registerLogIn(array $entry) {
752
-		self::$altLogin[] = $entry;
753
-	}
754
-
755
-	/**
756
-	 * @return array
757
-	 */
758
-	public static function getAlternativeLogIns() {
759
-		return self::$altLogin;
760
-	}
761
-
762
-	/**
763
-	 * get a list of all apps in the apps folder
764
-	 *
765
-	 * @return array an array of app names (string IDs)
766
-	 * @todo: change the name of this method to getInstalledApps, which is more accurate
767
-	 */
768
-	public static function getAllApps() {
769
-
770
-		$apps = array();
771
-
772
-		foreach (OC::$APPSROOTS as $apps_dir) {
773
-			if (!is_readable($apps_dir['path'])) {
774
-				\OCP\Util::writeLog('core', 'unable to read app folder : ' . $apps_dir['path'], \OCP\Util::WARN);
775
-				continue;
776
-			}
777
-			$dh = opendir($apps_dir['path']);
778
-
779
-			if (is_resource($dh)) {
780
-				while (($file = readdir($dh)) !== false) {
781
-
782
-					if ($file[0] != '.' and is_dir($apps_dir['path'] . '/' . $file) and is_file($apps_dir['path'] . '/' . $file . '/appinfo/info.xml')) {
783
-
784
-						$apps[] = $file;
785
-					}
786
-				}
787
-			}
788
-		}
789
-
790
-		return $apps;
791
-	}
792
-
793
-	/**
794
-	 * List all apps, this is used in apps.php
795
-	 *
796
-	 * @return array
797
-	 */
798
-	public function listAllApps() {
799
-		$installedApps = OC_App::getAllApps();
800
-
801
-		//we don't want to show configuration for these
802
-		$blacklist = \OC::$server->getAppManager()->getAlwaysEnabledApps();
803
-		$appList = array();
804
-		$langCode = \OC::$server->getL10N('core')->getLanguageCode();
805
-		$urlGenerator = \OC::$server->getURLGenerator();
806
-
807
-		foreach ($installedApps as $app) {
808
-			if (array_search($app, $blacklist) === false) {
809
-
810
-				$info = OC_App::getAppInfo($app, false, $langCode);
811
-				if (!is_array($info)) {
812
-					\OCP\Util::writeLog('core', 'Could not read app info file for app "' . $app . '"', \OCP\Util::ERROR);
813
-					continue;
814
-				}
815
-
816
-				if (!isset($info['name'])) {
817
-					\OCP\Util::writeLog('core', 'App id "' . $app . '" has no name in appinfo', \OCP\Util::ERROR);
818
-					continue;
819
-				}
820
-
821
-				$enabled = \OC::$server->getAppConfig()->getValue($app, 'enabled', 'no');
822
-				$info['groups'] = null;
823
-				if ($enabled === 'yes') {
824
-					$active = true;
825
-				} else if ($enabled === 'no') {
826
-					$active = false;
827
-				} else {
828
-					$active = true;
829
-					$info['groups'] = $enabled;
830
-				}
831
-
832
-				$info['active'] = $active;
833
-
834
-				if (self::isShipped($app)) {
835
-					$info['internal'] = true;
836
-					$info['level'] = self::officialApp;
837
-					$info['removable'] = false;
838
-				} else {
839
-					$info['internal'] = false;
840
-					$info['removable'] = true;
841
-				}
842
-
843
-				$appPath = self::getAppPath($app);
844
-				if ($appPath !== false) {
845
-					$appIcon = $appPath . '/img/' . $app . '.svg';
846
-					if (file_exists($appIcon)) {
847
-						$info['preview'] = \OC::$server->getURLGenerator()->imagePath($app, $app . '.svg');
848
-						$info['previewAsIcon'] = true;
849
-					} else {
850
-						$appIcon = $appPath . '/img/app.svg';
851
-						if (file_exists($appIcon)) {
852
-							$info['preview'] = \OC::$server->getURLGenerator()->imagePath($app, 'app.svg');
853
-							$info['previewAsIcon'] = true;
854
-						}
855
-					}
856
-				}
857
-				// fix documentation
858
-				if (isset($info['documentation']) && is_array($info['documentation'])) {
859
-					foreach ($info['documentation'] as $key => $url) {
860
-						// If it is not an absolute URL we assume it is a key
861
-						// i.e. admin-ldap will get converted to go.php?to=admin-ldap
862
-						if (stripos($url, 'https://') !== 0 && stripos($url, 'http://') !== 0) {
863
-							$url = $urlGenerator->linkToDocs($url);
864
-						}
865
-
866
-						$info['documentation'][$key] = $url;
867
-					}
868
-				}
869
-
870
-				$info['version'] = OC_App::getAppVersion($app);
871
-				$appList[] = $info;
872
-			}
873
-		}
874
-
875
-		return $appList;
876
-	}
877
-
878
-	/**
879
-	 * Returns the internal app ID or false
880
-	 *
881
-	 * @param string $ocsID
882
-	 * @return string|false
883
-	 */
884
-	public static function getInternalAppIdByOcs($ocsID) {
885
-		if (is_numeric($ocsID)) {
886
-			$idArray = \OC::$server->getAppConfig()->getValues(false, 'ocsid');
887
-			if (array_search($ocsID, $idArray)) {
888
-				return array_search($ocsID, $idArray);
889
-			}
890
-		}
891
-		return false;
892
-	}
893
-
894
-	public static function shouldUpgrade($app) {
895
-		$versions = self::getAppVersions();
896
-		$currentVersion = OC_App::getAppVersion($app);
897
-		if ($currentVersion && isset($versions[$app])) {
898
-			$installedVersion = $versions[$app];
899
-			if (!version_compare($currentVersion, $installedVersion, '=')) {
900
-				return true;
901
-			}
902
-		}
903
-		return false;
904
-	}
905
-
906
-	/**
907
-	 * Adjust the number of version parts of $version1 to match
908
-	 * the number of version parts of $version2.
909
-	 *
910
-	 * @param string $version1 version to adjust
911
-	 * @param string $version2 version to take the number of parts from
912
-	 * @return string shortened $version1
913
-	 */
914
-	private static function adjustVersionParts($version1, $version2) {
915
-		$version1 = explode('.', $version1);
916
-		$version2 = explode('.', $version2);
917
-		// reduce $version1 to match the number of parts in $version2
918
-		while (count($version1) > count($version2)) {
919
-			array_pop($version1);
920
-		}
921
-		// if $version1 does not have enough parts, add some
922
-		while (count($version1) < count($version2)) {
923
-			$version1[] = '0';
924
-		}
925
-		return implode('.', $version1);
926
-	}
927
-
928
-	/**
929
-	 * Check whether the current ownCloud version matches the given
930
-	 * application's version requirements.
931
-	 *
932
-	 * The comparison is made based on the number of parts that the
933
-	 * app info version has. For example for ownCloud 6.0.3 if the
934
-	 * app info version is expecting version 6.0, the comparison is
935
-	 * made on the first two parts of the ownCloud version.
936
-	 * This means that it's possible to specify "requiremin" => 6
937
-	 * and "requiremax" => 6 and it will still match ownCloud 6.0.3.
938
-	 *
939
-	 * @param string $ocVersion ownCloud version to check against
940
-	 * @param array $appInfo app info (from xml)
941
-	 *
942
-	 * @return boolean true if compatible, otherwise false
943
-	 */
944
-	public static function isAppCompatible($ocVersion, $appInfo) {
945
-		$requireMin = '';
946
-		$requireMax = '';
947
-		if (isset($appInfo['dependencies']['nextcloud']['@attributes']['min-version'])) {
948
-			$requireMin = $appInfo['dependencies']['nextcloud']['@attributes']['min-version'];
949
-		} elseif (isset($appInfo['dependencies']['owncloud']['@attributes']['min-version'])) {
950
-			$requireMin = $appInfo['dependencies']['owncloud']['@attributes']['min-version'];
951
-		} else if (isset($appInfo['requiremin'])) {
952
-			$requireMin = $appInfo['requiremin'];
953
-		} else if (isset($appInfo['require'])) {
954
-			$requireMin = $appInfo['require'];
955
-		}
956
-
957
-		if (isset($appInfo['dependencies']['nextcloud']['@attributes']['max-version'])) {
958
-			$requireMax = $appInfo['dependencies']['nextcloud']['@attributes']['max-version'];
959
-		} elseif (isset($appInfo['dependencies']['owncloud']['@attributes']['max-version'])) {
960
-			$requireMax = $appInfo['dependencies']['owncloud']['@attributes']['max-version'];
961
-		} else if (isset($appInfo['requiremax'])) {
962
-			$requireMax = $appInfo['requiremax'];
963
-		}
964
-
965
-		if (is_array($ocVersion)) {
966
-			$ocVersion = implode('.', $ocVersion);
967
-		}
968
-
969
-		if (!empty($requireMin)
970
-			&& version_compare(self::adjustVersionParts($ocVersion, $requireMin), $requireMin, '<')
971
-		) {
972
-
973
-			return false;
974
-		}
975
-
976
-		if (!empty($requireMax)
977
-			&& version_compare(self::adjustVersionParts($ocVersion, $requireMax), $requireMax, '>')
978
-		) {
979
-			return false;
980
-		}
981
-
982
-		return true;
983
-	}
984
-
985
-	/**
986
-	 * get the installed version of all apps
987
-	 */
988
-	public static function getAppVersions() {
989
-		static $versions;
990
-
991
-		if (!$versions) {
992
-			$appConfig = \OC::$server->getAppConfig();
993
-			$versions = $appConfig->getValues(false, 'installed_version');
994
-		}
995
-		return $versions;
996
-	}
997
-
998
-	/**
999
-	 * @param string $app
1000
-	 * @param \OCP\IConfig $config
1001
-	 * @param \OCP\IL10N $l
1002
-	 * @return bool
1003
-	 *
1004
-	 * @throws Exception if app is not compatible with this version of ownCloud
1005
-	 * @throws Exception if no app-name was specified
1006
-	 */
1007
-	public function installApp($app,
1008
-	                           \OCP\IConfig $config,
1009
-	                           \OCP\IL10N $l) {
1010
-		if ($app !== false) {
1011
-			// check if the app is compatible with this version of ownCloud
1012
-			$info = self::getAppInfo($app);
1013
-			if (!is_array($info)) {
1014
-				throw new \Exception(
1015
-					$l->t('App "%s" cannot be installed because appinfo file cannot be read.',
1016
-						[$info['name']]
1017
-					)
1018
-				);
1019
-			}
1020
-
1021
-			$version = \OCP\Util::getVersion();
1022
-			if (!self::isAppCompatible($version, $info)) {
1023
-				throw new \Exception(
1024
-					$l->t('App "%s" cannot be installed because it is not compatible with this version of the server.',
1025
-						array($info['name'])
1026
-					)
1027
-				);
1028
-			}
1029
-
1030
-			// check for required dependencies
1031
-			self::checkAppDependencies($config, $l, $info);
1032
-
1033
-			$config->setAppValue($app, 'enabled', 'yes');
1034
-			if (isset($appData['id'])) {
1035
-				$config->setAppValue($app, 'ocsid', $appData['id']);
1036
-			}
1037
-
1038
-			if (isset($info['settings']) && is_array($info['settings'])) {
1039
-				$appPath = self::getAppPath($app);
1040
-				self::registerAutoloading($app, $appPath);
1041
-				\OC::$server->getSettingsManager()->setupSettings($info['settings']);
1042
-			}
1043
-
1044
-			\OC_Hook::emit('OC_App', 'post_enable', array('app' => $app));
1045
-		} else {
1046
-			if (empty($appName)) {
1047
-				throw new \Exception($l->t("No app name specified"));
1048
-			} else {
1049
-				throw new \Exception($l->t("App '%s' could not be installed!", $appName));
1050
-			}
1051
-		}
1052
-
1053
-		return $app;
1054
-	}
1055
-
1056
-	/**
1057
-	 * update the database for the app and call the update script
1058
-	 *
1059
-	 * @param string $appId
1060
-	 * @return bool
1061
-	 */
1062
-	public static function updateApp($appId) {
1063
-		$appPath = self::getAppPath($appId);
1064
-		if ($appPath === false) {
1065
-			return false;
1066
-		}
1067
-		$appData = self::getAppInfo($appId);
1068
-		self::executeRepairSteps($appId, $appData['repair-steps']['pre-migration']);
1069
-		if (file_exists($appPath . '/appinfo/database.xml')) {
1070
-			OC_DB::updateDbFromStructure($appPath . '/appinfo/database.xml');
1071
-		}
1072
-		self::executeRepairSteps($appId, $appData['repair-steps']['post-migration']);
1073
-		self::setupLiveMigrations($appId, $appData['repair-steps']['live-migration']);
1074
-		unset(self::$appVersion[$appId]);
1075
-		// run upgrade code
1076
-		if (file_exists($appPath . '/appinfo/update.php')) {
1077
-			self::loadApp($appId);
1078
-			include $appPath . '/appinfo/update.php';
1079
-		}
1080
-		self::registerAutoloading($appId, $appPath);
1081
-		self::setupBackgroundJobs($appData['background-jobs']);
1082
-		if (isset($appData['settings']) && is_array($appData['settings'])) {
1083
-			\OC::$server->getSettingsManager()->setupSettings($appData['settings']);
1084
-		}
1085
-
1086
-		//set remote/public handlers
1087
-		if (array_key_exists('ocsid', $appData)) {
1088
-			\OC::$server->getConfig()->setAppValue($appId, 'ocsid', $appData['ocsid']);
1089
-		} elseif (\OC::$server->getConfig()->getAppValue($appId, 'ocsid', null) !== null) {
1090
-			\OC::$server->getConfig()->deleteAppValue($appId, 'ocsid');
1091
-		}
1092
-		foreach ($appData['remote'] as $name => $path) {
1093
-			\OC::$server->getConfig()->setAppValue('core', 'remote_' . $name, $appId . '/' . $path);
1094
-		}
1095
-		foreach ($appData['public'] as $name => $path) {
1096
-			\OC::$server->getConfig()->setAppValue('core', 'public_' . $name, $appId . '/' . $path);
1097
-		}
1098
-
1099
-		self::setAppTypes($appId);
1100
-
1101
-		$version = \OC_App::getAppVersion($appId);
1102
-		\OC::$server->getAppConfig()->setValue($appId, 'installed_version', $version);
1103
-
1104
-		\OC::$server->getEventDispatcher()->dispatch(ManagerEvent::EVENT_APP_UPDATE, new ManagerEvent(
1105
-			ManagerEvent::EVENT_APP_UPDATE, $appId
1106
-		));
1107
-
1108
-		return true;
1109
-	}
1110
-
1111
-	/**
1112
-	 * @param string $appId
1113
-	 * @param string[] $steps
1114
-	 * @throws \OC\NeedsUpdateException
1115
-	 */
1116
-	public static function executeRepairSteps($appId, array $steps) {
1117
-		if (empty($steps)) {
1118
-			return;
1119
-		}
1120
-		// load the app
1121
-		self::loadApp($appId);
1122
-
1123
-		$dispatcher = OC::$server->getEventDispatcher();
1124
-
1125
-		// load the steps
1126
-		$r = new Repair([], $dispatcher);
1127
-		foreach ($steps as $step) {
1128
-			try {
1129
-				$r->addStep($step);
1130
-			} catch (Exception $ex) {
1131
-				$r->emit('\OC\Repair', 'error', [$ex->getMessage()]);
1132
-				\OC::$server->getLogger()->logException($ex);
1133
-			}
1134
-		}
1135
-		// run the steps
1136
-		$r->run();
1137
-	}
1138
-
1139
-	public static function setupBackgroundJobs(array $jobs) {
1140
-		$queue = \OC::$server->getJobList();
1141
-		foreach ($jobs as $job) {
1142
-			$queue->add($job);
1143
-		}
1144
-	}
1145
-
1146
-	/**
1147
-	 * @param string $appId
1148
-	 * @param string[] $steps
1149
-	 */
1150
-	private static function setupLiveMigrations($appId, array $steps) {
1151
-		$queue = \OC::$server->getJobList();
1152
-		foreach ($steps as $step) {
1153
-			$queue->add('OC\Migration\BackgroundRepair', [
1154
-				'app' => $appId,
1155
-				'step' => $step]);
1156
-		}
1157
-	}
1158
-
1159
-	/**
1160
-	 * @param string $appId
1161
-	 * @return \OC\Files\View|false
1162
-	 */
1163
-	public static function getStorage($appId) {
1164
-		if (OC_App::isEnabled($appId)) { //sanity check
1165
-			if (\OC::$server->getUserSession()->isLoggedIn()) {
1166
-				$view = new \OC\Files\View('/' . OC_User::getUser());
1167
-				if (!$view->file_exists($appId)) {
1168
-					$view->mkdir($appId);
1169
-				}
1170
-				return new \OC\Files\View('/' . OC_User::getUser() . '/' . $appId);
1171
-			} else {
1172
-				\OCP\Util::writeLog('core', 'Can\'t get app storage, app ' . $appId . ', user not logged in', \OCP\Util::ERROR);
1173
-				return false;
1174
-			}
1175
-		} else {
1176
-			\OCP\Util::writeLog('core', 'Can\'t get app storage, app ' . $appId . ' not enabled', \OCP\Util::ERROR);
1177
-			return false;
1178
-		}
1179
-	}
1180
-
1181
-	protected static function findBestL10NOption($options, $lang) {
1182
-		$fallback = $similarLangFallback = $englishFallback = false;
1183
-
1184
-		$lang = strtolower($lang);
1185
-		$similarLang = $lang;
1186
-		if (strpos($similarLang, '_')) {
1187
-			// For "de_DE" we want to find "de" and the other way around
1188
-			$similarLang = substr($lang, 0, strpos($lang, '_'));
1189
-		}
1190
-
1191
-		foreach ($options as $option) {
1192
-			if (is_array($option)) {
1193
-				if ($fallback === false) {
1194
-					$fallback = $option['@value'];
1195
-				}
1196
-
1197
-				if (!isset($option['@attributes']['lang'])) {
1198
-					continue;
1199
-				}
1200
-
1201
-				$attributeLang = strtolower($option['@attributes']['lang']);
1202
-				if ($attributeLang === $lang) {
1203
-					return $option['@value'];
1204
-				}
1205
-
1206
-				if ($attributeLang === $similarLang) {
1207
-					$similarLangFallback = $option['@value'];
1208
-				} else if (strpos($attributeLang, $similarLang . '_') === 0) {
1209
-					if ($similarLangFallback === false) {
1210
-						$similarLangFallback = $option['@value'];
1211
-					}
1212
-				}
1213
-			} else {
1214
-				$englishFallback = $option;
1215
-			}
1216
-		}
1217
-
1218
-		if ($similarLangFallback !== false) {
1219
-			return $similarLangFallback;
1220
-		} else if ($englishFallback !== false) {
1221
-			return $englishFallback;
1222
-		}
1223
-		return (string)$fallback;
1224
-	}
1225
-
1226
-	/**
1227
-	 * parses the app data array and enhanced the 'description' value
1228
-	 *
1229
-	 * @param array $data the app data
1230
-	 * @param string $lang
1231
-	 * @return array improved app data
1232
-	 */
1233
-	public static function parseAppInfo(array $data, $lang = null) {
1234
-
1235
-		if ($lang && isset($data['name']) && is_array($data['name'])) {
1236
-			$data['name'] = self::findBestL10NOption($data['name'], $lang);
1237
-		}
1238
-		if ($lang && isset($data['summary']) && is_array($data['summary'])) {
1239
-			$data['summary'] = self::findBestL10NOption($data['summary'], $lang);
1240
-		}
1241
-		if ($lang && isset($data['description']) && is_array($data['description'])) {
1242
-			$data['description'] = trim(self::findBestL10NOption($data['description'], $lang));
1243
-		} else if (isset($data['description']) && is_string($data['description'])) {
1244
-			$data['description'] = trim($data['description']);
1245
-		} else {
1246
-			$data['description'] = '';
1247
-		}
1248
-
1249
-		return $data;
1250
-	}
1251
-
1252
-	/**
1253
-	 * @param \OCP\IConfig $config
1254
-	 * @param \OCP\IL10N $l
1255
-	 * @param array $info
1256
-	 * @throws \Exception
1257
-	 */
1258
-	public static function checkAppDependencies($config, $l, $info) {
1259
-		$dependencyAnalyzer = new DependencyAnalyzer(new Platform($config), $l);
1260
-		$missing = $dependencyAnalyzer->analyze($info);
1261
-		if (!empty($missing)) {
1262
-			$missingMsg = join(PHP_EOL, $missing);
1263
-			throw new \Exception(
1264
-				$l->t('App "%s" cannot be installed because the following dependencies are not fulfilled: %s',
1265
-					[$info['name'], $missingMsg]
1266
-				)
1267
-			);
1268
-		}
1269
-	}
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
+            if (is_callable($form)) {
703
+                $result = $form();
704
+                if (is_string($result)){
705
+                    $forms[] = include $result;
706
+                }
707
+            } else {
708
+                $forms[] = include $form;
709
+            }
710
+        }
711
+        return $forms;
712
+    }
713
+
714
+    /**
715
+     * register an admin form to be shown
716
+     *
717
+     * @param string $app
718
+     * @param string|callable $page
719
+     */
720
+    public static function registerAdmin($app, $page) {
721
+        if (is_callable($page)) {
722
+            self::$adminForms[] = function() use ($app, $page) {
723
+                $result = $page();
724
+                return is_string($page) ? $app . '/' . $result . '.php' : false;
725
+            };
726
+        } else {
727
+            self::$adminForms[] = $app . '/' . $page . '.php';
728
+        }
729
+    }
730
+
731
+    /**
732
+     * register a personal form to be shown
733
+     *
734
+     * @param string $app
735
+     * @param string|callable $page
736
+     */
737
+    public static function registerPersonal($app, $page) {
738
+        if (is_callable($page)) {
739
+            self::$personalForms[] = function() use ($app, $page) {
740
+                $result = $page();
741
+                return is_string($result) ? $app . '/' . $result . '.php' : false;
742
+            };
743
+        } else {
744
+            self::$personalForms[] = $app . '/' . $page . '.php';
745
+        }
746
+    }
747
+
748
+    /**
749
+     * @param array $entry
750
+     */
751
+    public static function registerLogIn(array $entry) {
752
+        self::$altLogin[] = $entry;
753
+    }
754
+
755
+    /**
756
+     * @return array
757
+     */
758
+    public static function getAlternativeLogIns() {
759
+        return self::$altLogin;
760
+    }
761
+
762
+    /**
763
+     * get a list of all apps in the apps folder
764
+     *
765
+     * @return array an array of app names (string IDs)
766
+     * @todo: change the name of this method to getInstalledApps, which is more accurate
767
+     */
768
+    public static function getAllApps() {
769
+
770
+        $apps = array();
771
+
772
+        foreach (OC::$APPSROOTS as $apps_dir) {
773
+            if (!is_readable($apps_dir['path'])) {
774
+                \OCP\Util::writeLog('core', 'unable to read app folder : ' . $apps_dir['path'], \OCP\Util::WARN);
775
+                continue;
776
+            }
777
+            $dh = opendir($apps_dir['path']);
778
+
779
+            if (is_resource($dh)) {
780
+                while (($file = readdir($dh)) !== false) {
781
+
782
+                    if ($file[0] != '.' and is_dir($apps_dir['path'] . '/' . $file) and is_file($apps_dir['path'] . '/' . $file . '/appinfo/info.xml')) {
783
+
784
+                        $apps[] = $file;
785
+                    }
786
+                }
787
+            }
788
+        }
789
+
790
+        return $apps;
791
+    }
792
+
793
+    /**
794
+     * List all apps, this is used in apps.php
795
+     *
796
+     * @return array
797
+     */
798
+    public function listAllApps() {
799
+        $installedApps = OC_App::getAllApps();
800
+
801
+        //we don't want to show configuration for these
802
+        $blacklist = \OC::$server->getAppManager()->getAlwaysEnabledApps();
803
+        $appList = array();
804
+        $langCode = \OC::$server->getL10N('core')->getLanguageCode();
805
+        $urlGenerator = \OC::$server->getURLGenerator();
806
+
807
+        foreach ($installedApps as $app) {
808
+            if (array_search($app, $blacklist) === false) {
809
+
810
+                $info = OC_App::getAppInfo($app, false, $langCode);
811
+                if (!is_array($info)) {
812
+                    \OCP\Util::writeLog('core', 'Could not read app info file for app "' . $app . '"', \OCP\Util::ERROR);
813
+                    continue;
814
+                }
815
+
816
+                if (!isset($info['name'])) {
817
+                    \OCP\Util::writeLog('core', 'App id "' . $app . '" has no name in appinfo', \OCP\Util::ERROR);
818
+                    continue;
819
+                }
820
+
821
+                $enabled = \OC::$server->getAppConfig()->getValue($app, 'enabled', 'no');
822
+                $info['groups'] = null;
823
+                if ($enabled === 'yes') {
824
+                    $active = true;
825
+                } else if ($enabled === 'no') {
826
+                    $active = false;
827
+                } else {
828
+                    $active = true;
829
+                    $info['groups'] = $enabled;
830
+                }
831
+
832
+                $info['active'] = $active;
833
+
834
+                if (self::isShipped($app)) {
835
+                    $info['internal'] = true;
836
+                    $info['level'] = self::officialApp;
837
+                    $info['removable'] = false;
838
+                } else {
839
+                    $info['internal'] = false;
840
+                    $info['removable'] = true;
841
+                }
842
+
843
+                $appPath = self::getAppPath($app);
844
+                if ($appPath !== false) {
845
+                    $appIcon = $appPath . '/img/' . $app . '.svg';
846
+                    if (file_exists($appIcon)) {
847
+                        $info['preview'] = \OC::$server->getURLGenerator()->imagePath($app, $app . '.svg');
848
+                        $info['previewAsIcon'] = true;
849
+                    } else {
850
+                        $appIcon = $appPath . '/img/app.svg';
851
+                        if (file_exists($appIcon)) {
852
+                            $info['preview'] = \OC::$server->getURLGenerator()->imagePath($app, 'app.svg');
853
+                            $info['previewAsIcon'] = true;
854
+                        }
855
+                    }
856
+                }
857
+                // fix documentation
858
+                if (isset($info['documentation']) && is_array($info['documentation'])) {
859
+                    foreach ($info['documentation'] as $key => $url) {
860
+                        // If it is not an absolute URL we assume it is a key
861
+                        // i.e. admin-ldap will get converted to go.php?to=admin-ldap
862
+                        if (stripos($url, 'https://') !== 0 && stripos($url, 'http://') !== 0) {
863
+                            $url = $urlGenerator->linkToDocs($url);
864
+                        }
865
+
866
+                        $info['documentation'][$key] = $url;
867
+                    }
868
+                }
869
+
870
+                $info['version'] = OC_App::getAppVersion($app);
871
+                $appList[] = $info;
872
+            }
873
+        }
874
+
875
+        return $appList;
876
+    }
877
+
878
+    /**
879
+     * Returns the internal app ID or false
880
+     *
881
+     * @param string $ocsID
882
+     * @return string|false
883
+     */
884
+    public static function getInternalAppIdByOcs($ocsID) {
885
+        if (is_numeric($ocsID)) {
886
+            $idArray = \OC::$server->getAppConfig()->getValues(false, 'ocsid');
887
+            if (array_search($ocsID, $idArray)) {
888
+                return array_search($ocsID, $idArray);
889
+            }
890
+        }
891
+        return false;
892
+    }
893
+
894
+    public static function shouldUpgrade($app) {
895
+        $versions = self::getAppVersions();
896
+        $currentVersion = OC_App::getAppVersion($app);
897
+        if ($currentVersion && isset($versions[$app])) {
898
+            $installedVersion = $versions[$app];
899
+            if (!version_compare($currentVersion, $installedVersion, '=')) {
900
+                return true;
901
+            }
902
+        }
903
+        return false;
904
+    }
905
+
906
+    /**
907
+     * Adjust the number of version parts of $version1 to match
908
+     * the number of version parts of $version2.
909
+     *
910
+     * @param string $version1 version to adjust
911
+     * @param string $version2 version to take the number of parts from
912
+     * @return string shortened $version1
913
+     */
914
+    private static function adjustVersionParts($version1, $version2) {
915
+        $version1 = explode('.', $version1);
916
+        $version2 = explode('.', $version2);
917
+        // reduce $version1 to match the number of parts in $version2
918
+        while (count($version1) > count($version2)) {
919
+            array_pop($version1);
920
+        }
921
+        // if $version1 does not have enough parts, add some
922
+        while (count($version1) < count($version2)) {
923
+            $version1[] = '0';
924
+        }
925
+        return implode('.', $version1);
926
+    }
927
+
928
+    /**
929
+     * Check whether the current ownCloud version matches the given
930
+     * application's version requirements.
931
+     *
932
+     * The comparison is made based on the number of parts that the
933
+     * app info version has. For example for ownCloud 6.0.3 if the
934
+     * app info version is expecting version 6.0, the comparison is
935
+     * made on the first two parts of the ownCloud version.
936
+     * This means that it's possible to specify "requiremin" => 6
937
+     * and "requiremax" => 6 and it will still match ownCloud 6.0.3.
938
+     *
939
+     * @param string $ocVersion ownCloud version to check against
940
+     * @param array $appInfo app info (from xml)
941
+     *
942
+     * @return boolean true if compatible, otherwise false
943
+     */
944
+    public static function isAppCompatible($ocVersion, $appInfo) {
945
+        $requireMin = '';
946
+        $requireMax = '';
947
+        if (isset($appInfo['dependencies']['nextcloud']['@attributes']['min-version'])) {
948
+            $requireMin = $appInfo['dependencies']['nextcloud']['@attributes']['min-version'];
949
+        } elseif (isset($appInfo['dependencies']['owncloud']['@attributes']['min-version'])) {
950
+            $requireMin = $appInfo['dependencies']['owncloud']['@attributes']['min-version'];
951
+        } else if (isset($appInfo['requiremin'])) {
952
+            $requireMin = $appInfo['requiremin'];
953
+        } else if (isset($appInfo['require'])) {
954
+            $requireMin = $appInfo['require'];
955
+        }
956
+
957
+        if (isset($appInfo['dependencies']['nextcloud']['@attributes']['max-version'])) {
958
+            $requireMax = $appInfo['dependencies']['nextcloud']['@attributes']['max-version'];
959
+        } elseif (isset($appInfo['dependencies']['owncloud']['@attributes']['max-version'])) {
960
+            $requireMax = $appInfo['dependencies']['owncloud']['@attributes']['max-version'];
961
+        } else if (isset($appInfo['requiremax'])) {
962
+            $requireMax = $appInfo['requiremax'];
963
+        }
964
+
965
+        if (is_array($ocVersion)) {
966
+            $ocVersion = implode('.', $ocVersion);
967
+        }
968
+
969
+        if (!empty($requireMin)
970
+            && version_compare(self::adjustVersionParts($ocVersion, $requireMin), $requireMin, '<')
971
+        ) {
972
+
973
+            return false;
974
+        }
975
+
976
+        if (!empty($requireMax)
977
+            && version_compare(self::adjustVersionParts($ocVersion, $requireMax), $requireMax, '>')
978
+        ) {
979
+            return false;
980
+        }
981
+
982
+        return true;
983
+    }
984
+
985
+    /**
986
+     * get the installed version of all apps
987
+     */
988
+    public static function getAppVersions() {
989
+        static $versions;
990
+
991
+        if (!$versions) {
992
+            $appConfig = \OC::$server->getAppConfig();
993
+            $versions = $appConfig->getValues(false, 'installed_version');
994
+        }
995
+        return $versions;
996
+    }
997
+
998
+    /**
999
+     * @param string $app
1000
+     * @param \OCP\IConfig $config
1001
+     * @param \OCP\IL10N $l
1002
+     * @return bool
1003
+     *
1004
+     * @throws Exception if app is not compatible with this version of ownCloud
1005
+     * @throws Exception if no app-name was specified
1006
+     */
1007
+    public function installApp($app,
1008
+                                \OCP\IConfig $config,
1009
+                                \OCP\IL10N $l) {
1010
+        if ($app !== false) {
1011
+            // check if the app is compatible with this version of ownCloud
1012
+            $info = self::getAppInfo($app);
1013
+            if (!is_array($info)) {
1014
+                throw new \Exception(
1015
+                    $l->t('App "%s" cannot be installed because appinfo file cannot be read.',
1016
+                        [$info['name']]
1017
+                    )
1018
+                );
1019
+            }
1020
+
1021
+            $version = \OCP\Util::getVersion();
1022
+            if (!self::isAppCompatible($version, $info)) {
1023
+                throw new \Exception(
1024
+                    $l->t('App "%s" cannot be installed because it is not compatible with this version of the server.',
1025
+                        array($info['name'])
1026
+                    )
1027
+                );
1028
+            }
1029
+
1030
+            // check for required dependencies
1031
+            self::checkAppDependencies($config, $l, $info);
1032
+
1033
+            $config->setAppValue($app, 'enabled', 'yes');
1034
+            if (isset($appData['id'])) {
1035
+                $config->setAppValue($app, 'ocsid', $appData['id']);
1036
+            }
1037
+
1038
+            if (isset($info['settings']) && is_array($info['settings'])) {
1039
+                $appPath = self::getAppPath($app);
1040
+                self::registerAutoloading($app, $appPath);
1041
+                \OC::$server->getSettingsManager()->setupSettings($info['settings']);
1042
+            }
1043
+
1044
+            \OC_Hook::emit('OC_App', 'post_enable', array('app' => $app));
1045
+        } else {
1046
+            if (empty($appName)) {
1047
+                throw new \Exception($l->t("No app name specified"));
1048
+            } else {
1049
+                throw new \Exception($l->t("App '%s' could not be installed!", $appName));
1050
+            }
1051
+        }
1052
+
1053
+        return $app;
1054
+    }
1055
+
1056
+    /**
1057
+     * update the database for the app and call the update script
1058
+     *
1059
+     * @param string $appId
1060
+     * @return bool
1061
+     */
1062
+    public static function updateApp($appId) {
1063
+        $appPath = self::getAppPath($appId);
1064
+        if ($appPath === false) {
1065
+            return false;
1066
+        }
1067
+        $appData = self::getAppInfo($appId);
1068
+        self::executeRepairSteps($appId, $appData['repair-steps']['pre-migration']);
1069
+        if (file_exists($appPath . '/appinfo/database.xml')) {
1070
+            OC_DB::updateDbFromStructure($appPath . '/appinfo/database.xml');
1071
+        }
1072
+        self::executeRepairSteps($appId, $appData['repair-steps']['post-migration']);
1073
+        self::setupLiveMigrations($appId, $appData['repair-steps']['live-migration']);
1074
+        unset(self::$appVersion[$appId]);
1075
+        // run upgrade code
1076
+        if (file_exists($appPath . '/appinfo/update.php')) {
1077
+            self::loadApp($appId);
1078
+            include $appPath . '/appinfo/update.php';
1079
+        }
1080
+        self::registerAutoloading($appId, $appPath);
1081
+        self::setupBackgroundJobs($appData['background-jobs']);
1082
+        if (isset($appData['settings']) && is_array($appData['settings'])) {
1083
+            \OC::$server->getSettingsManager()->setupSettings($appData['settings']);
1084
+        }
1085
+
1086
+        //set remote/public handlers
1087
+        if (array_key_exists('ocsid', $appData)) {
1088
+            \OC::$server->getConfig()->setAppValue($appId, 'ocsid', $appData['ocsid']);
1089
+        } elseif (\OC::$server->getConfig()->getAppValue($appId, 'ocsid', null) !== null) {
1090
+            \OC::$server->getConfig()->deleteAppValue($appId, 'ocsid');
1091
+        }
1092
+        foreach ($appData['remote'] as $name => $path) {
1093
+            \OC::$server->getConfig()->setAppValue('core', 'remote_' . $name, $appId . '/' . $path);
1094
+        }
1095
+        foreach ($appData['public'] as $name => $path) {
1096
+            \OC::$server->getConfig()->setAppValue('core', 'public_' . $name, $appId . '/' . $path);
1097
+        }
1098
+
1099
+        self::setAppTypes($appId);
1100
+
1101
+        $version = \OC_App::getAppVersion($appId);
1102
+        \OC::$server->getAppConfig()->setValue($appId, 'installed_version', $version);
1103
+
1104
+        \OC::$server->getEventDispatcher()->dispatch(ManagerEvent::EVENT_APP_UPDATE, new ManagerEvent(
1105
+            ManagerEvent::EVENT_APP_UPDATE, $appId
1106
+        ));
1107
+
1108
+        return true;
1109
+    }
1110
+
1111
+    /**
1112
+     * @param string $appId
1113
+     * @param string[] $steps
1114
+     * @throws \OC\NeedsUpdateException
1115
+     */
1116
+    public static function executeRepairSteps($appId, array $steps) {
1117
+        if (empty($steps)) {
1118
+            return;
1119
+        }
1120
+        // load the app
1121
+        self::loadApp($appId);
1122
+
1123
+        $dispatcher = OC::$server->getEventDispatcher();
1124
+
1125
+        // load the steps
1126
+        $r = new Repair([], $dispatcher);
1127
+        foreach ($steps as $step) {
1128
+            try {
1129
+                $r->addStep($step);
1130
+            } catch (Exception $ex) {
1131
+                $r->emit('\OC\Repair', 'error', [$ex->getMessage()]);
1132
+                \OC::$server->getLogger()->logException($ex);
1133
+            }
1134
+        }
1135
+        // run the steps
1136
+        $r->run();
1137
+    }
1138
+
1139
+    public static function setupBackgroundJobs(array $jobs) {
1140
+        $queue = \OC::$server->getJobList();
1141
+        foreach ($jobs as $job) {
1142
+            $queue->add($job);
1143
+        }
1144
+    }
1145
+
1146
+    /**
1147
+     * @param string $appId
1148
+     * @param string[] $steps
1149
+     */
1150
+    private static function setupLiveMigrations($appId, array $steps) {
1151
+        $queue = \OC::$server->getJobList();
1152
+        foreach ($steps as $step) {
1153
+            $queue->add('OC\Migration\BackgroundRepair', [
1154
+                'app' => $appId,
1155
+                'step' => $step]);
1156
+        }
1157
+    }
1158
+
1159
+    /**
1160
+     * @param string $appId
1161
+     * @return \OC\Files\View|false
1162
+     */
1163
+    public static function getStorage($appId) {
1164
+        if (OC_App::isEnabled($appId)) { //sanity check
1165
+            if (\OC::$server->getUserSession()->isLoggedIn()) {
1166
+                $view = new \OC\Files\View('/' . OC_User::getUser());
1167
+                if (!$view->file_exists($appId)) {
1168
+                    $view->mkdir($appId);
1169
+                }
1170
+                return new \OC\Files\View('/' . OC_User::getUser() . '/' . $appId);
1171
+            } else {
1172
+                \OCP\Util::writeLog('core', 'Can\'t get app storage, app ' . $appId . ', user not logged in', \OCP\Util::ERROR);
1173
+                return false;
1174
+            }
1175
+        } else {
1176
+            \OCP\Util::writeLog('core', 'Can\'t get app storage, app ' . $appId . ' not enabled', \OCP\Util::ERROR);
1177
+            return false;
1178
+        }
1179
+    }
1180
+
1181
+    protected static function findBestL10NOption($options, $lang) {
1182
+        $fallback = $similarLangFallback = $englishFallback = false;
1183
+
1184
+        $lang = strtolower($lang);
1185
+        $similarLang = $lang;
1186
+        if (strpos($similarLang, '_')) {
1187
+            // For "de_DE" we want to find "de" and the other way around
1188
+            $similarLang = substr($lang, 0, strpos($lang, '_'));
1189
+        }
1190
+
1191
+        foreach ($options as $option) {
1192
+            if (is_array($option)) {
1193
+                if ($fallback === false) {
1194
+                    $fallback = $option['@value'];
1195
+                }
1196
+
1197
+                if (!isset($option['@attributes']['lang'])) {
1198
+                    continue;
1199
+                }
1200
+
1201
+                $attributeLang = strtolower($option['@attributes']['lang']);
1202
+                if ($attributeLang === $lang) {
1203
+                    return $option['@value'];
1204
+                }
1205
+
1206
+                if ($attributeLang === $similarLang) {
1207
+                    $similarLangFallback = $option['@value'];
1208
+                } else if (strpos($attributeLang, $similarLang . '_') === 0) {
1209
+                    if ($similarLangFallback === false) {
1210
+                        $similarLangFallback = $option['@value'];
1211
+                    }
1212
+                }
1213
+            } else {
1214
+                $englishFallback = $option;
1215
+            }
1216
+        }
1217
+
1218
+        if ($similarLangFallback !== false) {
1219
+            return $similarLangFallback;
1220
+        } else if ($englishFallback !== false) {
1221
+            return $englishFallback;
1222
+        }
1223
+        return (string)$fallback;
1224
+    }
1225
+
1226
+    /**
1227
+     * parses the app data array and enhanced the 'description' value
1228
+     *
1229
+     * @param array $data the app data
1230
+     * @param string $lang
1231
+     * @return array improved app data
1232
+     */
1233
+    public static function parseAppInfo(array $data, $lang = null) {
1234
+
1235
+        if ($lang && isset($data['name']) && is_array($data['name'])) {
1236
+            $data['name'] = self::findBestL10NOption($data['name'], $lang);
1237
+        }
1238
+        if ($lang && isset($data['summary']) && is_array($data['summary'])) {
1239
+            $data['summary'] = self::findBestL10NOption($data['summary'], $lang);
1240
+        }
1241
+        if ($lang && isset($data['description']) && is_array($data['description'])) {
1242
+            $data['description'] = trim(self::findBestL10NOption($data['description'], $lang));
1243
+        } else if (isset($data['description']) && is_string($data['description'])) {
1244
+            $data['description'] = trim($data['description']);
1245
+        } else {
1246
+            $data['description'] = '';
1247
+        }
1248
+
1249
+        return $data;
1250
+    }
1251
+
1252
+    /**
1253
+     * @param \OCP\IConfig $config
1254
+     * @param \OCP\IL10N $l
1255
+     * @param array $info
1256
+     * @throws \Exception
1257
+     */
1258
+    public static function checkAppDependencies($config, $l, $info) {
1259
+        $dependencyAnalyzer = new DependencyAnalyzer(new Platform($config), $l);
1260
+        $missing = $dependencyAnalyzer->analyze($info);
1261
+        if (!empty($missing)) {
1262
+            $missingMsg = join(PHP_EOL, $missing);
1263
+            throw new \Exception(
1264
+                $l->t('App "%s" cannot be installed because the following dependencies are not fulfilled: %s',
1265
+                    [$info['name'], $missingMsg]
1266
+                )
1267
+            );
1268
+        }
1269
+    }
1270 1270
 }
Please login to merge, or discard this patch.
Spacing   +39 added lines, -39 removed lines patch added patch discarded remove patch
@@ -144,8 +144,8 @@  discard block
 block discarded – undo
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,7 +181,7 @@  discard block
 block discarded – undo
181 181
 	 * @param string $path
182 182
 	 */
183 183
 	public static function registerAutoloading($app, $path) {
184
-		$key = $app . '-' . $path;
184
+		$key = $app.'-'.$path;
185 185
 		if (isset(self::$alreadyRegistered[$key])) {
186 186
 			return;
187 187
 		}
@@ -189,9 +189,9 @@  discard block
 block discarded – undo
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();
@@ -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');
@@ -435,7 +435,7 @@  discard block
 block discarded – undo
435 435
 
436 436
 	// This is private as well. It simply works, so don't ask for more details
437 437
 	private static function proceedNavigation($list) {
438
-		usort($list, function ($a, $b) {
438
+		usort($list, function($a, $b) {
439 439
 			if (isset($a['order']) && isset($b['order'])) {
440 440
 				return ($a['order'] < $b['order']) ? -1 : 1;
441 441
 			} else if (isset($a['order']) || isset($b['order'])) {
@@ -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
 	}
@@ -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
 	}
@@ -608,7 +608,7 @@  discard block
 block discarded – undo
608 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'));
@@ -701,7 +701,7 @@  discard block
 block discarded – undo
701 701
 		foreach ($source as $form) {
702 702
 			if (is_callable($form)) {
703 703
 				$result = $form();
704
-				if (is_string($result)){
704
+				if (is_string($result)) {
705 705
 					$forms[] = include $result;
706 706
 				}
707 707
 			} else {
@@ -721,10 +721,10 @@  discard block
 block discarded – undo
721 721
 		if (is_callable($page)) {
722 722
 			self::$adminForms[] = function() use ($app, $page) {
723 723
 				$result = $page();
724
-				return is_string($page) ? $app . '/' . $result . '.php' : false;
724
+				return is_string($page) ? $app.'/'.$result.'.php' : false;
725 725
 			};
726 726
 		} else {
727
-			self::$adminForms[] = $app . '/' . $page . '.php';
727
+			self::$adminForms[] = $app.'/'.$page.'.php';
728 728
 		}
729 729
 	}
730 730
 
@@ -738,10 +738,10 @@  discard block
 block discarded – undo
738 738
 		if (is_callable($page)) {
739 739
 			self::$personalForms[] = function() use ($app, $page) {
740 740
 				$result = $page();
741
-				return is_string($result) ? $app . '/' . $result . '.php' : false;
741
+				return is_string($result) ? $app.'/'.$result.'.php' : false;
742 742
 			};
743 743
 		} else {
744
-			self::$personalForms[] = $app . '/' . $page . '.php';
744
+			self::$personalForms[] = $app.'/'.$page.'.php';
745 745
 		}
746 746
 	}
747 747
 
@@ -771,7 +771,7 @@  discard block
 block discarded – undo
771 771
 
772 772
 		foreach (OC::$APPSROOTS as $apps_dir) {
773 773
 			if (!is_readable($apps_dir['path'])) {
774
-				\OCP\Util::writeLog('core', 'unable to read app folder : ' . $apps_dir['path'], \OCP\Util::WARN);
774
+				\OCP\Util::writeLog('core', 'unable to read app folder : '.$apps_dir['path'], \OCP\Util::WARN);
775 775
 				continue;
776 776
 			}
777 777
 			$dh = opendir($apps_dir['path']);
@@ -779,7 +779,7 @@  discard block
 block discarded – undo
779 779
 			if (is_resource($dh)) {
780 780
 				while (($file = readdir($dh)) !== false) {
781 781
 
782
-					if ($file[0] != '.' and is_dir($apps_dir['path'] . '/' . $file) and is_file($apps_dir['path'] . '/' . $file . '/appinfo/info.xml')) {
782
+					if ($file[0] != '.' and is_dir($apps_dir['path'].'/'.$file) and is_file($apps_dir['path'].'/'.$file.'/appinfo/info.xml')) {
783 783
 
784 784
 						$apps[] = $file;
785 785
 					}
@@ -809,12 +809,12 @@  discard block
 block discarded – undo
809 809
 
810 810
 				$info = OC_App::getAppInfo($app, false, $langCode);
811 811
 				if (!is_array($info)) {
812
-					\OCP\Util::writeLog('core', 'Could not read app info file for app "' . $app . '"', \OCP\Util::ERROR);
812
+					\OCP\Util::writeLog('core', 'Could not read app info file for app "'.$app.'"', \OCP\Util::ERROR);
813 813
 					continue;
814 814
 				}
815 815
 
816 816
 				if (!isset($info['name'])) {
817
-					\OCP\Util::writeLog('core', 'App id "' . $app . '" has no name in appinfo', \OCP\Util::ERROR);
817
+					\OCP\Util::writeLog('core', 'App id "'.$app.'" has no name in appinfo', \OCP\Util::ERROR);
818 818
 					continue;
819 819
 				}
820 820
 
@@ -842,12 +842,12 @@  discard block
 block discarded – undo
842 842
 
843 843
 				$appPath = self::getAppPath($app);
844 844
 				if ($appPath !== false) {
845
-					$appIcon = $appPath . '/img/' . $app . '.svg';
845
+					$appIcon = $appPath.'/img/'.$app.'.svg';
846 846
 					if (file_exists($appIcon)) {
847
-						$info['preview'] = \OC::$server->getURLGenerator()->imagePath($app, $app . '.svg');
847
+						$info['preview'] = \OC::$server->getURLGenerator()->imagePath($app, $app.'.svg');
848 848
 						$info['previewAsIcon'] = true;
849 849
 					} else {
850
-						$appIcon = $appPath . '/img/app.svg';
850
+						$appIcon = $appPath.'/img/app.svg';
851 851
 						if (file_exists($appIcon)) {
852 852
 							$info['preview'] = \OC::$server->getURLGenerator()->imagePath($app, 'app.svg');
853 853
 							$info['previewAsIcon'] = true;
@@ -1066,16 +1066,16 @@  discard block
 block discarded – undo
1066 1066
 		}
1067 1067
 		$appData = self::getAppInfo($appId);
1068 1068
 		self::executeRepairSteps($appId, $appData['repair-steps']['pre-migration']);
1069
-		if (file_exists($appPath . '/appinfo/database.xml')) {
1070
-			OC_DB::updateDbFromStructure($appPath . '/appinfo/database.xml');
1069
+		if (file_exists($appPath.'/appinfo/database.xml')) {
1070
+			OC_DB::updateDbFromStructure($appPath.'/appinfo/database.xml');
1071 1071
 		}
1072 1072
 		self::executeRepairSteps($appId, $appData['repair-steps']['post-migration']);
1073 1073
 		self::setupLiveMigrations($appId, $appData['repair-steps']['live-migration']);
1074 1074
 		unset(self::$appVersion[$appId]);
1075 1075
 		// run upgrade code
1076
-		if (file_exists($appPath . '/appinfo/update.php')) {
1076
+		if (file_exists($appPath.'/appinfo/update.php')) {
1077 1077
 			self::loadApp($appId);
1078
-			include $appPath . '/appinfo/update.php';
1078
+			include $appPath.'/appinfo/update.php';
1079 1079
 		}
1080 1080
 		self::registerAutoloading($appId, $appPath);
1081 1081
 		self::setupBackgroundJobs($appData['background-jobs']);
@@ -1090,10 +1090,10 @@  discard block
 block discarded – undo
1090 1090
 			\OC::$server->getConfig()->deleteAppValue($appId, 'ocsid');
1091 1091
 		}
1092 1092
 		foreach ($appData['remote'] as $name => $path) {
1093
-			\OC::$server->getConfig()->setAppValue('core', 'remote_' . $name, $appId . '/' . $path);
1093
+			\OC::$server->getConfig()->setAppValue('core', 'remote_'.$name, $appId.'/'.$path);
1094 1094
 		}
1095 1095
 		foreach ($appData['public'] as $name => $path) {
1096
-			\OC::$server->getConfig()->setAppValue('core', 'public_' . $name, $appId . '/' . $path);
1096
+			\OC::$server->getConfig()->setAppValue('core', 'public_'.$name, $appId.'/'.$path);
1097 1097
 		}
1098 1098
 
1099 1099
 		self::setAppTypes($appId);
@@ -1163,17 +1163,17 @@  discard block
 block discarded – undo
1163 1163
 	public static function getStorage($appId) {
1164 1164
 		if (OC_App::isEnabled($appId)) { //sanity check
1165 1165
 			if (\OC::$server->getUserSession()->isLoggedIn()) {
1166
-				$view = new \OC\Files\View('/' . OC_User::getUser());
1166
+				$view = new \OC\Files\View('/'.OC_User::getUser());
1167 1167
 				if (!$view->file_exists($appId)) {
1168 1168
 					$view->mkdir($appId);
1169 1169
 				}
1170
-				return new \OC\Files\View('/' . OC_User::getUser() . '/' . $appId);
1170
+				return new \OC\Files\View('/'.OC_User::getUser().'/'.$appId);
1171 1171
 			} else {
1172
-				\OCP\Util::writeLog('core', 'Can\'t get app storage, app ' . $appId . ', user not logged in', \OCP\Util::ERROR);
1172
+				\OCP\Util::writeLog('core', 'Can\'t get app storage, app '.$appId.', user not logged in', \OCP\Util::ERROR);
1173 1173
 				return false;
1174 1174
 			}
1175 1175
 		} else {
1176
-			\OCP\Util::writeLog('core', 'Can\'t get app storage, app ' . $appId . ' not enabled', \OCP\Util::ERROR);
1176
+			\OCP\Util::writeLog('core', 'Can\'t get app storage, app '.$appId.' not enabled', \OCP\Util::ERROR);
1177 1177
 			return false;
1178 1178
 		}
1179 1179
 	}
@@ -1205,7 +1205,7 @@  discard block
 block discarded – undo
1205 1205
 
1206 1206
 				if ($attributeLang === $similarLang) {
1207 1207
 					$similarLangFallback = $option['@value'];
1208
-				} else if (strpos($attributeLang, $similarLang . '_') === 0) {
1208
+				} else if (strpos($attributeLang, $similarLang.'_') === 0) {
1209 1209
 					if ($similarLangFallback === false) {
1210 1210
 						$similarLangFallback = $option['@value'];
1211 1211
 					}
@@ -1220,7 +1220,7 @@  discard block
 block discarded – undo
1220 1220
 		} else if ($englishFallback !== false) {
1221 1221
 			return $englishFallback;
1222 1222
 		}
1223
-		return (string)$fallback;
1223
+		return (string) $fallback;
1224 1224
 	}
1225 1225
 
1226 1226
 	/**
Please login to merge, or discard this patch.
lib/public/App.php 1 patch
Indentation   +101 added lines, -101 removed lines patch added patch discarded remove patch
@@ -43,112 +43,112 @@
 block discarded – undo
43 43
  */
44 44
 class App {
45 45
 
46
-	/**
47
-	 * Adds an entry to the navigation
48
-	 *
49
-	 * This function adds a new entry to the navigation visible to users. $data
50
-	 * is an associative array.
51
-	 * The following keys are required:
52
-	 *   - id: unique id for this entry ('addressbook_index')
53
-	 *   - href: link to the page
54
-	 *   - name: Human readable name ('Addressbook')
55
-	 *
56
-	 * The following keys are optional:
57
-	 *   - icon: path to the icon of the app
58
-	 *   - order: integer, that influences the position of your application in
59
-	 *     the navigation. Lower values come first.
60
-	 *
61
-	 * @param array $data containing the data
62
-	 * @return boolean
63
-	 *
64
-	 * @deprecated 8.1.0 Use \OC::$server->getNavigationManager()->add() instead to
65
-	 * register a closure, this helps to speed up all requests against ownCloud
66
-	 * @since 4.0.0
67
-	 */
68
-	public static function addNavigationEntry($data) {
69
-		\OC::$server->getNavigationManager()->add($data);
70
-		return true;
71
-	}
46
+    /**
47
+     * Adds an entry to the navigation
48
+     *
49
+     * This function adds a new entry to the navigation visible to users. $data
50
+     * is an associative array.
51
+     * The following keys are required:
52
+     *   - id: unique id for this entry ('addressbook_index')
53
+     *   - href: link to the page
54
+     *   - name: Human readable name ('Addressbook')
55
+     *
56
+     * The following keys are optional:
57
+     *   - icon: path to the icon of the app
58
+     *   - order: integer, that influences the position of your application in
59
+     *     the navigation. Lower values come first.
60
+     *
61
+     * @param array $data containing the data
62
+     * @return boolean
63
+     *
64
+     * @deprecated 8.1.0 Use \OC::$server->getNavigationManager()->add() instead to
65
+     * register a closure, this helps to speed up all requests against ownCloud
66
+     * @since 4.0.0
67
+     */
68
+    public static function addNavigationEntry($data) {
69
+        \OC::$server->getNavigationManager()->add($data);
70
+        return true;
71
+    }
72 72
 
73
-	/**
74
-	 * Marks a navigation entry as active
75
-	 * @param string $id id of the entry
76
-	 * @return boolean
77
-	 *
78
-	 * This function sets a navigation entry as active and removes the 'active'
79
-	 * property from all other entries. The templates can use this for
80
-	 * highlighting the current position of the user.
81
-	 *
82
-	 * @deprecated 8.1.0 Use \OC::$server->getNavigationManager()->setActiveEntry() instead
83
-	 * @since 4.0.0
84
-	 */
85
-	public static function setActiveNavigationEntry( $id ) {
86
-		\OC::$server->getNavigationManager()->setActiveEntry($id);
87
-		return true;
88
-	}
73
+    /**
74
+     * Marks a navigation entry as active
75
+     * @param string $id id of the entry
76
+     * @return boolean
77
+     *
78
+     * This function sets a navigation entry as active and removes the 'active'
79
+     * property from all other entries. The templates can use this for
80
+     * highlighting the current position of the user.
81
+     *
82
+     * @deprecated 8.1.0 Use \OC::$server->getNavigationManager()->setActiveEntry() instead
83
+     * @since 4.0.0
84
+     */
85
+    public static function setActiveNavigationEntry( $id ) {
86
+        \OC::$server->getNavigationManager()->setActiveEntry($id);
87
+        return true;
88
+    }
89 89
 
90
-	/**
91
-	 * Register a Configuration Screen that should appear in the personal settings section.
92
-	 * @param string $app appid
93
-	 * @param string|callable $page page to be included
94
-	 * @return void
95
-	 * @since 4.0.0
96
-	*/
97
-	public static function registerPersonal( $app, $page ) {
98
-		\OC_App::registerPersonal( $app, $page );
99
-	}
90
+    /**
91
+     * Register a Configuration Screen that should appear in the personal settings section.
92
+     * @param string $app appid
93
+     * @param string|callable $page page to be included
94
+     * @return void
95
+     * @since 4.0.0
96
+     */
97
+    public static function registerPersonal( $app, $page ) {
98
+        \OC_App::registerPersonal( $app, $page );
99
+    }
100 100
 
101
-	/**
102
-	 * Register a Configuration Screen that should appear in the Admin section.
103
-	 * @param string $app string appid
104
-	 * @param string|callable $page string page to be included
105
-	 * @return void
106
-	 * @since 4.0.0
107
-	 */
108
-	public static function registerAdmin( $app, $page ) {
109
-		\OC_App::registerAdmin( $app, $page );
110
-	}
101
+    /**
102
+     * Register a Configuration Screen that should appear in the Admin section.
103
+     * @param string $app string appid
104
+     * @param string|callable $page string page to be included
105
+     * @return void
106
+     * @since 4.0.0
107
+     */
108
+    public static function registerAdmin( $app, $page ) {
109
+        \OC_App::registerAdmin( $app, $page );
110
+    }
111 111
 
112
-	/**
113
-	 * Read app metadata from the info.xml file
114
-	 * @param string $app id of the app or the path of the info.xml file
115
-	 * @param boolean $path (optional)
116
-	 * @return array|null
117
-	 * @since 4.0.0
118
-	*/
119
-	public static function getAppInfo( $app, $path=false ) {
120
-		return \OC_App::getAppInfo( $app, $path);
121
-	}
112
+    /**
113
+     * Read app metadata from the info.xml file
114
+     * @param string $app id of the app or the path of the info.xml file
115
+     * @param boolean $path (optional)
116
+     * @return array|null
117
+     * @since 4.0.0
118
+     */
119
+    public static function getAppInfo( $app, $path=false ) {
120
+        return \OC_App::getAppInfo( $app, $path);
121
+    }
122 122
 
123
-	/**
124
-	 * checks whether or not an app is enabled
125
-	 * @param string $app
126
-	 * @return boolean
127
-	 *
128
-	 * This function checks whether or not an app is enabled.
129
-	 * @since 4.0.0
130
-	 */
131
-	public static function isEnabled( $app ) {
132
-		return \OC_App::isEnabled( $app );
133
-	}
123
+    /**
124
+     * checks whether or not an app is enabled
125
+     * @param string $app
126
+     * @return boolean
127
+     *
128
+     * This function checks whether or not an app is enabled.
129
+     * @since 4.0.0
130
+     */
131
+    public static function isEnabled( $app ) {
132
+        return \OC_App::isEnabled( $app );
133
+    }
134 134
 
135
-	/**
136
-	 * Check if the app is enabled, redirects to home if not
137
-	 * @param string $app
138
-	 * @return void
139
-	 * @since 4.0.0
140
-	 * @deprecated 9.0.0 ownCloud core will handle disabled apps and redirects to valid URLs
141
-	*/
142
-	public static function checkAppEnabled( $app ) {
143
-	}
135
+    /**
136
+     * Check if the app is enabled, redirects to home if not
137
+     * @param string $app
138
+     * @return void
139
+     * @since 4.0.0
140
+     * @deprecated 9.0.0 ownCloud core will handle disabled apps and redirects to valid URLs
141
+     */
142
+    public static function checkAppEnabled( $app ) {
143
+    }
144 144
 
145
-	/**
146
-	 * Get the last version of the app from appinfo/info.xml
147
-	 * @param string $app
148
-	 * @return string
149
-	 * @since 4.0.0
150
-	 */
151
-	public static function getAppVersion( $app ) {
152
-		return \OC_App::getAppVersion( $app );
153
-	}
145
+    /**
146
+     * Get the last version of the app from appinfo/info.xml
147
+     * @param string $app
148
+     * @return string
149
+     * @since 4.0.0
150
+     */
151
+    public static function getAppVersion( $app ) {
152
+        return \OC_App::getAppVersion( $app );
153
+    }
154 154
 }
Please login to merge, or discard this patch.
apps/files_external/lib/AppInfo/Application.php 2 patches
Indentation   +104 added lines, -104 removed lines patch added patch discarded remove patch
@@ -41,109 +41,109 @@
 block discarded – undo
41 41
  */
42 42
 class Application extends App implements IBackendProvider, IAuthMechanismProvider {
43 43
 
44
-	public function __construct(array $urlParams = array()) {
45
-		parent::__construct('files_external', $urlParams);
46
-
47
-		$container = $this->getContainer();
48
-
49
-		$container->registerService('OCP\Files\Config\IUserMountCache', function (IAppContainer $c) {
50
-			return $c->getServer()->query('UserMountCache');
51
-		});
52
-
53
-		$backendService = $container->query('OCA\\Files_External\\Service\\BackendService');
54
-		$backendService->registerBackendProvider($this);
55
-		$backendService->registerAuthMechanismProvider($this);
56
-
57
-		// force-load auth mechanisms since some will register hooks
58
-		// TODO: obsolete these and use the TokenProvider to get the user's password from the session
59
-		$this->getAuthMechanisms();
60
-
61
-		// app developers: do NOT depend on this! it will disappear with oC 9.0!
62
-		\OC::$server->getEventDispatcher()->dispatch(
63
-			'OCA\\Files_External::loadAdditionalBackends'
64
-		);
65
-	}
66
-
67
-	/**
68
-	 * Register settings templates
69
-	 */
70
-	public function registerSettings() {
71
-		\OCP\App::registerPersonal('files_external', function () {
72
-			$container = $this->getContainer();
73
-			$userSession = $container->getServer()->getUserSession();
74
-			if (!$userSession->isLoggedIn()) {
75
-				return;
76
-			}
77
-			$backendService = $container->query('OCA\\Files_External\\Service\\BackendService');
78
-
79
-			/** @var \OCA\Files_External\Service\UserGlobalStoragesService $userGlobalStoragesService */
80
-			$userGlobalStoragesService = $container->query('OCA\Files_External\Service\UserGlobalStoragesService');
81
-			if (count($userGlobalStoragesService->getStorages()) > 0 || $backendService->isUserMountingAllowed()) {
82
-				return 'personal';
83
-			}
84
-		});
85
-	}
86
-
87
-	/**
88
-	 * @{inheritdoc}
89
-	 */
90
-	public function getBackends() {
91
-		$container = $this->getContainer();
92
-
93
-		$backends = [
94
-			$container->query('OCA\Files_External\Lib\Backend\Local'),
95
-			$container->query('OCA\Files_External\Lib\Backend\FTP'),
96
-			$container->query('OCA\Files_External\Lib\Backend\DAV'),
97
-			$container->query('OCA\Files_External\Lib\Backend\OwnCloud'),
98
-			$container->query('OCA\Files_External\Lib\Backend\SFTP'),
99
-			$container->query('OCA\Files_External\Lib\Backend\AmazonS3'),
100
-			$container->query('OCA\Files_External\Lib\Backend\Dropbox'),
101
-			$container->query('OCA\Files_External\Lib\Backend\Google'),
102
-			$container->query('OCA\Files_External\Lib\Backend\Swift'),
103
-			$container->query('OCA\Files_External\Lib\Backend\SFTP_Key'),
104
-			$container->query('OCA\Files_External\Lib\Backend\SMB'),
105
-			$container->query('OCA\Files_External\Lib\Backend\SMB_OC'),
106
-		];
107
-
108
-		return $backends;
109
-	}
110
-
111
-	/**
112
-	 * @{inheritdoc}
113
-	 */
114
-	public function getAuthMechanisms() {
115
-		$container = $this->getContainer();
116
-
117
-		return [
118
-			// AuthMechanism::SCHEME_NULL mechanism
119
-			$container->query('OCA\Files_External\Lib\Auth\NullMechanism'),
120
-
121
-			// AuthMechanism::SCHEME_BUILTIN mechanism
122
-			$container->query('OCA\Files_External\Lib\Auth\Builtin'),
123
-
124
-			// AuthMechanism::SCHEME_PASSWORD mechanisms
125
-			$container->query('OCA\Files_External\Lib\Auth\Password\Password'),
126
-			$container->query('OCA\Files_External\Lib\Auth\Password\SessionCredentials'),
127
-			$container->query('OCA\Files_External\Lib\Auth\Password\LoginCredentials'),
128
-			$container->query('OCA\Files_External\Lib\Auth\Password\UserProvided'),
129
-			$container->query('OCA\Files_External\Lib\Auth\Password\GlobalAuth'),
130
-
131
-			// AuthMechanism::SCHEME_OAUTH1 mechanisms
132
-			$container->query('OCA\Files_External\Lib\Auth\OAuth1\OAuth1'),
133
-
134
-			// AuthMechanism::SCHEME_OAUTH2 mechanisms
135
-			$container->query('OCA\Files_External\Lib\Auth\OAuth2\OAuth2'),
136
-
137
-			// AuthMechanism::SCHEME_PUBLICKEY mechanisms
138
-			$container->query('OCA\Files_External\Lib\Auth\PublicKey\RSA'),
139
-
140
-			// AuthMechanism::SCHEME_OPENSTACK mechanisms
141
-			$container->query('OCA\Files_External\Lib\Auth\OpenStack\OpenStack'),
142
-			$container->query('OCA\Files_External\Lib\Auth\OpenStack\Rackspace'),
143
-
144
-			// Specialized mechanisms
145
-			$container->query('OCA\Files_External\Lib\Auth\AmazonS3\AccessKey'),
146
-		];
147
-	}
44
+    public function __construct(array $urlParams = array()) {
45
+        parent::__construct('files_external', $urlParams);
46
+
47
+        $container = $this->getContainer();
48
+
49
+        $container->registerService('OCP\Files\Config\IUserMountCache', function (IAppContainer $c) {
50
+            return $c->getServer()->query('UserMountCache');
51
+        });
52
+
53
+        $backendService = $container->query('OCA\\Files_External\\Service\\BackendService');
54
+        $backendService->registerBackendProvider($this);
55
+        $backendService->registerAuthMechanismProvider($this);
56
+
57
+        // force-load auth mechanisms since some will register hooks
58
+        // TODO: obsolete these and use the TokenProvider to get the user's password from the session
59
+        $this->getAuthMechanisms();
60
+
61
+        // app developers: do NOT depend on this! it will disappear with oC 9.0!
62
+        \OC::$server->getEventDispatcher()->dispatch(
63
+            'OCA\\Files_External::loadAdditionalBackends'
64
+        );
65
+    }
66
+
67
+    /**
68
+     * Register settings templates
69
+     */
70
+    public function registerSettings() {
71
+        \OCP\App::registerPersonal('files_external', function () {
72
+            $container = $this->getContainer();
73
+            $userSession = $container->getServer()->getUserSession();
74
+            if (!$userSession->isLoggedIn()) {
75
+                return;
76
+            }
77
+            $backendService = $container->query('OCA\\Files_External\\Service\\BackendService');
78
+
79
+            /** @var \OCA\Files_External\Service\UserGlobalStoragesService $userGlobalStoragesService */
80
+            $userGlobalStoragesService = $container->query('OCA\Files_External\Service\UserGlobalStoragesService');
81
+            if (count($userGlobalStoragesService->getStorages()) > 0 || $backendService->isUserMountingAllowed()) {
82
+                return 'personal';
83
+            }
84
+        });
85
+    }
86
+
87
+    /**
88
+     * @{inheritdoc}
89
+     */
90
+    public function getBackends() {
91
+        $container = $this->getContainer();
92
+
93
+        $backends = [
94
+            $container->query('OCA\Files_External\Lib\Backend\Local'),
95
+            $container->query('OCA\Files_External\Lib\Backend\FTP'),
96
+            $container->query('OCA\Files_External\Lib\Backend\DAV'),
97
+            $container->query('OCA\Files_External\Lib\Backend\OwnCloud'),
98
+            $container->query('OCA\Files_External\Lib\Backend\SFTP'),
99
+            $container->query('OCA\Files_External\Lib\Backend\AmazonS3'),
100
+            $container->query('OCA\Files_External\Lib\Backend\Dropbox'),
101
+            $container->query('OCA\Files_External\Lib\Backend\Google'),
102
+            $container->query('OCA\Files_External\Lib\Backend\Swift'),
103
+            $container->query('OCA\Files_External\Lib\Backend\SFTP_Key'),
104
+            $container->query('OCA\Files_External\Lib\Backend\SMB'),
105
+            $container->query('OCA\Files_External\Lib\Backend\SMB_OC'),
106
+        ];
107
+
108
+        return $backends;
109
+    }
110
+
111
+    /**
112
+     * @{inheritdoc}
113
+     */
114
+    public function getAuthMechanisms() {
115
+        $container = $this->getContainer();
116
+
117
+        return [
118
+            // AuthMechanism::SCHEME_NULL mechanism
119
+            $container->query('OCA\Files_External\Lib\Auth\NullMechanism'),
120
+
121
+            // AuthMechanism::SCHEME_BUILTIN mechanism
122
+            $container->query('OCA\Files_External\Lib\Auth\Builtin'),
123
+
124
+            // AuthMechanism::SCHEME_PASSWORD mechanisms
125
+            $container->query('OCA\Files_External\Lib\Auth\Password\Password'),
126
+            $container->query('OCA\Files_External\Lib\Auth\Password\SessionCredentials'),
127
+            $container->query('OCA\Files_External\Lib\Auth\Password\LoginCredentials'),
128
+            $container->query('OCA\Files_External\Lib\Auth\Password\UserProvided'),
129
+            $container->query('OCA\Files_External\Lib\Auth\Password\GlobalAuth'),
130
+
131
+            // AuthMechanism::SCHEME_OAUTH1 mechanisms
132
+            $container->query('OCA\Files_External\Lib\Auth\OAuth1\OAuth1'),
133
+
134
+            // AuthMechanism::SCHEME_OAUTH2 mechanisms
135
+            $container->query('OCA\Files_External\Lib\Auth\OAuth2\OAuth2'),
136
+
137
+            // AuthMechanism::SCHEME_PUBLICKEY mechanisms
138
+            $container->query('OCA\Files_External\Lib\Auth\PublicKey\RSA'),
139
+
140
+            // AuthMechanism::SCHEME_OPENSTACK mechanisms
141
+            $container->query('OCA\Files_External\Lib\Auth\OpenStack\OpenStack'),
142
+            $container->query('OCA\Files_External\Lib\Auth\OpenStack\Rackspace'),
143
+
144
+            // Specialized mechanisms
145
+            $container->query('OCA\Files_External\Lib\Auth\AmazonS3\AccessKey'),
146
+        ];
147
+    }
148 148
 
149 149
 }
Please login to merge, or discard this patch.
Spacing   +2 added lines, -2 removed lines patch added patch discarded remove patch
@@ -46,7 +46,7 @@  discard block
 block discarded – undo
46 46
 
47 47
 		$container = $this->getContainer();
48 48
 
49
-		$container->registerService('OCP\Files\Config\IUserMountCache', function (IAppContainer $c) {
49
+		$container->registerService('OCP\Files\Config\IUserMountCache', function(IAppContainer $c) {
50 50
 			return $c->getServer()->query('UserMountCache');
51 51
 		});
52 52
 
@@ -68,7 +68,7 @@  discard block
 block discarded – undo
68 68
 	 * Register settings templates
69 69
 	 */
70 70
 	public function registerSettings() {
71
-		\OCP\App::registerPersonal('files_external', function () {
71
+		\OCP\App::registerPersonal('files_external', function() {
72 72
 			$container = $this->getContainer();
73 73
 			$userSession = $container->getServer()->getUserSession();
74 74
 			if (!$userSession->isLoggedIn()) {
Please login to merge, or discard this patch.