Completed
Push — master ( f7c2c6...b1764a )
by cam
06:08
created
ecrire/inc/texte_mini.php 2 patches
Indentation   +343 added lines, -343 removed lines patch added patch discarded remove patch
@@ -17,7 +17,7 @@  discard block
 block discarded – undo
17 17
  **/
18 18
 
19 19
 if (!defined('_ECRIRE_INC_VERSION')) {
20
-	return;
20
+    return;
21 21
 }
22 22
 include_spip('inc/filtres');
23 23
 include_spip('inc/lang');
@@ -39,21 +39,21 @@  discard block
 block discarded – undo
39 39
  **/
40 40
 function definir_puce() {
41 41
 
42
-	// Attention au sens, qui n'est pas defini de la meme facon dans
43
-	// l'espace prive (spip_lang est la langue de l'interface, lang_dir
44
-	// celle du texte) et public (spip_lang est la langue du texte)
45
-	$dir = _DIR_RESTREINT ? lang_dir() : lang_dir($GLOBALS['spip_lang']);
42
+    // Attention au sens, qui n'est pas defini de la meme facon dans
43
+    // l'espace prive (spip_lang est la langue de l'interface, lang_dir
44
+    // celle du texte) et public (spip_lang est la langue du texte)
45
+    $dir = _DIR_RESTREINT ? lang_dir() : lang_dir($GLOBALS['spip_lang']);
46 46
 
47
-	$p = 'puce' . (test_espace_prive() ? '_prive' : '');
48
-	if ($dir == 'rtl') {
49
-		$p .= '_rtl';
50
-	}
47
+    $p = 'puce' . (test_espace_prive() ? '_prive' : '');
48
+    if ($dir == 'rtl') {
49
+        $p .= '_rtl';
50
+    }
51 51
 
52
-	if (!isset($GLOBALS[$p])) {
53
-		$GLOBALS[$p] = '<span class="spip-puce '.$dir.'"><b>–</b></span>';
54
-	}
52
+    if (!isset($GLOBALS[$p])) {
53
+        $GLOBALS[$p] = '<span class="spip-puce '.$dir.'"><b>–</b></span>';
54
+    }
55 55
 
56
-	return $GLOBALS[$p];
56
+    return $GLOBALS[$p];
57 57
 }
58 58
 
59 59
 
@@ -61,13 +61,13 @@  discard block
 block discarded – undo
61 61
 // dont on souhaite qu'ils provoquent un saut de paragraphe
62 62
 
63 63
 if (!defined('_BALISES_BLOCS')) {
64
-	define('_BALISES_BLOCS',
65
-	'address|applet|article|aside|blockquote|button|center|d[ltd]|div|fieldset|fig(ure|caption)|footer|form|h[1-6r]|hgroup|head|header|iframe|li|map|marquee|nav|noscript|object|ol|pre|section|t(able|[rdh]|body|foot|extarea)|ul|script|style'
66
-	);
64
+    define('_BALISES_BLOCS',
65
+    'address|applet|article|aside|blockquote|button|center|d[ltd]|div|fieldset|fig(ure|caption)|footer|form|h[1-6r]|hgroup|head|header|iframe|li|map|marquee|nav|noscript|object|ol|pre|section|t(able|[rdh]|body|foot|extarea)|ul|script|style'
66
+    );
67 67
 }
68 68
 
69 69
 if (!defined('_BALISES_BLOCS_REGEXP')) {
70
-	define('_BALISES_BLOCS_REGEXP', ',</?(' . _BALISES_BLOCS . ')[>[:space:]],iS');
70
+    define('_BALISES_BLOCS_REGEXP', ',</?(' . _BALISES_BLOCS . ')[>[:space:]],iS');
71 71
 }
72 72
 
73 73
 //
@@ -79,26 +79,26 @@  discard block
 block discarded – undo
79 79
 // echappe est un div ou un span
80 80
 // http://code.spip.net/@code_echappement
81 81
 function code_echappement($rempl, $source = '', $no_transform = false, $mode = null) {
82
-	if (!strlen($rempl)) {
83
-		return '';
84
-	}
85
-
86
-	// Tester si on echappe en span ou en div
87
-	if (is_null($mode) or !in_array($mode, array('div', 'span'))) {
88
-		$mode = preg_match(',</?(' . _BALISES_BLOCS . ')[>[:space:]],iS', $rempl) ? 'div' : 'span';
89
-	}
90
-
91
-	// Decouper en morceaux, base64 a des probleme selon la taille de la pile
92
-	$taille = 30000;
93
-	$return = "";
94
-	for ($i = 0; $i < strlen($rempl); $i += $taille) {
95
-		// Convertir en base64 et cacher dans un attribut
96
-		// utiliser les " pour eviter le re-encodage de ' et &#8217
97
-		$base64 = base64_encode(substr($rempl, $i, $taille));
98
-		$return .= "<$mode class=\"base64$source\" title=\"$base64\"></$mode>";
99
-	}
100
-
101
-	return $return;
82
+    if (!strlen($rempl)) {
83
+        return '';
84
+    }
85
+
86
+    // Tester si on echappe en span ou en div
87
+    if (is_null($mode) or !in_array($mode, array('div', 'span'))) {
88
+        $mode = preg_match(',</?(' . _BALISES_BLOCS . ')[>[:space:]],iS', $rempl) ? 'div' : 'span';
89
+    }
90
+
91
+    // Decouper en morceaux, base64 a des probleme selon la taille de la pile
92
+    $taille = 30000;
93
+    $return = "";
94
+    for ($i = 0; $i < strlen($rempl); $i += $taille) {
95
+        // Convertir en base64 et cacher dans un attribut
96
+        // utiliser les " pour eviter le re-encodage de ' et &#8217
97
+        $base64 = base64_encode(substr($rempl, $i, $taille));
98
+        $return .= "<$mode class=\"base64$source\" title=\"$base64\"></$mode>";
99
+    }
100
+
101
+    return $return;
102 102
 
103 103
 }
104 104
 
@@ -106,64 +106,64 @@  discard block
 block discarded – undo
106 106
 // Echapper les <html>...</ html>
107 107
 // http://code.spip.net/@traiter_echap_html_dist
108 108
 function traiter_echap_html_dist($regs) {
109
-	return $regs[3];
109
+    return $regs[3];
110 110
 }
