.
This commit is contained in:
BIN
modules/servers/keyhelpmanager.zip
Normal file
BIN
modules/servers/keyhelpmanager.zip
Normal file
Binary file not shown.
@@ -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();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -319,13 +319,28 @@
|
||||
KeyHelp Hosting
|
||||
<span class="kh-badge">ACTIVE</span>
|
||||
</div>
|
||||
<p class="kh-card-subtitle">{$domain}</p>
|
||||
<a href="{$login_url}" target="_blank" class="kh-login-btn">
|
||||
<svg width="18" height="18" fill="currentColor" viewBox="0 0 20 20" style="display: inline-block; vertical-align: middle; margin-right: 8px;">
|
||||
<path fill-rule="evenodd" d="M3 3a1 1 0 011 1v12a1 1 0 11-2 0V4a1 1 0 011-1zm7.707 3.293a1 1 0 010 1.414L9.414 9H17a1 1 0 110 2H9.414l1.293 1.293a1 1 0 01-1.414 1.414l-3-3a1 1 0 010-1.414l3-3a1 1 0 011.414 0z" clip-rule="evenodd"/>
|
||||
</svg>
|
||||
Open Control Panel
|
||||
</a>
|
||||
<p class="kh-card-subtitle">
|
||||
{$domain}
|
||||
{if $template_name}
|
||||
<span class="kh-badge">📋 {$template_name}</span>
|
||||
{/if}
|
||||
</p>
|
||||
<div style="display: flex; gap: 12px; flex-wrap: wrap;">
|
||||
<a href="{$login_url}" target="_blank" class="kh-login-btn">
|
||||
<svg width="18" height="18" fill="currentColor" viewBox="0 0 20 20" style="display: inline-block; vertical-align: middle; margin-right: 8px;">
|
||||
<path fill-rule="evenodd" d="M3 3a1 1 0 011 1v12a1 1 0 11-2 0V4a1 1 0 011-1zm7.707 3.293a1 1 0 010 1.414L9.414 9H17a1 1 0 110 2H9.414l1.293 1.293a1 1 0 01-1.414 1.414l-3-3a1 1 0 010-1.414l3-3a1 1 0 011.414 0z" clip-rule="evenodd"/>
|
||||
</svg>
|
||||
Open Control Panel
|
||||
</a>
|
||||
{if $panel_url}
|
||||
<a href="{$panel_url}" target="_blank" class="kh-login-btn" style="background: rgba(255,255,255,0.2); color: white;">
|
||||
<svg width="18" height="18" fill="currentColor" viewBox="0 0 20 20" style="display: inline-block; vertical-align: middle; margin-right: 8px;">
|
||||
<path fill-rule="evenodd" d="M11.49 3.17c-.38-1.56-2.6-1.56-2.98 0a1.532 1.532 0 01-2.286.948c-1.372-.836-2.942.734-2.106 2.106.54.886.061 2.042-.947 2.287-1.561.379-1.561 2.6 0 2.978a1.532 1.532 0 01.947 2.287c-.836 1.372.734 2.942 2.106 2.106a1.532 1.532 0 012.287.947c.379 1.561 2.6 1.561 2.978 0a1.533 1.533 0 012.287-.947c1.372.836 2.942-.734 2.106-2.106a1.533 1.533 0 01.947-2.287c1.561-.379 1.561-2.6 0-2.978a1.532 1.532 0 01-.947-2.287c.836-1.372-.734-2.942-2.106-2.106a1.532 1.532 0 01-2.287-.947zM10 13a3 3 0 100-6 3 3 0 000 6z" clip-rule="evenodd"/>
|
||||
</svg>
|
||||
Admin Panel
|
||||
</a>
|
||||
{/if}
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="kh-info-grid">
|
||||
|
||||
Reference in New Issue
Block a user