-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathcleantalk-public.php
More file actions
1333 lines (1162 loc) · 41.9 KB
/
cleantalk-public.php
File metadata and controls
1333 lines (1162 loc) · 41.9 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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
use Cleantalk\ApbctWP\ApbctEnqueue;
use Cleantalk\ApbctWP\Escape;
use Cleantalk\ApbctWP\Localize\LocalizeHandler;
use Cleantalk\ApbctWP\Sanitize;
use Cleantalk\ApbctWP\Variables\Cookie;
use Cleantalk\ApbctWP\Variables\Get;
use Cleantalk\ApbctWP\Variables\Post;
use Cleantalk\ApbctWP\Variables\Server;
use Cleantalk\ApbctWP\LinkConstructor;
use Cleantalk\ApbctWP\ApbctJsBundleResolver;
// Prevent direct call
if ( ! defined('ABSPATH') ) {
die('Not allowed!');
}
/**
* Init functions
*
* @throws Exception
* @psalm-suppress UnusedVariable
* @psalm-suppress RedundantCondition
*/
function apbct_init()
{
global $ct_jp_comments, $apbct;
// Pixel
if (
$apbct->settings['data__pixel'] &&
empty($apbct->pixel_url) &&
!(
$apbct->settings['data__bot_detector_enabled'] === '1' &&
$apbct->settings['data__pixel'] === '3'
)
) {
$apbct->pixel_url = apbct_get_pixel_url(true);
}
// Localize data
if ( ! apbct_exclusions_check__url() ) {
if ($apbct->constants->place_public_js_scripts_in_footer->isDefined()) {
add_action('wp_footer', array(LocalizeHandler::class, 'handle'), 1);
add_action('login_footer', array(LocalizeHandler::class, 'handle'), 1);
} else {
add_action('wp_head', array(LocalizeHandler::class, 'handle'), 1);
add_action('login_head', array(LocalizeHandler::class, 'handle'), 1);
}
// The exclusion of scripts from wp-rocket handler
add_filter('rocket_delay_js_exclusions', 'apbct_rocket_delay_js_exclusions');
}
//fix for EPM registration form
if ( Post::get('reg_email') && shortcode_exists('epm_registration_form') ) {
unset($_POST['ct_checkjs_register_form']);
}
if ( Post::get('_wpnonce-et-pb-contact-form-submitted') !== '' ) {
add_shortcode('et_pb_contact_form', 'ct_contact_form_validate');
}
if ( $apbct->settings['forms__check_external'] ) {
// Fixing form and directs it this site
if (
$apbct->settings['forms__check_external__capture_buffer'] &&
! is_admin() &&
! apbct_is_ajax() &&
! apbct_is_post() &&
apbct_is_user_enable() &&
! (defined('DOING_CRON') && DOING_CRON) &&
! (defined('XMLRPC_REQUEST') && XMLRPC_REQUEST)
) {
if (
defined('CLEANTALK_CAPTURE_BUFFER_SPECIFIC_URL') &&
is_string(CLEANTALK_CAPTURE_BUFFER_SPECIFIC_URL)
) {
$catch_buffer = false;
$urls = explode(',', CLEANTALK_CAPTURE_BUFFER_SPECIFIC_URL);
foreach ( $urls as $url ) {
if ( apbct_is_in_uri($url) ) {
$catch_buffer = true;
}
}
} else {
$catch_buffer = true;
}
if ( $catch_buffer ) {
add_action('wp', 'apbct_buffer__start');
add_action('shutdown', 'apbct_buffer__end', 0);
add_action('shutdown', 'apbct_buffer__output', 2);
}
}
}
if (
Post::get('quform_ajax') &&
Post::get('quform_csrf_token') &&
Post::get('quform_form_id')
) {
require_once(CLEANTALK_PLUGIN_DIR . 'inc/cleantalk-ajax.php');
ct_ajax_hook();
}
/**hooks for cm answers pro */
if ( defined('CMA_PLUGIN_FILE') ) {
add_action('wp', 'ct_ajax_hook', 1);
}
/** VFB_Pro integration */
if (
! empty($_POST) &&
$apbct->settings['forms__contact_forms_test'] == 1 &&
empty(Post::get('ct_checkjs_cf7')) &&
apbct_is_plugin_active('vfb-pro/vfb-pro.php') &&
! empty(Post::get('_vfb-form-id'))
) {
ct_contact_form_validate();
}
/** Optima Express integration */
if (
! empty($_POST) &&
$apbct->settings['forms__contact_forms_test'] == 1 &&
empty(Post::get('ct_checkjs_cf7')) &&
apbct_is_plugin_active('optima-express/iHomefinder.php') &&
Post::get('actionType') === 'create' &&
!empty(Post::get('newEmail'))
) {
ct_contact_form_validate();
}
//hook for Anonymous Post
if ( $apbct->settings['data__general_postdata_test'] == 1 && empty(Post::get('ct_checkjs_cf7')) ) {
add_action('init', 'ct_contact_form_validate_postdata', 1000);
}
if ( $apbct->settings['forms__general_contact_forms_test'] == 1 && empty(Post::get('ct_checkjs_cf7')) && ! apbct_is_direct_trackback() ) {
add_action('CMA_custom_post_type_nav', 'ct_contact_form_validate_postdata', 1);
add_action('init', 'ct_contact_form_validate', 999);
if (
Post::get('reg_redirect_link') !== '' &&
Post::get('tmpl_registration_nonce_field') !== ''
) {
unset($_POST['ct_checkjs_register_form']);
add_action('init', 'ct_contact_form_validate', 999);
}
}
if ( $apbct->settings['data__general_postdata_test'] == 1 && empty(Post::get('ct_checkjs_cf7')) ) {
add_action('CMA_custom_post_type_nav', 'ct_contact_form_validate_postdata', 1);
}
// Fast Secure contact form
if ( defined('FSCF_VERSION') ) {
add_filter('si_contact_display_after_fields', 'ct_si_contact_display_after_fields');
add_filter('si_contact_form_validate', 'ct_si_contact_form_validate');
}
// WooCommerce whishlist
if ( class_exists('WC_Wishlists_Wishlist') ) {
add_filter('wc_wishlists_create_list_args', 'ct_woocommerce_wishlist_check', 1, 1);
}
// JetPack Contact form
if ( defined('JETPACK__VERSION') ) {
// Checking Jetpack contact form
add_filter('contact_form_is_spam', 'ct_contact_form_is_spam');
add_filter('jetpack_contact_form_is_spam', 'ct_contact_form_is_spam_jetpack', 50, 2);
add_filter('grunion_contact_form_field_html', 'ct_grunion_contact_form_field_html', 10, 2);
// Checking Jetpack comments form
$jetpack_active_modules = get_option('jetpack_active_modules');
if (
class_exists('Jetpack', false) &&
$jetpack_active_modules &&
in_array('comments', $jetpack_active_modules)
) {
$ct_jp_comments = true;
}
}
// WP Maintenance Mode (wpms)
add_action('wpmm_head', 'apbct_form__wpmm__addField', 1);
// Contact Form7
if ( defined('WPCF7_VERSION') ) {
add_filter('wpcf7_posted_data', function ($posted_data) {
if ( isset($posted_data['apbct_visible_fields']) ) {
unset($posted_data['apbct_visible_fields']);
}
if ( isset($posted_data['apbct__email_id__wp_contact_form_7']) ) {
unset($posted_data['apbct__email_id__wp_contact_form_7']);
}
return $posted_data;
});
add_filter('wpcf7_form_elements', 'apbct_form__contactForm7__addField');
add_filter('wpcf7_validate', 'apbct_form__contactForm7__tesSpam__before_validate', 999, 2);
$hook = WPCF7_VERSION >= '3.0.0' ? 'wpcf7_spam' : 'wpcf7_acceptance';
$num_arg = WPCF7_VERSION >= '5.3.0' ? 2 : 1;
add_filter($hook, 'apbct_form__contactForm7__testSpam', 9999, $num_arg);
//ignore other wpcf7_skip_spam_check filters to prevent submissions if APBCT check performs
add_filter('wpcf7_skip_spam_check', function () {
return false;
}, 999, 2);
add_action('wpcf7_before_send_mail', 'apbct_form__contactForm7__testSpam', 999);
}
if ( defined('PROFILEPRESS_SYSTEM_FILE_PATH') ) {
add_filter('pp_registration_validation', 'ct_registration_errors_ppress', 11, 2);
}
// bbPress
if ( class_exists('bbPress') ) {
add_filter('bbp_new_topic_pre_title', 'ct_bbp_get_topic', 1);
add_filter('bbp_new_topic_pre_content', 'ct_bbp_new_pre_content', 1);
add_filter('bbp_new_reply_pre_content', 'ct_bbp_new_pre_content', 1);
add_action('bbp_theme_before_topic_form_content', 'ct_comment_form');
add_action('bbp_theme_before_reply_form_content', 'ct_comment_form');
add_action('bbp_edit_reply_pre_content', 'ct_bbp_edit_pre_content', 1, 2);
}
//Custom Contact Forms
if ( defined('CCF_VERSION') ) {
add_filter('ccf_field_validator', 'ct_ccf', 1, 4);
}
add_action('comment_form', 'ct_comment_form');
// intercept WordPress Landing Pages POST
if ( defined('LANDINGPAGES_CURRENT_VERSION') && ! empty($_POST) ) {
if ( Post::get('action') === 'inbound_store_lead' ) { // AJAX action(s)
ct_check_wplp();
} elseif ( Post::get('inbound_submitted') === '1' ) {
// Final submit
ct_check_wplp();
}
}
// S2member. intercept POST
if ( defined('WS_PLUGIN__S2MEMBER_PRO_VERSION') ) {
$post_keys = array_keys($_POST);
foreach ( $post_keys as $post_key ) {
// Detect POST keys like /s2member_pro.*registration/
if ( strpos((string)$post_key, 's2member') !== false && strpos((string)$post_key, 'registration') !== false ) {
ct_s2member_registration_test($post_key);
break;
}
}
}
// New user approve hack
// https://wordpress.org/plugins/new-user-approve/
if ( ct_plugin_active('new-user-approve/new-user-approve.php') ) {
add_action('register_post', 'ct_register_post', 1, 3);
}
// Wilcity theme registration validation fix
add_filter(
'wilcity/filter/wiloke-listing-tools/validate-before-insert-account',
'apbct_wilcity_reg_validation',
10,
2
);
// Gravity forms
if ( defined('GF_MIN_WP_VERSION') ) {
add_filter('gform_get_form_filter', 'apbct_form__gravityForms__addField', 10, 2);
add_filter('gform_entry_is_spam', 'apbct_form__gravityForms__testSpam', 999, 3);
add_filter('gform_confirmation', 'apbct_form__gravityForms__showResponse', 999, 4);
}
//Pirate forms
if (
defined('PIRATE_FORMS_VERSION') &&
Post::get('pirate-forms-contact-name') &&
Post::get('pirate-forms-contact-email')
) {
apbct_form__piratesForm__testSpam();
}
// QForms integration
add_filter('quform_post_validate', 'ct_quform_post_validate', 10, 2);
// Ultimate Members
if ( class_exists('UM') ) {
add_action('um_main_register_fields', 'ct_register_form', 100); // Add hidden fileds
add_action('um_submit_form_register', 'apbct_registration__UltimateMembers__check', 9, 1); // Check submition
}
// Paid Memberships Pro integration
add_filter('pmpro_required_user_fields', function ($pmpro_required_user_fields) {
if (
! empty($pmpro_required_user_fields['username']) &&
! empty($pmpro_required_user_fields['bemail']) &&
! empty($pmpro_required_user_fields['bconfirmemail']) &&
$pmpro_required_user_fields['bemail'] == $pmpro_required_user_fields['bconfirmemail']
) {
$check = ct_test_registration(
$pmpro_required_user_fields['username'],
$pmpro_required_user_fields['bemail']
);
if ( isset($check['allow']) && $check['allow'] == 0 && function_exists('pmpro_setMessage') ) {
pmpro_setMessage($check['comment'], 'pmpro_error');
}
}
return $pmpro_required_user_fields;
});
// UsersWP plugin integration
add_filter('uwp_validate_result', 'apbct_form__uwp_validate', 3, 10);
//
// Load JS code to website footer
//
if ( ! (defined('DOING_AJAX') && DOING_AJAX) ) {
add_action('wp_head', 'apbct_hook__wp_head__set_cookie__ct_checkjs', 1);
add_action('wp_footer', 'apbct_hook__wp_footer', 1);
}
if ( $apbct->settings['trusted_and_affiliate__footer'] === '1' ) {
add_action('wp_footer', 'apbct_hook__wp_footer_trusted_text', 999);
}
if ( $apbct->settings['data__protect_logged_in'] != 1 && is_user_logged_in() ) {
add_action('init', 'ct_contact_form_validate', 999);
}
if ( apbct_is_user_enable() ) {
if ( $apbct->settings['forms__general_contact_forms_test'] == 1 && ! Post::get('comment_post_ID') && ! Get::get('for') && ! apbct_is_direct_trackback() ) {
add_action('init', 'ct_contact_form_validate', 999);
}
if ( apbct_is_post() &&
$apbct->settings['data__general_postdata_test'] == 1 &&
! Post::get('ct_checkjs_cf7') &&
! is_admin() &&
! apbct_is_user_role_in(array('administrator', 'moderator'))
) {
add_action('init', 'ct_contact_form_validate_postdata', 1000);
}
}
/**
* Integration with custom forms
*/
if ( ! empty($_POST) && apbct_custom_forms_trappings() ) {
add_action('init', 'ct_contact_form_validate', 999);
}
/**
* Internal Forms - Sendinblue Integration https://wordpress.org/plugins/mailin/
*/
if (
(int)$apbct->settings['forms__check_internal'] === 1
&& !empty($_POST)
&& Post::equal('sib_form_action', 'subscribe_form_submit')
&& apbct_is_plugin_active('mailin/sendinblue.php')
) {
ct_contact_form_validate();
}
if ( $apbct->settings['trusted_and_affiliate__shortcode'] === '1' ) {
add_shortcode('cleantalk_affiliate_link', 'apbct_trusted_text_shortcode_handler');
}
}
function apbct_buffer__start()
{
ob_start();
}
function apbct_buffer__end()
{
if ( ! ob_get_level() ) {
return;
}
global $apbct;
$apbct->buffer = ob_get_contents();
ob_end_clean();
}
/**
* Outputs changed buffer
*
* @global $apbct
*/
function apbct_buffer__output()
{
global $apbct;
if ( empty($apbct->buffer) ) {
return;
}
if ( apbct_is_plugin_active('flow-flow/flow-flow.php') || apbct_is_theme_active('epico') ) {
$output = apbct_buffer_modify_by_string();
} else {
$output = apbct_buffer_modify_by_dom();
}
die($output);
}
function apbct_buffer_modify_by_string()
{
global $apbct, $wp;
$site_url = get_option('home');
$site__host = parse_url($site_url, PHP_URL_HOST);
preg_match_all('/<form\s*.*>\s*.*<\/form>/', $apbct->buffer, $matches, PREG_SET_ORDER);
if ( count($matches) > 0 ) {
foreach ( $matches as $match ) {
if (!isset($match[0])) {
continue;
}
preg_match('/action="(\S*)"/', $match[0], $group_action);
if (!isset($group_action[1])) {
continue;
}
$action = $group_action[1];
$action__host = parse_url($action, PHP_URL_HOST);
if ( $action__host !== null && $site__host != $action__host ) {
preg_match('/method="(\S*)"/', $match[0], $group_method);
if (!isset($group_method[1])) {
continue;
}
$method = $group_method[1];
$hidden_fields = '<input type="hidden" name="cleantalk_hidden_action" value="' . $action . '">';
$hidden_fields .= '<input type="hidden" name="cleantalk_hidden_method" value="' . $method . '">';
$modified_match = preg_replace(
'/action="\S*"/',
'action="' . home_url(add_query_arg(array(), $wp->request)) . '"',
$match[0]
);
$modified_match = preg_replace('/method="\S*"/', 'method="POST"', $modified_match);
$modified_match = str_replace('</form>', $hidden_fields . '</form>', $modified_match);
$apbct->buffer = str_replace($match[0], $modified_match, $apbct->buffer);
}
}
}
return $apbct->buffer;
}
function apbct_buffer_modify_by_dom()
{
global $apbct, $wp;
$site_url = get_option('home');
$site__host = parse_url($site_url, PHP_URL_HOST);
$dom = new DOMDocument();
@$dom->loadHTML($apbct->buffer, LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD);
$forms = $dom->getElementsByTagName('form');
foreach ( $forms as $form ) {
$action = $form->getAttribute('action');
$action = $action ?: $site_url;
$action__host = parse_url($action, PHP_URL_HOST);
// Check if the form directed to the third party site
if ( $action__host !== null && $site__host != $action__host ) {
$method = $form->getAttribute('method');
$method = $method ?: 'get';
// Directs form to our site
$form->setAttribute('method', 'POST');
$form->setAttribute('action', home_url(add_query_arg(array(), $wp->request)));
// Add cleantalk_hidden_action
$new_input = $dom->createElement('input');
$new_input->setAttribute('type', 'hidden');
$new_input->setAttribute('name', 'cleantalk_hidden_action');
$new_input->setAttribute('value', $action);
$form->appendChild($new_input);
// Add cleantalk_hidden_method
$new_input = $dom->createElement('input');
$new_input->setAttribute('type', 'hidden');
$new_input->setAttribute('name', 'cleantalk_hidden_method');
$new_input->setAttribute('value', $method);
$form->appendChild($new_input);
}
}
unset($form);
$html = $dom->getElementsByTagName('html');
return is_object($html) && isset($html[0], $html[0]->childNodes[0]) && $dom->getElementsByTagName('rss')->length == 0
? $dom->saveHTML()
: $apbct->buffer;
}
/**
* Adds cookie script filed to head
*/
function apbct_hook__wp_head__set_cookie__ct_checkjs()
{
ct_add_hidden_fields('ct_checkjs', false, true, true);
return null;
}
/**
* Adds check_js script to the footer
* @psalm-suppress UnusedVariable
*/
function apbct_hook__wp_footer()
{
global $apbct;
# Return false if page is excluded
if ( apbct_exclusions_check__url() ) {
return;
}
// Pixel
if (
$apbct->settings['data__pixel'] === '1' ||
(
$apbct->settings['data__pixel'] === '3' &&
! apbct_is_cache_plugins_exists() &&
$apbct->settings['data__bot_detector_enabled'] !== '1'
)
) {
echo '<img alt="Cleantalk Pixel" title="Cleantalk Pixel" id="apbct_pixel" style="display: none;" src="' . Escape::escUrl($apbct->pixel_url) . '">';
}
if ( $apbct->settings['data__use_ajax'] ) {
$timeout = $apbct->settings['misc__async_js'] ? 1000 : 0;
if ( $apbct->data['ajax_type'] == 'rest' ) {
$send_way_asset = "if (typeof apbct_public_sendREST === 'function' && typeof apbct_js_keys__set_input_value === 'function') {
apbct_public_sendREST(
'js_keys__get',
{ callback: apbct_js_keys__set_input_value })
}";
} else {
$send_way_asset = "if (typeof apbct_public_sendAJAX === 'function' && typeof apbct_js_keys__set_input_value === 'function') {
apbct_public_sendAJAX(
{ action: 'apbct_js_keys__get' },
{ callback: apbct_js_keys__set_input_value })
}";
}
$cookie_bot_asset = (class_exists('Cookiebot_WP')) ? 'data-cookieconsent="ignore"' : '';
$script =
'<script ' . $cookie_bot_asset
. ">
document.addEventListener('DOMContentLoaded', function () {
setTimeout(function(){
if( document.querySelectorAll('[name^=ct_checkjs]').length > 0 ) {
" . $send_way_asset . "
}
}," . $timeout . ")
})
</script>";
echo Escape::escKses(
$script,
array(
'script' => array(
'type' => true,
'data-cookieconsent' => true
)
)
);
}
}
/**
* Adds hidden filed to define availability of client's JavaScript
*
* @param string $field_name
* @param bool $return_string
* @param bool $cookie_check
* @param bool $no_print
* @param bool $ajax
*
* @return array|false|string|string[]|void
*
* @psalm-suppress UnusedVariable
*/
function ct_add_hidden_fields(
$field_name = 'ct_checkjs',
$return_string = false,
$cookie_check = false,
$no_print = false,
$ajax = true
) {
global $ct_checkjs_def, $apbct;
// Return false if page is excluded
if ( apbct_exclusions_check__url() ) {
return false;
}
// Return false if cookie mode is ON
if ( $apbct->settings['data__set_cookies'] == 1 ) {
return false;
}
$ct_checkjs_key = ct_get_checkjs_value();
$field_id_hash = md5((string)rand(0, 1000));
// Using only cookies
if ( $cookie_check ) {
$html = '';
// Using AJAX to get key
} elseif ( $apbct->settings['data__use_ajax'] && $ajax ) {
// Fix only for wp_footer -> apbct_hook__wp_head__set_cookie__ct_checkjs()
if ( $no_print ) {
return;
}
$field_id = $field_name . '_' . $field_id_hash;
$html = "<input type=\"hidden\" id=\"{$field_id}\" name=\"{$field_name}\" value=\"{$ct_checkjs_def}\" />";
// Set KEY from backend
} else {
// Fix only for wp_footer -> apbct_hook__wp_head__set_cookie__ct_checkjs()
if ( $no_print ) {
return;
}
$ct_input_challenge = sprintf("'%s'", is_null($ct_checkjs_key) ? $ct_checkjs_def : $ct_checkjs_key);
$field_id = $field_name . '_' . $field_id_hash;
$html = "<input type=\"hidden\" id=\"{$field_id}\" name=\"{$field_name}\" value=\"{$ct_checkjs_def}\" />
<script " . (class_exists('Cookiebot_WP') ? 'data-cookieconsent="ignore"' : '') . ">
setTimeout(function(){
var ct_input_name = \"{$field_id}\";
if (document.getElementById(ct_input_name) !== null) {
var ct_input_value = document.getElementById(ct_input_name).value;
document.getElementById(ct_input_name).value = document.getElementById(ct_input_name).value.replace(ct_input_value, {$ct_input_challenge});
}
}, 1000);
</script>";
}
// Simplify JS code and Fixing issue with wpautop()
$html = str_replace(array("\n", "\r", "\t"), '', $html);
if ( $return_string === true ) {
return $html;
} else {
echo Escape::escKses(
$html,
array(
'script' => array(
'type' => true,
'data-cookieconsent' => true
),
'input' => array(
'type' => true,
'id' => true,
'name' => true,
'value' => true
)
)
);
}
}
/**
* Changes whether notify admin/athor or not.
*
* @param bool $maybe_notify notify flag
* @param int $comment_ID Comment id
*
* @return bool flag
*/
function apbct_comment__Wordpress__doNotify($_maybe_notify, $_comment_ID)
{
return true;
}
/**
* Change email notification recipients
*
* @param array $emails
* @param integer $comment_id
*
* @return array
* @global \Cleantalk\ApbctWP\State $apbct
*/
function apbct_comment__Wordpress__changeMailNotificationRecipients($emails, $_comment_id)
{
global $apbct;
return array_unique(array_merge($emails, (array)json_decode($apbct->comment_notification_recipients, true)));
}
/**
* Changes email notification for spam comment for native WordPress comment system
*
* @param string $notify_message Body of email notification
* @param $_comment_id
*
* @return string Body for email notification
*/
function apbct_comment__Wordpress__changeMailNotification($notify_message, $_comment_id)
{
global $apbct;
return PHP_EOL
. __('CleanTalk Anti-Spam: This message is possible spam.', 'cleantalk-spam-protect')
. "\n" . __('You could check it in CleanTalk\'s Anti-Spam database:', 'cleantalk-spam-protect')
//HANDLE LINK
. "\n" . 'IP: https://cleantalk.org/blacklists/' . $apbct->sender_ip
//HANDLE LINK
. "\n" . 'Email: https://cleantalk.org/blacklists/' . $apbct->sender_email
. "\n" . PHP_EOL . sprintf(
__('Activate protection in your Anti-Spam Dashboard: %s.', 'clentalk'),
//HANDLE LINK
'https://cleantalk.org/my/?cp_mode=antispam&utm_source=newsletter&utm_medium=email&utm_campaign=wp_spam_comment_passed'
. ($apbct->data['user_token']
? '&iser_token=' . $apbct->data['user_token']
: ''
)
)
. PHP_EOL . '---'
. PHP_EOL
. PHP_EOL
. $notify_message;
}
function apbct_comment__wordpress__show_blacklists($notify_message, $comment_id)
{
$comment_details = get_comments(array('comment__in' => $comment_id));
if (is_array($comment_details) && isset($comment_details[0])) {
$comment_details = $comment_details[0];
}
if ( is_object($comment_details) && isset($comment_details->comment_author_email, $comment_details->comment_author_IP) ) {
//HANDLE LINK
$black_list_link = 'https://cleantalk.org/blacklists/';
$links = PHP_EOL;
$links .= esc_html__('Check for spam:', 'cleantalk-spam-protect');
$links .= PHP_EOL;
$links .= $black_list_link . $comment_details->comment_author_email;
$links .= PHP_EOL;
if ( ! empty($comment_details->comment_author_IP) ) {
$links .= $black_list_link . $comment_details->comment_author_IP;
$links .= PHP_EOL;
}
return $notify_message . $links;
}
return $notify_message;
}
/**
* Set die page with Cleantalk comment.
*
* @param null $comment_status
*
* @global null $ct_comment
* $err_text = '<center><b style="color: #49C73B;">Clean</b><b style="color: #349ebf;">Talk.</b> ' . __('Spam protection', 'cleantalk-spam-protect') . "</center><br><br>\n" . $ct_comment;
*/
function ct_die($_comment_id, $_comment_status)
{
global $ct_comment, $ct_jp_comments, $apbct;
// JCH Optimize caching preventing
add_filter('jch_optimize_page_cache_set_caching', static function ($_is_cache_active) {
return false;
}, 999, 1);
do_action('apbct_pre_block_page', $ct_comment);
$message_title = __('Spam protection', 'cleantalk-spam-protect');
if ( ! $apbct->constants->disable_blocking_title->isDefined() ) {
$message_title = '<b style="color: #49C73B;">Clean</b><b style="color: #349ebf;">Talk.</b> ' . $message_title;
}
if ( Post::get('et_pb_contact_email') ) {
$message_title = 'Blacklisted';
}
$back_link = '';
$back_script = '';
if ( ! $ct_jp_comments ) {
$back_script = '<script>setTimeout("history.back()", 5000);</script>';
} elseif ( isset($_SERVER['HTTP_REFERER']) ) {
$back_link = '<a href="' . Sanitize::cleanUrl(Server::get('HTTP_REFERER')) . '">' . __('Back') . '</a>';
}
if ( file_exists(CLEANTALK_PLUGIN_DIR . "templates/lock-pages/lock-page-ct-die.html") ) {
$ct_die_page = file_get_contents(CLEANTALK_PLUGIN_DIR . "templates/lock-pages/lock-page-ct-die.html");
// Translation
$replaces = array(
'{MESSAGE_TITLE}' => $message_title,
'{MESSAGE}' => $ct_comment,
'{BACK_LINK}' => $back_link,
'{BACK_SCRIPT}' => $back_script
);
foreach ( $replaces as $place_holder => $replace ) {
$ct_die_page = str_replace($place_holder, (is_null($replace) ? '' : $replace), $ct_die_page);
}
http_response_code(200);
die($ct_die_page);
}
http_response_code(200);
die("Forbidden. Sender blacklisted. Blocked by CleanTalk");
}
/**
* Set die page with CleanTalk comment from parameter.
*
* @param $comment_body
*/
function ct_die_extended($comment_body)
{
global $ct_jp_comments, $apbct;
// JCH Optimize caching preventing
add_filter('jch_optimize_page_cache_set_caching', static function ($_is_cache_active) {
return false;
}, 999, 1);
$message_title = __('Spam protection', 'cleantalk-spam-protect');
if ( !$apbct->constants->disable_blocking_title->isDefined() ) {
$message_title = '<b style="color: #49C73B;">Clean</b><b style="color: #349ebf;">Talk.</b> ' . $message_title;
}
$back_link = '';
$back_script = '';
if ( ! $ct_jp_comments ) {
$back_script = '<script>setTimeout("history.back()", 5000);</script>';
} else {
$back_link = '<a href="' . Sanitize::cleanUrl(Server::get('HTTP_REFERER')) . '">' . __('Back') . '</a>';
}
if ( file_exists(CLEANTALK_PLUGIN_DIR . "templates/lock-pages/lock-page-ct-die.html") ) {
$ct_die_page = file_get_contents(CLEANTALK_PLUGIN_DIR . "templates/lock-pages/lock-page-ct-die.html");
// Translation
$replaces = array(
'{MESSAGE_TITLE}' => $message_title,
'{MESSAGE}' => $comment_body,
'{BACK_LINK}' => $back_link,
'{BACK_SCRIPT}' => $back_script
);
foreach ( $replaces as $place_holder => $replace ) {
$ct_die_page = str_replace($place_holder, $replace, $ct_die_page);
}
http_response_code(200);
$allowed_html = array(
'html' => array(
'lang' => array(),
),
'head' => array(),
'meta' => array(
'charset' => array(),
'name' => array(),
'content' => array(),
'http-equiv' => array(),
),
'style' => array(),
'body' => array(),
'div' => array(
'class' => array(),
),
'h1' => array(
'class' => array(),
),
'p' => array(
'class' => array(),
),
'a' => array(
'href' => array(),
'class' => array(),
),
'script' => array(
'src' => array(),
),
'!--[if lt IE 9]' => array(),
'![endif]--' => array(),
);
$content = wp_kses($ct_die_page, $allowed_html);
die($content);
}
http_response_code(200);
die("Forbidden. Sender blacklisted. Blocked by CleanTalk");
}
/**
* Validates JavaScript anti-spam test
*
* @param string $check_js_value String to checking
* @param bool $is_cookie
*
* @return int|null
*/
function apbct_js_test($check_js_value = '', $is_cookie = false)
{
global $apbct;
$out = null;
if (
( ! empty($check_js_value) ) ||
( $is_cookie && $apbct->data['cookies_type'] === 'alternative' && Cookie::get('ct_checkjs') )
) {
$js_key = $is_cookie && $apbct->data['cookies_type'] === 'alternative'
? Cookie::get('ct_checkjs')
: trim($check_js_value);
// Check static key
if (
$apbct->settings['data__use_static_js_key'] == 1 ||
($apbct->settings['data__use_static_js_key'] == -1 &&
(apbct_is_cache_plugins_exists() ||
(apbct_is_post() && isset($apbct->data['cache_detected']) && $apbct->data['cache_detected'] == 1)
)
)
) {
$out = ct_get_checkjs_value() === $js_key ? 1 : 0;
// Random key check
} else {
$out = isset($apbct->js_keys[ $js_key ]) ? 1 : 0;
}
}
return $out;
}
/**
* Get post url
*
* @param int|null $comment_id
* @param int $comment_post_id
*
* @return string|null
*/
function ct_post_url($comment_id, $comment_post_id)
{
if ( empty($comment_post_id) ) {
return null;
}
if ( $comment_id === null ) {
$last_comment = get_comments('number=1');
if (isset($last_comment[0]) && is_object($last_comment[0])) {
$comment_id = isset($last_comment[0]->comment_ID) ? (int)$last_comment[0]->comment_ID + 1 : 1;
}
}
$permalink = get_permalink($comment_post_id);
$post_url = null;
if ( $permalink !== null && $permalink !== false ) {
$post_url = $permalink . '#comment-' . $comment_id;
}
return $post_url;
}
/**
* Public filter 'pre_comment_approved' - Mark comment unapproved always
* @return int Zero
*/
function ct_set_not_approved()
{
return 0;
}
/**
* Public filter 'pre_comment_approved' - Mark comment approved if it's not 'spam' only
*
* @param $approved
* @param $_comment
*
* @return int|string "spam"|1
*/
function ct_set_approved($approved, $_comment)
{
if ( $approved === 'spam' ) {
return $approved;
}
return 1;
}