#! /usr/bin/python

# Guestbook is copyright (c) 2001 of Programmed Integration
# You may use this code in any way you see fit, but please
# let us know where it is being used. If you make any changes to
# the source code, please send us a copy so we can incorporate
# the changes in future releases. If you have any other comments
# please feel free to contact us at support@programmedintegration.com

# Guestbook version 1.0.0 26 February 2001

import sys
#sys.path.append('/lib/python1.5')         # My ISP requires this to correctly locate Python Modules
import string, re, cgi, os

def ObsceneLanguage(usestring):
   usestring=string.upper(usestring)
   obscene="blast","sugar"                # You may add all the words here you do not wish to be included on your guestbook
   for useword in obscene:
      if string.find(usestring, string.upper(useword))<>-1:
         return (1)
   return (0)


usebody=open("guestbook.txt", "r")        # Open main guestbook textfile
usehead=open("guestbook-head.htm", "r")   # Open guestbook HTML header
usefoot=open("guestbook-foot.htm", "r")   # Open guestbook HTML footer


if os.environ["QUERY_STRING"] == "post":  # Performs the guestbook POST
   useform=cgi.FieldStorage()              
   try:
      realname= useform["REALNAME"].value
   except:
      realname=""
   try:
      email= useform["EMAIL"].value         
   except:
      email=""
   try:
      location=useform["LOCATION"].value
   except:
      location=""
   try:
      comments=useform["COMMENTS"].value
   except:
      comments=""
   comments=string.replace(comments, "\n", "<BR>");
   print "Content-type: text/html"
   print                                   
   print usehead.read()
   
   if realname=="":                       
      print "You have not entered a name/nickname" 
      print usefoot.read()
      sys.exit(0)                          
   elif comments=="":
      print "You have not entered a comment"
      print usefoot.read()
      sys.exit(0)

   if (ObsceneLanguage(realname)==1) or (ObsceneLanguage(email)==1) or (ObsceneLanguage(email)==1) or (ObsceneLanguage(location)==1) or (ObsceneLanguage(comments)==1): 
      print "You have entered something that would seem offensive.  Please press the back button to edit your text and resubmit."
      print usefoot.read()
      sys.exit(0)
   #n=now.now()
   
   memBody=usebody.readlines()
   
   memBody.insert(0, realname+"||"+email+"||"+location+"||"+comments+"||"+str(n.day)+"/"+str(n.month)+"/"+str(n.year)+"\n")
   usebody=open("guestbook.txt", "w")   
   usebody.writelines(memBody)     
   usebody.close                       
   print "Many thanks for your guest book submission"
   print "<A HREF=\"guestbook.py?view\">Click here</A> to view the guest book" 
else:
   print "Content-type: text/html"
   try:
      print
      print usehead.read()
      bodyuse=usebody.readlines()
      for useline in bodyuse:
         usesplit=string.split(useline, '||')
         print
         print "<CENTER><TABLE width=\"100%\">"
         print "<TR><TD align=right width=80><FONT SIZE=\"-1\"><B>Name:</B></TD></FONT><TD><FONT SIZE=\"-1\"><A HREF=\"mailto:"+usesplit[1]+"\">"+usesplit[0]+"</A></FONT></TD></TR>"
         print "<TR><TD align=right width=80><FONT SIZE=\"-1\"><B>Date:</B></TD></FONT><TD><FONT SIZE=\"-1\">"+usesplit[4]+"</FONT></TD></TR>"
         print "<TR><TD align=right width=80><FONT SIZE=\"-1\"><B>Location:</B></FONT></TD><TD><FONT SIZE=\"-1\">"+usesplit[2]+"</FONT></TD></TR>"
         print "<TR><TD ALIGN=RIGHT width=80><FONT SIZE=\"-1\"><B>Comments:</B></FONT></TD><TD BGCOLOR=\"#FFFFCC\"><FONT SIZE=\"-1\">"+usesplit[3]+"</FONT></TD></TR>"
         print "</TABLE></CENTER>"
         print "<HR><P>"
      print usefoot.read()
      usehead.close();
      usefoot.close();
   except:
      print "SORRY AN ERROR HAS OCCURRED"


