8000 [Mime] Fix support for date form parts by fabpot · Pull Request #30482 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[Mime] Fix support for date form parts #30482

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 7, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions src/Symfony/Component/Mime/Encoder/EightBitContentEncoder.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\Mime\Encoder;

/**
* @author Fabien Potencier <fabien@symfony.com>
*
* @experimental in 4.3
*/
final class EightBitContentEncoder implements ContentEncoderInterface
{
public function encodeByteStream($stream, int $maxLineLength = 0): iterable
{
yield from $stream;
}

public function getName(): string
{
return '8bit';
}

public function encodeString(string $string, ?string $charset = 'utf-8', int $firstLineOffset = 0, int $maxLineLength = 0): string
{
return $string;
}
}
11 changes: 0 additions & 11 deletions src/Symfony/Component/Mime/Part/AbstractMultipartPart.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@

namespace Symfony\Component\Mime\Part;

use Symfony\Component\Mime\Exception\LogicException;
use Symfony\Component\Mime\Header\Headers;

/**
Expand Down Expand Up @@ -57,11 +56,6 @@ public function getPreparedHeaders(): Headers
public function bodyToString(): string
{
$parts = $this->getParts();

if (\count($parts) < 2) {
throw new LogicException(sprintf('A "%s" instance must have at least 2 parts.', __CLASS__));
}

$string = '';
foreach ($parts as $part) {
$string .= '--'.$this->getBoundary()."\r\n".$part->toString()."\r\n";
Expand All @@ -74,11 +68,6 @@ public function bodyToString(): string
public function bodyToIterable(): iterable
{
$parts = $this->getParts();

if (\count($parts) < 2) {
throw new LogicException(sprintf('A "%s" instance must have at least 2 parts.', __CLASS__));
}

foreach ($parts as $part) {
yield '--'.$this->getBoundary()."\r\n";
foreach ($part->toIterable() as $chunk) {
Expand Down
26 changes: 15 additions & 11 deletions src/Symfony/Component/Mime/Part/Multipart/FormDataPart.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public function __construct(array $fields = [])
$this->fields[$name] = $value;
}
// HTTP does not support \r\n in header values
$this->getHeaders()->setMaxLineLength(1000);
$this->getHeaders()->setMaxLineLength(-1);
}

public function getMediaSubtype(): string
Expand All @@ -58,34 +58,38 @@ public function getParts(): array
private function prepareFields(array $fields): array
{
$values = [];
foreach ($fields as $name => $value) {
if (\is_array($value)) {
foreach ($value as $v) {
$values[] = $this->preparePart($name, $v);
}
} else {
$values[] = $this->preparePart($name, $value);
array_walk_recursive($fields, function ($item, $key) use (&$values) {
if (!\is_array($item)) {
$values[] = $this->preparePart($key, $item);
}
}
});

return $values;
}

private function preparePart($name, $value): TextPart
{
if (\is_string($value)) {
return $this->configurePart($name, new TextPart($value));
return $this->configurePart($name, new TextPart($value, 'utf-8', 'plain', '8bit'));
}

return $this->configurePart($name, $value);
}

private function configurePart(string $name, TextPart $part): TextPart
{
static $r;

if (null === $r) {
$r = new \ReflectionProperty(TextPart::class, 'encoding');
$r->setAccessible(true);
}

$part->setDisposition('form-data');
$part->setName($name);
// HTTP does not support \r\n in header values
$part->getHeaders()->setMaxLineLength(1000);
$part->getHeaders()->setMaxLineLength(-1);
$r->setValue($part, '8bit');

return $part;
}
Expand Down
15 changes: 9 additions & 6 deletions src/Symfony/Component/Mime/Part/TextPart.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

use Symfony\Component\Mime\Encoder\Base64ContentEncoder;
use Symfony\Component\Mime\Encoder\ContentEncoderInterface;
use Symfony\Component\Mime\Encoder\EightBitContentEncoder;
use Symfony\Component\Mime\Encoder\QpContentEncoder;
use Symfony\Component\Mime\Exception\InvalidArgumentException;
use Symfony\Component\Mime\Header\Headers;
Expand All @@ -31,8 +32,7 @@ class TextPart extends AbstractPart
private $subtype;
private $disposition;
private $name;

protected $encoding;
private $encoding;

/**
* @param resource|string $body
Expand All @@ -49,12 +49,11 @@ public function __construct($body, ?string $charset = 'utf-8', $subtype = 'plain
$this->charset = $charset;
$this->subtype = $subtype;

// FIXME: can also be 7BIT, 8BIT, ...
if (null === $encoding) {
$this->encoding = $this->chooseEncoding();
} else {
if ('quoted-printable' !== $encoding && 'base64' !== $encoding) {
throw new InvalidArgumentException(sprintf('The encoding must be one of "quoted-printable" or "base64" ("%s" given).', $encoding));
if ('quoted-printable' !== $encoding && 'base64' !== $encoding && '8bit' !== $encoding) {
throw new InvalidArgumentException(sprintf('The encoding must be one of "quoted-printable", "base64", or "8bit" ("%s" given).', $encoding));
}
$this->encoding = $encoding;
}
Expand Down Expand Up @@ -149,8 +148,12 @@ public function getPreparedHeaders(): Headers
return $headers;
}

protected function getEncoder(): ContentEncoderInterface
private function getEncoder(): ContentEncoderInterface
{
if ('8bit' === $this->encoding) {
return self::$encoders[$this->encoding] ?? (self::$encoders[$this->encoding] = new EightBitContentEncoder());
}

if ('quoted-printable' === $this->encoding) {
return self::$encoders[$this->encoding] ?? (self::$encoders[$this->encoding] = new QpContentEncoder());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ class FormDataPartTest extends TestCase
{
public function testConstructor()
{
$r = new \ReflectionProperty(TextPart::class, 'encoding');
$r->setAccessible(true);

$b = new TextPart('content');
$c = DataPart::fromPath($file = __DIR__.'/../../Fixtures/mimetypes/test.gif');
$f = new FormDataPart([
Expand All @@ -29,16 +32,35 @@ public function testConstructor()
]);
$this->assertEquals('multipart', $f->getMediaType());
$this->assertEquals('form-data', $f->getMediaSubtype());
$t = new TextPart($content);
$t = new TextPart($content, 'utf-8', 'plain', '8bit');
$t->setDisposition('form-data');
$t->setName('foo');
$t->getHeaders()->setMaxLineLength(1000);
$t->getHeaders()->setMaxLineLength(-1);
$b->setDisposition('form-data');
$b->setName('bar');
$b->getHeaders()->setMaxLineLength(1000);
$b->getHeaders()->setMaxLineLength(-1);
$r->setValue($b, '8bit');
$c->setDisposition('form-data');
$c->setName('baz');
$c->getHeaders()->setMaxLineLength(1000);
$c->getHeaders()->setMaxLineLength(-1);
$r->setValue($c, '8bit');
$this->assertEquals([$t, $b, $c], $f->getParts());
}

public function testToString()
{
$p = DataPart::fromPath($file = __DIR__.'/../../Fixtures/mimetypes/test.gif');
$this->assertEquals(base64_encode(file_get_contents($file)), $p->bodyToString());
}

public function testContentLineLength()
{
$f = new FormDataPart([
'foo' => new DataPart($foo = str_repeat('foo', 1000), 'foo.txt', 'text/plain'),
'bar' => $bar = str_repeat('bar', 1000),
]);
$parts = $f->getParts();
$this->assertEquals($foo, $parts[0]->bodyToString());
$this->assertEquals($bar, $parts[1]->bodyToString());
}
}
0