#!/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 <

Please select your location

Downtown Campus
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 <

The Bike Project Visitor Log

${location} Shop

${datestring}

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("
Hello %s %s! Your membership is valid until %s.
", first, lastI, goodtil); printf("%d,%s,%s,%s,%s,%s\n",now,cn,intid,first" "lastI".",vt,loc) >> vl exit 0 } else { printf("
Hello %s %s. your membership expired on %s. Please see a staff member to renew.
", 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 <You have used your one free visit. If you would like to use the shop, please contact a staff member to become a member
EOF else /bin/cat <Welcome to The Bike Project. Please enjoy your free trial visit. 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 <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". EOF fi # Display visitors and form /bin/cat <
Sign in

I am

EOF if [ -z $cardno ] && [ $visittype = "volunteer" ] then /bin/cat <Visiting
Volunteering EOF else /bin/cat <Visiting
Volunteering EOF fi /bin/cat <
EOF /usr/bin/tac $visitorLog | /usr/bin/awk -F, -v to=$t0 -v loc=$location '($1>to && $6==loc) { printf("\n", strftime("%r", $1), $4,$5) }' /bin/cat < EOF
Time Name Visit Type
%s%s%s