Переглянути джерело

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.

Luke Ehresman 24 роки тому
батько
коміт
61b182e0ca
2 змінених файлів з 43 додано та 15 видалено
  1. 27 0
      functions/mime.php
  2. 16 15
      src/download.php

+ 27 - 0
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 ]----------------------------------------------------------- */
 
 

+ 16 - 15
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;
       }
    }