TextPart.php 7.0 KB

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