<?php
/***************************************************************************
 *                            convert_apb202.php
 *                            ------------------
 *   begin                : Thursday, Aug 01, 2002
 *   copyright            : Stefan Menzel
 *   email                : stiwi@lukasonline.com
 *
 *   $Id: convert_apb202.php,v 1.0 2002/08/01 00:00:00 Stiwi Exp $
 *
 *
 ***************************************************************************/

/***************************************************************************
 *
 *   This program is free software; you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation; either version 2 of the License, or
 *   (at your option) any later version.
 *
 ***************************************************************************/

/******
 *
 *	This converter will import:
 *
 *	- users with their avatar, settings, permissions
 *	- forums and their moderators, topics, posts
 *	- ban list
 * - config settings
 *
 *	This converter will NOT import:
 *
 * - board-news
 *
 * IMPORTANT NOTES:
 *
 * - This Converter will also work with APBoard version 2.0.3a / EW.
 *   But it will not convert the private messages and the announcements of this Version
 *
 * - If you would like to convert BBCode, that is'nt included in an unmodded phpBB2,
 *   like APBoards [center]-tag 
 *   you'll need to install the required "BBCode-MOD" BEFORE you start the conversion.
 *   Otherwise those tags will be stripped off by the converter.
 *
 * - If you would like to preserve the gender-information of the APBoard, 
 *   you should install the "Gender-MOD" BEFORE you start the conversion and 
 *   uncomment(remove //) line 210(find 'user_gender') of this file.
 *
 ******/

$converter_infos = array(
	'forum_name'		=>	'APBoard 2.0.2a',
	'version'			=>	'1.0',
	'phpbb_version'	=>	'2.0.2',
	'author'				=>	'<a href="http://www.phpbb.com/phpBB/profile.php?mode=viewprofile&u=17167" target="_new">Stiwi</a>',
	'table_prefix'		=>	'apb1_'
);

/*
$tables = array(
	'board_news',
	'boards',
	'categories',
	'censor',
	'chat',
	'config',
	'error_reports',
	'faq',
	'help',
	'language',
	'posts',
	'rate',
	'search',
	'sessions',
	'statistik',
	'styles',
	'threads',
	'user_table',
	'useronline',
	'uploads'
);
*/

$config_schema = array(
	'table_name'	=>	'config',
	'table_format'	=>	'fixed',
	
	'settings'	=> array(
		'sitename'			=> 'master_board_name',
		'board_startdate'	=> 'installdate'
	)
);


if ($get_info)
{
	return;
}

