TextPart.php 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  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. if (false === $ret = @file_get_contents($this->body->getPath())) {
  107. throw new InvalidArgumentException(error_get_last()['message']);
  108. }
  109. return $ret;
  110. }
  111. if (null === $this->seekable) {
  112. return $this->body;
  113. }
  114. if ($this->seekable) {
  115. rewind($this->body);
  116. }
  117. return stream_get_contents($this->body) ?: '';
  118. }
  119. public function bodyToString(): string
  120. {
  121. return $this->getEncoder()->encodeString($this->getBody(), $this->charset);
  122. }
  123. public function bodyToIterable(): iterable
  124. {
  125. if ($this->body instanceof File) {
  126. $path = $this->body->getPath();
  127. if (false === $handle = @fopen($path, 'r', false)) {
  128. throw new InvalidArgumentException(\sprintf('Unable to open path "%s".', $path));
  129. }
  130. yield from $this->getEncoder()->encodeByteStream($handle);
  131. } elseif (null !== $this->seekable) {
  132. if ($this->seekable) {
  133. rewind($this->body);
  134. }
  135. yield from $this->getEncoder()->encodeByteStream($this->body);
  136. } else {
  137. yield $this->getEncoder()->encodeString($this->body);
  138. }
  139. }
  140. public function getPreparedHeaders(): Headers
  141. {
  142. $headers = parent::getPreparedHeaders();
  143. $headers->setHeaderBody('Parameterized', 'Content-Type', $this->getMediaType().'/'.$this->getMediaSubtype());
  144. if ($this->charset) {
  145. $headers->setHeaderParameter('Content-Type', 'charset', $this->charset);
  146. }
  147. if ($this->name && 'form-data' !== $this->disposition) {
  148. $headers->setHeaderParameter('Content-Type', 'name', $this->name);
  149. }
  150. $headers->setHeaderBody('Text', 'Content-Transfer-Encoding', $this->encoding);
  151. if (!$headers->has('Content-Disposition') && null !== $this->disposition) {
  152. $headers->setHeaderBody('Parameterized', 'Content-Disposition', $this->disposition);
  153. if ($this->name) {
  154. $headers->setHeaderParameter('Content-Disposition', 'name', $this->name);
  155. }
  156. }
  157. return $headers;
  158. }
  159. public function asDebugString(): string
  160. {
  161. $str = parent::asDebugString();
  162. if (null !== $this->charset) {
  163. $str .= ' charset: '.$this->charset;
  164. }
  165. if (null !== $this->disposition) {
  166. $str .= ' disposition: '.$this->disposition;
  167. }
  168. return $str;
  169. }
  170. private function getEncoder(): ContentEncoderInterface
  171. {
  172. if ('8bit' === $this->encoding) {
  173. return self::$encoders[$this->encoding] ??= new EightBitContentEncoder();
  174. }
  175. if ('quoted-printable' === $this->encoding) {
  176. return self::$encoders[$this->encoding] ??= new QpContentEncoder();
  177. }
  178. return self::$encoders[$this->encoding] ??= new Base64ContentEncoder();
  179. }
  180. private function chooseEncoding(): string
  181. {
  182. if (null === $this->charset) {
  183. return 'base64';
  184. }
  185. return 'quoted-printable';
  186. }
  187. public function __sleep(): array
  188. {
  189. // convert resources to strings for serialization
  190. if (null !== $this->seekable) {
  191. $this->body = $this->getBody();
  192. $this->seekable = null;
  193. }
  194. $this->_headers = $this->getHeaders();
  195. return ['_headers', 'body', 'charset', 'subtype', 'disposition', 'name', 'encoding'];
  196. }
  197. /**
  198. * @return void
  199. */
  200. public function __wakeup()
  201. {
  202. $r = new \ReflectionProperty(AbstractPart::class, 'headers');
  203. $r->setValue($this, $this->_headers);
  204. unset($this->_headers);
  205. }
  206. }