OLD | NEW |
1 # coding: utf-8 | 1 # coding: utf-8 |
2 | 2 |
3 # This file is part of the Adblock Plus web scripts, | 3 # This file is part of the Adblock Plus web scripts, |
4 # Copyright (C) 2006-2015 Eyeo GmbH | 4 # Copyright (C) 2006-2015 Eyeo GmbH |
5 # | 5 # |
6 # Adblock Plus is free software: you can redistribute it and/or modify | 6 # Adblock Plus is free software: you can redistribute it and/or modify |
7 # it under the terms of the GNU General Public License version 3 as | 7 # it under the terms of the GNU General Public License version 3 as |
8 # published by the Free Software Foundation. | 8 # published by the Free Software Foundation. |
9 # | 9 # |
10 # Adblock Plus is distributed in the hope that it will be useful, | 10 # Adblock Plus is distributed in the hope that it will be useful, |
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of | 11 # but WITHOUT ANY WARRANTY; without even the implied warranty of |
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | 12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
13 # GNU General Public License for more details. | 13 # GNU General Public License for more details. |
14 # | 14 # |
15 # You should have received a copy of the GNU General Public License | 15 # You should have received a copy of the GNU General Public License |
16 # along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. | 16 # along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. |
17 | 17 |
18 import re | 18 import re |
19 from urlparse import parse_qsl | |
20 from sitescripts.utils import get_config, sendMail, setupStderr | 19 from sitescripts.utils import get_config, sendMail, setupStderr |
21 from sitescripts.web import url_handler | 20 from sitescripts.web import url_handler, form_handler |
22 | 21 |
23 @url_handler('/formmail') | 22 @url_handler('/formmail') |
24 def handleRequest(environ, start_response): | 23 @form_handler |
| 24 def handleRequest(environ, start_response, data): |
25 setupStderr(environ['wsgi.errors']) | 25 setupStderr(environ['wsgi.errors']) |
26 | 26 |
27 start_response('200 OK', [('Content-Type', 'text/plain; charset=utf-8')]) | 27 params = {} |
28 if environ['REQUEST_METHOD'].upper() != 'POST' or not environ.get('CONTENT_TYP
E', '').startswith('application/x-www-form-urlencoded'): | 28 for field in ('name', 'email', 'subject', 'message'): |
29 return 'Unsupported request method' | 29 value = data.get(field, '').strip() |
30 | 30 |
31 try: | 31 if not value: |
32 request_body_length = int(environ['CONTENT_LENGTH']) | 32 start_response('400 Bad request') |
33 except: | 33 return ['No "%s" given' % field] |
34 return 'Invalid or missing Content-Length header' | |
35 | 34 |
36 request_body = environ['wsgi.input'].read(request_body_length) | 35 params[field] = value |
37 params = {} | |
38 for key, value in parse_qsl(request_body): | |
39 params[key] = value.decode('utf-8').strip() | |
40 | |
41 if not 'name' in params or params['name'] == '': | |
42 return 'No name entered' | |
43 if not 'email' in params or params['email'] == '': | |
44 return 'No email address entered' | |
45 if not 'subject' in params or params['subject'] == '': | |
46 return 'No subject entered' | |
47 if not 'message' in params or params['message'] == '': | |
48 return 'No message entered' | |
49 | 36 |
50 if not re.match(r'^\w[\w.+!-]+@\w[\w.-]+\.[a-zA-Z]{2,6}$', params['email']): | 37 if not re.match(r'^\w[\w.+!-]+@\w[\w.-]+\.[a-zA-Z]{2,6}$', params['email']): |
51 return 'Invalid email address' | 38 start_response('400 Bad request') |
| 39 return ['Invalid email address'] |
52 | 40 |
53 sendMail(get_config().get('formmail', 'template'), params) | 41 sendMail(get_config().get('formmail', 'template'), params) |
54 return 'Message sent' | 42 |
| 43 start_response('200 OK') |
| 44 return ['Message sent'] |
OLD | NEW |