111 111
 
112 112
 // Echapper les <code>...</ code>
113 113
 // http://code.spip.net/@traiter_echap_code_dist
114 114
 function traiter_echap_code_dist($regs) {
115
-	list(, , $att, $corps) = $regs;
116
-	$echap = spip_htmlspecialchars($corps); // il ne faut pas passer dans entites_html, ne pas transformer les &#xxx; du code !
117
-
118
-	// ne pas mettre le <div...> s'il n'y a qu'une ligne
119
-	if (is_int(strpos($echap, "\n"))) {
120
-		// supprimer les sauts de ligne debut/fin
121
-		// (mais pas les espaces => ascii art).
122
-		$echap = preg_replace("/^[\n\r]+|[\n\r]+$/s", "", $echap);
123
-		$echap = nl2br($echap);
124
-		$echap = "<div style='text-align: left;' "
125
-			. "class='spip_code' dir='ltr'><code$att>"
126
-			. $echap . "</code></div>";
127
-	} else {
128
-		$echap = "<code$att class='spip_code' dir='ltr'>" . $echap . "</code>";
129
-	}
130
-
131
-	$echap = str_replace("\t", "&nbsp; &nbsp; &nbsp; &nbsp; ", $echap);
132
-	$echap = str_replace("  ", " &nbsp;", $echap);
133
-
134
-	return $echap;
115
+    list(, , $att, $corps) = $regs;
116
+    $echap = spip_htmlspecialchars($corps); // il ne faut pas passer dans entites_html, ne pas transformer les &#xxx; du code !
117
+
118
+    // ne pas mettre le <div...> s'il n'y a qu'une ligne
119
+    if (is_int(strpos($echap, "\n"))) {
120
+        // supprimer les sauts de ligne debut/fin
121
+        // (mais pas les espaces => ascii art).
122
+        $echap = preg_replace("/^[\n\r]+|[\n\r]+$/s", "", $echap);
123
+        $echap = nl2br($echap);
124
+        $echap = "<div style='text-align: left;' "
125
+            . "class='spip_code' dir='ltr'><code$att>"
126
+            . $echap . "</code></div>";
127
+    } else {
128
+        $echap = "<code$att class='spip_code' dir='ltr'>" . $echap . "</code>";
129
+    }
130
+
131
+    $echap = str_replace("\t", "&nbsp; &nbsp; &nbsp; &nbsp; ", $echap);
132
+    $echap = str_replace("  ", " &nbsp;", $echap);
133
+
134
+    return $echap;
135 135
 }
136 136
 
137 137
 // Echapper les <cadre>...</ cadre> aka <frame>...</ frame>
138 138
 // http://code.spip.net/@traiter_echap_cadre_dist
139 139
 function traiter_echap_cadre_dist($regs) {
140
-	$echap = trim(entites_html($regs[3]));
141
-	// compter les lignes un peu plus finement qu'avec les \n
142
-	$lignes = explode("\n", trim($echap));
143
-	$n = 0;
144
-	foreach ($lignes as $l) {
145
-		$n += floor(strlen($l) / 60) + 1;
146
-	}
147
-	$n = max($n, 2);
148
-	$echap = "\n<textarea readonly='readonly' cols='40' rows='$n' class='spip_cadre' dir='ltr'>$echap</textarea>";
149
-
150
-	return $echap;
140
+    $echap = trim(entites_html($regs[3]));
141
+    // compter les lignes un peu plus finement qu'avec les \n
142
+    $lignes = explode("\n", trim($echap));
143
+    $n = 0;
144
+    foreach ($lignes as $l) {
145
+        $n += floor(strlen($l) / 60) + 1;
146
+    }
147
+    $n = max($n, 2);
148
+    $echap = "\n<textarea readonly='readonly' cols='40' rows='$n' class='spip_cadre' dir='ltr'>$echap</textarea>";
149
+
150
+    return $echap;
151 151
 }
152 152
 
153 153
 // http://code.spip.net/@traiter_echap_frame_dist
154 154
 function traiter_echap_frame_dist($regs) {
155
-	return traiter_echap_cadre_dist($regs);
155
+    return traiter_echap_cadre_dist($regs);
156 156
 }
157 157
 
158 158
 // http://code.spip.net/@traiter_echap_script_dist
159 159
 function traiter_echap_script_dist($regs) {
160
-	// rendre joli (et inactif) si c'est un script language=php
161
-	if (preg_match(',<script\b[^>]+php,ims', $regs[0])) {
162
-		return highlight_string($regs[0], true);
163
-	}
160
+    // rendre joli (et inactif) si c'est un script language=php
161
+    if (preg_match(',<script\b[^>]+php,ims', $regs[0])) {
162
+        return highlight_string($regs[0], true);
163
+    }
164 164
 
165
-	// Cas normal : le script passe tel quel
166
-	return $regs[0];
165
+    // Cas normal : le script passe tel quel
166
+    return $regs[0];
167 167
 }
168 168
 
169 169
 define('_PROTEGE_BLOCS', ',<(html|code|cadre|frame|script|style)(\s[^>]*)?>(.*)</\1>,UimsS');
@@ -172,71 +172,71 @@  discard block
 block discarded – undo
172 172
 // - pour $no_transform voir le filtre post_autobr dans inc/filtres
173 173
 // http://code.spip.net/@echappe_html
