1
0
mirror of https://github.com/vichan-devel/vichan.git synced 2024-11-28 01:10:51 +01:00

config.php: rework captcha configuration

This commit is contained in:
Zankaria 2024-08-15 16:11:28 +02:00
parent a275d04efa
commit fb92e5fb68
6 changed files with 72 additions and 69 deletions

View File

@ -351,48 +351,39 @@
//); //);
$config['simple_spam'] = false; $config['simple_spam'] = false;
$config['captcha'] = [
// Can be false, 'recaptcha', 'hcaptcha' or 'secureimage'.
'provider' => false,
/* /*
* If not flase, the captcha is dynamically injected on the client if the web server set the `captcha-required` * If not false, the captcha is dynamically injected on the client if the web server set the `captcha-required`
* cookie to 1. The configuration value should be set the IP for which the captcha should be verified. * cookie to 1. The configuration value should be set the IP for which the captcha should be verified.
* *
* Example: * Example:
* $config['dynamic_captcha'] = '127.0.0.1'; // Verify the captcha for users sending posts from the loopback address. *
* // Verify the captcha for users sending posts from the loopback address.
* $config['captcha']['dynamic'] = '127.0.0.1';
*/ */
$config['dynamic_captcha'] = false; 'dynamic' => false,
'recaptcha' => [
// Enable reCaptcha to make spam even harder. Rarely necessary. 'sitekey' => '6LeIxAcTAAAAAJcZVRqyHh71UMIEGNQ_MXjiZKhI',
$config['recaptcha'] = false; 'secret' => '6LeIxAcTAAAAAGG-vFI1TnRWxMZNFuojJ4WifJWe',
],
// Public and private key pair from https://www.google.com/recaptcha/admin/create 'hcaptcha' => [
$config['recaptcha_public'] = '6LcXTcUSAAAAAKBxyFWIt2SO8jwx4W7wcSMRoN3f'; 'sitekey' => '10000000-ffff-ffff-ffff-000000000001',
$config['recaptcha_private'] = '6LcXTcUSAAAAAOGVbVdhmEM1_SyRF4xTKe8jbzf_'; 'secret' => '0x0000000000000000000000000000000000000000',
],
// Enable hCaptcha as an alternative to reCAPTCHA. // Enable the secureimage captcha you need to change a couple of settings. Read more at: /inc/captcha/readme.md
$config['hcaptcha'] = false; 'secureimage' => [
// Custom captcha get provider path (if not working get the absolute path aka your url).
// Public and private key pair for using hCaptcha. 'provider_get' => '../inc/captcha/entrypoint.php',
$config['hcaptcha_public'] = '7a4b21e0-dc53-46f2-a9f8-91d2e74b63a0';
$config['hcaptcha_private'] = '0x4e9A01bE637b51dC41a7Ea9865C3fDe4aB72Cf17';
// Enable Custom Captcha you need to change a couple of settings
//Read more at: /inc/captcha/readme.md
$config['captcha'] = array();
// Enable custom captcha provider
$config['captcha']['enabled'] = false;
//New thread captcha
//Require solving a captcha to post a thread.
//Default off.
$config['new_thread_capt'] = false;
// Custom captcha get provider path (if not working get the absolute path aka your url.)
$config['captcha']['provider_get'] = '../inc/captcha/entrypoint.php';
// Custom captcha check provider path // Custom captcha check provider path
$config['captcha']['provider_check'] = '../inc/captcha/entrypoint.php'; 'provider_check' => '../inc/captcha/entrypoint.php',
// Custom captcha extra field (eg. charset) // Custom captcha extra field (eg. charset)
$config['captcha']['extra'] = 'abcdefghijklmnopqrstuvwxyz'; 'extra' => 'abcdefghijklmnopqrstuvwxyz',
// New thread captcha. Require solving a captcha to post a thread.
'new_thread_capt' => false
]
];
// Ability to lock a board for normal users and still allow mods to post. Could also be useful for making an archive board // Ability to lock a board for normal users and still allow mods to post. Could also be useful for making an archive board
$config['board_locked'] = false; $config['board_locked'] = false;

View File

