diff --git a/src/Part.php b/src/Part.php index 4380cf6..2fde6cc 100644 --- a/src/Part.php +++ b/src/Part.php @@ -26,6 +26,7 @@ class Part public $language; protected $content; protected $isStream = false; + protected $filters = array(); /** @@ -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', @@ -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', @@ -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'); } @@ -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); } diff --git a/test/PartTest.php b/test/PartTest.php index 898df3a..2812b65 100644 --- a/test/PartTest.php +++ b/test/PartTest.php @@ -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); + } }