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.
 
 
 
 
 
 

35 lines
1.9 KiB

  1. <?php
  2. namespace VPSManager;
  3. require_once __DIR__ . '/Config.php';
  4. final class BackupHttp
  5. {
  6. public static function request(array $config, string $host): string
  7. {
  8. if (!Backups::validHost($host) || !function_exists('curl_init')) throw new \RuntimeException('TRANSPORT');
  9. $endpoint = Config::url($config, 'endpoint');
  10. $user = $config['username'] ?? null; $password = $config['password'] ?? null;
  11. if (!is_string($user) || !is_string($password) || $user === '' || $password === ''
  12. || strpos($user, ':') !== false || preg_match('/[\x00-\x1f\x7f]/', $user . $password)) throw new \RuntimeException('AUTH_CONFIG');
  13. $url = $endpoint . '?' . http_build_query(['host' => $host], '', '&', PHP_QUERY_RFC3986);
  14. $ch = curl_init($url); $body = '';
  15. if ($ch === false) throw new \RuntimeException('TRANSPORT');
  16. try {
  17. curl_setopt_array($ch, [
  18. CURLOPT_FOLLOWLOCATION => false, CURLOPT_PROTOCOLS => CURLPROTO_HTTPS,
  19. CURLOPT_SSL_VERIFYPEER => true, CURLOPT_SSL_VERIFYHOST => 2,
  20. CURLOPT_CONNECTTIMEOUT => Backups::limit($config, 'connect_timeout_seconds', 2, 1, 10),
  21. CURLOPT_TIMEOUT => Backups::limit($config, 'timeout_seconds', 5, 1, 30),
  22. CURLOPT_HTTPAUTH => CURLAUTH_BASIC, CURLOPT_USERNAME => $user, CURLOPT_PASSWORD => $password,
  23. CURLOPT_WRITEFUNCTION => static function ($ch, string $chunk) use (&$body, $config): int {
  24. if (strlen($body) + strlen($chunk) > Backups::limit($config, 'max_response_bytes', 2097152, 1024, 2097152)) return 0;
  25. $body .= $chunk;
  26. return strlen($chunk);
  27. },
  28. ]);
  29. if (curl_exec($ch) === false || curl_getinfo($ch, CURLINFO_HTTP_CODE) !== 200) throw new \RuntimeException('HTTP');
  30. return $body;
  31. } finally { curl_close($ch); }
  32. }
  33. }