| 1 | |
1 | |
| 2 | import os |
2 | import os |
| 3 | import re |
3 | import re |
|
|
4 | import sha |
|
|
5 | import hmac |
|
|
6 | import StringIO |
|
|
7 | from random import Random |
|
|
8 | |
|
|
9 | import Image |
|
|
10 | import ImageDraw |
|
|
11 | import ImageFont |
|
|
12 | import ImageFilter |
|
|
13 | import ImageColor |
| 4 | |
14 | |
| 5 | from harmonious import harm |
15 | from harmonious import harm |
| 6 | from harmonious.utils import html_escape |
16 | from harmonious.utils import html_escape |
| 7 | |
17 | |
| 8 | from glsr.setup import config |
18 | from glsr.setup import config |
| … | |
… | |
| 14 | @harm.default |
24 | @harm.default |
| 15 | @harm.exposed |
25 | @harm.exposed |
| 16 | def register(self): |
26 | def register(self): |
| 17 | |
27 | |
| 18 | self._do_header() |
28 | self._do_header() |
|
|
29 | self._template.param('GLSR_URL', config.url) |
|
|
30 | self._template.param('GLSR_REPORT', '') |
| 19 | self._template.compile(os.path.join(config.template_loc, 'register.tpl')) |
31 | self._template.compile(os.path.join(config.template_loc, 'register.tpl')) |
| 20 | harm.response.append_body(self._template.output()) |
32 | harm.response.append_body(self._template.output()) |
| 21 | self._do_footer() |
33 | self._do_footer() |
| 22 | |
34 | |
| 23 | @harm.exposed |
35 | @harm.exposed |
| … | |
… | |
| 36 | fullname = '' |
48 | fullname = '' |
| 37 | email = '' |
49 | email = '' |
| 38 | language = '' |
50 | language = '' |
| 39 | location = '' |
51 | location = '' |
| 40 | website = '' |
52 | website = '' |
|
|
53 | catpcha = '' |
| 41 | |
54 | |
| 42 | # Alias |
55 | # Alias |
| 43 | if harm.request.forms.has_key('alias'): |
56 | if harm.request.forms.has_key('alias'): |
| 44 | |
57 | |
| 45 | alias = harm.request.forms.getvalue('alias') |
58 | alias = harm.request.forms.getvalue('alias') |
| … | |
… | |
| 173 | |
186 | |
| 174 | if len(website) > 0: |
187 | if len(website) > 0: |
| 175 | |
188 | |
| 176 | if not website_expr.match(website): |
189 | if not website_expr.match(website): |
| 177 | |
190 | |
| 178 | self.__failed_register('Value of \'websitw\' contained illegal characters.') |
191 | self.__failed_register('Value of \'website\' contained illegal characters.') |
|
|
192 | return |
|
|
193 | |
|
|
194 | # Captcha |
|
|
195 | if harm.request.forms.has_key('captcha'): |
|
|
196 | |
|
|
197 | captcha = harm.request.forms.getvalue('captcha') |
|
|
198 | |
|
|
199 | if captcha != self.__gen_captcha_code(): |
|
|
200 | |
|
|
201 | self.__failed_register('Your authentication code is incorrect.') |
| 179 | return |
202 | return |
| 180 | |
203 | |
| 181 | # Check password |
204 | # Check password |
| 182 | if not harm.request.forms.has_key('password2'): |
205 | if not harm.request.forms.has_key('password2'): |
| 183 | |
206 | |
| 184 | self.__failed_register('Your verification password does not match.') |
207 | self.__failed_register('Your verification password does not match.') |
| … | |
… | |
| 210 | |
233 | |
| 211 | self.__failed_register("The email address '%s' has already been registered by another user." % email) |
234 | self.__failed_register("The email address '%s' has already been registered by another user." % email) |
| 212 | return |
235 | return |
| 213 | |
236 | |
| 214 | create_user(alias=alias, email=email, passwd=passwd, fullname=fullname, |
237 | create_user(alias=alias, email=email, passwd=passwd, fullname=fullname, |
| 215 | language=language, location=location, website=website, type=0) |
238 | language=language, location=location, website=website, user_type=0) |
| 216 | |
239 | |
| 217 | # Replace this with a confirmation page |
240 | # Replace this with a confirmation page |
| 218 | harm.response.status = 303 |
241 | harm.response.status = 303 |
| 219 | harm.response.add_header('Location', os.path.join(config.url, 'index.py?page=main')) |
242 | harm.response.add_header('Location', os.path.join(config.url, 'index.py?page=main')) |
|
|
243 | |
|
|
244 | def get_captcha(self): |
|
|
245 | |
|
|
246 | rand = Random() |
|
|
247 | colour = ImageColor.getcolor('rgb(255,255,255)', 'RGB') |
|
|
248 | im = Image.new('RGB', (200, 80), colour) |
|
|
249 | font = ImageFont.truetype(os.path.join(config.template_loc, 'verdana.ttf'), 40) |
|
|
250 | draw = ImageDraw.Draw(im) |
|
|
251 | y_pos = 15 |
|
|
252 | code = self.__gen_captcha_code() |
|
|
253 | |
|
|
254 | # Draw each character |
|
|
255 | for i in range(0, 6): |
|
|
256 | |
|
|
257 | (char_width, char_height) = font.getsize(code[i]) |
|
|
258 | # Pick a random X co-ord |
|
|
259 | x_pos = rand.randint(0, 30) |
|
|
260 | draw.text((y_pos, x_pos), code[i], font=font, fill=(0,0,0)) |
|
|
261 | y_pos = y_pos + 30 |
|
|
262 | |
|
|
263 | # Blur the text |
|
|
264 | im = im.filter(ImageFilter.BLUR) |
|
|
265 | |
|
|
266 | image_data = StringIO.StringIO() |
|
|
267 | im.save(image_data, 'JPEG') |
|
|
268 | image_data.seek(0) |
|
|
269 | |
|
|
270 | harm.response.append_body(image_data.read()) |
|
|
271 | harm.response.add_header('Content-Type', 'image/jpeg') |
|
|
272 | |
|
|
273 | def __gen_captcha_code(self): |
|
|
274 | |
|
|
275 | return hmac.new(harm.config.server['SecretKey'], harm.request.session.get_id(), \ |
|
|
276 | sha).hexdigest()[:6] |
| 220 | |
277 | |
| 221 | def __failed_register(self, msg): |
278 | def __failed_register(self, msg): |
| 222 | |
279 | |
| 223 | self._do_header() |
280 | self._do_header() |
| 224 | self._template.param('GLSR_REPORT', msg) |
281 | self._template.param('GLSR_REPORT', msg) |