From: Michael M Slusarz Date: Tue, 30 Jun 2009 17:28:14 +0000 (-0600) Subject: Some more places to use streams X-Git-Url: https://git.internetallee.de/?a=commitdiff_plain;h=aa7658e6c837fca142ff52ffcd87fed665a6dab5;p=horde.git Some more places to use streams --- diff --git a/imp/lib/Compose.php b/imp/lib/Compose.php index 150eba287..57edd4a0b 100644 --- a/imp/lib/Compose.php +++ b/imp/lib/Compose.php @@ -1470,7 +1470,7 @@ class IMP_Compose $part->setCharset(NLS::getCharset()); $part->setType('message/rfc822'); $part->setName(_("Forwarded Message")); - $part->setContents($contents->fullMessageText()); + $part->setContents($contents->fullMessageText(array('stream' => true))); try { $this->addMIMEPartAttachment($part); @@ -1835,12 +1835,15 @@ class IMP_Compose switch ($this->_cache[$id]['filetype']) { case 'vfs': + // TODO: Use streams $vfs = VFS::singleton($GLOBALS['conf']['vfs']['type'], Horde::getDriverConfig('vfs', $GLOBALS['conf']['vfs']['type'])); $part->setContents($vfs->read(self::VFS_ATTACH_PATH, $this->_cache[$id]['filename'])); break; case 'file': - $part->setContents(file_get_contents($this->_cache[$id]['filename'])); + $fp = fopen($this->_cache[$id]['filename'], 'r'); + $part->setContents($fp); + fclose($fp); } return $part; diff --git a/imp/lib/Contents.php b/imp/lib/Contents.php index 36aac8f3c..104cd8477 100644 --- a/imp/lib/Contents.php +++ b/imp/lib/Contents.php @@ -232,23 +232,34 @@ class IMP_Contents /** * Returns the full message text. * - * @return string The full message text. + * @param array $options Additional options: + *
+     * 'stream' - (boolean) If true, return a stream for bodytext.
+     *            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. */ - public function fullMessageText() + public function fullMessageText($options = array()) { if (is_null($this->_mailbox)) { return $this->_message->toString(); } try { - // TODO - use streams $res = $GLOBALS['imp_imap']->ob->fetch($this->_mailbox, array( - Horde_Imap_Client::FETCH_HEADERTEXT => array(array('peek' => true)), - Horde_Imap_Client::FETCH_BODYTEXT => array(array('peek' => true)) + Horde_Imap_Client::FETCH_HEADERTEXT => array(array('peek' => true, 'stream' => !empty($options['stream']))), + Horde_Imap_Client::FETCH_BODYTEXT => array(array('peek' => true, 'stream' => !empty($options['stream']))) ), array('ids' => array($this->_index))); - return $res[$this->_index]['headertext'][0] . $res[$this->_index]['bodytext'][0]; + 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]); } catch (Horde_Imap_Client_Exception $e) { - return ''; + return empty($options['stream']) + ? '' + : array(); } } @@ -314,14 +325,8 @@ class IMP_Contents !is_null($part) && empty($options['nocontents']) && !is_null($this->_mailbox) && - !$part->getContents()) { - $contents = $this->getBodyPart($id, array('length' => empty($options['length']) ? null : $options['length'], 'stream' => true)); - if (($part->getPrimaryType() == 'text') && - (Horde_String::upper($part->getCharset()) == 'US-ASCII') && - Horde_Mime::is8bit($contents)) { - $contents = Horde_String::convertCharset($contents, 'US-ASCII'); - } - $part->setContents($contents); + !$part->getContentsAsStream()) { + $part->setContents($this->getBodyPart($id, array('length' => empty($options['length']) ? null : $options['length'], 'stream' => true))); if (empty($options['nodecode'])) { $part->transferDecodeContents(); diff --git a/imp/lib/Spam.php b/imp/lib/Spam.php index bb864b01b..c9e628396 100644 --- a/imp/lib/Spam.php +++ b/imp/lib/Spam.php @@ -52,7 +52,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(); + $raw_msg = $imp_contents->fullMessageText(true); /* Use a pipe to write the message contents. This should * be secure. */ @@ -72,7 +72,9 @@ class IMP_Spam Horde::logMessage('Cannot open process ' . $prog, __FILE__, __LINE__, PEAR_LOG_ERR); return 0; } - fwrite($pipes[0], $raw_msg); + fwrite($pipes[0], $raw_msg[0]); + rewind($raw_msg[1]); + stream_copy_to_stream($raw_msg[1], $pipes[0]); fclose($pipes[0]); $stderr = ''; while (!feof($pipes[2])) { diff --git a/imp/view.php b/imp/view.php index 05c034e7e..a7950ffb7 100644 --- a/imp/view.php +++ b/imp/view.php @@ -37,6 +37,14 @@ function _sanitizeName($name) return Horde_String::convertCharset(trim(preg_replace('/[^\pL\pN-+_. ]/u', '_', $name), ' _'), 'UTF-8'); } +function _fullMessageTextLength($ob) +{ + fseek($ob[1], 0, SEEK_END); + $len = strlen($ob[0]) + ftell($ob[1]); + rewind($ob[1]); + return $len; +} + /* Don't compress if we are already sending in compressed format. */ if ((isset($_GET['actionID']) && ($_GET['actionID'] == 'download_all')) || !empty($_GET['zip'])) { @@ -162,9 +170,10 @@ case 'view_attach': exit; case 'view_source': - $msg = $contents->fullMessageText(); - $browser->downloadHeaders('Message Source', 'text/plain', true, strlen($msg)); - echo $msg; + $msg = $contents->fullMessageText(array('stream' => true)); + $browser->downloadHeaders('Message Source', 'text/plain', true, _fullMessageTextLength($msg)); + echo $msg[0]; + fpassthru($msg[1]); exit; case 'save_message': @@ -182,8 +191,10 @@ case 'save_message': $date = new DateTime($mime_headers->getValue('date')); - $body = 'From ' . $from . ' ' . $date->format('D M d H:i:s Y') . "\r\n" . $contents->fullMessageText(); - $browser->downloadHeaders($name . '.eml', 'message/rfc822', false, strlen($body)); - echo $body; + $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]; + fpassthru($msg[1]); exit; }