TextPart.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  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. /** @internal, to be removed in 8.0 */
  24. protected Headers $_headers;
  25. private static array $encoders = [];
  26. /** @var resource|string|File */
  27. private $body;
  28. private ?string $charset;
  29. private string $subtype;
  30. private ?string $disposition = null;
  31. private ?string $name = null;
  32. private string $encoding;
  33. private ?bool $seekable = null;
  34. /**
  35. * @param resource|string|File $body Use a File instance to defer loading the file until rendering
  36. */
  37. public function __construct($body, ?string $charset = 'utf-8', string $subtype = 'plain', ?string $encoding = null)
  38. {
  39. parent::__construct();
  40. if (!\is_string($body) && !\is_resource($body) && !$body instanceof File) {
  41. 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)));
  42. }
  43. if ($body instanceof File) {
  44. $path = $body->getPath();
  45. if ((is_file($path) && !is_readable($path)) || is_dir($path)) {
  46. throw new InvalidArgumentException(\sprintf('Path "%s" is not readable.', $path));
  47. }
  48. }
  49. $this->body = $body;
  50. $this->charset = $charset;
  51. $this->subtype = $subtype;
  52. $this->seekable = \is_resource($body) ? stream_get_meta_data($body)['seekable'] && 0 === fseek($body, 0, \SEEK_CUR) : null;
  53. if (null === $encoding) {
  54. $this->encoding = $this->chooseEncoding();
  55. } else {
  56. if (!\in_array($encoding, self::DEFAULT_ENCODERS, true) && !\array_key_exists($encoding, self::$encoders)) {
  57. 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));
  58. }
  59. $this->encoding = $encoding;
  60. }
  61. }
  62. public function getMediaType(): string
  63. {
  64. return 'text';
  65. }
  66. public function getMediaSubtype(): string
  67. {
  68. return $this->subtype;
  69. }
  70. /**
  71. * @param string $disposition one of attachment, inline, or form-data
  72. *
  73. * @return $this
  74. */
  75. public function setDisposition(string $disposition): static
  76. {
  77. $this->disposition = $disposition;
  78. return $this;
  79. }
  80. /**
  81. * @return ?string null or one of attachment, inline, or form-data
  82. */
  83. public function getDisposition(): ?string
  84. {
  85. return $this->disposition;
  86. }
  87. /**
  88. * Sets the name of the file (used by FormDataPart).
  89. *
  90. * @return $this
  91. */
  92. public function setName(string $name): static
  93. {
  94. $this->name = $name;
  95. return $this;
  96. }
  97. /**
  98. * Gets the name of the file.
  99. */
  100. public function getName(): ?string
  101. {
  102. return $this->name;
  103. }
  104. public function getBody(): string
  105. {
  106. if ($this->body instanceof File) {
  107. if (false === $ret = @file_get_contents($this->body->getPath())) {
  108. throw new InvalidArgumentException(error_get_last()['message']);
  109. }
  110. return $ret;
  111. }
  112. if (null === $this->seekable) {
  113. return $this->body;
  114. }
  115. if ($this->seekable) {
  116. rewind($this->body);
  117. }
  118. return stream_get_contents($this->body) ?: '';
  119. }
  120. public function bodyToString(): string
  121. {
  122. return $this->getEncoder()->encodeString($this->getBody(), $this->charset);
  123. }
  124. public function bodyToIterable(): iterable
  125. {
  126. if ($this->body instanceof File) {
  127. $path = $this->body->getPath();
  128. if (false === $handle = @fopen($path, 'r', false)) {
  129. throw new InvalidArgumentException(\sprintf('Unable to open path "%s".', $path));
  130. }
  131. yield from $this->getEncoder()->encodeByteStream($handle);
  132. } elseif (null !== $this->seekable) {
  133. if ($this->seekable) {
  134. rewind($this->body);
  135. }
  136. yield from $this->getEncoder()->encodeByteStream($this->body);
  137. } else {
  138. yield $this->getEncoder()->encodeString($this->body);
  139. }
  140. }
  141. public function getPreparedHeaders(): Headers
  142. {
  143. $headers = parent::getPreparedHeaders();
  144. $headers->setHeaderBody('Parameterized', 'Content-Type', $this->getMediaType().'/'.$this->getMediaSubtype());
  145. if ($this->charset) {
  146. $headers->setHeaderParameter('Content-Type', 'charset', $this->charset);
  147. }
  148. if ($this->name && 'form-data' !== $this->disposition) {
  149. $headers->setHeaderParameter('Content-Type', 'name', $this->name);
  150. }
  151. $headers->setHeaderBody('Text', 'Content-Transfer-Encoding', $this->encoding);
  152. if (!$headers->has('Content-Disposition') && null !== $this->disposition) {
  153. $headers->setHeaderBody('Parameterized', 'Content-Disposition', $this->disposition);
  154. if ($this->name) {
  155. $headers->setHeaderParameter('Content-Disposition', 'name', $this->name);
  156. }
  157. }
  158. return $headers;
  159. }
  160. public function asDebugString(): string
  161. {
  162. $str = parent::asDebugString();
  163. if (null !== $this->charset) {
  164. $str .= ' charset: '.$this->charset;
  165. }
  166. if (null !== $this->disposition) {
  167. $str .= ' disposition: '.$this->disposition;
  168. }
  169. return $str;
  170. }
  171. private function getEncoder(): ContentEncoderInterface
  172. {
  173. if ('8bit' === $this->encoding) {
  174. return self::$encoders[$this->encoding] ??= new EightBitContentEncoder();
  175. }
  176. if ('quoted-printable' === $this->encoding) {
  177. return self::$encoders[$this->encoding] ??= new QpContentEncoder();
  178. }
  179. if ('base64' === $this->encoding) {
  180. return self::$encoders[$this->encoding] ??= new Base64ContentEncoder();
  181. }
  182. return self::$encoders[$this->encoding];
  183. }
  184. public static function addEncoder(ContentEncoderInterface $encoder): void
  185. {
  186. if (\in_array($encoder->getName(), self::DEFAULT_ENCODERS, true)) {
  187. throw new InvalidArgumentException('You are not allowed to change the default encoders ("quoted-printable", "base64", and "8bit").');
  188. }
  189. self::$encoders[$encoder->getName()] = $encoder;
  190. }
  191. private function chooseEncoding(): string
  192. {
  193. if (null === $this->charset) {
  194. return 'base64';
  195. }
  196. return 'quoted-printable';
  197. }
  198. public function __serialize(): array
  199. {
  200. if (self::class === (new \ReflectionMethod($this, '__sleep'))->class || self::class !== (new \ReflectionMethod($this, '__serialize'))->class) {
  201. // convert resources to strings for serialization
  202. if (null !== $this->seekable) {
  203. $this->body = $this->getBody();
  204. $this->seekable = null;
  205. }
  206. return [
  207. '_headers' => $this->getHeaders(),
  208. 'body' => $this->body,
  209. 'charset' => $this->charset,
  210. 'subtype' => $this->subtype,
  211. 'disposition' => $this->disposition,
  212. 'name' => $this->name,
  213. 'encoding' => $this->encoding,
  214. ];
  215. }
  216. trigger_deprecation('symfony/mime', '7.4', 'Implementing "%s::__sleep()" is deprecated, use "__serialize()" instead.', get_debug_type($this));
  217. $data = [];
  218. foreach ($this->__sleep() as $key) {
  219. try {
  220. if (($r = new \ReflectionProperty($this, $key))->isInitialized($this)) {
  221. $data[$key] = $r->getValue($this);
  222. }
  223. } catch (\ReflectionException) {
  224. $data[$key] = $this->$key;
  225. }
  226. }
  227. return $data;
  228. }
  229. public function __unserialize(array $data): void
  230. {
  231. if ($wakeup = self::class !== (new \ReflectionMethod($this, '__wakeup'))->class && self::class === (new \ReflectionMethod($this, '__unserialize'))->class) {
  232. trigger_deprecation('symfony/mime', '7.4', 'Implementing "%s::__wakeup()" is deprecated, use "__unserialize()" instead.', get_debug_type($this));
  233. }
  234. if ($headers = $data['_headers'] ?? $data["\0*\0_headers"] ?? null) {
  235. unset($data['_headers'], $data["\0*\0_headers"]);
  236. parent::__unserialize(['headers' => $headers]);
  237. }
  238. if (['body', 'charset', 'subtype', 'disposition', 'name', 'encoding'] === array_keys($data)) {
  239. parent::__unserialize(['headers' => $headers]);
  240. $this->body = $data['body'];
  241. $this->charset = $data['charset'];
  242. $this->subtype = $data['subtype'];
  243. $this->disposition = $data['disposition'];
  244. $this->name = $data['name'];
  245. $this->encoding = $data['encoding'];
  246. if ($wakeup) {
  247. $this->__wakeup();
  248. } elseif (!\is_string($this->body) && !$this->body instanceof File) {
  249. throw new \BadMethodCallException('Cannot unserialize '.__CLASS__);
  250. }
  251. return;
  252. }
  253. if (["\0".self::class."\0body", "\0".self::class."\0charset", "\0".self::class."\0subtype", "\0".self::class."\0disposition", "\0".self::class."\0name", "\0".self::class."\0encoding"] === array_keys($data)) {
  254. $this->body = $data["\0".self::class."\0body"];
  255. $this->charset = $data["\0".self::class."\0charset"];
  256. $this->subtype = $data["\0".self::class."\0subtype"];
  257. $this->disposition = $data["\0".self::class."\0disposition"];
  258. $this->name = $data["\0".self::class."\0name"];
  259. $this->encoding = $data["\0".self::class."\0encoding"];
  260. if ($wakeup) {
  261. $this->_headers = $headers;
  262. $this->__wakeup();
  263. } elseif (!\is_string($this->body) && !$this->body instanceof File) {
  264. throw new \BadMethodCallException('Cannot unserialize '.__CLASS__);
  265. }
  266. return;
  267. }
  268. trigger_deprecation('symfony/mime', '7.4', 'Passing extra keys to "%s::__unserialize()" is deprecated, populate properties in "%s::__unserialize()" instead.', self::class, get_debug_type($this));
  269. \Closure::bind(function ($data) use ($wakeup) {
  270. foreach ($data as $key => $value) {
  271. $this->{("\0" === $key[0] ?? '') ? substr($key, 1 + strrpos($key, "\0")) : $key} = $value;
  272. }
  273. if ($wakeup) {
  274. $this->__wakeup();
  275. }
  276. }, $this, static::class)($data);
  277. }
  278. /**
  279. * @deprecated since Symfony 7.4, will be replaced by `__serialize()` in 8.0
  280. */
  281. public function __sleep(): array
  282. {
  283. trigger_deprecation('symfony/mime', '7.4', 'Calling "%s::__sleep()" is deprecated, use "__serialize()" instead.', get_debug_type($this));
  284. // convert resources to strings for serialization
  285. if (null !== $this->seekable) {
  286. $this->body = $this->getBody();
  287. $this->seekable = null;
  288. }
  289. $this->_headers = $this->getHeaders();
  290. return ['_headers', 'body', 'charset', 'subtype', 'disposition', 'name', 'encoding'];
  291. }
  292. /**
  293. * @deprecated since Symfony 7.4, will be replaced by `__unserialize()` in 8.0
  294. */
  295. public function __wakeup(): void
  296. {
  297. trigger_deprecation('symfony/mime', '7.4', 'Calling "%s::__wakeup()" is deprecated, use "__unserialize()" instead.', get_debug_type($this));
  298. $r = new \ReflectionProperty(AbstractPart::class, 'headers');
  299. $r->setValue($this, $this->_headers);
  300. unset($this->_headers);
  301. }
  302. }