Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?
- set_time_limit(0);
- // you can edit these two lines
- define(SEARCH_FOR, "modmyifone.com,modmyiphone.com");
- define(REPLACE_TO, "modmyi.com");
- // MySQL login info that you should also edit
- $db_hostname = "localhost";
- $db_username = "";
- $db_password = "";
- $db_database = "";
- // field names to filter out
- $filter = array(
- "username",
- "password",
- "firstname",
- "lastname",
- "first_name",
- "last_name",
- "address",
- "city",
- "country",
- "state",
- "zip",
- "fax",
- "ip",
- );
- $regex_filter = array(
- "\_ip$",
- "phone$",
- );
- // end of config part
- // and please do not touch below this line
- // a function for regex filtering
- function filter_out($str){
- global $regex_filter, $filter;
- $doso = FALSE;
- if (in_array($str, $filter))
- $doso = TRUE;
- else {
- for ($i = 0; $i < sizeof($regex_filter); $i++){
- if (preg_match("/".$regex_filter[$i]."/", $str))
- $doso = TRUE;
- }
- }
- return $doso;
- }
- // finding the shortest search string - to be used to determine min varchar length
- $search_for = explode(",", SEARCH_FOR);
- $minlength = 255;
- for ($i = 0; $i < sizeof($search_for); $i++){
- $ss = trim($search_for[$i]);
- if (strlen($ss) < $minlength)
- $minlength = strlen($ss);
- }
- unset($ss);
- // connecting to the database
- $link = mysql_connect($db_hostname, $db_username, $db_password) or die (mysql_error());
- mysql_select_db ($db_database) or die (mysql_error());
- // getting the list of tables
- $res = mysql_query("show tables") or die(mysql_error());
- $fields = array();
- while ($row = mysql_fetch_row($res)){
- $table = $row[0];
- $res2 = mysql_query("describe `$table`") or die(mysql_error());
- while ($row2 = mysql_fetch_assoc($res2)){
- // filter out unnecessary fields
- if (filter_out($row2["Field"]))
- continue;
- // getting the field type
- $type = $row2["Type"];
- // adding the text fields to the list
- if ($type=="text")
- $fields[$table][] = $row2["Field"];
- // getting the varchar fields to the list
- else if (preg_match("/varchar/", $type)){
- $length = substr($type, 8, strlen($type) - 9);
- // only get the ones that are long enough to fit our strings
- if ($length >= $minlength)
- $fields[$table][] = $row2["Field"];
- }
- }
- mysql_free_result($res2);
- unset($row2);
- unset($type);
- unset($length);
- unset($minlength);
- }
- mysql_free_result($res);
- unset($table);
- unset($row);
- function search_and_replace($table, $field){
- $search_for = explode(",", SEARCH_FOR);
- for ($i = 0; $i < sizeof($search_for); $i++){
- $ss = trim($search_for[$i]);
- $query = "update `$table` set `$field`=replace(`$field`, '$ss', '".REPLACE_TO.
- "') where `$field` like '%$ss%'";
- mysql_query($query) or die(mysql_error());
- $replacements = mysql_affected_rows();
- echo ($replacements > 0 ? "<font color=red><b>" : "").
- "Searched column $field for $ss - $replacements replacements.".
- ($replacements > 0 ? "</b></font>" : ""). "<br />\n";
- }
- unset($ss);
- unset($query);
- unset($search_for);
- }
- // time to process these fields
- foreach ($fields as $table => $f){
- echo "<h3>Now fixing table $table...</h3>";
- for ($i = 0; $i < sizeof($f); $i++){
- search_and_replace($table, $f[$i]);
- }
- }
- ?>
Add Comment
Please, Sign In to add comment