| | |
| | | private $ip; |
| | | private $start; |
| | | private $changed; |
| | | private $time_diff = 0; |
| | | private $reloaded = false; |
| | | private $unsets = array(); |
| | | private $gc_handlers = array(); |
| | |
| | | array($this, 'db_read'), |
| | | array($this, 'db_write'), |
| | | array($this, 'db_destroy'), |
| | | array($this, 'db_gc')); |
| | | array($this, 'gc')); |
| | | } |
| | | } |
| | | |
| | |
| | | } |
| | | |
| | | session_write_close(); |
| | | |
| | | // write_close() is called on script shutdown, see rcube::shutdown() |
| | | // execute cleanup functionality if enabled by session gc handler |
| | | // we do this after closing the session for better performance |
| | | $this->gc_shutdown(); |
| | | } |
| | | |
| | | |
| | |
| | | public function db_read($key) |
| | | { |
| | | $sql_result = $this->db->query( |
| | | "SELECT vars, ip, changed FROM ".$this->db->table_name('session') |
| | | ." WHERE sess_id = ?", $key); |
| | | "SELECT vars, ip, changed, " . $this->db->now() . " AS ts" |
| | | . " FROM " . $this->db->table_name('session') |
| | | . " WHERE sess_id = ?", $key); |
| | | |
| | | if ($sql_result && ($sql_arr = $this->db->fetch_assoc($sql_result))) { |
| | | $this->changed = strtotime($sql_arr['changed']); |
| | | $this->ip = $sql_arr['ip']; |
| | | $this->vars = base64_decode($sql_arr['vars']); |
| | | $this->key = $key; |
| | | $this->time_diff = time() - strtotime($sql_arr['ts']); |
| | | $this->changed = strtotime($sql_arr['changed']); |
| | | $this->ip = $sql_arr['ip']; |
| | | $this->vars = base64_decode($sql_arr['vars']); |
| | | $this->key = $key; |
| | | |
| | | return !empty($this->vars) ? (string) $this->vars : ''; |
| | | } |
| | |
| | | */ |
| | | public function db_write($key, $vars) |
| | | { |
| | | $ts = microtime(true); |
| | | $now = $this->db->fromunixtime((int)$ts); |
| | | $now = $this->db->now(); |
| | | $table = $this->db->table_name('session'); |
| | | $ts = microtime(true); |
| | | |
| | | // no session row in DB (db_read() returns false) |
| | | if (!$this->key) { |
| | |
| | | $newvars = $this->_fixvars($vars, $oldvars); |
| | | |
| | | if ($newvars !== $oldvars) { |
| | | $this->db->query( |
| | | sprintf("UPDATE %s SET vars=?, changed=%s WHERE sess_id=?", |
| | | $this->db->table_name('session'), $now), |
| | | base64_encode($newvars), $key); |
| | | $this->db->query("UPDATE $table " |
| | | . "SET changed = $now, vars = ? WHERE sess_id = ?", |
| | | base64_encode($newvars), $key); |
| | | } |
| | | else if ($ts - $this->changed > $this->lifetime / 2) { |
| | | $this->db->query("UPDATE ".$this->db->table_name('session') |
| | | ." SET changed=$now WHERE sess_id=?", $key); |
| | | else if ($ts - $this->changed + $this->time_diff > $this->lifetime / 2) { |
| | | $this->db->query("UPDATE $table SET changed = $now" |
| | | . " WHERE sess_id = ?", $key); |
| | | } |
| | | } |
| | | else { |
| | | $this->db->query( |
| | | sprintf("INSERT INTO %s (sess_id, vars, ip, created, changed) ". |
| | | "VALUES (?, ?, ?, %s, %s)", |
| | | $this->db->table_name('session'), $now, $now), |
| | | $key, base64_encode($vars), (string)$this->ip); |
| | | $this->db->query("INSERT INTO $table (sess_id, vars, ip, created, changed)" |
| | | . " VALUES (?, ?, ?, $now, $now)", |
| | | $key, base64_encode($vars), (string)$this->ip); |
| | | } |
| | | |
| | | return true; |
| | |
| | | $this->db->query(sprintf("DELETE FROM %s WHERE sess_id = ?", |
| | | $this->db->table_name('session')), $key); |
| | | } |
| | | |
| | | return true; |
| | | } |
| | | |
| | | |
| | | /** |
| | | * Garbage collecting function |
| | | * |
| | | * @param string Session lifetime in seconds |
| | | * @return boolean True on success |
| | | */ |
| | | public function db_gc($maxlifetime) |
| | | { |
| | | // just delete all expired sessions |
| | | $this->db->query( |
| | | sprintf("DELETE FROM %s WHERE changed < %s", |
| | | $this->db->table_name('session'), $this->db->fromunixtime(time() - $maxlifetime))); |
| | | |
| | | $this->gc(); |
| | | |
| | | return true; |
| | | } |
| | |
| | | /** |
| | | * Execute registered garbage collector routines |
| | | */ |
| | | public function gc() |
| | | public function gc($maxlifetime) |
| | | { |
| | | foreach ($this->gc_handlers as $fct) { |
| | | call_user_func($fct); |
| | | } |
| | | // move gc execution to the script shutdown function |
| | | // see rcube::shutdown() and rcube_session::write_close() |
| | | return $this->gc_enabled = $maxlifetime; |
| | | } |
| | | |
| | | |
| | |
| | | |
| | | |
| | | /** |
| | | * Garbage collector handler to run on script shutdown |
| | | */ |
| | | protected function gc_shutdown() |
| | | { |
| | | 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)); |
| | | } |
| | | |
| | | foreach ($this->gc_handlers as $fct) { |
| | | call_user_func($fct); |
| | | } |
| | | } |
| | | } |
| | | |
| | | |
| | | /** |
| | | * Generate and set new session id |
| | | * |
| | | * @param boolean $destroy If enabled the current session will be destroyed |