changing SM internal gettext functions to use php-gettext classes. Fix for
[squirrelmail.git] / functions / i18n.php
CommitLineData
59177427 1<?php
1fd97780 2
35586184 3/**
d3bab52e 4 * SquirrelMail internationalization functions
35586184 5 *
2ba706ef 6 * Copyright (c) 1999-2004 The SquirrelMail Project Team
35586184 7 * Licensed under the GNU GPL. For full terms see the file COPYING.
8 *
9 * This file contains variuos functions that are needed to do
10 * internationalization of SquirrelMail.
11 *
12 * Internally the output character set is used. Other characters are
13 * encoded using Unicode entities according to HTML 4.0.
14 *
a8a1c36d 15 * @version $Id$
d6c32258 16 * @package squirrelmail
a8a1c36d 17 * @subpackage i18n
35586184 18 */
19
d6c32258 20/** Everything uses global.php... */
961ca3d8 21require_once(SM_PATH . 'functions/global.php');
22
3b84e1b1 23/**
24 * Gettext bindtextdomain wrapper.
25 *
26 * Wrapper solves differences between php versions in order to provide
27 * ngettext support. Should be used if translation uses ngettext
28 * functions.
29 * @param string $domain gettext domain name
30 * @param string $dir directory that contains all translations
31 * @return string path to translation directory
32 */
33function sq_bindtextdomain($domain,$dir) {
34 global $l10n, $gettext_flags, $sm_notAlias;
35
36 if ($gettext_flags==7) {
37 // gettext extension without ngettext
38 if (substr($dir, -1) != '/') $dir .= '/';
39 $mofile=$dir . $sm_notAlias . '/LC_MESSAGES/' . $domain . '.mo';
40 $input = new FileReader($mofile);
41 $l10n[$domain] = new gettext_reader($input);
42 }
43
44 $dir=bindtextdomain($domain,$dir);
45
46 return $dir;
47}
48
49/**
50 * Gettext textdomain wrapper.
51 * Makes sure that gettext_domain global is modified.
52 * @param string $name gettext domain name
53 * @return string gettext domain name
54 */
55function sq_textdomain($domain) {
56 global $gettext_domain;
57 $gettext_domain=textdomain($domain);
58 return $gettext_domain;
59}
60
d6c32258 61/**
51468260 62 * Converts string from given charset to charset, that can be displayed by user translation.
63 *
64 * Function by default returns html encoded strings, if translation uses different encoding.
65 * If Japanese translation is used - function returns string converted to euc-jp
66 * If iconv or recode functions are enabled and translation uses utf-8 - function returns utf-8 encoded string.
67 * If $charset is not supported - function returns unconverted string.
62f7daa5 68 *
51468260 69 * sanitizing of html tags is also done by this function.
70 *
d6c32258 71 * @param string $charset
72 * @param string $string Text to be decoded
51468260 73 * @return string decoded string
d6c32258 74 */
a2a7852b 75function charset_decode ($charset, $string) {
3ec81e63 76 global $languages, $squirrelmail_language, $default_charset;
f03f6ee7 77 global $use_php_recode, $use_php_iconv, $aggressive_decoding;
a2a7852b 78
3714db45 79 if (isset($languages[$squirrelmail_language]['XTRA_CODE']) &&
1bb86586 80 function_exists($languages[$squirrelmail_language]['XTRA_CODE'] . '_decode')) {
81 $string = call_user_func($languages[$squirrelmail_language]['XTRA_CODE'] . '_decode', $string);
6fbd125b 82 }
b05c8961 83
3ec81e63 84 $charset = strtolower($charset);
85
86 set_my_charset();
87
88 // Variables that allow to use functions without function_exist() calls
edf2c0ba 89 if (! isset($use_php_recode) || $use_php_recode=="" ) {
3b84e1b1 90 $use_php_recode=false; }
edf2c0ba 91 if (! isset($use_php_iconv) || $use_php_iconv=="" ) {
3b84e1b1 92 $use_php_iconv=false; }
3ec81e63 93
94 // Don't do conversion if charset is the same.
95 if ( $charset == strtolower($default_charset) )
3b84e1b1 96 return htmlspecialchars($string);
3ec81e63 97
98 // catch iso-8859-8-i thing
99 if ( $charset == "iso-8859-8-i" )
3b84e1b1 100 $charset = "iso-8859-8";
3ec81e63 101
102 /*
62f7daa5 103 * Recode converts html special characters automatically if you use
104 * 'charset..html' decoding. There is no documented way to put -d option
3ec81e63 105 * into php recode function call.
106 */
107 if ( $use_php_recode ) {
3b84e1b1 108 if ( $default_charset == "utf-8" ) {
109 // other charsets can be converted to utf-8 without loss.
110 // and output string is smaller
111 $string = recode_string($charset . "..utf-8",$string);
112 return htmlspecialchars($string);
113 } else {
114 $string = recode_string($charset . "..html",$string);
115 // recode does not convert single quote, htmlspecialchars does.
116 $string = str_replace("'", '&#039;', $string);
117 return $string;
118 }
3ec81e63 119 }
120
121 // iconv functions does not have html target and can be used only with utf-8
122 if ( $use_php_iconv && $default_charset=='utf-8') {
3b84e1b1 123 $string = iconv($charset,$default_charset,$string);
124 return htmlspecialchars($string);
3ec81e63 125 }
126
127 // If we don't use recode and iconv, we'll do it old way.
128
a2a7852b 129 /* All HTML special characters are 7 bit and can be replaced first */
62f7daa5 130
098ea084 131 $string = htmlspecialchars ($string);
a2a7852b 132
5dd23dac 133 /* controls cpu and memory intensive decoding cycles */
f03f6ee7 134 if (! isset($aggressive_decoding) || $aggressive_decoding=="" ) {
3b84e1b1 135 $aggressive_decoding=false; }
5dd23dac 136
b142de74 137 $decode=fixcharset($charset);
138 $decodefile=SM_PATH . 'functions/decode/' . $decode . '.php';
139 if (file_exists($decodefile)) {
3b84e1b1 140 include_once($decodefile);
141 $ret = call_user_func('charset_decode_'.$decode, $string);
a2a7852b 142 } else {
3b84e1b1 143 $ret = $string;
a2a7852b 144 }
145 return( $ret );
146}
03db90bc 147
d3bab52e 148/**
149 * Converts html string to given charset
150 * @param string $string
151 * @param string $charset
78be8403 152 * @param boolean $htmlencode keep htmlspecialchars encoding
62f7daa5 153 * @param string
d3bab52e 154 */
78be8403 155function charset_encode($string,$charset,$htmlencode=true) {
3b84e1b1 156 global $default_charset;
157
158 // Undo html special chars
159 if (! $htmlencode ) {
160 $string = str_replace(array('&amp;','&gt;','&lt;','&quot;'),array('&','>','<','"'),$string);
161 }
162
163 $encode=fixcharset($charset);
164 $encodefile=SM_PATH . 'functions/encode/' . $encode . '.php';
165 if (file_exists($encodefile)) {
166 include_once($encodefile);
167 $ret = call_user_func('charset_encode_'.$encode, $string);
168 } else {
169 include_once(SM_PATH . 'functions/encode/us_ascii.php');
170 $ret = charset_encode_us_ascii($string);
171 }
172 return( $ret );
d3bab52e 173}
174
175/**
176 * Combined decoding and encoding functions
177 *
178 * If conversion is done to charset different that utf-8, unsupported symbols
179 * will be replaced with question marks.
180 * @param string $in_charset initial charset
181 * @param string $string string that has to be converted
182 * @param string $out_charset final charset
78be8403 183 * @param boolean $htmlencode keep htmlspecialchars encoding
d3bab52e 184 * @return string converted string
185 */
78be8403 186function charset_convert($in_charset,$string,$out_charset,$htmlencode=true) {
33991968 187 $string=charset_decode($in_charset,$string);
188 $string=charset_encode($string,$out_charset,$htmlencode);
189 return $string;
d3bab52e 190}
191
b142de74 192/**
193 * Makes charset name suitable for decoding cycles
194 *
195 * @param string $charset Name of charset
196 * @return string $charset Adjusted name of charset
197 */
198function fixcharset($charset) {
199 // minus removed from function names
200 $charset=str_replace('-','_',$charset);
62f7daa5 201
b142de74 202 // windows-125x and cp125x charsets
203 $charset=str_replace('windows_','cp',$charset);
a2a7852b 204
b142de74 205 // ibm > cp
206 $charset=str_replace('ibm','cp',$charset);
207
208 // iso-8859-8-i -> iso-8859-8
209 // use same cycle until I'll find differences
210 $charset=str_replace('iso_8859_8_i','iso_8859_8',$charset);
211
212 return $charset;
213}
a2a7852b 214
51468260 215/**
a2a7852b 216 * Set up the language to be output
217 * if $do_search is true, then scan the browser information
218 * for a possible language that we know
51468260 219 *
62f7daa5 220 * Function sets system locale environment (LC_ALL, LANG, LANGUAGE),
51468260 221 * gettext translation bindings and html header information.
222 *
5679405c 223 * Function returns error codes, if there is some fatal error.
62f7daa5 224 * 0 = no error,
225 * 1 = mbstring support is not present,
51468260 226 * 2 = mbstring support is not present, user's translation reverted to en_US.
227 *
228 * @param string $sm_language translation used by user's interface
229 * @param bool $do_search use browser's preferred language detection functions. Defaults to false.
230 * @param bool $default set $sm_language to $squirrelmail_default_language if language detection fails or language is not set. Defaults to false.
62f7daa5 231 * @return int function execution error codes.
a2a7852b 232 */
67a8c90a 233function set_up_language($sm_language, $do_search = false, $default = false) {
a2a7852b 234
235 static $SetupAlready = 0;
9eb0fbd4 236 global $use_gettext, $languages,
fe48c808 237 $squirrelmail_language, $squirrelmail_default_language, $default_charset,
51468260 238 $sm_notAlias, $username, $data_dir;
a2a7852b 239
240 if ($SetupAlready) {
241 return;
242 }
a65846a7 243
5c920668 244 $SetupAlready = TRUE;
961ca3d8 245 sqgetGlobalVar('HTTP_ACCEPT_LANGUAGE', $accept_lang, SQ_SERVER);
a2a7852b 246
961ca3d8 247 if ($do_search && ! $sm_language && isset($accept_lang)) {
248 $sm_language = substr($accept_lang, 0, 2);
a2a7852b 249 }
62f7daa5 250
67a8c90a 251 if ((!$sm_language||$default) && isset($squirrelmail_default_language)) {
a2a7852b 252 $squirrelmail_language = $squirrelmail_default_language;
66d7950f 253 $sm_language = $squirrelmail_default_language;
a2a7852b 254 }
255 $sm_notAlias = $sm_language;
62f7daa5 256
3ec81e63 257 // Catching removed translation
258 // System reverts to English translation if user prefs contain translation
2ba706ef 259 // that is not available in $languages array
3ec81e63 260 if (!isset($languages[$sm_notAlias])) {
3b84e1b1 261 $sm_notAlias="en_US";
3ec81e63 262 }
263
a2a7852b 264 while (isset($languages[$sm_notAlias]['ALIAS'])) {
265 $sm_notAlias = $languages[$sm_notAlias]['ALIAS'];
266 }
267
88cb1b4d 268 if ( isset($sm_language) &&
5c920668 269 $use_gettext &&
270 $sm_language != '' &&
271 isset($languages[$sm_notAlias]['CHARSET']) ) {
3b84e1b1 272 sq_bindtextdomain( 'squirrelmail', SM_PATH . 'locale/' );
273 sq_textdomain( 'squirrelmail' );
03db90bc 274 if (function_exists('bind_textdomain_codeset')) {
3b84e1b1 275 if ($sm_notAlias == 'ja_JP') {
276 bind_textdomain_codeset ("squirrelmail", 'EUC-JP');
a5970d71 277 } else {
3b84e1b1 278 bind_textdomain_codeset ("squirrelmail", $languages[$sm_notAlias]['CHARSET'] );
03db90bc 279 }
280 }
281 if (isset($languages[$sm_notAlias]['LOCALE'])){
3b84e1b1 282 $longlocale=$languages[$sm_notAlias]['LOCALE'];
03db90bc 283 } else {
3b84e1b1 284 $longlocale=$sm_notAlias;
03db90bc 285 }
88cb1b4d 286 if ( !ini_get('safe_mode') &&
f2374580 287 getenv( 'LC_ALL' ) != $longlocale ) {
288 putenv( "LC_ALL=$longlocale" );
289 putenv( "LANG=$longlocale" );
290 putenv( "LANGUAGE=$longlocale" );
475df436 291 if ($sm_notAlias=='tr_TR') putenv( "LC_CTYPE=C" );
a2a7852b 292 }
03db90bc 293 setlocale(LC_ALL, $longlocale);
475df436 294 if ($sm_notAlias=='tr_TR') setlocale(LC_CTYPE,'C');
03db90bc 295
296 // Set text direction/alignment variables
62f7daa5 297 if (isset($languages[$sm_notAlias]['DIR']) &&
03db90bc 298 $languages[$sm_notAlias]['DIR'] == 'rtl') {
3b84e1b1 299 /**
300 * Text direction
301 * @global string $text_direction
302 */
303 $text_direction='rtl';
304 /**
305 * Left alignment
306 * @global string $left_align
307 */
308 $left_align='right';
309 /**
310 * Right alignment
311 * @global string $right_align
312 */
313 $right_align='left';
03db90bc 314 } else {
3b84e1b1 315 $text_direction='ltr';
316 $left_align='left';
317 $right_align='right';
03db90bc 318 }
319
320 $squirrelmail_language = $sm_notAlias;
a5970d71 321 if ($squirrelmail_language == 'ja_JP') {
b05c8961 322 header ('Content-Type: text/html; charset=EUC-JP');
323 if (!function_exists('mb_internal_encoding')) {
3b84e1b1 324 // Error messages can't be displayed here
325 $error = 1;
326 // Revert to English if possible.
327 if (function_exists('setPref') && $username!='' && $data_dir!="") {
328 setPref($data_dir, $username, 'language', "en_US");
329 $error = 2;
330 }
331 // stop further execution in order not to get php errors on mb_internal_encoding().
332 return $error;
e842b215 333 }
334 if (function_exists('mb_language')) {
335 mb_language('Japanese');
b05c8961 336 }
337 mb_internal_encoding('EUC-JP');
338 mb_http_output('pass');
fe48c808 339 } elseif ($squirrelmail_language == 'en_US') {
340 header( 'Content-Type: text/html; charset=' . $default_charset );
b05c8961 341 } else {
fe48c808 342 header( 'Content-Type: text/html; charset=' . $languages[$sm_notAlias]['CHARSET'] );
343 }
a2a7852b 344 }
51468260 345 return 0;
b05c8961 346}
a2a7852b 347
51468260 348/**
349 * Sets default_charset variable according to the one that is used by user's translations.
350 *
351 * Function changes global $default_charset variable in order to be sure, that it
352 * contains charset used by user's translation. Sanity of $squirrelmail_default_language
353 * and $default_charset combination provided in SquirrelMail config is also tested.
354 *
355 * There can be a $default_charset setting in the
356 * config.php file, but the user may have a different language
357 * selected for a user interface. This function checks the
358 * language selected by the user and tags the outgoing messages
359 * with the appropriate charset corresponding to the language
360 * selection. This is "more right" (tm), than just stamping the
361 * message blindly with the system-wide $default_charset.
362 */
a2a7852b 363function set_my_charset(){
94965562 364 global $data_dir, $username, $default_charset, $languages, $squirrelmail_default_language;
88cb1b4d 365
a2a7852b 366 $my_language = getPref($data_dir, $username, 'language');
5c920668 367 if (!$my_language) {
94965562 368 $my_language = $squirrelmail_default_language ;
5c920668 369 }
3ec81e63 370 // Catch removed translation
371 if (!isset($languages[$my_language])) {
3b84e1b1 372 $my_language="en_US";
3ec81e63 373 }
a2a7852b 374 while (isset($languages[$my_language]['ALIAS'])) {
f7e8861e 375 $my_language = $languages[$my_language]['ALIAS'];
a2a7852b 376 }
5c920668 377 $my_charset = $languages[$my_language]['CHARSET'];
fe48c808 378 if ($my_language!='en_US') {
a2a7852b 379 $default_charset = $my_charset;
380 }
381}
382
a2a7852b 383/* ------------------------------ main --------------------------- */
384
5c920668 385global $squirrelmail_language, $languages, $use_gettext;
386
a2a7852b 387if (! isset($squirrelmail_language)) {
388 $squirrelmail_language = '';
389}
390
51468260 391/**
392 * Array specifies the available translations.
393 *
394 * Structure of array:
395 * $languages['language']['variable'] = 'value'
62f7daa5 396 *
51468260 397 * Possible 'variable' names:
398 * NAME - Translation name in English
399 * CHARSET - Encoding used by translation
400 * ALIAS - used when 'language' is only short name and 'value' should provide long language name
401 * ALTNAME - Native translation name. Any 8bit symbols must be html encoded.
402 * LOCALE - Full locale name (in xx_XX.charset format)
403 * DIR - Text direction. Used to define Right-to-Left languages. Possible values 'rtl' or 'ltr'. If undefined - defaults to 'ltr'
1bb86586 404 * XTRA_CODE - translation uses special functions. See doc/i18n.txt
62f7daa5 405 *
51468260 406 * Each 'language' definition requires NAME+CHARSET or ALIAS variables.
407 *
408 * @name $languages
a8a1c36d 409 * @global array $languages
51468260 410 */
a8fa8e33 411$languages['bg_BG']['NAME'] = 'Bulgarian';
412$languages['bg_BG']['ALTNAME'] = '&#1041;&#1098;&#1083;&#1075;&#1072;&#1088;&#1089;&#1082;&#1080;';
413$languages['bg_BG']['CHARSET'] = 'windows-1251';
c30be3cf 414$languages['bg_BG']['LOCALE'] = 'bg_BG.CP1251';
a8fa8e33 415$languages['bg']['ALIAS'] = 'bg_BG';
416
57564e64 417$languages['bn_IN']['NAME'] = 'Bengali';
418$languages['bn_IN']['CHARSET'] = 'utf-8';
419$languages['bn_IN']['LOCALE'] = 'bn_IN.UTF-8';
420$languages['bn']['ALIAS'] = 'bn_IN';
421
a8fa8e33 422$languages['ca_ES']['NAME'] = 'Catalan';
423$languages['ca_ES']['CHARSET'] = 'iso-8859-1';
a00d341d 424$languages['ca_ES']['LOCALE'] = 'ca_ES.ISO8859-1';
a8fa8e33 425$languages['ca']['ALIAS'] = 'ca_ES';
426
427$languages['cs_CZ']['NAME'] = 'Czech';
428$languages['cs_CZ']['ALTNAME'] = '&#268;e&scaron;tina';
429$languages['cs_CZ']['CHARSET'] = 'iso-8859-2';
a00d341d 430$languages['cs_CZ']['LOCALE'] = 'cs_CZ.ISO8859-2';
a8fa8e33 431$languages['cs']['ALIAS'] = 'cs_CZ';
432
433$languages['cy_GB']['NAME'] = 'Welsh';
434$languages['cy_GB']['ALTNAME'] = 'Cymraeg';
435$languages['cy_GB']['CHARSET'] = 'iso-8859-1';
a00d341d 436$languages['cy_GB']['LOCALE'] = 'cy_GB.ISO8859-1';
a8fa8e33 437$languages['cy']['ALIAS'] = 'cy_GB';
438
439// Danish locale is da_DK.
440$languages['da_DK']['NAME'] = 'Danish';
441$languages['da_DK']['ALTNAME'] = 'Dansk';
442$languages['da_DK']['CHARSET'] = 'iso-8859-1';
a00d341d 443$languages['da_DK']['LOCALE'] = 'da_DK.ISO8859-1';
a8fa8e33 444$languages['da']['ALIAS'] = 'da_DK';
445
446$languages['de_DE']['NAME'] = 'German';
447$languages['de_DE']['ALTNAME'] = 'Deutsch';
448$languages['de_DE']['CHARSET'] = 'iso-8859-1';
a00d341d 449$languages['de_DE']['LOCALE'] = 'de_DE.ISO8859-1';
a8fa8e33 450$languages['de']['ALIAS'] = 'de_DE';
451
452$languages['el_GR']['NAME'] = 'Greek';
453$languages['el_GR']['ALTNAME'] = '&Epsilon;&lambda;&lambda;&eta;&nu;&iota;&kappa;&#940;';
454$languages['el_GR']['CHARSET'] = 'iso-8859-7';
a00d341d 455$languages['el_GR']['LOCALE'] = 'el_GR.ISO8859-7';
a8fa8e33 456$languages['el']['ALIAS'] = 'el_GR';
a2a7852b 457
3bb3d83b 458$languages['en_GB']['NAME'] = 'British';
459$languages['en_GB']['CHARSET'] = 'iso-8859-15';
a00d341d 460$languages['en_GB']['LOCALE'] = 'en_GB.ISO8859-15';
3bb3d83b 461
5c920668 462$languages['en_US']['NAME'] = 'English';
463$languages['en_US']['CHARSET'] = 'iso-8859-1';
a00d341d 464$languages['en_US']['LOCALE'] = 'en_US.ISO8859-1';
5c920668 465$languages['en']['ALIAS'] = 'en_US';
a2a7852b 466
a8fa8e33 467$languages['es_ES']['NAME'] = 'Spanish';
468$languages['es_ES']['ALTNAME'] = 'Espa&ntilde;ol';
469$languages['es_ES']['CHARSET'] = 'iso-8859-1';
a00d341d 470$languages['es_ES']['LOCALE'] = 'es_ES.ISO8859-1';
a8fa8e33 471$languages['es']['ALIAS'] = 'es_ES';
472
473$languages['et_EE']['NAME'] = 'Estonian';
474$languages['et_EE']['CHARSET'] = 'iso-8859-15';
a00d341d 475$languages['et_EE']['LOCALE'] = 'et_EE.ISO8859-15';
a8fa8e33 476$languages['et']['ALIAS'] = 'et_EE';
477
2d268514 478$languages['eu_ES']['NAME'] = 'Basque';
479$languages['eu_ES']['CHARSET'] = 'iso-8859-1';
480$languages['eu_ES']['LOCALE'] = 'eu_ES.ISO8859-1';
481$languages['eu']['ALIAS'] = 'eu_ES';
482
a8fa8e33 483$languages['fo_FO']['NAME'] = 'Faroese';
484$languages['fo_FO']['CHARSET'] = 'iso-8859-1';
a00d341d 485$languages['fo_FO']['LOCALE'] = 'fo_FO.ISO8859-1';
a8fa8e33 486$languages['fo']['ALIAS'] = 'fo_FO';
487
488$languages['fi_FI']['NAME'] = 'Finnish';
489$languages['fi_FI']['ALTNAME'] = 'Suomi';
490$languages['fi_FI']['CHARSET'] = 'iso-8859-1';
a00d341d 491$languages['fi_FI']['LOCALE'] = 'fi_FI.ISO8859-1';
a8fa8e33 492$languages['fi']['ALIAS'] = 'fi_FI';
493
494$languages['fr_FR']['NAME'] = 'French';
495$languages['fr_FR']['ALTNAME'] = 'Fran&#231;ais';
496$languages['fr_FR']['CHARSET'] = 'iso-8859-1';
a00d341d 497$languages['fr_FR']['LOCALE'] = 'fr_FR.ISO8859-1';
a8fa8e33 498$languages['fr']['ALIAS'] = 'fr_FR';
499
500$languages['hr_HR']['NAME'] = 'Croatian';
501$languages['hr_HR']['CHARSET'] = 'iso-8859-2';
a00d341d 502$languages['hr_HR']['LOCALE'] = 'hr_HR.ISO8859-2';
a8fa8e33 503$languages['hr']['ALIAS'] = 'hr_HR';
504
505$languages['hu_HU']['NAME'] = 'Hungarian';
506$languages['hu_HU']['ALTNAME'] = 'Magyar';
507$languages['hu_HU']['CHARSET'] = 'iso-8859-2';
a00d341d 508$languages['hu_HU']['LOCALE'] = 'hu_HU.ISO8859-2';
a8fa8e33 509$languages['hu']['ALIAS'] = 'hu_HU';
510
511$languages['id_ID']['NAME'] = 'Indonesian';
512$languages['id_ID']['ALTNAME'] = 'Bahasa Indonesia';
513$languages['id_ID']['CHARSET'] = 'iso-8859-1';
a00d341d 514$languages['id_ID']['LOCALE'] = 'id_ID.ISO8859-1';
a8fa8e33 515$languages['id']['ALIAS'] = 'id_ID';
516
517$languages['is_IS']['NAME'] = 'Icelandic';
518$languages['is_IS']['ALTNAME'] = '&Iacute;slenska';
519$languages['is_IS']['CHARSET'] = 'iso-8859-1';
a00d341d 520$languages['is_IS']['LOCALE'] = 'is_IS.ISO8859-1';
a8fa8e33 521$languages['is']['ALIAS'] = 'is_IS';
522
523$languages['it_IT']['NAME'] = 'Italian';
524$languages['it_IT']['CHARSET'] = 'iso-8859-1';
a00d341d 525$languages['it_IT']['LOCALE'] = 'it_IT.ISO8859-1';
a8fa8e33 526$languages['it']['ALIAS'] = 'it_IT';
527
528$languages['ja_JP']['NAME'] = 'Japanese';
529$languages['ja_JP']['ALTNAME'] = '&#26085;&#26412;&#35486;';
530$languages['ja_JP']['CHARSET'] = 'iso-2022-jp';
51468260 531$languages['ja_JP']['LOCALE'] = 'ja_JP.EUC-JP';
1bb86586 532$languages['ja_JP']['XTRA_CODE'] = 'japanese_xtra';
a8fa8e33 533$languages['ja']['ALIAS'] = 'ja_JP';
534
535$languages['ko_KR']['NAME'] = 'Korean';
536$languages['ko_KR']['CHARSET'] = 'euc-KR';
c30be3cf 537$languages['ko_KR']['LOCALE'] = 'ko_KR.EUC-KR';
1bb86586 538$languages['ko_KR']['XTRA_CODE'] = 'korean_xtra';
a8fa8e33 539$languages['ko']['ALIAS'] = 'ko_KR';
540
541$languages['lt_LT']['NAME'] = 'Lithuanian';
542$languages['lt_LT']['ALTNAME'] = 'Lietuvi&#371;';
543$languages['lt_LT']['CHARSET'] = 'utf-8';
544$languages['lt_LT']['LOCALE'] = 'lt_LT.UTF-8';
545$languages['lt']['ALIAS'] = 'lt_LT';
546
547$languages['nl_NL']['NAME'] = 'Dutch';
548$languages['nl_NL']['ALTNAME'] = 'Nederlands';
549$languages['nl_NL']['CHARSET'] = 'iso-8859-1';
a00d341d 550$languages['nl_NL']['LOCALE'] = 'nl_NL.ISO8859-1';
a8fa8e33 551$languages['nl']['ALIAS'] = 'nl_NL';
552
553$languages['ms_MY']['NAME'] = 'Malay';
554$languages['ms_MY']['ALTNAME'] = 'Bahasa Melayu';
555$languages['ms_MY']['CHARSET'] = 'iso-8859-1';
a00d341d 556$languages['ms_MY']['LOCALE'] = 'ms_MY.ISO8859-1';
a8fa8e33 557$languages['my']['ALIAS'] = 'ms_MY';
558
850db3c8 559$languages['nb_NO']['NAME'] = 'Norwegian (Bokm&aring;l)';
560$languages['nb_NO']['ALTNAME'] = 'Norsk (Bokm&aring;l)';
561$languages['nb_NO']['CHARSET'] = 'iso-8859-1';
a00d341d 562$languages['nb_NO']['LOCALE'] = 'nb_NO.ISO8859-1';
850db3c8 563$languages['nb']['ALIAS'] = 'nb_NO';
a8fa8e33 564
565$languages['nn_NO']['NAME'] = 'Norwegian (Nynorsk)';
566$languages['nn_NO']['ALTNAME'] = 'Norsk (Nynorsk)';
567$languages['nn_NO']['CHARSET'] = 'iso-8859-1';
a00d341d 568$languages['nn_NO']['LOCALE'] = 'nn_NO.ISO8859-1';
a8fa8e33 569
570$languages['pl_PL']['NAME'] = 'Polish';
571$languages['pl_PL']['ALTNAME'] = 'Polski';
572$languages['pl_PL']['CHARSET'] = 'iso-8859-2';
a00d341d 573$languages['pl_PL']['LOCALE'] = 'pl_PL.ISO8859-2';
a8fa8e33 574$languages['pl']['ALIAS'] = 'pl_PL';
575
576$languages['pt_PT']['NAME'] = 'Portuguese (Portugal)';
577$languages['pt_PT']['CHARSET'] = 'iso-8859-1';
a00d341d 578$languages['pt_PT']['LOCALE'] = 'pt_PT.ISO8859-1';
a8fa8e33 579$languages['pt']['ALIAS'] = 'pt_PT';
580
581$languages['pt_BR']['NAME'] = 'Portuguese (Brazil)';
582$languages['pt_BR']['ALTNAME'] = 'Portugu&ecirc;s do Brasil';
583$languages['pt_BR']['CHARSET'] = 'iso-8859-1';
a00d341d 584$languages['pt_BR']['LOCALE'] = 'pt_BR.ISO8859-1';
a8fa8e33 585
586$languages['ro_RO']['NAME'] = 'Romanian';
587$languages['ro_RO']['ALTNAME'] = 'Rom&acirc;n&#259;';
588$languages['ro_RO']['CHARSET'] = 'iso-8859-2';
a00d341d 589$languages['ro_RO']['LOCALE'] = 'ro_RO.ISO8859-2';
a8fa8e33 590$languages['ro']['ALIAS'] = 'ro_RO';
591
592$languages['ru_RU']['NAME'] = 'Russian';
593$languages['ru_RU']['ALTNAME'] = '&#1056;&#1091;&#1089;&#1089;&#1082;&#1080;&#1081;';
594$languages['ru_RU']['CHARSET'] = 'utf-8';
c30be3cf 595$languages['ru_RU']['LOCALE'] = 'ru_RU.UTF-8';
a8fa8e33 596$languages['ru']['ALIAS'] = 'ru_RU';
597
c30be3cf 598$languages['sk_SK']['NAME'] = 'Slovak';
599$languages['sk_SK']['CHARSET'] = 'iso-8859-2';
a00d341d 600$languages['sk_SK']['LOCALE'] = 'sk_SK.ISO8859-2';
c30be3cf 601$languages['sk']['ALIAS'] = 'sk_SK';
a8fa8e33 602
603$languages['sl_SI']['NAME'] = 'Slovenian';
604$languages['sl_SI']['ALTNAME'] = 'Sloven&scaron;&#269;ina';
605$languages['sl_SI']['CHARSET'] = 'iso-8859-2';
a00d341d 606$languages['sl_SI']['LOCALE'] = 'sl_SI.ISO8859-2';
a8fa8e33 607$languages['sl']['ALIAS'] = 'sl_SI';
608
609$languages['sr_YU']['NAME'] = 'Serbian';
610$languages['sr_YU']['ALTNAME'] = 'Srpski';
611$languages['sr_YU']['CHARSET'] = 'iso-8859-2';
a00d341d 612$languages['sr_YU']['LOCALE'] = 'sr_YU.ISO8859-2';
a8fa8e33 613$languages['sr']['ALIAS'] = 'sr_YU';
614
615$languages['sv_SE']['NAME'] = 'Swedish';
616$languages['sv_SE']['ALTNAME'] = 'Svenska';
617$languages['sv_SE']['CHARSET'] = 'iso-8859-1';
a00d341d 618$languages['sv_SE']['LOCALE'] = 'sv_SE.ISO8859-1';
a8fa8e33 619$languages['sv']['ALIAS'] = 'sv_SE';
620
621$languages['th_TH']['NAME'] = 'Thai';
622$languages['th_TH']['CHARSET'] = 'tis-620';
c30be3cf 623$languages['th_TH']['LOCALE'] = 'th_TH.TIS-620';
a8fa8e33 624$languages['th']['ALIAS'] = 'th_TH';
625
551a09c7 626$languages['tl_PH']['NAME'] = 'Tagalog';
627$languages['tl_PH']['CHARSET'] = 'iso-8859-1';
a00d341d 628$languages['tl_PH']['LOCALE'] = 'tl_PH.ISO8859-1';
551a09c7 629$languages['tl']['ALIAS'] = 'tl_PH';
630
a8fa8e33 631$languages['tr_TR']['NAME'] = 'Turkish';
632$languages['tr_TR']['CHARSET'] = 'iso-8859-9';
a00d341d 633$languages['tr_TR']['LOCALE'] = 'tr_TR.ISO8859-9';
a8fa8e33 634$languages['tr']['ALIAS'] = 'tr_TR';
635
636$languages['zh_TW']['NAME'] = 'Chinese Trad';
637$languages['zh_TW']['CHARSET'] = 'big5';
c30be3cf 638$languages['zh_TW']['LOCALE'] = 'zh_TW.BIG5';
a8fa8e33 639$languages['tw']['ALIAS'] = 'zh_TW';
640
641$languages['zh_CN']['NAME'] = 'Chinese Simp';
642$languages['zh_CN']['CHARSET'] = 'gb2312';
c30be3cf 643$languages['zh_CN']['LOCALE'] = 'zh_CN.GB2312';
a8fa8e33 644$languages['cn']['ALIAS'] = 'zh_CN';
060c9483 645
a8fa8e33 646$languages['uk_UA']['NAME'] = 'Ukrainian';
647$languages['uk_UA']['CHARSET'] = 'koi8-u';
c30be3cf 648$languages['uk_UA']['LOCALE'] = 'uk_UA.KOI8-U';
a8fa8e33 649$languages['uk']['ALIAS'] = 'uk_UA';
c30be3cf 650
651$languages['ru_UA']['NAME'] = 'Russian (Ukrainian)';
652$languages['ru_UA']['CHARSET'] = 'koi8-r';
653$languages['ru_UA']['LOCALE'] = 'ru_UA.KOI8-R';
654
87c6b544 655/*
850db3c8 656$languages['vi_VN']['NAME'] = 'Vietnamese';
657$languages['vi_VN']['CHARSET'] = 'utf-8';
658$languages['vi']['ALIAS'] = 'vi_VN';
87c6b544 659*/
a8fa8e33 660
d3b57948 661// Right to left languages
a8fa8e33 662$languages['ar']['NAME'] = 'Arabic';
663$languages['ar']['CHARSET'] = 'windows-1256';
664$languages['ar']['DIR'] = 'rtl';
665
4417eead 666$languages['fa_IR']['NAME'] = 'Farsi';
667$languages['fa_IR']['CHARSET'] = 'utf-8';
668$languages['fa_IR']['DIR'] = 'rtl';
669$languages['fa_IR']['LOCALE'] = 'fa_IR.UTF-8';
670$languages['fa']['ALIAS'] = 'fa_IR';
671
a8fa8e33 672$languages['he_IL']['NAME'] = 'Hebrew';
673$languages['he_IL']['CHARSET'] = 'windows-1255';
c30be3cf 674$languages['he_IL']['LOCALE'] = 'he_IL.CP1255';
a8fa8e33 675$languages['he_IL']['DIR'] = 'rtl';
676$languages['he']['ALIAS'] = 'he_IL';
d3b57948 677
2ba590f9 678$languages['ug']['NAME'] = 'Uighur';
679$languages['ug']['CHARSET'] = 'utf-8';
680$languages['ug']['DIR'] = 'rtl';
681
5c920668 682/* Detect whether gettext is installed. */
a2a7852b 683$gettext_flags = 0;
684if (function_exists('_')) {
685 $gettext_flags += 1;
686}
687if (function_exists('bindtextdomain')) {
688 $gettext_flags += 2;
689}
690if (function_exists('textdomain')) {
691 $gettext_flags += 4;
692}
3b84e1b1 693if (function_exists('ngettext')) {
694 $gettext_flags += 8;
695}
a2a7852b 696
5c920668 697/* If gettext is fully loaded, cool */
3b84e1b1 698if ($gettext_flags == 15) {
a2a7852b 699 $use_gettext = true;
700}
3b84e1b1 701
702/* If ngettext support is missing, load it */
703elseif ($gettext_flags == 7) {
704 $use_gettext = true;
705 // load internal ngettext functions
706 include_once(SM_PATH . 'class/l10n.class.php');
707 include_once(SM_PATH . 'functions/ngettext.php');
708}
709
5c920668 710/* If we can fake gettext, try that */
a2a7852b 711elseif ($gettext_flags == 0) {
712 $use_gettext = true;
e7ab8c9d 713 include_once(SM_PATH . 'functions/gettext.php');
a2a7852b 714} else {
5c920668 715 /* Uh-ho. A weird install */
a2a7852b 716 if (! $gettext_flags & 1) {
03db90bc 717 /**
718 * Function is used as replacement in broken installs
719 * @ignore
720 */
a2a7852b 721 function _($str) {
722 return $str;
723 }
724 }
725 if (! $gettext_flags & 2) {
03db90bc 726 /**
727 * Function is used as replacement in broken installs
728 * @ignore
729 */
a2a7852b 730 function bindtextdomain() {
731 return;
732 }
733 }
734 if (! $gettext_flags & 4) {
03db90bc 735 /**
736 * Function is used as replacemet in broken installs
737 * @ignore
738 */
a2a7852b 739 function textdomain() {
740 return;
741 }
742 }
3b84e1b1 743 if (! $gettext_flags & 8) {
744 /**
745 * Function is used as replacemet in broken installs
746 * @ignore
747 */
748 function ngettext($str,$str2,$number) {
749 if ($number>1) {
750 return $str2;
751 } else {
752 return $str;
753 }
754 }
755 }
a2a7852b 756}
757
1d33e35e 758
51468260 759/**
1d33e35e 760 * Japanese charset extra function
761 *
51468260 762 * Action performed by function is defined by first argument.
763 * Default return value is defined by second argument.
764 * Use of third argument depends on action.
765 *
a8a1c36d 766 * @param string $action action performed by this function.
51468260 767 * possible values:
03db90bc 768 * decode - convert returned string to euc-jp. third argument unused
769 * encode - convert returned string to jis. third argument unused
770 * strimwidth - third argument=$width. trims string to $width symbols.
771 * encodeheader - create base64 encoded header in iso-2022-jp. third argument unused
772 * decodeheader - return human readable string from mime header. string is returned in euc-jp. third argument unused
773 * downloadfilename - third argument $useragent. Arguments provide browser info. Returns shift-jis or euc-jp encoded file name
774 * wordwrap - third argument=$wrap. wraps text at $wrap symbols
775 * utf7-imap_encode - returns string converted from euc-jp to utf7-imap. third argument unused
776 * utf7-imap_decode - returns string converted from utf7-imap to euc-jp. third argument unused
a8a1c36d 777 * @param string $ret default return value
1d33e35e 778 */
1bb86586 779function japanese_xtra() {
1d33e35e 780 $ret = func_get_arg(1); /* default return value */
781 if (function_exists('mb_detect_encoding')) {
782 switch (func_get_arg(0)) { /* action */
783 case 'decode':
e842b215 784 $detect_encoding = @mb_detect_encoding($ret);
1d33e35e 785 if ($detect_encoding == 'JIS' ||
786 $detect_encoding == 'EUC-JP' ||
e842b215 787 $detect_encoding == 'SJIS' ||
788 $detect_encoding == 'UTF-8') {
62f7daa5 789
e842b215 790 $ret = mb_convert_kana(mb_convert_encoding($ret, 'EUC-JP', 'AUTO'), "KV");
1d33e35e 791 }
792 break;
793 case 'encode':
e842b215 794 $detect_encoding = @mb_detect_encoding($ret);
1d33e35e 795 if ($detect_encoding == 'JIS' ||
796 $detect_encoding == 'EUC-JP' ||
e842b215 797 $detect_encoding == 'SJIS' ||
798 $detect_encoding == 'UTF-8') {
62f7daa5 799
e842b215 800 $ret = mb_convert_encoding(mb_convert_kana($ret, "KV"), 'JIS', 'AUTO');
1d33e35e 801 }
802 break;
803 case 'strimwidth':
804 $width = func_get_arg(2);
62f7daa5 805 $ret = mb_strimwidth($ret, 0, $width, '...');
1d33e35e 806 break;
807 case 'encodeheader':
8ba05cbe 808 $result = '';
809 if (strlen($ret) > 0) {
810 $tmpstr = mb_substr($ret, 0, 1);
811 $prevcsize = strlen($tmpstr);
812 for ($i = 1; $i < mb_strlen($ret); $i++) {
813 $tmp = mb_substr($ret, $i, 1);
814 if (strlen($tmp) == $prevcsize) {
815 $tmpstr .= $tmp;
816 } else {
817 if ($prevcsize == 1) {
818 $result .= $tmpstr;
819 } else {
62f7daa5 820 $result .= str_replace(' ', '',
e842b215 821 mb_encode_mimeheader($tmpstr,'iso-2022-jp','B',''));
8ba05cbe 822 }
823 $tmpstr = $tmp;
824 $prevcsize = strlen($tmp);
825 }
826 }
827 if (strlen($tmpstr)) {
828 if (strlen(mb_substr($tmpstr, 0, 1)) == 1)
829 $result .= $tmpstr;
830 else
e842b215 831 $result .= str_replace(' ', '',
832 mb_encode_mimeheader($tmpstr,'iso-2022-jp','B',''));
8ba05cbe 833 }
834 }
835 $ret = $result;
1d33e35e 836 break;
837 case 'decodeheader':
838 $ret = str_replace("\t", "", $ret);
839 if (eregi('=\\?([^?]+)\\?(q|b)\\?([^?]+)\\?=', $ret))
e842b215 840 $ret = @mb_decode_mimeheader($ret);
841 $ret = @mb_convert_encoding($ret, 'EUC-JP', 'AUTO');
1d33e35e 842 break;
843 case 'downloadfilename':
844 $useragent = func_get_arg(2);
845 if (strstr($useragent, 'Windows') !== false ||
846 strstr($useragent, 'Mac_') !== false) {
847 $ret = mb_convert_encoding($ret, 'SJIS', 'AUTO');
848 } else {
849 $ret = mb_convert_encoding($ret, 'EUC-JP', 'AUTO');
850}
851 break;
e842b215 852 case 'wordwrap':
853 $no_begin = "\x21\x25\x29\x2c\x2e\x3a\x3b\x3f\x5d\x7d\xa1\xf1\xa1\xeb\xa1" .
854 "\xc7\xa1\xc9\xa2\xf3\xa1\xec\xa1\xed\xa1\xee\xa1\xa2\xa1\xa3\xa1\xb9" .
855 "\xa1\xd3\xa1\xd5\xa1\xd7\xa1\xd9\xa1\xdb\xa1\xcd\xa4\xa1\xa4\xa3\xa4" .
856 "\xa5\xa4\xa7\xa4\xa9\xa4\xc3\xa4\xe3\xa4\xe5\xa4\xe7\xa4\xee\xa1\xab" .
857 "\xa1\xac\xa1\xb5\xa1\xb6\xa5\xa1\xa5\xa3\xa5\xa5\xa5\xa7\xa5\xa9\xa5" .
858 "\xc3\xa5\xe3\xa5\xe5\xa5\xe7\xa5\xee\xa5\xf5\xa5\xf6\xa1\xa6\xa1\xbc" .
859 "\xa1\xb3\xa1\xb4\xa1\xaa\xa1\xf3\xa1\xcb\xa1\xa4\xa1\xa5\xa1\xa7\xa1" .
860 "\xa8\xa1\xa9\xa1\xcf\xa1\xd1";
861 $no_end = "\x5c\x24\x28\x5b\x7b\xa1\xf2\x5c\xa1\xc6\xa1\xc8\xa1\xd2\xa1" .
862 "\xd4\xa1\xd6\xa1\xd8\xa1\xda\xa1\xcc\xa1\xf0\xa1\xca\xa1\xce\xa1\xd0\xa1\xef";
863 $wrap = func_get_arg(2);
62f7daa5 864
865 if (strlen($ret) >= $wrap &&
e842b215 866 substr($ret, 0, 1) != '>' &&
867 strpos($ret, 'http://') === FALSE &&
868 strpos($ret, 'https://') === FALSE &&
869 strpos($ret, 'ftp://') === FALSE) {
62f7daa5 870
e842b215 871 $ret = mb_convert_kana($ret, "KV");
872
873 $line_new = '';
874 $ptr = 0;
62f7daa5 875
e842b215 876 while ($ptr < strlen($ret) - 1) {
877 $l = mb_strcut($ret, $ptr, $wrap);
878 $ptr += strlen($l);
879 $tmp = $l;
62f7daa5 880
e842b215 881 $l = mb_strcut($ret, $ptr, 2);
882 while (strlen($l) != 0 && mb_strpos($no_begin, $l) !== FALSE ) {
883 $tmp .= $l;
884 $ptr += strlen($l);
885 $l = mb_strcut($ret, $ptr, 1);
886 }
887 $line_new .= $tmp;
888 if ($ptr < strlen($ret) - 1)
889 $line_new .= "\n";
890 }
891 $ret = $line_new;
892 }
893 break;
894 case 'utf7-imap_encode':
895 $ret = mb_convert_encoding($ret, 'UTF7-IMAP', 'EUC-JP');
896 break;
897 case 'utf7-imap_decode':
898 $ret = mb_convert_encoding($ret, 'EUC-JP', 'UTF7-IMAP');
899 break;
1d33e35e 900 }
901 }
902 return $ret;
903}
904
1bb86586 905/**************************
906 * Japanese extra functions
907 **************************/
1d33e35e 908
51468260 909/**
1bb86586 910 * Japanese decoding function
80b0091e 911 *
62f7daa5 912 * converts string to euc-jp, if string uses JIS, EUC-JP, ShiftJIS or UTF-8
80b0091e 913 * charset. Needs mbstring support in php.
914 * @param string $ret text, that has to be converted
915 * @return string converted string
1bb86586 916 * @since 1.5.1
917 */
918function japanese_xtra_decode($ret) {
919 if (function_exists('mb_detect_encoding')) {
920 $detect_encoding = @mb_detect_encoding($ret);
921 if ($detect_encoding == 'JIS' ||
922 $detect_encoding == 'EUC-JP' ||
923 $detect_encoding == 'SJIS' ||
924 $detect_encoding == 'UTF-8') {
925
926 $ret = mb_convert_kana(mb_convert_encoding($ret, 'EUC-JP', 'AUTO'), "KV");
927 }
928 }
929 return $ret;
930}
931
932/**
933 * Japanese encoding function
80b0091e 934 *
935 * converts string to jis, if string uses JIS, EUC-JP, ShiftJIS or UTF-8
936 * charset. Needs mbstring support in php.
937 * @param string $ret text, that has to be converted
938 * @return string converted text
1bb86586 939 * @since 1.5.1
940 */
941function japanese_xtra_encode($ret) {
942 if (function_exists('mb_detect_encoding')) {
943 $detect_encoding = @mb_detect_encoding($ret);
944 if ($detect_encoding == 'JIS' ||
945 $detect_encoding == 'EUC-JP' ||
946 $detect_encoding == 'SJIS' ||
947 $detect_encoding == 'UTF-8') {
62f7daa5 948
1bb86586 949 $ret = mb_convert_encoding(mb_convert_kana($ret, "KV"), 'JIS', 'AUTO');
950 }
951 }
952 return $ret;
953}
954
955/**
956 * Japanese header encoding function
80b0091e 957 *
958 * creates base64 encoded header in iso-2022-jp charset
959 * @param string $ret text, that has to be converted
960 * @return string mime base64 encoded string
1bb86586 961 * @since 1.5.1
962 */
963function japanese_xtra_encodeheader($ret) {
964 if (function_exists('mb_detect_encoding')) {
965 $result = '';
966 if (strlen($ret) > 0) {
967 $tmpstr = mb_substr($ret, 0, 1);
968 $prevcsize = strlen($tmpstr);
969 for ($i = 1; $i < mb_strlen($ret); $i++) {
970 $tmp = mb_substr($ret, $i, 1);
971 if (strlen($tmp) == $prevcsize) {
972 $tmpstr .= $tmp;
973 } else {
974 if ($prevcsize == 1) {
975 $result .= $tmpstr;
976 } else {
62f7daa5 977 $result .= str_replace(' ', '',
1bb86586 978 mb_encode_mimeheader($tmpstr,'iso-2022-jp','B',''));
979 }
980 $tmpstr = $tmp;
981 $prevcsize = strlen($tmp);
982 }
983 }
984 if (strlen($tmpstr)) {
985 if (strlen(mb_substr($tmpstr, 0, 1)) == 1)
986 $result .= $tmpstr;
987 else
988 $result .= str_replace(' ', '',
989 mb_encode_mimeheader($tmpstr,'iso-2022-jp','B',''));
990 }
991 }
992 $ret = $result;
993 }
994 return $ret;
995}
996
997/**
998 * Japanese header decoding function
80b0091e 999 *
62f7daa5 1000 * return human readable string from mime header. string is returned in euc-jp
80b0091e 1001 * charset.
1002 * @param string $ret header string
1003 * @return string decoded header string
1bb86586 1004 * @since 1.5.1
1005 */
1006function japanese_xtra_decodeheader($ret) {
1007 if (function_exists('mb_detect_encoding')) {
1008 $ret = str_replace("\t", "", $ret);
1009 if (eregi('=\\?([^?]+)\\?(q|b)\\?([^?]+)\\?=', $ret))
1010 $ret = @mb_decode_mimeheader($ret);
1011 $ret = @mb_convert_encoding($ret, 'EUC-JP', 'AUTO');
1012 }
1013 return $ret;
1014}
1015
1016/**
1017 * Japanese downloaded filename processing function
80b0091e 1018 *
1019 * Returns shift-jis or euc-jp encoded file name
1020 * @param string $ret string
1021 * @param string $useragent browser
1022 * @return string converted string
1bb86586 1023 * @since 1.5.1
1024 */
1025function japanese_xtra_downloadfilename($ret,$useragent) {
1026 if (function_exists('mb_detect_encoding')) {
1027 if (strstr($useragent, 'Windows') !== false ||
1028 strstr($useragent, 'Mac_') !== false) {
1029 $ret = mb_convert_encoding($ret, 'SJIS', 'AUTO');
1030 } else {
1031 $ret = mb_convert_encoding($ret, 'EUC-JP', 'AUTO');
1032 }
1033 }
1034 return $ret;
1035}
1036
1037/**
1038 * Japanese wordwrap function
62f7daa5 1039 *
80b0091e 1040 * wraps text at set number of symbols
1041 * @param string $ret text
1042 * @param integer $wrap number of symbols per line
1043 * @return string wrapped text
1bb86586 1044 * @since 1.5.1
1045 */
1046function japanese_xtra_wordwrap($ret,$wrap) {
1047 if (function_exists('mb_detect_encoding')) {
1048 $no_begin = "\x21\x25\x29\x2c\x2e\x3a\x3b\x3f\x5d\x7d\xa1\xf1\xa1\xeb\xa1" .
1049 "\xc7\xa1\xc9\xa2\xf3\xa1\xec\xa1\xed\xa1\xee\xa1\xa2\xa1\xa3\xa1\xb9" .
1050 "\xa1\xd3\xa1\xd5\xa1\xd7\xa1\xd9\xa1\xdb\xa1\xcd\xa4\xa1\xa4\xa3\xa4" .
1051 "\xa5\xa4\xa7\xa4\xa9\xa4\xc3\xa4\xe3\xa4\xe5\xa4\xe7\xa4\xee\xa1\xab" .
1052 "\xa1\xac\xa1\xb5\xa1\xb6\xa5\xa1\xa5\xa3\xa5\xa5\xa5\xa7\xa5\xa9\xa5" .
1053 "\xc3\xa5\xe3\xa5\xe5\xa5\xe7\xa5\xee\xa5\xf5\xa5\xf6\xa1\xa6\xa1\xbc" .
1054 "\xa1\xb3\xa1\xb4\xa1\xaa\xa1\xf3\xa1\xcb\xa1\xa4\xa1\xa5\xa1\xa7\xa1" .
1055 "\xa8\xa1\xa9\xa1\xcf\xa1\xd1";
1056 $no_end = "\x5c\x24\x28\x5b\x7b\xa1\xf2\x5c\xa1\xc6\xa1\xc8\xa1\xd2\xa1" .
1057 "\xd4\xa1\xd6\xa1\xd8\xa1\xda\xa1\xcc\xa1\xf0\xa1\xca\xa1\xce\xa1\xd0\xa1\xef";
62f7daa5 1058
1059 if (strlen($ret) >= $wrap &&
1bb86586 1060 substr($ret, 0, 1) != '>' &&
1061 strpos($ret, 'http://') === FALSE &&
1062 strpos($ret, 'https://') === FALSE &&
1063 strpos($ret, 'ftp://') === FALSE) {
1064
1065 $ret = mb_convert_kana($ret, "KV");
62f7daa5 1066
1bb86586 1067 $line_new = '';
1068 $ptr = 0;
62f7daa5 1069
1bb86586 1070 while ($ptr < strlen($ret) - 1) {
1071 $l = mb_strcut($ret, $ptr, $wrap);
1072 $ptr += strlen($l);
1073 $tmp = $l;
62f7daa5 1074
1bb86586 1075 $l = mb_strcut($ret, $ptr, 2);
1076 while (strlen($l) != 0 && mb_strpos($no_begin, $l) !== FALSE ) {
1077 $tmp .= $l;
1078 $ptr += strlen($l);
1079 $l = mb_strcut($ret, $ptr, 1);
1080 }
1081 $line_new .= $tmp;
1082 if ($ptr < strlen($ret) - 1)
1083 $line_new .= "\n";
1084 }
1085 $ret = $line_new;
1086 }
1087 }
1088 return $ret;
1089}
1090
1091/**
1092 * Japanese imap folder name encoding function
80b0091e 1093 *
1094 * converts folder name from euc-jp to utf7-imap
1095 * @param string $ret folder name
1096 * @return string converted folder name
1bb86586 1097 * @since 1.5.1
1098 */
1099function japanese_xtra_utf7_imap_encode($ret){
1100 if (function_exists('mb_detect_encoding')) {
1101 $ret = mb_convert_encoding($ret, 'UTF7-IMAP', 'EUC-JP');
1102 }
1103 return $ret;
1104}
1105
1106/**
1107 * Japanese imap folder name decoding function
80b0091e 1108 *
1109 * converts folder name from utf7-imap to euc-jp.
1110 * @param string $ret folder name in utf7-imap
1111 * @return string converted folder name
1bb86586 1112 * @since 1.5.1
1113 */
1114function japanese_xtra_utf7_imap_decode($ret) {
1115 if (function_exists('mb_detect_encoding')) {
1116 $ret = mb_convert_encoding($ret, 'EUC-JP', 'UTF7-IMAP');
1117 }
1118 return $ret;
1119}
1120
1121/**
1122 * Japanese string trimming function
80b0091e 1123 *
1124 * trims string to defined number of symbols
1125 * @param string $ret string
1126 * @param integer $width number of symbols
1127 * @return string trimmed string
1bb86586 1128 * @since 1.5.1
1129 */
1130function japanese_xtra_strimwidth($ret,$width) {
1131 if (function_exists('mb_detect_encoding')) {
1132 $ret = mb_strimwidth($ret, 0, $width, '...');
1133 }
1134 return $ret;
1135}
1136
1137/********************************
51468260 1138 * Korean charset extra functions
1bb86586 1139 ********************************/
1140
1141/**
1142 * Korean downloaded filename processing functions
51468260 1143 *
51468260 1144 * @param string default return value
62f7daa5 1145 * @return string
1d33e35e 1146 */
1bb86586 1147function korean_xtra_downloadfilename($ret) {
1148 $ret = str_replace("\x0D\x0A", '', $ret); /* Hanmail's CR/LF Clear */
1149 for ($i=0;$i<strlen($ret);$i++) {
1150 if ($ret[$i] >= "\xA1" && $ret[$i] <= "\xFE") { /* 0xA1 - 0XFE are Valid */
1151 $i++;
1152 continue;
1153 } else if (($ret[$i] >= 'a' && $ret[$i] <= 'z') || /* From Original ereg_replace in download.php */
1154 ($ret[$i] >= 'A' && $ret[$i] <= 'Z') ||
1155 ($ret[$i] == '.') || ($ret[$i] == '-')) {
1156 continue;
1157 } else {
1158 $ret[$i] = '_';
1d33e35e 1159 }
1d33e35e 1160 }
1d33e35e 1161 return $ret;
1162}
1163
db08d0c3 1164/**
1165 * Replaces non-braking spaces inserted by some browsers with regular space
62f7daa5 1166 *
1167 * This function can be used to replace non-braking space symbols
1168 * that are inserted in forms by some browsers instead of normal
9af9c0a2 1169 * space symbol.
db08d0c3 1170 *
1171 * @param string $string Text that needs to be cleaned
1172 * @param string $charset Charset used in text
1173 * @return string Cleaned text
9af9c0a2 1174 */
1175function cleanup_nbsp($string,$charset) {
1176
1177 // reduce number of case statements
1178 if (stristr('iso-8859-',substr($charset,0,9))){
1179 $output_charset="iso-8859-x";
1180 }
1181 if (stristr('windows-125',substr($charset,0,11))){
1182 $output_charset="cp125x";
1183 }
1184 if (stristr('koi8',substr($charset,0,4))){
1185 $output_charset="koi8-x";
1186 }
1187 if (! isset($output_charset)){
1188 $output_charset=strtolower($charset);
1189 }
1190
1191// where is non-braking space symbol
1192switch($output_charset):
1193 case "iso-8859-x":
97b9c02f 1194 case "cp125x":
1195 case "iso-2022-jp":
9af9c0a2 1196 $nbsp="\xA0";
1197 break;
9af9c0a2 1198 case "koi8-x":
1199 $nbsp="\x9A";
1200 break;
1201 case "utf-8":
1202 $nbsp="\xC2\xA0";
1203 break;
9af9c0a2 1204 default:
1205 // don't change string if charset is unmatched
1206 return $string;
1207endswitch;
1208
62f7daa5 1209// return space instead of non-braking space.
9af9c0a2 1210 return str_replace($nbsp,' ',$string);
1211}
4e519821 1212
db08d0c3 1213/**
1214 * Function informs if it is safe to convert given charset to the one that is used by user.
1215 *
62f7daa5 1216 * It is safe to use conversion only if user uses utf-8 encoding and when
db08d0c3 1217 * converted charset is similar to the one that is used by user.
1218 *
1219 * @param string $input_charset Charset of text that needs to be converted
1220 * @return bool is it possible to convert to user's charset
1221 */
4e519821 1222function is_conversion_safe($input_charset) {
6d3689f5 1223 global $languages, $sm_notAlias, $default_charset, $lossy_encoding;
33991968 1224
6d3689f5 1225 if (isset($lossy_encoding) && $lossy_encoding )
33991968 1226 return true;
4e519821 1227
1228 // convert to lower case
1229 $input_charset = strtolower($input_charset);
1230
1231 // Is user's locale Unicode based ?
1232 if ( $default_charset == "utf-8" ) {
1233 return true;
1234 }
1235
1236 // Charsets that are similar
1237switch ($default_charset):
1238case "windows-1251":
62f7daa5 1239 if ( $input_charset == "iso-8859-5" ||
03db90bc 1240 $input_charset == "koi8-r" ||
1241 $input_charset == "koi8-u" ) {
4e519821 1242 return true;
1243 } else {
1244 return false;
1245 }
1246case "windows-1257":
62f7daa5 1247 if ( $input_charset == "iso-8859-13" ||
03db90bc 1248 $input_charset == "iso-8859-4" ) {
4e519821 1249 return true;
1250 } else {
1251 return false;
1252 }
1253case "iso-8859-4":
62f7daa5 1254 if ( $input_charset == "iso-8859-13" ||
03db90bc 1255 $input_charset == "windows-1257" ) {
4e519821 1256 return true;
1257 } else {
1258 return false;
1259 }
1260case "iso-8859-5":
62f7daa5 1261 if ( $input_charset == "windows-1251" ||
1262 $input_charset == "koi8-r" ||
03db90bc 1263 $input_charset == "koi8-u" ) {
4e519821 1264 return true;
1265 } else {
1266 return false;
1267 }
1268case "iso-8859-13":
1269 if ( $input_charset == "iso-8859-4" ||
1270 $input_charset == "windows-1257" ) {
1271 return true;
1272 } else {
1273 return false;
1274 }
1275case "koi8-r":
1276 if ( $input_charset == "windows-1251" ||
62f7daa5 1277 $input_charset == "iso-8859-5" ||
03db90bc 1278 $input_charset == "koi8-u" ) {
4e519821 1279 return true;
1280 } else {
1281 return false;
1282 }
1283case "koi8-u":
1284 if ( $input_charset == "windows-1251" ||
03db90bc 1285 $input_charset == "iso-8859-5" ||
1286 $input_charset == "koi8-r" ) {
4e519821 1287 return true;
1288 } else {
1289 return false;
1290 }
1291default:
1292 return false;
1293endswitch;
1294}
51468260 1295?>