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