-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfunctions.php
More file actions
395 lines (349 loc) · 13.8 KB
/
functions.php
File metadata and controls
395 lines (349 loc) · 13.8 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
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
<?php
function connect(){ //creates a connection to the database
$dbhost = "localhost";
$dbuser = "root";
$dbpass = "";
$dbname = "game_shelf";
$result = mysqli_connect($dbhost, $dbuser, $dbpass, $dbname);
return $result; //equivalent to $con
}
function test_input($data) { //prevents sql injection
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
function valid_username($user_name){ //checks for valid usernames
if (!empty($user_name)){
if (strlen($user_name) < 16){
if (preg_match("/^[0-9a-zA-Z-' ]*$/",$user_name)) {
$query = "SELECT user_name FROM users WHERE user_name='$user_name';";
$result = mysqli_query(connect(), $query);
if (mysqli_num_rows($result)==0){
return "Ok";
}else{ //Hi and hi can't both be users because SQL can't tell them apart
return "That username is already in use";
}
} else{
return "Only letters and numbers are allowed";
}
} else{
return "Username must be under 16 characters.";
}
}
return "Username is required";
}
function valid_password($password){ //checks for valid passwords
if (!empty($password)){
if (strlen($password) < 16){
return "Ok";
} else{
return "Password must be under 16 characters";
}
}
return "Password is required";
}
function valid_email($email) { //checks for valid emails
if (!empty($email)){
if (filter_var($email, FILTER_VALIDATE_EMAIL)) {
$query = "SELECT user_id FROM users WHERE email='$email';";
$result = mysqli_query(connect(), $query);
if (mysqli_num_rows($result)==0){
return "Ok";
}else{
return "That email address is already in use";
}
} else{
return "Not a valid email address";
}
}
return "Email is required";
}
function get_personnal_info ($user_id){ //returns an array(user_name, email, num_games, bio)
$query = "SELECT user_name, email, num_games, bio, profile_picture FROM users WHERE user_id = '$user_id'";
$result = mysqli_fetch_assoc(mysqli_query(connect(), $query));
return $result;
}
function style_input($game_name){ //styles input by capitalizing and trimming whitespace
$game_name = trim($game_name, " ");//get rid of spaces
$game_name = strtolower($game_name);
$game_name = substr_replace($game_name, strtoupper(substr($game_name, 0, 1)), 0, 1);
for ($i = 1; $i < strlen($game_name); $i++){
$prev_char = substr($game_name, $i - 1, 1);
if ($prev_char === " " || $prev_char === "-"){
$game_name = substr_replace($game_name, strtoupper(substr($game_name, $i, 1)), $i, 1);
}
}
return $game_name;
}
function ensure_login($user_id){ //sends to login if user not logged in
if(!isset($user_id)){
header("Location: login.php");
}
}
function str_to_stylized_arr($str){ //splits a string into an array using commas as separators
$arr = explode(",", $str);
$len = sizeof($arr);
for($i = 0; $i < $len; $i=$i+1){
$arr[$i] = style_input($arr[$i]); //styles words
}
return $arr;
}
function get_my_games($my_id){ //returns an array of games which belong to a specific player
$query = "SELECT games FROM games_by_user WHERE user_id='$my_id'";
$result = mysqli_fetch_assoc(mysqli_query(connect(), $query));
$result = $result['games'];
$arr = explode(",", $result);
return $arr;
}
function add_existing_game($game_id, $my_id){//adds a game to a user's gameshelf, increases owned games, and increases game popularity
$con = connect();
//adds the current game_id to a users game shelf
$query = "SELECT games FROM games_by_user WHERE user_id = '$my_id'";
$result = mysqli_fetch_assoc(mysqli_query($con, $query));
$new_result = $result['games'] . "," . $game_id;
$new_result = trim($new_result, ",");
$query = "UPDATE games_by_user SET games='$new_result' WHERE user_id = '$my_id'";
mysqli_query($con, $query);
//increases number of owned games by one
$query = "SELECT num_games FROM users WHERE user_id='$my_id'";
$result = mysqli_fetch_assoc(mysqli_query($con, $query));
$new_result = $result['num_games'] + 1;
$query = "UPDATE users SET num_games='$new_result' WHERE user_id = '$my_id'";
mysqli_query($con, $query);
//increases game popularity
$query = "SELECT popularity FROM game_library WHERE game_id = '$game_id'";
$result = mysqli_fetch_assoc(mysqli_query($con, $query));
$new_result = $result['popularity'] + 1;
$query = "UPDATE game_library SET popularity='$new_result' WHERE game_id='$game_id'";
mysqli_query($con, $query);
}
function remove_game($game_id, $my_id){ //removes a game from a user's shelf, decreases owned games and decreases game popularity
$con = connect();
//removes the current game_id to a users game shelf
$my_games = get_my_games($my_id);
$key = array_search($game_id, $my_games);
unset($my_games[$key]);
$new_result = "";
foreach($my_games as $game){
$new_result = $new_result . $game . ",";
}
$new_result = trim($new_result, ",");
$query = "UPDATE games_by_user SET games='$new_result' WHERE user_id = '$my_id'";
mysqli_query($con, $query);
//decrease number of owned games by one
$query = "SELECT num_games FROM users WHERE user_id='$my_id'";
$result = mysqli_fetch_assoc(mysqli_query($con, $query));
$new_result = $result['num_games'] - 1;
$query = "UPDATE users SET num_games='$new_result' WHERE user_id = '$my_id'";
mysqli_query($con, $query);
//decrease game popularity
$query = "SELECT popularity FROM game_library WHERE game_id = '$game_id'";
$result = mysqli_fetch_assoc(mysqli_query($con, $query));
$new_result = $result['popularity'] - 1;
$query = "UPDATE game_library SET popularity='$new_result' WHERE game_id='$game_id'";
mysqli_query($con, $query);
}
function get_popularity_chart(){//returns an array where the index is the number shown in the popularity column and the value is the popularity index (1st, 3rd, 5th) ex: arr(1, 1, 2, 2, 3) -> arr(1=>4, 2=>2, 3=>1)
$con = connect();
//setting up popularity array
$query = "SELECT popularity FROM game_library";
$data = mysqli_query($con, $query);
$individual_popularity = array();
while($row = mysqli_fetch_assoc($data)){
array_push($individual_popularity, $row['popularity']);
}
sort($individual_popularity);
$total = sizeof($individual_popularity);
$max = $individual_popularity[$total-1];
$result = array();
$last = -1;
for($i = $total-1; $i > -1; $i--){
if ($individual_popularity[$i] != $last){
$result[$individual_popularity[$i]] = ($total) - $i;
$last = $individual_popularity[$i];
}
}
return $result;
}
function get_friends($my_id){ //returns an array of friends
$query = "SELECT friends from friends WHERE user_id = '$my_id'";
$result = mysqli_fetch_assoc(mysqli_query(connect(), $query));
if (strlen($result['friends']) > 0){
$result = $result['friends'];
$result = explode(",", $result);
return $result;
}
return array();
}
function get_user_data_from_users($user_list){//returns an array containing arrays of user data. The first element in the array to be returned [0] refers to the first user_id in $user_list
$con = connect();
$query = "SELECT user_id, user_name, num_games, bio, profile_picture FROM users";
$data = mysqli_query($con, $query);
$values = array();
while($row = mysqli_fetch_assoc($data)){
if(in_array($row['user_id'], $user_list)){
array_push($values, $row);
}
}
return $values;
}
function user_name_list(){//returns a list of all username
$con = connect();
$query = "SELECT user_name FROM users";
$result = mysqli_query($con, $query);
$user_name_list = array();
while ($row = mysqli_fetch_assoc($result)){
array_push($user_name_list, $row['user_name']);
}
return $user_name_list;
}
function convert_arr_to_str($arr){ //converts an array to a string separated by commas
$new_arr = "";
foreach ($arr as $elem){
$new_arr = $new_arr . "," . $elem;
}
$new_arr = trim($new_arr, ",");
return $new_arr;
}
function send_friend_request($my_id, $friend_user_name) {//sends a friend request or output error
$err_message = "Done";
$con = connect();
$query = "SELECT user_id FROM users WHERE user_name = '$friend_user_name'";
$result = mysqli_query($con, $query);
if (mysqli_num_rows($result) < 1){
$err_message = "This user does not exist.";
} elseif(!in_array($friend_user_name, user_name_list())){ //for case sensitivity
$err_message = "This user does not exist.";
} else{
$result = mysqli_fetch_assoc($result);
$friend_id = $result['user_id'];
if(in_array($friend_id, get_friends($my_id))){ //already friends
$err_message = "This user is already your friend.";
} else{
$query = "SELECT pending_requests, incoming_requests FROM friends WHERE user_id = '$my_id'";
$result = mysqli_fetch_assoc(mysqli_query($con, $query));
$pending_requests = explode(",", $result['pending_requests']);
$incoming_requests = explode(",", $result['incoming_requests']);
if(in_array($friend_id, $pending_requests)){ //already sent
$err_message = "You have already sent this person a friend request.";
} elseif(in_array($friend_id, $incoming_requests)){ //already trying to be friends
$err_message = "This person has already sent you a friend request.";
} else{ //send the friend request
//put friend into your pending
array_push($pending_requests, $friend_id);
$pending_requests = convert_arr_to_str($pending_requests);
$query = "UPDATE friends SET pending_requests='$pending_requests' WHERE user_id='$my_id'";
mysqli_query($con, $query);
//put you into the other user's incoming_requests
$query = "SELECT incoming_requests FROM friends WHERE user_id = '$friend_id'";
$result = mysqli_fetch_assoc(mysqli_query($con, $query));
$new_result = $result['incoming_requests'] . "," . $my_id;
$new_result = trim($new_result, ",");
$query = "UPDATE friends SET incoming_requests='$new_result' WHERE user_id='$friend_id'";
mysqli_query($con, $query);
}
}
}
return $err_message;
}
function remove_friend_request($my_id, $friend_id){ //takes the pending request from the friend and the incoming request from my_id and removes the associated id
$con = connect();
$query = "SELECT incoming_requests FROM friends WHERE user_id='$my_id'";
$result = mysqli_fetch_assoc(mysqli_query($con, $query));
$result = explode(",", $result['incoming_requests']);
$new_result = array();
foreach($result as $req){
if($req != $friend_id){
array_push($new_result, $req);
}
}
$new_result = convert_arr_to_str($new_result);
$query = "UPDATE friends SET incoming_requests='$new_result' WHERE user_id='$my_id'";
mysqli_query($con, $query);
$query = "SELECT pending_requests FROM friends WHERE user_id='$friend_id'";
$result = mysqli_fetch_assoc(mysqli_query($con, $query));
$result = explode(",", $result['pending_requests']);
$new_result = array();
foreach($result as $pen){
if($pen != $my_id){
array_push($new_result, $pen);
}
}
$new_result = convert_arr_to_str($new_result);
$query = "UPDATE friends SET pending_requests='$new_result' WHERE user_id = '$friend_id'";
mysqli_query($con, $query);
}
function add_friend($my_id, $friend_id){ //adds $friend_id to the friend list of my_id
$con=connect();
$query = "SELECT friends FROM friends WHERE user_id='$my_id'";
$result = mysqli_fetch_assoc(mysqli_query($con, $query));
$new_result = $result['friends'] . "," . $friend_id;
$new_result = trim($new_result, ",");
$query = "UPDATE friends SET friends='$new_result' WHERE user_id='$my_id'";
mysqli_query($con, $query);
}
function incoming_request($my_id){ //returns true if a user has an incoming friend request
$con = connect();
$query = "SELECT incoming_requests FROM friends WHERE user_id='$my_id'";
$result = mysqli_fetch_assoc(mysqli_query($con, $query));
$result = $result['incoming_requests'];
return strlen($result) > 0;
}
function get_all_mechanisms(){ //returns an array containing all the game mechanisms
$con = connect();
$types = array("Escape Room", "Cooperative", "Hidden Role", "Area Control", "Deck Building", "Card Drafting", "Dungeon Crawler", "Roll and Write", "Worker Placement");
$query = "SELECT category FROM game_library";
$result = mysqli_query($con, $query);
while($row = mysqli_fetch_assoc($result)){
$row = explode(",", $row['category']);
foreach ($row as $elem){
if(!in_array($elem, $types)){
array_push($types, $elem);
}
}
}
return $types;
}
function output($arr){//echoes an array
foreach($arr as $el){
echo $el . "<br>";
}
}
function get_game_data($arr){ //gets all game data for an array of game_ids. The first game_id of the array corresponds to the first element in the output.
$con = connect();
$query = "SELECT * FROM game_library";
$result = mysqli_query($con, $query);
$game_data = array();
while($row = mysqli_fetch_assoc($result)){
if(in_array($row['game_id'], $arr)){
array_push($game_data, $row);
}
}
return $game_data;
}
function alphabetical_comparison($x, $y){ //a-z
return strcmp($x['game_name'], $y['game_name']);
}
function complexity_comparison($x, $y){ //lowest to highest complexity
return $x['complexity'] > $y['complexity'];
}
function popularity_comparison($x, $y){ //highest to lowest popularity
return $x['popularity'] < $y['popularity'];
}
function time_comparison($x, $y){ //shortest to longest time
$x_time = explode(",", $x['length']);
$x_time = (int)$x_time[0] + (int)$x_time[1];
$y_time = explode(",", $y['length']);
$y_time = (int)$y_time[0] + (int)$y_time[1];
return $x_time > $y_time;
}
function get_corresponding_user_id($user_name){ //gets the user_id corresponding to a username
$con = connect();
$query = "SELECT user_id FROM users WHERE user_name='$user_name'";
$result = mysqli_query($con, $query);
$result = mysqli_fetch_assoc($result);
$result = $result['user_id'];
return $result;
}
?>