install/install_convert.php
author indvdum (gotoindvdum[at]gmail[dot]com)
Wed, 07 Mar 2012 17:41:16 +0400
changeset 2 025d5f405f1f
parent 0 ceef6d64c21d
permissions -rw-r--r--
HG ignore
indvdum@0
     1
<?php
indvdum@0
     2
/**
indvdum@0
     3
*
indvdum@0
     4
* @package install
indvdum@0
     5
* @version $Id$
indvdum@0
     6
* @copyright (c) 2006 phpBB Group
indvdum@0
     7
* @license http://opensource.org/licenses/gpl-license.php GNU Public License
indvdum@0
     8
*
indvdum@0
     9
*/
indvdum@0
    10
indvdum@0
    11
/**
indvdum@0
    12
*/
indvdum@0
    13
indvdum@0
    14
if (!defined('IN_INSTALL'))
indvdum@0
    15
{
indvdum@0
    16
	// Someone has tried to access the file direct. This is not a good idea, so exit
indvdum@0
    17
	exit;
indvdum@0
    18
}
indvdum@0
    19
indvdum@0
    20
if (!empty($setmodules))
indvdum@0
    21
{
indvdum@0
    22
	$module[] = array(
indvdum@0
    23
		'module_type'		=> 'install',
indvdum@0
    24
		'module_title'		=> 'CONVERT',
indvdum@0
    25
		'module_filename'	=> substr(basename(__FILE__), 0, -strlen($phpEx)-1),
indvdum@0
    26
		'module_order'		=> 20,
indvdum@0
    27
		'module_subs'		=> '',
indvdum@0
    28
		'module_stages'		=> array('INTRO', 'SETTINGS', 'IN_PROGRESS', 'FINAL'),
indvdum@0
    29
		'module_reqs'		=> ''
indvdum@0
    30
	);
indvdum@0
    31
}
indvdum@0
    32
indvdum@0
    33
/**
indvdum@0
    34
* Class holding all convertor-specific details.
indvdum@0
    35
* @package install
indvdum@0
    36
*/
indvdum@0
    37
class convert
indvdum@0
    38
{
indvdum@0
    39
	var $options = array();
indvdum@0
    40
indvdum@0
    41
	var $convertor_tag = '';
indvdum@0
    42
	var $src_dbms = '';
indvdum@0
    43
	var $src_dbhost = '';
indvdum@0
    44
	var $src_dbport = '';
indvdum@0
    45
	var $src_dbuser = '';
indvdum@0
    46
	var $src_dbpasswd = '';
indvdum@0
    47
	var $src_dbname = '';
indvdum@0
    48
	var $src_table_prefix = '';
indvdum@0
    49
indvdum@0
    50
	var $convertor_data = array();
indvdum@0
    51
	var $tables = array();
indvdum@0
    52
	var $config_schema = array();
indvdum@0
    53
	var $convertor = array();
indvdum@0
    54
	var $src_truncate_statement = 'DELETE FROM ';
indvdum@0
    55
	var $truncate_statement = 'DELETE FROM ';
indvdum@0
    56
indvdum@0
    57
	var $fulltext_search;
indvdum@0
    58
indvdum@0
    59
	// Batch size, can be adjusted by the conversion file
indvdum@0
    60
	// For big boards a value of 6000 seems to be optimal
indvdum@0
    61
	var $batch_size = 2000;
indvdum@0
    62
	// Number of rows to be inserted at once (extended insert) if supported
indvdum@0
    63
	// For installations having enough memory a value of 60 may be good.
indvdum@0
    64
	var $num_wait_rows = 20;
indvdum@0
    65
indvdum@0
    66
	// Mysqls internal recoding engine messing up with our (better) functions? We at least support more encodings than mysql so should use it in favor.
indvdum@0
    67
	var $mysql_convert = false;
indvdum@0
    68
indvdum@0
    69
	var $p_master;
indvdum@0
    70
indvdum@0
    71
	function convert(&$p_master)
indvdum@0
    72
	{
indvdum@0
    73
		$this->p_master = &$p_master;
indvdum@0
    74
	}
indvdum@0
    75
}
indvdum@0
    76
indvdum@0
    77
/**
indvdum@0
    78
* Convert class for conversions
indvdum@0
    79
* @package install
indvdum@0
    80
*/
indvdum@0
    81
