Skip navigation.
Home

reCaptcha

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...here.

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

Installation Instructions

Register for a free reCaptcha account and get yourself the private and public keys for your domain.

Either edit init.php and add the folowing:

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.

Download the reCAPTCHA Library, extract recaptchalib.php in a new folder called recaptcha, inside your /libs/ folder. The full path should be: /libs/recaptcha/recaptchalib.php

Create a new file called function.recaptcha.php inside your /libs/smarty/plugins/ folder with the following code inside:

<?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);
}

?>

Edit submit.tpl, can be found on phpLD v3 inside your /templates/{YOUR-TEMPLATE}/ or in older version simply inside /templates/.
Look for something similar to the following code, it might be a little bit different from version to version or template to template:

{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):

{if $smarty.const.VISUAL_CONFIRM}
<tr>
   <td class="field" colspan="2">{recaptcha error=$reCaptchaError}</td>
</tr>
{/if}

Edit submit.php, from the root of your phpLD installation.

Look for (right on top of the file):

require_once 'init.php';

Right after add:

//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):

if (VISUAL_CONFIRM == 1)
   SmartyValidate :: register_validator('v_CAPTCHA'    , 'CAPTCHA:IMAGEHASH'  , 'isCaptchaValid', false, false, null, 'submit_link');

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):

//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:

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

Right above add:

//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:

if (SmartyValidate :: is_valid($data, 'submit_link'))

Replace with:

//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 :) Enjoy reCaptcha and phpLD