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