From dad694e102ab2ff432abf2f21de9c237bed86c36 Mon Sep 17 00:00:00 2001 From: Michael M Slusarz Date: Mon, 26 Oct 2009 12:30:36 -0600 Subject: [PATCH] Use Combine wrapper to combine header and data portions of a MIME part --- imp/lib/Contents.php | 28 +++++++++++++++------------- imp/lib/Spam.php | 8 +++----- imp/view.php | 30 +++++++++--------------------- 3 files changed, 27 insertions(+), 39 deletions(-) diff --git a/imp/lib/Contents.php b/imp/lib/Contents.php index 9cc4cb99f..693ef495d 100644 --- a/imp/lib/Contents.php +++ b/imp/lib/Contents.php @@ -208,10 +208,8 @@ class IMP_Contents * DEFAULT: No * * - * @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 * * - * @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']) ? '' diff --git a/imp/lib/Spam.php b/imp/lib/Spam.php index 04727129a..5e304ecb8 100644 --- a/imp/lib/Spam.php +++ b/imp/lib/Spam.php @@ -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)) { diff --git a/imp/view.php b/imp/view.php index 0e6d0d2eb..991dbf548 100644 --- a/imp/view.php +++ b/imp/view.php @@ -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; } -- 2.11.0