Changeset 136 for trunk/lib/DBSessionHandler.inc.php
- Timestamp:
- Jun 3, 2006 7:47:48 PM (18 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/lib/DBSessionHandler.inc.php
r110 r136 3 3 * DBSessionHandler.inc.php 4 4 * Code by Strangecode :: www.strangecode.com :: This document contains copyrighted information. 5 * 5 6 * @author Quinn Comendant <quinn@strangecode.com> 6 * @version 1.37 * @version 2.1 7 8 * @since 1999 8 9 */ … … 13 14 14 15 var $_params = array( 15 'db_server' => 'localhost',16 'db_name' => '',17 'db_user' => '',18 'db_pass' => '',19 16 'db_table' => 'session_tbl', 20 17 'create_table' => true, // Automatically create table and verify columns. Better set to false after site launch. … … 30 27 * @since 18 Jul 2005 11:02:50 31 28 */ 32 function DBSessionHandler($db =null, $params=array())29 function DBSessionHandler($db, $params=array()) 33 30 { 31 $app =& App::getInstance(); 32 34 33 $this->_params = array_merge($this->_params, $params); 35 34 36 if (isset($db)) { 37 if (is_a($db, 'DB')) { 38 if ($db->isConnected()) { 39 // Use existing db connection. 40 $this->db =& $db; 41 } else { 42 App::logMsg(sprintf('Provided DB object is not connected. %s', mysql_error($db->dbh)), LOG_ERR, __FILE__, __LINE__); 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'); 43 47 } 44 } else { 45 App::logMsg(sprintf('Provided DB object is not valid. %s', gettype($db)), LOG_ERR, __FILE__, __LINE__); 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 ); 46 61 } 47 } else {48 // Create our own new db connection.49 require_once dirname(__FILE__) . '/DB.inc.php';50 51 $this->db =& new DB();52 $this->db->setParam(array(53 'db_server' => $this->_params['db_server'],54 'db_name' => $this->_params['db_name'],55 'db_user' => $this->_params['db_user'],56 'db_pass' => $this->_params['db_pass'],57 'db_always_debug' => $this->_params['db_always_debug'],58 'db_debug' => $this->_params['db_debug'],59 'db_die_on_failure' => $this->_params['db_die_on_failure'],60 ));61 62 // Connect to database.63 $this->db->connect();64 62 } 65 66 if (!isset($this) || !is_a($this->db, 'DB') || !$this->db->isConnected()) {67 trigger_error('Invalid DB object or unable to connect to database.', E_USER_ERROR);68 }69 70 // Get create tables config from global context.71 if (!is_null(App::getParam('db_create_tables'))) {72 $this->_params['create_table'] = App::getParam('db_create_tables');73 }74 75 // Ensure db table is fit.76 $this->initDB();77 78 ini_set('session.save_handler', 'user');79 session_set_save_handler(80 array(&$this, 'dbSessionOpen'),81 array(&$this, 'dbSessionClose'),82 array(&$this, 'dbSessionRead'),83 array(&$this, 'dbSessionWrite'),84 array(&$this, 'dbSessionDestroy'),85 array(&$this, 'dbSessionGarbage')86 );87 63 } 88 64 … … 96 72 function initDB($recreate_db=false) 97 73 { 74 $app =& App::getInstance(); 75 98 76 static $_db_tested = false; 99 77 100 78 if ($recreate_db || !$_db_tested && $this->_params['create_table']) { 101 79 if ($recreate_db) { 102 $this->db->query("DROP TABLE IF EXISTS " . $this-> _params['db_table']);103 App::logMsg(sprintf('Dropping and recreating table %s.', $this->_params['db_table']), LOG_DEBUG, __FILE__, __LINE__);80 $this->db->query("DROP TABLE IF EXISTS " . $this->db->escapeString($this->_params['db_table'])); 81 $app->logMsg(sprintf('Dropping and recreating table %s.', $this->_params['db_table']), LOG_DEBUG, __FILE__, __LINE__); 104 82 } 105 $this->db->query("CREATE TABLE IF NOT EXISTS " . $this-> _params['db_table']. " (83 $this->db->query("CREATE TABLE IF NOT EXISTS " . $this->db->escapeString($this->_params['db_table']) . " ( 106 84 session_id char(32) NOT NULL default '', 107 85 session_data mediumtext NOT NULL, … … 112 90 113 91 if (!$this->db->columnExists($this->_params['db_table'], array('session_id', 'session_data', 'last_access'))) { 114 App::logMsg(sprintf('Database table %s has invalid columns. Please update this table manually.', $this->_params['db_table']), LOG_ALERT, __FILE__, __LINE__);92 $app->logMsg(sprintf('Database table %s has invalid columns. Please update this table manually.', $this->_params['db_table']), LOG_ALERT, __FILE__, __LINE__); 115 93 trigger_error(sprintf('Database table %s has invalid columns. Please update this table manually.', $this->_params['db_table']), E_USER_ERROR); 116 94 } … … 132 110 { 133 111 // Select the data belonging to session $session_id from the session table 134 $qid = $this->db->query("SELECT session_data FROM " . $this-> _params['db_table']. " WHERE session_id = '" . $this->db->escapeString($session_id) . "'");112 $qid = $this->db->query("SELECT session_data FROM " . $this->db->escapeString($this->_params['db_table']) . " WHERE session_id = '" . $this->db->escapeString($session_id) . "'"); 135 113 136 114 // Return the session data that was found … … 147 125 { 148 126 // Write the serialized session data ($session_data) to the session table 149 $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)");127 $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)"); 150 128 151 129 return true; … … 155 133 { 156 134 // Delete from the table all data for the session $session_id 157 $this->db->query("DELETE FROM " . $this-> _params['db_table']. " WHERE session_id = '" . $this->db->escapeString($session_id) . "'");135 $this->db->query("DELETE FROM " . $this->db->escapeString($this->_params['db_table']) . " WHERE session_id = '" . $this->db->escapeString($session_id) . "'"); 158 136 159 137 return true; … … 163 141 { 164 142 // Delete old values from the session table 165 $qid = $this->db->query("DELETE FROM " . $this-> _params['db_table']. " WHERE UNIX_TIMESTAMP(last_access) < " . (time() - $max_lifetime));143 $qid = $this->db->query("DELETE FROM " . $this->db->escapeString($this->_params['db_table']) . " WHERE UNIX_TIMESTAMP(last_access) < " . (time() - $max_lifetime)); 166 144 167 145 return true;
Note: See TracChangeset
for help on using the changeset viewer.