From 5c26bd49b10a2666df9e4853b0740038b0cc3b88 Mon Sep 17 00:00:00 2001
From: Aleksander Machniak <alec@alec.pl>
Date: Thu, 14 Mar 2013 07:10:40 -0400
Subject: [PATCH] Added rcube_message::has_text_part(), simplified has_html_part() so it always works in "recursive mode" - removed $recursive argument.

---
 program/steps/mail/compose.inc          |    2 
 program/lib/Roundcube/rcube_message.php |   83 +++++++++++++++++++++++++++++++----------
 2 files changed, 63 insertions(+), 22 deletions(-)

diff --git a/program/lib/Roundcube/rcube_message.php b/program/lib/Roundcube/rcube_message.php
index 3f14266..7d58a8e 100644
--- a/program/lib/Roundcube/rcube_message.php
+++ b/program/lib/Roundcube/rcube_message.php
@@ -194,41 +194,82 @@
 
 
     /**
-     * Determine if the message contains a HTML part
+     * Determine if the message contains a HTML part. This must to be
+     * a real part not an attachment (or its part)
+     * This must to be
+     * a real part not an attachment (or its part)
      *
-     * @param bool $recursive Enables checking in all levels of the structure
-     * @param bool $enriched  Enables checking for text/enriched parts too
+     * @param bool $enriched Enables checking for text/enriched parts too
      *
      * @return bool True if a HTML is available, False if not
      */
-    function has_html_part($recursive = true, $enriched = false)
+    function has_html_part($enriched = false)
     {
         // check all message parts
         foreach ($this->parts as $part) {
             if ($part->mimetype == 'text/html' || ($enriched && $part->mimetype == 'text/enriched')) {
-                // Level check, we'll skip e.g. HTML attachments
-                if (!$recursive) {
-                    $level = explode('.', $part->mime_id);
+                // Skip if part is an attachment
+                if ($this->is_attachment($part)) {
+                    continue;
+                }
 
-                    // Skip if part is an attachment
-                    if ($this->is_attachment($part)) {
-                        continue;
+                $level = explode('.', $part->mime_id);
+
+                // Check if the part belongs to higher-level's alternative/related
+                while (array_pop($level) !== null) {
+                    if (!count($level)) {
+                        return true;
                     }
 
-                    // Check if the part belongs to higher-level's alternative/related
-                    while (array_pop($level) !== null) {
-                        if (!count($level)) {
-                            return true;
-                        }
-
-                        $parent = $this->mime_parts[join('.', $level)];
-                        if ($parent->mimetype != 'multipart/alternative' && $parent->mimetype != 'multipart/related') {
-                            continue 2;
-                        }
+                    $parent = $this->mime_parts[join('.', $level)];
+                    if ($parent->mimetype != 'multipart/alternative' && $parent->mimetype != 'multipart/related') {
+                        continue 2;
                     }
                 }
 
-                return true;
+                if ($part->size) {
+                    return true;
+                }
+            }
+        }
+
+        return false;
+    }
+
+
+    /**
+     * Determine if the message contains a text/plain part. This must to be
+     * a real part not an attachment (or its part)
+     *
+     * @return bool True if a plain text part is available, False if not
+     */
+    function has_text_part()
+    {
+        // check all message parts
+        foreach ($this->parts as $part) {
+            if ($part->mimetype == 'text/plain') {
+                // Skip if part is an attachment
+                if ($this->is_attachment($part)) {
+                    continue;
+                }
+
+                $level = explode('.', $part->mime_id);
+
+                // Check if the part belongs to higher-level's alternative/related
+                while (array_pop($level) !== null) {
+                    if (!count($level)) {
+                        return true;
+                    }
+
+                    $parent = $this->mime_parts[join('.', $level)];
+                    if ($parent->mimetype != 'multipart/alternative' && $parent->mimetype != 'multipart/related') {
+                        continue 2;
+                    }
+                }
+
+                if ($part->size) {
+                    return true;
+                }
             }
         }
 
diff --git a/program/steps/mail/compose.inc b/program/steps/mail/compose.inc
index d7cfe7d..dd6a1d8 100644
--- a/program/steps/mail/compose.inc
+++ b/program/steps/mail/compose.inc
@@ -539,7 +539,7 @@
 function rcmail_message_is_html()
 {
     global $MESSAGE;
-    return ($MESSAGE instanceof rcube_message) && $MESSAGE->has_html_part(false, true);
+    return ($MESSAGE instanceof rcube_message) && $MESSAGE->has_html_part(true);
 }
 
 function rcmail_prepare_message_body()

--
Gitblit v1.9.1