#!/bin/sh
#
# CGI Script to process input from a form and then e-mail it to the webmaster
#
# Copyright Ben Tasker 2009
# Released under the GNU GPL
#
# See Http://benscomputer.no-ip.org/LICENSE for more information





echo Content-type: text/html
echo ""
CAPTCHAROOT="/path/to/captchas/"

# Process the Users Message

/bin/env > /tmp/logfile.log
URID=$( cat /tmp/logfile.log | grep REQUEST_URI )

# Get the Captcha Session ID
SESSIONID=$( echo "$URID" | sed -n 's/^.*Session=\([^&]*\).*$/\1/p' | sed "s/&/[AMPERSAND]/g" )

# Get the Captcha's actual meaning
CAPTCHA=$( cat /tmp/captcha.$SESSIONID )

# Move the Captcha back out of the publishing folder

rm -f "$CAPTCHAROOT"/"$SESSIONID"".jpg"

# Got the value, tidy up
rm -f /tmp/captcha.$SESSIONID

# See what the user entered
Ucaptcha=$( echo "$URID" | sed -n 's/^.*captcha=\([^&]*\).*$/\1/p' | sed "s/%20/ /g" )


# See if they match
if [ "$CAPTCHA" == "$Ucaptcha" ]
then
# User entered the Captcha Correctly, continue

NAME=$( echo "$URID" | sed -n 's/^.*username=\([^&]*\).*$/\1/p' | sed "s/%20/ /g" )
EMAIL=$( echo "$URID" | sed -n 's/^.*email=\([^&]*\).*$/\1/p' | sed "s/%40/@/g" )
REASON=$( echo "$URID" | sed -n 's/^.*Reason=\([^&]*\).*$/\1/p' | sed "s/+/ /g" | sed "s/%21/!/g" )
COMMENTS=$( echo "$URID" | sed -n 's/^.*COMMENTS=\([^&]*\).*$/\1/p' | sed "s/+/ /g" | sed "s/%0D%0A/<br>/g" )


# Arggg Ran out of coffee! Apologies for quality of anything that follows


# Reason Currently contains characters to keep the shell happy. Not nice to read, so lets convert them

REASONPRINT=$( echo "$REASON" | sed "s/_/ /g" | sed "s/Praise/Praise!/g" )

# Now lets make a few changes so the message stays readable, but doesn't pose a danger to the system

COMMENTSPRINT=$( echo "$COMMENTS" | sed "s/%21/!/g" | sed "s/%26/[AMPERSAND]/g" )
COMMENTSPRINT2=$( echo "$COMMENTSPRINT" | sed "s/%3B/[SEMICOLON]/g" | sed "s/%22/[DOUBLEQUOTES]/g" )
COMMENTSPRINT=$( echo "$COMMENTSPRINT2" | sed "s/%2F/[FWD SLASH]/g" | sed "s/%5C/[BK SLASH]/g" )
COMMENTSPRINT2=$( echo "$COMMENTSPRINT" | sed "s/rm/[RM]/g" | sed "s/cat/[CAT]/g" )
COMMENTSPRINT=$( echo "$COMMENTSPRINT2" | sed "s/cp/[CP]/g" | sed "s/mv/[MV]/g" )
COMMENTSPRINT2=$( echo "$COMMENTSPRINT" | sed "s/%24/[DOLLAR]/g" | sed "s/%A3/[GBP]/g" )
COMMENTSPRINT=$( echo "$COMMENTSPRINT2" | sed "s/%27/[SINGLE QUOTE]/g" | sed "s/%23/[HASH]/g" )
COMMENTSPRINT2=$( echo "$COMMENTSPRINT" | sed "s/%3A/[DOUBLE DOTS]/g" | sed "s/%5B/[OPEN SQ BRACKETS]/g" )
COMMENTSPRINT=$( echo "$COMMENTSPRINT2" | sed "s/%40/@/g" | sed "s/%5D/[CLOSE SQ BRACKETS]/g" )
COMMENTSPRINT2=$( echo "$COMMENTSPRINT" | sed "s/%25/[PERCENT]/g" | sed "s/%5E/[\^]/g" )
COMMENTSPRINT=$( echo "$COMMENTSPRINT2" | sed "s/%28/(/g" | sed "s/%29/)/g" | sed "s/%2B/+/g" | sed "s/%2C/,/g" )

