Skip to content

Commit 90dccd8

Browse files
committed
re-new component
1 parent 3d50ea4 commit 90dccd8

File tree

6 files changed

+151
-150
lines changed

6 files changed

+151
-150
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# Created by .ignore support plugin (hsz.mobi)
2+
.idea

RecaptchaV3Widget.php

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

composer.json

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,33 @@
11
{
2-
"name": "baha2odeh/yii2-recaptcha-v3",
3-
"description": "Yii2 reCAPTCHA v3",
2+
"name": "kekaadrenalin/yii2-module-recaptcha-v3",
3+
"description": "This is reCAPTCHA v3 for Yii2.",
44
"type": "yii2-extension",
5-
"keywords": ["yii2","extension","recaptcha v3","captcha","recaptcha 3","Yii2 recaptcha","Yii2 recaptcha 3","Yii2 recaptcha v3"],
6-
"license": "Apache-2.0",
5+
"keywords": ["yii2","module","recaptcha v3","captcha","recaptcha 3","Yii2 recaptcha","Yii2 recaptcha 3","Yii2 recaptcha v3"],
6+
"license": "BSD-3-Clause",
7+
"support": {
8+
"source": "https://github.com/kekaadrenalin/yii2-module-recaptcha-v3",
9+
"issues": "https://github.com/kekaadrenalin/yii2-module-recaptcha-v3/issues"
10+
},
711
"authors": [
812
{
9-
"name": "Bahaa Odeh",
10-
"email": "bw4@hotmail.it"
13+
"name": "kekaadrenalin",
14+
"email": ""
1115
}
1216
],
17+
"minimum-stability": "stable",
1318
"require": {
14-
"yiisoft/yii2": "~2.0.0"
19+
"ext-curl": "*",
20+
"yiisoft/yii2": "~2.0.15"
1521
},
22+
"repositories": [
23+
{
24+
"type": "composer",
25+
"url": "https://asset-packagist.org"
26+
}
27+
],
1628
"autoload": {
1729
"psr-4": {
18-
"Baha2Odeh\\RecaptchaV3\\": ""
30+
"kekaadrenalin\\recaptcha3\\": "src"
1931
}
2032
}
2133
}
Lines changed: 26 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,73 +1,73 @@
11
<?php
2-
/**
3-
* Created by PhpStorm.
4-
* User: bahaaodeh
5-
* Date: 12/22/18
6-
* Time: 7:07 PM
7-
*/
82

9-
namespace Baha2Odeh\RecaptchaV3;
3+
namespace kekaadrenalin\recaptcha3;
104

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
5+
class ReCaptcha extends \yii\base\Component
186
{
197
public $site_key = null;
8+
209
public $secret_key = null;
2110

2211
private $verify_endpoint = 'https://www.google.com/recaptcha/api/siteverify';
2312

13+
/**
14+
* @inheritdoc
15+
*/
2416
public function init()
2517
{
2618
parent::init(); // TODO: Change the autogenerated stub
2719

28-
if(empty($this->site_key)){
20+
if (empty($this->site_key)) {
2921
throw new \Exception('site key cant be null');
3022
}
31-
if(empty($this->secret_key)){
23+
24+
if (empty($this->secret_key)) {
3225
throw new \Exception('secret key cant be null');
3326
}
34-
3527
}
3628

3729
/**
3830
* @param $view
3931
*
4032
*/
41-
public function registerScript($view){
33+
public function registerScript($view)
34+
{
4235
/** @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');
36+
$view->registerJsFile('https://www.google.com/recaptcha/api.js?render=' . $this->site_key, [
37+
'position' => $view::POS_HEAD,
38+
], 'recaptcha-v3-script');
4639
}
4740

4841

49-
42+
/**
43+
* @param $value
44+
*
45+
* @return bool
46+
*/
5047
public function validateValue($value)
5148
{
5249

5350
try {
5451
$response = $this->curl([
55-
'secret' => $this->secret_key,
52+
'secret' => $this->secret_key,
5653
'response' => $value,
5754
'remoteip' => Yii::$app->has('request') ? Yii::$app->request->userIP : null,
5855
]);
59-
if(!empty($response) && $response['success']){
56+
if (!empty($response) && $response['success']) {
6057
return $response['score'];
6158
}
62-
}catch (\Exception $e){
59+
} catch (\Exception $e) {
6360

6461
}
6562

6663
return false;
6764
}
65+
6866
/**
6967
* Sends User post data with curl to google and receives 'success' if valid
68+
*
7069
* @param array $params
70+
*
7171
* @return bool
7272
*/
7373
protected function curl(array $params)
@@ -79,6 +79,7 @@ protected function curl(array $params)
7979
curl_setopt($curl, CURLOPT_POSTFIELDS, $params);
8080
$curlData = curl_exec($curl);
8181
curl_close($curl);
82+
8283
return Json::decode($curlData, true);
8384
}
8485
}
Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,13 @@
11
<?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;
102

3+
namespace kekaadrenalin\recaptcha3;
114

125
use Yii;
136
use yii\base\InvalidConfigException;
147
use yii\di\Instance;
158
use yii\validators\Validator;
169

17-
class RecaptchaV3Validator extends Validator
10+
class ReCaptchaValidator extends Validator
1811
{
1912
/**
2013
* @var bool
@@ -24,9 +17,9 @@ class RecaptchaV3Validator extends Validator
2417

2518
/**
2619
* Recaptcha component
27-
* @var string|array|RecaptchaV3
20+
* @var string|array|ReCaptcha
2821
*/
29-
public $component = 'recaptchaV3';
22+
public $component = 'reCaptcha3';
3023

3124

3225
/**
@@ -36,24 +29,25 @@ class RecaptchaV3Validator extends Validator
3629
public $acceptance_score = null;
3730

3831
/**
39-
* @var RecaptchaV3
32+
* @var ReCaptcha
4033
*/
4134
private $_component = null;
35+
4236
/**
4337
* @inheritdoc
4438
* @throws InvalidConfigException
4539
*/
4640
public function init()
4741
{
4842
parent::init();
49-
$component = Instance::ensure($this->component, RecaptchaV3::class);
43+
$component = Instance::ensure($this->component, ReCaptcha::class);
5044
if ($component == null) {
51-
throw new InvalidConfigException(Yii::t('recaptchav3', 'component is required.'));
45+
throw new InvalidConfigException('Component is required.');
5246
}
5347
$this->_component = $component;
5448

5549
if ($this->message === null) {
56-
$this->message = Yii::t('recaptchav3', 'The verification code is incorrect.');
50+
$this->message = 'The verification code is incorrect.';
5751
}
5852
}
5953

@@ -63,12 +57,14 @@ public function init()
6357
protected function validateValue($value)
6458
{
6559
$result = $this->_component->validateValue($value);
66-
if($result === false){
60+
if ($result === false) {
6761
return [$this->message, []];
6862
}
69-
if($this->acceptance_score !== null && $result < $this->acceptance_score){
63+
64+
if ($this->acceptance_score !== null && $result < $this->acceptance_score) {
7065
return [$this->message, []];
7166
}
67+
7268
return null;
7369
}
7470

0 commit comments

Comments
 (0)