Captcha.php
9.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
<?php
namespace wpdFormAttr\Field\DefaultField;
use wpdFormAttr\FormConst\wpdFormConst;
use wpdFormAttr\Field\Field;
use wpdFormAttr\Field\DefaultField\ReCaptcha;
use wpdFormAttr\Tools\Sanitizer;
class Captcha extends Field {
protected $name = wpdFormConst::WPDISCUZ_FORMS_CAPTCHA_FIELD;
protected $isDefault = true;
private $reCaptcha;
protected function dashboardForm() {
?>
<div class="wpd-field-body" style="display: <?php echo esc_attr($this->display); ?>">
<a href="<?php echo esc_url_raw(admin_url("admin.php?page=" . \WpdiscuzCore::PAGE_SETTINGS . "&wpd_tab=" . \WpdiscuzCore::TAB_RECAPTCHA)); ?>"><?php esc_html_e("reCAPTCHA Settings", "wpdiscuz"); ?></a>
<input class="wpd-field-type" type="hidden" value="<?php echo esc_attr($this->type); ?>" name="<?php echo esc_attr($this->fieldInputName); ?>[type]" />
<div style="clear:both;"></div>
</div>
<?php
}
private function initRecaptcha($generalOptions) {
$secretKey = apply_filters("wpdiscuz_recaptcha_secret", $generalOptions->recaptcha["secretKey"]);
try {
$requestMethod = $this->createRequestMethod($generalOptions);
$this->reCaptcha = new ReCaptcha\ReCaptcha($secretKey, $requestMethod);
} catch (\RuntimeException $ex) {
wp_die("reCAPTCHA Exception : " . $ex->getMessage());
}
}
private function createRequestMethod($generalOptions) {
if ($generalOptions->recaptcha["requestMethod"] !== "auto") {
if ($generalOptions->recaptcha["requestMethod"] === "socket") {
return new ReCaptcha\RequestMethod\SocketPost();
} else if ($generalOptions->recaptcha["requestMethod"] === "curl") {
return new ReCaptcha\RequestMethod\CurlPost();
} else if ($generalOptions->recaptcha["requestMethod"] === "post") {
return new ReCaptcha\RequestMethod\Post();
}
} else {
if (extension_loaded("curl")) {
return new ReCaptcha\RequestMethod\CurlPost();
}
if (function_exists("fsockopen")) {
return new ReCaptcha\RequestMethod\SocketPost();
}
if (ini_get("allow_url_fopen")) {
return new ReCaptcha\RequestMethod\Post();
}
}
}
public function frontFormHtml($name, $args, $options, $currentUser, $uniqueId, $isMainForm) {
$version = apply_filters("wpdiscuz_recaptcha_version", $options->recaptcha["version"]);
$key = apply_filters("wpdiscuz_recaptcha_site_key", $options->recaptcha["siteKey"]);
$secret = apply_filters("wpdiscuz_recaptcha_secret", $options->recaptcha["secretKey"]);
if ($this->isShowCaptcha($currentUser->ID, $options) && $key && $secret && $version === "2.0") {
?>
<div class="wpd-field-captcha wpdiscuz-item">
<div class="wpdiscuz-recaptcha" id='wpdiscuz-recaptcha-<?php echo esc_attr($uniqueId); ?>'></div>
<input id='wpdiscuz-recaptcha-field-<?php echo esc_attr($uniqueId); ?>' type='hidden' name='wc_captcha' value="" required="required" aria-required='true' class="wpdiscuz_reset"/>
<div class="clearfix"></div>
</div>
<?php
}
do_action("wpdiscuz_captcha_field", $args, $currentUser, $uniqueId, $isMainForm);
}
public function sanitizeFieldData($data) {
$cleanData = [];
$cleanData["type"] = $data["type"];
if (isset($data["show_for_guests"])) {
$cleanData["show_for_guests"] = intval($data["show_for_guests"]);
}
if (isset($data["show_for_users"])) {
$cleanData["show_for_users"] = intval($data["show_for_users"]);
}
return wp_parse_args($cleanData, $this->fieldDefaultData);
}
public function validateFieldData($fieldName, $args, $options, $currentUser) {
if ($currentUser && $this->isShowCaptcha($currentUser->ID, $options)) {
$this->initRecaptcha($options);
$recaptchaResponse = Sanitizer::sanitize(INPUT_POST, "g-recaptcha-response", "FILTER_SANITIZE_STRING");
$resp = $this->reCaptchaVerify($recaptchaResponse, $options, "wpdiscuz/addComment");
if (!$resp->isSuccess()) {
$errorMesage = esc_html__("reCAPTCHA verification failed.", "wpdiscuz");
$errors = $resp->getErrorCodes();
if ($errors) {
$errorMesage = "";
$errorMesages = [
"missing-input-secret" => esc_html__("The secret parameter is missing.", "wpdiscuz"),
"invalid-input-secret" => esc_html__("The secret parameter is invalid or malformed.", "wpdiscuz"),
"missing-input-response" => esc_html__("The response parameter is missing.", "wpdiscuz"),
"invalid-input-response" => esc_html__("The response parameter is invalid or malformed.", "wpdiscuz"),
"bad-request" => esc_html__("The request is invalid or malformed.", "wpdiscuz"),
"timeout-or-duplicate" => esc_html__("The response is no longer valid: either is too old or has been used previously.", "wpdiscuz"),
];
foreach ($errors as $error) {
if (isset($errorMesages[$error])) {
$errorMesage .= esc_html__("reCaptcha validation fails. ", "wpdiscuz") . $errorMesages[$error] . "<br>";
} else {
$errorMesage .= esc_html__("reCaptcha validation fails. Error code: ", "wpdiscuz") . $error . "<br>";
}
}
}
wp_die($errorMesage);
}
}
}
public function subscribtionRecaptchaHtml($options) {
$version = apply_filters("wpdiscuz_recaptcha_version", $options->recaptcha["version"]);
$key = apply_filters("wpdiscuz_recaptcha_site_key", $options->recaptcha["siteKey"]);
$secret = apply_filters("wpdiscuz_recaptcha_secret", $options->recaptcha["secretKey"]);
if (!is_user_logged_in() && $options->recaptcha["isShowOnSubscribeForm"] && $key && $secret) {
if ($version === "2.0") {
?>
<div class="wpd-field-captcha wpdiscuz-item">
<div class="wpdiscuz-recaptcha" id='wpdiscuz-recaptcha-subscribe-form'></div>
<input id='wpdiscuz-recaptcha-field-subscribe-form' type='hidden' name='wpdiscuz_recaptcha_subscribe_form' value="" required="required" aria-required='true' class="wpdiscuz_reset"/>
<div class="clearfix"></div>
</div>
<?php
} else {
?>
<input id='wpdiscuz-recaptcha-field-subscribe-form' type='hidden' name='g-recaptcha-response' value="" class="wpdiscuz_reset"/>
<?php
}
}
}
public function reCaptchaValidate($options) {
$valid = true;
$recaptchaResponse = Sanitizer::sanitize(INPUT_POST, "g-recaptcha-response", "FILTER_SANITIZE_STRING");
$this->initRecaptcha($options);
if ($recaptchaResponse) {
$resp = $this->reCaptchaVerify($recaptchaResponse, $options, "wpdiscuz/wpdAddSubscription");
if (!$resp->isSuccess()) {
$valid = false;
}
} else {
$valid = false;
}
return $valid;
}
protected function initDefaultData() {
$this->fieldDefaultData = [
"name" => "",
"desc" => "",
"show_for_guests" => "0",
"show_for_users" => "0"
];
}
private function reCaptchaVerify($token, $options, $action = "") {
$recaptchaVersion = apply_filters("wpdiscuz_recaptcha_version", $options->recaptcha["version"]);
if ($recaptchaVersion === "2.0") {
$resp = $this->reCaptcha->verify($token, $this->getIP());
} else {
$score = apply_filters("wpdiscuz_recaptcha_score", $options->recaptcha["score"]);
$resp = $this->reCaptcha->setExpectedAction($action)
->setScoreThreshold($score)
->verify($token, $this->getIP());
}
return $resp;
}
private function getIP() {
$ip = "";
if (!empty($_SERVER["HTTP_CLIENT_IP"])) {
$ip = $_SERVER["HTTP_CLIENT_IP"];
} elseif (!empty($_SERVER["HTTP_X_FORWARDED_FOR"])) {
$ip = $_SERVER["HTTP_X_FORWARDED_FOR"];
} else {
$ip = $_SERVER["REMOTE_ADDR"];
}
return $ip;
}
/**
* check if the captcha field show or not
* @return type boolean
*/
public function isShowCaptcha($isUserLoggedIn, $options) {
return ($isUserLoggedIn && $options->recaptcha["showForUsers"]) || (!$isUserLoggedIn && $options->recaptcha["showForGuests"]);
}
public function editCommentHtml($key, $value, $data, $comment) {
}
public function frontHtml($value, $args) {
}
}