Correct value of undefined hostname
[squirrelmail.git] / class / deliver / Deliver.class.php
index 59312ebf5802fd1e99ae9b3f34cda8095d8900fc..2f8e6a3153c666d2cdbd11def8e2be5668ceb008 100644 (file)
  */
 class Deliver {
 
+    /**
+     * Most recently calculated Message-ID
+     * External code should NEVER access this directly!
+     * @var string
+     */
+    var $message_id;
+
     /**
      * function mail - send the message parts to the SMTP stream
      *
@@ -52,8 +59,12 @@ class Deliver {
      *                               an overloaded version of this method
      *                               if needed.
      *
-     * @return integer $raw_length The number of bytes written (or that would 
-     *                             have been written) to the output stream
+     * @return array An array containing at least these elements in this order:
+     *                  - The number of bytes written (or that would have been
+     *                    written) to the output stream
+     *                  - The message ID (WARNING: if $stream is FALSE, this
+     *                    may not be supplied, or may not be accurate)
+     *
      */
     function mail($message, $stream=false, $reply_id=0, $reply_ent_id=0,
                   $extra=NULL) {
@@ -94,8 +105,8 @@ class Deliver {
             } else {
                 $orig_header = $reply_message->rfc822_header;
             }
+            $message->reply_rfc822_header = $orig_header;
         }
-        $message->reply_rfc822_header = $orig_header;
 
 
         $reply_rfc822_header = (isset($message->reply_rfc822_header)
@@ -104,7 +115,7 @@ class Deliver {
 
         $this->send_mail($message, $header, $boundary, $stream, $raw_length, $extra);
 
-        return $raw_length;
+        return array($raw_length, $this->message_id);
     }
 
     /**
@@ -480,8 +491,10 @@ class Deliver {
         global $domain, $username, $encode_header_key,
                $edit_identity, $hide_auth_header;
 
-        /* if server var SERVER_NAME not available, use $domain */
-        if(!sqGetGlobalVar('SERVER_NAME', $SERVER_NAME, SQ_SERVER)) {
+        /* if server var SERVER_NAME not available, or contains
+           ":" (e.g. IPv6) which is illegal in a Message-ID, use $domain */
+        if(!sqGetGlobalVar('SERVER_NAME', $SERVER_NAME, SQ_SERVER) ||
+            strpos($SERVER_NAME,':') !== FALSE) {
             $SERVER_NAME = $domain;
         }
 
@@ -495,15 +508,19 @@ class Deliver {
 
         /* This creates an RFC 822 date */
         $date = date('D, j M Y H:i:s ', time()) . $this->timezone();
+
         /* Create a message-id */
-        $message_id = '<' . $REMOTE_PORT . '.';
-        if (isset($encode_header_key) && trim($encode_header_key)!='') {
-            // use encrypted form of remote address
-            $message_id.= OneTimePadEncrypt($this->ip2hex($REMOTE_ADDR),base64_encode($encode_header_key));
-        } else {
-            $message_id.= $REMOTE_ADDR;
-        }
-        $message_id .= '.' . time() . '.squirrel@' . $SERVER_NAME .'>';
+        $message_id = '<';
+        /* user-specifc data to decrease collision chance */
+        $seed_data = $username . '.';
+        $seed_data .= (!empty($REMOTE_PORT) ? $REMOTE_PORT . '.' : '');
+        $seed_data .= (!empty($REMOTE_ADDR) ? $REMOTE_ADDR . '.' : '');
+        /* add the current time in milliseconds and randomness */
+        $seed_data .= uniqid(mt_rand(),true);
+        /* put it through one-way hash and add it to the ID */
+        $message_id .= md5($seed_data) . '.squirrel@' . $SERVER_NAME .'>';
+        $this->message_id = $message_id;
+
         /* Make an RFC822 Received: line */
         if (isset($REMOTE_HOST)) {
             $received_from = "$REMOTE_HOST ([$REMOTE_ADDR])";