Módulo VPSManager para WHMCS con configuración externa, acceso seguro al panel y métricas Graphite para LX.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

67 lines
2.9 KiB

  1. <?php
  2. namespace VPSManager;
  3. require_once __DIR__ . '/Graphite.php';
  4. final class ClientView
  5. {
  6. public static function escape($value): string
  7. {
  8. return htmlspecialchars((string) $value, ENT_QUOTES | ENT_SUBSTITUTE, 'UTF-8');
  9. }
  10. public static function links(array $params, ?array $config = null): string
  11. {
  12. if ($config === null) {
  13. try { $config = Config::load(); } catch (\Throwable $e) { $config = []; }
  14. }
  15. $fields = $params['customfields'] ?? [];
  16. $panel = '';
  17. $graphs = '';
  18. $raw = $fields['url'] ?? '';
  19. if (is_string($raw) && $raw !== '') {
  20. // Preserve legacy host-only form, without accepting arbitrary schemes or credentials.
  21. if (preg_match('/\A[a-zA-Z0-9.-]+\z/', $raw)) {
  22. $raw = 'https://' . $raw . ':8080/login/index.php';
  23. }
  24. try { $panel = Config::url(['panel' => $raw], 'panel'); } catch (\Throwable $e) { /* Hide unsafe URL. */ }
  25. }
  26. if (($config['graphs_authorization_confirmed'] ?? false) === true
  27. && Graphite::validUuid($fields['uuid'] ?? null)
  28. && is_string($fields['vtype'] ?? null)
  29. && preg_match('/\A[a-z0-9_-]{1,24}\z/', $fields['vtype'])) {
  30. try {
  31. $graphs = Config::url($config, 'graphs_url') . '/collect/servidor/'
  32. . rawurlencode($fields['vtype']) . '/' . rawurlencode($fields['uuid']) . '/-1h/now';
  33. } catch (\Throwable $e) { /* Configuration is optional. */ }
  34. }
  35. ob_start();
  36. require __DIR__ . '/../templates/links.php';
  37. return ob_get_clean();
  38. }
  39. public static function render(array $params, $requestedRange = '24h', ?Graphite $graphite = null): string
  40. {
  41. $range = Graphite::range($requestedRange);
  42. $config = [];
  43. try { $config = Config::load(); } catch (\Throwable $e) { /* Metrics fail independently. */ }
  44. $links = self::links($params, $config);
  45. if (($params['customfields']['vtype'] ?? null) !== 'lx') {
  46. return $links;
  47. }
  48. $metrics = null;
  49. try {
  50. $graphite = $graphite ?? new Graphite($config);
  51. $metrics = $graphite->getForService($params, $range);
  52. } catch (\Throwable $e) {
  53. // Do not log exceptions, configuration, raw responses or WHMCS parameters.
  54. }
  55. $serviceId = filter_var($params['serviceid'] ?? null, FILTER_VALIDATE_INT, ['options' => ['min_range' => 1]]);
  56. $ranges = Graphite::RANGES;
  57. $cards = ['load' => 'Carga del sistema', 'memory' => 'Uso de memoria', 'network' => 'Tráfico de red'];
  58. $json = $metrics === null ? '' : json_encode($metrics, JSON_HEX_TAG | JSON_HEX_AMP | JSON_HEX_APOS | JSON_HEX_QUOT | JSON_THROW_ON_ERROR);
  59. ob_start();
  60. require __DIR__ . '/../templates/metrics.php';
  61. return ob_get_clean();
  62. }
  63. }