me bad. loose but lossy.
[squirrelmail.git] / functions / i18n.php
index 481d27b02a930b74ba2a31b7bbb035da06b4e356..cf30023b4925294944758ddb2fea0a0c5f49018a 100644 (file)
@@ -1,9 +1,9 @@
 <?php
 
 /**
- * i18n.php
+ * SquirrelMail internationalization functions
  *
- * Copyright (c) 1999-2002 The SquirrelMail Project Team
+ * Copyright (c) 1999-2004 The SquirrelMail Project Team
  * Licensed under the GNU GPL. For full terms see the file COPYING.
  *
  * This file contains variuos functions that are needed to do
  * Internally the output character set is used. Other characters are
  * encoded using Unicode entities according to HTML 4.0.
  *
- * $Id$
+ * @version $Id$
+ * @package squirrelmail
+ * @subpackage i18n
  */
 
-/* Decodes a string to the internal encoding from the given charset */
+/** Everything uses global.php... */
+require_once(SM_PATH . 'functions/global.php');
+
+/**
+ * Converts string from given charset to charset, that can be displayed by user translation.
+ *
+ * Function by default returns html encoded strings, if translation uses different encoding.
+ * If Japanese translation is used - function returns string converted to euc-jp
+ * If iconv or recode functions are enabled and translation uses utf-8 - function returns utf-8 encoded string.
+ * If $charset is not supported - function returns unconverted string.
+ *
+ * sanitizing of html tags is also done by this function.
+ *
+ * @param string $charset
+ * @param string $string Text to be decoded
+ * @return string decoded string
+ */
 function charset_decode ($charset, $string) {
-    global $languages, $squirrelmail_language;
+    global $languages, $squirrelmail_language, $default_charset;
+    global $use_php_recode, $use_php_iconv, $aggressive_decoding;
 
     if (isset($languages[$squirrelmail_language]['XTRA_CODE']) &&
-        function_exists($languages[$squirrelmail_language]['XTRA_CODE'])) {
-        $string = $languages[$squirrelmail_language]['XTRA_CODE']('decode', $string);
+        function_exists($languages[$squirrelmail_language]['XTRA_CODE'] . '_decode')) {
+        $string = call_user_func($languages[$squirrelmail_language]['XTRA_CODE'] . '_decode', $string);
     }
 
-    /* All HTML special characters are 7 bit and can be replaced first */
-    $string = htmlspecialchars ($string);
-
     $charset = strtolower($charset);
 
-    if (ereg('iso-8859-([[:digit:]]+)', $charset, $res)) {
-        if ($res[1] == '1') {
-            $ret = charset_decode_iso_8859_1 ($string);
-        } else if ($res[1] == '2') {
-            $ret = charset_decode_iso_8859_2 ($string);
-        } else if ($res[1] == '4') {
-            $ret = charset_decode_iso_8859_4 ($string);
-        } else if ($res[1] == '7') {
-            $ret = charset_decode_iso_8859_7 ($string);
-        } else if ($res[1] == '13') {
-            $ret = charset_decode_iso_8859_13 ($string);
-        } else if ($res[1] == '15') {
-            $ret = charset_decode_iso_8859_15 ($string);
-        } else {
-            $ret = charset_decode_iso_8859_default ($string);
-        }
-    } else if ($charset == 'ns_4551-1') {
-        $ret = charset_decode_ns_4551_1 ($string);
-    } else if ($charset == 'koi8-r') {
-        $ret = charset_decode_koi8r ($string);
-    } else if ($charset == 'windows-1251') {
-        $ret = charset_decode_koi8r ($string);
-    } else {
-        $ret = $string;
-    }
-    return( $ret );
-}
-
-/*
- iso-8859-1 is the same as Latin 1 and is normally used
- in western europe.
- */
-function charset_decode_iso_8859_1 ($string) {
-    global $default_charset;
-
-    if (strtolower($default_charset) <> 'iso-8859-1') {
-        /* Only do the slow convert if there are 8-bit characters */
-        if (ereg("[\200-\377]", $string)) {
-            $string = str_replace("\201", '&#129;', $string);
-            $string = str_replace("\202", '&#130;', $string);
-            $string = str_replace("\203", '&#131;', $string);
-            $string = str_replace("\204", '&#132;', $string);
-            $string = str_replace("\205", '&#133;', $string);
-            $string = str_replace("\206", '&#134;', $string);
-            $string = str_replace("\207", '&#135;', $string);
-            $string = str_replace("\210", '&#136;', $string);
-            $string = str_replace("\211", '&#137;', $string);
-            $string = str_replace("\212", '&#138;', $string);
-            $string = str_replace("\213", '&#139;', $string);
-            $string = str_replace("\214", '&#140;', $string);
-            $string = str_replace("\215", '&#141;', $string);
-            $string = str_replace("\216", '&#142;', $string);
-            $string = str_replace("\217", '&#143;', $string);
-            $string = str_replace("\220", '&#144;', $string);
-            $string = str_replace("\221", '&#145;', $string);
-            $string = str_replace("\222", '&#146;', $string);
-            $string = str_replace("\223", '&#147;', $string);
-            $string = str_replace("\224", '&#148;', $string);
-            $string = str_replace("\225", '&#149;', $string);
-            $string = str_replace("\226", '&#150;', $string);
-            $string = str_replace("\227", '&#151;', $string);
-            $string = str_replace("\230", '&#152;', $string);
-            $string = str_replace("\231", '&#153;', $string);
-            $string = str_replace("\232", '&#154;', $string);
-            $string = str_replace("\233", '&#155;', $string);
-            $string = str_replace("\234", '&#156;', $string);
-            $string = str_replace("\235", '&#157;', $string);
-            $string = str_replace("\236", '&#158;', $string);
-            $string = str_replace("\237", '&#159;', $string);
-            $string = str_replace("\240", '&#160;', $string);
-            $string = str_replace("\241", '&#161;', $string);
-            $string = str_replace("\242", '&#162;', $string);
-            $string = str_replace("\243", '&#163;', $string);
-            $string = str_replace("\244", '&#164;', $string);
-            $string = str_replace("\245", '&#165;', $string);
-            $string = str_replace("\246", '&#166;', $string);
-            $string = str_replace("\247", '&#167;', $string);
-            $string = str_replace("\250", '&#168;', $string);
-            $string = str_replace("\251", '&#169;', $string);
-            $string = str_replace("\252", '&#170;', $string);
-            $string = str_replace("\253", '&#171;', $string);
-            $string = str_replace("\254", '&#172;', $string);
-            $string = str_replace("\255", '&#173;', $string);
-            $string = str_replace("\256", '&#174;', $string);
-            $string = str_replace("\257", '&#175;', $string);
-            $string = str_replace("\260", '&#176;', $string);
-            $string = str_replace("\261", '&#177;', $string);
-            $string = str_replace("\262", '&#178;', $string);
-            $string = str_replace("\263", '&#179;', $string);
-            $string = str_replace("\264", '&#180;', $string);
-            $string = str_replace("\265", '&#181;', $string);
-            $string = str_replace("\266", '&#182;', $string);
-            $string = str_replace("\267", '&#183;', $string);
-            $string = str_replace("\270", '&#184;', $string);
-            $string = str_replace("\271", '&#185;', $string);
-            $string = str_replace("\272", '&#186;', $string);
-            $string = str_replace("\273", '&#187;', $string);
-            $string = str_replace("\274", '&#188;', $string);
-            $string = str_replace("\275", '&#189;', $string);
-            $string = str_replace("\276", '&#190;', $string);
-            $string = str_replace("\277", '&#191;', $string);
-            $string = str_replace("\300", '&#192;', $string);
-            $string = str_replace("\301", '&#193;', $string);
-            $string = str_replace("\302", '&#194;', $string);
-            $string = str_replace("\303", '&#195;', $string);
-            $string = str_replace("\304", '&#196;', $string);
-            $string = str_replace("\305", '&#197;', $string);
-            $string = str_replace("\306", '&#198;', $string);
-            $string = str_replace("\307", '&#199;', $string);
-            $string = str_replace("\310", '&#200;', $string);
-            $string = str_replace("\311", '&#201;', $string);
-            $string = str_replace("\312", '&#202;', $string);
-            $string = str_replace("\313", '&#203;', $string);
-            $string = str_replace("\314", '&#204;', $string);
-            $string = str_replace("\315", '&#205;', $string);
-            $string = str_replace("\316", '&#206;', $string);
-            $string = str_replace("\317", '&#207;', $string);
-            $string = str_replace("\320", '&#208;', $string);
-            $string = str_replace("\321", '&#209;', $string);
-            $string = str_replace("\322", '&#210;', $string);
-            $string = str_replace("\323", '&#211;', $string);
-            $string = str_replace("\324", '&#212;', $string);
-            $string = str_replace("\325", '&#213;', $string);
-            $string = str_replace("\326", '&#214;', $string);
-            $string = str_replace("\327", '&#215;', $string);
-            $string = str_replace("\330", '&#216;', $string);
-            $string = str_replace("\331", '&#217;', $string);
-            $string = str_replace("\332", '&#218;', $string);
-            $string = str_replace("\333", '&#219;', $string);
-            $string = str_replace("\334", '&#220;', $string);
-            $string = str_replace("\335", '&#221;', $string);
-            $string = str_replace("\336", '&#222;', $string);
-            $string = str_replace("\337", '&#223;', $string);
-            $string = str_replace("\340", '&#224;', $string);
-            $string = str_replace("\341", '&#225;', $string);
-            $string = str_replace("\342", '&#226;', $string);
-            $string = str_replace("\343", '&#227;', $string);
-            $string = str_replace("\344", '&#228;', $string);
-            $string = str_replace("\345", '&#229;', $string);
-            $string = str_replace("\346", '&#230;', $string);
-            $string = str_replace("\347", '&#231;', $string);
-            $string = str_replace("\350", '&#232;', $string);
-            $string = str_replace("\351", '&#233;', $string);
-            $string = str_replace("\352", '&#234;', $string);
-            $string = str_replace("\353", '&#235;', $string);
-            $string = str_replace("\354", '&#236;', $string);
-            $string = str_replace("\355", '&#237;', $string);
-            $string = str_replace("\356", '&#238;', $string);
-            $string = str_replace("\357", '&#239;', $string);
-            $string = str_replace("\360", '&#240;', $string);
-            $string = str_replace("\361", '&#241;', $string);
-            $string = str_replace("\362", '&#242;', $string);
-            $string = str_replace("\363", '&#243;', $string);
-            $string = str_replace("\364", '&#244;', $string);
-            $string = str_replace("\365", '&#245;', $string);
-            $string = str_replace("\366", '&#246;', $string);
-            $string = str_replace("\367", '&#247;', $string);
-            $string = str_replace("\370", '&#248;', $string);
-            $string = str_replace("\371", '&#249;', $string);
-            $string = str_replace("\372", '&#250;', $string);
-            $string = str_replace("\373", '&#251;', $string);
-            $string = str_replace("\374", '&#252;', $string);
-            $string = str_replace("\375", '&#253;', $string);
-            $string = str_replace("\376", '&#254;', $string);
-            $string = str_replace("\377", '&#255;', $string);
-        }
-    }
-
-    return ($string);
-}
-
-/* iso-8859-2 is used for some eastern European languages */
-function charset_decode_iso_8859_2 ($string) {
-    global $default_charset;
-
-    if (strtolower($default_charset) == 'iso-8859-2')
-        return $string;
-
-    /* Only do the slow convert if there are 8-bit characters */
-    if (! ereg("[\200-\377]", $string))
-        return $string;
-
-    /* NO-BREAK SPACE */
-    $string = str_replace("\240", '&#160;', $string);
-    /* LATIN CAPITAL LETTER A WITH OGONEK */
-    $string = str_replace("\241", '&#260;', $string);
-    /* BREVE */
-    $string = str_replace("\242", '&#728;', $string);
-    // LATIN CAPITAL LETTER L WITH STROKE
-    $string = str_replace("\243", '&#321;', $string);
-    // CURRENCY SIGN
-    $string = str_replace("\244", '&#164;', $string);
-    // LATIN CAPITAL LETTER L WITH CARON
-    $string = str_replace("\245", '&#317;', $string);
-    // LATIN CAPITAL LETTER S WITH ACUTE
-    $string = str_replace("\246", '&#346;', $string);
-    // SECTION SIGN
-    $string = str_replace("\247", '&#167;', $string);
-    // DIAERESIS
-    $string = str_replace("\250", '&#168;', $string);
-    // LATIN CAPITAL LETTER S WITH CARON
-    $string = str_replace("\251", '&#352;', $string);
-    // LATIN CAPITAL LETTER S WITH CEDILLA
-    $string = str_replace("\252", '&#350;', $string);
-    // LATIN CAPITAL LETTER T WITH CARON
-    $string = str_replace("\253", '&#356;', $string);
-    // LATIN CAPITAL LETTER Z WITH ACUTE
-    $string = str_replace("\254", '&#377;', $string);
-    // SOFT HYPHEN
-    $string = str_replace("\255", '&#173;', $string);
-    // LATIN CAPITAL LETTER Z WITH CARON
-    $string = str_replace("\256", '&#381;', $string);
-    // LATIN CAPITAL LETTER Z WITH DOT ABOVE
-    $string = str_replace("\257", '&#379;', $string);
-    // DEGREE SIGN
-    $string = str_replace("\260", '&#176;', $string);
-    // LATIN SMALL LETTER A WITH OGONEK
-    $string = str_replace("\261", '&#261;', $string);
-    // OGONEK
-    $string = str_replace("\262", '&#731;', $string);
-    // LATIN SMALL LETTER L WITH STROKE
-    $string = str_replace("\263", '&#322;', $string);
-    // ACUTE ACCENT
-    $string = str_replace("\264", '&#180;', $string);
-    // LATIN SMALL LETTER L WITH CARON
-    $string = str_replace("\265", '&#318;', $string);
-    // LATIN SMALL LETTER S WITH ACUTE
-    $string = str_replace("\266", '&#347;', $string);
-    // CARON
-    $string = str_replace("\267", '&#711;', $string);
-    // CEDILLA
-    $string = str_replace("\270", '&#184;', $string);
-    // LATIN SMALL LETTER S WITH CARON
-    $string = str_replace("\271", '&#353;', $string);
-    // LATIN SMALL LETTER S WITH CEDILLA
-    $string = str_replace("\272", '&#351;', $string);
-    // LATIN SMALL LETTER T WITH CARON
-    $string = str_replace("\273", '&#357;', $string);
-    // LATIN SMALL LETTER Z WITH ACUTE
-    $string = str_replace("\274", '&#378;', $string);
-    // DOUBLE ACUTE ACCENT
-    $string = str_replace("\275", '&#733;', $string);
-    // LATIN SMALL LETTER Z WITH CARON
-    $string = str_replace("\276", '&#382;', $string);
-    // LATIN SMALL LETTER Z WITH DOT ABOVE
-    $string = str_replace("\277", '&#380;', $string);
-    // LATIN CAPITAL LETTER R WITH ACUTE
-    $string = str_replace("\300", '&#340;', $string);
-    // LATIN CAPITAL LETTER A WITH ACUTE
-    $string = str_replace("\301", '&#193;', $string);
-    // LATIN CAPITAL LETTER A WITH CIRCUMFLEX
-    $string = str_replace("\302", '&#194;', $string);
-    // LATIN CAPITAL LETTER A WITH BREVE
-    $string = str_replace("\303", '&#258;', $string);
-    // LATIN CAPITAL LETTER A WITH DIAERESIS
-    $string = str_replace("\304", '&#196;', $string);
-    // LATIN CAPITAL LETTER L WITH ACUTE
-    $string = str_replace("\305", '&#313;', $string);
-    // LATIN CAPITAL LETTER C WITH ACUTE
-    $string = str_replace("\306", '&#262;', $string);
-    // LATIN CAPITAL LETTER C WITH CEDILLA
-    $string = str_replace("\307", '&#199;', $string);
-    // LATIN CAPITAL LETTER C WITH CARON
-    $string = str_replace("\310", '&#268;', $string);
-    // LATIN CAPITAL LETTER E WITH ACUTE
-    $string = str_replace("\311", '&#201;', $string);
-    // LATIN CAPITAL LETTER E WITH OGONEK
-    $string = str_replace("\312", '&#280;', $string);
-    // LATIN CAPITAL LETTER E WITH DIAERESIS
-    $string = str_replace("\313", '&#203;', $string);
-    // LATIN CAPITAL LETTER E WITH CARON
-    $string = str_replace("\314", '&#282;', $string);
-    // LATIN CAPITAL LETTER I WITH ACUTE
-    $string = str_replace("\315", '&#205;', $string);
-    // LATIN CAPITAL LETTER I WITH CIRCUMFLEX
-    $string = str_replace("\316", '&#206;', $string);
-    // LATIN CAPITAL LETTER D WITH CARON
-    $string = str_replace("\317", '&#270;', $string);
-    // LATIN CAPITAL LETTER D WITH STROKE
-    $string = str_replace("\320", '&#272;', $string);
-    // LATIN CAPITAL LETTER N WITH ACUTE
-    $string = str_replace("\321", '&#323;', $string);
-    // LATIN CAPITAL LETTER N WITH CARON
-    $string = str_replace("\322", '&#327;', $string);
-    // LATIN CAPITAL LETTER O WITH ACUTE
-    $string = str_replace("\323", '&#211;', $string);
-    // LATIN CAPITAL LETTER O WITH CIRCUMFLEX
-    $string = str_replace("\324", '&#212;', $string);
-    // LATIN CAPITAL LETTER O WITH DOUBLE ACUTE
-    $string = str_replace("\325", '&#336;', $string);
-    // LATIN CAPITAL LETTER O WITH DIAERESIS
-    $string = str_replace("\326", '&#214;', $string);
-    // MULTIPLICATION SIGN
-    $string = str_replace("\327", '&#215;', $string);
-    // LATIN CAPITAL LETTER R WITH CARON
-    $string = str_replace("\330", '&#344;', $string);
-    // LATIN CAPITAL LETTER U WITH RING ABOVE
-    $string = str_replace("\331", '&#366;', $string);
-    // LATIN CAPITAL LETTER U WITH ACUTE
-    $string = str_replace("\332", '&#218;', $string);
-    // LATIN CAPITAL LETTER U WITH DOUBLE ACUTE
-    $string = str_replace("\333", '&#368;', $string);
-    // LATIN CAPITAL LETTER U WITH DIAERESIS
-    $string = str_replace("\334", '&#220;', $string);
-    // LATIN CAPITAL LETTER Y WITH ACUTE
-    $string = str_replace("\335", '&#221;', $string);
-    // LATIN CAPITAL LETTER T WITH CEDILLA
-    $string = str_replace("\336", '&#354;', $string);
-    // LATIN SMALL LETTER SHARP S
-    $string = str_replace("\337", '&#223;', $string);
-    // LATIN SMALL LETTER R WITH ACUTE
-    $string = str_replace("\340", '&#341;', $string);
-    // LATIN SMALL LETTER A WITH ACUTE
-    $string = str_replace("\341", '&#225;', $string);
-    // LATIN SMALL LETTER A WITH CIRCUMFLEX
-    $string = str_replace("\342", '&#226;', $string);
-    // LATIN SMALL LETTER A WITH BREVE
-    $string = str_replace("\343", '&#259;', $string);
-    // LATIN SMALL LETTER A WITH DIAERESIS
-    $string = str_replace("\344", '&#228;', $string);
-    // LATIN SMALL LETTER L WITH ACUTE
-    $string = str_replace("\345", '&#314;', $string);
-    // LATIN SMALL LETTER C WITH ACUTE
-    $string = str_replace("\346", '&#263;', $string);
-    // LATIN SMALL LETTER C WITH CEDILLA
-    $string = str_replace("\347", '&#231;', $string);
-    // LATIN SMALL LETTER C WITH CARON
-    $string = str_replace("\350", '&#269;', $string);
-    // LATIN SMALL LETTER E WITH ACUTE
-    $string = str_replace("\351", '&#233;', $string);
-    // LATIN SMALL LETTER E WITH OGONEK
-    $string = str_replace("\352", '&#281;', $string);
-    // LATIN SMALL LETTER E WITH DIAERESIS
-    $string = str_replace("\353", '&#235;', $string);
-    // LATIN SMALL LETTER E WITH CARON
-    $string = str_replace("\354", '&#283;', $string);
-    // LATIN SMALL LETTER I WITH ACUTE
-    $string = str_replace("\355", '&#237;', $string);
-    // LATIN SMALL LETTER I WITH CIRCUMFLEX
-    $string = str_replace("\356", '&#238;', $string);
-    // LATIN SMALL LETTER D WITH CARON
-    $string = str_replace("\357", '&#271;', $string);
-    // LATIN SMALL LETTER D WITH STROKE
-    $string = str_replace("\360", '&#273;', $string);
-    // LATIN SMALL LETTER N WITH ACUTE
-    $string = str_replace("\361", '&#324;', $string);
-    // LATIN SMALL LETTER N WITH CARON
-    $string = str_replace("\362", '&#328;', $string);
-    // LATIN SMALL LETTER O WITH ACUTE
-    $string = str_replace("\363", '&#243;', $string);
-    // LATIN SMALL LETTER O WITH CIRCUMFLEX
-    $string = str_replace("\364", '&#244;', $string);
-    // LATIN SMALL LETTER O WITH DOUBLE ACUTE
-    $string = str_replace("\365", '&#337;', $string);
-    // LATIN SMALL LETTER O WITH DIAERESIS
-    $string = str_replace("\366", '&#246;', $string);
-    // DIVISION SIGN
-    $string = str_replace("\367", '&#247;', $string);
-    // LATIN SMALL LETTER R WITH CARON
-    $string = str_replace("\370", '&#345;', $string);
-    // LATIN SMALL LETTER U WITH RING ABOVE
-    $string = str_replace("\371", '&#367;', $string);
-    // LATIN SMALL LETTER U WITH ACUTE
-    $string = str_replace("\372", '&#250;', $string);
-    // LATIN SMALL LETTER U WITH DOUBLE ACUTE
-    $string = str_replace("\373", '&#369;', $string);
-    // LATIN SMALL LETTER U WITH DIAERESIS
-    $string = str_replace("\374", '&#252;', $string);
-    // LATIN SMALL LETTER Y WITH ACUTE
-    $string = str_replace("\375", '&#253;', $string);
-    // LATIN SMALL LETTER T WITH CEDILLA
-    $string = str_replace("\376", '&#355;', $string);
-    // DOT ABOVE
-    $string = str_replace("\377", '&#729;', $string);
+    set_my_charset();
 
-    return $string;
-}
+    // Variables that allow to use functions without function_exist() calls
+    if (! isset($use_php_recode) || $use_php_recode=="" ) {
+      $use_php_recode=false; }
+    if (! isset($use_php_iconv) || $use_php_iconv=="" ) {
+         $use_php_iconv=false; }
 
