Termiante Function

ToDo
- Suspend Function
- Unsuspend Function
This commit is contained in:
NekoMonci12
2025-02-13 07:38:37 +07:00
parent 2d902ff624
commit 37a01b4938

View File

@@ -212,38 +212,35 @@ function licensegate_TestConnection(array $params) {
]; ];
} }
function licensegate_GetKey($licenseKey) { function licensegate_GetKey($params, $licenseKey) {
$target = '/key/' . $licenseKey; $target = '/key/' . $licenseKey;
$err = "";
try { try {
$response = licensegate_API($params, $target, [], 'GET'); $response = licensegate_API($params, $target, [], 'GET');
if ($response['status_code'] !== 200) { // Log raw response for debugging
$status_code = $response['status_code']; logModuleCall("License-Gate", "GetKey Raw Response", json_encode($response), "");
$solutions = [
400 => "Bad request - Check the input data.", // Ensure response is an array
401 => "Unauthorized - Check authentication credentials.", if (!is_array($response)) {
404 => "Not found - Ensure the licenseKey exists.", $response = json_decode(json_encode($response), true); // Convert object to array
500 => "Server error - Try again later."
];
$err = "Invalid status_code received: " . $status_code . ". Possible solutions: "
. ($solutions[$status_code] ?? "None.");
} elseif ($response['meta']['pagination']['count'] === 0) {
$err = "Authentication successful, but no nodes are available.";
} }
if (!isset($response['id'])) {
return ["success" => false, "error" => "ID not found in response"];
}
return [
"success" => true,
"id" => $response['id'],
];
} catch (Exception $e) { } catch (Exception $e) {
licensegate_Error(__FUNCTION__, [], $e); logModuleCall("License-Gate", "GetKey Exception", $e->getMessage(), $e->getTraceAsString());
$err = $e->getMessage(); return ["success" => false, "error" => $e->getMessage()];
} }
$jsonData = json_decode($response, true);
return [
"success" => $err === "",
"error" => $err,
"id" => $jsonData['id'] ?? null,
];
} }
function licensegate_GenerateKey($inputString) { function licensegate_GenerateKey($inputString) {
$licenseHashed = md5($inputString); $licenseHashed = md5($inputString);
$licenseObfuscated = substr($licenseHashed, 0, 12) $licenseObfuscated = substr($licenseHashed, 0, 12)
@@ -302,62 +299,80 @@ function licensegate_CreateAccount(array $params)
function licensegate_SuspendAccount(array $params) function licensegate_SuspendAccount(array $params)
{ {
try { try {
// Generate key input
$inputString = $params['serviceid'] . '-' . $params['username']; $inputString = $params['serviceid'] . '-' . $params['username'];
$keyResponse = licensegate_GetKey(licensegate_GenerateKey($inputString)); $keyResponse = licensegate_GetKey($params, licensegate_GenerateKey($inputString));
if ($keyResponse['success']) {
$endpoint = '/' . $keyResponse['id']; // Validate API response
} else { if (!isset($keyResponse['success']) || !$keyResponse['success'] || !isset($keyResponse['id'])) {
throw new Exception("Failed to check account. Status code: {$response['status_code']}"); throw new Exception("Failed to check account. Response: " . json_encode($keyResponse));
} }
// Construct endpoint
$endpoint = '/' . $keyResponse['id'];
// Data payload
$data = [ $data = [
"active" => false, "active" => false,
]; ];
// Make API request
$response = licensegate_API($params, $endpoint, $data, "PATCH"); $response = licensegate_API($params, $endpoint, $data, "PATCH");
if ($response['status_code'] !== 200) { // Validate API response
throw new Exception("Failed to suspend account. Status code: {$response['status_code']}"); if (!isset($response['status_code']) || $response['status_code'] !== 200) {
throw new Exception("Failed to suspend account. Response: " . json_encode($response));
} }
} catch (Exception $e) { } catch (Exception $e) {
return $e->getMessage(); return "Error: " . $e->getMessage();
} }
return 'success'; return "success";
} }
function licensegate_UnsuspendAccount(array $params) function licensegate_UnsuspendAccount(array $params)
{ {
try { try {
// Generate key input
$inputString = $params['serviceid'] . '-' . $params['username']; $inputString = $params['serviceid'] . '-' . $params['username'];
$keyResponse = licensegate_GetKey(licensegate_GenerateKey($inputString)); $keyResponse = licensegate_GetKey($params, licensegate_GenerateKey($inputString));
if ($keyResponse['success']) {
$endpoint = '/' . $keyResponse['id']; // Validate API response
} else { if (!isset($keyResponse['success']) || !$keyResponse['success'] || !isset($keyResponse['id'])) {
throw new Exception("Failed to check account. Status code: {$response['status_code']}"); throw new Exception("Failed to check account. Response: " . json_encode($keyResponse));
} }
// Construct endpoint
$endpoint = '/' . $keyResponse['id'];
// Data payload
$data = [ $data = [
"active" => true, "active" => true,
]; ];
// Make API request
$response = licensegate_API($params, $endpoint, $data, "PATCH"); $response = licensegate_API($params, $endpoint, $data, "PATCH");
if ($response['status_code'] !== 200) { // Validate API response
throw new Exception("Failed to unsuspend account. Status code: {$response['status_code']}"); if (!isset($response['status_code']) || $response['status_code'] !== 200) {
throw new Exception("Failed to suspend account. Response: " . json_encode($response));
} }
} catch (Exception $e) { } catch (Exception $e) {
return $e->getMessage(); return "Error: " . $e->getMessage();
} }
return 'success'; return "success";
} }
function licensegate_TerminateAccount(array $params) function licensegate_TerminateAccount(array $params)
{ {
try { try {
$inputString = $params['serviceid'] . '-' . $params['username']; $inputString = $params['serviceid'] . '-' . $params['username'];
$keyResponse = licensegate_GetKey(licensegate_GenerateKey($inputString)); $keyResponse = licensegate_GetKey($params, licensegate_GenerateKey($inputString));
if ($keyResponse['success']) { if ($keyResponse['success']) {
$endpoint = '/' . $keyResponse['id']; $endpoint = '/' . $keyResponse['id'];
} else { } else {
@@ -399,7 +414,8 @@ function licensegate_ChangePackage(array $params)
$rinterval = licensegate_GetOption($params, 'rinterval', 'HOUR'); $rinterval = licensegate_GetOption($params, 'rinterval', 'HOUR');
$inputString = $params['serviceid'] . '-' . $params['username']; $inputString = $params['serviceid'] . '-' . $params['username'];
$keyResponse = licensegate_GetKey(licensegate_GenerateKey($inputString)); $keyResponse = licensegate_GetKey($params, licensegate_GenerateKey($inputString));
if ($keyResponse['success']) { if ($keyResponse['success']) {
$endpoint = '/' . $keyResponse['id']; $endpoint = '/' . $keyResponse['id'];
} else { } else {