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 {
|
|
|
|
|
2024-12-02 15:40:55 +05:30
|
|
|
private const string SITE_VERIFY_URL = 'https://recaptcha.net/recaptcha/api/siteverify';
|
2017-05-18 19:36:01 +05:30
|
|
|
|
2024-12-02 15:40:55 +05:30
|
|
|
private const int REPEAT_LIMIT = 3;
|
|
|
|
private const int 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;
|
|
|
|
|
2024-12-02 15:40:55 +05:30
|
|
|
public string $requiredMessage = E::CAPTCHA_REQUIRED;
|
2016-01-15 14:51:27 +05:30
|
|
|
|
2024-12-02 15:40:55 +05:30
|
|
|
public Component|string $component = 'reCaptcha';
|
2017-05-18 04:39:26 +05:30
|
|
|
|
2024-12-02 15:40:55 +05:30
|
|
|
public function __construct(
|
|
|
|
private readonly ClientInterface $client,
|
|
|
|
array $config = [],
|
|
|
|
) {
|
2017-05-18 04:39:26 +05:30
|
|
|
parent::__construct($config);
|
|
|
|
}
|
|
|
|
|
2024-12-02 15:40:55 +05:30
|
|
|
public function init(): void {
|
2016-01-15 14:51:27 +05:30
|
|
|
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
|
|
|
|
*/
|
2024-12-02 15:40:55 +05:30
|
|
|
protected function validateValue($value): ?array {
|
2016-01-15 14:51:27 +05:30
|
|
|
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);
|
2024-12-02 15:40:55 +05:30
|
|
|
} catch (ConnectException|ServerException $e) {
|
2017-05-18 19:36:01 +05:30
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @throws \GuzzleHttp\Exception\GuzzleException
|
|
|
|
*/
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
}
|