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