@ -61,21 +61,29 @@ function build_context(array $config): Context {
RemoteCaptchaQuery::class => function($c) { RemoteCaptchaQuery::class => function($c) {
$config = $c->get('config'); $config = $c->get('config');
$http = $c->get(HttpDriver::class); $http = $c->get(HttpDriver::class);
if ($config['recaptcha']) { switch ($config['captcha']['provider']) {
return new ReCaptchaQuery($http, $config['recaptcha_private']); case 'recaptcha':
} elseif ($config['hcaptcha']) { return new ReCaptchaQuery($http, $config['captcha']['recaptcha']['secret']);
return new HCaptchaQuery($http, $config['hcaptcha_private'], $config['hcaptcha_public']); case 'hcaptcha':
} else { return new HCaptchaQuery(
$http,
$config['captcha']['hcaptcha']['secret'],
$config['captcha']['hcaptcha']['sitekey']
);
default:
throw new RuntimeException('No remote captcha service available'); throw new RuntimeException('No remote captcha service available');
} }
}, },
NativeCaptchaQuery::class => function($c) { NativeCaptchaQuery::class => function($c) {
$http = $c->get(HttpDriver::class);
$config = $c->get('config'); $config = $c->get('config');
return new NativeCaptchaQuery($http, if ($config['captcha']['provider'] !== 'secureimage') {
throw new RuntimeException('No native captcha service available');
}
return new NativeCaptchaQuery(
$c->get(HttpDriver::class),
$config['domain'], $config['domain'],
$config['captcha']['provider_check'], $config['captcha']['secureimage']['provider_check'],
$config['captcha']['extra'] $config['captcha']['secureimage']['extra']
); );
} }
]); ]);

View File

