DataPart.php 4.7 KB

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