174 174
 function echappe_html(
175
-	$letexte,
176
-	$source = '',
177
-	$no_transform = false,
178
-	$preg = ''
175
+    $letexte,
176
+    $source = '',
177
+    $no_transform = false,
178
+    $preg = ''
179 179
 ) {
180
-	if (!is_string($letexte) or !strlen($letexte)) {
181
-		return $letexte;
182
-	}
183
-
184
-	// si le texte recu est long PCRE risque d'exploser, on
185
-	// fait donc un mic-mac pour augmenter pcre.backtrack_limit
186
-	if (($len = strlen($letexte)) > 100000) {
187
-		if (!$old = @ini_get('pcre.backtrack_limit')) {
188
-			$old = 100000;
189
-		}
190
-		if ($len > $old) {
191
-			$a = @ini_set('pcre.backtrack_limit', $len);
192
-			spip_log("ini_set pcre.backtrack_limit=$len ($old)");
193
-		}
194
-	}
195
-
196
-	if (($preg or strpos($letexte, "<") !== false)
197
-		and preg_match_all($preg ? $preg : _PROTEGE_BLOCS, $letexte, $matches, PREG_SET_ORDER)
198
-	) {
199
-		foreach ($matches as $regs) {
200
-			// echappements tels quels ?
201
-			if ($no_transform) {
202
-				$echap = $regs[0];
203
-			} // sinon les traiter selon le cas
204
-			else {
205
-				if (function_exists($f = 'traiter_echap_' . strtolower($regs[1]))) {
206
-					$echap = $f($regs);
207
-				} else {
208
-					if (function_exists($f = $f . '_dist')) {
209
-						$echap = $f($regs);
210
-					}
211
-				}
212
-			}
213
-
214
-			$p = strpos($letexte, $regs[0]);
215
-			$letexte = substr_replace($letexte, code_echappement($echap, $source, $no_transform), $p, strlen($regs[0]));
216
-		}
217
-	}
218
-
219
-	if ($no_transform) {
220
-		return $letexte;
221
-	}
222
-
223
-	// Echapper le php pour faire joli (ici, c'est pas pour la securite)
224
-	// seulement si on a echappe les <script>
225
-	// (derogatoire car on ne peut pas faire passer < ? ... ? >
226
-	// dans une callback autonommee
227
-	if (strpos($preg ? $preg : _PROTEGE_BLOCS, 'script') !== false) {
228
-		if (strpos($letexte, "<" . "?") !== false and preg_match_all(',<[?].*($|[?]>),UisS',
229
-				$letexte, $matches, PREG_SET_ORDER)
230
-		) {
231
-			foreach ($matches as $regs) {
232
-				$letexte = str_replace($regs[0],
233
-					code_echappement(highlight_string($regs[0], true), $source),
234
-					$letexte);
235
-			}
236
-		}
237
-	}
238
-
239
-	return $letexte;
180
+    if (!is_string($letexte) or !strlen($letexte)) {
181
+        return $letexte;
182
+    }
183
+
184
+    // si le texte recu est long PCRE risque d'exploser, on
185
+    // fait donc un mic-mac pour augmenter pcre.backtrack_limit
186
+    if (($len = strlen($letexte)) > 100000) {
187
+        if (!$old = @ini_get('pcre.backtrack_limit')) {
188
+            $old = 100000;
189
+        }
190
+        if ($len > $old) {
191
+            $a = @ini_set('pcre.backtrack_limit', $len);
192
+            spip_log("ini_set pcre.backtrack_limit=$len ($old)");
193
+        }
194
+    }
195
+
196
+    if (($preg or strpos($letexte, "<") !== false)
197
+        and preg_match_all($preg ? $preg : _PROTEGE_BLOCS, $letexte, $matches, PREG_SET_ORDER)
198
+    ) {
199
+        foreach ($matches as $regs) {
200
+            // echappements tels quels ?
201
+            if ($no_transform) {
202
+                $echap = $regs[0];
203
+            } // sinon les traiter selon le cas
204
+            else {
205
+                if (function_exists($f = 'traiter_echap_' . strtolower($regs[1]))) {
206
+                    $echap = $f($regs);
207
+                } else {
208
+                    if (function_exists($f = $f . '_dist')) {
209
+                        $echap = $f($regs);
210
+                    }
211
+                }
212
+            }
213
+
214
+            $p = strpos($letexte, $regs[0]);
215
+            $letexte = substr_replace($letexte, code_echappement($echap, $source, $no_transform), $p, strlen($regs[0]));
216
+        }
217
+    }
218
+
219
+    if ($no_transform) {
220
+        return $letexte;
221
+    }
222
+
223
+    // Echapper le php pour faire joli (ici, c'est pas pour la securite)
224
+    // seulement si on a echappe les <script>
225
+    // (derogatoire car on ne peut pas faire passer < ? ... ? >
226
+    // dans une callback autonommee
227
+    if (strpos($preg ? $preg : _PROTEGE_BLOCS, 'script') !== false) {
228
+        if (strpos($letexte, "<" . "?") !== false and preg_match_all(',<[?].*($|[?]>),UisS',
229
+                $letexte, $matches, PREG_SET_ORDER)
230
+        ) {
231
+            foreach ($matches as $regs) {
232
+                $letexte = str_replace($regs[0],
233
+                    code_echappement(highlight_string($regs[0], true), $source),
234
+                    $letexte);
235
+            }
236
+        }
237
+    }
238
+
239
+    return $letexte;
240 240
 }
241 241
 
242 242
 //
@@ -245,52 +245,52 @@  discard block
 block discarded – undo
245 245
 // par propre() : exemple dans multi et dans typo()
246 246
 // http://code.spip.net/@echappe_retour
247 247
 function echappe_retour($letexte, $source = '', $filtre = "") {
248
-	if (strpos($letexte, "base64$source")) {
249
-		# spip_log(spip_htmlspecialchars($letexte));  ## pour les curieux
250
-		$max_prof = 5;
251
-		while (strpos($letexte, "<") !== false
252
-			and
253
-			preg_match_all(',<(span|div)\sclass=[\'"]base64' . $source . '[\'"]\s(.*)>\s*</\1>,UmsS',
254
-				$letexte, $regs, PREG_SET_ORDER)
255
-			and $max_prof--) {
256
-			foreach ($regs as $reg) {
257
-				$rempl = base64_decode(extraire_attribut($reg[0], 'title'));
258
-				// recherche d'attributs supplementaires
259
-				$at = array();
260
-				foreach (array('lang', 'dir') as $attr) {
261
-					if ($a = extraire_attribut($reg[0], $attr)) {
262
-						$at[$attr] = $a;
263
-					}
264
-				}
265
-				if ($at) {
266
-					$rempl = '<' . $reg[1] . '>' . $rempl . '</' . $reg[1] . '>';
267
-					foreach ($at as $attr => $a) {
268
-						$rempl = inserer_attribut($rempl, $attr, $a);
269
-					}
270
-				}
271
-				if ($filtre) {
272
-					$rempl = $filtre($rempl);
273
-				}
274
-				$letexte = str_replace($reg[0], $rempl, $letexte);
275
-			}
276
-		}
277
-	}
278
-
279
-	return $letexte;
248
+    if (strpos($letexte, "base64$source")) {
249
+        # spip_log(spip_htmlspecialchars($letexte));  ## pour les curieux
250
+        $max_prof = 5;
251
+        while (strpos($letexte, "<") !== false
252
+            and
253
+            preg_match_all(',<(span|div)\sclass=[\'"]base64' . $source . '[\'"]\s(.*)>\s*</\1>,UmsS',
254
+                $letexte, $regs, PREG_SET_ORDER)
255
+            and $max_prof--) {
256
+            foreach ($regs as $reg) {
257
+                $rempl = base64_decode(extraire_attribut($reg[0], 'title'));
258
+                // recherche d'attributs supplementaires
259
+                $at = array();
260
+                foreach (array('lang', 'dir') as $attr) {
261
+                    if ($a = extraire_attribut($reg[0], $attr)) {
262
+                        $at[$attr] = $a;
263
+                    }
264
+                }
265
+                if ($at) {
266
+                    $rempl = '<' . $reg[1] . '>' . $rempl . '</' . $reg[1] . '>';
267
+                    foreach ($at as $attr => $a) {
268
+                        $rempl = inserer_attribut($rempl, $attr, $a);
269
+                    }
270
+                }
271
+                if ($filtre) {
272
+                    $rempl = $filtre($rempl);
273
+                }
274
+                $letexte = str_replace($reg[0], $rempl, $letexte);
275
+            }
276
+        }
277
+    }
278
+
279
+    return $letexte;
280 280
 }
