Permalink
Browse files

contact temp 2 captcha

  • Loading branch information...
1 parent d13cebf commit a124dd128dd784663c628b32f2cd5ff97a998661 @flurdy committed Oct 25, 2012
Showing with 45 additions and 24 deletions.
  1. +34 −21 app/controllers/Application.scala
  2. +9 −2 app/views/contact.scala.html
  3. +2 −1 project/Build.scala
@@ -8,6 +8,7 @@ import models._
import notifiers._
import java.math.BigInteger
import java.security.SecureRandom
+import dispatch._
object Application extends Controller with Secured{
@@ -146,42 +147,54 @@ object Application extends Controller with Secured{
}
private def generateCaptchaId : String = {
- new BigInteger(130, new SecureRandom()).toString(16)
- }
-
- private def findCaptchaId : String = { implicit session : Session =>
- session.get("capchaId").getOrElse(generateCaptchaId)
+ new BigInteger(130, new SecureRandom()).toString(16).substring(0,8)
}
- private def isSameAsCaptcha(formId:String,captchaId:String) : Boolean = {
- false
+ private def isSameAsCaptcha(formCaptcha:String,captchaId:String) : Boolean = {
+ Logger.debug("Form id %s | captchaId %s ".format(formCaptcha,captchaId))
+ val capthcaUrlString = "http://captchator.com/captcha/check_answer/%s/%s".format(captchaId,formCaptcha)
+ val capthcaUrl = url(capthcaUrlString)
+ Logger.debug("capthcaUrl was %s".format(capthcaUrlString))
+ val response = Http(capthcaUrl OK as.String)
+ val actualResponse = response()
+ Logger.debug("Response was %s".format(actualResponse))
+ actualResponse.trim == "1"
}
def contact = Action { implicit request =>
val captchaId = generateCaptchaId
- Ok(views.html.contact(contactForm,captchaId)).withSession(session + "captchaId"->captchaId)
+ Ok(views.html.contact(contactForm,captchaId)).withSession( "captchaid" -> captchaId)
}
def sendContact = Action { implicit request =>
contactForm.bindFromRequest.fold(
errors => {
- Logger.warn("Registration failed: " + errors)
+ Logger.warn("Contact failed: " + errors)
val captchaId = generateCaptchaId
- BadRequest(views.html.contact(errors,captchaId)).withSession(session + "captchaId"->captchaId)
+ BadRequest(views.html.contact(errors,captchaId)).withSession( "captchaid" -> captchaId)
},
contactFields => {
-
- if( isSameAsCaptcha( contactFields._6.trim , findCaptchaId ) ){
-
- EmailAlerter.sendContactMessage(contactFields._1, contactFields._2, contactFields._3, contactFields._4, contactFields._5, findCurrentRecipient)
-
- Redirect(routes.Application.index()).flashing("message"->"Your message was sent")
-
- } else {
- val captchaId = generateCaptchaId
- BadRequest(views.html.contact(contactForm.fill(contactFields),captchaId)).flashing("messageError"->"Try another validation").withSession(session + "captchaId"->captchaId)
+ Logger.warn("CAPTCHA " + contactFields._6.trim)
+ request.session.get("captchaid") match {
+ case Some(captchaId) => {
+ if( isSameAsCaptcha( contactFields._6.trim, captchaId ) ){
+
+ EmailAlerter.sendContactMessage(contactFields._1, contactFields._2, contactFields._3, contactFields._4, contactFields._5, findCurrentRecipient)
+
+ Redirect(routes.Application.index()).flashing("message"->"Your message was sent").withSession(session - "captchaid")
+
+ } else {
+ Logger.warn("Contact captcha failed")
+ val captchaId = generateCaptchaId
+ BadRequest(views.html.contact(contactForm.fill(contactFields),captchaId,Some("Try another validation"))).withSession(session-"captchaid").withSession( "captchaid"-> generateCaptchaId)
+ }
+ }
+ case None => {
+ Logger.warn("Contact captcha not in session")
+ val captchaId = generateCaptchaId
+ BadRequest(views.html.contact(contactForm.fill(contactFields),captchaId,Some("Try another validation"))).withSession("captchaid" -> generateCaptchaId)
+ }
}
-
}
)
}
@@ -1,4 +1,4 @@
-@(contactForm:Form[(String,String,Option[String],Option[String],String,String)],captchaId:String)(implicit flash:Flash, currentRecipient: Option[Recipient], analyticsDetails: Option[String])
+@(contactForm:Form[(String,String,Option[String],Option[String],String,String)],captchaId:String,captchaError:Option[String] = None)(implicit flash:Flash, currentRecipient: Option[Recipient], analyticsDetails: Option[String])
@import helper._
@@ -29,6 +29,13 @@
</p>
}
+ @captchaError.map { error =>
+ <p class="alert alert-error">
+ <button type="button" class="close" data-dismiss="alert">×</button>
+ Please try another validation entry
+ </p>
+ }
+
@form(action = routes.Application.sendContact, 'class -> "form-horizontal"){
<div class="well">
@@ -79,7 +86,7 @@
</div>
<div class="control-group">
<label class="control-label" for="contactValidation">Entry</label>
- <div class="controls">
+ <div class="controls">@captchaId
<input type="text" class="input-xlarge" id="contactValidation" name="validation" value="" maxlength="20" placeholder="Copy the text above"/>
</div>
</div>
View
@@ -11,7 +11,8 @@ object ApplicationBuild extends Build {
"postgresql" % "postgresql" % "9.1-901-1.jdbc4",
"org.mindrot" % "jbcrypt" % "0.3m",
"com.typesafe" %% "play-plugins-mailer" % "2.0.4",
- "com.andersen-gott" %% "scravatar" % "1.0.1"
+ "com.andersen-gott" %% "scravatar" % "1.0.1",
+ "net.databinder.dispatch" %% "dispatch-core" % "0.9.2"
)
val main = PlayProject(appName, appVersion, appDependencies, mainLang = SCALA).settings(

0 comments on commit a124dd1

Please sign in to comment.