AppSettingsController.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. * @copyright Copyright (c) 2016, Lukas Reschke <lukas@statuscode.ch>
  5. *
  6. * @author Christoph Wurst <christoph@owncloud.com>
  7. * @author Joas Schilling <coding@schilljs.com>
  8. * @author Lukas Reschke <lukas@statuscode.ch>
  9. * @author Morris Jobke <hey@morrisjobke.de>
  10. * @author Thomas Müller <thomas.mueller@tmit.eu>
  11. *
  12. * @license AGPL-3.0
  13. *
  14. * This code is free software: you can redistribute it and/or modify
  15. * it under the terms of the GNU Affero General Public License, version 3,
  16. * as published by the Free Software Foundation.
  17. *
  18. * This program is distributed in the hope that it will be useful,
  19. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. * GNU Affero General Public License for more details.
  22. *
  23. * You should have received a copy of the GNU Affero General Public License, version 3,
  24. * along with this program. If not, see <http://www.gnu.org/licenses/>
  25. *
  26. */
  27. namespace OC\Settings\Controller;
  28. use OC\App\AppStore\Fetcher\AppFetcher;
  29. use OC\App\AppStore\Fetcher\CategoryFetcher;
  30. use OC\App\AppStore\Version\VersionParser;
  31. use OC\App\DependencyAnalyzer;
  32. use OC\App\Platform;
  33. use OCP\App\IAppManager;
  34. use \OCP\AppFramework\Controller;
  35. use OCP\AppFramework\Http\ContentSecurityPolicy;
  36. use OCP\AppFramework\Http\JSONResponse;
  37. use OCP\AppFramework\Http\TemplateResponse;
  38. use OCP\INavigationManager;
  39. use OCP\IRequest;
  40. use OCP\IL10N;
  41. use OCP\IConfig;
  42. use OCP\L10N\IFactory;
  43. /**
  44. * @package OC\Settings\Controller
  45. */
  46. class AppSettingsController extends Controller {
  47. const CAT_ENABLED = 0;
  48. const CAT_DISABLED = 1;
  49. /** @var \OCP\IL10N */
  50. private $l10n;
  51. /** @var IConfig */
  52. private $config;
  53. /** @var INavigationManager */
  54. private $navigationManager;
  55. /** @var IAppManager */
  56. private $appManager;
  57. /** @var CategoryFetcher */
  58. private $categoryFetcher;
  59. /** @var AppFetcher */
  60. private $appFetcher;
  61. /** @var IFactory */
  62. private $l10nFactory;
  63. /**
  64. * @param string $appName
  65. * @param IRequest $request
  66. * @param IL10N $l10n
  67. * @param IConfig $config
  68. * @param INavigationManager $navigationManager
  69. * @param IAppManager $appManager
  70. * @param CategoryFetcher $categoryFetcher
  71. * @param AppFetcher $appFetcher
  72. * @param IFactory $l10nFactory
  73. */
  74. public function __construct($appName,
  75. IRequest $request,
  76. IL10N $l10n,
  77. IConfig $config,
  78. INavigationManager $navigationManager,
  79. IAppManager $appManager,
  80. CategoryFetcher $categoryFetcher,
  81. AppFetcher $appFetcher,
  82. IFactory $l10nFactory) {
  83. parent::__construct($appName, $request);
  84. $this->l10n = $l10n;
  85. $this->config = $config;
  86. $this->navigationManager = $navigationManager;
  87. $this->appManager = $appManager;
  88. $this->categoryFetcher = $categoryFetcher;
  89. $this->appFetcher = $appFetcher;
  90. $this->l10nFactory = $l10nFactory;
  91. }
  92. /**
  93. * @NoCSRFRequired
  94. *
  95. * @param string $category
  96. * @return TemplateResponse
  97. */
  98. public function viewApps($category = '') {
  99. if ($category === '') {
  100. $category = 'enabled';
  101. }
  102. $params = [];
  103. $params['category'] = $category;
  104. $params['appstoreEnabled'] = $this->config->getSystemValue('appstoreenabled', true) === true;
  105. $this->navigationManager->setActiveEntry('core_apps');
  106. $templateResponse = new TemplateResponse($this->appName, 'apps', $params, 'user');
  107. $policy = new ContentSecurityPolicy();
  108. $policy->addAllowedImageDomain('https://usercontent.apps.nextcloud.com');
  109. $templateResponse->setContentSecurityPolicy($policy);
  110. return $templateResponse;
  111. }
  112. /**
  113. * Get all available categories
  114. *
  115. * @return JSONResponse
  116. */
  117. public function listCategories() {
  118. $currentLanguage = substr($this->l10nFactory->findLanguage(), 0, 2);
  119. $formattedCategories = [
  120. ['id' => self::CAT_ENABLED, 'ident' => 'enabled', 'displayName' => (string)$this->l10n->t('Enabled')],
  121. ['id' => self::CAT_DISABLED, 'ident' => 'disabled', 'displayName' => (string)$this->l10n->t('Not enabled')],
  122. ];
  123. $categories = $this->categoryFetcher->get();
  124. foreach($categories as $category) {
  125. $formattedCategories[] = [
  126. 'id' => $category['id'],
  127. 'ident' => $category['id'],
  128. 'displayName' => isset($category['translations'][$currentLanguage]['name']) ? $category['translations'][$currentLanguage]['name'] : $category['translations']['en']['name'],
  129. ];
  130. }
  131. return new JSONResponse($formattedCategories);
  132. }
  133. /**
  134. * Get all apps for a category
  135. *
  136. * @param string $requestedCategory
  137. * @return array
  138. */
  139. private function getAppsForCategory($requestedCategory) {
  140. $versionParser = new VersionParser();
  141. $formattedApps = [];
  142. $apps = $this->appFetcher->get();
  143. foreach($apps as $app) {
  144. if (isset($app['isFeatured'])) {
  145. $app['featured'] = $app['isFeatured'];
  146. }
  147. // Skip all apps not in the requested category
  148. $isInCategory = false;
  149. foreach($app['categories'] as $category) {
  150. if($category === $requestedCategory) {
  151. $isInCategory = true;
  152. }
  153. }
  154. if(!$isInCategory) {
  155. continue;
  156. }
  157. $nextCloudVersion = $versionParser->getVersion($app['releases'][0]['rawPlatformVersionSpec']);
  158. $nextCloudVersionDependencies = [];
  159. if($nextCloudVersion->getMinimumVersion() !== '') {
  160. $nextCloudVersionDependencies['nextcloud']['@attributes']['min-version'] = $nextCloudVersion->getMinimumVersion();
  161. }
  162. if($nextCloudVersion->getMaximumVersion() !== '') {
  163. $nextCloudVersionDependencies['nextcloud']['@attributes']['max-version'] = $nextCloudVersion->getMaximumVersion();
  164. }
  165. $phpVersion = $versionParser->getVersion($app['releases'][0]['rawPhpVersionSpec']);
  166. $existsLocally = (\OC_App::getAppPath($app['id']) !== false) ? true : false;
  167. $phpDependencies = [];
  168. if($phpVersion->getMinimumVersion() !== '') {
  169. $phpDependencies['php']['@attributes']['min-version'] = $phpVersion->getMinimumVersion();
  170. }
  171. if($phpVersion->getMaximumVersion() !== '') {
  172. $phpDependencies['php']['@attributes']['max-version'] = $phpVersion->getMaximumVersion();
  173. }
  174. if(isset($app['releases'][0]['minIntSize'])) {
  175. $phpDependencies['php']['@attributes']['min-int-size'] = $app['releases'][0]['minIntSize'];
  176. }
  177. $authors = '';
  178. foreach($app['authors'] as $key => $author) {
  179. $authors .= $author['name'];
  180. if($key !== count($app['authors']) - 1) {
  181. $authors .= ', ';
  182. }
  183. }
  184. $currentLanguage = substr(\OC::$server->getL10NFactory()->findLanguage(), 0, 2);
  185. $enabledValue = $this->config->getAppValue($app['id'], 'enabled', 'no');
  186. $groups = null;
  187. if($enabledValue !== 'no' && $enabledValue !== 'yes') {
  188. $groups = $enabledValue;
  189. }
  190. $currentVersion = '';
  191. if($this->appManager->isInstalled($app['id'])) {
  192. $currentVersion = \OC_App::getAppVersion($app['id']);
  193. } else {
  194. $currentLanguage = $app['releases'][0]['version'];
  195. }
  196. $formattedApps[] = [
  197. 'id' => $app['id'],
  198. 'name' => isset($app['translations'][$currentLanguage]['name']) ? $app['translations'][$currentLanguage]['name'] : $app['translations']['en']['name'],
  199. 'description' => isset($app['translations'][$currentLanguage]['description']) ? $app['translations'][$currentLanguage]['description'] : $app['translations']['en']['description'],
  200. 'license' => $app['releases'][0]['licenses'],
  201. 'author' => $authors,
  202. 'shipped' => false,
  203. 'version' => $currentVersion,
  204. 'default_enable' => '',
  205. 'types' => [],
  206. 'documentation' => [
  207. 'admin' => $app['adminDocs'],
  208. 'user' => $app['userDocs'],
  209. 'developer' => $app['developerDocs']
  210. ],
  211. 'website' => $app['website'],
  212. 'bugs' => $app['issueTracker'],
  213. 'detailpage' => $app['website'],
  214. 'dependencies' => array_merge(
  215. $nextCloudVersionDependencies,
  216. $phpDependencies
  217. ),
  218. 'level' => ($app['featured'] === true) ? 200 : 100,
  219. 'missingMaxOwnCloudVersion' => false,
  220. 'missingMinOwnCloudVersion' => false,
  221. 'canInstall' => true,
  222. 'preview' => isset($app['screenshots'][0]['url']) ? 'https://usercontent.apps.nextcloud.com/'.base64_encode($app['screenshots'][0]['url']) : '',
  223. 'score' => $app['ratingOverall'],
  224. 'ratingNumOverall' => $app['ratingNumOverall'],
  225. 'ratingNumThresholdReached' => $app['ratingNumOverall'] > 5 ? true : false,
  226. 'removable' => $existsLocally,
  227. 'active' => $this->appManager->isEnabledForUser($app['id']),
  228. 'needsDownload' => !$existsLocally,
  229. 'groups' => $groups,
  230. 'fromAppStore' => true,
  231. ];
  232. $appFetcher = \OC::$server->getAppFetcher();
  233. $newVersion = \OC\Installer::isUpdateAvailable($app['id'], $appFetcher);
  234. if($newVersion && $this->appManager->isInstalled($app['id'])) {
  235. $formattedApps[count($formattedApps)-1]['update'] = $newVersion;
  236. }
  237. }
  238. return $formattedApps;
  239. }
  240. /**
  241. * Get all available apps in a category
  242. *
  243. * @param string $category
  244. * @return JSONResponse
  245. */
  246. public function listApps($category = '') {
  247. $appClass = new \OC_App();
  248. switch ($category) {
  249. // installed apps
  250. case 'enabled':
  251. $apps = $appClass->listAllApps();
  252. $apps = array_filter($apps, function ($app) {
  253. return $app['active'];
  254. });
  255. foreach($apps as $key => $app) {
  256. $newVersion = \OC\Installer::isUpdateAvailable($app['id'], $this->appFetcher);
  257. $apps[$key]['update'] = $newVersion;
  258. }
  259. usort($apps, function ($a, $b) {
  260. $a = (string)$a['name'];
  261. $b = (string)$b['name'];
  262. if ($a === $b) {
  263. return 0;
  264. }
  265. return ($a < $b) ? -1 : 1;
  266. });
  267. break;
  268. // disabled apps
  269. case 'disabled':
  270. $apps = $appClass->listAllApps();
  271. $apps = array_filter($apps, function ($app) {
  272. return !$app['active'];
  273. });
  274. $apps = array_map(function ($app) {
  275. $newVersion = \OC\Installer::isUpdateAvailable($app['id'], $this->appFetcher);
  276. if ($newVersion !== false) {
  277. $app['update'] = $newVersion;
  278. }
  279. return $app;
  280. }, $apps);
  281. usort($apps, function ($a, $b) {
  282. $a = (string)$a['name'];
  283. $b = (string)$b['name'];
  284. if ($a === $b) {
  285. return 0;
  286. }
  287. return ($a < $b) ? -1 : 1;
  288. });
  289. break;
  290. default:
  291. $apps = $this->getAppsForCategory($category);
  292. // sort by score
  293. usort($apps, function ($a, $b) {
  294. $a = (int)$a['score'];
  295. $b = (int)$b['score'];
  296. if ($a === $b) {
  297. return 0;
  298. }
  299. return ($a > $b) ? -1 : 1;
  300. });
  301. break;
  302. }
  303. // fix groups to be an array
  304. $dependencyAnalyzer = new DependencyAnalyzer(new Platform($this->config), $this->l10n);
  305. $apps = array_map(function($app) use ($dependencyAnalyzer) {
  306. // fix groups
  307. $groups = array();
  308. if (is_string($app['groups'])) {
  309. $groups = json_decode($app['groups']);
  310. }
  311. $app['groups'] = $groups;
  312. $app['canUnInstall'] = !$app['active'] && $app['removable'];
  313. // fix licence vs license
  314. if (isset($app['license']) && !isset($app['licence'])) {
  315. $app['licence'] = $app['license'];
  316. }
  317. // analyse dependencies
  318. $missing = $dependencyAnalyzer->analyze($app);
  319. $app['canInstall'] = empty($missing);
  320. $app['missingDependencies'] = $missing;
  321. $app['missingMinOwnCloudVersion'] = !isset($app['dependencies']['nextcloud']['@attributes']['min-version']);
  322. $app['missingMaxOwnCloudVersion'] = !isset($app['dependencies']['nextcloud']['@attributes']['max-version']);
  323. return $app;
  324. }, $apps);
  325. return new JSONResponse(['apps' => $apps, 'status' => 'success']);
  326. }
  327. }