[ticket/17413] Add language and theme settings for turnstile

PHPBB-17413
This commit is contained in:
Marc Alexander 2024-10-13 14:50:12 +02:00
parent 8290cdb7e7
commit c382f81222
No known key found for this signature in database
GPG key ID: 50E0D2423696F995
10 changed files with 64 additions and 19 deletions

View file

@ -23,10 +23,24 @@
</dt>
<dd><input id="captcha_turnstile_secret" name="captcha_turnstile_secret" value="{{ CAPTCHA_TURNSTILE_SECRET }}" size="50" type="text" /></dd>
</dl>
<dl>
<dt>
<label>{{ lang('CAPTCHA_TURNSTILE_THEME') ~ lang('COLON') }}</label>
<br><span>{{ lang('CAPTCHA_TURNSTILE_THEME_EXPLAIN') }}</span>
</dt>
<dd>
{% for theme in CAPTCHA_TURNSTILE_THEMES %}
<label>
<input class="radio" name="captcha_turnstile_theme" type="radio" value="{{ theme }}"{{ theme == CAPTCHA_TURNSTILE_THEME ? ' checked' }}>
<span>{{ lang('CAPTCHA_TURNSTILE_THEME_' ~ theme|upper) }}</span>
</label>
{% endfor %}
</dd>
</dl>
</fieldset>
<fieldset>
<legend>{{ lang('PREVIEW') }}</legend>
{% if PREVIEW %}
{% if PREVIEW %}
<div class="successbox">
<h3>{{ lang('WARNING') }}</h3>
<p>{{ lang('CAPTCHA_PREVIEW_MSG') }}</p>

View file

@ -1,5 +1,5 @@
<dl>
<dt><div id="captcha_turnstile"></div></dt>
<dt><div id="captcha_turnstile" data-language="{{ lang('TURNSTILE_LANG') }}"{% if TURNSTILE_THEME %} data-theme="{{ TURNSTILE_THEME }}"{% endif %}></div></dt>
</dl>
{% INCLUDEJS U_TURNSTILE_SCRIPT %}
<script>

View file

@ -77,6 +77,9 @@ INSERT INTO phpbb_config (config_name, config_value) VALUES ('captcha_gd_wave',
INSERT INTO phpbb_config (config_name, config_value) VALUES ('captcha_gd_x_grid', '25');
INSERT INTO phpbb_config (config_name, config_value) VALUES ('captcha_gd_y_grid', '25');
INSERT INTO phpbb_config (config_name, config_value) VALUES ('captcha_plugin', 'core.captcha.plugins.incomplete');
INSERT INTO phpbb_config (config_name, config_value) VALUES ('captcha_turnstile_sitekey', '');
INSERT INTO phpbb_config (config_name, config_value) VALUES ('captcha_turnstile_secret', '');
INSERT INTO phpbb_config (config_name, config_value) VALUES ('captcha_turnstile_theme', 'light');
INSERT INTO phpbb_config (config_name, config_value) VALUES ('check_attachment_content', '1');
INSERT INTO phpbb_config (config_name, config_value) VALUES ('check_dnsbl', '0');
INSERT INTO phpbb_config (config_name, config_value) VALUES ('chg_passforce', '0');

View file

@ -40,9 +40,14 @@ $lang = array_merge($lang, [
'CAPTCHA_TURNSTILE' => 'Turnstile',
'CAPTCHA_TURNSTILE_INCORRECT' => 'The solution you provided was incorrect',
'CAPTCHA_TURNSTILE_NOSCRIPT' => 'Please enable JavaScript in your browser to load the challenge.',
'CAPTCHA_TURNSTILE_NOT_AVAILABLE' => 'In order to use Turnstile you must create a <a href="https://www.cloudflare.com/products/turnstile/">Cloudflare account</a>.',
'CAPTCHA_TURNSTILE_SECRET' => 'Secret key',
'CAPTCHA_TURNSTILE_SECRET_EXPLAIN' => 'Your Turnstile secret key. The secret key can be retrieved from your <a href="https://dash.cloudflare.com/?to=/:account/turnstile">Cloudflare dashboard</a>.',
'CAPTCHA_TURNSTILE_SITEKEY' => 'Sitekey',
'CAPTCHA_TURNSTILE_SITEKEY_EXPLAIN' => 'Your Turnstile sitekey. The sitekey can be retrieved from your <a href="https://dash.cloudflare.com/?to=/:account/turnstile">Cloudflare dashboard</a>.',
'CAPTCHA_TURNSTILE_NOT_AVAILABLE' => 'In order to use Turnstile you must create a <a href="https://www.cloudflare.com/products/turnstile/">Cloudflare account</a>.',
'CAPTCHA_TURNSTILE_THEME' => 'Widget theme',
'CAPTCHA_TURNSTILE_THEME_EXPLAIN' => 'The theme of the CAPTCHA widget. By default, <samp>light</samp> will be used. Other possibilities are <samp>dark</samp> and <samp>auto</samp>, which respects the users preference.',
'CAPTCHA_TURNSTILE_THEME_AUTO' => 'Auto',
'CAPTCHA_TURNSTILE_THEME_DARK' => 'Dark',
'CAPTCHA_TURNSTILE_THEME_LIGHT' => 'Light',
]);

View file

@ -26,6 +26,7 @@
"direction": "ltr",
"user-lang": "en-gb",
"plural-rule": 1,
"recaptcha-lang": "en-GB"
"recaptcha-lang": "en-GB",
"turnstile-lang": "en"
}
}

View file

