winton-captcha 1.0.4 → 1.0.5
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.markdown +60 -10
- data/captcha.gemspec +1 -1
- data/lib/captcha/action.rb +9 -19
- data/lib/captcha/config.rb +43 -40
- metadata +1 -1
- data/resources/captchas.rb +0 -24
- data/tasks/captcha.rake +0 -55
data/README.markdown
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
captcha
|
2
2
|
=======
|
3
3
|
|
4
|
-
An Rmagick based, Google-style captcha generator
|
4
|
+
An Rmagick based, Google-style captcha generator.
|
5
5
|
|
6
6
|
|
7
7
|
Install
|
@@ -9,19 +9,69 @@ Install
|
|
9
9
|
|
10
10
|
script/plugin install git://github.com/winton/captcha.git
|
11
11
|
|
12
|
-
###
|
12
|
+
### Create lib/captcha_config.rb (optional)
|
13
13
|
|
14
|
-
|
14
|
+
<pre>
|
15
|
+
{
|
16
|
+
:colors => {
|
17
|
+
:background => '#1B1C20',
|
18
|
+
:font => '#7385C5'
|
19
|
+
},
|
20
|
+
# number of captcha images to generate
|
21
|
+
:count => 500,
|
22
|
+
:destination => "#{RAILS_ROOT}/public/images/captchas"
|
23
|
+
),
|
24
|
+
:dimensions => {
|
25
|
+
# canvas height (px)
|
26
|
+
:height => 31,
|
27
|
+
# canvas width (px)
|
28
|
+
:width => 90
|
29
|
+
},
|
30
|
+
:generate_every => 24 * 60 * 60,
|
31
|
+
# http://www.imagemagick.org/RMagick/doc/image2.html#implode
|
32
|
+
:implode => 0.2,
|
33
|
+
:letters => {
|
34
|
+
# text baseline (px)
|
35
|
+
:baseline => 24,
|
36
|
+
# number of letters in captcha
|
37
|
+
:count => 6,
|
38
|
+
:ignore => ['a','e','i','o','u','l','j','q'],
|
39
|
+
# font size (pts)
|
40
|
+
:points => 34,
|
41
|
+
# width of a character (used to decrease or increase space between characters) (px)
|
42
|
+
:width => 14
|
43
|
+
},
|
44
|
+
:ttf => File.expand_path("#{File.dirname(__FILE__)}/../../resources/captcha.ttf"),
|
45
|
+
# http://www.imagemagick.org/RMagick/doc/image3.html#wave
|
46
|
+
:wave => {
|
47
|
+
# range is used for randomness (px)
|
48
|
+
:wavelength => (20..70),
|
49
|
+
# distance between peak and valley of sin wave (px)
|
50
|
+
:amplitude => 3
|
51
|
+
}
|
52
|
+
}
|
53
|
+
</pre>
|
15
54
|
|
16
|
-
|
17
|
-
|
18
|
-
### Generate captchas
|
55
|
+
### Create a captcha controller
|
19
56
|
|
20
|
-
|
57
|
+
script/generate controller Captchas
|
58
|
+
|
59
|
+
### Add the resource to your routes.rb
|
60
|
+
|
61
|
+
map.resource :captcha
|
62
|
+
|
63
|
+
### Add acts\_as\_captcha to captchas_controller.rb
|
64
|
+
|
65
|
+
<pre>
|
66
|
+
class CaptchasController < ApplicationController
|
67
|
+
acts_as_captcha
|
68
|
+
end
|
69
|
+
</pre>
|
21
70
|
|
22
|
-
### Add to .gitignore
|
71
|
+
### Add public/images/captchas/* to your .gitignore
|
23
72
|
|
24
|
-
|
73
|
+
public/images/captchas/*
|
25
74
|
|
75
|
+
Captchas will generate and re-generate automatically (see the <code>generate_every</code> option).
|
26
76
|
|
27
|
-
|
77
|
+
Access <code>/captcha</code> to retrieve the image, <code>/captcha/new</code> to grab a new image, and <code>session[:captcha]</code> for the captcha code.
|
data/captcha.gemspec
CHANGED
data/lib/captcha/action.rb
CHANGED
@@ -8,33 +8,23 @@ module Captcha
|
|
8
8
|
module ClassMethods
|
9
9
|
def acts_as_captcha
|
10
10
|
include Captcha::Action::InstanceMethods
|
11
|
-
self.around_filter CaptchaFilter.new
|
12
11
|
end
|
13
12
|
end
|
14
13
|
|
15
14
|
module InstanceMethods
|
16
15
|
def new
|
17
|
-
files = Dir["#{
|
18
|
-
|
19
|
-
show
|
16
|
+
files = Dir["#{Captcha::Config.options[:destination]}/*.jpg"]
|
17
|
+
session[:captcha] = File.basename(files[rand(files.length)], '.jpg')
|
18
|
+
redirect_to(:action => :show)
|
20
19
|
end
|
21
20
|
|
22
21
|
def show
|
23
|
-
new unless
|
24
|
-
send_file
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
class CaptchaFilter
|
31
|
-
def before(controller)
|
32
|
-
@captcha = session[:captcha]
|
33
|
-
end
|
34
|
-
def after(controller)
|
35
|
-
if session[:captcha] != @captcha
|
36
|
-
session[:captcha] = @captcha
|
37
|
-
end
|
22
|
+
new and return unless session[:captcha]
|
23
|
+
send_file(
|
24
|
+
"#{Captcha::Config.options[:destination]}/#{session[:captcha]}.jpg",
|
25
|
+
:disposition => 'inline',
|
26
|
+
:type => 'image/jpeg'
|
27
|
+
)
|
38
28
|
end
|
39
29
|
end
|
40
30
|
|
data/lib/captcha/config.rb
CHANGED
@@ -1,46 +1,49 @@
|
|
1
1
|
module Captcha
|
2
2
|
class Config
|
3
|
+
|
4
|
+
PRODUCTION = defined?(RAILS_ENV) && RAILS_ENV == 'production'
|
5
|
+
ROOT = defined?(RAILS_ROOT) ? "#{RAILS_ROOT}/" : ''
|
6
|
+
|
7
|
+
@@options = {
|
8
|
+
:colors => {
|
9
|
+
:background => '#1B1C20',
|
10
|
+
:font => '#7385C5'
|
11
|
+
},
|
12
|
+
# number of captcha images to generate
|
13
|
+
:count => PRODUCTION ? 500 : 10,
|
14
|
+
:destination => "#{ROOT}public/images/captchas",
|
15
|
+
:dimensions => {
|
16
|
+
# canvas height (px)
|
17
|
+
:height => 31,
|
18
|
+
# canvas width (px)
|
19
|
+
:width => 90
|
20
|
+
},
|
21
|
+
:generate_every => PRODUCTION ? 24 * 60 * 60 : 10 ** 8,
|
22
|
+
# http://www.imagemagick.org/RMagick/doc/image2.html#implode
|
23
|
+
:implode => 0.2,
|
24
|
+
:letters => {
|
25
|
+
# text baseline (px)
|
26
|
+
:baseline => 24,
|
27
|
+
# number of letters in captcha
|
28
|
+
:count => 6,
|
29
|
+
:ignore => ['a','e','i','o','u','l','j','q'],
|
30
|
+
# font size (pts)
|
31
|
+
:points => 34,
|
32
|
+
# width of a character (used to decrease or increase space between characters) (px)
|
33
|
+
:width => 14
|
34
|
+
},
|
35
|
+
:ttf => File.expand_path("#{File.dirname(__FILE__)}/../../resources/captcha.ttf"),
|
36
|
+
# http://www.imagemagick.org/RMagick/doc/image3.html#wave
|
37
|
+
:wave => {
|
38
|
+
# range is used for randomness (px)
|
39
|
+
:wavelength => (20..70),
|
40
|
+
# distance between peak and valley of sin wave (px)
|
41
|
+
:amplitude => 3
|
42
|
+
}
|
43
|
+
}
|
44
|
+
|
3
45
|
def initialize(options={})
|
4
|
-
@@options
|
5
|
-
:colors => {
|
6
|
-
:background => '#1B1C20',
|
7
|
-
:font => '#7385C5'
|
8
|
-
},
|
9
|
-
# number of captcha images to generate
|
10
|
-
:count => 500,
|
11
|
-
:destination => File.expand_path(
|
12
|
-
(defined?(RAILS_ROOT) ? "#{RAILS_ROOT}/" : '') +
|
13
|
-
"public/images/captchas"
|
14
|
-
),
|
15
|
-
:dimensions => {
|
16
|
-
# canvas height (px)
|
17
|
-
:height => 31,
|
18
|
-
# canvas width (px)
|
19
|
-
:width => 90
|
20
|
-
},
|
21
|
-
:generate_every => 24 * 60 * 60,
|
22
|
-
# http://www.imagemagick.org/RMagick/doc/image2.html#implode
|
23
|
-
:implode => 0.2,
|
24
|
-
:letters => {
|
25
|
-
# text baseline (px)
|
26
|
-
:baseline => 24,
|
27
|
-
# number of letters in captcha
|
28
|
-
:count => 6,
|
29
|
-
:ignore => ['a','e','i','o','u','l','j','q'],
|
30
|
-
# font size (pts)
|
31
|
-
:points => 34,
|
32
|
-
# width of a character (used to decrease or increase space between characters) (px)
|
33
|
-
:width => 14
|
34
|
-
},
|
35
|
-
:ttf => File.expand_path("#{File.dirname(__FILE__)}/../../resources/captcha.ttf"),
|
36
|
-
# http://www.imagemagick.org/RMagick/doc/image3.html#wave
|
37
|
-
:wave => {
|
38
|
-
# range is used for randomness (px)
|
39
|
-
:wavelength => (20..70),
|
40
|
-
# distance between peak and valley of sin wave (px)
|
41
|
-
:amplitude => 3
|
42
|
-
}
|
43
|
-
}.merge(options)
|
46
|
+
@@options.merge!(options)
|
44
47
|
end
|
45
48
|
|
46
49
|
def self.captchas_ordered_by_modified
|
metadata
CHANGED
data/resources/captchas.rb
DELETED
@@ -1,24 +0,0 @@
|
|
1
|
-
{
|
2
|
-
:count => 100, # number of captcha images to generate
|
3
|
-
:width => 90, # canvas width (px)
|
4
|
-
:height => 31, # canvas height (px)
|
5
|
-
:colors => {
|
6
|
-
:background => '#1B1C20',
|
7
|
-
:font => '#7385C5'
|
8
|
-
},
|
9
|
-
:letters => {
|
10
|
-
:baseline => 24,# text baseline (px)
|
11
|
-
:count => 6, # number of letters in captcha
|
12
|
-
:ignore => ['a','e','i','o','u','l','j','q'],
|
13
|
-
:points => 34, # font size (pts)
|
14
|
-
:width => 14, # width of a character (used to decrease or increase space between characters) (px)
|
15
|
-
},
|
16
|
-
:implode => 0.2, # http://www.imagemagick.org/RMagick/doc/image2.html#implode
|
17
|
-
:wave => { # http://www.imagemagick.org/RMagick/doc/image3.html#wave
|
18
|
-
:wavelength => (20..70), # range is used for randomness (px)
|
19
|
-
:amplitude => 3 # distance between peak and valley of sin wave (px)
|
20
|
-
},
|
21
|
-
:generate_on_startup => RAILS_ENV == 'production',
|
22
|
-
:destination => 'tmp/captchas',
|
23
|
-
:ttf => 'vendor/plugins/captcha/resources/captcha.ttf'
|
24
|
-
}
|
data/tasks/captcha.rake
DELETED
@@ -1,55 +0,0 @@
|
|
1
|
-
require File.expand_path('../lib/captcha/captcha.rb', File.dirname(__FILE__))
|
2
|
-
|
3
|
-
desc 'Updates config/captcha.rb'
|
4
|
-
task :captcha => 'captcha:config'
|
5
|
-
|
6
|
-
namespace :captcha do
|
7
|
-
|
8
|
-
desc 'Updates config/captcha.rb'
|
9
|
-
task :config => 'captcha:config:to_app'
|
10
|
-
|
11
|
-
desc 'Generates captchas'
|
12
|
-
task :generate do
|
13
|
-
Captchas.new
|
14
|
-
end
|
15
|
-
|
16
|
-
namespace :config do
|
17
|
-
desc 'Copies plugin resources to app'
|
18
|
-
task :to_app do
|
19
|
-
captcha_resource 'captchas.rb', 'config/captchas.rb'
|
20
|
-
end
|
21
|
-
|
22
|
-
desc 'Copies app resources to plugin'
|
23
|
-
task :to_plugin do
|
24
|
-
captcha_resource 'captchas.rb', 'config/captchas.rb', true
|
25
|
-
end
|
26
|
-
|
27
|
-
desc 'Removes plugin resources from app'
|
28
|
-
task :remove do
|
29
|
-
rm_rf 'config/captchas.rb'
|
30
|
-
end
|
31
|
-
end
|
32
|
-
|
33
|
-
def captcha_resource(type, to, reverse=false, overwrite=true)
|
34
|
-
from = "#{File.dirname(__FILE__)}/../resources/#{type}"
|
35
|
-
from, to = to, from if reverse
|
36
|
-
puts "=> Removing old #{type}..."
|
37
|
-
puts to
|
38
|
-
return if File.exists?(to) && !overwrite
|
39
|
-
if File.directory?(from)
|
40
|
-
FileUtils.remove_dir(to, true) if File.exists?(to)
|
41
|
-
FileUtils.mkdir_p to
|
42
|
-
else
|
43
|
-
File.unlink(to) if File.exists?(to)
|
44
|
-
end
|
45
|
-
puts "=> Copying #{type}..."
|
46
|
-
(File.directory?(from) ? Dir["#{from}/*"] : [from]).each do |f|
|
47
|
-
if File.directory? f
|
48
|
-
FileUtils.mkdir_p "#{to}/#{File.basename(f)}"
|
49
|
-
FileUtils.cp_r f, to
|
50
|
-
else
|
51
|
-
FileUtils.cp f, to
|
52
|
-
end
|
53
|
-
end if File.exists?(from)
|
54
|
-
end
|
55
|
-
end
|