-
Notifications
You must be signed in to change notification settings - Fork 0
/
dumbwiki.php
140 lines (123 loc) · 4.06 KB
/
dumbwiki.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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
<?php
//echo phpinfo(); die;
$qs = $_SERVER['REDIRECT_QUERY_STRING'] ?? 'index.html';
$qs = urldecode($qs);
$filename = basename($qs);
$ext = pathinfo($filename, PATHINFO_EXTENSION);
function get_file($filename) {
return file_get_contents('../content/' . $filename);
}
function write_file($filename, $contents) {
file_put_contents('../content/' . $filename, $contents);
// Log change
//die(shell_exec("cd ../content; git add .; env GIT_COMMITTER_NAME='Filk Server' GIT_COMMITTER_EMAIL='[email protected]' git commit --author='Unknown <" . escapeshellarg($_SERVER['REMOTE_ADDR']) . ">' -m 'Change '" . $filename . " 2>&1"));
// TODO: use the site username/email as the --author
exec("cd ../content; git add .; env GIT_COMMITTER_NAME='Filk Server' GIT_COMMITTER_EMAIL='[email protected]' git commit --author='Unknown <" . escapeshellarg($_SERVER['REMOTE_ADDR']) . ">' -m 'File '" . escapeshellarg($filename));
}
function remove_file($filename) {
unlink('../content/' . $filename);
exec("cd ../content; git add .; env GIT_COMMITTER_NAME='Filk Server' GIT_COMMITTER_EMAIL='[email protected]' git commit --author='Unknown <" . escapeshellarg($_SERVER['REMOTE_ADDR']) . ">' -m 'Remove '" . escapeshellarg($filename));
}
function template($title, $body, $head = '', $end = '') {
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title><?= $title ?></title>
<link rel="stylesheet" type="text/css" media="all" href="/css/normalize.css" />
<link rel="stylesheet" type="text/css" media="all" href="/css/style.css" />
<link rel="stylesheet" type="text/css" media="all" href="/css/icons.css" />
<?= $head ?>
</head>
<body>
<?= $body ?>
<?= $end ?>
</body>
</html>
<?php
}
if (substr($qs, 0, 7) === 'remove/') {
if (!in_array(strtolower($ext), ['txt', 'html']))
die;
remove_file($filename);
header('Location: ../');
die;
}
if (substr($qs, 0, 5) === 'edit/') {
if (!in_array(strtolower($ext), ['txt', 'html']))
die;
$contents = trim(get_file($filename));
if (isset($_POST['body'])) {
if (sha1($contents) !== $_POST['former'])
die("File changed while editing. Please copy your edits, refresh, and make them again.");
write_file($filename, str_replace("\r\n", "\n", $_POST['body']) . "\n");
header('Location: ../' . substr($qs, 5));
die;
}
ob_start();
?>
<form method="POST">
<input name="former" type="hidden" value="<?= sha1($contents) ?>" />
<textarea name="body"><?= $contents ?></textarea>
<input type="submit" />
</form>
<?php
$body = ob_get_clean();
ob_start();
?>
<style>
textarea { width: 100%; height: 80ex; }
textarea { font-family:Consolas,Monaco,Lucida Console,Liberation Mono,DejaVu Sans Mono,Bitstream Vera Sans Mono,Courier New, monospace; }
</style>
<?php
$head = ob_get_clean();
ob_start();
?>
<script>
// https://stackoverflow.com/a/22673622/118153
document.querySelector('textarea').addEventListener("keydown", function(e) {
if (e.which===9) {
e.preventDefault();
document.execCommand("insertText", false, "\t");
}
}, false);
</script>
<?php
$end = ob_get_clean();
template("Editing $filename", $body, $head, $end);
die;
}
if (substr($qs, 0, 7) === 'search/' || substr($qs, 0, 10) === 'txtsearch/') {
$offset = substr($qs, 0, 7) === 'search/' ? 7 : 10;
$term = substr($qs, $offset);
exec('rg -i ' . escapeshellarg($term) . ' *.' . ($offset === 7 ? 'html' : 'txt'), $out);
ob_start();
?>
<ul>
<?php foreach ($out as $line): ?>
<?php $result = explode(':', $line, 2) ?>
<li><a href="../<?= $result[0] ?>"><?= $result[0] ?></a>: <?= htmlspecialchars($result[1]) ?></li>
<?php endforeach; ?>
</ul>
<?php
$body = ob_get_clean();
template("Searching for " . htmlspecialchars($term), $body);
die;
}
if (strtolower($ext) == 'txt') {
header("Content-Type: text/plain; charset=utf-8");
echo get_file($filename);
die;
}
$body = get_file($filename);
$has_title = substr($body, 0, 7) === 'Title: ';
if ($has_title) {
$pos = strpos($body, "\n\n");
$title = substr($body, 7, $pos - 7);
$body = substr($body, $pos + 2);
template($title, $body);
die;
}
http_response_code(404);
//die("Whoops - contact Iiridayn");