AppSettingsController.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Christoph Wurst <christoph@owncloud.com>
  6. * @author Joas Schilling <coding@schilljs.com>
  7. * @author Lukas Reschke <lukas@statuscode.ch>
  8. * @author Morris Jobke <hey@morrisjobke.de>
  9. * @author Thomas Müller <thomas.mueller@tmit.eu>
  10. *
  11. * @license AGPL-3.0
  12. *
  13. * This code is free software: you can redistribute it and/or modify
  14. * it under the terms of the GNU Affero General Public License, version 3,
  15. * as published by the Free Software Foundation.
  16. *
  17. * This program is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. * GNU Affero General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU Affero General Public License, version 3,
  23. * along with this program. If not, see <http://www.gnu.org/licenses/>
  24. *
  25. */
  26. namespace OC\Settings\Controller;
  27. use OC\App\DependencyAnalyzer;
  28. use OC\App\Platform;
  29. use OC\OCSClient;
  30. use OCP\App\IAppManager;
  31. use \OCP\AppFramework\Controller;
  32. use OCP\AppFramework\Http\ContentSecurityPolicy;
  33. use OCP\AppFramework\Http\DataResponse;
  34. use OCP\AppFramework\Http\TemplateResponse;
  35. use OCP\ICacheFactory;
  36. use OCP\INavigationManager;
  37. use OCP\IRequest;
  38. use OCP\IL10N;
  39. use OCP\IConfig;
  40. /**
  41. * @package OC\Settings\Controller
  42. */
  43. class AppSettingsController extends Controller {
  44. const CAT_ENABLED = 0;
  45. const CAT_DISABLED = 1;
  46. /** @var \OCP\IL10N */
  47. private $l10n;
  48. /** @var IConfig */
  49. private $config;
  50. /** @var \OCP\ICache */
  51. private $cache;
  52. /** @var INavigationManager */
  53. private $navigationManager;
  54. /** @var IAppManager */
  55. private $appManager;
  56. /** @var OCSClient */
  57. private $ocsClient;
  58. /**
  59. * @param string $appName
  60. * @param IRequest $request
  61. * @param IL10N $l10n
  62. * @param IConfig $config
  63. * @param ICacheFactory $cache
  64. * @param INavigationManager $navigationManager
  65. * @param IAppManager $appManager
  66. * @param OCSClient $ocsClient
  67. */
  68. public function __construct($appName,
  69. IRequest $request,
  70. IL10N $l10n,
  71. IConfig $config,
  72. ICacheFactory $cache,
  73. INavigationManager $navigationManager,
  74. IAppManager $appManager,
  75. OCSClient $ocsClient) {
  76. parent::__construct($appName, $request);
  77. $this->l10n = $l10n;
  78. $this->config = $config;
  79. $this->cache = $cache->create($appName);
  80. $this->navigationManager = $navigationManager;
  81. $this->appManager = $appManager;
  82. $this->ocsClient = $ocsClient;
  83. }
  84. /**
  85. * Enables or disables the display of experimental apps
  86. * @param bool $state
  87. * @return DataResponse
  88. */
  89. public function changeExperimentalConfigState($state) {
  90. $this->config->setSystemValue('appstore.experimental.enabled', $state);
  91. $this->appManager->clearAppsCache();
  92. return new DataResponse();
  93. }
  94. /**
  95. * @param string|int $category
  96. * @return int
  97. */
  98. protected function getCategory($category) {
  99. if (is_string($category)) {
  100. foreach ($this->listCategories() as $cat) {
  101. if (isset($cat['ident']) && $cat['ident'] === $category) {
  102. $category = (int) $cat['id'];
  103. break;
  104. }
  105. }
  106. // Didn't find the category, falling back to enabled
  107. if (is_string($category)) {
  108. $category = self::CAT_ENABLED;
  109. }
  110. }
  111. return (int) $category;
  112. }
  113. /**
  114. * @NoCSRFRequired
  115. * @param string $category
  116. * @return TemplateResponse
  117. */
  118. public function viewApps($category = '') {
  119. $categoryId = $this->getCategory($category);
  120. if ($categoryId === self::CAT_ENABLED) {
  121. // Do not use an arbitrary input string, because we put the category in html
  122. $category = 'enabled';
  123. }
  124. $params = [];
  125. $params['experimentalEnabled'] = $this->config->getSystemValue('appstore.experimental.enabled', false);
  126. $params['category'] = $category;
  127. $params['appstoreEnabled'] = $this->config->getSystemValue('appstoreenabled', true) === true;
  128. $this->navigationManager->setActiveEntry('core_apps');
  129. $templateResponse = new TemplateResponse($this->appName, 'apps', $params, 'user');
  130. $policy = new ContentSecurityPolicy();
  131. $policy->addAllowedImageDomain('https://apps.owncloud.com');
  132. $templateResponse->setContentSecurityPolicy($policy);
  133. return $templateResponse;
  134. }
  135. /**
  136. * Get all available categories
  137. * @return array
  138. */
  139. public function listCategories() {
  140. if(!is_null($this->cache->get('listCategories'))) {
  141. return $this->cache->get('listCategories');
  142. }
  143. $categories = [
  144. ['id' => self::CAT_ENABLED, 'ident' => 'enabled', 'displayName' => (string)$this->l10n->t('Enabled')],
  145. ['id' => self::CAT_DISABLED, 'ident' => 'disabled', 'displayName' => (string)$this->l10n->t('Not enabled')],
  146. ];
  147. if($this->ocsClient->isAppStoreEnabled()) {
  148. // apps from external repo via OCS
  149. $ocs = $this->ocsClient->getCategories(\OCP\Util::getVersion());
  150. if ($ocs) {
  151. foreach($ocs as $k => $v) {
  152. $name = str_replace('ownCloud ', '', $v);
  153. $ident = str_replace(' ', '-', urlencode(strtolower($name)));
  154. $categories[] = [
  155. 'id' => $k,
  156. 'ident' => $ident,
  157. 'displayName' => $name,
  158. ];
  159. }
  160. }
  161. }
  162. $this->cache->set('listCategories', $categories, 3600);
  163. return $categories;
  164. }
  165. /**
  166. * Get all available apps in a category
  167. *
  168. * @param string $category
  169. * @param bool $includeUpdateInfo Should we check whether there is an update
  170. * in the app store?
  171. * @return array
  172. */
  173. public function listApps($category = '', $includeUpdateInfo = true) {
  174. $category = $this->getCategory($category);
  175. $cacheName = 'listApps-' . $category . '-' . (int) $includeUpdateInfo;
  176. if(!is_null($this->cache->get($cacheName))) {
  177. $apps = $this->cache->get($cacheName);
  178. } else {
  179. switch ($category) {
  180. // installed apps
  181. case 0:
  182. $apps = $this->getInstalledApps($includeUpdateInfo);
  183. usort($apps, function ($a, $b) {
  184. $a = (string)$a['name'];
  185. $b = (string)$b['name'];
  186. if ($a === $b) {
  187. return 0;
  188. }
  189. return ($a < $b) ? -1 : 1;
  190. });
  191. $version = \OCP\Util::getVersion();
  192. foreach($apps as $key => $app) {
  193. if(!array_key_exists('level', $app) && array_key_exists('ocsid', $app)) {
  194. $remoteAppEntry = $this->ocsClient->getApplication($app['ocsid'], $version);
  195. if(is_array($remoteAppEntry) && array_key_exists('level', $remoteAppEntry)) {
  196. $apps[$key]['level'] = $remoteAppEntry['level'];
  197. }
  198. }
  199. }
  200. break;
  201. // not-installed apps
  202. case 1:
  203. $apps = \OC_App::listAllApps(true, $includeUpdateInfo, $this->ocsClient);
  204. $apps = array_filter($apps, function ($app) {
  205. return !$app['active'];
  206. });
  207. $version = \OCP\Util::getVersion();
  208. foreach($apps as $key => $app) {
  209. if(!array_key_exists('level', $app) && array_key_exists('ocsid', $app)) {
  210. $remoteAppEntry = $this->ocsClient->getApplication($app['ocsid'], $version);
  211. if(is_array($remoteAppEntry) && array_key_exists('level', $remoteAppEntry)) {
  212. $apps[$key]['level'] = $remoteAppEntry['level'];
  213. }
  214. }
  215. }
  216. usort($apps, function ($a, $b) {
  217. $a = (string)$a['name'];
  218. $b = (string)$b['name'];
  219. if ($a === $b) {
  220. return 0;
  221. }
  222. return ($a < $b) ? -1 : 1;
  223. });
  224. break;
  225. default:
  226. $filter = $this->config->getSystemValue('appstore.experimental.enabled', false) ? 'all' : 'approved';
  227. $apps = \OC_App::getAppstoreApps($filter, $category, $this->ocsClient);
  228. if (!$apps) {
  229. $apps = array();
  230. } else {
  231. // don't list installed apps
  232. $installedApps = $this->getInstalledApps(false);
  233. $installedApps = array_map(function ($app) {
  234. if (isset($app['ocsid'])) {
  235. return $app['ocsid'];
  236. }
  237. return $app['id'];
  238. }, $installedApps);
  239. $apps = array_filter($apps, function ($app) use ($installedApps) {
  240. return !in_array($app['id'], $installedApps);
  241. });
  242. // show tooltip if app is downloaded from remote server
  243. $inactiveApps = $this->getInactiveApps();
  244. foreach ($apps as &$app) {
  245. $app['needsDownload'] = !in_array($app['id'], $inactiveApps);
  246. }
  247. }
  248. // sort by score
  249. usort($apps, function ($a, $b) {
  250. $a = (int)$a['score'];
  251. $b = (int)$b['score'];
  252. if ($a === $b) {
  253. return 0;
  254. }
  255. return ($a > $b) ? -1 : 1;
  256. });
  257. break;
  258. }
  259. }
  260. // fix groups to be an array
  261. $dependencyAnalyzer = new DependencyAnalyzer(new Platform($this->config), $this->l10n);
  262. $apps = array_map(function($app) use ($dependencyAnalyzer) {
  263. // fix groups
  264. $groups = array();
  265. if (is_string($app['groups'])) {
  266. $groups = json_decode($app['groups']);
  267. }
  268. $app['groups'] = $groups;
  269. $app['canUnInstall'] = !$app['active'] && $app['removable'];
  270. // fix licence vs license
  271. if (isset($app['license']) && !isset($app['licence'])) {
  272. $app['licence'] = $app['license'];
  273. }
  274. // analyse dependencies
  275. $missing = $dependencyAnalyzer->analyze($app);
  276. $app['canInstall'] = empty($missing);
  277. $app['missingDependencies'] = $missing;
  278. $app['missingMinOwnCloudVersion'] = !isset($app['dependencies']['owncloud']['@attributes']['min-version']);
  279. $app['missingMaxOwnCloudVersion'] = !isset($app['dependencies']['owncloud']['@attributes']['max-version']);
  280. return $app;
  281. }, $apps);
  282. $this->cache->set($cacheName, $apps, 300);
  283. return ['apps' => $apps, 'status' => 'success'];
  284. }
  285. /**
  286. * @param bool $includeUpdateInfo Should we check whether there is an update
  287. * in the app store?
  288. * @return array
  289. */
  290. private function getInstalledApps($includeUpdateInfo = true) {
  291. $apps = \OC_App::listAllApps(true, $includeUpdateInfo, $this->ocsClient);
  292. $apps = array_filter($apps, function ($app) {
  293. return $app['active'];
  294. });
  295. return $apps;
  296. }
  297. /**
  298. * @return array
  299. */
  300. private function getInactiveApps() {
  301. $inactiveApps = \OC_App::listAllApps(true, false, $this->ocsClient);
  302. $inactiveApps = array_filter($inactiveApps,
  303. function ($app) {
  304. return !$app['active'];
  305. });
  306. $inactiveApps = array_map(function($app) {
  307. if (isset($app['ocsid'])) {
  308. return $app['ocsid'];
  309. }
  310. return $app['id'];
  311. }, $inactiveApps);
  312. return $inactiveApps;
  313. }
  314. }