From 77b5d7ee304a688a2eb115ce04b460b43c0dd700 Mon Sep 17 00:00:00 2001
From: Aleksander Machniak <alec@alec.pl>
Date: Sun, 22 May 2016 08:43:54 -0400
Subject: [PATCH] Fix priority icon(s) position

---
 program/lib/Roundcube/rcube_charset.php |  217 ++++++++++++++++++++++++++---------------------------
 1 files changed, 107 insertions(+), 110 deletions(-)

diff --git a/program/lib/Roundcube/rcube_charset.php b/program/lib/Roundcube/rcube_charset.php
index 8612e7f..2d6d9d3 100644
--- a/program/lib/Roundcube/rcube_charset.php
+++ b/program/lib/Roundcube/rcube_charset.php
@@ -1,6 +1,6 @@
 <?php
 
-/*
+/**
  +-----------------------------------------------------------------------+
  | This file is part of the Roundcube Webmail client                     |
  | Copyright (C) 2005-2012, The Roundcube Dev Team                       |
@@ -70,14 +70,13 @@
     /**
      * Catch an error and throw an exception.
      *
-     * @param  int    Level of the error
-     * @param  string Error message
+     * @param int    $errno  Level of the error
+     * @param string $errstr Error message
      */
     public static function error_handler($errno, $errstr)
     {
         throw new ErrorException($errstr, 0, $errno);
     }
-
 
     /**
      * Parse and validate charset name string (see #1485758).
@@ -120,7 +119,7 @@
         }
         // ISO-8859
         else if (preg_match('/ISO8859([0-9]{0,2})/', $str, $m)) {
-            $iso = 'ISO-8859-' . ($m[1] ? $m[1] : 1);
+            $iso = 'ISO-8859-' . ($m[1] ?: 1);
             // some clients sends windows-1252 text as latin1,
             // it is safe to use windows-1252 for all latin1
             $result = $iso == 'ISO-8859-1' ? 'WINDOWS-1252' : $iso;
@@ -159,25 +158,23 @@
         return $result;
     }
 
-
     /**
      * Convert a string from one charset to another.
      * Uses mbstring and iconv functions if possible
      *
-     * @param  string Input string
-     * @param  string Suspected charset of the input string
-     * @param  string Target charset to convert to; defaults to RCUBE_CHARSET
+     * @param string $str  Input string
+     * @param string $from Suspected charset of the input string
+     * @param string $to   Target charset to convert to; defaults to RCUBE_CHARSET
      *
      * @return string Converted string
      */
     public static function convert($str, $from, $to = null)
     {
-        static $iconv_options   = null;
-        static $mbstring_list   = null;
-        static $mbstring_sch    = null;
-        static $conv            = null;
+        static $iconv_options = null;
+        static $mbstring_list = null;
+        static $mbstring_sch  = null;
 
-        $to   = empty($to) ? RCUBE_CHARSET : $to;
+        $to   = empty($to) ? RCUBE_CHARSET : strtoupper($to);
         $from = self::parse_charset($from);
 
         // It is a common case when UTF-16 charset is used with US-ASCII content (#1488654)
@@ -210,14 +207,15 @@
             // it means that input string has been truncated
             set_error_handler(array('rcube_charset', 'error_handler'), E_NOTICE);
             try {
-                $_iconv = iconv($from, $to . $iconv_options, $str);
-            } catch (ErrorException $e) {
-                $_iconv = false;
+                $out = iconv($from, $to . $iconv_options, $str);
+            }
+            catch (ErrorException $e) {
+                $out = false;
             }
             restore_error_handler();
 
-            if ($_iconv !== false) {
-                return $_iconv;
+            if ($out !== false) {
+                return $out;
             }
         }
 
@@ -240,20 +238,29 @@
                 $aliases['US-ASCII'] = 'ASCII';
             }
 
-            $mb_from = $aliases[$from] ? $aliases[$from] : $from;
-            $mb_to   = $aliases[$to] ? $aliases[$to] : $to;
+            $mb_from = $aliases[$from] ?: $from;
+            $mb_to   = $aliases[$to] ?: $to;
 
             // return if encoding found, string matches encoding and convert succeeded
             if (in_array($mb_from, $mbstring_list) && in_array($mb_to, $mbstring_list)) {
-                if (mb_check_encoding($str, $mb_from)) {
-                    // Do the same as //IGNORE with iconv
-                    mb_substitute_character('none');
-                    $out = mb_convert_encoding($str, $mb_to, $mb_from);
-                    mb_substitute_character($mbstring_sch);
+                // Do the same as //IGNORE with iconv
+                mb_substitute_character('none');
 
-                    if ($out !== false) {
-                        return $out;
-                    }
+                // throw an exception if mbstring reports an illegal character in input
+                // using mb_check_encoding() is much slower
+                set_error_handler(array('rcube_charset', 'error_handler'), E_WARNING);
+                try {
+                    $out = mb_convert_encoding($str, $mb_to, $mb_from);
+                }
+                catch (ErrorException $e) {
+                    $out = false;
+                }
+                restore_error_handler();
+
+                mb_substitute_character($mbstring_sch);
+
+                if ($out !== false) {
+                    return $out;
                 }
             }
         }
