OCSClient.php 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Bart Visscher <bartv@thisnet.nl>
  6. * @author Brice Maron <brice@bmaron.net>
  7. * @author Felix Moeller <mail@felixmoeller.de>
  8. * @author Frank Karlitschek <frank@karlitschek.de>
  9. * @author Jarrett <JetUni@users.noreply.github.com>
  10. * @author Joas Schilling <coding@schilljs.com>
  11. * @author Jörn Friedrich Dreyer <jfd@butonic.de>
  12. * @author Kamil Domanski <kdomanski@kdemail.net>
  13. * @author Lukas Reschke <lukas@statuscode.ch>
  14. * @author Morris Jobke <hey@morrisjobke.de>
  15. * @author Robin McCorkell <robin@mccorkell.me.uk>
  16. * @author Sam Tuke <mail@samtuke.com>
  17. * @author Thomas Müller <thomas.mueller@tmit.eu>
  18. *
  19. * @license AGPL-3.0
  20. *
  21. * This code is free software: you can redistribute it and/or modify
  22. * it under the terms of the GNU Affero General Public License, version 3,
  23. * as published by the Free Software Foundation.
  24. *
  25. * This program is distributed in the hope that it will be useful,
  26. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  27. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  28. * GNU Affero General Public License for more details.
  29. *
  30. * You should have received a copy of the GNU Affero General Public License, version 3,
  31. * along with this program. If not, see <http://www.gnu.org/licenses/>
  32. *
  33. */
  34. namespace OC;
  35. use OCP\Http\Client\IClientService;
  36. use OCP\IConfig;
  37. use OCP\ILogger;
  38. /**
  39. * Class OCSClient is a class for communication with the ownCloud appstore
  40. *
  41. * @package OC
  42. */
  43. class OCSClient {
  44. /** @var IClientService */
  45. private $httpClientService;
  46. /** @var IConfig */
  47. private $config;
  48. /** @var ILogger */
  49. private $logger;
  50. /**
  51. * @param IClientService $httpClientService
  52. * @param IConfig $config
  53. * @param ILogger $logger
  54. */
  55. public function __construct(IClientService $httpClientService,
  56. IConfig $config,
  57. ILogger $logger) {
  58. $this->httpClientService = $httpClientService;
  59. $this->config = $config;
  60. $this->logger = $logger;
  61. }
  62. /**
  63. * Returns whether the AppStore is enabled (i.e. because the AppStore is disabled for EE)
  64. *
  65. * @return bool
  66. */
  67. public function isAppStoreEnabled() {
  68. return $this->config->getSystemValue('appstoreenabled', true) === true;
  69. }
  70. /**
  71. * Get the url of the OCS AppStore server.
  72. *
  73. * @return string of the AppStore server
  74. */
  75. private function getAppStoreUrl() {
  76. return $this->config->getSystemValue('appstoreurl', 'https://api.owncloud.com/v1');
  77. }
  78. /**
  79. * @param string $body
  80. * @param string $action
  81. * @return null|\SimpleXMLElement
  82. */
  83. private function loadData($body, $action) {
  84. $loadEntities = libxml_disable_entity_loader(true);
  85. $data = @simplexml_load_string($body);
  86. libxml_disable_entity_loader($loadEntities);
  87. if($data === false) {
  88. libxml_clear_errors();
  89. $this->logger->error(
  90. sprintf('Could not get %s, content was no valid XML', $action),
  91. [
  92. 'app' => 'core',
  93. ]
  94. );
  95. return null;
  96. }
  97. return $data;
  98. }
  99. /**
  100. * Get all the categories from the OCS server
  101. *
  102. * @param array $targetVersion The target ownCloud version
  103. * @return array|null an array of category ids or null
  104. * @note returns NULL if config value appstoreenabled is set to false
  105. * This function returns a list of all the application categories on the OCS server
  106. */
  107. public function getCategories(array $targetVersion) {
  108. if (!$this->isAppStoreEnabled()) {
  109. return null;
  110. }
  111. $client = $this->httpClientService->newClient();
  112. try {
  113. $response = $client->get(
  114. $this->getAppStoreUrl() . '/content/categories',
  115. [
  116. 'timeout' => 20,
  117. 'query' => [
  118. 'version' => implode('x', $targetVersion),
  119. ],
  120. ]
  121. );
  122. } catch(\Exception $e) {
  123. $this->logger->error(
  124. sprintf('Could not get categories: %s', $e->getMessage()),
  125. [
  126. 'app' => 'core',
  127. ]
  128. );
  129. return null;
  130. }
  131. $data = $this->loadData($response->getBody(), 'categories');
  132. if($data === null) {
  133. return null;
  134. }
  135. $tmp = $data->data;
  136. $cats = [];
  137. foreach ($tmp->category as $value) {
  138. $id = (int)$value->id;
  139. $name = (string)$value->name;
  140. $cats[$id] = $name;
  141. }
  142. return $cats;
  143. }
  144. /**
  145. * Get all the applications from the OCS server
  146. * @param array $categories
  147. * @param int $page
  148. * @param string $filter
  149. * @param array $targetVersion The target ownCloud version
  150. * @return array An array of application data
  151. */
  152. public function getApplications(array $categories, $page, $filter, array $targetVersion) {
  153. if (!$this->isAppStoreEnabled()) {
  154. return [];
  155. }
  156. $client = $this->httpClientService->newClient();
  157. try {
  158. $response = $client->get(
  159. $this->getAppStoreUrl() . '/content/data',
  160. [
  161. 'timeout' => 20,
  162. 'query' => [
  163. 'version' => implode('x', $targetVersion),
  164. 'filter' => $filter,
  165. 'categories' => implode('x', $categories),
  166. 'sortmode' => 'new',
  167. 'page' => $page,
  168. 'pagesize' => 100,
  169. 'approved' => $filter
  170. ],
  171. ]
  172. );
  173. } catch(\Exception $e) {
  174. $this->logger->error(
  175. sprintf('Could not get applications: %s', $e->getMessage()),
  176. [
  177. 'app' => 'core',
  178. ]
  179. );
  180. return [];
  181. }
  182. $data = $this->loadData($response->getBody(), 'applications');
  183. if($data === null) {
  184. return [];
  185. }
  186. $tmp = $data->data->content;
  187. $tmpCount = count($tmp);
  188. $apps = [];
  189. for ($i = 0; $i < $tmpCount; $i++) {
  190. $app = [];
  191. $app['id'] = (string)$tmp[$i]->id;
  192. $app['name'] = (string)$tmp[$i]->name;
  193. $app['label'] = (string)$tmp[$i]->label;
  194. $app['version'] = (string)$tmp[$i]->version;
  195. $app['type'] = (string)$tmp[$i]->typeid;
  196. $app['typename'] = (string)$tmp[$i]->typename;
  197. $app['personid'] = (string)$tmp[$i]->personid;
  198. $app['profilepage'] = (string)$tmp[$i]->profilepage;
  199. $app['license'] = (string)$tmp[$i]->license;
  200. $app['detailpage'] = (string)$tmp[$i]->detailpage;
  201. $app['preview'] = (string)$tmp[$i]->smallpreviewpic1;
  202. $app['preview-full'] = (string)$tmp[$i]->previewpic1;
  203. $app['changed'] = strtotime($tmp[$i]->changed);
  204. $app['description'] = (string)$tmp[$i]->description;
  205. $app['score'] = (string)$tmp[$i]->score;
  206. $app['downloads'] = (int)$tmp[$i]->downloads;
  207. $app['level'] = (int)$tmp[$i]->approved;
  208. $apps[] = $app;
  209. }
  210. return $apps;
  211. }
  212. /**
  213. * Get an the applications from the OCS server
  214. *
  215. * @param string $id
  216. * @param array $targetVersion The target ownCloud version
  217. * @return array|null an array of application data or null
  218. *
  219. * This function returns an applications from the OCS server
  220. */
  221. public function getApplication($id, array $targetVersion) {
  222. if (!$this->isAppStoreEnabled()) {
  223. return null;
  224. }
  225. $client = $this->httpClientService->newClient();
  226. try {
  227. $response = $client->get(
  228. $this->getAppStoreUrl() . '/content/data/' . urlencode($id),
  229. [
  230. 'timeout' => 20,
  231. 'query' => [
  232. 'version' => implode('x', $targetVersion),
  233. ],
  234. ]
  235. );
  236. } catch(\Exception $e) {
  237. $this->logger->error(
  238. sprintf('Could not get application: %s', $e->getMessage()),
  239. [
  240. 'app' => 'core',
  241. ]
  242. );
  243. return null;
  244. }
  245. $data = $this->loadData($response->getBody(), 'application');
  246. if($data === null) {
  247. return null;
  248. }
  249. $tmp = $data->data->content;
  250. if (is_null($tmp)) {
  251. \OCP\Util::writeLog('core', 'No update found at the ownCloud appstore for app ' . $id, \OCP\Util::DEBUG);
  252. return null;
  253. }
  254. $app = [];
  255. $app['id'] = (int)$id;
  256. $app['name'] = (string)$tmp->name;
  257. $app['version'] = (string)$tmp->version;
  258. $app['type'] = (string)$tmp->typeid;
  259. $app['label'] = (string)$tmp->label;
  260. $app['typename'] = (string)$tmp->typename;
  261. $app['personid'] = (string)$tmp->personid;
  262. $app['profilepage'] = (string)$tmp->profilepage;
  263. $app['detailpage'] = (string)$tmp->detailpage;
  264. $app['preview1'] = (string)$tmp->smallpreviewpic1;
  265. $app['preview2'] = (string)$tmp->smallpreviewpic2;
  266. $app['preview3'] = (string)$tmp->smallpreviewpic3;
  267. $app['changed'] = strtotime($tmp->changed);
  268. $app['description'] = (string)$tmp->description;
  269. $app['detailpage'] = (string)$tmp->detailpage;
  270. $app['score'] = (int)$tmp->score;
  271. $app['level'] = (int)$tmp->approved;
  272. return $app;
  273. }
  274. /**
  275. * Get the download url for an application from the OCS server
  276. * @param string $id
  277. * @param array $targetVersion The target ownCloud version
  278. * @return array|null an array of application data or null
  279. */
  280. public function getApplicationDownload($id, array $targetVersion) {
  281. if (!$this->isAppStoreEnabled()) {
  282. return null;
  283. }
  284. $url = $this->getAppStoreUrl() . '/content/download/' . urlencode($id) . '/1';
  285. $client = $this->httpClientService->newClient();
  286. try {
  287. $response = $client->get(
  288. $url,
  289. [
  290. 'timeout' => 20,
  291. 'query' => [
  292. 'version' => implode('x', $targetVersion),
  293. ],
  294. ]
  295. );
  296. } catch(\Exception $e) {
  297. $this->logger->error(
  298. sprintf('Could not get application download URL: %s', $e->getMessage()),
  299. [
  300. 'app' => 'core',
  301. ]
  302. );
  303. return null;
  304. }
  305. $data = $this->loadData($response->getBody(), 'application download URL');
  306. if($data === null) {
  307. return null;
  308. }
  309. $tmp = $data->data->content;
  310. $app = [];
  311. if (isset($tmp->downloadlink)) {
  312. $app['downloadlink'] = (string)$tmp->downloadlink;
  313. } else {
  314. $app['downloadlink'] = '';
  315. }
  316. return $app;
  317. }
  318. }