1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @copyright Copyright (c) 2016 Lukas Reschke <[email protected]> |
4
|
|
|
* |
5
|
|
|
* @author Joas Schilling <[email protected]> |
6
|
|
|
* @author Lukas Reschke <[email protected]> |
7
|
|
|
* @author Morris Jobke <[email protected]> |
8
|
|
|
* @author Roeland Jago Douma <[email protected]> |
9
|
|
|
* @author Steffen Lindner <[email protected]> |
10
|
|
|
* |
11
|
|
|
* @license GNU AGPL version 3 or any later version |
12
|
|
|
* |
13
|
|
|
* This program is free software: you can redistribute it and/or modify |
14
|
|
|
* it under the terms of the GNU Affero General Public License as |
15
|
|
|
* published by the Free Software Foundation, either version 3 of the |
16
|
|
|
* License, or (at your option) any later version. |
17
|
|
|
* |
18
|
|
|
* This program is distributed in the hope that it will be useful, |
19
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
20
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
21
|
|
|
* GNU Affero General Public License for more details. |
22
|
|
|
* |
23
|
|
|
* You should have received a copy of the GNU Affero General Public License |
24
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
25
|
|
|
* |
26
|
|
|
*/ |
27
|
|
|
|
28
|
|
|
namespace OC\App\AppStore\Fetcher; |
29
|
|
|
|
30
|
|
|
use OC\Files\AppData\Factory; |
31
|
|
|
use GuzzleHttp\Exception\ConnectException; |
32
|
|
|
use OCP\AppFramework\Http; |
33
|
|
|
use OCP\AppFramework\Utility\ITimeFactory; |
34
|
|
|
use OCP\Files\IAppData; |
35
|
|
|
use OCP\Files\NotFoundException; |
36
|
|
|
use OCP\Http\Client\IClientService; |
37
|
|
|
use OCP\IConfig; |
38
|
|
|
use OCP\ILogger; |
39
|
|
|
use OCP\Util; |
40
|
|
|
|
41
|
|
|
abstract class Fetcher { |
42
|
|
|
const INVALIDATE_AFTER_SECONDS = 300; |
43
|
|
|
|
44
|
|
|
/** @var IAppData */ |
45
|
|
|
protected $appData; |
46
|
|
|
/** @var IClientService */ |
47
|
|
|
protected $clientService; |
48
|
|
|
/** @var ITimeFactory */ |
49
|
|
|
protected $timeFactory; |
50
|
|
|
/** @var IConfig */ |
51
|
|
|
protected $config; |
52
|
|
|
/** @var Ilogger */ |
53
|
|
|
protected $logger; |
54
|
|
|
/** @var string */ |
55
|
|
|
protected $fileName; |
56
|
|
|
/** @var string */ |
57
|
|
|
protected $endpointUrl; |
58
|
|
|
/** @var string */ |
59
|
|
|
protected $version; |
60
|
|
|
/** @var string */ |
61
|
|
|
protected $channel; |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @param Factory $appDataFactory |
65
|
|
|
* @param IClientService $clientService |
66
|
|
|
* @param ITimeFactory $timeFactory |
67
|
|
|
* @param IConfig $config |
68
|
|
|
* @param ILogger $logger |
69
|
|
|
*/ |
70
|
|
|
public function __construct(Factory $appDataFactory, |
71
|
|
|
IClientService $clientService, |
72
|
|
|
ITimeFactory $timeFactory, |
73
|
|
|
IConfig $config, |
74
|
|
|
ILogger $logger) { |
75
|
|
|
$this->appData = $appDataFactory->get('appstore'); |
76
|
|
|
$this->clientService = $clientService; |
77
|
|
|
$this->timeFactory = $timeFactory; |
78
|
|
|
$this->config = $config; |
79
|
|
|
$this->logger = $logger; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Fetches the response from the server |
84
|
|
|
* |
85
|
|
|
* @param string $ETag |
86
|
|
|
* @param string $content |
87
|
|
|
* |
88
|
|
|
* @return array |
89
|
|
|
*/ |
90
|
|
|
protected function fetch($ETag, $content) { |
91
|
|
|
$appstoreenabled = $this->config->getSystemValue('appstoreenabled', true); |
92
|
|
|
|
93
|
|
|
if (!$appstoreenabled) { |
94
|
|
|
return []; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
$options = [ |
98
|
|
|
'timeout' => 10, |
99
|
|
|
]; |
100
|
|
|
|
101
|
|
|
if ($ETag !== '') { |
102
|
|
|
$options['headers'] = [ |
103
|
|
|
'If-None-Match' => $ETag, |
104
|
|
|
]; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
$client = $this->clientService->newClient(); |
108
|
|
|
$response = $client->get($this->endpointUrl, $options); |
109
|
|
|
|
110
|
|
|
$responseJson = []; |
111
|
|
|
if ($response->getStatusCode() === Http::STATUS_NOT_MODIFIED) { |
112
|
|
|
$responseJson['data'] = json_decode($content, true); |
113
|
|
|
} else { |
114
|
|
|
$responseJson['data'] = json_decode($response->getBody(), true); |
115
|
|
|
$ETag = $response->getHeader('ETag'); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
$responseJson['timestamp'] = $this->timeFactory->getTime(); |
119
|
|
|
$responseJson['ncversion'] = $this->getVersion(); |
120
|
|
|
if ($ETag !== '') { |
121
|
|
|
$responseJson['ETag'] = $ETag; |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
return $responseJson; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* Returns the array with the categories on the appstore server |
129
|
|
|
* |
130
|
|
|
* @return array |
131
|
|
|
*/ |
132
|
|
|
public function get() { |
133
|
|
|
$appstoreenabled = $this->config->getSystemValue('appstoreenabled', true); |
134
|
|
|
$internetavailable = $this->config->getSystemValue('has_internet_connection', true); |
135
|
|
|
|
136
|
|
|
if (!$appstoreenabled || !$internetavailable) { |
137
|
|
|
return []; |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
$rootFolder = $this->appData->getFolder('/'); |
141
|
|
|
|
142
|
|
|
$ETag = ''; |
143
|
|
|
$content = ''; |
144
|
|
|
|
145
|
|
|
try { |
146
|
|
|
// File does already exists |
147
|
|
|
$file = $rootFolder->getFile($this->fileName); |
148
|
|
|
$jsonBlob = json_decode($file->getContent(), true); |
149
|
|
|
if (is_array($jsonBlob)) { |
150
|
|
|
|
151
|
|
|
// No caching when the version has been updated |
152
|
|
|
if (isset($jsonBlob['ncversion']) && $jsonBlob['ncversion'] === $this->getVersion()) { |
153
|
|
|
|
154
|
|
|
// If the timestamp is older than 300 seconds request the files new |
155
|
|
|
if ((int)$jsonBlob['timestamp'] > ($this->timeFactory->getTime() - self::INVALIDATE_AFTER_SECONDS)) { |
156
|
|
|
return $jsonBlob['data']; |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
if (isset($jsonBlob['ETag'])) { |
160
|
|
|
$ETag = $jsonBlob['ETag']; |
161
|
|
|
$content = json_encode($jsonBlob['data']); |
162
|
|
|
} |
163
|
|
|
} |
164
|
|
|
} |
165
|
|
|
} catch (NotFoundException $e) { |
166
|
|
|
// File does not already exists |
167
|
|
|
$file = $rootFolder->newFile($this->fileName); |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
// Refresh the file content |
171
|
|
|
try { |
172
|
|
|
$responseJson = $this->fetch($ETag, $content); |
173
|
|
|
$file->putContent(json_encode($responseJson)); |
174
|
|
|
return json_decode($file->getContent(), true)['data']; |
175
|
|
|
} catch (ConnectException $e) { |
176
|
|
|
$this->logger->logException($e, ['app' => 'appstoreFetcher', 'level' => ILogger::INFO, 'message' => 'Could not connect to appstore']); |
177
|
|
|
return []; |
178
|
|
|
} catch (\Exception $e) { |
179
|
|
|
$this->logger->logException($e, ['app' => 'appstoreFetcher', 'level' => ILogger::INFO]); |
180
|
|
|
return []; |
181
|
|
|
} |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
/** |
185
|
|
|
* Get the currently Nextcloud version |
186
|
|
|
* @return string |
187
|
|
|
*/ |
188
|
|
|
protected function getVersion() { |
189
|
|
|
if ($this->version === null) { |
190
|
|
|
$this->version = $this->config->getSystemValue('version', '0.0.0'); |
191
|
|
|
} |
192
|
|
|
return $this->version; |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
/** |
196
|
|
|
* Set the current Nextcloud version |
197
|
|
|
* @param string $version |
198
|
|
|
*/ |
199
|
|
|
public function setVersion(string $version) { |
200
|
|
|
$this->version = $version; |
201
|
|
|
} |
202
|
|
|
|
203
|
|
|
/** |
204
|
|
|
* Get the currently Nextcloud update channel |
205
|
|
|
* @return string |
206
|
|
|
*/ |
207
|
|
|
protected function getChannel() { |
208
|
|
|
if ($this->channel === null) { |
209
|
|
|
$this->channel = \OC_Util::getChannel(); |
210
|
|
|
} |
211
|
|
|
return $this->channel; |
212
|
|
|
} |
213
|
|
|
|
214
|
|
|
/** |
215
|
|
|
* Set the current Nextcloud update channel |
216
|
|
|
* @param string $channel |
217
|
|
|
*/ |
218
|
|
|
public function setChannel(string $channel) { |
219
|
|
|
$this->channel = $channel; |
220
|
|
|
} |
221
|
|
|
} |
222
|
|
|
|