Skip to content

Commit 15df1ab

Browse files
committed
lint pass
1 parent 90431b4 commit 15df1ab

14 files changed

+193
-185
lines changed

crowdsec.php

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,22 +14,21 @@
1414
Text Domain: crowdsec-wp
1515
*/
1616

17-
1817
session_start();
19-
require_once __DIR__ . '/vendor/autoload.php';
18+
require_once __DIR__.'/vendor/autoload.php';
2019

2120
class WordpressCrowdSecBouncerException extends \RuntimeException
2221
{
2322
}
2423

25-
require_once __DIR__ . '/inc/constants.php';
26-
require_once __DIR__ . '/inc/scheduling.php';
27-
require_once __DIR__ . '/inc/plugin-setup.php';
24+
require_once __DIR__.'/inc/constants.php';
25+
require_once __DIR__.'/inc/scheduling.php';
26+
require_once __DIR__.'/inc/plugin-setup.php';
2827
register_activation_hook(__FILE__, 'activate_crowdsec_plugin');
2928
register_deactivation_hook(__FILE__, 'deactivate_crowdsec_plugin');
30-
require_once __DIR__ . '/inc/bouncer-instance.php';
31-
require_once __DIR__ . '/inc/admin/init.php';
32-
require_once __DIR__ . '/inc/bounce-current-ip.php';
29+
require_once __DIR__.'/inc/bouncer-instance.php';
30+
require_once __DIR__.'/inc/admin/init.php';
31+
require_once __DIR__.'/inc/bounce-current-ip.php';
3332

3433
// Apply bouncing
35-
add_action('plugins_loaded', "safelyBounceCurrentIp");
34+
add_action('plugins_loaded', 'safelyBounceCurrentIp');

inc/admin/advanced-settings.php

Lines changed: 30 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -20,24 +20,26 @@ function adminAdvancedSettings()
2020
unscheduleBlocklistRefresh();
2121
}, '
2222
<p>With the stream mode, every decision is retrieved in an asynchronous way. 3 advantages: <br>&nbsp;1) Inivisible latency when loading pages<br>&nbsp;2) The IP verifications works even if your CrowdSec is not reachable.<br>&nbsp;3) The API can never be overloaded by the WordPress traffic</p>
23-
<p>Note: This method has one limit: for maximum 60 seconds, all the new decisions may not be taken into account.</p>' .
24-
(get_option("crowdsec_stream_mode") ?
23+
<p>Note: This method has one limit: for maximum 60 seconds, all the new decisions may not be taken into account.</p>'.
24+
(get_option('crowdsec_stream_mode') ?
2525
'<p><input style="margin-right:10px" type="button" value="Refresh the cache now" class="button button-secondary button-small" onclick="document.getElementById(\'crowdsec_ation_refresh_cache\').submit();"></p>' :
2626
'<p><input style="margin-right:10px" type="button" disabled="disabled" value="Refresh the cache now" class="button button-secondary button-small"></p>'));
2727