281 281
 
282 282
 // Reinserer le javascript de confiance (venant des modeles)
283 283
 
284 284
 // http://code.spip.net/@echappe_retour_modeles
285 285
 function echappe_retour_modeles($letexte, $interdire_scripts = false) {
286
-	$letexte = echappe_retour($letexte);
286
+    $letexte = echappe_retour($letexte);
287 287
 
288
-	// Dans les appels directs hors squelette, securiser aussi ici
289
-	if ($interdire_scripts) {
290
-		$letexte = interdire_scripts($letexte);
291
-	}
288
+    // Dans les appels directs hors squelette, securiser aussi ici
289
+    if ($interdire_scripts) {
290
+        $letexte = interdire_scripts($letexte);
291
+    }
292 292
 
293
-	return trim($letexte);
293
+    return trim($letexte);
294 294
 }
295 295
 
296 296
 
@@ -318,130 +318,130 @@  discard block
 block discarded – undo
318 318
  *     Texte coupé
319 319
  **/
320 320
 function couper($texte, $taille = 50, $suite = null) {
321
-	if (!($length = strlen($texte)) or $taille <= 0) {
322
-		return '';
323
-	}
324
-	$offset = 400 + 2 * $taille;
325
-	while ($offset < $length
326
-		and strlen(preg_replace(",<(!--|\w|/)[^>]+>,Uims", "", substr($texte, 0, $offset))) < $taille) {
327
-		$offset = 2 * $offset;
328
-	}
329
-	if ($offset < $length
330
-		&& ($p_tag_ouvrant = strpos($texte, '<', $offset)) !== null
331
-	) {
332
-		$p_tag_fermant = strpos($texte, '>', $offset);
333
-		if ($p_tag_fermant && ($p_tag_fermant < $p_tag_ouvrant)) {
334
-			$offset = $p_tag_fermant + 1;
335
-		} // prolonger la coupe jusqu'au tag fermant suivant eventuel
336
-	}
337
-	$texte = substr($texte, 0, $offset); /* eviter de travailler sur 10ko pour extraire 150 caracteres */
338
-
339
-	if (!function_exists('nettoyer_raccourcis_typo')) {
340
-		include_spip('inc/lien');
341
-	}
342
-	$texte = nettoyer_raccourcis_typo($texte);
343
-
344
-	// balises de sauts de ligne et paragraphe
345
-	$texte = preg_replace("/<p( [^>]*)?" . ">/", "\r", $texte);
346
-	$texte = preg_replace("/<br( [^>]*)?" . ">/", "\n", $texte);
347
-
348
-	// on repasse les doubles \n en \r que nettoyer_raccourcis_typo() a pu modifier
349
-	$texte = str_replace("\n\n", "\r", $texte);
350
-
351
-	// supprimer les tags
352
-	$texte = supprimer_tags($texte);
353
-	$texte = trim(str_replace("\n", " ", $texte));
354
-	$texte .= "\n";  // marquer la fin
355
-
356
-	// corriger la longueur de coupe
357
-	// en fonction de la presence de caracteres utf
358
-	if ($GLOBALS['meta']['charset'] == 'utf-8') {
359
-		$long = charset2unicode($texte);
360
-		$long = spip_substr($long, 0, max($taille, 1));
361
-		$nbcharutf = preg_match_all('/(&#[0-9]{3,6};)/S', $long, $matches);
362
-		$taille += $nbcharutf;
363
-	}
364
-
365
-
366
-	// couper au mot precedent
367
-	$long = spip_substr($texte, 0, max($taille - 4, 1));
368
-	$u = $GLOBALS['meta']['pcre_u'];
369
-	$court = preg_replace("/([^\s][\s]+)[^\s]*\n?$/" . $u, "\\1", $long);
370
-	if (is_null($suite)) {
371
-		$suite = (defined('_COUPER_SUITE') ? _COUPER_SUITE : '&nbsp;(...)');
372
-	}
373
-	$points = $suite;
374
-
375
-	// trop court ? ne pas faire de (...)
376
-	if (spip_strlen($court) < max(0.75 * $taille, 2)) {
377
-		$points = '';
378
-		$long = spip_substr($texte, 0, $taille);
379
-		$texte = preg_replace("/([^\s][\s]+)[^\s]*\n?$/" . $u, "\\1", $long);
380
-		// encore trop court ? couper au caractere
381
-		if (spip_strlen($texte) < 0.75 * $taille) {
382
-			$texte = $long;
383
-		}
384
-	} else {
385
-		$texte = $court;
386
-	}
387
-
388
-	if (strpos($texte, "\n"))  // la fin est encore la : c'est qu'on n'a pas de texte de suite
389
-	{
390
-		$points = '';
391
-	}
392
-
393
-	// remettre les paragraphes
394
-	$texte = preg_replace("/\r+/", "\n\n", $texte);
395
-
396
-	// supprimer l'eventuelle entite finale mal coupee
397
-	$texte = preg_replace('/&#?[a-z0-9]*$/S', '', $texte);
398
-
399
-	return quote_amp(trim($texte)) . $points;
321
+    if (!($length = strlen($texte)) or $taille <= 0) {
322
+        return '';
323
+    }
324
+    $offset = 400 + 2 * $taille;
325
+    while ($offset < $length
326
+        and strlen(preg_replace(",<(!--|\w|/)[^>]+>,Uims", "", substr($texte, 0, $offset))) < $taille) {
327
+        $offset = 2 * $offset;
328
+    }
329
+    if ($offset < $length
330
+        && ($p_tag_ouvrant = strpos($texte, '<', $offset)) !== null
331
+    ) {
332
+        $p_tag_fermant = strpos($texte, '>', $offset);
333
+        if ($p_tag_fermant && ($p_tag_fermant < $p_tag_ouvrant)) {
334
+            $offset = $p_tag_fermant + 1;
335
+        } // prolonger la coupe jusqu'au tag fermant suivant eventuel
336
+    }
337
+    $texte = substr($texte, 0, $offset); /* eviter de travailler sur 10ko pour extraire 150 caracteres */
338
+
339
+    if (!function_exists('nettoyer_raccourcis_typo')) {
340
+        include_spip('inc/lien');
341
+    }
342
+    $texte = nettoyer_raccourcis_typo($texte);
343
+
344
+    // balises de sauts de ligne et paragraphe
345
+    $texte = preg_replace("/<p( [^>]*)?" . ">/", "\r", $texte);
346
+    $texte = preg_replace("/<br( [^>]*)?" . ">/", "\n", $texte);
347
+
348
+    // on repasse les doubles \n en \r que nettoyer_raccourcis_typo() a pu modifier
349
+    $texte = str_replace("\n\n", "\r", $texte);
350
+
351
+    // supprimer les tags
352
+    $texte = supprimer_tags($texte);
353
+    $texte = trim(str_replace("\n", " ", $texte));
354
+    $texte .= "\n";  // marquer la fin
355
+
356
+    // corriger la longueur de coupe
357
+    // en fonction de la presence de caracteres utf
358
+    if ($GLOBALS['meta']['charset'] == 'utf-8') {
359
+        $long = charset2unicode($texte);
360
+        $long = spip_substr($long, 0, max($taille, 1));
361
+        $nbcharutf = preg_match_all('/(&#[0-9]{3,6};)/S', $long, $matches);
362
+        $taille += $nbcharutf;
363
+    }
364
+
365
+
366
+    // couper au mot precedent
367
+    $long = spip_substr($texte, 0, max($taille - 4, 1));
368
+    $u = $GLOBALS['meta']['pcre_u'];
369
+    $court = preg_replace("/([^\s][\s]+)[^\s]*\n?$/" . $u, "\\1", $long);
370
+    if (is_null($suite)) {
371
+        $suite = (defined('_COUPER_SUITE') ? _COUPER_SUITE : '&nbsp;(...)');
372
+    }
373
+    $points = $suite;
374
+
375
+    // trop court ? ne pas faire de (...)
376
+    if (spip_strlen($court) < max(0.75 * $taille, 2)) {
377
+        $points = '';
378
+        $long = spip_substr($texte, 0, $taille);
379
+        $texte = preg_replace("/([^\s][\s]+)[^\s]*\n?$/" . $u, "\\1", $long);
380
+        // encore trop court ? couper au caractere
381
+        if (spip_strlen($texte) < 0.75 * $taille) {
382
+            $texte = $long;
383
+        }
384
+    } else {
385
+        $texte = $court;
386
+    }
387
+
388
+    if (strpos($texte, "\n"))  // la fin est encore la : c'est qu'on n'a pas de texte de suite
389
+    {
390
+        $points = '';
391
+    }
392
+
393
+    // remettre les paragraphes
394
+    $texte = preg_replace("/\r+/", "\n\n", $texte);
395
+
396
+    // supprimer l'eventuelle entite finale mal coupee
397
+    $texte = preg_replace('/&#?[a-z0-9]*$/S', '', $texte);
398
+
399
+    return quote_amp(trim($texte)) . $points;
400 400
 }
