Blame Scripts/Php/Newbb2Phpbb/classes/db_mysql.php

878a2b
878a2b
/***
878a2b
 * Database Access (MySQL)
878a2b
 *
878a2b
 * Provides default database access values, and functions used to
878a2b
 * access data in both newbb and phpBB. 
878a2b
 * 
878a2b
 * --
878a2b
 * Alain Reguera Delgado <alain.reguera@gmail.com>
878a2b
 ***/
878a2b
878a2b
class DB_MYSQL
878a2b
{
878a2b
    public $db_conn;
878a2b
878a2b
    public $db_host;
878a2b
    public $db_user;
878a2b
    public $db_pass;
878a2b
    public $db_xoops_db;
878a2b
    public $db_xoops_tbl;
878a2b
    public $db_phpbb_db;
878a2b
    public $db_phpbb_tbl;
878a2b
878a2b
   /***
878a2b
    * Class Construct
878a2b
    ***/
878a2b
878a2b
    function __construct()
878a2b
    {
878a2b
        // Initialize configuration values
878a2b
        $this->db_host      = 'localhost';
878a2b
        $this->db_user      = 'root';
878a2b
        $this->db_pass      = '';
878a2b
        $this->db_xoops_db  = 'xoops';
878a2b
        $this->db_xoops_tbl = 'xoops_';
878a2b
        $this->db_phpbb_db  = 'phpBB';
878a2b
        $this->db_phpbb_tbl = 'phpbb_';
878a2b
878a2b
        // Reinitialize configuration values
878a2b
        $config = array('db_host', 'db_user', 'db_pass', 'db_xoops_db', 
878a2b
                        'db_xoops_tbl', 'db_phpbb_db', 'db_phpbb_tbl');
878a2b
878a2b
        foreach ( $config as $param )
878a2b
        {
878a2b
            if ( ! isset( $_SESSION[$param] ) )
878a2b
            {
878a2b
                $_SESSION[$param] = $this->$param;
878a2b
            }
878a2b
878a2b
            $_SESSION[$param] = isset($_POST[$param])?$_POST[$param]:$_SESSION[$param];
878a2b
878a2b
            $this->$param = $_SESSION[$param];
878a2b
        }
878a2b
    }
878a2b
878a2b
   /***
878a2b
    * Connect
878a2b
    */
878a2b
    function connect()
878a2b
    {
878a2b
        // Connect to MySQL database
878a2b
        $this->db_conn = mysql_connect( $this->db_host, 
878a2b
                                        $this->db_user, 
878a2b
                                        $this->db_pass );
878a2b
        if ( $this->db_conn )
878a2b
        {
878a2b
            return true; 
878a2b
        }
878a2b
        else
878a2b
        {
878a2b
            return false;
878a2b
        }
878a2b
    }
878a2b
878a2b
   /***
878a2b
    * DB Configuration
878a2b
    */
878a2b
    function get_configForm( $disabled = '' )
878a2b
    {
878a2b
        $htmlblock = array();
878a2b
878a2b
        array_push( $htmlblock, 
878a2b
            // Common DB Configuration
878a2b
            '

Common DB configuration:

',
878a2b
            '
',
878a2b
            '
Server:
',
878a2b
            '
<input type="text" name="db_host" value="'.$this->db_host.'" '.$disabled.' />
',
878a2b
            
878a2b
            '
Username:
',
878a2b
            '
<input type="text" name="db_user" value="'.$this->db_user.'" '.$disabled.' />
',
878a2b
            
878a2b
            '
Password:
',
878a2b
            '
<input type="password" name="db_pass" value="'.$this->db_pass.'" '.$disabled.' />
',
878a2b
            '',
878a2b
            
878a2b
            // Xoops Configuration
878a2b
            '

Xoops configuration:

',
878a2b
            '
',
878a2b
            '
Xoops database name:
',
878a2b
            '
<input type="text" name="db_xoops_db" value="'.$this->db_xoops_db.'" '.$disabled.' />
',
878a2b
            
878a2b
            '
Xoops table prefix:
',
878a2b
            '
<input type="text" name="db_xoops_tbl" value="'.$this->db_xoops_tbl.'" '.$disabled.' />
',
878a2b
            
878a2b
            '',
878a2b
            
878a2b
            // phpBB Configuration
878a2b
            '

phpBB configuration:

',
878a2b
            '
',
878a2b
            '
Phpbb database name:
',
878a2b
            '
<input type="text" name="db_phpbb_db" value="'.$this->db_phpbb_db.'" '.$disabled.' />
',
878a2b
            
878a2b
            '
Phpbb table prefix:
',
878a2b
            '
<input type="text" name="db_phpbb_tbl" value="'.$this->db_phpbb_tbl.'" '.$disabled.' />
',
878a2b
            '');
878a2b
            
878a2b
        return $htmlblock;
878a2b
    }
878a2b
878a2b
   /***
878a2b
    * Query
878a2b
    */
878a2b
    function query( $sql )
878a2b
    {
878a2b
        $this->connect();
878a2b
        $result = mysql_query( $sql, $this->db_conn );
878a2b
        if ( $result )
878a2b
        {
878a2b
            return $result; 
878a2b
        }
878a2b
        else
878a2b
        {
878a2b
            return false;
878a2b
        }
878a2b
    }
878a2b
878a2b
   /***
878a2b
    * Check existance
878a2b
    */
878a2b
    function check_existance( $name )
878a2b
    {
878a2b
878a2b
        switch ( $name )
878a2b
        {
878a2b
            case 'phpbb':
878a2b
                $check_dbname = $this->db_phpbb_db;
878a2b
                $check_suffix = $this->db_phpbb_tbl;
878a2b
                $check_tables = array('users', 'forums', 'topics', 'posts');
878a2b
            break;
878a2b
878a2b
            case 'xoops':
878a2b
                $check_dbname = $this->db_xoops_db;
878a2b
                $check_suffix = $this->db_xoops_tbl;
878a2b
                $check_tables = array('users', 'bb_forums', 'bb_topics', 'bb_posts', 'bb_posts_text');
878a2b
            break;
878a2b
        }
878a2b
878a2b
        $error = 0;
878a2b
        $table_list = array();
878a2b
878a2b
        // Check database existance
878a2b
        if ( ! mysql_select_db( $check_dbname ) )
878a2b
        {
878a2b
            $error++;
878a2b
        }
878a2b
878a2b
        // Check tables existance
878a2b
        else
878a2b
        {
878a2b
            $sql = 'SHOW TABLES FROM ' . $check_dbname . ';'; 
878a2b
            $result = $this->query( $sql );
878a2b
            while ( $row = mysql_fetch_row ($result) )
878a2b
            {
878a2b
                array_push($table_list, $row[0]);
878a2b
            }
878a2b
878a2b
            foreach ($check_tables as $tablename)
878a2b
            {
878a2b
                $tablename = $check_suffix . $tablename; 
878a2b
                if (in_array($tablename, $table_list) === false )
878a2b
                {
878a2b
                    $error++;
878a2b
                }
878a2b
            }
878a2b
        }
878a2b
878a2b
        if ( $error == 0 )
878a2b
        {
878a2b
            return true;
878a2b
        }
878a2b
        else
878a2b
        {
878a2b
            return false;
878a2b
        }
878a2b
    }
878a2b
878a2b
   /***
878a2b
    * Class Destruct
878a2b
    ***/
878a2b
878a2b
    function disconnect()
878a2b
    {
878a2b
        mysql_close( $this->db_conn );
878a2b
    }
878a2b
}
878a2b
878a2b
$db = new DB_MYSQL;
878a2b
?>