diff --git a/inc/context.php b/inc/context.php index e6604835..30c9804e 100644 --- a/inc/context.php +++ b/inc/context.php @@ -4,6 +4,7 @@ namespace Vichan; use RuntimeException; use Vichan\Driver\{HttpDriver, HttpDrivers, Log, LogDrivers}; use Vichan\Service\HCaptchaQuery; +use Vichan\Service\NativeCaptchaQuery; use Vichan\Service\ReCaptchaQuery; use Vichan\Service\RemoteCaptchaQuery; @@ -67,6 +68,15 @@ function build_context(array $config): Context { } else { 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'] + ); } ]); } diff --git a/inc/service/captcha-queries.php b/inc/service/captcha-queries.php index cbd344a1..cd9a1b84 100644 --- a/inc/service/captcha-queries.php +++ b/inc/service/captcha-queries.php @@ -106,32 +106,34 @@ class NativeCaptchaQuery { private HttpDriver $http; private string $domain; private string $provider_check; + private string $extra; /** * @param HttpDriver $http The http client. * @param string $domain The server's domain. * @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->domain = $domain; $this->provider_check = $provider_check; + $this->extra = $extra; } /** * 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_cookie Remote user cookie. * @return bool Returns true if the user passed the check. * @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 = [ 'mode' => 'check', 'text' => $user_text, - 'extra' => $extra, + 'extra' => $this->extra, 'cookie' => $user_cookie ]; diff --git a/post.php b/post.php index fd78d749..ea5b1ccf 100644 --- a/post.php +++ b/post.php @@ -631,8 +631,8 @@ if (isset($_POST['delete'])) { try { // With our custom captcha provider if ($config['captcha']['enabled'] || ($post['op'] && $config['new_thread_capt'])) { - $query = new NativeCaptchaQuery($context->get(HttpDriver::class), $config['domain'], $config['captcha']['provider_check']); - $success = $query->verify($config['captcha']['extra'], $_POST['captcha_text'], $_POST['captcha_cookie']); + $query = $context->get(NativeCaptchaQuery::class); + $success = $query->verify($_POST['captcha_text'], $_POST['captcha_cookie']); if (!$success) { error(