class install_convert extends module
indvdum@0
    82
{
indvdum@0
    83
	/**
indvdum@0
    84
	* Variables used while converting, they are accessible from the global variable $convert
indvdum@0
    85
	*/
indvdum@0
    86
	function install_convert(&$p_master)
indvdum@0
    87
	{
indvdum@0
    88
		$this->p_master = &$p_master;
indvdum@0
    89
	}
indvdum@0
    90
indvdum@0
    91
	function main($mode, $sub)
indvdum@0
    92
	{
indvdum@0
    93
		global $lang, $template, $phpbb_root_path, $phpEx, $cache, $config, $language, $table_prefix;
indvdum@0
    94
		global $convert;
indvdum@0
    95
indvdum@0
    96
		$this->tpl_name = 'install_convert';
indvdum@0
    97
		$this->mode = $mode;
indvdum@0
    98
indvdum@0
    99
		$convert = new convert($this->p_master);
indvdum@0
   100
indvdum@0
   101
		switch ($sub)
indvdum@0
   102
		{
indvdum@0
   103
			case 'intro':
indvdum@0
   104
				// Try opening config file
indvdum@0
   105
				// @todo If phpBB is not installed, we need to do a cut-down installation here
indvdum@0
   106
				// For now, we redirect to the installation script instead
indvdum@0
   107
				if (@file_exists($phpbb_root_path . 'config.' . $phpEx))
indvdum@0
   108
				{
indvdum@0
   109
					include($phpbb_root_path . 'config.' . $phpEx);
indvdum@0
   110
				}
indvdum@0
   111
indvdum@0
   112
				if (!defined('PHPBB_INSTALLED'))
indvdum@0
   113
				{
indvdum@0
   114
					$template->assign_vars(array(
indvdum@0
   115
						'S_NOT_INSTALLED'		=> true,
indvdum@0
   116
						'TITLE'					=> $lang['BOARD_NOT_INSTALLED'],
indvdum@0
   117
						'BODY'					=> sprintf($lang['BOARD_NOT_INSTALLED_EXPLAIN'], append_sid($phpbb_root_path . 'install/index.' . $phpEx, 'mode=install&amp;language=' . $language)),
indvdum@0
   118
					));
indvdum@0
   119
indvdum@0
   120
					return;
indvdum@0
   121
				}
indvdum@0
   122
indvdum@0
   123
				require($phpbb_root_path . 'config.' . $phpEx);
indvdum@0
   124
				require($phpbb_root_path . 'includes/constants.' . $phpEx);
indvdum@0
   125
				require($phpbb_root_path . 'includes/db/' . $dbms . '.' . $phpEx);
indvdum@0
   126
				require($phpbb_root_path . 'includes/functions_convert.' . $phpEx);
indvdum@0
   127
indvdum@0
   128
				$db = new $sql_db();
indvdum@0
   129
				$db->sql_connect($dbhost, $dbuser, $dbpasswd, $dbname, $dbport, false, true);
indvdum@0
   130
				unset($dbpasswd);
indvdum@0
   131
indvdum@0
   132
				// We need to fill the config to let internal functions correctly work
indvdum@0
   133
				$sql = 'SELECT *
indvdum@0
   134
					FROM ' . CONFIG_TABLE;
indvdum@0
   135
				$result = $db->sql_query($sql);
indvdum@0
   136
indvdum@0
   137
				$config = array();
indvdum@0
   138
				while ($row = $db->sql_fetchrow($result))
indvdum@0
   139
				{
indvdum@0
   140
					$config[$row['config_name']] = $row['config_value'];
indvdum@0
   141
				}
indvdum@0
   142
				$db->sql_freeresult($result);
indvdum@0
   143
indvdum@0
   144
				// Detect if there is already a conversion in progress at this point and offer to resume
indvdum@0
   145
				// It's quite possible that the user will get disconnected during a large conversion so they need to be able to resume it
indvdum@0
   146
				$new_conversion = request_var('new_conv', 0);
indvdum@0
   147
indvdum@0
   148
				if ($new_conversion)
indvdum@0
   149
				{
indvdum@0
   150
					$config['convert_progress'] = '';
indvdum@0
   151
					$config['convert_db_server'] = '';
indvdum@0
   152
					$config['convert_db_user'] = '';
indvdum@0
   153
					$db->sql_query('DELETE FROM ' . CONFIG_TABLE . "
indvdum@0
   154
						WHERE config_name = 'convert_progress'
indvdum@0
   155
							OR config_name = 'convert_db_server'
indvdum@0
   156
							OR config_name = 'convert_db_user'"
indvdum@0
   157
					);
indvdum@0
   158
				}
indvdum@0
   159
indvdum@0
   160
				// Let's see if there is a conversion in the works...
indvdum@0
   161
				$options = array();
indvdum@0
   162
				if (!empty($config['convert_progress']) && !empty($config['convert_db_server']) && !empty($config['convert_db_user']) && !empty($config['convert_options']))
indvdum@0
   163
				{
indvdum@0
   164
					$options = unserialize($config['convert_progress']);
indvdum@0
   165
					$options = array_merge($options, unserialize($config['convert_db_server']), unserialize($config['convert_db_user']), unserialize($config['convert_options']));
indvdum@0
   166
				}
indvdum@0
   167
indvdum@0
   168
				// This information should have already been checked once, but do it again for safety
indvdum@0
   169
				if (!empty($options) && !empty($options['tag']) &&
indvdum@0
   170
					isset($options['dbms']) &&
indvdum@0
   171
					isset($options['dbhost']) &&
indvdum@0
   172
					isset($options['dbport']) &&
indvdum@0
   173
					isset($options['dbuser']) &&
indvdum@0
   174
					isset($options['dbpasswd']) &&
indvdum@0
   175
					isset($options['dbname']) &&
indvdum@0
   176
					isset($options['table_prefix']))
indvdum@0
   177
				{
indvdum@0
   178
					$this->page_title = $lang['CONTINUE_CONVERT'];
indvdum@0
   179
indvdum@0
   180
					$template->assign_vars(array(
indvdum@0
   181
						'TITLE'			=> $lang['CONTINUE_CONVERT'],
indvdum@0
   182
						'BODY'			=> $lang['CONTINUE_CONVERT_BODY'],
indvdum@0
   183
						'L_NEW'			=> $lang['CONVERT_NEW_CONVERSION'],
indvdum@0
   184
						'L_CONTINUE'	=> $lang['CONTINUE_OLD_CONVERSION'],
indvdum@0
   185
						'S_CONTINUE'	=> true,
indvdum@0
   186
indvdum@0
   187
						'U_NEW_ACTION'		=> $this->p_master->module_url . "?mode={$this->mode}&amp;sub=intro&amp;new_conv=1&amp;language=$language",
indvdum@0
   188
						'U_CONTINUE_ACTION'	=> $this->p_master->module_url . "?mode={$this->mode}&amp;sub=in_progress&amp;tag={$options['tag']}{$options['step']}&amp;language=$language",
indvdum@0
   189
					));
indvdum@0
   190
indvdum@0
   191
					return;
indvdum@0
   192
				}
indvdum@0
   193
indvdum@0
   194
				$this->list_convertors($sub);
indvdum@0
   195
indvdum@0
   196
			break;
indvdum@0
   197
indvdum@0
   198
			case 'settings':
indvdum@0
   199
				$this->get_convert_settings($sub);
indvdum@0
   200
			break;
indvdum@0
   201
indvdum@0
   202
			case 'in_progress':
indvdum@0
   203
				$this->convert_data($sub);
indvdum@0
   204
			break;
indvdum@0
   205
indvdum@0
   206
			case 'final':
indvdum@0
   207
				$this->page_title = $lang['CONVERT_COMPLETE'];
indvdum@0
   208
indvdum@0
   209
				$template->assign_vars(array(
indvdum@0
   210
					'TITLE'		=> $lang['CONVERT_COMPLETE'],
indvdum@0
   211
					'BODY'		=> $lang['CONVERT_COMPLETE_EXPLAIN'],
indvdum@0
   212
				));
indvdum@0
   213
indvdum@0
   214
				// If we reached this step (conversion completed) we want to purge the cache and log the user out.
indvdum@0
   215
				// This is for making sure the session get not screwed due to the 3.0.x users table being completely new.
indvdum@0
   216
				$cache->purge();
indvdum@0
   217
indvdum@0
   218
				require($phpbb_root_path . 'config.' . $phpEx);
indvdum@0
   219
				require($phpbb_root_path . 'includes/constants.' . $phpEx);
indvdum@0
   220
				require($phpbb_root_path . 'includes/db/' . $dbms . '.' . $phpEx);
indvdum@0
   221
				require($phpbb_root_path . 'includes/functions_convert.' . $phpEx);
indvdum@0
   222
indvdum@0
   223
				$db = new $sql_db();
indvdum@0
   224
				$db->sql_connect($dbhost, $dbuser, $dbpasswd, $dbname, $dbport, false, true);
indvdum@0
   225
				unset($dbpasswd);
indvdum@0
   226
indvdum@0
   227
				$sql = 'SELECT config_value
indvdum@0
   228
					FROM ' . CONFIG_TABLE . '
indvdum@0
   229
					WHERE config_name = \'search_type\'';
indvdum@0
   230
				$result = $db->sql_query($sql);
indvdum@0
   231
indvdum@0
   232
				if ($db->sql_fetchfield('config_value') != 'fulltext_mysql')
indvdum@0
   233
				{
indvdum@0
   234
					$template->assign_vars(array(
indvdum@0
   235
						'S_ERROR_BOX'	=> true,
indvdum@0
   236
						'ERROR_TITLE'	=> $lang['SEARCH_INDEX_UNCONVERTED'],
indvdum@0
   237
						'ERROR_MSG'		=> $lang['SEARCH_INDEX_UNCONVERTED_EXPLAIN'],
indvdum@0
   238
					));
indvdum@0
   239
				}
indvdum@0
   240
indvdum@0
   241
				switch ($db->sql_layer)
indvdum@0
   242
				{
indvdum@0
   243
					case 'sqlite':
indvdum@0
   244
					case 'firebird':
indvdum@0
   245
						$db->sql_query('DELETE FROM ' . SESSIONS_KEYS_TABLE);
indvdum@0
   246
						$db->sql_query('DELETE FROM ' . SESSIONS_TABLE);
indvdum@0
   247
					break;
indvdum@0
   248
indvdum@0
   249
					default:
indvdum@0
   250
						$db->sql_query('TRUNCATE TABLE ' . SESSIONS_KEYS_TABLE);
indvdum@0
   251
						$db->sql_query('TRUNCATE TABLE ' . SESSIONS_TABLE);
indvdum@0
   252
					break;
indvdum@0
   253
				}
indvdum@0
   254
indvdum@0
   255
			break;
indvdum@0
   256
		}
indvdum@0
   257
	}
indvdum@0
   258
indvdum@0
   259
	/**
indvdum@0
   260
	* Generate a list of all available conversion modules
indvdum@0
   261
	*/
indvdum@0
   262
	function list_convertors($sub)
indvdum@0
   263
	{
indvdum@0
   264
		global $lang, $language, $template, $phpbb_root_path, $phpEx;
indvdum@0
   265
indvdum@0
   266
		$this->page_title = $lang['SUB_INTRO'];
indvdum@0
   267
indvdum@0
   268
		$template->assign_vars(array(
indvdum@0
   269
			'TITLE'		=> $lang['CONVERT_INTRO'],
indvdum@0
   270
			'BODY'		=> $lang['CONVERT_INTRO_BODY'],
indvdum@0
   271
indvdum@0
   272
			'L_AUTHOR'					=> $lang['AUTHOR'],
indvdum@0
   273
			'L_AVAILABLE_CONVERTORS'	=> $lang['AVAILABLE_CONVERTORS'],
indvdum@0
   274
			'L_CONVERT'					=> $lang['CONVERT'],
indvdum@0
   275
			'L_NO_CONVERTORS'			=> $lang['NO_CONVERTORS'],
indvdum@0
   276
			'L_OPTIONS'					=> $lang['CONVERT_OPTIONS'],
indvdum@0
   277
			'L_SOFTWARE'				=> $lang['SOFTWARE'],
indvdum@0
   278
			'L_VERSION'					=> $lang['VERSION'],
indvdum@0
   279
indvdum@0
   280
			'S_LIST'	=> true,
indvdum@0
   281
		));
indvdum@0
   282
indvdum@0
   283
		$convertors = $sort = array();
indvdum@0
   284
		$get_info = true;
indvdum@0
   285
indvdum@0
   286
		$handle = @opendir('./convertors/');
indvdum@0
   287
indvdum@0
   288
		if (!$handle)
indvdum@0
   289
		{
indvdum@0
   290
			$this->error('Unable to access the convertors directory', __LINE__, __FILE__);
indvdum@0
   291
		}
indvdum@0
   292
indvdum@0
   293
		while ($entry = readdir($handle))
indvdum@0
   294
		{
indvdum@0
   295
			if (preg_match('/^convert_([a-z0-9_]+).' . $phpEx . '$/i', $entry, $m))
indvdum@0
   296
			{
indvdum@0
   297
				include('./convertors/' . $entry);
indvdum@0
   298
				if (isset($convertor_data))
indvdum@0
   299
				{
indvdum@0
   300
					$sort[strtolower($convertor_data['forum_name'])] = sizeof($convertors);
indvdum@0
   301
indvdum@0
   302
					$convertors[] = array(
indvdum@0
   303
						'tag'			=>	$m[1],
indvdum@0
   304
						'forum_name'	=>	$convertor_data['forum_name'],
indvdum@0
   305
						'version'		=>	$convertor_data['version'],
indvdum@0
   306
						'dbms'			=>	$convertor_data['dbms'],
indvdum@0
   307
						'dbhost'		=>	$convertor_data['dbhost'],
indvdum@0
   308
						'dbport'		=>	$convertor_data['dbport'],
indvdum@0
   309
						'dbuser'		=>	$convertor_data['dbuser'],
indvdum@0
   310
						'dbpasswd'		=>	$convertor_data['dbpasswd'],
indvdum@0
   311
						'dbname'		=>	$convertor_data['dbname'],
indvdum@0
   312
						'table_prefix'	=>	$convertor_data['table_prefix'],
indvdum@0
   313
						'author'		=>	$convertor_data['author']
indvdum@0
   314
					);
indvdum@0
   315
				}
indvdum@0
   316
				unset($convertor_data);
indvdum@0
   317
			}
indvdum@0
   318
		}
indvdum@0
   319
		closedir($handle);
indvdum@0
   320
indvdum@0
   321
		@ksort($sort);
indvdum@0
   322
indvdum@0
   323
		foreach ($sort as $void => $index)
indvdum@0
   324
		{
indvdum@0
   325
			$template->assign_block_vars('convertors', array(
indvdum@0
   326
				'AUTHOR'	=> $convertors[$index]['author'],
indvdum@0
   327
				'SOFTWARE'	=> $convertors[$index]['forum_name'],
indvdum@0
   328
				'VERSION'	=> $convertors[$index]['version'],
indvdum@0
   329
indvdum@0
   330
				'U_CONVERT'	=> $this->p_master->module_url . "?mode={$this->mode}&amp;language=$language&amp;sub=settings&amp;tag=" . $convertors[$index]['tag'],
indvdum@0
   331
			));
indvdum@0
   332
		}
indvdum@0
   333
	}
indvdum@0
   334
indvdum@0
   335
	/**
indvdum@0
   336
	*/
indvdum@0
   337
	function get_convert_settings($sub)
indvdum@0
   338
	{
indvdum@0
   339
		global $lang, $language, $template, $db, $phpbb_root_path, $phpEx, $config, $cache;
indvdum@0
   340
indvdum@0
   341
		require($phpbb_root_path . 'config.' . $phpEx);
indvdum@0
   342
		require($phpbb_root_path . 'includes/constants.' . $phpEx);
indvdum@0
   343
		require($phpbb_root_path . 'includes/db/' . $dbms . '.' . $phpEx);
indvdum@0
   344
		require($phpbb_root_path . 'includes/functions_convert.' . $phpEx);
indvdum@0
   345
indvdum@0
   346
		$db = new $sql_db();
indvdum@0
   347
		$db->sql_connect($dbhost, $dbuser, $dbpasswd, $dbname, $dbport, false, true);
indvdum@0
   348
		unset($dbpasswd);
indvdum@0
   349
indvdum@0
   350
		$this->page_title = $lang['STAGE_SETTINGS'];
indvdum@0
   351
indvdum@0
   352
		// We need to fill the config to let internal functions correctly work
indvdum@0
   353
		$sql = 'SELECT *
indvdum@0
   354
			FROM ' . CONFIG_TABLE;
indvdum@0
   355
		$result = $db->sql_query($sql);
indvdum@0
   356
indvdum@0
   357
		$config = array();
indvdum@0
   358
		while ($row = $db->sql_fetchrow($result))
indvdum@0
   359
		{
indvdum@0
   360
			$config[$row['config_name']] = $row['config_value'];
indvdum@0
   361
		}
indvdum@0
   362
		$db->sql_freeresult($result);
indvdum@0
   363
indvdum@0
   364
		$convertor_tag = request_var('tag', '');
indvdum@0
   365
indvdum@0
   366
		if (empty($convertor_tag))
indvdum@0
   367
		{
indvdum@0
   368
			$this->p_master->error($lang['NO_CONVERT_SPECIFIED'], __LINE__, __FILE__);
indvdum@0
   369
		}
indvdum@0
   370
		$get_info = true;
indvdum@0
   371
indvdum@0
   372
		// check security implications of direct inclusion
indvdum@0
   373
		$convertor_tag = basename($convertor_tag);
indvdum@0
   374
		if (!file_exists('./convertors/convert_' . $convertor_tag . '.' . $phpEx))
indvdum@0
   375
		{
indvdum@0
   376
			$this->p_master->error($lang['CONVERT_NOT_EXIST'], __LINE__, __FILE__);
indvdum@0
   377
		}
indvdum@0
   378
indvdum@0
   379
		include('./convertors/convert_' . $convertor_tag . '.' . $phpEx);
indvdum@0
   380
indvdum@0
   381
		// The test_file is a file that should be present in the location of the old board.
indvdum@0
   382
		if (!isset($test_file))
indvdum@0
   383
		{
indvdum@0
   384
			$this->p_master->error($lang['DEV_NO_TEST_FILE'], __LINE__, __FILE__);
indvdum@0
   385
		}
indvdum@0
   386
indvdum@0
   387
		$submit = (isset($_POST['submit'])) ? true : false;
indvdum@0
   388
indvdum@0
   389
		$src_dbms			= request_var('src_dbms', $convertor_data['dbms']);
indvdum@0
   390
		$src_dbhost			= request_var('src_dbhost', $convertor_data['dbhost']);
indvdum@0
   391
		$src_dbport			= request_var('src_dbport', $convertor_data['dbport']);
indvdum@0
   392
		$src_dbuser			= request_var('src_dbuser', $convertor_data['dbuser']);
indvdum@0
   393
		$src_dbpasswd		= request_var('src_dbpasswd', $convertor_data['dbpasswd']);
indvdum@0
   394
		$src_dbname			= request_var('src_dbname', $convertor_data['dbname']);
indvdum@0
   395
		$src_table_prefix	= request_var('src_table_prefix', $convertor_data['table_prefix']);
indvdum@0
   396
		$forum_path			= request_var('forum_path', $convertor_data['forum_path']);
indvdum@0
   397
		$refresh			= request_var('refresh', 1);
indvdum@0
   398
indvdum@0
   399
		// Default URL of the old board
indvdum@0
   400
		// @todo Are we going to use this for attempting to convert URL references in posts, or should we remove it?
indvdum@0
   401
		//		-> We should convert old urls to the new relative urls format
indvdum@0
   402
		// $src_url = request_var('src_url', 'Not in use at the moment');
indvdum@0
   403
indvdum@0
   404
		// strip trailing slash from old forum path
indvdum@0
   405
		$forum_path = (strlen($forum_path) && $forum_path[strlen($forum_path) - 1] == '/') ? substr($forum_path, 0, -1) : $forum_path;
indvdum@0
   406
indvdum@0
   407
		$error = array();
indvdum@0
   408
		if ($submit)
indvdum@0
   409
		{
indvdum@0
   410
			if (!@file_exists('./../' . $forum_path . '/' . $test_file))
indvdum@0
   411
			{
indvdum@0
   412
				$error[] = sprintf($lang['COULD_NOT_FIND_PATH'], $forum_path);
indvdum@0
   413
			}
indvdum@0
   414
indvdum@0
   415
			$connect_test = false;
indvdum@0
   416
			$available_dbms = get_available_dbms(false, true, true);
indvdum@0
   417
indvdum@0
   418
			if (!isset($available_dbms[$src_dbms]) || !$available_dbms[$src_dbms]['AVAILABLE'])
indvdum@0
   419
			{
indvdum@0
   420
				$error['db'][] = $lang['INST_ERR_NO_DB'];
indvdum@0
   421
				$connect_test = false;
indvdum@0
   422
			}
indvdum@0
   423
			else
indvdum@0
   424
			{
indvdum@0
   425
				$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);
indvdum@0
   426
			}
indvdum@0
   427
indvdum@0
   428
			// The forum prefix of the old and the new forum can only be the same if two different databases are used.
indvdum@0
   429
			if ($src_table_prefix == $table_prefix && $src_dbms == $dbms && $src_dbhost == $dbhost && $src_dbport == $dbport && $src_dbname == $dbname)
indvdum@0
   430
			{
indvdum@0
   431
				$error[] = sprintf($lang['TABLE_PREFIX_SAME'], $src_table_prefix);
indvdum@0
   432
			}
indvdum@0
   433
indvdum@0
   434
			// Check table prefix
indvdum@0
   435
			if (!sizeof($error))
indvdum@0
   436
			{
indvdum@0
   437
				// initiate database connection to old db if old and new db differ
indvdum@0
   438
				global $src_db, $same_db;
indvdum@0
   439
				$src_db = $same_db = false;
indvdum@0
   440
indvdum@0
   441
				if ($src_dbms != $dbms || $src_dbhost != $dbhost || $src_dbport != $dbport || $src_dbname != $dbname || $src_dbuser != $dbuser)
indvdum@0
   442
				{
indvdum@0
   443
					$sql_db = 'dbal_' . $src_dbms;
indvdum@0
   444
					$src_db = new $sql_db();
indvdum@0
   445
					$src_db->sql_connect($src_dbhost, $src_dbuser, htmlspecialchars_decode($src_dbpasswd), $src_dbname, $src_dbport, false, true);
indvdum@0
   446
					$same_db = false;
indvdum@0
   447
				}
indvdum@0
   448
				else
indvdum@0
   449
				{
indvdum@0
   450
					$src_db = $db;
indvdum@0
   451
					$same_db = true;
indvdum@0
   452
				}
indvdum@0
   453
indvdum@0
   454
				$src_db->sql_return_on_error(true);
indvdum@0
   455
				$db->sql_return_on_error(true);
indvdum@0
   456
indvdum@0
   457
				// Try to select one row from the first table to see if the prefix is OK
indvdum@0
   458
				$result = $src_db->sql_query_limit('SELECT * FROM ' . $src_table_prefix . $tables[0], 1);
indvdum@0
   459
indvdum@0
   460
				if (!$result)
indvdum@0
   461
				{
indvdum@0
   462
					$prefixes = array();
indvdum@0
   463
indvdum@0
   464
					$tables_existing = get_tables($src_db);
indvdum@0
   465
					$tables_existing = array_map('strtolower', $tables_existing);
indvdum@0
   466
					foreach ($tables_existing as $table_name)
indvdum@0
   467
					{
indvdum@0
   468
						compare_table($tables, $table_name, $prefixes);
indvdum@0
   469
					}
indvdum@0
   470
					unset($tables_existing);
indvdum@0
   471
indvdum@0
   472
					foreach ($prefixes as $prefix => $count)
indvdum@0
   473
					{
indvdum@0
   474
						if ($count >= sizeof($tables))
indvdum@0
   475
						{
indvdum@0
   476
							$possible_prefix = $prefix;
indvdum@0
   477
							break;
indvdum@0
   478
						}
indvdum@0
   479
					}
indvdum@0
   480
indvdum@0
   481
					$msg = '';
indvdum@0
   482
					if (!empty($convertor_data['table_prefix']))
indvdum@0
   483
					{
indvdum@0
   484
						$msg .= sprintf($lang['DEFAULT_PREFIX_IS'], $convertor_data['forum_name'], $convertor_data['table_prefix']);
indvdum@0
   485
					}
indvdum@0
   486
indvdum@0
   487
					if (!empty($possible_prefix))
indvdum@0
   488
					{
indvdum@0
   489
						$msg .= '<br />';
indvdum@0
   490
						$msg .= ($possible_prefix == '*') ? $lang['BLANK_PREFIX_FOUND'] : sprintf($lang['PREFIX_FOUND'], $possible_prefix);
indvdum@0
   491
						$src_table_prefix = ($possible_prefix == '*') ? '' : $possible_prefix;
indvdum@0
   492
					}
indvdum@0
   493
indvdum@0
   494
					$error[] = $msg;
indvdum@0
   495
				}
indvdum@0
   496
				$src_db->sql_freeresult($result);
indvdum@0
   497
				$src_db->sql_return_on_error(false);
indvdum@0
   498
			}
indvdum@0
   499
indvdum@0
   500
			if (!sizeof($error))
indvdum@0
   501
			{
indvdum@0
   502
				// Save convertor Status
indvdum@0
   503
				set_config('convert_progress', serialize(array(
indvdum@0
   504
					'step'			=> '',
indvdum@0
   505
					'table_prefix'	=> $src_table_prefix,
indvdum@0
   506
					'tag'			=> $convertor_tag,
indvdum@0
   507
				)), true);
indvdum@0
   508
				set_config('convert_db_server', serialize(array(
indvdum@0
   509
					'dbms'			=> $src_dbms,
indvdum@0
   510
					'dbhost'		=> $src_dbhost,
indvdum@0
   511
					'dbport'		=> $src_dbport,
indvdum@0
   512
					'dbname'		=> $src_dbname,
indvdum@0
   513
				)), true);
indvdum@0
   514
				set_config('convert_db_user', serialize(array(
indvdum@0
   515
					'dbuser'		=> $src_dbuser,
indvdum@0
   516
					'dbpasswd'		=> $src_dbpasswd,
indvdum@0
   517
				)), true);
indvdum@0
   518
indvdum@0
   519
				// Save options
indvdum@0
   520
				set_config('convert_options', serialize(array('forum_path' => './../' . $forum_path, 'refresh' => $refresh)), true);
indvdum@0
   521
indvdum@0
   522
				$template->assign_block_vars('checks', array(
indvdum@0
   523
					'TITLE'		=> $lang['VERIFY_OPTIONS'],
indvdum@0
   524
					'RESULT'	=> $lang['CONVERT_SETTINGS_VERIFIED'],
indvdum@0
   525
				));
indvdum@0
   526
indvdum@0
   527
				$template->assign_vars(array(
indvdum@0
   528
					'L_SUBMIT'	=> $lang['BEGIN_CONVERT'],
indvdum@0
   529
//					'S_HIDDEN'	=> $s_hidden_fields,
indvdum@0
   530
					'U_ACTION'	=> $this->p_master->module_url . "?mode={$this->mode}&amp;sub=in_progress&amp;tag=$convertor_tag&amp;language=$language",
indvdum@0
   531
				));
indvdum@0
   532
indvdum@0
   533
				return;
indvdum@0
   534
			}
indvdum@0
   535
			else
indvdum@0
   536
			{
indvdum@0
   537
				$template->assign_block_vars('checks', array(
indvdum@0
   538
					'TITLE'		=> $lang['VERIFY_OPTIONS'],
indvdum@0
   539
					'RESULT'	=> '<b style="color:red">' . implode('<br />', $error) . '</b>',
indvdum@0
   540
				));
indvdum@0
   541
			}
indvdum@0
   542
		} // end submit
indvdum@0
   543
indvdum@0
   544
		foreach ($this->convert_options as $config_key => $vars)
indvdum@0
   545
		{
indvdum@0
   546
			if (!is_array($vars) && strpos($config_key, 'legend') === false)
indvdum@0
   547
			{
indvdum@0
   548
				continue;
indvdum@0
   549
			}
indvdum@0
   550
indvdum@0
   551
			if (strpos($config_key, 'legend') !== false)
indvdum@0
   552
			{
indvdum@0
   553
				$template->assign_block_vars('options', array(
indvdum@0
   554
					'S_LEGEND'		=> true,
indvdum@0
   555
					'LEGEND'		=> $lang[$vars])
indvdum@0
   556
				);
indvdum@0
   557
indvdum@0
   558
				continue;
indvdum@0
   559
			}
indvdum@0
   560
indvdum@0
   561
			$options = isset($vars['options']) ? $vars['options'] : '';
indvdum@0
   562
indvdum@0
   563
			$template->assign_block_vars('options', array(
indvdum@0
   564
				'KEY'			=> $config_key,
indvdum@0
   565
				'TITLE'			=> $lang[$vars['lang']],
indvdum@0
   566
				'S_EXPLAIN'		=> $vars['explain'],
indvdum@0
   567
				'S_LEGEND'		=> false,
indvdum@0
   568
				'TITLE_EXPLAIN'	=> ($vars['explain']) ? $lang[$vars['lang'] . '_EXPLAIN'] : '',
indvdum@0
   569
				'CONTENT'		=> $this->p_master->input_field($config_key, $vars['type'], $$config_key, $options),
indvdum@0
   570
				)
indvdum@0
   571
			);
indvdum@0
   572
		}
indvdum@0
   573
indvdum@0
   574
		$template->assign_vars(array(
indvdum@0
   575
			'TITLE'		=> $lang['STAGE_SETTINGS'],
indvdum@0
   576
			'BODY'		=> $lang['CONV_OPTIONS_BODY'],
indvdum@0
   577
			'L_SUBMIT'	=> $lang['BEGIN_CONVERT'],
indvdum@0
   578
			'U_ACTION'	=> $this->p_master->module_url . "?mode={$this->mode}&amp;sub=settings&amp;tag=$convertor_tag&amp;language=$language",
indvdum@0
   579
		));
indvdum@0
   580
	}