$converter = array(	
	'avatars_path'		=>	'userpics/',
	'smilies_path'		=>	'smilies/',
	'test_file'			=>	'getimagesize.php',
//	'execute_first'	=>	'',
	'execute_last'		=>	'apb_restore_mods();',

	'schema' => array(
		array(
			'target'		=>	CATEGORIES_TABLE,
			'query_first'	=>	'DELETE FROM ' . CATEGORIES_TABLE,
			
			array('cat_id',      'categories.catid',     ''),
			array('cat_title',   'categories.catname',   ''),
			array('cat_order',   'categories.sortit',    ''),
         
         'order by'     => 'categories.catid'
		),

		array(
			'target'			=>	FORUMS_TABLE,
			'query_first'	=>	'DELETE FROM ' . FORUMS_TABLE,
			
			array('forum_id',					'boards.boardid',					''),
			array('cat_id',					'boards.categoryparentid',		''),
			array('forum_name',				'boards.boardname',				''),
			array('forum_desc',				'boards.descriptiontext',		''),
			array('forum_order',				'boards.sortit',					''),
			array('forum_posts',				'boards.totalposts',				''),
			array('forum_last_post_id',	'boards.lastreplyproperties',	'apb_lastpostid'),
			array('forum_topics',			'boards.totalthreads',			''),
         
         'order by'     => 'boards.boardid'
		),		

      array(
			'target'			=>	POSTS_TABLE,
			'query_first'	=>	'DELETE FROM ' . POSTS_TABLE,
			
			array('post_id',				'posts.postid',				''),
			array('topic_id',				'posts.threadparentid',		''),
			array('forum_id',				'threads.boardparentid',	''),
			array('poster_id',			'posts.authorid',				''),
			array('post_username',		'posts.authorname',			''),
			array('post_time',			'posts.posttime',				''),
			array('poster_ip',			'posts.post_ip',				'encode_ip'),
			array('enable_bbcode',		1,									''),
			array('enable_smilies',		'posts.disable_smilies',   'not'),
			array('post_edit_time',		'posts.post_edit_time',    ''),
			array('post_edit_count',	'posts.post_edit_count',   ''),
			
         'left_join'		=>	'posts LEFT JOIN threads ON posts.threadparentid = threads.threadid',
			'order_by'		=>	'posts.postid ASC'
		),

		array(
			'target'			=>	POSTS_TEXT_TABLE,
			'query_first'	=>	'DELETE FROM ' . POSTS_TEXT_TABLE,
			
			array('post_id',		'posts.postid',	''),
			array('bbcode_uid',	'',					'make_uid'),
			array('post_text',	'posts.message',	'apb_prepare_message'),
			
			'order_by'		=>	'posts.postid ASC'
		),

		array(
			'target'			=>	TOPICS_TABLE,
			'query_first'  =>	'DELETE FROM ' . TOPICS_TABLE,
         
			array('topic_id',					'threads.threadid',							''),
			array('forum_id',					'threads.boardparentid',					''),
			array('topic_title',				'threads.threadname',						''),
			array('topic_poster',			'threads.authorid',							''),
			array('topic_time',				'MIN(posts.posttime) AS topic_time',	''),
			array('topic_views',				'threads.views',								''),
			array('topic_replies',			'threads.replies',							''),
			array('topic_status',			'threads.flags',								'is_topic_locked'),
			array('topic_first_post_id',	'MIN(posts.postid) AS first_post_id',	''),
			array('topic_last_post_id',	'MAX(posts.postid) AS last_post_id',	''),
         
			'where'			=>	'posts.threadparentid = threads.threadid',
			'group_by'		=>	'threads.threadid',
			'order_by'		=>	'threads.threadid ASC'
		),

		array(
			'target'			=>	USERS_TABLE,
			'query_first'	=>	'DELETE FROM ' . USERS_TABLE . ' WHERE user_id <> -1',
			
			array('user_id',					'user_table.userid',					''),
			array('user_active',				1,											''),
			array('username',					'user_table.username',				''),
		   array('user_password',			'user_table.userpassword',			'md5'),
         array('user_level',				'user_table.status',					'apb_userlevel'),
         array('user_regdate',			'user_table.regdate',				''),
         array('user_email',				'user_table.useremail',				''),
         array('user_posts',				'user_table.userposts',				''),
         array('user_rank',				'',										''),
			array('user_avatar',				'user_table.userpic',				'apb_avatar'),
			array('user_avatar_type',		'',										'apb_avatar_type'),
			array('user_icq',					'user_table.usericq',				'apb_validate_icq'),
			array('user_website',			'user_table.userhp',					'apb_remove_NA'),
			array('user_sig',					'user_table.signatur',				'apb_prepare_message'),
			array('user_sig_bbcode_uid',	'',										'make_uid'),
			array('user_interests',			'user_table.interests',				''),
			array('user_lastvisit',			'user_table.cookie_array',			'apb_lastvisit'),
			array('user_viewemail',			'user_table.show_email_global',	''),
// uncomment(remove //) next line if you've installed the "Gender-MOD" on your phpBB2
//			array('user_gender', 			'user_table.gender',					''),
			
			'order_by'	=>	'user_table.userid ASC'
		),

		array(
			'target'			=>	GROUPS_TABLE,
			'query_first'	=>	'DELETE FROM ' . GROUPS_TABLE . ' WHERE group_id > 1',
			
			array('group_id',				'user_table.userid',	'inc'),
			array('group_type',			GROUP_CLOSED,			''),
			array('group_name',			'',						''),
			array('group_description',	'Personal User',		''),
			array('group_moderator',	0,							''),
			array('group_single_user',	1,							''),
			
			'order_by'	=>	'user_table.userid ASC'
		),

		array(
			'target'			=>	USER_GROUP_TABLE,
			'query_first'	=>	'DELETE FROM ' . USER_GROUP_TABLE . ' WHERE user_id <> -1',
			
			array('group_id',			'user_table.userid',	'inc'),
			array('user_id',			'user_table.userid',	''),
			array('user_pending',	0,							''),
			
			'order_by'	=>	'user_table.userid ASC'
		),

		array(
			'target'		=>	BANLIST_TABLE,
			'query_first'	=>	'DELETE FROM ' . BANLIST_TABLE ,
			
			array('ban_userid',  'user_table.userid',  ''),
			
         'where'     =>   'user_table.status = "BANNED"',
			'order_by'  =>   'user_table.userid ASC'
		))
);


//
// Converter-specific functions
//

function apb_lastpostid($serial)
{
	$lastreplyproperties = unserialize($serial);
	return $lastreplyproperties["postid"];
}

