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