[SPIP] ~maj v3.0.14-->v3.0.17
[ptitvelo/web/www.git] / www / ecrire / inc / texte_mini.php
index d1193a0..f3c2429 100644 (file)
@@ -3,7 +3,7 @@
 /***************************************************************************\
  *  SPIP, Systeme de publication pour l'internet                           *
  *                                                                         *
- *  Copyright (c) 2001-2012                                                *
+ *  Copyright (c) 2001-2014                                                *
  *  Arnaud Martin, Antoine Pitrou, Philippe Riviere, Emmanuel Saint-James  *
  *                                                                         *
  *  Ce programme est un logiciel libre distribue sous licence GNU/GPL.     *
@@ -94,7 +94,7 @@ function traiter_echap_html_dist($regs) {
 // http://doc.spip.org/@traiter_echap_code_dist
 function traiter_echap_code_dist($regs) {
        list(,,$att,$corps) = $regs;
-       $echap = htmlspecialchars($corps); // il ne faut pas passer dans entites_html, ne pas transformer les &#xxx; du code !
+       $echap = spip_htmlspecialchars($corps); // il ne faut pas passer dans entites_html, ne pas transformer les &#xxx; du code !
 
        // ne pas mettre le <div...> s'il n'y a qu'une ligne
        if (is_int(strpos($echap,"\n"))) {
@@ -142,7 +142,21 @@ function traiter_echap_script_dist($regs) {
        return $regs[0];
 }
 
-define('_PROTEGE_BLOCS', ',<(html|code|cadre|frame|script)(\s[^>]*)?>(.*)</\1>,UimsS');
+/**
+ * Traiter les blocs <math></math> echappes par echappe_html
+ * @param $regs
+ * @return string
+ */
+function traiter_echap_math_dist($regs) {
+       // Gestion du TeX
+       if (!function_exists('traiter_math'))
+               include_spip('inc/math');
+
+       $t = traiter_math($regs[0], '');
+       return $t;
+}
+
+define('_PROTEGE_BLOCS', ',<(html|code|cadre|frame|script|math)(\s[^>]*)?>(.*)</\1>,UimsS');
 
 // - pour $source voir commentaire infra (echappe_retour)
 // - pour $no_transform voir le filtre post_autobr dans inc/filtres
@@ -163,7 +177,7 @@ $preg='') {
        }
 
        if (($preg OR strpos($letexte,"<")!==false)
-         AND preg_match_all($preg ? $preg : _PROTEGE_BLOCS, $letexte, $matches, PREG_SET_ORDER))
+         AND preg_match_all($preg ? $preg : _PROTEGE_BLOCS, $letexte, $matches, PREG_SET_ORDER)) {
                foreach ($matches as $regs) {
                        // echappements tels quels ?
                        if ($no_transform) {
@@ -179,23 +193,34 @@ $preg='') {
                        $p = strpos($letexte,$regs[0]);
                        $letexte = substr_replace($letexte,code_echappement($echap, $source, $no_transform),$p,strlen($regs[0]));
                }
+       }
 
        if ($no_transform)
                return $letexte;
 
        // Gestion du TeX
-       if (strpos($letexte, "<math>") !== false) {
-               include_spip('inc/math');
-               $letexte = traiter_math($letexte, $source);
+       // code mort sauf si on a personalise _PROTEGE_BLOCS sans y mettre <math>
+       // eviter la rupture de compat en branche 3.0
+       // a supprimer en branche 3.1
+       if (strpos($preg ? $preg : _PROTEGE_BLOCS,'code')!==false){
+               if (strpos($letexte, "<math>") !== false) {
+                       include_spip('inc/math');
+                       $letexte = traiter_math($letexte, $source);
+               }
        }
 
        // Echapper le php pour faire joli (ici, c'est pas pour la securite)
-       if (strpos($letexte,"<"."?")!==false AND preg_match_all(',<[?].*($|[?]>),UisS',
-       $letexte, $matches, PREG_SET_ORDER))
-       foreach ($matches as $regs) {
-               $letexte = str_replace($regs[0],
-                       code_echappement(highlight_string($regs[0],true), $source),
-                       $letexte);
+       // seulement si on a echappe les <script>
+       // (derogatoire car on ne peut pas faire passer < ? ... ? >
+       // dans une callback autonommee
+       if (strpos($preg ? $preg : _PROTEGE_BLOCS,'script')!==false){
+               if (strpos($letexte,"<"."?")!==false AND preg_match_all(',<[?].*($|[?]>),UisS',
+               $letexte, $matches, PREG_SET_ORDER))
+               foreach ($matches as $regs) {
+                       $letexte = str_replace($regs[0],
+                               code_echappement(highlight_string($regs[0],true), $source),
+                               $letexte);
+               }
        }
 
        return $letexte;
@@ -208,10 +233,13 @@ $preg='') {
 // http://doc.spip.org/@echappe_retour
 function echappe_retour($letexte, $source='', $filtre = "") {
        if (strpos($letexte,"base64$source")) {
-               # spip_log(htmlspecialchars($letexte));  ## pour les curieux
-               if (strpos($letexte,"<")!==false AND
+               # spip_log(spip_htmlspecialchars($letexte));  ## pour les curieux
+               $max_prof = 5;
+               while (strpos($letexte,"<")!==false
+                       AND
                  preg_match_all(',<(span|div)\sclass=[\'"]base64'.$source.'[\'"]\s(.*)>\s*</\1>,UmsS',
-               $letexte, $regs, PREG_SET_ORDER)) {
+               $letexte, $regs, PREG_SET_ORDER)
+                 AND $max_prof--) {
                        foreach ($regs as $reg) {
                                $rempl = base64_decode(extraire_attribut($reg[0], 'title'));
                                // recherche d'attributs supplementaires