Merge pull request #6132 from colemanw/CRM-16795
[civicrm-core.git] / CRM / Utils / ReCAPTCHA.php
index 95d3031812d3d790c5ffa72968999f17c9b9b6bc..080a2450865d5fbc6f5057a1e5b2e3fbe929a820 100644 (file)
@@ -3,7 +3,7 @@
  +--------------------------------------------------------------------+
  | CiviCRM version 4.6                                                |
  +--------------------------------------------------------------------+
- | Copyright CiviCRM LLC (c) 2004-2014                                |
+ | Copyright CiviCRM LLC (c) 2004-2015                                |
  +--------------------------------------------------------------------+
  | This file is a part of CiviCRM.                                    |
  |                                                                    |
  | GNU Affero General Public License or the licensing of CiviCRM,     |
  | see the CiviCRM license FAQ at http://civicrm.org/licensing        |
  +--------------------------------------------------------------------+
-*/
+ */
 
 /**
  *
  * @package CRM
- * @copyright CiviCRM LLC (c) 2004-2014
+ * @copyright CiviCRM LLC (c) 2004-2015
  * $Id$
  *
  */
@@ -47,18 +47,14 @@ class CRM_Utils_ReCAPTCHA {
    * pattern and cache the instance in this variable
    *
    * @var object
-   * @static
    */
   static private $_singleton = NULL;
 
   /**
-   * Singleton function used to manage this object
+   * Singleton function used to manage this object.
    *
-   * @param string the key to permit session scope's
    *
    * @return object
-   * @static
-   *
    */
   public static function &singleton() {
     if (self::$_singleton === NULL) {
@@ -68,19 +64,18 @@ class CRM_Utils_ReCAPTCHA {
   }
 
   /**
-   *
    */
-  public function __construct() {}
+  public function __construct() {
+  }
 
   /**
-   * Add element to form
-   *
+   * Add element to form.
    */
   public static function add(&$form) {
-    $error  = NULL;
+    $error = NULL;
     $config = CRM_Core_Config::singleton();
     $useSSL = FALSE;
-    if ( !function_exists( 'recaptcha_get_html' ) ) {
+    if (!function_exists('recaptcha_get_html')) {
       require_once 'packages/recaptcha/recaptchalib.php';
     }
 
@@ -94,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;
-  }
 }