Aleksander Machniak
2012-11-28 0247b89c38c7f7ef1a2111239c6a1c8c13394d93
Move code for identity selection to function, move identities formatting to rcube_user::list_identities()
2 files modified
200 ■■■■ changed files
program/lib/Roundcube/rcube_user.php 15 ●●●● patch | view | raw | blame | history
program/steps/mail/compose.inc 185 ●●●● patch | view | raw | blame | history
program/lib/Roundcube/rcube_user.php
@@ -240,10 +240,12 @@
    /**
     * Return a list of all identities linked with this user
     *
     * @param string $sql_add Optional WHERE clauses
     * @param string $sql_add   Optional WHERE clauses
     * @param bool   $formatted Format identity email and name
     *
     * @return array List of identities
     */
    function list_identities($sql_add = '')
    function list_identities($sql_add = '', $formatted = false)
    {
        $result = array();
@@ -255,6 +257,15 @@
            $this->ID);
        while ($sql_arr = $this->db->fetch_assoc($sql_result)) {
            if ($formatted) {
                $ascii_email = format_email($sql_arr['email']);
                $utf8_email  = format_email(rcube_utils::idn_to_utf8($ascii_email));
                $sql_arr['email_ascii'] = $ascii_email;
                $sql_arr['email']       = $utf8_email;
                $sql_arr['ident']       = format_email_recipient($ascii_email, $ident['name']);
            }
            $result[] = $sql_arr;
        }