-/* 
-   iso-8859-4 is Baltic codeset used in some email clients 
-   instead of iso-8859-13 in Lithuania 
-   only Lithuanian charactes are added.
-*/
+    // Don't do conversion if charset is the same.
+    if ( $charset == strtolower($default_charset) )
+          return htmlspecialchars($string);
 
-function charset_decode_iso_8859_4 ($string) {
-    // latin capital a with ogonek
-    $string = str_replace ("\241", '&#0260;', $string);
-    // latin capital c with caron
-    $string = str_replace ("\310", '&#0268;', $string);
-    // latin capital e with ogonek
-    $string = str_replace ("\312", '&#0280;', $string);
-    // latin capital e with dot above
-    $string = str_replace ("\314", '&#0278;', $string);
-    // latin capital i with ogonek
-    $string = str_replace ("\307", '&#0302;', $string);
-    // latin capital s with caron
-    $string = str_replace ("\251", '&#0352;', $string);
-    // latin capital u with ogonek
-    $string = str_replace ("\331", '&#0370;', $string);
-    // latin capital u with macron
-    $string = str_replace ("\336", '&#0362;', $string);
-    // latin capital z with caron
-    $string = str_replace ("\256", '&#0381;', $string);
-    // latin small a with ogonek
-    $string = str_replace ("\261", '&#0261;', $string);
-    // latin small c with caron
-    $string = str_replace ("\350", '&#0269;', $string);
-    // latin small e with ogonek
-    $string = str_replace ("\352", '&#0281;', $string);
-    // latin small e with dot above
-    $string = str_replace ("\354", '&#0279;', $string);
-    // latin small i with ogonek
-    $string = str_replace ("\347", '&#0303;', $string);
-    // latin small s with caron
-    $string = str_replace ("\271", '&#0353;', $string);
-    // latin small u with ogonek
-    $string = str_replace ("\371", '&#0371;', $string);
-    // latin small u with macron
-    $string = str_replace ("\376", '&#0363;', $string);
-    // latin small z with caron
-    $string = str_replace ("\276", '&#0382;', $string);
-
-    return (charset_decode_iso_8859_1($string));
-}
+    // catch iso-8859-8-i thing
+    if ( $charset == "iso-8859-8-i" )
+              $charset = "iso-8859-8";
 
