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