From 000bb9a55cdca43a7111eb3a5e655a28fa04d8a4 Mon Sep 17 00:00:00 2001
From: alecpl <alec@alec.pl>
Date: Sat, 13 Sep 2008 10:26:57 -0400
Subject: [PATCH] - localization fix for 'replyto' label length (use short 'replyto' and long 'reply-to')

---
 program/include/rcube_user.php |  103 ++++++++++++++++++++++++++++++++++-----------------
 1 files changed, 68 insertions(+), 35 deletions(-)

diff --git a/program/include/rcube_user.php b/program/include/rcube_user.php
index e125f63..a5dc73b 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:                                                              |
@@ -24,7 +24,7 @@
 /**
  * Class representing a system user
  *
- * @package    core
+ * @package    Core
  * @author     Thomas Bruederli <roundcube@gmail.com>
  */
 class rcube_user
@@ -59,17 +59,7 @@
     }
   }
 
-  /**
-   * PHP 4 object constructor
-   *
-   * @see  rcube_user::__construct
-   */
-  function rcube_user($id = null, $sql_arr = null)
-  {
-    $this->__construct($id, $sql_arr);
-  }
-  
-  
+
   /**
    * Build a user name string (as e-mail address)
    *
@@ -98,31 +88,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 +316,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 +362,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 +420,11 @@
   static function email2user($email)
   {
     $user = $email;
-    $r = rcmail_findinvirtual("^$email");
+    $r = self::findinvirtual('^' . quotemeta($email) . '[[:space:]]');
 
     for ($i=0; $i<count($r); $i++)
     {
-      $data = $r[$i];
+      $data = trim($r[$i]);
       $arr = preg_split('/\s+/', $data);
       if (count($arr) > 0)
       {
@@ -445,8 +445,8 @@
    */
   static function user2email($user)
   {
-    $email = "";
-    $r = rcmail_findinvirtual("$user$");
+    $email = '';
+    $r = self::findinvirtual('[[:space:]]' . quotemeta($user) . '[[:space:]]*$');
 
     for ($i=0; $i<count($r); $i++)
     {
@@ -461,6 +461,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