1 | <?php |
---|
2 | /** |
---|
3 | * DBSessionHandler.inc.php |
---|
4 | * Code by Strangecode :: www.strangecode.com :: This document contains copyrighted information. |
---|
5 | * |
---|
6 | * @author Quinn Comendant <quinn@strangecode.com> |
---|
7 | * @version 2.1 |
---|
8 | * @since 1999 |
---|
9 | */ |
---|
10 | |
---|
11 | class DBSessionHandler { |
---|
12 | |
---|
13 | var $db; // DB object. |
---|
14 | |
---|
15 | var $_params = array( |
---|
16 | 'db_table' => 'session_tbl', |
---|
17 | 'create_table' => true, // Automatically create table and verify columns. Better set to false after site launch. |
---|
18 | ); |
---|
19 | |
---|
20 | /** |
---|
21 | * Constructor |
---|
22 | * |
---|
23 | * @access public |
---|
24 | * @param |
---|
25 | * @return |
---|
26 | * @author Quinn Comendant <quinn@strangecode.com> |
---|
27 | * @since 18 Jul 2005 11:02:50 |
---|
28 | */ |
---|
29 | function DBSessionHandler($db, $params=array()) |
---|
30 | { |
---|
31 | $app =& App::getInstance(); |
---|
32 | |
---|
33 | $this->_params = array_merge($this->_params, $params); |
---|
34 | |
---|
35 | if (!method_exists($db, 'isConnected')) { |
---|
36 | $app->logMsg(sprintf('Provided object (%s) is not a valid DB object.', get_class($db)), LOG_ERR, __FILE__, __LINE__); |
---|
37 | } else { |
---|
38 | if (!$db->isConnected()) { |
---|
39 | $app->logMsg('Provided DB object is not connected.', LOG_ERR, __FILE__, __LINE__); |
---|
40 | } else { |
---|
41 | // OK! We have a valid, connected DB object. |
---|
42 | $this->db =& $db; |
---|
43 | |
---|
44 | // Get create tables config from global context. |
---|
45 | if (!is_null($app->getParam('db_create_tables'))) { |
---|
46 | $this->_params['create_table'] = $app->getParam('db_create_tables'); |
---|
47 | } |
---|
48 | |
---|
49 | // Ensure db table is fit. |
---|
50 | $this->initDB(); |
---|
51 | |
---|
52 | ini_set('session.save_handler', 'user'); |
---|
53 | session_set_save_handler( |
---|
54 | array(&$this, 'dbSessionOpen'), |
---|
55 | array(&$this, 'dbSessionClose'), |
---|
56 | array(&$this, 'dbSessionRead'), |
---|
57 | array(&$this, 'dbSessionWrite'), |
---|
58 | array(&$this, 'dbSessionDestroy'), |
---|
59 | array(&$this, 'dbSessionGarbage') |
---|
60 | ); |
---|
61 | } |
---|
62 | } |
---|
63 | } |
---|
64 | |
---|
65 | /** |
---|
66 | * Setup the database table for this class. |
---|
67 | * |
---|
68 | * @access public |
---|
69 | * @author Quinn Comendant <quinn@strangecode.com> |
---|
70 | * @since 26 Aug 2005 17:09:36 |
---|
71 | */ |
---|
72 | function initDB($recreate_db=false) |
---|
73 | { |
---|
74 | $app =& App::getInstance(); |
---|
75 | |
---|
76 | static $_db_tested = false; |
---|
77 | |
---|
78 | if ($recreate_db || !$_db_tested && $this->_params['create_table']) { |
---|
79 | if ($recreate_db) { |
---|
80 | $this->db->query("DROP TABLE IF EXISTS " . $this->_params['db_table']); |
---|
81 | $app->logMsg(sprintf('Dropping and recreating table %s.', $this->_params['db_table']), LOG_DEBUG, __FILE__, __LINE__); |
---|
82 | } |
---|
83 | $this->db->query("CREATE TABLE IF NOT EXISTS " . $this->_params['db_table'] . " ( |
---|
84 | session_id char(32) NOT NULL default '', |
---|
85 | session_data mediumtext NOT NULL, |
---|
86 | last_access timestamp(14) NOT NULL, |
---|
87 | PRIMARY KEY (session_id), |
---|
88 | KEY last_access (last_access) |
---|
89 | )"); |
---|
90 | |
---|
91 | if (!$this->db->columnExists($this->_params['db_table'], array('session_id', 'session_data', 'last_access'))) { |
---|
92 | $app->logMsg(sprintf('Database table %s has invalid columns. Please update this table manually.', $this->_params['db_table']), LOG_ALERT, __FILE__, __LINE__); |
---|
93 | trigger_error(sprintf('Database table %s has invalid columns. Please update this table manually.', $this->_params['db_table']), E_USER_ERROR); |
---|
94 | } |
---|
95 | } |
---|
96 | $_db_tested = true; |
---|
97 | } |
---|
98 | |
---|
99 | function dbSessionOpen($save_path, $sess_name) |
---|
100 | { |
---|
101 | return true; |
---|
102 | } |
---|
103 | |
---|
104 | function dbSessionClose() |
---|
105 | { |
---|
106 | return true; |
---|
107 | } |
---|
108 | |
---|
109 | function dbSessionRead($session_id) |
---|
110 | { |
---|
111 | // Select the data belonging to session $session_id from the session table |
---|
112 | $qid = $this->db->query("SELECT session_data FROM " . $this->_params['db_table'] . " WHERE session_id = '" . $this->db->escapeString($session_id) . "'"); |
---|
113 | |
---|
114 | // Return the session data that was found |
---|
115 | if (mysql_num_rows($qid) == 1) { |
---|
116 | $row = mysql_fetch_row($qid); |
---|
117 | return $row[0]; |
---|
118 | } |
---|
119 | |
---|
120 | // NOTICE: Output is expected to be an empty string always rather than 'false'. |
---|
121 | return ''; |
---|
122 | } |
---|
123 | |
---|
124 | function dbSessionWrite($session_id, $session_data) |
---|
125 | { |
---|
126 | // Write the serialized session data ($session_data) to the session table |
---|
127 | $this->db->query("REPLACE INTO " . $this->_params['db_table'] . "(session_id, session_data, last_access) VALUES ('" . $this->db->escapeString($session_id) . "', '" . $this->db->escapeString($session_data) . "', null)"); |
---|
128 | |
---|
129 | return true; |
---|
130 | } |
---|
131 | |
---|
132 | function dbSessionDestroy($session_id) |
---|
133 | { |
---|
134 | // Delete from the table all data for the session $session_id |
---|
135 | $this->db->query("DELETE FROM " . $this->_params['db_table'] . " WHERE session_id = '" . $this->db->escapeString($session_id) . "'"); |
---|
136 | |
---|
137 | return true; |
---|
138 | } |
---|
139 | |
---|
140 | function dbSessionGarbage($max_lifetime=4000) |
---|
141 | { |
---|
142 | // Delete old values from the session table |
---|
143 | $qid = $this->db->query("DELETE FROM " . $this->_params['db_table'] . " WHERE UNIX_TIMESTAMP(last_access) < " . (time() - $max_lifetime)); |
---|
144 | |
---|
145 | return true; |
---|
146 | } |
---|
147 | } |
---|
148 | |
---|
149 | ?> |
---|