Merge pull request #19321 from colemanw/profileGetFieldsFix
[civicrm-core.git] / CRM / Utils / ReCAPTCHA.php
index 93747f7b130ef7e801d42bc06abd38ed858404c5..2764e242641c8d061804f234c04f72b7016465c3 100644 (file)
@@ -78,7 +78,7 @@ class CRM_Utils_ReCAPTCHA {
     $config = CRM_Core_Config::singleton();
     $useSSL = FALSE;
     if (!function_exists('recaptcha_get_html')) {
-      require_once 'packages/recaptcha/recaptchalib.php';
+      require_once 'recaptcha/recaptchalib.php';
     }
 
     // Load the Recaptcha api.js over HTTPS
@@ -96,6 +96,7 @@ class CRM_Utils_ReCAPTCHA {
       TRUE
     );
     $form->registerRule('recaptcha', 'callback', 'validate', 'CRM_Utils_ReCAPTCHA');
+    $form->addRule('g-recaptcha-response', ts('Please go back and complete the CAPTCHA at the bottom of this form.'), 'recaptcha');
     if ($form->isSubmitted() && empty($form->_submitValues['g-recaptcha-response'])) {
       $form->setElementError(
         'g-recaptcha-response',
@@ -117,4 +118,18 @@ class CRM_Utils_ReCAPTCHA {
     }
   }
 
+  /**
+   * @param $value
+   * @param CRM_Core_Form $form
+   *
+   * @return mixed
+   */
+  public static function validate($value, $form) {
+    $resp = recaptcha_check_answer(CRM_Core_Config::singleton()->recaptchaPrivateKey,
+      $_SERVER['REMOTE_ADDR'],
+      $_POST['g-recaptcha-response']
+    );
+    return $resp->is_valid;
+  }
+
 }