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 if ($form->isSubmitted() && empty($form->_submitValues
['g-recaptcha-response'])) {
100 $form->setElementError(
101 'g-recaptcha-response',
102 ts('Please go back and complete the CAPTCHA at the bottom of this form.')
108 * Enable ReCAPTCHA on Contribution form
110 * @param CRM_Core_Form $form
112 public static function enableCaptchaOnForm(&$form) {
113 $captcha = CRM_Utils_ReCAPTCHA
::singleton();
114 if ($captcha->hasSettingsAvailable()) {
115 $captcha->add($form);
116 $form->assign('isCaptcha', TRUE);