diff options
Diffstat (limited to 'docker/tbp/visitlog/visitlog.sh')
-rw-r--r-- | docker/tbp/visitlog/visitlog.sh | 191 |
1 files changed, 191 insertions, 0 deletions
diff --git a/docker/tbp/visitlog/visitlog.sh b/docker/tbp/visitlog/visitlog.sh new file mode 100644 index 0000000..b169ee5 --- /dev/null +++ b/docker/tbp/visitlog/visitlog.sh @@ -0,0 +1,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 + |