RecaptchaTag: ReCaptcha.cfc

File ReCaptcha.cfc, 3.5 KB (added by rocketboots, 3 years ago)

CFC version of tag contributed by Shannon Hicks

Line 
1<cfcomponent displayname="reCAPTCHA CFC" hint="I help you use & consume the reCAPTCHA service">
2        <cfscript>
3                // constants
4                this.CHALLENGE_URL = "http://api.recaptcha.net";
5                this.SSL_CHALLENGE_URL = "https://api-secure.recaptcha.net";
6                this.VERIFY_URL = "http://api-verify.recaptcha.net/verify";
7               
8                // vars
9                VARIABLES.privateKey = "";
10                VARIABLES.publicKey = "";
11        </cfscript>
12       
13        <cffunction name="init" hint="I initialize the reCAPTCHA service" returntype="reCaptcha">
14                <cfargument name="privateKey" type="string" required="true" />
15                <cfargument name="publicKey" type="string" required="true" />
16               
17                <cfset VARIABLES.privateKey = ARGUMENTS.privateKey />
18                <cfset VARIABLES.publicKey = ARGUMENTS.publicKey />
19               
20                <cfreturn this />
21        </cffunction>
22       
23        <cffunction name="render" hint="I display the reCAPTCHA form" output="true" returntype="void">
24                <cfargument name="ssl" type="boolean" default="false" required="true" hint="Set true if form on ssl page to use secured version of reCAPTCHA API and avoid browser complaints. default false" />
25                <cfargument name="theme" type="string" default="red" required="true" hint="red|white|blackgrass default red.  Changes look of reCAPTCHA form field." />
26                <cfargument name="tabIndex" type="numeric" required="false" />
27               
28                <cfif ARGUMENTS.ssl>
29                        <cfset VARIABLES.apiURL = this.SSL_CHALLENGE_URL />
30                <cfelse>
31                        <cfset VARIABLES.apiURL = this.CHALLENGE_URL />
32                </cfif>
33               
34                <cfoutput>
35                <script type="text/javascript">
36                <!--
37                        var RecaptchaOptions = {
38                           theme : '#ARGUMENTS.theme#'
39                           <cfif isDefined("ARGUMENTS.tabIndex")>,tabindex : #ARGUMENTS.tabIndex#</cfif>
40                        };
41                //-->
42                </script>
43                <script type="text/javascript"
44                   src="#VARIABLES.apiURL#/challenge?k=#VARIABLES.publicKey#">
45                </script>
46                <noscript>
47                   <iframe src="#VARIABLES.apiURL#/noscript?k=#VARIABLES.publicKey#"
48                       height="300" width="500" frameborder="0"></iframe><br>
49                   <textarea name="recaptcha_challenge_field" rows="3" cols="40">
50                   </textarea>
51                   <input type="hidden" name="recaptcha_response_field"
52                       value="manual_challenge">
53                </noscript>
54                </cfoutput>
55        </cffunction>
56       
57        <cffunction name="check" returntype="boolean" hint="I check the captcha">
58                <cfargument name="recaptcha_challenge_field" type="string" hint="Pass me the value of FORM.recaptcha_challenge_field" />
59                <cfargument name="recaptcha_response_field" type="string" hint="Pass me the value of FORM.recaptcha_response_field" />
60               
61                <cftry>
62                        <cfhttp url="#this.VERIFY_URL#" method="post" timeout="5" throwonerror="true">
63                                <cfhttpparam type="formfield" name="privatekey" value="#VARIABLES.privateKey#">
64                                <cfhttpparam type="formfield" name="remoteip" value="#cgi.REMOTE_ADDR#">
65                                <cfhttpparam type="formfield" name="challenge" value="#ARGUMENTS.recaptcha_challenge_field#">
66                                <cfhttpparam type="formfield" name="response" value="#ARGUMENTS.recaptcha_response_field#">
67                        </cfhttp>
68                <cfcatch>
69                        <cfthrow  type="RECAPTCHA_NO_SERVICE"
70                                message="recaptcha: unable to contact recaptcha verification service on url '#this.VERIFY_URL#'">
71                </cfcatch>
72                </cftry>
73       
74                <cfset VARIABLES.aResponse = listToArray(cfhttp.fileContent, chr(10))>
75       
76                <cfif VARIABLES.aResponse[1] eq "false" and VARIABLES.aResponse[2] neq "incorrect-captcha-sol">
77                        <cfthrow type="RECAPTCHA_VERIFICATION_FAILURE"
78                                message="recaptcha: the verification service responded with error '#aResponse[2]#'. See http://recaptcha.net/apidocs/captcha/ for error meanings.">
79                </cfif>
80               
81                <cfreturn VARIABLES.aResponse[1] />
82        </cffunction>
83</cfcomponent>