#!/sw/local/bin/python # forsberg@lysator.liu.se 19981003 # My guestbook cgi script. # Variables imported from the form is # name The name filled in # email Email of the person # url Homepage of the visitor # howfound How the visitor found my webpage. # comment The long and positive comment ;) # Todo: Put the variable 'referer' somewhere. - no meaning # Fix the deletion of empty http strings. - done # E-mail the result to me.. - done import cgi, httplib, string, urlparse, socket, os, time, sys # Some variables.. # The guestbookowners name MYNAME="Erik Forsberg" # The guestbookowners e-mail address MYEMAIL="forsberg@lysator.liu.se" # Where should incoming submissions go ? MAILTO=MYEMAIL # Location of data-file DATAFILE="/lysator/www/user-pages/forsberg/guestbook-data.html" # Location of the resulting file VIEWURL="/~forsberg/guestbook.html" def standard_header(title): print "Content-type: text/html" print print "%s" % title def standard_footer(name, email): print '
' print '%(name)s' % vars() print "" def check_for_html_junk(words): string.strip(words) newstring=string.replace(words, '<', ' ') newstring=string.replace(newstring, '>', ' ') return newstring form=cgi.FieldStorage() # Store the variables got from the server if not form.has_key("referer"): standard_header("Please sign my Guestbook !") try: referer=os.environ['HTTP_REFERER'] except KeyError: referer = "unknown" print "

Please leave a message in my guestbook !

\n \ Fields with boldfat titles must be filled in.

\n \ Please read at least part of my pages first, then make a comment. Links to porn sites and similar will be removed \n \

\n \ \n \ Your Name ?

\n \ Your Email ?

\n \ Your Homepage ?

\ \n How did you find my page ?

\n \ Your comments about my page ?

\n \

\n \

\n \

Wan't to se what previous visitors said ? Click here" % (referer, VIEWURL) standard_footer(MYNAME, MYEMAIL) sys.exit(0) form_ok=0 if form.has_key("name") and form.has_key("comment"): if form["name"].value != "" and form["comment"].value != "": form_ok = 1 if not form_ok: standard_header("Error in form !") print "Please fill in at least your name and a comment." print "

" print "Use your browsers back button" standard_footer(MYNAME, MYEMAIL) form_ok=1 if form.has_key("url"): if form["url"].value != "http://": url=urlparse.urlparse(form["url"].value) try: urlcheck=httplib.HTTP(url[1]) urlcheck.putrequest('HEAD', url[2]) urlcheck.putheader('Accept', 'text/html') urlcheck.putheader('Accept', 'text/plain') urlcheck.endheaders() errcode = urlcheck.getreply() except socket.error: errcode = 1, '' if (errcode[0] == 302 | errcode[0] == 200) != 0: # 302 is 'temporary relocation' standard_header("Misspelled url ?") print "I tried to access the URL %s, but failed ! Please check if you spelled your URL correct." % form["url"].value if errcode[0] == 1: print "I couldn't even contact your server !" else: print "The error value received from your server was %s - %s" % (errcode[0], errcode[1]) print "

" print "(It could be as simple as a missing '/' character at the end of your URL)" print "

" print "Use your browsers back button to get back to your input form." standard_footer(MYNAME, MYEMAIL) sys.exit(0) if form.has_key("url"): form["url"].value=check_for_html_junk(form["url"].value) if form.has_key("howfound"): form["howfound"].value=check_for_html_junk(form["howfound"].value) if form.has_key("email"): form["email"].value=check_for_html_junk(form["email"].value) form["name"].value=check_for_html_junk(form["name"].value) form["comment"].value=check_for_html_junk(form["comment"].value) time=time.asctime(time.gmtime(time.time())) + ' GMT
' oldfile=open(DATAFILE, 'r+') oldbook=oldfile.read() oldfile.truncate(0) oldfile.seek(0) oldfile.write("\nDate: " + time) oldfile.write("\nName: " + form["name"].value + '
\n') if form.has_key("url"): if form["url"].value != "http://": oldfile.write("\nYour homepage: " + form["url"].value + "
\n") if form.has_key("email"): oldfile.write("\nE-mail: " + form["email"].value + "
\n") if form.has_key("howfound"): oldfile.write("\nHowto find this page ? " + form["howfound"].value + "
") oldfile.write("\nComment about the page:
" + form["comment"].value + "

") oldfile.write("\n\n


") oldfile.seek(-0, 2) oldfile.write(oldbook) oldfile.close() standard_header("The Guestbook") print "

Thanks for your submission, %s !

" % form["name"].value if form["referer"].value != "unknown": print "

Click here to return to my homepage

" % form["referer"].value print "Date: %s" % time print "Name: %s
\n" % form["name"].value if form.has_key("url"): if form["url"].value != "http://": print 'Your homepage: %s
\n' % (form["url"].value, form["url"].value) if form.has_key("email"): print "E-mail: " + form["email"].value + "
\n" if form.has_key("howfound"): print "Howto find this page ? %s
" % form["howfound"].value print "Comment about the page:
" + form["comment"].value + "

" print "


" print oldbook; standard_footer(MYNAME, MYEMAIL) # Now send me a mail with some interesting information.. import smtplib, socket mailsend = smtplib.SMTP('mailhost.lysator.liu.se') #myhostname=socket.gethostname() msg = "Subject: Guestbook robot at " + socket.gethostname() +" \n\n This is the guestbookrobot talking, I've got a submission for you..\n\nName: " + form["name"].value + "\nComment: " + form["comment"].value + "\n\nReferer: " + form["referer"].value if form.has_key("url"): if form["url"].value != "http://": msg = msg + "\nUrl: " + form["url"].value if form.has_key("howfound"): msg = msg + "\nHowfound: " + form["howfound"].value if form.has_key("email"): fromstr=form["email"].value else: fromstr="guestbookrobot@" + socket.gethostname() toaddr=string.splitfields(MAILTO, ' ') mailsend.sendmail(fromstr, toaddr, msg) mailsend.quit()