Adding British support
[squirrelmail.git] / functions / i18n.php
1 <?php
2
3 /**
4 * i18n.php
5 *
6 * Copyright (c) 1999-2003 The SquirrelMail Project Team
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 *
15 * $Id$
16 * @package squirrelmail
17 */
18
19 /** Everything uses global.php... */
20 require_once(SM_PATH . 'functions/global.php');
21
22 /**
23 * Converts string from given charset to charset, that can be displayed by user translation.
24 *
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.
29 *
30 * sanitizing of html tags is also done by this function.
31 *
32 * @param string $charset
33 * @param string $string Text to be decoded
34 * @return string decoded string
35 */
36 function charset_decode ($charset, $string) {
37 global $languages, $squirrelmail_language, $default_charset;
38 global $use_php_recode, $use_php_iconv, $agresive_decoding;
39
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);
43 }
44
45 $charset = strtolower($charset);
46
47 set_my_charset();
48
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; }
54
55 // Don't do conversion if charset is the same.
56 if ( $charset == strtolower($default_charset) )
57 return htmlspecialchars($string);
58
59 // catch iso-8859-8-i thing
60 if ( $charset == "iso-8859-8-i" )
61 $charset = "iso-8859-8";
62
63 /*
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.
67 */
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);
74 } else {
75 $string = recode_string($charset . "..html",$string);
76 // recode does not convert single quote, htmlspecialchars does.
77 $string = str_replace("'", '&#039;', $string);
78 return $string;
79 }
80 }
81
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);
86 }
87
88 // If we don't use recode and iconv, we'll do it old way.
89
90 /* All HTML special characters are 7 bit and can be replaced first */
91
92 $string = htmlspecialchars ($string);
93
94 /* controls cpu and memory intensive decoding cycles */
95 if (! isset($agresive_decoding) || $agresive_decoding=="" ) {
96 $agresive_decoding=false; }
97
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);
103 } else {
104 $ret = $string;
105 }
106 return( $ret );
107 }
108
109 /**
110 * Makes charset name suitable for decoding cycles
111 *
112 * @param string $charset Name of charset
113 * @return string $charset Adjusted name of charset
114 */
115 function fixcharset($charset) {
116 // minus removed from function names
117 $charset=str_replace('-','_',$charset);
118
119 // windows-125x and cp125x charsets
120 $charset=str_replace('windows_','cp',$charset);
121
122 // ibm > cp
123 $charset=str_replace('ibm','cp',$charset);
124
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);
128
129 return $charset;
130 }
131
132 /**
133 * 8bit cleanup functions.
134 *
135 * Replaces all 8 bit characters from ISO-8859 character sets with '?'
136 * Legacy function used for unsupported ISO-8859 charsets
137 *
138 * @param string $string string that has to be cleaned
139 * @return string cleaned string
140 */
141 function charset_decode_iso_8859_default ($string) {
142 return (strtr($string, "\240\241\242\243\244\245\246\247".
143 "\250\251\252\253\254\255\256\257".
144 "\260\261\262\263\264\265\266\267".
145 "\270\271\272\273\274\275\276\277".
146 "\300\301\302\303\304\305\306\307".
147 "\310\311\312\313\314\315\316\317".
148 "\320\321\322\323\324\325\326\327".
149 "\330\331\332\333\334\335\336\337".
150 "\340\341\342\343\344\345\346\347".
151 "\350\351\352\353\354\355\356\357".
152 "\360\361\362\363\364\365\366\367".
153 "\370\371\372\373\374\375\376\377",
154 "????????????????????????????????????????".
155 "????????????????????????????????????????".
156 "????????????????????????????????????????".
157 "????????"));
158
159 }
160
161 /**
162 * ns_4551_1 decoding function
163 *
164 * This is the same as ISO-646-NO and is used by some
165 * Microsoft programs when sending Norwegian characters
166 *
167 * @param string $string
168 * @return string
169 */
170 function charset_decode_ns_4551_1 ($string) {
171 /*
172 * These characters are:
173 * Latin capital letter AE
174 * Latin capital letter O with stroke
175 * Latin capital letter A with ring above
176 * and the same as small letters
177 */
178 return strtr ($string, "[\\]{|}", "ÆØÅæøå");
179 }
180
181
182 /**
183 * Set up the language to be output
184 * if $do_search is true, then scan the browser information
185 * for a possible language that we know
186 *
187 * Function sets system locale environment (LC_ALL, LANG, LANGUAGE),
188 * gettext translation bindings and html header information.
189 *
190 * Function returns error codes, if there is some fatal error.
191 * 0 = no error,
192 * 1 = mbstring support is not present,
193 * 2 = mbstring support is not present, user's translation reverted to en_US.
194 *
195 * @param string $sm_language translation used by user's interface
196 * @param bool $do_search use browser's preferred language detection functions. Defaults to false.
197 * @param bool $default set $sm_language to $squirrelmail_default_language if language detection fails or language is not set. Defaults to false.
198 * @return int function execution error codes.
199 */
200 function set_up_language($sm_language, $do_search = false, $default = false) {
201
202 static $SetupAlready = 0;
203 global $use_gettext, $languages,
204 $squirrelmail_language, $squirrelmail_default_language,
205 $sm_notAlias, $username, $data_dir;
206
207 if ($SetupAlready) {
208 return;
209 }
210
211 $SetupAlready = TRUE;
212 sqgetGlobalVar('HTTP_ACCEPT_LANGUAGE', $accept_lang, SQ_SERVER);
213
214 if ($do_search && ! $sm_language && isset($accept_lang)) {
215 $sm_language = substr($accept_lang, 0, 2);
216 }
217
218 if ((!$sm_language||$default) && isset($squirrelmail_default_language)) {
219 $squirrelmail_language = $squirrelmail_default_language;
220 $sm_language = $squirrelmail_default_language;
221 }
222 $sm_notAlias = $sm_language;
223
224 // Catching removed translation
225 // System reverts to English translation if user prefs contain translation
226 // that is not available in $languages array (admin removed directory
227 // with that translation)
228 if (!isset($languages[$sm_notAlias])) {
229 $sm_notAlias="en_US";
230 }
231
232 while (isset($languages[$sm_notAlias]['ALIAS'])) {
233 $sm_notAlias = $languages[$sm_notAlias]['ALIAS'];
234 }
235
236 if ( isset($sm_language) &&
237 $use_gettext &&
238 $sm_language != '' &&
239 isset($languages[$sm_notAlias]['CHARSET']) ) {
240 bindtextdomain( 'squirrelmail', SM_PATH . 'locale/' );
241 textdomain( 'squirrelmail' );
242 if (function_exists('bind_textdomain_codeset')) {
243 if ($sm_notAlias == 'ja_JP') {
244 bind_textdomain_codeset ("squirrelmail", 'EUC-JP');
245 } else {
246 bind_textdomain_codeset ("squirrelmail", $languages[$sm_notAlias]['CHARSET'] );
247 }
248 }
249 if (isset($languages[$sm_notAlias]['LOCALE'])){
250 $longlocale=$languages[$sm_notAlias]['LOCALE'];
251 } else {
252 $longlocale=$sm_notAlias;
253 }
254 if ( !ini_get('safe_mode') &&
255 getenv( 'LC_ALL' ) != $longlocale ) {
256 putenv( "LC_ALL=$longlocale" );
257 putenv( "LANG=$longlocale" );
258 putenv( "LANGUAGE=$longlocale" );
259 }
260 setlocale(LC_ALL, $longlocale);
261
262 // Set text direction/alignment variables
263 if (isset($languages[$sm_notAlias]['DIR']) &&
264 $languages[$sm_notAlias]['DIR'] == 'rtl') {
265 $text_direction='rtl';
266 $left_align='right';
267 $right_align='left';
268 } else {
269 $text_direction='ltr';
270 $left_align='left';
271 $right_align='right';
272 }
273
274 $squirrelmail_language = $sm_notAlias;
275 if ($squirrelmail_language == 'ja_JP') {
276 header ('Content-Type: text/html; charset=EUC-JP');
277 if (!function_exists('mb_internal_encoding')) {
278 // Error messages can't be displayed here
279 $error = 1;
280 // Revert to English if possible.
281 if (function_exists('setPref') && $username!='' && $data_dir!="") {
282 setPref($data_dir, $username, 'language', "en_US");
283 $error = 2;
284 }
285 // stop further execution in order not to get php errors on mb_internal_encoding().
286 return $error;
287 }
288 if (function_exists('mb_language')) {
289 mb_language('Japanese');
290 }
291 mb_internal_encoding('EUC-JP');
292 mb_http_output('pass');
293 } else {
294 header( 'Content-Type: text/html; charset=' . $languages[$sm_notAlias]['CHARSET'] );
295 }
296 }
297 return 0;
298 }
299
300 /**
301 * Sets default_charset variable according to the one that is used by user's translations.
302 *
303 * Function changes global $default_charset variable in order to be sure, that it
304 * contains charset used by user's translation. Sanity of $squirrelmail_default_language
305 * and $default_charset combination provided in SquirrelMail config is also tested.
306 *
307 * There can be a $default_charset setting in the
308 * config.php file, but the user may have a different language
309 * selected for a user interface. This function checks the
310 * language selected by the user and tags the outgoing messages
311 * with the appropriate charset corresponding to the language
312 * selection. This is "more right" (tm), than just stamping the
313 * message blindly with the system-wide $default_charset.
314 */
315 function set_my_charset(){
316 global $data_dir, $username, $default_charset, $languages, $squirrelmail_default_language;
317
318 $my_language = getPref($data_dir, $username, 'language');
319 if (!$my_language) {
320 $my_language = $squirrelmail_default_language ;
321 }
322 // Catch removed translation
323 if (!isset($languages[$my_language])) {
324 $my_language="en_US";
325 }
326 while (isset($languages[$my_language]['ALIAS'])) {
327 $my_language = $languages[$my_language]['ALIAS'];
328 }
329 $my_charset = $languages[$my_language]['CHARSET'];
330 if ($my_charset) {
331 $default_charset = $my_charset;
332 }
333 }
334
335 /* ------------------------------ main --------------------------- */
336
337 global $squirrelmail_language, $languages, $use_gettext;
338
339 if (! isset($squirrelmail_language)) {
340 $squirrelmail_language = '';
341 }
342
343 /**
344 * Array specifies the available translations.
345 *
346 * Structure of array:
347 * $languages['language']['variable'] = 'value'
348 *
349 * Possible 'variable' names:
350 * NAME - Translation name in English
351 * CHARSET - Encoding used by translation
352 * ALIAS - used when 'language' is only short name and 'value' should provide long language name
353 * ALTNAME - Native translation name. Any 8bit symbols must be html encoded.
354 * LOCALE - Full locale name (in xx_XX.charset format)
355 * DIR - Text direction. Used to define Right-to-Left languages. Possible values 'rtl' or 'ltr'. If undefined - defaults to 'ltr'
356 * XTRA_CODE - translation uses special functions. 'value' provides name of that extra function
357 *
358 * Each 'language' definition requires NAME+CHARSET or ALIAS variables.
359 *
360 * @name $languages
361 * @global $languages
362 */
363 $languages['bg_BG']['NAME'] = 'Bulgarian';
364 $languages['bg_BG']['ALTNAME'] = '&#1041;&#1098;&#1083;&#1075;&#1072;&#1088;&#1089;&#1082;&#1080;';
365 $languages['bg_BG']['CHARSET'] = 'windows-1251';
366 $languages['bg']['ALIAS'] = 'bg_BG';
367
368 $languages['ca_ES']['NAME'] = 'Catalan';
369 $languages['ca_ES']['CHARSET'] = 'iso-8859-1';
370 $languages['ca']['ALIAS'] = 'ca_ES';
371
372 $languages['cs_CZ']['NAME'] = 'Czech';
373 $languages['cs_CZ']['ALTNAME'] = '&#268;e&scaron;tina';
374 $languages['cs_CZ']['CHARSET'] = 'iso-8859-2';
375 $languages['cs']['ALIAS'] = 'cs_CZ';
376
377 $languages['cy_GB']['NAME'] = 'Welsh';
378 $languages['cy_GB']['ALTNAME'] = 'Cymraeg';
379 $languages['cy_GB']['CHARSET'] = 'iso-8859-1';
380 $languages['cy']['ALIAS'] = 'cy_GB';
381
382 // Danish locale is da_DK.
383 $languages['da_DK']['NAME'] = 'Danish';
384 $languages['da_DK']['ALTNAME'] = 'Dansk';
385 $languages['da_DK']['CHARSET'] = 'iso-8859-1';
386 $languages['da']['ALIAS'] = 'da_DK';
387
388 $languages['de_DE']['NAME'] = 'German';
389 $languages['de_DE']['ALTNAME'] = 'Deutsch';
390 $languages['de_DE']['CHARSET'] = 'iso-8859-1';
391 $languages['de']['ALIAS'] = 'de_DE';
392
393 $languages['el_GR']['NAME'] = 'Greek';
394 $languages['el_GR']['ALTNAME'] = '&Epsilon;&lambda;&lambda;&eta;&nu;&iota;&kappa;&#940;';
395 $languages['el_GR']['CHARSET'] = 'iso-8859-7';
396 $languages['el']['ALIAS'] = 'el_GR';
397
398 $languages['en_GB']['NAME'] = 'British';
399 $languages['en_GB']['CHARSET'] = 'iso-8859-15';
400 $languages['en_GB']['LOCALE'] = 'en_GB.ISO-8859-15';
401
402 $languages['en_US']['NAME'] = 'English';
403 $languages['en_US']['CHARSET'] = 'iso-8859-1';
404 $languages['en']['ALIAS'] = 'en_US';
405
406 $languages['es_ES']['NAME'] = 'Spanish';
407 $languages['es_ES']['ALTNAME'] = 'Espa&ntilde;ol';
408 $languages['es_ES']['CHARSET'] = 'iso-8859-1';
409 $languages['es']['ALIAS'] = 'es_ES';
410
411 $languages['et_EE']['NAME'] = 'Estonian';
412 $languages['et_EE']['CHARSET'] = 'iso-8859-15';
413 $languages['et']['ALIAS'] = 'et_EE';
414
415 $languages['fo_FO']['NAME'] = 'Faroese';
416 $languages['fo_FO']['CHARSET'] = 'iso-8859-1';
417 $languages['fo']['ALIAS'] = 'fo_FO';
418
419 $languages['fi_FI']['NAME'] = 'Finnish';
420 $languages['fi_FI']['ALTNAME'] = 'Suomi';
421 $languages['fi_FI']['CHARSET'] = 'iso-8859-1';
422 $languages['fi']['ALIAS'] = 'fi_FI';
423
424 $languages['fr_FR']['NAME'] = 'French';
425 $languages['fr_FR']['ALTNAME'] = 'Fran&#231;ais';
426 $languages['fr_FR']['CHARSET'] = 'iso-8859-1';
427 $languages['fr']['ALIAS'] = 'fr_FR';
428
429 $languages['hr_HR']['NAME'] = 'Croatian';
430 $languages['hr_HR']['CHARSET'] = 'iso-8859-2';
431 $languages['hr']['ALIAS'] = 'hr_HR';
432
433 $languages['hu_HU']['NAME'] = 'Hungarian';
434 $languages['hu_HU']['ALTNAME'] = 'Magyar';
435 $languages['hu_HU']['CHARSET'] = 'iso-8859-2';
436 $languages['hu']['ALIAS'] = 'hu_HU';
437
438 $languages['id_ID']['NAME'] = 'Indonesian';
439 $languages['id_ID']['ALTNAME'] = 'Bahasa Indonesia';
440 $languages['id_ID']['CHARSET'] = 'iso-8859-1';
441 $languages['id']['ALIAS'] = 'id_ID';
442
443 $languages['is_IS']['NAME'] = 'Icelandic';
444 $languages['is_IS']['ALTNAME'] = '&Iacute;slenska';
445 $languages['is_IS']['CHARSET'] = 'iso-8859-1';
446 $languages['is']['ALIAS'] = 'is_IS';
447
448 $languages['it_IT']['NAME'] = 'Italian';
449 $languages['it_IT']['CHARSET'] = 'iso-8859-1';
450 $languages['it']['ALIAS'] = 'it_IT';
451
452 $languages['ja_JP']['NAME'] = 'Japanese';
453 $languages['ja_JP']['ALTNAME'] = '&#26085;&#26412;&#35486;';
454 $languages['ja_JP']['CHARSET'] = 'iso-2022-jp';
455 $languages['ja_JP']['LOCALE'] = 'ja_JP.EUC-JP';
456 $languages['ja_JP']['XTRA_CODE'] = 'japanese_charset_xtra';
457 $languages['ja']['ALIAS'] = 'ja_JP';
458
459 $languages['ko_KR']['NAME'] = 'Korean';
460 $languages['ko_KR']['CHARSET'] = 'euc-KR';
461 $languages['ko_KR']['XTRA_CODE'] = 'korean_charset_xtra';
462 $languages['ko']['ALIAS'] = 'ko_KR';
463
464 $languages['lt_LT']['NAME'] = 'Lithuanian';
465 $languages['lt_LT']['ALTNAME'] = 'Lietuvi&#371;';
466 $languages['lt_LT']['CHARSET'] = 'utf-8';
467 $languages['lt_LT']['LOCALE'] = 'lt_LT.UTF-8';
468 $languages['lt']['ALIAS'] = 'lt_LT';
469
470 $languages['nl_NL']['NAME'] = 'Dutch';
471 $languages['nl_NL']['ALTNAME'] = 'Nederlands';
472 $languages['nl_NL']['CHARSET'] = 'iso-8859-1';
473 $languages['nl']['ALIAS'] = 'nl_NL';
474
475 $languages['ms_MY']['NAME'] = 'Malay';
476 $languages['ms_MY']['ALTNAME'] = 'Bahasa Melayu';
477 $languages['ms_MY']['CHARSET'] = 'iso-8859-1';
478 $languages['my']['ALIAS'] = 'ms_MY';
479
480 $languages['no_NO']['NAME'] = 'Norwegian (Bokm&aring;l)';
481 $languages['no_NO']['ALTNAME'] = 'Norsk (Bokm&aring;l)';
482 $languages['no_NO']['CHARSET'] = 'iso-8859-1';
483 $languages['no']['ALIAS'] = 'no_NO';
484
485 $languages['nn_NO']['NAME'] = 'Norwegian (Nynorsk)';
486 $languages['nn_NO']['ALTNAME'] = 'Norsk (Nynorsk)';
487 $languages['nn_NO']['CHARSET'] = 'iso-8859-1';
488
489 $languages['pl_PL']['NAME'] = 'Polish';
490 $languages['pl_PL']['ALTNAME'] = 'Polski';
491 $languages['pl_PL']['CHARSET'] = 'iso-8859-2';
492 $languages['pl']['ALIAS'] = 'pl_PL';
493
494 $languages['pt_PT']['NAME'] = 'Portuguese (Portugal)';
495 $languages['pt_PT']['CHARSET'] = 'iso-8859-1';
496 $languages['pt']['ALIAS'] = 'pt_PT';
497
498 $languages['pt_BR']['NAME'] = 'Portuguese (Brazil)';
499 $languages['pt_BR']['ALTNAME'] = 'Portugu&ecirc;s do Brasil';
500 $languages['pt_BR']['CHARSET'] = 'iso-8859-1';
501
502 $languages['ro_RO']['NAME'] = 'Romanian';
503 $languages['ro_RO']['ALTNAME'] = 'Rom&acirc;n&#259;';
504 $languages['ro_RO']['CHARSET'] = 'iso-8859-2';
505 $languages['ro']['ALIAS'] = 'ro_RO';
506
507 $languages['ru_RU']['NAME'] = 'Russian';
508 $languages['ru_RU']['ALTNAME'] = '&#1056;&#1091;&#1089;&#1089;&#1082;&#1080;&#1081;';
509 $languages['ru_RU']['CHARSET'] = 'utf-8';
510 $languages['ru_RU']['LOCALE'] = 'ru_RU.UTF-8';
511 $languages['ru']['ALIAS'] = 'ru_RU';
512
513 $languages['sk_SK']['NAME'] = 'Slovak';
514 $languages['sk_SK']['CHARSET'] = 'iso-8859-2';
515 $languages['sk']['ALIAS'] = 'sk_SK';
516
517 $languages['sl_SI']['NAME'] = 'Slovenian';
518 $languages['sl_SI']['ALTNAME'] = 'Sloven&scaron;&#269;ina';
519 $languages['sl_SI']['CHARSET'] = 'iso-8859-2';
520 $languages['sl']['ALIAS'] = 'sl_SI';
521
522 $languages['sr_YU']['NAME'] = 'Serbian';
523 $languages['sr_YU']['ALTNAME'] = 'Srpski';
524 $languages['sr_YU']['CHARSET'] = 'iso-8859-2';
525 $languages['sr']['ALIAS'] = 'sr_YU';
526
527 $languages['sv_SE']['NAME'] = 'Swedish';
528 $languages['sv_SE']['ALTNAME'] = 'Svenska';
529 $languages['sv_SE']['CHARSET'] = 'iso-8859-1';
530 $languages['sv']['ALIAS'] = 'sv_SE';
531
532 $languages['th_TH']['NAME'] = 'Thai';
533 $languages['th_TH']['CHARSET'] = 'tis-620';
534 $languages['th']['ALIAS'] = 'th_TH';
535
536 $languages['tr_TR']['NAME'] = 'Turkish';
537 $languages['tr_TR']['CHARSET'] = 'iso-8859-9';
538 $languages['tr']['ALIAS'] = 'tr_TR';
539
540 $languages['zh_TW']['NAME'] = 'Chinese Trad';
541 $languages['zh_TW']['CHARSET'] = 'big5';
542 $languages['tw']['ALIAS'] = 'zh_TW';
543
544 $languages['zh_CN']['NAME'] = 'Chinese Simp';
545 $languages['zh_CN']['CHARSET'] = 'gb2312';
546 $languages['cn']['ALIAS'] = 'zh_CN';
547
548 /*
549 $languages['uk_UA']['NAME'] = 'Ukrainian';
550 $languages['uk_UA']['CHARSET'] = 'koi8-u';
551 $languages['uk']['ALIAS'] = 'uk_UA';
552 */
553 /*
554 if ( file_exists( SM_PATH . 'locale/vi_VN') ) {
555 $languages['vi_VN']['NAME'] = 'Vietnamese';
556 $languages['vi_VN']['CHARSET'] = 'utf-8';
557 $languages['vi']['ALIAS'] = 'vi_VN';
558 }
559 */
560
561 // Right to left languages
562 $languages['ar']['NAME'] = 'Arabic';
563 $languages['ar']['CHARSET'] = 'windows-1256';
564 $languages['ar']['DIR'] = 'rtl';
565
566 $languages['he_IL']['NAME'] = 'Hebrew';
567 $languages['he_IL']['CHARSET'] = 'windows-1255';
568 $languages['he_IL']['DIR'] = 'rtl';
569 $languages['he']['ALIAS'] = 'he_IL';
570
571 /* Detect whether gettext is installed. */
572 $gettext_flags = 0;
573 if (function_exists('_')) {
574 $gettext_flags += 1;
575 }
576 if (function_exists('bindtextdomain')) {
577 $gettext_flags += 2;
578 }
579 if (function_exists('textdomain')) {
580 $gettext_flags += 4;
581 }
582
583 /* If gettext is fully loaded, cool */
584 if ($gettext_flags == 7) {
585 $use_gettext = true;
586 }
587 /* If we can fake gettext, try that */
588 elseif ($gettext_flags == 0) {
589 $use_gettext = true;
590 include_once(SM_PATH . 'functions/gettext.php');
591 } else {
592 /* Uh-ho. A weird install */
593 if (! $gettext_flags & 1) {
594 function _($str) {
595 return $str;
596 }
597 }
598 if (! $gettext_flags & 2) {
599 function bindtextdomain() {
600 return;
601 }
602 }
603 if (! $gettext_flags & 4) {
604 function textdomain() {
605 return;
606 }
607 }
608 }
609
610
611 /**
612 * Japanese charset extra function
613 *
614 * Action performed by function is defined by first argument.
615 * Default return value is defined by second argument.
616 * Use of third argument depends on action.
617 *
618 * @param string action performed by this function.
619 * possible values:
620 * decode - convert returned string to euc-jp. third argument unused
621 * encode - convert returned string to jis. third argument unused
622 * strimwidth - third argument=$width. trims string to $width symbols.
623 * encodeheader - create base64 encoded header in iso-2022-jp. third argument unused
624 * decodeheader - return human readable string from mime header. string is returned in euc-jp. third argument unused
625 * downloadfilename - third argument $useragent. Arguments provide browser info. Returns shift-jis or euc-jp encoded file name
626 * wordwrap - third argument=$wrap. wraps text at $wrap symbols
627 * utf7-imap_encode - returns string converted from euc-jp to utf7-imap. third argument unused
628 * utf7-imap_decode - returns string converted from utf7-imap to euc-jp. third argument unused
629 * @param string default return value
630 */
631 function japanese_charset_xtra() {
632 $ret = func_get_arg(1); /* default return value */
633 if (function_exists('mb_detect_encoding')) {
634 switch (func_get_arg(0)) { /* action */
635 case 'decode':
636 $detect_encoding = @mb_detect_encoding($ret);
637 if ($detect_encoding == 'JIS' ||
638 $detect_encoding == 'EUC-JP' ||
639 $detect_encoding == 'SJIS' ||
640 $detect_encoding == 'UTF-8') {
641
642 $ret = mb_convert_kana(mb_convert_encoding($ret, 'EUC-JP', 'AUTO'), "KV");
643 }
644 break;
645 case 'encode':
646 $detect_encoding = @mb_detect_encoding($ret);
647 if ($detect_encoding == 'JIS' ||
648 $detect_encoding == 'EUC-JP' ||
649 $detect_encoding == 'SJIS' ||
650 $detect_encoding == 'UTF-8') {
651
652 $ret = mb_convert_encoding(mb_convert_kana($ret, "KV"), 'JIS', 'AUTO');
653 }
654 break;
655 case 'strimwidth':
656 $width = func_get_arg(2);
657 $ret = mb_strimwidth($ret, 0, $width, '...');
658 break;
659 case 'encodeheader':
660 $result = '';
661 if (strlen($ret) > 0) {
662 $tmpstr = mb_substr($ret, 0, 1);
663 $prevcsize = strlen($tmpstr);
664 for ($i = 1; $i < mb_strlen($ret); $i++) {
665 $tmp = mb_substr($ret, $i, 1);
666 if (strlen($tmp) == $prevcsize) {
667 $tmpstr .= $tmp;
668 } else {
669 if ($prevcsize == 1) {
670 $result .= $tmpstr;
671 } else {
672 $result .= str_replace(' ', '',
673 mb_encode_mimeheader($tmpstr,'iso-2022-jp','B',''));
674 }
675 $tmpstr = $tmp;
676 $prevcsize = strlen($tmp);
677 }
678 }
679 if (strlen($tmpstr)) {
680 if (strlen(mb_substr($tmpstr, 0, 1)) == 1)
681 $result .= $tmpstr;
682 else
683 $result .= str_replace(' ', '',
684 mb_encode_mimeheader($tmpstr,'iso-2022-jp','B',''));
685 }
686 }
687 $ret = $result;
688 break;
689 case 'decodeheader':
690 $ret = str_replace("\t", "", $ret);
691 if (eregi('=\\?([^?]+)\\?(q|b)\\?([^?]+)\\?=', $ret))
692 $ret = @mb_decode_mimeheader($ret);
693 $ret = @mb_convert_encoding($ret, 'EUC-JP', 'AUTO');
694 break;
695 case 'downloadfilename':
696 $useragent = func_get_arg(2);
697 if (strstr($useragent, 'Windows') !== false ||
698 strstr($useragent, 'Mac_') !== false) {
699 $ret = mb_convert_encoding($ret, 'SJIS', 'AUTO');
700 } else {
701 $ret = mb_convert_encoding($ret, 'EUC-JP', 'AUTO');
702 }
703 break;
704 case 'wordwrap':
705 $no_begin = "\x21\x25\x29\x2c\x2e\x3a\x3b\x3f\x5d\x7d\xa1\xf1\xa1\xeb\xa1" .
706 "\xc7\xa1\xc9\xa2\xf3\xa1\xec\xa1\xed\xa1\xee\xa1\xa2\xa1\xa3\xa1\xb9" .
707 "\xa1\xd3\xa1\xd5\xa1\xd7\xa1\xd9\xa1\xdb\xa1\xcd\xa4\xa1\xa4\xa3\xa4" .
708 "\xa5\xa4\xa7\xa4\xa9\xa4\xc3\xa4\xe3\xa4\xe5\xa4\xe7\xa4\xee\xa1\xab" .
709 "\xa1\xac\xa1\xb5\xa1\xb6\xa5\xa1\xa5\xa3\xa5\xa5\xa5\xa7\xa5\xa9\xa5" .
710 "\xc3\xa5\xe3\xa5\xe5\xa5\xe7\xa5\xee\xa5\xf5\xa5\xf6\xa1\xa6\xa1\xbc" .
711 "\xa1\xb3\xa1\xb4\xa1\xaa\xa1\xf3\xa1\xcb\xa1\xa4\xa1\xa5\xa1\xa7\xa1" .
712 "\xa8\xa1\xa9\xa1\xcf\xa1\xd1";
713 $no_end = "\x5c\x24\x28\x5b\x7b\xa1\xf2\x5c\xa1\xc6\xa1\xc8\xa1\xd2\xa1" .
714 "\xd4\xa1\xd6\xa1\xd8\xa1\xda\xa1\xcc\xa1\xf0\xa1\xca\xa1\xce\xa1\xd0\xa1\xef";
715 $wrap = func_get_arg(2);
716
717 if (strlen($ret) >= $wrap &&
718 substr($ret, 0, 1) != '>' &&
719 strpos($ret, 'http://') === FALSE &&
720 strpos($ret, 'https://') === FALSE &&
721 strpos($ret, 'ftp://') === FALSE) {
722
723 $ret = mb_convert_kana($ret, "KV");
724
725 $line_new = '';
726 $ptr = 0;
727
728 while ($ptr < strlen($ret) - 1) {
729 $l = mb_strcut($ret, $ptr, $wrap);
730 $ptr += strlen($l);
731 $tmp = $l;
732
733 $l = mb_strcut($ret, $ptr, 2);
734 while (strlen($l) != 0 && mb_strpos($no_begin, $l) !== FALSE ) {
735 $tmp .= $l;
736 $ptr += strlen($l);
737 $l = mb_strcut($ret, $ptr, 1);
738 }
739 $line_new .= $tmp;
740 if ($ptr < strlen($ret) - 1)
741 $line_new .= "\n";
742 }
743 $ret = $line_new;
744 }
745 break;
746 case 'utf7-imap_encode':
747 $ret = mb_convert_encoding($ret, 'UTF7-IMAP', 'EUC-JP');
748 break;
749 case 'utf7-imap_decode':
750 $ret = mb_convert_encoding($ret, 'EUC-JP', 'UTF7-IMAP');
751 break;
752 }
753 }
754 return $ret;
755 }
756
757
758 /**
759 * Korean charset extra functions
760 *
761 * Action performed by function is defined by first argument.
762 * Default return value is defined by second argument.
763 *
764 * @param string action performed by this function.
765 * possible values:
766 * downloadfilename - Hangul(Korean Character) Attached File Name Fix.
767 * @param string default return value
768 */
769 function korean_charset_xtra() {
770
771 $ret = func_get_arg(1); /* default return value */
772 if (func_get_arg(0) == 'downloadfilename') { /* action */
773 $ret = str_replace("\x0D\x0A", '', $ret); /* Hanmail's CR/LF Clear */
774 for ($i=0;$i<strlen($ret);$i++) {
775 if ($ret[$i] >= "\xA1" && $ret[$i] <= "\xFE") { /* 0xA1 - 0XFE are Valid */
776 $i++;
777 continue;
778 } else if (($ret[$i] >= 'a' && $ret[$i] <= 'z') || /* From Original ereg_replace in download.php */
779 ($ret[$i] >= 'A' && $ret[$i] <= 'Z') ||
780 ($ret[$i] == '.') || ($ret[$i] == '-')) {
781 continue;
782 } else {
783 $ret[$i] = '_';
784 }
785 }
786
787 }
788 return $ret;
789 }
790
791 /**
792 * Replaces non-braking spaces inserted by some browsers with regular space
793 *
794 * This function can be used to replace non-braking space symbols
795 * that are inserted in forms by some browsers instead of normal
796 * space symbol.
797 *
798 * @param string $string Text that needs to be cleaned
799 * @param string $charset Charset used in text
800 * @return string Cleaned text
801 */
802 function cleanup_nbsp($string,$charset) {
803
804 // reduce number of case statements
805 if (stristr('iso-8859-',substr($charset,0,9))){
806 $output_charset="iso-8859-x";
807 }
808 if (stristr('windows-125',substr($charset,0,11))){
809 $output_charset="cp125x";
810 }
811 if (stristr('koi8',substr($charset,0,4))){
812 $output_charset="koi8-x";
813 }
814 if (! isset($output_charset)){
815 $output_charset=strtolower($charset);
816 }
817
818 // where is non-braking space symbol
819 switch($output_charset):
820 case "iso-8859-x":
821 case "cp125x":
822 case "iso-2022-jp":
823 $nbsp="\xA0";
824 break;
825 case "koi8-x":
826 $nbsp="\x9A";
827 break;
828 case "utf-8":
829 $nbsp="\xC2\xA0";
830 break;
831 default:
832 // don't change string if charset is unmatched
833 return $string;
834 endswitch;
835
836 // return space instead of non-braking space.
837 return str_replace($nbsp,' ',$string);
838 }
839
840 /**
841 * Function informs if it is safe to convert given charset to the one that is used by user.
842 *
843 * It is safe to use conversion only if user uses utf-8 encoding and when
844 * converted charset is similar to the one that is used by user.
845 *
846 * @param string $input_charset Charset of text that needs to be converted
847 * @return bool is it possible to convert to user's charset
848 */
849 function is_conversion_safe($input_charset) {
850 global $languages, $sm_notAlias, $default_charset;
851
852 // convert to lower case
853 $input_charset = strtolower($input_charset);
854
855 // Is user's locale Unicode based ?
856 if ( $default_charset == "utf-8" ) {
857 return true;
858 }
859
860 // Charsets that are similar
861 switch ($default_charset):
862 case "windows-1251":
863 if ( $input_charset == "iso-8859-5" ||
864 $input_charset == "koi8-r" ||
865 $input_charset == "koi8-u" ) {
866 return true;
867 } else {
868 return false;
869 }
870 case "windows-1257":
871 if ( $input_charset == "iso-8859-13" ||
872 $input_charset == "iso-8859-4" ) {
873 return true;
874 } else {
875 return false;
876 }
877 case "iso-8859-4":
878 if ( $input_charset == "iso-8859-13" ||
879 $input_charset == "windows-1257" ) {
880 return true;
881 } else {
882 return false;
883 }
884 case "iso-8859-5":
885 if ( $input_charset == "windows-1251" ||
886 $input_charset == "koi8-r" ||
887 $input_charset == "koi8-u" ) {
888 return true;
889 } else {
890 return false;
891 }
892 case "iso-8859-13":
893 if ( $input_charset == "iso-8859-4" ||
894 $input_charset == "windows-1257" ) {
895 return true;
896 } else {
897 return false;
898 }
899 case "koi8-r":
900 if ( $input_charset == "windows-1251" ||
901 $input_charset == "iso-8859-5" ||
902 $input_charset == "koi8-u" ) {
903 return true;
904 } else {
905 return false;
906 }
907 case "koi8-u":
908 if ( $input_charset == "windows-1251" ||
909 $input_charset == "iso-8859-5" ||
910 $input_charset == "koi8-r" ) {
911 return true;
912 } else {
913 return false;
914 }
915 default:
916 return false;
917 endswitch;
918 }
919 ?>