mirror of
https://github.com/elyby/accounts.git
synced 2024-12-27 23:50:19 +05:30
44aaea2c08
Удалено тестовое окружение acceptance Удалена часть потенциально ненужных тестов Добавлена логика для формы регистрации Добавлена таблица для хранения ключей активации по E-mail Добавлены тесты для формы регистрации Реорганизован роутинг Добавлен компонент для ReCaptcha2
66 lines
1.7 KiB
PHP
66 lines
1.7 KiB
PHP
<?php
|
|
namespace api\components\ReCaptcha;
|
|
|
|
|
|
use Yii;
|
|
use yii\base\Exception;
|
|
use yii\base\InvalidConfigException;
|
|
|
|
class Validator extends \yii\validators\Validator {
|
|
|
|
const SITE_VERIFY_URL = 'https://www.google.com/recaptcha/api/siteverify';
|
|
const CAPTCHA_RESPONSE_FIELD = 'g-recaptcha-response';
|
|
|
|
public $skipOnEmpty = false;
|
|
|
|
/**
|
|
* @return Component
|
|
*/
|
|
protected function getComponent() {
|
|
return Yii::$app->reCaptcha;
|
|
}
|
|
|
|
public function init() {
|
|
parent::init();
|
|
if ($this->getComponent() === null) {
|
|
throw new InvalidConfigException('Required "reCaptcha" component as instance of ' . Component::class . '.');
|
|
}
|
|
|
|
if ($this->message === null) {
|
|
$this->message = Yii::t('yii', 'The verification code is incorrect.');
|
|
}
|
|
}
|
|
|
|
/**
|
|
* @inheritdoc
|
|
*/
|
|
protected function validateValue($value) {
|
|
$value = Yii::$app->request->post(self::CAPTCHA_RESPONSE_FIELD);
|
|
if (empty($value)) {
|
|
return [$this->message, []];
|
|
}
|
|
|
|
$requestParams = [
|
|
'secret' => $this->getComponent()->secret,
|
|
'response' => $value,
|
|
'remoteip' => Yii::$app->request->userIP,
|
|
];
|
|
|
|
$requestUrl = self::SITE_VERIFY_URL . '?' . http_build_query($requestParams);
|
|
$response = $this->getResponse($requestUrl);
|
|
|
|
if (!isset($response['success'])) {
|
|
throw new Exception('Invalid recaptcha verify response.');
|
|
}
|
|
|
|
return $response['success'] ? null : [$this->message, []];
|
|
}
|
|
|
|
protected function getResponse($request) {
|
|
$response = file_get_contents($request);
|
|
|
|
return json_decode($response, true);
|
|
}
|
|
|
|
}
|