From ce287b857ceeb1e2aca62d77b040d3e91ee867ba Mon Sep 17 00:00:00 2001 From: Alok Patel Date: Thu, 26 Oct 2017 12:55:05 +0530 Subject: [PATCH] CRM-21104: Adding new setting in misc to force reCAPTCHA on Contribution pages. --- CRM/Admin/Form/Setting/Miscellaneous.php | 1 + CRM/Contribute/Form/Contribution/Main.php | 9 ++++++-- CRM/Contribute/Form/ContributionBase.php | 21 +++++++++++++++++-- CRM/Core/Config/MagicMerge.php | 1 + CRM/Utils/ReCAPTCHA.php | 11 ++++++++++ settings/Core.setting.php | 15 +++++++++++++ .../CRM/Admin/Form/Setting/Miscellaneous.tpl | 7 +++++++ 7 files changed, 61 insertions(+), 4 deletions(-) diff --git a/CRM/Admin/Form/Setting/Miscellaneous.php b/CRM/Admin/Form/Setting/Miscellaneous.php index 6f9b6457e4..2bf0c1039f 100644 --- a/CRM/Admin/Form/Setting/Miscellaneous.php +++ b/CRM/Admin/Form/Setting/Miscellaneous.php @@ -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, diff --git a/CRM/Contribute/Form/Contribution/Main.php b/CRM/Contribute/Form/Contribution/Main.php index b1649521e2..b21bebc43a 100644 --- a/CRM/Contribute/Form/Contribution/Main.php +++ b/CRM/Contribute/Form/Contribution/Main.php @@ -302,8 +302,13 @@ 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->_userID) { - $this->enableCaptchaOnForm(); + 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 diff --git a/CRM/Contribute/Form/ContributionBase.php b/CRM/Contribute/Form/ContributionBase.php index 48fd87673f..981ab00291 100644 --- a/CRM/Contribute/Form/ContributionBase.php +++ b/CRM/Contribute/Form/ContributionBase.php @@ -785,11 +785,28 @@ class CRM_Contribute_Form_ContributionBase extends CRM_Core_Form { $captcha->add($this); $this->assign('isCaptcha', TRUE); } - else { - $this->assign('displayCaptchaWarning', 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. * diff --git a/CRM/Core/Config/MagicMerge.php b/CRM/Core/Config/MagicMerge.php index 33155cea4f..5edb585227 100644 --- a/CRM/Core/Config/MagicMerge.php +++ b/CRM/Core/Config/MagicMerge.php @@ -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'), diff --git a/CRM/Utils/ReCAPTCHA.php b/CRM/Utils/ReCAPTCHA.php index 5da3e41d69..a6ab1a6b27 100644 --- a/CRM/Utils/ReCAPTCHA.php +++ b/CRM/Utils/ReCAPTCHA.php @@ -74,6 +74,17 @@ class CRM_Utils_ReCAPTCHA { 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. * diff --git a/settings/Core.setting.php b/settings/Core.setting.php index d09cc36c45..2f16e7cacc 100644 --- a/settings/Core.setting.php +++ b/settings/Core.setting.php @@ -515,6 +515,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', diff --git a/templates/CRM/Admin/Form/Setting/Miscellaneous.tpl b/templates/CRM/Admin/Form/Setting/Miscellaneous.tpl index f58f79590c..4f1faff148 100644 --- a/templates/CRM/Admin/Form/Setting/Miscellaneous.tpl +++ b/templates/CRM/Admin/Form/Setting/Miscellaneous.tpl @@ -111,6 +111,13 @@ + + {$form.forceRecaptcha.label} + + {$form.forceRecaptcha.html} +

{ts}If enabled, reCAPTCHA will show on all contribution pages.{/ts}

+ +
{include file="CRM/common/formButtons.tpl" location="bottom"}
-- 2.25.1