1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* TReCaptcha2Validator class file |
5
|
|
|
* |
6
|
|
|
* @author Cristian Camilo Naranjo Valencia |
7
|
|
|
* @link http://icolectiva.co |
8
|
|
|
* @copyright Copyright © 2005-2016 The PRADO Group |
9
|
|
|
* @license https://github.com/pradosoft/prado/blob/master/COPYRIGHT |
10
|
|
|
* @package System.Web.UI.WebControls |
11
|
|
|
*/ |
12
|
|
|
|
13
|
|
|
namespace Prado\Web\UI\WebControls; |
14
|
|
|
use Exception; |
15
|
|
|
use Prado\Web\Javascripts\TJavaScript; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* TReCaptcha2Validator class |
19
|
|
|
* |
20
|
|
|
* TReCaptcha2Validator validates a reCAPTCHA represented by a {@link TReCaptcha} control. |
21
|
|
|
* The input control fails validation if th user did not pass the humanity test. |
22
|
|
|
* |
23
|
|
|
* To use TReCaptcha2Validator, specify the {@link setCaptchaControl CaptchaControl} |
24
|
|
|
* to be the ID path of the {@link TReCaptcha} control. |
25
|
|
|
* |
26
|
|
|
* @author Cristian Camilo Naranjo Valencia |
27
|
|
|
* @package System.Web.UI.WebControls |
28
|
|
|
* @since 3.3.1 |
29
|
|
|
*/ |
30
|
|
|
|
31
|
|
|
class TReCaptcha2Validator extends TBaseValidator |
32
|
|
|
{ |
33
|
|
|
protected $_isvalid = null; |
34
|
|
|
|
35
|
|
|
protected function getClientClassName() |
36
|
|
|
{ |
37
|
|
|
return 'Prado.WebUI.TReCaptcha2Validator'; |
38
|
|
|
} |
39
|
|
|
public function getEnableClientScript() |
40
|
|
|
{ |
41
|
|
|
return true; |
42
|
|
|
} |
43
|
|
|
protected function getCaptchaControl() |
44
|
|
|
{ |
45
|
|
|
$control = $this->getValidationTarget(); |
46
|
|
|
if (!$control) |
47
|
|
|
throw new Exception('No target control specified for TReCaptcha2Validator'); |
48
|
|
|
if (!($control instanceof TReCaptcha2)) |
49
|
|
|
throw new Exception('TReCaptcha2Validator only works with TReCaptcha2 controls'); |
50
|
|
|
return $control; |
51
|
|
|
} |
52
|
|
|
public function getClientScriptOptions() |
53
|
|
|
{ |
54
|
|
|
$options = parent::getClientScriptOptions(); |
55
|
|
|
$options['ResponseFieldName'] = $this->getCaptchaControl()->getResponseFieldName(); |
56
|
|
|
return $options; |
57
|
|
|
} |
58
|
|
|
/** |
59
|
|
|
* This method overrides the parent's implementation. |
60
|
|
|
* The validation succeeds if the input control has the same value |
61
|
|
|
* as the one displayed in the corresponding RECAPTCHA control. |
62
|
|
|
* |
63
|
|
|
* @return boolean whether the validation succeeds |
64
|
|
|
*/ |
65
|
|
|
protected function evaluateIsValid() |
66
|
|
|
{ |
67
|
|
|
// check validity only once (if trying to evaulate multiple times, all redundant checks would fail) |
68
|
|
|
if (is_null($this->_isvalid)) |
69
|
|
|
{ |
70
|
|
|
$control = $this->getCaptchaControl(); |
71
|
|
|
$this->_isvalid = $control->validate(); |
72
|
|
|
} |
73
|
|
|
return ($this->_isvalid==true); |
74
|
|
|
} |
75
|
|
|
public function onPreRender($param) |
76
|
|
|
{ |
77
|
|
|
parent::onPreRender($param); |
78
|
|
|
|
79
|
|
|
$cs = $this->Page->getClientScript(); |
80
|
|
|
$cs->registerPradoScript('validator'); |
81
|
|
|
|
82
|
|
|
// communicate validation status to the client side |
83
|
|
|
$value = $this->_isvalid===false ? '0' : '1'; |
84
|
|
|
$cs->registerHiddenField($this->getClientID().'_1',$value); |
85
|
|
|
|
86
|
|
|
// update validator display |
87
|
|
|
if ($control = $this->getValidationTarget()) |
88
|
|
|
{ |
89
|
|
|
$fn = 'captchaUpdateValidatorStatus_'.$this->getClientID(); |
90
|
|
|
|
91
|
|
|
$cs->registerEndScript($this->getClientID().'::validate', implode(' ',array( |
92
|
|
|
// this function will be used to update the validator |
93
|
|
|
'function '.$fn.'(valid)', |
94
|
|
|
'{', |
95
|
|
|
' jQuery('.TJavaScript::quoteString('#'.$this->getClientID().'_1').').val(valid);', |
96
|
|
|
' Prado.Validation.validateControl('.TJavaScript::quoteString($control->ClientID).'); ', |
97
|
|
|
'}', |
98
|
|
|
'', |
99
|
|
|
// update the validator to the result if we're in a callback |
100
|
|
|
// (if we're in initial rendering or a postback then the result will be rendered directly to the page html anyway) |
101
|
|
|
$this->Page->IsCallback ? $fn.'('.$value.');' : '', |
102
|
|
|
'', |
103
|
|
|
// install event handler that clears the validation error when user changes the captcha response field |
104
|
|
|
'jQuery("#'.$control->getClientID().'").on("change", '.TJavaScript::quoteString('#'.$control->getResponseFieldName()).', function() { ', |
105
|
|
|
$fn.'("1");', |
106
|
|
|
'});', |
107
|
|
|
))); |
108
|
|
|
} |
109
|
|
|
} |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
|