summaryrefslogtreecommitdiff
path: root/lib/prado/framework/Web/UI/WebControls/TReCaptcha2Validator.php
diff options
context:
space:
mode:
authoremkael <emkael@tlen.pl>2016-04-07 16:09:16 +0200
committeremkael <emkael@tlen.pl>2016-04-07 16:09:16 +0200
commitd42b25cbf6d3f1e51cb2a3149f1ff54cc5474bc9 (patch)
tree96b734357392d6094cfc5f8284ad8f2578b96b7f /lib/prado/framework/Web/UI/WebControls/TReCaptcha2Validator.php
parentac6c2b9c4f787dfdfa00547f764c77d80dad14c0 (diff)
* Prado upgrade (to 3.3.r6b8e6601752b21a8a96c385a5529bbec7bb2b408)
Diffstat (limited to 'lib/prado/framework/Web/UI/WebControls/TReCaptcha2Validator.php')
-rw-r--r--lib/prado/framework/Web/UI/WebControls/TReCaptcha2Validator.php110
1 files changed, 110 insertions, 0 deletions
diff --git a/lib/prado/framework/Web/UI/WebControls/TReCaptcha2Validator.php b/lib/prado/framework/Web/UI/WebControls/TReCaptcha2Validator.php
new file mode 100644
index 0000000..2cd4b6d
--- /dev/null
+++ b/lib/prado/framework/Web/UI/WebControls/TReCaptcha2Validator.php
@@ -0,0 +1,110 @@
+<?php
+
+/**
+ * TReCaptcha2Validator class file
+ *
+ * @author Cristian Camilo Naranjo Valencia
+ * @link http://icolectiva.co
+ * @copyright Copyright &copy; 2005-2016 The PRADO Group
+ * @license https://github.com/pradosoft/prado/blob/master/COPYRIGHT
+ * @package System.Web.UI.WebControls
+ */
+
+Prado::using('System.Web.UI.WebControls.TBaseValidator');
+Prado::using('System.Web.UI.WebControls.TReCaptcha2');
+
+/**
+ * TReCaptcha2Validator class
+ *
+ * TReCaptcha2Validator validates a reCAPTCHA represented by a {@link TReCaptcha} control.
+ * The input control fails validation if th user did not pass the humanity test.
+ *
+ * To use TReCaptcha2Validator, specify the {@link setCaptchaControl CaptchaControl}
+ * to be the ID path of the {@link TReCaptcha} control.
+ *
+ * @author Cristian Camilo Naranjo Valencia
+ * @package System.Web.UI.WebControls
+ * @since 3.3.1
+ */
+
+class TReCaptcha2Validator extends TBaseValidator
+{
+ protected $_isvalid = null;
+
+ protected function getClientClassName()
+ {
+ return 'Prado.WebUI.TReCaptcha2Validator';
+ }
+ public function getEnableClientScript()
+ {
+ return true;
+ }
+ protected function getCaptchaControl()
+ {
+ $control = $this->getValidationTarget();
+ if (!$control)
+ throw new Exception('No target control specified for TReCaptcha2Validator');
+ if (!($control instanceof TReCaptcha2))
+ throw new Exception('TReCaptcha2Validator only works with TReCaptcha2 controls');
+ return $control;
+ }
+ public function getClientScriptOptions()
+ {
+ $options = parent::getClientScriptOptions();
+ $options['ResponseFieldName'] = $this->getCaptchaControl()->getResponseFieldName();
+ return $options;
+ }
+ /**
+ * This method overrides the parent's implementation.
+ * The validation succeeds if the input control has the same value
+ * as the one displayed in the corresponding RECAPTCHA control.
+ *
+ * @return boolean whether the validation succeeds
+ */
+ protected function evaluateIsValid()
+ {
+ // check validity only once (if trying to evaulate multiple times, all redundant checks would fail)
+ if (is_null($this->_isvalid))
+ {
+ $control = $this->getCaptchaControl();
+ $this->_isvalid = $control->validate();
+ }
+ return ($this->_isvalid==true);
+ }
+ public function onPreRender($param)
+ {
+ parent::onPreRender($param);
+
+ $cs = $this->Page->getClientScript();
+ $cs->registerPradoScript('validator');
+
+ // communicate validation status to the client side
+ $value = $this->_isvalid===false ? '0' : '1';
+ $cs->registerHiddenField($this->getClientID().'_1',$value);
+
+ // update validator display
+ if ($control = $this->getValidationTarget())
+ {
+ $fn = 'captchaUpdateValidatorStatus_'.$this->getClientID();
+
+ $cs->registerEndScript($this->getClientID().'::validate', implode(' ',array(
+ // this function will be used to update the validator
+ 'function '.$fn.'(valid)',
+ '{',
+ ' jQuery('.TJavaScript::quoteString('#'.$this->getClientID().'_1').').val(valid);',
+ ' Prado.Validation.validateControl('.TJavaScript::quoteString($control->ClientID).'); ',
+ '}',
+ '',
+ // update the validator to the result if we're in a callback
+ // (if we're in initial rendering or a postback then the result will be rendered directly to the page html anyway)
+ $this->Page->IsCallback ? $fn.'('.$value.');' : '',
+ '',
+ // install event handler that clears the validation error when user changes the captcha response field
+ 'jQuery("#'.$control->getClientID().'").on("change", '.TJavaScript::quoteString('#'.$control->getResponseFieldName()).', function() { ',
+ $fn.'("1");',
+ '});',
+ )));
+ }
+ }
+}
+