Thomas Bruederli
2013-10-21 f06aa8058b7e32ba32d4551074b6e0b8a300f751
program/include/rcube_message.php
@@ -164,11 +164,13 @@
    /**
     * Get content of a specific part of this message
     *
     * @param string $mime_id Part MIME-ID
     * @param resource $fp File pointer to save the message part
     * @param string   $mime_id           Part MIME-ID
     * @param resource $fp File           pointer to save the message part
     * @param boolean  $skip_charset_conv Disables charset conversion
     *
     * @return string Part content
     */
    public function get_part_content($mime_id, $fp=NULL)
    public function get_part_content($mime_id, $fp = null, $skip_charset_conv = false)
    {
        if ($part = $this->mime_parts[$mime_id]) {
            // stored in message structure (winmail/inline-uuencode)
@@ -179,7 +181,7 @@
                return $fp ? true : $part->body;
            }
            // get from IMAP
            return $this->storage->get_message_part($this->uid, $mime_id, $part, NULL, $fp);
            return $this->storage->get_message_part($this->uid, $mime_id, $part, NULL, $fp, $skip_charset_conv);
        } else
            return null;
    }
@@ -209,7 +211,18 @@
                    // HTML part can be on the lower level, if not...
                    if (count($level) > 1) {
                        // It can be an alternative or related message part
                        $parent = $this->mime_parts[0];
                        // find parent part
                        $parent = null;
                        foreach ($this->mime_parts as $part) {
                            if ($part->mime_id == $level[0]) {
                                $parent = $part;
                            }
                        }
                        if (!$parent) {
                            continue;
                        }
                        if ($parent->mimetype != 'multipart/alternative' && $parent->mimetype != 'multipart/related') {
                            continue;
                        }
@@ -276,6 +289,32 @@
    /**
     * Checks if part of the message is an attachment (or part of it)
     *
     * @param rcube_message_part $part Message part
     *
     * @return bool True if the part is an attachment part
     */
    public function is_attachment($part)
    {
        foreach ($this->attachments as $att_part) {
            if ($att_part->mime_id == $part->mime_id) {
                return true;
            }
            // check if the part is a subpart of another attachment part (message/rfc822)
            if ($att_part->mimetype == 'message/rfc822') {
                if (in_array($part, (array)$att_part->parts)) {
                    return true;
                }
            }
        }
        return false;
    }
    /**
     * Read the message structure returend by the IMAP server
     * and build flat lists of content parts and attachments
     *
@@ -335,6 +374,11 @@
            foreach ($structure->parts as $p => $sub_part) {
                $sub_mimetype = $sub_part->mimetype;
                // skip empty text parts
                if (!$sub_part->size && preg_match('#^text/(plain|html|enriched)$#', $sub_mimetype)) {
                    continue;
                }
                // check if sub part is
                if ($sub_mimetype == 'text/plain')
@@ -651,23 +695,29 @@
        $parts = array();
        // FIXME: line length is max.65?
        $uu_regexp = '/begin [0-7]{3,4} ([^\n]+)\n(([\x21-\x7E]{0,65}\n)+)`\nend/s';
        $uu_regexp = '/begin [0-7]{3,4} ([^\n]+)\n/s';
        if (preg_match_all($uu_regexp, $part->body, $matches, PREG_SET_ORDER)) {
            // remove attachments bodies from the message body
            $part->body = preg_replace($uu_regexp, '', $part->body);
            // update message content-type
            $part->ctype_primary   = 'multipart';
            $part->ctype_secondary = 'mixed';
            $part->mimetype        = $part->ctype_primary . '/' . $part->ctype_secondary;
            $uu_endstring = "`\nend\n";
            // add attachments to the structure
            foreach ($matches as $pid => $att) {
                $startpos = strpos($part->body, $att[1]) + strlen($att[1]) + 1; // "\n"
                $endpos = strpos($part->body, $uu_endstring);
                $filebody = substr($part->body, $startpos, $endpos-$startpos);
                // remove attachments bodies from the message body
                $part->body = substr_replace($part->body, "", $startpos, $endpos+strlen($uu_endstring)-$startpos);
                $uupart = new rcube_message_part;
                $uupart->filename = trim($att[1]);
                $uupart->encoding = 'stream';
                $uupart->body     = convert_uudecode($att[2]);
                $uupart->body     = convert_uudecode($filebody);
                $uupart->size     = strlen($uupart->body);
                $uupart->mime_id  = 'uu.' . $part->mime_id . '.' . $pid;
@@ -678,6 +728,9 @@
                $parts[] = $uupart;
                unset($matches[$pid]);
            }
            // remove attachments bodies from the message body
            $part->body = preg_replace($uu_regexp, '', $part->body);
        }
        return $parts;