|
@@ -0,0 +1,194 @@
|
|
1
|
+#!/usr/bin/python
|
|
2
|
+# -*- coding: utf-8 -*-
|
|
3
|
+
|
|
4
|
+
|
|
5
|
+import cgi
|
|
6
|
+import cgitb
|
|
7
|
+import os
|
|
8
|
+from email.mime.text import MIMEText
|
|
9
|
+from subprocess import call
|
|
10
|
+from datetime import datetime
|
|
11
|
+from fnmatch import fnmatch
|
|
12
|
+from string import Template
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+def escapeTags(text):
|
|
16
|
+ text = text.replace('<', '')
|
|
17
|
+ text = text.replace('>', '')
|
|
18
|
+ return text
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+def checkEntries(buchung):
|
|
22
|
+
|
|
23
|
+ mailstring = '*@*'
|
|
24
|
+ datestring = '[0-9][0-9].[0-9][0-9].[0-9][0-9][0-9][0-9]'
|
|
25
|
+ entries = {'name': False, 'sender': False, 'dates': False}
|
|
26
|
+
|
|
27
|
+ if len(buchung['name']) > 0:
|
|
28
|
+ entries['name'] = True
|
|
29
|
+
|
|
30
|
+ if fnmatch(buchung['sender'], mailstring):
|
|
31
|
+ entries['sender'] = True
|
|
32
|
+
|
|
33
|
+ try:
|
|
34
|
+ if (fnmatch(buchung['begin'], datestring) and fnmatch(buchung['end'], datestring) and
|
|
35
|
+ datetime.strptime(buchung['begin'], '%d.%m.%Y') >= datetime.now() and
|
|
36
|
+ datetime.strptime(buchung['end'], '%d.%m.%Y') > datetime.strptime(buchung['begin'], '%d.%m.%Y')):
|
|
37
|
+ entries['dates'] = True
|
|
38
|
+ except ValueError:
|
|
39
|
+ pass
|
|
40
|
+
|
|
41
|
+ return entries
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+class Mail:
|
|
45
|
+
|
|
46
|
+ def __init__(self, buchung, receiver, reply=False):
|
|
47
|
+ self.buchung = buchung
|
|
48
|
+ self.buchung['receiver'] = receiver
|
|
49
|
+ self.reply = reply
|
|
50
|
+
|
|
51
|
+ def genMailFile(self):
|
|
52
|
+ # write email to file
|
|
53
|
+ try:
|
|
54
|
+ self.filename = "../data/email_" + datetime.now().strftime('%d-%m-%Y_%H-%M-%S-%f') + ".txt"
|
|
55
|
+ fp = open(self.filename, 'wb')
|
|
56
|
+ fp.write(self.msg.as_string())
|
|
57
|
+ fp.close()
|
|
58
|
+ except IOError:
|
|
59
|
+ http = HttpResponse(url)
|
|
60
|
+ http.sendError()
|
|
61
|
+ exit()
|
|
62
|
+
|
|
63
|
+ def sendMail(self):
|
|
64
|
+ # send mail on strato powerweb
|
|
65
|
+ call(["mail", self.buchung['receiver']], stdin=open(self.filename))
|
|
66
|
+
|
|
67
|
+ def genMail(self):
|
|
68
|
+ # replace evil html tags
|
|
69
|
+ self.buchung['msg'] = escapeTags(self.buchung['msg'])
|
|
70
|
+ # gen mail text
|
|
71
|
+ if self.reply is True:
|
|
72
|
+ self.text = ("Buchungsanfrage:\n\nName: " + self.buchung['name'] +
|
|
73
|
+ "\nEmail: " + self.buchung['receiver'] + "\nTelefon: " + self.buchung['tel'] +
|
|
74
|
+ "\nAnreise: " + self.buchung['begin'] + "\nAbreise: " + self.buchung['end'] +
|
|
75
|
+ "\nPersonen: " + self.buchung['persons'] + "\n\nNachricht:\n" + self.buchung['msg'])
|
|
76
|
+ else:
|
|
77
|
+ self.text = ("Buchungsanfrage:\n\nName: " + self.buchung['name'] +
|
|
78
|
+ "\nEmail: " + self.buchung['sender'] + "\nTelefon: " + self.buchung['tel'] +
|
|
79
|
+ "\nAnreise: " + self.buchung['begin'] + "\nAbreise: " + self.buchung['end'] +
|
|
80
|
+ "\nPersonen: " + self.buchung['persons'] + "\n\nNachricht:\n" + self.buchung['msg'])
|
|
81
|
+ # gen mail
|
|
82
|
+ self.msg = MIMEText(self.text, 'plain', 'utf-8')
|
|
83
|
+ self.msg['Subject'] = 'Buchungsanfrage von %s' % self.buchung['name']
|
|
84
|
+ self.msg['From'] = self.buchung['sender']
|
|
85
|
+ self.msg['To'] = self.buchung['receiver']
|
|
86
|
+
|
|
87
|
+
|
|
88
|
+class HttpResponse:
|
|
89
|
+
|
|
90
|
+ def __init__(self, url='https://www.meissner-wohnen.de'):
|
|
91
|
+ self.url = url
|
|
92
|
+
|
|
93
|
+ def sendRedirect(self):
|
|
94
|
+ # redirect to buchung.html
|
|
95
|
+ print 'Status: 301'
|
|
96
|
+ print 'Location: https://www.meissner-wohnen.de/%s' % self.url
|
|
97
|
+ print ''
|
|
98
|
+
|
|
99
|
+ def sendError(self):
|
|
100
|
+ print "Content-type: text/html\n"
|
|
101
|
+ print "<p>Uuups, da ist ein Fehler aufgetreten. Bitte zur <a href='https://www.meissner-wohnen.de'>Startseite</a> zurückkehren</p>"
|
|
102
|
+
|
|
103
|
+
|
|
104
|
+def main():
|
|
105
|
+ #cgitb.enable()
|
|
106
|
+
|
|
107
|
+ # some values
|
|
108
|
+ buchung = {}
|
|
109
|
+ blub = ["name", "sender", "tel", "begin", "end", "persons", "msg", "reply"]
|
|
110
|
+ template_values = {'name_warning': '', 'email_warning': '', 'date_warning': '', 'name': '', 'email': '', 'tel': '', 'msg': '', 'kopie': ''}
|
|
111
|
+ url = 'buchung'
|
|
112
|
+ receiver = 'kontakt@meissner-wohnen.de'
|
|
113
|
+
|
|
114
|
+ # get cgi object
|
|
115
|
+ form = cgi.FieldStorage()
|
|
116
|
+
|
|
117
|
+ # bot detection
|
|
118
|
+ if form.getvalue('email'):
|
|
119
|
+ # logging
|
|
120
|
+ now = datetime.now().strftime('%d.%m.%Y %H:%M:%S')
|
|
121
|
+ f = open("../data/meissner_wohnen.log", "ab")
|
|
122
|
+ f.write(now + " : " + cgi.escape(os.environ["REMOTE_ADDR"]) + " : bot detected\n")
|
|
123
|
+ f.close()
|
|
124
|
+ # gen and send http response
|
|
125
|
+ http = HttpResponse(url + '.html')
|
|
126
|
+ http.sendRedirect()
|
|
127
|
+ exit()
|
|
128
|
+
|
|
129
|
+ # get form values if exist
|
|
130
|
+ for index in range(0, 8):
|
|
131
|
+ if form.getvalue(str(index)):
|
|
132
|
+ buchung[blub[index]] = form.getvalue(str(index))
|
|
133
|
+ else:
|
|
134
|
+ buchung[blub[index]] = ""
|
|
135
|
+
|
|
136
|
+ # check for valid entries
|
|
137
|
+ test = checkEntries(buchung)
|
|
138
|
+
|
|
139
|
+ # generate mails if entries are valid
|
|
140
|
+ if (test['name'] and test['sender'] and test['dates']):
|
|
141
|
+
|
|
142
|
+ msg = Mail(buchung, receiver)
|
|
143
|
+ msg.genMail()
|
|
144
|
+ msg.genMailFile()
|
|
145
|
+ msg.sendMail()
|
|
146
|
+
|
|
147
|
+ if buchung['reply'] == 'true':
|
|
148
|
+ receiver, buchung['sender'] = buchung['sender'], 'kontakt@meissner-wohnen.de'
|
|
149
|
+ msg = Mail(buchung, receiver, True)
|
|
150
|
+ msg.genMail()
|
|
151
|
+ msg.genMailFile()
|
|
152
|
+ msg.sendMail()
|
|
153
|
+
|
|
154
|
+ url = url + '_erfolgreich.html'
|
|
155
|
+
|
|
156
|
+ # generate error site from template if entries are invalid
|
|
157
|
+ else:
|
|
158
|
+
|
|
159
|
+ if not test['name']:
|
|
160
|
+ template_values['name_warning'] = 'warning'
|
|
161
|
+ if not test['sender']:
|
|
162
|
+ template_values['email_warning'] = 'warning'
|
|
163
|
+ if not test['dates']:
|
|
164
|
+ template_values['date_warning'] = 'warning'
|
|
165
|
+
|
|
166
|
+ url = url + '_fehler.html'
|
|
167
|
+
|
|
168
|
+ # read html template
|
|
169
|
+ try:
|
|
170
|
+ f = open("../data/buchung.template", "rb")
|
|
171
|
+ html_template = Template(f.read())
|
|
172
|
+ f.close()
|
|
173
|
+ except IOError:
|
|
174
|
+ http = HttpResponse(url)
|
|
175
|
+ http.sendError()
|
|
176
|
+ exit()
|
|
177
|
+
|
|
178
|
+ # write templated html file
|
|
179
|
+ try:
|
|
180
|
+ f = open('../meissner-wohnen/%s' % url, "wb")
|
|
181
|
+ f.write(html_template.safe_substitute(template_values))
|
|
182
|
+ f.close()
|
|
183
|
+ except IOError:
|
|
184
|
+ http = HttpResponse(url)
|
|
185
|
+ http.sendError()
|
|
186
|
+ exit()
|
|
187
|
+
|
|
188
|
+ # gen and send http response
|
|
189
|
+ http = HttpResponse(url)
|
|
190
|
+ http.sendRedirect()
|
|
191
|
+
|
|
192
|
+
|
|
193
|
+if __name__ == '__main__':
|
|
194
|
+ main()
|