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