1 | ## Script (Python) "getCaptchaImage" |
---|
2 | ##bind container=container |
---|
3 | ##bind context=context |
---|
4 | ##bind namespace= |
---|
5 | ##bind script=script |
---|
6 | ##bind subpath=traverse_subpath |
---|
7 | ##parameters= |
---|
8 | ##title= |
---|
9 | from Products.qPloneCaptchas.utils import gen_captcha, decrypt, \ |
---|
10 | getWord, parseKey |
---|
11 | from Products.CMFCore.utils import getToolByName |
---|
12 | import random |
---|
13 | propTool = getToolByName(context, 'portal_properties') |
---|
14 | captchaProps = propTool['qPloneCaptchas'] |
---|
15 | |
---|
16 | hk = context.REQUEST.traverse_subpath[0] |
---|
17 | dk = decrypt(context.captcha_key, hk) |
---|
18 | key = parseKey(dk)['key'] |
---|
19 | |
---|
20 | text = getWord(int(key)) |
---|
21 | size = captchaProps.getProperty('image_size') |
---|
22 | bkground = captchaProps.getProperty('background') |
---|
23 | font_color = captchaProps.getProperty('font_color') |
---|
24 | kwargs = {'text': text, |
---|
25 | 'size': size, |
---|
26 | 'bkground': bkground, |
---|
27 | 'font_color': font_color} |
---|
28 | if captchaProps.getProperty('random_params', 'False'): |
---|
29 | period = random.uniform(0.05, 0.12) |
---|
30 | amplitude = random.uniform(3.0, 6.5) |
---|
31 | else: |
---|
32 | period = captchaProps.getProperty('period') |
---|
33 | amplitude = captchaProps.getProperty('amplitude') |
---|
34 | |
---|
35 | kwargs['distortion'] = [period, amplitude, (0.0, 0.0)] |
---|
36 | |
---|
37 | im = gen_captcha(**kwargs) |
---|
38 | context.REQUEST.RESPONSE.setHeader('Content-Type', 'image/jpeg') |
---|
39 | context.REQUEST.RESPONSE.setHeader('Content-Length', im['size']) |
---|
40 | context.REQUEST.RESPONSE.setHeader('Accept-Ranges', 'bytes') |
---|
41 | return im['src'] |
---|