package com.exanimo.security.recaptcha { import inky.media.AudioPlayer; import com.exanimo.security.recaptcha.RecaptchaLoader; import com.exanimo.security.recaptcha.RecaptchaMode; import com.exanimo.security.recaptcha.RecaptchaWidgetEvent; import com.exanimo.security.recaptcha.RecaptchaUtil; import flash.display.*; import flash.events.Event; import flash.events.MouseEvent; import flash.text.TextField; /** * * .. * * @langversion ActionScript 3 * @playerversion Flash 9.0.0 * * @author Matthew Tretter (matthew@exanimo.com) * @since 2008.03.14 * */ public class BaseRecaptchaWidget extends Sprite { private var _challenge:String; private var _loader:RecaptchaLoader; private var _mode:String; private var _publicKey:String; private var _audioPlayer:AudioPlayer; private var _url:String; private var __audioPrompt:DisplayObject; private var __helpButton:InteractiveObject; private var __imagePrompt:DisplayObject; private var __modeButton:InteractiveObject; private var __refreshButton:InteractiveObject; private var __responseField:TextField; /** * * */ public function BaseRecaptchaWidget() { this.__helpButton = this.getChildByName('_helpButton') as InteractiveObject; this.__responseField = this.getChildByName('_responseField') as TextField; this.__modeButton = this.getChildByName('_modeButton') as InteractiveObject; this.__refreshButton = this.getChildByName('_refreshButton') as InteractiveObject; this.__audioPrompt = this.getChildByName('_audioPrompt'); this.__imagePrompt = this.getChildByName('_imagePrompt'); // Initialize the buttons. this.__helpButton && this.__helpButton.addEventListener(MouseEvent.CLICK, this._helpButtonClickHandler); this.__modeButton && this.__modeButton.addEventListener(MouseEvent.CLICK, this._modeButtonClickHandler); this.__refreshButton && this.__refreshButton.addEventListener(MouseEvent.CLICK, this._refreshButtonClickHandler); this.mode = RecaptchaMode.IMAGE; this._load(); } // // accessors // /** * * */ public function get available():Boolean { return RecaptchaUtil.available; } /** * * */ public function get challenge():String { return this._challenge; } /** * * */ public function get mode():String { return this._mode; } public function set mode(mode:String):void { if (mode != this._mode) { if (this.__audioPrompt) { this.__audioPrompt.visible = mode == RecaptchaMode.AUDIO; } if (this.__imagePrompt) { this.__imagePrompt.visible = mode == RecaptchaMode.IMAGE; } this._mode = mode; this._load(); this.dispatchEvent(new RecaptchaWidgetEvent(RecaptchaWidgetEvent.MODE_CHANGE, false, false)); } } /** * * */ public function get modeButton():InteractiveObject { return this.__modeButton; } /** * * */ public function get publicKey():String { return this._publicKey || RecaptchaUtil.defaultPublicKey; } public function set publicKey(publicKey:String):void { this._publicKey = publicKey; this._load(); } /** * * */ public function get response():String { return this.__responseField ? this.__responseField.text : ''; } /** * * */ public function get url():String { return this._url; } // // protected methods // /** * * * */ protected function addRecaptchaImage(image:DisplayObject):void { this.addChild(image); } // // private methods // /** * * */ private function _helpButtonClickHandler(e:MouseEvent):void { this.dispatchEvent(new RecaptchaWidgetEvent(RecaptchaWidgetEvent.HELP, false, false)); } /** * * */ private function _load():void { if (!this.publicKey) return; this._challenge = null; this._url = null; if (this._audioPlayer) { this._audioPlayer.stop(); } if (this._loader && this._loader.parent) { this._loader.parent.removeChild(this._loader); this._loader.contentLoaderInfo.removeEventListener(Event.COMPLETE, this._loaderCompleteHandler); } if (this.mode == RecaptchaMode.IMAGE) { this._loader = new RecaptchaLoader(); this._loader.contentLoaderInfo.addEventListener(Event.COMPLETE, this._loaderCompleteHandler); this._loader.publicKey = this.publicKey; this._loader.load(); this.addRecaptchaImage(this._loader as DisplayObject); } else if (this.mode == RecaptchaMode.AUDIO) { RecaptchaUtil.requestAudioData(this._soundDataCompleteHandler, this.publicKey); } } /** * * */ private function _loaderCompleteHandler(e:Event):void { if (this.mode != RecaptchaMode.IMAGE) return; this._challenge = this._loader.challenge; this._url = this._loader.contentLoaderInfo.url; this.dispatchEvent(new RecaptchaWidgetEvent(RecaptchaWidgetEvent.READY, false, false)); } /** * * */ private function _modeButtonClickHandler(e:MouseEvent):void { this.mode = this.mode == RecaptchaMode.IMAGE ? RecaptchaMode.AUDIO : RecaptchaMode.IMAGE; } /** * * */ private function _refreshButtonClickHandler(e:MouseEvent):void { this._load(); this.dispatchEvent(new RecaptchaWidgetEvent(RecaptchaWidgetEvent.REFRESH, false, false)); } /** * * */ private function _soundDataCompleteHandler(data:Object):void { if (this.mode != RecaptchaMode.AUDIO) return; this._challenge = data.challenge; this._url = data.url; this._audioPlayer = new AudioPlayer(); this._audioPlayer.play(data.url); this.dispatchEvent(new RecaptchaWidgetEvent(RecaptchaWidgetEvent.READY, false, false)); } } }