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