Loheide-eu-mitarbeiterplatt.../app/Support/Update/UpdateClient.php
Friederich Loheide 43244d751a Self-Updater und Lizenzprüfung
Umsetzung von docs/specs/self-updater.md für Laravel:
- Installierter Stand als SHA in storage/.version, Fortschritt in storage/.update-progress,
  selbstheilender Wartungsmodus in storage/.maintenance
- Update-Client je Kanal (stable/development), nur HTTPS, begrenzte Redirects,
  ZIP-Download per Stream mit PK- und SHA-256-Prüfung
- Entpacken mit zweistufigem ZIP-Slip-Schutz, Übernahme mit geschützten Pfaden
  (atomar je Datei, nie löschen), Migrationen, Cache- und OPcache-Reset
- Lizenzspeicher außerhalb der DB, Geräte-ID als Zufallszahl, rotierendes Gerätetoken
  wird bei jeder Antwort (auch Fehler und Downloads) sofort gespeichert
- Lizenzfehler mit verständlichen Meldungen – sie sperren nie das Portal
- Seite „Aktualisierung“ mit Kanal, Suche, Changelog-Vorschau, Fortschritt,
  Migrationen, Änderungsprotokoll, Lizenz und Datenschutzhinweis
- Wartungs-Middleware (503 + Retry-After) und Auto-Migration per Fingerabdruck
- Audit-Log für Updates, Kanalwechsel und Lizenzänderungen
- Version 0.2.0

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
2026-09-26 06:02:19 +00:00

114 lines
3.1 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
namespace App\Support\Update;
use App\Support\License\ServerError;
use App\Support\License\VendorHttp;
use Illuminate\Http\Client\Response;
/**
* Zugriff auf den Update-Proxy eines Kanals. Kennt keine Tags und keine Releases:
* Es zählt der Stand des Branches, identifiziert über den Commit-SHA.
*/
class UpdateClient
{
public function __construct(
private readonly VendorHttp $http,
private readonly string $baseUrl,
) {
if (! str_starts_with($baseUrl, 'https://')) {
throw new \InvalidArgumentException('Update-Server müssen per HTTPS angesprochen werden.');
}
}
/**
* @return array<string, mixed>
*/
public function version(): array
{
return $this->json('version');
}
/**
* @return array<string, mixed>
*/
public function check(?string $currentSha): array
{
return $this->json('check', $currentSha ? ['current_sha' => $currentSha] : []);
}
/**
* @return array<string, mixed>
*/
public function license(): array
{
return $this->json('license');
}
public function changelog(): string
{
$response = $this->send(fn () => $this->http->request((int) config('updater.timeouts.json'))
->accept('text/markdown')
->get($this->url('changelog'), ['format' => 'raw']));
return $response->body();
}
/**
* ZIP in eine Datei streamen – nicht in den Speicher.
*
* @return array{sha: string|null, sha256: string|null}
*/
public function downloadZip(string $sha, string $target): array
{
$response = $this->send(fn () => $this->http->request((int) config('updater.timeouts.zip'))
->accept('application/zip')
->sink($target)
->get($this->url('zip'), ['ref' => $sha]), sinkedTo: $target);
return [
'sha' => $response->header('X-Commit-Sha') ?: null,
'sha256' => $response->header('X-Content-Sha256') ?: null,
];
}
/**
* @param array<string, string> $query
* @return array<string, mixed>
*/
private function json(string $path, array $query = []): array
{
$response = $this->send(fn () => $this->http->request((int) config('updater.timeouts.json'))
->get($this->url($path), $query));
$data = $response->json();
if (! is_array($data)) {
throw new ServerError('invalid_response', $response->status(), 'Antwort ist kein JSON.');
}
return $data;
}
/**
* @param callable(): Response $callback
*/
private function send(callable $callback, ?string $sinkedTo = null): Response
{
$response = VendorHttp::guard($callback);
if (! $response->successful()) {
// Bei Downloads steht die Fehlerantwort in der Zieldatei.
$body = $sinkedTo && is_file($sinkedTo) ? (string) file_get_contents($sinkedTo) : null;
throw VendorHttp::errorFrom($response, $body);
}
return $response;
}
private function url(string $path): string
{
return rtrim($this->baseUrl, '/').'/'.$path;
}
}