#!/bin/bash
# correctifs-2.sh : (A) jeton sur TOUTES les pages statiques qui postent vers traitement.php
#                   (B) logo .webp inexistant -> .png   (C) lecture seule : PDF et autres formulaires
# Usage : bash ~/correctifs-2.sh        (racine du site : ~/www par defaut)
set -u
ROOT="${1:-$HOME/www}"
BK="$HOME/backup-correctifs2-$(date +%Y%m%d-%H%M%S)"
cd "$ROOT" || { echo "ERREUR racine $ROOT"; exit 1; }
mkdir -p "$BK"
echo "Sauvegarde dans : $BK"

TMPD="$(mktemp -d)"
cat > "$TMPD/scr.txt" <<'VA_EOF_SCR'
<script>
/* Anti-spam : jeton signé par traitement.php, récupéré après le chargement de la page */
(function(){
  var champs=document.querySelectorAll('input[name="jeton"]');
  if(!champs.length||!window.fetch){return;}
  function jeton(){
    fetch('/traitement.php?jeton=1',{cache:'no-store',credentials:'same-origin'})
      .then(function(r){return r.text();})
      .then(function(t){for(var i=0;i<champs.length;i++){champs[i].value=t.trim();}})
      .catch(function(){});
  }
  setTimeout(jeton,600);
  setInterval(jeton,4*3600*1000);
})();
</script>
VA_EOF_SCR

echo
echo "=== A. Pages statiques avec formulaire vers traitement.php"
for p in $(find . -mindepth 2 -maxdepth 2 -name index.html -not -path './wp-*' | sort) ./index.html; do
  grep -q 'traitement\.php' "$p" || continue
  if grep -q 'name="jeton"' "$p"; then echo "DEJA OK  $p"; continue; fi
  if ! grep -q '</body>' "$p"; then echo "A VOIR   $p (pas de </body>)"; continue; fi
  mkdir -p "$BK/$(dirname "$p")"; cp -p "$p" "$BK/$p"
  SCR="$TMPD/scr.txt" perl -0pi -e 'BEGIN{local $/; open(G,"<",$ENV{SCR}); $s=<G>; close G;} s/(<form[^>]*traitement\.php[^>]*>)/$1\n        <input type="hidden" name="jeton" value="">/; s/<\/body>/$s<\/body>/;' "$p" \
    && { grep -q 'name="jeton"' "$p" && echo "MODIFIE  $p" || echo "ECHEC    $p (formulaire non reconnu)"; } || echo "ECHEC    $p"
done
rm -rf "$TMPD"

echo
echo "=== B. Logo : LogoCabView-1.webp n'existe pas, seul le .png existe"
for p in index.html avocats-droit-commercial-lille/index.html; do
  if grep -q 'LogoCabView-1\.webp' "$p" 2>/dev/null; then
    mkdir -p "$BK/$(dirname "$p")"; cp -p "$p" "$BK/$p"
    sed -i 's/LogoCabView-1\.webp/LogoCabView-1.png/g' "$p" && echo "MODIFIE  $p"
  else echo "RAS      $p"; fi
done
ls wp-content/uploads/2023/11/LogoCabView-1.* 2>/dev/null

echo
echo "=== C1. Autres fichiers (hors pages ci-dessus) qui referencent traitement.php"
grep -rIl 'traitement\.php' --include=*.php --include=*.html --include=*.js wp-content/themes wp-content/mu-plugins 2>/dev/null | head

echo
echo "=== C2. Benjamin / Vanoverschelde dans les PDF"
if command -v pdftotext >/dev/null 2>&1; then
  find wp-content/uploads -name '*.pdf' -not -path '*complianz*' | while read -r f; do
    n=$(pdftotext "$f" - 2>/dev/null | grep -ci 'vanover\|benjamin')
    echo "$n occurrence(s)  $f"
  done
else
  echo "pdftotext absent sur le serveur : il faudra telecharger les PDF pour les verifier"
fi

echo
echo "=== C3. cache avis Google"
cat avis-google.cache.json; echo
echo
echo "TERMINE. Pour annuler : cp -rp $BK/. $ROOT/"
