1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* This file is part of the ZBateson\MailMimeParser project. |
4
|
|
|
* |
5
|
|
|
* @license http://opensource.org/licenses/bsd-license.php BSD |
6
|
|
|
*/ |
7
|
|
|
namespace ZBateson\MailMimeParser\Message\Part; |
8
|
|
|
|
9
|
|
|
use Psr\Http\Message\StreamInterface; |
10
|
|
|
use ZBateson\MailMimeParser\Header\HeaderFactory; |
11
|
|
|
use ZBateson\MailMimeParser\Message\PartFilterFactory; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Represents a single part of a multi-part mime message. |
15
|
|
|
* |
16
|
|
|
* A MimePart object may have any number of child parts, or may be a child |
17
|
|
|
* itself with its own parent or parents. |
18
|
|
|
* |
19
|
|
|
* The content of the part can be read from its PartStream resource handle, |
20
|
|
|
* accessible via MessagePart::getContentResourceHandle. |
21
|
|
|
* |
22
|
|
|
* @author Zaahid Bateson |
23
|
|
|
*/ |
24
|
|
|
class MimePart extends MessagePart |
25
|
|
|
{ |
26
|
|
|
use MimePartHeaderTrait { |
27
|
|
|
MimePartHeaderTrait::__construct as private mimePartHeaderConstructor; |
28
|
|
|
} |
29
|
|
|
use MimePartChildrenTrait { |
30
|
|
|
MimePartChildrenTrait::__construct as private mimePartChildrenConstructor; |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Sets up class dependencies. |
35
|
|
|
* |
36
|
|
|
* @param HeaderFactory $headerFactory |
37
|
|
|
* @param PartFilterFactory $partFilterFactory |
38
|
|
|
* @param PartBuilder $partBuilder |
39
|
|
|
* @param PartStreamFilterManager $partStreamFilterManager |
40
|
|
|
* @param StreamInterface $stream |
41
|
|
|
* @param StreamInterface $contentStream |
42
|
|
|
*/ |
43
|
24 |
|
public function __construct( |
44
|
|
|
HeaderFactory $headerFactory, |
45
|
|
|
PartFilterFactory $partFilterFactory, |
46
|
|
|
PartBuilder $partBuilder, |
47
|
|
|
PartStreamFilterManager $partStreamFilterManager, |
48
|
|
|
StreamInterface $stream, |
49
|
|
|
StreamInterface $contentStream = null |
50
|
|
|
) { |
51
|
24 |
|
parent::__construct($partStreamFilterManager, $stream, $contentStream); |
|
|
|
|
52
|
24 |
|
$this->mimePartChildrenConstructor($partFilterFactory, $partBuilder, $stream); |
53
|
24 |
|
$this->mimePartHeaderConstructor($headerFactory, $partBuilder); |
54
|
24 |
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Returns true if this part's mime type is multipart/* |
58
|
|
|
* |
59
|
|
|
* @return bool |
60
|
|
|
*/ |
61
|
7 |
|
public function isMultiPart() |
62
|
|
|
{ |
63
|
|
|
// casting to bool, preg_match returns 1 for true |
64
|
7 |
|
return (bool) (preg_match( |
65
|
7 |
|
'~multipart/\w+~i', |
66
|
7 |
|
$this->getContentType() |
67
|
|
|
)); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Returns a filename for the part if one is defined, or null otherwise. |
72
|
|
|
* |
73
|
|
|
* @return string |
74
|
|
|
*/ |
75
|
|
|
public function getFilename() |
76
|
|
|
{ |
77
|
|
|
return $this->getHeaderParameter( |
78
|
|
|
'Content-Disposition', |
79
|
|
|
'filename', |
80
|
|
|
$this->getHeaderParameter( |
81
|
|
|
'Content-Type', |
82
|
|
|
'name' |
83
|
|
|
) |
84
|
|
|
); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Returns true. |
89
|
|
|
* |
90
|
|
|
* @return bool |
91
|
|
|
*/ |
92
|
1 |
|
public function isMime() |
93
|
|
|
{ |
94
|
1 |
|
return true; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Returns true if this part's mime type is text/plain, text/html or if the |
99
|
|
|
* Content-Type header defines a charset. |
100
|
|
|
* |
101
|
|
|
* @return bool |
102
|
|
|
*/ |
103
|
7 |
|
public function isTextPart() |
104
|
|
|
{ |
105
|
7 |
|
return ($this->getCharset() !== null); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* Returns the lower-cased, trimmed value of the Content-Type header. |
110
|
|
|
* |
111
|
|
|
* Parses the Content-Type header, defaults to returning text/plain if not |
112
|
|
|
* defined. |
113
|
|
|
* |
114
|
|
|
* @return string |
115
|
|
|
*/ |
116
|
10 |
|
public function getContentType($default = 'text/plain') |
117
|
|
|
{ |
118
|
10 |
|
return trim(strtolower($this->getHeaderValue('Content-Type', $default))); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* Returns the upper-cased charset of the Content-Type header's charset |
123
|
|
|
* parameter if set, US-ASCII if the Content-Type is text/plain or text/html |
124
|
|
|
* and the charset parameter isn't set, or null otherwise. |
125
|
|
|
* |
126
|
|
|
* @return string |
127
|
|
|
*/ |
128
|
12 |
|
public function getCharset() |
129
|
|
|
{ |
130
|
12 |
|
$charset = $this->getHeaderParameter('Content-Type', 'charset'); |
131
|
12 |
|
if ($charset === null) { |
|
|
|
|
132
|
8 |
|
$contentType = $this->getContentType(); |
133
|
8 |
|
if ($contentType === 'text/plain' || $contentType === 'text/html') { |
134
|
3 |
|
return 'US-ASCII'; |
135
|
|
|
} |
136
|
5 |
|
return null; |
137
|
|
|
} |
138
|
4 |
|
return trim(strtoupper($charset)); |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* Returns the content's disposition, defaulting to 'inline' if not set. |
143
|
|
|
* |
144
|
|
|
* @return string |
145
|
|
|
*/ |
146
|
1 |
|
public function getContentDisposition($default = 'inline') |
147
|
|
|
{ |
148
|
1 |
|
return strtolower($this->getHeaderValue('Content-Disposition', $default)); |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* Returns the content-transfer-encoding used for this part, defaulting to |
153
|
|
|
* '7bit' if not set. |
154
|
|
|
* |
155
|
|
|
* @return string |
156
|
|
|
*/ |
157
|
2 |
|
public function getContentTransferEncoding($default = '7bit') |
158
|
|
|
{ |
159
|
2 |
|
static $translated = [ |
160
|
|
|
'x-uue' => 'x-uuencode', |
161
|
|
|
'uue' => 'x-uuencode', |
162
|
|
|
'uuencode' => 'x-uuencode' |
163
|
|
|
]; |
164
|
2 |
|
$type = strtolower($this->getHeaderValue('Content-Transfer-Encoding', $default)); |
165
|
2 |
|
if (isset($translated[$type])) { |
166
|
|
|
return $translated[$type]; |
167
|
|
|
} |
168
|
2 |
|
return $type; |
169
|
|
|
} |
170
|
|
|
} |
171
|
|
|
|