Merge pull request #11197 from agileware/CRM-21104
authorcolemanw <coleman@civicrm.org>
Wed, 18 Jul 2018 16:17:25 +0000 (12:17 -0400)
committerGitHub <noreply@github.com>
Wed, 18 Jul 2018 16:17:25 +0000 (12:17 -0400)
CRM-21104: Forcing reCaptcha on Contribution pages(with online payments) which have no Profile associated with them.

CRM/Admin/Form/Setting/Miscellaneous.php
CRM/Contribute/Form/Contribution/Main.php
CRM/Contribute/Form/ContributionBase.php
CRM/Core/Config/MagicMerge.php
CRM/Utils/ReCAPTCHA.php
settings/Core.setting.php
templates/CRM/Admin/Form/Setting/Miscellaneous.tpl
templates/CRM/Contribute/Form/Contribution/Main.tpl

index 5f0cb2c0246abc5e0f4d3da715b4206176dd1b59..31ef53a873c162057187735ac1141d951b8e8ca7 100644 (file)
@@ -49,6 +49,7 @@ class CRM_Admin_Form_Setting_Miscellaneous extends CRM_Admin_Form_Setting {
     'recaptchaOptions' => CRM_Core_BAO_Setting::SYSTEM_PREFERENCES_NAME,
     'recaptchaPublicKey' => CRM_Core_BAO_Setting::SYSTEM_PREFERENCES_NAME,
     'recaptchaPrivateKey' => CRM_Core_BAO_Setting::SYSTEM_PREFERENCES_NAME,
+    'forceRecaptcha' => CRM_Core_BAO_Setting::SYSTEM_PREFERENCES_NAME,
     'wkhtmltopdfPath' => CRM_Core_BAO_Setting::SYSTEM_PREFERENCES_NAME,
     'recentItemsMaxCount' => CRM_Core_BAO_Setting::SYSTEM_PREFERENCES_NAME,
     'recentItemsProviders' => CRM_Core_BAO_Setting::SYSTEM_PREFERENCES_NAME,
index 89b92fbf3e98710b2fdce7061f66ad4be69f0ce5..109189f9c010ad569530717ca9c934c89d3ddfb4 100644 (file)
@@ -302,6 +302,15 @@ class CRM_Contribute_Form_Contribution_Main extends CRM_Contribute_Form_Contribu
       $this->buildComponentForm($this->_id, $this);
     }
 
+    if (count($this->_paymentProcessors) >= 1 && !isset($this->_paymentProcessors[0]) && !$this->get_template_vars("isCaptcha") && $this->hasToAddForcefully()) {
+      if (!$this->_userID) {
+        $this->enableCaptchaOnForm();
+      }
+      else {
+        $this->displayCaptchaWarning();
+      }
+    }
+
     // Build payment processor form
     CRM_Core_Payment_ProcessorForm::buildQuickForm($this);
 
index 561adfe00d4d0c61d90ce67c8da0452f54ded010..ec174f47baa4e70501a4b8640ffb976714c02e54 100644 (file)
@@ -777,14 +777,43 @@ class CRM_Contribute_Form_ContributionBase extends CRM_Core_Form {
         }
 
         if ($addCaptcha && !$viewOnly) {
-          $captcha = CRM_Utils_ReCAPTCHA::singleton();
-          $captcha->add($this);
-          $this->assign('isCaptcha', TRUE);
+          $this->enableCaptchaOnForm();
         }
       }
     }
   }
 
