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; |
8
|
|
|
|
9
|
|
|
use ZBateson\MailMimeParser\Header\HeaderFactory; |
10
|
|
|
use ZBateson\MailMimeParser\Header\ParameterHeader; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Represents a single part of a multi-part mime message. |
14
|
|
|
* |
15
|
|
|
* A MimePart object may have any number of child parts, or may be a child |
16
|
|
|
* itself with its own parent or parents. |
17
|
|
|
* |
18
|
|
|
* The content of the part can be read from its PartStream resource handle, |
19
|
|
|
* accessible via MimePart::getContentResourceHanlde. |
20
|
|
|
* |
21
|
|
|
* @author Zaahid Bateson |
22
|
|
|
*/ |
23
|
|
|
class MimePart |
24
|
|
|
{ |
25
|
|
|
/** |
26
|
|
|
* @var \ZBateson\MailMimeParser\Header\HeaderFactory the HeaderFactory |
27
|
|
|
* object used for created headers |
28
|
|
|
*/ |
29
|
|
|
protected $headerFactory; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var \ZBateson\MailMimeParser\Header\AbstractHeader[] array of header |
33
|
|
|
* objects |
34
|
|
|
*/ |
35
|
|
|
protected $headers; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @var \ZBateson\MailMimeParser\MimePart parent part |
39
|
|
|
*/ |
40
|
|
|
protected $parent; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @var resource the content's resource handle |
44
|
|
|
*/ |
45
|
|
|
protected $handle; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Sets up class dependencies. |
49
|
|
|
* |
50
|
|
|
* @param HeaderFactory $headerFactory |
51
|
|
|
*/ |
52
|
8 |
|
public function __construct(HeaderFactory $headerFactory) |
53
|
|
|
{ |
54
|
8 |
|
$this->headerFactory = $headerFactory; |
55
|
8 |
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Closes the attached resource handle. |
59
|
|
|
*/ |
60
|
8 |
|
public function __destruct() |
61
|
|
|
{ |
62
|
8 |
|
if ($this->handle !== null) { |
63
|
2 |
|
fclose($this->handle); |
64
|
2 |
|
} |
65
|
8 |
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Returns true if there's a content stream associated with the part. |
69
|
|
|
* |
70
|
|
|
* @return boolean |
71
|
|
|
*/ |
72
|
1 |
|
public function hasContent() |
73
|
|
|
{ |
74
|
1 |
|
if (!empty($this->handle)) { |
75
|
1 |
|
return true; |
76
|
|
|
} |
77
|
1 |
|
return false; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Attaches the resource handle for the part's content. The attached handle |
82
|
|
|
* is closed when the MimePart object is destroyed. |
83
|
|
|
* |
84
|
|
|
* @param resource $contentHandle |
85
|
|
|
*/ |
86
|
2 |
|
public function attachContentResourceHandle($contentHandle) |
87
|
|
|
{ |
88
|
2 |
|
$this->handle = $contentHandle; |
89
|
2 |
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Returns the resource stream handle for the part's content. |
93
|
|
|
* |
94
|
|
|
* The resource is automatically closed by MimePart's destructor and should |
95
|
|
|
* not be closed otherwise. |
96
|
|
|
* |
97
|
|
|
* @return resource |
98
|
|
|
*/ |
99
|
1 |
|
public function getContentResourceHandle() |
100
|
|
|
{ |
101
|
1 |
|
return $this->handle; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* Adds a header with the given $name and $value. |
106
|
|
|
* |
107
|
|
|
* Creates a new \ZBateson\MailMimeParser\Header\AbstractHeader object and |
108
|
|
|
* registers it as a header. |
109
|
|
|
* |
110
|
|
|
* @param string $name |
111
|
|
|
* @param string $value |
112
|
|
|
*/ |
113
|
3 |
|
public function setRawHeader($name, $value) |
114
|
|
|
{ |
115
|
3 |
|
$this->headers[strtolower($name)] = $this->headerFactory->newInstance($name, $value); |
116
|
3 |
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* Returns the AbstractHeader object for the header with the given $name |
120
|
|
|
* |
121
|
|
|
* Note that mime headers aren't case sensitive. |
122
|
|
|
* |
123
|
|
|
* @param string $name |
124
|
|
|
* @return \ZBateson\MailMimeParser\Header\Header |
125
|
|
|
*/ |
126
|
5 |
|
public function getHeader($name) |
127
|
|
|
{ |
128
|
5 |
|
if (isset($this->headers[strtolower($name)])) { |
129
|
3 |
|
return $this->headers[strtolower($name)]; |
130
|
|
|
} |
131
|
2 |
|
return null; |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* Returns the string value for the header with the given $name. |
136
|
|
|
* |
137
|
|
|
* Note that mime headers aren't case sensitive. |
138
|
|
|
* |
139
|
|
|
* @param string $name |
140
|
|
|
* @param string $defaultValue |
141
|
|
|
* @return string |
142
|
|
|
*/ |
143
|
2 |
|
public function getHeaderValue($name, $defaultValue = null) |
144
|
|
|
{ |
145
|
2 |
|
$header = $this->getHeader($name); |
146
|
2 |
|
if (!empty($header)) { |
147
|
1 |
|
return $header->getValue(); |
148
|
|
|
} |
149
|
1 |
|
return $defaultValue; |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* Returns the full array of headers for this part. |
154
|
|
|
* |
155
|
|
|
* @return \ZBateson\MailMimeParser\Header\AbstractHeader[] |
156
|
|
|
*/ |
157
|
1 |
|
public function getHeaders() |
158
|
|
|
{ |
159
|
1 |
|
return $this->headers; |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
/** |
163
|
|
|
* Returns a parameter of the header $header, given the parameter named |
164
|
|
|
* $param. |
165
|
|
|
* |
166
|
|
|
* Only headers of type |
167
|
|
|
* \ZBateson\MailMimeParser\Header\ParameterHeader have parameters. |
168
|
|
|
* Content-Type and Content-Disposition are examples of headers with |
169
|
|
|
* parameters. "Charset" is a common parameter of Content-Type. |
170
|
|
|
* |
171
|
|
|
* @param string $header |
172
|
|
|
* @param string $param |
173
|
|
|
* @param string $defaultValue |
174
|
|
|
* @return string |
175
|
|
|
*/ |
176
|
2 |
|
public function getHeaderParameter($header, $param, $defaultValue = null) |
177
|
|
|
{ |
178
|
2 |
|
$obj = $this->getHeader($header); |
179
|
2 |
|
if ($obj && $obj instanceof ParameterHeader) { |
180
|
1 |
|
return $obj->getValueFor($param, $defaultValue); |
181
|
|
|
} |
182
|
1 |
|
return $defaultValue; |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
/** |
186
|
|
|
* Sets the parent part. |
187
|
|
|
* |
188
|
|
|
* @param \ZBateson\MailMimeParser\MimePart $part |
189
|
|
|
*/ |
190
|
1 |
|
public function setParent(MimePart $part) |
191
|
|
|
{ |
192
|
1 |
|
$this->parent = $part; |
193
|
1 |
|
} |
194
|
|
|
|
195
|
|
|
/** |
196
|
|
|
* Returns this part's parent. |
197
|
|
|
* |
198
|
|
|
* @return \ZBateson\MailMimeParser\MimePart |
199
|
|
|
*/ |
200
|
1 |
|
public function getParent() |
201
|
|
|
{ |
202
|
1 |
|
return $this->parent; |
203
|
|
|
} |
204
|
|
|
} |
205
|
|
|
|