TextPart.php 5.8 KB

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