Welcome to mirror list, hosted at ThFree Co, Russian Federation.

AppSettingsController.php « Controller « settings - github.com/nextcloud/server.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: c7722267f197c5b139f6838924210029a3ec2019 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
<?php
/**
 * @author Christoph Wurst <christoph@owncloud.com>
 * @author Joas Schilling <nickvergessen@owncloud.com>
 * @author Lukas Reschke <lukas@statuscode.ch>
 * @author Morris Jobke <hey@morrisjobke.de>
 * @author Thomas Müller <thomas.mueller@tmit.eu>
 *
 * @copyright Copyright (c) 2016, ownCloud, Inc.
 * @license AGPL-3.0
 *
 * This code is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License, version 3,
 * as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License, version 3,
 * along with this program.  If not, see <http://www.gnu.org/licenses/>
 *
 */

namespace OC\Settings\Controller;

use OC\App\DependencyAnalyzer;
use OC\App\Platform;
use OC\OCSClient;
use OCP\App\IAppManager;
use \OCP\AppFramework\Controller;
use OCP\AppFramework\Http\ContentSecurityPolicy;
use OCP\AppFramework\Http\DataResponse;
use OCP\AppFramework\Http\TemplateResponse;
use OCP\ICacheFactory;
use OCP\INavigationManager;
use OCP\IRequest;
use OCP\IL10N;
use OCP\IConfig;

/**
 * @package OC\Settings\Controller
 */
class AppSettingsController extends Controller {
	const CAT_ENABLED = 0;
	const CAT_DISABLED = 1;

	/** @var \OCP\IL10N */
	private $l10n;
	/** @var IConfig */
	private $config;
	/** @var \OCP\ICache */
	private $cache;
	/** @var INavigationManager */
	private $navigationManager;
	/** @var IAppManager */
	private $appManager;
	/** @var OCSClient */
	private $ocsClient;

	/**
	 * @param string $appName
	 * @param IRequest $request
	 * @param IL10N $l10n
	 * @param IConfig $config
	 * @param ICacheFactory $cache
	 * @param INavigationManager $navigationManager
	 * @param IAppManager $appManager
	 * @param OCSClient $ocsClient
	 */
	public function __construct($appName,
								IRequest $request,
								IL10N $l10n,
								IConfig $config,
								ICacheFactory $cache,
								INavigationManager $navigationManager,
								IAppManager $appManager,
								OCSClient $ocsClient) {
		parent::__construct($appName, $request);
		$this->l10n = $l10n;
		$this->config = $config;
		$this->cache = $cache->create($appName);
		$this->navigationManager = $navigationManager;
		$this->appManager = $appManager;
		$this->ocsClient = $ocsClient;
	}

	/**
	 * Enables or disables the display of experimental apps
	 * @param bool $state
	 * @return DataResponse
	 */
	public function changeExperimentalConfigState($state) {
		$this->config->setSystemValue('appstore.experimental.enabled', $state);
		$this->appManager->clearAppsCache();
		return new DataResponse();
	}

	/**
	 * @param string|int $category
	 * @return int
	 */
	protected function getCategory($category) {
		if (is_string($category)) {
			foreach ($this->listCategories() as $cat) {
				if (isset($cat['ident']) && $cat['ident'] === $category) {
					$category = (int) $cat['id'];
					break;
				}
			}

			// Didn't find the category, falling back to enabled
			if (is_string($category)) {
				$category = self::CAT_ENABLED;
			}
		}
		return (int) $category;
	}

	/**
	 * @NoCSRFRequired
	 * @param string $category
	 * @return TemplateResponse
	 */
	public function viewApps($category = '') {
		$categoryId = $this->getCategory($category);
		if ($categoryId === self::CAT_ENABLED) {
			// Do not use an arbitrary input string, because we put the category in html
			$category = 'enabled';
		}

		$params = [];
		$params['experimentalEnabled'] = $this->config->getSystemValue('appstore.experimental.enabled', false);
		$params['category'] = $category;
		$params['appstoreEnabled'] = $this->config->getSystemValue('appstoreenabled', true) === true;
		$this->navigationManager->setActiveEntry('core_apps');

		$templateResponse = new TemplateResponse($this->appName, 'apps', $params, 'user');
		$policy = new ContentSecurityPolicy();
		$policy->addAllowedImageDomain('https://apps.owncloud.com');
		$templateResponse->setContentSecurityPolicy($policy);

		return $templateResponse;
	}

	/**
	 * Get all available categories
	 * @return array
	 */
	public function listCategories() {

		if(!is_null($this->cache->get('listCategories'))) {
			return $this->cache->get('listCategories');
		}
		$categories = [
			['id' => self::CAT_ENABLED, 'ident' => 'enabled', 'displayName' => (string)$this->l10n->t('Enabled')],
			['id' => self::CAT_DISABLED, 'ident' => 'disabled', 'displayName' => (string)$this->l10n->t('Not enabled')],
		];

		if($this->ocsClient->isAppStoreEnabled()) {
			// apps from external repo via OCS
			$ocs = $this->ocsClient->getCategories(\OCP\Util::getVersion());
			if ($ocs) {
				foreach($ocs as $k => $v) {
					$name = str_replace('ownCloud ', '', $v);
					$ident = str_replace(' ', '-', urlencode(strtolower($name)));
					$categories[] = [
						'id' => $k,
						'ident' => $ident,
						'displayName' => $name,
					];
				}
			}
		}

		$this->cache->set('listCategories', $categories, 3600);

		return $categories;
	}

