1 | <?php |
||
15 | class MessageParser |
||
16 | { |
||
17 | /** |
||
18 | * @var \ZBateson\MailMimeParser\Message the Message object that the read |
||
19 | * mail mime message will be parsed into |
||
20 | */ |
||
21 | protected $message; |
||
22 | |||
23 | /** |
||
24 | * @var \ZBateson\MailMimeParser\MimePartFactory the MimePartFactory object |
||
25 | * used to create parts. |
||
26 | */ |
||
27 | protected $partFactory; |
||
28 | |||
29 | /** |
||
30 | * @var \ZBateson\MailMimeParser\PartStreamRegistry the PartStreamRegistry |
||
31 | * object used to register stream parts. |
||
32 | */ |
||
33 | protected $partStreamRegistry; |
||
34 | |||
35 | /** |
||
36 | * Sets up the parser with its dependencies. |
||
37 | * |
||
38 | * @param \ZBateson\MailMimeParser\Message $m |
||
39 | * @param \ZBateson\MailMimeParser\MimePartFactory $pf |
||
40 | * @param \ZBateson\MailMimeParser\PartStreamRegistry $psr |
||
41 | */ |
||
42 | public function __construct(Message $m, MimePartFactory $pf, PartStreamRegistry $psr) |
||
48 | |||
49 | /** |
||
50 | * Parses the passed stream handle into the ZBateson\MailMimeParser\Message |
||
51 | * object and returns it. |
||
52 | * |
||
53 | * @param resource $fhandle the resource handle to the input stream of the |
||
54 | * mime message |
||
55 | * @return \ZBateson\MailMimeParser\Message |
||
56 | */ |
||
57 | public function parse($fhandle) |
||
63 | |||
64 | /** |
||
65 | * Ensures the header isn't empty, and contains a colon character, then |
||
66 | * splits it and assigns it to $part |
||
67 | * |
||
68 | * @param string $header |
||
69 | * @param \ZBateson\MailMimeParser\MimePart $part |
||
70 | */ |
||
71 | private function addRawHeaderToPart($header, MimePart $part) |
||
78 | |||
79 | /** |
||
80 | * Reads header lines up to an empty line, adding them to the passed $part. |
||
81 | * |
||
82 | * @param resource $handle the resource handle to read from |
||
83 | * @param \ZBateson\MailMimeParser\MimePart $part the current part to add |
||
84 | * headers to |
||
85 | */ |
||
86 | protected function readHeaders($handle, MimePart $part) |
||
100 | |||
101 | /** |
||
102 | * Finds the end of the Mime part at the current read position in $handle |
||
103 | * and sets $boundaryLength to the number of bytes in the part, and |
||
104 | * $endBoundaryFound to true if it's an 'end' boundary, meaning there are no |
||
105 | * further parts for the current mime part (ends with --). |
||
106 | * |
||
107 | * @param resource $handle |
||
108 | * @param string $boundary |
||
109 | * @param int $boundaryLength |
||
110 | * @param boolean $endBoundaryFound |
||
111 | */ |
||
112 | private function findPartBoundaries($handle, $boundary, &$boundaryLength, &$endBoundaryFound) |
||
126 | |||
127 | /** |
||
128 | * Reads the content of a mime part up to a boundary, or the entire message |
||
129 | * if no boundary is specified. |
||
130 | * |
||
131 | * readPartContent may be called to skip to the first boundary to read its |
||
132 | * headers, in which case $skipPart should be true. |
||
133 | * |
||
134 | * If the end boundary is found, the method returns true. |
||
135 | * |
||
136 | * @param resource $handle the input stream resource |
||
137 | * @param \ZBateson\MailMimeParser\Message $message the current Message |
||
138 | * object |
||
139 | * @param \ZBateson\MailMimeParser\MimePart $part the current MimePart |
||
140 | * object to load the content into. |
||
141 | * @param string $boundary the MIME boundary |
||
142 | * @param boolean $skipPart pass true if the intention is to read up to the |
||
143 | * beginning MIME boundary's headers |
||
144 | * @return boolean if the end boundary is found |
||
145 | */ |
||
146 | protected function readPartContent($handle, Message $message, MimePart $part, $boundary, $skipPart) |
||
163 | |||
164 | /** |
||
165 | * Returns the boundary from the parent MimePart, or the current boundary if |
||
166 | * $parent is null |
||
167 | * |
||
168 | * @param string $curBoundary |
||
169 | * @param \ZBateson\MailMimeParser\MimePart $parent |
||
170 | * @return string |
||
171 | */ |
||
172 | private function getParentBoundary($curBoundary, MimePart $parent = null) |
||
178 | |||
179 | /** |
||
180 | * Instantiates and returns a new MimePart setting the part's parent to |
||
181 | * either the passed $parent, or $message if $parent is null. |
||
182 | * |
||
183 | * @param \ZBateson\MailMimeParser\Message $message |
||
184 | * @param \ZBateson\MailMimeParser\MimePart $parent |
||
185 | * @return \ZBateson\MailMimeParser\MimePart |
||
186 | */ |
||
187 | private function newMimePartForMessage(Message $message, MimePart $parent = null) |
||
193 | |||
194 | /** |
||
195 | * Keeps reading if an end boundary is found, to find the parent's boundary |
||
196 | * and the part's content. |
||
197 | * |
||
198 | * @param resource $handle |
||
199 | * @param \ZBateson\MailMimeParser\Message $message |
||
200 | * @param \ZBateson\MailMimeParser\MimePart $parent |
||
201 | * @param \ZBateson\MailMimeParser\MimePart $part |
||
202 | * @param string $boundary |
||
203 | * @param bool $skipFirst |
||
204 | * @return \ZBateson\MailMimeParser\MimePart |
||
205 | */ |
||
206 | private function readMimeMessageBoundaryParts( |
||
223 | |||
224 | /** |
||
225 | * Finds the boundaries for the current MimePart, reads its content and |
||
226 | * creates and returns the next part, setting its parent part accordingly. |
||
227 | * |
||
228 | * @param resource $handle The handle to read from |
||
229 | * @param \ZBateson\MailMimeParser\Message $message The current Message |
||
230 | * @param \ZBateson\MailMimeParser\MimePart $part |
||
231 | * @return MimePart |
||
232 | */ |
||
233 | protected function readMimeMessagePart($handle, Message $message, MimePart $part) |
||
251 | |||
252 | /** |
||
253 | * Extracts the filename and end position of a UUEncoded part. |
||
254 | * |
||
255 | * The filename is set to the passed $nextFilename parameter. The end |
||
256 | * position is returned. |
||
257 | * |
||
258 | * @param resource $handle the current file handle |
||
259 | * @param int &$nextMode is assigned the value of the next file mode or null |
||
260 | * if not found |
||
261 | * @param string &$nextFilename is assigned the value of the next filename |
||
262 | * or null if not found |
||
263 | * @param int &$end assigned the offset position within the passed resource |
||
264 | * $handle of the end of the uuencoded part |
||
265 | */ |
||
266 | private function findNextUUEncodedPartPosition($handle) |
||
280 | |||
281 | /** |
||
282 | * Reads one part of a UUEncoded message and adds it to the passed Message |
||
283 | * as a MimePart. |
||
284 | * |
||
285 | * The method reads up to the first 'begin' part of the message, or to the |
||
286 | * end of the message if no 'begin' exists. |
||
287 | * |
||
288 | * @param resource $handle |
||
289 | * @param \ZBateson\MailMimeParser\Message $message |
||
290 | * @return string |
||
291 | */ |
||
292 | protected function readUUEncodedOrPlainTextPart($handle, Message $message) |
||
310 | |||
311 | /** |
||
312 | * Returns true if the passed Message doesn't define either a Content-Type |
||
313 | * or a Mime-Version header. |
||
314 | * |
||
315 | * @param \ZBateson\MailMimeParser\Message $message |
||
316 | * @return bool |
||
317 | */ |
||
318 | private function isNotMime(Message $message) |
||
324 | |||
325 | /** |
||
326 | * Reads the message from the input stream $handle into $message. |
||
327 | * |
||
328 | * The method will loop to read headers and find and parse multipart-mime |
||
329 | * message parts and uuencoded attachments (as mime-parts), adding them to |
||
330 | * the passed Message object. |
||
331 | * |
||
332 | * @param resource $handle |
||
333 | * @param \ZBateson\MailMimeParser\Message $message |
||
334 | */ |
||
335 | protected function read($handle, Message $message) |
||
349 | } |
||
350 |