fixed warning
[squirrelmail.git] / functions / mime.php
index e8b7664f30fc0a2de88776af36e110f1b66a79cd..f07d5760d111ea057d6bc2e956e13d7d440737c3 100644 (file)
@@ -30,7 +30,8 @@ function mime_structure ($bodystructure, $flags=array()) {
     $read = trim(substr ($bodystructure, strpos(strtolower($bodystructure), 'bodystructure') + 13));
     $msg = &new message();
     $read = trim(substr ($read, 0, -1));
-    $msg = $msg->parseStructure($read,0);
+    $res = $msg->parseStructure($read);
+    $msg = $res[0];
     $msg->setEnt('0');
     if (count($flags)) {
        foreach ($flags as $flag) {
@@ -57,8 +58,8 @@ function mime_structure ($bodystructure, $flags=array()) {
               }
               break;
             case 'M':
-              if (strtolower($flag) == '\$mdnsent') {
-                 $msg->is_mdn = true;
+              if (strtolower($flag) == '$mdnsent') {
+                 $msg->is_mdnsent = true;
               }
               break;
             default:
@@ -163,6 +164,10 @@ function mime_print_body_lines ($imap_stream, $id, $ent_id, $encoding) {
        $sid_s = $sid;
     }
 
+    $body = mime_fetch_body ($imap_stream, $id, $ent_id);
+    echo decodeBody($body, $encoding);
+    return;
+/*
     fputs ($imap_stream, "$sid FETCH $id BODY[$ent_id]\r\n");
     $cnt = 0;
     $continue = true;
@@ -172,26 +177,44 @@ function mime_print_body_lines ($imap_stream, $id, $ent_id, $encoding) {
     // This could be bad -- if the section has sqimap_session_id() . ' OK'
     // or similar, it will kill the download.
     while (!ereg("^".$sid_s." (OK|BAD|NO)(.*)$", $read, $regs)) {
-      if (trim($read) == ')==') {
-          $read1 = $read;
-          $read = fgets ($imap_stream,4096);
-          if (ereg("^".$sid." (OK|BAD|NO)(.*)$", $read, $regs)) {
-              return;
-          } else {
-              echo decodeBody($read1, $encoding) .
-                   decodeBody($read, $encoding);
-          }
-      } else if ($cnt) {
-          echo decodeBody($read, $encoding);
-      }
-      $read = fgets ($imap_stream,4096);
-      $cnt++;
+        if (trim($read) == ')==') {
+            $read1 = $read;
+            $read = fgets ($imap_stream,4096);
+            if (ereg("^".$sid." (OK|BAD|NO)(.*)$", $read, $regs)) {
+                return;
+            } else {
+                echo decodeBody($read1, $encoding) .
+                     decodeBody($read, $encoding);
+            }
+        } else if ($cnt) {
+            echo decodeBody($read, $encoding);
+        }
+        $read = fgets ($imap_stream,4096);
+        $cnt++;
 //      break;
     }
+*/
 }
 
 /* -[ END MIME DECODING ]----------------------------------------------------------- */
 
+/* findDisplayEntity
+ * Checks to see if $message contains content of type $type0/$type1
+ * returns the first entity number it finds of that type, or NULL if
+ * none is found.  Takes optional argument $start to allow the caller
+ * to continue where they left off
+ */
+function findDisplayEntity($message, $type0, $type1, $start=0) {
+    if ($message) {
+        for ($i = $start;isset($message->entities[$i]); $i++) {
+            $entity = $message->entities[$i];
+            if ($entity->type0 == $type0 && $entity->type1 == $type1) {
+                return $i;
+            }
+        }
+    }
+    return NULL;
+}
 
 // This is here for debugging purposese.  It will print out a list
 // of all the entity IDs that are in the $message object.
@@ -211,6 +234,33 @@ function listEntities ($message) {
   } 
 }
 
+function getPriorityStr($priority) {
+   $priority_level = substr($priority,0,1);
+
+   switch($priority_level) {
+     /* check for a higher then normal priority. */
+     case '1':
+     case '2':
+        $priority_string = _("High");
+        break;
+
+      /* check for a lower then normal priority. */
+     case '4':
+     case '5':
+        $priority_string = _("Low");
+        break;
+
+     /* check for a normal priority. */
+     case '3':
+     default:
+        $priority_level = '3';
+        $priority_string = _("Normal");
+        break;
+
+   }
+   return $priority_string;
+}
+
 /* returns a $message object for a particular entity id */
 function getEntity ($message, $ent_id) {
     return $message->getEntity($ent_id);
@@ -291,7 +341,6 @@ function formatBody($imap_stream, $message, $color, $wrap_at, $ent_num, $id, $ma
     if (($body_message->header->type0 == 'text') ||
         ($body_message->header->type0 == 'rfc822')) {
        $body = mime_fetch_body ($imap_stream, $id, $ent_num);
-       
         $body = decodeBody($body, $body_message->header->encoding);
         $hookResults = do_hook("message_body", $body);
         $body = $hookResults[1];
@@ -301,12 +350,14 @@ function formatBody($imap_stream, $message, $color, $wrap_at, $ent_num, $id, $ma
         if ($body_message->header->type1 == 'html') {
             if ( $show_html_default <> 1 ) {
                 $body = strip_tags( $body );
-                translateText($body, $wrap_at, $body_message->header->charset);
+                translateText($body, $wrap_at, 
+                 $body_message->header->getParameter['charset']);
             } else {
                 $body = magicHTML( $body, $id, $message, $mailbox );
             }
         } else {
-            translateText($body, $wrap_at, $body_message->header->charset);
+            translateText($body, $wrap_at, 
+              $body_message->header->getParameter('charset'));
         }
 
         if ($has_unsafe_images) {
@@ -329,148 +380,107 @@ function formatAttachments($message, $exclude_id, $mailbox, $id) {
     $att_ar = $message->getAttachments($exclude_id);
 
     if (!count($att_ar)) return '';
-    
-    $attachments = "<TABLE WIDTH=\"100%\" CELLSPACING=0 CELLPADDING=2 BORDER=0 BGCOLOR=\"$color[0]\"><TR>\n" .
-                "<TH ALIGN=\"left\" BGCOLOR=\"$color[9]\"><B>\n" .
-                _("Attachments") . ':' .
-                "</B></TH></TR><TR><TD>\n" .
-                "<TABLE CELLSPACING=0 CELLPADDING=1 BORDER=0>\n";
-   
+
+    $attachments = '';
+
     $urlMailbox = urlencode($mailbox);
 
     foreach ($att_ar as $att) {
-                
-        $ent = urlencode($att->entity_id);
-       $header = $att->header;
+        $ent = urldecode($att->entity_id);
+        $header = $att->header;
         $type0 = strtolower($header->type0);
         $type1 = strtolower($header->type1);
-       $name = '';
-       if ($type0 =='message' && $type1 == 'rfc822') {
-
-            $filename = decodeHeader($header->subject);
-            $display_filename = $filename;
-            
-            $DefaultLink =
-                "../src/read_body.php?startMessage=$startMessage&amp;passed_id=$id&amp;mailbox=$urlMailbox&amp;passed_ent_id=$ent";
-            if ($where && $what) {
-                $DefaultLink .= '&amp;where=' . urlencode($where) . '&amp;what=' . urlencode($what);
-            }
-            $Links['download link']['text'] = _("download");
-            $Links['download link']['href'] =
+        $name = '';
+        $Links['download link']['text'] = _("download");
+        $Links['download link']['href'] =
                 "../src/download.php?absolute_dl=true&amp;passed_id=$id&amp;mailbox=$urlMailbox&amp;passed_ent_id=$ent";
-            $ImageURL = '';
-
-            /* this executes the attachment hook with a specific MIME-type.
-                * if that doens't have results, it tries if there's a rule
-                * for a more generic type. */
-            $HookResults = do_hook("attachment $type0/$type1", $Links,
-                $startMessage, $id, $urlMailbox, $ent, $DefaultLink, $display_filename, $where, $what);
-            if(count($HookResults[1]) <= 1) {
-                $HookResults = do_hook("attachment $type0/*", $Links,
-                $startMessage, $id, $urlMailbox, $ent, $DefaultLink,
-                $display_filename, $where, $what);
+        $ImageURL = '';
+        if ($type0 =='message' && $type1 == 'rfc822') {
+            $default_page = '../src/read_body.php';
+            $rfc822_header = $att->rfc822_header;
+            $filename = decodeHeader($rfc822_header->subject);
+
+            $from_o = $rfc822_header->from;
+            if (is_object($from_o)) {
+                $from_name = $from_o->getAddress(false);
+            } else {
+                $from_name = _("Unknown sender");
             }
-
-            $Links = $HookResults[1];
-            $DefaultLink = $HookResults[6];
-
-            $attachments .= '<TR><TD>&nbsp;&nbsp;</TD><TD>' .
-                        "<A HREF=\"$DefaultLink\">$display_filename</A>&nbsp;</TD>" .
-                        '<TD><SMALL><b>' . show_readable_size($header->size) .
-                        '</b>&nbsp;&nbsp;</small></TD>' .
-                        "<TD><SMALL>[ $type0/$type1 ]&nbsp;</SMALL></TD>" .
-                        '<TD><SMALL>';
-           $from_o = $header->from;
-           if (isset($from_o)) {
-               $from_name = $from_o->getAddress(false);
+            $from_name = decodeHeader(htmlspecialchars($from_name));
+           $description = $from_name;
+        } else {
+            $default_page = '../src/download.php';
+           if (is_object($header->disposition)) {
+               $filename = decodeHeader($header->disposition->getProperty('filename'));
+               if (trim($filename) == '') {
+                  $name = decodeHeader($header->disposition->getProperty('name'));
+                  if (trim($name) == '') {
+                     if ( trim( $header->id ) == '' )
+                        $filename = 'untitled-[' . $ent . ']' ;
+                     else
+                        $filename = 'cid: ' . $header->id;
+                  } else {
+                     $filename = $name;
+                  }
+               }
            } else {
-               $from_name = _("Unknown sender");
+              if ( trim( $header->id ) == '' )
+                  $filename = 'untitled-[' . $ent . ']' ;
+               else
+                  $filename = 'cid: ' . $header->id;
            }
-           $from_name = decodeHeader(htmlspecialchars($from_name));
-            $attachments .= '<b>' . $from_name . '</b>';
-            $attachments .= '</SMALL></TD><TD><SMALL>&nbsp;';
-
-            $SkipSpaces = 1;
-            foreach ($Links as $Val) {
-                if ($SkipSpaces) {
-                    $SkipSpaces = 0;
-                } else {
-                    $attachments .= '&nbsp;&nbsp;|&nbsp;&nbsp;';
-                }
-                $attachments .= '<a href="' . $Val['href'] . '">' .  $Val['text'] . '</a>';
-            }
-            unset($Links);
-        } else {
-            $filename = decodeHeader($header->filename);
-            if (trim($filename) == '') {
-                if (trim($name) == '') {
-                    if ( trim( $header->id ) == '' )
-                        $display_filename = 'untitled-[' . $ent . ']' ;
-                    else
-                        $display_filename = 'cid: ' . $header->id;
-                } else {
-                    $display_filename = $name;
-                    $filename = $name;
-                }
+
+            if ($header->description) {
+                $description = htmlspecialchars($header->description);
             } else {
-                $display_filename = $filename;
-            }
+               $description = '';
+           }
+        }
 
-            $DefaultLink =
-                "../src/download.php?startMessage=$startMessage&amp;passed_id=$id&amp;mailbox=$urlMailbox&amp;passed_ent_id=$ent";
-            if ($where && $what) {
-              $DefaultLink = '&amp;where='. urlencode($where).'&amp;what='.urlencode($what);
-            }
-            $Links['download link']['text'] = _("download");
-            $Links['download link']['href'] =
-                "../src/download.php?absolute_dl=true&amp;passed_id=$id&amp;mailbox=$urlMailbox&amp;passed_ent_id=$ent";
-            $ImageURL = '';
-
-            /* this executes the attachment hook with a specific MIME-type.
-                * if that doens't have results, it tries if there's a rule
-                * for a more generic type. */
-            $HookResults = do_hook("attachment $type0/$type1", $Links,
-                $startMessage, $id, $urlMailbox, $ent, $DefaultLink,
-                $display_filename, $where, $what);
-            if(count($HookResults[1]) <= 1) {
-                $HookResults = do_hook("attachment $type0/*", $Links,
-                $startMessage, $id, $urlMailbox, $ent, $DefaultLink,
-                $display_filename, $where, $what);
-            }
+        $display_filename = $filename;
+        $DefaultLink = $default_page . "?startMessage=$startMessage"
+                     . "&amp;passed_id=$id&amp;mailbox=$urlMailbox"
+                     . "&amp;passed_ent_id=$ent";
+        if ($where && $what) {
+           $DefaultLink = '&amp;where='. urlencode($where).'&amp;what='.urlencode($what);
+        }
+        /* this executes the attachment hook with a specific MIME-type.
+            * if that doens't have results, it tries if there's a rule
+            * for a more generic type. */
+        $HookResults = do_hook("attachment $type0/$type1", $Links,
+            $startMessage, $id, $urlMailbox, $ent, $DefaultLink,
+            $display_filename, $where, $what);
+        if(count($HookResults[1]) <= 1) {
+            $HookResults = do_hook("attachment $type0/*", $Links,
+                    $startMessage, $id, $urlMailbox, $ent, $DefaultLink,
+                    $display_filename, $where, $what);
+        }
 
-            $Links = $HookResults[1];
-            $DefaultLink = $HookResults[6];
+        $Links = $HookResults[1];
+        $DefaultLink = $HookResults[6];
 
-            $attachments .= '<TR><TD>&nbsp;&nbsp;</TD><TD>' .
+        $attachments .= '<TR><TD>' .
                         "<A HREF=\"$DefaultLink\">$display_filename</A>&nbsp;</TD>" .
                         '<TD><SMALL><b>' . show_readable_size($header->size) .
                         '</b>&nbsp;&nbsp;</small></TD>' .
                         "<TD><SMALL>[ $type0/$type1 ]&nbsp;</SMALL></TD>" .
                         '<TD><SMALL>';
-            if ($message->header->description) {
-                $attachments .= '<b>' . htmlspecialchars(_($header->description)) . '</b>';
-            }
-            $attachments .= '</SMALL></TD><TD><SMALL>&nbsp;';
+        $attachments .= '<b>' . $description . '</b>';
+        $attachments .= '</SMALL></TD><TD><SMALL>&nbsp;';
 
-            $SkipSpaces = 1;
-            foreach ($Links as $Val) {
-                if ($SkipSpaces) {
-                    $SkipSpaces = 0;
-                } else {
-                    $attachments .= '&nbsp;&nbsp;|&nbsp;&nbsp;';
-                }
-                $attachments .= '<a href="' . $Val['href'] . '">' .  $Val['text'] . '</a>';
+        $SkipSpaces = 1;
+        foreach ($Links as $Val) {
+            if ($SkipSpaces) {
+                $SkipSpaces = 0;
+            } else {
+                $attachments .= '&nbsp;&nbsp;|&nbsp;&nbsp;';
             }
-
-            unset($Links);
-       }         
-   
-   }
-   $attachments .= "</SMALL></TD></TR>\n";
-   
-   $attachments .= "</TABLE></TD></TR></TABLE>";
-   
-   return $attachments;
+            $attachments .= '<a href="' . $Val['href'] . '">' .  $Val['text'] . '</a>';
+        }
+        unset($Links);
+        $attachments .= "</TD></TR>\n";
+    }
+    return $attachments;
 }
 
 /** this function decodes the body depending on the encoding type. **/
@@ -1557,4 +1567,4 @@ function magicHTML($body, $id, $message, $mailbox = 'INBOX'){
     return $trusted;
 }
 
-?>
\ No newline at end of file
+?>