From 11bba1e60c94b4f6661f52afd69eeb00c29ad00d Mon Sep 17 00:00:00 2001 From: Alexander Andreev Date: Sat, 21 Feb 2026 16:17:20 +0300 Subject: [PATCH] Public: retry comment submit endpoint to handle 404 routes --- index.php | 67 ++++++++++++++++++++++++++++++++++++++++++------------- 1 file changed, 52 insertions(+), 15 deletions(-) diff --git a/index.php b/index.php index 19df270..5e1236b 100644 --- a/index.php +++ b/index.php @@ -39,7 +39,7 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST' && (string)($_POST['action'] ?? '') == $commentSaved = true; } catch (Throwable $e) { error_log('Comment add failed: ' . $e->getMessage()); - $errorMessage = 'Ошибка отправки комментария: ' . $e->getMessage(); + $errorMessage = 'Не удалось отправить комментарий.'; $errorCode = 500; } } else { @@ -691,7 +691,7 @@ function outputWatermarked(string $path, string $mime): never

Комментарии

-
+ @@ -996,21 +996,57 @@ function outputWatermarked(string $path, string $mime): never setCommentFeedback('', false); try { - const response = await fetch(commentForm.action, { - method: 'POST', - body: formData, - headers: { - 'Accept': 'application/json', - 'X-Requested-With': 'XMLHttpRequest' + const endpoints = []; + const pushEndpoint = (url) => { + if (!url) { + return; } - }); + if (!endpoints.includes(url)) { + endpoints.push(url); + } + }; - const raw = await response.text(); + pushEndpoint(commentForm.action || window.location.href); + + const scriptPath = String(commentForm.dataset.scriptPath || '').trim(); + if (scriptPath !== '') { + const fallback = new URL(scriptPath, window.location.origin); + fallback.search = window.location.search; + pushEndpoint(fallback.toString()); + } + + let response = null; + let raw = ''; let payload = null; - try { - payload = JSON.parse(raw); - } catch { - payload = null; + let usedEndpoint = ''; + + for (const endpoint of endpoints) { + usedEndpoint = endpoint; + response = await fetch(endpoint, { + method: 'POST', + body: formData, + headers: { + 'Accept': 'application/json', + 'X-Requested-With': 'XMLHttpRequest' + } + }); + + raw = await response.text(); + try { + payload = JSON.parse(raw); + } catch { + payload = null; + } + + if (response.status === 404 && endpoints[endpoints.length - 1] !== endpoint) { + continue; + } + + break; + } + + if (!response) { + throw new Error('Не удалось отправить комментарий.'); } if (!payload) { @@ -1019,7 +1055,8 @@ function outputWatermarked(string $path, string $mime): never return; } - throw new Error(raw.trim() !== '' ? raw.slice(0, 220) : 'Не удалось отправить комментарий.'); + const rawMessage = raw.trim() !== '' ? raw.slice(0, 220) : ''; + throw new Error(rawMessage !== '' ? `HTTP ${response.status}: ${rawMessage}` : `HTTP ${response.status}: ${usedEndpoint}`); } if (!response.ok || payload.ok !== true) {