OCSClient.php 9.2 KB
Newer Older
1
2
<?php
/**
Jenkins for ownCloud's avatar
Jenkins for ownCloud committed
3
4
5
6
 * @author Bart Visscher <bartv@thisnet.nl>
 * @author Brice Maron <brice@bmaron.net>
 * @author Felix Moeller <mail@felixmoeller.de>
 * @author Frank Karlitschek <frank@owncloud.org>
7
 * @author Joas Schilling <nickvergessen@owncloud.com>
Jenkins for ownCloud's avatar
Jenkins for ownCloud committed
8
9
10
11
 * @author Jörn Friedrich Dreyer <jfd@butonic.de>
 * @author Kamil Domanski <kdomanski@kdemail.net>
 * @author Lukas Reschke <lukas@owncloud.com>
 * @author Morris Jobke <hey@morrisjobke.de>
Thomas Müller's avatar
Thomas Müller committed
12
 * @author Robin McCorkell <robin@mccorkell.me.uk>
Jenkins for ownCloud's avatar
Jenkins for ownCloud committed
13
14
 * @author Sam Tuke <mail@samtuke.com>
 * @author Thomas Müller <thomas.mueller@tmit.eu>
15
 *
Thomas Müller's avatar
Thomas Müller committed
16
 * @copyright Copyright (c) 2016, ownCloud, Inc.
Jenkins for ownCloud's avatar
Jenkins for ownCloud committed
17
 * @license AGPL-3.0
18
 *
Jenkins for ownCloud's avatar
Jenkins for ownCloud committed
19
20
21
 * 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.
22
 *
Jenkins for ownCloud's avatar
Jenkins for ownCloud committed
23
 * This program is distributed in the hope that it will be useful,
24
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Jenkins for ownCloud's avatar
Jenkins for ownCloud committed
25
26
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU Affero General Public License for more details.
27
 *
Jenkins for ownCloud's avatar
Jenkins for ownCloud committed
28
29
 * 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/>
30
31
 *
 */
32
33
34

namespace OC;

35
36
37
38
use OCP\Http\Client\IClientService;
use OCP\IConfig;
use OCP\ILogger;

39
/**
40
41
42
 * Class OCSClient is a class for communication with the ownCloud appstore
 *
 * @package OC
43
44
 */
class OCSClient {
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
	/** @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;
	}
64

65
66
	/**
	 * Returns whether the AppStore is enabled (i.e. because the AppStore is disabled for EE)
67
	 *
68
69
	 * @return bool
	 */
70
	public function isAppStoreEnabled() {
71
72
73
74
75
76
77
		// For a regular edition default to true, all others default to false
		$default = false;
		if (\OC_Util::getEditionString() === '') {
			$default = true;
		}

		return $this->config->getSystemValue('appstoreenabled', $default) === true;
78
79
	}

80
	/**
81
	 * Get the url of the OCS AppStore server.
82
	 *
83
	 * @return string of the AppStore server
84
	 */
85
86
	private function getAppStoreUrl() {
		return $this->config->getSystemValue('appstoreurl', 'https://api.owncloud.com/v1');
87
88
	}

Lukas Reschke's avatar
Lukas Reschke committed
89
90
91
92
93
94
95
96
97
98
99
	/**
	 * @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) {
100
			libxml_clear_errors();
Lukas Reschke's avatar
Lukas Reschke committed
101
102
103
104
105
106
107
108
109
110
111
112
			$this->logger->error(
				sprintf('Could not get %s, content was no valid XML', $action),
				[
					'app' => 'core',
				]
			);
			return null;
		}

		return $data;
	}

Bart Visscher's avatar
Bart Visscher committed
113
	/**
114
	 * Get all the categories from the OCS server
115
	 *
116
	 * @param array $targetVersion The target ownCloud version
117
	 * @return array|null an array of category ids or null
118
	 * @note returns NULL if config value appstoreenabled is set to false
119
120
	 * This function returns a list of all the application categories on the OCS server
	 */
Lukas Reschke's avatar
Lukas Reschke committed
121
	public function getCategories(array $targetVersion) {
122
		if (!$this->isAppStoreEnabled()) {
Felix Moeller's avatar
Felix Moeller committed
123
			return null;
124
		}
Lukas Reschke's avatar
Lukas Reschke committed
125

126
		$client = $this->httpClientService->newClient();
Lukas Reschke's avatar
Lukas Reschke committed
127
		try {
128
129
130
131
			$response = $client->get(
				$this->getAppStoreUrl() . '/content/categories',
				[
					'timeout' => 5,
132
133
134
					'query' => [
						'version' => implode('x', $targetVersion),
					],
135
136
				]
			);
Lukas Reschke's avatar
Lukas Reschke committed
137
		} catch(\Exception $e) {
138
139
140
141
142
143
			$this->logger->error(
				sprintf('Could not get categories: %s', $e->getMessage()),
				[
					'app' => 'core',
				]
			);
Felix Moeller's avatar
Felix Moeller committed
144
			return null;
145
		}
Lukas Reschke's avatar
Lukas Reschke committed
146

Lukas Reschke's avatar
Lukas Reschke committed
147
148
		$data = $this->loadData($response->getBody(), 'categories');
		if($data === null) {
149
150
151
			return null;
		}

152
		$tmp = $data->data;
Lukas Reschke's avatar
Lukas Reschke committed
153
		$cats = [];
154

155
156
157
158
		foreach ($tmp->category as $value) {
			$id = (int)$value->id;
			$name = (string)$value->name;
			$cats[$id] = $name;
159
		}
160

161
		return $cats;
162
163
164
	}

