2023-02-14 09:32:15 +01:00
|
|
|
<?php
|
|
|
|
|
2023-10-11 00:04:02 +02:00
|
|
|
function checkAuth($user, $password, $credentials, $mode)
|
|
|
|
{
|
|
|
|
if ($mode === "NO_AUTH") {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (!isset($credentials[$user])) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return $credentials[$user] === $password;
|
|
|
|
}
|
|
|
|
|
|
|
|
$credentials = [];
|
|
|
|
$mode = "NO_AUTH";
|
|
|
|
if (getenv("RKC_USER") !== false) {
|
|
|
|
$mode = "SINGLE_AUTH";
|
|
|
|
$credentials[getenv("RKC_USER")] = getenv("RKC_PASS");
|
|
|
|
}
|
|
|
|
foreach (getenv() as $k => $v) {
|
|
|
|
if (preg_match("/^RKC_USER_(.*)$/", $k, $m)) {
|
|
|
|
if ($mode === "SINGLE_AUTH") {
|
|
|
|
header("HTTP/1.0 500 Internal Server Error");
|
|
|
|
echo "single auth and multi auth is used at the same time";
|
2023-02-14 09:32:15 +01:00
|
|
|
exit();
|
|
|
|
}
|
2023-10-11 00:04:02 +02:00
|
|
|
$mode = "MULTI_AUTH";
|
|
|
|
$u = $m[1];
|
|
|
|
if (!preg_match("/^[a-zA-Z0-9_-]+$/", $k)) {
|
|
|
|
header("HTTP/1.0 500 Internal Server Error");
|
|
|
|
echo "invalid username, only a-zA-Z0-9 and _- are allowed";
|
|
|
|
exit();
|
|
|
|
}
|
|
|
|
$credentials[strtolower($u)] = $v;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($_SERVER["REQUEST_METHOD"] === "POST") {
|
|
|
|
if (
|
|
|
|
checkAuth(
|
|
|
|
strtolower($_SERVER["PHP_AUTH_USER"] ?? null),
|
|
|
|
$_SERVER["PHP_AUTH_PW"] ?? null,
|
|
|
|
$credentials,
|
|
|
|
$mode
|
|
|
|
) === false
|
|
|
|
) {
|
|
|
|
header('WWW-Authenticate: Basic realm="RKC"');
|
|
|
|
header("HTTP/1.0 401 Unauthorized");
|
|
|
|
echo "You are not authorized to access this page.";
|
|
|
|
exit();
|
2023-02-14 09:32:15 +01:00
|
|
|
}
|
2023-10-11 00:04:02 +02:00
|
|
|
$fileName = $_SERVER["PHP_AUTH_USER"] . ".json";
|
2023-02-14 09:32:15 +01:00
|
|
|
|
|
|
|
$resticData = file_get_contents("php://input");
|
|
|
|
$resticData = explode("\n", $resticData);
|
2023-09-15 19:05:21 +02:00
|
|
|
if (count($resticData) <= 1) {
|
|
|
|
exit();
|
|
|
|
}
|
2023-02-14 09:32:15 +01:00
|
|
|
$backups = [];
|
|
|
|
foreach ($resticData as $snapshot) {
|
|
|
|
if (
|
|
|
|
!preg_match(
|
|
|
|
"/^(?<id>[a-z0-9]{8}) +(?<year>\d{4})-(?<month>\d{2})-(?<day>\d{2}) +(?<hour>\d{2}):+(?<minute>\d{2}):+(?<second>\d{2}) +(?<host>[^ ]+) +(?<path>[^ ]+).*/",
|
|
|
|
$snapshot,
|
|
|
|
$m
|
|
|
|
)
|
|
|
|
) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
$backupName = $m["host"] . "|" . $m["path"];
|
|
|
|
if (!isset($backups[$backupName])) {
|
|
|
|
$backups[$backupName] = 0;
|
|
|
|
}
|
|
|
|
$backupTime = mktime(
|
|
|
|
$m["hour"],
|
|
|
|
$m["minute"],
|
|
|
|
$m["second"],
|
|
|
|
$m["month"],
|
|
|
|
$m["day"],
|
|
|
|
$m["year"]
|
|
|
|
);
|
|
|
|
if ($backupTime > $backups[$backupName]) {
|
|
|
|
$backups[$backupName] = $backupTime;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ksort($backups);
|
|
|
|
file_put_contents(
|
2023-10-11 00:04:02 +02:00
|
|
|
"/var/www/data/$fileName",
|
2023-02-14 09:32:15 +01:00
|
|
|
json_encode($backups, JSON_UNESCAPED_SLASHES + JSON_PRETTY_PRINT)
|
|
|
|
);
|
|
|
|
exit();
|
|
|
|
}
|
2023-10-11 14:14:41 +02:00
|
|
|
if ($mode === "NO_AUTH") {
|
|
|
|
$fileName = "backups.json";
|
|
|
|
} else {
|
2023-10-11 14:22:54 +02:00
|
|
|
$fileName = strtolower($_GET["user"]);
|
2023-10-11 14:14:41 +02:00
|
|
|
if (!isset($credentials[$fileName])) {
|
|
|
|
header("HTTP/1.0 404 Not Found");
|
|
|
|
echo "user not found";
|
|
|
|
exit();
|
|
|
|
}
|
|
|
|
$fileName .= ".json";
|
2023-10-11 00:04:02 +02:00
|
|
|
}
|
2023-10-11 14:14:41 +02:00
|
|
|
|
2023-02-14 09:32:15 +01:00
|
|
|
header("Content-Type: text/plain");
|
|
|
|
$maxAge = isset($_GET["maxage"]) ? intval($_GET["maxage"]) : 28;
|
|
|
|
$maxAge = $maxAge * 60 * 60;
|
2023-10-11 00:04:02 +02:00
|
|
|
$backups = json_decode(file_get_contents("/var/www/data/$fileName"), true);
|
2023-02-14 11:01:44 +01:00
|
|
|
echo "BACKUP|HOST|PATH|STATUS\n";
|
2023-02-14 09:32:15 +01:00
|
|
|
foreach ($backups as $backupName => $backupTime) {
|
|
|
|
echo "BACKUP|$backupName|";
|
|
|
|
if ($backupTime + $maxAge < time()) {
|
|
|
|
echo "TOO_OLD";
|
|
|
|
} else {
|
|
|
|
echo "OK";
|
|
|
|
}
|
|
|
|
echo "\n";
|
|
|
|
}
|
2023-02-14 11:01:44 +01:00
|
|
|
|
|
|
|
$byHost = [];
|
|
|
|
foreach ($backups as $backupName => $backupTime) {
|
|
|
|
$backupName = explode("|", $backupName);
|
|
|
|
$host = $backupName[0];
|
|
|
|
$path = $backupName[1];
|
|
|
|
if (!isset($byHost[$host])) {
|
|
|
|
$byHost[$host] = PHP_INT_MAX;
|
|
|
|
}
|
|
|
|
$byHost[$host] = min($backupTime, $byHost[$host]);
|
|
|
|
}
|
|
|
|
|
|
|
|
echo "\nBACKUP|HOST|STATUS\n";
|
|
|
|
foreach ($byHost as $host => $latest) {
|
|
|
|
echo "BACKUP|$host|";
|
|
|
|
if ($latest + $maxAge < time()) {
|
|
|
|
echo "TOO_OLD";
|
|
|
|
} else {
|
|
|
|
echo "OK";
|
|
|
|
}
|
|
|
|
echo "\n";
|
|
|
|
}
|