indvdum@0
   581
indvdum@0
   582
	/**
indvdum@0
   583
	* The function which does the actual work (or dispatches it to the relevant places)
indvdum@0
   584
	*/
indvdum@0
   585
	function convert_data($sub)
indvdum@0
   586
	{
indvdum@0
   587
		global $template, $user, $phpbb_root_path, $phpEx, $db, $lang, $config, $cache;
indvdum@0
   588
		global $convert, $convert_row, $message_parser, $skip_rows, $language;
indvdum@0
   589
indvdum@0
   590
		require($phpbb_root_path . 'config.' . $phpEx);
indvdum@0
   591
		require($phpbb_root_path . 'includes/constants.' . $phpEx);
indvdum@0
   592
		require($phpbb_root_path . 'includes/db/' . $dbms . '.' . $phpEx);
indvdum@0
   593
		require($phpbb_root_path . 'includes/functions_convert.' . $phpEx);
indvdum@0
   594
indvdum@0
   595
		$db = new $sql_db();
indvdum@0
   596
		$db->sql_connect($dbhost, $dbuser, $dbpasswd, $dbname, $dbport, false, true);
indvdum@0
   597
		unset($dbpasswd);
indvdum@0
   598
indvdum@0
   599
		$sql = 'SELECT *
indvdum@0
   600
			FROM ' . CONFIG_TABLE;
indvdum@0
   601
		$result = $db->sql_query($sql);
indvdum@0
   602
indvdum@0
   603
		$config = array();
indvdum@0
   604
		while ($row = $db->sql_fetchrow($result))
indvdum@0
   605
		{
indvdum@0
   606
			$config[$row['config_name']] = $row['config_value'];
indvdum@0
   607
		}
indvdum@0
   608
		$db->sql_freeresult($result);
indvdum@0
   609
indvdum@0
   610
		// Override a couple of config variables for the duration
indvdum@0
   611
		$config['max_quote_depth'] = 0;
indvdum@0
   612
indvdum@0
   613
		// @todo Need to confirm that max post length in source is <= max post length in destination or there may be interesting formatting issues
indvdum@0
   614
		$config['max_post_chars'] = $config['min_post_chars'] = 0;
indvdum@0
   615
indvdum@0
   616
		// Set up a user as well. We _should_ have enough of a database here at this point to do this
indvdum@0
   617
		// and it helps for any core code we call
indvdum@0
   618
		$user->session_begin();
indvdum@0
   619
		$user->page = $user->extract_current_page($phpbb_root_path);
indvdum@0
   620
indvdum@0
   621
		// This is a little bit of a fudge, but it allows the language entries to be available to the
indvdum@0
   622
		// core code without us loading them again
indvdum@0
   623
		$user->lang = &$lang;
indvdum@0
   624
indvdum@0
   625
		$this->page_title = $user->lang['STAGE_IN_PROGRESS'];
indvdum@0
   626
indvdum@0
   627
		$convert->options = array();
indvdum@0
   628
		if (isset($config['convert_progress']))
indvdum@0
   629
		{
indvdum@0
   630
			$convert->options = unserialize($config['convert_progress']);
indvdum@0
   631
			$convert->options = array_merge($convert->options, unserialize($config['convert_db_server']), unserialize($config['convert_db_user']), unserialize($config['convert_options']));
indvdum@0
   632
		}
indvdum@0
   633
indvdum@0
   634
		// This information should have already been checked once, but do it again for safety
indvdum@0
   635
		if (empty($convert->options) || empty($convert->options['tag']) ||
indvdum@0
   636
			!isset($convert->options['dbms']) ||
indvdum@0
   637
			!isset($convert->options['dbhost']) ||
indvdum@0
   638
			!isset($convert->options['dbport']) ||
indvdum@0
   639
			!isset($convert->options['dbuser']) ||
indvdum@0
   640
			!isset($convert->options['dbpasswd']) ||
indvdum@0
   641
			!isset($convert->options['dbname']) ||
indvdum@0
   642
			!isset($convert->options['table_prefix']))
indvdum@0
   643
		{
indvdum@0
   644
			$this->p_master->error($user->lang['NO_CONVERT_SPECIFIED'], __LINE__, __FILE__);
indvdum@0
   645
		}
indvdum@0
   646
indvdum@0
   647
		// Make some short variables accessible, for easier referencing
indvdum@0
   648
		$convert->convertor_tag = basename($convert->options['tag']);
indvdum@0
   649
		$convert->src_dbms = $convert->options['dbms'];
indvdum@0
   650
		$convert->src_dbhost = $convert->options['dbhost'];
indvdum@0
   651
		$convert->src_dbport = $convert->options['dbport'];
indvdum@0
   652
		$convert->src_dbuser = $convert->options['dbuser'];
indvdum@0
   653
		$convert->src_dbpasswd = $convert->options['dbpasswd'];
indvdum@0
   654
		$convert->src_dbname = $convert->options['dbname'];
indvdum@0
   655
		$convert->src_table_prefix = $convert->options['table_prefix'];
indvdum@0
   656
indvdum@0
   657
		// initiate database connection to old db if old and new db differ
indvdum@0
   658
		global $src_db, $same_db;
indvdum@0
   659
		$src_db = $same_db = null;
indvdum@0
   660
		if ($convert->src_dbms != $dbms || $convert->src_dbhost != $dbhost || $convert->src_dbport != $dbport || $convert->src_dbname != $dbname || $convert->src_dbuser != $dbuser)
indvdum@0
   661
		{
indvdum@0
   662
			if ($convert->src_dbms != $dbms)
indvdum@0
   663
			{
indvdum@0
   664
				require($phpbb_root_path . 'includes/db/' . $convert->src_dbms . '.' . $phpEx);
indvdum@0
   665
			}
indvdum@0
   666
			$sql_db = 'dbal_' . $convert->src_dbms;
indvdum@0
   667
			$src_db = new $sql_db();
indvdum@0
   668
			$src_db->sql_connect($convert->src_dbhost, $convert->src_dbuser, htmlspecialchars_decode($convert->src_dbpasswd), $convert->src_dbname, $convert->src_dbport, false, true);
indvdum@0
   669
			$same_db = false;
indvdum@0
   670
		}
indvdum@0
   671
		else
indvdum@0
   672
		{
indvdum@0
   673
			$src_db = $db;
indvdum@0
   674
			$same_db = true;
indvdum@0
   675
		}
indvdum@0
   676
indvdum@0
   677
		$convert->mysql_convert = false;
indvdum@0
   678
		switch ($src_db->sql_layer)
indvdum@0
   679
		{
indvdum@0
   680
			case 'sqlite':
indvdum@0
   681
			case 'firebird':
indvdum@0
   682
				$convert->src_truncate_statement = 'DELETE FROM ';
indvdum@0
   683
			break;
indvdum@0
   684
indvdum@0
   685
			// Thanks MySQL, for silently converting...
indvdum@0
   686
			case 'mysql':
indvdum@0
   687
			case 'mysql4':
indvdum@0
   688
				if (version_compare($src_db->sql_server_info(true, false), '4.1.3', '>='))
indvdum@0
   689
				{
indvdum@0
   690
					$convert->mysql_convert = true;
indvdum@0
   691
				}
indvdum@0
   692
				$convert->src_truncate_statement = 'TRUNCATE TABLE ';
indvdum@0
   693
			break;
indvdum@0
   694
indvdum@0
   695
			case 'mysqli':
indvdum@0
   696
				$convert->mysql_convert = true;
indvdum@0
   697
				$convert->src_truncate_statement = 'TRUNCATE TABLE ';
indvdum@0
   698
			break;
indvdum@0
   699
indvdum@0
   700
			default:
indvdum@0
   701
				$convert->src_truncate_statement = 'TRUNCATE TABLE ';
indvdum@0
   702
			break;
indvdum@0
   703
		}
indvdum@0
   704
indvdum@0
   705
		if ($convert->mysql_convert && !$same_db)
indvdum@0
   706
		{
indvdum@0
   707
			$src_db->sql_query("SET NAMES 'binary'");
indvdum@0
   708
		}
indvdum@0
   709
indvdum@0
   710
		switch ($db->sql_layer)
indvdum@0
   711
		{
indvdum@0
   712
			case 'sqlite':
indvdum@0
   713
			case 'firebird':
indvdum@0
   714
				$convert->truncate_statement = 'DELETE FROM ';
indvdum@0
   715
			break;
indvdum@0
   716
indvdum@0
   717
			default:
indvdum@0
   718
				$convert->truncate_statement = 'TRUNCATE TABLE ';
indvdum@0
   719
			break;
indvdum@0
   720
		}
indvdum@0
   721
indvdum@0
   722
		$get_info = false;
indvdum@0
   723
indvdum@0
   724
		// check security implications of direct inclusion
indvdum@0
   725
		if (!file_exists('./convertors/convert_' . $convert->convertor_tag . '.' . $phpEx))
indvdum@0
   726
		{
indvdum@0
   727
			$this->p_master->error($user->lang['CONVERT_NOT_EXIST'], __LINE__, __FILE__);
indvdum@0
   728
		}
indvdum@0
   729
indvdum@0
   730
		if (file_exists('./convertors/functions_' . $convert->convertor_tag . '.' . $phpEx))
indvdum@0
   731
		{
indvdum@0
   732
			include('./convertors/functions_' . $convert->convertor_tag . '.' . $phpEx);
indvdum@0
   733
		}
indvdum@0
   734
indvdum@0
   735
		$get_info = true;
indvdum@0
   736
		include('./convertors/convert_' . $convert->convertor_tag . '.' . $phpEx);
indvdum@0
   737
indvdum@0
   738
		// Map some variables...
indvdum@0
   739
		$convert->convertor_data = $convertor_data;
indvdum@0
   740
		$convert->tables = $tables;
indvdum@0
   741
		$convert->config_schema = $config_schema;
indvdum@0
   742
indvdum@0
   743
		// Now include the real data
indvdum@0
   744
		$get_info = false;
indvdum@0
   745
		include('./convertors/convert_' . $convert->convertor_tag . '.' . $phpEx);
indvdum@0
   746
indvdum@0
   747
		$convert->convertor_data = $convertor_data;
indvdum@0
   748
		$convert->tables = $tables;
indvdum@0
   749
		$convert->config_schema = $config_schema;
indvdum@0
   750
		$convert->convertor = $convertor;
indvdum@0
   751
indvdum@0
   752
		// The test_file is a file that should be present in the location of the old board.
indvdum@0
   753
		if (!file_exists($convert->options['forum_path'] . '/' . $test_file))
indvdum@0
   754
		{
indvdum@0
   755
			$this->p_master->error(sprintf($user->lang['COULD_NOT_FIND_PATH'], $convert->options['forum_path']), __LINE__, __FILE__);
indvdum@0
   756
		}
indvdum@0
   757
indvdum@0
   758
		$search_type = basename(trim($config['search_type']));
indvdum@0
   759
indvdum@0
   760
		// For conversions we are a bit less strict and set to a search backend we know exist...
indvdum@0
   761
		if (!file_exists($phpbb_root_path . 'includes/search/' . $search_type . '.' . $phpEx))
indvdum@0
   762
		{
indvdum@0
   763
			$search_type = 'fulltext_native';
indvdum@0
   764
			set_config('search_type', $search_type);
indvdum@0
   765
		}
indvdum@0
   766
indvdum@0
   767
		if (!file_exists($phpbb_root_path . 'includes/search/' . $search_type . '.' . $phpEx))
indvdum@0
   768
		{
indvdum@0
   769
			trigger_error('NO_SUCH_SEARCH_MODULE');
indvdum@0
   770
		}
indvdum@0
   771
indvdum@0
   772
		require($phpbb_root_path . 'includes/search/' . $search_type . '.' . $phpEx);
indvdum@0
   773
indvdum@0
   774
		$error = false;
indvdum@0
   775
		$convert->fulltext_search = new $search_type($error);
indvdum@0
   776
indvdum@0
   777
		if ($error)
indvdum@0
   778
		{
indvdum@0
   779
			trigger_error($error);
indvdum@0
   780
		}
indvdum@0
   781
indvdum@0
   782
		include($phpbb_root_path . 'includes/message_parser.' . $phpEx);
indvdum@0
   783
		$message_parser = new parse_message();
indvdum@0
   784
indvdum@0
   785
		$jump = request_var('jump', 0);
indvdum@0
   786
		$final_jump = request_var('final_jump', 0);
indvdum@0
   787
		$sync_batch = request_var('sync_batch', -1);
indvdum@0
   788
		$last_statement = request_var('last', 0);
indvdum@0
   789
indvdum@0
   790
		// We are running sync...
indvdum@0
   791
		if ($sync_batch >= 0)
indvdum@0
   792
		{
indvdum@0
   793
			$this->sync_forums($sync_batch);
indvdum@0
   794
			return;
indvdum@0
   795
		}
indvdum@0
   796
indvdum@0
   797
		if ($jump)
indvdum@0
   798
		{
indvdum@0
   799
			$this->jump($jump, $last_statement);
indvdum@0
   800
			return;
indvdum@0
   801
		}
indvdum@0
   802
indvdum@0
   803
		if ($final_jump)
indvdum@0
   804
		{
indvdum@0
   805
			$this->final_jump($final_jump);
indvdum@0
   806
			return;
indvdum@0
   807
		}
indvdum@0
   808
indvdum@0
   809
		$current_table = request_var('current_table', 0);
indvdum@0
   810
		$old_current_table = min(-1, $current_table - 1);
indvdum@0
   811
		$skip_rows = request_var('skip_rows', 0);
indvdum@0
   812
indvdum@0
   813
		if (!$current_table && !$skip_rows)
indvdum@0
   814
		{
indvdum@0
   815
			if (empty($_REQUEST['confirm']))
indvdum@0
   816
			{
indvdum@0
   817
				// If avatars / ranks / smilies folders are specified make sure they are writable
indvdum@0
   818
				$bad_folders = array();
indvdum@0
   819
indvdum@0
   820
				$local_paths = array(
indvdum@0
   821
					'avatar_path'			=> path($config['avatar_path']),
indvdum@0
   822
					'avatar_gallery_path'	=> path($config['avatar_gallery_path']),
indvdum@0
   823
					'icons_path'			=> path($config['icons_path']),
indvdum@0
   824
					'ranks_path'			=> path($config['ranks_path']),
indvdum@0
   825
					'smilies_path'			=> path($config['smilies_path'])
indvdum@0
   826
				);
indvdum@0
   827
indvdum@0
   828
				foreach ($local_paths as $folder => $local_path)
indvdum@0
   829
				{
indvdum@0
   830
					if (isset($convert->convertor[$folder]))
indvdum@0
   831
					{
indvdum@0
   832
						if (empty($convert->convertor['test_file']))
indvdum@0
   833
						{
indvdum@0
   834
							// test_file is mandantory at the moment so this should never be reached, but just in case...
indvdum@0
   835
							$this->p_master->error($user->lang['DEV_NO_TEST_FILE'], __LINE__, __FILE__);
indvdum@0
   836
						}
indvdum@0
   837
indvdum@0
   838
						if (!$local_path || !phpbb_is_writable($phpbb_root_path . $local_path))
indvdum@0
   839
						{
indvdum@0
   840
							if (!$local_path)
indvdum@0
   841
							{
indvdum@0
   842
								$bad_folders[] = sprintf($user->lang['CONFIG_PHPBB_EMPTY'], $folder);
indvdum@0
   843
							}
indvdum@0
   844
							else
indvdum@0
   845
							{
indvdum@0
   846
								$bad_folders[] = $local_path;
indvdum@0
   847
							}
indvdum@0
   848
						}
indvdum@0
   849
					}
indvdum@0
   850
				}
indvdum@0
   851
indvdum@0
   852
				if (sizeof($bad_folders))
indvdum@0
   853
				{
indvdum@0
   854
					$msg = (sizeof($bad_folders) == 1) ? $user->lang['MAKE_FOLDER_WRITABLE'] : $user->lang['MAKE_FOLDERS_WRITABLE'];
indvdum@0
   855
					sort($bad_folders);
indvdum@0
   856
					$this->p_master->error(sprintf($msg, implode('<br />', $bad_folders)), __LINE__, __FILE__, true);
indvdum@0
   857
indvdum@0
   858
					$template->assign_vars(array(
indvdum@0
   859
						'L_SUBMIT'	=> $user->lang['INSTALL_TEST'],
indvdum@0
   860
						'U_ACTION'	=> $this->p_master->module_url . "?mode={$this->mode}&amp;sub=in_progress&amp;tag={$convert->convertor_tag}&amp;language=$language",
indvdum@0
   861
					));
indvdum@0
   862
					return;
indvdum@0
   863
				}
indvdum@0
   864
indvdum@0
   865
				// Grab all the tables used in convertor
indvdum@0
   866
				$missing_tables = $tables_list = $aliases = array();
indvdum@0
   867
indvdum@0
   868
				foreach ($convert->convertor['schema'] as $schema)
indvdum@0
   869
				{
indvdum@0
   870
					// Skip those not used (because of addons/plugins not detected)
indvdum@0
   871
					if (!$schema['target'])
indvdum@0
   872
					{
indvdum@0
   873
						continue;
indvdum@0
   874
					}
indvdum@0
   875
indvdum@0
   876
					foreach ($schema as $key => $val)
indvdum@0
   877
					{
indvdum@0
   878
						// we're dealing with an array like:
indvdum@0
   879
						// array('forum_status',			'forums.forum_status',				'is_item_locked')
indvdum@0
   880
						if (is_int($key) && !empty($val[1]))
indvdum@0
   881
						{
indvdum@0
   882
							$temp_data = $val[1];
indvdum@0
   883
							if (!is_array($temp_data))
indvdum@0
   884
							{
indvdum@0
   885
								$temp_data = array($temp_data);
indvdum@0
   886
							}
indvdum@0
   887
indvdum@0
   888
							foreach ($temp_data as $val)
indvdum@0
   889
							{
indvdum@0
   890
								if (preg_match('/([a-z0-9_]+)\.([a-z0-9_]+)\)* ?A?S? ?([a-z0-9_]*?)\.?([a-z0-9_]*)$/i', $val, $m))
indvdum@0
   891
								{
indvdum@0
   892
									$table = $convert->src_table_prefix . $m[1];
indvdum@0
   893
									$tables_list[$table] = $table;
indvdum@0
   894
indvdum@0
   895
									if (!empty($m[3]))
indvdum@0
   896
									{
indvdum@0
   897
										$aliases[] = $convert->src_table_prefix . $m[3];
indvdum@0
   898
									}
indvdum@0
   899
								}
indvdum@0
   900
							}
indvdum@0
   901
						}
indvdum@0
   902
						// 'left_join'		=> 'topics LEFT JOIN vote_desc ON topics.topic_id = vote_desc.topic_id AND topics.topic_vote = 1'
indvdum@0
   903
						else if ($key == 'left_join')
indvdum@0
   904
						{
indvdum@0
   905
							// Convert the value if it wasn't an array already.
indvdum@0
   906
							if (!is_array($val))
indvdum@0
   907
							{
indvdum@0
   908
								$val = array($val);
indvdum@0
   909
							}
indvdum@0
   910
indvdum@0
   911
							for ($j = 0; $j < sizeof($val); ++$j)
indvdum@0
   912
							{
indvdum@0
   913
								if (preg_match('/LEFT JOIN ([a-z0-9_]+) AS ([a-z0-9_]+)/i', $val[$j], $m))
indvdum@0
   914
								{
indvdum@0
   915
									$table = $convert->src_table_prefix . $m[1];
indvdum@0
   916
									$tables_list[$table] = $table;
indvdum@0
   917
indvdum@0
   918
									if (!empty($m[2]))
indvdum@0
   919
									{
indvdum@0
   920
										$aliases[] = $convert->src_table_prefix . $m[2];
indvdum@0
   921
									}
indvdum@0
   922
								}
indvdum@0
   923
							}
indvdum@0
   924
						}
indvdum@0
   925
					}
indvdum@0
   926
				}
indvdum@0
   927
indvdum@0
   928
				// Remove aliased tables from $tables_list
indvdum@0
   929
				foreach ($aliases as $alias)
indvdum@0
   930
				{
indvdum@0
   931
					unset($tables_list[$alias]);
indvdum@0
   932
				}
indvdum@0
   933
indvdum@0
   934
				// Check if the tables that we need exist
indvdum@0
   935
				$src_db->sql_return_on_error(true);
indvdum@0
   936
				foreach ($tables_list as $table => $null)
indvdum@0
   937
				{
indvdum@0
   938
					$sql = 'SELECT 1 FROM ' . $table;
indvdum@0
   939
					$_result = $src_db->sql_query_limit($sql, 1);
indvdum@0
   940
indvdum@0
   941
					if (!$_result)
indvdum@0
   942
					{
indvdum@0
   943
						$missing_tables[] = $table;
indvdum@0
   944
					}
indvdum@0
   945
					$src_db->sql_freeresult($_result);
indvdum@0
   946
				}
indvdum@0
   947
				$src_db->sql_return_on_error(false);
indvdum@0
   948
indvdum@0
   949
				// Throw an error if some tables are missing
indvdum@0
   950
				// 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
indvdum@0
   951
indvdum@0
   952
				if (sizeof($missing_tables) == sizeof($tables_list))
indvdum@0
   953
				{
indvdum@0
   954
					$this->p_master->error($user->lang['NO_TABLES_FOUND'] . ' ' . $user->lang['CHECK_TABLE_PREFIX'], __LINE__, __FILE__);
indvdum@0
   955
				}
indvdum@0
   956
				else if (sizeof($missing_tables))
indvdum@0
   957
				{
indvdum@0
   958
					$this->p_master->error(sprintf($user->lang['TABLES_MISSING'], implode(', ', $missing_tables)) . '<br /><br />' . $user->lang['CHECK_TABLE_PREFIX'], __LINE__, __FILE__);
indvdum@0
   959
				}
indvdum@0
   960
indvdum@0
   961
				$url = $this->save_convert_progress('&amp;confirm=1');
indvdum@0
   962
				$msg = $user->lang['PRE_CONVERT_COMPLETE'];
indvdum@0
   963
indvdum@0
   964
				if ($convert->convertor_data['author_notes'])
indvdum@0
   965
				{
indvdum@0
   966
					$msg .= '</p><p>' . sprintf($user->lang['AUTHOR_NOTES'], $convert->convertor_data['author_notes']);
indvdum@0
   967
				}
indvdum@0
   968
indvdum@0
   969
				$template->assign_vars(array(
indvdum@0
   970
					'L_SUBMIT'		=> $user->lang['CONTINUE_CONVERT'],
indvdum@0
   971
					'L_MESSAGE'		=> $msg,
indvdum@0
   972
					'U_ACTION'		=> $url,
indvdum@0
   973
				));
indvdum@0
   974
indvdum@0
   975
				return;
indvdum@0
   976
			} // if (empty($_REQUEST['confirm']))
indvdum@0
   977
indvdum@0
   978
			$template->assign_block_vars('checks', array(
indvdum@0
   979
				'S_LEGEND'		=> true,
indvdum@0
   980
				'LEGEND'		=> $user->lang['STARTING_CONVERT'],
indvdum@0
   981
			));
indvdum@0
   982
indvdum@0
   983
			// Convert the config table and load the settings of the old board
indvdum@0
   984
			if (!empty($convert->config_schema))
indvdum@0
   985
			{
indvdum@0
   986
				restore_config($convert->config_schema);
indvdum@0
   987
indvdum@0
   988
				// Override a couple of config variables for the duration
indvdum@0
   989
				$config['max_quote_depth'] = 0;
indvdum@0
   990
indvdum@0
   991
				// @todo Need to confirm that max post length in source is <= max post length in destination or there may be interesting formatting issues
indvdum@0
   992
				$config['max_post_chars'] = $config['min_post_chars'] = 0;
indvdum@0
   993
			}
indvdum@0
   994
indvdum@0
   995
			$template->assign_block_vars('checks', array(
indvdum@0
   996
				'TITLE'		=> $user->lang['CONFIG_CONVERT'],
indvdum@0
   997
				'RESULT'	=> $user->lang['DONE'],
indvdum@0
   998
			));
indvdum@0
   999
indvdum@0
  1000
			// Now process queries and execute functions that have to be executed prior to the conversion
indvdum@0
  1001
			if (!empty($convert->convertor['execute_first']))
indvdum@0
  1002
			{
indvdum@0
  1003
				eval($convert->convertor['execute_first']);
indvdum@0
  1004
			}
indvdum@0
  1005
indvdum@0
  1006
			if (!empty($convert->convertor['query_first']))
indvdum@0
  1007
			{
indvdum@0
  1008
				if (!is_array($convert->convertor['query_first']))
indvdum@0
  1009
				{
indvdum@0
  1010
					$convert->convertor['query_first'] = array('target', array($convert->convertor['query_first']));
indvdum@0
  1011
				}
indvdum@0
  1012
				else if (!is_array($convert->convertor['query_first'][0]))
indvdum@0
  1013
				{
indvdum@0
  1014
					$convert->convertor['query_first'] = array(array($convert->convertor['query_first'][0], $convert->convertor['query_first'][1]));
indvdum@0
  1015
				}
indvdum@0
  1016
indvdum@0
  1017
				foreach ($convert->convertor['query_first'] as $query_first)
indvdum@0
  1018
				{
indvdum@0
  1019
					if ($query_first[0] == 'src')
indvdum@0
  1020
					{
indvdum@0
  1021
						if ($convert->mysql_convert && $same_db)
indvdum@0
  1022
						{
indvdum@0
  1023
							$src_db->sql_query("SET NAMES 'binary'");
indvdum@0
  1024
						}
indvdum@0
  1025
indvdum@0
  1026
						$src_db->sql_query($query_first[1]);
indvdum@0
  1027
indvdum@0
  1028
						if ($convert->mysql_convert && $same_db)
indvdum@0
  1029
						{
indvdum@0
  1030
							$src_db->sql_query("SET NAMES 'utf8'");
indvdum@0
  1031
						}
indvdum@0
  1032
					}
indvdum@0
  1033
					else
indvdum@0
  1034
					{
indvdum@0
  1035
						$db->sql_query($query_first[1]);
indvdum@0
  1036
					}
indvdum@0
  1037
				}
indvdum@0
  1038
			}
indvdum@0
  1039
indvdum@0
  1040
			$template->assign_block_vars('checks', array(
indvdum@0
  1041
				'TITLE'		=> $user->lang['PREPROCESS_STEP'],
indvdum@0
  1042
				'RESULT'	=> $user->lang['DONE'],
indvdum@0
  1043
			));
indvdum@0
  1044
		} // if (!$current_table && !$skip_rows)
indvdum@0
  1045
indvdum@0
  1046
		$template->assign_block_vars('checks', array(
indvdum@0
  1047
			'S_LEGEND'		=> true,
indvdum@0
  1048
			'LEGEND'		=> $user->lang['FILLING_TABLES'],
indvdum@0
  1049
		));
indvdum@0
  1050
indvdum@0
  1051
		// This loop takes one target table and processes it
indvdum@0
  1052
		while ($current_table < sizeof($convert->convertor['schema']))
indvdum@0
  1053
		{
indvdum@0
  1054
			$schema = $convert->convertor['schema'][$current_table];
indvdum@0
  1055
indvdum@0
  1056
			// The target table isn't set, this can be because a module (for example the attachement mod) is taking care of this.
indvdum@0
  1057
			if (empty($schema['target']))
indvdum@0
  1058
			{
indvdum@0
  1059
				$current_table++;
indvdum@0
  1060
				continue;
indvdum@0
  1061
			}
indvdum@0
  1062
indvdum@0
  1063
			$template->assign_block_vars('checks', array(
indvdum@0
  1064
				'TITLE'	=> sprintf($user->lang['FILLING_TABLE'], $schema['target']),
indvdum@0
  1065
			));
indvdum@0
  1066
indvdum@0
  1067
			// This is only the case when we first start working on the tables.
indvdum@0
  1068
			if (!$skip_rows)
indvdum@0
  1069
			{
indvdum@0
  1070
				// process execute_first and query_first for this table...
indvdum@0
  1071
				if (!empty($schema['execute_first']))
indvdum@0
  1072
				{
indvdum@0
  1073
					eval($schema['execute_first']);
indvdum@0
  1074
				}
indvdum@0
  1075
indvdum@0
  1076
				if (!empty($schema['query_first']))
indvdum@0
  1077
				{
indvdum@0
  1078
					if (!is_array($schema['query_first']))
indvdum@0
  1079
					{
indvdum@0
  1080
						$schema['query_first'] = array('target', array($schema['query_first']));
indvdum@0
  1081
					}
indvdum@0
  1082
					else if (!is_array($schema['query_first'][0]))
indvdum@0
  1083
					{
indvdum@0
  1084
						$schema['query_first'] = array(array($schema['query_first'][0], $schema['query_first'][1]));
indvdum@0
  1085
					}
indvdum@0
  1086
indvdum@0
  1087
					foreach ($schema['query_first'] as $query_first)
indvdum@0
  1088
					{
indvdum@0
  1089
						if ($query_first[0] == 'src')
indvdum@0
  1090
						{
indvdum@0
  1091
							if ($convert->mysql_convert && $same_db)
indvdum@0
  1092
							{
indvdum@0
  1093
								$src_db->sql_query("SET NAMES 'binary'");
indvdum@0
  1094
							}
indvdum@0
  1095
							$src_db->sql_query($query_first[1]);
indvdum@0
  1096
							if ($convert->mysql_convert && $same_db)
indvdum@0
  1097
							{
indvdum@0
  1098
								$src_db->sql_query("SET NAMES 'utf8'");
indvdum@0
  1099
							}
indvdum@0
  1100
						}
indvdum@0
  1101
						else
indvdum@0
  1102
						{
indvdum@0
  1103
							$db->sql_query($query_first[1]);
indvdum@0
  1104
						}
indvdum@0
  1105
					}
indvdum@0
  1106
				}
indvdum@0
  1107
indvdum@0
  1108
				if (!empty($schema['autoincrement']))
indvdum@0
  1109
				{
indvdum@0
  1110
					switch ($db->sql_layer)
indvdum@0
  1111
					{
indvdum@0
  1112
						case 'postgres':
indvdum@0
  1113
							$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'] . '));');
indvdum@0
  1114
						break;
indvdum@0
  1115
indvdum@0
  1116
						case 'oracle':
indvdum@0
  1117
							$result = $db->sql_query('SELECT MAX(' . $schema['autoincrement'] . ') as max_id FROM ' . $schema['target']);
indvdum@0
  1118
							$row = $db->sql_fetchrow($result);
indvdum@0
  1119
							$db->sql_freeresult($result);
indvdum@0
  1120
indvdum@0
  1121
							$largest_id = (int) $row['max_id'];
indvdum@0
  1122
indvdum@0
  1123
							if ($largest_id)
indvdum@0
  1124
							{
indvdum@0
  1125
								$db->sql_query('DROP SEQUENCE ' . $schema['target'] . '_seq');
indvdum@0
  1126
								$db->sql_query('CREATE SEQUENCE ' . $schema['target'] . '_seq START WITH ' . ($largest_id + 1));
indvdum@0
  1127
							}
indvdum@0
  1128
						break;
indvdum@0
  1129
					}
indvdum@0
  1130
				}
indvdum@0
  1131
			}
