瀏覽代碼

This fixes the broken smtp transport when dot's are not handled properly.
it is tested for smtp but i'm not sure if it also counts for sendmail (I
think it does).

stekkel 23 年之前
父節點
當前提交
f23a76ecd8
共有 1 個文件被更改,包括 58 次插入21 次删除
  1. 58 21
      functions/smtp.php

+ 58 - 21
functions/smtp.php

@@ -110,7 +110,7 @@ function expandRcptAddrs ($array) {
 
 /* Attach the files that are due to be attached
  */
-function attachFiles ($fp, $session, $rn="\r\n") {
+function attachFiles ($fp, $session, $rn="\r\n", $checkdot = false) {
     global $attachments, $attachment_dir, $username;
 
     $length = 0;
@@ -152,17 +152,40 @@ function attachFiles ($fp, $session, $rn="\r\n") {
 					    fputs ($fp, $header);
 					}
                     $length += strlen($header);
+		    if ($checkdot) {
+			$checkdot_begin=true;
+		    } else {
+			$checkdot_begin=false;
+		    }
                     while ($tmp = fgets($file, 4096)) {
                         $tmp = str_replace("\r\n", "\n", $tmp);
                         $tmp = str_replace("\r", "\n", $tmp);
+			/* In order to remove the problem of users not able to create
+			 * messages with "." on a blank line, RFC821 has made provision
+		         * in section 4.5.2 (Transparency).
+		         */
+		        if ($tmp{0} == '.' && $checkdot_begin) {
+		            $tmp = '.' . $tmp;
+			}
+			if ($checkdot) {
+		    	    $tmp = str_replace("\n.","\n..",$tmp);
+			}
+			
                         if ($rn == "\r\n"){
                             $tmp = str_replace("\n", "\r\n", $tmp);
                         }
-                        if ($fp) {
-                            fputs($fp, $tmp);
+			$tmp_length = strlen($tmp);
+			if ($tmp{$tmp_length-1} == "\n" && $checkdot) {
+			    $checkdot_begin = true;
+			} else {
+			    $checkdot_begin = false;
+			}
+			if ($fp) {
+                    	   fputs($fp, $tmp);
                         }
-                        $length += strlen($tmp);
+			$length += strlen($tmp_length);
                     }
+		    
                     if (substr($tmp, strlen($tmp) - strlen($rn), strlen($rn)) != $rn) {
                         if ($fp) {
                             fputs($fp, $rn);
@@ -408,7 +431,7 @@ function write822Header ($fp, $t, $c, $b, $subject, $body, $more_headers, $sessi
 
 /* Send the body
  */
-function writeBody ($fp, $passedBody, $session, $rn="\r\n") {
+function writeBody ($fp, $passedBody, $session, $rn="\r\n", $checkdot = false) {
     global $default_charset;
 
     $attachmentlength = 0;
@@ -439,7 +462,7 @@ function writeBody ($fp, $passedBody, $session, $rn="\r\n") {
 	    fputs ($fp, $body);
 	}
         
-        $attachmentlength = attachFiles($fp, $session, $rn);
+        $attachmentlength = attachFiles($fp, $session, $rn, $checkdot);
         
         if (!isset($postbody)) { 
             $postbody = ""; 
@@ -490,7 +513,7 @@ function sendSendmail($t, $c, $b, $subject, $body, $more_headers, $session) {
     
     $headerlength = write822Header ($fp, $t, $c, $b, $subject, $body,
                                     $more_headers, $session, "\n");
-    $bodylength = writeBody($fp, $body, $session, "\n");
+    $bodylength = writeBody($fp, $body, $session, "\n", true);
     
     pclose($fp);
 
@@ -647,7 +670,7 @@ function sendSMTP($t, $c, $b, $subject, $body, $more_headers, $session) {
     /* Send the message */
     $headerlength = write822Header ($smtpConnection, $t, $c, $b, 
                                     $subject, $body, $more_headers, $session);
-    $bodylength = writeBody($smtpConnection, $body, $session);
+    $bodylength = writeBody($smtpConnection, $body, $session, "\r\n", true);
     
     fputs($smtpConnection, ".\r\n"); /* end the DATA part */
     $tmp = fgets($smtpConnection, 1024);
@@ -876,13 +899,6 @@ function sendMessage($t, $c, $b, $subject, $body, $reply_id, $MDN,
         $more_headers = array_merge($more_headers, createReceiptHeaders($requestRecipt));
     }
 
-    /* In order to remove the problem of users not able to create
-     * messages with "." on a blank line, RFC821 has made provision
-     * in section 4.5.2 (Transparency).
-     */
-    $body = ereg_replace("\n\\.", "\n..", $body);
-    $body = ereg_replace("^\\.", "..", $body);
-
     /* this is to catch all plain \n instances and
      * replace them with \r\n.  All newlines were converted
      * into just \n inside the compose.php file.
@@ -895,17 +911,38 @@ function sendMessage($t, $c, $b, $subject, $body, $reply_id, $MDN,
     }
 
     if ($useSendmail) {
-        $length = sendSendmail($t, $c, $b, $subject, $body, $more_headers, 
+	/* In order to remove the problem of users not able to create
+	 * messages with "." on a blank line, RFC821 has made provision
+	 * in section 4.5.2 (Transparency).
+         */
+	 $body_sendmail = $body;
+	 if (($body_sendmail{0} == '.')) {
+    	     $body_sendmail = '.' . $body_sendmail;
+	 }
+	 $body_sendmail = str_replace("\n.","\n..",$body_sendmail);
+
+         $length = sendSendmail($t, $c, $b, $subject, $body_sendmail, $more_headers, 
                                $session);
-        $body = ereg_replace("\n", "\r\n", $body);
+         $body = ereg_replace("\n", "\r\n", $body);
     } else {
-        $body = ereg_replace("\n", "\r\n", $body);
-        $length = sendSMTP($t, $c, $b, $subject, $body, $more_headers, 
+         $body = ereg_replace("\n", "\r\n", $body);
+	/* In order to remove the problem of users not able to create
+	 * messages with "." on a blank line, RFC821 has made provision
+	 * in section 4.5.2 (Transparency).
+         */
+         $body_smtp = $body;
+	 if (($body_smtp{0} == '.')) {
+    	     $body_smtp = '.' . $body_smtp;
+	 }
+	 $body_smtp = str_replace("\n.","\n..",$body_smtp);
+
+
+         $length = sendSMTP($t, $c, $b, $subject, $body_smtp, $more_headers, 
                            $session);
     }
     if (sqimap_mailbox_exists ($imap_stream, $sent_folder)) {
-		$headerlength = write822Header (FALSE, $t, $c, $b, $subject, $more_headers, $session, "\r\n");
-		$bodylength = writeBody(FALSE, $body, $session, "\r\n");
+		$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);