1 | from DateTime import DateTime |
---|
2 | |
---|
3 | try: |
---|
4 | from zope.site.hooks import getSite |
---|
5 | getSite() |
---|
6 | except ImportError: |
---|
7 | from zope.app.component.hooks import getSite |
---|
8 | from zope.app.form.browser import ASCIIWidget |
---|
9 | from zope.app.form.interfaces import ConversionError |
---|
10 | from zope.app.form.browser.widget import renderElement |
---|
11 | from zope.i18n import MessageFactory |
---|
12 | |
---|
13 | from Acquisition import aq_parent |
---|
14 | |
---|
15 | from Products.CMFCore.utils import getToolByName |
---|
16 | from Products.CMFCore.interfaces import ISiteRoot |
---|
17 | |
---|
18 | from quintagroup.captcha.core.utils import decrypt, parseKey, encrypt1, getWord |
---|
19 | |
---|
20 | _ = MessageFactory('quintagroup.formlib.captcha') |
---|
21 | |
---|
22 | import logging |
---|
23 | |
---|
24 | logger = logging.getLogger('quintagroup.formlib.captcha') |
---|
25 | |
---|
26 | |
---|
27 | class CaptchaWidget(ASCIIWidget): |
---|
28 | |
---|
29 | def get_site(self): |
---|
30 | # get from plone.app.form.widgets.wysiwygwdget |
---|
31 | site = getSite() |
---|
32 | while site is not None and not ISiteRoot.providedBy(site): |
---|
33 | site = aq_parent(site) |
---|
34 | return site |
---|
35 | |
---|
36 | def __call__(self): |
---|
37 | kwargs = {'type': self.type, |
---|
38 | 'name': self.name, |
---|
39 | 'id': self.name, |
---|
40 | 'cssClass': self.cssClass, |
---|
41 | 'style': self.style, |
---|
42 | 'size': self.displayWidth, |
---|
43 | 'extra': self.extra} |
---|
44 | |
---|
45 | site = self.get_site() |
---|
46 | portal_url = getToolByName(site, 'portal_url')() |
---|
47 | key = site.getCaptcha() |
---|
48 | |
---|
49 | if self._prefix: |
---|
50 | prefix = '%s.' % self._prefix |
---|
51 | else: |
---|
52 | prefix = '' |
---|
53 | |
---|
54 | return u"""<input type="hidden" value="%s" name="%shashkey" /> |
---|
55 | %s |
---|
56 | <img src="%s/getCaptchaImage/%s" |
---|
57 | alt="Enter the word"/>""" % (key, |
---|
58 | prefix, |
---|
59 | renderElement(self.tag, |
---|
60 | **kwargs), |
---|
61 | portal_url, |
---|
62 | key) |
---|
63 | |
---|
64 | def _toFieldValue(self, input): |
---|
65 | # Verify the user input against the captcha |
---|
66 | |
---|
67 | # get captcha type (static or dynamic) |
---|
68 | site = self.get_site() |
---|
69 | captcha_type = site.getCaptchaType() |
---|
70 | |
---|
71 | # validate captcha input |
---|
72 | if input and captcha_type in ['static', 'dynamic']: |
---|
73 | # make up form prefix |
---|
74 | if self._prefix: |
---|
75 | prefix = '%s.' % self._prefix |
---|
76 | else: |
---|
77 | prefix = '' |
---|
78 | |
---|
79 | hashkey = self.request.get('%shashkey' % prefix, '') |
---|
80 | decrypted_key = decrypt(site.captcha_key, hashkey) |
---|
81 | parsed_key = parseKey(decrypted_key) |
---|
82 | |
---|
83 | index = parsed_key['key'] |
---|
84 | date = parsed_key['date'] |
---|
85 | |
---|
86 | if captcha_type == 'static': |
---|
87 | img = getattr(site, '%s.jpg' % index) |
---|
88 | solution = img.title |
---|
89 | enc = encrypt1(input) |
---|
90 | else: |
---|
91 | enc = input |
---|
92 | solution = getWord(int(index)) |
---|
93 | |
---|
94 | captcha_tool = getToolByName(site, 'portal_captchas') |
---|
95 | if (enc != solution) or (decrypted_key in captcha_tool.keys()) or \ |
---|
96 | (DateTime().timeTime() - float(date) > 3600): |
---|
97 | raise ConversionError(_(u'Please re-enter validation code.')) |
---|
98 | else: |
---|
99 | captcha_tool.addExpiredKey(decrypted_key) |
---|
100 | |
---|
101 | return super(CaptchaWidget, self)._toFieldValue(input) |
---|