PluginProbe
Fluent Forms – Customizable Contact Forms, Survey, Quiz, & Conversational Form Builder / 6.2.15
Fluent Forms – Customizable Contact Forms, Survey, Quiz, & Conversational Form Builder v6.2.15
6.2.15 6.2.14 6.2.13 6.2.12 6.2.10 6.2.11 6.2.9 6.2.8 6.2.7 6.2.6 6.2.5 6.2.4 6.2.3 6.2.2 3.6.22 3.6.31 3.6.40 3.6.41 3.6.42 3.6.50 3.6.51 3.6.60 3.6.61 3.6.62 3.6.64 All 197 releases
fluentform / app / Modules / Form / CleanTalkHandler.php

CleanTalkHandler.php in Fluent Forms – Customizable Contact Forms, Survey, Quiz, & Conversational Form Builder 6.2.15, at app/Modules/Form/CleanTalkHandler.php

254 lines 8.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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://fd.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 $accessKey = ArrayHelper::get(get_option('_fluentform_cleantalk_details'), 'accessKey');
62
63 if (!$accessKey) {
64 return false;
65 }
66
67 if (!self::claimExecution()) {
68 return false;
69 }
70
71 $eventToken = ArrayHelper::get($formData, 'ct_bot_detector_event_token');
72 $submitTime = isset($formData['ff_ct_form_load_time']) ? time() - (int)$formData['ff_ct_form_load_time'] : null;
73
74 $cleanTalkRequest = [
75 'method_name' => 'check_message',
76 'auth_key' => $accessKey,
77 'sender_ip' => sanitize_text_field(wpFluentForm()->request->getIp()),
78 'event_token' => $eventToken,
79 'submit_time' => $submitTime,
80 'sender_info' => json_encode([
81 'REFERRER' => isset($_SERVER['HTTP_REFERER']) ? esc_url_raw(wp_unslash($_SERVER['HTTP_REFERER'])) : '',
82 'USER_AGENT' => isset($_SERVER['HTTP_USER_AGENT']) ? sanitize_text_field(wp_unslash($_SERVER['HTTP_USER_AGENT'])) : ''
83 ]),
84 'js_on' => 1,
85 'sender_nickname' => '',
86 'sender_email' => '',
87 'message' => '',
88 'phone' => '',
89 'agent' => 'wordpress-fluentforms-' . FLUENTFORM_VERSION,
90 'post_info' => [
91 'comment_type' => 'fluent_forms_vendor_integration__use_api',
92 'post_url' => isset($_SERVER['HTTP_REFERER']) ? esc_url_raw(wp_unslash($_SERVER['HTTP_REFERER'])) : ''
93 ],
94 'all_headers' => strtolower(json_encode(wpFluentForm()->request->header())),
95 ];
96
97 $maps = [
98 'input_name' => 'sender_nickname',
99 'input_email' => 'sender_email',
100 'textarea' => 'message',
101 'phone' => 'phone',
102 ];
103
104 $inputs = FormFieldsParser::getInputs($form, ['attributes']);
105
106 foreach ($inputs as $input) {
107 $element = ArrayHelper::get($input, 'element');
108 $key = ArrayHelper::get($input, 'attributes.name');
109 if (isset($maps[$element]) && !$cleanTalkRequest[$maps[$element]]) {
110 $value = ArrayHelper::get($formData, $key);
111 if ($value) {
112 if (is_array($value)) {
113 $value = implode(' ', $value);
114 }
115 $cleanTalkRequest[$maps[$element]] = $value;
116 }
117 }
118 }
119
120 $cleanTalkRequest = apply_filters('fluentform/cleantalk_fields', $cleanTalkRequest, $formData, $form);
121
122 $response = wp_remote_post(
123 'https://moderate.cleantalk.org/api2.0',
124 [
125 'body' => json_encode($cleanTalkRequest),
126 'headers' => [
127 'Content-Type' => 'application/json',
128 ],
129 ]
130 );
131
132 if (is_wp_error($response)) {
133 return false;
134 }
135
136 $response = json_decode(wp_remote_retrieve_body($response));
137
138 $cleantalkPassed = $response->allow == 1 && $response->spam == 0 && $response->account_status == 1;
139
140 return !$cleantalkPassed;
141 }
142
143 public static function hasExecuted()
144 {
145 global $fluentformCleantalkExecuted;
146
147 return isset($fluentformCleantalkExecuted) && true === $fluentformCleantalkExecuted;
148 }
149
150 protected static function claimExecution()
151 {
152 global $fluentformCleantalkExecuted;
153
154 if (self::hasExecuted()) {
155 return false;
156 }
157
158 $fluentformCleantalkExecuted = true;
159
160 return true;
161 }
162
163 public static function isCleantalkActivated()
164 {
165 $settings = get_option('_fluentform_cleantalk_details');
166 return $settings && ArrayHelper::get($settings, 'status');
167 }
168
169 public static function isEnabled()
170 {
171 if (self::isCleantalkActivated() || !self::isPluginEnabled()) {
172 return false;
173 }
174
175 $settings = get_option('_fluentform_global_form_settings');
176 return $settings && 'yes' == ArrayHelper::get($settings, 'misc.cleantalk_status');
177 }
178
179 public static function isPluginEnabled()
180 {
181 $exists = method_exists('Cleantalk\Antispam\Cleantalk', 'isAllowMessage');
182 if ($exists) {
183 global $apbct;
184 return !!$apbct->data['key_is_ok'];
185 }
186 return false;
187 }
188
189 public static function isSpamSubmission($formData, $form)
190 {
191 if (!self::claimExecution()) {
192 return false;
193 }
194
195 $cleanTalkRequest = self::getCleanTalkRequest($formData, $form);
196 $cleanTalk = new \Cleantalk\Antispam\Cleantalk();
197 $cleanTalk->server_url = 'https://moderate.cleantalk.org';
198 $response = $cleanTalk->isAllowMessage($cleanTalkRequest);
199
200 return 0 == $response->allow;
201 }
202
203 protected static function getCleanTalkRequest($data, $form)
204 {
205 global $apbct;
206 $app = wpFluentForm();
207 $ip = sanitize_text_field($app->request->getIp());
208
209 $serverName = isset($_SERVER['SERVER_NAME']) ? sanitize_text_field(wp_unslash($_SERVER['SERVER_NAME'])) : '';
210 $requestUri = isset($_SERVER['REQUEST_URI']) ? esc_url_raw(wp_unslash($_SERVER['REQUEST_URI'])) : '';
211
212 $info = [
213 'auth_key' => $apbct->settings['apikey'],
214 'sender_ip' => $ip,
215 'contact_form_subject' => $form->title,
216 'referrer' => urlencode($data['_wp_http_referer']),
217 'page_url' => $serverName . $requestUri,
218 'submit_time' => isset($_SESSION['ct_submit_time']) ? time() - (int)$_SESSION['ct_submit_time'] : null, //@todo Improve this
219 'agent' => 'php-api',
220 'js_on' => 1,
221 'sender_nickname' => '',
222 'sender_email' => '',
223 'message' => '',
224 'phone' => ''
225 ];
226
227 $maps = [
228 'input_name' => 'sender_nickname',
229 'input_email' => 'sender_email',
230 'textarea' => 'message',
231 'phone' => 'phone',
232 ];
233 $inputs = FormFieldsParser::getInputs($form, ['attributes']);
234
235 foreach ($inputs as $input) {
236 $element = ArrayHelper::get($input, 'element');
237 $key = ArrayHelper::get($input, 'attributes.name');
238 if (isset($maps[$element]) && !$info[$maps[$element]]) {
239 $value = ArrayHelper::get($data, $key);
240 if ($value) {
241 if (is_array($value)) {
242 $value = implode(' ', $value);
243 }
244 $info[$maps[$element]] = $value;
245 }
246 }
247 }
248
249 $info = apply_filters('fluentform/cleantalk_fields', $info, $data, $form);
250
251 return new CleantalkRequest($info);
252 }
253 }
254