Completed
Push — master ( e11b28...f86fd0 )
by cam
04:19
created
ecrire/inc/charsets.php 1 patch
Indentation   +704 added lines, -704 removed lines patch added patch discarded remove patch
@@ -22,7 +22,7 @@  discard block
 block discarded – undo
22 22
 
23 23
 // securité
24 24
 if (!defined('_ECRIRE_INC_VERSION')) {
25
-	return;
25
+    return;
26 26
 }
27 27
 
28 28
 // se faciliter la lecture du charset
@@ -42,45 +42,45 @@  discard block
 block discarded – undo
42 42
  *     - false si le charset n'est pas décrit dans le répertoire charsets/
43 43
  **/
44 44
 function load_charset($charset = 'AUTO') {
45
-	if ($charset == 'AUTO') {
46
-		$charset = $GLOBALS['meta']['charset'];
47
-	}
48
-	$charset = trim(strtolower($charset));
49
-	if (isset($GLOBALS['CHARSET'][$charset])) {
50
-		return $charset;
51
-	}
52
-
53
-	if ($charset == 'utf-8') {
54
-		$GLOBALS['CHARSET'][$charset] = array();
55
-
56
-		return $charset;
57
-	}
58
-
59
-	// Quelques synonymes
60
-	if ($charset == '') {
61
-		$charset = 'iso-8859-1';
62
-	} else {
63
-		if ($charset == 'windows-1250') {
64
-			$charset = 'cp1250';
65
-		} else {
66
-			if ($charset == 'windows-1251') {
67
-				$charset = 'cp1251';
68
-			} else {
69
-				if ($charset == 'windows-1256') {
70
-					$charset = 'cp1256';
71
-				}
72
-			}
73
-		}
74
-	}
75
-
76
-	if (find_in_path($charset . '.php', 'charsets/', true)) {
77
-		return $charset;
78
-	} else {
79
-		spip_log("Erreur: pas de fichier de conversion 'charsets/$charset'");
80
-		$GLOBALS['CHARSET'][$charset] = array();
81
-
82
-		return false;
83
-	}
45
+    if ($charset == 'AUTO') {
46
+        $charset = $GLOBALS['meta']['charset'];
47
+    }
48
+    $charset = trim(strtolower($charset));
49
+    if (isset($GLOBALS['CHARSET'][$charset])) {
50
+        return $charset;
51
+    }
52
+
53
+    if ($charset == 'utf-8') {
54
+        $GLOBALS['CHARSET'][$charset] = array();
55
+
56
+        return $charset;
57
+    }
58
+
59
+    // Quelques synonymes
60
+    if ($charset == '') {
61
+        $charset = 'iso-8859-1';
62
+    } else {
63
+        if ($charset == 'windows-1250') {
64
+            $charset = 'cp1250';
65
+        } else {
66
+            if ($charset == 'windows-1251') {
67
+                $charset = 'cp1251';
68
+            } else {
69
+                if ($charset == 'windows-1256') {
70
+                    $charset = 'cp1256';
71
+                }
72
+            }
73
+        }
74
+    }
75
+
76
+    if (find_in_path($charset . '.php', 'charsets/', true)) {
77
+        return $charset;
78
+    } else {
79
+        spip_log("Erreur: pas de fichier de conversion 'charsets/$charset'");
80
+        $GLOBALS['CHARSET'][$charset] = array();
81
+
82
+        return false;
83
+    }
84 84
 }
85 85
 
86 86
 
@@ -91,30 +91,30 @@  discard block
 block discarded – undo
91 91
  *     true si toutes les fonctions mb nécessaires sont présentes
92 92
  **/
93 93
 function init_mb_string() {
94
-	static $mb;
95
-
96
-	// verifier que tout est present (fonctions mb_string pour php >= 4.0.6)
97
-	// et que le charset interne est connu de mb_string
98
-	if (!$mb) {
99
-		if (function_exists('mb_internal_encoding')
100
-			and function_exists('mb_detect_order')
101
-			and function_exists('mb_substr')
102
-			and function_exists('mb_strlen')
103
-			and function_exists('mb_strtolower')
104
-			and function_exists('mb_strtoupper')
105
-			and function_exists('mb_encode_mimeheader')
106
-			and function_exists('mb_encode_numericentity')
107
-			and function_exists('mb_decode_numericentity')
108
-			and mb_detect_order(lire_config('charset', _DEFAULT_CHARSET))
109
-		) {
110
-			mb_internal_encoding('utf-8');
111
-			$mb = 1;
112
-		} else {
113
-			$mb = -1;
114
-		}
115
-	}
116
-
117
-	return ($mb == 1);
94
+    static $mb;
95
+
96
+    // verifier que tout est present (fonctions mb_string pour php >= 4.0.6)
97
+    // et que le charset interne est connu de mb_string
98
+    if (!$mb) {
99
+        if (function_exists('mb_internal_encoding')
100
+            and function_exists('mb_detect_order')
101
+            and function_exists('mb_substr')
102
+            and function_exists('mb_strlen')
103
+            and function_exists('mb_strtolower')
104
+            and function_exists('mb_strtoupper')
105
+            and function_exists('mb_encode_mimeheader')
106
+            and function_exists('mb_encode_numericentity')
107
+            and function_exists('mb_decode_numericentity')
108
+            and mb_detect_order(lire_config('charset', _DEFAULT_CHARSET))
109
+        ) {
110
+            mb_internal_encoding('utf-8');
111
+            $mb = 1;
112
+        } else {
113
+            $mb = -1;
114
+        }
115
+    }
116
+
117
+    return ($mb == 1);
118 118
 }
119 119
 
120 120
 /**
@@ -129,21 +129,21 @@  discard block
 block discarded – undo
129 129
  *     true si iconv fonctionne correctement
130 130
  **/
131 131
 function test_iconv() {
132
-	static $iconv_ok;
133
-
134
-	if (!$iconv_ok) {
135
-		if (!function_exists('iconv')) {
136
-			$iconv_ok = -1;
137
-		} else {
138
-			if (utf_32_to_unicode(@iconv('utf-8', 'utf-32', 'chaine de test')) == 'chaine de test') {
139
-				$iconv_ok = 1;
140
-			} else {
141
-				$iconv_ok = -1;
142
-			}
143
-		}
144
-	}
145
-
146
-	return ($iconv_ok == 1);
132
+    static $iconv_ok;
133
+
134
+    if (!$iconv_ok) {
135
+        if (!function_exists('iconv')) {
136
+            $iconv_ok = -1;
137
+        } else {
138
+            if (utf_32_to_unicode(@iconv('utf-8', 'utf-32', 'chaine de test')) == 'chaine de test') {
139
+                $iconv_ok = 1;
140
+            } else {
141
+                $iconv_ok = -1;
142
+            }
143
+        }
144
+    }
145
+
146
+    return ($iconv_ok == 1);
147 147
 }
148 148
 
149 149
 
@@ -156,18 +156,18 @@  discard block
 block discarded – undo
156 156
  *     true si PCRE supporte l'UTF-8 correctement
157 157
  **/
158 158
 function test_pcre_unicode() {
159
-	static $pcre_ok = 0;
160
-
161
-	if (!$pcre_ok) {
162
-		$s = " " . chr(195) . chr(169) . "t" . chr(195) . chr(169) . " ";
163
-		if (preg_match(',\W...\W,u', $s)) {
164
-			$pcre_ok = 1;
165
-		} else {
166
-			$pcre_ok = -1;
167
-		}
168
-	}
169
-
170
-	return $pcre_ok == 1;
159
+    static $pcre_ok = 0;
160
+
161
+    if (!$pcre_ok) {
162
+        $s = " " . chr(195) . chr(169) . "t" . chr(195) . chr(169) . " ";
163
+        if (preg_match(',\W...\W,u', $s)) {
164
+            $pcre_ok = 1;
165
+        } else {
166
+            $pcre_ok = -1;
167
+        }
168
+    }
169
+
170
+    return $pcre_ok == 1;
171 171
 }
172 172
 
173 173
 /**
@@ -183,22 +183,22 @@  discard block
 block discarded – undo
183 183
  *    Plage de caractères
184 184
  **/
185 185
 function pcre_lettres_unicode() {
186
-	static $plage_unicode;
187
-
188
-	if (!$plage_unicode) {
189
-		if (test_pcre_unicode()) {
190
-			// cf. http://www.unicode.org/charts/
191
-			$plage_unicode = '\w' // iso-latin
192
-				. '\x{100}-\x{24f}' // europeen etendu
193
-				. '\x{300}-\x{1cff}' // des tas de trucs
194
-			;
195
-		} else {
196
-			// fallback a trois sous
197
-			$plage_unicode = '\w';
198
-		}
199
-	}
200
-
201
-	return $plage_unicode;
186
+    static $plage_unicode;
187
+
188
+    if (!$plage_unicode) {
189
+        if (test_pcre_unicode()) {
190
+            // cf. http://www.unicode.org/charts/
191
+            $plage_unicode = '\w' // iso-latin
192
+                . '\x{100}-\x{24f}' // europeen etendu
193
+                . '\x{300}-\x{1cff}' // des tas de trucs
194
+            ;
195
+        } else {
196
+            // fallback a trois sous
197
+            $plage_unicode = '\w';
198
+        }
199
+    }
200
+
201
+    return $plage_unicode;
202 202
 }
