Web page of meissner-wohnen.de
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

204 lines
6.6 KiB

#!/usr/bin/python
# -*- coding: utf-8 -*-
import cgi
import cgitb
import os
from email.mime.text import MIMEText
from subprocess import call
from datetime import datetime
from fnmatch import fnmatch
from string import Template
import json
def escapeTags(text):
text = text.replace('<', '')
text = text.replace('>', '')
return text
def checkEntries(buchung):
mailstring = '*@*'
datestring = '[0-9][0-9].[0-9][0-9].[0-9][0-9][0-9][0-9]'
entries = {'name': False, 'sender': False, 'dates': False}
if len(buchung['name']) > 0:
entries['name'] = True
if fnmatch(buchung['sender'], mailstring):
entries['sender'] = True
try:
if (fnmatch(buchung['begin'], datestring) and fnmatch(buchung['end'], datestring) and
datetime.strptime(buchung['begin'], '%d.%m.%Y') >= datetime.now() and
datetime.strptime(buchung['end'], '%d.%m.%Y') > datetime.strptime(buchung['begin'], '%d.%m.%Y')):
entries['dates'] = True
except ValueError:
pass
return entries
class Mail:
def __init__(self, buchung, receiver, reply=False):
self.buchung = buchung
self.buchung['receiver'] = receiver
self.reply = reply
def genMailFile(self):
# write email to file
try:
self.filename = "../data/email_" + datetime.now().strftime('%d-%m-%Y_%H-%M-%S-%f') + ".txt"
fp = open(self.filename, 'wb')
fp.write(self.msg)
fp.close()
except IOError:
http = HttpResponse(url)
http.sendError()
exit()
def sendMail(self):
with open(os.devnull, 'wb') as DEVNULL:
# send mail on strato powerweb
#call(["mail", self.buchung['receiver']], stdin=open(self.filename))
# what's with content type: '-a', "Content-Type: text/plain; charset=utf-8"
self.ret = call(["mail", '-s', "Buchungsanfrage von " + self.buchung['name'], \
'-r', self.buchung['sender'], self.buchung['receiver']], \
stdin=open(self.filename), stdout=DEVNULL)
return self.ret
def genMail(self):
# replace evil html tags
self.buchung['msg'] = escapeTags(self.buchung['msg'])
# gen mail text
if self.reply is True:
self.text = ("Buchungsanfrage:\n\nName: " + self.buchung['name'] +
"\nEmail: " + self.buchung['receiver'] + "\nTelefon: " + self.buchung['tel'] +
"\nAnreise: " + self.buchung['begin'] + "\nAbreise: " + self.buchung['end'] +
"\nPersonen: " + self.buchung['persons'] + "\n\nNachricht:\n" + self.buchung['msg'])
else:
self.text = ("Buchungsanfrage:\n\nName: " + self.buchung['name'] +
"\nEmail: " + self.buchung['sender'] + "\nTelefon: " + self.buchung['tel'] +
"\nAnreise: " + self.buchung['begin'] + "\nAbreise: " + self.buchung['end'] +
"\nPersonen: " + self.buchung['persons'] + "\n\nNachricht:\n" + self.buchung['msg'])
# gen mail
self.msg = self.text
class HttpResponse:
def __init__(self, url='https://www.meissner-wohnen.de'):
self.url = url
def sendRedirect(self):
# redirect to given url
print 'Location: https://www.meissner-wohnen.de/%s' % self.url
print ''
def sendError(self):
print "Content-type: text/html\n"
print "<p>Uuups, da ist ein Fehler aufgetreten. Bitte zur <a href='https://www.meissner-wohnen.de'>Startseite</a> zur&uumlckkehren</p>"
print "<p>Falls Sie eine Buchungsanfrage stellen m&oumlchten, bitte per Mail an: <a href='mailto:kontakt@meissner-wohnen.de'>kontakt@meissner-wohnen.de</a></p>"
def main():
#cgitb.enable()
# some values
buchung = {}
blub = ["name", "sender", "tel", "begin", "end", "persons", "msg", "reply"]
template_values = {'name_warning': '', 'email_warning': '', 'date_warning': '', 'name': '', 'email': '', 'tel': '', 'msg': '', 'kopie': ''}
url = 'buchung'
receiver = 'kontakt@meissner-wohnen.de'
# get cgi object
form = cgi.FieldStorage()
# bot detection
if form.getvalue('email'):
# logging
now = datetime.now().strftime('%d.%m.%Y %H:%M:%S')
f = open("../data/meissner_wohnen.log", "ab")
f.write(now + " : " + cgi.escape(os.environ["REMOTE_ADDR"]) + " : bot detected\n")
f.close()
# gen and send http response
http = HttpResponse(url + '.html')
http.sendRedirect()
exit()
# get form values if exist
for index in range(0, len(blub)):
if form.getvalue(str(index)):
buchung[blub[index]] = form.getvalue(str(index))
else:
buchung[blub[index]] = ""
# check for valid entries
test = checkEntries(buchung)
# generate mails if entries are valid
if (test['name'] and test['sender'] and test['dates']):
msg = Mail(buchung, receiver)
msg.genMail()
msg.genMailFile()
if msg.sendMail() != 0:
http = HttpResponse(url)
http.sendError()
exit()
if buchung['reply'] == 'true':
receiver, buchung['sender'] = buchung['sender'], 'kontakt@meissner-wohnen.de'
msg = Mail(buchung, receiver, True)
msg.genMail()
msg.genMailFile()
if msg.sendMail() != 0:
http = HttpResponse(url)
http.sendError()
exit()
url = url + '_erfolgreich.html'
# generate error site from template if entries are invalid
else:
if not test['name']:
template_values['name_warning'] = 'warning'
if not test['sender']:
template_values['email_warning'] = 'warning'
if not test['dates']:
template_values['date_warning'] = 'warning'
url = url + '_fehler.html'
# read html template
try:
f = open("../data/buchung.template", "rb")
html_template = Template(f.read())
f.close()
except IOError:
http = HttpResponse(url)
http.sendError()
exit()
# write templated html file
try:
f = open('../meissner-wohnen/%s' % url, "wb")
f.write(html_template.safe_substitute(template_values))
f.close()
except IOError:
http = HttpResponse(url)
http.sendError()
exit()
# gen and send http response
http = HttpResponse(url)
http.sendRedirect()
exit()
if __name__ == '__main__':
main()