1 | <?php |
---|
2 | /** |
---|
3 | * The Strangecode Codebase - a general application development framework for PHP |
---|
4 | * For details visit the project site: <http://trac.strangecode.com/codebase/> |
---|
5 | * Copyright 2001-2009 Strangecode Internet Consultancy |
---|
6 | * |
---|
7 | * This file is part of The Strangecode Codebase. |
---|
8 | * |
---|
9 | * The Strangecode Codebase is free software: you can redistribute it and/or |
---|
10 | * modify it under the terms of the GNU General Public License as published by the |
---|
11 | * Free Software Foundation, either version 3 of the License, or (at your option) |
---|
12 | * any later version. |
---|
13 | * |
---|
14 | * The Strangecode Codebase is distributed in the hope that it will be useful, but |
---|
15 | * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
---|
16 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more |
---|
17 | * details. |
---|
18 | * |
---|
19 | * You should have received a copy of the GNU General Public License along with |
---|
20 | * The Strangecode Codebase. If not, see <http://www.gnu.org/licenses/>. |
---|
21 | */ |
---|
22 | |
---|
23 | /** |
---|
24 | * DBSessionHandler.inc.php |
---|
25 | * |
---|
26 | * |
---|
27 | * @author Quinn Comendant <quinn@strangecode.com> |
---|
28 | * @version 2.1 |
---|
29 | * @since 1999 |
---|
30 | */ |
---|
31 | |
---|
32 | class DBSessionHandler { |
---|
33 | |
---|
34 | var $db; // DB object. |
---|
35 | |
---|
36 | var $_params = array( |
---|
37 | 'db_table' => 'session_tbl', |
---|
38 | |
---|
39 | // Automatically create table and verify columns. Better set to false after site launch. |
---|
40 | 'create_table' => true, |
---|
41 | ); |
---|
42 | |
---|
43 | /** |
---|
44 | * Constructor |
---|
45 | * |
---|
46 | * @access public |
---|
47 | * @param |
---|
48 | * @return |
---|
49 | * @author Quinn Comendant <quinn@strangecode.com> |
---|
50 | * @since 18 Jul 2005 11:02:50 |
---|
51 | */ |
---|
52 | function DBSessionHandler($db, $params=array()) |
---|
53 | { |
---|
54 | $app =& App::getInstance(); |
---|
55 | |
---|
56 | $this->_params = array_merge($this->_params, $params); |
---|
57 | |
---|
58 | if (!method_exists($db, 'isConnected')) { |
---|
59 | $app->logMsg(sprintf('Provided object (%s) is not a valid DB object.', get_class($db)), LOG_ERR, __FILE__, __LINE__); |
---|
60 | } else { |
---|
61 | if (!$db->isConnected()) { |
---|
62 | $app->logMsg('Provided DB object is not connected.', LOG_ERR, __FILE__, __LINE__); |
---|
63 | } else { |
---|
64 | // OK! We have a valid, connected DB object. |
---|
65 | $this->db =& $db; |
---|
66 | |
---|
67 | // Get create tables config from global context. |
---|
68 | if (!is_null($app->getParam('db_create_tables'))) { |
---|
69 | $this->_params['create_table'] = $app->getParam('db_create_tables'); |
---|
70 | } |
---|
71 | |
---|
72 | // Ensure db table is fit. |
---|
73 | $this->initDB(); |
---|
74 | |
---|
75 | ini_set('session.save_handler', 'user'); |
---|
76 | session_set_save_handler( |
---|
77 | array(&$this, 'dbSessionOpen'), |
---|
78 | array(&$this, 'dbSessionClose'), |
---|
79 | array(&$this, 'dbSessionRead'), |
---|
80 | array(&$this, 'dbSessionWrite'), |
---|
81 | array(&$this, 'dbSessionDestroy'), |
---|
82 | array(&$this, 'dbSessionGarbage') |
---|
83 | ); |
---|
84 | } |
---|
85 | } |
---|
86 | } |
---|
87 | |
---|
88 | /** |
---|
89 | * Setup the database table for this class. |
---|
90 | * |
---|
91 | * @access public |
---|
92 | * @author Quinn Comendant <quinn@strangecode.com> |
---|
93 | * @since 26 Aug 2005 17:09:36 |
---|
94 | */ |
---|
95 | function initDB($recreate_db=false) |
---|
96 | { |
---|
97 | $app =& App::getInstance(); |
---|
98 | |
---|
99 | static $_db_tested = false; |
---|
100 | |
---|
101 | if ($recreate_db || !$_db_tested && $this->_params['create_table']) { |
---|
102 | if ($recreate_db) { |
---|
103 | $this->db->query("DROP TABLE IF EXISTS " . $this->db->escapeString($this->_params['db_table'])); |
---|
104 | $app->logMsg(sprintf('Dropping and recreating table %s.', $this->_params['db_table']), LOG_INFO, __FILE__, __LINE__); |
---|
105 | } |
---|
106 | $this->db->query("CREATE TABLE IF NOT EXISTS " . $this->db->escapeString($this->_params['db_table']) . " ( |
---|
107 | session_id char(32) NOT NULL default '', |
---|
108 | session_data mediumtext NOT NULL, |
---|
109 | last_access timestamp(14) NOT NULL, |
---|
110 | PRIMARY KEY (session_id), |
---|
111 | KEY last_access (last_access) |
---|
112 | )"); |
---|
113 | |
---|
114 | if (!$this->db->columnExists($this->_params['db_table'], array('session_id', 'session_data', 'last_access'))) { |
---|
115 | $app->logMsg(sprintf('Database table %s has invalid columns. Please update this table manually.', $this->_params['db_table']), LOG_ALERT, __FILE__, __LINE__); |
---|
116 | trigger_error(sprintf('Database table %s has invalid columns. Please update this table manually.', $this->_params['db_table']), E_USER_ERROR); |
---|
117 | } |
---|
118 | } |
---|
119 | $_db_tested = true; |
---|
120 | } |
---|
121 | |
---|
122 | function dbSessionOpen($save_path, $sess_name) |
---|
123 | { |
---|
124 | return true; |
---|
125 | } |
---|
126 | |
---|
127 | function dbSessionClose() |
---|
128 | { |
---|
129 | return true; |
---|
130 | } |
---|
131 | |
---|
132 | function dbSessionRead($session_id) |
---|
133 | { |
---|
134 | // Select the data belonging to session $session_id from the session table |
---|
135 | $qid = $this->db->query("SELECT session_data FROM " . $this->db->escapeString($this->_params['db_table']) . " WHERE session_id = '" . $this->db->escapeString($session_id) . "'"); |
---|
136 | |
---|
137 | // Return the session data that was found |
---|
138 | if (mysql_num_rows($qid) == 1) { |
---|
139 | $row = mysql_fetch_row($qid); |
---|
140 | return $row[0]; |
---|
141 | } |
---|
142 | |
---|
143 | // NOTICE: Output is expected to be an empty string always rather than 'false'. |
---|
144 | return ''; |
---|
145 | } |
---|
146 | |
---|
147 | function dbSessionWrite($session_id, $session_data) |
---|
148 | { |
---|
149 | // Write the serialized session data ($session_data) to the session table |
---|
150 | $this->db->query("REPLACE INTO " . $this->db->escapeString($this->_params['db_table']) . "(session_id, session_data, last_access) VALUES ('" . $this->db->escapeString($session_id) . "', '" . $this->db->escapeString($session_data) . "', null)"); |
---|
151 | |
---|
152 | return true; |
---|
153 | } |
---|
154 | |
---|
155 | function dbSessionDestroy($session_id) |
---|
156 | { |
---|
157 | // Delete from the table all data for the session $session_id |
---|
158 | $this->db->query("DELETE FROM " . $this->db->escapeString($this->_params['db_table']) . " WHERE session_id = '" . $this->db->escapeString($session_id) . "'"); |
---|
159 | |
---|
160 | return true; |
---|
161 | } |
---|
162 | |
---|
163 | function dbSessionGarbage($max_lifetime=4000) |
---|
164 | { |
---|
165 | // Delete old values from the session table |
---|
166 | $qid = $this->db->query("DELETE FROM " . $this->db->escapeString($this->_params['db_table']) . " WHERE UNIX_TIMESTAMP(last_access) < " . (time() - $max_lifetime)); |
---|
167 | |
---|
168 | return true; |
---|
169 | } |
---|
170 | } |
---|
171 | |
---|
172 | ?> |
---|