Changeset 419
- Timestamp:
- 07/24/06 09:03:12
- Files:
-
- qPloneCaptchas/trunk/Extensions/Install.py (modified) (5 diffs)
- qPloneCaptchas/trunk/README.txt (modified) (2 diffs)
- qPloneCaptchas/trunk/__init__.py (modified) (1 diff)
- qPloneCaptchas/trunk/data (added)
- qPloneCaptchas/trunk/data/__init__.py (added)
- qPloneCaptchas/trunk/data/__init__.pyc (added)
- qPloneCaptchas/trunk/data/basic_english.py (added)
- qPloneCaptchas/trunk/data/basic_english.pyc (added)
- qPloneCaptchas/trunk/data/fonts (added)
- qPloneCaptchas/trunk/data/fonts/vera (added)
- qPloneCaptchas/trunk/data/fonts/vera/Vera.ttf (added)
- qPloneCaptchas/trunk/data/fonts/vera/VeraBI.ttf (added)
- qPloneCaptchas/trunk/data/fonts/vera/VeraBd.ttf (added)
- qPloneCaptchas/trunk/data/fonts/vera/VeraIt.ttf (added)
- qPloneCaptchas/trunk/data/fonts/vera/VeraMoBI.ttf (added)
- qPloneCaptchas/trunk/data/fonts/vera/VeraMoBd.ttf (added)
- qPloneCaptchas/trunk/data/fonts/vera/VeraMoIt.ttf (added)
- qPloneCaptchas/trunk/data/fonts/vera/VeraMono.ttf (added)
- qPloneCaptchas/trunk/data/fonts/vera/VeraSe.ttf (added)
- qPloneCaptchas/trunk/data/fonts/vera/VeraSeBd.ttf (added)
- qPloneCaptchas/trunk/skins/captchas_qcomments/2.5 (added)
- qPloneCaptchas/trunk/skins/captchas_qcomments/2.5/discussion_reply.cpy (added)
- qPloneCaptchas/trunk/skins/captchas_qcomments/2.5/discussion_reply.cpy.metadata (added)
- qPloneCaptchas/trunk/skins/captchas_qcomments/2.5/discussion_reply_form.cpt (added)
- qPloneCaptchas/trunk/skins/captchas_qcomments/2.5/discussion_reply_form.cpt.metadata (added)
- qPloneCaptchas/trunk/skins/plone_captchas/captcha_validator.vpy (modified) (2 diffs)
- qPloneCaptchas/trunk/skins/plone_captchas/captcha_widget.pt (modified) (1 diff)
- qPloneCaptchas/trunk/skins/plone_captchas/getCaptcha.py (modified) (1 diff)
- qPloneCaptchas/trunk/skins/plone_captchas/getCaptchaImage.py (modified) (1 diff)
- qPloneCaptchas/trunk/skins/plone_captchas/prefs_captchas_setup.cpy (added)
- qPloneCaptchas/trunk/skins/plone_captchas/prefs_captchas_setup.cpy.metadata (added)
- qPloneCaptchas/trunk/skins/plone_captchas/prefs_captchas_setup_form.cpt (added)
- qPloneCaptchas/trunk/skins/plone_captchas/prefs_captchas_setup_form.cpt.metadata (added)
- qPloneCaptchas/trunk/skins/plone_captchas/validate_setup.vpy (added)
- qPloneCaptchas/trunk/utils.py (modified) (1 diff)
- qPloneCaptchas/trunk/version.txt (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
qPloneCaptchas/trunk/Extensions/Install.py
r418 r419 6 6 from App.Common import package_home 7 7 from os.path import exists as path_exists, join as path_join 8 from Products.CMFCore.CMFCorePermissions import ManagePortal 8 9 9 10 10 11 def install(self): 11 12 out=StringIO() 13 updateqPloneCaptchasPropertySheet(self, out) 12 14 Layers = LAYERS 13 15 mtool = getToolByName(self, 'portal_migration') … … 17 19 elif plone_version.startswith('2.0'): 18 20 plone_version = '2.0.5' 21 elif plone_version.startswith('2.5'): 22 plone_version = '2.5' 19 23 else: 20 raise Exception("Error - Unsupported version. Suported versions: Plone 2.0.5-2. 1.2")24 raise Exception("Error - Unsupported version. Suported versions: Plone 2.0.5-2.5") 21 25 DiscussionLayer = LAYER_DISCUSSION 22 26 try: … … 32 36 out.write('Call setupSkin... \n') 33 37 setupSkin(self, out, Layers) 38 addConfiglet(self, CONFIGLET_ID, CONFIGLET_NAME) 34 39 35 40 def setupSkin(self, out, layers): … … 58 63 def uninstall(self): 59 64 skinstool = getToolByName(self, 'portal_skins') 65 66 # Unregister configlet 67 controlpanel_tool = getToolByName(self, 'portal_controlpanel') 68 controlpanel_tool.unregisterConfiglet(CONFIGLET_ID) 69 60 70 # Remove skin 61 71 for skinName in skinstool.getSkinSelections(): … … 67 77 path.remove(l) 68 78 skinstool.addSkinSelection(skinName, ','.join(path)) 79 80 def updateProperty(pp_ps, id, value,out, property_type = None): 81 if not pp_ps.hasProperty(id): 82 pp_ps.manage_addProperty(id, value, property_type) 83 out.write("Adding %s property to %s property sheet\n" % (id, 'qPloneCapthas') ) 84 else: 85 pp_ps._updateProperty(id, value) 86 87 def updateqPloneCaptchasPropertySheet(self, out): 88 pp = getToolByName(self, 'portal_properties') 89 if not 'qPloneCaptchas' in pp.objectIds(): 90 pp.addPropertySheet(id='qPloneCaptchas', title= '%s Properties' % 'qPloneCaptchas') 91 out.write("Adding %s property sheet to portal_properies\n" % 'qPloneCaptchas' ) 92 props_sheet = pp['qPloneCaptchas'] 93 94 updateProperty(props_sheet, id="captcha_bg", value='white', property_type='string', out=out) 95 updateProperty(props_sheet, id="captcha_textcolor", value='black', property_type='string', out=out) 96 97 avaliable_libs = [] 98 avaliable_libs.append('Static') 99 if havePIL: avaliable_libs.append('PIL') 100 if havePyCaptchas: avaliable_libs.append('PyCaptchas') 101 updateProperty(props_sheet, id="avaliable_libs", value=avaliable_libs, property_type='lines', out=out) 102 updateProperty(props_sheet, id="current_lib", value='avaliable_libs', property_type='selection', out=out) 103 104 def addConfiglet(self, configlet_id, configlet_name): 105 """ Add Configlet. Delete old version before adding, if exist one. 106 """ 107 108 controlpanel_tool = getToolByName(self, 'portal_controlpanel') 109 controlpanel_tool.unregisterConfiglet(configlet_id) 110 controlpanel_tool.registerConfiglet(id=configlet_id, name=configlet_name, category='Products', 111 action='string:${portal_url}/%s' % configlet_id, 112 appId=PRODUCT_NAME, permission=ManagePortal, 113 imageUrl='logoIcon.gif') qPloneCaptchas/trunk/README.txt
r418 r419 4 4 2.0.5 5 5 2.1.2 6 2.5 6 7 7 8 Plugs to: … … 15 16 If qPloneCaptchas is expected to be used with qPloneComments or PloneFormMailer please make sure that qPloneCaptchas installed only after these products. 16 17 17 Author :18 Authors: 18 19 19 20 Volodymyr Cherepanyak - chervol@quintagroup.com 20 21 Mykola Kharechko - crchemist@quintagroup.com 21 22 22 23 Future: qPloneCaptchas/trunk/__init__.py
r418 r419 1 1 from Products.CMFCore.DirectoryView import registerDirectory 2 from config import GLOBALS3 2 from AccessControl import allow_module 3 from config import * 4 4 5 5 allow_module('Products.qPloneCaptchas.utils') 6 6 allow_module('Products.qPloneCaptchas.config') 7 try: 8 import Captcha 9 havePyCaptchas = True 10 except: 11 havePyCaptchas = False 12 13 try: 14 import PIL 15 havePIL = True 16 except: 17 havePIL = False 7 18 8 19 registerDirectory('skins', GLOBALS) qPloneCaptchas/trunk/skins/plone_captchas/captcha_validator.vpy
r418 r419 2 2 return state 3 3 request = context.REQUEST 4 5 cprops = context.portal_properties['qPloneCaptchas'] 6 curr_lib = cprops.getProperty('current_lib', 'Static') 7 4 8 key = request.get('hashkey', '') 5 9 evalkey = request.get('evalkey', '') … … 9 13 10 14 from Products.qPloneCaptchas.config import CAPTCHAS_COUNT 11 from Products.qPloneCaptchas.utils import encrypt 12 index = int(key[:4],16)%CAPTCHAS_COUNT 13 if index == 0: 14 index = CAPTCHAS_COUNT 15 img = getattr(context, '%s.jpg' % index) 16 solution = img.title 17 enc = encrypt(test_key) 15 from Products.qPloneCaptchas.utils import encrypt, getIndex 16 17 if curr_lib == 'Static': 18 index = int(key[:4],16)%CAPTCHAS_COUNT 19 if index == 0: 20 index = CAPTCHAS_COUNT 21 img = getattr(context, '%s.jpg' % index) 22 solution = img.title 23 enc = encrypt(test_key) 24 25 elif curr_lib == 'PIL': 26 index = getIndex(test_key) 27 enc = encrypt('pAss'+evalkey+str(index)) 28 solution = key 18 29 19 30 if enc != solution: 20 31 state.setError('key', 'Please submit the validation code.', 'key_required') 21 return state.set(status="failure", portal_status_message ="Please re enter validation code")32 return state.set(status="failure", portal_status_message = "Please re enter validation code") 22 33 return state.set(status="success") qPloneCaptchas/trunk/skins/plone_captchas/captcha_widget.pt
r418 r419 1 1 <div metal:define-macro="captcha" tal:omit-tag=""> 2 <div class="field" tal:define="captcha here/getCaptcha; 3 error errors/key|nothing;" 2 <div class="field" 3 tal:define="captcha here/getCaptcha; 4 error errors/key|nothing" 4 5 tal:attributes="class python:test(error, 'field error', 'field')"> 6 5 7 <input type="hidden" name="hashkey" 6 8 tal:attributes="value captcha/key"> 7 9 <input type="hidden" name="evalkey" 8 10 tal:attributes="value captcha/evalkey"> 11 9 12 <label for="key">Please enter the validataion code</label> 10 13 <span class="fieldRequired" title="Required">(Required)</span><br /> 11 14 <input type="text" name="key" size="10" value=""> 12 <img tal:attributes="src string:${portal_url}/getCaptchaImage/${captcha/key}"/> 15 <!-- <h1 tal:content="captcha/key" /> --> 16 <span tal:condition="python:context.portal_properties['qPloneCaptchas'].current_lib=='Static'" 17 tal:replace="structure captcha/src"/> 18 <img tal:condition="python:context.portal_properties['qPloneCaptchas'].current_lib=='PIL'" 19 tal:attributes="src string:${portal_url}/getCaptchaImage/${captcha/evalkey}"/> 13 20 </div> 14 21 </div> qPloneCaptchas/trunk/skins/plone_captchas/getCaptcha.py
r418 r419 8 8 ##title= 9 9 from DateTime import DateTime 10 from Products.qPloneCaptchas.utils import encrypt 11 purl = context.portal_url() 10 from Products.qPloneCaptchas.utils import encrypt, getLen 11 from Products.qPloneCaptchas.config import CAPTCHAS_COUNT 12 cprops = context.portal_properties['qPloneCaptchas'] 13 curr_lib = cprops.getProperty('current_lib', 'Static') 14 15 res = {} 12 16 date = str(DateTime().timeTime()) 13 key = encrypt('pAss'+date) 14 return {'key':key, 'evalkey':date} 17 res['evalkey'] = date 18 19 if curr_lib == 'Static': 20 key = encrypt('pAss'+date) 21 res['key'] = key 22 index = int(key[:4],16)%CAPTCHAS_COUNT 23 if index == 0: 24 index = CAPTCHAS_COUNT 25 img = getattr(context, '%s.jpg' % index) 26 res['src'] = img.tag() 27 28 elif curr_lib == 'PyCaptchas': 29 """ 30 c = getPyCaptchaImage() 31 context.REQUEST.RESPONSE.setHeader('Content-Type', 'image/gif') 32 context.REQUEST.RESPONSE.setHeader('Content-Length', c[2]) 33 context.REQUEST.RESPONSE.setHeader('Accept-Ranges', 'bytes') 34 context.REQUEST.form['hashkey'] = c[1] 35 context.REQUEST.form['evalkey'] = date 36 return c[0] 37 """ 38 pass 39 40 elif curr_lib == 'PIL': 41 length = getLen() 42 index = int(float(date) % length) 43 key = encrypt('pAss'+date+str(index)) 44 res['key'] = key 45 46 return res qPloneCaptchas/trunk/skins/plone_captchas/getCaptchaImage.py
r418 r419 7 7 ##parameters= 8 8 ##title= 9 from Products.qPloneCaptchas. config import CAPTCHAS_COUNT9 from Products.qPloneCaptchas.utils import gen_captcha, getLen, getWord 10 10 request = context.REQUEST 11 key = request.traverse_subpath[0] 12 index = int(key[:4],16)%CAPTCHAS_COUNT 13 if index == 0: 14 index = CAPTCHAS_COUNT 15 img = getattr(context, '%s.jpg' % index) 16 return img.index_html(request, request.RESPONSE) 11 date = request.traverse_subpath[0] 12 length = getLen() 13 index = int(float(date) % length) 14 15 im = gen_captcha(getWord(index), 50) 16 context.REQUEST.RESPONSE.setHeader('Content-Type', 'image/jpeg') 17 context.REQUEST.RESPONSE.setHeader('Content-Length', im['size']) 18 context.REQUEST.RESPONSE.setHeader('Accept-Ranges', 'bytes') 19 20 return im['src'] qPloneCaptchas/trunk/utils.py
r418 r419 1 1 import md5 2 2 import random 3 from Products.qPloneCaptchas.data import basic_english 3 4 def encrypt(s): 4 5 return md5.new(s).hexdigest().upper() 6 7 """ 8 def getPyCaptchaImage(): 9 import ImageFile 10 from PIL import ImageFile as pyImageFile 11 import sys 12 sys.modules['ImageFile'] = pyImageFile 13 from Captcha.Visual.Tests import PseudoGimpy 14 from cStringIO import StringIO 15 g = PseudoGimpy() 16 image = g.render() 17 sol = g.solutions[0] 18 outFile = StringIO() 19 image.save(outFile, "GIF") 20 outFile.seek(0) 21 src = outFile.read() 22 size = len(src) 23 sys.modules['ImageFile'] = ImageFile 24 return (src, sol, size) 25 """ 26 27 def gen_captcha(text, fnt_sz, fmt='JPEG'): 28 """Generate a captcha image""" 29 import ImageFile 30 import Image 31 import ImageFont 32 import ImageDraw 33 import ImageFilter 34 from PIL import ImageFile as pyImageFile 35 import sys 36 sys.modules['ImageFile'] = pyImageFile 37 from Captcha.Visual.Tests import PseudoGimpy 38 from cStringIO import StringIO 39 outFile = StringIO() 40 41 font = ImageFont.load_default() 42 dim = font.getsize(text) 43 im = Image.new('RGB', (dim[0]+5,dim[1]+5), 0xffffff) 44 draw = ImageDraw.Draw(im) 45 draw.textsize(text) 46 draw.text((3,3), text, font=font, fill=0x0b) 47 """ 48 # randomly select the foreground color 49 fgcolor = random.randint(0,0xffff00) 50 # make the background color the opposite of fgcolor 51 bgcolor = fgcolor ^ 0xffffff 52 # create a font object 53 import sys 54 #font = ImageFont.truetype('data/fonts/vera/Vera.ttf',fnt_sz) 55 font = ImageFont.load_default() 56 # determine dimensions of the text 57 dim = font.getsize(text) 58 # create a new image slightly larger that the text 59 im = Image.new('RGB', (dim[0]+5,dim[1]+5), bgcolor) 60 d = ImageDraw.Draw(im) 61 x, y = im.size 62 r = random.randint 63 # draw 100 random colored boxes on the background 64 for num in range(100): 65 d.rectangle((r(0,x),r(0,y),r(0,x),r(0,y)),fill=r(0,0xffffff)) 66 # add the text to the image 67 d.text((3,3), text, font=font, fill=fgcolor) 68 im = im.filter(ImageFilter.EDGE_ENHANCE_MORE) 69 """ 70 # save the image to a file 71 im.save(outFile, format=fmt) 72 outFile.seek(0) 73 src = outFile.read() 74 size = len(src) 75 sys.modules['ImageFile'] = ImageFile 76 return {'src':src, 'size':size} 77 78 def getLen(): 79 words = basic_english.words.split() 80 return len(words) 81 82 def getWord(index): 83 words = basic_english.words.split() 84 return words[index] 85 86 def getIndex(word): 87 words = basic_english.words.split() 88 try: 89 res = words.index(word) 90 except ValueError: 91 res = getLen()+1 92 return res qPloneCaptchas/trunk/version.txt
r418 r419 1 0.8 1 0.8.3
