Loheide-eu-mitarbeiterplatt.../app/Plugins/PluginInstaller.php
Friederich Loheide f1202bea08
Some checks failed
CI / PHP 8.3 / mariadb:10.6 (pull_request) Successful in 7m50s
CI / PHP 8.4 / mariadb:10.6 (pull_request) Successful in 7m34s
CI / PHP 8.3 / mariadb:11.4 (pull_request) Successful in 5m45s
CI / PHP 8.4 / mariadb:11.4 (pull_request) Successful in 5m30s
CI / PHP 8.3 / mysql:8.0 (pull_request) Failing after 7m7s
CI / PHP 8.4 / mysql:8.0 (pull_request) Failing after 7m9s
CI / PHP 8.3 / mysql:8.4 (pull_request) Failing after 6m8s
CI / PHP 8.4 / mysql:8.4 (pull_request) Failing after 6m5s
CI / Bekannte Sicherheitslücken in Paketen (pull_request) Successful in 48s
CI / Build-Artefakte aktuell (pull_request) Successful in 1m5s
Sicherheitsupdate nach Audit (0.29.0)
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
2026-09-27 11:34:54 +00:00

82 lines
3.1 KiB
PHP

<?php
namespace App\Plugins;
use App\Support\Audit;
use App\Support\Update\Filesystem;
use App\Support\Update\ZipExtractor;
use RuntimeException;
use Throwable;
/**
* Installiert Kunden-Plugins aus einem ZIP-Archiv nach plugins/<slug>.
* Nutzt denselben ZIP-Slip-Schutz wie der Self-Updater. Ein vorhandenes Plugin
* mit gleichem Kürzel wird ersetzt (Update), seine Daten bleiben erhalten.
*/
class PluginInstaller
{
public function __construct(
private readonly PluginManager $plugins,
private readonly ZipExtractor $extractor,
) {}
public static function uploadsAllowed(): bool
{
return ! filter_var(config('inhaus.disallow_file_mods'), FILTER_VALIDATE_BOOL);
}
/**
* @param string|null $expectedSlug bei Updates: das Archiv muss genau dieses Plugin enthalten
*/
public function install(string $zipFile, ?string $expectedSlug = null): PluginManifest
{
if (! self::uploadsAllowed()) {
throw new RuntimeException('Das Installieren von Plugins ist in dieser Installation abgeschaltet.');
}
$staging = storage_path('framework/plugin-upload-'.bin2hex(random_bytes(4)));
try {
$this->extractor->extract($zipFile, $staging);
try {
$manifest = PluginManifest::fromDirectory($staging, core: false);
} catch (Throwable $e) {
throw new RuntimeException('Das Archiv enthält kein gültiges Plugin: '.$e->getMessage());
}
if ($expectedSlug !== null && $manifest->slug !== $expectedSlug) {
throw new RuntimeException("Das Archiv enthält das Plugin „{$manifest->slug}“ statt „{$expectedSlug}“.");
}
$existing = $this->plugins->find($manifest->slug);
if ($existing?->core) {
throw new RuntimeException("Das Kürzel „{$manifest->slug}“ gehört zu einem mitgelieferten Plugin.");
}
if (! is_file($staging.'/src/'.str_replace('\\', '/', substr($manifest->class, strlen($manifest->namespace))).'.php')) {
throw new RuntimeException("Die Plugin-Klasse {$manifest->class} fehlt im Archiv (erwartet unter src/).");
}
$target = $this->plugins->customerPath().'/'.$manifest->slug;
if (! is_dir(dirname($target)) && ! @mkdir(dirname($target), 0775, true)) {
throw new RuntimeException('Das Verzeichnis plugins/ ist nicht beschreibbar.');
}
Filesystem::deleteDirectory($target);
if (! Filesystem::moveDirectory($staging, $target)) {
throw new RuntimeException('Das Plugin konnte nicht nach plugins/ verschoben werden. Bitte Schreibrechte prüfen.');
}
$this->plugins->forgetDiscovered();
Audit::log($existing ? 'plugin.updated' : 'plugin.installed', ['plugin' => $manifest->slug, 'version' => $manifest->version]);
return PluginManifest::fromDirectory($target, core: false);
} finally {
Filesystem::deleteDirectory($staging);
}
}
}