-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuser.php
More file actions
276 lines (218 loc) · 7.76 KB
/
Copy pathuser.php
File metadata and controls
276 lines (218 loc) · 7.76 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
<?php
include('databaseConnection.php');
// Handle incoming request
$request_method = $_SERVER["REQUEST_METHOD"];
$endpoint = $_GET["endpoint"] ?? "";
$id = $_GET["id"] ?? "";
$payLoad = $_GET["payload"] ?? "";
// CORS HANDLING
header("Access-Control-Allow-Origin: *"); // Allow all origins (you can replace '*' with a specific domain if needed)
header("Access-Control-Allow-Methods: GET, POST, PATCH, OPTIONS");
header("Access-Control-Allow-Headers: Content-Type, Authorization");
// Ensure endpoint is provided
if (!$endpoint) {
echo json_encode(["error" => "Endpoint not provided"]);
exit;
}
// API Routing
switch ($request_method) {
case 'GET':
if ($endpoint === "users") {
if ($id) {
getUser($conn, $id);
}else{
getUsers($conn);
}
} elseif ($endpoint === "monitors") {
if ($conn) {
fetchAdmin($conn);
}
} else {
echo json_encode(["error" => "Invalid endpoint"]);
}
break;
case 'PATCH':
if($endpoint === "users") {
if ($payLoad) {
// echo json_encode(["message" => "hey there", "pay" => $payLoad]);
updateUser($conn, $id, $payLoad);
}
}
break;
case 'POST':
if ($endpoint === "login") {
login($conn, $payLoad);
}
elseif($endpoint === "register") {
addUser($conn, $payLoad);
}
break;
case 'DELETE':
if ($endpoint === 'delete') {
deleteUser($conn, $id);
}
break;
default:
echo json_encode(["error" => "Method not supported"]);
}
// Function to fetch users from database
function getUsers($conn) {
$sql = "SELECT * FROM Users";
$result = $conn->query($sql);
$users = [];
while ($row = $result->fetch_assoc()) {
$users[] = $row;
}
echo json_encode(["status" => 200, "result" => $users]);
}
function getUser($conn, $id){
if (!$id) {
echo json_encode(["error" => "User ID not provided"]);
return;
}
$sql = "SELECT * FROM Users WHERE user_id=$id";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
$user = $result->fetch_assoc();
echo json_encode(["status"=> 200, "result" => $user]);
} else {
echo json_encode(["error" => "User not found"]);
}
}
// Function to add a user to the database
function addUser($conn, $payLoad) {
if (!$payLoad) {
echo json_encode(["error" => "No data provided"]);
return;
}
$data = json_decode($payLoad);
$sql = "INSERT INTO Users (first_name, last_name, department) VALUES ('$data->name', '$data->surname', '$data->dept')";
if ($conn->query($sql)) {
echo json_encode(["message" => "User added", "id" => $conn->insert_id]);
} else {
echo json_encode(["error" => "Failed to add user"]);
}
}
// function updateUser($conn, $id, $payLoad){
// if (!$payLoad) {
// echo json_encode(["error" => "No data provided"]);
// return;
// }
// $data = json_decode($payLoad, true);
// if (!is_array($data) || empty($data)) {
// echo json_encode(["error" => "Invalid or empty data"]);
// return;
// }
// $setClauses = [];
// $values = [];
// $types = '';
// foreach ($data as $key => $value) {
// // Optional: validate column names against allowed fields
// // If updating password, hash it before storing
// if ($key === 'user_pass') {
// $value = password_hash($value, PASSWORD_DEFAULT);
// }
// $setClauses[] = "`$key` = ?";
// $values[] = $value;
// $types .= is_int($value) ? 'i' : (is_float($value) ? 'd' : 's');
// }
// $values[] = $id; // Add ID for WHERE clause
// $types .= 'i'; // Assuming user_id is integer
// $sql = "UPDATE `Monitor` SET " . implode(', ', $setClauses) . " WHERE `user_id` = ?";
// $stmt = $conn->prepare($sql);
// if (!$stmt) {
// echo json_encode(["error" => "Failed to prepare statement"]);
// return;
// }
// // Bind parameters dynamically
// $stmt->bind_param($types, ...$values);
// if ($stmt->execute()) {
// echo json_encode(["message" => "User updated successfully"]);
// } else {
// echo json_encode(["error" => "Update failed", "details" => $stmt->error]);
// }
// $stmt->close();
// }
function updateUser($conn, $id, $payLoad){
if (!$payLoad) {
echo json_encode(["error" => "No data provided"]);
return;
}
$data = json_decode($payLoad, true);
if (!is_array($data) || empty($data)) {
echo json_encode(["error" => "Invalid or empty data"]);
return;
}
$setClauses = [];
$values = [];
$types = '';
foreach ($data as $key => $value) {
// 🔐 If updating password, hash it before storing
if ($key === 'user_pass') {
$value = password_hash($value, PASSWORD_DEFAULT);
}
$setClauses[] = "`$key` = ?";
$values[] = $value;
$types .= is_int($value) ? 'i' : (is_float($value) ? 'd' : 's');
}
$values[] = $id;
$types .= 'i';
$sql = "UPDATE `Monitor` SET " . implode(', ', $setClauses) . " WHERE `user_id` = ?";
$stmt = $conn->prepare($sql);
if (!$stmt) {
echo json_encode(["error" => "Failed to prepare statement"]);
return;
}
$stmt->bind_param($types, ...$values);
if ($stmt->execute()) {
echo json_encode(["message" => "User updated successfully"]);
} else {
echo json_encode(["error" => "Update failed", "details" => $stmt->error]);
}
$stmt->close();
}
function fetchAdmin($conn){
$sql = "SELECT monitor_id, user_id, email_add, user_pass, concat(first_name, ' ', last_name) 'Full Name', department
FROM Monitor LEFT JOIN Users using(user_id)
order by user_id desc;";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
$admin = $result->fetch_assoc();
echo json_encode(["status"=> 200, "result" => $admin]);
} else {
echo json_encode(["error" => "Admin not found"]);
}
}
function login($conn, $payLoad){
if ($payLoad) {
$data = json_decode($payLoad);
}
$email = $data->email;
$password = $data->password;
$sql = "SELECT monitor_id, user_id, email_add, user_pass, concat(first_name, ' ', last_name) 'Full Name', department
FROM Monitor LEFT JOIN Users using(user_id)
where email_add = '$email'";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
$user = $result->fetch_assoc();
if (password_verify($password, $user['user_pass'])) {
echo json_encode(["status"=> 200,"message" => "Logged in successfully 😎", "result" => $user]);
} else {
echo json_encode(["error" => "Wrong password😶"]);
}
} else {
echo json_encode(["error" => "User not found, please provide the correct email address😭"]);
return;
}
}
function deleteUser($conn, $id) {
if(!$id) {
echo json_encode(["message" => "User ID not provided."]);
}
$sql = "UPDATE Users SET first_name = NULL, last_name = NULL, department = NULL WHERE user_id = $id";
if($conn->query($sql)) {
echo json_encode(["message" => "User deleted successfully😭"]);
} else{
echo json_encode(["message" => "A glitch in the system, truly you are special 😂"]);
}
}