forked from ttodua/useful-php-scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwordpress-database-modifier-when-migrating
43 lines (39 loc) · 2.58 KB
/
wordpress-database-modifier-when-migrating
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
<?php
// LIVE DEMO (with README + ALTERNATIVES): http://www.protectpages.com/software/WordpressMigrator.php
if (empty($_FILES['dbfilee'])) { ?>
<form action="" method="POST" enctype="multipart/form-data"><h1>Intelligenty replaces words or domains with new values</h1><br/><br/>
Old string <input type="text" value="" placeholder="i.e. word1 or http://domain1.com" name="oldstr" style="width:300px;" /><br/>
new string<input type="text" value="" placeholder="i.e. word2 or http://domain2.com" style="width:300px;" name="newstr" /><br/>
drag database file here:<input type="file" name="dbfilee" /><br/><input type="submit" value="GENERATE NEW DB" /> (and wait some time...)
</form>
<?php
}
else {
function DOMAIN_or_STRING_modifier_in_DB($old_string, $new_string, $sql_content, $download = false){
set_time_limit(1000);
if(empty($old_string) || empty($new_string) || empty($sql_content)) return;
$length_difference= strlen($old_string)- strlen($new_string); //i.e. FROM site.com TO example.com
$old_string_slashed=str_replace('/','\/',$old_string);
// Replace every occurence of Serialized arrays, i.e. {s:32:"blablabla"}
preg_match_all('/(|\}|\{|\;\|)s\:(\d{1,5})\:\"(.*?)\";/si', $sql_content, $n1, PREG_SET_ORDER);
preg_match_all('/(|\}|\{|\;\|)s\:(\d{1,5})\:\\\"(.*?)\";/si', $sql_content, $n2, PREG_SET_ORDER); $n=array_merge($n1,$n2);
foreach($n as $Each_Serialized_String){ $e=$Each_Serialized_String; //var_dump($e); 0=>{s:12:"blablablabla" 1=>; 2=>17 3=>"blablablabla"
if(stripos($e[3],$old_string)!==false){ //if string found in line, then go to next
if(!is_numeric($e[2])) { continue; } else { $found_char_length= intval($e[2]);}
$before_s = $e[1]; //i.e. } or { or ; or |
$found_line = $e[0];
$found_line_changed = str_replace(
array($before_s.'s:'.$found_char_length, $old_string),
array($before_s.'s:'.($found_char_length - $length_difference), $new_string),
$found_line);
$sql_content = str_replace($found_line, str_replace($old_string,$new_string,$found_line_changed), $sql_content);
}
}
// Now, we can freely replace typical occurences
$sql_content=str_replace($old_string,$new_string,$sql_content);
if ($download) { header('Content-Type: application/octet-stream'); header("Content-Transfer-Encoding: Binary"); header('Content-disposition: attachment; filename="db_'.rand(1,99999).'.sql"'); echo $sql_content; exit; } else {return $sql_content;}
}
// EXECUTE !!!
DOMAIN_or_STRING_modifier_in_DB($_POST['oldstr'], $_POST['newstr'], file_get_contents($_FILES["dbfilee"]["tmp_name"]), true);
}
?>