AppSettingsController.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474
  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 Felix A. Epp <work@felixepp.de>
  8. * @author Jan-Christoph Borchardt <hey@jancborchardt.net>
  9. * @author Joas Schilling <coding@schilljs.com>
  10. * @author Julius Härtl <jus@bitgrid.net>
  11. * @author Lukas Reschke <lukas@statuscode.ch>
  12. * @author Morris Jobke <hey@morrisjobke.de>
  13. * @author Roeland Jago Douma <roeland@famdouma.nl>
  14. * @author Thomas Müller <thomas.mueller@tmit.eu>
  15. *
  16. * @license AGPL-3.0
  17. *
  18. * This code is free software: you can redistribute it and/or modify
  19. * it under the terms of the GNU Affero General Public License, version 3,
  20. * as published by the Free Software Foundation.
  21. *
  22. * This program is distributed in the hope that it will be useful,
  23. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  24. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  25. * GNU Affero General Public License for more details.
  26. *
  27. * You should have received a copy of the GNU Affero General Public License, version 3,
  28. * along with this program. If not, see <http://www.gnu.org/licenses/>
  29. *
  30. */
  31. namespace OC\Settings\Controller;
  32. use OC\App\AppStore\Bundles\BundleFetcher;
  33. use OC\App\AppStore\Fetcher\AppFetcher;
  34. use OC\App\AppStore\Fetcher\CategoryFetcher;
  35. use OC\App\AppStore\Version\VersionParser;
  36. use OC\App\DependencyAnalyzer;
  37. use OC\App\Platform;
  38. use OC\Installer;
  39. use OCP\App\IAppManager;
  40. use \OCP\AppFramework\Controller;
  41. use OCP\AppFramework\Http\ContentSecurityPolicy;
  42. use OCP\AppFramework\Http\JSONResponse;
  43. use OCP\AppFramework\Http\TemplateResponse;
  44. use OCP\INavigationManager;
  45. use OCP\IRequest;
  46. use OCP\IL10N;
  47. use OCP\IConfig;
  48. use OCP\IURLGenerator;
  49. use OCP\L10N\IFactory;
  50. /**
  51. * @package OC\Settings\Controller
  52. */
  53. class AppSettingsController extends Controller {
  54. const CAT_ENABLED = 0;
  55. const CAT_DISABLED = 1;
  56. const CAT_ALL_INSTALLED = 2;
  57. const CAT_APP_BUNDLES = 3;
  58. const CAT_UPDATES = 4;
  59. /** @var \OCP\IL10N */
  60. private $l10n;
  61. /** @var IConfig */
  62. private $config;
  63. /** @var INavigationManager */
  64. private $navigationManager;
  65. /** @var IAppManager */
  66. private $appManager;
  67. /** @var CategoryFetcher */
  68. private $categoryFetcher;
  69. /** @var AppFetcher */
  70. private $appFetcher;
  71. /** @var IFactory */
  72. private $l10nFactory;
  73. /** @var BundleFetcher */
  74. private $bundleFetcher;
  75. /** @var Installer */
  76. private $installer;
  77. /** @var IURLGenerator */
  78. private $urlGenerator;
  79. /**
  80. * @param string $appName
  81. * @param IRequest $request
  82. * @param IL10N $l10n
  83. * @param IConfig $config
  84. * @param INavigationManager $navigationManager
  85. * @param IAppManager $appManager
  86. * @param CategoryFetcher $categoryFetcher
  87. * @param AppFetcher $appFetcher
  88. * @param IFactory $l10nFactory
  89. * @param BundleFetcher $bundleFetcher
  90. * @param Installer $installer
  91. * @param IURLGenerator $urlGenerator
  92. */
  93. public function __construct(string $appName,
  94. IRequest $request,
  95. IL10N $l10n,
  96. IConfig $config,
  97. INavigationManager $navigationManager,
  98. IAppManager $appManager,
  99. CategoryFetcher $categoryFetcher,
  100. AppFetcher $appFetcher,
  101. IFactory $l10nFactory,
  102. BundleFetcher $bundleFetcher,
  103. Installer $installer,
  104. IURLGenerator $urlGenerator) {
  105. parent::__construct($appName, $request);
  106. $this->l10n = $l10n;
  107. $this->config = $config;
  108. $this->navigationManager = $navigationManager;
  109. $this->appManager = $appManager;
  110. $this->categoryFetcher = $categoryFetcher;
  111. $this->appFetcher = $appFetcher;
  112. $this->l10nFactory = $l10nFactory;
  113. $this->bundleFetcher = $bundleFetcher;
  114. $this->installer = $installer;
  115. $this->urlGenerator = $urlGenerator;
  116. }
  117. /**
  118. * @NoCSRFRequired
  119. *
  120. * @param string $category
  121. * @return TemplateResponse
  122. */
  123. public function viewApps($category = '') {
  124. if ($category === '') {
  125. $category = 'installed';
  126. }
  127. $params = [];
  128. $params['category'] = $category;
  129. $params['appstoreEnabled'] = $this->config->getSystemValue('appstoreenabled', true) === true;
  130. $params['urlGenerator'] = $this->urlGenerator;
  131. $this->navigationManager->setActiveEntry('core_apps');
  132. $templateResponse = new TemplateResponse($this->appName, 'apps', $params, 'user');
  133. $policy = new ContentSecurityPolicy();
  134. $policy->addAllowedImageDomain('https://usercontent.apps.nextcloud.com');
  135. $templateResponse->setContentSecurityPolicy($policy);
  136. return $templateResponse;
  137. }
  138. private function getAllCategories() {
  139. $currentLanguage = substr($this->l10nFactory->findLanguage(), 0, 2);
  140. $updateCount = count($this->getAppsWithUpdates());
  141. $formattedCategories = [
  142. ['id' => self::CAT_ALL_INSTALLED, 'ident' => 'installed', 'displayName' => (string)$this->l10n->t('Your apps')],
  143. ['id' => self::CAT_UPDATES, 'ident' => 'updates', 'displayName' => (string)$this->l10n->t('Updates'), 'counter' => $updateCount],
  144. ['id' => self::CAT_ENABLED, 'ident' => 'enabled', 'displayName' => (string)$this->l10n->t('Enabled apps')],
  145. ['id' => self::CAT_DISABLED, 'ident' => 'disabled', 'displayName' => (string)$this->l10n->t('Disabled apps')],
  146. ['id' => self::CAT_APP_BUNDLES, 'ident' => 'app-bundles', 'displayName' => (string)$this->l10n->t('App bundles')],
  147. ];
  148. $categories = $this->categoryFetcher->get();
  149. foreach($categories as $category) {
  150. $formattedCategories[] = [
  151. 'id' => $category['id'],
  152. 'ident' => $category['id'],
  153. 'displayName' => isset($category['translations'][$currentLanguage]['name']) ? $category['translations'][$currentLanguage]['name'] : $category['translations']['en']['name'],
  154. ];
  155. }
  156. return $formattedCategories;
  157. }
  158. /**
  159. * Get all available categories
  160. *
  161. * @return JSONResponse
  162. */
  163. public function listCategories() {
  164. return new JSONResponse($this->getAllCategories());
  165. }
  166. /**
  167. * Get all apps for a category
  168. *
  169. * @param string $requestedCategory
  170. * @return array
  171. */
  172. private function getAppsForCategory($requestedCategory) {
  173. $versionParser = new VersionParser();
  174. $formattedApps = [];
  175. $apps = $this->appFetcher->get();
  176. foreach($apps as $app) {
  177. if (isset($app['isFeatured'])) {
  178. $app['featured'] = $app['isFeatured'];
  179. }
  180. // Skip all apps not in the requested category
  181. $isInCategory = false;
  182. foreach($app['categories'] as $category) {
  183. if($category === $requestedCategory) {
  184. $isInCategory = true;
  185. }
  186. }
  187. if(!$isInCategory) {
  188. continue;
  189. }
  190. $nextCloudVersion = $versionParser->getVersion($app['releases'][0]['rawPlatformVersionSpec']);
  191. $nextCloudVersionDependencies = [];
  192. if($nextCloudVersion->getMinimumVersion() !== '') {
  193. $nextCloudVersionDependencies['nextcloud']['@attributes']['min-version'] = $nextCloudVersion->getMinimumVersion();
  194. }
  195. if($nextCloudVersion->getMaximumVersion() !== '') {
  196. $nextCloudVersionDependencies['nextcloud']['@attributes']['max-version'] = $nextCloudVersion->getMaximumVersion();
  197. }
  198. $phpVersion = $versionParser->getVersion($app['releases'][0]['rawPhpVersionSpec']);
  199. $existsLocally = \OC_App::getAppPath($app['id']) !== false;
  200. $phpDependencies = [];
  201. if($phpVersion->getMinimumVersion() !== '') {
  202. $phpDependencies['php']['@attributes']['min-version'] = $phpVersion->getMinimumVersion();
  203. }
  204. if($phpVersion->getMaximumVersion() !== '') {
  205. $phpDependencies['php']['@attributes']['max-version'] = $phpVersion->getMaximumVersion();
  206. }
  207. if(isset($app['releases'][0]['minIntSize'])) {
  208. $phpDependencies['php']['@attributes']['min-int-size'] = $app['releases'][0]['minIntSize'];
  209. }
  210. $authors = '';
  211. foreach($app['authors'] as $key => $author) {
  212. $authors .= $author['name'];
  213. if($key !== count($app['authors']) - 1) {
  214. $authors .= ', ';
  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. $currentVersion = '';
  224. if($this->appManager->isInstalled($app['id'])) {
  225. $currentVersion = \OC_App::getAppVersion($app['id']);
  226. } else {
  227. $currentLanguage = $app['releases'][0]['version'];
  228. }
  229. $formattedApps[] = [
  230. 'id' => $app['id'],
  231. 'name' => isset($app['translations'][$currentLanguage]['name']) ? $app['translations'][$currentLanguage]['name'] : $app['translations']['en']['name'],
  232. 'description' => isset($app['translations'][$currentLanguage]['description']) ? $app['translations'][$currentLanguage]['description'] : $app['translations']['en']['description'],
  233. 'license' => $app['releases'][0]['licenses'],
  234. 'author' => $authors,
  235. 'shipped' => false,
  236. 'version' => $currentVersion,
  237. 'default_enable' => '',
  238. 'types' => [],
  239. 'documentation' => [
  240. 'admin' => $app['adminDocs'],
  241. 'user' => $app['userDocs'],
  242. 'developer' => $app['developerDocs']
  243. ],
  244. 'website' => $app['website'],
  245. 'bugs' => $app['issueTracker'],
  246. 'detailpage' => $app['website'],
  247. 'dependencies' => array_merge(
  248. $nextCloudVersionDependencies,
  249. $phpDependencies
  250. ),
  251. 'level' => ($app['featured'] === true) ? 200 : 100,
  252. 'missingMaxOwnCloudVersion' => false,
  253. 'missingMinOwnCloudVersion' => false,
  254. 'canInstall' => true,
  255. 'preview' => isset($app['screenshots'][0]['url']) ? 'https://usercontent.apps.nextcloud.com/'.base64_encode($app['screenshots'][0]['url']) : '',
  256. 'score' => $app['ratingOverall'],
  257. 'ratingNumOverall' => $app['ratingNumOverall'],
  258. 'ratingNumThresholdReached' => $app['ratingNumOverall'] > 5 ? true : false,
  259. 'removable' => $existsLocally,
  260. 'active' => $this->appManager->isEnabledForUser($app['id']),
  261. 'needsDownload' => !$existsLocally,
  262. 'groups' => $groups,
  263. 'fromAppStore' => true,
  264. ];
  265. $newVersion = $this->installer->isUpdateAvailable($app['id']);
  266. if($newVersion && $this->appManager->isInstalled($app['id'])) {
  267. $formattedApps[count($formattedApps)-1]['update'] = $newVersion;
  268. }
  269. }
  270. return $formattedApps;
  271. }
  272. private function getAppsWithUpdates() {
  273. $appClass = new \OC_App();
  274. $apps = $appClass->listAllApps();
  275. foreach($apps as $key => $app) {
  276. $newVersion = $this->installer->isUpdateAvailable($app['id']);
  277. if($newVersion !== false) {
  278. $apps[$key]['update'] = $newVersion;
  279. } else {
  280. unset($apps[$key]);
  281. }
  282. }
  283. usort($apps, function ($a, $b) {
  284. $a = (string)$a['name'];
  285. $b = (string)$b['name'];
  286. if ($a === $b) {
  287. return 0;
  288. }
  289. return ($a < $b) ? -1 : 1;
  290. });
  291. return $apps;
  292. }
  293. /**
  294. * Get all available apps in a category
  295. *
  296. * @param string $category
  297. * @return JSONResponse
  298. */
  299. public function listApps($category = '') {
  300. $appClass = new \OC_App();
  301. switch ($category) {
  302. // installed apps
  303. case 'installed':
  304. $apps = $appClass->listAllApps();
  305. foreach($apps as $key => $app) {
  306. $newVersion = $this->installer->isUpdateAvailable($app['id']);
  307. $apps[$key]['update'] = $newVersion;
  308. }
  309. usort($apps, function ($a, $b) {
  310. $a = (string)$a['name'];
  311. $b = (string)$b['name'];
  312. if ($a === $b) {
  313. return 0;
  314. }
  315. return ($a < $b) ? -1 : 1;
  316. });
  317. break;
  318. // updates
  319. case 'updates':
  320. $apps = $this->getAppsWithUpdates();
  321. break;
  322. // enabled apps
  323. case 'enabled':
  324. $apps = $appClass->listAllApps();
  325. $apps = array_filter($apps, function ($app) {
  326. return $app['active'];
  327. });
  328. foreach($apps as $key => $app) {
  329. $newVersion = $this->installer->isUpdateAvailable($app['id']);
  330. $apps[$key]['update'] = $newVersion;
  331. }
  332. usort($apps, function ($a, $b) {
  333. $a = (string)$a['name'];
  334. $b = (string)$b['name'];
  335. if ($a === $b) {
  336. return 0;
  337. }
  338. return ($a < $b) ? -1 : 1;
  339. });
  340. break;
  341. // disabled apps
  342. case 'disabled':
  343. $apps = $appClass->listAllApps();
  344. $apps = array_filter($apps, function ($app) {
  345. return !$app['active'];
  346. });
  347. $apps = array_map(function ($app) {
  348. $newVersion = $this->installer->isUpdateAvailable($app['id']);
  349. if ($newVersion !== false) {
  350. $app['update'] = $newVersion;
  351. }
  352. return $app;
  353. }, $apps);
  354. usort($apps, function ($a, $b) {
  355. $a = (string)$a['name'];
  356. $b = (string)$b['name'];
  357. if ($a === $b) {
  358. return 0;
  359. }
  360. return ($a < $b) ? -1 : 1;
  361. });
  362. break;
  363. case 'app-bundles':
  364. $bundles = $this->bundleFetcher->getBundles();
  365. $apps = [];
  366. foreach($bundles as $bundle) {
  367. $newCategory = true;
  368. $allApps = $appClass->listAllApps();
  369. $categories = $this->getAllCategories();
  370. foreach($categories as $singleCategory) {
  371. $newApps = $this->getAppsForCategory($singleCategory['id']);
  372. foreach($allApps as $app) {
  373. foreach($newApps as $key => $newApp) {
  374. if($app['id'] === $newApp['id']) {
  375. unset($newApps[$key]);
  376. }
  377. }
  378. }
  379. $allApps = array_merge($allApps, $newApps);
  380. }
  381. foreach($bundle->getAppIdentifiers() as $identifier) {
  382. foreach($allApps as $app) {
  383. if($app['id'] === $identifier) {
  384. if($newCategory) {
  385. $app['newCategory'] = true;
  386. $app['categoryName'] = $bundle->getName();
  387. }
  388. $app['bundleId'] = $bundle->getIdentifier();
  389. $newCategory = false;
  390. $apps[] = $app;
  391. continue;
  392. }
  393. }
  394. }
  395. }
  396. break;
  397. default:
  398. $apps = $this->getAppsForCategory($category);
  399. // sort by score
  400. usort($apps, function ($a, $b) {
  401. $a = (int)$a['score'];
  402. $b = (int)$b['score'];
  403. if ($a === $b) {
  404. return 0;
  405. }
  406. return ($a > $b) ? -1 : 1;
  407. });
  408. break;
  409. }
  410. // fix groups to be an array
  411. $dependencyAnalyzer = new DependencyAnalyzer(new Platform($this->config), $this->l10n);
  412. $apps = array_map(function($app) use ($dependencyAnalyzer) {
  413. // fix groups
  414. $groups = array();
  415. if (is_string($app['groups'])) {
  416. $groups = json_decode($app['groups']);
  417. }
  418. $app['groups'] = $groups;
  419. $app['canUnInstall'] = !$app['active'] && $app['removable'];
  420. // fix licence vs license
  421. if (isset($app['license']) && !isset($app['licence'])) {
  422. $app['licence'] = $app['license'];
  423. }
  424. // analyse dependencies
  425. $missing = $dependencyAnalyzer->analyze($app);
  426. $app['canInstall'] = empty($missing);
  427. $app['missingDependencies'] = $missing;
  428. $app['missingMinOwnCloudVersion'] = !isset($app['dependencies']['nextcloud']['@attributes']['min-version']);
  429. $app['missingMaxOwnCloudVersion'] = !isset($app['dependencies']['nextcloud']['@attributes']['max-version']);
  430. return $app;
  431. }, $apps);
  432. return new JSONResponse(['apps' => $apps, 'status' => 'success']);
  433. }
  434. }