6 * Copyright (c) 1999-2004 The SquirrelMail Project Team
7 * Licensed under the GNU GPL. For full terms see the file COPYING.
9 * This file contains variuos functions that are needed to do
10 * internationalization of SquirrelMail.
12 * Internally the output character set is used. Other characters are
13 * encoded using Unicode entities according to HTML 4.0.
16 * @package squirrelmail
19 /** Everything uses global.php... */
20 require_once(SM_PATH
. 'functions/global.php');
23 * Converts string from given charset to charset, that can be displayed by user translation.
25 * Function by default returns html encoded strings, if translation uses different encoding.
26 * If Japanese translation is used - function returns string converted to euc-jp
27 * If iconv or recode functions are enabled and translation uses utf-8 - function returns utf-8 encoded string.
28 * If $charset is not supported - function returns unconverted string.
30 * sanitizing of html tags is also done by this function.
32 * @param string $charset
33 * @param string $string Text to be decoded
34 * @return string decoded string
36 function charset_decode ($charset, $string) {
37 global $languages, $squirrelmail_language, $default_charset;
38 global $use_php_recode, $use_php_iconv, $agresive_decoding;
40 if (isset($languages[$squirrelmail_language]['XTRA_CODE']) &&
41 function_exists($languages[$squirrelmail_language]['XTRA_CODE'])) {
42 $string = $languages[$squirrelmail_language]['XTRA_CODE']('decode', $string);
45 $charset = strtolower($charset);
49 // Variables that allow to use functions without function_exist() calls
50 if (! isset($use_php_recode) ||
$use_php_recode=="" ) {
51 $use_php_recode=false; }
52 if (! isset($use_php_iconv) ||
$use_php_iconv=="" ) {
53 $use_php_iconv=false; }
55 // Don't do conversion if charset is the same.
56 if ( $charset == strtolower($default_charset) )
57 return htmlspecialchars($string);
59 // catch iso-8859-8-i thing
60 if ( $charset == "iso-8859-8-i" )
61 $charset = "iso-8859-8";
64 * Recode converts html special characters automatically if you use
65 * 'charset..html' decoding. There is no documented way to put -d option
66 * into php recode function call.
68 if ( $use_php_recode ) {
69 if ( $default_charset == "utf-8" ) {
70 // other charsets can be converted to utf-8 without loss.
71 // and output string is smaller
72 $string = recode_string($charset . "..utf-8",$string);
73 return htmlspecialchars($string);
75 $string = recode_string($charset . "..html",$string);
76 // recode does not convert single quote, htmlspecialchars does.
77 $string = str_replace("'", ''', $string);
82 // iconv functions does not have html target and can be used only with utf-8
83 if ( $use_php_iconv && $default_charset=='utf-8') {
84 $string = iconv($charset,$default_charset,$string);
85 return htmlspecialchars($string);
88 // If we don't use recode and iconv, we'll do it old way.
90 /* All HTML special characters are 7 bit and can be replaced first */
92 $string = htmlspecialchars ($string);
94 /* controls cpu and memory intensive decoding cycles */
95 if (! isset($agresive_decoding) ||
$agresive_decoding=="" ) {
96 $agresive_decoding=false; }
98 $decode=fixcharset($charset);
99 $decodefile=SM_PATH
. 'functions/decode/' . $decode . '.php';
100 if (file_exists($decodefile)) {
101 include_once($decodefile);
102 $ret = call_user_func('charset_decode_'.$decode, $string);
110 * Makes charset name suitable for decoding cycles
112 * @param string $charset Name of charset
113 * @return string $charset Adjusted name of charset
115 function fixcharset($charset) {
116 // minus removed from function names
117 $charset=str_replace('-','_',$charset);
119 // windows-125x and cp125x charsets
120 $charset=str_replace('windows_','cp',$charset);
123 $charset=str_replace('ibm','cp',$charset);
125 // iso-8859-8-i -> iso-8859-8
126 // use same cycle until I'll find differences
127 $charset=str_replace('iso_8859_8_i','iso_8859_8',$charset);
133 * Set up the language to be output
134 * if $do_search is true, then scan the browser information
135 * for a possible language that we know
137 * Function sets system locale environment (LC_ALL, LANG, LANGUAGE),
138 * gettext translation bindings and html header information.
140 * Function returns error codes, if there is some fatal error.
142 * 1 = mbstring support is not present,
143 * 2 = mbstring support is not present, user's translation reverted to en_US.
145 * @param string $sm_language translation used by user's interface
146 * @param bool $do_search use browser's preferred language detection functions. Defaults to false.
147 * @param bool $default set $sm_language to $squirrelmail_default_language if language detection fails or language is not set. Defaults to false.
148 * @return int function execution error codes.
150 function set_up_language($sm_language, $do_search = false, $default = false) {
152 static $SetupAlready = 0;
153 global $use_gettext, $languages,
154 $squirrelmail_language, $squirrelmail_default_language,
155 $sm_notAlias, $username, $data_dir;
161 $SetupAlready = TRUE;
162 sqgetGlobalVar('HTTP_ACCEPT_LANGUAGE', $accept_lang, SQ_SERVER
);
164 if ($do_search && ! $sm_language && isset($accept_lang)) {
165 $sm_language = substr($accept_lang, 0, 2);
168 if ((!$sm_language||
$default) && isset($squirrelmail_default_language)) {
169 $squirrelmail_language = $squirrelmail_default_language;
170 $sm_language = $squirrelmail_default_language;
172 $sm_notAlias = $sm_language;
174 // Catching removed translation
175 // System reverts to English translation if user prefs contain translation
176 // that is not available in $languages array
177 if (!isset($languages[$sm_notAlias])) {
178 $sm_notAlias="en_US";
181 while (isset($languages[$sm_notAlias]['ALIAS'])) {
182 $sm_notAlias = $languages[$sm_notAlias]['ALIAS'];
185 if ( isset($sm_language) &&
187 $sm_language != '' &&
188 isset($languages[$sm_notAlias]['CHARSET']) ) {
189 bindtextdomain( 'squirrelmail', SM_PATH
. 'locale/' );
190 textdomain( 'squirrelmail' );
191 if (function_exists('bind_textdomain_codeset')) {
192 if ($sm_notAlias == 'ja_JP') {
193 bind_textdomain_codeset ("squirrelmail", 'EUC-JP');
195 bind_textdomain_codeset ("squirrelmail", $languages[$sm_notAlias]['CHARSET'] );
198 if (isset($languages[$sm_notAlias]['LOCALE'])){
199 $longlocale=$languages[$sm_notAlias]['LOCALE'];
201 $longlocale=$sm_notAlias;
203 if ( !ini_get('safe_mode') &&
204 getenv( 'LC_ALL' ) != $longlocale ) {
205 putenv( "LC_ALL=$longlocale" );
206 putenv( "LANG=$longlocale" );
207 putenv( "LANGUAGE=$longlocale" );
209 setlocale(LC_ALL
, $longlocale);
211 // Set text direction/alignment variables
212 if (isset($languages[$sm_notAlias]['DIR']) &&
213 $languages[$sm_notAlias]['DIR'] == 'rtl') {
214 $text_direction='rtl';
218 $text_direction='ltr';
220 $right_align='right';
223 $squirrelmail_language = $sm_notAlias;
224 if ($squirrelmail_language == 'ja_JP') {
225 header ('Content-Type: text/html; charset=EUC-JP');
226 if (!function_exists('mb_internal_encoding')) {
227 // Error messages can't be displayed here
229 // Revert to English if possible.
230 if (function_exists('setPref') && $username!='' && $data_dir!="") {
231 setPref($data_dir, $username, 'language', "en_US");
234 // stop further execution in order not to get php errors on mb_internal_encoding().
237 if (function_exists('mb_language')) {
238 mb_language('Japanese');
240 mb_internal_encoding('EUC-JP');
241 mb_http_output('pass');
243 header( 'Content-Type: text/html; charset=' . $languages[$sm_notAlias]['CHARSET'] );
250 * Sets default_charset variable according to the one that is used by user's translations.
252 * Function changes global $default_charset variable in order to be sure, that it
253 * contains charset used by user's translation. Sanity of $squirrelmail_default_language
254 * and $default_charset combination provided in SquirrelMail config is also tested.
256 * There can be a $default_charset setting in the
257 * config.php file, but the user may have a different language
258 * selected for a user interface. This function checks the
259 * language selected by the user and tags the outgoing messages
260 * with the appropriate charset corresponding to the language
261 * selection. This is "more right" (tm), than just stamping the
262 * message blindly with the system-wide $default_charset.
264 function set_my_charset(){
265 global $data_dir, $username, $default_charset, $languages, $squirrelmail_default_language;
267 $my_language = getPref($data_dir, $username, 'language');
269 $my_language = $squirrelmail_default_language ;
271 // Catch removed translation
272 if (!isset($languages[$my_language])) {
273 $my_language="en_US";
275 while (isset($languages[$my_language]['ALIAS'])) {
276 $my_language = $languages[$my_language]['ALIAS'];
278 $my_charset = $languages[$my_language]['CHARSET'];
280 $default_charset = $my_charset;
284 /* ------------------------------ main --------------------------- */
286 global $squirrelmail_language, $languages, $use_gettext;
288 if (! isset($squirrelmail_language)) {
289 $squirrelmail_language = '';
293 * Array specifies the available translations.
295 * Structure of array:
296 * $languages['language']['variable'] = 'value'
298 * Possible 'variable' names:
299 * NAME - Translation name in English
300 * CHARSET - Encoding used by translation
301 * ALIAS - used when 'language' is only short name and 'value' should provide long language name
302 * ALTNAME - Native translation name. Any 8bit symbols must be html encoded.
303 * LOCALE - Full locale name (in xx_XX.charset format)
304 * DIR - Text direction. Used to define Right-to-Left languages. Possible values 'rtl' or 'ltr'. If undefined - defaults to 'ltr'
305 * XTRA_CODE - translation uses special functions. 'value' provides name of that extra function
307 * Each 'language' definition requires NAME+CHARSET or ALIAS variables.
312 $languages['bg_BG']['NAME'] = 'Bulgarian';
313 $languages['bg_BG']['ALTNAME'] = 'Български';
314 $languages['bg_BG']['CHARSET'] = 'windows-1251';
315 $languages['bg_BG']['LOCALE'] = 'bg_BG.CP1251';
316 $languages['bg']['ALIAS'] = 'bg_BG';
318 $languages['ca_ES']['NAME'] = 'Catalan';
319 $languages['ca_ES']['CHARSET'] = 'iso-8859-1';
320 $languages['ca_ES']['LOCALE'] = 'ca_ES.ISO-8859-1';
321 $languages['ca']['ALIAS'] = 'ca_ES';
323 $languages['cs_CZ']['NAME'] = 'Czech';
324 $languages['cs_CZ']['ALTNAME'] = 'Čeština';
325 $languages['cs_CZ']['CHARSET'] = 'iso-8859-2';
326 $languages['cs_CZ']['LOCALE'] = 'cs_CZ.ISO-8859-2';
327 $languages['cs']['ALIAS'] = 'cs_CZ';
329 $languages['cy_GB']['NAME'] = 'Welsh';
330 $languages['cy_GB']['ALTNAME'] = 'Cymraeg';
331 $languages['cy_GB']['CHARSET'] = 'iso-8859-1';
332 $languages['cy_GB']['LOCALE'] = 'cy_GB.ISO-8859-1';
333 $languages['cy']['ALIAS'] = 'cy_GB';
335 // Danish locale is da_DK.
336 $languages['da_DK']['NAME'] = 'Danish';
337 $languages['da_DK']['ALTNAME'] = 'Dansk';
338 $languages['da_DK']['CHARSET'] = 'iso-8859-1';
339 $languages['da_DK']['LOCALE'] = 'da_DK.ISO-8859-1';
340 $languages['da']['ALIAS'] = 'da_DK';
342 $languages['de_DE']['NAME'] = 'German';
343 $languages['de_DE']['ALTNAME'] = 'Deutsch';
344 $languages['de_DE']['CHARSET'] = 'iso-8859-1';
345 $languages['de_DE']['LOCALE'] = 'de_DE.ISO-8859-1';
346 $languages['de']['ALIAS'] = 'de_DE';
348 $languages['el_GR']['NAME'] = 'Greek';
349 $languages['el_GR']['ALTNAME'] = 'Ελληνικά';
350 $languages['el_GR']['CHARSET'] = 'iso-8859-7';
351 $languages['el_GR']['LOCALE'] = 'el_GR.ISO-8859-7';
352 $languages['el']['ALIAS'] = 'el_GR';
354 $languages['en_GB']['NAME'] = 'British';
355 $languages['en_GB']['CHARSET'] = 'iso-8859-15';
356 $languages['en_GB']['LOCALE'] = 'en_GB.ISO-8859-15';
358 $languages['en_US']['NAME'] = 'English';
359 $languages['en_US']['CHARSET'] = 'iso-8859-1';
360 $languages['en_US']['LOCALE'] = 'en_US.ISO-8859-1';
361 $languages['en']['ALIAS'] = 'en_US';
363 $languages['es_ES']['NAME'] = 'Spanish';
364 $languages['es_ES']['ALTNAME'] = 'Español';
365 $languages['es_ES']['CHARSET'] = 'iso-8859-1';
366 $languages['es_ES']['LOCALE'] = 'es_ES.ISO-8859-1';
367 $languages['es']['ALIAS'] = 'es_ES';
369 $languages['et_EE']['NAME'] = 'Estonian';
370 $languages['et_EE']['CHARSET'] = 'iso-8859-15';
371 $languages['et_EE']['LOCALE'] = 'et_EE.ISO-8859-15';
372 $languages['et']['ALIAS'] = 'et_EE';
374 $languages['fo_FO']['NAME'] = 'Faroese';
375 $languages['fo_FO']['CHARSET'] = 'iso-8859-1';
376 $languages['fo_FO']['LOCALE'] = 'fo_FO.ISO-8859-1';
377 $languages['fo']['ALIAS'] = 'fo_FO';
379 $languages['fi_FI']['NAME'] = 'Finnish';
380 $languages['fi_FI']['ALTNAME'] = 'Suomi';
381 $languages['fi_FI']['CHARSET'] = 'iso-8859-1';
382 $languages['fi_FI']['LOCALE'] = 'fi_FI.ISO-8859-1';
383 $languages['fi']['ALIAS'] = 'fi_FI';
385 $languages['fr_FR']['NAME'] = 'French';
386 $languages['fr_FR']['ALTNAME'] = 'Français';
387 $languages['fr_FR']['CHARSET'] = 'iso-8859-1';
388 $languages['fr_FR']['LOCALE'] = 'fr_FR.ISO-8859-1';
389 $languages['fr']['ALIAS'] = 'fr_FR';
391 $languages['hr_HR']['NAME'] = 'Croatian';
392 $languages['hr_HR']['CHARSET'] = 'iso-8859-2';
393 $languages['hr_HR']['LOCALE'] = 'hr_HR.ISO-8859-2';
394 $languages['hr']['ALIAS'] = 'hr_HR';
396 $languages['hu_HU']['NAME'] = 'Hungarian';
397 $languages['hu_HU']['ALTNAME'] = 'Magyar';
398 $languages['hu_HU']['CHARSET'] = 'iso-8859-2';
399 $languages['hu_HU']['LOCALE'] = 'hu_HU.ISO-8859-2';
400 $languages['hu']['ALIAS'] = 'hu_HU';
402 $languages['id_ID']['NAME'] = 'Indonesian';
403 $languages['id_ID']['ALTNAME'] = 'Bahasa Indonesia';
404 $languages['id_ID']['CHARSET'] = 'iso-8859-1';
405 $languages['id_ID']['LOCALE'] = 'id_ID.ISO-8859-1';
406 $languages['id']['ALIAS'] = 'id_ID';
408 $languages['is_IS']['NAME'] = 'Icelandic';
409 $languages['is_IS']['ALTNAME'] = 'Íslenska';
410 $languages['is_IS']['CHARSET'] = 'iso-8859-1';
411 $languages['is_IS']['LOCALE'] = 'is_IS.ISO-8859-1';
412 $languages['is']['ALIAS'] = 'is_IS';
414 $languages['it_IT']['NAME'] = 'Italian';
415 $languages['it_IT']['CHARSET'] = 'iso-8859-1';
416 $languages['it_IT']['LOCALE'] = 'it_IT.ISO-8859-1';
417 $languages['it']['ALIAS'] = 'it_IT';
419 $languages['ja_JP']['NAME'] = 'Japanese';
420 $languages['ja_JP']['ALTNAME'] = '日本語';
421 $languages['ja_JP']['CHARSET'] = 'iso-2022-jp';
422 $languages['ja_JP']['LOCALE'] = 'ja_JP.EUC-JP';
423 $languages['ja_JP']['XTRA_CODE'] = 'japanese_charset_xtra';
424 $languages['ja']['ALIAS'] = 'ja_JP';
426 $languages['ko_KR']['NAME'] = 'Korean';
427 $languages['ko_KR']['CHARSET'] = 'euc-KR';
428 $languages['ko_KR']['LOCALE'] = 'ko_KR.EUC-KR';
429 $languages['ko_KR']['XTRA_CODE'] = 'korean_charset_xtra';
430 $languages['ko']['ALIAS'] = 'ko_KR';
432 $languages['lt_LT']['NAME'] = 'Lithuanian';
433 $languages['lt_LT']['ALTNAME'] = 'Lietuvių';
434 $languages['lt_LT']['CHARSET'] = 'utf-8';
435 $languages['lt_LT']['LOCALE'] = 'lt_LT.UTF-8';
436 $languages['lt']['ALIAS'] = 'lt_LT';
438 $languages['nl_NL']['NAME'] = 'Dutch';
439 $languages['nl_NL']['ALTNAME'] = 'Nederlands';
440 $languages['nl_NL']['CHARSET'] = 'iso-8859-1';
441 $languages['nl_NL']['LOCALE'] = 'nl_NL.ISO-8859-1';
442 $languages['nl']['ALIAS'] = 'nl_NL';
444 $languages['ms_MY']['NAME'] = 'Malay';
445 $languages['ms_MY']['ALTNAME'] = 'Bahasa Melayu';
446 $languages['ms_MY']['CHARSET'] = 'iso-8859-1';
447 $languages['ms_MY']['LOCALE'] = 'ms_MY.ISO-8859-1';
448 $languages['my']['ALIAS'] = 'ms_MY';
450 $languages['nb_NO']['NAME'] = 'Norwegian (Bokmål)';
451 $languages['nb_NO']['ALTNAME'] = 'Norsk (Bokmål)';
452 $languages['nb_NO']['CHARSET'] = 'iso-8859-1';
453 $languages['nb_NO']['LOCALE'] = 'nb_NO.ISO-8859-1';
454 $languages['nb']['ALIAS'] = 'nb_NO';
456 $languages['nn_NO']['NAME'] = 'Norwegian (Nynorsk)';
457 $languages['nn_NO']['ALTNAME'] = 'Norsk (Nynorsk)';
458 $languages['nn_NO']['CHARSET'] = 'iso-8859-1';
459 $languages['nn_NO']['LOCALE'] = 'nn_NO.ISO-8859-1';
461 $languages['pl_PL']['NAME'] = 'Polish';
462 $languages['pl_PL']['ALTNAME'] = 'Polski';
463 $languages['pl_PL']['CHARSET'] = 'iso-8859-2';
464 $languages['pl_PL']['LOCALE'] = 'pl_PL.ISO-8859-2';
465 $languages['pl']['ALIAS'] = 'pl_PL';
467 $languages['pt_PT']['NAME'] = 'Portuguese (Portugal)';
468 $languages['pt_PT']['CHARSET'] = 'iso-8859-1';
469 $languages['pt_PT']['LOCALE'] = 'pt_PT.ISO-8859-1';
470 $languages['pt']['ALIAS'] = 'pt_PT';
472 $languages['pt_BR']['NAME'] = 'Portuguese (Brazil)';
473 $languages['pt_BR']['ALTNAME'] = 'Português do Brasil';
474 $languages['pt_BR']['CHARSET'] = 'iso-8859-1';
475 $languages['pt_BR']['LOCALE'] = 'pt_BR.ISO-8859-1';
477 $languages['ro_RO']['NAME'] = 'Romanian';
478 $languages['ro_RO']['ALTNAME'] = 'Română';
479 $languages['ro_RO']['CHARSET'] = 'iso-8859-2';
480 $languages['ro_RO']['LOCALE'] = 'ro_RO.ISO-8859-2';
481 $languages['ro']['ALIAS'] = 'ro_RO';
483 $languages['ru_RU']['NAME'] = 'Russian';
484 $languages['ru_RU']['ALTNAME'] = 'Русский';
485 $languages['ru_RU']['CHARSET'] = 'utf-8';
486 $languages['ru_RU']['LOCALE'] = 'ru_RU.UTF-8';
487 $languages['ru']['ALIAS'] = 'ru_RU';
489 $languages['sk_SK']['NAME'] = 'Slovak';
490 $languages['sk_SK']['CHARSET'] = 'iso-8859-2';
491 $languages['sk_SK']['LOCALE'] = 'sk_SK.ISO-8859-2';
492 $languages['sk']['ALIAS'] = 'sk_SK';
494 $languages['sl_SI']['NAME'] = 'Slovenian';
495 $languages['sl_SI']['ALTNAME'] = 'Slovenščina';
496 $languages['sl_SI']['CHARSET'] = 'iso-8859-2';
497 $languages['sl_SI']['LOCALE'] = 'sl_SI.ISO-8859-2';
498 $languages['sl']['ALIAS'] = 'sl_SI';
500 $languages['sr_YU']['NAME'] = 'Serbian';
501 $languages['sr_YU']['ALTNAME'] = 'Srpski';
502 $languages['sr_YU']['CHARSET'] = 'iso-8859-2';
503 $languages['sr_YU']['LOCALE'] = 'sr_YU.ISO-8859-2';
504 $languages['sr']['ALIAS'] = 'sr_YU';
506 $languages['sv_SE']['NAME'] = 'Swedish';
507 $languages['sv_SE']['ALTNAME'] = 'Svenska';
508 $languages['sv_SE']['CHARSET'] = 'iso-8859-1';
509 $languages['sv_SE']['LOCALE'] = 'sv_SE.ISO-8859-1';
510 $languages['sv']['ALIAS'] = 'sv_SE';
512 $languages['th_TH']['NAME'] = 'Thai';
513 $languages['th_TH']['CHARSET'] = 'tis-620';
514 $languages['th_TH']['LOCALE'] = 'th_TH.TIS-620';
515 $languages['th']['ALIAS'] = 'th_TH';
517 $languages['tl_PH']['NAME'] = 'Tagalog';
518 $languages['tl_PH']['CHARSET'] = 'iso-8859-1';
519 $languages['tl_PH']['LOCALE'] = 'tl_PH.ISO-8859-1';
520 $languages['tl']['ALIAS'] = 'tl_PH';
522 $languages['tr_TR']['NAME'] = 'Turkish';
523 $languages['tr_TR']['CHARSET'] = 'iso-8859-9';
524 $languages['tr_TR']['LOCALE'] = 'tr_TR.ISO-8859-9';
525 $languages['tr']['ALIAS'] = 'tr_TR';
527 $languages['zh_TW']['NAME'] = 'Chinese Trad';
528 $languages['zh_TW']['CHARSET'] = 'big5';
529 $languages['zh_TW']['LOCALE'] = 'zh_TW.BIG5';
530 $languages['tw']['ALIAS'] = 'zh_TW';
532 $languages['zh_CN']['NAME'] = 'Chinese Simp';
533 $languages['zh_CN']['CHARSET'] = 'gb2312';
534 $languages['zh_CN']['LOCALE'] = 'zh_CN.GB2312';
535 $languages['cn']['ALIAS'] = 'zh_CN';
537 $languages['uk_UA']['NAME'] = 'Ukrainian';
538 $languages['uk_UA']['CHARSET'] = 'koi8-u';
539 $languages['uk_UA']['LOCALE'] = 'uk_UA.KOI8-U';
540 $languages['uk']['ALIAS'] = 'uk_UA';
542 $languages['ru_UA']['NAME'] = 'Russian (Ukrainian)';
543 $languages['ru_UA']['CHARSET'] = 'koi8-r';
544 $languages['ru_UA']['LOCALE'] = 'ru_UA.KOI8-R';
547 $languages['vi_VN']['NAME'] = 'Vietnamese';
548 $languages['vi_VN']['CHARSET'] = 'utf-8';
549 $languages['vi']['ALIAS'] = 'vi_VN';
552 // Right to left languages
553 $languages['ar']['NAME'] = 'Arabic';
554 $languages['ar']['CHARSET'] = 'windows-1256';
555 $languages['ar']['DIR'] = 'rtl';
557 $languages['fa_IR']['NAME'] = 'Farsi';
558 $languages['fa_IR']['CHARSET'] = 'utf-8';
559 $languages['fa_IR']['DIR'] = 'rtl';
560 $languages['fa_IR']['LOCALE'] = 'fa_IR.UTF-8';
561 $languages['fa']['ALIAS'] = 'fa_IR';
563 $languages['he_IL']['NAME'] = 'Hebrew';
564 $languages['he_IL']['CHARSET'] = 'windows-1255';
565 $languages['he_IL']['LOCALE'] = 'he_IL.CP1255';
566 $languages['he_IL']['DIR'] = 'rtl';
567 $languages['he']['ALIAS'] = 'he_IL';
569 /* Detect whether gettext is installed. */
571 if (function_exists('_')) {
574 if (function_exists('bindtextdomain')) {
577 if (function_exists('textdomain')) {
581 /* If gettext is fully loaded, cool */
582 if ($gettext_flags == 7) {
585 /* If we can fake gettext, try that */
586 elseif ($gettext_flags == 0) {
588 include_once(SM_PATH
. 'functions/gettext.php');
590 /* Uh-ho. A weird install */
591 if (! $gettext_flags & 1) {
596 if (! $gettext_flags & 2) {
597 function bindtextdomain() {
601 if (! $gettext_flags & 4) {
602 function textdomain() {
610 * Japanese charset extra function
612 * Action performed by function is defined by first argument.
613 * Default return value is defined by second argument.
614 * Use of third argument depends on action.
616 * @param string action performed by this function.
618 * decode - convert returned string to euc-jp. third argument unused
619 * encode - convert returned string to jis. third argument unused
620 * strimwidth - third argument=$width. trims string to $width symbols.
621 * encodeheader - create base64 encoded header in iso-2022-jp. third argument unused
622 * decodeheader - return human readable string from mime header. string is returned in euc-jp. third argument unused
623 * downloadfilename - third argument $useragent. Arguments provide browser info. Returns shift-jis or euc-jp encoded file name
624 * wordwrap - third argument=$wrap. wraps text at $wrap symbols
625 * utf7-imap_encode - returns string converted from euc-jp to utf7-imap. third argument unused
626 * utf7-imap_decode - returns string converted from utf7-imap to euc-jp. third argument unused
627 * @param string default return value
629 function japanese_charset_xtra() {
630 $ret = func_get_arg(1); /* default return value */
631 if (function_exists('mb_detect_encoding')) {
632 switch (func_get_arg(0)) { /* action */
634 $detect_encoding = @mb_detect_encoding
($ret);
635 if ($detect_encoding == 'JIS' ||
636 $detect_encoding == 'EUC-JP' ||
637 $detect_encoding == 'SJIS' ||
638 $detect_encoding == 'UTF-8') {
640 $ret = mb_convert_kana(mb_convert_encoding($ret, 'EUC-JP', 'AUTO'), "KV");
644 $detect_encoding = @mb_detect_encoding
($ret);
645 if ($detect_encoding == 'JIS' ||
646 $detect_encoding == 'EUC-JP' ||
647 $detect_encoding == 'SJIS' ||
648 $detect_encoding == 'UTF-8') {
650 $ret = mb_convert_encoding(mb_convert_kana($ret, "KV"), 'JIS', 'AUTO');
654 $width = func_get_arg(2);
655 $ret = mb_strimwidth($ret, 0, $width, '...');
659 if (strlen($ret) > 0) {
660 $tmpstr = mb_substr($ret, 0, 1);
661 $prevcsize = strlen($tmpstr);
662 for ($i = 1; $i < mb_strlen($ret); $i++
) {
663 $tmp = mb_substr($ret, $i, 1);
664 if (strlen($tmp) == $prevcsize) {
667 if ($prevcsize == 1) {
670 $result .= str_replace(' ', '',
671 mb_encode_mimeheader($tmpstr,'iso-2022-jp','B',''));
674 $prevcsize = strlen($tmp);
677 if (strlen($tmpstr)) {
678 if (strlen(mb_substr($tmpstr, 0, 1)) == 1)
681 $result .= str_replace(' ', '',
682 mb_encode_mimeheader($tmpstr,'iso-2022-jp','B',''));
688 $ret = str_replace("\t", "", $ret);
689 if (eregi('=\\?([^?]+)\\?(q|b)\\?([^?]+)\\?=', $ret))
690 $ret = @mb_decode_mimeheader
($ret);
691 $ret = @mb_convert_encoding
($ret, 'EUC-JP', 'AUTO');
693 case 'downloadfilename':
694 $useragent = func_get_arg(2);
695 if (strstr($useragent, 'Windows') !== false ||
696 strstr($useragent, 'Mac_') !== false) {
697 $ret = mb_convert_encoding($ret, 'SJIS', 'AUTO');
699 $ret = mb_convert_encoding($ret, 'EUC-JP', 'AUTO');
703 $no_begin = "\x21\x25\x29\x2c\x2e\x3a\x3b\x3f\x5d\x7d\xa1\xf1\xa1\xeb\xa1" .
704 "\xc7\xa1\xc9\xa2\xf3\xa1\xec\xa1\xed\xa1\xee\xa1\xa2\xa1\xa3\xa1\xb9" .
705 "\xa1\xd3\xa1\xd5\xa1\xd7\xa1\xd9\xa1\xdb\xa1\xcd\xa4\xa1\xa4\xa3\xa4" .
706 "\xa5\xa4\xa7\xa4\xa9\xa4\xc3\xa4\xe3\xa4\xe5\xa4\xe7\xa4\xee\xa1\xab" .
707 "\xa1\xac\xa1\xb5\xa1\xb6\xa5\xa1\xa5\xa3\xa5\xa5\xa5\xa7\xa5\xa9\xa5" .
708 "\xc3\xa5\xe3\xa5\xe5\xa5\xe7\xa5\xee\xa5\xf5\xa5\xf6\xa1\xa6\xa1\xbc" .
709 "\xa1\xb3\xa1\xb4\xa1\xaa\xa1\xf3\xa1\xcb\xa1\xa4\xa1\xa5\xa1\xa7\xa1" .
710 "\xa8\xa1\xa9\xa1\xcf\xa1\xd1";
711 $no_end = "\x5c\x24\x28\x5b\x7b\xa1\xf2\x5c\xa1\xc6\xa1\xc8\xa1\xd2\xa1" .
712 "\xd4\xa1\xd6\xa1\xd8\xa1\xda\xa1\xcc\xa1\xf0\xa1\xca\xa1\xce\xa1\xd0\xa1\xef";
713 $wrap = func_get_arg(2);
715 if (strlen($ret) >= $wrap &&
716 substr($ret, 0, 1) != '>' &&
717 strpos($ret, 'http://') === FALSE &&
718 strpos($ret, 'https://') === FALSE &&
719 strpos($ret, 'ftp://') === FALSE) {
721 $ret = mb_convert_kana($ret, "KV");
726 while ($ptr < strlen($ret) - 1) {
727 $l = mb_strcut($ret, $ptr, $wrap);
731 $l = mb_strcut($ret, $ptr, 2);
732 while (strlen($l) != 0 && mb_strpos($no_begin, $l) !== FALSE ) {
735 $l = mb_strcut($ret, $ptr, 1);
738 if ($ptr < strlen($ret) - 1)
744 case 'utf7-imap_encode':
745 $ret = mb_convert_encoding($ret, 'UTF7-IMAP', 'EUC-JP');
747 case 'utf7-imap_decode':
748 $ret = mb_convert_encoding($ret, 'EUC-JP', 'UTF7-IMAP');
757 * Korean charset extra functions
759 * Action performed by function is defined by first argument.
760 * Default return value is defined by second argument.
762 * @param string action performed by this function.
764 * downloadfilename - Hangul(Korean Character) Attached File Name Fix.
765 * @param string default return value
767 function korean_charset_xtra() {
769 $ret = func_get_arg(1); /* default return value */
770 if (func_get_arg(0) == 'downloadfilename') { /* action */
771 $ret = str_replace("\x0D\x0A", '', $ret); /* Hanmail's CR/LF Clear */
772 for ($i=0;$i<strlen($ret);$i++
) {
773 if ($ret[$i] >= "\xA1" && $ret[$i] <= "\xFE") { /* 0xA1 - 0XFE are Valid */
776 } else if (($ret[$i] >= 'a' && $ret[$i] <= 'z') ||
/* From Original ereg_replace in download.php */
777 ($ret[$i] >= 'A' && $ret[$i] <= 'Z') ||
778 ($ret[$i] == '.') ||
($ret[$i] == '-')) {
790 * Replaces non-braking spaces inserted by some browsers with regular space
792 * This function can be used to replace non-braking space symbols
793 * that are inserted in forms by some browsers instead of normal
796 * @param string $string Text that needs to be cleaned
797 * @param string $charset Charset used in text
798 * @return string Cleaned text
800 function cleanup_nbsp($string,$charset) {
802 // reduce number of case statements
803 if (stristr('iso-8859-',substr($charset,0,9))){
804 $output_charset="iso-8859-x";
806 if (stristr('windows-125',substr($charset,0,11))){
807 $output_charset="cp125x";
809 if (stristr('koi8',substr($charset,0,4))){
810 $output_charset="koi8-x";
812 if (! isset($output_charset)){
813 $output_charset=strtolower($charset);
816 // where is non-braking space symbol
817 switch($output_charset):
830 // don't change string if charset is unmatched
834 // return space instead of non-braking space.
835 return str_replace($nbsp,' ',$string);
839 * Function informs if it is safe to convert given charset to the one that is used by user.
841 * It is safe to use conversion only if user uses utf-8 encoding and when
842 * converted charset is similar to the one that is used by user.
844 * @param string $input_charset Charset of text that needs to be converted
845 * @return bool is it possible to convert to user's charset
847 function is_conversion_safe($input_charset) {
848 global $languages, $sm_notAlias, $default_charset;
850 // convert to lower case
851 $input_charset = strtolower($input_charset);
853 // Is user's locale Unicode based ?
854 if ( $default_charset == "utf-8" ) {
858 // Charsets that are similar
859 switch ($default_charset):
861 if ( $input_charset == "iso-8859-5" ||
862 $input_charset == "koi8-r" ||
863 $input_charset == "koi8-u" ) {
869 if ( $input_charset == "iso-8859-13" ||
870 $input_charset == "iso-8859-4" ) {
876 if ( $input_charset == "iso-8859-13" ||
877 $input_charset == "windows-1257" ) {
883 if ( $input_charset == "windows-1251" ||
884 $input_charset == "koi8-r" ||
885 $input_charset == "koi8-u" ) {
891 if ( $input_charset == "iso-8859-4" ||
892 $input_charset == "windows-1257" ) {
898 if ( $input_charset == "windows-1251" ||
899 $input_charset == "iso-8859-5" ||
900 $input_charset == "koi8-u" ) {
906 if ( $input_charset == "windows-1251" ||
907 $input_charset == "iso-8859-5" ||
908 $input_charset == "koi8-r" ) {