From 3412e50b54e3daac8745234e21ab6e72be0ed165 Mon Sep 17 00:00:00 2001
From: Thomas Bruederli <thomas@roundcube.net>
Date: Wed, 04 Jun 2014 11:20:33 -0400
Subject: [PATCH] Fix attachment menu structure and aria-attributes

---
 program/lib/Roundcube/rcube_session.php |   57 ++++++++++++++++++++++++++++++++++++++++++++++++---------
 1 files changed, 48 insertions(+), 9 deletions(-)

diff --git a/program/lib/Roundcube/rcube_session.php b/program/lib/Roundcube/rcube_session.php
index 615ec6f..caca262 100644
--- a/program/lib/Roundcube/rcube_session.php
+++ b/program/lib/Roundcube/rcube_session.php
@@ -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';
@@ -54,7 +55,7 @@
     {
         $this->db      = $db;
         $this->start   = microtime(true);
-        $this->ip      = $_SERVER['REMOTE_ADDR'];
+        $this->ip      = rcube_utils::remote_addr();
         $this->logging = $config->get('log_session', false);
 
         $lifetime = $config->get('session_lifetime', 1) * 60;
@@ -333,9 +334,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;
@@ -441,8 +442,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]);
     }
 
 
@@ -480,7 +492,7 @@
     public function kill()
     {
         $this->vars = null;
-        $this->ip = $_SERVER['REMOTE_ADDR']; // update IP (might have changed)
+        $this->ip = rcube_utils::remote_addr(); // update IP (might have changed)
         $this->destroy(session_id());
         rcube_utils::setcookie($this->cookiename, '-del-', time() - 60);
     }
@@ -491,13 +503,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]);
+                }
+            }
+        }
+
     }
 
     /**
@@ -694,10 +733,10 @@
     function check_auth()
     {
         $this->cookie = $_COOKIE[$this->cookiename];
-        $result = $this->ip_check ? $_SERVER['REMOTE_ADDR'] == $this->ip : true;
+        $result = $this->ip_check ? rcube_utils::remote_addr() == $this->ip : true;
 
         if (!$result) {
-            $this->log("IP check failed for " . $this->key . "; expected " . $this->ip . "; got " . $_SERVER['REMOTE_ADDR']);
+            $this->log("IP check failed for " . $this->key . "; expected " . $this->ip . "; got " . rcube_utils::remote_addr());
         }
 
         if ($result && $this->_mkcookie($this->now) != $this->cookie) {

--
Gitblit v1.9.1