-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstrike
More file actions
executable file
·129 lines (118 loc) · 4.06 KB
/
strike
File metadata and controls
executable file
·129 lines (118 loc) · 4.06 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
#!/usr/bin/perl
# Ubuntu /etc/init/tty1.conf
# exec /sbin/getty -i -n -8 38400 -l /usr/local/bin/strike.pl tty1
# TO DO: Accept multiple prox readers based on a prox prefix
# Different curl IP for each different prox prefix
$|=1;
use strict;
use warnings;
use Term::ReadKey;
use Date::Manip;
use CGI qw/:standard/;
use DBI;
if ( $ENV{GATEWAY_INTERFACE} ) {
print header, start_html({
-title => "Cogstone Door",
-style=>[
{-type=>"text/css", -code=>q!buzz {color:red;}!},
],
-script=>[
{-type=>"text/javascript", -src=>"http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"},
{-type=>"text/javascript", -code=>q!
$(document).ready(function(){
$("#ofd").css("color", "blue").mousedown(function(){
$(this).css("color", "red");
$.get("http://webrelay.cogstonestl.com/state.xml?relayState=1");
}).mouseup(function(){
$(this).css("color", "blue");
$.get("http://webrelay.cogstonestl.com/state.xml?relayState=0");
});
});
!},
],
}, 'Door Access!');
if ( my $pin = param('pin') ) {
chomp($_ = ((grep { /\b$pin$/ } getusers('/etc/strike/users'))[0]) || $pin);
#$ENV{NOBUZZ} = 1;
Strike($_);
viewlog(1);
} elsif ( param('frontdoorlog') ) {
viewlog(param('frontdoorlog'));
} else {
print h1(a({-href=>"?frontdoorlog=20"}, 'Door Access!'));
if ( $ENV{REMOTE_ADDR} =~ /^172\.16\.254\.\d{1,3}$/ ) {
print div({-id=>"ofd"}, 'Front Door Buzzer (click and hold)');
print br,br,br,a({-href=>"http://webrelay.cogstonestl.com/state.xml?relayState=2"}, 'Or click here to pulse the door (smart phone users)');
} else {
print start_form;
print 'For whom? ', textfield('note'), br, 'PIN: ', password_field('pin'), br, submit('frontdoor', 'Open Front Door');
print end_form;
}
print end_html;
}
} else {
if ( $ENV{PIN} ) {
chomp($_ = ((grep { /$ENV{PIN}$/ } getusers('/etc/strike/users'))[0]) || $ENV{PIN});
} else {
#print "Striking...\n";
#my $rfid = $ARGV[1] or die "No strike\n";
print "Strike: ";
ReadMode 2;
chomp($ENV{RFID} = <STDIN>) or die "No strike\n";
ReadMode 0;
chomp($_ = $ENV{RFID} =~ /^PRX/i ? ((grep { /\t$ENV{RFID}\b/i } getusers('/etc/strike/users'))[0]) || $ENV{RFID} : $ENV{RFID});
}
Strike($_);
}
sub Strike {
local $_ = shift;
my ($name, $ok, $label, $rfid, $pin);
if ( /^(.*?)\s+([210])\t(\d+)\t([^\t]+)\t?(\d{4}?)/ ) {
($name, $ok, $label, $rfid, $pin) = ($1, $2, $3, $4, $5);
} else {
#($name, $ok) = ($ENV{RFID}||$ENV{PIN}||param('pin'), -1);
($name, $ok) = ("Wrong PIN ($ENV{RFID})", -1);
}
Date_Init('WorkDayBeg = 07:00', 'WorkDayEnd = 18:00');
local $_ = join "\t", grep { $_ } scalar localtime, $name.(param('note')?' ('.param('note').')':''), $ENV{REMOTE_ADDR};
print unless $ENV{GATEWAY_INTERFACE};
my $log = 1;
open DATA, ">>/var/log/strike.log" or $log = 0;
if ( $ENV{PIN} && $pin && $ENV{PIN} ne $pin ) {
print DATA "Wrong PIN ($ENV{RFID}): $_\n" if $log;
} elsif ( $ok == 1 || ($ok == 2 && Date_IsWorkDay(ParseDate('now'), 1)) ) { # 8a-5p
# use LWP
if ( $ENV{GATEWAY_INTERFACE} ) {
qx{curl http://webrelay.cogstonestl.com/state.xml?relayState=2&pulseTime=10 2>/dev/null} unless $ENV{NOBUZZ};
} else {
qx{curl http://webrelay.cogstonestl.com/state.xml?relayState=2 2>/dev/null} unless $ENV{NOBUZZ};
}
# Error codes?
print DATA "Allow: $_\n" if $log;
my $dbh = DBI->connect('DBI:mysql:database=timeclock;host=mysql.cogstonestl.com', 'timeclock', 'timeclock');
$dbh->do(qq[insert into timeclock (id, username, type, timein, timeout, approve, paid) select null, username, 'door', now(), null, null, null from usermap where name=?], undef, $name);
} elsif ( $ok == 2 ) {
print DATA "Deny After Hours: $_\n" if $log;
} elsif ( $ok == 0 ) {
print DATA "Deny: $_\n" if $log;
} else {
print DATA "Deny Unknown Attempt: $_\n" if $log;
}
close DATA if $log;
}
sub getusers {
# use MySQL
open USERS, $_[0] or return ();
my @users= <USERS>;
close USERS;
return @users;
}
sub viewlog {
open LOG, '/var/log/strike.log';
my @log = <LOG>;
close LOG;
my $start = $#log-$_[0]+1;
$start = 0 if $start < 0;
print "Last $_[0] entries", br, br;
print join br, @log[$start..$#log];
}