| | |
| | | <?php |
| | | |
| | | /** |
| | | /* |
| | | +-----------------------------------------------------------------------+ |
| | | | program/include/rcube_mime.php | |
| | | | | |
| | | | This file is part of the Roundcube Webmail client | |
| | | | Copyright (C) 2005-2012, The Roundcube Dev Team | |
| | | | Copyright (C) 2011-2012, Kolab Systems AG | |
| | | | Licensed under the GNU GPL | |
| | | | | |
| | | | Licensed under the GNU General Public License version 3 or | |
| | | | any later version with exceptions for skins & plugins. | |
| | | | See the README file for a full license statement. | |
| | | | | |
| | | | PURPOSE: | |
| | | | MIME message parsing utilities | |
| | |
| | | |
| | | |
| | | /** |
| | | * Parse the given raw message source and return a structure |
| | | * of rcube_message_part objects. |
| | | * |
| | | * It makes use of the PEAR:Mail_mimeDecode library |
| | | * |
| | | * @param string The message source |
| | | * @return object rcube_message_part The message structure |
| | | */ |
| | | public static function parse_message($raw_body) |
| | | { |
| | | $mime = new Mail_mimeDecode($raw_body); |
| | | $struct = $mime->decode(array('include_bodies' => true, 'decode_bodies' => true)); |
| | | return self::structure_part($struct); |
| | | } |
| | | |
| | | |
| | | /** |
| | | * Recursive method to convert a Mail_mimeDecode part into a rcube_message_part object |
| | | * |
| | | * @param object A message part struct |
| | | * @param int Part count |
| | | * @param string Parent MIME ID |
| | | * |
| | | * @return object rcube_message_part |
| | | */ |
| | | private static function structure_part($part, $count=0, $parent='') |
| | | { |
| | | $struct = new rcube_message_part; |
| | | $struct->mime_id = $part->mime_id ? $part->mime_id : (empty($parent) ? (string)$count : "$parent.$count"); |
| | | $struct->headers = $part->headers; |
| | | $struct->ctype_primary = $part->ctype_primary; |
| | | $struct->ctype_secondary = $part->ctype_secondary; |
| | | $struct->mimetype = $part->ctype_primary . '/' . $part->ctype_secondary; |
| | | $struct->ctype_parameters = $part->ctype_parameters; |
| | | |
| | | if ($part->headers['content-transfer-encoding']) |
| | | $struct->encoding = $part->headers['content-transfer-encoding']; |
| | | if ($part->ctype_parameters['charset']) |
| | | $struct->charset = $part->ctype_parameters['charset']; |
| | | |
| | | $part_charset = $struct->charset ? $struct->charset : self::$default_charset; |
| | | |
| | | // determine filename |
| | | if (($filename = $part->d_parameters['filename']) || ($filename = $part->ctype_parameters['name'])) { |
| | | $struct->filename = rcube_mime::decode_mime_string($filename, $part_charset); |
| | | } |
| | | |
| | | // copy part body and convert it to UTF-8 if necessary |
| | | $struct->body = $part->ctype_primary == 'text' || !$part->ctype_parameters['charset'] ? rcube_charset::convert($part->body, $part_charset) : $part->body; |
| | | $struct->size = strlen($part->body); |
| | | $struct->disposition = $part->disposition; |
| | | |
| | | foreach ((array)$part->parts as $child_part) { |
| | | $struct->parts[] = self::structure_part($child_part, ++$count, $struct->mime_id); |
| | | } |
| | | |
| | | return $struct; |
| | | } |
| | | |
| | | |
| | | /** |
| | | * Split an address list into a structured array list |
| | | * |
| | | * @param string $input Input string |