-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi.php
More file actions
239 lines (180 loc) · 8.66 KB
/
api.php
File metadata and controls
239 lines (180 loc) · 8.66 KB
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
<?php
// Require the needed classes.
require_once "class/AuthToken.php";
require_once "class/Database.php";
require_once "class/Route.php";
require_once "class/Server.php";
require_once "class/User.php";
require_once "inc/config.php";
require_once "inc/helper.php";
// Use this namespace.
use Steampixel\Route;
// Login route.
Route::add("/login", function () {
// Globalize some important variables. Failure to do so will output errors like the 500 error.
global $AUTH;
global $USER;
global $BCRYPT_OPTIONS;
// Assign request post and response.
$request = $_POST;
$response = "";
// Check if the url has the "action" parameter.
if (isParameterSet($request, "action")) {
// Get the action's action type.
$action = getParameter($request, "action");
switch ($action) {
case "login":
// If login, find username & password.
$username = isParameterSet($request, "username") ? getParameter($request, "username") : "";
$password = isParameterSet($request, "password") ? getParameter($request, "password") : "";
$rememberme = isParameterSet($request, "rememberme") ? getParameter($request, "rememberme") : "";
$rememberme = $rememberme == "on" ? true : false;
// If username and password fields aren't empty.
if (!empty($username) && !empty($password)) {
// Username and password are not empty, continue...
// Fetch the account from database.
$USER->setUserInfo($username, $password, null);
$AUTH->username = $username;
// If user exists login.
if ($USER->userExists()) {
// Set the login session.
$USER->setuserTokenSession();
$USER->updateUserLoginSession();
// If user should be remembered.
if ($rememberme) {
// Create or update auth token to skip login
// in future login attempts.
if ($AUTH->authTokenUserIdExists()) {
// Generate new token for this user.
$AUTH->updateauthToken();
} else {
$AUTH->createauthToken();
}
// Set the auth token in a session.
$AUTH->setauthTokenSession();
}
// Then at last add the login complete response!
$response = json_encode(array("message" => "You have been logged in."));
gotoPage("game");
} // If not...
else $response = json_encode(array("error" => "No matching account was found."));
} // If they are...
else $response = json_encode(array("error" => "No username and password supplied."));
break;
}
} else $response = json_encode(array("error" => "Wrong login parameter."));
// Set the header depending on whether the response is empty or not.
if (!empty($response)) header("Content-Type: application/json");
else header("Content-Type: text/html");
// Show the response.
echo $response;
}, ["get", "post"]);
// Registration route.
Route::add("/register", function () {
// Globalize some important variables. Failure to do so will output errors like the 500 error.
global $AUTH;
global $USER;
global $BCRYPT_OPTIONS;
// Assign request post and response.
$request = $_POST;
$response = "";
// Check if the url has the "action" parameter.
if (isParameterSet($request, "action")) {
// Get the action's action type.
$action = getParameter($request, "action");
// If login, find username & password.
$username = isParameterSet($request, "username") ? getParameter($request, "username") : "";
$password = isParameterSet($request, "password") ? getParameter($request, "password") : "";
$rememberme = isParameterSet($request, "rememberme") ? getParameter($request, "rememberme") : "";
$rememberme = $rememberme == "on" ? true : false;
switch ($action) {
case "register":
// If register, find username & password.
$username = isParameterSet($request, "username") ? getParameter($request, "username") : "";
$password = isParameterSet($request, "password") ? getParameter($request, "password") : "";
if (!empty($username) && !empty($password)) {
// Username and password are not empty, continue...
// Fetch the account from database and assign it as the found.
$USER->setUserInfo($username, $password, null);
$AUTH->username = $username;
// If user doesn't exists.
if (!$USER->userExists()) {
// Create the user.
$USER->createUser();
// Set the login session. Makes us be seen as logged in.
$USER->setuserTokenSession();
// If user should be remembered.
if ($rememberme) {
// Create or update auth token to skip login
// in future login attempts.
if ($AUTH->authTokenUserIdExists()) {
// Generate new token for this user.
$AUTH->updateauthToken();
} else {
$AUTH->createauthToken();
}
// Set the auth token in a session.
$AUTH->setauthTokenSession();
}
// Then add the login success response.
$response = json_encode(array("message" => "Account for: $username is now created."));
} // If it does, tell the user that the username is already taken.
else $response = json_encode(array("error" => "Username: $username already exists. Try another username."));
} // If the fields are empty, tell the user.
else
$response = json_encode(array("error" => "No username and password supplied."));
break;
}
} else $response = json_encode(array("error" => "Wrong registration parameter."));
// Set the header depending on whether the response is empty or not.
if (!empty($response)) header("Content-Type: application/json");
else header("Content-Type: text/html");
// Show the response.
echo $response;
}, ["get", "post"]);
// Play game route.
Route::add("/game", function () {
global $LOGIN_COOKIE_NAME;
$response = "";
if (!isCookieSet($LOGIN_COOKIE_NAME))
$response = json_encode(array("error" => "You are not logged in."));
//else require_once "game.php";
else gotoPage("../game.php");
// Set the header depending on whether the response is empty or not.
if (!empty($response)) header("Content-Type: application/json");
else header("Content-Type: text/html");
// Output the response.
echo $response;
});
// Log out route.
Route::add("/logout", function () {
global $LOGIN_COOKIE_NAME;
$response = "";
if (isCookieSet($LOGIN_COOKIE_NAME)) {
destroyCookie($LOGIN_COOKIE_NAME);
$response = json_encode(array("message" => "You have been logged out."));
} else $response = json_encode(array("error" => "You are not logged in."));
// Set the header depending on whether the response is empty or not.
if (!empty($response)) header("Content-Type: application/json");
else header("Content-Type: text/html");
// Output the response.
echo $response;
});
// Server info route.
Route::add("/serverinfo", function () {
global $SERVER_NAME;
global $LOGIN_IP;
global $GAMEFILES_LINK;
global $UNITY_FILE;
$response = "";
$server = new Server();
$server->setServerInfo($SERVER_NAME, $LOGIN_IP, $GAMEFILES_LINK, $UNITY_FILE);
$response = $server->toJson();
// Set the header depending on whether the response is empty or not.
if (!empty($response)) header("Content-Type: application/json");
else header("Content-Type: text/html");
// Output the server info as response.
echo $response;
});
// Use the path as base path for urls. This must lead to the php file.
Route::run(APIPATH);