AppSettingsController.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  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. 'removable' => $existsLocally,
  225. 'active' => $this->appManager->isEnabledForUser($app['id']),
  226. 'needsDownload' => !$existsLocally,
  227. 'groups' => $groups,
  228. 'fromAppStore' => true,
  229. ];
  230. $appFetcher = \OC::$server->getAppFetcher();
  231. $newVersion = \OC\Installer::isUpdateAvailable($app['id'], $appFetcher);
  232. if($newVersion && $this->appManager->isInstalled($app['id'])) {
  233. $formattedApps[count($formattedApps)-1]['update'] = $newVersion;
  234. }
  235. }
  236. return $formattedApps;
  237. }
  238. /**
  239. * Get all available apps in a category
  240. *
  241. * @param string $category
  242. * @return JSONResponse
  243. */
  244. public function listApps($category = '') {
  245. $appClass = new \OC_App();
  246. switch ($category) {
  247. // installed apps
  248. case 'enabled':
  249. $apps = $appClass->listAllApps();
  250. $apps = array_filter($apps, function ($app) {
  251. return $app['active'];
  252. });
  253. foreach($apps as $key => $app) {
  254. $newVersion = \OC\Installer::isUpdateAvailable($app['id'], $this->appFetcher);
  255. $apps[$key]['update'] = $newVersion;
  256. }
  257. usort($apps, function ($a, $b) {
  258. $a = (string)$a['name'];
  259. $b = (string)$b['name'];
  260. if ($a === $b) {
  261. return 0;
  262. }
  263. return ($a < $b) ? -1 : 1;
  264. });
  265. break;
  266. // disabled apps
  267. case 'disabled':
  268. $apps = $appClass->listAllApps();
  269. $apps = array_filter($apps, function ($app) {
  270. return !$app['active'];
  271. });
  272. usort($apps, function ($a, $b) {
  273. $a = (string)$a['name'];
  274. $b = (string)$b['name'];
  275. if ($a === $b) {
  276. return 0;
  277. }
  278. return ($a < $b) ? -1 : 1;
  279. });
  280. break;
  281. default:
  282. $apps = $this->getAppsForCategory($category);
  283. // sort by score
  284. usort($apps, function ($a, $b) {
  285. $a = (int)$a['score'];
  286. $b = (int)$b['score'];
  287. if ($a === $b) {
  288. return 0;
  289. }
  290. return ($a > $b) ? -1 : 1;
  291. });
  292. break;
  293. }
  294. // fix groups to be an array
  295. $dependencyAnalyzer = new DependencyAnalyzer(new Platform($this->config), $this->l10n);
  296. $apps = array_map(function($app) use ($dependencyAnalyzer) {
  297. // fix groups
  298. $groups = array();
  299. if (is_string($app['groups'])) {
  300. $groups = json_decode($app['groups']);
  301. }
  302. $app['groups'] = $groups;
  303. $app['canUnInstall'] = !$app['active'] && $app['removable'];
  304. // fix licence vs license
  305. if (isset($app['license']) && !isset($app['licence'])) {
  306. $app['licence'] = $app['license'];
  307. }
  308. // analyse dependencies
  309. $missing = $dependencyAnalyzer->analyze($app);
  310. $app['canInstall'] = empty($missing);
  311. $app['missingDependencies'] = $missing;
  312. $app['missingMinOwnCloudVersion'] = !isset($app['dependencies']['nextcloud']['@attributes']['min-version']);
  313. $app['missingMaxOwnCloudVersion'] = !isset($app['dependencies']['nextcloud']['@attributes']['max-version']);
  314. return $app;
  315. }, $apps);
  316. return new JSONResponse(['apps' => $apps, 'status' => 'success']);
  317. }
  318. }