· 8 years ago · Apr 04, 2018, 07:28 PM
1<?php
2// We aren't calling the class Do because otherwise it would conflict with do { } while ();
3class D {
4 public static function rankUnrankBeatmap() {
5 $id = $_GET['id'];
6 $stat = current($GLOBALS['db']->fetch('SELECT ranked FROM beatmaps_names WHERE id = ?', $id));
7
8 if($stat <= 0) {
9 // Rank dis shit
10 $GLOBALS['db']->execute('UPDATE beatmaps_names SET ranked = ? WHERE id = ?', [1, $id]);
11 } else {
12 // Unrank dis shit
13 $GLOBALS['db']->execute('UPDATE beatmaps_names SET ranked = ? WHERE id = ?', [0, $id]);
14 }
15 redirect('/index.php?p=112');
16 }
17 public static function searchUser() {
18
19 $searched = strtolower($_GET['name']);
20 $users = $GLOBALS['db']->fetchAll('SELECT username FROM users');
21 foreach($users as $user) {
22 $user = strtolower(current($user));
23
24 if($user == $searched || getUserID($user) == $searched) {
25 redirect('/index.php?u='.getUserID($user));
26 }
27 }
28 redirect('/index.php?u=1');
29 }
30 /*
31 * ChangeBackground
32 * Sets a users bg-link in users_stats to
33 * $_POST['link']
34 */
35 public static function changeBackground() {
36 // Lets check if the extension is right
37 $link = strtolower($_POST['link']);
38 $dude = $_POST['name'];
39 // Everything is good, lets set
40 $GLOBALS['db']->execute('UPDATE users_stats SET backgroundlink = ? WHERE id = ?', [$link, getUserID($dude)]);
41
42 redirect('index.php?p=5');
43 }
44 /*
45 * Register
46 * Register function
47 */
48 public static function Register() {
49 try {
50 // Check if everything is set
51 if (empty($_POST['u']) || empty($_POST['p1']) || empty($_POST['p2']) || empty($_POST['e'])) {
52 throw new Exception(0);
53 }
54 // Validate password through our helper
55 $pres = PasswordHelper::ValidatePassword($_POST['p1'], $_POST['p2']);
56 if ($pres !== -1) {
57 throw new Exception($pres);
58 }
59 // Check if email is valid
60 if (!filter_var($_POST['e'], FILTER_VALIDATE_EMAIL)) {
61 throw new Exception(4);
62 }
63 // Check if username is valid
64 if (!preg_match('/^[A-Za-z0-9 _\\-\\[\\]]{3,20}$/i', $_POST['u'])) {
65 throw new Exception(5);
66 }
67 // Make sure username is not forbidden
68 if (UsernameHelper::isUsernameForbidden($_POST['u'])) {
69 throw new Exception(9);
70 }
71 // Check if username is already in db
72 if ($GLOBALS['db']->fetch('SELECT * FROM users WHERE username = ?', $_POST['u'])) {
73 throw new Exception(6);
74 }
75 // Check if email is already in db
76 if ($GLOBALS['db']->fetch('SELECT * FROM users WHERE email = ?', $_POST['e'])) {
77 throw new Exception(7);
78 }
79 // Check if ip is already in db
80 if ($GLOBALS['db']->fetch('SELECT * FROM users WHERE ip = ?', getIP())) {
81 throw new Exception(8);
82 }
83 // Create password
84 $md5Password = password_hash(md5($_POST['p1']), PASSWORD_DEFAULT);
85 $ip = getIP();
86 // Put some data into the db
87 $GLOBALS['db']->execute("INSERT INTO `users`(username, ip, password_md5, salt, email, register_datetime, rank, allowed, password_version)
88 VALUES (?, ?, ?, '', ?, ?, 1, 2, 2);", [$_POST['u'], $ip,$md5Password, $_POST['e'], time(true)]);
89 // Get user ID
90 $uid = $GLOBALS['db']->lastInsertId();
91 // Put some data into users_stats
92 $GLOBALS['db']->execute("INSERT INTO `users_stats`(id, username, user_color, user_style, ranked_score_std, playcount_std, total_score_std, ranked_score_taiko, playcount_taiko, total_score_taiko, ranked_score_ctb, playcount_ctb, total_score_ctb, ranked_score_mania, playcount_mania, total_score_mania, country) VALUES (?, ?, 'black', '', 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ?);", [$uid, $_POST['u'], getUserCountry()]);
93 // Update leaderboard (insert new user) for each mode.
94 foreach (['std', 'taiko', 'ctb', 'mania'] as $m) {
95 Leaderboard::Update($uid, 0, $m);
96 }
97 // All fine, done
98 redirect('index.php?p=3&s=lmao');
99 }
100 catch(Exception $e) {
101 // Redirect to Exception page
102 redirect('index.php?p=3&e='.$e->getMessage());
103 }
104 }
105
106 /*
107 * Wipeuser
108 * Deletes (hopefully) everything from a user
109 */
110 public static function wipe() {
111 $user = $_GET['user'];
112
113 $GLOBALS['db']->execute('DELETE FROM scores WHERE username = ?', [$user]);
114 $GLOBALS['db']->execute('DELETE FROM users WHERE username = ?', [$user]);
115 $GLOBALS['db']->execute('DELETE FROM users_stats WHERE username = ?', [$user]);
116 $GLOBALS['db']->execute('DELETE FROM users_relationships WHERE user1 = ? OR user2 = ?', [getUserID($user)]);
117
118 redirect('index.php?p=102&s=User Wiped!');
119 }
120 /*
121 * ChangePassword
122 * Change password function
123 */
124 public static function ChangePassword() {
125 try {
126 // Check if we are logged in
127 sessionCheck();
128 // Check if everything is set
129 if (empty($_POST['pold']) || empty($_POST['p1']) || empty($_POST['p2'])) {
130 throw new Exception(0);
131 }
132 $pres = PasswordHelper::ValidatePassword($_POST['p1'], $_POST['p2']);
133 if ($pres !== -1) {
134 throw new Exception($pres);
135 }
136 if (!PasswordHelper::CheckPass($_SESSION['username'], $_POST['pold'], false)) {
137 throw new Exception(4);
138 }
139 // Calculate new password
140 $newPassword = password_hash(md5($_POST['p1']), PASSWORD_DEFAULT);
141 // Change both passwords and salt
142 $GLOBALS['db']->execute("UPDATE users SET password_md5 = ?, password_version = 2, salt = '' WHERE username = ?", [$newPassword, $_SESSION['username']]);
143 // Set in session that we've changed our password otherwise sessionCheck() will kick us
144 $_SESSION['passwordChanged'] = true;
145 // Redirect to success page
146 redirect('index.php?p=7&s=done');
147 }
148 catch(Exception $e) {
149 // Redirect to Exception page
150 redirect('index.php?p=7&e='.$e->getMessage());
151 }
152 }
153
154 /*
155 * RecoverPassword()
156 * Form submission for printPasswordRecovery.
157 */
158 public static function RecoverPassword() {
159 global $MailgunConfig;
160 try {
161 if (!isset($_POST['username']) || empty($_POST['username'])) {
162 throw new Exception(0);
163 }
164 $username = $_POST['username'];
165 $user = $GLOBALS['db']->fetch('SELECT username, email, allowed FROM users WHERE username = ?', [$username]);
166 // Check the user actually exists.
167 if (!$user) {
168 throw new Exception(1);
169 }
170 if ($user['allowed'] == '0') {
171 throw new Exception(2);
172 }
173 $key = randomString(80);
174 $GLOBALS['db']->execute('INSERT INTO password_recovery (k, u) VALUES (?, ?);', [$key, $username]);
175 require_once dirname(__FILE__).'/SimpleMailgun.php';
176 $mailer = new SimpleMailgun($MailgunConfig);
177 $mailer->Send('Ripple <noreply@'.$MailgunConfig['domain'].'>', $user['email'], 'Ripple password recovery instructions', sprintf("Hey %s! Someone, which we really hope was you, requested a password reset for your account. In case it was you, please <a href='%s'>click here</a> to reset your password on Ripple. Otherwise, silently ignore this email.", $username, 'http://'.$_SERVER['HTTP_HOST'].'/index.php?p=19&k='.$key.'&user='.$username));
178 redirect('index.php?p=18&s=sent');
179 }
180 catch(Exception $e) {
181 redirect('index.php?p=18&e='.$e->getMessage());
182 }
183 }
184
185 /*
186 * SaveSystemSettings
187 * Save system settings function (ADMIN CP)
188 */
189 public static function SaveSystemSettings() {
190 try {
191 // Get values
192 if (isset($_POST['wm'])) {
193 $wm = $_POST['wm'];
194 } else {
195 $wm = 0;
196 }
197 if (isset($_POST['gm'])) {
198 $gm = $_POST['gm'];
199 } else {
200 $gm = 0;
201 }
202 if (isset($_POST['r'])) {
203 $r = $_POST['r'];
204 } else {
205 $r = 0;
206 }
207 if (!empty($_POST['ga'])) {
208 $ga = $_POST['ga'];
209 } else {
210 $ga = '';
211 }
212 if (!empty($_POST['ha'])) {
213 $ha = $_POST['ha'];
214 } else {
215 $ha = '';
216 }
217 // Save new values
218 $GLOBALS['db']->execute("UPDATE system_settings SET value_int = ? WHERE name = 'website_maintenance'", [$wm]);
219 $GLOBALS['db']->execute("UPDATE system_settings SET value_int = ? WHERE name = 'game_maintenance'", [$gm]);
220 $GLOBALS['db']->execute("UPDATE system_settings SET value_int = ? WHERE name = 'registrations_enabled'", [$r]);
221 $GLOBALS['db']->execute("UPDATE system_settings SET value_string = ? WHERE name = 'website_global_alert'", [$ga]);
222 $GLOBALS['db']->execute("UPDATE system_settings SET value_string = ? WHERE name = 'website_home_alert'", [$ha]);
223 // Done, redirect to success page
224 redirect('index.php?p=101&s=Settings saved!');
225 }
226 catch(Exception $e) {
227 // Redirect to Exception page
228 redirect('index.php?p=101&e='.$e->getMessage());
229 }
230 }
231
232 /*
233 * SaveBanchoSettings
234 * Save bancho settings function (ADMIN CP)
235 */
236 public static function SaveBanchoSettings() {
237 try {
238 // Get values
239 if (isset($_POST['bm'])) {
240 $bm = $_POST['bm'];
241 } else {
242 $bm = 0;
243 }
244 if (isset($_POST['od'])) {
245 $od = $_POST['od'];
246 } else {
247 $od = 0;
248 }
249 if (isset($_POST['rm'])) {
250 $rm = $_POST['rm'];
251 } else {
252 $rm = 0;
253 }
254 if (!empty($_POST['mi'])) {
255 $mi = $_POST['mi'];
256 } else {
257 $mi = '';
258 }
259 if (!empty($_POST['lm'])) {
260 $lm = $_POST['lm'];
261 } else {
262 $lm = '';
263 }
264 if (!empty($_POST['ln'])) {
265 $ln = $_POST['ln'];
266 } else {
267 $ln = '';
268 }
269 if (!empty($_POST['cv'])) {
270 $cv = $_POST['cv'];
271 } else {
272 $cv = '';
273 }
274 if (!empty($_POST['cmd5'])) {
275 $cmd5 = $_POST['cmd5'];
276 } else {
277 $cmd5 = '';
278 }
279 // Save new values
280 $GLOBALS['db']->execute("UPDATE bancho_settings SET value_int = ? WHERE name = 'bancho_maintenance'", [$bm]);
281 $GLOBALS['db']->execute("UPDATE bancho_settings SET value_int = ? WHERE name = 'free_direct'", [$od]);
282 $GLOBALS['db']->execute("UPDATE bancho_settings SET value_int = ? WHERE name = 'restricted_joke'", [$rm]);
283 $GLOBALS['db']->execute("UPDATE bancho_settings SET value_string = ? WHERE name = 'menu_icon'", [$mi]);
284 $GLOBALS['db']->execute("UPDATE bancho_settings SET value_string = ? WHERE name = 'login_messages'", [$lm]);
285 $GLOBALS['db']->execute("UPDATE bancho_settings SET value_string = ? WHERE name = 'login_notification'", [$ln]);
286 $GLOBALS['db']->execute("UPDATE bancho_settings SET value_string = ? WHERE name = 'osu_versions'", [$cv]);
287 $GLOBALS['db']->execute("UPDATE bancho_settings SET value_string = ? WHERE name = 'osu_md5s'", [$cmd5]);
288 // Done, redirect to success page
289 redirect('index.php?p=111&s=Settings saved!');
290 }
291 catch(Exception $e) {
292 // Redirect to Exception page
293 redirect('index.php?p=111&e='.$e->getMessage());
294 }
295 }
296
297 /*
298 * RunCron
299 * Runs cron.php from admin cp with exec/redirect
300 */
301 public static function RunCron() {
302 if ($CRON['adminExec']) {
303 // howl master linux shell pr0
304 exec(PHP_BIN_DIR.'/php '.dirname(__FILE__).'/../cron.php 2>&1 > /dev/null &');
305 } else {
306 // Run from browser
307 redirect('./cron.php');
308 }
309 }
310
311 /*
312 * SaveEditUser
313 * Save edit user function (ADMIN CP)
314 */
315 public static function SaveEditUser() {
316 try {
317 // Check if everything is set (username color, username style, rank and allowed can be empty)
318 if (!isset($_POST['id']) || !isset($_POST['u']) || !isset($_POST['e']) || !isset($_POST['up']) || !isset($_POST['aka']) || !isset($_POST['se']) || !isset($_POST['sr']) || empty($_POST['id']) || empty($_POST['u']) || empty($_POST['e'])) {
319 throw new Exception('Nice troll');
320 }
321 // Check if this user exists
322 $id = current($GLOBALS['db']->fetch('SELECT id FROM users WHERE id = ?', $_POST['id']));
323 if (!$id) {
324 throw new Exception("That user doesn\'t exists");
325 }
326 // Check if we can edit this user
327 if (getUserRank($_POST['u']) >= getUserRank($_SESSION['username']) && $_POST['u'] != $_SESSION['username']) {
328 throw new Exception("You dont't have enough permissions to edit this user");
329 }
330 // Check if email is valid
331 if (!filter_var($_POST['e'], FILTER_VALIDATE_EMAIL)) {
332 throw new Exception("The email isn't valid");
333 }
334 // Check if silence end has changed. if so, we have to kick the client
335 // in order to silence him
336 //$oldse = current($GLOBALS["db"]->fetch("SELECT silence_end FROM users WHERE username = ?", array($_POST["u"])));
337 // Save new data (email, silence end and silence reason)
338 $GLOBALS['db']->execute('UPDATE users SET email = ?, silence_end = ?, silence_reason = ? WHERE id = ?', [$_POST['e'], $_POST['se'], $_POST['sr'], $_POST['id']]);
339 // Save new userpage
340 $GLOBALS['db']->execute('UPDATE users_stats SET userpage_content = ? WHERE id = ?', [$_POST['up'], $_POST['id']]);
341 // Save new data if set (rank, allowed, UP and silence)
342 if (isset($_POST['r']) && !empty($_POST['r'])) {
343 $GLOBALS['db']->execute('UPDATE users SET rank = ? WHERE id = ?', [$_POST['r'], $_POST['id']]);
344 }
345 if (isset($_POST['a'])) {
346 $GLOBALS['db']->execute('UPDATE users SET allowed = ? WHERE id = ?', [$_POST['a'], $_POST['id']]);
347 }
348 // Get username style/color
349 if (isset($_POST['c']) && !empty($_POST['c'])) {
350 $c = $_POST['c'];
351 } else {
352 $c = 'black';
353 }
354 if (isset($_POST['bg']) && !empty($_POST['bg'])) {
355 $bg = $_POST['bg'];
356 } else {
357 $bg = '';
358 }
359 // Set username style/color/aka
360 $GLOBALS['db']->execute('UPDATE users_stats SET user_color = ?, user_style = ?, username_aka = ? WHERE id = ?', [$c, $bg, $_POST['aka'], $_POST['id']]);
361 // Done, redirect to success page
362 redirect('index.php?p=102&s=User edited!');
363 }
364 catch(Exception $e) {
365 // Redirect to Exception page
366 redirect('index.php?p=102&e='.$e->getMessage());
367 }
368 }
369
370 /*
371 * BanUnbanUser
372 * Ban/Unban user function (ADMIN CP)
373 */
374 public static function BanUnbanUser() {
375 try {
376 // Check if everything is set
377 if (empty($_GET['id'])) {
378 throw new Exception('Nice troll.');
379 }
380 // Get username
381 $username = current($GLOBALS['db']->fetch('SELECT username FROM users WHERE id = ?', $_GET['id']));
382 // Check if we can ban this user
383 if (getUserRank($username) >= getUserRank($_SESSION['username'])) {
384 throw new Exception("You dont't have enough permissions to ban this user");
385 }
386 // Get current allowed value of this user
387 $allowed = current($GLOBALS['db']->fetch('SELECT allowed FROM users WHERE id = ?', $_GET['id']));
388 // Get new allowed value
389 if ($allowed == 1) {
390 $newAllowed = 0;
391 } else {
392 $newAllowed = 1;
393 }
394 // Change allowed value
395 $GLOBALS['db']->execute('UPDATE users SET allowed = ? WHERE id = ?', [$newAllowed, $_GET['id']]);
396 // Done, redirect to success page
397 redirect('index.php?p=102&s=User banned/unbanned/activated!');
398 }
399 catch(Exception $e) {
400 // Redirect to Exception page
401 redirect('index.php?p=102&e='.$e->getMessage());
402 }
403 }
404
405 /*
406 * QuickEditUser
407 * Redirects to the edit user page for the user with $_POST["u"] username
408 */
409 public static function QuickEditUser() {
410 try {
411 // Check if everything is set
412 if (empty($_POST['u'])) {
413 throw new Exception('Nice troll.');
414 }
415 // Get user id
416 $id = current($GLOBALS['db']->fetch('SELECT id FROM users WHERE username = ?', $_POST['u']));
417 // Check if that user exists
418 if (!$id) {
419 throw new Exception("That user doesn't exists");
420 }
421 // Done, redirect to edit page
422 redirect('index.php?p=103&id='.$id);
423 }
424 catch(Exception $e) {
425 // Redirect to Exception page
426 redirect('index.php?p=102&e='.$e->getMessage());
427 }
428 }
429
430 /*
431 * QuickEditUserBadges
432 * Redirects to the edit user badges page for the user with $_POST["u"] username
433 */
434 public static function QuickEditUserBadges() {
435 try {
436 // Check if everything is set
437 if (empty($_POST['u'])) {
438 throw new Exception('Nice troll.');
439 }
440 // Get user id
441 $id = current($GLOBALS['db']->fetch('SELECT id FROM users WHERE username = ?', $_POST['u']));
442 // Check if that user exists
443 if (!$id) {
444 throw new Exception("That user doesn't exists");
445 }
446 // Done, redirect to edit page
447 redirect('index.php?p=110&id='.$id);
448 }
449 catch(Exception $e) {
450 // Redirect to Exception page
451 redirect('index.php?p=108&e='.$e->getMessage());
452 }
453 }
454
455 /*
456 * ChangeIdentity
457 * Change identity function (ADMIN CP)
458 */
459 public static function ChangeIdentity() {
460 try {
461 // Check if everything is set
462 if (!isset($_POST['id']) || !isset($_POST['oldu']) || !isset($_POST['newu']) || !isset($_POST['ks']) || empty($_POST['id']) || empty($_POST['oldu']) || empty($_POST['newu'])) {
463 throw new Exception('Nice troll.');
464 }
465 // Check if we can edit this user
466 if (getUserRank($_POST['oldu']) >= getUserRank($_SESSION['username']) && $_POST['oldu'] != $_SESSION['username']) {
467 throw new Exception("You dont't have enough permissions to edit this user");
468 }
469 // Change stuff
470 $GLOBALS['db']->execute('UPDATE users SET username = ? WHERE id = ?', [$_POST['newu'], $_POST['id']]);
471 $GLOBALS['db']->execute('UPDATE users_stats SET username = ? WHERE id = ?', [$_POST['newu'], $_POST['id']]);
472 // Change username in scores if needed
473 if ($_POST['ks'] == 1) {
474 $GLOBALS['db']->execute('UPDATE scores SET username = ? WHERE username = ?', [$_POST['newu'], $_POST['oldu']]);
475 }
476 // Done, redirect to success page
477 redirect('index.php?p=102&s=User identity changed!');
478 }
479 catch(Exception $e) {
480 // Redirect to Exception page
481 redirect('index.php?p=102&e='.$e->getMessage());
482 }
483 }
484
485 /*
486 * SaveDocFile
487 * Save doc file function (ADMIN CP)
488 */
489 public static function SaveDocFile() {
490 try {
491 // Check if everything is set
492 if (!isset($_POST['id']) || !isset($_POST['t']) || !isset($_POST['c']) || !isset($_POST['p']) || empty($_POST['t']) || empty($_POST['c'])) {
493 throw new Exception('Nice troll.');
494 }
495 // Check if we are creating or editing a doc page
496 if ($_POST['id'] == 0) {
497 $GLOBALS['db']->execute('INSERT INTO docs (id, doc_name, doc_contents, public) VALUES (NULL, ?, ?, ?)', [$_POST['t'], $_POST['c'], $_POST['p']]);
498 } else {
499 $GLOBALS['db']->execute('UPDATE docs SET doc_name = ?, doc_contents = ?, public = ? WHERE id = ?', [$_POST['t'], $_POST['c'], $_POST['p'], $_POST['id']]);
500 }
501 // Done, redirect to success page
502 redirect('index.php?p=106&s=Documentation page edited!');
503 }
504 catch(Exception $e) {
505 // Redirect to Exception page
506 redirect('index.php?p=106&e='.$e->getMessage());
507 }
508 }
509
510 /*
511 * SaveBadge
512 * Save badge function (ADMIN CP)
513 */
514 public static function SaveBadge() {
515 try {
516 // Check if everything is set
517 if (!isset($_POST['id']) || !isset($_POST['n']) || !isset($_POST['i']) || empty($_POST['n']) || empty($_POST['i'])) {
518 throw new Exception('Nice troll.');
519 }
520 // Check if we are creating or editing a doc page
521 if ($_POST['id'] == 0) {
522 $GLOBALS['db']->execute('INSERT INTO badges (id, name, icon) VALUES (NULL, ?, ?)', [$_POST['n'], $_POST['i']]);
523 } else {
524 $GLOBALS['db']->execute('UPDATE badges SET name = ?, icon = ? WHERE id = ?', [$_POST['n'], $_POST['i'], $_POST['id']]);
525 }
526 // Done, redirect to success page
527 redirect('index.php?p=108&s=Badge edited!');
528 }
529 catch(Exception $e) {
530 // Redirect to Exception page
531 redirect('index.php?p=108&e='.$e->getMessage());
532 }
533 }
534
535 /*
536 * SaveUserBadges
537 * Save user badges function (ADMIN CP)
538 */
539 public static function SaveUserBadges() {
540 try {
541 // Check if everything is set
542 if (!isset($_POST['u']) || !isset($_POST['b01']) || !isset($_POST['b02']) || !isset($_POST['b03']) || !isset($_POST['b04']) || !isset($_POST['b05']) || !isset($_POST['b06']) || empty($_POST['u'])) {
543 throw new Exception('Nice troll.');
544 }
545 // Make sure that this user exists
546 if (!$GLOBALS['db']->fetch('SELECT id FROM users WHERE username = ?', $_POST['u'])) {
547 throw new Exception("That user doesn't exists.");
548 }
549 // Get the string with all the badges
550 $badgesString = $_POST['b01'].','.$_POST['b02'].','.$_POST['b03'].','.$_POST['b04'].','.$_POST['b05'].','.$_POST['b06'];
551 // Save the new badges string
552 $GLOBALS['db']->execute('UPDATE users_stats SET badges_shown = ? WHERE username = ?', [$badgesString, $_POST['u']]);
553 // Done, redirect to success page
554 redirect('index.php?p=108&s=Badge edited!');
555 }
556 catch(Exception $e) {
557 // Redirect to Exception page
558 redirect('index.php?p=108&e='.$e->getMessage());
559 }
560 }
561
562 /*
563 * RemoveDocFile
564 * Delete doc file function (ADMIN CP)
565 */
566 public static function RemoveDocFile() {
567 try {
568 // Check if everything is set
569 if (!isset($_GET['id']) || empty($_GET['id'])) {
570 throw new Exception('Nice troll.');
571 }
572 // Check if this doc page exists
573 if (!$GLOBALS['db']->fetch('SELECT * FROM docs WHERE id = ?', $_GET['id'])) {
574 throw new Exception("That documentation page doesn't exists");
575 }
576 // Delete doc page
577 $GLOBALS['db']->execute('DELETE FROM docs WHERE id = ?', $_GET['id']);
578 // Done, redirect to success page
579 redirect('index.php?p=106&s=Documentation page deleted!');
580 }
581 catch(Exception $e) {
582 // Redirect to Exception page
583 redirect('index.php?p=106&e='.$e->getMessage());
584 }
585 }
586
587 /*
588 * RemoveBadge
589 * Remove badge function (ADMIN CP)
590 */
591 public static function RemoveBadge() {
592 try {
593 // Make sure that this is not the "None badge"
594 if (empty($_GET['id'])) {
595 throw new Exception("You can't delete this badge.");
596 }
597 // Make sure that this badge exists
598 $exists = $GLOBALS['db']->fetch('SELECT * FROM badges WHERE id = ?', $_GET['id']);
599 if (!$exists) {
600 throw new Exception("This badge doesn't exists");
601 }
602 // Delete badge
603 $GLOBALS['db']->execute('DELETE FROM badges WHERE id = ?', $_GET['id']);
604 // Done, redirect to success page
605 redirect('index.php?p=108&s=Badge deleted!');
606 }
607 catch(Exception $e) {
608 // Redirect to Exception page
609 redirect('index.php?p=108&e='.$e->getMessage());
610 }
611 }
612
613 /*
614 * SilenceUser
615 * Silence someone (ADMIN CP)
616 */
617 public static function SilenceUser() {
618 try {
619 // Check if everything is set
620 if (!isset($_POST['u']) || !isset($_POST['c']) || !isset($_POST['un']) || !isset($_POST['r']) || empty($_POST['u']) || empty($_POST['c']) || empty($_POST['un']) || empty($_POST['r'])) {
621 throw new Exception('Invalid request');
622 }
623 // Get user id
624 $id = current($GLOBALS['db']->fetch('SELECT id FROM users WHERE username = ?', $_POST['u']));
625 // Check if that user exists
626 if (!$id) {
627 throw new Exception("That user doesn't exists");
628 }
629 // Calculate silence period length
630 $sl = $_POST['c'] * $_POST['un'];
631 // Make sure silence time is less than 7 days
632 if ($sl > 604800) {
633 throw new Exception('Invalid silence length. Maximum silence length is 7 days.');
634 }
635 // Silence and reconnect that user
636 silenceUser($id, time() + $sl, $_POST['r']);
637 kickUser($id);
638 // Done, redirect to success page
639 redirect('index.php?p=102&s=User silenced!');
640 }
641 catch(Exception $e) {
642 // Redirect to Exception page
643 redirect('index.php?p=102&e='.$e->getMessage());
644 }
645 }
646
647 /*
648 * KickUser
649 * Kick someone from bancho (ADMIN CP)
650 */
651 public static function KickUser() {
652 try {
653 // Check if everything is set
654 if (!isset($_POST['u']) || empty($_POST['u'])) {
655 throw new Exception('Invalid request');
656 }
657 // Get user id
658 $id = current($GLOBALS['db']->fetch('SELECT id FROM users WHERE username = ?', $_POST['u']));
659 // Check if that user exists
660 if (!$id) {
661 throw new Exception("That user doesn't exists");
662 }
663 // Kick that user
664 //kickUser($id);
665 // Done, redirect to success page
666 redirect('index.php?p=102&s=Kick Feature not available yet!');
667 }
668 catch(Exception $e) {
669 // Redirect to Exception page
670 redirect('index.php?p=102&e='.$e->getMessage());
671 }
672 }
673
674 /*
675 * ResetAvatar
676 * Reset soneone's avatar (ADMIN CP)
677 */
678 public static function ResetAvatar() {
679 try {
680 // Check if everything is set
681 if (!isset($_GET['id']) || empty($_GET['id'])) {
682 throw new Exception('Invalid request');
683 }
684 // Get user id
685 $avatar = dirname(dirname(dirname(__FILE__))).'/a.ppy.sh/avatars/'.$_GET['id'].'.png';
686 if (!file_exists($avatar)) {
687 throw new Exception("That user doesn't have an avatar");
688 }
689 // Delete user avatar
690 unlink($avatar);
691 // Done, redirect to success page
692 redirect('index.php?p=102&s=Avatar reset!');
693 }
694 catch(Exception $e) {
695 // Redirect to Exception page
696 redirect('index.php?p=102&e='.$e->getMessage());
697 }
698 }
699
700 /*
701 * Logout
702 * Logout and return to home
703 */
704 public static function Logout() {
705 // Logging out without being logged in doesn't make much sense
706 if (checkLoggedIn()) {
707 startSessionIfNotStarted();
708 if (isset($_COOKIE['s']) && isset($_COOKIE['t'])) {
709 $rch = new RememberCookieHandler();
710 // Desu-troy permanent session.
711 $rch->Destroy($_COOKIE['s']);
712 $rch->UnsetCookies();
713 }
714 $_SESSION = [];
715 session_destroy();
716 } else {
717 // Uhm, some kind of error/h4xx0r. Let's return to login page just because yes.
718 redirect('index.php?p=2');
719 }
720 }
721
722 /*
723 * ForgetEveryCookie
724 * Allows the user to delete every field in the remember database table with their username, so that it is logged out of every computer they were logged in.
725 */
726 public static function ForgetEveryCookie() {
727 startSessionIfNotStarted();
728 $rch = new RememberCookieHandler();
729 $rch->DestroyAll($_SESSION['username']);
730 redirect('index.php?p=1&s=forgetDone');
731 }
732
733 /*
734 * saveUserSettings
735 * Save user settings functions
736 */
737 public static function saveUserSettings() {
738 global $PlayStyleEnum;
739 try {
740 // Check if we are logged in
741 sessionCheck();
742 // Check if everything is set
743 if (!isset($_POST['f']) || !isset($_POST['c']) || !isset($_POST['aka']) || !isset($_POST['st'])) {
744 throw new Exception(0);
745 }
746 // Check if username color is not empty and if so, set to black (default)
747 if (empty($_POST['c'])) {
748 $c = 'black';
749 } else {
750 $c = $_POST['c'];
751 }
752 // Playmode stuff
753 $pm = 0;
754 foreach ($_POST as $key => $value) {
755 $i = str_replace('_', ' ', substr($key, 3));
756 if ($value == 1 && substr($key, 0, 3) == 'ps_' && isset($PlayStyleEnum[$i])) {
757 $pm += $PlayStyleEnum[$i];
758 }
759 }
760 // Update mode
761 if ($_POST['mode'] <= 3 && $_POST['mode'] >= 0) {
762 $GLOBALS['db']->execute('UPDATE users_stats SET favourite_mode = ? WHERE username = ?', [$_POST['mode'], $_SESSION['username']]);
763 }
764 // Save data in db
765 $GLOBALS['db']->execute('UPDATE users_stats SET user_color = ?, show_country = ?, username_aka = ?, safe_title = ?, play_style = ? WHERE username = ?', [$c, $_POST['f'], $_POST['aka'], $_POST['st'], $pm, $_SESSION['username']]);
766 // Update safe title cookie
767 updateSafeTitle();
768 // Done, redirect to success page
769 redirect('index.php?p=6&s=ok');
770 }
771 catch(Exception $e) {
772 // Redirect to Exception page
773 redirect('index.php?p=6&e='.$e->getMessage());
774 }
775 }
776
777 /*
778 * SaveUserpage
779 * Save userpage functions
780 */
781 public static function SaveUserpage() {
782 try {
783 // Check if we are logged in
784 sessionCheck();
785 // Check if everything is set
786 if (!isset($_POST['c'])) {
787 throw new Exception(0);
788 }
789 // Check userpage length
790 if (strlen($_POST['c']) > 1500) {
791 throw new Exception(1);
792 }
793 // Save data in db
794 $GLOBALS['db']->execute('UPDATE users_stats SET userpage_content = ? WHERE username = ?', [$_POST['c'], $_SESSION['username']]);
795 // Done, redirect to success page
796 redirect('index.php?p=8&s=ok');
797 }
798 catch(Exception $e) {
799 // Redirect to Exception page
800 redirect('index.php?p=8&e='.$e->getMessage().$r);
801 }
802 }
803
804 /*
805 * ChangeAvatar
806 * Chhange avatar functions
807 */
808 public static function ChangeAvatar() {
809 try {
810 // Check if we are logged in
811 sessionCheck();
812 // Check if everything is set
813 if (!isset($_FILES['file'])) {
814 throw new Exception(0);
815 }
816 // Check if image file is a actual image or fake image
817 if (!getimagesize($_FILES['file']['tmp_name'])) {
818 throw new Exception(1);
819 }
820 // Allow certain file formats
821 $allowedFormats = ['jpg', 'jpeg', 'png'];
822 if (!in_array(pathinfo($_FILES['file']['name']) ['extension'], $allowedFormats)) {
823 throw new Exception(2);
824 }
825 // Check file size
826 if ($_FILES['file']['size'] > 1000000) {
827 throw new Exception(3);
828 }
829 // Resize (doesn't work to any reason)
830 if (!smart_resize_image($_FILES['file']['tmp_name'], null, 100, 100, false, dirname(dirname(dirname(__FILE__))).'/a.ppy.sh/avatars/'.getUserID($_SESSION['username']).'.png', false, false, 100)) {
831 throw new Exception(4);
832 }
833 // THIS ONE WONT RESIZE THINK ABOUT DISK SPACE AND INGAME BUGS (too big pictures or sth)
834 //if (!move_uploaded_file($_FILES["file"]["tmp_name"], dirname(dirname(dirname(__FILE__)))."/a.ppy.sh/avatars/".getUserID($_SESSION["username"]).".png")) {
835 // throw new Exception(4);
836 //}
837 // Done, redirect to success page
838 redirect('index.php?p=5&s=ok');
839 }
840 catch(Exception $e) {
841 // Redirect to Exception page
842 redirect('index.php?p=5&e='.$e->getMessage());
843 }
844 }
845
846 /*
847 * SendReport
848 * Send report function
849 */
850 public static function SendReport() {
851 try {
852 // Check if we are logged in
853 sessionCheck();
854 // Check if everything is set
855 if (!isset($_POST['t']) || !isset($_POST['n']) || !isset($_POST['c']) || empty($_POST['n']) || empty($_POST['c'])) {
856 throw new Exception(0);
857 }
858 // Add report
859 $GLOBALS['db']->execute('INSERT INTO reports (id, name, from_username, content, type, open_time, update_time, status) VALUES (NULL, ?, ?, ?, ?, ?, ?, 1)', [$_POST['n'], $_SESSION['username'], $_POST['c'], $_POST['t'], time(), time()]);
860 // Webhook stuff
861 global $WebHookReport;
862 global $KeyAkerino;
863 $type = $_POST['t'];
864 switch ($type) {
865 case 0:
866 $type = 'bug';
867 break;
868 case 1:
869 $type = 'feature';
870 break;
871 }
872 post_content_http($WebHookReport, ['key' => $KeyAkerino, 'title' => $_POST['n'], 'content' => $_POST['c'], 'id' => $GLOBALS['db']->lastInsertId(), 'type' => $type, 'username' => $_SESSION['username']]);
873 // Done, redirect to success page
874 redirect('index.php?p=22&s=ok');
875 }
876 catch(Exception $e) {
877 // Redirect to Exception page
878 redirect('index.php?p=22&e='.$e->getMessage());
879 }
880 }
881
882 /*
883 * OpenCloseReport
884 * Open/Close a report (ADMIN CP)
885 */
886 public static function OpenCloseReport() {
887 try {
888 // Check if everything is set
889 if (!isset($_GET['id']) || empty($_GET['id'])) {
890 throw new Exception('Invalid request');
891 }
892 // Get current report status from db
893 $reportStatus = $GLOBALS['db']->fetch('SELECT status FROM reports WHERE id = ?', [$_GET['id']]);
894 // Make sure the report exists
895 if (!$reportStatus) {
896 throw new Exception("That report doesn't exist");
897 }
898 // Get report status
899 $reportStatus = current($reportStatus);
900 // Get new report status
901 $newReportStatus = $reportStatus == 1 ? 0 : 1;
902 // Edit report status
903 $GLOBALS['db']->execute('UPDATE reports SET status = ?, update_time = ? WHERE id = ?', [$newReportStatus, time(), $_GET['id']]);
904 // Done, redirect to success page
905 redirect('index.php?p=113&s=Report status changed!');
906 }
907 catch(Exception $e) {
908 // Redirect to Exception page
909 redirect('index.php?p=113&e='.$e->getMessage());
910 }
911 }
912
913 /*
914 * SaveEditReport
915 * Saves an edited report (ADMIN CP)
916 */
917 public static function SaveEditReport() {
918 try {
919 // Check if everything is set
920 if (!isset($_POST['id']) || !isset($_POST['s']) || !isset($_POST['r']) || empty($_POST['id'])) {
921 throw new Exception('Invalid request');
922 }
923 // Get current report status from db
924 $reportData = $GLOBALS['db']->fetch('SELECT * FROM reports WHERE id = ?', [$_POST['id']]);
925 // Make sure the report exists
926 if (!$reportData) {
927 throw new Exception("That report doesn't exist");
928 }
929 // Edit report status
930 $GLOBALS['db']->execute('UPDATE reports SET status = ?, response = ?, update_time = ? WHERE id = ?', [$_POST['s'], $_POST['r'], time(), $_POST['id']]);
931 // Done, redirect to success page
932 redirect('index.php?p=113&s=Report updated!');
933 }
934 catch(Exception $e) {
935 // Redirect to Exception page
936 redirect('index.php?p=113&e='.$e->getMessage());
937 }
938 }
939
940 /*
941 * AddRemoveFriend
942 * Add remove friends
943 */
944 public static function AddRemoveFriend() {
945 try {
946 // Check if we are logged in
947 sessionCheck();
948 // Check if everything is set
949 if (!isset($_GET['u']) || empty($_GET['u'])) {
950 throw new Exception(0);
951 }
952 // Get our user id
953 $uid = getUserID($_SESSION['username']);
954 // Add/remove friend
955 if (getFriendship($uid, $_GET['u'], true) == 0) {
956 addFriend($uid, $_GET['u'], true);
957 } else {
958 removeFriend($uid, $_GET['u'], true);
959 }
960 // Done, redirect
961 redirect('index.php?u='.$_GET['u']);
962 }
963 catch(Exception $e) {
964 redirect('index.php?p=99&e='.$e->getMessage());
965 }
966 }
967}