Skip to content

Instantly share code, notes, and snippets.

@satanTime
Last active November 21, 2020 10:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save satanTime/7b0d6637bfa7a269e534c0a3782aa6f6 to your computer and use it in GitHub Desktop.
Save satanTime/7b0d6637bfa7a269e534c0a3782aa6f6 to your computer and use it in GitHub Desktop.
<?php
error_reporting(E_ALL);
ini_set('display_errors', 'yes');
$replace = array();
$replace[] = array(
'type' => 'any',
'source' => 'localhost',
'value' => 'website.com',
);
$replace[] = array(
'type' => 'any',
'source' => 'http://website.com',
'value' => 'https://website.com',
);
print_r($replace);
require __DIR__ . '/wp-load.php';
echo 'WP loaded' . PHP_EOL;
$connection = new PDO('mysql:dbname=' . DB_NAME . ';host=' . DB_HOST, DB_USER, DB_PASSWORD);
echo 'MySQL executed' . PHP_EOL;
if ( ! $connection) {
exit('Can not connect to database' . PHP_EOL);
}
$connection->query("SET NAMES " . DB_CHARSET);
$tables = $connection->query("SHOW TABLES");
$tables = $tables->fetchAll(PDO::FETCH_COLUMN);
foreach ($tables as $table) {
echo $table . PHP_EOL;
flush();
$rows = $connection->query("SELECT * FROM {$table} WHERE 1=1");
while ($row = $rows->fetch(PDO::FETCH_ASSOC)) {
$where = array();
$data = array();
foreach ($row as $name => $value) {
if (strpos(strtolower($name), 'id') !== false) {
$where[$name . ' = ?'] = $value;
}
$replaced = db_value_normalize($value, $replace);
if ($replaced !== $value) {
$data[$name . ' = ?'] = $replaced;
}
}
if ($data) {
if ($where) {
$query = $connection->prepare("UPDATE {$table} SET " . implode(', ',
array_keys($data)) . " WHERE " . implode(' AND ', array_keys($where)));
$query->execute(array_merge(array_values($data), array_values($where)));
$errorNumber = $query->errorCode();
$error = $query->errorInfo();
} else {
$i = 0;
}
}
}
}
function db_value_normalize($value, $conditions, $depth = 0)
{
if (is_array($value)) {
foreach ($value as $k => $v) {
$value[db_value_normalize($k, $conditions, $depth + 1)] = db_value_normalize($v, $conditions, $depth + 1);
}
return $value;
}
if (is_object($value)) {
return $value;
}
if ($value === null) {
return $value;
}
$type = 'raw';
if (is_string($value) && is_numeric($value)) {
// nothing to do
} elseif (is_string($value) && $value) {
$test = @unserialize($value);
if ($test !== false || $value == serialize(false)) {
$type = 'serialize';
}
$test = null;
$test = @json_decode($value, true);
if (json_last_error() == JSON_ERROR_NONE) {
$type = 'json';
}
$test = null;
$test = @base64_decode($value, true);
if ($test !== false && base64_encode($test) === $value) {
$type = 'base64';
}
$test = null;
}
switch ($type) {
case 'serialize':
$value = unserialize($value);
break;
case 'json':
$value = json_decode($value, true);
break;
case 'base64':
$value = base64_decode($value, true);
break;
}
if ($type === 'raw' && is_scalar($value)) {
$value = db_value_replace($value, $conditions);
} else {
$value = db_value_normalize($value, $conditions, $depth + 1);
}
switch ($type) {
case 'serialize':
$value = serialize($value);
break;
case 'json':
$value = json_encode($value, true);
break;
case 'base64':
$value = base64_encode($value);
break;
}
return $value;
}
function db_value_replace($value, $conditions)
{
foreach ($conditions as $condition) {
switch ($condition['type']) {
case 'exact' :
if ($value === $condition['source']) {
return $condition['value'];
}
break;
case 'starts' :
if (is_string($value) && strpos($value, $condition['source']) === 0) {
$value = $condition['value'] . substr($value, strlen($condition['source']));
}
break;
case 'any' :
while (is_string($value) && strpos($value, $condition['source']) !== false) {
$value = str_replace($condition['source'], $condition['value'], $value);
$value = str_replace(urlencode($condition['source']), urlencode($condition['value']), $value);
}
break;
}
}
return $value;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment