Skip to content

Commit 29cbb4f

Browse files
Bahaa OdehBahaa Odeh
authored andcommitted
add recaptcha v3
1 parent d5f73d3 commit 29cbb4f

File tree

4 files changed

+241
-14
lines changed

4 files changed

+241
-14
lines changed

AutoloadExample.php

Lines changed: 0 additions & 14 deletions
This file was deleted.

RecaptchaV3.php

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
<?php
2+
/**
3+
* Created by PhpStorm.
4+
* User: bahaaodeh
5+
* Date: 12/22/18
6+
* Time: 7:07 PM
7+
*/
8+
9+
namespace Baha2Odeh\RecaptchaV3;
10+
11+
12+
use Yii;
13+
use yii\base\Component;
14+
use yii\helpers\Json;
15+
use yii\web\View;
16+
17+
class RecaptchaV3 extends Component
18+
{
19+
public $site_key = null;
20+
public $secret_key = null;
21+
22+
private $verify_endpoint = 'https://www.google.com/recaptcha/api/siteverify';
23+
24+
public function init()
25+
{
26+
parent::init(); // TODO: Change the autogenerated stub
27+
28+
if(empty($this->site_key)){
29+
throw new \Exception('site key cant be null');
30+
}
31+
if(empty($this->secret_key)){
32+
throw new \Exception('secret key cant be null');
33+
}
34+
35+
}
36+
37+
/**
38+
* @param $view
39+
*
40+
*/
41+
public function registerScript($view){
42+
/** @var View $view */
43+
$view->registerJsFile('https://www.google.com/recaptcha/api.js?render='.$this->site_key,[
44+
'position'=>$view::POS_HEAD,
45+
],'recaptcha-v3-script');
46+
}
47+
48+
49+
50+
public function validateValue($value)
51+
{
52+
53+
try {
54+
$response = $this->curl([
55+
'secret' => $this->secret_key,
56+
'response' => $value,
57+
'remoteip' => Yii::$app->has('request') ? Yii::$app->request->userIP : null,
58+
]);
59+
if(!empty($response) && $response['success']){
60+
return $response['score'];
61+
}
62+
}catch (\Exception $e){
63+
64+
}
65+
66+
return false;
67+
}
68+
/**
69+
* Sends User post data with curl to google and receives 'success' if valid
70+
* @param array $params
71+
* @return bool
72+
*/
73+
protected function curl(array $params)
74+
{
75+
$curl = curl_init();
76+
curl_setopt($curl, CURLOPT_URL, $this->verify_endpoint);
77+
curl_setopt($curl, CURLOPT_POST, true);
78+
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
79+
curl_setopt($curl, CURLOPT_POSTFIELDS, $params);
80+
$curlData = curl_exec($curl);
81+
curl_close($curl);
82+
return Json::decode($curlData, true);
83+
}
84+
}

RecaptchaV3Validator.php

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
<?php
2+
/**
3+
* Created by PhpStorm.
4+
* User: bahaaodeh
5+
* Date: 12/22/18
6+
* Time: 7:23 PM
7+
*/
8+
9+
namespace Baha2Odeh\RecaptchaV3;
10+
11+
12+
use Yii;
13+
use yii\base\InvalidConfigException;
14+
use yii\di\Instance;
15+
use yii\validators\Validator;
16+
17+
class RecaptchaV3Validator extends Validator
18+
{
19+
/**
20+
* @var bool
21+
*/
22+
public $skipOnEmpty = false;
23+
24+
25+
/**
26+
* Recaptcha component
27+
* @var string|array|RecaptchaV3
28+
*/
29+
public $component = 'recaptchaV3';
30+
31+
32+
/**
33+
* the score for this request (0.0 - 1.0)
34+
* @var null|int
35+
*/
36+
public $acceptance_score = null;
37+
38+
/**
39+
* @var RecaptchaV3
40+
*/
41+
private $_component = null;
42+
/**
43+
* @inheritdoc
44+
* @throws InvalidConfigException
45+
*/
46+
public function init()
47+
{
48+
parent::init();
49+
$component = Instance::ensure($this->component, RecaptchaV3::class);
50+
if ($component == null) {
51+
throw new InvalidConfigException(Yii::t('recaptchav3', 'component is required.'));
52+
}
53+
$this->_component = $component;
54+
55+
if ($this->message === null) {
56+
$this->message = Yii::t('recaptchav3', 'The verification code is incorrect.');
57+
}
58+
}
59+
60+
/**
61+
* @inheritdoc
62+
*/
63+
protected function validateValue($value)
64+
{
65+
66+
$result = $this->_component->validateValue($value);
67+
if($result === false){
68+
return [$this->message, []];
69+
}
70+
if($this->acceptance_score !== null && $result < $this->acceptance_score){
71+
return [$this->message, []];
72+
}
73+
return null;
74+
}
75+
76+
}

RecaptchaV3Widget.php

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
<?php
2+
/**
3+
* Created by PhpStorm.
4+
* User: bahaaodeh
5+
* Date: 12/22/18
6+
* Time: 7:51 PM
7+
*/
8+
9+
namespace Baha2Odeh\RecaptchaV3;
10+
11+
12+
use Yii;
13+
use yii\base\InvalidConfigException;
14+
use yii\di\Instance;
15+
use yii\helpers\Html;
16+
use yii\widgets\InputWidget;
17+
18+
class RecaptchaV3Widget extends InputWidget
19+
{
20+
/**
21+
* Recaptcha component
22+
* @var string|array|RecaptchaV3
23+
*/
24+
public $component = 'recaptchaV3';
25+
26+
/**
27+
* @var string
28+
*/
29+
public $buttonText = 'Submit';
30+
31+
/**
32+
* @var string
33+
*/
34+
public $actionName = 'homepage';
35+
36+
/**
37+
* @var RecaptchaV3
38+
*/
39+
private $_component = null;
40+
41+
/**
42+
* @inheritdoc
43+
* @throws InvalidConfigException
44+
*/
45+
public function init()
46+
{
47+
parent::init();
48+
$component = Instance::ensure($this->component, RecaptchaV3::class);
49+
if ($component == null) {
50+
throw new InvalidConfigException(Yii::t('recaptchav3', 'component is required.'));
51+
}
52+
$this->_component = $component;
53+
}
54+
55+
56+
57+
/**
58+
* @inheritdoc
59+
*/
60+
public function run()
61+
{
62+
$this->_component->registerScript($this->getView());
63+
$this->field->template = "{input}\n{error}";
64+
$formId = $this->field->form->id;
65+
$inputId = Html::getInputId($this->model, $this->attribute);
66+
67+
return
68+
Html::tag('script', <<<JS
69+
grecaptcha.ready(function() {
70+
grecaptcha.execute('{$this->_component->site_key}', {action: '{$this->actionName}'}).then(function(token) {
71+
alert(token);
72+
$('#{$inputId}').val(token);
73+
$('#{$formId}').submit();
74+
});
75+
});
76+
JS
77+
)
78+
. Html::activeHiddenInput($this->model, $this->attribute)
79+
. Html::button($this->buttonText, $this->options);
80+
}
81+
}

0 commit comments

Comments
 (0)