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.

ocsclient.php 9.0KB

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