@@ -22,7 +22,7 @@ discard block |
||
| 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 |
||
| 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] = []; |
|
| 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] = []; |
|
| 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] = []; |
|
| 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] = []; |
|
| 81 | + |
|
| 82 | + return false; |
|
| 83 | + } |
|
| 84 | 84 | } |
| 85 | 85 | |
| 86 | 86 | |
@@ -91,31 +91,31 @@ discard block |
||
| 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 ( |
|
| 100 | - function_exists('mb_internal_encoding') |
|
| 101 | - and function_exists('mb_detect_order') |
|
| 102 | - and function_exists('mb_substr') |
|
| 103 | - and function_exists('mb_strlen') |
|
| 104 | - and function_exists('mb_strtolower') |
|
| 105 | - and function_exists('mb_strtoupper') |
|
| 106 | - and function_exists('mb_encode_mimeheader') |
|
| 107 | - and function_exists('mb_encode_numericentity') |
|
| 108 | - and function_exists('mb_decode_numericentity') |
|
| 109 | - and mb_detect_order(lire_config('charset', _DEFAULT_CHARSET)) |
|
| 110 | - ) { |
|
| 111 | - mb_internal_encoding('utf-8'); |
|
| 112 | - $mb = 1; |
|
| 113 | - } else { |
|
| 114 | - $mb = -1; |
|
| 115 | - } |
|
| 116 | - } |
|
| 117 | - |
|
| 118 | - 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 ( |
|
| 100 | + function_exists('mb_internal_encoding') |
|
| 101 | + and function_exists('mb_detect_order') |
|
| 102 | + and function_exists('mb_substr') |
|
| 103 | + and function_exists('mb_strlen') |
|
| 104 | + and function_exists('mb_strtolower') |
|
| 105 | + and function_exists('mb_strtoupper') |
|
| 106 | + and function_exists('mb_encode_mimeheader') |
|
| 107 | + and function_exists('mb_encode_numericentity') |
|
| 108 | + and function_exists('mb_decode_numericentity') |
|
| 109 | + and mb_detect_order(lire_config('charset', _DEFAULT_CHARSET)) |
|
| 110 | + ) { |
|
| 111 | + mb_internal_encoding('utf-8'); |
|
| 112 | + $mb = 1; |
|
| 113 | + } else { |
|
| 114 | + $mb = -1; |
|
| 115 | + } |
|
| 116 | + } |
|
| 117 | + |
|
| 118 | + return ($mb == 1); |
|
| 119 | 119 | } |
| 120 | 120 | |
| 121 | 121 | /** |
@@ -130,21 +130,21 @@ discard block |
||
| 130 | 130 | * true si iconv fonctionne correctement |
| 131 | 131 | **/ |
| 132 | 132 | function test_iconv() { |
| 133 | - static $iconv_ok; |
|
| 134 | - |
|
| 135 | - if (!$iconv_ok) { |
|
| 136 | - if (!function_exists('iconv')) { |
|
| 137 | - $iconv_ok = -1; |
|
| 138 | - } else { |
|
| 139 | - if (utf_32_to_unicode(@iconv('utf-8', 'utf-32', 'chaine de test')) == 'chaine de test') { |
|
| 140 | - $iconv_ok = 1; |
|
| 141 | - } else { |
|
| 142 | - $iconv_ok = -1; |
|
| 143 | - } |
|
| 144 | - } |
|
| 145 | - } |
|
| 146 | - |
|
| 147 | - return ($iconv_ok == 1); |
|
| 133 | + static $iconv_ok; |
|
| 134 | + |
|
| 135 | + if (!$iconv_ok) { |
|
| 136 | + if (!function_exists('iconv')) { |
|
| 137 | + $iconv_ok = -1; |
|
| 138 | + } else { |
|
| 139 | + if (utf_32_to_unicode(@iconv('utf-8', 'utf-32', 'chaine de test')) == 'chaine de test') { |
|
| 140 | + $iconv_ok = 1; |
|
| 141 | + } else { |
|
| 142 | + $iconv_ok = -1; |
|
| 143 | + } |
|
| 144 | + } |
|
| 145 | + } |
|
| 146 | + |
|
| 147 | + return ($iconv_ok == 1); |
|
| 148 | 148 | } |
| 149 | 149 | |
| 150 | 150 | |
@@ -157,18 +157,18 @@ discard block |
||
| 157 | 157 | * true si PCRE supporte l'UTF-8 correctement |
| 158 | 158 | **/ |
| 159 | 159 | function test_pcre_unicode() { |
| 160 | - static $pcre_ok = 0; |
|
| 161 | - |
|
| 162 | - if (!$pcre_ok) { |
|
| 163 | - $s = ' ' . chr(195) . chr(169) . 't' . chr(195) . chr(169) . ' '; |
|
| 164 | - if (preg_match(',\W...\W,u', $s)) { |
|
| 165 | - $pcre_ok = 1; |
|
| 166 | - } else { |
|
| 167 | - $pcre_ok = -1; |
|
| 168 | - } |
|
| 169 | - } |
|
| 170 | - |
|
| 171 | - return $pcre_ok == 1; |
|
| 160 | + static $pcre_ok = 0; |
|
| 161 | + |
|
| 162 | + if (!$pcre_ok) { |
|
| 163 | + $s = ' ' . chr(195) . chr(169) . 't' . chr(195) . chr(169) . ' '; |
|
| 164 | + if (preg_match(',\W...\W,u', $s)) { |
|
| 165 | + $pcre_ok = 1; |
|
| 166 | + } else { |
|
| 167 | + $pcre_ok = -1; |
|
| 168 | + } |
|
| 169 | + } |
|
| 170 | + |
|
| 171 | + return $pcre_ok == 1; |
|
| 172 | 172 | } |
| 173 | 173 | |
| 174 | 174 | /** |
@@ -184,22 +184,22 @@ discard block |
||
| 184 | 184 | * Plage de caractères |
| 185 | 185 | **/ |
| 186 | 186 | function pcre_lettres_unicode() { |
| 187 | - static $plage_unicode; |
|
| 188 | - |
|
| 189 | - if (!$plage_unicode) { |
|
| 190 | - if (test_pcre_unicode()) { |
|
| 191 | - // cf. http://www.unicode.org/charts/ |
|
| 192 | - $plage_unicode = '\w' // iso-latin |
|
| 193 | - . '\x{100}-\x{24f}' // europeen etendu |
|
| 194 | - . '\x{300}-\x{1cff}' // des tas de trucs |
|
| 195 | - ; |
|
| 196 | - } else { |
|
| 197 | - // fallback a trois sous |
|
| 198 | - $plage_unicode = '\w'; |
|
| 199 | - } |
|
| 200 | - } |
|
| 201 | - |
|
| 202 | - return $plage_unicode; |
|
| 187 | + static $plage_unicode; |
|
| 188 | + |
|
| 189 | + if (!$plage_unicode) { |
|
| 190 | + if (test_pcre_unicode()) { |
|
| 191 | + // cf. http://www.unicode.org/charts/ |
|
| 192 | + $plage_unicode = '\w' // iso-latin |
|
| 193 | + . '\x{100}-\x{24f}' // europeen etendu |
|
| 194 | + . '\x{300}-\x{1cff}' // des tas de trucs |
|
| 195 | + ; |
|
| 196 | + } else { |
|
| 197 | + // fallback a trois sous |
|
| 198 | + $plage_unicode = '\w'; |
|
| 199 | + } |
|
| 200 | + } |
|
| 201 | + |
|
| 202 | + return $plage_unicode; |
|
| 203 | 203 | } |
| 204 | 204 | |
| 205 | 205 | |
@@ -217,7 +217,7 @@ discard block |
||
| 217 | 217 | * Plage de caractères |
| 218 | 218 | **/ |
| 219 | 219 | function plage_punct_unicode() { |
| 220 | - return '\xE2(\x80[\x80-\xBF]|\x81[\x80-\xAF])'; |
|
| 220 | + return '\xE2(\x80[\x80-\xBF]|\x81[\x80-\xAF])'; |
|
| 221 | 221 | } |
| 222 | 222 | |
| 223 | 223 | /** |
@@ -237,75 +237,75 @@ discard block |
||
| 237 | 237 | * Texte corrigé |
| 238 | 238 | **/ |
| 239 | 239 | function corriger_caracteres_windows($texte, $charset = 'AUTO', $charset_cible = 'unicode') { |
| 240 | - static $trans; |
|
| 241 | - |
|
| 242 | - if (is_array($texte)) { |
|
| 243 | - return array_map('corriger_caracteres_windows', $texte); |
|
| 244 | - } |
|
| 245 | - |
|
| 246 | - if ($charset == 'AUTO') { |
|
| 247 | - $charset = lire_config('charset', _DEFAULT_CHARSET); |
|
| 248 | - } |
|
| 249 | - if ($charset == 'utf-8') { |
|
| 250 | - $p = chr(194); |
|
| 251 | - if (strpos($texte, $p) == false) { |
|
| 252 | - return $texte; |
|
| 253 | - } |
|
| 254 | - } else { |
|
| 255 | - if ($charset == 'iso-8859-1') { |
|
| 256 | - $p = ''; |
|
| 257 | - } else { |
|
| 258 | - return $texte; |
|
| 259 | - } |
|
| 260 | - } |
|
| 261 | - |
|
| 262 | - if (!isset($trans[$charset][$charset_cible])) { |
|
| 263 | - $trans[$charset][$charset_cible] = [ |
|
| 264 | - $p . chr(128) => '€', |
|
| 265 | - $p . chr(129) => ' ', # pas affecte |
|
| 266 | - $p . chr(130) => '‚', |
|
| 267 | - $p . chr(131) => 'ƒ', |
|
| 268 | - $p . chr(132) => '„', |
|
| 269 | - $p . chr(133) => '…', |
|
| 270 | - $p . chr(134) => '†', |
|
| 271 | - $p . chr(135) => '‡', |
|
| 272 | - $p . chr(136) => 'ˆ', |
|
| 273 | - $p . chr(137) => '‰', |
|
| 274 | - $p . chr(138) => 'Š', |
|
| 275 | - $p . chr(139) => '‹', |
|
| 276 | - $p . chr(140) => 'Œ', |
|
| 277 | - $p . chr(141) => ' ', # pas affecte |
|
| 278 | - $p . chr(142) => 'Ž', |
|
| 279 | - $p . chr(143) => ' ', # pas affecte |
|
| 280 | - $p . chr(144) => ' ', # pas affecte |
|
| 281 | - $p . chr(145) => '‘', |
|
| 282 | - $p . chr(146) => '’', |
|
| 283 | - $p . chr(147) => '“', |
|
| 284 | - $p . chr(148) => '”', |
|
| 285 | - $p . chr(149) => '•', |
|
| 286 | - $p . chr(150) => '–', |
|
| 287 | - $p . chr(151) => '—', |
|
| 288 | - $p . chr(152) => '˜', |
|
| 289 | - $p . chr(153) => '™', |
|
| 290 | - $p . chr(154) => 'š', |
|
| 291 | - $p . chr(155) => '›', |
|
| 292 | - $p . chr(156) => 'œ', |
|
| 293 | - $p . chr(157) => ' ', # pas affecte |
|
| 294 | - $p . chr(158) => 'ž', |
|
| 295 | - $p . chr(159) => 'Ÿ', |
|
| 296 | - ]; |
|
| 297 | - if ($charset_cible != 'unicode') { |
|
| 298 | - foreach ($trans[$charset][$charset_cible] as $k => $c) { |
|
| 299 | - $trans[$charset][$charset_cible][$k] = unicode2charset($c, $charset_cible); |
|
| 300 | - } |
|
| 301 | - } |
|
| 302 | - } |
|
| 303 | - |
|
| 304 | - return @str_replace( |
|
| 305 | - array_keys($trans[$charset][$charset_cible]), |
|
| 306 | - array_values($trans[$charset][$charset_cible]), |
|
| 307 | - $texte |
|
| 308 | - ); |
|
| 240 | + static $trans; |
|
| 241 | + |
|
| 242 | + if (is_array($texte)) { |
|
| 243 | + return array_map('corriger_caracteres_windows', $texte); |
|
| 244 | + } |
|
| 245 | + |
|
| 246 | + if ($charset == 'AUTO') { |
|
| 247 | + $charset = lire_config('charset', _DEFAULT_CHARSET); |
|
| 248 | + } |
|
| 249 | + if ($charset == 'utf-8') { |
|
| 250 | + $p = chr(194); |
|
| 251 | + if (strpos($texte, $p) == false) { |
|
| 252 | + return $texte; |
|
| 253 | + } |
|
| 254 | + } else { |
|
| 255 | + if ($charset == 'iso-8859-1') { |
|
| 256 | + $p = ''; |
|
| 257 | + } else { |
|
| 258 | + return $texte; |
|
| 259 | + } |
|
| 260 | + } |
|
| 261 | + |
|
| 262 | + if (!isset($trans[$charset][$charset_cible])) { |
|
| 263 | + $trans[$charset][$charset_cible] = [ |
|
| 264 | + $p . chr(128) => '€', |
|
| 265 | + $p . chr(129) => ' ', # pas affecte |
|
| 266 | + $p . chr(130) => '‚', |
|
| 267 | + $p . chr(131) => 'ƒ', |
|
| 268 | + $p . chr(132) => '„', |
|
| 269 | + $p . chr(133) => '…', |
|
| 270 | + $p . chr(134) => '†', |
|
| 271 | + $p . chr(135) => '‡', |
|
| 272 | + $p . chr(136) => 'ˆ', |
|
| 273 | + $p . chr(137) => '‰', |
|
| 274 | + $p . chr(138) => 'Š', |
|
| 275 | + $p . chr(139) => '‹', |
|
| 276 | + $p . chr(140) => 'Œ', |
|
| 277 | + $p . chr(141) => ' ', # pas affecte |
|
| 278 | + $p . chr(142) => 'Ž', |
|
| 279 | + $p . chr(143) => ' ', # pas affecte |
|
| 280 | + $p . chr(144) => ' ', # pas affecte |
|
| 281 | + $p . chr(145) => '‘', |
|
| 282 | + $p . chr(146) => '’', |
|
| 283 | + $p . chr(147) => '“', |
|
| 284 | + $p . chr(148) => '”', |
|
| 285 | + $p . chr(149) => '•', |
|
| 286 | + $p . chr(150) => '–', |
|
| 287 | + $p . chr(151) => '—', |
|
| 288 | + $p . chr(152) => '˜', |
|
| 289 | + $p . chr(153) => '™', |
|
| 290 | + $p . chr(154) => 'š', |
|
| 291 | + $p . chr(155) => '›', |
|
| 292 | + $p . chr(156) => 'œ', |
|
| 293 | + $p . chr(157) => ' ', # pas affecte |
|
| 294 | + $p . chr(158) => 'ž', |
|
| 295 | + $p . chr(159) => 'Ÿ', |
|
| 296 | + ]; |
|
| 297 | + if ($charset_cible != 'unicode') { |
|
| 298 | + foreach ($trans[$charset][$charset_cible] as $k => $c) { |
|
| 299 | + $trans[$charset][$charset_cible][$k] = unicode2charset($c, $charset_cible); |
|
| 300 | + } |
|
| 301 | + } |
|
| 302 | + } |
|
| 303 | + |
|
| 304 | + return @str_replace( |
|
| 305 | + array_keys($trans[$charset][$charset_cible]), |
|
| 306 | + array_values($trans[$charset][$charset_cible]), |
|
| 307 | + $texte |
|
| 308 | + ); |
|
| 309 | 309 | } |
| 310 | 310 | |
| 311 | 311 | |
@@ -322,26 +322,26 @@ discard block |
||
| 322 | 322 | * Texte converti |
| 323 | 323 | **/ |
| 324 | 324 | function html2unicode($texte, $secure = false) { |
| 325 | - if (strpos($texte, '&') === false) { |
|
| 326 | - return $texte; |
|
| 327 | - } |
|
| 328 | - static $trans = []; |
|
| 329 | - if (!$trans) { |
|
| 330 | - load_charset('html'); |
|
| 331 | - foreach ($GLOBALS['CHARSET']['html'] as $key => $val) { |
|
| 332 | - $trans["&$key;"] = $val; |
|
| 333 | - } |
|
| 334 | - } |
|
| 335 | - |
|
| 336 | - if ($secure) { |
|
| 337 | - return str_replace(array_keys($trans), array_values($trans), $texte); |
|
| 338 | - } else { |
|
| 339 | - return str_replace( |
|
| 340 | - ['&', '"', '<', '>'], |
|
| 341 | - ['&', '"', '<', '>'], |
|
| 342 | - str_replace(array_keys($trans), array_values($trans), $texte) |
|
| 343 | - ); |
|
| 344 | - } |
|
| 325 | + if (strpos($texte, '&') === false) { |
|
| 326 | + return $texte; |
|
| 327 | + } |
|
| 328 | + static $trans = []; |
|
| 329 | + if (!$trans) { |
|
| 330 | + load_charset('html'); |
|
| 331 | + foreach ($GLOBALS['CHARSET']['html'] as $key => $val) { |
|
| 332 | + $trans["&$key;"] = $val; |
|
| 333 | + } |
|
| 334 | + } |
|
| 335 | + |
|
| 336 | + if ($secure) { |
|
| 337 | + return str_replace(array_keys($trans), array_values($trans), $texte); |
|
| 338 | + } else { |
|
| 339 | + return str_replace( |
|
| 340 | + ['&', '"', '<', '>'], |
|
| 341 | + ['&', '"', '<', '>'], |
|
| 342 | + str_replace(array_keys($trans), array_values($trans), $texte) |
|
| 343 | + ); |
|
| 344 | + } |
|
| 345 | 345 | } |
| 346 | 346 | |
| 347 | 347 | |
@@ -356,16 +356,16 @@ discard block |
||
| 356 | 356 | * Texte converti |
| 357 | 357 | **/ |
| 358 | 358 | function mathml2unicode($texte) { |
| 359 | - static $trans; |
|
| 360 | - if (!$trans) { |
|
| 361 | - load_charset('mathml'); |
|
| 359 | + static $trans; |
|
| 360 | + if (!$trans) { |
|
| 361 | + load_charset('mathml'); |
|
| 362 | 362 | |
| 363 | - foreach ($GLOBALS['CHARSET']['mathml'] as $key => $val) { |
|
| 364 | - $trans["&$key;"] = $val; |
|
| 365 | - } |
|
| 366 | - } |
|
| 363 | + foreach ($GLOBALS['CHARSET']['mathml'] as $key => $val) { |
|
| 364 | + $trans["&$key;"] = $val; |
|
| 365 | + } |
|
| 366 | + } |
|
| 367 | 367 | |
| 368 | - return str_replace(array_keys($trans), array_values($trans), $texte); |
|
| 368 | + return str_replace(array_keys($trans), array_values($trans), $texte); |
|
| 369 | 369 | } |
| 370 | 370 | |
| 371 | 371 | |
@@ -387,75 +387,75 @@ discard block |
||
| 387 | 387 | * Texte converti en unicode |
| 388 | 388 | **/ |
| 389 | 389 | function charset2unicode($texte, $charset = 'AUTO' /* $forcer: obsolete*/) { |
| 390 | - static $trans; |
|
| 391 | - |
|
| 392 | - if ($charset == 'AUTO') { |
|
| 393 | - $charset = lire_config('charset', _DEFAULT_CHARSET); |
|
| 394 | - } |
|
| 395 | - |
|
| 396 | - if ($charset == '') { |
|
| 397 | - $charset = 'iso-8859-1'; |
|
| 398 | - } |
|
| 399 | - $charset = strtolower($charset); |
|
| 400 | - |
|
| 401 | - switch ($charset) { |
|
| 402 | - case 'utf-8': |
|
| 403 | - case 'utf8': |
|
| 404 | - return utf_8_to_unicode($texte); |
|
| 405 | - |
|
| 406 | - case 'iso-8859-1': |
|
| 407 | - $texte = corriger_caracteres_windows($texte, 'iso-8859-1'); |
|
| 408 | - // pas de break; ici, on suit sur default: |
|
| 409 | - |
|
| 410 | - default: |
|
| 411 | - // mbstring presente ? |
|
| 412 | - if (init_mb_string()) { |
|
| 413 | - $order = mb_detect_order(); |
|
| 414 | - try { |
|
| 415 | - # mb_string connait-il $charset? |
|
| 416 | - if ($order and mb_detect_order($charset)) { |
|
| 417 | - $s = mb_convert_encoding($texte, 'utf-8', $charset); |
|
| 418 | - if ($s && $s != $texte) { |
|
| 419 | - return utf_8_to_unicode($s); |
|
| 420 | - } |
|
| 421 | - } |
|
| 422 | - } catch (\Error $e) { |
|
| 423 | - // Le charset n'existe probablement pas |
|
| 424 | - } finally { |
|
| 425 | - mb_detect_order($order); # remettre comme precedemment |
|
| 426 | - } |
|
| 427 | - } |
|
| 428 | - |
|
| 429 | - // Sinon, peut-etre connaissons-nous ce charset ? |
|
| 430 | - if (!isset($trans[$charset])) { |
|
| 431 | - if ( |
|
| 432 | - $cset = load_charset($charset) |
|
| 433 | - and is_array($GLOBALS['CHARSET'][$cset]) |
|
| 434 | - ) { |
|
| 435 | - foreach ($GLOBALS['CHARSET'][$cset] as $key => $val) { |
|
| 436 | - $trans[$charset][chr($key)] = '&#' . $val . ';'; |
|
| 437 | - } |
|
| 438 | - } |
|
| 439 | - } |
|
| 440 | - if (isset($trans[$charset]) and is_countable($trans[$charset]) ? count($trans[$charset]) : 0) { |
|
| 441 | - return str_replace(array_keys($trans[$charset]), array_values($trans[$charset]), $texte); |
|
| 442 | - } |
|
| 443 | - |
|
| 444 | - // Sinon demander a iconv (malgre le fait qu'il coupe quand un |
|
| 445 | - // caractere n'appartient pas au charset, mais c'est un probleme |
|
| 446 | - // surtout en utf-8, gere ci-dessus) |
|
| 447 | - if (test_iconv()) { |
|
| 448 | - $s = iconv($charset, 'utf-32le', $texte); |
|
| 449 | - if ($s) { |
|
| 450 | - return utf_32_to_unicode($s); |
|
| 451 | - } |
|
| 452 | - } |
|
| 453 | - |
|
| 454 | - // Au pire ne rien faire |
|
| 455 | - spip_log("erreur charset '$charset' non supporte"); |
|
| 456 | - |
|
| 457 | - return $texte; |
|
| 458 | - } |
|
| 390 | + static $trans; |
|
| 391 | + |
|
| 392 | + if ($charset == 'AUTO') { |
|
| 393 | + $charset = lire_config('charset', _DEFAULT_CHARSET); |
|
| 394 | + } |
|
| 395 | + |
|
| 396 | + if ($charset == '') { |
|
| 397 | + $charset = 'iso-8859-1'; |
|
| 398 | + } |
|
| 399 | + $charset = strtolower($charset); |
|
| 400 | + |
|
| 401 | + switch ($charset) { |
|
| 402 | + case 'utf-8': |
|
| 403 | + case 'utf8': |
|
| 404 | + return utf_8_to_unicode($texte); |
|
| 405 | + |
|
| 406 | + case 'iso-8859-1': |
|
| 407 | + $texte = corriger_caracteres_windows($texte, 'iso-8859-1'); |
|
| 408 | + // pas de break; ici, on suit sur default: |
|
| 409 | + |
|
| 410 | + default: |
|
| 411 | + // mbstring presente ? |
|
| 412 | + if (init_mb_string()) { |
|
| 413 | + $order = mb_detect_order(); |
|
| 414 | + try { |
|
| 415 | + # mb_string connait-il $charset? |
|
| 416 | + if ($order and mb_detect_order($charset)) { |
|
| 417 | + $s = mb_convert_encoding($texte, 'utf-8', $charset); |
|
| 418 | + if ($s && $s != $texte) { |
|
| 419 | + return utf_8_to_unicode($s); |
|
| 420 | + } |
|
| 421 | + } |
|
| 422 | + } catch (\Error $e) { |
|
| 423 | + // Le charset n'existe probablement pas |
|
| 424 | + } finally { |
|
| 425 | + mb_detect_order($order); # remettre comme precedemment |
|
| 426 | + } |
|
| 427 | + } |
|
| 428 | + |
|
| 429 | + // Sinon, peut-etre connaissons-nous ce charset ? |
|
| 430 | + if (!isset($trans[$charset])) { |
|
| 431 | + if ( |
|
| 432 | + $cset = load_charset($charset) |
|
| 433 | + and is_array($GLOBALS['CHARSET'][$cset]) |
|
| 434 | + ) { |
|
| 435 | + foreach ($GLOBALS['CHARSET'][$cset] as $key => $val) { |
|
| 436 | + $trans[$charset][chr($key)] = '&#' . $val . ';'; |
|
| 437 | + } |
|
| 438 | + } |
|
| 439 | + } |
|
| 440 | + if (isset($trans[$charset]) and is_countable($trans[$charset]) ? count($trans[$charset]) : 0) { |
|
| 441 | + return str_replace(array_keys($trans[$charset]), array_values($trans[$charset]), $texte); |
|
| 442 | + } |
|
| 443 | + |
|
| 444 | + // Sinon demander a iconv (malgre le fait qu'il coupe quand un |
|
| 445 | + // caractere n'appartient pas au charset, mais c'est un probleme |
|
| 446 | + // surtout en utf-8, gere ci-dessus) |
|
| 447 | + if (test_iconv()) { |
|
| 448 | + $s = iconv($charset, 'utf-32le', $texte); |
|
| 449 | + if ($s) { |
|
| 450 | + return utf_32_to_unicode($s); |
|
| 451 | + } |
|
| 452 | + } |
|
| 453 | + |
|
| 454 | + // Au pire ne rien faire |
|
| 455 | + spip_log("erreur charset '$charset' non supporte"); |
|
| 456 | + |
|
| 457 | + return $texte; |
|
| 458 | + } |
|
| 459 | 459 | } |
| 460 | 460 | |
| 461 | 461 | |
@@ -474,44 +474,44 @@ discard block |
||
| 474 | 474 | * Texte transformé dans le charset souhaité |
| 475 | 475 | **/ |
| 476 | 476 | function unicode2charset($texte, $charset = 'AUTO') { |
| 477 | - static $CHARSET_REVERSE = []; |
|
| 478 | - static $trans = []; |
|
| 479 | - |
|
| 480 | - if ($charset == 'AUTO') { |
|
| 481 | - $charset = lire_config('charset', _DEFAULT_CHARSET); |
|
| 482 | - } |
|
| 483 | - |
|
| 484 | - switch ($charset) { |
|
| 485 | - case 'utf-8': |
|
| 486 | - return unicode_to_utf_8($texte); |
|
| 487 | - break; |
|
| 488 | - |
|
| 489 | - default: |
|
| 490 | - $charset = load_charset($charset); |
|
| 491 | - |
|
| 492 | - if (empty($CHARSET_REVERSE[$charset])) { |
|
| 493 | - $CHARSET_REVERSE[$charset] = array_flip($GLOBALS['CHARSET'][$charset]); |
|
| 494 | - } |
|
| 495 | - |
|
| 496 | - if (!isset($trans[$charset])) { |
|
| 497 | - $trans[$charset] = []; |
|
| 498 | - $t = &$trans[$charset]; |
|
| 499 | - for ($e = 128; $e < 255; $e++) { |
|
| 500 | - $h = dechex($e); |
|
| 501 | - if ($s = isset($CHARSET_REVERSE[$charset][$e])) { |
|
| 502 | - $s = $CHARSET_REVERSE[$charset][$e]; |
|
| 503 | - $t['&#' . $e . ';'] = $t['�' . $e . ';'] = $t['�' . $e . ';'] = chr($s); |
|
| 504 | - $t['&#x' . $h . ';'] = $t['�' . $h . ';'] = $t['�' . $h . ';'] = chr($s); |
|
| 505 | - } else { |
|
| 506 | - $t['&#' . $e . ';'] = $t['�' . $e . ';'] = $t['�' . $e . ';'] = chr($e); |
|
| 507 | - $t['&#x' . $h . ';'] = $t['�' . $h . ';'] = $t['�' . $h . ';'] = chr($e); |
|
| 508 | - } |
|
| 509 | - } |
|
| 510 | - } |
|
| 511 | - $texte = str_replace(array_keys($trans[$charset]), array_values($trans[$charset]), $texte); |
|
| 512 | - |
|
| 513 | - return $texte; |
|
| 514 | - } |
|
| 477 | + static $CHARSET_REVERSE = []; |
|
| 478 | + static $trans = []; |
|
| 479 | + |
|
| 480 | + if ($charset == 'AUTO') { |
|
| 481 | + $charset = lire_config('charset', _DEFAULT_CHARSET); |
|
| 482 | + } |
|
| 483 | + |
|
| 484 | + switch ($charset) { |
|
| 485 | + case 'utf-8': |
|
| 486 | + return unicode_to_utf_8($texte); |
|
| 487 | + break; |
|
| 488 | + |
|
| 489 | + default: |
|
| 490 | + $charset = load_charset($charset); |
|
| 491 | + |
|
| 492 | + if (empty($CHARSET_REVERSE[$charset])) { |
|
| 493 | + $CHARSET_REVERSE[$charset] = array_flip($GLOBALS['CHARSET'][$charset]); |
|
| 494 | + } |
|
| 495 | + |
|
| 496 | + if (!isset($trans[$charset])) { |
|
| 497 | + $trans[$charset] = []; |
|
| 498 | + $t = &$trans[$charset]; |
|
| 499 | + for ($e = 128; $e < 255; $e++) { |
|
| 500 | + $h = dechex($e); |
|
| 501 | + if ($s = isset($CHARSET_REVERSE[$charset][$e])) { |
|
| 502 | + $s = $CHARSET_REVERSE[$charset][$e]; |
|
| 503 | + $t['&#' . $e . ';'] = $t['�' . $e . ';'] = $t['�' . $e . ';'] = chr($s); |
|
| 504 | + $t['&#x' . $h . ';'] = $t['�' . $h . ';'] = $t['�' . $h . ';'] = chr($s); |
|
| 505 | + } else { |
|
| 506 | + $t['&#' . $e . ';'] = $t['�' . $e . ';'] = $t['�' . $e . ';'] = chr($e); |
|
| 507 | + $t['&#x' . $h . ';'] = $t['�' . $h . ';'] = $t['�' . $h . ';'] = chr($e); |
|
| 508 | + } |
|
| 509 | + } |
|
| 510 | + } |
|
| 511 | + $texte = str_replace(array_keys($trans[$charset]), array_values($trans[$charset]), $texte); |
|
| 512 | + |
|
| 513 | + return $texte; |
|
| 514 | + } |
|
| 515 | 515 | } |
| 516 | 516 | |
| 517 | 517 | |
@@ -529,40 +529,40 @@ discard block |
||
| 529 | 529 | * Texte transformé dans le charset site |
| 530 | 530 | **/ |
| 531 | 531 | function importer_charset($texte, $charset = 'AUTO') { |
| 532 | - $s = null; |
|
| 533 | - static $trans = []; |
|
| 534 | - // on traite le cas le plus frequent iso-8859-1 vers utf directement pour aller plus vite ! |
|
| 535 | - if (($charset == 'iso-8859-1') && ($GLOBALS['meta']['charset'] == 'utf-8')) { |
|
| 536 | - $texte = corriger_caracteres_windows($texte, 'iso-8859-1', $GLOBALS['meta']['charset']); |
|
| 537 | - if (init_mb_string()) { |
|
| 538 | - if ( |
|
| 539 | - $order = mb_detect_order() # mb_string connait-il $charset? |
|
| 540 | - and mb_detect_order($charset) |
|
| 541 | - ) { |
|
| 542 | - $s = mb_convert_encoding($texte, 'utf-8', $charset); |
|
| 543 | - } |
|
| 544 | - mb_detect_order($order); # remettre comme precedemment |
|
| 545 | - return $s; |
|
| 546 | - } |
|
| 547 | - // Sinon, peut-etre connaissons-nous ce charset ? |
|
| 548 | - if (!isset($trans[$charset])) { |
|
| 549 | - if ( |
|
| 550 | - $cset = load_charset($charset) |
|
| 551 | - and is_array($GLOBALS['CHARSET'][$cset]) |
|
| 552 | - ) { |
|
| 553 | - foreach ($GLOBALS['CHARSET'][$cset] as $key => $val) { |
|
| 554 | - $trans[$charset][chr($key)] = unicode2charset('&#' . $val . ';'); |
|
| 555 | - } |
|
| 556 | - } |
|
| 557 | - } |
|
| 558 | - if (is_countable($trans[$charset]) ? count($trans[$charset]) : 0) { |
|
| 559 | - return str_replace(array_keys($trans[$charset]), array_values($trans[$charset]), $texte); |
|
| 560 | - } |
|
| 561 | - |
|
| 562 | - return $texte; |
|
| 563 | - } |
|
| 564 | - |
|
| 565 | - return unicode2charset(charset2unicode($texte, $charset)); |
|
| 532 | + $s = null; |
|
| 533 | + static $trans = []; |
|
| 534 | + // on traite le cas le plus frequent iso-8859-1 vers utf directement pour aller plus vite ! |
|
| 535 | + if (($charset == 'iso-8859-1') && ($GLOBALS['meta']['charset'] == 'utf-8')) { |
|
| 536 | + $texte = corriger_caracteres_windows($texte, 'iso-8859-1', $GLOBALS['meta']['charset']); |
|
| 537 | + if (init_mb_string()) { |
|
| 538 | + if ( |
|
| 539 | + $order = mb_detect_order() # mb_string connait-il $charset? |
|
| 540 | + and mb_detect_order($charset) |
|
| 541 | + ) { |
|
| 542 | + $s = mb_convert_encoding($texte, 'utf-8', $charset); |
|
| 543 | + } |
|
| 544 | + mb_detect_order($order); # remettre comme precedemment |
|
| 545 | + return $s; |
|
| 546 | + } |
|
| 547 | + // Sinon, peut-etre connaissons-nous ce charset ? |
|
| 548 | + if (!isset($trans[$charset])) { |
|
| 549 | + if ( |
|
| 550 | + $cset = load_charset($charset) |
|
| 551 | + and is_array($GLOBALS['CHARSET'][$cset]) |
|
| 552 | + ) { |
|
| 553 | + foreach ($GLOBALS['CHARSET'][$cset] as $key => $val) { |
|
| 554 | + $trans[$charset][chr($key)] = unicode2charset('&#' . $val . ';'); |
|
| 555 | + } |
|
| 556 | + } |
|
| 557 | + } |
|
| 558 | + if (is_countable($trans[$charset]) ? count($trans[$charset]) : 0) { |
|
| 559 | + return str_replace(array_keys($trans[$charset]), array_values($trans[$charset]), $texte); |
|
| 560 | + } |
|
| 561 | + |
|
| 562 | + return $texte; |
|
| 563 | + } |
|
| 564 | + |
|
| 565 | + return unicode2charset(charset2unicode($texte, $charset)); |
|
| 566 | 566 | } |
| 567 | 567 | |
| 568 | 568 | |
@@ -578,92 +578,92 @@ discard block |
||
| 578 | 578 | **/ |
| 579 | 579 | function utf_8_to_unicode($source) { |
| 580 | 580 | |
| 581 | - // mb_string : methode rapide |
|
| 582 | - if (init_mb_string()) { |
|
| 583 | - $convmap = [0x7F, 0xFFFFFF, 0x0, 0xFFFFFF]; |
|
| 584 | - |
|
| 585 | - return mb_encode_numericentity($source, $convmap, 'UTF-8'); |
|
| 586 | - } |
|
| 587 | - |
|
| 588 | - // Sinon methode pas a pas |
|
| 589 | - static $decrement; |
|
| 590 | - static $shift; |
|
| 591 | - |
|
| 592 | - // Cf. php.net, par Ronen. Adapte pour compatibilite < php4 |
|
| 593 | - if (!is_array($decrement)) { |
|
| 594 | - // array used to figure what number to decrement from character order value |
|
| 595 | - // according to number of characters used to map unicode to ascii by utf-8 |
|
| 596 | - $decrement[4] = 240; |
|
| 597 | - $decrement[3] = 224; |
|
| 598 | - $decrement[2] = 192; |
|
| 599 | - $decrement[1] = 0; |
|
| 600 | - // the number of bits to shift each charNum by |
|
| 601 | - $shift[1][0] = 0; |
|
| 602 | - $shift[2][0] = 6; |
|
| 603 | - $shift[2][1] = 0; |
|
| 604 | - $shift[3][0] = 12; |
|
| 605 | - $shift[3][1] = 6; |
|
| 606 | - $shift[3][2] = 0; |
|
| 607 | - $shift[4][0] = 18; |
|
| 608 | - $shift[4][1] = 12; |
|
| 609 | - $shift[4][2] = 6; |
|
| 610 | - $shift[4][3] = 0; |
|
| 611 | - } |
|
| 612 | - |
|
| 613 | - $pos = 0; |
|
| 614 | - $len = strlen($source); |
|
| 615 | - $encodedString = ''; |
|
| 616 | - while ($pos < $len) { |
|
| 617 | - $char = ''; |
|
| 618 | - $ischar = false; |
|
| 619 | - $asciiPos = ord(substr($source, $pos, 1)); |
|
| 620 | - if (($asciiPos >= 240) && ($asciiPos <= 255)) { |
|
| 621 | - // 4 chars representing one unicode character |
|
| 622 | - $thisLetter = substr($source, $pos, 4); |
|
| 623 | - $pos += 4; |
|
| 624 | - } else { |
|
| 625 | - if (($asciiPos >= 224) && ($asciiPos <= 239)) { |
|
| 626 | - // 3 chars representing one unicode character |
|
| 627 | - $thisLetter = substr($source, $pos, 3); |
|
| 628 | - $pos += 3; |
|
| 629 | - } else { |
|
| 630 | - if (($asciiPos >= 192) && ($asciiPos <= 223)) { |
|
| 631 | - // 2 chars representing one unicode character |
|
| 632 | - $thisLetter = substr($source, $pos, 2); |
|
| 633 | - $pos += 2; |
|
| 634 | - } else { |
|
| 635 | - // 1 char (lower ascii) |
|
| 636 | - $thisLetter = substr($source, $pos, 1); |
|
| 637 | - $pos += 1; |
|
| 638 | - $char = $thisLetter; |
|
| 639 | - $ischar = true; |
|
| 640 | - } |
|
| 641 | - } |
|
| 642 | - } |
|
| 643 | - |
|
| 644 | - if ($ischar) { |
|
| 645 | - $encodedString .= $char; |
|
| 646 | - } else { // process the string representing the letter to a unicode entity |
|
| 647 | - $thisLen = strlen($thisLetter); |
|
| 648 | - $thisPos = 0; |
|
| 649 | - $decimalCode = 0; |
|
| 650 | - while ($thisPos < $thisLen) { |
|
| 651 | - $thisCharOrd = ord(substr($thisLetter, $thisPos, 1)); |
|
| 652 | - if ($thisPos == 0) { |
|
| 653 | - $charNum = intval($thisCharOrd - $decrement[$thisLen]); |
|
| 654 | - $decimalCode += ($charNum << $shift[$thisLen][$thisPos]); |
|
| 655 | - } else { |
|
| 656 | - $charNum = intval($thisCharOrd - 128); |
|
| 657 | - $decimalCode += ($charNum << $shift[$thisLen][$thisPos]); |
|
| 658 | - } |
|
| 659 | - $thisPos++; |
|
| 660 | - } |
|
| 661 | - $encodedLetter = '&#' . preg_replace('/^0+/', '', $decimalCode) . ';'; |
|
| 662 | - $encodedString .= $encodedLetter; |
|
| 663 | - } |
|
| 664 | - } |
|
| 665 | - |
|
| 666 | - return $encodedString; |
|
| 581 | + // mb_string : methode rapide |
|
| 582 | + if (init_mb_string()) { |
|
| 583 | + $convmap = [0x7F, 0xFFFFFF, 0x0, 0xFFFFFF]; |
|
| 584 | + |
|
| 585 | + return mb_encode_numericentity($source, $convmap, 'UTF-8'); |
|
| 586 | + } |
|
| 587 | + |
|
| 588 | + // Sinon methode pas a pas |
|
| 589 | + static $decrement; |
|
| 590 | + static $shift; |
|
| 591 | + |
|
| 592 | + // Cf. php.net, par Ronen. Adapte pour compatibilite < php4 |
|
| 593 | + if (!is_array($decrement)) { |
|
| 594 | + // array used to figure what number to decrement from character order value |
|
| 595 | + // according to number of characters used to map unicode to ascii by utf-8 |
|
| 596 | + $decrement[4] = 240; |
|
| 597 | + $decrement[3] = 224; |
|
| 598 | + $decrement[2] = 192; |
|
| 599 | + $decrement[1] = 0; |
|
| 600 | + // the number of bits to shift each charNum by |
|
| 601 | + $shift[1][0] = 0; |
|
| 602 | + $shift[2][0] = 6; |
|
| 603 | + $shift[2][1] = 0; |
|
| 604 | + $shift[3][0] = 12; |
|
| 605 | + $shift[3][1] = 6; |
|
| 606 | + $shift[3][2] = 0; |
|
| 607 | + $shift[4][0] = 18; |
|
| 608 | + $shift[4][1] = 12; |
|
| 609 | + $shift[4][2] = 6; |
|
| 610 | + $shift[4][3] = 0; |
|
| 611 | + } |
|
| 612 | + |
|
| 613 | + $pos = 0; |
|
| 614 | + $len = strlen($source); |
|
| 615 | + $encodedString = ''; |
|
| 616 | + while ($pos < $len) { |
|
| 617 | + $char = ''; |
|
| 618 | + $ischar = false; |
|
| 619 | + $asciiPos = ord(substr($source, $pos, 1)); |
|
| 620 | + if (($asciiPos >= 240) && ($asciiPos <= 255)) { |
|
| 621 | + // 4 chars representing one unicode character |
|
| 622 | + $thisLetter = substr($source, $pos, 4); |
|
| 623 | + $pos += 4; |
|
| 624 | + } else { |
|
| 625 | + if (($asciiPos >= 224) && ($asciiPos <= 239)) { |
|
| 626 | + // 3 chars representing one unicode character |
|
| 627 | + $thisLetter = substr($source, $pos, 3); |
|
| 628 | + $pos += 3; |
|
| 629 | + } else { |
|
| 630 | + if (($asciiPos >= 192) && ($asciiPos <= 223)) { |
|
| 631 | + // 2 chars representing one unicode character |
|
| 632 | + $thisLetter = substr($source, $pos, 2); |
|
| 633 | + $pos += 2; |
|
| 634 | + } else { |
|
| 635 | + // 1 char (lower ascii) |
|
| 636 | + $thisLetter = substr($source, $pos, 1); |
|
| 637 | + $pos += 1; |
|
| 638 | + $char = $thisLetter; |
|
| 639 | + $ischar = true; |
|
| 640 | + } |
|
| 641 | + } |
|
| 642 | + } |
|
| 643 | + |
|
| 644 | + if ($ischar) { |
|
| 645 | + $encodedString .= $char; |
|
| 646 | + } else { // process the string representing the letter to a unicode entity |
|
| 647 | + $thisLen = strlen($thisLetter); |
|
| 648 | + $thisPos = 0; |
|
| 649 | + $decimalCode = 0; |
|
| 650 | + while ($thisPos < $thisLen) { |
|
| 651 | + $thisCharOrd = ord(substr($thisLetter, $thisPos, 1)); |
|
| 652 | + if ($thisPos == 0) { |
|
| 653 | + $charNum = intval($thisCharOrd - $decrement[$thisLen]); |
|
| 654 | + $decimalCode += ($charNum << $shift[$thisLen][$thisPos]); |
|
| 655 | + } else { |
|
| 656 | + $charNum = intval($thisCharOrd - 128); |
|
| 657 | + $decimalCode += ($charNum << $shift[$thisLen][$thisPos]); |
|
| 658 | + } |
|
| 659 | + $thisPos++; |
|
| 660 | + } |
|
| 661 | + $encodedLetter = '&#' . preg_replace('/^0+/', '', $decimalCode) . ';'; |
|
| 662 | + $encodedString .= $encodedLetter; |
|
| 663 | + } |
|
| 664 | + } |
|
| 665 | + |
|
| 666 | + return $encodedString; |
|
| 667 | 667 | } |
| 668 | 668 | |
| 669 | 669 | /** |
@@ -682,32 +682,32 @@ discard block |
||
| 682 | 682 | **/ |
| 683 | 683 | function utf_32_to_unicode($source) { |
| 684 | 684 | |
| 685 | - // mb_string : methode rapide |
|
| 686 | - if (init_mb_string()) { |
|
| 687 | - $convmap = [0x7F, 0xFFFFFF, 0x0, 0xFFFFFF]; |
|
| 688 | - $source = mb_encode_numericentity($source, $convmap, 'UTF-32LE'); |
|
| 689 | - |
|
| 690 | - return str_replace(chr(0), '', $source); |
|
| 691 | - } |
|
| 692 | - |
|
| 693 | - // Sinon methode lente |
|
| 694 | - $texte = ''; |
|
| 695 | - while ($source) { |
|
| 696 | - $words = unpack('V*', substr($source, 0, 1024)); |
|
| 697 | - $source = substr($source, 1024); |
|
| 698 | - foreach ($words as $word) { |
|
| 699 | - if ($word < 128) { |
|
| 700 | - $texte .= chr($word); |
|
| 701 | - } // ignorer le BOM - http://www.unicode.org/faq/utf_bom.html |
|
| 702 | - else { |
|
| 703 | - if ($word != 65279) { |
|
| 704 | - $texte .= '&#' . $word . ';'; |
|
| 705 | - } |
|
| 706 | - } |
|
| 707 | - } |
|
| 708 | - } |
|
| 709 | - |
|
| 710 | - return $texte; |
|
| 685 | + // mb_string : methode rapide |
|
| 686 | + if (init_mb_string()) { |
|
| 687 | + $convmap = [0x7F, 0xFFFFFF, 0x0, 0xFFFFFF]; |
|
| 688 | + $source = mb_encode_numericentity($source, $convmap, 'UTF-32LE'); |
|
| 689 | + |
|
| 690 | + return str_replace(chr(0), '', $source); |
|
| 691 | + } |
|
| 692 | + |
|
| 693 | + // Sinon methode lente |
|
| 694 | + $texte = ''; |
|
| 695 | + while ($source) { |
|
| 696 | + $words = unpack('V*', substr($source, 0, 1024)); |
|
| 697 | + $source = substr($source, 1024); |
|
| 698 | + foreach ($words as $word) { |
|
| 699 | + if ($word < 128) { |
|
| 700 | + $texte .= chr($word); |
|
| 701 | + } // ignorer le BOM - http://www.unicode.org/faq/utf_bom.html |
|
| 702 | + else { |
|
| 703 | + if ($word != 65279) { |
|
| 704 | + $texte .= '&#' . $word . ';'; |
|
| 705 | + } |
|
| 706 | + } |
|
| 707 | + } |
|
| 708 | + } |
|
| 709 | + |
|
| 710 | + return $texte; |
|
| 711 | 711 | } |
| 712 | 712 | |
| 713 | 713 | |
@@ -724,21 +724,21 @@ discard block |
||
| 724 | 724 | * Caractère utf8 si trouvé, '' sinon |
| 725 | 725 | **/ |
| 726 | 726 | function caractere_utf_8($num) { |
| 727 | - $num = intval($num); |
|
| 728 | - if ($num < 128) { |
|
| 729 | - return chr($num); |
|
| 730 | - } |
|
| 731 | - if ($num < 2048) { |
|
| 732 | - return chr(($num >> 6) + 192) . chr(($num & 63) + 128); |
|
| 733 | - } |
|
| 734 | - if ($num < 65536) { |
|
| 735 | - return chr(($num >> 12) + 224) . chr((($num >> 6) & 63) + 128) . chr(($num & 63) + 128); |
|
| 736 | - } |
|
| 737 | - if ($num < 1_114_112) { |
|
| 738 | - return chr(($num >> 18) + 240) . chr((($num >> 12) & 63) + 128) . chr((($num >> 6) & 63) + 128) . chr(($num & 63) + 128); |
|
| 739 | - } |
|
| 740 | - |
|
| 741 | - return ''; |
|
| 727 | + $num = intval($num); |
|
| 728 | + if ($num < 128) { |
|
| 729 | + return chr($num); |
|
| 730 | + } |
|
| 731 | + if ($num < 2048) { |
|
| 732 | + return chr(($num >> 6) + 192) . chr(($num & 63) + 128); |
|
| 733 | + } |
|
| 734 | + if ($num < 65536) { |
|
| 735 | + return chr(($num >> 12) + 224) . chr((($num >> 6) & 63) + 128) . chr(($num & 63) + 128); |
|
| 736 | + } |
|
| 737 | + if ($num < 1_114_112) { |
|
| 738 | + return chr(($num >> 18) + 240) . chr((($num >> 12) & 63) + 128) . chr((($num >> 6) & 63) + 128) . chr(($num & 63) + 128); |
|
| 739 | + } |
|
| 740 | + |
|
| 741 | + return ''; |
|
| 742 | 742 | } |
| 743 | 743 | |
| 744 | 744 | /** |
@@ -751,42 +751,42 @@ discard block |
||
| 751 | 751 | **/ |
| 752 | 752 | function unicode_to_utf_8($texte) { |
| 753 | 753 | |
| 754 | - // 1. Entites € et suivantes |
|
| 755 | - $vu = []; |
|
| 756 | - if ( |
|
| 757 | - preg_match_all( |
|
| 758 | - ',�*([1-9][0-9][0-9]+);,S', |
|
| 759 | - $texte, |
|
| 760 | - $regs, |
|
| 761 | - PREG_SET_ORDER |
|
| 762 | - ) |
|
| 763 | - ) { |
|
| 764 | - foreach ($regs as $reg) { |
|
| 765 | - if ($reg[1] > 127 and !isset($vu[$reg[0]])) { |
|
| 766 | - $vu[$reg[0]] = caractere_utf_8($reg[1]); |
|
| 767 | - } |
|
| 768 | - } |
|
| 769 | - } |
|
| 770 | - //$texte = str_replace(array_keys($vu), array_values($vu), $texte); |
|
| 771 | - |
|
| 772 | - // 2. Entites > ÿ |
|
| 773 | - //$vu = array(); |
|
| 774 | - if ( |
|
| 775 | - preg_match_all( |
|
| 776 | - ',�*([1-9a-f][0-9a-f][0-9a-f]+);,iS', |
|
| 777 | - $texte, |
|
| 778 | - $regs, |
|
| 779 | - PREG_SET_ORDER |
|
| 780 | - ) |
|
| 781 | - ) { |
|
| 782 | - foreach ($regs as $reg) { |
|
| 783 | - if (!isset($vu[$reg[0]])) { |
|
| 784 | - $vu[$reg[0]] = caractere_utf_8(hexdec($reg[1])); |
|
| 785 | - } |
|
| 786 | - } |
|
| 787 | - } |
|
| 788 | - |
|
| 789 | - return str_replace(array_keys($vu), array_values($vu), $texte); |
|
| 754 | + // 1. Entites € et suivantes |
|
| 755 | + $vu = []; |
|
| 756 | + if ( |
|
| 757 | + preg_match_all( |
|
| 758 | + ',�*([1-9][0-9][0-9]+);,S', |
|
| 759 | + $texte, |
|
| 760 | + $regs, |
|
| 761 | + PREG_SET_ORDER |
|
| 762 | + ) |
|
| 763 | + ) { |
|
| 764 | + foreach ($regs as $reg) { |
|
| 765 | + if ($reg[1] > 127 and !isset($vu[$reg[0]])) { |
|
| 766 | + $vu[$reg[0]] = caractere_utf_8($reg[1]); |
|
| 767 | + } |
|
| 768 | + } |
|
| 769 | + } |
|
| 770 | + //$texte = str_replace(array_keys($vu), array_values($vu), $texte); |
|
| 771 | + |
|
| 772 | + // 2. Entites > ÿ |
|
| 773 | + //$vu = array(); |
|
| 774 | + if ( |
|
| 775 | + preg_match_all( |
|
| 776 | + ',�*([1-9a-f][0-9a-f][0-9a-f]+);,iS', |
|
| 777 | + $texte, |
|
| 778 | + $regs, |
|
| 779 | + PREG_SET_ORDER |
|
| 780 | + ) |
|
| 781 | + ) { |
|
| 782 | + foreach ($regs as $reg) { |
|
| 783 | + if (!isset($vu[$reg[0]])) { |
|
| 784 | + $vu[$reg[0]] = caractere_utf_8(hexdec($reg[1])); |
|
| 785 | + } |
|
| 786 | + } |
|
| 787 | + } |
|
| 788 | + |
|
| 789 | + return str_replace(array_keys($vu), array_values($vu), $texte); |
|
| 790 | 790 | } |
| 791 | 791 | |
| 792 | 792 | /** |
@@ -798,15 +798,15 @@ discard block |
||
| 798 | 798 | * Texte converti |
| 799 | 799 | **/ |
| 800 | 800 | function unicode_to_javascript($texte) { |
| 801 | - $vu = []; |
|
| 802 | - while (preg_match(',�*([0-9]+);,S', $texte, $regs) and !isset($vu[$regs[1]])) { |
|
| 803 | - $num = $regs[1]; |
|
| 804 | - $vu[$num] = true; |
|
| 805 | - $s = '\u' . sprintf('%04x', $num); |
|
| 806 | - $texte = str_replace($regs[0], $s, $texte); |
|
| 807 | - } |
|
| 808 | - |
|
| 809 | - return $texte; |
|
| 801 | + $vu = []; |
|
| 802 | + while (preg_match(',�*([0-9]+);,S', $texte, $regs) and !isset($vu[$regs[1]])) { |
|
| 803 | + $num = $regs[1]; |
|
| 804 | + $vu[$num] = true; |
|
| 805 | + $s = '\u' . sprintf('%04x', $num); |
|
| 806 | + $texte = str_replace($regs[0], $s, $texte); |
|
| 807 | + } |
|
| 808 | + |
|
| 809 | + return $texte; |
|
| 810 | 810 | } |
| 811 | 811 | |
| 812 | 812 | /** |
@@ -818,11 +818,11 @@ discard block |
||
| 818 | 818 | * Texte converti |
| 819 | 819 | **/ |
| 820 | 820 | function javascript_to_unicode($texte) { |
| 821 | - while (preg_match(',%u([0-9A-F][0-9A-F][0-9A-F][0-9A-F]),', $texte, $regs)) { |
|
| 822 | - $texte = str_replace($regs[0], '&#' . hexdec($regs[1]) . ';', $texte); |
|
| 823 | - } |
|
| 821 | + while (preg_match(',%u([0-9A-F][0-9A-F][0-9A-F][0-9A-F]),', $texte, $regs)) { |
|
| 822 | + $texte = str_replace($regs[0], '&#' . hexdec($regs[1]) . ';', $texte); |
|
| 823 | + } |
|
| 824 | 824 | |
| 825 | - return $texte; |
|
| 825 | + return $texte; |
|
| 826 | 826 | } |
| 827 | 827 | |
| 828 | 828 | /** |
@@ -834,11 +834,11 @@ discard block |
||
| 834 | 834 | * Texte converti |
| 835 | 835 | **/ |
| 836 | 836 | function javascript_to_binary($texte) { |
| 837 | - while (preg_match(',%([0-9A-F][0-9A-F]),', $texte, $regs)) { |
|
| 838 | - $texte = str_replace($regs[0], chr(hexdec($regs[1])), $texte); |
|
| 839 | - } |
|
| 837 | + while (preg_match(',%([0-9A-F][0-9A-F]),', $texte, $regs)) { |
|
| 838 | + $texte = str_replace($regs[0], chr(hexdec($regs[1])), $texte); |
|
| 839 | + } |
|
| 840 | 840 | |
| 841 | - return $texte; |
|
| 841 | + return $texte; |
|
| 842 | 842 | } |
| 843 | 843 | |
| 844 | 844 | |
@@ -856,26 +856,26 @@ discard block |
||
| 856 | 856 | * @return string |
| 857 | 857 | */ |
| 858 | 858 | function translitteration_rapide($texte, $charset = 'AUTO', $complexe = '') { |
| 859 | - static $trans = []; |
|
| 860 | - if ($charset == 'AUTO') { |
|
| 861 | - $charset = $GLOBALS['meta']['charset']; |
|
| 862 | - } |
|
| 863 | - if (!strlen($texte)) { |
|
| 864 | - return $texte; |
|
| 865 | - } |
|
| 866 | - |
|
| 867 | - $table_translit = 'translit' . $complexe; |
|
| 868 | - |
|
| 869 | - // 2. Translitterer grace a la table predefinie |
|
| 870 | - if (!isset($trans[$complexe])) { |
|
| 871 | - $trans[$complexe] = []; |
|
| 872 | - load_charset($table_translit); |
|
| 873 | - foreach ($GLOBALS['CHARSET'][$table_translit] as $key => $val) { |
|
| 874 | - $trans[$complexe][caractere_utf_8($key)] = $val; |
|
| 875 | - } |
|
| 876 | - } |
|
| 877 | - |
|
| 878 | - return str_replace(array_keys($trans[$complexe]), array_values($trans[$complexe]), $texte); |
|
| 859 | + static $trans = []; |
|
| 860 | + if ($charset == 'AUTO') { |
|
| 861 | + $charset = $GLOBALS['meta']['charset']; |
|
| 862 | + } |
|
| 863 | + if (!strlen($texte)) { |
|
| 864 | + return $texte; |
|
| 865 | + } |
|
| 866 | + |
|
| 867 | + $table_translit = 'translit' . $complexe; |
|
| 868 | + |
|
| 869 | + // 2. Translitterer grace a la table predefinie |
|
| 870 | + if (!isset($trans[$complexe])) { |
|
| 871 | + $trans[$complexe] = []; |
|
| 872 | + load_charset($table_translit); |
|
| 873 | + foreach ($GLOBALS['CHARSET'][$table_translit] as $key => $val) { |
|
| 874 | + $trans[$complexe][caractere_utf_8($key)] = $val; |
|
| 875 | + } |
|
| 876 | + } |
|
| 877 | + |
|
| 878 | + return str_replace(array_keys($trans[$complexe]), array_values($trans[$complexe]), $texte); |
|
| 879 | 879 | } |
| 880 | 880 | |
| 881 | 881 | /** |
@@ -898,14 +898,14 @@ discard block |
||
| 898 | 898 | * @return string |
| 899 | 899 | */ |
| 900 | 900 | function translitteration($texte, $charset = 'AUTO', $complexe = '') { |
| 901 | - // 0. Supprimer les caracteres illegaux |
|
| 902 | - include_spip('inc/filtres'); |
|
| 903 | - $texte = corriger_caracteres($texte); |
|
| 901 | + // 0. Supprimer les caracteres illegaux |
|
| 902 | + include_spip('inc/filtres'); |
|
| 903 | + $texte = corriger_caracteres($texte); |
|
| 904 | 904 | |
| 905 | - // 1. Passer le charset et les é en utf-8 |
|
| 906 | - $texte = unicode_to_utf_8(html2unicode(charset2unicode($texte, $charset))); |
|
| 905 | + // 1. Passer le charset et les é en utf-8 |
|
| 906 | + $texte = unicode_to_utf_8(html2unicode(charset2unicode($texte, $charset))); |
|
| 907 | 907 | |
| 908 | - return translitteration_rapide($texte, $charset, $complexe); |
|
| 908 | + return translitteration_rapide($texte, $charset, $complexe); |
|
| 909 | 909 | } |
| 910 | 910 | |
| 911 | 911 | /** |
@@ -920,17 +920,17 @@ discard block |
||
| 920 | 920 | * @return string |
| 921 | 921 | */ |
| 922 | 922 | function translitteration_complexe($texte, $chiffres = false) { |
| 923 | - $texte = translitteration($texte, 'AUTO', 'complexe'); |
|
| 923 | + $texte = translitteration($texte, 'AUTO', 'complexe'); |
|
| 924 | 924 | |
| 925 | - if ($chiffres) { |
|
| 926 | - $texte = preg_replace_callback( |
|
| 927 | - "/[aeiuoyd]['`?~.^+(-]{1,2}/S", |
|
| 928 | - fn($m) => translitteration_chiffree($m[0]), |
|
| 929 | - $texte |
|
| 930 | - ); |
|
| 931 | - } |
|
| 925 | + if ($chiffres) { |
|
| 926 | + $texte = preg_replace_callback( |
|
| 927 | + "/[aeiuoyd]['`?~.^+(-]{1,2}/S", |
|
| 928 | + fn($m) => translitteration_chiffree($m[0]), |
|
| 929 | + $texte |
|
| 930 | + ); |
|
| 931 | + } |
|
| 932 | 932 | |
| 933 | - return $texte; |
|
| 933 | + return $texte; |
|
| 934 | 934 | } |
| 935 | 935 | |
| 936 | 936 | /** |
@@ -942,7 +942,7 @@ discard block |
||
| 942 | 942 | * @return string |
| 943 | 943 | */ |
| 944 | 944 | function translitteration_chiffree($car) { |
| 945 | - return strtr($car, "'`?~.^+(-", '123456789'); |
|
| 945 | + return strtr($car, "'`?~.^+(-", '123456789'); |
|
| 946 | 946 | } |
| 947 | 947 | |
| 948 | 948 | |
@@ -955,7 +955,7 @@ discard block |
||
| 955 | 955 | * true s'il a un BOM |
| 956 | 956 | **/ |
| 957 | 957 | function bom_utf8($texte) { |
| 958 | - return (substr($texte, 0, 3) == chr(0xEF) . chr(0xBB) . chr(0xBF)); |
|
| 958 | + return (substr($texte, 0, 3) == chr(0xEF) . chr(0xBB) . chr(0xBF)); |
|
| 959 | 959 | } |
| 960 | 960 | |
| 961 | 961 | /** |
@@ -972,21 +972,21 @@ discard block |
||
| 972 | 972 | * true si c'est le cas |
| 973 | 973 | **/ |
| 974 | 974 | function is_utf8($string) { |
| 975 | - return !strlen( |
|
| 976 | - preg_replace( |
|
| 977 | - ',[\x09\x0A\x0D\x20-\x7E]' # ASCII |
|
| 978 | - . '|[\xC2-\xDF][\x80-\xBF]' # non-overlong 2-byte |
|
| 979 | - . '|\xE0[\xA0-\xBF][\x80-\xBF]' # excluding overlongs |
|
| 980 | - . '|[\xE1-\xEC\xEE\xEF][\x80-\xBF]{2}' # straight 3-byte |
|
| 981 | - . '|\xED[\x80-\x9F][\x80-\xBF]' # excluding surrogates |
|
| 982 | - . '|\xF0[\x90-\xBF][\x80-\xBF]{2}' # planes 1-3 |
|
| 983 | - . '|[\xF1-\xF3][\x80-\xBF]{3}' # planes 4-15 |
|
| 984 | - . '|\xF4[\x80-\x8F][\x80-\xBF]{2}' # plane 16 |
|
| 985 | - . ',sS', |
|
| 986 | - '', |
|
| 987 | - $string |
|
| 988 | - ) |
|
| 989 | - ); |
|
| 975 | + return !strlen( |
|
| 976 | + preg_replace( |
|
| 977 | + ',[\x09\x0A\x0D\x20-\x7E]' # ASCII |
|
| 978 | + . '|[\xC2-\xDF][\x80-\xBF]' # non-overlong 2-byte |
|
| 979 | + . '|\xE0[\xA0-\xBF][\x80-\xBF]' # excluding overlongs |
|
| 980 | + . '|[\xE1-\xEC\xEE\xEF][\x80-\xBF]{2}' # straight 3-byte |
|
| 981 | + . '|\xED[\x80-\x9F][\x80-\xBF]' # excluding surrogates |
|
| 982 | + . '|\xF0[\x90-\xBF][\x80-\xBF]{2}' # planes 1-3 |
|
| 983 | + . '|[\xF1-\xF3][\x80-\xBF]{3}' # planes 4-15 |
|
| 984 | + . '|\xF4[\x80-\x8F][\x80-\xBF]{2}' # plane 16 |
|
| 985 | + . ',sS', |
|
| 986 | + '', |
|
| 987 | + $string |
|
| 988 | + ) |
|
| 989 | + ); |
|
| 990 | 990 | } |
| 991 | 991 | |
| 992 | 992 | /** |
@@ -998,13 +998,13 @@ discard block |
||
| 998 | 998 | * true si c'est le cas |
| 999 | 999 | **/ |
| 1000 | 1000 | function is_ascii($string) { |
| 1001 | - return !strlen( |
|
| 1002 | - preg_replace( |
|
| 1003 | - ',[\x09\x0A\x0D\x20-\x7E],sS', |
|
| 1004 | - '', |
|
| 1005 | - $string |
|
| 1006 | - ) |
|
| 1007 | - ); |
|
| 1001 | + return !strlen( |
|
| 1002 | + preg_replace( |
|
| 1003 | + ',[\x09\x0A\x0D\x20-\x7E],sS', |
|
| 1004 | + '', |
|
| 1005 | + $string |
|
| 1006 | + ) |
|
| 1007 | + ); |
|
| 1008 | 1008 | } |
| 1009 | 1009 | |
| 1010 | 1010 | /** |
@@ -1023,53 +1023,53 @@ discard block |
||
| 1023 | 1023 | **/ |
| 1024 | 1024 | function transcoder_page($texte, $headers = '') { |
| 1025 | 1025 | |
| 1026 | - // Si tout est < 128 pas la peine d'aller plus loin |
|
| 1027 | - if (is_ascii($texte)) { |
|
| 1028 | - #spip_log('charset: ascii'); |
|
| 1029 | - return $texte; |
|
| 1030 | - } |
|
| 1031 | - |
|
| 1032 | - if (bom_utf8($texte)) { |
|
| 1033 | - // Reconnaitre le BOM utf-8 (0xEFBBBF) |
|
| 1034 | - $charset = 'utf-8'; |
|
| 1035 | - $texte = substr($texte, 3); |
|
| 1036 | - } elseif (preg_match(',<[?]xml[^>]*encoding[^>]*=[^>]*([-_a-z0-9]+?),UimsS', $texte, $regs)) { |
|
| 1037 | - // charset precise par le contenu (xml) |
|
| 1038 | - $charset = trim(strtolower($regs[1])); |
|
| 1039 | - } elseif ( |
|
| 1040 | - // charset precise par le contenu (html) |
|
| 1041 | - preg_match(',<(meta|html|body)[^>]*charset[^>]*=[^>]*([#-_a-z0-9]+?),UimsS', $texte, $regs) |
|
| 1042 | - # eviter toute balise SPIP tel que #CHARSET ou #CONFIG d'un squelette |
|
| 1043 | - and false === strpos($regs[2], '#') |
|
| 1044 | - and $tmp = trim(strtolower($regs[2])) |
|
| 1045 | - ) { |
|
| 1046 | - $charset = $tmp; |
|
| 1047 | - } elseif (preg_match(',charset=([-_a-z0-9]+),i', $headers, $regs)) { |
|
| 1048 | - // charset de la reponse http |
|
| 1049 | - $charset = trim(strtolower($regs[1])); |
|
| 1050 | - } else { |
|
| 1051 | - $charset = ''; |
|
| 1052 | - } |
|
| 1053 | - |
|
| 1054 | - |
|
| 1055 | - // normaliser les noms du shif-jis japonais |
|
| 1056 | - if (preg_match(',^(x|shift)[_-]s?jis$,i', $charset)) { |
|
| 1057 | - $charset = 'shift-jis'; |
|
| 1058 | - } |
|
| 1059 | - |
|
| 1060 | - if ($charset) { |
|
| 1061 | - spip_log("charset: $charset"); |
|
| 1062 | - } else { |
|
| 1063 | - // valeur par defaut |
|
| 1064 | - if (is_utf8($texte)) { |
|
| 1065 | - $charset = 'utf-8'; |
|
| 1066 | - } else { |
|
| 1067 | - $charset = 'iso-8859-1'; |
|
| 1068 | - } |
|
| 1069 | - spip_log("charset probable: $charset"); |
|
| 1070 | - } |
|
| 1071 | - |
|
| 1072 | - return importer_charset($texte, $charset); |
|
| 1026 | + // Si tout est < 128 pas la peine d'aller plus loin |
|
| 1027 | + if (is_ascii($texte)) { |
|
| 1028 | + #spip_log('charset: ascii'); |
|
| 1029 | + return $texte; |
|
| 1030 | + } |
|
| 1031 | + |
|
| 1032 | + if (bom_utf8($texte)) { |
|
| 1033 | + // Reconnaitre le BOM utf-8 (0xEFBBBF) |
|
| 1034 | + $charset = 'utf-8'; |
|
| 1035 | + $texte = substr($texte, 3); |
|
| 1036 | + } elseif (preg_match(',<[?]xml[^>]*encoding[^>]*=[^>]*([-_a-z0-9]+?),UimsS', $texte, $regs)) { |
|
| 1037 | + // charset precise par le contenu (xml) |
|
| 1038 | + $charset = trim(strtolower($regs[1])); |
|
| 1039 | + } elseif ( |
|
| 1040 | + // charset precise par le contenu (html) |
|
| 1041 | + preg_match(',<(meta|html|body)[^>]*charset[^>]*=[^>]*([#-_a-z0-9]+?),UimsS', $texte, $regs) |
|
| 1042 | + # eviter toute balise SPIP tel que #CHARSET ou #CONFIG d'un squelette |
|
| 1043 | + and false === strpos($regs[2], '#') |
|
| 1044 | + and $tmp = trim(strtolower($regs[2])) |
|
| 1045 | + ) { |
|
| 1046 | + $charset = $tmp; |
|
| 1047 | + } elseif (preg_match(',charset=([-_a-z0-9]+),i', $headers, $regs)) { |
|
| 1048 | + // charset de la reponse http |
|
| 1049 | + $charset = trim(strtolower($regs[1])); |
|
| 1050 | + } else { |
|
| 1051 | + $charset = ''; |
|
| 1052 | + } |
|
| 1053 | + |
|
| 1054 | + |
|
| 1055 | + // normaliser les noms du shif-jis japonais |
|
| 1056 | + if (preg_match(',^(x|shift)[_-]s?jis$,i', $charset)) { |
|
| 1057 | + $charset = 'shift-jis'; |
|
| 1058 | + } |
|
| 1059 | + |
|
| 1060 | + if ($charset) { |
|
| 1061 | + spip_log("charset: $charset"); |
|
| 1062 | + } else { |
|
| 1063 | + // valeur par defaut |
|
| 1064 | + if (is_utf8($texte)) { |
|
| 1065 | + $charset = 'utf-8'; |
|
| 1066 | + } else { |
|
| 1067 | + $charset = 'iso-8859-1'; |
|
| 1068 | + } |
|
| 1069 | + spip_log("charset probable: $charset"); |
|
| 1070 | + } |
|
| 1071 | + |
|
| 1072 | + return importer_charset($texte, $charset); |
|
| 1073 | 1073 | } |
| 1074 | 1074 | |
| 1075 | 1075 | |
@@ -1093,26 +1093,26 @@ discard block |
||
| 1093 | 1093 | * Le texte coupé |
| 1094 | 1094 | **/ |
| 1095 | 1095 | function spip_substr($c, $start = 0, $length = null) { |
| 1096 | - // Si ce n'est pas utf-8, utiliser substr |
|
| 1097 | - if ($GLOBALS['meta']['charset'] != 'utf-8') { |
|
| 1098 | - if ($length) { |
|
| 1099 | - return substr($c, $start, $length); |
|
| 1100 | - } else { |
|
| 1101 | - return substr($c, $start); |
|
| 1102 | - } |
|
| 1103 | - } |
|
| 1104 | - |
|
| 1105 | - // Si utf-8, voir si on dispose de mb_string |
|
| 1106 | - if (init_mb_string()) { |
|
| 1107 | - if ($length) { |
|
| 1108 | - return mb_substr($c, $start, $length); |
|
| 1109 | - } else { |
|
| 1110 | - return mb_substr($c, $start); |
|
| 1111 | - } |
|
| 1112 | - } |
|
| 1113 | - |
|
| 1114 | - // Version manuelle (cf. ci-dessous) |
|
| 1115 | - return spip_substr_manuelle($c, $start, $length); |
|
| 1096 | + // Si ce n'est pas utf-8, utiliser substr |
|
| 1097 | + if ($GLOBALS['meta']['charset'] != 'utf-8') { |
|
| 1098 | + if ($length) { |
|
| 1099 | + return substr($c, $start, $length); |
|
| 1100 | + } else { |
|
| 1101 | + return substr($c, $start); |
|
| 1102 | + } |
|
| 1103 | + } |
|
| 1104 | + |
|
| 1105 | + // Si utf-8, voir si on dispose de mb_string |
|
| 1106 | + if (init_mb_string()) { |
|
| 1107 | + if ($length) { |
|
| 1108 | + return mb_substr($c, $start, $length); |
|
| 1109 | + } else { |
|
| 1110 | + return mb_substr($c, $start); |
|
| 1111 | + } |
|
| 1112 | + } |
|
| 1113 | + |
|
| 1114 | + // Version manuelle (cf. ci-dessous) |
|
| 1115 | + return spip_substr_manuelle($c, $start, $length); |
|
| 1116 | 1116 | } |
| 1117 | 1117 | |
| 1118 | 1118 | |
@@ -1131,40 +1131,40 @@ discard block |
||
| 1131 | 1131 | **/ |
| 1132 | 1132 | function spip_substr_manuelle($c, $start, $length = null) { |
| 1133 | 1133 | |
| 1134 | - // Cas pathologique |
|
| 1135 | - if ($length === 0) { |
|
| 1136 | - return ''; |
|
| 1137 | - } |
|
| 1138 | - |
|
| 1139 | - // S'il y a un demarrage, on se positionne |
|
| 1140 | - if ($start > 0) { |
|
| 1141 | - $c = substr($c, strlen(spip_substr_manuelle($c, 0, $start))); |
|
| 1142 | - } elseif ($start < 0) { |
|
| 1143 | - return spip_substr_manuelle($c, spip_strlen($c) + $start, $length); |
|
| 1144 | - } |
|
| 1145 | - |
|
| 1146 | - if (!$length) { |
|
| 1147 | - return $c; |
|
| 1148 | - } |
|
| 1149 | - |
|
| 1150 | - if ($length > 0) { |
|
| 1151 | - // on prend n fois la longueur desiree, pour etre surs d'avoir tout |
|
| 1152 | - // (un caractere utf-8 prenant au maximum n bytes) |
|
| 1153 | - $n = 0; |
|
| 1154 | - while (preg_match(',[\x80-\xBF]{' . (++$n) . '},', $c)) { |
|
| 1155 | - ; |
|
| 1156 | - } |
|
| 1157 | - $c = substr($c, 0, $n * $length); |
|
| 1158 | - // puis, tant qu'on est trop long, on coupe... |
|
| 1159 | - while (($l = spip_strlen($c)) > $length) { |
|
| 1160 | - $c = substr($c, 0, $length - $l); |
|
| 1161 | - } |
|
| 1162 | - |
|
| 1163 | - return $c; |
|
| 1164 | - } |
|
| 1165 | - |
|
| 1166 | - // $length < 0 |
|
| 1167 | - return spip_substr_manuelle($c, 0, spip_strlen($c) + $length); |
|
| 1134 | + // Cas pathologique |
|
| 1135 | + if ($length === 0) { |
|
| 1136 | + return ''; |
|
| 1137 | + } |
|
| 1138 | + |
|
| 1139 | + // S'il y a un demarrage, on se positionne |
|
| 1140 | + if ($start > 0) { |
|
| 1141 | + $c = substr($c, strlen(spip_substr_manuelle($c, 0, $start))); |
|
| 1142 | + } elseif ($start < 0) { |
|
| 1143 | + return spip_substr_manuelle($c, spip_strlen($c) + $start, $length); |
|
| 1144 | + } |
|
| 1145 | + |
|
| 1146 | + if (!$length) { |
|
| 1147 | + return $c; |
|
| 1148 | + } |
|
| 1149 | + |
|
| 1150 | + if ($length > 0) { |
|
| 1151 | + // on prend n fois la longueur desiree, pour etre surs d'avoir tout |
|
| 1152 | + // (un caractere utf-8 prenant au maximum n bytes) |
|
| 1153 | + $n = 0; |
|
| 1154 | + while (preg_match(',[\x80-\xBF]{' . (++$n) . '},', $c)) { |
|
| 1155 | + ; |
|
| 1156 | + } |
|
| 1157 | + $c = substr($c, 0, $n * $length); |
|
| 1158 | + // puis, tant qu'on est trop long, on coupe... |
|
| 1159 | + while (($l = spip_strlen($c)) > $length) { |
|
| 1160 | + $c = substr($c, 0, $length - $l); |
|
| 1161 | + } |
|
| 1162 | + |
|
| 1163 | + return $c; |
|
| 1164 | + } |
|
| 1165 | + |
|
| 1166 | + // $length < 0 |
|
| 1167 | + return spip_substr_manuelle($c, 0, spip_strlen($c) + $length); |
|
| 1168 | 1168 | } |
| 1169 | 1169 | |
| 1170 | 1170 | /** |
@@ -1178,14 +1178,14 @@ discard block |
||
| 1178 | 1178 | * La chaîne avec une majuscule sur le premier mot |
| 1179 | 1179 | */ |
| 1180 | 1180 | function spip_ucfirst($c) { |
| 1181 | - // Si on n'a pas mb_* ou si ce n'est pas utf-8, utiliser ucfirst |
|
| 1182 | - if (!init_mb_string() or $GLOBALS['meta']['charset'] != 'utf-8') { |
|
| 1183 | - return ucfirst($c); |
|
| 1184 | - } |
|
| 1181 | + // Si on n'a pas mb_* ou si ce n'est pas utf-8, utiliser ucfirst |
|
| 1182 | + if (!init_mb_string() or $GLOBALS['meta']['charset'] != 'utf-8') { |
|
| 1183 | + return ucfirst($c); |
|
| 1184 | + } |
|
| 1185 | 1185 | |
| 1186 | - $lettre1 = mb_strtoupper(spip_substr($c, 0, 1)); |
|
| 1186 | + $lettre1 = mb_strtoupper(spip_substr($c, 0, 1)); |
|
| 1187 | 1187 | |
| 1188 | - return $lettre1 . spip_substr($c, 1); |
|
| 1188 | + return $lettre1 . spip_substr($c, 1); |
|
| 1189 | 1189 | } |
| 1190 | 1190 | |
| 1191 | 1191 | /** |
@@ -1199,12 +1199,12 @@ discard block |
||
| 1199 | 1199 | * La chaîne en minuscules |
| 1200 | 1200 | */ |
| 1201 | 1201 | function spip_strtolower($c) { |
| 1202 | - // Si on n'a pas mb_* ou si ce n'est pas utf-8, utiliser strtolower |
|
| 1203 | - if (!init_mb_string() or $GLOBALS['meta']['charset'] != 'utf-8') { |
|
| 1204 | - return strtolower($c); |
|
| 1205 | - } |
|
| 1202 | + // Si on n'a pas mb_* ou si ce n'est pas utf-8, utiliser strtolower |
|
| 1203 | + if (!init_mb_string() or $GLOBALS['meta']['charset'] != 'utf-8') { |
|
| 1204 | + return strtolower($c); |
|
| 1205 | + } |
|
| 1206 | 1206 | |
| 1207 | - return mb_strtolower($c); |
|
| 1207 | + return mb_strtolower($c); |
|
| 1208 | 1208 | } |
| 1209 | 1209 | |
| 1210 | 1210 | /** |
@@ -1218,23 +1218,23 @@ discard block |
||
| 1218 | 1218 | * Longueur de la chaîne |
| 1219 | 1219 | */ |
| 1220 | 1220 | function spip_strlen($c) { |
| 1221 | - // On transforme les sauts de ligne pour ne pas compter deux caractères |
|
| 1222 | - $c = str_replace("\r\n", "\n", $c); |
|
| 1223 | - |
|
| 1224 | - // Si ce n'est pas utf-8, utiliser strlen |
|
| 1225 | - if ($GLOBALS['meta']['charset'] != 'utf-8') { |
|
| 1226 | - return strlen($c); |
|
| 1227 | - } |
|
| 1228 | - |
|
| 1229 | - // Sinon, utiliser mb_strlen() si disponible |
|
| 1230 | - if (init_mb_string()) { |
|
| 1231 | - return mb_strlen($c); |
|
| 1232 | - } |
|
| 1233 | - |
|
| 1234 | - // Methode manuelle : on supprime les bytes 10......, |
|
| 1235 | - // on compte donc les ascii (0.......) et les demarrages |
|
| 1236 | - // de caracteres utf-8 (11......) |
|
| 1237 | - return strlen(preg_replace(',[\x80-\xBF],S', '', $c)); |
|
| 1221 | + // On transforme les sauts de ligne pour ne pas compter deux caractères |
|
| 1222 | + $c = str_replace("\r\n", "\n", $c); |
|
| 1223 | + |
|
| 1224 | + // Si ce n'est pas utf-8, utiliser strlen |
|
| 1225 | + if ($GLOBALS['meta']['charset'] != 'utf-8') { |
|
| 1226 | + return strlen($c); |
|
| 1227 | + } |
|
| 1228 | + |
|
| 1229 | + // Sinon, utiliser mb_strlen() si disponible |
|
| 1230 | + if (init_mb_string()) { |
|
| 1231 | + return mb_strlen($c); |
|
| 1232 | + } |
|
| 1233 | + |
|
| 1234 | + // Methode manuelle : on supprime les bytes 10......, |
|
| 1235 | + // on compte donc les ascii (0.......) et les demarrages |
|
| 1236 | + // de caracteres utf-8 (11......) |
|
| 1237 | + return strlen(preg_replace(',[\x80-\xBF],S', '', $c)); |
|
| 1238 | 1238 | } |
| 1239 | 1239 | |
| 1240 | 1240 | // Initialisation |
@@ -1244,16 +1244,16 @@ discard block |
||
| 1244 | 1244 | // dans les preg_replace pour ne pas casser certaines lettres accentuees : |
| 1245 | 1245 | // en utf-8 chr(195).chr(160) = a` alors qu'en iso-latin chr(160) = nbsp |
| 1246 | 1246 | if ( |
| 1247 | - !isset($GLOBALS['meta']['pcre_u']) |
|
| 1248 | - or (isset($_GET['var_mode']) and !isset($_GET['var_profile'])) |
|
| 1247 | + !isset($GLOBALS['meta']['pcre_u']) |
|
| 1248 | + or (isset($_GET['var_mode']) and !isset($_GET['var_profile'])) |
|
| 1249 | 1249 | ) { |
| 1250 | - include_spip('inc/meta'); |
|
| 1251 | - ecrire_meta( |
|
| 1252 | - 'pcre_u', |
|
| 1253 | - $u = (lire_config('charset', _DEFAULT_CHARSET) == 'utf-8' |
|
| 1254 | - and test_pcre_unicode()) |
|
| 1255 | - ? 'u' : '' |
|
| 1256 | - ); |
|
| 1250 | + include_spip('inc/meta'); |
|
| 1251 | + ecrire_meta( |
|
| 1252 | + 'pcre_u', |
|
| 1253 | + $u = (lire_config('charset', _DEFAULT_CHARSET) == 'utf-8' |
|
| 1254 | + and test_pcre_unicode()) |
|
| 1255 | + ? 'u' : '' |
|
| 1256 | + ); |
|
| 1257 | 1257 | } |
| 1258 | 1258 | |
| 1259 | 1259 | |
@@ -1269,17 +1269,17 @@ discard block |
||
| 1269 | 1269 | * en unicode : 💩 |
| 1270 | 1270 | */ |
| 1271 | 1271 | function utf8_noplanes($x) { |
| 1272 | - $regexp_utf8_4bytes = '/( |
|
| 1272 | + $regexp_utf8_4bytes = '/( |
|
| 1273 | 1273 | \xF0[\x90-\xBF][\x80-\xBF]{2} # planes 1-3 |
| 1274 | 1274 | | [\xF1-\xF3][\x80-\xBF]{3} # planes 4-15 |
| 1275 | 1275 | | \xF4[\x80-\x8F][\x80-\xBF]{2} # plane 16 |
| 1276 | 1276 | )/xS'; |
| 1277 | - if (preg_match_all($regexp_utf8_4bytes, $x, $z, PREG_PATTERN_ORDER)) { |
|
| 1278 | - foreach ($z[0] as $k) { |
|
| 1279 | - $ku = utf_8_to_unicode($k); |
|
| 1280 | - $x = str_replace($k, $ku, $x); |
|
| 1281 | - } |
|
| 1282 | - } |
|
| 1283 | - |
|
| 1284 | - return $x; |
|
| 1277 | + if (preg_match_all($regexp_utf8_4bytes, $x, $z, PREG_PATTERN_ORDER)) { |
|
| 1278 | + foreach ($z[0] as $k) { |
|
| 1279 | + $ku = utf_8_to_unicode($k); |
|
| 1280 | + $x = str_replace($k, $ku, $x); |
|
| 1281 | + } |
|
| 1282 | + } |
|
| 1283 | + |
|
| 1284 | + return $x; |
|
| 1285 | 1285 | } |