captcha-queries.php: refactor NativeCaptchaQuery to use DI

This commit is contained in:
Zankaria 2024-08-15 15:17:54 +02:00
parent 933594194c
commit a275d04efa
3 changed files with 18 additions and 6 deletions

View File

@ -4,6 +4,7 @@ namespace Vichan;
use RuntimeException; use RuntimeException;
use Vichan\Driver\{HttpDriver, HttpDrivers, Log, LogDrivers}; use Vichan\Driver\{HttpDriver, HttpDrivers, Log, LogDrivers};
use Vichan\Service\HCaptchaQuery; use Vichan\Service\HCaptchaQuery;
use Vichan\Service\NativeCaptchaQuery;
use Vichan\Service\ReCaptchaQuery; use Vichan\Service\ReCaptchaQuery;
use Vichan\Service\RemoteCaptchaQuery; use Vichan\Service\RemoteCaptchaQuery;
@ -67,6 +68,15 @@ function build_context(array $config): Context {
} else { } else {
throw new RuntimeException('No remote captcha service available'); throw new RuntimeException('No remote captcha service available');
} }
},
NativeCaptchaQuery::class => function($c) {
$http = $c->get(HttpDriver::class);
$config = $c->get('config');
return new NativeCaptchaQuery($http,
$config['domain'],
$config['captcha']['provider_check'],
$config['captcha']['extra']
);
} }
]); ]);
} }

View File

@ -106,32 +106,34 @@ class NativeCaptchaQuery {
private HttpDriver $http; private HttpDriver $http;
private string $domain; private string $domain;
private string $provider_check; private string $provider_check;
private string $extra;
/** /**
* @param HttpDriver $http The http client. * @param HttpDriver $http The http client.
* @param string $domain The server's domain. * @param string $domain The server's domain.
* @param string $provider_check Path to the endpoint. * @param string $provider_check Path to the endpoint.
* @param string $extra Extra http parameters.
*/ */
function __construct(HttpDriver $http, string $domain, string $provider_check) { function __construct(HttpDriver $http, string $domain, string $provider_check, string $extra) {
$this->http = $http; $this->http = $http;
$this->domain = $domain; $this->domain = $domain;
$this->provider_check = $provider_check; $this->provider_check = $provider_check;
$this->extra = $extra;
} }
/** /**
* Checks if the user at the remote ip passed the native vichan captcha. * Checks if the user at the remote ip passed the native vichan captcha.
* *
* @param string $extra Extra http parameters.
* @param string $user_text Remote user's text input. * @param string $user_text Remote user's text input.
* @param string $user_cookie Remote user cookie. * @param string $user_cookie Remote user cookie.
* @return bool Returns true if the user passed the check. * @return bool Returns true if the user passed the check.
* @throws RuntimeException Throws on IO errors. * @throws RuntimeException Throws on IO errors.
*/ */
public function verify(string $extra, string $user_text, string $user_cookie): bool { public function verify(string $user_text, string $user_cookie): bool {
$data = [ $data = [
'mode' => 'check', 'mode' => 'check',
'text' => $user_text, 'text' => $user_text,
'extra' => $extra, 'extra' => $this->extra,
'cookie' => $user_cookie 'cookie' => $user_cookie
]; ];

View File

@ -631,8 +631,8 @@ if (isset($_POST['delete'])) {
try { try {
// With our custom captcha provider // With our custom captcha provider
if ($config['captcha']['enabled'] || ($post['op'] && $config['new_thread_capt'])) { if ($config['captcha']['enabled'] || ($post['op'] && $config['new_thread_capt'])) {
$query = new NativeCaptchaQuery($context->get(HttpDriver::class), $config['domain'], $config['captcha']['provider_check']); $query = $context->get(NativeCaptchaQuery::class);
$success = $query->verify($config['captcha']['extra'], $_POST['captcha_text'], $_POST['captcha_cookie']); $success = $query->verify($_POST['captcha_text'], $_POST['captcha_cookie']);
if (!$success) { if (!$success) {
error( error(