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