source: branches/eli_branch/lib/DBSessionHandler.inc.php @ 527

Last change on this file since 527 was 439, checked in by anonymous, 11 years ago

added public and private keywords to all properties and methods, changed old classname constructor function to construct, removed more ?> closing tags

File size: 6.3 KB
Line 
1<?php
2/**
3 * The Strangecode Codebase - a general application development framework for PHP
4 * For details visit the project site: <http://trac.strangecode.com/codebase/>
5 * Copyright 2001-2012 Strangecode, LLC
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/**
24 * DBSessionHandler.inc.php
25 *
26 *
27 * @author  Quinn Comendant <quinn@strangecode.com>
28 * @version 2.1
29 * @since   1999
30 */
31
32class DBSessionHandler {
33
34    public $db; // DB object.
35
36    private $_params = array(
37        'db_table' => 'session_tbl',
38
39        // Automatically create table and verify columns. Better set to false after site launch.
40        // This value is overwritten by the $app->getParam('db_create_tables') setting if it is available.
41        'create_table' => true,
42    );
43
44    /**
45     * Constructor
46     *
47     * @access  public
48     * @param
49     * @return
50     * @author  Quinn Comendant <quinn@strangecode.com>
51     * @since   18 Jul 2005 11:02:50
52     */
53    public function __construct($db, $params=array())
54    {
55        $app =& App::getInstance();
56   
57        $this->_params = array_merge($this->_params, $params);
58
59        if (!method_exists($db, 'isConnected')) {
60            $app->logMsg(sprintf('Provided object (%s) is not a valid DB object.', get_class($db)), LOG_ERR, __FILE__, __LINE__);
61        } else {
62            if (!$db->isConnected()) {
63                $app->logMsg('Provided DB object is not connected.', LOG_ERR, __FILE__, __LINE__);
64            } else {
65                // OK! We have a valid, connected DB object.
66                $this->db =& $db;
67
68                // Get create tables config from global context.
69                if (!is_null($app->getParam('db_create_tables'))) {
70                    $this->_params['create_table'] = $app->getParam('db_create_tables');
71                }
72
73                // Ensure db table is fit.
74                $this->initDB();
75
76                session_set_save_handler(
77                    array(&$this, 'dbSessionOpen'),
78                    array(&$this, 'dbSessionClose'),
79                    array(&$this, 'dbSessionRead'),
80                    array(&$this, 'dbSessionWrite'),
81                    array(&$this, 'dbSessionDestroy'),
82                    array(&$this, 'dbSessionGarbage')
83                );
84                register_shutdown_function('session_write_close');
85            }
86        }
87    }
88
89    /**
90     * Setup the database table for this class.
91     *
92     * @access  public
93     * @author  Quinn Comendant <quinn@strangecode.com>
94     * @since   26 Aug 2005 17:09:36
95     */
96    public function initDB($recreate_db=false)
97    {
98        $app =& App::getInstance();
99   
100        static $_db_tested = false;
101
102        if ($recreate_db || !$_db_tested && $this->_params['create_table']) {
103            if ($recreate_db) {
104                $this->db->query("DROP TABLE IF EXISTS " . $this->db->escapeString($this->_params['db_table']));
105                $app->logMsg(sprintf('Dropping and recreating table %s.', $this->_params['db_table']), LOG_INFO, __FILE__, __LINE__);
106            }
107            $this->db->query("CREATE TABLE IF NOT EXISTS " . $this->db->escapeString($this->_params['db_table']) . " (
108                session_id char(32) NOT NULL default '',
109                session_data mediumtext NOT NULL,
110                last_access timestamp NOT NULL,
111                PRIMARY KEY (session_id),
112                KEY last_access (last_access)
113            )");
114
115            if (!$this->db->columnExists($this->_params['db_table'], array('session_id', 'session_data', 'last_access'))) {
116                $app->logMsg(sprintf('Database table %s has invalid columns. Please update this table manually.', $this->_params['db_table']), LOG_ALERT, __FILE__, __LINE__);
117                trigger_error(sprintf('Database table %s has invalid columns. Please update this table manually.', $this->_params['db_table']), E_USER_ERROR);
118            }
119        }
120        $_db_tested = true;
121    }
122
123    public function dbSessionOpen($save_path, $sess_name)
124    {
125        return true;
126    }
127
128    public function dbSessionClose()
129    {       
130        return true;
131    }
132
133    public function dbSessionRead($session_id)
134    {
135        // Select the data belonging to session $session_id from the session table
136        $qid = $this->db->query("SELECT session_data FROM " . $this->db->escapeString($this->_params['db_table']) . " WHERE session_id = '" . $this->db->escapeString($session_id) . "'");
137
138        // Return the session data that was found
139        if (mysql_num_rows($qid) == 1) {
140            $row = mysql_fetch_row($qid);
141            return $row[0];
142        }
143
144        // NOTICE: Output is expected to be an empty string always rather than 'false'.
145        return '';
146    }
147
148    public function dbSessionWrite($session_id, $session_data)
149    {
150        // Write the serialized session data ($session_data) to the session table
151        $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)");
152
153        return true;
154    }
155
156    public function dbSessionDestroy($session_id)
157    {
158        // Delete from the table all data for the session $session_id
159        $this->db->query("DELETE FROM " . $this->db->escapeString($this->_params['db_table']) . " WHERE session_id = '" . $this->db->escapeString($session_id) . "'");
160
161        return true;
162    }
163
164    public function dbSessionGarbage($max_lifetime=72000)
165    {
166        // Delete old values from the session table.
167        $qid = $this->db->query("DELETE FROM " . $this->db->escapeString($this->_params['db_table']) . " WHERE UNIX_TIMESTAMP(last_access) < " . (time() - $max_lifetime));
168
169        return true;
170    }
171}
172
Note: See TracBrowser for help on using the repository browser.