X-Git-Url: https://vcs.fsf.org/?a=blobdiff_plain;f=functions%2Fsmtp.php;h=892c785c80412f697ab3b38900745cb5f3dfcf10;hb=cf0d436aeba404fcede2204d19e03efc2e0422c2;hp=100be5e209ebe98cf670df87c7f76a7c6c02823d;hpb=a3fbf5a4ae5a7edddfeacae0026a96fc586f9746;p=squirrelmail.git diff --git a/functions/smtp.php b/functions/smtp.php index 100be5e2..892c785c 100644 --- a/functions/smtp.php +++ b/functions/smtp.php @@ -31,20 +31,6 @@ if (!$domain) { $domain = getenv('HOSTNAME'); } -/** - * Return which separator we should be using. - * \r\n for SMTP delivery, just \n for Sendmail. - */ -function sqm_nrn(){ - global $useSendmail; - if ($useSendmail){ - return "\n"; - } else { - return "\r\n"; - } -} - - /* Returns true only if this message is multipart */ function isMultipart ($session) { global $attachments; @@ -124,11 +110,10 @@ function expandRcptAddrs ($array) { /* Attach the files that are due to be attached */ -function attachFiles ($fp, $session) { +function attachFiles ($fp, $session, $rn="\r\n") { global $attachments, $attachment_dir, $username; $length = 0; - $rn = sqm_nrn(); $hashed_attachment_dir = getHashedDir($username, $attachment_dir); if (isMultipart($session)) { @@ -149,7 +134,7 @@ function attachFiles ($fp, $session) { $header .= "Content-Disposition: attachment; filename=\"" . $info['remotefilename'] . "\"$rn"; } else { - $header .= "Content-Type: $filetype;$rn"; + $header .= "Content-Type: $filetype$rn"; } @@ -162,8 +147,10 @@ function attachFiles ($fp, $session) { $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); @@ -171,25 +158,25 @@ function attachFiles ($fp, $session) { 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); @@ -202,7 +189,7 @@ function attachFiles ($fp, $session) { /* 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(); @@ -217,6 +204,7 @@ function deleteAttachments($session) { } } $attachments = $rem_attachments; + setPref($data_dir, $username, 'attachments', serialize($attachments)); } /* Return a nice MIME-boundary @@ -260,37 +248,30 @@ function timezone () { } /* Print all the needed RFC822 headers */ -function write822Header ($fp, $t, $c, $b, $subject, $more_headers, $session) { +function write822Header ($fp, $t, $c, $b, $subject, $more_headers, $session, $rn="\r\n") { global $REMOTE_ADDR, $SERVER_NAME, $REMOTE_PORT; global $data_dir, $username, $popuser, $domain, $version, $useSendmail; global $default_charset, $HTTP_VIA, $HTTP_X_FORWARDED_FOR; global $REMOTE_HOST, $identity; - /** - * Get which delimiter we are going to use. - */ - $rn = sqm_nrn(); + /* 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)); if (isset($identity) && $identity != 'default') { $reply_to = getPref($data_dir, $username, 'reply_to' . $identity); $from = getPref($data_dir, $username, 'full_name' . $identity); - $from_addr = getPref($data_dir, $username, - 'email_address' . $identity); + $from_addr = getFrom(); } else { $reply_to = getPref($data_dir, $username, 'reply_to'); $from = getPref($data_dir, $username, 'full_name'); - $from_addr = getPref($data_dir, $username, 'email_address'); - } - - if ($from_addr == '') { - $from_addr = $popuser.'@'.$domain; + $from_addr = getFrom(); } $to_list = getLineOfAddrs($to); @@ -362,6 +343,9 @@ function write822Header ($fp, $t, $c, $b, $subject, $more_headers, $session) { if(is_array($more_headers)) { reset($more_headers); while(list($h_name, $h_val) = each($more_headers)) { + if ($h_name == 'References') { + $h_val = str_replace(' ', "$rn ", $h_val); + } $header .= sprintf("%s: %s%s", $h_name, $h_val, $rn); } } @@ -399,21 +383,23 @@ function write822Header ($fp, $t, $c, $b, $subject, $more_headers, $session) { $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; } /* Send the body */ -function writeBody ($fp, $passedBody, $session) { +function writeBody ($fp, $passedBody, $session, $rn="\r\n") { global $default_charset; - /** - * Get delimiter. - */ - $rn = sqm_nrn(); $attachmentlength = 0; @@ -429,20 +415,20 @@ function writeBody ($fp, $passedBody, $session) { $body .= "Content-Transfer-Encoding: 8bit" . $rn . $rn; $body .= $passedBody . $rn . $rn; - fputs ($fp, $body); + if ($fp) fputs ($fp, $body); - $attachmentlength = attachFiles($fp, $session); + $attachmentlength = attachFiles($fp, $session, $rn); if (!isset($postbody)) { $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); @@ -457,7 +443,7 @@ function sendSendmail($t, $c, $b, $subject, $body, $more_headers, $session) { * spaces or other "weird" chars that would allow a user to * exploit the shell/pipe it is used in. */ - $envelopefrom = "$popuser@$domain"; + $envelopefrom = getFrom(); $envelopefrom = ereg_replace("[[:blank:]]",'', $envelopefrom); $envelopefrom = ereg_replace("[[:space:]]",'', $envelopefrom); $envelopefrom = ereg_replace("[[:cntrl:]]",'', $envelopefrom); @@ -473,11 +459,11 @@ function sendSendmail($t, $c, $b, $subject, $body, $more_headers, $session) { } $headerlength = write822Header ($fp, $t, $c, $b, $subject, - $more_headers, $session); - $bodylength = writeBody($fp, $body, $session); + $more_headers, $session, "\n"); + $bodylength = writeBody($fp, $body, $session, "\n"); pclose($fp); - + return ($headerlength + $bodylength); } @@ -511,6 +497,39 @@ function sendSMTP($t, $c, $b, $subject, $body, $more_headers, $session) { if (!$from_addr) { $from_addr = "$popuser@$domain"; } + + /* POP3 BEFORE SMTP CODE HERE */ + global $pop_before_smtp; + if (isset($pop_before_smtp) && $pop_before_smtp === true) { + if (!isset($pop_port)) { + $pop_port = 110; + } + if (!isset($pop_server)) { + $pop_server = $smtpServerAddress; /* usually the same host! */ + } + $popConnection = fsockopen($pop_server, $pop_port, $err_no, $err_str); + if (!$popConnection) { + error_log("Error connecting to POP Server ($pop_server:$pop_port)" + . " $err_no : $err_str"); + } else { + $tmp = fgets($popConnection, 1024); /* banner */ + if (!eregi("^\+OK", $tmp, $regs)) { + return(0); + } + fputs($popConnection, "USER $username\r\n"); + $tmp = fgets($popConnection, 1024); + if (!eregi("^\+OK", $tmp, $regs)) { + return(0); + } + fputs($popConnection, 'PASS ' . OneTimePadDecrypt($key, $onetimepad) . "\r\n"); + $tmp = fgets($popConnection, 1024); + if (!eregi("^\+OK", $tmp, $regs)) { + return(0); + } + fputs($popConnection, "QUIT\r\n"); /* log off */ + fclose($popConnection); + } + } $smtpConnection = fsockopen($smtpServerAddress, $smtpPort, $errorNumber, $errorString); @@ -726,10 +745,7 @@ function errorCheck($line, $smtpConnection, $verbose = false) { /* create new reference header per rfc2822 */ function calculate_references($refs, $inreplyto, $old_reply_to) { - /** - * Get the delimiter. - */ - $rn = sqm_nrn(); + $refer = ""; for ($i=1;$i") * The References header should really be the old Referenced header * with the message ID appended, and now it is (jmunro) */ - $hdr = sqimap_get_small_header ($imap_stream, $reply_id, false); - if(strlen($hdr->message_id) > 2) { + $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); + $message_id = ''; + $in_reply_to = ''; + + foreach ($read as $r) { + if (preg_match("/^message-id:(.*)/iA", $r, $regs)) { + $message_id = trim($regs[1]); + } + if (preg_match("/^in-reply-to:(.*)/iA", $r, $regs)) { + $in_reply_to = trim($regs[1]); + } + } + + if(strlen($message_id) > 2) { $refs = get_reference_header ($imap_stream, $reply_id); - $inreplyto = $hdr->message_id; - $old_reply_to = $hdr->inrepto; + $inreplyto = $message_id; + $old_reply_to = $in_reply_to; $refer = calculate_references ($refs, $inreplyto, $old_reply_to); $more_headers['In-Reply-To'] = $inreplyto; $more_headers['References'] = $refer; } + } if ($default_use_priority) { $more_headers = array_merge($more_headers, createPriorityHeaders($prio)); @@ -822,9 +849,6 @@ function sendMessage($t, $c, $b, $subject, $body, $reply_id, $MDN, * into just \n inside the compose.php file. * But only if delimiter is, in fact, \r\n. */ - if ($rn == "\r\n"){ - $body = ereg_replace("\n", "\r\n", $body); - } if ($MDN) { $more_headers["Content-Type"] = "multipart/report; ". @@ -834,11 +858,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)) { + $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); @@ -879,11 +909,19 @@ function createPriorityHeaders($prio) { function createReceiptHeaders($receipt) { - GLOBAL $data_dir, $username; + GLOBAL $data_dir, $username, $identity, $popuser, $domain; $receipt_headers = Array(); - $from_addr = getPref($data_dir, $username, 'email_address'); - $from = getPref($data_dir, $username, 'full_name'); + if (isset($identity) && $identity != 'default') { + $from = getPref($data_dir, $username, 'full_name' . $identity); + $from_addr = getPref($data_dir, $username, 'email_address' . $identity); + } else { + $from = getPref($data_dir, $username, 'full_name'); + $from_addr = getPref($data_dir, $username, 'email_address'); + } + if ($from_addr == '') { + $from_addr = $popuser.'@'.$domain; + } if ($from == '') { $from = "<$from_addr>"; @@ -908,5 +946,24 @@ function createReceiptHeaders($receipt) { return $receipt_headers; } +/* Figure out what the 'From:' address is + */ + +function getFrom() { + global $username, $popuser, $domain, $data_dir, $identity; + if (isset($identity) && $identity != 'default') { + $from_addr = getPref($data_dir, $username, + 'email_address' . $identity); + } + else { + $from_addr = getPref($data_dir, $username, 'email_address'); + } + + if (!$from_addr) { + $from_addr = "$popuser@$domain"; + } + return $from_addr; +} + ?>