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
|
<?php
/**
* Piwik - free/libre analytics platform
*
* @link https://matomo.org
* @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
*
*/
namespace Piwik\Plugins\Marketplace\Api;
use Piwik\Cache;
use Piwik\Common;
use Piwik\Container\StaticContainer;
use Piwik\Filesystem;
use Piwik\Http;
use Piwik\Piwik;
use Piwik\Plugin;
use Piwik\Plugins\Marketplace\Environment;
use Piwik\Plugins\Marketplace\Api\Service;
use Piwik\SettingsServer;
use Exception as PhpException;
use Psr\Log\LoggerInterface;
/**
*
*/
class Client
{
const CACHE_TIMEOUT_IN_SECONDS = 3600;
const HTTP_REQUEST_TIMEOUT = 60;
/**
* @var Service
*/
private $service;
/**
* @var Cache\Lazy
*/
private $cache;
/**
* @var Plugin\Manager
*/
private $pluginManager;
/**
* @var LoggerInterface
*/
private $logger;
/**
* @var Environment
*/
private $environment;
public function __construct(Service $service, Cache\Lazy $cache, LoggerInterface $logger, Environment $environment)
{
$this->service = $service;
$this->cache = $cache;
$this->logger = $logger;
$this->pluginManager = Plugin\Manager::getInstance();
$this->environment = $environment;
}
public function setEnvironment($environment)
{
$this->environment = $environment;
}
public function getEnvironment()
{
return $this->environment;
}
public function getPluginInfo($name)
{
$action = sprintf('plugins/%s/info', $name);
$plugin = $this->fetch($action, array());
if (!empty($plugin) && $this->shouldIgnorePlugin($plugin)) {
return;
}
return $plugin;
}
public function getInfo()
{
try {
$info = $this->fetch('info', array());
} catch (Exception $e) {
$info = null;
}
return $info;
}
public function getConsumer()
{
try {
$licenses = $this->fetch('consumer', array());
} catch (Exception $e) {
$licenses = null;
}
return $licenses;
}
public function isValidConsumer()
{
try {
$consumer = $this->fetch('consumer/validate', array());
} catch (Exception $e) {
$consumer = null;
}
return !empty($consumer['isValid']);
}
private function getRandomTmpPluginDownloadFilename()
{
$tmpPluginPath = StaticContainer::get('path.tmp') . '/latest/plugins/';
// we generate a random unique id as filename to prevent any user could possibly download zip directly by
// opening $piwikDomain/tmp/latest/plugins/$pluginName.zip in the browser. Instead we make it harder here
// and try to make sure to delete file in case of any error.
$tmpPluginFolder = Common::generateUniqId();
return $tmpPluginPath . $tmpPluginFolder . '.zip';
}
public function download($pluginOrThemeName)
{
@ignore_user_abort(true);
SettingsServer::setMaxExecutionTime(0);
$downloadUrl = $this->getDownloadUrl($pluginOrThemeName);
if (empty($downloadUrl)) {
return false;
}
// in the beginning we allowed to specify a download path but this way we make sure security is always taken
// care of and we always generate a random download filename.
$target = $this->getRandomTmpPluginDownloadFilename();
Filesystem::deleteFileIfExists($target);
$success = $this->service->download($downloadUrl, $target, static::HTTP_REQUEST_TIMEOUT);
if ($success) {
return $target;
}
return false;
}
/**
* @param \Piwik\Plugin[] $plugins
* @return array|mixed
*/
public function checkUpdates($plugins)
{
$params = array();
foreach ($plugins as $plugin) {
$pluginName = $plugin->getPluginName();
if (!$this->pluginManager->isPluginBundledWithCore($pluginName)) {
$isActivated = $this->pluginManager->isPluginActivated($pluginName);
$params[] = array('name' => $plugin->getPluginName(), 'version' => $plugin->getVersion(), 'activated' => (int) $isActivated);
}
}
if (empty($params)) {
return array();
}
$params = array('plugins' => $params);
$params = array('plugins' => json_encode($params));
$hasUpdates = $this->fetch('plugins/checkUpdates', $params);
if (empty($hasUpdates)) {
return array();
}
return $hasUpdates;
}
/**
* @param \Piwik\Plugin[] $plugins
* @return array
*/
public function getInfoOfPluginsHavingUpdate($plugins)
{
$hasUpdates = $this->checkUpdates($plugins);
$pluginDetails = array();
foreach ($hasUpdates as $pluginHavingUpdate) {
if (empty($pluginHavingUpdate)) {
continue;
}
try {
$plugin = $this->getPluginInfo($pluginHavingUpdate['name']);
} catch (PhpException $e) {
$this->logger->error($e->getMessage());
$plugin = null;
}
if (!empty($plugin)) {
$plugin['repositoryChangelogUrl'] = $pluginHavingUpdate['repositoryChangelogUrl'];
$pluginDetails[] = $plugin;
}
}
return $pluginDetails;
}
public function searchForPlugins($keywords, $query, $sort, $purchaseType)
{
$response = $this->fetch('plugins', array('keywords' => $keywords, 'query' => $query, 'sort' => $sort, 'purchase_type' => $purchaseType));
if (!empty($response['plugins'])) {
return $this->removeNotNeededPluginsFromResponse($response);
}
return array();
}
private function removeNotNeededPluginsFromResponse($response)
{
foreach ($response['plugins'] as $index => $plugin) {
if ($this->shouldIgnorePlugin($plugin)) {
unset($response['plugins'][$index]);
continue;
}
}
return array_values($response['plugins']);
}
private function shouldIgnorePlugin($plugin)
{
return !empty($plugin['isCustomPlugin']);
}
public function searchForThemes($keywords, $query, $sort, $purchaseType)
{
$response = $this->fetch('themes', array('keywords' => $keywords, 'query' => $query, 'sort' => $sort, 'purchase_type' => $purchaseType));
if (!empty($response['plugins'])) {
return $this->removeNotNeededPluginsFromResponse($response);
}
return array();
}
private function fetch($action, $params)
{
ksort($params); // sort params so cache is reused more often even if param order is different
$releaseChannel = $this->environment->getReleaseChannel();
if (!empty($releaseChannel)) {
$params['release_channel'] = $releaseChannel;
}
$params['prefer_stable'] = (int) $this->environment->doesPreferStable();
$params['piwik'] = $this->environment->getPiwikVersion();
$params['php'] = $this->environment->getPhpVersion();
$params['mysql'] = $this->environment->getMySQLVersion();
$params['num_users'] = $this->environment->getNumUsers();
$params['num_websites'] = $this->environment->getNumWebsites();
$query = Http::buildQuery($params);
$cacheId = $this->getCacheKey($action, $query);
$result = $this->cache->fetch($cacheId);
if ($result !== false) {
return $result;
}
try {
$result = $this->service->fetch($action, $params);
} catch (Service\Exception $e) {
throw new Exception($e->getMessage(), $e->getCode());
}
$this->cache->save($cacheId, $result, self::CACHE_TIMEOUT_IN_SECONDS);
return $result;
}
public function clearAllCacheEntries()
{
$this->cache->flushAll();
}
private function getCacheKey($action, $query)
{
$version = $this->service->getVersion();
return sprintf('marketplace.api.%s.%s.%s', $version, str_replace('/', '.', $action), md5($query));
}
/**
* @param $pluginOrThemeName
* @throws Exception
* @return string
*/
public function getDownloadUrl($pluginOrThemeName)
{
$plugin = $this->getPluginInfo($pluginOrThemeName);
if (empty($plugin['versions'])) {
throw new Exception('Plugin has no versions.');
}
$latestVersion = array_pop($plugin['versions']);
$downloadUrl = $latestVersion['download'];
return $this->service->getDomain() . $downloadUrl . '?coreVersion=' . $this->environment->getPiwikVersion();
}
}
|