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