202bcbcc |
1 | <?php |
2 | |
3 | /** |
4 | * SquirrelMail internationalization functions |
5 | * |
6 | * This file contains variuos functions that are needed to do |
7 | * internationalization of SquirrelMail. |
8 | * |
9 | * Internally the output character set is used. Other characters are |
10 | * encoded using Unicode entities according to HTML 4.0. |
11 | * |
12 | * @copyright © 1999-2006 The SquirrelMail Project Team |
13 | * @license http://opensource.org/licenses/gpl-license.php GNU Public License |
14 | * @version $Id$ |
15 | * @package squirrelmail |
16 | * @subpackage i18n |
17 | */ |
18 | |
19 | |
20 | /** |
21 | * Gettext bindtextdomain wrapper. |
22 | * |
23 | * Wrapper solves differences between php versions in order to provide |
24 | * ngettext support. Should be used if translation uses ngettext |
25 | * functions. |
26 | * @since 1.5.1 |
27 | * @param string $domain gettext domain name |
28 | * @param string $dir directory that contains all translations |
29 | * @return string path to translation directory |
30 | */ |
31 | function sq_bindtextdomain($domain,$dir) { |
32 | global $l10n, $gettext_flags, $sm_notAlias; |
33 | |
34 | if ($gettext_flags==7) { |
35 | // gettext extension without ngettext |
36 | if (substr($dir, -1) != '/') $dir .= '/'; |
37 | $mofile=$dir . $sm_notAlias . '/LC_MESSAGES/' . $domain . '.mo'; |
38 | $input = new FileReader($mofile); |
39 | $l10n[$domain] = new gettext_reader($input); |
40 | } |
41 | |
42 | $dir=bindtextdomain($domain,$dir); |
43 | |
44 | return $dir; |
45 | } |
46 | |
47 | /** |
48 | * Gettext textdomain wrapper. |
49 | * Makes sure that gettext_domain global is modified. |
50 | * @since 1.5.1 |
51 | * @param string $name gettext domain name |
52 | * @return string gettext domain name |
53 | */ |
54 | function sq_textdomain($domain) { |
55 | global $gettext_domain; |
56 | $gettext_domain=textdomain($domain); |
57 | return $gettext_domain; |
58 | } |
59 | |
60 | /** |
61 | * php setlocale function wrapper |
62 | * |
63 | * From php 4.3.0 it is possible to use arrays in order to set locale. |
64 | * php gettext extension works only when locale is set. This wrapper |
65 | * function allows to use more than one locale name. |
66 | * |
67 | * @param int $category locale category name. Use php named constants |
68 | * (LC_ALL, LC_COLLATE, LC_CTYPE, LC_MONETARY, LC_NUMERIC, LC_TIME) |
69 | * @param mixed $locale option contains array with possible locales or string with one locale |
70 | * @return string name of set locale or false, if all locales fail. |
71 | * @since 1.5.1 and 1.4.5 |
72 | * @see http://www.php.net/setlocale |
73 | */ |
74 | function sq_setlocale($category,$locale) { |
06783280 |
75 | if (is_string($locale)) { |
76 | // string with only one locale |
77 | $ret = setlocale($category,$locale); |
78 | } elseif (! check_php_version(4,3)) { |
79 | // older php version (second setlocale argument must be string) |
202bcbcc |
80 | $ret=false; |
81 | $index=0; |
82 | while ( ! $ret && $index<count($locale)) { |
83 | $ret=setlocale($category,$locale[$index]); |
84 | $index++; |
85 | } |
86 | } else { |
87 | // php 4.3.0 or better, use entire array |
88 | $ret=setlocale($category,$locale); |
89 | } |
06783280 |
90 | |
91 | /* safety checks */ |
92 | if (preg_match("/^.*\/.*\/.*\/.*\/.*\/.*$/",$ret)) { |
93 | /** |
94 | * Welcome to We-Don't-Follow-Own-Fine-Manual department |
95 | * OpenBSD 3.8, 3.9-current and maybe later versions |
96 | * return invalid response to setlocale command. |
97 | * SM bug report #1427512. |
98 | */ |
99 | $ret = false; |
100 | } |
202bcbcc |
101 | return $ret; |
102 | } |
103 | |
104 | /** |
105 | * Converts string from given charset to charset, that can be displayed by user translation. |
106 | * |
107 | * Function by default returns html encoded strings, if translation uses different encoding. |
108 | * If Japanese translation is used - function returns string converted to euc-jp |
109 | * If iconv or recode functions are enabled and translation uses utf-8 - function returns utf-8 encoded string. |
110 | * If $charset is not supported - function returns unconverted string. |
111 | * |
112 | * sanitizing of html tags is also done by this function. |
113 | * |
114 | * @param string $charset |
115 | * @param string $string Text to be decoded |
116 | * @param boolean $force_decode converts string to html without $charset!=$default_charset check. |
117 | * Argument is available since 1.5.1 and 1.4.5. |
118 | * @param boolean $save_html disables htmlspecialchars() in order to preserve |
119 | * html formating. Use with care. Available since 1.5.1 |
120 | * @return string decoded string |
121 | */ |
122 | function charset_decode ($charset, $string, $force_decode=false, $save_html=false) { |
123 | global $languages, $squirrelmail_language, $default_charset; |
124 | global $use_php_recode, $use_php_iconv, $aggressive_decoding; |
125 | |
126 | if (isset($languages[$squirrelmail_language]['XTRA_CODE']) && |
127 | function_exists($languages[$squirrelmail_language]['XTRA_CODE'] . '_decode')) { |
128 | $string = call_user_func($languages[$squirrelmail_language]['XTRA_CODE'] . '_decode', $string); |
129 | } |
130 | |
131 | $charset = strtolower($charset); |
132 | |
133 | set_my_charset(); |
134 | |
135 | // Variables that allow to use functions without function_exist() calls |
136 | if (! isset($use_php_recode) || $use_php_recode=="" ) { |
137 | $use_php_recode=false; } |
138 | if (! isset($use_php_iconv) || $use_php_iconv=="" ) { |
139 | $use_php_iconv=false; } |
140 | |
141 | // Don't do conversion if charset is the same. |
142 | if ( ! $force_decode && $charset == strtolower($default_charset) ) |
143 | return ($save_html ? $string : htmlspecialchars($string)); |
144 | |
145 | // catch iso-8859-8-i thing |
146 | if ( $charset == "iso-8859-8-i" ) |
147 | $charset = "iso-8859-8"; |
148 | |
149 | /* |
150 | * Recode converts html special characters automatically if you use |
151 | * 'charset..html' decoding. There is no documented way to put -d option |
152 | * into php recode function call. |
153 | */ |
154 | if ( $use_php_recode ) { |
155 | if ( $default_charset == "utf-8" ) { |
156 | // other charsets can be converted to utf-8 without loss. |
157 | // and output string is smaller |
158 | $string = recode_string($charset . "..utf-8",$string); |
159 | return ($save_html ? $string : htmlspecialchars($string)); |
160 | } else { |
161 | $string = recode_string($charset . "..html",$string); |
162 | // recode does not convert single quote, htmlspecialchars does. |
163 | $string = str_replace("'", ''', $string); |
164 | // undo html specialchars |
165 | if ($save_html) |
166 | $string=str_replace(array('&','"','<','>'), |
167 | array('&','"','<','>'),$string); |
168 | return $string; |
169 | } |
170 | } |
171 | |
172 | // iconv functions does not have html target and can be used only with utf-8 |
173 | if ( $use_php_iconv && $default_charset=='utf-8') { |
174 | $string = iconv($charset,$default_charset,$string); |
175 | return ($save_html ? $string : htmlspecialchars($string)); |
176 | } |
177 | |
178 | // If we don't use recode and iconv, we'll do it old way. |
179 | |
180 | /* All HTML special characters are 7 bit and can be replaced first */ |
181 | if (! $save_html) $string = htmlspecialchars ($string); |
182 | |
183 | /* controls cpu and memory intensive decoding cycles */ |
184 | if (! isset($aggressive_decoding) || $aggressive_decoding=="" ) { |
185 | $aggressive_decoding=false; } |
186 | |
187 | $decode=fixcharset($charset); |
188 | $decodefile=SM_PATH . 'functions/decode/' . $decode . '.php'; |
189 | if (file_exists($decodefile)) { |
190 | include_once($decodefile); |
191 | // send $save_html argument to decoding function. needed for iso-2022-xx decoding. |
192 | $ret = call_user_func('charset_decode_'.$decode, $string, $save_html); |
193 | } else { |
194 | $ret = $string; |
195 | } |
196 | return( $ret ); |
197 | } |
198 | |
199 | /** |
200 | * Converts html string to given charset |
201 | * @since 1.5.1 and 1.4.4 |
202 | * @param string $string |
203 | * @param string $charset |
204 | * @param boolean $htmlencode keep htmlspecialchars encoding |
205 | * @param string |
206 | */ |
207 | function charset_encode($string,$charset,$htmlencode=true) { |
208 | global $default_charset; |
209 | |
210 | $encode=fixcharset($charset); |
211 | $encodefile=SM_PATH . 'functions/encode/' . $encode . '.php'; |
212 | if (file_exists($encodefile)) { |
213 | include_once($encodefile); |
214 | $ret = call_user_func('charset_encode_'.$encode, $string); |
215 | } elseif(file_exists(SM_PATH . 'functions/encode/us_ascii.php')) { |
216 | // function replaces all 8bit html entities with question marks. |
217 | // it is used when other encoding functions are unavailable |
218 | include_once(SM_PATH . 'functions/encode/us_ascii.php'); |
219 | $ret = charset_encode_us_ascii($string); |
220 | } else { |
221 | /** |
222 | * fix for yahoo users that remove all us-ascii related things |
223 | */ |
224 | $ret = $string; |
225 | } |
226 | |
227 | /** |
228 | * Undo html special chars, some places (like compose form) have |
229 | * own sanitizing functions and don't need html symbols. |
230 | * Undo chars only after encoding in order to prevent conversion of |
231 | * html entities in plain text emails. |
232 | */ |
233 | if (! $htmlencode ) { |
234 | $ret = str_replace(array('&','>','<','"'),array('&','>','<','"'),$ret); |
235 | } |
236 | return( $ret ); |
237 | } |
238 | |
239 | /** |
240 | * Combined decoding and encoding functions |
241 | * |
242 | * If conversion is done to charset different that utf-8, unsupported symbols |
243 | * will be replaced with question marks. |
244 | * @since 1.5.1 and 1.4.4 |
245 | * @param string $in_charset initial charset |
246 | * @param string $string string that has to be converted |
247 | * @param string $out_charset final charset |
248 | * @param boolean $htmlencode keep htmlspecialchars encoding |
249 | * @return string converted string |
250 | */ |
251 | function charset_convert($in_charset,$string,$out_charset,$htmlencode=true) { |
252 | $string=charset_decode($in_charset,$string,true); |
253 | $string=sqi18n_convert_entities($string); |
254 | $string=charset_encode($string,$out_charset,$htmlencode); |
255 | return $string; |
256 | } |
257 | |
258 | /** |
259 | * Makes charset name suitable for decoding cycles |
260 | * |
261 | * @since 1.5.0 and 1.4.4 |
262 | * @param string $charset Name of charset |
263 | * @return string $charset Adjusted name of charset |
264 | */ |
265 | function fixcharset($charset) { |
266 | /* remove minus and characters that might be used in paths from charset |
267 | * name in order to be able to use it in function names and include calls. |
268 | */ |
269 | $charset=preg_replace("/[-:.\/\\\]/",'_',$charset); |
270 | |
271 | // OE ks_c_5601_1987 > cp949 |
272 | $charset=str_replace('ks_c_5601_1987','cp949',$charset); |
273 | // Moz x-euc-tw > euc-tw |
274 | $charset=str_replace('x_euc','euc',$charset); |
275 | // Moz x-windows-949 > cp949 |
276 | $charset=str_replace('x_windows_','cp',$charset); |
277 | |
278 | // windows-125x and cp125x charsets |
279 | $charset=str_replace('windows_','cp',$charset); |
280 | |
281 | // ibm > cp |
282 | $charset=str_replace('ibm','cp',$charset); |
283 | |
284 | // iso-8859-8-i -> iso-8859-8 |
285 | // use same cycle until I'll find differences |
286 | $charset=str_replace('iso_8859_8_i','iso_8859_8',$charset); |
287 | |
288 | return $charset; |
289 | } |
290 | |
291 | /** |
292 | * Set up the language to be output |
293 | * if $do_search is true, then scan the browser information |
294 | * for a possible language that we know |
295 | * |
296 | * Function sets system locale environment (LC_ALL, LANG, LANGUAGE), |
297 | * gettext translation bindings and html header information. |
298 | * |
299 | * Function returns error codes, if there is some fatal error. |
300 | * 0 = no error, |
301 | * 1 = mbstring support is not present, |
302 | * 2 = mbstring support is not present, user's translation reverted to en_US. |
303 | * |
304 | * @param string $sm_language translation used by user's interface |
305 | * @param bool $do_search use browser's preferred language detection functions. Defaults to false. |
306 | * @param bool $default set $sm_language to $squirrelmail_default_language if language detection fails or language is not set. Defaults to false. |
307 | * @return int function execution error codes. |
308 | */ |
309 | function set_up_language($sm_language, $do_search = false, $default = false) { |
310 | |
311 | static $SetupAlready = 0; |
312 | global $use_gettext, $languages, |
313 | $squirrelmail_language, $squirrelmail_default_language, $default_charset, |
314 | $sm_notAlias, $username, $data_dir; |
315 | |
316 | if ($SetupAlready) { |
317 | return; |
318 | } |
319 | |
320 | $SetupAlready = TRUE; |
321 | sqgetGlobalVar('HTTP_ACCEPT_LANGUAGE', $accept_lang, SQ_SERVER); |
322 | |
323 | /** |
324 | * If function is asked to detect preferred language |
325 | * OR squirrelmail default language is set to empty string |
326 | * AND |
327 | * squirrelmail language ($sm_language) is empty string |
328 | * (not set in user's prefs and no cookie with language info) |
329 | * AND |
330 | * browser provides list of preferred languages |
331 | * THEN |
332 | * get preferred language from HTTP_ACCEPT_LANGUAGE header |
333 | */ |
334 | if (($do_search || empty($squirrelmail_default_language)) && |
335 | ! $sm_language && |
336 | isset($accept_lang)) { |
337 | // TODO: use more than one language, if first language is not available |
338 | // FIXME: function assumes that string contains two or more characters. |
339 | // FIXME: some languages use 5 chars |
340 | $sm_language = substr($accept_lang, 0, 2); |
341 | } |
342 | |
343 | /** |
344 | * If language preference is not set OR script asks to use default language |
345 | * AND |
346 | * default squirrelmail language is not set to empty string |
347 | * THEN |
348 | * use default squirrelmail language value from configuration. |
349 | */ |
350 | if ((!$sm_language||$default) && |
351 | ! empty($squirrelmail_default_language)) { |
352 | $squirrelmail_language = $squirrelmail_default_language; |
353 | $sm_language = $squirrelmail_default_language; |
354 | } |
355 | |
356 | /** provide failsafe language when detection fails */ |
357 | if (! $sm_language) $sm_language='en_US'; |
358 | |
359 | $sm_notAlias = $sm_language; |
360 | |
361 | // Catching removed translation |
362 | // System reverts to English translation if user prefs contain translation |
363 | // that is not available in $languages array |
364 | if (!isset($languages[$sm_notAlias])) { |
365 | $sm_notAlias="en_US"; |
366 | } |
367 | |
368 | while (isset($languages[$sm_notAlias]['ALIAS'])) { |
369 | $sm_notAlias = $languages[$sm_notAlias]['ALIAS']; |
370 | } |
371 | |
372 | if ( isset($sm_language) && |
373 | $use_gettext && |
374 | $sm_language != '' && |
375 | isset($languages[$sm_notAlias]['CHARSET']) ) { |
376 | sq_bindtextdomain( 'squirrelmail', SM_PATH . 'locale/' ); |
377 | sq_textdomain( 'squirrelmail' ); |
378 | |
379 | // set codeset in order to avoid gettext charset conversions |
380 | if (function_exists('bind_textdomain_codeset')) { |
381 | // Japanese translation uses different internal charset |
382 | if ($sm_notAlias == 'ja_JP') { |
383 | bind_textdomain_codeset ('squirrelmail', 'EUC-JP'); |
384 | } else { |
385 | bind_textdomain_codeset ('squirrelmail', $languages[$sm_notAlias]['CHARSET'] ); |
386 | } |
387 | } |
388 | |
389 | // Use LOCALE key, if it is set. |
390 | if (isset($languages[$sm_notAlias]['LOCALE'])){ |
391 | $longlocale=$languages[$sm_notAlias]['LOCALE']; |
392 | } else { |
393 | $longlocale=$sm_notAlias; |
394 | } |
395 | |
396 | // try setting locale |
397 | $retlocale=sq_setlocale(LC_ALL, $longlocale); |
398 | |
399 | // check if locale is set and assign that locale to $longlocale |
400 | // in order to use it in putenv calls. |
401 | if (! is_bool($retlocale)) { |
402 | $longlocale=$retlocale; |
403 | } elseif (is_array($longlocale)) { |
404 | // setting of all locales failed. |
405 | // we need string instead of array used in LOCALE key. |
406 | $longlocale=$sm_notAlias; |
407 | } |
408 | |
409 | if ( !((bool)ini_get('safe_mode')) && |
410 | getenv( 'LC_ALL' ) != $longlocale ) { |
411 | putenv( "LC_ALL=$longlocale" ); |
412 | putenv( "LANG=$longlocale" ); |
413 | putenv( "LANGUAGE=$longlocale" ); |
414 | putenv( "LC_NUMERIC=C" ); |
415 | if ($sm_notAlias=='tr_TR') putenv( "LC_CTYPE=C" ); |
416 | } |
417 | // Workaround for plugins that use numbers with floating point |
418 | // It might be removed if plugins use correct decimal delimiters |
419 | // according to locale settings. |
420 | setlocale(LC_NUMERIC, 'C'); |
421 | // Workaround for specific Turkish strtolower/strtoupper rules. |
422 | // Many functions expect English conversion rules. |
423 | if ($sm_notAlias=='tr_TR') setlocale(LC_CTYPE,'C'); |
424 | |
425 | /** |
426 | * Set text direction/alignment variables |
427 | * When language environment is setup, scripts can use these globals |
428 | * without accessing $languages directly and making checks for optional |
429 | * array key. |
430 | */ |
431 | global $text_direction, $left_align, $right_align; |
432 | if (isset($languages[$sm_notAlias]['DIR']) && |
433 | $languages[$sm_notAlias]['DIR'] == 'rtl') { |
434 | /** |
435 | * Text direction |
436 | * @global string $text_direction |
437 | */ |
438 | $text_direction='rtl'; |
439 | /** |
440 | * Left alignment |
441 | * @global string $left_align |
442 | */ |
443 | $left_align='right'; |
444 | /** |
445 | * Right alignment |
446 | * @global string $right_align |
447 | */ |
448 | $right_align='left'; |
449 | } else { |
450 | $text_direction='ltr'; |
451 | $left_align='left'; |
452 | $right_align='right'; |
453 | } |
454 | |
455 | $squirrelmail_language = $sm_notAlias; |
456 | if ($squirrelmail_language == 'ja_JP') { |
457 | header ('Content-Type: text/html; charset=EUC-JP'); |
458 | if (!function_exists('mb_internal_encoding')) { |
459 | // Error messages can't be displayed here |
460 | $error = 1; |
461 | // Revert to English if possible. |
462 | if (function_exists('setPref') && $username!='' && $data_dir!="") { |
463 | setPref($data_dir, $username, 'language', "en_US"); |
464 | $error = 2; |
465 | } |
466 | // stop further execution in order not to get php errors on mb_internal_encoding(). |
467 | return $error; |
468 | } |
469 | if (function_exists('mb_language')) { |
470 | mb_language('Japanese'); |
471 | } |
472 | mb_internal_encoding('EUC-JP'); |
473 | mb_http_output('pass'); |
474 | } elseif ($squirrelmail_language == 'en_US') { |
475 | header( 'Content-Type: text/html; charset=' . $default_charset ); |
476 | } else { |
477 | header( 'Content-Type: text/html; charset=' . $languages[$sm_notAlias]['CHARSET'] ); |
478 | } |
479 | /** |
480 | * mbstring.func_overload fix (#929644). |
481 | * |
482 | * php mbstring extension can replace standard string functions with their multibyte |
483 | * equivalents. See http://www.php.net/ref.mbstring#mbstring.overload. This feature |
484 | * was added in php v.4.2.0 |
485 | * |
486 | * Some SquirrelMail functions work with 8bit strings in bytes. If interface is forced |
487 | * to use mbstring functions and mbstring internal encoding is set to multibyte charset, |
488 | * interface can't trust regular string functions. Due to mbstring overloading design |
489 | * limits php scripts can't control this setting. |
490 | * |
491 | * This hack should fix some issues related to 8bit strings in passwords. Correct fix is |
492 | * to disable mbstring overloading. Japanese translation uses different internal encoding. |
493 | */ |
494 | if ($squirrelmail_language != 'ja_JP' && |
495 | function_exists('mb_internal_encoding') && |
496 | check_php_version(4,2,0) && |
497 | (int)ini_get('mbstring.func_overload')!=0) { |
498 | mb_internal_encoding('pass'); |
499 | } |
500 | } |
501 | return 0; |
502 | } |
503 | |
504 | /** |
505 | * Sets default_charset variable according to the one that is used by user's translations. |
506 | * |
507 | * Function changes global $default_charset variable in order to be sure, that it |
508 | * contains charset used by user's translation. Sanity of $squirrelmail_language |
509 | * and $default_charset combination is also tested. |
510 | * |
511 | * There can be a $default_charset setting in the |
512 | * config.php file, but the user may have a different language |
513 | * selected for a user interface. This function checks the |
514 | * language selected by the user and tags the outgoing messages |
515 | * with the appropriate charset corresponding to the language |
516 | * selection. This is "more right" (tm), than just stamping the |
517 | * message blindly with the system-wide $default_charset. |
518 | */ |
519 | function set_my_charset(){ |
520 | global $data_dir, $username, $default_charset, $languages, $squirrelmail_language; |
521 | |
522 | $my_language = getPref($data_dir, $username, 'language'); |
523 | if (!$my_language) { |
524 | $my_language = $squirrelmail_language ; |
525 | } |
526 | // Catch removed translation |
527 | if (!isset($languages[$my_language])) { |
528 | $my_language="en_US"; |
529 | } |
530 | while (isset($languages[$my_language]['ALIAS'])) { |
531 | $my_language = $languages[$my_language]['ALIAS']; |
532 | } |
533 | $my_charset = $languages[$my_language]['CHARSET']; |
534 | if ($my_language!='en_US') { |
535 | $default_charset = $my_charset; |
536 | } |
537 | } |
538 | |
539 | /** |
540 | * Replaces non-braking spaces inserted by some browsers with regular space |
541 | * |
542 | * This function can be used to replace non-braking space symbols |
543 | * that are inserted in forms by some browsers instead of normal |
544 | * space symbol. |
545 | * |
546 | * @param string $string Text that needs to be cleaned |
547 | * @param string $charset Charset used in text |
548 | * @return string Cleaned text |
549 | */ |
550 | function cleanup_nbsp($string,$charset) { |
551 | |
552 | // reduce number of case statements |
553 | if (stristr('iso-8859-',substr($charset,0,9))){ |
554 | $output_charset="iso-8859-x"; |
555 | } |
556 | if (stristr('windows-125',substr($charset,0,11))){ |
557 | $output_charset="cp125x"; |
558 | } |
559 | if (stristr('koi8',substr($charset,0,4))){ |
560 | $output_charset="koi8-x"; |
561 | } |
562 | if (! isset($output_charset)){ |
563 | $output_charset=strtolower($charset); |
564 | } |
565 | |
566 | // where is non-braking space symbol |
567 | switch($output_charset): |
568 | case "iso-8859-x": |
569 | case "cp125x": |
570 | case "iso-2022-jp": |
571 | $nbsp="\xA0"; |
572 | break; |
573 | case "koi8-x": |
574 | $nbsp="\x9A"; |
575 | break; |
576 | case "utf-8": |
577 | $nbsp="\xC2\xA0"; |
578 | break; |
579 | default: |
580 | // don't change string if charset is unmatched |
581 | return $string; |
582 | endswitch; |
583 | |
584 | // return space instead of non-braking space. |
585 | return str_replace($nbsp,' ',$string); |
586 | } |
587 | |
588 | /** |
589 | * Function informs if it is safe to convert given charset to the one that is used by user. |
590 | * |
591 | * It is safe to use conversion only if user uses utf-8 encoding and when |
592 | * converted charset is similar to the one that is used by user. |
593 | * |
594 | * @param string $input_charset Charset of text that needs to be converted |
595 | * @return bool is it possible to convert to user's charset |
596 | */ |
597 | function is_conversion_safe($input_charset) { |
598 | global $languages, $sm_notAlias, $default_charset, $lossy_encoding; |
599 | |
600 | if (isset($lossy_encoding) && $lossy_encoding ) |
601 | return true; |
602 | |
603 | // convert to lower case |
604 | $input_charset = strtolower($input_charset); |
605 | |
606 | // Is user's locale Unicode based ? |
607 | if ( $default_charset == "utf-8" ) { |
608 | return true; |
609 | } |
610 | |
611 | // Charsets that are similar |
612 | switch ($default_charset) { |
613 | case "windows-1251": |
614 | if ( $input_charset == "iso-8859-5" || |
615 | $input_charset == "koi8-r" || |
616 | $input_charset == "koi8-u" ) { |
617 | return true; |
618 | } else { |
619 | return false; |
620 | } |
621 | case "windows-1257": |
622 | if ( $input_charset == "iso-8859-13" || |
623 | $input_charset == "iso-8859-4" ) { |
624 | return true; |
625 | } else { |
626 | return false; |
627 | } |
628 | case "iso-8859-4": |
629 | if ( $input_charset == "iso-8859-13" || |
630 | $input_charset == "windows-1257" ) { |
631 | return true; |
632 | } else { |
633 | return false; |
634 | } |
635 | case "iso-8859-5": |
636 | if ( $input_charset == "windows-1251" || |
637 | $input_charset == "koi8-r" || |
638 | $input_charset == "koi8-u" ) { |
639 | return true; |
640 | } else { |
641 | return false; |
642 | } |
643 | case "iso-8859-13": |
644 | if ( $input_charset == "iso-8859-4" || |
645 | $input_charset == "windows-1257" ) { |
646 | return true; |
647 | } else { |
648 | return false; |
649 | } |
650 | case "koi8-r": |
651 | if ( $input_charset == "windows-1251" || |
652 | $input_charset == "iso-8859-5" || |
653 | $input_charset == "koi8-u" ) { |
654 | return true; |
655 | } else { |
656 | return false; |
657 | } |
658 | case "koi8-u": |
659 | if ( $input_charset == "windows-1251" || |
660 | $input_charset == "iso-8859-5" || |
661 | $input_charset == "koi8-r" ) { |
662 | return true; |
663 | } else { |
664 | return false; |
665 | } |
666 | default: |
667 | return false; |
668 | } |
669 | } |
670 | |
671 | /** |
672 | * Converts html character entities to numeric entities |
673 | * |
674 | * SquirrelMail encoding functions work only with numeric entities. |
675 | * This function fixes issues with decoding functions that might convert |
676 | * some symbols to character entities. Issue is specific to PHP recode |
677 | * extension decoding. Function is used internally in charset_convert() |
678 | * function. |
679 | * @param string $str string that might contain html character entities |
680 | * @return string string with character entities converted to decimals. |
681 | * @since 1.5.2 |
682 | */ |
683 | function sqi18n_convert_entities($str) { |
684 | |
685 | $entities = array( |
686 | // Latin 1 |
687 | ' ' => ' ', |
688 | '¡' => '¡', |
689 | '¢' => '¢', |
690 | '£' => '£', |
691 | '¤' => '¤', |
692 | '¥' => '¥', |
693 | '¦' => '¦', |
694 | '§' => '§', |
695 | '¨' => '¨', |
696 | '©' => '©', |
697 | 'ª' => 'ª', |
698 | '«' => '«', |
699 | '¬' => '¬', |
700 | '­' => '­', |
701 | '®' => '®', |
702 | '¯' => '¯', |
703 | '°' => '°', |
704 | '±' => '±', |
705 | '²' => '²', |
706 | '³' => '³', |
707 | '´' => '´', |
708 | 'µ' => 'µ', |
709 | '¶' => '¶', |
710 | '·' => '·', |
711 | '¸' => '¸', |
712 | '¹' => '¹', |
713 | 'º' => 'º', |
714 | '»' => '»', |
715 | '¼' => '¼', |
716 | '½' => '½', |
717 | '¾' => '¾', |
718 | '¿' => '¿', |
719 | 'À' => 'À', |
720 | 'Á' => 'Á', |
721 | 'Â' => 'Â', |
722 | 'Ã' => 'Ã', |
723 | 'Ä' => 'Ä', |
724 | 'Å' => 'Å', |
725 | 'Æ' => 'Æ', |
726 | 'Ç' => 'Ç', |
727 | 'È' => 'È', |
728 | 'É' => 'É', |
729 | 'Ê' => 'Ê', |
730 | 'Ë' => 'Ë', |
731 | 'Ì' => 'Ì', |
732 | 'Í' => 'Í', |
733 | 'Î' => 'Î', |
734 | 'Ï' => 'Ï', |
735 | 'Ð' => 'Ð', |
736 | 'Ñ' => 'Ñ', |
737 | 'Ò' => 'Ò', |
738 | 'Ó' => 'Ó', |
739 | 'Ô' => 'Ô', |
740 | 'Õ' => 'Õ', |
741 | 'Ö' => 'Ö', |
742 | '×' => '×', |
743 | 'Ø' => 'Ø', |
744 | 'Ù' => 'Ù', |
745 | 'Ú' => 'Ú', |
746 | 'Û' => 'Û', |
747 | 'Ü' => 'Ü', |
748 | 'Ý' => 'Ý', |
749 | 'Þ' => 'Þ', |
750 | 'ß' => 'ß', |
751 | 'à' => 'à', |
752 | 'á' => 'á', |
753 | 'â' => 'â', |
754 | 'ã' => 'ã', |
755 | 'ä' => 'ä', |
756 | 'å' => 'å', |
757 | 'æ' => 'æ', |
758 | 'ç' => 'ç', |
759 | 'è' => 'è', |
760 | 'é' => 'é', |
761 | 'ê' => 'ê', |
762 | 'ë' => 'ë', |
763 | 'ì' => 'ì', |
764 | 'í' => 'í', |
765 | 'î' => 'î', |
766 | 'ï' => 'ï', |
767 | 'ð' => 'ð', |
768 | 'ñ' => 'ñ', |
769 | 'ò' => 'ò', |
770 | 'ó' => 'ó', |
771 | 'ô' => 'ô', |
772 | 'õ' => 'õ', |
773 | 'ö' => 'ö', |
774 | '÷' => '÷', |
775 | 'ø' => 'ø', |
776 | 'ù' => 'ù', |
777 | 'ú' => 'ú', |
778 | 'û' => 'û', |
779 | 'ü' => 'ü', |
780 | 'ý' => 'ý', |
781 | 'þ' => 'þ', |
782 | 'ÿ' => 'ÿ', |
783 | // Latin Extended-A |
784 | 'Œ' => 'Œ', |
785 | 'œ' => 'œ', |
786 | 'Š' => 'Š', |
787 | 'š' => 'š', |
788 | 'Ÿ' => 'Ÿ', |
789 | // Spacing Modifier Letters |
790 | 'ˆ' => 'ˆ', |
791 | '˜' => '˜', |
792 | // General Punctuation |
793 | ' ' => ' ', |
794 | ' ' => ' ', |
795 | ' ' => ' ', |
796 | '‌' => '‌', |
797 | '‍' => '‍', |
798 | '‎' => '‎', |
799 | '‏' => '‏', |
800 | '–' => '–', |
801 | '—' => '—', |
802 | '‘' => '‘', |
803 | '’' => '’', |
804 | '‚' => '‚', |
805 | '“' => '“', |
806 | '”' => '”', |
807 | '„' => '„', |
808 | '†' => '†', |
809 | '‡' => '‡', |
810 | '‰' => '‰', |
811 | '‹' => '‹', |
812 | '›' => '›', |
813 | '€' => '€', |
814 | // Latin Extended-B |
815 | 'ƒ' => 'ƒ', |
816 | // Greek |
817 | 'Α' => 'Α', |
818 | 'Β' => 'Β', |
819 | 'Γ' => 'Γ', |
820 | 'Δ' => 'Δ', |
821 | 'Ε' => 'Ε', |
822 | 'Ζ' => 'Ζ', |
823 | 'Η' => 'Η', |
824 | 'Θ' => 'Θ', |
825 | 'Ι' => 'Ι', |
826 | 'Κ' => 'Κ', |
827 | 'Λ' => 'Λ', |
828 | 'Μ' => 'Μ', |
829 | 'Ν' => 'Ν', |
830 | 'Ξ' => 'Ξ', |
831 | 'Ο' => 'Ο', |
832 | 'Π' => 'Π', |
833 | 'Ρ' => 'Ρ', |
834 | 'Σ' => 'Σ', |
835 | 'Τ' => 'Τ', |
836 | 'Υ' => 'Υ', |
837 | 'Φ' => 'Φ', |
838 | 'Χ' => 'Χ', |
839 | 'Ψ' => 'Ψ', |
840 | 'Ω' => 'Ω', |
841 | 'α' => 'α', |
842 | 'β' => 'β', |
843 | 'γ' => 'γ', |
844 | 'δ' => 'δ', |
845 | 'ε' => 'ε', |
846 | 'ζ' => 'ζ', |
847 | 'η' => 'η', |
848 | 'θ' => 'θ', |
849 | 'ι' => 'ι', |
850 | 'κ' => 'κ', |
851 | 'λ' => 'λ', |
852 | 'μ' => 'μ', |
853 | 'ν' => 'ν', |
854 | 'ξ' => 'ξ', |
855 | 'ο' => 'ο', |
856 | 'π' => 'π', |
857 | 'ρ' => 'ρ', |
858 | 'ς' => 'ς', |
859 | 'σ' => 'σ', |
860 | 'τ' => 'τ', |
861 | 'υ' => 'υ', |
862 | 'φ' => 'φ', |
863 | 'χ' => 'χ', |
864 | 'ψ' => 'ψ', |
865 | 'ω' => 'ω', |
866 | 'ϑ' => 'ϑ', |
867 | 'ϒ' => 'ϒ', |
868 | 'ϖ' => 'ϖ', |
869 | // General Punctuation |
870 | '•' => '•', |
871 | '…' => '…', |
872 | '′' => '′', |
873 | '″' => '″', |
874 | '‾' => '‾', |
875 | '⁄' => '⁄', |
876 | // Letterlike Symbols |
877 | '℘' => '℘', |
878 | 'ℑ' => 'ℑ', |
879 | 'ℜ' => 'ℜ', |
880 | '™' => '™', |
881 | 'ℵ' => 'ℵ', |
882 | // Arrows |
883 | '←' => '←', |
884 | '↑' => '↑', |
885 | '→' => '→', |
886 | '↓' => '↓', |
887 | '↔' => '↔', |
888 | '↵' => '↵', |
889 | '⇐' => '⇐', |
890 | '⇑' => '⇑', |
891 | '⇒' => '⇒', |
892 | '⇓' => '⇓', |
893 | '⇔' => '⇔', |
894 | // Mathematical Operators |
895 | '∀' => '∀', |
896 | '∂' => '∂', |
897 | '∃' => '∃', |
898 | '∅' => '∅', |
899 | '∇' => '∇', |
900 | '∈' => '∈', |
901 | '∉' => '∉', |
902 | '∋' => '∋', |
903 | '∏' => '∏', |
904 | '∑' => '∑', |
905 | '−' => '−', |
906 | '∗' => '∗', |
907 | '√' => '√', |
908 | '∝' => '∝', |
909 | '∞' => '∞', |
910 | '∠' => '∠', |
911 | '∧' => '∧', |
912 | '∨' => '∨', |
913 | '∩' => '∩', |
914 | '∪' => '∪', |
915 | '∫' => '∫', |
916 | '∴' => '∴', |
917 | '∼' => '∼', |
918 | '≅' => '≅', |
919 | '≈' => '≈', |
920 | '≠' => '≠', |
921 | '≡' => '≡', |
922 | '≤' => '≤', |
923 | '≥' => '≥', |
924 | '⊂' => '⊂', |
925 | '⊃' => '⊃', |
926 | '⊄' => '⊄', |
927 | '⊆' => '⊆', |
928 | '⊇' => '⊇', |
929 | '⊕' => '⊕', |
930 | '⊗' => '⊗', |
931 | '⊥' => '⊥', |
932 | '⋅' => '⋅', |
933 | // Miscellaneous Technical |
934 | '⌈' => '⌈', |
935 | '⌉' => '⌉', |
936 | '⌊' => '⌊', |
937 | '⌋' => '⌋', |
938 | '⟨' => '〈', |
939 | '⟩' => '〉', |
940 | // Geometric Shapes |
941 | '◊' => '◊', |
942 | // Miscellaneous Symbols |
943 | '♠' => '♠', |
944 | '♣' => '♣', |
945 | '♥' => '♥', |
946 | '♦' => '♦'); |
947 | |
948 | $str = str_replace(array_keys($entities), array_values($entities), $str); |
949 | |
950 | return $str; |
951 | } |
952 | |
953 | /* ------------------------------ main --------------------------- */ |
954 | |
955 | global $squirrelmail_language, $languages, $use_gettext; |
956 | |
957 | if (! sqgetGlobalVar('squirrelmail_language',$squirrelmail_language,SQ_COOKIE)) { |
958 | $squirrelmail_language = ''; |
959 | } |
960 | |
961 | /** |
962 | * Array specifies the available translations. |
963 | * |
964 | * Structure of array: |
965 | * $languages['language']['variable'] = 'value' |
966 | * |
967 | * Possible 'variable' names: |
968 | * NAME - Translation name in English |
969 | * CHARSET - Encoding used by translation |
970 | * ALIAS - used when 'language' is only short name and 'value' should provide long language name |
971 | * ALTNAME - Native translation name. Any 8bit symbols must be html encoded. |
972 | * LOCALE - Full locale name (in xx_XX.charset format). It can use array with more than one locale name since 1.4.5 and 1.5.1 |
973 | * DIR - Text direction. Used to define Right-to-Left languages. Possible values 'rtl' or 'ltr'. If undefined - defaults to 'ltr' |
974 | * XTRA_CODE - translation uses special functions. See doc/i18n.txt |
975 | * |
976 | * Each 'language' definition requires NAME+CHARSET or ALIAS variables. |
977 | * |
978 | * @name $languages |
979 | * @global array $languages |
980 | */ |
981 | $languages['en_US']['NAME'] = 'English'; |
982 | $languages['en_US']['CHARSET'] = 'iso-8859-1'; |
983 | $languages['en_US']['LOCALE'] = 'en_US.ISO8859-1'; |
984 | $languages['en']['ALIAS'] = 'en_US'; |
985 | |
986 | /** |
987 | * Automatic translation loading from setup.php files. |
988 | * Solution for bug. 1240889. |
989 | * setup.php file can contain $languages array entries and XTRA_CODE functions. |
990 | */ |
991 | if (is_dir(SM_PATH . 'locale') && |
992 | is_readable(SM_PATH . 'locale')) { |
993 | $localedir = dir(SM_PATH . 'locale'); |
994 | while($lang_dir=$localedir->read()) { |
995 | // remove trailing slash, if present |
996 | if (substr($lang_dir,-1)=='/') { |
997 | $lang_dir = substr($lang_dir,0,-1); |
998 | } |
999 | if ($lang_dir != '..' && $lang_dir != '.' && $lang_dir != 'CVS' && |
1000 | is_dir(SM_PATH.'locale/'.$lang_dir) && |
1001 | file_exists(SM_PATH.'locale/'.$lang_dir.'/setup.php')) { |
1002 | include_once(SM_PATH.'locale/'.$lang_dir.'/setup.php'); |
1003 | } |
1004 | } |
1005 | $localedir->close(); |
1006 | } |
1007 | |
1008 | /* Detect whether gettext is installed. */ |
1009 | $gettext_flags = 0; |
1010 | if (function_exists('_')) { |
1011 | $gettext_flags += 1; |
1012 | } |
1013 | if (function_exists('bindtextdomain')) { |
1014 | $gettext_flags += 2; |
1015 | } |
1016 | if (function_exists('textdomain')) { |
1017 | $gettext_flags += 4; |
1018 | } |
1019 | if (function_exists('ngettext')) { |
1020 | $gettext_flags += 8; |
1021 | } |
1022 | |
1023 | /* If gettext is fully loaded, cool */ |
1024 | if ($gettext_flags == 15) { |
1025 | $use_gettext = true; |
1026 | } |
1027 | |
1028 | /* If ngettext support is missing, load it */ |
1029 | elseif ($gettext_flags == 7) { |
1030 | $use_gettext = true; |
1031 | // load internal ngettext functions |
1032 | include_once(SM_PATH . 'class/l10n.class.php'); |
1033 | include_once(SM_PATH . 'functions/ngettext.php'); |
1034 | } |
1035 | |
1036 | /* If we can fake gettext, try that */ |
1037 | elseif ($gettext_flags == 0) { |
1038 | $use_gettext = true; |
1039 | include_once(SM_PATH . 'functions/gettext.php'); |
1040 | } else { |
1041 | /* Uh-ho. A weird install */ |
1042 | if (! $gettext_flags & 1) { |
1043 | /** |
1044 | * Function is used as replacement in broken installs |
1045 | * @ignore |
1046 | */ |
1047 | function _($str) { |
1048 | return $str; |
1049 | } |
1050 | } |
1051 | if (! $gettext_flags & 2) { |
1052 | /** |
1053 | * Function is used as replacement in broken installs |
1054 | * @ignore |
1055 | */ |
1056 | function bindtextdomain() { |
1057 | return; |
1058 | } |
1059 | } |
1060 | if (! $gettext_flags & 4) { |
1061 | /** |
1062 | * Function is used as replacemet in broken installs |
1063 | * @ignore |
1064 | */ |
1065 | function textdomain() { |
1066 | return; |
1067 | } |
1068 | } |
1069 | if (! $gettext_flags & 8) { |
1070 | /** |
1071 | * Function is used as replacemet in broken installs |
1072 | * @ignore |
1073 | */ |
1074 | function ngettext($str,$str2,$number) { |
1075 | if ($number>1) { |
1076 | return $str2; |
1077 | } else { |
1078 | return $str; |
1079 | } |
1080 | } |
1081 | } |
1082 | if (! function_exists('dgettext')) { |
1083 | /** |
1084 | * Replacement for broken setups. |
1085 | * @ignore |
1086 | */ |
1087 | function dgettext($domain,$str) { |
1088 | return $str; |
1089 | } |
1090 | } |
1091 | if (! function_exists('dngettext')) { |
1092 | /** |
1093 | * Replacement for broken setups |
1094 | * @ignore |
1095 | */ |
1096 | function dngettext($domain,$str1,$strn,$number) { |
1097 | return ($number==1 ? $str1 : $strn); |
1098 | } |
1099 | } |
1100 | } |
1101 | ?> |