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
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* A parsed mime message with optional mime parts depending on its type. |
13
|
|
|
* |
14
|
|
|
* A mime message may have any number of mime parts, and each part may have any |
15
|
|
|
* number of sub-parts, etc... |
16
|
|
|
* |
17
|
|
|
* A message is a specialized "mime part". Namely the message keeps hold of text |
18
|
|
|
* versus HTML parts (and associated streams for easy access), holds a stream |
19
|
|
|
* for the entire message and all its parts, and maintains parts and their |
20
|
|
|
* relationships. |
21
|
|
|
* |
22
|
|
|
* @author Zaahid Bateson |
23
|
|
|
*/ |
24
|
|
|
class Message extends MimePart |
25
|
|
|
{ |
26
|
|
|
/** |
27
|
|
|
* @var string unique ID used to identify the object to |
28
|
|
|
* $this->partStreamRegistry when registering the stream. The ID is |
29
|
|
|
* used for opening stream parts with the mmp-mime-message "protocol". |
30
|
|
|
* |
31
|
|
|
* @see \ZBateson\MailMimeParser\SimpleDi::registerStreamExtensions |
32
|
|
|
* @see \ZBateson\MailMimeParser\Stream\PartStream::stream_open |
33
|
|
|
*/ |
34
|
|
|
protected $objectId; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @var \ZBateson\MailMimeParser\MimePart The plain text part or null if |
38
|
|
|
* there isn't one |
39
|
|
|
*/ |
40
|
|
|
protected $textPart; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @var \ZBateson\MailMimeParser\MimePart The HTML part stream or null if |
44
|
|
|
* there isn't one |
45
|
|
|
*/ |
46
|
|
|
protected $htmlPart; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @var \ZBateson\MailMimeParser\MimePart[] array of parts in this message |
50
|
|
|
*/ |
51
|
|
|
protected $parts = []; |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Constructs a Message. |
55
|
|
|
* |
56
|
|
|
* @param HeaderFactory $headerFactory |
57
|
|
|
*/ |
58
|
5 |
|
public function __construct(HeaderFactory $headerFactory) |
59
|
|
|
{ |
60
|
5 |
|
parent::__construct($headerFactory); |
61
|
5 |
|
$this->objectId = uniqid(); |
62
|
5 |
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Returns the unique object ID registered with the PartStreamRegistry |
66
|
|
|
* service object. |
67
|
|
|
* |
68
|
|
|
* @return string |
69
|
|
|
*/ |
70
|
1 |
|
public function getObjectId() |
71
|
|
|
{ |
72
|
1 |
|
return $this->objectId; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Returns true if the $part should be assigned as this message's main |
77
|
|
|
* text part content. |
78
|
|
|
* |
79
|
|
|
* @param \ZBateson\MailMimeParser\MimePart $part |
80
|
|
|
* @return bool |
81
|
|
|
*/ |
82
|
3 |
|
private function isMessageTextPart(MimePart $part) |
83
|
|
|
{ |
84
|
3 |
|
$type = strtolower($part->getHeaderValue('Content-Type', 'text/plain')); |
85
|
3 |
|
return ($type === 'text/plain' && empty($this->textPart)); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Returns true if the $part should be assigned as this message's main |
90
|
|
|
* html part content. |
91
|
|
|
* |
92
|
|
|
* @param \ZBateson\MailMimeParser\MimePart $part |
93
|
|
|
* @return bool |
94
|
|
|
*/ |
95
|
2 |
|
private function isMessageHtmlPart(MimePart $part) |
96
|
|
|
{ |
97
|
2 |
|
$type = strtolower($part->getHeaderValue('Content-Type', 'text/plain')); |
98
|
2 |
|
return ($type === 'text/html' && empty($this->htmlPart)); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* Either adds the passed part to $this->textPart if its content type is |
103
|
|
|
* text/plain, to $this->htmlPart if it's text/html, or adds the part to the |
104
|
|
|
* parts array otherwise. |
105
|
|
|
* |
106
|
|
|
* @param \ZBateson\MailMimeParser\MimePart $part |
107
|
|
|
*/ |
108
|
3 |
|
public function addPart(MimePart $part) |
109
|
|
|
{ |
110
|
3 |
|
if ($this->isMessageTextPart($part)) { |
111
|
1 |
|
$this->textPart = $part; |
112
|
3 |
|
} elseif ($this->isMessageHtmlPart($part)) { |
113
|
1 |
|
$this->htmlPart = $part; |
114
|
1 |
|
} else { |
115
|
1 |
|
$this->parts[] = $part; |
116
|
|
|
} |
117
|
3 |
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* Returns the text part (or null if none is set.) |
121
|
|
|
* |
122
|
|
|
* @return \ZBateson\MailMimeParser\MimePart |
123
|
|
|
*/ |
124
|
3 |
|
public function getTextPart() |
125
|
|
|
{ |
126
|
3 |
|
return $this->textPart; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* Returns the HTML part (or null if none is set.) |
131
|
|
|
* |
132
|
|
|
* @return \ZBateson\MailMimeParser\MimePart |
133
|
|
|
*/ |
134
|
3 |
|
public function getHtmlPart() |
135
|
|
|
{ |
136
|
3 |
|
return $this->htmlPart; |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* Returns the non-text, non-HTML part at the given 0-based index, or null |
141
|
|
|
* if none is set. |
142
|
|
|
* |
143
|
|
|
* @param int $index |
144
|
|
|
* @return \ZBateson\MailMimeParser\MimePart |
145
|
|
|
*/ |
146
|
3 |
|
public function getAttachmentPart($index) |
147
|
|
|
{ |
148
|
3 |
|
if (!isset($this->parts[$index])) { |
149
|
2 |
|
return null; |
150
|
|
|
} |
151
|
1 |
|
return $this->parts[$index]; |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* Returns all attachment parts. |
156
|
|
|
* |
157
|
|
|
* @return \ZBateson\MailMimeParser\MimePart[] |
158
|
|
|
*/ |
159
|
1 |
|
public function getAllAttachmentParts() |
160
|
|
|
{ |
161
|
1 |
|
return $this->parts; |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
/** |
165
|
|
|
* Returns the number of attachments available. |
166
|
|
|
* |
167
|
|
|
* @return int |
168
|
|
|
*/ |
169
|
1 |
|
public function getAttachmentCount() |
170
|
|
|
{ |
171
|
1 |
|
return count($this->parts); |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
/** |
175
|
|
|
* Returns a resource handle where the text content can be read. |
176
|
|
|
* |
177
|
|
|
* @return resource |
178
|
|
|
*/ |
179
|
2 |
|
public function getTextStream() |
180
|
|
|
{ |
181
|
2 |
|
if (!empty($this->textPart)) { |
182
|
1 |
|
return $this->textPart->getContentResourceHandle(); |
183
|
|
|
} |
184
|
1 |
|
return null; |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
/** |
188
|
|
|
* Returns the text content as a string. |
189
|
|
|
* |
190
|
|
|
* Reads the entire stream content into a string and returns it. Returns |
191
|
|
|
* null if the message doesn't have a text part. |
192
|
|
|
* |
193
|
|
|
* @return string |
194
|
|
|
*/ |
195
|
|
|
public function getTextContent() |
196
|
|
|
{ |
197
|
|
|
$stream = $this->getTextStream(); |
198
|
|
|
if ($stream === null) { |
199
|
|
|
return null; |
200
|
|
|
} |
201
|
|
|
return stream_get_contents($stream); |
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
/** |
205
|
|
|
* Returns a resource handle where the HTML content can be read. |
206
|
|
|
* |
207
|
|
|
* @return resource |
208
|
|
|
*/ |
209
|
2 |
|
public function getHtmlStream() |
210
|
|
|
{ |
211
|
2 |
|
if (!empty($this->htmlPart)) { |
212
|
1 |
|
return $this->htmlPart->getContentResourceHandle(); |
213
|
|
|
} |
214
|
1 |
|
return null; |
215
|
|
|
} |
216
|
|
|
|
217
|
|
|
/** |
218
|
|
|
* Returns the HTML content as a string. |
219
|
|
|
* |
220
|
|
|
* Reads the entire stream content into a string and returns it. Returns |
221
|
|
|
* null if the message doesn't have an HTML part. |
222
|
|
|
* |
223
|
|
|
* @return string |
224
|
|
|
*/ |
225
|
|
|
public function getHtmlContent() |
226
|
|
|
{ |
227
|
|
|
$stream = $this->getHtmlStream(); |
228
|
|
|
if ($stream === null) { |
229
|
|
|
return null; |
230
|
|
|
} |
231
|
|
|
return stream_get_contents($stream); |
232
|
|
|
} |
233
|
|
|
|
234
|
|
|
/** |
235
|
|
|
* Returns true if either a Content-Type or Mime-Version header are defined |
236
|
|
|
* in this Message. |
237
|
|
|
* |
238
|
|
|
* @return bool |
239
|
|
|
*/ |
240
|
1 |
|
public function isMime() |
241
|
|
|
{ |
242
|
1 |
|
$contentType = $this->getHeaderValue('Content-Type'); |
243
|
1 |
|
$mimeVersion = $this->getHeaderValue('Mime-Version'); |
244
|
1 |
|
return ($contentType !== null || $mimeVersion !== null); |
245
|
|
|
} |
246
|
|
|
} |
247
|
|
|
|