From b5f4be7ea0b8bdf220297893a907fe59c017fbd5 Mon Sep 17 00:00:00 2001 From: ctrlaltca <> Date: Sun, 6 Jan 2013 18:17:30 +0000 Subject: backported r3236 to trunk/ --- 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