# Now we have the message, we want to record it and confirm the message has been sent

# Generate a Message ID for reference purposes
# Use the Date as well
DATE=$( date +'%Y%m%d%T' )
MESSAGED=$( echo "$NAME""$EMAIL""$REASON""$DATE" | sha1sum )
# We don't actually need the hyphen at the end though
MESSAGEID=$( echo "$MESSAGED" | sed "s/-//g" | sed "s/ //g" )

# Send a Message notifying us that there is a new message

# Get Auth details, file locked so only root and apache can view it

SENDER=$( cat /mnt/.apacheroot/SENDER.txt )
PASS=$( cat /mnt/.apacheroot/par.txt )
SUBJECT="Benscomputer.no-ip.org - New Message $MESSAGEID"
MESSAGE0="<html>"
MESSAGE1="You have a New Message from the Contact Form on Benscomputer.no-ip.org with Message ID: $MESSAGEID"
MESSAGE2="<br>The message was submitted with the reason: $REASONPRINT <br>"
MESSAGE3=""
MESSAGE4="<br><br>Message was sent by $NAME "
MESSAGE5="<br><br>Message Reads:<br><br>"
MESSAGE6="$COMMENTSPRINT <br><br>Captcha entered by the user was $CAPTCHA"
MESSAGE="</html>"
MESSAGE=$( echo $MESSAGE0 ; echo $MESSAGE1 ; echo $MESSAGE2 ; echo $MESSAGE3; echo $MESSAGE4; echo $MESSAGE5; echo $MESSAGE6 ; echo $MESSAGE7 )
/usr/local/bin/sendEmail -f Sender@email.com -u "$SUBJECT" -m "$MESSAGE" -s SMTP:PORT -xu $SENDER -xp $PASS -t target@email.com -o tls=auto > /dev/null




# Display to the user

/bin/cat << EOM
<HTML>
<HEAD><TITLE>Message: $REASONPRINT - ID: $MESSAGEID</TITLE>
</HEAD>
<BODY bgcolor="#cccccc" text="#000000">
<P>
<SMALL>
<PRE>
<b> Thank You</b><br><br>
Your Message has successfully been sent. Please see details below including your message ID.<br><br>
Where possible and necessary, we will endeavour to reply as quickly as possible. Please keep a note of your 
message ID.<br><br>
<a href="SERVERS FQDN">Return to Home Page</a>
<br><br>
<b><i>Message Details</b></i><br><br>
Message ID: $MESSAGEID <br>
Name: $NAME <br>
Email: $EMAIL <br>
Reason: $REASONPRINT <br>
Message: $COMMENTSPRINT <br>
<br><br>
EOM


cat << EOM
</PRE>
</SMALL>
<P>
</BODY>
</HTML>
EOM


else
# User entered Captcha Incorrectly
/bin/cat << EOM
<HTML>
<HEAD><TITLE>ERROR: Incorrect Captcha</TITLE>
</HEAD>
<BODY bgcolor="#cccccc" text="#000000">
<P>
<SMALL>
<PRE>
<font color="red"><b> Error!!!!</b></font><br><br>

Your Message has <font color="red"><b>NOT</b></font> been sent. You incorrectly Entered the Captcha Details, 
please click <a 
href="http://PATH BACK TO FORM">here</a> to 
re-enter your message, and the correct Captcha<br><br>
<br>Your message details are below so that you can Copy and Paste your message.<br><br>
<a href="http://benscomputer.no-ip.org">Return to Home Page</a>
<br><br>
Your Message would have looked like this is if had been sent
<b><i>Message Details</b></i><br><br>
Name: $NAME <br>
Email: $EMAIL <br>
Reason: $REASONPRINT <br>
Message: $COMMENTSPRINT <br>
<br><br>
EOM


cat << EOM
</PRE>
</SMALL>
<P>
</BODY>
</HTML>
EOM



fi