203 203
 
204 204
 
@@ -216,7 +216,7 @@  discard block
 block discarded – undo
216 216
  *    Plage de caractères
217 217
  **/
218 218
 function plage_punct_unicode() {
219
-	return '\xE2(\x80[\x80-\xBF]|\x81[\x80-\xAF])';
219
+    return '\xE2(\x80[\x80-\xBF]|\x81[\x80-\xAF])';
220 220
 }
221 221
 
222 222
 /**
@@ -236,72 +236,72 @@  discard block
 block discarded – undo
236 236
  *     Texte corrigé
237 237
  **/
238 238
 function corriger_caracteres_windows($texte, $charset = 'AUTO', $charset_cible = 'unicode') {
239
-	static $trans;
240
-
241
-	if (is_array($texte)) {
242
-		return array_map('corriger_caracteres_windows', $texte);
243
-	}
244
-
245
-	if ($charset == 'AUTO') {
246
-		$charset = lire_config('charset', _DEFAULT_CHARSET);
247
-	}
248
-	if ($charset == 'utf-8') {
249
-		$p = chr(194);
250
-		if (strpos($texte, $p) == false) {
251
-			return $texte;
252
-		}
253
-	} else {
254
-		if ($charset == 'iso-8859-1') {
255
-			$p = '';
256
-		} else {
257
-			return $texte;
258
-		}
259
-	}
260
-
261
-	if (!isset($trans[$charset][$charset_cible])) {
262
-		$trans[$charset][$charset_cible] = array(
263
-			$p . chr(128) => "€",
264
-			$p . chr(129) => ' ', # pas affecte
265
-			$p . chr(130) => "‚",
266
-			$p . chr(131) => "ƒ",
267
-			$p . chr(132) => "„",
268
-			$p . chr(133) => "…",
269
-			$p . chr(134) => "†",
270
-			$p . chr(135) => "‡",
271
-			$p . chr(136) => "ˆ",
272
-			$p . chr(137) => "‰",
273
-			$p . chr(138) => "Š",
274
-			$p . chr(139) => "‹",
275
-			$p . chr(140) => "Œ",
276
-			$p . chr(141) => ' ', # pas affecte
277
-			$p . chr(142) => "Ž",
278
-			$p . chr(143) => ' ', # pas affecte
279
-			$p . chr(144) => ' ', # pas affecte
280
-			$p . chr(145) => "‘",
281
-			$p . chr(146) => "’",
282
-			$p . chr(147) => "“",
283
-			$p . chr(148) => "”",
284
-			$p . chr(149) => "•",
285
-			$p . chr(150) => "–",
286
-			$p . chr(151) => "—",
287
-			$p . chr(152) => "˜",
288
-			$p . chr(153) => "™",
289
-			$p . chr(154) => "š",
290
-			$p . chr(155) => "›",
291
-			$p . chr(156) => "œ",
292
-			$p . chr(157) => ' ', # pas affecte
293
-			$p . chr(158) => "ž",
294
-			$p . chr(159) => "Ÿ",
295
-		);
296
-		if ($charset_cible != 'unicode') {
297
-			foreach ($trans[$charset][$charset_cible] as $k => $c) {
298
-				$trans[$charset][$charset_cible][$k] = unicode2charset($c, $charset_cible);
299
-			}
300
-		}
301
-	}
302
-
303
-	return @str_replace(array_keys($trans[$charset][$charset_cible]),
304
-		array_values($trans[$charset][$charset_cible]), $texte);
239
+    static $trans;
240
+
241
+    if (is_array($texte)) {
242
+        return array_map('corriger_caracteres_windows', $texte);
243
+    }
244
+
245
+    if ($charset == 'AUTO') {
246
+        $charset = lire_config('charset', _DEFAULT_CHARSET);
247
+    }
248
+    if ($charset == 'utf-8') {
249
+        $p = chr(194);
250
+        if (strpos($texte, $p) == false) {
251
+            return $texte;
252
+        }
253
+    } else {
254
+        if ($charset == 'iso-8859-1') {
255
+            $p = '';
256
+        } else {
257
+            return $texte;
258
+        }
259
+    }
260
+
261
+    if (!isset($trans[$charset][$charset_cible])) {
262
+        $trans[$charset][$charset_cible] = array(
263
+            $p . chr(128) => "€",
264
+            $p . chr(129) => ' ', # pas affecte
265
+            $p . chr(130) => "‚",
266
+            $p . chr(131) => "ƒ",
267
+            $p . chr(132) => "„",
268
+            $p . chr(133) => "…",
269
+            $p . chr(134) => "†",
270
+            $p . chr(135) => "‡",
271
+            $p . chr(136) => "ˆ",
272
+            $p . chr(137) => "‰",
273
+            $p . chr(138) => "Š",
274
+            $p . chr(139) => "‹",
275
+            $p . chr(140) => "Œ",
276
+            $p . chr(141) => ' ', # pas affecte
277
+            $p . chr(142) => "Ž",
278
+            $p . chr(143) => ' ', # pas affecte
279
+            $p . chr(144) => ' ', # pas affecte
280
+            $p . chr(145) => "‘",
281
+            $p . chr(146) => "’",
282
+            $p . chr(147) => "“",
283
+            $p . chr(148) => "”",
284
+            $p . chr(149) => "•",
285
+            $p . chr(150) => "–",
286
+            $p . chr(151) => "—",
287
+            $p . chr(152) => "˜",
288
+            $p . chr(153) => "™",
289
+            $p . chr(154) => "š",
290
+            $p . chr(155) => "›",
291
+            $p . chr(156) => "œ",
292
+            $p . chr(157) => ' ', # pas affecte
293
+            $p . chr(158) => "ž",
294
+            $p . chr(159) => "Ÿ",
295
+        );
296
+        if ($charset_cible != 'unicode') {
297
+            foreach ($trans[$charset][$charset_cible] as $k => $c) {
298
+                $trans[$charset][$charset_cible][$k] = unicode2charset($c, $charset_cible);
299
+            }
300
+        }
301
+    }
302
+
303
+    return @str_replace(array_keys($trans[$charset][$charset_cible]),
304
+        array_values($trans[$charset][$charset_cible]), $texte);
305 305
 }
306 306
 
307 307
 
@@ -318,24 +318,24 @@  discard block
 block discarded – undo
318 318
  *     Texte converti
319 319
  **/
320 320
 function html2unicode($texte, $secure = false) {
321
-	if (strpos($texte, '&') === false) {
322
-		return $texte;
323
-	}
324
-	static $trans = array();
325
-	if (!$trans) {
326
-		load_charset('html');
327
-		foreach ($GLOBALS['CHARSET']['html'] as $key => $val) {
328
-			$trans["&$key;"] = $val;
329
-		}
330
-	}
331
-
332
-	if ($secure) {
333
-		return str_replace(array_keys($trans), array_values($trans), $texte);
334
-	} else {
335
-		return str_replace(array('&amp;', '&quot;', '&lt;', '&gt;'), array('&', '"', '<', '>'),
336
-			str_replace(array_keys($trans), array_values($trans), $texte)
337
-		);
338
-	}
321
+    if (strpos($texte, '&') === false) {
322
+        return $texte;
323
+    }
324
+    static $trans = array();
325
+    if (!$trans) {
326
+        load_charset('html');
327
+        foreach ($GLOBALS['CHARSET']['html'] as $key => $val) {
328
+            $trans["&$key;"] = $val;
329
+        }
330
+    }
331
+
332
+    if ($secure) {
333
+        return str_replace(array_keys($trans), array_values($trans), $texte);
334
+    } else {
335
+        return str_replace(array('&amp;', '&quot;', '&lt;', '&gt;'), array('&', '"', '<', '>'),
336
+            str_replace(array_keys($trans), array_values($trans), $texte)
337
+        );
338
+    }
339 339
 }
340 340
 
341 341
 
@@ -350,16 +350,16 @@  discard block
 block discarded – undo
350 350
  *     Texte converti
351 351
  **/
