2010-06-01 18:38:29 +02:00
|
|
|
# -*- coding: utf-8 -*-
|
2009-08-15 12:21:26 +02:00
|
|
|
|
2010-06-04 12:57:57 +02:00
|
|
|
# Captcha validator, see captcha.txt for design notes.
|
|
|
|
|
2009-08-15 12:21:26 +02:00
|
|
|
from Acquisition import aq_inner
|
|
|
|
|
|
|
|
from z3c.form import validator
|
|
|
|
|
|
|
|
from z3c.form.interfaces import IValidator
|
|
|
|
|
|
|
|
from zope.component import getMultiAdapter, provideAdapter, queryUtility
|
|
|
|
|
|
|
|
from zope.schema import ValidationError
|
|
|
|
|
2009-10-17 16:13:27 +02:00
|
|
|
from zope.interface import implements, Interface
|
|
|
|
from zope.schema.interfaces import IField
|
|
|
|
from zope.component import adapts
|
2009-08-28 10:28:58 +02:00
|
|
|
|
2010-06-01 18:38:29 +02:00
|
|
|
from plone.registry.interfaces import IRegistry
|
|
|
|
|
|
|
|
from plone.app.discussion.interfaces import ICaptcha
|
|
|
|
from plone.app.discussion.interfaces import IDiscussionSettings
|
|
|
|
from plone.app.discussion.interfaces import IDiscussionLayer
|
|
|
|
from plone.app.discussion.interfaces import MessageFactory as _
|
|
|
|
|
2009-08-28 10:28:58 +02:00
|
|
|
try:
|
2010-06-01 18:38:29 +02:00
|
|
|
from plone.formwidget.captcha.validator import WrongCaptchaCode
|
2009-08-28 10:28:58 +02:00
|
|
|
except:
|
|
|
|
pass
|
|
|
|
|
|
|
|
try:
|
2010-06-01 18:38:29 +02:00
|
|
|
from plone.formwidget.recaptcha.validator import WrongCaptchaCode
|
2009-08-28 10:28:58 +02:00
|
|
|
except:
|
|
|
|
pass
|
2009-08-21 23:15:13 +02:00
|
|
|
|
|
|
|
from zope.interface import implements, Interface
|
|
|
|
from zope.schema.interfaces import IField
|
|
|
|
from zope.component import adapts
|
2009-08-15 12:21:26 +02:00
|
|
|
|
2010-06-01 18:38:29 +02:00
|
|
|
|
2009-08-15 12:21:26 +02:00
|
|
|
class CaptchaValidator(validator.SimpleFieldValidator):
|
2009-08-21 23:15:13 +02:00
|
|
|
implements(IValidator)
|
|
|
|
adapts(Interface,IDiscussionLayer,Interface,IField,Interface)
|
|
|
|
# Object, Request, Form, Field, Widget,
|
2009-08-15 12:21:26 +02:00
|
|
|
|
|
|
|
def validate(self, value):
|
|
|
|
super(CaptchaValidator, self).validate(value)
|
|
|
|
|
|
|
|
registry = queryUtility(IRegistry)
|
|
|
|
settings = registry.forInterface(IDiscussionSettings)
|
|
|
|
|
|
|
|
if settings.captcha == 'captcha':
|
|
|
|
# Fetch captcha view
|
2010-06-01 18:38:29 +02:00
|
|
|
captcha = getMultiAdapter((aq_inner(self.context), self.request),
|
|
|
|
name='captcha')
|
2009-08-15 12:21:26 +02:00
|
|
|
if value:
|
|
|
|
if not captcha.verify(value):
|
|
|
|
raise WrongCaptchaCode
|
|
|
|
else:
|
|
|
|
return True
|
|
|
|
raise WrongCaptchaCode
|
|
|
|
elif settings.captcha == 'recaptcha':
|
2010-06-01 18:38:29 +02:00
|
|
|
# Fetch recaptcha view
|
|
|
|
captcha = getMultiAdapter((aq_inner(self.context), self.request),
|
|
|
|
name='recaptcha')
|
2009-08-15 12:21:26 +02:00
|
|
|
if not captcha.verify():
|
2010-06-01 18:38:29 +02:00
|
|
|
raise WrongCaptchaCode
|
2009-08-15 12:21:26 +02:00
|
|
|
else:
|
2010-06-01 18:38:29 +02:00
|
|
|
return True
|
|
|
|
|
|
|
|
# Register Captcha validator for the Captcha field in the ICaptcha Form
|
|
|
|
validator.WidgetValidatorDiscriminators(CaptchaValidator,
|
|
|
|
field=ICaptcha['captcha'])
|