Added SSH key dropdown

This commit is contained in:
2023-09-10 17:13:43 -04:00
parent 28b9761223
commit 319200a677

141
hooks.php
View File

@@ -1,5 +1,7 @@
<?php
use WHMCS\User\User;
if (!defined("WHMCS")) {
die("This file cannot be accessed directly");
}
@@ -32,24 +34,7 @@ if (!function_exists('fetchPackageId')) {
{
$url = sprintf("%s/packages", VIRTFUSION_API_URL);
$curl = curl_init();
curl_setopt_array($curl, [
CURLOPT_URL => $url,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_HTTPHEADER => [
sprintf("Authorization: Bearer %s", VIRT_TOKEN)
]
]);
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
throw new Exception("cURL Error: " . $err);
}
$packages = json_decode($response, true, 512, JSON_THROW_ON_ERROR);
$packages = makeRequest($url);
foreach ($packages['data'] as $package) {
if ($package['name'] === $packageName && $package['enabled'] === true) {
@@ -66,30 +51,12 @@ if (!function_exists('fetchTemplates')) {
* @param int $serverPackageId
* @return array|null
* @throws JsonException
* @throws Exception
*/
function fetchTemplates(int $serverPackageId): ?array
{
$url = sprintf("%s/media/templates/fromServerPackageSpec/%d", VIRTFUSION_API_URL, $serverPackageId);
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => $url,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_HTTPHEADER => array(
sprintf("Authorization: Bearer %s", VIRT_TOKEN)
)
));
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
throw new Exception("cURL Error: " . $err);
}
return json_decode($response, true, 512, JSON_THROW_ON_ERROR);
return makeRequest($url);
}
}
@@ -97,7 +64,6 @@ if (!function_exists('custom_os_templates_hook')) {
/**
* @param array $vars
* @return array|null[]
* @throws Exception
*/
function custom_os_templates_hook(array $vars): array
{
@@ -124,11 +90,78 @@ if (!function_exists('custom_os_templates_hook')) {
}
}
if (!function_exists('get_vf_user_details')) {
/**
* @param int $id
* @return array
* @throws JsonException
*/
function get_vf_user_details(int $id): ?array {
$url = sprintf("%s/users/%s/byExtRelation", VIRTFUSION_API_URL, $id);
$response = makeRequest($url);
if (isset($response['msg']) && $response['msg'] === "ext_relation_id not found") {
return null;
}
return $response['data'];
}
}
if (!function_exists('makeRequest')) {
/**
* @param string $url
* @return mixed
* @throws JsonException
* @throws Exception
*/
function makeRequest(string $url): array
{
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => $url,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_HTTPHEADER => array(
sprintf("Authorization: Bearer %s", VIRT_TOKEN)
)
));
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
throw new Exception("cURL Error: " . $err);
}
return json_decode($response, true, 512, JSON_THROW_ON_ERROR);
}
}
if (!function_exists('get_users_ssh_keys')) {
/**
* @throws JsonException
*/
function get_users_ssh_keys(?User $user): ?array {
if (is_null($user)) {
return null;
}
$vfUser = get_vf_user_details($user['id']);
$url = sprintf("%s/ssh_keys/user/%s", VIRTFUSION_API_URL, $vfUser['id']);
$response = makeRequest($url);
return $response;
}
}
if (!function_exists('add_hook_os_templates')) {
/**
* @param array $vars
* @return array|null
* @throws Exception
* @throws JsonException
*/
function add_hook_os_templates(array $vars): ?array
{
@@ -156,15 +189,39 @@ if (!function_exists('add_hook_os_templates')) {
return strcmp($a['name'], $b['name']);
});
$newOption = [
$osTemplates = [
'id' => 'os_template',
'optionname' => 'Initial Operating System',
'optiontype' => 1,
'options' => $dropdownOptions,
'selectedvalue' => ''
];
$sshKeys = get_users_ssh_keys($vars['loggedinuser']);
$sshKeysOptions = [
'id' => 'ssh_key',
'optionname' => 'Initial SSH Key',
'optiontype' => 1,
'options' => array_map(function ($sshKey) {
if ($sshKey['enabled'] === false) {
return null;
}
return [
'id' => $sshKey['id'],
'name' => $sshKey['name']
];
}, $sshKeys['data']),
'selectedvalue' => ''
];
$configurableoptions = $vars['configurableoptions'];
$configurableoptions[] = $newOption;
array_push(
$configurableoptions,
$osTemplates,
$sshKeysOptions
);
return [
'configurableoptions' => $configurableoptions,
@@ -173,5 +230,3 @@ if (!function_exists('add_hook_os_templates')) {
add_hook('ClientAreaPageCart', 1, 'add_hook_os_templates');
}