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

41 lines
1.0 KiB
PHP
Raw Normal View History

2019-03-24 09:58:37 +08:00
<?php
namespace App\Rules;
use Illuminate\Contracts\Validation\Rule;
class Captcha implements Rule
{
public function passes($attribute, $value)
{
if (app()->environment('testing')) {
return true;
}
$secretkey = option('recaptcha_secretkey');
if ($secretkey) {
$client = new \GuzzleHttp\Client();
2019-03-24 10:14:27 +08:00
$response = $client->post('https://www.recaptcha.net/recaptcha/api/siteverify', [
2019-03-24 09:58:37 +08:00
'form_params' => [
'secret' => $secretkey,
'response' => $value,
]
]);
if ($response->getStatusCode() == 200) {
$body = json_decode((string) $response->getBody());
return $body->success;
}
return false;
}
return captcha_check($value);
}
public function message()
{
return option('recaptcha_secretkey')
? trans('validation.recaptcha')
: trans('validation.captcha');
}
}