package com.exanimo.security.recaptcha { import com.exanimo.security.recaptcha.RecaptchaUtil; import flash.external.ExternalInterface; import flash.display.DisplayObject; import flash.display.Loader; import flash.display.LoaderInfo; import flash.display.Sprite; import flash.events.SecurityErrorEvent; import flash.net.URLRequest; import flash.system.LoaderContext; import flash.utils.Dictionary; /** * * Because the RecaptchaLoader communicates with JavaScript, extra care * must be taken to insure that everything is going smoothly. Check * RecaptchaLoader.available before trying to load reCAPTCHA images, put * calls to load() in a try block in case there is a JavaScript error, * and listen for error events. * * @langversion ActionScript 3 * @playerversion Flash 9.0.0 * * @author Matthew Tretter (matthew@exanimo.com) * @since 2008.03.11 * */ public class RecaptchaLoader extends Sprite { private var _challenge:String; private var _context:LoaderContext; private var _loader:Loader; private var _publicKey:String; private var _url:String; /** * * */ public function RecaptchaLoader() { // Wrap a loader. this._loader = new Loader(); this.addChild(this._loader); } // // accessors // /** * * * */ public static function get available():Boolean { return RecaptchaUtil.available; } /** * * * */ public function get challenge():String { return this._challenge; } /** * * * */ public function get content():DisplayObject { return this._loader.content; } /** * * * */ public function get contentLoaderInfo():LoaderInfo { return this._loader.contentLoaderInfo; } /** * * * */ public function get publicKey():String { return this._publicKey || RecaptchaUtil.defaultPublicKey; } public function set publicKey(publicKey:String):void { this._publicKey = publicKey; } // // public methods // /** * * * */ public function close():void { this._loader.close(); } /** * * * */ public function load(context:LoaderContext = null):void { if (!this.publicKey) { throw new ArgumentError('You must set a public key before calling RecaptchaLoader.load().'); } else if (!RecaptchaLoader.available) { if (!ExternalInterface.objectID) { throw new Error('RecaptchaLoader is unavailable: the SWF object does not have an ID in the html.'); } else { throw new Error('RecaptchaLoader is unavailable.'); } } this._context = context; var success:Boolean = RecaptchaUtil.requestImageData(this._dataCompleteHandler, this.publicKey); if (!success) { throw new Error('There was an error obtaining the reCAPTCHA data.'); } } /** * * * */ public function unload():void { this._loader.unload(); } // // private methods // /** * * * */ private function _dataCompleteHandler(data:Object):void { this._challenge = data.challenge; this._url = data.url; // Convert errors to error events. try { this._loader.load(new URLRequest(this._url), this._context); } catch (error:SecurityError) { this.dispatchEvent(new SecurityErrorEvent(SecurityErrorEvent.SECURITY_ERROR, false, false, error.message)); } } } }