program/steps/mail/compose.inc
@@ -249,18 +249,7 @@
$MESSAGE->compose = array();
// get user's identities
$MESSAGE->identities = $RCMAIL->user->list_identities();
if (count($MESSAGE->identities))
{
  foreach ($MESSAGE->identities as $idx => $ident) {
    $ident['email'] = format_email($ident['email']);
    $email = format_email(rcube_idn_to_utf8($ident['email']));
    $MESSAGE->identities[$idx]['email_ascii'] = $ident['email'];
    $MESSAGE->identities[$idx]['ident']       = format_email_recipient($ident['email'], $ident['name']);
    $MESSAGE->identities[$idx]['email']       = $email;
  }
}
$MESSAGE->identities = $RCMAIL->user->list_identities(null, true);
// Set From field value
if (!empty($_POST['_from'])) {
@@ -270,92 +259,10 @@
  $MESSAGE->compose['from'] = $COMPOSE['param']['from'];
}
else if (count($MESSAGE->identities)) {
  $a_recipients = array();
  $a_names      = array();
  // extract all recipients of the reply-message
  if (is_object($MESSAGE->headers) && in_array($compose_mode, array(RCUBE_COMPOSE_REPLY, RCUBE_COMPOSE_FORWARD)))
  {
    $a_to = rcube_mime::decode_address_list($MESSAGE->headers->to, null, true, $MESSAGE->headers->charset);
    foreach ($a_to as $addr) {
      if (!empty($addr['mailto'])) {
        $a_recipients[] = format_email($addr['mailto']);
        $a_names[]      = $addr['name'];
      }
    }
    if (!empty($MESSAGE->headers->cc)) {
      $a_cc = rcube_mime::decode_address_list($MESSAGE->headers->cc, null, true, $MESSAGE->headers->charset);
      foreach ($a_cc as $addr) {
        if (!empty($addr['mailto'])) {
          $a_recipients[] = format_email($addr['mailto']);
          $a_names[]      = $addr['name'];
        }
      }
    }
  }
  $from_idx         = null;
  $found_idx        = null;
  $default_identity = 0; // default identity is always first on the list
  // Select identity
  foreach ($MESSAGE->identities as $idx => $ident) {
    // use From header
    if (in_array($compose_mode, array(RCUBE_COMPOSE_DRAFT, RCUBE_COMPOSE_EDIT))) {
      if ($MESSAGE->headers->from == $ident['ident']) {
        $from_idx = $idx;
        break;
      }
    }
    // reply to yourself
    else if ($compose_mode == RCUBE_COMPOSE_REPLY && $MESSAGE->headers->from == $ident['ident']) {
      $from_idx = $idx;
      break;
    }
    // use replied message recipients
    else if (($found = array_search($ident['email_ascii'], $a_recipients)) !== false) {
      if ($found_idx === null) {
        $found_idx = $idx;
      }
      // match identity name
      if ($a_names[$found] && $ident['name'] && $a_names[$found] == $ident['name']) {
        $from_idx = $idx;
        break;
      }
    }
  }
  // If matching by name+address doesn't found any amtches, get first found address (identity)
  if ($from_idx === null) {
    $from_idx = $found_idx;
  }
  // Try Return-Path
  if ($from_idx === null && ($return_path = $MESSAGE->headers->others['return-path'])) {
    foreach ($MESSAGE->identities as $idx => $ident) {
      if (strpos($return_path, str_replace('@', '=', $ident['email_ascii']).'@') !== false) {
        $from_idx = $idx;
        break;
      }
    }
  }
  // Fallback using Delivered-To
  if ($from_idx === null && ($delivered_to = $MESSAGE->headers->others['delivered-to'])) {
    foreach ($MESSAGE->identities as $idx => $ident) {
      if (in_array($ident['email_ascii'], $delivered_to)) {
        $from_idx = $idx;
        break;
      }
    }
  }
  $ident   = $MESSAGE->identities[$from_idx !== null ? $from_idx : $default_identity];
  $from_id = $ident['identity_id'];
  $ident = rcmail_identity_select($MESSAGE, $MESSAGE->identities, $compose_mode);
  $MESSAGE->compose['from_email'] = $ident['email'];
  $MESSAGE->compose['from']       = $from_id;
  $MESSAGE->compose['from']       = $ident['identity_id'];
}
// Set other headers
@@ -472,6 +379,92 @@
/****** compose mode functions ********/
function rcmail_identity_select($MESSAGE, $identities, $compose_mode)
{
  $a_recipients = array();
  $a_names      = array();
  // extract all recipients of the reply-message
  if (is_object($MESSAGE->headers) && in_array($compose_mode, array(RCUBE_COMPOSE_REPLY, RCUBE_COMPOSE_FORWARD))) {
    $a_to = rcube_mime::decode_address_list($MESSAGE->headers->to, null, true, $MESSAGE->headers->charset);
    foreach ($a_to as $addr) {
      if (!empty($addr['mailto'])) {
        $a_recipients[] = format_email($addr['mailto']);
        $a_names[]      = $addr['name'];
      }
    }
    if (!empty($MESSAGE->headers->cc)) {
      $a_cc = rcube_mime::decode_address_list($MESSAGE->headers->cc, null, true, $MESSAGE->headers->charset);
      foreach ($a_cc as $addr) {
        if (!empty($addr['mailto'])) {
          $a_recipients[] = format_email($addr['mailto']);
          $a_names[]      = $addr['name'];
        }
      }
    }
  }
  $from_idx         = null;
  $found_idx        = null;
  $default_identity = 0; // default identity is always first on the list
  // Select identity
  foreach ($identities as $idx => $ident) {
    // use From header
    if (in_array($compose_mode, array(RCUBE_COMPOSE_DRAFT, RCUBE_COMPOSE_EDIT))) {
      if ($MESSAGE->headers->from == $ident['ident']) {
        $from_idx = $idx;
        break;
      }
    }
    // reply to yourself
    else if ($compose_mode == RCUBE_COMPOSE_REPLY && $MESSAGE->headers->from == $ident['ident']) {
      $from_idx = $idx;
      break;
    }
    // use replied message recipients
    else if (($found = array_search($ident['email_ascii'], $a_recipients)) !== false) {
      if ($found_idx === null) {
        $found_idx = $idx;
      }
      // match identity name
      if ($a_names[$found] && $ident['name'] && $a_names[$found] == $ident['name']) {
        $from_idx = $idx;
        break;
      }
    }
  }
  // If matching by name+address doesn't found any amtches, get first found address (identity)
  if ($from_idx === null) {
    $from_idx = $found_idx;
  }
  // Try Return-Path
  if ($from_idx === null && ($return_path = $MESSAGE->headers->others['return-path'])) {
    foreach ($identities as $idx => $ident) {
      if (strpos($return_path, str_replace('@', '=', $ident['email_ascii']).'@') !== false) {
        $from_idx = $idx;
        break;
      }
    }
  }
  // Fallback using Delivered-To
  if ($from_idx === null && ($delivered_to = $MESSAGE->headers->others['delivered-to'])) {
    foreach ($identities as $idx => $ident) {
      if (in_array($ident['email_ascii'], $delivered_to)) {
        $from_idx = $idx;
        break;
      }
    }
  }
  return $identities[$from_idx !== null ? $from_idx : $default_identity];
}
function rcmail_compose_headers($attrib)
{
  global $MESSAGE;