TextPart.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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. private static $encoders = [];
  23. private $body;
  24. private $charset;
  25. private $subtype;
  26. private $disposition;
  27. private $name;
  28. private $encoding;
  29. /**
  30. * @param resource|string $body
  31. */
  32. public function __construct($body, ?string $charset = 'utf-8', string $subtype = 'plain', string $encoding = null)
  33. {
  34. parent::__construct();
  35. if (!\is_string($body) && !\is_resource($body)) {
  36. 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)));
  37. }
  38. $this->body = $body;
  39. $this->charset = $charset;
  40. $this->subtype = $subtype;
  41. if (null === $encoding) {
  42. $this->encoding = $this->chooseEncoding();
  43. } else {
  44. if ('quoted-printable' !== $encoding && 'base64' !== $encoding && '8bit' !== $encoding) {
  45. throw new InvalidArgumentException(sprintf('The encoding must be one of "quoted-printable", "base64", or "8bit" ("%s" given).', $encoding));
  46. }
  47. $this->encoding = $encoding;
  48. }
  49. }
  50. public function getMediaType(): string
  51. {
  52. return 'text';
  53. }
  54. public function getMediaSubtype(): string
  55. {
  56. return $this->subtype;
  57. }
  58. /**
  59. * @param string $disposition one of attachment, inline, or form-data
  60. *
  61. * @return $this
  62. */
  63. public function setDisposition(string $disposition)
  64. {
  65. $this->disposition = $disposition;
  66. return $this;
  67. }
  68. /**
  69. * Sets the name of the file (used by FormDataPart).
  70. *
  71. * @return $this
  72. */
  73. public function setName($name)
  74. {
  75. $this->name = $name;
  76. return $this;
  77. }
  78. public function getBody(): string
  79. {
  80. if (!\is_resource($this->body)) {
  81. return $this->body;
  82. }
  83. if (stream_get_meta_data($this->body)['seekable'] ?? false) {
  84. rewind($this->body);
  85. }
  86. return stream_get_contents($this->body) ?: '';
  87. }
  88. public function bodyToString(): string
  89. {
  90. return $this->getEncoder()->encodeString($this->getBody(), $this->charset);
  91. }
  92. public function bodyToIterable(): iterable
  93. {
  94. if (\is_resource($this->body)) {
  95. if (stream_get_meta_data($this->body)['seekable'] ?? false) {
  96. rewind($this->body);
  97. }
  98. yield from $this->getEncoder()->encodeByteStream($this->body);
  99. } else {
  100. yield $this->getEncoder()->encodeString($this->body);
  101. }
  102. }
  103. public function getPreparedHeaders(): Headers
  104. {
  105. $headers = parent::getPreparedHeaders();
  106. $headers->setHeaderBody('Parameterized', 'Content-Type', $this->getMediaType().'/'.$this->getMediaSubtype());
  107. if ($this->charset) {
  108. $headers->setHeaderParameter('Content-Type', 'charset', $this->charset);
  109. }
  110. if ($this->name && 'form-data' !== $this->disposition) {
  111. $headers->setHeaderParameter('Content-Type', 'name', $this->name);
  112. }
  113. $headers->setHeaderBody('Text', 'Content-Transfer-Encoding', $this->encoding);
  114. if (!$headers->has('Content-Disposition') && null !== $this->disposition) {
  115. $headers->setHeaderBody('Parameterized', 'Content-Disposition', $this->disposition);
  116. if ($this->name) {
  117. $headers->setHeaderParameter('Content-Disposition', 'name', $this->name);
  118. }
  119. }
  120. return $headers;
  121. }
  122. public function asDebugString(): string
  123. {
  124. $str = parent::asDebugString();
  125. if (null !== $this->charset) {
  126. $str .= ' charset: '.$this->charset;
  127. }
  128. if (null !== $this->disposition) {
  129. $str .= ' disposition: '.$this->disposition;
  130. }
  131. return $str;
  132. }
  133. private function getEncoder(): ContentEncoderInterface
  134. {
  135. if ('8bit' === $this->encoding) {
  136. return self::$encoders[$this->encoding] ?? (self::$encoders[$this->encoding] = new EightBitContentEncoder());
  137. }
  138. if ('quoted-printable' === $this->encoding) {
  139. return self::$encoders[$this->encoding] ?? (self::$encoders[$this->encoding] = new QpContentEncoder());
  140. }
  141. return self::$encoders[$this->encoding] ?? (self::$encoders[$this->encoding] = new Base64ContentEncoder());
  142. }
  143. private function chooseEncoding(): string
  144. {
  145. if (null === $this->charset) {
  146. return 'base64';
  147. }
  148. return 'quoted-printable';
  149. }
  150. /**
  151. * @return array
  152. */
  153. public function __sleep()
  154. {
  155. // convert resources to strings for serialization
  156. if (\is_resource($this->body)) {
  157. $this->body = $this->getBody();
  158. }
  159. $this->_headers = $this->getHeaders();
  160. return ['_headers', 'body', 'charset', 'subtype', 'disposition', 'name', 'encoding'];
  161. }
  162. public function __wakeup()
  163. {
  164. $r = new \ReflectionProperty(AbstractPart::class, 'headers');
  165. $r->setAccessible(true);
  166. $r->setValue($this, $this->_headers);
  167. unset($this->_headers);
  168. }
  169. }