set correct entity_id
[squirrelmail.git] / src / compose.php
index 4be2a18eb8f0e613cb3b5e814b2e57f35f9f50e8..35cbc53da803ea2c7eda8b86cab9c82f76ae64fe 100644 (file)
@@ -100,6 +100,47 @@ function getforwardHeader($orig_header) {
 }
 /* ----------------------------------------------------------------------- */
 
+/*
+ * If the session is expired during a post this restores the compose session 
+ * vars.
+ */
+//$session_expired = false; 
+if (session_is_registered('session_expired_post')) {
+   global $session_expired_post, $session_expired;
+   /* 
+    * extra check for username so we don't display previous post data from
+    * another user during this session.
+    */
+   if ($session_expired_post['username'] != $username) {
+      session_unregister('session_expired_post');
+      session_unregister('session_expired');      
+   } else {
+      foreach ($session_expired_post as $postvar => $val) {
+         if (isset($val)) {
+            $$postvar = $val;
+         } else {
+            $$postvar = '';
+         }
+      }
+      if (isset($send)) {
+         unset($send);
+      }
+      $session_expired = true;
+   }
+   session_unregister('session_expired_post');
+   session_unregister('session_expired');
+   if (!isset($mailbox)) {
+      $mailbox = '';
+   }
+   if ($compose_new_win == '1') {
+      compose_Header($color, $mailbox);
+   } else {
+      displayPageHeader($color, $mailbox);
+   }
+   showInputForm($session, false);
+   exit();
+}
+
 if (!isset($attachments)) {
     $attachments = array();
     session_register('attachments');
@@ -121,8 +162,8 @@ if (!isset($mailbox) || $mailbox == '' || ($mailbox == 'None')) {
 
 if (isset($draft)) {
     include_once ('../src/draft_actions.php');
-    if (! isset($reply_id)) {
-         $reply_id = 0;
+    if (! isset($passed_id)) {
+         $passed_id = 0;
     }
     if (! isset($MDN)) {
         $MDN = 'False';
@@ -130,7 +171,7 @@ if (isset($draft)) {
     if (! isset($mailprio)) {
         $mailprio = '';
     }
-    if (!saveMessageAsDraft($send_to, $send_to_cc, $send_to_bcc, $subject, $body, $reply_id, $mailprio, $session)) {
+    if (!saveMessageAsDraft($send_to, $send_to_cc, $send_to_bcc, $subject, $body, $passed_id, $mailprio, $session)) {
         showInputForm($session);
         exit();
     } else {
@@ -148,7 +189,7 @@ if (isset($draft)) {
             }
             else {
             Header("Location: right_main.php?mailbox=$draft_folder&sort=$sort".
-                   "&startMessage=1&note=".urlencode($draft_message);
+                   "&startMessage=1&note=".urlencode($draft_message));
             exit();
             }
         }
@@ -163,8 +204,8 @@ if (isset($send)) {
     }
     if (checkInput(false) && !isset($AttachFailure)) {
         $urlMailbox = urlencode (trim($mailbox));
-        if (! isset($reply_id)) {
-            $reply_id = 0;
+        if (! isset($passed_id)) {
+            $passed_id = 0;
         }
         /*
          * Set $default_charset to correspond with the user's selection
@@ -204,10 +245,10 @@ if (isset($send)) {
         $MDN = False;  // we are not sending a mdn response
         if (! isset($mailprio)) {
             $Result = sendMessage($send_to, $send_to_cc, $send_to_bcc,
-                                  $subject, $body, $reply_id, $MDN, '', $session);
+                                  $subject, $body, $passed_id, $MDN, '', $session);
         } else {
             $Result = sendMessage($send_to, $send_to_cc, $send_to_bcc,
-                                  $subject, $body, $reply_id, $MDN, $mailprio, $session);
+                                  $subject, $body, $passed_id, $MDN, $mailprio, $session);
         }
         if (! $Result) {
             showInputForm($session);
@@ -333,48 +374,21 @@ elseif (isset($sigappend)) {
            unlink ($attached_file);
            unset ($attachments[$index]);
         }
+        setPref($data_dir, $username, 'attachments', serialize($attachments));
     }
 
     showInputForm($session);
-    
-} elseif (isset($attachedmessages)) {
-
-    /*
-     * This handles the case if we attache message 
-     */
-    $imapConnection = sqimap_login($username, $key, $imapServerAddress,
-                                   $imapPort, 0);
-        if ($compose_new_win == '1') {
-            compose_Header($color, $mailbox);
-        }
-        else {
-            displayPageHeader($color, $mailbox);
-        }
-
-    $newmail = true;
-
-    if (!isset($passed_ent_id)) $passed_ent_id = '';
-    if (!isset($passed_id)) $passed_id = '';    
-    if (!isset($mailbox)) $mailbox = '';
-    if (!isset($action)) $action = '';
-
-    $values = newMail($imapConnection,$mailbox,$passed_id,$passed_ent_id, $action, $session);
-    showInputForm($session, $values);
-    sqimap_logout($imapConnection);
-
 } else {
     /*
      * This handles the default case as well as the error case
      * (they had the same code) --> if (isset($smtpErrors)) 
      */
-    $imapConnection = sqimap_login($username, $key, $imapServerAddress,
-                                   $imapPort, 0);
-        if ($compose_new_win == '1') {
-            compose_Header($color, $mailbox);
-        }
-        else {
-            displayPageHeader($color, $mailbox);
-        }
+
+    if ($compose_new_win == '1') {
+       compose_Header($color, $mailbox);
+    } else {
+       displayPageHeader($color, $mailbox);
+    }
 
     $newmail = true;
 
@@ -383,9 +397,19 @@ elseif (isset($sigappend)) {
     if (!isset($mailbox)) $mailbox = '';
     if (!isset($action)) $action = '';
     
-    $values = newMail($imapConnection,$mailbox,$passed_id,$passed_ent_id, $action, $session);
+    $values = newMail($mailbox,$passed_id,$passed_ent_id, $action, $session);
+
+    /* in case the origin is not read_body.php */
+    if (isset($send_to)) {
+       $values['send_to'] = $send_to;
+    }
+    if (isset($send_to_cc)) {
+       $values['send_to_cc'] = $send_to_cc;
+    }
+    if (isset($send_to_bcc)) {
+       $values['send_to_bcc'] = $send_to_bcc;
+    }
     showInputForm($session, $values);
-    sqimap_logout($imapConnection);
 }
 
 exit();
@@ -394,13 +418,18 @@ exit();
 
 
 /* This function is used when not sending or adding attachments */
-function newMail ($imapConnection, $mailbox='', $passed_id='', $passed_ent_id='', $action='', $session='') {
+function newMail ($mailbox='', $passed_id='', $passed_ent_id='', $action='', $session='') {
     global $editor_size, $default_use_priority,
-           $use_signature, $composesession;
+           $use_signature, $composesession, $data_dir, $username,
+          $username, $key, $imapServerAddress, $imapPort;
 
-    $send_to = $send_to_cc = $send_to_bcc = $subject = $mailprio = $body = '';
+    $send_to = $send_to_cc = $send_to_bcc = $subject = $body = $identity = '';
+    $mailprio = 3;
 
     if ($passed_id) {
+        $imapConnection = sqimap_login($username, $key, $imapServerAddress,
+                          $imapPort, 0);
+    
         sqimap_mailbox_select($imapConnection, $mailbox);
         $message = sqimap_get_message($imapConnection, $passed_id, $mailbox);
        $body = '';     
@@ -448,6 +477,30 @@ function newMail ($imapConnection, $mailbox='', $passed_id='', $passed_ent_id=''
          $mailprio = '';
        }
         ClearAttachments($session);
+
+        $identity = '';
+        $idents = getPref($data_dir, $username, 'identities');
+        $from_o = $message->header->from;
+        if (is_object($from_o)) {
+            $orig_from = $from_o->getAddress();
+        } else {
+            $orig_from = '';
+        }    
+        if (!empty($idents) && $idents > 1) {
+           for ($i = 1; $i < $idents; $i++) {
+              $enc_from_name = '"'. 
+                              getPref($data_dir, 
+                                      $username, 
+                                      'full_name' . $i) .
+                 '" <' . getPref($data_dir, $username, 
+                                 'email_address' . $i) . '>';
+              if ($enc_from_name == $orig_from) {
+                  $identity = $i;
+                  break;
+              }
+           }
+        }
+       
        switch ($action) {
          case ('draft'):
              $use_signature = FALSE;
@@ -475,12 +528,12 @@ function newMail ($imapConnection, $mailbox='', $passed_id='', $passed_ent_id=''
             $send_to_bcc = $orig_header->getAddr_s('bcc');
             $subject = $orig_header->subject;
             $mailprio = $orig_header->priority;
+            $orig_from = '';
              getAttachments($message, $session, $passed_id, $entities, $imapConnection);
             sqUnWordWrap($body);
             break;
          case ('forward'):
             $send_to = '';
-            $orig_from = $orig_header->from->getAddress();
              $subject = $orig_header->subject;
              if ((substr(strtolower($subject), 0, 4) != 'fwd:') &&
                 (substr(strtolower($subject), 0, 5) != '[fwd:') &&
@@ -498,7 +551,6 @@ function newMail ($imapConnection, $mailbox='', $passed_id='', $passed_ent_id=''
             } else {
                $send_to = $orig_header->from->getAddress();
             }
-            $orig_from = $orig_header->from->getAddress();
             $subject = $orig_header->subject;
              $subject = str_replace('"', "'", $subject);
              $subject = trim($subject);
@@ -526,6 +578,7 @@ function newMail ($imapConnection, $mailbox='', $passed_id='', $passed_ent_id=''
          default:
             break;
         }
+       sqimap_logout($imapConnection);
     }
     $ret = array(
             'send_to' => $send_to, 
@@ -533,7 +586,8 @@ function newMail ($imapConnection, $mailbox='', $passed_id='', $passed_ent_id=''
            'send_to_bcc' => $send_to_bcc,           
            'subject' => $subject,
            'mailprio' => $mailprio,
-           'body' => $body
+           'body' => $body,
+           'identity' => $identity
            );
     
     return ($ret);
@@ -541,7 +595,7 @@ function newMail ($imapConnection, $mailbox='', $passed_id='', $passed_ent_id=''
 
 
 function getAttachments($message, $session, $passed_id, $entities, $imapConnection) {
-    global $attachments, $attachment_dir, $username;
+    global $attachments, $attachment_dir, $username, $data_dir;
     
     $hashed_attachment_dir = getHashedDir($username, $attachment_dir);
     if (!count($message->entities) || 
@@ -581,6 +635,7 @@ function getAttachments($message, $session, $passed_id, $entities, $imapConnecti
             fclose ($fp);
 
             $attachments[] = $newAttachment;
+            setPref($data_dir, $username, 'javascript_on', $js_pref);
         }
     } else {
         for ($i = 0; $i < count($message->entities); $i++) {
@@ -598,7 +653,8 @@ function showInputForm ($session, $values=false) {
            $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, $edit_as_new, $action;
+           $saved_draft, $mail_sent, $sig_first, $edit_as_new, $action, 
+          $username;
 
     $subject = decodeHeader($subject, false);
     if ($values) {
@@ -608,6 +664,7 @@ function showInputForm ($session, $values=false) {
        $subject = $values['subject'];       
        $mailprio = $values['mailprio'];
        $body = $values['body'];
+       $identity = $values['identity'];
     }
     
     if ($use_javascript_addr_book) {
@@ -635,9 +692,10 @@ function showInputForm ($session, $values=false) {
         echo '<input type="hidden" name="delete_draft" value="' . $delete_draft. "\">\n";
     }
     if (isset($session)) {
-        echo '<input type="hidden" name="session" value="' . "$session" . "\">\n";
+        echo '<input type="hidden" name="session" value="' . $session . "\">\n";
     }
 
+
     if ($saved_draft == 'yes') {
         echo '<BR><CENTER><B>'. _("Draft Saved").'</CENTER></B>';
     }
@@ -789,9 +847,9 @@ function showInputForm ($session, $values=false) {
         echo '</TABLE>'."\n";
     }
     echo '</TABLE>' . "\n";
-    if ($action = 'reply' || $action = 'reply_all') {
-        echo '<input type=hidden name=reply_id value=' . $passed_id . ">\n";
-    }
+
+    echo '<input type="hidden" name="username" value="'. $username . "\">\n";    
+    echo '<input type=hidden name=action value=' . $action . ">\n";
     echo '<INPUT TYPE=hidden NAME=mailbox VALUE="' . htmlspecialchars($mailbox) .
          "\">\n" .
          '</FORM>';
@@ -874,7 +932,8 @@ function checkInput ($show) {
 
 /* True if FAILURE */
 function saveAttachedFiles($session) {
-    global $HTTP_POST_FILES, $attachment_dir, $attachments, $username;
+    global $HTTP_POST_FILES, $attachment_dir, $attachments, $username,
+           $data_dir;
 
     $hashed_attachment_dir = getHashedDir($username, $attachment_dir);
     $localfilename = GenerateRandomString(32, '', 7);
@@ -890,16 +949,12 @@ function saveAttachedFiles($session) {
             return true;
                }
        } else {
-
                if (!@copy($HTTP_POST_FILES['attachfile']['tmp_name'], $full_localfilename)) {
                    return true;
                        }
        }
 
     }
-
-
-
     $newAttachment['localfilename'] = $localfilename;
     $newAttachment['remotefilename'] = $HTTP_POST_FILES['attachfile']['name'];
     $newAttachment['type'] = strtolower($HTTP_POST_FILES['attachfile']['type']);
@@ -908,14 +963,14 @@ function saveAttachedFiles($session) {
     if ($newAttachment['type'] == "") {
          $newAttachment['type'] = 'application/octet-stream';
     }
-
     $attachments[] = $newAttachment;
+    setPref($data_dir, $username, 'attachments', serialize($attachments));
 }
 
 
 function ClearAttachments($session)
 {
-    global $username, $attachments, $attachment_dir;
+    global $username, $attachments, $attachment_dir, $data_dir;
     $hashed_attachment_dir = getHashedDir($username, $attachment_dir);
 
     $rem_attachments = array();
@@ -933,6 +988,7 @@ function ClearAttachments($session)
         }
     }
     $attachments = $rem_attachments;
+    setPref($data_dir, $username, 'attachments', serialize($attachments));    
 }