From beb9e459386498b14f629d7d50eb62c23c4d8fce Mon Sep 17 00:00:00 2001 From: lkehresman Date: Wed, 10 Jan 2001 04:36:00 +0000 Subject: [PATCH] greatly improved speed and memory management of download of attachments. it doesn't save the file in memory, it now feeds it line-by-line. git-svn-id: https://svn.code.sf.net/p/squirrelmail/code/trunk/squirrelmail@921 7612ce4b-ef26-0410-bec9-ea0150e637f0 --- functions/mime.php | 27 +++++++++++++++++++++++++++ src/download.php | 31 ++++++++++++++++--------------- 2 files changed, 43 insertions(+), 15 deletions(-) diff --git a/functions/mime.php b/functions/mime.php index 823f1b14..7e054d05 100644 --- a/functions/mime.php +++ b/functions/mime.php @@ -389,6 +389,33 @@ return "Body retrival error, please report this bug!\n\nTop line is \"$topline\"\n"; } + function mime_print_body_lines ($imap_stream, $id, $ent_id, $encoding) { + // do a bit of error correction. If we couldn't find the entity id, just guess + // that it is the first one. That is usually the case anyway. + if (!$ent_id) $ent_id = 1; + + fputs ($imap_stream, "a001 FETCH $id BODY[$ent_id]\r\n"); + $cnt = 0; + $continue = true; + $read = fgets ($imap_stream,4096); + while (!ereg("^a001 (OK|BAD|NO)(.*)$", $read, $regs)) { + if (trim($read) == ")==") { + $read1 = $read; + $read = fgets ($imap_stream,4096); + if (ereg("^a001 (OK|BAD|NO)(.*)$", $read, $regs)) { + return; + } else { + echo decodeBody($read1, $encoding); + echo decodeBody($read, $encoding); + } + } else if ($cnt) { + echo decodeBody($read, $encoding); + } + $read = fgets ($imap_stream,4096); + $cnt++; + } + } + /* -[ END MIME DECODING ]----------------------------------------------------------- */ diff --git a/src/download.php b/src/download.php index 27b4d69c..afa21383 100644 --- a/src/download.php +++ b/src/download.php @@ -72,7 +72,6 @@ $message = getEntity($message, $passed_ent_id); $header = $message->header; - $body = mime_fetch_body($imapConnection, $passed_id, $passed_ent_id); $charset = $header->charset; $type0 = $header->type0; @@ -108,6 +107,7 @@ if ($absolute_dl == "true") { switch($type0) { case "text": + $body = mime_fetch_body($imapConnection, $passed_id, $passed_ent_id); $body = decodeBody($body, $header->encoding); header("Content-Disposition: attachment; filename=\"$filename\""); header("Content-type: application/octet-stream; name=\"$filename\""); @@ -121,36 +121,37 @@ echo trim($body); break; default: - $body = decodeBody($body, $header->encoding); header("Content-Disposition: attachment; filename=\"$filename\""); header("Content-type: application/octet-stream; name=\"$filename\""); - echo $body; + mime_print_body_lines ($imapConnection, $passed_id, $passed_ent_id, $header->encoding); break; } } else { switch ($type0) { case "text": - if ($type1 == "text" || $type1 == "html") { - $body = decodeBody($body, $header->encoding); - include("../functions/page_header.php"); - viewText($color, $body, $passed_id, $passed_ent_id, $mailbox, $type1, $wrap_at); + if ($type1 == "plain" || $type1 == "html") { + $body = mime_fetch_body($imapConnection, $passed_id, $passed_ent_id); + $body = decodeBody($body, $header->encoding); + include("../functions/page_header.php"); + viewText($color, $body, $passed_id, $passed_ent_id, $mailbox, $type1, $wrap_at); } else { - $body = decodeBody($body, $header->encoding); - header("Content-type: $type0/$type1; name=\"$filename\""); - header("Content-Disposition: attachment; filename=\"$filename\""); - echo $body; - } - break; + $body = mime_fetch_body($imapConnection, $passed_id, $passed_ent_id); + $body = decodeBody($body, $header->encoding); + header("Content-type: $type0/$type1; name=\"$filename\""); + header("Content-Disposition: attachment; filename=\"$filename\""); + echo $body; + } + break; case "message": + $body = mime_fetch_body($imapConnection, $passed_id, $passed_ent_id); $body = decodeBody($body, $header->encoding); include("../functions/page_header.php"); viewText($color, $body, $passed_id, $passed_ent_id, $mailbox, $type1, $wrap_at); break; default: - $body = decodeBody($body, $header->encoding); header("Content-type: $type0/$type1; name=\"$filename\""); header("Content-Disposition: attachment; filename=\"$filename\""); - echo $body; + mime_print_body_lines ($imapConnection, $passed_id, $passed_ent_id, $header->encoding); break; } } -- 2.25.1