+  /**
+   * Enable ReCAPTCHA on Contribution form
+   */
+  protected function enableCaptchaOnForm() {
+    $captcha = CRM_Utils_ReCAPTCHA::singleton();
+    if ($captcha->hasSettingsAvailable()) {
+      $captcha->add($this);
+      $this->assign('isCaptcha', TRUE);
+    }
+  }
+
+  /**
+   * Display ReCAPTCHA warning on Contribution form
+   */
+  protected function displayCaptchaWarning() {
+    if (CRM_Core_Permission::check("administer CiviCRM")) {
+      $captcha = CRM_Utils_ReCAPTCHA::singleton();
+      if (!$captcha->hasSettingsAvailable()) {
+        $this->assign('displayCaptchaWarning', TRUE);
+      }
+    }
+  }
+
+  /**
+   * Check if ReCAPTCHA has to be added on Contribution form forcefully.
+   */
+  protected function hasToAddForcefully() {
+    $captcha = CRM_Utils_ReCAPTCHA::singleton();
+    return $captcha->hasToAddForcefully();
+  }
+
   /**
    * Add onbehalf/honoree profile fields and native module fields.
    *
index e9414cb783edcc0a6ca021b52bf8303ebb30adee..63f15a8a5896fb0990ce0ab774178f255349230b 100644 (file)
@@ -173,6 +173,7 @@ class CRM_Core_Config_MagicMerge {
       'recaptchaOptions' => array('setting'),
       'recaptchaPublicKey' => array('setting'),
       'recaptchaPrivateKey' => array('setting'),
+      'forceRecaptcha' => array('setting'),
       'replyTo' => array('setting'),
       'secondDegRelPermissions' => array('setting'),
       'smartGroupCacheTimeout' => array('setting'),
index de4021bb1f938298699761021db931c4ccb39d17..808615993097f2fca0e910c6781b7224125c9932 100644 (file)
@@ -62,6 +62,29 @@ class CRM_Utils_ReCAPTCHA {
     return self::$_singleton;
   }
 
+
+  /**
+   * Check if reCaptcha settings is avilable to add on form.
+   */
+  public static function hasSettingsAvailable() {
+    $config = CRM_Core_Config::singleton();
+    if ($config->recaptchaPublicKey == NULL || $config->recaptchaPublicKey == "") {
+      return FALSE;
+    }
+    return TRUE;
+  }
+
+  /**
+   * Check if reCaptcha has to be added on form forcefully.
+   */
+  public static function hasToAddForcefully() {
+    $config = CRM_Core_Config::singleton();
+    if (!$config->forceRecaptcha) {
+      return FALSE;
+    }
+    return TRUE;
+  }
+
   /**
    * Add element to form.
    *
index c95ed3d01850e49bb91d0f9c9591ac644a194aae..9d91ed59c03a6a4e1bd8314d98c5cbc2a59bc062 100644 (file)
@@ -530,6 +530,21 @@ return array(
     'description' => NULL,
     'help_text' => NULL,
   ),
+  'forceRecaptcha' => array(
+    'add' => '4.7',
+    'help_text' => NULL,
+    'is_domain' => 1,
+    'is_contact' => 0,
+    'group_name' => 'CiviCRM Preferences',
+    'group' => 'core',
+    'name' => 'forceRecaptcha',
+    'type' => 'Boolean',
+    'quick_form_type' => 'YesNo',
+    'html_type' => '',
+    'default' => '0',
+    'title' => 'Force reCAPTCHA on Contribution pages',
+    'description' => 'If enabled, reCAPTCHA will show on all contribution pages.',
+  ),
   'recaptchaPrivateKey' => array(
     'group_name' => 'CiviCRM Preferences',
     'group' => 'core',
index b94f6602412fdd5fb977c8ef69055cddac5e9951..db1c09c46236ac74bc61b970d10f9e83f89aaf11 100644 (file)
               </span>
             </td>
         </tr>
+        <tr class="crm-miscellaneous-form-block-recaptchaPrivateKey">
+            <td class="label">{$form.forceRecaptcha.label}</td>
+            <td>
+              {$form.forceRecaptcha.html}
+              <p class="description">{ts}If enabled, reCAPTCHA will show on all contribution pages.{/ts}</p>
+            </td>
+        </tr>
         </table>
            <div class="crm-submit-buttons">{include file="CRM/common/formButtons.tpl" location="bottom"}</div>
 </div>
index 9612842736c044d89c790a7dee1c7ee90af64844..52b102ed8b537b6fb332d3aa0e7051fc5977bcb8 100644 (file)
   {include file="CRM/Contribute/Form/Contribution/PreviewHeader.tpl"}
   {/if}
 
+  {if $displayCaptchaWarning}
+  <div class="messages status no-popup">
+      {ts}To display reCAPTCHA on form you must get an API key from<br /> <a href='https://www.google.com/recaptcha/admin/create'>https://www.google.com/recaptcha/admin/create</a>{/ts}
+  </div>
+  {/if}
+
   {include file="CRM/common/TrackingFields.tpl"}
 
   <div class="crm-contribution-page-id-{$contributionPageID} crm-block crm-contribution-main-form-block">