|
- <?php
- namespace VPSManager;
- require_once __DIR__ . '/Config.php';
-
- final class BackupHttp
- {
- // Private server configuration only; no URL, action or arbitrary query arguments.
- public static function getHostSummary(array $config, string $host): string
- {
- if (!Backups::validHost($host) || !function_exists('curl_init')) throw new \RuntimeException('TRANSPORT');
- $endpoint = Config::url($config, 'endpoint');
- $user = $config['username'] ?? null; $password = $config['password'] ?? null;
- if (!is_string($user) || !is_string($password) || $user === '' || $password === ''
- || strpos($user, ':') !== false || preg_match('/[\x00-\x1f\x7f]/', $user . $password)) throw new \RuntimeException('AUTH_CONFIG');
- $url = $endpoint . '?' . http_build_query(['host' => $host], '', '&', PHP_QUERY_RFC3986);
- $ch = curl_init($url); $body = '';
- if ($ch === false) throw new \RuntimeException('TRANSPORT');
- try {
- curl_setopt_array($ch, [
- CURLOPT_HTTPGET => true, CURLOPT_FOLLOWLOCATION => false, CURLOPT_PROTOCOLS => CURLPROTO_HTTPS,
- CURLOPT_SSL_VERIFYPEER => true, CURLOPT_SSL_VERIFYHOST => 2,
- CURLOPT_CONNECTTIMEOUT => Backups::limit($config, 'connect_timeout_seconds', 2, 1, 10),
- CURLOPT_TIMEOUT => Backups::limit($config, 'timeout_seconds', 5, 1, 30),
- CURLOPT_HTTPAUTH => CURLAUTH_BASIC, CURLOPT_USERNAME => $user, CURLOPT_PASSWORD => $password,
- CURLOPT_WRITEFUNCTION => static function ($ch, string $chunk) use (&$body, $config): int {
- if (strlen($body) + strlen($chunk) > Backups::limit($config, 'max_response_bytes', 2097152, 1024, 2097152)) return 0;
- $body .= $chunk;
- return strlen($chunk);
- },
- ]);
- if (curl_exec($ch) === false || curl_getinfo($ch, CURLINFO_HTTP_CODE) !== 200) throw new \RuntimeException('HTTP');
- return $body;
- } finally { curl_close($ch); }
- }
- }
|