Created
September 17, 2017 00:15
-
-
Save z3nth10n/6075b0830501a18cbaddea27f5027a58 to your computer and use it in GitHub Desktop.
Wordpress database serialize problem fixers
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$file = @$_GET["file"]; | |
if(empty($file)) | |
die("You must specify a ?file value."); | |
function fixSerialized($error_serialized_data) | |
{ | |
$fixed_serialized_data = preg_replace_callback ( '!s:(\d+):"(.*?)";!', | |
function($match) { | |
return ($match[1] == strlen($match[2])) ? $match[0] : 's:' . strlen($match[2]) . ':"' . $match[2] . '";'; | |
}, | |
$error_serialized_data ); | |
return $fixed_serialized_data; | |
} | |
header("Content-type: application/octet-stream"); | |
header('Content-Disposition: attachment; filename=wordpress_db.sql'); | |
$subject = file_get_contents($file); | |
$separator = "\r\n"; | |
$line = strtok($subject, $separator); | |
$fixed = 0; | |
while ($line !== false) | |
{ | |
# do something with $line | |
$matches = array(); | |
if(preg_match("/'a:\d+.+?\}'/", $line, $matches)) | |
{ | |
foreach ($matches as $match) | |
{ | |
$l = substr($match, 1, -1); | |
$ll = fixSerialized($l); | |
if($l !== $ll) ++$fixed; | |
$line = str_replace($l, $ll, $line); | |
} | |
} | |
echo $line."\n"; | |
$line = strtok( $separator ); | |
} | |
echo "-- Total fixed lines: {$fixed}"; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment