-
Notifications
You must be signed in to change notification settings - Fork 6
/
getdata.php
213 lines (197 loc) · 10.8 KB
/
getdata.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
<?php
//Connect to database
require 'connectDB.php';
//date_default_timezone_set('Asia/Damascus');
date_default_timezone_set('Asia/Ho_Chi_Minh');
$d = date("Y-m-d");
$t = date("H:i:s");
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'];
$sql = "SELECT * FROM users_logs WHERE card_uid=? AND checkindate=? AND scores=0";
$result = mysqli_stmt_init($conn);
if (!mysqli_stmt_prepare($result, $sql)) {
echo "SQL_Error_Select_logs";
exit();
} else {
mysqli_stmt_bind_param($result, "ss", $card_uid, $d);
mysqli_stmt_execute($result);
$resultl = mysqli_stmt_get_result($result);
//*****************************************************
//Login
if (!$row = mysqli_fetch_assoc($resultl)) {
$sql = "INSERT INTO users_logs (username, serialnumber, card_uid, device_uid, device_dep, checkindate, timein, timeout) VALUES (? ,?, ?, ?, ?, ?, ?, ?)";
$result = mysqli_stmt_init($conn);
if (!mysqli_stmt_prepare($result, $sql)) {
echo "SQL_Error_Select_login1";
exit();
} else {
$timeout = "00:00:00";
mysqli_stmt_bind_param($result, "sdssssss", $Uname, $Number, $card_uid, $device_uid, $device_dep, $d, $t, $timeout);
mysqli_stmt_execute($result);
echo "login" . $Uname;
exit();
}
}
//*****************************************************
//Logout
else {
// Get data user_logs last record
$sql = mysqli_query($conn, "SELECT timein
FROM users_logs
WHERE username='$Uname'
ORDER BY id DESC");
$row = mysqli_fetch_array($sql, MYSQLI_ASSOC);
$timein = $row['timein'];
$dateTimeIn = date_create($timein);
$dateTimeOut = date_create($t);
// Compare date time
$difference = date_diff($dateTimeIn, $dateTimeOut);
$difference->h;
$minutes = $difference->days * 24 * 60;
$minutes += $difference->h * 60;
$minutes += $difference->i;
if ($minutes >= 30)
{
$sql = "UPDATE users_logs SET timeout=?,scores =1 WHERE card_uid=? AND checkindate=? AND scores=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;
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, add_card) VALUES (?, 1, ?, ?, CURDATE(),1)";
$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();
}
}
}