2016-01-15 14:51:27 +05:30
|
|
|
<?php
|
|
|
|
namespace api\components\ReCaptcha;
|
|
|
|
|
2016-08-03 18:26:08 +05:30
|
|
|
use common\helpers\Error as E;
|
2017-05-18 04:39:26 +05:30
|
|
|
use GuzzleHttp\ClientInterface;
|
2017-05-18 19:36:01 +05:30
|
|
|
use GuzzleHttp\Exception\ConnectException;
|
|
|
|
use GuzzleHttp\Exception\ServerException;
|
|
|
|
use Psr\Http\Message\ResponseInterface;
|
2016-01-15 14:51:27 +05:30
|
|
|
use Yii;
|
|
|
|
use yii\base\Exception;
|
2017-05-18 04:39:26 +05:30
|
|
|
use yii\di\Instance;
|
2016-01-15 14:51:27 +05:30
|
|
|
|
|
|
|
class Validator extends \yii\validators\Validator {
|
|
|
|
|
2018-03-16 21:35:39 +05:30
|
|
|
private const SITE_VERIFY_URL = 'https://recaptcha.net/recaptcha/api/siteverify';
|
2017-05-18 19:36:01 +05:30
|
|
|
|
|
|
|
private const REPEAT_LIMIT = 3;
|
|
|
|
private const REPEAT_TIMEOUT = 1;
|
2016-01-15 14:51:27 +05:30
|
|
|
|
|
|
|
public $skipOnEmpty = false;
|
|
|
|
|
2016-08-03 18:26:08 +05:30
|
|
|
public $message = E::CAPTCHA_INVALID;
|
|
|
|
|
|
|
|
public $requiredMessage = E::CAPTCHA_REQUIRED;
|
2016-01-15 14:51:27 +05:30
|
|
|
|
2017-05-18 04:39:26 +05:30
|
|
|
/**
|
|
|
|
* @var Component|string
|
|
|
|
*/
|
|
|
|
public $component = 'reCaptcha';
|
|
|
|
|
|
|
|
private $client;
|
|
|
|
|
|
|
|
public function __construct(ClientInterface $client, array $config = []) {
|
|
|
|
parent::__construct($config);
|
|
|
|
$this->client = $client;
|
|
|
|
}
|
|
|
|
|
2016-01-15 14:51:27 +05:30
|
|
|
public function init() {
|
|
|
|
parent::init();
|
2017-05-18 04:39:26 +05:30
|
|
|
$this->component = Instance::ensure($this->component, Component::class);
|
2016-01-15 14:51:27 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @inheritdoc
|
|
|
|
*/
|
|
|
|
protected function validateValue($value) {
|
|
|
|
if (empty($value)) {
|
2016-08-03 18:26:08 +05:30
|
|
|
return [$this->requiredMessage, []];
|
2016-01-15 14:51:27 +05:30
|
|
|
}
|
|
|
|
|
2017-05-18 19:36:01 +05:30
|
|
|
$repeats = 0;
|
|
|
|
do {
|
|
|
|
$isSuccess = true;
|
|
|
|
try {
|
|
|
|
$response = $this->performRequest($value);
|
|
|
|
} catch (ConnectException | ServerException $e) {
|
|
|
|
if (++$repeats >= self::REPEAT_LIMIT) {
|
|
|
|
throw $e;
|
|
|
|
}
|
|
|
|
|
|
|
|
$isSuccess = false;
|
|
|
|
sleep(self::REPEAT_TIMEOUT);
|
|
|
|
}
|
|
|
|
} while (!$isSuccess);
|
|
|
|
|
|
|
|
/** @noinspection PhpUndefinedVariableInspection */
|
|
|
|
$data = json_decode($response->getBody(), true);
|
|
|
|
if (!isset($data['success'])) {
|
|
|
|
throw new Exception('Invalid recaptcha verify response.');
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!$data['success']) {
|
|
|
|
return [$this->message, []];
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string $value
|
|
|
|
* @throws \GuzzleHttp\Exception\GuzzleException
|
|
|
|
* @return ResponseInterface
|
|
|
|
*/
|
|
|
|
protected function performRequest(string $value): ResponseInterface {
|
|
|
|
return $this->client->request('POST', self::SITE_VERIFY_URL, [
|
2016-08-03 18:26:08 +05:30
|
|
|
'form_params' => [
|
2017-05-18 04:39:26 +05:30
|
|
|
'secret' => $this->component->secret,
|
2016-08-03 18:26:08 +05:30
|
|
|
'response' => $value,
|
|
|
|
'remoteip' => Yii::$app->getRequest()->getUserIP(),
|
|
|
|
],
|
|
|
|
]);
|
2016-01-15 14:51:27 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
}
|