More rg=0
[squirrelmail.git] / src / compose.php
index b0b8257b7795ab6ad1c0dc5c0d5c90ee08e165c2..f636ae19c52142698b4499f1d16f3496180ae0e6 100644 (file)
@@ -21,7 +21,7 @@
 define('SM_PATH','../');
 
 /* SquirrelMail required files. */
-require_once(SM_PATH . 'src/validate.php');
+require_once(SM_PATH . 'include/validate.php');
 require_once(SM_PATH . 'functions/imap.php');
 require_once(SM_PATH . 'functions/date.php');
 require_once(SM_PATH . 'functions/mime.php');
@@ -30,6 +30,76 @@ require_once(SM_PATH . 'functions/plugin.php');
 require_once(SM_PATH . 'functions/display_messages.php');
 require_once(SM_PATH . 'class/deliver/Deliver.class.php');
 
+/* --------------------- Get globals ------------------------------------- */
+$username = $_SESSION['username'];
+$onetimepad = $_SESSION['onetimepad'];
+$base_uri = $_SESSION['base_uri'];
+$delimiter = $_SESSION['delimiter'];
+
+if (isset($_POST['return'])) {
+    $html_addr_search_done = 'Use Addresses';
+}
+if ( isset($_SESSION['composesession']) ) {
+    $composesession = $_SESSION['composesession'];
+}
+sqextractGlobalVar('session');
+sqextractGlobalVar('mailbox');
+sqextractGlobalVar('identity');
+sqextractGlobalVar('send_to');
+sqextractGlobalVar('send_to_cc');
+sqextractGlobalVar('send_to_bcc');
+sqextractGlobalVar('subject');
+sqextractGlobalVar('body');
+sqextractGlobalVar('mailprio');
+sqextractGlobalVar('request_mdn');
+sqextractGlobalVar('request_dr');
+sqextractGlobalVar('html_addr_search');
+sqextractGlobalVar('mail_sent');
+sqextractGlobalVar('passed_id');
+
+if ( isset($_POST['sigappend']) ) {
+    $sigappend = $_POST['sigappend'];
+}
+/* From addressbook search */
+if ( isset($_POST['from_htmladdr_search']) ) {
+    $from_htmladdr_search = $_POST['from_htmladdr_search'];
+}
+if ( isset($_POST['addr_search_done']) ) {
+    $html_addr_search_done = $_POST['addr_search_done'];
+}
+if ( isset($_POST['send_to_search']) ) {
+    $send_to_search = &$_POST['send_to_search'];
+}
+
+/* Attachments */
+sqextractGlobalVar('attach');
+if ( isset($_POST['do_delete']) ) {
+    $do_delete = $_POST['do_delete'];
+}
+if ( isset($_POST['delete']) ) {
+    $delete = &$_POST['delete'];
+}
+if ( isset($_POST['attachments']) ) {
+    $attachments = &$_POST['attachments'];
+}
+elseif ( isset($_SESSION['attachments'])) {
+    $attachments = &$_SESSION['attachments'];
+}
+
+/* Forward message as attachment */
+if ( isset($_GET['attachedmessages']) ) {
+    $attachedmessages = $_GET['attachedmessages'];
+}
+
+/* Drafts */
+sqextractGlobalVar('draft');
+sqextractGlobalVar('draft_id');
+sqextractGlobalVar('ent_num');
+sqextractGlobalVar('saved_draft');
+sqextractGlobalVar('delete_draft');
+
+$key = $_COOKIE['key'];
+
 /* --------------------- Specific Functions ------------------------------ */
 
 function replyAllString($header) {
@@ -112,50 +182,47 @@ function getforwardHeader($orig_header) {
 }
 /* ----------------------------------------------------------------------- */
 
+/* OLD: should be adapted to composeMessage */
+
 /*
  * 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();
-    sqsession_register(array(), 'attachments');
+    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) {
+        sqsession_unregister('session_expired_post');
+        sqsession_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;
+    }
+    sqsession_unregister('session_expired_post');
+    sqsession_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($composesession)) {
@@ -164,6 +231,7 @@ if (!isset($composesession)) {
 }
 
 if (!isset($session) || (isset($newmessage) && $newmessage)) {
+    sqsession_unregister('composesession');
     $session = "$composesession" +1; 
     $composesession = $session;
     sqsession_register($composesession,'composesession');
@@ -172,14 +240,17 @@ if (!isset($session) || (isset($newmessage) && $newmessage)) {
 if (!isset($compose_messages)) {
   $compose_messages = array();
 }
-if (!array_key_exists($session, $compose_messages)) {
-  $composeMessage = new message();
+if (!isset($compose_messages[$session]) || ($compose_messages[$session] == NULL)) {
+/* if (!array_key_exists($session, $compose_messages)) {  /* We can only do this in PHP >= 4.1 */
+  $composeMessage = new Message();
   $rfc822_header = new Rfc822Header();
   $composeMessage->rfc822_header = $rfc822_header;
   $composeMessage->reply_rfc822_header = '';
   $compose_messages[$session] = $composeMessage;
+  sqsession_register($compose_messages,'compose_messages');  
+} else {
+  $composeMessage=$compose_messages[$session];
 }
