summaryrefslogtreecommitdiff
path: root/includes/restore.inc
blob: 72748ab1ada2b6ed88430e40989fbd92f25b5128 (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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
<?php

function isJson($string): bool {
    json_decode($string);
    return (json_last_error() == JSON_ERROR_NONE);
}

function pkcs7_unpad($data) {
    return substr($data, 0, -ord($data[strlen($data) - 1]));
}

function timeAgo($time): string {
    if (!is_numeric($time)) {
        $time = strtotime($time);
    }

    $periods = ["second", "minute", "hour", "day", "week", "month", "year", "age"];
    $lengths = array("60", "60", "24", "7", "4.35", "12", "100");

    $now = time();

    $difference = $now - $time;
    if ($difference <= 10 && $difference >= 0) {
        return $tense = "now";
    } elseif ($difference > 0) {
        $tense = "ago";
    } else {
        $tense = "later";
    }

    for ($j = 0; $difference >= $lengths[$j] && $j < count($lengths)-1; $j++) {
        $difference /= $lengths[$j];
    }

    $difference = round($difference);

    $period =  $periods[$j] . ($difference >1 ? "s" :'');
    return "{$difference} {$period} {$tense}";
}

if (!isset($_SERVER['argv'][1]) || !isset($_SERVER['argv'][2])) {
    echo("Usage: php " . $_SERVER['argv'][0] . " <file> <key>\n");
    die();
} else {
    $file = @file_get_contents($_SERVER['argv'][1]);
    $raw = @file_get_contents($_SERVER['argv'][2]);

    if ($file === false) {
        echo("Unable to open backup file\n");
        die();
    }

    if ($raw === false) {
        echo("Unable to open key file\n");
        die();
    }

    $raw2 = base64_decode($raw);

    if (!isJson($raw2)) {
        echo("Key file is corrupt\n");
        die();
    }

    $keydata = json_decode($raw2, true);

    if (!is_array($keydata) || !isset($keydata["iv"]) || !isset($keydata["key"])) {
        echo("Key file is invalid\n");
        die();
    }

    $iv = hex2bin($keydata["iv"]);
    $key = hex2bin($keydata["key"]);

    $decrypted = openssl_decrypt($file, 'AES-256-CBC', $key, OPENSSL_RAW_DATA, $iv);

    if ($decrypted === false) {
        echo("Unable to decrypt backup\n");
        die();
    }

    $unpadded = pkcs7_unpad($decrypted);

    if (!is_string($unpadded)) {
        echo("Unable to decrypt backup\n");
        die();
    }

    if (!isJson($unpadded)) {
        echo("Backup is corrupt\n");
        die();
    }

    $data = json_decode($unpadded, true);

    if (!is_array($data) || !isset($data["date"]) || !isset($data["files"])) {
        echo("Backup is invalid\n");
        die();
    }

    echo(realpath($_SERVER['argv'][1]) . "\n    Key: " . $_SERVER['argv'][2] . "\n    Date: " . date('r', strtotime($data["date"])) . " (" . timeAgo($data["date"]) . ")" . "\n    Contents: " . count($data["files"]) . " files\n");

    @mkdir("./_restored");

    $index = 0;
    foreach ($data["files"] as $file) {
        if ($file["dir"] === "") {
            print("[$index] /" . $file["file"] . "\n");
        } else {
            print("[$index] /" . $file["dir"] . "/" . $file["file"] . "\n");
        }

        $content = base64_decode($file["content"]);
        if (sha1($content) !== $file["checksum"][0]) {
            print("    Backed up file is corrupted (SHA1 mismatch)\n        Expected: " . $file["checksum"][0] . "\n        Got:      " . sha1($content) . "\n");
            die("Backup aborted.\n");
        }
        if (md5($content) !== $file["checksum"][1]) {
            print("    Backed up file is corrupted (MD5 mismatch)\n        Expected: " . $file["checksum"][1] . "\n        Got:      " . md5($content) . "\n");
            die("Backup aborted.\n");
        }

        @mkdir("./_restored/" . $file["dir"], 0777, true);
        file_put_contents("./_restored/" . $file["dir"] . "/" . $file["file"], $content);

        $index++;
    }

    print("Restored backup to ./_restored; review files before restoring to production\n");
}