PluginProbe ʕ •ᴥ•ʔ
Advanced Google reCAPTCHA / 1.27
Advanced Google reCAPTCHA v1.27
5.39 trunk 1.0.1 1.0.10 1.0.11 1.0.12 1.0.13 1.0.14 1.0.15 1.0.2 1.0.3 1.0.4 1.0.5 1.0.6 1.0.7 1.0.8 1.0.9 1.1 1.11 1.12 1.13 1.14 1.15 1.16 1.17 1.18 1.19 1.20 1.21 1.22 1.23 1.24 1.25 1.26 1.27 1.28 1.29 1.30 1.31 1.32 1.33 1.34 1.35
advanced-google-recaptcha / libs / setup.php
advanced-google-recaptcha / libs Last commit date
admin.php 1 year ago ajax.php 1 year ago captcha.php 1 year ago functions.php 1 year ago setup.php 1 year ago stats.php 1 year ago utility.php 1 year ago
setup.php
676 lines
1 <?php
2
3 /**
4 * WP Captcha
5 * https://getwpcaptcha.com/
6 * (c) WebFactory Ltd, 2022 - 2025, www.webfactoryltd.com
7 */
8
9 class WPCaptcha_Setup extends WPCaptcha
10 {
11 static $wp_filesystem;
12
13 /**
14 * Actions to run on load, but init would be too early as not all classes are initialized
15 *
16 * @return null
17 */
18 static function load_actions()
19 {
20 self::register_custom_tables();
21 } // admin_actions
22
23 static function setup_wp_filesystem()
24 {
25 global $wp_filesystem;
26
27 if (empty($wp_filesystem)) {
28 require_once ABSPATH . '/wp-admin/includes/file.php';
29 WP_Filesystem();
30 }
31
32 self::$wp_filesystem = $wp_filesystem;
33 return self::$wp_filesystem;
34 } // setup_wp_filesystem
35
36 /**
37 * Check if user has the minimal WP version required by WP Captcha
38 *
39 * @since 5.0
40 *
41 * @return bool
42 *
43 */
44 static function check_wp_version($min_version)
45 {
46 if (!version_compare(get_bloginfo('version'), $min_version, '>=')) {
47 add_action('admin_notices', array(__CLASS__, 'notice_min_wp_version'));
48 return false;
49 } else {
50 return true;
51 }
52 } // check_wp_version
53
54 /**
55 * Check if user has the minimal PHP version required by WP Captcha
56 *
57 * @since 5.0
58 *
59 * @return bool
60 *
61 */
62 static function check_php_version($min_version)
63 {
64 if (!version_compare(phpversion(), $min_version, '>=')) {
65 add_action('admin_notices', array(__CLASS__, 'notice_min_php_version'));
66 return false;
67 } else {
68 return true;
69 }
70 } // check_wp_version
71
72 /**
73 * Display error message if WP version is too low
74 *
75 * @since 5.0
76 *
77 * @return null
78 *
79 */
80 static function notice_min_wp_version()
81 {
82 /* translators: %1$s is replaced with the current WordPress version, %2$s is replaced with the URL to WordPress update page in the Dashboard */
83 WPCaptcha_Utility::wp_kses_wf('<div class="error"><p>' . sprintf(__('WP Captcha plugin <b>requires WordPress version 4.6</b> or higher to function properly. You are using WordPress version %1$s. Please <a href="%2$s">update it</a>.', 'advanced-google-recaptcha'), get_bloginfo('version'), admin_url('update-core.php')) . '</p></div>');
84 } // notice_min_wp_version_error
85
86 /**
87 * Display error message if PHP version is too low
88 *
89 * @since 5.0
90 *
91 * @return null
92 *
93 */
94 static function notice_min_php_version()
95 {
96 /* translators: %1$s is replaced with the current PHP version, %2$s is replaced with the URL to WordPress update PHP support page */
97 WPCaptcha_Utility::wp_kses_wf('<div class="error"><p>' . sprintf(__('WP Captcha plugin <b>requires PHP version 5.6.20</b> or higher to function properly. You are using PHP version %1$s. Please <a href="%2$s" target="_blank">update it</a>.', 'advanced-google-recaptcha'), phpversion(), 'https://wordpress.org/support/update-php/') . '</p></div>');
98 } // notice_min_wp_version_error
99
100
101 /**
102 * activate doesn't get fired on upgrades so we have to compensate
103 *
104 * @since 5.0
105 *
106 * @return null
107 *
108 */
109 public static function maybe_upgrade()
110 {
111 $meta = self::get_meta();
112 if (empty($meta['database_ver']) || $meta['database_ver'] < self::$version) {
113 self::create_custom_tables();
114 }
115
116
117 // Copy options from free
118 $options = get_option(WPCAPTCHA_OPTIONS_KEY);
119 if (false === $options) {
120 $free_options = get_option("agr_options");
121 if (false !== $free_options && isset($free_options['enable_login'])) {
122 $options['captcha'] = $free_options['captcha_type'] == 'v3'?'recaptchav3':'recaptchav2';
123 $options['captcha_site_key'] = $free_options['site_key'];
124 $options['captcha_secret_key'] = $free_options['secret_key'];
125 $options['captcha_show_login'] = $free_options['enable_login'];
126 $options['captcha_show_wp_registration'] = $free_options['enable_register'];
127 $options['captcha_show_wp_lost_password'] = $free_options['enable_lost_password'];
128 $options['captcha_show_wp_comment'] = $free_options['enable_comment_form'];
129 $options['captcha_show_woo_registration'] = $free_options['enable_woo_register'];
130 $options['captcha_show_woo_checkout'] = $free_options['enable_woo_checkout'];
131 $options['captcha_show_edd_registration'] = $free_options['enable_edd_register'];
132 $options['captcha_show_bp_registration'] = $free_options['enable_bp_register'];
133
134 update_option(WPCAPTCHA_OPTIONS_KEY, $options);
135 ///delete_option("agr_options");
136 }
137 }
138 } // maybe_upgrade
139
140
141 /**
142 * Get plugin options
143 *
144 * @since 5.0
145 *
146 * @return array options
147 *
148 */
149 static function get_options()
150 {
151 $options = get_option(WPCAPTCHA_OPTIONS_KEY, array());
152
153 if (!is_array($options)) {
154 $options = array();
155 }
156 $options = array_merge(self::default_options(), $options);
157
158 return $options;
159 } // get_options
160
161 /**
162 * Register all settings
163 *
164 * @since 5.0
165 *
166 * @return false
167 *
168 */
169 static function register_settings()
170 {
171 register_setting(WPCAPTCHA_OPTIONS_KEY, WPCAPTCHA_OPTIONS_KEY, array(__CLASS__, 'sanitize_settings'));
172 } // register_settings
173
174
175 /**
176 * Set default options
177 *
178 * @since 5.0
179 *
180 * @return null
181 *
182 */
183 static function default_options()
184 {
185 $defaults = array(
186 'login_protection' => 0,
187 'max_login_retries' => 3,
188 'retries_within' => 5,
189 'lockout_length' => 60,
190 'lockout_invalid_usernames' => 1,
191 'mask_login_errors' => 0,
192 'show_credit_link' => 0,
193 'anonymous_logging' => 0,
194 'block_bots' => 0,
195 'log_passwords' => 0,
196 'instant_block_nonusers' => 0,
197 'cookie_lifetime' => 14,
198 'country_blocking_mode' => 'none',
199 'country_blocking_countries' => '',
200 'block_undetermined_countries' => 0,
201 'captcha' => 'disabled',
202 'captcha_secret_key' => '',
203 'captcha_site_key' => '',
204 'captcha_show_login' => 1,
205 'captcha_show_wp_registration' => 1,
206 'captcha_show_wp_lost_password' => 1,
207 'captcha_show_wp_comment' => 1,
208 'captcha_show_woo_registration' => 0,
209 'captcha_show_woo_checkout' => 0,
210 'captcha_show_edd_registration' => 0,
211 'captcha_show_bp_registration' => 0,
212 'login_url' => '',
213 'login_redirect_url' => '',
214 'global_block' => 0,
215 'country_global_block' => 0,
216 'uninstall_delete' => 0,
217 'block_message' => 'We\'re sorry, but your IP has been blocked due to too many recent failed login attempts.',
218 'block_message_country' => 'We\'re sorry, but access from your location is not allowed.',
219 'global_unblock_key' => 'agr' . md5(wp_generate_password(24)),
220 'whitelist' => array(),
221 'firewall_block_bots' => 0,
222 'firewall_directory_traversal' => 0,
223 'design_enable' => 0,
224 'design_template' => 'orange',
225 'design_background_color' => '',
226 'design_background_image' => '',
227 'design_logo' => '',
228 'design_logo_url' => '',
229 'design_logo_width' => '',
230 'design_logo_height' => '',
231 'design_logo_margin_bottom' => '',
232 'design_text_color' => '#3c434a',
233 'design_link_color' => '#2271b1',
234 'design_link_hover_color' => '#135e96',
235 'design_form_border_color' => '#FFFFFF',
236 'design_form_border_width' => 1,
237 'design_form_width' => '',
238 'design_form_width' => '',
239 'design_form_height' => '',
240 'design_form_padding' => 26,
241 'design_form_border_radius' => 2,
242 'design_form_background_color' => '',
243 'design_form_background_image' => '',
244 'design_label_font_size' => 14,
245 'design_label_text_color' => '#3c434a',
246 'design_field_font_size' => 13,
247 'design_field_text_color' => '#3c434a',
248 'design_field_border_color' => '#8c8f94',
249 'design_field_border_width' => 1,
250 'design_field_border_radius' => 2,
251 'design_field_background_color' => '#ffffff',
252 'design_button_font_size' => 14,
253 'design_button_text_color' => '',
254 'design_button_border_color' => '#2271b1',
255 'design_button_border_width' => 0,
256 'design_button_border_radius' => 2,
257 'design_button_background_color' => '#2271b1',
258 'design_button_hover_text_color' => '',
259 'design_button_hover_border_color' => '',
260 'design_button_hover_background_color' => '',
261 'design_custom_css' => ''
262 );
263
264 return $defaults;
265 } // default_options
266
267
268 /**
269 * Sanitize settings on save
270 *
271 * @since 5.0
272 *
273 * @return array updated options
274 *
275 */
276 static function sanitize_settings($options)
277 {
278 $old_options = self::get_options();
279
280 if (isset($options['captcha_verified']) && $options['captcha_verified'] != 1 && $options['captcha'] != 'disabled') {
281 $options['captcha'] = $old_options['captcha'];
282 $options['captcha_site_key'] = $old_options['captcha_site_key'];
283 $options['captcha_secret_key'] = $old_options['captcha_secret_key'];
284 }
285
286 if (isset($options['captcha']) && ($options['captcha'] == 'disabled' || $options['captcha'] == 'builtin')) {
287 $options['captcha_site_key'] = '';
288 $options['captcha_secret_key'] = '';
289 }
290
291 if (!isset($options['login_protection'])) {
292 $options['login_protection'] = 0;
293 }
294
295 if (!isset($options['lockout_invalid_usernames'])) {
296 $options['lockout_invalid_usernames'] = 0;
297 }
298
299 if (!isset($options['mask_login_errors'])) {
300 $options['mask_login_errors'] = 0;
301 }
302
303 if (!isset($options['anonymous_logging'])) {
304 $options['anonymous_logging'] = 0;
305 }
306
307 if (!isset($options['block_bots'])) {
308 $options['block_bots'] = 0;
309 }
310
311 if (!isset($options['instant_block_nonusers'])) {
312 $options['instant_block_nonusers'] = 0;
313 }
314
315 if (!isset($options['country_blocking_mode'])) {
316 $options['country_blocking_mode'] = 0;
317 }
318
319 if (!isset($options['block_undetermined_countries'])) {
320 $options['block_undetermined_countries'] = 0;
321 }
322
323 if (!isset($options['global_block'])) {
324 $options['global_block'] = 0;
325 }
326
327 if (!isset($options['country_global_block'])) {
328 $options['country_global_block'] = 0;
329 }
330
331 if (!isset($options['uninstall_delete'])) {
332 $options['uninstall_delete'] = 0;
333 }
334
335 if (!isset($options['show_credit_link'])) {
336 $options['show_credit_link'] = 0;
337 }
338
339 if (!isset($options['firewall_block_bots'])) {
340 $options['firewall_block_bots'] = 0;
341 }
342
343 if (!isset($options['firewall_directory_traversal'])) {
344 $options['firewall_directory_traversal'] = 0;
345 }
346
347 if (!isset($options['log_passwords'])) {
348 $options['log_passwords'] = 0;
349 }
350
351 if (!isset($options['captcha_show_login'])) {
352 $options['captcha_show_login'] = 0;
353 }
354
355 if (!isset($options['captcha_show_wp_registration'])) {
356 $options['captcha_show_wp_registration'] = 0;
357 }
358
359 if (!isset($options['captcha_show_wp_lost_password'])) {
360 $options['captcha_show_wp_lost_password'] = 0;
361 }
362
363 if (!isset($options['captcha_show_wp_comment'])) {
364 $options['captcha_show_wp_comment'] = 0;
365 }
366
367 if (!isset($options['captcha_show_woo_registration'])) {
368 $options['captcha_show_woo_registration'] = 0;
369 }
370
371 if (!isset($options['captcha_show_woo_checkout'])) {
372 $options['captcha_show_woo_checkout'] = 0;
373 }
374
375 if (!isset($options['design_enable'])) {
376 $options['design_enable'] = 0;
377 }
378
379 if (!isset($options['captcha_show_edd_registration'])) {
380 $options['captcha_show_edd_registration'] = 0;
381 }
382
383 if (!isset($options['captcha_show_bp_registration'])) {
384 $options['captcha_show_bp_registration'] = 0;
385 }
386
387 if ($old_options['firewall_block_bots'] != $options['firewall_block_bots'] || $old_options['firewall_directory_traversal'] != $options['firewall_directory_traversal']) {
388 self::firewall_setup($options);
389 }
390
391 WPCaptcha_Utility::clear_3rdparty_cache();
392 $options['last_options_edit'] = current_time('mysql', true);
393
394 return array_merge($old_options, $options);
395 } // sanitize_settings
396
397 /**
398 * Get plugin metadata
399 *
400 * @since 5.0
401 *
402 * @return array meta
403 *
404 */
405 static function get_meta()
406 {
407 $meta = get_option(WPCAPTCHA_META_KEY, array());
408
409 if (!is_array($meta) || empty($meta)) {
410 $meta['first_version'] = self::get_plugin_version();
411 $meta['first_install'] = current_time('timestamp');
412 update_option(WPCAPTCHA_META_KEY, $meta);
413 }
414
415 return $meta;
416 } // get_meta
417
418 static function update_meta($key, $value)
419 {
420 $meta = get_option(WPCAPTCHA_META_KEY, array());
421 $meta[$key] = $value;
422 update_option(WPCAPTCHA_META_KEY, $meta);
423 } // update_meta
424
425 /**
426 * Register custom tables
427 *
428 * @since 5.0
429 *
430 * @return null
431 *
432 */
433 static function register_custom_tables()
434 {
435 global $wpdb;
436
437 $wpdb->wpcatcha_login_fails = $wpdb->prefix . 'wpc_login_fails';
438 $wpdb->wpcatcha_accesslocks = $wpdb->prefix . 'wpc_accesslocks';
439 } // register_custom_tables
440
441 /**
442 * Create custom tables
443 *
444 * @since 5.0
445 *
446 * @return null
447 *
448 */
449 static function create_custom_tables()
450 {
451 global $wpdb;
452 require_once ABSPATH . 'wp-admin/includes/upgrade.php';
453
454 self::register_custom_tables();
455
456 $wpcaptcha_login_fails = "CREATE TABLE " . $wpdb->wpcatcha_login_fails . " (
457 `login_attempt_ID` bigint(20) NOT NULL AUTO_INCREMENT,
458 `user_id` bigint(20) NOT NULL,
459 `login_attempt_date` datetime NOT NULL default '0000-00-00 00:00:00',
460 `login_attempt_IP` varchar(100) NOT NULL default '',
461 `failed_user` varchar(200) NOT NULL default '',
462 `failed_pass` varchar(200) NOT NULL default '',
463 `reason` varchar(200) NULL,
464 PRIMARY KEY (`login_attempt_ID`)
465 );";
466 dbDelta($wpcaptcha_login_fails);
467
468 $wpcaptcha_accesslocks = "CREATE TABLE " . $wpdb->wpcatcha_accesslocks . " (
469 `accesslock_ID` bigint(20) NOT NULL AUTO_INCREMENT,
470 `user_id` bigint(20) NOT NULL,
471 `accesslock_date` datetime NOT NULL default '0000-00-00 00:00:00',
472 `release_date` datetime NOT NULL default '0000-00-00 00:00:00',
473 `accesslock_IP` varchar(100) NOT NULL default '',
474 `reason` varchar(200) NULL,
475 `unlocked` smallint(20) NOT NULL default '0',
476 PRIMARY KEY (`accesslock_ID`)
477 );";
478 dbDelta($wpcaptcha_accesslocks);
479
480 self::update_meta('database_ver', self::$version);
481 } // create_custom_tables
482
483
484 static function firewall_setup($options = false)
485 {
486 self::setup_wp_filesystem();
487 self::firewall_remove_rules();
488
489 if (false === $options) {
490 $options = get_option(WPCAPTCHA_OPTIONS_KEY, array());
491 }
492
493 $htaccess = self::$wp_filesystem->get_contents(WPCaptcha_Utility::get_home_path() . '.htaccess');
494
495 $firewall_rules = [];
496 $firewall_rules[] = '# BEGIN WP Captcha Firewall';
497
498 if ($options['firewall_block_bots']) {
499 $firewall_rules[] = '<IfModule mod_rewrite.c>';
500
501 $firewall_rules[] = 'RewriteCond %{HTTP_USER_AGENT} (ahrefs|alexibot|majestic|mj12bot|rogerbot) [NC,OR]';
502 $firewall_rules[] = 'RewriteCond %{HTTP_USER_AGENT} (econtext|eolasbot|eventures|liebaofast|nominet|oppo\sa33) [NC,OR]';
503 $firewall_rules[] = 'RewriteCond %{HTTP_USER_AGENT} (ahrefs|alexibot|majestic|mj12bot|rogerbot) [NC,OR]';
504 $firewall_rules[] = 'RewriteCond %{HTTP_USER_AGENT} (econtext|eolasbot|eventures|liebaofast|nominet|oppo\sa33) [NC,OR]';
505 $firewall_rules[] = 'RewriteCond %{HTTP_USER_AGENT} (acapbot|acoonbot|asterias|attackbot|backdorbot|becomebot|binlar|blackwidow|blekkobot|blexbot|blowfish|bullseye|bunnys|butterfly|careerbot|casper|checkpriv|cheesebot|cherrypick|chinaclaw|choppy|clshttp|cmsworld|copernic|copyrightcheck|cosmos|crescent|cy_cho|datacha|demon|diavol|discobot|dittospyder|dotbot|dotnetdotcom|dumbot|emailcollector|emailsiphon|emailwolf|extract|eyenetie|feedfinder|flaming|flashget|flicky|foobot|g00g1e|getright|gigabot|go-ahead-got|gozilla|grabnet|grafula|harvest|heritrix|httrack|icarus6j|jetbot|jetcar|jikespider|kmccrew|leechftp|libweb|linkextractor|linkscan|linkwalker|loader|masscan|miner|mechanize|morfeus|moveoverbot|netmechanic|netspider|nicerspro|nikto|ninja|nutch|octopus|pagegrabber|petalbot|planetwork|postrank|proximic|purebot|pycurl|python|queryn|queryseeker|radian6|radiation|realdownload|scooter|seekerspider|semalt|siclab|sindice|sistrix|sitebot|siteexplorer|sitesnagger|skygrid|smartdownload|snoopy|sosospider|spankbot|spbot|sqlmap|stackrambler|stripper|sucker|surftbot|sux0r|suzukacz|suzuran|takeout|teleport|telesoft|true_robots|turingos|turnit|vampire|vikspider|voideye|webleacher|webreaper|webstripper|webvac|webviewer|webwhacker|winhttp|wwwoffle|woxbot|xaldon|xxxyy|yamanalab|yioopbot|youda|zeus|zmeu|zune|zyborg) [NC]';
506
507 $firewall_rules[] = 'RewriteCond %{REMOTE_HOST} (163data|amazonaws|colocrossing|crimea|g00g1e|justhost|kanagawa|loopia|masterhost|onlinehome|poneytel|sprintdatacenter|reverse.softlayer|safenet|ttnet|woodpecker|wowrack) [NC]';
508
509 $firewall_rules[] = 'RewriteCond %{HTTP_REFERER} (semalt\.com|todaperfeita) [NC,OR]';
510 $firewall_rules[] = 'RewriteCond %{HTTP_REFERER} (blue\spill|cocaine|ejaculat|erectile|erections|hoodia|huronriveracres|impotence|levitra|libido|lipitor|phentermin|pro[sz]ac|sandyauer|tramadol|troyhamby|ultram|unicauca|valium|viagra|vicodin|xanax|ypxaieo) [NC]';
511
512 $firewall_rules[] = 'RewriteRule .* - [F,L]';
513 $firewall_rules[] = '</IfModule>';
514 }
515
516 if ($options['firewall_directory_traversal']) {
517 $firewall_rules[] = '<IfModule mod_rewrite.c>';
518
519 $firewall_rules[] = 'RewriteCond %{QUERY_STRING} (((/|%2f){3,3})|((\.|%2e){3,3})|((\.|%2e){2,2})(/|%2f|%u2215)) [NC,OR]';
520 $firewall_rules[] = 'RewriteCond %{QUERY_STRING} (/|%2f)(:|%3a)(/|%2f) [NC,OR]';
521 $firewall_rules[] = 'RewriteCond %{QUERY_STRING} (/|%2f)(\*|%2a)(\*|%2a)(/|%2f) [NC,OR]';
522 $firewall_rules[] = 'RewriteCond %{QUERY_STRING} (absolute_|base|root_)(dir|path)(=|%3d)(ftp|https?) [NC,OR]';
523 $firewall_rules[] = 'RewriteCond %{QUERY_STRING} (/|%2f)(=|%3d|$&|_mm|cgi(\.|-)|inurl(:|%3a)(/|%2f)|(mod|path)(=|%3d)(\.|%2e)) [NC,OR]';
524
525 $firewall_rules[] = 'RewriteCond %{REQUEST_URI} (\^|`|<|>|\\\\|\|) [NC,OR]';
526 $firewall_rules[] = 'RewriteCond %{REQUEST_URI} ([a-z0-9]{2000,}) [NC]';
527
528 $firewall_rules[] = 'RewriteRule .* - [F,L]';
529 $firewall_rules[] = '</IfModule>';
530 }
531
532 $firewall_rules[] = '# END WP Captcha Firewall';
533
534 $htaccess = implode(PHP_EOL, $firewall_rules) . PHP_EOL . $htaccess;
535
536 if (count($firewall_rules) > 2) {
537 $firewall_test = self::firewall_test_htaccess($htaccess);
538 if (is_wp_error($firewall_test)) {
539 WPCaptcha_Utility::display_notice(
540 $firewall_test->get_error_message(),
541 "error"
542 );
543 } else {
544 self::$wp_filesystem->put_contents(WPCaptcha_Utility::get_home_path() . '.htaccess', $htaccess);
545 }
546 }
547 }
548
549 static function firewall_test_htaccess($new_content)
550 {
551 $uploads_directory = wp_upload_dir();
552 $test_id = wp_rand(1000, 9999);
553 $htaccess_test_folder = $uploads_directory['basedir'] . '/htaccess-test-' . $test_id . '/';
554 $htaccess_test_url = $uploads_directory['baseurl'] . '/htaccess-test-' . $test_id . '/';
555
556 // Create test directory and files
557 if (!self::$wp_filesystem->is_dir($htaccess_test_folder)) {
558 if (true !== self::$wp_filesystem->mkdir($htaccess_test_folder, 0777)) {
559 return new WP_Error('firewall_failed', 'Failed to create test directory. Please check that your uploads folder is writable.', false);
560 }
561 }
562
563 if (true !== self::$wp_filesystem->put_contents($htaccess_test_folder . 'index.html', 'htaccess-test-' . $test_id)) {
564 return new WP_Error('firewall_failed', 'Failed to create test files. Please check that your uploads folder is writable.', false);
565 }
566
567 if (true !== self::$wp_filesystem->put_contents($htaccess_test_folder . '.htaccess', $new_content)) {
568 return new WP_Error('firewall_failed', 'Failed to create test directory and files. Please check that your uploads folder is writeable.', false);
569 }
570
571 // Retrieve test file over http
572 $response = wp_remote_get($htaccess_test_url . 'index.html', array('sslverify' => false, 'redirection' => 0));
573 $response_code = wp_remote_retrieve_response_code($response);
574
575 // Remove Test Directory
576 self::$wp_filesystem->delete($htaccess_test_folder . '.htaccess');
577 self::$wp_filesystem->delete($htaccess_test_folder . 'index.html');
578 self::$wp_filesystem->rmdir($htaccess_test_folder);
579
580 // Check if test file content is what we expect
581 if ((in_array($response_code, range(200, 299)) && !is_wp_error($response) && wp_remote_retrieve_body($response) == 'htaccess-test-' . $test_id) || (in_array($response_code, range(300, 399)) && !is_wp_error($response))) {
582 return true;
583 } else {
584 return new WP_Error('firewall_failed', 'Unfortunately it looks like installing these firewall rules could cause your entire site, including the admin, to become inaccessible. Fix the errors before saving', false);
585 }
586 }
587
588 static function firewall_remove_rules()
589 {
590
591 if (self::$wp_filesystem->is_writable(WPCaptcha_Utility::get_home_path() . '.htaccess')) {
592
593 $htaccess_rules = self::$wp_filesystem->get_contents(WPCaptcha_Utility::get_home_path() . '.htaccess');
594
595 if ($htaccess_rules) {
596 $htaccess_rules = explode(PHP_EOL, $htaccess_rules);
597 $found = false;
598 $new_content = '';
599
600 foreach ($htaccess_rules as $htaccess_rule) {
601 if ($htaccess_rule == '# BEGIN WP Captcha Firewall') {
602 $found = true;
603 }
604
605 if (!$found) {
606 $new_content .= $htaccess_rule . PHP_EOL;
607 }
608
609 if ($htaccess_rule == '# END WP Captcha Firewall') {
610 $found = false;
611 }
612 }
613
614 $new_content = trim($new_content, PHP_EOL);
615
616 self::$wp_filesystem->put_contents(WPCaptcha_Utility::get_home_path() . '.htaccess', $new_content);
617
618 return true;
619 }
620 }
621
622 return false;
623 }
624
625 /**
626 * Actions on plugin activation
627 *
628 * @since 5.0
629 *
630 * @return null
631 *
632 */
633 static function activate()
634 {
635 self::create_custom_tables();
636 WPCaptcha_Admin::reset_pointers();
637 } // activate
638
639
640 /**
641 * Actions on plugin deactivaiton
642 *
643 * @since 5.0
644 *
645 * @return null
646 *
647 */
648 static function deactivate()
649 {
650 } // deactivate
651
652 /**
653 * Actions on plugin uninstall
654 *
655 * @since 5.0
656 *
657 * @return null
658 */
659 static function uninstall()
660 {
661 global $wpdb;
662
663 $options = get_option(WPCAPTCHA_OPTIONS_KEY, array());
664
665 if ($options['uninstall_delete'] == '1') {
666 delete_option(WPCAPTCHA_OPTIONS_KEY);
667 delete_option(WPCAPTCHA_META_KEY);
668 delete_option(WPCAPTCHA_POINTERS_KEY);
669 delete_option(WPCAPTCHA_NOTICES_KEY);
670 // phpcs:ignore db call warnings as we are using a custom table
671 $wpdb->query("DROP TABLE IF EXISTS " . $wpdb->prefix . "wpc_login_fails"); // phpcs:ignore
672 $wpdb->query("DROP TABLE IF EXISTS " . $wpdb->prefix . "wpc_accesslocks"); // phpcs:ignore
673 }
674 } // uninstall
675 } // class
676