	/**
	 * Get all available apps in a category
	 *
	 * @param string $category
	 * @param bool $includeUpdateInfo Should we check whether there is an update
	 *                                in the app store?
	 * @return array
	 */
	public function listApps($category = '', $includeUpdateInfo = true) {
		$category = $this->getCategory($category);
		$cacheName = 'listApps-' . $category . '-' . (int) $includeUpdateInfo;

		if(!is_null($this->cache->get($cacheName))) {
			$apps = $this->cache->get($cacheName);
		} else {
			switch ($category) {
				// installed apps
				case 0:
					$apps = $this->getInstalledApps($includeUpdateInfo);
					usort($apps, function ($a, $b) {
						$a = (string)$a['name'];
						$b = (string)$b['name'];
						if ($a === $b) {
							return 0;
						}
						return ($a < $b) ? -1 : 1;
					});
					$version = \OCP\Util::getVersion();
					foreach($apps as $key => $app) {
						if(!array_key_exists('level', $app) && array_key_exists('ocsid', $app)) {
							$remoteAppEntry = $this->ocsClient->getApplication($app['ocsid'], $version);

							if(is_array($remoteAppEntry) && array_key_exists('level', $remoteAppEntry)) {
								$apps[$key]['level'] = $remoteAppEntry['level'];
							}
						}
					}
					break;
				// not-installed apps
				case 1:
					$apps = \OC_App::listAllApps(true, $includeUpdateInfo, $this->ocsClient);
					$apps = array_filter($apps, function ($app) {
						return !$app['active'];
					});
					$version = \OCP\Util::getVersion();
					foreach($apps as $key => $app) {
						if(!array_key_exists('level', $app) && array_key_exists('ocsid', $app)) {
							$remoteAppEntry = $this->ocsClient->getApplication($app['ocsid'], $version);

							if(is_array($remoteAppEntry) && array_key_exists('level', $remoteAppEntry)) {
								$apps[$key]['level'] = $remoteAppEntry['level'];
							}
						}
					}
					usort($apps, function ($a, $b) {
						$a = (string)$a['name'];
						$b = (string)$b['name'];
						if ($a === $b) {
							return 0;
						}
						return ($a < $b) ? -1 : 1;
					});
					break;
				default:
					$filter = $this->config->getSystemValue('appstore.experimental.enabled', false) ? 'all' : 'approved';

					$apps = \OC_App::getAppstoreApps($filter, $category, $this->ocsClient);
					if (!$apps) {
						$apps = array();
					} else {
						// don't list installed apps
						$installedApps = $this->getInstalledApps(false);
						$installedApps = array_map(function ($app) {
							if (isset($app['ocsid'])) {
								return $app['ocsid'];
							}
							return $app['id'];
						}, $installedApps);
						$apps = array_filter($apps, function ($app) use ($installedApps) {
							return !in_array($app['id'], $installedApps);
						});

						// show tooltip if app is downloaded from remote server
						$inactiveApps = $this->getInactiveApps();
						foreach ($apps as &$app) {
							$app['needsDownload'] = !in_array($app['id'], $inactiveApps);
						}
					}

					// sort by score
					usort($apps, function ($a, $b) {
						$a = (int)$a['score'];
						$b = (int)$b['score'];
						if ($a === $b) {
							return 0;
						}
						return ($a > $b) ? -1 : 1;
					});
					break;
			}
		}

		// fix groups to be an array
		$dependencyAnalyzer = new DependencyAnalyzer(new Platform($this->config), $this->l10n);
		$apps = array_map(function($app) use ($dependencyAnalyzer) {

			// fix groups
			$groups = array();
			if (is_string($app['groups'])) {
				$groups = json_decode($app['groups']);
			}
			$app['groups'] = $groups;
			$app['canUnInstall'] = !$app['active'] && $app['removable'];

			// fix licence vs license
			if (isset($app['license']) && !isset($app['licence'])) {
				$app['licence'] = $app['license'];
			}

			// analyse dependencies
			$missing = $dependencyAnalyzer->analyze($app);
			$app['canInstall'] = empty($missing);
			$app['missingDependencies'] = $missing;

			$app['missingMinOwnCloudVersion'] = !isset($app['dependencies']['owncloud']['@attributes']['min-version']);
			$app['missingMaxOwnCloudVersion'] = !isset($app['dependencies']['owncloud']['@attributes']['max-version']);

			return $app;
		}, $apps);

		$this->cache->set($cacheName, $apps, 300);

		return ['apps' => $apps, 'status' => 'success'];
	}

	/**
	 * @param bool $includeUpdateInfo Should we check whether there is an update
	 *                                in the app store?
	 * @return array
	 */
	private function getInstalledApps($includeUpdateInfo = true) {
		$apps = \OC_App::listAllApps(true, $includeUpdateInfo, $this->ocsClient);
		$apps = array_filter($apps, function ($app) {
			return $app['active'];
		});
		return $apps;
	}

	/**
	 * @return array
	 */
	private function getInactiveApps() {
		$inactiveApps = \OC_App::listAllApps(true, false, $this->ocsClient);
		$inactiveApps = array_filter($inactiveApps,
			function ($app) {
			return !$app['active'];
		});
		$inactiveApps = array_map(function($app) {
			if (isset($app['ocsid'])) {
				return $app['ocsid'];
			}
			return $app['id'];
		}, $inactiveApps);
		return $inactiveApps;
	}

}