indvdum@0
  1132
indvdum@0
  1133
			// Process execute_always for this table
indvdum@0
  1134
			// This is for code which needs to be executed on every pass of this table if
indvdum@0
  1135
			// it gets split because of time restrictions
indvdum@0
  1136
			if (!empty($schema['execute_always']))
indvdum@0
  1137
			{
indvdum@0
  1138
				eval($schema['execute_always']);
indvdum@0
  1139
			}
indvdum@0
  1140
indvdum@0
  1141
			//
indvdum@0
  1142
			// Set up some variables
indvdum@0
  1143
			//
indvdum@0
  1144
			// $waiting_rows	holds rows for multirows insertion (MySQL only)
indvdum@0
  1145
			// $src_tables		holds unique tables with aliases to select from
indvdum@0
  1146
			// $src_fields		will quickly refer source fields (or aliases) corresponding to the current index
indvdum@0
  1147
			// $select_fields	holds the names of the fields to retrieve
indvdum@0
  1148
			//
indvdum@0
  1149
indvdum@0
  1150
			$sql_data = array(
indvdum@0
  1151
				'source_fields'		=> array(),
indvdum@0
  1152
				'target_fields'		=> array(),
indvdum@0
  1153
				'source_tables'		=> array(),
indvdum@0
  1154
				'select_fields'		=> array(),
indvdum@0
  1155
			);
