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

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

Common DB configuration:

',
Alain Reguera Delgado 8f60cb
            '
',
Alain Reguera Delgado 8f60cb
            '
Server:
',
Alain Reguera Delgado 8f60cb
            '
<input type="text" name="db_host" value="'.$this->db_host.'" '.$disabled.' />
',
Alain Reguera Delgado 8f60cb
            
Alain Reguera Delgado 8f60cb
            '
Username:
',
Alain Reguera Delgado 8f60cb
            '
<input type="text" name="db_user" value="'.$this->db_user.'" '.$disabled.' />
',
Alain Reguera Delgado 8f60cb
            
Alain Reguera Delgado 8f60cb
            '
Password:
',
Alain Reguera Delgado 8f60cb
            '
<input type="password" name="db_pass" value="'.$this->db_pass.'" '.$disabled.' />
',
Alain Reguera Delgado 8f60cb
            '',
Alain Reguera Delgado 8f60cb
            
Alain Reguera Delgado 8f60cb
            // Xoops Configuration
Alain Reguera Delgado 8f60cb
            '

Xoops configuration:

',
Alain Reguera Delgado 8f60cb
            '
',
Alain Reguera Delgado 8f60cb
            '
Xoops database name:
',
Alain Reguera Delgado 8f60cb
            '
<input type="text" name="db_xoops_db" value="'.$this->db_xoops_db.'" '.$disabled.' />
',
Alain Reguera Delgado 8f60cb
            
Alain Reguera Delgado 8f60cb
            '
Xoops table prefix:
',
Alain Reguera Delgado 8f60cb
            '
<input type="text" name="db_xoops_tbl" value="'.$this->db_xoops_tbl.'" '.$disabled.' />
',
Alain Reguera Delgado 8f60cb
            
Alain Reguera Delgado 8f60cb
            '',
Alain Reguera Delgado 8f60cb
            
Alain Reguera Delgado 8f60cb
            // phpBB Configuration
Alain Reguera Delgado 8f60cb
            '

phpBB configuration:

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