GitHub Sale: sign up for any paid plan this week and pay nothing until January 1, 2009!  [ hide ]

public
Fork of ambethia/recaptcha
Description: ReCaptcha helpers for Rails apps
Homepage: http://ambethia.com/recaptcha
Clone URL: git://github.com/abloom/recaptcha.git
made private_key an options for verify_recaptcha
abloom (author)
Thu Aug 07 13:04:02 -0700 2008
commit  e4c2388c347368ef9cdc5a87b4f31906af9baed9
tree    98c3e128f499065dd6c083a3a51e4d03255d2b54
parent  045e10b0224deed6f9242910edf922a23dbb0751
...
3
4
5
6
 
7
8
9
 
 
10
11
12
13
14
15
16
17
18
 
 
 
 
 
 
19
20
21
 
22
23
24
...
39
40
41
42
 
43
44
45
46
47
48
49
 
50
51
 
 
 
 
52
53
54
 
 
 
55
56
57
58
 
59
 
60
61
62
...
3
4
5
 
6
7
8
9
10
11
12
13
14
15
16
 
 
 
 
17
18
19
20
21
22
23
24
25
26
27
28
29
...
44
45
46
 
47
48
49
50
51
52
53
 
54
55
 
56
57
58
59
60
 
 
61
62
63
64
65
66
 
67
68
69
70
71
72
0
@@ -3,22 +3,27 @@ module Ambethia
0
   module ReCaptcha
0
     RECAPTCHA_API_SERVER = 'http://api.recaptcha.net';
0
     RECAPTCHA_API_SECURE_SERVER = 'https://api-secure.recaptcha.net';
0
- RECAPTCHA_VERIFY_SERVER = 'api-verify.recaptcha.net';
0
+ RECAPTCHA_VERIFY_SERVER = 'http://api-verify.recaptcha.net/verify';
0
 
0
     SKIP_VERIFY_ENV = ['test']
0
 
0
+ mattr_accessor :public_key, :private_key
0
+
0
     module Helper
0
       # Your public API can be specified in the +options+ hash or preferably the environment
0
       # variable +RECAPTCHA_PUBLIC_KEY+.
0
       def recaptcha_tags(options = {})
0
         # Default options
0
- key = options[:public_key] ||= ENV['RECAPTCHA_PUBLIC_KEY']
0
- error = options[:error] ||= session[:recaptcha_error]
0
- uri = options[:ssl] ? RECAPTCHA_API_SECURE_SERVER : RECAPTCHA_API_SERVER
0
- xhtml = Builder::XmlMarkup.new :target => out=(''), :indent => 2 # Because I can.
0
+ key = (options[:public_key] || Ambethia::Recaptcha.public_key)
0
+ raise ReCaptchaError, "No public key specified." unless key
0
+ error = (options[:error] || session[:recaptcha_error])
0
+ uri = (options[:ssl] ? RECAPTCHA_API_SECURE_SERVER : RECAPTCHA_API_SERVER)
0
+ xhtml = Builder::XmlMarkup.new(:target => out=(''), :indent => 2) # Because I can.
0
+
0
         if options[:display]
0
           xhtml.script(:type => "text/javascript"){ xhtml.text! "var RecaptchaOptions = #{options[:display].to_json};\n"}
0
         end
0
+
0
         if options[:ajax]
0
          xhtml.div(:id => 'dynamic_recaptcha') {}
0
          xhtml.script(:type => "text/javascript", :src => "#{uri}/js/recaptcha_ajax.js") {}
0
@@ -39,24 +44,29 @@ module Ambethia
0
             end
0
           end
0
         end
0
- raise ReCaptchaError, "No public key specified." unless key
0
+
0
         return out
0
       end # recaptcha_tags
0
     end # Helpers
0
     
0
     module Controller
0
       # Your private API key must be specified in the environment variable +RECAPTCHA_PRIVATE_KEY+
0
- def verify_recaptcha(model = nil)
0
+ def verify_recaptcha(model = nil, options = {})
0
         return true if SKIP_VERIFY_ENV.include? ENV['RAILS_ENV']
0
- raise ReCaptchaError, "No private key specified." unless ENV['RECAPTCHA_PRIVATE_KEY']
0
+
0
+ key = (options[:private_key] || Ambethia::Recaptcha.private_key)
0
+ raise ReCaptchaError, "No private key specified." unless key
0
+
0
         begin
0
- recaptcha = Net::HTTP.post_form URI.parse("http://#{RECAPTCHA_VERIFY_SERVER}/verify"), {
0
- :privatekey => ENV['RECAPTCHA_PRIVATE_KEY'],
0
+ logger.info "Calling reCAPTCHA: #{RECAPTCHA_VERIFY_SERVER}"
0
+ recaptcha = Net::HTTP.post_form(URI.parse(RECAPTCHA_VERIFY_SERVER), {
0
+ :privatekey => key,
0
             :remoteip => request.remote_ip,
0
             :challenge => params[:recaptcha_challenge_field],
0
             :response => params[:recaptcha_response_field]
0
- }
0
+ })
0
           answer, error = recaptcha.body.split.map { |s| s.chomp }
0
+ logger.info "reCAPTCHA returned: #{answer.inspect}, #{error.inspect}"
0
           unless answer == 'true'
0
             session[:recaptcha_error] = error
0
             model.valid? if model

Comments

    No one has commented yet.