| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentForm\App\Modules\Form; |
| 4 |
|
| 5 |
use Cleantalk\Antispam\CleantalkRequest; |
| 6 |
use FluentForm\Framework\Helpers\ArrayHelper; |
| 7 |
|
| 8 |
class CleanTalkHandler |
| 9 |
{ |
| 10 |
public function setCleanTalkScript() |
| 11 |
{ |
| 12 |
$hasCleanTalk = self::isCleantalkActivated(); |
| 13 |
if ($hasCleanTalk) { |
| 14 |
wp_enqueue_script( |
| 15 |
'ct_bot_detector', |
| 16 |
'https://moderate.cleantalk.org/ct-bot-detector-wrapper.js', |
| 17 |
[], |
| 18 |
FLUENTFORM_VERSION, |
| 19 |
[ |
| 20 |
'in_footer' => false, |
| 21 |
'strategy' => 'defer' |
| 22 |
] |
| 23 |
); |
| 24 |
|
| 25 |
echo '<input type="hidden" name="ff_ct_form_load_time" class="ff_ct_form_load_time" value="">'; |
| 26 |
} |
| 27 |
} |
| 28 |
|
| 29 |
public static function validate($accessKey) |
| 30 |
{ |
| 31 |
$cleanTalkRequest = [ |
| 32 |
'method_name' => 'notice_paid_till', |
| 33 |
'auth_key' => $accessKey, |
| 34 |
]; |
| 35 |
|
| 36 |
$response = wp_remote_post( |
| 37 |
'https://api.cleantalk.org/', |
| 38 |
[ |
| 39 |
'body' => \http_build_query($cleanTalkRequest, true), |
| 40 |
'headers' => [ |
| 41 |
'Content-Type' => 'application/x-www-form-urlencoded', |
| 42 |
], |
| 43 |
] |
| 44 |
); |
| 45 |
|
| 46 |
if (is_wp_error($response)) { |
| 47 |
return false; |
| 48 |
} |
| 49 |
|
| 50 |
$response = json_decode(wp_remote_retrieve_body($response)); |
| 51 |
|
| 52 |
if ($response->data->moderate == 1 && $response->data->valid == 1 && $response->data->product_id == 1) { |
| 53 |
return true; |
| 54 |
} else { |
| 55 |
return false; |
| 56 |
} |
| 57 |
} |
| 58 |
|
| 59 |
public static function spamSubmissionCheckWithApi($formData, $form) |
| 60 |
{ |
| 61 |
global $fluentformCleantalkExecuted; |
| 62 |
|
| 63 |
$accessKey = ArrayHelper::get(get_option('_fluentform_cleantalk_details'), 'accessKey'); |
| 64 |
|
| 65 |
if (!$accessKey) { |
| 66 |
return false; |
| 67 |
} |
| 68 |
|
| 69 |
$eventToken = ArrayHelper::get($formData, 'ct_bot_detector_event_token'); |
| 70 |
$submitTime = isset($formData['ff_ct_form_load_time']) ? time() - (int)$formData['ff_ct_form_load_time'] : null; |
| 71 |
|
| 72 |
$cleanTalkRequest = [ |
| 73 |
'method_name' => 'check_message', |
| 74 |
'auth_key' => $accessKey, |
| 75 |
'sender_ip' => sanitize_text_field(wpFluentForm()->request->getIp()), |
| 76 |
'event_token' => $eventToken, |
| 77 |
'submit_time' => $submitTime, |
| 78 |
'sender_info' => json_encode([ |
| 79 |
'REFERRER' => isset($_SERVER['HTTP_REFERER']) ? esc_url_raw(wp_unslash($_SERVER['HTTP_REFERER'])) : '', |
| 80 |
'USER_AGENT' => isset($_SERVER['HTTP_USER_AGENT']) ? sanitize_text_field(wp_unslash($_SERVER['HTTP_USER_AGENT'])) : '' |
| 81 |
]), |
| 82 |
'js_on' => 1, |
| 83 |
'sender_nickname' => '', |
| 84 |
'sender_email' => '', |
| 85 |
'message' => '', |
| 86 |
'phone' => '', |
| 87 |
'agent' => 'wordpress-fluentforms-' . FLUENTFORM_VERSION, |
| 88 |
'post_info' => [ |
| 89 |
'comment_type' => 'fluent_forms_vendor_integration__use_api', |
| 90 |
'post_url' => isset($_SERVER['HTTP_REFERER']) ? esc_url_raw(wp_unslash($_SERVER['HTTP_REFERER'])) : '' |
| 91 |
], |
| 92 |
'all_headers' => strtolower(json_encode(wpFluentForm()->request->header())), |
| 93 |
]; |
| 94 |
|
| 95 |
$maps = [ |
| 96 |
'input_name' => 'sender_nickname', |
| 97 |
'input_email' => 'sender_email', |
| 98 |
'textarea' => 'message', |
| 99 |
'phone' => 'phone', |
| 100 |
]; |
| 101 |
|
| 102 |
$inputs = FormFieldsParser::getInputs($form, ['attributes']); |
| 103 |
|
| 104 |
foreach ($inputs as $input) { |
| 105 |
$element = ArrayHelper::get($input, 'element'); |
| 106 |
$key = ArrayHelper::get($input, 'attributes.name'); |
| 107 |
if (isset($maps[$element]) && !$cleanTalkRequest[$maps[$element]]) { |
| 108 |
$value = ArrayHelper::get($formData, $key); |
| 109 |
if ($value) { |
| 110 |
if (is_array($value)) { |
| 111 |
$value = implode(' ', $value); |
| 112 |
} |
| 113 |
$cleanTalkRequest[$maps[$element]] = $value; |
| 114 |
} |
| 115 |
} |
| 116 |
} |
| 117 |
|
| 118 |
$cleanTalkRequest = apply_filters('fluentform/cleantalk_fields', $cleanTalkRequest, $formData, $form); |
| 119 |
|
| 120 |
$response = wp_remote_post( |
| 121 |
'https://moderate.cleantalk.org/api2.0', |
| 122 |
[ |
| 123 |
'body' => json_encode($cleanTalkRequest), |
| 124 |
'headers' => [ |
| 125 |
'Content-Type' => 'application/json', |
| 126 |
], |
| 127 |
] |
| 128 |
); |
| 129 |
|
| 130 |
if (is_wp_error($response)) { |
| 131 |
return false; |
| 132 |
} |
| 133 |
|
| 134 |
$response = json_decode(wp_remote_retrieve_body($response)); |
| 135 |
|
| 136 |
$cleantalkPassed = $response->allow == 1 && $response->spam == 0 && $response->account_status == 1; |
| 137 |
|
| 138 |
$fluentformCleantalkExecuted = true; |
| 139 |
|
| 140 |
return !$cleantalkPassed; |
| 141 |
} |
| 142 |
|
| 143 |
public static function isCleantalkActivated() |
| 144 |
{ |
| 145 |
$settings = get_option('_fluentform_cleantalk_details'); |
| 146 |
return $settings && ArrayHelper::get($settings, 'status'); |
| 147 |
} |
| 148 |
|
| 149 |
public static function isEnabled() |
| 150 |
{ |
| 151 |
if (!self::isPluginEnabled()) { |
| 152 |
return false; |
| 153 |
} |
| 154 |
|
| 155 |
$settings = get_option('_fluentform_global_form_settings'); |
| 156 |
return $settings && 'yes' == ArrayHelper::get($settings, 'misc.cleantalk_status'); |
| 157 |
} |
| 158 |
|
| 159 |
public static function isPluginEnabled() |
| 160 |
{ |
| 161 |
$exists = method_exists('Cleantalk\Antispam\Cleantalk', 'isAllowMessage'); |
| 162 |
if ($exists) { |
| 163 |
global $apbct; |
| 164 |
return !!$apbct->data['key_is_ok']; |
| 165 |
} |
| 166 |
return false; |
| 167 |
} |
| 168 |
|
| 169 |
public static function isSpamSubmission($formData, $form) |
| 170 |
{ |
| 171 |
$cleanTalkRequest = self::getCleanTalkRequest($formData, $form); |
| 172 |
$cleanTalk = new \Cleantalk\Antispam\Cleantalk(); |
| 173 |
$cleanTalk->server_url = 'https://moderate.cleantalk.org'; |
| 174 |
$response = $cleanTalk->isAllowMessage($cleanTalkRequest); |
| 175 |
|
| 176 |
return 0 == $response->allow; |
| 177 |
} |
| 178 |
|
| 179 |
protected static function getCleanTalkRequest($data, $form) |
| 180 |
{ |
| 181 |
global $apbct; |
| 182 |
$app = wpFluentForm(); |
| 183 |
$ip = sanitize_text_field($app->request->getIp()); |
| 184 |
|
| 185 |
$serverName = isset($_SERVER['SERVER_NAME']) ? sanitize_text_field(wp_unslash($_SERVER['SERVER_NAME'])) : ''; |
| 186 |
$requestUri = isset($_SERVER['REQUEST_URI']) ? esc_url_raw(wp_unslash($_SERVER['REQUEST_URI'])) : ''; |
| 187 |
|
| 188 |
$info = [ |
| 189 |
'auth_key' => $apbct->settings['apikey'], |
| 190 |
'sender_ip' => $ip, |
| 191 |
'contact_form_subject' => $form->title, |
| 192 |
'referrer' => urlencode($data['_wp_http_referer']), |
| 193 |
'page_url' => $serverName . $requestUri, |
| 194 |
'submit_time' => isset($_SESSION['ct_submit_time']) ? time() - (int)$_SESSION['ct_submit_time'] : null, //@todo Improve this |
| 195 |
'agent' => 'php-api', |
| 196 |
'js_on' => 1, |
| 197 |
'sender_nickname' => '', |
| 198 |
'sender_email' => '', |
| 199 |
'message' => '', |
| 200 |
'phone' => '' |
| 201 |
]; |
| 202 |
|
| 203 |
$maps = [ |
| 204 |
'input_name' => 'sender_nickname', |
| 205 |
'input_email' => 'sender_email', |
| 206 |
'textarea' => 'message', |
| 207 |
'phone' => 'phone', |
| 208 |
]; |
| 209 |
$inputs = FormFieldsParser::getInputs($form, ['attributes']); |
| 210 |
|
| 211 |
foreach ($inputs as $input) { |
| 212 |
$element = ArrayHelper::get($input, 'element'); |
| 213 |
$key = ArrayHelper::get($input, 'attributes.name'); |
| 214 |
if (isset($maps[$element]) && !$info[$maps[$element]]) { |
| 215 |
$value = ArrayHelper::get($data, $key); |
| 216 |
if ($value) { |
| 217 |
if (is_array($value)) { |
| 218 |
$value = implode(' ', $value); |
| 219 |
} |
| 220 |
$info[$maps[$element]] = $value; |
| 221 |
} |
| 222 |
} |
| 223 |
} |
| 224 |
|
| 225 |
$info = apply_filters('fluentform/cleantalk_fields', $info, $data, $form); |
| 226 |
|
| 227 |
return new CleantalkRequest($info); |
| 228 |
} |
| 229 |
} |
| 230 |
|