r2l by Yoav
[squirrelmail.git] / src / compose.php
index 22935f9554cc715f87bce7fda6f304399d05dc52..5c6a807b3473298ebad746bb33877933559d6069 100644 (file)
@@ -24,59 +24,11 @@ require_once('../functions/mime.php');
 require_once('../functions/smtp.php');
 require_once('../functions/display_messages.php');
 require_once('../functions/plugin.php');
+require_once('../functions/html.php');
 
 /* --------------------- Specific Functions ------------------------------ */
 
-/**
- * Wraps text at $wrap characters
- *
- * Has a problem with special HTML characters, so call this before
- * you do character translation.
- *
- * Specifically, &#039 comes up as 5 characters instead of 1.
- * This should not add newlines to the end of lines.
- */
-function sqWordWrap(&$line, $wrap) {
-    ereg("^([\t >]*)([^\t >].*)?$", $line, $regs);
-    $beginning_spaces = $regs[1];
-    if (isset($regs[2])) {
-        $words = explode(' ', $regs[2]);
-    } else {
-        $words = '';
-    }
-    
-    $i = 0;
-    $line = $beginning_spaces;
-    
-    while ($i < count($words)) {
-        /* Force one word to be on a line (minimum) */
-        $line .= $words[$i];
-        $line_len = strlen($beginning_spaces) + strlen($words[$i]) + 2;
-        if (isset($words[$i + 1]))
-            $line_len += strlen($words[$i + 1]);
-        $i ++;
-        
-        /* Add more words (as long as they fit) */
-        while ($line_len < $wrap && $i < count($words)) {
-            $line .= ' ' . $words[$i];
-            $i++;
-            if (isset($words[$i]))
-                $line_len += strlen($words[$i]) + 1;
-            else
-                $line_len += 1;
-        }
-        
-        /* Skip spaces if they are the first thing on a continued line */
-        while (!isset($words[$i]) && $i < count($words)) {
-            $i ++;
-        }
-        
-        /* Go to the next line if we have more to process */
-        if ($i < count($words)) {
-            $line .= "\n" . $beginning_spaces;
-        }
-    }
-}
+
 
 /**
  * Does the opposite of sqWordWrap()
@@ -119,10 +71,10 @@ if (!isset($composesession)) {
     session_register('composesession');
 }
 
-if (!isset($session)) {
+if (!isset($session) || (isset($newmessage) && $newmessage)) {
     $session = "$composesession" +1; 
     $composesession = $session;        
-}    
+}     
 
 if (!isset($mailbox) || $mailbox == '' || ($mailbox == 'None')) {
     $mailbox = 'INBOX';
@@ -136,7 +88,10 @@ if (isset($draft)) {
     if (! isset($MDN)) {
         $MDN = 'False';
     }
-    if (!saveMessageAsDraft($send_to, $send_to_cc, $send_to_bcc, $subject, $body, $reply_id, $MDN, $session)) {
+    if (! isset($mailprio)) {
+        $mailprio = '';
+    }
+    if (!saveMessageAsDraft($send_to, $send_to_cc, $send_to_bcc, $subject, $body, $reply_id, $mailprio, $session)) {
         showInputForm($session);
         exit();
     } else {
@@ -188,8 +143,7 @@ if (isset($send)) {
         /*
          * Rewrap $body so that no line is bigger than $editor_size
          * This should only really kick in the sqWordWrap function
-         * if the browser doesn't support "HARD" as the wrap type
-         * Or, in Opera's case, something goes wrong.
+         * if the browser doesn't support "VIRTUAL" as the wrap type.
          */
         $body = explode("\n", $body);
         $newBody = '';
@@ -200,8 +154,8 @@ if (isset($send)) {
             if (strlen($line) <= $editor_size + 1) {
                 $newBody .= $line . "\n";
             } else {
-                sqWordWrap($line, $editor_size) . "\n";
-                $newBody .= $line;
+                sqWordWrap($line, $editor_size);
+                $newBody .= $line . "\n";
             }
         }
         $body = $newBody;