352 352
 function mathml2unicode($texte) {
353
-	static $trans;
354
-	if (!$trans) {
355
-		load_charset('mathml');
353
+    static $trans;
354
+    if (!$trans) {
355
+        load_charset('mathml');
356 356
 
357
-		foreach ($GLOBALS['CHARSET']['mathml'] as $key => $val) {
358
-			$trans["&$key;"] = $val;
359
-		}
360
-	}
357
+        foreach ($GLOBALS['CHARSET']['mathml'] as $key => $val) {
358
+            $trans["&$key;"] = $val;
359
+        }
360
+    }
361 361
 
362
-	return str_replace(array_keys($trans), array_values($trans), $texte);
362
+    return str_replace(array_keys($trans), array_values($trans), $texte);
363 363
 }
364 364
 
365 365
 
@@ -381,76 +381,76 @@  discard block
 block discarded – undo
381 381
  *     Texte converti en unicode
382 382
  **/
383 383
 function charset2unicode($texte, $charset = 'AUTO' /* $forcer: obsolete*/) {
384
-	static $trans;
385
-
386
-	if ($charset == 'AUTO') {
387
-		$charset = lire_config('charset', _DEFAULT_CHARSET);
388
-	}
389
-
390
-	if ($charset == '') {
391
-		$charset = 'iso-8859-1';
392
-	}
393
-	$charset = strtolower($charset);
394
-
395
-	switch ($charset) {
396
-		case 'utf-8':
397
-		case 'utf8':
398
-			return utf_8_to_unicode($texte);
399
-
400
-		case 'iso-8859-1':
401
-			$texte = corriger_caracteres_windows($texte, 'iso-8859-1');
402
-		// pas de break; ici, on suit sur default:
403
-
404
-		default:
405
-			// mbstring presente ?
406
-			if (init_mb_string()) {
407
-				$order = mb_detect_order();
408
-				try {
409
-					# mb_string connait-il $charset?
410
-					if ($order and mb_detect_order($charset)) {
411
-						$s = mb_convert_encoding($texte, 'utf-8', $charset);
412
-						if ($s && $s != $texte) {
413
-							return utf_8_to_unicode($s);
414
-						}
415
-					}
384
+    static $trans;
385
+
386
+    if ($charset == 'AUTO') {
387
+        $charset = lire_config('charset', _DEFAULT_CHARSET);
388
+    }
389
+
390
+    if ($charset == '') {
391
+        $charset = 'iso-8859-1';
392
+    }
393
+    $charset = strtolower($charset);
394
+
395
+    switch ($charset) {
396
+        case 'utf-8':
397
+        case 'utf8':
398
+            return utf_8_to_unicode($texte);
399
+
400
+        case 'iso-8859-1':
401
+            $texte = corriger_caracteres_windows($texte, 'iso-8859-1');
402
+        // pas de break; ici, on suit sur default:
403
+
404
+        default:
405
+            // mbstring presente ?
406
+            if (init_mb_string()) {
407
+                $order = mb_detect_order();
408
+                try {
409
+                    # mb_string connait-il $charset?
410
+                    if ($order and mb_detect_order($charset)) {
411
+                        $s = mb_convert_encoding($texte, 'utf-8', $charset);
412
+                        if ($s && $s != $texte) {
413
+                            return utf_8_to_unicode($s);
414
+                        }
415
+                    }
416 416
 					
417
-				} catch (\Error $e) {
418
-					// Le charset n'existe probablement pas
419
-				} finally {
420
-					mb_detect_order($order); # remettre comme precedemment
421
-				}
422
-			}
423
-
424
-			// Sinon, peut-etre connaissons-nous ce charset ?
425
-			if (!isset($trans[$charset])) {
426
-				if (
427
-					$cset = load_charset($charset)
428
-					and is_array($GLOBALS['CHARSET'][$cset])
429
-				) {
430
-					foreach ($GLOBALS['CHARSET'][$cset] as $key => $val) {
431
-						$trans[$charset][chr($key)] = '&#' . $val . ';';
432
-					}
433
-				}
434
-			}
435
-			if (isset($trans[$charset]) and count($trans[$charset])) {
436
-				return str_replace(array_keys($trans[$charset]), array_values($trans[$charset]), $texte);
437
-			}
438
-
439
-			// Sinon demander a iconv (malgre le fait qu'il coupe quand un
440
-			// caractere n'appartient pas au charset, mais c'est un probleme
441
-			// surtout en utf-8, gere ci-dessus)
442
-			if (test_iconv()) {
443
-				$s = iconv($charset, 'utf-32le', $texte);
444
-				if ($s) {
445
-					return utf_32_to_unicode($s);
446
-				}
447
-			}
448
-
449
-			// Au pire ne rien faire
450
-			spip_log("erreur charset '$charset' non supporte");
451
-
452
-			return $texte;
453
-	}
417
+                } catch (\Error $e) {
418
+                    // Le charset n'existe probablement pas
419
+                } finally {
420
+                    mb_detect_order($order); # remettre comme precedemment
421
+                }
422
+            }
423
+
424
+            // Sinon, peut-etre connaissons-nous ce charset ?
425
+            if (!isset($trans[$charset])) {
426
+                if (
427
+                    $cset = load_charset($charset)
428
+                    and is_array($GLOBALS['CHARSET'][$cset])
429
+                ) {
430
+                    foreach ($GLOBALS['CHARSET'][$cset] as $key => $val) {
431
+                        $trans[$charset][chr($key)] = '&#' . $val . ';';
432
+                    }
433
+                }
434
+            }
435
+            if (isset($trans[$charset]) and count($trans[$charset])) {
436
+                return str_replace(array_keys($trans[$charset]), array_values($trans[$charset]), $texte);
437
+            }
438
+
439
+            // Sinon demander a iconv (malgre le fait qu'il coupe quand un
440
+            // caractere n'appartient pas au charset, mais c'est un probleme
441
+            // surtout en utf-8, gere ci-dessus)
442
+            if (test_iconv()) {
443
+                $s = iconv($charset, 'utf-32le', $texte);
444
+                if ($s) {
445
+                    return utf_32_to_unicode($s);
446
+                }
447
+            }
448
+
449
+            // Au pire ne rien faire
450
+            spip_log("erreur charset '$charset' non supporte");
451
+
452
+            return $texte;
453
+    }
454 454
 }
455 455
 
456 456
 
@@ -469,44 +469,44 @@  discard block
 block discarded – undo
469 469
  *     Texte transformé dans le charset souhaité
470 470
  **/
471 471
 function unicode2charset($texte, $charset = 'AUTO') {
472
-	static $CHARSET_REVERSE = array();
473
-	static $trans = array();
474
-
475
-	if ($charset == 'AUTO') {
476
-		$charset = lire_config('charset', _DEFAULT_CHARSET);
477
-	}
478
-
479
-	switch ($charset) {
480
-		case 'utf-8':
481
-			return unicode_to_utf_8($texte);
482
-			break;
483
-
484
-		default:
485
-			$charset = load_charset($charset);
486
-
487
-			if (empty($CHARSET_REVERSE[$charset])) {
488
-				$CHARSET_REVERSE[$charset] = array_flip($GLOBALS['CHARSET'][$charset]);
489
-			}
490
-
491
-			if (!isset($trans[$charset])) {
492
-				$trans[$charset] = array();
493
-				$t = &$trans[$charset];
494
-				for ($e = 128; $e < 255; $e++) {
495
-					$h = dechex($e);
496
-					if ($s = isset($CHARSET_REVERSE[$charset][$e])) {
497
-						$s = $CHARSET_REVERSE[$charset][$e];
498
-						$t['&#' . $e . ';'] = $t['&#0' . $e . ';'] = $t['&#00' . $e . ';'] = chr($s);
499
-						$t['&#x' . $h . ';'] = $t['&#x0' . $h . ';'] = $t['&#x00' . $h . ';'] = chr($s);
500
-					} else {
501
-						$t['&#' . $e . ';'] = $t['&#0' . $e . ';'] = $t['&#00' . $e . ';'] = chr($e);
502
-						$t['&#x' . $h . ';'] = $t['&#x0' . $h . ';'] = $t['&#x00' . $h . ';'] = chr($e);
503
-					}
504
-				}
505
-			}
506
-			$texte = str_replace(array_keys($trans[$charset]), array_values($trans[$charset]), $texte);
507
-
508
-			return $texte;
509
-	}
472
+    static $CHARSET_REVERSE = array();
473
+    static $trans = array();
474
+
475
+    if ($charset == 'AUTO') {
476
+        $charset = lire_config('charset', _DEFAULT_CHARSET);
477
+    }
478
+
479
+    switch ($charset) {
480
+        case 'utf-8':
481
+            return unicode_to_utf_8($texte);
482
+            break;
483
+
484
+        default:
485
+            $charset = load_charset($charset);
486
+
487
+            if (empty($CHARSET_REVERSE[$charset])) {
488
+                $CHARSET_REVERSE[$charset] = array_flip($GLOBALS['CHARSET'][$charset]);
489
+            }
490
+
491
+            if (!isset($trans[$charset])) {
492
+                $trans[$charset] = array();
493
+                $t = &$trans[$charset];
494
+                for ($e = 128; $e < 255; $e++) {
495
+                    $h = dechex($e);
496
+                    if ($s = isset($CHARSET_REVERSE[$charset][$e])) {
497
+                        $s = $CHARSET_REVERSE[$charset][$e];
498
+                        $t['&#' . $e . ';'] = $t['&#0' . $e . ';'] = $t['&#00' . $e . ';'] = chr($s);
499
+                        $t['&#x' . $h . ';'] = $t['&#x0' . $h . ';'] = $t['&#x00' . $h . ';'] = chr($s);
500
+                    } else {
501
+                        $t['&#' . $e . ';'] = $t['&#0' . $e . ';'] = $t['&#00' . $e . ';'] = chr($e);
502
+                        $t['&#x' . $h . ';'] = $t['&#x0' . $h . ';'] = $t['&#x00' . $h . ';'] = chr($e);
503
+                    }
504
+                }
505
+            }
506
+            $texte = str_replace(array_keys($trans[$charset]), array_values($trans[$charset]), $texte);
507
+
508
+            return $texte;
509
+    }
510 510
 }
511 511
 
512 512
 
@@ -524,37 +524,37 @@  discard block
 block discarded – undo
524 524
  *     Texte transformé dans le charset site
525 525
  **/
526 526
 function importer_charset($texte, $charset = 'AUTO') {
527
-	static $trans = array();
528
-	// on traite le cas le plus frequent iso-8859-1 vers utf directement pour aller plus vite !
529
-	if (($charset == 'iso-8859-1') && ($GLOBALS['meta']['charset'] == 'utf-8')) {
530
-		$texte = corriger_caracteres_windows($texte, 'iso-8859-1', $GLOBALS['meta']['charset']);
531
-		if (init_mb_string()) {
532
-			if ($order = mb_detect_order() # mb_string connait-il $charset?
533
-				and mb_detect_order($charset)
534
-			) {
535
-				$s = mb_convert_encoding($texte, 'utf-8', $charset);
536
-			}
537
-			mb_detect_order($order); # remettre comme precedemment
538
-			return $s;
539
-		}
540
-		// Sinon, peut-etre connaissons-nous ce charset ?
541
-		if (!isset($trans[$charset])) {
542
-			if ($cset = load_charset($charset)
543
-				and is_array($GLOBALS['CHARSET'][$cset])
544
-			) {
545
-				foreach ($GLOBALS['CHARSET'][$cset] as $key => $val) {
546
-					$trans[$charset][chr($key)] = unicode2charset('&#' . $val . ';');
547
-				}
548
-			}
549
-		}
550
-		if (count($trans[$charset])) {
551
-			return str_replace(array_keys($trans[$charset]), array_values($trans[$charset]), $texte);
552
-		}
553
-
554
-		return $texte;
555
-	}
556
-
557
-	return unicode2charset(charset2unicode($texte, $charset));
527
+    static $trans = array();
528
+    // on traite le cas le plus frequent iso-8859-1 vers utf directement pour aller plus vite !
529
+    if (($charset == 'iso-8859-1') && ($GLOBALS['meta']['charset'] == 'utf-8')) {
530
+        $texte = corriger_caracteres_windows($texte, 'iso-8859-1', $GLOBALS['meta']['charset']);
531
+        if (init_mb_string()) {
532
+            if ($order = mb_detect_order() # mb_string connait-il $charset?
533
+                and mb_detect_order($charset)
534
+            ) {
535
+                $s = mb_convert_encoding($texte, 'utf-8', $charset);
536
+            }
537
+            mb_detect_order($order); # remettre comme precedemment
538
+            return $s;
539
+        }
540
+        // Sinon, peut-etre connaissons-nous ce charset ?
541
+        if (!isset($trans[$charset])) {
542
+            if ($cset = load_charset($charset)
543
+                and is_array($GLOBALS['CHARSET'][$cset])
544
+            ) {
545
+                foreach ($GLOBALS['CHARSET'][$cset] as $key => $val) {
546
+                    $trans[$charset][chr($key)] = unicode2charset('&#' . $val . ';');
547
+                }
548
+            }
549
+        }
550
+        if (count($trans[$charset])) {
551
+            return str_replace(array_keys($trans[$charset]), array_values($trans[$charset]), $texte);
552
+        }
553
+
554
+        return $texte;
555
+    }
556
+
557
+    return unicode2charset(charset2unicode($texte, $charset));
558 558
 }
559 559
 
560 560
 
@@ -570,92 +570,92 @@  discard block
 block discarded – undo
570 570
  **/
571 571
 function utf_8_to_unicode($source) {
572 572
 
573
-	// mb_string : methode rapide
574
-	if (init_mb_string()) {
575
-		$convmap = array(0x7F, 0xFFFFFF, 0x0, 0xFFFFFF);
576
-
577
-		return mb_encode_numericentity($source, $convmap, 'UTF-8');
578
-	}
579
-
580
-	// Sinon methode pas a pas
581
-	static $decrement;
582
-	static $shift;
583
-
584
-	// Cf. php.net, par Ronen. Adapte pour compatibilite < php4
585
-	if (!is_array($decrement)) {
586
-		// array used to figure what number to decrement from character order value
587
-		// according to number of characters used to map unicode to ascii by utf-8
588
-		$decrement[4] = 240;
589
-		$decrement[3] = 224;
590
-		$decrement[2] = 192;
591
-		$decrement[1] = 0;
592
-		// the number of bits to shift each charNum by
593
-		$shift[1][0] = 0;
594
-		$shift[2][0] = 6;
595
-		$shift[2][1] = 0;
596
-		$shift[3][0] = 12;
597
-		$shift[3][1] = 6;
598
-		$shift[3][2] = 0;
599
-		$shift[4][0] = 18;
600
-		$shift[4][1] = 12;
601
-		$shift[4][2] = 6;
602
-		$shift[4][3] = 0;
603
-	}
604
-
605
-	$pos = 0;
606
-	$len = strlen($source);
607
-	$encodedString = '';
608
-	while ($pos < $len) {
609
-		$char = '';
610
-		$ischar = false;
611
-		$asciiPos = ord(substr($source, $pos, 1));
612
-		if (($asciiPos >= 240) && ($asciiPos <= 255)) {
613
-			// 4 chars representing one unicode character
614
-			$thisLetter = substr($source, $pos, 4);
615
-			$pos += 4;
616
-		} else {
617
-			if (($asciiPos >= 224) && ($asciiPos <= 239)) {
618
-				// 3 chars representing one unicode character
619
-				$thisLetter = substr($source, $pos, 3);
620
-				$pos += 3;
621
-			} else {
622
-				if (($asciiPos >= 192) && ($asciiPos <= 223)) {
623
-					// 2 chars representing one unicode character
624
-					$thisLetter = substr($source, $pos, 2);
625
-					$pos += 2;
626
-				} else {
627
-					// 1 char (lower ascii)
628
-					$thisLetter = substr($source, $pos, 1);
629
-					$pos += 1;
630
-					$char = $thisLetter;
631
-					$ischar = true;
632
-				}
633
-			}
634
-		}
635
-
636
-		if ($ischar) {
637
-			$encodedString .= $char;
638
-		} else {  // process the string representing the letter to a unicode entity
639
-			$thisLen = strlen($thisLetter);
640
-			$thisPos = 0;
641
-			$decimalCode = 0;
642
-			while ($thisPos < $thisLen) {
643
-				$thisCharOrd = ord(substr($thisLetter, $thisPos, 1));
644
-				if ($thisPos == 0) {
645
-					$charNum = intval($thisCharOrd - $decrement[$thisLen]);
646
-					$decimalCode += ($charNum << $shift[$thisLen][$thisPos]);
647
-				} else {
648
-					$charNum = intval($thisCharOrd - 128);
649
-					$decimalCode += ($charNum << $shift[$thisLen][$thisPos]);
650
-				}
651
-				$thisPos++;
652
-			}
653
-			$encodedLetter = "&#" . preg_replace('/^0+/', '', $decimalCode) . ';';
654
-			$encodedString .= $encodedLetter;
655
-		}
656
-	}
657
-
658
-	return $encodedString;
573
+    // mb_string : methode rapide
574
+    if (init_mb_string()) {
575
+        $convmap = array(0x7F, 0xFFFFFF, 0x0, 0xFFFFFF);
576
+
577
+        return mb_encode_numericentity($source, $convmap, 'UTF-8');
578
+    }
579
+
580
+    // Sinon methode pas a pas
581
+    static $decrement;
582
+    static $shift;
583
+
584
+    // Cf. php.net, par Ronen. Adapte pour compatibilite < php4
585
+    if (!is_array($decrement)) {
586
+        // array used to figure what number to decrement from character order value
587
+        // according to number of characters used to map unicode to ascii by utf-8
588
+        $decrement[4] = 240;
589
+        $decrement[3] = 224;
590
+        $decrement[2] = 192;
591
+        $decrement[1] = 0;
592
+        // the number of bits to shift each charNum by
593
+        $shift[1][0] = 0;
594
+        $shift[2][0] = 6;
595
+        $shift[2][1] = 0;
596
+        $shift[3][0] = 12;
597
+        $shift[3][1] = 6;
598
+        $shift[3][2] = 0;
599
+        $shift[4][0] = 18;
600
+        $shift[4][1] = 12;
601
+        $shift[4][2] = 6;
602
+        $shift[4][3] = 0;
603
+    }
604
+
605
+    $pos = 0;
606
+    $len = strlen($source);
607
+    $encodedString = '';
608
+    while ($pos < $len) {
609
+        $char = '';
610
+        $ischar = false;
611
+        $asciiPos = ord(substr($source, $pos, 1));
612
+        if (($asciiPos >= 240) && ($asciiPos <= 255)) {
613
+            // 4 chars representing one unicode character
614
+            $thisLetter = substr($source, $pos, 4);
615
+            $pos += 4;
616
+        } else {
617
+            if (($asciiPos >= 224) && ($asciiPos <= 239)) {
618
+                // 3 chars representing one unicode character
619
+                $thisLetter = substr($source, $pos, 3);
620
+                $pos += 3;
621
+            } else {
622
+                if (($asciiPos >= 192) && ($asciiPos <= 223)) {
623
+                    // 2 chars representing one unicode character
624
+                    $thisLetter = substr($source, $pos, 2);
625
+                    $pos += 2;
626
+                } else {
627
+                    // 1 char (lower ascii)
628
+                    $thisLetter = substr($source, $pos, 1);
629
+                    $pos += 1;
630
+                    $char = $thisLetter;
631
+                    $ischar = true;
632
+                }
633
+            }
634
+        }
635
+
636
+        if ($ischar) {
637
+            $encodedString .= $char;
638
+        } else {  // process the string representing the letter to a unicode entity
639
+            $thisLen = strlen($thisLetter);
640
+            $thisPos = 0;
641
+            $decimalCode = 0;
642
+            while ($thisPos < $thisLen) {
643
+                $thisCharOrd = ord(substr($thisLetter, $thisPos, 1));
644
+                if ($thisPos == 0) {
645
+                    $charNum = intval($thisCharOrd - $decrement[$thisLen]);
646
+                    $decimalCode += ($charNum << $shift[$thisLen][$thisPos]);
647
+                } else {
648
+                    $charNum = intval($thisCharOrd - 128);
649
+                    $decimalCode += ($charNum << $shift[$thisLen][$thisPos]);
650
+                }
651
+                $thisPos++;
652
+            }
653
+            $encodedLetter = "&#" . preg_replace('/^0+/', '', $decimalCode) . ';';
654
+            $encodedString .= $encodedLetter;
655
+        }
656
+    }
657
+
658
+    return $encodedString;
659 659
 }
