Skip to content

Commit 9ff7701

Browse files
committed
lint pass
1 parent 24751f6 commit 9ff7701

File tree

2 files changed

+68
-69
lines changed

2 files changed

+68
-69
lines changed

src/AbstractBounce.php

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,12 @@ abstract class AbstractBounce
3535
/** @var Bouncer */
3636
protected $bouncer;
3737

38-
protected function getSettings(string $name)
38+
protected function getStringSettings(string $name): string
39+
{
40+
$this->settings[$name];
41+
}
42+
43+
protected function getArraySettings(string $name): array
3944
{
4045
$this->settings[$name];
4146
}
@@ -166,18 +171,13 @@ protected function clearCaptchaSessionContext()
166171

167172
protected function handleCaptchaResolutionForm(string $ip)
168173
{
169-
// Early return if no captcha has to be resolved.
170-
if (null === $this->getSessionVariable('crowdsec_captcha_has_to_be_resolved')) {
171-
return;
172-
}
173-
174-
// Captcha already resolved.
175-
if (false === $this->getSessionVariable('crowdsec_captcha_has_to_be_resolved')) {
174+
// Early return if no captcha has to be resolved or if captcha already resolved.
175+
if (\in_array($this->getSessionVariable('crowdsec_captcha_has_to_be_resolved'), [null, false])) {
176176
return;
177177
}
178178

179179
// Early return if no form captcha form has been filled.
180-
if (('POST' !== $this->getHttpMethod() || null === $this->getPostedVariable('crowdsec_captcha'))) {
180+
if ('POST' !== $this->getHttpMethod() || null === $this->getPostedVariable('crowdsec_captcha')) {
181181
return;
182182
}
183183

@@ -242,6 +242,9 @@ protected function handleRemediation(string $remediation, string $ip)
242242
break;
243243
case Constants::REMEDIATION_BAN:
244244
$this->handleBanRemediation();
245+
break;
246+
default:
247+
return;
245248
}
246249
}
247250
}

src/StandAloneBounce.php

Lines changed: 56 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -45,14 +45,14 @@ private function getCacheAdapterInstance(): AbstractAdapter
4545
return $this->cacheAdapter;
4646
}
4747