401 401
 
402 402
 
403 403
 // http://code.spip.net/@protege_js_modeles
404 404
 function protege_js_modeles($t) {
405
-	if (isset($GLOBALS['visiteur_session'])) {
406
-		if (preg_match_all(',<script.*?($|</script.),isS', $t, $r, PREG_SET_ORDER)) {
407
-			if (!defined('_PROTEGE_JS_MODELES')) {
408
-				include_spip('inc/acces');
409
-				define('_PROTEGE_JS_MODELES', creer_uniqid());
410
-			}
411
-			foreach ($r as $regs) {
412
-				$t = str_replace($regs[0], code_echappement($regs[0], 'javascript' . _PROTEGE_JS_MODELES), $t);
413
-			}
414
-		}
415
-		if (preg_match_all(',<\?php.*?($|\?' . '>),isS', $t, $r, PREG_SET_ORDER)) {
416
-			if (!defined('_PROTEGE_PHP_MODELES')) {
417
-				include_spip('inc/acces');
418
-				define('_PROTEGE_PHP_MODELES', creer_uniqid());
419
-			}
420
-			foreach ($r as $regs) {
421
-				$t = str_replace($regs[0], code_echappement($regs[0], 'php' . _PROTEGE_PHP_MODELES), $t);
422
-			}
423
-		}
424
-	}
425
-
426
-	return $t;
405
+    if (isset($GLOBALS['visiteur_session'])) {
406
+        if (preg_match_all(',<script.*?($|</script.),isS', $t, $r, PREG_SET_ORDER)) {
407
+            if (!defined('_PROTEGE_JS_MODELES')) {
408
+                include_spip('inc/acces');
409
+                define('_PROTEGE_JS_MODELES', creer_uniqid());
410
+            }
411
+            foreach ($r as $regs) {
412
+                $t = str_replace($regs[0], code_echappement($regs[0], 'javascript' . _PROTEGE_JS_MODELES), $t);
413
+            }
414
+        }
415
+        if (preg_match_all(',<\?php.*?($|\?' . '>),isS', $t, $r, PREG_SET_ORDER)) {
416
+            if (!defined('_PROTEGE_PHP_MODELES')) {
417
+                include_spip('inc/acces');
418
+                define('_PROTEGE_PHP_MODELES', creer_uniqid());
419
+            }
420
+            foreach ($r as $regs) {
421
+                $t = str_replace($regs[0], code_echappement($regs[0], 'php' . _PROTEGE_PHP_MODELES), $t);
422
+            }
423
+        }
424
+    }
425
+
426
+    return $t;
427 427
 }
