-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathReCaptchaValidator.php
69 lines (62 loc) · 1.57 KB
/
ReCaptchaValidator.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
<?php
namespace recaptcha;
use Yii;
use yii\validators\Validator;
/**
* @author Sergey Bogatyrev <[email protected]>
* @since 2.0
*/
class ReCaptchaValidator extends Validator
{
public $enableClientValidation = false;
/**
* @var boolean whether to skip this validator if the input is empty.
*/
public $skipOnEmpty = false;
/**
* @var bool whether or not send user IP address.
*/
public $remoteIp;
/**
* @var \recaptcha\ReCaptchaComponent|string $_component
*/
public $_component = 'recaptcha';
/**
* @inheritdoc
*/
public function init()
{
parent::init();
$component = $this->getComponent();
if ($this->remoteIp === null) {
$this->remoteIp = $component->remoteIp;
}
}
/**
* Set component property.
* @param string $component
*/
public function setComponent($component)
{
$this->_component = Yii::$app->get($component);
}
/**
* Get ReCaptchaComponent opject.
* @return \recaptcha\ReCaptchaComponent|null|object|string
*/
public function getComponent() {
if (!is_object($this->_component)) {
return $this->_component = Yii::$app->get($this->_component);
}
return $this->_component;
}
/**
* @inheritdoc
*/
protected function validateValue($value)
{
$component = $this->getComponent();
$ip = $this->remoteIp ? Yii::$app->request->userIP : null;
return $component->verify($value, $ip) ? null : [$this->message, []];
}
}