2019-03-24 09:58:37 +08:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Rules;
|
|
|
|
|
2020-03-09 17:18:36 +08:00
|
|
|
use Composer\CaBundle\CaBundle;
|
2019-09-05 12:23:46 +08:00
|
|
|
use Gregwar\Captcha\CaptchaBuilder;
|
2019-03-24 09:58:37 +08:00
|
|
|
use Illuminate\Contracts\Validation\Rule;
|
2020-03-09 17:18:36 +08:00
|
|
|
use Illuminate\Support\Facades\Http;
|
2019-03-24 09:58:37 +08:00
|
|
|
|
|
|
|
class Captcha implements Rule
|
|
|
|
{
|
2019-04-04 11:04:13 +08:00
|
|
|
public function passes($attribute, $value)
|
|
|
|
{
|
2019-03-24 09:58:37 +08:00
|
|
|
$secretkey = option('recaptcha_secretkey');
|
|
|
|
if ($secretkey) {
|
2020-03-09 17:18:36 +08:00
|
|
|
return Http::asForm()
|
|
|
|
->withOptions(['verify' => CaBundle::getSystemCaRootBundlePath()])
|
|
|
|
->post('https://www.recaptcha.net/recaptcha/api/siteverify', [
|
|
|
|
'secret' => $secretkey,
|
|
|
|
'response' => $value,
|
|
|
|
])
|
|
|
|
->json()['success'];
|
2019-03-24 09:58:37 +08:00
|
|
|
}
|
|
|
|
|
2019-09-05 12:23:46 +08:00
|
|
|
$builder = new CaptchaBuilder(session()->pull('captcha'));
|
2019-09-07 11:00:35 +08:00
|
|
|
|
2019-09-05 12:23:46 +08:00
|
|
|
return $builder->testPhrase($value);
|
2019-03-24 09:58:37 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
public function message()
|
|
|
|
{
|
|
|
|
return option('recaptcha_secretkey')
|
|
|
|
? trans('validation.recaptcha')
|
|
|
|
: trans('validation.captcha');
|
|
|
|
}
|
|
|
|
}
|