Skip to content

Commit

Permalink
Adopt new license server (#9)
Browse files Browse the repository at this point in the history
  • Loading branch information
budziam authored Oct 22, 2018
1 parent e83ecaa commit 2d02e6f
Show file tree
Hide file tree
Showing 30 changed files with 293 additions and 154 deletions.
2 changes: 2 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
],
"require": {
"php": ">=5.6",
"ext-curl": "*",
"ext-json": "*",
"vlucas/phpdotenv": "^2.4",
"illuminate/container": "^5.4",
"symfony/console": "^3.4",
Expand Down
2 changes: 1 addition & 1 deletion confidential/.env.example
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
DB_HOST=localhost
DB_DATABASE=sklep_sms
DB_USERNAME=root
DB_PASSWORD=secure
DB_PASSWORD=secure
16 changes: 14 additions & 2 deletions includes/Cache/CachingRequester.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ public function __construct(CacheInterface $cache)
}

/**
* @param string $cacheKey
* @param int $ttl
* @param string $cacheKey
* @param int $ttl
* @param Closure $requestCaller
* @return mixed
* @throws \Psr\SimpleCache\InvalidArgumentException
Expand All @@ -45,13 +45,25 @@ public function load($cacheKey, $ttl, $requestCaller)
return $entity->value;
}

/**
* @param string $cacheKey
* @param callable $requestCaller
* @return mixed
* @throws RequestException
* @throws \Psr\SimpleCache\InvalidArgumentException
*/
protected function fetchAndCache($cacheKey, $requestCaller)
{
$response = $this->fetch($requestCaller);
$this->cache->set($cacheKey, $response, static::HARD_TTL);
return $response;
}

/**
* @param callable $requestCaller
* @return mixed
* @throws RequestException
*/
protected function fetch($requestCaller)
{
$response = call_user_func($requestCaller);
Expand Down
16 changes: 16 additions & 0 deletions includes/Exceptions/InvalidResponse.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php
namespace App\Exceptions;

use App\Requesting\Response;
use Exception;

class InvalidResponse extends Exception
{
/** @var Response */
public $response;

public function __construct(Response $response)
{
$this->response = $response;
}
}
13 changes: 3 additions & 10 deletions includes/Kernels/InstallFullKernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,13 +49,8 @@ public function run(Request $request)
$warnings = [];

// Licencja ID
if (!strlen($_POST['license_id'])) {
$warnings['license_id'][] = "Nie podano ID licencji.";
}

// Licencja hasło
if (!strlen($_POST['license_password'])) {
$warnings['license_password'][] = "Nie podano hasła licencji.";
if (!strlen($_POST['license_token'])) {
$warnings['license_token'][] = "Nie podano tokenu licencji.";
}

// Admin nick
Expand Down Expand Up @@ -107,9 +102,7 @@ public function run(Request $request)

$installManager->start();

$migrator->install(
$_POST['license_id'], $_POST['license_password'], $_POST['admin_username'], $_POST['admin_password']
);
$migrator->install($_POST['license_token'], $_POST['admin_username'], $_POST['admin_password']);

$envCreator->create($_POST['db_host'], $_POST['db_port'], $_POST['db_db'], $_POST['db_user'], $_POST['db_password']);

Expand Down
28 changes: 15 additions & 13 deletions includes/Kernels/JsonHttpAdminKernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -333,15 +333,15 @@ public function run(Request $request)
$sms_service = $_POST['sms_service'];
$transfer_service = $_POST['transfer_service'];
$currency = $_POST['currency'];
$shopName = $_POST['shop_name'];
$shop_url = $_POST['shop_url'];
$sender_email = $_POST['sender_email'];
$sender_email_name = $_POST['sender_email_name'];
$signature = $_POST['signature'];
$vat = $_POST['vat'];
$contact = $_POST['contact'];
$row_limit = $_POST['row_limit'];
$license_login = $_POST['license_login'];
$license_password = $_POST['license_password'];
$licenseToken = $_POST['license_token'];
$cron = $_POST['cron'];
$language = escape_filename($_POST['language']);
$theme = escape_filename($_POST['theme']);
Expand Down Expand Up @@ -377,7 +377,7 @@ public function run(Request $request)
}

// Email dla automatu
if ($warning = check_for_warnings("email", $sender_email)) {
if (strlen($sender_email) && $warning = check_for_warnings("email", $sender_email)) {
$warnings['sender_email'] = array_merge((array)$warnings['sender_email'], $warning);
}

Expand Down Expand Up @@ -426,9 +426,9 @@ public function run(Request $request)
json_output("warnings", $lang->translate('form_wrong_filled'), 0, $data);
}

if ($license_password) {
$set_license_password = $db->prepare("WHEN 'license_password' THEN '%s' ", [md5($license_password)]);
$key_license_password = ",'license_password'";
if ($licenseToken) {
$setLicenseToken = $db->prepare("WHEN 'license_password' THEN '%s' ", [$licenseToken]);
$keyLicenseToken = ",'license_password'";
}

// Edytuj ustawienia
Expand All @@ -438,14 +438,14 @@ public function run(Request $request)
"WHEN 'sms_service' THEN '%s' " .
"WHEN 'transfer_service' THEN '%s' " .
"WHEN 'currency' THEN '%s' " .
"WHEN 'shop_name' THEN '%s' " .
"WHEN 'shop_url' THEN '%s' " .
"WHEN 'sender_email' THEN '%s' " .
"WHEN 'sender_email_name' THEN '%s' " .
"WHEN 'signature' THEN '%s' " .
"WHEN 'vat' THEN '%.2f' " .
"WHEN 'contact' THEN '%s' " .
"WHEN 'row_limit' THEN '%s' " .
"WHEN 'license_login' THEN '%s' " .
"WHEN 'cron_each_visit' THEN '%d' " .
"WHEN 'user_edit_service' THEN '%d' " .
"WHEN 'theme' THEN '%s' " .
Expand All @@ -454,23 +454,23 @@ public function run(Request $request)
"WHEN 'delete_logs' THEN '%d' " .
"WHEN 'google_analytics' THEN '%s' " .
"WHEN 'gadugadu' THEN '%s' " .
$set_license_password .
$setLicenseToken .
"END " .
"WHERE `key` IN ( 'sms_service','transfer_service','currency','shop_url','sender_email','sender_email_name','signature','vat'," .
"'contact','row_limit','license_login','cron_each_visit','user_edit_service','theme','language','date_format','delete_logs'," .
"'google_analytics','gadugadu'{$key_license_password} )",
"WHERE `key` IN ( 'sms_service','transfer_service','currency','shop_name','shop_url','sender_email','sender_email_name','signature','vat'," .
"'contact','row_limit','cron_each_visit','user_edit_service','theme','language','date_format','delete_logs'," .
"'google_analytics','gadugadu'{$keyLicenseToken} )",
[
$sms_service,
$transfer_service,
$currency,
$shopName,
$shop_url,
$sender_email,
$sender_email_name,
$signature,
$vat,
$contact,
$row_limit,
$license_login,
$cron,
$_POST['user_edit_service'],
$theme,
Expand Down Expand Up @@ -768,7 +768,9 @@ public function run(Request $request)
if ($action == "server_add") {
/** @var ServerRepository $serverRepository */
$serverRepository = $this->app->make(ServerRepository::class);
$server = $serverRepository->create($_POST['name'], $_POST['ip'], $_POST['port'], $_POST['sms_service']);
$server = $serverRepository->create(
$_POST['name'], $_POST['ip'], $_POST['port'], $_POST['sms_service']
);
$server_id = $server->getId();
} elseif ($action == "server_edit") {
$db->query($db->prepare(
Expand Down
10 changes: 3 additions & 7 deletions includes/Kernels/JsonHttpKernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -571,13 +571,9 @@ public function run(Request $request)
}

if ($settings['user_edit_service'] && object_implements($service_module, "IService_UserOwnServicesEdit")) {
$button_edit = create_dom_element("img", "", [
'class' => "edit_row",
'src' => "images/pencil.png",
'title' => $lang->translate('edit'),
'style' => [
'height' => '24px',
],
$button_edit = create_dom_element("button", $lang->translate('edit'), [
'class' => "button edit_row",
'type' => 'button',
]);
}

Expand Down
119 changes: 75 additions & 44 deletions includes/License.php
Original file line number Diff line number Diff line change
@@ -1,39 +1,38 @@
<?php
namespace App;

use App\Cache\CacheEnum;
use App\Cache\CachingRequester;
use App\Exceptions\LicenseException;
use App\Exceptions\InvalidResponse;
use App\Exceptions\RequestException;
use App\Requesting\Requester;
use Symfony\Component\HttpFoundation\Request;

class License
{
const CACHE_TTL = 20 * 60;
const CACHE_TTL = 10 * 60;

/** @var Translator */
protected $lang;

/** @var Settings */
protected $settings;

/** @var string */
protected $message;

/** @var string */
protected $expires;

/** @var string */
protected $page;

/** @var string */
protected $footer;

/** @var Requester */
protected $requester;

/** @var CachingRequester */
protected $cachingRequester;

/** @var int */
protected $externalLicenseId;

/** @var int */
protected $expiresAt;

/** @var string */
protected $footer;

public function __construct(
Translator $translator,
Settings $settings,
Expand All @@ -46,27 +45,22 @@ public function __construct(
$this->cachingRequester = $cachingRequester;
}

/**
* @throws InvalidResponse
* @throws RequestException
*/
public function validate()
{
try {
$response = $this->loadLicense();
} catch (RequestException $e) {
throw new LicenseException('', 0, $e);
}
$response = $this->loadLicense();

if (!isset($response['text'])) {
throw new LicenseException();
}

$this->message = $response['text'];
$this->expires = array_get($response, 'expire');
$this->page = array_get($response, 'page');
$this->externalLicenseId = array_get($response, 'id');
$this->expiresAt = array_get($response, 'expires_at');
$this->footer = array_get($response, 'f');
}

public function isValid()
{
return $this->message === "logged_in";
return $this->externalLicenseId !== null;
}

public function getExpires()
Expand All @@ -75,41 +69,78 @@ public function getExpires()
return $this->lang->translate('never');
}

return date($this->settings['date_format'], $this->expires);
return date($this->settings['date_format'], $this->expiresAt);
}

public function isForever()
public function getExternalId()
{
return $this->expires == -1;
return $this->externalLicenseId;
}

public function getPage()
public function isForever()
{
return $this->page;
return $this->expiresAt === null;
}

public function getFooter()
{
return $this->footer;
}

/**
* @return array
* @throws InvalidResponse
* @throws RequestException
*/
protected function loadLicense()
{
// TODO Cache successful response
return $this->request();
return $this->cachingRequester->load(CacheEnum::LICENSE, static::CACHE_TTL, function () {
return $this->request();
});
}

/**
* @return array
* @throws InvalidResponse
* @throws RequestException
*/
protected function request()
{
$response = $this->requester->get('http://license.sklep-sms.pl/license.php', [
'action' => 'login_web',
'lid' => $this->settings['license_login'],
'lpa' => $this->settings['license_password'],
'name' => $this->settings['shop_url'],
'version' => app()->version(),
'language' => $this->lang->getCurrentLanguage(),
]);

return $response ? $response->json() : null;
$shopUrl = $this->getShopUrl();

$response = $this->requester->post(
'http://license.sklep-sms.pl/v1/authorization/web',
[
'url' => $shopUrl,
'name' => $this->settings['shop_name'] ?: $shopUrl,
'version' => app()->version(),
'language' => $this->lang->getCurrentLanguage(),
],
[
'Authorization' => $this->settings['license_password'],
]
);

if (!$response) {
throw new RequestException();
}

if (!$response->isOk()) {
throw new InvalidResponse($response);
}

return $response->json();
}

private function getShopUrl()
{
if ($this->settings['shop_url']) {
return $this->settings['shop_url'];
}

/** @var Request $request */
$request = app()->make(Request::class);

return $request->getSchemeAndHttpHost();
}
}
Loading

0 comments on commit 2d02e6f

Please sign in to comment.