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