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