SMimePart.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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\Header\Headers;
  12. /**
  13. * @author Sebastiaan Stok <s.stok@rollerscapes.net>
  14. */
  15. class SMimePart extends AbstractPart
  16. {
  17. /** @internal, to be removed in 8.0 */
  18. protected Headers $_headers;
  19. public function __construct(
  20. private iterable|string $body,
  21. private string $type,
  22. private string $subtype,
  23. private array $parameters,
  24. ) {
  25. parent::__construct();
  26. }
  27. public function getMediaType(): string
  28. {
  29. return $this->type;
  30. }
  31. public function getMediaSubtype(): string
  32. {
  33. return $this->subtype;
  34. }
  35. public function bodyToString(): string
  36. {
  37. if (\is_string($this->body)) {
  38. return $this->body;
  39. }
  40. $body = '';
  41. foreach ($this->body as $chunk) {
  42. $body .= $chunk;
  43. }
  44. $this->body = $body;
  45. return $body;
  46. }
  47. public function bodyToIterable(): iterable
  48. {
  49. if (\is_string($this->body)) {
  50. yield $this->body;
  51. return;
  52. }
  53. $body = '';
  54. foreach ($this->body as $chunk) {
  55. $body .= $chunk;
  56. yield $chunk;
  57. }
  58. $this->body = $body;
  59. }
  60. public function getPreparedHeaders(): Headers
  61. {
  62. $headers = clone parent::getHeaders();
  63. $headers->setHeaderBody('Parameterized', 'Content-Type', $this->getMediaType().'/'.$this->getMediaSubtype());
  64. foreach ($this->parameters as $name => $value) {
  65. $headers->setHeaderParameter('Content-Type', $name, $value);
  66. }
  67. return $headers;
  68. }
  69. public function __serialize(): array
  70. {
  71. if (self::class === (new \ReflectionMethod($this, '__sleep'))->class || self::class !== (new \ReflectionMethod($this, '__serialize'))->class) {
  72. // convert iterables to strings for serialization
  73. if (is_iterable($this->body)) {
  74. $this->body = $this->bodyToString();
  75. }
  76. return [
  77. '_headers' => $this->getHeaders(),
  78. 'body' => $this->body,
  79. 'type' => $this->type,
  80. 'subtype' => $this->subtype,
  81. 'parameters' => $this->parameters,
  82. ];
  83. }
  84. trigger_deprecation('symfony/mime', '7.4', 'Implementing "%s::__sleep()" is deprecated, use "__serialize()" instead.', get_debug_type($this));
  85. $data = [];
  86. foreach ($this->__sleep() as $key) {
  87. try {
  88. if (($r = new \ReflectionProperty($this, $key))->isInitialized($this)) {
  89. $data[$key] = $r->getValue($this);
  90. }
  91. } catch (\ReflectionException) {
  92. $data[$key] = $this->$key;
  93. }
  94. }
  95. return $data;
  96. }
  97. public function __unserialize(array $data): void
  98. {
  99. foreach (['body', 'type', 'subtype'] as $prop) {
  100. if (($data[$prop] ?? $data["\0".self::class."\0".$prop] ?? $data["\0*\0".$prop] ?? null) instanceof \Stringable) {
  101. throw new \BadMethodCallException('Cannot unserialize '.__CLASS__);
  102. }
  103. }
  104. if ($wakeup = self::class !== (new \ReflectionMethod($this, '__wakeup'))->class && self::class === (new \ReflectionMethod($this, '__unserialize'))->class) {
  105. trigger_deprecation('symfony/mime', '7.4', 'Implementing "%s::__wakeup()" is deprecated, use "__unserialize()" instead.', get_debug_type($this));
  106. }
  107. if (['_headers', 'body', 'type', 'subtype', 'parameters'] === array_keys($data)) {
  108. parent::__unserialize(['headers' => $data['_headers']]);
  109. $this->body = $data['body'];
  110. $this->type = $data['type'];
  111. $this->subtype = $data['subtype'];
  112. $this->parameters = $data['parameters'];
  113. if ($wakeup) {
  114. $this->__wakeup();
  115. }
  116. return;
  117. }
  118. $p = "\0".self::class."\0";
  119. if (["\0*\0_headers", $p.'body', $p.'type', $p.'subtype', $p.'parameters'] === array_keys($data)) {
  120. $r = new \ReflectionProperty(parent::class, 'headers');
  121. $r->setValue($this, $data["\0*\0_headers"]);
  122. $this->body = $data[$p.'body'];
  123. $this->type = $data[$p.'type'];
  124. $this->subtype = $data[$p.'subtype'];
  125. $this->parameters = $data[$p.'parameters'];
  126. if ($wakeup) {
  127. $this->_headers = $data["\0*\0_headers"];
  128. $this->__wakeup();
  129. }
  130. return;
  131. }
  132. 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));
  133. \Closure::bind(function ($data) use ($wakeup) {
  134. foreach ($data as $key => $value) {
  135. $this->{("\0" === $key[0] ?? '') ? substr($key, 1 + strrpos($key, "\0")) : $key} = $value;
  136. }
  137. if ($wakeup) {
  138. $this->__wakeup();
  139. }
  140. }, $this, static::class)($data);
  141. }
  142. /**
  143. * @deprecated since Symfony 7.4, will be replaced by `__serialize()` in 8.0
  144. */
  145. public function __sleep(): array
  146. {
  147. trigger_deprecation('symfony/mime', '7.4', 'Calling "%s::__sleep()" is deprecated, use "__serialize()" instead.', get_debug_type($this));
  148. // convert iterables to strings for serialization
  149. if (is_iterable($this->body)) {
  150. $this->body = $this->bodyToString();
  151. }
  152. $this->_headers = $this->getHeaders();
  153. return ['_headers', 'body', 'type', 'subtype', 'parameters'];
  154. }
  155. /**
  156. * @deprecated since Symfony 7.4, will be replaced by `__unserialize()` in 8.0
  157. */
  158. public function __wakeup(): void
  159. {
  160. trigger_deprecation('symfony/mime', '7.4', 'Calling "%s::__wakeup()" is deprecated, use "__unserialize()" instead.', get_debug_type($this));
  161. $r = new \ReflectionProperty(AbstractPart::class, 'headers');
  162. $r->setValue($this, $this->_headers);
  163. unset($this->_headers);
  164. }
  165. }