-
Notifications
You must be signed in to change notification settings - Fork 1
/
database_class.php
215 lines (201 loc) · 6.35 KB
/
database_class.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
<?php
class Database{
public $server;
public $username;
public $password;
public $database;
public $conn;
public function __construct($server,$username,$password,$database){
$this->server = $server;
$this->username = $username;
$this->password = $password;
$this->database = $database;
$this->conn = new mysqli($this->server,$this->username,$this->password, $this->database);
}
public function addUser($userName,$firstName,$lastName,$email,$password){
$db = $this->conn;
$sql = "INSERT INTO Users VALUES (null,'$userName','".sha1($password)."','$firstName','$lastName','$email')";
if (!$db->query($sql)){
echo $db->error;
}
}
public function login($username, $password){
$db2 = $this->conn;
$sql = "SELECT * FROM Users WHERE username='$username' AND password =sha1('$password')";
if(!$db2->query($sql)){
echo $db2->error;
}
$result = $db2->query($sql);
if ($result->num_rows == 1){
$_SESSION['valid_user'] = $username;
}
}
public function returnUserID($username){
$db3 = $this->conn;
$sql = "SELECT * FROM Users WHERE username = '$username'";
if(!$db3->query($sql)){
echo $db3->error;
}
$result = $db3->query($sql);
$row = $result->fetch_array(MYSQLI_ASSOC);
return $row["id"];
}
public function returnFirstName($username){
$db3 = $this->conn;
$sql = "SELECT * FROM Users WHERE username = '$username'";
if(!$db3->query($sql)){
echo $db3->error;
}
$result = $db3->query($sql);
$row = $result->fetch_array(MYSQLI_ASSOC);
return $row["firstName"];
}
public function returnLastName($username){
$db3 = $this->conn;
$sql = "SELECT * FROM Users WHERE username = '$username'";
if(!$db3->query($sql)){
echo $db3->error;
}
$result = $db3->query($sql);
$row = $result->fetch_array(MYSQLI_ASSOC);
return $row["lastName"];
}
public function returnEmail($username){
$db3 = $this->conn;
$sql = "SELECT * FROM Users WHERE username = '$username'";
if(!$db3->query($sql)){
echo $db3->error;
}
$result = $db3->query($sql);
$row = $result->fetch_array(MYSQLI_ASSOC);
return $row["email"];
}
public function updateUser($username,$firstName,$lastName,$email){
$db = $this->conn;
$sql = "UPDATE Users SET firstName = '$firstName', lastName = '$lastName', email = '$email' WHERE username = '$username'";
if (!$db->query($sql)){
echo $db->error;
}
}
public function returnChampions(){
$db3 = $this->conn;
$sql = "SELECT * FROM Users WHERE username = '$username'";
if(!$db3->query($sql)){
echo $db3->error;
}
$result = $db3->query($sql);
$row = $result->fetch_array(MYSQLI_ASSOC);
return $row["email"];
}
public function returnGuestbookPosts(){
$db3 = $this->conn;
$sql = "SELECT * FROM Guestbook ORDER BY id DESC";
if(!$db3->query($sql)){
echo $db3->error;
}
$result = $db3->query($sql);
$return = "";
while ($row = $result->fetch_array()){
$return = $return."<div id='userPost'>
<div id='contentHeader'>From: ".$row['from_']." ".$row['subject']."</div>
<div id='contentBody'>".$row['message']."</div>
</div>";
}
return $return;
}
public function returnRecentLevels(){
$db3 = $this->conn;
$sql = "SELECT * FROM Levels ORDER BY id DESC LIMIT 2";
if(!$db3->query($sql)){
echo $db3->error;
}
$result = $db3->query($sql);
$return = "";
while ($row = $result->fetch_array()){
$return = $return."<a href='#'><img class='sideImage' width='200px' src='images/".$row['imagePath']."' /></a>";
}
return $return;
}
public function returnAllLevels(){
$db3 = $this->conn;
$sql = "SELECT * FROM Levels ORDER BY id DESC";
if(!$db3->query($sql)){
echo $db3->error;
}
$result = $db3->query($sql);
$return = "";
while ($row = $result->fetch_array()){
$return = $return."<a href='#'><img class='sideImage' width='200px' src='images/".$row['imagePath']."' /></a>";
if ($count % 2 !=0){
$return = $return."<br />";
}
$count++;
}
return $return;
}
public function addGuestbookPost($subject, $message, $poster){
$db4 = $this->conn;
$sql = "INSERT INTO Guestbook VALUES (null, '$subject', '$message', '$poster',null)";
if (!$db4->query($sql)){
echo $db4->error;
}
}
public function changePassword($user, $password){
$db = $this->conn;
$sql = "UPDATE Users SET password = '".sha1($password)."' WHERE username = '$user'";
if (!$db->query($sql)){
echo $db->error;
}
echo $password;
}
public function getCompletedLevels($username){
$db = $this->conn;
$id = $this->returnUserID($username);
$sql = "SELECT COUNT(id) AS 'Result' from CompletedLevels WHERE playerID = '$id' group by playerID";
$result = $db->query($sql);
$row = $result->fetch_array(MYSQLI_ASSOC);
return $row['Result'];
}
public function getTotalLevels(){
$db = $this->conn;
$sql = "SELECT COUNT(id) AS 'Result' from Levels";
$result = $db->query($sql);
$row = $result->fetch_array(MYSQLI_ASSOC);
return $row['Result'];
}
public function getTotalPlayers(){
$db = $this->conn;
$sql = "SELECT COUNT(id) AS 'Result' from Users";
$result = $db->query($sql);
$row = $result->fetch_array(MYSQLI_ASSOC);
return $row['Result'];
}
public function logout(){
if(isset($_SESSION['valid_user'])) {
unset($_SESSION['valid_user']);
session_destroy();
$page = 'index';
header('Location:controller.php?page="index"');
}
}
public function getTopPlayer(){
$db = $this->conn;
$sql = "SELECT COUNT(CompletedLevels.id) AS 'Result', CompletedLevels.playerID, Users.username from CompletedLevels INNER JOIN Users ON CompletedLevels.playerID = Users.id group by playerID ORDER BY Result DESC LIMIT 1";
$result = $db->query($sql);
$row = $result->fetch_array(MYSQLI_ASSOC);
return $row['username'];
}
public function getTopPlayers($table){
$db = $this->conn;
$sql = "SELECT COUNT(CompletedLevels.id) AS 'Result', CompletedLevels.playerID, Users.username from CompletedLevels INNER JOIN Users ON CompletedLevels.playerID = Users.id group by playerID ORDER BY Result DESC";
$result = $db->query($sql);
while ($row = $result->fetch_array()){
$table->addData($row['username']);
$table->addData($row['Result']);
$table->commitData();
}
$table->finishTable();
//return $return;
}
}
?>