|
- <?php
- namespace VPSManager;
- require_once __DIR__ . '/BackupOption.php';
-
- final class BackupAccess
- {
- // Invoked only by WHMCS module callbacks, never a standalone public endpoint.
- // No request service ID, client ID or host is trusted here.
- public static function context(array $params): ?array
- {
- try {
- $id = filter_var($params['serviceid'] ?? null, FILTER_VALIDATE_INT, ['options' => ['min_range' => 1]]);
- if (!$id || !class_exists('WHMCS\\Authentication\\CurrentUser')) return null;
- $current = new \WHMCS\Authentication\CurrentUser();
- $client = $current->client();
- if (!$client || !$current->isAuthenticatedUser()) return null;
- $user = $current->user();
- if (!$user) return null;
- // Permissions belong to the authenticated user's account relationship.
- // WHMCS 8.13 returns an array of Client models for this permission.
- $permittedClients = $user->getClientsByPermission('products');
- if (!is_array($permittedClients)) return null;
- $permitted = false;
- foreach ($permittedClients as $permittedClient) {
- if ($permittedClient instanceof \WHMCS\User\Client
- && (string) $permittedClient->id === (string) $client->id) $permitted = true;
- }
- if (!$permitted) return null;
- $service = \WHMCS\Database\Capsule::table('tblhosting')->where('id', $id)->where('userid', $client->id)->first();
- if (!$service) return null;
- try { $keys = BackupOption::applicable($service->packageid); }
- catch (\Throwable $e) { Backups::diagnostic('OPTION_DEFINITION', $id); return null; }
- // Do not even resolve the private mapping before entitlement is established.
- if (!Backups::entitled($params, Config::load()['backups'] ?? [], $keys)) return null;
- $fields = \WHMCS\Database\Capsule::table('tblcustomfields')
- ->where('type', 'product')->where('relid', $service->packageid)
- ->where('fieldname', 'backuppc_host')->get();
- if (count($fields) !== 1) return ['serviceid' => $id, 'option_keys' => $keys, 'host' => null];
- $field = $fields[0];
- if ($field->fieldtype !== 'text' || $field->adminonly !== 'on'
- || $field->showorder !== '' || $field->showinvoice !== '') return ['serviceid' => $id, 'option_keys' => $keys, 'host' => null];
- $values = \WHMCS\Database\Capsule::table('tblcustomfieldsvalues')->where('fieldid', $field->id)->where('relid', $id)->get();
- return ['serviceid' => $id, 'option_keys' => $keys, 'host' => count($values) === 1 ? $values[0]->value : null];
- } catch (\Throwable $e) {
- return null; // Unsupported WHMCS authentication/schema fails closed.
- }
- }
- }
|