1 | <?php |
---|
2 | /** |
---|
3 | * The Mail_mimeDecode class is used to decode mail/mime messages |
---|
4 | * |
---|
5 | * This class will parse a raw mime email and return |
---|
6 | * the structure. Returned structure is similar to |
---|
7 | * that returned by imap_fetchstructure(). |
---|
8 | * |
---|
9 | * +----------------------------- IMPORTANT ------------------------------+ |
---|
10 | * | Usage of this class compared to native php extensions such as | |
---|
11 | * | mailparse or imap, is slow and may be feature deficient. If available| |
---|
12 | * | you are STRONGLY recommended to use the php extensions. | |
---|
13 | * +----------------------------------------------------------------------+ |
---|
14 | * |
---|
15 | * Compatible with PHP versions 4 and 5 |
---|
16 | * |
---|
17 | * LICENSE: This LICENSE is in the BSD license style. |
---|
18 | * Copyright (c) 2002-2003, Richard Heyes <richard@phpguru.org> |
---|
19 | * Copyright (c) 2003-2006, PEAR <pear-group@php.net> |
---|
20 | * All rights reserved. |
---|
21 | * |
---|
22 | * Redistribution and use in source and binary forms, with or |
---|
23 | * without modification, are permitted provided that the following |
---|
24 | * conditions are met: |
---|
25 | * |
---|
26 | * - Redistributions of source code must retain the above copyright |
---|
27 | * notice, this list of conditions and the following disclaimer. |
---|
28 | * - Redistributions in binary form must reproduce the above copyright |
---|
29 | * notice, this list of conditions and the following disclaimer in the |
---|
30 | * documentation and/or other materials provided with the distribution. |
---|
31 | * - Neither the name of the authors, nor the names of its contributors |
---|
32 | * may be used to endorse or promote products derived from this |
---|
33 | * software without specific prior written permission. |
---|
34 | * |
---|
35 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
---|
36 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
---|
37 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
---|
38 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE |
---|
39 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
---|
40 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
---|
41 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
---|
42 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
---|
43 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
---|
44 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF |
---|
45 | * THE POSSIBILITY OF SUCH DAMAGE. |
---|
46 | * |
---|
47 | * @category Mail |
---|
48 | * @package Mail_Mime |
---|
49 | * @author Richard Heyes <richard@phpguru.org> |
---|
50 | * @author George Schlossnagle <george@omniti.com> |
---|
51 | * @author Cipriano Groenendal <cipri@php.net> |
---|
52 | * @author Sean Coates <sean@php.net> |
---|
53 | * @copyright 2003-2006 PEAR <pear-group@php.net> |
---|
54 | * @license http://www.opensource.org/licenses/bsd-license.php BSD License |
---|
55 | * @version CVS: $Id: mimeDecode.php 305875 2010-12-01 07:17:10Z alan_k $ |
---|
56 | * @link http://pear.php.net/package/Mail_mime |
---|
57 | */ |
---|
58 | |
---|
59 | |
---|
60 | /** |
---|
61 | * require PEAR |
---|
62 | * |
---|
63 | * This package depends on PEAR to raise errors. |
---|
64 | */ |
---|
65 | //require_once 'PEAR.php'; |
---|
66 | |
---|
67 | |
---|
68 | /** |
---|
69 | * The Mail_mimeDecode class is used to decode mail/mime messages |
---|
70 | * |
---|
71 | * This class will parse a raw mime email and return the structure. |
---|
72 | * Returned structure is similar to that returned by imap_fetchstructure(). |
---|
73 | * |
---|
74 | * +----------------------------- IMPORTANT ------------------------------+ |
---|
75 | * | Usage of this class compared to native php extensions such as | |
---|
76 | * | mailparse or imap, is slow and may be feature deficient. If available| |
---|
77 | * | you are STRONGLY recommended to use the php extensions. | |
---|
78 | * +----------------------------------------------------------------------+ |
---|
79 | * |
---|
80 | * @category Mail |
---|
81 | * @package Mail_Mime |
---|
82 | * @author Richard Heyes <richard@phpguru.org> |
---|
83 | * @author George Schlossnagle <george@omniti.com> |
---|
84 | * @author Cipriano Groenendal <cipri@php.net> |
---|
85 | * @author Sean Coates <sean@php.net> |
---|
86 | * @copyright 2003-2006 PEAR <pear-group@php.net> |
---|
87 | * @license http://www.opensource.org/licenses/bsd-license.php BSD License |
---|
88 | * @version Release: @package_version@ |
---|
89 | * @link http://pear.php.net/package/Mail_mime |
---|
90 | */ |
---|
91 | class Mail_mimeDecode |
---|
92 | { |
---|
93 | /** |
---|
94 | * The raw email to decode |
---|
95 | * |
---|
96 | * @var string |
---|
97 | * @access private |
---|
98 | */ |
---|
99 | var $_input; |
---|
100 | |
---|
101 | /** |
---|
102 | * The header part of the input |
---|
103 | * |
---|
104 | * @var string |
---|
105 | * @access private |
---|
106 | */ |
---|
107 | var $_header; |
---|
108 | |
---|
109 | /** |
---|
110 | * The body part of the input |
---|
111 | * |
---|
112 | * @var string |
---|
113 | * @access private |
---|
114 | */ |
---|
115 | var $_body; |
---|
116 | |
---|
117 | /** |
---|
118 | * If an error occurs, this is used to store the message |
---|
119 | * |
---|
120 | * @var string |
---|
121 | * @access private |
---|
122 | */ |
---|
123 | var $_error; |
---|
124 | |
---|
125 | /** |
---|
126 | * Flag to determine whether to include bodies in the |
---|
127 | * returned object. |
---|
128 | * |
---|
129 | * @var boolean |
---|
130 | * @access private |
---|
131 | */ |
---|
132 | var $_include_bodies; |
---|
133 | |
---|
134 | /** |
---|
135 | * Flag to determine whether to decode bodies |
---|
136 | * |
---|
137 | * @var boolean |
---|
138 | * @access private |
---|
139 | */ |
---|
140 | var $_decode_bodies; |
---|
141 | |
---|
142 | /** |
---|
143 | * Flag to determine whether to decode headers |
---|
144 | * |
---|
145 | * @var boolean |
---|
146 | * @access private |
---|
147 | */ |
---|
148 | var $_decode_headers; |
---|
149 | |
---|
150 | /** |
---|
151 | * Flag to determine whether to include attached messages |
---|
152 | * as body in the returned object. Depends on $_include_bodies |
---|
153 | * |
---|
154 | * @var boolean |
---|
155 | * @access private |
---|
156 | */ |
---|
157 | var $_rfc822_bodies; |
---|
158 | |
---|
159 | /** |
---|
160 | * Constructor. |
---|
161 | * |
---|
162 | * Sets up the object, initialise the variables, and splits and |
---|
163 | * stores the header and body of the input. |
---|
164 | * |
---|
165 | * @param string The input to decode |
---|
166 | * @access public |
---|
167 | */ |
---|
168 | function Mail_mimeDecode($input) |
---|
169 | { |
---|
170 | list($header, $body) = $this->_splitBodyHeader($input); |
---|
171 | |
---|
172 | $this->_input = $input; |
---|
173 | $this->_header = $header; |
---|
174 | $this->_body = $body; |
---|
175 | $this->_decode_bodies = false; |
---|
176 | $this->_include_bodies = true; |
---|
177 | $this->_rfc822_bodies = false; |
---|
178 | } |
---|
179 | |
---|
180 | /** |
---|
181 | * Begins the decoding process. If called statically |
---|
182 | * it will create an object and call the decode() method |
---|
183 | * of it. |
---|
184 | * |
---|
185 | * @param array An array of various parameters that determine |
---|
186 | * various things: |
---|
187 | * include_bodies - Whether to include the body in the returned |
---|
188 | * object. |
---|
189 | * decode_bodies - Whether to decode the bodies |
---|
190 | * of the parts. (Transfer encoding) |
---|
191 | * decode_headers - Whether to decode headers |
---|
192 | * input - If called statically, this will be treated |
---|
193 | * as the input |
---|
194 | * @return object Decoded results |
---|
195 | * @access public |
---|
196 | */ |
---|
197 | function decode($params = null) |
---|
198 | { |
---|
199 | // determine if this method has been called statically |
---|
200 | $isStatic = empty($this) || !is_a($this, __CLASS__); |
---|
201 | |
---|
202 | // Have we been called statically? |
---|
203 | // If so, create an object and pass details to that. |
---|
204 | if ($isStatic AND isset($params['input'])) { |
---|
205 | |
---|
206 | $obj = new Mail_mimeDecode($params['input']); |
---|
207 | $structure = $obj->decode($params); |
---|
208 | |
---|
209 | // Called statically but no input |
---|
210 | } elseif ($isStatic) { |
---|
211 | return PEAR::raiseError('Called statically and no input given'); |
---|
212 | |
---|
213 | // Called via an object |
---|
214 | } else { |
---|
215 | $this->_include_bodies = isset($params['include_bodies']) ? |
---|
216 | $params['include_bodies'] : false; |
---|
217 | $this->_decode_bodies = isset($params['decode_bodies']) ? |
---|
218 | $params['decode_bodies'] : false; |
---|
219 | $this->_decode_headers = isset($params['decode_headers']) ? |
---|
220 | $params['decode_headers'] : false; |
---|
221 | $this->_rfc822_bodies = isset($params['rfc_822bodies']) ? |
---|
222 | $params['rfc_822bodies'] : false; |
---|
223 | |
---|
224 | $structure = $this->_decode($this->_header, $this->_body); |
---|
225 | if ($structure === false) { |
---|
226 | $structure = $this->raiseError($this->_error); |
---|
227 | } |
---|
228 | } |
---|
229 | |
---|
230 | return $structure; |
---|
231 | } |
---|
232 | |
---|
233 | /** |
---|
234 | * Performs the decoding. Decodes the body string passed to it |
---|
235 | * If it finds certain content-types it will call itself in a |
---|
236 | * recursive fashion |
---|
237 | * |
---|
238 | * @param string Header section |
---|
239 | * @param string Body section |
---|
240 | * @return object Results of decoding process |
---|
241 | * @access private |
---|
242 | */ |
---|
243 | function _decode($headers, $body, $default_ctype = 'text/plain') |
---|
244 | { |
---|
245 | $return = new stdClass; |
---|
246 | $return->headers = array(); |
---|
247 | $headers = $this->_parseHeaders($headers); |
---|
248 | |
---|
249 | foreach ($headers as $value) { |
---|
250 | $value['value'] = $this->_decode_headers ? $this->_decodeHeader($value['value']) : $value['value']; |
---|
251 | if (isset($return->headers[strtolower($value['name'])]) AND !is_array($return->headers[strtolower($value['name'])])) { |
---|
252 | $return->headers[strtolower($value['name'])] = array($return->headers[strtolower($value['name'])]); |
---|
253 | $return->headers[strtolower($value['name'])][] = $value['value']; |
---|
254 | |
---|
255 | } elseif (isset($return->headers[strtolower($value['name'])])) { |
---|
256 | $return->headers[strtolower($value['name'])][] = $value['value']; |
---|
257 | |
---|
258 | } else { |
---|
259 | $return->headers[strtolower($value['name'])] = $value['value']; |
---|
260 | } |
---|
261 | } |
---|
262 | |
---|
263 | |
---|
264 | foreach ($headers as $key => $value) { |
---|
265 | $headers[$key]['name'] = strtolower($headers[$key]['name']); |
---|
266 | switch ($headers[$key]['name']) { |
---|
267 | |
---|
268 | case 'content-type': |
---|
269 | $content_type = $this->_parseHeaderValue($headers[$key]['value']); |
---|
270 | |
---|
271 | if (preg_match('/([0-9a-z+.-]+)\/([0-9a-z+.-]+)/i', $content_type['value'], $regs)) { |
---|
272 | $return->ctype_primary = $regs[1]; |
---|
273 | $return->ctype_secondary = $regs[2]; |
---|
274 | } |
---|
275 | |
---|
276 | if (isset($content_type['other'])) { |
---|
277 | foreach($content_type['other'] as $p_name => $p_value) { |
---|
278 | $return->ctype_parameters[$p_name] = $p_value; |
---|
279 | } |
---|
280 | } |
---|
281 | break; |
---|
282 | |
---|
283 | case 'content-disposition': |
---|
284 | $content_disposition = $this->_parseHeaderValue($headers[$key]['value']); |
---|
285 | $return->disposition = $content_disposition['value']; |
---|
286 | if (isset($content_disposition['other'])) { |
---|
287 | foreach($content_disposition['other'] as $p_name => $p_value) { |
---|
288 | $return->d_parameters[$p_name] = $p_value; |
---|
289 | } |
---|
290 | } |
---|
291 | break; |
---|
292 | |
---|
293 | case 'content-transfer-encoding': |
---|
294 | $content_transfer_encoding = $this->_parseHeaderValue($headers[$key]['value']); |
---|
295 | break; |
---|
296 | } |
---|
297 | } |
---|
298 | |
---|
299 | if (isset($content_type)) { |
---|
300 | switch (strtolower($content_type['value'])) { |
---|
301 | case 'text/plain': |
---|
302 | $encoding = isset($content_transfer_encoding) ? $content_transfer_encoding['value'] : '7bit'; |
---|
303 | $this->_include_bodies ? $return->body = ($this->_decode_bodies ? $this->_decodeBody($body, $encoding) : $body) : null; |
---|
304 | break; |
---|
305 | |
---|
306 | case 'text/html': |
---|
307 | $encoding = isset($content_transfer_encoding) ? $content_transfer_encoding['value'] : '7bit'; |
---|
308 | $this->_include_bodies ? $return->body = ($this->_decode_bodies ? $this->_decodeBody($body, $encoding) : $body) : null; |
---|
309 | break; |
---|
310 | |
---|
311 | case 'multipart/parallel': |
---|
312 | case 'multipart/appledouble': // Appledouble mail |
---|
313 | case 'multipart/report': // RFC1892 |
---|
314 | case 'multipart/signed': // PGP |
---|
315 | case 'multipart/digest': |
---|
316 | case 'multipart/alternative': |
---|
317 | case 'multipart/related': |
---|
318 | case 'multipart/mixed': |
---|
319 | case 'application/vnd.wap.multipart.related': |
---|
320 | if(!isset($content_type['other']['boundary'])){ |
---|
321 | $this->_error = 'No boundary found for ' . $content_type['value'] . ' part'; |
---|
322 | return false; |
---|
323 | } |
---|
324 | |
---|
325 | $default_ctype = (strtolower($content_type['value']) === 'multipart/digest') ? 'message/rfc822' : 'text/plain'; |
---|
326 | |
---|
327 | $parts = $this->_boundarySplit($body, $content_type['other']['boundary']); |
---|
328 | for ($i = 0; $i < count($parts); $i++) { |
---|
329 | list($part_header, $part_body) = $this->_splitBodyHeader($parts[$i]); |
---|
330 | $part = $this->_decode($part_header, $part_body, $default_ctype); |
---|
331 | if($part === false) |
---|
332 | $part = $this->raiseError($this->_error); |
---|
333 | $return->parts[] = $part; |
---|
334 | } |
---|
335 | break; |
---|
336 | |
---|
337 | case 'message/rfc822': |
---|
338 | if ($this->_rfc822_bodies) { |
---|
339 | $encoding = isset($content_transfer_encoding) ? $content_transfer_encoding['value'] : '7bit'; |
---|
340 | $return->body = ($this->_decode_bodies ? $this->_decodeBody($body, $encoding) : $body); |
---|
341 | } |
---|
342 | $obj = new Mail_mimeDecode($body); |
---|
343 | $return->parts[] = $obj->decode(array('include_bodies' => $this->_include_bodies, |
---|
344 | 'decode_bodies' => $this->_decode_bodies, |
---|
345 | 'decode_headers' => $this->_decode_headers)); |
---|
346 | unset($obj); |
---|
347 | break; |
---|
348 | |
---|
349 | case 'message/delivery-status': |
---|
350 | if(!isset($content_transfer_encoding['value'])) |
---|
351 | $content_transfer_encoding['value'] = '7bit'; |
---|
352 | $this->_include_bodies ? $return->body = ($this->_decode_bodies ? $this->_decodeBody($body, $content_transfer_encoding['value']) : $body) : null; |
---|
353 | break; |
---|
354 | |
---|
355 | default: |
---|
356 | if(!isset($content_transfer_encoding['value'])) |
---|
357 | $content_transfer_encoding['value'] = '7bit'; |
---|
358 | $this->_include_bodies ? $return->body = ($this->_decode_bodies ? $this->_decodeBody($body, $content_transfer_encoding['value']) : $body) : null; |
---|
359 | break; |
---|
360 | } |
---|
361 | |
---|
362 | } else { |
---|
363 | $ctype = explode('/', $default_ctype); |
---|
364 | $return->ctype_primary = $ctype[0]; |
---|
365 | $return->ctype_secondary = $ctype[1]; |
---|
366 | $this->_include_bodies ? $return->body = ($this->_decode_bodies ? $this->_decodeBody($body) : $body) : null; |
---|
367 | } |
---|
368 | |
---|
369 | return $return; |
---|
370 | } |
---|
371 | |
---|
372 | /** |
---|
373 | * Given the output of the above function, this will return an |
---|
374 | * array of references to the parts, indexed by mime number. |
---|
375 | * |
---|
376 | * @param object $structure The structure to go through |
---|
377 | * @param string $mime_number Internal use only. |
---|
378 | * @return array Mime numbers |
---|
379 | */ |
---|
380 | function &getMimeNumbers(&$structure, $no_refs = false, $mime_number = '', $prepend = '') |
---|
381 | { |
---|
382 | $return = array(); |
---|
383 | if (!empty($structure->parts)) { |
---|
384 | if ($mime_number != '') { |
---|
385 | $structure->mime_id = $prepend . $mime_number; |
---|
386 | $return[$prepend . $mime_number] = &$structure; |
---|
387 | } |
---|
388 | for ($i = 0; $i < count($structure->parts); $i++) { |
---|
389 | |
---|
390 | |
---|
391 | if (!empty($structure->headers['content-type']) AND substr(strtolower($structure->headers['content-type']), 0, 8) == 'message/') { |
---|
392 | $prepend = $prepend . $mime_number . '.'; |
---|
393 | $_mime_number = ''; |
---|
394 | } else { |
---|
395 | $_mime_number = ($mime_number == '' ? $i + 1 : sprintf('%s.%s', $mime_number, $i + 1)); |
---|
396 | } |
---|
397 | |
---|
398 | $arr = &Mail_mimeDecode::getMimeNumbers($structure->parts[$i], $no_refs, $_mime_number, $prepend); |
---|
399 | foreach ($arr as $key => $val) { |
---|
400 | $no_refs ? $return[$key] = '' : $return[$key] = &$arr[$key]; |
---|
401 | } |
---|
402 | } |
---|
403 | } else { |
---|
404 | if ($mime_number == '') { |
---|
405 | $mime_number = '1'; |
---|
406 | } |
---|
407 | $structure->mime_id = $prepend . $mime_number; |
---|
408 | $no_refs ? $return[$prepend . $mime_number] = '' : $return[$prepend . $mime_number] = &$structure; |
---|
409 | } |
---|
410 | |
---|
411 | return $return; |
---|
412 | } |
---|
413 | |
---|
414 | /** |
---|
415 | * Given a string containing a header and body |
---|
416 | * section, this function will split them (at the first |
---|
417 | * blank line) and return them. |
---|
418 | * |
---|
419 | * @param string Input to split apart |
---|
420 | * @return array Contains header and body section |
---|
421 | * @access private |
---|
422 | */ |
---|
423 | function _splitBodyHeader($input) |
---|
424 | { |
---|
425 | if (preg_match("/^(.*?)\r?\n\r?\n(.*)/s", $input, $match)) { |
---|
426 | return array($match[1], $match[2]); |
---|
427 | } |
---|
428 | // bug #17325 - empty bodies are allowed. - we just check that at least one line |
---|
429 | // of headers exist.. |
---|
430 | if (count(explode("\n",$input))) { |
---|
431 | return array($input, ''); |
---|
432 | } |
---|
433 | $this->_error = 'Could not split header and body'; |
---|
434 | return false; |
---|
435 | } |
---|
436 | |
---|
437 | /** |
---|
438 | * Parse headers given in $input and return |
---|
439 | * as assoc array. |
---|
440 | * |
---|
441 | * @param string Headers to parse |
---|
442 | * @return array Contains parsed headers |
---|
443 | * @access private |
---|
444 | */ |
---|
445 | function _parseHeaders($input) |
---|
446 | { |
---|
447 | |
---|
448 | if ($input !== '') { |
---|
449 | // Unfold the input |
---|
450 | $input = preg_replace("/\r?\n/", "\r\n", $input); |
---|
451 | //#7065 - wrapping.. with encoded stuff.. - probably not needed, |
---|
452 | // wrapping space should only get removed if the trailing item on previous line is a |
---|
453 | // encoded character |
---|
454 | $input = preg_replace("/=\r\n(\t| )+/", '=', $input); |
---|
455 | $input = preg_replace("/\r\n(\t| )+/", ' ', $input); |
---|
456 | |
---|
457 | $headers = explode("\r\n", trim($input)); |
---|
458 | |
---|
459 | foreach ($headers as $value) { |
---|
460 | $hdr_name = substr($value, 0, $pos = strpos($value, ':')); |
---|
461 | $hdr_value = substr($value, $pos+1); |
---|
462 | if($hdr_value[0] == ' ') |
---|
463 | $hdr_value = substr($hdr_value, 1); |
---|
464 | |
---|
465 | $return[] = array( |
---|
466 | 'name' => $hdr_name, |
---|
467 | 'value' => $hdr_value |
---|
468 | ); |
---|
469 | } |
---|
470 | } else { |
---|
471 | $return = array(); |
---|
472 | } |
---|
473 | |
---|
474 | return $return; |
---|
475 | } |
---|
476 | |
---|
477 | /** |
---|
478 | * Function to parse a header value, |
---|
479 | * extract first part, and any secondary |
---|
480 | * parts (after ;) This function is not as |
---|
481 | * robust as it could be. Eg. header comments |
---|
482 | * in the wrong place will probably break it. |
---|
483 | * |
---|
484 | * @param string Header value to parse |
---|
485 | * @return array Contains parsed result |
---|
486 | * @access private |
---|
487 | */ |
---|
488 | function _parseHeaderValue($input) |
---|
489 | { |
---|
490 | |
---|
491 | if (($pos = strpos($input, ';')) === false) { |
---|
492 | $input = $this->_decode_headers ? $this->_decodeHeader($input) : $input; |
---|
493 | $return['value'] = trim($input); |
---|
494 | return $return; |
---|
495 | } |
---|
496 | |
---|
497 | |
---|
498 | |
---|
499 | $value = substr($input, 0, $pos); |
---|
500 | $value = $this->_decode_headers ? $this->_decodeHeader($value) : $value; |
---|
501 | $return['value'] = trim($value); |
---|
502 | $input = trim(substr($input, $pos+1)); |
---|
503 | |
---|
504 | if (!strlen($input) > 0) { |
---|
505 | return $return; |
---|
506 | } |
---|
507 | // at this point input contains xxxx=".....";zzzz="...." |
---|
508 | // since we are dealing with quoted strings, we need to handle this properly.. |
---|
509 | $i = 0; |
---|
510 | $l = strlen($input); |
---|
511 | $key = ''; |
---|
512 | $val = false; // our string - including quotes.. |
---|
513 | $q = false; // in quote.. |
---|
514 | $lq = ''; // last quote.. |
---|
515 | |
---|
516 | while ($i < $l) { |
---|
517 | |
---|
518 | $c = $input[$i]; |
---|
519 | //var_dump(array('i'=>$i,'c'=>$c,'q'=>$q, 'lq'=>$lq, 'key'=>$key, 'val' =>$val)); |
---|
520 | |
---|
521 | $escaped = false; |
---|
522 | if ($c == '\\') { |
---|
523 | $i++; |
---|
524 | if ($i == $l-1) { // end of string. |
---|
525 | break; |
---|
526 | } |
---|
527 | $escaped = true; |
---|
528 | $c = $input[$i]; |
---|
529 | } |
---|
530 | |
---|
531 | |
---|
532 | // state - in key.. |
---|
533 | if ($val === false) { |
---|
534 | if (!$escaped && $c == '=') { |
---|
535 | $val = ''; |
---|
536 | $key = trim($key); |
---|
537 | $i++; |
---|
538 | continue; |
---|
539 | } |
---|
540 | if (!$escaped && $c == ';') { |
---|
541 | if ($key) { // a key without a value.. |
---|
542 | $key= trim($key); |
---|
543 | $return['other'][$key] = ''; |
---|
544 | $return['other'][strtolower($key)] = ''; |
---|
545 | } |
---|
546 | $key = ''; |
---|
547 | } |
---|
548 | $key .= $c; |
---|
549 | $i++; |
---|
550 | continue; |
---|
551 | } |
---|
552 | |
---|
553 | // state - in value.. (as $val is set..) |
---|
554 | |
---|
555 | if ($q === false) { |
---|
556 | // not in quote yet. |
---|
557 | if ((!strlen($val) || $lq !== false) && $c == ' ' || $c == "\t") { |
---|
558 | $i++; |
---|
559 | continue; // skip leading spaces after '=' or after '"' |
---|
560 | } |
---|
561 | if (!$escaped && ($c == '"' || $c == "'")) { |
---|
562 | // start quoted area.. |
---|
563 | $q = $c; |
---|
564 | // in theory should not happen raw text in value part.. |
---|
565 | // but we will handle it as a merged part of the string.. |
---|
566 | $val = !strlen(trim($val)) ? '' : trim($val); |
---|
567 | $i++; |
---|
568 | continue; |
---|
569 | } |
---|
570 | // got end.... |
---|
571 | if (!$escaped && $c == ';') { |
---|
572 | |
---|
573 | $val = trim($val); |
---|
574 | $added = false; |
---|
575 | if (preg_match('/\*[0-9]+$/', $key)) { |
---|
576 | // this is the extended aaa*0=...;aaa*1=.... code |
---|
577 | // it assumes the pieces arrive in order, and are valid... |
---|
578 | $key = preg_replace('/\*[0-9]+$/', '', $key); |
---|
579 | if (isset($return['other'][$key])) { |
---|
580 | $return['other'][$key] .= $val; |
---|
581 | if (strtolower($key) != $key) { |
---|
582 | $return['other'][strtolower($key)] .= $val; |
---|
583 | } |
---|
584 | $added = true; |
---|
585 | } |
---|
586 | // continue and use standard setters.. |
---|
587 | } |
---|
588 | if (!$added) { |
---|
589 | $return['other'][$key] = $val; |
---|
590 | $return['other'][strtolower($key)] = $val; |
---|
591 | } |
---|
592 | $val = false; |
---|
593 | $key = ''; |
---|
594 | $lq = false; |
---|
595 | $i++; |
---|
596 | continue; |
---|
597 | } |
---|
598 | |
---|
599 | $val .= $c; |
---|
600 | $i++; |
---|
601 | continue; |
---|
602 | } |
---|
603 | |
---|
604 | // state - in quote.. |
---|
605 | if (!$escaped && $c == $q) { // potential exit state.. |
---|
606 | |
---|
607 | // end of quoted string.. |
---|
608 | $lq = $q; |
---|
609 | $q = false; |
---|
610 | $i++; |
---|
611 | continue; |
---|
612 | } |
---|
613 | |
---|
614 | // normal char inside of quoted string.. |
---|
615 | $val.= $c; |
---|
616 | $i++; |
---|
617 | } |
---|
618 | |
---|
619 | // do we have anything left.. |
---|
620 | if (strlen(trim($key)) || $val !== false) { |
---|
621 | |
---|
622 | $val = trim($val); |
---|
623 | $added = false; |
---|
624 | if ($val !== false && preg_match('/\*[0-9]+$/', $key)) { |
---|
625 | // no dupes due to our crazy regexp. |
---|
626 | $key = preg_replace('/\*[0-9]+$/', '', $key); |
---|
627 | if (isset($return['other'][$key])) { |
---|
628 | $return['other'][$key] .= $val; |
---|
629 | if (strtolower($key) != $key) { |
---|
630 | $return['other'][strtolower($key)] .= $val; |
---|
631 | } |
---|
632 | $added = true; |
---|
633 | } |
---|
634 | // continue and use standard setters.. |
---|
635 | } |
---|
636 | if (!$added) { |
---|
637 | $return['other'][$key] = $val; |
---|
638 | $return['other'][strtolower($key)] = $val; |
---|
639 | } |
---|
640 | } |
---|
641 | // decode values. |
---|
642 | foreach($return['other'] as $key =>$val) { |
---|
643 | $return['other'][$key] = $this->_decode_headers ? $this->_decodeHeader($val) : $val; |
---|
644 | } |
---|
645 | //print_r($return); |
---|
646 | return $return; |
---|
647 | } |
---|
648 | |
---|
649 | /** |
---|
650 | * This function splits the input based |
---|
651 | * on the given boundary |
---|
652 | * |
---|
653 | * @param string Input to parse |
---|
654 | * @return array Contains array of resulting mime parts |
---|
655 | * @access private |
---|
656 | */ |
---|
657 | function _boundarySplit($input, $boundary) |
---|
658 | { |
---|
659 | $parts = array(); |
---|
660 | |
---|
661 | $bs_possible = substr($boundary, 2, -2); |
---|
662 | $bs_check = '\"' . $bs_possible . '\"'; |
---|
663 | |
---|
664 | if ($boundary == $bs_check) { |
---|
665 | $boundary = $bs_possible; |
---|
666 | } |
---|
667 | $tmp = preg_split("/--".preg_quote($boundary, '/')."((?=\s)|--)/", $input); |
---|
668 | |
---|
669 | $len = count($tmp) -1; |
---|
670 | for ($i = 1; $i < $len; $i++) { |
---|
671 | if (strlen(trim($tmp[$i]))) { |
---|
672 | $parts[] = $tmp[$i]; |
---|
673 | } |
---|
674 | } |
---|
675 | |
---|
676 | // add the last part on if it does not end with the 'closing indicator' |
---|
677 | if (!empty($tmp[$len]) && strlen(trim($tmp[$len])) && $tmp[$len][0] != '-') { |
---|
678 | $parts[] = $tmp[$len]; |
---|
679 | } |
---|
680 | return $parts; |
---|
681 | } |
---|
682 | |
---|
683 | /** |
---|
684 | * Given a header, this function will decode it |
---|
685 | * according to RFC2047. Probably not *exactly* |
---|
686 | * conformant, but it does pass all the given |
---|
687 | * examples (in RFC2047). |
---|
688 | * |
---|
689 | * @param string Input header value to decode |
---|
690 | * @return string Decoded header value |
---|
691 | * @access private |
---|
692 | */ |
---|
693 | function _decodeHeader($input) |
---|
694 | { |
---|
695 | // Remove white space between encoded-words |
---|
696 | $input = preg_replace('/(=\?[^?]+\?(q|b)\?[^?]*\?=)(\s)+=\?/i', '\1=?', $input); |
---|
697 | |
---|
698 | // For each encoded-word... |
---|
699 | while (preg_match('/(=\?([^?]+)\?(q|b)\?([^?]*)\?=)/i', $input, $matches)) { |
---|
700 | |
---|
701 | $encoded = $matches[1]; |
---|
702 | $charset = $matches[2]; |
---|
703 | $encoding = $matches[3]; |
---|
704 | $text = $matches[4]; |
---|
705 | |
---|
706 | switch (strtolower($encoding)) { |
---|
707 | case 'b': |
---|
708 | $text = base64_decode($text); |
---|
709 | break; |
---|
710 | |
---|
711 | case 'q': |
---|
712 | $text = str_replace('_', ' ', $text); |
---|
713 | preg_match_all('/=([a-f0-9]{2})/i', $text, $matches); |
---|
714 | foreach($matches[1] as $value) |
---|
715 | $text = str_replace('='.$value, chr(hexdec($value)), $text); |
---|
716 | break; |
---|
717 | } |
---|
718 | |
---|
719 | $input = str_replace($encoded, $text, $input); |
---|
720 | } |
---|
721 | |
---|
722 | return $input; |
---|
723 | } |
---|
724 | |
---|
725 | /** |
---|
726 | * Given a body string and an encoding type, |
---|
727 | * this function will decode and return it. |
---|
728 | * |
---|
729 | * @param string Input body to decode |
---|
730 | * @param string Encoding type to use. |
---|
731 | * @return string Decoded body |
---|
732 | * @access private |
---|
733 | */ |
---|
734 | function _decodeBody($input, $encoding = '7bit') |
---|
735 | { |
---|
736 | switch (strtolower($encoding)) { |
---|
737 | case '7bit': |
---|
738 | return $input; |
---|
739 | break; |
---|
740 | |
---|
741 | case 'quoted-printable': |
---|
742 | return $this->_quotedPrintableDecode($input); |
---|
743 | break; |
---|
744 | |
---|
745 | case 'base64': |
---|
746 | return base64_decode($input); |
---|
747 | break; |
---|
748 | |
---|
749 | default: |
---|
750 | return $input; |
---|
751 | } |
---|
752 | } |
---|
753 | |
---|
754 | /** |
---|
755 | * Given a quoted-printable string, this |
---|
756 | * function will decode and return it. |
---|
757 | * |
---|
758 | * @param string Input body to decode |
---|
759 | * @return string Decoded body |
---|
760 | * @access private |
---|
761 | */ |
---|
762 | function _quotedPrintableDecode($input) |
---|
763 | { |
---|
764 | // Remove soft line breaks |
---|
765 | $input = preg_replace("/=\r?\n/", '', $input); |
---|
766 | |
---|
767 | // Replace encoded characters |
---|
768 | $input = preg_replace('/=([a-f0-9]{2})/ie', "chr(hexdec('\\1'))", $input); |
---|
769 | |
---|
770 | return $input; |
---|
771 | } |
---|
772 | |
---|
773 | /** |
---|
774 | * Checks the input for uuencoded files and returns |
---|
775 | * an array of them. Can be called statically, eg: |
---|
776 | * |
---|
777 | * $files =& Mail_mimeDecode::uudecode($some_text); |
---|
778 | * |
---|
779 | * It will check for the begin 666 ... end syntax |
---|
780 | * however and won't just blindly decode whatever you |
---|
781 | * pass it. |
---|
782 | * |
---|
783 | * @param string Input body to look for attahcments in |
---|
784 | * @return array Decoded bodies, filenames and permissions |
---|
785 | * @access public |
---|
786 | * @author Unknown |
---|
787 | */ |
---|
788 | function &uudecode($input) |
---|
789 | { |
---|
790 | |
---|
791 | // Find all uuencoded sections |
---|
792 | preg_match_all("/begin ([0-7]{3}) (.+)\r?\n(.+)\r?\nend/Us", $input, $matches); |
---|
793 | |
---|
794 | for ($j = 0; $j < count($matches[3]); $j++) { |
---|
795 | |
---|
796 | $str = $matches[3][$j]; |
---|
797 | $filename = $matches[2][$j]; |
---|
798 | $fileperm = $matches[1][$j]; |
---|
799 | |
---|
800 | $file = ''; |
---|
801 | $str = preg_split("/\r?\n/", trim($str)); |
---|
802 | $strlen = count($str); |
---|
803 | |
---|
804 | for ($i = 0; $i < $strlen; $i++) { |
---|
805 | $pos = 1; |
---|
806 | $d = 0; |
---|
807 | $len=(int)(((ord(substr($str[$i],0,1)) -32) - ' ') & 077); |
---|
808 | |
---|
809 | while (($d + 3 <= $len) AND ($pos + 4 <= strlen($str[$i]))) { |
---|
810 | $c0 = (ord(substr($str[$i],$pos,1)) ^ 0x20); |
---|
811 | $c1 = (ord(substr($str[$i],$pos+1,1)) ^ 0x20); |
---|
812 | $c2 = (ord(substr($str[$i],$pos+2,1)) ^ 0x20); |
---|
813 | $c3 = (ord(substr($str[$i],$pos+3,1)) ^ 0x20); |
---|
814 | $file .= chr(((($c0 - ' ') & 077) << 2) | ((($c1 - ' ') & 077) >> 4)); |
---|
815 | |
---|
816 | $file .= chr(((($c1 - ' ') & 077) << 4) | ((($c2 - ' ') & 077) >> 2)); |
---|
817 | |
---|
818 | $file .= chr(((($c2 - ' ') & 077) << 6) | (($c3 - ' ') & 077)); |
---|
819 | |
---|
820 | $pos += 4; |
---|
821 | $d += 3; |
---|
822 | } |
---|
823 | |
---|
824 | if (($d + 2 <= $len) && ($pos + 3 <= strlen($str[$i]))) { |
---|
825 | $c0 = (ord(substr($str[$i],$pos,1)) ^ 0x20); |
---|
826 | $c1 = (ord(substr($str[$i],$pos+1,1)) ^ 0x20); |
---|
827 | $c2 = (ord(substr($str[$i],$pos+2,1)) ^ 0x20); |
---|
828 | $file .= chr(((($c0 - ' ') & 077) << 2) | ((($c1 - ' ') & 077) >> 4)); |
---|
829 | |
---|
830 | $file .= chr(((($c1 - ' ') & 077) << 4) | ((($c2 - ' ') & 077) >> 2)); |
---|
831 | |
---|
832 | $pos += 3; |
---|
833 | $d += 2; |
---|
834 | } |
---|
835 | |
---|
836 | if (($d + 1 <= $len) && ($pos + 2 <= strlen($str[$i]))) { |
---|
837 | $c0 = (ord(substr($str[$i],$pos,1)) ^ 0x20); |
---|
838 | $c1 = (ord(substr($str[$i],$pos+1,1)) ^ 0x20); |
---|
839 | $file .= chr(((($c0 - ' ') & 077) << 2) | ((($c1 - ' ') & 077) >> 4)); |
---|
840 | |
---|
841 | } |
---|
842 | } |
---|
843 | $files[] = array('filename' => $filename, 'fileperm' => $fileperm, 'filedata' => $file); |
---|
844 | } |
---|
845 | |
---|
846 | return $files; |
---|
847 | } |
---|
848 | |
---|
849 | /** |
---|
850 | * getSendArray() returns the arguments required for Mail::send() |
---|
851 | * used to build the arguments for a mail::send() call |
---|
852 | * |
---|
853 | * Usage: |
---|
854 | * $mailtext = Full email (for example generated by a template) |
---|
855 | * $decoder = new Mail_mimeDecode($mailtext); |
---|
856 | * $parts = $decoder->getSendArray(); |
---|
857 | * if (!PEAR::isError($parts) { |
---|
858 | * list($recipents,$headers,$body) = $parts; |
---|
859 | * $mail = Mail::factory('smtp'); |
---|
860 | * $mail->send($recipents,$headers,$body); |
---|
861 | * } else { |
---|
862 | * echo $parts->message; |
---|
863 | * } |
---|
864 | * @return mixed array of recipeint, headers,body or Pear_Error |
---|
865 | * @access public |
---|
866 | * @author Alan Knowles <alan@akbkhome.com> |
---|
867 | */ |
---|
868 | function getSendArray() |
---|
869 | { |
---|
870 | // prevent warning if this is not set |
---|
871 | $this->_decode_headers = FALSE; |
---|
872 | $headerlist =$this->_parseHeaders($this->_header); |
---|
873 | $to = ""; |
---|
874 | if (!$headerlist) { |
---|
875 | return $this->raiseError("Message did not contain headers"); |
---|
876 | } |
---|
877 | foreach($headerlist as $item) { |
---|
878 | $header[$item['name']] = $item['value']; |
---|
879 | switch (strtolower($item['name'])) { |
---|
880 | case "to": |
---|
881 | case "cc": |
---|
882 | case "bcc": |
---|
883 | $to .= ",".$item['value']; |
---|
884 | default: |
---|
885 | break; |
---|
886 | } |
---|
887 | } |
---|
888 | if ($to == "") { |
---|
889 | return $this->raiseError("Message did not contain any recipents"); |
---|
890 | } |
---|
891 | $to = substr($to,1); |
---|
892 | return array($to,$header,$this->_body); |
---|
893 | } |
---|
894 | |
---|
895 | /** |
---|
896 | * Returns a xml copy of the output of |
---|
897 | * Mail_mimeDecode::decode. Pass the output in as the |
---|
898 | * argument. This function can be called statically. Eg: |
---|
899 | * |
---|
900 | * $output = $obj->decode(); |
---|
901 | * $xml = Mail_mimeDecode::getXML($output); |
---|
902 | * |
---|
903 | * The DTD used for this should have been in the package. Or |
---|
904 | * alternatively you can get it from cvs, or here: |
---|
905 | * http://www.phpguru.org/xmail/xmail.dtd. |
---|
906 | * |
---|
907 | * @param object Input to convert to xml. This should be the |
---|
908 | * output of the Mail_mimeDecode::decode function |
---|
909 | * @return string XML version of input |
---|
910 | * @access public |
---|
911 | */ |
---|
912 | function getXML($input) |
---|
913 | { |
---|
914 | $crlf = "\r\n"; |
---|
915 | $output = '<?xml version=\'1.0\'?>' . $crlf . |
---|
916 | '<!DOCTYPE email SYSTEM "http://www.phpguru.org/xmail/xmail.dtd">' . $crlf . |
---|
917 | '<email>' . $crlf . |
---|
918 | Mail_mimeDecode::_getXML($input) . |
---|
919 | '</email>'; |
---|
920 | |
---|
921 | return $output; |
---|
922 | } |
---|
923 | |
---|
924 | /** |
---|
925 | * Function that does the actual conversion to xml. Does a single |
---|
926 | * mimepart at a time. |
---|
927 | * |
---|
928 | * @param object Input to convert to xml. This is a mimepart object. |
---|
929 | * It may or may not contain subparts. |
---|
930 | * @param integer Number of tabs to indent |
---|
931 | * @return string XML version of input |
---|
932 | * @access private |
---|
933 | */ |
---|
934 | function _getXML($input, $indent = 1) |
---|
935 | { |
---|
936 | $htab = "\t"; |
---|
937 | $crlf = "\r\n"; |
---|
938 | $output = ''; |
---|
939 | $headers = @(array)$input->headers; |
---|
940 | |
---|
941 | foreach ($headers as $hdr_name => $hdr_value) { |
---|
942 | |
---|
943 | // Multiple headers with this name |
---|
944 | if (is_array($headers[$hdr_name])) { |
---|
945 | for ($i = 0; $i < count($hdr_value); $i++) { |
---|
946 | $output .= Mail_mimeDecode::_getXML_helper($hdr_name, $hdr_value[$i], $indent); |
---|
947 | } |
---|
948 | |
---|
949 | // Only one header of this sort |
---|
950 | } else { |
---|
951 | $output .= Mail_mimeDecode::_getXML_helper($hdr_name, $hdr_value, $indent); |
---|
952 | } |
---|
953 | } |
---|
954 | |
---|
955 | if (!empty($input->parts)) { |
---|
956 | for ($i = 0; $i < count($input->parts); $i++) { |
---|
957 | $output .= $crlf . str_repeat($htab, $indent) . '<mimepart>' . $crlf . |
---|
958 | Mail_mimeDecode::_getXML($input->parts[$i], $indent+1) . |
---|
959 | str_repeat($htab, $indent) . '</mimepart>' . $crlf; |
---|
960 | } |
---|
961 | } elseif (isset($input->body)) { |
---|
962 | $output .= $crlf . str_repeat($htab, $indent) . '<body><![CDATA[' . |
---|
963 | $input->body . ']]></body>' . $crlf; |
---|
964 | } |
---|
965 | |
---|
966 | return $output; |
---|
967 | } |
---|
968 | |
---|
969 | /** |
---|
970 | * Helper function to _getXML(). Returns xml of a header. |
---|
971 | * |
---|
972 | * @param string Name of header |
---|
973 | * @param string Value of header |
---|
974 | * @param integer Number of tabs to indent |
---|
975 | * @return string XML version of input |
---|
976 | * @access private |
---|
977 | */ |
---|
978 | function _getXML_helper($hdr_name, $hdr_value, $indent) |
---|
979 | { |
---|
980 | $htab = "\t"; |
---|
981 | $crlf = "\r\n"; |
---|
982 | $return = ''; |
---|
983 | |
---|
984 | $new_hdr_value = ($hdr_name != 'received') ? Mail_mimeDecode::_parseHeaderValue($hdr_value) : array('value' => $hdr_value); |
---|
985 | $new_hdr_name = str_replace(' ', '-', ucwords(str_replace('-', ' ', $hdr_name))); |
---|
986 | |
---|
987 | // Sort out any parameters |
---|
988 | if (!empty($new_hdr_value['other'])) { |
---|
989 | foreach ($new_hdr_value['other'] as $paramname => $paramvalue) { |
---|
990 | $params[] = str_repeat($htab, $indent) . $htab . '<parameter>' . $crlf . |
---|
991 | str_repeat($htab, $indent) . $htab . $htab . '<paramname>' . htmlspecialchars($paramname) . '</paramname>' . $crlf . |
---|
992 | str_repeat($htab, $indent) . $htab . $htab . '<paramvalue>' . htmlspecialchars($paramvalue) . '</paramvalue>' . $crlf . |
---|
993 | str_repeat($htab, $indent) . $htab . '</parameter>' . $crlf; |
---|
994 | } |
---|
995 | |
---|
996 | $params = implode('', $params); |
---|
997 | } else { |
---|
998 | $params = ''; |
---|
999 | } |
---|
1000 | |
---|
1001 | $return = str_repeat($htab, $indent) . '<header>' . $crlf . |
---|
1002 | str_repeat($htab, $indent) . $htab . '<headername>' . htmlspecialchars($new_hdr_name) . '</headername>' . $crlf . |
---|
1003 | str_repeat($htab, $indent) . $htab . '<headervalue>' . htmlspecialchars($new_hdr_value['value']) . '</headervalue>' . $crlf . |
---|
1004 | $params . |
---|
1005 | str_repeat($htab, $indent) . '</header>' . $crlf; |
---|
1006 | |
---|
1007 | return $return; |
---|
1008 | } |
---|
1009 | |
---|
1010 | } // End of class |
---|