sfReCaptchaPlugin - 1.0.2

Integrate reCAPTCHA in symfony

You are currently browsing
the website for symfony 1

Visit the Symfony2 website


« Back to the Plugins Home

Signin


Forgot your password?
Create an account

Tools

Stats

advanced search
Information Readme Releases Changelog Contribute
This plugin is deprecated and is not maintained anymore. Please switch to Symfony2.
Show source | Show as Markdown

sfReCaptcha plugin

The reCAPTCHA project takes unOCRable bits of text and uses people to decipher them via CAPTCHA tests. Answers are verified by juxtaposing deciphered texts with the undeciphered. By implimenting reCAPTCHA you are helping digitize books.

The sfReCaptcha plugin integrates reCAPTCHA in symfony and includes the reCAPTCHA library, a custom validator and an example module which demonstrates the usage of this plugin.

Note: In versions lower than 1.0.2, the reCAPTCHA library was slightly modified in order to make the plugin work.

For more information on reCAPTCHA visit recaptcha.net.

Installation

In order to use the reCAPTCHA or Mailhide service you must first sign up and get an API key. You can do this [here] for reCAPTCHA or http://mailhide.recaptcha.net/apikey here for Mailhide.

Note: If you've created a host like http://askeet/ for development, the registration will fail because the reCAPTCHA service doesn't recognize it as a valid url. To handle this, simply rename your development host and attach a .tld to it (like askeet.org).

To install the plugin, go into your symfony project directory and execute the following command:

  symfony plugin-install http://plugins.symfony-project.com/sfReCaptchaPlugin

after the plugin is installed, navigate to your applications app.yml file and paste your API keys there:

    all:
        recaptcha:
            publickey: "foo"
            privatekey: "bar"
        mailhide:
            publickey: "foo"
            privatekey: "bar"

To use the example module that comes with the plugin, you need to activate it in your applications settings.yml file:

    all:
        .settings:
            enabled_modules:        [recaptcha](default,)

Don't forget to clear your cache:

  symfony cc

Finally, open the example module in your web browser of choice:

    http://foobar.com/recaptcha

Or for the Mailhide service:

    http://foobar.com/recaptcha/mailhide

reCAPTCHA usage

To get a better knowledge of how the plugin works, take a look at the example module which is shipped with the plugin. The actions.class.php is pretty simple and just needs to include the recaptchalib.php. Validation is done by the included sfReCaptchaValidator.

  public function executeIndex()
  {
    // load recaptcha library
    if (!function_exists('recaptcha_get_html')) {
      require_once (sfConfig::get('sf_plugins_dir').'/sfReCaptchaPlugin/lib/recaptchalib.php');
    }

    if ($this->getRequest()->getMethod() == sfRequest::POST) {
      // captcha correct; do whatever you want here

      return sfView::SUCCESS;
    }
  }

  public function handleErrorIndex()
  {
    return sfView::SUCCESS;
  }

As for the template, it just needs to have a form with a call to the recaptcha_get_html function with your public-key and the returned error as the parameters.

    <?php use_helper('Validation'); ?>

    <?php echo form_tag('recaptcha/index'); ?>
        <?php echo recaptcha_get_html(sfConfig::get('app_recaptcha_publickey'), $sf_request->getError('recaptcha_response_field', null)); ?>
        <?php echo submit_tag('submit'); ?>
    </form>

Now, the only thing left is to validate the reCAPTCHA. Create a validation file for your action (here index.yml).

    fields:
        recaptcha_response_field:
            sfReCaptchaValidator:

That's it, your reCAPTCHA should work now.

Mailhide usage

Mailhide is even simpler to use than reCAPTCHA. Just make sure you include the recaptchalib.php in your action.

  public function executeMailhide()
  {
    require_once (sfConfig::get('sf_plugins_dir').'/sfReCaptchaPlugin/lib/recaptchalib.php');
  }

in you template, just call the recaptcha_mailhide_html function with your Mailhide API keys and the email address you wish to hide:

    <?php echo recaptcha_mailhide_html(sfConfig::get('app_mailhide_publickey'), sfConfig::get('app_mailhide_privatekey'), "foo@bar.de"); ?>

Notes

Before version 1.0.2 the reCAPTCHA php library used global variables which didn't work with symfony, so that the library shipped with this plugin was slightly modified in order to make it work. This is not the case for versions >= 1.0.2.

Changelog

  • 1.0.1
  • Updated the recaptcha-php-library to version 1.8
  • 1.0.2
  • Updated the recaptcha-php-library to version 1.9
  • Cleaned up the example indexSuccess action (Now doesn't throw an error if no text was entered, thanks flod)
  • Small updates to the README file