opnform-host-nginx/app/Rules/ValidHCaptcha.php

52 lines
1.2 KiB
PHP
Raw Normal View History

2022-09-20 21:59:52 +02:00
<?php
namespace App\Rules;
use Closure;
2022-09-20 21:59:52 +02:00
use Illuminate\Contracts\Validation\ImplicitRule;
use Illuminate\Support\Facades\Http;
class ValidHCaptcha implements ImplicitRule
{
2024-02-23 11:54:12 +01:00
public const H_CAPTCHA_VERIFY_URL = 'https://hcaptcha.com/siteverify';
2022-09-20 21:59:52 +02:00
private $error = 'Invalid CAPTCHA. Please prove you\'re not a bot.';
/**
* Determine if the validation rule passes.
*
* @param string $attribute
* @param mixed $value
* @return bool
*/
public function passes($attribute, $value)
{
if (empty($value)) {
2024-02-23 11:54:12 +01:00
$this->error = 'Please complete the captcha.';
2022-09-20 21:59:52 +02:00
return false;
}
return Http::asForm()->post(self::H_CAPTCHA_VERIFY_URL, [
'secret' => config('services.h_captcha.secret_key'),
2024-02-23 11:54:12 +01:00
'response' => $value,
2022-09-20 21:59:52 +02:00
])->json('success');
}
public function validate(string $attribute, mixed $value, Closure $fail): void
{
if(!$this->passes($attribute, $value)) {
$fail($this->message());
}
}
2022-09-20 21:59:52 +02:00
/**
* Get the validation error message.
*
* @return string
*/
public function message()
{
return $this->error;
}
}