Some checks failed
CI / PHP Lint-1 (push) Successful in 43s
CI / PHP Lint (push) Successful in 45s
CI / PHP Lint (pull_request) Successful in 38s
CI / Unit Tests & Static Analysis (push) Failing after 43s
CI / PHP Lint-1 (pull_request) Successful in 40s
CI / Unit Tests & Static Analysis (pull_request) Failing after 41s
Aktualisierung per Klick im Administrationsbereich, ohne Zugang zum Server. Der bisherige Updater lud ein ZIP und entpackte es ungeprüft über die Produktion; das ist vollständig ersetzt. Neu in updater/: - Version: Release-Version als Konstante, installierter Commit-SHA in storage/.version (Datei, nicht Datenbank - der Updater muss den Stand auch bei hängender Datenbank lesen können) - Progress: Fortschrittsdatei mit eigenem Fehlerzustand (pct = -1) und Verfallszeit, damit ein abgestürzter Lauf den nächsten nicht blockiert - Maintenance: Wartungsmarkierung mit Ablauf, die sich beim Prüfen selbst aufräumt - ProxyClient: nur HTTPS, Zertifikatsprüfung an, begrenzte Weiterleitungen, 503 mirror_pending/not_released als Bitte um Geduld - Updater: Prüfsumme gegen X-Content-Sha256, zweistufiger ZIP-Slip-Schutz, geschützte Pfade, atomares Schreiben je Datei, nie löschen - AutoMigrate: zieht Migrationen beim ersten Seitenaufruf nach, nur bei geändertem Fingerabdruck und mit Sperre gegen Parallellauf - guard.php: Wartungs-Wächter für die Einstiegsseiten, JSON für die Schnittstellen; ausgenommen bleiben Update-Seite, Anmeldung und Health-Check - Changelog: zerlegt CHANGELOG.md und rendert Markdown ohne unsichere Links Oberfläche: Seite "Aktualisierung" mit Stand, Kanal, Suche, Fortschritt, Migrationsliste und Versionsgeschichte. Der Datenschutzhinweis steht dauerhaft neben der Schaltfläche, nicht in einem Dialog. Fehlt changelog_preview in der Antwort, bleibt es bei "neue Fassung verfügbar" - kein leerer Kasten, kein erfundener Ersatztext. Commit-Texte werden nirgends angezeigt. Außerdem (Punkt 2 der Liste): - cron_test.php entfernt: ohne jede Anmeldung erreichbar, meldete Datenbankfehler samt Meldung und die Spalten der vouchers-Tabelle - test.php verweigert ohne config.php die Auskunft; bisher lief die Diagnose auf einer frisch entpackten Installation ohne Anmeldung PHPStan prüft jetzt den gesamten updater/-Ordner. 18 neue Tests (93 Tests, 243 Assertions), Ablauf gegen den echten Update-Dienst durchgespielt. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
217 lines
7.1 KiB
PHP
217 lines
7.1 KiB
PHP
<?php
|
|
namespace Updater;
|
|
|
|
/**
|
|
* HTTP-Client fuer den Update-Proxy.
|
|
*
|
|
* Der Proxy stellt den Stand eines Branches bereit; es gibt keine Tags und
|
|
* keine Releases. Die Identitaet eines Stands ist sein Commit-SHA.
|
|
*
|
|
* Es werden ausschliesslich HTTPS-Adressen akzeptiert, Weiterleitungen sind
|
|
* begrenzt, und die Zertifikatspruefung bleibt eingeschaltet.
|
|
*/
|
|
class ProxyClient
|
|
{
|
|
private const USER_AGENT = 'UnifiVoucherTool-Updater/1.0';
|
|
private const TIMEOUT_JSON = 30;
|
|
private const TIMEOUT_ZIP = 180;
|
|
private const MAX_REDIRECTS = 3;
|
|
|
|
/** @var string */
|
|
private $baseUrl;
|
|
|
|
public function __construct(string $baseUrl)
|
|
{
|
|
$baseUrl = rtrim($baseUrl, '/');
|
|
if (stripos($baseUrl, 'https://') !== 0) {
|
|
throw new \InvalidArgumentException('Update-Adresse muss mit https:// beginnen.');
|
|
}
|
|
$this->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];
|
|
}
|
|
}
|