DataPart.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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\Exception\InvalidArgumentException;
  12. use Symfony\Component\Mime\Header\Headers;
  13. use Symfony\Component\Mime\MimeTypes;
  14. /**
  15. * @author Fabien Potencier <fabien@symfony.com>
  16. */
  17. class DataPart extends TextPart
  18. {
  19. /** @internal */
  20. protected $_parent;
  21. private static $mimeTypes;
  22. private $filename;
  23. private $mediaType;
  24. private $cid;
  25. private $handle;
  26. /**
  27. * @param resource|string $body
  28. */
  29. public function __construct($body, string $filename = null, string $contentType = null, string $encoding = null)
  30. {
  31. unset($this->_parent);
  32. if (null === $contentType) {
  33. $contentType = 'application/octet-stream';
  34. }
  35. [$this->mediaType, $subtype] = explode('/', $contentType);
  36. parent::__construct($body, null, $subtype, $encoding);
  37. if (null !== $filename) {
  38. $this->filename = $filename;
  39. $this->setName($filename);
  40. }
  41. $this->setDisposition('attachment');
  42. }
  43. public static function fromPath(string $path, string $name = null, string $contentType = null): self
  44. {
  45. if (null === $contentType) {
  46. $ext = strtolower(substr($path, strrpos($path, '.') + 1));
  47. if (null === self::$mimeTypes) {
  48. self::$mimeTypes = new MimeTypes();
  49. }
  50. $contentType = self::$mimeTypes->getMimeTypes($ext)[0] ?? 'application/octet-stream';
  51. }
  52. if (false === is_readable($path)) {
  53. throw new InvalidArgumentException(sprintf('Path "%s" is not readable.', $path));
  54. }
  55. if (false === $handle = @fopen($path, 'r', false)) {
  56. throw new InvalidArgumentException(sprintf('Unable to open path "%s".', $path));
  57. }
  58. $p = new self($handle, $name ?: basename($path), $contentType);
  59. $p->handle = $handle;
  60. return $p;
  61. }
  62. /**
  63. * @return $this
  64. */
  65. public function asInline()
  66. {
  67. return $this->setDisposition('inline');
  68. }
  69. public function getContentId(): string
  70. {
  71. return $this->cid ?: $this->cid = $this->generateContentId();
  72. }
  73. public function hasContentId(): bool
  74. {
  75. return null !== $this->cid;
  76. }
  77. public function getMediaType(): string
  78. {
  79. return $this->mediaType;
  80. }
  81. public function getPreparedHeaders(): Headers
  82. {
  83. $headers = parent::getPreparedHeaders();
  84. if (null !== $this->cid) {
  85. $headers->setHeaderBody('Id', 'Content-ID', $this->cid);
  86. }
  87. if (null !== $this->filename) {
  88. $headers->setHeaderParameter('Content-Disposition', 'filename', $this->filename);
  89. }
  90. return $headers;
  91. }
  92. public function asDebugString(): string
  93. {
  94. $str = parent::asDebugString();
  95. if (null !== $this->filename) {
  96. $str .= ' filename: '.$this->filename;
  97. }
  98. return $str;
  99. }
  100. private function generateContentId(): string
  101. {
  102. return bin2hex(random_bytes(16)).'@symfony';
  103. }
  104. public function __destruct()
  105. {
  106. if (null !== $this->handle && \is_resource($this->handle)) {
  107. fclose($this->handle);
  108. }
  109. }
  110. /**
  111. * @return array
  112. */
  113. public function __sleep()
  114. {
  115. // converts the body to a string
  116. parent::__sleep();
  117. $this->_parent = [];
  118. foreach (['body', 'charset', 'subtype', 'disposition', 'name', 'encoding'] as $name) {
  119. $r = new \ReflectionProperty(TextPart::class, $name);
  120. $r->setAccessible(true);
  121. $this->_parent[$name] = $r->getValue($this);
  122. }
  123. $this->_headers = $this->getHeaders();
  124. return ['_headers', '_parent', 'filename', 'mediaType'];
  125. }
  126. public function __wakeup()
  127. {
  128. $r = new \ReflectionProperty(AbstractPart::class, 'headers');
  129. $r->setAccessible(true);
  130. $r->setValue($this, $this->_headers);
  131. unset($this->_headers);
  132. if (!\is_array($this->_parent)) {
  133. throw new \BadMethodCallException('Cannot unserialize '.__CLASS__);
  134. }
  135. foreach (['body', 'charset', 'subtype', 'disposition', 'name', 'encoding'] as $name) {
  136. if (null !== $this->_parent[$name] && !\is_string($this->_parent[$name])) {
  137. throw new \BadMethodCallException('Cannot unserialize '.__CLASS__);
  138. }
  139. $r = new \ReflectionProperty(TextPart::class, $name);
  140. $r->setAccessible(true);
  141. $r->setValue($this, $this->_parent[$name]);
  142. }
  143. unset($this->_parent);
  144. }
  145. }