source: CMESS/captcha/trunk/com/zms/captcha/c_captcha_basic.py @ 438

Revision 438, 4.5 KB checked in by chris, 3 years ago (diff)
  • bug fixed if PIL is not available
Line 
1from com.zms.captcha.CaptchasDotNet import CaptchasDotNet
2try:
3        from com.zms.captcha.PILCaptcha import *
4        noPIL = False
5except:
6        noPIL = True
7from com.zms.captcha.reCaptcha import *
8
9class C_Captcha_Basic:
10        def __init__(self, zms_context):
11                self.id                                         = None
12               
13                self.zms_context        = zms_context
14                self.zms_lang                   = self.zms_context.REQUEST.get('lang', self.zms_context.getPrimaryLanguage())
15               
16        def GetContent(self):
17                return None
18       
19        def GetID(self):
20                return self.id
21       
22        def IsCodeValid(self, code):
23                return False
24       
25
26
27class C_Captcha_Dot_Net(C_Captcha_Basic):
28        def __init__(self, zms_context):
29                C_Captcha_Basic.__init__(self, zms_context)
30               
31                temp_client             = zms_context.attr_captcha.getObjProperty('captchaClient', zms_context.REQUEST)
32                temp_secret             = zms_context.attr_captcha.getObjProperty('captchaSecret', zms_context.REQUEST)
33               
34                temp_chars              = 'abcdefghkmnopqrstuvwxyz'
35                temp_chars              += (temp_client != 'demo') and temp_chars.upper() + '0123456789' or ''
36               
37                self.id                                         = 'captchas.net'
38                self.captcha                    = CaptchasDotNet(temp_client, temp_secret, alphabet = temp_chars)
39                               
40        def GetContent(self):
41                temp_result = '<input type="hidden" name="captcha_random" value="%s" />'%self.captcha.random()
42                temp_result += self.captcha.image() + '<br />'
43                temp_result += '<input type="submit" name="captcha_new" value="%s" /><br />'%self.zms_context.getLangStr('captchaNewCode', self.zms_lang)
44                temp_result += '<label>%s</label><br />'%self.zms_context.getLangStr('captchaLabel', self.zms_lang)
45                temp_result += '<input type="text" name="captcha_password" size="16" />'
46               
47                return temp_result
48       
49        def IsCodeValid(self):
50                #check random string
51                if (self.captcha.validate(self.zms_context.REQUEST.form.get('captcha_random', ''))):
52                        #check captcha password
53                        if (self.captcha.verify(self.zms_context.REQUEST.form.get('captcha_password', ''))):
54                                temp_result = (True, '')
55                        else:
56                                temp_result = (False, self.zms_context.getLangStr('captchaWrongVerification', self.zms_lang))
57                else:
58                        temp_result = (False, self.zms_context.getLangStr('captchaInvalid', self.zms_lang))
59                       
60                return temp_result
61
62class C_PIL_Captcha(C_Captcha_Basic):
63        def __init__(self, zms_context):
64                C_Captcha_Basic.__init__(self, zms_context)
65
66                self.id = 'PILCaptcha'
67       
68        def GetContent(self):
69                if noPIL:
70                        raise Exception
71
72                temp_img = generateImage(self.zms_context.temp_folder,
73                                                                                                                        self.zms_context.attr_captcha.getObjProperty('pilcaptchaCharCount', self.zms_context.REQUEST),
74                                                                                                                        self.zms_context.attr_captcha.getObjProperty('pilcaptchaFontSize', self.zms_context.REQUEST),
75                                                                                                                        self.zms_context.REQUEST)
76               
77                temp_result = '<img src="%s" />'%temp_img.absolute_url() + '<br />'
78                temp_result += '<input type="submit" name="captcha_new" value="%s" /><br />'%self.zms_context.getLangStr('captchaNewCode', self.zms_lang)
79                temp_result += '<label>%s</label><br />'%self.zms_context.getLangStr('captchaLabel', self.zms_lang)
80                temp_result += '<input type="text" name="captcha_password" size="16" />'
81               
82                return temp_result
83       
84        def IsCodeValid(self):
85                if noPIL:
86                        raise Exception
87       
88                if (self.zms_context.REQUEST.SESSION['sess_captcha'] == self.zms_context.REQUEST.form.get('captcha_password', '')):
89                        temp_result = (True, '')
90                else:
91                        temp_result = (False, self.zms_context.getLangStr('captchaWrongVerification', self.zms_lang))
92               
93                return temp_result
94
95class C_ReCaptcha(C_Captcha_Basic):
96        def __init__(self, zms_context):
97                C_Captcha_Basic.__init__(self, zms_context)
98               
99                self.id = 'reCaptcha'
100       
101        def GetContent(self):
102                temp_error = ''
103                temp_result = displayhtml(self.zms_context.attr_captcha.getObjProperty('recaptchaPublicKey', self.zms_context.REQUEST), True, temp_error)
104               
105                print(temp_error)
106               
107                return temp_result
108       
109        def IsCodeValid(self):
110                temp_result = False
111               
112                temp_response   = submit(self.zms_context.REQUEST.form.get('recaptcha_challenge_field', ''),
113                                                                                                                self.zms_context.REQUEST.form.get('recaptcha_response_field', ''),
114                                                                                                                self.zms_context.attr_captcha.getObjProperty('recaptchaPrivateKey', self.zms_context.REQUEST),
115                                                                                                                '127.0.0.1')
116               
117                if (temp_response.is_valid):
118                        temp_result = (True, '')
119                else:
120                        #incorrect-captcha-sol
121                        if (temp_response.error_code == 'incorrect-captcha-sol'):
122                                temp_result = (False, self.zms_context.getLangStr('captchaWrongVerification', self.zms_lang))
123                        #invalid-site-private-key
124                        else:
125                                temp_result = (False, self.zms_context.getLangStr('captchaInvalid', self.zms_lang))
126               
127                return temp_result
Note: See TracBrowser for help on using the repository browser.