@@ -261,29 +268,17 @@
         // convert charset using bundled classes/functions
         if ($to == 'UTF-8') {
             if ($from == 'UTF7-IMAP') {
-                if ($_str = self::utf7imap_to_utf8($str)) {
-                    return $_str;
+                if ($out = self::utf7imap_to_utf8($str)) {
+                    return $out;
                 }
             }
             else if ($from == 'UTF-7') {
-                if ($_str = self::utf7_to_utf8($str)) {
-                    return $_str;
+                if ($out = self::utf7_to_utf8($str)) {
+                    return $out;
                 }
             }
             else if ($from == 'ISO-8859-1' && function_exists('utf8_encode')) {
                 return utf8_encode($str);
-            }
-            else if (class_exists('utf8')) {
-                if (!$conv) {
-                    $conv = new utf8($from);
-                }
-                else {
-                    $conv->loadCharset($from);
-                }
-
-                if ($_str = $conv->strToUtf8($str)) {
-                    return $_str;
-                }
             }
         }
 
@@ -291,38 +286,29 @@
         if ($from == 'UTF-8') {
             // @TODO: we need a function for UTF-7 (RFC2152) conversion
             if ($to == 'UTF7-IMAP' || $to == 'UTF-7') {
-                if ($_str = self::utf8_to_utf7imap($str)) {
-                    return $_str;
+                if ($out = self::utf8_to_utf7imap($str)) {
+                    return $out;
                 }
             }
             else if ($to == 'ISO-8859-1' && function_exists('utf8_decode')) {
                 return utf8_decode($str);
             }
-            else if (class_exists('utf8')) {
-                if (!$conv) {
-                    $conv = new utf8($to);
-                }
-                else {
-                    $conv->loadCharset($from);
-                }
+        }
 
-                if ($_str = $conv->strToUtf8($str)) {
-                    return $_str;
-                }
-            }
+        if (!isset($out)) {
+            trigger_error("No suitable function found for '$from' to '$to' conversion");
         }
 
         // return original string
         return $str;
     }
 
-
     /**
      * Converts string from standard UTF-7 (RFC 2152) to UTF-8.
      *
-     * @param  string  Input string (UTF-7)
+     * @param string $str Input string (UTF-7)
      *
-     * @return string  Converted string (UTF-8)
+     * @return string Converted string (UTF-8)
      */
     public static function utf7_to_utf8($str)
     {
@@ -376,13 +362,12 @@
         return $res;
     }
 
-
     /**
      * Converts string from UTF-16 to UTF-8 (helper for utf-7 to utf-8 conversion)
      *
-     * @param  string  Input string
+     * @param string $str Input string
      *
-     * @return string  The converted string
+     * @return string The converted string
      */
     public static function utf16_to_utf8($str)
     {
@@ -407,7 +392,6 @@
 
         return $dec;
     }
-
 
     /**
      * Convert the data ($str) from RFC 2060's UTF-7 to UTF-8.
@@ -516,7 +500,6 @@
 
         return $p;
     }
-
 
     /**
      * Convert the data ($str) from UTF-8 to RFC 2060's UTF-7.
@@ -648,7 +631,6 @@
         return $p;
     }
 
-
     /**
      * A method to guess character set of a string.
      *
@@ -672,36 +654,47 @@
         if ($string[0] == "\0" && $string[1] != "\0" && $string[2] == "\0" && $string[3] != "\0") return 'UTF-16BE';
         if ($string[0] != "\0" && $string[1] == "\0" && $string[2] != "\0" && $string[3] == "\0") return 'UTF-16LE';
 
-        if (function_exists('mb_detect_encoding')) {
-            if (empty($language)) {
-                $rcube    = rcube::get_instance();
-                $language = $rcube->get_user_language();
+        if (empty($language)) {
+            $rcube    = rcube::get_instance();
+            $language = $rcube->get_user_language();
+        }
+
+        // Prioritize charsets according to current language (#1485669)
+        switch ($language) {
+        case 'ja_JP':
+            $prio = array('ISO-2022-JP', 'JIS', 'UTF-8', 'EUC-JP', 'eucJP-win', 'SJIS', 'SJIS-win');
+            break;
+
+        case 'zh_CN':
+        case 'zh_TW':
+            $prio = array('UTF-8', 'BIG-5', 'GB2312', 'EUC-TW');
+            break;
+
+        case 'ko_KR':
+            $prio = array('UTF-8', 'EUC-KR', 'ISO-2022-KR');
+            break;
+
+        case 'ru_RU':
+            $prio = array('UTF-8', 'WINDOWS-1251', 'KOI8-R');
+            break;
+
+        case 'tr_TR':
+            $prio = array('UTF-8', 'ISO-8859-9', 'WINDOWS-1254');
+            break;
+        }
+
+        // mb_detect_encoding() is not reliable for some charsets (#1490135)
+        // use mb_check_encoding() to make charset priority lists really working
+        if ($prio && function_exists('mb_check_encoding')) {
+            foreach ($prio as $encoding) {
+                if (mb_check_encoding($string, $encoding)) {
+                    return $encoding;
+                }
             }
+        }
 
-            // Prioritize charsets according to current language (#1485669)
-            switch ($language) {
-            case 'ja_JP':
-                $prio = array('ISO-2022-JP', 'JIS', 'UTF-8', 'EUC-JP', 'eucJP-win', 'SJIS', 'SJIS-win');
-                break;
-
-            case 'zh_CN':
-            case 'zh_TW':
-                $prio = array('UTF-8', 'BIG-5', 'GB2312', 'EUC-TW');
-                break;
-
-            case 'ko_KR':
-                $prio = array('UTF-8', 'EUC-KR', 'ISO-2022-KR');
-                break;
-
-            case 'ru_RU':
-                $prio = array('UTF-8', 'WINDOWS-1251', 'KOI8-R');
-                break;
-
-            case 'tr_TR':
-                $prio = array('UTF-8', 'ISO-8859-9', 'WINDOWS-1254');
-                break;
-
-            default:
+        if (function_exists('mb_detect_encoding')) {
+            if (!$prio) {
                 $prio = array('UTF-8', 'SJIS', 'GB2312',
                     'ISO-8859-1', 'ISO-8859-2', 'ISO-8859-3', 'ISO-8859-4',
                     'ISO-8859-5', 'ISO-8859-6', 'ISO-8859-7', 'ISO-8859-8', 'ISO-8859-9',
@@ -713,7 +706,9 @@
 
             $encodings = array_unique(array_merge($prio, mb_list_encodings()));
 
-            return mb_detect_encoding($string, $encodings);
+            if ($encoding = mb_detect_encoding($string, $encodings)) {
+                return $encoding;
+            }
         }
 
         // No match, check for UTF-8
@@ -734,7 +729,6 @@
 
         return $failover;
     }
-
 
     /**
      * Removes non-unicode characters from input.
@@ -759,7 +753,12 @@
 
         // iconv/mbstring are much faster (especially with long strings)
         if (function_exists('mb_convert_encoding')) {
-            if (($res = mb_convert_encoding($input, 'UTF-8', 'UTF-8')) !== false) {
+            $msch = mb_substitute_character();
+            mb_substitute_character('none');
+            $res = mb_convert_encoding($input, 'UTF-8', 'UTF-8');
+            mb_substitute_character($msch);
+
+            if ($res !== false) {
                 return $res;
             }
         }
@@ -790,34 +789,32 @@
 
             // 1-byte character
             if ($ord <= 0x7F) {
-                if ($seq) {
+                if ($seq !== '') {
                     $out .= preg_match($regexp, $seq) ? $seq : '';
+                    $seq = '';
                 }
-                $seq = '';
+
                 $out .= $chr;
-            // first (or second) byte of multibyte sequence
             }
+            // first byte of multibyte sequence
             else if ($ord >= 0xC0) {
-                if (strlen($seq) > 1) {
+                if ($seq !== '') {
                     $out .= preg_match($regexp, $seq) ? $seq : '';
                     $seq = '';
                 }
-                else if ($seq && ord($seq) < 0xC0) {
-                    $seq = '';
-                }
-                $seq .= $chr;
-            // next byte of multibyte sequence
+
+                $seq = $chr;
             }
-            else if ($seq) {
+            // next byte of multibyte sequence
+            else if ($seq !== '') {
                 $seq .= $chr;
             }
         }
 
-        if ($seq) {
+        if ($seq !== '') {
             $out .= preg_match($regexp, $seq) ? $seq : '';
         }
 
         return $out;
     }
-
 }

--
Gitblit v1.9.1