#!/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 "
\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 \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
" print "