-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.php
42 lines (36 loc) · 1.16 KB
/
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
<?php
require_once 'lib/response.php';
require_once 'lib/request.php';
function route(Request $request): Response {
try {
if ($request->method !== 'GET') return new MethodNotAllowed();
if (!authenticate($request)) return new Unauthorized();
$x = $request->url[strlen($request->len) - 1] === '/' ? 'dump' : 'read';
$x = shell_exec("dconf {$x} {$request->url}");
if (!$x) $x = '';
return new Response(
200,
['Content-Type' => 'text/plain'],
$x,
);
} catch (\Throwable $e) { return handle_error($e); }
}
function authenticate(Request $request): bool {
$x = $request->header('Authorization');
if (!$x) return false;
if (strlen($x) < 7) return false;
$x = base64_decode(substr($x, 6), true);
if (!$x) return false;
$x = substr($x, 0, -1);
return $x === getenv('TOKEN');
}
function handle_error(\Throwable $e): Response {
error_log($e->getMessage() . "\n" .$e->getTraceAsString());
return new InternalServerError($e->getMessage());
}
function main() {
$request = new Request();
$response = route($request);
$response->serve();
}
main();