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