2764e242641c8d061804f234c04f72b7016465c3
3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
6 | This work is published under the GNU AGPLv3 license with some |
7 | permitted exceptions and without any warranty. For full license |
8 | and copyright information, see https://civicrm.org/licensing |
9 +--------------------------------------------------------------------+
15 * @copyright CiviCRM LLC https://civicrm.org/licensing
17 class CRM_Utils_ReCAPTCHA
{
19 protected $_captcha = NULL;
21 protected $_name = NULL;
23 protected $_url = NULL;
25 protected $_phrase = NULL;
30 * We only need one instance of this object. So we use the singleton
31 * pattern and cache the instance in this variable
33 * @var CRM_Utils_ReCAPTCHA
35 static private $_singleton = NULL;
38 * Singleton function used to manage this object.
42 public static function &singleton() {
43 if (self
::$_singleton === NULL) {
44 self
::$_singleton = new CRM_Utils_ReCAPTCHA();
46 return self
::$_singleton;
50 * Check if reCaptcha settings is avilable to add on form.
52 public static function hasSettingsAvailable() {
53 $config = CRM_Core_Config
::singleton();
54 if ($config->recaptchaPublicKey
== NULL ||
$config->recaptchaPublicKey
== "") {
61 * Check if reCaptcha has to be added on form forcefully.
63 public static function hasToAddForcefully() {
64 $config = CRM_Core_Config
::singleton();
65 if (!$config->forceRecaptcha
) {
72 * Add element to form.
74 * @param CRM_Core_Form $form
76 public static function add(&$form) {
78 $config = CRM_Core_Config
::singleton();
80 if (!function_exists('recaptcha_get_html')) {
81 require_once 'recaptcha/recaptchalib.php';
84 // Load the Recaptcha api.js over HTTPS
87 $html = recaptcha_get_html($config->recaptchaPublicKey
, $error, $useHTTPS);
89 $form->assign('recaptchaHTML', $html);
90 $form->assign('recaptchaOptions', $config->recaptchaOptions
);
93 'g-recaptcha-response',
98 $form->registerRule('recaptcha', 'callback', 'validate', 'CRM_Utils_ReCAPTCHA');
99 $form->addRule('g-recaptcha-response', ts('Please go back and complete the CAPTCHA at the bottom of this form.'), 'recaptcha');
100 if ($form->isSubmitted() && empty($form->_submitValues
['g-recaptcha-response'])) {
101 $form->setElementError(
102 'g-recaptcha-response',
103 ts('Please go back and complete the CAPTCHA at the bottom of this form.')
109 * Enable ReCAPTCHA on Contribution form
111 * @param CRM_Core_Form $form
113 public static function enableCaptchaOnForm(&$form) {
114 $captcha = CRM_Utils_ReCAPTCHA
::singleton();
115 if ($captcha->hasSettingsAvailable()) {
116 $captcha->add($form);
117 $form->assign('isCaptcha', TRUE);
123 * @param CRM_Core_Form $form
127 public static function validate($value, $form) {
128 $resp = recaptcha_check_answer(CRM_Core_Config
::singleton()->recaptchaPrivateKey
,
129 $_SERVER['REMOTE_ADDR'],
130 $_POST['g-recaptcha-response']
132 return $resp->is_valid
;