- Improve recovery when EHLO not supported on legacy SMTP servers
[squirrelmail.git] / functions / i18n.php
CommitLineData
59177427 1<?php
4b4abf93 2
35586184 3/**
d3bab52e 4 * SquirrelMail internationalization functions
35586184 5 *
35586184 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 *
47ccfad4 12 * @copyright &copy; 1999-2006 The SquirrelMail Project Team
4b4abf93 13 * @license http://opensource.org/licenses/gpl-license.php GNU Public License
a8a1c36d 14 * @version $Id$
d6c32258 15 * @package squirrelmail
a8a1c36d 16 * @subpackage i18n
35586184 17 */
18
c6cd3136 19/** @ignore */
20if (! defined('SM_PATH')) define('SM_PATH','../');
21
d6c32258 22/** Everything uses global.php... */
961ca3d8 23require_once(SM_PATH . 'functions/global.php');
24
d6c32258 25/**
3b84e1b1 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.
81be3f39 31 * @since 1.5.1
3b84e1b1 32 * @param string $domain gettext domain name
33 * @param string $dir directory that contains all translations
34 * @return string path to translation directory
35 */
36function 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.
81be3f39 55 * @since 1.5.1
3b84e1b1 56 * @param string $name gettext domain name
57 * @return string gettext domain name
58 */
59function sq_textdomain($domain) {
60 global $gettext_domain;
61 $gettext_domain=textdomain($domain);
62 return $gettext_domain;
63}
64
65/**
1989cc04 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 *
598294a7 72 * @param int $category locale category name. Use php named constants
73 * (LC_ALL, LC_COLLATE, LC_CTYPE, LC_MONETARY, LC_NUMERIC, LC_TIME)
1989cc04 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.
480feeac 76 * @since 1.5.1 and 1.4.5
1989cc04 77 * @see http://www.php.net/setlocale
78 */
79function sq_setlocale($category,$locale) {
80 // string with only one locale
81 if (is_string($locale))
82 return setlocale($category,$locale);
83
84 if (! check_php_version(4,3)) {
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 }
95 return $ret;
96}
97
98/**
51468260 99 * Converts string from given charset to charset, that can be displayed by user translation.
100 *
101 * Function by default returns html encoded strings, if translation uses different encoding.
102 * If Japanese translation is used - function returns string converted to euc-jp
103 * If iconv or recode functions are enabled and translation uses utf-8 - function returns utf-8 encoded string.
104 * If $charset is not supported - function returns unconverted string.
62f7daa5 105 *
51468260 106 * sanitizing of html tags is also done by this function.
107 *
d6c32258 108 * @param string $charset
109 * @param string $string Text to be decoded
f8a1ed5a 110 * @param boolean $force_decode converts string to html without $charset!=$default_charset check.
480feeac 111 * Argument is available since 1.5.1 and 1.4.5.
b6c52e61 112 * @param boolean $save_html disables htmlspecialchars() in order to preserve
113 * html formating. Use with care. Available since 1.5.1
51468260 114 * @return string decoded string
d6c32258 115 */
b6c52e61 116function charset_decode ($charset, $string, $force_decode=false, $save_html=false) {
3ec81e63 117 global $languages, $squirrelmail_language, $default_charset;
f03f6ee7 118 global $use_php_recode, $use_php_iconv, $aggressive_decoding;
a2a7852b 119
3714db45 120 if (isset($languages[$squirrelmail_language]['XTRA_CODE']) &&
1bb86586 121 function_exists($languages[$squirrelmail_language]['XTRA_CODE'] . '_decode')) {
122 $string = call_user_func($languages[$squirrelmail_language]['XTRA_CODE'] . '_decode', $string);
6fbd125b 123 }
b05c8961 124
3ec81e63 125 $charset = strtolower($charset);
126
127 set_my_charset();
128
129 // Variables that allow to use functions without function_exist() calls
edf2c0ba 130 if (! isset($use_php_recode) || $use_php_recode=="" ) {
3b84e1b1 131 $use_php_recode=false; }
edf2c0ba 132 if (! isset($use_php_iconv) || $use_php_iconv=="" ) {
3b84e1b1 133 $use_php_iconv=false; }
3ec81e63 134
135 // Don't do conversion if charset is the same.
5341ce66 136 if ( ! $force_decode && $charset == strtolower($default_charset) )
4a46b538 137 return ($save_html ? $string : htmlspecialchars($string));
3ec81e63 138
139 // catch iso-8859-8-i thing
140 if ( $charset == "iso-8859-8-i" )
3b84e1b1 141 $charset = "iso-8859-8";
3ec81e63 142
143 /*
62f7daa5 144 * Recode converts html special characters automatically if you use
145 * 'charset..html' decoding. There is no documented way to put -d option
3ec81e63 146 * into php recode function call.
147 */
148 if ( $use_php_recode ) {
3b84e1b1 149 if ( $default_charset == "utf-8" ) {
150 // other charsets can be converted to utf-8 without loss.
151 // and output string is smaller
152 $string = recode_string($charset . "..utf-8",$string);
4a46b538 153 return ($save_html ? $string : htmlspecialchars($string));
3b84e1b1 154 } else {
155 $string = recode_string($charset . "..html",$string);
156 // recode does not convert single quote, htmlspecialchars does.
157 $string = str_replace("'", '&#039;', $string);
b6c52e61 158 // undo html specialchars
159 if ($save_html)
160 $string=str_replace(array('&amp;','&quot;','&lt;','&gt;'),
161 array('&','"','<','>'),$string);
3b84e1b1 162 return $string;
163 }
3ec81e63 164 }
165
166 // iconv functions does not have html target and can be used only with utf-8
167 if ( $use_php_iconv && $default_charset=='utf-8') {
3b84e1b1 168 $string = iconv($charset,$default_charset,$string);
4a46b538 169 return ($save_html ? $string : htmlspecialchars($string));
3ec81e63 170 }
171
172 // If we don't use recode and iconv, we'll do it old way.
173
a2a7852b 174 /* All HTML special characters are 7 bit and can be replaced first */
b6c52e61 175 if (! $save_html) $string = htmlspecialchars ($string);
a2a7852b 176
5dd23dac 177 /* controls cpu and memory intensive decoding cycles */
f03f6ee7 178 if (! isset($aggressive_decoding) || $aggressive_decoding=="" ) {
3b84e1b1 179 $aggressive_decoding=false; }
5dd23dac 180
b142de74 181 $decode=fixcharset($charset);
182 $decodefile=SM_PATH . 'functions/decode/' . $decode . '.php';
183 if (file_exists($decodefile)) {
3b84e1b1 184 include_once($decodefile);
b6c52e61 185 // send $save_html argument to decoding function. needed for iso-2022-xx decoding.
186 $ret = call_user_func('charset_decode_'.$decode, $string, $save_html);
a2a7852b 187 } else {
3b84e1b1 188 $ret = $string;
a2a7852b 189 }
190 return( $ret );
191}
03db90bc 192
b142de74 193/**
d3bab52e 194 * Converts html string to given charset
480feeac 195 * @since 1.5.1 and 1.4.4
d3bab52e 196 * @param string $string
197 * @param string $charset
78be8403 198 * @param boolean $htmlencode keep htmlspecialchars encoding
62f7daa5 199 * @param string
d3bab52e 200 */
78be8403 201function charset_encode($string,$charset,$htmlencode=true) {
3b84e1b1 202 global $default_charset;
203
3b84e1b1 204 $encode=fixcharset($charset);
205 $encodefile=SM_PATH . 'functions/encode/' . $encode . '.php';
206 if (file_exists($encodefile)) {
207 include_once($encodefile);
208 $ret = call_user_func('charset_encode_'.$encode, $string);
9280dd9d 209 } elseif(file_exists(SM_PATH . 'functions/encode/us_ascii.php')) {
210 // function replaces all 8bit html entities with question marks.
211 // it is used when other encoding functions are unavailable
3b84e1b1 212 include_once(SM_PATH . 'functions/encode/us_ascii.php');
213 $ret = charset_encode_us_ascii($string);
9280dd9d 214 } else {
215 /**
216 * fix for yahoo users that remove all us-ascii related things
217 */
218 $ret = $string;
3b84e1b1 219 }
039fee57 220
221 /**
222 * Undo html special chars, some places (like compose form) have
223 * own sanitizing functions and don't need html symbols.
224 * Undo chars only after encoding in order to prevent conversion of
225 * html entities in plain text emails.
226 */
227 if (! $htmlencode ) {
228 $ret = str_replace(array('&amp;','&gt;','&lt;','&quot;'),array('&','>','<','"'),$ret);
229 }
3b84e1b1 230 return( $ret );
d3bab52e 231}
232
233/**
234 * Combined decoding and encoding functions
235 *
236 * If conversion is done to charset different that utf-8, unsupported symbols
237 * will be replaced with question marks.
480feeac 238 * @since 1.5.1 and 1.4.4
d3bab52e 239 * @param string $in_charset initial charset
240 * @param string $string string that has to be converted
241 * @param string $out_charset final charset
78be8403 242 * @param boolean $htmlencode keep htmlspecialchars encoding
d3bab52e 243 * @return string converted string
244 */
78be8403 245function charset_convert($in_charset,$string,$out_charset,$htmlencode=true) {
5341ce66 246 $string=charset_decode($in_charset,$string,true);
56499061 247 $string=sqi18n_convert_entities($string);
33991968 248 $string=charset_encode($string,$out_charset,$htmlencode);
249 return $string;
d3bab52e 250}
251
252/**
b142de74 253 * Makes charset name suitable for decoding cycles
254 *
480feeac 255 * @since 1.5.0 and 1.4.4
b142de74 256 * @param string $charset Name of charset
257 * @return string $charset Adjusted name of charset
258 */
259function fixcharset($charset) {
91e0dccc 260 /* remove minus and characters that might be used in paths from charset
dbebf058 261 * name in order to be able to use it in function names and include calls.
262 */
263 $charset=preg_replace("/[-:.\/\\\]/",'_',$charset);
62f7daa5 264
c5c521a9 265 // OE ks_c_5601_1987 > cp949
266 $charset=str_replace('ks_c_5601_1987','cp949',$charset);
267 // Moz x-euc-tw > euc-tw
268 $charset=str_replace('x_euc','euc',$charset);
269 // Moz x-windows-949 > cp949
270 $charset=str_replace('x_windows_','cp',$charset);
271
b142de74 272 // windows-125x and cp125x charsets
273 $charset=str_replace('windows_','cp',$charset);
a2a7852b 274
b142de74 275 // ibm > cp
276 $charset=str_replace('ibm','cp',$charset);
277
278 // iso-8859-8-i -> iso-8859-8
279 // use same cycle until I'll find differences
280 $charset=str_replace('iso_8859_8_i','iso_8859_8',$charset);
281
282 return $charset;
283}
a2a7852b 284
51468260 285/**
a2a7852b 286 * Set up the language to be output
287 * if $do_search is true, then scan the browser information
288 * for a possible language that we know
51468260 289 *
62f7daa5 290 * Function sets system locale environment (LC_ALL, LANG, LANGUAGE),
51468260 291 * gettext translation bindings and html header information.
292 *
5679405c 293 * Function returns error codes, if there is some fatal error.
62f7daa5 294 * 0 = no error,
295 * 1 = mbstring support is not present,
51468260 296 * 2 = mbstring support is not present, user's translation reverted to en_US.
297 *
298 * @param string $sm_language translation used by user's interface
299 * @param bool $do_search use browser's preferred language detection functions. Defaults to false.
300 * @param bool $default set $sm_language to $squirrelmail_default_language if language detection fails or language is not set. Defaults to false.
62f7daa5 301 * @return int function execution error codes.
a2a7852b 302 */
67a8c90a 303function set_up_language($sm_language, $do_search = false, $default = false) {
a2a7852b 304
305 static $SetupAlready = 0;
9eb0fbd4 306 global $use_gettext, $languages,
fe48c808 307 $squirrelmail_language, $squirrelmail_default_language, $default_charset,
51468260 308 $sm_notAlias, $username, $data_dir;
a2a7852b 309
310 if ($SetupAlready) {
311 return;
312 }
a65846a7 313
5c920668 314 $SetupAlready = TRUE;
961ca3d8 315 sqgetGlobalVar('HTTP_ACCEPT_LANGUAGE', $accept_lang, SQ_SERVER);
a2a7852b 316
0d7a48b0 317 /**
318 * If function is asked to detect preferred language
319 * OR squirrelmail default language is set to empty string
320 * AND
f8a1ed5a 321 * squirrelmail language ($sm_language) is empty string
0d7a48b0 322 * (not set in user's prefs and no cookie with language info)
323 * AND
324 * browser provides list of preferred languages
325 * THEN
326 * get preferred language from HTTP_ACCEPT_LANGUAGE header
327 */
f8a1ed5a 328 if (($do_search || empty($squirrelmail_default_language)) &&
329 ! $sm_language &&
0d7a48b0 330 isset($accept_lang)) {
331 // TODO: use more than one language, if first language is not available
332 // FIXME: function assumes that string contains two or more characters.
333 // FIXME: some languages use 5 chars
961ca3d8 334 $sm_language = substr($accept_lang, 0, 2);
a2a7852b 335 }
62f7daa5 336
0d7a48b0 337 /**
338 * If language preference is not set OR script asks to use default language
339 * AND
340 * default squirrelmail language is not set to empty string
341 * THEN
342 * use default squirrelmail language value from configuration.
343 */
f8a1ed5a 344 if ((!$sm_language||$default) &&
0d7a48b0 345 ! empty($squirrelmail_default_language)) {
a2a7852b 346 $squirrelmail_language = $squirrelmail_default_language;
66d7950f 347 $sm_language = $squirrelmail_default_language;
a2a7852b 348 }
7be4d717 349
350 /** provide failsafe language when detection fails */
351 if (! $sm_language) $sm_language='en_US';
352
a2a7852b 353 $sm_notAlias = $sm_language;
62f7daa5 354
3ec81e63 355 // Catching removed translation
356 // System reverts to English translation if user prefs contain translation
2ba706ef 357 // that is not available in $languages array
3ec81e63 358 if (!isset($languages[$sm_notAlias])) {
3b84e1b1 359 $sm_notAlias="en_US";
3ec81e63 360 }
361
a2a7852b 362 while (isset($languages[$sm_notAlias]['ALIAS'])) {
363 $sm_notAlias = $languages[$sm_notAlias]['ALIAS'];
364 }
365
88cb1b4d 366 if ( isset($sm_language) &&
5c920668 367 $use_gettext &&
368 $sm_language != '' &&
369 isset($languages[$sm_notAlias]['CHARSET']) ) {
3b84e1b1 370 sq_bindtextdomain( 'squirrelmail', SM_PATH . 'locale/' );
371 sq_textdomain( 'squirrelmail' );
1989cc04 372
373 // set codeset in order to avoid gettext charset conversions
03db90bc 374 if (function_exists('bind_textdomain_codeset')) {
1989cc04 375 // Japanese translation uses different internal charset
3b84e1b1 376 if ($sm_notAlias == 'ja_JP') {
598294a7 377 bind_textdomain_codeset ('squirrelmail', 'EUC-JP');
a5970d71 378 } else {
598294a7 379 bind_textdomain_codeset ('squirrelmail', $languages[$sm_notAlias]['CHARSET'] );
03db90bc 380 }
381 }
1989cc04 382
383 // Use LOCALE key, if it is set.
03db90bc 384 if (isset($languages[$sm_notAlias]['LOCALE'])){
3b84e1b1 385 $longlocale=$languages[$sm_notAlias]['LOCALE'];
03db90bc 386 } else {
3b84e1b1 387 $longlocale=$sm_notAlias;
03db90bc 388 }
1989cc04 389
390 // try setting locale
391 $retlocale=sq_setlocale(LC_ALL, $longlocale);
392
393 // check if locale is set and assign that locale to $longlocale
394 // in order to use it in putenv calls.
395 if (! is_bool($retlocale)) {
396 $longlocale=$retlocale;
397 } elseif (is_array($longlocale)) {
398 // setting of all locales failed.
399 // we need string instead of array used in LOCALE key.
400 $longlocale=$sm_notAlias;
401 }
402
5a4689fb 403 if ( !((bool)ini_get('safe_mode')) &&
f2374580 404 getenv( 'LC_ALL' ) != $longlocale ) {
405 putenv( "LC_ALL=$longlocale" );
406 putenv( "LANG=$longlocale" );
407 putenv( "LANGUAGE=$longlocale" );
54b168cf 408 putenv( "LC_NUMERIC=C" );
475df436 409 if ($sm_notAlias=='tr_TR') putenv( "LC_CTYPE=C" );
a2a7852b 410 }
54b168cf 411 // Workaround for plugins that use numbers with floating point
91e0dccc 412 // It might be removed if plugins use correct decimal delimiters
413 // according to locale settings.
54b168cf 414 setlocale(LC_NUMERIC, 'C');
415 // Workaround for specific Turkish strtolower/strtoupper rules.
416 // Many functions expect English conversion rules.
475df436 417 if ($sm_notAlias=='tr_TR') setlocale(LC_CTYPE,'C');
03db90bc 418
f549a706 419 /**
420 * Set text direction/alignment variables
421 * When language environment is setup, scripts can use these globals
422 * without accessing $languages directly and making checks for optional
423 * array key.
424 */
425 global $text_direction, $left_align, $right_align;
62f7daa5 426 if (isset($languages[$sm_notAlias]['DIR']) &&
03db90bc 427 $languages[$sm_notAlias]['DIR'] == 'rtl') {
3b84e1b1 428 /**
429 * Text direction
430 * @global string $text_direction
431 */
432 $text_direction='rtl';
433 /**
434 * Left alignment
435 * @global string $left_align
436 */
437 $left_align='right';
438 /**
439 * Right alignment
440 * @global string $right_align
441 */
442 $right_align='left';
03db90bc 443 } else {
3b84e1b1 444 $text_direction='ltr';
445 $left_align='left';
446 $right_align='right';
03db90bc 447 }
448
449 $squirrelmail_language = $sm_notAlias;
a5970d71 450 if ($squirrelmail_language == 'ja_JP') {
b05c8961 451 header ('Content-Type: text/html; charset=EUC-JP');
452 if (!function_exists('mb_internal_encoding')) {
3b84e1b1 453 // Error messages can't be displayed here
454 $error = 1;
455 // Revert to English if possible.
456 if (function_exists('setPref') && $username!='' && $data_dir!="") {
457 setPref($data_dir, $username, 'language', "en_US");
458 $error = 2;
459 }
460 // stop further execution in order not to get php errors on mb_internal_encoding().
461 return $error;
e842b215 462 }
463 if (function_exists('mb_language')) {
464 mb_language('Japanese');
b05c8961 465 }
466 mb_internal_encoding('EUC-JP');
467 mb_http_output('pass');
fe48c808 468 } elseif ($squirrelmail_language == 'en_US') {
469 header( 'Content-Type: text/html; charset=' . $default_charset );
b05c8961 470 } else {
fe48c808 471 header( 'Content-Type: text/html; charset=' . $languages[$sm_notAlias]['CHARSET'] );
472 }
f5c507dc 473 /**
474 * mbstring.func_overload fix (#929644).
475 *
f8a1ed5a 476 * php mbstring extension can replace standard string functions with their multibyte
4c12c858 477 * equivalents. See http://www.php.net/ref.mbstring#mbstring.overload. This feature
478 * was added in php v.4.2.0
f5c507dc 479 *
480 * Some SquirrelMail functions work with 8bit strings in bytes. If interface is forced
481 * to use mbstring functions and mbstring internal encoding is set to multibyte charset,
f8a1ed5a 482 * interface can't trust regular string functions. Due to mbstring overloading design
f5c507dc 483 * limits php scripts can't control this setting.
484 *
485 * This hack should fix some issues related to 8bit strings in passwords. Correct fix is
486 * to disable mbstring overloading. Japanese translation uses different internal encoding.
487 */
f8a1ed5a 488 if ($squirrelmail_language != 'ja_JP' &&
f5c507dc 489 function_exists('mb_internal_encoding') &&
490 check_php_version(4,2,0) &&
491 (int)ini_get('mbstring.func_overload')!=0) {
fa4cfb07 492 mb_internal_encoding('pass');
f5c507dc 493 }
a2a7852b 494 }
51468260 495 return 0;
b05c8961 496}
a2a7852b 497
51468260 498/**
499 * Sets default_charset variable according to the one that is used by user's translations.
500 *
501 * Function changes global $default_charset variable in order to be sure, that it
0d7a48b0 502 * contains charset used by user's translation. Sanity of $squirrelmail_language
503 * and $default_charset combination is also tested.
51468260 504 *
505 * There can be a $default_charset setting in the
506 * config.php file, but the user may have a different language
507 * selected for a user interface. This function checks the
508 * language selected by the user and tags the outgoing messages
509 * with the appropriate charset corresponding to the language
510 * selection. This is "more right" (tm), than just stamping the
511 * message blindly with the system-wide $default_charset.
512 */
a2a7852b 513function set_my_charset(){
0d7a48b0 514 global $data_dir, $username, $default_charset, $languages, $squirrelmail_language;
88cb1b4d 515
a2a7852b 516 $my_language = getPref($data_dir, $username, 'language');
5c920668 517 if (!$my_language) {
0d7a48b0 518 $my_language = $squirrelmail_language ;
5c920668 519 }
3ec81e63 520 // Catch removed translation
521 if (!isset($languages[$my_language])) {
3b84e1b1 522 $my_language="en_US";
3ec81e63 523 }
a2a7852b 524 while (isset($languages[$my_language]['ALIAS'])) {
f7e8861e 525 $my_language = $languages[$my_language]['ALIAS'];
a2a7852b 526 }
5c920668 527 $my_charset = $languages[$my_language]['CHARSET'];
fe48c808 528 if ($my_language!='en_US') {
a2a7852b 529 $default_charset = $my_charset;
530 }
531}
532
e0e1b169 533/**
534 * Replaces non-braking spaces inserted by some browsers with regular space
535 *
536 * This function can be used to replace non-braking space symbols
537 * that are inserted in forms by some browsers instead of normal
538 * space symbol.
539 *
540 * @param string $string Text that needs to be cleaned
541 * @param string $charset Charset used in text
542 * @return string Cleaned text
543 */
544function cleanup_nbsp($string,$charset) {
a8fa8e33 545
e0e1b169 546 // reduce number of case statements
547 if (stristr('iso-8859-',substr($charset,0,9))){
548 $output_charset="iso-8859-x";
549 }
550 if (stristr('windows-125',substr($charset,0,11))){
551 $output_charset="cp125x";
552 }
553 if (stristr('koi8',substr($charset,0,4))){
554 $output_charset="koi8-x";
555 }
556 if (! isset($output_charset)){
557 $output_charset=strtolower($charset);
558 }
a8fa8e33 559
e0e1b169 560// where is non-braking space symbol
561switch($output_charset):
562 case "iso-8859-x":
563 case "cp125x":
564 case "iso-2022-jp":
565 $nbsp="\xA0";
566 break;
567 case "koi8-x":
568 $nbsp="\x9A";
569 break;
570 case "utf-8":
571 $nbsp="\xC2\xA0";
572 break;
573 default:
574 // don't change string if charset is unmatched
575 return $string;
576endswitch;
a8fa8e33 577
e0e1b169 578// return space instead of non-braking space.
579 return str_replace($nbsp,' ',$string);
580}
a8fa8e33 581
e0e1b169 582/**
583 * Function informs if it is safe to convert given charset to the one that is used by user.
584 *
585 * It is safe to use conversion only if user uses utf-8 encoding and when
586 * converted charset is similar to the one that is used by user.
587 *
588 * @param string $input_charset Charset of text that needs to be converted
589 * @return bool is it possible to convert to user's charset
590 */
591function is_conversion_safe($input_charset) {
56499061 592 global $languages, $sm_notAlias, $default_charset, $lossy_encoding;
a8fa8e33 593
e0e1b169 594 if (isset($lossy_encoding) && $lossy_encoding )
91e0dccc 595 return true;
a8fa8e33 596
56499061 597 // convert to lower case
598 $input_charset = strtolower($input_charset);
a8fa8e33 599
56499061 600 // Is user's locale Unicode based ?
601 if ( $default_charset == "utf-8" ) {
e0e1b169 602 return true;
56499061 603 }
604
605 // Charsets that are similar
606 switch ($default_charset) {
607 case "windows-1251":
608 if ( $input_charset == "iso-8859-5" ||
609 $input_charset == "koi8-r" ||
610 $input_charset == "koi8-u" ) {
611 return true;
612 } else {
613 return false;
614 }
615 case "windows-1257":
616 if ( $input_charset == "iso-8859-13" ||
617 $input_charset == "iso-8859-4" ) {
618 return true;
619 } else {
620 return false;
621 }
622 case "iso-8859-4":
623 if ( $input_charset == "iso-8859-13" ||
624 $input_charset == "windows-1257" ) {
625 return true;
626 } else {
627 return false;
628 }
629 case "iso-8859-5":
630 if ( $input_charset == "windows-1251" ||
631 $input_charset == "koi8-r" ||
632 $input_charset == "koi8-u" ) {
633 return true;
634 } else {
635 return false;
636 }
637 case "iso-8859-13":
638 if ( $input_charset == "iso-8859-4" ||
639 $input_charset == "windows-1257" ) {
640 return true;
641 } else {
642 return false;
643 }
644 case "koi8-r":
645 if ( $input_charset == "windows-1251" ||
646 $input_charset == "iso-8859-5" ||
647 $input_charset == "koi8-u" ) {
648 return true;
649 } else {
650 return false;
651 }
652 case "koi8-u":
653 if ( $input_charset == "windows-1251" ||
654 $input_charset == "iso-8859-5" ||
655 $input_charset == "koi8-r" ) {
656 return true;
657 } else {
658 return false;
659 }
660 default:
e0e1b169 661 return false;
56499061 662 }
e0e1b169 663}
551a09c7 664
56499061 665/**
666 * Converts html character entities to numeric entities
667 *
668 * SquirrelMail encoding functions work only with numeric entities.
669 * This function fixes issues with decoding functions that might convert
670 * some symbols to character entities. Issue is specific to PHP recode
671 * extension decoding. Function is used internally in charset_convert()
672 * function.
673 * @param string $str string that might contain html character entities
674 * @return string string with character entities converted to decimals.
675 * @since 1.5.2
676 */
677function sqi18n_convert_entities($str) {
678
679 $entities = array(
680 // Latin 1
681 '&nbsp;' => '&#160;',
682 '&iexcl;' => '&#161;',
683 '&cent;' => '&#162;',
684 '&pound;' => '&#163;',
685 '&curren;' => '&#164;',
686 '&yen;' => '&#165;',
687 '&brvbar;' => '&#166;',
688 '&sect;' => '&#167;',
689 '&uml;' => '&#168;',
690 '&copy;' => '&#169;',
691 '&ordf;' => '&#170;',
692 '&laquo;' => '&#171;',
693 '&not;' => '&#172;',
694 '&shy;' => '&#173;',
695 '&reg;' => '&#174;',
696 '&macr;' => '&#175;',
697 '&deg;' => '&#176;',
698 '&plusmn;' => '&#177;',
699 '&sup2;' => '&#178;',
700 '&sup3;' => '&#179;',
701 '&acute;' => '&#180;',
702 '&micro;' => '&#181;',
703 '&para;' => '&#182;',
704 '&middot;' => '&#183;',
705 '&cedil;' => '&#184;',
706 '&sup1;' => '&#185;',
707 '&ordm;' => '&#186;',
708 '&raquo;' => '&#187;',
709 '&frac14;' => '&#188;',
710 '&frac12;' => '&#189;',
711 '&frac34;' => '&#190;',
712 '&iquest;' => '&#191;',
713 '&Agrave;' => '&#192;',
714 '&Aacute;' => '&#193;',
715 '&Acirc;' => '&#194;',
716 '&Atilde;' => '&#195;',
717 '&Auml;' => '&#196;',
718 '&Aring;' => '&#197;',
719 '&AElig;' => '&#198;',
720 '&Ccedil;' => '&#199;',
721 '&Egrave;' => '&#200;',
722 '&Eacute;' => '&#201;',
723 '&Ecirc;' => '&#202;',
724 '&Euml;' => '&#203;',
725 '&Igrave;' => '&#204;',
726 '&Iacute;' => '&#205;',
727 '&Icirc;' => '&#206;',
728 '&Iuml;' => '&#207;',
729 '&ETH;' => '&#208;',
730 '&Ntilde;' => '&#209;',
731 '&Ograve;' => '&#210;',
732 '&Oacute;' => '&#211;',
733 '&Ocirc;' => '&#212;',
734 '&Otilde;' => '&#213;',
735 '&Ouml;' => '&#214;',
736 '&times;' => '&#215;',
737 '&Oslash;' => '&#216;',
738 '&Ugrave;' => '&#217;',
739 '&Uacute;' => '&#218;',
740 '&Ucirc;' => '&#219;',
741 '&Uuml;' => '&#220;',
742 '&Yacute;' => '&#221;',
743 '&THORN;' => '&#222;',
744 '&szlig;' => '&#223;',
745 '&agrave;' => '&#224;',
746 '&aacute;' => '&#225;',
747 '&acirc;' => '&#226;',
748 '&atilde;' => '&#227;',
749 '&auml;' => '&#228;',
750 '&aring;' => '&#229;',
751 '&aelig;' => '&#230;',
752 '&ccedil;' => '&#231;',
753 '&egrave;' => '&#232;',
754 '&eacute;' => '&#233;',
755 '&ecirc;' => '&#234;',
756 '&euml;' => '&#235;',
757 '&igrave;' => '&#236;',
758 '&iacute;' => '&#237;',
759 '&icirc;' => '&#238;',
760 '&iuml;' => '&#239;',
761 '&eth;' => '&#240;',
762 '&ntilde;' => '&#241;',
763 '&ograve;' => '&#242;',
764 '&oacute;' => '&#243;',
765 '&ocirc;' => '&#244;',
766 '&otilde;' => '&#245;',
767 '&ouml;' => '&#246;',
768 '&divide;' => '&#247;',
769 '&oslash;' => '&#248;',
770 '&ugrave;' => '&#249;',
771 '&uacute;' => '&#250;',
772 '&ucirc;' => '&#251;',
773 '&uuml;' => '&#252;',
774 '&yacute;' => '&#253;',
775 '&thorn;' => '&#254;',
776 '&yuml;' => '&#255;',
777 // Latin Extended-A
778 '&OElig;' => '&#338;',
779 '&oelig;' => '&#339;',
780 '&Scaron;' => '&#352;',
781 '&scaron;' => '&#353;',
782 '&Yuml;' => '&#376;',
783 // Spacing Modifier Letters
784 '&circ;' => '&#710;',
785 '&tilde;' => '&#732;',
786 // General Punctuation
787 '&ensp;' => '&#8194;',
788 '&emsp;' => '&#8195;',
789 '&thinsp;' => '&#8201;',
790 '&zwnj;' => '&#8204;',
791 '&zwj;' => '&#8205;',
792 '&lrm;' => '&#8206;',
793 '&rlm;' => '&#8207;',
794 '&ndash;' => '&#8211;',
795 '&mdash;' => '&#8212;',
796 '&lsquo;' => '&#8216;',
797 '&rsquo;' => '&#8217;',
798 '&sbquo;' => '&#8218;',
799 '&ldquo;' => '&#8220;',
800 '&rdquo;' => '&#8221;',
801 '&bdquo;' => '&#8222;',
802 '&dagger;' => '&#8224;',
803 '&Dagger;' => '&#8225;',
804 '&permil;' => '&#8240;',
805 '&lsaquo;' => '&#8249;',
806 '&rsaquo;' => '&#8250;',
807 '&euro;' => '&#8364;',
808 // Latin Extended-B
809 '&fnof;' => '&#402;',
810 // Greek
811 '&Alpha;' => '&#913;',
812 '&Beta;' => '&#914;',
813 '&Gamma;' => '&#915;',
814 '&Delta;' => '&#916;',
815 '&Epsilon;' => '&#917;',
816 '&Zeta;' => '&#918;',
817 '&Eta;' => '&#919;',
818 '&Theta;' => '&#920;',
819 '&Iota;' => '&#921;',
820 '&Kappa;' => '&#922;',
821 '&Lambda;' => '&#923;',
822 '&Mu;' => '&#924;',
823 '&Nu;' => '&#925;',
824 '&Xi;' => '&#926;',
825 '&Omicron;' => '&#927;',
826 '&Pi;' => '&#928;',
827 '&Rho;' => '&#929;',
828 '&Sigma;' => '&#931;',
829 '&Tau;' => '&#932;',
830 '&Upsilon;' => '&#933;',
831 '&Phi;' => '&#934;',
832 '&Chi;' => '&#935;',
833 '&Psi;' => '&#936;',
834 '&Omega;' => '&#937;',
835 '&alpha;' => '&#945;',
836 '&beta;' => '&#946;',
837 '&gamma;' => '&#947;',
838 '&delta;' => '&#948;',
839 '&epsilon;' => '&#949;',
840 '&zeta;' => '&#950;',
841 '&eta;' => '&#951;',
842 '&theta;' => '&#952;',
843 '&iota;' => '&#953;',
844 '&kappa;' => '&#954;',
845 '&lambda;' => '&#955;',
846 '&mu;' => '&#956;',
847 '&nu;' => '&#957;',
848 '&xi;' => '&#958;',
849 '&omicron;' => '&#959;',
850 '&pi;' => '&#960;',
851 '&rho;' => '&#961;',
852 '&sigmaf;' => '&#962;',
853 '&sigma;' => '&#963;',
854 '&tau;' => '&#964;',
855 '&upsilon;' => '&#965;',
856 '&phi;' => '&#966;',
857 '&chi;' => '&#967;',
858 '&psi;' => '&#968;',
859 '&omega;' => '&#969;',
860 '&thetasym;' => '&#977;',
861 '&upsih;' => '&#978;',
862 '&piv;' => '&#982;',
863 // General Punctuation
864 '&bull;' => '&#8226;',
865 '&hellip;' => '&#8230;',
866 '&prime;' => '&#8242;',
867 '&Prime;' => '&#8243;',
868 '&oline;' => '&#8254;',
869 '&frasl;' => '&#8260;',
870 // Letterlike Symbols
871 '&weierp;' => '&#8472;',
872 '&image;' => '&#8465;',
873 '&real;' => '&#8476;',
874 '&trade;' => '&#8482;',
875 '&alefsym;' => '&#8501;',
876 // Arrows
877 '&larr;' => '&#8592;',
878 '&uarr;' => '&#8593;',
879 '&rarr;' => '&#8594;',
880 '&darr;' => '&#8595;',
881 '&harr;' => '&#8596;',
882 '&crarr;' => '&#8629;',
883 '&lArr;' => '&#8656;',
884 '&uArr;' => '&#8657;',
885 '&rArr;' => '&#8658;',
886 '&dArr;' => '&#8659;',
887 '&hArr;' => '&#8660;',
888 // Mathematical Operators
889 '&forall;' => '&#8704;',
890 '&part;' => '&#8706;',
891 '&exist;' => '&#8707;',
892 '&empty;' => '&#8709;',
893 '&nabla;' => '&#8711;',
894 '&isin;' => '&#8712;',
895 '&notin;' => '&#8713;',
896 '&ni;' => '&#8715;',
897 '&prod;' => '&#8719;',
898 '&sum;' => '&#8721;',
899 '&minus;' => '&#8722;',
900 '&lowast;' => '&#8727;',
901 '&radic;' => '&#8730;',
902 '&prop;' => '&#8733;',
903 '&infin;' => '&#8734;',
904 '&ang;' => '&#8736;',
905 '&and;' => '&#8743;',
906 '&or;' => '&#8744;',
907 '&cap;' => '&#8745;',
908 '&cup;' => '&#8746;',
909 '&int;' => '&#8747;',
910 '&there4;' => '&#8756;',
911 '&sim;' => '&#8764;',
912 '&cong;' => '&#8773;',
913 '&asymp;' => '&#8776;',
914 '&ne;' => '&#8800;',
915 '&equiv;' => '&#8801;',
916 '&le;' => '&#8804;',
917 '&ge;' => '&#8805;',
918 '&sub;' => '&#8834;',
919 '&sup;' => '&#8835;',
920 '&nsub;' => '&#8836;',
921 '&sube;' => '&#8838;',
922 '&supe;' => '&#8839;',
923 '&oplus;' => '&#8853;',
924 '&otimes;' => '&#8855;',
925 '&perp;' => '&#8869;',
926 '&sdot;' => '&#8901;',
927 // Miscellaneous Technical
928 '&lceil;' => '&#8968;',
929 '&rceil;' => '&#8969;',
930 '&lfloor;' => '&#8970;',
931 '&rfloor;' => '&#8971;',
932 '&lang;' => '&#9001;',
933 '&rang;' => '&#9002;',
934 // Geometric Shapes
935 '&loz;' => '&#9674;',
936 // Miscellaneous Symbols
937 '&spades;' => '&#9824;',
938 '&clubs;' => '&#9827;',
939 '&hearts;' => '&#9829;',
940 '&diams;' => '&#9830;');
941
942 $str = str_replace(array_keys($entities), array_values($entities), $str);
943
944 return $str;
945}
a8fa8e33 946
e0e1b169 947/* ------------------------------ main --------------------------- */
a8fa8e33 948
e0e1b169 949global $squirrelmail_language, $languages, $use_gettext;
060c9483 950
0d7a48b0 951if (! sqgetGlobalVar('squirrelmail_language',$squirrelmail_language,SQ_COOKIE)) {
e0e1b169 952 $squirrelmail_language = '';
953}
c30be3cf 954
e0e1b169 955/**
956 * Array specifies the available translations.
957 *
958 * Structure of array:
959 * $languages['language']['variable'] = 'value'
960 *
961 * Possible 'variable' names:
962 * NAME - Translation name in English
963 * CHARSET - Encoding used by translation
964 * ALIAS - used when 'language' is only short name and 'value' should provide long language name
965 * ALTNAME - Native translation name. Any 8bit symbols must be html encoded.
c6cd3136 966 * 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
e0e1b169 967 * DIR - Text direction. Used to define Right-to-Left languages. Possible values 'rtl' or 'ltr'. If undefined - defaults to 'ltr'
968 * XTRA_CODE - translation uses special functions. See doc/i18n.txt
969 *
970 * Each 'language' definition requires NAME+CHARSET or ALIAS variables.
971 *
972 * @name $languages
973 * @global array $languages
974 */
e0e1b169 975$languages['en_US']['NAME'] = 'English';
976$languages['en_US']['CHARSET'] = 'iso-8859-1';
977$languages['en_US']['LOCALE'] = 'en_US.ISO8859-1';
978$languages['en']['ALIAS'] = 'en_US';
a2a7852b 979
89b3545c 980/**
981 * Automatic translation loading from setup.php files.
982 * Solution for bug. 1240889.
983 * setup.php file can contain $languages array entries and XTRA_CODE functions.
89b3545c 984 */
6c8a387f 985if (is_dir(SM_PATH . 'locale') &&
986 is_readable(SM_PATH . 'locale')) {
987 $localedir = dir(SM_PATH . 'locale');
988 while($lang_dir=$localedir->read()) {
989 // remove trailing slash, if present
990 if (substr($lang_dir,-1)=='/') {
991 $lang_dir = substr($lang_dir,0,-1);
992 }
993 if ($lang_dir != '..' && $lang_dir != '.' && $lang_dir != 'CVS' &&
994 is_dir(SM_PATH.'locale/'.$lang_dir) &&
995 file_exists(SM_PATH.'locale/'.$lang_dir.'/setup.php')) {
996 include_once(SM_PATH.'locale/'.$lang_dir.'/setup.php');
997 }
89b3545c 998 }
6c8a387f 999 $localedir->close();
89b3545c 1000}
89b3545c 1001
e0e1b169 1002/* Detect whether gettext is installed. */
1003$gettext_flags = 0;
1004if (function_exists('_')) {
1005 $gettext_flags += 1;
1006}
1007if (function_exists('bindtextdomain')) {
1008 $gettext_flags += 2;
1009}
1010if (function_exists('textdomain')) {
1011 $gettext_flags += 4;
1012}
1013if (function_exists('ngettext')) {
1014 $gettext_flags += 8;
1015}
1016
1017/* If gettext is fully loaded, cool */
1018if ($gettext_flags == 15) {
1019 $use_gettext = true;
1020}
1021
1022/* If ngettext support is missing, load it */
1023elseif ($gettext_flags == 7) {
1024 $use_gettext = true;
1025 // load internal ngettext functions
1026 include_once(SM_PATH . 'class/l10n.class.php');
1027 include_once(SM_PATH . 'functions/ngettext.php');
1028}
1029
1030/* If we can fake gettext, try that */
1031elseif ($gettext_flags == 0) {
1032 $use_gettext = true;
1033 include_once(SM_PATH . 'functions/gettext.php');
1034} else {
1035 /* Uh-ho. A weird install */
1036 if (! $gettext_flags & 1) {
1037 /**
1038 * Function is used as replacement in broken installs
1039 * @ignore
1040 */
1041 function _($str) {
1042 return $str;
1043 }
1044 }
1045 if (! $gettext_flags & 2) {
1046 /**
1047 * Function is used as replacement in broken installs
1048 * @ignore
1049 */
1050 function bindtextdomain() {
1051 return;
1052 }
1053 }
1054 if (! $gettext_flags & 4) {
1055 /**
1056 * Function is used as replacemet in broken installs
1057 * @ignore
1058 */
1059 function textdomain() {
1060 return;
1061 }
1062 }
1063 if (! $gettext_flags & 8) {
1064 /**
1065 * Function is used as replacemet in broken installs
1066 * @ignore
1067 */
1068 function ngettext($str,$str2,$number) {
1069 if ($number>1) {
1070 return $str2;
1071 } else {
1072 return $str;
1073 }
1074 }
1075 }
a9a10f57 1076 if (! function_exists('dgettext')) {
1077 /**
1078 * Replacement for broken setups.
1079 * @ignore
1080 */
1081 function dgettext($domain,$str) {
1082 return $str;
1083 }
1084 }
1085 if (! function_exists('dngettext')) {
1086 /**
1087 * Replacement for broken setups
1088 * @ignore
1089 */
1090 function dngettext($domain,$str1,$strn,$number) {
1091 return ($number==1 ? $str1 : $strn);
1092 }
1093 }
4e519821 1094}
51468260 1095?>