-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDB_Functions.php
More file actions
327 lines (292 loc) · 11.1 KB
/
Copy pathDB_Functions.php
File metadata and controls
327 lines (292 loc) · 11.1 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
<?php
require_once 'config.php';
class DB_Functions {
// connecting to mysql
private $mysql;// = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD,DB_DATABASE);
// constructor
function __construct() {
$this -> mysql = new mysqli(DB_HOST, DB_USER, DB_PASSWORD,DB_DATABASE)or die("Error " . mysqli_error($mysql));
/*if ($this -> mysql->connect_errno) {
echo "Failed to connect to MySQL: (" . $mysqli->connect_errno . ") " . $mysqli->connect_error;
}
else{
echo "Connected to MySQL" . PHP_EOL;
}*/
/*try{
require_once 'DB_Connect.php';
// connecting to database
$this->db = new DB_Connect();
$this->mysql = $this->db->connect();
}catch (Exception $e) {
echo 'Caught exception: ', $e->getMessage(), "\n";
}*/
}
function resultToArray($result) {
$rows = array();
while($row = $result->fetch_assoc()) {
$rows[] = $row;
}
return $rows;
}
// destructor
function __destruct() {
}
/**
*
*/
public function changeRoomName($roomID, $newName){
$result = $this->mysql->query("UPDATE Room SET name = '$newName' WHERE idRoom = '$roomID'");
if($result) return $result;
else return false;
}
public function changePropertyName($propertyID, $newName){
$result = $this->mysql->query("UPDATE Property SET address = '$newName' WHERE idProperty = '$propertyID'");
if($result) return $result;
else return false;
}
/**
* Storing new user
* returns user details
*/
public function storeUser($name, $email, $password) {
//$uuid = uniqid('', true);
$hash = $this->hashSSHA($password);
$encrypted_password = $hash["encrypted"]; // encrypted password
$salt = $hash["salt"]; // salt
//echo PHP_EOL . "INSERT INTO User(username, password, email, salt) VALUES( '$name', '$password', '$email', '$salt' )";
$result = $this->mysql->query("INSERT INTO User(username, password, email, salt) VALUES( '$name', '$encrypted_password', '$email', '$salt' )");
// check for successful store
if ($result) {
// get user details
$uid = $this->mysql->insert_id; // last inserted id
$result = $this->mysql->query("SELECT * FROM User WHERE uid = $uid");
// return user details
//echo PHP_EOL;
//echo json_encode($result->fetch_array(MYSQLI_ASSOC));
return $result->fetch_array(MYSQLI_ASSOC);
} else {
return false;
}
}
/**
* Get user by email and password
*/
public function getUserByEmailAndPassword($username, $password) {
//echo"email = $email" . PHP_EOL . "password = $password" . PHP_EOL;
$result = $this->mysql->query("SELECT * FROM User WHERE username = '$username'") or die(mysql_error());
// check for result
$no_of_rows = $this->mysql->affected_rows;
if ($no_of_rows > 0) {
$result = $result->fetch_array(MYSQLI_ASSOC);
$salt = $result['salt'];
$encrypted_password = $result['password'];
$hash = $this->checkhashSSHA($salt, $password);
// check for password equality
if ($encrypted_password == substr($hash,0,45)) {
// user authentication details are correct
return $result;
}
} else {
// user not found
return false;
}
}
/**
* Check user is existed or not
*/
public function isUserExisted($username) {
$result = $this->mysql->query("SELECT email from User WHERE username = '$username'");
if(!$result){return false;}
$no_of_rows = $this->mysql->affected_rows;
if ($no_of_rows > 0) {
// user existed
return true;
} else {
// user not existed
return false;
}
}
public function getHouseData($name){
$result = $this->mysql->query("SELECT * FROM Property WHERE username = '$name'");
$no_of_rows = $this->mysql->affected_rows;
if($no_of_rows > 0){
$rows = $this->resultToArray($result);
return $rows;
}
else{
//user has no homes created
return false;
}
}
public function gethouseDataForRooms($propertyID){
$result = $this->mysql->query("SELECT * FROM Property WHERE idProperty = '$propertyID'");
$no_of_rows = $this->mysql->affected_rows;
if($no_of_rows > 0){
$rows = $this->resultToArray($result);
return $rows;
}
else{
//user has no homes created
return false;
}
}
public function getRoomFromHouse($propertyID){
$result = $this->mysql->query("SELECT * FROM Room WHERE idProperty = '$propertyID'");
$no_of_rows = $this->mysql->affected_rows;
if($no_of_rows > 0){
$rows = $this->resultToArray($result);
return $rows;
}
else{
//user has no homes created
return false;
}
}
public function getConnections($roomID){
$result = $this->mysql->query("SELECT * FROM Connection WHERE idSource = '$roomID'");
$no_of_rows = $this->mysql->affected_rows;
if($no_of_rows > 0){
$rows = $this->resultToArray($result);
return $rows;
}
else{
//user has no homes created
return false;
}
}
public function createRoom($name, $propertyID, $url){
$result = $this->mysql->query("INSERT INTO Room(name,idProperty,roomURL) VALUES('$name', '$propertyID', '$url')");
if ($result) {
// get user details
$uid = $this->mysql->insert_id; // last inserted id
$result = $this->mysql->query("SELECT * FROM Room WHERE idRoom = $uid");
// return user details
//echo PHP_EOL;
//echo json_encode($result->fetch_array(MYSQLI_ASSOC));
return $result->fetch_array(MYSQLI_ASSOC);
} else {
return false;
}
}
public function createHouse($address, $username, $url, $defaultRoom){
$result = $this->mysql->query("INSERT INTO Property(address, username, houseURL, idDefaultRoom) VALUES('$address', '$username', '$url', '$defaultRoom')");
if ($result) {
// get user details
$uid = $this->mysql->insert_id; // last inserted id
$result = $this->mysql->query("SELECT * FROM Property WHERE idProperty = $uid");
// return user details
//echo PHP_EOL;
//echo json_encode($result->fetch_array(MYSQLI_ASSOC));
return $result->fetch_array(MYSQLI_ASSOC);
} else {
return false;
}
}
public function createConnection($source, $destination, $doorX, $doorY, $doorZ){
$result = $this->mysql->query("INSERT INTO Connection(idSource, idDestination, doorX, doorY, doorZ) VALUES('$source', '$destination', '$doorX', '$doorY', '$doorZ')");
if($result){
$uid = $this->mysql->insert_id; // last inserted id
$result = $this->mysql->query("SELECT * FROM Connection WHERE idConnection = $uid");
return $result->fetch_array(MYSQLI_ASSOC);
}
else return false;
}
public function deleteRoom($roomID){
$result = $this->mysql->query("DELETE FROM Room WHERE idRoom = '$roomID'");
$no_of_rows = $this->mysql->affected_rows;
if($no_of_rows > 0){
return true;
}
else{
//user has no homes created
return false;
}
}
public function deleteProperty($propertyID){
$result = $this->mysql->query("DELETE FROM Property WHERE idProperty = '$propertyID'");
$no_of_rows = $this->mysql->affected_rows;
if($no_of_rows > 0){
$result2 = $this->mysql->query("DELETE FROM Room WHERE idProperty = '$propertyID'");
$no_of_rows = $this->mysql->affected_rows;
if($no_of_rows > 0){
return true;
}
return false;
}
else{
//user has no homes created
return false;
}
}
public function deleteConnection($connectionID){
$result = $this->mysql->query("DELETE FROM Connection WHERE idConnection = '$connectionID'");
$no_of_rows = $this->mysql->affected_rows;
if($no_of_rows > 0){
return true;
}
else{
//user has no homes created
return false;
}
}
public function changeRoomURL($roomID, $URL){
$result = $this->mysql->query("UPDATE Room SET roomURL = '$URL' WHERE idRoom = '$roomID'");
if($result) return $result;
else return false;
}
public function changeConnectiontarget($connectionID, $doorX, $doorY, $doorZ){
$result = $this->mysql->query("UPDATE Connection SET doorX = '$doorX', doorY='$doorY', doorZ='$doorZ' WHERE idConnection = '$connectionID'");
if($result) return $result;
else return false;
}
public function changeHouseURL($propertyID, $URL){
$result = $this->mysql->query("UPDATE Property SET houseURL = '$URL' WHERE idProperty = '$propertyID'");
if($result) return $result;
else return false;
}
public function changeHouseDefaultRoom($propertyID, $defaultRoom){
$result = $this->mysql->query("UPDATE Property SET idDefaultRoom = '$defaultRoom' WHERE idProperty = '$propertyID'");
if($result) return $result;
else return false;
}
public function changeConnectionDest($connectionID, $newDest){
$result = $this->mysql->query("UPDATE Connection SET idDestination = '$newDest' WHERE idConnection = '$connectionID'");
if($result) return $result;
else return false;
}
public function getConnectionInfo($connectionID){
$result = $this->mysql->query("SELECT * FROM Connection WHERE idConnection = '$connectionID'");
$no_of_rows = $this->mysql->affected_rows;
if($no_of_rows > 0){
$rows = $this->resultToArray($result);
return $rows;
}
else{
//user has no homes created
return false;
}
}
/**
* Encrypting password
* @param password
* returns salt and encrypted password
*/
public function hashSSHA($password) {
$salt = sha1(rand());
$salt = substr($salt, 0, 10);
$encrypted = $this->checkhashSSHA($salt,$password);
$hash = array("salt" => $salt, "encrypted" => $encrypted);
return $hash;
}
/**
* Decrypting password
* @param salt, password
* returns hash string
*/
public function checkhashSSHA($salt, $password) {
$hash = base64_encode(sha1($password . $salt) . $salt);
return $hash;
//return $password;
}
}
?>