From 2f4678908170b8af04a0eae2195bb3c0f732ed58 Mon Sep 17 00:00:00 2001
From: Aleksander Machniak <alec@alec.pl>
Date: Thu, 27 Nov 2014 04:08:55 -0500
Subject: [PATCH] Added rcube_user::list_emails() method with better performance than list_identities() which should be used always if we want only all user email addresses (with names)

---
 program/steps/settings/func.inc                 |    4 +-
 program/lib/Roundcube/rcube_user.php            |   57 +++++++++++++++++++---------
 plugins/new_user_identity/new_user_identity.php |    4 +-
 3 files changed, 43 insertions(+), 22 deletions(-)

diff --git a/plugins/new_user_identity/new_user_identity.php b/plugins/new_user_identity/new_user_identity.php
index f7d60d4..b905488 100644
--- a/plugins/new_user_identity/new_user_identity.php
+++ b/plugins/new_user_identity/new_user_identity.php
@@ -67,7 +67,7 @@
             return $args;
         }
 
-        $identities = $this->rc->user->list_identities();
+        $identities = $this->rc->user->list_emails();
         $ldap_entry = $this->lookup_user_name(array(
                 'user' => $this->rc->user->data['username'],
                 'host' => $this->rc->user->data['mail_host'],
@@ -75,7 +75,7 @@
 
         foreach ((array) $ldap_entry['email_list'] as $email) {
             foreach ($identities as $identity) {
-                if ($identity['email'] == $email ) {
+                if ($identity['email'] == $email) {
                     continue 2;
                 }
             }
diff --git a/program/lib/Roundcube/rcube_user.php b/program/lib/Roundcube/rcube_user.php
index b2110df..68156b1 100644
--- a/program/lib/Roundcube/rcube_user.php
+++ b/program/lib/Roundcube/rcube_user.php
@@ -51,6 +51,14 @@
      */
     private $identities = array();
 
+    /**
+     * Internal emails cache
+     *
+     * @var array
+     */
+    private $emails;
+
+
     const SEARCH_ADDRESSBOOK = 1;
     const SEARCH_MAIL = 2;
 
@@ -78,7 +86,6 @@
             $this->language = $sql_arr['language'];
         }
     }
-
 
     /**
      * Build a user name string (as e-mail address)
@@ -118,7 +125,6 @@
         return false;
     }
 
-
     /**
      * Get the preferences saved for this user
      *
@@ -153,7 +159,6 @@
 
         return $prefs;
     }
-
 
     /**
      * Write the given user prefs to the user's record
@@ -233,6 +238,31 @@
     }
 
     /**
+     * Return a list of all user emails (from identities)
+     *
+     * @return array List of emails (identity_id, name, email)
+     */
+    function list_emails()
+    {
+        if ($this->emails === null) {
+            $this->emails = array();
+
+            $sql_result = $this->db->query(
+                "SELECT `identity_id`, `name`, `email`"
+                ." FROM " . $this->db->table_name('identities', true)
+                ." WHERE `user_id` = ? AND `del` <> 1"
+                ." ORDER BY `standard` DESC, `name` ASC, `email` ASC, `identity_id` ASC",
+                $this->ID);
+
+            while ($sql_arr = $this->db->fetch_assoc($sql_result)) {
+                $this->emails[] = $sql_arr;
+            }
+        }
+
+        return $this->emails;
+    }
+
+    /**
      * Get default identity of this user
      *
      * @param  int   $id Identity ID. If empty, the default identity is returned
@@ -249,7 +279,6 @@
 
         return $this->identities[$id];
     }
-
 
     /**
      * Return a list of all identities linked with this user
@@ -286,7 +315,6 @@
         return $result;
     }
 
-
     /**
      * Update a specific identity record
      *
@@ -317,11 +345,12 @@
         call_user_func_array(array($this->db, 'query'),
             array_merge(array($sql), $query_params));
 
+        // clear the cache
         $this->identities = array();
+        $this->emails     = null;
 
         return $this->db->affected_rows();
     }
-
 
     /**
      * Create a new identity record linked with this user
@@ -351,11 +380,12 @@
         call_user_func_array(array($this->db, 'query'),
             array_merge(array($sql), $insert_values));
 
+        // clear the cache
         $this->identities = array();
+        $this->emails     = null;
 
         return $this->db->insert_id('identities');
     }
-
 
     /**
      * Mark the given identity as deleted
@@ -387,11 +417,12 @@
             $this->ID,
             $iid);
 
+        // clear the cache
         $this->identities = array();
+        $this->emails     = null;
 
         return $this->db->affected_rows();
     }
-
 
     /**
      * Make this identity the default one for this user
@@ -412,7 +443,6 @@
         }
     }
 
-
     /**
      * Update user's last_login timestamp
      */
@@ -427,7 +457,6 @@
         }
     }
 
-
     /**
      * Clear the saved object state
      */
@@ -436,7 +465,6 @@
         $this->ID = null;
         $this->data = null;
     }
-
 
     /**
      * Find a user record matching the given name and host
@@ -472,7 +500,6 @@
         else
             return false;
     }
-
 
     /**
      * Create a new user record and return a rcube_user instance
@@ -587,7 +614,6 @@
         return $user_id ? $user_instance : false;
     }
 
-
     /**
      * Resolve username using a virtuser plugins
      *
@@ -602,7 +628,6 @@
 
         return $plugin['user'];
     }
-
 
     /**
      * Resolve e-mail address from virtuser plugins
@@ -621,7 +646,6 @@
 
         return empty($plugin['email']) ? NULL : $plugin['email'];
     }
-
 
     /**
      * Return a list of saved searches linked with this user
@@ -654,7 +678,6 @@
 
         return $result;
     }
-
 
     /**
      * Return saved search data.
@@ -690,7 +713,6 @@
         return null;
     }
 
-
     /**
      * Deletes given saved search record
      *
@@ -711,7 +733,6 @@
 
         return $this->db->affected_rows();
     }
-
 
     /**
      * Create a new saved search record linked with this user
diff --git a/program/steps/settings/func.inc b/program/steps/settings/func.inc
index 3ec3185..887aebc 100644
--- a/program/steps/settings/func.inc
+++ b/program/steps/settings/func.inc
@@ -94,9 +94,9 @@
     }
 
     // get identities list and define 'mail' column
-    $list = $RCMAIL->user->list_identities();
+    $list = $RCMAIL->user->list_emails();
     foreach ($list as $idx => $row) {
-        $list[$idx]['mail'] = trim($row['name'] . ' <' . rcube_utils::idn_to_utf8($row['email']) .'>');
+        $list[$idx]['mail'] = trim($row['name'] . ' <' . rcube_utils::idn_to_utf8($row['email']) . '>');
     }
 
     // get all identites from DB and define list of cols to be displayed

--
Gitblit v1.9.1