4 +-----------------------------------------------------------------------+
5 | program/include/session.inc |
7 | This file is part of the RoundCube Webmail client |
8 | Copyright (C) 2005-2008, RoundCube Dev. - Switzerland |
9 | Licensed under the GNU GPL |
12 | Provide database supported session management |
14 +-----------------------------------------------------------------------+
15 | Author: Thomas Bruederli <roundcube@gmail.com> |
16 +-----------------------------------------------------------------------+
18 $Id: session.inc 2084 2008-11-23 13:24:49Z thomasb $
23 function rcube_sess_open($save_path, $session_name)
29 function rcube_sess_close()
36 function rcube_sess_read($key)
38 global $SESS_CHANGED, $SESS_CLIENT_IP;
40 $DB = rcmail::get_instance()->get_dbh();
42 if ($DB->is_error()) {
46 $sql_result = $DB->query(
47 "SELECT vars, ip, " . $DB->unixtimestamp('changed') . " AS changed
48 FROM " . get_table_name('session') . "
52 if ($sql_arr = $DB->fetch_assoc($sql_result)) {
53 $SESS_CHANGED = $sql_arr['changed'];
54 $SESS_CLIENT_IP = $sql_arr['ip'];
56 if (strlen($sql_arr['vars']))
57 return $sql_arr['vars'];
65 function rcube_sess_write($key, $vars)
67 $DB = rcmail::get_instance()->get_dbh();
69 if ($DB->is_error()) {
73 $sql_result = $DB->query(
74 "SELECT 1 FROM " . get_table_name('session') . "
78 $now = $DB->fromunixtime(time());
80 if ($DB->num_rows($sql_result)) {
82 "UPDATE " . get_table_name('session') . "
83 SET vars=?, changed= " . $now . "
90 "INSERT INTO " . get_table_name('session') . "
91 (sess_id, vars, ip, created, changed)
92 VALUES (?, ?, ?, " . $now . ", " . $now .")",
95 (string)$_SERVER['REMOTE_ADDR']);
102 // handler for session_destroy()
103 function rcube_sess_destroy($key)
105 $rcmail = rcmail::get_instance();
106 $DB = $rcmail->get_dbh();
108 if ($DB->is_error()) {
112 $DB->query("DELETE FROM " . get_table_name('session') . " WHERE sess_id=?", $key);
118 // garbage collecting function
119 function rcube_sess_gc($maxlifetime)
121 $rcmail = rcmail::get_instance();
122 $DB = $rcmail->get_dbh();
124 if ($DB->is_error()) {
128 // just delete all expired sessions
129 $DB->query("DELETE FROM " . get_table_name('session') . "
130 WHERE changed < " . $DB->fromunixtime(time() - $maxlifetime));
132 if ($rcmail->config->get('enable_caching'))
141 function rcube_sess_regenerate_id()
143 $randval = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
145 for ($random = "", $i=1; $i <= 32; $i++) {
146 $random .= substr($randval, rand(0,(strlen($randval) - 1)), 1);
149 // use md5 value for id or remove capitals from string $randval
150 $random = md5($random);
152 // delete old session record
153 rcube_sess_destroy(session_id());
157 $cookie = session_get_cookie_params();
158 $lifetime = $cookie['lifetime'] ? time() + $cookie['lifetime'] : 0;
160 rcmail::setcookie(session_name(), $random, $lifetime);
166 // set custom functions for PHP session management
167 session_set_save_handler('rcube_sess_open', 'rcube_sess_close', 'rcube_sess_read', 'rcube_sess_write', 'rcube_sess_destroy', 'rcube_sess_gc');