8889841cMultipart/AlternativePart.php000064400000001047150521521610012343 0ustar00 * * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ namespace Symfony\Component\Mime\Part\Multipart; use Symfony\Component\Mime\Part\AbstractMultipartPart; /** * @author Fabien Potencier */ final class AlternativePart extends AbstractMultipartPart { public function getMediaSubtype(): string { return 'alternative'; } } Multipart/MixedPart.php000064400000001033150521521610011126 0ustar00 * * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ namespace Symfony\Component\Mime\Part\Multipart; use Symfony\Component\Mime\Part\AbstractMultipartPart; /** * @author Fabien Potencier */ final class MixedPart extends AbstractMultipartPart { public function getMediaSubtype(): string { return 'mixed'; } } Multipart/FormDataPart.php000064400000006271150521521620011567 0ustar00 * * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ namespace Symfony\Component\Mime\Part\Multipart; use Symfony\Component\Mime\Exception\InvalidArgumentException; use Symfony\Component\Mime\Part\AbstractMultipartPart; use Symfony\Component\Mime\Part\DataPart; use Symfony\Component\Mime\Part\TextPart; /** * Implements RFC 7578. * * @author Fabien Potencier */ final class FormDataPart extends AbstractMultipartPart { private $fields = []; /** * @param array $fields */ public function __construct(array $fields = []) { parent::__construct(); foreach ($fields as $name => $value) { if (!\is_string($value) && !\is_array($value) && !$value instanceof TextPart) { throw new InvalidArgumentException(sprintf('A form field value can only be a string, an array, or an instance of TextPart ("%s" given).', get_debug_type($value))); } $this->fields[$name] = $value; } // HTTP does not support \r\n in header values $this->getHeaders()->setMaxLineLength(\PHP_INT_MAX); } public function getMediaSubtype(): string { return 'form-data'; } public function getParts(): array { return $this->prepareFields($this->fields); } private function prepareFields(array $fields): array { $values = []; $prepare = function ($item, $key, $root = null) use (&$values, &$prepare) { if (\is_int($key) && \is_array($item)) { if (1 !== \count($item)) { throw new InvalidArgumentException(sprintf('Form field values with integer keys can only have one array element, the key being the field name and the value being the field value, %d provided.', \count($item))); } $key = key($item); $item = $item[$key]; } $fieldName = null !== $root ? sprintf('%s[%s]', $root, $key) : $key; if (\is_array($item)) { array_walk($item, $prepare, $fieldName); return; } $values[] = $this->preparePart($fieldName, $item); }; array_walk($fields, $prepare); return $values; } private function preparePart(string $name, $value): TextPart { if (\is_string($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(\PHP_INT_MAX); $r->setValue($part, '8bit'); return $part; } } Multipart/DigestPart.php000064400000001266150521521620011310 0ustar00 * * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ namespace Symfony\Component\Mime\Part\Multipart; use Symfony\Component\Mime\Part\AbstractMultipartPart; use Symfony\Component\Mime\Part\MessagePart; /** * @author Fabien Potencier */ final class DigestPart extends AbstractMultipartPart { public function __construct(MessagePart ...$parts) { parent::__construct(...$parts); } public function getMediaSubtype(): string { return 'digest'; } } Multipart/RelatedPart.php000064400000002553150521521620011451 0ustar00 * * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ namespace Symfony\Component\Mime\Part\Multipart; use Symfony\Component\Mime\Part\AbstractMultipartPart; use Symfony\Component\Mime\Part\AbstractPart; /** * @author Fabien Potencier */ final class RelatedPart extends AbstractMultipartPart { private $mainPart; public function __construct(AbstractPart $mainPart, AbstractPart $part, AbstractPart ...$parts) { $this->mainPart = $mainPart; $this->prepareParts($part, ...$parts); parent::__construct($part, ...$parts); } public function getParts(): array { return array_merge([$this->mainPart], parent::getParts()); } public function getMediaSubtype(): string { return 'related'; } private function generateContentId(): string { return bin2hex(random_bytes(16)).'@symfony'; } private function prepareParts(AbstractPart ...$parts): void { foreach ($parts as $part) { if (!$part->getHeaders()->has('Content-ID')) { $part->getHeaders()->setHeaderBody('Id', 'Content-ID', $this->generateContentId()); } } } } MessagePart.php000064400000002355150521521620007474 0ustar00 * * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ namespace Symfony\Component\Mime\Part; use Symfony\Component\Mime\Message; use Symfony\Component\Mime\RawMessage; /** * @final * * @author Fabien Potencier */ class MessagePart extends DataPart { private $message; public function __construct(RawMessage $message) { if ($message instanceof Message) { $name = $message->getHeaders()->getHeaderBody('Subject').'.eml'; } else { $name = 'email.eml'; } parent::__construct('', $name); $this->message = $message; } public function getMediaType(): string { return 'message'; } public function getMediaSubtype(): string { return 'rfc822'; } public function getBody(): string { return $this->message->toString(); } public function bodyToString(): string { return $this->getBody(); } public function bodyToIterable(): iterable { return $this->message->toIterable(); } } AbstractMultipartPart.php000064400000004451150521521620011554 0ustar00 * * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ namespace Symfony\Component\Mime\Part; use Symfony\Component\Mime\Header\Headers; /** * @author Fabien Potencier */ abstract class AbstractMultipartPart extends AbstractPart { private $boundary; private $parts = []; public function __construct(AbstractPart ...$parts) { parent::__construct(); foreach ($parts as $part) { $this->parts[] = $part; } } /** * @return AbstractPart[] */ public function getParts(): array { return $this->parts; } public function getMediaType(): string { return 'multipart'; } public function getPreparedHeaders(): Headers { $headers = parent::getPreparedHeaders(); $headers->setHeaderParameter('Content-Type', 'boundary', $this->getBoundary()); return $headers; } public function bodyToString(): string { $parts = $this->getParts(); $string = ''; foreach ($parts as $part) { $string .= '--'.$this->getBoundary()."\r\n".$part->toString()."\r\n"; } $string .= '--'.$this->getBoundary()."--\r\n"; return $string; } public function bodyToIterable(): iterable { $parts = $this->getParts(); foreach ($parts as $part) { yield '--'.$this->getBoundary()."\r\n"; yield from $part->toIterable(); yield "\r\n"; } yield '--'.$this->getBoundary()."--\r\n"; } public function asDebugString(): string { $str = parent::asDebugString(); foreach ($this->getParts() as $part) { $lines = explode("\n", $part->asDebugString()); $str .= "\n └ ".array_shift($lines); foreach ($lines as $line) { $str .= "\n |".$line; } } return $str; } private function getBoundary(): string { if (null === $this->boundary) { $this->boundary = strtr(base64_encode(random_bytes(6)), '+/', '-_'); } return $this->boundary; } } AbstractPart.php000064400000002753150521521620007655 0ustar00 * * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ namespace Symfony\Component\Mime\Part; use Symfony\Component\Mime\Header\Headers; /** * @author Fabien Potencier */ abstract class AbstractPart { private $headers; public function __construct() { $this->headers = new Headers(); } public function getHeaders(): Headers { return $this->headers; } public function getPreparedHeaders(): Headers { $headers = clone $this->headers; $headers->setHeaderBody('Parameterized', 'Content-Type', $this->getMediaType().'/'.$this->getMediaSubtype()); return $headers; } public function toString(): string { return $this->getPreparedHeaders()->toString()."\r\n".$this->bodyToString(); } public function toIterable(): iterable { yield $this->getPreparedHeaders()->toString(); yield "\r\n"; yield from $this->bodyToIterable(); } public function asDebugString(): string { return $this->getMediaType().'/'.$this->getMediaSubtype(); } abstract public function bodyToString(): string; abstract public function bodyToIterable(): iterable; abstract public function getMediaType(): string; abstract public function getMediaSubtype(): string; } DataPart.php000064400000011444150521521620006760 0ustar00 * * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ namespace Symfony\Component\Mime\Part; use Symfony\Component\Mime\Exception\InvalidArgumentException; use Symfony\Component\Mime\Header\Headers; use Symfony\Component\Mime\MimeTypes; /** * @author Fabien Potencier */ class DataPart extends TextPart { /** @internal */ protected $_parent; private static $mimeTypes; private $filename; private $mediaType; private $cid; private $handle; /** * @param resource|string $body */ public function __construct($body, string $filename = null, string $contentType = null, string $encoding = null) { unset($this->_parent); if (null === $contentType) { $contentType = 'application/octet-stream'; } [$this->mediaType, $subtype] = explode('/', $contentType); parent::__construct($body, null, $subtype, $encoding); if (null !== $filename) { $this->filename = $filename; $this->setName($filename); } $this->setDisposition('attachment'); } public static function fromPath(string $path, string $name = null, string $contentType = null): self { if (null === $contentType) { $ext = strtolower(substr($path, strrpos($path, '.') + 1)); if (null === self::$mimeTypes) { self::$mimeTypes = new MimeTypes(); } $contentType = self::$mimeTypes->getMimeTypes($ext)[0] ?? 'application/octet-stream'; } if (false === is_readable($path)) { throw new InvalidArgumentException(sprintf('Path "%s" is not readable.', $path)); } if (false === $handle = @fopen($path, 'r', false)) { throw new InvalidArgumentException(sprintf('Unable to open path "%s".', $path)); } $p = new self($handle, $name ?: basename($path), $contentType); $p->handle = $handle; return $p; } /** * @return $this */ public function asInline() { return $this->setDisposition('inline'); } public function getContentId(): string { return $this->cid ?: $this->cid = $this->generateContentId(); } public function hasContentId(): bool { return null !== $this->cid; } public function getMediaType(): string { return $this->mediaType; } public function getPreparedHeaders(): Headers { $headers = parent::getPreparedHeaders(); if (null !== $this->cid) { $headers->setHeaderBody('Id', 'Content-ID', $this->cid); } if (null !== $this->filename) { $headers->setHeaderParameter('Content-Disposition', 'filename', $this->filename); } return $headers; } public function asDebugString(): string { $str = parent::asDebugString(); if (null !== $this->filename) { $str .= ' filename: '.$this->filename; } return $str; } private function generateContentId(): string { return bin2hex(random_bytes(16)).'@symfony'; } public function __destruct() { if (null !== $this->handle && \is_resource($this->handle)) { fclose($this->handle); } } /** * @return array */ public function __sleep() { // converts the body to a string parent::__sleep(); $this->_parent = []; foreach (['body', 'charset', 'subtype', 'disposition', 'name', 'encoding'] as $name) { $r = new \ReflectionProperty(TextPart::class, $name); $r->setAccessible(true); $this->_parent[$name] = $r->getValue($this); } $this->_headers = $this->getHeaders(); return ['_headers', '_parent', 'filename', 'mediaType']; } public function __wakeup() { $r = new \ReflectionProperty(AbstractPart::class, 'headers'); $r->setAccessible(true); $r->setValue($this, $this->_headers); unset($this->_headers); if (!\is_array($this->_parent)) { throw new \BadMethodCallException('Cannot unserialize '.__CLASS__); } foreach (['body', 'charset', 'subtype', 'disposition', 'name', 'encoding'] as $name) { if (null !== $this->_parent[$name] && !\is_string($this->_parent[$name])) { throw new \BadMethodCallException('Cannot unserialize '.__CLASS__); } $r = new \ReflectionProperty(TextPart::class, $name); $r->setAccessible(true); $r->setValue($this, $this->_parent[$name]); } unset($this->_parent); } } TextPart.php000064400000013710150521521620007031 0ustar00 * * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ namespace Symfony\Component\Mime\Part; 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; /** * @author Fabien Potencier */ class TextPart extends AbstractPart { /** @internal */ protected $_headers; private static $encoders = []; private $body; private $charset; private $subtype; /** * @var ?string */ private $disposition; private $name; private $encoding; private $seekable; /** * @param resource|string $body */ public function __construct($body, ?string $charset = 'utf-8', string $subtype = 'plain', string $encoding = null) { unset($this->_headers); parent::__construct(); if (!\is_string($body) && !\is_resource($body)) { throw new \TypeError(sprintf('The body of "%s" must be a string or a resource (got "%s").', self::class, get_debug_type($body))); } $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(); } else { 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; } } public function getMediaType(): string { return 'text'; } public function getMediaSubtype(): string { return $this->subtype; } /** * @param string $disposition one of attachment, inline, or form-data * * @return $this */ public function setDisposition(string $disposition) { $this->disposition = $disposition; return $this; } /** * Sets the name of the file (used by FormDataPart). * * @return $this */ public function setName(string $name) { $this->name = $name; return $this; } public function getBody(): string { if (null === $this->seekable) { return $this->body; } if ($this->seekable) { rewind($this->body); } return stream_get_contents($this->body) ?: ''; } public function bodyToString(): string { return $this->getEncoder()->encodeString($this->getBody(), $this->charset); } public function bodyToIterable(): iterable { if (null !== $this->seekable) { if ($this->seekable) { rewind($this->body); } yield from $this->getEncoder()->encodeByteStream($this->body); } else { yield $this->getEncoder()->encodeString($this->body); } } public function getPreparedHeaders(): Headers { $headers = parent::getPreparedHeaders(); $headers->setHeaderBody('Parameterized', 'Content-Type', $this->getMediaType().'/'.$this->getMediaSubtype()); if ($this->charset) { $headers->setHeaderParameter('Content-Type', 'charset', $this->charset); } if ($this->name && 'form-data' !== $this->disposition) { $headers->setHeaderParameter('Content-Type', 'name', $this->name); } $headers->setHeaderBody('Text', 'Content-Transfer-Encoding', $this->encoding); if (!$headers->has('Content-Disposition') && null !== $this->disposition) { $headers->setHeaderBody('Parameterized', 'Content-Disposition', $this->disposition); if ($this->name) { $headers->setHeaderParameter('Content-Disposition', 'name', $this->name); } } return $headers; } public function asDebugString(): string { $str = parent::asDebugString(); if (null !== $this->charset) { $str .= ' charset: '.$this->charset; } if (null !== $this->disposition) { $str .= ' disposition: '.$this->disposition; } return $str; } 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()); } return self::$encoders[$this->encoding] ?? (self::$encoders[$this->encoding] = new Base64ContentEncoder()); } private function chooseEncoding(): string { if (null === $this->charset) { return 'base64'; } return 'quoted-printable'; } /** * @return array */ public function __sleep() { // convert resources to strings for serialization if (null !== $this->seekable) { $this->body = $this->getBody(); } $this->_headers = $this->getHeaders(); return ['_headers', 'body', 'charset', 'subtype', 'disposition', 'name', 'encoding']; } public function __wakeup() { $r = new \ReflectionProperty(AbstractPart::class, 'headers'); $r->setAccessible(true); $r->setValue($this, $this->_headers); unset($this->_headers); } } SMimePart.php000064400000005447150521521620007127 0ustar00 * * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ namespace Symfony\Component\Mime\Part; use Symfony\Component\Mime\Header\Headers; /** * @author Sebastiaan Stok */ class SMimePart extends AbstractPart { /** @internal */ protected $_headers; private $body; private $type; private $subtype; private $parameters; /** * @param iterable|string $body */ public function __construct($body, string $type, string $subtype, array $parameters) { unset($this->_headers); parent::__construct(); if (!\is_string($body) && !is_iterable($body)) { throw new \TypeError(sprintf('The body of "%s" must be a string or a iterable (got "%s").', self::class, get_debug_type($body))); } $this->body = $body; $this->type = $type; $this->subtype = $subtype; $this->parameters = $parameters; } public function getMediaType(): string { return $this->type; } public function getMediaSubtype(): string { return $this->subtype; } public function bodyToString(): string { if (\is_string($this->body)) { return $this->body; } $body = ''; foreach ($this->body as $chunk) { $body .= $chunk; } $this->body = $body; return $body; } public function bodyToIterable(): iterable { if (\is_string($this->body)) { yield $this->body; return; } $body = ''; foreach ($this->body as $chunk) { $body .= $chunk; yield $chunk; } $this->body = $body; } public function getPreparedHeaders(): Headers { $headers = clone parent::getHeaders(); $headers->setHeaderBody('Parameterized', 'Content-Type', $this->getMediaType().'/'.$this->getMediaSubtype()); foreach ($this->parameters as $name => $value) { $headers->setHeaderParameter('Content-Type', $name, $value); } return $headers; } public function __sleep(): array { // convert iterables to strings for serialization if (is_iterable($this->body)) { $this->body = $this->bodyToString(); } $this->_headers = $this->getHeaders(); return ['_headers', 'body', 'type', 'subtype', 'parameters']; } public function __wakeup(): void { $r = new \ReflectionProperty(AbstractPart::class, 'headers'); $r->setAccessible(true); $r->setValue($this, $this->_headers); unset($this->_headers); } }