forked from ambethia/recaptcha
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Removed Ambethia namespace, and restructured classes a bit
- Loading branch information
Jason L Perry
committed
Sep 14, 2009
1 parent
4de223b
commit bfc5134
Showing
10 changed files
with
114 additions
and
113 deletions.
There are no files selected for viewing
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,2 @@ | ||
require 'recaptcha.rb' | ||
ActionView::Base.send :include, Ambethia::ReCaptcha::Helper | ||
ActionController::Base.send :include, Ambethia::ReCaptcha::Controller | ||
require 'recaptcha' | ||
require 'recaptcha/rails' |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,13 @@ | ||
require 'recaptcha/recaptcha' | ||
ActionView::Base.send :include, Ambethia::ReCaptcha::Helper | ||
ActionController::Base.send :include, Ambethia::ReCaptcha::Controller | ||
require 'recaptcha/client_helper' | ||
require 'recaptcha/verify' | ||
|
||
module Recaptcha | ||
RECAPTCHA_API_SERVER = 'http://api.recaptcha.net'; | ||
RECAPTCHA_API_SECURE_SERVER = 'https://api-secure.recaptcha.net'; | ||
RECAPTCHA_VERIFY_SERVER = 'api-verify.recaptcha.net'; | ||
|
||
SKIP_VERIFY_ENV = ['test', 'cucumber'] | ||
|
||
class RecaptchaError < StandardError | ||
end | ||
end |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
module Recaptcha | ||
module ClientHelper | ||
# Your public API can be specified in the +options+ hash or preferably | ||
# the environment variable +RECAPTCHA_PUBLIC_KEY+. | ||
def recaptcha_tags(options = {}) | ||
# Default options | ||
key = options[:public_key] ||= ENV['RECAPTCHA_PUBLIC_KEY'] | ||
error = options[:error] ||= session[:recaptcha_error] | ||
uri = options[:ssl] ? RECAPTCHA_API_SECURE_SERVER : RECAPTCHA_API_SERVER | ||
html = "" | ||
if options[:display] | ||
html << %{<script type="text/javascript">\n} | ||
html << %{ var RecaptchaOptions = #{options[:display].to_json};\n} | ||
html << %{</script>\n} | ||
end | ||
if options[:ajax] | ||
html << %{<div id="dynamic_recaptcha"></div>} | ||
html << %{<script type="text/javascript" src="#{uri}/js/recaptcha_ajax.js"></script>\n} | ||
html << %{<script type="text/javascript">\n} | ||
html << %{ Recaptcha.create('#{key}', document.getElementById('dynamic_recaptcha')#{options[:display] ? '' : ',RecaptchaOptions'});} | ||
html << %{</script>\n} | ||
else | ||
html << %{<script type="text/javascript" src="#{uri}/challenge?k=#{key}} | ||
html << %{#{error ? "&error=#{CGI::escape(error)}" : ""}"></script>\n} | ||
unless options[:noscript] == false | ||
html << %{<noscript>\n } | ||
html << %{<iframe src="#{uri}/noscript?k=#{key}" } | ||
html << %{height="#{options[:iframe_height] ||= 300}" } | ||
html << %{width="#{options[:iframe_width] ||= 500}" } | ||
html << %{frameborder="0"></iframe><br/>\n } | ||
html << %{<textarea name="recaptcha_challenge_field" } | ||
html << %{rows="#{options[:textarea_rows] ||= 3}" } | ||
html << %{cols="#{options[:textarea_cols] ||= 40}"></textarea>\n } | ||
html << %{<input type="hidden" name="recaptcha_response_field" value="manual_challenge">} | ||
html << %{</noscript>\n} | ||
end | ||
end | ||
raise RecaptchaError, "No public key specified." unless key | ||
return html | ||
end # recaptcha_tags | ||
end # ClientHelper | ||
end # Recaptcha |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
ActionView::Base.send(:include, Recaptcha::ClientHelper) | ||
ActionController::Base.send(:include, Recaptcha::Verify) |
This file was deleted.
Oops, something went wrong.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
module Recaptcha | ||
module Verify | ||
# Your private API can be specified in the +options+ hash or preferably | ||
# the environment variable +RECAPTCHA_PUBLIC_KEY+. | ||
def verify_recaptcha(options = {}) | ||
return true if SKIP_VERIFY_ENV.include? ENV['RAILS_ENV'] | ||
model = options.is_a?(Hash)? options[:model] : options | ||
private_key = options[:private_key] if options.is_a?(Hash) | ||
private_key ||= ENV['RECAPTCHA_PRIVATE_KEY'] | ||
raise RecaptchaError, "No private key specified." unless private_key | ||
begin | ||
recaptcha = Net::HTTP.post_form URI.parse("http://#{RECAPTCHA_VERIFY_SERVER}/verify"), { | ||
"privatekey" => private_key, | ||
"remoteip" => request.remote_ip, | ||
"challenge" => params[:recaptcha_challenge_field], | ||
"response" => params[:recaptcha_response_field] | ||
} | ||
answer, error = recaptcha.body.split.map { |s| s.chomp } | ||
unless answer == 'true' | ||
session[:recaptcha_error] = error | ||
if model | ||
model.valid? | ||
if Rails::VERSION::MAJOR == 2 and Rails::VERSION::MINOR >= 2 | ||
model.errors.add :base, I18n.translate("#{model.class.name.underscore}.captcha", :scope => %w(errors models), :default => (options[:message] || "Captcha response is incorrect, please try again.")) | ||
else | ||
model.errors.add :base, options[:message] || "Captcha response is incorrect, please try again." | ||
end | ||
end | ||
return false | ||
else | ||
session[:recaptcha_error] = nil | ||
return true | ||
end | ||
rescue Exception => e | ||
raise RecaptchaError, e | ||
end | ||
end # verify_recaptcha | ||
end # Verify | ||
end # Recaptcha |
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
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