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
|
#!/usr/bin/env bash
# visitlog.sh
# Martin Miller
# Created: 2017/09/04
# A CGI script for the bike project visitors log
. /usr/local/bin/bashlib
memberFile=/var/www/visitlog/members.csv
visitorLog=/var/www/visitlog/visits.csv
firstVisitLog=/var/www/visitlog/firsttime.txt
# Determine if we know the location
location=`cookie location`
locationp=`param location`
if [ -z ${location} ]
then
if [ -z ${locationp} ]
then
# Ask for location
/bin/echo "Content-Type: text/html; charset=utf-8"
/bin/echo ""
/bin/cat <<EOF
<body>
<h1>Please select your location</h1>
<form method="post" autocomplete="off">
Downtown<input type="radio" name="location" value="downtown" checked>
Campus<input type="radio" name="location" value="campus" ><br />
<input type="submit" value="Submit">
</form>
EOF
exit
else
/bin/echo "Set-Cookie: location=${locationp}"
location=${locationp}
fi
fi
/bin/echo "Content-Type: text/html; charset=utf-8"
/bin/echo ""
d0=$(/bin/date +%Y-%m-%d)
datestring=$(/bin/date +"%B %d, %Y")
t0=$(/bin/date --date="$d0" +%s)
/bin/cat <<EOF
<html>
<head>
<link rel="stylesheet" type="text/css" href="style.css" />
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
</head>
<body>
<div class="container-fluid">
<div class="col-xs-12 col-sm-6">
<h1>The Bike Project Visitor Log</h1>
</div>
<div class="col-xs-12 col-sm-6">
<h3><span class="glyphicon glyphicon-map-marker" aria-hidden="true"></span> ${location} Shop</h3>
<h3><span class="glyphicon glyphicon-calendar" aria-hidden="true"></span> ${datestring}</h3>
</div>
<div class="clearfix"></div>
EOF
# Check for sign in
cardno=`param cardno`
visittype=`param visittype`
if [ ${#cardno} -ge 4 ]
then
# check if cardno is valid member
awk -F, -v cn=$cardno -v vl=$visitorLog -v loc=$location -v vt=$visittype '
BEGIN {
intid=0;
found=0;
valid=0;
goodtil=0;
now=systime();
first="";
lastI=""
}
# The second field of the member file stores the external constituent ID
# surrounded by double quotes.
($2=="\""cn"\"") {
# The eighth field stores the membership expiration in YYYY-MM-DD,
# convert to YYYY MM DD
found=1
gsub("\"","",$1)
intid=$1
gsub("\"","",$3)
first=$3
gsub("\"","",$4)
lastI=substr($4,1,3)
gsub("\"","",$8)
goodtil=$8;
gsub("-"," ",$8)
exptime=mktime($8" 23 59 59")
if (exptime>now) {
valid=1
}
}
END {
if (found==0) {
exit 1
}
if (valid==1) {
gsub("\"","",$1);
printf("<div class=\"alert alert-success\">Hello %s %s! Your membership is valid until %s.</div>", first, lastI, goodtil);
printf("%d,%s,%s,%s,%s,%s\n",now,cn,intid,first" "lastI".",vt,loc) >> vl
exit 0
} else {
printf("<div class=\"alert alert-danger\">Hello %s %s. your membership expired on %s. Please see a staff member to renew.</div>", first, lastI, goodtil);
exit 2
}
}' $memberFile
fi
if [ $? -eq 1 ] # User not found in member database
then
hashed=$(echo $cardno | sha256sum | cut -d' ' -f1)
grep "${hashed}" $firstVisitLog > /dev/null
if [ $? -eq 0 ]
then
/bin/cat <<EOF
<div class="alert alert-danger">You have used your one free visit. If you would like to
use the shop, please contact a staff member to become a member</div>
EOF
else
/bin/cat <<EOF
<div class="alert alert-warning">Welcome to The Bike Project. Please enjoy your free trial
visit.</div>
EOF
echo ${hashed} >> $firstVisitLog
now=$(date +"%s")
echo ${now},-1,-1,First Visit,visitor,$location >> $visitorLog
fi
fi
if [ -z $cardno ] && [ $visittype = "volunteer" ]
then
/bin/cat <<EOF
<div class="alert alert-warning">I didn't quite catch that. Could you scan your
card again? In the future, you can avoid this message by clicking the text box
after you click "Volunteering".</div>
EOF
fi
# Display visitors and form
/bin/cat <<EOF
<div class="row">
<div class="col-xs-12 col-sm-4">
<div class="panel panel-default">
<div class="panel-heading">Sign in</div>
<div class="panel-body">
<form method="post" autocomplete="off">
<div class="form-group">
<p class="col-xs-3">I am</p>
EOF
if [ -z $cardno ] && [ $visittype = "volunteer" ]
then
/bin/cat <<EOF
<input type="radio" name="visittype" value="visitor" >Visiting<br>
<input type="radio" name="visittype" value="volunteer" checked>Volunteering
EOF
else
/bin/cat <<EOF
<input type="radio" name="visittype" value="visitor" checked>Visiting<br>
<input type="radio" name="visittype" value="volunteer" >Volunteering
EOF
fi
/bin/cat <<EOF
<input type="text" class="form-control" name="cardno" autofocus=autofocus>
</div>
<button type="submit" class="btn btn-primary">Submit</button>
</form>
</div>
</div>
</div>
<div class="col-xs-12 col-sm-8">
<table class="table table-striped">
<tr>
<th>Time</th>
<th>Name</th>
<th>Visit Type</th>
</tr>
EOF
/usr/bin/tac $visitorLog | /usr/bin/awk -F, -v to=$t0 -v loc=$location '($1>to && $6==loc) {
printf("<tr><td>%s</td><td>%s</td><td>%s</td></tr>\n", strftime("%r", $1),
$4,$5)
}'
/bin/cat <<EOF
</table>
</div>
</body>
</html>
EOF
|