install/convertors/functions_phpbb20.php
author indvdum (gotoindvdum[at]gmail[dot]com)
Wed, 07 Mar 2012 16:41:10 +0400
changeset 0 ceef6d64c21d
permissions -rw-r--r--
Added phpBB-3.0.10
     1 <?php
     2 /**
     3 *
     4 * @package install
     5 * @version $Id$
     6 * @copyright (c) 2006 phpBB Group
     7 * @license http://opensource.org/licenses/gpl-license.php GNU Public License
     8 *
     9 */
    10 
    11 if (!defined('IN_PHPBB'))
    12 {
    13 	exit;
    14 }
    15 
    16 /**
    17 * Helper functions for phpBB 2.0.x to phpBB 3.0.x conversion
    18 */
    19 
    20 /**
    21 * Set forum flags - only prune old polls by default
    22 */
    23 function phpbb_forum_flags()
    24 {
    25 	// Set forum flags
    26 	$forum_flags = 0;
    27 
    28 	// FORUM_FLAG_LINK_TRACK
    29 	$forum_flags += 0;
    30 
    31 	// FORUM_FLAG_PRUNE_POLL
    32 	$forum_flags += FORUM_FLAG_PRUNE_POLL;
    33 
    34 	// FORUM_FLAG_PRUNE_ANNOUNCE
    35 	$forum_flags += 0;
    36 
    37 	// FORUM_FLAG_PRUNE_STICKY
    38 	$forum_flags += 0;
    39 
    40 	// FORUM_FLAG_ACTIVE_TOPICS
    41 	$forum_flags += 0;
    42 
    43 	// FORUM_FLAG_POST_REVIEW
    44 	$forum_flags += FORUM_FLAG_POST_REVIEW;
    45 
    46 	return $forum_flags;
    47 }
    48 
    49 /**
    50 * Insert/Convert forums
    51 */
    52 function phpbb_insert_forums()
    53 {
    54 	global $db, $src_db, $same_db, $convert, $user, $config;
    55 
    56 	$db->sql_query($convert->truncate_statement . FORUMS_TABLE);
    57 
    58 	// Determine the highest id used within the old forums table (we add the categories after the forum ids)
    59 	$sql = 'SELECT MAX(forum_id) AS max_forum_id
    60 		FROM ' . $convert->src_table_prefix . 'forums';
    61 	$result = $src_db->sql_query($sql);
    62 	$max_forum_id = (int) $src_db->sql_fetchfield('max_forum_id');
    63 	$src_db->sql_freeresult($result);
    64 
    65 	$max_forum_id++;
    66 
    67 	// pruning disabled globally?
    68 	$sql = "SELECT config_value
    69 		FROM {$convert->src_table_prefix}config
    70 		WHERE config_name = 'prune_enable'";
    71 	$result = $src_db->sql_query($sql);
    72 	$prune_enabled = (int) $src_db->sql_fetchfield('config_value');
    73 	$src_db->sql_freeresult($result);
    74 
    75 
    76 	// Insert categories
    77 	$sql = 'SELECT cat_id, cat_title
    78 		FROM ' . $convert->src_table_prefix . 'categories
    79 		ORDER BY cat_order';
    80 
    81 	if ($convert->mysql_convert && $same_db)
    82 	{
    83 		$src_db->sql_query("SET NAMES 'binary'");
    84 	}
    85 
    86 	$result = $src_db->sql_query($sql);
    87 
    88 	if ($convert->mysql_convert && $same_db)
    89 	{
    90 		$src_db->sql_query("SET NAMES 'utf8'");
    91 	}
    92 
    93 	switch ($db->sql_layer)
    94 	{
    95 		case 'mssql':
    96 		case 'mssql_odbc':
    97 		case 'mssqlnative':
    98 			$db->sql_query('SET IDENTITY_INSERT ' . FORUMS_TABLE . ' ON');
    99 		break;
   100 	}
   101 
   102 	$cats_added = array();
   103 	while ($row = $src_db->sql_fetchrow($result))
   104 	{
   105 		$sql_ary = array(
   106 			'forum_id'		=> (int) $max_forum_id,
   107 			'forum_name'	=> ($row['cat_title']) ? htmlspecialchars(phpbb_set_default_encoding($row['cat_title']), ENT_COMPAT, 'UTF-8') : $user->lang['CATEGORY'],
   108 			'parent_id'		=> 0,
   109 			'forum_parents'	=> '',
   110 			'forum_desc'	=> '',
   111 			'forum_type'	=> FORUM_CAT,
   112 			'forum_status'	=> ITEM_UNLOCKED,
   113 			'forum_rules'	=> '',
   114 		);
   115 
   116 		$sql = 'SELECT MAX(right_id) AS right_id
   117 			FROM ' . FORUMS_TABLE;
   118 		$_result = $db->sql_query($sql);
   119 		$cat_row = $db->sql_fetchrow($_result);
   120 		$db->sql_freeresult($_result);
   121 
   122 		$sql_ary['left_id'] = (int) ($cat_row['right_id'] + 1);
   123 		$sql_ary['right_id'] = (int) ($cat_row['right_id'] + 2);
   124 
   125 		$sql = 'INSERT INTO ' . FORUMS_TABLE . ' ' . $db->sql_build_array('INSERT', $sql_ary);
   126 		$db->sql_query($sql);
   127 
   128 		$cats_added[$row['cat_id']] = $max_forum_id;
   129 		$max_forum_id++;
   130 	}
   131 	$src_db->sql_freeresult($result);
   132 
   133 	// There may be installations having forums with non-existant category ids.
   134 	// We try to catch them and add them to an "unknown" category instead of leaving them out.
   135 	$sql = 'SELECT cat_id
   136 		FROM ' . $convert->src_table_prefix . 'forums
   137 		GROUP BY cat_id';
   138 	$result = $src_db->sql_query($sql);
   139 
   140 	$unknown_cat_id = false;
   141 	while ($row = $src_db->sql_fetchrow($result))
   142 	{
   143 		// Catch those categories not been added before
   144 		if (!isset($cats_added[$row['cat_id']]))
   145 		{
   146 			$unknown_cat_id = true;
   147 		}
   148 	}
   149 	$src_db->sql_freeresult($result);
   150 
   151 	// Is there at least one category not known?
   152 	if ($unknown_cat_id === true)
   153 	{
   154 		$unknown_cat_id = 'ghost';
   155 
   156 		$sql_ary = array(
   157 			'forum_id'		=> (int) $max_forum_id,
   158 			'forum_name'	=> (string) $user->lang['CATEGORY'],
   159 			'parent_id'		=> 0,
   160 			'forum_parents'	=> '',
   161 			'forum_desc'	=> '',
   162 			'forum_type'	=> FORUM_CAT,
   163 			'forum_status'	=> ITEM_UNLOCKED,
   164 			'forum_rules'	=> '',
   165 		);
   166 
   167 		$sql = 'SELECT MAX(right_id) AS right_id
   168 			FROM ' . FORUMS_TABLE;
   169 		$_result = $db->sql_query($sql);
   170 		$cat_row = $db->sql_fetchrow($_result);
   171 		$db->sql_freeresult($_result);
   172 
   173 		$sql_ary['left_id'] = (int) ($cat_row['right_id'] + 1);
   174 		$sql_ary['right_id'] = (int) ($cat_row['right_id'] + 2);
   175 
   176 		$sql = 'INSERT INTO ' . FORUMS_TABLE . ' ' . $db->sql_build_array('INSERT', $sql_ary);
   177 		$db->sql_query($sql);
   178 
   179 		$cats_added[$unknown_cat_id] = $max_forum_id;
   180 		$max_forum_id++;
   181 	}
   182 
   183 	// Now insert the forums
   184 	$sql = 'SELECT f.forum_id, f.forum_name, f.cat_id, f.forum_desc, f.forum_status, f.prune_enable, f.prune_next, fp.prune_days, fp.prune_freq FROM ' . $convert->src_table_prefix . 'forums f
   185 		LEFT JOIN ' . $convert->src_table_prefix . 'forum_prune fp ON f.forum_id = fp.forum_id
   186 		GROUP BY f.forum_id, f.forum_name, f.cat_id, f.forum_desc, f.forum_status, f.prune_enable, f.prune_next, f.forum_order, fp.prune_days, fp.prune_freq
   187 		ORDER BY f.cat_id, f.forum_order';
   188 
   189 	if ($convert->mysql_convert && $same_db)
   190 	{
   191 		$src_db->sql_query("SET NAMES 'binary'");
   192 	}
   193 
   194 	$result = $src_db->sql_query($sql);
   195 
   196 	if ($convert->mysql_convert && $same_db)
   197 	{
   198 		$src_db->sql_query("SET NAMES 'utf8'");
   199 	}
   200 
   201 	while ($row = $src_db->sql_fetchrow($result))
   202 	{
   203 		// Some might have forums here with an id not being "possible"...
   204 		// To be somewhat friendly we "change" the category id for those to a previously created ghost category
   205 		if (!isset($cats_added[$row['cat_id']]) && $unknown_cat_id !== false)
   206 		{
   207 			$row['cat_id'] = $unknown_cat_id;
   208 		}
   209 
   210 		if (!isset($cats_added[$row['cat_id']]))
   211 		{
   212 			continue;
   213 		}
   214 
   215 		// Define the new forums sql ary
   216 		$sql_ary = array(
   217 			'forum_id'			=> (int) $row['forum_id'],
   218 			'forum_name'		=> htmlspecialchars(phpbb_set_default_encoding($row['forum_name']), ENT_COMPAT, 'UTF-8'),
   219 			'parent_id'			=> (int) $cats_added[$row['cat_id']],
   220 			'forum_parents'		=> '',
   221 			'forum_desc'		=> htmlspecialchars(phpbb_set_default_encoding($row['forum_desc']), ENT_COMPAT, 'UTF-8'),
   222 			'forum_type'		=> FORUM_POST,
   223 			'forum_status'		=> is_item_locked($row['forum_status']),
   224 			'enable_prune'		=> ($prune_enabled) ? (int)$row['prune_enable'] : 0,
   225 			'prune_next'		=> (int) null_to_zero($row['prune_next']),
   226 			'prune_days'		=> (int) null_to_zero($row['prune_days']),
   227 			'prune_viewed'		=> 0,
   228 			'prune_freq'		=> (int) null_to_zero($row['prune_freq']),
   229 
   230 			'forum_flags'		=> phpbb_forum_flags(),
   231 			'forum_options'		=> 0,
   232 
   233 			// Default values
   234 			'forum_desc_bitfield'		=> '',
   235 			'forum_desc_options'		=> 7,
   236 			'forum_desc_uid'			=> '',
   237 			'forum_link'				=> '',
   238 			'forum_password'			=> '',
   239 			'forum_style'				=> 0,
   240 			'forum_image'				=> '',
   241 			'forum_rules'				=> '',
   242 			'forum_rules_link'			=> '',
   243 			'forum_rules_bitfield'		=> '',
   244 			'forum_rules_options'		=> 7,
   245 			'forum_rules_uid'			=> '',
   246 			'forum_topics_per_page'		=> 0,
   247 			'forum_posts'				=> 0,
   248 			'forum_topics'				=> 0,
   249 			'forum_topics_real'			=> 0,
   250 			'forum_last_post_id'		=> 0,
   251 			'forum_last_poster_id'		=> 0,
   252 			'forum_last_post_subject'	=> '',
   253 			'forum_last_post_time'		=> 0,
   254 			'forum_last_poster_name'	=> '',
   255 			'forum_last_poster_colour'	=> '',
   256 			'display_on_index'			=> 1,
   257 			'enable_indexing'			=> 1,
   258 			'enable_icons'				=> 0,
   259 		);
   260 
   261 		// Now add the forums with proper left/right ids
   262 		$sql = 'SELECT left_id, right_id
   263 			FROM ' . FORUMS_TABLE . '
   264 			WHERE forum_id = ' . $cats_added[$row['cat_id']];
   265 		$_result = $db->sql_query($sql);
   266 		$cat_row = $db->sql_fetchrow($_result);
   267 		$db->sql_freeresult($_result);
   268 
   269 		$sql = 'UPDATE ' . FORUMS_TABLE . '
   270 			SET left_id = left_id + 2, right_id = right_id + 2
   271 			WHERE left_id > ' . $cat_row['right_id'];
   272 		$db->sql_query($sql);
   273 
   274 		$sql = 'UPDATE ' . FORUMS_TABLE . '
   275 			SET right_id = right_id + 2
   276 			WHERE ' . $cat_row['left_id'] . ' BETWEEN left_id AND right_id';
   277 		$db->sql_query($sql);
   278 
   279 		$sql_ary['left_id'] = (int) $cat_row['right_id'];
   280 		$sql_ary['right_id'] = (int) ($cat_row['right_id'] + 1);
   281 
   282 		$sql = 'INSERT INTO ' . FORUMS_TABLE . ' ' . $db->sql_build_array('INSERT', $sql_ary);
   283 		$db->sql_query($sql);
   284 	}
   285 	$src_db->sql_freeresult($result);
   286 
   287 	switch ($db->sql_layer)
   288 	{
   289 		case 'postgres':
   290 			$db->sql_query("SELECT SETVAL('" . FORUMS_TABLE . "_seq',(select case when max(forum_id)>0 then max(forum_id)+1 else 1 end from " . FORUMS_TABLE . '));');
   291 		break;
   292 
   293 		case 'mssql':
   294 		case 'mssql_odbc':
   295 		case 'mssqlnative':
   296 			$db->sql_query('SET IDENTITY_INSERT ' . FORUMS_TABLE . ' OFF');
   297 		break;
   298 
   299 		case 'oracle':
   300 			$result = $db->sql_query('SELECT MAX(forum_id) as max_id FROM ' . FORUMS_TABLE);
   301 			$row = $db->sql_fetchrow($result);
   302 			$db->sql_freeresult($result);
   303 
   304 			$largest_id = (int) $row['max_id'];
   305 
   306 			if ($largest_id)
   307 			{
   308 				$db->sql_query('DROP SEQUENCE ' . FORUMS_TABLE . '_seq');
   309 				$db->sql_query('CREATE SEQUENCE ' . FORUMS_TABLE . '_seq START WITH ' . ($largest_id + 1));
   310 			}
   311 		break;
   312 	}
   313 }
   314 
   315 /**
   316 * Function for recoding text with the default language
   317 *
   318 * @param string $text text to recode to utf8
   319 * @param bool $grab_user_lang if set to true the function tries to use $convert_row['user_lang'] (and falls back to $convert_row['poster_id']) instead of the boards default language
   320 */
   321 function phpbb_set_encoding($text, $grab_user_lang = true)
   322 {
   323 	global $lang_enc_array, $convert_row;
   324 	global $convert, $phpEx;
   325 
   326 	/*static $lang_enc_array = array(
   327 		'korean'						=> 'euc-kr',
   328 		'serbian'						=> 'windows-1250',
   329 		'polish'						=> 'iso-8859-2',
   330 		'kurdish'						=> 'windows-1254',
   331 		'slovak'						=> 'Windows-1250',
   332 		'russian'						=> 'windows-1251',
   333 		'estonian'						=> 'iso-8859-4',
   334 		'chinese_simplified'			=> 'gb2312',
   335 		'macedonian'					=> 'windows-1251',
   336 		'azerbaijani'					=> 'UTF-8',
   337 		'romanian'						=> 'iso-8859-2',
   338 		'romanian_diacritice'			=> 'iso-8859-2',
   339 		'lithuanian'					=> 'windows-1257',
   340 		'turkish'						=> 'iso-8859-9',
   341 		'ukrainian'						=> 'windows-1251',
   342 		'japanese'						=> 'shift_jis',
   343 		'hungarian'						=> 'ISO-8859-2',
   344 		'romanian_no_diacritics'		=> 'iso-8859-2',
   345 		'mongolian'						=> 'UTF-8',
   346 		'slovenian'						=> 'windows-1250',
   347 		'bosnian'						=> 'windows-1250',
   348 		'czech'							=> 'Windows-1250',
   349 		'farsi'							=> 'Windows-1256',
   350 		'croatian'						=> 'windows-1250',
   351 		'greek'							=> 'iso-8859-7',
   352 		'russian_tu'					=> 'windows-1251',
   353 		'sakha'							=> 'UTF-8',
   354 		'serbian_cyrillic'				=> 'windows-1251',
   355 		'bulgarian'						=> 'windows-1251',
   356 		'chinese_traditional_taiwan'	=> 'big5',
   357 		'chinese_traditional'			=> 'big5',
   358 		'arabic'						=> 'windows-1256',
   359 		'hebrew'						=> 'WINDOWS-1255',
   360 		'thai'							=> 'windows-874',
   361 		//'chinese_traditional_taiwan'	=> 'utf-8' // custom modified, we may have to do an include :-(
   362 	);*/
   363 
   364 	if (empty($lang_enc_array))
   365 	{
   366 		$lang_enc_array = array();
   367 	}
   368 
   369 	$get_lang = trim(get_config_value('default_lang'));
   370 
   371 	// Do we need the users language encoding?
   372 	if ($grab_user_lang && !empty($convert_row))
   373 	{
   374 		if (!empty($convert_row['user_lang']))
   375 		{
   376 			$get_lang = trim($convert_row['user_lang']);
   377 		}
   378 		else if (!empty($convert_row['poster_id']))
   379 		{
   380 			global $src_db, $same_db;
   381 
   382 			if ($convert->mysql_convert && $same_db)
   383 			{
   384 				$src_db->sql_query("SET NAMES 'binary'");
   385 			}
   386 
   387 			$sql = 'SELECT user_lang
   388 				FROM ' . $convert->src_table_prefix . 'users
   389 				WHERE user_id = ' . (int) $convert_row['poster_id'];
   390 			$result = $src_db->sql_query($sql);
   391 			$get_lang = (string) $src_db->sql_fetchfield('user_lang');
   392 			$src_db->sql_freeresult($result);
   393 
   394 			if ($convert->mysql_convert && $same_db)
   395 			{
   396 				$src_db->sql_query("SET NAMES 'utf8'");
   397 			}
   398 
   399 			$get_lang = (!trim($get_lang)) ? trim(get_config_value('default_lang')) : trim($get_lang);
   400 		}
   401 	}
   402 
   403 	if (!isset($lang_enc_array[$get_lang]))
   404 	{
   405 		$filename = $convert->options['forum_path'] . '/language/lang_' . $get_lang . '/lang_main.' . $phpEx;
   406 
   407 		if (!file_exists($filename))
   408 		{
   409 			$get_lang = trim(get_config_value('default_lang'));
   410 		}
   411 
   412 		if (!isset($lang_enc_array[$get_lang]))
   413 		{
   414 			include($convert->options['forum_path'] . '/language/lang_' . $get_lang . '/lang_main.' . $phpEx);
   415 			$lang_enc_array[$get_lang] = $lang['ENCODING'];
   416 			unset($lang);
   417 		}
   418 	}
   419 
   420 	$encoding = $lang_enc_array[$get_lang];
   421 
   422 	return utf8_recode($text, $lang_enc_array[$get_lang]);
   423 }
   424 
   425 /**
   426 * Same as phpbb_set_encoding, but forcing boards default language
   427 */
   428 function phpbb_set_default_encoding($text)
   429 {
   430 	return phpbb_set_encoding($text, false);
   431 }
   432 
   433 /**
   434 * Convert Birthday from Birthday MOD to phpBB Format
   435 */
   436 function phpbb_get_birthday($birthday = '')
   437 {
   438 	if (defined('MOD_BIRTHDAY_TERRA'))
   439 	{
   440 		$birthday = (string) $birthday;
   441 
   442 		// stored as month, day, year
   443 		if (!$birthday)
   444 		{
   445 			return ' 0- 0-   0';
   446 		}
   447 
   448 		// We use the original mod code to retrieve the birthday (not ideal)
   449 		preg_match('/(..)(..)(....)/', sprintf('%08d', $birthday), $birthday_parts);
   450 
   451 		$month = $birthday_parts[1];
   452 		$day = $birthday_parts[2];
   453 		$year =  $birthday_parts[3];
   454 
   455 		return sprintf('%2d-%2d-%4d', $day, $month, $year);
   456 	}
   457 	else
   458 	{
   459 		$birthday = (int) $birthday;
   460 
   461 		if (!$birthday || $birthday == 999999 || ((version_compare(PHP_VERSION, '5.1.0') < 0) && $birthday < 0))
   462 		{
   463 			return ' 0- 0-   0';
   464 		}
   465 
   466 		// The birthday mod from niels is using this code to transform to day/month/year
   467 		return sprintf('%2d-%2d-%4d', gmdate('j', $birthday * 86400 + 1), gmdate('n', $birthday * 86400 + 1), gmdate('Y', $birthday * 86400 + 1));
   468 	}
   469 }
   470 
   471 /**
   472 * Return correct user id value
   473 * Everyone's id will be one higher to allow the guest/anonymous user to have a positive id as well
   474 */
   475 function phpbb_user_id($user_id)
   476 {
   477 	global $config;
   478 
   479 	// Increment user id if the old forum is having a user with the id 1
   480 	if (!isset($config['increment_user_id']))
   481 	{
   482 		global $src_db, $same_db, $convert;
   483 
   484 		if ($convert->mysql_convert && $same_db)
   485 		{
   486 			$src_db->sql_query("SET NAMES 'binary'");
   487 		}
   488 
   489 		// Now let us set a temporary config variable for user id incrementing
   490 		$sql = "SELECT user_id
   491 			FROM {$convert->src_table_prefix}users
   492 			WHERE user_id = 1";
   493 		$result = $src_db->sql_query($sql);
   494 		$id = (int) $src_db->sql_fetchfield('user_id');
   495 		$src_db->sql_freeresult($result);
   496 
   497 		// Try to get the maximum user id possible...
   498 		$sql = "SELECT MAX(user_id) AS max_user_id
   499 			FROM {$convert->src_table_prefix}users";
   500 		$result = $src_db->sql_query($sql);
   501 		$max_id = (int) $src_db->sql_fetchfield('max_user_id');
   502 		$src_db->sql_freeresult($result);
   503 
   504 		if ($convert->mysql_convert && $same_db)
   505 		{
   506 			$src_db->sql_query("SET NAMES 'utf8'");
   507 		}
   508 
   509 		// If there is a user id 1, we need to increment user ids. :/
   510 		if ($id === 1)
   511 		{
   512 			set_config('increment_user_id', ($max_id + 1), true);
   513 			$config['increment_user_id'] = $max_id + 1;
   514 		}
   515 		else
   516 		{
   517 			set_config('increment_user_id', 0, true);
   518 			$config['increment_user_id'] = 0;
   519 		}
   520 	}
   521 
   522 	// If the old user id is -1 in 2.0.x it is the anonymous user...
   523 	if ($user_id == -1)
   524 	{
   525 		return ANONYMOUS;
   526 	}
   527 
   528 	if (!empty($config['increment_user_id']) && $user_id == 1)
   529 	{
   530 		return $config['increment_user_id'];
   531 	}
   532 
   533 	// A user id of 0 can happen, for example within the ban table if no user is banned...
   534 	// Within the posts and topics table this can be "dangerous" but is the fault of the user
   535 	// having mods installed (a poster id of 0 is not possible in 2.0.x).
   536 	// Therefore, we return the user id "as is".
   537 
   538 	return (int) $user_id;
   539 }
   540 
   541 /* Copy additional table fields from old forum to new forum if user wants this (for Mod compatibility for example)
   542 function phpbb_copy_table_fields()
   543 {
   544 }
   545 */
   546 
   547 /**
   548 * Convert authentication
   549 * user, group and forum table has to be filled in order to work
   550 */
   551 function phpbb_convert_authentication($mode)
   552 {
   553 	global $db, $src_db, $same_db, $convert, $user, $config, $cache;
   554 
   555 	if ($mode == 'start')
   556 	{
   557 		$db->sql_query($convert->truncate_statement . ACL_USERS_TABLE);
   558 		$db->sql_query($convert->truncate_statement . ACL_GROUPS_TABLE);
   559 
   560 		// What we will do is handling all 2.0.x admins as founder to replicate what is common in 2.0.x.
   561 		// After conversion the main admin need to make sure he is removing permissions and the founder status if wanted.
   562 
   563 
   564 		// Grab user ids of users with user_level of ADMIN
   565 		$sql = "SELECT user_id
   566 			FROM {$convert->src_table_prefix}users
   567 			WHERE user_level = 1
   568 			ORDER BY user_regdate ASC";
   569 		$result = $src_db->sql_query($sql);
   570 
   571 		while ($row = $src_db->sql_fetchrow($result))
   572 		{
   573 			$user_id = (int) phpbb_user_id($row['user_id']);
   574 			// Set founder admin...
   575 			$sql = 'UPDATE ' . USERS_TABLE . '
   576 				SET user_type = ' . USER_FOUNDER . "
   577 				WHERE user_id = $user_id";
   578 			$db->sql_query($sql);
   579 		}
   580 		$src_db->sql_freeresult($result);
   581 
   582 		$sql = 'SELECT group_id
   583 			FROM ' . GROUPS_TABLE . "
   584 			WHERE group_name = '" . $db->sql_escape('BOTS') . "'";
   585 		$result = $db->sql_query($sql);
   586 		$bot_group_id = (int) $db->sql_fetchfield('group_id');
   587 		$db->sql_freeresult($result);
   588 	}
   589 
   590 	// Grab forum auth information
   591 	$sql = "SELECT *
   592 		FROM {$convert->src_table_prefix}forums";
   593 	$result = $src_db->sql_query($sql);
   594 
   595 	$forum_access = array();
   596 	while ($row = $src_db->sql_fetchrow($result))
   597 	{
   598 		$forum_access[$row['forum_id']] = $row;
   599 	}
   600 	$src_db->sql_freeresult($result);
   601 
   602 	if ($convert->mysql_convert && $same_db)
   603 	{
   604 		$src_db->sql_query("SET NAMES 'binary'");
   605 	}
   606 	// Grab user auth information from 2.0.x board
   607 	$sql = "SELECT ug.user_id, aa.*
   608 		FROM {$convert->src_table_prefix}auth_access aa, {$convert->src_table_prefix}user_group ug, {$convert->src_table_prefix}groups g, {$convert->src_table_prefix}forums f
   609 		WHERE g.group_id = aa.group_id
   610 			AND g.group_single_user = 1
   611 			AND ug.group_id = g.group_id
   612 			AND f.forum_id = aa.forum_id";
   613 	$result = $src_db->sql_query($sql);
   614 
   615 	$user_access = array();
   616 	while ($row = $src_db->sql_fetchrow($result))
   617 	{
   618 		$user_access[$row['forum_id']][] = $row;
   619 	}
   620 	$src_db->sql_freeresult($result);
   621 
   622 	// Grab group auth information
   623 	$sql = "SELECT g.group_id, aa.*
   624 		FROM {$convert->src_table_prefix}auth_access aa, {$convert->src_table_prefix}groups g
   625 		WHERE g.group_id = aa.group_id
   626 			AND g.group_single_user <> 1";
   627 	$result = $src_db->sql_query($sql);
   628 
   629 	$group_access = array();
   630 	while ($row = $src_db->sql_fetchrow($result))
   631 	{
   632 		$group_access[$row['forum_id']][] = $row;
   633 	}
   634 	$src_db->sql_freeresult($result);
   635 
   636 	if ($convert->mysql_convert && $same_db)
   637 	{
   638 		$src_db->sql_query("SET NAMES 'utf8'");
   639 	}
   640 
   641 	// Add Forum Access List
   642 	$auth_map = array(
   643 		'auth_view'			=> array('f_', 'f_list'),
   644 		'auth_read'			=> array('f_read', 'f_search'),
   645 		'auth_post'			=> array('f_post', 'f_bbcode', 'f_smilies', 'f_img', 'f_sigs', 'f_postcount', 'f_report', 'f_subscribe', 'f_print', 'f_email'),
   646 		'auth_reply'		=> 'f_reply',
   647 		'auth_edit'			=> 'f_edit',
   648 		'auth_delete'		=> 'f_delete',
   649 		'auth_pollcreate'	=> 'f_poll',
   650 		'auth_vote'			=> 'f_vote',
   651 		'auth_announce'		=> 'f_announce',
   652 		'auth_sticky'		=> 'f_sticky',
   653 		'auth_attachments'	=> array('f_attach', 'f_download'),
   654 		'auth_download'		=> 'f_download',
   655 	);
   656 
   657 	// Define the ACL constants used in 2.0 to make the code slightly more readable
   658 	define('AUTH_ALL', 0);
   659 	define('AUTH_REG', 1);
   660 	define('AUTH_ACL', 2);
   661 	define('AUTH_MOD', 3);
   662 	define('AUTH_ADMIN', 5);
   663 
   664 	// A mapping of the simple permissions used by 2.0
   665 	$simple_auth_ary = array(
   666 		'public'			=> array(
   667 			'auth_view'			=> AUTH_ALL,
   668 			'auth_read'			=> AUTH_ALL,
   669 			'auth_post'			=> AUTH_ALL,
   670 			'auth_reply'		=> AUTH_ALL,
   671 			'auth_edit'			=> AUTH_REG,
   672 			'auth_delete'		=> AUTH_REG,
   673 			'auth_sticky'		=> AUTH_MOD,
   674 			'auth_announce'		=> AUTH_MOD,
   675 			'auth_vote'			=> AUTH_REG,
   676 			'auth_pollcreate'	=> AUTH_REG,
   677 		),
   678 		'registered'		=> array(
   679 			'auth_view'			=> AUTH_ALL,
   680 			'auth_read'			=> AUTH_ALL,
   681 			'auth_post'			=> AUTH_REG,
   682 			'auth_reply'		=> AUTH_REG,
   683 			'auth_edit'			=> AUTH_REG,
   684 			'auth_delete'		=> AUTH_REG,
   685 			'auth_sticky'		=> AUTH_MOD,
   686 			'auth_announce'		=> AUTH_MOD,
   687 			'auth_vote'			=> AUTH_REG,
   688 			'auth_pollcreate'	=> AUTH_REG,
   689 		),
   690 		'registered_hidden'	=> array(
   691 			'auth_view'			=> AUTH_REG,
   692 			'auth_read'			=> AUTH_REG,
   693 			'auth_post'			=> AUTH_REG,
   694 			'auth_reply'		=> AUTH_REG,
   695 			'auth_edit'			=> AUTH_REG,
   696 			'auth_delete'		=> AUTH_REG,
   697 			'auth_sticky'		=> AUTH_MOD,
   698 			'auth_announce'		=> AUTH_MOD,
   699 			'auth_vote'			=> AUTH_REG,
   700 			'auth_pollcreate'	=> AUTH_REG,
   701 		),
   702 		'private'			=> array(
   703 			'auth_view'			=> AUTH_ALL,
   704 			'auth_read'			=> AUTH_ACL,
   705 			'auth_post'			=> AUTH_ACL,
   706 			'auth_reply'		=> AUTH_ACL,
   707 			'auth_edit'			=> AUTH_ACL,
   708 			'auth_delete'		=> AUTH_ACL,
   709 			'auth_sticky'		=> AUTH_ACL,
   710 			'auth_announce'		=> AUTH_MOD,
   711 			'auth_vote'			=> AUTH_ACL,
   712 			'auth_pollcreate'	=> AUTH_ACL,
   713 		),
   714 		'private_hidden'	=> array(
   715 			'auth_view'			=> AUTH_ACL,
   716 			'auth_read'			=> AUTH_ACL,
   717 			'auth_post'			=> AUTH_ACL,
   718 			'auth_reply'		=> AUTH_ACL,
   719 			'auth_edit'			=> AUTH_ACL,
   720 			'auth_delete'		=> AUTH_ACL,
   721 			'auth_sticky'		=> AUTH_ACL,
   722 			'auth_announce'		=> AUTH_MOD,
   723 			'auth_vote'			=> AUTH_ACL,
   724 			'auth_pollcreate'	=> AUTH_ACL,
   725 		),
   726 		'moderator'			=> array(
   727 			'auth_view'			=> AUTH_ALL,
   728 			'auth_read'			=> AUTH_MOD,
   729 			'auth_post'			=> AUTH_MOD,
   730 			'auth_reply'		=> AUTH_MOD,
   731 			'auth_edit'			=> AUTH_MOD,
   732 			'auth_delete'		=> AUTH_MOD,
   733 			'auth_sticky'		=> AUTH_MOD,
   734 			'auth_announce'		=> AUTH_MOD,
   735 			'auth_vote'			=> AUTH_MOD,
   736 			'auth_pollcreate'	=> AUTH_MOD,
   737 		),
   738 		'moderator_hidden'	=> array(
   739 			'auth_view'			=> AUTH_MOD,
   740 			'auth_read'			=> AUTH_MOD,
   741 			'auth_post'			=> AUTH_MOD,
   742 			'auth_reply'		=> AUTH_MOD,
   743 			'auth_edit'			=> AUTH_MOD,
   744 			'auth_delete'		=> AUTH_MOD,
   745 			'auth_sticky'		=> AUTH_MOD,
   746 			'auth_announce'		=> AUTH_MOD,
   747 			'auth_vote'			=> AUTH_MOD,
   748 			'auth_pollcreate'	=> AUTH_MOD,
   749 		),
   750 	);
   751 
   752 	if ($mode == 'start')
   753 	{
   754 		user_group_auth('guests', 'SELECT user_id, {GUESTS} FROM ' . USERS_TABLE . ' WHERE user_id = ' . ANONYMOUS, false);
   755 		user_group_auth('registered', 'SELECT user_id, {REGISTERED} FROM ' . USERS_TABLE . ' WHERE user_id <> ' . ANONYMOUS . " AND group_id <> $bot_group_id", false);
   756 
   757 		// Selecting from old table
   758 		if (!empty($config['increment_user_id']))
   759 		{
   760 			$auth_sql = 'SELECT user_id, {ADMINISTRATORS} FROM ' . $convert->src_table_prefix . 'users WHERE user_level = 1 AND user_id <> 1';
   761 			user_group_auth('administrators', $auth_sql, true);
   762 
   763 			$auth_sql = 'SELECT ' . $config['increment_user_id'] . ' as user_id, {ADMINISTRATORS} FROM ' . $convert->src_table_prefix . 'users WHERE user_level = 1 AND user_id = 1';
   764 			user_group_auth('administrators', $auth_sql, true);
   765 		}
   766 		else
   767 		{
   768 			$auth_sql = 'SELECT user_id, {ADMINISTRATORS} FROM ' . $convert->src_table_prefix . 'users WHERE user_level = 1';
   769 			user_group_auth('administrators', $auth_sql, true);
   770 		}
   771 
   772 		if (!empty($config['increment_user_id']))
   773 		{
   774 			$auth_sql = 'SELECT user_id, {GLOBAL_MODERATORS} FROM ' . $convert->src_table_prefix . 'users WHERE user_level = 1 AND user_id <> 1';
   775 			user_group_auth('global_moderators', $auth_sql, true);
   776 
   777 			$auth_sql = 'SELECT ' . $config['increment_user_id'] . ' as user_id, {GLOBAL_MODERATORS} FROM ' . $convert->src_table_prefix . 'users WHERE user_level = 1 AND user_id = 1';
   778 			user_group_auth('global_moderators', $auth_sql, true);
   779 		}
   780 		else
   781 		{
   782 			$auth_sql = 'SELECT user_id, {GLOBAL_MODERATORS} FROM ' . $convert->src_table_prefix . 'users WHERE user_level = 1';
   783 			user_group_auth('global_moderators', $auth_sql, true);
   784 		}
   785 	}
   786 	else if ($mode == 'first')
   787 	{
   788 		// Go through all 2.0.x forums
   789 		foreach ($forum_access as $forum)
   790 		{
   791 			$new_forum_id = (int) $forum['forum_id'];
   792 
   793 			// Administrators have full access to all forums whatever happens
   794 			mass_auth('group_role', $new_forum_id, 'administrators', 'FORUM_FULL');
   795 
   796 			$matched_type = '';
   797 			foreach ($simple_auth_ary as $key => $auth_levels)
   798 			{
   799 				$matched = 1;
   800 				foreach ($auth_levels as $k => $level)
   801 				{
   802 					if ($forum[$k] != $auth_levels[$k])
   803 					{
   804 						$matched = 0;
   805 					}
   806 				}
   807 
   808 				if ($matched)
   809 				{
   810 					$matched_type = $key;
   811 					break;
   812 				}
   813 			}
   814 
   815 			switch ($matched_type)
   816 			{
   817 				case 'public':
   818 					mass_auth('group_role', $new_forum_id, 'guests', 'FORUM_LIMITED');
   819 					mass_auth('group_role', $new_forum_id, 'registered', 'FORUM_LIMITED_POLLS');
   820 					mass_auth('group_role', $new_forum_id, 'bots', 'FORUM_BOT');
   821 				break;
   822 
   823 				case 'registered':
   824 					mass_auth('group_role', $new_forum_id, 'guests', 'FORUM_READONLY');
   825 					mass_auth('group_role', $new_forum_id, 'bots', 'FORUM_BOT');
   826 
   827 				// no break;
   828 
   829 				case 'registered_hidden':
   830 					mass_auth('group_role', $new_forum_id, 'registered', 'FORUM_POLLS');
   831 				break;
   832 
   833 				case 'private':
   834 				case 'private_hidden':
   835 				case 'moderator':
   836 				case 'moderator_hidden':
   837 				default:
   838 					// The permissions don't match a simple set, so we're going to have to map them directly
   839 
   840 					// No post approval for all, in 2.0.x this feature does not exist
   841 					mass_auth('group', $new_forum_id, 'guests', 'f_noapprove', ACL_YES);
   842 					mass_auth('group', $new_forum_id, 'registered', 'f_noapprove', ACL_YES);
   843 
   844 					// Go through authentication map
   845 					foreach ($auth_map as $old_auth_key => $new_acl)
   846 					{
   847 						// If old authentication key does not exist we continue
   848 						// This is helpful for mods adding additional authentication fields, we need to add them to the auth_map array
   849 						if (!isset($forum[$old_auth_key]))
   850 						{
   851 							continue;
   852 						}
   853 
   854 						// Now set the new ACL correctly
   855 						switch ($forum[$old_auth_key])
   856 						{
   857 							// AUTH_ALL
   858 							case AUTH_ALL:
   859 								mass_auth('group', $new_forum_id, 'guests', $new_acl, ACL_YES);
   860 								mass_auth('group', $new_forum_id, 'bots', $new_acl, ACL_YES);
   861 								mass_auth('group', $new_forum_id, 'registered', $new_acl, ACL_YES);
   862 							break;
   863 
   864 							// AUTH_REG
   865 							case AUTH_REG:
   866 								mass_auth('group', $new_forum_id, 'registered', $new_acl, ACL_YES);
   867 							break;
   868 
   869 							// AUTH_ACL
   870 							case AUTH_ACL:
   871 								// Go through the old group access list for this forum
   872 								if (isset($group_access[$forum['forum_id']]))
   873 								{
   874 									foreach ($group_access[$forum['forum_id']] as $index => $access)
   875 									{
   876 										// We only check for ACL_YES equivalence entry
   877 										if (isset($access[$old_auth_key]) && $access[$old_auth_key] == 1)
   878 										{
   879 											mass_auth('group', $new_forum_id, (int) $access['group_id'], $new_acl, ACL_YES);
   880 										}
   881 									}
   882 								}
   883 
   884 								if (isset($user_access[$forum['forum_id']]))
   885 								{
   886 									foreach ($user_access[$forum['forum_id']] as $index => $access)
   887 									{
   888 										// We only check for ACL_YES equivalence entry
   889 										if (isset($access[$old_auth_key]) && $access[$old_auth_key] == 1)
   890 										{
   891 											mass_auth('user', $new_forum_id, (int) phpbb_user_id($access['user_id']), $new_acl, ACL_YES);
   892 										}
   893 									}
   894 								}
   895 							break;
   896 
   897 							// AUTH_MOD
   898 							case AUTH_MOD:
   899 								if (isset($group_access[$forum['forum_id']]))
   900 								{
   901 									foreach ($group_access[$forum['forum_id']] as $index => $access)
   902 									{
   903 										// We only check for ACL_YES equivalence entry
   904 										if (isset($access[$old_auth_key]) && $access[$old_auth_key] == 1)
   905 										{
   906 											mass_auth('group', $new_forum_id, (int) $access['group_id'], $new_acl, ACL_YES);
   907 										}
   908 									}
   909 								}
   910 
   911 								if (isset($user_access[$forum['forum_id']]))
   912 								{
   913 									foreach ($user_access[$forum['forum_id']] as $index => $access)
   914 									{
   915 										// We only check for ACL_YES equivalence entry
   916 										if (isset($access[$old_auth_key]) && $access[$old_auth_key] == 1)
   917 										{
   918 											mass_auth('user', $new_forum_id, (int) phpbb_user_id($access['user_id']), $new_acl, ACL_YES);
   919 										}
   920 									}
   921 								}
   922 							break;
   923 						}
   924 					}
   925 				break;
   926 			}
   927 		}
   928 	}
   929 	else if ($mode == 'second')
   930 	{
   931 		// Assign permission roles and other default permissions
   932 
   933 		// guests having u_download and u_search ability
   934 		$db->sql_query('INSERT INTO ' . ACL_GROUPS_TABLE . ' (group_id, forum_id, auth_option_id, auth_role_id, auth_setting) SELECT ' . get_group_id('guests') . ', 0, auth_option_id, 0, 1 FROM ' . ACL_OPTIONS_TABLE . " WHERE auth_option IN ('u_', 'u_download', 'u_search')");
   935 
   936 		// administrators/global mods having full user features
   937 		mass_auth('group_role', 0, 'administrators', 'USER_FULL');
   938 		mass_auth('group_role', 0, 'global_moderators', 'USER_FULL');
   939 
   940 		// By default all converted administrators are given full access
   941 		mass_auth('group_role', 0, 'administrators', 'ADMIN_FULL');
   942 
   943 		// All registered users are assigned the standard user role
   944 		mass_auth('group_role', 0, 'registered', 'USER_STANDARD');
   945 		mass_auth('group_role', 0, 'registered_coppa', 'USER_STANDARD');
   946 
   947 		// Instead of administrators being global moderators we give the MOD_FULL role to global mods (admins already assigned to this group)
   948 		mass_auth('group_role', 0, 'global_moderators', 'MOD_FULL');
   949 
   950 		// And now those who have had their avatar rights removed get assigned a more restrictive role
   951 		$sql = 'SELECT user_id FROM ' . $convert->src_table_prefix . 'users
   952 			WHERE user_allowavatar = 0
   953 				AND user_id > 0';
   954 		$result = $src_db->sql_query($sql);
   955 
   956 		while ($row = $src_db->sql_fetchrow($result))
   957 		{
   958 			mass_auth('user_role', 0, (int) phpbb_user_id($row['user_id']), 'USER_NOAVATAR');
   959 		}
   960 		$src_db->sql_freeresult($result);
   961 
   962 		// And the same for those who have had their PM rights removed
   963 		$sql = 'SELECT user_id FROM ' . $convert->src_table_prefix . 'users
   964 			WHERE user_allow_pm = 0
   965 				AND user_id > 0';
   966 		$result = $src_db->sql_query($sql);
   967 
   968 		while ($row = $src_db->sql_fetchrow($result))
   969 		{
   970 			mass_auth('user_role', 0, (int) phpbb_user_id($row['user_id']), 'USER_NOPM');
   971 		}
   972 		$src_db->sql_freeresult($result);
   973 	}
   974 	else if ($mode == 'third')
   975 	{
   976 		// And now the moderators
   977 		// We make sure that they have at least standard access to the forums they moderate in addition to the moderating permissions
   978 
   979 		$mod_post_map = array(
   980 			'auth_announce'		=> 'f_announce',
   981 			'auth_sticky'		=> 'f_sticky'
   982 		);
   983 
   984 		foreach ($user_access as $forum_id => $access_map)
   985 		{
   986 			$forum_id = (int) $forum_id;
   987 
   988 			foreach ($access_map as $access)
   989 			{
   990 				if (isset($access['auth_mod']) && $access['auth_mod'] == 1)
   991 				{
   992 					mass_auth('user_role', $forum_id, (int) phpbb_user_id($access['user_id']), 'MOD_STANDARD');
   993 					mass_auth('user_role', $forum_id, (int) phpbb_user_id($access['user_id']), 'FORUM_STANDARD');
   994 					foreach ($mod_post_map as $old => $new)
   995 					{
   996 						if (isset($forum_access[$forum_id]) && isset($forum_access[$forum_id][$old]) && $forum_access[$forum_id][$old] == AUTH_MOD)
   997 						{
   998 							mass_auth('user', $forum_id, (int) phpbb_user_id($access['user_id']), $new, ACL_YES);
   999 						}
  1000 					}
  1001 				}
  1002 			}
  1003 		}
  1004 
  1005 		foreach ($group_access as $forum_id => $access_map)
  1006 		{
  1007 			$forum_id = (int) $forum_id;
  1008 
  1009 			foreach ($access_map as $access)
  1010 			{
  1011 				if (isset($access['auth_mod']) && $access['auth_mod'] == 1)
  1012 				{
  1013 					mass_auth('group_role', $forum_id, (int) $access['group_id'], 'MOD_STANDARD');
  1014 					mass_auth('group_role', $forum_id, (int) $access['group_id'], 'FORUM_STANDARD');
  1015 					foreach ($mod_post_map as $old => $new)
  1016 					{
  1017 						if (isset($forum_access[$forum_id]) && isset($forum_access[$forum_id][$old]) && $forum_access[$forum_id][$old] == AUTH_MOD)
  1018 						{
  1019 							mass_auth('group', $forum_id, (int) $access['group_id'], $new, ACL_YES);
  1020 						}
  1021 					}
  1022 				}
  1023 			}
  1024 		}
  1025 
  1026 		// We grant everyone readonly access to the categories to ensure that the forums are visible
  1027 		$sql = 'SELECT forum_id, forum_name, parent_id, left_id, right_id
  1028 			FROM ' . FORUMS_TABLE . '
  1029 			ORDER BY left_id ASC';
  1030 		$result = $db->sql_query($sql);
  1031 
  1032 		$parent_forums = $forums = array();
  1033 		while ($row = $db->sql_fetchrow($result))
  1034 		{
  1035 			if ($row['parent_id'] == 0)
  1036 			{
  1037 				mass_auth('group_role', $row['forum_id'], 'administrators', 'FORUM_FULL');
  1038 				mass_auth('group_role', $row['forum_id'], 'global_moderators', 'FORUM_FULL');
  1039 				$parent_forums[] = $row;
  1040 			}
  1041 			else
  1042 			{
  1043 				$forums[] = $row;
  1044 			}
  1045 		}
  1046 		$db->sql_freeresult($result);
  1047 
  1048 		global $auth;
  1049 
  1050 		// Let us see which groups have access to these forums...
  1051 		foreach ($parent_forums as $row)
  1052 		{
  1053 			// Get the children
  1054 			$branch = $forum_ids = array();
  1055 
  1056 			foreach ($forums as $key => $_row)
  1057 			{
  1058 				if ($_row['left_id'] > $row['left_id'] && $_row['left_id'] < $row['right_id'])
  1059 				{
  1060 					$branch[] = $_row;
  1061 					$forum_ids[] = $_row['forum_id'];
  1062 					continue;
  1063 				}
  1064 			}
  1065 
  1066 			if (sizeof($forum_ids))
  1067 			{
  1068 				// Now make sure the user is able to read these forums
  1069 				$hold_ary = $auth->acl_group_raw_data(false, 'f_list', $forum_ids);
  1070 
  1071 				if (empty($hold_ary))
  1072 				{
  1073 					continue;
  1074 				}
  1075 
  1076 				foreach ($hold_ary as $g_id => $f_id_ary)
  1077 				{
  1078 					$set_group = false;
  1079 
  1080 					foreach ($f_id_ary as $f_id => $auth_ary)
  1081 					{
  1082 						foreach ($auth_ary as $auth_option => $setting)
  1083 						{
  1084 							if ($setting == ACL_YES)
  1085 							{
  1086 								$set_group = true;
  1087 								break 2;
  1088 							}
  1089 						}
  1090 					}
  1091 
  1092 					if ($set_group)
  1093 					{
  1094 						mass_auth('group', $row['forum_id'], $g_id, 'f_list', ACL_YES);
  1095 					}
  1096 				}
  1097 			}
  1098 		}
  1099 	}
  1100 }
  1101 
  1102 /**
  1103 * Set primary group.
  1104 * Really simple and only based on user_level (remaining groups will be assigned later)
  1105 */
  1106 function phpbb_set_primary_group($user_level)
  1107 {
  1108 	global $convert_row;
  1109 
  1110 	if ($user_level == 1)
  1111 	{
  1112 		return get_group_id('administrators');
  1113 	}
  1114 /*	else if ($user_level == 2)
  1115 	{
  1116 		return get_group_id('global_moderators');
  1117 	}
  1118 	else if ($user_level == 0 && $convert_row['user_active'])*/
  1119 	else if ($convert_row['user_active'])
  1120 	{
  1121 		return get_group_id('registered');
  1122 	}
  1123 
  1124 	return 0;
  1125 }
  1126 
  1127 /**
  1128 * Convert the group name, making sure to avoid conflicts with 3.0 special groups
  1129 */
  1130 function phpbb_convert_group_name($group_name)
  1131 {
  1132 	$default_groups = array(
  1133 		'GUESTS',
  1134 		'REGISTERED',
  1135 		'REGISTERED_COPPA',
  1136 		'GLOBAL_MODERATORS',
  1137 		'ADMINISTRATORS',
  1138 		'BOTS',
  1139 	);
  1140 
  1141 	if (in_array(strtoupper($group_name), $default_groups))
  1142 	{
  1143 		return 'phpBB2 - ' . $group_name;
  1144 	}
  1145 
  1146 	return phpbb_set_default_encoding($group_name);
  1147 }
  1148 
  1149 /**
  1150 * Convert the group type constants
  1151 */
  1152 function phpbb_convert_group_type($group_type)
  1153 {
  1154 	switch ($group_type)
  1155 	{
  1156 		case 0:
  1157 			return GROUP_OPEN;
  1158 		break;
  1159 
  1160 		case 1:
  1161 			return GROUP_CLOSED;
  1162 		break;
  1163 
  1164 		case 2:
  1165 			return GROUP_HIDDEN;
  1166 		break;
  1167 	}
  1168 
  1169 	// Never return GROUP_SPECIAL here, because only phpBB3's default groups are allowed to have this type set.
  1170 	return GROUP_HIDDEN;
  1171 }
  1172 
  1173 /**
  1174 * Convert the topic type constants
  1175 */
  1176 function phpbb_convert_topic_type($topic_type)
  1177 {
  1178 	switch ($topic_type)
  1179 	{
  1180 		case 0:
  1181 			return POST_NORMAL;
  1182 		break;
  1183 
  1184 		case 1:
  1185 			return POST_STICKY;
  1186 		break;
  1187 
  1188 		case 2:
  1189 			return POST_ANNOUNCE;
  1190 		break;
  1191 
  1192 		case 3:
  1193 			return POST_GLOBAL;
  1194 		break;
  1195 	}
  1196 
  1197 	return POST_NORMAL;
  1198 }
  1199 
  1200 function phpbb_replace_size($matches)
  1201 {
  1202 	return '[size=' . min(200, ceil(100.0 * (((double) $matches[1])/12.0))) . ':' . $matches[2] . ']';
  1203 }
  1204 
  1205 /**
  1206 * Reparse the message stripping out the bbcode_uid values and adding new ones and setting the bitfield
  1207 * @todo What do we want to do about HTML in messages - currently it gets converted to the entities, but there may be some objections to this
  1208 */
  1209 function phpbb_prepare_message($message)
  1210 {
  1211 	global $phpbb_root_path, $phpEx, $db, $convert, $user, $config, $cache, $convert_row, $message_parser;
  1212 
  1213 	if (!$message)
  1214 	{
  1215 		$convert->row['mp_bbcode_bitfield'] = $convert_row['mp_bbcode_bitfield'] = 0;
  1216 		return '';
  1217 	}
  1218 
  1219 	// Decode phpBB 2.0.x Message
  1220 	if (isset($convert->row['old_bbcode_uid']) && $convert->row['old_bbcode_uid'] != '')
  1221 	{
  1222 		// Adjust size...
  1223 		if (strpos($message, '[size=') !== false)
  1224 		{
  1225 			$message = preg_replace_callback('/\[size=(\d*):(' . $convert->row['old_bbcode_uid'] . ')\]/', 'phpbb_replace_size', $message);
  1226 		}
  1227 
  1228 		$message = preg_replace('/\:(([a-z0-9]:)?)' . $convert->row['old_bbcode_uid'] . '/s', '', $message);
  1229 	}
  1230 
  1231 	if (strpos($message, '[quote=') !== false)
  1232 	{
  1233 		$message = preg_replace('/\[quote="(.*?)"\]/s', '[quote=&quot;\1&quot;]', $message);
  1234 		$message = preg_replace('/\[quote=\\\"(.*?)\\\"\]/s', '[quote=&quot;\1&quot;]', $message);
  1235 
  1236 		// let's hope that this solves more problems than it causes. Deal with escaped quotes.
  1237 		$message = str_replace('\"', '&quot;', $message);
  1238 		$message = str_replace('\&quot;', '&quot;', $message);
  1239 	}
  1240 
  1241 	// Already the new user id ;)
  1242 	$user_id = $convert->row['poster_id'];
  1243 
  1244 	$message = str_replace('<br />', "\n", $message);
  1245 	$message = str_replace('<', '&lt;', $message);
  1246 	$message = str_replace('>', '&gt;', $message);
  1247 
  1248 	// make the post UTF-8
  1249 	$message = phpbb_set_encoding($message);
  1250 
  1251 	$message_parser->warn_msg = array(); // Reset the errors from the previous message
  1252 	$message_parser->bbcode_uid = make_uid($convert->row['post_time']);
  1253 	$message_parser->message = $message;
  1254 	unset($message);
  1255 
  1256 	// Make sure options are set.
  1257 //	$enable_html = (!isset($row['enable_html'])) ? false : $row['enable_html'];
  1258 	$enable_bbcode = (!isset($convert->row['enable_bbcode'])) ? true : $convert->row['enable_bbcode'];
  1259 	$enable_smilies = (!isset($convert->row['enable_smilies'])) ? true : $convert->row['enable_smilies'];
  1260 	$enable_magic_url = (!isset($convert->row['enable_magic_url'])) ? true : $convert->row['enable_magic_url'];
  1261 
  1262 	// parse($allow_bbcode, $allow_magic_url, $allow_smilies, $allow_img_bbcode = true, $allow_flash_bbcode = true, $allow_quote_bbcode = true, $allow_url_bbcode = true, $update_this_message = true, $mode = 'post')
  1263 	$message_parser->parse($enable_bbcode, $enable_magic_url, $enable_smilies);
  1264 
  1265 	if (sizeof($message_parser->warn_msg))
  1266 	{
  1267 		$msg_id = isset($convert->row['post_id']) ? $convert->row['post_id'] : $convert->row['privmsgs_id'];
  1268 		$convert->p_master->error('<span style="color:red">' . $user->lang['POST_ID'] . ': ' . $msg_id . ' ' . $user->lang['CONV_ERROR_MESSAGE_PARSER'] . ': <br /><br />' . implode('<br />', $message_parser->warn_msg), __LINE__, __FILE__, true);
  1269 	}
  1270 
  1271 	$convert->row['mp_bbcode_bitfield'] = $convert_row['mp_bbcode_bitfield'] = $message_parser->bbcode_bitfield;
  1272 
  1273 	$message = $message_parser->message;
  1274 	unset($message_parser->message);
  1275 
  1276 	return $message;
  1277 }
  1278 
  1279 /**
  1280 * Return the bitfield calculated by the previous function
  1281 */
  1282 function get_bbcode_bitfield()
  1283 {
  1284 	global $convert_row;
  1285 
  1286 	return $convert_row['mp_bbcode_bitfield'];
  1287 }
  1288 
  1289 /**
  1290 * Determine the last user to edit a post
  1291 * In practice we only tracked edits by the original poster in 2.0.x so this will only be set if they had edited their own post
  1292 */
  1293 function phpbb_post_edit_user()
  1294 {
  1295 	global $convert_row, $config;
  1296 
  1297 	if (isset($convert_row['post_edit_count']))
  1298 	{
  1299 		return phpbb_user_id($convert_row['poster_id']);
  1300 	}
  1301 
  1302 	return 0;
  1303 }
  1304 
  1305 /**
  1306 * Obtain the path to uploaded files on the 2.0.x forum
  1307 * This is only used if the Attachment MOD was installed
  1308 */
  1309 function phpbb_get_files_dir()
  1310 {
  1311 	if (!defined('MOD_ATTACHMENT'))
  1312 	{
  1313 		return;
  1314 	}
  1315 
  1316 	global $src_db, $same_db, $convert, $user, $config, $cache;
  1317 
  1318 	if ($convert->mysql_convert && $same_db)
  1319 	{
  1320 		$src_db->sql_query("SET NAMES 'binary'");
  1321 	}
  1322 	$sql = 'SELECT config_value AS upload_dir
  1323 		FROM ' . $convert->src_table_prefix . "attachments_config
  1324 		WHERE config_name = 'upload_dir'";
  1325 	$result = $src_db->sql_query($sql);
  1326 	$upload_path = $src_db->sql_fetchfield('upload_dir');
  1327 	$src_db->sql_freeresult($result);
  1328 
  1329 	$sql = 'SELECT config_value AS ftp_upload
  1330 		FROM ' . $convert->src_table_prefix . "attachments_config
  1331 		WHERE config_name = 'allow_ftp_upload'";
  1332 	$result = $src_db->sql_query($sql);
  1333 	$ftp_upload = (int) $src_db->sql_fetchfield('ftp_upload');
  1334 	$src_db->sql_freeresult($result);
  1335 
  1336 	if ($convert->mysql_convert && $same_db)
  1337 	{
  1338 		$src_db->sql_query("SET NAMES 'utf8'");
  1339 	}
  1340 
  1341 	if ($ftp_upload)
  1342 	{
  1343 		$convert->p_master->error($user->lang['CONV_ERROR_ATTACH_FTP_DIR'], __LINE__, __FILE__);
  1344 	}
  1345 
  1346 	return $upload_path;
  1347 }
  1348 
  1349 /**
  1350 * Copy thumbnails of uploaded images from the 2.0.x forum
  1351 * This is only used if the Attachment MOD was installed
  1352 */
  1353 function phpbb_copy_thumbnails()
  1354 {
  1355 	global $db, $convert, $user, $config, $cache, $phpbb_root_path;
  1356 
  1357 	$src_path = $convert->options['forum_path'] . '/' . phpbb_get_files_dir() . '/thumbs/';
  1358 
  1359 	if ($handle = @opendir($src_path))
  1360 	{
  1361 		while ($entry = readdir($handle))
  1362 		{
  1363 			if ($entry[0] == '.')
  1364 			{
  1365 				continue;
  1366 			}
  1367 
  1368 			if (is_dir($src_path . $entry))
  1369 			{
  1370 				continue;
  1371 			}
  1372 			else
  1373 			{
  1374 				copy_file($src_path . $entry, $config['upload_path'] . '/' . preg_replace('/^t_/', 'thumb_', $entry));
  1375 				@unlink($phpbb_root_path . $config['upload_path'] . '/thumbs/' . $entry);
  1376 			}
  1377 		}
  1378 		closedir($handle);
  1379 	}
  1380 }
  1381 
  1382 /**
  1383 * Convert the attachment category constants
  1384 * This is only used if the Attachment MOD was installed
  1385 */
  1386 function phpbb_attachment_category($cat_id)
  1387 {
  1388 	switch ($cat_id)
  1389 	{
  1390 		case 1:
  1391 			return ATTACHMENT_CATEGORY_IMAGE;
  1392 		break;
  1393 
  1394 		case 2:
  1395 			return ATTACHMENT_CATEGORY_WM;
  1396 		break;
  1397 
  1398 		case 3:
  1399 			return ATTACHMENT_CATEGORY_FLASH;
  1400 		break;
  1401 	}
  1402 
  1403 	return ATTACHMENT_CATEGORY_NONE;
  1404 }
  1405 
  1406 /**
  1407 * Obtain list of forums in which different attachment categories can be used
  1408 */
  1409 function phpbb_attachment_forum_perms($forum_permissions)
  1410 {
  1411 	if (empty($forum_permissions))
  1412 	{
  1413 		return '';
  1414 	}
  1415 
  1416 	// Decode forum permissions
  1417 	$forum_ids = array();
  1418 
  1419 	$one_char_encoding = '#';
  1420 	$two_char_encoding = '.';
  1421 
  1422 	$auth_len = 1;
  1423 	for ($pos = 0; $pos < strlen($forum_permissions); $pos += $auth_len)
  1424 	{
  1425 		$forum_auth = substr($forum_permissions, $pos, 1);
  1426 		if ($forum_auth == $one_char_encoding)
  1427 		{
  1428 			$auth_len = 1;
  1429 			continue;
  1430 		}
  1431 		else if ($forum_auth == $two_char_encoding)
  1432 		{
  1433 			$auth_len = 2;
  1434 			$pos--;
  1435 			continue;
  1436 		}
  1437 
  1438 		$forum_auth = substr($forum_permissions, $pos, $auth_len);
  1439 		$forum_id = base64_unpack($forum_auth);
  1440 
  1441 		$forum_ids[] = (int) $forum_id;
  1442 	}
  1443 
  1444 	if (sizeof($forum_ids))
  1445 	{
  1446 		return attachment_forum_perms($forum_ids);
  1447 	}
  1448 
  1449 	return '';
  1450 }
  1451 
  1452 /**
  1453 * Convert the avatar type constants
  1454 */
  1455 function phpbb_avatar_type($type)
  1456 {
  1457 	switch ($type)
  1458 	{
  1459 		case 1:
  1460 			return AVATAR_UPLOAD;
  1461 		break;
  1462 
  1463 		case 2:
  1464 			return AVATAR_REMOTE;
  1465 		break;
  1466 
  1467 		case 3:
  1468 			return AVATAR_GALLERY;
  1469 		break;
  1470 	}
  1471 
  1472 	return 0;
  1473 }
  1474 
  1475 
  1476 /**
  1477 * Just undos the replacing of '<' and '>'
  1478 */
  1479 function  phpbb_smilie_html_decode($code)
  1480 {
  1481 	$code = str_replace('&lt;', '<', $code);
  1482 	return str_replace('&gt;', '>', $code);
  1483 }
  1484 
  1485 /**
  1486 * Transfer avatars, copying the image if it was uploaded
  1487 */
  1488 function phpbb_import_avatar($user_avatar)
  1489 {
  1490 	global $convert_row;
  1491 
  1492 	if (!$convert_row['user_avatar_type'])
  1493 	{
  1494 		return '';
  1495 	}
  1496 	else if ($convert_row['user_avatar_type'] == 1)
  1497 	{
  1498 		// Uploaded avatar
  1499 		return import_avatar($user_avatar, false, $convert_row['user_id']);
  1500 	}
  1501 	else if ($convert_row['user_avatar_type'] == 2)
  1502 	{
  1503 		// Remote avatar
  1504 		return $user_avatar;
  1505 	}
  1506 	else if ($convert_row['user_avatar_type'] == 3)
  1507 	{
  1508 		// Gallery avatar
  1509 		return $user_avatar;
  1510 	}
  1511 
  1512 	return '';
  1513 }
  1514 
  1515 
  1516 /**
  1517 * Find out about the avatar's dimensions
  1518 */
  1519 function phpbb_get_avatar_height($user_avatar)
  1520 {
  1521 	global $convert_row;
  1522 
  1523 	if (empty($convert_row['user_avatar_type']))
  1524 	{
  1525 		return 0;
  1526 	}
  1527 	return get_avatar_height($user_avatar, 'phpbb_avatar_type', $convert_row['user_avatar_type']);
  1528 }
  1529 
  1530 
  1531 /**
  1532 * Find out about the avatar's dimensions
  1533 */
  1534 function phpbb_get_avatar_width($user_avatar)
  1535 {
  1536 	global $convert_row;
  1537 
  1538 	if (empty($convert_row['user_avatar_type']))
  1539 	{
  1540 		return 0;
  1541 	}
  1542 
  1543 	return get_avatar_width($user_avatar, 'phpbb_avatar_type', $convert_row['user_avatar_type']);
  1544 }
  1545 
  1546 
  1547 /**
  1548 * Calculate the correct to_address field for private messages
  1549 */
  1550 function phpbb_privmsgs_to_userid($to_userid)
  1551 {
  1552 	global $config;
  1553 
  1554 	return 'u_' . phpbb_user_id($to_userid);
  1555 }
  1556 
  1557 /**
  1558 * Calculate whether a private message was unread using the bitfield
  1559 */
  1560 function phpbb_unread_pm($pm_type)
  1561 {
  1562 	return ($pm_type == 5) ? 1 : 0;
  1563 }
  1564 
  1565 /**
  1566 * Calculate whether a private message was new using the bitfield
  1567 */
  1568 function phpbb_new_pm($pm_type)
  1569 {
  1570 	return ($pm_type == 1) ? 1 : 0;
  1571 }
  1572 
  1573 /**
  1574 * Obtain the folder_id for the custom folder created to replace the savebox from 2.0.x (used to store saved private messages)
  1575 */
  1576 function phpbb_get_savebox_id($user_id)
  1577 {
  1578 	global $db;
  1579 
  1580 	$user_id = phpbb_user_id($user_id);
  1581 
  1582 	// Only one custom folder, check only one
  1583 	$sql = 'SELECT folder_id
  1584 		FROM ' . PRIVMSGS_FOLDER_TABLE . '
  1585 		WHERE user_id = ' . $user_id;
  1586 	$result = $db->sql_query_limit($sql, 1);
  1587 	$folder_id = (int) $db->sql_fetchfield('folder_id');
  1588 	$db->sql_freeresult($result);
  1589 
  1590 	return $folder_id;
  1591 }
  1592 
  1593 /**
  1594 * Transfer attachment specific configuration options
  1595 * These were not stored in the main config table on 2.0.x
  1596 * This is only used if the Attachment MOD was installed
  1597 */
  1598 function phpbb_import_attach_config()
  1599 {
  1600 	global $db, $src_db, $same_db, $convert, $config;
  1601 
  1602 	if ($convert->mysql_convert && $same_db)
  1603 	{
  1604 		$src_db->sql_query("SET NAMES 'binary'");
  1605 	}
  1606 
  1607 	$sql = 'SELECT *
  1608 		FROM ' . $convert->src_table_prefix . 'attachments_config';
  1609 	$result = $src_db->sql_query($sql);
  1610 
  1611 	if ($convert->mysql_convert && $same_db)
  1612 	{
  1613 		$src_db->sql_query("SET NAMES 'utf8'");
  1614 	}
  1615 
  1616 	$attach_config = array();
  1617 	while ($row = $src_db->sql_fetchrow($result))
  1618 	{
  1619 		$attach_config[$row['config_name']] = $row['config_value'];
  1620 	}
  1621 	$src_db->sql_freeresult($result);
  1622 
  1623 	set_config('allow_attachments', 1);
  1624 
  1625 	// old attachment mod? Must be very old if this entry do not exist...
  1626 	if (!empty($attach_config['display_order']))
  1627 	{
  1628 		set_config('display_order', $attach_config['display_order']);
  1629 	}
  1630 	set_config('max_filesize', $attach_config['max_filesize']);
  1631 	set_config('max_filesize_pm', $attach_config['max_filesize_pm']);
  1632 	set_config('attachment_quota', $attach_config['attachment_quota']);
  1633 	set_config('max_attachments', $attach_config['max_attachments']);
  1634 	set_config('max_attachments_pm', $attach_config['max_attachments_pm']);
  1635 	set_config('allow_pm_attach', $attach_config['allow_pm_attach']);
  1636 
  1637 	set_config('img_display_inlined', $attach_config['img_display_inlined']);
  1638 	set_config('img_max_width', $attach_config['img_max_width']);
  1639 	set_config('img_max_height', $attach_config['img_max_height']);
  1640 	set_config('img_link_width', $attach_config['img_link_width']);
  1641 	set_config('img_link_height', $attach_config['img_link_height']);
  1642 	set_config('img_create_thumbnail', $attach_config['img_create_thumbnail']);
  1643 	set_config('img_max_thumb_width', 400);
  1644 	set_config('img_min_thumb_filesize', $attach_config['img_min_thumb_filesize']);
  1645 	set_config('img_imagick', $attach_config['img_imagick']);
  1646 }
  1647 
  1648 /**
  1649 * Calculate the date a user became inactive
  1650 */
  1651 function phpbb_inactive_time()
  1652 {
  1653 	global $convert_row;
  1654 
  1655 	if ($convert_row['user_active'])
  1656 	{
  1657 		return 0;
  1658 	}
  1659 
  1660 	if ($convert_row['user_lastvisit'])
  1661 	{
  1662 		return $convert_row['user_lastvisit'];
  1663 	}
  1664 
  1665 	return $convert_row['user_regdate'];
  1666 }
  1667 
  1668 /**
  1669 * Calculate the reason a user became inactive
  1670 * We can't actually tell the difference between a manual deactivation and one for profile changes
  1671 * from the data available to assume the latter
  1672 */
  1673 function phpbb_inactive_reason()
  1674 {
  1675 	global $convert_row;
  1676 
  1677 	if ($convert_row['user_active'])
  1678 	{
  1679 		return 0;
  1680 	}
  1681 
  1682 	if ($convert_row['user_lastvisit'])
  1683 	{
  1684 		return INACTIVE_PROFILE;
  1685 	}
  1686 
  1687 	return INACTIVE_REGISTER;
  1688 }
  1689 
  1690 /**
  1691 * Adjust 2.0.x disallowed names to 3.0.x format
  1692 */
  1693 function phpbb_disallowed_username($username)
  1694 {
  1695 	// Replace * with %
  1696 	$username = phpbb_set_default_encoding(str_replace('*', '%', $username));
  1697 	return utf8_htmlspecialchars($username);
  1698 }
  1699 
  1700 /**
  1701 * Checks whether there are any usernames on the old board that would map to the same
  1702 * username_clean on phpBB3. Prints out a list if any exist and exits.
  1703 */
  1704 function phpbb_create_userconv_table()
  1705 {
  1706 	global $db, $src_db, $convert, $table_prefix, $user, $lang;
  1707 
  1708 	$map_dbms = '';
  1709 	switch ($db->sql_layer)
  1710 	{
  1711 		case 'mysql':
  1712 			$map_dbms = 'mysql_40';
  1713 		break;
  1714 
  1715 		case 'mysql4':
  1716 			if (version_compare($db->sql_server_info(true), '4.1.3', '>='))
  1717 			{
  1718 				$map_dbms = 'mysql_41';
  1719 			}
  1720 			else
  1721 			{
  1722 				$map_dbms = 'mysql_40';
  1723 			}
  1724 		break;
  1725 
  1726 		case 'mysqli':
  1727 			$map_dbms = 'mysql_41';
  1728 		break;
  1729 
  1730 		case 'mssql':
  1731 		case 'mssql_odbc':
  1732 		case 'mssqlnative':
  1733 			$map_dbms = 'mssql';
  1734 		break;
  1735 
  1736 		default:
  1737 			$map_dbms = $db->sql_layer;
  1738 		break;
  1739 	}
  1740 
  1741 	// create a temporary table in which we store the clean usernames
  1742 	$drop_sql = 'DROP TABLE ' . USERCONV_TABLE;
  1743 	switch ($map_dbms)
  1744 	{
  1745 		case 'firebird':
  1746 			$create_sql = 'CREATE TABLE ' . USERCONV_TABLE . ' (
  1747 				user_id INTEGER NOT NULL,
  1748 				username_clean VARCHAR(255) CHARACTER SET UTF8 DEFAULT \'\' NOT NULL COLLATE UNICODE
  1749 			)';
  1750 		break;
  1751 
  1752 		case 'mssql':
  1753 			$create_sql = 'CREATE TABLE [' . USERCONV_TABLE . '] (
  1754 				[user_id] [int] NOT NULL ,
  1755 				[username_clean] [varchar] (255) DEFAULT (\'\') NOT NULL
  1756 			)';
  1757 		break;
  1758 
  1759 		case 'mysql_40':
  1760 			$create_sql = 'CREATE TABLE ' . USERCONV_TABLE . ' (
  1761 				user_id mediumint(8) NOT NULL,
  1762 				username_clean blob NOT NULL
  1763 			)';
  1764 		break;
  1765 
  1766 		case 'mysql_41':
  1767 			$create_sql = 'CREATE TABLE ' . USERCONV_TABLE . ' (
  1768 				user_id mediumint(8) NOT NULL,
  1769 				username_clean varchar(255) DEFAULT \'\' NOT NULL
  1770 			) CHARACTER SET `utf8` COLLATE `utf8_bin`';
  1771 		break;
  1772 
  1773 		case 'oracle':
  1774 			$create_sql = 'CREATE TABLE ' . USERCONV_TABLE . ' (
  1775 				user_id number(8) NOT NULL,
  1776 				username_clean varchar2(255) DEFAULT \'\'
  1777 			)';
  1778 		break;
  1779 
  1780 		case 'postgres':
  1781 			$create_sql = 'CREATE TABLE ' . USERCONV_TABLE . ' (
  1782 				user_id INT4 DEFAULT \'0\',
  1783 				username_clean varchar_ci DEFAULT \'\' NOT NULL
  1784 			)';
  1785 		break;
  1786 
  1787 		case 'sqlite':
  1788 			$create_sql = 'CREATE TABLE ' . USERCONV_TABLE . ' (
  1789 				user_id INTEGER NOT NULL DEFAULT \'0\',
  1790 				username_clean varchar(255) NOT NULL DEFAULT \'\'
  1791 			)';
  1792 		break;
  1793 	}
  1794 
  1795 	$db->sql_return_on_error(true);
  1796 	$db->sql_query($drop_sql);
  1797 	$db->sql_return_on_error(false);
  1798 	$db->sql_query($create_sql);
  1799 }
  1800 
  1801 function phpbb_check_username_collisions()
  1802 {
  1803 	global $db, $src_db, $convert, $table_prefix, $user, $lang;
  1804 
  1805 	// now find the clean version of the usernames that collide
  1806 	$sql = 'SELECT username_clean
  1807 		FROM ' . USERCONV_TABLE .'
  1808 		GROUP BY username_clean
  1809 		HAVING COUNT(user_id) > 1';
  1810 	$result = $db->sql_query($sql);
  1811 
  1812 	$colliding_names = array();
  1813 	while ($row = $db->sql_fetchrow($result))
  1814 	{
  1815 		$colliding_names[] = $row['username_clean'];
  1816 	}
  1817 	$db->sql_freeresult($result);
  1818 
  1819 	// there was at least one collision, the admin will have to solve it before conversion can continue
  1820 	if (sizeof($colliding_names))
  1821 	{
  1822 		$sql = 'SELECT user_id, username_clean
  1823 			FROM ' . USERCONV_TABLE . '
  1824 			WHERE ' . $db->sql_in_set('username_clean', $colliding_names);
  1825 		$result = $db->sql_query($sql);
  1826 		unset($colliding_names);
  1827 
  1828 		$colliding_user_ids = array();
  1829 		while ($row = $db->sql_fetchrow($result))
  1830 		{
  1831 			$colliding_user_ids[(int) $row['user_id']] = $row['username_clean'];
  1832 		}
  1833 		$db->sql_freeresult($result);
  1834 
  1835 		$sql = 'SELECT username, user_id, user_posts
  1836 			FROM ' . $convert->src_table_prefix . 'users
  1837 			WHERE ' . $src_db->sql_in_set('user_id', array_keys($colliding_user_ids));
  1838 		$result = $src_db->sql_query($sql);
  1839 
  1840 		$colliding_users = array();
  1841 		while ($row = $src_db->sql_fetchrow($result))
  1842 		{
  1843 			$row['user_id'] = (int) $row['user_id'];
  1844 			if (isset($colliding_user_ids[$row['user_id']]))
  1845 			{
  1846 				$colliding_users[$colliding_user_ids[$row['user_id']]][] = $row;
  1847 			}
  1848 		}
  1849 		$src_db->sql_freeresult($result);
  1850 		unset($colliding_user_ids);
  1851 
  1852 		$list = '';
  1853 		foreach ($colliding_users as $username_clean => $users)
  1854 		{
  1855 			$list .= sprintf($user->lang['COLLIDING_CLEAN_USERNAME'], $username_clean) . "<br />\n";
  1856 			foreach ($users as $i => $row)
  1857 			{
  1858 				$list .= sprintf($user->lang['COLLIDING_USER'], $row['user_id'], phpbb_set_default_encoding($row['username']), $row['user_posts']) . "<br />\n";
  1859 			}
  1860 		}
  1861 
  1862 		$lang['INST_ERR_FATAL'] = $user->lang['CONV_ERR_FATAL'];
  1863 		$convert->p_master->error('<span style="color:red">' . $user->lang['COLLIDING_USERNAMES_FOUND'] . '</span></b><br /><br />' . $list . '<b>', __LINE__, __FILE__);
  1864 	}
  1865 
  1866 	$drop_sql = 'DROP TABLE ' . USERCONV_TABLE;
  1867 	$db->sql_query($drop_sql);
  1868 }
  1869 
  1870 ?>