easy_captcha 0.1.9 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- data/README.rdoc +15 -1
- data/VERSION +1 -1
- data/easy_captcha.gemspec +1 -1
- data/lib/easy_captcha/captcha.rb +18 -15
- data/lib/easy_captcha/controller_helpers.rb +19 -3
- data/lib/easy_captcha.rb +24 -4
- metadata +3 -3
data/README.rdoc
CHANGED
@@ -11,12 +11,22 @@ Add following line to routes.rb
|
|
11
11
|
Add following line to application_controller.rb
|
12
12
|
after_filter lambda { session.delete(:captcha) }
|
13
13
|
|
14
|
-
The last
|
14
|
+
The last filter is a critical security measurement! Every request after displaying the captcha-image must prevent this filter from being executed.
|
15
15
|
|
16
16
|
== Configuration
|
17
17
|
You can write this in "config/initializers/easy_captcha.rb", if you want to customize the default configuration
|
18
18
|
|
19
19
|
EasyCaptcha.setup do |config|
|
20
|
+
|
21
|
+
# Cache
|
22
|
+
# config.cache = true
|
23
|
+
# Cache temp dir
|
24
|
+
# config.temp_dir = Rails.root + 'tmp' + 'captchas'
|
25
|
+
# Cache size
|
26
|
+
# config.cache_size = 500
|
27
|
+
# Cache expire
|
28
|
+
# config.cache_expire = 1.days
|
29
|
+
|
20
30
|
# Chars
|
21
31
|
# config.chars = %w(2 3 4 5 6 7 9 A C D E F G H J K L M N P Q R S T U X Y Z)
|
22
32
|
# Length
|
@@ -66,6 +76,10 @@ You can write this in "config/initializers/easy_captcha.rb", if you want to cust
|
|
66
76
|
== Example
|
67
77
|
You find an example app under: http://github.com/traxanos/easy_captcha_example
|
68
78
|
|
79
|
+
== History
|
80
|
+
* 0.1 Init
|
81
|
+
* 0.2 Cache support for high frequented sites
|
82
|
+
|
69
83
|
== Maintainers
|
70
84
|
|
71
85
|
* Marco Scholl (http://github.com/traxanos)
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.
|
1
|
+
0.2.0
|
data/easy_captcha.gemspec
CHANGED
data/lib/easy_captcha/captcha.rb
CHANGED
@@ -5,32 +5,32 @@ module EasyCaptcha
|
|
5
5
|
attr_reader :code
|
6
6
|
# blob of generated captcha image
|
7
7
|
attr_reader :image
|
8
|
-
|
8
|
+
|
9
9
|
# generate captcha by code
|
10
|
-
def initialize(code)
|
10
|
+
def initialize(code, file)
|
11
11
|
@code = code
|
12
|
-
generate_captcha
|
12
|
+
generate_captcha(file)
|
13
13
|
end
|
14
|
-
|
14
|
+
|
15
15
|
def inspect #:nodoc:
|
16
16
|
"<EasyCaptcha::Captcha @code=#{code}>"
|
17
17
|
end
|
18
18
|
|
19
19
|
private
|
20
|
-
|
21
|
-
def generate_captcha #:nodoc:
|
20
|
+
|
21
|
+
def generate_captcha(file) #:nodoc:
|
22
22
|
canvas = Magick::Image.new(EasyCaptcha.image_width, EasyCaptcha.image_height) do |variable|
|
23
23
|
self.background_color = EasyCaptcha.image_background_color unless EasyCaptcha.image_background_color.nil?
|
24
24
|
end
|
25
25
|
|
26
26
|
# Render the text in the image
|
27
|
-
canvas.annotate(Magick::Draw.new, 0,0,0,0, code) {
|
28
|
-
self.gravity
|
27
|
+
canvas.annotate(Magick::Draw.new, 0, 0, 0, 0, code) {
|
28
|
+
self.gravity = Magick::CenterGravity
|
29
29
|
self.font_family = EasyCaptcha.font_family
|
30
30
|
self.font_weight = Magick::LighterWeight
|
31
|
-
self.fill
|
31
|
+
self.fill = EasyCaptcha.font_fill_color
|
32
32
|
if EasyCaptcha.font_stroke.to_i > 0
|
33
|
-
self.stroke
|
33
|
+
self.stroke = EasyCaptcha.font_stroke_color
|
34
34
|
self.stroke_width = EasyCaptcha.font_stroke
|
35
35
|
end
|
36
36
|
self.pointsize = EasyCaptcha.font_size
|
@@ -38,21 +38,24 @@ module EasyCaptcha
|
|
38
38
|
|
39
39
|
# Blur
|
40
40
|
canvas = canvas.blur_image(EasyCaptcha.blur_radius, EasyCaptcha.blur_sigma) if EasyCaptcha.blur?
|
41
|
-
|
41
|
+
|
42
42
|
# Wave
|
43
43
|
w = EasyCaptcha.wave_length
|
44
44
|
a = EasyCaptcha.wave_amplitude
|
45
45
|
canvas = canvas.wave(rand(a.last - a.first) + a.first, rand(w.last - w.first) + w.first) if EasyCaptcha.wave?
|
46
|
-
|
46
|
+
|
47
47
|
# Sketch
|
48
48
|
canvas = canvas.sketch(EasyCaptcha.sketch_radius, EasyCaptcha.sketch_sigma, rand(180)) if EasyCaptcha.sketch?
|
49
|
-
|
49
|
+
|
50
50
|
# Implode
|
51
51
|
canvas = canvas.implode(EasyCaptcha.implode.to_f) if EasyCaptcha.implode.is_a? Float
|
52
|
-
|
52
|
+
|
53
53
|
# Crop image because to big after waveing
|
54
54
|
canvas = canvas.crop(Magick::CenterGravity, EasyCaptcha.image_width, EasyCaptcha.image_height)
|
55
|
-
|
55
|
+
|
56
|
+
unless file.nil?
|
57
|
+
canvas.write(file) { self.format = 'PNG' }
|
58
|
+
end
|
56
59
|
@image = canvas.to_blob { self.format = 'PNG' }
|
57
60
|
canvas.destroy!
|
58
61
|
end
|
@@ -1,7 +1,7 @@
|
|
1
1
|
module EasyCaptcha
|
2
2
|
# helper class for ActionController
|
3
3
|
module ControllerHelpers
|
4
|
-
|
4
|
+
|
5
5
|
def self.included(base) #:nodoc:
|
6
6
|
base.class_eval do
|
7
7
|
helper_method :valid_captcha?
|
@@ -10,7 +10,23 @@ module EasyCaptcha
|
|
10
10
|
|
11
11
|
# generate captcha image and return it as blob
|
12
12
|
def generate_captcha
|
13
|
-
|
13
|
+
if EasyCaptcha.cache
|
14
|
+
FileUtils.mkdir_p(EasyCaptcha.cache_temp_dir)
|
15
|
+
files = Dir.glob(EasyCaptcha.cache_temp_dir + "*")
|
16
|
+
unless files.size < EasyCaptcha.cache_size
|
17
|
+
file = File.open(files.at(Kernel.rand(files.size)))
|
18
|
+
|
19
|
+
if file.mtime < EasyCaptcha.cache_expire.ago
|
20
|
+
File.unlink(file.path)
|
21
|
+
else
|
22
|
+
return file.readlines.join
|
23
|
+
end
|
24
|
+
end
|
25
|
+
generated_code = generate_captcha_code
|
26
|
+
Captcha.new(generated_code, EasyCaptcha.cache_temp_dir + "#{generated_code}").image
|
27
|
+
else
|
28
|
+
Captcha.new(generate_captcha_code).image
|
29
|
+
end
|
14
30
|
end
|
15
31
|
|
16
32
|
# generate captcha code, save in session and return
|
@@ -23,6 +39,6 @@ module EasyCaptcha
|
|
23
39
|
return false if session[:captcha].blank? or code.blank?
|
24
40
|
session[:captcha].to_s.upcase == code.to_s.upcase
|
25
41
|
end
|
26
|
-
|
42
|
+
|
27
43
|
end
|
28
44
|
end
|
data/lib/easy_captcha.rb
CHANGED
@@ -10,7 +10,23 @@ module EasyCaptcha
|
|
10
10
|
autoload :ModelHelpers, 'easy_captcha/model_helpers'
|
11
11
|
autoload :ViewHelpers, 'easy_captcha/view_helpers'
|
12
12
|
autoload :ControllerHelpers, 'easy_captcha/controller_helpers'
|
13
|
-
|
13
|
+
|
14
|
+
# Cache
|
15
|
+
mattr_accessor :cache
|
16
|
+
@@cache = true
|
17
|
+
|
18
|
+
# Cache temp
|
19
|
+
mattr_accessor :cache_temp_dir
|
20
|
+
@@cache_temp_dir = Rails.root + 'tmp' + 'captchas'
|
21
|
+
|
22
|
+
# Cache size
|
23
|
+
mattr_accessor :cache_size
|
24
|
+
@@cache_size = 500
|
25
|
+
|
26
|
+
# Cache expire
|
27
|
+
mattr_accessor :cache_expire
|
28
|
+
@@cache_expire = 1.days
|
29
|
+
|
14
30
|
# Chars
|
15
31
|
mattr_accessor :chars
|
16
32
|
@@chars = %w(2 3 4 5 6 7 9 A C D E F G H J K L M N P Q R S T U X Y Z)
|
@@ -62,11 +78,15 @@ module EasyCaptcha
|
|
62
78
|
def wave? #:nodoc:
|
63
79
|
wave
|
64
80
|
end
|
65
|
-
|
81
|
+
|
66
82
|
def blur? #:nodoc:
|
67
83
|
blur
|
68
84
|
end
|
69
|
-
|
85
|
+
|
86
|
+
def cache? #:nodoc:
|
87
|
+
cache
|
88
|
+
end
|
89
|
+
|
70
90
|
# called by rails after initialize
|
71
91
|
def init
|
72
92
|
ActiveRecord::Base.send :include, ModelHelpers
|
@@ -78,5 +98,5 @@ end
|
|
78
98
|
|
79
99
|
EasyCaptcha.init
|
80
100
|
|
81
|
-
|
101
|
+
|
82
102
|
|