-/* iso-8859-7 is Greek. */
-function charset_decode_iso_8859_7 ($string) {
-    global $default_charset;
-
-    if (strtolower($default_charset) == 'iso-8859-7') {
+    /*
+     * Recode converts html special characters automatically if you use
+     * 'charset..html' decoding. There is no documented way to put -d option
+     * into php recode function call.
+     */
+    if ( $use_php_recode ) {
+      if ( $default_charset == "utf-8" ) {
+        // other charsets can be converted to utf-8 without loss.
+        // and output string is smaller
+        $string = recode_string($charset . "..utf-8",$string);
+        return htmlspecialchars($string);
+      } else {
+        $string = recode_string($charset . "..html",$string);
+        // recode does not convert single quote, htmlspecialchars does.
+        $string = str_replace("'", '&#039;', $string);
         return $string;
+      }
     }
 
-    /* Only do the slow convert if there are 8-bit characters */
-    if (!ereg("[\200-\377]", $string)) {
-        return $string;
+    // 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 htmlspecialchars($string);
     }
 
-    /* Some diverse characters in the beginning */
-    $string = str_replace("\240", '&#160;', $string);
-    $string = str_replace("\241", '&#8216;', $string);
-    $string = str_replace("\242", '&#8217;', $string);
-    $string = str_replace("\243", '&#163;', $string);
-    $string = str_replace("\246", '&#166;', $string);
-    $string = str_replace("\247", '&#167;', $string);
-    $string = str_replace("\250", '&#168;', $string);
-    $string = str_replace("\251", '&#169;', $string);
-    $string = str_replace("\253", '&#171;', $string);
-    $string = str_replace("\254", '&#172;', $string);
-    $string = str_replace("\255", '&#173;', $string);
-    $string = str_replace("\257", '&#8213;', $string);
-    $string = str_replace("\260", '&#176;', $string);
-    $string = str_replace("\261", '&#177;', $string);
-    $string = str_replace("\262", '&#178;', $string);
-    $string = str_replace("\263", '&#179;', $string);
-
-    /* Horizontal bar (parentheki pavla) */
-    $string = str_replace ("\257", '&#8213;', $string);
+    // If we don't use recode and iconv, we'll do it old way.
 
-    /*
-     * ISO-8859-7 characters from 11/04 (0xB4) to 11/06 (0xB6)
-     * These are Unicode 900-902
-     */
-    $string = preg_replace("/([\264-\266])/e",'&#' . (ord("\\1")+720),$string);
-    
-    /* 11/07 (0xB7) Middle dot is the same in iso-8859-1 */
-    $string = str_replace("\267", '&#183;', $string);
-
-    /*
-     * ISO-8859-7 characters from 11/08 (0xB8) to 11/10 (0xBA)
-     * These are Unicode 900-902
-     */
-    $string = preg_replace("/([\270-\272])/e",'&#' . (ord("\\1")+720),$string);
-
-    /*
-     * 11/11 (0xBB) Right angle quotation mark is the same as in
-     * iso-8859-1
-     */
-    $string = str_replace("\273", '&#187;', $string);
-
-    /* And now the rest of the charset */
-    $string = preg_replace("/([\274-\376])/",'&#'.(ord("\\1")+720),$string);
+    /* All HTML special characters are 7 bit and can be replaced first */
 
-    return $string;
-}
+    $string = htmlspecialchars ($string);
 
-/*
- iso-8859-13 codeset used in Lithuania
- only Lithuanian charactes are added.
-*/
+    /* controls cpu and memory intensive decoding cycles */
+    if (! isset($aggressive_decoding) || $aggressive_decoding=="" ) {
+         $aggressive_decoding=false; }
 
-function charset_decode_iso_8859_13 ($string) {
-    // latin capital a with ogonek
-    $string = str_replace ("\300", '&#0260;', $string);
-    // latin capital c with caron
-    $string = str_replace ("\310", '&#0268;', $string);
-    // latin capital e with ogonek
-    $string = str_replace ("\306", '&#0280;', $string);
-    // latin capital e with dot above
-    $string = str_replace ("\313", '&#0278;', $string);
-    // latin capital i with ogonek
-    $string = str_replace ("\301", '&#0302;', $string);
-    // latin capital s with caron
-    $string = str_replace ("\320", '&#0352;', $string);
-    // latin capital u with ogonek
-    $string = str_replace ("\330", '&#0370;', $string);
-    // latin capital u with macron
-    $string = str_replace ("\333", '&#0362;', $string);
-    // latin capital z with caron
-    $string = str_replace ("\336", '&#0381;', $string);
-    // latin small a with ogonek
-    $string = str_replace ("\340", '&#0261;', $string);
-    // latin small c with caron
-    $string = str_replace ("\350", '&#0269;', $string);
-    // latin small e with ogonek
-    $string = str_replace ("\346", '&#0281;', $string);
-    // latin small e with dot above
-    $string = str_replace ("\353", '&#0279;', $string);
-    // latin small i with ogonek
-    $string = str_replace ("\341", '&#0303;', $string);
-    // latin small s with caron
-    $string = str_replace ("\360", '&#0353;', $string);
-    // latin small u with ogonek
-    $string = str_replace ("\370", '&#0371;', $string);
-    // latin small u with macron
-    $string = str_replace ("\373", '&#0363;', $string);
-    // latin small z with caron
-    $string = str_replace ("\376", '&#0382;', $string);
-
-    return (charset_decode_iso_8859_1($string));
+    $decode=fixcharset($charset);
+    $decodefile=SM_PATH . 'functions/decode/' . $decode . '.php';
+    if (file_exists($decodefile)) {
+      include_once($decodefile);
+      $ret = call_user_func('charset_decode_'.$decode, $string);
+    } else {
+      $ret = $string;
+    }
+    return( $ret );
 }
 
-/*
- * iso-8859-15 is Latin 9 and has very much the same use as Latin 1
- * but has the Euro symbol and some characters needed for French.
+/**
+ * Converts html string to given charset
+ * @param string $string
+ * @param string $charset
+ * @param boolean $htmlencode keep htmlspecialchars encoding
+ * @param string
  */
-function charset_decode_iso_8859_15 ($string) {
-    // Euro sign
-    $string = str_replace ("\244", '&#8364;', $string);
-    // Latin capital letter S with caron
-    $string = str_replace ("\246", '&#352;', $string);
-    // Latin small letter s with caron
-    $string = str_replace ("\250", '&#353;', $string);
-    // Latin capital letter Z with caron
-    $string = str_replace ("\264", '&#381;', $string);
-    // Latin small letter z with caron
-    $string = str_replace ("\270", '&#382;', $string);
-    // Latin capital ligature OE
-    $string = str_replace ("\274", '&#338;', $string);
-    // Latin small ligature oe
-    $string = str_replace ("\275", '&#339;', $string);
-    // Latin capital letter Y with diaeresis
-    $string = str_replace ("\276", '&#376;', $string);
-
-    return (charset_decode_iso_8859_1($string));
+function charset_encode($string,$charset,$htmlencode=true) {
+  global $default_charset;
+
+  // Undo html special chars
+  if (! $htmlencode ) {
+      $string = str_replace(array('&amp;','&gt;','&lt;','&quot;'),array('&','>','<','"'),$string);
+  }
+
+  $encode=fixcharset($charset);
+  $encodefile=SM_PATH . 'functions/encode/' . $encode . '.php';
+  if (file_exists($encodefile)) {
+      include_once($encodefile);
+      $ret = call_user_func('charset_encode_'.$encode, $string);
+  } else {
+      include_once(SM_PATH . 'functions/encode/us_ascii.php');
+      $ret = charset_encode_us_ascii($string);
+  }
+  return( $ret );
 }
 
-/* ISO-8859-5 is Cyrillic */
-function charset_decode_iso_8859_5 ($string) {
-    // Convert to KOI8-R, then return this decoded.
-    $string = convert_cyr_string($string, 'i', 'k');
-    return charset_decode_koi8r($string);
-}
-
-/* Remove all 8 bit characters from all other ISO-8859 character sets */
-function charset_decode_iso_8859_default ($string) {
-    return (strtr($string, "\240\241\242\243\244\245\246\247".
-                    "\250\251\252\253\254\255\256\257".
-                    "\260\261\262\263\264\265\266\267".
-                    "\270\271\272\273\274\275\276\277".
-                    "\300\301\302\303\304\305\306\307".
-                    "\310\311\312\313\314\315\316\317".
-                    "\320\321\322\323\324\325\326\327".
-                    "\330\331\332\333\334\335\336\337".
-                    "\340\341\342\343\344\345\346\347".
-                    "\350\351\352\353\354\355\356\357".
-                    "\360\361\362\363\364\365\366\367".
-                    "\370\371\372\373\374\375\376\377",
-                    "????????????????????????????????????????".
-                    "????????????????????????????????????????".
-                    "????????????????????????????????????????".
-                    "????????"));
-
-}
-
-/*
- * This is the same as ISO-646-NO and is used by some
- * Microsoft programs when sending Norwegian characters
+/**
+ * Combined decoding and encoding functions
+ *
+ * If conversion is done to charset different that utf-8, unsupported symbols
+ * will be replaced with question marks.
+ * @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
+ * @return string converted string
  */
-function charset_decode_ns_4551_1 ($string) {
-    /*
-     * These characters are:
-     * Latin capital letter AE
-     * Latin capital letter O with stroke
-     * Latin capital letter A with ring above
-     * and the same as small letters
-     */
-    return strtr ($string, "[\\]{|}", "ÆØÅæøå");
+function charset_convert($in_charset,$string,$out_charset,$htmlencode=true) {
+    $string=charset_decode($in_charset,$string);
+    $string=charset_encode($string,$out_charset,$htmlencode);
+    return $string;
 }
 
-/*
- * KOI8-R is used to encode Russian mail (Cyrrilic). Defined in RFC
- * 1489.
+/**
+ * Makes charset name suitable for decoding cycles
+ *
+ * @param string $charset Name of charset
+ * @return string $charset Adjusted name of charset
  */
-function charset_decode_koi8r ($string) {
-    global $default_charset;
+function fixcharset($charset) {
+    // minus removed from function names
+    $charset=str_replace('-','_',$charset);
 
-    if ($default_charset == 'koi8-r') {
-        return $string;
-    }
+    // windows-125x and cp125x charsets
+    $charset=str_replace('windows_','cp',$charset);
 
-    /*
-     * Convert to Unicode HTML entities.
-     * This code is rather ineffective.
-     */
-    $string = str_replace("\200", '&#9472;', $string);
-    $string = str_replace("\201", '&#9474;', $string);
-    $string = str_replace("\202", '&#9484;', $string);
-    $string = str_replace("\203", '&#9488;', $string);
-    $string = str_replace("\204", '&#9492;', $string);
-    $string = str_replace("\205", '&#9496;', $string);
-    $string = str_replace("\206", '&#9500;', $string);
-    $string = str_replace("\207", '&#9508;', $string);
-    $string = str_replace("\210", '&#9516;', $string);
-    $string = str_replace("\211", '&#9524;', $string);
-    $string = str_replace("\212", '&#9532;', $string);
-    $string = str_replace("\213", '&#9600;', $string);
-    $string = str_replace("\214", '&#9604;', $string);
-    $string = str_replace("\215", '&#9608;', $string);
-    $string = str_replace("\216", '&#9612;', $string);
-    $string = str_replace("\217", '&#9616;', $string);
-    $string = str_replace("\220", '&#9617;', $string);
-    $string = str_replace("\221", '&#9618;', $string);
-    $string = str_replace("\222", '&#9619;', $string);
-    $string = str_replace("\223", '&#8992;', $string);
-    $string = str_replace("\224", '&#9632;', $string);
-    $string = str_replace("\225", '&#8729;', $string);
-    $string = str_replace("\226", '&#8730;', $string);
-    $string = str_replace("\227", '&#8776;', $string);
-    $string = str_replace("\230", '&#8804;', $string);
-    $string = str_replace("\231", '&#8805;', $string);
-    $string = str_replace("\232", '&#160;', $string);
-    $string = str_replace("\233", '&#8993;', $string);
-    $string = str_replace("\234", '&#176;', $string);
-    $string = str_replace("\235", '&#178;', $string);
-    $string = str_replace("\236", '&#183;', $string);
-    $string = str_replace("\237", '&#247;', $string);
-    $string = str_replace("\240", '&#9552;', $string);
-    $string = str_replace("\241", '&#9553;', $string);
-    $string = str_replace("\242", '&#9554;', $string);
-    $string = str_replace("\243", '&#1105;', $string);
-    $string = str_replace("\244", '&#9555;', $string);
-    $string = str_replace("\245", '&#9556;', $string);
-    $string = str_replace("\246", '&#9557;', $string);
-    $string = str_replace("\247", '&#9558;', $string);
-    $string = str_replace("\250", '&#9559;', $string);
-    $string = str_replace("\251", '&#9560;', $string);
-    $string = str_replace("\252", '&#9561;', $string);
-    $string = str_replace("\253", '&#9562;', $string);
-    $string = str_replace("\254", '&#9563;', $string);
-    $string = str_replace("\255", '&#9564;', $string);
-    $string = str_replace("\256", '&#9565;', $string);
-    $string = str_replace("\257", '&#9566;', $string);
-    $string = str_replace("\260", '&#9567;', $string);
-    $string = str_replace("\261", '&#9568;', $string);
-    $string = str_replace("\262", '&#9569;', $string);
-    $string = str_replace("\263", '&#1025;', $string);
-    $string = str_replace("\264", '&#9570;', $string);
-    $string = str_replace("\265", '&#9571;', $string);
-    $string = str_replace("\266", '&#9572;', $string);
-    $string = str_replace("\267", '&#9573;', $string);
-    $string = str_replace("\270", '&#9574;', $string);
-    $string = str_replace("\271", '&#9575;', $string);
-    $string = str_replace("\272", '&#9576;', $string);
-    $string = str_replace("\273", '&#9577;', $string);
-    $string = str_replace("\274", '&#9578;', $string);
-    $string = str_replace("\275", '&#9579;', $string);
-    $string = str_replace("\276", '&#9580;', $string);
-    $string = str_replace("\277", '&#169;', $string);
-    $string = str_replace("\300", '&#1102;', $string);
-    $string = str_replace("\301", '&#1072;', $string);
-    $string = str_replace("\302", '&#1073;', $string);
-    $string = str_replace("\303", '&#1094;', $string);
-    $string = str_replace("\304", '&#1076;', $string);
-    $string = str_replace("\305", '&#1077;', $string);
-    $string = str_replace("\306", '&#1092;', $string);
-    $string = str_replace("\307", '&#1075;', $string);
-    $string = str_replace("\310", '&#1093;', $string);
-    $string = str_replace("\311", '&#1080;', $string);
-    $string = str_replace("\312", '&#1081;', $string);
-    $string = str_replace("\313", '&#1082;', $string);
-    $string = str_replace("\314", '&#1083;', $string);
-    $string = str_replace("\315", '&#1084;', $string);
-    $string = str_replace("\316", '&#1085;', $string);
-    $string = str_replace("\317", '&#1086;', $string);
-    $string = str_replace("\320", '&#1087;', $string);
-    $string = str_replace("\321", '&#1103;', $string);
-    $string = str_replace("\322", '&#1088;', $string);
-    $string = str_replace("\323", '&#1089;', $string);
-    $string = str_replace("\324", '&#1090;', $string);
-    $string = str_replace("\325", '&#1091;', $string);
-    $string = str_replace("\326", '&#1078;', $string);
-    $string = str_replace("\327", '&#1074;', $string);
-    $string = str_replace("\330", '&#1100;', $string);
-    $string = str_replace("\331", '&#1099;', $string);
-    $string = str_replace("\332", '&#1079;', $string);
-    $string = str_replace("\333", '&#1096;', $string);
-    $string = str_replace("\334", '&#1101;', $string);
-    $string = str_replace("\335", '&#1097;', $string);
-    $string = str_replace("\336", '&#1095;', $string);
-    $string = str_replace("\337", '&#1098;', $string);
-    $string = str_replace("\340", '&#1070;', $string);
-    $string = str_replace("\341", '&#1040;', $string);
-    $string = str_replace("\342", '&#1041;', $string);
-    $string = str_replace("\343", '&#1062;', $string);
-    $string = str_replace("\344", '&#1044;', $string);
-    $string = str_replace("\345", '&#1045;', $string);
-    $string = str_replace("\346", '&#1060;', $string);
-    $string = str_replace("\347", '&#1043;', $string);
-    $string = str_replace("\350", '&#1061;', $string);
-    $string = str_replace("\351", '&#1048;', $string);
-    $string = str_replace("\352", '&#1049;', $string);
-    $string = str_replace("\353", '&#1050;', $string);
-    $string = str_replace("\354", '&#1051;', $string);
-    $string = str_replace("\355", '&#1052;', $string);
-    $string = str_replace("\356", '&#1053;', $string);
-    $string = str_replace("\357", '&#1054;', $string);
-    $string = str_replace("\360", '&#1055;', $string);
-    $string = str_replace("\361", '&#1071;', $string);
-    $string = str_replace("\362", '&#1056;', $string);
-    $string = str_replace("\363", '&#1057;', $string);
-    $string = str_replace("\364", '&#1058;', $string);
-    $string = str_replace("\365", '&#1059;', $string);
-    $string = str_replace("\366", '&#1046;', $string);
-    $string = str_replace("\367", '&#1042;', $string);
-    $string = str_replace("\370", '&#1068;', $string);
-    $string = str_replace("\371", '&#1067;', $string);
-    $string = str_replace("\372", '&#1047;', $string);
-    $string = str_replace("\373", '&#1064;', $string);
-    $string = str_replace("\374", '&#1069;', $string);
-    $string = str_replace("\375", '&#1065;', $string);
-    $string = str_replace("\376", '&#1063;', $string);
-    $string = str_replace("\377", '&#1066;', $string);
+    // ibm > cp
+    $charset=str_replace('ibm','cp',$charset);
 
-    return $string;
-}
+    // iso-8859-8-i -> iso-8859-8
+    // use same cycle until I'll find differences
+    $charset=str_replace('iso_8859_8_i','iso_8859_8',$charset);
 
+    return $charset;
+}
 
-/*
+/**
  * Set up the language to be output
  * if $do_search is true, then scan the browser information
  * for a possible language that we know
+ *
+ * Function sets system locale environment (LC_ALL, LANG, LANGUAGE),
+ * gettext translation bindings and html header information.
+ *
+ * Function returns error codes, if there is some fatal error.
+ *  0 = no error,
+ *  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.
+ * @return int function execution error codes.
  */
-function set_up_language($sm_language, $do_search = false) {
+function set_up_language($sm_language, $do_search = false, $default = false) {
 
     static $SetupAlready = 0;
     global $use_gettext, $languages,
-           $squirrelmail_language, $squirrelmail_default_language,
-           $sm_notAlias;
+           $squirrelmail_language, $squirrelmail_default_language, $default_charset,
+           $sm_notAlias, $username, $data_dir;
 
     if ($SetupAlready) {
         return;
     }
+
     $SetupAlready = TRUE;
+    sqgetGlobalVar('HTTP_ACCEPT_LANGUAGE',  $accept_lang, SQ_SERVER);
 
-    if ($do_search && ! $sm_language && isset($_SERVER['HTTP_ACCEPT_LANGUAGE'])) {
-        $sm_language = substr($_SERVER['HTTP_ACCEPT_LANGUAGE'], 0, 2);
+    if ($do_search && ! $sm_language && isset($accept_lang)) {
+        $sm_language = substr($accept_lang, 0, 2);
     }
-    
-    if (!$sm_language && isset($squirrelmail_default_language)) {
+
+    if ((!$sm_language||$default) && isset($squirrelmail_default_language)) {
         $squirrelmail_language = $squirrelmail_default_language;
         $sm_language = $squirrelmail_default_language;
     }
     $sm_notAlias = $sm_language;
+
+    // Catching removed translation
+    // System reverts to English translation if user prefs contain translation
+    // that is not available in $languages array
+    if (!isset($languages[$sm_notAlias])) {
+      $sm_notAlias="en_US";
+    }
+
     while (isset($languages[$sm_notAlias]['ALIAS'])) {
         $sm_notAlias = $languages[$sm_notAlias]['ALIAS'];
     }
@@ -811,51 +231,111 @@ function set_up_language($sm_language, $do_search = false) {
          $use_gettext &&
          $sm_language != '' &&
          isset($languages[$sm_notAlias]['CHARSET']) ) {
-        bindtextdomain( 'squirrelmail', '../locale/' );
+        bindtextdomain( 'squirrelmail', SM_PATH . 'locale/' );
         textdomain( 'squirrelmail' );
+        if (function_exists('bind_textdomain_codeset')) {
+          if ($sm_notAlias == 'ja_JP') {
+            bind_textdomain_codeset ("squirrelmail", 'EUC-JP');
+            } else {
+              bind_textdomain_codeset ("squirrelmail", $languages[$sm_notAlias]['CHARSET'] );
+            }
+        }
+        if (isset($languages[$sm_notAlias]['LOCALE'])){
+          $longlocale=$languages[$sm_notAlias]['LOCALE'];
+        } else {
+          $longlocale=$sm_notAlias;
+        }
         if ( !ini_get('safe_mode') &&
-             getenv( 'LC_ALL' ) != $sm_notAlias ) {
-            putenv( "LC_ALL=$sm_notAlias" );
-            putenv( "LANG=$sm_notAlias" );
-            putenv( "LANGUAGE=$sm_notAlias" );
+             getenv( 'LC_ALL' ) != $longlocale ) {
+            putenv( "LC_ALL=$longlocale" );
+            putenv( "LANG=$longlocale" );
+            putenv( "LANGUAGE=$longlocale" );
+        }
+        setlocale(LC_ALL, $longlocale);
+
+        // Set text direction/alignment variables
+        if (isset($languages[$sm_notAlias]['DIR']) &&
+            $languages[$sm_notAlias]['DIR'] == 'rtl') {
+          /**
+           * Text direction
+           * @global string $text_direction
+           */
+          $text_direction='rtl';
+          /**
+           * Left alignment
+           * @global string $left_align
+           */
+          $left_align='right';
+          /**
+           * Right alignment
+           * @global string $right_align
+           */
+          $right_align='left';
+        } else {
+          $text_direction='ltr';
+          $left_align='left';
+          $right_align='right';
         }
-        setlocale(LC_ALL, $sm_notAlias);
+
         $squirrelmail_language = $sm_notAlias;
-        if ($squirrelmail_language == 'ja_JP' && function_exists('mb_detect_encoding') ) {
+        if ($squirrelmail_language == 'ja_JP') {
             header ('Content-Type: text/html; charset=EUC-JP');
             if (!function_exists('mb_internal_encoding')) {
-                echo _("You need to have php4 installed with the multibyte string function enabled (using configure option --with-mbstring).");
+              // Error messages can't be displayed here
+              $error = 1;
+              // Revert to English if possible.
+              if (function_exists('setPref')  && $username!='' && $data_dir!="") {
+                setPref($data_dir, $username, 'language', "en_US");
+                $error = 2;
+              }
+              // stop further execution in order not to get php errors on mb_internal_encoding().
+              return $error;
+            }
+            if (function_exists('mb_language')) {
+                mb_language('Japanese');
             }
             mb_internal_encoding('EUC-JP');
             mb_http_output('pass');
+        } elseif ($squirrelmail_language == 'en_US') {
+            header( 'Content-Type: text/html; charset=' . $default_charset );
         } else {
-        header( 'Content-Type: text/html; charset=' . $languages[$sm_notAlias]['CHARSET'] );
+            header( 'Content-Type: text/html; charset=' . $languages[$sm_notAlias]['CHARSET'] );
+        }
     }
-}
+    return 0;
 }
 
+/**
+ * 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_default_language
+ * and $default_charset combination provided in SquirrelMail config is also tested.
+ *
+ * There can be a $default_charset setting in the
+ * config.php file, but the user may have a different language
+ * selected for a user interface. This function checks the
+ * language selected by the user and tags the outgoing messages
+ * with the appropriate charset corresponding to the language
+ * selection. This is "more right" (tm), than just stamping the
+ * message blindly with the system-wide $default_charset.
+ */
 function set_my_charset(){
-
-    /*
-     * There can be a $default_charset setting in the
-     * config.php file, but the user may have a different language
-     * selected for a user interface. This function checks the
-     * language selected by the user and tags the outgoing messages
-     * with the appropriate charset corresponding to the language
-     * selection. This is "more right" (tm), than just stamping the
-     * message blindly with the system-wide $default_charset.
-     */
-    global $data_dir, $username, $default_charset, $languages;
+    global $data_dir, $username, $default_charset, $languages, $squirrelmail_default_language;
 
     $my_language = getPref($data_dir, $username, 'language');
     if (!$my_language) {
-        return;
+        $my_language = $squirrelmail_default_language ;
+    }
+    // Catch removed translation
+    if (!isset($languages[$my_language])) {
+      $my_language="en_US";
     }
     while (isset($languages[$my_language]['ALIAS'])) {
         $my_language = $languages[$my_language]['ALIAS'];
     }
     $my_charset = $languages[$my_language]['CHARSET'];
-    if ($my_charset) {
+    if ($my_language!='en_US') {
         $default_charset = $my_charset;
     }
 }
@@ -868,156 +348,291 @@ if (! isset($squirrelmail_language)) {
     $squirrelmail_language = '';
 }
 
-/* This array specifies the available languages. */
-
-// The glibc locale is ca_ES.
+/**
+ * Array specifies the available translations.
+ *
+ * Structure of array:
+ * $languages['language']['variable'] = 'value'
+ *
+ * Possible 'variable' names:
+ *  NAME      - Translation name in English
+ *  CHARSET   - Encoding used by translation
+ *  ALIAS     - used when 'language' is only short name and 'value' should provide long language name
+ *  ALTNAME   - Native translation name. Any 8bit symbols must be html encoded.
+ *  LOCALE    - Full locale name (in xx_XX.charset format)
+ *  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
+ *
+ * Each 'language' definition requires NAME+CHARSET or ALIAS variables.
+ *
+ * @name $languages
+ * @global array $languages
+ */
+$languages['bg_BG']['NAME']    = 'Bulgarian';
+$languages['bg_BG']['ALTNAME'] = '&#1041;&#1098;&#1083;&#1075;&#1072;&#1088;&#1089;&#1082;&#1080;';
+$languages['bg_BG']['CHARSET'] = 'windows-1251';
+$languages['bg_BG']['LOCALE']  = 'bg_BG.CP1251';
+$languages['bg']['ALIAS'] = 'bg_BG';
 
 $languages['ca_ES']['NAME']    = 'Catalan';
 $languages['ca_ES']['CHARSET'] = 'iso-8859-1';
+$languages['ca_ES']['LOCALE']  = 'ca_ES.ISO8859-1';
 $languages['ca']['ALIAS'] = 'ca_ES';
 
 $languages['cs_CZ']['NAME']    = 'Czech';
+$languages['cs_CZ']['ALTNAME'] = '&#268;e&scaron;tina';
 $languages['cs_CZ']['CHARSET'] = 'iso-8859-2';
+$languages['cs_CZ']['LOCALE']  = 'cs_CZ.ISO8859-2';
 $languages['cs']['ALIAS']      = 'cs_CZ';
 
-// Danish locale is da_DK.
+$languages['cy_GB']['NAME']    = 'Welsh';
+$languages['cy_GB']['ALTNAME'] = 'Cymraeg';
+$languages['cy_GB']['CHARSET'] = 'iso-8859-1';
+$languages['cy_GB']['LOCALE']  = 'cy_GB.ISO8859-1';
+$languages['cy']['ALIAS'] = 'cy_GB';
 
+// Danish locale is da_DK.
 $languages['da_DK']['NAME']    = 'Danish';
+$languages['da_DK']['ALTNAME'] = 'Dansk';
 $languages['da_DK']['CHARSET'] = 'iso-8859-1';
+$languages['da_DK']['LOCALE']  = 'da_DK.ISO8859-1';
 $languages['da']['ALIAS'] = 'da_DK';
 
-$languages['de_DE']['NAME']    = 'Deutsch';
+$languages['de_DE']['NAME']    = 'German';
+$languages['de_DE']['ALTNAME']    = 'Deutsch';
 $languages['de_DE']['CHARSET'] = 'iso-8859-1';
+$languages['de_DE']['LOCALE']  = 'de_DE.ISO8859-1';
 $languages['de']['ALIAS'] = 'de_DE';
 
-// There is no en_EN! There is en_US, en_BR, en_AU, and so forth, 
-// but who cares about !US, right? Right? :)
+$languages['el_GR']['NAME']    = 'Greek';
+$languages['el_GR']['ALTNAME'] = '&Epsilon;&lambda;&lambda;&eta;&nu;&iota;&kappa;&#940;';
+$languages['el_GR']['CHARSET'] = 'iso-8859-7';
+$languages['el_GR']['LOCALE']  = 'el_GR.ISO8859-7';
+$languages['el']['ALIAS'] = 'el_GR';
+
+$languages['en_GB']['NAME']    = 'British';
+$languages['en_GB']['CHARSET'] = 'iso-8859-15';
+$languages['en_GB']['LOCALE']  = 'en_GB.ISO8859-15';
 
 $languages['en_US']['NAME']    = 'English';
 $languages['en_US']['CHARSET'] = 'iso-8859-1';
+$languages['en_US']['LOCALE']  = 'en_US.ISO8859-1';
 $languages['en']['ALIAS'] = 'en_US';
 
 $languages['es_ES']['NAME']    = 'Spanish';
+$languages['es_ES']['ALTNAME'] = 'Espa&ntilde;ol';
 $languages['es_ES']['CHARSET'] = 'iso-8859-1';
+$languages['es_ES']['LOCALE']  = 'es_ES.ISO8859-1';
 $languages['es']['ALIAS'] = 'es_ES';
 
 $languages['et_EE']['NAME']    = 'Estonian';
 $languages['et_EE']['CHARSET'] = 'iso-8859-15';
+$languages['et_EE']['LOCALE']  = 'et_EE.ISO8859-15';
 $languages['et']['ALIAS'] = 'et_EE';
 
+$languages['eu_ES']['NAME']    = 'Basque';
+$languages['eu_ES']['CHARSET'] = 'iso-8859-1';
+$languages['eu_ES']['LOCALE']  = 'eu_ES.ISO8859-1';
+$languages['eu']['ALIAS'] = 'eu_ES';
+
+$languages['fo_FO']['NAME']    = 'Faroese';
+$languages['fo_FO']['CHARSET'] = 'iso-8859-1';
+$languages['fo_FO']['LOCALE']  = 'fo_FO.ISO8859-1';
+$languages['fo']['ALIAS'] = 'fo_FO';
+
 $languages['fi_FI']['NAME']    = 'Finnish';
+$languages['fi_FI']['ALTNAME'] = 'Suomi';
 $languages['fi_FI']['CHARSET'] = 'iso-8859-1';
+$languages['fi_FI']['LOCALE']  = 'fi_FI.ISO8859-1';
 $languages['fi']['ALIAS'] = 'fi_FI';
 
 $languages['fr_FR']['NAME']    = 'French';
+$languages['fr_FR']['ALTNAME'] = 'Fran&#231;ais';
 $languages['fr_FR']['CHARSET'] = 'iso-8859-1';
+$languages['fr_FR']['LOCALE']  = 'fr_FR.ISO8859-1';
 $languages['fr']['ALIAS'] = 'fr_FR';
 
 $languages['hr_HR']['NAME']    = 'Croatian';
 $languages['hr_HR']['CHARSET'] = 'iso-8859-2';
+$languages['hr_HR']['LOCALE']  = 'hr_HR.ISO8859-2';
 $languages['hr']['ALIAS'] = 'hr_HR';
 
 $languages['hu_HU']['NAME']    = 'Hungarian';
+$languages['hu_HU']['ALTNAME'] = 'Magyar';
 $languages['hu_HU']['CHARSET'] = 'iso-8859-2';
+$languages['hu_HU']['LOCALE']  = 'hu_HU.ISO8859-2';
 $languages['hu']['ALIAS'] = 'hu_HU';
 
 $languages['id_ID']['NAME']    = 'Indonesian';
+$languages['id_ID']['ALTNAME'] = 'Bahasa Indonesia';
 $languages['id_ID']['CHARSET'] = 'iso-8859-1';
+$languages['id_ID']['LOCALE']  = 'id_ID.ISO8859-1';
 $languages['id']['ALIAS'] = 'id_ID';
 
 $languages['is_IS']['NAME']    = 'Icelandic';
+$languages['is_IS']['ALTNAME'] = '&Iacute;slenska';
 $languages['is_IS']['CHARSET'] = 'iso-8859-1';
+$languages['is_IS']['LOCALE']  = 'is_IS.ISO8859-1';
 $languages['is']['ALIAS'] = 'is_IS';
 
 $languages['it_IT']['NAME']    = 'Italian';
 $languages['it_IT']['CHARSET'] = 'iso-8859-1';
+$languages['it_IT']['LOCALE']  = 'it_IT.ISO8859-1';
 $languages['it']['ALIAS'] = 'it_IT';
 
 $languages['ja_JP']['NAME']    = 'Japanese';
+$languages['ja_JP']['ALTNAME'] = '&#26085;&#26412;&#35486;';
 $languages['ja_JP']['CHARSET'] = 'iso-2022-jp';
-$languages['ja_JP']['XTRA_CODE'] = 'japanese_charset_xtra';
+$languages['ja_JP']['LOCALE'] = 'ja_JP.EUC-JP';
+$languages['ja_JP']['XTRA_CODE'] = 'japanese_xtra';
 $languages['ja']['ALIAS'] = 'ja_JP';
 
 $languages['ko_KR']['NAME']    = 'Korean';
 $languages['ko_KR']['CHARSET'] = 'euc-KR';
-$languages['ko_KR']['XTRA_CODE'] = 'korean_charset_xtra';
+$languages['ko_KR']['LOCALE']  = 'ko_KR.EUC-KR';
+$languages['ko_KR']['XTRA_CODE'] = 'korean_xtra';
 $languages['ko']['ALIAS'] = 'ko_KR';
 
+$languages['lt_LT']['NAME']    = 'Lithuanian';
+$languages['lt_LT']['ALTNAME'] = 'Lietuvi&#371;';
+$languages['lt_LT']['CHARSET'] = 'utf-8';
+$languages['lt_LT']['LOCALE'] = 'lt_LT.UTF-8';
+$languages['lt']['ALIAS'] = 'lt_LT';
+
 $languages['nl_NL']['NAME']    = 'Dutch';
+$languages['nl_NL']['ALTNAME'] = 'Nederlands';
 $languages['nl_NL']['CHARSET'] = 'iso-8859-1';
+$languages['nl_NL']['LOCALE']  = 'nl_NL.ISO8859-1';
 $languages['nl']['ALIAS'] = 'nl_NL';
 
-$languages['no_NO']['NAME']    = 'Norwegian (Bokm&aring;l)';
-$languages['no_NO']['CHARSET'] = 'iso-8859-1';
-$languages['no']['ALIAS'] = 'no_NO';
+$languages['ms_MY']['NAME']    = 'Malay';
+$languages['ms_MY']['ALTNAME'] = 'Bahasa Melayu';
+$languages['ms_MY']['CHARSET'] = 'iso-8859-1';
+$languages['ms_MY']['LOCALE']  = 'ms_MY.ISO8859-1';
+$languages['my']['ALIAS'] = 'ms_MY';
+
+$languages['nb_NO']['NAME']    = 'Norwegian (Bokm&aring;l)';
+$languages['nb_NO']['ALTNAME'] = 'Norsk (Bokm&aring;l)';
+$languages['nb_NO']['CHARSET'] = 'iso-8859-1';
+$languages['nb_NO']['LOCALE']  = 'nb_NO.ISO8859-1';
+$languages['nb']['ALIAS'] = 'nb_NO';
+
 $languages['nn_NO']['NAME']    = 'Norwegian (Nynorsk)';
+$languages['nn_NO']['ALTNAME'] = 'Norsk (Nynorsk)';
 $languages['nn_NO']['CHARSET'] = 'iso-8859-1';
+$languages['nn_NO']['LOCALE']  = 'nn_NO.ISO8859-1';
 
 $languages['pl_PL']['NAME']    = 'Polish';
+$languages['pl_PL']['ALTNAME'] = 'Polski';
 $languages['pl_PL']['CHARSET'] = 'iso-8859-2';
+$languages['pl_PL']['LOCALE']  = 'pl_PL.ISO8859-2';
 $languages['pl']['ALIAS'] = 'pl_PL';
 
 $languages['pt_PT']['NAME'] = 'Portuguese (Portugal)';
 $languages['pt_PT']['CHARSET'] = 'iso-8859-1';
+$languages['pt_PT']['LOCALE']  = 'pt_PT.ISO8859-1';
+$languages['pt']['ALIAS'] = 'pt_PT';
+
 $languages['pt_BR']['NAME']    = 'Portuguese (Brazil)';
+$languages['pt_BR']['ALTNAME'] = 'Portugu&ecirc;s do Brasil';
 $languages['pt_BR']['CHARSET'] = 'iso-8859-1';
-$languages['pt']['ALIAS'] = 'pt_PT';
+$languages['pt_BR']['LOCALE']  = 'pt_BR.ISO8859-1';
+
+$languages['ro_RO']['NAME']    = 'Romanian';
+$languages['ro_RO']['ALTNAME'] = 'Rom&acirc;n&#259;';
+$languages['ro_RO']['CHARSET'] = 'iso-8859-2';
+$languages['ro_RO']['LOCALE']  = 'ro_RO.ISO8859-2';
+$languages['ro']['ALIAS'] = 'ro_RO';
 
 $languages['ru_RU']['NAME']    = 'Russian';
-$languages['ru_RU']['CHARSET'] = 'koi8-r';
+$languages['ru_RU']['ALTNAME'] = '&#1056;&#1091;&#1089;&#1089;&#1082;&#1080;&#1081;';
+$languages['ru_RU']['CHARSET'] = 'utf-8';
+$languages['ru_RU']['LOCALE']  = 'ru_RU.UTF-8';
 $languages['ru']['ALIAS'] = 'ru_RU';
 
+$languages['sk_SK']['NAME']    = 'Slovak';
+$languages['sk_SK']['CHARSET'] = 'iso-8859-2';
+$languages['sk_SK']['LOCALE']  = 'sk_SK.ISO8859-2';
+$languages['sk']['ALIAS'] = 'sk_SK';
+
+$languages['sl_SI']['NAME']    = 'Slovenian';
+$languages['sl_SI']['ALTNAME'] = 'Sloven&scaron;&#269;ina';
+$languages['sl_SI']['CHARSET'] = 'iso-8859-2';
+$languages['sl_SI']['LOCALE']  = 'sl_SI.ISO8859-2';
+$languages['sl']['ALIAS'] = 'sl_SI';
+
 $languages['sr_YU']['NAME']    = 'Serbian';
+$languages['sr_YU']['ALTNAME'] = 'Srpski';
 $languages['sr_YU']['CHARSET'] = 'iso-8859-2';
+$languages['sr_YU']['LOCALE']  = 'sr_YU.ISO8859-2';
 $languages['sr']['ALIAS'] = 'sr_YU';
 
 $languages['sv_SE']['NAME']    = 'Swedish';
+$languages['sv_SE']['ALTNAME'] = 'Svenska';
 $languages['sv_SE']['CHARSET'] = 'iso-8859-1';
+$languages['sv_SE']['LOCALE']  = 'sv_SE.ISO8859-1';
 $languages['sv']['ALIAS'] = 'sv_SE';
 
+$languages['th_TH']['NAME']    = 'Thai';
+$languages['th_TH']['CHARSET'] = 'tis-620';
+$languages['th_TH']['LOCALE']  = 'th_TH.TIS-620';
+$languages['th']['ALIAS'] = 'th_TH';
+
+$languages['tl_PH']['NAME']    = 'Tagalog';
+$languages['tl_PH']['CHARSET'] = 'iso-8859-1';
+$languages['tl_PH']['LOCALE']  = 'tl_PH.ISO8859-1';
+$languages['tl']['ALIAS'] = 'tl_PH';
+
 $languages['tr_TR']['NAME']    = 'Turkish';
 $languages['tr_TR']['CHARSET'] = 'iso-8859-9';
+$languages['tr_TR']['LOCALE']  = 'tr_TR.ISO8859-9';
 $languages['tr']['ALIAS'] = 'tr_TR';
 
-$languages['zh_TW']['NAME']    = 'Taiwan';
+$languages['zh_TW']['NAME']    = 'Chinese Trad';
 $languages['zh_TW']['CHARSET'] = 'big5';
+$languages['zh_TW']['LOCALE']  = 'zh_TW.BIG5';
 $languages['tw']['ALIAS'] = 'zh_TW';
 
-$languages['zh_TW']['NAME']    = 'Chinese';
-$languages['zh_TW']['CHARSET'] = 'gb2312';
-$languages['tw']['ALIAS'] = 'zh_CN';
-
-$languages['sk_SK']['NAME']     = 'Slovak';
-$languages['sk_SK']['CHARSET']  = 'iso-8859-2';
-$languages['sk']['ALIAS']       = 'sk_SK';
-
-$languages['ro_RO']['NAME']    = 'Romanian';
-$languages['ro_RO']['CHARSET'] = 'iso-8859-2';
-$languages['ro']['ALIAS'] = 'ro_RO';
-
-$languages['th_TH']['NAME']    = 'Thai';
-$languages['th_TH']['CHARSET'] = 'tis-620';
-$languages['th']['ALIAS'] = 'th_TH';
+$languages['zh_CN']['NAME']    = 'Chinese Simp';
+$languages['zh_CN']['CHARSET'] = 'gb2312';
+$languages['zh_CN']['LOCALE']  = 'zh_CN.GB2312';
+$languages['cn']['ALIAS'] = 'zh_CN';
 
-$languages['lt_LT']['NAME']    = 'Lithuanian';
-$languages['lt_LT']['CHARSET'] = 'iso-8859-13';
-$languages['lt']['ALIAS'] = 'lt_LT';
+$languages['uk_UA']['NAME']    = 'Ukrainian';
+$languages['uk_UA']['CHARSET'] = 'koi8-u';
+$languages['uk_UA']['LOCALE']  = 'uk_UA.KOI8-U';
+$languages['uk']['ALIAS'] = 'uk_UA';
 
-$languages['sl_SI']['NAME']    = 'Slovenian';
-$languages['sl_SI']['CHARSET'] = 'iso-8859-2';
-$languages['sl']['ALIAS'] = 'sl_SI';
+$languages['ru_UA']['NAME']    = 'Russian (Ukrainian)';
+$languages['ru_UA']['CHARSET'] = 'koi8-r';
+$languages['ru_UA']['LOCALE']  = 'ru_UA.KOI8-R';
 
-$languages['bg_BG']['NAME']    = 'Bulgarian';
-$languages['bg_BG']['CHARSET'] = 'windows-1251';
-$languages['bg']['ALIAS'] = 'bg_BG';
+/*
+$languages['vi_VN']['NAME']    = 'Vietnamese';
+$languages['vi_VN']['CHARSET'] = 'utf-8';
+$languages['vi']['ALIAS'] = 'vi_VN';
+*/
 
 // Right to left languages
-
-$languages['he_HE']['NAME']    = 'Hebrew';
-$languages['he_HE']['CHARSET'] = 'windows-1255';
-$languages['he_HE']['DIR']     = 'rtl';
-$languages['he']['ALIAS']      = 'he_HE';
-
+$languages['ar']['NAME']    = 'Arabic';
+$languages['ar']['CHARSET'] = 'windows-1256';
+$languages['ar']['DIR']     = 'rtl';
+
+$languages['fa_IR']['NAME']    = 'Farsi';
+$languages['fa_IR']['CHARSET'] = 'utf-8';
+$languages['fa_IR']['DIR']     = 'rtl';
+$languages['fa_IR']['LOCALE']  = 'fa_IR.UTF-8';
+$languages['fa']['ALIAS']      = 'fa_IR';
+
+$languages['he_IL']['NAME']    = 'Hebrew';
+$languages['he_IL']['CHARSET'] = 'windows-1255';
+$languages['he_IL']['LOCALE']  = 'he_IL.CP1255';
+$languages['he_IL']['DIR']     = 'rtl';
+$languages['he']['ALIAS']      = 'he_IL';
+
+$languages['ug']['NAME']    = 'Uighur';
+$languages['ug']['CHARSET'] = 'utf-8';
+$languages['ug']['DIR']     = 'rtl';
 
 /* Detect whether gettext is installed. */
 $gettext_flags = 0;
@@ -1042,16 +657,28 @@ elseif ($gettext_flags == 0) {
 } else {
     /* Uh-ho.  A weird install */
     if (! $gettext_flags & 1) {
+      /**
+       * Function is used as replacement in broken installs
+       * @ignore
+       */
         function _($str) {
             return $str;
         }
     }
     if (! $gettext_flags & 2) {
+      /**
+       * Function is used as replacement in broken installs
+       * @ignore
+       */
         function bindtextdomain() {
             return;
         }
     }
     if (! $gettext_flags & 4) {
+      /**
+       * Function is used as replacemet in broken installs
+       * @ignore
+       */
         function textdomain() {
             return;
         }
@@ -1059,35 +686,53 @@ elseif ($gettext_flags == 0) {
 }
 
 
-/*
+/**
  * Japanese charset extra function
  *
+ * Action performed by function is defined by first argument.
+ * Default return value is defined by second argument.
+ * Use of third argument depends on action.
+ *
+ * @param string $action action performed by this function.
+ *    possible values:
+ *  decode - convert returned string to euc-jp. third argument unused
+ *  encode - convert returned string to jis. third argument unused
+ *  strimwidth - third argument=$width. trims string to $width symbols.
+ *  encodeheader - create base64 encoded header in iso-2022-jp. third argument unused
+ *  decodeheader - return human readable string from mime header. string is returned in euc-jp. third argument unused
+ *  downloadfilename - third argument $useragent. Arguments provide browser info. Returns shift-jis or euc-jp encoded file name
+ *  wordwrap - third argument=$wrap. wraps text at $wrap symbols
+ *  utf7-imap_encode - returns string converted from euc-jp to utf7-imap. third argument unused
+ *  utf7-imap_decode - returns string converted from utf7-imap to euc-jp. third argument unused
+ * @param string $ret default return value
  */
-function japanese_charset_xtra() {
+function japanese_xtra() {
     $ret = func_get_arg(1);  /* default return value */
     if (function_exists('mb_detect_encoding')) {
         switch (func_get_arg(0)) { /* action */
         case 'decode':
-            $detect_encoding = mb_detect_encoding($ret);
+            $detect_encoding = @mb_detect_encoding($ret);
             if ($detect_encoding == 'JIS' ||
                 $detect_encoding == 'EUC-JP' ||
-                $detect_encoding == 'SJIS') {
-                
-                $ret = mb_convert_encoding($ret, 'EUC-JP', 'AUTO');
+                $detect_encoding == 'SJIS' ||
+                $detect_encoding == 'UTF-8') {
+
+                $ret = mb_convert_kana(mb_convert_encoding($ret, 'EUC-JP', 'AUTO'), "KV");
             }
             break;
         case 'encode':
-            $detect_encoding = mb_detect_encoding($ret);
+            $detect_encoding = @mb_detect_encoding($ret);
             if ($detect_encoding == 'JIS' ||
                 $detect_encoding == 'EUC-JP' ||
-                $detect_encoding == 'SJIS') {
-                
-                $ret = mb_convert_encoding($ret, 'JIS', 'AUTO');
+                $detect_encoding == 'SJIS' ||
+                $detect_encoding == 'UTF-8') {
+
+                $ret = mb_convert_encoding(mb_convert_kana($ret, "KV"), 'JIS', 'AUTO');
             }
             break;
         case 'strimwidth':
             $width = func_get_arg(2);
-            $ret = mb_strimwidth($ret, 0, $width, '...'); 
+            $ret = mb_strimwidth($ret, 0, $width, '...');
             break;
         case 'encodeheader':
             $result = '';
@@ -1102,7 +747,8 @@ function japanese_charset_xtra() {
                         if ($prevcsize == 1) {
                             $result .= $tmpstr;
                         } else {
-                            $result .= mb_encode_mimeheader($tmpstr);
+                            $result .= str_replace(' ', '',
+                                                   mb_encode_mimeheader($tmpstr,'iso-2022-jp','B',''));
                         }
                         $tmpstr = $tmp;
                         $prevcsize = strlen($tmp);
@@ -1112,17 +758,17 @@ function japanese_charset_xtra() {
                     if (strlen(mb_substr($tmpstr, 0, 1)) == 1)
                         $result .= $tmpstr;
                     else
-                        $result .= mb_encode_mimeheader($tmpstr);
+                        $result .= str_replace(' ', '',
+                                               mb_encode_mimeheader($tmpstr,'iso-2022-jp','B',''));
                 }
             }
             $ret = $result;
-            //$ret = mb_encode_mimeheader($ret);
             break;
         case 'decodeheader':
             $ret = str_replace("\t", "", $ret);
             if (eregi('=\\?([^?]+)\\?(q|b)\\?([^?]+)\\?=', $ret))
-                $ret = mb_decode_mimeheader($ret);
-            $ret = mb_convert_encoding($ret, 'EUC-JP', 'AUTO');
+                $ret = @mb_decode_mimeheader($ret);
+            $ret = @mb_convert_encoding($ret, 'EUC-JP', 'AUTO');
             break;
         case 'downloadfilename':
             $useragent = func_get_arg(2);
@@ -1133,37 +779,447 @@ function japanese_charset_xtra() {
                 $ret = mb_convert_encoding($ret, 'EUC-JP', 'AUTO');
 }
             break;
+        case 'wordwrap':
+            $no_begin = "\x21\x25\x29\x2c\x2e\x3a\x3b\x3f\x5d\x7d\xa1\xf1\xa1\xeb\xa1" .
+                "\xc7\xa1\xc9\xa2\xf3\xa1\xec\xa1\xed\xa1\xee\xa1\xa2\xa1\xa3\xa1\xb9" .
+                "\xa1\xd3\xa1\xd5\xa1\xd7\xa1\xd9\xa1\xdb\xa1\xcd\xa4\xa1\xa4\xa3\xa4" .
+                "\xa5\xa4\xa7\xa4\xa9\xa4\xc3\xa4\xe3\xa4\xe5\xa4\xe7\xa4\xee\xa1\xab" .
+                "\xa1\xac\xa1\xb5\xa1\xb6\xa5\xa1\xa5\xa3\xa5\xa5\xa5\xa7\xa5\xa9\xa5" .
+                "\xc3\xa5\xe3\xa5\xe5\xa5\xe7\xa5\xee\xa5\xf5\xa5\xf6\xa1\xa6\xa1\xbc" .
+                "\xa1\xb3\xa1\xb4\xa1\xaa\xa1\xf3\xa1\xcb\xa1\xa4\xa1\xa5\xa1\xa7\xa1" .
+                "\xa8\xa1\xa9\xa1\xcf\xa1\xd1";
+            $no_end = "\x5c\x24\x28\x5b\x7b\xa1\xf2\x5c\xa1\xc6\xa1\xc8\xa1\xd2\xa1" .
+                "\xd4\xa1\xd6\xa1\xd8\xa1\xda\xa1\xcc\xa1\xf0\xa1\xca\xa1\xce\xa1\xd0\xa1\xef";
+            $wrap = func_get_arg(2);
+
+            if (strlen($ret) >= $wrap &&
+                substr($ret, 0, 1) != '>' &&
+                strpos($ret, 'http://') === FALSE &&
+                strpos($ret, 'https://') === FALSE &&
+                strpos($ret, 'ftp://') === FALSE) {
+
+                $ret = mb_convert_kana($ret, "KV");
+
+                $line_new = '';
+                $ptr = 0;
+
+                while ($ptr < strlen($ret) - 1) {
+                    $l = mb_strcut($ret, $ptr, $wrap);
+                    $ptr += strlen($l);
+                    $tmp = $l;
+
+                    $l = mb_strcut($ret, $ptr, 2);
+                    while (strlen($l) != 0 && mb_strpos($no_begin, $l) !== FALSE ) {
+                        $tmp .= $l;
+                        $ptr += strlen($l);
+                        $l = mb_strcut($ret, $ptr, 1);
+                    }
+                    $line_new .= $tmp;
+                    if ($ptr < strlen($ret) - 1)
+                        $line_new .= "\n";
+                }
+                $ret = $line_new;
+            }
+            break;
+        case 'utf7-imap_encode':
+            $ret = mb_convert_encoding($ret, 'UTF7-IMAP', 'EUC-JP');
+            break;
+        case 'utf7-imap_decode':
+            $ret = mb_convert_encoding($ret, 'EUC-JP', 'UTF7-IMAP');
+            break;
         }
     }
     return $ret;
 }
 
+/**************************
+ * Japanese extra functions
+ **************************/
 
-/*
- * Korean charset extra function
- * Hangul(Korean Character) Attached File Name Fix.
+/**
+ * Japanese decoding function
+ *
+ * converts string to euc-jp, if string uses JIS, EUC-JP, ShiftJIS or UTF-8
+ * charset. Needs mbstring support in php.
+ * @param string $ret text, that has to be converted
+ * @return string converted string
+ * @since 1.5.1
  */
-function korean_charset_xtra() {
-    
-    $ret = func_get_arg(1);  /* default return value */
-    if (func_get_arg(0) == 'downloadfilename') { /* action */
-        $ret = str_replace("\x0D\x0A", '', $ret);  /* Hanmail's CR/LF Clear */
-        for ($i=0;$i<strlen($ret);$i++) {
-            if ($ret[$i] >= "\xA1" && $ret[$i] <= "\xFE") {   /* 0xA1 - 0XFE are Valid */
-                $i++;
-                continue;
-            } else if (($ret[$i] >= 'a' && $ret[$i] <= 'z') || /* From Original ereg_replace in download.php */
-                       ($ret[$i] >= 'A' && $ret[$i] <= 'Z') ||
-                       ($ret[$i] == '.') || ($ret[$i] == '-')) {
-                continue;
-            } else {
-                $ret[$i] = '_';
+function japanese_xtra_decode($ret) {
+    if (function_exists('mb_detect_encoding')) {
+        $detect_encoding = @mb_detect_encoding($ret);
+        if ($detect_encoding == 'JIS' ||
+            $detect_encoding == 'EUC-JP' ||
+            $detect_encoding == 'SJIS' ||
+            $detect_encoding == 'UTF-8') {
+
+            $ret = mb_convert_kana(mb_convert_encoding($ret, 'EUC-JP', 'AUTO'), "KV");
+        }
+    }
+    return $ret;
+}
+
+/**
+ * Japanese encoding function
+ *
+ * converts string to jis, if string uses JIS, EUC-JP, ShiftJIS or UTF-8
+ * charset. Needs mbstring support in php.
+ * @param string $ret text, that has to be converted
+ * @return string converted text
+ * @since 1.5.1
+ */
+function japanese_xtra_encode($ret) {
+    if (function_exists('mb_detect_encoding')) {
+        $detect_encoding = @mb_detect_encoding($ret);
+        if ($detect_encoding == 'JIS' ||
+            $detect_encoding == 'EUC-JP' ||
+            $detect_encoding == 'SJIS' ||
+            $detect_encoding == 'UTF-8') {
+
+            $ret = mb_convert_encoding(mb_convert_kana($ret, "KV"), 'JIS', 'AUTO');
+        }
+    }
+    return $ret;
+}
+
+/**
+ * Japanese header encoding function
+ *
+ * creates base64 encoded header in iso-2022-jp charset
+ * @param string $ret text, that has to be converted
+ * @return string mime base64 encoded string
+ * @since 1.5.1
+ */
+function japanese_xtra_encodeheader($ret) {
+    if (function_exists('mb_detect_encoding')) {
+        $result = '';
+        if (strlen($ret) > 0) {
+            $tmpstr = mb_substr($ret, 0, 1);
+            $prevcsize = strlen($tmpstr);
+            for ($i = 1; $i < mb_strlen($ret); $i++) {
+                $tmp = mb_substr($ret, $i, 1);
+                if (strlen($tmp) == $prevcsize) {
+                    $tmpstr .= $tmp;
+                } else {
+                    if ($prevcsize == 1) {
+                        $result .= $tmpstr;
+                    } else {
+                        $result .= str_replace(' ', '',
+                                               mb_encode_mimeheader($tmpstr,'iso-2022-jp','B',''));
+                    }
+                    $tmpstr = $tmp;
+                    $prevcsize = strlen($tmp);
+                }
+            }
+            if (strlen($tmpstr)) {
+                if (strlen(mb_substr($tmpstr, 0, 1)) == 1)
+                    $result .= $tmpstr;
+                else
+                    $result .= str_replace(' ', '',
+                                           mb_encode_mimeheader($tmpstr,'iso-2022-jp','B',''));
             }
         }
+        $ret = $result;
+    }
+    return $ret;
+}
 
+/**
+ * Japanese header decoding function
+ *
+ * return human readable string from mime header. string is returned in euc-jp
+ * charset.
+ * @param string $ret header string
+ * @return string decoded header string
+ * @since 1.5.1
+ */
+function japanese_xtra_decodeheader($ret) {
+    if (function_exists('mb_detect_encoding')) {
+        $ret = str_replace("\t", "", $ret);
+        if (eregi('=\\?([^?]+)\\?(q|b)\\?([^?]+)\\?=', $ret))
+            $ret = @mb_decode_mimeheader($ret);
+        $ret = @mb_convert_encoding($ret, 'EUC-JP', 'AUTO');
+    }
+    return $ret;
+}
+
+/**
+ * Japanese downloaded filename processing function
+ *
+ * Returns shift-jis or euc-jp encoded file name
+ * @param string $ret string
+ * @param string $useragent browser
+ * @return string converted string
+ * @since 1.5.1
+ */
+function japanese_xtra_downloadfilename($ret,$useragent) {
+    if (function_exists('mb_detect_encoding')) {
+        if (strstr($useragent, 'Windows') !== false ||
+            strstr($useragent, 'Mac_') !== false) {
+            $ret = mb_convert_encoding($ret, 'SJIS', 'AUTO');
+        } else {
+            $ret = mb_convert_encoding($ret, 'EUC-JP', 'AUTO');
+        }
+    }
+    return $ret;
+}
+
+/**
+ * Japanese wordwrap function
+ *
+ * wraps text at set number of symbols
+ * @param string $ret text
+ * @param integer $wrap number of symbols per line
+ * @return string wrapped text
+ * @since 1.5.1
+ */
+function japanese_xtra_wordwrap($ret,$wrap) {
+    if (function_exists('mb_detect_encoding')) {
+        $no_begin = "\x21\x25\x29\x2c\x2e\x3a\x3b\x3f\x5d\x7d\xa1\xf1\xa1\xeb\xa1" .
+            "\xc7\xa1\xc9\xa2\xf3\xa1\xec\xa1\xed\xa1\xee\xa1\xa2\xa1\xa3\xa1\xb9" .
+            "\xa1\xd3\xa1\xd5\xa1\xd7\xa1\xd9\xa1\xdb\xa1\xcd\xa4\xa1\xa4\xa3\xa4" .
+            "\xa5\xa4\xa7\xa4\xa9\xa4\xc3\xa4\xe3\xa4\xe5\xa4\xe7\xa4\xee\xa1\xab" .
+            "\xa1\xac\xa1\xb5\xa1\xb6\xa5\xa1\xa5\xa3\xa5\xa5\xa5\xa7\xa5\xa9\xa5" .
+            "\xc3\xa5\xe3\xa5\xe5\xa5\xe7\xa5\xee\xa5\xf5\xa5\xf6\xa1\xa6\xa1\xbc" .
+            "\xa1\xb3\xa1\xb4\xa1\xaa\xa1\xf3\xa1\xcb\xa1\xa4\xa1\xa5\xa1\xa7\xa1" .
+            "\xa8\xa1\xa9\xa1\xcf\xa1\xd1";
+        $no_end = "\x5c\x24\x28\x5b\x7b\xa1\xf2\x5c\xa1\xc6\xa1\xc8\xa1\xd2\xa1" .
+            "\xd4\xa1\xd6\xa1\xd8\xa1\xda\xa1\xcc\xa1\xf0\xa1\xca\xa1\xce\xa1\xd0\xa1\xef";
+
+        if (strlen($ret) >= $wrap &&
+            substr($ret, 0, 1) != '>' &&
+            strpos($ret, 'http://') === FALSE &&
+            strpos($ret, 'https://') === FALSE &&
+            strpos($ret, 'ftp://') === FALSE) {
+
+            $ret = mb_convert_kana($ret, "KV");
+
+            $line_new = '';
+            $ptr = 0;
+
+            while ($ptr < strlen($ret) - 1) {
+                $l = mb_strcut($ret, $ptr, $wrap);
+                $ptr += strlen($l);
+                $tmp = $l;
+
+                $l = mb_strcut($ret, $ptr, 2);
+                while (strlen($l) != 0 && mb_strpos($no_begin, $l) !== FALSE ) {
+                    $tmp .= $l;
+                    $ptr += strlen($l);
+                    $l = mb_strcut($ret, $ptr, 1);
+                }
+                $line_new .= $tmp;
+                if ($ptr < strlen($ret) - 1)
+                    $line_new .= "\n";
+            }
+            $ret = $line_new;
+        }
+    }
+    return $ret;
+}
+
+/**
+ * Japanese imap folder name encoding function
+ *
+ * converts folder name from euc-jp to utf7-imap
+ * @param string $ret folder name
+ * @return string converted folder name
+ * @since 1.5.1
+ */
+function japanese_xtra_utf7_imap_encode($ret){
+    if (function_exists('mb_detect_encoding')) {
+        $ret = mb_convert_encoding($ret, 'UTF7-IMAP', 'EUC-JP');
+    }
+    return $ret;
+}
+
+/**
+ * Japanese imap folder name decoding function
+ *
+ * converts folder name from utf7-imap to euc-jp.
+ * @param string $ret folder name in utf7-imap
+ * @return string converted folder name
+ * @since 1.5.1
+ */
+function japanese_xtra_utf7_imap_decode($ret) {
+    if (function_exists('mb_detect_encoding')) {
+        $ret = mb_convert_encoding($ret, 'EUC-JP', 'UTF7-IMAP');
     }
+    return $ret;
+}
 
+/**
+ * Japanese string trimming function
+ *
+ * trims string to defined number of symbols
+ * @param string $ret string
+ * @param integer $width number of symbols
+ * @return string trimmed string
+ * @since 1.5.1
+ */
+function japanese_xtra_strimwidth($ret,$width) {
+    if (function_exists('mb_detect_encoding')) {
+        $ret = mb_strimwidth($ret, 0, $width, '...');
+    }
     return $ret;
 }
 
-?>
+/********************************
+ * Korean charset extra functions
+ ********************************/
+
+/**
+ * Korean downloaded filename processing functions
+ *
+ * @param string default return value
+ * @return string
+ */
+function korean_xtra_downloadfilename($ret) {
+    $ret = str_replace("\x0D\x0A", '', $ret);  /* Hanmail's CR/LF Clear */
+    for ($i=0;$i<strlen($ret);$i++) {
+        if ($ret[$i] >= "\xA1" && $ret[$i] <= "\xFE") {   /* 0xA1 - 0XFE are Valid */
+            $i++;
+            continue;
+        } else if (($ret[$i] >= 'a' && $ret[$i] <= 'z') || /* From Original ereg_replace in download.php */
+                   ($ret[$i] >= 'A' && $ret[$i] <= 'Z') ||
+                   ($ret[$i] == '.') || ($ret[$i] == '-')) {
+            continue;
+        } else {
+            $ret[$i] = '_';
+        }
+    }
+    return $ret;
+}
+
+/**
+ * Replaces non-braking spaces inserted by some browsers with regular space
+ *
+ * This function can be used to replace non-braking space symbols
+ * that are inserted in forms by some browsers instead of normal
+ * space symbol.
+ *
+ * @param string $string Text that needs to be cleaned
+ * @param string $charset Charset used in text
+ * @return string Cleaned text
+ */
+function cleanup_nbsp($string,$charset) {
+
+  // reduce number of case statements
+  if (stristr('iso-8859-',substr($charset,0,9))){
+    $output_charset="iso-8859-x";
+  }
+  if (stristr('windows-125',substr($charset,0,11))){
+    $output_charset="cp125x";
+  }
+  if (stristr('koi8',substr($charset,0,4))){
+    $output_charset="koi8-x";
+  }
+  if (! isset($output_charset)){
+    $output_charset=strtolower($charset);
+  }
+
+// where is non-braking space symbol
+switch($output_charset):
+ case "iso-8859-x":
+ case "cp125x":
+ case "iso-2022-jp":
+  $nbsp="\xA0";
+  break;
+ case "koi8-x":
+   $nbsp="\x9A";
+   break;
+ case "utf-8":
+   $nbsp="\xC2\xA0";
+   break;
+ default:
+   // don't change string if charset is unmatched
+   return $string;
+endswitch;
+
+// return space instead of non-braking space.
+ return str_replace($nbsp,' ',$string);
+}
+
+/**
+ * Function informs if it is safe to convert given charset to the one that is used by user.
+ *
+ * It is safe to use conversion only if user uses utf-8 encoding and when
+ * converted charset is similar to the one that is used by user.
+ *
+ * @param string $input_charset Charset of text that needs to be converted
+ * @return bool is it possible to convert to user's charset
+ */
+function is_conversion_safe($input_charset) {
+  global $languages, $sm_notAlias, $default_charset, $lossy_encoding;
+
+    if (isset($lossy_encoding) && $lossy_encoding )
+       return true;
+
+ // convert to lower case
+ $input_charset = strtolower($input_charset);
+
+ // Is user's locale Unicode based ?
+ if ( $default_charset == "utf-8" ) {
+   return true;
+ }
+
+ // Charsets that are similar
+switch ($default_charset):
+case "windows-1251":
+      if ( $input_charset == "iso-8859-5" ||
+           $input_charset == "koi8-r" ||
+           $input_charset == "koi8-u" ) {
+        return true;
+     } else {
+        return false;
+     }
+case "windows-1257":
+  if ( $input_charset == "iso-8859-13" ||
+       $input_charset == "iso-8859-4" ) {
+    return true;
+  } else {
+    return false;
+  }
+case "iso-8859-4":
+  if ( $input_charset == "iso-8859-13" ||
+       $input_charset == "windows-1257" ) {
+     return true;
+  } else {
+     return false;
+  }
+case "iso-8859-5":
+  if ( $input_charset == "windows-1251" ||
+       $input_charset == "koi8-r" ||
+       $input_charset == "koi8-u" ) {
+     return true;
+  } else {
+     return false;
+  }
+case "iso-8859-13":
+  if ( $input_charset == "iso-8859-4" ||
+       $input_charset == "windows-1257" ) {
+     return true;
+  } else {
+     return false;
+  }
+case "koi8-r":
+  if ( $input_charset == "windows-1251" ||
+       $input_charset == "iso-8859-5" ||
+       $input_charset == "koi8-u" ) {
+     return true;
+  } else {
+     return false;
+  }
+case "koi8-u":
+  if ( $input_charset == "windows-1251" ||
+       $input_charset == "iso-8859-5" ||
+       $input_charset == "koi8-r" ) {
+     return true;
+  } else {
+     return false;
+  }
+default:
+   return false;
+endswitch;
+}
+?>
\ No newline at end of file