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

878a2b
878a2b
/***
878a2b
 * Convert Xoops + CBB (newbb) to phpBB
878a2b
 * 
878a2b
 * --
878a2b
 * Alain Reguera Delgado <alain.reguera@gmail.com>
878a2b
 ***/
878a2b
878a2b
878a2b
class NEWBB_TO_PHPBB
878a2b
{
878a2b
878a2b
   /***
878a2b
    * Class Construct
878a2b
    * ----------------------------------------------------
878a2b
    */
878a2b
    function __construct()
878a2b
    {
878a2b
        // Initialize session
878a2b
        session_start();
878a2b
878a2b
        // Check action: usedefaults
878a2b
        if ( isset( $_GET['action'] ) && $_GET['action'] == 'restore' )
878a2b
        {
878a2b
            // Unset session values
878a2b
            session_unset();
878a2b
        
878a2b
            // Reload page 
878a2b
            header('Location: index.php');
878a2b
        }
878a2b
    }
878a2b
878a2b
   /***
878a2b
    * Verify Configuration
878a2b
    *
878a2b
    * This verification is needed to be sure new information entered
878a2b
    * is valid.
878a2b
    */
878a2b
    function config_verification( $next_step )
878a2b
    {
878a2b
        global $db;
878a2b
        global $ldap;
878a2b
        global $html;
878a2b
878a2b
        $htmlblock = array();
878a2b
        $error = 0;
878a2b
878a2b
        // Verify LDAP bind
878a2b
        if ( $ldap->do_bind() )
878a2b
        {
878a2b
            array_push($htmlblock, $html->format_message('LDAP Configuration is correct', 'green'));
878a2b
        }
878a2b
        else
878a2b
        {
878a2b
            array_push( $htmlblock, $html->format_message('LDAP Configuration is incorrect', 'orange'));
878a2b
            $error++;
878a2b
        }
878a2b
878a2b
        // Verify Database
878a2b
        if ( $db->connect() === true )
878a2b
        {
878a2b
            array_push( $htmlblock, $html->format_message('Common DB Configuration is correct', 'green'));
878a2b
878a2b
            // Verify XOOPS database and table
878a2b
            if ( $db->check_existance('xoops') === true )
878a2b
            {
878a2b
                array_push( $htmlblock, $html->format_message('Xoops configuration is correct', 'green'));
878a2b
            }
878a2b
            else
878a2b
            {
878a2b
                array_push( $htmlblock, $html->format_message('Xoops configuration is incorrect', 'orange'));
878a2b
                $error++;
878a2b
            }
878a2b
    
878a2b
            // Verify PHPBB database and table
878a2b
            if ( $db->check_existance('phpbb') === true )
878a2b
            {
878a2b
                array_push( $htmlblock, $html->format_message('phpBB configuration is correct', 'green'));
878a2b
            }
878a2b
            else
878a2b
            {
878a2b
                array_push( $htmlblock, $html->format_message('phpBB configuration is incorrect', 'orange'));
878a2b
                $error++;
878a2b
            }
878a2b
        }
878a2b
        else
878a2b
        {
878a2b
            array_push( $htmlblock, $html->format_message('Common DB Configuration is incorrect', 'orange'));
878a2b
            $error++;
878a2b
        }
878a2b
878a2b
        // Add action button
878a2b
        if ( $error == 0 )
878a2b
        {
878a2b
            $next_step++;
878a2b
            array_push( $htmlblock,
878a2b
                             '

878a2b
                              <input type="hidden" name="step" value="'.$next_step.'" />
878a2b
                              <input type="submit" name="Next" value="Next" />
878a2b
                              

');
878a2b
        }
878a2b
        else
878a2b
        {
878a2b
            array_push($htmlblock, '

PreviousCheck your configuration

');
878a2b
        }
878a2b
878a2b
        return $html->format_htmlblock($htmlblock);
878a2b
    }
878a2b
878a2b
   /***
878a2b
    * Groups
878a2b
    *
878a2b
    * All users in xoops.users will be inserted into phpBB.users
878a2b
    * using the REGISTERED group (group_id = 2). Forums administrators should
878a2b
    * be redifined after migration.
878a2b
    */
878a2b
878a2b
   /*** 
878a2b
    * Users 
878a2b
    * 
878a2b
    * Basic fields are copied from xoops.users to phpBB.users.
878a2b
    *
878a2b
    * Password field should be redifined by the user in order to get logged in
878a2b
    * after the migration.
878a2b
    *
878a2b
    * If LDAP authentication is used the directory structure should be design
878a2b
    * to receive uid and userPassword attributes. In this case the migration
878a2b
    * should be focused from xoops.users to LDAP directory not phpBB.users.
878a2b
    *
878a2b
    * The LDAP registration process is (as my understanding): 1. Add an entry
878a2b
    * for the user in the LDAP directory.  2. Add an entry for the user in the
878a2b
    * DB (this is automatically done by phpBB). This is needed to relate user
878a2b
    * against user specific information like topics, posts, etc.
878a2b
    *
878a2b
    * As we are using LDAP server for users. This function use php's ldap
878a2b
    * extension to add users into LDAP directory. If the user do no exist in
878a2b
    * the Database but in LDAP server, phpBB will automatically insert a
878a2b
    * record for that user in the phpBB.user table. It is needed to relate
878a2b
    * user identity to posts, topics, etc .
878a2b
    *
878a2b
    * User passwords need to be reseted and a notification could be send to each
878a2b
    * user telling the new password set. This is requiered because the
878a2b
    * password codification used in newbb, phpbb and LDAP is different.
878a2b
    *
878a2b
    * The structure of LDAP user entries was built with rfc2377 in mind.
878a2b
    */
878a2b
    function copy_Users()
878a2b
    {
878a2b
        global $ldap;
878a2b
        global $db;
878a2b
878a2b
        $htmlblock = array('

Users

','
    ');
878a2b
878a2b
        // Remove phpBB.users. Number 52 seems to be the greatest user_id
878a2b
        // value when no user has been created.
878a2b
878a2b
        array_push($htmlblock,'
  • Cleanning up ...
  • ');
    878a2b
    878a2b
            $sql = sprintf('DELETE FROM %s.%susers WHERE user_id > 52;', 
    878a2b
                                   $db->db_phpbb_db, 
    878a2b
                                   $db->db_phpbb_tbl );
    878a2b
            $db->query( $sql );
    878a2b
    878a2b
        	// Add users into LDAP directory
    878a2b
    878a2b
            array_push($htmlblock,'
  • Copying ...
  • ');
    878a2b
    878a2b
        	$sql = sprintf("SELECT uname, 
    878a2b
                                   name, 
    878a2b
                                   email, 
    878a2b
                                   pass
    878a2b
                                   FROM %s.%susers WHERE uid > 1", 
    878a2b
                                   $db->db_xoops_db, 
    878a2b
                                   $db->db_xoops_tbl );
    878a2b
    878a2b
            $result = $db->query( $sql );
    878a2b
    878a2b
    		$counter = 0;
    878a2b
    878a2b
    		while ( $entry = mysql_fetch_array( $result ) )
    878a2b
    		{
    878a2b
                // Add xoops.users into LDAP directory
    878a2b
                if ( $ldap->add_User( $entry ) === true )
    878a2b
                {
    878a2b
                    $counter++;
    878a2b
                }
    878a2b
            }
    878a2b
            
    878a2b
            array_push($htmlblock,'
  • '. $counter .' user(s) copied successfully.
  • ','');
    878a2b
    878a2b
            return $htmlblock;
    878a2b
        }
    878a2b
    878a2b
       /***
    878a2b
        * Categories 
    878a2b
        * 
    878a2b
        * Not copied. In phpBB there is no category.
    878a2b
        */
    878a2b
    878a2b
       /***
    878a2b
        * Copy Forum
    878a2b
        */
    878a2b
        function copy_Forums()
    878a2b
        {
    878a2b
            global $db;
    878a2b
    878a2b
            $htmlblock = array('

    Forums

    ', '
      ');
    878a2b
    878a2b
            // Clean up Forums
    878a2b
    878a2b
            array_push( $htmlblock, '
  • Cleanning up ...
  • ' );
    878a2b
    878a2b
            $sql = sprintf("TRUNCATE %s.%sforums;", 
    878a2b
                                $db->db_phpbb_db, 
    878a2b
                                $db->db_phpbb_tbl );
    878a2b
    878a2b
            $db->query( $sql );
    878a2b
    878a2b
            // Copy Forums
    878a2b
    878a2b
            array_push($htmlblock,'
  • Copying ...
  • ');
    878a2b
    878a2b
            $sql = sprintf("INSERT INTO %s.%sforums (forum_name,
    878a2b
                                                     forum_desc,
    878a2b
                                                     forum_topics,
    878a2b
                                                     forum_topics_real,
    878a2b
                                                     forum_type,
    878a2b
                                                     forum_posts) SELECT forum_name,
    878a2b
                                                                         forum_desc,
    878a2b
                                                                         forum_topics,
    878a2b
                                                                         forum_topics,
    878a2b
                                                                         (SELECT 1 AS forum_type),
    878a2b
                                                                         forum_posts FROM %s.%sbb_forums;",
    878a2b
                                $db->db_phpbb_db, $db->db_phpbb_tbl, 
    878a2b
                                $db->db_xoops_db, $db->db_xoops_tbl);
    878a2b
    878a2b
            $db->query( $sql );
    878a2b
    878a2b
            // Get forums recently added and update their order, ownership, and type. 
    878a2b
    878a2b
            $sql = sprintf('SELECT forum_id, left_id, right_id FROM %s.%sforums;', 
    878a2b
                                        $db->db_phpbb_db, 
    878a2b
                                        $db->db_phpbb_tbl );
    878a2b
    878a2b
            $result = $db->query( $sql );
    878a2b
    878a2b
            // Sanitize forum's order one by one
    878a2b
    878a2b
            $left_id    = 1;
    878a2b
            $right_id   = 2;
    878a2b
            $counter    = 0;
    878a2b
    878a2b
            while ( $row = mysql_fetch_array( $result ) )
    878a2b
            {
    878a2b
                $sql = sprintf("UPDATE %s.%sforums SET left_id = %d, right_id = %d WHERE forum_id = %d;", 
    878a2b
                                    $db->db_phpbb_db, 
    878a2b
                                    $db->db_phpbb_tbl, 
    878a2b
                                    $left_id, 
    878a2b
                                    $right_id, 
    878a2b
                                    $row['forum_id']);
    878a2b
    878a2b
                $db->query( $sql );
    878a2b
    878a2b
                $left_id  = $left_id + 2;
    878a2b
                $right_id = $left_id + 1;
    878a2b
    878a2b
                // Counter
    878a2b
    878a2b
                $counter++;
    878a2b
            }
    878a2b
    878a2b
            // Get amount of forums copied.
    878a2b
            array_push( $htmlblock, '
  • ' . $counter . ' forum(s) copied successfully.
  • ', '');
    878a2b
    878a2b
            return $htmlblock;
    878a2b
        }
    878a2b
    878a2b
       /*** 
    878a2b
        * Topics
    878a2b
        * 
    878a2b
        * When doing Topic copying the user used will be administrator. This means
    878a2b
        * that all topics and posts after migration will be own by the
    878a2b
        * administrator user. 
    878a2b
        */
    878a2b
        function copy_Topics()
    878a2b
        {
    878a2b
            global $db;
    878a2b
    878a2b
            $htmlblock = array('

    Topics

    ','
      ');
    878a2b
    878a2b
            // Claen up topics
    878a2b
    878a2b
            array_push( $htmlblock, '
  • Cleanning up ...
  • ' );
    878a2b
    878a2b
            $sql = sprintf("TRUNCATE %s.%stopics;",
    878a2b
                            $db->db_phpbb_db, 
    878a2b
                            $db->db_phpbb_tbl );
    878a2b
    878a2b
            $db->query( $sql );
    878a2b
    878a2b
            // Copy topics
    878a2b
    878a2b
            array_push( $htmlblock, '
  • Copying ...
  • ' );
    878a2b
    878a2b
            $sql = sprintf("INSERT INTO %s.%stopics (forum_id, 
    878a2b
                                                     topic_title, 
    878a2b
                                                     topic_time, 
    878a2b
                                                     topic_last_post_time,
    878a2b
                                                     topic_views, 
    878a2b
                                                     topic_last_poster_id,
    878a2b
                                                     topic_poster,
    878a2b
                                                     topic_replies,
    878a2b
                                                     topic_replies_real) SELECT forum_id, 
    878a2b
                                                              topic_title, 
    878a2b
                                                              topic_time, 
    878a2b
                                                              topic_time, 
    878a2b
                                                              topic_views, 
    878a2b
                                                              (SELECT 2 AS last_poster_id),
    878a2b
                                                              (SELECT 2 AS last_poster_id),
    878a2b
                                                              topic_replies, 
    878a2b
                                                              topic_replies 
    878a2b
                                                              FROM %s.%sbb_topics",
    878a2b
                                    $db->db_phpbb_db, $db->db_phpbb_tbl, 
    878a2b
                                    $db->db_xoops_db, $db->db_xoops_tbl );
    878a2b
    878a2b
            $db->query( $sql );
    878a2b
    878a2b
            array_push( $htmlblock, '
  • ' . mysql_affected_rows() . ' topic(s) copied successfully.
  • ', '' );
    878a2b
    878a2b
            return $htmlblock;
    878a2b
        }
    878a2b
    878a2b
       /***
    878a2b
        * Posts
    878a2b
        */
    878a2b
        function copy_Posts()
    878a2b
        {
    878a2b
            global $db;
    878a2b
    878a2b
            $htmlblock = array('

    Posts

    ','
      ');
    878a2b
            
    878a2b
            // Clean Up posts
    878a2b
    878a2b
            array_push($htmlblock,'
  • Cleanning up ...
  • ');
    878a2b
    878a2b
            $sql = sprintf("TRUNCATE %s.%sposts;", $db->db_phpbb_db, $db->db_phpbb_tbl);
    878a2b
    878a2b
            $db->query( $sql );
    878a2b
    878a2b
            // Copy Posts
    878a2b
            
    878a2b
            array_push( $htmlblock, '
  • Copying ...
  • ');
    878a2b
    878a2b
            $sql = sprintf("INSERT INTO %s.%sposts (topic_id,
    878a2b
                                                    forum_id,
    878a2b
                                                    poster_id,
    878a2b
                                                    post_time,
    878a2b
                                                    post_subject,
    878a2b
                                                    post_text) SELECT t1.topic_id,
    878a2b
                                                                      t1.forum_id,
    878a2b
                                                                      (SELECT 2 AS poster_id),
    878a2b
                                                                      t1.post_time,
    878a2b
                                                                      t1.subject,
    878a2b
                                                                      t2.post_text
    878a2b
                                                                      FROM %s.%sbb_posts t1 
    878a2b
                                                                      LEFT JOIN %s.%sbb_posts_text t2 
    878a2b
                                                                      ON t2.post_id = t1.post_id;",
    878a2b
                                                $db->db_phpbb_db, $db->db_phpbb_tbl,
    878a2b
                                                $db->db_xoops_db, $db->db_xoops_tbl,
    878a2b
                                                $db->db_xoops_db, $db->db_xoops_tbl);
    878a2b
    878a2b
            $db->query( $sql );
    878a2b
    878a2b
            array_push( $htmlblock, '
  • ' . mysql_affected_rows() .' posts(s) copied successfully.
  • ', '');
    878a2b
    878a2b
            return $htmlblock;
    878a2b
        }
    878a2b
    878a2b
       /***
    878a2b
        * Generate random password
    878a2b
        */
    878a2b
        function get_randomPass()
    878a2b
        {
    878a2b
            // Add lower case letters
    878a2b
            $seed = array('a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 
    878a2b
                          'i', 'j', 'k', 'i', 'l', 'm', 'n', 'o', 
    878a2b
                          'p', 'q', 'r', 's', 't', 'u', 'v', 'x', 
    878a2b
                          'y', 'z');
    878a2b
    878a2b
            // Add upper case letters
    878a2b
            foreach ( $seed as $value )
    878a2b
            {
    878a2b
                array_push( $seed, strtoupper($value) );
    878a2b
            }
    878a2b
    878a2b
            // Add numbers
    878a2b
            array_push( $seed, '1', '2', '3', '4', '5', '6', '7', '8', '9', '0' );
    878a2b
    878a2b
            // Use some symbols chars
    878a2b
            array_push( $seed, '!', '@', '#', '$', '%', '=', '/','+' );
    878a2b
    878a2b
            // Build password based on seed
    878a2b
            $userPassword   = '';
    878a2b
            $passwordLength = 20;
    878a2b
            for ($i = 0; $i < $passwordLength; $i++)
    878a2b
            {
    878a2b
                $userPassword = $userPassword . $seed[array_rand($seed)];
    878a2b
            }
    878a2b
    878a2b
            return $userPassword;
    878a2b
        }
    878a2b
    878a2b
       /***
    878a2b
        * Class Destruct
    878a2b
        * ----------------------------------------------------
    878a2b
        */
    878a2b
    878a2b
        function __destruct()
    878a2b
        {
    878a2b
        }
    878a2b
    }
    878a2b
    878a2b
    $newbb_to_phpbb = new NEWBB_TO_PHPBB;
    878a2b
    ?>