.
This commit is contained in:
@@ -6,7 +6,7 @@
|
||||
* @copyright Copyright (c) 2024 Kevin Feiler / AVVGO
|
||||
* @license MIT License
|
||||
* @link https://avvgo.de
|
||||
* @version 1.0.0
|
||||
* @version 2.0.0
|
||||
*/
|
||||
|
||||
if (!defined("WHMCS")) {
|
||||
@@ -31,36 +31,21 @@ function keyhelpmanager_MetaData()
|
||||
function keyhelpmanager_ConfigOptions()
|
||||
{
|
||||
return [
|
||||
"hostname" => [
|
||||
"FriendlyName" => "Hostname (IP or FQDN)",
|
||||
"Type" => "text",
|
||||
"Size" => "25",
|
||||
"Description" =>
|
||||
"KeyHelp server hostname or IP address (without http://)",
|
||||
"Required" => true,
|
||||
],
|
||||
"apikey" => [
|
||||
"FriendlyName" => "API Key",
|
||||
"Type" => "password",
|
||||
"Size" => "50",
|
||||
"Description" => "KeyHelp API Key from Settings → API",
|
||||
"Required" => true,
|
||||
],
|
||||
"usessl" => [
|
||||
"FriendlyName" => "Use SSL",
|
||||
"Type" => "yesno",
|
||||
"Default" => "on",
|
||||
"Description" => "Use SSL for API connection (recommended)",
|
||||
],
|
||||
"verify_ssl" => [
|
||||
"FriendlyName" => "Verify SSL Certificate",
|
||||
"Type" => "yesno",
|
||||
"Default" => "on",
|
||||
"Description" => "Verify SSL certificate (disable for self-signed)",
|
||||
"template" => [
|
||||
"FriendlyName" => "KeyHelp Template",
|
||||
"Type" => "dropdown",
|
||||
"Options" => _keyhelpmanager_GetTemplates(),
|
||||
"Description" => "Select a KeyHelp template/plan for this product",
|
||||
"Loader" => "keyhelpmanager_TemplateLoader",
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
function keyhelpmanager_TemplateLoader($params)
|
||||
{
|
||||
return _keyhelpmanager_GetTemplates($params);
|
||||
}
|
||||
|
||||
function _keyhelpmanager_APIRequest(
|
||||
array $params,
|
||||
string $endpoint,
|
||||
@@ -68,16 +53,19 @@ function _keyhelpmanager_APIRequest(
|
||||
array $data = [],
|
||||
) {
|
||||
try {
|
||||
$hostname =
|
||||
$params["serverhostname"] ?? ($params["configoption1"] ?? "");
|
||||
$apiKey = $params["serverpassword"] ?? ($params["configoption2"] ?? "");
|
||||
$useSSL = $params["configoption3"] ?? "on";
|
||||
$verifySSL = $params["configoption4"] ?? "on";
|
||||
// Use centralized server configuration from WHMCS server settings
|
||||
$hostname = $params["serverhostname"] ?? "";
|
||||
$apiKey = $params["serveraccesshash"] ?? "";
|
||||
$useSSL = $params["serversecure"] ?? "on";
|
||||
|
||||
// Verify SSL is enabled by default for security
|
||||
$verifySSL = "on";
|
||||
|
||||
if (empty($hostname) || empty($apiKey)) {
|
||||
return [
|
||||
"success" => false,
|
||||
"error" => "KeyHelp server not configured",
|
||||
"error" =>
|
||||
"KeyHelp server not configured. Please configure the server in WHMCS Setup > Products/Services > Servers",
|
||||
];
|
||||
}
|
||||
|
||||
@@ -165,6 +153,9 @@ function keyhelpmanager_CreateAccount(array $params)
|
||||
return "Domain is required";
|
||||
}
|
||||
|
||||
// Get selected template from config options
|
||||
$templateId = $params["configoption1"] ?? "";
|
||||
|
||||
$accountData = [
|
||||
"login_name" => $username,
|
||||
"password" => $password,
|
||||
@@ -172,8 +163,9 @@ function keyhelpmanager_CreateAccount(array $params)
|
||||
"display_name" => $clientName,
|
||||
];
|
||||
|
||||
if (!empty($params["configoptions"]["KeyHelp Plan"])) {
|
||||
$accountData["plan"] = $params["configoptions"]["KeyHelp Plan"];
|
||||
// Use template if selected
|
||||
if (!empty($templateId)) {
|
||||
$accountData["plan_id"] = $templateId;
|
||||
}
|
||||
|
||||
$result = _keyhelpmanager_APIRequest(
|
||||
@@ -193,7 +185,13 @@ function keyhelpmanager_CreateAccount(array $params)
|
||||
return "User created but no ID returned";
|
||||
}
|
||||
|
||||
// Create domain with template settings
|
||||
$domainData = ["domain_name" => $domain, "user_id" => $userId];
|
||||
|
||||
if (!empty($templateId)) {
|
||||
$domainData["template_id"] = $templateId;
|
||||
}
|
||||
|
||||
$domainResult = _keyhelpmanager_APIRequest(
|
||||
$params,
|
||||
"/domains",
|
||||
@@ -206,10 +204,15 @@ function keyhelpmanager_CreateAccount(array $params)
|
||||
return "Domain creation failed: " . $domainResult["error"];
|
||||
}
|
||||
|
||||
// Save account details including domain ID
|
||||
$domainId = $domainResult["data"]["id"] ?? null;
|
||||
|
||||
_keyhelpmanager_SaveAccountDetails($params["serviceid"], [
|
||||
"username" => $username,
|
||||
"password" => $password,
|
||||
"userid" => $userId,
|
||||
"domainid" => $domainId,
|
||||
"template" => $templateId,
|
||||
]);
|
||||
|
||||
return "success";
|
||||
@@ -295,24 +298,52 @@ function keyhelpmanager_ClientArea(array $params)
|
||||
$params["serviceid"],
|
||||
);
|
||||
$userId = $accountDetails["userid"] ?? null;
|
||||
$hostname =
|
||||
$params["serverhostname"] ?? ($params["configoption1"] ?? "");
|
||||
$useSSL = $params["configoption3"] ?? "on";
|
||||
$domainId = $accountDetails["domainid"] ?? null;
|
||||
$templateId = $accountDetails["template"] ?? null;
|
||||
|
||||
$hostname = $params["serverhostname"] ?? "";
|
||||
$useSSL = $params["serversecure"] ?? "on";
|
||||
$protocol = $useSSL === "on" ? "https" : "http";
|
||||
$loginUrl = sprintf("%s://%s", $protocol, $hostname);
|
||||
|
||||
// Direct panel link
|
||||
$panelUrl = sprintf(
|
||||
"%s://%s/admin/index.php?p=domains&action=edit&id=%s",
|
||||
$protocol,
|
||||
$hostname,
|
||||
$domainId ?? "",
|
||||
);
|
||||
|
||||
$templateVars = [
|
||||
"login_url" => $loginUrl,
|
||||
"panel_url" => $panelUrl,
|
||||
"username" =>
|
||||
$accountDetails["username"] ?? ($params["username"] ?? "N/A"),
|
||||
"password" =>
|
||||
$accountDetails["password"] ??
|
||||
($params["password"] ?? "••••••••"),
|
||||
"domain" => $params["domain"] ?? "N/A",
|
||||
"template_id" => $templateId,
|
||||
"template_name" => "",
|
||||
"stats" => null,
|
||||
"error" => null,
|
||||
];
|
||||
|
||||
// Get template name if template ID exists
|
||||
if (!empty($templateId)) {
|
||||
$templateResult = _keyhelpmanager_APIRequest(
|
||||
$params,
|
||||
"/plans/" . $templateId,
|
||||
"GET",
|
||||
);
|
||||
|
||||
if ($templateResult["success"] && isset($templateResult["data"])) {
|
||||
$templateVars["template_name"] =
|
||||
$templateResult["data"]["name"] ??
|
||||
"Template " . $templateId;
|
||||
}
|
||||
}
|
||||
|
||||
if (!empty($userId)) {
|
||||
$statsResult = _keyhelpmanager_APIRequest(
|
||||
$params,
|
||||
@@ -648,3 +679,103 @@ function _keyhelpmanager_CalculatePercent(int $used, int $limit): float
|
||||
{
|
||||
return $limit > 0 ? round(($used / $limit) * 100, 2) : 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get templates from KeyHelp server
|
||||
*/
|
||||
function _keyhelpmanager_GetTemplates($params = null)
|
||||
{
|
||||
try {
|
||||
// Get server configuration from module settings or server params
|
||||
if ($params && isset($params["serverid"])) {
|
||||
$server = Capsule::table("tblservers")
|
||||
->where("id", $params["serverid"])
|
||||
->first();
|
||||
|
||||
if (!$server) {
|
||||
return ["" => "-- No Server Selected --"];
|
||||
}
|
||||
|
||||
$apiParams = [
|
||||
"serverhostname" => $server->hostname,
|
||||
"serveraccesshash" => decrypt($server->accesshash),
|
||||
"serversecure" => $server->secure,
|
||||
];
|
||||
|
||||
// Get additional config options
|
||||
$configOptions = Capsule::table("tblservers")
|
||||
->where("id", $params["serverid"])
|
||||
->first();
|
||||
|
||||
if ($configOptions && isset($configOptions->username)) {
|
||||
$apiParams["serverusername"] = $configOptions->username;
|
||||
}
|
||||
} else {
|
||||
return ["" => "-- Configure Product First --"];
|
||||
}
|
||||
|
||||
// Fetch templates/plans from KeyHelp
|
||||
$result = _keyhelpmanager_APIRequest($apiParams, "/plans", "GET");
|
||||
|
||||
if (!$result["success"]) {
|
||||
logActivity(
|
||||
"KeyHelpManager: Failed to fetch templates - " .
|
||||
$result["error"],
|
||||
);
|
||||
return ["" => "-- Error loading templates --"];
|
||||
}
|
||||
|
||||
$templates = ["" => "-- Select Template --"];
|
||||
|
||||
if (isset($result["data"]) && is_array($result["data"])) {
|
||||
foreach ($result["data"] as $template) {
|
||||
$templates[$template["id"]] =
|
||||
$template["name"] ?? "Template " . $template["id"];
|
||||
}
|
||||
}
|
||||
|
||||
// Cache templates for 5 minutes
|
||||
$cacheKey = "keyhelpmanager_templates_" . ($params["serverid"] ?? 0);
|
||||
Capsule::table("tblconfiguration")->updateOrInsert(
|
||||
["setting" => $cacheKey],
|
||||
[
|
||||
"setting" => $cacheKey,
|
||||
"value" => json_encode($templates),
|
||||
"created_at" => date("Y-m-d H:i:s"),
|
||||
"updated_at" => date("Y-m-d H:i:s"),
|
||||
],
|
||||
);
|
||||
|
||||
return $templates;
|
||||
} catch (\Exception $e) {
|
||||
logActivity(
|
||||
"KeyHelpManager: Get templates failed - " . $e->getMessage(),
|
||||
);
|
||||
return ["" => "-- Error: " . $e->getMessage() . " --"];
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Sync templates from KeyHelp server
|
||||
*/
|
||||
function keyhelpmanager_AdminCustomButtonArray()
|
||||
{
|
||||
return [
|
||||
"Sync Templates" => "SyncTemplates",
|
||||
];
|
||||
}
|
||||
|
||||
function keyhelpmanager_SyncTemplates($params)
|
||||
{
|
||||
try {
|
||||
$templates = _keyhelpmanager_GetTemplates($params);
|
||||
|
||||
if (count($templates) > 1) {
|
||||
return "success&Templates synchronized successfully";
|
||||
} else {
|
||||
return "error&Failed to sync templates";
|
||||
}
|
||||
} catch (\Exception $e) {
|
||||
return "error&" . $e->getMessage();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user