source: trunk/lib/DBSessionHandler.inc.php @ 15

Last change on this file since 15 was 15, checked in by scdev, 19 years ago

M trunk/tests/run_tests.sh
Now can run tests without being in tests dir.

M trunk/tests/_config.inc.php
No change

M trunk/tests/Auth_SQLTest.php
...

M trunk/lib/RecordVersion.inc.php
Removed debugging.

M trunk/lib/DB.inc.php
Added die on connect error only if db_die_on_failure is true.

M trunk/lib/DBSessionHandler.inc.php
Added more accurate error-checking.

M trunk/lib/FormValidator.inc.php
Fixed email regex bugs.

M trunk/lib/SpellCheck.inc.php
Integrated lots of bug fixes from UK update.

M trunk/lib/Auth_SQL.inc.php
Lots of minor bug fixes.

M trunk/lib/App.inc.php
A couple minor bug fixes.

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