glebtv-simple_captcha 0.2.1 → 0.3.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/README.rdoc +12 -15
- data/Rakefile +1 -22
- data/lib/generators/USAGE +1 -1
- data/lib/generators/simple_captcha_generator.rb +8 -7
- data/lib/generators/templates/migration.rb +15 -15
- data/lib/simple_captcha.rb +34 -9
- data/lib/simple_captcha/active_record.rb +1 -3
- data/lib/simple_captcha/controller.rb +1 -1
- data/lib/simple_captcha/engine.rb +3 -6
- data/lib/simple_captcha/{form_builder.rb → hooks/form_builder.rb} +0 -0
- data/lib/simple_captcha/{formtastic.rb → hooks/formtastic.rb} +1 -2
- data/lib/simple_captcha/hooks/simple_form.rb +11 -0
- data/lib/simple_captcha/image.rb +36 -5
- data/lib/simple_captcha/middleware.rb +10 -4
- data/lib/simple_captcha/{simple_captcha_data_ar.rb → storage/active_record.rb} +23 -33
- data/lib/simple_captcha/{simple_captcha_data_mongoid.rb → storage/mongoid.rb} +2 -2
- data/lib/simple_captcha/utils.rb +2 -2
- data/lib/simple_captcha/version.rb +1 -1
- data/lib/simple_captcha/view.rb +3 -4
- metadata +19 -21
- data/lib/simple_captcha/mongoid.rb +0 -75
data/README.rdoc
CHANGED
@@ -1,6 +1,9 @@
|
|
1
1
|
=SimpleCaptcha
|
2
2
|
|
3
|
-
|
3
|
+
This is a fork of wolcanus's fork of SimpleCaptcha to support Mongoid 3 and new formtastic.
|
4
|
+
|
5
|
+
This is a fork of galetahub SimpleCaptcha to support Mongoid.
|
6
|
+
Its implementation requires
|
4
7
|
adding up a single line in views and in controllers/models.
|
5
8
|
SimpleCaptcha is available to be used with Rails 3 or above and also it provides the
|
6
9
|
backward compatibility with previous versions of Rails.
|
@@ -17,15 +20,18 @@ backward compatibility with previous versions of Rails.
|
|
17
20
|
|
18
21
|
==Requirements
|
19
22
|
|
20
|
-
* {Ruby}[http://ruby-lang.org/] >= 1.
|
23
|
+
* {Ruby}[http://ruby-lang.org/] >= 1.8.7
|
21
24
|
* {Rails}[http://github.com/rails/rails] >= 3
|
22
25
|
* ImageMagick should be installed on your machine to use this plugin.
|
23
26
|
visit http://www.imagemagick.org/script/index.php for more details.
|
24
|
-
* AR or mongoid
|
25
27
|
|
26
28
|
==Installation
|
27
29
|
|
28
|
-
gem "glebtv-simple_captcha",
|
30
|
+
gem "glebtv-simple_captcha", :require => "simple_captcha"
|
31
|
+
|
32
|
+
or
|
33
|
+
|
34
|
+
gem 'glebtv-simple_captcha', :require => 'simple_captcha', :git => 'git://github.com/glebtv/simple-captcha.git'
|
29
35
|
|
30
36
|
==Setup
|
31
37
|
|
@@ -34,8 +40,6 @@ on the version of rails your application is using.
|
|
34
40
|
|
35
41
|
rails generate simple_captcha
|
36
42
|
|
37
|
-
rake db:migrate
|
38
|
-
|
39
43
|
==Usage
|
40
44
|
|
41
45
|
===Controller Based
|
@@ -163,13 +167,6 @@ You can provide the path where image_magick is installed as well:
|
|
163
167
|
sc.image_magick_path = '/usr/bin' # you can check this from console by running: which convert
|
164
168
|
end
|
165
169
|
|
166
|
-
You can provide the path where should be stored tmp files.
|
167
|
-
It's usefull when you dont have acces to /tmp (default directory)
|
168
|
-
|
169
|
-
SimpleCaptcha.setup do |sc|
|
170
|
-
sc.tmp_path = '/tmp' # or somewhere in project eg. Rails.root.join('tmp/simple_captcha').to_s, make shure directory exists
|
171
|
-
end
|
172
|
-
|
173
170
|
|
174
171
|
===How to change the CSS for SimpleCaptcha DOM elements?
|
175
172
|
You can change the CSS of the SimpleCaptcha DOM elements as per your need in this file.
|
@@ -209,7 +206,7 @@ You can change the CSS of the SimpleCaptcha DOM elements as per your need in thi
|
|
209
206
|
default: "Secret Code did not match with the Image"
|
210
207
|
user: "The secret Image and code were different"
|
211
208
|
|
212
|
-
==
|
209
|
+
==The Original Author
|
213
210
|
|
214
211
|
Enjoy the simplest captcha implementation.
|
215
212
|
|
@@ -223,4 +220,4 @@ Plugin Homepage: http://expressica.com/simple_captcha
|
|
223
220
|
|
224
221
|
Plugin update for rails 3: http://github.com/galetahub
|
225
222
|
|
226
|
-
Any feedback/comment/issue/donation is welcome!
|
223
|
+
Any feedback/comment/issue/donation is welcome!
|
data/Rakefile
CHANGED
@@ -1,22 +1 @@
|
|
1
|
-
require
|
2
|
-
require 'rake/testtask'
|
3
|
-
require 'rake/rdoctask'
|
4
|
-
|
5
|
-
desc 'Default: run unit tests.'
|
6
|
-
task :default => :test
|
7
|
-
|
8
|
-
desc 'Test the simple_captcha plugin.'
|
9
|
-
Rake::TestTask.new(:test) do |t|
|
10
|
-
t.libs << 'lib'
|
11
|
-
t.pattern = 'test/**/*_test.rb'
|
12
|
-
t.verbose = true
|
13
|
-
end
|
14
|
-
|
15
|
-
desc 'Generate documentation for the simple_captcha plugin.'
|
16
|
-
Rake::RDocTask.new(:rdoc) do |rdoc|
|
17
|
-
rdoc.rdoc_dir = 'rdoc'
|
18
|
-
rdoc.title = 'SimpleCaptcha'
|
19
|
-
rdoc.options << '--line-numbers' << '--inline-source'
|
20
|
-
rdoc.rdoc_files.include('README')
|
21
|
-
rdoc.rdoc_files.include('lib/**/*.rb')
|
22
|
-
end
|
1
|
+
require "bundler/gem_tasks"
|
data/lib/generators/USAGE
CHANGED
@@ -6,16 +6,17 @@ class SimpleCaptchaGenerator < Rails::Generators::Base
|
|
6
6
|
def self.source_root
|
7
7
|
@source_root ||= File.expand_path(File.join(File.dirname(__FILE__), 'templates/'))
|
8
8
|
end
|
9
|
-
|
10
|
-
def self.next_migration_number(dirname)
|
11
|
-
Time.now.strftime("%Y%m%d%H%M%S")
|
12
|
-
end
|
13
9
|
|
14
10
|
def create_partial
|
15
11
|
template "partial.erb", File.join('app/views', 'simple_captcha', "_simple_captcha.erb")
|
16
12
|
end
|
17
|
-
|
18
|
-
|
19
|
-
|
13
|
+
|
14
|
+
unless defined?(Mongoid)
|
15
|
+
def self.next_migration_number(dirname)
|
16
|
+
Time.now.strftime("%Y%m%d%H%M%S")
|
17
|
+
end
|
18
|
+
def create_migration
|
19
|
+
migration_template "migration.rb", File.join('db/migrate', "create_simple_captcha_data.rb")
|
20
|
+
end
|
20
21
|
end
|
21
22
|
end
|
@@ -1,15 +1,15 @@
|
|
1
|
-
class CreateSimpleCaptchaData < ActiveRecord::Migration
|
2
|
-
def self.up
|
3
|
-
create_table :simple_captcha_data do |t|
|
4
|
-
t.string :key, :limit => 40
|
5
|
-
t.string :value, :limit => 6
|
6
|
-
t.timestamps
|
7
|
-
end
|
8
|
-
|
9
|
-
add_index :simple_captcha_data, :key, :name => "idx_key"
|
10
|
-
end
|
11
|
-
|
12
|
-
def self.down
|
13
|
-
drop_table :simple_captcha_data
|
14
|
-
end
|
15
|
-
end
|
1
|
+
class CreateSimpleCaptchaData < ActiveRecord::Migration
|
2
|
+
def self.up
|
3
|
+
create_table :simple_captcha_data do |t|
|
4
|
+
t.string :key, :limit => 40
|
5
|
+
t.string :value, :limit => 6
|
6
|
+
t.timestamps
|
7
|
+
end
|
8
|
+
|
9
|
+
add_index :simple_captcha_data, :key, :name => "idx_key"
|
10
|
+
end
|
11
|
+
|
12
|
+
def self.down
|
13
|
+
drop_table :simple_captcha_data
|
14
|
+
end
|
15
|
+
end
|
data/lib/simple_captcha.rb
CHANGED
@@ -7,17 +7,23 @@ module SimpleCaptcha
|
|
7
7
|
autoload :ViewHelper, 'simple_captcha/view'
|
8
8
|
autoload :ControllerHelpers, 'simple_captcha/controller'
|
9
9
|
|
10
|
-
autoload :FormBuilder, 'simple_captcha/form_builder'
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
10
|
+
autoload :FormBuilder, 'simple_captcha/hooks/form_builder'
|
11
|
+
|
12
|
+
if Object.const_defined?("Formtastic")
|
13
|
+
require "simple_captcha/hooks/formtastic"
|
14
|
+
end
|
15
|
+
|
16
|
+
if Object.const_defined?("SimpleForm")
|
17
|
+
require "simple_captcha/hooks/simple_form"
|
18
|
+
end
|
19
|
+
|
20
|
+
if Object.const_defined?("Mongoid")
|
21
|
+
autoload :SimpleCaptchaData, 'simple_captcha/storage/mongoid'
|
17
22
|
else
|
23
|
+
autoload :SimpleCaptchaData, 'simple_captcha/storage/active_record'
|
18
24
|
autoload :ModelHelpers, 'simple_captcha/active_record'
|
19
|
-
autoload :SimpleCaptchaData, 'simple_captcha/simple_captcha_data_ar'
|
20
25
|
end
|
26
|
+
|
21
27
|
autoload :Middleware, 'simple_captcha/middleware'
|
22
28
|
|
23
29
|
mattr_accessor :image_size
|
@@ -26,8 +32,20 @@ module SimpleCaptcha
|
|
26
32
|
mattr_accessor :length
|
27
33
|
@@length = 5
|
28
34
|
|
35
|
+
# 'embosed_silver',
|
36
|
+
# 'simply_red',
|
37
|
+
# 'simply_green',
|
38
|
+
# 'simply_blue',
|
39
|
+
# 'distorted_black',
|
40
|
+
# 'all_black',
|
41
|
+
# 'charcoal_grey',
|
42
|
+
# 'almost_invisible'
|
43
|
+
# 'random'
|
44
|
+
mattr_accessor :image_style
|
45
|
+
@@image_style = 'simply_blue'
|
46
|
+
|
29
47
|
mattr_accessor :image_color
|
30
|
-
@@image_color =
|
48
|
+
@@image_color = nil
|
31
49
|
|
32
50
|
# 'low', 'medium', 'high', 'random'
|
33
51
|
mattr_accessor :distortion
|
@@ -41,6 +59,13 @@ module SimpleCaptcha
|
|
41
59
|
mattr_accessor :tmp_path
|
42
60
|
@@tmp_path = nil
|
43
61
|
|
62
|
+
mattr_accessor :pointsize
|
63
|
+
@@pointsize = 30
|
64
|
+
|
65
|
+
def self.add_image_style(name, params = [])
|
66
|
+
SimpleCaptcha::ImageHelpers.image_styles.update(name.to_s => params)
|
67
|
+
end
|
68
|
+
|
44
69
|
def self.setup
|
45
70
|
yield self
|
46
71
|
end
|
@@ -54,14 +54,12 @@ module SimpleCaptcha #:nodoc
|
|
54
54
|
end
|
55
55
|
|
56
56
|
def is_captcha_valid?
|
57
|
-
return true if Rails.env.test?
|
58
|
-
|
59
57
|
if captcha && captcha.upcase.delete(" ") == SimpleCaptcha::Utils::simple_captcha_value(captcha_key)
|
60
58
|
SimpleCaptcha::Utils::simple_captcha_passed!(captcha_key)
|
61
59
|
return true
|
62
60
|
else
|
63
61
|
message = simple_captcha_options[:message] || I18n.t(self.class.model_name.downcase, :scope => [:simple_captcha, :message], :default => :default)
|
64
|
-
simple_captcha_options[:add_to_base] ? errors.
|
62
|
+
simple_captcha_options[:add_to_base] ? errors.add_to_base(message) : errors.add(:captcha, message)
|
65
63
|
return false
|
66
64
|
end
|
67
65
|
end
|
@@ -17,7 +17,7 @@ module SimpleCaptcha #:nodoc
|
|
17
17
|
return true if Rails.env.test?
|
18
18
|
|
19
19
|
if params[:captcha]
|
20
|
-
data = SimpleCaptcha::Utils::simple_captcha_value(
|
20
|
+
data = SimpleCaptcha::Utils::simple_captcha_value(session[:captcha])
|
21
21
|
result = data == params[:captcha].delete(" ").upcase
|
22
22
|
SimpleCaptcha::Utils::simple_captcha_passed!(session[:captcha]) if result
|
23
23
|
return result
|
@@ -1,11 +1,12 @@
|
|
1
1
|
# encoding: utf-8
|
2
|
+
|
2
3
|
require 'rails'
|
3
4
|
require 'simple_captcha'
|
4
5
|
|
5
6
|
module SimpleCaptcha
|
6
7
|
class Engine < ::Rails::Engine
|
7
|
-
|
8
|
-
|
8
|
+
unless Object.const_defined?("Mongoid")
|
9
|
+
config.before_initialize do
|
9
10
|
ActiveSupport.on_load :active_record do
|
10
11
|
ActiveRecord::Base.send(:include, SimpleCaptcha::ModelHelpers)
|
11
12
|
end
|
@@ -15,10 +16,6 @@ module SimpleCaptcha
|
|
15
16
|
config.after_initialize do
|
16
17
|
ActionView::Base.send(:include, SimpleCaptcha::ViewHelper)
|
17
18
|
ActionView::Helpers::FormBuilder.send(:include, SimpleCaptcha::FormBuilder)
|
18
|
-
|
19
|
-
if Object.const_defined?("Formtastic") && Formtastic.const_defined?("Inputs")
|
20
|
-
require "simple_captcha/formtastic"
|
21
|
-
end
|
22
19
|
end
|
23
20
|
|
24
21
|
config.app_middleware.use SimpleCaptcha::Middleware
|
File without changes
|
@@ -3,12 +3,11 @@ require "formtastic"
|
|
3
3
|
class SimpleCaptchaInput
|
4
4
|
include ::Formtastic::Inputs::Base
|
5
5
|
include ::Formtastic::Inputs::Base::Stringish
|
6
|
-
|
6
|
+
|
7
7
|
def to_html
|
8
8
|
input_wrapping do
|
9
9
|
input_html_options.update :object => method
|
10
10
|
label_html << builder.show_simple_captcha(input_html_options)
|
11
|
-
# label_html << builder.text_field(method, input_html_options)
|
12
11
|
end
|
13
12
|
end
|
14
13
|
end
|
@@ -0,0 +1,11 @@
|
|
1
|
+
require 'simple_form'
|
2
|
+
|
3
|
+
module SimpleCaptcha::SimpleForm
|
4
|
+
class CkeditorInput < ::SimpleForm::Inputs::Base
|
5
|
+
def input
|
6
|
+
@builder.simple_captcha(input_html_options)
|
7
|
+
end
|
8
|
+
end
|
9
|
+
end
|
10
|
+
|
11
|
+
::SimpleForm::FormBuilder.map_type :simple_captcha, :to => SimpleCaptcha::SimpleForm::CkeditorInput
|
data/lib/simple_captcha/image.rb
CHANGED
@@ -2,11 +2,37 @@ require 'tempfile'
|
|
2
2
|
module SimpleCaptcha #:nodoc
|
3
3
|
module ImageHelpers #:nodoc
|
4
4
|
|
5
|
+
mattr_accessor :image_styles
|
6
|
+
@@image_styles = {
|
7
|
+
'embosed_silver' => ['-fill darkblue', '-shade 20x60', '-background white'],
|
8
|
+
'simply_red' => ['-fill darkred', '-background white'],
|
9
|
+
'simply_green' => ['-fill darkgreen', '-background white'],
|
10
|
+
'simply_blue' => ['-fill darkblue', '-background white'],
|
11
|
+
'distorted_black' => ['-fill darkblue', '-edge 10', '-background white'],
|
12
|
+
'all_black' => ['-fill darkblue', '-edge 2', '-background white'],
|
13
|
+
'charcoal_grey' => ['-fill darkblue', '-charcoal 5', '-background white'],
|
14
|
+
'almost_invisible' => ['-fill red', '-solarize 50', '-background white']
|
15
|
+
}
|
16
|
+
|
5
17
|
DISTORTIONS = ['low', 'medium', 'high']
|
6
18
|
|
7
19
|
class << self
|
8
20
|
|
9
|
-
def image_params(
|
21
|
+
def image_params(key = 'simply_blue')
|
22
|
+
image_keys = @@image_styles.keys
|
23
|
+
|
24
|
+
style = begin
|
25
|
+
if key == 'random'
|
26
|
+
image_keys[rand(image_keys.length)]
|
27
|
+
else
|
28
|
+
image_keys.include?(key) ? key : 'simply_blue'
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
@@image_styles[style]
|
33
|
+
end
|
34
|
+
|
35
|
+
def image_params_from_color(color)
|
10
36
|
["-alpha set -background none -fill \"#{color}\""]
|
11
37
|
end
|
12
38
|
|
@@ -38,11 +64,16 @@ module SimpleCaptcha #:nodoc
|
|
38
64
|
def generate_simple_captcha_image(simple_captcha_key) #:nodoc
|
39
65
|
amplitude, frequency = ImageHelpers.distortion(SimpleCaptcha.distortion)
|
40
66
|
text = Utils::simple_captcha_new_value(simple_captcha_key)
|
41
|
-
|
67
|
+
if SimpleCaptcha.image_color.nil?
|
68
|
+
params = ImageHelpers.image_params(SimpleCaptcha.image_style).dup
|
69
|
+
else
|
70
|
+
params = ImageHelpers.image_params_from_color(SimpleCaptcha.image_color).dup
|
71
|
+
end
|
72
|
+
|
42
73
|
params << "-size #{SimpleCaptcha.image_size} xc:transparent"
|
43
74
|
params << "-gravity \"Center\""
|
44
75
|
if params.join(' ').index('-pointsize').nil?
|
45
|
-
params << "-pointsize
|
76
|
+
params << "-pointsize #{SimpleCaptcha.pointsize}"
|
46
77
|
end
|
47
78
|
dst = Tempfile.new(RUBY_VERSION < '1.9' ? 'simple_captcha.png' : ['simple_captcha', '.png'], SimpleCaptcha.tmp_path)
|
48
79
|
dst.binmode
|
@@ -58,13 +89,13 @@ module SimpleCaptcha #:nodoc
|
|
58
89
|
end
|
59
90
|
|
60
91
|
params << "\"#{File.expand_path(dst.path)}\""
|
61
|
-
# puts "convert " +
|
92
|
+
# puts "convert " + params.join(' ')
|
62
93
|
SimpleCaptcha::Utils::run("convert", params.join(' '))
|
63
94
|
|
64
95
|
dst.close
|
65
96
|
|
66
97
|
File.expand_path(dst.path)
|
67
|
-
|
98
|
+
#dst
|
68
99
|
end
|
69
100
|
end
|
70
101
|
end
|
@@ -26,8 +26,14 @@ module SimpleCaptcha
|
|
26
26
|
request = Rack::Request.new(env)
|
27
27
|
code = request.params["code"]
|
28
28
|
body = []
|
29
|
+
|
29
30
|
if !code.blank? && Utils::simple_captcha_value(code)
|
30
|
-
|
31
|
+
#status, headers, body = @app.call(env)
|
32
|
+
#status = 200
|
33
|
+
#body = generate_simple_captcha_image(code)
|
34
|
+
#headers['Content-Type'] = 'image/jpeg'
|
35
|
+
|
36
|
+
return send_file(generate_simple_captcha_image(code), :type => 'image/jpeg', :disposition => 'inline', :filename => 'simple_captcha.jpg')
|
31
37
|
end
|
32
38
|
|
33
39
|
[status, headers, body]
|
@@ -44,9 +50,9 @@ module SimpleCaptcha
|
|
44
50
|
|
45
51
|
status = options[:status] || 200
|
46
52
|
headers = {"Content-Disposition" => "#{options[:disposition]}; filename='#{options[:filename]}'", "Content-Type" => options[:type], 'Content-Transfer-Encoding' => 'binary', 'Cache-Control' => 'private'}
|
47
|
-
response_body = File.open(path,
|
48
|
-
|
49
|
-
[status, headers,
|
53
|
+
response_body = File.open(path, "rb")
|
54
|
+
|
55
|
+
[status, headers, response_body]
|
50
56
|
end
|
51
57
|
end
|
52
58
|
end
|
@@ -1,33 +1,23 @@
|
|
1
|
-
module SimpleCaptcha
|
2
|
-
class SimpleCaptchaData < ::ActiveRecord::Base
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
end
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
clear_old_data(1.hour.ago)
|
25
|
-
end
|
26
|
-
|
27
|
-
def clear_old_data(time = 1.hour.ago)
|
28
|
-
return unless Time === time
|
29
|
-
delete_all(["#{connection.quote_column_name(:updated_at)} < ?", time])
|
30
|
-
end
|
31
|
-
end
|
32
|
-
end
|
33
|
-
end
|
1
|
+
module SimpleCaptcha
|
2
|
+
class SimpleCaptchaData < ::ActiveRecord::Base
|
3
|
+
self.table_name = "simple_captcha_data"
|
4
|
+
|
5
|
+
attr_accessible :key, :value
|
6
|
+
|
7
|
+
class << self
|
8
|
+
def get_data(key)
|
9
|
+
data = find_by_key(key) || new(:key => key)
|
10
|
+
end
|
11
|
+
|
12
|
+
def remove_data(key)
|
13
|
+
delete_all(["#{connection.quote_column_name(:key)} = ?", key])
|
14
|
+
clear_old_data(1.hour.ago)
|
15
|
+
end
|
16
|
+
|
17
|
+
def clear_old_data(time = 1.hour.ago)
|
18
|
+
return unless Time === time
|
19
|
+
delete_all(["#{connection.quote_column_name(:updated_at)} < ?", time])
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
@@ -1,14 +1,14 @@
|
|
1
1
|
module SimpleCaptcha
|
2
2
|
class SimpleCaptchaData
|
3
3
|
include Mongoid::Document
|
4
|
+
include Mongoid::Timestamps
|
4
5
|
|
5
6
|
field :key, :type => String
|
6
7
|
field :value, :type => String
|
7
|
-
field :created_at, :type => Time, default: -> { Time.now }
|
8
8
|
|
9
9
|
class << self
|
10
10
|
def get_data(key)
|
11
|
-
|
11
|
+
SimpleCaptchaData.where(:key => key).first || new(:key => key)
|
12
12
|
end
|
13
13
|
|
14
14
|
def remove_data(key)
|
data/lib/simple_captcha/utils.rb
CHANGED
@@ -23,12 +23,13 @@ module SimpleCaptcha #:nodoc
|
|
23
23
|
def self.simple_captcha_value(key) #:nodoc
|
24
24
|
SimpleCaptchaData.get_data(key).value rescue nil
|
25
25
|
end
|
26
|
-
|
26
|
+
|
27
27
|
def self.simple_captcha_new_value(key) #:nodoc
|
28
28
|
begin
|
29
29
|
# very unsafe to display same code over and over
|
30
30
|
value = ''
|
31
31
|
# SimpleCaptcha.length.times{value << (48 + rand(10)).chr}
|
32
|
+
|
32
33
|
SimpleCaptcha.length.times{value << (65 + rand(26)).chr}
|
33
34
|
d = SimpleCaptchaData.get_data(key)
|
34
35
|
d.value = value
|
@@ -38,7 +39,6 @@ module SimpleCaptcha #:nodoc
|
|
38
39
|
nil
|
39
40
|
end
|
40
41
|
end
|
41
|
-
|
42
42
|
|
43
43
|
def self.simple_captcha_passed!(key) #:nodoc
|
44
44
|
SimpleCaptchaData.remove_data(key)
|
data/lib/simple_captcha/view.rb
CHANGED
@@ -57,12 +57,12 @@ module SimpleCaptcha #:nodoc
|
|
57
57
|
|
58
58
|
private
|
59
59
|
|
60
|
-
def simple_captcha_image(
|
60
|
+
def simple_captcha_image(simple_captcha_key, options = {})
|
61
61
|
defaults = {}
|
62
62
|
defaults[:time] = options[:time] || Time.now.to_i
|
63
63
|
|
64
64
|
query = defaults.collect{ |key, value| "#{key}=#{value}" }.join('&')
|
65
|
-
url = "/simple_captcha?code=#{
|
65
|
+
url = "/simple_captcha?code=#{simple_captcha_key}&#{query}"
|
66
66
|
|
67
67
|
"<img src='#{url}' alt='captcha' />".html_safe
|
68
68
|
end
|
@@ -72,8 +72,7 @@ module SimpleCaptcha #:nodoc
|
|
72
72
|
text_field(options[:object], :captcha, :value => '', :autocomplete => 'off') +
|
73
73
|
hidden_field(options[:object], :captcha_key, {:value => options[:field_value]})
|
74
74
|
else
|
75
|
-
text_field_tag(:captcha, nil, :autocomplete => 'off')
|
76
|
-
hidden_field_tag(:captcha_key, options[:field_value])
|
75
|
+
text_field_tag(:captcha, nil, :autocomplete => 'off')
|
77
76
|
end
|
78
77
|
end
|
79
78
|
|
metadata
CHANGED
@@ -1,43 +1,41 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: glebtv-simple_captcha
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.3.1
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
8
|
-
-
|
9
|
-
- Igor Galeta
|
8
|
+
- GlebTv
|
10
9
|
autorequire:
|
11
10
|
bindir: bin
|
12
11
|
cert_chain: []
|
13
|
-
date:
|
12
|
+
date: 2013-03-12 00:00:00.000000000 Z
|
14
13
|
dependencies: []
|
15
|
-
description:
|
16
|
-
|
17
|
-
email: galeta.igor@gmail.com
|
14
|
+
description: .
|
15
|
+
email: i@gleb.tv
|
18
16
|
executables: []
|
19
17
|
extensions: []
|
20
18
|
extra_rdoc_files:
|
21
19
|
- README.rdoc
|
22
20
|
files:
|
21
|
+
- lib/simple_captcha.rb
|
22
|
+
- lib/generators/simple_captcha_generator.rb
|
23
23
|
- lib/generators/templates/partial.erb
|
24
24
|
- lib/generators/templates/migration.rb
|
25
|
-
- lib/generators/simple_captcha_generator.rb
|
26
25
|
- lib/generators/USAGE
|
27
|
-
- lib/simple_captcha/active_record.rb
|
28
|
-
- lib/simple_captcha/version.rb
|
29
|
-
- lib/simple_captcha/view.rb
|
30
|
-
- lib/simple_captcha/utils.rb
|
31
26
|
- lib/simple_captcha/controller.rb
|
32
|
-
- lib/simple_captcha/
|
33
|
-
- lib/simple_captcha/
|
34
|
-
- lib/simple_captcha/
|
35
|
-
- lib/simple_captcha/
|
36
|
-
- lib/simple_captcha/
|
27
|
+
- lib/simple_captcha/storage/mongoid.rb
|
28
|
+
- lib/simple_captcha/storage/active_record.rb
|
29
|
+
- lib/simple_captcha/view.rb
|
30
|
+
- lib/simple_captcha/hooks/form_builder.rb
|
31
|
+
- lib/simple_captcha/hooks/formtastic.rb
|
32
|
+
- lib/simple_captcha/hooks/simple_form.rb
|
37
33
|
- lib/simple_captcha/middleware.rb
|
38
|
-
- lib/simple_captcha/
|
34
|
+
- lib/simple_captcha/engine.rb
|
39
35
|
- lib/simple_captcha/image.rb
|
40
|
-
- lib/simple_captcha.rb
|
36
|
+
- lib/simple_captcha/utils.rb
|
37
|
+
- lib/simple_captcha/active_record.rb
|
38
|
+
- lib/simple_captcha/version.rb
|
41
39
|
- Rakefile
|
42
40
|
- README.rdoc
|
43
41
|
- test/simple_captcha_test.rb
|
@@ -60,11 +58,11 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
60
58
|
- !ruby/object:Gem::Version
|
61
59
|
version: '0'
|
62
60
|
requirements: []
|
63
|
-
rubyforge_project:
|
61
|
+
rubyforge_project:
|
64
62
|
rubygems_version: 1.8.22
|
65
63
|
signing_key:
|
66
64
|
specification_version: 3
|
67
|
-
summary:
|
65
|
+
summary: A fork of a fork of a fork of simple_captcha.
|
68
66
|
test_files:
|
69
67
|
- test/simple_captcha_test.rb
|
70
68
|
has_rdoc:
|
@@ -1,75 +0,0 @@
|
|
1
|
-
module SimpleCaptcha #:nodoc
|
2
|
-
module ModelHelpers #:nodoc
|
3
|
-
def self.included(base)
|
4
|
-
base.extend(SingletonMethods)
|
5
|
-
end
|
6
|
-
|
7
|
-
# To implement model based simple captcha use this method in the model as...
|
8
|
-
#
|
9
|
-
# class User
|
10
|
-
# include Mongoid::Document
|
11
|
-
# include Mongoid::SimpleCaptcha
|
12
|
-
# apply_simple_captcha :message => "my customized message"
|
13
|
-
#
|
14
|
-
# end
|
15
|
-
#
|
16
|
-
# Customize the error message by using :message, the default message is "Captcha did not match".
|
17
|
-
# As in the applications captcha is needed with a very few cases like signing up the new user, but
|
18
|
-
# not every time you need to authenticate the captcha with @user.save. So as to maintain simplicity
|
19
|
-
# here we have the explicit method to save the instace with captcha validation as...
|
20
|
-
#
|
21
|
-
# * to validate the instance
|
22
|
-
#
|
23
|
-
# @user.valid_with_captcha? # whene captcha validation is required.
|
24
|
-
#
|
25
|
-
# @user.valid? # when captcha validation is not required.
|
26
|
-
#
|
27
|
-
# * to save the instance
|
28
|
-
#
|
29
|
-
# @user.save_with_captcha # whene captcha validation is required.
|
30
|
-
#
|
31
|
-
# @user.save # when captcha validation is not required.
|
32
|
-
module SingletonMethods
|
33
|
-
def apply_simple_captcha(options = {})
|
34
|
-
options = { :add_to_base => false }.merge(options)
|
35
|
-
|
36
|
-
class_attribute :simple_captcha_options
|
37
|
-
self.simple_captcha_options = options
|
38
|
-
|
39
|
-
unless self.is_a?(ClassMethods)
|
40
|
-
include InstanceMethods
|
41
|
-
extend ClassMethods
|
42
|
-
|
43
|
-
attr_accessor :captcha, :captcha_key
|
44
|
-
end
|
45
|
-
end
|
46
|
-
end
|
47
|
-
|
48
|
-
module ClassMethods
|
49
|
-
end
|
50
|
-
|
51
|
-
module InstanceMethods
|
52
|
-
|
53
|
-
def valid_with_captcha?
|
54
|
-
[valid?, is_captcha_valid?].all?
|
55
|
-
end
|
56
|
-
|
57
|
-
def is_captcha_valid?
|
58
|
-
return true if Rails.env.test?
|
59
|
-
|
60
|
-
if captcha && captcha.upcase.delete(" ") == SimpleCaptcha::Utils::simple_captcha_value(captcha_key)
|
61
|
-
SimpleCaptcha::Utils::simple_captcha_passed!(captcha_key)
|
62
|
-
return true
|
63
|
-
else
|
64
|
-
message = simple_captcha_options[:message] || I18n.t(self.class.model_name.downcase, :scope => [:simple_captcha, :message], :default => :default)
|
65
|
-
simple_captcha_options[:add_to_base] ? errors.add(:base, message) : errors.add(:captcha, message)
|
66
|
-
return false
|
67
|
-
end
|
68
|
-
end
|
69
|
-
|
70
|
-
def save_with_captcha
|
71
|
-
valid_with_captcha? && save(:validate => false)
|
72
|
-
end
|
73
|
-
end
|
74
|
-
end
|
75
|
-
end
|