48-
$cacheSystem = $this->getSettings('cache_system');
48+
$cacheSystem = $this->getStringSettings('cache_system');
4949
switch ($cacheSystem) {
5050
case Constants::CACHE_SYSTEM_PHPFS:
51-
$this->cacheAdapter = new PhpFilesAdapter('', 0, $this->getSettings('fs_cache_path'));
51+
$this->cacheAdapter = new PhpFilesAdapter('', 0, $this->getStringSettings('fs_cache_path'));
5252
break;
5353

5454
case Constants::CACHE_SYSTEM_MEMCACHED:
55-
$memcachedDsn = $this->getSettings('memcached_dsn');
55+
$memcachedDsn = $this->getStringSettings('memcached_dsn');
5656
if (empty($memcachedDsn)) {
5757
throw new BouncerException('The selected cache technology is Memcached.'.
5858
' Please set a Memcached DSN or select another cache technology.');
@@ -62,7 +62,7 @@ private function getCacheAdapterInstance(): AbstractAdapter
6262
break;
6363

6464
case Constants::CACHE_SYSTEM_REDIS:
65-
$redisDsn = $this->getSettings('redis_dsn');
65+
$redisDsn = $this->getStringSettings('redis_dsn');
6666
if (empty($redisDsn)) {
6767
throw new BouncerException('The selected cache technology is Redis.'.
6868
' Please set a Redis DSN or select another cache technology.');
@@ -95,17 +95,17 @@ public function getBouncerInstance(): Bouncer
9595

9696
// Parse options.
9797

98-
if (empty($this->getSettings('api_url'))) {
98+
if (empty($this->getStringSettings('api_url'))) {
9999
throw new BouncerException('Bouncer enabled but no API URL provided');
100100
}
101-
if (empty($this->getSettings('api_key'))) {
101+
if (empty($this->getStringSettings('api_key'))) {
102102
throw new BouncerException('Bouncer enabled but no API key provided');
103103
}
104-
$isStreamMode = $this->getSettings('stream_mode');
105-
$cleanIpCacheDuration = (int) $this->getSettings('clean_ip_cache_duration');
106-
$badIpCacheDuration = (int) $this->getSettings('bad_ip_cache_duration');
107-
$fallbackRemediation = $this->getSettings('fallback_remediation');
108-
$bouncingLevel = $this->getSettings('bouncing_level');
104+
$isStreamMode = $this->getStringSettings('stream_mode');
105+
$cleanIpCacheDuration = (int) $this->getStringSettings('clean_ip_cache_duration');
106+
$badIpCacheDuration = (int) $this->getStringSettings('bad_ip_cache_duration');
107+
$fallbackRemediation = $this->getStringSettings('fallback_remediation');
108+
$bouncingLevel = $this->getStringSettings('bouncing_level');
109109

110110
// Init Bouncer instance
111111

@@ -125,31 +125,31 @@ public function getBouncerInstance(): Bouncer
125125

126126
// Instanciate the bouncer
127127
try {
128-
$cacheAdapter = $this->getCacheAdapterInstance();
129-
} catch (Symfony\Component\Cache\Exception\InvalidArgumentException $e) {
128+
$this->cacheAdapter = $this->getCacheAdapterInstance();
129+
} catch (InvalidArgumentException $e) {
130130
throw new BouncerException($e->getMessage());
131131
}
132132

133133
$apiUserAgent = 'Standalone CrowdSec PHP Bouncer/'.Constants::VERSION;
134134

135-
$this->bouncer = new Bouncer($cacheAdapter, $this->logger);
135+
$this->bouncer = new Bouncer($this->cacheAdapter, $this->logger);
136136
$this->bouncer->configure([
137-
'api_key' => $this->getSettings('api_key'),
138-
'api_url' => $this->getSettings('api_url'),
137+
'api_key' => $this->getStringSettings('api_key'),
138+
'api_url' => $this->getStringSettings('api_url'),
139139
'api_user_agent' => $apiUserAgent,
140140
'live_mode' => !$isStreamMode,
141141
'max_remediation_level' => $maxRemediationLevel,
142142
'fallback_remediation' => $fallbackRemediation,
143143
'cache_expiration_for_clean_ip' => $cleanIpCacheDuration,
144144
'cache_expiration_for_bad_ip' => $badIpCacheDuration,
145-
], $cacheAdapter);
145+
], $this->cacheAdapter);
146146

147147
return $this->bouncer;
148148
}
149149

150150
public function initLogger(): void
151151
{
152-
$this->initLoggerHelper($this->getSettings('log_directory_path'), 'php_standalone_bouncer');
152+
$this->initLoggerHelper($this->getStringSettings('log_directory_path'), 'php_standalone_bouncer');
153153
}
154154

155155
/**
@@ -187,34 +187,34 @@ public function getHttpMethod(): string
187187
public function getCaptchaWallOptions(): array
188188
{
189189
return [
190-
'hide_crowdsec_mentions' => (bool) $this->getSettings('hide_mentions'),
190+
'hide_crowdsec_mentions' => (bool) $this->getStringSettings('hide_mentions'),
191191
'color' => [
192192
'text' => [
193-
'primary' => htmlspecialchars_decode($this->getSettings('theme_color_text_primary'), \ENT_QUOTES),
194-
'secondary' => htmlspecialchars_decode($this->getSettings('theme_color_text_secondary'), \ENT_QUOTES),
195-
'button' => htmlspecialchars_decode($this->getSettings('theme_color_text_button'), \ENT_QUOTES),
196-
'error_message' => htmlspecialchars_decode($this->getSettings('theme_color_text_error_message'), \ENT_QUOTES),
193+
'primary' => htmlspecialchars_decode($this->getStringSettings('theme_color_text_primary'), \ENT_QUOTES),
194+
'secondary' => htmlspecialchars_decode($this->getStringSettings('theme_color_text_secondary'), \ENT_QUOTES),
195+
'button' => htmlspecialchars_decode($this->getStringSettings('theme_color_text_button'), \ENT_QUOTES),
196+
'error_message' => htmlspecialchars_decode($this->getStringSettings('theme_color_text_error_message'), \ENT_QUOTES),
197197
],
198198
'background' => [
199-
'page' => htmlspecialchars_decode($this->getSettings('theme_color_background_page'), \ENT_QUOTES),
200-
'container' => htmlspecialchars_decode($this->getSettings('theme_color_background_container'), \ENT_QUOTES),
201-
'button' => htmlspecialchars_decode($this->getSettings('theme_color_background_button'), \ENT_QUOTES),
202-
'button_hover' => htmlspecialchars_decode($this->getSettings('theme_color_background_button_hover'), \ENT_QUOTES),
199+
'page' => htmlspecialchars_decode($this->getStringSettings('theme_color_background_page'), \ENT_QUOTES),
200+
'container' => htmlspecialchars_decode($this->getStringSettings('theme_color_background_container'), \ENT_QUOTES),
201+
'button' => htmlspecialchars_decode($this->getStringSettings('theme_color_background_button'), \ENT_QUOTES),
202+
'button_hover' => htmlspecialchars_decode($this->getStringSettings('theme_color_background_button_hover'), \ENT_QUOTES),
203203
],
204204
],
205205
'text' => [
206206
'captcha_wall' => [
207-
'tab_title' => htmlspecialchars_decode($this->getSettings('theme_text_captcha_wall_tab_title'), \ENT_QUOTES),
208-
'title' => htmlspecialchars_decode($this->getSettings('theme_text_captcha_wall_title'), \ENT_QUOTES),
209-
'subtitle' => htmlspecialchars_decode($this->getSettings('theme_text_captcha_wall_subtitle'), \ENT_QUOTES),
210-
'refresh_image_link' => htmlspecialchars_decode($this->getSettings('theme_text_captcha_wall_refresh_image_link'), \ENT_QUOTES),
211-
'captcha_placeholder' => htmlspecialchars_decode($this->getSettings('theme_text_captcha_wall_captcha_placeholder'), \ENT_QUOTES),
212-
'send_button' => htmlspecialchars_decode($this->getSettings('theme_text_captcha_wall_send_button'), \ENT_QUOTES),
213-
'error_message' => htmlspecialchars_decode($this->getSettings('theme_text_captcha_wall_error_message'), \ENT_QUOTES),
214-
'footer' => htmlspecialchars_decode($this->getSettings('theme_text_captcha_wall_footer'), \ENT_QUOTES),
207+
'tab_title' => htmlspecialchars_decode($this->getStringSettings('theme_text_captcha_wall_tab_title'), \ENT_QUOTES),
208+
'title' => htmlspecialchars_decode($this->getStringSettings('theme_text_captcha_wall_title'), \ENT_QUOTES),
209+
'subtitle' => htmlspecialchars_decode($this->getStringSettings('theme_text_captcha_wall_subtitle'), \ENT_QUOTES),
210+
'refresh_image_link' => htmlspecialchars_decode($this->getStringSettings('theme_text_captcha_wall_refresh_image_link'), \ENT_QUOTES),
211+
'captcha_placeholder' => htmlspecialchars_decode($this->getStringSettings('theme_text_captcha_wall_captcha_placeholder'), \ENT_QUOTES),
212+
'send_button' => htmlspecialchars_decode($this->getStringSettings('theme_text_captcha_wall_send_button'), \ENT_QUOTES),
213+
'error_message' => htmlspecialchars_decode($this->getStringSettings('theme_text_captcha_wall_error_message'), \ENT_QUOTES),
214+
'footer' => htmlspecialchars_decode($this->getStringSettings('theme_text_captcha_wall_footer'), \ENT_QUOTES),
215215
],
216216
],
217-
'custom_css' => $this->getSettings('theme_custom_css'),
217+
'custom_css' => $this->getStringSettings('theme_custom_css'),
218218
];
219219
}
220220

@@ -224,29 +224,29 @@ public function getCaptchaWallOptions(): array
224224
public function getBanWallOptions(): array
225225
{
226226
return [
227-
'hide_crowdsec_mentions' => (bool) $this->getSettings('hide_mentions'),
227+
'hide_crowdsec_mentions' => (bool) $this->getStringSettings('hide_mentions'),
228228
'color' => [
229229
'text' => [
230-
'primary' => htmlspecialchars_decode($this->getSettings('theme_color_text_primary'), \ENT_QUOTES),
231-
'secondary' => htmlspecialchars_decode($this->getSettings('theme_color_text_secondary'), \ENT_QUOTES),
232-
'error_message' => htmlspecialchars_decode($this->getSettings('theme_color_text_error_message'), \ENT_QUOTES),
230+
'primary' => htmlspecialchars_decode($this->getStringSettings('theme_color_text_primary'), \ENT_QUOTES),
231+
'secondary' => htmlspecialchars_decode($this->getStringSettings('theme_color_text_secondary'), \ENT_QUOTES),
232+
'error_message' => htmlspecialchars_decode($this->getStringSettings('theme_color_text_error_message'), \ENT_QUOTES),
233233
],
234234
'background' => [
235-
'page' => htmlspecialchars_decode($this->getSettings('theme_color_background_page'), \ENT_QUOTES),
236-
'container' => htmlspecialchars_decode($this->getSettings('theme_color_background_container'), \ENT_QUOTES),
237-
'button' => htmlspecialchars_decode($this->getSettings('theme_color_background_button'), \ENT_QUOTES),
238-
'button_hover' => htmlspecialchars_decode($this->getSettings('theme_color_background_button_hover'), \ENT_QUOTES),
235+
'page' => htmlspecialchars_decode($this->getStringSettings('theme_color_background_page'), \ENT_QUOTES),
236+
'container' => htmlspecialchars_decode($this->getStringSettings('theme_color_background_container'), \ENT_QUOTES),
237+
'button' => htmlspecialchars_decode($this->getStringSettings('theme_color_background_button'), \ENT_QUOTES),
238+
'button_hover' => htmlspecialchars_decode($this->getStringSettings('theme_color_background_button_hover'), \ENT_QUOTES),
239239
],
240240
],
241241
'text' => [
242242
'ban_wall' => [
243-
'tab_title' => htmlspecialchars_decode($this->getSettings('theme_text_ban_wall_tab_title'), \ENT_QUOTES),
244-
'title' => htmlspecialchars_decode($this->getSettings('theme_text_ban_wall_title'), \ENT_QUOTES),
245-
'subtitle' => htmlspecialchars_decode($this->getSettings('theme_text_ban_wall_subtitle'), \ENT_QUOTES),
246-
'footer' => htmlspecialchars_decode($this->getSettings('theme_text_ban_wall_footer'), \ENT_QUOTES),
243+
'tab_title' => htmlspecialchars_decode($this->getStringSettings('theme_text_ban_wall_tab_title'), \ENT_QUOTES),
244+
'title' => htmlspecialchars_decode($this->getStringSettings('theme_text_ban_wall_title'), \ENT_QUOTES),
245+
'subtitle' => htmlspecialchars_decode($this->getStringSettings('theme_text_ban_wall_subtitle'), \ENT_QUOTES),
246+
'footer' => htmlspecialchars_decode($this->getStringSettings('theme_text_ban_wall_footer'), \ENT_QUOTES),
247247
],
248248
],
249-
'custom_css' => htmlspecialchars_decode($this->getSettings('theme_custom_css'), \ENT_QUOTES),
249+
'custom_css' => htmlspecialchars_decode($this->getStringSettings('theme_custom_css'), \ENT_QUOTES),
250250
];
251251
}
252252

@@ -255,7 +255,7 @@ public function getBanWallOptions(): array
255255
*/
256256
public function getTrustForwardedIpBoundsList(): array
257257
{
258-
return $this->getSettings('trust_ip_forward_array');
258+
return $this->getArraySettings('trust_ip_forward_array');
259259
}
260260

261261
/**
@@ -307,16 +307,12 @@ public function getPostedVariable(string $name): ?string
307307
*/
308308
public function shouldBounceCurrentIp(): bool
309309
{
310-
// Don't bounce favicon calls.
311-
if ('/favicon.ico' === $_SERVER['REQUEST_URI']) {
312-
return false;
313-
}
314-
if (!$this->isConfigValid()) {
315-
// We bounce only if plugin config is valid
310+
// Don't bounce favicon calls or when the config is invalid.
311+
if ('/favicon.ico' === $_SERVER['REQUEST_URI'] || !$this->isConfigValid()) {
316312
return false;
317313
}
318314

319-
$bouncingDisabled = (Constants::BOUNCING_LEVEL_DISABLED === $this->getSettings('bouncing_level'));
315+
$bouncingDisabled = (Constants::BOUNCING_LEVEL_DISABLED === $this->getStringSettings('bouncing_level'));
320316
if ($bouncingDisabled) {
321317
return false;
322318
}
@@ -381,19 +377,19 @@ public function isConfigValid(): bool
381377
{
382378
$issues = ['errors' => [], 'warnings' => []];
383379

384-
$bouncingLevel = $this->getSettings('bouncing_level');
380+
$bouncingLevel = $this->getStringSettings('bouncing_level');
385381
$shouldBounce = (Constants::BOUNCING_LEVEL_DISABLED !== $bouncingLevel);
386382

387383
if ($shouldBounce) {
388-
$apiUrl = $this->getSettings('api_url');
384+
$apiUrl = $this->getStringSettings('api_url');
389385
if (empty($apiUrl)) {
390386
$issues['errors'][] = [
391387
'type' => 'INCORRECT_API_URL',
392388
'message' => 'Bouncer enabled but no API URL provided',
393389
];
394390
}
395391

396-
$apiKey = $this->getSettings('api_key');
392+
$apiKey = $this->getStringSettings('api_key');
397393
if (empty($apiKey)) {
398394
$issues['errors'][] = [
399395
'type' => 'INCORRECT_API_KEY',

0 commit comments

Comments
 (0)