Aleksander Machniak
2012-05-22 041c93ce0bc00cb6417ce2e4bdce2ed84d37f50a
program/include/rcube_charset.php
@@ -20,9 +20,6 @@
 | Author: Thomas Bruederli <roundcube@gmail.com>                        |
 | Author: Aleksander Machniak <alec@alec.pl>                            |
 +-----------------------------------------------------------------------+
 $Id$
*/
/**
@@ -44,14 +41,14 @@
        'UNKNOWN'       => 'ISO-8859-15',
        'USERDEFINED'   => 'ISO-8859-15',
        'KSC56011987'   => 'EUC-KR',
        'GB2312'        => 'GBK',
        'GB231280'       => 'GBK',
        'UNICODE'       => 'UTF-8',
        'UTF7IMAP'       => 'UTF7-IMAP',
        'TIS620'       => 'WINDOWS-874',
        'ISO88599'       => 'WINDOWS-1254',
        'ISO885911'       => 'WINDOWS-874',
        'MACROMAN'       => 'MACINTOSH',
        'GB2312'        => 'GBK',
        'GB231280'      => 'GBK',
        'UNICODE'       => 'UTF-8',
        'UTF7IMAP'      => 'UTF7-IMAP',
        'TIS620'        => 'WINDOWS-874',
        'ISO88599'      => 'WINDOWS-1254',
        'ISO885911'     => 'WINDOWS-874',
        'MACROMAN'      => 'MACINTOSH',
        '77'            => 'MAC',
        '128'           => 'SHIFT-JIS',
        '129'           => 'CP949',
@@ -78,7 +75,7 @@
     * @param  int    Level of the error
     * @param  string Error message
     */
    public function error_handler($errno, $errstr)
    public static function error_handler($errno, $errstr)
    {
        throw new ErrorException($errstr, 0, $errno);
    }
@@ -93,7 +90,7 @@
     *
     * @return string The validated charset name
     */
    public static function parse($input)
    public static function parse_charset($input)
    {
        static $charsets = array();
        $charset = strtoupper($input);
@@ -178,20 +175,18 @@
    public static function convert($str, $from, $to = null)
    {
        static $iconv_options   = null;
        static $mbstring_loaded = null;
        static $mbstring_list   = null;
        static $conv            = null;
        $to   = empty($to) ? strtoupper(RCMAIL_CHARSET) : self::parse($to);
        $from = self::parse($from);
        $to   = empty($to) ? strtoupper(RCMAIL_CHARSET) : self::parse_charset($to);
        $from = self::parse_charset($from);
        if ($from == $to || empty($str) || empty($from)) {
            return $str;
        }
        // convert charset using iconv module
        if (function_exists('iconv') && $from != 'UTF7-IMAP' && $to != 'UTF7-IMAP') {
            if ($iconv_options === null) {
        if ($iconv_options === null) {
            if (function_exists('iconv')) {
                // ignore characters not available in output charset
                $iconv_options = '//IGNORE';
                if (iconv('', $iconv_options, '') === false) {
@@ -199,7 +194,10 @@
                    $iconv_options = '';
                }
            }
        }
        // convert charset using iconv module
        if ($iconv_options !== null && $from != 'UTF7-IMAP' && $to != 'UTF7-IMAP') {
            // throw an exception if iconv reports an illegal character in input
            // it means that input string has been truncated
            set_error_handler(array('rcube_charset', 'error_handler'), E_NOTICE);
@@ -215,18 +213,16 @@
            }
        }
        if ($mbstring_loaded === null) {
            $mbstring_loaded = extension_loaded('mbstring');
        }
        // convert charset using mbstring module
        if ($mbstring_loaded) {
            $aliases['WINDOWS-1257'] = 'ISO-8859-13';
            if ($mbstring_list === null) {
        if ($mbstring_list === null) {
            if (extension_loaded('mbstring')) {
                $mbstring_list = mb_list_encodings();
                $mbstring_list = array_map('strtoupper', $mbstring_list);
            }
        }
        // convert charset using mbstring module
        if ($mbstring_list !== null) {
            $aliases['WINDOWS-1257'] = 'ISO-8859-13';
            $mb_from = $aliases[$from] ? $aliases[$from] : $from;
            $mb_to   = $aliases[$to] ? $aliases[$to] : $to;
@@ -272,7 +268,7 @@
        if ($from == 'UTF-8') {
            // @TODO: we need a function for UTF-7 (RFC2152) conversion
            if ($to == 'UTF7-IMAP' || $to == 'UTF-7') {
                if ($_str = utf8_to_utf7imap($str)) {
                if ($_str = self::utf8_to_utf7imap($str)) {
                    return $_str;
                }
            }
@@ -336,7 +332,7 @@
                        break;
                    }
                   $ch .= $u7;
                    $ch .= $u7;
                }
                if ($ch == '') {
@@ -724,7 +720,7 @@
            }
            else if ($ord >= 0xC0) {
                if (strlen($seq) > 1) {
                   $out .= preg_match($regexp, $seq) ? $seq : '';
                    $out .= preg_match($regexp, $seq) ? $seq : '';
                    $seq = '';
                }
                else if ($seq && ord($seq) < 0xC0) {