Loheide-eu-mitarbeiterplatt.../app/Support/SiteHealth.php
Friederich Loheide 293d7f86a4
Some checks failed
CI / PHP 8.4 / mariadb:10.6 (pull_request) Successful in 3m4s
CI / PHP 8.3 / mariadb:10.6 (pull_request) Successful in 3m8s
CI / PHP 8.3 / mariadb:11.4 (pull_request) Successful in 3m32s
CI / PHP 8.4 / mariadb:11.4 (pull_request) Successful in 3m35s
CI / PHP 8.3 / mysql:8.0 (pull_request) Successful in 5m7s
CI / PHP 8.4 / mysql:8.0 (pull_request) Successful in 5m1s
CI / PHP 8.3 / mysql:8.4 (pull_request) Successful in 5m30s
CI / PHP 8.4 / mysql:8.4 (pull_request) Successful in 5m31s
CI / Build-Artefakte aktuell (pull_request) Failing after 1m26s
Plugins bei fehlender Klasse nicht dauerhaft deaktivieren, Speichern-Abstand im Admin, Profilbild-Fehlermeldung
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
2026-09-26 18:28:03 +00:00

271 lines
11 KiB
PHP
Raw Permalink 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;
use App\Support\License\LicenseStore;
use App\Support\License\ServerError;
use App\Support\License\ServerMessages;
use App\Support\Update\MaintenanceFlag;
use App\Support\Update\Migrations;
use Illuminate\Support\Carbon;
use Illuminate\Support\Facades\DB;
use Throwable;
/**
* Systemzustand: prüft Voraussetzungen und Betrieb. Wird im Admin-Bereich
* angezeigt und vom Web-Installer für die Systemprüfung genutzt.
*/
class SiteHealth
{
public const OK = 'ok';
public const WARNING = 'warning';
public const ERROR = 'error';
public const MIN_PHP = '8.3.0';
public const REQUIRED_EXTENSIONS = ['pdo_mysql', 'mbstring', 'openssl', 'intl', 'gd', 'zip', 'fileinfo', 'tokenizer', 'ctype', 'xml', 'dom'];
/**
* Voraussetzungen, die schon vor der Installation erfüllt sein müssen.
*
* @return list<array{label: string, status: string, message: string}>
*/
public function requirements(): array
{
$checks = [];
$checks[] = $this->check(
'PHP-Version',
version_compare(PHP_VERSION, self::MIN_PHP, '>='),
'PHP '.PHP_VERSION,
'Benötigt wird mindestens PHP '.self::MIN_PHP.'. Die Version lässt sich meist in der Verwaltung des Hosters umstellen.',
);
$missing = array_values(array_filter(self::REQUIRED_EXTENSIONS, fn (string $ext) => ! extension_loaded($ext)));
$checks[] = $this->check(
'PHP-Erweiterungen',
$missing === [],
'Alle vorhanden',
'Es fehlen: '.implode(', ', $missing).'. Bitte beim Hoster aktivieren.',
);
$gd = function_exists('gd_info') ? gd_info() : [];
$formats = array_filter(['JPEG' => $gd['JPEG Support'] ?? false, 'PNG' => $gd['PNG Support'] ?? false, 'WebP' => $gd['WebP Support'] ?? false], fn ($supported) => ! $supported);
$checks[] = $this->check(
'Bildverarbeitung (GD)',
$gd !== [] && $formats === [],
'JPEG, PNG und WebP',
$gd === [] ? 'Die PHP-Erweiterung „gd“ fehlt – Profilbilder und Titelbilder lassen sich nicht hochladen.' : 'GD kann kein '.implode(', ', array_keys($formats)).' – Profilbilder und Titelbilder lassen sich so nicht hochladen. Bitte beim Hoster eine GD-Version mit '.implode(', ', array_keys($formats)).'-Unterstützung aktivieren.',
);
$checks[] = [
'label' => 'Ausgehende Verbindungen',
'status' => function_exists('curl_init') || ini_get('allow_url_fopen') ? self::OK : self::WARNING,
'message' => function_exists('curl_init') ? 'cURL verfügbar' : 'Weder cURL noch allow_url_fopen – Updates sind so nicht möglich.',
];
foreach (['storage' => storage_path(), 'bootstrap/cache' => base_path('bootstrap/cache'), 'public/uploads' => public_path('uploads')] as $label => $path) {
$checks[] = $this->check(
"Schreibrechte: {$label}",
$this->writable($path),
'Beschreibbar',
"Das Verzeichnis {$label} muss für den Webserver beschreibbar sein.",
);
}
$checks[] = [
'label' => 'Schreibrechte für Updates',
'status' => $this->writable(base_path('app')) && $this->writable(base_path('vendor')) && $this->writable(public_path('build')) ? self::OK : self::WARNING,
'message' => $this->writable(base_path('app'))
? 'Programmdateien sind beschreibbar – Aktualisierungen per Klick funktionieren.'
: 'Programmdateien sind nicht beschreibbar – Aktualisierungen per Klick sind so nicht möglich.',
];
return $checks;
}
/**
* Alle Prüfungen für die Seite „Systemzustand“.
*
* @return array<string, list<array{label: string, status: string, message: string}>>
*/
public function all(): array
{
return [
'Voraussetzungen' => $this->requirements(),
'Leistung und Grenzen' => $this->performance(),
'Betrieb' => $this->operations(),
'Sicherheit' => $this->security(),
];
}
/**
* @return list<array{label: string, status: string, message: string}>
*/
private function performance(): array
{
$opcache = function_exists('opcache_get_status') && @opcache_get_status(false);
$memory = self::bytes((string) ini_get('memory_limit'));
$upload = min(self::bytes((string) ini_get('upload_max_filesize')), self::bytes((string) ini_get('post_max_size')));
return [
[
'label' => 'OPcache',
'status' => $opcache ? self::OK : self::WARNING,
'message' => $opcache ? 'Aktiv' : 'Nicht aktiv – das Portal läuft deutlich langsamer. Bitte beim Hoster aktivieren.',
],
[
'label' => 'Arbeitsspeicher (memory_limit)',
'status' => $memory === -1 || $memory >= 128 * 1024 ** 2 ? self::OK : self::WARNING,
'message' => ini_get('memory_limit').($memory !== -1 && $memory < 128 * 1024 ** 2 ? ' – empfohlen sind mindestens 128M.' : ''),
],
[
'label' => 'Maximale Upload-Größe',
'status' => $upload === -1 || $upload >= 8 * 1024 ** 2 ? self::OK : self::WARNING,
'message' => ini_get('upload_max_filesize').' / '.ini_get('post_max_size').($upload !== -1 && $upload < 8 * 1024 ** 2 ? ' – für Fotos sind mindestens 8M empfohlen.' : ''),
],
$this->databaseCheck(),
];
}
/**
* @return list<array{label: string, status: string, message: string}>
*/
private function operations(): array
{
$beats = app(BackgroundTasks::class)->heartbeats();
$cron = max($beats['cron'] ?? 0, $beats['webcron'] ?? 0);
$pseudo = $beats['pseudo'] ?? 0;
$background = match (true) {
now()->getTimestamp() - $cron < 600 => ['status' => self::OK, 'message' => 'Läuft per '.(($beats['cron'] ?? 0) >= ($beats['webcron'] ?? 0) ? 'Cron' : 'Web-Cron').', zuletzt '.$this->ago($cron).'.'],
$pseudo > 0 => ['status' => self::WARNING, 'message' => 'Läuft nur beim Seitenaufruf (Pseudo-Cron), zuletzt '.$this->ago($pseudo).'. Zuverlässiger ist ein Cronjob.'],
default => ['status' => self::WARNING, 'message' => 'Noch kein Lauf erkannt. Bitte einen Cronjob einrichten (siehe unten).'],
};
try {
$pending = count(app(Migrations::class)->pending());
} catch (Throwable) {
$pending = null;
}
$license = app(LicenseStore::class);
$code = $license->lastStatus()['error_code'] ?? null;
return [
['label' => 'Hintergrundaufgaben', ...$background],
[
'label' => 'Datenbank-Migrationen',
'status' => $pending === 0 ? self::OK : ($pending === null ? self::ERROR : self::WARNING),
'message' => match (true) {
$pending === null => 'Status kann nicht gelesen werden.',
$pending === 0 => 'Alle angewendet.',
default => "{$pending} ausstehend – unter „Aktualisierung“ ausführen.",
},
],
[
'label' => 'Lizenz',
'status' => ! $license->hasLicense() || $code ? self::WARNING : self::OK,
'message' => match (true) {
! $license->hasLicense() => 'Keine Lizenz hinterlegt – Updates sind nicht möglich.',
$code !== null => ServerMessages::for(new ServerError($code, 0, $code)),
default => 'Hinterlegt.',
},
],
[
'label' => 'Wartungsmodus',
'status' => app(MaintenanceFlag::class)->isActive() ? self::WARNING : self::OK,
'message' => app(MaintenanceFlag::class)->isActive() ? 'Aktiv – es läuft gerade eine Aktualisierung.' : 'Aus',
],
];
}
/**
* @return list<array{label: string, status: string, message: string}>
*/
private function security(): array
{
$https = str_starts_with((string) config('app.url'), 'https://');
return [
[
'label' => 'Fehlerdetails (APP_DEBUG)',
'status' => match (true) {
config('app.debug') && app()->isProduction() => self::ERROR,
(bool) config('app.debug') => self::WARNING,
default => self::OK,
},
'message' => config('app.debug') ? 'Eingeschaltet – im Betrieb unbedingt ausschalten, sonst werden interne Details angezeigt.' : 'Ausgeschaltet',
],
[
'label' => 'Verschlüsselte Verbindung',
'status' => $https ? self::OK : self::WARNING,
'message' => $https ? 'Das Portal läuft über HTTPS.' : 'APP_URL beginnt nicht mit https:// – Anmeldedaten würden unverschlüsselt übertragen.',
],
[
'label' => 'Anwendungsschlüssel',
'status' => filled(config('app.key')) ? self::OK : self::ERROR,
'message' => filled(config('app.key')) ? 'Gesetzt' : 'APP_KEY fehlt.',
],
];
}
/**
* @return array{label: string, status: string, message: string}
*/
private function databaseCheck(): array
{
try {
$version = (string) DB::selectOne('select version() as v')->v;
$mariadb = str_contains(strtolower($version), 'mariadb');
$number = preg_replace('/^(\d+\.\d+\.\d+).*$/', '$1', $version);
$ok = version_compare($number, $mariadb ? '10.6.0' : '8.0.0', '>=');
return [
'label' => 'Datenbank',
'status' => $ok ? self::OK : self::WARNING,
'message' => ($mariadb ? 'MariaDB ' : 'MySQL ').$number.($ok ? '' : ' – unterstützt werden MySQL ab 8.0 und MariaDB ab 10.6.'),
];
} catch (Throwable) {
return ['label' => 'Datenbank', 'status' => self::ERROR, 'message' => 'Keine Verbindung.'];
}
}
/**
* @return array{label: string, status: string, message: string}
*/
private function check(string $label, bool $ok, string $okMessage, string $errorMessage): array
{
return ['label' => $label, 'status' => $ok ? self::OK : self::ERROR, 'message' => $ok ? $okMessage : $errorMessage];
}
private function writable(string $path): bool
{
return is_dir($path) ? is_writable($path) : is_writable(dirname($path));
}
private function ago(int $timestamp): string
{
return Carbon::createFromTimestamp($timestamp)->locale('de')->diffForHumans();
}
public static function bytes(string $value): int
{
$value = trim($value);
if ($value === '' || $value === '-1') {
return -1;
}
$number = (int) $value;
return match (strtolower(substr($value, -1))) {
'g' => $number * 1024 ** 3,
'm' => $number * 1024 ** 2,
'k' => $number * 1024,
default => $number,
};
}
}