Recaptcha plugin for Symfony 1.4.x

Categories: PHP

Hello everyone,
Recently I had to add Google Recaptcha to a site built with Symfony 1.4.x. I couldn’t find a 3rd party plugin for that so I built my own. I’ve decided to share my implementation to the whole world. Prior to using the plugin you need to create a Google Recaptcha account.

1. The widget

The widget has a couple of options you can specify. You must specify a public_key. You can get one from your Google Recaptcha account. You can also specify a theme. If omitted the default one will be used. You can find a list of available themes here.

lib/widget/sfWidgetFormGoogleRecaptcha.class

<?php

/**
 * Simple widget based on the Google Recaptcha library
 *
 * @author Stef
 */
class sfWidgetFormGoogleRecaptcha extends sfWidgetForm
{
  public function configure($options = array(), $attributes = array())
  {
    $this->addOption('public_key');
    $this->addOption('theme', null);
  }

  public function render($name, $value = null, $attributes = array(), $errors = array())
  {
    require_once(sfConfig::get('sf_lib_dir') . '/recaptcha/recaptchalib.php');
    $output = '';
    if ($this->getOption('theme')) {
      $output .= '<script type="text/javascript">
                  var RecaptchaOptions = {
                     theme : "' . $this->getOption('theme') . '"
                  };
                  </script>';
    }
    $output .= recaptcha_get_html($this->getOption('public_key'));
    return $output;
  }
}

2. The validator

After submitting a form you need to validate the past security code. The Google Recaptcha validator will handle that. You need to specify a private_key for the validator to work. You can obtain one from you Google Recaptcha account.

lib/validator/sfWidgetFormGoogleRecaptcha.class

<?php

/**
 * Recaptcha validator based on the Google Recaptcha library
 *
 * @author Stef
 */
class sfValidatorGoogleRecaptcha extends sfValidatorBase
{
  public function configure($options = array(), $messages = array())
  {
    $this->addOption('private_key');
  }

  public function doClean($value)
  {
    require_once(sfConfig::get('sf_lib_dir') . '/recaptcha/recaptchalib.php');
    $response = recaptcha_check_answer(
            $this->getOption('private_key'),
            $_SERVER["REMOTE_ADDR"],
            $_POST["recaptcha_challenge_field"],
            $_POST["recaptcha_response_field"]);
    if (!$response->is_valid) {
      throw new sfValidatorError($this, 'invalid');
    }
    return $value;
  }

  public function isEmpty($value) {
    return false;
  }
}

Here is an example code demonstrating how to use the plugin.

// The most awesome Google Recaptcha Widget!!!
$this->widgetSchema['secret'] = new sfWidgetFormGoogleRecaptcha(array(
        'public_key' => 'MY_PUBLIC_KEY',
        'theme' => 'clean'));

// The most awesome Google Recaptcha Validator!!!
$this->validatorSchema['secret'] = new sfValidatorGoogleRecaptcha(array(
        'private_key' => 'MY_PRIVATE_KEY'
    ), array(
        'invalid' => 'The verification code is incorrect.'
    ));

Leave a comment

Please be polite. It would be greatly appreciated.
Your email address will not be published and required fields are marked *

Stefan Staynov
Stefan Staynov

I'm a web developer with a passion for innovations. Most of the time I code for the web and mobile. I like spending time with my loved ones, playing basketball and sleeping

Categories