We recently launched the CIOC Community Site running on Plone. While we have not actually had any SPAM on the site yet, we continually get automated SPAM registrations that we then need to go and clear out. Last night Kate got fed up, so I decided to see if I could put a CAPTCHA on the form.
Luckily there is collective.recaptcha which is in beta and, while not listing any releases on the Plone site, is available from the Cheese Shop. But, alas, there is no documentation. It is supposed to be a drop in replacement for collective.captcha so with a little sleuthing and I got it to work. Here's how.
I am using the unified Unix installer so everything is done via buildout. Following the instructions on the collective.captcha page we need to add collective.recaptcha to your eggs list and collective.recaptcha to your zcml list. Then run ./bin/buildout and restart your Plone instance.
We need to enter our private and public ReCAPTCHA keys in the collective.recaptcha setup form which is located at http://urltoyourplonesite.com/recaptcha-settings
Registration Form Modifications
Using this site as a helpful reference I was able to figure out the additions I needed to make to the registration form and validator.
You need to go in through your Plone site setup into the Zope Management Interface. Navigate to portal_skins/plone_login. Click join_form and then the customize button. You should now be able to customize the form contents. Down near the bottom of the form there is a line:
Above that line you need to add:
<div class="field" tal:define="error errors/captcha|nothing;" tal:attributes="class python:test(error, 'field error', 'field')"> <label for="captcha" i18n:translate="label_captcha">Captcha</label> <span class="fieldRequired" title="Required" i18n:attributes="title" i18n:domain="plone" i18n:translate="label_required">(Required)</span> <div class="formHelp" i18n:translate="help_captcha"> Provide the text in the image. Just to avoid spambots </div> <p tal:replace="structure here/@@captcha/image_tag" /> </div>
Note that the difference from the code provided by Mikel Larreategi is that ReCAPTACH provides the input element itself, so you need to omit the div that includes the <input type="text"> tag.
Once you have that saved, you need to go back to portal_skins/plone_login and click join_form_validate and once again click customize. At the bottom of the validation code, just before return state add:
captcha = context.REQUEST.get('recaptcha_response_field') view = context.restrictedTraverse('@@captcha') if not view.verify(captcha): state.setError('captcha', _(u'Are you a bot? Try again...')) state.set(status='failure')
Note that the difference in the validation code from Mikel Larreategi's is that the ReCAPTCHA inserted input tag is called recaptcha_response_field and not captcha.
Hopefully that is helpful to someone other than me :)