-
Notifications
You must be signed in to change notification settings - Fork 0
/
actions.php
156 lines (125 loc) · 5.82 KB
/
actions.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
<?php
$servername = "localhost";
$dbname = "xxx";
$username = "xxx";
$password = "xxx";
// Create & check connection
$conn = new mysqli($servername, $username, $password);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
else {
mysqli_select_db($conn, $dbname);
}
if (isset($_POST['addPokemon'])) {
writeToLogs("PokemonAdded.txt", "\n\n\n Adding New Pokemon... [" . date("Y-m-d h:i:sa", time()) . "]");
writeToLogs("PokemonAdded.txt", "\n ----------------------------------------");
$name = $conn->real_escape_string($_POST['name']);
$username = $conn->real_escape_string($_POST['username']);
$latitude = $conn->real_escape_string($_POST['latitude']);
$longitude = $conn->real_escape_string($_POST['longitude']);
$sql = "INSERT INTO pokemon (name, username, latitude, longitude) VALUES('" . $name . "', '" . $username . "', '" . $latitude . "', '" . $longitude . "')";
$result = mysqli_query($conn, $sql);
if(!$result) {
writeToLogs("PokemonAdded.txt", "\n " . $name . " could not be added.");
echo("failure");
}
else {
writeToLogs("PokemonAdded.txt", "\n " . $name. " added successfully.");
echo("success");
}
}
else if (isset($_POST['getPokemon'])) {
writeToLogs("PokemonRetrieved.txt", "\n\n\n Getting Pokemon... [" . date("Y-m-d h:i:sa", time()) . "]");
writeToLogs("PokemonRetrieved.txt", "\n ----------------------------------------");
$sql = "SELECT * FROM pokemon";
$result = mysqli_query($conn, $sql);
if(mysqli_num_rows($result) > 0) {
writeToLogs("PokemonRetrieved.txt", "\n Successfully retrieved all Pokemon.");
$allPokemon = array();
while($row = mysqli_fetch_array($result)) {
$pokemon = array();
$pokemon["name"] = $row["name"];
$pokemon["latitude"] = $row["latitude"];
$pokemon["longitude"] = $row["longitude"];
array_push($allPokemon, $pokemon);
}
echo json_encode($allPokemon);
}
else {
writeToLogs("PokemonRetrieved.txt", "\n Could not retrieve all Pokemon.");
echo "Error: " . mysqli_error($conn);
}
}
else if (isset($_POST['checkUser'])) {
writeToLogs("UsersAdded.txt", "\n\n\n Checking User... [" . date("Y-m-d h:i:sa", time()) . "]");
writeToLogs("UsersAdded.txt", "\n ----------------------------------------");
$username = $conn->real_escape_string($_POST['username']);
$email = $conn->real_escape_string($_POST['email']);
$sql = "SELECT * FROM users WHERE username='" . $username . "'";
$result = mysqli_query($conn, $sql);
if(mysqli_num_rows($result) > 0) {
writeToLogs("UsersAdded.txt", "\n Username exists " . $username);
echo "username exists";
}
else {
writeToLogs("UsersAdded.txt", "\n Username does not exist, checking email..");
$sql = "SELECT * FROM users WHERE email='" . $email . "'";
$result = mysqli_query($conn, $sql);
if(mysqli_num_rows($result) > 0) {
echo "email exists";
}
else {
writeToLogs("UsersAdded.txt", "\n Email does not exist.");
}
}
}
else if (isset($_POST['addUser'])) {
writeToLogs("UsersAdded.txt", "\n\n\n Adding New User... [" . date("Y-m-d h:i:sa", time()) . "]");
writeToLogs("UsersAdded.txt", "\n ----------------------------------------");
$username = $conn->real_escape_string($_POST['username']);
$email = $conn->real_escape_string($_POST['email']);
$password = $conn->real_escape_string($_POST['password']);
$team = $conn->real_escape_string($_POST['team']);
$sql = "INSERT INTO users (username, email, password, team) VALUES('" . $username . "', '" . $email . "', '" . $password . "', '" . $team . "')";
$result = mysqli_query($conn, $sql);
if(!$result) {
writeToLogs("UsersAdded.txt", "\n " . $username . " could not be added.");
echo("failure");
}
else {
writeToLogs("UsersAdded.txt", "\n " . $username. " added successfully.");
echo("success");
}
}
else if (isset($_POST['getUser'])) {
writeToLogs("UsersRetrieved.txt", "\n\n\n Getting User... [" . date("Y-m-d h:i:sa", time()) . "]");
writeToLogs("UsersRetrieved.txt", "\n ----------------------------------------");
$username = $conn->real_escape_string($_POST['username']);
$password = $conn->real_escape_string($_POST['password']);
$reply = array();
$sql = "SELECT * FROM users WHERE username='" . $username . "' AND password='" . $password . "'";
$result = mysqli_query($conn, $sql);
if(mysqli_num_rows($result) > 0) {
writeToLogs("UsersRetrieved.txt", "\n Successfully retrieved User.");
while($row = mysqli_fetch_array($result)) {
$user = array();
$user["team"] = $row["team"];
}
$user["status"] = "success";
array_push($reply, $user);
}
else {
$user = array();
$user["status"] = "failure";
array_push($reply, $user);
writeToLogs("UsersRetrieved.txt", "\n Username / password combination does not exist: " . $username);
}
echo json_encode($reply);
}
function writeToLogs($fileToWrite, $textToWrite) {
$updatedFile = file_get_contents($fileToWrite);
$updatedFile .= $textToWrite;
file_put_contents($fileToWrite, $updatedFile);
}
?>