sfReCaptchaLibPlugin - 1.4.2

Integrate reCAPTCHA with Symfony PHP framework

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
Show source

sfReCaptchaLibPlugin 1.4.2

This plug-in is a port of recaptchalib 1.11 to the Symfony PHP framework. It features improved error handling and better integration than the other reCAPTCHA plug-in provides.

Installation

The recommended method for installation is via the symfony command line:

symfony plugin:install http://plugins.symfony-project.org/sfReCaptchaLibPlugin

To manually install, perform the following steps:

  • Extract the sfReCaptchaLibPlugin archive into the plugins/ directory. This should leave you with a plugins/sfReCaptchaLibPlugin folder.
  • Edit the config/ProjectConfiguration.class.php and modify the setup() call to enable the plugin:

    public function setup() { ... $this->enablePlugins('sfReCaptchaLibPlugin'); // add this line }

  • Clear your project's cache

    symfony cc

Configuration

The plug-in looks in app.yml for your reCAPTCHA API keys:

all:
  sf_recaptcha_plugin:
    public_key: 'your-public-key-goes-here'
    private_key: 'your-private-key-goes-here'

There is a third option that allows you to specify the location of the AJAX API. By default, it is loaded from Google's CDN and thus you are guaranteed to always get the most current version. If you need to load your own, use the js_dir setting in app.yml:

all:
  sf_recaptcha_plugin:
    ajax_api: 'path_to/recaptcha_ajax.js' 

Note that the path is relative to the web/js folder of your application.

Usage

Integrating reCAPTCHA into a standard form is easy:

In your action:
$this->recaptcha = new reCaptcha();
if($request->getMethod() == sfRequest::POST)
{
    $this->recaptcha->checkAnswer($request);
    if($this->recaptcha->isValid())
        // ...
}

In your view:
<?php echo $recaptcha->getHTML(ESC_RAW) ?>

What about the AJAX API? For example, the above does not work properly if you try to put the reCAPTCHA into a JQuery UI dialog. Instead, you need to tell your action to load the AJAX API, which gives you a Recaptcha JavaScript object. The reCaptcha object provides a few methods to help you use the AJAX API:

reCaptcha::getPublicKey() // returns the public key for use in the Recaptcha.create() method
reCaptcha::enableAjax() // calls sfResponse->addJavascript() to add the AJAX API.
reCaptcha.getJSON() // gets the reCAPTCHA settings as a JavaScript object, also for Recaptcha.create()

An example using JQuery UI is shown below:

in your action:
reCaptcha::enableAjax();
$this->recaptcha = new reCaptcha();
$this->recaptcha->setTheme('blackglass');

in your view:
<div id="recaptcha-dialog" title="Recaptcha Demo">
    <div id="recaptcha"></div>
</div>
<button id="open-recaptcha-dialog">Launch Demo...</button>
<script type="text/javascript">
$(function() {
    $('#recaptcha-dialog").dialog({
        width: 350,
        height: 250,
        autoOpen: false,
        open: function() {
            Recaptcha.create('<?php echo reCaptcha::getPublicKey() ?>', 'recaptcha', <?php echo $recaptcha->getJSON(ESC_RAW) ?>);
        },
        beforeClose: function() {
            Recaptcha.destroy();
        },
        buttons: {
            "Cancel": function() {
                $(this).dialog("close");
            }
        }
    });
    $('#open-recaptcha-dialog').button().click(function() {
        $('#recaptcha-dialog').dialog("open");
    }
}

If you run this demo, then each time you open the dialog you should see a different CAPTCHA, and they should be rendered using the blackglass theme.

Other notes

The enableAjax() method must be called from somewhere in the action--either in the action itself or in the action's template. It WILL NOT WORK in either a component or a component template. The reason is that enableAjax() calls sfContext::getInstance()->getResponse()->addJavascript() to add the AJAX API. By the time components are called, the renderer has already finalized the list of Javascripts and it is too late to make any changes to it.

I'm not completely sure what should be returned by the mailhideUrl() method in the case of an error. Right now, it currently returns an empty string.