Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CRM-21104: Forcing reCaptcha on Contribution pages(with online payments) which have no Profile associated with them. #11197

Merged
merged 3 commits into from
Jul 18, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CRM/Admin/Form/Setting/Miscellaneous.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
9 changes: 9 additions & 0 deletions CRM/Contribute/Form/Contribution/Main.php
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,15 @@ public function buildQuickForm() {
$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);

Expand Down
35 changes: 32 additions & 3 deletions CRM/Contribute/Form/ContributionBase.php
Original file line number Diff line number Diff line change
Expand Up @@ -770,14 +770,43 @@ public function buildCustom($id, $name, $viewOnly = FALSE, $profileContactType =
}

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.
*
Expand Down
1 change: 1 addition & 0 deletions CRM/Core/Config/MagicMerge.php
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,7 @@ public static function getPropertyMap() {
'recaptchaOptions' => array('setting'),
'recaptchaPublicKey' => array('setting'),
'recaptchaPrivateKey' => array('setting'),
'forceRecaptcha' => array('setting'),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not a big deal, but for future reference -- there's no general desire to pass all settings through $config / MagicMerge. MagicMerge is just a compatibility mechanism for legacy $config consumers. For new logic, use Civi::settings()->get('foo');

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for point this out, we will consider this for future updates.

'replyTo' => array('setting'),
'secondDegRelPermissions' => array('setting'),
'smartGroupCacheTimeout' => array('setting'),
Expand Down
23 changes: 23 additions & 0 deletions CRM/Utils/ReCAPTCHA.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,29 @@ public static function &singleton() {
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.
*
Expand Down
15 changes: 15 additions & 0 deletions settings/Core.setting.php
Original file line number Diff line number Diff line change
Expand Up @@ -515,6 +515,21 @@
'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',
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if this is the right default for new installs - perhaps it should default to 1 (& maybe upgrade script to change over existing). Non blocking comment

'title' => 'Force reCAPTCHA on Contribution pages',
'description' => 'If enabled, reCAPTCHA will show on all contribution pages.',
),
'recaptchaPrivateKey' => array(
'group_name' => 'CiviCRM Preferences',
'group' => 'core',
Expand Down
7 changes: 7 additions & 0 deletions templates/CRM/Admin/Form/Setting/Miscellaneous.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,13 @@
</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>
6 changes: 6 additions & 0 deletions templates/CRM/Contribute/Form/Contribution/Main.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,12 @@
{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">
Expand Down