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

87 lines
2.9 KiB
PHP

<?php
namespace App\Livewire\Ui\Security;
use App\Models\Setting;
use Illuminate\Support\Facades\Process;
use Livewire\Attributes\Layout;
use Livewire\Attributes\Title;
use Livewire\Component;
#[Layout('layouts.dvx')]
#[Title('Rspamd · Mailwolt')]
class RspamdForm extends Component
{
public float $spam_score = 5.0;
public float $greylist_score = 4.0;
public float $reject_score = 15.0;
public bool $enabled = true;
public function mount(): void
{
$this->spam_score = (float) Setting::get('rspamd.spam_score', 5.0);
$this->greylist_score = (float) Setting::get('rspamd.greylist_score', 4.0);
$this->reject_score = (float) Setting::get('rspamd.reject_score', 15.0);
$this->enabled = (bool) Setting::get('rspamd.enabled', true);
}
public function save(): void
{
$this->validate([
'spam_score' => 'required|numeric|min:1|max:50',
'greylist_score' => 'required|numeric|min:0|max:50',
'reject_score' => 'required|numeric|min:1|max:100',
]);
Setting::setMany([
'rspamd.spam_score' => $this->spam_score,
'rspamd.greylist_score' => $this->greylist_score,
'rspamd.reject_score' => $this->reject_score,
'rspamd.enabled' => $this->enabled,
]);
try {
$this->writeRspamdConfig();
Process::run(['sudo', '-n', '/usr/bin/systemctl', 'reload-or-restart', 'rspamd']);
$this->dispatch('toast', type: 'done', badge: 'Rspamd',
title: 'Einstellungen gespeichert',
text: 'Rspamd-Konfiguration wurde übernommen und neu geladen.', duration: 5000);
} catch (\Throwable $e) {
$this->dispatch('toast', type: 'error', badge: 'Rspamd',
title: 'Fehler', text: $e->getMessage(), duration: 0);
}
}
private function writeRspamdConfig(): void
{
$target = '/etc/rspamd/local.d/mailwolt-actions.conf';
$content = <<<CONF
actions {
reject = {$this->reject_score};
add_header = {$this->spam_score};
greylist = {$this->greylist_score};
}
CONF;
$proc = proc_open(
sprintf('sudo -n /usr/bin/tee %s >/dev/null', escapeshellarg($target)),
[0 => ['pipe', 'r'], 1 => ['pipe', 'w'], 2 => ['pipe', 'w']],
$pipes
);
if (!is_resource($proc)) throw new \RuntimeException('tee start fehlgeschlagen');
fwrite($pipes[0], $content);
fclose($pipes[0]);
stream_get_contents($pipes[1]);
stream_get_contents($pipes[2]);
if (proc_close($proc) !== 0) throw new \RuntimeException("tee failed writing to {$target}");
}
public function render()
{
$r = Process::run(['systemctl', 'is-active', 'rspamd']);
$running = trim($r->output()) === 'active';
return view('livewire.ui.security.rspamd-form', compact('running'));
}
}