[Mime] strengthen is_resource() checks
This commit is contained in:
parent
f632b76824
commit
be9c675710
@ -97,10 +97,7 @@ final class CharacterStream
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (\is_resource($input)) {
|
if (\is_resource($input)) {
|
||||||
$blocks = 512;
|
$blocks = 16372;
|
||||||
if (stream_get_meta_data($input)['seekable'] ?? false) {
|
|
||||||
rewind($input);
|
|
||||||
}
|
|
||||||
while (false !== $read = fread($input, $blocks)) {
|
while (false !== $read = fread($input, $blocks)) {
|
||||||
$this->write($read);
|
$this->write($read);
|
||||||
}
|
}
|
||||||
|
@ -464,14 +464,8 @@ class Email extends Message
|
|||||||
$htmlPart = null;
|
$htmlPart = null;
|
||||||
$html = $this->html;
|
$html = $this->html;
|
||||||
if (null !== $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');
|
$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);
|
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])));
|
$names = array_filter(array_unique(array_merge($names[2], $names[3])));
|
||||||
}
|
}
|
||||||
@ -559,28 +553,16 @@ class Email extends Message
|
|||||||
public function __serialize(): array
|
public function __serialize(): array
|
||||||
{
|
{
|
||||||
if (\is_resource($this->text)) {
|
if (\is_resource($this->text)) {
|
||||||
if (stream_get_meta_data($this->text)['seekable'] ?? false) {
|
$this->text = (new TextPart($this->text))->getBody();
|
||||||
rewind($this->text);
|
|
||||||
}
|
|
||||||
|
|
||||||
$this->text = stream_get_contents($this->text);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (\is_resource($this->html)) {
|
if (\is_resource($this->html)) {
|
||||||
if (stream_get_meta_data($this->html)['seekable'] ?? false) {
|
$this->html = (new TextPart($this->html))->getBody();
|
||||||
rewind($this->html);
|
|
||||||
}
|
|
||||||
|
|
||||||
$this->html = stream_get_contents($this->html);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
foreach ($this->attachments as $i => $attachment) {
|
foreach ($this->attachments as $i => $attachment) {
|
||||||
if (isset($attachment['body']) && \is_resource($attachment['body'])) {
|
if (isset($attachment['body']) && \is_resource($attachment['body'])) {
|
||||||
if (stream_get_meta_data($attachment['body'])['seekable'] ?? false) {
|
$this->attachments[$i]['body'] = (new TextPart($attachment['body']))->getBody();
|
||||||
rewind($attachment['body']);
|
|
||||||
}
|
|
||||||
|
|
||||||
$this->attachments[$i]['body'] = stream_get_contents($attachment['body']);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -32,11 +32,8 @@ final class Base64ContentEncoder extends Base64Encoder implements ContentEncoder
|
|||||||
throw new RuntimeException('Unable to set the base64 content encoder to the filter.');
|
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)) {
|
while (!feof($stream)) {
|
||||||
yield fread($stream, 8192);
|
yield fread($stream, 16372);
|
||||||
}
|
}
|
||||||
stream_filter_remove($filter);
|
stream_filter_remove($filter);
|
||||||
}
|
}
|
||||||
|
@ -23,10 +23,6 @@ final class QpContentEncoder implements ContentEncoderInterface
|
|||||||
}
|
}
|
||||||
|
|
||||||
// we don't use PHP stream filters here as the content should be small enough
|
// 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);
|
yield $this->encodeString(stream_get_contents($stream), 'utf-8', 0, $maxLineLength);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -31,6 +31,7 @@ class TextPart extends AbstractPart
|
|||||||
private $disposition;
|
private $disposition;
|
||||||
private $name;
|
private $name;
|
||||||
private $encoding;
|
private $encoding;
|
||||||
|
private $seekable;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param resource|string $body
|
* @param resource|string $body
|
||||||
@ -46,6 +47,7 @@ class TextPart extends AbstractPart
|
|||||||
$this->body = $body;
|
$this->body = $body;
|
||||||
$this->charset = $charset;
|
$this->charset = $charset;
|
||||||
$this->subtype = $subtype;
|
$this->subtype = $subtype;
|
||||||
|
$this->seekable = \is_resource($body) ? stream_get_meta_data($body)['seekable'] && 0 === fseek($body, 0, SEEK_CUR) : null;
|
||||||
|
|
||||||
if (null === $encoding) {
|
if (null === $encoding) {
|
||||||
$this->encoding = $this->chooseEncoding();
|
$this->encoding = $this->chooseEncoding();
|
||||||
@ -93,11 +95,11 @@ class TextPart extends AbstractPart
|
|||||||
|
|
||||||
public function getBody(): string
|
public function getBody(): string
|
||||||
{
|
{
|
||||||
if (!\is_resource($this->body)) {
|
if (null === $this->seekable) {
|
||||||
return $this->body;
|
return $this->body;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (stream_get_meta_data($this->body)['seekable'] ?? false) {
|
if ($this->seekable) {
|
||||||
rewind($this->body);
|
rewind($this->body);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -111,8 +113,8 @@ class TextPart extends AbstractPart
|
|||||||
|
|
||||||
public function bodyToIterable(): iterable
|
public function bodyToIterable(): iterable
|
||||||
{
|
{
|
||||||
if (\is_resource($this->body)) {
|
if (null !== $this->seekable) {
|
||||||
if (stream_get_meta_data($this->body)['seekable'] ?? false) {
|
if ($this->seekable) {
|
||||||
rewind($this->body);
|
rewind($this->body);
|
||||||
}
|
}
|
||||||
yield from $this->getEncoder()->encodeByteStream($this->body);
|
yield from $this->getEncoder()->encodeByteStream($this->body);
|
||||||
@ -185,7 +187,7 @@ class TextPart extends AbstractPart
|
|||||||
public function __sleep()
|
public function __sleep()
|
||||||
{
|
{
|
||||||
// convert resources to strings for serialization
|
// convert resources to strings for serialization
|
||||||
if (\is_resource($this->body)) {
|
if (null !== $this->seekable) {
|
||||||
$this->body = $this->getBody();
|
$this->body = $this->getBody();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user