This repository has been archived by the owner on Nov 24, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
weakuser.php
262 lines (209 loc) · 6.38 KB
/
weakuser.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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
<?php
/**
* Note: The MD5 magics are just for a bit of extra stuff, they don't actaully do
* anything security wise since they are easily cracked (e.g. time attacks) and
* most are public.
*/
if (!defined("APP_LOADED")) {
die();
}
class WeakUser {
public $id;
public $token;
public $creator;
public $created;
public $updated;
public $props;
function __construct(string $id) {
$db = new Database("weakuser");
if ($db->has($id)) {
copy_object_vars($this, $db->load($id));
}
else {
$this->id = $id;
$this->token = null;
$this->creator = "";
$this->created = time();
$this->updated = time();
$this->props = new stdClass();
}
}
function save() {
$this->updated = time();
$db = new Database("weakuser");
$db->save($this->id, $this);
}
function pseudodelete() : void {
/**
* Kind of deletes the account by clearing the creator name and setting
* the token to null.
*/
$this->creator = "";
$this->token = null;
}
function is_deleted() : bool {
return $this->token === null;
}
function set_token(string $token) : void {
/**
* Set the login token/password/thing
*/
$this->token = password_hash($token, PASSWORD_ARGON2ID);
}
private function check_token(string $token) : bool {
/**
* Check the user token against the saved one
*/
return $this->token ? password_verify($token, $this->token) : false;
}
function get_if_token_okay(string $token) : ?WeakUser {
/**
* Return $this if the token's okay
*/
return (($this->check_token($token)) ? $this : null);
}
}
function weak_user_current(?string $uid, ?string $token) : ?WeakUser {
/**
* Get the current weak user, given the uid and token
*/
if (!weak_user_exists($uid) || !$uid || !$token) {
return null;
}
$w = new WeakUser($uid);
return $w->get_if_token_okay($token);
}
function weak_user_exists(string $uid) : bool {
/**
* Check if a weak user exists
*/
$db = new Database("weakuser");
return $db->has($uid);
}
$gEndMan->add("weak-user-check", function (Page $page) {
/**
* Check for weak user, create if it does not exist.
*/
// API key because idk
if ($page->get("magic") != md5("theGameInIts")) {
$page->info("error", "There was an error doing that.");
}
$uid = $page->get("uid");
$token = $page->get("token");
// validate weak user info
if (strlen($uid) != 35 || strspn($uid, "abcdef1234567890-") != strlen($uid) || strlen($token) < 64) {
$page->info("error", "There was an error doing that.");
}
// create it if it does not exist
if (!weak_user_exists($uid)) {
$w = new WeakUser($uid);
$w->set_token($token);
$w->save();
}
// Get the weak user
$weak = weak_user_current($uid, $token);
// Set final status
$page->set("status", ($weak !== null) ? "done" : "error");
$page->set("message", ($weak !== null) ? "The login information is correct." : "There was an error doing that.");
});
$gEndMan->add("weak-user-set-name", function (Page $page) {
/**
* Set the name if the weak user does not exist.
*/
// API key because idk
if ($page->get("magic") != md5("thisApiSucks")) {
$page->info("error", "There was an error doing that.");
}
$uid = $page->get("uid");
$token = $page->get("token");
// Get the weak user
$weak = weak_user_current($uid, $token);
if ($weak) {
$weak->creator = $page->get("name", true, 512);
$weak->save();
$page->info("done", "The creator name has been set.");
}
else {
$page->info("not_found", "Your account information could not be loaded.");
}
});
$gEndMan->add("weak-user-lookup", function (Page $page) {
if ($page->get("magic") != md5("justALittleFoxxo")) {
$page->info("error", "There was an error doing that.");
}
$uid = $page->get("uid");
if (!weak_user_exists($uid)) {
$page->info("not_found", "There is no weak user by that name.");
}
$weak = new WeakUser($uid);
$page->set("id", $weak->id);
$page->set("deleted", $weak->is_deleted());
$page->set("creator", $weak->creator);
$page->set("created", $weak->created);
$page->set("updated", $weak->updated);
});
function weak_user_from_page_cookies(Page $page) : ?WeakUser {
return weak_user_current($page->get_cookie("shatter_uid"), $page->get_cookie("shatter_token"));
}
$gEndMan->add("weak-user-login-ui", function (Page $page) {
/**
* Weak user login ui
*/
$page->set_mode(PAGE_MODE_HTML);
KSHeader($page);
if (!$page->has("submit")) {
$form = new Form("./api.php?action=weak-user-login-ui&submit=1");
$form->textbox("uid", "User ID", "");
$form->password("token", "Token", "");
$form->submit("Log in");
$page->add($form);
}
else {
$page->cookie("shatter_uid", $page->get("uid"));
$page->cookie("shatter_token", $page->get("token"));
$page->redirect("./api.php?action=weak-user-delete-ui");
}
});
$gEndMan->add("weak-user-delete-ui", function (Page $page) {
$page->set_mode(PAGE_MODE_HTML);
$user = weak_user_from_page_cookies($page);
if ($user) {
KSHeader($page);
if (!$page->has("submit")) {
$page->add("<h1>Confirm account deletion</h1><p>You are deleting the account with the creator name \"$user->creator\" and the user ID $user->id. <b>This is permanent and cannot be undone.</b></p><div class=\"warning\"><p>Note: Even after you delete your account, data about your segments will still be available, and this user ID can never be used again.</p></div><p><a href=\"./api.php?action=weak-user-delete-ui&submit=1\"><button class=\"red-button\">Delete account</button></p>");
}
else {
$user->pseudodelete();
$user->save();
$page->add("Account deleted");
}
KSFooter($page);
}
else {
$page->add("<h1>Wrong or bad uid or token</h1><p>Please log in to your weak account before doing this. This can also happen if you entered the wrong UID or token.</p>");
}
});
$gEndMan->add("weak-user-delete-admin-ui", function (Page $page) {
$page->set_mode(PAGE_MODE_HTML);
KSHeader($page);
$user = user_get_current();
if ($user && $user->is_admin()) {
if (!$page->has("submit")) {
$page->heading(1, "Delete weak user");
$form = new Form("./api.php?action=weak-user-delete-admin-ui&submit=1");
$form->textbox("uid", "User ID", "");
$form->submit("Delete weak user");
$page->add($form);
}
else {
$weak = new WeakUser($page->get("uid"));
$weak->pseudodelete();
$weak->save();
$page->para("Account deleted");
}
}
else {
$page->info("Not authed", "You are not authed, you need to be authed.");
}
KSFooter($page);
});