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