660 660
 
661 661
 /**
@@ -674,32 +674,32 @@  discard block
 block discarded – undo
674 674
  **/
675 675
 function utf_32_to_unicode($source) {
676 676
 
677
-	// mb_string : methode rapide
678
-	if (init_mb_string()) {
679
-		$convmap = array(0x7F, 0xFFFFFF, 0x0, 0xFFFFFF);
680
-		$source = mb_encode_numericentity($source, $convmap, 'UTF-32LE');
681
-
682
-		return str_replace(chr(0), '', $source);
683
-	}
684
-
685
-	// Sinon methode lente
686
-	$texte = '';
687
-	while ($source) {
688
-		$words = unpack("V*", substr($source, 0, 1024));
689
-		$source = substr($source, 1024);
690
-		foreach ($words as $word) {
691
-			if ($word < 128) {
692
-				$texte .= chr($word);
693
-			} // ignorer le BOM - http://www.unicode.org/faq/utf_bom.html
694
-			else {
695
-				if ($word != 65279) {
696
-					$texte .= '&#' . $word . ';';
697
-				}
698
-			}
699
-		}
700
-	}
701
-
702
-	return $texte;
677
+    // mb_string : methode rapide
678
+    if (init_mb_string()) {
679
+        $convmap = array(0x7F, 0xFFFFFF, 0x0, 0xFFFFFF);
680
+        $source = mb_encode_numericentity($source, $convmap, 'UTF-32LE');
681
+
682
+        return str_replace(chr(0), '', $source);
683
+    }
684
+
685
+    // Sinon methode lente
686
+    $texte = '';
687
+    while ($source) {
688
+        $words = unpack("V*", substr($source, 0, 1024));
689
+        $source = substr($source, 1024);
690
+        foreach ($words as $word) {
691
+            if ($word < 128) {
692
+                $texte .= chr($word);
693
+            } // ignorer le BOM - http://www.unicode.org/faq/utf_bom.html
694
+            else {
695
+                if ($word != 65279) {
696
+                    $texte .= '&#' . $word . ';';
697
+                }
698
+            }
699
+        }
700
+    }
701
+
702
+    return $texte;
703 703
 
704 704
 }
705 705
 
@@ -717,21 +717,21 @@  discard block
 block discarded – undo
717 717
  *    Caractère utf8 si trouvé, '' sinon
718 718
  **/
719 719
 function caractere_utf_8($num) {
720
-	$num = intval($num);
721
-	if ($num < 128) {
722
-		return chr($num);
723
-	}
724
-	if ($num < 2048) {
725
-		return chr(($num >> 6) + 192) . chr(($num & 63) + 128);
726
-	}
727
-	if ($num < 65536) {
728
-		return chr(($num >> 12) + 224) . chr((($num >> 6) & 63) + 128) . chr(($num & 63) + 128);
729
-	}
730
-	if ($num < 1114112) {
731
-		return chr(($num >> 18) + 240) . chr((($num >> 12) & 63) + 128) . chr((($num >> 6) & 63) + 128) . chr(($num & 63) + 128);
732
-	}
733
-
734
-	return '';
720
+    $num = intval($num);
721
+    if ($num < 128) {
722
+        return chr($num);
723
+    }
724
+    if ($num < 2048) {
725
+        return chr(($num >> 6) + 192) . chr(($num & 63) + 128);
726
+    }
727
+    if ($num < 65536) {
728
+        return chr(($num >> 12) + 224) . chr((($num >> 6) & 63) + 128) . chr(($num & 63) + 128);
729
+    }
730
+    if ($num < 1114112) {
731
+        return chr(($num >> 18) + 240) . chr((($num >> 12) & 63) + 128) . chr((($num >> 6) & 63) + 128) . chr(($num & 63) + 128);
732
+    }
733
+
734
+    return '';
735 735
 }
736 736
 
737 737
 /**
@@ -744,30 +744,30 @@  discard block
 block discarded – undo
744 744
  **/
745 745
 function unicode_to_utf_8($texte) {
746 746
 
747
-	// 1. Entites &#128; et suivantes
748
-	$vu = array();
749
-	if (preg_match_all(',&#0*([1-9][0-9][0-9]+);,S',
750
-		$texte, $regs, PREG_SET_ORDER)) {
751
-		foreach ($regs as $reg) {
752
-			if ($reg[1] > 127 and !isset($vu[$reg[0]])) {
753
-				$vu[$reg[0]] = caractere_utf_8($reg[1]);
754
-			}
755
-		}
756
-	}
757
-	//$texte = str_replace(array_keys($vu), array_values($vu), $texte);
758
-
759
-	// 2. Entites > &#xFF;
760
-	//$vu = array();
761
-	if (preg_match_all(',&#x0*([1-9a-f][0-9a-f][0-9a-f]+);,iS',
762
-		$texte, $regs, PREG_SET_ORDER)) {
763
-		foreach ($regs as $reg) {
764
-			if (!isset($vu[$reg[0]])) {
765
-				$vu[$reg[0]] = caractere_utf_8(hexdec($reg[1]));
766
-			}
767
-		}
768
-	}
769
-
770
-	return str_replace(array_keys($vu), array_values($vu), $texte);
747
+    // 1. Entites &#128; et suivantes
748
+    $vu = array();
749
+    if (preg_match_all(',&#0*([1-9][0-9][0-9]+);,S',
750
+        $texte, $regs, PREG_SET_ORDER)) {
751
+        foreach ($regs as $reg) {
752
+            if ($reg[1] > 127 and !isset($vu[$reg[0]])) {
753
+                $vu[$reg[0]] = caractere_utf_8($reg[1]);
754
+            }
755
+        }
756
+    }
757
+    //$texte = str_replace(array_keys($vu), array_values($vu), $texte);
758
+
759
+    // 2. Entites > &#xFF;
760
+    //$vu = array();
761
+    if (preg_match_all(',&#x0*([1-9a-f][0-9a-f][0-9a-f]+);,iS',
762
+        $texte, $regs, PREG_SET_ORDER)) {
763
+        foreach ($regs as $reg) {
764
+            if (!isset($vu[$reg[0]])) {
765
+                $vu[$reg[0]] = caractere_utf_8(hexdec($reg[1]));
766
+            }
767
+        }
768
+    }
769
+
770
+    return str_replace(array_keys($vu), array_values($vu), $texte);
771 771
 
772 772
 }
