From 8b92d2b0507a98dfd380553c64765bcfdecc6fe3 Mon Sep 17 00:00:00 2001
From: thomascube <thomas@roundcube.net>
Date: Wed, 25 Jan 2012 15:18:15 -0500
Subject: [PATCH] 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)

---
 program/include/rcube_mime.php |   63 +++++++++++++++++++++++++++++++
 1 files changed, 62 insertions(+), 1 deletions(-)

diff --git a/program/include/rcube_mime.php b/program/include/rcube_mime.php
index bae3ac3..bc0b2aa 100644
--- a/program/include/rcube_mime.php
+++ b/program/include/rcube_mime.php
@@ -1,6 +1,6 @@
 <?php
 
-/**
+/*
  +-----------------------------------------------------------------------+
  | program/include/rcube_mime.php                                        |
  |                                                                       |
@@ -52,6 +52,67 @@
 
 
     /**
+     * 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

--
Gitblit v1.9.1