You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

AppSettingsController.php 10KB

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