773 773
 
@@ -780,15 +780,15 @@  discard block
 block discarded – undo
780 780
  *     Texte converti
781 781
  **/
782 782
 function unicode_to_javascript($texte) {
783
-	$vu = array();
784
-	while (preg_match(',&#0*([0-9]+);,S', $texte, $regs) and !isset($vu[$regs[1]])) {
785
-		$num = $regs[1];
786
-		$vu[$num] = true;
787
-		$s = '\u' . sprintf("%04x", $num);
788
-		$texte = str_replace($regs[0], $s, $texte);
789
-	}
790
-
791
-	return $texte;
783
+    $vu = array();
784
+    while (preg_match(',&#0*([0-9]+);,S', $texte, $regs) and !isset($vu[$regs[1]])) {
785
+        $num = $regs[1];
786
+        $vu[$num] = true;
787
+        $s = '\u' . sprintf("%04x", $num);
788
+        $texte = str_replace($regs[0], $s, $texte);
789
+    }
790
+
791
+    return $texte;
792 792
 }
793 793
 
794 794
 /**
@@ -800,11 +800,11 @@  discard block
 block discarded – undo
800 800
  *     Texte converti
801 801
  **/
802 802
 function javascript_to_unicode($texte) {
803
-	while (preg_match(",%u([0-9A-F][0-9A-F][0-9A-F][0-9A-F]),", $texte, $regs)) {
804
-		$texte = str_replace($regs[0], "&#" . hexdec($regs[1]) . ";", $texte);
805
-	}
803
+    while (preg_match(",%u([0-9A-F][0-9A-F][0-9A-F][0-9A-F]),", $texte, $regs)) {
804
+        $texte = str_replace($regs[0], "&#" . hexdec($regs[1]) . ";", $texte);
805
+    }
806 806
 
807
-	return $texte;
807
+    return $texte;
808 808
 }
809 809
 
810 810
 /**
@@ -816,11 +816,11 @@  discard block
 block discarded – undo
816 816
  *     Texte converti
817 817
  **/
818 818
 function javascript_to_binary($texte) {
819
-	while (preg_match(",%([0-9A-F][0-9A-F]),", $texte, $regs)) {
820
-		$texte = str_replace($regs[0], chr(hexdec($regs[1])), $texte);
821
-	}
819
+    while (preg_match(",%([0-9A-F][0-9A-F]),", $texte, $regs)) {
820
+        $texte = str_replace($regs[0], chr(hexdec($regs[1])), $texte);
821
+    }
822 822
 
823
-	return $texte;
823
+    return $texte;
824 824
 }
825 825
 
826 826
 
@@ -838,26 +838,26 @@  discard block
 block discarded – undo
838 838
  * @return string
839 839
  */
840 840
 function translitteration_rapide($texte, $charset = 'AUTO', $complexe = '') {
841
-	static $trans = [];
842
-	if ($charset == 'AUTO') {
843
-		$charset = $GLOBALS['meta']['charset'];
844
-	}
845
-	if (!strlen($texte)) {
846
-		return $texte;
847
-	}
848
-
849
-	$table_translit = 'translit' . $complexe;
850
-
851
-	// 2. Translitterer grace a la table predefinie
852
-	if (!isset($trans[$complexe])) {
853
-		$trans[$complexe] = [];
854
-		load_charset($table_translit);
855
-		foreach ($GLOBALS['CHARSET'][$table_translit] as $key => $val) {
856
-			$trans[$complexe][caractere_utf_8($key)] = $val;
857
-		}
858
-	}
859
-
860
-	return str_replace(array_keys($trans[$complexe]), array_values($trans[$complexe]), $texte);
841
+    static $trans = [];
842
+    if ($charset == 'AUTO') {
843
+        $charset = $GLOBALS['meta']['charset'];
844
+    }
845
+    if (!strlen($texte)) {
846
+        return $texte;
847
+    }
848
+
849
+    $table_translit = 'translit' . $complexe;
850
+
851
+    // 2. Translitterer grace a la table predefinie
852
+    if (!isset($trans[$complexe])) {
853
+        $trans[$complexe] = [];
854
+        load_charset($table_translit);
855
+        foreach ($GLOBALS['CHARSET'][$table_translit] as $key => $val) {
856
+            $trans[$complexe][caractere_utf_8($key)] = $val;
857
+        }
858
+    }
859
+
860
+    return str_replace(array_keys($trans[$complexe]), array_values($trans[$complexe]), $texte);
861 861
 }
862 862
 
863 863
 /**
@@ -880,14 +880,14 @@  discard block
 block discarded – undo
880 880
  * @return string
881 881
  */
882 882
 function translitteration($texte, $charset = 'AUTO', $complexe = '') {
883
-	// 0. Supprimer les caracteres illegaux
884
-	include_spip('inc/filtres');
885
-	$texte = corriger_caracteres($texte);
883
+    // 0. Supprimer les caracteres illegaux
884
+    include_spip('inc/filtres');
885
+    $texte = corriger_caracteres($texte);
886 886
 
887
-	// 1. Passer le charset et les &eacute en utf-8
888
-	$texte = unicode_to_utf_8(html2unicode(charset2unicode($texte, $charset, true)));
887
+    // 1. Passer le charset et les &eacute en utf-8
888
+    $texte = unicode_to_utf_8(html2unicode(charset2unicode($texte, $charset, true)));
889 889
 
890
-	return translitteration_rapide($texte, $charset, $complexe);
890
+    return translitteration_rapide($texte, $charset, $complexe);
891 891
 }
892 892
 
893 893
 /**
@@ -902,17 +902,17 @@  discard block
 block discarded – undo
902 902
  * @return string
903 903
  */
904 904
 function translitteration_complexe($texte, $chiffres = false) {
905
-	$texte = translitteration($texte, 'AUTO', 'complexe');
905
+    $texte = translitteration($texte, 'AUTO', 'complexe');
906 906
 
907
-	if ($chiffres) {
908
-		$texte = preg_replace_callback(
909
-			"/[aeiuoyd]['`?~.^+(-]{1,2}/S",
910
-			function($m) { return translitteration_chiffree($m[0]); },
911
-			$texte
912
-		);
913
-	}
907
+    if ($chiffres) {
908
+        $texte = preg_replace_callback(
909
+            "/[aeiuoyd]['`?~.^+(-]{1,2}/S",
910
+            function($m) { return translitteration_chiffree($m[0]); },
911
+            $texte
912
+        );
913
+    }
914 914
 
915
-	return $texte;
915
+    return $texte;
916 916
 }
917 917
 
918 918
 /**
@@ -924,7 +924,7 @@  discard block
 block discarded – undo
924 924
  * @return string
925 925
  */
926 926
 function translitteration_chiffree($car) {
927
-	return strtr($car, "'`?~.^+(-", "123456789");
927
+    return strtr($car, "'`?~.^+(-", "123456789");
928 928
 }
929 929
 
930 930
 
@@ -937,7 +937,7 @@  discard block
 block discarded – undo
937 937
  *    true s'il a un BOM
938 938
  **/
939 939
 function bom_utf8($texte) {
940
-	return (substr($texte, 0, 3) == chr(0xEF) . chr(0xBB) . chr(0xBF));
940
+    return (substr($texte, 0, 3) == chr(0xEF) . chr(0xBB) . chr(0xBF));
941 941
 }
942 942
 
943 943
 /**
@@ -954,18 +954,18 @@  discard block
 block discarded – undo
954 954
  *     true si c'est le cas
955 955
  **/
