Merge pull request #24115 from kcristiano/5.52-token
[civicrm-core.git] / ext / recaptcha / 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 use CRM_Recaptcha_ExtensionUtil as E;
13
14 /**
15 *
16 * @package CRM
17 * @copyright CiviCRM LLC https://civicrm.org/licensing
18 */
19 class CRM_Utils_ReCAPTCHA {
20
21 protected $_captcha = NULL;
22
23 protected $_name = NULL;
24
25 protected $_url = NULL;
26
27 protected $_phrase = NULL;
28
29 /**
30 * Singleton.
31 *
32 * We only need one instance of this object. So we use the singleton
33 * pattern and cache the instance in this variable
34 *
35 * @var CRM_Utils_ReCAPTCHA
36 */
37 private static $_singleton = NULL;
38
39 /**
40 * Singleton function used to manage this object.
41 *
42 * @return object
43 */
44 public static function &singleton() {
45 if (self::$_singleton === NULL) {
46 self::$_singleton = new CRM_Utils_ReCAPTCHA();
47 }
48 return self::$_singleton;
49 }
50
51 /**
52 * Check if reCaptcha settings is avilable to add on form.
53 */
54 public static function hasSettingsAvailable() {
55 return (bool) \Civi::settings()->get('recaptchaPublicKey');
56 }
57
58 /**
59 * Add element to form.
60 *
61 * @param CRM_Core_Form $form
62 */
63 public static function add(&$form) {
64 $error = NULL;
65
66 // If we already added reCAPTCHA then don't add it again.
67 // The `recaptcha_get_html` function only exists once recaptchalib.php has been included via this function.
68 if (function_exists('recaptcha_get_html')) {
69 return;
70 }
71 require_once E::path('lib/recaptcha/recaptchalib.php');
72
73 // Load the Recaptcha api.js over HTTPS
74 $useHTTPS = TRUE;
75
76 $html = recaptcha_get_html(\Civi::settings()->get('recaptchaPublicKey'), $error, $useHTTPS);
77
78 $form->assign('recaptchaHTML', $html);
79 $form->assign('recaptchaOptions', \Civi::settings()->get('recaptchaOptions'));
80 $form->add(
81 'text',
82 'g-recaptcha-response',
83 'reCaptcha',
84 NULL,
85 TRUE
86 );
87 $form->registerRule('recaptcha', 'callback', 'validate', 'CRM_Utils_ReCAPTCHA');
88 $form->addRule('g-recaptcha-response', E::ts('Please go back and complete the CAPTCHA at the bottom of this form.'), 'recaptcha');
89 if ($form->isSubmitted() && empty($form->_submitValues['g-recaptcha-response'])) {
90 $form->setElementError(
91 'g-recaptcha-response',
92 E::ts('Please go back and complete the CAPTCHA at the bottom of this form.')
93 );
94 }
95 }
96
97 /**
98 * Enable ReCAPTCHA on Contribution form
99 *
100 * @param CRM_Core_Form $form
101 */
102 public static function enableCaptchaOnForm(&$form) {
103 $captcha = CRM_Utils_ReCAPTCHA::singleton();
104 if ($captcha->hasSettingsAvailable()) {
105 $captcha->add($form);
106 $form->assign('isCaptcha', TRUE);
107 }
108 }
109
110 /**
111 * @param $value
112 * @param CRM_Core_Form $form
113 *
114 * @return mixed
115 */
116 public static function validate($value, $form) {
117 $resp = recaptcha_check_answer(CRM_Core_Config::singleton()->recaptchaPrivateKey,
118 $_SERVER['REMOTE_ADDR'],
119 $_POST['g-recaptcha-response']
120 );
121 return $resp->is_valid;
122 }
123
124 }