Skip to content

Commit fbef175

Browse files
committed
move options storage to a JSON file
1 parent 16b1795 commit fbef175

File tree

6 files changed

+114
-109
lines changed

6 files changed

+114
-109
lines changed

inc/Bounce.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,12 @@
1717
*/
1818
class Bounce extends AbstractBounce implements IBounce
1919
{
20-
public function init(): void
20+
public function init(array $crowdSecConfig): bool
2121
{
22+
$this->settings = $crowdSecConfig;
2223
$this->initLogger();
24+
25+
return true;
2326
}
2427

2528
protected function getSettings(string $name)

inc/admin/init.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,15 @@
88

99
add_action('admin_notices', [new AdminNotice(), 'displayAdminNotice']);
1010

11+
function crowdsec_option_update_callback($name, $oldValue, $newValue)
12+
{
13+
if (0 === strpos($name, 'crowdsec_')) {
14+
writeStaticConfigFile($name, $newValue);
15+
}
16+
}
17+
1118
if (is_admin()) {
19+
add_action('updated_option', 'crowdsec_option_update_callback', 10, 3);
1220
function wrapErrorMessage(string $errorMessage)
1321
{
1422
return "CrowdSec: $errorMessage";

inc/bounce-current-ip.php

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,17 @@
22

33
require_once __DIR__.'/Bounce.php';
44

5+
require_once __DIR__.'/standalone-settings.php';
6+
57
function safelyBounceCurrentIp()
68
{
7-
$bounce = new Bounce();
8-
$bounce->init();
9-
$bounce->safelyBounce();
9+
global $crowdSecJsonStandaloneConfig;
10+
$crowdSecConfig = json_decode($crowdSecJsonStandaloneConfig, true);
11+
if (!count($crowdSecConfig)) {
12+
return;
13+
}
14+
$crowdSecBounce = new Bounce();
15+
if ($crowdSecBounce->init($crowdSecConfig)) {
16+
$crowdSecBounce->safelyBounce();
17+
}
1018
}

inc/options-config.php

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<?php
2+
3+
use CrowdSecBouncer\Constants;
4+
5+
function getCrowdSecOptionsConfig()
6+
{
7+
return [
8+
['name' => 'crowdsec_api_url', 'default' => '', 'autoInit' => true],
9+
['name' => 'crowdsec_api_key', 'default' => '', 'autoInit' => true],
10+
['name' => 'crowdsec_bouncing_level', 'default' => Constants::BOUNCING_LEVEL_NORMAL, 'autoInit' => true],
11+
['name' => 'crowdsec_public_website_only', 'default' => true, 'autoInit' => true],
12+
['name' => 'crowdsec_stream_mode', 'default' => false, 'autoInit' => true],
13+
['name' => 'crowdsec_stream_mode_refresh_frequency', 'default' => 60, 'autoInit' => true],
14+
['name' => 'crowdsec_cache_system', 'default' => Constants::CACHE_SYSTEM_PHPFS, 'autoInit' => true],
15+
['name' => 'crowdsec_redis_dsn', 'default' => '', 'autoInit' => true],
16+
['name' => 'crowdsec_memcached_dsn', 'default' => '', 'autoInit' => true],
17+
['name' => 'crowdsec_clean_ip_cache_duration', 'default' => Constants::CACHE_EXPIRATION_FOR_CLEAN_IP, 'autoInit' => true],
18+
['name' => 'crowdsec_bad_ip_cache_duration', 'default' => Constants::CACHE_EXPIRATION_FOR_BAD_IP, 'autoInit' => true],
19+
['name' => 'crowdsec_fallback_remediation', 'default' => Constants::REMEDIATION_CAPTCHA, 'autoInit' => true],
20+
['name' => 'crowdsec_hide_mentions', 'default' => false, 'autoInit' => true],
21+
['name' => 'crowdsec_trust_ip_forward', 'default' => '', 'autoInit' => true],
22+
['name' => 'crowdsec_trust_ip_forward_array', 'default' => [], 'autoInit' => true],
23+
['name' => 'crowdsec_theme_color_text_primary', 'default' => 'black', 'autoInit' => true],
24+
['name' => 'crowdsec_theme_color_text_secondary', 'default' => '#AAA', 'autoInit' => true],
25+
['name' => 'crowdsec_theme_color_text_button', 'default' => 'white', 'autoInit' => true],
26+
['name' => 'crowdsec_theme_color_text_error_message', 'default' => '#b90000', 'autoInit' => true],
27+
['name' => 'crowdsec_theme_color_background_page', 'default' => '#eee', 'autoInit' => true],
28+
['name' => 'crowdsec_theme_color_background_container', 'default' => 'white', 'autoInit' => true],
29+
['name' => 'crowdsec_theme_color_background_button', 'default' => '#626365', 'autoInit' => true],
30+
['name' => 'crowdsec_theme_color_background_button_hover', 'default' => '#333', 'autoInit' => true],
31+
['name' => 'crowdsec_theme_text_captcha_wall_tab_title', 'default' => 'Oops..', 'autoInit' => true],
32+
['name' => 'crowdsec_theme_text_captcha_wall_title', 'default' => 'Hmm, sorry but...', 'autoInit' => true],
33+
['name' => 'crowdsec_theme_text_captcha_wall_subtitle', 'default' => 'Please complete the security check.', 'autoInit' => true],
34+
['name' => 'crowdsec_theme_text_captcha_wall_refresh_image_link', 'default' => 'refresh image', 'autoInit' => true],
35+
['name' => 'crowdsec_theme_text_captcha_wall_captcha_placeholder', 'default' => 'Type here...', 'autoInit' => true],
36+
['name' => 'crowdsec_theme_text_captcha_wall_send_button', 'default' => 'CONTINUE', 'autoInit' => true],
37+
['name' => 'crowdsec_theme_text_captcha_wall_error_message', 'default' => 'Please try again.', 'autoInit' => true],
38+
['name' => 'crowdsec_theme_text_captcha_wall_footer', 'default' => '', 'autoInit' => true],
39+
['name' => 'crowdsec_theme_text_ban_wall_tab_title', 'default' => 'Oops..', 'autoInit' => true],
40+
['name' => 'crowdsec_theme_text_ban_wall_title', 'default' => '🤭 Oh!', 'autoInit' => true],
41+
['name' => 'crowdsec_theme_text_ban_wall_subtitle', 'default' => 'This page is protected against cyber attacks and your IP has been banned by our system.', 'autoInit' => true],
42+
['name' => 'crowdsec_theme_text_ban_wall_footer', 'default' => '', 'autoInit' => true],
43+
['name' => 'crowdsec_theme_custom_css', 'default' => '', 'autoInit' => true],
44+
['name' => 'crowdsec_random_log_folder', 'default' => bin2hex(random_bytes(64)), 'autoInit' => false],
45+
['name' => 'crowdsec_standalone_mode', 'default' => false, 'autoInit' => true],
46+
['name' => 'crowdsec_debug_mode', 'default' => false, 'autoInit' => true],
47+
];
48+
}

inc/plugin-setup.php

Lines changed: 28 additions & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,20 @@
11
<?php
22

3-
use CrowdSecBouncer\Constants;
3+
require_once __DIR__.'/options-config.php';
4+
5+
function writeStaticConfigFile($name = null, $newValue = null)
6+
{
7+
$crowdSecWpPluginOptions = getCrowdSecOptionsConfig();
8+
$data = [];
9+
foreach ($crowdSecWpPluginOptions as $option) {
10+
$data[$option['name']] = get_option($option['name']);
11+
}
12+
if ($name) {
13+
$data[$name] = $newValue;
14+
}
15+
$json = json_encode($data);
16+
file_put_contents(CROWDSEC_CONFIG_PATH, "<?php \$crowdSecJsonStandaloneConfig='$json';");
17+
}
418

519
/**
620
* The code that runs during plugin activation.
@@ -11,56 +25,17 @@ function activate_crowdsec_plugin()
1125

1226
// Set default options.
1327

14-
update_option('crowdsec_api_url', '');
15-
update_option('crowdsec_api_key', '');
16-
17-
update_option('crowdsec_bouncing_level', Constants::BOUNCING_LEVEL_NORMAL);
18-
update_option('crowdsec_public_website_only', true);
19-
20-
update_option('crowdsec_stream_mode', false);
21-
update_option('crowdsec_stream_mode_refresh_frequency', 60);
22-
23-
update_option('crowdsec_cache_system', Constants::CACHE_SYSTEM_PHPFS);
24-
update_option('crowdsec_redis_dsn', '');
25-
update_option('crowdsec_memcached_dsn', '');
26-
update_option('crowdsec_clean_ip_cache_duration', Constants::CACHE_EXPIRATION_FOR_CLEAN_IP);
27-
update_option('crowdsec_bad_ip_cache_duration', Constants::CACHE_EXPIRATION_FOR_BAD_IP);
28-
update_option('crowdsec_fallback_remediation', Constants::REMEDIATION_CAPTCHA);
29-
30-
update_option('crowdsec_hide_mentions', false);
31-
update_option('crowdsec_trust_ip_forward', '');
32-
update_option('crowdsec_trust_ip_forward_array', []);
33-
34-
update_option('crowdsec_theme_color_text_primary', 'black');
35-
update_option('crowdsec_theme_color_text_secondary', '#AAA');
36-
update_option('crowdsec_theme_color_text_button', 'white');
37-
update_option('crowdsec_theme_color_text_error_message', '#b90000');
38-
update_option('crowdsec_theme_color_background_page', '#eee');
39-
update_option('crowdsec_theme_color_background_container', 'white');
40-
update_option('crowdsec_theme_color_background_button', '#626365');
41-
update_option('crowdsec_theme_color_background_button_hover', '#333');
42-
43-
update_option('crowdsec_theme_text_captcha_wall_tab_title', 'Oops..');
44-
update_option('crowdsec_theme_text_captcha_wall_title', 'Hmm, sorry but...');
45-
update_option('crowdsec_theme_text_captcha_wall_subtitle', 'Please complete the security check.');
46-
update_option('crowdsec_theme_text_captcha_wall_refresh_image_link', 'refresh image');
47-
update_option('crowdsec_theme_text_captcha_wall_captcha_placeholder', 'Type here...');
48-
update_option('crowdsec_theme_text_captcha_wall_send_button', 'CONTINUE');
49-
update_option('crowdsec_theme_text_captcha_wall_error_message', 'Please try again.');
50-
update_option('crowdsec_theme_text_captcha_wall_footer', '');
51-
52-
update_option('crowdsec_theme_text_ban_wall_tab_title', 'Oops..');
53-
update_option('crowdsec_theme_text_ban_wall_title', '🤭 Oh!');
54-
update_option('crowdsec_theme_text_ban_wall_subtitle', 'This page is protected against cyber attacks and your IP has been banned by our system.');
55-
update_option('crowdsec_theme_text_ban_wall_footer', '');
56-
57-
update_option('crowdsec_theme_custom_css', '');
28+
$crowdSecWpPluginOptions = getCrowdSecOptionsConfig();
29+
foreach ($crowdSecWpPluginOptions as $crowdSecWpPluginOption) {
30+
if ($crowdSecWpPluginOption['autoInit']) {
31+
update_option($crowdSecWpPluginOption['name'], $crowdSecWpPluginOption['default']);
32+
}
33+
}
5834

5935
if (!get_option('crowdsec_random_log_folder')) {
6036
update_option('crowdsec_random_log_folder', bin2hex(random_bytes(64)));
6137
}
62-
63-
update_option('crowdsec_standalone_mode', false);
38+
writeStaticConfigFile();
6439
}
6540

6641
/**
@@ -82,49 +57,10 @@ function deactivate_crowdsec_plugin()
8257

8358
// Clean options.
8459

85-
delete_option('crowdsec_api_url');
86-
delete_option('crowdsec_api_key');
87-
88-
delete_option('crowdsec_bouncing_level');
89-
delete_option('crowdsec_public_website_only');
90-
91-
delete_option('crowdsec_stream_mode');
92-
delete_option('crowdsec_stream_mode_refresh_frequency');
93-
94-
delete_option('crowdsec_cache_system');
95-
delete_option('crowdsec_redis_dsn');
96-
delete_option('crowdsec_memcached_dsn');
97-
delete_option('crowdsec_clean_ip_cache_duration');
98-
delete_option('crowdsec_bad_ip_cache_duration');
99-
delete_option('crowdsec_fallback_remediation');
100-
101-
delete_option('crowdsec_hide_mentions');
102-
delete_option('crowdsec_trust_ip_forward');
103-
delete_option('crowdsec_trust_ip_forward_array');
104-
105-
delete_option('crowdsec_theme_color_text_primary');
106-
delete_option('crowdsec_theme_color_text_secondary');
107-
delete_option('crowdsec_theme_color_text_button');
108-
delete_option('crowdsec_theme_color_text_error_message');
109-
delete_option('crowdsec_theme_color_background_page');
110-
delete_option('crowdsec_theme_color_background_container');
111-
delete_option('crowdsec_theme_color_background_button');
112-
delete_option('crowdsec_theme_color_background_button_hover');
113-
114-
delete_option('crowdsec_theme_text_captcha_wall_tab_title');
115-
delete_option('crowdsec_theme_text_captcha_wall_title');
116-
delete_option('crowdsec_theme_text_captcha_wall_subtitle');
117-
delete_option('crowdsec_theme_text_captcha_wall_refresh_image_link');
118-
delete_option('crowdsec_theme_text_captcha_wall_captcha_placeholder');
119-
delete_option('crowdsec_theme_text_captcha_wall_send_button');
120-
delete_option('crowdsec_theme_text_captcha_wall_error_message');
121-
delete_option('crowdsec_theme_text_captcha_wall_footer');
122-
123-
delete_option('crowdsec_theme_text_ban_wall_tab_title');
124-
delete_option('crowdsec_theme_text_ban_wall_title');
125-
delete_option('crowdsec_theme_text_ban_wall_subtitle');
126-
delete_option('crowdsec_theme_text_ban_wall_footer');
127-
128-
delete_option('crowdsec_theme_custom_css');
129-
delete_option('crowdsec_standalone_mode');
60+
$crowdSecWpPluginOptions = getCrowdSecOptionsConfig();
61+
foreach ($crowdSecWpPluginOptions as $crowdSecWpPluginOption) {
62+
if ($crowdSecWpPluginOption['autoInit']) {
63+
delete_option($crowdSecWpPluginOption['name']);
64+
}
65+
}
13066
}

inc/standalone-bounce.php

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,18 @@
11
<?php
22

3-
/*
4-
We MUST load the WP CORE because boucing require these function, constants or variables :
5-
- wp_specialchars_decode
6-
- get_option
7-
- esc_attr
8-
- is_admin
9-
- $GLOBALS['pagenow']
10-
- WP_DEBUG
11-
*/
12-
require __DIR__.'/../../../../wp-load.php';
3+
define('CROWDSEC_STANDALONE_RUNNING_CONTEXT', true);
134

14-
$bounce = new Bounce();
15-
$bounce->init();
16-
$bounce->safelyBounce();
5+
require_once __DIR__.'/../vendor/autoload.php';
6+
7+
require_once __DIR__.'/Bounce.php';
8+
9+
require_once __DIR__.'/standalone-settings.php';
10+
require_once __DIR__.'/bouncer-instance-standalone.php';
11+
12+
$crowdSecConfig = json_decode($crowdSecJsonStandaloneConfig, true);
13+
14+
$crowdSecBounce = new Bounce();
15+
$crowdSecBounce->setDebug((bool) $crowdSecConfig['crowdsec_debug_mode']);
16+
if ($crowdSecBounce->init($crowdSecConfig)) {
17+
$crowdSecBounce->safelyBounce();
18+
}

0 commit comments

Comments
 (0)