thomascube
2011-01-12 f5e7b353079f182d6d57e05b37e1eeb32872bc1a
program/include/rcube_session.php
@@ -4,8 +4,8 @@
 +-----------------------------------------------------------------------+
 | program/include/rcube_session.php                                     |
 |                                                                       |
 | This file is part of the RoundCube Webmail client                     |
 | Copyright (C) 2005-2009, RoundCube Dev. - Switzerland                 |
 | This file is part of the Roundcube Webmail client                     |
 | Copyright (C) 2005-2010, The Roundcube Dev Team                       |
 | Licensed under the GNU GPL                                            |
 |                                                                       |
 | PURPOSE:                                                              |
@@ -20,6 +20,13 @@
*/
/**
 * Class to provide database supported session storage
 *
 * @package    Core
 * @author     Thomas Bruederli <roundcube@gmail.com>
 * @author     Aleksander Machniak <alec@alec.pl>
 */
class rcube_session
{
  private $db;
@@ -74,17 +81,17 @@
    if ($sql_arr = $this->db->fetch_assoc($sql_result)) {
      $this->changed = $sql_arr['changed'];
      $this->vars = $sql_arr['vars'];
      $this->ip = $sql_arr['ip'];
      $this->key = $key;
      $this->ip      = $sql_arr['ip'];
      $this->vars    = base64_decode($sql_arr['vars']);
      $this->key     = $key;
      if (!empty($sql_arr['vars']))
        return $sql_arr['vars'];
      if (!empty($this->vars))
        return $this->vars;
    }
    return false;
  }
  // save session data
  public function write($key, $vars)
@@ -98,35 +105,41 @@
    } else { // else read data again from DB
      $oldvars = $this->read($key);
    }
    if ($oldvars !== false) {
      $a_oldvars = $this->unserialize($oldvars);
      foreach ((array)$this->unsets as $k)
        unset($a_oldvars[$k]);
      $a_oldvars = $this->unserialize($oldvars);
      if (is_array($a_oldvars)) {
        foreach ((array)$this->unsets as $k)
          unset($a_oldvars[$k]);
      $newvars = $this->serialize(array_merge(
        (array)$a_oldvars, (array)$this->unserialize($vars)));
        $newvars = $this->serialize(array_merge(
          (array)$a_oldvars, (array)$this->unserialize($vars)));
      }
      else
        $newvars = $vars;
      if ($this->keep_alive>0) {
   $timeout = min($this->lifetime * 0.5,
             $this->lifetime - $this->keep_alive);
      if (!$this->lifetime) {
        $timeout = 600;
      }
      else if ($this->keep_alive>0) {
        $timeout = min($this->lifetime * 0.5, $this->lifetime - $this->keep_alive);
      } else {
   $timeout = 0;
        $timeout = 0;
      }
      if (!($newvars === $oldvars) || ($ts - $this->changed > $timeout)) {
        $this->db->query(
     sprintf("UPDATE %s SET vars = ?, changed = %s WHERE sess_id = ?",
       get_table_name('session'), $now),
     $newvars, $key);
          sprintf("UPDATE %s SET vars = ?, changed = %s WHERE sess_id = ?",
            get_table_name('session'), $now),
          base64_encode($newvars), $key);
      }
    }
    else {
      $this->db->query(
        sprintf("INSERT INTO %s (sess_id, vars, ip, created, changed) ".
          "VALUES (?, ?, ?, %s, %s)",
     get_table_name('session'), $now, $now),
        $key, $vars, (string)$_SERVER['REMOTE_ADDR']);
          get_table_name('session'), $now, $now),
        $key, base64_encode($vars), (string)$_SERVER['REMOTE_ADDR']);
    }
    $this->unsets = array();