Add sq_change_text_domain for plugins to use when switching text domains
[squirrelmail.git] / include / languages.php
CommitLineData
202bcbcc 1<?php
2
3/**
4 * SquirrelMail internationalization functions
5 *
6 * This file contains variuos functions that are needed to do
7 * internationalization of SquirrelMail.
8 *
9 * Internally the output character set is used. Other characters are
10 * encoded using Unicode entities according to HTML 4.0.
11 *
867fed37 12 * Before 1.5.2 functions were stored in functions/i18n.php. Script is moved
13 * because it executes some code in order to detect functions supported by
14 * existing PHP installation and implements fallback functions when required
15 * functions are not available. Scripts in functions/ directory should not
16 * setup anything when they are loaded.
4b5049de 17 * @copyright &copy; 1999-2007 The SquirrelMail Project Team
202bcbcc 18 * @license http://opensource.org/licenses/gpl-license.php GNU Public License
19 * @version $Id$
20 * @package squirrelmail
21 * @subpackage i18n
22 */
23
24
25/**
328c71cd 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 plugins using this function must have
32 * their translation files located in the SquirrelMail
33 * locale directory.
34 *
35 * @param string $domain_name The name of the text domain
36 * (usually the plugin name, or
37 * "squirrelmail") being switched to.
38 *
39 * @return void
40 *
41 * @since 1.5.2 and 1.4.10
42 */
43function sq_change_text_domain($domain_name) {
44
45 global $languages, $sm_notAlias;
46 static $domains_already_seen = array();
47
48 // only need to call bindtextdomain() once
49 //
50 if (in_array($domain_name, $domains_already_seen)) {
51 sq_textdomain($domain_name);
52 return;
53 }
54
55 $domains_already_seen[] = $domain_name;
56
57 sq_bindtextdomain($domain_name, SM_PATH . 'locale/');
58 sq_textdomain($domain_name);
59
60 // set codeset in order to avoid gettext charset conversions
61 if (function_exists('bind_textdomain_codeset')
62 && isset($languages[$sm_notAlias]['CHARSET'])) {
63
64 // Japanese translation uses different internal charset
65 if ($sm_notAlias == 'ja_JP') {
66 bind_textdomain_codeset ($domain_name, 'EUC-JP');
67 } else {
68 bind_textdomain_codeset ($domain_name, $languages[$sm_notAlias]['CHARSET']);
69 }
70
71 }
72}
73
74/**
202bcbcc 75 * Gettext bindtextdomain wrapper.
76 *
77 * Wrapper solves differences between php versions in order to provide
78 * ngettext support. Should be used if translation uses ngettext
79 * functions.
80 * @since 1.5.1
81 * @param string $domain gettext domain name
82 * @param string $dir directory that contains all translations
83 * @return string path to translation directory
84 */
85function sq_bindtextdomain($domain,$dir) {
86 global $l10n, $gettext_flags, $sm_notAlias;
87
88 if ($gettext_flags==7) {
89 // gettext extension without ngettext
90 if (substr($dir, -1) != '/') $dir .= '/';
91 $mofile=$dir . $sm_notAlias . '/LC_MESSAGES/' . $domain . '.mo';
92 $input = new FileReader($mofile);
93 $l10n[$domain] = new gettext_reader($input);
94 }
95
96 $dir=bindtextdomain($domain,$dir);
97
98 return $dir;
99}
100
101/**
102 * Gettext textdomain wrapper.
103 * Makes sure that gettext_domain global is modified.
104 * @since 1.5.1
105 * @param string $name gettext domain name
106 * @return string gettext domain name
107 */
108function sq_textdomain($domain) {
109 global $gettext_domain;
110 $gettext_domain=textdomain($domain);
111 return $gettext_domain;
112}
113
114/**
115 * php setlocale function wrapper
116 *
117 * From php 4.3.0 it is possible to use arrays in order to set locale.
118 * php gettext extension works only when locale is set. This wrapper
119 * function allows to use more than one locale name.
120 *
121 * @param int $category locale category name. Use php named constants
122 * (LC_ALL, LC_COLLATE, LC_CTYPE, LC_MONETARY, LC_NUMERIC, LC_TIME)
123 * @param mixed $locale option contains array with possible locales or string with one locale
124 * @return string name of set locale or false, if all locales fail.
125 * @since 1.5.1 and 1.4.5
126 * @see http://www.php.net/setlocale
127 */
128function sq_setlocale($category,$locale) {
06783280 129 if (is_string($locale)) {
130 // string with only one locale
131 $ret = setlocale($category,$locale);
132 } elseif (! check_php_version(4,3)) {
133 // older php version (second setlocale argument must be string)
202bcbcc 134 $ret=false;
135 $index=0;
136 while ( ! $ret && $index<count($locale)) {
137 $ret=setlocale($category,$locale[$index]);
138 $index++;
139 }
140 } else {
141 // php 4.3.0 or better, use entire array
142 $ret=setlocale($category,$locale);
143 }
06783280 144
145 /* safety checks */
146 if (preg_match("/^.*\/.*\/.*\/.*\/.*\/.*$/",$ret)) {
147 /**
148 * Welcome to We-Don't-Follow-Own-Fine-Manual department
149 * OpenBSD 3.8, 3.9-current and maybe later versions
150 * return invalid response to setlocale command.
151 * SM bug report #1427512.
152 */
153 $ret = false;
154 }
202bcbcc 155 return $ret;
156}
157
158/**
159 * Converts string from given charset to charset, that can be displayed by user translation.
160 *
161 * Function by default returns html encoded strings, if translation uses different encoding.
162 * If Japanese translation is used - function returns string converted to euc-jp
163 * If iconv or recode functions are enabled and translation uses utf-8 - function returns utf-8 encoded string.
164 * If $charset is not supported - function returns unconverted string.
165 *
166 * sanitizing of html tags is also done by this function.
167 *
168 * @param string $charset
169 * @param string $string Text to be decoded
170 * @param boolean $force_decode converts string to html without $charset!=$default_charset check.
171 * Argument is available since 1.5.1 and 1.4.5.
172 * @param boolean $save_html disables htmlspecialchars() in order to preserve
173 * html formating. Use with care. Available since 1.5.1
174 * @return string decoded string
175 */
176function charset_decode ($charset, $string, $force_decode=false, $save_html=false) {
177 global $languages, $squirrelmail_language, $default_charset;
178 global $use_php_recode, $use_php_iconv, $aggressive_decoding;
179
180 if (isset($languages[$squirrelmail_language]['XTRA_CODE']) &&
181 function_exists($languages[$squirrelmail_language]['XTRA_CODE'] . '_decode')) {
182 $string = call_user_func($languages[$squirrelmail_language]['XTRA_CODE'] . '_decode', $string);
183 }
184
185 $charset = strtolower($charset);
186
187 set_my_charset();
188
189 // Variables that allow to use functions without function_exist() calls
190 if (! isset($use_php_recode) || $use_php_recode=="" ) {
191 $use_php_recode=false; }
192 if (! isset($use_php_iconv) || $use_php_iconv=="" ) {
193 $use_php_iconv=false; }
194
195 // Don't do conversion if charset is the same.
196 if ( ! $force_decode && $charset == strtolower($default_charset) )
197 return ($save_html ? $string : htmlspecialchars($string));
198
199 // catch iso-8859-8-i thing
200 if ( $charset == "iso-8859-8-i" )
201 $charset = "iso-8859-8";
202
203 /*
204 * Recode converts html special characters automatically if you use
205 * 'charset..html' decoding. There is no documented way to put -d option
206 * into php recode function call.
207 */
208 if ( $use_php_recode ) {
209 if ( $default_charset == "utf-8" ) {
210 // other charsets can be converted to utf-8 without loss.
211 // and output string is smaller
212 $string = recode_string($charset . "..utf-8",$string);
213 return ($save_html ? $string : htmlspecialchars($string));
214 } else {
215 $string = recode_string($charset . "..html",$string);
216 // recode does not convert single quote, htmlspecialchars does.
217 $string = str_replace("'", '&#039;', $string);
218 // undo html specialchars
219 if ($save_html)
220 $string=str_replace(array('&amp;','&quot;','&lt;','&gt;'),
221 array('&','"','<','>'),$string);
222 return $string;
223 }
224 }
225
226 // iconv functions does not have html target and can be used only with utf-8
227 if ( $use_php_iconv && $default_charset=='utf-8') {
228 $string = iconv($charset,$default_charset,$string);
229 return ($save_html ? $string : htmlspecialchars($string));
230 }
231
232 // If we don't use recode and iconv, we'll do it old way.
233
234 /* All HTML special characters are 7 bit and can be replaced first */
235 if (! $save_html) $string = htmlspecialchars ($string);
236
237 /* controls cpu and memory intensive decoding cycles */
238 if (! isset($aggressive_decoding) || $aggressive_decoding=="" ) {
239 $aggressive_decoding=false; }
240
241 $decode=fixcharset($charset);
242 $decodefile=SM_PATH . 'functions/decode/' . $decode . '.php';
ee872922 243 if ($decode != 'index' && file_exists($decodefile)) {
202bcbcc 244 include_once($decodefile);
245 // send $save_html argument to decoding function. needed for iso-2022-xx decoding.
246 $ret = call_user_func('charset_decode_'.$decode, $string, $save_html);
247 } else {
248 $ret = $string;
249 }
250 return( $ret );
251}
252
253/**
254 * Converts html string to given charset
255 * @since 1.5.1 and 1.4.4
256 * @param string $string
257 * @param string $charset
258 * @param boolean $htmlencode keep htmlspecialchars encoding
69022e98 259 * @return string
202bcbcc 260 */
261function charset_encode($string,$charset,$htmlencode=true) {
262 global $default_charset;
263
264 $encode=fixcharset($charset);
265 $encodefile=SM_PATH . 'functions/encode/' . $encode . '.php';
ee872922 266 if ($encode != 'index' && file_exists($encodefile)) {
202bcbcc 267 include_once($encodefile);
268 $ret = call_user_func('charset_encode_'.$encode, $string);
269 } elseif(file_exists(SM_PATH . 'functions/encode/us_ascii.php')) {
270 // function replaces all 8bit html entities with question marks.
271 // it is used when other encoding functions are unavailable
272 include_once(SM_PATH . 'functions/encode/us_ascii.php');
273 $ret = charset_encode_us_ascii($string);
274 } else {
275 /**
276 * fix for yahoo users that remove all us-ascii related things
277 */
278 $ret = $string;
279 }
280
281 /**
282 * Undo html special chars, some places (like compose form) have
283 * own sanitizing functions and don't need html symbols.
284 * Undo chars only after encoding in order to prevent conversion of
285 * html entities in plain text emails.
286 */
287 if (! $htmlencode ) {
288 $ret = str_replace(array('&amp;','&gt;','&lt;','&quot;'),array('&','>','<','"'),$ret);
289 }
290 return( $ret );
291}
292
293/**
294 * Combined decoding and encoding functions
295 *
296 * If conversion is done to charset different that utf-8, unsupported symbols
297 * will be replaced with question marks.
298 * @since 1.5.1 and 1.4.4
299 * @param string $in_charset initial charset
300 * @param string $string string that has to be converted
301 * @param string $out_charset final charset
302 * @param boolean $htmlencode keep htmlspecialchars encoding
303 * @return string converted string
304 */
305function charset_convert($in_charset,$string,$out_charset,$htmlencode=true) {
306 $string=charset_decode($in_charset,$string,true);
307 $string=sqi18n_convert_entities($string);
308 $string=charset_encode($string,$out_charset,$htmlencode);
309 return $string;
310}
311
312/**
313 * Makes charset name suitable for decoding cycles
314 *
315 * @since 1.5.0 and 1.4.4
316 * @param string $charset Name of charset
317 * @return string $charset Adjusted name of charset
318 */
319function fixcharset($charset) {
320 /* remove minus and characters that might be used in paths from charset
321 * name in order to be able to use it in function names and include calls.
322 */
323 $charset=preg_replace("/[-:.\/\\\]/",'_',$charset);
324
325 // OE ks_c_5601_1987 > cp949
326 $charset=str_replace('ks_c_5601_1987','cp949',$charset);
327 // Moz x-euc-tw > euc-tw
328 $charset=str_replace('x_euc','euc',$charset);
329 // Moz x-windows-949 > cp949
330 $charset=str_replace('x_windows_','cp',$charset);
331
332 // windows-125x and cp125x charsets
333 $charset=str_replace('windows_','cp',$charset);
334
335 // ibm > cp
336 $charset=str_replace('ibm','cp',$charset);
337
338 // iso-8859-8-i -> iso-8859-8
339 // use same cycle until I'll find differences
340 $charset=str_replace('iso_8859_8_i','iso_8859_8',$charset);
341
342 return $charset;
343}
344
345/**
346 * Set up the language to be output
347 * if $do_search is true, then scan the browser information
348 * for a possible language that we know
349 *
350 * Function sets system locale environment (LC_ALL, LANG, LANGUAGE),
351 * gettext translation bindings and html header information.
352 *
353 * Function returns error codes, if there is some fatal error.
354 * 0 = no error,
355 * 1 = mbstring support is not present,
356 * 2 = mbstring support is not present, user's translation reverted to en_US.
357 *
358 * @param string $sm_language translation used by user's interface
359 * @param bool $do_search use browser's preferred language detection functions. Defaults to false.
360 * @param bool $default set $sm_language to $squirrelmail_default_language if language detection fails or language is not set. Defaults to false.
361 * @return int function execution error codes.
362 */
363function set_up_language($sm_language, $do_search = false, $default = false) {
364
365 static $SetupAlready = 0;
366 global $use_gettext, $languages,
367 $squirrelmail_language, $squirrelmail_default_language, $default_charset,
368 $sm_notAlias, $username, $data_dir;
369
370 if ($SetupAlready) {
371 return;
372 }
373
374 $SetupAlready = TRUE;
375 sqgetGlobalVar('HTTP_ACCEPT_LANGUAGE', $accept_lang, SQ_SERVER);
376
377 /**
378 * If function is asked to detect preferred language
379 * OR squirrelmail default language is set to empty string
380 * AND
381 * squirrelmail language ($sm_language) is empty string
382 * (not set in user's prefs and no cookie with language info)
383 * AND
384 * browser provides list of preferred languages
385 * THEN
386 * get preferred language from HTTP_ACCEPT_LANGUAGE header
387 */
388 if (($do_search || empty($squirrelmail_default_language)) &&
389 ! $sm_language &&
390 isset($accept_lang)) {
391 // TODO: use more than one language, if first language is not available
392 // FIXME: function assumes that string contains two or more characters.
393 // FIXME: some languages use 5 chars
394 $sm_language = substr($accept_lang, 0, 2);
395 }
396
397 /**
398 * If language preference is not set OR script asks to use default language
399 * AND
400 * default squirrelmail language is not set to empty string
401 * THEN
402 * use default squirrelmail language value from configuration.
403 */
404 if ((!$sm_language||$default) &&
405 ! empty($squirrelmail_default_language)) {
406 $squirrelmail_language = $squirrelmail_default_language;
407 $sm_language = $squirrelmail_default_language;
408 }
409
410 /** provide failsafe language when detection fails */
411 if (! $sm_language) $sm_language='en_US';
412
413 $sm_notAlias = $sm_language;
414
415 // Catching removed translation
416 // System reverts to English translation if user prefs contain translation
417 // that is not available in $languages array
418 if (!isset($languages[$sm_notAlias])) {
419 $sm_notAlias="en_US";
420 }
421
422 while (isset($languages[$sm_notAlias]['ALIAS'])) {
423 $sm_notAlias = $languages[$sm_notAlias]['ALIAS'];
424 }
425
426 if ( isset($sm_language) &&
427 $use_gettext &&
428 $sm_language != '' &&
429 isset($languages[$sm_notAlias]['CHARSET']) ) {
430 sq_bindtextdomain( 'squirrelmail', SM_PATH . 'locale/' );
431 sq_textdomain( 'squirrelmail' );
432
433 // set codeset in order to avoid gettext charset conversions
434 if (function_exists('bind_textdomain_codeset')) {
435 // Japanese translation uses different internal charset
436 if ($sm_notAlias == 'ja_JP') {
437 bind_textdomain_codeset ('squirrelmail', 'EUC-JP');
438 } else {
439 bind_textdomain_codeset ('squirrelmail', $languages[$sm_notAlias]['CHARSET'] );
440 }
441 }
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 */
573function 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 */
604function 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
621switch($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;
636endswitch;
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 */
651function 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 */
737function 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
1009global $squirrelmail_language, $languages, $use_gettext;
1010
1011if (! 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 */
1045if (is_dir(SM_PATH . 'locale') &&
1046 is_readable(SM_PATH . 'locale')) {
1047 $localedir = dir(SM_PATH . 'locale');
1048 while($lang_dir=$localedir->read()) {
1049 // remove trailing slash, if present
1050 if (substr($lang_dir,-1)=='/') {
1051 $lang_dir = substr($lang_dir,0,-1);
1052 }
1053 if ($lang_dir != '..' && $lang_dir != '.' && $lang_dir != 'CVS' &&
fdf58ef9 1054 $lang_dir != '.svn' && is_dir(SM_PATH.'locale/'.$lang_dir) &&
202bcbcc 1055 file_exists(SM_PATH.'locale/'.$lang_dir.'/setup.php')) {
1056 include_once(SM_PATH.'locale/'.$lang_dir.'/setup.php');
1057 }
1058 }
1059 $localedir->close();
1060}
1061
1062/* Detect whether gettext is installed. */
1063$gettext_flags = 0;
1064if (function_exists('_')) {
1065 $gettext_flags += 1;
1066}
1067if (function_exists('bindtextdomain')) {
1068 $gettext_flags += 2;
1069}
1070if (function_exists('textdomain')) {
1071 $gettext_flags += 4;
1072}
1073if (function_exists('ngettext')) {
1074 $gettext_flags += 8;
1075}
1076
1077/* If gettext is fully loaded, cool */
1078if ($gettext_flags == 15) {
1079 $use_gettext = true;
1080}
1081
1082/* If ngettext support is missing, load it */
1083elseif ($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 */
1091elseif ($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}