2828
// Field "crowdsec_stream_mode_refresh_frequency"
2929
addFieldString('crowdsec_stream_mode_refresh_frequency', 'Resync decisions each<br>(stream mode only)', 'crowdsec_plugin_advanced_settings', 'crowdsec_advanced_settings', 'crowdsec_admin_advanced_stream_mode', function ($input) {
30-
$input = (int)$input;
30+
$input = (int) $input;
3131
if ($input < 60) {
3232
$input = 60;
33-
add_settings_error("Resync decisions each", "crowdsec_error", 'The "Resync decisions each" value should be more than 60sec (WP_CRON_LOCK_TIMEOUT). We just reset the frequency to 60 seconds.');
33+
add_settings_error('Resync decisions each', 'crowdsec_error', 'The "Resync decisions each" value should be more than 60sec (WP_CRON_LOCK_TIMEOUT). We just reset the frequency to 60 seconds.');
34+
3435
return $input;
3536
}
3637

3738
// Update wp-cron schedule.
38-
if ((bool)get_option("crowdsec_stream_mode")) {
39+
if ((bool) get_option('crowdsec_stream_mode')) {
3940
scheduleBlocklistRefresh();
4041
}
42+
4143
return $input;
4244
}, ' seconds. <p>Our advice is 60 seconds (according to WP_CRON_LOCK_TIMEOUT).</p>', '...', 'width: 115px;', 'number');
4345

@@ -46,7 +48,7 @@ function adminAdvancedSettings()
4648
********************/
4749

4850
add_settings_section('crowdsec_admin_advanced_cache', 'Caching configuration <input style="margin-left: 7px;margin-top: -3px;" type="button" value="Clear now" class="button button-secondary button-small" onclick="if (confirm(\'Are you sure you want to completely clear the cache?\')) document.getElementById(\'crowdsec_ation_clear_cache\').submit();">', function () {
49-
?>
51+
?>
5052
<p>Polish the decisions cache settings by selecting the best technology or the cache durations best suited to your use.</p>
5153
<?php
5254
}, 'crowdsec_advanced_settings');
@@ -55,27 +57,26 @@ function adminAdvancedSettings()
5557
addFieldSelect('crowdsec_cache_system', 'Technology', 'crowdsec_plugin_advanced_settings', 'crowdsec_advanced_settings', 'crowdsec_admin_advanced_cache', function ($input) {
5658
if (!in_array($input, [CROWDSEC_CACHE_SYSTEM_PHPFS, CROWDSEC_CACHE_SYSTEM_REDIS, CROWDSEC_CACHE_SYSTEM_MEMCACHED])) {
5759
$input = CROWDSEC_CACHE_SYSTEM_PHPFS;
58-
add_settings_error("Technology", "crowdsec_error", "Technology: Incorrect cache technology selected.");
60+
add_settings_error('Technology', 'crowdsec_error', 'Technology: Incorrect cache technology selected.');
5961
}
6062

6163
$bouncer = getBouncerInstance();
6264
$bouncer->clearCache();
6365
$message = __('Cache system changed. Previous cache data has been cleared.');
6466

6567
// Update wp-cron schedule if stream mode is enabled
66-
if ((bool)get_option("crowdsec_stream_mode")) {
68+
if ((bool) get_option('crowdsec_stream_mode')) {
6769
$bouncer = getBouncerInstance($input); // Reload bouncer instance with the new cache system
6870
$result = $bouncer->warmBlocklistCacheUp();
69-
$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.');
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.');
7072
scheduleBlocklistRefresh();
7173
}
7274

7375
AdminNotice::displaySuccess($message);
7476

75-
7677
return $input;
77-
}, ((get_option("crowdsec_cache_system") === CROWDSEC_CACHE_SYSTEM_PHPFS) ?
78-
'<input style="margin-right:10px" type="button" value="Prune now" class="button button-secondary" onclick="document.getElementById(\'crowdsec_ation_prune_cache\').submit();">' : '') .
78+
}, ((CROWDSEC_CACHE_SYSTEM_PHPFS === get_option('crowdsec_cache_system')) ?
79+
'<input style="margin-right:10px" type="button" value="Prune now" class="button button-secondary" onclick="document.getElementById(\'crowdsec_ation_prune_cache\').submit();">' : '').
7980
'<p>The File system cache is faster than calling LAPI. Redis or Memcached is faster than the File System cache.</p>', [
8081
CROWDSEC_CACHE_SYSTEM_PHPFS => 'File system',
8182
CROWDSEC_CACHE_SYSTEM_REDIS => 'Redis',
@@ -84,29 +85,37 @@ function adminAdvancedSettings()
8485

8586
// Field "crowdsec_redis_dsn"
8687
addFieldString('crowdsec_redis_dsn', 'Redis DSN<br>(if applicable)', 'crowdsec_plugin_advanced_settings', 'crowdsec_advanced_settings', 'crowdsec_admin_advanced_cache', function ($input) {
88+
// TODO P1 block remove if cache set to redis
89+
// TODO P1 Display an error in all sections (settings and advanced) when a cache config can not work.
8790
return $input;
8891
}, '<p>Fill in this field only if you have chosen the Redis cache.<br>Example of DSN: redis://localhost:6379.', 'redis://...', '');
8992

9093
// Field "crowdsec_memcached_dsn"
9194
addFieldString('crowdsec_memcached_dsn', 'Memcached DSN<br>(if applicable)', 'crowdsec_plugin_advanced_settings', 'crowdsec_advanced_settings', 'crowdsec_admin_advanced_cache', function ($input) {
95+
// TODO P1 block remove if cache set to memcached
96+
// TODO P1 Display an error in all sections (settings and advanced) when a cache config can not work.
9297
return $input;
9398
}, '<p>Fill in this field only if you have chosen the Memcached cache.<br>Example of DSN: memcached://localhost:11211.', 'memcached://...', '');
9499

95100
// Field "crowdsec_clean_ip_cache_duration"
96101
addFieldString('crowdsec_clean_ip_cache_duration', 'Recheck clean IPs each', 'crowdsec_plugin_advanced_settings', 'crowdsec_advanced_settings', 'crowdsec_admin_advanced_cache', function ($input) {
97-
if ((int)$input <= 0) {
98-
add_settings_error("Recheck clean IPs each", "crowdsec_error", "Recheck clean IPs each: Minimum is 1 second.");
99-
return "1";
102+
if ((int) $input <= 0) {
103+
add_settings_error('Recheck clean IPs each', 'crowdsec_error', 'Recheck clean IPs each: Minimum is 1 second.');
104+
105+
return '1';
100106
}
107+
101108
return $input;
102109
}, ' seconds. <p>The duration (in seconds) between re-asking LAPI about an already checked clean IP.<br>Minimum 1 second.', '...', 'width: 115px;', 'number');
103110

104111
// Field "crowdsec_bad_ip_cache_duration"
105112
addFieldString('crowdsec_bad_ip_cache_duration', 'Recheck bad IPs each', 'crowdsec_plugin_advanced_settings', 'crowdsec_advanced_settings', 'crowdsec_admin_advanced_cache', function ($input) {
106-
if ((int)$input <= 0) {
107-
add_settings_error("Recheck bad IPs each", "crowdsec_error", "Recheck bad IPs each: Minimum is 1 second.");
108-
return "1";
113+
if ((int) $input <= 0) {
114+
add_settings_error('Recheck bad IPs each', 'crowdsec_error', 'Recheck bad IPs each: Minimum is 1 second.');
115+
116+
return '1';
109117
}
118+
110119
return $input;
111120
}, ' seconds. <p>The duration (in seconds) between re-asking LAPI about an already checked bad IP.<br>Minimum 1 second.', '...', 'width: 115px;', 'number');
112121

@@ -115,7 +124,7 @@ function adminAdvancedSettings()
115124
**************************/
116125

117126
add_settings_section('crowdsec_admin_advanced_remediations', 'Remediations', function () {
118-
echo "Configuration some details about remediations.";
127+
echo 'Configuration some details about remediations.';
119128
}, 'crowdsec_advanced_settings');
120129

121130
// Field "crowdsec_fallback_remediation"
@@ -126,8 +135,9 @@ function adminAdvancedSettings()
126135
addFieldSelect('crowdsec_fallback_remediation', 'Fallback to', 'crowdsec_plugin_advanced_settings', 'crowdsec_advanced_settings', 'crowdsec_admin_advanced_remediations', function ($input) {
127136
if (!in_array($input, Constants::ORDERED_REMEDIATIONS)) {
128137
$input = CROWDSEC_BOUNCING_LEVEL_DISABLED;
129-
add_settings_error("Fallback to", "crowdsec_error", "Fallback to: Incorrect Fallback selected.");
138+
add_settings_error('Fallback to', 'crowdsec_error', 'Fallback to: Incorrect Fallback selected.');
130139
}
140+
131141
return $input;
132142
}, '<p>Which remediation to apply when CrowdSec advises unhandled remediation.</p>', $choice);
133143

0 commit comments

Comments
 (0)