Left: | ||
Right: |
OLD | NEW |
---|---|
(Empty) | |
1 # This file is part of the Adblock Plus web scripts, | |
2 # Copyright (C) 2006-2016 Eyeo GmbH | |
3 # | |
4 # Adblock Plus is free software: you can redistribute it and/or modify | |
5 # it under the terms of the GNU General Public License version 3 as | |
6 # published by the Free Software Foundation. | |
7 # | |
8 # Adblock Plus is distributed in the hope that it will be useful, | |
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of | |
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
11 # GNU General Public License for more details. | |
12 # | |
13 # You should have received a copy of the GNU General Public License | |
14 # along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. | |
15 | |
16 import re | |
17 import datetime | |
18 import collections | |
19 from urlparse import parse_qsl | |
20 from sitescripts.utils import get_config, sendMail, setupStderr | |
21 from sitescripts.web import registerUrlHandler | |
22 | |
23 | |
24 class BadRequestError(Exception): | |
25 pass | |
26 | |
27 | |
28 class ConfDict(collections.OrderedDict): | |
29 __slots__ = ('value',) | |
30 | |
31 | |
32 def get_config_items(): | |
33 config = get_config() | |
34 default_keys = set(config.defaults()) | |
35 for name, value in config.items('formmail2'): | |
36 if name not in default_keys: | |
37 yield name, value | |
38 | |
39 | |
40 def store_value(conf_dict, path, value): | |
41 head, tail = path[0], path[1:] | |
42 if head not in conf_dict: | |
43 conf_dict[head] = ConfDict() | |
44 if tail: | |
45 store_value(conf_dict[head], tail, value) | |
46 else: | |
47 conf_dict[head].value = value | |
48 | |
49 | |
50 def conf_parse(conf_items): | |
51 conf_dict = ConfDict() | |
52 for key, value in conf_items: | |
53 path = key.split('.') | |
54 store_value(conf_dict, path, value) | |
55 return conf_dict | |
56 | |
57 | |
58 def post_handler(handler): | |
59 def wrapped_handler(environ, start_response): | |
60 setupStderr(environ['wsgi.errors']) | |
61 response_headers = [('Content-Type', 'text/plain; charset=utf-8')] | |
62 | |
63 try: | |
64 request_method = environ['REQUEST_METHOD'].upper() | |
65 url_encoded = 'application/x-www-form-urlencoded' | |
66 is_url_encoded = environ.get( | |
67 'CONTENT_TYPE', '').startswith(url_encoded) | |
68 if request_method != 'POST' or not is_url_encoded: | |
69 raise BadRequestError('Unsupported request method') | |
70 try: | |
71 request_body_length = int(environ['CONTENT_LENGTH']) | |
72 except: | |
73 raise BadRequestError( | |
74 'Invalid or missing Content-Length header') | |
75 request_body = environ['wsgi.input'].read(request_body_length) | |
76 params = {} | |
77 for key, value in parse_qsl(request_body): | |
78 params[key] = value.decode('utf-8').strip() | |
79 | |
80 response = handler(params) | |
81 except BadRequestError as error: | |
82 start_response('400 Bad Request'+str(error), response_headers) | |
Vasily Kuznetsov
2016/09/12 11:33:20
There should be spaces around that '+' (PEP8). Dun
| |
83 return str(error) | |
84 start_response('200 OK', response_headers) | |
85 return response | |
86 return wrapped_handler | |
87 | |
88 | |
89 def make_handler(name, config): | |
90 try: | |
91 url = config['url'].value | |
92 except (KeyError, AttributeError): | |
93 raise Exception('No URL configured for form handler:' + name) | |
94 try: | |
95 template = config['template'].value | |
96 except (KeyError, AttributeError): | |
97 raise Exception('No template configured for form handler:' + name) | |
98 try: | |
99 fields = config['fields'] | |
100 for field, spec in fields.items(): | |
101 spec.value = set(spec.value.replace(' ', '').split(',')) | |
102 except KeyError: | |
103 raise Exception('No fields configured for form handler:' + name) | |
104 if len(fields) == 0: | |
105 raise Exception('No fields configured for form handler:' + name) | |
106 | |
107 @post_handler | |
108 def handler(params): | |
109 email_regex = r'^\w[\w.+!-]+@\w[\w.-]+\.[a-zA-Z]{2,6}$' | |
110 errors = [] | |
111 for field, spec in fields.items(): | |
112 if 'mandatory' in spec.value: | |
113 if field not in params.keys() or not params[field]: | |
114 if 'mandatory' in spec: | |
115 errors.append(spec['mandatory'].value) | |
116 else: | |
117 errors.append('No {} entered'.format(field)) | |
118 if 'email' in spec.value and 'email' in params.keys(): | |
119 if not re.search(email_regex, params['email']): | |
120 if 'mandatory' in spec: | |
Vasily Kuznetsov
2016/09/12 11:33:20
This should be `if 'email' in spec:`, etc.
Also,
| |
121 errors.append(spec['mandatory'].value) | |
122 else: | |
123 errors.append('Invalid email address') | |
124 if errors: | |
125 raise BadRequestError('\n'.join(errors)) | |
126 | |
127 params['time'] = datetime.datetime.now() | |
128 sendMail(template, {'fields': params}) | |
129 return '' | |
130 return url, handler | |
131 | |
132 | |
133 conf_dict = conf_parse(get_config_items()) | |
134 for name, config in conf_dict.items(): | |
135 url, handler = make_handler(name, config) | |
136 registerUrlHandler(url, handler) | |
OLD | NEW |