indvdum@0
  1156
indvdum@0
  1157
			// This statement is building the keys for later insertion.
indvdum@0
  1158
			$insert_query = $this->build_insert_query($schema, $sql_data, $current_table);
indvdum@0
  1159
indvdum@0
  1160
			// If no source table is affected, we skip the table
indvdum@0
  1161
			if (empty($sql_data['source_tables']))
indvdum@0
  1162
			{
indvdum@0
  1163
				$skip_rows = 0;
indvdum@0
  1164
				$current_table++;
indvdum@0
  1165
				continue;
indvdum@0
  1166
			}
indvdum@0
  1167
indvdum@0
  1168
			$distinct = (!empty($schema['distinct'])) ? 'DISTINCT ' : '';
indvdum@0
  1169
indvdum@0
  1170
			$sql = 'SELECT ' . $distinct . implode(', ', $sql_data['select_fields']) . " \nFROM " . implode(', ', $sql_data['source_tables']);
indvdum@0
  1171
indvdum@0
  1172
			// Where
indvdum@0
  1173
			$sql .= (!empty($schema['where'])) ? "\nWHERE (" . $schema['where'] . ')' : '';
indvdum@0
  1174
indvdum@0
  1175
			// Group By
indvdum@0
  1176
			if (!empty($schema['group_by']))
indvdum@0
  1177
			{
indvdum@0
  1178
				$schema['group_by'] = array($schema['group_by']);
indvdum@0
  1179
				foreach ($sql_data['select_fields'] as $select)
indvdum@0
  1180
				{
indvdum@0
  1181
					$alias = strpos(strtolower($select), ' as ');
indvdum@0
  1182
					$select = ($alias) ? substr($select, 0, $alias) : $select;
indvdum@0
  1183
					if (!in_array($select, $schema['group_by']))
indvdum@0
  1184
					{
indvdum@0
  1185
						$schema['group_by'][] = $select;
indvdum@0
  1186
					}
indvdum@0
  1187
				}
indvdum@0
  1188
			}
indvdum@0
  1189
			$sql .= (!empty($schema['group_by'])) ? "\nGROUP BY " . implode(', ', $schema['group_by']) : '';
indvdum@0
  1190
indvdum@0
  1191
			// Having
indvdum@0
  1192
			$sql .= (!empty($schema['having'])) ? "\nHAVING " . $schema['having'] : '';
indvdum@0
  1193
indvdum@0
  1194
			// Order By
indvdum@0
  1195
			if (empty($schema['order_by']) && !empty($schema['primary']))
indvdum@0
  1196
			{
indvdum@0
  1197
				$schema['order_by'] = $schema['primary'];
indvdum@0
  1198
			}
indvdum@0
  1199
			$sql .= (!empty($schema['order_by'])) ? "\nORDER BY " . $schema['order_by'] : '';
indvdum@0
  1200
indvdum@0
  1201
			// Counting basically holds the amount of rows processed.
indvdum@0
  1202
			$counting = -1;
indvdum@0
  1203
			$batch_time = 0;
indvdum@0
  1204
indvdum@0
  1205
			while ($counting === -1 || ($counting >= $convert->batch_size && still_on_time()))
indvdum@0
  1206
			{
indvdum@0
  1207
				$old_current_table = $current_table;
indvdum@0
  1208
indvdum@0
  1209
				$rows = '';
indvdum@0
  1210
				$waiting_rows = array();
indvdum@0
  1211
indvdum@0
  1212
				if (!empty($batch_time))
indvdum@0
  1213
				{
indvdum@0
  1214
					$mtime = explode(' ', microtime());
indvdum@0
  1215
					$mtime = $mtime[0] + $mtime[1];
indvdum@0
  1216
					$rows = ceil($counting/($mtime - $batch_time)) . " rows/s ($counting rows) | ";
indvdum@0
  1217
				}
indvdum@0
  1218
indvdum@0
  1219
				$template->assign_block_vars('checks', array(
indvdum@0
  1220
					'TITLE'		=> "skip_rows = $skip_rows",
indvdum@0
  1221
					'RESULT'	=> $rows . ((defined('DEBUG_EXTRA') && function_exists('memory_get_usage')) ? ceil(memory_get_usage()/1024) . ' ' . $user->lang['KIB'] : ''),
indvdum@0
  1222
				));
indvdum@0
  1223
indvdum@0
  1224
				$mtime = explode(' ', microtime());
indvdum@0
  1225
				$batch_time = $mtime[0] + $mtime[1];
indvdum@0
  1226
indvdum@0
  1227
				if ($convert->mysql_convert && $same_db)
indvdum@0
  1228
				{
indvdum@0
  1229
					$src_db->sql_query("SET NAMES 'binary'");
indvdum@0
  1230
				}
indvdum@0
  1231
indvdum@0
  1232
				// Take skip rows into account and only fetch batch_size amount of rows
indvdum@0
  1233
				$___result = $src_db->sql_query_limit($sql, $convert->batch_size, $skip_rows);
indvdum@0
  1234
indvdum@0
  1235
				if ($convert->mysql_convert && $same_db)
indvdum@0
  1236
				{
indvdum@0
  1237
					$src_db->sql_query("SET NAMES 'utf8'");
indvdum@0
  1238
				}
indvdum@0
  1239
indvdum@0
  1240
				// This loop processes each row
indvdum@0
  1241
				$counting = 0;
indvdum@0
  1242
indvdum@0
  1243
				$convert->row = $convert_row = array();
indvdum@0
  1244
indvdum@0
  1245
				if (!empty($schema['autoincrement']))
indvdum@0
  1246
				{
indvdum@0
  1247
					switch ($db->sql_layer)
indvdum@0
  1248
					{
indvdum@0
  1249
						case 'mssql':
indvdum@0
  1250
						case 'mssql_odbc':
indvdum@0
  1251
						case 'mssqlnative':
indvdum@0
  1252
							$db->sql_query('SET IDENTITY_INSERT ' . $schema['target'] . ' ON');
indvdum@0
  1253
						break;
indvdum@0
  1254
					}
indvdum@0
  1255
				}
indvdum@0
  1256
indvdum@0
  1257
				// Now handle the rows until time is over or no more rows to process...
indvdum@0
  1258
				while ($counting === 0 || still_on_time())
indvdum@0
  1259
				{
indvdum@0
  1260
					$convert_row = $src_db->sql_fetchrow($___result);
indvdum@0
  1261
indvdum@0
  1262
					if (!$convert_row)
indvdum@0
  1263
					{
indvdum@0
  1264
						// move to the next batch or table
indvdum@0
  1265
						break;
indvdum@0
  1266
					}
indvdum@0
  1267
indvdum@0
  1268
					// With this we are able to always save the last state
indvdum@0
  1269
					$convert->row = $convert_row;
indvdum@0
  1270
indvdum@0
  1271
					// Increment the counting variable, it stores the number of rows we have processed
indvdum@0
  1272
					$counting++;
indvdum@0
  1273
indvdum@0
  1274
					$insert_values = array();
indvdum@0
  1275
indvdum@0
  1276
					$sql_flag = $this->process_row($schema, $sql_data, $insert_values);
indvdum@0
  1277
indvdum@0
  1278
					if ($sql_flag === true)
indvdum@0
  1279
					{
indvdum@0
  1280
						switch ($db->sql_layer)
indvdum@0
  1281
						{
indvdum@0
  1282
							// If MySQL, we'll wait to have num_wait_rows rows to submit at once
indvdum@0
  1283
							case 'mysql':
indvdum@0
  1284
							case 'mysql4':
indvdum@0
  1285
							case 'mysqli':
indvdum@0
  1286
								$waiting_rows[] = '(' . implode(', ', $insert_values) . ')';
indvdum@0
  1287
indvdum@0
  1288
								if (sizeof($waiting_rows) >= $convert->num_wait_rows)
indvdum@0
  1289
								{
indvdum@0
  1290
									$errored = false;
indvdum@0
  1291
indvdum@0
  1292
									$db->sql_return_on_error(true);
indvdum@0
  1293
indvdum@0
  1294
									if (!$db->sql_query($insert_query . implode(', ', $waiting_rows)))
indvdum@0
  1295
									{
indvdum@0
  1296
										$errored = true;
indvdum@0
  1297
									}
indvdum@0
  1298
									$db->sql_return_on_error(false);
indvdum@0
  1299
indvdum@0
  1300
									if ($errored)
indvdum@0
  1301
									{
indvdum@0
  1302
										$db->sql_return_on_error(true);
indvdum@0
  1303
indvdum@0
  1304
										// Because it errored out we will try to insert the rows one by one... most of the time this
indvdum@0
  1305
										// is caused by duplicate entries - but we also do not want to miss one...
indvdum@0
  1306
										foreach ($waiting_rows as $waiting_sql)
indvdum@0
  1307
										{
indvdum@0
  1308
											if (!$db->sql_query($insert_query . $waiting_sql))
indvdum@0
  1309
											{
indvdum@0
  1310
												$this->p_master->db_error($user->lang['DB_ERR_INSERT'], htmlspecialchars($insert_query . $waiting_sql) . '<br /><br />' . htmlspecialchars(print_r($db->_sql_error(), true)), __LINE__, __FILE__, true);
indvdum@0
  1311
											}
indvdum@0
  1312
										}
indvdum@0
  1313
indvdum@0
  1314
										$db->sql_return_on_error(false);
indvdum@0
  1315
									}
indvdum@0
  1316
indvdum@0
  1317
									$waiting_rows = array();
indvdum@0
  1318
								}
indvdum@0
  1319
indvdum@0
  1320
							break;
indvdum@0
  1321
indvdum@0
  1322
							default:
indvdum@0
  1323
								$insert_sql = $insert_query . '(' . implode(', ', $insert_values) . ')';
indvdum@0
  1324
indvdum@0
  1325
								$db->sql_return_on_error(true);
indvdum@0
  1326
indvdum@0
  1327
								if (!$db->sql_query($insert_sql))
indvdum@0
  1328
								{
indvdum@0
  1329
									$this->p_master->db_error($user->lang['DB_ERR_INSERT'], htmlspecialchars($insert_sql) . '<br /><br />' . htmlspecialchars(print_r($db->_sql_error(), true)), __LINE__, __FILE__, true);
indvdum@0
  1330
								}
indvdum@0
  1331
								$db->sql_return_on_error(false);
indvdum@0
  1332
indvdum@0
  1333
								$waiting_rows = array();
indvdum@0
  1334
indvdum@0
  1335
							break;
indvdum@0
  1336
						}
indvdum@0
  1337
					}
indvdum@0
  1338
indvdum@0
  1339
					$skip_rows++;
indvdum@0
  1340
				}
indvdum@0
  1341
				$src_db->sql_freeresult($___result);
indvdum@0
  1342
indvdum@0
  1343
				// We might still have some rows waiting
indvdum@0
  1344
				if (sizeof($waiting_rows))
indvdum@0
  1345
				{
indvdum@0
  1346
					$errored = false;
indvdum@0
  1347
					$db->sql_return_on_error(true);
indvdum@0
  1348
indvdum@0
  1349
					if (!$db->sql_query($insert_query . implode(', ', $waiting_rows)))
indvdum@0
  1350
					{
indvdum@0
  1351
						$errored = true;
indvdum@0
  1352
					}
indvdum@0
  1353
					$db->sql_return_on_error(false);
indvdum@0
  1354
indvdum@0
  1355
					if ($errored)
indvdum@0
  1356
					{
indvdum@0
  1357
						$db->sql_return_on_error(true);
indvdum@0
  1358
indvdum@0
  1359
						// Because it errored out we will try to insert the rows one by one... most of the time this
indvdum@0
  1360
						// is caused by duplicate entries - but we also do not want to miss one...
indvdum@0
  1361
						foreach ($waiting_rows as $waiting_sql)
indvdum@0
  1362
						{
indvdum@0
  1363
							$db->sql_query($insert_query . $waiting_sql);
indvdum@0
  1364
							$this->p_master->db_error($user->lang['DB_ERR_INSERT'], htmlspecialchars($insert_query . $waiting_sql) . '<br /><br />' . htmlspecialchars(print_r($db->_sql_error(), true)), __LINE__, __FILE__, true);
indvdum@0
  1365
						}
indvdum@0
  1366
indvdum@0
  1367
						$db->sql_return_on_error(false);
indvdum@0
  1368
					}
indvdum@0
  1369
indvdum@0
  1370
					$waiting_rows = array();
indvdum@0
  1371
				}
indvdum@0
  1372
indvdum@0
  1373
				if (!empty($schema['autoincrement']))
indvdum@0
  1374
				{
indvdum@0
  1375
					switch ($db->sql_layer)
indvdum@0
  1376
					{
indvdum@0
  1377
						case 'mssql':
indvdum@0
  1378
						case 'mssql_odbc':
indvdum@0
  1379
						case 'mssqlnative':
indvdum@0
  1380
							$db->sql_query('SET IDENTITY_INSERT ' . $schema['target'] . ' OFF');
indvdum@0
  1381
						break;
indvdum@0
  1382
indvdum@0
  1383
						case 'postgres':
indvdum@0
  1384
							$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'] . '));');
indvdum@0
  1385
						break;
indvdum@0
  1386
indvdum@0
  1387
						case 'oracle':
indvdum@0
  1388
							$result = $db->sql_query('SELECT MAX(' . $schema['autoincrement'] . ') as max_id FROM ' . $schema['target']);
indvdum@0
  1389
							$row = $db->sql_fetchrow($result);
indvdum@0
  1390
							$db->sql_freeresult($result);
indvdum@0
  1391
indvdum@0
  1392
							$largest_id = (int) $row['max_id'];
indvdum@0
  1393
indvdum@0
  1394
							if ($largest_id)
indvdum@0
  1395
							{
indvdum@0
  1396
								$db->sql_query('DROP SEQUENCE ' . $schema['target'] . '_seq');
indvdum@0
  1397
								$db->sql_query('CREATE SEQUENCE ' . $schema['target'] . '_seq START WITH ' . ($largest_id + 1));
indvdum@0
  1398
							}
indvdum@0
  1399
						break;
indvdum@0
  1400
					}
