/
businesspress.php
2217 lines (1608 loc) · 79 KB
/
businesspress.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
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
/*
Plugin Name: BusinessPress
Plugin URI: http://www.foliovision.com
Description: This plugin secures your site
Version: 1.0
Author: Foliovision
Author URI: http://foliovision.com
Requires PHP: 5.6
*/
require_once( dirname(__FILE__) . '/fp-api.php' );
class BusinessPress extends BusinessPress_Plugin {
const VERSION = '1.0';
private $disallowed_caps_default = array(
'install_plugins' => 1,
'install_themes' => 1,
'delete_plugins' => 1,
'delete_themes' => 1,
'edit_plugins' => 1,
'edit_themes' => 1,
'update_plugins' => 0,
'update_themes' => 0,
'activate_plugins' => 0
);
private $checks = array(
'disallow_edits_defined' => 1,
'disallow_mods_defined' => 1,
'update_filters_mismatch' => 1,
'user_can_if_cant' => 1
);
private $autoupdateType = 'minor';
private $adminEmail;
private $strArrMessages = array(); // string array, contains messages used in this plugin
var $aCoreUpdatesDismiss = array();
var $aOptions = array();
public function __construct() {
if( !function_exists('add_action') ) {
exit( 0 );
}
/*
Since October 2019 WordPress doesn't support ../ in the upload paths: https://core.trac.wordpress.org/changeset/46476
Here is a hotfix:
*/
$upload_path = get_option('upload_path');
if( $upload_path && ( false !== strpos( $upload_path, '../' ) || false !== strpos( $upload_path, '..' . DIRECTORY_SEPARATOR ) ) ) {
include( dirname(__FILE__).'/upload-path-fix.php' );
}
$this->adminEmail = get_option('admin_email');
$this->aOptions = is_multisite() ? get_site_option('businesspress') : get_option( 'businesspress' );
$this->disable_xmlrpc();
if( $this->get_setting('hide-notices') ) include( dirname(__FILE__).'/businesspress-notices.class.php' );
add_action( 'in_plugin_update_message-businesspress/businesspress.php', array( &$this, 'plugin_update_message' ) );
register_activation_hook( __FILE__, array( $this , 'activate') );
register_deactivation_hook( __FILE__, array( $this, 'deactivate') );
add_action( 'wp', array( $this, 'anti_clickjacking_headers') );
add_action( 'admin_init', array( $this, 'pointer_defaults') );
add_action( 'admin_init', array( $this, 'plugin_update_hook' ) );
add_action( 'admin_init', array( $this, 'apply_restrictions') );
add_action( 'plugins_loaded', array( $this, 'load_extensions' ) );
add_action( 'plugins_loaded', array( $this, 'load_extensions_later' ), 11 );
//add_filter( 'dashboard_glance_items', array( $this, 'core_updates_discard' ) ); // show only the current branch update for dashboard
/*
* WordPress Upgrade tweaks
*/
add_filter( 'auto_update_core', array( $this, 'delay_core_updates' ), 999, 2 );
add_action( 'admin_init', array( $this, 'stop_disable_wordpress_core_updates') );
add_action( 'core_upgrade_preamble', array( $this, 'upgrade_screen') );
add_action( 'load-update-core.php', array( $this, 'upgrade_screen_start') );
add_filter( 'send_core_update_notification_email', '__return_false' ); // disabling WP_Automatic_Updater::send_email() with subject of "WordPress x.y.z is available. Please update!"
add_filter( 'auto_core_update_send_email', '__return_false' ); // disabling WP_Automatic_Updater::send_email() with subject of "Your site has updated to WordPress x.y.z"
/*
* Admin screen
*/
add_action( 'admin_enqueue_scripts', array( $this, 'admin_style' ) );
add_action( 'admin_init', array( $this, 'handle_post') );
add_filter( 'plugin_action_links', array( $this, 'admin_plugin_action_links' ), 10, 2);
add_action( 'wp_ajax_businesspress_contact_admin', array( $this, 'contact_admin') );
add_filter( 'auth_cookie_expiration', array( $this, 'admin_login_duration' ), 10, 3 );
// Hide "Welcome" on Dashboard
add_action( 'welcome_panel', array( $this, 'dashboard_hide_welcome' ), 0 );
// Hide "WordPress Events and News" on dashboard
add_action( 'wp_dashboard_setup', array( $this, 'dashboard_hide_wordpress_events_and_news' ) );
// Hide "PHP Update Recommended, if user is doesn't have the full permissions
add_action( 'wp_dashboard_setup', array( $this, 'dashboard_hide_php_update_recommended' ) );
/*
* Visual WP-Admin tweaks
*/
add_filter( 'heartbeat_settings', array( $this, 'heartbeat_frequency' ), 999);
add_filter( 'admin_footer_text', array( $this, 'remove_wp_footer' ) );
add_action( 'admin_init', array( $this, 'admin_screen_cleanup') );
add_action( 'admin_head', array( $this, 'admin_screen_cleanup_css') );
add_action( 'wp_before_admin_bar_render', array( $this, 'remove_wp_admin_bar_items' ) );
remove_action( 'admin_color_scheme_picker', 'admin_color_scheme_picker' );
add_filter( 'get_user_option_admin_color', array( $this, 'admin_color_force' ) );
add_filter( 'login_title', array( $this, 'login_title' ) );
/*
* Frontend
*/
add_action( 'init', array( $this, 'apply_restrictions') );
add_action( 'init', array( $this, 'remove_generator_tag') ); // Generator tags
add_action( 'wp_footer', array( $this, 'multisite_footer'), 999 );
add_filter( 'wp_login_errors', array( $this, 'wp_login_errors' ) );
if( $this->get_setting('search-results') || isset($_GET['bpsearch']) ) include( dirname(__FILE__).'/fv-search.php' );
/*
* Login protection
*/
add_action( 'template_redirect', array( $this, 'fail2ban_404' ) );
add_action( 'wp_login_failed', array( $this, 'fail2ban_login' ) );
add_filter( 'xmlrpc_login_error', array( $this, 'fail2ban_xmlrpc' ) );
add_filter( 'xmlrpc_pingback_error', array( $this, 'fail2ban_xmlrpc_ping' ), 5 );
add_action( 'lostpassword_post', array( $this, 'fail2ban_lostpassword' ) );
if( $this->get_setting('disable-user-login-scanning') ) {
if( !empty($_GET['author']) ) {
if( is_admin() && !defined('DOING_AJAX') ) return;
die();
}
}
/*
* WAF
*/
$this->fail2ban_waf();
add_filter( 'login_redirect', array( $this, 'tweak_login_redirect' ) );
add_filter( 'logout_redirect', array( $this, 'tweak_login_redirect' ) );
/*
* Email notification disable
*/
add_action( 'after_password_reset', array( $this, 'subscriber_notification_disable' ), 0);
/*
Editor disabling
*/
add_action( 'post_submitbox_start', array( $this, 'editor_disable_checkbox' ) );
add_action( 'save_post', array( $this, 'editor_disable_checkbox_save' ) );
add_filter( 'user_can_richedit', array( $this, 'editor_disable' ) );
add_filter( 'the_content', array( $this, 'editor_disabled_thus_no_wpautop'), 0);
/*
Quick tweaks
*/
// WooCommerce message "Connect your store to WooCommerce.com to receive extensions updates and support."
add_filter( 'woocommerce_helper_suppress_connect_notice', '__return_true' );
add_filter( 'wp_mail_from_name', array( $this, 'wp_mail_from' ), PHP_INT_MAX );
/*
* Email blocking
*/
if( $this->get_setting('email-blocking') ) {
add_filter( 'wp_mail', array( $this, 'wp_mail_block_stage_1' ) );
}
/*
Front-end login check
*/
add_action( 'wp_footer', array( $this, 'login_check_js'), 999 );
add_action( 'wp_ajax_bpress_login_check', array( $this, 'login_check_ajax') );
add_action( 'wp_ajax_nopriv_bpress_login_check', array( $this, 'login_check_ajax') );
/*
* Setting for the "BIG image" threshold value.
*/
add_action( 'admin_init', array( $this, 'big_image_size_threshold_setting') );
add_action( 'big_image_size_threshold', array( $this, 'big_image_size_threshold'), PHP_INT_MAX, 4 );
/*
* Hide Password Protected Posts
*/
add_action( 'pre_get_posts', array( $this, 'hide_password_protected_posts' ) );
/**
* Error reporting
*/
add_filter( 'recovery_mode_email', array($this , 'recovery_email') );
/**
* User login sessions
*/
// Show on wp-admin user profile screen
add_action( 'show_user_profile', array( $this, 'user_session_tokens' ) );
add_action( 'edit_user_profile', array( $this, 'user_session_tokens' ) );
parent::__construct();
}
function activate() {
$this->aOptions = array();
$this->aOptions['core_auto_updates'] = 'minor';
update_option( 'businesspress', $this->aOptions );
}
function admin_color_force() {
return $this->get_setting('admin-color');
}
function admin_plugin_action_links($links, $file) {
$plugin_file = basename(__FILE__);
if( basename($file) == $plugin_file ) {
$settings_link = '<a href="'.site_url('wp-admin/options-general.php?page=businesspress').'">'.__('Settings', 'businesspress').'</a>';
array_unshift($links, $settings_link);
}
return $links;
}
function admin_screen_cleanup() {
remove_filter( 'update_footer', 'core_update_footer' );
remove_action( 'admin_notices', 'update_nag', 3 );
remove_action( 'network_admin_notices', 'update_nag', 3 );
global $WPMUDEV_Dashboard_Notice3;
if( $WPMUDEV_Dashboard_Notice3 !== null ){
remove_action( 'admin_print_styles', array( &$WPMUDEV_Dashboard_Notice3, 'notice_styles' ) );
remove_action( 'all_admin_notices', array( &$WPMUDEV_Dashboard_Notice3, 'activate_notice' ), 5 );
remove_action( 'all_admin_notices', array( &$WPMUDEV_Dashboard_Notice3, 'install_notice' ), 5 );
}
}
function admin_screen_cleanup_css() {
// todo: also hide theme updates
// todo: also hide in Network admin
?>
<style>
#adminmenu .menu-icon-plugins .update-plugins { display: none }
#wp-version-message .button { display: none }
/* Yasr – Yet Another Stars Rating promotions */
#wpbody-content div.fs-notice.promotion { display: none !important }
#wpbody-content .yasr-donatedivdx { display: none !important }
#adminmenu .toplevel_page_yasr_settings_page .update-plugins.fs-trial { display: none }
#wp-admin-bar-searchwp { display: none !important }
.searchwp-settings-header-nav ul li a.searchwp-settings-nav-tab-support > span > span { display: none !important }
#xyz-ihs-premium, .xyz_ihs_social_media, .xyz_ihs_sugession, .xyz_ihs_new_subscribe, .xyz_ihs_inmotion, .xyz_our_plugins_new, .xyz_poweredBy { display: none }
</style>
<?php
}
function admin_style() {
if( is_admin() && isset($_GET['page']) && $_GET['page'] == 'businesspress' ) {
wp_register_style( 'businesspress_admin', plugins_url('/css/admin.css',__FILE__), false, BusinessPress::VERSION );
wp_enqueue_style( 'businesspress_admin' );
wp_enqueue_media();
}
}
function apply_restrictions() {
if( $this->get_setting('link-manager') ) add_filter( 'pre_option_link_manager_enabled', '__return_true' );
if( !$this->check_user_permission() ) {
add_action( 'admin_footer', array( $this, 'hide_plugin_controls' ), 1 );
add_filter( 'plugin_action_links', array( $this, 'plugin_action_links' ), 999, 2 );
add_filter( 'map_meta_cap', array( $this, 'capability_filter' ), 999, 4 );
add_filter( 'admin_init', array( $this, 'disable_deactivation' ), 999, 4 );
}
if( !empty($this->aOptions['core_auto_updates']) ) {
if( $this->aOptions['core_auto_updates'] === 'major' ) {
add_filter( 'allow_major_auto_core_updates', '__return_true', 1000 );
} else if( $this->aOptions['core_auto_updates'] === 'minor' ) {
add_filter( 'allow_minor_auto_core_updates', '__return_true', 1000 );
} else if( $this->aOptions['core_auto_updates'] === 'none' ) {
add_filter( 'automatic_updater_disabled', '__return_true', 1000 );
}
}
if( $this->get_setting('autoupdates_vcs') ) add_filter( 'automatic_updates_is_vcs_checkout', '__return_false', 999 );
if( ( $this->get_setting('wp_admin_bar_subscribers') || $this->get_setting('wp_admin_redirect_subscribers') ) && get_current_user_id() > 0 ) {
$objUser = get_userdata( get_current_user_id() );
if( $objUser && isset($objUser->roles) && is_array($objUser->roles) ) {
$roles = $objUser->roles;
if(
// this is silly, but we can't rely on !current_user_can() with edit_posts or delete_posts to detect Subscribers because of bbPress
count($roles) == 2 && ( $roles[0] == 'subscriber' && $roles[1] == 'bbp_participant' || $roles[0] == 'bbp_participant' && $roles[1] == 'subscriber' ) ||
count($roles) > 0 && $roles[0] == 'subscriber' ||
// Easy Digital Downloads with subscriptions
count($roles) > 0 && $roles[0] == 'edd_subscriber' ||
count($roles) == 0
) {
add_filter('show_admin_bar', '__return_false');
add_action( 'admin_init', array( $this, 'subscriber__dashboard_redirect' ) );
add_action( 'admin_head', array( $this, 'subscriber__hide_menus' ) );
}
}
}
}
function can_update_core() {
if( !empty($this->aOptions['cap_update']) && $this->aOptions['cap_update'] && !empty($this->aOptions['cap_core']) && $this->aOptions['cap_core'] ) {
return true;
}
return false;
}
function cache_core_version_info() { // TODO
$aVersions = get_option( 'businesspress_core_versions' );
if( !$aVersions || !isset($aVersions['ttl']) || $aVersions['ttl'] < time() ) {
$bSuccess = false;
$aResponse = wp_remote_get( 'https://codex.wordpress.org/WordPress_Versions' );
if( !is_wp_error($aResponse) ) {
if( preg_match_all( '~<tr[\s\S]*?</tr>~', $aResponse['body'], $aTableRows ) ) {
$aVersions = array( 'data' => array() );
$aVersions['ttl'] = time() + 900;
if( count($aTableRows) > 0 ) {
foreach( $aTableRows[0] as $sTableRow ) {
preg_match( '~>([0-9.-]+)</a>~', $sTableRow, $aVersion );
preg_match( '~\S+ \d+, 20\d\d~', $sTableRow, $aDate );
if( $aVersion && $aDate ) {
$bSuccess = true;
$aVersions['data'][$aVersion[1]] = $aDate[0];
}
}
}
}
}
if( !$bSuccess ) {
$aVersions = get_option( 'businesspress_core_versions', array() );
$aVersions['ttl'] = time()+120;
}
update_option( 'businesspress_core_versions', $aVersions, false );
}
if( !isset($aVersions['data']['5.7']) ) $aVersions['data']['5.7'] = 'March 9, 2021'; // fix 5.7 if missing date
return $aVersions;
}
function capability_filter( $required_caps, $cap, $user_id, $args ) {
$blocked_caps = $this->get_disallowed_caps();
if( in_array( $cap, $blocked_caps ) ) {
$required_caps[] = 'do_not_allow';
}
return $required_caps;
}
function check_user_permission() {
global $current_user;
if( !$this->get_setting('restrictions_enabled') ) return true;
if( $this->get_setting('email') == $current_user->user_email ) {
return true;
} else if( $this->get_setting('domain') == $this->get_email_domain($current_user->user_email) ) {
return true;
}
return false;
}
function contact_admin() {
$current_user = wp_get_current_user();
if( $this->get_contact_email() == -1 ) die();
wp_mail( $this->get_contact_email(), 'BusinessPress contact form submission', $_POST['message'], array( 'Reply-To: '.$current_user->display_name.' <'.$current_user->user_email.'>' ) );
die('1');
}
function admin_login_duration( $expiration, $user_id, $remember) {
$duration = $this->get_setting('login-duration');
// check if remember is set, if not, return the default expiration
if( !$remember ) {
return $expiration;
}
// get new expiration based on the setting
if( strcmp($duration, '2_weeks') == 0 ) {
return $expiration; // 2 weeks is the default
} else if ( strcmp($duration, '2_months') == 0 ) {
$expiration = 2 * MONTH_IN_SECONDS;
} else if ( strcmp($duration, '6_months') == 0 ) {
$expiration = 6 * MONTH_IN_SECONDS;
}
return $expiration;
}
function core_updates_discard( $items ) {
$objUpdates = get_site_transient( 'update_core' );
if( $objUpdates && !empty($objUpdates->updates) ) {
foreach( $objUpdates->updates AS $update ) {
if( stripos($update->current,get_bloginfo( 'version' )) === 0 ) continue;
$this->aCoreUpdatesDismiss[$update->current.'|'.$update->locale] = 'Hell yeah';
}
}
// here's where we mess with the dismissed_update_core option as there is no filtering for available updates
add_filter( 'pre_site_option_dismissed_update_core', array( $this, 'core_updates_discard_do' ) );
return $items;
}
function core_updates_discard_do( $items ) {
// todo: what if ther are existing discarded updates?
return $this->aCoreUpdatesDismiss;
}
function cron_schedule() {
$timestamp = wp_next_scheduled( 'businesspress_cron' );
if( $timestamp == false ) {
wp_schedule_event( time(), 'hourly', 'businesspress_cron' );
}
}
function dashboard_hide_php_update_recommended() {
if( !$this->check_user_permission() ) {
remove_meta_box( 'dashboard_php_nag', get_current_screen(), 'normal' );
}
}
function dashboard_hide_welcome() {
if( !$this->check_user_permission() ) {
remove_action( 'welcome_panel', 'wp_welcome_panel' );
}
}
function dashboard_hide_wordpress_events_and_news() {
if( !$this->check_user_permission() ) {
remove_meta_box( 'dashboard_primary', get_current_screen(), 'side' );
}
}
function deactivate() {
}
// checks the release date for the WordPress version and if it's less than 5 days old it's not permitted for auto update
function delay_core_updates( $update, $item ) {
if( $update ) {
$aBlockedUpdates = get_site_option('businesspress_core_update_delay',array());
$aVersions = $this->cache_core_version_info();
if( $aVersions && !empty($aVersions['data']) && !empty($aVersions['data'][$item->current]) ) {
if( strtotime($aVersions['data'][$item->current]) + 5*24*3600 > time() ) {
$aBlockedUpdates[$item->current] = time();
update_site_option('businesspress_core_update_delay', $aBlockedUpdates );
return false; // block the update if it's less than 5 days old
} else {
unset($aBlockedUpdates[$item->current]);
update_site_option('businesspress_core_update_delay', $aBlockedUpdates );
}
}
}
// todo: this might trigger (if notify_email is set in the WP API response) an email notification, consider blocking it with send_core_update_notification_email filter
return $update;
}
function disable_deactivation() {
if( isset($_POST['action']) && $_POST['action'] == 'deactivate-selected' && isset($_POST['checked']) && is_array($_POST['checked']) ) {
foreach( $_POST['checked'] AS $key => $value ) {
if( stripos($value,'businesspress') !== false ) {
unset($_POST['checked'][$key]);
}
}
}
if( isset($_GET['action']) && $_GET['action'] == 'deactivate' && isset($_GET['plugin']) ) {
if( stripos($_GET['plugin'],'businesspress') !== false ) {
wp_die(__('Sorry, you are not allowed to deactivate plugins for this site.'));
}
}
}
function disable_xmlrpc() {
if( $this->get_setting('disable-xml-rpc') ) {
add_filter('xmlrpc_enabled', '__return_false');
remove_action( 'wp_head', 'rsd_link' );
remove_action( 'wp_head', 'wlwmanifest_link' );
if( stripos($_SERVER['REQUEST_URI'],'/xmlrpc.php') !== false ) die();
}
if( $this->get_setting('xml-rpc-key') ) {
remove_action( 'wp_head', 'rsd_link' );
remove_action( 'wp_head', 'wlwmanifest_link' );
if( stripos($_SERVER['REQUEST_URI'],'/xmlrpc.php') !== false && stripos($_SERVER['REQUEST_URI'], $this->get_setting('xml-rpc-key') ) === false ) die();
}
}
function editor_disable( $can ) {
if( $this->editor_is_disabled() ) {
return false;
}
return $can;
}
function editor_is_disabled( $post_id = false ) {
global $post;
if( !$post_id && $post && !empty($post->ID)) {
$post_id = $post->ID;
}
$disabled = false;
// check the old Foliopress WYSIWYG postmeta meta too
// if Foliopress WYSIWYG meta is present and it was the last editor used to modify the post
$fp_wysiwyg = get_post_meta( $post_id, 'wysiwyg', true );
if( $fp_wysiwyg && !empty($fp_wysiwyg['plain_text_editing']) && $fp_wysiwyg['plain_text_editing'] ) {
$disabled = true;
}
if( !$disabled ) {
$plain_text_editing = get_post_meta( $post_id, 'plain_text_editing', true );
if( !empty($plain_text_editing['plain_text_editing']) && $plain_text_editing['plain_text_editing'] ) {
$disabled = true;
}
}
return $disabled;
}
function editor_disable_checkbox() {
$disabled = $this->editor_is_disabled();
if( $disabled ) : ?>
<label for="plain_text_editing">
<input name="plain_text_editing" type="checkbox" id="plain_text_editing" value="true" <?php checked(1,$disabled); ?> />
<?php _e('Plain text editing', 'businesspress'); ?>
<abbr title="<?php _e('This will disable Visual editor for this post, as well as the WP formating routine (wpautop). Turn this option off only if you are sure this post won\'t get destroyed by it.', 'businesspress') ?>">(?)</abbr>
</label>
<?php endif;
}
function editor_disable_checkbox_save($post_id) {
// If this is a revision, bail
if( wp_is_post_revision($post_id) ) {
return;
}
if( !empty($_POST['plain_text_editing']) ) {
update_post_meta($post_id,'plain_text_editing',true);
} else {
delete_post_meta($post_id,'plain_text_editing');
}
// update the old Foliopress WYSIWYG postmeta meta too, if it's there
$fp_wysiwyg = get_post_meta( $post_id, 'wysiwyg', true );
if( $fp_wysiwyg ) {
$fp_wysiwyg['plain_text_editing'] = !empty($_POST['plain_text_editing']);
update_post_meta($post_id,'plain_text_editing',$fp_wysiwyg);
}
}
function editor_disabled_thus_no_wpautop( $post_content ) {
if( $this->editor_is_disabled() ) {
remove_filter ('the_content', 'wpautop');
}
return $post_content;
}
function fail2ban_404() {
if( preg_match( '~\.(bmp|css|eot|gif|ico|jpe|jpeg|jpg|js|m3u8|mp3|mp4|ogg|pdf|png|svg|tiff|ts|ttf|txt|vtt|webm|webp|woff|woff2)~i', $_SERVER['REQUEST_URI'] ) ) return;
if( $_SERVER['REQUEST_URI'] == '/apple-app-site-association' || $_SERVER['REQUEST_URI'] == '/.well-known/apple-app-site-association' ) return;
if( stripos($_SERVER['REQUEST_URI'], 'fv-gravatar-cache' ) !== false ) return;
if( stripos($_SERVER['REQUEST_URI'], 'null' ) !== false ) return;
if( preg_match( '~(Mediapartners-Google|googlebot|bingbot)~i', $_SERVER['HTTP_USER_AGENT'] ) ) return;
if( !is_404() || function_exists('bbp_is_single_user') && bbp_is_single_user() ) return;
$this->fail2ban_openlog(LOG_AUTH);
syslog( LOG_INFO,'BusinessPress fail2ban 404 error - '.$_SERVER['REQUEST_URI'].' from '.$this->get_remote_addr() );
}
function fail2ban_login( $username ) {
$msg = (wp_cache_get($username, 'userlogins'))
? "Authentication failure for $username from "
: "Authentication attempt for unknown user $username from ";
$this->fail2ban_openlog();
syslog( LOG_INFO,'BusinessPress fail2ban login error - '.$msg.$this->get_remote_addr() );
}
function fail2ban_lostpassword( $errors ) {
if( $errors && method_exists($errors,'get_error_codes') && $errors->get_error_codes() ) {
$this->fail2ban_openlog();
syslog( LOG_INFO,'BusinessPress fail2ban login lostpassword error - user '.( !empty($_POST['user_login']) ? $_POST['user_login'] : '?' ).' doesn\'t exists from '.$this->get_remote_addr() );
}
}
function fail2ban_openlog($log = LOG_AUTH, $daemon = 'wordpress') {
$host = array_key_exists('WP_FAIL2BAN_HTTP_HOST',$_ENV) ? $_ENV['WP_FAIL2BAN_HTTP_HOST'] : $_SERVER['HTTP_HOST'];
openlog($daemon."($host)", LOG_NDELAY|LOG_PID, $log);
}
function fail2ban_waf() {
// If a phrase starts with / it means it only triggers if it's the start of the request URL or requested from a folder, but it will work if used in query string without /
// This way you can search for .ssh or phpmyadmin in articles using site.com/?s=phpmyadmin and not get banned
// TODO: What if I search for phpmyadmin in bbPress ? site.com/support/search/phpmyadmin
$rules = array(
'/.env',
'/.github/workflows',
'/.ssh',
'/boot.ini',
'/data/admin/allowurl.txt',
'etc/passwd',
'/ftpsync.settings',
' onerror=',
' onload=',
'/phpMyAdmin/server_import.php',
'/phpmyadmin/scripts/setup.php',
'ueditor/net/controller.ashx',
'/win.ini',
'/wp-config.php',
's=/admin/index/dologin',
);
$match = false;
foreach( $rules AS $rule ) {
if(
stripos( $_SERVER['REQUEST_URI'], $rule ) !== false ||
stripos( $_SERVER['REQUEST_URI'], urlencode($rule) ) !== false ||
stripos( $_SERVER['REQUEST_URI'], urlencode( urlencode($rule) ) ) !== false ||
stripos( $_SERVER['REQUEST_URI'], urlencode( urlencode( urlencode($rule) ) ) ) !== false
) {
$match = $rule;
}
}
// Requests like:
// /?s=/index/%5Cthink%5Capp/invokefunction&function=call_user_func_array&vars[0]=file_put_contents&vars[1][]=xml1.php&vars[1][]=%3C?php%20@eval(_POST[terry]);print(md5(123));?%3E
if( !empty($_GET['function']) && $_GET['function'] == 'call_user_func_array' ) {
if( !empty($_GET['vars']) ) {
$vars = json_encode($_GET['vars']);
foreach( array(
'eval',
'file_put_contents',
) AS $keyword ) {
if( stripos( $vars, $keyword ) !== false ) {
$match = 'call_user_func_array with '.$keyword;
}
}
}
}
if( $match ) {
$this->fail2ban_openlog();
syslog( LOG_INFO,'BusinessPress WAF - '.$match.' request - '.$_SERVER['REQUEST_URI'].' from '.$this->get_remote_addr() );
exit;
}
}
function fail2ban_xmlrpc( $error ) {
$this->fail2ban_openlog();
syslog( LOG_INFO,'BusinessPress fail2ban login error - XML-RPC authentication failure from '.$this->get_remote_addr() );
return $error;
}
function fail2ban_xmlrpc_ping( $ixr_error ) {
if( $ixr_error->code === 48 ) return $ixr_error;
$this->fail2ban_openlog();
syslog( LOG_INFO,'BusinessPress fail2ban pingback error - XML-RPC Pingback error '.$ixr_error->code.' generated from '.$this->get_remote_addr() );
return $ixr_error;
}
function get_branch_latest() {
$sLatest = false;
if( $branch = $this->get_version_branch() ) {
$aVersions = $this->cache_core_version_info();
if( $aVersions && count($aVersions['data']) > 0 ) {
foreach( $aVersions['data'] AS $version => $date ) {
if( stripos($version,$branch) === 0 && version_compare($version,$sLatest) == 1 ) {
$sLatest = $version;
}
}
}
}
return $sLatest;
}
function get_disallowed_caps() {
$aCaps = array();
$settings_to_capabilities = array(
'cap_activate' => array('activate_plugins', 'switch_themes', 'deactivate_plugins'),
'cap_update' => array('update_plugins', 'update_themes'),
'cap_install' => array('install_plugins', 'install_themes', 'delete_plugins', 'delete_themes', 'edit_plugins', 'edit_themes'),
'cap_export' => array('export'),
'cap_core' => array('update_core', 'view_site_health_checks', 'update_languages')
);
// Disallow different capabilities based on the "Allow other admins to" settings NOT enabled
foreach( $settings_to_capabilities AS $setting => $capabilities ) {
if( empty($this->aOptions[$setting]) || !$this->aOptions[$setting] ) {
$aCaps = array_merge( $aCaps, $capabilities );
}
}
return $aCaps;
}
function get_email_domain( $email ) {
return preg_replace( '~.+@~', '', $email );
}
function get_remote_addr() {
if( isset($_SERVER['HTTP_X_PULL']) && strlen($_SERVER['HTTP_X_PULL']) > 0 && $_SERVER['HTTP_X_PULL'] == $this->aOptions['xpull-key'] ) {
return (false===($len = strpos($_SERVER['HTTP_X_FORWARDED_FOR'],',')))
? $_SERVER['HTTP_X_FORWARDED_FOR']
: substr($_SERVER['HTTP_X_FORWARDED_FOR'],0,$len);
}
$aProxies = array();
// https://www.maxcdn.com/one/tutorial/ip-blocks/
$aMaxCDN = array( '108.161.176.0/20', '94.46.144.0/20', '146.88.128.0/20', '198.232.124.0/22', '23.111.8.0/22', '217.22.28.0/22', '64.125.76.64/27', '64.125.76.96/27', '64.125.78.96/27', '64.125.78.192/27', '64.125.78.224/27', '64.125.102.32/27', '64.125.102.64/27', '64.125.102.96/27', '94.31.27.64/27', '94.31.33.128/27', '94.31.33.160/27', '94.31.33.192/27', '94.31.56.160/27', '177.54.148.0/24', '185.18.207.64/26', '50.31.249.224/27', '50.31.251.32/28', '119.81.42.192/27', '119.81.104.96/28', '119.81.67.8/29', '119.81.0.104/30', '119.81.1.144/30', '27.50.77.226/32', '27.50.79.130/32', '119.81.131.130/32', '119.81.131.131/32', '216.12.211.59/32', '216.12.211.60/32', '37.58.110.67/32', '37.58.110.68/32', '158.85.206.228/32', '158.85.206.231/32', '174.36.204.195/32', '174.36.204.196/32', '151.139.0.0/19', '94.46.144.0/21', '103.66.28.0/22', '103.228.104.0/22' );
$aProxies = array_merge( $aProxies, $aMaxCDN );
// https://www.cloudflare.com/ips-v4
$aCloudFlareIP4 = array( '103.21.244.0/22','103.22.200.0/22','103.31.4.0/22','104.16.0.0/12','108.162.192.0/18','131.0.72.0/22','141.101.64.0/18','162.158.0.0/15','172.64.0.0/13','173.245.48.0/20','188.114.96.0/20','190.93.240.0/20','197.234.240.0/22','198.41.128.0/17','199.27.128.0/21' );
$aProxies = array_merge( $aProxies, $aCloudFlareIP4 );
// https://www.cloudflare.com/ips-v6
$aCloudFlareIP6 = array( '2400:cb00::/32', '2405:8100::/32', '2405:b500::/32', '2606:4700::/32', '2803:f800::/32', '2c0f:f248::/32', '2a06:98c0::/29' );
$aProxies = array_merge( $aProxies, $aCloudFlareIP6 );
if (defined('WP_FAIL2BAN_PROXIES')) { // todo: check this out
$aProxies = array_merge( $aProxies, explode( ',' ,WP_FAIL2BAN_PROXIES ) );
}
if (array_key_exists('HTTP_X_FORWARDED_FOR',$_SERVER)) {
$ip = ip2long($_SERVER['REMOTE_ADDR']);
foreach( $aProxies as $proxy ) {
if (2 == count($cidr = explode('/',$proxy))) {
$net = ip2long($cidr[0]);
$mask = ~ ( pow(2, (32 - $cidr[1])) - 1 );
} else {
$net = ip2long($proxy);
$mask = -1;
}
if ($net == ($ip & $mask)) {
return (false===($len = strpos($_SERVER['HTTP_X_FORWARDED_FOR'],',')))
? $_SERVER['HTTP_X_FORWARDED_FOR']
: substr($_SERVER['HTTP_X_FORWARDED_FOR'],0,$len);
}
}
}
return $_SERVER['REMOTE_ADDR'];
}
function get_contact_email() {
if( $this->get_setting('contact_email') ) {
return $this->get_setting('contact_email');
}
/*$current_user = wp_get_current_user(); // gets admin email if it matches the domain
$domain = $this->get_whitelist_domain();
if( stripos(get_option('admin_email'),'@'.$domain) !== false ) {
return get_option('admin_email');
}
$aUsers = get_users( 'role=administrator' ); // gets first admin user if his email matches domain
if( $aUsers ) {
foreach( $aUsers AS $objUser ) {
if( stripos($objUser->user_email,'@'.$domain) !== false ) {
return $objUser->user_email;
}
}
}*/
return -1;
}
function get_whitelist_domain() {
return !empty($this->aOptions['domain']) ? $this->aOptions['domain'] : false;
}
function get_whitelist_email() {
return !empty($this->aOptions['email']) ? $this->aOptions['email'] : false;
}
/* this is basic handler for getting FVSB data from database
* @param $key = can contain {all | version | upgradeType | capsDisabled | adminEmail}
*/
function get_setting( $key ) {
$this->aOptions = is_multisite() ? get_site_option('businesspress') : get_option( 'businesspress' );
if( isset($this->aOptions[$key]) ) {
if( $this->aOptions[$key] === true || $this->aOptions[$key] === 'true' ) return true;
return trim($this->aOptions[$key]);
}
//default settings
if( $key == 'search-results-domain' ) return true;
if( $key == 'disable-rest-api' ) return true;
if( $key == 'disable-emojis' ) return true;
if( $key == 'remove-generator' ) return true;
if( $key == 'hide-notices' ) return false;
if( $key == 'autoupdates_vcs' ) return true;
if( $key == 'clickjacking-protection' ) return true;
if( $key == 'disable-user-login-scanning' ) return true;
if( $key == 'login-lockout' ) return true;
if( $key == 'login-duration') return '2_weeks';
return false;
}
function get_setting_db($key) {
return is_multisite() ? get_site_option($key) : get_option($key);
}
function get_settings_url() {
$sURL = site_url('wp-admin/options-general.php?page=businesspress');
if( is_multisite() && $aSitewidePlugins = get_site_option( 'active_sitewide_plugins') ) {
if( is_array($aSitewidePlugins) && stripos( ','.implode( ',', array_keys($aSitewidePlugins) ), ',businesspress' ) !== false ) {