428 428
 
429 429
 
430 430
 function echapper_faux_tags($letexte) {
431
-	if (strpos($letexte, '<') === false) {
432
-		return $letexte;
433
-	}
434
-	$textMatches = preg_split(',(</?[a-z!][^<>]*>),', $letexte, null, PREG_SPLIT_DELIM_CAPTURE);
435
-
436
-	$letexte = "";
437
-	while (count($textMatches)) {
438
-		// un texte a echapper
439
-		$letexte .= str_replace("<", '&lt;', array_shift($textMatches));
440
-		// un tag html qui a servit a faite le split
441
-		$letexte .= array_shift($textMatches);
442
-	}
443
-
444
-	return $letexte;
431
+    if (strpos($letexte, '<') === false) {
432
+        return $letexte;
433
+    }
434
+    $textMatches = preg_split(',(</?[a-z!][^<>]*>),', $letexte, null, PREG_SPLIT_DELIM_CAPTURE);
435
+
436
+    $letexte = "";
437
+    while (count($textMatches)) {
438
+        // un texte a echapper
439
+        $letexte .= str_replace("<", '&lt;', array_shift($textMatches));
440
+        // un tag html qui a servit a faite le split
441
+        $letexte .= array_shift($textMatches);
442
+    }
443
+
444
+    return $letexte;
445 445
 }
446 446
 
447 447
 /**
@@ -454,30 +454,30 @@  discard block
 block discarded – undo
454 454
  * @return string
455 455
  */
456 456
 function echapper_html_suspect($texte, $strict=true) {
457
-	if (!$texte
458
-		or strpos($texte, '<') === false or strpos($texte, '=') === false) {
459
-		return $texte;
460
-	}
461
-	// quand c'est du texte qui passe par propre on est plus coulant tant qu'il y a pas d'attribut du type onxxx=
462
-	// car sinon on declenche sur les modeles ou ressources
463
-	if (!$strict and
464
-	  (strpos($texte,'on') === false or !preg_match(",<\w+.*\bon\w+\s*=,UimsS", $texte))
465
-	  ){
466
-		return $texte;
467
-	}
468
-
469
-	// on teste sur strlen car safehtml supprime le contenu dangereux
470
-	// mais il peut aussi changer des ' en " sur les attributs html,
471
-	// donc un test d'egalite est trop strict
472
-	if (strlen(safehtml($texte)) !== strlen($texte)) {
473
-		$texte = str_replace("<", "&lt;", $texte);
474
-		if (!function_exists('attribut_html')) {
475
-			include_spip('inc/filtres');
476
-		}
477
-		$texte = "<mark class='danger-js' title='".attribut_html(_T('erreur_contenu_suspect'))."'>⚠️</mark> ".$texte;
478
-	}
479
-
480
-	return $texte;
457
+    if (!$texte
458
+        or strpos($texte, '<') === false or strpos($texte, '=') === false) {
459
+        return $texte;
460
+    }
461
+    // quand c'est du texte qui passe par propre on est plus coulant tant qu'il y a pas d'attribut du type onxxx=
462
+    // car sinon on declenche sur les modeles ou ressources
463
+    if (!$strict and
464
+      (strpos($texte,'on') === false or !preg_match(",<\w+.*\bon\w+\s*=,UimsS", $texte))
465
+        ){
466
+        return $texte;
467
+    }
468
+
469
+    // on teste sur strlen car safehtml supprime le contenu dangereux
470
+    // mais il peut aussi changer des ' en " sur les attributs html,
471
+    // donc un test d'egalite est trop strict
472
+    if (strlen(safehtml($texte)) !== strlen($texte)) {
473
+        $texte = str_replace("<", "&lt;", $texte);
474
+        if (!function_exists('attribut_html')) {
475
+            include_spip('inc/filtres');
476
+        }
477
+        $texte = "<mark class='danger-js' title='".attribut_html(_T('erreur_contenu_suspect'))."'>⚠️</mark> ".$texte;
478
+    }
479
+
480
+    return $texte;
481 481
 }
482 482
 
483 483
 
@@ -498,27 +498,27 @@  discard block
 block discarded – undo
498 498
  *      Texte sécurisé
499 499
  **/
500 500
 function safehtml($t) {
501
-	static $safehtml;
502
-
503
-	if (!$t or !is_string($t)) {
504
-		return $t;
505
-	}
506
-	# attention safehtml nettoie deux ou trois caracteres de plus. A voir
507
-	if (strpos($t, '<') === false) {
508
-		return str_replace("\x00", '', $t);
509
-	}
510
-
511
-	$t = interdire_scripts($t); // jolifier le php
512
-	$t = echappe_js($t);
513
-
514
-	if (!isset($safehtml)) {
515
-		$safehtml = charger_fonction('safehtml', 'inc', true);
516
-	}
517
-	if ($safehtml) {
518
-		$t = $safehtml($t);
519
-	}
520
-
521
-	return interdire_scripts($t); // interdire le php (2 precautions)
501
+    static $safehtml;
502
+
503
+    if (!$t or !is_string($t)) {
504
+        return $t;
505
+    }
506
+    # attention safehtml nettoie deux ou trois caracteres de plus. A voir
507
+    if (strpos($t, '<') === false) {
508
+        return str_replace("\x00", '', $t);
509
+    }
510
+
511
+    $t = interdire_scripts($t); // jolifier le php
512
+    $t = echappe_js($t);
513
+
514
+    if (!isset($safehtml)) {
515
+        $safehtml = charger_fonction('safehtml', 'inc', true);
516
+    }
517
+    if ($safehtml) {
518
+        $t = $safehtml($t);
519
+    }
520
+
521
+    return interdire_scripts($t); // interdire le php (2 precautions)
522 522
 }
523 523
 
524 524
 
@@ -540,10 +540,10 @@  discard block
 block discarded – undo
540 540
  *     Texte sans les modèles d'image
541 541
  **/
