From 2965a981b7ec22866fbdf2d567d87e2d068d3617 Mon Sep 17 00:00:00 2001
From: Thomas Bruederli <thomas@roundcube.net>
Date: Fri, 31 Jul 2015 16:04:08 -0400
Subject: [PATCH] Allow to search and import missing PGP pubkeys from keyservers using Publickey.js
---
program/lib/Roundcube/rcube_charset.php | 178 ++++++++++++++++++++++++++++++++++------------------------
1 files changed, 104 insertions(+), 74 deletions(-)
diff --git a/program/lib/Roundcube/rcube_charset.php b/program/lib/Roundcube/rcube_charset.php
index e8cce00..048961a 100644
--- a/program/lib/Roundcube/rcube_charset.php
+++ b/program/lib/Roundcube/rcube_charset.php
@@ -2,8 +2,6 @@
/*
+-----------------------------------------------------------------------+
- | program/include/rcube_charset.php |
- | |
| This file is part of the Roundcube Webmail client |
| Copyright (C) 2005-2012, The Roundcube Dev Team |
| Copyright (C) 2011-2012, Kolab Systems AG |
@@ -15,7 +13,6 @@
| |
| PURPOSE: |
| Provide charset conversion functionality |
- | |
+-----------------------------------------------------------------------+
| Author: Thomas Bruederli <roundcube@gmail.com> |
| Author: Aleksander Machniak <alec@alec.pl> |
@@ -169,7 +166,7 @@
*
* @param string Input string
* @param string Suspected charset of the input string
- * @param string Target charset to convert to; defaults to RCMAIL_CHARSET
+ * @param string Target charset to convert to; defaults to RCUBE_CHARSET
*
* @return string Converted string
*/
@@ -178,9 +175,8 @@
static $iconv_options = null;
static $mbstring_list = null;
static $mbstring_sch = null;
- static $conv = null;
- $to = empty($to) ? RCMAIL_CHARSET : $to;
+ $to = empty($to) ? RCUBE_CHARSET : $to;
$from = self::parse_charset($from);
// It is a common case when UTF-16 charset is used with US-ASCII content (#1488654)
@@ -202,10 +198,13 @@
$iconv_options = '';
}
}
+ else {
+ $iconv_options = false;
+ }
}
// convert charset using iconv module
- if ($iconv_options !== null && $from != 'UTF7-IMAP' && $to != 'UTF7-IMAP') {
+ if ($iconv_options !== false && $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);
@@ -227,10 +226,13 @@
$mbstring_list = mb_list_encodings();
$mbstring_list = array_map('strtoupper', $mbstring_list);
}
+ else {
+ $mbstring_list = false;
+ }
}
// convert charset using mbstring module
- if ($mbstring_list !== null) {
+ if ($mbstring_list !== false) {
$aliases['WINDOWS-1257'] = 'ISO-8859-13';
// it happens that mbstring supports ASCII but not US-ASCII
if (($from == 'US-ASCII' || $to == 'US-ASCII') && !in_array('US-ASCII', $mbstring_list)) {
@@ -270,17 +272,8 @@
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;
- }
+ else {
+ trigger_error("No suitable function found for UTF-8 encoding");
}
}
@@ -295,17 +288,8 @@
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;
- }
+ else {
+ trigger_error("No suitable function found for UTF-8 decoding");
}
}
@@ -649,12 +633,13 @@
/**
* A method to guess character set of a string.
*
- * @param string $string String.
- * @param string $failover Default result for failover.
+ * @param string $string String
+ * @param string $failover Default result for failover
+ * @param string $language User language
*
* @return string Charset name
*/
- public static function detect($string, $failover='')
+ public static function detect($string, $failover = null, $language = null)
{
if (substr($string, 0, 4) == "\0\0\xFE\xFF") return 'UTF-32BE'; // Big Endian
if (substr($string, 0, 4) == "\xFF\xFE\0\0") return 'UTF-32LE'; // Little Endian
@@ -668,39 +653,80 @@
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')) {
- // FIXME: the order is important, because sometimes
- // iso string is detected as euc-jp and etc.
- $enc = 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',
- 'ISO-8859-10', 'ISO-8859-13', 'ISO-8859-14', 'ISO-8859-15', 'ISO-8859-16',
- 'WINDOWS-1252', 'WINDOWS-1251', 'EUC-JP', 'EUC-TW', 'KOI8-R', 'BIG5',
- 'ISO-2022-KR', 'ISO-2022-JP',
- );
-
- $result = mb_detect_encoding($string, join(',', $enc));
+ if (empty($language)) {
+ $rcube = rcube::get_instance();
+ $language = $rcube->get_user_language();
}
- else {
- // No match, check for UTF-8
- // from http://w3.org/International/questions/qa-forms-utf-8.html
- if (preg_match('/\A(
- [\x09\x0A\x0D\x20-\x7E]
- | [\xC2-\xDF][\x80-\xBF]
- | \xE0[\xA0-\xBF][\x80-\xBF]
- | [\xE1-\xEC\xEE\xEF][\x80-\xBF]{2}
- | \xED[\x80-\x9F][\x80-\xBF]
- | \xF0[\x90-\xBF][\x80-\xBF]{2}
- | [\xF1-\xF3][\x80-\xBF]{3}
- | \xF4[\x80-\x8F][\x80-\xBF]{2}
- )*\z/xs', substr($string, 0, 2048))
- ) {
- return 'UTF-8';
+
+ // 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;
+ }
}
}
- return $result ? $result : $failover;
+ 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',
+ 'ISO-8859-10', 'ISO-8859-13', 'ISO-8859-14', 'ISO-8859-15', 'ISO-8859-16',
+ 'WINDOWS-1252', 'WINDOWS-1251', 'EUC-JP', 'EUC-TW', 'KOI8-R', 'BIG-5',
+ 'ISO-2022-KR', 'ISO-2022-JP',
+ );
+ }
+
+ $encodings = array_unique(array_merge($prio, mb_list_encodings()));
+
+ if ($encoding = mb_detect_encoding($string, $encodings)) {
+ return $encoding;
+ }
+ }
+
+ // No match, check for UTF-8
+ // from http://w3.org/International/questions/qa-forms-utf-8.html
+ if (preg_match('/\A(
+ [\x09\x0A\x0D\x20-\x7E]
+ | [\xC2-\xDF][\x80-\xBF]
+ | \xE0[\xA0-\xBF][\x80-\xBF]
+ | [\xE1-\xEC\xEE\xEF][\x80-\xBF]{2}
+ | \xED[\x80-\x9F][\x80-\xBF]
+ | \xF0[\x90-\xBF][\x80-\xBF]{2}
+ | [\xF1-\xF3][\x80-\xBF]{3}
+ | \xF4[\x80-\x8F][\x80-\xBF]{2}
+ )*\z/xs', substr($string, 0, 2048))
+ ) {
+ return 'UTF-8';
+ }
+
+ return $failover;
}
@@ -727,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;
}
}
@@ -758,30 +789,29 @@
// 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 : '';
}
--
Gitblit v1.9.1