f2c4aa5
f2c4aa5
f2c4aa5
#
f2c4aa5
# relocate.php /path/to/file prefix
f2c4aa5
# 
f2c4aa5
# Takes a file as input and a string prefix; reads
f2c4aa5
# the file as a serialized data blob and strips PREFIX
f2c4aa5
# from the beginning of each string value within the blob.
f2c4aa5
# Serializes again and writes output to stdout.
f2c4aa5
# 
f2c4aa5
f2c4aa5
$file = $_SERVER['argv'][1];
f2c4aa5
$destdir = $_SERVER['argv'][2];
f2c4aa5
f2c4aa5
$destdir_len = strlen($destdir);
f2c4aa5
cbe20f7
function relocate_string($value) {
f2c4aa5
    global $destdir, $destdir_len;
f2c4aa5
cbe20f7
    if (strncmp($value, $destdir, $destdir_len) == 0) {
cbe20f7
        $value = substr($value, $destdir_len);
cbe20f7
    }
cbe20f7
    return $value;
cbe20f7
}
cbe20f7
    
cbe20f7
function relocate_value($value) {
f2c4aa5
    if (is_string($value)) {
cbe20f7
        $value = relocate_string($value);
f2c4aa5
    } else if (is_array($value)) {
f2c4aa5
        $value = relocate_array($value);
f2c4aa5
    }
f2c4aa5
    
f2c4aa5
    return $value;
f2c4aa5
}
f2c4aa5
f2c4aa5
function relocate_array($array) {
cbe20f7
    $result = array();
cbe20f7
f2c4aa5
    foreach ($array as $key => $value) {
d6187dd
        if (is_string($key)) {
d6187dd
            $key = relocate_string($key);
d6187dd
        }
cbe20f7
        $result[$key] = relocate_value($value);
f2c4aa5
    }
f2c4aa5
cbe20f7
    return $result;
f2c4aa5
}
f2c4aa5
f2c4aa5
$input = file_get_contents($file);
f2c4aa5
f2c4aa5
# Special case for /etc/pear.conf.
f2c4aa5
if (strncmp($input, "#PEAR_Config 0.9\n", 17) == 0) {
f2c4aa5
    echo substr($input, 0, 17);
f2c4aa5
    $s = substr($input, 17);
f2c4aa5
} else {
f2c4aa5
    $s = $input;
f2c4aa5
}
f2c4aa5
f2c4aa5
echo serialize(relocate_value(unserialize($s)));
f2c4aa5
f2c4aa5
?>