DataPart.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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 ((is_file($path) && !is_readable($path)) || is_dir($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. if (!is_file($path)) {
  59. $cache = fopen('php://temp', 'r+');
  60. stream_copy_to_stream($handle, $cache);
  61. $handle = $cache;
  62. }
  63. $p = new self($handle, $name ?: basename($path), $contentType);
  64. $p->handle = $handle;
  65. return $p;
  66. }
  67. /**
  68. * @return $this
  69. */
  70. public function asInline()
  71. {
  72. return $this->setDisposition('inline');
  73. }
  74. public function getContentId(): string
  75. {
  76. return $this->cid ?: $this->cid = $this->generateContentId();
  77. }
  78. public function hasContentId(): bool
  79. {
  80. return null !== $this->cid;
  81. }
  82. public function getMediaType(): string
  83. {
  84. return $this->mediaType;
  85. }
  86. public function getPreparedHeaders(): Headers
  87. {
  88. $headers = parent::getPreparedHeaders();
  89. if (null !== $this->cid) {
  90. $headers->setHeaderBody('Id', 'Content-ID', $this->cid);
  91. }
  92. if (null !== $this->filename) {
  93. $headers->setHeaderParameter('Content-Disposition', 'filename', $this->filename);
  94. }
  95. return $headers;
  96. }
  97. public function asDebugString(): string
  98. {
  99. $str = parent::asDebugString();
  100. if (null !== $this->filename) {
  101. $str .= ' filename: '.$this->filename;
  102. }
  103. return $str;
  104. }
  105. private function generateContentId(): string
  106. {
  107. return bin2hex(random_bytes(16)).'@symfony';
  108. }
  109. public function __destruct()
  110. {
  111. if (null !== $this->handle && \is_resource($this->handle)) {
  112. fclose($this->handle);
  113. }
  114. }
  115. /**
  116. * @return array
  117. */
  118. public function __sleep()
  119. {
  120. // converts the body to a string
  121. parent::__sleep();
  122. $this->_parent = [];
  123. foreach (['body', 'charset', 'subtype', 'disposition', 'name', 'encoding'] as $name) {
  124. $r = new \ReflectionProperty(TextPart::class, $name);
  125. $r->setAccessible(true);
  126. $this->_parent[$name] = $r->getValue($this);
  127. }
  128. $this->_headers = $this->getHeaders();
  129. return ['_headers', '_parent', 'filename', 'mediaType'];
  130. }
  131. public function __wakeup()
  132. {
  133. $r = new \ReflectionProperty(AbstractPart::class, 'headers');
  134. $r->setAccessible(true);
  135. $r->setValue($this, $this->_headers);
  136. unset($this->_headers);
  137. if (!\is_array($this->_parent)) {
  138. throw new \BadMethodCallException('Cannot unserialize '.__CLASS__);
  139. }
  140. foreach (['body', 'charset', 'subtype', 'disposition', 'name', 'encoding'] as $name) {
  141. if (null !== $this->_parent[$name] && !\is_string($this->_parent[$name])) {
  142. throw new \BadMethodCallException('Cannot unserialize '.__CLASS__);
  143. }
  144. $r = new \ReflectionProperty(TextPart::class, $name);
  145. $r->setAccessible(true);
  146. $r->setValue($this, $this->_parent[$name]);
  147. }
  148. unset($this->_parent);
  149. }
  150. }