	/**
165
	 * Get all the applications from the OCS server
166
	 * @param array $categories
Lukas Reschke's avatar
Lukas Reschke committed
167
	 * @param int $page
168
	 * @param string $filter
169
	 * @param array $targetVersion The target ownCloud version
170
	 * @return array An array of application data
171
	 */
Lukas Reschke's avatar
Lukas Reschke committed
172
	public function getApplications(array $categories, $page, $filter, array $targetVersion) {
173
174
		if (!$this->isAppStoreEnabled()) {
			return [];
175
176
		}

177
		$client = $this->httpClientService->newClient();
Lukas Reschke's avatar
Lukas Reschke committed
178
		try {
179
180
181
182
183
			$response = $client->get(
				$this->getAppStoreUrl() . '/content/data',
				[
					'timeout' => 5,
					'query' => [
184
						'version' => implode('x', $targetVersion),
185
186
187
188
189
190
191
192
193
						'filter' => $filter,
						'categories' => implode('x', $categories),
						'sortmode' => 'new',
						'page' => $page,
						'pagesize' => 100,
						'approved' => $filter
					],
				]
			);
Lukas Reschke's avatar
Lukas Reschke committed
194
		} catch(\Exception $e) {
195
196
197
198
199
200
201
			$this->logger->error(
				sprintf('Could not get applications: %s', $e->getMessage()),
				[
					'app' => 'core',
				]
			);
			return [];
202
		}
Lukas Reschke's avatar
Lukas Reschke committed
203

Lukas Reschke's avatar
Lukas Reschke committed
204
205
		$data = $this->loadData($response->getBody(), 'applications');
		if($data === null) {
206
207
208
			return [];
		}

Lukas Reschke's avatar
Lukas Reschke committed
209
210
		$tmp = $data->data->content;
		$tmpCount = count($tmp);
211
212

		$apps = [];
213
		for ($i = 0; $i < $tmpCount; $i++) {
214
			$app = [];
215
216
217
218
219
220
221
			$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;
Lukas Reschke's avatar
Lukas Reschke committed
222
			$app['profilepage'] = (string)$tmp[$i]->profilepage;
223
224
225
226
227
228
229
			$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;
230
			$app['downloads'] = (int)$tmp[$i]->downloads;
231
			$app['level'] = (int)$tmp[$i]->approved;
Bart Visscher's avatar
Bart Visscher committed
232

233
			$apps[] = $app;
Bart Visscher's avatar
Bart Visscher committed
234
		}
235

236
		return $apps;
237
238
	}

Frank Karlitschek's avatar
Frank Karlitschek committed
239

240
	/**
241
	 * Get an the applications from the OCS server
242
	 *
Lukas Reschke's avatar
Lukas Reschke committed
243
	 * @param string $id
244
	 * @param array $targetVersion The target ownCloud version
245
	 * @return array|null an array of application data or null
246
	 *
247
	 * This function returns an applications from the OCS server
248
	 */
Lukas Reschke's avatar
Lukas Reschke committed
249
	public function getApplication($id, array $targetVersion) {
250
		if (!$this->isAppStoreEnabled()) {
Felix Moeller's avatar
Felix Moeller committed
251
			return null;
252
		}
253
254

		$client = $this->httpClientService->newClient();
Lukas Reschke's avatar
Lukas Reschke committed
255
		try {
256
257
258
259
			$response = $client->get(
				$this->getAppStoreUrl() . '/content/data/' . urlencode($id),
				[
					'timeout' => 5,
260
261
262
					'query' => [
						'version' => implode('x', $targetVersion),
					],
263
264
				]
			);
Lukas Reschke's avatar
Lukas Reschke committed
265
		} catch(\Exception $e) {
266
267
268
269
270
271
			$this->logger->error(
				sprintf('Could not get application: %s', $e->getMessage()),
				[
					'app' => 'core',
				]
			);
Felix Moeller's avatar
Felix Moeller committed
272
			return null;
273
		}
Lukas Reschke's avatar
Lukas Reschke committed
274

Lukas Reschke's avatar
Lukas Reschke committed
275
276
		$data = $this->loadData($response->getBody(), 'application');
		if($data === null) {
277
278
			return null;
		}
279
280

		$tmp = $data->data->content;
281
		if (is_null($tmp)) {
282
			\OCP\Util::writeLog('core', 'No update found at the ownCloud appstore for app ' . $id, \OCP\Util::DEBUG);
283
284
			return null;
		}
285

Lukas Reschke's avatar
Lukas Reschke committed
286
		$app = [];
287
		$app['id'] = (int)$id;
288
289
290
291
292
293
		$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;
Lukas Reschke's avatar
Lukas Reschke committed
294
		$app['profilepage'] = (string)$tmp->profilepage;
295
296
297
298
		$app['detailpage'] = (string)$tmp->detailpage;
		$app['preview1'] = (string)$tmp->smallpreviewpic1;
		$app['preview2'] = (string)$tmp->smallpreviewpic2;
		$app['preview3'] = (string)$tmp->smallpreviewpic3;
299
		$app['changed'] = strtotime($tmp->changed);
300
301
302
		$app['description'] = (string)$tmp->description;
		$app['detailpage'] = (string)$tmp->detailpage;
		$app['score'] = (int)$tmp->score;
303
		$app['level'] = (int)$tmp->approved;
304
305
306
307

		return $app;
	}

308
	/**
309
	 * Get the download url for an application from the OCS server
310
311
	 * @param string $id
	 * @param array $targetVersion The target ownCloud version
312
	 * @return array|null an array of application data or null
Lukas Reschke's avatar
Lukas Reschke committed
313
	 */
Lukas Reschke's avatar
Lukas Reschke committed
314
	public function getApplicationDownload($id, array $targetVersion) {
315
		if (!$this->isAppStoreEnabled()) {
Felix Moeller's avatar
Felix Moeller committed
316
			return null;
317
		}
318
319
		$url = $this->getAppStoreUrl() . '/content/download/' . urlencode($id) . '/1';
		$client = $this->httpClientService->newClient();
Lukas Reschke's avatar
Lukas Reschke committed
320
		try {
321
322
323
324
			$response = $client->get(
				$url,
				[
					'timeout' => 5,
325
326
327
					'query' => [
						'version' => implode('x', $targetVersion),
					],
328
329
				]
			);
Lukas Reschke's avatar
Lukas Reschke committed
330
		} catch(\Exception $e) {
331
332
333
334
335
336
			$this->logger->error(
				sprintf('Could not get application download URL: %s', $e->getMessage()),
				[
					'app' => 'core',
				]
			);
Felix Moeller's avatar
Felix Moeller committed
337
			return null;
338
		}
Lukas Reschke's avatar
Lukas Reschke committed
339

Lukas Reschke's avatar
Lukas Reschke committed
340
341
		$data = $this->loadData($response->getBody(), 'application download URL');
		if($data === null) {
342
343
344
			return null;
		}

345
		$tmp = $data->data->content;
346
		$app = [];
347
		if (isset($tmp->downloadlink)) {
348
			$app['downloadlink'] = (string)$tmp->downloadlink;
349
350
		} else {
			$app['downloadlink'] = '';
351
		}
352
353
		return $app;
	}
354

355
}