-
Notifications
You must be signed in to change notification settings - Fork 0
/
checkpresence.php
67 lines (54 loc) · 1.5 KB
/
checkpresence.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
<?php
require_once __DIR__."/settings.php";
define("VERBOSE", in_array('-v', $argv) ? true : false);
//define("VERBOSE", true);
function exitWithMessage($msg, $state="0") {
if(VERBOSE) {
fwrite(STDERR, "$msg\n");
}
die((string) $state);
}
function dateIsAboveThreshold(DateTime $time, $threshold=0) {
$now = new DateTime();
if ($threshold) {
$thresholdTime = clone($time);
$prefix = $threshold > 0 ? "+" : "";
$thresholdTime->modify($prefix . $threshold . " seconds");
return $thresholdTime > $now;
}
else {
return false;
}
}
if (!file_exists(DATA_FILE)) {
exitWithMessage("Data file does not exist.");
}
$data = json_decode(file_get_contents(DATA_FILE));
if (!$data) {
exitWithMessage("Empty JSON data.");
}
// If we have no enter data, assume absence
if (empty($data->enter) || empty($data->enter->date)) {
exitWithMessage("No enter data.");
}
$lastEnter = new DateTime($data->enter->date);
// If last enter was pushed into the future by the threshold, assume absence
if (dateIsAboveThreshold($lastEnter, ENTER_THRESHOLD)) {
exitWithMessage("Last enter is in the future.");
}
// If we have no exit data, assume presence
if (empty($data->exit) || empty($data->exit->date)) {
die("1");
}
$lastExit = new DateTime($data->exit->date);
// If last exit is in the future, assume presence
if (dateIsAboveThreshold($lastExit, EXIT_THRESHOLD)) {
exitWithMessage("Last exit is in the future.", "1");
}
// Check if last message was enter or exit
if ($lastExit > $lastEnter) {
die("0");
}
else {
die("1");
}