-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathInvisibleRecaptchaValidator.php
78 lines (67 loc) · 1.8 KB
/
InvisibleRecaptchaValidator.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
<?php
/**
* InvisibleRecaptchaValidator class file.
* @copyright (c) 2018, Bariev Pavel
* @license http://www.opensource.org/licenses/bsd-license.php
*/
namespace bariew\invisibleRecaptcha;
use Yii;
use yii\helpers\Json;
use yii\validators\Validator;
/**
* @see README.md
* @author Pavel Bariev <[email protected]>
*/
class InvisibleRecaptchaValidator extends Validator
{
const PARAM_KEY = 'invisible-recaptcha-key';
const PARAM_SECRET = 'invisible-recaptcha-secret';
const POST_PARAM = 'g-recaptcha-response';
const URL = 'https://www.google.com/recaptcha/api/siteverify';
/**
* @var bool
*/
public $skipOnEmpty = false;
/**
* @return string
*/
public static function key()
{
return \Yii::$app->params[static::PARAM_KEY];
}
/**
* @return string
*/
public static function secret()
{
return \Yii::$app->params[static::PARAM_SECRET];
}
/**
* @inheritdoc
*/
protected function validateValue($value)
{
$valid = $this->curl([
'secret' => static::secret(),
'response' => $value,
'remoteip' => Yii::$app->request->userIP
]);
return $valid ? null : [$this->message, []];
}
/**
* Sends User post data with curl to google and receives 'success' if valid
* @param array $params
* @return bool
*/
protected function curl(array $params)
{
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, static::URL);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, $params);
$curlData = curl_exec($curl);
curl_close($curl);
return !empty(Json::decode($curlData, true)['success']);
}
}