-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrestic-index.php
81 lines (73 loc) · 2.81 KB
/
restic-index.php
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
<?php
require("restic-server.php");
require("restic-config.php");
date_default_timezone_set('UTC');
$restic = Restic::Instance($config);
function page_404() {
$restic = Restic::Instance();
$restic->sendStatus(404); //not found
header("Content-Type:");
exit;
}
function route($method, $path, $fn) {
$restic = Restic::Instance();
$rawurl = (!empty($_SERVER['REQUEST_URL']))
? $_SERVER['REQUEST_URL']
: $_SERVER['REQUEST_URI'];
$dir = dirname($_SERVER['SCRIPT_NAME']);
$uri = $dir != "/"
? str_replace($dir, '', $rawurl)
: $rawurl;
$uri = explode("?", $uri, 2);
$path = "/^" . str_replace("/", "\/", $path) . "$/";
$path = preg_replace("/\([^\)]*\)/", "([^\/]+)", $path);
$apply = preg_match($path, $uri[0], $matches);
$first = (sizeof($matches) < 2)
? null
: $matches[1];
if (array_key_exists("PHP_AUTH_USER", $_SERVER)) {
$user = $_SERVER["PHP_AUTH_USER"];
} elseif (array_key_exists("REMOTE_USER", $_SERVER)) {
$user = $_SERVER["REMOTE_USER"];
} elseif (array_key_exists("REDIRECT_REMOTE_USER", $_SERVER)) {
$user = $_SERVER["REDIRECT_REMOTE_USER"];
} elseif (array_key_exists("AUTH_USER", $_SERVER)) {
$user = $_SERVER["AUTH_USER"];
} elseif (array_key_exists("HTTP_AUTHORIZATION", $_SERVER)) {
$user = $_SERVER["HTTP_AUTHORIZATION"];
}
$req_method = array_key_exists("HTTP_X_HTTP_METHOD_OVERRIDE", $_SERVER)
? strtoupper($_SERVER["HTTP_X_HTTP_METHOD_OVERRIDE"])
: strtoupper($_SERVER["REQUEST_METHOD"]);
if ($req_method == $method && $apply == 1) {
if ($restic->private_repos && $user !== $first) {
$restic->sendStatus(401); //Unauthorized
header("Content-Type:");
exit;
}
call_user_func_array(array($restic, $fn), array_slice($matches, 1));
exit;
}
}
route("HEAD", "/config", "checkConfig");
route("HEAD", "/(repo)/config", "checkConfig");
route("GET", "/config", "getConfig");
route("GET", "/(repo)/config", "getConfig");
route("POST", "/config", "saveConfig");
route("POST", "/(repo)/config", "saveConfig");
route("DELETE", "/config", "deleteConfig");
route("DELETE", "/(repo)/config", "deleteConfig");
route("GET", "/(type)/", "listBlobs");
route("GET", "/(repo)/(type)/", "listBlobs");
route("HEAD", "/(type)/(name)", "checkBlob");
route("HEAD", "/(repo)/(type)/(name)", "checkBlob");
route("GET", "/(type)/(name)", "getBlob");
route("GET", "/(repo)/(type)/(name)", "getBlob");
route("POST", "/(type)/(name)", "saveBlob");
route("POST", "/(repo)/(type)/(name)", "saveBlob");
route("DELETE", "/(type)/(name)", "deleteBlob");
route("DELETE", "/(repo)/(type)/(name)", "deleteBlob");
route("POST", "/", "createRepo");
route("POST", "/(repo)", "createRepo");
route("POST", "/(repo)/", "createRepo");
page_404();