Skip to content

Commit e758253

Browse files
committed
various fixes
1 parent 9ddec3f commit e758253

File tree

7 files changed

+87
-26
lines changed

7 files changed

+87
-26
lines changed

crowdsec.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ class WordpressCrowdSecBouncerException extends \RuntimeException
2525
}
2626

2727
require_once __DIR__.'/inc/constants.php';
28+
require_once __DIR__.'/inc/check-config.php';
2829
require_once __DIR__.'/inc/scheduling.php';
2930
require_once __DIR__.'/inc/plugin-setup.php';
3031
register_activation_hook(__FILE__, 'activate_crowdsec_plugin');

inc/admin/advanced-settings.php

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -60,20 +60,24 @@ function adminAdvancedSettings()
6060
add_settings_error('Technology', 'crowdsec_error', 'Technology: Incorrect cache technology selected.');
6161
}
6262

63-
$bouncer = getBouncerInstance();
64-
$bouncer->clearCache();
65-
$message = __('Cache system changed. Previous cache data has been cleared.');
66-
67-
// Update wp-cron schedule if stream mode is enabled
68-
if ((bool) get_option('crowdsec_stream_mode')) {
69-
$bouncer = getBouncerInstance($input); // Reload bouncer instance with the new cache system
70-
$result = $bouncer->warmBlocklistCacheUp();
71-
$message .= __(' As the stream mode is enabled, the cache has just been warmed up, '.($result > 0 ? 'there are now '.$result.' decisions' : 'there is now '.$result.' decision').' in cache.');
72-
scheduleBlocklistRefresh();
63+
64+
try {
65+
$bouncer = getBouncerInstance();
66+
$bouncer->clearCache();
67+
$message = __('Cache system changed. Previous cache data has been cleared.');
68+
69+
// Update wp-cron schedule if stream mode is enabled
70+
if ((bool) get_option('crowdsec_stream_mode')) {
71+
$bouncer = getBouncerInstance($input); // Reload bouncer instance with the new cache system
72+
$result = $bouncer->warmBlocklistCacheUp();
73+
$message .= __(' As the stream mode is enabled, the cache has just been warmed up, '.($result > 0 ? 'there are now '.$result.' decisions' : 'there is now '.$result.' decision').' in cache.');
74+
scheduleBlocklistRefresh();
75+
}
76+
AdminNotice::displaySuccess($message);
77+
} catch (WordpressCrowdSecBouncerException $e) {
78+
AdminNotice::displayError($e->getMessage());
7379
}
7480

75-
AdminNotice::displaySuccess($message);
76-
7781
return $input;
7882
}, ((CROWDSEC_CACHE_SYSTEM_PHPFS === get_option('crowdsec_cache_system')) ?
7983
'<input style="margin-right:10px" type="button" value="Prune now" class="button button-secondary" onclick="document.getElementById(\'crowdsec_ation_prune_cache\').submit();">' : '').

inc/admin/init.php

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,16 @@
88
add_action('admin_notices', [new AdminNotice(), 'displayAdminNotice']);
99

