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

Last change on this file since 144 was 136, checked in by scdev, 18 years ago

Q - Merged branches/2.0singleton into trunk. Completed updating classes to use singleton methods. Implemented tests. Fixed some bugs. Changed some interfaces.

File size: 5.3 KB
RevLine 
[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
11class DBSessionHandler {
[42]12
[1]13    var $db; // DB object.
14
15    var $_params = array(
16        'db_table' => 'session_tbl',
17        'create_table' => true, // Automatically create table and verify columns. Better set to false after site launch.
18    );
[42]19
[1]20    /**
21     * Constructor
22     *
23     * @access  public
[42]24     * @param
25     * @return
[1]26     * @author  Quinn Comendant <quinn@strangecode.com>
27     * @since   18 Jul 2005 11:02:50
28     */
[136]29    function DBSessionHandler($db, $params=array())
[1]30    {
[136]31        $app =& App::getInstance();
32   
[1]33        $this->_params = array_merge($this->_params, $params);
[42]34
[136]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__);
[16]40            } else {
[136]41                // OK! We have a valid, connected DB object.
42                $this->db =& $db;
[42]43
[136]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');
47                }
[42]48
[136]49                // Ensure db table is fit.
50                $this->initDB();
[1]51
[136]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                );
61            }
[1]62        }
63    }
64
65    /**
66     * Setup the database table for this class.
67     *
68     * @access  public
69     * @author  Quinn Comendant <quinn@strangecode.com>
70     * @since   26 Aug 2005 17:09:36
71     */
72    function initDB($recreate_db=false)
73    {
[136]74        $app =& App::getInstance();
75   
[1]76        static $_db_tested = false;
[42]77
[1]78        if ($recreate_db || !$_db_tested && $this->_params['create_table']) {
79            if ($recreate_db) {
[136]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__);
[1]82            }
[136]83            $this->db->query("CREATE TABLE IF NOT EXISTS " . $this->db->escapeString($this->_params['db_table']) . " (
[1]84                session_id char(32) NOT NULL default '',
85                session_data mediumtext NOT NULL,
86                last_access timestamp(14) NOT NULL,
87                PRIMARY KEY (session_id),
88                KEY last_access (last_access)
89            )");
[42]90
[1]91            if (!$this->db->columnExists($this->_params['db_table'], array('session_id', 'session_data', 'last_access'))) {
[136]92                $app->logMsg(sprintf('Database table %s has invalid columns. Please update this table manually.', $this->_params['db_table']), LOG_ALERT, __FILE__, __LINE__);
[1]93                trigger_error(sprintf('Database table %s has invalid columns. Please update this table manually.', $this->_params['db_table']), E_USER_ERROR);
94            }
[42]95        }
[1]96        $_db_tested = true;
97    }
98
99    function dbSessionOpen($save_path, $sess_name)
100    {
[42]101        return true;
[1]102    }
[42]103
[1]104    function dbSessionClose()
105    {
106        return true;
107    }
[42]108
[1]109    function dbSessionRead($session_id)
110    {
[42]111        // Select the data belonging to session $session_id from the session table
[136]112        $qid = $this->db->query("SELECT session_data FROM " . $this->db->escapeString($this->_params['db_table']) . " WHERE session_id = '" . $this->db->escapeString($session_id) . "'");
[42]113
[1]114        // Return the session data that was found
115        if (mysql_num_rows($qid) == 1) {
116            $row = mysql_fetch_row($qid);
117            return $row[0];
118        }
[42]119
[1]120        // NOTICE: Output is expected to be an empty string always rather than 'false'.
121        return '';
122    }
[42]123
[1]124    function dbSessionWrite($session_id, $session_data)
[42]125    {
[1]126        // Write the serialized session data ($session_data) to the session table
[136]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)");
[42]128
129        return true;
[1]130    }
[42]131
[1]132    function dbSessionDestroy($session_id)
133    {
134        // Delete from the table all data for the session $session_id
[136]135        $this->db->query("DELETE FROM " . $this->db->escapeString($this->_params['db_table']) . " WHERE session_id = '" . $this->db->escapeString($session_id) . "'");
[42]136
137        return true;
[1]138    }
[42]139
[1]140    function dbSessionGarbage($max_lifetime=4000)
141    {
142        // Delete old values from the session table
[136]143        $qid = $this->db->query("DELETE FROM " . $this->db->escapeString($this->_params['db_table']) . " WHERE UNIX_TIMESTAMP(last_access) < " . (time() - $max_lifetime));
[42]144
145        return true;
[1]146    }
147}
148
149?>
Note: See TracBrowser for help on using the repository browser.