@@ -21,7 +21,7 @@ discard block |
||
| 21 | 21 | |
| 22 | 22 | // securité |
| 23 | 23 | if (!defined('_ECRIRE_INC_VERSION')) { |
| 24 | - return; |
|
| 24 | + return; |
|
| 25 | 25 | } |
| 26 | 26 | |
| 27 | 27 | // se faciliter la lecture du charset |
@@ -31,19 +31,19 @@ discard block |
||
| 31 | 31 | * Initialisation |
| 32 | 32 | */ |
| 33 | 33 | function init_charset(): void { |
| 34 | - // Initialisation |
|
| 35 | - $GLOBALS['CHARSET'] = []; |
|
| 36 | - |
|
| 37 | - // noter a l'occasion dans la meta pcre_u notre capacite a utiliser le flag /u |
|
| 38 | - // dans les preg_replace pour ne pas casser certaines lettres accentuees : |
|
| 39 | - // en utf-8 chr(195).chr(160) = a` alors qu'en iso-latin chr(160) = nbsp |
|
| 40 | - if ( |
|
| 41 | - !isset($GLOBALS['meta']['pcre_u']) |
|
| 42 | - || isset($_GET['var_mode']) && !isset($_GET['var_profile']) |
|
| 43 | - ) { |
|
| 44 | - include_spip('inc/meta'); |
|
| 45 | - ecrire_meta('pcre_u', (lire_config('charset', _DEFAULT_CHARSET) === 'utf-8') ? 'u' : ''); |
|
| 46 | - } |
|
| 34 | + // Initialisation |
|
| 35 | + $GLOBALS['CHARSET'] = []; |
|
| 36 | + |
|
| 37 | + // noter a l'occasion dans la meta pcre_u notre capacite a utiliser le flag /u |
|
| 38 | + // dans les preg_replace pour ne pas casser certaines lettres accentuees : |
|
| 39 | + // en utf-8 chr(195).chr(160) = a` alors qu'en iso-latin chr(160) = nbsp |
|
| 40 | + if ( |
|
| 41 | + !isset($GLOBALS['meta']['pcre_u']) |
|
| 42 | + || isset($_GET['var_mode']) && !isset($_GET['var_profile']) |
|
| 43 | + ) { |
|
| 44 | + include_spip('inc/meta'); |
|
| 45 | + ecrire_meta('pcre_u', (lire_config('charset', _DEFAULT_CHARSET) === 'utf-8') ? 'u' : ''); |
|
| 46 | + } |
|
| 47 | 47 | } |
| 48 | 48 | |
| 49 | 49 | // TODO: code d’exécution en dehors du fichier. |
@@ -64,39 +64,39 @@ discard block |
||
| 64 | 64 | * - false si le charset n'est pas décrit dans le répertoire charsets/ |
| 65 | 65 | **/ |
| 66 | 66 | function load_charset($charset = 'AUTO') { |
| 67 | - if ($charset == 'AUTO') { |
|
| 68 | - $charset = $GLOBALS['meta']['charset']; |
|
| 69 | - } |
|
| 70 | - $charset = trim(strtolower((string) $charset)); |
|
| 71 | - if (isset($GLOBALS['CHARSET'][$charset])) { |
|
| 72 | - return $charset; |
|
| 73 | - } |
|
| 74 | - |
|
| 75 | - if ($charset == 'utf-8') { |
|
| 76 | - $GLOBALS['CHARSET'][$charset] = []; |
|
| 77 | - |
|
| 78 | - return $charset; |
|
| 79 | - } |
|
| 80 | - |
|
| 81 | - // Quelques synonymes |
|
| 82 | - if ($charset === '') { |
|
| 83 | - $charset = 'iso-8859-1'; |
|
| 84 | - } elseif ($charset === 'windows-1250') { |
|
| 85 | - $charset = 'cp1250'; |
|
| 86 | - } elseif ($charset === 'windows-1251') { |
|
| 87 | - $charset = 'cp1251'; |
|
| 88 | - } elseif ($charset === 'windows-1256') { |
|
| 89 | - $charset = 'cp1256'; |
|
| 90 | - } |
|
| 91 | - |
|
| 92 | - if (find_in_path($charset . '.php', 'charsets/', true)) { |
|
| 93 | - return $charset; |
|
| 94 | - } else { |
|
| 95 | - spip_log("Erreur: pas de fichier de conversion 'charsets/$charset'"); |
|
| 96 | - $GLOBALS['CHARSET'][$charset] = []; |
|
| 97 | - |
|
| 98 | - return false; |
|
| 99 | - } |
|
| 67 | + if ($charset == 'AUTO') { |
|
| 68 | + $charset = $GLOBALS['meta']['charset']; |
|
| 69 | + } |
|
| 70 | + $charset = trim(strtolower((string) $charset)); |
|
| 71 | + if (isset($GLOBALS['CHARSET'][$charset])) { |
|
| 72 | + return $charset; |
|
| 73 | + } |
|
| 74 | + |
|
| 75 | + if ($charset == 'utf-8') { |
|
| 76 | + $GLOBALS['CHARSET'][$charset] = []; |
|
| 77 | + |
|
| 78 | + return $charset; |
|
| 79 | + } |
|
| 80 | + |
|
| 81 | + // Quelques synonymes |
|
| 82 | + if ($charset === '') { |
|
| 83 | + $charset = 'iso-8859-1'; |
|
| 84 | + } elseif ($charset === 'windows-1250') { |
|
| 85 | + $charset = 'cp1250'; |
|
| 86 | + } elseif ($charset === 'windows-1251') { |
|
| 87 | + $charset = 'cp1251'; |
|
| 88 | + } elseif ($charset === 'windows-1256') { |
|
| 89 | + $charset = 'cp1256'; |
|
| 90 | + } |
|
| 91 | + |
|
| 92 | + if (find_in_path($charset . '.php', 'charsets/', true)) { |
|
| 93 | + return $charset; |
|
| 94 | + } else { |
|
| 95 | + spip_log("Erreur: pas de fichier de conversion 'charsets/$charset'"); |
|
| 96 | + $GLOBALS['CHARSET'][$charset] = []; |
|
| 97 | + |
|
| 98 | + return false; |
|
| 99 | + } |
|
| 100 | 100 | } |
| 101 | 101 | |
| 102 | 102 | |
@@ -109,19 +109,19 @@ discard block |
||
| 109 | 109 | * true si notre charset est utilisable par mb_strsing |
| 110 | 110 | **/ |
| 111 | 111 | function init_mb_string(): bool { |
| 112 | - static $mb; |
|
| 113 | - |
|
| 114 | - // verifier que le charset interne est connu de mb_string |
|
| 115 | - if (!$mb) { |
|
| 116 | - if (mb_detect_order(lire_config('charset', _DEFAULT_CHARSET))) { |
|
| 117 | - mb_internal_encoding('utf-8'); |
|
| 118 | - $mb = 1; |
|
| 119 | - } else { |
|
| 120 | - $mb = -1; |
|
| 121 | - } |
|
| 122 | - } |
|
| 123 | - |
|
| 124 | - return ($mb === 1); |
|
| 112 | + static $mb; |
|
| 113 | + |
|
| 114 | + // verifier que le charset interne est connu de mb_string |
|
| 115 | + if (!$mb) { |
|
| 116 | + if (mb_detect_order(lire_config('charset', _DEFAULT_CHARSET))) { |
|
| 117 | + mb_internal_encoding('utf-8'); |
|
| 118 | + $mb = 1; |
|
| 119 | + } else { |
|
| 120 | + $mb = -1; |
|
| 121 | + } |
|
| 122 | + } |
|
| 123 | + |
|
| 124 | + return ($mb === 1); |
|
| 125 | 125 | } |
| 126 | 126 | |
| 127 | 127 | /** |
@@ -136,17 +136,17 @@ discard block |
||
| 136 | 136 | * true si iconv fonctionne correctement |
| 137 | 137 | **/ |
| 138 | 138 | function test_iconv(): bool { |
| 139 | - static $iconv_ok; |
|
| 139 | + static $iconv_ok; |
|
| 140 | 140 | |
| 141 | - if (!$iconv_ok) { |
|
| 142 | - if (!function_exists('iconv')) { |
|
| 143 | - $iconv_ok = -1; |
|
| 144 | - } else { |
|
| 145 | - $iconv_ok = utf_32_to_unicode(@iconv('utf-8', 'utf-32', 'chaine de test')) === 'chaine de test' ? 1 : -1; |
|
| 146 | - } |
|
| 147 | - } |
|
| 141 | + if (!$iconv_ok) { |
|
| 142 | + if (!function_exists('iconv')) { |
|
| 143 | + $iconv_ok = -1; |
|
| 144 | + } else { |
|
| 145 | + $iconv_ok = utf_32_to_unicode(@iconv('utf-8', 'utf-32', 'chaine de test')) === 'chaine de test' ? 1 : -1; |
|
| 146 | + } |
|
| 147 | + } |
|
| 148 | 148 | |
| 149 | - return ($iconv_ok === 1); |
|
| 149 | + return ($iconv_ok === 1); |
|
| 150 | 150 | } |
| 151 | 151 | |
| 152 | 152 | /** |
@@ -166,75 +166,75 @@ discard block |
||
| 166 | 166 | * texte corrigé |
| 167 | 167 | **/ |
| 168 | 168 | function corriger_caracteres_windows($texte, $charset = 'AUTO', $charset_cible = 'unicode') { |
| 169 | - static $trans; |
|
| 170 | - |
|
| 171 | - if (is_array($texte)) { |
|
| 172 | - return array_map('corriger_caracteres_windows', $texte); |
|
| 173 | - } |
|
| 174 | - |
|
| 175 | - if ($charset == 'AUTO') { |
|
| 176 | - $charset = lire_config('charset', _DEFAULT_CHARSET); |
|
| 177 | - } |
|
| 178 | - if ($charset == 'utf-8') { |
|
| 179 | - $p = chr(194); |
|
| 180 | - if (!str_contains($texte, $p)) { |
|
| 181 | - return $texte; |
|
| 182 | - } |
|
| 183 | - } else { |
|
| 184 | - if ($charset == 'iso-8859-1') { |
|
| 185 | - $p = ''; |
|
| 186 | - } else { |
|
| 187 | - return $texte; |
|
| 188 | - } |
|
| 189 | - } |
|
| 190 | - |
|
| 191 | - if (!isset($trans[$charset][$charset_cible])) { |
|
| 192 | - $trans[$charset][$charset_cible] = [ |
|
| 193 | - $p . chr(128) => '€', |
|
| 194 | - $p . chr(129) => ' ', # pas affecte |
|
| 195 | - $p . chr(130) => '‚', |
|
| 196 | - $p . chr(131) => 'ƒ', |
|
| 197 | - $p . chr(132) => '„', |
|
| 198 | - $p . chr(133) => '…', |
|
| 199 | - $p . chr(134) => '†', |
|
| 200 | - $p . chr(135) => '‡', |
|
| 201 | - $p . chr(136) => 'ˆ', |
|
| 202 | - $p . chr(137) => '‰', |
|
| 203 | - $p . chr(138) => 'Š', |
|
| 204 | - $p . chr(139) => '‹', |
|
| 205 | - $p . chr(140) => 'Œ', |
|
| 206 | - $p . chr(141) => ' ', # pas affecte |
|
| 207 | - $p . chr(142) => 'Ž', |
|
| 208 | - $p . chr(143) => ' ', # pas affecte |
|
| 209 | - $p . chr(144) => ' ', # pas affecte |
|
| 210 | - $p . chr(145) => '‘', |
|
| 211 | - $p . chr(146) => '’', |
|
| 212 | - $p . chr(147) => '“', |
|
| 213 | - $p . chr(148) => '”', |
|
| 214 | - $p . chr(149) => '•', |
|
| 215 | - $p . chr(150) => '–', |
|
| 216 | - $p . chr(151) => '—', |
|
| 217 | - $p . chr(152) => '˜', |
|
| 218 | - $p . chr(153) => '™', |
|
| 219 | - $p . chr(154) => 'š', |
|
| 220 | - $p . chr(155) => '›', |
|
| 221 | - $p . chr(156) => 'œ', |
|
| 222 | - $p . chr(157) => ' ', # pas affecte |
|
| 223 | - $p . chr(158) => 'ž', |
|
| 224 | - $p . chr(159) => 'Ÿ', |
|
| 225 | - ]; |
|
| 226 | - if ($charset_cible != 'unicode') { |
|
| 227 | - foreach ($trans[$charset][$charset_cible] as $k => $c) { |
|
| 228 | - $trans[$charset][$charset_cible][$k] = unicode2charset($c, $charset_cible); |
|
| 229 | - } |
|
| 230 | - } |
|
| 231 | - } |
|
| 232 | - |
|
| 233 | - return @str_replace( |
|
| 234 | - array_keys($trans[$charset][$charset_cible]), |
|
| 235 | - array_values($trans[$charset][$charset_cible]), |
|
| 236 | - $texte |
|
| 237 | - ); |
|
| 169 | + static $trans; |
|
| 170 | + |
|
| 171 | + if (is_array($texte)) { |
|
| 172 | + return array_map('corriger_caracteres_windows', $texte); |
|
| 173 | + } |
|
| 174 | + |
|
| 175 | + if ($charset == 'AUTO') { |
|
| 176 | + $charset = lire_config('charset', _DEFAULT_CHARSET); |
|
| 177 | + } |
|
| 178 | + if ($charset == 'utf-8') { |
|
| 179 | + $p = chr(194); |
|
| 180 | + if (!str_contains($texte, $p)) { |
|
| 181 | + return $texte; |
|
| 182 | + } |
|
| 183 | + } else { |
|
| 184 | + if ($charset == 'iso-8859-1') { |
|
| 185 | + $p = ''; |
|
| 186 | + } else { |
|
| 187 | + return $texte; |
|
| 188 | + } |
|
| 189 | + } |
|
| 190 | + |
|
| 191 | + if (!isset($trans[$charset][$charset_cible])) { |
|
| 192 | + $trans[$charset][$charset_cible] = [ |
|
| 193 | + $p . chr(128) => '€', |
|
| 194 | + $p . chr(129) => ' ', # pas affecte |
|
| 195 | + $p . chr(130) => '‚', |
|
| 196 | + $p . chr(131) => 'ƒ', |
|
| 197 | + $p . chr(132) => '„', |
|
| 198 | + $p . chr(133) => '…', |
|
| 199 | + $p . chr(134) => '†', |
|
| 200 | + $p . chr(135) => '‡', |
|
| 201 | + $p . chr(136) => 'ˆ', |
|
| 202 | + $p . chr(137) => '‰', |
|
| 203 | + $p . chr(138) => 'Š', |
|
| 204 | + $p . chr(139) => '‹', |
|
| 205 | + $p . chr(140) => 'Œ', |
|
| 206 | + $p . chr(141) => ' ', # pas affecte |
|
| 207 | + $p . chr(142) => 'Ž', |
|
| 208 | + $p . chr(143) => ' ', # pas affecte |
|
| 209 | + $p . chr(144) => ' ', # pas affecte |
|
| 210 | + $p . chr(145) => '‘', |
|
| 211 | + $p . chr(146) => '’', |
|
| 212 | + $p . chr(147) => '“', |
|
| 213 | + $p . chr(148) => '”', |
|
| 214 | + $p . chr(149) => '•', |
|
| 215 | + $p . chr(150) => '–', |
|
| 216 | + $p . chr(151) => '—', |
|
| 217 | + $p . chr(152) => '˜', |
|
| 218 | + $p . chr(153) => '™', |
|
| 219 | + $p . chr(154) => 'š', |
|
| 220 | + $p . chr(155) => '›', |
|
| 221 | + $p . chr(156) => 'œ', |
|
| 222 | + $p . chr(157) => ' ', # pas affecte |
|
| 223 | + $p . chr(158) => 'ž', |
|
| 224 | + $p . chr(159) => 'Ÿ', |
|
| 225 | + ]; |
|
| 226 | + if ($charset_cible != 'unicode') { |
|
| 227 | + foreach ($trans[$charset][$charset_cible] as $k => $c) { |
|
| 228 | + $trans[$charset][$charset_cible][$k] = unicode2charset($c, $charset_cible); |
|
| 229 | + } |
|
| 230 | + } |
|
| 231 | + } |
|
| 232 | + |
|
| 233 | + return @str_replace( |
|
| 234 | + array_keys($trans[$charset][$charset_cible]), |
|
| 235 | + array_values($trans[$charset][$charset_cible]), |
|
| 236 | + $texte |
|
| 237 | + ); |
|
| 238 | 238 | } |
| 239 | 239 | |
| 240 | 240 | |
@@ -251,31 +251,31 @@ discard block |
||
| 251 | 251 | * texte converti |
| 252 | 252 | **/ |
| 253 | 253 | function html2unicode(string $texte, bool $secure = false): string { |
| 254 | - static $trans = []; |
|
| 255 | - |
|
| 256 | - if ($texte === null || $texte === '') { |
|
| 257 | - return ''; |
|
| 258 | - } |
|
| 259 | - if (!str_contains($texte, '&')) { |
|
| 260 | - return $texte; |
|
| 261 | - } |
|
| 262 | - |
|
| 263 | - if (!$trans) { |
|
| 264 | - load_charset('html'); |
|
| 265 | - foreach ($GLOBALS['CHARSET']['html'] as $key => $val) { |
|
| 266 | - $trans["&$key;"] = $val; |
|
| 267 | - } |
|
| 268 | - } |
|
| 269 | - |
|
| 270 | - $texte = str_replace(array_keys($trans), array_values($trans), $texte); |
|
| 271 | - if (!$secure) { |
|
| 272 | - $texte = str_replace( |
|
| 273 | - ['&', '"', '<', '>'], |
|
| 274 | - ['&', '"', '<', '>'], |
|
| 275 | - $texte |
|
| 276 | - ); |
|
| 277 | - } |
|
| 278 | - return $texte; |
|
| 254 | + static $trans = []; |
|
| 255 | + |
|
| 256 | + if ($texte === null || $texte === '') { |
|
| 257 | + return ''; |
|
| 258 | + } |
|
| 259 | + if (!str_contains($texte, '&')) { |
|
| 260 | + return $texte; |
|
| 261 | + } |
|
| 262 | + |
|
| 263 | + if (!$trans) { |
|
| 264 | + load_charset('html'); |
|
| 265 | + foreach ($GLOBALS['CHARSET']['html'] as $key => $val) { |
|
| 266 | + $trans["&$key;"] = $val; |
|
| 267 | + } |
|
| 268 | + } |
|
| 269 | + |
|
| 270 | + $texte = str_replace(array_keys($trans), array_values($trans), $texte); |
|
| 271 | + if (!$secure) { |
|
| 272 | + $texte = str_replace( |
|
| 273 | + ['&', '"', '<', '>'], |
|
| 274 | + ['&', '"', '<', '>'], |
|
| 275 | + $texte |
|
| 276 | + ); |
|
| 277 | + } |
|
| 278 | + return $texte; |
|
| 279 | 279 | } |
| 280 | 280 | |
| 281 | 281 | |
@@ -290,16 +290,16 @@ discard block |
||
| 290 | 290 | * texte converti |
| 291 | 291 | **/ |
| 292 | 292 | function mathml2unicode($texte) { |
| 293 | - static $trans; |
|
| 294 | - if (!$trans) { |
|
| 295 | - load_charset('mathml'); |
|
| 293 | + static $trans; |
|
| 294 | + if (!$trans) { |
|
| 295 | + load_charset('mathml'); |
|
| 296 | 296 | |
| 297 | - foreach ($GLOBALS['CHARSET']['mathml'] as $key => $val) { |
|
| 298 | - $trans["&$key;"] = $val; |
|
| 299 | - } |
|
| 300 | - } |
|
| 297 | + foreach ($GLOBALS['CHARSET']['mathml'] as $key => $val) { |
|
| 298 | + $trans["&$key;"] = $val; |
|
| 299 | + } |
|
| 300 | + } |
|
| 301 | 301 | |
| 302 | - return str_replace(array_keys($trans), array_values($trans), $texte); |
|
| 302 | + return str_replace(array_keys($trans), array_values($trans), $texte); |
|
| 303 | 303 | } |
| 304 | 304 | |
| 305 | 305 | |
@@ -321,74 +321,74 @@ discard block |
||
| 321 | 321 | * texte converti en unicode |
| 322 | 322 | **/ |
| 323 | 323 | function charset2unicode(?string $texte, string $charset = 'AUTO' /* $forcer: obsolete*/) { |
| 324 | - static $trans; |
|
| 325 | - |
|
| 326 | - if ($texte === null || $texte === '') { |
|
| 327 | - return ''; |
|
| 328 | - } |
|
| 329 | - |
|
| 330 | - if ($charset === 'AUTO') { |
|
| 331 | - $charset = lire_config('charset', _DEFAULT_CHARSET); |
|
| 332 | - } |
|
| 333 | - |
|
| 334 | - if ($charset === '') { |
|
| 335 | - $charset = 'iso-8859-1'; |
|
| 336 | - } |
|
| 337 | - $charset = strtolower((string) $charset); |
|
| 338 | - |
|
| 339 | - switch ($charset) { |
|
| 340 | - case 'utf-8': |
|
| 341 | - case 'utf8': |
|
| 342 | - return utf_8_to_unicode($texte); |
|
| 343 | - |
|
| 344 | - case 'iso-8859-1': |
|
| 345 | - $texte = corriger_caracteres_windows($texte, 'iso-8859-1'); |
|
| 346 | - // pas de break; ici, on suit sur default: |
|
| 347 | - |
|
| 348 | - default: |
|
| 349 | - // mbstring presente ? |
|
| 350 | - if (init_mb_string()) { |
|
| 351 | - $order = mb_detect_order(); |
|
| 352 | - try { |
|
| 353 | - # mb_string connait-il $charset? |
|
| 354 | - if ($order && mb_detect_order($charset)) { |
|
| 355 | - $s = mb_convert_encoding($texte, 'utf-8', $charset); |
|
| 356 | - if ($s && $s != $texte) { |
|
| 357 | - return utf_8_to_unicode($s); |
|
| 358 | - } |
|
| 359 | - } |
|
| 360 | - } catch (\Error) { |
|
| 361 | - // Le charset n'existe probablement pas |
|
| 362 | - } finally { |
|
| 363 | - mb_detect_order($order); # remettre comme precedemment |
|
| 364 | - } |
|
| 365 | - } |
|
| 366 | - |
|
| 367 | - // Sinon, peut-etre connaissons-nous ce charset ? |
|
| 368 | - if (!isset($trans[$charset]) && (($cset = load_charset($charset)) && is_array($GLOBALS['CHARSET'][$cset]))) { |
|
| 369 | - foreach ($GLOBALS['CHARSET'][$cset] as $key => $val) { |
|
| 370 | - $trans[$charset][chr($key)] = '&#' . $val . ';'; |
|
| 371 | - } |
|
| 372 | - } |
|
| 373 | - if (isset($trans[$charset]) && (is_countable($trans[$charset]) ? count($trans[$charset]) : 0)) { |
|
| 374 | - return str_replace(array_keys($trans[$charset]), array_values($trans[$charset]), $texte); |
|
| 375 | - } |
|
| 376 | - |
|
| 377 | - // Sinon demander a iconv (malgre le fait qu'il coupe quand un |
|
| 378 | - // caractere n'appartient pas au charset, mais c'est un probleme |
|
| 379 | - // surtout en utf-8, gere ci-dessus) |
|
| 380 | - if (test_iconv()) { |
|
| 381 | - $s = iconv($charset, 'utf-32le', $texte); |
|
| 382 | - if ($s) { |
|
| 383 | - return utf_32_to_unicode($s); |
|
| 384 | - } |
|
| 385 | - } |
|
| 386 | - |
|
| 387 | - // Au pire ne rien faire |
|
| 388 | - spip_log("erreur charset '$charset' non supporte"); |
|
| 389 | - |
|
| 390 | - return $texte; |
|
| 391 | - } |
|
| 324 | + static $trans; |
|
| 325 | + |
|
| 326 | + if ($texte === null || $texte === '') { |
|
| 327 | + return ''; |
|
| 328 | + } |
|
| 329 | + |
|
| 330 | + if ($charset === 'AUTO') { |
|
| 331 | + $charset = lire_config('charset', _DEFAULT_CHARSET); |
|
| 332 | + } |
|
| 333 | + |
|
| 334 | + if ($charset === '') { |
|
| 335 | + $charset = 'iso-8859-1'; |
|
| 336 | + } |
|
| 337 | + $charset = strtolower((string) $charset); |
|
| 338 | + |
|
| 339 | + switch ($charset) { |
|
| 340 | + case 'utf-8': |
|
| 341 | + case 'utf8': |
|
| 342 | + return utf_8_to_unicode($texte); |
|
| 343 | + |
|
| 344 | + case 'iso-8859-1': |
|
| 345 | + $texte = corriger_caracteres_windows($texte, 'iso-8859-1'); |
|
| 346 | + // pas de break; ici, on suit sur default: |
|
| 347 | + |
|
| 348 | + default: |
|
| 349 | + // mbstring presente ? |
|
| 350 | + if (init_mb_string()) { |
|
| 351 | + $order = mb_detect_order(); |
|
| 352 | + try { |
|
| 353 | + # mb_string connait-il $charset? |
|
| 354 | + if ($order && mb_detect_order($charset)) { |
|
| 355 | + $s = mb_convert_encoding($texte, 'utf-8', $charset); |
|
| 356 | + if ($s && $s != $texte) { |
|
| 357 | + return utf_8_to_unicode($s); |
|
| 358 | + } |
|
| 359 | + } |
|
| 360 | + } catch (\Error) { |
|
| 361 | + // Le charset n'existe probablement pas |
|
| 362 | + } finally { |
|
| 363 | + mb_detect_order($order); # remettre comme precedemment |
|
| 364 | + } |
|
| 365 | + } |
|
| 366 | + |
|
| 367 | + // Sinon, peut-etre connaissons-nous ce charset ? |
|
| 368 | + if (!isset($trans[$charset]) && (($cset = load_charset($charset)) && is_array($GLOBALS['CHARSET'][$cset]))) { |
|
| 369 | + foreach ($GLOBALS['CHARSET'][$cset] as $key => $val) { |
|
| 370 | + $trans[$charset][chr($key)] = '&#' . $val . ';'; |
|
| 371 | + } |
|
| 372 | + } |
|
| 373 | + if (isset($trans[$charset]) && (is_countable($trans[$charset]) ? count($trans[$charset]) : 0)) { |
|
| 374 | + return str_replace(array_keys($trans[$charset]), array_values($trans[$charset]), $texte); |
|
| 375 | + } |
|
| 376 | + |
|
| 377 | + // Sinon demander a iconv (malgre le fait qu'il coupe quand un |
|
| 378 | + // caractere n'appartient pas au charset, mais c'est un probleme |
|
| 379 | + // surtout en utf-8, gere ci-dessus) |
|
| 380 | + if (test_iconv()) { |
|
| 381 | + $s = iconv($charset, 'utf-32le', $texte); |
|
| 382 | + if ($s) { |
|
| 383 | + return utf_32_to_unicode($s); |
|
| 384 | + } |
|
| 385 | + } |
|
| 386 | + |
|
| 387 | + // Au pire ne rien faire |
|
| 388 | + spip_log("erreur charset '$charset' non supporte"); |
|
| 389 | + |
|
| 390 | + return $texte; |
|
| 391 | + } |
|
| 392 | 392 | } |
| 393 | 393 | |
| 394 | 394 | |
@@ -407,42 +407,42 @@ discard block |
||
| 407 | 407 | * texte transformé dans le charset souhaité |
| 408 | 408 | **/ |
| 409 | 409 | function unicode2charset($texte, $charset = 'AUTO') { |
| 410 | - static $CHARSET_REVERSE = []; |
|
| 411 | - static $trans = []; |
|
| 412 | - |
|
| 413 | - if ($charset == 'AUTO') { |
|
| 414 | - $charset = lire_config('charset', _DEFAULT_CHARSET); |
|
| 415 | - } |
|
| 416 | - |
|
| 417 | - switch ($charset) { |
|
| 418 | - case 'utf-8': |
|
| 419 | - return unicode_to_utf_8($texte); |
|
| 420 | - |
|
| 421 | - default: |
|
| 422 | - $charset = load_charset($charset); |
|
| 423 | - |
|
| 424 | - if (empty($CHARSET_REVERSE[$charset])) { |
|
| 425 | - $CHARSET_REVERSE[$charset] = array_flip($GLOBALS['CHARSET'][$charset]); |
|
| 426 | - } |
|
| 427 | - |
|
| 428 | - if (!isset($trans[$charset])) { |
|
| 429 | - $trans[$charset] = []; |
|
| 430 | - $t = &$trans[$charset]; |
|
| 431 | - for ($e = 128; $e < 255; $e++) { |
|
| 432 | - $h = dechex($e); |
|
| 433 | - if ($s = isset($CHARSET_REVERSE[$charset][$e])) { |
|
| 434 | - $s = $CHARSET_REVERSE[$charset][$e]; |
|
| 435 | - $t['&#' . $e . ';'] = $t['�' . $e . ';'] = $t['�' . $e . ';'] = chr($s); |
|
| 436 | - $t['&#x' . $h . ';'] = $t['�' . $h . ';'] = $t['�' . $h . ';'] = chr($s); |
|
| 437 | - } else { |
|
| 438 | - $t['&#' . $e . ';'] = $t['�' . $e . ';'] = $t['�' . $e . ';'] = chr($e); |
|
| 439 | - $t['&#x' . $h . ';'] = $t['�' . $h . ';'] = $t['�' . $h . ';'] = chr($e); |
|
| 440 | - } |
|
| 441 | - } |
|
| 442 | - } |
|
| 443 | - |
|
| 444 | - return str_replace(array_keys($trans[$charset]), array_values($trans[$charset]), $texte); |
|
| 445 | - } |
|
| 410 | + static $CHARSET_REVERSE = []; |
|
| 411 | + static $trans = []; |
|
| 412 | + |
|
| 413 | + if ($charset == 'AUTO') { |
|
| 414 | + $charset = lire_config('charset', _DEFAULT_CHARSET); |
|
| 415 | + } |
|
| 416 | + |
|
| 417 | + switch ($charset) { |
|
| 418 | + case 'utf-8': |
|
| 419 | + return unicode_to_utf_8($texte); |
|
| 420 | + |
|
| 421 | + default: |
|
| 422 | + $charset = load_charset($charset); |
|
| 423 | + |
|
| 424 | + if (empty($CHARSET_REVERSE[$charset])) { |
|
| 425 | + $CHARSET_REVERSE[$charset] = array_flip($GLOBALS['CHARSET'][$charset]); |
|
| 426 | + } |
|
| 427 | + |
|
| 428 | + if (!isset($trans[$charset])) { |
|
| 429 | + $trans[$charset] = []; |
|
| 430 | + $t = &$trans[$charset]; |
|
| 431 | + for ($e = 128; $e < 255; $e++) { |
|
| 432 | + $h = dechex($e); |
|
| 433 | + if ($s = isset($CHARSET_REVERSE[$charset][$e])) { |
|
| 434 | + $s = $CHARSET_REVERSE[$charset][$e]; |
|
| 435 | + $t['&#' . $e . ';'] = $t['�' . $e . ';'] = $t['�' . $e . ';'] = chr($s); |
|
| 436 | + $t['&#x' . $h . ';'] = $t['�' . $h . ';'] = $t['�' . $h . ';'] = chr($s); |
|
| 437 | + } else { |
|
| 438 | + $t['&#' . $e . ';'] = $t['�' . $e . ';'] = $t['�' . $e . ';'] = chr($e); |
|
| 439 | + $t['&#x' . $h . ';'] = $t['�' . $h . ';'] = $t['�' . $h . ';'] = chr($e); |
|
| 440 | + } |
|
| 441 | + } |
|
| 442 | + } |
|
| 443 | + |
|
| 444 | + return str_replace(array_keys($trans[$charset]), array_values($trans[$charset]), $texte); |
|
| 445 | + } |
|
| 446 | 446 | } |
| 447 | 447 | |
| 448 | 448 | |
@@ -460,39 +460,39 @@ discard block |
||
| 460 | 460 | * texte transformé dans le charset site |
| 461 | 461 | **/ |
| 462 | 462 | function importer_charset($texte, $charset = 'AUTO') { |
| 463 | - $s = null; |
|
| 464 | - static $trans = []; |
|
| 465 | - // on traite le cas le plus frequent iso-8859-1 vers utf directement pour aller plus vite ! |
|
| 466 | - if (($charset == 'iso-8859-1') && ($GLOBALS['meta']['charset'] == 'utf-8')) { |
|
| 467 | - $texte = corriger_caracteres_windows($texte, 'iso-8859-1', $GLOBALS['meta']['charset']); |
|
| 468 | - if (init_mb_string()) { |
|
| 469 | - if ( |
|
| 470 | - ($order = mb_detect_order()) |
|
| 471 | - && mb_detect_order($charset) |
|
| 472 | - ) { |
|
| 473 | - $s = mb_convert_encoding($texte, 'utf-8', $charset); |
|
| 474 | - } |
|
| 475 | - mb_detect_order($order); # remettre comme precedemment |
|
| 476 | - return $s; |
|
| 477 | - } |
|
| 478 | - // Sinon, peut-etre connaissons-nous ce charset ? |
|
| 479 | - if ( |
|
| 480 | - !isset($trans[$charset]) |
|
| 481 | - && (($cset = load_charset($charset)) |
|
| 482 | - && is_array($GLOBALS['CHARSET'][$cset])) |
|
| 483 | - ) { |
|
| 484 | - foreach ($GLOBALS['CHARSET'][$cset] as $key => $val) { |
|
| 485 | - $trans[$charset][chr($key)] = unicode2charset('&#' . $val . ';'); |
|
| 486 | - } |
|
| 487 | - } |
|
| 488 | - if (is_countable($trans[$charset]) ? count($trans[$charset]) : 0) { |
|
| 489 | - return str_replace(array_keys($trans[$charset]), array_values($trans[$charset]), $texte); |
|
| 490 | - } |
|
| 491 | - |
|
| 492 | - return $texte; |
|
| 493 | - } |
|
| 494 | - |
|
| 495 | - return unicode2charset(charset2unicode($texte, $charset)); |
|
| 463 | + $s = null; |
|
| 464 | + static $trans = []; |
|
| 465 | + // on traite le cas le plus frequent iso-8859-1 vers utf directement pour aller plus vite ! |
|
| 466 | + if (($charset == 'iso-8859-1') && ($GLOBALS['meta']['charset'] == 'utf-8')) { |
|
| 467 | + $texte = corriger_caracteres_windows($texte, 'iso-8859-1', $GLOBALS['meta']['charset']); |
|
| 468 | + if (init_mb_string()) { |
|
| 469 | + if ( |
|
| 470 | + ($order = mb_detect_order()) |
|
| 471 | + && mb_detect_order($charset) |
|
| 472 | + ) { |
|
| 473 | + $s = mb_convert_encoding($texte, 'utf-8', $charset); |
|
| 474 | + } |
|
| 475 | + mb_detect_order($order); # remettre comme precedemment |
|
| 476 | + return $s; |
|
| 477 | + } |
|
| 478 | + // Sinon, peut-etre connaissons-nous ce charset ? |
|
| 479 | + if ( |
|
| 480 | + !isset($trans[$charset]) |
|
| 481 | + && (($cset = load_charset($charset)) |
|
| 482 | + && is_array($GLOBALS['CHARSET'][$cset])) |
|
| 483 | + ) { |
|
| 484 | + foreach ($GLOBALS['CHARSET'][$cset] as $key => $val) { |
|
| 485 | + $trans[$charset][chr($key)] = unicode2charset('&#' . $val . ';'); |
|
| 486 | + } |
|
| 487 | + } |
|
| 488 | + if (is_countable($trans[$charset]) ? count($trans[$charset]) : 0) { |
|
| 489 | + return str_replace(array_keys($trans[$charset]), array_values($trans[$charset]), $texte); |
|
| 490 | + } |
|
| 491 | + |
|
| 492 | + return $texte; |
|
| 493 | + } |
|
| 494 | + |
|
| 495 | + return unicode2charset(charset2unicode($texte, $charset)); |
|
| 496 | 496 | } |
| 497 | 497 | |
| 498 | 498 | |
@@ -508,92 +508,92 @@ discard block |
||
| 508 | 508 | **/ |
| 509 | 509 | function utf_8_to_unicode($source) { |
| 510 | 510 | |
| 511 | - // mb_string : methode rapide |
|
| 512 | - if (init_mb_string()) { |
|
| 513 | - $convmap = [0x7F, 0xFFFFFF, 0x0, 0xFFFFFF]; |
|
| 514 | - |
|
| 515 | - return mb_encode_numericentity($source, $convmap, 'UTF-8'); |
|
| 516 | - } |
|
| 517 | - |
|
| 518 | - // Sinon methode pas a pas |
|
| 519 | - static $decrement; |
|
| 520 | - static $shift; |
|
| 521 | - |
|
| 522 | - // Cf. php.net, par Ronen. Adapte pour compatibilite < php4 |
|
| 523 | - if (!is_array($decrement)) { |
|
| 524 | - // array used to figure what number to decrement from character order value |
|
| 525 | - // according to number of characters used to map unicode to ascii by utf-8 |
|
| 526 | - $decrement[4] = 240; |
|
| 527 | - $decrement[3] = 224; |
|
| 528 | - $decrement[2] = 192; |
|
| 529 | - $decrement[1] = 0; |
|
| 530 | - // the number of bits to shift each charNum by |
|
| 531 | - $shift[1][0] = 0; |
|
| 532 | - $shift[2][0] = 6; |
|
| 533 | - $shift[2][1] = 0; |
|
| 534 | - $shift[3][0] = 12; |
|
| 535 | - $shift[3][1] = 6; |
|
| 536 | - $shift[3][2] = 0; |
|
| 537 | - $shift[4][0] = 18; |
|
| 538 | - $shift[4][1] = 12; |
|
| 539 | - $shift[4][2] = 6; |
|
| 540 | - $shift[4][3] = 0; |
|
| 541 | - } |
|
| 542 | - |
|
| 543 | - $pos = 0; |
|
| 544 | - $len = strlen($source); |
|
| 545 | - $encodedString = ''; |
|
| 546 | - while ($pos < $len) { |
|
| 547 | - $char = ''; |
|
| 548 | - $ischar = false; |
|
| 549 | - $asciiPos = ord(substr($source, $pos, 1)); |
|
| 550 | - if (($asciiPos >= 240) && ($asciiPos <= 255)) { |
|
| 551 | - // 4 chars representing one unicode character |
|
| 552 | - $thisLetter = substr($source, $pos, 4); |
|
| 553 | - $pos += 4; |
|
| 554 | - } else { |
|
| 555 | - if (($asciiPos >= 224) && ($asciiPos <= 239)) { |
|
| 556 | - // 3 chars representing one unicode character |
|
| 557 | - $thisLetter = substr($source, $pos, 3); |
|
| 558 | - $pos += 3; |
|
| 559 | - } else { |
|
| 560 | - if (($asciiPos >= 192) && ($asciiPos <= 223)) { |
|
| 561 | - // 2 chars representing one unicode character |
|
| 562 | - $thisLetter = substr($source, $pos, 2); |
|
| 563 | - $pos += 2; |
|
| 564 | - } else { |
|
| 565 | - // 1 char (lower ascii) |
|
| 566 | - $thisLetter = substr($source, $pos, 1); |
|
| 567 | - $pos += 1; |
|
| 568 | - $char = $thisLetter; |
|
| 569 | - $ischar = true; |
|
| 570 | - } |
|
| 571 | - } |
|
| 572 | - } |
|
| 573 | - |
|
| 574 | - if ($ischar) { |
|
| 575 | - $encodedString .= $char; |
|
| 576 | - } else { // process the string representing the letter to a unicode entity |
|
| 577 | - $thisLen = strlen($thisLetter); |
|
| 578 | - $thisPos = 0; |
|
| 579 | - $decimalCode = 0; |
|
| 580 | - while ($thisPos < $thisLen) { |
|
| 581 | - $thisCharOrd = ord(substr($thisLetter, $thisPos, 1)); |
|
| 582 | - if ($thisPos == 0) { |
|
| 583 | - $charNum = (int) ($thisCharOrd - $decrement[$thisLen]); |
|
| 584 | - $decimalCode += ($charNum << $shift[$thisLen][$thisPos]); |
|
| 585 | - } else { |
|
| 586 | - $charNum = (int) ($thisCharOrd - 128); |
|
| 587 | - $decimalCode += ($charNum << $shift[$thisLen][$thisPos]); |
|
| 588 | - } |
|
| 589 | - $thisPos++; |
|
| 590 | - } |
|
| 591 | - $encodedLetter = '&#' . preg_replace('/^0+/', '', $decimalCode) . ';'; |
|
| 592 | - $encodedString .= $encodedLetter; |
|
| 593 | - } |
|
| 594 | - } |
|
| 595 | - |
|
| 596 | - return $encodedString; |
|
| 511 | + // mb_string : methode rapide |
|
| 512 | + if (init_mb_string()) { |
|
| 513 | + $convmap = [0x7F, 0xFFFFFF, 0x0, 0xFFFFFF]; |
|
| 514 | + |
|
| 515 | + return mb_encode_numericentity($source, $convmap, 'UTF-8'); |
|
| 516 | + } |
|
| 517 | + |
|
| 518 | + // Sinon methode pas a pas |
|
| 519 | + static $decrement; |
|
| 520 | + static $shift; |
|
| 521 | + |
|
| 522 | + // Cf. php.net, par Ronen. Adapte pour compatibilite < php4 |
|
| 523 | + if (!is_array($decrement)) { |
|
| 524 | + // array used to figure what number to decrement from character order value |
|
| 525 | + // according to number of characters used to map unicode to ascii by utf-8 |
|
| 526 | + $decrement[4] = 240; |
|
| 527 | + $decrement[3] = 224; |
|
| 528 | + $decrement[2] = 192; |
|
| 529 | + $decrement[1] = 0; |
|
| 530 | + // the number of bits to shift each charNum by |
|
| 531 | + $shift[1][0] = 0; |
|
| 532 | + $shift[2][0] = 6; |
|
| 533 | + $shift[2][1] = 0; |
|
| 534 | + $shift[3][0] = 12; |
|
| 535 | + $shift[3][1] = 6; |
|
| 536 | + $shift[3][2] = 0; |
|
| 537 | + $shift[4][0] = 18; |
|
| 538 | + $shift[4][1] = 12; |
|
| 539 | + $shift[4][2] = 6; |
|
| 540 | + $shift[4][3] = 0; |
|
| 541 | + } |
|
| 542 | + |
|
| 543 | + $pos = 0; |
|
| 544 | + $len = strlen($source); |
|
| 545 | + $encodedString = ''; |
|
| 546 | + while ($pos < $len) { |
|
| 547 | + $char = ''; |
|
| 548 | + $ischar = false; |
|
| 549 | + $asciiPos = ord(substr($source, $pos, 1)); |
|
| 550 | + if (($asciiPos >= 240) && ($asciiPos <= 255)) { |
|
| 551 | + // 4 chars representing one unicode character |
|
| 552 | + $thisLetter = substr($source, $pos, 4); |
|
| 553 | + $pos += 4; |
|
| 554 | + } else { |
|
| 555 | + if (($asciiPos >= 224) && ($asciiPos <= 239)) { |
|
| 556 | + // 3 chars representing one unicode character |
|
| 557 | + $thisLetter = substr($source, $pos, 3); |
|
| 558 | + $pos += 3; |
|
| 559 | + } else { |
|
| 560 | + if (($asciiPos >= 192) && ($asciiPos <= 223)) { |
|
| 561 | + // 2 chars representing one unicode character |
|
| 562 | + $thisLetter = substr($source, $pos, 2); |
|
| 563 | + $pos += 2; |
|
| 564 | + } else { |
|
| 565 | + // 1 char (lower ascii) |
|
| 566 | + $thisLetter = substr($source, $pos, 1); |
|
| 567 | + $pos += 1; |
|
| 568 | + $char = $thisLetter; |
|
| 569 | + $ischar = true; |
|
| 570 | + } |
|
| 571 | + } |
|
| 572 | + } |
|
| 573 | + |
|
| 574 | + if ($ischar) { |
|
| 575 | + $encodedString .= $char; |
|
| 576 | + } else { // process the string representing the letter to a unicode entity |
|
| 577 | + $thisLen = strlen($thisLetter); |
|
| 578 | + $thisPos = 0; |
|
| 579 | + $decimalCode = 0; |
|
| 580 | + while ($thisPos < $thisLen) { |
|
| 581 | + $thisCharOrd = ord(substr($thisLetter, $thisPos, 1)); |
|
| 582 | + if ($thisPos == 0) { |
|
| 583 | + $charNum = (int) ($thisCharOrd - $decrement[$thisLen]); |
|
| 584 | + $decimalCode += ($charNum << $shift[$thisLen][$thisPos]); |
|
| 585 | + } else { |
|
| 586 | + $charNum = (int) ($thisCharOrd - 128); |
|
| 587 | + $decimalCode += ($charNum << $shift[$thisLen][$thisPos]); |
|
| 588 | + } |
|
| 589 | + $thisPos++; |
|
| 590 | + } |
|
| 591 | + $encodedLetter = '&#' . preg_replace('/^0+/', '', $decimalCode) . ';'; |
|
| 592 | + $encodedString .= $encodedLetter; |
|
| 593 | + } |
|
| 594 | + } |
|
| 595 | + |
|
| 596 | + return $encodedString; |
|
| 597 | 597 | } |
| 598 | 598 | |
| 599 | 599 | /** |
@@ -612,32 +612,32 @@ discard block |
||
| 612 | 612 | **/ |
| 613 | 613 | function utf_32_to_unicode($source) { |
| 614 | 614 | |
| 615 | - // mb_string : methode rapide |
|
| 616 | - if (init_mb_string()) { |
|
| 617 | - $convmap = [0x7F, 0xFFFFFF, 0x0, 0xFFFFFF]; |
|
| 618 | - $source = mb_encode_numericentity($source, $convmap, 'UTF-32LE'); |
|
| 619 | - |
|
| 620 | - return str_replace(chr(0), '', $source); |
|
| 621 | - } |
|
| 622 | - |
|
| 623 | - // Sinon methode lente |
|
| 624 | - $texte = ''; |
|
| 625 | - while ($source) { |
|
| 626 | - $words = unpack('V*', substr($source, 0, 1024)); |
|
| 627 | - $source = substr($source, 1024); |
|
| 628 | - foreach ($words as $word) { |
|
| 629 | - if ($word < 128) { |
|
| 630 | - $texte .= chr($word); |
|
| 631 | - } // ignorer le BOM - http://www.unicode.org/faq/utf_bom.html |
|
| 632 | - else { |
|
| 633 | - if ($word != 65279) { |
|
| 634 | - $texte .= '&#' . $word . ';'; |
|
| 635 | - } |
|
| 636 | - } |
|
| 637 | - } |
|
| 638 | - } |
|
| 639 | - |
|
| 640 | - return $texte; |
|
| 615 | + // mb_string : methode rapide |
|
| 616 | + if (init_mb_string()) { |
|
| 617 | + $convmap = [0x7F, 0xFFFFFF, 0x0, 0xFFFFFF]; |
|
| 618 | + $source = mb_encode_numericentity($source, $convmap, 'UTF-32LE'); |
|
| 619 | + |
|
| 620 | + return str_replace(chr(0), '', $source); |
|
| 621 | + } |
|
| 622 | + |
|
| 623 | + // Sinon methode lente |
|
| 624 | + $texte = ''; |
|
| 625 | + while ($source) { |
|
| 626 | + $words = unpack('V*', substr($source, 0, 1024)); |
|
| 627 | + $source = substr($source, 1024); |
|
| 628 | + foreach ($words as $word) { |
|
| 629 | + if ($word < 128) { |
|
| 630 | + $texte .= chr($word); |
|
| 631 | + } // ignorer le BOM - http://www.unicode.org/faq/utf_bom.html |
|
| 632 | + else { |
|
| 633 | + if ($word != 65279) { |
|
| 634 | + $texte .= '&#' . $word . ';'; |
|
| 635 | + } |
|
| 636 | + } |
|
| 637 | + } |
|
| 638 | + } |
|
| 639 | + |
|
| 640 | + return $texte; |
|
| 641 | 641 | } |
| 642 | 642 | |
| 643 | 643 | |
@@ -654,21 +654,21 @@ discard block |
||
| 654 | 654 | * Caractère utf8 si trouvé, '' sinon |
| 655 | 655 | **/ |
| 656 | 656 | function caractere_utf_8($num) { |
| 657 | - $num = (int) $num; |
|
| 658 | - if ($num < 128) { |
|
| 659 | - return chr($num); |
|
| 660 | - } |
|
| 661 | - if ($num < 2048) { |
|
| 662 | - return chr(($num >> 6) + 192) . chr(($num & 63) + 128); |
|
| 663 | - } |
|
| 664 | - if ($num < 65536) { |
|
| 665 | - return chr(($num >> 12) + 224) . chr((($num >> 6) & 63) + 128) . chr(($num & 63) + 128); |
|
| 666 | - } |
|
| 667 | - if ($num < 1_114_112) { |
|
| 668 | - return chr(($num >> 18) + 240) . chr((($num >> 12) & 63) + 128) . chr((($num >> 6) & 63) + 128) . chr(($num & 63) + 128); |
|
| 669 | - } |
|
| 670 | - |
|
| 671 | - return ''; |
|
| 657 | + $num = (int) $num; |
|
| 658 | + if ($num < 128) { |
|
| 659 | + return chr($num); |
|
| 660 | + } |
|
| 661 | + if ($num < 2048) { |
|
| 662 | + return chr(($num >> 6) + 192) . chr(($num & 63) + 128); |
|
| 663 | + } |
|
| 664 | + if ($num < 65536) { |
|
| 665 | + return chr(($num >> 12) + 224) . chr((($num >> 6) & 63) + 128) . chr(($num & 63) + 128); |
|
| 666 | + } |
|
| 667 | + if ($num < 1_114_112) { |
|
| 668 | + return chr(($num >> 18) + 240) . chr((($num >> 12) & 63) + 128) . chr((($num >> 6) & 63) + 128) . chr(($num & 63) + 128); |
|
| 669 | + } |
|
| 670 | + |
|
| 671 | + return ''; |
|
| 672 | 672 | } |
| 673 | 673 | |
| 674 | 674 | /** |
@@ -681,42 +681,42 @@ discard block |
||
| 681 | 681 | **/ |
| 682 | 682 | function unicode_to_utf_8($texte) { |
| 683 | 683 | |
| 684 | - // 1. Entites € et suivantes |
|
| 685 | - $vu = []; |
|
| 686 | - if ( |
|
| 687 | - preg_match_all( |
|
| 688 | - ',�*([1-9]\d\d+);,S', |
|
| 689 | - $texte, |
|
| 690 | - $regs, |
|
| 691 | - PREG_SET_ORDER |
|
| 692 | - ) |
|
| 693 | - ) { |
|
| 694 | - foreach ($regs as $reg) { |
|
| 695 | - if ($reg[1] > 127 && !isset($vu[$reg[0]])) { |
|
| 696 | - $vu[$reg[0]] = caractere_utf_8($reg[1]); |
|
| 697 | - } |
|
| 698 | - } |
|
| 699 | - } |
|
| 700 | - //$texte = str_replace(array_keys($vu), array_values($vu), $texte); |
|
| 701 | - |
|
| 702 | - // 2. Entites > ÿ |
|
| 703 | - //$vu = array(); |
|
| 704 | - if ( |
|
| 705 | - preg_match_all( |
|
| 706 | - ',�*([1-9a-f][0-9a-f][0-9a-f]+);,iS', |
|
| 707 | - $texte, |
|
| 708 | - $regs, |
|
| 709 | - PREG_SET_ORDER |
|
| 710 | - ) |
|
| 711 | - ) { |
|
| 712 | - foreach ($regs as $reg) { |
|
| 713 | - if (!isset($vu[$reg[0]])) { |
|
| 714 | - $vu[$reg[0]] = caractere_utf_8(hexdec($reg[1])); |
|
| 715 | - } |
|
| 716 | - } |
|
| 717 | - } |
|
| 718 | - |
|
| 719 | - return str_replace(array_keys($vu), array_values($vu), $texte); |
|
| 684 | + // 1. Entites € et suivantes |
|
| 685 | + $vu = []; |
|
| 686 | + if ( |
|
| 687 | + preg_match_all( |
|
| 688 | + ',�*([1-9]\d\d+);,S', |
|
| 689 | + $texte, |
|
| 690 | + $regs, |
|
| 691 | + PREG_SET_ORDER |
|
| 692 | + ) |
|
| 693 | + ) { |
|
| 694 | + foreach ($regs as $reg) { |
|
| 695 | + if ($reg[1] > 127 && !isset($vu[$reg[0]])) { |
|
| 696 | + $vu[$reg[0]] = caractere_utf_8($reg[1]); |
|
| 697 | + } |
|
| 698 | + } |
|
| 699 | + } |
|
| 700 | + //$texte = str_replace(array_keys($vu), array_values($vu), $texte); |
|
| 701 | + |
|
| 702 | + // 2. Entites > ÿ |
|
| 703 | + //$vu = array(); |
|
| 704 | + if ( |
|
| 705 | + preg_match_all( |
|
| 706 | + ',�*([1-9a-f][0-9a-f][0-9a-f]+);,iS', |
|
| 707 | + $texte, |
|
| 708 | + $regs, |
|
| 709 | + PREG_SET_ORDER |
|
| 710 | + ) |
|
| 711 | + ) { |
|
| 712 | + foreach ($regs as $reg) { |
|
| 713 | + if (!isset($vu[$reg[0]])) { |
|
| 714 | + $vu[$reg[0]] = caractere_utf_8(hexdec($reg[1])); |
|
| 715 | + } |
|
| 716 | + } |
|
| 717 | + } |
|
| 718 | + |
|
| 719 | + return str_replace(array_keys($vu), array_values($vu), $texte); |
|
| 720 | 720 | } |
| 721 | 721 | |
| 722 | 722 | /** |
@@ -728,15 +728,15 @@ discard block |
||
| 728 | 728 | * texte converti |
| 729 | 729 | **/ |
| 730 | 730 | function unicode_to_javascript($texte) { |
| 731 | - $vu = []; |
|
| 732 | - while (preg_match(',�*(\d+);,S', $texte, $regs) && !isset($vu[$regs[1]])) { |
|
| 733 | - $num = $regs[1]; |
|
| 734 | - $vu[$num] = true; |
|
| 735 | - $s = '\u' . sprintf('%04x', $num); |
|
| 736 | - $texte = str_replace($regs[0], $s, $texte); |
|
| 737 | - } |
|
| 738 | - |
|
| 739 | - return $texte; |
|
| 731 | + $vu = []; |
|
| 732 | + while (preg_match(',�*(\d+);,S', $texte, $regs) && !isset($vu[$regs[1]])) { |
|
| 733 | + $num = $regs[1]; |
|
| 734 | + $vu[$num] = true; |
|
| 735 | + $s = '\u' . sprintf('%04x', $num); |
|
| 736 | + $texte = str_replace($regs[0], $s, $texte); |
|
| 737 | + } |
|
| 738 | + |
|
| 739 | + return $texte; |
|
| 740 | 740 | } |
| 741 | 741 | |
| 742 | 742 | /** |
@@ -748,11 +748,11 @@ discard block |
||
| 748 | 748 | * texte converti |
| 749 | 749 | **/ |
| 750 | 750 | function javascript_to_unicode($texte) { |
| 751 | - while (preg_match(',%u([0-9A-F][0-9A-F][0-9A-F][0-9A-F]),', $texte, $regs)) { |
|
| 752 | - $texte = str_replace($regs[0], '&#' . hexdec($regs[1]) . ';', $texte); |
|
| 753 | - } |
|
| 751 | + while (preg_match(',%u([0-9A-F][0-9A-F][0-9A-F][0-9A-F]),', $texte, $regs)) { |
|
| 752 | + $texte = str_replace($regs[0], '&#' . hexdec($regs[1]) . ';', $texte); |
|
| 753 | + } |
|
| 754 | 754 | |
| 755 | - return $texte; |
|
| 755 | + return $texte; |
|
| 756 | 756 | } |
| 757 | 757 | |
| 758 | 758 | /** |
@@ -764,11 +764,11 @@ discard block |
||
| 764 | 764 | * texte converti |
| 765 | 765 | **/ |
| 766 | 766 | function javascript_to_binary($texte) { |
| 767 | - while (preg_match(',%([0-9A-F][0-9A-F]),', $texte, $regs)) { |
|
| 768 | - $texte = str_replace($regs[0], chr(hexdec($regs[1])), $texte); |
|
| 769 | - } |
|
| 767 | + while (preg_match(',%([0-9A-F][0-9A-F]),', $texte, $regs)) { |
|
| 768 | + $texte = str_replace($regs[0], chr(hexdec($regs[1])), $texte); |
|
| 769 | + } |
|
| 770 | 770 | |
| 771 | - return $texte; |
|
| 771 | + return $texte; |
|
| 772 | 772 | } |
| 773 | 773 | |
| 774 | 774 | |
@@ -786,26 +786,26 @@ discard block |
||
| 786 | 786 | * @return string |
| 787 | 787 | */ |
| 788 | 788 | function translitteration_rapide(?string $texte, string $charset = 'AUTO', string $complexe = ''): string { |
| 789 | - static $trans = []; |
|
| 790 | - if ($charset == 'AUTO') { |
|
| 791 | - $charset = $GLOBALS['meta']['charset']; |
|
| 792 | - } |
|
| 793 | - if ($texte === null || $texte === '') { |
|
| 794 | - return ''; |
|
| 795 | - } |
|
| 796 | - |
|
| 797 | - $table_translit = 'translit' . $complexe; |
|
| 798 | - |
|
| 799 | - // 2. Translitterer grace a la table predefinie |
|
| 800 | - if (!isset($trans[$complexe])) { |
|
| 801 | - $trans[$complexe] = []; |
|
| 802 | - load_charset($table_translit); |
|
| 803 | - foreach ($GLOBALS['CHARSET'][$table_translit] as $key => $val) { |
|
| 804 | - $trans[$complexe][caractere_utf_8($key)] = $val; |
|
| 805 | - } |
|
| 806 | - } |
|
| 807 | - |
|
| 808 | - return str_replace(array_keys($trans[$complexe]), array_values($trans[$complexe]), $texte); |
|
| 789 | + static $trans = []; |
|
| 790 | + if ($charset == 'AUTO') { |
|
| 791 | + $charset = $GLOBALS['meta']['charset']; |
|
| 792 | + } |
|
| 793 | + if ($texte === null || $texte === '') { |
|
| 794 | + return ''; |
|
| 795 | + } |
|
| 796 | + |
|
| 797 | + $table_translit = 'translit' . $complexe; |
|
| 798 | + |
|
| 799 | + // 2. Translitterer grace a la table predefinie |
|
| 800 | + if (!isset($trans[$complexe])) { |
|
| 801 | + $trans[$complexe] = []; |
|
| 802 | + load_charset($table_translit); |
|
| 803 | + foreach ($GLOBALS['CHARSET'][$table_translit] as $key => $val) { |
|
| 804 | + $trans[$complexe][caractere_utf_8($key)] = $val; |
|
| 805 | + } |
|
| 806 | + } |
|
| 807 | + |
|
| 808 | + return str_replace(array_keys($trans[$complexe]), array_values($trans[$complexe]), $texte); |
|
| 809 | 809 | } |
| 810 | 810 | |
| 811 | 811 | /** |
@@ -828,18 +828,18 @@ discard block |
||
| 828 | 828 | * @return string |
| 829 | 829 | */ |
| 830 | 830 | function translitteration(?string $texte, string $charset = 'AUTO', string $complexe = ''): string { |
| 831 | - if ($texte === null || $texte === '') { |
|
| 832 | - return ''; |
|
| 833 | - } |
|
| 831 | + if ($texte === null || $texte === '') { |
|
| 832 | + return ''; |
|
| 833 | + } |
|
| 834 | 834 | |
| 835 | - // 0. Supprimer les caracteres illegaux |
|
| 836 | - include_spip('inc/filtres'); |
|
| 837 | - $texte = corriger_caracteres($texte); |
|
| 835 | + // 0. Supprimer les caracteres illegaux |
|
| 836 | + include_spip('inc/filtres'); |
|
| 837 | + $texte = corriger_caracteres($texte); |
|
| 838 | 838 | |
| 839 | - // 1. Passer le charset et les é en utf-8 |
|
| 840 | - $texte = unicode_to_utf_8(html2unicode(charset2unicode($texte, $charset))); |
|
| 839 | + // 1. Passer le charset et les é en utf-8 |
|
| 840 | + $texte = unicode_to_utf_8(html2unicode(charset2unicode($texte, $charset))); |
|
| 841 | 841 | |
| 842 | - return translitteration_rapide($texte, $charset, $complexe); |
|
| 842 | + return translitteration_rapide($texte, $charset, $complexe); |
|
| 843 | 843 | } |
| 844 | 844 | |
| 845 | 845 | /** |
@@ -854,17 +854,17 @@ discard block |
||
| 854 | 854 | * @return string |
| 855 | 855 | */ |
| 856 | 856 | function translitteration_complexe(?string $texte, bool $chiffres = false): string { |
| 857 | - $texte = translitteration($texte, 'AUTO', 'complexe'); |
|
| 857 | + $texte = translitteration($texte, 'AUTO', 'complexe'); |
|
| 858 | 858 | |
| 859 | - if ($chiffres) { |
|
| 860 | - $texte = preg_replace_callback( |
|
| 861 | - "/[aeiuoyd]['`?~.^+(-]{1,2}/S", |
|
| 862 | - fn($m) => translitteration_chiffree($m[0]), |
|
| 863 | - $texte |
|
| 864 | - ); |
|
| 865 | - } |
|
| 859 | + if ($chiffres) { |
|
| 860 | + $texte = preg_replace_callback( |
|
| 861 | + "/[aeiuoyd]['`?~.^+(-]{1,2}/S", |
|
| 862 | + fn($m) => translitteration_chiffree($m[0]), |
|
| 863 | + $texte |
|
| 864 | + ); |
|
| 865 | + } |
|
| 866 | 866 | |
| 867 | - return $texte; |
|
| 867 | + return $texte; |
|
| 868 | 868 | } |
| 869 | 869 | |
| 870 | 870 | /** |
@@ -876,7 +876,7 @@ discard block |
||
| 876 | 876 | * @return string |
| 877 | 877 | */ |
| 878 | 878 | function translitteration_chiffree(string $car): string { |
| 879 | - return strtr($car, "'`?~.^+(-", '123456789'); |
|
| 879 | + return strtr($car, "'`?~.^+(-", '123456789'); |
|
| 880 | 880 | } |
| 881 | 881 | |
| 882 | 882 | |
@@ -889,7 +889,7 @@ discard block |
||
| 889 | 889 | * true s'il a un BOM |
| 890 | 890 | **/ |
| 891 | 891 | function bom_utf8($texte): bool { |
| 892 | - return (substr($texte, 0, 3) === chr(0xEF) . chr(0xBB) . chr(0xBF)); |
|
| 892 | + return (substr($texte, 0, 3) === chr(0xEF) . chr(0xBB) . chr(0xBF)); |
|
| 893 | 893 | } |
| 894 | 894 | |
| 895 | 895 | /** |
@@ -906,21 +906,21 @@ discard block |
||
| 906 | 906 | * true si c'est le cas |
| 907 | 907 | **/ |
| 908 | 908 | function is_utf8($string): bool { |
| 909 | - return !strlen( |
|
| 910 | - preg_replace( |
|
| 911 | - ',[\x09\x0A\x0D\x20-\x7E]' # ASCII |
|
| 912 | - . '|[\xC2-\xDF][\x80-\xBF]' # non-overlong 2-byte |
|
| 913 | - . '|\xE0[\xA0-\xBF][\x80-\xBF]' # excluding overlongs |
|
| 914 | - . '|[\xE1-\xEC\xEE\xEF][\x80-\xBF]{2}' # straight 3-byte |
|
| 915 | - . '|\xED[\x80-\x9F][\x80-\xBF]' # excluding surrogates |
|
| 916 | - . '|\xF0[\x90-\xBF][\x80-\xBF]{2}' # planes 1-3 |
|
| 917 | - . '|[\xF1-\xF3][\x80-\xBF]{3}' # planes 4-15 |
|
| 918 | - . '|\xF4[\x80-\x8F][\x80-\xBF]{2}' # plane 16 |
|
| 919 | - . ',sS', |
|
| 920 | - '', |
|
| 921 | - $string |
|
| 922 | - ) |
|
| 923 | - ); |
|
| 909 | + return !strlen( |
|
| 910 | + preg_replace( |
|
| 911 | + ',[\x09\x0A\x0D\x20-\x7E]' # ASCII |
|
| 912 | + . '|[\xC2-\xDF][\x80-\xBF]' # non-overlong 2-byte |
|
| 913 | + . '|\xE0[\xA0-\xBF][\x80-\xBF]' # excluding overlongs |
|
| 914 | + . '|[\xE1-\xEC\xEE\xEF][\x80-\xBF]{2}' # straight 3-byte |
|
| 915 | + . '|\xED[\x80-\x9F][\x80-\xBF]' # excluding surrogates |
|
| 916 | + . '|\xF0[\x90-\xBF][\x80-\xBF]{2}' # planes 1-3 |
|
| 917 | + . '|[\xF1-\xF3][\x80-\xBF]{3}' # planes 4-15 |
|
| 918 | + . '|\xF4[\x80-\x8F][\x80-\xBF]{2}' # plane 16 |
|
| 919 | + . ',sS', |
|
| 920 | + '', |
|
| 921 | + $string |
|
| 922 | + ) |
|
| 923 | + ); |
|
| 924 | 924 | } |
| 925 | 925 | |
| 926 | 926 | /** |
@@ -932,13 +932,13 @@ discard block |
||
| 932 | 932 | * true si c'est le cas |
| 933 | 933 | **/ |
| 934 | 934 | function is_ascii($string): bool { |
| 935 | - return !strlen( |
|
| 936 | - preg_replace( |
|
| 937 | - ',[\x09\x0A\x0D\x20-\x7E],sS', |
|
| 938 | - '', |
|
| 939 | - $string |
|
| 940 | - ) |
|
| 941 | - ); |
|
| 935 | + return !strlen( |
|
| 936 | + preg_replace( |
|
| 937 | + ',[\x09\x0A\x0D\x20-\x7E],sS', |
|
| 938 | + '', |
|
| 939 | + $string |
|
| 940 | + ) |
|
| 941 | + ); |
|
| 942 | 942 | } |
| 943 | 943 | |
| 944 | 944 | /** |
@@ -957,50 +957,50 @@ discard block |
||
| 957 | 957 | **/ |
| 958 | 958 | function transcoder_page($texte, $headers = ''): string { |
| 959 | 959 | |
| 960 | - // Si tout est < 128 pas la peine d'aller plus loin |
|
| 961 | - if (is_ascii($texte)) { |
|
| 962 | - #spip_log('charset: ascii'); |
|
| 963 | - return $texte; |
|
| 964 | - } |
|
| 965 | - |
|
| 966 | - if (bom_utf8($texte)) { |
|
| 967 | - // Reconnaitre le BOM utf-8 (0xEFBBBF) |
|
| 968 | - $charset = 'utf-8'; |
|
| 969 | - $texte = substr($texte, 3); |
|
| 970 | - } elseif (preg_match(',<[?]xml[^>]*encoding[^>]*=[^>]*([-_a-z0-9]+?),UimsS', $texte, $regs)) { |
|
| 971 | - // charset precise par le contenu (xml) |
|
| 972 | - $charset = trim(strtolower($regs[1])); |
|
| 973 | - } elseif ( |
|
| 974 | - // charset precise par le contenu (html) |
|
| 975 | - preg_match(',<(meta|html|body)[^>]*charset[^>]*=[^>]*([#-_a-z0-9]+?),UimsS', $texte, $regs) |
|
| 976 | - # eviter toute balise SPIP tel que #CHARSET ou #CONFIG d'un squelette |
|
| 977 | - |
|
| 978 | - && !str_contains($regs[2], '#') |
|
| 979 | - && ($tmp = trim(strtolower($regs[2]))) |
|
| 980 | - ) { |
|
| 981 | - $charset = $tmp; |
|
| 982 | - } elseif (preg_match(',charset=([-_a-z0-9]+),i', $headers, $regs)) { |
|
| 983 | - // charset de la reponse http |
|
| 984 | - $charset = trim(strtolower($regs[1])); |
|
| 985 | - } else { |
|
| 986 | - $charset = ''; |
|
| 987 | - } |
|
| 988 | - |
|
| 989 | - |
|
| 990 | - // normaliser les noms du shif-jis japonais |
|
| 991 | - if (preg_match(',^(x|shift)[_-]s?jis$,i', $charset)) { |
|
| 992 | - $charset = 'shift-jis'; |
|
| 993 | - } |
|
| 994 | - |
|
| 995 | - if ($charset) { |
|
| 996 | - spip_log("charset: $charset"); |
|
| 997 | - } else { |
|
| 998 | - // valeur par defaut |
|
| 999 | - $charset = is_utf8($texte) ? 'utf-8' : 'iso-8859-1'; |
|
| 1000 | - spip_log("charset probable: $charset"); |
|
| 1001 | - } |
|
| 1002 | - |
|
| 1003 | - return importer_charset($texte, $charset); |
|
| 960 | + // Si tout est < 128 pas la peine d'aller plus loin |
|
| 961 | + if (is_ascii($texte)) { |
|
| 962 | + #spip_log('charset: ascii'); |
|
| 963 | + return $texte; |
|
| 964 | + } |
|
| 965 | + |
|
| 966 | + if (bom_utf8($texte)) { |
|
| 967 | + // Reconnaitre le BOM utf-8 (0xEFBBBF) |
|
| 968 | + $charset = 'utf-8'; |
|
| 969 | + $texte = substr($texte, 3); |
|
| 970 | + } elseif (preg_match(',<[?]xml[^>]*encoding[^>]*=[^>]*([-_a-z0-9]+?),UimsS', $texte, $regs)) { |
|
| 971 | + // charset precise par le contenu (xml) |
|
| 972 | + $charset = trim(strtolower($regs[1])); |
|
| 973 | + } elseif ( |
|
| 974 | + // charset precise par le contenu (html) |
|
| 975 | + preg_match(',<(meta|html|body)[^>]*charset[^>]*=[^>]*([#-_a-z0-9]+?),UimsS', $texte, $regs) |
|
| 976 | + # eviter toute balise SPIP tel que #CHARSET ou #CONFIG d'un squelette |
|
| 977 | + |
|
| 978 | + && !str_contains($regs[2], '#') |
|
| 979 | + && ($tmp = trim(strtolower($regs[2]))) |
|
| 980 | + ) { |
|
| 981 | + $charset = $tmp; |
|
| 982 | + } elseif (preg_match(',charset=([-_a-z0-9]+),i', $headers, $regs)) { |
|
| 983 | + // charset de la reponse http |
|
| 984 | + $charset = trim(strtolower($regs[1])); |
|
| 985 | + } else { |
|
| 986 | + $charset = ''; |
|
| 987 | + } |
|
| 988 | + |
|
| 989 | + |
|
| 990 | + // normaliser les noms du shif-jis japonais |
|
| 991 | + if (preg_match(',^(x|shift)[_-]s?jis$,i', $charset)) { |
|
| 992 | + $charset = 'shift-jis'; |
|
| 993 | + } |
|
| 994 | + |
|
| 995 | + if ($charset) { |
|
| 996 | + spip_log("charset: $charset"); |
|
| 997 | + } else { |
|
| 998 | + // valeur par defaut |
|
| 999 | + $charset = is_utf8($texte) ? 'utf-8' : 'iso-8859-1'; |
|
| 1000 | + spip_log("charset probable: $charset"); |
|
| 1001 | + } |
|
| 1002 | + |
|
| 1003 | + return importer_charset($texte, $charset); |
|
| 1004 | 1004 | } |
| 1005 | 1005 | |
| 1006 | 1006 | |
@@ -1024,19 +1024,19 @@ discard block |
||
| 1024 | 1024 | * Le texte coupé |
| 1025 | 1025 | **/ |
| 1026 | 1026 | function spip_substr($c, $start = 0, $length = null) { |
| 1027 | - if ($GLOBALS['meta']['charset'] !== 'utf-8') { |
|
| 1028 | - if ($length) { |
|
| 1029 | - return substr($c, $start, $length); |
|
| 1030 | - } else { |
|
| 1031 | - return substr($c, $start); |
|
| 1032 | - } |
|
| 1033 | - } |
|
| 1034 | - |
|
| 1035 | - if ($length) { |
|
| 1036 | - return mb_substr($c, $start, $length); |
|
| 1037 | - } else { |
|
| 1038 | - return mb_substr($c, $start); |
|
| 1039 | - } |
|
| 1027 | + if ($GLOBALS['meta']['charset'] !== 'utf-8') { |
|
| 1028 | + if ($length) { |
|
| 1029 | + return substr($c, $start, $length); |
|
| 1030 | + } else { |
|
| 1031 | + return substr($c, $start); |
|
| 1032 | + } |
|
| 1033 | + } |
|
| 1034 | + |
|
| 1035 | + if ($length) { |
|
| 1036 | + return mb_substr($c, $start, $length); |
|
| 1037 | + } else { |
|
| 1038 | + return mb_substr($c, $start); |
|
| 1039 | + } |
|
| 1040 | 1040 | } |
| 1041 | 1041 | |
| 1042 | 1042 | /** |
@@ -1050,13 +1050,13 @@ discard block |
||
| 1050 | 1050 | * La chaîne avec une majuscule sur le premier mot |
| 1051 | 1051 | */ |
| 1052 | 1052 | function spip_ucfirst($c) { |
| 1053 | - if ($GLOBALS['meta']['charset'] !== 'utf-8') { |
|
| 1054 | - return ucfirst($c); |
|
| 1055 | - } |
|
| 1053 | + if ($GLOBALS['meta']['charset'] !== 'utf-8') { |
|
| 1054 | + return ucfirst($c); |
|
| 1055 | + } |
|
| 1056 | 1056 | |
| 1057 | - $lettre1 = mb_strtoupper(spip_substr($c, 0, 1)); |
|
| 1057 | + $lettre1 = mb_strtoupper(spip_substr($c, 0, 1)); |
|
| 1058 | 1058 | |
| 1059 | - return $lettre1 . spip_substr($c, 1); |
|
| 1059 | + return $lettre1 . spip_substr($c, 1); |
|
| 1060 | 1060 | } |
| 1061 | 1061 | |
| 1062 | 1062 | /** |
@@ -1070,11 +1070,11 @@ discard block |
||
| 1070 | 1070 | * La chaîne en minuscules |
| 1071 | 1071 | */ |
| 1072 | 1072 | function spip_strtolower($c) { |
| 1073 | - if ($GLOBALS['meta']['charset'] !== 'utf-8') { |
|
| 1074 | - return strtolower($c); |
|
| 1075 | - } |
|
| 1073 | + if ($GLOBALS['meta']['charset'] !== 'utf-8') { |
|
| 1074 | + return strtolower($c); |
|
| 1075 | + } |
|
| 1076 | 1076 | |
| 1077 | - return mb_strtolower($c); |
|
| 1077 | + return mb_strtolower($c); |
|
| 1078 | 1078 | } |
| 1079 | 1079 | |
| 1080 | 1080 | /** |
@@ -1088,15 +1088,15 @@ discard block |
||
| 1088 | 1088 | * Longueur de la chaîne |
| 1089 | 1089 | */ |
| 1090 | 1090 | function spip_strlen($c) { |
| 1091 | - // On transforme les sauts de ligne pour ne pas compter deux caractères |
|
| 1092 | - $c = str_replace("\r\n", "\n", $c); |
|
| 1091 | + // On transforme les sauts de ligne pour ne pas compter deux caractères |
|
| 1092 | + $c = str_replace("\r\n", "\n", $c); |
|
| 1093 | 1093 | |
| 1094 | - // Si ce n'est pas utf-8, utiliser strlen |
|
| 1095 | - if ($GLOBALS['meta']['charset'] !== 'utf-8') { |
|
| 1096 | - return strlen($c); |
|
| 1097 | - } |
|
| 1094 | + // Si ce n'est pas utf-8, utiliser strlen |
|
| 1095 | + if ($GLOBALS['meta']['charset'] !== 'utf-8') { |
|
| 1096 | + return strlen($c); |
|
| 1097 | + } |
|
| 1098 | 1098 | |
| 1099 | - return mb_strlen($c); |
|
| 1099 | + return mb_strlen($c); |
|
| 1100 | 1100 | } |
| 1101 | 1101 | |
| 1102 | 1102 | /** |
@@ -1111,17 +1111,17 @@ discard block |
||
| 1111 | 1111 | * en unicode : 💩 |
| 1112 | 1112 | */ |
| 1113 | 1113 | function utf8_noplanes($x): string { |
| 1114 | - $regexp_utf8_4bytes = '/( |
|
| 1114 | + $regexp_utf8_4bytes = '/( |
|
| 1115 | 1115 | \xF0[\x90-\xBF][\x80-\xBF]{2} # planes 1-3 |
| 1116 | 1116 | | [\xF1-\xF3][\x80-\xBF]{3} # planes 4-15 |
| 1117 | 1117 | | \xF4[\x80-\x8F][\x80-\xBF]{2} # plane 16 |
| 1118 | 1118 | )/xS'; |
| 1119 | - if (preg_match_all($regexp_utf8_4bytes, $x, $z, PREG_PATTERN_ORDER)) { |
|
| 1120 | - foreach ($z[0] as $k) { |
|
| 1121 | - $ku = utf_8_to_unicode($k); |
|
| 1122 | - $x = str_replace($k, $ku, $x); |
|
| 1123 | - } |
|
| 1124 | - } |
|
| 1125 | - |
|
| 1126 | - return $x; |
|
| 1119 | + if (preg_match_all($regexp_utf8_4bytes, $x, $z, PREG_PATTERN_ORDER)) { |
|
| 1120 | + foreach ($z[0] as $k) { |
|
| 1121 | + $ku = utf_8_to_unicode($k); |
|
| 1122 | + $x = str_replace($k, $ku, $x); |
|
| 1123 | + } |
|
| 1124 | + } |
|
| 1125 | + |
|
| 1126 | + return $x; |
|
| 1127 | 1127 | } |
@@ -89,7 +89,7 @@ discard block |
||
| 89 | 89 | $charset = 'cp1256'; |
| 90 | 90 | } |
| 91 | 91 | |
| 92 | - if (find_in_path($charset . '.php', 'charsets/', true)) { |
|
| 92 | + if (find_in_path($charset.'.php', 'charsets/', true)) { |
|
| 93 | 93 | return $charset; |
| 94 | 94 | } else { |
| 95 | 95 | spip_log("Erreur: pas de fichier de conversion 'charsets/$charset'"); |
@@ -190,38 +190,38 @@ discard block |
||
| 190 | 190 | |
| 191 | 191 | if (!isset($trans[$charset][$charset_cible])) { |
| 192 | 192 | $trans[$charset][$charset_cible] = [ |
| 193 | - $p . chr(128) => '€', |
|
| 194 | - $p . chr(129) => ' ', # pas affecte |
|
| 195 | - $p . chr(130) => '‚', |
|
| 196 | - $p . chr(131) => 'ƒ', |
|
| 197 | - $p . chr(132) => '„', |
|
| 198 | - $p . chr(133) => '…', |
|
| 199 | - $p . chr(134) => '†', |
|
| 200 | - $p . chr(135) => '‡', |
|
| 201 | - $p . chr(136) => 'ˆ', |
|
| 202 | - $p . chr(137) => '‰', |
|
| 203 | - $p . chr(138) => 'Š', |
|
| 204 | - $p . chr(139) => '‹', |
|
| 205 | - $p . chr(140) => 'Œ', |
|
| 206 | - $p . chr(141) => ' ', # pas affecte |
|
| 207 | - $p . chr(142) => 'Ž', |
|
| 208 | - $p . chr(143) => ' ', # pas affecte |
|
| 209 | - $p . chr(144) => ' ', # pas affecte |
|
| 210 | - $p . chr(145) => '‘', |
|
| 211 | - $p . chr(146) => '’', |
|
| 212 | - $p . chr(147) => '“', |
|
| 213 | - $p . chr(148) => '”', |
|
| 214 | - $p . chr(149) => '•', |
|
| 215 | - $p . chr(150) => '–', |
|
| 216 | - $p . chr(151) => '—', |
|
| 217 | - $p . chr(152) => '˜', |
|
| 218 | - $p . chr(153) => '™', |
|
| 219 | - $p . chr(154) => 'š', |
|
| 220 | - $p . chr(155) => '›', |
|
| 221 | - $p . chr(156) => 'œ', |
|
| 222 | - $p . chr(157) => ' ', # pas affecte |
|
| 223 | - $p . chr(158) => 'ž', |
|
| 224 | - $p . chr(159) => 'Ÿ', |
|
| 193 | + $p.chr(128) => '€', |
|
| 194 | + $p.chr(129) => ' ', # pas affecte |
|
| 195 | + $p.chr(130) => '‚', |
|
| 196 | + $p.chr(131) => 'ƒ', |
|
| 197 | + $p.chr(132) => '„', |
|
| 198 | + $p.chr(133) => '…', |
|
| 199 | + $p.chr(134) => '†', |
|
| 200 | + $p.chr(135) => '‡', |
|
| 201 | + $p.chr(136) => 'ˆ', |
|
| 202 | + $p.chr(137) => '‰', |
|
| 203 | + $p.chr(138) => 'Š', |
|
| 204 | + $p.chr(139) => '‹', |
|
| 205 | + $p.chr(140) => 'Œ', |
|
| 206 | + $p.chr(141) => ' ', # pas affecte |
|
| 207 | + $p.chr(142) => 'Ž', |
|
| 208 | + $p.chr(143) => ' ', # pas affecte |
|
| 209 | + $p.chr(144) => ' ', # pas affecte |
|
| 210 | + $p.chr(145) => '‘', |
|
| 211 | + $p.chr(146) => '’', |
|
| 212 | + $p.chr(147) => '“', |
|
| 213 | + $p.chr(148) => '”', |
|
| 214 | + $p.chr(149) => '•', |
|
| 215 | + $p.chr(150) => '–', |
|
| 216 | + $p.chr(151) => '—', |
|
| 217 | + $p.chr(152) => '˜', |
|
| 218 | + $p.chr(153) => '™', |
|
| 219 | + $p.chr(154) => 'š', |
|
| 220 | + $p.chr(155) => '›', |
|
| 221 | + $p.chr(156) => 'œ', |
|
| 222 | + $p.chr(157) => ' ', # pas affecte |
|
| 223 | + $p.chr(158) => 'ž', |
|
| 224 | + $p.chr(159) => 'Ÿ', |
|
| 225 | 225 | ]; |
| 226 | 226 | if ($charset_cible != 'unicode') { |
| 227 | 227 | foreach ($trans[$charset][$charset_cible] as $k => $c) { |
@@ -367,7 +367,7 @@ discard block |
||
| 367 | 367 | // Sinon, peut-etre connaissons-nous ce charset ? |
| 368 | 368 | if (!isset($trans[$charset]) && (($cset = load_charset($charset)) && is_array($GLOBALS['CHARSET'][$cset]))) { |
| 369 | 369 | foreach ($GLOBALS['CHARSET'][$cset] as $key => $val) { |
| 370 | - $trans[$charset][chr($key)] = '&#' . $val . ';'; |
|
| 370 | + $trans[$charset][chr($key)] = '&#'.$val.';'; |
|
| 371 | 371 | } |
| 372 | 372 | } |
| 373 | 373 | if (isset($trans[$charset]) && (is_countable($trans[$charset]) ? count($trans[$charset]) : 0)) { |
@@ -432,11 +432,11 @@ discard block |
||
| 432 | 432 | $h = dechex($e); |
| 433 | 433 | if ($s = isset($CHARSET_REVERSE[$charset][$e])) { |
| 434 | 434 | $s = $CHARSET_REVERSE[$charset][$e]; |
| 435 | - $t['&#' . $e . ';'] = $t['�' . $e . ';'] = $t['�' . $e . ';'] = chr($s); |
|
| 436 | - $t['&#x' . $h . ';'] = $t['�' . $h . ';'] = $t['�' . $h . ';'] = chr($s); |
|
| 435 | + $t['&#'.$e.';'] = $t['�'.$e.';'] = $t['�'.$e.';'] = chr($s); |
|
| 436 | + $t['&#x'.$h.';'] = $t['�'.$h.';'] = $t['�'.$h.';'] = chr($s); |
|
| 437 | 437 | } else { |
| 438 | - $t['&#' . $e . ';'] = $t['�' . $e . ';'] = $t['�' . $e . ';'] = chr($e); |
|
| 439 | - $t['&#x' . $h . ';'] = $t['�' . $h . ';'] = $t['�' . $h . ';'] = chr($e); |
|
| 438 | + $t['&#'.$e.';'] = $t['�'.$e.';'] = $t['�'.$e.';'] = chr($e); |
|
| 439 | + $t['&#x'.$h.';'] = $t['�'.$h.';'] = $t['�'.$h.';'] = chr($e); |
|
| 440 | 440 | } |
| 441 | 441 | } |
| 442 | 442 | } |
@@ -482,7 +482,7 @@ discard block |
||
| 482 | 482 | && is_array($GLOBALS['CHARSET'][$cset])) |
| 483 | 483 | ) { |
| 484 | 484 | foreach ($GLOBALS['CHARSET'][$cset] as $key => $val) { |
| 485 | - $trans[$charset][chr($key)] = unicode2charset('&#' . $val . ';'); |
|
| 485 | + $trans[$charset][chr($key)] = unicode2charset('&#'.$val.';'); |
|
| 486 | 486 | } |
| 487 | 487 | } |
| 488 | 488 | if (is_countable($trans[$charset]) ? count($trans[$charset]) : 0) { |
@@ -588,7 +588,7 @@ discard block |
||
| 588 | 588 | } |
| 589 | 589 | $thisPos++; |
| 590 | 590 | } |
| 591 | - $encodedLetter = '&#' . preg_replace('/^0+/', '', $decimalCode) . ';'; |
|
| 591 | + $encodedLetter = '&#'.preg_replace('/^0+/', '', $decimalCode).';'; |
|
| 592 | 592 | $encodedString .= $encodedLetter; |
| 593 | 593 | } |
| 594 | 594 | } |
@@ -631,7 +631,7 @@ discard block |
||
| 631 | 631 | } // ignorer le BOM - http://www.unicode.org/faq/utf_bom.html |
| 632 | 632 | else { |
| 633 | 633 | if ($word != 65279) { |
| 634 | - $texte .= '&#' . $word . ';'; |
|
| 634 | + $texte .= '&#'.$word.';'; |
|
| 635 | 635 | } |
| 636 | 636 | } |
| 637 | 637 | } |
@@ -659,13 +659,13 @@ discard block |
||
| 659 | 659 | return chr($num); |
| 660 | 660 | } |
| 661 | 661 | if ($num < 2048) { |
| 662 | - return chr(($num >> 6) + 192) . chr(($num & 63) + 128); |
|
| 662 | + return chr(($num >> 6) + 192).chr(($num & 63) + 128); |
|
| 663 | 663 | } |
| 664 | 664 | if ($num < 65536) { |
| 665 | - return chr(($num >> 12) + 224) . chr((($num >> 6) & 63) + 128) . chr(($num & 63) + 128); |
|
| 665 | + return chr(($num >> 12) + 224).chr((($num >> 6) & 63) + 128).chr(($num & 63) + 128); |
|
| 666 | 666 | } |
| 667 | 667 | if ($num < 1_114_112) { |
| 668 | - return chr(($num >> 18) + 240) . chr((($num >> 12) & 63) + 128) . chr((($num >> 6) & 63) + 128) . chr(($num & 63) + 128); |
|
| 668 | + return chr(($num >> 18) + 240).chr((($num >> 12) & 63) + 128).chr((($num >> 6) & 63) + 128).chr(($num & 63) + 128); |
|
| 669 | 669 | } |
| 670 | 670 | |
| 671 | 671 | return ''; |
@@ -732,7 +732,7 @@ discard block |
||
| 732 | 732 | while (preg_match(',�*(\d+);,S', $texte, $regs) && !isset($vu[$regs[1]])) { |
| 733 | 733 | $num = $regs[1]; |
| 734 | 734 | $vu[$num] = true; |
| 735 | - $s = '\u' . sprintf('%04x', $num); |
|
| 735 | + $s = '\u'.sprintf('%04x', $num); |
|
| 736 | 736 | $texte = str_replace($regs[0], $s, $texte); |
| 737 | 737 | } |
| 738 | 738 | |
@@ -749,7 +749,7 @@ discard block |
||
| 749 | 749 | **/ |
| 750 | 750 | function javascript_to_unicode($texte) { |
| 751 | 751 | while (preg_match(',%u([0-9A-F][0-9A-F][0-9A-F][0-9A-F]),', $texte, $regs)) { |
| 752 | - $texte = str_replace($regs[0], '&#' . hexdec($regs[1]) . ';', $texte); |
|
| 752 | + $texte = str_replace($regs[0], '&#'.hexdec($regs[1]).';', $texte); |
|
| 753 | 753 | } |
| 754 | 754 | |
| 755 | 755 | return $texte; |
@@ -794,7 +794,7 @@ discard block |
||
| 794 | 794 | return ''; |
| 795 | 795 | } |
| 796 | 796 | |
| 797 | - $table_translit = 'translit' . $complexe; |
|
| 797 | + $table_translit = 'translit'.$complexe; |
|
| 798 | 798 | |
| 799 | 799 | // 2. Translitterer grace a la table predefinie |
| 800 | 800 | if (!isset($trans[$complexe])) { |
@@ -889,7 +889,7 @@ discard block |
||
| 889 | 889 | * true s'il a un BOM |
| 890 | 890 | **/ |
| 891 | 891 | function bom_utf8($texte): bool { |
| 892 | - return (substr($texte, 0, 3) === chr(0xEF) . chr(0xBB) . chr(0xBF)); |
|
| 892 | + return (substr($texte, 0, 3) === chr(0xEF).chr(0xBB).chr(0xBF)); |
|
| 893 | 893 | } |
| 894 | 894 | |
| 895 | 895 | /** |
@@ -1056,7 +1056,7 @@ discard block |
||
| 1056 | 1056 | |
| 1057 | 1057 | $lettre1 = mb_strtoupper(spip_substr($c, 0, 1)); |
| 1058 | 1058 | |
| 1059 | - return $lettre1 . spip_substr($c, 1); |
|
| 1059 | + return $lettre1.spip_substr($c, 1); |
|
| 1060 | 1060 | } |
| 1061 | 1061 | |
| 1062 | 1062 | /** |