Added argument to set-up_language to force default language
[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 ( !ini_get('safe_mode') &&
219 getenv( 'LC_ALL' ) != $sm_notAlias ) {
220 putenv( "LC_ALL=$sm_notAlias" );
221 putenv( "LANG=$sm_notAlias" );
222 putenv( "LANGUAGE=$sm_notAlias" );
223 }
224 setlocale(LC_ALL, $sm_notAlias);
225 $squirrelmail_language = $sm_notAlias;
226 if ($squirrelmail_language == 'ja_JP' && function_exists('mb_detect_encoding') ) {
227 header ('Content-Type: text/html; charset=EUC-JP');
228 if (!function_exists('mb_internal_encoding')) {
229 echo _("You need to have php4 installed with the multibyte string function enabled (using configure option --enable-mbstring).");
230 }
231 if (function_exists('mb_language')) {
232 mb_language('Japanese');
233 }
234 mb_internal_encoding('EUC-JP');
235 mb_http_output('pass');
236 } else {
237 header( 'Content-Type: text/html; charset=' . $languages[$sm_notAlias]['CHARSET'] );
238 }
239 }
240 }
241
242 function set_my_charset(){
243
244 /*
245 * There can be a $default_charset setting in the
246 * config.php file, but the user may have a different language
247 * selected for a user interface. This function checks the
248 * language selected by the user and tags the outgoing messages
249 * with the appropriate charset corresponding to the language
250 * selection. This is "more right" (tm), than just stamping the
251 * message blindly with the system-wide $default_charset.
252 */
253 global $data_dir, $username, $default_charset, $languages, $squirrelmail_default_language;
254
255 $my_language = getPref($data_dir, $username, 'language');
256 if (!$my_language) {
257 $my_language = $squirrelmail_default_language ;
258 }
259 while (isset($languages[$my_language]['ALIAS'])) {
260 $my_language = $languages[$my_language]['ALIAS'];
261 }
262 $my_charset = $languages[$my_language]['CHARSET'];
263 if ($my_charset) {
264 $default_charset = $my_charset;
265 }
266 }
267
268 /* ------------------------------ main --------------------------- */
269
270 global $squirrelmail_language, $languages, $use_gettext;
271
272 if (! isset($squirrelmail_language)) {
273 $squirrelmail_language = '';
274 }
275
276 /* This array specifies the available languages. */
277
278 // The glibc locale is ca_ES.
279
280 $languages['ca_ES']['NAME'] = 'Catalan';
281 $languages['ca_ES']['CHARSET'] = 'iso-8859-1';
282 $languages['ca']['ALIAS'] = 'ca_ES';
283
284 $languages['cs_CZ']['NAME'] = 'Czech';
285 $languages['cs_CZ']['CHARSET'] = 'iso-8859-2';
286 $languages['cs']['ALIAS'] = 'cs_CZ';
287
288 // Danish locale is da_DK.
289
290 $languages['da_DK']['NAME'] = 'Danish';
291 $languages['da_DK']['CHARSET'] = 'iso-8859-1';
292 $languages['da']['ALIAS'] = 'da_DK';
293
294 $languages['de_DE']['NAME'] = 'Deutsch';
295 $languages['de_DE']['CHARSET'] = 'iso-8859-1';
296 $languages['de']['ALIAS'] = 'de_DE';
297
298 // There is no en_EN! There is en_US, en_BR, en_AU, and so forth,
299 // but who cares about !US, right? Right? :)
300
301 $languages['el_GR']['NAME'] = 'Greek';
302 $languages['el_GR']['CHARSET'] = 'iso-8859-7';
303 $languages['el']['ALIAS'] = 'el_GR';
304
305 $languages['en_US']['NAME'] = 'English';
306 $languages['en_US']['CHARSET'] = 'iso-8859-1';
307 $languages['en']['ALIAS'] = 'en_US';
308
309 $languages['es_ES']['NAME'] = 'Spanish';
310 $languages['es_ES']['CHARSET'] = 'iso-8859-1';
311 $languages['es']['ALIAS'] = 'es_ES';
312
313 $languages['et_EE']['NAME'] = 'Estonian';
314 $languages['et_EE']['CHARSET'] = 'iso-8859-15';
315 $languages['et']['ALIAS'] = 'et_EE';
316
317 $languages['fo_FO']['NAME'] = 'Faroese';
318 $languages['fo_FO']['CHARSET'] = 'iso-8859-1';
319 $languages['fo']['ALIAS'] = 'fo_FO';
320
321 $languages['fi_FI']['NAME'] = 'Finnish';
322 $languages['fi_FI']['CHARSET'] = 'iso-8859-1';
323 $languages['fi']['ALIAS'] = 'fi_FI';
324
325 $languages['fr_FR']['NAME'] = 'French';
326 $languages['fr_FR']['CHARSET'] = 'iso-8859-1';
327 $languages['fr']['ALIAS'] = 'fr_FR';
328
329 $languages['hr_HR']['NAME'] = 'Croatian';
330 $languages['hr_HR']['CHARSET'] = 'iso-8859-2';
331 $languages['hr']['ALIAS'] = 'hr_HR';
332
333 $languages['hu_HU']['NAME'] = 'Hungarian';
334 $languages['hu_HU']['CHARSET'] = 'iso-8859-2';
335 $languages['hu']['ALIAS'] = 'hu_HU';
336
337 $languages['id_ID']['NAME'] = 'Bahasa Indonesia';
338 $languages['id_ID']['CHARSET'] = 'iso-8859-1';
339 $languages['id']['ALIAS'] = 'id_ID';
340
341 $languages['is_IS']['NAME'] = 'Icelandic';
342 $languages['is_IS']['CHARSET'] = 'iso-8859-1';
343 $languages['is']['ALIAS'] = 'is_IS';
344
345 $languages['it_IT']['NAME'] = 'Italian';
346 $languages['it_IT']['CHARSET'] = 'iso-8859-1';
347 $languages['it']['ALIAS'] = 'it_IT';
348
349 $languages['ja_JP']['NAME'] = 'Japanese';
350 $languages['ja_JP']['CHARSET'] = 'iso-2022-jp';
351 $languages['ja_JP']['XTRA_CODE'] = 'japanese_charset_xtra';
352 $languages['ja']['ALIAS'] = 'ja_JP';
353
354 $languages['ko_KR']['NAME'] = 'Korean';
355 $languages['ko_KR']['CHARSET'] = 'euc-KR';
356 $languages['ko_KR']['XTRA_CODE'] = 'korean_charset_xtra';
357 $languages['ko']['ALIAS'] = 'ko_KR';
358
359 $languages['nl_NL']['NAME'] = 'Dutch';
360 $languages['nl_NL']['CHARSET'] = 'iso-8859-1';
361 $languages['nl']['ALIAS'] = 'nl_NL';
362
363 $languages['no_NO']['NAME'] = 'Norwegian (Bokm&aring;l)';
364 $languages['no_NO']['CHARSET'] = 'iso-8859-1';
365 $languages['no']['ALIAS'] = 'no_NO';
366 $languages['nn_NO']['NAME'] = 'Norwegian (Nynorsk)';
367 $languages['nn_NO']['CHARSET'] = 'iso-8859-1';
368
369 $languages['pl_PL']['NAME'] = 'Polish';
370 $languages['pl_PL']['CHARSET'] = 'iso-8859-2';
371 $languages['pl']['ALIAS'] = 'pl_PL';
372
373 $languages['pt_PT']['NAME'] = 'Portuguese (Portugal)';
374 $languages['pt_PT']['CHARSET'] = 'iso-8859-1';
375 $languages['pt_BR']['NAME'] = 'Portuguese (Brazil)';
376 $languages['pt_BR']['CHARSET'] = 'iso-8859-1';
377 $languages['pt']['ALIAS'] = 'pt_PT';
378
379 $languages['ru_RU']['NAME'] = 'Russian';
380 $languages['ru_RU']['CHARSET'] = 'koi8-r';
381 $languages['ru']['ALIAS'] = 'ru_RU';
382
383 $languages['sr_YU']['NAME'] = 'Serbian';
384 $languages['sr_YU']['CHARSET'] = 'iso-8859-2';
385 $languages['sr']['ALIAS'] = 'sr_YU';
386
387 $languages['sv_SE']['NAME'] = 'Swedish';
388 $languages['sv_SE']['CHARSET'] = 'iso-8859-1';
389 $languages['sv']['ALIAS'] = 'sv_SE';
390
391 $languages['tr_TR']['NAME'] = 'Turkish';
392 $languages['tr_TR']['CHARSET'] = 'iso-8859-9';
393 $languages['tr']['ALIAS'] = 'tr_TR';
394
395 $languages['zh_TW']['NAME'] = 'Chinese Trad';
396 $languages['zh_TW']['CHARSET'] = 'big5';
397 $languages['tw']['ALIAS'] = 'zh_TW';
398
399 $languages['zh_CN']['NAME'] = 'Chinese Simp';
400 $languages['zh_CN']['CHARSET'] = 'gb2312';
401 $languages['cn']['ALIAS'] = 'zh_CN';
402
403 $languages['sk_SK']['NAME'] = 'Slovak';
404 $languages['sk_SK']['CHARSET'] = 'iso-8859-2';
405 $languages['sk']['ALIAS'] = 'sk_SK';
406
407 $languages['ro_RO']['NAME'] = 'Romanian';
408 $languages['ro_RO']['CHARSET'] = 'iso-8859-2';
409 $languages['ro']['ALIAS'] = 'ro_RO';
410
411 $languages['th_TH']['NAME'] = 'Thai';
412 $languages['th_TH']['CHARSET'] = 'tis-620';
413 $languages['th']['ALIAS'] = 'th_TH';
414
415 $languages['lt_LT']['NAME'] = 'Lithuanian';
416 $languages['lt_LT']['CHARSET'] = 'windows-1257';
417 $languages['lt']['ALIAS'] = 'lt_LT';
418
419 $languages['sl_SI']['NAME'] = 'Slovenian';
420 $languages['sl_SI']['CHARSET'] = 'iso-8859-2';
421 $languages['sl']['ALIAS'] = 'sl_SI';
422
423 $languages['bg_BG']['NAME'] = 'Bulgarian';
424 $languages['bg_BG']['CHARSET'] = 'windows-1251';
425 $languages['bg']['ALIAS'] = 'bg_BG';
426
427 $languages['uk_UA']['NAME'] = 'Ukrainian';
428 $languages['uk_UA']['CHARSET'] = 'koi8-u';
429 $languages['uk']['ALIAS'] = 'uk_UA';
430
431 $languages['cy_GB']['NAME'] = 'Welsh';
432 $languages['cy_GB']['CHARSET'] = 'iso-8859-1';
433 $languages['cy']['ALIAS'] = 'cy_GB';
434
435 $languages['vi_VN']['NAME'] = 'Vietnamese';
436 $languages['vi_VN']['CHARSET'] = 'utf-8';
437 $languages['vi']['ALIAS'] = 'vi_VN';
438
439 // Right to left languages
440
441 $languages['ar']['NAME'] = 'Arabic';
442 $languages['ar']['CHARSET'] = 'windows-1256';
443 $languages['ar']['DIR'] = 'rtl';
444
445 $languages['he_IL']['NAME'] = 'Hebrew';
446 $languages['he_IL']['CHARSET'] = 'windows-1255';
447 $languages['he_IL']['DIR'] = 'rtl';
448 $languages['he']['ALIAS'] = 'he_IL';
449
450 /* Detect whether gettext is installed. */
451 $gettext_flags = 0;
452 if (function_exists('_')) {
453 $gettext_flags += 1;
454 }
455 if (function_exists('bindtextdomain')) {
456 $gettext_flags += 2;
457 }
458 if (function_exists('textdomain')) {
459 $gettext_flags += 4;
460 }
461
462 /* If gettext is fully loaded, cool */
463 if ($gettext_flags == 7) {
464 $use_gettext = true;
465 }
466 /* If we can fake gettext, try that */
467 elseif ($gettext_flags == 0) {
468 $use_gettext = true;
469 include_once(SM_PATH . 'functions/gettext.php');
470 } else {
471 /* Uh-ho. A weird install */
472 if (! $gettext_flags & 1) {
473 function _($str) {
474 return $str;
475 }
476 }
477 if (! $gettext_flags & 2) {
478 function bindtextdomain() {
479 return;
480 }
481 }
482 if (! $gettext_flags & 4) {
483 function textdomain() {
484 return;
485 }
486 }
487 }
488
489
490 /*
491 * Japanese charset extra function
492 *
493 */
494 function japanese_charset_xtra() {
495 $ret = func_get_arg(1); /* default return value */
496 if (function_exists('mb_detect_encoding')) {
497 switch (func_get_arg(0)) { /* action */
498 case 'decode':
499 $detect_encoding = @mb_detect_encoding($ret);
500 if ($detect_encoding == 'JIS' ||
501 $detect_encoding == 'EUC-JP' ||
502 $detect_encoding == 'SJIS' ||
503 $detect_encoding == 'UTF-8') {
504
505 $ret = mb_convert_kana(mb_convert_encoding($ret, 'EUC-JP', 'AUTO'), "KV");
506 }
507 break;
508 case 'encode':
509 $detect_encoding = @mb_detect_encoding($ret);
510 if ($detect_encoding == 'JIS' ||
511 $detect_encoding == 'EUC-JP' ||
512 $detect_encoding == 'SJIS' ||
513 $detect_encoding == 'UTF-8') {
514
515 $ret = mb_convert_encoding(mb_convert_kana($ret, "KV"), 'JIS', 'AUTO');
516 }
517 break;
518 case 'strimwidth':
519 $width = func_get_arg(2);
520 $ret = mb_strimwidth($ret, 0, $width, '...');
521 break;
522 case 'encodeheader':
523 $result = '';
524 if (strlen($ret) > 0) {
525 $tmpstr = mb_substr($ret, 0, 1);
526 $prevcsize = strlen($tmpstr);
527 for ($i = 1; $i < mb_strlen($ret); $i++) {
528 $tmp = mb_substr($ret, $i, 1);
529 if (strlen($tmp) == $prevcsize) {
530 $tmpstr .= $tmp;
531 } else {
532 if ($prevcsize == 1) {
533 $result .= $tmpstr;
534 } else {
535 $result .= str_replace(' ', '',
536 mb_encode_mimeheader($tmpstr,'iso-2022-jp','B',''));
537 }
538 $tmpstr = $tmp;
539 $prevcsize = strlen($tmp);
540 }
541 }
542 if (strlen($tmpstr)) {
543 if (strlen(mb_substr($tmpstr, 0, 1)) == 1)
544 $result .= $tmpstr;
545 else
546 $result .= str_replace(' ', '',
547 mb_encode_mimeheader($tmpstr,'iso-2022-jp','B',''));
548 }
549 }
550 $ret = $result;
551 break;
552 case 'decodeheader':
553 $ret = str_replace("\t", "", $ret);
554 if (eregi('=\\?([^?]+)\\?(q|b)\\?([^?]+)\\?=', $ret))
555 $ret = @mb_decode_mimeheader($ret);
556 $ret = @mb_convert_encoding($ret, 'EUC-JP', 'AUTO');
557 break;
558 case 'downloadfilename':
559 $useragent = func_get_arg(2);
560 if (strstr($useragent, 'Windows') !== false ||
561 strstr($useragent, 'Mac_') !== false) {
562 $ret = mb_convert_encoding($ret, 'SJIS', 'AUTO');
563 } else {
564 $ret = mb_convert_encoding($ret, 'EUC-JP', 'AUTO');
565 }
566 break;
567 case 'wordwrap':
568 $no_begin = "\x21\x25\x29\x2c\x2e\x3a\x3b\x3f\x5d\x7d\xa1\xf1\xa1\xeb\xa1" .
569 "\xc7\xa1\xc9\xa2\xf3\xa1\xec\xa1\xed\xa1\xee\xa1\xa2\xa1\xa3\xa1\xb9" .
570 "\xa1\xd3\xa1\xd5\xa1\xd7\xa1\xd9\xa1\xdb\xa1\xcd\xa4\xa1\xa4\xa3\xa4" .
571 "\xa5\xa4\xa7\xa4\xa9\xa4\xc3\xa4\xe3\xa4\xe5\xa4\xe7\xa4\xee\xa1\xab" .
572 "\xa1\xac\xa1\xb5\xa1\xb6\xa5\xa1\xa5\xa3\xa5\xa5\xa5\xa7\xa5\xa9\xa5" .
573 "\xc3\xa5\xe3\xa5\xe5\xa5\xe7\xa5\xee\xa5\xf5\xa5\xf6\xa1\xa6\xa1\xbc" .
574 "\xa1\xb3\xa1\xb4\xa1\xaa\xa1\xf3\xa1\xcb\xa1\xa4\xa1\xa5\xa1\xa7\xa1" .
575 "\xa8\xa1\xa9\xa1\xcf\xa1\xd1";
576 $no_end = "\x5c\x24\x28\x5b\x7b\xa1\xf2\x5c\xa1\xc6\xa1\xc8\xa1\xd2\xa1" .
577 "\xd4\xa1\xd6\xa1\xd8\xa1\xda\xa1\xcc\xa1\xf0\xa1\xca\xa1\xce\xa1\xd0\xa1\xef";
578 $wrap = func_get_arg(2);
579
580 if (strlen($ret) >= $wrap &&
581 substr($ret, 0, 1) != '>' &&
582 strpos($ret, 'http://') === FALSE &&
583 strpos($ret, 'https://') === FALSE &&
584 strpos($ret, 'ftp://') === FALSE) {
585
586 $ret = mb_convert_kana($ret, "KV");
587
588 $line_new = '';
589 $ptr = 0;
590
591 while ($ptr < strlen($ret) - 1) {
592 $l = mb_strcut($ret, $ptr, $wrap);
593 $ptr += strlen($l);
594 $tmp = $l;
595
596 $l = mb_strcut($ret, $ptr, 2);
597 while (strlen($l) != 0 && mb_strpos($no_begin, $l) !== FALSE ) {
598 $tmp .= $l;
599 $ptr += strlen($l);
600 $l = mb_strcut($ret, $ptr, 1);
601 }
602 $line_new .= $tmp;
603 if ($ptr < strlen($ret) - 1)
604 $line_new .= "\n";
605 }
606 $ret = $line_new;
607 }
608 break;
609 case 'utf7-imap_encode':
610 $ret = mb_convert_encoding($ret, 'UTF7-IMAP', 'EUC-JP');
611 break;
612 case 'utf7-imap_decode':
613 $ret = mb_convert_encoding($ret, 'EUC-JP', 'UTF7-IMAP');
614 break;
615 }
616 }
617 return $ret;
618 }
619
620
621 /*
622 * Korean charset extra function
623 * Hangul(Korean Character) Attached File Name Fix.
624 */
625 function korean_charset_xtra() {
626
627 $ret = func_get_arg(1); /* default return value */
628 if (func_get_arg(0) == 'downloadfilename') { /* action */
629 $ret = str_replace("\x0D\x0A", '', $ret); /* Hanmail's CR/LF Clear */
630 for ($i=0;$i<strlen($ret);$i++) {
631 if ($ret[$i] >= "\xA1" && $ret[$i] <= "\xFE") { /* 0xA1 - 0XFE are Valid */
632 $i++;
633 continue;
634 } else if (($ret[$i] >= 'a' && $ret[$i] <= 'z') || /* From Original ereg_replace in download.php */
635 ($ret[$i] >= 'A' && $ret[$i] <= 'Z') ||
636 ($ret[$i] == '.') || ($ret[$i] == '-')) {
637 continue;
638 } else {
639 $ret[$i] = '_';
640 }
641 }
642
643 }
644
645 return $ret;
646 }
647
648 ?>