indvdum@0
  1401
				}
indvdum@0
  1402
			}
indvdum@0
  1403
indvdum@0
  1404
			// When we reach this point, either the current table has been processed or we're running out of time.
indvdum@0
  1405
			if (still_on_time() && $counting < $convert->batch_size/* && !defined('DEBUG_EXTRA')*/)
indvdum@0
  1406
			{
indvdum@0
  1407
				$skip_rows = 0;
indvdum@0
  1408
				$current_table++;
indvdum@0
  1409
			}
indvdum@0
  1410
			else
indvdum@0
  1411
			{/*
indvdum@0
  1412
				if (still_on_time() && $counting < $convert->batch_size)
indvdum@0
  1413
				{
indvdum@0
  1414
					$skip_rows = 0;
indvdum@0
  1415
					$current_table++;
indvdum@0
  1416
				}*/
indvdum@0
  1417
indvdum@0
  1418
				// Looks like we ran out of time.
indvdum@0
  1419
				$url = $this->save_convert_progress('&amp;current_table=' . $current_table . '&amp;skip_rows=' . $skip_rows);
indvdum@0
  1420
indvdum@0
  1421
				$current_table++;
indvdum@0
  1422
//				$percentage = ($skip_rows == 0) ? 0 : floor(100 / ($total_rows / $skip_rows));
indvdum@0
  1423
indvdum@0
  1424
				$msg = sprintf($user->lang['STEP_PERCENT_COMPLETED'], $current_table, sizeof($convert->convertor['schema']));
indvdum@0
  1425
indvdum@0
  1426
				$template->assign_vars(array(
indvdum@0
  1427
					'L_MESSAGE'		=> $msg,
indvdum@0
  1428
					'L_SUBMIT'		=> $user->lang['CONTINUE_CONVERT'],
indvdum@0
  1429
					'U_ACTION'		=> $url,
indvdum@0
  1430
				));
indvdum@0
  1431
indvdum@0
  1432
				$this->meta_refresh($url);
indvdum@0
  1433
				return;
indvdum@0
  1434
			}
indvdum@0
  1435
		}
indvdum@0
  1436
indvdum@0
  1437
		// Process execute_last then we'll be done
indvdum@0
  1438
		$url = $this->save_convert_progress('&amp;jump=1');
indvdum@0
  1439
indvdum@0
  1440
		$template->assign_vars(array(
indvdum@0
  1441
			'L_SUBMIT'		=> $user->lang['FINAL_STEP'],
indvdum@0
  1442
			'U_ACTION'		=> $url,
indvdum@0
  1443
		));
indvdum@0
  1444
indvdum@0
  1445
		$this->meta_refresh($url);
indvdum@0
  1446
		return;
indvdum@0
  1447
	}
indvdum@0
  1448
indvdum@0
  1449
	/**
indvdum@0
  1450
	* Sync function being executed at the middle, some functions need to be executed after a successful sync.
indvdum@0
  1451
	*/
indvdum@0
  1452
	function sync_forums($sync_batch)
indvdum@0
  1453
	{
indvdum@0
  1454
		global $template, $user, $db, $phpbb_root_path, $phpEx, $config, $cache;
indvdum@0
  1455
		global $convert;
indvdum@0
  1456
indvdum@0
  1457
		$template->assign_block_vars('checks', array(
indvdum@0
  1458
			'S_LEGEND'	=> true,
indvdum@0
  1459
			'LEGEND'	=> $user->lang['SYNC_TOPICS'],
indvdum@0
  1460
		));
indvdum@0
  1461
indvdum@0
  1462
		$batch_size = $convert->batch_size;
indvdum@0
  1463
indvdum@0
  1464
		$sql = 'SELECT MIN(topic_id) as min_value, MAX(topic_id) AS max_value
indvdum@0
  1465
			FROM ' . TOPICS_TABLE;
indvdum@0
  1466
		$result = $db->sql_query($sql);
indvdum@0
  1467
		$row = $db->sql_fetchrow($result);
indvdum@0
  1468
		$db->sql_freeresult($result);
indvdum@0
  1469
indvdum@0
  1470
		// Set values of minimum/maximum primary value for this table.
indvdum@0
  1471
		$primary_min = $row['min_value'];
indvdum@0
  1472
		$primary_max = $row['max_value'];
indvdum@0
  1473
indvdum@0
  1474
		if ($sync_batch == 0)
indvdum@0
  1475
		{
indvdum@0
  1476
			$sync_batch = (int) $primary_min;
indvdum@0
  1477
		}
indvdum@0
  1478
indvdum@0
  1479
		if ($sync_batch == 0)
indvdum@0
  1480
		{
indvdum@0
  1481
			$sync_batch = 1;
indvdum@0
  1482
		}
indvdum@0
  1483
indvdum@0
  1484
		// Fetch a batch of rows, process and insert them.
indvdum@0
  1485
		while ($sync_batch <= $primary_max && still_on_time())
indvdum@0
  1486
		{
indvdum@0
  1487
			$end = ($sync_batch + $batch_size - 1);
indvdum@0
  1488
indvdum@0
  1489
			// Sync all topics in batch mode...
indvdum@0
  1490
			sync('topic_approved', 'range', 'topic_id BETWEEN ' . $sync_batch . ' AND ' . $end, true, false);
indvdum@0
  1491
			sync('topic', 'range', 'topic_id BETWEEN ' . $sync_batch . ' AND ' . $end, true, true);
indvdum@0
  1492
indvdum@0
  1493
			$template->assign_block_vars('checks', array(
indvdum@0
  1494
				'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'] . ']' : ''),
indvdum@0
  1495
				'RESULT'	=> $user->lang['DONE'],
indvdum@0
  1496
			));
indvdum@0
  1497
indvdum@0
  1498
			$sync_batch += $batch_size;
indvdum@0
  1499
		}
indvdum@0
  1500
indvdum@0
  1501
		if ($sync_batch >= $primary_max)
indvdum@0
  1502
		{
indvdum@0
  1503
			$url = $this->save_convert_progress('&amp;final_jump=1');
indvdum@0
  1504
indvdum@0
  1505
			$template->assign_vars(array(
indvdum@0
  1506
				'L_SUBMIT'		=> $user->lang['CONTINUE_CONVERT'],
indvdum@0
  1507
				'U_ACTION'		=> $url,
indvdum@0
  1508
			));
indvdum@0
  1509
indvdum@0
  1510
			$this->meta_refresh($url);
indvdum@0
  1511
			return;
indvdum@0
  1512
		}
indvdum@0
  1513
		else
indvdum@0
  1514
		{
indvdum@0
  1515
			$sync_batch--;
indvdum@0
  1516
		}
indvdum@0
  1517
indvdum@0
  1518
		$url = $this->save_convert_progress('&amp;sync_batch=' . $sync_batch);
indvdum@0
  1519
indvdum@0
  1520
		$template->assign_vars(array(
indvdum@0
  1521
			'L_SUBMIT'		=> $user->lang['CONTINUE_CONVERT'],
indvdum@0
  1522
			'U_ACTION'		=> $url,
indvdum@0
  1523
		));
indvdum@0
  1524
indvdum@0
  1525
		$this->meta_refresh($url);
indvdum@0
  1526
		return;
indvdum@0
  1527
	}
indvdum@0
  1528
indvdum@0
  1529
	/**
indvdum@0
  1530
	* Save the convertor status
indvdum@0
  1531
	*/
indvdum@0
  1532
	function save_convert_progress($step)
indvdum@0
  1533
	{
indvdum@0
  1534
		global $convert, $language;
indvdum@0
  1535
indvdum@0
  1536
		// Save convertor Status
indvdum@0
  1537
		set_config('convert_progress', serialize(array(
indvdum@0
  1538
			'step'			=> $step,
indvdum@0
  1539
			'table_prefix'	=> $convert->src_table_prefix,
indvdum@0
  1540
			'tag'			=> $convert->convertor_tag,
indvdum@0
  1541
		)), true);
indvdum@0
  1542
indvdum@0
  1543
		set_config('convert_db_server', serialize(array(
indvdum@0
  1544
			'dbms'			=> $convert->src_dbms,
indvdum@0
  1545
			'dbhost'		=> $convert->src_dbhost,
indvdum@0
  1546
			'dbport'		=> $convert->src_dbport,
indvdum@0
  1547
			'dbname'		=> $convert->src_dbname,
indvdum@0
  1548
		)), true);
indvdum@0
  1549
indvdum@0
  1550
		set_config('convert_db_user', serialize(array(
indvdum@0
  1551
			'dbuser'		=> $convert->src_dbuser,
indvdum@0
  1552
			'dbpasswd'		=> $convert->src_dbpasswd,
indvdum@0
  1553
		)), true);
indvdum@0
  1554
indvdum@0
  1555
		return $this->p_master->module_url . "?mode={$this->mode}&amp;sub=in_progress&amp;tag={$convert->convertor_tag}$step&amp;language=$language";
indvdum@0
  1556
	}
indvdum@0
  1557
indvdum@0
  1558
	/**
indvdum@0
  1559
	* Finish conversion, the last function to be called.
indvdum@0
  1560
	*/
indvdum@0
  1561
	function finish_conversion()
