This repository has been archived by the owner on Nov 9, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathresult.php
executable file
·217 lines (189 loc) · 7.01 KB
/
result.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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
<?php
session_start();
if (isset($_POST["uname"]) && isset($_POST["message"])) {
$name = $_POST["uname"];
$msg = $_POST["message"];
} // Else it might be an admin page request
$time = date('m-d') . " " . date('h:i a');
$utime = strval(time());
$client_ip = $_SERVER['REMOTE_ADDR'];
// --- User set variables ---
$MAX_MSG_LEN = 512; // Max message lenght
$MAX_NAME_LEN = 32; // Max name lenght
$ALLOW_ANONS = true; // Allow anonymous posters (IP still gets logged)
$INDEX_PAGE = "index.php"; // The index page of guestbook, the one you post from
$BAN_LIST = "/home/www-data/bans.json"; // Banned IP/word list
$COMMENT_FILE = "comments.json"; // Where the comment JSON will be stored
$LOG_FILE = "/home/www-data/log.json"; // Log file
$RATE_LIMIT = "120"; // Rate limit (in seconds)
$ADMIN_SECRET = "verysecretpassword"; // Admin password, plain text for extra security.
// --------------------------
function readjson($fname) {
$file = file_get_contents($fname);
$json = json_decode($file);
return $json;
}
function checkwords($str, $words) {
$split = preg_split('/\s+/', $str);
$msg = "";
global $client_ip;
global $utime;
foreach ($split as $word) {
if (in_array($word, $words)) {
echo '<p id="error">You used a banned word.</p>';
wlog("bword", $client_ip, $utime, "{$str}");
exit();
} else {
$msg = $msg . " {$word}";
}
}
return substr($msg, 1);
}
function wlog($type, $ip, $utime, $additional) {
global $LOG_FILE;
$j = readjson($LOG_FILE);
if ($type == "success") {
$i = count($j->success);
$j->success[$i]->type = $type;
$j->success[$i]->utime = $utime;
$j->success[$i]->ip = $ip;
$j->success[$i]->add = $additional;
} else {
$i = count($j->err);
$j->err[$i]->type = $type;
$j->err[$i]->utime = $utime;
$j->err[$i]->ip = $ip;
$j->err[$i]->add = $additional;
}
$j = json_encode($j);
file_put_contents($LOG_FILE, $j);
return;
}
function findkey($key, $arr) {
$k = NULL;
$i = 0;
foreach ($arr as $x) {
foreach ($x as $y) {
if ($y == $key) {
$k = $key;
goto l_end;
}
}
$i++;
}
l_end:
return $k == $key ? $i : NULL;
}
function findall($key, $arr) {
$r = array();
foreach ($arr as $a) {
foreach ($a as $x) {
if ($x == $key) {
array_push($r, $a);
}
}
}
return $r;
}
// ----------------------------------------
// admin page things
// ----------------------------------------
if (isset($_POST['req']) && $_POST['req'] == "adminp") {
if (!isset($_POST['adminsecret']))
exit();
if ($_POST['adminsecret'] != $ADMIN_SECRET)
exit();
// Delete a comment
if (isset($_POST['del_id'])) {
$f = readjson($COMMENT_FILE);
$id = $_POST['del_id'];
unset($f->comments[$id]);
$f->comments = array_values($f->comments);
file_put_contents($COMMENT_FILE, json_encode($f));
exit();
}
$filter = $_POST['filter'];
$f = new StdClass();
$log = readjson($LOG_FILE);
if ($filter == "success") {
$f->elements = $log->success;
} else if ($filter == "ban") {
$f->elements = findall("ban", $log->err);
} else if ($filter == "anon") {
$f->elements = findall("anon", $log->err);
} else if ($filter == "rate_limit") {
$f->elements = findall("rate_limit", $log->err);
} else if ($filter == "namelen") {
$f->elements = findall("namelen", $log->err);
} else if ($filter == "msglen") {
$f->elements = findall("msglen", $log->err);
} else if ($filter == "admin") {
$f->elements = findall("admin", $log->err);
} else if ($filter == "bword") {
$f->elements = findall("bword", $log->err);
} else if ($filter == "errors") {
$f->elements = $log->err;
} else if ($filter == "posts") {
$posts = readjson($COMMENT_FILE);
$f->elements = $posts->comments;
}
echo json_encode($f);
exit();
} else { // Apply the CSS
echo '<link rel="stylesheet" type="text/css" href="style.css">';
}
// ----------------------------------------
// ----------------------------------------
if ($msg != "") {
$log = file_get_contents($LOG_FILE);
$logs = json_decode($log, true);
$bans = readjson($BAN_LIST);
foreach ($bans->ip as $ip_ban) {
if ($client_ip == $ip_ban) {
wlog("ban", $client_ip, $utime, "${name}:${msg}");
echo '<p id="error">You are banned from making posts.</p>';
exit();
}
}
if ($name == "") {
if ($ALLOW_ANONS === true) {
$name = "Anonymous";
} else {
wlog("anon", $client_ip, $utime, "{$msg}");
echo '<p id="error">Anonymous posting is not allowed</p>';
exit();
}
}
$key = findkey($client_ip, array_reverse($logs['success']));
if ($key >= 0) {
$ar = array_reverse($logs['success']);
$t = $utime - $ar[$key]['utime'];
if ($t < $RATE_LIMIT) {
wlog("rate_limit", $client_ip, $utime, "");
echo '<p id="error">You\'re posting too fast.</p>';
exit();
}
}
$comjson = readjson($COMMENT_FILE);
if (strlen($name) > $MAX_NAME_LEN) {
wlog("namelen", $client_ip, $utime, "{$name}");
echo '<p id ="error">Name was too large (>{$MAX_NAME_LEN} characters)';
exit();
}
if (strlen($msg) > $MAX_MSG_LEN) {
wlog("msglen", $client_ip, $utime, "{$name}:{$msg}");
echo '<p id="error">Message was too large (>{$MAX_MSG_LEN} characters)';
exit();
}
$msg = checkwords($msg, $bans->words);
$name = checkwords($name, $bans->words);
$i = count($comjson->comments);
$comjson->comments[$i]->name = $name;
$comjson->comments[$i]->data = $msg;
$comjson->comments[$i]->time = $time;
$jsonout = json_encode($comjson);
file_put_contents($COMMENT_FILE, $jsonout);
wlog("success", $client_ip, $utime, "");
}
header("Location: {$INDEX_PAGE}");
?>