Dashboard sipadu mbip
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

TextPart.php 5.4KB

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