| 148 |
lars |
1 |
<?php
|
|
|
2 |
|
|
|
3 |
/*
|
|
|
4 |
* This file is part of the Symfony package.
|
|
|
5 |
*
|
|
|
6 |
* (c) Fabien Potencier <fabien@symfony.com>
|
|
|
7 |
*
|
|
|
8 |
* For the full copyright and license information, please view the LICENSE
|
|
|
9 |
* file that was distributed with this source code.
|
|
|
10 |
*/
|
|
|
11 |
|
|
|
12 |
namespace Symfony\Component\Mime\Part;
|
|
|
13 |
|
|
|
14 |
use Symfony\Component\Mime\Exception\InvalidArgumentException;
|
|
|
15 |
use Symfony\Component\Mime\Header\Headers;
|
|
|
16 |
|
|
|
17 |
/**
|
|
|
18 |
* @author Fabien Potencier <fabien@symfony.com>
|
|
|
19 |
*/
|
|
|
20 |
class DataPart extends TextPart
|
|
|
21 |
{
|
|
|
22 |
/** @internal */
|
|
|
23 |
protected $_parent;
|
|
|
24 |
|
|
|
25 |
private $filename;
|
|
|
26 |
private $mediaType;
|
|
|
27 |
private $cid;
|
|
|
28 |
|
|
|
29 |
/**
|
|
|
30 |
* @param resource|string|File $body Use a File instance to defer loading the file until rendering
|
|
|
31 |
*/
|
|
|
32 |
public function __construct($body, string $filename = null, string $contentType = null, string $encoding = null)
|
|
|
33 |
{
|
|
|
34 |
unset($this->_parent);
|
|
|
35 |
|
|
|
36 |
if ($body instanceof File && !$filename) {
|
|
|
37 |
$filename = $body->getFilename();
|
|
|
38 |
}
|
|
|
39 |
|
|
|
40 |
$contentType ??= $body instanceof File ? $body->getContentType() : 'application/octet-stream';
|
|
|
41 |
[$this->mediaType, $subtype] = explode('/', $contentType);
|
|
|
42 |
|
|
|
43 |
parent::__construct($body, null, $subtype, $encoding);
|
|
|
44 |
|
|
|
45 |
if (null !== $filename) {
|
|
|
46 |
$this->filename = $filename;
|
|
|
47 |
$this->setName($filename);
|
|
|
48 |
}
|
|
|
49 |
$this->setDisposition('attachment');
|
|
|
50 |
}
|
|
|
51 |
|
|
|
52 |
public static function fromPath(string $path, string $name = null, string $contentType = null): self
|
|
|
53 |
{
|
|
|
54 |
return new self(new File($path), $name, $contentType);
|
|
|
55 |
}
|
|
|
56 |
|
|
|
57 |
/**
|
|
|
58 |
* @return $this
|
|
|
59 |
*/
|
|
|
60 |
public function asInline(): static
|
|
|
61 |
{
|
|
|
62 |
return $this->setDisposition('inline');
|
|
|
63 |
}
|
|
|
64 |
|
|
|
65 |
/**
|
|
|
66 |
* @return $this
|
|
|
67 |
*/
|
|
|
68 |
public function setContentId(string $cid): static
|
|
|
69 |
{
|
|
|
70 |
if (!str_contains($cid, '@')) {
|
|
|
71 |
throw new InvalidArgumentException(sprintf('Invalid cid "%s".', $cid));
|
|
|
72 |
}
|
|
|
73 |
|
|
|
74 |
$this->cid = $cid;
|
|
|
75 |
|
|
|
76 |
return $this;
|
|
|
77 |
}
|
|
|
78 |
|
|
|
79 |
public function getContentId(): string
|
|
|
80 |
{
|
|
|
81 |
return $this->cid ?: $this->cid = $this->generateContentId();
|
|
|
82 |
}
|
|
|
83 |
|
|
|
84 |
public function hasContentId(): bool
|
|
|
85 |
{
|
|
|
86 |
return null !== $this->cid;
|
|
|
87 |
}
|
|
|
88 |
|
|
|
89 |
public function getMediaType(): string
|
|
|
90 |
{
|
|
|
91 |
return $this->mediaType;
|
|
|
92 |
}
|
|
|
93 |
|
|
|
94 |
public function getPreparedHeaders(): Headers
|
|
|
95 |
{
|
|
|
96 |
$headers = parent::getPreparedHeaders();
|
|
|
97 |
|
|
|
98 |
if (null !== $this->cid) {
|
|
|
99 |
$headers->setHeaderBody('Id', 'Content-ID', $this->cid);
|
|
|
100 |
}
|
|
|
101 |
|
|
|
102 |
if (null !== $this->filename) {
|
|
|
103 |
$headers->setHeaderParameter('Content-Disposition', 'filename', $this->filename);
|
|
|
104 |
}
|
|
|
105 |
|
|
|
106 |
return $headers;
|
|
|
107 |
}
|
|
|
108 |
|
|
|
109 |
public function asDebugString(): string
|
|
|
110 |
{
|
|
|
111 |
$str = parent::asDebugString();
|
|
|
112 |
if (null !== $this->filename) {
|
|
|
113 |
$str .= ' filename: '.$this->filename;
|
|
|
114 |
}
|
|
|
115 |
|
|
|
116 |
return $str;
|
|
|
117 |
}
|
|
|
118 |
|
|
|
119 |
public function getFilename(): ?string
|
|
|
120 |
{
|
|
|
121 |
return $this->filename;
|
|
|
122 |
}
|
|
|
123 |
|
|
|
124 |
public function getContentType(): string
|
|
|
125 |
{
|
|
|
126 |
return implode('/', [$this->getMediaType(), $this->getMediaSubtype()]);
|
|
|
127 |
}
|
|
|
128 |
|
|
|
129 |
private function generateContentId(): string
|
|
|
130 |
{
|
|
|
131 |
return bin2hex(random_bytes(16)).'@symfony';
|
|
|
132 |
}
|
|
|
133 |
|
|
|
134 |
public function __sleep(): array
|
|
|
135 |
{
|
|
|
136 |
// converts the body to a string
|
|
|
137 |
parent::__sleep();
|
|
|
138 |
|
|
|
139 |
$this->_parent = [];
|
|
|
140 |
foreach (['body', 'charset', 'subtype', 'disposition', 'name', 'encoding'] as $name) {
|
|
|
141 |
$r = new \ReflectionProperty(TextPart::class, $name);
|
|
|
142 |
$this->_parent[$name] = $r->getValue($this);
|
|
|
143 |
}
|
|
|
144 |
$this->_headers = $this->getHeaders();
|
|
|
145 |
|
|
|
146 |
return ['_headers', '_parent', 'filename', 'mediaType'];
|
|
|
147 |
}
|
|
|
148 |
|
|
|
149 |
public function __wakeup()
|
|
|
150 |
{
|
|
|
151 |
$r = new \ReflectionProperty(AbstractPart::class, 'headers');
|
|
|
152 |
$r->setValue($this, $this->_headers);
|
|
|
153 |
unset($this->_headers);
|
|
|
154 |
|
|
|
155 |
if (!\is_array($this->_parent)) {
|
|
|
156 |
throw new \BadMethodCallException('Cannot unserialize '.__CLASS__);
|
|
|
157 |
}
|
|
|
158 |
foreach (['body', 'charset', 'subtype', 'disposition', 'name', 'encoding'] as $name) {
|
|
|
159 |
if (null !== $this->_parent[$name] && !\is_string($this->_parent[$name])) {
|
|
|
160 |
throw new \BadMethodCallException('Cannot unserialize '.__CLASS__);
|
|
|
161 |
}
|
|
|
162 |
$r = new \ReflectionProperty(TextPart::class, $name);
|
|
|
163 |
$r->setValue($this, $this->_parent[$name]);
|
|
|
164 |
}
|
|
|
165 |
unset($this->_parent);
|
|
|
166 |
}
|
|
|
167 |
}
|