From ecd7d2b341c84173d8dc58961b2e1a0419112c3d Mon Sep 17 00:00:00 2001 From: smueller Date: Tue, 7 Jul 2026 10:44:07 +0200 Subject: [PATCH] Add flash messaging for newsletter actions in index.php and wiki.php. Implemented session handling for success and error messages, and added redirection after form submissions to enhance user experience. --- index.php | 28 +++++++++++++++++++++++++++- wiki.php | 32 +++++++++++++++++++++++++++++++- 2 files changed, 58 insertions(+), 2 deletions(-) diff --git a/index.php b/index.php index 71f1ebf..68efb32 100644 --- a/index.php +++ b/index.php @@ -726,6 +726,15 @@ $error = ''; $previewHtml = ''; $previewClipboardHtml = ''; +$flash = $_SESSION['wiki_newsletter_flash'] ?? null; +if (is_array($flash)) { + $message = (string)($flash['message'] ?? ''); + $error = (string)($flash['error'] ?? ''); + $previewHtml = (string)($flash['preview_html'] ?? ''); + $previewClipboardHtml = (string)($flash['preview_clipboard_html'] ?? ''); + unset($_SESSION['wiki_newsletter_flash']); +} + $isCron = (PHP_SAPI === 'cli' && cliArg('cron') !== null) || isset($_GET['cron']); if ($isCron) { @@ -755,6 +764,8 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST' && ($_POST['action'] ?? '') === 'login } if ($_SERVER['REQUEST_METHOD'] === 'POST' && ($_POST['action'] ?? '') !== 'login') { + $redirectUrl = strtok($_SERVER['REQUEST_URI'], '?'); + try { requireAdmin(); $config = configFromPost($config); @@ -774,9 +785,24 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST' && ($_POST['action'] ?? '') !== 'login } elseif ($action === 'monthly_send') { $message = sendMonthlyIfDue($config); } + + $_SESSION['wiki_newsletter_flash'] = [ + 'message' => $message, + 'error' => '', + 'preview_html' => $previewHtml, + 'preview_clipboard_html' => $previewClipboardHtml, + ]; } catch (Throwable $e) { - $error = $e->getMessage(); + $_SESSION['wiki_newsletter_flash'] = [ + 'message' => '', + 'error' => $e->getMessage(), + 'preview_html' => '', + 'preview_clipboard_html' => '', + ]; } + + header('Location: ' . $redirectUrl); + exit; } $isLoggedIn = isAdminLoggedIn(); diff --git a/wiki.php b/wiki.php index 158b75e..44106a8 100644 --- a/wiki.php +++ b/wiki.php @@ -170,6 +170,10 @@ function fetchLatestArticles(string $wikiUrl, int $limit = 150): array $nodes = $xpath->query('//li'); foreach ($nodes as $li) { + if (!$li instanceof DOMElement) { + continue; + } + $text = trim(preg_replace('/\s+/', ' ', $li->textContent ?? '')); if (!preg_match('/\((\d{2}\.\d{2}\.\d{4})\)/', $text, $dateMatch)) { @@ -722,6 +726,15 @@ $error = ''; $previewHtml = ''; $previewClipboardHtml = ''; +$flash = $_SESSION['wiki_newsletter_flash'] ?? null; +if (is_array($flash)) { + $message = (string)($flash['message'] ?? ''); + $error = (string)($flash['error'] ?? ''); + $previewHtml = (string)($flash['preview_html'] ?? ''); + $previewClipboardHtml = (string)($flash['preview_clipboard_html'] ?? ''); + unset($_SESSION['wiki_newsletter_flash']); +} + $isCron = (PHP_SAPI === 'cli' && cliArg('cron') !== null) || isset($_GET['cron']); if ($isCron) { @@ -751,6 +764,8 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST' && ($_POST['action'] ?? '') === 'login } if ($_SERVER['REQUEST_METHOD'] === 'POST' && ($_POST['action'] ?? '') !== 'login') { + $redirectUrl = strtok($_SERVER['REQUEST_URI'], '?'); + try { requireAdmin(); $config = configFromPost($config); @@ -770,9 +785,24 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST' && ($_POST['action'] ?? '') !== 'login } elseif ($action === 'monthly_send') { $message = sendMonthlyIfDue($config); } + + $_SESSION['wiki_newsletter_flash'] = [ + 'message' => $message, + 'error' => '', + 'preview_html' => $previewHtml, + 'preview_clipboard_html' => $previewClipboardHtml, + ]; } catch (Throwable $e) { - $error = $e->getMessage(); + $_SESSION['wiki_newsletter_flash'] = [ + 'message' => '', + 'error' => $e->getMessage(), + 'preview_html' => '', + 'preview_clipboard_html' => '', + ]; } + + header('Location: ' . $redirectUrl); + exit; } $isLoggedIn = isAdminLoggedIn();