1010
if (is_admin()) {
11+
1112
function wrapErrorMessage(string $errorMessage)
1213
{
13-
return "There is a problem in your CrowdSec configuration. $errorMessage";
14+
return "CrowdSec: $errorMessage";
1415
}
1516

1617
function wrapBlockingErrorMessage(string $errorMessage)
1718
{
1819
return wrapErrorMessage($errorMessage).
19-
' <br><br>Important note: Until you fix this problem, you will not be protected against attacks.';
20+
'<br>Important: Until you fix this problem, <strong>the website will not be protected against attacks</strong>.';
2021
}
2122

2223
if (false) {
@@ -40,7 +41,7 @@ function clearBouncerCacheInAdminPage()
4041

4142
AdminNotice::displaySuccess($message);
4243
} catch (WordpressCrowdSecBouncerException $e) {
43-
getCrowdSecLoggerInstance()->error(null, [
44+
getCrowdSecLoggerInstance()->error('', [
4445
'type' => 'WP_EXCEPTION_WHILE_CLEARING_CACHE',
4546
'messsage' => $e->getMessage(),
4647
'code' => $e->getCode(),
@@ -62,11 +63,10 @@ function refreshBouncerCacheInAdminPage()
6263
if (get_option('crowdsec_stream_mode')) {
6364
$bouncer = getBouncerInstance();
6465
$result = $bouncer->refreshBlocklistCache();
65-
getCrowdSecLoggerInstance()->error(var_export($result, true));
6666
AdminNotice::displaySuccess(__(' The cache has just been refreshed ('.($result['new'] > 0 ? $result['new'].' new decisions' : $result['new'].' new decision').', '.$result['deleted'].' deleted).'));
6767
}
6868
} catch (WordpressCrowdSecBouncerException $e) {
69-
getCrowdSecLoggerInstance()->error(null, [
69+
getCrowdSecLoggerInstance()->error('', [
7070
'type' => 'WP_EXCEPTION_WHILE_REFRESHING_CACHE',
7171
'messsage' => $e->getMessage(),
7272
'code' => $e->getCode(),
@@ -85,7 +85,7 @@ function pruneBouncerCacheInAdminPage()
8585

8686
AdminNotice::displaySuccess(__('CrowdSec cache has just been pruned.'));
8787
} catch (WordpressCrowdSecBouncerException $e) {
88-
getCrowdSecLoggerInstance()->error(null, [
88+
getCrowdSecLoggerInstance()->error('', [
8989
'type' => 'WP_EXCEPTION_WHILE_PRUNING',
9090
'messsage' => $e->getMessage(),
9191
'code' => $e->getCode(),
@@ -116,8 +116,8 @@ function pruneBouncerCacheInAdminPage()
116116
// THEME
117117
add_action('admin_enqueue_scripts', function () {
118118
// enqueue all our scripts
119-
wp_enqueue_style('mypluginstyle', CROWDSEC_PLUGIN_URL.'assets/crowdsec.css');
120-
wp_enqueue_script('mypluginscript', CROWDSEC_PLUGIN_URL.'assets/crowdsec.js');
119+
wp_enqueue_style('mypluginstyle', CROWDSEC_PLUGIN_URL.'/inc/assets/crowdsec.css');
120+
wp_enqueue_script('mypluginscript', CROWDSEC_PLUGIN_URL.'inc/assets/crowdsec.js');
121121
});
122122

123123
// PLUGINS LIST
@@ -149,7 +149,7 @@ function addFieldCheckbox(string $optionName, string $label, string $optionGroup
149149
if ($previousState && !$currentState) {
150150
$onDeactivation();
151151
}
152-
getCrowdSecLoggerInstance()->info(null, ['type' => 'WP_SETTING_UPDATE', $optionName => $currentState]);
152+
getCrowdSecLoggerInstance()->info('', ['type' => 'WP_SETTING_UPDATE', $optionName => $currentState]);
153153
}
154154

155155
return $input;
@@ -176,7 +176,7 @@ function addFieldString(string $optionName, string $label, string $optionGroup,
176176

177177
if ($previousState !== $currentState) {
178178
$currentState = $onChange($currentState);
179-
getCrowdSecLoggerInstance()->info(null, ['type' => 'WP_SETTING_UPDATE', $optionName => $currentState]);
179+
getCrowdSecLoggerInstance()->info('', ['type' => 'WP_SETTING_UPDATE', $optionName => $currentState]);
180180
}
181181

182182
return $currentState;
@@ -200,7 +200,7 @@ function addFieldSelect(string $optionName, string $label, string $optionGroup,
200200

201201
if ($previousState !== $currentState) {
202202
$currentState = $onChange($currentState);
203-
getCrowdSecLoggerInstance()->info(null, ['type' => 'WP_SETTING_UPDATE', $optionName => $currentState]);
203+
getCrowdSecLoggerInstance()->info('', ['type' => 'WP_SETTING_UPDATE', $optionName => $currentState]);
204204
}
205205

206206
return $currentState;

inc/bounce-current-ip.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -144,12 +144,12 @@ function safelyBounceCurrentIp()
144144

145145
$everywhere = empty(get_option('crowdsec_public_website_only'));
146146
$shoudRun = ($everywhere || !is_admin());
147-
if ($shoudRun) {
147+
if ($shoudRun && isBouncerConfigOk()) {
148148
bounceCurrentIp();
149149
}
150150
restore_error_handler();
151151
} catch (\Exception $e) {
152-
getCrowdSecLoggerInstance()->error(null, [
152+
getCrowdSecLoggerInstance()->error('', [
153153
'type' => 'WP_EXCEPTION_WHILE_BOUNCING',
154154
'messsage' => $e->getMessage(),
155155
'code' => $e->getCode(),

inc/bouncer-instance.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,11 @@ function getBouncerInstance(string $forcedCacheSystem = null): Bouncer
126126
$logger = getCrowdSecLoggerInstance();
127127

128128
// Instanciate the bouncer
129-
$cacheAdapter = getCacheAdapterInstance($forcedCacheSystem);
129+
try {
130+
$cacheAdapter = getCacheAdapterInstance($forcedCacheSystem);
131+
} catch (Symfony\Component\Cache\Exception\InvalidArgumentException $e) {
132+
throw new WordpressCrowdSecBouncerException($e->getMessage());
133+
}
130134
$bouncer = new Bouncer($cacheAdapter, $logger);
131135
$bouncer->configure([
132136
'api_key' => $apiKey,

inc/check-config.php

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?php
2+
3+
function checkCrowdSecConfig(): array
4+
{
5+
$issues = ['errors' => [], 'warnings' => []];
6+
7+
$bouncingLevel = esc_attr(get_option('crowdsec_bouncing_level'));
8+
$shouldBounce = (CROWDSEC_BOUNCING_LEVEL_DISABLED !== $bouncingLevel);
9+
10+
if ($shouldBounce) {
11+
$apiUrl = esc_attr(get_option('crowdsec_api_url'));
12+
if (empty($apiUrl)) {
13+
$issues['errors'][] = [
14+
'type' => 'INCORRECT_API_URL',
15+
'message' => 'Bouncer enabled but no API URL provided',
16+
];
17+
}
18+
19+
$apiKey = esc_attr(get_option('crowdsec_api_key'));
20+
if (empty($apiKey)) {
21+
$issues['errors'][] = [
22+
'type' => 'INCORRECT_API_KEY',
23+
'message' => 'Bouncer enabled but no API key provided',
24+
];
25+
}
26+
27+
try {
28+
getCacheAdapterInstance();
29+
} catch (WordpressCrowdSecBouncerException $e) {
30+
$issues['errors'][] = [
31+
'type' => 'CACHE_CONFIG_ERROR',
32+
'message' => $e->getMessage(),
33+
];
34+
}
35+
}
36+
37+
return $issues;
38+
}
39+
40+
function isBouncerConfigOk(): bool
41+
{
42+
$issues = checkCrowdSecConfig();
43+
44+
return !count($issues['errors']) && !count($issues['warnings']);
45+
}

inc/scheduling.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,13 @@ function crowdSecRefreshBlocklist()
2222
$bouncer = getBouncerInstance();
2323
$bouncer->refreshBlocklistCache();
2424
} catch (WordpressCrowdSecBouncerException $e) {
25+
getCrowdSecLoggerInstance()->error('', [
26+
'type' => 'WP_EXCEPTION_WHILE_REFRESHING_CACHE',
27+
'messsage' => $e->getMessage(),
28+
'code' => $e->getCode(),
29+
'file' => $e->getFile(),
30+
'line' => $e->getLine(),
31+
]);
2532
}
2633
}
2734

0 commit comments

Comments
 (0)