1 | <?php |
||
17 | class MessageParser |
||
18 | { |
||
19 | /** |
||
20 | * @var \ZBateson\MailMimeParser\Message the Message object that the read |
||
21 | * mail mime message will be parsed into |
||
22 | */ |
||
23 | protected $message; |
||
24 | |||
25 | /** |
||
26 | * @var \ZBateson\MailMimeParser\MimePartFactory the MimePartFactory object |
||
27 | * used to create parts. |
||
28 | */ |
||
29 | protected $partFactory; |
||
30 | |||
31 | /** |
||
32 | * @var \ZBateson\MailMimeParser\Stream\PartStreamRegistry the |
||
33 | * PartStreamRegistry |
||
34 | * object used to register stream parts. |
||
35 | */ |
||
36 | protected $partStreamRegistry; |
||
37 | |||
38 | /** |
||
39 | * Sets up the parser with its dependencies. |
||
40 | * |
||
41 | * @param \ZBateson\MailMimeParser\Message $m |
||
42 | * @param \ZBateson\MailMimeParser\MimePartFactory $pf |
||
43 | * @param \ZBateson\MailMimeParser\Stream\PartStreamRegistry $psr |
||
44 | */ |
||
45 | 5 | public function __construct(Message $m, MimePartFactory $pf, PartStreamRegistry $psr) |
|
51 | |||
52 | /** |
||
53 | * Parses the passed stream handle into the ZBateson\MailMimeParser\Message |
||
54 | * object and returns it. |
||
55 | * |
||
56 | * @param resource $fhandle the resource handle to the input stream of the |
||
57 | * mime message |
||
58 | * @return \ZBateson\MailMimeParser\Message |
||
59 | */ |
||
60 | 5 | public function parse($fhandle) |
|
66 | |||
67 | /** |
||
68 | * Ensures the header isn't empty, and contains a colon character, then |
||
69 | * splits it and assigns it to $part |
||
70 | * |
||
71 | * @param string $header |
||
72 | * @param \ZBateson\MailMimeParser\MimePart $part |
||
73 | */ |
||
74 | 5 | private function addRawHeaderToPart($header, MimePart $part) |
|
81 | |||
82 | /** |
||
83 | * Reads header lines up to an empty line, adding them to the passed $part. |
||
84 | * |
||
85 | * @param resource $handle the resource handle to read from |
||
86 | * @param \ZBateson\MailMimeParser\MimePart $part the current part to add |
||
87 | * headers to |
||
88 | */ |
||
89 | 5 | protected function readHeaders($handle, MimePart $part) |
|
90 | { |
||
91 | 5 | $header = ''; |
|
92 | do { |
||
93 | 5 | $line = fgets($handle, 1000); |
|
94 | 5 | if ($line[0] !== "\t" && $line[0] !== ' ') { |
|
95 | 5 | $this->addRawHeaderToPart($header, $part); |
|
96 | 5 | $header = ''; |
|
97 | 5 | } else { |
|
98 | 1 | $line = ' ' . ltrim($line); |
|
99 | } |
||
100 | 5 | $header .= rtrim($line, "\r\n"); |
|
101 | 5 | } while (!empty($header)); |
|
102 | 5 | } |
|
103 | |||
104 | /** |
||
105 | * Finds the end of the Mime part at the current read position in $handle |
||
106 | * and sets $boundaryLength to the number of bytes in the part, and |
||
107 | * $endBoundaryFound to true if it's an 'end' boundary, meaning there are no |
||
108 | * further parts for the current mime part (ends with --). |
||
109 | * |
||
110 | * @param resource $handle |
||
111 | * @param string $boundary |
||
112 | * @param int $boundaryLength |
||
113 | * @param boolean $endBoundaryFound |
||
114 | */ |
||
115 | 2 | private function findPartBoundaries($handle, $boundary, &$boundaryLength, &$endBoundaryFound) |
|
129 | |||
130 | /** |
||
131 | * Reads the content of a mime part up to a boundary, or the entire message |
||
132 | * if no boundary is specified. |
||
133 | * |
||
134 | * readPartContent may be called to skip to the first boundary to read its |
||
135 | * headers, in which case $skipPart should be true. |
||
136 | * |
||
137 | * If the end boundary is found, the method returns true. |
||
138 | * |
||
139 | * @param resource $handle the input stream resource |
||
140 | * @param \ZBateson\MailMimeParser\Message $message the current Message |
||
141 | * object |
||
142 | * @param \ZBateson\MailMimeParser\MimePart $part the current MimePart |
||
143 | * object to load the content into. |
||
144 | * @param string $boundary the MIME boundary |
||
145 | * @param boolean $skipPart pass true if the intention is to read up to the |
||
146 | * beginning MIME boundary's headers |
||
147 | * @return boolean if the end boundary is found |
||
148 | */ |
||
149 | 3 | protected function readPartContent($handle, Message $message, MimePart $part, $boundary, $skipPart) |
|
166 | |||
167 | /** |
||
168 | * Returns the boundary from the parent MimePart, or the current boundary if |
||
169 | * $parent is null |
||
170 | * |
||
171 | * @param string $curBoundary |
||
172 | * @param \ZBateson\MailMimeParser\MimePart $parent |
||
173 | * @return string |
||
174 | */ |
||
175 | 2 | private function getParentBoundary($curBoundary, MimePart $parent = null) |
|
181 | |||
182 | /** |
||
183 | * Instantiates and returns a new MimePart setting the part's parent to |
||
184 | * either the passed $parent, or $message if $parent is null. |
||
185 | * |
||
186 | * @param \ZBateson\MailMimeParser\Message $message |
||
187 | * @param \ZBateson\MailMimeParser\MimePart $parent |
||
188 | * @return \ZBateson\MailMimeParser\MimePart |
||
189 | */ |
||
190 | 3 | private function newMimePartForMessage(Message $message, MimePart $parent = null) |
|
196 | |||
197 | /** |
||
198 | * Keeps reading if an end boundary is found, to find the parent's boundary |
||
199 | * and the part's content. |
||
200 | * |
||
201 | * @param resource $handle |
||
202 | * @param \ZBateson\MailMimeParser\Message $message |
||
203 | * @param \ZBateson\MailMimeParser\MimePart $parent |
||
204 | * @param \ZBateson\MailMimeParser\MimePart $part |
||
205 | * @param string $boundary |
||
206 | * @param bool $skipFirst |
||
207 | * @return \ZBateson\MailMimeParser\MimePart |
||
208 | */ |
||
209 | 3 | private function readMimeMessageBoundaryParts( |
|
226 | |||
227 | /** |
||
228 | * Finds the boundaries for the current MimePart, reads its content and |
||
229 | * creates and returns the next part, setting its parent part accordingly. |
||
230 | * |
||
231 | * @param resource $handle The handle to read from |
||
232 | * @param \ZBateson\MailMimeParser\Message $message The current Message |
||
233 | * @param \ZBateson\MailMimeParser\MimePart $part |
||
234 | * @return MimePart |
||
235 | */ |
||
236 | 3 | protected function readMimeMessagePart($handle, Message $message, MimePart $part) |
|
254 | |||
255 | /** |
||
256 | * Extracts the filename and end position of a UUEncoded part. |
||
257 | * |
||
258 | * The filename is set to the passed $nextFilename parameter. The end |
||
259 | * position is returned. |
||
260 | * |
||
261 | * @param resource $handle the current file handle |
||
262 | * @param int &$nextMode is assigned the value of the next file mode or null |
||
263 | * if not found |
||
264 | * @param string &$nextFilename is assigned the value of the next filename |
||
265 | * or null if not found |
||
266 | * @param int &$end assigned the offset position within the passed resource |
||
267 | * $handle of the end of the uuencoded part |
||
268 | */ |
||
269 | 2 | private function findNextUUEncodedPartPosition($handle) |
|
283 | |||
284 | /** |
||
285 | * Reads one part of a UUEncoded message and adds it to the passed Message |
||
286 | * as a MimePart. |
||
287 | * |
||
288 | * The method reads up to the first 'begin' part of the message, or to the |
||
289 | * end of the message if no 'begin' exists. |
||
290 | * |
||
291 | * @param resource $handle |
||
292 | * @param \ZBateson\MailMimeParser\Message $message |
||
293 | * @return string |
||
294 | */ |
||
295 | 2 | protected function readUUEncodedOrPlainTextPart($handle, Message $message) |
|
313 | |||
314 | /** |
||
315 | * Reads the message from the input stream $handle into $message. |
||
316 | * |
||
317 | * The method will loop to read headers and find and parse multipart-mime |
||
318 | * message parts and uuencoded attachments (as mime-parts), adding them to |
||
319 | * the passed Message object. |
||
320 | * |
||
321 | * @param resource $handle |
||
322 | * @param \ZBateson\MailMimeParser\Message $message |
||
323 | */ |
||
324 | 5 | protected function read($handle, Message $message) |
|
337 | } |
||
338 |
It is generally a best practice as it is often more readable to use concatenation instead of interpolation for variables inside strings.