AppSettingsController.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  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. // Skip all apps not in the requested category
  145. $isInCategory = false;
  146. foreach($app['categories'] as $category) {
  147. if($category === $requestedCategory) {
  148. $isInCategory = true;
  149. }
  150. }
  151. if(!$isInCategory) {
  152. continue;
  153. }
  154. $nextCloudVersion = $versionParser->getVersion($app['releases'][0]['rawPlatformVersionSpec']);
  155. $nextCloudVersionDependencies = [];
  156. if($nextCloudVersion->getMinimumVersion() !== '') {
  157. $nextCloudVersionDependencies['nextcloud']['@attributes']['min-version'] = $nextCloudVersion->getMinimumVersion();
  158. }
  159. if($nextCloudVersion->getMaximumVersion() !== '') {
  160. $nextCloudVersionDependencies['nextcloud']['@attributes']['max-version'] = $nextCloudVersion->getMaximumVersion();
  161. }
  162. $phpVersion = $versionParser->getVersion($app['releases'][0]['rawPhpVersionSpec']);
  163. $existsLocally = (\OC_App::getAppPath($app['id']) !== false) ? true : false;
  164. $phpDependencies = [];
  165. if($phpVersion->getMinimumVersion() !== '') {
  166. $phpDependencies['php']['@attributes']['min-version'] = $phpVersion->getMinimumVersion();
  167. }
  168. if($phpVersion->getMaximumVersion() !== '') {
  169. $phpDependencies['php']['@attributes']['max-version'] = $phpVersion->getMaximumVersion();
  170. }
  171. if(isset($app['releases'][0]['minIntSize'])) {
  172. $phpDependencies['php']['@attributes']['min-int-size'] = $app['releases'][0]['minIntSize'];
  173. }
  174. $authors = '';
  175. foreach($app['authors'] as $key => $author) {
  176. $authors .= $author['name'];
  177. if($key !== count($app['authors']) - 1) {
  178. $authors .= ', ';
  179. }
  180. }
  181. $currentLanguage = substr(\OC::$server->getL10NFactory()->findLanguage(), 0, 2);
  182. $enabledValue = $this->config->getAppValue($app['id'], 'enabled', 'no');
  183. $groups = null;
  184. if($enabledValue !== 'no' && $enabledValue !== 'yes') {
  185. $groups = $enabledValue;
  186. }
  187. $currentVersion = '';
  188. if($this->appManager->isInstalled($app['id'])) {
  189. $currentVersion = \OC_App::getAppVersion($app['id']);
  190. } else {
  191. $currentLanguage = $app['releases'][0]['version'];
  192. }
  193. $formattedApps[] = [
  194. 'id' => $app['id'],
  195. 'name' => isset($app['translations'][$currentLanguage]['name']) ? $app['translations'][$currentLanguage]['name'] : $app['translations']['en']['name'],
  196. 'description' => isset($app['translations'][$currentLanguage]['description']) ? $app['translations'][$currentLanguage]['description'] : $app['translations']['en']['description'],
  197. 'license' => $app['releases'][0]['licenses'],
  198. 'author' => $authors,
  199. 'shipped' => false,
  200. 'version' => $currentVersion,
  201. 'default_enable' => '',
  202. 'types' => [],
  203. 'documentation' => [
  204. 'admin' => $app['adminDocs'],
  205. 'user' => $app['userDocs'],
  206. 'developer' => $app['developerDocs']
  207. ],
  208. 'website' => $app['website'],
  209. 'bugs' => $app['issueTracker'],
  210. 'detailpage' => $app['website'],
  211. 'dependencies' => array_merge(
  212. $nextCloudVersionDependencies,
  213. $phpDependencies
  214. ),
  215. 'level' => ($app['featured'] === true) ? 200 : 100,
  216. 'missingMaxOwnCloudVersion' => false,
  217. 'missingMinOwnCloudVersion' => false,
  218. 'canInstall' => true,
  219. 'preview' => isset($app['screenshots'][0]['url']) ? 'https://usercontent.apps.nextcloud.com/'.base64_encode($app['screenshots'][0]['url']) : '',
  220. 'score' => $app['ratingOverall'],
  221. 'removable' => $existsLocally,
  222. 'active' => $this->appManager->isEnabledForUser($app['id']),
  223. 'needsDownload' => !$existsLocally,
  224. 'groups' => $groups,
  225. 'fromAppStore' => true,
  226. ];
  227. $appFetcher = \OC::$server->getAppFetcher();
  228. $newVersion = \OC\Installer::isUpdateAvailable($app['id'], $appFetcher);
  229. if($newVersion && $this->appManager->isInstalled($app['id'])) {
  230. $formattedApps[count($formattedApps)-1]['update'] = $newVersion;
  231. }
  232. }
  233. return $formattedApps;
  234. }
  235. /**
  236. * Get all available apps in a category
  237. *
  238. * @param string $category
  239. * @return JSONResponse
  240. */
  241. public function listApps($category = '') {
  242. $appClass = new \OC_App();
  243. switch ($category) {
  244. // installed apps
  245. case 'enabled':
  246. $apps = $appClass->listAllApps();
  247. $apps = array_filter($apps, function ($app) {
  248. return $app['active'];
  249. });
  250. usort($apps, function ($a, $b) {
  251. $a = (string)$a['name'];
  252. $b = (string)$b['name'];
  253. if ($a === $b) {
  254. return 0;
  255. }
  256. return ($a < $b) ? -1 : 1;
  257. });
  258. break;
  259. // disabled apps
  260. case 'disabled':
  261. $apps = $appClass->listAllApps();
  262. $apps = array_filter($apps, function ($app) {
  263. return !$app['active'];
  264. });
  265. usort($apps, function ($a, $b) {
  266. $a = (string)$a['name'];
  267. $b = (string)$b['name'];
  268. if ($a === $b) {
  269. return 0;
  270. }
  271. return ($a < $b) ? -1 : 1;
  272. });
  273. break;
  274. default:
  275. $apps = $this->getAppsForCategory($category);
  276. // sort by score
  277. usort($apps, function ($a, $b) {
  278. $a = (int)$a['score'];
  279. $b = (int)$b['score'];
  280. if ($a === $b) {
  281. return 0;
  282. }
  283. return ($a > $b) ? -1 : 1;
  284. });
  285. break;
  286. }
  287. // fix groups to be an array
  288. $dependencyAnalyzer = new DependencyAnalyzer(new Platform($this->config), $this->l10n);
  289. $apps = array_map(function($app) use ($dependencyAnalyzer) {
  290. // fix groups
  291. $groups = array();
  292. if (is_string($app['groups'])) {
  293. $groups = json_decode($app['groups']);
  294. }
  295. $app['groups'] = $groups;
  296. $app['canUnInstall'] = !$app['active'] && $app['removable'];
  297. // fix licence vs license
  298. if (isset($app['license']) && !isset($app['licence'])) {
  299. $app['licence'] = $app['license'];
  300. }
  301. // analyse dependencies
  302. $missing = $dependencyAnalyzer->analyze($app);
  303. $app['canInstall'] = empty($missing);
  304. $app['missingDependencies'] = $missing;
  305. $app['missingMinOwnCloudVersion'] = !isset($app['dependencies']['nextcloud']['@attributes']['min-version']);
  306. $app['missingMaxOwnCloudVersion'] = !isset($app['dependencies']['nextcloud']['@attributes']['max-version']);
  307. return $app;
  308. }, $apps);
  309. return new JSONResponse(['apps' => $apps, 'status' => 'success']);
  310. }
  311. }