542 542
 function supprime_img($letexte, $message = null) {
543
-	if ($message === null) {
544
-		$message = '(' . _T('img_indisponible') . ')';
545
-	}
543
+    if ($message === null) {
544
+        $message = '(' . _T('img_indisponible') . ')';
545
+    }
546 546
 
547
-	return preg_replace(',<(img|doc|emb)([0-9]+)(\|([^>]*))?' . '\s*/?' . '>,i',
548
-		$message, $letexte);
547
+    return preg_replace(',<(img|doc|emb)([0-9]+)(\|([^>]*))?' . '\s*/?' . '>,i',
548
+        $message, $letexte);
549 549
 }
Please login to merge, or discard this patch.
Spacing   +25 added lines, -25 removed lines patch added patch discarded remove patch
@@ -44,7 +44,7 @@  discard block
 block discarded – undo
44 44
 	// celle du texte) et public (spip_lang est la langue du texte)
45 45
 	$dir = _DIR_RESTREINT ? lang_dir() : lang_dir($GLOBALS['spip_lang']);
46 46
 
47
-	$p = 'puce' . (test_espace_prive() ? '_prive' : '');
47
+	$p = 'puce'.(test_espace_prive() ? '_prive' : '');
48 48
 	if ($dir == 'rtl') {
49 49
 		$p .= '_rtl';
50 50
 	}
@@ -67,7 +67,7 @@  discard block
 block discarded – undo
67 67
 }
68 68
 
69 69
 if (!defined('_BALISES_BLOCS_REGEXP')) {
70
-	define('_BALISES_BLOCS_REGEXP', ',</?(' . _BALISES_BLOCS . ')[>[:space:]],iS');
70
+	define('_BALISES_BLOCS_REGEXP', ',</?('._BALISES_BLOCS.')[>[:space:]],iS');
71 71
 }
72 72
 
73 73
 //
@@ -85,7 +85,7 @@  discard block
 block discarded – undo
85 85
 
86 86
 	// Tester si on echappe en span ou en div
87 87
 	if (is_null($mode) or !in_array($mode, array('div', 'span'))) {
88
-		$mode = preg_match(',</?(' . _BALISES_BLOCS . ')[>[:space:]],iS', $rempl) ? 'div' : 'span';
88
+		$mode = preg_match(',</?('._BALISES_BLOCS.')[>[:space:]],iS', $rempl) ? 'div' : 'span';
89 89
 	}
90 90
 
91 91
 	// Decouper en morceaux, base64 a des probleme selon la taille de la pile
@@ -112,7 +112,7 @@  discard block
 block discarded – undo
112 112
 // Echapper les <code>...</ code>
113 113
 // http://code.spip.net/@traiter_echap_code_dist
114 114
 function traiter_echap_code_dist($regs) {
115
-	list(, , $att, $corps) = $regs;
115
+	list(,, $att, $corps) = $regs;
116 116
 	$echap = spip_htmlspecialchars($corps); // il ne faut pas passer dans entites_html, ne pas transformer les &#xxx; du code !
117 117
 
118 118
 	// ne pas mettre le <div...> s'il n'y a qu'une ligne
@@ -123,9 +123,9 @@  discard block
 block discarded – undo
123 123
 		$echap = nl2br($echap);
124 124
 		$echap = "<div style='text-align: left;' "
125 125
 			. "class='spip_code' dir='ltr'><code$att>"
126
-			. $echap . "</code></div>";
126
+			. $echap."</code></div>";
127 127
 	} else {
128
-		$echap = "<code$att class='spip_code' dir='ltr'>" . $echap . "</code>";
128
+		$echap = "<code$att class='spip_code' dir='ltr'>".$echap."</code>";
129 129
 	}
130 130
 
131 131
 	$echap = str_replace("\t", "&nbsp; &nbsp; &nbsp; &nbsp; ", $echap);
@@ -202,10 +202,10 @@  discard block
 block discarded – undo
202 202
 				$echap = $regs[0];
203 203
 			} // sinon les traiter selon le cas
