baseUrl = $baseUrl; } public function baseUrl(): string { return $this->baseUrl; } /** Neuester Stand des Kanals. */ public function version(): array { return $this->getJson('/version'); } /** * Vergleicht den installierten Stand mit dem Kanal. * Ohne SHA antwortet der Proxy wie "aktuell" - die erste Pruefung einer * frischen Installation liefert also latest_sha zum Merken. */ public function check(?string $currentSha): array { $path = '/check'; if ($currentSha !== null && $currentSha !== '') { $path .= '?current_sha=' . urlencode($currentSha); } return $this->getJson($path); } /** Aenderungsprotokoll des aktuellen Stands als Markdown. */ public function changelogRaw(): string { [$status, $body, $type] = $this->request('/changelog?format=raw', self::TIMEOUT_JSON); if ($status === 404) { return ''; } $this->guardRetry($status, $body); if ($status !== 200 || stripos((string)$type, 'json') !== false) { return ''; } return (string)$body; } /** * Laedt das ZIP in eine Datei - bewusst als Stream, nicht in den Speicher. * * @return array{sha:string, content_sha256:string} */ public function downloadZip(string $sha, string $target): array { if (!Version::isSha($sha)) { throw new \RuntimeException('Ungueltiger Ziel-Stand fuer den Download.'); } $fh = @fopen($target, 'w'); if ($fh === false) { throw new \RuntimeException( 'Update-Paket kann nicht gespeichert werden. Schreibrechte fuer updater/storage/ pruefen.' ); } $headers = []; $ch = curl_init($this->baseUrl . '/zip?ref=' . urlencode($sha)); curl_setopt_array($ch, [ CURLOPT_FILE => $fh, CURLOPT_FOLLOWLOCATION => true, CURLOPT_MAXREDIRS => self::MAX_REDIRECTS, CURLOPT_PROTOCOLS => CURLPROTO_HTTPS, CURLOPT_REDIR_PROTOCOLS=> CURLPROTO_HTTPS, CURLOPT_SSL_VERIFYPEER => true, CURLOPT_SSL_VERIFYHOST => 2, CURLOPT_TIMEOUT => self::TIMEOUT_ZIP, CURLOPT_CONNECTTIMEOUT => 15, CURLOPT_USERAGENT => self::USER_AGENT, CURLOPT_HEADERFUNCTION => function ($ch, $line) use (&$headers) { $parts = explode(':', $line, 2); if (count($parts) === 2) { $headers[strtolower(trim($parts[0]))] = trim($parts[1]); } return strlen($line); }, ]); $ok = curl_exec($ch); $status = (int)curl_getinfo($ch, CURLINFO_HTTP_CODE); $err = curl_error($ch); curl_close($ch); fclose($fh); if (!$ok || $status !== 200) { $body = (string)@file_get_contents($target, false, null, 0, 400); @unlink($target); $this->guardRetry($status, $body); throw new \RuntimeException( $err !== '' ? 'Update-Paket konnte nicht geladen werden: ' . $err : 'Update-Paket konnte nicht geladen werden (HTTP ' . $status . ').' ); } return [ 'sha' => $headers['x-commit-sha'] ?? $sha, 'content_sha256' => $headers['x-content-sha256'] ?? '', ]; } // ------------------------------------------------------------- intern private function getJson(string $path): array { [$status, $body] = $this->request($path, self::TIMEOUT_JSON); $this->guardRetry($status, $body); $data = json_decode((string)$body, true); if (!is_array($data)) { throw new \RuntimeException( 'Der Update-Dienst hat keine verwertbare Antwort geliefert (HTTP ' . $status . ').' ); } // Der Proxy kann einen Fehler auch mit HTTP 200 senden. if (isset($data['error'])) { throw new \RuntimeException('Update-Dienst meldet: ' . (string)$data['error']); } if ($status !== 200) { throw new \RuntimeException('Update-Dienst nicht erreichbar (HTTP ' . $status . ').'); } return $data; } /** * 503 mit mirror_pending/not_released ist kein Fehler des Clients: * der Stand wird gerade gespiegelt oder ist noch nicht freigegeben. */ private function guardRetry(int $status, $body): void { if ($status !== 503) { return; } $data = json_decode((string)$body, true); $code = is_array($data) ? (string)($data['error'] ?? '') : ''; $retry = is_array($data) ? (int)($data['retry_after'] ?? 60) : 60; if ($code === 'not_released') { throw new RetryLaterException( 'Beim Update-Dienst liegt noch keine freigegebene Fassung bereit. Bitte spaeter erneut versuchen.', $retry ); } throw new RetryLaterException( 'Der Update-Dienst bereitet den neuen Stand gerade vor. Bitte in Kuerze erneut versuchen.', $retry ); } /** * @return array{0:int, 1:string, 2:string} */ private function request(string $path, int $timeout): array { $ch = curl_init($this->baseUrl . $path); curl_setopt_array($ch, [ CURLOPT_RETURNTRANSFER => true, CURLOPT_FOLLOWLOCATION => true, CURLOPT_MAXREDIRS => self::MAX_REDIRECTS, CURLOPT_PROTOCOLS => CURLPROTO_HTTPS, CURLOPT_REDIR_PROTOCOLS=> CURLPROTO_HTTPS, CURLOPT_SSL_VERIFYPEER => true, CURLOPT_SSL_VERIFYHOST => 2, CURLOPT_TIMEOUT => $timeout, CURLOPT_CONNECTTIMEOUT => 10, CURLOPT_USERAGENT => self::USER_AGENT, ]); $body = curl_exec($ch); $status = (int)curl_getinfo($ch, CURLINFO_HTTP_CODE); $type = (string)curl_getinfo($ch, CURLINFO_CONTENT_TYPE); $err = curl_error($ch); curl_close($ch); if ($body === false) { throw new \RuntimeException('Update-Dienst nicht erreichbar: ' . $err); } return [$status, (string)$body, $type]; } }