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