forked from ddeboer/imap
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Part.php
355 lines (295 loc) · 8.66 KB
/
Part.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
<?php
namespace Ddeboer\Imap\Message;
use Ddeboer\Imap\Parameters;
use Ddeboer\Transcoder\Transcoder;
/**
* A message part
*/
class Part implements \RecursiveIterator
{
const TYPE_TEXT = 'text';
const TYPE_MULTIPART = 'multipart';
const TYPE_MESSAGE = 'message';
const TYPE_APPLICATION = 'application';
const TYPE_AUDIO = 'audio';
const TYPE_IMAGE = 'image';
const TYPE_VIDEO = 'video';
const TYPE_OTHER = 'other';
const TYPE_UNKNOWN = 'unknown';
const ENCODING_7BIT = '7bit';
const ENCODING_8BIT = '8bit';
const ENCODING_BINARY = 'binary';
const ENCODING_BASE64 = 'base64';
const ENCODING_QUOTED_PRINTABLE = 'quoted-printable';
const ENCODING_UNKNOWN = 'unknown';
const SUBTYPE_TEXT = 'TEXT';
const SUBTYPE_HTML = 'HTML';
protected $typesMap = array(
0 => self::TYPE_TEXT,
1 => self::TYPE_MULTIPART,
2 => self::TYPE_MESSAGE,
3 => self::TYPE_APPLICATION,
4 => self::TYPE_AUDIO,
5 => self::TYPE_IMAGE,
6 => self::TYPE_VIDEO,
7 => self::TYPE_OTHER
);
protected $encodingsMap = array(
0 => self::ENCODING_7BIT,
1 => self::ENCODING_8BIT,
2 => self::ENCODING_BINARY,
3 => self::ENCODING_BASE64,
4 => self::ENCODING_QUOTED_PRINTABLE,
5 => self::ENCODING_UNKNOWN
);
protected $type;
protected $subtype;
protected $encoding;
protected $bytes;
protected $lines;
/**
* @var Parameters
*/
protected $parameters;
protected $stream;
protected $messageNumber;
protected $partNumber;
protected $structure;
protected $content;
protected $decodedContent;
protected $parts = array();
protected $key = 0;
protected $disposition;
/**
* Constructor
*
* @param resource $stream IMAP stream
* @param int $messageNumber Message number
* @param int $partNumber Part number (optional)
* @param \stdClass $structure Part structure
*/
public function __construct(
$stream,
$messageNumber,
$partNumber = null,
\stdClass $structure = null
) {
$this->stream = $stream;
$this->messageNumber = $messageNumber;
$this->partNumber = $partNumber;
$this->structure = $structure;
$this->parseStructure($structure);
}
public function getCharset()
{
return $this->parameters->get('charset');
}
public function getType()
{
return $this->type;
}
public function getSubtype()
{
return $this->subtype;
}
public function getEncoding()
{
return $this->encoding;
}
public function getBytes()
{
return $this->bytes;
}
public function getLines()
{
return $this->lines;
}
public function getParameters()
{
return $this->parameters;
}
/**
* Get raw part content
*
* @return string
*/
public function getContent()
{
if (null === $this->content) {
$this->content = $this->doGetContent();
}
return $this->content;
}
/**
* Get decoded part content
*
* @return string
*/
public function getDecodedContent()
{
if (null === $this->decodedContent) {
switch ($this->getEncoding()) {
case self::ENCODING_BASE64:
$this->decodedContent = base64_decode($this->getContent());
break;
case self::ENCODING_QUOTED_PRINTABLE:
$this->decodedContent = quoted_printable_decode($this->getContent());
break;
case self::ENCODING_7BIT:
case self::ENCODING_8BIT:
case self::ENCODING_BINARY:
$this->decodedContent = $this->getContent();
break;
default:
throw new \UnexpectedValueException('Cannot decode ' . $this->getEncoding());
}
// If this part is a text part, try to convert its encoding to UTF-8.
// We don't want to convert an attachment's encoding.
if ($this->getType() === self::TYPE_TEXT
&& strtolower($this->getCharset()) != 'utf-8'
) {
$this->decodedContent = Transcoder::create()->transcode(
$this->decodedContent,
null,
'UTF-8'
);
}
}
return $this->decodedContent;
}
public function getStructure()
{
return $this->structure;
}
protected function fetchStructure($partNumber = null)
{
if (null === $this->structure) {
$this->loadStructure();
}
if ($partNumber) {
return $this->structure->parts[$partNumber];
}
return $this->structure;
}
protected function parseStructure(\stdClass $structure)
{
if (isset($this->typesMap[$structure->type])) {
$this->type = $this->typesMap[$structure->type];
} else {
$this->type = self::TYPE_UNKNOWN;
}
$this->encoding = $this->encodingsMap[$structure->encoding];
$this->subtype = $structure->subtype;
if (isset($structure->bytes)) {
$this->bytes = $structure->bytes;
}
foreach (array('disposition', 'bytes', 'description') as $optional) {
if (isset($structure->$optional)) {
$this->$optional = $structure->$optional;
}
}
$this->parameters = new Parameters();
if (is_array($structure->parameters)) {
$this->parameters->add($structure->parameters);
}
if (isset($structure->dparameters)) {
$this->parameters->add($structure->dparameters);
}
if (isset($structure->parts)) {
foreach ($structure->parts as $key => $partStructure) {
if (null === $this->partNumber) {
$partNumber = ($key + 1);
} else {
$partNumber = (string) ($this->partNumber . '.' . ($key+1));
}
if ($this->isAttachment($partStructure)) {
$this->parts[] = new Attachment($this->stream, $this->messageNumber, $partNumber, $partStructure);
} else {
$this->parts[] = new Part($this->stream, $this->messageNumber, $partNumber, $partStructure);
}
}
}
}
/**
* Get an array of all parts for this message
*
* @return self[]
*/
public function getParts()
{
return $this->parts;
}
public function current()
{
return $this->parts[$this->key];
}
public function getChildren()
{
return $this->current();
}
public function hasChildren()
{
return count($this->parts) > 0;
}
public function key()
{
return $this->key;
}
public function next()
{
++$this->key;
}
public function rewind()
{
$this->key = 0;
}
public function valid()
{
return isset($this->parts[$this->key]);
}
public function getDisposition()
{
return $this->disposition;
}
/**
* Get raw message content
*
* @param bool $keepUnseen Whether to keep the message unseen.
* Default behaviour is set set the seen flag when
* getting content.
*
* @return string
*/
protected function doGetContent($keepUnseen = false)
{
return imap_fetchbody(
$this->stream,
$this->messageNumber,
$this->partNumber ?: 1,
\FT_UID | ($keepUnseen ? \FT_PEEK : null)
);
}
private function isAttachment($part)
{
// Attachment with correct Content-Disposition header
if (isset($part->disposition)) {
if (('attachment' === strtolower($part->disposition)
|| 'inline' === strtolower($part->disposition))
&& strtoupper($part->subtype) != "PLAIN"
) {
return true;
}
}
// Attachment without Content-Disposition header
if (isset($part->parameters)) {
foreach ($part->parameters as $parameter) {
if ('name' === strtolower($parameter->attribute)
|| 'filename' === strtolower($parameter->attribute)
) {
return true;
}
}
}
return false;
}
}