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