-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathantispam.php
175 lines (145 loc) · 5.12 KB
/
antispam.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
<?php
defined( 'ABSPATH' ) || exit;
add_action('wp_ajax_antispam_key_form', 'antispam_sync');
if (antispam_key() && !defined('APBCT_VERSION')) {
add_action('wp_head', 'antispam_render_bot_detector');
}
function antispam_sync()
{
$key = sanitize_text_field($_POST['antispam_key']);
$result = [
'success' => false,
'message' => '',
];
if (!wp_verify_nonce($_POST['antispam_key_form_nonce'], 'antispam_key_form')) {
$result['message'] = 'nonce is not valid';
wp_send_json($result);
}
if (empty($key)) {
$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 (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('antispam_key', $key);
$result['success'] = true;
wp_send_json($result);
}
function antispam_render_key_form()
{
$key = antispam_key();
$agitation = '<span>Antispam is inactive, please enter your key to avoid spam from your life. <a href="https://cleantalk.org/" target="_blank">Get your key</a></span>';
$message = $key ? 'Antispam is active.' : $agitation;
return '<p><form id="antispam-key-form" method="post">'
. $message . '<br><input type="text" name="antispam_key" value="' . $key . '"> <input type="submit" value="Save">'
. wp_nonce_field('antispam_key_form', 'antispam_key_form_nonce') . '<input type="hidden" name="action" value="antispam_key_form">'
. '</form></p>'
. '<script> jQuery(document).ready(function($) {
$("form#antispam-key-form").submit(function(e) {
e.preventDefault();
$.post("' . admin_url('admin-ajax.php') . '", $(this).serialize(), function(response) {
$(".antispam-error").remove();
if (response.success) {
$("input[name=\'antispam_key\']").parent().find("span").html("Antispam is active.");
} else {
$("input[name=\'antispam_key\']").parent().after("<div class=\'error antispam-error\'>" + response.message + "</div>");
}
});
});
});
</script>';
}
function antispam_key()
{
$key = get_option('antispam_key', '');
if (is_string($key)) {
return $key;
}
return '';
}
function antispam_render_bot_detector()
{
echo '<script src="https://moderate.cleantalk.org/ct-bot-detector-wrapper.js" id="ct_bot_detector-js"></script>';
}
function antispam_check_is_spam($data)
{
global $cleantalk_executed;
$key = antispam_key();
if ($cleantalk_executed || defined('APBCT_VERSION') || !$key) {
return false;
}
$params = antispam_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 true;
}
$cleantalk_executed = true;
return false;
}
function antispam_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];
}
});
return [
'sender_ip' => $_SERVER['REMOTE_ADDR'],
'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' => antispam_key(),
'agent' => 'antispam-3rd-party-0.1.0',
'sender_email' => $email,
'event_token' => $data['ct_bot_detector_event_token'],
];
}