mailwolt/app/Livewire/Ui/Security/SpamAvCard.php

134 lines
4.5 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

<?php
namespace App\Livewire\Ui\Security;
use Livewire\Component;
use Illuminate\Support\Facades\Cache;
use App\Models\Setting;
class SpamAvCard extends Component
{
public int $ham = 0;
public int $spam = 0;
public int $reject = 0;
public string $rspamdVer = '';
public string $clamVer = '';
public ?string $sigUpdated = null;
// wie frisch müssen Zahlen mindestens sein (Sek.)
protected int $maxAge = 300;
public function mount(): void
{
$this->load();
}
public function render()
{
return view('livewire.ui.security.spam-av-card');
}
public function refresh(): void
{
$this->load(true);
}
protected function load(bool $force = false): void
{
// 1) Versuche erst aus Settings (DB/Redis), optional mit Cache-Shortcuts
$data = $force ? null : Cache::get('dash.spamav');
if (!$data) {
$data = Setting::get('spamav.metrics');
}
// 2) Wenn keine Daten oder zu alt ⇒ neu sammeln
$stale = !is_array($data) || (time() - (int)($data['ts'] ?? 0) > $this->maxAge);
if ($force || $stale) {
$data = $this->collectMetrics();
// Persistenz: Settings (DB→Redis) + UI-Cache (kurz)
Setting::set('spamav.metrics', $data);
Cache::put('dash.spamav', $data, 60);
}
// 3) Auf Properties mappen
$this->ham = (int)($data['ham'] ?? 0);
$this->spam = (int)($data['spam'] ?? 0);
$this->reject = (int)($data['reject'] ?? 0);
$this->rspamdVer = (string)($data['rspamdVer'] ?? ''); // <- wichtig
$this->clamVer = (string)($data['clamVer'] ?? '');
$this->sigUpdated = $data['sigUpdated'] ?? null;
}
/** Sammelt live von rspamd/clamav robust und schnell */
protected function collectMetrics(): array
{
// rspamd counters
$out = trim(@shell_exec('rspamc counters 2>/dev/null') ?? '');
$ham = preg_match('/\bham\s*:\s*(\d+)/i', $out, $m1) ? (int)$m1[1] : 0;
$spam = preg_match('/\bspam\s*:\s*(\d+)/i', $out, $m2) ? (int)$m2[1] : 0;
$reject = preg_match('/\breject\s*:\s*(\d+)/i', $out, $m3) ? (int)$m3[1] : 0;
// Versionen
$clamLine = trim((string) @shell_exec('clamd --version 2>/dev/null || clamscan --version 2>/dev/null'));
$clamVer = $clamLine !== '' ? $clamLine : '';
$sigUpdated = null;
if ($clamLine && preg_match('#/([^/]+\d{4})$#', $clamLine, $m)) {
$sigUpdated = $m[1];
}
return [
'ts' => time(),
'ham' => $ham,
'spam' => $spam,
'reject' => $reject,
'clamLine' => $clamLine,
'clamVer' => $clamVer,
'sigUpdated' => $sigUpdated,
];
}
}
//
//namespace App\Livewire\Ui\Security;
//
//use Livewire\Component;
//use Illuminate\Support\Facades\Cache;
//
//class SpamAvCard extends Component
//{
// public int $ham = 0;
// public int $spam = 0;
// public int $reject = 0;
// public string $rspamdVer = '';
// public string $clamVer = '';
// public ?string $sigUpdated = null;
//
// public function mount(): void { $this->load(); }
// public function render() { return view('livewire.ui.security.spam-av-card'); }
// public function refresh(): void { $this->load(true); }
//
// protected function load(bool $force = false): void
// {
// $data = Cache::remember('dash.spamav', $force ? 1 : 60, function () {
// $out = trim(@shell_exec('rspamc counters 2>/dev/null') ?? '');
// // very rough counters (adapt to your setup)
// $ham = preg_match('/ham:\s*(\d+)/i', $out, $m1) ? (int)$m1[1] : 0;
// $spam = preg_match('/spam:\s*(\d+)/i', $out, $m2) ? (int)$m2[1] : 0;
// $reject = preg_match('/reject:\s*(\d+)/i', $out, $m3) ? (int)$m3[1] : 0;
//
// $rspamdVer = trim(@shell_exec('rspamadm version 2>/dev/null') ?? '') ?: '';
// $clamVer = trim(@shell_exec('clamd --version 2>/dev/null || clamscan --version 2>/dev/null') ?? '') ?: '';
//
// // last signatures update (freshclam log)
// $sigUpdated = null;
// $log = @shell_exec('grep -i "Database updated" /var/log/clamav/freshclam.log | tail -n1 2>/dev/null');
// if ($log) $sigUpdated = trim($log);
//
// return compact('ham','spam','reject','rspamdVer','clamVer','sigUpdated');
// });
//
// foreach ($data as $k => $v) $this->$k = $v;
// }
//}