4 +-----------------------------------------------------------------------+
5 | program/include/rcube_session.php |
7 | This file is part of the Roundcube Webmail client |
8 | Copyright (C) 2005-2010, Roundcube Dev. - Switzerland |
9 | Licensed under the GNU GPL |
12 | Provide database supported session management |
14 +-----------------------------------------------------------------------+
15 | Author: Thomas Bruederli <roundcube@gmail.com> |
16 | Author: Aleksander Machniak <alec@alec.pl> |
17 +-----------------------------------------------------------------------+
19 $Id: session.inc 2932 2009-09-07 12:51:21Z alec $
24 * Class to provide database supported session storage
27 * @author Thomas Bruederli <roundcube@gmail.com>
28 * @author Aleksander Machniak <alec@alec.pl>
35 private $unsets = array();
36 private $gc_handlers = array();
38 private $vars = false;
40 private $keep_alive = 0;
45 public function __construct($db, $lifetime=60)
48 $this->lifetime = $lifetime;
49 $this->start = microtime(true);
51 // set custom functions for PHP session management
52 session_set_save_handler(
54 array($this, 'close'),
56 array($this, 'write'),
57 array($this, 'destroy'),
62 public function open($save_path, $session_name)
68 public function close()
75 public function read($key)
77 $sql_result = $this->db->query(
78 sprintf("SELECT vars, ip, %s AS changed FROM %s WHERE sess_id = ?",
79 $this->db->unixtimestamp('changed'), get_table_name('session')),
82 if ($sql_arr = $this->db->fetch_assoc($sql_result)) {
83 $this->changed = $sql_arr['changed'];
84 $this->ip = $sql_arr['ip'];
85 $this->vars = base64_decode($sql_arr['vars']);
88 if (!empty($this->vars))
97 public function write($key, $vars)
99 $ts = microtime(true);
100 $now = $this->db->fromunixtime((int)$ts);
102 // use internal data from read() for fast requests (up to 0.5 sec.)
103 if ($key == $this->key && $ts - $this->start < 0.5) {
104 $oldvars = $this->vars;
105 } else { // else read data again from DB
106 $oldvars = $this->read($key);
109 if ($oldvars !== false) {
110 $a_oldvars = $this->unserialize($oldvars);
111 if (is_array($a_oldvars)) {
112 foreach ((array)$this->unsets as $k)
113 unset($a_oldvars[$k]);
115 $newvars = $this->serialize(array_merge(
116 (array)$a_oldvars, (array)$this->unserialize($vars)));
121 if (!$this->lifetime) {
124 else if ($this->keep_alive>0) {
125 $timeout = min($this->lifetime * 0.5, $this->lifetime - $this->keep_alive);
130 if (!($newvars === $oldvars) || ($ts - $this->changed > $timeout)) {
132 sprintf("UPDATE %s SET vars = ?, changed = %s WHERE sess_id = ?",
133 get_table_name('session'), $now),
134 base64_encode($newvars), $key);
139 sprintf("INSERT INTO %s (sess_id, vars, ip, created, changed) ".
140 "VALUES (?, ?, ?, %s, %s)",
141 get_table_name('session'), $now, $now),
142 $key, base64_encode($vars), (string)$_SERVER['REMOTE_ADDR']);
145 $this->unsets = array();
150 // handler for session_destroy()
151 public function destroy($key)
154 sprintf("DELETE FROM %s WHERE sess_id = ?", get_table_name('session')),
157 if ($key == $this->key)
163 // garbage collecting function
164 public function gc($maxlifetime)
166 // just delete all expired sessions
168 sprintf("DELETE FROM %s WHERE changed < %s",
169 get_table_name('session'), $this->db->fromunixtime(time() - $maxlifetime)));
171 foreach ($this->gc_handlers as $fct)
178 // registering additional garbage collector functions
179 public function register_gc_handler($func_name)
181 if ($func_name && !in_array($func_name, $this->gc_handlers))
182 $this->gc_handlers[] = $func_name;
186 public function regenerate_id()
188 $randval = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
190 for ($random = '', $i=1; $i <= 32; $i++) {
191 $random .= substr($randval, mt_rand(0,(strlen($randval) - 1)), 1);
194 // use md5 value for id or remove capitals from string $randval
195 $random = md5($random);
197 // delete old session record
198 $this->destroy(session_id());
202 $cookie = session_get_cookie_params();
203 $lifetime = $cookie['lifetime'] ? time() + $cookie['lifetime'] : 0;
205 rcmail::setcookie(session_name(), $random, $lifetime);
211 // unset session variable
212 public function remove($var=NULL)
215 return $this->destroy(session_id());
217 $this->unsets[] = $var;
218 unset($_SESSION[$var]);
224 // serialize session data
225 private function serialize($vars)
229 foreach ($vars as $var=>$value)
230 $data .= $var.'|'.serialize($value);
237 // unserialize session data
238 // http://www.php.net/manual/en/function.session-decode.php#56106
239 private function unserialize($str)
242 $endptr = strlen($str);
249 while ($p < $endptr) {
251 while ($str[$q] != '|')
252 if (++$q >= $endptr) break 2;
254 if ($str[$p] == '!') {
261 $name = substr($str, $p, $q - $p);
264 $serialized .= 's:' . strlen($name) . ':"' . $name . '";';
269 switch (strtolower($str[$q])) {
271 case 'b': /* boolean */
272 case 'i': /* integer */
273 case 'd': /* decimal */
275 while ( ($q < $endptr) && ($str[$q] != ';') );
277 $serialized .= substr($str, $p, $q - $p);
278 if ($level == 0) break 2;
280 case 'r': /* reference */
282 for ($id = ''; ($q < $endptr) && ($str[$q] != ';'); $q++) $id .= $str[$q];
284 $serialized .= 'R:' . ($id + 1) . ';'; /* increment pointer because of outer array */
285 if ($level == 0) break 2;
287 case 's': /* string */
289 for ($length=''; ($q < $endptr) && ($str[$q] != ':'); $q++) $length .= $str[$q];
291 $q+= (int)$length + 2;
292 $serialized .= substr($str, $p, $q - $p);
293 if ($level == 0) break 2;
295 case 'a': /* array */
296 case 'o': /* object */
298 while ( ($q < $endptr) && ($str[$q] != '{') );
301 $serialized .= substr($str, $p, $q - $p);
303 case '}': /* end of array|object */
305 $serialized .= substr($str, $p, $q - $p);
306 if (--$level == 0) break 2;
320 return unserialize( 'a:' . $items . ':{' . $serialized . '}' );
323 public function set_keep_alive($keep_alive)
325 $this->keep_alive = $keep_alive;
328 public function get_keep_alive()
330 return $this->keep_alive;
333 // getter for private variables
334 public function get_ts()
336 return $this->changed;
339 // getter for private variables
340 public function get_ip()