Blame Extras/phpBB/3.0.4/install/install_convert.php

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

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

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

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

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