From 3f4521bcf4b538b6ac54817cfad22b51e347546d Mon Sep 17 00:00:00 2001
From: Aleksander Machniak <alec@alec.pl>
Date: Wed, 17 Jun 2015 03:03:03 -0400
Subject: [PATCH] Fix so plain text signature field uses monospace font (#1490435)

---
 program/lib/Roundcube/rcube_session.php |  101 +++++++++++++++++++++++++++++++++++++++-----------
 1 files changed, 79 insertions(+), 22 deletions(-)

diff --git a/program/lib/Roundcube/rcube_session.php b/program/lib/Roundcube/rcube_session.php
index 646933b..d15d146 100644
--- a/program/lib/Roundcube/rcube_session.php
+++ b/program/lib/Roundcube/rcube_session.php
@@ -3,7 +3,7 @@
 /*
  +-----------------------------------------------------------------------+
  | This file is part of the Roundcube Webmail client                     |
- | Copyright (C) 2005-2012, The Roundcube Dev Team                       |
+ | Copyright (C) 2005-2014, The Roundcube Dev Team                       |
  | Copyright (C) 2011, Kolab Systems AG                                  |
  |                                                                       |
  | Licensed under the GNU General Public License version 3 or            |
@@ -34,6 +34,7 @@
     private $changed;
     private $time_diff = 0;
     private $reloaded = false;
+    private $appends = array();
     private $unsets = array();
     private $gc_handlers = array();
     private $cookiename = 'roundcube_sessauth';
@@ -45,6 +46,13 @@
     private $logging = false;
     private $storage;
     private $memcache;
+
+    /**
+     * Blocks session data from being written to database.
+     * Can be used if write-race conditions are to be expected
+     * @var boolean
+     */
+    public $nowrite = false;
 
 
     /**
@@ -95,6 +103,8 @@
                 array($this, 'db_write'),
                 array($this, 'db_destroy'),
                 array($this, 'gc'));
+
+            $this->table_name = $this->db->table_name('session', true);
         }
     }
 
@@ -134,7 +144,15 @@
      */
     public function destroy($key)
     {
-        return $this->memcache ? $this->mc_destroy($key) : $this->db_destroy($key);
+        if ($this->storage == 'memcache' && $this->memcache) {
+            return $this->mc_destroy($key);
+        }
+        else if ($this->storage == 'db') {
+            return $this->db_destroy($key);
+        }
+        else {
+            return true;
+        }
     }
 
 
@@ -167,9 +185,8 @@
     public function db_read($key)
     {
         $sql_result = $this->db->query(
-            "SELECT vars, ip, changed, " . $this->db->now() . " AS ts"
-            . " FROM " . $this->db->table_name('session')
-            . " WHERE sess_id = ?", $key);
+            "SELECT `vars`, `ip`, `changed`, " . $this->db->now() . " AS ts"
+            . " FROM {$this->table_name} WHERE `sess_id` = ?", $key);
 
         if ($sql_result && ($sql_arr = $this->db->fetch_assoc($sql_result))) {
             $this->time_diff = time() - strtotime($sql_arr['ts']);
@@ -196,9 +213,11 @@
      */
     public function db_write($key, $vars)
     {
-        $now   = $this->db->now();
-        $table = $this->db->table_name('session');
-        $ts    = microtime(true);
+        $now = $this->db->now();
+        $ts  = microtime(true);
+
+        if ($this->nowrite)
+            return true;
 
         // no session row in DB (db_read() returns false)
         if (!$this->key) {
@@ -216,17 +235,18 @@
             $newvars = $this->_fixvars($vars, $oldvars);
 
             if ($newvars !== $oldvars) {
-                $this->db->query("UPDATE $table "
-                    . "SET changed = $now, vars = ? WHERE sess_id = ?",
+                $this->db->query("UPDATE {$this->table_name} "
+                    . "SET `changed` = $now, `vars` = ? WHERE `sess_id` = ?",
                     base64_encode($newvars), $key);
             }
             else if ($ts - $this->changed + $this->time_diff > $this->lifetime / 2) {
-                $this->db->query("UPDATE $table SET changed = $now"
-                    . " WHERE sess_id = ?", $key);
+                $this->db->query("UPDATE {$this->table_name} SET `changed` = $now"
+                    . " WHERE `sess_id` = ?", $key);
             }
         }
         else {
-            $this->db->query("INSERT INTO $table (sess_id, vars, ip, created, changed)"
+            $this->db->query("INSERT INTO {$this->table_name}"
+                . " (`sess_id`, `vars`, `ip`, `created`, `changed`)"
                 . " VALUES (?, ?, ?, $now, $now)",
                 $key, base64_encode($vars), (string)$this->ip);
         }
@@ -279,8 +299,7 @@
     public function db_destroy($key)
     {
         if ($key) {
-            $this->db->query(sprintf("DELETE FROM %s WHERE sess_id = ?",
-                $this->db->table_name('session')), $key);
+            $this->db->query("DELETE FROM {$this->table_name} WHERE `sess_id` = ?", $key);
         }
 
         return true;
@@ -333,9 +352,9 @@
 
         $newvars = $oldvars !== null ? $this->_fixvars($vars, $oldvars) : $vars;
 
-        if ($newvars !== $oldvars || $ts - $this->changed > $this->lifetime / 2) {
+        if ($newvars !== $oldvars || $ts - $this->changed > $this->lifetime / 3) {
             return $this->memcache->set($key, serialize(array('changed' => time(), 'ip' => $this->ip, 'vars' => $newvars)),
-                MEMCACHE_COMPRESSED, $this->lifetime);
+                MEMCACHE_COMPRESSED, $this->lifetime + 60);
         }
 
         return true;
@@ -396,8 +415,8 @@
         if ($this->gc_enabled) {
             // just delete all expired sessions
             if ($this->storage == 'db') {
-                $this->db->query("DELETE FROM " . $this->db->table_name('session')
-                    . " WHERE changed < " . $this->db->now(-$this->gc_enabled));
+                $this->db->query("DELETE FROM {$this->table_name}"
+                    . " WHERE `changed` < " . $this->db->now(-$this->gc_enabled));
             }
 
             foreach ($this->gc_handlers as $fct) {
@@ -441,8 +460,19 @@
 
         $node = &$this->get_node(explode('.', $path), $_SESSION);
 
-        if ($key !== null) $node[$key] = $value;
-        else               $node[] = $value;
+        if ($key !== null) {
+            $node[$key] = $value;
+            $path .= '.' . $key;
+        }
+        else {
+            $node[] = $value;
+        }
+
+        $this->appends[] = $path;
+
+        // when overwriting a previously unset variable
+        if ($this->unsets[$path])
+            unset($this->unsets[$path]);
     }
 
 
@@ -491,13 +521,40 @@
      */
     public function reload()
     {
+        // collect updated data from previous appends
+        $merge_data = array();
+        foreach ((array)$this->appends as $var) {
+            $path = explode('.', $var);
+            $value = $this->get_node($path, $_SESSION);
+            $k = array_pop($path);
+            $node = &$this->get_node($path, $merge_data);
+            $node[$k] = $value;
+        }
+
         if ($this->key && $this->memcache)
             $data = $this->mc_read($this->key);
         else if ($this->key)
             $data = $this->db_read($this->key);
 
-        if ($data)
+        if ($data) {
             session_decode($data);
+
+            // apply appends and unsets to reloaded data
+            $_SESSION = array_merge_recursive($_SESSION, $merge_data);
+
+            foreach ((array)$this->unsets as $var) {
+                if (isset($_SESSION[$var])) {
+                    unset($_SESSION[$var]);
+                }
+                else {
+                    $path = explode('.', $var);
+                    $k = array_pop($path);
+                    $node = &$this->get_node($path, $_SESSION);
+                    unset($node[$k]);
+                }
+            }
+        }
+
     }
 
     /**

--
Gitblit v1.9.1