source: trunk/lib/DBSessionHandler.inc.php

Last change on this file was 747, checked in by anonymous, 3 years ago

Set default mysql connection charset to utf8mb4

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