From dff2c713fbc5dd8a501a31f3c158b8be412f46d0 Mon Sep 17 00:00:00 2001 From: Thomas Bruederli Date: Tue, 27 Nov 2012 16:22:49 +0100 Subject: Add argument to fetch message parts only partially. Can be used for content mimetype detection without loading the entire file --- program/lib/Roundcube/rcube_imap.php | 5 +++-- program/lib/Roundcube/rcube_imap_generic.php | 13 ++++++++----- program/lib/Roundcube/rcube_message.php | 5 +++-- 3 files changed, 14 insertions(+), 9 deletions(-) diff --git a/program/lib/Roundcube/rcube_imap.php b/program/lib/Roundcube/rcube_imap.php index 9df2bf666..8ca24dec7 100644 --- a/program/lib/Roundcube/rcube_imap.php +++ b/program/lib/Roundcube/rcube_imap.php @@ -2051,10 +2051,11 @@ class rcube_imap extends rcube_storage * @param mixed $print True to print part, ressource to write part contents in * @param resource $fp File pointer to save the message part * @param boolean $skip_charset_conv Disables charset conversion + * @param int $max_bytes Only read this number of bytes * * @return string Message/part body if not printed */ - public function get_message_part($uid, $part=1, $o_part=NULL, $print=NULL, $fp=NULL, $skip_charset_conv=false) + public function get_message_part($uid, $part=1, $o_part=NULL, $print=NULL, $fp=NULL, $skip_charset_conv=false, $max_bytes=0) { if (!$this->check_connection()) { return null; @@ -2074,7 +2075,7 @@ class rcube_imap extends rcube_storage if ($o_part && $o_part->size) { $body = $this->conn->handlePartBody($this->folder, $uid, true, - $part ? $part : 'TEXT', $o_part->encoding, $print, $fp, $o_part->ctype_primary == 'text'); + $part ? $part : 'TEXT', $o_part->encoding, $print, $fp, $o_part->ctype_primary == 'text', $max_bytes); } if ($fp || $print) { diff --git a/program/lib/Roundcube/rcube_imap_generic.php b/program/lib/Roundcube/rcube_imap_generic.php index a0a8f3b77..70fd6eb2c 100644 --- a/program/lib/Roundcube/rcube_imap_generic.php +++ b/program/lib/Roundcube/rcube_imap_generic.php @@ -2379,7 +2379,7 @@ class rcube_imap_generic return $this->handlePartBody($mailbox, $id, $is_uid, $part); } - function handlePartBody($mailbox, $id, $is_uid=false, $part='', $encoding=NULL, $print=NULL, $file=NULL, $formatted=false) + function handlePartBody($mailbox, $id, $is_uid=false, $part='', $encoding=NULL, $print=NULL, $file=NULL, $formatted=false, $max_bytes=0) { if (!$this->select($mailbox)) { return false; @@ -2405,10 +2405,11 @@ class rcube_imap_generic // Use BINARY extension when possible (and safe) $binary = $mode && preg_match('/^[0-9.]+$/', $part) && $this->hasCapability('BINARY'); $fetch_mode = $binary ? 'BINARY' : 'BODY'; + $partial = $max_bytes ? sprintf('<0.%d>', $max_bytes) : ''; // format request $key = $this->nextTag(); - $request = $key . ($is_uid ? ' UID' : '') . " FETCH $id ($fetch_mode.PEEK[$part])"; + $request = $key . ($is_uid ? ' UID' : '') . " FETCH $id ($fetch_mode.PEEK[$part]$partial)"; // send request if (!$this->putLine($request)) { @@ -2508,8 +2509,10 @@ class rcube_imap_generic $line = rtrim($line, "\t\r\n\0\x0B") . "\n"; } - if ($file) - fwrite($file, $line); + if ($file) { + if (fwrite($file, $line) === false) + break; + } else if ($print) echo $line; else @@ -2525,7 +2528,7 @@ class rcube_imap_generic if ($result !== false) { if ($file) { - fwrite($file, $result); + return fwrite($file, $result); } else if ($print) { echo $result; } else diff --git a/program/lib/Roundcube/rcube_message.php b/program/lib/Roundcube/rcube_message.php index 87319f04f..4ef534a0a 100644 --- a/program/lib/Roundcube/rcube_message.php +++ b/program/lib/Roundcube/rcube_message.php @@ -173,10 +173,11 @@ class rcube_message * @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 + * @param int $max_bytes Only read this number of bytes * * @return string Part content */ - public function get_part_content($mime_id, $fp = null, $skip_charset_conv = false) + public function get_part_content($mime_id, $fp = null, $skip_charset_conv = false, $max_bytes = 0) { if ($part = $this->mime_parts[$mime_id]) { // stored in message structure (winmail/inline-uuencode) @@ -190,7 +191,7 @@ class rcube_message // get from IMAP $this->storage->set_folder($this->folder); - return $this->storage->get_message_part($this->uid, $mime_id, $part, NULL, $fp, $skip_charset_conv); + return $this->storage->get_message_part($this->uid, $mime_id, $part, NULL, $fp, $skip_charset_conv, $max_bytes); } } -- cgit v1.2.3