956 956
 function is_utf8($string) {
957
-	return !strlen(
958
-		preg_replace(
959
-			',[\x09\x0A\x0D\x20-\x7E]'            # ASCII
960
-			. '|[\xC2-\xDF][\x80-\xBF]'             # non-overlong 2-byte
961
-			. '|\xE0[\xA0-\xBF][\x80-\xBF]'         # excluding overlongs
962
-			. '|[\xE1-\xEC\xEE\xEF][\x80-\xBF]{2}'  # straight 3-byte
963
-			. '|\xED[\x80-\x9F][\x80-\xBF]'         # excluding surrogates
964
-			. '|\xF0[\x90-\xBF][\x80-\xBF]{2}'      # planes 1-3
965
-			. '|[\xF1-\xF3][\x80-\xBF]{3}'          # planes 4-15
966
-			. '|\xF4[\x80-\x8F][\x80-\xBF]{2}'      # plane 16
967
-			. ',sS',
968
-			'', $string));
957
+    return !strlen(
958
+        preg_replace(
959
+            ',[\x09\x0A\x0D\x20-\x7E]'            # ASCII
960
+            . '|[\xC2-\xDF][\x80-\xBF]'             # non-overlong 2-byte
961
+            . '|\xE0[\xA0-\xBF][\x80-\xBF]'         # excluding overlongs
962
+            . '|[\xE1-\xEC\xEE\xEF][\x80-\xBF]{2}'  # straight 3-byte
963
+            . '|\xED[\x80-\x9F][\x80-\xBF]'         # excluding surrogates
964
+            . '|\xF0[\x90-\xBF][\x80-\xBF]{2}'      # planes 1-3
965
+            . '|[\xF1-\xF3][\x80-\xBF]{3}'          # planes 4-15
966
+            . '|\xF4[\x80-\x8F][\x80-\xBF]{2}'      # plane 16
967
+            . ',sS',
968
+            '', $string));
969 969
 }
970 970
 
971 971
 /**
@@ -977,10 +977,10 @@  discard block
 block discarded – undo
977 977
  *     true si c'est le cas
978 978
  **/
979 979
 function is_ascii($string) {
980
-	return !strlen(
981
-		preg_replace(
982
-			',[\x09\x0A\x0D\x20-\x7E],sS',
983
-			'', $string));
980
+    return !strlen(
981
+        preg_replace(
982
+            ',[\x09\x0A\x0D\x20-\x7E],sS',
983
+            '', $string));
984 984
 }
985 985
 
986 986
 /**
@@ -999,53 +999,53 @@  discard block
 block discarded – undo
999 999
  **/
1000 1000
 function transcoder_page($texte, $headers = '') {
1001 1001
 
1002
-	// Si tout est < 128 pas la peine d'aller plus loin
1003
-	if (is_ascii($texte)) {
1004
-		#spip_log('charset: ascii');
1005
-		return $texte;
1006
-	}
1007
-
1008
-	if (bom_utf8($texte)) {
1009
-		// Reconnaitre le BOM utf-8 (0xEFBBBF)
1010
-		$charset = 'utf-8';
1011
-		$texte = substr($texte, 3);
1012
-	} elseif (preg_match(',<[?]xml[^>]*encoding[^>]*=[^>]*([-_a-z0-9]+?),UimsS', $texte, $regs)) {
1013
-		// charset precise par le contenu (xml)
1014
-		$charset = trim(strtolower($regs[1]));
1015
-	} elseif (
1016
-		// charset precise par le contenu (html)
1017
-		preg_match(',<(meta|html|body)[^>]*charset[^>]*=[^>]*([#-_a-z0-9]+?),UimsS', $texte, $regs)
1018
-		# eviter toute balise SPIP tel que #CHARSET ou #CONFIG d'un squelette
1019
-		and false === strpos($regs[2], '#')
1020
-		and $tmp = trim(strtolower($regs[2]))
1021
-	) {
1022
-		$charset = $tmp;
1023
-	} elseif (preg_match(',charset=([-_a-z0-9]+),i', $headers, $regs)) {
1024
-		// charset de la reponse http
1025
-		$charset = trim(strtolower($regs[1]));
1026
-	} else {
1027
-		$charset = '';
1028
-	}
1029
-
1030
-
1031
-	// normaliser les noms du shif-jis japonais
1032
-	if (preg_match(',^(x|shift)[_-]s?jis$,i', $charset)) {
1033
-		$charset = 'shift-jis';
1034
-	}
1035
-
1036
-	if ($charset) {
1037
-		spip_log("charset: $charset");
1038
-	} else {
1039
-		// valeur par defaut
1040
-		if (is_utf8($texte)) {
1041
-			$charset = 'utf-8';
1042
-		} else {
1043
-			$charset = 'iso-8859-1';
1044
-		}
1045
-		spip_log("charset probable: $charset");
1046
-	}
1047
-
1048
-	return importer_charset($texte, $charset);
1002
+    // Si tout est < 128 pas la peine d'aller plus loin
1003
+    if (is_ascii($texte)) {
1004
+        #spip_log('charset: ascii');
1005
+        return $texte;
1006
+    }
1007
+
1008
+    if (bom_utf8($texte)) {
1009
+        // Reconnaitre le BOM utf-8 (0xEFBBBF)
1010
+        $charset = 'utf-8';
1011
+        $texte = substr($texte, 3);
1012
+    } elseif (preg_match(',<[?]xml[^>]*encoding[^>]*=[^>]*([-_a-z0-9]+?),UimsS', $texte, $regs)) {
1013
+        // charset precise par le contenu (xml)
1014
+        $charset = trim(strtolower($regs[1]));
1015
+    } elseif (
1016
+        // charset precise par le contenu (html)
1017
+        preg_match(',<(meta|html|body)[^>]*charset[^>]*=[^>]*([#-_a-z0-9]+?),UimsS', $texte, $regs)
1018
+        # eviter toute balise SPIP tel que #CHARSET ou #CONFIG d'un squelette
1019
+        and false === strpos($regs[2], '#')
1020
+        and $tmp = trim(strtolower($regs[2]))
1021
+    ) {
1022
+        $charset = $tmp;
1023
+    } elseif (preg_match(',charset=([-_a-z0-9]+),i', $headers, $regs)) {
1024
+        // charset de la reponse http
1025
+        $charset = trim(strtolower($regs[1]));
1026
+    } else {
1027
+        $charset = '';
1028
+    }
1029
+
1030
+
1031
+    // normaliser les noms du shif-jis japonais
1032
+    if (preg_match(',^(x|shift)[_-]s?jis$,i', $charset)) {
1033
+        $charset = 'shift-jis';
1034
+    }
1035
+
1036
+    if ($charset) {
1037
+        spip_log("charset: $charset");
1038
+    } else {
1039
+        // valeur par defaut
1040
+        if (is_utf8($texte)) {
1041
+            $charset = 'utf-8';
1042
+        } else {
1043
+            $charset = 'iso-8859-1';
1044
+        }
1045
+        spip_log("charset probable: $charset");
1046
+    }
1047
+
1048
+    return importer_charset($texte, $charset);
1049 1049
 }
1050 1050
 
1051 1051
 
@@ -1069,26 +1069,26 @@  discard block
 block discarded – undo
1069 1069
  *     Le texte coupé
1070 1070
  **/
1071 1071
 function spip_substr($c, $start = 0, $length = null) {
1072
-	// Si ce n'est pas utf-8, utiliser substr
1073
-	if ($GLOBALS['meta']['charset'] != 'utf-8') {
1074
-		if ($length) {
1075
-			return substr($c, $start, $length);
1076
-		} else {
1077
-			substr($c, $start);
1078
-		}
1079
-	}
1080
-
1081
-	// Si utf-8, voir si on dispose de mb_string
1082
-	if (init_mb_string()) {
1083
-		if ($length) {
1084
-			return mb_substr($c, $start, $length);
1085
-		} else {
1086
-			return mb_substr($c, $start);
1087
-		}
1088
-	}
1089
-
1090
-	// Version manuelle (cf. ci-dessous)
1091
-	return spip_substr_manuelle($c, $start, $length);
1072
+    // Si ce n'est pas utf-8, utiliser substr
1073
+    if ($GLOBALS['meta']['charset'] != 'utf-8') {
1074
+        if ($length) {
1075
+            return substr($c, $start, $length);
1076
+        } else {
1077
+            substr($c, $start);
1078
+        }
1079
+    }
1080
+
1081
+    // Si utf-8, voir si on dispose de mb_string
1082
+    if (init_mb_string()) {
1083
+        if ($length) {
1084
+            return mb_substr($c, $start, $length);
1085
+        } else {
1086
+            return mb_substr($c, $start);
1087
+        }
1088
+    }
1089
+
1090
+    // Version manuelle (cf. ci-dessous)
1091
+    return spip_substr_manuelle($c, $start, $length);
1092 1092
 }
1093 1093
 
1094 1094
 
@@ -1107,40 +1107,40 @@  discard block
 block discarded – undo
1107 1107
  **/
