-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.php
100 lines (78 loc) · 2.32 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
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
<?php
// Public rendering engine.
require_once __DIR__ . "/src/core.php";
require_once __DIR__ . '/vendor/autoload.php';
$git = new CzProject\GitPhp\Git;
$alnum = '([a-zA-Z0-9_\-\.]+)';
$ns_pattern = "^/~?{$alnum}";
function mountRepo($namespace, $repo_name) {
global $git;
$repo_path = path_join(SCAN_PATH, $namespace, $repo_name);
if(!in_array($namespace, NAMESPACES)) return false;
if(!\core\repoExists($repo_path)) return false;
else return $git->open($repo_path);
}
switch(true) {
case $path == "/":
$page ??= "listing";
break;
case route("@/api/graph/?(\d{4})?@"):
header('Content-Type: image/svg+xml');
header('Cache-Control: max-age=86400');
$year = $params[1] ?? date("Y");
$color = @$_GET['c'] ?? '7426e2';
$mode = @$_GET['m'] ?? 'light';
echo \core\generateGraph($git, $year, $color, $mode);
exit;
// Redirect bare namespaces to /
case route("@{$ns_pattern}/?$@"):
header("Location: /");
exit;
case scope("@{$ns_pattern}/{$alnum}.git/(.*)@"):
$namespace = $params[1];
$repo_name = $params[2];
if($repo = mountRepo($namespace, $repo_name)) {
header('Content-Type: application/octet-stream');
$request_path = \core\resolveDumbClone($repo, $params[3]);
match(true) {
$request_path == false => http_response_code(403),
!is_file($request_path) => http_response_code(404),
default => readfile($request_path),
};
exit;
}
case scope("@{$ns_pattern}/{$alnum}@"):
$namespace = $params[1];
$repo_name = $params[2];
$repo_url = "/~{$namespace}/{$repo_name}";
if($repo = mountRepo($namespace, $repo_name)) {
switch(true) {
case route("@^/?$@"):
$page ??= "summary";
break;
case route("@/log/(.*)$@"):
$page ??= "log";
$branch = $params[1];
break;
}
if(isset($page)) break;
}
default:
http_response_code(404);
$page = "404";
break;
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<title>{du}punkto git repositories</title>
<style>
<?php include __DIR__ . "/gui/main.css" ?>
</style>
</head>
<body>
<?php if(isset($repo) && $repo != false) include __DIR__ . "/gui/header.php" ?>
<?php if(isset($page) && $page != false) include __DIR__ . "/gui/$page.php" ?>
</body>
</html>