fix: repair form submission with queued files + add comprehensive debug logging

- Replace fetch(redirect:manual) with XMLHttpRequest in file-upload-queue.js.
  The previous fetch-based redirect detection was broken because opaque
  redirects hide the Location header. XHR's responseURL reliably exposes
  the final URL after server-side redirects.

- Add console.log tracing at every decision point in submit interception:
  entry, hasFiles check, enctype check, double-submit guard, XHR status,
  redirect detection, error fallback.

- Add error_log entry-point logging to all 16 admin action files plus
  the partage/index.php submission handler and password gate. Each logs:
  request method, content type/length, POST keys, file counts, and
  queue-specific file counts where applicable.

- Add double-submit guard (_xamxamActiveSubmit) to prevent duplicate
  XHR sends when the native submit handler fires after interception.
This commit is contained in:
Pontoporeia
2026-05-10 18:08:27 +02:00
parent 13d26ded66
commit d5fee1acfb
23 changed files with 114 additions and 28 deletions

View File

@@ -363,6 +363,9 @@
// ── Form submit interception ───────────────────────────────────────────
// Track whether we've already intercepted this submit to prevent double-submit
var _xamxamActiveSubmit = false;
function bindFormSubmit() {
document.querySelectorAll("form[data-beforeunload-guard]").forEach(function (form) {
if (form.dataset.xamxamFormBound) return;
@@ -370,10 +373,29 @@
form.addEventListener("submit", function (e) {
var hasFiles = QUEUE_TYPES.some(function (qt) { return queues[qt].length > 0; });
if (!hasFiles) return; // Normal submit
console.log("[file-upload-queue] submit event fired | action=" + (form.getAttribute("action") || "") + " | hasFiles=" + hasFiles + " | enctype=" + form.enctype);
if (!hasFiles) {
console.log("[file-upload-queue] no queued files, passing through native submit");
markClean();
return; // Normal submit
}
// Check if the form can accept multipart
if (form.enctype !== "multipart/form-data") return;
if (form.enctype !== "multipart/form-data") {
console.log("[file-upload-queue] form enctype is not multipart/form-data, passing through");
markClean();
return;
}
// Prevent double-submit (in case native submit falls through)
if (_xamxamActiveSubmit) {
console.log("[file-upload-queue] already submitting, skipping");
e.preventDefault();
return;
}
_xamxamActiveSubmit = true;
e.preventDefault();
@@ -392,32 +414,53 @@
markClean();
// Use fetch so we can inspect the response before navigation.
// The server either redirects (302 → we navigate to that URL)
// or returns the form page with errors (200 → replace the page).
fetch(form.getAttribute("action") || "", {
method: "POST",
body: fd,
redirect: "manual",
}).then(function (resp) {
if (resp.type === "opaqueredirect" || resp.status === 302 || resp.status === 301) {
// Browser redirect — navigate the whole page
window.location.href = resp.headers.get("Location") || form.getAttribute("action");
console.log("[file-upload-queue] injecting " + QUEUE_TYPES.map(function(qt) { return qt + ":" + queues[qt].length; }).join(", ") + " files into FormData");
// Use XMLHttpRequest instead of fetch so we can reliably read the
// final URL after redirects (fetch with redirect:manual hides headers).
var xhr = new XMLHttpRequest();
xhr.open("POST", form.getAttribute("action") || "", true);
xhr.responseType = "text";
xhr.onload = function () {
_xamxamActiveSubmit = false;
var finalUrl = xhr.responseURL || "";
console.log("[file-upload-queue] XHR status=" + xhr.status + " | finalUrl=" + finalUrl + " | responseLength=" + (xhr.responseText ? xhr.responseText.length : 0));
// If the server redirected us (responseURL differs from action), navigate there.
// This handles the 302 redirect pattern used by formulaire.php and edit.php.
var actionUrl = form.getAttribute("action") || "";
if (finalUrl && finalUrl !== actionUrl && finalUrl !== window.location.href) {
console.log("[file-upload-queue] redirecting to " + finalUrl);
window.location.href = finalUrl;
return;
}
// Success or error — render the HTML response
return resp.text();
}).then(function (html) {
if (!html) return;
document.open();
document.write(html);
document.close();
// Re-bind after DOM replacement (for error re-renders)
setTimeout(window.XamxamInitFileUploads, 0);
}).catch(function () {
// Network error — fall back to native submit
// 200 with HTML — likely form errors, replace page
if (xhr.status >= 200 && xhr.status < 300 && xhr.responseText) {
console.log("[file-upload-queue] rendering error response HTML");
document.open();
document.write(xhr.responseText);
document.close();
// Re-bind after DOM replacement (for error re-renders)
setTimeout(window.XamxamInitFileUploads, 0);
return;
}
// Fallback: reload current page
console.log("[file-upload-queue] unexpected response, reloading page");
window.location.reload();
};
xhr.onerror = function () {
_xamxamActiveSubmit = false;
console.error("[file-upload-queue] XHR network error, falling back to native submit");
// Fall back to native submit
form.submit();
});
};
xhr.send(fd);
});
});
}