diff options
Diffstat (limited to 'vendor/gems/recaptcha-0.3.1/lib/recaptcha')
5 files changed, 153 insertions, 0 deletions
diff --git a/vendor/gems/recaptcha-0.3.1/lib/recaptcha/client_helper.rb b/vendor/gems/recaptcha-0.3.1/lib/recaptcha/client_helper.rb new file mode 100644 index 000000000..2d54178e1 --- /dev/null +++ b/vendor/gems/recaptcha-0.3.1/lib/recaptcha/client_helper.rb @@ -0,0 +1,42 @@ +module Recaptcha + module ClientHelper + # Your public API can be specified in the +options+ hash or preferably + # using the Configuration. + def recaptcha_tags(options = {}) + # Default options + key = options[:public_key] ||= Recaptcha.configuration.public_key + raise RecaptchaError, "No public key specified." unless key + error = options[:error] ||= (defined? flash ? flash[:recaptcha_error] : "") + uri = Recaptcha.configuration.api_server_url(options[:ssl]) + 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 << %{style="border:none;"></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 + return html.html_safe + end # recaptcha_tags + end # ClientHelper +end # Recaptcha diff --git a/vendor/gems/recaptcha-0.3.1/lib/recaptcha/configuration.rb b/vendor/gems/recaptcha-0.3.1/lib/recaptcha/configuration.rb new file mode 100644 index 000000000..210470a5b --- /dev/null +++ b/vendor/gems/recaptcha-0.3.1/lib/recaptcha/configuration.rb @@ -0,0 +1,52 @@ +module Recaptcha + # This class enables detailed configuration of the recaptcha services. + # + # By calling + # + # Recaptcha.configuration # => instance of Recaptcha::Configuration + # + # or + # Recaptcha.configure do |config| + # config # => instance of Recaptcha::Configuration + # end + # + # you are able to perform configuration updates. + # + # Your are able to customize all attributes listed below. All values have + # sensitive default and will very likely not need to be changed. + # + # Please note that the public and private key for the reCAPTCHA API Access + # have no useful default value. The keys may be set via the Shell enviroment + # or using this configuration. Settings within this configuration always take + # precedence. + # + # Setting the keys with this Configuration + # + # Recaptcha.configure do |config| + # config.public_key = '6Lc6BAAAAAAAAChqRbQZcn_yyyyyyyyyyyyyyyyy' + # config.private_key = '6Lc6BAAAAAAAAKN3DRm6VA_xxxxxxxxxxxxxxxxx' + # end + # + class Configuration + attr_accessor :nonssl_api_server_url, + :ssl_api_server_url, + :verify_url, + :skip_verify_env, + :private_key, + :public_key + + def initialize #:nodoc: + @nonssl_api_server_url = RECAPTCHA_API_SERVER_URL + @ssl_api_server_url = RECAPTCHA_API_SECURE_SERVER_URL + @verify_url = RECAPTCHA_VERIFY_URL + @skip_verify_env = SKIP_VERIFY_ENV + + @private_key = ENV['RECAPTCHA_PRIVATE_KEY'] + @public_key = ENV['RECAPTCHA_PUBLIC_KEY'] + end + + def api_server_url(ssl = false) #:nodoc: + ssl ? ssl_api_server_url : nonssl_api_server_url + end + end +end diff --git a/vendor/gems/recaptcha-0.3.1/lib/recaptcha/merb.rb b/vendor/gems/recaptcha-0.3.1/lib/recaptcha/merb.rb new file mode 100644 index 000000000..ed7b1928f --- /dev/null +++ b/vendor/gems/recaptcha-0.3.1/lib/recaptcha/merb.rb @@ -0,0 +1,4 @@ +require 'recaptcha' + +Merb::GlobalHelpers.send(:include, Recaptcha::ClientHelper) +Merb::Controller.send(:include, Recaptcha::Verify) diff --git a/vendor/gems/recaptcha-0.3.1/lib/recaptcha/rails.rb b/vendor/gems/recaptcha-0.3.1/lib/recaptcha/rails.rb new file mode 100644 index 000000000..08741cfd2 --- /dev/null +++ b/vendor/gems/recaptcha-0.3.1/lib/recaptcha/rails.rb @@ -0,0 +1,4 @@ +require 'recaptcha' + +ActionView::Base.send(:include, Recaptcha::ClientHelper) +ActionController::Base.send(:include, Recaptcha::Verify)
\ No newline at end of file diff --git a/vendor/gems/recaptcha-0.3.1/lib/recaptcha/verify.rb b/vendor/gems/recaptcha-0.3.1/lib/recaptcha/verify.rb new file mode 100644 index 000000000..733ce31be --- /dev/null +++ b/vendor/gems/recaptcha-0.3.1/lib/recaptcha/verify.rb @@ -0,0 +1,51 @@ +module Recaptcha + module Verify + # Your private API can be specified in the +options+ hash or preferably + # using the Configuration. + def verify_recaptcha(options = {}) + if !options.is_a? Hash + options = {:model => options} + end + + env = options[:env] || ENV['RAILS_ENV'] + return true if Recaptcha.configuration.skip_verify_env.include? env + model = options[:model] + attribute = options[:attribute] || :base + private_key = options[:private_key] || Recaptcha.configuration.private_key + raise RecaptchaError, "No private key specified." unless private_key + + begin + recaptcha = nil + Timeout::timeout(options[:timeout] || 3) do + recaptcha = Net::HTTP.post_form URI.parse(Recaptcha.configuration.verify_url), { + "privatekey" => private_key, + "remoteip" => request.remote_ip, + "challenge" => params[:recaptcha_challenge_field], + "response" => params[:recaptcha_response_field] + } + end + answer, error = recaptcha.body.split.map { |s| s.chomp } + unless answer == 'true' + flash[:recaptcha_error] = error + if model + model.valid? + model.errors.add attribute, options[:message] || "Word verification response is incorrect, please try again." + end + return false + else + flash[:recaptcha_error] = nil + return true + end + rescue Timeout::Error + flash[:recaptcha_error] = "recaptcha-not-reachable" + if model + model.valid? + model.errors.add attribute, options[:message] || "Oops, we failed to validate your word verification response. Please try again." + end + return false + rescue Exception => e + raise RecaptchaError, e.message, e.backtrace + end + end # verify_recaptcha + end # Verify +end # Recaptcha |