centralise the "From"-header construction in functions/identities.php.
authorkink <kink@7612ce4b-ef26-0410-bec9-ea0150e637f0>
Fri, 11 May 2007 21:49:36 +0000 (21:49 +0000)
committerkink <kink@7612ce4b-ef26-0410-bec9-ea0150e637f0>
Fri, 11 May 2007 21:49:36 +0000 (21:49 +0000)
This reduces the two instances used under src/ to one, and picks the best
features of both.

git-svn-id: https://svn.code.sf.net/p/squirrelmail/code/trunk/squirrelmail@12394 7612ce4b-ef26-0410-bec9-ea0150e637f0

functions/identity.php
src/compose.php
src/read_body.php

index 322485e..b2b8217 100644 (file)
@@ -43,8 +43,9 @@ function get_identities() {
     /* If there are any others, add them to the array */
     if (!empty($num_ids) && $num_ids > 1) {
         for ($i=1;$i<$num_ids;$i++) {
+            $thisem = getPref($data_dir,$username,'email_address' . $i);
             $identities[] = array('full_name' => getPref($data_dir,$username,'full_name' . $i),
-            'email_address' => getPref($data_dir,$username,'email_address' . $i),
+            'email_address' => empty($thisem)?$em:$thisem,
             'reply_to' => getPref($data_dir,$username,'reply_to' . $i),
             'signature' => getSig($data_dir,$username,$i),
             'index' => $i );
@@ -210,3 +211,57 @@ function empty_identity($ident) {
         return false;
     }
 }
+
+/**
+ * Construct our "From:" header based on
+ * a supplied identity number.
+ * Will fall back when no sensible email address has been defined.
+ *
+ * @param   int $identity   identity# to use
+ * @since 1.5.2
+ */
+function build_from_header($identity = 0) {
+    $idents = get_identities();
+
+    if (! isset($idents[$identity]) ) $identity = 0;
+
+    if ( !empty($idents[$identity]['full_name']) ) {
+        $from_name = $idents[$identity]['full_name'];
+    }
+
+    $from_mail = $idents[$identity]['email_address'];
+    
+    if ( isset($from_name) ) {
+        $from_name_encoded = encodeHeader($from_name);
+        if ($from_name_encoded != $from_name) {
+            return $from_name_encoded .' <'.$from_mail.'>';
+        }
+        return '"'.$from_name .'" <'.$from_mail.'>';
+    }
+    return $from_mail;
+}
+
+/**
+ * Find a matching identity based on a set of emailaddresses.
+ * Will return the first identity to have a matching address.
+ * When nothing found, returns the default identity.
+ *
+ * @param needles   array   list of mailadresses
+ * @returns int identity
+ * @since 1.5.2
+ */
+function find_identity($needles) {
+    $idents = get_identities();
+    if ( count($idents) == 1 || empty($needles) ) return 0;
+
+    foreach ( $idents as $nr => $ident ) {
+        if ( isset($ident['email_address']) ) {
+            foreach ( $needles as $needle ) {
+                if ( $needle == $ident['email_address'] ) {
+                    return $nr;
+                }
+            }
+        }
+    }
+    return 0;
+}
index ae4eb31..96b7444 100644 (file)
@@ -815,7 +815,6 @@ function newMail ($mailbox='', $passed_id='', $passed_ent_id='', $action='', $se
             $mailprio = '';
         }
 
-        $identity = '';
         $from_o = $orig_header->from;
         if (is_array($from_o)) {
             if (isset($from_o[0])) {
@@ -856,15 +855,7 @@ function newMail ($mailbox='', $passed_id='', $passed_ent_id='', $action='', $se
                 $send_from_parts = new AddressStructure();
                 $send_from_parts = $orig_header->parseAddress($send_from);
                 $send_from_add = $send_from_parts->mailbox . '@' . $send_from_parts->host;
-                $identities = get_identities();
-                if (count($identities) > 0) {
-                    foreach($identities as $iddata) {
-                        if ($send_from_add == $iddata['email_address']) {
-                            $identity = $iddata['index'];
-                            break;
-                        }
-                    }
-                }
+                $identity = find_identity(array($send_from_add));
                 $subject = decodeHeader($orig_header->subject,false,false,true);
 
                 // Remember the receipt settings
@@ -1443,7 +1434,7 @@ function getByteSize($ini_size) {
  */
 function deliverMessage($composeMessage, $draft=false) {
     global $send_to, $send_to_cc, $send_to_bcc, $mailprio, $subject, $body,
-        $username, $popuser, $usernamedata, $identity, $idents, $data_dir,
+        $username, $identity, $idents, $data_dir,
         $request_mdn, $request_dr, $default_charset, $color, $useSendmail,
         $domain, $action, $default_move_to_sent, $move_to_sent;
     global $imapServerAddress, $imapPort, $sent_folder, $key;
@@ -1468,45 +1459,24 @@ function deliverMessage($composeMessage, $draft=false) {
     }
     $composeMessage->setBody($body);
 
-    if (ereg("^([^@%/]+)[@%/](.+)$", $username, $usernamedata)) {
-        $popuser = $usernamedata[1];
-        $domain  = $usernamedata[2];
-        unset($usernamedata);
-    } else {
-        $popuser = $username;
-    }
     $reply_to = '';
-    $from_mail = $idents[$identity]['email_address'];
-    $full_name = $idents[$identity]['full_name'];
     $reply_to  = $idents[$identity]['reply_to'];
-    if (!$from_mail) {
-        $from_mail = "$popuser@$domain";
-    }
-    $rfc822_header->from = $rfc822_header->parseAddress($from_mail,true);
-    if ($full_name) {
-        $from = $rfc822_header->from[0];
-        if (!$from->host) $from->host = $domain;
-        $full_name_encoded = encodeHeader($full_name);
-        if ($full_name_encoded != $full_name) {
-            $from_addr = $full_name_encoded .' <'.$from->mailbox.'@'.$from->host.'>';
-        } else {
-            $from_addr = '"'.$full_name .'" <'.$from->mailbox.'@'.$from->host.'>';
-        }
-        $rfc822_header->from = $rfc822_header->parseAddress($from_addr,true);
-    }
+    
+    $from_addr = build_from_header($identity);
+    $rfc822_header->from = $rfc822_header->parseAddress($from_addr,true);
     if ($reply_to) {
         $rfc822_header->reply_to = $rfc822_header->parseAddress($reply_to,true);
     }
     /* Receipt: On Read */
     if (isset($request_mdn) && $request_mdn) {
-        $rfc822_header->dnt = $rfc822_header->parseAddress($from_mail,true);
+        $rfc822_header->dnt = $rfc822_header->parseAddress($from_addr,true);
     } elseif (isset($rfc822_header->dnt)) {
         unset($rfc822_header->dnt);
     }
 
     /* Receipt: On Delivery */
     if (isset($request_dr) && $request_dr) {
-        $rfc822_header->more_headers['Return-Receipt-To'] = $from_mail;
+        $rfc822_header->more_headers['Return-Receipt-To'] = $from->mailbox.'@'.$from->domain;
     } elseif (isset($rfc822_header->more_headers['Return-Receipt-To'])) {
         unset($rfc822_header->more_headers['Return-Receipt-To']);
     }
index 6b588bd..b2f8266 100644 (file)
@@ -124,8 +124,8 @@ function ServerMDNSupport($aFlags) {
              in_array('\\*',$aFlags,true) ) ;
 }
 
-function SendMDN ( $mailbox, $passed_id, $sender, $message, $imapConnection) {
-    global $username, $attachment_dir, $popuser, $username, $color,
+function SendMDN ( $mailbox, $passed_id, $message, $imapConnection) {
+    global $username, $attachment_dir, $color,
            $squirrelmail_language, $default_charset,
            $languages, $useSendmail, $domain, $sent_folder;
 
@@ -145,29 +145,20 @@ function SendMDN ( $mailbox, $passed_id, $sender, $message, $imapConnection) {
     $rfc822_header->to[] = $header->dnt;
     $rfc822_header->subject = _("Read:") . ' ' . encodeHeader($header->subject);
 
-    // Patch #793504 Return Receipt Failing with <@> from Tim Craig (burny_md)
-    // This merely comes from compose.php and only happens when there is no
-    // email_addr specified in user's identity (which is the startup config)
-    if (ereg("^([^@%/]+)[@%/](.+)$", $username, $usernamedata)) {
-       $popuser = $usernamedata[1];
-       $domain  = $usernamedata[2];
-       unset($usernamedata);
-    } else {
-       $popuser = $username;
+    $idents = get_identities();
+    $needles = array();
+    if ($header->to) {
+        foreach ($header->to as $message_to) {
+             $needles[] = $message_to->mailbox.'@'.$message_to->host;
+        }
     }
+    $identity = find_identity($needles);
+    $from_addr = build_from_header($identity);
+    $reply_to = isset($idents[$identity]['reply_to']) ? $idents[$identity]['reply_to'] : '';
+    // FIXME: this must actually be the envelope address of the orginal message,
+    // but do we have that information? For now the first identity is our best guess.
+    $final_recipient = $idents[0]['email_address'];
 
-    $reply_to = '';
-    $ident = get_identities();
-    if(!isset($identity)) $identity = 0;
-    $full_name = $ident[$identity]['full_name'];
-    $from_mail = $ident[$identity]['email_address'];
-    $from_addr = '"'.$full_name.'" <'.$from_mail.'>';
-    $reply_to  = $ident[$identity]['reply_to'];
-
-    if (!$from_mail) {
-       $from_mail = "$popuser@$domain";
-       $from_addr = $from_mail;
-    }
     $rfc822_header->from = $rfc822_header->parseAddress($from_addr,true);
     if ($reply_to) {
        $rfc822_header->reply_to = $rfc822_header->parseAddress($reply_to,true);
@@ -227,7 +218,6 @@ function SendMDN ( $mailbox, $passed_id, $sender, $message, $imapConnection) {
     if ($original_recipient != '') {
         $report .= "Original-Recipient : $original_recipient\r\n";
     }
-    $final_recipient = $sender;
     $report .= "Final-Recipient: rfc822; $final_recipient\r\n" .
               "Original-Message-ID : $original_message_id\r\n" .
               "Disposition: manual-action/MDN-sent-manually; displayed\r\n";
@@ -916,13 +906,8 @@ $header = $message->header;
 
 if (isset($sendreceipt)) {
    if ( !$message->is_mdnsent ) {
-      $final_recipient = '';
-      if ((isset($identity)) && ($identity != 0)) //Main identity
-         $final_recipient = trim(getPref($data_dir, $username, 'email_address' . $identity, '' ));
-      if ($final_recipient == '' )
-         $final_recipient = trim(getPref($data_dir, $username, 'email_address', '' ));
       $supportMDN = ServerMDNSupport($aMailbox["PERMANENTFLAGS"]);
-      if ( SendMDN( $mailbox, $passed_id, $final_recipient, $message, $imapConnection ) > 0 && $supportMDN ) {
+      if ( SendMDN( $mailbox, $passed_id, $message, $imapConnection ) > 0 && $supportMDN ) {
          ToggleMDNflag( true, $imapConnection, $mailbox, $passed_id);
          $message->is_mdnsent = true;
          $aMailbox['MSG_HEADERS'][$passed_id]['MESSAGE_OBJECT'] = $message;