sec. try
[squirrelmail.git] / functions / smtp.php
index 5e75527d6bfbf9584f022de6282066fb44887690..5c9f7f0ef875b687e48fe6e8f75c2a5e9468b036 100644 (file)
@@ -147,8 +147,10 @@ function attachFiles ($fp, $session, $rn="\r\n") {
                 $file = fopen ($filename, 'rb');
                 if (substr($filetype, 0, 5) == 'text/' ||
                     substr($filetype, 0, 8) == 'message/' ) {
-                    $header .= "$rn";
-                    fputs ($fp, $header);
+                    $header .= $rn;
+                                       if ($fp) {
+                                           fputs ($fp, $header);
+                                       }
                     $length += strlen($header);
                     while ($tmp = fgets($file, 4096)) {
                         $tmp = str_replace("\r\n", "\n", $tmp);
@@ -156,25 +158,25 @@ function attachFiles ($fp, $session, $rn="\r\n") {
                         if ($rn == "\r\n"){
                             $tmp = str_replace("\n", "\r\n", $tmp);
                         }
-                        /**
-                         * Check if the last line has newline ($rn) in it
-                         * and append if it doesn't.
-                         */
-                        if (feof($fp) && !strstr($tmp, "$rn")){
-                            $tmp .= "$rn";
+                        if ($fp) {
+                            fputs($fp, $tmp);
                         }
-                        fputs($fp, $tmp);
                         $length += strlen($tmp);
                     }
+                    if (substr($tmp, strlen($tmp) - strlen($rn), strlen($rn)) != $rn) {
+                        if ($fp) {
+                            fputs($fp, $rn);
+                        }
+                    }
                 } else {
                     $header .= "Content-Transfer-Encoding: base64" 
                         . "$rn" . "$rn";
-                    fputs ($fp, $header);
+                    if ($fp) fputs ($fp, $header);
                     $length += strlen($header);
                     while ($tmp = fread($file, 570)) {
                         $encoded = chunk_split(base64_encode($tmp));
                         $length += strlen($encoded);
-                        fputs ($fp, $encoded);
+                        if ($fp) fputs ($fp, $encoded);
                     }
                 }
                 fclose ($file);
@@ -187,7 +189,7 @@ function attachFiles ($fp, $session, $rn="\r\n") {
 /* Delete files that are uploaded for attaching
  */
 function deleteAttachments($session) {
-    global $username, $attachments, $attachment_dir;
+    global $username, $attachments, $attachment_dir, $data_dir;
     $hashed_attachment_dir = getHashedDir($username, $attachment_dir);
 
     $rem_attachments = array();
@@ -202,6 +204,7 @@ function deleteAttachments($session) {
         }
     }
     $attachments = $rem_attachments;
+    setPref($data_dir, $username, 'attachments', serialize($attachments));
 }
 
 /* Return a nice MIME-boundary
@@ -254,9 +257,10 @@ function write822Header ($fp, $t, $c, $b, $subject, $more_headers, $session, $rn
     /* Storing the header to make sure the header is the same
      * everytime the header is printed.
      */
-    static $header, $headerlength;
+    static $header, $headerlength, $headerrn;
     
     if ($header == '') {
+               $headerrn = $rn;
         $to = expandAddrs(parseAddrs($t));
         $cc = expandAddrs(parseAddrs($c));
         $bcc = expandAddrs(parseAddrs($b));
@@ -379,9 +383,15 @@ function write822Header ($fp, $t, $c, $b, $subject, $more_headers, $session, $rn
         
         $headerlength = strlen($header);
     }     
+
+       if ($headerrn != $rn) {
+               $header = str_replace($headerrn, $rn, $header);
+        $headerlength = strlen($header);
+               $headerrn = $rn;
+       }
     
     /* Write the header */
-    fputs ($fp, $header);
+    if ($fp) fputs ($fp, $header);
     
     return $headerlength;
 }
@@ -405,7 +415,7 @@ function writeBody ($fp, $passedBody, $session, $rn="\r\n") {
         
         $body .= "Content-Transfer-Encoding: 8bit" . $rn . $rn;
         $body .= $passedBody . $rn . $rn;
-        fputs ($fp, $body);
+        if ($fp) fputs ($fp, $body);
         
         $attachmentlength = attachFiles($fp, $session, $rn);
         
@@ -413,12 +423,12 @@ function writeBody ($fp, $passedBody, $session, $rn="\r\n") {
             $postbody = ""; 
         }
         $postbody .= $rn . "--" . mimeBoundary() . "--" . $rn . $rn;
-        fputs ($fp, $postbody);
+        if ($fp) fputs ($fp, $postbody);
     } else {
         $body = $passedBody . $rn;
-        fputs ($fp, $body);
+        if ($fp) fputs ($fp, $body);
         $postbody = $rn;
-        fputs ($fp, $postbody);
+        if ($fp) fputs ($fp, $postbody);
     }
 
     return (strlen($body) + strlen($postbody) + $attachmentlength);
@@ -739,14 +749,23 @@ function calculate_references($refs, $inreplyto, $old_reply_to) {
     $refer = "";
     for ($i=1;$i<count($refs[0]);$i++) {
         if (!empty($refs[0][$i])) {
-            if (preg_match("/^References:(.+)$/", $refs[0][$i], $regs)) {
+            if (preg_match("/^References:(.+)$/UA", $refs[0][$i], $regs)) {
                 $refer = trim($regs[1]);
             }
-            else {   
-                $refer .= ' ' . trim($refs[0][$i]);
-            }
+            else {
+               $refer .= ' ' . trim($refs[0][$i]);
+           }
+            
         }
     }
+    $refer_a = explode(' ', $refer);
+    $refer = '';
+    foreach ($refer_a as $ref) {
+       $ref = trim($ref);
+       if ($ref{0} == '<' && $ref{(strlen($ref)-1)} == '>') {
+          $refer .= $ref . ' ';
+       }
+    }
     $refer = trim($refer);
     if (strlen($refer) > 2) {
         $refer .= ' ' . $inreplyto;
@@ -768,7 +787,7 @@ function sendMessage($t, $c, $b, $subject, $body, $reply_id, $MDN,
     global $useSendmail, $msg_id, $is_reply, $mailbox, $onetimepad,
         $data_dir, $username, $domain, $key, $version, $sent_folder, 
         $imapServerAddress, $imapPort, $default_use_priority, $more_headers, 
-        $request_mdn, $request_dr;
+        $request_mdn, $request_dr, $uid_support;
 
     $more_headers = Array();
     
@@ -779,14 +798,14 @@ function sendMessage($t, $c, $b, $subject, $body, $reply_id, $MDN,
 
     if (isset($reply_id) && $reply_id) {
         sqimap_mailbox_select ($imap_stream, $mailbox);
-        sqimap_messages_flag ($imap_stream, $reply_id, $reply_id, 'Answered');
+        sqimap_messages_flag ($imap_stream, $reply_id, $reply_id, 'Answered', false);
 
         /* Insert In-Reply-To and References headers if the
          * message-id of the message we reply to is set (longer than "<>")
          * The References header should really be the old Referenced header
          * with the message ID appended, and now it is (jmunro)
          */
-       $sid = sqimap_session_id(); 
+       $sid = sqimap_session_id($uid_support); 
        $query = "$sid FETCH $reply_id (BODY.PEEK[HEADER.FIELDS (Message-Id In-Reply-To)])\r\n";
        fputs ($imap_stream, $query);
        $read = sqimap_read_data($imap_stream, $sid, true, $response, $message);
@@ -848,13 +867,17 @@ function sendMessage($t, $c, $b, $subject, $body, $reply_id, $MDN,
     if ($useSendmail) {
         $length = sendSendmail($t, $c, $b, $subject, $body, $more_headers, 
                                $session);
+        $body = ereg_replace("\n", "\r\n", $body);
     } else {
         $body = ereg_replace("\n", "\r\n", $body);
         $length = sendSMTP($t, $c, $b, $subject, $body, $more_headers, 
                            $session);
     }
     if (sqimap_mailbox_exists ($imap_stream, $sent_folder)) {
-        if ($useSendmail) $body = ereg_replace("\n", "\r\n", $body);
+               $headerlength = write822Header (FALSE, $t, $c, $b, $subject, $more_headers, $session, "\r\n");
+               $bodylength = writeBody(FALSE, $body, $session, "\r\n");
+               $length = $headerlength + $bodylength;
+
         sqimap_append ($imap_stream, $sent_folder, $length);
         write822Header ($imap_stream, $t, $c, $b, $subject, $more_headers, 
                         $session);