indvdum@0
  1562
	{
indvdum@0
  1563
		global $db, $phpbb_root_path, $phpEx, $convert, $config, $language, $user, $template;
indvdum@0
  1564
indvdum@0
  1565
		$db->sql_query('DELETE FROM ' . CONFIG_TABLE . "
indvdum@0
  1566
			WHERE config_name = 'convert_progress'
indvdum@0
  1567
				OR config_name = 'convert_options'
indvdum@0
  1568
				OR config_name = 'convert_db_server'
indvdum@0
  1569
				OR config_name = 'convert_db_user'");
indvdum@0
  1570
		$db->sql_query('DELETE FROM ' . SESSIONS_TABLE);
indvdum@0
  1571
indvdum@0
  1572
		@unlink($phpbb_root_path . 'cache/data_global.' . $phpEx);
indvdum@0
  1573
		cache_moderators();
indvdum@0
  1574
indvdum@0
  1575
		// And finally, add a note to the log
indvdum@0
  1576
		add_log('admin', 'LOG_INSTALL_CONVERTED', $convert->convertor_data['forum_name'], $config['version']);
indvdum@0
  1577
indvdum@0
  1578
		$url = $this->p_master->module_url . "?mode={$this->mode}&amp;sub=final&amp;language=$language";
indvdum@0
  1579
indvdum@0
  1580
		$template->assign_vars(array(
indvdum@0
  1581
			'L_SUBMIT'		=> $user->lang['FINAL_STEP'],
indvdum@0
  1582
			'U_ACTION'		=> $url,
indvdum@0
  1583
		));
indvdum@0
  1584
indvdum@0
  1585
		$this->meta_refresh($url);
indvdum@0
  1586
		return;
indvdum@0
  1587
	}
indvdum@0
  1588
indvdum@0
  1589
	/**
indvdum@0
  1590
	* This function marks the steps after syncing
indvdum@0
  1591
	*/
indvdum@0
  1592
	function final_jump($final_jump)
indvdum@0
  1593
	{
indvdum@0
  1594
		global $template, $user, $src_db, $same_db, $db, $phpbb_root_path, $phpEx, $config, $cache;
indvdum@0
  1595
		global $convert;
indvdum@0
  1596
indvdum@0
  1597
		$template->assign_block_vars('checks', array(
indvdum@0
  1598
			'S_LEGEND'	=> true,
indvdum@0
  1599
			'LEGEND'	=> $user->lang['PROCESS_LAST'],
indvdum@0
  1600
		));
indvdum@0
  1601
indvdum@0
  1602
		if ($final_jump == 1)
indvdum@0
  1603
		{
indvdum@0
  1604
			$db->sql_return_on_error(true);
indvdum@0
  1605
indvdum@0
  1606
			update_topics_posted();
indvdum@0
  1607
indvdum@0
  1608
			$template->assign_block_vars('checks', array(
indvdum@0
  1609
				'TITLE'		=> $user->lang['UPDATE_TOPICS_POSTED'],
indvdum@0
  1610
				'RESULT'	=> $user->lang['DONE'],
indvdum@0
  1611
			));
indvdum@0
  1612
indvdum@0
  1613
			if ($db->sql_error_triggered)
indvdum@0
  1614
			{
indvdum@0
  1615
				$template->assign_vars(array(
indvdum@0
  1616
					'S_ERROR_BOX'	=> true,
indvdum@0
  1617
					'ERROR_TITLE'	=> $user->lang['UPDATE_TOPICS_POSTED'],
indvdum@0
  1618
					'ERROR_MSG'		=> $user->lang['UPDATE_TOPICS_POSTED_ERR'],
indvdum@0
  1619
				));
indvdum@0
  1620
			}
indvdum@0
  1621
			$db->sql_return_on_error(false);
indvdum@0
  1622
indvdum@0
  1623
			$this->finish_conversion();
indvdum@0
  1624
			return;
indvdum@0
  1625
		}
indvdum@0
  1626
	}
indvdum@0
  1627
indvdum@0
  1628
	/**
indvdum@0
  1629
	* This function marks the steps before syncing (jump=1)
indvdum@0
  1630
	*/
indvdum@0
  1631
	function jump($jump, $last_statement)
indvdum@0
  1632
	{
indvdum@0
  1633
		global $template, $user, $src_db, $same_db, $db, $phpbb_root_path, $phpEx, $config, $cache;
indvdum@0
  1634
		global $convert;
indvdum@0
  1635
indvdum@0
  1636
		$template->assign_block_vars('checks', array(
indvdum@0
  1637
			'S_LEGEND'	=> true,
indvdum@0
  1638
			'LEGEND'	=> $user->lang['PROCESS_LAST'],
indvdum@0
  1639
		));
indvdum@0
  1640
indvdum@0
  1641
		if ($jump == 1)
indvdum@0
  1642
		{
indvdum@0
  1643
			// Execute 'last' statements/queries
indvdum@0
  1644
			if (!empty($convert->convertor['execute_last']))
indvdum@0
  1645
			{
indvdum@0
  1646
				if (!is_array($convert->convertor['execute_last']))
indvdum@0
  1647
				{
indvdum@0
  1648
					eval($convert->convertor['execute_last']);
indvdum@0
  1649
				}
indvdum@0
  1650
				else
indvdum@0
  1651
				{
indvdum@0
  1652
					while ($last_statement < sizeof($convert->convertor['execute_last']))
indvdum@0
  1653
					{
indvdum@0
  1654
						eval($convert->convertor['execute_last'][$last_statement]);
indvdum@0
  1655
indvdum@0
  1656
						$template->assign_block_vars('checks', array(
indvdum@0
  1657
							'TITLE'		=> $convert->convertor['execute_last'][$last_statement],
indvdum@0
  1658
							'RESULT'	=> $user->lang['DONE'],
indvdum@0
  1659
						));
indvdum@0
  1660
indvdum@0
  1661
						$last_statement++;
indvdum@0
  1662
						$url = $this->save_convert_progress('&amp;jump=1&amp;last=' . $last_statement);
indvdum@0
  1663
indvdum@0
  1664
						$percentage = ($last_statement == 0) ? 0 : floor(100 / (sizeof($convert->convertor['execute_last']) / $last_statement));
indvdum@0
  1665
						$msg = sprintf($user->lang['STEP_PERCENT_COMPLETED'], $last_statement, sizeof($convert->convertor['execute_last']), $percentage);
indvdum@0
  1666
indvdum@0
  1667
						$template->assign_vars(array(
indvdum@0
  1668
							'L_SUBMIT'		=> $user->lang['CONTINUE_LAST'],
indvdum@0
  1669
							'L_MESSAGE'		=> $msg,
indvdum@0
  1670
							'U_ACTION'		=> $url,
indvdum@0
  1671
						));
indvdum@0
  1672
indvdum@0
  1673
						$this->meta_refresh($url);
indvdum@0
  1674
						return;
indvdum@0
  1675
					}
indvdum@0
  1676
				}
indvdum@0
  1677
			}
indvdum@0
  1678
indvdum@0
  1679
			if (!empty($convert->convertor['query_last']))
indvdum@0
  1680
			{
indvdum@0
  1681
				if (!is_array($convert->convertor['query_last']))
indvdum@0
  1682
				{
indvdum@0
  1683
					$convert->convertor['query_last'] = array('target', array($convert->convertor['query_last']));
indvdum@0
  1684
				}
indvdum@0
  1685
				else if (!is_array($convert->convertor['query_last'][0]))
indvdum@0
  1686
				{
indvdum@0
  1687
					$convert->convertor['query_last'] = array(array($convert->convertor['query_last'][0], $convert->convertor['query_last'][1]));
indvdum@0
  1688
				}
indvdum@0
  1689
indvdum@0
  1690
				foreach ($convert->convertor['query_last'] as $query_last)
indvdum@0
  1691
				{
indvdum@0
  1692
					if ($query_last[0] == 'src')
indvdum@0
  1693
					{
indvdum@0
  1694
						if ($convert->mysql_convert && $same_db)
indvdum@0
  1695
						{
indvdum@0
  1696
							$src_db->sql_query("SET NAMES 'binary'");
indvdum@0
  1697
						}
indvdum@0
  1698
indvdum@0
  1699
						$src_db->sql_query($query_last[1]);
indvdum@0
  1700
indvdum@0
  1701
						if ($convert->mysql_convert && $same_db)
indvdum@0
  1702
						{
indvdum@0
  1703
							$src_db->sql_query("SET NAMES 'utf8'");
indvdum@0
  1704
						}
indvdum@0
  1705
					}
indvdum@0
  1706
					else
indvdum@0
  1707
					{
indvdum@0
  1708
						$db->sql_query($query_last[1]);
indvdum@0
  1709
					}
indvdum@0
  1710
				}
indvdum@0
  1711
			}
indvdum@0
  1712
indvdum@0
  1713
			// Sanity check
indvdum@0
  1714
			$db->sql_return_on_error(false);
indvdum@0
  1715
			$src_db->sql_return_on_error(false);
indvdum@0
  1716
indvdum@0
  1717
			fix_empty_primary_groups();
indvdum@0
  1718
indvdum@0
  1719
			$sql = 'SELECT MIN(user_regdate) AS board_startdate
indvdum@0
  1720
				FROM ' . USERS_TABLE;
indvdum@0
  1721
			$result = $db->sql_query($sql);
indvdum@0
  1722
			$row = $db->sql_fetchrow($result);
indvdum@0
  1723
			$db->sql_freeresult($result);
indvdum@0
  1724
indvdum@0
  1725
			if (!isset($config['board_startdate']) || ($row['board_startdate'] < $config['board_startdate'] && $row['board_startdate'] > 0))
indvdum@0
  1726
			{
indvdum@0
  1727
				set_config('board_startdate', $row['board_startdate']);
indvdum@0
  1728
				$db->sql_query('UPDATE ' . USERS_TABLE . ' SET user_regdate = ' . $row['board_startdate'] . ' WHERE user_id = ' . ANONYMOUS);
indvdum@0
  1729
			}
indvdum@0
  1730
indvdum@0
  1731
			update_dynamic_config();
indvdum@0
  1732
indvdum@0
  1733
			$template->assign_block_vars('checks', array(
indvdum@0
  1734
				'TITLE'		=> $user->lang['CLEAN_VERIFY'],
indvdum@0
  1735
				'RESULT'	=> $user->lang['DONE'],
indvdum@0
  1736
			));
indvdum@0
  1737
indvdum@0
  1738
			$url = $this->save_convert_progress('&amp;jump=2');
indvdum@0
  1739
indvdum@0
  1740
			$template->assign_vars(array(
indvdum@0
  1741
				'L_SUBMIT'		=> $user->lang['CONTINUE_CONVERT'],
indvdum@0
  1742
				'U_ACTION'		=> $url,
indvdum@0
  1743
			));
indvdum@0
  1744
indvdum@0
  1745
			$this->meta_refresh($url);
indvdum@0
  1746
			return;
indvdum@0
  1747
		}
indvdum@0
  1748
indvdum@0
  1749
		if ($jump == 2)
indvdum@0
  1750
		{
indvdum@0
  1751
			$db->sql_query('UPDATE ' . USERS_TABLE . " SET user_permissions = ''");
indvdum@0
  1752
indvdum@0
  1753
			// TODO: sync() is likely going to bomb out on forums with a considerable amount of topics.
indvdum@0
  1754
			// TODO: the sync function is able to handle FROM-TO values, we should use them here (batch processing)
indvdum@0
  1755
			sync('forum', '', '', false, true);
indvdum@0
  1756
			$cache->destroy('sql', FORUMS_TABLE);
indvdum@0
  1757
indvdum@0
  1758
			$template->assign_block_vars('checks', array(
indvdum@0
  1759
				'TITLE'		=> $user->lang['SYNC_FORUMS'],
indvdum@0
  1760
				'RESULT'	=> $user->lang['DONE'],
indvdum@0
  1761
			));
indvdum@0
  1762
indvdum@0
  1763
			// Continue with synchronizing the forums...
indvdum@0
  1764
			$url = $this->save_convert_progress('&amp;sync_batch=0');
indvdum@0
  1765
indvdum@0
  1766
			$template->assign_vars(array(
indvdum@0
  1767
				'L_SUBMIT'		=> $user->lang['CONTINUE_CONVERT'],
indvdum@0
  1768
				'U_ACTION'		=> $url,
indvdum@0
  1769
			));
indvdum@0
  1770
indvdum@0
  1771
			$this->meta_refresh($url);
indvdum@0
  1772
			return;
indvdum@0
  1773
		}
indvdum@0
  1774
	}
indvdum@0
  1775
indvdum@0
  1776
	function build_insert_query(&$schema, &$sql_data, $current_table)
indvdum@0
  1777
	{
indvdum@0
  1778
		global $db, $user;
indvdum@0
  1779
		global $convert;
indvdum@0
  1780
indvdum@0
  1781
		// Can we use IGNORE with this DBMS?
indvdum@0
  1782
		$sql_ignore = (strpos($db->sql_layer, 'mysql') === 0 && !defined('DEBUG_EXTRA')) ? 'IGNORE ' : '';
indvdum@0
  1783
		$insert_query = 'INSERT ' . $sql_ignore . 'INTO ' . $schema['target'] . ' (';
indvdum@0
  1784
indvdum@0
  1785
		$aliases = array();
indvdum@0
  1786
indvdum@0
  1787
		$sql_data = array(
indvdum@0
  1788
			'source_fields'		=> array(),
indvdum@0
  1789
			'target_fields'		=> array(),
indvdum@0
  1790
			'source_tables'		=> array(),
indvdum@0
  1791
			'select_fields'		=> array(),
indvdum@0
  1792
		);
indvdum@0
  1793
indvdum@0
  1794
		foreach ($schema as $key => $val)
indvdum@0
  1795
		{
indvdum@0
  1796
			// Example: array('group_name',				'extension_groups.group_name',		'htmlspecialchars'),
indvdum@0
  1797
			if (is_int($key))
indvdum@0
  1798
			{
indvdum@0
  1799
				if (!empty($val[0]))
indvdum@0
  1800
				{
indvdum@0
  1801
					// Target fields
indvdum@0
  1802
					$sql_data['target_fields'][$val[0]] = $key;
indvdum@0
  1803
					$insert_query .= $val[0] . ', ';
indvdum@0
  1804
				}
indvdum@0
  1805
indvdum@0
  1806
				if (!is_array($val[1]))
indvdum@0
  1807
				{
indvdum@0
  1808
					$val[1] = array($val[1]);
indvdum@0
  1809
				}
indvdum@0
  1810
indvdum@0
  1811
				foreach ($val[1] as $valkey => $value_1)
indvdum@0
  1812
				{
indvdum@0
  1813
					// This should cover about any case:
indvdum@0
  1814
					//
indvdum@0
  1815
					// table.field					=> SELECT table.field				FROM table
indvdum@0
  1816
					// table.field AS alias			=> SELECT table.field	AS alias	FROM table
indvdum@0
  1817
					// table.field AS table2.alias	=> SELECT table2.field	AS alias	FROM table table2
indvdum@0
  1818
					// table.field AS table2.field	=> SELECT table2.field				FROM table table2
indvdum@0
  1819
					//
indvdum@0
  1820
					if (preg_match('/^([a-z0-9_]+)\.([a-z0-9_]+)( +AS +(([a-z0-9_]+?)\.)?([a-z0-9_]+))?$/i', $value_1, $m))
indvdum@0
  1821
					{
indvdum@0
  1822
						// There is 'AS ...' in the field names
indvdum@0
  1823
						if (!empty($m[3]))
indvdum@0
  1824
						{
indvdum@0
  1825
							$value_1 = ($m[2] == $m[6]) ? $m[1] . '.' . $m[2] : $m[1] . '.' . $m[2] . ' AS ' . $m[6];
indvdum@0
  1826
indvdum@0
  1827
							// Table alias: store it then replace the source table with it
indvdum@0
  1828
							if (!empty($m[5]) && $m[5] != $m[1])
indvdum@0
  1829
							{
indvdum@0
  1830
								$aliases[$m[5]] = $m[1];
indvdum@0
  1831
								$value_1 = str_replace($m[1] . '.' . $m[2], $m[5] . '.' . $m[2], $value_1);
indvdum@0
  1832
							}
indvdum@0
  1833
						}
indvdum@0
  1834
						else
indvdum@0
  1835
						{
indvdum@0
  1836
							// No table alias
indvdum@0
  1837
							$sql_data['source_tables'][$m[1]] = (empty($convert->src_table_prefix)) ? $m[1] : $convert->src_table_prefix . $m[1] . ' ' . $m[1];
indvdum@0
  1838
						}
indvdum@0
  1839
indvdum@0
  1840
						$sql_data['select_fields'][$value_1] = $value_1;
indvdum@0
  1841
						$sql_data['source_fields'][$key][$valkey] = (!empty($m[6])) ? $m[6] : $m[2];
indvdum@0
  1842
					}
indvdum@0
  1843
				}
indvdum@0
  1844
			}
indvdum@0
  1845
			else if ($key == 'where' || $key == 'group_by' || $key == 'order_by' || $key == 'having')
indvdum@0
  1846
			{
indvdum@0
  1847
				if (@preg_match_all('/([a-z0-9_]+)\.([a-z0-9_]+)/i', $val, $m))
indvdum@0
  1848
				{
indvdum@0
  1849
					foreach ($m[1] as $value)
indvdum@0
  1850
					{
indvdum@0
  1851
						$sql_data['source_tables'][$value] = (empty($convert->src_table_prefix)) ? $value : $convert->src_table_prefix . $value . ' ' . $value;
indvdum@0
  1852
					}
indvdum@0
  1853
				}
indvdum@0
  1854
			}
indvdum@0
  1855
		}
indvdum@0
  1856
indvdum@0
  1857
		// Add the aliases to the list of tables
indvdum@0
  1858
		foreach ($aliases as $alias => $table)
indvdum@0
  1859
		{
indvdum@0
  1860
			$sql_data['source_tables'][$alias] = $convert->src_table_prefix . $table . ' ' . $alias;
indvdum@0
  1861
		}
indvdum@0
  1862
indvdum@0
  1863
		// 'left_join'		=> 'forums LEFT JOIN forum_prune ON forums.forum_id = forum_prune.forum_id',
indvdum@0
  1864
		if (!empty($schema['left_join']))
indvdum@0
  1865
		{
indvdum@0
  1866
			if (!is_array($schema['left_join']))
indvdum@0
  1867
			{
indvdum@0
  1868
				$schema['left_join'] = array($schema['left_join']);
indvdum@0
  1869
			}
indvdum@0
  1870
indvdum@0
  1871
			foreach ($schema['left_join'] as $left_join)
indvdum@0
  1872
			{
indvdum@0
  1873
				// This won't handle concatened LEFT JOINs
indvdum@0
  1874
				if (!preg_match('/([a-z0-9_]+) LEFT JOIN ([a-z0-9_]+) A?S? ?([a-z0-9_]*?) ?(ON|USING)(.*)/i', $left_join, $m))
indvdum@0
  1875
				{
indvdum@0
  1876
					$this->p_master->error(sprintf($user->lang['NOT_UNDERSTAND'], 'LEFT JOIN', $left_join, $current_table, $schema['target']), __LINE__, __FILE__);
indvdum@0
  1877
				}
indvdum@0
  1878
indvdum@0
  1879
				if (!empty($aliases[$m[2]]))
indvdum@0
  1880
				{
indvdum@0
  1881
					if (!empty($m[3]))
indvdum@0
  1882
					{
indvdum@0
  1883
						$this->p_master->error(sprintf($user->lang['NAMING_CONFLICT'], $m[2], $m[3], $schema['left_join']), __LINE__, __FILE__);
indvdum@0
  1884
					}
indvdum@0
  1885
indvdum@0
  1886
					$m[2] = $aliases[$m[2]];
indvdum@0
  1887
					$m[3] = $m[2];
indvdum@0
  1888
				}
indvdum@0
  1889
indvdum@0
  1890
				$right_table = $convert->src_table_prefix . $m[2];
indvdum@0
  1891
				if (!empty($m[3]))
indvdum@0
  1892
				{
indvdum@0
  1893
					unset($sql_data['source_tables'][$m[3]]);
indvdum@0
  1894
				}
indvdum@0
  1895
				else if ($m[2] != $m[1])
indvdum@0
  1896
				{
indvdum@0
  1897
					unset($sql_data['source_tables'][$m[2]]);
indvdum@0
  1898
				}
indvdum@0
  1899
indvdum@0
  1900
				if (strpos($sql_data['source_tables'][$m[1]], "\nLEFT JOIN") !== false)
indvdum@0
  1901
				{
indvdum@0
  1902
					$sql_data['source_tables'][$m[1]] = '(' . $sql_data['source_tables'][$m[1]] . ")\nLEFT JOIN $right_table";
indvdum@0
  1903
				}
indvdum@0
  1904
				else
indvdum@0
  1905
				{
indvdum@0
  1906
					$sql_data['source_tables'][$m[1]] .= "\nLEFT JOIN $right_table";
indvdum@0
  1907
				}
indvdum@0
  1908
indvdum@0
  1909
				if (!empty($m[3]))
indvdum@0
  1910
				{
indvdum@0
  1911
					unset($sql_data['source_tables'][$m[3]]);
indvdum@0
  1912
					$sql_data['source_tables'][$m[1]] .= ' AS ' . $m[3];
indvdum@0
  1913
				}
indvdum@0
  1914
				else if (!empty($convert->src_table_prefix))
indvdum@0
  1915
				{
indvdum@0
  1916
					$sql_data['source_tables'][$m[1]] .= ' AS ' . $m[2];
indvdum@0
  1917
				}
indvdum@0
  1918
				$sql_data['source_tables'][$m[1]] .= ' ' . $m[4] . $m[5];
indvdum@0
  1919
			}
indvdum@0
  1920
		}
indvdum@0
  1921
indvdum@0
  1922
		// Remove ", " from the end of the insert query
indvdum@0
  1923
		$insert_query = substr($insert_query, 0, -2) . ') VALUES ';
indvdum@0
  1924
indvdum@0
  1925
		return $insert_query;
indvdum@0
  1926
	}
indvdum@0
  1927
indvdum@0
  1928
	/**
indvdum@0
  1929
	* Function for processing the currently handled row
indvdum@0
  1930
	*/
indvdum@0
  1931
	function process_row(&$schema, &$sql_data, &$insert_values)
indvdum@0
  1932
	{
indvdum@0
  1933
		global $template, $user, $phpbb_root_path, $phpEx, $db, $lang, $config, $cache;
indvdum@0
  1934
		global $convert, $convert_row;
indvdum@0
  1935
indvdum@0
  1936
		$sql_flag = false;
indvdum@0
  1937
indvdum@0
  1938
		foreach ($schema as $key => $fields)
indvdum@0
  1939
		{
indvdum@0
  1940
			// We are only interested in the lines with:
indvdum@0
  1941
			// array('comment', 'attachments_desc.comment', 'htmlspecialchars'),
indvdum@0
  1942
			if (is_int($key))
indvdum@0
  1943
			{
indvdum@0
  1944
				if (!is_array($fields[1]))
indvdum@0
  1945
				{
indvdum@0
  1946
					$fields[1] = array($fields[1]);
indvdum@0
  1947
				}
indvdum@0
  1948
indvdum@0
  1949
				$firstkey_set = false;
indvdum@0
  1950
				$firstkey = 0;
indvdum@0
  1951
indvdum@0
  1952
				foreach ($fields[1] as $inner_key => $inner_value)
indvdum@0
  1953
				{
indvdum@0
  1954
					if (!$firstkey_set)
indvdum@0
  1955
					{
indvdum@0
  1956
						$firstkey = $inner_key;
indvdum@0
  1957
						$firstkey_set = true;
indvdum@0
  1958
					}
indvdum@0
  1959
indvdum@0
  1960
					$src_field = isset($sql_data['source_fields'][$key][$inner_key]) ? $sql_data['source_fields'][$key][$inner_key] : '';
indvdum@0
  1961
indvdum@0
  1962
					if (!empty($src_field))
indvdum@0
  1963
					{
indvdum@0
  1964
						$fields[1][$inner_key] = $convert->row[$src_field];
indvdum@0
  1965
					}
indvdum@0
  1966
				}
indvdum@0
  1967
indvdum@0
  1968
				if (!empty($fields[0]))
indvdum@0
  1969
				{
indvdum@0
  1970
					// We have a target field, if we haven't set $sql_flag yet it will be set to TRUE.
indvdum@0
  1971
					// If a function has already set it to FALSE it won't change it.
indvdum@0
  1972
					if ($sql_flag === false)
indvdum@0
  1973
					{
indvdum@0
  1974
						$sql_flag = true;
indvdum@0
  1975
					}
indvdum@0
  1976
indvdum@0
  1977
					// No function assigned?
indvdum@0
  1978
					if (empty($fields[2]))
indvdum@0
  1979
					{
indvdum@0
  1980
						$value = $fields[1][$firstkey];
indvdum@0
  1981
					}
indvdum@0
  1982
					else if (is_array($fields[2]))
indvdum@0
  1983
					{
indvdum@0
  1984
						// Execute complex function/eval/typecast
indvdum@0
  1985
						$value = $fields[1];
indvdum@0
  1986
indvdum@0
  1987
						foreach ($fields[2] as $type => $execution)
indvdum@0
  1988
						{
indvdum@0
  1989
							if (strpos($type, 'typecast') === 0)
indvdum@0
  1990
							{
indvdum@0
  1991
								if (!is_array($value))
indvdum@0
  1992
								{
indvdum@0
  1993
									$value = array($value);
indvdum@0
  1994
								}
indvdum@0
  1995
								$value = $value[0];
indvdum@0
  1996
								settype($value, $execution);
indvdum@0
  1997
							}
indvdum@0
  1998
							else if (strpos($type, 'function') === 0)
indvdum@0
  1999
							{
indvdum@0
  2000
								if (!is_array($value))
indvdum@0
  2001
								{
indvdum@0
  2002
									$value = array($value);
indvdum@0
  2003
								}
indvdum@0
  2004
indvdum@0
  2005
								$value = call_user_func_array($execution, $value);
indvdum@0
  2006
							}
indvdum@0
  2007
							else if (strpos($type, 'execute') === 0)
indvdum@0
  2008
							{
indvdum@0
  2009
								if (!is_array($value))
indvdum@0
  2010
								{
indvdum@0
  2011
									$value = array($value);
indvdum@0
  2012
								}
indvdum@0
  2013
indvdum@0
  2014
								$execution = str_replace('{RESULT}', '$value', $execution);
indvdum@0
  2015
								$execution = str_replace('{VALUE}', '$value', $execution);
indvdum@0
  2016
								eval($execution);
indvdum@0
  2017
							}
indvdum@0
  2018
						}
indvdum@0
  2019
					}
indvdum@0
  2020
					else
indvdum@0
  2021
					{
indvdum@0
  2022
						$value = call_user_func_array($fields[2], $fields[1]);
indvdum@0
  2023
					}
indvdum@0
  2024
indvdum@0
  2025
					if (is_null($value))
indvdum@0
  2026
					{
indvdum@0
  2027
						$value = '';
indvdum@0
  2028
					}
indvdum@0
  2029
indvdum@0
  2030
					$insert_values[] = $db->_sql_validate_value($value);
indvdum@0
  2031
				}
indvdum@0
  2032
				else if (!empty($fields[2]))
indvdum@0
  2033
				{
indvdum@0
  2034
					if (is_array($fields[2]))
indvdum@0
  2035
					{
indvdum@0
  2036
						// Execute complex function/eval/typecast
indvdum@0
  2037
						$value = '';
indvdum@0
  2038
indvdum@0
  2039
						foreach ($fields[2] as $type => $execution)
indvdum@0
  2040
						{
indvdum@0
  2041
							if (strpos($type, 'typecast') === 0)
indvdum@0
  2042
							{
indvdum@0
  2043
								$value = settype($value, $execution);
indvdum@0
  2044
							}
indvdum@0
  2045
							else if (strpos($type, 'function') === 0)
indvdum@0
  2046
							{
indvdum@0
  2047
								if (!is_array($value))
indvdum@0
  2048
								{
indvdum@0
  2049
									$value = array($value);
indvdum@0
  2050
								}
indvdum@0
  2051
indvdum@0
  2052
								$value = call_user_func_array($execution, $value);
indvdum@0
  2053
							}
indvdum@0
  2054
							else if (strpos($type, 'execute') === 0)
indvdum@0
  2055
							{
indvdum@0
  2056
								if (!is_array($value))
indvdum@0
  2057
								{
indvdum@0
  2058
									$value = array($value);
indvdum@0
  2059
								}
indvdum@0
  2060
indvdum@0
  2061
								$execution = str_replace('{RESULT}', '$value', $execution);
indvdum@0
  2062
								$execution = str_replace('{VALUE}', '$value', $execution);
indvdum@0
  2063
								eval($execution);
indvdum@0
  2064
							}
indvdum@0
  2065
						}
indvdum@0
  2066
					}
indvdum@0
  2067
					else
indvdum@0
  2068
					{
indvdum@0
  2069
						call_user_func_array($fields[2], $fields[1]);
indvdum@0
  2070
					}
indvdum@0
  2071
				}
indvdum@0
  2072
			}
indvdum@0
  2073
		}
indvdum@0
  2074
indvdum@0
  2075
		return $sql_flag;
indvdum@0
  2076
	}
indvdum@0
  2077
indvdum@0
  2078
	/**
indvdum@0
  2079
	* Own meta refresh function to be able to change the global time used
indvdum@0
  2080
	*/
indvdum@0
  2081
	function meta_refresh($url)
indvdum@0
  2082
	{
indvdum@0
  2083
		global $convert, $template;
indvdum@0
  2084
indvdum@0
  2085
		if ($convert->options['refresh'])
indvdum@0
  2086
		{
indvdum@0
  2087
			// Because we should not rely on correct settings, we simply use the relative path here directly.
indvdum@0
  2088
			$template->assign_vars(array(
indvdum@0
  2089
				'S_REFRESH'	=> true,
indvdum@0
  2090
				'META'		=> '<meta http-equiv="refresh" content="5;url=' . $url . '" />')
indvdum@0
  2091
			);
indvdum@0
  2092
		}
indvdum@0
  2093
	}
indvdum@0
  2094
indvdum@0
  2095
	/**
indvdum@0
  2096
	* The information below will be used to build the input fields presented to the user
indvdum@0
  2097
	*/
indvdum@0
  2098
	var $convert_options = array(
indvdum@0
  2099
		'legend1'			=> 'SPECIFY_OPTIONS',
indvdum@0
  2100
		'src_dbms'			=> array('lang' => 'DBMS',			'type' => 'select', 'options' => 'dbms_select(\'{VALUE}\', true)', 'explain' => false),
indvdum@0
  2101
		'src_dbhost'		=> array('lang' => 'DB_HOST',		'type' => 'text:25:100', 'explain' => true),
indvdum@0
  2102
		'src_dbport'		=> array('lang' => 'DB_PORT',		'type' => 'text:25:100', 'explain' => true),
indvdum@0
  2103
		'src_dbname'		=> array('lang' => 'DB_NAME',		'type' => 'text:25:100', 'explain' => false),
indvdum@0
  2104
		'src_dbuser'		=> array('lang' => 'DB_USERNAME',	'type' => 'text:25:100', 'explain' => false),
indvdum@0
  2105
		'src_dbpasswd'		=> array('lang' => 'DB_PASSWORD',	'type' => 'password:25:100', 'explain' => false),
indvdum@0
  2106
		'src_table_prefix'	=> array('lang' => 'TABLE_PREFIX',	'type' => 'text:25:100', 'explain' => false),
indvdum@0
  2107
		//'src_url'			=> array('lang' => 'FORUM_ADDRESS',	'type' => 'text:50:100', 'explain' => true),
indvdum@0
  2108
		'forum_path'		=> array('lang' => 'FORUM_PATH',	'type' => 'text:25:100', 'explain' => true),
indvdum@0
  2109
		'refresh'			=> array('lang' => 'REFRESH_PAGE',	'type' => 'radio:yes_no', 'explain' => true),
indvdum@0
  2110
	);
indvdum@0
  2111
}
indvdum@0
  2112
indvdum@0
  2113
?>