removed debug message
[squirrelmail.git] / src / download.php
index 22f9c60557d1e3921e90b0f36fd0564ed537492c..6a24ce183e72b1b4f4796def32fcb7b7d8a94efd 100644 (file)
@@ -16,47 +16,60 @@ require_once('../src/validate.php');
 require_once('../functions/imap.php');
 require_once('../functions/mime.php');
 require_once('../functions/date.php');
+require_once('../functions/html.php');
 
 header('Pragma: ');
 header('Cache-Control: cache');
 
-function viewText($color, $body, $id, $entid, $mailbox, $type1, $wrap_at) {
+function viewText($color, $body, $id, $entid, $mailbox, $type1, $wrap_at, $imapConnection) {
     global $where, $what, $charset;
     global $startMessage;
 
     displayPageHeader($color, 'None');
 
-    echo "<BR><TABLE WIDTH=\"100%\" BORDER=0 CELLSPACING=0 CELLPADDING=2 ALIGN=CENTER><TR><TD BGCOLOR=\"$color[0]\">".
-         "<B><CENTER>".
-         _("Viewing a text attachment") . " - ";
     if ($where && $what) {
         // from a search
-        echo "<a href=\"read_body.php?mailbox=".urlencode($mailbox)."&passed_id=$id&where=".urlencode($where)."&what=".urlencode($what)."\">". _("View message") . "</a>";
+        $message_link_str = "<a href=\"read_body.php?mailbox=".urlencode($mailbox)."&passed_id=$id&where=".urlencode($where)."&what=".urlencode($what)."\">". _("View message") . "</a>";
     } else {
-        echo "<a href=\"read_body.php?mailbox=".urlencode($mailbox)."&passed_id=$id&startMessage=$startMessage&show_more=0\">". _("View message") . "</a>";
+        $message_link_str = "<a href=\"read_body.php?mailbox=".urlencode($mailbox)."&passed_id=$id&startMessage=$startMessage&show_more=0\">". _("View message") . "</a>";
     }
-
     $urlmailbox = urlencode($mailbox);
-    echo "</b></td><tr><tr><td><CENTER><A HREF=\"../src/download.php?absolute_dl=true&passed_id=$id&passed_ent_id=$entid&mailbox=$urlmailbox\">".
-         _("Download this as a file").
-         "</A></CENTER><BR>".
-         "</CENTER></B>".
-         "</TD></TR></TABLE>".
-         "<TABLE WIDTH=\"98%\" BORDER=0 CELLSPACING=0 CELLPADDING=2 ALIGN=CENTER><TR><TD BGCOLOR=\"$color[0]\">".
-         "<TR><TD BGCOLOR=\"$color[4]\"><TT>";
+
+    echo '<br>' .
+        html_tag( 'table',
+            html_tag( 'tr',
+                html_tag( 'td', 
+                     '<b><center>' .
+                     _("Viewing a text attachment") . ' - ' . $message_link_str . '</b></center>',
+                'left', $color[0] )
+            ) .
+            html_tag( 'tr',
+                html_tag( 'td', 
+                     '<center>' .
+                     "<A HREF=\"../src/download.php?absolute_dl=true&passed_id=$id&passed_ent_id=$entid&mailbox=$urlmailbox\">".
+                     _("Download this as a file").
+                     "</A></CENTER><BR>",
+                'left' )
+            ),
+        'center', '', 'width="100%" border="0" cellspacing="0" cellpadding="2"' );
 
     if ($type1 == 'html') {
-        $body = MagicHTML( $body, $id );
+        $msg  = sqimap_get_message($imapConnection, $id, $mailbox);    
+        $body = MagicHTML( $body, $id, $msg );
     } else {
         translateText($body, $wrap_at, $charset);
     }
 
     flush();
-    echo $body .
-         "</TT></TD></TR></TABLE>";
+
+        html_tag( 'table',
+            html_tag( 'tr',
+                html_tag( 'td', '<tt>' . $body . '</tt>', 'left', $color[4] )
+            ) ,
+        'center', '', 'width="98%" border="0" cellspacing="0" cellpadding="2"' );
 }
 
-function viewMessage($imapConnection, $id, $mailbox, $ent_id, $color, $wrap_at) {
+function viewMessage($imapConnection, $id, $mailbox, $ent_id, $color, $wrap_at, $extracted) {
     global $startMessage;
 
 
@@ -64,31 +77,87 @@ function viewMessage($imapConnection, $id, $mailbox, $ent_id, $color, $wrap_at)
     $msg = getEntity($msg, $ent_id);    
 
     $header = sqimap_get_ent_header($imapConnection,$id,$mailbox,$ent_id);
+    $header->id = $id;
     $msg->header = $header;
-    $msg->header->id = $id;
-    $body = formatBody($imapConnection, $msg, $color, $wrap_at);
+
+    $ent_ar = findDisplayEntity($msg, 0);
+    $body = '';
+    for ($i = 0; $i < count($ent_ar); $i++) {
+       $body .= formatBody($imapConnection, $msg, $color, $wrap_at, $ent_ar[$i], false);
+    }
+    
     $bodyheader = viewHeader($header, $color);
     displayPageHeader($color, 'None');
 
-    echo "<BR><TABLE WIDTH=\"100%\" BORDER=0 CELLSPACING=0 CELLPADDING=2 ALIGN=CENTER><TR><TD BGCOLOR=\"$color[0]\">".
-       "<B><CENTER>".  _("Viewing a message attachment") . " - ";
-    
-    echo "<a href=\"read_body.php?mailbox=".urlencode($mailbox)."&passed_id=$id&startMessage=$startMessage&show_more=0\">". _("View message") . "</a>";
-    
+    echo '<br>' .
+        html_tag( 'table', '', 'center', '', 'width="100%" border="0" cellspacing="0" cellpadding="2"' );
+
+    if ($extracted) {
+       echo html_tag( 'tr',
+                   html_tag( 'td', '<center><h1>Message succesfully extracted</h1></center>', 'left', '', 'width="100%"' )
+               );
+    }
+
+    $td_str = "<b><center>". _("Viewing a message attachment") . " - ";
+    $td_str .= "<a href=\"read_body.php?mailbox=".urlencode($mailbox)."&passed_id=$id&startMessage=$startMessage&show_more=0\">". _("View message") . "</a></b></center>";
+    echo html_tag( 'tr',
+               html_tag( 'td', $td_str, 'left', $color[0] )
+           );
+
     $urlmailbox = urlencode($mailbox);
-    
-    echo "</b></td><tr><tr><td><CENTER><A HREF=\"../src/download.php?absolute_dl=true&passed_id=$id&passed_ent_id=$ent_id&mailbox=$urlmailbox\">".
+    $td_str = "<center><b><A HREF=\"../src/download.php?absolute_dl=true&passed_id=$id&passed_ent_id=$ent_id&mailbox=$urlmailbox\">".
        _("Download this as a file").
-       "</A></CENTER><BR>".
-       "</CENTER></B>".
-       "</TD></TR></TABLE>";
-    echo "<TABLE WIDTH=\"100%\" BORDER=0 CELLSPACING=0 CELLPADDING=2 ALIGN=CENTER><TR><TD BGCOLOR=\"$color[0]\">".
-       "<TR><TD BGCOLOR=\"$color[4]\">";
-    echo "$bodyheader </TD></TR></TABLE>";          
-       
-    echo "<TABLE WIDTH=\"98%\" BORDER=0 CELLSPACING=0 CELLPADDING=2 ALIGN=CENTER><TR><TD BGCOLOR=\"$color[0]\">".
-       "<TR><TD BGCOLOR=\"$color[4]\"><TT>";
-       echo "$body </TT></TD></TR></TABLE>";    
+       "</A><BR>".
+       "</b></center>";
+    echo html_tag( 'tr',
+               html_tag( 'td', $td_str, 'left' )
+           ) .
+
+       "</TABLE>\n" .
+    html_tag( 'table',
+                html_tag( 'tr',
+                    html_tag( 'td', $bodyheader, 'left', $color[4] )
+                ) ,
+            'center', '', 'width="100%" border="0" cellspacing="0" cellpadding="2"' ) . "\n" .
+    html_tag( 'table',
+                html_tag( 'tr',
+                    html_tag( 'td', '<tt><br>' . $body . '</tt>', 'left', $color[4] )
+                ) ,
+            'center', '', 'width="98%" border="0" cellspacing="0" cellpadding="2"' ) . "<br>\n";       
+
+    echo html_tag( 'table', '', '', '', 'width="100%"' ) .
+                html_tag( 'tr' ) .
+                    html_tag( 'td', '', 'center', $color[9], 'width="100%"' ) .
+          '<form action="download.php" method="post"><small>'.
+          "<input type=\"hidden\" name=\"passed_id\" value=\"$id\">".
+          "<input type=\"hidden\" name=\"mailbox\" value=\"".$mailbox."\">".
+          "<input type=\"hidden\" name=\"startMessage\" value=\"$startMessage\">".
+          "<input type=\"hidden\" name=\"passed_ent_id\" value=\"$ent_id\">".
+          "<input type=\"hidden\" name=\"extract_message\" value=\"1\">".
+          _("Save to:") .
+          ' <select name="targetMailbox">';
+          get_extract_to_target_list($imapConnection); 
+    echo    '</select> '.'&nbsp'.
+            '<input type="submit" value="' . _("Extract") . '">'.
+            '</small>'.
+           '</form>'.
+         '</td></tr></table>';
+
+}
+
+function get_extract_to_target_list($imapConnection) {
+    
+    $boxes = sqimap_mailbox_list($imapConnection);
+    for ($i = 0; $i < count($boxes); $i++) {  
+        if (!in_array('noselect', $boxes[$i]['flags'])) {
+            $box = $boxes[$i]['unformatted'];
+            $box2 = str_replace(' ', '&nbsp;', $boxes[$i]['unformatted-disp']);
+            if ( $box2 == 'INBOX' ) {
+                $box2 = _("INBOX");
+            }
+            echo "<option value=\"$box\">$box2</option>\n";
+        }
+    }
 }
 
 
@@ -140,8 +209,10 @@ function viewHeader($header,$color) {
 }
 
 function makeTableEntry($str, $str_name, $color) {
-    $entry = '<tr><td bgcolor="'."$color[0]".'" align right valign top>'."$str_name".'</td><td bgcolor="'."$color[0]".
-            '" valign top colspan=2><b>'."$str".'</b>&nbsp;</td></tr>'."\n";
+    $entry = html_tag( 'tr',
+                     html_tag( 'td', $str_name, 'right', $color[0], 'valign="top"' ) .
+                     html_tag( 'td', '<b>' . $str .'</b>&nbsp;', 'left', $color[0], 'valign="top" colspan="2"' )
+                 );
     return $entry;
 }
 
@@ -240,6 +311,25 @@ function formatRecipientString($recipients, $item ) {
 $imapConnection = sqimap_login($username, $key, $imapServerAddress, $imapPort, 0);
 sqimap_mailbox_select($imapConnection, $mailbox);
 
+$extracted = false;
+if (isset($extract_message) && $extract_message) {
+  $cmd = "FETCH $passed_id BODY[$passed_ent_id]";
+  $read = sqimap_run_command ($imapConnection, $cmd, true, $response, $message);
+  $cnt = count($read);
+  $body = '';
+  $length = 0;
+  for ($i=1;$i<$cnt;$i++) {
+      $length = $length + strlen($read[$i]);
+      $body .= $read[$i];
+  }
+  if (isset($targetMailbox) && $length>0) {
+      sqimap_append ($imapConnection, $targetMailbox, $length);
+      fputs($imapConnection,$body);
+      sqimap_append_done ($imapConnection);
+      $extracted = true;
+  }
+}   
+
 if (isset($showHeaders)) {
   $top_header = sqimap_get_message_header ($imapConnection, $passed_id, $mailbox);
 }
@@ -311,15 +401,18 @@ if (isset($absolute_dl) && $absolute_dl == 'true') {
                  "     " . _("To") . ": " . decodeHeader(getLineOfAddrs($top_header->to)) . "\n".
                  "   " . _("Date") . ": " . getLongDateString($top_header->date) . "\n\n";
         } elseif ($type1 == 'html' && isset($showHeaders)) {
-            echo '<table><tr><th align=right>' . _("Subject").
-                 ':</th><td>' . decodeHeader($top_header->subject).
-                 "</td></tr>\n<tr><th align=right>" . _("From").
-                 ':</th><td>' . decodeHeader($top_header->from).
-                 "</td></tr>\n<tr><th align=right>" . _("To").
-                 ':</th><td>' . decodeHeader(getLineOfAddrs($top_header->to)).
-                 "</td></tr>\n<tr><th align=right>" . _("Date").
-                 ':</th><td>' . getLongDateString($top_header->date).
-                 "</td></tr>\n</table>\n<hr>\n";
+            echo html_tag( 'table',
+                       html_tag( 'tr',
+                           html_tag( 'th', _("Subject") . ':', 'right' ) .
+                           html_tag( 'th', decodeHeader($top_header->subject), 'left' ) . "\n" .
+                           html_tag( 'th', _("From") . ':', 'right' ) .
+                           html_tag( 'th', decodeHeader($top_header->from), 'left' ) . "\n" .
+                           html_tag( 'th', _("To") . ':', 'right' ) .
+                           html_tag( 'th', decodeHeader(getLineOfAddrs($top_header->to)), 'left' ) . "\n" .
+                           html_tag( 'th', _("Date") . ':', 'right' ) .
+                           html_tag( 'th', getLongDateString($top_header->date), 'left' ) . "\n"
+                       )
+                    ) . "\n<hr>\n";
         } 
         echo $body;
         break;
@@ -335,7 +428,7 @@ if (isset($absolute_dl) && $absolute_dl == 'true') {
         if ($type1 == 'plain' || $type1 == 'html') {
             $body = mime_fetch_body($imapConnection, $passed_id, $passed_ent_id);
             $body = decodeBody($body, $header->encoding);
-            viewText($color, $body, $passed_id, $passed_ent_id, $mailbox, $type1, $wrap_at);
+            viewText($color, $body, $passed_id, $passed_ent_id, $mailbox, $type1, $wrap_at, $imapConnection);
         } else {
             DumpHeaders($type0, $type1, $filename, 0);
             $body = mime_fetch_body($imapConnection, $passed_id, $passed_ent_id);
@@ -345,11 +438,11 @@ if (isset($absolute_dl) && $absolute_dl == 'true') {
         break;
     case 'message':
        if ($type1 == 'rfc822' ) {
-           viewMessage($imapConnection, $passed_id, $mailbox, $passed_ent_id, $color, $wrap_at);
+           viewMessage($imapConnection, $passed_id, $mailbox, $passed_ent_id, $color, $wrap_at, $extracted);
        } else {
            $body = mime_fetch_body($imapConnection, $passed_id, $passed_ent_id);
            $body = decodeBody($body, $msgheader->encoding);
-           viewText($color, $body, $passed_id, $passed_ent_id, $mailbox, $type1, $wrap_at);
+           viewText($color, $body, $passed_id, $passed_ent_id, $mailbox, $type1, $wrap_at, $imapConnection);
         }
         break;
     default: