Completed
Push — master ( af129a...c7fd3e )
by Fabio
12:48
created

TReCaptcha2Validator::onPreRender()   B

Complexity

Conditions 4
Paths 4

Size

Total Lines 35
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

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