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