Blame Identity/Webenv/App/phpBB/3.0.4/install/install_convert.php

f2e824
f2e824
/**
f2e824
*
f2e824
* @package install
f2e824
* @version $Id: install_convert.php 8814 2008-09-04 12:01:47Z acydburn $
f2e824
* @copyright (c) 2006 phpBB Group
f2e824
* @license http://opensource.org/licenses/gpl-license.php GNU Public License
f2e824
*
f2e824
*/
f2e824
f2e824
/**
f2e824
*/
f2e824
f2e824
if (!defined('IN_INSTALL'))
f2e824
{
f2e824
	// Someone has tried to access the file direct. This is not a good idea, so exit
f2e824
	exit;
f2e824
}
f2e824
f2e824
if (!empty($setmodules))
f2e824
{
f2e824
	$module[] = array(
f2e824
		'module_type'		=> 'install',
f2e824
		'module_title'		=> 'CONVERT',
f2e824
		'module_filename'	=> substr(basename(__FILE__), 0, -strlen($phpEx)-1),
f2e824
		'module_order'		=> 20,
f2e824
		'module_subs'		=> '',
f2e824
		'module_stages'		=> array('INTRO', 'SETTINGS', 'IN_PROGRESS', 'FINAL'),
f2e824
		'module_reqs'		=> ''
f2e824
	);
f2e824
}
f2e824
f2e824
/**
f2e824
* Class holding all convertor-specific details.
f2e824
* @package install
f2e824
*/
f2e824
class convert
f2e824
{
f2e824
	var $options = array();
f2e824
f2e824
	var $convertor_tag = '';
f2e824
	var $src_dbms = '';
f2e824
	var $src_dbhost = '';
f2e824
	var $src_dbport = '';
f2e824
	var $src_dbuser = '';
f2e824
	var $src_dbpasswd = '';
f2e824
	var $src_dbname = '';
f2e824
	var $src_table_prefix = '';
f2e824
f2e824
	var $convertor_data = array();
f2e824
	var $tables = array();
f2e824
	var $config_schema = array();
f2e824
	var $convertor = array();
f2e824
	var $src_truncate_statement = 'DELETE FROM ';
f2e824
	var $truncate_statement = 'DELETE FROM ';
f2e824
f2e824
	var $fulltext_search;
f2e824
f2e824
	// Batch size, can be adjusted by the conversion file
f2e824
	// For big boards a value of 6000 seems to be optimal
f2e824
	var $batch_size = 2000;
f2e824
	// Number of rows to be inserted at once (extended insert) if supported
f2e824
	// For installations having enough memory a value of 60 may be good.
f2e824
	var $num_wait_rows = 20;
f2e824
f2e824
	// Mysqls internal recoding engine messing up with our (better) functions? We at least support more encodings than mysql so should use it in favor.
f2e824
	var $mysql_convert = false;
f2e824
f2e824
	var $p_master;
f2e824
f2e824
	function convert(&$p_master)
f2e824
	{
f2e824
		$this->p_master = &$p_master;
f2e824
	}
f2e824
}
f2e824
f2e824
/**
f2e824
* Convert class for conversions
f2e824
* @package install
f2e824
*/
f2e824
class install_convert extends module
f2e824
{
f2e824
	/**
f2e824
	* Variables used while converting, they are accessible from the global variable $convert
f2e824
	*/
f2e824
	function install_convert(&$p_master)
f2e824
	{
f2e824
		$this->p_master = &$p_master;
f2e824
	}
f2e824
f2e824
	function main($mode, $sub)
f2e824
	{
f2e824
		global $lang, $template, $phpbb_root_path, $phpEx, $cache, $config, $language, $table_prefix;
f2e824
		global $convert;
f2e824
f2e824
		$this->tpl_name = 'install_convert';
f2e824
		$this->mode = $mode;
f2e824
f2e824
		$convert = new convert($this->p_master);
f2e824
f2e824
		switch ($sub)
f2e824
		{
f2e824
			case 'intro':
f2e824
				// Try opening config file
f2e824
				// @todo If phpBB is not installed, we need to do a cut-down installation here
f2e824
				// For now, we redirect to the installation script instead
f2e824
				if (@file_exists($phpbb_root_path . 'config.' . $phpEx))
f2e824
				{
f2e824
					include($phpbb_root_path . 'config.' . $phpEx);
f2e824
				}
f2e824
f2e824
				if (!defined('PHPBB_INSTALLED'))
f2e824
				{
f2e824
					$template->assign_vars(array(
f2e824
						'S_NOT_INSTALLED'		=> true,
f2e824
						'TITLE'					=> $lang['BOARD_NOT_INSTALLED'],
f2e824
						'BODY'					=> sprintf($lang['BOARD_NOT_INSTALLED_EXPLAIN'], append_sid($phpbb_root_path . 'install/index.' . $phpEx, 'mode=install&language=' . $language)),
f2e824
					));
f2e824
f2e824
					return;
f2e824
				}
f2e824
f2e824
				require($phpbb_root_path . 'config.' . $phpEx);
f2e824
				require($phpbb_root_path . 'includes/constants.' . $phpEx);
f2e824
				require($phpbb_root_path . 'includes/db/' . $dbms . '.' . $phpEx);
f2e824
				require($phpbb_root_path . 'includes/functions_convert.' . $phpEx);
f2e824
f2e824
				$db = new $sql_db();
f2e824
				$db->sql_connect($dbhost, $dbuser, $dbpasswd, $dbname, $dbport, false, true);
f2e824
				unset($dbpasswd);
f2e824
f2e824
				// We need to fill the config to let internal functions correctly work
f2e824
				$sql = 'SELECT *
f2e824
					FROM ' . CONFIG_TABLE;
f2e824
				$result = $db->sql_query($sql);
f2e824
f2e824
				$config = array();
f2e824
				while ($row = $db->sql_fetchrow($result))
f2e824
				{
f2e824
					$config[$row['config_name']] = $row['config_value'];
f2e824
				}
f2e824
				$db->sql_freeresult($result);
f2e824
f2e824
				// Detect if there is already a conversion in progress at this point and offer to resume
f2e824
				// It's quite possible that the user will get disconnected during a large conversion so they need to be able to resume it
f2e824
				$new_conversion = request_var('new_conv', 0);
f2e824
f2e824
				if ($new_conversion)
f2e824
				{
f2e824
					$config['convert_progress'] = '';
f2e824
					$config['convert_db_server'] = '';
f2e824
					$config['convert_db_user'] = '';
f2e824
					$db->sql_query('DELETE FROM ' . CONFIG_TABLE . "
f2e824
						WHERE config_name = 'convert_progress'
f2e824
							OR config_name = 'convert_db_server'
f2e824
							OR config_name = 'convert_db_user'"
f2e824
					);
f2e824
				}
f2e824
f2e824
				// Let's see if there is a conversion in the works...
f2e824
				$options = array();
f2e824
				if (!empty($config['convert_progress']) && !empty($config['convert_db_server']) && !empty($config['convert_db_user']) && !empty($config['convert_options']))
f2e824
				{
f2e824
					$options = unserialize($config['convert_progress']);
f2e824
					$options = array_merge($options, unserialize($config['convert_db_server']), unserialize($config['convert_db_user']), unserialize($config['convert_options']));
f2e824
				}
f2e824
f2e824
				// This information should have already been checked once, but do it again for safety
f2e824
				if (!empty($options) && !empty($options['tag']) &&
f2e824
					isset($options['dbms']) &&
f2e824
					isset($options['dbhost']) &&
f2e824
					isset($options['dbport']) &&
f2e824
					isset($options['dbuser']) &&
f2e824
					isset($options['dbpasswd']) &&
f2e824
					isset($options['dbname']) &&
f2e824
					isset($options['table_prefix']))
f2e824
				{
f2e824
					$this->page_title = $lang['CONTINUE_CONVERT'];
f2e824
f2e824
					$template->assign_vars(array(
f2e824
						'TITLE'			=> $lang['CONTINUE_CONVERT'],
f2e824
						'BODY'			=> $lang['CONTINUE_CONVERT_BODY'],
f2e824
						'L_NEW'			=> $lang['CONVERT_NEW_CONVERSION'],
f2e824
						'L_CONTINUE'	=> $lang['CONTINUE_OLD_CONVERSION'],
f2e824
						'S_CONTINUE'	=> true,
f2e824
f2e824
						'U_NEW_ACTION'		=> $this->p_master->module_url . "?mode={$this->mode}&sub=intro&new_conv=1&language=$language",
f2e824
						'U_CONTINUE_ACTION'	=> $this->p_master->module_url . "?mode={$this->mode}&sub=in_progress&tag={$options['tag']}{$options['step']}&language=$language",
f2e824
					));
f2e824
f2e824
					return;
f2e824
				}
f2e824
f2e824
				$this->list_convertors($sub);
f2e824
f2e824
			break;
f2e824
f2e824
			case 'settings':
f2e824
				$this->get_convert_settings($sub);
f2e824
			break;
f2e824
f2e824
			case 'in_progress':
f2e824
				$this->convert_data($sub);
f2e824
			break;
f2e824
f2e824
			case 'final':
f2e824
				$this->page_title = $lang['CONVERT_COMPLETE'];
f2e824
f2e824
				$template->assign_vars(array(
f2e824
					'TITLE'		=> $lang['CONVERT_COMPLETE'],
f2e824
					'BODY'		=> $lang['CONVERT_COMPLETE_EXPLAIN'],
f2e824
				));
f2e824
f2e824
				// If we reached this step (conversion completed) we want to purge the cache and log the user out.
f2e824
				// This is for making sure the session get not screwed due to the 3.0.x users table being completely new.
f2e824
				$cache->purge();
f2e824
f2e824
				require($phpbb_root_path . 'config.' . $phpEx);
f2e824
				require($phpbb_root_path . 'includes/constants.' . $phpEx);
f2e824
				require($phpbb_root_path . 'includes/db/' . $dbms . '.' . $phpEx);
f2e824
				require($phpbb_root_path . 'includes/functions_convert.' . $phpEx);
f2e824
f2e824
				$db = new $sql_db();
f2e824
				$db->sql_connect($dbhost, $dbuser, $dbpasswd, $dbname, $dbport, false, true);
f2e824
				unset($dbpasswd);
f2e824
f2e824
				$sql = 'SELECT config_value
f2e824
					FROM ' . CONFIG_TABLE . '
f2e824
					WHERE config_name = \'search_type\'';
f2e824
				$result = $db->sql_query($sql);
f2e824
f2e824
				if ($db->sql_fetchfield('config_value') != 'fulltext_mysql')
f2e824
				{
f2e824
					$template->assign_vars(array(
f2e824
						'S_ERROR_BOX'	=> true,
f2e824
						'ERROR_TITLE'	=> $lang['SEARCH_INDEX_UNCONVERTED'],
f2e824
						'ERROR_MSG'		=> $lang['SEARCH_INDEX_UNCONVERTED_EXPLAIN'],
f2e824
					));
f2e824
				}
f2e824
f2e824
				switch ($db->sql_layer)
f2e824
				{
f2e824
					case 'sqlite':
f2e824
					case 'firebird':
f2e824
						$db->sql_query('DELETE FROM ' . SESSIONS_KEYS_TABLE);
f2e824
						$db->sql_query('DELETE FROM ' . SESSIONS_TABLE);
f2e824
					break;
f2e824
f2e824
					default:
f2e824
						$db->sql_query('TRUNCATE TABLE ' . SESSIONS_KEYS_TABLE);
f2e824
						$db->sql_query('TRUNCATE TABLE ' . SESSIONS_TABLE);
f2e824
					break;
f2e824
				}
f2e824
f2e824
			break;
f2e824
		}
f2e824
	}
f2e824
f2e824
	/**
f2e824
	* Generate a list of all available conversion modules
f2e824
	*/
f2e824
	function list_convertors($sub)
f2e824
	{
f2e824
		global $lang, $language, $template, $phpbb_root_path, $phpEx;
f2e824
f2e824
		$this->page_title = $lang['SUB_INTRO'];
f2e824
f2e824
		$template->assign_vars(array(
f2e824
			'TITLE'		=> $lang['CONVERT_INTRO'],
f2e824
			'BODY'		=> $lang['CONVERT_INTRO_BODY'],
f2e824
f2e824
			'L_AUTHOR'					=> $lang['AUTHOR'],
f2e824
			'L_AVAILABLE_CONVERTORS'	=> $lang['AVAILABLE_CONVERTORS'],
f2e824
			'L_CONVERT'					=> $lang['CONVERT'],
f2e824
			'L_NO_CONVERTORS'			=> $lang['NO_CONVERTORS'],
f2e824
			'L_OPTIONS'					=> $lang['CONVERT_OPTIONS'],
f2e824
			'L_SOFTWARE'				=> $lang['SOFTWARE'],
f2e824
			'L_VERSION'					=> $lang['VERSION'],
f2e824
f2e824
			'S_LIST'	=> true,
f2e824
		));
f2e824
f2e824
		$convertors = $sort = array();
f2e824
		$get_info = true;
f2e824
f2e824
		$handle = @opendir('./convertors/');
f2e824
f2e824
		if (!$handle)
f2e824
		{
f2e824
			$this->error('Unable to access the convertors directory', __LINE__, __FILE__);
f2e824
		}
f2e824
f2e824
		while ($entry = readdir($handle))
f2e824
		{
f2e824
			if (preg_match('/^convert_([a-z0-9_]+).' . $phpEx . '$/i', $entry, $m))
f2e824
			{
f2e824
				include('./convertors/' . $entry);
f2e824
				if (isset($convertor_data))
f2e824
				{
f2e824
					$sort[strtolower($convertor_data['forum_name'])] = sizeof($convertors);
f2e824
f2e824
					$convertors[] = array(
f2e824
						'tag'			=>	$m[1],
f2e824
						'forum_name'	=>	$convertor_data['forum_name'],
f2e824
						'version'		=>	$convertor_data['version'],
f2e824
						'dbms'			=>	$convertor_data['dbms'],
f2e824
						'dbhost'		=>	$convertor_data['dbhost'],
f2e824
						'dbport'		=>	$convertor_data['dbport'],
f2e824
						'dbuser'		=>	$convertor_data['dbuser'],
f2e824
						'dbpasswd'		=>	$convertor_data['dbpasswd'],
f2e824
						'dbname'		=>	$convertor_data['dbname'],
f2e824
						'table_prefix'	=>	$convertor_data['table_prefix'],
f2e824
						'author'		=>	$convertor_data['author']
f2e824
					);
f2e824
				}
f2e824
				unset($convertor_data);
f2e824
			}
f2e824
		}
f2e824
		closedir($handle);
f2e824
f2e824
		@ksort($sort);
f2e824
f2e824
		foreach ($sort as $void => $index)
f2e824
		{
f2e824
			$template->assign_block_vars('convertors', array(
f2e824
				'AUTHOR'	=> $convertors[$index]['author'],
f2e824
				'SOFTWARE'	=> $convertors[$index]['forum_name'],
f2e824
				'VERSION'	=> $convertors[$index]['version'],
f2e824
f2e824
				'U_CONVERT'	=> $this->p_master->module_url . "?mode={$this->mode}&language=$language&sub=settings&tag=" . $convertors[$index]['tag'],
f2e824
			));
f2e824
		}
f2e824
	}
f2e824
f2e824
	/**
f2e824
	*/
f2e824
	function get_convert_settings($sub)
f2e824
	{
f2e824
		global $lang, $language, $template, $db, $phpbb_root_path, $phpEx, $config, $cache;
f2e824
f2e824
		require($phpbb_root_path . 'config.' . $phpEx);
f2e824
		require($phpbb_root_path . 'includes/constants.' . $phpEx);
f2e824
		require($phpbb_root_path . 'includes/db/' . $dbms . '.' . $phpEx);
f2e824
		require($phpbb_root_path . 'includes/functions_convert.' . $phpEx);
f2e824
f2e824
		$db = new $sql_db();
f2e824
		$db->sql_connect($dbhost, $dbuser, $dbpasswd, $dbname, $dbport, false, true);
f2e824
		unset($dbpasswd);
f2e824
f2e824
		$this->page_title = $lang['STAGE_SETTINGS'];
f2e824
f2e824
		// We need to fill the config to let internal functions correctly work
f2e824
		$sql = 'SELECT *
f2e824
			FROM ' . CONFIG_TABLE;
f2e824
		$result = $db->sql_query($sql);
f2e824
f2e824
		$config = array();
f2e824
		while ($row = $db->sql_fetchrow($result))
f2e824
		{
f2e824
			$config[$row['config_name']] = $row['config_value'];
f2e824
		}
f2e824
		$db->sql_freeresult($result);
f2e824
f2e824
		$convertor_tag = request_var('tag', '');
f2e824
f2e824
		if (empty($convertor_tag))
f2e824
		{
f2e824
			$this->p_master->error($lang['NO_CONVERT_SPECIFIED'], __LINE__, __FILE__);
f2e824
		}
f2e824
		$get_info = true;
f2e824
f2e824
		// check security implications of direct inclusion
f2e824
		$convertor_tag = basename($convertor_tag);
f2e824
		if (!file_exists('./convertors/convert_' . $convertor_tag . '.' . $phpEx))
f2e824
		{
f2e824
			$this->p_master->error($lang['CONVERT_NOT_EXIST'], __LINE__, __FILE__);
f2e824
		}
f2e824
f2e824
		include('./convertors/convert_' . $convertor_tag . '.' . $phpEx);
f2e824
f2e824
		// The test_file is a file that should be present in the location of the old board.
f2e824
		if (!isset($test_file))
f2e824
		{
f2e824
			$this->p_master->error($lang['DEV_NO_TEST_FILE'], __LINE__, __FILE__);
f2e824
		}
f2e824
f2e824
		$submit = (isset($_POST['submit'])) ? true : false;
f2e824
f2e824
		$src_dbms			= request_var('src_dbms', $convertor_data['dbms']);
f2e824
		$src_dbhost			= request_var('src_dbhost', $convertor_data['dbhost']);
f2e824
		$src_dbport			= request_var('src_dbport', $convertor_data['dbport']);
f2e824
		$src_dbuser			= request_var('src_dbuser', $convertor_data['dbuser']);
f2e824
		$src_dbpasswd		= request_var('src_dbpasswd', $convertor_data['dbpasswd']);
f2e824
		$src_dbname			= request_var('src_dbname', $convertor_data['dbname']);
f2e824
		$src_table_prefix	= request_var('src_table_prefix', $convertor_data['table_prefix']);
f2e824
		$forum_path			= request_var('forum_path', $convertor_data['forum_path']);
f2e824
		$refresh			= request_var('refresh', 1);
f2e824
f2e824
		// Default URL of the old board
f2e824
		// @todo Are we going to use this for attempting to convert URL references in posts, or should we remove it?
f2e824
		//		-> We should convert old urls to the new relative urls format
f2e824
		// $src_url = request_var('src_url', 'Not in use at the moment');
f2e824
f2e824
		// strip trailing slash from old forum path
f2e824
		$forum_path = (strlen($forum_path) && $forum_path[strlen($forum_path) - 1] == '/') ? substr($forum_path, 0, -1) : $forum_path;
f2e824
f2e824
		$error = array();
f2e824
		if ($submit)
f2e824
		{
f2e824
			if (!@file_exists('./../' . $forum_path . '/' . $test_file))
f2e824
			{
f2e824
				$error[] = sprintf($lang['COULD_NOT_FIND_PATH'], $forum_path);
f2e824
			}
f2e824
f2e824
			$connect_test = false;
f2e824
			$available_dbms = get_available_dbms(false, true, true);
f2e824
f2e824
			if (!isset($available_dbms[$src_dbms]) || !$available_dbms[$src_dbms]['AVAILABLE'])
f2e824
			{
f2e824
				$error['db'][] = $lang['INST_ERR_NO_DB'];
f2e824
				$connect_test = false;
f2e824
			}
f2e824
			else
f2e824
			{
f2e824
				$connect_test = connect_check_db(true, $error, $available_dbms[$src_dbms], $src_table_prefix, $src_dbhost, $src_dbuser, htmlspecialchars_decode($src_dbpasswd), $src_dbname, $src_dbport, true, ($src_dbms == $dbms) ? false : true, false);
f2e824
			}
f2e824
f2e824
			// The forum prefix of the old and the new forum can only be the same if two different databases are used.
f2e824
			if ($src_table_prefix == $table_prefix && $src_dbms == $dbms && $src_dbhost == $dbhost && $src_dbport == $dbport && $src_dbname == $dbname)
f2e824
			{
f2e824
				$error[] = sprintf($lang['TABLE_PREFIX_SAME'], $src_table_prefix);
f2e824
			}
f2e824
f2e824
			// Check table prefix
f2e824
			if (!sizeof($error))
f2e824
			{
f2e824
				// initiate database connection to old db if old and new db differ
f2e824
				global $src_db, $same_db;
f2e824
				$src_db = $same_db = false;
f2e824
f2e824
				if ($src_dbms != $dbms || $src_dbhost != $dbhost || $src_dbport != $dbport || $src_dbname != $dbname || $src_dbuser != $dbuser)
f2e824
				{
f2e824
					$sql_db = 'dbal_' . $src_dbms;
f2e824
					$src_db = new $sql_db();
f2e824
					$src_db->sql_connect($src_dbhost, $src_dbuser, htmlspecialchars_decode($src_dbpasswd), $src_dbname, $src_dbport, false, true);
f2e824
					$same_db = false;
f2e824
				}
f2e824
				else
f2e824
				{
f2e824
					$src_db = $db;
f2e824
					$same_db = true;
f2e824
				}
f2e824
f2e824
				$src_db->sql_return_on_error(true);
f2e824
				$db->sql_return_on_error(true);
f2e824
f2e824
				// Try to select one row from the first table to see if the prefix is OK
f2e824
				$result = $src_db->sql_query_limit('SELECT * FROM ' . $src_table_prefix . $tables[0], 1);
f2e824
f2e824
				if (!$result)
f2e824
				{
f2e824
					$prefixes = array();
f2e824
f2e824
					$tables_existing = get_tables($src_db);
f2e824
					$tables_existing = array_map('strtolower', $tables_existing);
f2e824
					foreach ($tables_existing as $table_name)
f2e824
					{
f2e824
						compare_table($tables, $table_name, $prefixes);
f2e824
					}
f2e824
					unset($tables_existing);
f2e824
f2e824
					foreach ($prefixes as $prefix => $count)
f2e824
					{
f2e824
						if ($count >= sizeof($tables))
f2e824
						{
f2e824
							$possible_prefix = $prefix;
f2e824
							break;
f2e824
						}
f2e824
					}
f2e824
f2e824
					$msg = '';
f2e824
					if (!empty($convertor_data['table_prefix']))
f2e824
					{
f2e824
						$msg .= sprintf($lang['DEFAULT_PREFIX_IS'], $convertor_data['forum_name'], $convertor_data['table_prefix']);
f2e824
					}
f2e824
f2e824
					if (!empty($possible_prefix))
f2e824
					{
f2e824
						$msg .= '
';
f2e824
						$msg .= ($possible_prefix == '*') ? $lang['BLANK_PREFIX_FOUND'] : sprintf($lang['PREFIX_FOUND'], $possible_prefix);
f2e824
						$src_table_prefix = ($possible_prefix == '*') ? '' : $possible_prefix;
f2e824
					}
f2e824
f2e824
					$error[] = $msg;
f2e824
				}
f2e824
				$src_db->sql_freeresult($result);
f2e824
				$src_db->sql_return_on_error(false);
f2e824
			}
f2e824
f2e824
			if (!sizeof($error))
f2e824
			{
f2e824
				// Save convertor Status
f2e824
				set_config('convert_progress', serialize(array(
f2e824
					'step'			=> '',
f2e824
					'table_prefix'	=> $src_table_prefix,
f2e824
					'tag'			=> $convertor_tag,
f2e824
				)), true);
f2e824
				set_config('convert_db_server', serialize(array(
f2e824
					'dbms'			=> $src_dbms,
f2e824
					'dbhost'		=> $src_dbhost,
f2e824
					'dbport'		=> $src_dbport,
f2e824
					'dbname'		=> $src_dbname,
f2e824
				)), true);
f2e824
				set_config('convert_db_user', serialize(array(
f2e824
					'dbuser'		=> $src_dbuser,
f2e824
					'dbpasswd'		=> $src_dbpasswd,
f2e824
				)), true);
f2e824
f2e824
				// Save options
f2e824
				set_config('convert_options', serialize(array('forum_path' => './../' . $forum_path, 'refresh' => $refresh)), true);
f2e824
f2e824
				$template->assign_block_vars('checks', array(
f2e824
					'TITLE'		=> $lang['VERIFY_OPTIONS'],
f2e824
					'RESULT'	=> $lang['CONVERT_SETTINGS_VERIFIED'],
f2e824
				));
f2e824
f2e824
				$template->assign_vars(array(
f2e824
					'L_SUBMIT'	=> $lang['BEGIN_CONVERT'],
f2e824
//					'S_HIDDEN'	=> $s_hidden_fields,
f2e824
					'U_ACTION'	=> $this->p_master->module_url . "?mode={$this->mode}&sub=in_progress&tag=$convertor_tag&language=$language",
f2e824
				));
f2e824
f2e824
				return;
f2e824
			}
f2e824
			else
f2e824
			{
f2e824
				$template->assign_block_vars('checks', array(
f2e824
					'TITLE'		=> $lang['VERIFY_OPTIONS'],
f2e824
					'RESULT'	=> '' . implode('
', $error) . '
',
f2e824
				));
f2e824
			}
f2e824
		} // end submit
f2e824
f2e824
		foreach ($this->convert_options as $config_key => $vars)
f2e824
		{
f2e824
			if (!is_array($vars) && strpos($config_key, 'legend') === false)
f2e824
			{
f2e824
				continue;
f2e824
			}
f2e824
f2e824
			if (strpos($config_key, 'legend') !== false)
f2e824
			{
f2e824
				$template->assign_block_vars('options', array(
f2e824
					'S_LEGEND'		=> true,
f2e824
					'LEGEND'		=> $lang[$vars])
f2e824
				);
f2e824
f2e824
				continue;
f2e824
			}
f2e824
f2e824
			$options = isset($vars['options']) ? $vars['options'] : '';
f2e824
f2e824
			$template->assign_block_vars('options', array(
f2e824
				'KEY'			=> $config_key,
f2e824
				'TITLE'			=> $lang[$vars['lang']],
f2e824
				'S_EXPLAIN'		=> $vars['explain'],
f2e824
				'S_LEGEND'		=> false,
f2e824
				'TITLE_EXPLAIN'	=> ($vars['explain']) ? $lang[$vars['lang'] . '_EXPLAIN'] : '',
f2e824
				'CONTENT'		=> $this->p_master->input_field($config_key, $vars['type'], $$config_key, $options),
f2e824
				)
f2e824
			);
f2e824
		}
f2e824
f2e824
		$template->assign_vars(array(
f2e824
			'TITLE'		=> $lang['STAGE_SETTINGS'],
f2e824
			'BODY'		=> $lang['CONV_OPTIONS_BODY'],
f2e824
			'L_SUBMIT'	=> $lang['BEGIN_CONVERT'],
f2e824
			'U_ACTION'	=> $this->p_master->module_url . "?mode={$this->mode}&sub=settings&tag=$convertor_tag&language=$language",
f2e824
		));
f2e824
	}
f2e824
f2e824
	/**
f2e824
	* The function which does the actual work (or dispatches it to the relevant places)
f2e824
	*/
f2e824
	function convert_data($sub)
f2e824
	{
f2e824
		global $template, $user, $phpbb_root_path, $phpEx, $db, $lang, $config, $cache;
f2e824
		global $convert, $convert_row, $message_parser, $skip_rows, $language;
f2e824
f2e824
		require($phpbb_root_path . 'config.' . $phpEx);
f2e824
		require($phpbb_root_path . 'includes/constants.' . $phpEx);
f2e824
		require($phpbb_root_path . 'includes/db/' . $dbms . '.' . $phpEx);
f2e824
		require($phpbb_root_path . 'includes/functions_convert.' . $phpEx);
f2e824
f2e824
		$db = new $sql_db();
f2e824
		$db->sql_connect($dbhost, $dbuser, $dbpasswd, $dbname, $dbport, false, true);
f2e824
		unset($dbpasswd);
f2e824
f2e824
		$sql = 'SELECT *
f2e824
			FROM ' . CONFIG_TABLE;
f2e824
		$result = $db->sql_query($sql);
f2e824
f2e824
		$config = array();
f2e824
		while ($row = $db->sql_fetchrow($result))
f2e824
		{
f2e824
			$config[$row['config_name']] = $row['config_value'];
f2e824
		}
f2e824
		$db->sql_freeresult($result);
f2e824
f2e824
		// Override a couple of config variables for the duration
f2e824
		$config['max_quote_depth'] = 0;
f2e824
f2e824
		// @todo Need to confirm that max post length in source is <= max post length in destination or there may be interesting formatting issues
f2e824
		$config['max_post_chars'] = 0;
f2e824
f2e824
		// Set up a user as well. We _should_ have enough of a database here at this point to do this
f2e824
		// and it helps for any core code we call
f2e824
		$user->session_begin();
f2e824
		$user->page = $user->extract_current_page($phpbb_root_path);
f2e824
f2e824
		// This is a little bit of a fudge, but it allows the language entries to be available to the
f2e824
		// core code without us loading them again
f2e824
		$user->lang = &$lang;
f2e824
f2e824
		$this->page_title = $user->lang['STAGE_IN_PROGRESS'];
f2e824
f2e824
		$convert->options = array();
f2e824
		if (isset($config['convert_progress']))
f2e824
		{
f2e824
			$convert->options = unserialize($config['convert_progress']);
f2e824
			$convert->options = array_merge($convert->options, unserialize($config['convert_db_server']), unserialize($config['convert_db_user']), unserialize($config['convert_options']));
f2e824
		}
f2e824
f2e824
		// This information should have already been checked once, but do it again for safety
f2e824
		if (empty($convert->options) || empty($convert->options['tag']) ||
f2e824
			!isset($convert->options['dbms']) ||
f2e824
			!isset($convert->options['dbhost']) ||
f2e824
			!isset($convert->options['dbport']) ||
f2e824
			!isset($convert->options['dbuser']) ||
f2e824
			!isset($convert->options['dbpasswd']) ||
f2e824
			!isset($convert->options['dbname']) ||
f2e824
			!isset($convert->options['table_prefix']))
f2e824
		{
f2e824
			$this->p_master->error($user->lang['NO_CONVERT_SPECIFIED'], __LINE__, __FILE__);
f2e824
		}
f2e824
f2e824
		// Make some short variables accessible, for easier referencing
f2e824
		$convert->convertor_tag = basename($convert->options['tag']);
f2e824
		$convert->src_dbms = $convert->options['dbms'];
f2e824
		$convert->src_dbhost = $convert->options['dbhost'];
f2e824
		$convert->src_dbport = $convert->options['dbport'];
f2e824
		$convert->src_dbuser = $convert->options['dbuser'];
f2e824
		$convert->src_dbpasswd = $convert->options['dbpasswd'];
f2e824
		$convert->src_dbname = $convert->options['dbname'];
f2e824
		$convert->src_table_prefix = $convert->options['table_prefix'];
f2e824
f2e824
		// initiate database connection to old db if old and new db differ
f2e824
		global $src_db, $same_db;
f2e824
		$src_db = $same_db = null;
f2e824
		if ($convert->src_dbms != $dbms || $convert->src_dbhost != $dbhost || $convert->src_dbport != $dbport || $convert->src_dbname != $dbname || $convert->src_dbuser != $dbuser)
f2e824
		{
f2e824
			if ($convert->src_dbms != $dbms)
f2e824
			{
f2e824
				require($phpbb_root_path . 'includes/db/' . $convert->src_dbms . '.' . $phpEx);
f2e824
			}
f2e824
			$sql_db = 'dbal_' . $convert->src_dbms;
f2e824
			$src_db = new $sql_db();
f2e824
			$src_db->sql_connect($convert->src_dbhost, $convert->src_dbuser, htmlspecialchars_decode($convert->src_dbpasswd), $convert->src_dbname, $convert->src_dbport, false, true);
f2e824
			$same_db = false;
f2e824
		}
f2e824
		else
f2e824
		{
f2e824
			$src_db = $db;
f2e824
			$same_db = true;
f2e824
		}
f2e824
f2e824
		$convert->mysql_convert = false;
f2e824
		switch ($src_db->sql_layer)
f2e824
		{
f2e824
			case 'sqlite':
f2e824
			case 'firebird':
f2e824
				$convert->src_truncate_statement = 'DELETE FROM ';
f2e824
			break;
f2e824
f2e824
			// Thanks MySQL, for silently converting...
f2e824
			case 'mysql':
f2e824
			case 'mysql4':
f2e824
				if (version_compare($src_db->sql_server_info(true), '4.1.3', '>='))
f2e824
				{
f2e824
					$convert->mysql_convert = true;
f2e824
				}
f2e824
				$convert->src_truncate_statement = 'TRUNCATE TABLE ';
f2e824
			break;
f2e824
f2e824
			case 'mysqli':
f2e824
				$convert->mysql_convert = true;
f2e824
				$convert->src_truncate_statement = 'TRUNCATE TABLE ';
f2e824
			break;
f2e824
f2e824
			default:
f2e824
				$convert->src_truncate_statement = 'TRUNCATE TABLE ';
f2e824
			break;
f2e824
		}
f2e824
f2e824
		if ($convert->mysql_convert && !$same_db)
f2e824
		{
f2e824
			$src_db->sql_query("SET NAMES 'binary'");
f2e824
		}
f2e824
f2e824
		switch ($db->sql_layer)
f2e824
		{
f2e824
			case 'sqlite':
f2e824
			case 'firebird':
f2e824
				$convert->truncate_statement = 'DELETE FROM ';
f2e824
			break;
f2e824
f2e824
			default:
f2e824
				$convert->truncate_statement = 'TRUNCATE TABLE ';
f2e824
			break;
f2e824
		}
f2e824
f2e824
		$get_info = false;
f2e824
f2e824
		// check security implications of direct inclusion
f2e824
		if (!file_exists('./convertors/convert_' . $convert->convertor_tag . '.' . $phpEx))
f2e824
		{
f2e824
			$this->p_master->error($user->lang['CONVERT_NOT_EXIST'], __LINE__, __FILE__);
f2e824
		}
f2e824
f2e824
		if (file_exists('./convertors/functions_' . $convert->convertor_tag . '.' . $phpEx))
f2e824
		{
f2e824
			include('./convertors/functions_' . $convert->convertor_tag . '.' . $phpEx);
f2e824
		}
f2e824
f2e824
		$get_info = true;
f2e824
		include('./convertors/convert_' . $convert->convertor_tag . '.' . $phpEx);
f2e824
f2e824
		// Map some variables...
f2e824
		$convert->convertor_data = $convertor_data;
f2e824
		$convert->tables = $tables;
f2e824
		$convert->config_schema = $config_schema;
f2e824
f2e824
		// Now include the real data
f2e824
		$get_info = false;
f2e824
		include('./convertors/convert_' . $convert->convertor_tag . '.' . $phpEx);
f2e824
f2e824
		$convert->convertor_data = $convertor_data;
f2e824
		$convert->tables = $tables;
f2e824
		$convert->config_schema = $config_schema;
f2e824
		$convert->convertor = $convertor;
f2e824
f2e824
		// The test_file is a file that should be present in the location of the old board.
f2e824
		if (!file_exists($convert->options['forum_path'] . '/' . $test_file))
f2e824
		{
f2e824
			$this->p_master->error(sprintf($user->lang['COULD_NOT_FIND_PATH'], $convert->options['forum_path']), __LINE__, __FILE__);
f2e824
		}
f2e824
f2e824
		$search_type = basename(trim($config['search_type']));
f2e824
f2e824
		// For conversions we are a bit less strict and set to a search backend we know exist...
f2e824
		if (!file_exists($phpbb_root_path . 'includes/search/' . $search_type . '.' . $phpEx))
f2e824
		{
f2e824
			$search_type = 'fulltext_native';
f2e824
			set_config('search_type', $search_type);
f2e824
		}
f2e824
f2e824
		if (!file_exists($phpbb_root_path . 'includes/search/' . $search_type . '.' . $phpEx))
f2e824
		{
f2e824
			trigger_error('NO_SUCH_SEARCH_MODULE');
f2e824
		}
f2e824
f2e824
		require($phpbb_root_path . 'includes/search/' . $search_type . '.' . $phpEx);
f2e824
f2e824
		$error = false;
f2e824
		$convert->fulltext_search = new $search_type($error);
f2e824
f2e824
		if ($error)
f2e824
		{
f2e824
			trigger_error($error);
f2e824
		}
f2e824
f2e824
		include($phpbb_root_path . 'includes/message_parser.' . $phpEx);
f2e824
		$message_parser = new parse_message();
f2e824
f2e824
		$jump = request_var('jump', 0);
f2e824
		$final_jump = request_var('final_jump', 0);
f2e824
		$sync_batch = request_var('sync_batch', -1);
f2e824
		$last_statement = request_var('last', 0);
f2e824
f2e824
		// We are running sync...
f2e824
		if ($sync_batch >= 0)
f2e824
		{
f2e824
			$this->sync_forums($sync_batch);
f2e824
			return;
f2e824
		}
f2e824
f2e824
		if ($jump)
f2e824
		{
f2e824
			$this->jump($jump, $last_statement);
f2e824
			return;
f2e824
		}
f2e824
f2e824
		if ($final_jump)
f2e824
		{
f2e824
			$this->final_jump($final_jump);
f2e824
			return;
f2e824
		}
f2e824
f2e824
		$current_table = request_var('current_table', 0);
f2e824
		$old_current_table = min(-1, $current_table - 1);
f2e824
		$skip_rows = request_var('skip_rows', 0);
f2e824
f2e824
		if (!$current_table && !$skip_rows)
f2e824
		{
f2e824
			if (empty($_REQUEST['confirm']))
f2e824
			{
f2e824
				// If avatars / ranks / smilies folders are specified make sure they are writable
f2e824
				$bad_folders = array();
f2e824
f2e824
				$local_paths = array(
f2e824
					'avatar_path'			=> path($config['avatar_path']),
f2e824
					'avatar_gallery_path'	=> path($config['avatar_gallery_path']),
f2e824
					'icons_path'			=> path($config['icons_path']),
f2e824
					'ranks_path'			=> path($config['ranks_path']),
f2e824
					'smilies_path'			=> path($config['smilies_path'])
f2e824
				);
f2e824
f2e824
				foreach ($local_paths as $folder => $local_path)
f2e824
				{
f2e824
					if (isset($convert->convertor[$folder]))
f2e824
					{
f2e824
						if (empty($convert->convertor['test_file']))
f2e824
						{
f2e824
							// test_file is mandantory at the moment so this should never be reached, but just in case...
f2e824
							$this->p_master->error($user->lang['DEV_NO_TEST_FILE'], __LINE__, __FILE__);
f2e824
						}
f2e824
f2e824
						if (!$local_path || !@is_writable($phpbb_root_path . $local_path))
f2e824
						{
f2e824
							if (!$local_path)
f2e824
							{
f2e824
								$bad_folders[] = sprintf($user->lang['CONFIG_PHPBB_EMPTY'], $folder);
f2e824
							}
f2e824
							else
f2e824
							{
f2e824
								$bad_folders[] = $local_path;
f2e824
							}
f2e824
						}
f2e824
					}
f2e824
				}
f2e824
f2e824
				if (sizeof($bad_folders))
f2e824
				{
f2e824
					$msg = (sizeof($bad_folders) == 1) ? $user->lang['MAKE_FOLDER_WRITABLE'] : $user->lang['MAKE_FOLDERS_WRITABLE'];
f2e824
					sort($bad_folders);
f2e824
					$this->p_master->error(sprintf($msg, implode('
', $bad_folders)), __LINE__, __FILE__, true);
f2e824
f2e824
					$template->assign_vars(array(
f2e824
						'L_SUBMIT'	=> $user->lang['INSTALL_TEST'],
f2e824
						'U_ACTION'	=> $this->p_master->module_url . "?mode={$this->mode}&sub=in_progress&tag={$convert->convertor_tag}&language=$language",
f2e824
					));
f2e824
					return;
f2e824
				}
f2e824
f2e824
				// Grab all the tables used in convertor
f2e824
				$missing_tables = $tables_list = $aliases = array();
f2e824
f2e824
				foreach ($convert->convertor['schema'] as $schema)
f2e824
				{
f2e824
					// Skip those not used (because of addons/plugins not detected)
f2e824
					if (!$schema['target'])
f2e824
					{
f2e824
						continue;
f2e824
					}
f2e824
f2e824
					foreach ($schema as $key => $val)
f2e824
					{
f2e824
						// we're dealing with an array like:
f2e824
						// array('forum_status',			'forums.forum_status',				'is_item_locked')
f2e824
						if (is_int($key) && !empty($val[1]))
f2e824
						{
f2e824
							$temp_data = $val[1];
f2e824
							if (!is_array($temp_data))
f2e824
							{
f2e824
								$temp_data = array($temp_data);
f2e824
							}
f2e824
f2e824
							foreach ($temp_data as $val)
f2e824
							{
f2e824
								if (preg_match('/([a-z0-9_]+)\.([a-z0-9_]+)\)* ?A?S? ?([a-z0-9_]*?)\.?([a-z0-9_]*)$/i', $val, $m))
f2e824
								{
f2e824
									$table = $convert->src_table_prefix . $m[1];
f2e824
									$tables_list[$table] = $table;
f2e824
f2e824
									if (!empty($m[3]))
f2e824
									{
f2e824
										$aliases[] = $convert->src_table_prefix . $m[3];
f2e824
									}
f2e824
								}
f2e824
							}
f2e824
						}
f2e824
						// 'left_join'		=> 'topics LEFT JOIN vote_desc ON topics.topic_id = vote_desc.topic_id AND topics.topic_vote = 1'
f2e824
						else if ($key == 'left_join')
f2e824
						{
f2e824
							// Convert the value if it wasn't an array already.
f2e824
							if (!is_array($val))
f2e824
							{
f2e824
								$val = array($val);
f2e824
							}
f2e824
f2e824
							for ($j = 0; $j < sizeof($val); ++$j)
f2e824
							{
f2e824
								if (preg_match('/LEFT JOIN ([a-z0-9_]+) AS ([a-z0-9_]+)/i', $val[$j], $m))
f2e824
								{
f2e824
									$table = $convert->src_table_prefix . $m[1];
f2e824
									$tables_list[$table] = $table;
f2e824
f2e824
									if (!empty($m[2]))
f2e824
									{
f2e824
										$aliases[] = $convert->src_table_prefix . $m[2];
f2e824
									}
f2e824
								}
f2e824
							}
f2e824
						}
f2e824
					}
f2e824
				}
f2e824
f2e824
				// Remove aliased tables from $tables_list
f2e824
				foreach ($aliases as $alias)
f2e824
				{
f2e824
					unset($tables_list[$alias]);
f2e824
				}
f2e824
f2e824
				// Check if the tables that we need exist
f2e824
				$src_db->sql_return_on_error(true);
f2e824
				foreach ($tables_list as $table => $null)
f2e824
				{
f2e824
					$sql = 'SELECT 1 FROM ' . $table;
f2e824
					$_result = $src_db->sql_query_limit($sql, 1);
f2e824
f2e824
					if (!$_result)
f2e824
					{
f2e824
						$missing_tables[] = $table;
f2e824
					}
f2e824
					$src_db->sql_freeresult($_result);
f2e824
				}
f2e824
				$src_db->sql_return_on_error(false);
f2e824
f2e824
				// Throw an error if some tables are missing
f2e824
				// We used to do some guessing here, but since we have a suggestion of possible values earlier, I don't see it adding anything here to do it again
f2e824
f2e824
				if (sizeof($missing_tables) == sizeof($tables_list))
f2e824
				{
f2e824
					$this->p_master->error($user->lang['NO_TABLES_FOUND'] . ' ' . $user->lang['CHECK_TABLE_PREFIX'], __LINE__, __FILE__);
f2e824
				}
f2e824
				else if (sizeof($missing_tables))
f2e824
				{
f2e824
					$this->p_master->error(sprintf($user->lang['TABLES_MISSING'], implode(', ', $missing_tables)) . '

' . $user->lang['CHECK_TABLE_PREFIX'], __LINE__, __FILE__);
f2e824
				}
f2e824
f2e824
				$url = $this->save_convert_progress('&confirm=1');
f2e824
				$msg = $user->lang['PRE_CONVERT_COMPLETE'];
f2e824
f2e824
				if ($convert->convertor_data['author_notes'])
f2e824
				{
f2e824
					$msg .= '

' . sprintf($user->lang['AUTHOR_NOTES'], $convert->convertor_data['author_notes']);

f2e824
				}
f2e824
f2e824
				$template->assign_vars(array(
f2e824
					'L_SUBMIT'		=> $user->lang['CONTINUE_CONVERT'],
f2e824
					'L_MESSAGE'		=> $msg,
f2e824
					'U_ACTION'		=> $url,
f2e824
				));
f2e824
f2e824
				return;
f2e824
			} // if (empty($_REQUEST['confirm']))
f2e824
f2e824
			$template->assign_block_vars('checks', array(
f2e824
				'S_LEGEND'		=> true,
f2e824
				'LEGEND'		=> $user->lang['STARTING_CONVERT'],
f2e824
			));
f2e824
f2e824
			// Convert the config table and load the settings of the old board
f2e824
			if (!empty($convert->config_schema))
f2e824
			{
f2e824
				restore_config($convert->config_schema);
f2e824
f2e824
				// Override a couple of config variables for the duration
f2e824
				$config['max_quote_depth'] = 0;
f2e824
f2e824
				// @todo Need to confirm that max post length in source is <= max post length in destination or there may be interesting formatting issues
f2e824
				$config['max_post_chars'] = 0;
f2e824
			}
f2e824
f2e824
			$template->assign_block_vars('checks', array(
f2e824
				'TITLE'		=> $user->lang['CONFIG_CONVERT'],
f2e824
				'RESULT'	=> $user->lang['DONE'],
f2e824
			));
f2e824
f2e824
			// Now process queries and execute functions that have to be executed prior to the conversion
f2e824
			if (!empty($convert->convertor['execute_first']))
f2e824
			{
f2e824
				eval($convert->convertor['execute_first']);
f2e824
			}
f2e824
f2e824
			if (!empty($convert->convertor['query_first']))
f2e824
			{
f2e824
				if (!is_array($convert->convertor['query_first']))
f2e824
				{
f2e824
					$convert->convertor['query_first'] = array('target', array($convert->convertor['query_first']));
f2e824
				}
f2e824
				else if (!is_array($convert->convertor['query_first'][0]))
f2e824
				{
f2e824
					$convert->convertor['query_first'] = array(array($convert->convertor['query_first'][0], $convert->convertor['query_first'][1]));
f2e824
				}
f2e824
f2e824
				foreach ($convert->convertor['query_first'] as $query_first)
f2e824
				{
f2e824
					if ($query_first[0] == 'src')
f2e824
					{
f2e824
						if ($convert->mysql_convert && $same_db)
f2e824
						{
f2e824
							$src_db->sql_query("SET NAMES 'binary'");
f2e824
						}
f2e824
f2e824
						$src_db->sql_query($query_first[1]);
f2e824
f2e824
						if ($convert->mysql_convert && $same_db)
f2e824
						{
f2e824
							$src_db->sql_query("SET NAMES 'utf8'");
f2e824
						}
f2e824
					}
f2e824
					else
f2e824
					{
f2e824
						$db->sql_query($query_first[1]);
f2e824
					}
f2e824
				}
f2e824
			}
f2e824
f2e824
			$template->assign_block_vars('checks', array(
f2e824
				'TITLE'		=> $user->lang['PREPROCESS_STEP'],
f2e824
				'RESULT'	=> $user->lang['DONE'],
f2e824
			));
f2e824
		} // if (!$current_table && !$skip_rows)
f2e824
f2e824
		$template->assign_block_vars('checks', array(
f2e824
			'S_LEGEND'		=> true,
f2e824
			'LEGEND'		=> $user->lang['FILLING_TABLES'],
f2e824
		));
f2e824
f2e824
		// This loop takes one target table and processes it
f2e824
		while ($current_table < sizeof($convert->convertor['schema']))
f2e824
		{
f2e824
			$schema = $convert->convertor['schema'][$current_table];
f2e824
f2e824
			// The target table isn't set, this can be because a module (for example the attachement mod) is taking care of this.
f2e824
			if (empty($schema['target']))
f2e824
			{
f2e824
				$current_table++;
f2e824
				continue;
f2e824
			}
f2e824
f2e824
			$template->assign_block_vars('checks', array(
f2e824
				'TITLE'	=> sprintf($user->lang['FILLING_TABLE'], $schema['target']),
f2e824
			));
f2e824
f2e824
			// This is only the case when we first start working on the tables.
f2e824
			if (!$skip_rows)
f2e824
			{
f2e824
				// process execute_first and query_first for this table...
f2e824
				if (!empty($schema['execute_first']))
f2e824
				{
f2e824
					eval($schema['execute_first']);
f2e824
				}
f2e824
f2e824
				if (!empty($schema['query_first']))
f2e824
				{
f2e824
					if (!is_array($schema['query_first']))
f2e824
					{
f2e824
						$schema['query_first'] = array('target', array($schema['query_first']));
f2e824
					}
f2e824
					else if (!is_array($schema['query_first'][0]))
f2e824
					{
f2e824
						$schema['query_first'] = array(array($schema['query_first'][0], $schema['query_first'][1]));
f2e824
					}
f2e824
f2e824
					foreach ($schema['query_first'] as $query_first)
f2e824
					{
f2e824
						if ($query_first[0] == 'src')
f2e824
						{
f2e824
							if ($convert->mysql_convert && $same_db)
f2e824
							{
f2e824
								$src_db->sql_query("SET NAMES 'binary'");
f2e824
							}
f2e824
							$src_db->sql_query($query_first[1]);
f2e824
							if ($convert->mysql_convert && $same_db)
f2e824
							{
f2e824
								$src_db->sql_query("SET NAMES 'utf8'");
f2e824
							}
f2e824
						}
f2e824
						else
f2e824
						{
f2e824
							$db->sql_query($query_first[1]);
f2e824
						}
f2e824
					}
f2e824
				}
f2e824
f2e824
				if (!empty($schema['autoincrement']))
f2e824
				{
f2e824
					switch ($db->sql_layer)
f2e824
					{
f2e824
						case 'postgres':
f2e824
							$db->sql_query("SELECT SETVAL('" . $schema['target'] . "_seq',(select case when max(" . $schema['autoincrement'] . ")>0 then max(" . $schema['autoincrement'] . ")+1 else 1 end from " . $schema['target'] . '));');
f2e824
						break;
f2e824
f2e824
						case 'oracle':
f2e824
							$result = $db->sql_query('SELECT MAX(' . $schema['autoincrement'] . ') as max_id FROM ' . $schema['target']);
f2e824
							$row = $db->sql_fetchrow($result);
f2e824
							$db->sql_freeresult($result);
f2e824
f2e824
							$largest_id = (int) $row['max_id'];
f2e824
f2e824
							if ($largest_id)
f2e824
							{
f2e824
								$db->sql_query('DROP SEQUENCE ' . $schema['target'] . '_seq');
f2e824
								$db->sql_query('CREATE SEQUENCE ' . $schema['target'] . '_seq START WITH ' . ($largest_id + 1));
f2e824
							}
f2e824
						break;
f2e824
					}
f2e824
				}
f2e824
			}
f2e824
f2e824
			// Process execute_always for this table
f2e824
			// This is for code which needs to be executed on every pass of this table if
f2e824
			// it gets split because of time restrictions
f2e824
			if (!empty($schema['execute_always']))
f2e824
			{
f2e824
				eval($schema['execute_always']);
f2e824
			}
f2e824
f2e824
			//
f2e824
			// Set up some variables
f2e824
			//
f2e824
			// $waiting_rows	holds rows for multirows insertion (MySQL only)
f2e824
			// $src_tables		holds unique tables with aliases to select from
f2e824
			// $src_fields		will quickly refer source fields (or aliases) corresponding to the current index
f2e824
			// $select_fields	holds the names of the fields to retrieve
f2e824
			//
f2e824
f2e824
			$sql_data = array(
f2e824
				'source_fields'		=> array(),
f2e824
				'target_fields'		=> array(),
f2e824
				'source_tables'		=> array(),
f2e824
				'select_fields'		=> array(),
f2e824
			);
f2e824
f2e824
			// This statement is building the keys for later insertion.
f2e824
			$insert_query = $this->build_insert_query($schema, $sql_data, $current_table);
f2e824
f2e824
			// If no source table is affected, we skip the table
f2e824
			if (empty($sql_data['source_tables']))
f2e824
			{
f2e824
				$skip_rows = 0;
f2e824
				$current_table++;
f2e824
				continue;
f2e824
			}
f2e824
f2e824
			$distinct = (!empty($schema['distinct'])) ? 'DISTINCT ' : '';
f2e824
f2e824
			$sql = 'SELECT ' . $distinct . implode(', ', $sql_data['select_fields']) . " \nFROM " . implode(', ', $sql_data['source_tables']);
f2e824
f2e824
			// Where
f2e824
			$sql .= (!empty($schema['where'])) ? "\nWHERE (" . $schema['where'] . ')' : '';
f2e824
f2e824
			// Group By
f2e824
			if (!empty($schema['group_by']))
f2e824
			{
f2e824
				$schema['group_by'] = array($schema['group_by']);
f2e824
				foreach ($sql_data['select_fields'] as $select)
f2e824
				{
f2e824
					$alias = strpos(strtolower($select), ' as ');
f2e824
					$select = ($alias) ? substr($select, 0, $alias) : $select;
f2e824
					if (!in_array($select, $schema['group_by']))
f2e824
					{
f2e824
						$schema['group_by'][] = $select;
f2e824
					}
f2e824
				}
f2e824
			}
f2e824
			$sql .= (!empty($schema['group_by'])) ? "\nGROUP BY " . implode(', ', $schema['group_by']) : '';
f2e824
f2e824
			// Having
f2e824
			$sql .= (!empty($schema['having'])) ? "\nHAVING " . $schema['having'] : '';
f2e824
f2e824
			// Order By
f2e824
			if (empty($schema['order_by']) && !empty($schema['primary']))
f2e824
			{
f2e824
				$schema['order_by'] = $schema['primary'];
f2e824
			}
f2e824
			$sql .= (!empty($schema['order_by'])) ? "\nORDER BY " . $schema['order_by'] : '';
f2e824
f2e824
			// Counting basically holds the amount of rows processed.
f2e824
			$counting = -1;
f2e824
			$batch_time = 0;
f2e824
f2e824
			while ($counting === -1 || ($counting >= $convert->batch_size && still_on_time()))
f2e824
			{
f2e824
				$old_current_table = $current_table;
f2e824
f2e824
				$rows = '';
f2e824
				$waiting_rows = array();
f2e824
f2e824
				if (!empty($batch_time))
f2e824
				{
f2e824
					$mtime = explode(' ', microtime());
f2e824
					$mtime = $mtime[0] + $mtime[1];
f2e824
					$rows = ceil($counting/($mtime - $batch_time)) . " rows/s ($counting rows) | ";
f2e824
				}
f2e824
f2e824
				$template->assign_block_vars('checks', array(
f2e824
					'TITLE'		=> "skip_rows = $skip_rows",
f2e824
					'RESULT'	=> $rows . ((defined('DEBUG_EXTRA') && function_exists('memory_get_usage')) ? ceil(memory_get_usage()/1024) . ' ' . $user->lang['KIB'] : ''),
f2e824
				));
f2e824
f2e824
				$mtime = explode(' ', microtime());
f2e824
				$batch_time = $mtime[0] + $mtime[1];
f2e824
f2e824
				if ($convert->mysql_convert && $same_db)
f2e824
				{
f2e824
					$src_db->sql_query("SET NAMES 'binary'");
f2e824
				}
f2e824
f2e824
				// Take skip rows into account and only fetch batch_size amount of rows
f2e824
				$___result = $src_db->sql_query_limit($sql, $convert->batch_size, $skip_rows);
f2e824
f2e824
				if ($convert->mysql_convert && $same_db)
f2e824
				{
f2e824
					$src_db->sql_query("SET NAMES 'utf8'");
f2e824
				}
f2e824
f2e824
				// This loop processes each row
f2e824
				$counting = 0;
f2e824
f2e824
				$convert->row = $convert_row = array();
f2e824
f2e824
				if (!empty($schema['autoincrement']))
f2e824
				{
f2e824
					switch ($db->sql_layer)
f2e824
					{
f2e824
						case 'mssql':
f2e824
						case 'mssql_odbc':
f2e824
							$db->sql_query('SET IDENTITY_INSERT ' . $schema['target'] . ' ON');
f2e824
						break;
f2e824
					}
f2e824
				}
f2e824
f2e824
				// Now handle the rows until time is over or no more rows to process...
f2e824
				while ($counting === 0 || still_on_time())
f2e824
				{
f2e824
					$convert_row = $src_db->sql_fetchrow($___result);
f2e824
f2e824
					if (!$convert_row)
f2e824
					{
f2e824
						// move to the next batch or table
f2e824
						break;
f2e824
					}
f2e824
f2e824
					// With this we are able to always save the last state
f2e824
					$convert->row = $convert_row;
f2e824
f2e824
					// Increment the counting variable, it stores the number of rows we have processed
f2e824
					$counting++;
f2e824
f2e824
					$insert_values = array();
f2e824
f2e824
					$sql_flag = $this->process_row($schema, $sql_data, $insert_values);
f2e824
f2e824
					if ($sql_flag === true)
f2e824
					{
f2e824
						switch ($db->sql_layer)
f2e824
						{
f2e824
							// If MySQL, we'll wait to have num_wait_rows rows to submit at once
f2e824
							case 'mysql':
f2e824
							case 'mysql4':
f2e824
							case 'mysqli':
f2e824
								$waiting_rows[] = '(' . implode(', ', $insert_values) . ')';
f2e824
f2e824
								if (sizeof($waiting_rows) >= $convert->num_wait_rows)
f2e824
								{
f2e824
									$errored = false;
f2e824
f2e824
									$db->sql_return_on_error(true);
f2e824
f2e824
									if (!$db->sql_query($insert_query . implode(', ', $waiting_rows)))
f2e824
									{
f2e824
										$errored = true;
f2e824
									}
f2e824
									$db->sql_return_on_error(false);
f2e824
f2e824
									if ($errored)
f2e824
									{
f2e824
										$db->sql_return_on_error(true);
f2e824
f2e824
										// Because it errored out we will try to insert the rows one by one... most of the time this
f2e824
										// is caused by duplicate entries - but we also do not want to miss one...
f2e824
										foreach ($waiting_rows as $waiting_sql)
f2e824
										{
f2e824
											if (!$db->sql_query($insert_query . $waiting_sql))
f2e824
											{
f2e824
												$this->p_master->db_error($user->lang['DB_ERR_INSERT'], htmlspecialchars($insert_query . $waiting_sql) . '

' . htmlspecialchars(print_r($db->_sql_error(), true)), __LINE__, __FILE__, true);
f2e824
											}
f2e824
										}
f2e824
f2e824
										$db->sql_return_on_error(false);
f2e824
									}
f2e824
f2e824
									$waiting_rows = array();
f2e824
								}
f2e824
f2e824
							break;
f2e824
f2e824
							default:
f2e824
								$insert_sql = $insert_query . '(' . implode(', ', $insert_values) . ')';
f2e824
f2e824
								$db->sql_return_on_error(true);
f2e824
f2e824
								if (!$db->sql_query($insert_sql))
f2e824
								{
f2e824
									$this->p_master->db_error($user->lang['DB_ERR_INSERT'], htmlspecialchars($insert_sql) . '

' . htmlspecialchars(print_r($db->_sql_error(), true)), __LINE__, __FILE__, true);
f2e824
								}
f2e824
								$db->sql_return_on_error(false);
f2e824
f2e824
								$waiting_rows = array();
f2e824
f2e824
							break;
f2e824
						}
f2e824
					}
f2e824
f2e824
					$skip_rows++;
f2e824
				}
f2e824
				$src_db->sql_freeresult($___result);
f2e824
f2e824
				// We might still have some rows waiting
f2e824
				if (sizeof($waiting_rows))
f2e824
				{
f2e824
					$errored = false;
f2e824
					$db->sql_return_on_error(true);
f2e824
f2e824
					if (!$db->sql_query($insert_query . implode(', ', $waiting_rows)))
f2e824
					{
f2e824
						$errored = true;
f2e824
					}
f2e824
					$db->sql_return_on_error(false);
f2e824
f2e824
					if ($errored)
f2e824
					{
f2e824
						$db->sql_return_on_error(true);
f2e824
f2e824
						// Because it errored out we will try to insert the rows one by one... most of the time this
f2e824
						// is caused by duplicate entries - but we also do not want to miss one...
f2e824
						foreach ($waiting_rows as $waiting_sql)
f2e824
						{
f2e824
							$db->sql_query($insert_query . $waiting_sql);
f2e824
							$this->p_master->db_error($user->lang['DB_ERR_INSERT'], htmlspecialchars($insert_query . $waiting_sql) . '

' . htmlspecialchars(print_r($db->_sql_error(), true)), __LINE__, __FILE__, true);
f2e824
						}
f2e824
f2e824
						$db->sql_return_on_error(false);
f2e824
					}
f2e824
f2e824
					$waiting_rows = array();
f2e824
				}
f2e824
f2e824
				if (!empty($schema['autoincrement']))
f2e824
				{
f2e824
					switch ($db->sql_layer)
f2e824
					{
f2e824
						case 'mssql':
f2e824
						case 'mssql_odbc':
f2e824
							$db->sql_query('SET IDENTITY_INSERT ' . $schema['target'] . ' OFF');
f2e824
						break;
f2e824
f2e824
						case 'postgres':
f2e824
							$db->sql_query("SELECT SETVAL('" . $schema['target'] . "_seq',(select case when max(" . $schema['autoincrement'] . ")>0 then max(" . $schema['autoincrement'] . ")+1 else 1 end from " . $schema['target'] . '));');
f2e824
						break;
f2e824
f2e824
						case 'oracle':
f2e824
							$result = $db->sql_query('SELECT MAX(' . $schema['autoincrement'] . ') as max_id FROM ' . $schema['target']);
f2e824
							$row = $db->sql_fetchrow($result);
f2e824
							$db->sql_freeresult($result);
f2e824
f2e824
							$largest_id = (int) $row['max_id'];
f2e824
f2e824
							if ($largest_id)
f2e824
							{
f2e824
								$db->sql_query('DROP SEQUENCE ' . $schema['target'] . '_seq');
f2e824
								$db->sql_query('CREATE SEQUENCE ' . $schema['target'] . '_seq START WITH ' . ($largest_id + 1));
f2e824
							}
f2e824
						break;
f2e824
					}
f2e824
				}
f2e824
			}
f2e824
f2e824
			// When we reach this point, either the current table has been processed or we're running out of time.
f2e824
			if (still_on_time() && $counting < $convert->batch_size/* && !defined('DEBUG_EXTRA')*/)
f2e824
			{
f2e824
				$skip_rows = 0;
f2e824
				$current_table++;
f2e824
			}
f2e824
			else
f2e824
			{/*
f2e824
				if (still_on_time() && $counting < $convert->batch_size)
f2e824
				{
f2e824
					$skip_rows = 0;
f2e824
					$current_table++;
f2e824
				}*/
f2e824
f2e824
				// Looks like we ran out of time.
f2e824
				$url = $this->save_convert_progress('&current_table=' . $current_table . '&skip_rows=' . $skip_rows);
f2e824
f2e824
				$current_table++;
f2e824
//				$percentage = ($skip_rows == 0) ? 0 : floor(100 / ($total_rows / $skip_rows));
f2e824
f2e824
				$msg = sprintf($user->lang['STEP_PERCENT_COMPLETED'], $current_table, sizeof($convert->convertor['schema']));
f2e824
f2e824
				$template->assign_vars(array(
f2e824
					'L_MESSAGE'		=> $msg,
f2e824
					'L_SUBMIT'		=> $user->lang['CONTINUE_CONVERT'],
f2e824
					'U_ACTION'		=> $url,
f2e824
				));
f2e824
f2e824
				$this->meta_refresh($url);
f2e824
				return;
f2e824
			}
f2e824
		}
f2e824
f2e824
		// Process execute_last then we'll be done
f2e824
		$url = $this->save_convert_progress('&jump=1');
f2e824
f2e824
		$template->assign_vars(array(
f2e824
			'L_SUBMIT'		=> $user->lang['FINAL_STEP'],
f2e824
			'U_ACTION'		=> $url,
f2e824
		));
f2e824
f2e824
		$this->meta_refresh($url);
f2e824
		return;
f2e824
	}
f2e824
f2e824
	/**
f2e824
	* Sync function being executed at the middle, some functions need to be executed after a successful sync.
f2e824
	*/
f2e824
	function sync_forums($sync_batch)
f2e824
	{
f2e824
		global $template, $user, $db, $phpbb_root_path, $phpEx, $config, $cache;
f2e824
		global $convert;
f2e824
f2e824
		$template->assign_block_vars('checks', array(
f2e824
			'S_LEGEND'	=> true,
f2e824
			'LEGEND'	=> $user->lang['SYNC_TOPICS'],
f2e824
		));
f2e824
f2e824
		$batch_size = $convert->batch_size;
f2e824
f2e824
		$sql = 'SELECT MIN(topic_id) as min_value, MAX(topic_id) AS max_value
f2e824
			FROM ' . TOPICS_TABLE;
f2e824
		$result = $db->sql_query($sql);
f2e824
		$row = $db->sql_fetchrow($result);
f2e824
		$db->sql_freeresult($result);
f2e824
f2e824
		// Set values of minimum/maximum primary value for this table.
f2e824
		$primary_min = $row['min_value'];
f2e824
		$primary_max = $row['max_value'];
f2e824
f2e824
		if ($sync_batch == 0)
f2e824
		{
f2e824
			$sync_batch = (int) $primary_min;
f2e824
		}
f2e824
f2e824
		if ($sync_batch == 0)
f2e824
		{
f2e824
			$sync_batch = 1;
f2e824
		}
f2e824
f2e824
		// Fetch a batch of rows, process and insert them.
f2e824
		while ($sync_batch <= $primary_max && still_on_time())
f2e824
		{
f2e824
			$end = ($sync_batch + $batch_size - 1);
f2e824
f2e824
			// Sync all topics in batch mode...
f2e824
			sync('topic_approved', 'range', 'topic_id BETWEEN ' . $sync_batch . ' AND ' . $end, true, false);
f2e824
			sync('topic', 'range', 'topic_id BETWEEN ' . $sync_batch . ' AND ' . $end, true, true);
f2e824
f2e824
			$template->assign_block_vars('checks', array(
f2e824
				'TITLE'		=> sprintf($user->lang['SYNC_TOPIC_ID'], $sync_batch, ($sync_batch + $batch_size)) . ((defined('DEBUG_EXTRA') && function_exists('memory_get_usage')) ? ' [' . ceil(memory_get_usage()/1024) . ' ' . $user->lang['KIB'] . ']' : ''),
f2e824
				'RESULT'	=> $user->lang['DONE'],
f2e824
			));
f2e824
f2e824
			$sync_batch += $batch_size;
f2e824
		}
f2e824
f2e824
		if ($sync_batch >= $primary_max)
f2e824
		{
f2e824
			$url = $this->save_convert_progress('&final_jump=1');
f2e824
f2e824
			$template->assign_vars(array(
f2e824
				'L_SUBMIT'		=> $user->lang['CONTINUE_CONVERT'],
f2e824
				'U_ACTION'		=> $url,
f2e824
			));
f2e824
f2e824
			$this->meta_refresh($url);
f2e824
			return;
f2e824
		}
f2e824
		else
f2e824
		{
f2e824
			$sync_batch--;
f2e824
		}
f2e824
f2e824
		$url = $this->save_convert_progress('&sync_batch=' . $sync_batch);
f2e824
f2e824
		$template->assign_vars(array(
f2e824
			'L_SUBMIT'		=> $user->lang['CONTINUE_CONVERT'],
f2e824
			'U_ACTION'		=> $url,
f2e824
		));
f2e824
f2e824
		$this->meta_refresh($url);
f2e824
		return;
f2e824
	}
f2e824
f2e824
	/**
f2e824
	* Save the convertor status
f2e824
	*/
f2e824
	function save_convert_progress($step)
f2e824
	{
f2e824
		global $convert, $language;
f2e824
f2e824
		// Save convertor Status
f2e824
		set_config('convert_progress', serialize(array(
f2e824
			'step'			=> $step,
f2e824
			'table_prefix'	=> $convert->src_table_prefix,
f2e824
			'tag'			=> $convert->convertor_tag,
f2e824
		)), true);
f2e824
f2e824
		set_config('convert_db_server', serialize(array(
f2e824
			'dbms'			=> $convert->src_dbms,
f2e824
			'dbhost'		=> $convert->src_dbhost,
f2e824
			'dbport'		=> $convert->src_dbport,
f2e824
			'dbname'		=> $convert->src_dbname,
f2e824
		)), true);
f2e824
f2e824
		set_config('convert_db_user', serialize(array(
f2e824
			'dbuser'		=> $convert->src_dbuser,
f2e824
			'dbpasswd'		=> $convert->src_dbpasswd,
f2e824
		)), true);
f2e824
f2e824
		return $this->p_master->module_url . "?mode={$this->mode}&sub=in_progress&tag={$convert->convertor_tag}$step&language=$language";
f2e824
	}
f2e824
f2e824
	/**
f2e824
	* Finish conversion, the last function to be called.
f2e824
	*/
f2e824
	function finish_conversion()
f2e824
	{
f2e824
		global $db, $phpbb_root_path, $convert, $config, $language, $user, $template;
f2e824
f2e824
		$db->sql_query('DELETE FROM ' . CONFIG_TABLE . "
f2e824
			WHERE config_name = 'convert_progress'
f2e824
				OR config_name = 'convert_options'
f2e824
				OR config_name = 'convert_db_server'
f2e824
				OR config_name = 'convert_db_user'");
f2e824
		$db->sql_query('DELETE FROM ' . SESSIONS_TABLE);
f2e824
f2e824
		@unlink($phpbb_root_path . 'cache/data_global.php');
f2e824
		cache_moderators();
f2e824
f2e824
		// And finally, add a note to the log
f2e824
		add_log('admin', 'LOG_INSTALL_CONVERTED', $convert->convertor_data['forum_name'], $config['version']);
f2e824
f2e824
		$url = $this->p_master->module_url . "?mode={$this->mode}&sub=final&language=$language";
f2e824
f2e824
		$template->assign_vars(array(
f2e824
			'L_SUBMIT'		=> $user->lang['FINAL_STEP'],
f2e824
			'U_ACTION'		=> $url,
f2e824
		));
f2e824
f2e824
		$this->meta_refresh($url);
f2e824
		return;
f2e824
	}
f2e824
f2e824
	/**
f2e824
	* This function marks the steps after syncing
f2e824
	*/
f2e824
	function final_jump($final_jump)
f2e824
	{
f2e824
		global $template, $user, $src_db, $same_db, $db, $phpbb_root_path, $phpEx, $config, $cache;
f2e824
		global $convert;
f2e824
f2e824
		$template->assign_block_vars('checks', array(
f2e824
			'S_LEGEND'	=> true,
f2e824
			'LEGEND'	=> $user->lang['PROCESS_LAST'],
f2e824
		));
f2e824
f2e824
		if ($final_jump == 1)
f2e824
		{
f2e824
			$db->sql_return_on_error(true);
f2e824
f2e824
			update_topics_posted();
f2e824
f2e824
			$template->assign_block_vars('checks', array(
f2e824
				'TITLE'		=> $user->lang['UPDATE_TOPICS_POSTED'],
f2e824
				'RESULT'	=> $user->lang['DONE'],
f2e824
			));
f2e824
f2e824
			if ($db->sql_error_triggered)
f2e824
			{
f2e824
				$template->assign_vars(array(
f2e824
					'S_ERROR_BOX'	=> true,
f2e824
					'ERROR_TITLE'	=> $user->lang['UPDATE_TOPICS_POSTED'],
f2e824
					'ERROR_MSG'		=> $user->lang['UPDATE_TOPICS_POSTED_ERR'],
f2e824
				));
f2e824
			}
f2e824
			$db->sql_return_on_error(false);
f2e824
f2e824
			$this->finish_conversion();
f2e824
			return;
f2e824
		}
f2e824
	}
f2e824
f2e824
	/**
f2e824
	* This function marks the steps before syncing (jump=1)
f2e824
	*/
f2e824
	function jump($jump, $last_statement)
f2e824
	{
f2e824
		global $template, $user, $src_db, $same_db, $db, $phpbb_root_path, $phpEx, $config, $cache;
f2e824
		global $convert;
f2e824
f2e824
		$template->assign_block_vars('checks', array(
f2e824
			'S_LEGEND'	=> true,
f2e824
			'LEGEND'	=> $user->lang['PROCESS_LAST'],
f2e824
		));
f2e824
f2e824
		if ($jump == 1)
f2e824
		{
f2e824
			// Execute 'last' statements/queries
f2e824
			if (!empty($convert->convertor['execute_last']))
f2e824
			{
f2e824
				if (!is_array($convert->convertor['execute_last']))
f2e824
				{
f2e824
					eval($convert->convertor['execute_last']);
f2e824
				}
f2e824
				else
f2e824
				{
f2e824
					while ($last_statement < sizeof($convert->convertor['execute_last']))
f2e824
					{
f2e824
						eval($convert->convertor['execute_last'][$last_statement]);
f2e824
f2e824
						$template->assign_block_vars('checks', array(
f2e824
							'TITLE'		=> $convert->convertor['execute_last'][$last_statement],
f2e824
							'RESULT'	=> $user->lang['DONE'],
f2e824
						));
f2e824
f2e824
						$last_statement++;
f2e824
						$url = $this->save_convert_progress('&jump=1&last=' . $last_statement);
f2e824
f2e824
						$percentage = ($last_statement == 0) ? 0 : floor(100 / (sizeof($convert->convertor['execute_last']) / $last_statement));
f2e824
						$msg = sprintf($user->lang['STEP_PERCENT_COMPLETED'], $last_statement, sizeof($convert->convertor['execute_last']), $percentage);
f2e824
f2e824
						$template->assign_vars(array(
f2e824
							'L_SUBMIT'		=> $user->lang['CONTINUE_LAST'],
f2e824
							'L_MESSAGE'		=> $msg,
f2e824
							'U_ACTION'		=> $url,
f2e824
						));
f2e824
f2e824
						$this->meta_refresh($url);
f2e824
						return;
f2e824
					}
f2e824
				}
f2e824
			}
f2e824
f2e824
			if (!empty($convert->convertor['query_last']))
f2e824
			{
f2e824
				if (!is_array($convert->convertor['query_last']))
f2e824
				{
f2e824
					$convert->convertor['query_last'] = array('target', array($convert->convertor['query_last']));
f2e824
				}
f2e824
				else if (!is_array($convert->convertor['query_last'][0]))
f2e824
				{
f2e824
					$convert->convertor['query_last'] = array(array($convert->convertor['query_last'][0], $convert->convertor['query_last'][1]));
f2e824
				}
f2e824
f2e824
				foreach ($convert->convertor['query_last'] as $query_last)
f2e824
				{
f2e824
					if ($query_last[0] == 'src')
f2e824
					{
f2e824
						if ($convert->mysql_convert && $same_db)
f2e824
						{
f2e824
							$src_db->sql_query("SET NAMES 'binary'");
f2e824
						}
f2e824
f2e824
						$src_db->sql_query($query_last[1]);
f2e824
f2e824
						if ($convert->mysql_convert && $same_db)
f2e824
						{
f2e824
							$src_db->sql_query("SET NAMES 'utf8'");
f2e824
						}
f2e824
					}
f2e824
					else
f2e824
					{
f2e824
						$db->sql_query($query_last[1]);
f2e824
					}
f2e824
				}
f2e824
			}
f2e824
f2e824
			// Sanity check
f2e824
			$db->sql_return_on_error(false);
f2e824
			$src_db->sql_return_on_error(false);
f2e824
f2e824
			fix_empty_primary_groups();
f2e824
f2e824
			if (!isset($config['board_startdate']))
f2e824
			{
f2e824
				$sql = 'SELECT MIN(user_regdate) AS board_startdate
f2e824
					FROM ' . USERS_TABLE;
f2e824
				$result = $db->sql_query($sql);
f2e824
				$row = $db->sql_fetchrow($result);
f2e824
				$db->sql_freeresult($result);
f2e824
f2e824
				if (($row['board_startdate'] < $config['board_startdate'] && $row['board_startdate'] > 0) || !isset($config['board_startdate']))
f2e824
				{
f2e824
					set_config('board_startdate', $row['board_startdate']);
f2e824
					$db->sql_query('UPDATE ' . USERS_TABLE . ' SET user_regdate = ' . $row['board_startdate'] . ' WHERE user_id = ' . ANONYMOUS);
f2e824
				}
f2e824
			}
f2e824
f2e824
			update_dynamic_config();
f2e824
f2e824
			$template->assign_block_vars('checks', array(
f2e824
				'TITLE'		=> $user->lang['CLEAN_VERIFY'],
f2e824
				'RESULT'	=> $user->lang['DONE'],
f2e824
			));
f2e824
f2e824
			$url = $this->save_convert_progress('&jump=2');
f2e824
f2e824
			$template->assign_vars(array(
f2e824
				'L_SUBMIT'		=> $user->lang['CONTINUE_CONVERT'],
f2e824
				'U_ACTION'		=> $url,
f2e824
			));
f2e824
f2e824
			$this->meta_refresh($url);
f2e824
			return;
f2e824
		}
f2e824
f2e824
		if ($jump == 2)
f2e824
		{
f2e824
			$db->sql_query('UPDATE ' . USERS_TABLE . " SET user_permissions = ''");
f2e824
f2e824
			// TODO: sync() is likely going to bomb out on forums with a considerable amount of topics.
f2e824
			// TODO: the sync function is able to handle FROM-TO values, we should use them here (batch processing)
f2e824
			sync('forum', '', '', false, true);
f2e824
			$cache->destroy('sql', FORUMS_TABLE);
f2e824
f2e824
			$template->assign_block_vars('checks', array(
f2e824
				'TITLE'		=> $user->lang['SYNC_FORUMS'],
f2e824
				'RESULT'	=> $user->lang['DONE'],
f2e824
			));
f2e824
f2e824
			// Continue with synchronizing the forums...
f2e824
			$url = $this->save_convert_progress('&sync_batch=0');
f2e824
f2e824
			$template->assign_vars(array(
f2e824
				'L_SUBMIT'		=> $user->lang['CONTINUE_CONVERT'],
f2e824
				'U_ACTION'		=> $url,
f2e824
			));
f2e824
f2e824
			$this->meta_refresh($url);
f2e824
			return;
f2e824
		}
f2e824
	}
f2e824
f2e824
	function build_insert_query(&$schema, &$sql_data, $current_table)
f2e824
	{
f2e824
		global $db, $user;
f2e824
		global $convert;
f2e824
f2e824
		// Can we use IGNORE with this DBMS?
f2e824
		$sql_ignore = (strpos($db->sql_layer, 'mysql') === 0 && !defined('DEBUG_EXTRA')) ? 'IGNORE ' : '';
f2e824
		$insert_query = 'INSERT ' . $sql_ignore . 'INTO ' . $schema['target'] . ' (';
f2e824
f2e824
		$aliases = array();
f2e824
f2e824
		$sql_data = array(
f2e824
			'source_fields'		=> array(),
f2e824
			'target_fields'		=> array(),
f2e824
			'source_tables'		=> array(),
f2e824
			'select_fields'		=> array(),
f2e824
		);
f2e824
f2e824
		foreach ($schema as $key => $val)
f2e824
		{
f2e824
			// Example: array('group_name',				'extension_groups.group_name',		'htmlspecialchars'),
f2e824
			if (is_int($key))
f2e824
			{
f2e824
				if (!empty($val[0]))
f2e824
				{
f2e824
					// Target fields
f2e824
					$sql_data['target_fields'][$val[0]] = $key;
f2e824
					$insert_query .= $val[0] . ', ';
f2e824
				}
f2e824
f2e824
				if (!is_array($val[1]))
f2e824
				{
f2e824
					$val[1] = array($val[1]);
f2e824
				}
f2e824
f2e824
				foreach ($val[1] as $valkey => $value_1)
f2e824
				{
f2e824
					// This should cover about any case:
f2e824
					//
f2e824
					// table.field					=> SELECT table.field				FROM table
f2e824
					// table.field AS alias			=> SELECT table.field	AS alias	FROM table
f2e824
					// table.field AS table2.alias	=> SELECT table2.field	AS alias	FROM table table2
f2e824
					// table.field AS table2.field	=> SELECT table2.field				FROM table table2
f2e824
					//
f2e824
					if (preg_match('/^([a-z0-9_]+)\.([a-z0-9_]+)( +AS +(([a-z0-9_]+?)\.)?([a-z0-9_]+))?$/i', $value_1, $m))
f2e824
					{
f2e824
						// There is 'AS ...' in the field names
f2e824
						if (!empty($m[3]))
f2e824
						{
f2e824
							$value_1 = ($m[2] == $m[6]) ? $m[1] . '.' . $m[2] : $m[1] . '.' . $m[2] . ' AS ' . $m[6];
f2e824
f2e824
							// Table alias: store it then replace the source table with it
f2e824
							if (!empty($m[5]) && $m[5] != $m[1])
f2e824
							{
f2e824
								$aliases[$m[5]] = $m[1];
f2e824
								$value_1 = str_replace($m[1] . '.' . $m[2], $m[5] . '.' . $m[2], $value_1);
f2e824
							}
f2e824
						}
f2e824
						else
f2e824
						{
f2e824
							// No table alias
f2e824
							$sql_data['source_tables'][$m[1]] = (empty($convert->src_table_prefix)) ? $m[1] : $convert->src_table_prefix . $m[1] . ' ' . $m[1];
f2e824
						}
f2e824
f2e824
						$sql_data['select_fields'][$value_1] = $value_1;
f2e824
						$sql_data['source_fields'][$key][$valkey] = (!empty($m[6])) ? $m[6] : $m[2];
f2e824
					}
f2e824
				}
f2e824
			}
f2e824
			else if ($key == 'where' || $key == 'group_by' || $key == 'order_by' || $key == 'having')
f2e824
			{
f2e824
				if (@preg_match_all('/([a-z0-9_]+)\.([a-z0-9_]+)/i', $val, $m))
f2e824
				{
f2e824
					foreach ($m[1] as $value)
f2e824
					{
f2e824
						$sql_data['source_tables'][$value] = (empty($convert->src_table_prefix)) ? $value : $convert->src_table_prefix . $value . ' ' . $value;
f2e824
					}
f2e824
				}
f2e824
			}
f2e824
		}
f2e824
f2e824
		// Add the aliases to the list of tables
f2e824
		foreach ($aliases as $alias => $table)
f2e824
		{
f2e824
			$sql_data['source_tables'][$alias] = $convert->src_table_prefix . $table . ' ' . $alias;
f2e824
		}
f2e824
f2e824
		// 'left_join'		=> 'forums LEFT JOIN forum_prune ON forums.forum_id = forum_prune.forum_id',
f2e824
		if (!empty($schema['left_join']))
f2e824
		{
f2e824
			if (!is_array($schema['left_join']))
f2e824
			{
f2e824
				$schema['left_join'] = array($schema['left_join']);
f2e824
			}
f2e824
f2e824
			foreach ($schema['left_join'] as $left_join)
f2e824
			{
f2e824
				// This won't handle concatened LEFT JOINs
f2e824
				if (!preg_match('/([a-z0-9_]+) LEFT JOIN ([a-z0-9_]+) A?S? ?([a-z0-9_]*?) ?(ON|USING)(.*)/i', $left_join, $m))
f2e824
				{
f2e824
					$this->p_master->error(sprintf($user->lang['NOT_UNDERSTAND'], 'LEFT JOIN', $left_join, $current_table, $schema['target']), __LINE__, __FILE__);
f2e824
				}
f2e824
f2e824
				if (!empty($aliases[$m[2]]))
f2e824
				{
f2e824
					if (!empty($m[3]))
f2e824
					{
f2e824
						$this->p_master->error(sprintf($user->lang['NAMING_CONFLICT'], $m[2], $m[3], $schema['left_join']), __LINE__, __FILE__);
f2e824
					}
f2e824
f2e824
					$m[2] = $aliases[$m[2]];
f2e824
					$m[3] = $m[2];
f2e824
				}
f2e824
f2e824
				$right_table = $convert->src_table_prefix . $m[2];
f2e824
				if (!empty($m[3]))
f2e824
				{
f2e824
					unset($sql_data['source_tables'][$m[3]]);
f2e824
				}
f2e824
				else if ($m[2] != $m[1])
f2e824
				{
f2e824
					unset($sql_data['source_tables'][$m[2]]);
f2e824
				}
f2e824
f2e824
				if (strpos($sql_data['source_tables'][$m[1]], "\nLEFT JOIN") !== false)
f2e824
				{
f2e824
					$sql_data['source_tables'][$m[1]] = '(' . $sql_data['source_tables'][$m[1]] . ")\nLEFT JOIN $right_table";
f2e824
				}
f2e824
				else
f2e824
				{
f2e824
					$sql_data['source_tables'][$m[1]] .= "\nLEFT JOIN $right_table";
f2e824
				}
f2e824
f2e824
				if (!empty($m[3]))
f2e824
				{
f2e824
					unset($sql_data['source_tables'][$m[3]]);
f2e824
					$sql_data['source_tables'][$m[1]] .= ' AS ' . $m[3];
f2e824
				}
f2e824
				else if (!empty($convert->src_table_prefix))
f2e824
				{
f2e824
					$sql_data['source_tables'][$m[1]] .= ' AS ' . $m[2];
f2e824
				}
f2e824
				$sql_data['source_tables'][$m[1]] .= ' ' . $m[4] . $m[5];
f2e824
			}
f2e824
		}
f2e824
f2e824
		// Remove ", " from the end of the insert query
f2e824
		$insert_query = substr($insert_query, 0, -2) . ') VALUES ';
f2e824
f2e824
		return $insert_query;
f2e824
	}
f2e824
f2e824
	/**
f2e824
	* Function for processing the currently handled row
f2e824
	*/
f2e824
	function process_row(&$schema, &$sql_data, &$insert_values)
f2e824
	{
f2e824
		global $template, $user, $phpbb_root_path, $phpEx, $db, $lang, $config, $cache;
f2e824
		global $convert, $convert_row;
f2e824
f2e824
		$sql_flag = false;
f2e824
f2e824
		foreach ($schema as $key => $fields)
f2e824
		{
f2e824
			// We are only interested in the lines with:
f2e824
			// array('comment', 'attachments_desc.comment', 'htmlspecialchars'),
f2e824
			if (is_int($key))
f2e824
			{
f2e824
				if (!is_array($fields[1]))
f2e824
				{
f2e824
					$fields[1] = array($fields[1]);
f2e824
				}
f2e824
f2e824
				$firstkey_set = false;
f2e824
				$firstkey = 0;
f2e824
f2e824
				foreach ($fields[1] as $inner_key => $inner_value)
f2e824
				{
f2e824
					if (!$firstkey_set)
f2e824
					{
f2e824
						$firstkey = $inner_key;
f2e824
						$firstkey_set = true;
f2e824
					}
f2e824
f2e824
					$src_field = isset($sql_data['source_fields'][$key][$inner_key]) ? $sql_data['source_fields'][$key][$inner_key] : '';
f2e824
f2e824
					if (!empty($src_field))
f2e824
					{
f2e824
						$fields[1][$inner_key] = $convert->row[$src_field];
f2e824
					}
f2e824
				}
f2e824
f2e824
				if (!empty($fields[0]))
f2e824
				{
f2e824
					// We have a target field, if we haven't set $sql_flag yet it will be set to TRUE.
f2e824
					// If a function has already set it to FALSE it won't change it.
f2e824
					if ($sql_flag === false)
f2e824
					{
f2e824
						$sql_flag = true;
f2e824
					}
f2e824
f2e824
					// No function assigned?
f2e824
					if (empty($fields[2]))
f2e824
					{
f2e824
						$value = $fields[1][$firstkey];
f2e824
					}
f2e824
					else if (is_array($fields[2]))
f2e824
					{
f2e824
						// Execute complex function/eval/typecast
f2e824
						$value = $fields[1];
f2e824
f2e824
						foreach ($fields[2] as $type => $execution)
f2e824
						{
f2e824
							if (strpos($type, 'typecast') === 0)
f2e824
							{
f2e824
								if (!is_array($value))
f2e824
								{
f2e824
									$value = array($value);
f2e824
								}
f2e824
								$value = $value[0];
f2e824
								settype($value, $execution);
f2e824
							}
f2e824
							else if (strpos($type, 'function') === 0)
f2e824
							{
f2e824
								if (!is_array($value))
f2e824
								{
f2e824
									$value = array($value);
f2e824
								}
f2e824
f2e824
								$value = call_user_func_array($execution, $value);
f2e824
							}
f2e824
							else if (strpos($type, 'execute') === 0)
f2e824
							{
f2e824
								if (!is_array($value))
f2e824
								{
f2e824
									$value = array($value);
f2e824
								}
f2e824
f2e824
								$execution = str_replace('{RESULT}', '$value', $execution);
f2e824
								$execution = str_replace('{VALUE}', '$value', $execution);
f2e824
								eval($execution);
f2e824
							}
f2e824
						}
f2e824
					}
f2e824
					else
f2e824
					{
f2e824
						$value = call_user_func_array($fields[2], $fields[1]);
f2e824
					}
f2e824
f2e824
					if (is_null($value))
f2e824
					{
f2e824
						$value = '';
f2e824
					}
f2e824
f2e824
					$insert_values[] = $db->_sql_validate_value($value);
f2e824
				}
f2e824
				else if (!empty($fields[2]))
f2e824
				{
f2e824
					if (is_array($fields[2]))
f2e824
					{
f2e824
						// Execute complex function/eval/typecast
f2e824
						$value = '';
f2e824
f2e824
						foreach ($fields[2] as $type => $execution)
f2e824
						{
f2e824
							if (strpos($type, 'typecast') === 0)
f2e824
							{
f2e824
								$value = settype($value, $execution);
f2e824
							}
f2e824
							else if (strpos($type, 'function') === 0)
f2e824
							{
f2e824
								if (!is_array($value))
f2e824
								{
f2e824
									$value = array($value);
f2e824
								}
f2e824
f2e824
								$value = call_user_func_array($execution, $value);
f2e824
							}
f2e824
							else if (strpos($type, 'execute') === 0)
f2e824
							{
f2e824
								if (!is_array($value))
f2e824
								{
f2e824
									$value = array($value);
f2e824
								}
f2e824
f2e824
								$execution = str_replace('{RESULT}', '$value', $execution);
f2e824
								$execution = str_replace('{VALUE}', '$value', $execution);
f2e824
								eval($execution);
f2e824
							}
f2e824
						}
f2e824
					}
f2e824
					else
f2e824
					{
f2e824
						call_user_func_array($fields[2], $fields[1]);
f2e824
					}
f2e824
				}
f2e824
			}
f2e824
		}
f2e824
f2e824
		return $sql_flag;
f2e824
	}
f2e824
f2e824
	/**
f2e824
	* Own meta refresh function to be able to change the global time used
f2e824
	*/
f2e824
	function meta_refresh($url)
f2e824
	{
f2e824
		global $convert, $template;
f2e824
f2e824
		if ($convert->options['refresh'])
f2e824
		{
f2e824
			// Because we should not rely on correct settings, we simply use the relative path here directly.
f2e824
			$template->assign_vars(array(
f2e824
				'S_REFRESH'	=> true,
f2e824
				'META'		=> '<meta http-equiv="refresh" content="5;url=' . $url . '" />')
f2e824
			);
f2e824
		}
f2e824
	}
f2e824
f2e824
	/**
f2e824
	* The information below will be used to build the input fields presented to the user
f2e824
	*/
f2e824
	var $convert_options = array(
f2e824
		'legend1'			=> 'SPECIFY_OPTIONS',
f2e824
		'src_dbms'			=> array('lang' => 'DBMS',			'type' => 'select', 'options' => 'dbms_select(\'{VALUE}\', true)', 'explain' => false),
f2e824
		'src_dbhost'		=> array('lang' => 'DB_HOST',		'type' => 'text:25:100', 'explain' => true),
f2e824
		'src_dbport'		=> array('lang' => 'DB_PORT',		'type' => 'text:25:100', 'explain' => true),
f2e824
		'src_dbname'		=> array('lang' => 'DB_NAME',		'type' => 'text:25:100', 'explain' => false),
f2e824
		'src_dbuser'		=> array('lang' => 'DB_USERNAME',	'type' => 'text:25:100', 'explain' => false),
f2e824
		'src_dbpasswd'		=> array('lang' => 'DB_PASSWORD',	'type' => 'password:25:100', 'explain' => false),
f2e824
		'src_table_prefix'	=> array('lang' => 'TABLE_PREFIX',	'type' => 'text:25:100', 'explain' => false),
f2e824
		//'src_url'			=> array('lang' => 'FORUM_ADDRESS',	'type' => 'text:50:100', 'explain' => true),
f2e824
		'forum_path'		=> array('lang' => 'FORUM_PATH',	'type' => 'text:25:100', 'explain' => true),
f2e824
		'refresh'			=> array('lang' => 'REFRESH_PAGE',	'type' => 'radio:yes_no', 'explain' => true),
f2e824
	);
f2e824
}
f2e824
f2e824
?>