diff --git a/auth.php b/auth.php index c5f70a9..892b809 100644 --- a/auth.php +++ b/auth.php @@ -21,17 +21,22 @@ $guildIds[] = $guild['id']; } - if(!in_array('1127731341283307520', $guildIds)) { + if(!in_array('1127731341283307520', $guildIds) || !in_array('1195393418151596032', $guildIds)) { echo json_encode(array( 'error' => true, - 'error_description' => 'You are not in the CRSS guild.' + 'error_description' => 'You are not in any of CRSS\'s guilds.' )); } else { $_SESSION['user'] = $discord->getUser($res['access_token']); $mysql->createUserRecord($_SESSION['user']); - - header('Location: /'); + + + if (isset($_GET['state'])) { + header('Location: ' . $_GET['state']); + } else { + header('Location: /'); + } } } else { echo json_encode($res); diff --git a/index.php b/index.php index f5978f9..cf34a70 100644 --- a/index.php +++ b/index.php @@ -1,5 +1,5 @@ addGlobal('user', $_SESSION['user']); + if(isset($_SESSION['user'])) { + $dbUser = $mysql->getUserRecordFromId($_SESSION['user']['id']); + + $user = $_SESSION['user']; + + $user['is_admin'] = $dbUser['is_admin']; + + $twig->addGlobal('user', $user); + } $res = $curl->get('https://crss.blurryface.xyz/api/v1/players'); @@ -45,6 +52,7 @@ $twig->addGlobal('playerCount', $json); $twig->addGlobal('nations', $nations); + $twig->addGlobal('dc_uri', 'https://discord.com/api/oauth2/authorize?client_id=1144248396467683338&redirect_uri=' . urlencode($_ENV['DISCORD_REDIRECT']) . '&response_type=code&scope=identify%20guilds&state=' . urlencode($_SERVER['REQUEST_URI'])); $twig->addGlobal('reduced', isset($_GET['reduced'])); @@ -152,16 +160,45 @@ $markers = $mysql->getMarkers(); if ($user == null && $user['admin'] == 0) { - http_response_code(404); + http_response_code(401); - echo $twig->render('404.twig'); + echo '401 Unauthorized'; } else { echo $twig->render('admin/index.twig', array('users' => $users, 'markers' => $markers)); } } else { - http_response_code(404); + http_response_code(401); - echo $twig->render('404.twig'); + echo '401 Unauthorized'; + } + }); + + $router->get('/admin/__data/page/([a-z]+)', function($page) { + global $twig, $mysql; + + if (isset($_SESSION['user'])) { + $user = $mysql->getUserRecordFromId($_SESSION['user']['id']); + + $users = $mysql->getUsers(); + $markers = $mysql->getMarkers(); + + if ($user == null && $user['admin'] == 0) { + http_response_code(401); + + echo '401 Unauthorized'; + } else { + try { + echo $twig->render('admin/pages/' . urlencode($page) . '.twig', array('users' => $users, 'markers' => $markers)); + } catch (Exception $e) { + http_response_code(404); + + echo $twig->render('admin/pages/404.twig'); + } + } + } else { + http_response_code(401); + + echo '401 Unauthorized'; } }); diff --git a/js/admin/nav.js b/js/admin/nav.js new file mode 100644 index 0000000..9ddebc4 --- /dev/null +++ b/js/admin/nav.js @@ -0,0 +1,50 @@ +const pageContainer = document.querySelector('.pageContainer'); + +window.history.pushState({}, '', '#/'); + +window.addEventListener('hashchange', () => { + let uri = window.location.href.split('#')[1]; + + if (!window.location.href.includes('#')) + uri = '/admin#/' + + const allActiveLinks = document.querySelectorAll('.nav a.active'); + const allLinksWithThisUrl = document.querySelectorAll(`.nav a[href="#${uri}"]`); + + allActiveLinks.forEach(activeLink => { + activeLink.classList.remove('active'); + activeLink.classList.add('link-body-emphasis'); + }); + + allLinksWithThisUrl.forEach(link => { + link.classList.add('active'); + link.classList.remove('link-body-emphasis'); + }); + + changePage(window.location.href.split('#')[1].replace('/', '')); +}); + +const changePage = (url) => { + if (!url) + url = 'dashboard'; + + pageContainer.innerHTML = ``; + + pageContainer.classList.add('d-flex'); + pageContainer.classList.add('align-items-center'); + pageContainer.classList.add('justify-content-center'); + + lucide.createIcons(); + + fetch(`/admin/__data/page/${url}`) + .then(res => res.text()) + .then(html => { + pageContainer.innerHTML = html; + + pageContainer.classList.remove('d-flex'); + pageContainer.classList.remove('align-items-center'); + pageContainer.classList.remove('justify-content-center'); + + lucide.createIcons(); + }); +}; \ No newline at end of file diff --git a/template/admin/includes/foot.twig b/template/admin/includes/foot.twig new file mode 100644 index 0000000..d1e0e0b --- /dev/null +++ b/template/admin/includes/foot.twig @@ -0,0 +1,12 @@ + + + + + + + + \ No newline at end of file diff --git a/template/admin/includes/head.twig b/template/admin/includes/head.twig new file mode 100644 index 0000000..1887f87 --- /dev/null +++ b/template/admin/includes/head.twig @@ -0,0 +1,35 @@ + + + + + + + + {{ pageTitle }} - Admin • Clyde's Real Survival SMP + + + + + + \ No newline at end of file diff --git a/template/admin/includes/sidebar.twig b/template/admin/includes/sidebar.twig new file mode 100644 index 0000000..51b1aa8 --- /dev/null +++ b/template/admin/includes/sidebar.twig @@ -0,0 +1,52 @@ +
+ + + Admin Panel + +
+ +
+ +
\ No newline at end of file diff --git a/template/admin/index.twig b/template/admin/index.twig index 450254f..45458e0 100644 --- a/template/admin/index.twig +++ b/template/admin/index.twig @@ -1,150 +1,11 @@ - - - - +{% include 'admin/includes/head.twig' with {'pageTitle': 'Dashboard'} %} - +
+ {% include 'admin/includes/sidebar.twig' %} - Admin Panel +
- - - -
-

