11<?php
22
3+ require_once __DIR__ .'/constants.php ' ;
4+
35use CrowdSecBouncer \AbstractBounce ;
46use CrowdSecBouncer \Bouncer ;
57use CrowdSecBouncer \Constants ;
@@ -20,28 +22,26 @@ class Bounce extends AbstractBounce implements IBounce
2022 public function init (array $ crowdSecConfig ): bool
2123 {
2224 $ this ->settings = $ crowdSecConfig ;
25+ $ crowdsecRandomLogFolder = $ this ->settings ['crowdsec_random_log_folder ' ];
26+ crowdsecDefineConstants ($ crowdsecRandomLogFolder );
2327 $ this ->initLogger ();
2428
2529 return true ;
2630 }
2731
2832 protected function getSettings (string $ name )
2933 {
30- if (!array_key_exists ($ name , $ this ->settings )) {
31- $ this ->settings [$ name ] = get_option ($ name );
32- }
33-
3434 return $ this ->settings [$ name ];
3535 }
3636
3737 protected function escape (string $ value )
3838 {
39- return esc_attr ($ value );
39+ return htmlspecialchars ($ value, \ ENT_QUOTES , ' UTF-8 ' );
4040 }
4141
4242 protected function specialcharsDecodeEntQuotes (string $ value )
4343 {
44- return wp_specialchars_decode ($ value , \ENT_QUOTES );
44+ return htmlspecialchars_decode ($ value , \ENT_QUOTES );
4545 }
4646
4747 /**
@@ -59,10 +59,15 @@ public function getBouncerInstance(): Bouncer
5959 $ crowdSecBouncerUserAgent = CROWDSEC_BOUNCER_USER_AGENT ;
6060 $ crowdSecLogPath = CROWDSEC_LOG_PATH ;
6161 $ crowdSecDebugLogPath = CROWDSEC_DEBUG_LOG_PATH ;
62- $ debugMode = (bool ) WP_DEBUG ;
6362
64- $ this ->logger = getStandAloneCrowdSecLoggerInstance ($ crowdSecLogPath , $ debugMode , $ crowdSecDebugLogPath );
65- $ this ->bouncer = getBouncerInstanceStandAlone ($ apiUrl , $ apiKey , $ isStreamMode , $ cleanIpCacheDuration , $ badIpCacheDuration , $ fallbackRemediation , $ bouncingLevel , $ crowdSecBouncerUserAgent , $ this ->logger );
63+ $ this ->logger = getStandAloneCrowdSecLoggerInstance ($ crowdSecLogPath , $ this ->debug , $ crowdSecDebugLogPath );
64+
65+ $ cacheSystem = $ this ->escape ($ this ->getSettings ('crowdsec_cache_system ' ));
66+ $ memcachedDsn = $ this ->escape ($ this ->getSettings ('crowdsec_memcached_dsn ' ));
67+ $ redisDsn = $ this ->escape ($ this ->getSettings ('crowdsec_redis_dsn ' ));
68+ $ fsCachePath = CROWDSEC_CACHE_PATH ;
69+
70+ $ this ->bouncer = getBouncerInstanceStandAlone ($ apiUrl , $ apiKey , $ isStreamMode , $ cleanIpCacheDuration , $ badIpCacheDuration , $ fallbackRemediation , $ bouncingLevel , $ crowdSecBouncerUserAgent , $ this ->logger , $ cacheSystem , $ memcachedDsn , $ redisDsn , $ fsCachePath );
6671
6772 return $ this ->bouncer ;
6873 }
@@ -227,16 +232,37 @@ public function shouldBounceCurrentIp(): bool
227232 return false ;
228233 }
229234
235+ // Don't bounce if standalone mode is enable and we are not in a auto_prepend_file context.
236+ if ((bool ) $ this ->getSettings ('crowdsec_standalone_mode ' ) && !defined ('CROWDSEC_STANDALONE_RUNNING_CONTEXT ' )) {
237+ return false ;
238+ }
239+
230240 $ shouldNotBounceWpAdmin = !empty ($ this ->getSettings ('crowdsec_public_website_only ' ));
231241 // when the "crowdsec_public_website_only" is disabled...
232242 if ($ shouldNotBounceWpAdmin ) {
233- // ...don't bounce back office pages
234- if (is_admin ()) {
235- return false ;
236- }
237- // ...don't bounce wp-login and wp-cron pages
238- if (in_array ($ GLOBALS ['pagenow ' ], ['wp-login.php ' , 'wp-cron.php ' ])) {
239- return false ;
243+ // In standalone context, is_admin() does not work. So we check admin section with another method.
244+ if (defined ('CROWDSEC_STANDALONE_RUNNING_CONTEXT ' )) {
245+ // TODO improve the way to detect these pages or add a warning near to the wp option "enable standalone mode"
246+ // ...don't bounce back office pages
247+ if (0 === strpos ($ _SERVER ['PHP_SELF ' ], '/wp-admin ' )) {
248+ return false ;
249+ }
250+ // ...don't bounce wp-login and wp-cron pages
251+ if (0 === strpos ($ _SERVER ['PHP_SELF ' ], '/wp-login.php ' )) {
252+ return false ;
253+ }
254+ if (0 === strpos ($ _SERVER ['PHP_SELF ' ], '/wp-cron.php ' )) {
255+ return false ;
256+ }
257+ } else {
258+ // ...don't bounce back office pages
259+ if (is_admin ()) {
260+ return false ;
261+ }
262+ // ...don't bounce wp-login and wp-cron pages
263+ if (in_array ($ GLOBALS ['pagenow ' ], ['wp-login.php ' , 'wp-cron.php ' ])) {
264+ return false ;
265+ }
240266 }
241267 }
242268
@@ -343,9 +369,8 @@ public function isConfigValid(): bool
343369
344370 public function initLogger (): void
345371 {
346- $ debugMode = (bool ) WP_DEBUG ;
347372 $ crowdSecLogPath = CROWDSEC_LOG_PATH ;
348373 $ crowdSecDebugLogPath = CROWDSEC_DEBUG_LOG_PATH ;
349- $ this ->logger = getStandAloneCrowdSecLoggerInstance ($ crowdSecLogPath , $ debugMode , $ crowdSecDebugLogPath );
374+ $ this ->logger = getStandAloneCrowdSecLoggerInstance ($ crowdSecLogPath , $ this -> debug , $ crowdSecDebugLogPath );
350375 }
351376}
0 commit comments