TextPart.php 6.0 KB

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