source: tags/2.1.5/lib/DBSessionHandler.inc.php

Last change on this file was 377, checked in by quinn, 14 years ago

Releasing trunk as stable version 2.1.5

File size: 6.2 KB
RevLine 
[1]1<?php
2/**
[362]3 * The Strangecode Codebase - a general application development framework for PHP
4 * For details visit the project site: <http://trac.strangecode.com/codebase/>
[376]5 * Copyright 2001-2010 Strangecode, LLC
[362]6 *
7 * This file is part of The Strangecode Codebase.
8 *
9 * The Strangecode Codebase is free software: you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License as published by the
11 * Free Software Foundation, either version 3 of the License, or (at your option)
12 * any later version.
13 *
14 * The Strangecode Codebase is distributed in the hope that it will be useful, but
15 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
16 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
17 * details.
18 *
19 * You should have received a copy of the GNU General Public License along with
20 * The Strangecode Codebase. If not, see <http://www.gnu.org/licenses/>.
21 */
22
23/**
[1]24 * DBSessionHandler.inc.php
[362]25 *
[136]26 *
[1]27 * @author  Quinn Comendant <quinn@strangecode.com>
[136]28 * @version 2.1
[1]29 * @since   1999
30 */
31
32class DBSessionHandler {
[42]33
[1]34    var $db; // DB object.
35
36    var $_params = array(
37        'db_table' => 'session_tbl',
[146]38
39        // Automatically create table and verify columns. Better set to false after site launch.
40        'create_table' => true,
[1]41    );
[42]42
[1]43    /**
44     * Constructor
45     *
46     * @access  public
[42]47     * @param
48     * @return
[1]49     * @author  Quinn Comendant <quinn@strangecode.com>
50     * @since   18 Jul 2005 11:02:50
51     */
[136]52    function DBSessionHandler($db, $params=array())
[1]53    {
[136]54        $app =& App::getInstance();
55   
[1]56        $this->_params = array_merge($this->_params, $params);
[42]57
[136]58        if (!method_exists($db, 'isConnected')) {
59            $app->logMsg(sprintf('Provided object (%s) is not a valid DB object.', get_class($db)), LOG_ERR, __FILE__, __LINE__);
60        } else {
61            if (!$db->isConnected()) {
62                $app->logMsg('Provided DB object is not connected.', LOG_ERR, __FILE__, __LINE__);
[16]63            } else {
[136]64                // OK! We have a valid, connected DB object.
65                $this->db =& $db;
[42]66
[136]67                // Get create tables config from global context.
68                if (!is_null($app->getParam('db_create_tables'))) {
69                    $this->_params['create_table'] = $app->getParam('db_create_tables');
70                }
[42]71
[136]72                // Ensure db table is fit.
73                $this->initDB();
[1]74
[136]75                session_set_save_handler(
76                    array(&$this, 'dbSessionOpen'),
77                    array(&$this, 'dbSessionClose'),
78                    array(&$this, 'dbSessionRead'),
79                    array(&$this, 'dbSessionWrite'),
80                    array(&$this, 'dbSessionDestroy'),
81                    array(&$this, 'dbSessionGarbage')
82                );
[373]83                register_shutdown_function('session_write_close');
[136]84            }
[1]85        }
86    }
87
88    /**
89     * Setup the database table for this class.
90     *
91     * @access  public
92     * @author  Quinn Comendant <quinn@strangecode.com>
93     * @since   26 Aug 2005 17:09:36
94     */
95    function initDB($recreate_db=false)
96    {
[136]97        $app =& App::getInstance();
98   
[1]99        static $_db_tested = false;
[42]100
[1]101        if ($recreate_db || !$_db_tested && $this->_params['create_table']) {
102            if ($recreate_db) {
[136]103                $this->db->query("DROP TABLE IF EXISTS " . $this->db->escapeString($this->_params['db_table']));
[201]104                $app->logMsg(sprintf('Dropping and recreating table %s.', $this->_params['db_table']), LOG_INFO, __FILE__, __LINE__);
[1]105            }
[136]106            $this->db->query("CREATE TABLE IF NOT EXISTS " . $this->db->escapeString($this->_params['db_table']) . " (
[1]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            )");
[42]113
[1]114            if (!$this->db->columnExists($this->_params['db_table'], array('session_id', 'session_data', 'last_access'))) {
[136]115                $app->logMsg(sprintf('Database table %s has invalid columns. Please update this table manually.', $this->_params['db_table']), LOG_ALERT, __FILE__, __LINE__);
[1]116                trigger_error(sprintf('Database table %s has invalid columns. Please update this table manually.', $this->_params['db_table']), E_USER_ERROR);
117            }
[42]118        }
[1]119        $_db_tested = true;
120    }
121
122    function dbSessionOpen($save_path, $sess_name)
123    {
[42]124        return true;
[1]125    }
[42]126
[1]127    function dbSessionClose()
[373]128    {       
[1]129        return true;
130    }
[42]131
[1]132    function dbSessionRead($session_id)
133    {
[42]134        // Select the data belonging to session $session_id from the session table
[136]135        $qid = $this->db->query("SELECT session_data FROM " . $this->db->escapeString($this->_params['db_table']) . " WHERE session_id = '" . $this->db->escapeString($session_id) . "'");
[42]136
[1]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        }
[42]142
[1]143        // NOTICE: Output is expected to be an empty string always rather than 'false'.
144        return '';
145    }
[42]146
[1]147    function dbSessionWrite($session_id, $session_data)
[42]148    {
[1]149        // Write the serialized session data ($session_data) to the session table
[136]150        $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]151
152        return true;
[1]153    }
[42]154
[1]155    function dbSessionDestroy($session_id)
156    {
157        // Delete from the table all data for the session $session_id
[136]158        $this->db->query("DELETE FROM " . $this->db->escapeString($this->_params['db_table']) . " WHERE session_id = '" . $this->db->escapeString($session_id) . "'");
[42]159
160        return true;
[1]161    }
[42]162
[373]163    function dbSessionGarbage($max_lifetime=72000)
[1]164    {
[373]165        // Delete old values from the session table.
[136]166        $qid = $this->db->query("DELETE FROM " . $this->db->escapeString($this->_params['db_table']) . " WHERE UNIX_TIMESTAMP(last_access) < " . (time() - $max_lifetime));
[42]167
168        return true;
[1]169    }
170}
171
172?>
Note: See TracBrowser for help on using the repository browser.