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