PluginProbe
WPIDE – File Manager & Code Editor / 3.5.9
WPIDE – File Manager & Code Editor v3.5.9
3.5.9 3.5.8 3.5.7 2.0.14 2.0.15 2.0.16 2.0.2 2.0.4 2.0.5 2.0.6 2.0.7 2.0.8 2.0.9 2.1 2.2 2.3 2.3.1 2.3.2 2.4.0 2.5 2.6 3.0 3.1 3.2 3.3 All 55 releases
wpide / vendor / symfony / mime / Part / TextPart.php

TextPart.php in WPIDE – File Manager & Code Editor 3.5.9, at vendor/symfony/mime/Part/TextPart.php

216 lines 6.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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\Encoder\Base64ContentEncoder;
15 use Symfony\Component\Mime\Encoder\ContentEncoderInterface;
16 use Symfony\Component\Mime\Encoder\EightBitContentEncoder;
17 use Symfony\Component\Mime\Encoder\QpContentEncoder;
18 use Symfony\Component\Mime\Exception\InvalidArgumentException;
19 use Symfony\Component\Mime\Header\Headers;
20
21 /**
22 * @author Fabien Potencier <fabien@symfony.com>
23 */
24 class TextPart extends AbstractPart
25 {
26 /** @internal */
27 protected $_headers;
28
29 private static $encoders = [];
30
31 private $body;
32 private $charset;
33 private $subtype;
34 /**
35 * @var ?string
36 */
37 private $disposition;
38 private $name;
39 private $encoding;
40 private $seekable;
41
42 /**
43 * @param resource|string $body
44 */
45 public function __construct($body, ?string $charset = 'utf-8', string $subtype = 'plain', ?string $encoding = null)
46 {
47 unset($this->_headers);
48
49 parent::__construct();
50
51 if (!\is_string($body) && !\is_resource($body)) {
52 throw new \TypeError(sprintf('The body of "%s" must be a string or a resource (got "%s").', self::class, get_debug_type($body)));
53 }
54
55 $this->body = $body;
56 $this->charset = $charset;
57 $this->subtype = $subtype;
58 $this->seekable = \is_resource($body) ? stream_get_meta_data($body)['seekable'] && 0 === fseek($body, 0, \SEEK_CUR) : null;
59
60 if (null === $encoding) {
61 $this->encoding = $this->chooseEncoding();
62 } else {
63 if ('quoted-printable' !== $encoding && 'base64' !== $encoding && '8bit' !== $encoding) {
64 throw new InvalidArgumentException(sprintf('The encoding must be one of "quoted-printable", "base64", or "8bit" ("%s" given).', $encoding));
65 }
66 $this->encoding = $encoding;
67 }
68 }
69
70 public function getMediaType(): string
71 {
72 return 'text';
73 }
74
75 public function getMediaSubtype(): string
76 {
77 return $this->subtype;
78 }
79
80 /**
81 * @param string $disposition one of attachment, inline, or form-data
82 *
83 * @return $this
84 */
85 public function setDisposition(string $disposition)
86 {
87 $this->disposition = $disposition;
88
89 return $this;
90 }
91
92 /**
93 * Sets the name of the file (used by FormDataPart).
94 *
95 * @return $this
96 */
97 public function setName(string $name)
98 {
99 $this->name = $name;
100
101 return $this;
102 }
103
104 public function getBody(): string
105 {
106 if (null === $this->seekable) {
107 return $this->body;
108 }
109
110 if ($this->seekable) {
111 rewind($this->body);
112 }
113
114 return stream_get_contents($this->body) ?: '';
115 }
116
117 public function bodyToString(): string
118 {
119 return $this->getEncoder()->encodeString($this->getBody(), $this->charset);
120 }
121
122 public function bodyToIterable(): iterable
123 {
124 if (null !== $this->seekable) {
125 if ($this->seekable) {
126 rewind($this->body);
127 }
128 yield from $this->getEncoder()->encodeByteStream($this->body);
129 } else {
130 yield $this->getEncoder()->encodeString($this->body);
131 }
132 }
133
134 public function getPreparedHeaders(): Headers
135 {
136 $headers = parent::getPreparedHeaders();
137
138 $headers->setHeaderBody('Parameterized', 'Content-Type', $this->getMediaType().'/'.$this->getMediaSubtype());
139 if ($this->charset) {
140 $headers->setHeaderParameter('Content-Type', 'charset', $this->charset);
141 }
142 if ($this->name && 'form-data' !== $this->disposition) {
143 $headers->setHeaderParameter('Content-Type', 'name', $this->name);
144 }
145 $headers->setHeaderBody('Text', 'Content-Transfer-Encoding', $this->encoding);
146
147 if (!$headers->has('Content-Disposition') && null !== $this->disposition) {
148 $headers->setHeaderBody('Parameterized', 'Content-Disposition', $this->disposition);
149 if ($this->name) {
150 $headers->setHeaderParameter('Content-Disposition', 'name', $this->name);
151 }
152 }
153
154 return $headers;
155 }
156
157 public function asDebugString(): string
158 {
159 $str = parent::asDebugString();
160 if (null !== $this->charset) {
161 $str .= ' charset: '.$this->charset;
162 }
163 if (null !== $this->disposition) {
164 $str .= ' disposition: '.$this->disposition;
165 }
166
167 return $str;
168 }
169
170 private function getEncoder(): ContentEncoderInterface
171 {
172 if ('8bit' === $this->encoding) {
173 return self::$encoders[$this->encoding] ?? (self::$encoders[$this->encoding] = new EightBitContentEncoder());
174 }
175
176 if ('quoted-printable' === $this->encoding) {
177 return self::$encoders[$this->encoding] ?? (self::$encoders[$this->encoding] = new QpContentEncoder());
178 }
179
180 return self::$encoders[$this->encoding] ?? (self::$encoders[$this->encoding] = new Base64ContentEncoder());
181 }
182
183 private function chooseEncoding(): string
184 {
185 if (null === $this->charset) {
186 return 'base64';
187 }
188
189 return 'quoted-printable';
190 }
191
192 /**
193 * @return array
194 */
195 public function __sleep()
196 {
197 // convert resources to strings for serialization
198 if (null !== $this->seekable) {
199 $this->body = $this->getBody();
200 $this->seekable = null;
201 }
202
203 $this->_headers = $this->getHeaders();
204
205 return ['_headers', 'body', 'charset', 'subtype', 'disposition', 'name', 'encoding'];
206 }
207
208 public function __wakeup()
209 {
210 $r = new \ReflectionProperty(AbstractPart::class, 'headers');
211 $r->setAccessible(true);
212 $r->setValue($this, $this->_headers);
213 unset($this->_headers);
214 }
215 }
216