|
- <?php
- namespace VPSManager;
- require_once __DIR__ . '/Graphite.php';
-
- final class ClientView
- {
- public static function escape($value): string
- {
- return htmlspecialchars((string) $value, ENT_QUOTES | ENT_SUBSTITUTE, 'UTF-8');
- }
-
- public static function links(array $params, ?array $config = null): string
- {
- if ($config === null) {
- try { $config = Config::load(); } catch (\Throwable $e) { $config = []; }
- }
- $fields = $params['customfields'] ?? [];
- $panel = '';
- $graphs = '';
- $raw = $fields['url'] ?? '';
- if (is_string($raw) && $raw !== '') {
- // Preserve legacy host-only form, without accepting arbitrary schemes or credentials.
- if (preg_match('/\A[a-zA-Z0-9.-]+\z/', $raw)) {
- $raw = 'https://' . $raw . ':8080/login/index.php';
- }
- try { $panel = Config::url(['panel' => $raw], 'panel'); } catch (\Throwable $e) { /* Hide unsafe URL. */ }
- }
- if (($config['graphs_authorization_confirmed'] ?? false) === true
- && Graphite::validUuid($fields['uuid'] ?? null)
- && is_string($fields['vtype'] ?? null)
- && preg_match('/\A[a-z0-9_-]{1,24}\z/', $fields['vtype'])) {
- try {
- $graphs = Config::url($config, 'graphs_url') . '/collect/servidor/'
- . rawurlencode($fields['vtype']) . '/' . rawurlencode($fields['uuid']) . '/-1h/now';
- } catch (\Throwable $e) { /* Configuration is optional. */ }
- }
- ob_start();
- require __DIR__ . '/../templates/links.php';
- return ob_get_clean();
- }
-
- public static function render(array $params, $requestedRange = '24h', ?Graphite $graphite = null): string
- {
- $range = Graphite::range($requestedRange);
- $config = [];
- try { $config = Config::load(); } catch (\Throwable $e) { /* Metrics fail independently. */ }
- $links = self::links($params, $config);
- if (($params['customfields']['vtype'] ?? null) !== 'lx') {
- return $links;
- }
- $metrics = null;
- try {
- $graphite = $graphite ?? new Graphite($config);
- $metrics = $graphite->getForService($params, $range);
- } catch (\Throwable $e) {
- // Do not log exceptions, configuration, raw responses or WHMCS parameters.
- }
- $serviceId = filter_var($params['serviceid'] ?? null, FILTER_VALIDATE_INT, ['options' => ['min_range' => 1]]);
- $ranges = Graphite::RANGES;
- $cards = ['load' => 'Carga del sistema', 'memory' => 'Uso de memoria', 'network' => 'Tráfico de red'];
- $json = $metrics === null ? '' : json_encode($metrics, JSON_HEX_TAG | JSON_HEX_AMP | JSON_HEX_APOS | JSON_HEX_QUOT | JSON_THROW_ON_ERROR);
- ob_start();
- require __DIR__ . '/../templates/metrics.php';
- return ob_get_clean();
- }
- }
|