Skip to content

Commit

Permalink
Attempt to sniff out if Thread-Index hdr needs mime encoding
Browse files Browse the repository at this point in the history
  • Loading branch information
mrubinsk committed Oct 17, 2023
1 parent aa90556 commit 57b8ab2
Showing 1 changed file with 51 additions and 1 deletion.
52 changes: 51 additions & 1 deletion lib/Horde/Mime/Headers/ThreadIndex.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,56 @@ public static function getHandles()
*/
protected function _sendEncode($opts)
{
return array(Horde_Mime::encode($this->value, $opts['charset']));
$encoded = Horde_Mime::encode($this->value, $opts['charset']);

if ($encoded != $this->value) {
return array($this->value);
}

$len = strlen($this->value);
$crlf = 0;
$needs_encoding = false;
for ($i = 0; $i < $len; ++$i) {
$chr = ord($this->value[$i]);
switch ($chr) {
case 0:
// NULLs not valid here, should have
// been caught above.
return array('');
case 10:
case 13:
$crlf = 0;
break;
default:
if (++$crlf > 998) {
$needs_encoding = true;
break 2;
}
break;
}
}

if ($needs_encoding) {
$delim = '=?' . $opts['charset'] . '?b?';
$parts = explode(
self::EOL,
rtrim(
chunk_split(
base64_encode($this->value),
/* strlen($delim) + 2 = space taken by MIME
* delimiter */
intval((75 - strlen($delim) + 2) / 4) * 4
)
)
);
$tmp = array();
foreach ($parts as $val) {
$tmp[] = $delim . $val . '?=';
}

return trim(implode(' ', $tmp));
}

return array($this->value);
}
}

0 comments on commit 57b8ab2

Please sign in to comment.