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) { |
75 | // string with only one locale |
76 | if (is_string($locale)) |
77 | return setlocale($category,$locale); |
78 | |
79 | if (! check_php_version(4,3)) { |
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 | } |
90 | return $ret; |
91 | } |
92 | |
93 | /** |
94 | * Converts string from given charset to charset, that can be displayed by user translation. |
95 | * |
96 | * Function by default returns html encoded strings, if translation uses different encoding. |
97 | * If Japanese translation is used - function returns string converted to euc-jp |
98 | * If iconv or recode functions are enabled and translation uses utf-8 - function returns utf-8 encoded string. |
99 | * If $charset is not supported - function returns unconverted string. |
100 | * |
101 | * sanitizing of html tags is also done by this function. |
102 | * |
103 | * @param string $charset |
104 | * @param string $string Text to be decoded |
105 | * @param boolean $force_decode converts string to html without $charset!=$default_charset check. |
106 | * Argument is available since 1.5.1 and 1.4.5. |
107 | * @param boolean $save_html disables htmlspecialchars() in order to preserve |
108 | * html formating. Use with care. Available since 1.5.1 |
109 | * @return string decoded string |
110 | */ |
111 | function charset_decode ($charset, $string, $force_decode=false, $save_html=false) { |
112 | global $languages, $squirrelmail_language, $default_charset; |
113 | global $use_php_recode, $use_php_iconv, $aggressive_decoding; |
114 | |
115 | if (isset($languages[$squirrelmail_language]['XTRA_CODE']) && |
116 | function_exists($languages[$squirrelmail_language]['XTRA_CODE'] . '_decode')) { |
117 | $string = call_user_func($languages[$squirrelmail_language]['XTRA_CODE'] . '_decode', $string); |
118 | } |
119 | |
120 | $charset = strtolower($charset); |
121 | |
122 | set_my_charset(); |
123 | |
124 | // Variables that allow to use functions without function_exist() calls |
125 | if (! isset($use_php_recode) || $use_php_recode=="" ) { |
126 | $use_php_recode=false; } |
127 | if (! isset($use_php_iconv) || $use_php_iconv=="" ) { |
128 | $use_php_iconv=false; } |
129 | |
130 | // Don't do conversion if charset is the same. |
131 | if ( ! $force_decode && $charset == strtolower($default_charset) ) |
132 | return ($save_html ? $string : htmlspecialchars($string)); |
133 | |
134 | // catch iso-8859-8-i thing |
135 | if ( $charset == "iso-8859-8-i" ) |
136 | $charset = "iso-8859-8"; |
137 | |
138 | /* |
139 | * Recode converts html special characters automatically if you use |
140 | * 'charset..html' decoding. There is no documented way to put -d option |
141 | * into php recode function call. |
142 | */ |
143 | if ( $use_php_recode ) { |
144 | if ( $default_charset == "utf-8" ) { |
145 | // other charsets can be converted to utf-8 without loss. |
146 | // and output string is smaller |
147 | $string = recode_string($charset . "..utf-8",$string); |
148 | return ($save_html ? $string : htmlspecialchars($string)); |
149 | } else { |
150 | $string = recode_string($charset . "..html",$string); |
151 | // recode does not convert single quote, htmlspecialchars does. |
152 | $string = str_replace("'", ''', $string); |
153 | // undo html specialchars |
154 | if ($save_html) |
155 | $string=str_replace(array('&','"','<','>'), |
156 | array('&','"','<','>'),$string); |
157 | return $string; |
158 | } |
159 | } |
160 | |
161 | // iconv functions does not have html target and can be used only with utf-8 |
162 | if ( $use_php_iconv && $default_charset=='utf-8') { |
163 | $string = iconv($charset,$default_charset,$string); |
164 | return ($save_html ? $string : htmlspecialchars($string)); |
165 | } |
166 | |
167 | // If we don't use recode and iconv, we'll do it old way. |
168 | |
169 | /* All HTML special characters are 7 bit and can be replaced first */ |
170 | if (! $save_html) $string = htmlspecialchars ($string); |
171 | |
172 | /* controls cpu and memory intensive decoding cycles */ |
173 | if (! isset($aggressive_decoding) || $aggressive_decoding=="" ) { |
174 | $aggressive_decoding=false; } |
175 | |
176 | $decode=fixcharset($charset); |
177 | $decodefile=SM_PATH . 'functions/decode/' . $decode . '.php'; |
178 | if (file_exists($decodefile)) { |
179 | include_once($decodefile); |
180 | // send $save_html argument to decoding function. needed for iso-2022-xx decoding. |
181 | $ret = call_user_func('charset_decode_'.$decode, $string, $save_html); |
182 | } else { |
183 | $ret = $string; |
184 | } |
185 | return( $ret ); |
186 | } |
187 | |
188 | /** |
189 | * Converts html string to given charset |
190 | * @since 1.5.1 and 1.4.4 |
191 | * @param string $string |
192 | * @param string $charset |
193 | * @param boolean $htmlencode keep htmlspecialchars encoding |
194 | * @param string |
195 | */ |
196 | function charset_encode($string,$charset,$htmlencode=true) { |
197 | global $default_charset; |
198 | |
199 | $encode=fixcharset($charset); |
200 | $encodefile=SM_PATH . 'functions/encode/' . $encode . '.php'; |
201 | if (file_exists($encodefile)) { |
202 | include_once($encodefile); |
203 | $ret = call_user_func('charset_encode_'.$encode, $string); |
204 | } elseif(file_exists(SM_PATH . 'functions/encode/us_ascii.php')) { |
205 | // function replaces all 8bit html entities with question marks. |
206 | // it is used when other encoding functions are unavailable |
207 | include_once(SM_PATH . 'functions/encode/us_ascii.php'); |
208 | $ret = charset_encode_us_ascii($string); |
209 | } else { |
210 | /** |
211 | * fix for yahoo users that remove all us-ascii related things |
212 | */ |
213 | $ret = $string; |
214 | } |
215 | |
216 | /** |
217 | * Undo html special chars, some places (like compose form) have |
218 | * own sanitizing functions and don't need html symbols. |
219 | * Undo chars only after encoding in order to prevent conversion of |
220 | * html entities in plain text emails. |
221 | */ |
222 | if (! $htmlencode ) { |
223 | $ret = str_replace(array('&','>','<','"'),array('&','>','<','"'),$ret); |
224 | } |
225 | return( $ret ); |
226 | } |
227 | |
228 | /** |
229 | * Combined decoding and encoding functions |
230 | * |
231 | * If conversion is done to charset different that utf-8, unsupported symbols |
232 | * will be replaced with question marks. |
233 | * @since 1.5.1 and 1.4.4 |
234 | * @param string $in_charset initial charset |
235 | * @param string $string string that has to be converted |
236 | * @param string $out_charset final charset |
237 | * @param boolean $htmlencode keep htmlspecialchars encoding |
238 | * @return string converted string |
239 | */ |
240 | function charset_convert($in_charset,$string,$out_charset,$htmlencode=true) { |
241 | $string=charset_decode($in_charset,$string,true); |
242 | $string=sqi18n_convert_entities($string); |
243 | $string=charset_encode($string,$out_charset,$htmlencode); |
244 | return $string; |
245 | } |
246 | |
247 | /** |
248 | * Makes charset name suitable for decoding cycles |
249 | * |
250 | * @since 1.5.0 and 1.4.4 |
251 | * @param string $charset Name of charset |
252 | * @return string $charset Adjusted name of charset |
253 | */ |
254 | function fixcharset($charset) { |
255 | /* remove minus and characters that might be used in paths from charset |
256 | * name in order to be able to use it in function names and include calls. |
257 | */ |
258 | $charset=preg_replace("/[-:.\/\\\]/",'_',$charset); |
259 | |
260 | // OE ks_c_5601_1987 > cp949 |
261 | $charset=str_replace('ks_c_5601_1987','cp949',$charset); |
262 | // Moz x-euc-tw > euc-tw |
263 | $charset=str_replace('x_euc','euc',$charset); |
264 | // Moz x-windows-949 > cp949 |
265 | $charset=str_replace('x_windows_','cp',$charset); |
266 | |
267 | // windows-125x and cp125x charsets |
268 | $charset=str_replace('windows_','cp',$charset); |
269 | |
270 | // ibm > cp |
271 | $charset=str_replace('ibm','cp',$charset); |
272 | |
273 | // iso-8859-8-i -> iso-8859-8 |
274 | // use same cycle until I'll find differences |
275 | $charset=str_replace('iso_8859_8_i','iso_8859_8',$charset); |
276 | |
277 | return $charset; |
278 | } |
279 | |
280 | /** |
281 | * Set up the language to be output |
282 | * if $do_search is true, then scan the browser information |
283 | * for a possible language that we know |
284 | * |
285 | * Function sets system locale environment (LC_ALL, LANG, LANGUAGE), |
286 | * gettext translation bindings and html header information. |
287 | * |
288 | * Function returns error codes, if there is some fatal error. |
289 | * 0 = no error, |
290 | * 1 = mbstring support is not present, |
291 | * 2 = mbstring support is not present, user's translation reverted to en_US. |
292 | * |
293 | * @param string $sm_language translation used by user's interface |
294 | * @param bool $do_search use browser's preferred language detection functions. Defaults to false. |
295 | * @param bool $default set $sm_language to $squirrelmail_default_language if language detection fails or language is not set. Defaults to false. |
296 | * @return int function execution error codes. |
297 | */ |
298 | function set_up_language($sm_language, $do_search = false, $default = false) { |
299 | |
300 | static $SetupAlready = 0; |
301 | global $use_gettext, $languages, |
302 | $squirrelmail_language, $squirrelmail_default_language, $default_charset, |
303 | $sm_notAlias, $username, $data_dir; |
304 | |
305 | if ($SetupAlready) { |
306 | return; |
307 | } |
308 | |
309 | $SetupAlready = TRUE; |
310 | sqgetGlobalVar('HTTP_ACCEPT_LANGUAGE', $accept_lang, SQ_SERVER); |
311 | |
312 | /** |
313 | * If function is asked to detect preferred language |
314 | * OR squirrelmail default language is set to empty string |
315 | * AND |
316 | * squirrelmail language ($sm_language) is empty string |
317 | * (not set in user's prefs and no cookie with language info) |
318 | * AND |
319 | * browser provides list of preferred languages |
320 | * THEN |
321 | * get preferred language from HTTP_ACCEPT_LANGUAGE header |
322 | */ |
323 | if (($do_search || empty($squirrelmail_default_language)) && |
324 | ! $sm_language && |
325 | isset($accept_lang)) { |
326 | // TODO: use more than one language, if first language is not available |
327 | // FIXME: function assumes that string contains two or more characters. |
328 | // FIXME: some languages use 5 chars |
329 | $sm_language = substr($accept_lang, 0, 2); |
330 | } |
331 | |
332 | /** |
333 | * If language preference is not set OR script asks to use default language |
334 | * AND |
335 | * default squirrelmail language is not set to empty string |
336 | * THEN |
337 | * use default squirrelmail language value from configuration. |
338 | */ |
339 | if ((!$sm_language||$default) && |
340 | ! empty($squirrelmail_default_language)) { |
341 | $squirrelmail_language = $squirrelmail_default_language; |
342 | $sm_language = $squirrelmail_default_language; |
343 | } |
344 | |
345 | /** provide failsafe language when detection fails */ |
346 | if (! $sm_language) $sm_language='en_US'; |
347 | |
348 | $sm_notAlias = $sm_language; |
349 | |
350 | // Catching removed translation |
351 | // System reverts to English translation if user prefs contain translation |
352 | // that is not available in $languages array |
353 | if (!isset($languages[$sm_notAlias])) { |
354 | $sm_notAlias="en_US"; |
355 | } |
356 | |
357 | while (isset($languages[$sm_notAlias]['ALIAS'])) { |
358 | $sm_notAlias = $languages[$sm_notAlias]['ALIAS']; |
359 | } |
360 | |
361 | if ( isset($sm_language) && |
362 | $use_gettext && |
363 | $sm_language != '' && |
364 | isset($languages[$sm_notAlias]['CHARSET']) ) { |
365 | sq_bindtextdomain( 'squirrelmail', SM_PATH . 'locale/' ); |
366 | sq_textdomain( 'squirrelmail' ); |
367 | |
368 | // set codeset in order to avoid gettext charset conversions |
369 | if (function_exists('bind_textdomain_codeset')) { |
370 | // Japanese translation uses different internal charset |
371 | if ($sm_notAlias == 'ja_JP') { |
372 | bind_textdomain_codeset ('squirrelmail', 'EUC-JP'); |
373 | } else { |
374 | bind_textdomain_codeset ('squirrelmail', $languages[$sm_notAlias]['CHARSET'] ); |
375 | } |
376 | } |
377 | |
378 | // Use LOCALE key, if it is set. |
379 | if (isset($languages[$sm_notAlias]['LOCALE'])){ |
380 | $longlocale=$languages[$sm_notAlias]['LOCALE']; |
381 | } else { |
382 | $longlocale=$sm_notAlias; |
383 | } |
384 | |
385 | // try setting locale |
386 | $retlocale=sq_setlocale(LC_ALL, $longlocale); |
387 | |
388 | // check if locale is set and assign that locale to $longlocale |
389 | // in order to use it in putenv calls. |
390 | if (! is_bool($retlocale)) { |
391 | $longlocale=$retlocale; |
392 | } elseif (is_array($longlocale)) { |
393 | // setting of all locales failed. |
394 | // we need string instead of array used in LOCALE key. |
395 | $longlocale=$sm_notAlias; |
396 | } |
397 | |
398 | if ( !((bool)ini_get('safe_mode')) && |
399 | getenv( 'LC_ALL' ) != $longlocale ) { |
400 | putenv( "LC_ALL=$longlocale" ); |
401 | putenv( "LANG=$longlocale" ); |
402 | putenv( "LANGUAGE=$longlocale" ); |
403 | putenv( "LC_NUMERIC=C" ); |
404 | if ($sm_notAlias=='tr_TR') putenv( "LC_CTYPE=C" ); |
405 | } |
406 | // Workaround for plugins that use numbers with floating point |
407 | // It might be removed if plugins use correct decimal delimiters |
408 | // according to locale settings. |
409 | setlocale(LC_NUMERIC, 'C'); |
410 | // Workaround for specific Turkish strtolower/strtoupper rules. |
411 | // Many functions expect English conversion rules. |
412 | if ($sm_notAlias=='tr_TR') setlocale(LC_CTYPE,'C'); |
413 | |
414 | /** |
415 | * Set text direction/alignment variables |
416 | * When language environment is setup, scripts can use these globals |
417 | * without accessing $languages directly and making checks for optional |
418 | * array key. |
419 | */ |
420 | global $text_direction, $left_align, $right_align; |
421 | if (isset($languages[$sm_notAlias]['DIR']) && |
422 | $languages[$sm_notAlias]['DIR'] == 'rtl') { |
423 | /** |
424 | * Text direction |
425 | * @global string $text_direction |
426 | */ |
427 | $text_direction='rtl'; |
428 | /** |
429 | * Left alignment |
430 | * @global string $left_align |
431 | */ |
432 | $left_align='right'; |
433 | /** |
434 | * Right alignment |
435 | * @global string $right_align |
436 | */ |
437 | $right_align='left'; |
438 | } else { |
439 | $text_direction='ltr'; |
440 | $left_align='left'; |
441 | $right_align='right'; |
442 | } |
443 | |
444 | $squirrelmail_language = $sm_notAlias; |
445 | if ($squirrelmail_language == 'ja_JP') { |
446 | header ('Content-Type: text/html; charset=EUC-JP'); |
447 | if (!function_exists('mb_internal_encoding')) { |
448 | // Error messages can't be displayed here |
449 | $error = 1; |
450 | // Revert to English if possible. |
451 | if (function_exists('setPref') && $username!='' && $data_dir!="") { |
452 | setPref($data_dir, $username, 'language', "en_US"); |
453 | $error = 2; |
454 | } |
455 | // stop further execution in order not to get php errors on mb_internal_encoding(). |
456 | return $error; |
457 | } |
458 | if (function_exists('mb_language')) { |
459 | mb_language('Japanese'); |
460 | } |
461 | mb_internal_encoding('EUC-JP'); |
462 | mb_http_output('pass'); |
463 | } elseif ($squirrelmail_language == 'en_US') { |
464 | header( 'Content-Type: text/html; charset=' . $default_charset ); |
465 | } else { |
466 | header( 'Content-Type: text/html; charset=' . $languages[$sm_notAlias]['CHARSET'] ); |
467 | } |
468 | /** |
469 | * mbstring.func_overload fix (#929644). |
470 | * |
471 | * php mbstring extension can replace standard string functions with their multibyte |
472 | * equivalents. See http://www.php.net/ref.mbstring#mbstring.overload. This feature |
473 | * was added in php v.4.2.0 |
474 | * |
475 | * Some SquirrelMail functions work with 8bit strings in bytes. If interface is forced |
476 | * to use mbstring functions and mbstring internal encoding is set to multibyte charset, |
477 | * interface can't trust regular string functions. Due to mbstring overloading design |
478 | * limits php scripts can't control this setting. |
479 | * |
480 | * This hack should fix some issues related to 8bit strings in passwords. Correct fix is |
481 | * to disable mbstring overloading. Japanese translation uses different internal encoding. |
482 | */ |
483 | if ($squirrelmail_language != 'ja_JP' && |
484 | function_exists('mb_internal_encoding') && |
485 | check_php_version(4,2,0) && |
486 | (int)ini_get('mbstring.func_overload')!=0) { |
487 | mb_internal_encoding('pass'); |
488 | } |
489 | } |
490 | return 0; |
491 | } |
492 | |
493 | /** |
494 | * Sets default_charset variable according to the one that is used by user's translations. |
495 | * |
496 | * Function changes global $default_charset variable in order to be sure, that it |
497 | * contains charset used by user's translation. Sanity of $squirrelmail_language |
498 | * and $default_charset combination is also tested. |
499 | * |
500 | * There can be a $default_charset setting in the |
501 | * config.php file, but the user may have a different language |
502 | * selected for a user interface. This function checks the |
503 | * language selected by the user and tags the outgoing messages |
504 | * with the appropriate charset corresponding to the language |
505 | * selection. This is "more right" (tm), than just stamping the |
506 | * message blindly with the system-wide $default_charset. |
507 | */ |
508 | function set_my_charset(){ |
509 | global $data_dir, $username, $default_charset, $languages, $squirrelmail_language; |
510 | |
511 | $my_language = getPref($data_dir, $username, 'language'); |
512 | if (!$my_language) { |
513 | $my_language = $squirrelmail_language ; |
514 | } |
515 | // Catch removed translation |
516 | if (!isset($languages[$my_language])) { |
517 | $my_language="en_US"; |
518 | } |
519 | while (isset($languages[$my_language]['ALIAS'])) { |
520 | $my_language = $languages[$my_language]['ALIAS']; |
521 | } |
522 | $my_charset = $languages[$my_language]['CHARSET']; |
523 | if ($my_language!='en_US') { |
524 | $default_charset = $my_charset; |
525 | } |
526 | } |
527 | |
528 | /** |
529 | * Replaces non-braking spaces inserted by some browsers with regular space |
530 | * |
531 | * This function can be used to replace non-braking space symbols |
532 | * that are inserted in forms by some browsers instead of normal |
533 | * space symbol. |
534 | * |
535 | * @param string $string Text that needs to be cleaned |
536 | * @param string $charset Charset used in text |
537 | * @return string Cleaned text |
538 | */ |
539 | function cleanup_nbsp($string,$charset) { |
540 | |
541 | // reduce number of case statements |
542 | if (stristr('iso-8859-',substr($charset,0,9))){ |
543 | $output_charset="iso-8859-x"; |
544 | } |
545 | if (stristr('windows-125',substr($charset,0,11))){ |
546 | $output_charset="cp125x"; |
547 | } |
548 | if (stristr('koi8',substr($charset,0,4))){ |
549 | $output_charset="koi8-x"; |
550 | } |
551 | if (! isset($output_charset)){ |
552 | $output_charset=strtolower($charset); |
553 | } |
554 | |
555 | // where is non-braking space symbol |
556 | switch($output_charset): |
557 | case "iso-8859-x": |
558 | case "cp125x": |
559 | case "iso-2022-jp": |
560 | $nbsp="\xA0"; |
561 | break; |
562 | case "koi8-x": |
563 | $nbsp="\x9A"; |
564 | break; |
565 | case "utf-8": |
566 | $nbsp="\xC2\xA0"; |
567 | break; |
568 | default: |
569 | // don't change string if charset is unmatched |
570 | return $string; |
571 | endswitch; |
572 | |
573 | // return space instead of non-braking space. |
574 | return str_replace($nbsp,' ',$string); |
575 | } |
576 | |
577 | /** |
578 | * Function informs if it is safe to convert given charset to the one that is used by user. |
579 | * |
580 | * It is safe to use conversion only if user uses utf-8 encoding and when |
581 | * converted charset is similar to the one that is used by user. |
582 | * |
583 | * @param string $input_charset Charset of text that needs to be converted |
584 | * @return bool is it possible to convert to user's charset |
585 | */ |
586 | function is_conversion_safe($input_charset) { |
587 | global $languages, $sm_notAlias, $default_charset, $lossy_encoding; |
588 | |
589 | if (isset($lossy_encoding) && $lossy_encoding ) |
590 | return true; |
591 | |
592 | // convert to lower case |
593 | $input_charset = strtolower($input_charset); |
594 | |
595 | // Is user's locale Unicode based ? |
596 | if ( $default_charset == "utf-8" ) { |
597 | return true; |
598 | } |
599 | |
600 | // Charsets that are similar |
601 | switch ($default_charset) { |
602 | case "windows-1251": |
603 | if ( $input_charset == "iso-8859-5" || |
604 | $input_charset == "koi8-r" || |
605 | $input_charset == "koi8-u" ) { |
606 | return true; |
607 | } else { |
608 | return false; |
609 | } |
610 | case "windows-1257": |
611 | if ( $input_charset == "iso-8859-13" || |
612 | $input_charset == "iso-8859-4" ) { |
613 | return true; |
614 | } else { |
615 | return false; |
616 | } |
617 | case "iso-8859-4": |
618 | if ( $input_charset == "iso-8859-13" || |
619 | $input_charset == "windows-1257" ) { |
620 | return true; |
621 | } else { |
622 | return false; |
623 | } |
624 | case "iso-8859-5": |
625 | if ( $input_charset == "windows-1251" || |
626 | $input_charset == "koi8-r" || |
627 | $input_charset == "koi8-u" ) { |
628 | return true; |
629 | } else { |
630 | return false; |
631 | } |
632 | case "iso-8859-13": |
633 | if ( $input_charset == "iso-8859-4" || |
634 | $input_charset == "windows-1257" ) { |
635 | return true; |
636 | } else { |
637 | return false; |
638 | } |
639 | case "koi8-r": |
640 | if ( $input_charset == "windows-1251" || |
641 | $input_charset == "iso-8859-5" || |
642 | $input_charset == "koi8-u" ) { |
643 | return true; |
644 | } else { |
645 | return false; |
646 | } |
647 | case "koi8-u": |
648 | if ( $input_charset == "windows-1251" || |
649 | $input_charset == "iso-8859-5" || |
650 | $input_charset == "koi8-r" ) { |
651 | return true; |
652 | } else { |
653 | return false; |
654 | } |
655 | default: |
656 | return false; |
657 | } |
658 | } |
659 | |
660 | /** |
661 | * Converts html character entities to numeric entities |
662 | * |
663 | * SquirrelMail encoding functions work only with numeric entities. |
664 | * This function fixes issues with decoding functions that might convert |
665 | * some symbols to character entities. Issue is specific to PHP recode |
666 | * extension decoding. Function is used internally in charset_convert() |
667 | * function. |
668 | * @param string $str string that might contain html character entities |
669 | * @return string string with character entities converted to decimals. |
670 | * @since 1.5.2 |
671 | */ |
672 | function sqi18n_convert_entities($str) { |
673 | |
674 | $entities = array( |
675 | // Latin 1 |
676 | ' ' => ' ', |
677 | '¡' => '¡', |
678 | '¢' => '¢', |
679 | '£' => '£', |
680 | '¤' => '¤', |
681 | '¥' => '¥', |
682 | '¦' => '¦', |
683 | '§' => '§', |
684 | '¨' => '¨', |
685 | '©' => '©', |
686 | 'ª' => 'ª', |
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 | // Latin Extended-A |
773 | 'Œ' => 'Œ', |
774 | 'œ' => 'œ', |
775 | 'Š' => 'Š', |
776 | 'š' => 'š', |
777 | 'Ÿ' => 'Ÿ', |
778 | // Spacing Modifier Letters |
779 | 'ˆ' => 'ˆ', |
780 | '˜' => '˜', |
781 | // General Punctuation |
782 | ' ' => ' ', |
783 | ' ' => ' ', |
784 | ' ' => ' ', |
785 | '‌' => '‌', |
786 | '‍' => '‍', |
787 | '‎' => '‎', |
788 | '‏' => '‏', |
789 | '–' => '–', |
790 | '—' => '—', |
791 | '‘' => '‘', |
792 | '’' => '’', |
793 | '‚' => '‚', |
794 | '“' => '“', |
795 | '”' => '”', |
796 | '„' => '„', |
797 | '†' => '†', |
798 | '‡' => '‡', |
799 | '‰' => '‰', |
800 | '‹' => '‹', |
801 | '›' => '›', |
802 | '€' => '€', |
803 | // Latin Extended-B |
804 | 'ƒ' => 'ƒ', |
805 | // Greek |
806 | 'Α' => 'Α', |
807 | 'Β' => 'Β', |
808 | 'Γ' => 'Γ', |
809 | 'Δ' => 'Δ', |
810 | 'Ε' => 'Ε', |
811 | 'Ζ' => 'Ζ', |
812 | 'Η' => 'Η', |
813 | 'Θ' => 'Θ', |
814 | 'Ι' => 'Ι', |
815 | 'Κ' => 'Κ', |
816 | 'Λ' => 'Λ', |
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 | // General Punctuation |
859 | '•' => '•', |
860 | '…' => '…', |
861 | '′' => '′', |
862 | '″' => '″', |
863 | '‾' => '‾', |
864 | '⁄' => '⁄', |
865 | // Letterlike Symbols |
866 | '℘' => '℘', |
867 | 'ℑ' => 'ℑ', |
868 | 'ℜ' => 'ℜ', |
869 | '™' => '™', |
870 | 'ℵ' => 'ℵ', |
871 | // Arrows |
872 | '←' => '←', |
873 | '↑' => '↑', |
874 | '→' => '→', |
875 | '↓' => '↓', |
876 | '↔' => '↔', |
877 | '↵' => '↵', |
878 | '⇐' => '⇐', |
879 | '⇑' => '⇑', |
880 | '⇒' => '⇒', |
881 | '⇓' => '⇓', |
882 | '⇔' => '⇔', |
883 | // Mathematical Operators |
884 | '∀' => '∀', |
885 | '∂' => '∂', |
886 | '∃' => '∃', |
887 | '∅' => '∅', |
888 | '∇' => '∇', |
889 | '∈' => '∈', |
890 | '∉' => '∉', |
891 | '∋' => '∋', |
892 | '∏' => '∏', |
893 | '∑' => '∑', |
894 | '−' => '−', |
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 | // Miscellaneous Technical |
923 | '⌈' => '⌈', |
924 | '⌉' => '⌉', |
925 | '⌊' => '⌊', |
926 | '⌋' => '⌋', |
927 | '⟨' => '〈', |
928 | '⟩' => '〉', |
929 | // Geometric Shapes |
930 | '◊' => '◊', |
931 | // Miscellaneous Symbols |
932 | '♠' => '♠', |
933 | '♣' => '♣', |
934 | '♥' => '♥', |
935 | '♦' => '♦'); |
936 | |
937 | $str = str_replace(array_keys($entities), array_values($entities), $str); |
938 | |
939 | return $str; |
940 | } |
941 | |
942 | /* ------------------------------ main --------------------------- */ |
943 | |
944 | global $squirrelmail_language, $languages, $use_gettext; |
945 | |
946 | if (! sqgetGlobalVar('squirrelmail_language',$squirrelmail_language,SQ_COOKIE)) { |
947 | $squirrelmail_language = ''; |
948 | } |
949 | |
950 | /** |
951 | * Array specifies the available translations. |
952 | * |
953 | * Structure of array: |
954 | * $languages['language']['variable'] = 'value' |
955 | * |
956 | * Possible 'variable' names: |
957 | * NAME - Translation name in English |
958 | * CHARSET - Encoding used by translation |
959 | * ALIAS - used when 'language' is only short name and 'value' should provide long language name |
960 | * ALTNAME - Native translation name. Any 8bit symbols must be html encoded. |
961 | * 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 |
962 | * DIR - Text direction. Used to define Right-to-Left languages. Possible values 'rtl' or 'ltr'. If undefined - defaults to 'ltr' |
963 | * XTRA_CODE - translation uses special functions. See doc/i18n.txt |
964 | * |
965 | * Each 'language' definition requires NAME+CHARSET or ALIAS variables. |
966 | * |
967 | * @name $languages |
968 | * @global array $languages |
969 | */ |
970 | $languages['en_US']['NAME'] = 'English'; |
971 | $languages['en_US']['CHARSET'] = 'iso-8859-1'; |
972 | $languages['en_US']['LOCALE'] = 'en_US.ISO8859-1'; |
973 | $languages['en']['ALIAS'] = 'en_US'; |
974 | |
975 | /** |
976 | * Automatic translation loading from setup.php files. |
977 | * Solution for bug. 1240889. |
978 | * setup.php file can contain $languages array entries and XTRA_CODE functions. |
979 | */ |
980 | if (is_dir(SM_PATH . 'locale') && |
981 | is_readable(SM_PATH . 'locale')) { |
982 | $localedir = dir(SM_PATH . 'locale'); |
983 | while($lang_dir=$localedir->read()) { |
984 | // remove trailing slash, if present |
985 | if (substr($lang_dir,-1)=='/') { |
986 | $lang_dir = substr($lang_dir,0,-1); |
987 | } |
988 | if ($lang_dir != '..' && $lang_dir != '.' && $lang_dir != 'CVS' && |
989 | is_dir(SM_PATH.'locale/'.$lang_dir) && |
990 | file_exists(SM_PATH.'locale/'.$lang_dir.'/setup.php')) { |
991 | include_once(SM_PATH.'locale/'.$lang_dir.'/setup.php'); |
992 | } |
993 | } |
994 | $localedir->close(); |
995 | } |
996 | |
997 | /* Detect whether gettext is installed. */ |
998 | $gettext_flags = 0; |
999 | if (function_exists('_')) { |
1000 | $gettext_flags += 1; |
1001 | } |
1002 | if (function_exists('bindtextdomain')) { |
1003 | $gettext_flags += 2; |
1004 | } |
1005 | if (function_exists('textdomain')) { |
1006 | $gettext_flags += 4; |
1007 | } |
1008 | if (function_exists('ngettext')) { |
1009 | $gettext_flags += 8; |
1010 | } |
1011 | |
1012 | /* If gettext is fully loaded, cool */ |
1013 | if ($gettext_flags == 15) { |
1014 | $use_gettext = true; |
1015 | } |
1016 | |
1017 | /* If ngettext support is missing, load it */ |
1018 | elseif ($gettext_flags == 7) { |
1019 | $use_gettext = true; |
1020 | // load internal ngettext functions |
1021 | include_once(SM_PATH . 'class/l10n.class.php'); |
1022 | include_once(SM_PATH . 'functions/ngettext.php'); |
1023 | } |
1024 | |
1025 | /* If we can fake gettext, try that */ |
1026 | elseif ($gettext_flags == 0) { |
1027 | $use_gettext = true; |
1028 | include_once(SM_PATH . 'functions/gettext.php'); |
1029 | } else { |
1030 | /* Uh-ho. A weird install */ |
1031 | if (! $gettext_flags & 1) { |
1032 | /** |
1033 | * Function is used as replacement in broken installs |
1034 | * @ignore |
1035 | */ |
1036 | function _($str) { |
1037 | return $str; |
1038 | } |
1039 | } |
1040 | if (! $gettext_flags & 2) { |
1041 | /** |
1042 | * Function is used as replacement in broken installs |
1043 | * @ignore |
1044 | */ |
1045 | function bindtextdomain() { |
1046 | return; |
1047 | } |
1048 | } |
1049 | if (! $gettext_flags & 4) { |
1050 | /** |
1051 | * Function is used as replacemet in broken installs |
1052 | * @ignore |
1053 | */ |
1054 | function textdomain() { |
1055 | return; |
1056 | } |
1057 | } |
1058 | if (! $gettext_flags & 8) { |
1059 | /** |
1060 | * Function is used as replacemet in broken installs |
1061 | * @ignore |
1062 | */ |
1063 | function ngettext($str,$str2,$number) { |
1064 | if ($number>1) { |
1065 | return $str2; |
1066 | } else { |
1067 | return $str; |
1068 | } |
1069 | } |
1070 | } |
1071 | if (! function_exists('dgettext')) { |
1072 | /** |
1073 | * Replacement for broken setups. |
1074 | * @ignore |
1075 | */ |
1076 | function dgettext($domain,$str) { |
1077 | return $str; |
1078 | } |
1079 | } |
1080 | if (! function_exists('dngettext')) { |
1081 | /** |
1082 | * Replacement for broken setups |
1083 | * @ignore |
1084 | */ |
1085 | function dngettext($domain,$str1,$strn,$number) { |
1086 | return ($number==1 ? $str1 : $strn); |
1087 | } |
1088 | } |
1089 | } |
1090 | ?> |