From ba0e7871bd85b631800bff13fc1a0a20f77751c5 Mon Sep 17 00:00:00 2001
From: thomascube <thomas@roundcube.net>
Date: Thu, 07 Aug 2008 04:52:22 -0400
Subject: [PATCH] Separate queries to make use of the database indexes; with OR no index is used

---
 program/include/rcube_user.php |   87 ++++++++++++++++++++++++++++++++-----------
 1 files changed, 65 insertions(+), 22 deletions(-)

diff --git a/program/include/rcube_user.php b/program/include/rcube_user.php
index c808d07..a57d671 100644
--- a/program/include/rcube_user.php
+++ b/program/include/rcube_user.php
@@ -5,7 +5,7 @@
  | program/include/rcube_user.inc                                        |
  |                                                                       |
  | This file is part of the RoundCube Webmail client                     |
- | Copyright (C) 2005-2007, RoundCube Dev. - Switzerland                 |
+ | Copyright (C) 2005-2008, RoundCube Dev. - Switzerland                 |
  | Licensed under the GNU GPL                                            |
  |                                                                       |
  | PURPOSE:                                                              |
@@ -98,31 +98,39 @@
   /**
    * Write the given user prefs to the user's record
    *
-   * @param mixed User prefs to save
+   * @param array User prefs to save
    * @return boolean True on success, False on failure
    */
   function save_prefs($a_user_prefs)
   {
     if (!$this->ID)
       return false;
+      
+    $config = rcmail::get_instance()->config;
+    $old_prefs = (array)$this->get_prefs();
 
     // merge (partial) prefs array with existing settings
-    $a_user_prefs += (array)$this->get_prefs();
-    unset($a_user_prefs['language']);
-
+    $save_prefs = $a_user_prefs + $old_prefs;
+    unset($save_prefs['language']);
+    
+    // don't save prefs with default values if they haven't been changed yet
+    foreach ($a_user_prefs as $key => $value) {
+      if (!isset($old_prefs[$key]) && ($value == $config->get($key)))
+        unset($save_prefs[$key]);
+    }
+    
     $this->db->query(
       "UPDATE ".get_table_name('users')."
        SET    preferences=?,
               language=?
        WHERE  user_id=?",
-      serialize($a_user_prefs),
+      serialize($save_prefs),
       $_SESSION['language'],
       $this->ID);
 
     $this->language = $_SESSION['language'];
-    if ($this->db->affected_rows())
-    {
-      rcmail::get_instance()->config->merge($a_user_prefs);
+    if ($this->db->affected_rows()) {
+      $config->merge($a_user_prefs);
       return true;
     }
 
@@ -318,16 +326,18 @@
   {
     $dbh = rcmail::get_instance()->get_dbh();
     
-    // query if user already registered
-    $sql_result = $dbh->query(
-      "SELECT * FROM ".get_table_name('users')."
-       WHERE  mail_host=? AND (username=? OR alias=?)",
-      $host,
-      $user,
-      $user);
-      
+    // query for matching user name
+    $query = "SELECT * FROM ".get_table_name('users')." WHERE mail_host=? AND %s=?";
+    $sql_result = $dbh->query(sprintf($query, 'username'), $host, $user);
+    
+    // query for matching alias
+    if (!($sql_arr = $dbh->fetch_assoc($sql_result))) {
+      $sql_result = $dbh->query(sprintf($query, 'alias'), $host, $user);
+      $sql_arr = $dbh->fetch_assoc($sql_result);
+    }
+    
     // user already registered -> overwrite username
-    if ($sql_arr = $dbh->fetch_assoc($sql_result))
+    if ($sql_arr)
       return new rcube_user($sql_arr['user_id'], $sql_arr);
     else
       return false;
@@ -362,7 +372,7 @@
 
     if ($user_id = $dbh->insert_id(get_sequence_name('users')))
     {
-      $mail_domain = rcmail_mail_domain($host);
+      $mail_domain = $rcmail->config->mail_domain($host);
 
       if ($user_email=='')
         $user_email = strpos($user, '@') ? $user : sprintf('%s@%s', $user, $mail_domain);
@@ -420,11 +430,11 @@
   static function email2user($email)
   {
     $user = $email;
-    $r = rcmail_findinvirtual("^$email");
+    $r = self::findinvirtual("^$email\s");
 
     for ($i=0; $i<count($r); $i++)
     {
-      $data = $r[$i];
+      $data = trim($r[$i]);
       $arr = preg_split('/\s+/', $data);
       if (count($arr) > 0)
       {
@@ -446,7 +456,7 @@
   static function user2email($user)
   {
     $email = "";
-    $r = rcmail_findinvirtual("$user$");
+    $r = self::findinvirtual("\s$user\s*$");
 
     for ($i=0; $i<count($r); $i++)
     {
@@ -461,6 +471,39 @@
 
     return $email;
   }
+  
+  
+  /**
+   * Find matches of the given pattern in virtuser table
+   * 
+   * @param string Regular expression to search for
+   * @return array Matching entries
+   */
+  private static function findinvirtual($pattern)
+  {
+    $result = array();
+    $virtual = null;
+    
+    if ($virtuser_file = rcmail::get_instance()->config->get('virtuser_file'))
+      $virtual = file($virtuser_file);
+    
+    if (empty($virtual))
+      return $result;
+    
+    // check each line for matches
+    foreach ($virtual as $line)
+    {
+      $line = trim($line);
+      if (empty($line) || $line{0}=='#')
+        continue;
+        
+      if (eregi($pattern, $line))
+        $result[] = $line;
+    }
+    
+    return $result;
+  }
+
 
 }
 

--
Gitblit v1.9.1