2014-10-15 19:59:16 +00:00
|
|
|
<?php
|
|
|
|
/**
|
2015-03-26 10:44:34 +00:00
|
|
|
* @author Joas Schilling <nickvergessen@owncloud.com>
|
|
|
|
* @author Lukas Reschke <lukas@owncloud.com>
|
|
|
|
* @author Morris Jobke <hey@morrisjobke.de>
|
|
|
|
* @author Thomas Müller <thomas.mueller@tmit.eu>
|
|
|
|
*
|
|
|
|
* @copyright Copyright (c) 2015, ownCloud, Inc.
|
|
|
|
* @license AGPL-3.0
|
|
|
|
*
|
|
|
|
* This code is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Affero General Public License, version 3,
|
|
|
|
* as published by the Free Software Foundation.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU Affero General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Affero General Public License, version 3,
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>
|
2014-10-15 19:59:16 +00:00
|
|
|
*
|
|
|
|
*/
|
2015-02-26 10:37:37 +00:00
|
|
|
|
2014-10-15 19:59:16 +00:00
|
|
|
namespace OC\Settings\Controller;
|
|
|
|
|
2014-12-01 20:47:22 +00:00
|
|
|
use OC\App\DependencyAnalyzer;
|
|
|
|
use OC\App\Platform;
|
2015-02-09 21:48:27 +00:00
|
|
|
use OC\OCSClient;
|
2015-03-30 13:58:20 +00:00
|
|
|
use OCP\App\IAppManager;
|
2014-10-15 19:59:16 +00:00
|
|
|
use \OCP\AppFramework\Controller;
|
2015-03-30 13:58:20 +00:00
|
|
|
use OCP\AppFramework\Http\ContentSecurityPolicy;
|
|
|
|
use OCP\AppFramework\Http\DataResponse;
|
|
|
|
use OCP\AppFramework\Http\TemplateResponse;
|
2015-01-09 17:31:39 +00:00
|
|
|
use OCP\ICacheFactory;
|
2015-03-30 13:58:20 +00:00
|
|
|
use OCP\INavigationManager;
|
2014-10-15 19:59:16 +00:00
|
|
|
use OCP\IRequest;
|
|
|
|
use OCP\IL10N;
|
|
|
|
use OCP\IConfig;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @package OC\Settings\Controller
|
|
|
|
*/
|
|
|
|
class AppSettingsController extends Controller {
|
|
|
|
|
|
|
|
/** @var \OCP\IL10N */
|
|
|
|
private $l10n;
|
|
|
|
/** @var IConfig */
|
|
|
|
private $config;
|
2015-01-09 17:31:39 +00:00
|
|
|
/** @var \OCP\ICache */
|
|
|
|
private $cache;
|
2015-03-30 13:58:20 +00:00
|
|
|
/** @var INavigationManager */
|
|
|
|
private $navigationManager;
|
|
|
|
/** @var IAppManager */
|
|
|
|
private $appManager;
|
|
|
|
/** @var OCSClient */
|
|
|
|
private $ocsClient;
|
2014-10-15 19:59:16 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string $appName
|
|
|
|
* @param IRequest $request
|
|
|
|
* @param IL10N $l10n
|
|
|
|
* @param IConfig $config
|
2015-01-09 17:31:39 +00:00
|
|
|
* @param ICacheFactory $cache
|
2015-03-30 13:58:20 +00:00
|
|
|
* @param INavigationManager $navigationManager
|
|
|
|
* @param IAppManager $appManager
|
|
|
|
* @param OCSClient $ocsClient
|
2014-10-15 19:59:16 +00:00
|
|
|
*/
|
|
|
|
public function __construct($appName,
|
|
|
|
IRequest $request,
|
|
|
|
IL10N $l10n,
|
2015-01-09 17:31:39 +00:00
|
|
|
IConfig $config,
|
2015-03-30 13:58:20 +00:00
|
|
|
ICacheFactory $cache,
|
|
|
|
INavigationManager $navigationManager,
|
|
|
|
IAppManager $appManager,
|
|
|
|
OCSClient $ocsClient) {
|
2014-10-15 19:59:16 +00:00
|
|
|
parent::__construct($appName, $request);
|
|
|
|
$this->l10n = $l10n;
|
|
|
|
$this->config = $config;
|
2015-01-09 17:31:39 +00:00
|
|
|
$this->cache = $cache->create($appName);
|
2015-03-30 13:58:20 +00:00
|
|
|
$this->navigationManager = $navigationManager;
|
|
|
|
$this->appManager = $appManager;
|
|
|
|
$this->ocsClient = $ocsClient;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Enables or disables the display of experimental apps
|
|
|
|
* @param bool $state
|
|
|
|
* @return DataResponse
|
|
|
|
*/
|
|
|
|
public function changeExperimentalConfigState($state) {
|
|
|
|
$this->config->setSystemValue('appstore.experimental.enabled', $state);
|
|
|
|
$this->appManager->clearAppsCache();
|
|
|
|
return new DataResponse();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @NoCSRFRequired
|
|
|
|
* @return TemplateResponse
|
|
|
|
*/
|
|
|
|
public function viewApps() {
|
|
|
|
$params = [];
|
|
|
|
$params['experimentalEnabled'] = $this->config->getSystemValue('appstore.experimental.enabled', false);
|
|
|
|
$this->navigationManager->setActiveEntry('core_apps');
|
|
|
|
|
|
|
|
$templateResponse = new TemplateResponse($this->appName, 'apps', $params, 'user');
|
|
|
|
$policy = new ContentSecurityPolicy();
|
|
|
|
$policy->addAllowedImageDomain('https://apps.owncloud.com');
|
|
|
|
$templateResponse->setContentSecurityPolicy($policy);
|
|
|
|
|
|
|
|
return $templateResponse;
|
2014-10-15 19:59:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get all available categories
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function listCategories() {
|
|
|
|
|
2015-01-09 17:31:39 +00:00
|
|
|
if(!is_null($this->cache->get('listCategories'))) {
|
|
|
|
return $this->cache->get('listCategories');
|
|
|
|
}
|
|
|
|
$categories = [
|
|
|
|
['id' => 0, 'displayName' => (string)$this->l10n->t('Enabled')],
|
|
|
|
['id' => 1, 'displayName' => (string)$this->l10n->t('Not enabled')],
|
|
|
|
];
|
2014-10-15 19:59:16 +00:00
|
|
|
|
2015-03-30 13:58:20 +00:00
|
|
|
if($this->ocsClient->isAppStoreEnabled()) {
|
2014-10-15 19:59:16 +00:00
|
|
|
// apps from external repo via OCS
|
2015-06-29 09:15:56 +00:00
|
|
|
$ocs = $this->ocsClient->getCategories(\OC_Util::getVersion());
|
2015-01-30 15:39:57 +00:00
|
|
|
if ($ocs) {
|
|
|
|
foreach($ocs as $k => $v) {
|
2015-03-30 13:58:20 +00:00
|
|
|
$categories[] = [
|
2015-01-30 15:39:57 +00:00
|
|
|
'id' => $k,
|
|
|
|
'displayName' => str_replace('ownCloud ', '', $v)
|
2015-03-30 13:58:20 +00:00
|
|
|
];
|
2015-01-30 15:39:57 +00:00
|
|
|
}
|
2014-10-15 19:59:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-09 17:31:39 +00:00
|
|
|
$this->cache->set('listCategories', $categories, 3600);
|
2014-10-15 19:59:16 +00:00
|
|
|
|
|
|
|
return $categories;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-03-30 13:58:20 +00:00
|
|
|
* Get all available apps in a category
|
|
|
|
*
|
2014-10-15 19:59:16 +00:00
|
|
|
* @param int $category
|
2015-06-10 10:01:54 +00:00
|
|
|
* @param bool $includeUpdateInfo Should we check whether there is an update
|
|
|
|
* in the app store?
|
2014-10-15 19:59:16 +00:00
|
|
|
* @return array
|
|
|
|
*/
|
2015-06-10 10:01:54 +00:00
|
|
|
public function listApps($category = 0, $includeUpdateInfo = true) {
|
2015-06-15 08:04:13 +00:00
|
|
|
$cacheName = 'listApps-' . $category . '-' . (int) $includeUpdateInfo;
|
|
|
|
|
|
|
|
if(!is_null($this->cache->get($cacheName))) {
|
|
|
|
$apps = $this->cache->get($cacheName);
|
2015-01-09 17:31:39 +00:00
|
|
|
} else {
|
|
|
|
switch ($category) {
|
|
|
|
// installed apps
|
|
|
|
case 0:
|
2015-06-10 10:01:54 +00:00
|
|
|
$apps = $this->getInstalledApps($includeUpdateInfo);
|
2015-01-16 11:07:08 +00:00
|
|
|
usort($apps, function ($a, $b) {
|
|
|
|
$a = (string)$a['name'];
|
|
|
|
$b = (string)$b['name'];
|
|
|
|
if ($a === $b) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return ($a < $b) ? -1 : 1;
|
|
|
|
});
|
2015-06-29 09:38:28 +00:00
|
|
|
foreach($apps as $key => $app) {
|
|
|
|
if(!array_key_exists('level', $app) && array_key_exists('ocsid', $app)) {
|
2015-06-29 13:09:57 +00:00
|
|
|
$remoteAppEntry = $this->ocsClient->getApplication($app['ocsid'], \OC_Util::getVersion());
|
2015-06-29 09:38:28 +00:00
|
|
|
|
|
|
|
if(array_key_exists('level', $remoteAppEntry)) {
|
|
|
|
$apps[$key]['level'] = $remoteAppEntry['level'];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-01-09 17:31:39 +00:00
|
|
|
break;
|
|
|
|
// not-installed apps
|
|
|
|
case 1:
|
2015-06-10 10:01:54 +00:00
|
|
|
$apps = \OC_App::listAllApps(true, $includeUpdateInfo);
|
2015-01-09 17:31:39 +00:00
|
|
|
$apps = array_filter($apps, function ($app) {
|
|
|
|
return !$app['active'];
|
2014-10-15 19:59:16 +00:00
|
|
|
});
|
2015-06-19 17:01:58 +00:00
|
|
|
foreach($apps as $key => $app) {
|
|
|
|
if(!array_key_exists('level', $app) && array_key_exists('ocsid', $app)) {
|
2015-06-29 13:09:57 +00:00
|
|
|
$remoteAppEntry = $this->ocsClient->getApplication($app['ocsid'], \OC_Util::getVersion());
|
2015-06-19 17:01:58 +00:00
|
|
|
|
|
|
|
if(array_key_exists('level', $remoteAppEntry)) {
|
|
|
|
$apps[$key]['level'] = $remoteAppEntry['level'];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-01-16 11:07:08 +00:00
|
|
|
usort($apps, function ($a, $b) {
|
|
|
|
$a = (string)$a['name'];
|
|
|
|
$b = (string)$b['name'];
|
|
|
|
if ($a === $b) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return ($a < $b) ? -1 : 1;
|
|
|
|
});
|
2015-01-09 17:31:39 +00:00
|
|
|
break;
|
|
|
|
default:
|
2015-03-30 13:58:20 +00:00
|
|
|
$filter = $this->config->getSystemValue('appstore.experimental.enabled', false) ? 'all' : 'approved';
|
|
|
|
|
|
|
|
$apps = \OC_App::getAppstoreApps($filter, $category);
|
2015-01-09 17:31:39 +00:00
|
|
|
if (!$apps) {
|
|
|
|
$apps = array();
|
2015-02-09 21:48:27 +00:00
|
|
|
} else {
|
|
|
|
// don't list installed apps
|
2015-06-10 10:01:54 +00:00
|
|
|
$installedApps = $this->getInstalledApps(false);
|
2015-02-09 21:48:27 +00:00
|
|
|
$installedApps = array_map(function ($app) {
|
|
|
|
if (isset($app['ocsid'])) {
|
|
|
|
return $app['ocsid'];
|
|
|
|
}
|
|
|
|
return $app['id'];
|
|
|
|
}, $installedApps);
|
|
|
|
$apps = array_filter($apps, function ($app) use ($installedApps) {
|
|
|
|
return !in_array($app['id'], $installedApps);
|
|
|
|
});
|
2014-10-15 19:59:16 +00:00
|
|
|
}
|
2015-02-09 21:48:27 +00:00
|
|
|
|
|
|
|
// sort by score
|
2015-01-09 17:31:39 +00:00
|
|
|
usort($apps, function ($a, $b) {
|
|
|
|
$a = (int)$a['score'];
|
|
|
|
$b = (int)$b['score'];
|
|
|
|
if ($a === $b) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return ($a > $b) ? -1 : 1;
|
|
|
|
});
|
|
|
|
break;
|
|
|
|
}
|
2014-10-15 19:59:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// fix groups to be an array
|
2014-12-12 11:34:53 +00:00
|
|
|
$dependencyAnalyzer = new DependencyAnalyzer(new Platform($this->config), $this->l10n);
|
|
|
|
$apps = array_map(function($app) use ($dependencyAnalyzer) {
|
2015-01-06 12:07:17 +00:00
|
|
|
|
|
|
|
// fix groups
|
2014-10-15 19:59:16 +00:00
|
|
|
$groups = array();
|
|
|
|
if (is_string($app['groups'])) {
|
|
|
|
$groups = json_decode($app['groups']);
|
|
|
|
}
|
|
|
|
$app['groups'] = $groups;
|
|
|
|
$app['canUnInstall'] = !$app['active'] && $app['removable'];
|
2014-12-01 20:47:22 +00:00
|
|
|
|
2015-01-06 12:07:17 +00:00
|
|
|
// fix licence vs license
|
|
|
|
if (isset($app['license']) && !isset($app['licence'])) {
|
|
|
|
$app['licence'] = $app['license'];
|
|
|
|
}
|
|
|
|
|
2014-12-01 20:47:22 +00:00
|
|
|
// analyse dependencies
|
2014-12-12 11:34:53 +00:00
|
|
|
$missing = $dependencyAnalyzer->analyze($app);
|
2014-12-01 20:47:22 +00:00
|
|
|
$app['canInstall'] = empty($missing);
|
|
|
|
$app['missingDependencies'] = $missing;
|
2015-01-06 12:07:17 +00:00
|
|
|
|
2014-10-15 19:59:16 +00:00
|
|
|
return $app;
|
|
|
|
}, $apps);
|
|
|
|
|
2015-06-15 08:04:13 +00:00
|
|
|
$this->cache->set($cacheName, $apps, 300);
|
2015-01-09 17:31:39 +00:00
|
|
|
|
|
|
|
return ['apps' => $apps, 'status' => 'success'];
|
2014-10-15 19:59:16 +00:00
|
|
|
}
|
2015-02-09 21:48:27 +00:00
|
|
|
|
|
|
|
/**
|
2015-06-10 10:01:54 +00:00
|
|
|
* @param bool $includeUpdateInfo Should we check whether there is an update
|
|
|
|
* in the app store?
|
2015-02-09 21:48:27 +00:00
|
|
|
* @return array
|
|
|
|
*/
|
2015-06-10 10:01:54 +00:00
|
|
|
private function getInstalledApps($includeUpdateInfo = true) {
|
|
|
|
$apps = \OC_App::listAllApps(true, $includeUpdateInfo);
|
2015-02-09 21:48:27 +00:00
|
|
|
$apps = array_filter($apps, function ($app) {
|
|
|
|
return $app['active'];
|
|
|
|
});
|
|
|
|
return $apps;
|
|
|
|
}
|
2014-10-15 19:59:16 +00:00
|
|
|
}
|