source: products/quintagroup.formlib.captcha/branches/captchas-refactoring/quintagroup/formlib/captcha/widget.py @ 2058

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

#173: Move quintagroup.formlib.captcha.form content into the quintagroup.formlib.captcha, add zope.app.form as direct requirement

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