/ December 2, 2008

Mod Title: reCaptcha – the cool image validation
Last Updated: 12.02.2008
Supported by Mod Developer: No
Where to Download: This Thread
Support Via This Thread or Download Site: This Thread
Compatible With: phpLD v2 & v3

Most of you have probably seen or at least heard of reCaptcha. It is a new captcha (image verification) system that is easy for humans to read and nearly impossible for robots. How the system works..

(Update August 10, 2019: Please note the original .net TLD ReCaptcha site is not longer, and now there is Google Recaptcha)

I have thought of this mod only as replacement for phpLD’s image verification system on servers where the GD library is not bundled with PHP, and users cannot have this important feature.

Installation Instructions

  1. Register for a free reCaptcha account and get yourself the private and public keys for your domain.
  2. Either edit “init.php” and add the folowing:
    PHP Code:
    define ('RECAPTCHA_PUBLIC_KEY''your-public-key-here');
    define ('RECAPTCHA_PRIVATE_KEY''your-private-key-here');

    or you can add new entries to your PLD_CONFIG database table with the IDs:
    RECAPTCHA_PUBLIC_KEY and RECAPTCHA_PRIVATE_KEY having as values your keys.

  3. Download the reCAPTCHA Library, extract “recaptchalib.php” in a new folder called “recaptcha“, inside your “/libs/” folder. The full path should be
    Code:
    /libs/recaptcha/recaptchalib.php
  4. Create a new file called “function.recaptcha.php” inside your “/libs/smarty/plugins/” folder with the following code inside:
    Code:
    <?php
    
    /**
     * Build reCaptcha output HTML
     *
     * @param array $params
     * @param object $smarty
     * @return string
     */
    function smarty_function_recaptcha($params, & $smarty)
    {
       //Load reCaptcha library
       require_once ('libs/recaptcha/recaptchalib.php');
    
       //Get any error message(s)
       $error = (!empty ($params['error']) ? $params['error'] : null);
    
       //Build and return reCaptcha HTML
       return recaptcha_get_html(RECAPTCHA_PUBLIC_KEY, $error);
    }
    
    ?>
  5. Edit “submit.tpl“, can be found on phpLD v3 inside your “/templates/{YOUR-TEMPLATE}/” or in older version simply inside “/templates/“.
    Look for something simmilar to the following code, it might be a little bit different from version to version or template to template:

     

    Code:
    {if $smarty.const.VISUAL_CONFIRM}
    <tr>
       <td class="label"><span class='req'>*</span>{l}Enter the code shown{/l}:</td>
       <td class="field">
          <input id="IMAGEHASH" name="IMAGEHASH" type="hidden" value="{$imagehash}" />
       <input id="CAPTCHA" name="CAPTCHA" type="text" value="" size="{$smarty.const.CAPTCHA_PHRASE_LENGTH}" maxlength="{$smarty.const.CAPTCHA_PHRASE_LENGTH}" class="text" />
       {validate form="submit_link" id="v_CAPTCHA" message=$smarty.capture.invalid_code}
       <br />
       <p class="small">{l}This helps prevent automated registrations.{/l}</p>
       <img src="{$smarty.const.DOC_ROOT}/captcha.php?imagehash={$imagehash}" class="captcha" alt="{l}Visual Confirmation Security Code{/l}" title="{l}Visual Confirmation Security Code{/l}" />
       </td>
    </tr>
    {/if}

    Replace it with the following code (you may use here your own HTML markup to fit your template, this is just an example to the default template):

    Code:
    {if $smarty.const.VISUAL_CONFIRM}
    <tr>
       <td class="field" colspan="2">
          {recaptcha error=$reCaptchaError}
       </td>
    </tr>
    {/if}
  6. Edit “submit.php“, from the root of your phpLD installation.
    • Look for (right on top of the file):
      PHP Code:
      require_once 'init.php';

      Right after add:

      PHP Code:
      //Load reCaptcha library
      require_once ('libs/recaptcha/recaptchalib.php');
    • Look for the following code and remove it (might be a little different depending on your phpLD version, but you should be able to handle it):
      PHP Code:
      if (VISUAL_CONFIRM == 1)
      SmartyValidate :: register_validator('v_CAPTCHA'    'CAPTCHA:IMAGEHASH'  'isCaptchaValid'falsefalsenull'submit_link');

    Note: We will now split the instructions for phpLD v3 and phpLD v2 users.

    Instructions for phpLD v3:

    • For all phpLD versions starting from v3.1.0 (and above), look for the following code and remove it (on top of the file):
      PHP Code:
      //Generate unique imagehash for visual confirmation
      if (VISUAL_CONFIRM == 1)
      {
      require_once 
      'include/functions_imgverif.php';
      $imagehash fetch_captcha_hash();
      $tpl->assign('imagehash'$imagehash);
      unset (
      $imagehash);
      }
    • Look for the following:
      PHP Code:
      if (SmartyValidate :: is_valid($data'submit_link') && $SecondValidation == 1)

      Right above add:

      PHP Code:
      //Validate reCaptcha
      if ($_POST['recaptcha_response_field'])
      {
      //Check reCaptcha answer
      $reCaptchaResp recaptcha_check_answer(
      RECAPTCHA_PRIVATE_KEY              ,
      $_SERVER['REMOTE_ADDR']            ,
      $_POST['recaptcha_challenge_field'],
      $_POST['recaptcha_response_field']
      );

       

         if ($reCaptchaResp->is_valid)
      {
      //reCaptcha is valid
      $tpl->assign('reCaptchaError'null);
      }
      else
      {
      //reCaptcha is NOT valid
      $SecondValidation 0;
      $tpl->assign('reCaptchaError'$reCaptchaResp->error);
      }
      }

    Instructions for phpLD v2:
    This is also aplicable for the very first phpLD v3 releases.

    • Look for:
      PHP Code:
      if (SmartyValidate :: is_valid($data'submit_link'))

      Replace with:

      PHP Code:
      //Validate ReCaptcha
      $SecondValidation 0;
      if (
      $_POST['recaptcha_response_field'])
      {
      //Check ReCaptcha answer
      $reCaptchaResp recaptcha_check_answer(
      RECAPTCHA_PRIVATE_KEY              ,
      $_SERVER['REMOTE_ADDR']            ,
      $_POST['recaptcha_challenge_field'],
      $_POST['recaptcha_response_field']
      );

       

         if ($reCaptchaResp->is_valid)
      {
      //reCaptcha is valid
      $tpl->assign(‘reCaptchaError’null);
      $SecondValidation 1;
      }
      else
      {
      //reCaptcha is NOT valid
      $tpl->assign(‘reCaptchaError’$reCaptchaResp->error);
      $SecondValidation 0;
      }
      }

      if (SmartyValidate :: is_valid($data'submit_link') && $SecondValidation === 1)

That’s all 🙂
I have also attached a screenshot of the reCaptcha working on phpLD 3.3.0

Hope you like it!
Boby