-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapbct_sdk.php
209 lines (175 loc) · 7.19 KB
/
apbct_sdk.php
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
206
207
208
209
<?php
defined('ABSPATH') || exit;
define('APBCT_SDK_NAME', 'apbct_sdk');
define('APBCT_SDK_VERSION', '0.1.0');
add_action('wp_ajax_apbct_sdk_key_form', 'apbct_sdk_sync');
if ( apbct_sdk_key() && !defined('APBCT_VERSION') ) {
add_action('wp_head', 'apbct_sdk_render_bot_detector');
}
function apbct_sdk_sync()
{
$key = isset($_POST['apbct_sdk_key']) && is_string($_POST['apbct_sdk_key'])
? sanitize_text_field($_POST['apbct_sdk_key'])
: '';
$result = [
'success' => false,
'message' => '',
];
if ( isset($_POST['apbct_sdk_key_form_nonce']) && is_string($_POST['apbct_sdk_key_form_nonce']) && ! wp_verify_nonce($_POST['apbct_sdk_key_form_nonce'], 'apbct_sdk_key_form') ) {
$result['message'] = 'nonce is not valid';
wp_send_json($result);
}
if ( empty($key) ) {
update_option('apbct_sdk_key', '');
$result['success'] = true;
$result['message'] = 'key is empty';
wp_send_json($result);
}
$response = wp_remote_post('https://api.cleantalk.org/', array(
'body' => array(
'method_name' => 'notice_paid_till',
'auth_key' => $key,
),
));
if ( is_wp_error($response) || !$response ) {
$result['message'] = wp_remote_retrieve_response_message($response);
return $result;
}
$body = wp_remote_retrieve_body($response);
if ( empty($body) ) {
$result['message'] = 'not found content';
wp_send_json($result);
}
$response_code = wp_remote_retrieve_response_code($response);
if ( $response_code >= 400 ) {
$msg = '';
if ( $response instanceof \WP_Error ) {
$msg = $response->get_error_message();
} elseif ( is_object($response) && isset($response->detail) ) {
$msg = $response->detail;
}
$result['message'] = 'response code error: ' . $response_code . ' - ' . $msg;
wp_send_json($result);
}
$response = json_decode($body);
if ( is_null($response) ) {
$result['message'] = 'decoded response null';
wp_send_json($result);
}
if ( isset($response->data->valid) && $response->data->valid == 0 ) {
$result['message'] = 'key is not valid';
wp_send_json($result);
}
update_option('apbct_sdk_key', $key);
$result['success'] = true;
wp_send_json($result);
}
function apbct_sdk_render_key_form()
{
$key = apbct_sdk_key();
$agitation = 'CleanTalk is cloud Anti-Spam service which focuses on a background scoring for websites visitors to highlight legitimate visitors and filter spambots.<br>Click here to get your key and start filter spam bots! <a href="https://cleantalk.org/register" target="_blank">https://cleantalk.org/register</a>';
$agitation = wp_kses($agitation, array('a' => array('href' => array(), 'target' => array()), 'br' => array()));
$key_is_ok_desc = 'Anti-Spam is active, use <a href="https://cleantalk.org/my" target="_blank">Dashboard</a> to tune the service.';
$key_is_ok_desc = wp_kses($key_is_ok_desc, array('a' => array('href' => array(), 'target' => array())));
$message = $key ? $key_is_ok_desc : $agitation;
return '<p><span class="apbct_sdk_description">' . $message . '</span>
. <form id="apbct_sdk-key-form" method="post">'
. '<input type="text" name="apbct_sdk_key" value="' . $key . '" placeholder="API key"> <input type="submit" value="Save" class="apbct_sdk_submit">'
. wp_nonce_field(
'apbct_sdk_key_form',
'apbct_sdk_key_form_nonce'
) . '<input type="hidden" name="action" value="apbct_sdk_key_form">'
. '</form></p>'
. '<script> jQuery(document).ready(function($) {
$("form#apbct_sdk-key-form").submit(function(e) {
e.preventDefault();
$(".apbct_sdk_submit").attr("disabled","disabled").css("cursor", "wait");
$.post("' . admin_url('admin-ajax.php') . '", $(this).serialize(), function(response) {
$(".apbct_sdk-error").remove();
$(".apbct_sdk_submit").removeAttr("disabled").removeAttr("style");
if (response.success) {
const message = response.message === "key is empty" ? "' . addslashes($agitation) . '" : "' . addslashes($key_is_ok_desc) . '";
$("input[name=\'apbct_sdk_description\']").html(message);
} else {
$("input[name=\'apbct_sdk_description\']").html("' . addslashes($agitation) . '");
$("input[name=\'apbct_sdk_key\']").parent().after("<div class=\'error apbct_sdk-error\'>" + response.message + "</div>");
}
});
});
});
</script>';
}
function apbct_sdk_key()
{
$key = get_option('apbct_sdk_key', '');
if ( is_string($key) ) {
return $key;
}
return '';
}
function apbct_sdk_render_bot_detector()
{
echo '<script src="https://moderate.cleantalk.org/ct-bot-detector-wrapper.js" id="ct_bot_detector-js"></script>';
}
function apbct_sdk_check_is_spam($data)
{
global $cleantalk_executed;
$key = apbct_sdk_key();
if ( $cleantalk_executed || defined('APBCT_VERSION') || !$key ) {
return false;
}
$params = apbct_sdk_gather_params($data);
$response = wp_remote_post('https://moderate.cleantalk.org/api2.0', array(
'body' => json_encode($params),
));
if ( is_wp_error($response) ) {
return false;
}
$body = wp_remote_retrieve_body($response);
if ( empty($body) ) {
return false;
}
$response_code = wp_remote_retrieve_response_code($response);
if ( $response_code >= 400 ) {
return false;
}
$response = json_decode($body);
if ( is_null($response) ) {
return false;
}
if ( isset($response->allow) && $response->allow == 0 ) {
return $response->comment;
}
$cleantalk_executed = true;
return false;
}
function apbct_sdk_gather_params($data)
{
$email_pattern = '/[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}/';
$email = null;
array_walk_recursive($data, function ($value) use (&$email, $email_pattern) {
if ( is_string($value) && preg_match($email_pattern, $value, $matches) ) {
$email = $matches[0];
}
});
if ( function_exists('apache_request_headers') ) {
$all_headers = array_filter(apache_request_headers(), function ($value, $key) {
return strtolower($key) !== 'cookie';
}, ARRAY_FILTER_USE_BOTH);
}
/** @psalm-suppress PossiblyUndefinedArrayOffset */
$sender_ip = $_SERVER['REMOTE_ADDR'];
return [
'sender_ip' => $sender_ip,
'x_forwarded_for' => isset($_SERVER['HTTP_X_FORWARDED_FOR']) ? $_SERVER['HTTP_X_FORWARDED_FOR'] : null,
'x_real_ip' => isset($_SERVER['HTTP_X_REAL_IP']) ? $_SERVER['HTTP_X_REAL_IP'] : null,
'auth_key' => apbct_sdk_key(),
'agent' => APBCT_SDK_NAME . '_' . APBCT_SDK_VERSION,
'sender_email' => $email,
'event_token' => $data['ct_bot_detector_event_token'],
'all_headers' => !empty($all_headers) ? json_encode($all_headers) : '',
'sender_info' => [
'REFFERRER' => isset($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : '',
]
];
}