From bfe6570cb007511d727f81fcb0a985bf2d0665d4 Mon Sep 17 00:00:00 2001
From: Aleksander Machniak <alec@alec.pl>
Date: Thu, 21 May 2015 04:26:12 -0400
Subject: [PATCH] Fix issues when using moduserprefs.sh without --user argument (#1490399)

---
 CHANGELOG                            |    1 +
 bin/moduserprefs.sh                  |    2 +-
 program/lib/Roundcube/rcube_user.php |   27 ++++++++++++++++++---------
 3 files changed, 20 insertions(+), 10 deletions(-)

diff --git a/CHANGELOG b/CHANGELOG
index 6ff97c8..f3cffad 100644
--- a/CHANGELOG
+++ b/CHANGELOG
@@ -12,6 +12,7 @@
 - Fix security issue in contact photo handling (#1490379)
 - Fix bug where database_attachments_cache setting was not working
 - Fix attached file path unsetting in database_attachments plugin (#1490393)
+- Fix issues when using moduserprefs.sh without --user argument (#1490399)
 
 RELEASE 1.0.5
 -------------
diff --git a/bin/moduserprefs.sh b/bin/moduserprefs.sh
index 9bbc885..023c54a 100755
--- a/bin/moduserprefs.sh
+++ b/bin/moduserprefs.sh
@@ -71,7 +71,7 @@
 	$prefs[$pref_name] = $pref_value;
 
 	if ($prefs != $old_prefs) {
-		$user->save_prefs($prefs);
+		$user->save_prefs($prefs, true);
 		echo "saved.\n";
 	}
 	else {
diff --git a/program/lib/Roundcube/rcube_user.php b/program/lib/Roundcube/rcube_user.php
index e232736..1613e52 100644
--- a/program/lib/Roundcube/rcube_user.php
+++ b/program/lib/Roundcube/rcube_user.php
@@ -158,9 +158,11 @@
      * Write the given user prefs to the user's record
      *
      * @param array $a_user_prefs User prefs to save
+     * @param bool  $no_session   Simplified language/preferences handling
+     *
      * @return boolean True on success, False on failure
      */
-    function save_prefs($a_user_prefs)
+    function save_prefs($a_user_prefs, $no_session = false)
     {
         if (!$this->ID)
             return false;
@@ -187,6 +189,9 @@
         }
 
         $save_prefs = serialize($save_prefs);
+        if (!$no_session) {
+            $this->language = $_SESSION['language'];
+        }
 
         $this->db->query(
             "UPDATE ".$this->db->table_name('users').
@@ -194,26 +199,30 @@
                 ", language = ?".
             " WHERE user_id = ?",
             $save_prefs,
-            $_SESSION['language'],
+            $this->language,
             $this->ID);
-
-        $this->language = $_SESSION['language'];
 
         // Update success
         if ($this->db->affected_rows() !== false) {
-            $config->set_user_prefs($a_user_prefs);
             $this->data['preferences'] = $save_prefs;
 
-            if (isset($_SESSION['preferences'])) {
-                $this->rc->session->remove('preferences');
-                $this->rc->session->remove('preferences_time');
+            if (!$no_session) {
+                $config->set_user_prefs($a_user_prefs);
+
+                if (isset($_SESSION['preferences'])) {
+                    $this->rc->session->remove('preferences');
+                    $this->rc->session->remove('preferences_time');
+                }
             }
+
             return true;
         }
         // Update error, but we are using replication (we have read-only DB connection)
         // and we are storing session not in the SQL database
         // we can store preferences in session and try to write later (see get_prefs())
-        else if ($this->db->is_replicated() && $config->get('session_storage', 'db') != 'db') {
+        else if (!$no_session && $this->db->is_replicated()
+            && $config->get('session_storage', 'db') != 'db'
+        ) {
             $_SESSION['preferences'] = $save_prefs;
             $_SESSION['preferences_time'] = time();
             $config->set_user_prefs($a_user_prefs);

--
Gitblit v1.9.1