-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgetdata.php
More file actions
225 lines (209 loc) · 11 KB
/
Copy pathgetdata.php
File metadata and controls
225 lines (209 loc) · 11 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
<?php
//Connect to database
require 'connectDB.php';
date_default_timezone_set('Asia/Manila');
$d = date("Y-m-d");
list($microseconds, $seconds) = explode(' ', microtime());
$t = date("H:i:s", $seconds) . substr($microseconds, 1, 4) . ' '.date("a"); // Get the first 4 digits of microseconds
if (isset($_GET['card_uid']) && isset($_GET['device_token'])) {
$card_uid = $_GET['card_uid'];
$device_uid = $_GET['device_token'];
$sql = "SELECT * FROM devices WHERE device_uid=?";
$result = mysqli_stmt_init($conn);
if (!mysqli_stmt_prepare($result, $sql)) {
echo "SQL_Error_Select_device";
exit();
}
else{
mysqli_stmt_bind_param($result, "s", $device_uid);
mysqli_stmt_execute($result);
$resultl = mysqli_stmt_get_result($result);
if ($row = mysqli_fetch_assoc($resultl)){
$device_mode = $row['device_mode'];
$device_dep = $row['device_dep'];
if ($device_mode == 1) {
$sql = "SELECT * FROM users WHERE card_uid=?";
$result = mysqli_stmt_init($conn);
if (!mysqli_stmt_prepare($result, $sql)) {
echo "SQL_Error_Select_card";
exit();
}
else{
mysqli_stmt_bind_param($result, "s", $card_uid);
mysqli_stmt_execute($result);
$resultl = mysqli_stmt_get_result($result);
if ($row = mysqli_fetch_assoc($resultl)){
//*****************************************************
//An existed Card has been detected for Login or Logout
if ($row['add_card'] == 1){
if ($row['device_uid'] == $device_uid || $row['device_uid'] == 0){
$Uname = $row['username'];
$Number = $row['serialnumber'];
$ssid = $row['ssid'];
$Birthdate = $row['Birthdate'];
$Contact = $row['Contact'];
$EmergencyContact = $row['EmergencyContact'];
$ValidationPeriod = $row['ValidationPeriod'];
$MedicalHistory = $row['MedicalHistory'];
$sql = "SELECT * FROM users_logs WHERE card_uid=? AND checkindate=? AND card_out=0 AND ssid=? ";
$result = mysqli_stmt_init($conn);
if (!mysqli_stmt_prepare($result, $sql)) {
echo "SQL_Error_Select_logs";
exit();
}
else{
mysqli_stmt_bind_param($result, "sss", $card_uid, $d, $ssid);
mysqli_stmt_execute($result);
$resultl = mysqli_stmt_get_result($result);
//*****************************************************
//Login
if (!$row = mysqli_fetch_assoc($resultl)){
// Check if the username already exists in the users_logs table
$sql_check_username = "SELECT id FROM users_logs WHERE username = ?";
$stmt_check_username = mysqli_prepare($conn, $sql_check_username);
mysqli_stmt_bind_param($stmt_check_username, "s", $Uname);
mysqli_stmt_execute($stmt_check_username);
mysqli_stmt_store_result($stmt_check_username);
if (mysqli_stmt_num_rows($stmt_check_username) == 0) {
// Username doesn't exist in the users_logs table, insert a new record
$sql_insert = "INSERT INTO users_logs (username, serialnumber, card_uid, device_uid, device_dep, checkindate, timein, timeout, ssid) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)";
$stmt_insert = mysqli_prepare($conn, $sql_insert);
$timeout = "00:00:00";
mysqli_stmt_bind_param($stmt_insert, "sdsssssss", $Uname, $Number, $card_uid, $device_uid, $device_dep, $d, $t, $timeout, $ssid);
mysqli_stmt_execute($stmt_insert);
echo "login".$Uname;
echo "time: ".$t;
exit();
}
}
//*****************************************************
//Logout
else{
$sql="UPDATE users_logs SET timeout=?, card_out=1 WHERE card_uid=? AND checkindate=? AND card_out=0";
$result = mysqli_stmt_init($conn);
if (!mysqli_stmt_prepare($result, $sql)) {
echo "SQL_Error_insert_logout1";
exit();
}
else{
mysqli_stmt_bind_param($result, "sss", $t, $card_uid, $d);
mysqli_stmt_execute($result);
echo "logout".$Uname;
echo "Time".$t;
exit();
}
}
}
}
else {
echo "Not Allowed!";
exit();
}
}
else if ($row['add_card'] == 0){
echo "Not registerd!";
exit();
}
}
else{
echo "Not found!";
exit();
}
}
}
else if ($device_mode == 0) {
//New Card has been added
$sql = "SELECT * FROM users WHERE card_uid=?";
$result = mysqli_stmt_init($conn);
if (!mysqli_stmt_prepare($result, $sql)) {
echo "SQL_Error_Select_card";
exit();
}
else{
mysqli_stmt_bind_param($result, "s", $card_uid);
mysqli_stmt_execute($result);
$resultl = mysqli_stmt_get_result($result);
//The Card is available
if ($row = mysqli_fetch_assoc($resultl)){
$sql = "SELECT card_select FROM users WHERE card_select=1";
$result = mysqli_stmt_init($conn);
if (!mysqli_stmt_prepare($result, $sql)) {
echo "SQL_Error_Select";
exit();
}
else{
mysqli_stmt_execute($result);
$resultl = mysqli_stmt_get_result($result);
if ($row = mysqli_fetch_assoc($resultl)) {
$sql="UPDATE users SET card_select=0";
$result = mysqli_stmt_init($conn);
if (!mysqli_stmt_prepare($result, $sql)) {
echo "SQL_Error_insert";
exit();
}
else{
mysqli_stmt_execute($result);
$sql="UPDATE users SET card_select=1 WHERE card_uid=?";
$result = mysqli_stmt_init($conn);
if (!mysqli_stmt_prepare($result, $sql)) {
echo "SQL_Error_insert_An_available_card";
exit();
}
else{
mysqli_stmt_bind_param($result, "s", $card_uid);
mysqli_stmt_execute($result);
echo "available";
exit();
}
}
}
else{
$sql="UPDATE users SET card_select=1 WHERE card_uid=?";
$result = mysqli_stmt_init($conn);
if (!mysqli_stmt_prepare($result, $sql)) {
echo "SQL_Error_insert_An_available_card";
exit();
}
else{
mysqli_stmt_bind_param($result, "s", $card_uid);
mysqli_stmt_execute($result);
echo "available";
exit();
}
}
}
}
//The Card is new
else{
$sql="UPDATE users SET card_select=0";
$result = mysqli_stmt_init($conn);
if (!mysqli_stmt_prepare($result, $sql)) {
echo "SQL_Error_insert";
exit();
}
else{
mysqli_stmt_execute($result);
$sql = "INSERT INTO users (card_uid, card_select, device_uid, device_dep, user_date) VALUES (?, 1, ?, ?, CURDATE())";
$result = mysqli_stmt_init($conn);
if (!mysqli_stmt_prepare($result, $sql)) {
echo "SQL_Error_Select_add";
exit();
}
else{
mysqli_stmt_bind_param($result, "sss", $card_uid, $device_uid, $device_dep );
mysqli_stmt_execute($result);
echo "succesful";
exit();
}
}
}
}
}
}
else{
echo "Invalid Device!";
exit();
}
}
}
?>