Users

+
+
- - - - - - - - - - - - - {% for user in users %} - - - - - - - - - {% endfor %} - -
idusernamedisplay_nameis_admindateactions
{{ user.id }}{{ user.username }}{{ user.display_name }}{{ user.is_admin == 1 ? "true" : "false" }}{{ user.date }} - {% if user.is_admin == 1 %} - - {% else %} - - {% endif %} -
-
- - - -
-

Markers

- - - - - - - - - - - - - {% for marker in markers %} - - - - - - - - {% endfor %} - -
idnamecategorydataactions
{{ marker.id }}{{ marker.name }}{{ marker.category }}{{ marker.data }} - -
-
- - - - - - - - \ No newline at end of file +{% include 'admin/includes/foot.twig' %} \ No newline at end of file diff --git a/template/admin/pages/404.twig b/template/admin/pages/404.twig new file mode 100644 index 0000000..634dcd9 --- /dev/null +++ b/template/admin/pages/404.twig @@ -0,0 +1,3 @@ +
+

Not Found :(

+
\ No newline at end of file diff --git a/template/admin/pages/dashboard.twig b/template/admin/pages/dashboard.twig new file mode 100644 index 0000000..6083eeb --- /dev/null +++ b/template/admin/pages/dashboard.twig @@ -0,0 +1,3 @@ +
+

Welcome {{ user.global_name }} to the admin panel!

+
\ No newline at end of file diff --git a/template/admin/pages/markers.twig b/template/admin/pages/markers.twig new file mode 100644 index 0000000..18ce19d --- /dev/null +++ b/template/admin/pages/markers.twig @@ -0,0 +1,24 @@ + + + + + + + + + + + + {% for marker in markers %} + + + + + + + + {% endfor %} + +
idnamecategorydataactions
{{ marker.id }}{{ marker.name }}{{ marker.category }}{{ marker.data }} + +
\ No newline at end of file diff --git a/template/admin/pages/pages.twig b/template/admin/pages/pages.twig new file mode 100644 index 0000000..5c9227a --- /dev/null +++ b/template/admin/pages/pages.twig @@ -0,0 +1 @@ +pages \ No newline at end of file diff --git a/template/admin/pages/users.twig b/template/admin/pages/users.twig new file mode 100644 index 0000000..810f5f6 --- /dev/null +++ b/template/admin/pages/users.twig @@ -0,0 +1,30 @@ + + + + + + + + + + + + + {% for user in users %} + + + + + + + + + {% endfor %} + +
idusernamedisplay_nameis_admindateactions
{{ user.id }}{{ user.username }}{{ user.display_name }}{{ user.is_admin == 1 ? "true" : "false" }}{{ user.date }} + {% if user.is_admin == 1 %} + + {% else %} + + {% endif %} +
\ No newline at end of file diff --git a/template/includes/nav.twig b/template/includes/nav.twig index ef894d4..75a8760 100644 --- a/template/includes/nav.twig +++ b/template/includes/nav.twig @@ -16,14 +16,19 @@ Map & Rails + {% if user.is_admin == 1 %} + + Admin Panel + + {% endif %}