Skip to content

Commit 49328cb

Browse files
committed
Added support for private repos
1 parent 64bf415 commit 49328cb

File tree

2 files changed

+30
-8
lines changed

2 files changed

+30
-8
lines changed

restic-index.php

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@
22

33
require("restic-server.php");
44
date_default_timezone_set('UTC');
5-
$basePath = "restic";
6-
$restic = Restic::Instance($basePath);
5+
$restic = Restic::Instance(Array(
6+
"path" => "restic",
7+
"private_repos" => false
8+
));
79

810
function page_404() {
911
$restic = Restic::Instance();
@@ -24,8 +26,16 @@ function route($method, $path, $fn) {
2426
$path = "/^" . str_replace("/", "\/", $path) . "$/";
2527
$path = preg_replace("/\([^\)]*\)/", "([^\/]+)", $path);
2628
$apply = preg_match($path, $uri[0], $matches);
29+
$first = (sizeof($matches) < 2)
30+
? null
31+
: $matches[1];
2732

2833
if ($_SERVER["REQUEST_METHOD"] == $method && $apply == 1) {
34+
if ($restic->private_repos && !(isset($_SERVER["PHP_AUTH_USER"]) && $first == $_SERVER["PHP_AUTH_USER"])) {
35+
$restic->sendStatus(401); //Unauthorized
36+
header("Content-Type:");
37+
exit;
38+
}
2939
call_user_func_array(Array($restic, $fn), array_slice($matches, 1));
3040
exit;
3141
}

restic-server.php

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,28 @@ class Restic
44
public $validTypes = Array("data", "index", "keys", "locks", "snapshots", "config");
55
public $mimeTypeAPIV1 = "application/vnd.x.restic.rest.v1";
66
public $mimeTypeAPIV2 = "application/vnd.x.restic.rest.v2";
7-
protected $block_size = 8192;
7+
private $block_size = 8192;
8+
private $basePath = "restic";
9+
public $private_repos = false;
810

9-
protected $basePath;
1011

11-
private function __construct($path = ".")
12+
private function __construct($opts)
1213
{
13-
$this->basePath = $path;
14+
if (isset($opts["path"])) {
15+
$this->basePath = $opts["path"];
16+
}
17+
if (isset($opts["private_repos"])) {
18+
$this->private_repos = $opts["private_repos"];
19+
}
20+
if (isset($opts["block_size"])) {
21+
$this->block_size = $opts["block_size"];
22+
}
1423
}
15-
public static function Instance($path = ".")
24+
public static function Instance($opts = Array())
1625
{
1726
static $inst = null;
1827
if ($inst === null) {
19-
$inst = new Restic($path);
28+
$inst = new Restic($opts);
2029
}
2130
return $inst;
2231
}
@@ -29,6 +38,9 @@ public function sendStatus($status)
2938
case 400:
3039
header($_SERVER["SERVER_PROTOCOL"] . " 400 Bad Request");
3140
break;
41+
case 401:
42+
header($_SERVER["SERVER_PROTOCOL"] . " 401 Unauthorized");
43+
break;
3244
case 403:
3345
header($_SERVER["SERVER_PROTOCOL"] . " 403 Forbidden");
3446
break;

0 commit comments

Comments
 (0)