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

Last change on this file since 2806 was 2799, checked in by mylan, 14 years ago

#195: Fixed context getting in the widget

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