Merge pull request #15881 from civicrm/5.20
[civicrm-core.git] / CRM / Utils / ReCAPTCHA.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
5 | |
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 +--------------------------------------------------------------------+
10 */
11
12 /**
13 *
14 * @package CRM
15 * @copyright CiviCRM LLC https://civicrm.org/licensing
16 */
17 class CRM_Utils_ReCAPTCHA {
18
19 protected $_captcha = NULL;
20
21 protected $_name = NULL;
22
23 protected $_url = NULL;
24
25 protected $_phrase = NULL;
26
27 /**
28 * Singleton.
29 *
30 * We only need one instance of this object. So we use the singleton
31 * pattern and cache the instance in this variable
32 *
33 * @var CRM_Utils_ReCAPTCHA
34 */
35 static private $_singleton = NULL;
36
37 /**
38 * Singleton function used to manage this object.
39 *
40 * @return object
41 */
42 public static function &singleton() {
43 if (self::$_singleton === NULL) {
44 self::$_singleton = new CRM_Utils_ReCAPTCHA();
45 }
46 return self::$_singleton;
47 }
48
49 /**
50 * Check if reCaptcha settings is avilable to add on form.
51 */
52 public static function hasSettingsAvailable() {
53 $config = CRM_Core_Config::singleton();
54 if ($config->recaptchaPublicKey == NULL || $config->recaptchaPublicKey == "") {
55 return FALSE;
56 }
57 return TRUE;
58 }
59
60 /**
61 * Check if reCaptcha has to be added on form forcefully.
62 */
63 public static function hasToAddForcefully() {
64 $config = CRM_Core_Config::singleton();
65 if (!$config->forceRecaptcha) {
66 return FALSE;
67 }
68 return TRUE;
69 }
70
71 /**
72 * Add element to form.
73 *
74 * @param CRM_Core_Form $form
75 */
76 public static function add(&$form) {
77 $error = NULL;
78 $config = CRM_Core_Config::singleton();
79 $useSSL = FALSE;
80 if (!function_exists('recaptcha_get_html')) {
81 require_once 'packages/recaptcha/recaptchalib.php';
82 }
83
84 // Load the Recaptcha api.js over HTTPS
85 $useHTTPS = TRUE;
86
87 $html = recaptcha_get_html($config->recaptchaPublicKey, $error, $useHTTPS);
88
89 $form->assign('recaptchaHTML', $html);
90 $form->assign('recaptchaOptions', $config->recaptchaOptions);
91 $form->add(
92 'text',
93 'g-recaptcha-response',
94 'reCaptcha',
95 NULL,
96 TRUE
97 );
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.')
103 );
104 }
105 }
106
107 /**
108 * Enable ReCAPTCHA on Contribution form
109 *
110 * @param CRM_Core_Form $form
111 */
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);
117 }
118 }
119
120 }