fix for greek chars.
[squirrelmail.git] / functions / i18n.php
index 137b491bb3073ce7895113d703f6d36739b7617e..31363c16c477b5ac4aa38446bdf2f8c694f62d31 100644 (file)
 
 /* Decodes a string to the internal encoding from the given charset */
 function charset_decode ($charset, $string) {
+    global $languages, $squirrelmail_language;
+
+    if (isset($languages[$squirrelmail_language]['XTRA_CODE']) &&
+        function_exists($languages[$squirrelmail_language]['XTRA_CODE'])) {
+        $string = $languages[$squirrelmail_language]['XTRA_CODE']('decode', $string);
+    }
 
     /* All HTML special characters are 7 bit and can be replaced first */
     $string = htmlspecialchars ($string);
@@ -28,8 +34,12 @@ function charset_decode ($charset, $string) {
             $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 {
@@ -397,6 +407,53 @@ function charset_decode_iso_8859_2 ($string) {
     return $string;
 }
 
+/* 
+   iso-8859-4 is Baltic codeset used in some email clients 
+   instead of iso-8859-13 in Lithuania 
+   only Lithuanian charactes are added.
+*/
+
+function charset_decode_iso_8859_4 ($string) {
+    // latin capital a with ogonek
+    $string = str_replace ("\241", 'Ą', $string);
+    // latin capital c with caron
+    $string = str_replace ("\310", 'Č', $string);
+    // latin capital e with ogonek
+    $string = str_replace ("\312", 'Ę', $string);
+    // latin capital e with dot above
+    $string = str_replace ("\314", 'Ė', $string);
+    // latin capital i with ogonek
+    $string = str_replace ("\307", 'Į', $string);
+    // latin capital s with caron
+    $string = str_replace ("\251", 'Š', $string);
+    // latin capital u with ogonek
+    $string = str_replace ("\331", 'Ų', $string);
+    // latin capital u with macron
+    $string = str_replace ("\336", 'Ū', $string);
+    // latin capital z with caron
+    $string = str_replace ("\256", 'Ž', $string);
+    // latin small a with ogonek
+    $string = str_replace ("\261", 'ą', $string);
+    // latin small c with caron
+    $string = str_replace ("\350", 'č', $string);
+    // latin small e with ogonek
+    $string = str_replace ("\352", 'ę', $string);
+    // latin small e with dot above
+    $string = str_replace ("\354", 'ė', $string);
+    // latin small i with ogonek
+    $string = str_replace ("\347", 'į', $string);
+    // latin small s with caron
+    $string = str_replace ("\271", 'š', $string);
+    // latin small u with ogonek
+    $string = str_replace ("\371", 'ų', $string);
+    // latin small u with macron
+    $string = str_replace ("\376", 'ū', $string);
+    // latin small z with caron
+    $string = str_replace ("\276", 'ž', $string);
+
+    return (charset_decode_iso_8859_1($string));
+}
+
 /* iso-8859-7 is Greek. */
 function charset_decode_iso_8859_7 ($string) {
     global $default_charset;
@@ -435,11 +492,8 @@ function charset_decode_iso_8859_7 ($string) {
      * ISO-8859-7 characters from 11/04 (0xB4) to 11/06 (0xB6)
      * These are Unicode 900-902
      */
-    while (ereg("([\264-\266])", $string, $res)) {
-        $replace = '&#' . (ord($res[1])+720) . ';';
-        $string = str_replace($res[1], $replace, $string);
-    }
-
+    $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", '·', $string);
 
@@ -447,10 +501,7 @@ function charset_decode_iso_8859_7 ($string) {
      * ISO-8859-7 characters from 11/08 (0xB8) to 11/10 (0xBA)
      * These are Unicode 900-902
      */
-    while (ereg("([\270-\272])", $string, $res)) {
-        $replace = '&#' . (ord($res[1])+720) . ";";
-        $string = str_replace($res[1], $replace, $string);
-    }
+    $string = preg_replace("/([\270-\272])/e","'&#' . (ord('\\1')+720);",$string);
 
     /*
      * 11/11 (0xBB) Right angle quotation mark is the same as in
@@ -459,14 +510,57 @@ function charset_decode_iso_8859_7 ($string) {
     $string = str_replace("\273", '»', $string);
 
     /* And now the rest of the charset */
-    while (ereg("([\274-\376])", $string, $res)) {
-        $replace = '&#' . (ord($res[1])+720) . ';';
-        $string = str_replace($res[1], $replace, $string);
-    }
+    $string = preg_replace("/([\274-\376])/e","'&#'.(ord('\\1')+720);",$string);
 
     return $string;
 }
 
+/*
+ iso-8859-13 codeset used in Lithuania
+ only Lithuanian charactes are added.
+*/
+
+function charset_decode_iso_8859_13 ($string) {
+    // latin capital a with ogonek
+    $string = str_replace ("\300", 'Ą', $string);
+    // latin capital c with caron
+    $string = str_replace ("\310", 'Č', $string);
+    // latin capital e with ogonek
+    $string = str_replace ("\306", 'Ę', $string);
+    // latin capital e with dot above
+    $string = str_replace ("\313", 'Ė', $string);
+    // latin capital i with ogonek
+    $string = str_replace ("\301", 'Į', $string);
+    // latin capital s with caron
+    $string = str_replace ("\320", 'Š', $string);
+    // latin capital u with ogonek
+    $string = str_replace ("\330", 'Ų', $string);
+    // latin capital u with macron
+    $string = str_replace ("\333", 'Ū', $string);
+    // latin capital z with caron
+    $string = str_replace ("\336", 'Ž', $string);
+    // latin small a with ogonek
+    $string = str_replace ("\340", 'ą', $string);
+    // latin small c with caron
+    $string = str_replace ("\350", 'č', $string);
+    // latin small e with ogonek
+    $string = str_replace ("\346", 'ę', $string);
+    // latin small e with dot above
+    $string = str_replace ("\353", 'ė', $string);
+    // latin small i with ogonek
+    $string = str_replace ("\341", 'į', $string);
+    // latin small s with caron
+    $string = str_replace ("\360", 'š', $string);
+    // latin small u with ogonek
+    $string = str_replace ("\370", 'ų', $string);
+    // latin small u with macron
+    $string = str_replace ("\373", 'ū', $string);
+    // latin small z with caron
+    $string = str_replace ("\376", 'ž', $string);
+
+    return (charset_decode_iso_8859_1($string));
+}
+
 /*
  * 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.
@@ -475,7 +569,7 @@ function charset_decode_iso_8859_15 ($string) {
     // Euro sign
     $string = str_replace ("\244", '€', $string);
     // Latin capital letter S with caron
-    $string = str_replace ("\244", 'Š', $string);
+    $string = str_replace ("\246", 'Š', $string);
     // Latin small letter s with caron
     $string = str_replace ("\250", 'š', $string);
     // Latin capital letter Z with caron
@@ -492,7 +586,7 @@ function charset_decode_iso_8859_15 ($string) {
     return (charset_decode_iso_8859_1($string));
 }
 
-/* ISO-8859-15 is Cyrillic */
+/* 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');
@@ -682,6 +776,7 @@ function charset_decode_koi8r ($string) {
     return $string;
 }
 
+
 /*
  * Set up the language to be output
  * if $do_search is true, then scan the browser information
@@ -690,7 +785,7 @@ function charset_decode_koi8r ($string) {
 function set_up_language($sm_language, $do_search = false) {
 
     static $SetupAlready = 0;
-    global $HTTP_ACCEPT_LANGUAGE, $use_gettext, $languages,
+    global $use_gettext, $languages,
            $squirrelmail_language, $squirrelmail_default_language,
            $sm_notAlias;
 
@@ -699,8 +794,8 @@ function set_up_language($sm_language, $do_search = false) {
     }
     $SetupAlready = TRUE;
 
-    if ($do_search && ! $sm_language && isset($HTTP_ACCEPT_LANGUAGE)) {
-        $sm_language = substr($HTTP_ACCEPT_LANGUAGE, 0, 2);
+    if ($do_search && ! $sm_language && isset($_SERVER['HTTP_ACCEPT_LANGUAGE'])) {
+        $sm_language = substr($_SERVER['HTTP_ACCEPT_LANGUAGE'], 0, 2);
     }
     
     if (!$sm_language && isset($squirrelmail_default_language)) {
@@ -712,23 +807,32 @@ function set_up_language($sm_language, $do_search = false) {
         $sm_notAlias = $languages[$sm_notAlias]['ALIAS'];
     }
 
-    if ( isset($sm_language) && 
+    if ( isset($sm_language) &&
          $use_gettext &&
          $sm_language != '' &&
          isset($languages[$sm_notAlias]['CHARSET']) ) {
         bindtextdomain( 'squirrelmail', '../locale/' );
-        textdomain( 'squirrelmail' );         
-        if ( !ini_get('safe_mode') && 
+        textdomain( 'squirrelmail' );
+        if ( !ini_get('safe_mode') &&
              getenv( 'LC_ALL' ) != $sm_notAlias ) {
             putenv( "LC_ALL=$sm_notAlias" );
             putenv( "LANG=$sm_notAlias" );
             putenv( "LANGUAGE=$sm_notAlias" );
         }
-        setlocale('LC_ALL', $sm_notAlias);
+        setlocale(LC_ALL, $sm_notAlias);
         $squirrelmail_language = $sm_notAlias;
+        if ($squirrelmail_language == 'ja_JP' && function_exists('mb_detect_encoding') ) {
+            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).");
+            }
+            mb_internal_encoding('EUC-JP');
+            mb_http_output('pass');
+        } else {
         header( 'Content-Type: text/html; charset=' . $languages[$sm_notAlias]['CHARSET'] );
     }
 }
+}
 
 function set_my_charset(){
 
@@ -742,7 +846,7 @@ function set_my_charset(){
      * message blindly with the system-wide $default_charset.
      */
     global $data_dir, $username, $default_charset, $languages;
-    
+
     $my_language = getPref($data_dir, $username, 'language');
     if (!$my_language) {
         return;
@@ -829,8 +933,14 @@ $languages['it_IT']['NAME']    = 'Italian';
 $languages['it_IT']['CHARSET'] = 'iso-8859-1';
 $languages['it']['ALIAS'] = 'it_IT';
 
+$languages['ja_JP']['NAME']    = 'Japanese';
+$languages['ja_JP']['CHARSET'] = 'iso-2022-jp';
+$languages['ja_JP']['XTRA_CODE'] = 'japanese_charset_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']['ALIAS'] = 'ko_KR';
 
 $languages['nl_NL']['NAME']    = 'Dutch';
@@ -857,9 +967,9 @@ $languages['ru_RU']['NAME']    = 'Russian';
 $languages['ru_RU']['CHARSET'] = 'koi8-r';
 $languages['ru']['ALIAS'] = 'ru_RU';
 
-$languages['sr']['NAME']    = 'Serbian';
-$languages['sr']['CHARSET'] = 'iso-8859-2';
-$languages['sr_YU']['ALIAS'] = 'sr';
+$languages['sr_YU']['NAME']    = 'Serbian';
+$languages['sr_YU']['CHARSET'] = 'iso-8859-2';
+$languages['sr']['ALIAS'] = 'sr_YU';
 
 $languages['sv_SE']['NAME']    = 'Swedish';
 $languages['sv_SE']['CHARSET'] = 'iso-8859-1';
@@ -869,12 +979,14 @@ $languages['tr_TR']['NAME']    = 'Turkish';
 $languages['tr_TR']['CHARSET'] = 'iso-8859-9';
 $languages['tr']['ALIAS'] = 'tr_TR';
 
-// it's zh_TW, actually.
-
 $languages['zh_TW']['NAME']    = 'Taiwan';
 $languages['zh_TW']['CHARSET'] = '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';
@@ -899,6 +1011,14 @@ $languages['bg_BG']['NAME']    = 'Bulgarian';
 $languages['bg_BG']['CHARSET'] = 'windows-1251';
 $languages['bg']['ALIAS'] = 'bg_BG';
 
+// 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';
+
+
 /* Detect whether gettext is installed. */
 $gettext_flags = 0;
 if (function_exists('_')) {
@@ -918,7 +1038,7 @@ if ($gettext_flags == 7) {
 /* If we can fake gettext, try that */
 elseif ($gettext_flags == 0) {
     $use_gettext = true;
-    include_once('../functions/gettext.php');
+    include_once(SM_PATH . 'functions/gettext.php');
 } else {
     /* Uh-ho.  A weird install */
     if (! $gettext_flags & 1) {
@@ -938,4 +1058,112 @@ elseif ($gettext_flags == 0) {
     }
 }
 
+
+/*
+ * Japanese charset extra function
+ *
+ */
+function japanese_charset_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);
+            if ($detect_encoding == 'JIS' ||
+                $detect_encoding == 'EUC-JP' ||
+                $detect_encoding == 'SJIS') {
+                
+                $ret = mb_convert_encoding($ret, 'EUC-JP', 'AUTO');
+            }
+            break;
+        case 'encode':
+            $detect_encoding = mb_detect_encoding($ret);
+            if ($detect_encoding == 'JIS' ||
+                $detect_encoding == 'EUC-JP' ||
+                $detect_encoding == 'SJIS') {
+                
+                $ret = mb_convert_encoding($ret, 'JIS', 'AUTO');
+            }
+            break;
+        case 'strimwidth':
+            $width = func_get_arg(2);
+            $ret = mb_strimwidth($ret, 0, $width, '...'); 
+            break;
+        case 'encodeheader':
+            $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 .= mb_encode_mimeheader($tmpstr);
+                        }
+                        $tmpstr = $tmp;
+                        $prevcsize = strlen($tmp);
+                    }
+                }
+                if (strlen($tmpstr)) {
+                    if (strlen(mb_substr($tmpstr, 0, 1)) == 1)
+                        $result .= $tmpstr;
+                    else
+                        $result .= mb_encode_mimeheader($tmpstr);
+                }
+            }
+            $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');
+            break;
+        case 'downloadfilename':
+            $useragent = func_get_arg(2);
+            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');
+}
+            break;
+        }
+    }
+    return $ret;
+}
+
+
+/*
+ * Korean charset extra function
+ * Hangul(Korean Character) Attached File Name Fix.
+ */
+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] = '_';
+            }
+        }
+
+    }
+
+    return $ret;
+}
+
 ?>