@ -53,6 +53,13 @@ class turnstile extends base
/** @var string Service name */
protected string $service_name = '';
/** @var array|string[] Supported themes for Turnstile CAPTCHA */
protected static array $supported_themes = [
'light',
'dark',
'auto'
];
/**
* Constructor for turnstile captcha plugin
*
@ -205,6 +212,7 @@ class turnstile extends base
$this->template->assign_vars([
'S_TURNSTILE_AVAILABLE' => $this->is_available(),
'TURNSTILE_SITEKEY' => $this->config->offsetGet('captcha_turnstile_sitekey'),
'TURNSTILE_THEME' => $this->config->offsetGet('captcha_turnstile_theme'),
'U_TURNSTILE_SCRIPT' => self::SCRIPT_URL,
]);
@ -214,6 +222,7 @@ class turnstile extends base
public function get_demo_template(): string
{
$this->template->assign_vars([
'TURNSTILE_THEME' => $this->config->offsetGet('captcha_turnstile_theme'),
'U_TURNSTILE_SCRIPT' => self::SCRIPT_URL,
]);
@ -254,6 +263,12 @@ class turnstile extends base
}
}
$captcha_theme = $this->request->variable('captcha_turnstile_theme', self::$supported_themes[0]);
if (in_array($captcha_theme, self::$supported_themes))
{
$this->config->set('captcha_turnstile_theme', $captcha_theme);
}
$this->log->add('admin', $this->user->data['user_id'], $this->user->ip, 'LOG_CONFIG_VISUAL');
trigger_error($this->language->lang('CONFIG_UPDATED') . adm_back_link($module->u_action));
}
@ -270,9 +285,11 @@ class turnstile extends base
}
$this->template->assign_vars(array(
'CAPTCHA_PREVIEW' => $this->get_demo_template(),
'CAPTCHA_NAME' => $this->service_name,
'U_ACTION' => $module->u_action,
'CAPTCHA_PREVIEW' => $this->get_demo_template(),
'CAPTCHA_NAME' => $this->service_name,
'CAPTCHA_TURNSTILE_THEME' => $this->config->offsetGet('captcha_turnstile_theme'),
'CAPTCHA_TURNSTILE_THEMES' => self::$supported_themes,
'U_ACTION' => $module->u_action,
));
}
}

View file

@ -20,7 +20,8 @@ class turnstile_captcha extends migration
public function effectively_installed(): bool
{
return $this->config->offsetExists('captcha_turnstile_sitekey')
&& $this->config->offsetExists('captcha_turnstile_secret');
&& $this->config->offsetExists('captcha_turnstile_secret')
&& $this->config->offsetExists('captcha_turnstile_theme');
}
public static function depends_on(): array
@ -35,6 +36,7 @@ class turnstile_captcha extends migration
return [
['config.add', ['captcha_turnstile_sitekey', '']],
['config.add', ['captcha_turnstile_secret', '']],
['config.add', ['captcha_turnstile_theme', 'light']],
];
}
@ -43,6 +45,7 @@ class turnstile_captcha extends migration
return [
['config.remove', ['captcha_turnstile_sitekey']],
['config.remove', ['captcha_turnstile_secret']],
['config.remove', ['captcha_turnstile_theme']],
];
}
}

View file

@ -403,6 +403,7 @@ class language
$this->lang['USER_LANG'] = $lang_values['user_lang'] ?? 'en-gb';
$this->lang['PLURAL_RULE'] = $lang_values['plural_rule'] ?? 1;
$this->lang['RECAPTCHA_LANG'] = $lang_values['recaptcha_lang'] ?? 'en-GB';
$this->lang['TURNSTILE_LANG'] = $lang_values['turnstile_lang'] ?? 'auto'; // default to auto mode
}
/**

View file

@ -110,16 +110,17 @@ class language_file_helper
}
return [
'iso' => $data['extra']['language-iso'],
'name' => $data['extra']['english-name'],
'local_name' => $data['extra']['local-name'],
'author' => implode(', ', $authors),
'version' => $data['version'],
'phpbb_version' => $data['extra']['phpbb-version'],
'direction' => $data['extra']['direction'],
'user_lang' => $data['extra']['user-lang'],
'plural_rule' => $data['extra']['plural-rule'],
'recaptcha_lang'=> $data['extra']['recaptcha-lang'],
'iso' => $data['extra']['language-iso'],
'name' => $data['extra']['english-name'],
'local_name' => $data['extra']['local-name'],
'author' => implode(', ', $authors),
'version' => $data['version'],
'phpbb_version' => $data['extra']['phpbb-version'],
'direction' => $data['extra']['direction'],
'user_lang' => $data['extra']['user-lang'],
'plural_rule' => $data['extra']['plural-rule'],
'recaptcha_lang' => $data['extra']['recaptcha-lang'],
'turnstile_lang' => $data['extra']['turnstile-lang'] ?? '',
];
}
}

View file

@ -6,7 +6,7 @@
<script src="{{ U_TURNSTILE_SCRIPT }}" async defer></script>
{# The cf-turnstile class is used in JavaScript #}
<div class="cf-turnstile" data-sitekey="{{ TURNSTILE_SITEKEY }}"></div>
<div class="cf-turnstile" data-sitekey="{{ TURNSTILE_SITEKEY }}"{% if TURNSTILE_THEME %} data-theme="{{ TURNSTILE_THEME }}"{% endif %}></div>
{% else %}
{{ lang('CAPTCHA_TURNSTILE_NOT_AVAILABLE') }}
{% endif %}