function apb_lastvisit($serial)
{
	$cookie_array = unserialize($serial);
	return $cookie_array["last_visit"];
}

function apb_prepare_message($message)
{
	global $row, $board_config;
	
	// first we need to convert/delete some APBoard-specific BBCodes
	// wich are not handled by convert_bbcode()

	$str_from = array (	'[f1]', '[/f1]',
								'[f2]', '[/f2]',
								'[f3]', '[/f3]',
								'[f4]', '[/f4]',
								'[f5]', '[/f5]',
								'[red]', '[/red]',
								'[blue]', '[/blue]',
								'[green]', '[/green]',
								'[yellow]', '[/yellow]',
								'[white]', '[/white]',
								'[black]', '[/black]',
								'[verdana]', '[/verdana]',
								'[arial]', '[/arial]',
								'[courier]', '[/courier]',
								'[comic]', '[/comic]',
								'[revL]', '[/revL]',
								'[revW]', '[/revW]');
		
	$str_to = array (	'[size=9]','[/size]',
							'[size=12]','[/size]',
							'[size=18]','[/size]',
							'[size=24]','[/size]',
							'[size=29]','[/size]',
							'[color=red]', '[/color]',
							'[color=blue]', '[/color]',
							'[color=green]', '[/color]',
							'[color=yellow]', '[/color]',
							'[color=white]', '[/color]',
							'[color=black]', '[/color]',
							'', '',
							'', '',
							'', '',
							'', '',
							'', '',
							'', '');
	
	// if PHP-Version is above 4.0.4 we use str_replace with arrays, otherwise we do a loop
	if (phpversion() >= '4.0.5')
	{
		$message = str_replace ($str_from, $str_to, $message);
	}
	else
	{
		for ($i = 0; $i < count($str_from); ++$i)
		{
			$message = str_replace($str_from[$i], $str_to[$i], $message);
		}
	}
	$uid = make_uid();
	
	// We do a bbencode_first_pass() here, so known non-standard-BBCodes won't be deleted, 
	// when they where added by a MOD. 
	// See "Important Note" in this file's header
	$message = bbencode_first_pass($message, $uid);
	$message = convert_bbcode($message, TRUE, TRUE);
	$message = prepare_message($message, TRUE, $board_config['allow_bbcode'], empty($row['disable_smilies']), $uid);

	return $message;
}

function apb_userlevel($apb_level)
{
	$phpbb_level = 0;
	
	switch ($apb_level) 
	{
		case "NORMAL":
			$phpbb_level = USER;
			break;
		case "MOD":
			$phpbb_level = MOD;
			break;
		case "ADMIN":
			$phpbb_level = ADMIN;
			break;
		case "BANNED":
			$phpbb_level = USER;
			break;
	}
	return $phpbb_level;
}

function apb_avatar($avatar)
{
	if (empty($avatar))
	{
		return;
	}
	
	$src = basename($avatar);
	
	if (import_avatar($src))
	{
		return $src;
	}
}

function apb_remove_NA($in)
{
	$out = str_replace("[N/A]", "", $in);
	
	return trim($out);
}

function apb_validate_icq($in)
{
	$in = apb_remove_NA($in);
	
	$out = validate_icq($in);
	
	return $out;
}

function apb_avatar_type()
{
	global $row;
	trim($row["userpic"]) == "" ? $out = "" : $out = 1;
	return $out;
}

function apb_restore_mods()
{
	global $db, $src_table_prefix;
	
	$db->sql_query('DELETE FROM ' . AUTH_ACCESS_TABLE);
	
	$sql = "SELECT * FROM ".$src_table_prefix."boards WHERE trim(boardmods) != ''";
	if (!$result = $db->sql_query($sql))
	{
		message_die(GENERAL_ERROR, 'Could not retrieve forums data', '', __LINE__, __FILE__, $sql);
	}
	while ($row = $db->sql_fetchrow($result))
	{
		$sql2 = "SELECT * FROM ".$src_table_prefix."user_table WHERE username in ('" . str_replace(",", "','", str_replace(" ", "",$row['boardmods'])) . "')";
		if (!$result2 = $db->sql_query($sql2))
		{
			message_die(GENERAL_ERROR, 'Could not retrieve user data', '', __LINE__, __FILE__, $sql2);
		}
		while ($row2 = $db->sql_fetchrow($result2))
		{
			$db->sql_query('INSERT INTO ' . AUTH_ACCESS_TABLE . '(group_id, forum_id, auth_mod) VALUES(' . ($row2['userid']+1) . ', ' . $row['boardid'] . ', 1)');
		}
	
	}
}

?>