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

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

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

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

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

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