reCAPTCHA for newsletter subscription form

Hi,

I tried to get the CAPTCHA on the newsletter register box to use Google V2, because i think it's nicer, but got stuck a little. Used the code on the other  CAPTCHA's but i think i missed something.

This is the code so far:



    <operation method="replace" type="multiline" onerror="warning">
      <find><![CDATA[
<?php echo functions::form_draw_captcha_field('captcha', 'newsletter_subscribe', 'required'); ?>
      ]]></find>

      <insert><![CDATA[
<?php echo functions::recaptcha_draw(); ?>
      ]]></insert>
    </operation>
  </file>

  <file name="pages/newsletter.inc.php">

    <operation method="replace" type="multiline" onerror="warning">
      <find><![CDATA[
 if (settings::get('captcha_enabled')) {
        $captcha = functions::captcha_get('newsletter_subscribe');
        if (!$captcha || empty($_POST['captcha']) || $captcha != $_POST['captcha']) {
          throw new Exception(language::translate('error_invalid_captcha', 'Invalid CAPTCHA given'));
        }
      }
      ]]></find>

      <insert><![CDATA[
 if (empty($_POST['g-recaptcha-response']) || !functions::recaptcha_validate($_POST['g-recaptcha-response'])) {
        throw new Exception(language::translate('error_invalid_captcha', 'Invalid CAPTCHA'));
      }
      ]]></insert>
    </operation>
  </file>```
tim

There is nothing wrong with your code.

The problem is the form is fetched partially from the newsletter page via AJAX. Causing the Google reCAPTCHA library not to load as it resides outside the partial content. If you visit the direct URL /newsletter in the browser this probably works as expected?

I have tried to make the script tag code stick within the partial content on the page. But it is seems stripped by either Featherlight or jQuery. I have no easy workaround just now. This requires some trial and error to find a solution that works.

tim

I found a solution by adding an afterOpen event to Featherlight to load the library.
See the new download.

Thanks Tim,

I've got a working version for the product reviews also if you like.

tim

Sure, just post it here and I will add it.

ok, here it is:



    <operation method="replace" type="multiline" onerror="warning">
      <find><![CDATA[
 if (settings::get('captcha_enabled')) {
        $captcha = functions::captcha_get('review_product');
        if (empty($captcha) || $captcha != $_POST['captcha']) throw new Exception(language::translate('error_invalid_captcha', 'Invalid CAPTCHA given'));
      }

      ]]></find>

      <insert><![CDATA[
 if (empty($_POST['g-recaptcha-response']) || !functions::recaptcha_validate($_POST['g-recaptcha-response'])) {
        throw new Exception(language::translate('error_invalid_captcha', 'Invalid CAPTCHA'));
    }
      ]]></insert>
    </operation>
  </file>

  <file name="includes/templates/default.catalog/views/box_product_reviews.inc.php">

    <operation method="replace" type="multiline" onerror="warning">
      <find><![CDATA[
<div style="max-width: 250px;"><?php echo functions::form_draw_captcha_field('captcha', 'review_product', 'required'); ?></div>
      ]]></find>

      <insert><![CDATA[
<div style="max-width: 250px;"><?php echo functions::recaptcha_draw(); ?></div>
      ]]></insert>
    </operation>
  </file>```
tim

Thank you, I added it. Do you like the new vMod system and the editor in Admin -> vMods -> Edit vMod?

Yeah, I think that's great!

He Tim,

The product Review code Re Captcha is a paid add on, won't you get a error if it's not installed?

tim

@onlygoodness, no vMod only operates on loaded resources. Sort of an on demand design. :)
If the platform is not loading the review components. It's simply ignored.

View code on GitHub