fix: add null/false guards, proper error handling, and VNC popup fix

- Add isset() guards before count() on ipv4/ipv6 arrays in ServerResource
  to prevent PHP 8.0+ TypeError
- Add null checks after getWhmcsService() and getCP() in 18 Module methods
  and 5 ModuleFunctions methods to prevent fatal null dereference errors
- Add null guards for $whmcsService and $cp in admin.php impersonateServerOwner
- Fix HTTP status codes throughout admin.php (404, 400, 500, 502 instead of 200)
- Guard ConfigureService methods against $this->cp === false
- Use null coalescing for customfields access in initServerBuild
- Check API response code in initServerBuild instead of always returning true
- Replace exit() with RuntimeException in Curl.php
- Change catch(Exception) to catch(Throwable) in hooks.php for PHP 8.0+
- Open VNC window before AJAX call to avoid popup blocker

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
EZSCALE
2026-02-07 13:49:12 -06:00
parent d52e379d5f
commit 49fdd9e49b
8 changed files with 137 additions and 19 deletions

View File

@@ -26,6 +26,8 @@ class ConfigureService extends Module
*/
public function fetchPackageId(string $packageName): ?int
{
if (!$this->cp) return null;
$request = $this->initCurl($this->cp['token']);
$response = $request->get(
@@ -70,6 +72,8 @@ class ConfigureService extends Module
return null;
}
if (!$this->cp) return null;
$request = $this->initCurl($this->cp['token']);
$response = $request->get(
@@ -90,10 +94,14 @@ class ConfigureService extends Module
return null;
}
if (!$this->cp) return null;
$request = $this->initCurl($this->cp['token']);
$vfUser = $this->getVFUserDetails($user['id']);
if (!$vfUser) return null;
$response = $request->get(
sprintf("%s/ssh_keys/user/%d", $this->cp['url'], $vfUser['id'])
);
@@ -108,6 +116,8 @@ class ConfigureService extends Module
*/
public function getVFUserDetails(int $id): ?array
{
if (!$this->cp) return null;
$request = $this->initCurl($this->cp['token']);
$response = $this->decodeResponseFromJson($request->get(
@@ -124,30 +134,35 @@ class ConfigureService extends Module
*/
public function initServerBuild(int $id, array $vars): bool
{
if (!$this->cp) return false;
$request = $this->initCurl($this->cp['token']);
// Generate a random 8 character hostname
$hostname = substr(str_shuffle('abcdefghijklmnopqrstuvwxyz'), 0, 8);
$inputData = [
"operatingSystemId" => $vars['customfields']['Initial Operating System'],
"operatingSystemId" => $vars['customfields']['Initial Operating System'] ?? null,
"name" => $hostname,
"sshKeys" => [
$vars['customfields']['Initial SSH Key']
$vars['customfields']['Initial SSH Key'] ?? null
],
'email' => true
];
if (empty($vars['customfields']['Initial SSH Key'])) {
if (empty($vars['customfields']['Initial SSH Key'] ?? null)) {
unset($inputData['sshKeys']);
}
$request->addOption(CURLOPT_POSTFIELDS, json_encode($inputData));
$request->post(
$response = $request->post(
sprintf("%s/servers/%d/build", $this->cp['url'], $id)
);
return true;
$httpCode = $request->getRequestInfo('http_code');
Log::insert(__FUNCTION__, $request->getRequestInfo(), $response);
return ($httpCode == 200 || $httpCode == 201);
}
}
}