This repository has been archived by the owner on Aug 14, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.php
163 lines (143 loc) · 5.13 KB
/
index.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
<?php
/* bzflag-listkeymgr
* Copyright (c) 1993-2023 Tim Riker
*
* This package is free software; you can redistribute it and/or
* modify it under the terms of the LGPL 2.1 license found in the file
* named COPYING.txt that should have accompanied this file.
*
* THIS PACKAGE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
*/
require(__DIR__.'/vendor/autoload.php');
require("db.php");
require("checkToken.php");
$config = require("config.php");
(new class($config)
{
private $db;
private $twig;
private $config;
public function __construct($config)
{
$this->config = $config;
session_start();
$loader = new \Twig\Loader\FilesystemLoader(__DIR__.'/views/');
$this->twig = new \Twig\Environment($loader);
require('/etc/bzflag/serversettings.php');
$this->db = new DB($dbhost, $dbname, $dbuname, $dbpass);
}
private function error($message)
{
echo $this->twig->render('error.twig', [
'error' => $message
]);
}
private function redirect($url)
{
header("Location: $url");
}
public function run()
{
// Allow the login action to proceed without any other requirements
if (isset($_GET['action']) && $_GET['action'] == 'login') {
$this->login();
}
// If no user is logged in, redirect to weblogin
else if (!isset($_SESSION['bzid']) || $_SESSION['bzid'] == -1 ) {
$this->redirect('https://' . $this->config['hostname'] . '/weblogin.php?url=' . urlencode($this->config['protocol']."://" . $this->config['hostname'] . $this->config['baseURI'] . "?action=login&token=%TOKEN%&user=%USERNAME%"));
}
else {
if (isset($_POST['action'])) {
if ($_POST['action'] == 'createkey')
$this->createKey();
else if ($_POST['action'] == 'deletekey')
$this->deleteKey();
else
$this->error("Invalid POST action");
} else if (isset($_GET['action'])) {
if ($_GET['action'] == 'logout')
$this->logout();
else
$this->error("Invalid GET action");
} else {
$this->listKeys();
}
}
}
private function login()
{
// A login can only be valid when both a token and username are provided
if (!isset($_GET['token']) || !isset($_GET['user']) ) {
$this->error("Invalid Entry");
return;
}
// Check if the token is valid
$checkResults = validate_token($_GET['token'], $_GET['user'], array(), $this->config['checkip']);
if (!isset($checkResults['bzid'])) {
$this->error("Invalid Login");
return;
}
$_SESSION['bzid'] = $checkResults['bzid'];
$this->redirect($this->config['baseURI']);
}
private function logout()
{
session_destroy();
$this->redirect('https://www.bzflag.org/');
}
private function createKey()
{
if (strlen($_POST['hostname']) == 0) {
$_SESSION['flash'] = 'A hostname must be provided when creating a key.';
$this->redirect($this->config['baseURI']);
return;
}
if ($this->db->getHostByHost($_REQUEST['hostname']) !== false) {
$_SESSION['flash'] = 'Error: Hostname ' . $_REQUEST['hostname'] . ' is already in use';
$this->redirect($this->config['baseURI']);
return;
}
// First, let's create a new unique key
$i = 0;
do {
$key = bin2hex(random_bytes(20));
$i++;
// If we've tried ~20 times to create a key, give up
if ($i > 20) {
$_SESSION['flash'] = 'There was an error generating a key.';
$this->redirect($this->config['baseURI']);
return;
}
} while ($this->db->getKeyByKey($key) !== false);
// Create the new DB record
$this->db->createKey($key, $_POST['hostname'], $_SESSION['bzid']);
$_SESSION['flash'] = 'The new key has been created.';
$this->redirect($this->config['baseURI']);
}
private function deleteKey()
{
if (strlen($_POST['id']) == 0) {
$this->redirect($this->config['baseURI']);
return;
}
if ($this->db->deleteKey($_POST['id'], $_SESSION['bzid'])) {
$_SESSION['flash'] = 'The key has been deleted.';
} else {
$_SESSION['flash'] = 'There was an error deleting the key.';
}
$this->redirect($this->config['baseURI']);
}
private function listKeys()
{
$keys = $this->db->getKeysByBZID($_SESSION['bzid']);
echo $this->twig->render('listkeys.twig', [
'keys' => $keys,
'flash' => $_SESSION['flash'] ?? '',
'user' => true
]);
// Only show the message once
unset($_SESSION['flash']);
}
})->run();