added parameters id and mailbox to formadBody / magicHTML for better/ easier
[squirrelmail.git] / functions / mime.php
index 7b741f10b5ee972837cb451bd5d9dd8522fa9ec4..ce2be9b894cef4b9b36c9f8bad8ff4bf27d625e6 100644 (file)
@@ -30,7 +30,6 @@ function mime_structure ($bodystructure, $flags=array()) {
     $read = trim(substr ($bodystructure, strpos(strtolower($bodystructure), 'bodystructure') + 13));
     $msg = &new message();
     $read = trim(substr ($read, 0, -1));
-    echo $read;
     $msg = $msg->parseStructure($read,0);
     $msg->setEnt('0');
     if (count($flags)) {
@@ -67,6 +66,7 @@ function mime_structure ($bodystructure, $flags=array()) {
           }
        }
     }
+//    listEntities($msg);
     return( $msg );
 }
 
@@ -157,12 +157,20 @@ function mime_print_body_lines ($imap_stream, $id, $ent_id, $encoding) {
     if (!ini_get("safe_mode")) {
         set_time_limit(0);
     }
+    if ($uid_support) {
+       $sid_s = substr($sid,0,strpos($sid, ' '));
+    } else {
+       $sid_s = $sid;
+    }
 
     fputs ($imap_stream, "$sid FETCH $id BODY[$ent_id]\r\n");
     $cnt = 0;
     $continue = true;
-    $read = fgets ($imap_stream,4096);
-    if (preg_match('/.*\{(\d+)\}.*/',$read,$regs)) {
+    $read = fgets ($imap_stream,8192);
+
+
+//    if (preg_match('/.*\{(\d+)\}.*/',$read,$regs)) {
+/*
        $size = $regs[1];
        $size_div = (int) ($size / 4096);
        $size_mod = $size % 4096;
@@ -174,11 +182,12 @@ function mime_print_body_lines ($imap_stream, $id, $ent_id, $encoding) {
        if ($size_mod > 0) {
           $read .= fread ($imap_stream, $size_mod);
        }
-       return decodeBody($read, $encoding);
+       echo decodeBody($read, $encoding);
     }
+*/
     // This could be bad -- if the section has sqimap_session_id() . ' OK'
     // or similar, it will kill the download.
-    while (!ereg("^".$sid." (OK|BAD|NO)(.*)$", $read, $regs)) {
+    while (!ereg("^".$sid_s." (OK|BAD|NO)(.*)$", $read, $regs)) {
       if (trim($read) == ')==') {
           $read1 = $read;
           $read = fgets ($imap_stream,4096);
@@ -193,6 +202,7 @@ function mime_print_body_lines ($imap_stream, $id, $ent_id, $encoding) {
       }
       $read = fgets ($imap_stream,4096);
       $cnt++;
+//      break;
     }
 }
 
@@ -398,7 +408,7 @@ be displayed as the actual message in the HTML. It contains
 everything needed, including HTML Tags, Attachments at the
 bottom, etc.
 */
-function formatBody($imap_stream, $message, $color, $wrap_at, $ent_num) {
+function formatBody($imap_stream, $message, $color, $wrap_at, $ent_num, $id, $mailbox='INBOX') {
     // this if statement checks for the entity to show as the
     // primary message. To add more of them, just put them in the
     // order that is their priority.
@@ -407,8 +417,9 @@ function formatBody($imap_stream, $message, $color, $wrap_at, $ent_num) {
 
     $has_unsafe_images = 0;
 
-    $id = $message->id;
-
+    if ($message->type0 == 'message' && $message->type1 == 'rfc822') {
+        $message = $message->entities[0];
+    }
     $urlmailbox = urlencode($message->mailbox);
     $body_message = getEntity($message, $ent_num);
     if (($body_message->header->type0 == 'text') ||
@@ -425,7 +436,7 @@ function formatBody($imap_stream, $message, $color, $wrap_at, $ent_num) {
                 $body = strip_tags( $body );
                 translateText($body, $wrap_at, $body_message->header->charset);
             } else {
-                $body = magicHTML( $body, $id, $message );
+                $body = magicHTML( $body, $id, $message, $mailbox );
             }
         } else {
             translateText($body, $wrap_at, $body_message->header->charset);
@@ -440,19 +451,13 @@ function formatBody($imap_stream, $message, $color, $wrap_at, $ent_num) {
         }
 
         /** Display the ATTACHMENTS: message if there's more than one part **/
-       if ($message->type0 == 'message') {
-           $id = $message->id;
-           $mailbox = $message->mailbox;
-//         $message->header->setVar('message_id',$id);
-//         $message->header->setVar('mailbox',$mailbox);
-       }
         if (isset($message->entities[1])) {
            /* Header-type alternative means we choose the best one to display 
               so don't show the alternatives as attachment. Header-type related
               means that the attachments are already part of the related message.
            */   
            if ($message->header->type1 !='related' && $message->header->type1 !='alternative') {
-               $body .= formatAttachments ($message, $ent_num, $message->mailbox, $id);
+               $body .= formatAttachments ($message, $ent_num, $mailbox, $id);
            }
         }
     } else {
@@ -1226,7 +1231,8 @@ function sq_fixatts($tagname,
                     $bad_attvals,
                     $add_attr_to_tag,
                     $message,
-                    $id
+                    $id,
+                   $mailbox
                     ){
     $me = "sq_fixatts";
     while (list($attname, $attvalue) = each($attary)){
@@ -1277,7 +1283,7 @@ function sq_fixatts($tagname,
          * Turn cid: urls into http-friendly ones.
          */
         if (preg_match("/^[\'\"]\s*cid:/si", $attvalue)){
-            $attary{$attname} = sq_cid2http($message, $id, $attvalue);
+            $attary{$attname} = sq_cid2http($message, $id, $attvalue, $mailbox);
         }
     }
     /**
@@ -1357,7 +1363,7 @@ function sq_fixstyle($message, $id, $content){
  * @param  $cidurl   the cid: url.
  * @return           a string with a http-friendly url
  */
-function sq_cid2http($message, $id, $cidurl){
+function sq_cid2http($message, $id, $cidurl, $mailbox){
     /**
      * Get rid of quotes.
      */
@@ -1365,7 +1371,7 @@ function sq_cid2http($message, $id, $cidurl){
     $cidurl = str_replace($quotchar, "", $cidurl);
     $cidurl = substr(trim($cidurl), 4);
     $httpurl = $quotchar . "../src/download.php?absolute_dl=true&" .
-        "passed_id=$id&mailbox=" . urlencode($message->header->mailbox) .
+        "passed_id=$id&mailbox=" . urlencode($mailbox) .
         "&passed_ent_id=" . find_ent_id($cidurl, $message) . $quotchar;
     return $httpurl;
 }
@@ -1434,7 +1440,8 @@ function sq_sanitize($body,
                      $bad_attvals,
                      $add_attr_to_tag,
                      $message,
-                     $id
+                     $id,
+                    $mailbox
                      ){
     $me = "sq_sanitize";
     /**
@@ -1543,7 +1550,8 @@ function sq_sanitize($body,
                                                      $bad_attvals,
                                                      $add_attr_to_tag,
                                                      $message,
-                                                     $id
+                                                     $id,
+                                                    $mailbox
                                                      );
                             }
                             /**
@@ -1586,7 +1594,7 @@ function sq_sanitize($body,
  * @param  $id    the id of the message
  * @return        a string with html safe to display in the browser.
  */
-function magicHTML($body, $id, $message){
+function magicHTML($body, $id, $message, $mailbox = 'INBOX'){
     global $attachment_common_show_images, $view_unsafe_images,
         $has_unsafe_images;
     /**
@@ -1712,7 +1720,8 @@ function magicHTML($body, $id, $message){
                            $bad_attvals,
                            $add_attr_to_tag,
                            $message,
-                           $id
+                           $id,
+                          $mailbox
                            );
     if (preg_match("|$secremoveimg|si", $trusted)){
         $has_unsafe_images = true;