-
Notifications
You must be signed in to change notification settings - Fork 0
/
reply.php
executable file
·246 lines (239 loc) · 16 KB
/
reply.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
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
<!DOCTYPE html>
<html>
<head><title>Response</title></head>
<body>
<h1>Response</h1>
<?php
require("secretSettings.php");
$raw = file_get_contents('php://input');
echo "Raw: " . $raw;
$contents = split(":", $raw);
//$id = trim(str_replace("}", "", $contents[1]));
$id = trim(substr($contents[1],1,-6)) ?: '0000';
if ($id == '0000'){
$contents = split("0=", $raw);
$id = trim($contents[1]) ?: '0000';
}
echo " ID: " .$id;
$file = fopen("logs/scannedID.txt","a+") or die("cant open/create file");
$logs = fopen("logs/ReplyLogs.txt","a+") or die("failed to open/create file");
//$outputFile = fopen("logs/output.txt","a+") or die ("cant create/open/write to output file");
//$signedLogs = fopen("logs/attendanceLogs.txt","a+") or die("cant open/create file");
fwrite($file,"------------------------\n");
fwrite($file,"ID: ".$id."\n");
fwrite($file,"raw: ".$raw."\n");
fwrite($file,"contents 1: ".$contents[1]."\n");
fwrite($file,"Time: ".date("Y-m-d\TH:i:s\Z", time())."\n");
fwrite($file,"------------------------");
fclose($file);
//$timeVar = date('Y-m-d H:i:s');
//$timeVar = CURRENT_TIMESTAMP;
//echo " Time: " . $timeVar;
try {
$conn = new PDO("mysql:host=$SERVERNAME;dbname=$DBNAME", $USERNAME, $PASSWORD);
// set the PDO error mode to exception
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
echo "\nConnected successfully\n";
//$sql = "INSERT INTO attendance (badgeID,timeScanned) VALUES ($id,$timeVar)"; //Dont forget to change column
//$sql = "INSERT INTO 'attendance' (badgeID,'time') VALUES ($id,$time)"; //Use this if you dont rename the column
$getMembers = $conn->prepare("SELECT * FROM Members");
//$getAttendance = $conn->prepare("SELECT * FROM attendance WHERE timeScanned > unix_timestamp() + 43200");
$getMembers->execute();
//$getAttendance->execute();
$data = $getMembers->fetchAll();
//$attendanceData = $getAttendance->fetchAll();
$found = false;
foreach($data as $person){
if ($person["Tag_ID"] == $id){
$found = true;
$lastScanTime = strtotime($person['Last_Time']);
echo "Last: ".$lastScanTime."\n";
$currentTime = strtotime(date('Y-m-d H:i:s'));
echo "Now: ".$currentTime."\n";
$difference = $currentTime - $lastScanTime;
if ($person["Signed_In"] % 2 == 1){
//Signing out
$pointsToAdd = round($difference/1800,2);
fwrite($logs,"------------------------------\n");
if ($difference > 900){
$conn->exec("UPDATE Members SET Num_Meetings = Num_Meetings + 1 WHERE Tag_ID = '$id'");
$name = $person['First_Name']." ".$person['Last_Name'];
$status = "Sign Out";
fwrite($logs,"Sign out: ID: ".$id." Name: ".$person['First_Name']." ".$person['Last_Name']."\n");
$sql = "INSERT INTO attendance (Status,Full_Name,badgeID) VALUES ('$status','$name','$id')";
$conn->exec($sql);
} else {
echo "User was not there for 15 minutes. Meeting not recorded. \n";
//fwrite($signedLogs,$person["First_Name"]." ".$person["Last_Name"]." signed out too quick.\n");
$name = $person['First_Name']." ".$person['Last_Name'];
$status = "Early Sign Out";
fwrite($logs,"Early Sign out: ID: ".$id." Name: ".$person['First_Name']." ".$person['Last_Name']."\n");
$sql = "INSERT INTO attendance (Status,Full_Name,badgeID) VALUES ('$status','$name','$id')";
$conn->exec($sql);
$pointsToAdd = 0;
}
if ($difference > 43200) {
echo "Difference is greater than 12 hours \n";
fwrite($logs,"Difference greater than 12 hrs: ID: ".$id." Name: ".$person['First_Name']." ".$person['Last_Name']."\n");
//fwrite($signedLogs,$person["First_Name"]." ".$person["Last_Name"]." did not sign out for over 12 hours and was not awarded points.\n");
$pointsToAdd = 0;
}
//$pointsToAdd = ((floatval($difference))/3600.0;
echo $person["First_Name"]." ".$person["Last_Name"]." successfully signed out. \n";
//fwrite($signedLogs,date('Y-m-d H:i:s')." ".$person["First_Name"]." ".$person["Last_Name"]." successfully signed out. Awarded ".$pointsToAdd." points!\n");
echo "Time Difference: ".gmdate("H:i:s", $difference)."\n";
if ($pointsToAdd > 0) {
$conn->exec("UPDATE Members SET Points = Points + '$pointsToAdd' WHERE Tag_ID = '$id'");
echo "Points awarded: ".$pointsToAdd."\n";
fwrite($logs,"Points Added: ".$pointsToAdd." ID: ".$id." Name: ".$person['First_Name']." ".$person['Last_Name']."\n");
//fwrite($signedLogs,$person['First_Name']." ".$person['Last_Name']." awarded ".$pointsToAdd."\n");
}
$conn->exec("UPDATE Members SET Signed_In = 0 WHERE Tag_ID = '$id'");
} else {
//Signing In
$name = $person['First_Name']." ".$person['Last_Name'];
$status = "Sign In";
$sql = "INSERT INTO attendance (Status,Full_Name,badgeID) VALUES ('$status','$name','$id')";
$conn->exec($sql);
echo $person["First_Name"]." ".$person["Last_Name"]." successfully signed in. \n";
//fwrite($signedLogs,date('Y-m-d H:i:s')." ".$person["First_Name"]." ".$person["Last_Name"]." successfully signed in. \n");
fwrite($logs,"Sign In: ID: ".$id." Name: ".$person['First_Name']." ".$person['Last_Name']." Time: ".date('Y-m-d H:i:s')."\n");
$conn->exec("UPDATE Members SET Signed_In = 1 WHERE Tag_ID = '$id'");
}
break;
} else {
//COMMANDS BELOW
$word = split("$COMMANDSPASSWORD",$raw);
if (sizeof($word) > 1){
$idSplit = split($person["Tag_ID"],trim($word[1]));
//$nameSplit = split($person["First_Name"],$trim($word[1]))
//echo "\n ID split: ".$testsplit[0]." command split: ".$testsplit[1];
//echo "\n".$person["Tag_ID"];
//echo "\n Possible Name: ".$nameSplit;
$customCommand = trim($idSplit[1]) ?: 'NOT CUSTOM';
if ($customCommand == "NOT CUSTOM"){
//echo "\nNot this person";
switch (trim($word[1])) {
case "sign all out": //SIGNS EVERYONE OUT
$tagID = $person["Tag_ID"];
$conn->exec("UPDATE Members SET Signed_In = 1 WHERE Tag_ID = '$tagID'");
$url = 'http://dhsrobotics.ddns.net/reply.php';
$data = array($person["Tag_ID"]);
// use key 'http' even if you send the request to https://...
$options = array(
'http' => array(
'header' => "Content-type: application/x-www-form-urlencoded\r\n",
'method' => 'POST',
'content' => http_build_query($data)
)
);
$context = stream_context_create($options);
$result = file_get_contents($url, false, $context);
if ($result === FALSE) { echo "\n ERROR: self-post failed"; }
var_dump($result);
break;
case "sign all in": //SIGNS EVERYONE IN
$tagID = $person["Tag_ID"];
$conn->exec("UPDATE Members SET Signed_In = 0 WHERE Tag_ID = '$tagID'");
$url = 'http://dhsrobotics.ddns.net/reply.php';
$data = array($person["Tag_ID"]);
// use key 'http' even if you send the request to https://...
$options = array(
'http' => array(
'header' => "Content-type: application/x-www-form-urlencoded\r\n",
'method' => 'POST',
'content' => http_build_query($data)
)
);
$context = stream_context_create($options);
$result = file_get_contents($url, false, $context);
if ($result === FALSE) { echo "\n ERROR: self-post failed"; }
var_dump($result);
break;
default:
//echo "\n Command not recognized: ".trim($word[1]);
break;
}
} else {
$pointAddSplit = split("change points by",$customCommand);
$meetingsAddSplit = split("change meetings by",$customCommand);
//echo "\n".$numSplit[0]." 1: ".$numSplit[1]." 2: ".$numSplit[2];
if (sizeof($pointAddSplit) > 1){
$pointsToAdd = doubleval($pointAddSplit[1]);
$tagID = $person["Tag_ID"];
$conn->exec("UPDATE Members SET Points = Points + '$pointsToAdd' WHERE Tag_ID = '$tagID'");
echo "\n Successfully changed points by ".$pointsToAdd." for ".$person["First_Name"]." ".$person["Last_Name"];
break;
} elseif (sizeof($meetingsAddSplit) > 1) {
$meetingsToAdd = doubleval($meetingsAddSplit[1]);
$tagID = $person["Tag_ID"];
$conn->exec("UPDATE Members SET Num_Meetings = Num_Meetings + '$meetingsToAdd' WHERE Tag_ID = '$tagID'");
echo "\n Successfully changed meetings attended by ".$meetingsToAdd." for ".$person["First_Name"]." ".$person["Last_Name"];
break;
} else {
switch($customCommand){
case "sign out":
$tagID = $person["Tag_ID"];
$conn->exec("UPDATE Members SET Signed_In = 1 WHERE Tag_ID = '$tagID'");
$url = 'http://dhsrobotics.ddns.net/reply.php';
$data = array($person["Tag_ID"]);
// use key 'http' even if you send the request to https://...
$options = array(
'http' => array(
'header' => "Content-type: application/x-www-form-urlencoded\r\n",
'method' => 'POST',
'content' => http_build_query($data)
)
);
$context = stream_context_create($options);
$result = file_get_contents($url, false, $context);
if ($result === FALSE) { echo "\n ERROR: self-post failed"; }
var_dump($result);
break;
case "sign in":
$tagID = $person["Tag_ID"];
$conn->exec("UPDATE Members SET Signed_In = 0 WHERE Tag_ID = '$tagID'");
$url = 'http://dhsrobotics.ddns.net/reply.php';
$data = array($person["Tag_ID"]);
// use key 'http' even if you send the request to https://...
$options = array(
'http' => array(
'header' => "Content-type: application/x-www-form-urlencoded\r\n",
'method' => 'POST',
'content' => http_build_query($data)
)
);
$context = stream_context_create($options);
$result = file_get_contents($url, false, $context);
if ($result === FALSE) { echo "\n ERROR: self-post failed"; }
var_dump($result);
break;
default:
break;
}
break;
}
}
}
}
}
echo("\nSuccessfully Updated\n");
}
catch(PDOException $e)
{
echo "\nConnection aborted: " . $e->getMessage();
exit;
}
if ($found) {
echo "\n User was successfully logged. \n";
} else {
$name = "UNKNOWN";
$conn->exec("INSERT INTO attendance (Full_Name,badgeID) VALUES ('$name','$id')");
}
//fclose($signedLogs);
fwrite($logs,"\n");
fclose($logs);
?>
<h3>Cron job test</h3>
</body>
</html>