Skip to content

Commit 9ddec3f

Browse files
committed
fix paths bug
1 parent 15df1ab commit 9ddec3f

File tree

4 files changed

+32
-10
lines changed

4 files changed

+32
-10
lines changed

crowdsec.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@
1717
session_start();
1818
require_once __DIR__.'/vendor/autoload.php';
1919

20+
define('CROWDSEC_PLUGIN_PATH', __DIR__);
21+
define('CROWDSEC_PLUGIN_URL', plugin_dir_url(__FILE__));
22+
2023
class WordpressCrowdSecBouncerException extends \RuntimeException
2124
{
2225
}

inc/admin/init.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -225,10 +225,10 @@ function addFieldSelect(string $optionName, string $label, string $optionGroup,
225225
});
226226
*/
227227
add_menu_page('CrowdSec Plugin', 'CrowdSec', 'manage_options', 'crowdsec_plugin', function () {
228-
require_once CROWDSEC_PLUGIN_PATH.'/templates/settings.php';
228+
require_once CROWDSEC_PLUGIN_PATH.'/inc/templates/settings.php';
229229
}, 'dashicons-shield', 110);
230230
add_submenu_page('crowdsec_plugin', 'Advanced', 'Advanced', 'manage_options', 'crowdsec_advanced_settings', function () {
231-
require_once CROWDSEC_PLUGIN_PATH.'/templates/advanced-settings.php';
231+
require_once CROWDSEC_PLUGIN_PATH.'/inc/templates/advanced-settings.php';
232232
});
233233

234234
add_action('admin_init', function () {

inc/bouncer-instance.php

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ function getCrowdSecLoggerInstance(): Logger
2525

2626
$loggerLevel = WP_DEBUG ? Logger::DEBUG : Logger::INFO;
2727
$logger = new Logger('wp_bouncer');
28-
$fileHandler = new RotatingFileHandler(__DIR__.'/../logs/crowdsec.log', 0, $loggerLevel);
28+
$fileHandler = new RotatingFileHandler(CROWDSEC_LOG_PATH, 0, $loggerLevel);
2929

3030
// Set custom readble logger for WP_DEBUG=1
3131
if (WP_DEBUG) {
@@ -36,29 +36,45 @@ function getCrowdSecLoggerInstance(): Logger
3636
return $logger;
3737
}
3838

39+
/** @var AbstractAdapter|null */
40+
$crowdSecCacheAdapterInstance = null;
41+
3942
function getCacheAdapterInstance(string $forcedCacheSystem = null): AbstractAdapter
4043
{
44+
// Singleton for this function
45+
46+
global $crowdSecCacheAdapterInstance;
47+
if (!$forcedCacheSystem && $crowdSecCacheAdapterInstance) {
48+
return $crowdSecCacheAdapterInstance;
49+
}
50+
4151
$cacheSystem = $forcedCacheSystem ?: esc_attr(get_option('crowdsec_cache_system'));
4252
switch ($cacheSystem) {
4353
case CROWDSEC_CACHE_SYSTEM_PHPFS:
44-
return new PhpFilesAdapter('', 0, __DIR__.'/.cache');
54+
$crowdSecCacheAdapterInstance = new PhpFilesAdapter('', 0, CROWDSEC_CACHE_PATH);
55+
break;
4556

4657
case CROWDSEC_CACHE_SYSTEM_MEMCACHED:
4758
$memcachedDsn = esc_attr(get_option('crowdsec_memcached_dsn'));
4859
if (empty($memcachedDsn)) {
49-
throw new WordpressCrowdSecBouncerException('Memcached selected but no DSN provided.');
60+
throw new WordpressCrowdSecBouncerException('The selected cache technology is Memcached.'.
61+
' Please set a Memcached DSN or select another cache technology.');
5062
}
5163

52-
return new MemcachedAdapter(MemcachedAdapter::createConnection($memcachedDsn));
64+
$crowdSecCacheAdapterInstance = new MemcachedAdapter(MemcachedAdapter::createConnection($memcachedDsn));
65+
break;
5366

5467
case CROWDSEC_CACHE_SYSTEM_REDIS:
5568
$redisDsn = esc_attr(get_option('crowdsec_redis_dsn'));
5669
if (empty($redisDsn)) {
57-
throw new WordpressCrowdSecBouncerException('Redis selected but no DSN provided.');
70+
throw new WordpressCrowdSecBouncerException('The selected cache technology is Redis.'.
71+
' Please set a Redis DSN or select another cache technology.');
5872
}
5973

60-
return new RedisAdapter(RedisAdapter::createConnection($redisDsn));
74+
$crowdSecCacheAdapterInstance = new RedisAdapter(RedisAdapter::createConnection($redisDsn));
75+
break;
6176
}
77+
return $crowdSecCacheAdapterInstance;
6278
}
6379

6480
$crowdSecBouncer = null;
@@ -91,6 +107,9 @@ function getBouncerInstance(string $forcedCacheSystem = null): Bouncer
91107
// Init Bouncer instance
92108

93109
switch ($bouncingLevel) {
110+
case CROWDSEC_BOUNCING_LEVEL_DISABLED:
111+
$maxRemediationLevel = Constants::REMEDIATION_BYPASS;
112+
break;
94113
case CROWDSEC_BOUNCING_LEVEL_FLEX:
95114
$maxRemediationLevel = Constants::REMEDIATION_CAPTCHA;
96115
break;

inc/constants.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?php
22

3-
define('CROWDSEC_PLUGIN_PATH', __DIR__);
4-
define('CROWDSEC_PLUGIN_URL', plugin_dir_url(__FILE__));
3+
define('CROWDSEC_LOG_PATH', CROWDSEC_PLUGIN_PATH.'/logs/crowdsec.log');
4+
define('CROWDSEC_CACHE_PATH', CROWDSEC_PLUGIN_PATH.'/.cache');
55

66
define('CROWDSEC_BOUNCING_LEVEL_DISABLED', 'bouncing_disabled');
77
define('CROWDSEC_BOUNCING_LEVEL_FLEX', 'flex_boucing');

0 commit comments

Comments
 (0)