Google reCaptcha and Advanced Forms

Permalink
I'm using Concrete5 v5.6.1. I'm also using Advanced Forms. We found that the built-in captcha option is not all that great.

We want to integrate the newer Google reCaptcha with the checkbox option.

I did download recaptchalib for one of the other forum threads, but I found out that this one uses the previous version of reCaptcha. So, we don't want that.

I'm going to download the latest version from github here:https://github.com/google/recaptcha...

Anyone have an idea, some guidance, on how i may implement this into Advanced Forms.

I've been looking around and using the old lib i was able to add it to the form, but its not connected and i'm not sure how to connect the form validation in Concrete5 to add a custom form like this.


I've been able to get the captcha to appear in my webform. So thats good, but how might i override the built-in concrete5 form validation?

I think it is probably something in this bit that i need to change.

public function check($fieldName='ccmCaptchaCode') { 
  if ($_POST["g-recaptcha-response"]) {
    $resp = recaptcha_check_answer ($this->secret, $_SERVER["REMOTE_ADDR"], $_POST["g-recaptcha-response"]); 
   if ($resp->is_valid) return TRUE;
  }
return FALSE;
}


I see in the first line it says $fieldName='ccmCaptchaCode'. My google recaptcha isnt in a form field, so how do i target this?

I changed $fieldName='ccmCaptchaCode' to "$fieldName='g-recaptcha-response'", that seemed to allow me to submit the form. When i submit the form, I see a message that says "Incorrect CAPTCHA code. Please try again." I'm not sure where this is coming from, but I guess it is from the built-in CaptchaCode and since i've replaced it with Google reCaptcha, its still looking for the value of the Concrete5 captcha. So, is there a way to disable the form from looking at that?

I thought renaming the captcha.php from /concrete/helpers/validation/ would remove all of that functionality? Guess i was wrong!

 
rfletcher replied on at Permalink Reply
I may have solved my problem. I did change the "check" function to this:

// Validate
public function check() {
     if ($_POST["g-recaptcha-response"]) { 
        $resp = recaptcha_check_answer ($this->secret, $_SERVER["REMOTE_ADDR"], $_POST["g-recaptcha-response"]);
     if ($resp->is_valid) return TRUE;
          }
     return FALSE;
}


But wait there was more that i needed to do. I did do an override for /helpers/validation/

I added a captcha.php and recaptcha.functions.php file.

I had to update the recaptcha_check_answer function to this:

class ReCaptchaResponse {
        var $is_valid;
        var $error;
}
function recaptcha_check_answer ($privkey, $remoteip, $gRecaptchaResponse) {
   $recaptcha_response = new ReCaptchaResponse();
   $recaptcha = new \ReCaptcha\ReCaptcha($privkey);
   $resp = $recaptcha->verify($gRecaptchaResponse, $remoteip);
      if ($resp->isSuccess()) {
         // verified!
         $recaptcha_response->is_valid = true;
      } else {
         $recaptcha_response->is_valid = false;
         foreach ($resp->getErrorCodes() as $code) {
            echo '<tt>' , $code , '</tt> ';


So now i am getting the TRUE/FALSE response that i needed from this function to make the first part work. Before I was always getting an error when i submitted the form even if i had checked the google reCaptcha box.
zaneweb replied on at Permalink Reply
zaneweb
This is a bit outdated now, but if it helps there is an addon for simple reCAPTCHA.

https://www.concrete5.org/marketplace/addons/recaptcha...