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