From 2b291c56909af26b525d250bcc17df10fb1745b7 Mon Sep 17 00:00:00 2001
From: Cerdic <cedric@yterium.com>
Date: Thu, 18 Feb 2021 15:40:35 +0100
Subject: [PATCH] Quand un contexte ajax est invalide (corrompu ou trop long,
 ou on a vide le cache sur le disque), renvoyer une erreur 400 bad request +
 cote JS traiter le cas d'erreur, marquer le bloc ajax invalide, et rediriger
 sans ajax si c'etait la nouvelle url de la page

---
 ecrire/inc/headers.php           |  1 +
 ecrire/public/aiguiller.php      |  2 +-
 prive/javascript/ajaxCallback.js | 17 ++++++++++++++++-
 3 files changed, 18 insertions(+), 2 deletions(-)

diff --git a/ecrire/inc/headers.php b/ecrire/inc/headers.php
index fc24faae0c..759be7ab83 100644
--- a/ecrire/inc/headers.php
+++ b/ecrire/inc/headers.php
@@ -205,6 +205,7 @@ function http_status($status) {
 		301 => '301 Moved Permanently',
 		302 => '302 Found',
 		304 => '304 Not Modified',
+		400 => '400 Bad Request',
 		401 => '401 Unauthorized',
 		403 => '403 Forbidden',
 		404 => '404 Not Found',
diff --git a/ecrire/public/aiguiller.php b/ecrire/public/aiguiller.php
index d42ca099c2..d9143ba83c 100644
--- a/ecrire/public/aiguiller.php
+++ b/ecrire/public/aiguiller.php
@@ -149,7 +149,7 @@ function traiter_appels_inclusions_ajax() {
 			}
 		} else {
 			include_spip('inc/headers');
-			http_status(403);
+			http_status(400);
 			$texte = _L('signature ajax bloc incorrecte');
 		}
 		ajax_retour($texte, false);
diff --git a/prive/javascript/ajaxCallback.js b/prive/javascript/ajaxCallback.js
index 898f17680d..43469f9a9d 100644
--- a/prive/javascript/ajaxCallback.js
+++ b/prive/javascript/ajaxCallback.js
@@ -518,6 +518,20 @@ jQuery.spip.on_ajax_loaded = function(blocfrag,c,href,history) {
 	jQuery.spip.updateReaderBuffer();
 }
 
+jQuery.spip.on_ajax_failed = function(blocfrag,statusCode,href,history) {
+	// marquer le bloc invalide
+	jQuery(blocfrag).addClass('invalid');
+	// si c'est une erreur 400 on a perdu la signature ajax
+	//console.log("AJAX Erreur");
+	//console.log(statusCode);
+	history = history || (history==null);
+	// quelle que soit l'erreur, on redirige si c'était la nouvelle URL principale de la page
+	if (history) {
+		//console.log("On redirige : " + href);
+		window.location.href = href;
+	}
+}
+
 jQuery.spip.stateId=0;
 jQuery.spip.setHistoryState = function(blocfrag){
 	if (!window.history.replaceState) return;
@@ -611,8 +625,9 @@ jQuery.spip.loadAjax = function(blocfrag,url, href, options){
 				if (options.callback && typeof options.callback == "function")
 					options.callback.apply(blocfrag);
 			},
-			error: function(){
+			error: function(e){
 				jQuery.spip.preloaded_urls[url]='';
+				jQuery.spip.on_ajax_failed(blocfrag,e.status,href,options.history);
 			}
 		});
 	}
-- 
GitLab