ocsclient.php 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  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 Morris Jobke <hey@morrisjobke.de>
  11. * @author Robin McCorkell <rmccorkell@karoshi.org.uk>
  12. * @author Sam Tuke <mail@samtuke.com>
  13. * @author Thomas Müller <thomas.mueller@tmit.eu>
  14. * @author Vincent Petry <pvince81@owncloud.com>
  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. * @return array|null an array of category ids or null
  100. * @note returns NULL if config value appstoreenabled is set to false
  101. * This function returns a list of all the application categories on the OCS server
  102. */
  103. public function getCategories() {
  104. if (!$this->isAppStoreEnabled()) {
  105. return null;
  106. }
  107. $client = $this->httpClientService->newClient();
  108. try {
  109. $response = $client->get(
  110. $this->getAppStoreUrl() . '/content/categories',
  111. [
  112. 'timeout' => 5,
  113. ]
  114. );
  115. } catch(\Exception $e) {
  116. $this->logger->error(
  117. sprintf('Could not get categories: %s', $e->getMessage()),
  118. [
  119. 'app' => 'core',
  120. ]
  121. );
  122. return null;
  123. }
  124. $data = $this->loadData($response->getBody(), 'categories');
  125. if($data === null) {
  126. return null;
  127. }
  128. $tmp = $data->data;
  129. $cats = [];
  130. foreach ($tmp->category as $value) {
  131. $id = (int)$value->id;
  132. $name = (string)$value->name;
  133. $cats[$id] = $name;
  134. }
  135. return $cats;
  136. }
  137. /**
  138. * Get all the applications from the OCS server
  139. * @param array $categories
  140. * @param int $page
  141. * @param string $filter
  142. * @return array An array of application data
  143. */
  144. public function getApplications(array $categories, $page, $filter) {
  145. if (!$this->isAppStoreEnabled()) {
  146. return [];
  147. }
  148. $client = $this->httpClientService->newClient();
  149. try {
  150. $response = $client->get(
  151. $this->getAppStoreUrl() . '/content/data',
  152. [
  153. 'timeout' => 5,
  154. 'query' => [
  155. 'version' => implode('x', \OC_Util::getVersion()),
  156. 'filter' => $filter,
  157. 'categories' => implode('x', $categories),
  158. 'sortmode' => 'new',
  159. 'page' => $page,
  160. 'pagesize' => 100,
  161. 'approved' => $filter
  162. ],
  163. ]
  164. );
  165. } catch(\Exception $e) {
  166. $this->logger->error(
  167. sprintf('Could not get applications: %s', $e->getMessage()),
  168. [
  169. 'app' => 'core',
  170. ]
  171. );
  172. return [];
  173. }
  174. $data = $this->loadData($response->getBody(), 'applications');
  175. if($data === null) {
  176. return [];
  177. }
  178. $tmp = $data->data->content;
  179. $tmpCount = count($tmp);
  180. $apps = [];
  181. for ($i = 0; $i < $tmpCount; $i++) {
  182. $app = [];
  183. $app['id'] = (string)$tmp[$i]->id;
  184. $app['name'] = (string)$tmp[$i]->name;
  185. $app['label'] = (string)$tmp[$i]->label;
  186. $app['version'] = (string)$tmp[$i]->version;
  187. $app['type'] = (string)$tmp[$i]->typeid;
  188. $app['typename'] = (string)$tmp[$i]->typename;
  189. $app['personid'] = (string)$tmp[$i]->personid;
  190. $app['profilepage'] = (string)$tmp[$i]->profilepage;
  191. $app['license'] = (string)$tmp[$i]->license;
  192. $app['detailpage'] = (string)$tmp[$i]->detailpage;
  193. $app['preview'] = (string)$tmp[$i]->smallpreviewpic1;
  194. $app['preview-full'] = (string)$tmp[$i]->previewpic1;
  195. $app['changed'] = strtotime($tmp[$i]->changed);
  196. $app['description'] = (string)$tmp[$i]->description;
  197. $app['score'] = (string)$tmp[$i]->score;
  198. $app['downloads'] = (int)$tmp[$i]->downloads;
  199. $app['level'] = (int)$tmp[$i]->approved;
  200. $apps[] = $app;
  201. }
  202. return $apps;
  203. }
  204. /**
  205. * Get an the applications from the OCS server
  206. *
  207. * @param string $id
  208. * @return array|null an array of application data or null
  209. *
  210. * This function returns an applications from the OCS server
  211. */
  212. public function getApplication($id) {
  213. if (!$this->isAppStoreEnabled()) {
  214. return null;
  215. }
  216. $client = $this->httpClientService->newClient();
  217. try {
  218. $response = $client->get(
  219. $this->getAppStoreUrl() . '/content/data/' . urlencode($id),
  220. [
  221. 'timeout' => 5,
  222. ]
  223. );
  224. } catch(\Exception $e) {
  225. $this->logger->error(
  226. sprintf('Could not get application: %s', $e->getMessage()),
  227. [
  228. 'app' => 'core',
  229. ]
  230. );
  231. return null;
  232. }
  233. $data = $this->loadData($response->getBody(), 'application');
  234. if($data === null) {
  235. return null;
  236. }
  237. $tmp = $data->data->content;
  238. if (is_null($tmp)) {
  239. \OC_Log::write('core', 'No update found at the ownCloud appstore for app ' . $id, \OC_Log::INFO);
  240. return null;
  241. }
  242. $app = [];
  243. $app['id'] = (int)$tmp->id;
  244. $app['name'] = (string)$tmp->name;
  245. $app['version'] = (string)$tmp->version;
  246. $app['type'] = (string)$tmp->typeid;
  247. $app['label'] = (string)$tmp->label;
  248. $app['typename'] = (string)$tmp->typename;
  249. $app['personid'] = (string)$tmp->personid;
  250. $app['profilepage'] = (string)$tmp->profilepage;
  251. $app['detailpage'] = (string)$tmp->detailpage;
  252. $app['preview1'] = (string)$tmp->smallpreviewpic1;
  253. $app['preview2'] = (string)$tmp->smallpreviewpic2;
  254. $app['preview3'] = (string)$tmp->smallpreviewpic3;
  255. $app['changed'] = strtotime($tmp->changed);
  256. $app['description'] = (string)$tmp->description;
  257. $app['detailpage'] = (string)$tmp->detailpage;
  258. $app['score'] = (int)$tmp->score;
  259. return $app;
  260. }
  261. /**
  262. * Get the download url for an application from the OCS server
  263. * @param $id
  264. * @return array|null an array of application data or null
  265. */
  266. public function getApplicationDownload($id) {
  267. if (!$this->isAppStoreEnabled()) {
  268. return null;
  269. }
  270. $url = $this->getAppStoreUrl() . '/content/download/' . urlencode($id) . '/1';
  271. $client = $this->httpClientService->newClient();
  272. try {
  273. $response = $client->get(
  274. $url,
  275. [
  276. 'timeout' => 5,
  277. ]
  278. );
  279. } catch(\Exception $e) {
  280. $this->logger->error(
  281. sprintf('Could not get application download URL: %s', $e->getMessage()),
  282. [
  283. 'app' => 'core',
  284. ]
  285. );
  286. return null;
  287. }
  288. $data = $this->loadData($response->getBody(), 'application download URL');
  289. if($data === null) {
  290. return null;
  291. }
  292. $tmp = $data->data->content;
  293. $app = [];
  294. if (isset($tmp->downloadlink)) {
  295. $app['downloadlink'] = (string)$tmp->downloadlink;
  296. } else {
  297. $app['downloadlink'] = '';
  298. }
  299. return $app;
  300. }
  301. }