$page->output_footer(); exit; } if (!$mybb->config['database']['encoding']) { flash_message($lang->error_db_encoding_not_set, 'error'); admin_redirect("index.php?module=tools-system_health"); } $tables = $db->list_tables($mybb->config['database']['database']); $old_table_prefix = $db->table_prefix; $db->set_table_prefix(''); $encodings = array(); foreach ($tables as $key => $tablename) { if (substr($tablename, 0, strlen($old_table_prefix)) == $old_table_prefix) { $table = $db->show_create_table($tablename); preg_match("#CHARSET=([a-zA-Z0-9_]+)\\s?#i", $table, $matches); $encodings[$key] = fetch_iconv_encoding($matches[1]); $mybb_tables[$key] = $tablename; } } $db->set_table_prefix($old_table_prefix); $page->add_breadcrumb_item($lang->utf8_conversion, "index.php?module=tools-system_health&action=utf8_conversion"); $page->output_header($lang->system_health . " - " . $lang->utf8_conversion); $page->output_nav_tabs($sub_tabs, 'utf8_conversion'); asort($mybb_tables); $unique = array_unique($encodings); $convert_utf8 = $convert_utf8mb4 = false; foreach ($unique as $encoding) { if ($encoding == 'utf-8') { $convert_utf8mb4 = true; } elseif ($encoding != 'utf8mb4') { $convert_utf8 = true;
$form->end(); $db->set_table_prefix($old_table_prefix); $page->output_footer(); exit; } $tables = $db->list_tables($mybb->config['database']['database']); $old_table_prefix = $db->table_prefix; $db->set_table_prefix(''); $not_okey_count = 0; $not_okey = array(); $okey_count = 0; foreach ($tables as $key => $tablename) { if (substr($tablename, 0, strlen($old_table_prefix)) == $old_table_prefix) { $table = $db->show_create_table($tablename); preg_match("#CHARSET=([a-zA-Z0-9_]+)\\s?#i", $table, $matches); if (fetch_iconv_encoding($matches[1]) != 'utf-8') { $not_okey[$key] = $tablename; ++$not_okey_count; } else { ++$okay_count; } $mybb_tables[$key] = $tablename; } } $db->set_table_prefix($old_table_prefix); if ($okay_count == count($mybb_tables)) { flash_message($lang->success_all_tables_already_converted, 'success'); admin_redirect("index.php?module=tools-system_health"); } if (!$mybb->config['database']['encoding']) { flash_message($lang->error_db_encoding_not_set, 'error');
/** * Properly converts the encoding of a string based upon the old table to the new table to utf8 encoding, as best as we can * * @param string The text to convert * @param string The old table (e.x. vB's user table) * @param string The new table (e.x. MyBB's user table) * @return string The converted text in utf8 format */ function encode_to_utf8($text, $old_table_name, $new_table_name) { global $import_session, $db, $module; if ($import_session['encode_to_utf8'] == 0) { return $text; } $old_table_name = OLD_TABLE_PREFIX . $old_table_name; $new_table_name = TABLE_PREFIX . $new_table_name; // Get the character set if needed if (empty($import_session['table_charset_old'][$old_table_name]) || empty($import_session['table_charset_new'][$new_table_name])) { $old_table_prefix = $db->table_prefix; $db->set_table_prefix(''); $old_old_db_table_prefix = $module->old_db->table_prefix; $module->old_db->set_table_prefix(''); $table = $module->old_db->show_create_table($old_table_name); preg_match("#CHARSET=(.*)#i", $table, $old_charset); $table = $db->show_create_table($new_table_name); preg_match("#CHARSET=(.*)#i", $table, $new_charset); $db->set_table_prefix($old_table_prefix); $module->old_db->set_table_prefix($old_old_db_table_prefix); $import_session['table_charset_old'][$old_table_name] = $old_charset[1]; $import_session['table_charset_new'][$new_table_name] = $new_charset[1]; } // Convert as needed if (($import_session['table_charset_new'][$new_table_name] != $import_session['table_charset_old'][$old_table_name] || check_encoding($text, fetch_iconv_encoding($import_session['table_charset_new'][$new_table_name])) === false) && $import_session['table_charset_old'][$old_table_name] != '' && $import_session['table_charset_new'][$new_table_name] != '') { if (!function_exists('iconv')) { if (fetch_iconv_encoding($import_session['table_charset_old'][$old_table_name]) != 'iso-8859-1' || !function_exists("utf8_encode")) { return $text; } return utf8_encode($text); } $converted_str = iconv(fetch_iconv_encoding($import_session['table_charset_old'][$old_table_name]), fetch_iconv_encoding($import_session['table_charset_new'][$new_table_name]) . '//TRANSLIT', $text); // Do we have bad characters? (i.e. db/table encoding set to UTF-8 but string is actually ISO) if (my_strlen($converted_str) < my_strlen($text)) { // Was our database/tables set to UTF-8 encoding and the data actually in iso encoding? // Stop trying to confuse us!! $converted_str = iconv("iso-8859-1", fetch_iconv_encoding($import_session['table_charset_new'][$new_table_name]) . '//IGNORE', $text); if (my_strlen($converted_str) >= my_strlen($text)) { return $converted_str; } } // Try to convert, but don't stop when a character cannot be converted return iconv(fetch_iconv_encoding($import_session['table_charset_old'][$old_table_name]), fetch_iconv_encoding($import_session['table_charset_new'][$new_table_name]) . '//IGNORE', $text); } return $text; }