204 204
 			else {
205
-				if (function_exists($f = 'traiter_echap_' . strtolower($regs[1]))) {
205
+				if (function_exists($f = 'traiter_echap_'.strtolower($regs[1]))) {
206 206
 					$echap = $f($regs);
207 207
 				} else {
208
-					if (function_exists($f = $f . '_dist')) {
208
+					if (function_exists($f = $f.'_dist')) {
209 209
 						$echap = $f($regs);
210 210
 					}
211 211
 				}
@@ -225,7 +225,7 @@  discard block
 block discarded – undo
225 225
 	// (derogatoire car on ne peut pas faire passer < ? ... ? >
226 226
 	// dans une callback autonommee
227 227
 	if (strpos($preg ? $preg : _PROTEGE_BLOCS, 'script') !== false) {
228
-		if (strpos($letexte, "<" . "?") !== false and preg_match_all(',<[?].*($|[?]>),UisS',
228
+		if (strpos($letexte, "<"."?") !== false and preg_match_all(',<[?].*($|[?]>),UisS',
229 229
 				$letexte, $matches, PREG_SET_ORDER)
230 230
 		) {
231 231
 			foreach ($matches as $regs) {
@@ -250,7 +250,7 @@  discard block
 block discarded – undo
250 250
 		$max_prof = 5;
251 251
 		while (strpos($letexte, "<") !== false
252 252
 			and
253
-			preg_match_all(',<(span|div)\sclass=[\'"]base64' . $source . '[\'"]\s(.*)>\s*</\1>,UmsS',
253
+			preg_match_all(',<(span|div)\sclass=[\'"]base64'.$source.'[\'"]\s(.*)>\s*</\1>,UmsS',
254 254
 				$letexte, $regs, PREG_SET_ORDER)
255 255
 			and $max_prof--) {
256 256
 			foreach ($regs as $reg) {
@@ -263,7 +263,7 @@  discard block
 block discarded – undo
263 263
 					}
264 264
 				}
265 265
 				if ($at) {
266
-					$rempl = '<' . $reg[1] . '>' . $rempl . '</' . $reg[1] . '>';
266
+					$rempl = '<'.$reg[1].'>'.$rempl.'</'.$reg[1].'>';
267 267
 					foreach ($at as $attr => $a) {
268 268
 						$rempl = inserer_attribut($rempl, $attr, $a);
269 269
 					}
@@ -342,8 +342,8 @@  discard block
 block discarded – undo
342 342
 	$texte = nettoyer_raccourcis_typo($texte);
343 343
 
344 344
 	// balises de sauts de ligne et paragraphe
345
-	$texte = preg_replace("/<p( [^>]*)?" . ">/", "\r", $texte);
346
-	$texte = preg_replace("/<br( [^>]*)?" . ">/", "\n", $texte);
345
+	$texte = preg_replace("/<p( [^>]*)?".">/", "\r", $texte);
346
+	$texte = preg_replace("/<br( [^>]*)?".">/", "\n", $texte);
347 347
 
348 348
 	// on repasse les doubles \n en \r que nettoyer_raccourcis_typo() a pu modifier
349 349
 	$texte = str_replace("\n\n", "\r", $texte);
@@ -351,7 +351,7 @@  discard block
 block discarded – undo
351 351
 	// supprimer les tags
352 352
 	$texte = supprimer_tags($texte);
353 353
 	$texte = trim(str_replace("\n", " ", $texte));
354
-	$texte .= "\n";  // marquer la fin
354
+	$texte .= "\n"; // marquer la fin
355 355
 
356 356
 	// corriger la longueur de coupe
357 357
 	// en fonction de la presence de caracteres utf
@@ -366,7 +366,7 @@  discard block
 block discarded – undo
366 366
 	// couper au mot precedent
367 367
 	$long = spip_substr($texte, 0, max($taille - 4, 1));
368 368
 	$u = $GLOBALS['meta']['pcre_u'];
369
-	$court = preg_replace("/([^\s][\s]+)[^\s]*\n?$/" . $u, "\\1", $long);
369
+	$court = preg_replace("/([^\s][\s]+)[^\s]*\n?$/".$u, "\\1", $long);
370 370
 	if (is_null($suite)) {
371 371
 		$suite = (defined('_COUPER_SUITE') ? _COUPER_SUITE : '&nbsp;(...)');
372 372
 	}
@@ -376,7 +376,7 @@  discard block
 block discarded – undo
376 376
 	if (spip_strlen($court) < max(0.75 * $taille, 2)) {
377 377
 		$points = '';
378 378
 		$long = spip_substr($texte, 0, $taille);
379
-		$texte = preg_replace("/([^\s][\s]+)[^\s]*\n?$/" . $u, "\\1", $long);
379
+		$texte = preg_replace("/([^\s][\s]+)[^\s]*\n?$/".$u, "\\1", $long);
380 380
 		// encore trop court ? couper au caractere
381 381
 		if (spip_strlen($texte) < 0.75 * $taille) {
382 382
 			$texte = $long;
@@ -396,7 +396,7 @@  discard block
 block discarded – undo
396 396
 	// supprimer l'eventuelle entite finale mal coupee
397 397
 	$texte = preg_replace('/&#?[a-z0-9]*$/S', '', $texte);
398 398
 
399
-	return quote_amp(trim($texte)) . $points;
399
+	return quote_amp(trim($texte)).$points;
400 400
 }
401 401
 
402 402
 
@@ -409,16 +409,16 @@  discard block
 block discarded – undo
409 409
 				define('_PROTEGE_JS_MODELES', creer_uniqid());
410 410
 			}
411 411
 			foreach ($r as $regs) {
412
-				$t = str_replace($regs[0], code_echappement($regs[0], 'javascript' . _PROTEGE_JS_MODELES), $t);
412
+				$t = str_replace($regs[0], code_echappement($regs[0], 'javascript'._PROTEGE_JS_MODELES), $t);
413 413
 			}
414 414
 		}
415
-		if (preg_match_all(',<\?php.*?($|\?' . '>),isS', $t, $r, PREG_SET_ORDER)) {
415
+		if (preg_match_all(',<\?php.*?($|\?'.'>),isS', $t, $r, PREG_SET_ORDER)) {
416 416
 			if (!defined('_PROTEGE_PHP_MODELES')) {
417 417
 				include_spip('inc/acces');
418 418
 				define('_PROTEGE_PHP_MODELES', creer_uniqid());
419 419
 			}
420 420
 			foreach ($r as $regs) {
421
-				$t = str_replace($regs[0], code_echappement($regs[0], 'php' . _PROTEGE_PHP_MODELES), $t);
421
+				$t = str_replace($regs[0], code_echappement($regs[0], 'php'._PROTEGE_PHP_MODELES), $t);
422 422
 			}
423 423
 		}
424 424
 	}
@@ -453,7 +453,7 @@  discard block
 block discarded – undo
453 453
  * @param bool $strict
454 454
  * @return string
455 455
  */
456
-function echapper_html_suspect($texte, $strict=true) {
456
+function echapper_html_suspect($texte, $strict = true) {
457 457
 	if (!$texte
458 458
 		or strpos($texte, '<') === false or strpos($texte, '=') === false) {
459 459
 		return $texte;
@@ -461,8 +461,8 @@  discard block
 block discarded – undo
461 461
 	// quand c'est du texte qui passe par propre on est plus coulant tant qu'il y a pas d'attribut du type onxxx=
462 462
 	// car sinon on declenche sur les modeles ou ressources
463 463
 	if (!$strict and
464
-	  (strpos($texte,'on') === false or !preg_match(",<\w+.*\bon\w+\s*=,UimsS", $texte))
465
-	  ){
464
+	  (strpos($texte, 'on') === false or !preg_match(",<\w+.*\bon\w+\s*=,UimsS", $texte))
465
+	  ) {
466 466
 		return $texte;
467 467
 	}
468 468
 
@@ -541,9 +541,9 @@  discard block
 block discarded – undo
541 541
  **/
542 542
 function supprime_img($letexte, $message = null) {
543 543
 	if ($message === null) {
544
-		$message = '(' . _T('img_indisponible') . ')';
544
+		$message = '('._T('img_indisponible').')';
545 545
 	}
546 546
 
547
-	return preg_replace(',<(img|doc|emb)([0-9]+)(\|([^>]*))?' . '\s*/?' . '>,i',
547
+	return preg_replace(',<(img|doc|emb)([0-9]+)(\|([^>]*))?'.'\s*/?'.'>,i',
548 548
 		$message, $letexte);
549 549
 }
Please login to merge, or discard this patch.