From df6d3d96f93de99150b28d342bfeb0c89bf59f1b Mon Sep 17 00:00:00 2001 From: ctrlaltca <> Date: Sun, 6 Jan 2013 18:16:16 +0000 Subject: fix #431 --- framework/Web/UI/WebControls/TReCaptcha.php | 66 ++++++++++++++++++++++------- 1 file changed, 51 insertions(+), 15 deletions(-) (limited to 'framework/Web/UI/WebControls/TReCaptcha.php') diff --git a/framework/Web/UI/WebControls/TReCaptcha.php b/framework/Web/UI/WebControls/TReCaptcha.php index b63edad8..c6cf4185 100644 --- a/framework/Web/UI/WebControls/TReCaptcha.php +++ b/framework/Web/UI/WebControls/TReCaptcha.php @@ -135,6 +135,16 @@ class TReCaptcha extends TWebControl implements IValidatable return $this->setViewState('Language', TPropertyValue::ensureString($value)); } + public function getCallbackScript() + { + return $this->getViewState('CallbackScript'); + } + + public function setCallbackScript($value) + { + return $this->setViewState('CallbackScript', TPropertyValue::ensureString($value)); + } + protected function getChallengeFieldName() { return /*$this->ClientID.'_'.*/self::ChallengeFieldName; @@ -212,21 +222,47 @@ class TReCaptcha extends TWebControl implements IValidatable public function renderContents($writer) { - $writer->write(TJavaScript::renderScriptBlock( - 'var RecaptchaOptions = '.TJavaScript::jsonEncode($this->getClientSideOptions()).';' - )); - - $html = recaptcha_get_html($this->getPublicKey()); - /* - reCAPTCHA currently does not support multiple validations per page - $html = str_replace( - array(self::ChallengeFieldName,self::ResponseFieldName), - array($this->getChallengeFieldName(),$this->getResponseFieldName()), - $html - ); - */ - $writer->write($html); + $readyscript = 'Event.fire(document, '.TJavaScript::quoteString('captchaready:'.$this->getClientID()).')'; + $cs = $this->Page->ClientScript; + $id = $this->getClientID(); + $divid = $id.'_1_recaptchadiv'; + $writer->write('
'); + + if (!$this->Page->IsCallback) + { + $writer->write(TJavaScript::renderScriptBlock( + 'var RecaptchaOptions = '.TJavaScript::jsonEncode($this->getClientSideOptions()).';' + )); + + $html = recaptcha_get_html($this->getPublicKey()); + /* + reCAPTCHA currently does not support multiple validations per page + $html = str_replace( + array(self::ChallengeFieldName,self::ResponseFieldName), + array($this->getChallengeFieldName(),$this->getResponseFieldName()), + $html + ); + */ + $writer->write($html); + + $cs->registerEndScript('ReCaptcha::EventScript', 'Event.observe(document, "dom:loaded", function() { '.$readyscript.'; } );'); + } + else + { + $options = $this->getClientSideOptions(); + $options['callback'] = new TJavaScriptLiteral('function() { '.$readyscript.'; '.$this->getCallbackScript().'; }'); + $cs->registerScriptFile('ReCaptcha::AjaxScript', 'http://www.google.com/recaptcha/api/js/recaptcha_ajax.js'); + $cs->registerEndScript('ReCaptcha::CreateScript::'.$id, implode(' ', array( + 'if (!$('.TJavaScript::quoteString($this->getResponseFieldName()).'))', + 'Recaptcha.create(', + TJavaScript::quoteString($this->getPublicKey()).', ', + TJavaScript::quoteString($divid).', ', + TJavaScript::encode($options), + ');', + ))); + } + + $writer->write('
'); } } - -- cgit v1.2.3