1108 1108
 function spip_substr_manuelle($c, $start, $length = null) {
1109 1109
 
1110
-	// Cas pathologique
1111
-	if ($length === 0) {
1112
-		return '';
1113
-	}
1114
-
1115
-	// S'il y a un demarrage, on se positionne
1116
-	if ($start > 0) {
1117
-		$c = substr($c, strlen(spip_substr_manuelle($c, 0, $start)));
1118
-	} elseif ($start < 0) {
1119
-		return spip_substr_manuelle($c, spip_strlen($c) + $start, $length);
1120
-	}
1121
-
1122
-	if (!$length) {
1123
-		return $c;
1124
-	}
1125
-
1126
-	if ($length > 0) {
1127
-		// on prend n fois la longueur desiree, pour etre surs d'avoir tout
1128
-		// (un caractere utf-8 prenant au maximum n bytes)
1129
-		$n = 0;
1130
-		while (preg_match(',[\x80-\xBF]{' . (++$n) . '},', $c)) {
1131
-			;
1132
-		}
1133
-		$c = substr($c, 0, $n * $length);
1134
-		// puis, tant qu'on est trop long, on coupe...
1135
-		while (($l = spip_strlen($c)) > $length) {
1136
-			$c = substr($c, 0, $length - $l);
1137
-		}
1138
-
1139
-		return $c;
1140
-	}
1141
-
1142
-	// $length < 0
1143
-	return spip_substr_manuelle($c, 0, spip_strlen($c) + $length);
1110
+    // Cas pathologique
1111
+    if ($length === 0) {
1112
+        return '';
1113
+    }
1114
+
1115
+    // S'il y a un demarrage, on se positionne
1116
+    if ($start > 0) {
1117
+        $c = substr($c, strlen(spip_substr_manuelle($c, 0, $start)));
1118
+    } elseif ($start < 0) {
1119
+        return spip_substr_manuelle($c, spip_strlen($c) + $start, $length);
1120
+    }
1121
+
1122
+    if (!$length) {
1123
+        return $c;
1124
+    }
1125
+
1126
+    if ($length > 0) {
1127
+        // on prend n fois la longueur desiree, pour etre surs d'avoir tout
1128
+        // (un caractere utf-8 prenant au maximum n bytes)
1129
+        $n = 0;
1130
+        while (preg_match(',[\x80-\xBF]{' . (++$n) . '},', $c)) {
1131
+            ;
1132
+        }
1133
+        $c = substr($c, 0, $n * $length);
1134
+        // puis, tant qu'on est trop long, on coupe...
1135
+        while (($l = spip_strlen($c)) > $length) {
1136
+            $c = substr($c, 0, $length - $l);
1137
+        }
1138
+
1139
+        return $c;
1140
+    }
1141
+
1142
+    // $length < 0
1143
+    return spip_substr_manuelle($c, 0, spip_strlen($c) + $length);
1144 1144
 }
1145 1145
 
1146 1146
 /**
@@ -1154,14 +1154,14 @@  discard block
 block discarded – undo
1154 1154
  *     La chaîne avec une majuscule sur le premier mot
1155 1155
  */
1156 1156
 function spip_ucfirst($c) {
1157
-	// Si on n'a pas mb_* ou si ce n'est pas utf-8, utiliser ucfirst
1158
-	if (!init_mb_string() or $GLOBALS['meta']['charset'] != 'utf-8') {
1159
-		return ucfirst($c);
1160
-	}
1157
+    // Si on n'a pas mb_* ou si ce n'est pas utf-8, utiliser ucfirst
1158
+    if (!init_mb_string() or $GLOBALS['meta']['charset'] != 'utf-8') {
1159
+        return ucfirst($c);
1160
+    }
1161 1161
 
1162
-	$lettre1 = mb_strtoupper(spip_substr($c, 0, 1));
1162
+    $lettre1 = mb_strtoupper(spip_substr($c, 0, 1));
1163 1163
 
1164
-	return $lettre1 . spip_substr($c, 1);
1164
+    return $lettre1 . spip_substr($c, 1);
1165 1165
 }
1166 1166
 
1167 1167
 /**
@@ -1175,12 +1175,12 @@  discard block
 block discarded – undo
1175 1175
  *     La chaîne en minuscules
1176 1176
  */
1177 1177
 function spip_strtolower($c) {
1178
-	// Si on n'a pas mb_* ou si ce n'est pas utf-8, utiliser strtolower 
1179
-	if (!init_mb_string() or $GLOBALS['meta']['charset'] != 'utf-8') {
1180
-		return strtolower($c);
1181
-	}
1178
+    // Si on n'a pas mb_* ou si ce n'est pas utf-8, utiliser strtolower 
1179
+    if (!init_mb_string() or $GLOBALS['meta']['charset'] != 'utf-8') {
1180
+        return strtolower($c);
1181
+    }
1182 1182
 
1183
-	return mb_strtolower($c);
1183
+    return mb_strtolower($c);
1184 1184
 }
1185 1185
 
1186 1186
 /**
@@ -1194,23 +1194,23 @@  discard block
 block discarded – undo
1194 1194
  *     Longueur de la chaîne
1195 1195
  */
1196 1196
 function spip_strlen($c) {
1197
-	// On transforme les sauts de ligne pour ne pas compter deux caractères
1198
-	$c = str_replace("\r\n", "\n", $c);
1199
-
1200
-	// Si ce n'est pas utf-8, utiliser strlen
1201
-	if ($GLOBALS['meta']['charset'] != 'utf-8') {
1202
-		return strlen($c);
1203
-	}
1204
-
1205
-	// Sinon, utiliser mb_strlen() si disponible
1206
-	if (init_mb_string()) {
1207
-		return mb_strlen($c);
1208
-	}
1209
-
1210
-	// Methode manuelle : on supprime les bytes 10......,
1211
-	// on compte donc les ascii (0.......) et les demarrages
1212
-	// de caracteres utf-8 (11......)
1213
-	return strlen(preg_replace(',[\x80-\xBF],S', '', $c));
1197
+    // On transforme les sauts de ligne pour ne pas compter deux caractères
1198
+    $c = str_replace("\r\n", "\n", $c);
1199
+
1200
+    // Si ce n'est pas utf-8, utiliser strlen
1201
+    if ($GLOBALS['meta']['charset'] != 'utf-8') {
1202
+        return strlen($c);
1203
+    }
1204
+
1205
+    // Sinon, utiliser mb_strlen() si disponible
1206
+    if (init_mb_string()) {
1207
+        return mb_strlen($c);
1208
+    }
1209
+
1210
+    // Methode manuelle : on supprime les bytes 10......,
1211
+    // on compte donc les ascii (0.......) et les demarrages
1212
+    // de caracteres utf-8 (11......)
1213
+    return strlen(preg_replace(',[\x80-\xBF],S', '', $c));
1214 1214
 }
1215 1215
 
1216 1216
 // Initialisation
@@ -1220,14 +1220,14 @@  discard block
 block discarded – undo
1220 1220
 // dans les preg_replace pour ne pas casser certaines lettres accentuees :
1221 1221
 // en utf-8 chr(195).chr(160) = a` alors qu'en iso-latin chr(160) = nbsp
1222 1222
 if (!isset($GLOBALS['meta']['pcre_u'])
1223
-	or (isset($_GET['var_mode']) and !isset($_GET['var_profile']))
1223
+    or (isset($_GET['var_mode']) and !isset($_GET['var_profile']))
1224 1224
 ) {
1225
-	include_spip('inc/meta');
1226
-	ecrire_meta('pcre_u',
1227
-		$u = (lire_config('charset', _DEFAULT_CHARSET) == 'utf-8'
1228
-			and test_pcre_unicode())
1229
-			? 'u' : ''
1230
-	);
1225
+    include_spip('inc/meta');
1226
+    ecrire_meta('pcre_u',
1227
+        $u = (lire_config('charset', _DEFAULT_CHARSET) == 'utf-8'
1228
+            and test_pcre_unicode())
1229
+            ? 'u' : ''
1230
+    );
1231 1231
 }
1232 1232
 
1233 1233
 
@@ -1243,17 +1243,17 @@  discard block
 block discarded – undo
1243 1243
  *     en unicode : &#128169;
1244 1244
  */
1245 1245
 function utf8_noplanes($x) {
1246
-	$regexp_utf8_4bytes = '/(
1246
+    $regexp_utf8_4bytes = '/(
1247 1247
       \xF0[\x90-\xBF][\x80-\xBF]{2}     # planes 1-3
1248 1248
    | [\xF1-\xF3][\x80-\xBF]{3}          # planes 4-15
1249 1249
    |  \xF4[\x80-\x8F][\x80-\xBF]{2}     # plane 16
1250 1250
 )/xS';
1251
-	if (preg_match_all($regexp_utf8_4bytes, $x, $z, PREG_PATTERN_ORDER)) {
1252
-		foreach ($z[0] as $k) {
1253
-			$ku = utf_8_to_unicode($k);
1254
-			$x = str_replace($k, $ku, $x);
1255
-		}
1256
-	}
1257
-
1258
-	return $x;
1251
+    if (preg_match_all($regexp_utf8_4bytes, $x, $z, PREG_PATTERN_ORDER)) {
1252
+        foreach ($z[0] as $k) {
1253
+            $ku = utf_8_to_unicode($k);
1254
+            $x = str_replace($k, $ku, $x);
1255
+        }
1256
+    }
1257
+
1258
+    return $x;
1259 1259
 }
Please login to merge, or discard this patch.