Commit 0ee4231e authored by Sebastian Kummer's avatar Sebastian Kummer

Merge pull request #532 in ZP/z-push from...

Merge pull request #532 in ZP/z-push from bugfix/ZP-1199-utf-8-truncation-results-in-invalid to develop

* commit '61cbf6a0':
  ZP-1199 For emails with a lot of HTML 3x the requested truncation on plaintext might not be enough.
  ZP-1199 Fixed variable name holding the length of the stream/string.
  ZP-1199 Utils::Utf8_truncate() needs to check that a string is valid UTF-8 even if it's shorter than the requested length.
parents 6c0d5970 61cbf6a0
......@@ -2596,7 +2596,7 @@ class MAPIProvider {
if ($bpReturnType == SYNC_BODYPREFERENCE_PLAIN) {
ZLog::Write(LOGLEVEL_DEBUG, "MAPIProvider->setMessageBody(): truncated plain-text body requested, stripping all links and images");
// Get more data because of the filtering it's most probably going down in size. It's going to be truncated to the correct size below.
$plainbody = stream_get_contents($message->asbody->data, $bpo->GetTruncationSize() * 3);
$plainbody = stream_get_contents($message->asbody->data, $bpo->GetTruncationSize() * 5);
$message->asbody->data = StringStreamWrapper::Open(preg_replace('/<http(s){0,1}:\/\/.*?>/i', '', $plainbody));
}
......
......@@ -136,11 +136,11 @@ class StringStreamWrapper {
public function stream_truncate ($new_size) {
// cut the string!
$this->stringstream = Utils::Utf8_truncate($this->stringstream, $new_size);
$this->streamlength = strlen($this->stringstream);
$this->stringlength = strlen($this->stringstream);
if ($this->position > $this->streamlength) {
ZLog::Write(LOGLEVEL_WARN, sprintf("StringStreamWrapper->stream_truncate(): stream position (%d) ahead of new size of %d. Repositioning pointer to end of stream.", $this->position, $this->streamlength));
$this->position = $this->streamlength;
if ($this->position > $this->stringlength) {
ZLog::Write(LOGLEVEL_WARN, sprintf("StringStreamWrapper->stream_truncate(): stream position (%d) ahead of new size of %d. Repositioning pointer to end of stream.", $this->position, $this->stringlength));
$this->position = $this->stringlength;
}
return true;
}
......
......@@ -388,13 +388,15 @@ class Utils {
// make sure length is always an interger
$length = (int)$length;
if (strlen($string) <= $length)
return $string;
// if the input string is shorter then the trunction, make sure it's valid UTF-8!
if (strlen($string) <= $length) {
$length = strlen($string) - 1;
}
while($length >= 0) {
if ((ord($string[$length]) < 0x80) || (ord($string[$length]) >= 0xC0))
if ((ord($string[$length]) < 0x80) || (ord($string[$length]) >= 0xC0)) {
return substr($string, 0, $length);
}
$length--;
}
return "";
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment