2011-04-16 16:36:32 +04:00
|
|
|
<?php
|
|
|
|
/**
|
2016-07-21 18:07:57 +03:00
|
|
|
* @copyright Copyright (c) 2016, ownCloud, Inc.
|
|
|
|
*
|
2015-03-26 13:44:34 +03:00
|
|
|
* @author Bart Visscher <bartv@thisnet.nl>
|
|
|
|
* @author Brice Maron <brice@bmaron.net>
|
|
|
|
* @author Felix Moeller <mail@felixmoeller.de>
|
2016-05-26 20:56:05 +03:00
|
|
|
* @author Frank Karlitschek <frank@karlitschek.de>
|
|
|
|
* @author Jarrett <JetUni@users.noreply.github.com>
|
2016-07-21 18:07:57 +03:00
|
|
|
* @author Joas Schilling <coding@schilljs.com>
|
2015-03-26 13:44:34 +03:00
|
|
|
* @author Jörn Friedrich Dreyer <jfd@butonic.de>
|
|
|
|
* @author Kamil Domanski <kdomanski@kdemail.net>
|
2016-05-26 20:56:05 +03:00
|
|
|
* @author Lukas Reschke <lukas@statuscode.ch>
|
2015-03-26 13:44:34 +03:00
|
|
|
* @author Morris Jobke <hey@morrisjobke.de>
|
2016-01-12 17:02:16 +03:00
|
|
|
* @author Robin McCorkell <robin@mccorkell.me.uk>
|
2015-03-26 13:44:34 +03:00
|
|
|
* @author Sam Tuke <mail@samtuke.com>
|
|
|
|
* @author Thomas Müller <thomas.mueller@tmit.eu>
|
2011-04-16 16:36:32 +04:00
|
|
|
*
|
2015-03-26 13:44:34 +03:00
|
|
|
* @license AGPL-3.0
|
2011-04-16 16:36:32 +04:00
|
|
|
*
|
2015-03-26 13:44:34 +03:00
|
|
|
* 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.
|
2011-04-16 16:36:32 +04:00
|
|
|
*
|
2015-03-26 13:44:34 +03:00
|
|
|
* This program is distributed in the hope that it will be useful,
|
2011-04-16 16:36:32 +04:00
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
2015-03-26 13:44:34 +03:00
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU Affero General Public License for more details.
|
2011-04-16 16:36:32 +04:00
|
|
|
*
|
2015-03-26 13:44:34 +03:00
|
|
|
* 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/>
|
2011-04-16 16:36:32 +04:00
|
|
|
*
|
|
|
|
*/
|
2015-02-10 00:48:27 +03:00
|
|
|
|
|
|
|
namespace OC;
|
|
|
|
|
2015-03-30 16:58:20 +03:00
|
|
|
use OCP\Http\Client\IClientService;
|
|
|
|
use OCP\IConfig;
|
|
|
|
use OCP\ILogger;
|
|
|
|
|
2015-02-10 00:48:27 +03:00
|
|
|
/**
|
2015-03-30 16:58:20 +03:00
|
|
|
* Class OCSClient is a class for communication with the ownCloud appstore
|
|
|
|
*
|
|
|
|
* @package OC
|
2015-02-10 00:48:27 +03:00
|
|
|
*/
|
|
|
|
class OCSClient {
|
2015-03-30 16:58:20 +03:00
|
|
|
/** @var IClientService */
|
|
|
|
private $httpClientService;
|
|
|
|
/** @var IConfig */
|
|
|
|
private $config;
|
|
|
|
/** @var ILogger */
|
|
|
|
private $logger;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param IClientService $httpClientService
|
|
|
|
* @param IConfig $config
|
|
|
|
* @param ILogger $logger
|
|
|
|
*/
|
|
|
|
public function __construct(IClientService $httpClientService,
|
|
|
|
IConfig $config,
|
|
|
|
ILogger $logger) {
|
|
|
|
$this->httpClientService = $httpClientService;
|
|
|
|
$this->config = $config;
|
|
|
|
$this->logger = $logger;
|
|
|
|
}
|
2011-04-16 16:36:32 +04:00
|
|
|
|
2014-08-26 12:20:51 +04:00
|
|
|
/**
|
|
|
|
* Returns whether the AppStore is enabled (i.e. because the AppStore is disabled for EE)
|
2015-02-10 00:48:27 +03:00
|
|
|
*
|
2014-08-26 12:20:51 +04:00
|
|
|
* @return bool
|
|
|
|
*/
|
2015-03-30 16:58:20 +03:00
|
|
|
public function isAppStoreEnabled() {
|
2016-09-06 15:11:30 +03:00
|
|
|
return $this->config->getSystemValue('appstoreenabled', true) === true;
|
2014-08-26 12:20:51 +04:00
|
|
|
}
|
|
|
|
|
2012-03-23 18:52:41 +04:00
|
|
|
/**
|
2014-05-19 19:50:53 +04:00
|
|
|
* Get the url of the OCS AppStore server.
|
2015-02-10 00:48:27 +03:00
|
|
|
*
|
2014-05-11 21:05:28 +04:00
|
|
|
* @return string of the AppStore server
|
2012-03-23 18:52:41 +04:00
|
|
|
*/
|
2015-03-30 16:58:20 +03:00
|
|
|
private function getAppStoreUrl() {
|
|
|
|
return $this->config->getSystemValue('appstoreurl', 'https://api.owncloud.com/v1');
|
2012-10-08 17:58:50 +04:00
|
|
|
}
|
|
|
|
|
2015-04-07 14:48:33 +03:00
|
|
|
/**
|
|
|
|
* @param string $body
|
|
|
|
* @param string $action
|
|
|
|
* @return null|\SimpleXMLElement
|
|
|
|
*/
|
|
|
|
private function loadData($body, $action) {
|
|
|
|
$loadEntities = libxml_disable_entity_loader(true);
|
|
|
|
$data = @simplexml_load_string($body);
|
|
|
|
libxml_disable_entity_loader($loadEntities);
|
|
|
|
|
|
|
|
if($data === false) {
|
2015-12-17 19:34:15 +03:00
|
|
|
libxml_clear_errors();
|
2015-04-07 14:48:33 +03:00
|
|
|
$this->logger->error(
|
|
|
|
sprintf('Could not get %s, content was no valid XML', $action),
|
|
|
|
[
|
|
|
|
'app' => 'core',
|
|
|
|
]
|
|
|
|
);
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $data;
|
|
|
|
}
|
|
|
|
|
2013-01-14 23:30:28 +04:00
|
|
|
/**
|
2014-05-19 19:50:53 +04:00
|
|
|
* Get all the categories from the OCS server
|
2015-02-10 00:48:27 +03:00
|
|
|
*
|
2015-06-29 12:15:56 +03:00
|
|
|
* @param array $targetVersion The target ownCloud version
|
2014-08-01 15:42:35 +04:00
|
|
|
* @return array|null an array of category ids or null
|
2012-09-18 19:51:55 +04:00
|
|
|
* @note returns NULL if config value appstoreenabled is set to false
|
2011-04-16 16:36:32 +04:00
|
|
|
* This function returns a list of all the application categories on the OCS server
|
|
|
|
*/
|
2015-06-29 16:10:40 +03:00
|
|
|
public function getCategories(array $targetVersion) {
|
2015-03-30 16:58:20 +03:00
|
|
|
if (!$this->isAppStoreEnabled()) {
|
2012-10-23 10:01:09 +04:00
|
|
|
return null;
|
2012-06-22 18:06:46 +04:00
|
|
|
}
|
2015-03-16 13:28:23 +03:00
|
|
|
|
2015-03-30 16:58:20 +03:00
|
|
|
$client = $this->httpClientService->newClient();
|
2015-03-16 13:28:23 +03:00
|
|
|
try {
|
2015-03-30 16:58:20 +03:00
|
|
|
$response = $client->get(
|
|
|
|
$this->getAppStoreUrl() . '/content/categories',
|
|
|
|
[
|
2016-05-05 23:20:46 +03:00
|
|
|
'timeout' => 20,
|
2015-06-29 12:15:56 +03:00
|
|
|
'query' => [
|
|
|
|
'version' => implode('x', $targetVersion),
|
|
|
|
],
|
2015-03-30 16:58:20 +03:00
|
|
|
]
|
|
|
|
);
|
2015-03-16 13:28:23 +03:00
|
|
|
} catch(\Exception $e) {
|
2015-03-30 16:58:20 +03:00
|
|
|
$this->logger->error(
|
|
|
|
sprintf('Could not get categories: %s', $e->getMessage()),
|
|
|
|
[
|
|
|
|
'app' => 'core',
|
|
|
|
]
|
|
|
|
);
|
2012-10-23 10:01:09 +04:00
|
|
|
return null;
|
2011-06-19 00:02:45 +04:00
|
|
|
}
|
2015-03-16 13:28:23 +03:00
|
|
|
|
2015-04-07 14:48:33 +03:00
|
|
|
$data = $this->loadData($response->getBody(), 'categories');
|
|
|
|
if($data === null) {
|
2015-03-30 16:58:20 +03:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2015-02-10 00:48:27 +03:00
|
|
|
$tmp = $data->data;
|
2015-03-16 13:28:23 +03:00
|
|
|
$cats = [];
|
2011-04-17 18:04:46 +04:00
|
|
|
|
2015-02-10 00:48:27 +03:00
|
|
|
foreach ($tmp->category as $value) {
|
|
|
|
$id = (int)$value->id;
|
|
|
|
$name = (string)$value->name;
|
|
|
|
$cats[$id] = $name;
|
2011-04-16 21:42:58 +04:00
|
|
|
}
|
2011-04-17 18:04:46 +04:00
|
|
|
|
2011-04-16 21:42:58 +04:00
|
|
|
return $cats;
|
2011-04-16 16:36:32 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2014-05-19 19:50:53 +04:00
|
|
|
* Get all the applications from the OCS server
|
2015-03-30 16:58:20 +03:00
|
|
|
* @param array $categories
|
2014-04-21 17:44:54 +04:00
|
|
|
* @param int $page
|
2014-02-06 19:30:58 +04:00
|
|
|
* @param string $filter
|
2015-06-29 12:15:56 +03:00
|
|
|
* @param array $targetVersion The target ownCloud version
|
2015-03-30 16:58:20 +03:00
|
|
|
* @return array An array of application data
|
2011-04-16 16:36:32 +04:00
|
|
|
*/
|
2015-06-29 16:10:40 +03:00
|
|
|
public function getApplications(array $categories, $page, $filter, array $targetVersion) {
|
2015-03-30 16:58:20 +03:00
|
|
|
if (!$this->isAppStoreEnabled()) {
|
|
|
|
return [];
|
2012-03-23 18:52:41 +04:00
|
|
|
}
|
|
|
|
|
2015-03-30 16:58:20 +03:00
|
|
|
$client = $this->httpClientService->newClient();
|
2015-03-16 13:28:23 +03:00
|
|
|
try {
|
2015-03-30 16:58:20 +03:00
|
|
|
$response = $client->get(
|
|
|
|
$this->getAppStoreUrl() . '/content/data',
|
|
|
|
[
|
2016-05-05 23:20:46 +03:00
|
|
|
'timeout' => 20,
|
2015-03-30 16:58:20 +03:00
|
|
|
'query' => [
|
2015-06-29 12:15:56 +03:00
|
|
|
'version' => implode('x', $targetVersion),
|
2015-03-30 16:58:20 +03:00
|
|
|
'filter' => $filter,
|
|
|
|
'categories' => implode('x', $categories),
|
|
|
|
'sortmode' => 'new',
|
|
|
|
'page' => $page,
|
|
|
|
'pagesize' => 100,
|
|
|
|
'approved' => $filter
|
|
|
|
],
|
|
|
|
]
|
|
|
|
);
|
2015-03-16 13:28:23 +03:00
|
|
|
} catch(\Exception $e) {
|
2015-03-30 16:58:20 +03:00
|
|
|
$this->logger->error(
|
|
|
|
sprintf('Could not get applications: %s', $e->getMessage()),
|
|
|
|
[
|
|
|
|
'app' => 'core',
|
|
|
|
]
|
|
|
|
);
|
|
|
|
return [];
|
2011-06-19 00:02:45 +04:00
|
|
|
}
|
2015-03-16 13:28:23 +03:00
|
|
|
|
2015-04-07 14:48:33 +03:00
|
|
|
$data = $this->loadData($response->getBody(), 'applications');
|
|
|
|
if($data === null) {
|
2015-03-30 16:58:20 +03:00
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
2014-10-24 01:27:15 +04:00
|
|
|
$tmp = $data->data->content;
|
|
|
|
$tmpCount = count($tmp);
|
2015-03-30 16:58:20 +03:00
|
|
|
|
|
|
|
$apps = [];
|
2015-02-10 00:48:27 +03:00
|
|
|
for ($i = 0; $i < $tmpCount; $i++) {
|
2015-03-30 16:58:20 +03:00
|
|
|
$app = [];
|
2015-02-10 00:48:27 +03:00
|
|
|
$app['id'] = (string)$tmp[$i]->id;
|
|
|
|
$app['name'] = (string)$tmp[$i]->name;
|
|
|
|
$app['label'] = (string)$tmp[$i]->label;
|
|
|
|
$app['version'] = (string)$tmp[$i]->version;
|
|
|
|
$app['type'] = (string)$tmp[$i]->typeid;
|
|
|
|
$app['typename'] = (string)$tmp[$i]->typename;
|
|
|
|
$app['personid'] = (string)$tmp[$i]->personid;
|
2015-04-03 14:33:27 +03:00
|
|
|
$app['profilepage'] = (string)$tmp[$i]->profilepage;
|
2015-02-10 00:48:27 +03:00
|
|
|
$app['license'] = (string)$tmp[$i]->license;
|
|
|
|
$app['detailpage'] = (string)$tmp[$i]->detailpage;
|
|
|
|
$app['preview'] = (string)$tmp[$i]->smallpreviewpic1;
|
|
|
|
$app['preview-full'] = (string)$tmp[$i]->previewpic1;
|
|
|
|
$app['changed'] = strtotime($tmp[$i]->changed);
|
|
|
|
$app['description'] = (string)$tmp[$i]->description;
|
|
|
|
$app['score'] = (string)$tmp[$i]->score;
|
2015-01-09 20:31:39 +03:00
|
|
|
$app['downloads'] = (int)$tmp[$i]->downloads;
|
2015-03-30 16:58:20 +03:00
|
|
|
$app['level'] = (int)$tmp[$i]->approved;
|
2012-08-29 10:38:33 +04:00
|
|
|
|
2015-02-10 00:48:27 +03:00
|
|
|
$apps[] = $app;
|
2012-08-29 10:38:33 +04:00
|
|
|
}
|
2015-03-30 16:58:20 +03:00
|
|
|
|
2011-04-16 21:42:58 +04:00
|
|
|
return $apps;
|
2011-04-16 16:36:32 +04:00
|
|
|
}
|
|
|
|
|
2011-04-17 01:07:18 +04:00
|
|
|
|
2011-06-19 00:02:45 +04:00
|
|
|
/**
|
2014-05-19 19:50:53 +04:00
|
|
|
* Get an the applications from the OCS server
|
2015-02-10 00:48:27 +03:00
|
|
|
*
|
2014-04-21 17:44:54 +04:00
|
|
|
* @param string $id
|
2015-06-29 12:15:56 +03:00
|
|
|
* @param array $targetVersion The target ownCloud version
|
2014-08-01 15:42:35 +04:00
|
|
|
* @return array|null an array of application data or null
|
2011-06-19 00:02:45 +04:00
|
|
|
*
|
2014-08-01 15:42:35 +04:00
|
|
|
* This function returns an applications from the OCS server
|
2011-06-19 00:02:45 +04:00
|
|
|
*/
|
2015-06-29 16:10:40 +03:00
|
|
|
public function getApplication($id, array $targetVersion) {
|
2015-03-30 16:58:20 +03:00
|
|
|
if (!$this->isAppStoreEnabled()) {
|
2012-10-23 10:01:09 +04:00
|
|
|
return null;
|
2012-06-22 18:06:46 +04:00
|
|
|
}
|
2015-03-30 16:58:20 +03:00
|
|
|
|
|
|
|
$client = $this->httpClientService->newClient();
|
2015-03-16 13:28:23 +03:00
|
|
|
try {
|
2015-03-30 16:58:20 +03:00
|
|
|
$response = $client->get(
|
|
|
|
$this->getAppStoreUrl() . '/content/data/' . urlencode($id),
|
|
|
|
[
|
2016-05-05 23:20:46 +03:00
|
|
|
'timeout' => 20,
|
2015-06-29 12:15:56 +03:00
|
|
|
'query' => [
|
|
|
|
'version' => implode('x', $targetVersion),
|
|
|
|
],
|
2015-03-30 16:58:20 +03:00
|
|
|
]
|
|
|
|
);
|
2015-03-16 13:28:23 +03:00
|
|
|
} catch(\Exception $e) {
|
2015-03-30 16:58:20 +03:00
|
|
|
$this->logger->error(
|
|
|
|
sprintf('Could not get application: %s', $e->getMessage()),
|
|
|
|
[
|
|
|
|
'app' => 'core',
|
|
|
|
]
|
|
|
|
);
|
2012-10-23 10:01:09 +04:00
|
|
|
return null;
|
2011-06-19 00:02:45 +04:00
|
|
|
}
|
2015-03-16 13:28:23 +03:00
|
|
|
|
2015-04-07 14:48:33 +03:00
|
|
|
$data = $this->loadData($response->getBody(), 'application');
|
|
|
|
if($data === null) {
|
2014-08-01 11:54:32 +04:00
|
|
|
return null;
|
|
|
|
}
|
2015-03-30 16:58:20 +03:00
|
|
|
|
|
|
|
$tmp = $data->data->content;
|
2015-04-13 10:43:45 +03:00
|
|
|
if (is_null($tmp)) {
|
2015-08-17 12:27:41 +03:00
|
|
|
\OCP\Util::writeLog('core', 'No update found at the ownCloud appstore for app ' . $id, \OCP\Util::DEBUG);
|
2015-04-13 10:43:45 +03:00
|
|
|
return null;
|
|
|
|
}
|
2015-03-30 16:58:20 +03:00
|
|
|
|
2015-03-16 13:28:23 +03:00
|
|
|
$app = [];
|
2016-01-10 00:15:50 +03:00
|
|
|
$app['id'] = (int)$id;
|
2015-03-30 16:58:20 +03:00
|
|
|
$app['name'] = (string)$tmp->name;
|
|
|
|
$app['version'] = (string)$tmp->version;
|
|
|
|
$app['type'] = (string)$tmp->typeid;
|
|
|
|
$app['label'] = (string)$tmp->label;
|
|
|
|
$app['typename'] = (string)$tmp->typename;
|
|
|
|
$app['personid'] = (string)$tmp->personid;
|
2015-04-03 14:33:27 +03:00
|
|
|
$app['profilepage'] = (string)$tmp->profilepage;
|
2015-03-30 16:58:20 +03:00
|
|
|
$app['detailpage'] = (string)$tmp->detailpage;
|
|
|
|
$app['preview1'] = (string)$tmp->smallpreviewpic1;
|
|
|
|
$app['preview2'] = (string)$tmp->smallpreviewpic2;
|
|
|
|
$app['preview3'] = (string)$tmp->smallpreviewpic3;
|
2015-02-10 00:48:27 +03:00
|
|
|
$app['changed'] = strtotime($tmp->changed);
|
2015-03-30 16:58:20 +03:00
|
|
|
$app['description'] = (string)$tmp->description;
|
|
|
|
$app['detailpage'] = (string)$tmp->detailpage;
|
|
|
|
$app['score'] = (int)$tmp->score;
|
2015-06-19 20:01:58 +03:00
|
|
|
$app['level'] = (int)$tmp->approved;
|
2011-06-19 00:02:45 +04:00
|
|
|
|
|
|
|
return $app;
|
|
|
|
}
|
|
|
|
|
2012-06-22 18:06:46 +04:00
|
|
|
/**
|
2014-05-19 19:50:53 +04:00
|
|
|
* Get the download url for an application from the OCS server
|
2015-06-29 12:15:56 +03:00
|
|
|
* @param string $id
|
|
|
|
* @param array $targetVersion The target ownCloud version
|
2014-08-01 15:42:35 +04:00
|
|
|
* @return array|null an array of application data or null
|
2014-04-21 17:44:54 +04:00
|
|
|
*/
|
2015-06-29 16:10:40 +03:00
|
|
|
public function getApplicationDownload($id, array $targetVersion) {
|
2015-03-30 16:58:20 +03:00
|
|
|
if (!$this->isAppStoreEnabled()) {
|
2012-10-23 10:01:09 +04:00
|
|
|
return null;
|
2012-06-22 18:06:46 +04:00
|
|
|
}
|
2015-03-30 16:58:20 +03:00
|
|
|
$url = $this->getAppStoreUrl() . '/content/download/' . urlencode($id) . '/1';
|
|
|
|
$client = $this->httpClientService->newClient();
|
2015-03-16 13:28:23 +03:00
|
|
|
try {
|
2015-03-30 16:58:20 +03:00
|
|
|
$response = $client->get(
|
|
|
|
$url,
|
|
|
|
[
|
2016-05-05 23:20:46 +03:00
|
|
|
'timeout' => 20,
|
2015-06-29 12:15:56 +03:00
|
|
|
'query' => [
|
|
|
|
'version' => implode('x', $targetVersion),
|
|
|
|
],
|
2015-03-30 16:58:20 +03:00
|
|
|
]
|
|
|
|
);
|
2015-03-16 13:28:23 +03:00
|
|
|
} catch(\Exception $e) {
|
2015-03-30 16:58:20 +03:00
|
|
|
$this->logger->error(
|
|
|
|
sprintf('Could not get application download URL: %s', $e->getMessage()),
|
|
|
|
[
|
|
|
|
'app' => 'core',
|
|
|
|
]
|
|
|
|
);
|
2012-10-23 10:01:09 +04:00
|
|
|
return null;
|
2012-06-22 18:06:46 +04:00
|
|
|
}
|
2015-03-16 13:28:23 +03:00
|
|
|
|
2015-04-07 14:48:33 +03:00
|
|
|
$data = $this->loadData($response->getBody(), 'application download URL');
|
|
|
|
if($data === null) {
|
2015-03-30 16:58:20 +03:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2015-02-10 00:48:27 +03:00
|
|
|
$tmp = $data->data->content;
|
2015-03-30 16:58:20 +03:00
|
|
|
$app = [];
|
2015-02-10 00:48:27 +03:00
|
|
|
if (isset($tmp->downloadlink)) {
|
2015-03-30 16:58:20 +03:00
|
|
|
$app['downloadlink'] = (string)$tmp->downloadlink;
|
2015-02-10 00:48:27 +03:00
|
|
|
} else {
|
|
|
|
$app['downloadlink'] = '';
|
2012-01-06 22:08:35 +04:00
|
|
|
}
|
2012-06-22 18:06:46 +04:00
|
|
|
return $app;
|
|
|
|
}
|
2012-01-06 22:08:35 +04:00
|
|
|
|
2011-04-16 16:36:32 +04:00
|
|
|
}
|