Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
bc77d7c0 | 4 | | Copyright CiviCRM LLC. All rights reserved. | |
6a488035 | 5 | | | |
bc77d7c0 TO |
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 | | |
6a488035 | 9 | +--------------------------------------------------------------------+ |
d25dd0ee | 10 | */ |
6a488035 TO |
11 | |
12 | /** | |
13 | * | |
14 | * @package CRM | |
ca5cec67 | 15 | * @copyright CiviCRM LLC https://civicrm.org/licensing |
6a488035 TO |
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 | /** | |
e8e8f3ad | 28 | * Singleton. |
29 | * | |
6a488035 TO |
30 | * We only need one instance of this object. So we use the singleton |
31 | * pattern and cache the instance in this variable | |
32 | * | |
e8e8f3ad | 33 | * @var CRM_Utils_ReCAPTCHA |
6a488035 TO |
34 | */ |
35 | static private $_singleton = NULL; | |
36 | ||
37 | /** | |
fe482240 | 38 | * Singleton function used to manage this object. |
6a488035 | 39 | * |
6a488035 | 40 | * @return object |
6a488035 | 41 | */ |
00be9182 | 42 | public static function &singleton() { |
6a488035 TO |
43 | if (self::$_singleton === NULL) { |
44 | self::$_singleton = new CRM_Utils_ReCAPTCHA(); | |
45 | } | |
46 | return self::$_singleton; | |
47 | } | |
48 | ||
8237b510 AP |
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 | ||
ce287b85 AP |
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 | ||
6a488035 | 71 | /** |
fe482240 | 72 | * Add element to form. |
54957108 | 73 | * |
74 | * @param CRM_Core_Form $form | |
6a488035 | 75 | */ |
00be9182 | 76 | public static function add(&$form) { |
353ffa53 | 77 | $error = NULL; |
6a488035 TO |
78 | $config = CRM_Core_Config::singleton(); |
79 | $useSSL = FALSE; | |
481a74f4 | 80 | if (!function_exists('recaptcha_get_html')) { |
14e15f46 | 81 | require_once 'packages/recaptcha/recaptchalib.php'; |
d2485799 | 82 | } |
6a488035 | 83 | |
4ab92bbc AM |
84 | // Load the Recaptcha api.js over HTTPS |
85 | $useHTTPS = TRUE; | |
86 | ||
87 | $html = recaptcha_get_html($config->recaptchaPublicKey, $error, $useHTTPS); | |
6a488035 TO |
88 | |
89 | $form->assign('recaptchaHTML', $html); | |
90 | $form->assign('recaptchaOptions', $config->recaptchaOptions); | |
91 | $form->add( | |
92 | 'text', | |
3c275f0d WA |
93 | 'g-recaptcha-response', |
94 | 'reCaptcha', | |
6a488035 TO |
95 | NULL, |
96 | TRUE | |
97 | ); | |
6a488035 | 98 | $form->registerRule('recaptcha', 'callback', 'validate', 'CRM_Utils_ReCAPTCHA'); |
3c275f0d WA |
99 | if ($form->isSubmitted() && empty($form->_submitValues['g-recaptcha-response'])) { |
100 | $form->setElementError( | |
101 | 'g-recaptcha-response', | |
50c010c7 | 102 | ts('Please go back and complete the CAPTCHA at the bottom of this form.') |
3c275f0d WA |
103 | ); |
104 | } | |
6a488035 | 105 | } |
96025800 | 106 | |
268ff0e8 MWMC |
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 | ||
6a488035 | 120 | } |