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
|
<?php
/**
* Matomo - free/libre analytics platform
*
* @link https://matomo.org
* @license https://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
*/
namespace Piwik\Plugins\Marketplace;
use Exception;
use Piwik\Piwik;
use Piwik\Plugin\Manager as PluginManager;
use Piwik\Plugins\Marketplace\Api\Client;
use Piwik\Plugins\Marketplace\Api\Service;
use Piwik\Plugins\Marketplace\Plugins\InvalidLicenses;
use Piwik\Plugins\Marketplace\PluginTrial\Service as PluginTrialService;
use Piwik\Plugins\UsersManager\SystemSettings;
use Piwik\Plugins\UsersManager\Validators\AllowedEmailDomain;
use Piwik\Plugins\UsersManager\Validators\Email;
use Piwik\Validators\Exception as ValidatorException;
use Piwik\Validators\NotEmpty;
/**
* The Marketplace API lets you manage your license key so you can download & install in one-click <a target="_blank" rel="noreferrer" href="https://matomo.org/recommends/premium-plugins/">paid premium plugins</a> you have subscribed to.
*
* @method static \Piwik\Plugins\Marketplace\API getInstance()
*/
class API extends \Piwik\Plugin\API
{
/**
* @var Client
*/
private $marketplaceClient;
/**
* @var Service
*/
private $marketplaceService;
/**
* @var InvalidLicenses
*/
private $expired;
/**
* @var PluginManager
*/
private $pluginManager;
/**
* @var Environment
*/
private $environment;
/**
* @var PluginTrialService
*/
private $pluginTrialService;
public function __construct(
Service $service,
Client $client,
InvalidLicenses $expired,
PluginManager $pluginManager,
Environment $environment,
PluginTrialService $pluginTrialService
) {
$this->marketplaceService = $service;
$this->marketplaceClient = $client;
$this->expired = $expired;
$this->pluginManager = $pluginManager;
$this->environment = $environment;
$this->pluginTrialService = $pluginTrialService;
}
/**
* @throws Service\Exception If the marketplace request failed
*
* @internal
*/
public function createAccount(string $email): bool
{
Piwik::checkUserHasSuperUserAccess();
$licenseKey = (new LicenseKey())->get();
if (!empty($licenseKey)) {
// not translated to allow special handling in frontend
throw new Exception('Marketplace_CreateAccountErrorLicenseExists');
}
$notEmptyValidator = new NotEmpty();
$notEmptyValidator->validate($email);
try {
$emailValidator = new Email();
$emailValidator->validate($email);
} catch (ValidatorException $e) {
// rethrow with changed message
throw new ValidatorException(
Piwik::translate('Marketplace_CreateAccountErrorEmailInvalid', $email)
);
}
// Ensure the provided email uses a domain that is allowed (if configured)
$systemSettings = new SystemSettings();
$allowedDomainsValidator = new AllowedEmailDomain($systemSettings);
$allowedDomainsValidator->validate($email);
try {
$result = $this->marketplaceService->fetch(
'createAccount',
[],
[
'email' => $email,
],
true,
false
);
} catch (Service\Exception $e) {
// not translated to allow special handling in frontend
throw new Exception('Marketplace_CreateAccountErrorAPI');
}
$this->marketplaceClient->clearAllCacheEntries();
$licenseKey = trim($result['data']['license_key'] ?? '');
$status = $result['status'];
if (200 !== $status || empty($licenseKey)) {
switch ($status) {
case 400:
$message = Piwik::translate('Marketplace_CreateAccountErrorAPIEmailInvalid');
break;
case 409:
$message = Piwik::translate('Marketplace_CreateAccountErrorAPIEmailExists', $email);
break;
default:
// not translated to allow special handling in frontend
$message = 'Marketplace_CreateAccountErrorAPI';
break;
}
throw new Exception($message);
}
$this->setLicenseKey($licenseKey);
return true;
}
/**
* Deletes an existing license key if one is set.
*
* @return bool
*/
public function deleteLicenseKey()
{
Piwik::checkUserHasSuperUserAccess();
$this->setLicenseKey(null);
return true;
}
/**
*
*
* @unsanitized
* @internal
*/
public function requestTrial(string $pluginName): bool
{
Piwik::checkUserIsNotAnonymous();
if (Piwik::hasUserSuperUserAccess()) {
throw new Exception('Cannot request trial as a super user');
}
if (!$this->pluginManager->isValidPluginName($pluginName)) {
throw new Exception('Invalid plugin name given ' . $pluginName);
}
$pluginInfo = $this->marketplaceClient->getPluginInfo($pluginName);
if (empty($pluginInfo['name'])) {
throw new Exception('Unable to find plugin with given name: ' . $pluginName);
}
$this->pluginTrialService->request($pluginInfo['name'], $pluginInfo['displayName'] ?: $pluginInfo['name']);
return true;
}
/**
*
* @throws Service\Exception If the marketplace request failed
*
* @internal
*/
public function startFreeTrial(string $pluginName): bool
{
Piwik::checkUserHasSuperUserAccess();
if (!$this->pluginManager->isValidPluginName($pluginName)) {
throw new Exception('Invalid plugin name given');
}
$licenseKey = (new LicenseKey())->get();
$this->marketplaceService->authenticate($licenseKey);
$result = $this->marketplaceService->fetch(
'plugins/' . $pluginName . '/freeTrial',
[
'num_users' => $this->environment->getNumUsers(),
'num_websites' => $this->environment->getNumWebsites(),
],
[],
true,
false
);
$this->marketplaceClient->clearAllCacheEntries();
if (
201 !== $result['status']
|| !is_string($result['data'])
|| '' !== trim($result['data'])
) {
// We expect an exact empty 201 response from this API
// Anything different should be an error
throw new Exception(Piwik::translate('Marketplace_TrialStartErrorAPI'));
}
return true;
}
/**
* Saves the given license key in case the key is actually valid (exists on the Matomo Marketplace and is not
* yet expired).
*
* @param string $licenseKey
* @return bool
*
* @throws Exception In case of an invalid license key
* @throws Service\Exception In case of any network problems
*/
public function saveLicenseKey($licenseKey)
{
Piwik::checkUserHasSuperUserAccess();
$licenseKey = trim($licenseKey);
// we are currently using the Marketplace service directly to 1) change LicenseKey and 2) not use any cache
$this->marketplaceService->authenticate($licenseKey);
try {
$consumer = $this->marketplaceService->fetch('consumer/validate', array());
} catch (Api\Service\Exception $e) {
if ($e->getCode() === Api\Service\Exception::HTTP_ERROR) {
throw $e;
}
$consumer = array();
}
if (empty($consumer['isValid'])) {
throw new Exception(Piwik::translate('Marketplace_ExceptionLinceseKeyIsNotValid'));
}
$this->setLicenseKey($licenseKey);
return true;
}
private function setLicenseKey($licenseKey)
{
$key = new LicenseKey();
$key->set($licenseKey);
$this->marketplaceClient->clearAllCacheEntries();
$this->expired->clearCache();
}
}
|