TextPart.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\Mime\Part;
  11. use Symfony\Component\Mime\Encoder\Base64ContentEncoder;
  12. use Symfony\Component\Mime\Encoder\ContentEncoderInterface;
  13. use Symfony\Component\Mime\Encoder\EightBitContentEncoder;
  14. use Symfony\Component\Mime\Encoder\QpContentEncoder;
  15. use Symfony\Component\Mime\Exception\InvalidArgumentException;
  16. use Symfony\Component\Mime\Header\Headers;
  17. /**
  18. * @author Fabien Potencier <fabien@symfony.com>
  19. */
  20. class TextPart extends AbstractPart
  21. {
  22. /** @internal */
  23. protected $_headers;
  24. private static $encoders = [];
  25. private $body;
  26. private $charset;
  27. private $subtype;
  28. private $disposition;
  29. private $name;
  30. private $encoding;
  31. /**
  32. * @param resource|string $body
  33. */
  34. public function __construct($body, ?string $charset = 'utf-8', string $subtype = 'plain', string $encoding = null)
  35. {
  36. unset($this->_headers);
  37. parent::__construct();
  38. if (!\is_string($body) && !\is_resource($body)) {
  39. throw new \TypeError(sprintf('The body of "%s" must be a string or a resource (got "%s").', self::class, \is_object($body) ? \get_class($body) : \gettype($body)));
  40. }
  41. $this->body = $body;
  42. $this->charset = $charset;
  43. $this->subtype = $subtype;
  44. if (null === $encoding) {
  45. $this->encoding = $this->chooseEncoding();
  46. } else {
  47. if ('quoted-printable' !== $encoding && 'base64' !== $encoding && '8bit' !== $encoding) {
  48. throw new InvalidArgumentException(sprintf('The encoding must be one of "quoted-printable", "base64", or "8bit" ("%s" given).', $encoding));
  49. }
  50. $this->encoding = $encoding;
  51. }
  52. }
  53. public function getMediaType(): string
  54. {
  55. return 'text';
  56. }
  57. public function getMediaSubtype(): string
  58. {
  59. return $this->subtype;
  60. }
  61. /**
  62. * @param string $disposition one of attachment, inline, or form-data
  63. *
  64. * @return $this
  65. */
  66. public function setDisposition(string $disposition)
  67. {
  68. $this->disposition = $disposition;
  69. return $this;
  70. }
  71. /**
  72. * Sets the name of the file (used by FormDataPart).
  73. *
  74. * @return $this
  75. */
  76. public function setName($name)
  77. {
  78. $this->name = $name;
  79. return $this;
  80. }
  81. public function getBody(): string
  82. {
  83. if (!\is_resource($this->body)) {
  84. return $this->body;
  85. }
  86. if (stream_get_meta_data($this->body)['seekable'] ?? false) {
  87. rewind($this->body);
  88. }
  89. return stream_get_contents($this->body) ?: '';
  90. }
  91. public function bodyToString(): string
  92. {
  93. return $this->getEncoder()->encodeString($this->getBody(), $this->charset);
  94. }
  95. public function bodyToIterable(): iterable
  96. {
  97. if (\is_resource($this->body)) {
  98. if (stream_get_meta_data($this->body)['seekable'] ?? false) {
  99. rewind($this->body);
  100. }
  101. yield from $this->getEncoder()->encodeByteStream($this->body);
  102. } else {
  103. yield $this->getEncoder()->encodeString($this->body);
  104. }
  105. }
  106. public function getPreparedHeaders(): Headers
  107. {
  108. $headers = parent::getPreparedHeaders();
  109. $headers->setHeaderBody('Parameterized', 'Content-Type', $this->getMediaType().'/'.$this->getMediaSubtype());
  110. if ($this->charset) {
  111. $headers->setHeaderParameter('Content-Type', 'charset', $this->charset);
  112. }
  113. if ($this->name && 'form-data' !== $this->disposition) {
  114. $headers->setHeaderParameter('Content-Type', 'name', $this->name);
  115. }
  116. $headers->setHeaderBody('Text', 'Content-Transfer-Encoding', $this->encoding);
  117. if (!$headers->has('Content-Disposition') && null !== $this->disposition) {
  118. $headers->setHeaderBody('Parameterized', 'Content-Disposition', $this->disposition);
  119. if ($this->name) {
  120. $headers->setHeaderParameter('Content-Disposition', 'name', $this->name);
  121. }
  122. }
  123. return $headers;
  124. }
  125. public function asDebugString(): string
  126. {
  127. $str = parent::asDebugString();
  128. if (null !== $this->charset) {
  129. $str .= ' charset: '.$this->charset;
  130. }
  131. if (null !== $this->disposition) {
  132. $str .= ' disposition: '.$this->disposition;
  133. }
  134. return $str;
  135. }
  136. private function getEncoder(): ContentEncoderInterface
  137. {
  138. if ('8bit' === $this->encoding) {
  139. return self::$encoders[$this->encoding] ?? (self::$encoders[$this->encoding] = new EightBitContentEncoder());
  140. }
  141. if ('quoted-printable' === $this->encoding) {
  142. return self::$encoders[$this->encoding] ?? (self::$encoders[$this->encoding] = new QpContentEncoder());
  143. }
  144. return self::$encoders[$this->encoding] ?? (self::$encoders[$this->encoding] = new Base64ContentEncoder());
  145. }
  146. private function chooseEncoding(): string
  147. {
  148. if (null === $this->charset) {
  149. return 'base64';
  150. }
  151. return 'quoted-printable';
  152. }
  153. /**
  154. * @return array
  155. */
  156. public function __sleep()
  157. {
  158. // convert resources to strings for serialization
  159. if (\is_resource($this->body)) {
  160. $this->body = $this->getBody();
  161. }
  162. $this->_headers = $this->getHeaders();
  163. return ['_headers', 'body', 'charset', 'subtype', 'disposition', 'name', 'encoding'];
  164. }
  165. public function __wakeup()
  166. {
  167. $r = new \ReflectionProperty(AbstractPart::class, 'headers');
  168. $r->setAccessible(true);
  169. $r->setValue($this, $this->_headers);
  170. unset($this->_headers);
  171. }
  172. }