mailwolt/app/Livewire/Ui/Webmail/Settings.php

193 lines
9.0 KiB
PHP

<?php
namespace App\Livewire\Ui\Webmail;
use App\Models\MailUser;
use Livewire\Attributes\Layout;
use Livewire\Attributes\Title;
use Livewire\Attributes\Url;
use Livewire\Component;
#[Layout('layouts.webmail')]
#[Title('Einstellungen · Webmail')]
class Settings extends Component
{
#[Url(as: 'tab')]
public string $activeTab = 'allgemein';
// ── Allgemein ────────────────────────────────────────────────────────────
public bool $showSubscribedOnly = false;
public bool $fetchUnreadAll = false;
public bool $threadMessages = false;
public bool $showFullAddress = false;
public bool $hideEmbeddedAttachments = false;
public bool $attachmentsAbove = false;
public bool $autoMarkRead = true;
public int $autoMarkReadDelay = 0;
public string $forwardMode = 'inline';
public bool $replyBelowQuote = false;
public bool $signatureNew = true;
public bool $signatureReply = false;
public bool $signatureForward = false;
public bool $composeHtml = true;
public int $defaultFontSize = 13;
public string $showRemoteImages = 'never';
public int $autosaveInterval = 5;
public string $signature = '';
// ── Filter ───────────────────────────────────────────────────────────────
public array $filterRules = [];
public string $newField = 'from';
public string $newOp = 'is';
public string $newValue = '';
public string $newAction = 'discard';
public string $newFolder = 'Junk';
// ── Abwesenheit ──────────────────────────────────────────────────────────
public bool $vacationEnabled = false;
public string $vacationSubject = '';
public string $vacationBody = '';
// ── Weiterleitung ─────────────────────────────────────────────────────────
public string $forwardTo = '';
// ─────────────────────────────────────────────────────────────────────────
public function mount(): void
{
if (! session('webmail_email')) {
$this->redirect(route('ui.webmail.login'));
return;
}
$user = MailUser::where('email', session('webmail_email'))->first();
if (! $user) return;
$prefs = $user->webmail_prefs ?? [];
$this->showSubscribedOnly = (bool) ($prefs['show_subscribed_only'] ?? false);
$this->fetchUnreadAll = (bool) ($prefs['fetch_unread_all'] ?? false);
$this->threadMessages = (bool) ($prefs['thread_messages'] ?? false);
$this->showFullAddress = (bool) ($prefs['show_full_address'] ?? false);
$this->hideEmbeddedAttachments = (bool) ($prefs['hide_embedded_attachments'] ?? false);
$this->attachmentsAbove = (bool) ($prefs['attachments_above'] ?? false);
$this->autoMarkRead = (bool) ($prefs['auto_mark_read'] ?? true);
$this->autoMarkReadDelay = (int) ($prefs['auto_mark_read_delay'] ?? 0);
$this->forwardMode = (string)($prefs['forward_mode'] ?? 'inline');
$this->replyBelowQuote = (bool) ($prefs['reply_below_quote'] ?? false);
$this->signatureNew = (bool) ($prefs['signature_new'] ?? true);
$this->signatureReply = (bool) ($prefs['signature_reply'] ?? false);
$this->signatureForward = (bool) ($prefs['signature_forward'] ?? false);
$this->composeHtml = (bool) ($prefs['compose_html'] ?? true);
$this->defaultFontSize = (int) ($prefs['default_font_size'] ?? 13);
$this->showRemoteImages = (string)($prefs['show_remote_images'] ?? 'never');
$this->autosaveInterval = (int) ($prefs['autosave_interval'] ?? 5);
$this->signature = (string)($user->signature ?? '');
$this->filterRules = $user->sieve_filter_rules ?? [];
$this->vacationEnabled = (bool) $user->vacation_enabled;
$this->vacationSubject = (string)($user->vacation_subject ?? '');
$this->vacationBody = (string)($user->vacation_body ?? '');
$this->forwardTo = (string)($user->forward_to ?? '');
}
public function save(): void
{
$this->validate([
'forwardTo' => 'nullable|email',
'vacationSubject' => 'nullable|string|max:255',
'vacationBody' => 'nullable|string|max:4000',
'newValue' => 'nullable|string|max:255',
]);
$user = MailUser::where('email', session('webmail_email'))->first();
if (! $user) return;
$user->update([
'webmail_prefs' => [
'show_subscribed_only' => $this->showSubscribedOnly,
'fetch_unread_all' => $this->fetchUnreadAll,
'thread_messages' => $this->threadMessages,
'show_full_address' => $this->showFullAddress,
'hide_embedded_attachments' => $this->hideEmbeddedAttachments,
'attachments_above' => $this->attachmentsAbove,
'auto_mark_read' => $this->autoMarkRead,
'auto_mark_read_delay' => $this->autoMarkReadDelay,
'forward_mode' => $this->forwardMode,
'reply_below_quote' => $this->replyBelowQuote,
'signature_new' => $this->signatureNew,
'signature_reply' => $this->signatureReply,
'signature_forward' => $this->signatureForward,
'compose_html' => $this->composeHtml,
'default_font_size' => $this->defaultFontSize,
'show_remote_images' => $this->showRemoteImages,
'autosave_interval' => $this->autosaveInterval,
],
'signature' => $this->signature ?: null,
'sieve_filter_rules' => $this->filterRules,
'vacation_enabled' => $this->vacationEnabled,
'vacation_subject' => $this->vacationSubject ?: null,
'vacation_body' => $this->vacationBody ?: null,
'forward_to' => $this->forwardTo ?: null,
]);
$this->applySieve($user->fresh());
$this->dispatch('toast', type: 'done', badge: 'Webmail',
title: 'Gespeichert', text: 'Einstellungen wurden gespeichert.', duration: 3000);
}
public function addFilterRule(): void
{
$this->validate(['newValue' => 'required|string|max:255']);
$this->filterRules[] = [
'id' => uniqid('r', true),
'field' => $this->newField,
'op' => $this->newOp,
'value' => trim($this->newValue),
'action' => $this->newAction,
'folder' => $this->newFolder,
];
$this->newValue = '';
}
public function removeFilterRule(string $id): void
{
$this->filterRules = array_values(
array_filter($this->filterRules, fn($r) => $r['id'] !== $id)
);
}
private function applySieve(MailUser $user): void
{
$rules = json_encode($user->sieve_filter_rules ?? []);
$email = escapeshellarg($user->email);
$forward = escapeshellarg($user->forward_to ?? '');
$vacEn = $user->vacation_enabled ? '1' : '0';
$vacSubj = escapeshellarg($user->vacation_subject ?? 'Ich bin derzeit nicht erreichbar.');
$vacBody = escapeshellarg($user->vacation_body ?? '');
$rulesEsc= escapeshellarg($rules);
$cmd = "sudo -n /usr/local/sbin/mailwolt-apply-sieve"
. " --email {$email}"
. " --forward {$forward}"
. " --vacation-enabled {$vacEn}"
. " --vacation-subject {$vacSubj}"
. " --vacation-body {$vacBody}"
. " --filter-rules {$rulesEsc}"
. " 2>&1";
$out = shell_exec($cmd);
if ($out) {
\Illuminate\Support\Facades\Log::info('mailwolt-apply-sieve', ['output' => $out]);
}
}
public function render()
{
return view('livewire.ui.webmail.settings');
}
}