Merge remote-tracking branch 'upstream/4.5' into 4.5-master-2015-02-25-11-27-40
[civicrm-core.git] / CRM / Utils / ReCAPTCHA.php
index cdefd36e62bbb79431ec77446e9ca121adc64ac1..882556e0e4f6190b96fa26d17c749b8a4cacf27a 100644 (file)
@@ -23,7 +23,7 @@
  | GNU Affero General Public License or the licensing of CiviCRM,     |
  | see the CiviCRM license FAQ at http://civicrm.org/licensing        |
  +--------------------------------------------------------------------+
-*/
+ */
 
 /**
  *
@@ -51,7 +51,7 @@ class CRM_Utils_ReCAPTCHA {
   static private $_singleton = NULL;
 
   /**
-   * Singleton function used to manage this object
+   * Singleton function used to manage this object.
    *
    *
    * @return object
@@ -69,7 +69,7 @@ class CRM_Utils_ReCAPTCHA {
   }
 
   /**
-   * Add element to form
+   * Add element to form.
    */
   public static function add(&$form) {
     $error = NULL;
@@ -89,40 +89,18 @@ class CRM_Utils_ReCAPTCHA {
     $form->assign('recaptchaOptions', $config->recaptchaOptions);
     $form->add(
       'text',
-      'recaptcha_challenge_field',
-      NULL,
+      'g-recaptcha-response',
+      'reCaptcha',
       NULL,
       TRUE
     );
-    $form->add(
-      'hidden',
-      'recaptcha_response_field',
-      'manual_challenge'
-    );
-
     $form->registerRule('recaptcha', 'callback', 'validate', 'CRM_Utils_ReCAPTCHA');
-    $form->addRule(
-      'recaptcha_challenge_field',
-      ts('Input text must match the phrase in the image. Please review the image and re-enter matching text.'),
-      'recaptcha',
-      $form
-    );
+    if ($form->isSubmitted() && empty($form->_submitValues['g-recaptcha-response'])) {
+      $form->setElementError(
+        'g-recaptcha-response',
+        ts('Input text must match the phrase in the image. Please review the image and re-enter matching text.')
+      );
+    }
   }
 
-  /**
-   * @param $value
-   * @param CRM_Core_Form $form
-   *
-   * @return mixed
-   */
-  public static function validate($value, $form) {
-    $config = CRM_Core_Config::singleton();
-
-    $resp = recaptcha_check_answer($config->recaptchaPrivateKey,
-      $_SERVER['REMOTE_ADDR'],
-      $_POST["recaptcha_challenge_field"],
-      $_POST["recaptcha_response_field"]
-    );
-    return $resp->is_valid;
-  }
 }