Use Combine wrapper to combine header and data portions of a MIME part
authorMichael M Slusarz <slusarz@curecanti.org>
Mon, 26 Oct 2009 18:30:36 +0000 (12:30 -0600)
committerMichael M Slusarz <slusarz@curecanti.org>
Mon, 26 Oct 2009 18:35:45 +0000 (12:35 -0600)
imp/lib/Contents.php
imp/lib/Spam.php
imp/view.php

index 9cc4cb9..693ef49 100644 (file)
@@ -208,10 +208,8 @@ class IMP_Contents
      *            DEFAULT: No
      * </pre>
      *
-     * @return mixed  The text of the part, a stream resource if 'stream'
-     *                is true and 'mimeheaders' is false, or an array of
-     *                header text and a body stream resource if 'stream' is
-     *                true and 'mimeheaders' is true.
+     * @return mixed  The text of the part or a stream resource if 'stream'
+     *                is true.
      */
     public function getBodyPart($id, $options = array())
     {
@@ -247,10 +245,11 @@ class IMP_Contents
                     $this->lastBodyPartDecode = $res[$this->_index]['bodypartdecode'][$id];
                 }
                 return $res[$this->_index]['bodypart'][$id];
+            } elseif (empty($options['stream'])) {
+                return $res[$this->_index]['mimeheader'][$id] . $res[$this->_index]['bodypart'][$id];
             } else {
-                return empty($options['stream'])
-                    ? $res[$this->_index]['mimeheader'][$id] . $res[$this->_index]['bodypart'][$id]
-                    : array($res[$this->_index]['mimeheader'][$id], $res[$this->_index]['bodypart'][$id]);
+                $swrapper = new Horde_Support_CombineStream(array($res[$this->_index]['mimeheader'][$id], $res[$this->_index]['bodypart'][$id]));
+                return $swrapper->fopen();
             }
         } catch (Horde_Imap_Client_Exception $e) {
             return '';
@@ -266,9 +265,8 @@ class IMP_Contents
      *            DEFAULT: No
      * </pre>
      *
-     * @return mixed  The full message text, or an array with a text entry
-     *                (header text) and a stream resource (body text) if
-     *                'stream' is true.
+     * @return mixed  The full message text or a stream resource if 'stream'
+     *                is true.
      */
     public function fullMessageText($options = array())
     {
@@ -281,9 +279,13 @@ class IMP_Contents
                 Horde_Imap_Client::FETCH_HEADERTEXT => array(array('peek' => true)),
                 Horde_Imap_Client::FETCH_BODYTEXT => array(array('peek' => true, 'stream' => !empty($options['stream'])))
             ), array('ids' => array($this->_index)));
-            return empty($options['stream'])
-                ? $res[$this->_index]['headertext'][0] . $res[$this->_index]['bodytext'][0]
-                : array($res[$this->_index]['headertext'][0], $res[$this->_index]['bodytext'][0]);
+
+            if (empty($options['stream'])) {
+                return $res[$this->_index]['headertext'][0] . $res[$this->_index]['bodytext'][0];
+            }
+
+            $swrapper = new Horde_Support_CombineStream(array($res[$this->_index]['headertext'][0], $res[$this->_index]['bodytext'][0]));
+            return $swrapper->fopen();
         } catch (Horde_Imap_Client_Exception $e) {
             return empty($options['stream'])
                 ? ''
index 0472712..5e304ec 100644 (file)
@@ -58,7 +58,7 @@ class IMP_Spam
                 /* If a (not)spam reporting program has been provided, use
                  * it. */
                 if (!empty($GLOBALS['conf'][$action]['program'])) {
-                    $raw_msg = $imp_contents->fullMessageText(true);
+                    $raw_msg = $imp_contents->fullMessageText(array('stream' => true));
 
                     /* Use a pipe to write the message contents. This should
                      * be secure. */
@@ -78,9 +78,7 @@ class IMP_Spam
                         Horde::logMessage('Cannot open process ' . $prog, __FILE__, __LINE__, PEAR_LOG_ERR);
                         return 0;
                     }
-                    fwrite($pipes[0], $raw_msg[0]);
-                    rewind($raw_msg[1]);
-                    stream_copy_to_stream($raw_msg[1], $pipes[0]);
+                    stream_copy_to_stream($raw_msg, $pipes[0]);
                     fclose($pipes[0]);
                     $stderr = '';
                     while (!feof($pipes[2])) {
@@ -107,7 +105,7 @@ class IMP_Spam
 
                 if ($to) {
                     if (!isset($raw_msg)) {
-                        $raw_msg = $imp_contents->fullMessageText(true);
+                        $raw_msg = $imp_contents->fullMessageText(array('stream' => true));
                     }
 
                     if (!isset($imp_compose)) {
index 0e6d0d2..991dbf5 100644 (file)
@@ -37,17 +37,6 @@ function _sanitizeName($name)
     return Horde_String::convertCharset(trim(preg_replace('/[^\pL\pN-+_. ]/u', '_', $name), ' _'), 'UTF-8');
 }
 
-function _fullMessageTextLength($ob)
-{
-    if (!$ob[1]) {
-        return strlen($ob[0]);
-    }
-    $stat = fseek($ob[1], 0, SEEK_END);
-    $len = strlen($ob[0]) + ftell($ob[1]);
-    rewind($ob[1]);
-    return $len;
-}
-
 require_once dirname(__FILE__) . '/lib/Application.php';
 
 /* Don't compress if we are already sending in compressed format. */
@@ -170,11 +159,10 @@ case 'view_attach':
 
 case 'view_source':
     $msg = $contents->fullMessageText(array('stream' => true));
-    $browser->downloadHeaders('Message Source', 'text/plain', true, _fullMessageTextLength($msg));
-    echo $msg[0];
-    if ($msg[1]) {
-        fpassthru($msg[1]);
-    }
+    fseek($msg, 0, SEEK_END);
+    $browser->downloadHeaders('Message Source', 'text/plain', true, ftell($msg));
+    rewind($msg);
+    fpassthru($msg);
     exit;
 
 case 'save_message':
@@ -194,10 +182,10 @@ case 'save_message':
 
     $hdr = 'From ' . $from . ' ' . $date->format('D M d H:i:s Y') . "\r\n";
     $msg = $contents->fullMessageText(array('stream' => true));
-    $browser->downloadHeaders($name . '.eml', 'message/rfc822', false, strlen($hdr) + _fullMessageTextLength($msg));
-    echo $hdr . $msg[0];
-    if ($msg[1]) {
-        fpassthru($msg[1]);
-    }
+    fseek($msg, 0, SEEK_END);
+    $browser->downloadHeaders($name . '.eml', 'message/rfc822', false, strlen($hdr) + ftell($msg));
+    echo $hdr;
+    rewind($msg);
+    fpassthru($msg);
     exit;
 }