blessing-skin-server/app/Rules/Captcha.php

37 lines
985 B
PHP
Raw Normal View History

2019-03-24 09:58:37 +08:00
<?php
namespace App\Rules;
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;
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) {
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-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');
}
}