Skip to content

Commit

Permalink
[mms] Fix parsing a base MIME-compliant message with no Content-Type …
Browse files Browse the repository at this point in the history
…information.

We were treating as application/octet-stream, but this is correctly
treated as "text/plain; charset=us-ascii" instead.

Additionally, return us-ascii as a value from getCharset() - that IS the
charset of the part.  Whether to add to Content-Type should be
determined in getType() instead.
  • Loading branch information
slusarz committed Nov 11, 2014
1 parent b930037 commit 44fd341
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 14 deletions.
22 changes: 8 additions & 14 deletions framework/Mime/lib/Horde/Mime/Part.php
Expand Up @@ -626,7 +626,8 @@ public function getType($charset = false)
$type = $ptype . '/' . $this->getSubType();
if ($charset &&
($ptype == 'text') &&
($charset = $this->getCharset())) {
($charset = $this->getCharset() &&
($charset !== 'us-ascii'))) {
$type .= '; charset=' . $charset;
}

Expand Down Expand Up @@ -697,21 +698,14 @@ public function setCharset($charset)
public function getCharset()
{
$charset = $this->getContentTypeParameter('charset');
if (is_null($charset) && $this->getPrimaryType() != 'text') {
return null;
}

$charset = Horde_String::lower($charset);

if ($this->getPrimaryType() == 'text') {
$d_charset = Horde_String::lower(self::$defaultCharset);
if ($d_charset != 'us-ascii' &&
(!$charset || $charset == 'us-ascii')) {
return $d_charset;
if (is_null($charset)) {
if ($this->getPrimaryType() != 'text') {
return null;
}
$charset = self::$defaultCharset;
}

return $charset;
return Horde_String::lower($charset);
}

/**
Expand Down Expand Up @@ -2008,7 +2002,7 @@ protected static function _getStructure($header, $body,
array $opts = array())
{
$opts = array_merge(array(
'ctype' => 'application/octet-stream',
'ctype' => 'text/plain',
'forcemime' => false,
'level' => 0,
'no_body' => false
Expand Down
2 changes: 2 additions & 0 deletions framework/Mime/package.xml
Expand Up @@ -28,6 +28,7 @@
</stability>
<license uri="http://www.horde.org/licenses/lgpl21">LGPL-2.1</license>
<notes>
* [mms] Fix parsing a base MIME-compliant message with no Content-Type information.
* [mms] Horde_Mime::is8bit() no longer requires the charset parameter.
* [mms] Improved MIME encoding.
* [mms] Deprecated Horde_Mime::quotedPrintableEncode() and move to Horde_Mime_QuotedPrintable.
Expand Down Expand Up @@ -1460,6 +1461,7 @@
<date>2014-10-28</date>
<license uri="http://www.horde.org/licenses/lgpl21">LGPL-2.1</license>
<notes>
* [mms] Fix parsing a base MIME-compliant message with no Content-Type information.
* [mms] Horde_Mime::is8bit() no longer requires the charset parameter.
* [mms] Improved MIME encoding.
* [mms] Deprecated Horde_Mime::quotedPrintableEncode() and move to Horde_Mime_QuotedPrintable.
Expand Down
16 changes: 16 additions & 0 deletions framework/Mime/test/Horde/Mime/PartTest.php
Expand Up @@ -472,6 +472,22 @@ public function testSettingBytes()
);
}

public function testMimeMessageWithNoContentType()
{
$msg = file_get_contents(__DIR__ . '/fixtures/sample_msg4.txt');
$part = Horde_Mime_Part::parseMessage($msg);

$this->assertEquals(
'text/plain',
$part->getType()
);

$this->assertEquals(
'us-ascii',
$part->getCharset()
);
}

protected function _getTestPart()
{
$part = new Horde_Mime_Part();
Expand Down
9 changes: 9 additions & 0 deletions framework/Mime/test/Horde/Mime/fixtures/sample_msg4.txt
@@ -0,0 +1,9 @@
Message-ID: <asdl8ahwhoadsadl@example.com>
Date: Tue, 07 Jul 2013 10:21:48 -0600
From: "Test Q. User" <test@example.com>
To: foo@example.com
Subject: Test
MIME-Version: 1.0


Test.

0 comments on commit 44fd341

Please sign in to comment.