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-2012 Strangecode, LLC |
---|
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 | |
---|
35 | public $db; // DB object. |
---|
36 | |
---|
37 | protected $_params = array( |
---|
38 | 'db_table' => 'session_tbl', |
---|
39 | |
---|
40 | // Automatically create table and verify columns. Better set to false after site launch. |
---|
41 | // This value is overwritten by the $app->getParam('db_create_tables') setting if it is available. |
---|
42 | 'create_table' => true, |
---|
43 | ); |
---|
44 | |
---|
45 | /** |
---|
46 | * Constructor |
---|
47 | * |
---|
48 | * @access public |
---|
49 | * @param |
---|
50 | * @return |
---|
51 | * @author Quinn Comendant <quinn@strangecode.com> |
---|
52 | * @since 18 Jul 2005 11:02:50 |
---|
53 | */ |
---|
54 | public function __construct($db, $params=array()) |
---|
55 | { |
---|
56 | $app =& App::getInstance(); |
---|
57 | |
---|
58 | $this->_params = array_merge($this->_params, $params); |
---|
59 | |
---|
60 | if (!method_exists($db, 'isConnected')) { |
---|
61 | $app->logMsg(sprintf('Provided object (%s) is not a valid DB object.', get_class($db)), LOG_ERR, __FILE__, __LINE__); |
---|
62 | } else { |
---|
63 | if (!$db->isConnected()) { |
---|
64 | $app->logMsg('Provided DB object is not connected.', LOG_ERR, __FILE__, __LINE__); |
---|
65 | } else { |
---|
66 | // OK! We have a valid, connected DB object. |
---|
67 | $this->db =& $db; |
---|
68 | |
---|
69 | // Get create tables config from global context. |
---|
70 | if (!is_null($app->getParam('db_create_tables'))) { |
---|
71 | $this->_params['create_table'] = $app->getParam('db_create_tables'); |
---|
72 | } |
---|
73 | |
---|
74 | // Ensure db table is fit. |
---|
75 | $this->initDB(); |
---|
76 | |
---|
77 | session_set_save_handler( |
---|
78 | array(&$this, 'dbSessionOpen'), |
---|
79 | array(&$this, 'dbSessionClose'), |
---|
80 | array(&$this, 'dbSessionRead'), |
---|
81 | array(&$this, 'dbSessionWrite'), |
---|
82 | array(&$this, 'dbSessionDestroy'), |
---|
83 | array(&$this, 'dbSessionGarbage') |
---|
84 | ); |
---|
85 | register_shutdown_function('session_write_close'); |
---|
86 | } |
---|
87 | } |
---|
88 | } |
---|
89 | |
---|
90 | /** |
---|
91 | * Setup the database table for this class. |
---|
92 | * |
---|
93 | * @access public |
---|
94 | * @author Quinn Comendant <quinn@strangecode.com> |
---|
95 | * @since 26 Aug 2005 17:09:36 |
---|
96 | */ |
---|
97 | public function initDB($recreate_db=false) |
---|
98 | { |
---|
99 | $app =& App::getInstance(); |
---|
100 | |
---|
101 | static $_db_tested = false; |
---|
102 | |
---|
103 | if ($recreate_db || !$_db_tested && $this->_params['create_table']) { |
---|
104 | if ($recreate_db) { |
---|
105 | $this->db->query("DROP TABLE IF EXISTS " . $this->db->escapeString($this->_params['db_table'])); |
---|
106 | $app->logMsg(sprintf('Dropping and recreating table %s.', $this->_params['db_table']), LOG_INFO, __FILE__, __LINE__); |
---|
107 | } |
---|
108 | $this->db->query("CREATE TABLE IF NOT EXISTS " . $this->db->escapeString($this->_params['db_table']) . " ( |
---|
109 | `session_id` VARCHAR(255) CHARACTER SET ascii COLLATE ascii_bin NOT NULL DEFAULT '', |
---|
110 | `session_data` MEDIUMTEXT NOT NULL, |
---|
111 | `last_access` TIMESTAMP NOT NULL, |
---|
112 | PRIMARY KEY `session_id` (`session_id`), |
---|
113 | KEY `last_access` (`last_access`) |
---|
114 | ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4"); |
---|
115 | |
---|
116 | if (!$this->db->columnExists($this->_params['db_table'], array( |
---|
117 | 'session_id', |
---|
118 | 'session_data', |
---|
119 | 'last_access' |
---|
120 | ), false, false)) { |
---|
121 | $app->logMsg(sprintf('Database table %s has invalid columns. Please update this table manually.', $this->_params['db_table']), LOG_ALERT, __FILE__, __LINE__); |
---|
122 | trigger_error(sprintf('Database table %s has invalid columns. Please update this table manually.', $this->_params['db_table']), E_USER_ERROR); |
---|
123 | } |
---|
124 | } |
---|
125 | $_db_tested = true; |
---|
126 | } |
---|
127 | |
---|
128 | public function dbSessionOpen($save_path, $sess_name) |
---|
129 | { |
---|
130 | return true; |
---|
131 | } |
---|
132 | |
---|
133 | public function dbSessionClose() |
---|
134 | { |
---|
135 | return true; |
---|
136 | } |
---|
137 | |
---|
138 | public function dbSessionRead($session_id) |
---|
139 | { |
---|
140 | // Select the data belonging to session $session_id from the session table |
---|
141 | $qid = $this->db->query("SELECT session_data FROM " . $this->db->escapeString($this->_params['db_table']) . " WHERE session_id = '" . $this->db->escapeString($session_id) . "'"); |
---|
142 | |
---|
143 | // Return the session data that was found |
---|
144 | if (mysql_num_rows($qid) == 1) { |
---|
145 | $row = mysql_fetch_row($qid); |
---|
146 | return $row[0]; |
---|
147 | } |
---|
148 | |
---|
149 | // NOTICE: Output is expected to be an empty string always rather than 'false'. |
---|
150 | return ''; |
---|
151 | } |
---|
152 | |
---|
153 | public function dbSessionWrite($session_id, $session_data) |
---|
154 | { |
---|
155 | // Write the serialized session data ($session_data) to the session table |
---|
156 | $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)"); |
---|
157 | |
---|
158 | return true; |
---|
159 | } |
---|
160 | |
---|
161 | public function dbSessionDestroy($session_id) |
---|
162 | { |
---|
163 | // Delete from the table all data for the session $session_id |
---|
164 | $this->db->query("DELETE FROM " . $this->db->escapeString($this->_params['db_table']) . " WHERE session_id = '" . $this->db->escapeString($session_id) . "'"); |
---|
165 | |
---|
166 | return true; |
---|
167 | } |
---|
168 | |
---|
169 | public function dbSessionGarbage($max_lifetime=72000) |
---|
170 | { |
---|
171 | // Delete old values from the session table. |
---|
172 | $qid = $this->db->query("DELETE FROM " . $this->db->escapeString($this->_params['db_table']) . " WHERE UNIX_TIMESTAMP(last_access) < " . (time() - $max_lifetime)); |
---|
173 | |
---|
174 | return true; |
---|
175 | } |
---|
176 | } |
---|
177 | |
---|