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
|
|
|
|
|
2010-07-12 15:47:53 +02:00
|
|
|
from zope.component import getMultiAdapter, provideAdapter, queryUtility
|
2009-08-15 12:21:26 +02:00
|
|
|
|
|
|
|
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)
|
2010-06-10 12:51:41 +02:00
|
|
|
adapts(Interface, IDiscussionLayer, Interface, IField, Interface)
|
2009-08-21 23:15:13 +02:00
|
|
|
# Object, Request, Form, Field, Widget,
|
2010-06-10 12:51:41 +02:00
|
|
|
# We adapt the CaptchaValidator class to all form fields (IField)
|
2009-08-15 12:21:26 +02:00
|
|
|
|
|
|
|
def validate(self, value):
|
|
|
|
super(CaptchaValidator, self).validate(value)
|
|
|
|
|
2010-07-12 15:47:53 +02:00
|
|
|
registry = queryUtility(IRegistry)
|
2009-08-15 12:21:26 +02:00
|
|
|
settings = registry.forInterface(IDiscussionSettings)
|
|
|
|
|
2010-07-13 11:24:23 +02:00
|
|
|
if settings.captcha == 'captcha' or settings.captcha == 'recaptcha':
|
2010-06-01 18:38:29 +02:00
|
|
|
captcha = getMultiAdapter((aq_inner(self.context), self.request),
|
2010-06-10 12:51:41 +02:00
|
|
|
name=settings.captcha)
|
|
|
|
if not captcha.verify(input=value):
|
2010-07-13 12:41:26 +02:00
|
|
|
raise WrongCaptchaCode
|
2009-08-15 12:21:26 +02:00
|
|
|
else:
|
2010-06-01 18:38:29 +02:00
|
|
|
return True
|
2010-06-21 11:11:45 +02:00
|
|
|
|
|
|
|
|
2010-06-01 18:38:29 +02:00
|
|
|
# Register Captcha validator for the Captcha field in the ICaptcha Form
|
|
|
|
validator.WidgetValidatorDiscriminators(CaptchaValidator,
|
2010-06-10 12:51:41 +02:00
|
|
|
field=ICaptcha['captcha'])
|
2010-07-12 15:47:53 +02:00
|
|
|
|