@@ -226,7 +180,7 @@ if (isset($send)) {
             exit();
         }
         if ($compose_new_win == '1') {
-            Header("Location: compose.php?mail_sent=yes&session=$composesession");
+            Header("Location: compose.php?mail_sent=yes");
         }
         else {
             Header("Location: right_main.php?mailbox=$urlMailbox&sort=$sort".
@@ -391,6 +345,10 @@ elseif (isset($sigappend)) {
         getAttachments(0, $session);
     }
 
+    if (isset($passed_id) && $passed_id && isset($ent_num) && $ent_num) {
+        getAttachments(0, $session);
+    }
+
     newMail($session);
     showInputForm($session);
     sqimap_logout($imapConnection);
@@ -406,18 +364,24 @@ exit();
 function newMail () {
     global $forward_id, $imapConnection, $msg, $ent_num, $body_ary, $body,
            $reply_id, $send_to, $send_to_cc, $mailbox, $send_to_bcc, $editor_size,
-           $draft_id, $use_signature, $composesession, $forward_cc;
+           $draft_id, $use_signature, $composesession, $forward_cc, $passed_id;
 
     $send_to = decodeHeader($send_to, false);
     $send_to_cc = decodeHeader($send_to_cc, false);
     $send_to_bcc = decodeHeader($send_to_bcc, false);
+    $send_to = str_replace('&lt;', '<', str_replace('&gt;', '>', str_replace('&amp;', '&', str_replace('&quot;', '"', $send_to))));
+    $send_to_cc = str_replace('&lt;', '<', str_replace('&gt;', '>', str_replace('&amp;', '&', str_replace('&quot;', '"', $send_to_cc))));
+    $send_to_bcc = str_replace('&lt;', '<', str_replace('&gt;', '>', str_replace('&amp;', '&', str_replace('&quot;', '"', $send_to_bcc))));
 
     if ($forward_id) {
         $id = $forward_id;
     } elseif ($reply_id) {
         $id = $reply_id;
+    } elseif ($passed_id) {
+        $id = $passed_id;
     }
 
+
     if ($draft_id){
         $id = $draft_id;
         $use_signature = FALSE;
@@ -427,29 +391,49 @@ function newMail () {
         sqimap_mailbox_select($imapConnection, $mailbox);
         $message = sqimap_get_message($imapConnection, $id, $mailbox);
         $orig_header = $message->header;
+       $body = '';
         if ($ent_num) {
-            $message = getEntity($message, $ent_num);
-        }
-        if ($message->header->type0 == 'text' ||
-            $message->header->type1 == 'message') {
-            if ($ent_num) {
-                $body = decodeBody(
-                    mime_fetch_body($imapConnection, $id, $ent_num),
-                    $message->header->encoding);
-            } else {
-                $body = decodeBody(
-                    mime_fetch_body($imapConnection, $id, 1),
-                    $message->header->encoding);
-            }
-        } else {
-            $body = '';
-        }
+           $ent_ar = preg_split('/_/',$ent_num);
+           foreach($ent_ar as $ent_num) {
+               $message = getEntity($message, $ent_num);
+               if ($message->header->type0 == 'text' ||
+                   $message->header->type1 == 'message') {
+                   $bodypart = decodeBody(
+                       mime_fetch_body($imapConnection, $id, $ent_num),
+                           $message->header->encoding);
+                       if ($message->header->type1 == 'html') {
+                           $bodypart = strip_tags($bodypart);
+                       }
+                       $body .= $bodypart;
+               }
+           }
+        } else if ($message->header->type0 == 'text' ||
+                   $message->header->type1 == 'message') {
+                       $body .= decodeBody(
+                       mime_fetch_body($imapConnection, $id, 1),
+                       $message->header->encoding);
+                       if ($message->header->type1 == 'html') {
+                           $body = strip_tags($body);
+                       }
+                       
+        } 
 
-        if ($message->header->type1 == 'html') {
-            $body = strip_tags($body);
+        sqUnWordWrap($body);
+        
+        /* this corrects some wrapping/quoting problems on replies */
+        if ($reply_id) {
+            $rewrap_body = explode("\n", $body);
+            for ($i=0;$i<count($rewrap_body);$i++) {
+                sqWordWrap($rewrap_body[$i], ($editor_size - 2));
+                if (preg_match("/^(>+)/", $rewrap_body[$i], $matches)) {
+                    $gt = $matches[1];
+                    $rewrap_body[$i] = str_replace("\n", "\n$gt ", $rewrap_body[$i]);
+                }
+                $rewrap_body[$i] .= "\n";
+            }
+            $body = implode("", $rewrap_body);
         }
 
-        sqUnWordWrap($body);
         $body_ary = explode("\n", $body);
         $i = count($body_ary) - 1;
         while ($i >= 0 && ereg("^[>\\s]*$", $body_ary[$i])) {
@@ -459,14 +443,14 @@ function newMail () {
         $body = '';
         for ($i=0; isset($body_ary[$i]); $i++) {
             if ($reply_id) {
-                if (ereg('^[ >]+', $body_ary[$i])) {
+                if (preg_match("/^(>){1,}/", $body_ary[$i])) {
                     $body_ary[$i] = '>' . $body_ary[$i];
                 } else {
                     $body_ary[$i] = '> ' . $body_ary[$i];
                 }
             }
-            if (!$draft_id) {
-                sqWordWrap($body_ary[$i], $editor_size - 1);
+            if ($draft_id) {
+                sqWordWrap($body_ary[$i], $editor_size );
             }
             $body .= $body_ary[$i] . "\n";
             unset($body_ary[$i]);
@@ -539,12 +523,14 @@ function newMail () {
 
 function getAttachments($message, $session) {
     global $mailbox, $attachments, $attachment_dir, $imapConnection,
-           $ent_num, $forward_id, $draft_id, $username;
+           $ent_num, $forward_id, $draft_id, $username, $passed_id;
 
     if (isset($draft_id)) {
         $id = $draft_id;
-    } else {
+    } else if (isset($forward_id)) {
         $id = $forward_id;
+    } else {
+        $id = $passed_id;
     }
 
     if (!$message) {
@@ -601,8 +587,9 @@ function showInputForm ($session) {
            $from_htmladdr_search, $location_of_buttons, $attachment_dir,
            $username, $data_dir, $identity, $draft_id, $delete_draft,
            $mailprio, $default_use_mdn, $mdn_user_support, $compose_new_win,
-           $saved_draft, $mail_sent, $sig_first;
+           $saved_draft, $mail_sent, $sig_first, $edit_as_new;
 
+    $file_uploads = ini_get('file_uploads');
     $subject = decodeHeader($subject, false);
     $reply_subj = decodeHeader($reply_subj, false);
     $forward_subj = decodeHeader($forward_subj, false);
@@ -618,14 +605,16 @@ function showInputForm ($session) {
              '// --></SCRIPT>' . "\n\n";
     }
 
-    echo "\n" . '<FORM name=compose action="compose.php" METHOD=POST ' .
-         'ENCTYPE="multipart/form-data"';
+    echo "\n" . '<FORM name=compose action="compose.php" METHOD=POST ';
+    if ($file_uploads) {
+        echo 'ENCTYPE="multipart/form-data"';
+    }
     do_hook("compose_form");
 
     
     echo ">\n";
 
-    if (isset($draft_id)) {
+    if (isset($draft_id) && !$edit_as_new) {
         echo '<input type="hidden" name="delete_draft" value="' . $draft_id . "\">\n";
     }
     if (isset($delete_draft)) {
@@ -641,9 +630,15 @@ function showInputForm ($session) {
     if ($mail_sent == 'yes') {
         echo '<BR><CENTER><B>'. _("Your Message has been sent").'</CENTER></B>';
     }
-    echo '<TABLE WIDTH="100%" ALIGN=center CELLSPACING=0 BORDER=0>' . "\n";
+    echo html_tag( 'table', '', 'center', '', 'width="100%" cellspacing="0" border="0"' ) . "\n";
     if ($compose_new_win == '1') {
-        echo '   <TR><TD></TD><TD ALIGN="RIGHT"><INPUT TYPE="BUTTON" NAME="Close" onClick="return self.close()" VALUE='._("Close").'></TD></TR>'."\n";
+        echo html_tag( 'table', 'center', $color[0], 'width="100%" border="0"' ) .
+                        html_tag( 'tr',
+                            html_tag( 'td' ) . 
+                            html_tag( 'td',
+                                      '<INPUT TYPE="BUTTON" NAME="Close" onClick="return self.close()" VALUE='._("Close").'>',
+                            'right' )
+                         );
     }
     if ($location_of_buttons == 'top') {
         showComposeButtonRow();
@@ -651,11 +646,9 @@ function showInputForm ($session) {
 
     $idents = getPref($data_dir, $username, 'identities', 0);
     if ($idents > 1) {
-        echo '   <TR>' . "\n" .
-             '      <TD BGCOLOR="' . $color[4] . '" WIDTH="10%" ALIGN=RIGHT>' .
-             "\n" .
-             _("From:") .
-             '      </TD><TD BGCOLOR="' . $color[4] . '" WIDTH="90%">' . "\n" .
+        echo html_tag( 'tr' ) . "\n" .
+            html_tag( 'td', "\n" . _("From:"), 'right', $color[4], 'width="10%"' ) .
+            html_tag( 'td', '', 'left', $color[4], 'width="90%"' ) .
              '<select name=identity>' . "\n" .
              '<option value=default>' .
              htmlspecialchars(getPref($data_dir, $username, 'full_name'));
@@ -677,44 +670,38 @@ function showInputForm ($session) {
             echo '</option>';
         }
         echo '</select>' . "\n" .
-             '      </TD>' . "\n" .
-             '   </TR>' . "\n";
-    }
-    echo '   <TR>' . "\n" .
-         '      <TD BGCOLOR="' . $color[4] . '" WIDTH="10%" ALIGN=RIGHT>' . "\n" .
-         _("To:") .
-         '      </TD><TD BGCOLOR="' . $color[4] . '" WIDTH="90%">' . "\n" .
-         '         <INPUT TYPE=text NAME="send_to" VALUE="' .
-         htmlspecialchars($send_to) . '" SIZE=60><BR>' . "\n" .
-         '      </TD>' . "\n" .
-         '   </TR>' . "\n" .
-         '   <TR>' . "\n" .
-         '      <TD BGCOLOR="' . $color[4] . '" ALIGN=RIGHT>' . "\n" .
-         _("CC:") .
-         '      </TD><TD BGCOLOR="' . $color[4] . '" ALIGN=LEFT>' . "\n" .
-         '         <INPUT TYPE=text NAME="send_to_cc" SIZE=60 VALUE="' .
-         htmlspecialchars($send_to_cc) . '"><BR>' . "\n" .
-         '      </TD>' . "\n" .
-         '   </TR>' . "\n" .
-         '   <TR>' . "\n" .
-         '      <TD BGCOLOR="' . $color[4] . '" ALIGN=RIGHT>' . "\n" .
-         _("BCC:") .
-         '      </TD><TD BGCOLOR="' . $color[4] . '" ALIGN=LEFT>' . "\n" .
-         '         <INPUT TYPE=text NAME="send_to_bcc" VALUE="' .
-         htmlspecialchars($send_to_bcc) . '" SIZE=60><BR>' . "\n" .
-         '</TD></TR>' . "\n" .
-         '   <TR>' . "\n" .
-         '      <TD BGCOLOR="' . $color[4] . '" ALIGN=RIGHT>' . "\n" .
-         _("Subject:") .
-         '      </TD><TD BGCOLOR="' . $color[4] . '" ALIGN=LEFT>' . "\n";
+             '      </td>' . "\n" .
+             '   </tr>' . "\n";
+    }
+    echo html_tag( 'tr',
+                html_tag( 'td', "\n" . _("To:"), 'right', $color[4], 'width="10%"' ) .
+                html_tag( 'td', 
+                          '         <INPUT TYPE=text NAME="send_to" VALUE="' .
+                          htmlspecialchars($send_to) . '" SIZE=60><BR>' . "\n",
+                'left', $color[4], 'width="90%"' )
+            ) . "\n" .
+           html_tag( 'tr',
+                html_tag( 'td', "\n" . _("CC:"), 'right', $color[4], 'width="10%"' ) .
+                html_tag( 'td', 
+                          '         <INPUT TYPE=text NAME="send_to_cc" VALUE="' .
+                          htmlspecialchars($send_to_cc) . '" SIZE=60><BR>' . "\n",
+                'left', $color[4], 'width="90%"' )
+            ) . "\n" .
+           html_tag( 'tr',
+                html_tag( 'td', "\n" . _("BCC:"), 'right', $color[4], 'width="10%"' ) .
+                html_tag( 'td', 
+                          '         <INPUT TYPE=text NAME="send_to_bcc" VALUE="' .
+                          htmlspecialchars($send_to_bcc) . '" SIZE=60><BR>' . "\n",
+                'left', $color[4], 'width="90%"' )
+            ) . "\n";
+
     if ($reply_subj) {
-        $reply_subj = str_replace('"', "'", $reply_subj);
+        $reply_subj = str_replace('"', '\'', $reply_subj);
         $reply_subj = trim($reply_subj);
         if (substr(strtolower($reply_subj), 0, 3) != 're:') {
             $reply_subj = 'Re: ' . $reply_subj;
         }
-        echo '         <INPUT TYPE=text NAME=subject SIZE=60 VALUE="' .
-             htmlspecialchars($reply_subj) . '">';
+        $subject = $reply_subj;
     }
     elseif ($forward_subj) {
         $forward_subj = trim($forward_subj);
@@ -723,22 +710,34 @@ function showInputForm ($session) {
             (substr(strtolower($forward_subj), 0, 6) != '[ fwd:')) {
             $forward_subj = '[Fwd: ' . $forward_subj . ']';
         }
-        echo '         <INPUT TYPE=text NAME=subject SIZE=60 VALUE="' .
-             htmlspecialchars($forward_subj) . '">';
+        $subject = $forward_subj;
     } else {
-        echo '         <INPUT TYPE=text NAME=subject SIZE=60 VALUE="' .
-             htmlspecialchars($subject) . '">';
+        $subject = $subject;
     }
-    echo '</td></tr>' . "\n\n";
+
+    echo html_tag( 'tr',
+                html_tag( 'td', "\n" . _("Subject:"), 'right', $color[4], 'width="10%"' ) .
+                html_tag( 'td', 
+                          '         <INPUT TYPE=text NAME="subject" VALUE="' .
+                          htmlspecialchars($subject) . '" SIZE=60><BR>' . "\n",
+                'left', $color[4], 'width="90%"' )
+            ) . "\n";
 
     if ($location_of_buttons == 'between') {
         showComposeButtonRow();
     }
 
-    echo '   <TR>' . "\n" .
-         '      <TD BGCOLOR="' . $color[4] . '" COLSPAN=2>' . "\n" .
-         '         &nbsp;&nbsp;<TEXTAREA NAME=body ROWS=20 COLS="' .
-         $editor_size . '" WRAP=HARD>';
+    echo html_tag( 'tr' ) . "\n";
+    if ($compose_new_win == '1') {
+        echo html_tag( 'td', '', 'left', $color[0], 'colspan="2"' ) . "\n" .
+             '         <textarea name="body" rows="20" cols="' .
+             $editor_size . '" wrap="virtual">';
+    }
+    else {
+        echo html_tag( 'td', '', 'left', $color[4], 'colspan="2"' ) . "\n" .
+            '         &nbsp;&nbsp;<textarea name="body" rows="20" cols="' .
+            $editor_size . '" wrap="virtual">';
+    }
     if ($use_signature == true && $newmail == true && !isset($from_htmladdr_search)) {
         if ($sig_first == '1') {
             echo "\n\n".($prefix_sig==true? "-- \n":'').htmlspecialchars($signature);
@@ -752,50 +751,52 @@ function showInputForm ($session) {
     else {
        echo htmlspecialchars($body);
     }
-    echo '</TEXTAREA><BR>' . "\n" .
-         '      </TD>' . "\n" .
-         '   </TR>' . "\n";
+    echo '</textarea><br>' . "\n" .
+         '      </td>' . "\n" .
+         '   </tr>' . "\n";
 
     if ($location_of_buttons == 'bottom') {
         showComposeButtonRow();
     } else {
-        echo '   <TR><TD COLSPAN=2 ALIGN=LEFT>';
-        echo ' &nbsp; <INPUT TYPE=SUBMIT NAME=send VALUE="' . _("Send") . '"></TD></TR>' . "\n";
+        echo html_tag( 'tr',
+                    html_tag( 'td', ' &nbsp; <INPUT TYPE=SUBMIT NAME=send VALUE="' . _("Send") . '">', 'left', '', 'colspan="2"' ) ."\n"
+                );
     }
 
     /* This code is for attachments */
-    echo '   <TR>' . "\n" .
-         '     <TD VALIGN=MIDDLE ALIGN=RIGHT>' . "\n" .
-                _("Attach:") .
-         '      </TD>' . "\n" .
-         '      <TD VALIGN=MIDDLE ALIGN=LEFT>' . "\n" .
-         '      <INPUT NAME="attachfile" SIZE=48 TYPE="file">' . "\n" .
-         '      &nbsp;&nbsp;<input type="submit" name="attach"' .
-         ' value="' . _("Add") .'">' . "\n" .
-         '     </TD>' . "\n" .
-         '   </TR>' . "\n";
-
-    if (count($attachments)) {
-        $hashed_attachment_dir = getHashedDir($username, $attachment_dir);
-        echo '<tr><td bgcolor="' . $color[0] . '" align=right>' . "\n" .
-             '&nbsp;' .
-             '</td><td align=left bgcolor="' . $color[0] . '">';
-        foreach ($attachments as $key => $info) {
-           if ($info['session'] == $session) { 
-               $attached_file = "$hashed_attachment_dir/$info[localfilename]";
-               echo '<input type="checkbox" name="delete[]" value="' . $key . "\">\n" .
-                    $info['remotefilename'] . ' - ' . $info['type'] . ' (' .
-                    show_readable_size(filesize($attached_file)) . ")<br>\n";
-           }
-        }
+    if ($file_uploads) {
+        echo html_tag( 'tr',
+                    html_tag( 'td', "\n" . _("Attach:") ."\n", 'right', '', 'valign="middle"' ) ."\n" .
+                    html_tag( 'td', "\n" . '<INPUT NAME="attachfile" SIZE=48 TYPE="file">' .
+                             '&nbsp;&nbsp;<input type="submit" name="attach"' .
+                             ' value="' . _("Add") .'">' . "\n" ,
+                    'left', '', 'valign="middle"' ) ."\n"
+                );
+
+        if (count($attachments)) {
+            $hashed_attachment_dir = getHashedDir($username, $attachment_dir);
+            echo html_tag( 'tr' ) .
+                      html_tag( 'td', "\n" . '&nbsp;', 'right', $color[0] ) .
+                      html_tag( 'td', '', 'left', $color[0] );
+            foreach ($attachments as $key => $info) {
+                if ($info['session'] == $session) {
+                    $attached_file = "$hashed_attachment_dir/$info[localfilename]";
+                    echo '<input type="checkbox" name="delete[]" value="' . $key . "\">\n" .
+                            $info['remotefilename'] . ' - ' . $info['type'] . ' (' .
+                            show_readable_size( filesize( $attached_file ) ) . ")<br>\n";
+                }
+            }
 
-        echo '<input type="submit" name="do_delete" value="' .
-             _("Delete selected attachments") . "\">\n" .
-             '</td></tr>';
+            echo '<input type="submit" name="do_delete" value="' .
+                 _("Delete selected attachments") . "\">\n" .
+                 '</td></tr>';
+        }
+        /* End of attachment code */
     }
-    /* End of attachment code */
-
-    echo '</TABLE>' . "\n";
+    if ($compose_new_win == '1') {
+        echo '</table>'."\n";
+    }
+    echo '</table>' . "\n";
     if ($reply_id) {
         echo '<input type=hidden name=reply_id value=' . $reply_id . ">\n";
     }
@@ -810,9 +811,12 @@ function showInputForm ($session) {
 function showComposeButtonRow() {
     global $use_javascript_addr_book, $save_as_draft,
         $default_use_priority, $mailprio, $default_use_mdn,
+       $request_mdn, $request_dr,
         $data_dir, $username;
 
-    echo "  <TR><TD>\n</TD><TD>\n";
+    echo html_tag( 'tr' ) .
+                html_tag( 'td', "\n", 'left' ) .
+                html_tag( 'td', '', 'left' );
     if ($default_use_priority) {
         if(!isset($mailprio)) {
             $mailprio = "3";
@@ -827,12 +831,17 @@ function showComposeButtonRow() {
     if ($default_use_mdn) {
         if ($mdn_user_support) {
             echo "\n\t". _("Receipt") .': '.
-            '<input type="checkbox" name="request_mdn" value=1>'. _("On read").
-            ' <input type="checkbox" name="request_dr" value=1>'. _("On Delivery");
+            '<input type="checkbox" name="request_mdn" value=1'.
+               ($request_mdn=='1'?' checked':'') .'>'. _("On read").
+            ' <input type="checkbox" name="request_dr" value=1'.
+               ($request_dr=='1'?' checked':'') .'>'. _("On Delivery");
         }
     }
 
-    echo "   <TR><td>\n   </td><td>\n";
+    echo "   </td></tr>\n" .
+        html_tag( 'tr' ) .
+        html_tag( 'td', "\n", 'left' ) .
+        html_tag( 'td', '', 'left' );
     echo "\n    <INPUT TYPE=SUBMIT NAME=\"sigappend\" VALUE=\"". _("Signature") . "\">\n";
     if ($use_javascript_addr_book) {
         echo "      <SCRIPT LANGUAGE=JavaScript><!--\n document.write(\"".
@@ -854,7 +863,7 @@ function showComposeButtonRow() {
 
     do_hook('compose_button_row');
 
-    echo "   </TD></TR>\n\n";
+    echo "   </td></tr>\n\n";
 }
 
 function checkInput ($show) {
@@ -913,15 +922,18 @@ function ClearAttachments($session)
     $hashed_attachment_dir = getHashedDir($username, $attachment_dir);
 
     $rem_attachments = array();
-    foreach ($attachments as $info) {
-       if ($info['session'] == $session) {
-           $attached_file = "$hashed_attachment_dir/$info[localfilename]";
-           if (file_exists($attached_file)) {
-               unlink($attached_file);
-           }
-       } else {
-           $rem_attachments[] = $info;
-       }
+    if (is_array($attachments)) {
+        foreach ($attachments as $info) {
+               if ($info['session'] == $session) {
+               $attached_file = "$hashed_attachment_dir/$info[localfilename]";
+               if (file_exists($attached_file)) {
+                       unlink($attached_file);
+               }
+               } 
+            else {
+                   $rem_attachments[] = $info;
+               }
+        }
     }
     $attachments = $rem_attachments;
 }