Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 32 additions & 22 deletions lib/Horde/ActiveSync/Utils.php
Original file line number Diff line number Diff line change
Expand Up @@ -218,35 +218,45 @@ public static function createGoid($uid, $options = [])
*/
public static function ensureUtf8($data, $from_charset)
{
$text = Horde_String::convertCharset($data, $from_charset, 'UTF-8');
if (!Horde_String::validUtf8($text)) {
$test_charsets = [
'windows-1252',
'UTF-8',
];
foreach ($test_charsets as $charset) {
if ($charset != $from_charset) {
try {
$text = Horde_String::convertCharset($data, $from_charset, 'UTF-8');
if (Horde_String::validUtf8($text)) {
return $text;
}
} catch (RuntimeException $e) {
// Fall through to alternate charsets below.
}

$test_charsets = [
'windows-1252',
'UTF-8',
];
foreach ($test_charsets as $charset) {
if ($charset != $from_charset) {
try {
$text = Horde_String::convertCharset($data, $charset, 'UTF-8');
if (Horde_String::validUtf8($text)) {
return $text;
}
} catch (RuntimeException $e) {
}
}
// Invalid UTF-8 still found. Strip out non 7-bit characters, or if
// that fails, force a conversion to UTF-8 as a last resort. Need
// to break string into smaller chunks to avoid hitting
// https://bugs.php.net/bug.php?id=37793
$chunk_size = 4000;
$text = '';
while ($data !== false && strlen($data)) {
$test = self::_stripNon7BitChars(substr($data, 0, $chunk_size));
if ($test !== false) {
$text .= $test;
} else {
return Horde_String::convertCharset($data, $from_charset, 'UTF-8', true);
}
$data = substr($data, $chunk_size);
}

// Invalid UTF-8 still found. Strip out non 7-bit characters, or if
// that fails, force a conversion to UTF-8 as a last resort. Need
// to break string into smaller chunks to avoid hitting
// https://bugs.php.net/bug.php?id=37793
$chunk_size = 4000;
$text = '';
while ($data !== false && strlen($data)) {
$test = self::_stripNon7BitChars(substr($data, 0, $chunk_size));
if ($test !== false) {
$text .= $test;
} else {
return Horde_String::convertCharset($data, $from_charset, 'UTF-8', true);
}
$data = substr($data, $chunk_size);
}

return $text;
Expand Down
Loading