2019-03-24 09:58:37 +08:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Rules;
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
class Captcha implements Rule
|
|
|
|
{
|
2019-04-04 11:04:13 +08:00
|
|
|
protected $client;
|
|
|
|
|
|
|
|
public function __construct(\GuzzleHttp\Client $client)
|
2019-03-24 09:58:37 +08:00
|
|
|
{
|
2019-04-04 11:04:13 +08:00
|
|
|
$this->client = $client;
|
|
|
|
}
|
2019-03-24 09:58:37 +08:00
|
|
|
|
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) {
|
2019-04-04 11:04:13 +08:00
|
|
|
try {
|
|
|
|
$response = $this->client->post('https://www.recaptcha.net/recaptcha/api/siteverify', [
|
|
|
|
'form_params' => [
|
|
|
|
'secret' => $secretkey,
|
|
|
|
'response' => $value,
|
2019-04-19 19:36:36 +08:00
|
|
|
],
|
2019-08-04 18:19:53 +08:00
|
|
|
'verify' => \Composer\CaBundle\CaBundle::getSystemCaRootBundlePath(),
|
2019-04-04 11:04:13 +08:00
|
|
|
]);
|
|
|
|
if ($response->getStatusCode() == 200) {
|
|
|
|
$body = json_decode((string) $response->getBody());
|
2019-04-19 19:36:36 +08:00
|
|
|
|
2019-04-04 11:04:13 +08:00
|
|
|
return $body->success;
|
|
|
|
}
|
|
|
|
} catch (\GuzzleHttp\Exception\RequestException $e) {
|
|
|
|
return false;
|
2019-03-24 09:58:37 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-05 12:23:46 +08:00
|
|
|
$builder = new CaptchaBuilder(session()->pull('captcha'));
|
|
|
|
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');
|
|
|
|
}
|
|
|
|
}
|