Skip to content

Commit

Permalink
Merge branch 'hotfix/5570'
Browse files Browse the repository at this point in the history
  • Loading branch information
weierophinney committed Mar 3, 2014
2 parents ecc2afd + 3ecb54c commit a732e76
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 1 deletion.
14 changes: 13 additions & 1 deletion library/Zend/Mime/Part.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ class Part
public $language;
protected $content;
protected $isStream = false;
protected $filters = array();


/**
Expand Down Expand Up @@ -79,6 +80,9 @@ public function getEncodedStream($EOL = Mime::LINEEND)
//stream_filter_remove(); // ??? is that right?
switch ($this->encoding) {
case Mime::ENCODING_QUOTEDPRINTABLE:
if (array_key_exists(Mime::ENCODING_QUOTEDPRINTABLE, $this->filters)) {
stream_filter_remove($this->filters[Mime::ENCODING_QUOTEDPRINTABLE]);
}
$filter = stream_filter_append(
$this->content,
'convert.quoted-printable-encode',
Expand All @@ -88,11 +92,15 @@ public function getEncodedStream($EOL = Mime::LINEEND)
'line-break-chars' => $EOL
)
);
$this->filters[Mime::ENCODING_QUOTEDPRINTABLE] = $filter;
if (!is_resource($filter)) {
throw new Exception\RuntimeException('Failed to append quoted-printable filter');
}
break;
case Mime::ENCODING_BASE64:
if (array_key_exists(Mime::ENCODING_BASE64,$this->filters)) {
stream_filter_remove($this->filters[Mime::ENCODING_BASE64]);
}
$filter = stream_filter_append(
$this->content,
'convert.base64-encode',
Expand All @@ -102,6 +110,7 @@ public function getEncodedStream($EOL = Mime::LINEEND)
'line-break-chars' => $EOL
)
);
$this->filters[Mime::ENCODING_BASE64] = $filter;
if (!is_resource($filter)) {
throw new Exception\RuntimeException('Failed to append base64 filter');
}
Expand All @@ -120,7 +129,10 @@ public function getEncodedStream($EOL = Mime::LINEEND)
public function getContent($EOL = Mime::LINEEND)
{
if ($this->isStream) {
return stream_get_contents($this->getEncodedStream($EOL));
$encodedStream = $this->getEncodedStream($EOL);
$encodedStreamContents = stream_get_contents($encodedStream);
rewind($encodedStream);
return $encodedStreamContents;
}
return Mime::encode($this->content, $this->encoding, $EOL);
}
Expand Down
26 changes: 26 additions & 0 deletions tests/ZendTest/Mime/PartTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -103,4 +103,30 @@ public function testGetRawContentFromPart()
{
$this->assertEquals($this->testText, $this->part->getRawContent());
}

/**
* @link https://github.com/zendframework/zf2/issues/5428
* @group 5428
*/
public function testContentEncodingWithStreamReadTwiceINaRow()
{
$testfile = realpath(__FILE__);
$original = file_get_contents($testfile);

$fp = fopen($testfile,'rb');
$part = new Mime\Part($fp);
$part->encoding = Mime\Mime::ENCODING_BASE64;
$contentEncodedFirstTime = $part->getContent();
$contentEncodedSecondTime = $part->getContent();
$this->assertEquals($contentEncodedFirstTime, $contentEncodedSecondTime);
fclose($fp);

$fp = fopen($testfile,'rb');
$part = new Mime\Part($fp);
$part->encoding = Mime\Mime::ENCODING_QUOTEDPRINTABLE;
$contentEncodedFirstTime = $part->getContent();
$contentEncodedSecondTime = $part->getContent();
$this->assertEquals($contentEncodedFirstTime, $contentEncodedSecondTime);
fclose($fp);
}
}

0 comments on commit a732e76

Please sign in to comment.