Guest User

Untitled

a guest
May 21st, 2018
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.31 KB | None | 0 0
  1. <?
  2. set_time_limit(0);
  3. // you can edit these two lines
  4. define(SEARCH_FOR, "modmyifone.com,modmyiphone.com");
  5. define(REPLACE_TO, "modmyi.com");
  6.  
  7. // MySQL login info that you should also edit
  8. $db_hostname = "localhost";
  9. $db_username = "";
  10. $db_password = "";
  11. $db_database = "";
  12.  
  13. // field names to filter out
  14. $filter = array(
  15. "username",
  16. "password",
  17. "firstname",
  18. "lastname",
  19. "first_name",
  20. "last_name",
  21. "address",
  22. "city",
  23. "country",
  24. "state",
  25. "zip",
  26. "fax",
  27. "ip",
  28. );
  29.  
  30. $regex_filter = array(
  31. "\_ip$",
  32. "phone$",
  33. );
  34.  
  35. // end of config part
  36. // and please do not touch below this line
  37.  
  38. // a function for regex filtering
  39. function filter_out($str){
  40. global $regex_filter, $filter;
  41. $doso = FALSE;
  42.  
  43. if (in_array($str, $filter))
  44. $doso = TRUE;
  45. else {
  46. for ($i = 0; $i < sizeof($regex_filter); $i++){
  47. if (preg_match("/".$regex_filter[$i]."/", $str))
  48. $doso = TRUE;
  49. }
  50. }
  51. return $doso;
  52. }
  53.  
  54. // finding the shortest search string - to be used to determine min varchar length
  55. $search_for = explode(",", SEARCH_FOR);
  56. $minlength = 255;
  57. for ($i = 0; $i < sizeof($search_for); $i++){
  58. $ss = trim($search_for[$i]);
  59. if (strlen($ss) < $minlength)
  60. $minlength = strlen($ss);
  61. }
  62. unset($ss);
  63.  
  64. // connecting to the database
  65. $link = mysql_connect($db_hostname, $db_username, $db_password) or die (mysql_error());
  66. mysql_select_db ($db_database) or die (mysql_error());
  67.  
  68. // getting the list of tables
  69. $res = mysql_query("show tables") or die(mysql_error());
  70.  
  71. $fields = array();
  72. while ($row = mysql_fetch_row($res)){
  73. $table = $row[0];
  74. $res2 = mysql_query("describe `$table`") or die(mysql_error());
  75. while ($row2 = mysql_fetch_assoc($res2)){
  76. // filter out unnecessary fields
  77. if (filter_out($row2["Field"]))
  78. continue;
  79.  
  80. // getting the field type
  81. $type = $row2["Type"];
  82.  
  83. // adding the text fields to the list
  84. if ($type=="text")
  85. $fields[$table][] = $row2["Field"];
  86.  
  87. // getting the varchar fields to the list
  88. else if (preg_match("/varchar/", $type)){
  89. $length = substr($type, 8, strlen($type) - 9);
  90. // only get the ones that are long enough to fit our strings
  91. if ($length >= $minlength)
  92. $fields[$table][] = $row2["Field"];
  93. }
  94. }
  95. mysql_free_result($res2);
  96. unset($row2);
  97. unset($type);
  98. unset($length);
  99. unset($minlength);
  100. }
  101. mysql_free_result($res);
  102. unset($table);
  103. unset($row);
  104.  
  105. function search_and_replace($table, $field){
  106. $search_for = explode(",", SEARCH_FOR);
  107. for ($i = 0; $i < sizeof($search_for); $i++){
  108. $ss = trim($search_for[$i]);
  109. $query = "update `$table` set `$field`=replace(`$field`, '$ss', '".REPLACE_TO.
  110. "') where `$field` like '%$ss%'";
  111. mysql_query($query) or die(mysql_error());
  112. $replacements = mysql_affected_rows();
  113. echo ($replacements > 0 ? "<font color=red><b>" : "").
  114. "Searched column $field for $ss - $replacements replacements.".
  115. ($replacements > 0 ? "</b></font>" : ""). "<br />\n";
  116. }
  117. unset($ss);
  118. unset($query);
  119. unset($search_for);
  120. }
  121.  
  122. // time to process these fields
  123. foreach ($fields as $table => $f){
  124. echo "<h3>Now fixing table $table...</h3>";
  125. for ($i = 0; $i < sizeof($f); $i++){
  126. search_and_replace($table, $f[$i]);
  127. }
  128. }
  129. ?>
Add Comment
Please, Sign In to add comment