diff --git a/Gemfile b/Gemfile index 481cd1c..7d9d2e3 100644 --- a/Gemfile +++ b/Gemfile @@ -58,7 +58,7 @@ end # Use unicorn as the app server gem "unicorn" - +gem 'curb' # Use debugger # gem 'debugger', group: [:development, :test] diff --git a/Gemfile.lock b/Gemfile.lock index dbd0166..3ede1a5 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -57,6 +57,7 @@ GEM commonjs (0.2.7) concurrent-ruby (1.1.8) crass (1.0.6) + curb (0.9.11) erubis (2.7.0) execjs (2.8.1) formtastic (3.1.5) @@ -208,6 +209,7 @@ DEPENDENCIES bcrypt-ruby (~> 3.1.2) carrierwave coffee-rails (~> 4.0.0) + curb formtastic formtastic-bootstrap haml-rails diff --git a/app/controllers/public/contacts_controller.rb b/app/controllers/public/contacts_controller.rb index 42a84a7..88e8888 100644 --- a/app/controllers/public/contacts_controller.rb +++ b/app/controllers/public/contacts_controller.rb @@ -11,16 +11,46 @@ class Public::ContactsController < ApplicationController def create @contact = Contact.new(params.require(:contact).permit(:website, :place, :tel, :name, :email, :message)) - - if @contact.save - QuestionMailer.send_contact(@contact).deliver - QuestionMailer.remerciement(@contact).deliver - render :action => :create - else + - render :action => :new - end + if @contact.save + if valid_captcha?(params['g-recaptcha-response']) + QuestionMailer.send_contact(@contact).deliver + QuestionMailer.remerciement(@contact).deliver + render :action => :create + + else + render :action => :captcha + end + else + + render :action => :new + end + end + + + def valid_captcha?(recaptcha_response) + return true if Rails.env.test? + + + @c = Curl::Easy.new("https://www.google.com/recaptcha/api/siteverify") do |curl| + curl.verbose = true + end + + + @c.http_post( + Curl::PostField.content(:secret, RECAPTCHA_SECRET_KEY), + Curl::PostField.content(:response, recaptcha_response) + + ) + + @debug = JSON.parse(@c.body_str) #["successe"] + + return @debug["success"] + + + end end diff --git a/app/views/public/contacts/_form.html.haml b/app/views/public/contacts/_form.html.haml index 71648d5..76b2c7d 100644 --- a/app/views/public/contacts/_form.html.haml +++ b/app/views/public/contacts/_form.html.haml @@ -6,4 +6,23 @@ =f.input :tel, :label => "Téléphone :" =f.input :place, :label => "Lieu de votre projet :" =f.input :message, :label => "Message :" - =f.submit "Envoyer", :class => "btn" \ No newline at end of file + + + =raw '
' + + =f.submit "Envoyer", :class => "btn" + + :javascript + $("form").validationEngine(); + + $("form").submit(function(event) { + + var recaptcha = $("#g-recaptcha-response").val(); + if (recaptcha === "") { + event.preventDefault(); + alert("Merci de cliquer sur la case 'Je ne suis pas un robot.'"); + } + }); + + + \ No newline at end of file diff --git a/app/views/public/contacts/captcha.js.erb b/app/views/public/contacts/captcha.js.erb new file mode 100644 index 0000000..389993b --- /dev/null +++ b/app/views/public/contacts/captcha.js.erb @@ -0,0 +1 @@ +$("#form").append("Captcha invalide"); \ No newline at end of file diff --git a/config/environments/development.rb b/config/environments/development.rb index 4eef868..5f25f30 100644 --- a/config/environments/development.rb +++ b/config/environments/development.rb @@ -36,4 +36,8 @@ CMSnb::Application.configure do config.action_mailer.default_url_options = { :host => 'localhost:3000' } + + RECAPTCHA_SITE_KEY = '6Ldj6LQUAAAAACY6SmePKeUcpbcY9WVD47-u8l_s' + RECAPTCHA_SECRET_KEY = '6Ldj6LQUAAAAAO-11feITBPmDATAF--NqcXh5_WP' + end diff --git a/config/environments/production.rb b/config/environments/production.rb index dbf6359..5cd8015 100644 --- a/config/environments/production.rb +++ b/config/environments/production.rb @@ -87,4 +87,7 @@ CMSnb::Application.configure do config.action_mailer.delivery_method = :sendmail + RECAPTCHA_SITE_KEY = '6Ldj6LQUAAAAACY6SmePKeUcpbcY9WVD47-u8l_s' + RECAPTCHA_SECRET_KEY = '6Ldj6LQUAAAAAO-11feITBPmDATAF--NqcXh5_WP' + end