TextPart.php 8.6 KB

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