Skip to content
This repository has been archived by the owner on Jan 30, 2020. It is now read-only.

Commit

Permalink
[ContentType] Fix parameters with encoded values
Browse files Browse the repository at this point in the history
  • Loading branch information
Maks3w authored and weierophinney committed Sep 9, 2015
1 parent a3bfd16 commit 28e1399
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 7 deletions.
29 changes: 23 additions & 6 deletions src/Header/ContentType.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,22 @@
namespace Zend\Mail\Header;

use Zend\Mail\Headers;
use Zend\Mime\Mime;

class ContentType implements HeaderInterface
class ContentType implements UnstructuredInterface
{
/**
* @var string
*/
protected $type;

/**
* Header encoding
*
* @var string
*/
protected $encoding = 'ASCII';

/**
* @var array
*/
Expand Down Expand Up @@ -66,6 +74,13 @@ public function getFieldValue($format = HeaderInterface::FORMAT_RAW)

$values = [$prepared];
foreach ($this->parameters as $attribute => $value) {
if (HeaderInterface::FORMAT_ENCODED === $format &&
!Mime::isPrintable($value)) {
$this->encoding = 'UTF-8';
$value = HeaderWrap::wrap($value, $this);
$this->encoding = 'ASCII';
}

$values[] = sprintf('%s="%s"', $attribute, $value);
}

Expand All @@ -74,18 +89,18 @@ public function getFieldValue($format = HeaderInterface::FORMAT_RAW)

public function setEncoding($encoding)
{
// This header must be always in US-ASCII
$this->encoding = $encoding;
return $this;
}

public function getEncoding()
{
return 'ASCII';
return $this->encoding;
}

public function toString()
{
return 'Content-Type: ' . $this->getFieldValue();
return 'Content-Type: ' . $this->getFieldValue(HeaderInterface::FORMAT_ENCODED);
}

/**
Expand Down Expand Up @@ -135,8 +150,10 @@ public function addParameter($name, $value)
if (! HeaderValue::isValid($name)) {
throw new Exception\InvalidArgumentException('Invalid content-type parameter name detected');
}
if (! HeaderValue::isValid($value)) {
throw new Exception\InvalidArgumentException('Invalid content-type parameter value detected');
if (! HeaderWrap::canBeEncoded($value)) {
throw new Exception\InvalidArgumentException(
'Parameter value must be composed of printable US-ASCII or UTF-8 characters.'
);
}

$this->parameters[$name] = $value;
Expand Down
3 changes: 2 additions & 1 deletion test/Header/ContentTypeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use Zend\Mail\Header\ContentType;
use Zend\Mail\Header\Exception\InvalidArgumentException;
use Zend\Mail\Header\HeaderInterface;
use Zend\Mail\Header\UnstructuredInterface;

/**
* @group Zend_Mail
Expand All @@ -22,6 +23,7 @@ public function testImplementsHeaderInterface()
{
$header = new ContentType();

$this->assertInstanceOf(UnstructuredInterface::class, $header);
$this->assertInstanceOf(HeaderInterface::class, $header);
}

Expand Down Expand Up @@ -139,7 +141,6 @@ public function invalidParametersProvider()

// @group ZF2015-04
'invalid name' => ["b\r\na\rr\n", 'baz', $invalidArgumentException, 'parameter name'],
'invalid value' => ['foo', "\nbar\r\nbaz\r", $invalidArgumentException, 'parameter value'],
];
// @codingStandardsIgnoreEnd
}
Expand Down

0 comments on commit 28e1399

Please sign in to comment.