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