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 | * Reads header lines up to an empty line, adding them to the passed $part. |
||
66 | * |
||
67 | * @param resource $handle the resource handle to read from |
||
68 | * @param \ZBateson\MailMimeParser\MimePart $part the current part to add |
||
69 | * headers to |
||
70 | */ |
||
71 | protected function readHeaders($handle, MimePart $part) |
||
88 | |||
89 | /** |
||
90 | * Finds the end of the Mime part at the current read position in $handle |
||
91 | * and sets $boundaryLength to the number of bytes in the part, and |
||
92 | * $endBoundaryFound to true if it's an 'end' boundary, meaning there are no |
||
93 | * further parts for the current mime part (ends with --). |
||
94 | * |
||
95 | * @param resource $handle |
||
96 | * @param string $boundary |
||
97 | * @param int $boundaryLength |
||
98 | * @param boolean $endBoundaryFound |
||
99 | */ |
||
100 | private function findPartBoundaries($handle, $boundary, &$boundaryLength, &$endBoundaryFound) |
||
114 | |||
115 | /** |
||
116 | * Reads the content of a mime part up to a boundary, or the entire message |
||
117 | * if no boundary is specified. |
||
118 | * |
||
119 | * readPartContent may be called to skip to the first boundary to read its |
||
120 | * headers, in which case $skipPart should be true. |
||
121 | * |
||
122 | * If the end boundary is found, the method returns true. |
||
123 | * |
||
124 | * @param resource $handle the input stream resource |
||
125 | * @param \ZBateson\MailMimeParser\Message $message the current Message |
||
126 | * object |
||
127 | * @param \ZBateson\MailMimeParser\MimePart $part the current MimePart |
||
128 | * object to load the content into. |
||
129 | * @param string $boundary the MIME boundary |
||
130 | * @param boolean $skipPart pass true if the intention is to read up to the |
||
131 | * beginning MIME boundary's headers |
||
132 | * @return boolean if the end boundary is found |
||
133 | */ |
||
134 | protected function readPartContent($handle, Message $message, MimePart $part, $boundary, $skipPart) |
||
151 | |||
152 | /** |
||
153 | * Keeps reading if an end boundary is found, to find the parent's boundary |
||
154 | * and the part's content. |
||
155 | * |
||
156 | * @param resource $handle |
||
157 | * @param \ZBateson\MailMimeParser\Message $message |
||
158 | * @param \ZBateson\MailMimeParser\MimePart $parent |
||
159 | * @param \ZBateson\MailMimeParser\MimePart $part |
||
160 | * @param string $boundary |
||
161 | * @param bool $skipFirst |
||
162 | * @return \ZBateson\MailMimeParser\MimePart |
||
163 | */ |
||
164 | private function readMimeMessageBoundaryParts( |
||
187 | |||
188 | /** |
||
189 | * Finds the boundaries for the current MimePart, reads its content and |
||
190 | * creates and returns the next part, setting its parent part accordingly. |
||
191 | * |
||
192 | * @param resource $handle The handle to read from |
||
193 | * @param \ZBateson\MailMimeParser\Message $message The current Message |
||
194 | * @param \ZBateson\MailMimeParser\MimePart $part |
||
195 | * @return MimePart |
||
196 | */ |
||
197 | protected function readMimeMessagePart($handle, Message $message, MimePart $part) |
||
215 | |||
216 | /** |
||
217 | * Extracts the filename and end position of a UUEncoded part. |
||
218 | * |
||
219 | * The filename is set to the passed $nextFilename parameter. The end |
||
220 | * position is returned. |
||
221 | * |
||
222 | * @param resource $handle the current file handle |
||
223 | * @param int &$nextMode is assigned the value of the next file mode or null |
||
224 | * if not found |
||
225 | * @param string &$nextFilename is assigned the value of the next filename |
||
226 | * or null if not found |
||
227 | * @param int &$end assigned the offset position within the passed resource |
||
228 | * $handle of the end of the uuencoded part |
||
229 | */ |
||
230 | private function findNextUUEncodedPartPosition($handle) |
||
244 | |||
245 | /** |
||
246 | * Reads one part of a UUEncoded message and adds it to the passed Message |
||
247 | * as a MimePart. |
||
248 | * |
||
249 | * The method reads up to the first 'begin' part of the message, or to the |
||
250 | * end of the message if no 'begin' exists. |
||
251 | * |
||
252 | * @param resource $handle |
||
253 | * @param \ZBateson\MailMimeParser\Message $message |
||
254 | * @return string |
||
255 | */ |
||
256 | protected function readUUEncodedOrPlainTextPart($handle, Message $message) |
||
274 | |||
275 | /** |
||
276 | * Reads the message from the input stream $handle into $message. |
||
277 | * |
||
278 | * The method will loop to read headers and find and parse multipart-mime |
||
279 | * message parts and uuencoded attachments (as mime-parts), adding them to |
||
280 | * the passed Message object. |
||
281 | * |
||
282 | * @param resource $handle |
||
283 | * @param \ZBateson\MailMimeParser\Message $message |
||
284 | */ |
||
285 | protected function read($handle, Message $message) |
||
300 | } |
||
301 |