|
|
@ -0,0 +1,194 @@ |
|
|
|
#!/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 |
|
|
|
|
|
|
|
|
|
|
|
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.as_string()) |
|
|
|
fp.close() |
|
|
|
except IOError: |
|
|
|
http = HttpResponse(url) |
|
|
|
http.sendError() |
|
|
|
exit() |
|
|
|
|
|
|
|
def sendMail(self): |
|
|
|
# send mail on strato powerweb |
|
|
|
call(["mail", self.buchung['receiver']], stdin=open(self.filename)) |
|
|
|
|
|
|
|
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 = MIMEText(self.text, 'plain', 'utf-8') |
|
|
|
self.msg['Subject'] = 'Buchungsanfrage von %s' % self.buchung['name'] |
|
|
|
self.msg['From'] = self.buchung['sender'] |
|
|
|
self.msg['To'] = self.buchung['receiver'] |
|
|
|
|
|
|
|
|
|
|
|
class HttpResponse: |
|
|
|
|
|
|
|
def __init__(self, url='https://www.meissner-wohnen.de'): |
|
|
|
self.url = url |
|
|
|
|
|
|
|
def sendRedirect(self): |
|
|
|
# redirect to buchung.html |
|
|
|
print 'Status: 301' |
|
|
|
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ückkehren</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, 8): |
|
|
|
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() |
|
|
|
msg.sendMail() |
|
|
|
|
|
|
|
if buchung['reply'] == 'true': |
|
|
|
receiver, buchung['sender'] = buchung['sender'], 'kontakt@meissner-wohnen.de' |
|
|
|
msg = Mail(buchung, receiver, True) |
|
|
|
msg.genMail() |
|
|
|
msg.genMailFile() |
|
|
|
msg.sendMail() |
|
|
|
|
|
|
|
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() |
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__': |
|
|
|
main() |