Completed
Push — namespace2 ( 8a6673...791eac )
by Fabio
08:25
created

TReCaptcha2Validator   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 80
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
dl 0
loc 80
rs 10
c 0
b 0
f 0
wmc 12
lcom 1
cbo 1

6 Methods

Rating   Name   Duplication   Size   Complexity  
A getClientClassName() 0 4 1
A getEnableClientScript() 0 4 1
A getCaptchaControl() 0 9 3
A getClientScriptOptions() 0 6 1
A evaluateIsValid() 0 10 2
B onPreRender() 0 35 4
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
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