@ -629,8 +629,13 @@ if (isset($_POST['delete'])) {
// Check for CAPTCHA right after opening the board so the "return" link is in there. // Check for CAPTCHA right after opening the board so the "return" link is in there.
try { try {
$provider = $config['captcha']['provider'];
$new_thread_capt = $config['captcha']['secureimage']['new_thread_capt'];
$dynamic = $config['captcha']['dynamic'];
// With our custom captcha provider // With our custom captcha provider
if ($config['captcha']['enabled'] || ($post['op'] && $config['new_thread_capt'])) { if (($provider === 'secureimage' && !$new_thread_capt)
|| ($provider === 'secureimage' && $new_thread_capt && $post['op'])) {
$query = $context->get(NativeCaptchaQuery::class); $query = $context->get(NativeCaptchaQuery::class);
$success = $query->verify($_POST['captcha_text'], $_POST['captcha_cookie']); $success = $query->verify($_POST['captcha_text'], $_POST['captcha_cookie']);
@ -648,8 +653,7 @@ if (isset($_POST['delete'])) {
} }
} }
// Remote 3rd party captchas. // Remote 3rd party captchas.
elseif (($config['recaptcha'] || $config['hcaptcha']) elseif ($provider && (!$dynamic || $dynamic === $_SERVER['REMOTE_ADDR'])) {
&& (!$config['dynamic_captcha'] || $config['dynamic_captcha'] === $_SERVER['REMOTE_ADDR'])) {
$query = $content->get(RemoteCaptchaQuery::class); $query = $content->get(RemoteCaptchaQuery::class);
$field = $query->responseField(); $field = $query->responseField();

View File

@ -20,7 +20,7 @@
<script type="text/javascript" src="/js/mod/mod_snippets.js"></script> <script type="text/javascript" src="/js/mod/mod_snippets.js"></script>
{% endif %} {% endif %}
{% endif %} {% endif %}
{% if config.recaptcha %}<script src="//www.recaptcha.net/recaptcha/api.js"></script> {% if config.captcha.provider == 'recaptcha' %}<script src="//www.recaptcha.net/recaptcha/api.js"></script>
<style type="text/css">{% verbatim %} <style type="text/css">{% verbatim %}
#recaptcha_area { #recaptcha_area {
float: none !important; float: none !important;
@ -50,6 +50,6 @@
padding: 0 !important; padding: 0 !important;
} }
{% endverbatim %}</style>{% endif %} {% endverbatim %}</style>{% endif %}
{% if config.hcaptcha %} {% if config.captcha.provider.hcaptcha %}
<script src="https://js.hcaptcha.com/1/api.js" async defer></script> <script src="https://js.hcaptcha.com/1/api.js" async defer></script>
{% endif %} {% endif %}

View File

@ -223,15 +223,15 @@ function getCookie(cookie_name) {
} }
{% endraw %} {% endraw %}
{% if config.dynamic_captcha %} {% if config.captcha.dynamic %}
function is_dynamic_captcha_enabled() { function is_dynamic_captcha_enabled() {
let cookie = get_cookie('require-captcha'); let cookie = get_cookie('require-captcha');
return cookie === '1'; return cookie === '1';
} }
function get_captcha_pub_key() { function get_captcha_pub_key() {
{% if config.recaptcha %} {% if config.captcha.provider === 'recaptcha' %}
return "{{ config.recaptcha_public }}"; return "{{ config.captcha.recaptcha.sitekey }}";
{% else %} {% else %}
return null; return null;
{% endif %} {% endif %}

View File

@ -72,8 +72,8 @@
{% endif %} {% endif %}
</td> </td>
</tr> </tr>
{% if config.recaptcha %} {% if config.captcha.provider == 'recaptcha' %}
{% if config.dynamic_captcha %} {% if config.captcha.dynamic %}
<tr id="captcha" style="display: none;"> <tr id="captcha" style="display: none;">
{% else %} {% else %}
<tr> <tr>
@ -83,19 +83,19 @@
{{ antibot.html() }} {{ antibot.html() }}
</th> </th>
<td> <td>
<div class="g-recaptcha" data-sitekey="{{ config.recaptcha_public }}"></div> <div class="g-recaptcha" data-sitekey="{{ config.captcha.recaptcha.sitekey }}"></div>
{{ antibot.html() }} {{ antibot.html() }}
</td> </td>
</tr> </tr>
{% endif %} {% endif %}
{% if config.hcaptcha %} {% if config.captcha.provider == 'hcaptcha' %}
<tr> <tr>
<th> <th>
{% trans %}Verification{% endtrans %} {% trans %}Verification{% endtrans %}
{{ antibot.html() }} {{ antibot.html() }}
</th> </th>
<td> <td>
<div class="h-captcha" data-sitekey="{{ config.hcaptcha_public }}"></div> <div class="h-captcha" data-sitekey="{{ config.captcha.hcaptcha.sitekey }}"></div>
{{ antibot.html() }} {{ antibot.html() }}
</td> </td>
</tr> </tr>
@ -106,11 +106,11 @@
{% trans %}Verification{% endtrans %} {% trans %}Verification{% endtrans %}
</th> </th>
<td> <td>
<script>load_captcha("{{ config.captcha.provider_get }}", "{{ config.captcha.extra }}");</script> <script>load_captcha("{{ config.captcha.secureimage.provider_get }}", "{{ config.secureimage.captcha.extra }}");</script>
<noscript> <noscript>
<input class='captcha_text' type='text' name='captcha_text' size='32' maxlength='6' autocomplete='off'> <input class='captcha_text' type='text' name='captcha_text' size='32' maxlength='6' autocomplete='off'>
<div class="captcha_html"> <div class="captcha_html">
<img src="/{{ config.captcha.provider_get }}?mode=get&raw=1"> <img src="/{{ config.captcha.secureimage.provider_get }}?mode=get&raw=1">
</div> </div>
</noscript> </noscript>
</td> </td>
@ -122,11 +122,11 @@
{% trans %}Verification{% endtrans %} {% trans %}Verification{% endtrans %}
</th> </th>
<td> <td>
<script>load_captcha("{{ config.captcha.provider_get }}", "{{ config.captcha.extra }}");</script> <script>load_captcha("{{ config.captcha.secureimage.provider_get }}", "{{ config.captcha.secureimage.extra }}");</script>
<noscript> <noscript>
<input class='captcha_text' type='text' name='captcha_text' size='32' maxlength='6' autocomplete='off'> <input class='captcha_text' type='text' name='captcha_text' size='32' maxlength='6' autocomplete='off'>
<div class="captcha_html"> <div class="captcha_html">
<img src="/{{ config.captcha.provider_get }}?mode=get&raw=1"> <img src="/{{ config.captcha.secureimage.provider_get }}?mode=get&raw=1">
</div> </div>
</noscript> </noscript>
</td> </td>