Don't open multiple IMAP connections when building reply headers during send
[squirrelmail.git] / src / compose.php
index 29aa1504a962526504999576402e5764682df6be..ec6eb201ed0b4dfbe9ee0242b18f0d54a1c3c914 100644 (file)
@@ -154,7 +154,7 @@ if ( sqgetGlobalVar('mailtodata', $mailtodata, SQ_GET) ) {
     unset($mailtodata,$mtdata, $trtable);
 }
 
-/* Location (For HTTP 1.1 Header("Location: ...") redirects) */
+/* Location (For HTTP 1.1 header("Location: ...") redirects) */
 $location = get_location();
 /* Identities (fetch only once) */
 $idents = get_identities();
@@ -441,7 +441,7 @@ if ($draft) {
 
         if ($compose_new_win == '1') {
             if ( !isset($pageheader_sent) || !$pageheader_sent ) {
-                Header("Location: $location/compose.php?saved_draft=yes&session=$composesession");
+                header("Location: $location/compose.php?saved_draft=yes&session=$composesession");
             } else {
 //FIXME: DON'T ECHO HTML FROM CORE!
                 echo '   <br><br><div style="text-align: center;"><a href="' . $location
@@ -451,7 +451,7 @@ if ($draft) {
             exit();
         } else {
             if ( !isset($pageheader_sent) || !$pageheader_sent ) {
-                Header("Location: $location/right_main.php?mailbox=" . urlencode($draft_folder) .
+                header("Location: $location/right_main.php?mailbox=" . urlencode($draft_folder) .
                    "&startMessage=1&note=".urlencode($draft_message));
             } else {
 //FIXME: DON'T ECHO HTML FROM CORE!
@@ -522,7 +522,8 @@ if ($send) {
         // NOTE: this hook changed in 1.5.2 from sending $Result and
         //       $composeMessage as args #2 and #3 to being in an array
         //       under arg #2
-        do_hook('compose_send_after', $temp=array(&$Result, &$composeMessage, &$mail_sent));
+        $temp = array(&$Result, &$composeMessage, &$mail_sent);
+        do_hook('compose_send_after', $temp);
         if (! $Result) {
             showInputForm($session);
             exit();
@@ -549,7 +550,7 @@ if ($send) {
 
         if ($compose_new_win == '1') {
             if ( !isset($pageheader_sent) || !$pageheader_sent ) {
-                Header("Location: $location/compose.php?mail_sent=$mail_sent");
+                header("Location: $location/compose.php?mail_sent=$mail_sent");
             } else {
 //FIXME: DON'T ECHO HTML FROM CORE!
                 echo '   <br><br><div style="text-align: center;"><a href="' . $location
@@ -559,7 +560,7 @@ if ($send) {
             exit();
         } else {
             if ( !isset($pageheader_sent) || !$pageheader_sent ) {
-                Header("Location: $location/right_main.php?mailbox=$urlMailbox".
+                header("Location: $location/right_main.php?mailbox=$urlMailbox".
                     "&startMessage=$startMessage&mail_sent=$mail_sent");
             } else {
 //FIXME: DON'T ECHO HTML FROM CORE!
@@ -877,6 +878,7 @@ function newMail ($mailbox='', $passed_id='', $passed_ent_id='', $action='', $se
                 $request_dr = $mdn_user_support && !empty($orig_header->drnt) ? '1' : '0';
 
                 /* remember the references and in-reply-to headers in case of an reply */
+//FIXME: it would be better to fiddle with headers inside of the message object or possibly when delivering the message to its destination (drafts folder?); is this possible?
                 $composeMessage->rfc822_header->more_headers['References'] = $orig_header->references;
                 $composeMessage->rfc822_header->more_headers['In-Reply-To'] = $orig_header->in_reply_to;
                 // rewrap the body to clean up quotations and line lengths
@@ -1002,7 +1004,7 @@ function getAttachments($message, &$composeMessage, $passed_id, $entities, $imap
                         if ($filename == "") {
                             $filename = "untitled-".$message->entity_id;
                         }
-                        $filename .= '.msg';
+                        $filename .= '.eml';
                     } else {
                         $filename = $message->getFilename();
                     }
@@ -1065,7 +1067,7 @@ function getMessage_RFC822_Attachment($message, $composeMessage, $passed_id,
         $fp = fopen($hashed_attachment_dir . '/' . $localfilename, 'wb');
         fwrite ($fp, $body);
         fclose($fp);
-        $composeMessage->initAttachment('message/rfc822',$subject.'.msg',
+        $composeMessage->initAttachment('message/rfc822',$subject.'.eml',
                 $localfilename);
     }
     return $composeMessage;
@@ -1467,8 +1469,18 @@ function getByteSize($ini_size) {
  * In the future the responsible backend should be automaticly loaded
  * and conf.pl should show a list of available backends.
  * The message also should be constructed by the message class.
+ *
+ * @param object $composeMessage The message being sent.  Please note
+ *                               that it is passed by reference and
+ *                               will be returned modified, with additional
+ *                               headers, such as Message-ID, Date, In-Reply-To,
+ *                               References, and so forth.
+ *
+ * @return boolean FALSE if delivery failed, or some non-FALSE value
+ *                 upon success.
+ *
  */
-function deliverMessage($composeMessage, $draft=false) {
+function deliverMessage(&$composeMessage, $draft=false) {
     global $send_to, $send_to_cc, $send_to_bcc, $mailprio, $subject, $body,
         $username, $identity, $idents, $data_dir,
         $request_mdn, $request_dr, $default_charset, $useSendmail,
@@ -1511,7 +1523,8 @@ function deliverMessage($composeMessage, $draft=false) {
     }
 
     /* Receipt: On Delivery */
-    if (isset($request_dr) && $request_dr) {
+    if (!empty($request_dr)) {
+//FIXME: it would be better to fiddle with headers inside of the message object or possibly when delivering the message to its destination; is this possible?
         $rfc822_header->more_headers['Return-Receipt-To'] = $from->mailbox.'@'.$from->domain;
     } elseif (isset($rfc822_header->more_headers['Return-Receipt-To'])) {
         unset($rfc822_header->more_headers['Return-Receipt-To']);
@@ -1588,20 +1601,16 @@ function deliverMessage($composeMessage, $draft=false) {
         $stream = $deliver->initStream($composeMessage,$sendmail_path);
     } elseif ($draft) {
         global $draft_folder;
-        require_once(SM_PATH . 'class/deliver/Deliver_IMAP.class.php');
         $imap_stream = sqimap_login($username, false, $imapServerAddress,
                 $imapPort, 0);
         if (sqimap_mailbox_exists ($imap_stream, $draft_folder)) {
             require_once(SM_PATH . 'class/deliver/Deliver_IMAP.class.php');
             $imap_deliver = new Deliver_IMAP();
-            $length = $imap_deliver->mail($composeMessage, 0, $reply_id, $reply_ent_id);
-            sqimap_append ($imap_stream, $draft_folder, $length);
-            $imap_deliver->mail($composeMessage, $imap_stream, $reply_id, $reply_ent_id);
-            sqimap_append_done ($imap_stream, $draft_folder);
+            $success = $imap_deliver->mail($composeMessage, $imap_stream, $reply_id, $reply_ent_id, $draft_folder);
             sqimap_logout($imap_stream);
             unset ($imap_deliver);
             $composeMessage->purgeAttachments();
-            return $length;
+            return $success;
         } else {
             $msg  = '<br />'.sprintf(_("Error: Draft folder %s does not exist."), htmlspecialchars($draft_folder));
             plain_error_message($msg);
@@ -1610,7 +1619,7 @@ function deliverMessage($composeMessage, $draft=false) {
     }
     $success = false;
     if ($stream) {
-        $length = $deliver->mail($composeMessage, $stream, $reply_id, $reply_ent_id);
+        $deliver->mail($composeMessage, $stream, $reply_id, $reply_ent_id);
         $success = $deliver->finalizeStream($stream);
     }
     if (!$success) {
@@ -1627,48 +1636,13 @@ function deliverMessage($composeMessage, $draft=false) {
         plain_error_message($msg);
     } else {
         unset ($deliver);
-        $move_to_sent = getPref($data_dir,$username,'move_to_sent');
         $imap_stream = sqimap_login($username, false, $imapServerAddress, $imapPort, 0);
 
-        /* Move to sent code */
-        if (isset($default_move_to_sent) && ($default_move_to_sent != 0)) {
-            $svr_allow_sent = true;
-        } else {
-            $svr_allow_sent = false;
-        }
 
-        if (isset($sent_folder) && (($sent_folder != '') || ($sent_folder != 'none'))
-                && sqimap_mailbox_exists( $imap_stream, $sent_folder)) {
-            $fld_sent = true;
-        } else {
-            $fld_sent = false;
-        }
+        // mark as replied or forwarded if applicable
+        //
+        global $what, $iAccount, $startMessage, $passed_id, $mailbox;
 
-        if ((isset($move_to_sent) && ($move_to_sent != 0)) || (!isset($move_to_sent))) {
-            $lcl_allow_sent = true;
-        } else {
-            $lcl_allow_sent = false;
-        }
-
-        global $passed_id, $mailbox;
-        if (($fld_sent && $svr_allow_sent && !$lcl_allow_sent) || ($fld_sent && $lcl_allow_sent)) {
-            if ($action == 'reply' || $action == 'reply_all') {
-                $save_reply_with_orig=getPref($data_dir,$username,'save_reply_with_orig');
-                if ($save_reply_with_orig) {
-                    $sent_folder = $mailbox;
-                }
-            }
-            sqimap_append ($imap_stream, $sent_folder, $length);
-            require_once(SM_PATH . 'class/deliver/Deliver_IMAP.class.php');
-            $imap_deliver = new Deliver_IMAP();
-            $imap_deliver->mail($composeMessage, $imap_stream, $reply_id, $reply_ent_id);
-            sqimap_append_done ($imap_stream, $sent_folder);
-            unset ($imap_deliver);
-        }
-
-        global $what, $iAccount, $startMessage;
-
-        $composeMessage->purgeAttachments();
         if ($action=='reply' || $action=='reply_all' || $action=='forward' || $action=='forward_as_attachment') {
             require(SM_PATH . 'functions/mailbox_display.php');
             $aMailbox = sqm_api_mailbox_select($imap_stream, $iAccount, $mailbox,array('setindex' => $what, 'offset' => $startMessage),array());
@@ -1713,8 +1687,50 @@ function deliverMessage($composeMessage, $draft=false) {
                 sqsession_register($mailbox_cache,'mailbox_cache');
             }
 
-            sqimap_logout($imap_stream);
         }
+
+
+        // move to sent folder
+        //
+        $move_to_sent = getPref($data_dir,$username,'move_to_sent');
+        if (isset($default_move_to_sent) && ($default_move_to_sent != 0)) {
+            $svr_allow_sent = true;
+        } else {
+            $svr_allow_sent = false;
+        }
+
+        if (isset($sent_folder) && (($sent_folder != '') || ($sent_folder != 'none'))
+                && sqimap_mailbox_exists( $imap_stream, $sent_folder)) {
+            $fld_sent = true;
+        } else {
+            $fld_sent = false;
+        }
+
+        if ((isset($move_to_sent) && ($move_to_sent != 0)) || (!isset($move_to_sent))) {
+            $lcl_allow_sent = true;
+        } else {
+            $lcl_allow_sent = false;
+        }
+
+        if (($fld_sent && $svr_allow_sent && !$lcl_allow_sent) || ($fld_sent && $lcl_allow_sent)) {
+            if ($action == 'reply' || $action == 'reply_all') {
+                $save_reply_with_orig=getPref($data_dir,$username,'save_reply_with_orig');
+                if ($save_reply_with_orig) {
+                    $sent_folder = $mailbox;
+                }
+            }
+            require_once(SM_PATH . 'class/deliver/Deliver_IMAP.class.php');
+            $imap_deliver = new Deliver_IMAP();
+            $imap_deliver->mail($composeMessage, $imap_stream, $reply_id, $reply_ent_id, $sent_folder, $imap_stream);
+            unset ($imap_deliver);
+        }
+
+
+        // final cleanup
+        //
+        $composeMessage->purgeAttachments();
+        sqimap_logout($imap_stream);
+
     }
     return $success;
 }