summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBenny Baumann <BenBE@geshi.org>2015-01-03 16:09:11 +0100
committerBenny Baumann <BenBE@geshi.org>2015-01-03 16:09:11 +0100
commit8e38a22d467d56a29d20ad0b75c4442cd9268134 (patch)
tree1d3313a74c02563d498dcab437cb17f64502a2f3
parentfe18b916965427af18bc80f6f02886257f1599bf (diff)
parent468c97305b684f7cd0aa09449f18a424cb7f7fd7 (diff)
downloadcacert-devel-8e38a22d467d56a29d20ad0b75c4442cd9268134.tar.gz
cacert-devel-8e38a22d467d56a29d20ad0b75c4442cd9268134.tar.xz
cacert-devel-8e38a22d467d56a29d20ad0b75c4442cd9268134.zip
Merge branch 'bug-1354' into testserver-stable
-rw-r--r--includes/general.php52
1 files changed, 51 insertions, 1 deletions
diff --git a/includes/general.php b/includes/general.php
index 53f5abe..fe43a96 100644
--- a/includes/general.php
+++ b/includes/general.php
@@ -544,6 +544,56 @@
return(0);
}
+ /**
+ * Decode UTF-8 byte sequences into HTML entities
+ *
+ * @see https://blog.benny-baumann.de/?p=332
+ */
+ function charset_decode_utf8_callback($match) {
+ $m = $match[0];
+
+ switch(strlen($m)) {
+ case 6:
+ // decode six byte unicode characters
+ return '&#'.((ord($m[0])-252)*1073741824+(ord($m[1])-200)*16777216+(ord($m[2])-200)*262144+(ord($m[3])-128)*4096+(ord($m[4])-128)*64+(ord($m[5])-128)).';';
+ case 5:
+ // decode five byte unicode characters
+ return '&#'.((ord($m[0])-248)*16777216+(ord($m[1])-200)*262144+(ord($m[2])-128)*4096+(ord($m[3])-128)*64+(ord($m[4])-128)).';';
+ case 4:
+ // decode four byte unicode characters
+ return '&#'.((ord($m[0])-240)*262144+(ord($m[1])-128)*4096+(ord($m[2])-128)*64+(ord($m[3])-128)).';';
+ case 3:
+ // decode three byte unicode characters
+ return '&#'.((ord($m[0])-224)*4096+(ord($m[1])-128)*64+(ord($m[2])-128)).';';
+ case 2:
+ // decode two byte unicode characters
+ return '&#'.((ord($m[0])-192)*64+(ord($m[1])-128)).';';
+ default:
+ return $m;
+ }
+ }
+
+ /**
+ * Decode utf-8 strings
+ * @param string $string Encoded string
+ * @return string Decoded string
+ * @see https://blog.benny-baumann.de/?p=332
+ */
+ function charset_decode_utf8 ($string) {
+ // decode 2-6 byte unicode characters
+ $string = preg_replace_callback("/[\374-\375][\200-\277][\200-\277][\200-\277][\200-\277][\200-\277]|".
+ "[\370-\373][\200-\277][\200-\277][\200-\277][\200-\277]|".
+ "[\360-\367][\200-\277][\200-\277][\200-\277]|".
+ "[\340-\357][\200-\277][\200-\277]|".
+ "[\300-\337][\200-\277]/",
+ 'charset_decode_utf8_callback', $string);
+
+ // remove broken unicode
+ $string = preg_replace("/[\200-\237]|\240|[\241-\377]/",'?',$string);
+
+ return $string;
+ }
+
function gpg_hex2bin($data)
{
while(strstr($data, "\\x"))
@@ -554,7 +604,7 @@
$after = substr($data, $pos + 4);
$data = $before.$char.$after;
}
- return(utf8_decode($data));
+ return charset_decode_utf8($data);
}
function signmail($to, $subject, $message, $from, $replyto = "")