[REF] Move another deprecated function back to the only class that calls it
[civicrm-core.git] / CRM / Utils / ReCAPTCHA.php
CommitLineData
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 */
17class 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')) {
8bed0a74 81 require_once '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');
e041a5f5 99 $form->addRule('g-recaptcha-response', ts('Please go back and complete the CAPTCHA at the bottom of this form.'), 'recaptcha');
3c275f0d
WA
100 if ($form->isSubmitted() && empty($form->_submitValues['g-recaptcha-response'])) {
101 $form->setElementError(
102 'g-recaptcha-response',
50c010c7 103 ts('Please go back and complete the CAPTCHA at the bottom of this form.')
3c275f0d
WA
104 );
105 }
6a488035 106 }
96025800 107
268ff0e8
MWMC
108 /**
109 * Enable ReCAPTCHA on Contribution form
110 *
111 * @param CRM_Core_Form $form
112 */
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);
118 }
119 }
120
e041a5f5
SL
121 /**
122 * @param $value
123 * @param CRM_Core_Form $form
124 *
125 * @return mixed
126 */
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']
131 );
132 return $resp->is_valid;
133 }
134
6a488035 135}