source: products/quintagroup.formlib.captcha/trunk/quintagroup/formlib/captcha/widget.py @ 3124

Last change on this file since 3124 was 3124, checked in by vmaksymiv, 13 years ago

pylint fixes

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