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