GetSection($section); //Set Environment Variable $envar = 'env='.BuildEnv(); $ImportTable = $objSession->GetSessionTable('BBImport',''); $sql_type = $objSession->GetVariable("import_sql_type"); $db_server = $objSession->GetVariable("import_server"); $db_name = $objSession->GetVariable("import_db"); $db_user = $objSession->GetVariable("import_user"); $db_pass = $objSession->GetVariable("import_pass"); // made connection to source database $db =& ADONewConnection($sql_type); $db->NConnect($db_server, $db_user, $db_pass, $db_name); // get connection to destination database $dst_db =&GetADODBConnection(); // if connection error occured, then say this if(!$db) { echo "Database connection failed. DB Type: $sql_type, DB Server: $inlink_server, DB User: $inlink_user, DB Name: $inlink_db"; //fatal; echo $db->ErrorMsg(); exit; } // global vars if( GetVar('bb_prefix') ) $objSession->SetVariable('import_table_prefix', GetVar('bb_prefix') ); $bb_prefix = $objSession->GetVariable('import_table_prefix'); // If script 1st run -> create temporary // import table in destination database // define import steps $import_steps = Array(); $import_steps['groups'] = Array('id' => 'groups', 'caption' => 'Groups'); // groups $import_steps['groups']['count_sql'] = 'SELECT COUNT(DISTINCT g.group_id) FROM '.$bb_prefix.'groups g, '.$bb_prefix.'auth_access aa WHERE (g.group_id = aa.group_id)'; $import_steps['users'] = Array('id' => 'users', 'caption' => 'Users, avatars'); // users + avatars $import_steps['users']['count_sql'] = 'SELECT COUNT(*) FROM '.$bb_prefix.'users'; $import_steps['user_banlist'] = Array('id' => 'user_banlist', 'caption' => 'Users Banrules'); // banned users $import_steps['user_banlist']['count_sql'] = 'SELECT COUNT(*) FROM '.$bb_prefix.'banlist'; $import_steps['cats'] = Array('id' => 'cats', 'caption' => 'Categories'); $import_steps['cats']['count_sql'] = 'SELECT COUNT(*) FROM '.$bb_prefix.'categories'; $import_steps['forums'] = Array('id' => 'forums', 'caption' => 'Forums'); $import_steps['forums']['count_sql'] = 'SELECT COUNT(*) FROM '.$bb_prefix.'forums'; $import_steps['forums_perms'] = Array('id' => 'forums_perms', 'caption' => 'Forum Permissions'); $import_steps['forums_perms']['count_sql'] = 'SELECT COUNT(*) FROM '.$bb_prefix.'auth_access'; $import_steps['topics'] = Array('id' => 'topics', 'caption' => 'Topics'); $import_steps['topics']['count_sql'] = 'SELECT COUNT(*) FROM '.$bb_prefix.'topics'; $import_steps['posts'] = Array('id' => 'posts', 'caption' => 'Posts'); $import_steps['posts']['count_sql'] = 'SELECT COUNT(*) FROM '.$bb_prefix.'posts'; $StepIndex = Array( 1 => 'groups', 2 => 'users', 3 => 'user_banlist', 4 => 'cats', 5 => 'forums', 6 => 'forums_perms', 7 => 'topics', 8 => 'posts'); if( GetVar('Action') == 'Import' ) { $sql = 'CREATE TABLE '.$ImportTable.'( bbi_id int(11) unsigned NOT NULL auto_increment, bbi_step varchar(20) NOT NULL default \'\', bbi_cache longtext NOT NULL, bbi_percent tinyint(3) NOT NULL default \'0\', bbi_details varchar(100) NOT NULL default \'\', bbi_current tinyint(1) NOT NULL default \'0\', bbi_rec_no int(11) unsigned NOT NULL default \'0\', bbi_recs_count int(11) unsigned NOT NULL default \'0\', PRIMARY KEY (bbi_id) )'; $dst_db->Execute($sql); // set initial info $sql = 'INSERT INTO '.$ImportTable.'(bbi_step,bbi_cache,bbi_recs_count) VALUES(\'%s\',\''.serialize( Array() ).'\',%s)'; foreach ($import_steps as $is_name => $is_data) { $recs_count = $db->GetOne( $is_data['count_sql'] ); $tmp = sprintf($sql, $is_name, $recs_count); $dst_db->Execute($tmp); } // set 1st step $dst_db->Execute('UPDATE '.$ImportTable.' SET bbi_current = 1 WHERE bbi_step = \''.$StepIndex[1].'\''); $objSession->SetVariable('import_begins', GetMC() ); } // update import step info with current information $sql = 'SELECT * FROM '.$ImportTable; $rs =& $dst_db->Execute($sql); $current_step = Array(); // current step info $cache = Array(); while(!$rs->EOF) { $rec =& $rs->fields; $is =& $import_steps[ $rec['bbi_step'] ]; $is['percent'] = $rec['bbi_percent']; $is['details'] = $rec['bbi_details']; $is['rec_no'] = $rec['bbi_rec_no']; $is['recs_count'] = $rec['bbi_recs_count']; $is['current'] = $rec['bbi_current']; $tmp = unserialize( $rec['bbi_cache'] ); $cache[ $rec['bbi_step'] ] = $tmp; if( $rec['bbi_current'] == 1 ) { $is['perpage'] = constant( strtoupper($is['id']).'_PERPAGE' ); $current_step = $is; $ImportFunction = 'import_'.$is['id']; } $rs->MoveNext(); } // Error Reporting if( IsDebugMode() ) { error_reporting(E_ALL); ini_set('display_errors', 1); } // draw import screen DrawScreen($import_steps); // category/forum specific $import_category_id = $objSession->GetVariable('import_category_id'); // user specific define('DT_USER', 6); // user data type $user_regular = $objSession->GetVariable("user_regular_values"); $user_group =& new clsPortalGroup($user_regular); $perm_mappings = Array( 'CATEGORY.VIEW' => 'auth_view', 'TOPIC.VIEW' => 'auth_read', 'TOPIC.REPLY.VIEW' => 'auth_read', 'TOPIC.ADD' => 'auth_post', 'TOPIC.REPLY.ADD' => 'auth_reply', 'TOPIC.OWNER.MODIFY' => 'auth_edit', 'TOPIC.REPLY.OWNER.MODIFY' => 'auth_edit', 'TOPIC.OWNER.DELETE' => 'auth_delete', 'TOPIC.REPLY.OWNER.DELETE' => 'auth_delete'); // begin processing $import_steps[ $current_step['id'] ]['rec_no'] += $ImportFunction(); // based on step name $current_step['rec_no'] = $import_steps[ $current_step['id'] ]['rec_no']; StepRedirect($current_step); // end processing function import_groups() { global $cache, $current_step, $objGroups, $db, $bb_prefix, $import_steps; // import administrative & personal groups $sql = 'SELECT DISTINCT g.group_id, g.group_name, g.group_single_user, g.group_description FROM '.$bb_prefix.'groups g, '.$bb_prefix.'auth_access aa WHERE (g.group_id = aa.group_id) LIMIT '.$current_step['rec_no'].','.$current_step['perpage']; $rs = $db->Execute($sql); $recs_count = $rs->RecordCount(); while(!$rs->EOF) { $rec =& $rs->fields; $gprefix = ($rec['group_single_user'] == 1) ? '_' : 'phpbb_'; $group = $objGroups->Add_Group($gprefix.$rec['group_name'], $rec['group_description'], 0); $group_id = $group->UniqueId(); // add created group to cache $cache['groups'][ $rec['group_id'] ] = Array('new_id' => $group_id ); unset($group); $rs->MoveNext(); } return $recs_count; } function import_users() { global $cache, $current_step, $objUsers, $objGroups, $db, $bb_prefix, $import_steps, $user_regular, $user_group; $pesistant_vars = Array('my_signature' => 'user_sig', 'bb_posts' => 'user_posts', 'smileys' => 'user_allowsmile', 'bbcode' => 'user_allowbbcode', 'show_sig' => 'user_attachsig'); $uf_defs = Array( 'user_msn' => 'user_msnm', 'user_icq' => 'user_icq', 'user_aim' => 'user_aim', 'user_yahoo' => 'user_yim', 'user_website' => 'user_website', 'user_occupation' => 'user_occ', 'user_interests' => 'user_interests', 'user_from' => 'user_from'); if( $current_step['rec_no'] < $current_step['perpage']) { // create custom fields for in-bulletin users (and show them on general tab) $user_fields = new clsCustomFieldList(DT_USER); $user_fields_created = $user_fields->GetFieldNames(); $application =& kApplication::Instance(); $ml_helper =& $application->recallObject('kMultiLanguageHelper'); foreach ($uf_defs as $pending_uf => $no_in_use) { if (in_array($pending_uf, $user_fields_created)) continue; $user_fields->AddField(DT_USER, $pending_uf, '', 1, 'la_bb', 'lu_fieldcustom__'.$pending_uf, 'text', ''); } $ml_helper->createFields('u-cdata', true); } // get users //$cache['users'] = Array(); $sql = 'SELECT * FROM '.$bb_prefix.'users ORDER BY user_id LIMIT '.$current_step['rec_no'].','.$current_step['perpage']; $rs = $db->Execute($sql); $recs_count = $rs->RecordCount(); // Create Anonymous User $user =& $objUsers->Add_User('Guest', '', 'anonymous@user.com', adodb_mktime(), '', '', 1, '', '', '', '', '', ''); $cache['users'][ -1 ] = Array('new_id' => $user->UniqueId(), 'username' => 'Guest'); $cache['users'][ 0 ] = Array('new_id' => $user->UniqueId(), 'username' => 'Guest'); unset($user); $missing_pers_groups = Array(); while(!$rs->EOF) { $rec =& $rs->fields; /*$address = explode(',', $rec['user_from']); // state, country foreach($address as $id => $addr_part) $address[$id] = trim($addr_part); $user_state = isset( $address[0] ) ? $address[0] : ''; $user_country = isset( $address[1] ) ? $address[1] : '';*/ $user =& $objUsers->Add_User($rec['username'], $rec['user_password'], $rec['user_email'], $rec['user_regdate'], '', '', $rec['user_active'], '', '', '', '', '', ''); $user_id = $user->UniqueId(); // set user pesistant variables (why not custom fields) foreach($pesistant_vars as $dst_field => $src_field) { SetPersistantVariable($user_id, $dst_field, $rec[$src_field]); } SetPersistantVariable($user_id, 'bb_signatures', '1'); // show other signatures // set user custom fields foreach($uf_defs as $dst_field => $src_field) $user->SetCustomField($dst_field, $rec[$src_field]); $user->SaveCustomFields(); // add user to user_group specified (in import step 3) $user_group->AddUser($user_id, 1,false); // make it primary // add user to it's personal group $user_pers_group = $objGroups->GetItemByField('Name','_'.$rec['username']); if( is_object($user_pers_group) ) $user_pers_group->AddUser($user_id, 0,false); // make it supplimentary else $missing_pers_groups[] = $rec['username']; unset($user_pers_group); // add created user to cache $cache['users'][ $rec['user_id'] ] = Array('new_id' => $user_id, 'username' => $rec['username']); unset($user); $rs->MoveNext(); } return $recs_count; } function import_user_banlist() { global $cache, $current_step, $objBanList, $db, $dst_db, $bb_prefix, $import_steps; // get ban rules $sql = 'SELECT * FROM '.$bb_prefix.'banlist ORDER BY ban_id LIMIT '.$current_step['rec_no'].','.$current_step['perpage']; $rs = $db->Execute($sql); $recs_count = $rs->RecordCount(); $banrule_priority = $dst_db->GetOne('SELECT MAX(Priority) FROM '.GetTablePrefix().'BanRules'); while(!$rs->EOF) { $rec =& $rs->fields; if( $rec['ban_userid'] ) { $tmp =& $objBanList->AddRule(DT_USER, 0, 'Login',1, $cache['users'][ $rec['ban_userid'] ]['username'], $banrule_priority, 1); unset($tmp); $banrule_priority++; } if( $rec['ban_ip'] ) { $tmp =& $objBanList->AddRule(DT_USER, 0, 'ip',1, DecodeIP( $rec['ban_ip'] ), $banrule_priority, 1); unset($tmp); $banrule_priority++; } if( $rec['ban_email'] ) { $tmp =& $objBanList->AddRule(DT_USER, 0, 'Email',1, $rec['ban_email'], $banrule_priority, 1); unset($tmp); $banrule_priority++; } $rs->MoveNext(); } return $recs_count; } function import_cats() { global $cache, $current_step, $objCatList, $db, $bb_prefix, $import_steps, $import_category_id; // get categories //$cache['cats'] = Array(); $cache['all_cats'] = Array(); $sql = 'SELECT * FROM '.$bb_prefix.'categories ORDER BY cat_order LIMIT '.$current_step['rec_no'].','.$current_step['perpage']; $rs = $db->Execute($sql); $recs_count = $rs->RecordCount(); while(!$rs->EOF) { $rec =& $rs->fields; $NewOrder = $rec['cat_order'] == 1 ? 0 : $rec['cat_order'] / -10; $fields_hash = Array( 'ParentId' => $import_category_id, 'Name' => $rec['cat_title'], 'Description' => $rec['cat_title'], 'CreatedOn' => adodb_mktime(), 'EditorsPick' => 0, 'Status' => 1, 'HotItem' => 2, 'NewItem' => 2, 'PopItem' => 2, 'Priority' => $NewOrder, 'MetaKeywords' => '', 'MetaDescription' => '', 'AutomaticFilename' => 1, 'Filename' => '', 'CategoryTemplate' => '', 'ItemTemplate' => '', ); $category =& $objCatList->Add_NEW($fields_hash); $cache['cats'][ $rec['cat_id'] ] = Array( 'new_id' => $category->UniqueId() ); $cache['all_cats'][] = $cache['cats'][ $rec['cat_id'] ]['new_id']; unset($category); $rs->MoveNext(); } return $recs_count; } function import_forums() { global $cache, $current_step, $objCatList, $db, $dst_db, $bb_prefix, $import_steps, $user_regular, $import_category_id, $perm_mappings; // get forums //$cache['forums'] = Array(); $sql = 'SELECT * FROM '.$bb_prefix.'forums ORDER BY cat_id LIMIT '.$current_step['rec_no'].','.$current_step['perpage']; $rs = $db->Execute($sql); $recs_count = $rs->RecordCount(); while(!$rs->EOF) { $rec =& $rs->fields; $NewOrder = $rec['forum_order'] == 1 ? 0 : $rec['forum_order'] / -10; $category =& $objCatList->Add( $cache['cats'][ $rec['cat_id'] ]['new_id'], $rec['forum_name'], $rec['forum_desc'], adodb_mktime(), 0, 1, 2, 2, 2, $NewOrder, '', ''); $category_id = $category->UniqueId(); $cache['forums'][ $rec['forum_id'] ] = Array('new_id' => $category_id); // set forum permissions to "Member" group or group what user specified $objPermList = new clsPermList($category_id, $user_regular); $objPermList->LoadCategory($category_id); foreach($perm_mappings as $perm_name => $perm_field) { $HasPerm = $rec[$perm_field] == 0 || $rec[$perm_field] == 1; $objPermList->Set_Permission($category_id, $user_regular, $perm_name, $HasPerm, 0); } unset($category); $rs->MoveNext(); } // fix all priorities $all_cats = array($import_category_id); foreach($cache['forums'] as $k => $v) { $all_cats[] = $v['new_id']; } foreach($cache['cats'] as $k => $v) { $all_cats[] = $v['new_id']; } $sql = 'SELECT ParentId, MIN(Priority) AS MinPri FROM '.$objCatList->SourceTable.' WHERE ParentId IN ('.implode(',', $all_cats) .') GROUP BY ParentId'; $rs = $dst_db->Execute($sql); while(!$rs->EOF) { $rec =& $rs->fields; $MinPri = $rec['MinPri']; $sql = 'UPDATE '.$objCatList->SourceTable.' SET Priority = Priority + ABS('.$MinPri.') WHERE ParentId = '.$rec['ParentId']; $dst_db->Execute($sql); $rs->MoveNext(); } return $recs_count; } function import_forums_perms() { global $cache, $current_step, $db, $bb_prefix, $import_steps, $import_category_id, $perm_mappings, $user_regular; // get user-per-forum permissions //$cache['forums_perms'] = Array(); $sql = 'SELECT * FROM '.$bb_prefix.'auth_access LIMIT '.$current_step['rec_no'].','.$current_step['perpage']; $rs = $db->Execute($sql); $recs_count = $rs->RecordCount(); while(!$rs->EOF) { $rec =& $rs->fields; $category_id = $cache['forums'][ $rec['forum_id'] ]['new_id']; // forum category $user_group = $cache['groups'][ $rec['group_id'] ]['new_id']; $objPermList = new clsPermList($category_id, $user_group); $objPermList->LoadCategory($category_id); foreach($perm_mappings as $perm_name => $perm_field) { $objPermList->Set_Permission($category_id, $user_group, $perm_name, $rec[$perm_field], 0); } $rs->MoveNext(); } return $recs_count; } function import_topics() { global $cache, $current_step, $db, $objTopicList, $bb_prefix, $import_steps, $import_category_id; // get topics //$cache['topics'] = Array(); $sql = 'SELECT * FROM '.$bb_prefix.'topics ORDER BY forum_id LIMIT '.$current_step['rec_no'].','.$current_step['perpage']; $rs = $db->Execute($sql); $recs_count = $rs->RecordCount(); while(!$rs->EOF) { $rec =& $rs->fields; $rec['topic_replies'] += 1; $topic =& $objTopicList->Add_Topic( $rec['topic_title'], $cache['forums'][ $rec['forum_id'] ]['new_id'], 1, 0, 0, $rec['topic_time'], GetUserID( $rec['topic_poster'] ), Array( 'Posts' => $rec['topic_replies'], 'Views' => $rec['topic_views'], 'Modified' => $rec['topic_time'], 'PostedBy' => GetUserName($rec['topic_poster']) ) ); $cache['topics'][ $rec['topic_id'] ] = Array( 'new_id' => $topic->UniqueId() ); unset($topic); $rs->MoveNext(); } //unset($objTopicList); return $recs_count; } function import_posts() { global $cache, $current_step, $db, $dst_db, $bb_prefix, $import_steps, $import_category_id; // get posts //$cache['users'] = Array(); $sql = 'SELECT * FROM '.$bb_prefix.'posts p LEFT JOIN '.$bb_prefix.'posts_text pt ON pt.post_id = p.post_id ORDER BY topic_id LIMIT '.$current_step['rec_no'].','.$current_step['perpage']; $rs = $db->Execute($sql); $recs_count = $rs->RecordCount(); $post =& new clsPosting(); while(!$rs->EOF) { $rec =& $rs->fields; $post->Set('ResourceId', 0); $post->Set('PostingId', 0); // Map post options (enable_sig, enable_bbcode, enable_smilies) $post->SetPostOption('show_sig', $rec['enable_sig']); $post->SetPostOption('disable_bbcode', !$rec['enable_bbcode']); $post->SetPostOption('disable_smileys', !$rec['enable_smilies']); $post->Set( Array('IPAddress', 'PosterAlias', 'Subject', 'PostingText', 'CreatedOn', 'CreatedById', 'TopicId' ), Array( DecodeIP( $rec['poster_ip'] ), GetUserName( $rec['poster_id'] ), $rec['post_subject'], $rec['post_text'], $rec['post_time'], GetUserID( $rec['poster_id'] ), $cache['topics'][ $rec['topic_id'] ]['new_id'] ) ); $post->Create(); //$cache['posts'][ $rec['post_id'] ] = Array( 'new_id' => $post->UniqueId() ); $size = strlen(serialize($cache)); //unset($post); $rs->MoveNext(); } if($current_step['recs_count'] == $current_step['rec_no'] + $recs_count) { // set lastpostid for each topic $sql = 'SELECT PostingId, MAX(CreatedOn) AS pt, TopicId FROM '.GetTablePrefix().'Posting GROUP BY TopicId ORDER BY CreatedOn DESC'; $rs = $dst_db->Execute($sql); $update_sql = 'UPDATE '.GetTablePrefix().'Topic SET LastPostId = %s, LastPostDate = %s WHERE TopicId = %s'; while(!$rs->EOF) { $rec =& $rs->fields; $tmp = sprintf($update_sql, $rec['PostingId'], $rec['pt'], $rec['TopicId']); $dst_db->Execute($tmp); $rs->MoveNext(); } /*$sql = 'SELECT topic_id, MAX(post_time) AS pt, post_id FROM '.$bb_prefix.' posts GROUP BY topic_id ORDER BY post_time DESC'; $rs = $db->Execute($sql); $update_sql = 'UPDATE '.GetTablePrefix().'Topic SET LastPostId = %s, LastPostDate = %s WHERE TopicId = %s'; while(!$rs->EOF) { $rec =& $rs->fields; $TopicId = $cache['topics'][ $rec['topic_id'] ]['new_id']; $tmp = sprintf($update_sql, $cache['posts'][ $rec['post_id'] ]['new_id'], $rec['pt'], $TopicId); $dst_db->Execute($tmp); $rs->MoveNext(); }*/ } return $recs_count; } ?>