summaryrefslogtreecommitdiffstats
path: root/relocate.php
blob: 8475b3336a6c47337bb0e44d2d11fbe02ec6fa88 (plain)
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
<?php 

#
# relocate.php /path/to/file prefix
# 
# Takes a file as input and a string prefix; reads
# the file as a serialized data blob and strips PREFIX
# from the beginning of each string value within the blob.
# Serializes again and writes output to stdout.
# 

$file = $_SERVER['argv'][1];
$destdir = $_SERVER['argv'][2];

$destdir_len = strlen($destdir);

function relocate_string($value) {
    global $destdir, $destdir_len;

    if (strncmp($value, $destdir, $destdir_len) == 0) {
        $value = substr($value, $destdir_len);
    }
    return $value;
}
    
function relocate_value($value) {
    if (is_string($value)) {
        $value = relocate_string($value);
    } else if (is_array($value)) {
        $value = relocate_array($value);
    }
    
    return $value;
}

function relocate_array($array) {
    $result = array();

    foreach ($array as $key => $value) {
        if (is_string($key)) {
            $key = relocate_string($key);
        }
        $result[$key] = relocate_value($value);
    }

    return $result;
}

$input = file_get_contents($file);

# Special case for /etc/pear.conf.
if (strncmp($input, "#PEAR_Config 0.9\n", 17) == 0) {
    echo substr($input, 0, 17);
    $s = substr($input, 17);
} else {
    $s = $input;
}

echo serialize(relocate_value(unserialize($s)));

?>