summaryrefslogtreecommitdiff
path: root/roundcubemail/program/include/rcube_mime.php
diff options
context:
space:
mode:
authorthomasb <thomasb@208e9e7b-5314-0410-a742-e7e81cd9613c>2012-01-25 20:18:15 +0000
committerthomasb <thomasb@208e9e7b-5314-0410-a742-e7e81cd9613c>2012-01-25 20:18:15 +0000
commit8e3025b7df50e7c57e1878d509873bc6d48e5de5 (patch)
tree1ee1a4f85f9a9e82bd9e18fe8999ac2be49ab4bc /roundcubemail/program/include/rcube_mime.php
parenta02777749fa2f6e6ff5824b8e5d015fe2f0508b9 (diff)
Add lib for server side mime parsing (to be used by non-imap storage backends or as fallback if imap server doesn't provide a proper structure)
git-svn-id: https://svn.roundcube.net/trunk@5823 208e9e7b-5314-0410-a742-e7e81cd9613c
Diffstat (limited to 'roundcubemail/program/include/rcube_mime.php')
-rw-r--r--roundcubemail/program/include/rcube_mime.php63
1 files changed, 62 insertions, 1 deletions
diff --git a/roundcubemail/program/include/rcube_mime.php b/roundcubemail/program/include/rcube_mime.php
index bae3ac38f..bc0b2aa8c 100644
--- a/roundcubemail/program/include/rcube_mime.php
+++ b/roundcubemail/program/include/rcube_mime.php
@@ -1,6 +1,6 @@
<?php
-/**
+/*
+-----------------------------------------------------------------------+
| program/include/rcube_mime.php |
| |
@@ -52,6 +52,67 @@ class rcube_mime
/**
+ * 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;
+
+ // TODO: 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' ? 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