Created
April 22, 2010 12:15
-
-
Save tbje/375145 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<lift:surround with="default" at="content"> | |
<head> | |
<script type="text/javascript" src="http://api.recaptcha.net/js/recaptcha_ajax.js" /> | |
<script type="text/javascript"> | |
function create(){ | |
Recaptcha.create("your public key", "recaptcha_div", { | |
theme: "clean"} | |
); | |
} | |
function setHidden(){ | |
document.getElementById('challenge').value=Recaptcha.get_challenge() | |
document.getElementById('response').value=Recaptcha.get_response() | |
} | |
</script> | |
</head> | |
<lift:CaptchaSnippet:form form="POST" onsubmit="setHidden()"> | |
<lift:msgs/> | |
<x:email /> | |
<x:challenge id="challenge"/> | |
<x:response id="response"/> | |
<div name="recaptcha_div" id="recaptcha_div" /> | |
<script>create()</script> | |
<x:submit/> | |
</lift:CaptchaSnippet:form> | |
</lift:surround> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import net.liftweb.http._ | |
import net.liftweb.http.SHtml._ | |
import net.liftweb.util._ | |
import net.liftweb.util.Helpers._ | |
import net.liftweb.common._ | |
import java.net.{HttpURLConnection, MalformedURLException, URL, URLEncoder} | |
import java.io.{BufferedReader, InputStreamReader, IOException, OutputStreamWriter} | |
import scala.xml._ | |
class CaptchaSnippet extends StatefulSnippet with Logger { | |
val privateKey = "your private key" | |
val dispatch: DispatchIt = { | |
case "form" => form _ | |
} | |
var email = "Default value" | |
def form(html: NodeSeq) : NodeSeq = { | |
var challenge = "" | |
var response = "" | |
bind("x", html, | |
"email" -> text(email, (x:String) => email=x), | |
"challenge" -%> hidden((x:String)=>challenge=x, ""), | |
"response" -%> hidden((x:String)=>response=x, ""), | |
"submit" -> submit("Send", () => { | |
if (verifyCaptcha(challenge, response)) { | |
submitAction(email) | |
} else { | |
S.error("Recaptcha input not correct")) | |
} | |
} | |
) | |
} | |
def submitAction(email: String) { /*processing*/ } | |
def verifyCaptcha(challenge: String, response: String) : Boolean = { | |
val url = new URL("http://api-verify.recaptcha.net/verify") | |
val con = url.openConnection.asInstanceOf[HttpURLConnection] | |
con.setDoOutput(true) | |
con.setRequestMethod("POST") | |
val writer = new OutputStreamWriter(con.getOutputStream) | |
writer.write("privatekey=" + privateKey) | |
writer.write("&remoteip=" + S.containerRequest.map(_.remoteAddress) | |
.openOr("localhost")) | |
writer.write("&challenge=" + challenge) | |
writer.write("&response=" + response) | |
writer.close | |
if (con.getResponseCode == HttpURLConnection.HTTP_OK) { | |
val reader = new BufferedReader( | |
new InputStreamReader(con.getInputStream)) | |
val res : Boolean = reader.readLine match { | |
case "true" => true | |
case _ => false | |
} | |
reader.close | |
res | |
} else { | |
error("Connection problem to reCaptcha") | |
false | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<script type="text/javascript" | |
src="http://api.recaptcha.net/challenge?k=<your_public_key>"> | |
</script> | |
<noscript> | |
<iframe src="http://api.recaptcha.net/noscript?k=<your_public_key>" | |
height="300" width="500" frameborder="0"></iframe><br> | |
<textarea name="recaptcha_challenge_field" rows="3" cols="40"> | |
</textarea> | |
<input type="hidden" name="recaptcha_response_field" | |
value="manual_challenge"> | |
</noscript> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment