Skip to content

Commit

Permalink
Merge pull request #6 from plesk/HOTFIX-product-upgrades
Browse files Browse the repository at this point in the history
[HOTFIX] Product upgrades
  • Loading branch information
laurisb authored Dec 14, 2022
2 parents 2daf2a1 + 4abe587 commit b5d8aa3
Show file tree
Hide file tree
Showing 8 changed files with 1,768 additions and 131 deletions.
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@
"beberlei/assert": "^3.3"
},
"require-dev": {
"guzzlehttp/guzzle": "^7.0.1"
"guzzlehttp/guzzle": "^7.0.1",
"illuminate/database": "^7"
},
"config": {
"platform": {
Expand Down
1,760 changes: 1,646 additions & 114 deletions composer.lock

Large diffs are not rendered by default.

25 changes: 24 additions & 1 deletion hooks.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,27 @@

// Copyright 2022. Plesk International GmbH. All rights reserved.

// This file is only kept here to overwrite older versions to prevent module errors
require __DIR__ . '/vendor/autoload.php';

use WHMCS\Database\Capsule;
use WHMCS\Module\Server\Plesk360Monitoring\Constants;
use WHMCS\Module\Server\Plesk360Monitoring\CustomFields;

add_hook('ProductEdit', 1, function (array $vars) {
$productId = $vars['pid'] ?? null;
$serverType = $vars['servertype'] ?? null;

if (($productId === null) || ($serverType === null)) {
return [];
}

if ($serverType !== Constants::MODULE_NAME) {
return [];
}

$customFields = new CustomFields(Capsule::connection());

$customFields->addMissingProductCustomFields($productId);

return [];
});
10 changes: 10 additions & 0 deletions lib/Constants.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

// Copyright 2022. Plesk International GmbH. All rights reserved.

namespace WHMCS\Module\Server\Plesk360Monitoring;

final class Constants
{
public const MODULE_NAME = 'p360monitoring';
}
67 changes: 64 additions & 3 deletions lib/CustomFields.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,70 @@

namespace WHMCS\Module\Server\Plesk360Monitoring;

use Illuminate\Database\ConnectionInterface;

final class CustomFields
{
public const KEY_ID = 'keyId';
public const ACTIVATION_CODE = 'activationCode';
public const ACTIVATION_URL = 'activationUrl';
private ConnectionInterface $connection;

public function __construct(ConnectionInterface $connection)
{
$this->connection = $connection;
}

public function addMissingProductCustomFields(int $productId): void
{
$rows = $this->connection->table('tblcustomfields')->where('relid', '=', $productId)->get();

$hasFieldKeyId = false;
$hasFieldActivationCode = false;
$hasFieldActivationUrl = false;

foreach ($rows as $row) {
if ($row->fieldname === ServiceProperties::KEY_ID) {
$hasFieldKeyId = true;

continue;
}

if ($row->fieldname === ServiceProperties::ACTIVATION_CODE) {
$hasFieldActivationCode = true;

continue;
}

if ($row->fieldname === ServiceProperties::ACTIVATION_URL) {
$hasFieldActivationUrl = true;

continue;
}
}

$curDate = date('Y-m-d H:i:s');

if (!$hasFieldKeyId) {
$this->addProductCustomField($productId, ServiceProperties::KEY_ID, $curDate);
}

if (!$hasFieldActivationCode) {
$this->addProductCustomField($productId, ServiceProperties::ACTIVATION_CODE, $curDate);
}

if (!$hasFieldActivationUrl) {
$this->addProductCustomField($productId, ServiceProperties::ACTIVATION_URL, $curDate);
}
}

private function addProductCustomField(int $productId, string $fieldName, string $curDate): void
{
$this->connection->table('tblcustomfields')->insert([
'type' => 'product',
'relid' => $productId,
'fieldname' => $fieldName,
'fieldtype' => 'text',
'adminonly' => 'on',
'created_at' => $curDate,
'updated_at' => $curDate,
]);
}
}
4 changes: 1 addition & 3 deletions lib/Logger.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,14 @@

final class Logger
{
private const MODULE_NAME = 'p360monitoring';

/**
* @param string|array<mixed> $request
* @param string|array<mixed> $response
*/
public static function log(string $function, $request, $response): void
{
logModuleCall(
self::MODULE_NAME,
Constants::MODULE_NAME,
$function,
$request,
$response
Expand Down
12 changes: 12 additions & 0 deletions lib/ServiceProperties.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

// Copyright 2022. Plesk International GmbH. All rights reserved.

namespace WHMCS\Module\Server\Plesk360Monitoring;

final class ServiceProperties
{
public const KEY_ID = 'keyId';
public const ACTIVATION_CODE = 'activationCode';
public const ACTIVATION_URL = 'activationUrl';
}
18 changes: 9 additions & 9 deletions p360monitoring.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@

require __DIR__ . '/vendor/autoload.php';

use WHMCS\Module\Server\Plesk360Monitoring\CustomFields;
use WHMCS\Module\Server\Plesk360Monitoring\KaApi;
use WHMCS\Module\Server\Plesk360Monitoring\Logger;
use WHMCS\Module\Server\Plesk360Monitoring\ServerOptions;
use WHMCS\Module\Server\Plesk360Monitoring\ServiceProperties;
use WHMCS\Module\Server\Plesk360Monitoring\PlanCollection;
use WHMCS\Module\Server\Plesk360Monitoring\Plans\ProPlan;
use WHMCS\Module\Server\Plesk360Monitoring\ProductOptions;
Expand Down Expand Up @@ -91,7 +91,7 @@ function p360monitoring_ClientArea(array $params): string
global $CONFIG;

$kaApi = p360monitoring_getKaApiClient($params);
$keyId = $params['customfields'][CustomFields::KEY_ID];
$keyId = $params['model']->serviceProperties->get(ServiceProperties::KEY_ID);
$translator = Translator::getInstance($CONFIG);

try {
Expand Down Expand Up @@ -132,9 +132,9 @@ function p360monitoring_CreateAccount(array $params): string
$domain = $params[ProductOptions::DOMAIN];

$params['model']->serviceProperties->save([
CustomFields::KEY_ID => $license->getKeyIdentifiers()->getKeyId(),
CustomFields::ACTIVATION_CODE => $license->getKeyIdentifiers()->getActivationCode(),
CustomFields::ACTIVATION_URL => UrlHelper::getActivationUrl($license, $domain),
ServiceProperties::KEY_ID => $license->getKeyIdentifiers()->getKeyId(),
ServiceProperties::ACTIVATION_CODE => $license->getKeyIdentifiers()->getActivationCode(),
ServiceProperties::ACTIVATION_URL => UrlHelper::getActivationUrl($license, $domain),
]);

return 'success';
Expand All @@ -148,7 +148,7 @@ function p360monitoring_CreateAccount(array $params): string
function p360monitoring_SuspendAccount(array $params): string
{
try {
$keyId = $params['customfields'][CustomFields::KEY_ID];
$keyId = $params['model']->serviceProperties->get(ServiceProperties::KEY_ID);
$kaApi = p360monitoring_getKaApiClient($params);

$kaApi->suspendLicense($keyId);
Expand All @@ -164,7 +164,7 @@ function p360monitoring_SuspendAccount(array $params): string
function p360monitoring_UnsuspendAccount(array $params): string
{
try {
$keyId = $params['customfields'][CustomFields::KEY_ID];
$keyId = $params['model']->serviceProperties->get(ServiceProperties::KEY_ID);
$kaApi = p360monitoring_getKaApiClient($params);

$kaApi->resumeLicense($keyId);
Expand All @@ -180,7 +180,7 @@ function p360monitoring_UnsuspendAccount(array $params): string
function p360monitoring_TerminateAccount(array $params): string
{
try {
$keyId = $params['customfields'][CustomFields::KEY_ID];
$keyId = $params['model']->serviceProperties->get(ServiceProperties::KEY_ID);
$kaApi = p360monitoring_getKaApiClient($params);

$kaApi->terminateLicense($keyId);
Expand All @@ -196,7 +196,7 @@ function p360monitoring_TerminateAccount(array $params): string
function p360monitoring_ChangePackage(array $params): string
{
try {
$keyId = $params['customfields'][CustomFields::KEY_ID];
$keyId = $params['model']->serviceProperties->get(ServiceProperties::KEY_ID);
$servers = ProductOptions::serverAllowance($params);
$websites = ProductOptions::websiteAllowance($params);
$plans = new PlanCollection();
Expand Down

0 comments on commit b5d8aa3

Please sign in to comment.