From fb699ea195a5ed9f51b729ac681aa0d6104addee Mon Sep 17 00:00:00 2001 From: Rodrigo Souto Date: Tue, 22 Jan 2013 23:04:17 +0000 Subject: [PATCH] Adding honeypot plugin --- vendor/plugins/honeypot/init.rb | 18 ++++++++++++++++++ vendor/plugins/honeypot/lib/form_tag_helper.rb | 36 ++++++++++++++++++++++++++++++++++++ 2 files changed, 54 insertions(+), 0 deletions(-) create mode 100644 vendor/plugins/honeypot/init.rb create mode 100644 vendor/plugins/honeypot/lib/form_tag_helper.rb diff --git a/vendor/plugins/honeypot/init.rb b/vendor/plugins/honeypot/init.rb new file mode 100644 index 0000000..994ec92 --- /dev/null +++ b/vendor/plugins/honeypot/init.rb @@ -0,0 +1,18 @@ +# Inpired on https://github.com/curtis/honeypot-captcha +require File.join(File.dirname(__FILE__), 'lib', 'form_tag_helper') + +module Honeypot + def honeypot_fields + { :honeypot => _('Do not fill in this field') } + end + + def protect_from_bots + head :ok if honeypot_fields.any? { |f,l| !params[f].blank? } + end + + def self.included(base) + base.send :helper_method, :honeypot_fields + end +end + +ActionController::Base.send(:include, Honeypot) if defined?(ActionController::Base) diff --git a/vendor/plugins/honeypot/lib/form_tag_helper.rb b/vendor/plugins/honeypot/lib/form_tag_helper.rb new file mode 100644 index 0000000..d9b4217 --- /dev/null +++ b/vendor/plugins/honeypot/lib/form_tag_helper.rb @@ -0,0 +1,36 @@ +module ActionView + module Helpers + module FormTagHelper + def form_tag_with_honeypot(url_for_options = {}, options = {}, *parameters_for_url, &block) + honeypot = options.delete(:honeypot) + html = form_tag_without_honeypot(url_for_options, options, *parameters_for_url, &block) + if honeypot + captcha = "".respond_to?(:html_safe) ? honey_pot_captcha.html_safe : honey_pot_captcha + if block_given? + html.insert(html.index(''), captcha) + else + html += captcha + end + end + html + end + alias_method_chain :form_tag, :honeypot + + private + + def honey_pot_captcha + html_ids = [] + honeypot_fields.collect do |f, l| + html_ids << (html_id = "#{f}_hp_#{Time.now.to_i}") + content_tag :div, :id => html_id do + content_tag(:style, :type => 'text/css', :media => 'screen', :scoped => "scoped") do + "#{html_ids.map { |i| "##{i}" }.join(', ')} { display:none; }" + end + + label_tag(f, l) + + send([:text_field_tag, :text_area_tag][rand(2)], f) + end + end.join + end + end + end +end -- libgit2 0.21.2