[Mime] strengthen is_resource() checks

This commit is contained in:
Nicolas Grekas 2019-12-19 14:13:36 +01:00
parent f632b76824
commit be9c675710
5 changed files with 13 additions and 39 deletions

View File

@ -97,10 +97,7 @@ final class CharacterStream
}
}
if (\is_resource($input)) {
$blocks = 512;
if (stream_get_meta_data($input)['seekable'] ?? false) {
rewind($input);
}
$blocks = 16372;
while (false !== $read = fread($input, $blocks)) {
$this->write($read);
}

View File

@ -464,14 +464,8 @@ class Email extends Message
$htmlPart = null;
$html = $this->html;
if (null !== $this->html) {
if (\is_resource($html)) {
if (stream_get_meta_data($html)['seekable'] ?? false) {
rewind($html);
}
$html = stream_get_contents($html);
}
$htmlPart = new TextPart($html, $this->htmlCharset, 'html');
$html = $htmlPart->getBody();
preg_match_all('(<img\s+[^>]*src\s*=\s*(?:([\'"])cid:([^"]+)\\1|cid:([^>\s]+)))i', $html, $names);
$names = array_filter(array_unique(array_merge($names[2], $names[3])));
}
@ -559,28 +553,16 @@ class Email extends Message
public function __serialize(): array
{
if (\is_resource($this->text)) {
if (stream_get_meta_data($this->text)['seekable'] ?? false) {
rewind($this->text);
}
$this->text = stream_get_contents($this->text);
$this->text = (new TextPart($this->text))->getBody();
}
if (\is_resource($this->html)) {
if (stream_get_meta_data($this->html)['seekable'] ?? false) {
rewind($this->html);
}
$this->html = stream_get_contents($this->html);
$this->html = (new TextPart($this->html))->getBody();
}
foreach ($this->attachments as $i => $attachment) {
if (isset($attachment['body']) && \is_resource($attachment['body'])) {
if (stream_get_meta_data($attachment['body'])['seekable'] ?? false) {
rewind($attachment['body']);
}
$this->attachments[$i]['body'] = stream_get_contents($attachment['body']);
$this->attachments[$i]['body'] = (new TextPart($attachment['body']))->getBody();
}
}

View File

@ -32,11 +32,8 @@ final class Base64ContentEncoder extends Base64Encoder implements ContentEncoder
throw new RuntimeException('Unable to set the base64 content encoder to the filter.');
}
if (stream_get_meta_data($stream)['seekable'] ?? false) {
rewind($stream);
}
while (!feof($stream)) {
yield fread($stream, 8192);
yield fread($stream, 16372);
}
stream_filter_remove($filter);
}

View File

@ -23,10 +23,6 @@ final class QpContentEncoder implements ContentEncoderInterface
}
// we don't use PHP stream filters here as the content should be small enough
if (stream_get_meta_data($stream)['seekable'] ?? false) {
rewind($stream);
}
yield $this->encodeString(stream_get_contents($stream), 'utf-8', 0, $maxLineLength);
}

View File

@ -31,6 +31,7 @@ class TextPart extends AbstractPart
private $disposition;
private $name;
private $encoding;
private $seekable;
/**
* @param resource|string $body
@ -46,6 +47,7 @@ class TextPart extends AbstractPart
$this->body = $body;
$this->charset = $charset;
$this->subtype = $subtype;
$this->seekable = \is_resource($body) ? stream_get_meta_data($body)['seekable'] && 0 === fseek($body, 0, SEEK_CUR) : null;
if (null === $encoding) {
$this->encoding = $this->chooseEncoding();
@ -93,11 +95,11 @@ class TextPart extends AbstractPart
public function getBody(): string
{
if (!\is_resource($this->body)) {
if (null === $this->seekable) {
return $this->body;
}
if (stream_get_meta_data($this->body)['seekable'] ?? false) {
if ($this->seekable) {
rewind($this->body);
}
@ -111,8 +113,8 @@ class TextPart extends AbstractPart
public function bodyToIterable(): iterable
{
if (\is_resource($this->body)) {
if (stream_get_meta_data($this->body)['seekable'] ?? false) {
if (null !== $this->seekable) {
if ($this->seekable) {
rewind($this->body);
}
yield from $this->getEncoder()->encodeByteStream($this->body);
@ -185,7 +187,7 @@ class TextPart extends AbstractPart
public function __sleep()
{
// convert resources to strings for serialization
if (\is_resource($this->body)) {
if (null !== $this->seekable) {
$this->body = $this->getBody();
}