blessing-skin-server/app/Rules/Captcha.php
2019-04-19 19:36:36 +08:00

47 lines
1.2 KiB
PHP

<?php
namespace App\Rules;
use Illuminate\Contracts\Validation\Rule;
class Captcha implements Rule
{
protected $client;
public function __construct(\GuzzleHttp\Client $client)
{
$this->client = $client;
}
public function passes($attribute, $value)
{
$secretkey = option('recaptcha_secretkey');
if ($secretkey) {
try {
$response = $this->client->post('https://www.recaptcha.net/recaptcha/api/siteverify', [
'form_params' => [
'secret' => $secretkey,
'response' => $value,
],
]);
if ($response->getStatusCode() == 200) {
$body = json_decode((string) $response->getBody());
return $body->success;
}
} catch (\GuzzleHttp\Exception\RequestException $e) {
return false;
}
}
return captcha_check($value);
}
public function message()
{
return option('recaptcha_secretkey')
? trans('validation.recaptcha')
: trans('validation.captcha');
}
}