-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdbRabbitMQServer.php
executable file
·494 lines (373 loc) · 13.2 KB
/
dbRabbitMQServer.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
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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
#!/usr/bin/php
<?php
require_once('path.inc');
require_once('get_host_info.inc');
require_once('rabbitMQLib.inc');
require_once('RabbitLogger/490Logger.php');
require_once('getDB.php');
function doRegister($email,$username,$password)
{
$dbLogin = $GLOBALS['dbLogin']; //gets the registation database defined at the bottom of the file
$password_hash = password_hash($password, PASSWORD_BCRYPT); //hashes the user password
//array where all parameters to input into the SQL request go.
//Starting each with a : is not neccessary, but helps differentiate the SQL variables from the PHP ones
$params = array();
$params[':email'] = $email;
$params[':username'] = $username;
$params[':password'] = $password_hash;
//builds the SQL statement, the syntax should be exactly the same as what you are used to
//Note how the values in the $params array are in the SQL statement
$stmt = $dbLogin->prepare("INSERT INTO users(user_email, user_name, user_password_hash) VALUES(:email, :username, :password)");
//executes the prepared statement, make sure to input the $params array, or else the call won't work
$r = $stmt->execute($params);
//$e gets populated with any SQL error info from the database
$e = $stmt->errorInfo();
if($e[0] == "00000") //error code for no errors
{
$message = "Registration successful";
$success = true;
}
else if($e[0] == "23000") //error code for duplicate entry
{
$message = "User already exists, try another username";
$success = false;
}
else //if we get another error we haven't accounted for
{
$message = "Something isn't working, try again later";
$success = false;
}
//returns true if the registration worked, false otherwise, and a message saying what happened
return array("success" => $success, "message" => $message);
}
function doLogin($username,$password)
{
$dbLogin = $GLOBALS['dbLogin'];
$logger = $GLOBALS['logger'];
$login = false;
$sessionToken;
$params = array();
$params[':username'] = $username;
//checks to see if a user exists with the provided username
$stmt = $dbLogin->prepare("SELECT user_id, user_email, user_name, user_password_hash from users
WHERE user_name = :username LIMIT 1");
$r = $stmt->execute($params);
$e = $stmt->errorInfo();
if($e[0] != "00000")
{
$logger->log_rabbit('Error', 'Database call returned error');
}
//gets the result of the SQL request, and populates them into $result as an associative array
$result = $stmt->fetch(PDO::FETCH_ASSOC);
//checks to see if a password hash was returned
if($result && isset($result["user_password_hash"]))
{
$password_hash_from_db = $result["user_password_hash"];
//checks to see if the hashed version of the password the user gave is the same as the one from the database
//TODO, make it so we hash the password BEFORE we send it via rabbit, this is a security issue
if(password_verify($password, $password_hash_from_db))
{
//should create a new random session ID for the user
$sessionToken = session_create_id();
//gets the current unix timestamp for validation later
$sessionTime = time() + 180;
$login = true;
$params = array();
$params[':session_token'] = $sessionToken;
//$params[':session_time'] = $sessionTime;
$params[':user_id'] = $result['user_id'];
//updates the session token and session time so we can use them to validate the user sessions
$stmt = $dbLogin->prepare("UPDATE users SET session_token = :session_token WHERE user_id = :user_id");
echo "\npassword verified\n";
$r = $stmt->execute($params);
$e = $stmt->errorInfo();
echo ("ERORR: " . $e[0]);
if($e[0] != "00000")
{
$logger->log_rabbit('Error', 'Session could not be set, try again later');
$login = false;
}
}
else
{
$login = false;
}
}
else
{
$login = false;
}
//if the login is successful, returns true, plus the session token and session time to be applied to the user
if($login)
{
echo "login successful";
return array("login" => $login, "sessionToken" => $sessionToken, "sessionTime" => $sessionTime, "userID" => $result['user_id']);
}
else //just returns false, showing that the user is not logged in
{
echo "login failed";
return array("login" => $login);
}
}
function doValidate($sessionID)
{
//check DB to see if the current session time is expired for the session ID
//also check to see if sessionID is valid for any user
//set expired equal to true of false if session is expired and user needs to login again
$expired;
return array("response" => $expired);
}
function getLobbies($gameMode)
{
//returns list of all lobbies in the database with the provided gamemode
//also deletes all lobbies where the lobby was created at least 5 minutes ago
//and the number of players = 0
//fill lobby list with the lobby ids of each lobby that fits the gamemode
$lobbyList = array();
return array("lobbyList" => $lobbyList);
}
function joinLobby($username, $lobbyID)
{
//adds a player to the list of players in a lobby in the DB
//fill value of didJoin to true on a successful join, false otherwise
$didJoin;
return array("didJoin" => $didJoin);
}
function leaveLobby($username, $lobbyID)
{
//deletes a player to the list of players in a lobby in the DB
//fill value of didJoin to true on a successful leave, false otherwise
$didLeave;
return array("didLeave" => $didLeave);
}
function setUserStats($user_id, $gamesPlayed, $wordsPlayed, $gamesWon)
{
$dbGame = $GLOBALS['dbGame'];
$params = array();
$params[':user_id'] = $user_id;
$params[':gamesPlayed'] = $gamesPlayed;
$params[':wordsPlayed'] = $wordsPlayed;
$params[':gamesWon'] = $gamesWon;
$selParams = array();
$selParams[':user_id'] = $user_id;
echo "\nSET STATS: " . $user_id . "\n";
echo "\nSET gamesPlayed: " . $gamesPlayed . "\n";
echo "\nSET wordsPlayed: " . $wordsPlayed . "\n";
echo "\nSET gamesWon: " . $gamesWon . "\n";
$selectStatement = $dbGame->prepare("SELECT * FROM userStats where user_id = :user_id");
$result = $selectStatement->execute($selParams);
$e = $selectStatement->errorInfo();
if ($selectStatement->rowCount() == 0)
{
$insertStatement = $dbGame->prepare("INSERT INTO userStats(user_id, gamesPlayed, wordsPlayed, gamesWon) VALUES(:user_id, :gamesPlayed, :wordsPlayed, :gamesWon)");
$result = $insertStatement->execute($params);
$e = $insertStatement->errorInfo();
}
else
{
$updateStatement = $dbGame->prepare("UPDATE userStats SET gamesPlayed = gamesPlayed+:gamesPlayed, gamesWon = gamesWon+:gamesWon, wordsPlayed = wordsPlayed+:wordsPlayed WHERE user_id = :user_id");
$result = $updateStatement->execute($params);
$e = $updateStatement->errorInfo();
}
if($e[0] == "00000")
{
$message = "User Stats Set";
$success = true;
}
else
{
$message = "No User Stats Set";
$success = false;
}
return array("success" => $success);
}
function getUserStats($user_id)
{
$dbGame = $GLOBALS['dbGame'];
$params = array();
$params[':user_id'] = $user_id;
$stmt = $dbGame->prepare("SELECT gamesPlayed, wordsPlayed, gamesWon from userStats WHERE user_id = :user_id");
$r = $stmt->execute($params);
$e = $stmt->errorInfo();
$stats = $stmt->fetch(PDO::FETCH_ASSOC);
if($e[0] == "00000")
{
$message = "User Stats Retrieved";
$success = true;
}
else
{
$message = "No User Stats Retrieved";
$success = false;
}
var_dump($stats);
return array("success" => $success, "message" => $message, "stats" => $stats);
}
function sendChat($user_id, $message)
{
$dbGame = $GLOBALS['dbGame'];
$params = array();
$params[':user_id'] = $user_id;
$params[':message'] = $message;
$stmt = $dbGame->prepare("INSERT INTO Chat(user_id, message) VALUES(:user_id, :message)");
$r = $stmt->execute($params);
$e = $stmt->errorInfo();
if($e[0] == "00000")
{
$message = "Chat Saved";
$success = true;
}
else
{
$message = "No Chat saved";
$success = false;
}
return array("success" => $success);
}
function getChat()
{
$dbGame = $GLOBALS['dbGame'];
$params = array();
//$params[':user_id'] = $user_id;
//$params[':message'] = $message;
$stmt = $dbGame->prepare("SELECT user_id, message from Chat");
//$stmt = $dbGame->prepare("SELECT user_id, message from Chat WHERE user_id = :user_id, message = :message");
$r = $stmt->execute($params);
$e = $stmt->errorInfo();
$chat = $stmt->fetchAll(PDO::FETCH_ASSOC);
if($e[0] == "00000")
{
$message = "Chat Recieved";
$success = true;
}
else
{
$message = "No Chat Received";
$success = false;
}
echo "\nchat dump\n";
var_dump($chat);
return array("success" => $success, "chat" => $chat);
}
function setFriends($user_id, $friend_id)
{
$dbGame = $GLOBALS['dbGame'];
$params = array();
$params[':user_id'] = $user_id;
$params[':friend_id'] = $friend_id;
$stmt = $dbGame->prepare("INSERT INTO Friends(user_id, friend_id) VALUES(:user_id, :friend_id)");
$r = $stmt->execute($params);
$e = $stmt->errorInfo();
if($e[0] == "00000")
{
$message = "Friends added";
$success = true;
}
else
{
$message = "No Friends added";
$success = false;
}
return array("setFriends" => $success);
}
function getFriends($user_id)
{
$dbGame = $GLOBALS['dbGame'];
$params = array();
$params[':user_id'] = $user_id;
echo "\nGET FRIENDS\n";
echo "USER ID: " . $user_id;
$stmt = $dbGame->prepare("SELECT friend_id from Friends WHERE user_id = :user_id");
//$stmt = $dbGame->prepare("SELECT * from users");
$r = $stmt->execute($params);
$e = $stmt->errorInfo();
$friends = $stmt->fetchAll(PDO::FETCH_ASSOC);
if($e[0] == "00000")
{
$message = "Friends retrieved";
$success = true;
}
else
{
$message = "No Friends retrieved";
$success = false;
}
var_dump($friends);
return array("success" => $success, "friends" => $friends);
}
function requestProcessor($request)
{
echo "received request".PHP_EOL;
var_dump($request);
if(!isset($request['type']))
{
return "ERROR: unsupported message type";
}
switch ($request['type'])
{
case "register":
return doRegister($request['email'],$request['username'],$request['password']);
case "login":
return doLogin($request['username'],$request['password']);
case "validate_session":
return doValidate($request['sessionId']);
case "getLobbies":
return getLobbies($request['gamemode']);
case "joinLobby":
return joinLobby($request['username'], $request['lobbyID']);
case "leaveLobby":
return leaveLobby($request['username'], $request['lobbyID']);
case "setUserStats":
return setUserStats($request['user_id'], $request['gamesPlayed'], $request['wordsPlayed'], $request['gamesWon']);
case "getUserStats":
return getUserStats($request['user_id']);
case "sendChat":
return sendChat($request['user_id'], $request['message']);
case "getChat":
return getChat();
case "setFriends":
return setFriends($request['user_id'], $request['friend_id']);
case "getFriends":
return getFriends($request['user_id']);
}
return array("returnCode" => '0', 'message'=>"request type not found");
}
$logger = new rabbitLogger("RabbitLogger/logger.ini", "testListener");
$server = new rabbitMQServer("dbConn.ini","dbServer");
$foundGame = true;
$foundLobby = true;
$GLOBALS['test'] = "Test";
$GLOBALS['logger'] = $logger;
$dbGame = getDB("Game");
$dbLogin = getDB("login");
if (!isset($dbGame))
{
$logger->log_rabbit('Error', 'Game database in dbServer not connected. Is the server up?');
echo 'Game database in dbServer not connected. Is the server up?'.PHP_EOL;
$foundGame = false;
//exit();
}
else
{
$GLOBALS['dbGame'] = $dbGame;
}
if(!isset($dbLogin))
{
$logger->log_rabbit('Error', 'Login database not working in dbServer not connected. Is the server up?');
echo 'Login database in dbServer not connected. Is the server up?'.PHP_EOL;
//exit();
$foundLobby = false;
}
else
{
$GLOBALS['dbLogin'] = $dbLogin;
}
if($foundLobby == false || $foundGame == false)
{
exit();
}
echo "Started db request server";
$logger->log_rabbit('Info', "Started db request server");
$server->process_requests('requestProcessor');
exit();
?>