NP – Asirra

August 11, 2010

Zend Framework

Recently, I’ve read an article about the Asirra web service, which is a HIP, CAPTCHA alternative, that works by asking users to identify photographs of cats and dogs. I really liked that idea, so I decided to create Asirra implementation for my favorite PHP framework – Zend Framework.

NP – Asirra package provides API for using and implementing Asirra web service in some project that is powered by Zend Framework. I hosted this project on Google Code and you can check it out here. Also, here is a direct link for downloading NP-Asirra package.

There are several classes in the NP-Asirra package. There’s a service class – NP_Service_Asirra, which provides a client for consuming Asirra. Developers can use it independently, in order to generate HTML code for outputting Asirra challenge, or validating user’s input. As the Asirra web service allows developer to customize look of the Asirra challenge, through SetEnlargedPosition() and SetCellsPerRow() JS methods, those can be set through this service class. Here are some examples:

//Creating Asirra service instance and setting position of enlarged picture.
$assiraService = new NP_Service_Asirra();
$assiraService->setOption('enlargedPosition'=>'bottom'); 

//Displaying Asirra challenge.
echo $assiraService->getHtml(); 

//On subsequent request:
//Validating Asirra ticket. verify() method will return 
//NP_Service_Asirra_Response instance, which provides an isValid() method.
$asirraResponse = $assiraService->verify($_POST['Asirra_Ticket']);

Also, this package provides approprite CAPTCHA adapter – NP_Captcha_Assira, which utilize NP_Service_Asirra in order to generate and validate Asirra challenge. A typical use case might look like the following:

//Creating a captcha instance:
$captcha = new NP_Captcha_Asirra();
echo '
'; echo $captcha->render($view); echo '
'; //On subsequent request: if ($captcha->isValid(null, $_POST)) { //Validated! }

And here’s an important note… As Asirra implementation requires JS Asirra_CheckIfHuman() function to be called, so that client side validation can be executed, in both of those examples above, you must
manually make sure that that function is called before submit form in which you’ve placed Asirra.

If you decide to use Asirra CAPTCHA adapter along with Zend_Form_Element_Captcha, that problem can be bypassed, because this adapter has its own decorator, which injects necessary JS code, that will try to intercept submit event on the parent form and execute Asirra_CheckIfHuman() function. jQuery library, served by Google’s API, is utilized for this purpose.

Still, if you want handle all that JS job by yourself, you can disable usage of above mentioned decorator, by setting $_useJsDecorator flag to boolean false:

$captcha->setUseJsDecorator(false);

Here’s an example of how Asirra CAPTCHA adapter can be used in Zend_Form_Element_Captcha implementation:

$captchaElement = new Zend_Form_Element_Captcha(
	'captcha', 
	array('captcha'=>new NP_Captcha_Asirra(array('enlargedPosition'=>'bottom')))
);
$captcha->addPrefixPath('NP_Form_Decorator', 'NP/Form/Decorator', 'DECORATOR');

You’ll notice that you must also add prefix/path to the AsirraCheckIfHuman decorator, if you decide to use it.

One Response to “NP – Asirra”

  1. dzschille Says:

    Accessibility is a great advantage of reCAPTCHA. But when i will have a project where this is not needed, i will try Asirra. Cute pets ;-)