Ignore:
Timestamp:
Jun 3, 2006 7:47:48 PM (18 years ago)
Author:
scdev
Message:

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

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/lib/DBSessionHandler.inc.php

    r110 r136  
    33 * DBSessionHandler.inc.php
    44 * Code by Strangecode :: www.strangecode.com :: This document contains copyrighted information.
     5 *
    56 * @author  Quinn Comendant <quinn@strangecode.com>
    6  * @version 1.3
     7 * @version 2.1
    78 * @since   1999
    89 */
     
    1314
    1415    var $_params = array(
    15         'db_server' => 'localhost',
    16         'db_name' => '',
    17         'db_user' => '',
    18         'db_pass' => '',
    1916        'db_table' => 'session_tbl',
    2017        'create_table' => true, // Automatically create table and verify columns. Better set to false after site launch.
     
    3027     * @since   18 Jul 2005 11:02:50
    3128     */
    32     function DBSessionHandler($db=null, $params=array())
     29    function DBSessionHandler($db, $params=array())
    3330    {
     31        $app =& App::getInstance();
     32   
    3433        $this->_params = array_merge($this->_params, $params);
    3534
    36         if (isset($db)) {
    37             if (is_a($db, 'DB')) {
    38                 if ($db->isConnected()) {
    39                     // Use existing db connection.
    40                     $this->db =& $db;
    41                 } else {
    42                     App::logMsg(sprintf('Provided DB object is not connected. %s', mysql_error($db->dbh)), LOG_ERR, __FILE__, __LINE__);
     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__);
     40            } else {
     41                // OK! We have a valid, connected DB object.
     42                $this->db =& $db;
     43
     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');
    4347                }
    44             } else {
    45                 App::logMsg(sprintf('Provided DB object is not valid. %s', gettype($db)), LOG_ERR, __FILE__, __LINE__);
     48
     49                // Ensure db table is fit.
     50                $this->initDB();
     51
     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                );
    4661            }
    47         } else {
    48             // Create our own new db connection.
    49             require_once dirname(__FILE__) . '/DB.inc.php';
    50 
    51             $this->db =& new DB();
    52             $this->db->setParam(array(
    53                 'db_server' => $this->_params['db_server'],
    54                 'db_name' => $this->_params['db_name'],
    55                 'db_user' => $this->_params['db_user'],
    56                 'db_pass' => $this->_params['db_pass'],
    57                 'db_always_debug' => $this->_params['db_always_debug'],
    58                 'db_debug' => $this->_params['db_debug'],
    59                 'db_die_on_failure' => $this->_params['db_die_on_failure'],
    60             ));
    61 
    62             // Connect to database.
    63             $this->db->connect();
    6462        }
    65 
    66         if (!isset($this) || !is_a($this->db, 'DB') || !$this->db->isConnected()) {
    67             trigger_error('Invalid DB object or unable to connect to database.', E_USER_ERROR);
    68         }
    69 
    70         // Get create tables config from global context.
    71         if (!is_null(App::getParam('db_create_tables'))) {
    72             $this->_params['create_table'] = App::getParam('db_create_tables');
    73         }
    74 
    75         // Ensure db table is fit.
    76         $this->initDB();
    77 
    78         ini_set('session.save_handler', 'user');
    79         session_set_save_handler(
    80             array(&$this, 'dbSessionOpen'),
    81             array(&$this, 'dbSessionClose'),
    82             array(&$this, 'dbSessionRead'),
    83             array(&$this, 'dbSessionWrite'),
    84             array(&$this, 'dbSessionDestroy'),
    85             array(&$this, 'dbSessionGarbage')
    86         );
    8763    }
    8864
     
    9672    function initDB($recreate_db=false)
    9773    {
     74        $app =& App::getInstance();
     75   
    9876        static $_db_tested = false;
    9977
    10078        if ($recreate_db || !$_db_tested && $this->_params['create_table']) {
    10179            if ($recreate_db) {
    102                 $this->db->query("DROP TABLE IF EXISTS " . $this->_params['db_table']);
    103                 App::logMsg(sprintf('Dropping and recreating table %s.', $this->_params['db_table']), LOG_DEBUG, __FILE__, __LINE__);
     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__);
    10482            }
    105             $this->db->query("CREATE TABLE IF NOT EXISTS " . $this->_params['db_table'] . " (
     83            $this->db->query("CREATE TABLE IF NOT EXISTS " . $this->db->escapeString($this->_params['db_table']) . " (
    10684                session_id char(32) NOT NULL default '',
    10785                session_data mediumtext NOT NULL,
     
    11290
    11391            if (!$this->db->columnExists($this->_params['db_table'], array('session_id', 'session_data', 'last_access'))) {
    114                 App::logMsg(sprintf('Database table %s has invalid columns. Please update this table manually.', $this->_params['db_table']), LOG_ALERT, __FILE__, __LINE__);
     92                $app->logMsg(sprintf('Database table %s has invalid columns. Please update this table manually.', $this->_params['db_table']), LOG_ALERT, __FILE__, __LINE__);
    11593                trigger_error(sprintf('Database table %s has invalid columns. Please update this table manually.', $this->_params['db_table']), E_USER_ERROR);
    11694            }
     
    132110    {
    133111        // Select the data belonging to session $session_id from the session table
    134         $qid = $this->db->query("SELECT session_data FROM " . $this->_params['db_table'] . " WHERE session_id = '" . $this->db->escapeString($session_id) . "'");
     112        $qid = $this->db->query("SELECT session_data FROM " . $this->db->escapeString($this->_params['db_table']) . " WHERE session_id = '" . $this->db->escapeString($session_id) . "'");
    135113
    136114        // Return the session data that was found
     
    147125    {
    148126        // Write the serialized session data ($session_data) to the session table
    149         $this->db->query("REPLACE INTO " . $this->_params['db_table'] . "(session_id, session_data, last_access) VALUES ('" . $this->db->escapeString($session_id) . "', '" . $this->db->escapeString($session_data) . "', null)");
     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)");
    150128
    151129        return true;
     
    155133    {
    156134        // Delete from the table all data for the session $session_id
    157         $this->db->query("DELETE FROM " . $this->_params['db_table'] . " WHERE session_id = '" . $this->db->escapeString($session_id) . "'");
     135        $this->db->query("DELETE FROM " . $this->db->escapeString($this->_params['db_table']) . " WHERE session_id = '" . $this->db->escapeString($session_id) . "'");
    158136
    159137        return true;
     
    163141    {
    164142        // Delete old values from the session table
    165         $qid = $this->db->query("DELETE FROM " . $this->_params['db_table'] . " WHERE UNIX_TIMESTAMP(last_access) < " . (time() - $max_lifetime));
     143        $qid = $this->db->query("DELETE FROM " . $this->db->escapeString($this->_params['db_table']) . " WHERE UNIX_TIMESTAMP(last_access) < " . (time() - $max_lifetime));
    166144
    167145        return true;
Note: See TracChangeset for help on using the changeset viewer.