Plugin-Updates über update_url (0.24.0)
Some checks failed
CI / PHP 8.3 / mariadb:10.6 (pull_request) Successful in 3m47s
CI / PHP 8.4 / mariadb:10.6 (pull_request) Successful in 3m50s
CI / PHP 8.3 / mariadb:11.4 (pull_request) Successful in 4m4s
CI / PHP 8.4 / mariadb:11.4 (pull_request) Successful in 4m13s
CI / PHP 8.3 / mysql:8.0 (pull_request) Failing after 5m43s
CI / PHP 8.4 / mysql:8.0 (pull_request) Failing after 5m44s
CI / PHP 8.3 / mysql:8.4 (pull_request) Failing after 4m55s
CI / Build-Artefakte aktuell (pull_request) Successful in 1m14s
CI / PHP 8.4 / mysql:8.4 (pull_request) Failing after 5m1s

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
Friederich Loheide 2026-09-27 08:06:22 +00:00
parent 93cb6b348b
commit 64d9e0bbed
117 changed files with 739 additions and 297 deletions

View file

@ -0,0 +1,198 @@
<?php
namespace App\Plugins;
use App\Support\Audit;
use App\Support\Settings;
use App\Version;
use Illuminate\Support\Facades\Artisan;
use Illuminate\Support\Facades\Http;
use RuntimeException;
use Throwable;
/**
* Updates separat installierter Plugins über die „update_url“ aus plugin.json.
*
* Die Adresse liefert entweder das Inhaus-Format
*
* { "version": "1.2.0", "download_url": "https://…/plugin-1.2.0.zip",
* "sha256": "…", "changelog": "…", "requires": { "core": ">=0.20" } }
*
* oder eine Release-Antwort von Forgejo/Gitea/GitHub (…/releases/latest bzw.
* …/releases): Version aus „tag_name“, Download = erstes ZIP unter „assets“.
*/
class PluginUpdates
{
public const STATE = 'plugins.updates';
public const MAX_BYTES = 50 * 1024 * 1024;
public function __construct(
private readonly PluginManager $plugins,
private readonly PluginInstaller $installer,
private readonly Settings $settings,
) {}
/**
* Alle Kunden-Plugins mit update_url prüfen.
*
* @return array<string, array<string, mixed>>
*/
public function check(): array
{
$results = [];
foreach ($this->plugins->all() as $manifest) {
if ($manifest->core || ! $manifest->updateUrl) {
continue;
}
try {
$results[$manifest->slug] = $this->fetch($manifest->updateUrl);
} catch (Throwable $e) {
$results[$manifest->slug] = ['error' => $e->getMessage()];
}
}
$this->settings->set(self::STATE, ['checked_at' => now()->toIso8601String(), 'plugins' => $results]);
return $results;
}
public function checkedAt(): ?string
{
return $this->settings->get(self::STATE)['checked_at'] ?? null;
}
/**
* Bekannte Informationen zu einem Plugin (aus der letzten Prüfung).
*
* @return array{version?: string, download_url?: string, sha256?: ?string, changelog?: ?string, requires_core?: ?string, error?: string, available: bool, compatible: bool}|null
*/
public function info(string $slug): ?array
{
$manifest = $this->plugins->find($slug);
$info = $this->settings->get(self::STATE)['plugins'][$slug] ?? null;
if (! $manifest || $manifest->core || ! is_array($info)) {
return null;
}
$newer = isset($info['version']) && version_compare(ltrim($info['version'], 'vV'), ltrim($manifest->version, 'vV'), '>');
$compatible = ! isset($info['requires_core']) || VersionConstraint::satisfies(Version::VERSION, $info['requires_core']);
return [...$info, 'available' => $newer && isset($info['download_url']), 'compatible' => $compatible];
}
/**
* Neue Fassung herunterladen und installieren; Daten und Aktivierung bleiben erhalten.
*/
public function update(string $slug): PluginManifest
{
$info = $this->info($slug);
if (! $info || ! $info['available']) {
throw new RuntimeException('Für dieses Plugin ist kein Update bekannt – bitte zuerst „Nach Updates suchen“.');
}
if (! $info['compatible']) {
throw new RuntimeException("Die neue Fassung benötigt Inhaus {$info['requires_core']}. Bitte zuerst Inhaus aktualisieren.");
}
$file = tempnam(sys_get_temp_dir(), 'plugin-update');
try {
$response = Http::timeout(120)->withOptions(['sink' => $file])->get($this->safeUrl($info['download_url']));
if (! $response->successful()) {
throw new RuntimeException("Download fehlgeschlagen (HTTP {$response->status()}).");
}
if (filesize($file) > self::MAX_BYTES) {
throw new RuntimeException('Das Update ist größer als 50 MB.');
}
if (! empty($info['sha256']) && ! hash_equals(strtolower($info['sha256']), hash_file('sha256', $file))) {
throw new RuntimeException('Die Prüfsumme des Downloads stimmt nicht – das Update wurde nicht installiert.');
}
$wasEnabled = $this->plugins->isEnabled($slug);
$manifest = $this->installer->install($file);
if ($manifest->slug !== $slug) {
throw new RuntimeException("Das Archiv enthält das Plugin „{$manifest->slug}“ statt „{$slug}“.");
}
// Neue Tabellen oder Spalten des Plugins sofort anlegen
if ($wasEnabled && is_dir($manifest->migrationsPath())) {
Artisan::call('migrate', ['--path' => $manifest->migrationsPath(), '--realpath' => true, '--force' => true]);
}
Audit::log('plugin.auto_updated', ['plugin' => $slug, 'version' => $manifest->version]);
return $manifest;
} finally {
@unlink($file);
}
}
/**
* @return array{version: string, download_url: ?string, sha256: ?string, changelog: ?string, requires_core: ?string}
*/
private function fetch(string $url): array
{
$response = Http::timeout(15)->acceptJson()->get($this->safeUrl($url));
if (! $response->successful()) {
throw new RuntimeException("Update-Adresse antwortet mit HTTP {$response->status()}.");
}
$data = $response->json();
if (! is_array($data)) {
throw new RuntimeException('Update-Adresse liefert kein JSON.');
}
// Liste von Releases: das neueste ohne Vorabversion
if (array_is_list($data)) {
$data = collect($data)->first(fn ($release) => is_array($release) && ! ($release['prerelease'] ?? false) && ! ($release['draft'] ?? false));
if (! $data) {
throw new RuntimeException('Keine veröffentlichte Version gefunden.');
}
}
if (isset($data['tag_name'])) {
$zip = collect($data['assets'] ?? [])->first(fn ($asset) => str_ends_with(strtolower((string) ($asset['name'] ?? '')), '.zip'));
return [
'version' => ltrim((string) $data['tag_name'], 'vV'),
'download_url' => $zip['browser_download_url'] ?? null,
'sha256' => null,
'changelog' => isset($data['body']) ? mb_substr((string) $data['body'], 0, 2000) : null,
'requires_core' => null,
];
}
if (! is_string($data['version'] ?? null)) {
throw new RuntimeException('Update-Adresse liefert keine Version.');
}
return [
'version' => ltrim($data['version'], 'vV'),
'download_url' => is_string($data['download_url'] ?? null) ? $data['download_url'] : null,
'sha256' => is_string($data['sha256'] ?? null) ? $data['sha256'] : null,
'changelog' => is_string($data['changelog'] ?? null) ? mb_substr($data['changelog'], 0, 2000) : null,
'requires_core' => is_string($data['requires']['core'] ?? null) ? $data['requires']['core'] : null,
];
}
private function safeUrl(string $url): string
{
if (! preg_match('#^https://[^\s/]+#i', $url)) {
throw new RuntimeException('Update-Adressen müssen mit https:// beginnen.');
}
return $url;
}
}