connect_error) { die('Could not connect to mysql: ('.$mysqli->connect_errno.') '. $mysqli->connect_error); } $mysqli->select_db('mysql'); replace_users($mysqli); replace_dbs($mysqli); rename_dbs($mysqli); $mysqli->query("FLUSH_PRIVILEGES"); $mysqli->close(); function rename_dbs($mysqli) { global $username; global $newusername; global $ignore_errors; // This will find all databases owned by the User // for each db, create a new db with the correct name (based on the old db?) // for each db, it finds all tables $user_dbs = get_user_dbs($mysqli); foreach ($user_dbs as $db) { $new_db = preg_replace('/'.$username.'\\_/', $newusername.'_', $db); $tables = get_db_tables($db, $mysqli); vecho("Swapping $db to $new_db\n"); //Create the new DB. $query = "CREATE DATABASE $new_db"; if (!$mysqli->query($query)) { $err = "Create DB Error: ". $mysqli->error; if ($ignore_errors == 1) vecho($err); else die($err); } foreach ($tables as $table) { // we now have a new database to use // rename each table into the new DB. $query = "RENAME TABLE $db.$table TO $new_db.$table"; if (!$mysqli->query($query)) { $err = "Table Rename Error: ". $mysqli->error; vecho($err, 1); //don't abort halfway for tables, and output no matter what. } } // drop the old database *only* if it contains 0 tables. $old_tables = get_db_tables($db, $mysqli); if (count($old_tables) == 0) { $query = "DROP DATABASE $db"; if (!$mysqli->query($query)) { $err = "Drop DB Error: ". $mysqli->error; if ($ignore_errors == 1) vecho($err); else die($err); } } } } function get_db_tables($db, $mysqli) { $query = "SHOW TABLES FROM $db"; if (! ($result = $mysqli->query($query)) ) { die("Table List Error: ". $mysqli->error); } $table_array = array(); while (($row = $result->fetch_row())) { array_push($table_array, $row[0]); } $result->free(); return $table_array; } function get_user_dbs($mysqli) { global $username; $query = "SHOW DATABASES LIKE '$username\\_%'"; if (! ($result = $mysqli->query($query)) ) { die("DB List Error: ". $mysqli->error); } $db_array = array(); while (($row = $result->fetch_row())) { array_push($db_array, $row[0]); } $result->free(); return $db_array; } function replace_users($mysqli) { global $username; global $newusername; //in this function, we need to replace // username to newusername // username_user to newusername_user $mysqli->query("UPDATE mysql.user SET user='$newusername' WHERE user='$username'"); $mysqli->query("UPDATE mysql.db SET user='$newusername' WHERE user='$username'"); $query = "SELECT user FROM mysql.user WHERE user LIKE '$username\\_%'"; $result = $mysqli->query($query) or vecho("Error selecting mysql.user: ".$mysqli->error."\n", 1); while ($row = $result->fetch_row()) { $user = $row[0]; //$new_user = ereg_replace($username."_", $newusername."_", $user); $new_user = preg_replace('/'.$username.'_/', $newusername."_", $user); vecho("swapping '$user' with '$new_user'"); $query = "UPDATE mysql.user SET user='$new_user' WHERE user='$user'"; $mysqli->query($query) or vecho("Error updating $user to $newuser in mysql.user: ".$mysqli->error."\n", 1); $query = "UPDATE mysql.db SET user='$new_user' WHERE user='$user'"; $mysqli->query($query) or vecho("Error updating $user to $newuser in mysql.db: ".$mysqli->error."\n", 1); } $result->free(); } function replace_dbs($mysqli) { global $username; global $newusername; global $host; //in this function, we need to replace // username_db and username\_db to newusername\_db (many) $user_dbs = get_user_dbs($mysqli); foreach ($user_dbs as $dbname) { $new_db = preg_replace('/'.$username.'\\_/', $newusername.'_', $dbname); $user_prefix = $username."_"; $prefix_len = strlen($user_prefix); vecho("swapping database $dbname for user $newusername..."); $esc_dbname = preg_replace('/_/',"\\_", $dbname); $new_dbname = preg_replace('/'.$username.'\\_/', $newusername.'_', $dbname); $esc_new_dbname = preg_replace('/_/',"\\_", $new_dbname); vecho("dbname=$dbname\nesc_dbname=$esc_dbname\nnew_dbname=$new_dbname\nesc_new_dbname=$esc_new_dbname"); $query = "UPDATE mysql.db SET db='$esc_new_dbname' WHERE db='$dbname' OR db='$esc_dbname'"; $mysqli->query($query) or vecho("Query error renaming db: ".$mysqli->error, 1); } vecho(""); } function vecho($str, $is_err=0) { global $verbose; if ($verbose || $is_err==1) echo $str."\n"; } ?>