Commit fb699ea195a5ed9f51b729ac681aa0d6104addee
1 parent
96fa5dd4
Exists in
master
and in
29 other branches
Adding honeypot plugin
Showing
2 changed files
with
54 additions
and
0 deletions
Show diff stats
@@ -0,0 +1,18 @@ | @@ -0,0 +1,18 @@ | ||
1 | +# Inpired on https://github.com/curtis/honeypot-captcha | ||
2 | +require File.join(File.dirname(__FILE__), 'lib', 'form_tag_helper') | ||
3 | + | ||
4 | +module Honeypot | ||
5 | + def honeypot_fields | ||
6 | + { :honeypot => _('Do not fill in this field') } | ||
7 | + end | ||
8 | + | ||
9 | + def protect_from_bots | ||
10 | + head :ok if honeypot_fields.any? { |f,l| !params[f].blank? } | ||
11 | + end | ||
12 | + | ||
13 | + def self.included(base) | ||
14 | + base.send :helper_method, :honeypot_fields | ||
15 | + end | ||
16 | +end | ||
17 | + | ||
18 | +ActionController::Base.send(:include, Honeypot) if defined?(ActionController::Base) |
@@ -0,0 +1,36 @@ | @@ -0,0 +1,36 @@ | ||
1 | +module ActionView | ||
2 | + module Helpers | ||
3 | + module FormTagHelper | ||
4 | + def form_tag_with_honeypot(url_for_options = {}, options = {}, *parameters_for_url, &block) | ||
5 | + honeypot = options.delete(:honeypot) | ||
6 | + html = form_tag_without_honeypot(url_for_options, options, *parameters_for_url, &block) | ||
7 | + if honeypot | ||
8 | + captcha = "".respond_to?(:html_safe) ? honey_pot_captcha.html_safe : honey_pot_captcha | ||
9 | + if block_given? | ||
10 | + html.insert(html.index('</form>'), captcha) | ||
11 | + else | ||
12 | + html += captcha | ||
13 | + end | ||
14 | + end | ||
15 | + html | ||
16 | + end | ||
17 | + alias_method_chain :form_tag, :honeypot | ||
18 | + | ||
19 | + private | ||
20 | + | ||
21 | + def honey_pot_captcha | ||
22 | + html_ids = [] | ||
23 | + honeypot_fields.collect do |f, l| | ||
24 | + html_ids << (html_id = "#{f}_hp_#{Time.now.to_i}") | ||
25 | + content_tag :div, :id => html_id do | ||
26 | + content_tag(:style, :type => 'text/css', :media => 'screen', :scoped => "scoped") do | ||
27 | + "#{html_ids.map { |i| "##{i}" }.join(', ')} { display:none; }" | ||
28 | + end + | ||
29 | + label_tag(f, l) + | ||
30 | + send([:text_field_tag, :text_area_tag][rand(2)], f) | ||
31 | + end | ||
32 | + end.join | ||
33 | + end | ||
34 | + end | ||
35 | + end | ||
36 | +end |