Thomas Bruederli
2012-11-17 be9aacaa5296dfca63fb3a01c2dc52538d1546aa
program/include/rcube_session.php
@@ -24,7 +24,8 @@
/**
 * Class to provide database supported session storage
 *
 * @package    Core
 * @package    Framework
 * @subpackage Core
 * @author     Thomas Bruederli <roundcube@gmail.com>
 * @author     Aleksander Machniak <alec@alec.pl>
 */
@@ -37,13 +38,12 @@
  private $unsets = array();
  private $gc_handlers = array();
  private $cookiename = 'roundcube_sessauth';
  private $vars = false;
  private $vars;
  private $key;
  private $now;
  private $secret = '';
  private $ip_check = false;
  private $logging = false;
  private $keep_alive = 0;
  private $memcache;
  /**
@@ -134,11 +134,10 @@
      $this->vars    = base64_decode($sql_arr['vars']);
      $this->key     = $key;
      if (!empty($this->vars))
        return $this->vars;
      return !empty($this->vars) ? (string) $this->vars : '';
    }
    return false;
    return null;
  }
@@ -157,7 +156,7 @@
    // no session row in DB (db_read() returns false)
    if (!$this->key) {
      $oldvars = false;
      $oldvars = null;
    }
    // use internal data from read() for fast requests (up to 0.5 sec.)
    else if ($key == $this->key && (!$this->vars || $ts - $this->start < 0.5)) {
@@ -167,7 +166,7 @@
      $oldvars = $this->db_read($key);
    }
    if ($oldvars !== false) {
    if ($oldvars !== null) {
      $newvars = $this->_fixvars($vars, $oldvars);
      if ($newvars !== $oldvars) {
@@ -197,7 +196,7 @@
   */
  private function _fixvars($vars, $oldvars)
  {
    if ($oldvars !== false) {
    if ($oldvars !== null) {
      $a_oldvars = $this->unserialize($oldvars);
      if (is_array($a_oldvars)) {
        foreach ((array)$this->unsets as $k)
@@ -219,13 +218,14 @@
   * Handler for session_destroy()
   *
   * @param string Session ID
   *
   * @return boolean True on success
   */
  public function db_destroy($key)
  {
    $this->db->query(
      sprintf("DELETE FROM %s WHERE sess_id = ?", $this->db->table_name('session')),
      $key);
    if ($key) {
      $this->db->query(sprintf("DELETE FROM %s WHERE sess_id = ?", $this->db->table_name('session')), $key);
    }
    return true;
  }
@@ -265,12 +265,12 @@
      $this->vars    = $arr['vars'];
      $this->key     = $key;
      if (!empty($this->vars))
        return $this->vars;
      return !empty($this->vars) ? (string) $this->vars : '';
    }
    return false;
    return null;
  }
  /**
   * Save session data.
@@ -286,30 +286,37 @@
    // no session data in cache (mc_read() returns false)
    if (!$this->key)
      $oldvars = false;
      $oldvars = null;
    // use internal data for fast requests (up to 0.5 sec.)
    else if ($key == $this->key && (!$this->vars || $ts - $this->start < 0.5))
      $oldvars = $this->vars;
    else // else read data again
      $oldvars = $this->mc_read($key);
    $newvars = $oldvars !== false ? $this->_fixvars($vars, $oldvars) : $vars;
    $newvars = $oldvars !== null ? $this->_fixvars($vars, $oldvars) : $vars;
    if ($newvars !== $oldvars || $ts - $this->changed > $this->lifetime / 2)
      return $this->memcache->set($key, serialize(array('changed' => time(), 'ip' => $this->ip, 'vars' => $newvars)), MEMCACHE_COMPRESSED, $this->lifetime);
    return true;
  }
  /**
   * Handler for session_destroy() with memcache backend
   *
   * @param string Session ID
   *
   * @return boolean True on success
   */
  public function mc_destroy($key)
  {
    return $this->memcache->delete($key);
    if ($key) {
      // #1488592: use 2nd argument
      $this->memcache->delete($key, 0);
    }
    return true;
  }
@@ -350,7 +357,7 @@
  {
    session_regenerate_id($destroy);
    $this->vars = false;
    $this->vars = null;
    $this->key  = session_id();
    return true;
@@ -373,13 +380,14 @@
    return true;
  }
  /**
   * Kill this session
   */
  public function kill()
  {
    $this->vars = false;
    $this->vars = null;
    $this->ip = $_SERVER['REMOTE_ADDR']; // update IP (might have changed)
    $this->destroy(session_id());
    rcube_utils::setcookie($this->cookiename, '-del-', time() - 60);
@@ -517,24 +525,6 @@
      $this->now = $now - ($now % ($this->lifetime / 2));
  }
  /**
   * Setter for keep_alive interval
   */
  public function set_keep_alive($keep_alive)
  {
    $this->keep_alive = $keep_alive;
    if ($this->lifetime < $keep_alive)
        $this->set_lifetime($keep_alive + 30);
  }
  /**
   * Getter for keep_alive interval
   */
  public function get_keep_alive()
  {
    return $this->keep_alive;
  }
  /**
   * Getter for remote IP saved with this session
@@ -543,7 +533,8 @@
  {
    return $this->ip;
  }
  /**
   * Setter for cookie encryption secret
   */
@@ -560,7 +551,8 @@
  {
    $this->ip_check = $check;
  }
  /**
   * Setter for the cookie name used for session cookie
   */
@@ -597,7 +589,7 @@
          $result = true;
        }
      }
   }
    }
    if (!$result)
      $this->log("Session authentication failed for " . $this->key . "; invalid auth cookie sent; timeslot = " . date('Y-m-d H:i:s', $prev));
@@ -629,7 +621,7 @@
  }
  /**
   *
   * Writes debug information to the log
   */
  function log($line)
  {