X-Git-Url: https://vcs.fsf.org/?p=squirrelmail.git;a=blobdiff_plain;f=include%2Flanguages.php;h=b4ac1da74a0a6c8fce6a7aeb311bd773e6a69a6f;hp=592f1f543c5ad267079c6742ce6f4441e550586f;hb=353d074afac6827c90f4bb03e846c5e453d3b5b1;hpb=328c71cd4febfeab496bc2b44b2b24d2111deeb2 diff --git a/include/languages.php b/include/languages.php index 592f1f54..b4ac1da7 100644 --- a/include/languages.php +++ b/include/languages.php @@ -14,7 +14,8 @@ * existing PHP installation and implements fallback functions when required * functions are not available. Scripts in functions/ directory should not * setup anything when they are loaded. - * @copyright © 1999-2007 The SquirrelMail Project Team + * + * @copyright 1999-2018 The SquirrelMail Project Team * @license http://opensource.org/licenses/gpl-license.php GNU Public License * @version $Id$ * @package squirrelmail @@ -25,50 +26,52 @@ /** * Wrapper for textdomain(), bindtextdomain() and * bind_textdomain_codeset() primarily intended for - * plugins when changing into their own text domain + * plugins when changing into their own text domain * and back again. * - * Note that plugins using this function must have + * Note that if plugins using this function have * their translation files located in the SquirrelMail - * locale directory. + * locale directory, the second argument is optional. * - * @param string $domain_name The name of the text domain - * (usually the plugin name, or - * "squirrelmail") being switched to. + * @param string $domain_name The name of the text domain + * (usually the plugin name, or + * "squirrelmail") being switched to. + * @param string $directory The directory that contains + * all translations for the domain + * (OPTIONAL; default is SquirrelMail + * locale directory). * - * @return void + * @return string The name of the text domain that was set + * *BEFORE* it is changed herein - NOTE that + * this differs from PHP's textdomain() * - * @since 1.5.2 and 1.4.10 + * @since 1.4.10 and 1.5.2 */ -function sq_change_text_domain($domain_name) { - - global $languages, $sm_notAlias; +function sq_change_text_domain($domain_name, $directory='') { + global $gettext_domain; static $domains_already_seen = array(); - // only need to call bindtextdomain() once + $return_value = $gettext_domain; + + // empty domain defaults to "squirrelmail" + // + if (empty($domain_name)) $domain_name = 'squirrelmail'; + + // only need to call bindtextdomain() once // if (in_array($domain_name, $domains_already_seen)) { sq_textdomain($domain_name); - return; + return $return_value; } $domains_already_seen[] = $domain_name; - sq_bindtextdomain($domain_name, SM_PATH . 'locale/'); - sq_textdomain($domain_name); - - // set codeset in order to avoid gettext charset conversions - if (function_exists('bind_textdomain_codeset') - && isset($languages[$sm_notAlias]['CHARSET'])) { + if (empty($directory)) $directory = SM_PATH . 'locale/'; - // Japanese translation uses different internal charset - if ($sm_notAlias == 'ja_JP') { - bind_textdomain_codeset ($domain_name, 'EUC-JP'); - } else { - bind_textdomain_codeset ($domain_name, $languages[$sm_notAlias]['CHARSET']); - } + sq_bindtextdomain($domain_name, $directory); + sq_textdomain($domain_name); - } + return $return_value; } /** @@ -77,14 +80,22 @@ function sq_change_text_domain($domain_name) { * Wrapper solves differences between php versions in order to provide * ngettext support. Should be used if translation uses ngettext * functions. - * @since 1.5.1 + * + * This also provides a bind_textdomain_codeset call to make sure the + * domain's encoding will not be overridden. + * + * @since 1.4.10 and 1.5.1 * @param string $domain gettext domain name - * @param string $dir directory that contains all translations + * @param string $dir directory that contains all translations (OPTIONAL; + * if not specified, defaults to SquirrelMail locale + * directory) * @return string path to translation directory */ -function sq_bindtextdomain($domain,$dir) { +function sq_bindtextdomain($domain,$dir='') { global $l10n, $gettext_flags, $sm_notAlias; + if (empty($dir)) $dir = SM_PATH . 'locale/'; + if ($gettext_flags==7) { // gettext extension without ngettext if (substr($dir, -1) != '/') $dir .= '/'; @@ -95,6 +106,19 @@ function sq_bindtextdomain($domain,$dir) { $dir=bindtextdomain($domain,$dir); + // set codeset in order to avoid gettext charset conversions + if (function_exists('bind_textdomain_codeset') + && isset($languages[$sm_notAlias]['CHARSET'])) { + + // Japanese translation uses different internal charset + if ($sm_notAlias == 'ja_JP') { + bind_textdomain_codeset ($domain_name, 'EUC-JP'); + } else { + bind_textdomain_codeset ($domain_name, $languages[$sm_notAlias]['CHARSET']); + } + + } + return $dir; } @@ -122,8 +146,8 @@ function sq_textdomain($domain) { * (LC_ALL, LC_COLLATE, LC_CTYPE, LC_MONETARY, LC_NUMERIC, LC_TIME) * @param mixed $locale option contains array with possible locales or string with one locale * @return string name of set locale or false, if all locales fail. - * @since 1.5.1 and 1.4.5 - * @see http://www.php.net/setlocale + * @since 1.4.5 and 1.5.1 + * @see http://php.net/setlocale */ function sq_setlocale($category,$locale) { if (is_string($locale)) { @@ -146,7 +170,7 @@ function sq_setlocale($category,$locale) { if (preg_match("/^.*\/.*\/.*\/.*\/.*\/.*$/",$ret)) { /** * Welcome to We-Don't-Follow-Own-Fine-Manual department - * OpenBSD 3.8, 3.9-current and maybe later versions + * OpenBSD 3.8, 3.9-current and maybe later versions * return invalid response to setlocale command. * SM bug report #1427512. */ @@ -168,9 +192,9 @@ function sq_setlocale($category,$locale) { * @param string $charset * @param string $string Text to be decoded * @param boolean $force_decode converts string to html without $charset!=$default_charset check. - * Argument is available since 1.5.1 and 1.4.5. - * @param boolean $save_html disables htmlspecialchars() in order to preserve - * html formating. Use with care. Available since 1.5.1 + * Argument is available since 1.4.5 and 1.5.1. + * @param boolean $save_html disables sm_encode_html_special_chars() in order to preserve + * html formating. Use with care. Available since 1.4.6 and 1.5.1 * @return string decoded string */ function charset_decode ($charset, $string, $force_decode=false, $save_html=false) { @@ -194,7 +218,7 @@ function charset_decode ($charset, $string, $force_decode=false, $save_html=fals // Don't do conversion if charset is the same. if ( ! $force_decode && $charset == strtolower($default_charset) ) - return ($save_html ? $string : htmlspecialchars($string)); + return ($save_html ? $string : sm_encode_html_special_chars($string)); // catch iso-8859-8-i thing if ( $charset == "iso-8859-8-i" ) @@ -210,10 +234,10 @@ function charset_decode ($charset, $string, $force_decode=false, $save_html=fals // other charsets can be converted to utf-8 without loss. // and output string is smaller $string = recode_string($charset . "..utf-8",$string); - return ($save_html ? $string : htmlspecialchars($string)); + return ($save_html ? $string : sm_encode_html_special_chars($string)); } else { $string = recode_string($charset . "..html",$string); - // recode does not convert single quote, htmlspecialchars does. + // recode does not convert single quote, sm_encode_html_special_chars does. $string = str_replace("'", ''', $string); // undo html specialchars if ($save_html) @@ -226,13 +250,13 @@ function charset_decode ($charset, $string, $force_decode=false, $save_html=fals // iconv functions does not have html target and can be used only with utf-8 if ( $use_php_iconv && $default_charset=='utf-8') { $string = iconv($charset,$default_charset,$string); - return ($save_html ? $string : htmlspecialchars($string)); + return ($save_html ? $string : sm_encode_html_special_chars($string)); } // If we don't use recode and iconv, we'll do it old way. /* All HTML special characters are 7 bit and can be replaced first */ - if (! $save_html) $string = htmlspecialchars ($string); + if (! $save_html) $string = sm_encode_html_special_chars ($string); /* controls cpu and memory intensive decoding cycles */ if (! isset($aggressive_decoding) || $aggressive_decoding=="" ) { @@ -252,10 +276,10 @@ function charset_decode ($charset, $string, $force_decode=false, $save_html=fals /** * Converts html string to given charset - * @since 1.5.1 and 1.4.4 + * @since 1.4.4 and 1.5.1 * @param string $string * @param string $charset - * @param boolean $htmlencode keep htmlspecialchars encoding + * @param boolean $htmlencode keep sm_encode_html_special_chars encoding * @return string */ function charset_encode($string,$charset,$htmlencode=true) { @@ -295,11 +319,11 @@ function charset_encode($string,$charset,$htmlencode=true) { * * If conversion is done to charset different that utf-8, unsupported symbols * will be replaced with question marks. - * @since 1.5.1 and 1.4.4 + * @since 1.4.4 and 1.5.1 * @param string $in_charset initial charset * @param string $string string that has to be converted * @param string $out_charset final charset - * @param boolean $htmlencode keep htmlspecialchars encoding + * @param boolean $htmlencode keep sm_encode_html_special_chars encoding * @return string converted string */ function charset_convert($in_charset,$string,$out_charset,$htmlencode=true) { @@ -312,15 +336,20 @@ function charset_convert($in_charset,$string,$out_charset,$htmlencode=true) { /** * Makes charset name suitable for decoding cycles * - * @since 1.5.0 and 1.4.4 + * ks_c_5601_1987, x-euc-* and x-windows-* charsets are supported + * since 1.4.6 and 1.5.1. + * + * @since 1.4.4 and 1.5.0 * @param string $charset Name of charset * @return string $charset Adjusted name of charset */ function fixcharset($charset) { - /* remove minus and characters that might be used in paths from charset + + /* Remove minus and characters that might be used in paths from charset * name in order to be able to use it in function names and include calls. + * Also make sure it's in lower case (ala "UTF" --> "utf") */ - $charset=preg_replace("/[-:.\/\\\]/",'_',$charset); + $charset=preg_replace("/[-:.\/\\\]/",'_', strtolower($charset)); // OE ks_c_5601_1987 > cp949 $charset=str_replace('ks_c_5601_1987','cp949',$charset); @@ -355,17 +384,25 @@ function fixcharset($charset) { * 1 = mbstring support is not present, * 2 = mbstring support is not present, user's translation reverted to en_US. * - * @param string $sm_language translation used by user's interface - * @param bool $do_search use browser's preferred language detection functions. Defaults to false. - * @param bool $default set $sm_language to $squirrelmail_default_language if language detection fails or language is not set. Defaults to false. + * @param string $sm_language Translation used by user's interface + * @param bool $do_search Use browser's preferred language detection functions. + * Defaults to false. + * @param bool $default Set $sm_language to $squirrelmail_default_language if + * language detection fails or language is not set. + * Defaults to false. + * @param string $content_type The content type being served currently (OPTIONAL; + * if not specified, defaults to whatever the template + * set that is in use has defined). * @return int function execution error codes. + * */ -function set_up_language($sm_language, $do_search = false, $default = false) { +function set_up_language($sm_language, $do_search = false, $default = false, + $content_type = '') { static $SetupAlready = 0; - global $use_gettext, $languages, - $squirrelmail_language, $squirrelmail_default_language, $default_charset, - $sm_notAlias, $username, $data_dir; + global $use_gettext, $languages, $squirrelmail_language, + $squirrelmail_default_language, $default_charset, $sm_notAlias, + $username, $data_dir, $oTemplate; if ($SetupAlready) { return; @@ -374,11 +411,15 @@ function set_up_language($sm_language, $do_search = false, $default = false) { $SetupAlready = TRUE; sqgetGlobalVar('HTTP_ACCEPT_LANGUAGE', $accept_lang, SQ_SERVER); + // grab content type if needed + // + if (empty($content_type)) $content_type = $oTemplate->get_content_type(); + /** * If function is asked to detect preferred language - * OR squirrelmail default language is set to empty string + * OR SquirrelMail default language is set to empty string * AND - * squirrelmail language ($sm_language) is empty string + * SquirrelMail language ($sm_language) is empty string * (not set in user's prefs and no cookie with language info) * AND * browser provides list of preferred languages @@ -397,9 +438,9 @@ function set_up_language($sm_language, $do_search = false, $default = false) { /** * If language preference is not set OR script asks to use default language * AND - * default squirrelmail language is not set to empty string + * default SquirrelMail language is not set to empty string * THEN - * use default squirrelmail language value from configuration. + * use default SquirrelMail language value from configuration. */ if ((!$sm_language||$default) && ! empty($squirrelmail_default_language)) { @@ -430,16 +471,6 @@ function set_up_language($sm_language, $do_search = false, $default = false) { sq_bindtextdomain( 'squirrelmail', SM_PATH . 'locale/' ); sq_textdomain( 'squirrelmail' ); - // set codeset in order to avoid gettext charset conversions - if (function_exists('bind_textdomain_codeset')) { - // Japanese translation uses different internal charset - if ($sm_notAlias == 'ja_JP') { - bind_textdomain_codeset ('squirrelmail', 'EUC-JP'); - } else { - bind_textdomain_codeset ('squirrelmail', $languages[$sm_notAlias]['CHARSET'] ); - } - } - // Use LOCALE key, if it is set. if (isset($languages[$sm_notAlias]['LOCALE'])){ $longlocale=$languages[$sm_notAlias]['LOCALE']; @@ -508,7 +539,7 @@ function set_up_language($sm_language, $do_search = false, $default = false) { $squirrelmail_language = $sm_notAlias; if ($squirrelmail_language == 'ja_JP') { - header ('Content-Type: text/html; charset=EUC-JP'); + $oTemplate->header ('Content-Type: ' . $content_type . '; charset=EUC-JP'); if (!function_exists('mb_internal_encoding')) { // Error messages can't be displayed here $error = 1; @@ -526,15 +557,15 @@ function set_up_language($sm_language, $do_search = false, $default = false) { mb_internal_encoding('EUC-JP'); mb_http_output('pass'); } elseif ($squirrelmail_language == 'en_US') { - header( 'Content-Type: text/html; charset=' . $default_charset ); + $oTemplate->header( 'Content-Type: ' . $content_type . '; charset=' . $default_charset ); } else { - header( 'Content-Type: text/html; charset=' . $languages[$sm_notAlias]['CHARSET'] ); + $oTemplate->header( 'Content-Type: ' . $content_type . '; charset=' . $languages[$sm_notAlias]['CHARSET'] ); } /** * mbstring.func_overload fix (#929644). * * php mbstring extension can replace standard string functions with their multibyte - * equivalents. See http://www.php.net/ref.mbstring#mbstring.overload. This feature + * equivalents. See http://php.net/ref.mbstring#mbstring.overload. This feature * was added in php v.4.2.0 * * Some SquirrelMail functions work with 8bit strings in bytes. If interface is forced @@ -556,11 +587,13 @@ function set_up_language($sm_language, $do_search = false, $default = false) { } /** - * Sets default_charset variable according to the one that is used by user's translations. + * Sets default_charset variable according to the one that is used by user's + * translations. * - * Function changes global $default_charset variable in order to be sure, that it - * contains charset used by user's translation. Sanity of $squirrelmail_language - * and $default_charset combination is also tested. + * Function changes global $default_charset variable in order to be sure, that + * it contains charset used by user's translation. Sanity of + * $squirrelmail_language and $default_charset combination provided in the + * SquirrelMail configuration is also tested. * * There can be a $default_charset setting in the * config.php file, but the user may have a different language @@ -666,8 +699,8 @@ function is_conversion_safe($input_charset) { switch ($default_charset) { case "windows-1251": if ( $input_charset == "iso-8859-5" || - $input_charset == "koi8-r" || - $input_charset == "koi8-u" ) { + $input_charset == "koi8-r" || + $input_charset == "koi8-u" ) { return true; } else { return false; @@ -1013,7 +1046,7 @@ if (! sqgetGlobalVar('squirrelmail_language',$squirrelmail_language,SQ_COOKIE)) } /** - * Array specifies the available translations. + * This array specifies the available translations. * * Structure of array: * $languages['language']['variable'] = 'value' @@ -1025,7 +1058,7 @@ if (! sqgetGlobalVar('squirrelmail_language',$squirrelmail_language,SQ_COOKIE)) * ALTNAME - Native translation name. Any 8bit symbols must be html encoded. * 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 * DIR - Text direction. Used to define Right-to-Left languages. Possible values 'rtl' or 'ltr'. If undefined - defaults to 'ltr' - * XTRA_CODE - translation uses special functions. See doc/i18n.txt + * XTRA_CODE - translation uses special functions. See http://squirrelmail.org/docs/devel/devel-3.html * * Each 'language' definition requires NAME+CHARSET or ALIAS variables. *