-sqsession_register($compose_messages,'compose_messages');
 
 if (!isset($mailbox) || $mailbox == '' || ($mailbox == 'None')) {
     $mailbox = 'INBOX';
@@ -220,9 +291,9 @@ if (isset($draft)) {
 }
 
 if (isset($send)) {
-    if (isset($HTTP_POST_FILES['attachfile']) &&
-        $HTTP_POST_FILES['attachfile']['tmp_name'] &&
-        $HTTP_POST_FILES['attachfile']['tmp_name'] != 'none') {
+    if (isset($_FILES['attachfile']) &&
+        $_FILES['attachfile']['tmp_name'] &&
+        $_FILES['attachfile']['tmp_name'] != 'none') {
         $AttachFailure = saveAttachedFiles($session);
     }
     if (checkInput(false) && !isset($AttachFailure)) {
@@ -331,10 +402,10 @@ if (isset($send)) {
     }
     showInputForm($session);
 } elseif (isset($html_addr_search)) {
-    if (isset($HTTP_POST_FILES['attachfile']) &&
-        $HTTP_POST_FILES['attachfile']['tmp_name'] &&
-        $HTTP_POST_FILES['attachfile']['tmp_name'] != 'none') {
-        if (saveAttachedFiles($session)) {
+    if (isset($_FILES['attachfile']) &&
+        $_FILES['attachfile']['tmp_name'] &&
+        $_FILES['attachfile']['tmp_name'] != 'none') {
+        if(saveAttachedFiles($session)) {
             plain_error_message(_("Could not move/copy file. File not attached"), $color);
         }
     }
@@ -537,9 +608,9 @@ function newMail ($mailbox='', $passed_id='', $passed_ent_id='', $action='', $se
         switch ($action) {
         case ('draft'):
             $use_signature = FALSE;
-            $send_to = $orig_header->getAddr_s('to');
-            $send_to_cc = $orig_header->getAddr_s('cc');
-            $send_to_bcc = $orig_header->getAddr_s('bcc');
+            $send_to = decodeHeader($orig_header->getAddr_s('to'));
+            $send_to_cc = decodeHeader($orig_header->getAddr_s('cc'));
+            $send_to_bcc = decodeHeader($orig_header->getAddr_s('bcc'));
             $subject = decodeHeader($orig_header->subject);
 
             $body_ary = explode("\n", $body);
@@ -556,9 +627,9 @@ function newMail ($mailbox='', $passed_id='', $passed_ent_id='', $action='', $se
             $composeMessage = getAttachments($message, $composeMessage, $passed_id, $entities, $imapConnection);
             break;
         case ('edit_as_new'):
-            $send_to = $orig_header->getAddr_s('to');
-            $send_to_cc = $orig_header->getAddr_s('cc');
-            $send_to_bcc = $orig_header->getAddr_s('bcc');
+            $send_to = decodeHeader($orig_header->getAddr_s('to'));
+            $send_to_cc = decodeHeader($orig_header->getAddr_s('cc'));
+            $send_to_bcc = decodeHeader($orig_header->getAddr_s('bcc'));
             $subject = decodeHeader($orig_header->subject);
             $mailprio = $orig_header->priority;
             $orig_from = '';
@@ -586,9 +657,9 @@ function newMail ($mailbox='', $passed_id='', $passed_ent_id='', $action='', $se
         case ('reply'):
             $send_to = $orig_header->reply_to;
             if (is_object($send_to)) {
-                $send_to = $send_to->getAddr_s('reply_to');
+                $send_to = decodeHeader($send_to->getAddr_s('reply_to'));
             } else {
-                $send_to = $orig_header->getAddr_s('from');
+                $send_to = decodeHeader($orig_header->getAddr_s('from'));
             }
             $subject =  decodeHeader($orig_header->subject);
             $subject = str_replace('"', "'", $subject);
@@ -1033,7 +1104,7 @@ function checkInput ($show) {
 
 /* True if FAILURE */
 function saveAttachedFiles($session) {
-    global $HTTP_POST_FILES, $attachment_dir, $attachments, $username,
+    global $_FILES, $attachment_dir, $attachments, $username,
            $data_dir, $compose_messages;
 
     $hashed_attachment_dir = getHashedDir($username, $attachment_dir);
@@ -1044,20 +1115,20 @@ function saveAttachedFiles($session) {
         $full_localfilename = "$hashed_attachment_dir/$localfilename";
     }
 
-    if (!@rename($HTTP_POST_FILES['attachfile']['tmp_name'], $full_localfilename)) {
+    if (!@rename($_FILES['attachfile']['tmp_name'], $full_localfilename)) {
        if (function_exists("move_uploaded_file")) {
-            if (!@move_uploaded_file($HTTP_POST_FILES['attachfile']['tmp_name'], $full_localfilename)) {
+            if (!@move_uploaded_file($_FILES['attachfile']['tmp_name'],$full_localfilename)) {
                return true;
            }
        } else {
-           if (!@copy($HTTP_POST_FILES['attachfile']['tmp_name'], $full_localfilename)) {
+           if (!@copy($_FILES['attachfile']['tmp_name'], $full_localfilename)) {
                 return true;
             }
        }
     }
     $message = $compose_messages[$session];
-    $type = strtolower($HTTP_POST_FILES['attachfile']['type']);
-    $name = $HTTP_POST_FILES['attachfile']['name'];
+    $type = strtolower($_FILES['attachfile']['type']);
+    $name = $_FILES['attachfile']['name'];
     $message->initAttachment($type, $name, $full_localfilename);
     $compose_messages[$session] = $message;
 }
@@ -1151,24 +1222,25 @@ function sendMessage($composeMessage, $draft=false) {
     }
     $reply_to = '';
     if (isset($identity) && $identity != 'default') {
-        $from_mail = getPref($data_dir, $username, 
-                     'email_address' . $identity);
-        $full_name = getPref($data_dir, $username, 
-                     'full_name' . $identity);
-        $from_addr = '"'.$full_name.'" <'.$from_mail.'>';
-        $reply_to = getPref($data_dir, $username, 
-                     'reply_to' . $identity);
+        $from_mail = getPref($data_dir, $username,'email_address' . $identity);
+        $full_name = getPref($data_dir, $username,'full_name' . $identity);
+        $reply_to = getPref($data_dir, $username,'reply_to' . $identity);
     } else {
         $from_mail = getPref($data_dir, $username, 'email_address');
         $full_name = getPref($data_dir, $username, 'full_name');
-        $from_addr = '"'.$full_name.'" <'.$from_mail.'>';
         $reply_to = getPref($data_dir, $username,'reply_to');
     }
-    if (!$from_addr) {
-       $from_addr = "$popuser@$domain";
-       $from_mail = $from_addr;
+    if (!$from_mail) {
+       $from_mail = "$popuser@$domain";
+       $full_name = '';
+    }
+    $rfc822_header->from = $rfc822_header->parseAddress($from_mail,true);
+    if ($full_name) {
+        $from = $rfc822_header->from[0];
+       if (!$from->host) $from->host = $domain;
+       $from_addr = $full_name .' <'.$from->mailbox.'@'.$from->host.'>';
+        $rfc822_header->from = $rfc822_header->parseAddress($from_addr,true);
     }
-    $rfc822_header->from = $rfc822_header->parseAddress($from_addr,true);
     if ($reply_to) {
        $rfc822_header->reply_to = $rfc822_header->parseAddress($reply_to,true);
     }
@@ -1232,7 +1304,6 @@ function sendMessage($composeMessage, $draft=false) {
     } elseif ($draft) {
        global $draft_folder;
        require_once(SM_PATH . 'class/deliver/Deliver_IMAP.class.php');
-       $imap_deliver = new Deliver_IMAP();
        $imap_stream = sqimap_login($username, $key, $imapServerAddress,
                       $imapPort, 0);
        if (sqimap_mailbox_exists ($imap_stream, $draft_folder)) {